Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / dbdes.c @ 9f1500f5

History | View | Annotate | Download (10.6 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999--2004 Ondrej Filip <feela@network.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#include "ospf.h"
10

    
11

    
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
  
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
  unsigned int i, j;
57

    
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
/**
67
 * ospf_dbdes_send - transmit database description packet
68
 * @n: neighbor
69
 * @next: whether to send a next packet in a sequence (1) or to retransmit the old one (0)
70
 *
71
 * Sending of a database description packet is described in 10.8 of RFC 2328.
72
 * 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
 */
77
void
78
ospf_dbdes_send(struct ospf_neighbor *n, int next)
79
{
80
  struct ospf_dbdes_packet *pkt;
81
  struct ospf_packet *op;
82
  struct ospf_iface *ifa = n->ifa;
83
  struct ospf_area *oa = ifa->oa;
84
  struct proto_ospf *po = oa->po;
85
  struct proto *p = &po->proto;
86
  u16 length, i, j;
87

    
88
  /* FIXME ??? */
89
  if ((oa->rt == NULL) || (EMPTY_LIST(po->lsal)))
90
    update_rt_lsa(oa);
91

    
92
  switch (n->state)
93
  {
94
  case NEIGHBOR_EXSTART:        /* Send empty packets */
95
    n->myimms.bit.i = 1;
96
    pkt = ospf_tx_buffer(ifa);
97
    op = &pkt->ospf_packet;
98
    ospf_pkt_fill_hdr(ifa, pkt, DBDES_P);
99
    pkt->iface_mtu = (ifa->type == OSPF_IT_VLINK) ? 0 : htons(ifa->iface->mtu);
100
    pkt->options = hton_opt(oa->options);
101
    pkt->imms = n->myimms;
102
    pkt->ddseq = htonl(n->dds);
103
    length = sizeof(struct ospf_dbdes_packet);
104
    op->length = htons(length);
105

    
106
    OSPF_PACKET(ospf_dump_dbdes, pkt, "DBDES packet sent to %I via %s", n->ip, ifa->iface->name);
107
    ospf_send_to(ifa, n->ip);
108
    break;
109

    
110
  case NEIGHBOR_EXCHANGE:
111
    n->myimms.bit.i = 0;
112

    
113
    if (next)
114
    {
115
      snode *sn;
116
      struct ospf_lsa_header *lsa;
117

    
118
      pkt = n->ldbdes;
119
      op = (struct ospf_packet *) pkt;
120

    
121
      ospf_pkt_fill_hdr(ifa, pkt, DBDES_P);
122
      pkt->iface_mtu = (ifa->type == OSPF_IT_VLINK) ? 0 : htons(ifa->iface->mtu);
123
      pkt->ddseq = htonl(n->dds);
124
      pkt->options = hton_opt(oa->options);
125

    
126
      j = i = (ospf_pkt_maxsize(ifa) - sizeof(struct ospf_dbdes_packet)) / sizeof(struct ospf_lsa_header);        /* Number of possible lsaheaders to send */
127
      lsa = (n->ldbdes + sizeof(struct ospf_dbdes_packet));
128

    
129
      if (n->myimms.bit.m)
130
      {
131
        sn = s_get(&(n->dbsi));
132

    
133
        DBG("Number of LSA: %d\n", j);
134
        for (; i > 0; i--)
135
        {
136
          struct top_hash_entry *en= (struct top_hash_entry *) sn;
137

    
138
          if (ospf_lsa_flooding_allowed(&en->lsa, en->domain, ifa))
139
          {
140
            htonlsah(&(en->lsa), lsa);
141
            DBG("Working on: %d\n", i);
142
            DBG("\tX%01x %-1R %-1R %p\n", en->lsa.type, en->lsa.id, en->lsa.rt, en->lsa_body);
143

    
144
            lsa++;
145
          }
146
          else i++;        /* No lsa added */
147

    
148
          if (sn == STAIL(po->lsal))
149
          {
150
            i--;
151
            break;
152
          }
153

    
154
          sn = sn->next;
155
        }
156

    
157
        if (sn == STAIL(po->lsal))
158
        {
159
          DBG("Number of LSA NOT sent: %d\n", i);
160
          DBG("M bit unset.\n");
161
          n->myimms.bit.m = 0;        /* Unset more bit */
162
        }
163

    
164
        s_put(&(n->dbsi), sn);
165
      }
166

    
167
      pkt->imms.byte = n->myimms.byte;
168

    
169
      length = (j - i) * sizeof(struct ospf_lsa_header) +
170
        sizeof(struct ospf_dbdes_packet);
171
      op->length = htons(length);
172

    
173
      DBG("%s: DB_DES (M) prepared for %I.\n", p->name, n->ip);
174
    }
175

    
176
  case NEIGHBOR_LOADING:
177
  case NEIGHBOR_FULL:
178
    length = ntohs(((struct ospf_packet *) n->ldbdes)->length);
179

    
180
    if (!length)
181
    {
182
      OSPF_TRACE(D_PACKETS, "No packet in my buffer for repeating");
183
      ospf_neigh_sm(n, INM_KILLNBR);
184
      return;
185
    }
186

    
187
    /* Copy last sent packet again */
188
    pkt = ospf_tx_buffer(ifa);
189
    memcpy(pkt, n->ldbdes, length);
190

    
191
    OSPF_PACKET(ospf_dump_dbdes, pkt, "DBDES packet sent to %I via %s", n->ip, ifa->iface->name);
192
    ospf_send_to(ifa, n->ip);
193

    
194
    if(n->myimms.bit.ms) tm_start(n->rxmt_timer, n->ifa->rxmtint);                /* Restart timer */
195

    
196
    if (!n->myimms.bit.ms)
197
    {
198
      if ((n->myimms.bit.m == 0) && (n->imms.bit.m == 0) &&
199
          (n->state == NEIGHBOR_EXCHANGE))
200
      {
201
        ospf_neigh_sm(n, INM_EXDONE);
202
      }
203
    }
204
    break;
205

    
206
  default:                        /* Ignore it */
207
    break;
208
  }
209
}
210

    
211
static void
212
ospf_dbdes_reqladd(struct ospf_dbdes_packet *ps, struct ospf_neighbor *n)
213
{
214
  struct ospf_lsa_header *plsa, lsa;
215
  struct top_hash_entry *he, *sn;
216
  struct ospf_area *oa = n->ifa->oa;
217
  struct top_graph *gr = oa->po->gr;
218
  struct ospf_packet *op;
219
  int i, j;
220

    
221
  op = (struct ospf_packet *) ps;
222

    
223
  plsa = (void *) (ps + 1);
224

    
225
  j = (ntohs(op->length) - sizeof(struct ospf_dbdes_packet)) /
226
    sizeof(struct ospf_lsa_header);
227

    
228
  for (i = 0; i < j; i++)
229
  {
230
    ntohlsah(plsa + i, &lsa);
231
    u32 dom = ospf_lsa_domain(lsa.type, n->ifa);
232
    if (((he = ospf_hash_find_header(gr, dom, &lsa)) == NULL) ||
233
        (lsa_comp(&lsa, &(he->lsa)) == 1))
234
    {
235
      /* Is this condition necessary? */
236
      if (ospf_hash_find_header(n->lsrqh, dom, &lsa) == NULL)
237
      {
238
        sn = ospf_hash_get_header(n->lsrqh, dom, &lsa);
239
        ntohlsah(plsa + i, &(sn->lsa));
240
        s_add_tail(&(n->lsrql), SNODE sn);
241
      }
242
    }
243
  }
244
}
245

    
246
void
247
ospf_dbdes_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa,
248
                   struct ospf_neighbor *n)
249
{
250
  struct proto_ospf *po = ifa->oa->po;
251
  struct proto *p = &po->proto;
252

    
253
  unsigned int size = ntohs(ps_i->length);
254
  if (size < sizeof(struct ospf_dbdes_packet))
255
  {
256
    log(L_ERR "Bad OSPF DBDES packet from %I -  too short (%u B)", n->ip, size);
257
    return;
258
  }
259

    
260
  struct ospf_dbdes_packet *ps = (void *) ps_i;
261
  u32 ps_ddseq = ntohl(ps->ddseq);
262
  u32 ps_options = ntoh_opt(ps->options);
263
  u16 ps_iface_mtu = ntohs(ps->iface_mtu);
264
  
265
  OSPF_PACKET(ospf_dump_dbdes, ps, "DBDES packet received from %I via %s", n->ip, ifa->iface->name);
266

    
267
  ospf_neigh_sm(n, INM_HELLOREC);
268

    
269
  switch (n->state)
270
  {
271
  case NEIGHBOR_DOWN:
272
  case NEIGHBOR_ATTEMPT:
273
  case NEIGHBOR_2WAY:
274
    return;
275
    break;
276
  case NEIGHBOR_INIT:
277
    ospf_neigh_sm(n, INM_2WAYREC);
278
    if (n->state != NEIGHBOR_EXSTART)
279
      return;
280
  case NEIGHBOR_EXSTART:
281

    
282
    if ((ps_iface_mtu != ifa->iface->mtu) && (ifa->type != OSPF_IT_VLINK)
283
        && (ps_iface_mtu != 0) && (ifa->iface->mtu != 0))
284
      log(L_WARN "OSPF: MTU mismatch with neighbour %I on interface %s (remote %d, local %d)",
285
          n->ip, ifa->iface->name, ps_iface_mtu, ifa->iface->mtu);
286

    
287
    if ((ps->imms.bit.m && ps->imms.bit.ms && ps->imms.bit.i)
288
        && (n->rid > po->router_id) && (size == sizeof(struct ospf_dbdes_packet)))
289
    {
290
      /* I'm slave! */
291
      n->dds = ps_ddseq;
292
      n->ddr = ps_ddseq;
293
      n->options = ps_options;
294
      n->myimms.bit.ms = 0;
295
      n->imms.byte = ps->imms.byte;
296
      OSPF_TRACE(D_PACKETS, "I'm slave to %I.", n->ip);
297
      ospf_neigh_sm(n, INM_NEGDONE);
298
      ospf_dbdes_send(n, 1);
299
      break;
300
    }
301

    
302
    if (((ps->imms.bit.i == 0) && (ps->imms.bit.ms == 0)) &&
303
        (n->rid < po->router_id) && (n->dds == ps_ddseq))
304
    {
305
      /* I'm master! */
306
      n->options = ps_options;
307
      n->ddr = ps_ddseq - 1;        /* It will be set corectly a few lines down */
308
      n->imms.byte = ps->imms.byte;
309
      OSPF_TRACE(D_PACKETS, "I'm master to %I.", n->ip);
310
      ospf_neigh_sm(n, INM_NEGDONE);
311
    }
312
    else
313
    {
314
      DBG("%s: Nothing happend to %I (imms=%u)\n", p->name, n->ip,
315
          ps->imms.byte);
316
      break;
317
    }
318
  case NEIGHBOR_EXCHANGE:
319
    if ((ps->imms.byte == n->imms.byte) && (ps_options == n->options) &&
320
        (ps_ddseq == n->ddr))
321
    {
322
      /* Duplicate packet */
323
      OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I.", n->ip);
324
      if (n->myimms.bit.ms == 0)
325
      {
326
        /* Slave should retransmit dbdes packet */
327
        ospf_dbdes_send(n, 0);
328
      }
329
      return;
330
    }
331

    
332
    n->ddr = ps_ddseq;
333

    
334
    if (ps->imms.bit.ms != n->imms.bit.ms)        /* M/S bit differs */
335
    {
336
      OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (bit MS)",
337
                 n->ip);
338
      ospf_neigh_sm(n, INM_SEQMIS);
339
      break;
340
    }
341

    
342
    if (ps->imms.bit.i)                /* I bit is set */
343
    {
344
      OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (bit I)",
345
                 n->ip);
346
      ospf_neigh_sm(n, INM_SEQMIS);
347
      break;
348
    }
349

    
350
    n->imms.byte = ps->imms.byte;
351

    
352
    if (ps_options != n->options)        /* Options differs */
353
    {
354
      OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (options)",
355
                 n->ip);
356
      ospf_neigh_sm(n, INM_SEQMIS);
357
      break;
358
    }
359

    
360
    if (n->myimms.bit.ms)
361
    {
362
      if (ps_ddseq != n->dds)        /* MASTER */
363
      {
364
        OSPF_TRACE(D_PACKETS,
365
                   "dbdes - sequence mismatch neighbor %I (master)", n->ip);
366
        ospf_neigh_sm(n, INM_SEQMIS);
367
        break;
368
      }
369
      n->dds++;
370
      DBG("Incrementing dds\n");
371
      ospf_dbdes_reqladd(ps, n);
372
      if ((n->myimms.bit.m == 0) && (ps->imms.bit.m == 0))
373
      {
374
        ospf_neigh_sm(n, INM_EXDONE);
375
      }
376
      else
377
      {
378
        ospf_dbdes_send(n, 1);
379
      }
380

    
381
    }
382
    else
383
    {
384
      if (ps_ddseq != (n->dds + 1))        /* SLAVE */
385
      {
386
        OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (slave)",
387
                   n->ip);
388
        ospf_neigh_sm(n, INM_SEQMIS);
389
        break;
390
      }
391
      n->ddr = ps_ddseq;
392
      n->dds = ps_ddseq;
393
      ospf_dbdes_reqladd(ps, n);
394
      ospf_dbdes_send(n, 1);
395
    }
396

    
397
    break;
398
  case NEIGHBOR_LOADING:
399
  case NEIGHBOR_FULL:
400
    if ((ps->imms.byte == n->imms.byte) && (ps_options == n->options)
401
        && (ps_ddseq == n->ddr))
402
      /* Only duplicate are accepted */
403
    {
404
      OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I.", n->ip);
405
      if (n->myimms.bit.ms == 0)
406
      {
407
        /* Slave should retransmit dbdes packet */
408
        ospf_dbdes_send(n, 0);
409
      }
410
      return;
411
    }
412
    else
413
    {
414
      OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (full)",
415
                 n->ip);
416
      DBG("PS=%u, DDR=%u, DDS=%u\n", ps_ddseq, n->ddr, n->dds);
417
      ospf_neigh_sm(n, INM_SEQMIS);
418
    }
419
    break;
420
  default:
421
    bug("Received dbdes from %I in undefined state.", n->ip);
422
  }
423
}