Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / iface.c @ af157fa3

History | View | Annotate | Download (18.6 KB)

1 4364b47e Ondrej Filip
/*
2
 *        BIRD -- OSPF
3
 *
4 e300066d Ondrej Filip
 *        (c) 1999--2005 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 b9ed99f7 Ondrej Filip
char *ospf_is[] = { "down", "loop", "waiting", "point-to-point", "drother",
12
  "backup", "dr"
13
};
14 8914e37d Ondrej Filip
15 b9ed99f7 Ondrej Filip
char *ospf_ism[] = { "interface up", "wait timer fired", "backup seen",
16
  "neighbor change", "loop indicated", "unloop indicated", "interface down"
17
};
18 79f036ef Ondrej Filip
19 b9ed99f7 Ondrej Filip
char *ospf_it[] = { "broadcast", "nbma", "point-to-point", "virtual link" };
20 c4f0f014 Ondrej Filip
21 9831e591 Martin Mares
static void
22 b9ed99f7 Ondrej Filip
poll_timer_hook(timer * timer)
23 39e517d4 Ondrej Filip
{
24
  ospf_hello_send(timer, 1, NULL);
25
}
26
27 9831e591 Martin Mares
static void
28 b9ed99f7 Ondrej Filip
hello_timer_hook(timer * timer)
29 39e517d4 Ondrej Filip
{
30
  ospf_hello_send(timer, 0, NULL);
31
}
32
33 9831e591 Martin Mares
static void
34 b9ed99f7 Ondrej Filip
wait_timer_hook(timer * timer)
35 39e517d4 Ondrej Filip
{
36 b9ed99f7 Ondrej Filip
  struct ospf_iface *ifa = (struct ospf_iface *) timer->data;
37 86c84d76 Ondrej Filip
  struct proto *p = &ifa->oa->po->proto;
38 39e517d4 Ondrej Filip
39 b9ed99f7 Ondrej Filip
  OSPF_TRACE(D_EVENTS, "Wait timer fired on interface %s.", ifa->iface->name);
40
  ospf_iface_sm(ifa, ISM_WAITF);
41 39e517d4 Ondrej Filip
}
42
43 94c42054 Ondrej Filip
u32
44
rxbufsize(struct ospf_iface *ifa)
45
{
46
  switch(ifa->rxbuf)
47
  {
48
    case OSPF_RXBUF_NORMAL:
49
      return (ifa->iface->mtu * 2);
50
      break;
51
    case OSPF_RXBUF_LARGE:
52
      return OSPF_MAX_PKT_SIZE;
53
      break;
54
    default:
55
      return ifa->rxbuf;
56
      break;
57
  }
58
}
59
60 353729f5 Ondrej Zajicek
static int
61 0aad2b92 Ondrej Zajicek
ospf_sk_open(struct ospf_iface *ifa)
62 3b16080c Ondrej Filip
{
63 353729f5 Ondrej Zajicek
  sock *sk;
64 86c84d76 Ondrej Filip
  struct proto *p = &ifa->oa->po->proto;
65 3b16080c Ondrej Filip
66 353729f5 Ondrej Zajicek
  sk = sk_new(p->pool);
67
  sk->type = SK_IP;
68
  sk->dport = OSPF_PROTO;
69 061ab802 Ondrej Zajicek
70
#ifdef OSPFv2
71 a3062085 Ondrej Zajicek
  /*
72
   * In Linux IPv4, binding a raw socket to an IP address of an iface causes
73
   * that the socket does not receive multicast packets, as they have
74
   * different (multicast) destination IP address.
75
   */
76 0aad2b92 Ondrej Zajicek
  sk->saddr = IPA_NONE;
77 061ab802 Ondrej Zajicek
#else /* OSPFv3 */
78 353729f5 Ondrej Zajicek
  sk->saddr = ifa->addr->ip; /* link-local addr */
79 061ab802 Ondrej Zajicek
#endif
80
81 353729f5 Ondrej Zajicek
  sk->tos = IP_PREC_INTERNET_CONTROL;
82
  sk->rx_hook = ospf_rx_hook;
83
  sk->tx_hook = ospf_tx_hook;
84
  sk->err_hook = ospf_err_hook;
85
  sk->iface = ifa->iface;
86
  sk->rbsize = rxbufsize(ifa);
87
  sk->tbsize = ifa->iface->mtu;
88
  sk->data = (void *) ifa;
89
  sk->flags = SKF_LADDR_RX;
90
91
  if (sk_open(sk) != 0)
92 f9c799a0 Ondrej Zajicek
    goto err;
93
94 4ac7c834 Ondrej Zajicek
#ifdef OSPFv3
95
  /* 12 is an offset of the checksum in an OSPF packet */
96 353729f5 Ondrej Zajicek
  if (sk_set_ipv6_checksum(sk, 12) < 0)
97 4ac7c834 Ondrej Zajicek
    goto err;
98
#endif
99
100 0aad2b92 Ondrej Zajicek
  sk->saddr = ifa->addr->ip;
101
  if (sk_setup_multicast(sk) < 0)
102 353729f5 Ondrej Zajicek
    goto err;
103 f9c799a0 Ondrej Zajicek
104 353729f5 Ondrej Zajicek
  ifa->sk = sk;
105
  ifa->sk_spf = 0;
106
  ifa->sk_dr = 0;
107
  return 1;
108 f9c799a0 Ondrej Zajicek
109
 err:
110 353729f5 Ondrej Zajicek
  rfree(sk);
111
  return 0;
112
}
113
114
static inline void
115
ospf_sk_join_spf(struct ospf_iface *ifa)
116
{
117
  if (ifa->sk_spf)
118
    return;
119
120
  sk_join_group(ifa->sk, AllSPFRouters);
121
  ifa->sk_spf = 1;
122
}
123
124
static inline void
125
ospf_sk_join_dr(struct ospf_iface *ifa)
126
{
127
  if (ifa->sk_dr)
128
    return;
129
130
  sk_join_group(ifa->sk, AllDRouters);
131
  ifa->sk_dr = 1;
132
}
133
134
static inline void
135
ospf_sk_leave_spf(struct ospf_iface *ifa)
136
{
137
  if (!ifa->sk_spf)
138
    return;
139
140
  sk_leave_group(ifa->sk, AllSPFRouters);
141
  ifa->sk_spf = 0;
142
}
143
144
static inline void
145
ospf_sk_leave_dr(struct ospf_iface *ifa)
146
{
147
  if (!ifa->sk_dr)
148
    return;
149
150
  sk_leave_group(ifa->sk, AllDRouters);
151
  ifa->sk_dr = 0;
152
}
153
154
static inline void
155
ospf_sk_close(struct ospf_iface *ifa)
156
{
157
  ASSERT(ifa->sk);
158
159
  rfree(ifa->sk);
160
  ifa->sk = NULL;
161 3b16080c Ondrej Filip
}
162
163 39e517d4 Ondrej Filip
164 d5e4b518 Ondrej Filip
/**
165 b9ed99f7 Ondrej Filip
 * ospf_iface_chstate - handle changes of interface state
166 d5e4b518 Ondrej Filip
 * @ifa: OSPF interface
167
 * @state: new state
168
 *
169 baa5dd6c Ondrej Filip
 * Many actions must be taken according to interface state changes. New network
170
 * LSAs must be originated, flushed, new multicast sockets to listen for messages for
171
 * %ALLDROUTERS have to be opened, etc.
172 d5e4b518 Ondrej Filip
 */
173 4364b47e Ondrej Filip
void
174 b9ed99f7 Ondrej Filip
ospf_iface_chstate(struct ospf_iface *ifa, u8 state)
175 4364b47e Ondrej Filip
{
176 86c84d76 Ondrej Filip
  struct proto_ospf *po = ifa->oa->po;
177 b9ed99f7 Ondrej Filip
  struct proto *p = &po->proto;
178 3b16080c Ondrej Filip
  u8 oldstate = ifa->state;
179 4364b47e Ondrej Filip
180 3b16080c Ondrej Filip
  if (oldstate != state)
181 2a092594 Ondrej Filip
  {
182 b9ed99f7 Ondrej Filip
    ifa->state = state;
183 3b16080c Ondrej Filip
184
    if (ifa->type == OSPF_IT_VLINK)
185 2a092594 Ondrej Filip
    {
186 3b16080c Ondrej Filip
      OSPF_TRACE(D_EVENTS,
187 3aab39f5 Ondrej Zajicek
                 "Changing state of virtual link %R from \"%s\" into \"%s\".",
188
                 ifa->vid, ospf_is[oldstate], ospf_is[state]);
189 3b16080c Ondrej Filip
    }
190
    else
191
    {
192
      OSPF_TRACE(D_EVENTS,
193
                 "Changing state of iface: %s from \"%s\" into \"%s\".",
194
                 ifa->iface->name, ospf_is[oldstate], ospf_is[state]);
195
      if (ifa->iface->flags & IF_MULTICAST)
196
      {
197 f9c799a0 Ondrej Zajicek
        if ((ifa->type != OSPF_IT_NBMA) && (ifa->ioprob == OSPF_I_OK) &&
198
            ((state == OSPF_IS_BACKUP) || (state == OSPF_IS_DR)))
199 353729f5 Ondrej Zajicek
          ospf_sk_join_dr(ifa);
200
        else
201
          ospf_sk_leave_dr(ifa);
202
203 c3226991 Ondrej Zajicek
        if ((oldstate == OSPF_IS_DR) && (ifa->net_lsa != NULL))
204 3b16080c Ondrej Filip
        {
205 c3226991 Ondrej Zajicek
          ifa->net_lsa->lsa.age = LSA_MAXAGE;
206 3b16080c Ondrej Filip
          if (state >= OSPF_IS_WAITING)
207
          {
208 c3226991 Ondrej Zajicek
            ospf_lsupd_flush_nlsa(po, ifa->net_lsa);
209 3b16080c Ondrej Filip
          }
210 86c84d76 Ondrej Filip
          if (can_flush_lsa(po))
211 c3226991 Ondrej Zajicek
            flush_lsa(ifa->net_lsa, po);
212
          ifa->net_lsa = NULL;
213 eb2c99a1 Ondrej Filip
        }
214 dc2548d2 Ondrej Filip
      }
215 353729f5 Ondrej Zajicek
      // FIXME flushling of link LSA
216 2a092594 Ondrej Filip
    }
217
  }
218 4364b47e Ondrej Filip
}
219
220 b9ed99f7 Ondrej Filip
static void
221
ospf_iface_down(struct ospf_iface *ifa)
222 4364b47e Ondrej Filip
{
223 b9ed99f7 Ondrej Filip
  struct ospf_neighbor *n, *nx;
224 86c84d76 Ondrej Filip
  struct proto_ospf *po = ifa->oa->po;
225
  struct proto *p = &po->proto;
226 98ac6176 Ondrej Filip
  struct ospf_iface *iff;
227 18a0c0bb Ondrej Filip
228 353729f5 Ondrej Zajicek
  OSPF_TRACE(D_EVENTS, "Removing interface %s", ifa->iface->name);
229
230 3b16080c Ondrej Filip
  /* First of all kill all the related vlinks */
231
  if (ifa->type != OSPF_IT_VLINK)
232
  {
233
    WALK_LIST(iff, po->iface_list)
234
    {
235 0aad2b92 Ondrej Zajicek
      if ((iff->type == OSPF_IT_VLINK) && (iff->vifa == ifa))
236 353729f5 Ondrej Zajicek
        ospf_iface_sm(iff, ISM_DOWN);
237 3b16080c Ondrej Filip
    }
238
  }
239
240 b9ed99f7 Ondrej Filip
  WALK_LIST_DELSAFE(n, nx, ifa->neigh_list)
241 18a0c0bb Ondrej Filip
  {
242 1ae494a7 Martin Mares
    OSPF_TRACE(D_EVENTS, "Removing neighbor %I", n->ip);
243 18a0c0bb Ondrej Filip
    ospf_neigh_remove(n);
244
  }
245 f9c799a0 Ondrej Zajicek
246 3b16080c Ondrej Filip
  if (ifa->type == OSPF_IT_VLINK)
247 98ac6176 Ondrej Filip
  {
248 0aad2b92 Ondrej Zajicek
    ifa->vifa = NULL;
249 98ac6176 Ondrej Filip
    ifa->iface = NULL;
250 0aad2b92 Ondrej Zajicek
    ifa->addr = NULL;
251
    ifa->sk = NULL;
252
    ifa->vip = IPA_NONE;
253 98ac6176 Ondrej Filip
    return;
254
  }
255
  else
256
  {
257 353729f5 Ondrej Zajicek
    ospf_sk_close(ifa);
258 98ac6176 Ondrej Filip
    rfree(ifa->wait_timer);
259
    rfree(ifa->hello_timer);
260
    rfree(ifa->poll_timer);
261
    rfree(ifa->lock);
262
    rem_node(NODE ifa);
263
    mb_free(ifa);
264
  }
265 4364b47e Ondrej Filip
}
266
267 d5e4b518 Ondrej Filip
/**
268 b9ed99f7 Ondrej Filip
 * ospf_iface_sm - OSPF interface state machine
269 d5e4b518 Ondrej Filip
 * @ifa: OSPF interface
270
 * @event: event comming to state machine
271
 *
272 98ac6176 Ondrej Filip
 * This fully respects 9.3 of RFC 2328 except we don't use %LOOP state of
273 d5e4b518 Ondrej Filip
 * interface.
274
 */
275 4364b47e Ondrej Filip
void
276 b9ed99f7 Ondrej Filip
ospf_iface_sm(struct ospf_iface *ifa, int event)
277 4364b47e Ondrej Filip
{
278 b9ed99f7 Ondrej Filip
  struct ospf_area *oa = ifa->oa;
279 4364b47e Ondrej Filip
280 18722dc9 Ondrej Filip
  DBG("SM on %s %s. Event is '%s'\n", (ifa->type == OSPF_IT_VLINK) ? "vlink" : "iface",
281
    ifa->iface ? ifa->iface->name : "(none)" , ospf_ism[event]);
282 4364b47e Ondrej Filip
283 b9ed99f7 Ondrej Filip
  switch (event)
284 4364b47e Ondrej Filip
  {
285 b9ed99f7 Ondrej Filip
  case ISM_UP:
286
    if (ifa->state == OSPF_IS_DOWN)
287
    {
288
      /* Now, nothing should be adjacent */
289
      if ((ifa->type == OSPF_IT_PTP) || (ifa->type == OSPF_IT_VLINK))
290 3b16080c Ondrej Filip
      {
291 b9ed99f7 Ondrej Filip
        ospf_iface_chstate(ifa, OSPF_IS_PTP);
292 3b16080c Ondrej Filip
      }
293 b9ed99f7 Ondrej Filip
      else
294 4364b47e Ondrej Filip
      {
295 b9ed99f7 Ondrej Filip
        if (ifa->priority == 0)
296
          ospf_iface_chstate(ifa, OSPF_IS_DROTHER);
297
        else
298
        {
299
          ospf_iface_chstate(ifa, OSPF_IS_WAITING);
300
          tm_start(ifa->wait_timer, ifa->waitint);
301
        }
302 4364b47e Ondrej Filip
      }
303 3b16080c Ondrej Filip
304
      tm_start(ifa->hello_timer, ifa->helloint);
305
306
      if (ifa->poll_timer)
307
        tm_start(ifa->poll_timer, ifa->pollint);
308
309 897999c2 Ondrej Filip
      hello_timer_hook(ifa->hello_timer);
310 b9ed99f7 Ondrej Filip
    }
311 be2d38b7 Ondrej Zajicek
    schedule_link_lsa(ifa);
312 b9ed99f7 Ondrej Filip
    schedule_rt_lsa(ifa->oa);
313
    break;
314
  case ISM_BACKS:
315
  case ISM_WAITF:
316
    if (ifa->state == OSPF_IS_WAITING)
317
    {
318
      bdr_election(ifa);
319
    }
320
    break;
321
  case ISM_NEICH:
322
    if ((ifa->state == OSPF_IS_DROTHER) || (ifa->state == OSPF_IS_DR) ||
323
        (ifa->state == OSPF_IS_BACKUP))
324
    {
325
      bdr_election(ifa);
326 70a38319 Ondrej Filip
      schedule_rt_lsa(ifa->oa);
327 b9ed99f7 Ondrej Filip
    }
328
    break;
329
  case ISM_DOWN:
330
    ospf_iface_chstate(ifa, OSPF_IS_DOWN);
331
    ospf_iface_down(ifa);
332
    schedule_rt_lsa(oa);
333
    break;
334 353729f5 Ondrej Zajicek
    /*
335
  case ISM_LOOP:
336 b9ed99f7 Ondrej Filip
    ospf_iface_chstate(ifa, OSPF_IS_LOOP);
337
    break;
338
  case ISM_UNLOOP:
339
    ospf_iface_chstate(ifa, OSPF_IS_DOWN);
340
    break;
341 353729f5 Ondrej Zajicek
    */
342 b9ed99f7 Ondrej Filip
  default:
343
    bug("OSPF_I_SM - Unknown event?");
344
    break;
345 4364b47e Ondrej Filip
  }
346 b9ed99f7 Ondrej Filip
347 4364b47e Ondrej Filip
}
348
349
u8
350 0aad2b92 Ondrej Zajicek
ospf_iface_clasify(struct iface *ifa, struct ifa *addr)
351 4364b47e Ondrej Filip
{
352 0aad2b92 Ondrej Zajicek
  if (addr->flags & IA_UNNUMBERED)
353 aa80826e Ondrej Zajicek
    return OSPF_IT_PTP;
354
355 b9ed99f7 Ondrej Filip
  if ((ifa->flags & (IF_MULTIACCESS | IF_MULTICAST)) ==
356
      (IF_MULTIACCESS | IF_MULTICAST))
357
    return OSPF_IT_BCAST;
358
359
  if ((ifa->flags & (IF_MULTIACCESS | IF_MULTICAST)) == IF_MULTIACCESS)
360 4364b47e Ondrej Filip
    return OSPF_IT_NBMA;
361 b9ed99f7 Ondrej Filip
362 4364b47e Ondrej Filip
  return OSPF_IT_PTP;
363
}
364
365 b9ed99f7 Ondrej Filip
struct ospf_iface *
366
ospf_iface_find(struct proto_ospf *p, struct iface *what)
367 4364b47e Ondrej Filip
{
368
  struct ospf_iface *i;
369
370 3b16080c Ondrej Filip
  WALK_LIST(i, p->iface_list) if ((i->iface == what) && (i->type != OSPF_IT_VLINK))
371 b9ed99f7 Ondrej Filip
    return i;
372 4364b47e Ondrej Filip
  return NULL;
373
}
374
375 b9ed99f7 Ondrej Filip
static void
376
ospf_iface_add(struct object_lock *lock)
377
{
378
  struct ospf_iface *ifa = lock->data;
379 86c84d76 Ondrej Filip
  struct proto_ospf *po = ifa->oa->po;
380 b9ed99f7 Ondrej Filip
  struct proto *p = &po->proto;
381
382 98ac6176 Ondrej Filip
  ifa->lock = lock;
383
384 0aad2b92 Ondrej Zajicek
  if (ospf_sk_open(ifa))
385 b9ed99f7 Ondrej Filip
  {
386 0aad2b92 Ondrej Zajicek
    if (ifa->type != OSPF_IT_NBMA)
387
      ospf_sk_join_spf(ifa);
388
  }
389
  else
390
  {
391
    log(L_ERR "%s: Socket open failed on interface %s, declaring as stub", p->name, ifa->iface->name);
392
    ifa->ioprob = OSPF_I_SK;
393 b9ed99f7 Ondrej Filip
    ifa->stub = 1;
394
  }
395
396
  ifa->state = OSPF_IS_DOWN;
397
  ospf_iface_sm(ifa, ISM_UP);
398
}
399
400 4364b47e Ondrej Filip
void
401 0aad2b92 Ondrej Zajicek
ospf_iface_new(struct proto_ospf *po, struct iface *iface, struct ifa *addr,
402 3b16080c Ondrej Filip
               struct ospf_area_config *ac, struct ospf_iface_patt *ip)
403 98ac6176 Ondrej Filip
{
404
  struct proto *p = &po->proto;
405
  struct ospf_iface *ifa;
406
  struct nbma_node *nbma, *nb;
407
  struct object_lock *lock;
408
  struct ospf_area *oa;
409
410 353729f5 Ondrej Zajicek
  if (ip->type != OSPF_IT_VLINK)
411
    OSPF_TRACE(D_EVENTS, "Adding interface %s", iface->name);
412
413 98ac6176 Ondrej Filip
  ifa = mb_allocz(p->pool, sizeof(struct ospf_iface));
414
  ifa->iface = iface;
415 0aad2b92 Ondrej Zajicek
  ifa->addr = addr;
416 98ac6176 Ondrej Filip
417
  ifa->cost = ip->cost;
418
  ifa->rxmtint = ip->rxmtint;
419
  ifa->inftransdelay = ip->inftransdelay;
420
  ifa->priority = ip->priority;
421
  ifa->helloint = ip->helloint;
422
  ifa->pollint = ip->pollint;
423
  ifa->strictnbma = ip->strictnbma;
424
  ifa->waitint = ip->waitint;
425 d8c7d9e8 Ondrej Filip
  ifa->dead = (ip->dead == 0) ? ip->deadc * ifa->helloint : ip->dead;
426 af157fa3 Ondrej Zajicek
  ifa->stub = ospf_iface_stubby(ip, addr);
427 0aad2b92 Ondrej Zajicek
  ifa->ioprob = OSPF_I_OK;
428
  ifa->rxbuf = ip->rxbuf;
429 c3226991 Ondrej Zajicek
430
#ifdef OSPFv2
431 98ac6176 Ondrej Filip
  ifa->autype = ip->autype;
432 3e2bd0f1 Ondrej Filip
  ifa->passwords = ip->passwords;
433 c3226991 Ondrej Zajicek
#endif
434
435
#ifdef OSPFv3
436
  ifa->instance_id = ip->instance_id;
437 b49e6f5a Ondrej Zajicek
438 0aad2b92 Ondrej Zajicek
  /*
439
  addr = NULL;
440 b49e6f5a Ondrej Zajicek
  if (ifa->type != OSPF_IT_VLINK)
441
    {
442
      struct ifa *a;
443
      WALK_LIST(a, iface->addrs)
444
        if (a->scope == SCOPE_LINK)
445
          {
446 0aad2b92 Ondrej Zajicek
            addr = a;
447 b49e6f5a Ondrej Zajicek
            break;
448
          }
449

450 0aad2b92 Ondrej Zajicek
      if (!addr)
451
      {
452
        log(L_ERR "%s: Missing link-local address on interface %s, declaring as stub", p->name,  iface->name);
453
        ifa->ioprob = OSPF_I_LL;
454
        ifa->stub = 1;
455
      }
456 b49e6f5a Ondrej Zajicek
    }
457 0aad2b92 Ondrej Zajicek
  */
458 c3226991 Ondrej Zajicek
#endif
459
460 98ac6176 Ondrej Filip
  if (ip->type == OSPF_IT_UNDEF)
461 0aad2b92 Ondrej Zajicek
    ifa->type = ospf_iface_clasify(iface, addr);
462 98ac6176 Ondrej Filip
  else
463
    ifa->type = ip->type;
464
465 aa80826e Ondrej Zajicek
#ifdef OSPFv2
466
  if ((ifa->type != OSPF_IT_PTP) && (ifa->type != OSPF_IT_VLINK) &&
467 0aad2b92 Ondrej Zajicek
      (addr->flags & IA_UNNUMBERED))
468 aa80826e Ondrej Zajicek
  {
469
    log(L_WARN "%s: Missing proper IP prefix on interface %s, forcing point-to-point mode",
470
        p->name,  iface->name);
471
    ifa->type = OSPF_IT_PTP;
472
  }
473
#endif
474
475 98ac6176 Ondrej Filip
  init_list(&ifa->neigh_list);
476
  init_list(&ifa->nbma_list);
477 3b16080c Ondrej Filip
478 98ac6176 Ondrej Filip
  WALK_LIST(nb, ip->nbma_list)
479
  {
480
    nbma = mb_alloc(p->pool, sizeof(struct nbma_node));
481
    nbma->ip = nb->ip;
482
    nbma->eligible = nb->eligible;
483
    add_tail(&ifa->nbma_list, NODE nbma);
484
  }
485
486
  /* Add hello timer */
487
  ifa->hello_timer = tm_new(p->pool);
488
  ifa->hello_timer->data = ifa;
489
  ifa->hello_timer->randomize = 0;
490
  ifa->hello_timer->hook = hello_timer_hook;
491
  ifa->hello_timer->recurrent = ifa->helloint;
492
  DBG("%s: Installing hello timer. (%u)\n", p->name, ifa->helloint);
493
494
  if (ifa->type == OSPF_IT_NBMA)
495
  {
496
    ifa->poll_timer = tm_new(p->pool);
497
    ifa->poll_timer->data = ifa;
498
    ifa->poll_timer->randomize = 0;
499
    ifa->poll_timer->hook = poll_timer_hook;
500
    ifa->poll_timer->recurrent = ifa->pollint;
501
    DBG("%s: Installing poll timer. (%u)\n", p->name, ifa->pollint);
502
  }
503
  else
504
    ifa->poll_timer = NULL;
505
506
  ifa->wait_timer = tm_new(p->pool);
507
  ifa->wait_timer->data = ifa;
508
  ifa->wait_timer->randomize = 0;
509
  ifa->wait_timer->hook = wait_timer_hook;
510
  ifa->wait_timer->recurrent = 0;
511
  DBG("%s: Installing wait timer. (%u)\n", p->name, ifa->waitint);
512
  add_tail(&((struct proto_ospf *) p)->iface_list, NODE ifa);
513
  ifa->state = OSPF_IS_DOWN;
514
515 3b16080c Ondrej Filip
  ifa->oa = NULL;
516 98ac6176 Ondrej Filip
  WALK_LIST(oa, po->area_list)
517
  {
518 3b16080c Ondrej Filip
    if (oa->areaid == ac->areaid)
519
    {
520
      ifa->oa = oa;
521 98ac6176 Ondrej Filip
      break;
522 3b16080c Ondrej Filip
    }
523 98ac6176 Ondrej Filip
  }
524
525 3b16080c Ondrej Filip
  if (!ifa->oa)
526 98ac6176 Ondrej Filip
    bug("Cannot add any area to accepted Interface");
527
  else
528
529
  if (ifa->type == OSPF_IT_VLINK)
530
  {
531
    ifa->oa = po->backbone;
532
    ifa->voa = oa;
533
    ifa->vid = ip->vid;
534 3b16080c Ondrej Filip
    return;                        /* Don't lock, don't add sockets */
535 98ac6176 Ondrej Filip
  }
536
537 0aad2b92 Ondrej Zajicek
  /*
538
   * In some cases we allow more ospf_ifaces on one physical iface.
539
   * In OSPFv2, if they use different IP address prefix.
540
   * In OSPFv3, if they use different instance_id.
541
   * Therefore, we store such info to lock->addr field.
542
   */
543
544 98ac6176 Ondrej Filip
  lock = olock_new(p->pool);
545 0aad2b92 Ondrej Zajicek
#ifdef OSPFv2
546
  lock->addr = ifa->addr->prefix;
547
#else /* OSPFv3 */
548
  lock->addr = _MI(0,0,0,ifa->instance_id);
549
#endif
550 98ac6176 Ondrej Filip
  lock->type = OBJLOCK_IP;
551
  lock->port = OSPF_PROTO;
552
  lock->iface = iface;
553
  lock->data = ifa;
554
  lock->hook = ospf_iface_add;
555
556
  olock_acquire(lock);
557
}
558
559 0aad2b92 Ondrej Zajicek
560
#ifdef OSPFv2
561
562
void
563
ospf_ifa_notify(struct proto *p, unsigned flags, struct ifa *a)
564
{
565
  struct proto_ospf *po = (struct proto_ospf *) p;
566
  struct ospf_config *cf = (struct ospf_config *) (p->cf);
567
568
  if (a->flags & IA_SECONDARY)
569
    return;
570
571
  if (a->scope <= SCOPE_LINK)
572
    return;
573
574
  /* In OSPFv2, we create OSPF iface for each address. */
575
  if (flags & IF_CHANGE_UP)
576
  {
577
    int done = 0;
578
    struct ospf_area_config *ac;
579
    WALK_LIST(ac, cf->area_list)
580
    {
581
      struct ospf_iface_patt *ip = (struct ospf_iface_patt *)
582
        iface_patt_find(&ac->patt_list, a->iface, a);
583
584
      if (ip)
585
      {
586
        if (!done)
587
          ospf_iface_new(po, a->iface, a, ac, ip);
588
        done++;
589
      }
590
    }
591
592
    if (done > 1)
593
      log(L_WARN "%s: Interface %s (IP %I) matches for multiple areas", p->name,  a->iface->name, a->ip);
594
  }
595
596
  if (flags & IF_CHANGE_DOWN)
597
  {
598
    struct ospf_iface *ifa, *ifx;
599
    WALK_LIST_DELSAFE(ifa, ifx, po->iface_list)
600
    {
601
      if ((ifa->type != OSPF_IT_VLINK) && (ifa->addr == a))
602
        ospf_iface_sm(ifa, ISM_DOWN);
603
      /* See a note in ospf_iface_notify() */
604
    }
605
  }
606
}
607
608
#else /* OSPFv3 */
609
610
static inline int iflag_test(u32 *a, u8 i)
611
{
612
  return a[i / 32] & (1u << (i % 32));
613
}
614
615
static inline void iflag_set(u32 *a, u8 i)
616
{
617
  a[i / 32] |= (1u << (i % 32));
618
}
619
620
void
621
ospf_ifa_notify(struct proto *p, unsigned flags, struct ifa *a)
622
{
623
  struct proto_ospf *po = (struct proto_ospf *) p;
624
  struct ospf_config *cf = (struct ospf_config *) (p->cf);
625
626
  if (a->flags & IA_SECONDARY)
627
    return;
628
629
  if (a->scope < SCOPE_LINK)
630
    return;
631
632
  /* In OSPFv3, we create OSPF iface for link-local address,
633
     other addresses are used for link-LSA. */
634
  if (a->scope == SCOPE_LINK)
635
  {
636
    if (flags & IF_CHANGE_UP)
637
    {
638
      u32 found_all[8] = {};
639
      struct ospf_area_config *ac;
640
641
      WALK_LIST(ac, cf->area_list)
642
      {
643
        u32 found_new[8] = {};
644
        struct iface_patt *p;
645
646
        WALK_LIST(p, ac->patt_list)
647
        {
648
          if (iface_patt_match(p, i, a))
649
          {
650
            struct ospf_iface_patt *ip = (struct ospf_iface_patt *) p;
651
652
            /* If true, we already assigned that IID and we skip
653
               this to implement first-match behavior */
654
            if (iflag_test(found_new, ip->instance_id))
655
              continue;
656
657
            /* If true, we already assigned that in a different area,
658
               we log collision */
659
            if (iflag_test(found_all, ip->instance_id))
660
            {
661
              log(L_WARN "%s: Interface %s (IID %d) matches for multiple areas",
662
                  p->name,  a->iface->name, ip->instance_id);
663
              continue;
664
            }
665
666
            iflag_set(found_all, ip->instance_id);
667
            iflag_set(found_new, ip->instance_id);
668
            ospf_iface_new(po, a->iface, a, ac, ip);
669
          }
670
        }
671
      }
672
    }
673
674
    if (flags & IF_CHANGE_DOWN)
675
    {
676
      struct ospf_iface *ifa, *ifx;
677
      WALK_LIST_DELSAFE(ifa, ifx, po->iface_list)
678
      {
679
        if ((ifa->type != OSPF_IT_VLINK) && (ifa->addr == a))
680
          ospf_iface_sm(ifa, ISM_DOWN);
681
        /* See a note in ospf_iface_notify() */
682
      }
683
    }
684
  }
685
  else
686
  {
687
    struct ospf_iface *ifa;
688
    WALK_LIST(ifa, po->iface_list)
689
    {
690
      if (ifa->iface == a->iface)
691
      {
692
        schedule_rt_lsa(ifa->oa);
693
        /* Event 5 from RFC5340 4.4.3. */
694
        schedule_link_lsa(ifa);
695
        return;
696
      }
697
    }
698
  }
699
}
700
701
#endif
702
703 98ac6176 Ondrej Filip
void
704 94c42054 Ondrej Filip
ospf_iface_change_mtu(struct proto_ospf *po, struct ospf_iface *ifa)
705
{
706
  struct proto *p = &po->proto;
707
  struct ospf_packet *op;
708
  struct ospf_neighbor *n;
709
  OSPF_TRACE(D_EVENTS, "Changing MTU on interface %s.", ifa->iface->name);
710 f9c799a0 Ondrej Zajicek
711
  if (ifa->sk)
712 94c42054 Ondrej Filip
  {
713 f9c799a0 Ondrej Zajicek
    ifa->sk->rbsize = rxbufsize(ifa);
714
    ifa->sk->tbsize = ifa->iface->mtu;
715
    sk_reallocate(ifa->sk);
716 94c42054 Ondrej Filip
  }
717
718
  WALK_LIST(n, ifa->neigh_list)
719
  {
720
    op = (struct ospf_packet *) n->ldbdes;
721
    n->ldbdes = mb_allocz(n->pool, ifa->iface->mtu);
722
723
    if (ntohs(op->length) <= ifa->iface->mtu)        /* If the packet in old buffer is bigger, let it filled by zeros */
724
      memcpy(n->ldbdes, op, ifa->iface->mtu);        /* If the packet is old is same or smaller, copy it */
725
726
    rfree(op);
727
  }
728
}
729
730
void
731 b9ed99f7 Ondrej Filip
ospf_iface_notify(struct proto *p, unsigned flags, struct iface *iface)
732 4364b47e Ondrej Filip
{
733 b9ed99f7 Ondrej Filip
  struct proto_ospf *po = (struct proto_ospf *) p;
734 0aad2b92 Ondrej Zajicek
    
735 e6fcf113 Ondrej Filip
  DBG("%s: If notify called\n", p->name);
736 4364b47e Ondrej Filip
  if (iface->flags & IF_IGNORE)
737
    return;
738
739 b9ed99f7 Ondrej Filip
  if (flags & IF_CHANGE_DOWN)
740 4364b47e Ondrej Filip
  {
741 0aad2b92 Ondrej Zajicek
    struct ospf_iface *ifa, *ifx;
742
    WALK_LIST_DELSAFE(ifa, ifx, po->iface_list)
743
      if ((ifa->type != OSPF_IT_VLINK) && (ifa->iface == iface))
744
        ospf_iface_sm(ifa, ISM_DOWN);
745
746
    /* We use here that even shutting down iface also shuts down
747
       the vlinks, but vlinks are not freed and stays in the
748
       iface_list even when down */
749 4364b47e Ondrej Filip
  }
750
751 b9ed99f7 Ondrej Filip
  if (flags & IF_CHANGE_MTU)
752 4364b47e Ondrej Filip
  {
753 0aad2b92 Ondrej Zajicek
    struct ospf_iface *ifa;
754
    WALK_LIST(ifa, po->iface_list)
755
      if ((ifa->type != OSPF_IT_VLINK) && (ifa->iface == iface))
756
        ospf_iface_change_mtu(po, ifa);
757 4364b47e Ondrej Filip
  }
758
}
759
760 c4f0f014 Ondrej Filip
void
761
ospf_iface_info(struct ospf_iface *ifa)
762
{
763 b9ed99f7 Ondrej Filip
  char *strict = "(strict)";
764
765
  if ((ifa->type != OSPF_IT_NBMA) || (ifa->strictnbma == 0))
766
    strict = "";
767 3b16080c Ondrej Filip
  if (ifa->type == OSPF_IT_VLINK)
768
  {
769 3aab39f5 Ondrej Zajicek
    cli_msg(-1015, "Virtual link to %R:", ifa->vid);
770 6901fd06 Ondrej Filip
    cli_msg(-1015, "\tPeer IP: %I", ifa->vip);
771 3aab39f5 Ondrej Zajicek
    cli_msg(-1015, "\tTransit area: %R (%u)", ifa->voa->areaid,
772 3b16080c Ondrej Filip
            ifa->voa->areaid);
773 3127b817 Ondrej Filip
    cli_msg(-1015, "\tInterface: \"%s\"",
774
            (ifa->iface ? ifa->iface->name : "(none)"));
775 3b16080c Ondrej Filip
  }
776
  else
777
  {
778 0aad2b92 Ondrej Zajicek
#ifdef OSPFv2
779
    if (ifa->addr->flags & IA_UNNUMBERED)
780
      cli_msg(-1015, "Interface %s (peer %I)", ifa->iface->name, ifa->addr->opposite);
781
    else
782
      cli_msg(-1015, "Interface %s (%I/%d)", ifa->iface->name, ifa->addr->prefix, ifa->addr->pxlen);
783
#else /* OSPFv3 */
784
    cli_msg(-1015, "Interface %s (IID %d)", ifa->iface->name, ifa->instance_id);
785
#endif
786 3b16080c Ondrej Filip
    cli_msg(-1015, "\tType: %s %s", ospf_it[ifa->type], strict);
787 3aab39f5 Ondrej Zajicek
    cli_msg(-1015, "\tArea: %R (%u)", ifa->oa->areaid, ifa->oa->areaid);
788 3b16080c Ondrej Filip
  }
789 b9ed99f7 Ondrej Filip
  cli_msg(-1015, "\tState: %s %s", ospf_is[ifa->state],
790
          ifa->stub ? "(stub)" : "");
791
  cli_msg(-1015, "\tPriority: %u", ifa->priority);
792
  cli_msg(-1015, "\tCost: %u", ifa->cost);
793
  cli_msg(-1015, "\tHello timer: %u", ifa->helloint);
794 f9bdcad4 Ondrej Zajicek
795 b9ed99f7 Ondrej Filip
  if (ifa->type == OSPF_IT_NBMA)
796 f8f1e1f1 Ondrej Filip
  {
797 b9ed99f7 Ondrej Filip
    cli_msg(-1015, "\tPoll timer: %u", ifa->pollint);
798 f8f1e1f1 Ondrej Filip
  }
799 b9ed99f7 Ondrej Filip
  cli_msg(-1015, "\tWait timer: %u", ifa->waitint);
800 d8c7d9e8 Ondrej Filip
  cli_msg(-1015, "\tDead timer: %u", ifa->dead);
801 b9ed99f7 Ondrej Filip
  cli_msg(-1015, "\tRetransmit timer: %u", ifa->rxmtint);
802
  if ((ifa->type == OSPF_IT_BCAST) || (ifa->type == OSPF_IT_NBMA))
803 f8f1e1f1 Ondrej Filip
  {
804 3aab39f5 Ondrej Zajicek
    cli_msg(-1015, "\tDesigned router (ID): %R", ifa->drid);
805 b9ed99f7 Ondrej Filip
    cli_msg(-1015, "\tDesigned router (IP): %I", ifa->drip);
806 3aab39f5 Ondrej Zajicek
    cli_msg(-1015, "\tBackup designed router (ID): %R", ifa->bdrid);
807 b9ed99f7 Ondrej Filip
    cli_msg(-1015, "\tBackup designed router (IP): %I", ifa->bdrip);
808 f8f1e1f1 Ondrej Filip
  }
809 78e2c6cc Ondrej Filip
}
810 39e517d4 Ondrej Filip
811
void
812
ospf_iface_shutdown(struct ospf_iface *ifa)
813
{
814
  init_list(&ifa->neigh_list);
815
  hello_timer_hook(ifa->hello_timer);
816 353729f5 Ondrej Zajicek
  ospf_sk_close(ifa);
817 39e517d4 Ondrej Filip
}