Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / iface.c @ 6901fd06

History | View | Annotate | Download (14.9 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999--2005 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
char *ospf_is[] = { "down", "loop", "waiting", "point-to-point", "drother",
12
  "backup", "dr"
13
};
14

    
15
char *ospf_ism[] = { "interface up", "wait timer fired", "backup seen",
16
  "neighbor change", "loop indicated", "unloop indicated", "interface down"
17
};
18

    
19
char *ospf_it[] = { "broadcast", "nbma", "point-to-point", "virtual link" };
20

    
21
static void
22
poll_timer_hook(timer * timer)
23
{
24
  log("POLL!");
25
  ospf_hello_send(timer, 1, NULL);
26
}
27

    
28
static void
29
hello_timer_hook(timer * timer)
30
{
31
  ospf_hello_send(timer, 0, NULL);
32
}
33

    
34
static void
35
wait_timer_hook(timer * timer)
36
{
37
  struct ospf_iface *ifa = (struct ospf_iface *) timer->data;
38
  struct proto *p = &ifa->oa->po->proto;
39

    
40
  OSPF_TRACE(D_EVENTS, "Wait timer fired on interface %s.", ifa->iface->name);
41
  ospf_iface_sm(ifa, ISM_WAITF);
42
}
43

    
44
u32
45
rxbufsize(struct ospf_iface *ifa)
46
{
47
  switch(ifa->rxbuf)
48
  {
49
    case OSPF_RXBUF_NORMAL:
50
      return (ifa->iface->mtu * 2);
51
      break;
52
    case OSPF_RXBUF_LARGE:
53
      return OSPF_MAX_PKT_SIZE;
54
      break;
55
    default:
56
      return ifa->rxbuf;
57
      break;
58
  }
59
}
60

    
61
static sock *
62
ospf_open_socket(struct ospf_iface *ifa, int mc)
63
{
64
  sock *ipsk;
65
  struct proto *p = &ifa->oa->po->proto;
66

    
67
  ipsk = sk_new(p->pool);
68
  ipsk->type = SK_IP;
69
  ipsk->dport = OSPF_PROTO;
70

    
71
#ifdef OSPFv2
72
  //  ipsk->saddr = ifa->iface->addr->ip;
73
  ipsk->saddr = IPA_NONE;
74
#else /* OSPFv3 */
75
  ipsk->saddr = ifa->lladdr;
76
#endif
77

    
78
  ipsk->tos = IP_PREC_INTERNET_CONTROL;
79
  ipsk->ttl = 1;
80
  if (ifa->type == OSPF_IT_VLINK)
81
    ipsk->ttl = 255;
82
  ipsk->rx_hook = ospf_rx_hook;
83
  ipsk->tx_hook = ospf_tx_hook;
84
  ipsk->err_hook = ospf_err_hook;
85
  ipsk->iface = ifa->iface;
86
  ipsk->rbsize = rxbufsize(ifa);
87
  ipsk->tbsize = ifa->iface->mtu;
88
  ipsk->data = (void *) ifa;
89
  if (sk_open(ipsk) != 0)
90
    goto err;
91

    
92
#ifdef OSPFv3
93
  /* 12 is an offset of the checksum in an OSPF packet */
94
  if (sk_set_ipv6_checksum(ipsk, 12) < 0)
95
    goto err;
96
#endif
97

    
98
  if (mc)
99
  {
100
    if (sk_setup_multicast(ipsk) < 0)
101
      goto err;
102

    
103
    if (sk_join_group(ipsk, AllSPFRouters) < 0)
104
      goto err;
105
  }
106

    
107
  return ipsk;
108

    
109
 err:
110
  rfree(ipsk);
111
  return NULL;
112
}
113

    
114

    
115
/**
116
 * ospf_iface_chstate - handle changes of interface state
117
 * @ifa: OSPF interface
118
 * @state: new state
119
 *
120
 * Many actions must be taken according to interface state changes. New network
121
 * LSAs must be originated, flushed, new multicast sockets to listen for messages for
122
 * %ALLDROUTERS have to be opened, etc.
123
 */
124
void
125
ospf_iface_chstate(struct ospf_iface *ifa, u8 state)
126
{
127
  struct proto_ospf *po = ifa->oa->po;
128
  struct proto *p = &po->proto;
129
  u8 oldstate = ifa->state;
130

    
131
  if (oldstate != state)
132
  {
133
    ifa->state = state;
134

    
135
    if (ifa->type == OSPF_IT_VLINK)
136
    {
137
      OSPF_TRACE(D_EVENTS,
138
                 "Changing state of virtual link %R from \"%s\" into \"%s\".",
139
                 ifa->vid, ospf_is[oldstate], ospf_is[state]);
140
      if (state == OSPF_IS_PTP)
141
      {
142
        ifa->sk = ospf_open_socket(ifa, 0);
143
      }
144
    }
145
    else
146
    {
147
      OSPF_TRACE(D_EVENTS,
148
                 "Changing state of iface: %s from \"%s\" into \"%s\".",
149
                 ifa->iface->name, ospf_is[oldstate], ospf_is[state]);
150
      if (ifa->iface->flags & IF_MULTICAST)
151
      {
152
        if ((ifa->type != OSPF_IT_NBMA) && (ifa->ioprob == OSPF_I_OK) &&
153
            ((state == OSPF_IS_BACKUP) || (state == OSPF_IS_DR)))
154
        {
155
          if (!ifa->dr_up == 0)
156
          {
157
            /* FIXME some error handing ? */
158
            sk_join_group(ifa->sk, AllDRouters);
159
            ifa->dr_up = 1;
160
          }
161
        }
162
        else if (ifa->dr_up)
163
        {
164
          sk_leave_group(ifa->sk, AllDRouters);
165
          ifa->dr_up = 0;
166
        }
167
        if ((oldstate == OSPF_IS_DR) && (ifa->net_lsa != NULL))
168
        {
169
          ifa->net_lsa->lsa.age = LSA_MAXAGE;
170
          if (state >= OSPF_IS_WAITING)
171
          {
172
            ospf_lsupd_flush_nlsa(po, ifa->net_lsa);
173
          }
174
          if (can_flush_lsa(po))
175
            flush_lsa(ifa->net_lsa, po);
176
          ifa->net_lsa = NULL;
177
        }
178
      }
179
    }
180
  }
181
}
182

    
183
static void
184
ospf_iface_down(struct ospf_iface *ifa)
185
{
186
  struct ospf_neighbor *n, *nx;
187
  struct proto_ospf *po = ifa->oa->po;
188
  struct proto *p = &po->proto;
189
  struct ospf_iface *iff;
190

    
191
  /* First of all kill all the related vlinks */
192
  if (ifa->type != OSPF_IT_VLINK)
193
  {
194
    WALK_LIST(iff, po->iface_list)
195
    {
196
      if ((iff->type == OSPF_IT_VLINK) && (iff->iface == ifa->iface))
197
        ospf_iface_down(iff);
198
    }
199
  }
200

    
201
  WALK_LIST_DELSAFE(n, nx, ifa->neigh_list)
202
  {
203
    OSPF_TRACE(D_EVENTS, "Removing neighbor %I", n->ip);
204
    ospf_neigh_remove(n);
205
  }
206

    
207
  rfree(ifa->sk);
208
  ifa->sk = NULL;
209

    
210
  if (ifa->type == OSPF_IT_VLINK)
211
  {
212
    ifa->iface = NULL;
213
    return;
214
  }
215
  else
216
  {
217
    rfree(ifa->wait_timer);
218
    rfree(ifa->hello_timer);
219
    rfree(ifa->poll_timer);
220
    rfree(ifa->lock);
221
    rem_node(NODE ifa);
222
    mb_free(ifa);
223
  }
224
}
225

    
226
/**
227
 * ospf_iface_sm - OSPF interface state machine
228
 * @ifa: OSPF interface
229
 * @event: event comming to state machine
230
 *
231
 * This fully respects 9.3 of RFC 2328 except we don't use %LOOP state of
232
 * interface.
233
 */
234
void
235
ospf_iface_sm(struct ospf_iface *ifa, int event)
236
{
237
  struct ospf_area *oa = ifa->oa;
238

    
239
  DBG("SM on %s %s. Event is '%s'\n", (ifa->type == OSPF_IT_VLINK) ? "vlink" : "iface",
240
    ifa->iface ? ifa->iface->name : "(none)" , ospf_ism[event]);
241

    
242
  switch (event)
243
  {
244
  case ISM_UP:
245
    if (ifa->state == OSPF_IS_DOWN)
246
    {
247
      /* Now, nothing should be adjacent */
248
      if ((ifa->type == OSPF_IT_PTP) || (ifa->type == OSPF_IT_VLINK))
249
      {
250
        ospf_iface_chstate(ifa, OSPF_IS_PTP);
251
      }
252
      else
253
      {
254
        if (ifa->priority == 0)
255
          ospf_iface_chstate(ifa, OSPF_IS_DROTHER);
256
        else
257
        {
258
          ospf_iface_chstate(ifa, OSPF_IS_WAITING);
259
          tm_start(ifa->wait_timer, ifa->waitint);
260
        }
261
      }
262

    
263
      tm_start(ifa->hello_timer, ifa->helloint);
264

    
265
      if (ifa->poll_timer)
266
        tm_start(ifa->poll_timer, ifa->pollint);
267

    
268
      hello_timer_hook(ifa->hello_timer);
269
    }
270
    schedule_link_lsa(ifa);
271
    schedule_rt_lsa(ifa->oa);
272
    break;
273
  case ISM_BACKS:
274
  case ISM_WAITF:
275
    if (ifa->state == OSPF_IS_WAITING)
276
    {
277
      bdr_election(ifa);
278
    }
279
    break;
280
  case ISM_NEICH:
281
    if ((ifa->state == OSPF_IS_DROTHER) || (ifa->state == OSPF_IS_DR) ||
282
        (ifa->state == OSPF_IS_BACKUP))
283
    {
284
      bdr_election(ifa);
285
      schedule_rt_lsa(ifa->oa);
286
    }
287
    break;
288
  case ISM_DOWN:
289
    ospf_iface_chstate(ifa, OSPF_IS_DOWN);
290
    ospf_iface_down(ifa);
291
    schedule_link_lsa(ifa);
292
    schedule_rt_lsa(oa);
293
    break;
294
  case ISM_LOOP:                /* Useless? */
295
    ospf_iface_chstate(ifa, OSPF_IS_LOOP);
296
    ospf_iface_down(ifa);
297
    schedule_rt_lsa(ifa->oa);
298
    break;
299
  case ISM_UNLOOP:
300
    ospf_iface_chstate(ifa, OSPF_IS_DOWN);
301
    schedule_rt_lsa(ifa->oa);
302
    break;
303
  default:
304
    bug("OSPF_I_SM - Unknown event?");
305
    break;
306
  }
307

    
308
}
309

    
310
#if 0
311
static sock *
312
ospf_open_mc_socket(struct ospf_iface *ifa)
313
{
314
  sock *mcsk;
315
  struct proto *p = &ifa->oa->po->proto;
316

317
  mcsk = sk_new(p->pool);
318
  mcsk->type = SK_IP_MC;
319
  mcsk->sport = 0;
320
  mcsk->dport = OSPF_PROTO;
321

322
#ifdef OSPFv2
323
  mcsk->saddr = AllSPFRouters;
324
#else /* OSPFv3 */
325
  // mcsk->saddr = AllSPFRouters;
326
  mcsk->saddr = ifa->lladdr;
327
#endif
328

    
329
  mcsk->daddr = AllSPFRouters;
330
  mcsk->tos = IP_PREC_INTERNET_CONTROL;
331
  mcsk->ttl = 1;
332
  mcsk->rx_hook = ospf_rx_hook;
333
  mcsk->tx_hook = ospf_tx_hook;
334
  mcsk->err_hook = ospf_err_hook;
335
  mcsk->iface = ifa->iface;
336
  mcsk->rbsize = rxbufsize(ifa);
337
  mcsk->tbsize = ifa->iface->mtu;
338
  mcsk->data = (void *) ifa;
339
  if (sk_open(mcsk) != 0)
340
  {
341
    DBG("%s: SK_OPEN: mc open failed.\n", p->name);
342
    return (NULL);
343
  }
344
  DBG("%s: SK_OPEN: mc opened.\n", p->name);
345
  return (mcsk);
346
}
347
#endif
348

    
349
u8
350
ospf_iface_clasify(struct iface * ifa)
351
{
352
  if ((ifa->flags & (IF_MULTIACCESS | IF_MULTICAST)) ==
353
      (IF_MULTIACCESS | IF_MULTICAST))
354
    return OSPF_IT_BCAST;
355

    
356
  if ((ifa->flags & (IF_MULTIACCESS | IF_MULTICAST)) == IF_MULTIACCESS)
357
    return OSPF_IT_NBMA;
358

    
359
  return OSPF_IT_PTP;
360
}
361

    
362
struct ospf_iface *
363
ospf_iface_find(struct proto_ospf *p, struct iface *what)
364
{
365
  struct ospf_iface *i;
366

    
367
  WALK_LIST(i, p->iface_list) if ((i->iface == what) && (i->type != OSPF_IT_VLINK))
368
    return i;
369
  return NULL;
370
}
371

    
372
static void
373
ospf_iface_add(struct object_lock *lock)
374
{
375
  struct ospf_iface *ifa = lock->data;
376
  struct proto_ospf *po = ifa->oa->po;
377
  struct proto *p = &po->proto;
378
  struct iface *iface = lock->iface;
379

    
380
  ifa->lock = lock;
381

    
382
  ifa->ioprob = OSPF_I_OK;
383

    
384
  ifa->sk = ospf_open_socket(ifa, ifa->type != OSPF_IT_NBMA);
385
  if (ifa->sk == NULL)
386
  {
387
    log("%s: Huh? could not open ip socket on interface %s?", p->name,
388
        iface->name);
389
    log("%s: Declaring as stub.", p->name);
390
    ifa->stub = 1;
391
    ifa->ioprob += OSPF_I_IP;
392
  }
393

    
394
  ifa->state = OSPF_IS_DOWN;
395
  ospf_iface_sm(ifa, ISM_UP);
396
}
397

    
398
void
399
ospf_iface_new(struct proto_ospf *po, struct iface *iface,
400
               struct ospf_area_config *ac, struct ospf_iface_patt *ip)
401
{
402
  struct proto *p = &po->proto;
403
  struct ospf_iface *ifa;
404
  struct nbma_node *nbma, *nb;
405
  struct object_lock *lock;
406
  struct ospf_area *oa;
407

    
408
  ifa = mb_allocz(p->pool, sizeof(struct ospf_iface));
409
  ifa->iface = iface;
410

    
411
  ifa->cost = ip->cost;
412
  ifa->rxmtint = ip->rxmtint;
413
  ifa->inftransdelay = ip->inftransdelay;
414
  ifa->priority = ip->priority;
415
  ifa->helloint = ip->helloint;
416
  ifa->pollint = ip->pollint;
417
  ifa->strictnbma = ip->strictnbma;
418
  ifa->waitint = ip->waitint;
419
  ifa->dead = (ip->dead == 0) ? ip->deadc * ifa->helloint : ip->dead;
420
  ifa->stub = ip->stub;
421

    
422
#ifdef OSPFv2
423
  ifa->autype = ip->autype;
424
  ifa->passwords = ip->passwords;
425
#endif
426

    
427
#ifdef OSPFv3
428
  ifa->instance_id = ip->instance_id;
429

    
430
  ifa->lladdr = IPA_NONE;
431

    
432
  /* Find link-local address */
433
  if (ifa->type != OSPF_IT_VLINK)
434
    {
435
      struct ifa *a;
436
      WALK_LIST(a, iface->addrs)
437
        if (a->scope == SCOPE_LINK)
438
          {
439
            ifa->lladdr = a->ip;
440
            break;
441
          }
442

    
443
      if (! ipa_nonzero(ifa->lladdr))
444
        log(L_WARN "%s: Missing link local address on interface %s", p->name,  iface->name);
445
    }
446
#endif
447

    
448
  ifa->rxbuf = ip->rxbuf;
449

    
450
  if (ip->type == OSPF_IT_UNDEF)
451
    ifa->type = ospf_iface_clasify(ifa->iface);
452
  else
453
    ifa->type = ip->type;
454

    
455
  init_list(&ifa->neigh_list);
456
  init_list(&ifa->nbma_list);
457

    
458
  WALK_LIST(nb, ip->nbma_list)
459
  {
460
    nbma = mb_alloc(p->pool, sizeof(struct nbma_node));
461
    nbma->ip = nb->ip;
462
    nbma->eligible = nb->eligible;
463
    add_tail(&ifa->nbma_list, NODE nbma);
464
  }
465

    
466
  /* Add hello timer */
467
  ifa->hello_timer = tm_new(p->pool);
468
  ifa->hello_timer->data = ifa;
469
  ifa->hello_timer->randomize = 0;
470
  ifa->hello_timer->hook = hello_timer_hook;
471
  ifa->hello_timer->recurrent = ifa->helloint;
472
  DBG("%s: Installing hello timer. (%u)\n", p->name, ifa->helloint);
473

    
474
  if (ifa->type == OSPF_IT_NBMA)
475
  {
476
    ifa->poll_timer = tm_new(p->pool);
477
    ifa->poll_timer->data = ifa;
478
    ifa->poll_timer->randomize = 0;
479
    ifa->poll_timer->hook = poll_timer_hook;
480
    ifa->poll_timer->recurrent = ifa->pollint;
481
    DBG("%s: Installing poll timer. (%u)\n", p->name, ifa->pollint);
482
  }
483
  else
484
    ifa->poll_timer = NULL;
485

    
486
  ifa->wait_timer = tm_new(p->pool);
487
  ifa->wait_timer->data = ifa;
488
  ifa->wait_timer->randomize = 0;
489
  ifa->wait_timer->hook = wait_timer_hook;
490
  ifa->wait_timer->recurrent = 0;
491
  DBG("%s: Installing wait timer. (%u)\n", p->name, ifa->waitint);
492
  add_tail(&((struct proto_ospf *) p)->iface_list, NODE ifa);
493
  ifa->state = OSPF_IS_DOWN;
494

    
495
  ifa->oa = NULL;
496
  WALK_LIST(oa, po->area_list)
497
  {
498
    if (oa->areaid == ac->areaid)
499
    {
500
      ifa->oa = oa;
501
      break;
502
    }
503
  }
504

    
505
  if (!ifa->oa)
506
    bug("Cannot add any area to accepted Interface");
507
  else
508

    
509
  if (ifa->type == OSPF_IT_VLINK)
510
  {
511
    ifa->oa = po->backbone;
512
    ifa->voa = oa;
513
    ifa->vid = ip->vid;
514
    return;                        /* Don't lock, don't add sockets */
515
  }
516

    
517
  lock = olock_new(p->pool);
518
  lock->addr = AllSPFRouters;
519
  lock->type = OBJLOCK_IP;
520
  lock->port = OSPF_PROTO;
521
  lock->iface = iface;
522
  lock->data = ifa;
523
  lock->hook = ospf_iface_add;
524

    
525
  olock_acquire(lock);
526
}
527

    
528
void
529
ospf_iface_change_mtu(struct proto_ospf *po, struct ospf_iface *ifa)
530
{
531
  struct proto *p = &po->proto;
532
  struct ospf_packet *op;
533
  struct ospf_neighbor *n;
534
  OSPF_TRACE(D_EVENTS, "Changing MTU on interface %s.", ifa->iface->name);
535

    
536
  if (ifa->sk)
537
  {
538
    ifa->sk->rbsize = rxbufsize(ifa);
539
    ifa->sk->tbsize = ifa->iface->mtu;
540
    sk_reallocate(ifa->sk);
541
  }
542

    
543
  WALK_LIST(n, ifa->neigh_list)
544
  {
545
    op = (struct ospf_packet *) n->ldbdes;
546
    n->ldbdes = mb_allocz(n->pool, ifa->iface->mtu);
547

    
548
    if (ntohs(op->length) <= ifa->iface->mtu)        /* If the packet in old buffer is bigger, let it filled by zeros */
549
      memcpy(n->ldbdes, op, ifa->iface->mtu);        /* If the packet is old is same or smaller, copy it */
550

    
551
    rfree(op);
552
  }
553
}
554

    
555
void
556
ospf_iface_notify(struct proto *p, unsigned flags, struct iface *iface)
557
{
558
  struct proto_ospf *po = (struct proto_ospf *) p;
559
  struct ospf_config *c = (struct ospf_config *) (p->cf);
560
  struct ospf_area_config *ac;
561
  struct ospf_iface_patt *ip = NULL;
562
  struct ospf_iface *ifa;
563

    
564
  DBG("%s: If notify called\n", p->name);
565
  if (iface->flags & IF_IGNORE)
566
    return;
567

    
568
  if (flags & IF_CHANGE_UP)
569
  {
570
    WALK_LIST(ac, c->area_list)
571
    {
572
      if (ip = (struct ospf_iface_patt *)
573
          iface_patt_find(&ac->patt_list, iface))
574
        break;
575
    }
576

    
577
    if (ip)
578
    {
579
      OSPF_TRACE(D_EVENTS, "Using interface %s.", iface->name);
580
      ospf_iface_new(po, iface, ac, ip);
581
    }
582
  }
583

    
584
  if (flags & IF_CHANGE_DOWN)
585
  {
586
    if ((ifa = ospf_iface_find((struct proto_ospf *) p, iface)) != NULL)
587
    {
588
      OSPF_TRACE(D_EVENTS, "Killing interface %s.", iface->name);
589
      ospf_iface_sm(ifa, ISM_DOWN);
590
    }
591
  }
592

    
593
  if (flags & IF_CHANGE_MTU)
594
  {
595
    if ((ifa = ospf_iface_find((struct proto_ospf *) p, iface)) != NULL)
596
      ospf_iface_change_mtu(po, ifa);
597
  }
598
}
599

    
600
void
601
ospf_iface_info(struct ospf_iface *ifa)
602
{
603
  char *strict = "(strict)";
604

    
605
  if ((ifa->type != OSPF_IT_NBMA) || (ifa->strictnbma == 0))
606
    strict = "";
607
  if (ifa->type == OSPF_IT_VLINK)
608
  {
609
    cli_msg(-1015, "Virtual link to %R:", ifa->vid);
610
    cli_msg(-1015, "\tPeer IP: %I", ifa->vip);
611
    cli_msg(-1015, "\tTransit area: %R (%u)", ifa->voa->areaid,
612
            ifa->voa->areaid);
613
    cli_msg(-1015, "\tInterface: \"%s\"",
614
            (ifa->iface ? ifa->iface->name : "(none)"));
615
  }
616
  else
617
  {
618
    cli_msg(-1015, "Interface \"%s\":",
619
            (ifa->iface ? ifa->iface->name : "(none)"));
620
    cli_msg(-1015, "\tType: %s %s", ospf_it[ifa->type], strict);
621
    cli_msg(-1015, "\tArea: %R (%u)", ifa->oa->areaid, ifa->oa->areaid);
622
  }
623
  cli_msg(-1015, "\tState: %s %s", ospf_is[ifa->state],
624
          ifa->stub ? "(stub)" : "");
625
  cli_msg(-1015, "\tPriority: %u", ifa->priority);
626
  cli_msg(-1015, "\tCost: %u", ifa->cost);
627
  cli_msg(-1015, "\tHello timer: %u", ifa->helloint);
628

    
629
  if (ifa->type == OSPF_IT_NBMA)
630
  {
631
    cli_msg(-1015, "\tPoll timer: %u", ifa->pollint);
632
  }
633
  cli_msg(-1015, "\tWait timer: %u", ifa->waitint);
634
  cli_msg(-1015, "\tDead timer: %u", ifa->dead);
635
  cli_msg(-1015, "\tRetransmit timer: %u", ifa->rxmtint);
636
  if ((ifa->type == OSPF_IT_BCAST) || (ifa->type == OSPF_IT_NBMA))
637
  {
638
    cli_msg(-1015, "\tDesigned router (ID): %R", ifa->drid);
639
    cli_msg(-1015, "\tDesigned router (IP): %I", ifa->drip);
640
    cli_msg(-1015, "\tBackup designed router (ID): %R", ifa->bdrid);
641
    cli_msg(-1015, "\tBackup designed router (IP): %I", ifa->bdrip);
642
  }
643
}
644

    
645
void
646
ospf_iface_shutdown(struct ospf_iface *ifa)
647
{
648
  init_list(&ifa->neigh_list);
649
  hello_timer_hook(ifa->hello_timer);
650
}