Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (33.4 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999--2005 Ondrej Filip <feela@network.cz>
5
 *        (c) 2009--2014 Ondrej Zajicek <santiago@crfreenet.org>
6
 *        (c) 2009--2014 CZ.NIC z.s.p.o.
7
 *
8
 *        Can be freely distributed and used under the terms of the GNU GPL.
9
 */
10

    
11
#include "ospf.h"
12

    
13

    
14
const char *ospf_is_names[] = {
15
  "Down", "Loopback", "Waiting", "PtP", "DROther", "Backup", "DR"
16
};
17

    
18
const char *ospf_ism_names[] = {
19
  "InterfaceUp", "WaitTimer", "BackupSeen", "NeighborChange",
20
  "LoopInd", "UnloopInd", "InterfaceDown"
21
};
22

    
23
const char *ospf_it[] = { "broadcast", "nbma", "ptp", "ptmp", "virtual link" };
24

    
25

    
26
static void
27
poll_timer_hook(timer * timer)
28
{
29
  ospf_send_hello(timer->data, OHS_POLL, NULL);
30
}
31

    
32
static void
33
hello_timer_hook(timer * timer)
34
{
35
  ospf_send_hello(timer->data, OHS_HELLO, NULL);
36
}
37

    
38
static void
39
wait_timer_hook(timer * timer)
40
{
41
  struct ospf_iface *ifa = (struct ospf_iface *) timer->data;
42
  struct ospf_proto *p = ifa->oa->po;
43

    
44
  OSPF_TRACE(D_EVENTS, "Wait timer fired on %s", ifa->ifname);
45
  ospf_iface_sm(ifa, ISM_WAITF);
46
}
47

    
48
static inline uint
49
ifa_tx_length(struct ospf_iface *ifa)
50
{
51
  return ifa->cf->tx_length ?: ifa->iface->mtu;
52
}
53

    
54
static inline uint
55
ifa_bufsize(struct ospf_iface *ifa)
56
{
57
  uint bsize = ifa->cf->rx_buffer ?: ifa->iface->mtu;
58
  return MAX(bsize, ifa->tx_length);
59
}
60

    
61
static inline uint
62
ifa_flood_queue_size(struct ospf_iface *ifa)
63
{
64
  return ifa->tx_length / 24;
65
}
66

    
67
int
68
ospf_iface_assure_bufsize(struct ospf_iface *ifa, uint plen)
69
{
70
  struct ospf_proto *p = ifa->oa->po;
71

    
72
  plen += ospf_is_v2(p) ? IP4_HEADER_LENGTH : IP6_HEADER_LENGTH;
73

    
74
  /* This is relevant just for OSPFv2 */
75
  if (ifa->autype == OSPF_AUTH_CRYPT)
76
    plen += OSPF_AUTH_CRYPT_SIZE;
77

    
78
  if (plen <= ifa->sk->tbsize)
79
    return 0;
80

    
81
  if (ifa->cf->rx_buffer || (plen > 0xffff))
82
    return -1;
83

    
84
  plen = BIRD_ALIGN(plen, 1024);
85
  plen = MIN(plen, 0xffff);
86
  sk_set_tbsize(ifa->sk, plen);
87
  return 1;
88
}
89

    
90

    
91
struct nbma_node *
92
find_nbma_node_(list *nnl, ip_addr ip)
93
{
94
  struct nbma_node *nn;
95

    
96
  WALK_LIST(nn, *nnl)
97
    if (ipa_equal(nn->ip, ip))
98
      return nn;
99

    
100
  return NULL;
101
}
102

    
103

    
104
static int
105
ospf_sk_open(struct ospf_iface *ifa)
106
{
107
  struct ospf_proto *p = ifa->oa->po;
108

    
109
  sock *sk = sk_new(ifa->pool);
110
  sk->type = SK_IP;
111
  sk->dport = OSPF_PROTO;
112
  sk->saddr = ifa->addr->ip;
113
  sk->iface = ifa->iface;
114

    
115
  sk->tos = ifa->cf->tx_tos;
116
  sk->priority = ifa->cf->tx_priority;
117
  sk->rx_hook = ospf_rx_hook;
118
  // sk->tx_hook = ospf_tx_hook;
119
  sk->err_hook = ospf_err_hook;
120
  sk->rbsize = sk->tbsize = ifa_bufsize(ifa);
121
  sk->data = (void *) ifa;
122
  sk->flags = SKF_LADDR_RX | (ifa->check_ttl ? SKF_TTL_RX : 0);
123
  sk->ttl = ifa->cf->ttl_security ? 255 : 1;
124

    
125
  if (sk_open(sk) < 0)
126
    goto err;
127

    
128
  /* 12 is an offset of the checksum in an OSPFv3 packet */
129
  if (ospf_is_v3(p))
130
    if (sk_set_ipv6_checksum(sk, 12) < 0)
131
      goto err;
132

    
133
  if ((ifa->type == OSPF_IT_BCAST) || (ifa->type == OSPF_IT_PTP))
134
  {
135
    if (ifa->cf->real_bcast)
136
    {
137
      ifa->all_routers = ifa->addr->brd;
138
      ifa->des_routers = IPA_NONE;
139

    
140
      if (sk_setup_broadcast(sk) < 0)
141
        goto err;
142
    }
143
    else
144
    {
145
      ifa->all_routers = ospf_is_v2(p) ? IP4_OSPF_ALL_ROUTERS : IP6_OSPF_ALL_ROUTERS;
146
      ifa->des_routers = ospf_is_v2(p) ? IP4_OSPF_DES_ROUTERS : IP6_OSPF_DES_ROUTERS;
147

    
148
      if (sk_setup_multicast(sk) < 0)
149
        goto err;
150

    
151
      if (sk_join_group(sk, ifa->all_routers) < 0)
152
        goto err;
153
    }
154
  }
155

    
156
  ifa->sk = sk;
157
  ifa->sk_dr = 0;
158
  return 1;
159

    
160
 err:
161
  sk_log_error(sk, p->p.name);
162
  rfree(sk);
163
  return 0;
164
}
165

    
166
static inline void
167
ospf_sk_join_dr(struct ospf_iface *ifa)
168
{
169
  if (ifa->sk_dr)
170
    return;
171

    
172
  if (sk_join_group(ifa->sk, ifa->des_routers) < 0)
173
    sk_log_error(ifa->sk, ifa->oa->po->p.name);
174

    
175
  ifa->sk_dr = 1;
176
}
177

    
178
static inline void
179
ospf_sk_leave_dr(struct ospf_iface *ifa)
180
{
181
  if (!ifa->sk_dr)
182
    return;
183

    
184
  if (sk_leave_group(ifa->sk, ifa->des_routers) < 0)
185
    sk_log_error(ifa->sk, ifa->oa->po->p.name);
186

    
187
  ifa->sk_dr = 0;
188
}
189

    
190
void
191
ospf_open_vlink_sk(struct ospf_proto *p)
192
{
193
  sock *sk = sk_new(p->p.pool);
194
  sk->type = SK_IP;
195
  sk->dport = OSPF_PROTO;
196

    
197
  /* FIXME: configurable tos/priority ? */
198
  sk->tos = IP_PREC_INTERNET_CONTROL;
199
  sk->priority = sk_priority_control;
200
  sk->err_hook = ospf_verr_hook;
201

    
202
  sk->rbsize = 0;
203
  sk->tbsize = ospf_is_v2(p) ? IP4_MIN_MTU : IP6_MIN_MTU;
204
  sk->data = (void *) p;
205
  sk->flags = 0;
206

    
207
  if (sk_open(sk) < 0)
208
    goto err;
209

    
210
  /* 12 is an offset of the checksum in an OSPFv3 packet */
211
  if (ospf_is_v3(p))
212
    if (sk_set_ipv6_checksum(sk, 12) < 0)
213
      goto err;
214

    
215
  p->vlink_sk = sk;
216
  return;
217

    
218
 err:
219
  sk_log_error(sk, p->p.name);
220
  log(L_ERR "%s: Cannot open virtual link socket", p->p.name);
221
  rfree(sk);
222
}
223

    
224
static void
225
ospf_iface_down(struct ospf_iface *ifa)
226
{
227
  struct ospf_proto *p = ifa->oa->po;
228
  struct ospf_neighbor *n, *nx;
229
  struct ospf_iface *iff;
230

    
231
  if (ifa->type != OSPF_IT_VLINK)
232
  {
233
    if (ospf_is_v3(ifa->oa->po))
234
      OSPF_TRACE(D_EVENTS, "Removing interface %s (IID %d) from area %R",
235
                 ifa->ifname, ifa->instance_id, ifa->oa->areaid);
236
    else if (ifa->addr->flags & IA_PEER)
237
      OSPF_TRACE(D_EVENTS, "Removing interface %s (peer %I) from area %R",
238
                 ifa->ifname, ifa->addr->opposite, ifa->oa->areaid);
239
    else
240
      OSPF_TRACE(D_EVENTS, "Removing interface %s (%I/%d) from area %R",
241
                 ifa->ifname, ifa->addr->prefix, ifa->addr->pxlen, ifa->oa->areaid);
242

    
243
    /* First of all kill all the related vlinks */
244
    WALK_LIST(iff, p->iface_list)
245
    {
246
      if ((iff->type == OSPF_IT_VLINK) && (iff->vifa == ifa))
247
        ospf_iface_sm(iff, ISM_DOWN);
248
    }
249
  }
250

    
251
  WALK_LIST_DELSAFE(n, nx, ifa->neigh_list)
252
    ospf_neigh_sm(n, INM_KILLNBR);
253

    
254
  if (ifa->hello_timer)
255
    tm_stop(ifa->hello_timer);
256

    
257
  if (ifa->poll_timer)
258
    tm_stop(ifa->poll_timer);
259

    
260
  if (ifa->wait_timer)
261
    tm_stop(ifa->wait_timer);
262

    
263
  ospf_flush2_lsa(p, &ifa->link_lsa);
264
  ospf_flush2_lsa(p, &ifa->net_lsa);
265
  ospf_flush2_lsa(p, &ifa->pxn_lsa);
266

    
267
  if (ifa->type == OSPF_IT_VLINK)
268
  {
269
    ifa->vifa = NULL;
270
    ifa->addr = NULL;
271
    ifa->cost = 0;
272
    ifa->vip = IPA_NONE;
273
  }
274

    
275
  ifa->rt_pos_beg = 0;
276
  ifa->rt_pos_end = 0;
277
  ifa->px_pos_beg = 0;
278
  ifa->px_pos_end = 0;
279
}
280

    
281

    
282
void
283
ospf_iface_remove(struct ospf_iface *ifa)
284
{
285
  struct ospf_proto *p = ifa->oa->po;
286
  int i;
287

    
288
  if (ifa->type == OSPF_IT_VLINK)
289
    OSPF_TRACE(D_EVENTS, "Removing vlink to %R via area %R", ifa->vid, ifa->voa->areaid);
290

    
291
  /* Release LSAs from flood queue */
292
  if (!ifa->stub)
293
    for (i = 0; i < ifa->flood_queue_used; i++)
294
      ifa->flood_queue[i]->ret_count--;
295

    
296
  ospf_iface_sm(ifa, ISM_DOWN);
297
  rem_node(NODE ifa);
298
  rfree(ifa->pool);
299
}
300

    
301
void
302
ospf_iface_shutdown(struct ospf_iface *ifa)
303
{
304
  if (ifa->state > OSPF_IS_DOWN)
305
    ospf_send_hello(ifa, OHS_SHUTDOWN, NULL);
306
}
307

    
308
/**
309
 * ospf_iface_chstate - handle changes of interface state
310
 * @ifa: OSPF interface
311
 * @state: new state
312
 *
313
 * Many actions must be taken according to interface state changes. New network
314
 * LSAs must be originated, flushed, new multicast sockets to listen for messages for
315
 * %ALLDROUTERS have to be opened, etc.
316
 */
317
void
318
ospf_iface_chstate(struct ospf_iface *ifa, u8 state)
319
{
320
  struct ospf_proto *p = ifa->oa->po;
321
  u8 oldstate = ifa->state;
322

    
323
  if (state == oldstate)
324
    return;
325

    
326
  OSPF_TRACE(D_EVENTS, "Interface %s changed state from %s to %s",
327
             ifa->ifname, ospf_is_names[oldstate], ospf_is_names[state]);
328

    
329
  ifa->state = state;
330

    
331
  if ((ifa->type == OSPF_IT_BCAST) && ipa_nonzero(ifa->des_routers) && ifa->sk)
332
  {
333
    if ((state == OSPF_IS_BACKUP) || (state == OSPF_IS_DR))
334
      ospf_sk_join_dr(ifa);
335
    else
336
      ospf_sk_leave_dr(ifa);
337
  }
338

    
339
  if ((oldstate > OSPF_IS_LOOP) && (state <= OSPF_IS_LOOP))
340
    ospf_iface_down(ifa);
341

    
342
  /* RFC 2328 12.4 Event 2 - iface state change */
343
  ospf_notify_rt_lsa(ifa->oa);
344

    
345
  /* RFC 5340 4.4.3 Event 1 - iface state change */
346
  ospf_notify_link_lsa(ifa);
347

    
348
  /* RFC 2328 12.4 Event 3 - iface enters/leaves DR state */
349
  ospf_notify_net_lsa(ifa);
350
}
351

    
352
/**
353
 * ospf_iface_sm - OSPF interface state machine
354
 * @ifa: OSPF interface
355
 * @event: event comming to state machine
356
 *
357
 * This fully respects 9.3 of RFC 2328 except we have slightly
358
 * different handling of %DOWN and %LOOP state. We remove intefaces
359
 * that are %DOWN. %DOWN state is used when an interface is waiting
360
 * for a lock. %LOOP state is used when an interface does not have a
361
 * link.
362
 */
363
void
364
ospf_iface_sm(struct ospf_iface *ifa, int event)
365
{
366
  DBG("SM on %s. Event is '%s'\n", ifa->ifname, ospf_ism_names[event]);
367

    
368
  switch (event)
369
  {
370
  case ISM_UP:
371
    if (ifa->state <= OSPF_IS_LOOP)
372
    {
373
      /* Now, nothing should be adjacent */
374
      if ((ifa->type == OSPF_IT_PTP) ||
375
          (ifa->type == OSPF_IT_PTMP) ||
376
          (ifa->type == OSPF_IT_VLINK))
377
      {
378
        ospf_iface_chstate(ifa, OSPF_IS_PTP);
379
      }
380
      else
381
      {
382
        if (ifa->priority == 0)
383
          ospf_iface_chstate(ifa, OSPF_IS_DROTHER);
384
        else
385
        {
386
          ospf_iface_chstate(ifa, OSPF_IS_WAITING);
387
          if (ifa->wait_timer)
388
            tm_start(ifa->wait_timer, ifa->waitint);
389
        }
390
      }
391

    
392
      if (ifa->hello_timer)
393
        tm_start(ifa->hello_timer, ifa->helloint);
394

    
395
      if (ifa->poll_timer)
396
        tm_start(ifa->poll_timer, ifa->pollint);
397

    
398
      ospf_send_hello(ifa, OHS_HELLO, NULL);
399
    }
400
    break;
401

    
402
  case ISM_BACKS:
403
  case ISM_WAITF:
404
    if (ifa->state == OSPF_IS_WAITING)
405
      ospf_dr_election(ifa);
406
    break;
407

    
408
  case ISM_NEICH:
409
    if (ifa->state >= OSPF_IS_DROTHER)
410
      ospf_dr_election(ifa);
411
    break;
412

    
413
  case ISM_LOOP:
414
    if ((ifa->state > OSPF_IS_LOOP) && ifa->check_link)
415
      ospf_iface_chstate(ifa, OSPF_IS_LOOP);
416
    break;
417

    
418
  case ISM_UNLOOP:
419
    /* Immediate go UP */
420
    if (ifa->state == OSPF_IS_LOOP)
421
      ospf_iface_sm(ifa, ISM_UP);
422
    break;
423

    
424
  case ISM_DOWN:
425
    ospf_iface_chstate(ifa, OSPF_IS_DOWN);
426
    break;
427

    
428
  default:
429
    bug("OSPF_I_SM - Unknown event?");
430
    break;
431
  }
432

    
433
}
434

    
435
static u8
436
ospf_iface_classify_(struct iface *ifa, struct ifa *addr)
437
{
438
  if (ipa_nonzero(addr->opposite))
439
    return (ifa->flags & IF_MULTICAST) ? OSPF_IT_PTP :  OSPF_IT_PTMP;
440

    
441
  if ((ifa->flags & (IF_MULTIACCESS | IF_MULTICAST)) ==
442
      (IF_MULTIACCESS | IF_MULTICAST))
443
    return OSPF_IT_BCAST;
444

    
445
  if ((ifa->flags & (IF_MULTIACCESS | IF_MULTICAST)) == IF_MULTIACCESS)
446
    return OSPF_IT_NBMA;
447

    
448
  return OSPF_IT_PTP;
449
}
450

    
451
static inline u8
452
ospf_iface_classify(u8 type, struct ifa *addr)
453
{
454
  return (type != OSPF_IT_UNDEF) ? type : ospf_iface_classify_(addr->iface, addr);
455
}
456

    
457

    
458
struct ospf_iface *
459
ospf_iface_find(struct ospf_proto *p, struct iface *what)
460
{
461
  struct ospf_iface *ifa;
462

    
463
  WALK_LIST(ifa, p->iface_list)
464
    if ((ifa->iface == what) && (ifa->type != OSPF_IT_VLINK))
465
      return ifa;
466

    
467
  return NULL;
468
}
469

    
470
static void
471
ospf_iface_add(struct object_lock *lock)
472
{
473
  struct ospf_iface *ifa = lock->data;
474
  struct ospf_proto *p = ifa->oa->po;
475

    
476
  /* Open socket if interface is not stub */
477
  if (! ifa->stub && ! ospf_sk_open(ifa))
478
  {
479
    log(L_ERR "%s: Cannot open socket for %s, declaring as stub", p->p.name, ifa->ifname);
480
    ifa->ioprob = OSPF_I_SK;
481
    ifa->stub = 1;
482
  }
483

    
484
  if (! ifa->stub)
485
  {
486
    ifa->hello_timer = tm_new_set(ifa->pool, hello_timer_hook, ifa, 0, ifa->helloint);
487

    
488
    if (ifa->type == OSPF_IT_NBMA)
489
      ifa->poll_timer = tm_new_set(ifa->pool, poll_timer_hook, ifa, 0, ifa->pollint);
490

    
491
    if ((ifa->type == OSPF_IT_BCAST) || (ifa->type == OSPF_IT_NBMA))
492
      ifa->wait_timer = tm_new_set(ifa->pool, wait_timer_hook, ifa, 0, 0);
493

    
494
    ifa->flood_queue_size = ifa_flood_queue_size(ifa);
495
    ifa->flood_queue = mb_allocz(ifa->pool, ifa->flood_queue_size * sizeof(void *));
496
  }
497

    
498
  /* Do iface UP, unless there is no link (then wait in LOOP state) */
499
  if (!ifa->check_link || (ifa->iface->flags & IF_LINK_UP))
500
    ospf_iface_sm(ifa, ISM_UP);
501
  else
502
    ospf_iface_chstate(ifa, OSPF_IS_LOOP);
503
}
504

    
505
static inline void
506
add_nbma_node(struct ospf_iface *ifa, struct nbma_node *src, int found)
507
{
508
  struct nbma_node *n = mb_alloc(ifa->pool, sizeof(struct nbma_node));
509
  add_tail(&ifa->nbma_list, NODE n);
510
  n->ip = src->ip;
511
  n->eligible = src->eligible;
512
  n->found = found;
513
}
514

    
515
static int
516
ospf_iface_stubby(struct ospf_iface_patt *ip, struct ifa *addr)
517
{
518
  /* a host address */
519
  if (addr->flags & IA_HOST)
520
    return 1;
521

    
522
  /* a loopback iface */
523
  if (addr->iface->flags & IF_LOOPBACK)
524
    return 1;
525

    
526
  /*
527
   * For compatibility reasons on BSD systems, we force OSPF
528
   * interfaces with non-primary IP prefixes to be stub.
529
   */
530
#if defined(OSPFv2) && !defined(CONFIG_MC_PROPER_SRC)
531
  if (!ip->bsd_secondary && !(addr->flags & IA_PRIMARY))
532
    return 1;
533
#endif
534

    
535
  return ip->stub;
536
}
537

    
538
void
539
ospf_iface_new(struct ospf_area *oa, struct ifa *addr, struct ospf_iface_patt *ip)
540
{
541
  struct ospf_proto *p = oa->po;
542
  struct iface *iface = addr->iface;
543
  struct ospf_iface *ifa;
544
  struct pool *pool;
545

    
546
  if (ospf_is_v3(p))
547
    OSPF_TRACE(D_EVENTS, "Adding interface %s (IID %d) to area %R",
548
               iface->name, ip->instance_id, oa->areaid);
549
  else if (addr->flags & IA_PEER)
550
    OSPF_TRACE(D_EVENTS, "Adding interface %s (peer %I) to area %R",
551
               iface->name, addr->opposite, oa->areaid);
552
  else
553
    OSPF_TRACE(D_EVENTS, "Adding interface %s (%I/%d) to area %R",
554
               iface->name, addr->prefix, addr->pxlen, oa->areaid);
555

    
556
  pool = rp_new(p->p.pool, "OSPF Interface");
557
  ifa = mb_allocz(pool, sizeof(struct ospf_iface));
558
  ifa->iface = iface;
559
  ifa->addr = addr;
560
  ifa->oa = oa;
561
  ifa->cf = ip;
562
  ifa->pool = pool;
563

    
564
  ifa->iface_id = iface->index;
565
  ifa->ifname = iface->name;
566

    
567
  ifa->cost = ip->cost;
568
  ifa->rxmtint = ip->rxmtint;
569
  ifa->inftransdelay = ip->inftransdelay;
570
  ifa->priority = ip->priority;
571
  ifa->helloint = ip->helloint;
572
  ifa->pollint = ip->pollint;
573
  ifa->strictnbma = ip->strictnbma;
574
  ifa->waitint = ip->waitint;
575
  ifa->deadint = ip->deadint;
576
  ifa->stub = ospf_iface_stubby(ip, addr);
577
  ifa->ioprob = OSPF_I_OK;
578
  ifa->tx_length = ifa_tx_length(ifa);
579
  ifa->check_link = ip->check_link;
580
  ifa->ecmp_weight = ip->ecmp_weight;
581
  ifa->check_ttl = (ip->ttl_security == 1);
582
  ifa->bfd = ip->bfd;
583
  ifa->autype = ip->autype;
584
  ifa->passwords = ip->passwords;
585
  ifa->instance_id = ip->instance_id;
586

    
587
  ifa->ptp_netmask = !(addr->flags & IA_PEER);
588
  if (ip->ptp_netmask < 2)
589
    ifa->ptp_netmask = ip->ptp_netmask;
590

    
591

    
592
  ifa->type = ospf_iface_classify(ip->type, addr);
593

    
594
  /* Check validity of interface type */
595
  int old_type = ifa->type;
596
  u32 if_multi_flag = ip->real_bcast ? IF_BROADCAST : IF_MULTICAST;
597

    
598
  if (ospf_is_v2(p) && (ifa->type == OSPF_IT_BCAST) && (addr->flags & IA_PEER))
599
    ifa->type = OSPF_IT_PTP;
600

    
601
  if (ospf_is_v2(p) && (ifa->type == OSPF_IT_NBMA) && (addr->flags & IA_PEER))
602
    ifa->type = OSPF_IT_PTMP;
603

    
604
  if ((ifa->type == OSPF_IT_BCAST) && !(iface->flags & if_multi_flag))
605
    ifa->type = OSPF_IT_NBMA;
606

    
607
  if ((ifa->type == OSPF_IT_PTP) && !(iface->flags & if_multi_flag))
608
    ifa->type = OSPF_IT_PTMP;
609

    
610
  if (ifa->type != old_type)
611
    log(L_WARN "%s: Cannot use interface %s as %s, forcing %s",
612
        p->p.name, iface->name, ospf_it[old_type], ospf_it[ifa->type]);
613

    
614

    
615
  if ((ifa->type == OSPF_IT_PTP) || (ifa->type == OSPF_IT_PTMP))
616
    ifa->link_lsa_suppression = ip->link_lsa_suppression;
617

    
618
  ifa->state = OSPF_IS_DOWN;
619
  init_list(&ifa->neigh_list);
620
  init_list(&ifa->nbma_list);
621

    
622
  struct nbma_node *nb;
623
  WALK_LIST(nb, ip->nbma_list)
624
  {
625
    /* In OSPFv3, addr is link-local while configured neighbors could
626
       have global IP (although RFC 5340 C.5 says link-local addresses
627
       should be used). Because OSPFv3 iface is not subnet-specific,
628
       there is no need for ipa_in_net() check */
629

    
630
    if (ospf_is_v2(p) && !ipa_in_net(nb->ip, addr->prefix, addr->pxlen))
631
      continue;
632

    
633
    if (ospf_is_v3(p) && !ipa_is_link_local(nb->ip))
634
      log(L_WARN "%s: Configured neighbor address (%I) should be link-local",
635
          p->p.name, nb->ip);
636

    
637
    add_nbma_node(ifa, nb, 0);
638
  }
639

    
640
  add_tail(&oa->po->iface_list, NODE ifa);
641

    
642
  struct object_lock *lock = olock_new(pool);
643
  lock->addr = ospf_is_v2(p) ? ifa->addr->prefix : IPA_NONE;
644
  lock->type = OBJLOCK_IP;
645
  lock->port = OSPF_PROTO;
646
  lock->inst = ifa->instance_id;
647
  lock->iface = iface;
648
  lock->data = ifa;
649
  lock->hook = ospf_iface_add;
650

    
651
  olock_acquire(lock);
652
}
653

    
654
void
655
ospf_iface_new_vlink(struct ospf_proto *p, struct ospf_iface_patt *ip)
656
{
657
  struct ospf_iface *ifa;
658
  struct pool *pool;
659

    
660
  if (!p->vlink_sk)
661
    return;
662

    
663
  OSPF_TRACE(D_EVENTS, "Adding vlink to %R via area %R", ip->vid, ip->voa);
664

    
665
  /* Vlink ifname is stored just after the ospf_iface structure */
666

    
667
  pool = rp_new(p->p.pool, "OSPF Vlink");
668
  ifa = mb_allocz(pool, sizeof(struct ospf_iface) + 16);
669
  ifa->oa = p->backbone;
670
  ifa->cf = ip;
671
  ifa->pool = pool;
672

    
673
  /* Assign iface ID, for vlinks, this is ugly hack */
674
  u32 vlink_id = p->last_vlink_id++;
675
  ifa->iface_id = vlink_id + OSPF_VLINK_ID_OFFSET;
676
  ifa->ifname = (void *) (ifa + 1);
677
  bsprintf(ifa->ifname, "vlink%d", vlink_id);
678

    
679
  ifa->voa = ospf_find_area(p, ip->voa);
680
  ifa->vid = ip->vid;
681
  ifa->sk = p->vlink_sk;
682

    
683
  ifa->helloint = ip->helloint;
684
  ifa->rxmtint = ip->rxmtint;
685
  ifa->waitint = ip->waitint;
686
  ifa->deadint = ip->deadint;
687
  ifa->inftransdelay = ip->inftransdelay;
688
  ifa->tx_length = ospf_is_v2(p) ? IP4_MIN_MTU : IP6_MIN_MTU;
689
  ifa->autype = ip->autype;
690
  ifa->passwords = ip->passwords;
691
  ifa->instance_id = ip->instance_id;
692

    
693
  ifa->type = OSPF_IT_VLINK;
694

    
695
  ifa->state = OSPF_IS_DOWN;
696
  init_list(&ifa->neigh_list);
697
  init_list(&ifa->nbma_list);
698

    
699
  add_tail(&p->iface_list, NODE ifa);
700

    
701
  ifa->hello_timer = tm_new_set(ifa->pool, hello_timer_hook, ifa, 0, ifa->helloint);
702

    
703
  ifa->flood_queue_size = ifa_flood_queue_size(ifa);
704
  ifa->flood_queue = mb_allocz(ifa->pool, ifa->flood_queue_size * sizeof(void *));
705
}
706

    
707
static void
708
ospf_iface_change_timer(timer *tm, uint val)
709
{
710
  if (!tm)
711
    return;
712

    
713
  tm->recurrent = val;
714

    
715
  if (tm->expires)
716
    tm_start(tm, val);
717
}
718

    
719
static inline void
720
ospf_iface_update_flood_queue_size(struct ospf_iface *ifa)
721
{
722
  uint old_size = ifa->flood_queue_size;
723
  uint new_size = ifa_flood_queue_size(ifa);
724

    
725
  if (new_size <= old_size)
726
    return;
727

    
728
  ifa->flood_queue_size = new_size;
729
  ifa->flood_queue = mb_realloc(ifa->flood_queue, new_size * sizeof(void *));
730
  bzero(ifa->flood_queue + old_size, (new_size - old_size) * sizeof(void *));
731
}
732

    
733
int
734
ospf_iface_reconfigure(struct ospf_iface *ifa, struct ospf_iface_patt *new)
735
{
736
  struct ospf_proto *p = ifa->oa->po;
737
  struct ospf_iface_patt *old = ifa->cf;
738
  char *ifname = ifa->ifname;
739

    
740
  /* Type could be changed in ospf_iface_new(),
741
     but if config values are same then also results are same */
742
  int old_type = ospf_iface_classify(old->type, ifa->addr);
743
  int new_type = ospf_iface_classify(new->type, ifa->addr);
744
  if (old_type != new_type)
745
    return 0;
746

    
747
  int new_stub = ospf_iface_stubby(new, ifa->addr);
748
  if (ifa->stub != new_stub)
749
    return 0;
750

    
751
  /* Change of these options would require to reset the iface socket */
752
  if ((new->real_bcast != old->real_bcast) ||
753
      (new->tx_tos != old->tx_tos) ||
754
      (new->tx_priority != old->tx_priority) ||
755
      (new->ttl_security != old->ttl_security))
756
    return 0;
757

    
758
  ifa->cf = new;
759
  ifa->marked = 0;
760

    
761

    
762
  /* HELLO TIMER */
763
  if (ifa->helloint != new->helloint)
764
  {
765
    OSPF_TRACE(D_EVENTS, "Changing hello interval of %s from %d to %d",
766
               ifname, ifa->helloint, new->helloint);
767

    
768
    ifa->helloint = new->helloint;
769
    ospf_iface_change_timer(ifa->hello_timer, ifa->helloint);
770
  }
771

    
772
  /* RXMT TIMER */
773
  if (ifa->rxmtint != new->rxmtint)
774
  {
775
    OSPF_TRACE(D_EVENTS, "Changing retransmit interval of %s from %d to %d",
776
               ifname, ifa->rxmtint, new->rxmtint);
777

    
778
    ifa->rxmtint = new->rxmtint;
779
    /* FIXME: Update neighbors' timers */
780
  }
781

    
782
  /* POLL TIMER */
783
  if (ifa->pollint != new->pollint)
784
  {
785
    OSPF_TRACE(D_EVENTS, "Changing poll interval of %s from %d to %d",
786
               ifname, ifa->pollint, new->pollint);
787

    
788
    ifa->pollint = new->pollint;
789
    ospf_iface_change_timer(ifa->poll_timer, ifa->pollint);
790
  }
791

    
792
  /* WAIT TIMER */
793
  if (ifa->waitint != new->waitint)
794
  {
795
    OSPF_TRACE(D_EVENTS, "Changing wait interval of %s from %d to %d",
796
               ifname, ifa->waitint, new->waitint);
797

    
798
    ifa->waitint = new->waitint;
799
    if (ifa->wait_timer && ifa->wait_timer->expires)
800
      tm_start(ifa->wait_timer, ifa->waitint);
801
  }
802

    
803
  /* DEAD TIMER */
804
  if (ifa->deadint != new->deadint)
805
  {
806
    OSPF_TRACE(D_EVENTS, "Changing dead interval of %s from %d to %d",
807
               ifname, ifa->deadint, new->deadint);
808
    ifa->deadint = new->deadint;
809
  }
810

    
811
  /* INFTRANS */
812
  if (ifa->inftransdelay != new->inftransdelay)
813
  {
814
    OSPF_TRACE(D_EVENTS, "Changing transmit delay of %s from %d to %d",
815
                     ifname, ifa->inftransdelay, new->inftransdelay);
816
    ifa->inftransdelay = new->inftransdelay;
817
  }
818

    
819
  /* AUTHENTICATION */
820
  if (ifa->autype != new->autype)
821
  {
822
    OSPF_TRACE(D_EVENTS, "Changing authentication type of %s", ifname);
823
    ifa->autype = new->autype;
824
  }
825

    
826
  /* Update passwords */
827
  ifa->passwords = new->passwords;
828

    
829
  /* Remaining options are just for proper interfaces */
830
  if (ifa->type == OSPF_IT_VLINK)
831
    return 1;
832

    
833

    
834
  /* COST */
835
  if (ifa->cost != new->cost)
836
  {
837
    OSPF_TRACE(D_EVENTS, "Changing cost of %s from %d to %d",
838
               ifname, ifa->cost, new->cost);
839

    
840
    ifa->cost = new->cost;
841
  }
842

    
843
  /* PRIORITY */
844
  if (ifa->priority != new->priority)
845
  {
846
    OSPF_TRACE(D_EVENTS, "Changing priority of %s from %d to %d",
847
               ifname, ifa->priority, new->priority);
848

    
849
    ifa->priority = new->priority;
850
    ospf_notify_link_lsa(ifa);
851
  }
852

    
853
  /* STRICT NBMA */
854
  if (ifa->strictnbma != new->strictnbma)
855
  {
856
    OSPF_TRACE(D_EVENTS, "Changing NBMA strictness of %s from %d to %d",
857
               ifname, ifa->strictnbma, new->strictnbma);
858
    ifa->strictnbma = new->strictnbma;
859
  }
860

    
861
  struct nbma_node *nb, *nbx;
862

    
863
  /* NBMA LIST - remove or update old */
864
  WALK_LIST_DELSAFE(nb, nbx, ifa->nbma_list)
865
  {
866
    struct nbma_node *nb2 = find_nbma_node_(&new->nbma_list, nb->ip);
867
    if (nb2)
868
    {
869
      if (nb->eligible != nb2->eligible)
870
      {
871
        OSPF_TRACE(D_EVENTS, "Changing eligibility of NBMA neighbor %I on %s",
872
                   nb->ip, ifname);
873
        nb->eligible = nb2->eligible;
874
      }
875
    }
876
    else
877
    {
878
      OSPF_TRACE(D_EVENTS, "Removing NBMA neighbor %I on %s",
879
                       nb->ip, ifname);
880
      rem_node(NODE nb);
881
      mb_free(nb);
882
    }
883
  }
884

    
885
  /* NBMA LIST - add new */
886
  WALK_LIST(nb, new->nbma_list)
887
  {
888
    /* See related note in ospf_iface_new() */
889
    if (ospf_is_v2(p) && !ipa_in_net(nb->ip, ifa->addr->prefix, ifa->addr->pxlen))
890
      continue;
891

    
892
    if (ospf_is_v3(p) && !ipa_is_link_local(nb->ip))
893
      log(L_WARN "%s: Configured neighbor address (%I) should be link-local",
894
          p->p.name, nb->ip);
895

    
896
    if (! find_nbma_node(ifa, nb->ip))
897
    {
898
      OSPF_TRACE(D_EVENTS, "Adding NBMA neighbor %I on %s",
899
                 nb->ip, ifname);
900
      add_nbma_node(ifa, nb, !!find_neigh_by_ip(ifa, nb->ip));
901
    }
902
  }
903

    
904
  int update_buffers = 0;
905

    
906
  /* TX LENGTH */
907
  if (old->tx_length != new->tx_length)
908
  {
909
    OSPF_TRACE(D_EVENTS, "Changing TX length of %s from %d to %d",
910
               ifname, old->tx_length, new->tx_length);
911

    
912
    /* ifa cannot be vlink */
913
    ifa->tx_length = ifa_tx_length(ifa);
914
    update_buffers = 1;
915

    
916
    if (!ifa->stub)
917
      ospf_iface_update_flood_queue_size(ifa);
918
  }
919

    
920
  /* RX BUFFER */
921
  if (old->rx_buffer != new->rx_buffer)
922
  {
923
    OSPF_TRACE(D_EVENTS, "Changing buffer size of %s from %d to %d",
924
               ifname, old->rx_buffer, new->rx_buffer);
925

    
926
    /* ifa cannot be vlink */
927
    update_buffers = 1;
928
  }
929

    
930
  /* Buffer size depends on both tx_length and rx_buffer options */
931
  if (update_buffers && ifa->sk)
932
  {
933
    uint bsize = ifa_bufsize(ifa);
934
    sk_set_rbsize(ifa->sk, bsize);
935
    sk_set_tbsize(ifa->sk, bsize);
936
  }
937

    
938
  /* LINK */
939
  if (ifa->check_link != new->check_link)
940
  {
941
    OSPF_TRACE(D_EVENTS, "%s link check for %s",
942
               new->check_link ? "Enabling" : "Disabling", ifname);
943
    ifa->check_link = new->check_link;
944

    
945
    /* ifa cannot be vlink */
946
    if (!(ifa->iface->flags & IF_LINK_UP))
947
      ospf_iface_sm(ifa, ifa->check_link ? ISM_LOOP : ISM_UNLOOP);
948
  }
949

    
950
  /* ECMP weight */
951
  if (ifa->ecmp_weight != new->ecmp_weight)
952
  {
953
    OSPF_TRACE(D_EVENTS, "Changing ECMP weight of %s from %d to %d",
954
               ifname, (int)ifa->ecmp_weight + 1, (int)new->ecmp_weight + 1);
955
    ifa->ecmp_weight = new->ecmp_weight;
956
  }
957

    
958
  /* Link LSA suppression */
959
  if (((ifa->type == OSPF_IT_PTP) || (ifa->type == OSPF_IT_PTMP)) &&
960
      (ifa->link_lsa_suppression != new->link_lsa_suppression))
961
  {
962
    OSPF_TRACE(D_EVENTS, "Changing link LSA suppression of %s from %d to %d",
963
               ifname, ifa->link_lsa_suppression, new->link_lsa_suppression);
964

    
965
    ifa->link_lsa_suppression = new->link_lsa_suppression;
966
    ospf_notify_link_lsa(ifa);
967
  }
968

    
969
  /* BFD */
970
  if (ifa->bfd != new->bfd)
971
  {
972
    OSPF_TRACE(D_EVENTS, "%s BFD for %s",
973
               new->bfd ? "Enabling" : "Disabling", ifname);
974
    ifa->bfd = new->bfd;
975

    
976
    struct ospf_neighbor *n;
977
    WALK_LIST(n, ifa->neigh_list)
978
      ospf_neigh_update_bfd(n, ifa->bfd);
979
  }
980

    
981

    
982
  /* instance_id is not updated - it is part of key */
983

    
984
  return 1;
985
}
986

    
987

    
988
/*
989
 * State for matching iface pattterns walk
990
 *
991
 * This is significantly different in OSPFv2 and OSPFv3.
992
 * In OSPFv2, OSPF ifaces are created for each IP prefix (struct ifa)
993
 * In OSPFv3, OSPF ifaces are created based on real iface (struct iface)
994
 * We support instance_id for both OSPFv2 (RFC 6549) and OSPFv3.
995
 *
996
 * We process one ifa/iface and match it for all configured instance IDs. We
997
 * maintain bitfields to track whether given instance ID was already matched.
998
 * We have two bitfields, one global (active) and one per area (ignore), to
999
 * detect misconfigured cases where one iface with one instance ID matches in
1000
 * multiple areas.
1001
 */
1002

    
1003
struct ospf_mip_walk {
1004
  u32 active[8];                /* Bitfield of active instance IDs */
1005
  u32 ignore[8];                /* Bitfield of instance IDs matched in current area */
1006
  struct ospf_area *oa;                /* Current area */
1007
  struct ospf_iface_patt *ip;        /* Current iface pattern */
1008
  struct iface *iface;                /* Specified iface (input) */
1009
  struct ifa *a;                /* Specified ifa (input) */
1010
  int warn;                        /* Whether iface matched in multiple areas */
1011
};
1012

    
1013
static int
1014
ospf_walk_matching_iface_patts(struct ospf_proto *p, struct ospf_mip_walk *s)
1015
{
1016
  int id;
1017

    
1018
  if (s->ip)
1019
    goto step;
1020

    
1021
  WALK_LIST(s->oa, p->area_list)
1022
  {
1023
    if (s->oa->marked)
1024
      continue;
1025

    
1026
    WALK_LIST(s->ip, s->oa->ac->patt_list)
1027
    {
1028
      id = s->ip->instance_id;
1029
      if (BIT32_TEST(s->ignore, id))
1030
        continue;
1031

    
1032
      if (iface_patt_match(&s->ip->i, s->iface, s->a))
1033
      {
1034
        /* Now we matched ifa/iface/instance_id for the first time in current area */
1035
        BIT32_SET(s->ignore, id);
1036

    
1037
        /* If we already found it in previous areas, ignore it and add warning */
1038
        if (BIT32_TEST(s->active, id))
1039
          { s->warn = 1; continue; }
1040

    
1041
        BIT32_SET(s->active, id);
1042
        return 1;
1043
      step:
1044
        ;
1045
      }
1046
    }
1047
    BIT32_ZERO(s->ignore, 256);
1048
  }
1049

    
1050
  if (s->warn)
1051
    log(L_WARN "%s: Interface %s matches for multiple areas", p->p.name, s->iface->name);
1052

    
1053
  return 0;
1054
}
1055

    
1056

    
1057
static struct ospf_iface *
1058
ospf_iface_find_by_key(struct ospf_proto *p, struct ifa *a, int instance_id)
1059
{
1060
  struct ospf_iface *ifa;
1061

    
1062
  WALK_LIST(ifa, p->iface_list)
1063
    if ((ifa->addr == a) && (ifa->instance_id == instance_id) &&
1064
        (ifa->type != OSPF_IT_VLINK))
1065
      return ifa;
1066

    
1067
  return NULL;
1068
}
1069

    
1070

    
1071
void
1072
ospf_ifa_notify2(struct proto *P, uint flags, struct ifa *a)
1073
{
1074
  struct ospf_proto *p = (struct ospf_proto *) P;
1075

    
1076
  if (a->flags & IA_SECONDARY)
1077
    return;
1078

    
1079
  if (a->scope <= SCOPE_LINK)
1080
    return;
1081

    
1082
  /* In OSPFv2, we create OSPF iface for each address. */
1083
  if (flags & IF_CHANGE_UP)
1084
  {
1085
    struct ospf_mip_walk s = { .iface = a->iface, .a = a };
1086
    while (ospf_walk_matching_iface_patts(p, &s))
1087
      ospf_iface_new(s.oa, a, s.ip);
1088
  }
1089

    
1090
  if (flags & IF_CHANGE_DOWN)
1091
  {
1092
    struct ospf_iface *ifa, *ifx;
1093
    WALK_LIST_DELSAFE(ifa, ifx, p->iface_list)
1094
      if ((ifa->type != OSPF_IT_VLINK) && (ifa->addr == a))
1095
        ospf_iface_remove(ifa);
1096
    /* See a note in ospf_iface_notify() */
1097
  }
1098
}
1099

    
1100
void
1101
ospf_ifa_notify3(struct proto *P, uint flags, struct ifa *a)
1102
{
1103
  struct ospf_proto *p = (struct ospf_proto *) P;
1104

    
1105
  if (a->flags & IA_SECONDARY)
1106
    return;
1107

    
1108
  if (a->scope < SCOPE_LINK)
1109
    return;
1110

    
1111
  /* In OSPFv3, we create OSPF iface for link-local address,
1112
     other addresses are used for link-LSA. */
1113
  if (a->scope == SCOPE_LINK)
1114
  {
1115
    if (flags & IF_CHANGE_UP)
1116
    {
1117
      struct ospf_mip_walk s = { .iface = a->iface };
1118
      while (ospf_walk_matching_iface_patts(p, &s))
1119
        ospf_iface_new(s.oa, a, s.ip);
1120
    }
1121

    
1122
    if (flags & IF_CHANGE_DOWN)
1123
    {
1124
      struct ospf_iface *ifa, *ifx;
1125
      WALK_LIST_DELSAFE(ifa, ifx, p->iface_list)
1126
        if ((ifa->addr == a) && (ifa->type != OSPF_IT_VLINK))
1127
          ospf_iface_remove(ifa);
1128
    }
1129
  }
1130
  else
1131
  {
1132
    struct ospf_iface *ifa;
1133
    WALK_LIST(ifa, p->iface_list)
1134
      if (ifa->iface == a->iface)
1135
      {
1136
        /* RFC 5340 4.4.3 Event 5 - prefix added/deleted */
1137
        ospf_notify_link_lsa(ifa);
1138
        ospf_notify_rt_lsa(ifa->oa);
1139
      }
1140
  }
1141
}
1142

    
1143

    
1144
static void
1145
ospf_reconfigure_ifaces2(struct ospf_proto *p)
1146
{
1147
  struct iface *iface;
1148
  struct ifa *a;
1149

    
1150
  WALK_LIST(iface, iface_list)
1151
  {
1152
    if (! (iface->flags & IF_UP))
1153
      continue;
1154

    
1155
    WALK_LIST(a, iface->addrs)
1156
    {
1157
      if (a->flags & IA_SECONDARY)
1158
        continue;
1159

    
1160
      if (a->scope <= SCOPE_LINK)
1161
        continue;
1162

    
1163
      struct ospf_mip_walk s = { .iface = iface, .a = a };
1164
      while (ospf_walk_matching_iface_patts(p, &s))
1165
      {
1166
        /* Main inner loop */
1167
        struct ospf_iface *ifa = ospf_iface_find_by_key(p, a, s.ip->instance_id);
1168
        if (ifa)
1169
        {
1170
          if ((ifa->oa == s.oa) && (ifa->marked < 2) &&
1171
              ospf_iface_reconfigure(ifa, s.ip))
1172
            continue;
1173

    
1174
          /* Hard restart */
1175
          log(L_INFO "%s: Restarting interface %s (%I/%d) in area %R",
1176
              p->p.name, ifa->ifname, a->prefix, a->pxlen, s.oa->areaid);
1177
          ospf_iface_shutdown(ifa);
1178
          ospf_iface_remove(ifa);
1179
        }
1180

    
1181
        ospf_iface_new(s.oa, a, s.ip);
1182
      }
1183
    }
1184
  }
1185
}
1186

    
1187
static void
1188
ospf_reconfigure_ifaces3(struct ospf_proto *p)
1189
{
1190
  struct iface *iface;
1191
  struct ifa *a;
1192

    
1193
  WALK_LIST(iface, iface_list)
1194
  {
1195
    if (! (iface->flags & IF_UP))
1196
      continue;
1197

    
1198
    WALK_LIST(a, iface->addrs)
1199
    {
1200
      if (a->flags & IA_SECONDARY)
1201
        continue;
1202

    
1203
      if (a->scope != SCOPE_LINK)
1204
        continue;
1205

    
1206
      struct ospf_mip_walk s = { .iface = iface };
1207
      while (ospf_walk_matching_iface_patts(p, &s))
1208
      {
1209
        /* Main inner loop */
1210
        struct ospf_iface *ifa = ospf_iface_find_by_key(p, a, s.ip->instance_id);
1211
        if (ifa)
1212
        {
1213
          if ((ifa->oa == s.oa) && (ifa->marked < 2) &&
1214
              ospf_iface_reconfigure(ifa, s.ip))
1215
            continue;
1216

    
1217
          /* Hard restart */
1218
          log(L_INFO "%s: Restarting interface %s (IID %d) in area %R",
1219
              p->p.name, ifa->ifname, ifa->instance_id, s.oa->areaid);
1220
          ospf_iface_shutdown(ifa);
1221
          ospf_iface_remove(ifa);
1222
        }
1223

    
1224
        ospf_iface_new(s.oa, a, s.ip);
1225
      }
1226
    }
1227
  }
1228
}
1229

    
1230
void
1231
ospf_reconfigure_ifaces(struct ospf_proto *p)
1232
{
1233
  if (ospf_is_v2(p))
1234
    ospf_reconfigure_ifaces2(p);
1235
  else
1236
    ospf_reconfigure_ifaces3(p);
1237
}
1238

    
1239

    
1240
static void
1241
ospf_iface_change_mtu(struct ospf_proto *p, struct ospf_iface *ifa)
1242
{
1243
  /* ifa is not vlink */
1244

    
1245
  OSPF_TRACE(D_EVENTS, "Interface %s changed MTU to %d", ifa->iface->mtu);
1246

    
1247
  ifa->tx_length = ifa_tx_length(ifa);
1248

    
1249
  if (!ifa->sk)
1250
    return;
1251

    
1252
  /* We do not shrink dynamic buffers */
1253
  uint bsize = ifa_bufsize(ifa);
1254
  if (bsize > ifa->sk->rbsize)
1255
    sk_set_rbsize(ifa->sk, bsize);
1256
  if (bsize > ifa->sk->tbsize)
1257
    sk_set_tbsize(ifa->sk, bsize);
1258

    
1259
  if (!ifa->stub)
1260
    ospf_iface_update_flood_queue_size(ifa);
1261
}
1262

    
1263
static void
1264
ospf_iface_notify(struct ospf_proto *p, uint flags, struct ospf_iface *ifa)
1265
{
1266
  /* ifa is not vlink */
1267

    
1268
  if (flags & IF_CHANGE_DOWN)
1269
  {
1270
    ospf_iface_remove(ifa);
1271
    return;
1272
  }
1273

    
1274
  if (flags & IF_CHANGE_LINK)
1275
    ospf_iface_sm(ifa, (ifa->iface->flags & IF_LINK_UP) ? ISM_UNLOOP : ISM_LOOP);
1276

    
1277
  if (flags & IF_CHANGE_MTU)
1278
    ospf_iface_change_mtu(p, ifa);
1279
}
1280

    
1281
void
1282
ospf_if_notify(struct proto *P, uint flags, struct iface *iface)
1283
{
1284
  struct ospf_proto *p = (struct ospf_proto *) P;
1285

    
1286
  /*
1287
  if (iface->flags & IF_IGNORE)
1288
    return;
1289
  */
1290

    
1291
  /* Going up means that there are no such ifaces yet */
1292
  if (flags & IF_CHANGE_UP)
1293
    return;
1294

    
1295
  struct ospf_iface *ifa, *ifx;
1296
  WALK_LIST_DELSAFE(ifa, ifx, p->iface_list)
1297
    if (ifa->iface == iface)
1298
      ospf_iface_notify(p, flags, ifa);
1299

    
1300
  /* We use here that even shutting down iface also shuts down
1301
     the vlinks, but vlinks are not freed and stays in the
1302
     iface_list even when down */
1303
}
1304

    
1305
void
1306
ospf_iface_info(struct ospf_iface *ifa)
1307
{
1308
  char *more = "";
1309

    
1310
  if (ifa->strictnbma &&
1311
      ((ifa->type == OSPF_IT_NBMA) || (ifa->type == OSPF_IT_PTMP)))
1312
    more = " (strict)";
1313

    
1314
  if (ifa->cf->real_bcast &&
1315
      ((ifa->type == OSPF_IT_BCAST) || (ifa->type == OSPF_IT_PTP)))
1316
    more = " (real)";
1317

    
1318
  if (ifa->type == OSPF_IT_VLINK)
1319
  {
1320
    cli_msg(-1015, "Virtual link %s to %R", ifa->ifname, ifa->vid);
1321
    cli_msg(-1015, "\tPeer IP: %I", ifa->vip);
1322
    cli_msg(-1015, "\tTransit area: %R (%u)", ifa->voa->areaid, ifa->voa->areaid);
1323
  }
1324
  else
1325
  {
1326
    if (ospf_is_v3(ifa->oa->po))
1327
      cli_msg(-1015, "Interface %s (IID %d)", ifa->ifname, ifa->instance_id);
1328
    else if (ifa->addr->flags & IA_PEER)
1329
      cli_msg(-1015, "Interface %s (peer %I)", ifa->ifname, ifa->addr->opposite);
1330
    else
1331
      cli_msg(-1015, "Interface %s (%I/%d)", ifa->ifname, ifa->addr->prefix, ifa->addr->pxlen);
1332

    
1333
    cli_msg(-1015, "\tType: %s%s", ospf_it[ifa->type], more);
1334
    cli_msg(-1015, "\tArea: %R (%u)", ifa->oa->areaid, ifa->oa->areaid);
1335
  }
1336
  cli_msg(-1015, "\tState: %s%s", ospf_is_names[ifa->state], ifa->stub ? " (stub)" : "");
1337
  cli_msg(-1015, "\tPriority: %u", ifa->priority);
1338
  cli_msg(-1015, "\tCost: %u", ifa->cost);
1339
  if (ifa->oa->po->ecmp)
1340
    cli_msg(-1015, "\tECMP weight: %d", ((int) ifa->ecmp_weight) + 1);
1341
  cli_msg(-1015, "\tHello timer: %u", ifa->helloint);
1342

    
1343
  if (ifa->type == OSPF_IT_NBMA)
1344
  {
1345
    cli_msg(-1015, "\tPoll timer: %u", ifa->pollint);
1346
  }
1347
  cli_msg(-1015, "\tWait timer: %u", ifa->waitint);
1348
  cli_msg(-1015, "\tDead timer: %u", ifa->deadint);
1349
  cli_msg(-1015, "\tRetransmit timer: %u", ifa->rxmtint);
1350
  if ((ifa->type == OSPF_IT_BCAST) || (ifa->type == OSPF_IT_NBMA))
1351
  {
1352
    cli_msg(-1015, "\tDesignated router (ID): %R", ifa->drid);
1353
    cli_msg(-1015, "\tDesignated router (IP): %I", ifa->drip);
1354
    cli_msg(-1015, "\tBackup designated router (ID): %R", ifa->bdrid);
1355
    cli_msg(-1015, "\tBackup designated router (IP): %I", ifa->bdrip);
1356
  }
1357
}