Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / dbdes.c @ 48e5f32d

History | View | Annotate | Download (10.7 KB)

1 4364b47e Ondrej Filip
/*
2
 *        BIRD -- OSPF
3
 *
4 f158bb71 Ondrej Filip
 *        (c) 1999--2004 Ondrej Filip <feela@network.cz>
5 4364b47e Ondrej Filip
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9
#include "ospf.h"
10
11 c3226991 Ondrej Zajicek
12
#ifdef OSPFv2
13
struct ospf_dbdes_packet
14
{
15
  struct ospf_packet ospf_packet;
16
  u16 iface_mtu;
17
  u8 options;
18
  union imms imms;                /* I, M, MS bits */
19
  u32 ddseq;
20
};
21
22
#define hton_opt(X) X
23
#define ntoh_opt(X) X
24
#endif
25
26
27
#ifdef OSPFv3
28
struct ospf_dbdes_packet
29
{
30
  struct ospf_packet ospf_packet;
31
  u32 options;
32
  u16 iface_mtu;
33
  u8 padding;
34
  union imms imms;                /* I, M, MS bits */
35
  u32 ddseq;
36
};
37
38
#define hton_opt(X) htonl(X)
39
#define ntoh_opt(X) ntohl(X)
40
#endif
41
42 8298d780 Ondrej Zajicek
  
43
static void ospf_dump_dbdes(struct proto *p, struct ospf_dbdes_packet *pkt)
44
{
45
  struct ospf_packet *op = &pkt->ospf_packet;
46
47
  ASSERT(op->type == DBDES_P);
48
  ospf_dump_common(p, op);
49
  log(L_TRACE "%s:     imms     %s%s%s",
50
      p->name, pkt->imms.bit.ms ? "MS " : "",
51
      pkt->imms.bit.m ? "M " : "",
52
      pkt->imms.bit.i ? "I " : "" );
53
  log(L_TRACE "%s:     ddseq    %u", p->name, ntohl(pkt->ddseq));
54
55
  struct ospf_lsa_header *plsa = (void *) (pkt + 1);
56 a6bc04d5 Ondrej Zajicek
  unsigned int i, j;
57 8298d780 Ondrej Zajicek
58
  j = (ntohs(op->length) - sizeof(struct ospf_dbdes_packet)) /
59
    sizeof(struct ospf_lsa_header);
60
61
  for (i = 0; i < j; i++)
62
    ospf_dump_lsahdr(p, plsa + i);
63
}
64
65
66 eee9cefd Ondrej Filip
/**
67 12bed559 Ondrej Filip
 * ospf_dbdes_send - transmit database description packet
68 eee9cefd Ondrej Filip
 * @n: neighbor
69 b722fe7e Ondrej Zajicek
 * @next: whether to send a next packet in a sequence (1) or to retransmit the old one (0)
70 eee9cefd Ondrej Filip
 *
71 c3226991 Ondrej Zajicek
 * Sending of a database description packet is described in 10.8 of RFC 2328.
72 baa5dd6c Ondrej Filip
 * Reception of each packet is acknowledged in the sequence number of another.
73
 * When I send a packet to a neighbor I keep a copy in a buffer. If the neighbor
74
 * does not reply, I don't create a new packet but just send the content
75
 * of the buffer.
76 eee9cefd Ondrej Filip
 */
77 4364b47e Ondrej Filip
void
78 b722fe7e Ondrej Zajicek
ospf_dbdes_send(struct ospf_neighbor *n, int next)
79 4364b47e Ondrej Filip
{
80
  struct ospf_dbdes_packet *pkt;
81
  struct ospf_packet *op;
82 12bed559 Ondrej Filip
  struct ospf_iface *ifa = n->ifa;
83
  struct ospf_area *oa = ifa->oa;
84 86c84d76 Ondrej Filip
  struct proto_ospf *po = oa->po;
85
  struct proto *p = &po->proto;
86
  u16 length, i, j;
87 4364b47e Ondrej Filip
88 b49e6f5a Ondrej Zajicek
  /* FIXME ??? */
89 86c84d76 Ondrej Filip
  if ((oa->rt == NULL) || (EMPTY_LIST(po->lsal)))
90 b49e6f5a Ondrej Zajicek
    update_rt_lsa(oa);
91 054a3524 Ondrej Filip
92 12bed559 Ondrej Filip
  switch (n->state)
93 4364b47e Ondrej Filip
  {
94 12bed559 Ondrej Filip
  case NEIGHBOR_EXSTART:        /* Send empty packets */
95
    n->myimms.bit.i = 1;
96 e7b76b97 Ondrej Zajicek
    pkt = ospf_tx_buffer(ifa);
97 353729f5 Ondrej Zajicek
    op = &pkt->ospf_packet;
98 3e2bd0f1 Ondrej Filip
    ospf_pkt_fill_hdr(ifa, pkt, DBDES_P);
99 9f1500f5 Ondrej Zajicek
    pkt->iface_mtu = (ifa->type == OSPF_IT_VLINK) ? 0 : htons(ifa->iface->mtu);
100 c3226991 Ondrej Zajicek
    pkt->options = hton_opt(oa->options);
101 12bed559 Ondrej Filip
    pkt->imms = n->myimms;
102
    pkt->ddseq = htonl(n->dds);
103
    length = sizeof(struct ospf_dbdes_packet);
104
    op->length = htons(length);
105 8298d780 Ondrej Zajicek
106 48e5f32d Ondrej Zajicek
    OSPF_PACKET(ospf_dump_dbdes, pkt, "DBDES packet sent to %I via %s", n->ip, ifa->ifname);
107 f9c799a0 Ondrej Zajicek
    ospf_send_to(ifa, n->ip);
108 12bed559 Ondrej Filip
    break;
109
110
  case NEIGHBOR_EXCHANGE:
111
    n->myimms.bit.i = 0;
112
113 b722fe7e Ondrej Zajicek
    if (next)
114 12bed559 Ondrej Filip
    {
115 b722fe7e Ondrej Zajicek
      snode *sn;
116 12bed559 Ondrej Filip
      struct ospf_lsa_header *lsa;
117
118 48e5f32d Ondrej Zajicek
      if (n->ldd_bsize != ifa->tx_length)
119
      {
120
        mb_free(n->ldd_buffer);
121
        n->ldd_buffer = mb_allocz(n->pool, ifa->tx_length);
122
        n->ldd_bsize = ifa->tx_length;
123
      }
124
125
      pkt = n->ldd_buffer;
126 12bed559 Ondrej Filip
      op = (struct ospf_packet *) pkt;
127
128 3e2bd0f1 Ondrej Filip
      ospf_pkt_fill_hdr(ifa, pkt, DBDES_P);
129 9f1500f5 Ondrej Zajicek
      pkt->iface_mtu = (ifa->type == OSPF_IT_VLINK) ? 0 : htons(ifa->iface->mtu);
130 12bed559 Ondrej Filip
      pkt->ddseq = htonl(n->dds);
131 c3226991 Ondrej Zajicek
      pkt->options = hton_opt(oa->options);
132 30147b89 Ondrej Filip
133 3e2bd0f1 Ondrej Filip
      j = i = (ospf_pkt_maxsize(ifa) - sizeof(struct ospf_dbdes_packet)) / sizeof(struct ospf_lsa_header);        /* Number of possible lsaheaders to send */
134 48e5f32d Ondrej Zajicek
      lsa = (n->ldd_buffer + sizeof(struct ospf_dbdes_packet));
135 781aa475 Ondrej Filip
136 12bed559 Ondrej Filip
      if (n->myimms.bit.m)
137 30147b89 Ondrej Filip
      {
138 12bed559 Ondrej Filip
        sn = s_get(&(n->dbsi));
139
140
        DBG("Number of LSA: %d\n", j);
141
        for (; i > 0; i--)
142 910e557b Ondrej Filip
        {
143 86c84d76 Ondrej Filip
          struct top_hash_entry *en= (struct top_hash_entry *) sn;
144 98ac6176 Ondrej Filip
145 c3226991 Ondrej Zajicek
          if (ospf_lsa_flooding_allowed(&en->lsa, en->domain, ifa))
146 98ac6176 Ondrej Filip
          {
147
            htonlsah(&(en->lsa), lsa);
148
            DBG("Working on: %d\n", i);
149 3aab39f5 Ondrej Zajicek
            DBG("\tX%01x %-1R %-1R %p\n", en->lsa.type, en->lsa.id, en->lsa.rt, en->lsa_body);
150 98ac6176 Ondrej Filip
151
            lsa++;
152
          }
153
          else i++;        /* No lsa added */
154 963ea03d Ondrej Filip
155 86c84d76 Ondrej Filip
          if (sn == STAIL(po->lsal))
156 98ac6176 Ondrej Filip
          {
157
            i--;
158
            break;
159
          }
160
161 12bed559 Ondrej Filip
          sn = sn->next;
162 910e557b Ondrej Filip
        }
163 963ea03d Ondrej Filip
164 86c84d76 Ondrej Filip
        if (sn == STAIL(po->lsal))
165 12bed559 Ondrej Filip
        {
166
          DBG("Number of LSA NOT sent: %d\n", i);
167
          DBG("M bit unset.\n");
168
          n->myimms.bit.m = 0;        /* Unset more bit */
169
        }
170 0844b65d Ondrej Zajicek
171
        s_put(&(n->dbsi), sn);
172 30147b89 Ondrej Filip
      }
173
174 12bed559 Ondrej Filip
      pkt->imms.byte = n->myimms.byte;
175 30147b89 Ondrej Filip
176 12bed559 Ondrej Filip
      length = (j - i) * sizeof(struct ospf_lsa_header) +
177
        sizeof(struct ospf_dbdes_packet);
178
      op->length = htons(length);
179
180
      DBG("%s: DB_DES (M) prepared for %I.\n", p->name, n->ip);
181
    }
182
183
  case NEIGHBOR_LOADING:
184
  case NEIGHBOR_FULL:
185 48e5f32d Ondrej Zajicek
    length = n->ldd_buffer ? ntohs(((struct ospf_packet *) n->ldd_buffer)->length) : 0;
186 12bed559 Ondrej Filip
187 2e10a170 Ondrej Filip
    if (!length)
188 e7ef86a5 Ondrej Filip
    {
189
      OSPF_TRACE(D_PACKETS, "No packet in my buffer for repeating");
190
      ospf_neigh_sm(n, INM_KILLNBR);
191 551e3088 Ondrej Filip
      return;
192 e7ef86a5 Ondrej Filip
    }
193
194 48e5f32d Ondrej Zajicek
    /* Send last packet from ldd buffer */
195 04c6319a Ondrej Filip
196 48e5f32d Ondrej Zajicek
    OSPF_PACKET(ospf_dump_dbdes, n->ldd_buffer, "DBDES packet sent to %I via %s", n->ip, ifa->ifname);
197
198
    sk_set_tbuf(ifa->sk, n->ldd_buffer);
199 f9c799a0 Ondrej Zajicek
    ospf_send_to(ifa, n->ip);
200 48e5f32d Ondrej Zajicek
    sk_set_tbuf(ifa->sk, NULL);
201 b1f7229a Ondrej Filip
202
    if(n->myimms.bit.ms) tm_start(n->rxmt_timer, n->ifa->rxmtint);                /* Restart timer */
203
204 12bed559 Ondrej Filip
    if (!n->myimms.bit.ms)
205
    {
206
      if ((n->myimms.bit.m == 0) && (n->imms.bit.m == 0) &&
207
          (n->state == NEIGHBOR_EXCHANGE))
208 910e557b Ondrej Filip
      {
209 12bed559 Ondrej Filip
        ospf_neigh_sm(n, INM_EXDONE);
210 910e557b Ondrej Filip
      }
211 12bed559 Ondrej Filip
    }
212
    break;
213 4364b47e Ondrej Filip
214 12bed559 Ondrej Filip
  default:                        /* Ignore it */
215
    break;
216 4364b47e Ondrej Filip
  }
217
}
218
219 9831e591 Martin Mares
static void
220 12bed559 Ondrej Filip
ospf_dbdes_reqladd(struct ospf_dbdes_packet *ps, struct ospf_neighbor *n)
221 910e557b Ondrej Filip
{
222 12bed559 Ondrej Filip
  struct ospf_lsa_header *plsa, lsa;
223
  struct top_hash_entry *he, *sn;
224 86c84d76 Ondrej Filip
  struct ospf_area *oa = n->ifa->oa;
225
  struct top_graph *gr = oa->po->gr;
226 6d2b3211 Ondrej Filip
  struct ospf_packet *op;
227 12bed559 Ondrej Filip
  int i, j;
228 910e557b Ondrej Filip
229 12bed559 Ondrej Filip
  op = (struct ospf_packet *) ps;
230 910e557b Ondrej Filip
231 12bed559 Ondrej Filip
  plsa = (void *) (ps + 1);
232 6d2b3211 Ondrej Filip
233 12bed559 Ondrej Filip
  j = (ntohs(op->length) - sizeof(struct ospf_dbdes_packet)) /
234
    sizeof(struct ospf_lsa_header);
235 6d2b3211 Ondrej Filip
236 12bed559 Ondrej Filip
  for (i = 0; i < j; i++)
237 6d2b3211 Ondrej Filip
  {
238 12bed559 Ondrej Filip
    ntohlsah(plsa + i, &lsa);
239 b49e6f5a Ondrej Zajicek
    u32 dom = ospf_lsa_domain(lsa.type, n->ifa);
240
    if (((he = ospf_hash_find_header(gr, dom, &lsa)) == NULL) ||
241 12bed559 Ondrej Filip
        (lsa_comp(&lsa, &(he->lsa)) == 1))
242 95eb1dba Ondrej Filip
    {
243 394acced Ondrej Filip
      /* Is this condition necessary? */
244 b49e6f5a Ondrej Zajicek
      if (ospf_hash_find_header(n->lsrqh, dom, &lsa) == NULL)
245 95eb1dba Ondrej Filip
      {
246 b49e6f5a Ondrej Zajicek
        sn = ospf_hash_get_header(n->lsrqh, dom, &lsa);
247 12bed559 Ondrej Filip
        ntohlsah(plsa + i, &(sn->lsa));
248
        s_add_tail(&(n->lsrql), SNODE sn);
249 95eb1dba Ondrej Filip
      }
250
    }
251 6d2b3211 Ondrej Filip
  }
252 4364b47e Ondrej Filip
}
253
254
void
255 c3226991 Ondrej Zajicek
ospf_dbdes_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa,
256
                   struct ospf_neighbor *n)
257 4364b47e Ondrej Filip
{
258 8a70a13e Ondrej Zajicek
  struct proto_ospf *po = ifa->oa->po;
259
  struct proto *p = &po->proto;
260 4364b47e Ondrej Filip
261 a6bc04d5 Ondrej Zajicek
  unsigned int size = ntohs(ps_i->length);
262
  if (size < sizeof(struct ospf_dbdes_packet))
263
  {
264
    log(L_ERR "Bad OSPF DBDES packet from %I -  too short (%u B)", n->ip, size);
265
    return;
266
  }
267
268
  struct ospf_dbdes_packet *ps = (void *) ps_i;
269 c3226991 Ondrej Zajicek
  u32 ps_ddseq = ntohl(ps->ddseq);
270
  u32 ps_options = ntoh_opt(ps->options);
271 9f1500f5 Ondrej Zajicek
  u16 ps_iface_mtu = ntohs(ps->iface_mtu);
272 c3226991 Ondrej Zajicek
  
273 48e5f32d Ondrej Zajicek
  OSPF_PACKET(ospf_dump_dbdes, ps, "DBDES packet received from %I via %s", n->ip, ifa->ifname);
274 b1f7229a Ondrej Filip
275 4bb9ce56 Ondrej Filip
  ospf_neigh_sm(n, INM_HELLOREC);
276 353f3261 Ondrej Filip
277 12bed559 Ondrej Filip
  switch (n->state)
278 4364b47e Ondrej Filip
  {
279 12bed559 Ondrej Filip
  case NEIGHBOR_DOWN:
280
  case NEIGHBOR_ATTEMPT:
281
  case NEIGHBOR_2WAY:
282
    return;
283
    break;
284
  case NEIGHBOR_INIT:
285
    ospf_neigh_sm(n, INM_2WAYREC);
286
    if (n->state != NEIGHBOR_EXSTART)
287
      return;
288
  case NEIGHBOR_EXSTART:
289 9f1500f5 Ondrej Zajicek
290 48e5f32d Ondrej Zajicek
    if ((ifa->type != OSPF_IT_VLINK) && (ps_iface_mtu != ifa->iface->mtu)
291 9f1500f5 Ondrej Zajicek
        && (ps_iface_mtu != 0) && (ifa->iface->mtu != 0))
292
      log(L_WARN "OSPF: MTU mismatch with neighbour %I on interface %s (remote %d, local %d)",
293 48e5f32d Ondrej Zajicek
          n->ip, ifa->ifname, ps_iface_mtu, ifa->iface->mtu);
294 9f1500f5 Ondrej Zajicek
295 12bed559 Ondrej Filip
    if ((ps->imms.bit.m && ps->imms.bit.ms && ps->imms.bit.i)
296 8a70a13e Ondrej Zajicek
        && (n->rid > po->router_id) && (size == sizeof(struct ospf_dbdes_packet)))
297 12bed559 Ondrej Filip
    {
298
      /* I'm slave! */
299 c3226991 Ondrej Zajicek
      n->dds = ps_ddseq;
300
      n->ddr = ps_ddseq;
301
      n->options = ps_options;
302 12bed559 Ondrej Filip
      n->myimms.bit.ms = 0;
303
      n->imms.byte = ps->imms.byte;
304
      OSPF_TRACE(D_PACKETS, "I'm slave to %I.", n->ip);
305
      ospf_neigh_sm(n, INM_NEGDONE);
306 b722fe7e Ondrej Zajicek
      ospf_dbdes_send(n, 1);
307 4364b47e Ondrej Filip
      break;
308 12bed559 Ondrej Filip
    }
309 b1f7229a Ondrej Filip
310
    if (((ps->imms.bit.i == 0) && (ps->imms.bit.ms == 0)) &&
311 8a70a13e Ondrej Zajicek
        (n->rid < po->router_id) && (n->dds == ps_ddseq))
312 12bed559 Ondrej Filip
    {
313 b1f7229a Ondrej Filip
      /* I'm master! */
314 c3226991 Ondrej Zajicek
      n->options = ps_options;
315
      n->ddr = ps_ddseq - 1;        /* It will be set corectly a few lines down */
316 b1f7229a Ondrej Filip
      n->imms.byte = ps->imms.byte;
317
      OSPF_TRACE(D_PACKETS, "I'm master to %I.", n->ip);
318
      ospf_neigh_sm(n, INM_NEGDONE);
319 12bed559 Ondrej Filip
    }
320 b1f7229a Ondrej Filip
    else
321
    {
322
      DBG("%s: Nothing happend to %I (imms=%u)\n", p->name, n->ip,
323
          ps->imms.byte);
324 12bed559 Ondrej Filip
      break;
325 b1f7229a Ondrej Filip
    }
326 12bed559 Ondrej Filip
  case NEIGHBOR_EXCHANGE:
327 c3226991 Ondrej Zajicek
    if ((ps->imms.byte == n->imms.byte) && (ps_options == n->options) &&
328
        (ps_ddseq == n->ddr))
329 12bed559 Ondrej Filip
    {
330
      /* Duplicate packet */
331
      OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I.", n->ip);
332 d901db3f Ondrej Zajicek
      if (n->myimms.bit.ms == 0)
333 12bed559 Ondrej Filip
      {
334 d901db3f Ondrej Zajicek
        /* Slave should retransmit dbdes packet */
335 b722fe7e Ondrej Zajicek
        ospf_dbdes_send(n, 0);
336 12bed559 Ondrej Filip
      }
337
      return;
338
    }
339 910e557b Ondrej Filip
340 c3226991 Ondrej Zajicek
    n->ddr = ps_ddseq;
341 4364b47e Ondrej Filip
342 12bed559 Ondrej Filip
    if (ps->imms.bit.ms != n->imms.bit.ms)        /* M/S bit differs */
343
    {
344
      OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (bit MS)",
345
                 n->ip);
346
      ospf_neigh_sm(n, INM_SEQMIS);
347 4364b47e Ondrej Filip
      break;
348 12bed559 Ondrej Filip
    }
349
350
    if (ps->imms.bit.i)                /* I bit is set */
351
    {
352
      OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (bit I)",
353
                 n->ip);
354
      ospf_neigh_sm(n, INM_SEQMIS);
355 4364b47e Ondrej Filip
      break;
356 12bed559 Ondrej Filip
    }
357
358
    n->imms.byte = ps->imms.byte;
359
360 c3226991 Ondrej Zajicek
    if (ps_options != n->options)        /* Options differs */
361 12bed559 Ondrej Filip
    {
362
      OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (options)",
363
                 n->ip);
364
      ospf_neigh_sm(n, INM_SEQMIS);
365 4364b47e Ondrej Filip
      break;
366 12bed559 Ondrej Filip
    }
367 4364b47e Ondrej Filip
368 12bed559 Ondrej Filip
    if (n->myimms.bit.ms)
369
    {
370 c3226991 Ondrej Zajicek
      if (ps_ddseq != n->dds)        /* MASTER */
371 12bed559 Ondrej Filip
      {
372 48e5f32d Ondrej Zajicek
        OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (master)",
373
                   n->ip);
374 12bed559 Ondrej Filip
        ospf_neigh_sm(n, INM_SEQMIS);
375
        break;
376
      }
377
      n->dds++;
378
      DBG("Incrementing dds\n");
379
      ospf_dbdes_reqladd(ps, n);
380
      if ((n->myimms.bit.m == 0) && (ps->imms.bit.m == 0))
381
      {
382
        ospf_neigh_sm(n, INM_EXDONE);
383
      }
384
      else
385
      {
386 b722fe7e Ondrej Zajicek
        ospf_dbdes_send(n, 1);
387 12bed559 Ondrej Filip
      }
388
389
    }
390
    else
391
    {
392 c3226991 Ondrej Zajicek
      if (ps_ddseq != (n->dds + 1))        /* SLAVE */
393 12bed559 Ondrej Filip
      {
394 48e5f32d Ondrej Zajicek
        OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (slave)", n->ip);
395 12bed559 Ondrej Filip
        ospf_neigh_sm(n, INM_SEQMIS);
396
        break;
397
      }
398 c3226991 Ondrej Zajicek
      n->ddr = ps_ddseq;
399
      n->dds = ps_ddseq;
400 12bed559 Ondrej Filip
      ospf_dbdes_reqladd(ps, n);
401 b722fe7e Ondrej Zajicek
      ospf_dbdes_send(n, 1);
402 12bed559 Ondrej Filip
    }
403
404
    break;
405
  case NEIGHBOR_LOADING:
406
  case NEIGHBOR_FULL:
407 c3226991 Ondrej Zajicek
    if ((ps->imms.byte == n->imms.byte) && (ps_options == n->options)
408
        && (ps_ddseq == n->ddr))
409 12bed559 Ondrej Filip
      /* Only duplicate are accepted */
410
    {
411
      OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I.", n->ip);
412 a9dc5e1e Ondrej Zajicek
      if (n->myimms.bit.ms == 0)
413
      {
414
        /* Slave should retransmit dbdes packet */
415 b722fe7e Ondrej Zajicek
        ospf_dbdes_send(n, 0);
416 a9dc5e1e Ondrej Zajicek
      }
417 12bed559 Ondrej Filip
      return;
418
    }
419
    else
420
    {
421
      OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (full)",
422
                 n->ip);
423 c3226991 Ondrej Zajicek
      DBG("PS=%u, DDR=%u, DDS=%u\n", ps_ddseq, n->ddr, n->dds);
424 12bed559 Ondrej Filip
      ospf_neigh_sm(n, INM_SEQMIS);
425
    }
426
    break;
427 189dab54 Martin Mares
  default:
428 12bed559 Ondrej Filip
    bug("Received dbdes from %I in undefined state.", n->ip);
429
  }
430
}