Statistics
| Branch: | Revision:

iof-bird-daemon / nest / iface.c @ 153f02da

History | View | Annotate | Download (19.4 KB)

1
/*
2
 *        BIRD -- Management of Interfaces and Neighbor Cache
3
 *
4
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
/**
10
 * DOC: Interfaces
11
 *
12
 * The interface module keeps track of all network interfaces in the
13
 * system and their addresses.
14
 *
15
 * Each interface is represented by an &iface structure which carries
16
 * interface capability flags (%IF_MULTIACCESS, %IF_BROADCAST etc.),
17
 * MTU, interface name and index and finally a linked list of network
18
 * prefixes assigned to the interface, each one represented by
19
 * struct &ifa.
20
 *
21
 * The interface module keeps a `soft-up' state for each &iface which
22
 * is a conjunction of link being up, the interface being of a `sane'
23
 * type and at least one IP address assigned to it.
24
 */
25

    
26
#undef LOCAL_DEBUG
27

    
28
#include "nest/bird.h"
29
#include "nest/iface.h"
30
#include "nest/protocol.h"
31
#include "nest/cli.h"
32
#include "lib/resource.h"
33
#include "lib/string.h"
34
#include "conf/conf.h"
35
#include "sysdep/unix/krt.h"
36

    
37
static pool *if_pool;
38

    
39
list iface_list;
40

    
41
static void if_recalc_preferred(struct iface *i);
42

    
43
/**
44
 * ifa_dump - dump interface address
45
 * @a: interface address descriptor
46
 *
47
 * This function dumps contents of an &ifa to the debug output.
48
 */
49
void
50
ifa_dump(struct ifa *a)
51
{
52
  debug("\t%I, net %N bc %I -> %I%s%s%s%s\n", a->ip, &a->prefix, a->brd, a->opposite,
53
        (a->flags & IA_PRIMARY) ? " PRIMARY" : "",
54
        (a->flags & IA_SECONDARY) ? " SEC" : "",
55
        (a->flags & IA_HOST) ? " HOST" : "",
56
        (a->flags & IA_PEER) ? " PEER" : "");
57
}
58

    
59
/**
60
 * if_dump - dump interface
61
 * @i: interface to dump
62
 *
63
 * This function dumps all information associated with a given
64
 * network interface to the debug output.
65
 */
66
void
67
if_dump(struct iface *i)
68
{
69
  struct ifa *a;
70

    
71
  debug("IF%d: %s", i->index, i->name);
72
  if (i->flags & IF_SHUTDOWN)
73
    debug(" SHUTDOWN");
74
  if (i->flags & IF_UP)
75
    debug(" UP");
76
  else
77
    debug(" DOWN");
78
  if (i->flags & IF_ADMIN_UP)
79
    debug(" LINK-UP");
80
  if (i->flags & IF_MULTIACCESS)
81
    debug(" MA");
82
  if (i->flags & IF_BROADCAST)
83
    debug(" BC");
84
  if (i->flags & IF_MULTICAST)
85
    debug(" MC");
86
  if (i->flags & IF_LOOPBACK)
87
    debug(" LOOP");
88
  if (i->flags & IF_IGNORE)
89
    debug(" IGN");
90
  if (i->flags & IF_TMP_DOWN)
91
    debug(" TDOWN");
92
  debug(" MTU=%d\n", i->mtu);
93
  WALK_LIST(a, i->addrs)
94
    {
95
      ifa_dump(a);
96
      ASSERT((a != i->addr) == !(a->flags & IA_PRIMARY));
97
    }
98
}
99

    
100
/**
101
 * if_dump_all - dump all interfaces
102
 *
103
 * This function dumps information about all known network
104
 * interfaces to the debug output.
105
 */
106
void
107
if_dump_all(void)
108
{
109
  struct iface *i;
110

    
111
  debug("Known network interfaces:\n");
112
  WALK_LIST(i, iface_list)
113
    if_dump(i);
114
  debug("Router ID: %08x\n", config->router_id);
115
}
116

    
117
static inline unsigned
118
if_what_changed(struct iface *i, struct iface *j)
119
{
120
  unsigned c;
121

    
122
  if (((i->flags ^ j->flags) & ~(IF_UP | IF_SHUTDOWN | IF_UPDATED | IF_ADMIN_UP | IF_LINK_UP | IF_TMP_DOWN | IF_JUST_CREATED))
123
      || i->index != j->index)
124
    return IF_CHANGE_TOO_MUCH;
125
  c = 0;
126
  if ((i->flags ^ j->flags) & IF_UP)
127
    c |= (i->flags & IF_UP) ? IF_CHANGE_DOWN : IF_CHANGE_UP;
128
  if ((i->flags ^ j->flags) & IF_LINK_UP)
129
    c |= IF_CHANGE_LINK;
130
  if (i->mtu != j->mtu)
131
    c |= IF_CHANGE_MTU;
132
  return c;
133
}
134

    
135
static inline void
136
if_copy(struct iface *to, struct iface *from)
137
{
138
  to->flags = from->flags | (to->flags & IF_TMP_DOWN);
139
  to->mtu = from->mtu;
140
}
141

    
142
static inline void
143
ifa_send_notify(struct proto *p, unsigned c, struct ifa *a)
144
{
145
  if (p->ifa_notify && (p->proto_state != PS_DOWN))
146
    {
147
      if (p->debug & D_IFACES)
148
        log(L_TRACE "%s < %s address %N on interface %s %s",
149
            p->name, (a->flags & IA_PRIMARY) ? "primary" : "secondary",
150
            &a->prefix, a->iface->name, (c & IF_CHANGE_UP) ? "added" : "removed");
151
      p->ifa_notify(p, c, a);
152
    }
153
}
154

    
155
static void
156
ifa_notify_change_(unsigned c, struct ifa *a)
157
{
158
  struct proto *p;
159

    
160
  DBG("IFA change notification (%x) for %s:%I\n", c, a->iface->name, a->ip);
161

    
162
  WALK_LIST(p, proto_list)
163
    ifa_send_notify(p, c, a);
164
}
165

    
166
static inline void
167
ifa_notify_change(unsigned c, struct ifa *a)
168
{
169
  if (c & IF_CHANGE_DOWN)
170
    neigh_ifa_update(a);
171

    
172
  ifa_notify_change_(c, a);
173

    
174
  if (c & IF_CHANGE_UP)
175
    neigh_ifa_update(a);
176
}
177

    
178
static inline void
179
if_send_notify(struct proto *p, unsigned c, struct iface *i)
180
{
181
  if (p->if_notify && (p->proto_state != PS_DOWN))
182
    {
183
      if (p->debug & D_IFACES)
184
        log(L_TRACE "%s < interface %s %s", p->name, i->name,
185
            (c & IF_CHANGE_UP) ? "goes up" :
186
            (c & IF_CHANGE_DOWN) ? "goes down" :
187
            (c & IF_CHANGE_MTU) ? "changes MTU" :
188
            (c & IF_CHANGE_LINK) ? "changes link" :
189
            (c & IF_CHANGE_PREFERRED) ? "changes preferred address" :
190
            (c & IF_CHANGE_CREATE) ? "created" :
191
            "sends unknown event");
192
      p->if_notify(p, c, i);
193
    }
194
}
195

    
196
static void
197
if_notify_change(unsigned c, struct iface *i)
198
{
199
  struct proto *p;
200
  struct ifa *a;
201

    
202
  if (i->flags & IF_JUST_CREATED)
203
    {
204
      i->flags &= ~IF_JUST_CREATED;
205
      c |= IF_CHANGE_CREATE | IF_CHANGE_MTU;
206
    }
207

    
208
  DBG("Interface change notification (%x) for %s\n", c, i->name);
209
#ifdef LOCAL_DEBUG
210
  if_dump(i);
211
#endif
212

    
213
  if (c & IF_CHANGE_DOWN)
214
    neigh_if_down(i);
215

    
216
  if (c & IF_CHANGE_DOWN)
217
    WALK_LIST(a, i->addrs)
218
      ifa_notify_change_(IF_CHANGE_DOWN, a);
219

    
220
  WALK_LIST(p, proto_list)
221
    if_send_notify(p, c, i);
222

    
223
  if (c & IF_CHANGE_UP)
224
    WALK_LIST(a, i->addrs)
225
      ifa_notify_change_(IF_CHANGE_UP, a);
226

    
227
  if (c & IF_CHANGE_UP)
228
    neigh_if_up(i);
229

    
230
  if ((c & (IF_CHANGE_UP | IF_CHANGE_DOWN | IF_CHANGE_LINK)) == IF_CHANGE_LINK)
231
    neigh_if_link(i);
232
}
233

    
234
static uint
235
if_recalc_flags(struct iface *i UNUSED, uint flags)
236
{
237
  if ((flags & IF_ADMIN_UP) && !(flags & (IF_SHUTDOWN | IF_TMP_DOWN)))
238
    flags |= IF_UP;
239
  else
240
    flags &= ~IF_UP;
241

    
242
  return flags;
243
}
244

    
245
static void
246
if_change_flags(struct iface *i, uint flags)
247
{
248
  uint of = i->flags;
249
  i->flags = if_recalc_flags(i, flags);
250

    
251
  if ((i->flags ^ of) & IF_UP)
252
    if_notify_change((i->flags & IF_UP) ? IF_CHANGE_UP : IF_CHANGE_DOWN, i);
253
}
254

    
255
/**
256
 * if_delete - remove interface
257
 * @old: interface
258
 *
259
 * This function is called by the low-level platform dependent code
260
 * whenever it notices an interface disappears. It is just a shorthand
261
 * for if_update().
262
 */
263

    
264
void
265
if_delete(struct iface *old)
266
{
267
  struct iface f = {};
268
  strncpy(f.name, old->name, sizeof(f.name)-1);
269
  f.flags = IF_SHUTDOWN;
270
  if_update(&f);
271
}
272

    
273
/**
274
 * if_update - update interface status
275
 * @new: new interface status
276
 *
277
 * if_update() is called by the low-level platform dependent code
278
 * whenever it notices an interface change.
279
 *
280
 * There exist two types of interface updates -- synchronous and asynchronous
281
 * ones. In the synchronous case, the low-level code calls if_start_update(),
282
 * scans all interfaces reported by the OS, uses if_update() and ifa_update()
283
 * to pass them to the core and then it finishes the update sequence by
284
 * calling if_end_update(). When working asynchronously, the sysdep code
285
 * calls if_update() and ifa_update() whenever it notices a change.
286
 *
287
 * if_update() will automatically notify all other modules about the change.
288
 */
289
struct iface *
290
if_update(struct iface *new)
291
{
292
  struct iface *i;
293
  unsigned c;
294

    
295
  WALK_LIST(i, iface_list)
296
    if (!strcmp(new->name, i->name))
297
      {
298
        new->flags = if_recalc_flags(new, new->flags);
299
        c = if_what_changed(i, new);
300
        if (c & IF_CHANGE_TOO_MUCH)        /* Changed a lot, convert it to down/up */
301
          {
302
            DBG("Interface %s changed too much -- forcing down/up transition\n", i->name);
303
            if_change_flags(i, i->flags | IF_TMP_DOWN);
304
            rem_node(&i->n);
305
            new->addr4 = i->addr4;
306
            new->addr6 = i->addr6;
307
            new->llv6 = i->llv6;
308
            new->sysdep = i->sysdep;
309
            memcpy(&new->addrs, &i->addrs, sizeof(i->addrs));
310
            memcpy(i, new, sizeof(*i));
311
            i->flags &= ~IF_UP;         /* IF_TMP_DOWN will be added later */
312
            goto newif;
313
          }
314

    
315
        if_copy(i, new);
316
        if (c)
317
          if_notify_change(c, i);
318

    
319
        i->flags |= IF_UPDATED;
320
        return i;
321
      }
322
  i = mb_alloc(if_pool, sizeof(struct iface));
323
  memcpy(i, new, sizeof(*i));
324
  init_list(&i->addrs);
325
newif:
326
  init_list(&i->neighbors);
327
  i->flags |= IF_UPDATED | IF_TMP_DOWN;                /* Tmp down as we don't have addresses yet */
328
  add_tail(&iface_list, &i->n);
329
  return i;
330
}
331

    
332
void
333
if_start_update(void)
334
{
335
  struct iface *i;
336
  struct ifa *a;
337

    
338
  WALK_LIST(i, iface_list)
339
    {
340
      i->flags &= ~IF_UPDATED;
341
      WALK_LIST(a, i->addrs)
342
        a->flags &= ~IA_UPDATED;
343
    }
344
}
345

    
346
void
347
if_end_partial_update(struct iface *i)
348
{
349
  if (i->flags & IF_NEEDS_RECALC)
350
    if_recalc_preferred(i);
351

    
352
  if (i->flags & IF_TMP_DOWN)
353
    if_change_flags(i, i->flags & ~IF_TMP_DOWN);
354
}
355

    
356
void
357
if_end_update(void)
358
{
359
  struct iface *i;
360
  struct ifa *a, *b;
361

    
362
  WALK_LIST(i, iface_list)
363
    {
364
      if (!(i->flags & IF_UPDATED))
365
        if_change_flags(i, (i->flags & ~IF_ADMIN_UP) | IF_SHUTDOWN);
366
      else
367
        {
368
          WALK_LIST_DELSAFE(a, b, i->addrs)
369
            if (!(a->flags & IA_UPDATED))
370
              ifa_delete(a);
371
          if_end_partial_update(i);
372
        }
373
    }
374
}
375

    
376
void
377
if_flush_ifaces(struct proto *p)
378
{
379
  if (p->debug & D_EVENTS)
380
    log(L_TRACE "%s: Flushing interfaces", p->name);
381
  if_start_update();
382
  if_end_update();
383
}
384

    
385
/**
386
 * if_feed_baby - advertise interfaces to a new protocol
387
 * @p: protocol to feed
388
 *
389
 * When a new protocol starts, this function sends it a series
390
 * of notifications about all existing interfaces.
391
 */
392
void
393
if_feed_baby(struct proto *p)
394
{
395
  struct iface *i;
396
  struct ifa *a;
397

    
398
  if (!p->if_notify && !p->ifa_notify)        /* shortcut */
399
    return;
400
  DBG("Announcing interfaces to new protocol %s\n", p->name);
401
  WALK_LIST(i, iface_list)
402
    {
403
      if_send_notify(p, IF_CHANGE_CREATE | ((i->flags & IF_UP) ? IF_CHANGE_UP : 0), i);
404
      if (i->flags & IF_UP)
405
        WALK_LIST(a, i->addrs)
406
          ifa_send_notify(p, IF_CHANGE_CREATE | IF_CHANGE_UP, a);
407
    }
408
}
409

    
410
/**
411
 * if_find_by_index - find interface by ifindex
412
 * @idx: ifindex
413
 *
414
 * This function finds an &iface structure corresponding to an interface
415
 * of the given index @idx. Returns a pointer to the structure or %NULL
416
 * if no such structure exists.
417
 */
418
struct iface *
419
if_find_by_index(unsigned idx)
420
{
421
  struct iface *i;
422

    
423
  WALK_LIST(i, iface_list)
424
    if (i->index == idx && !(i->flags & IF_SHUTDOWN))
425
      return i;
426
  return NULL;
427
}
428

    
429
/**
430
 * if_find_by_name - find interface by name
431
 * @name: interface name
432
 *
433
 * This function finds an &iface structure corresponding to an interface
434
 * of the given name @name. Returns a pointer to the structure or %NULL
435
 * if no such structure exists.
436
 */
437
struct iface *
438
if_find_by_name(char *name)
439
{
440
  struct iface *i;
441

    
442
  WALK_LIST(i, iface_list)
443
    if (!strcmp(i->name, name))
444
      return i;
445
  return NULL;
446
}
447

    
448
struct iface *
449
if_get_by_name(char *name)
450
{
451
  struct iface *i;
452

    
453
  if (i = if_find_by_name(name))
454
    return i;
455

    
456
  /* No active iface, create a dummy */
457
  i = mb_allocz(if_pool, sizeof(struct iface));
458
  strncpy(i->name, name, sizeof(i->name)-1);
459
  i->flags = IF_SHUTDOWN;
460
  init_list(&i->addrs);
461
  init_list(&i->neighbors);
462
  add_tail(&iface_list, &i->n);
463
  return i;
464
}
465

    
466
static inline void
467
if_set_preferred(struct ifa **pos, struct ifa *new)
468
{
469
  if (*pos)
470
    (*pos)->flags &= ~IA_PRIMARY;
471
  if (new)
472
    new->flags |= IA_PRIMARY;
473

    
474
  *pos = new;
475
}
476

    
477
static void
478
if_recalc_preferred(struct iface *i)
479
{
480
  /*
481
   * Preferred address selection priority:
482
   * 1) Address configured in Device protocol
483
   * 2) Sysdep IPv4 address (BSD)
484
   * 3) Old preferred address
485
   * 4) First address in list
486
   */
487

    
488
  struct kif_iface_config *ic = kif_get_iface_config(i);
489
  struct ifa *a4 = i->addr4, *a6 = i->addr6, *ll = i->llv6;
490
  ip_addr pref_v4 = ic->pref_v4;
491
  uint change = 0;
492

    
493
  if (kif_update_sysdep_addr(i))
494
    change |= IF_CHANGE_SYSDEP;
495

    
496
  /* BSD sysdep address */
497
  if (ipa_zero(pref_v4) && ip4_nonzero(i->sysdep))
498
    pref_v4 = ipa_from_ip4(i->sysdep);
499

    
500
  struct ifa *a;
501
  WALK_LIST(a, i->addrs)
502
    {
503
      /* Secondary address is never selected */
504
      if (a->flags & IA_SECONDARY)
505
        continue;
506

    
507
      if (ipa_is_ip4(a->ip)) {
508
        if (!a4 || ipa_equal(a->ip, pref_v4))
509
          a4 = a;
510
      } else if (!ipa_is_link_local(a->ip)) {
511
        if (!a6 || ipa_equal(a->ip, ic->pref_v6))
512
          a6 = a;
513
      } else {
514
        if (!ll || ipa_equal(a->ip, ic->pref_ll))
515
          ll = a;
516
      }
517
    }
518

    
519
  if (a4 != i->addr4)
520
  {
521
    if_set_preferred(&i->addr4, a4);
522
    change |= IF_CHANGE_ADDR4;
523
  }
524

    
525
  if (a6 != i->addr6)
526
  {
527
    if_set_preferred(&i->addr6, a6);
528
    change |= IF_CHANGE_ADDR6;
529
  }
530

    
531
  if (ll != i->llv6)
532
  {
533
    if_set_preferred(&i->llv6, ll);
534
    change |= IF_CHANGE_LLV6;
535
  }
536

    
537
  i->flags &= ~IF_NEEDS_RECALC;
538

    
539
  /*
540
   * FIXME: There should be proper notification instead of iface restart:
541
   * if_notify_change(change, i)
542
   */
543
  if (change)
544
    if_change_flags(i, i->flags | IF_TMP_DOWN);
545
}
546

    
547
void
548
if_recalc_all_preferred_addresses(void)
549
{
550
  struct iface *i;
551

    
552
  WALK_LIST(i, iface_list)
553
  {
554
    if_recalc_preferred(i);
555

    
556
    if (i->flags & IF_TMP_DOWN)
557
      if_change_flags(i, i->flags & ~IF_TMP_DOWN);
558
  }
559
}
560

    
561
static inline int
562
ifa_same(struct ifa *a, struct ifa *b)
563
{
564
  return ipa_equal(a->ip, b->ip) && net_equal(&a->prefix, &b->prefix);
565
}
566

    
567

    
568
/**
569
 * ifa_update - update interface address
570
 * @a: new interface address
571
 *
572
 * This function adds address information to a network
573
 * interface. It's called by the platform dependent code during
574
 * the interface update process described under if_update().
575
 */
576
struct ifa *
577
ifa_update(struct ifa *a)
578
{
579
  struct iface *i = a->iface;
580
  struct ifa *b;
581

    
582
  WALK_LIST(b, i->addrs)
583
    if (ifa_same(b, a))
584
      {
585
        if (ipa_equal(b->brd, a->brd) &&
586
            ipa_equal(b->opposite, a->opposite) &&
587
            b->scope == a->scope &&
588
            !((b->flags ^ a->flags) & IA_PEER))
589
          {
590
            b->flags |= IA_UPDATED;
591
            return b;
592
          }
593
        ifa_delete(b);
594
        break;
595
      }
596

    
597
  if ((a->prefix.type == NET_IP4) && (i->flags & IF_BROADCAST) && ipa_zero(a->brd))
598
    log(L_WARN "Missing broadcast address for interface %s", i->name);
599

    
600
  b = mb_alloc(if_pool, sizeof(struct ifa));
601
  memcpy(b, a, sizeof(struct ifa));
602
  add_tail(&i->addrs, &b->n);
603
  b->flags |= IA_UPDATED;
604

    
605
  i->flags |= IF_NEEDS_RECALC;
606
  if (i->flags & IF_UP)
607
    ifa_notify_change(IF_CHANGE_CREATE | IF_CHANGE_UP, b);
608
  return b;
609
}
610

    
611
/**
612
 * ifa_delete - remove interface address
613
 * @a: interface address
614
 *
615
 * This function removes address information from a network
616
 * interface. It's called by the platform dependent code during
617
 * the interface update process described under if_update().
618
 */
619
void
620
ifa_delete(struct ifa *a)
621
{
622
  struct iface *i = a->iface;
623
  struct ifa *b;
624

    
625
  WALK_LIST(b, i->addrs)
626
    if (ifa_same(b, a))
627
      {
628
        rem_node(&b->n);
629

    
630
        if (b->flags & IA_PRIMARY)
631
          {
632
            /*
633
             * We unlink deleted preferred address and mark for recalculation.
634
             * FIXME: This could break if we make iface scan non-atomic, as
635
             * protocols still could use the freed address until they get
636
             * if_notify from preferred route recalculation.
637
             */
638
            if (b == i->addr4) i->addr4 = NULL;
639
            if (b == i->addr6) i->addr6 = NULL;
640
            if (b == i->llv6) i->llv6 = NULL;
641
            i->flags |= IF_NEEDS_RECALC;
642
          }
643

    
644
        if (i->flags & IF_UP)
645
          ifa_notify_change(IF_CHANGE_DOWN, b);
646

    
647
        mb_free(b);
648
        return;
649
      }
650
}
651

    
652
u32
653
if_choose_router_id(struct iface_patt *mask, u32 old_id)
654
{
655
  struct iface *i;
656
  struct ifa *a, *b;
657

    
658
  b = NULL;
659
  WALK_LIST(i, iface_list)
660
    {
661
      if (!(i->flags & IF_ADMIN_UP) ||
662
          (i->flags & IF_SHUTDOWN))
663
        continue;
664

    
665
      WALK_LIST(a, i->addrs)
666
        {
667
          if (a->prefix.type != NET_IP4)
668
            continue;
669

    
670
          if (a->flags & IA_SECONDARY)
671
            continue;
672

    
673
          if (a->scope <= SCOPE_LINK)
674
            continue;
675

    
676
          /* Check pattern if specified */
677
          if (mask && !iface_patt_match(mask, i, a))
678
            continue;
679

    
680
          /* No pattern or pattern matched */
681
          if (!b || ipa_to_u32(a->ip) < ipa_to_u32(b->ip))
682
            b = a;
683
        }
684
    }
685

    
686
  if (!b)
687
    return 0;
688

    
689
  u32 id = ipa_to_u32(b->ip);
690
  if (id != old_id)
691
    log(L_INFO "Chosen router ID %R according to interface %s", id, b->iface->name);
692

    
693
  return id;
694
}
695

    
696
/**
697
 * if_init - initialize interface module
698
 *
699
 * This function is called during BIRD startup to initialize
700
 * all data structures of the interface module.
701
 */
702
void
703
if_init(void)
704
{
705
  if_pool = rp_new(&root_pool, "Interfaces");
706
  init_list(&iface_list);
707
  neigh_init(if_pool);
708
}
709

    
710
/*
711
 *        Interface Pattern Lists
712
 */
713

    
714
int
715
iface_patt_match(struct iface_patt *ifp, struct iface *i, struct ifa *a)
716
{
717
  struct iface_patt_node *p;
718

    
719
  WALK_LIST(p, ifp->ipn_list)
720
    {
721
      char *t = p->pattern;
722
      int pos = p->positive;
723

    
724
      if (t)
725
        {
726
          if (*t == '-')
727
            {
728
              t++;
729
              pos = !pos;
730
            }
731

    
732
          if (!patmatch(t, i->name))
733
            continue;
734
        }
735

    
736
      if (p->prefix.pxlen == 0)
737
        return pos;
738

    
739
      if (!a)
740
        continue;
741

    
742
      if (ipa_in_netX(a->ip, &p->prefix))
743
        return pos;
744

    
745
      if ((a->flags & IA_PEER) &&
746
          ipa_in_netX(a->opposite, &p->prefix))
747
        return pos;
748

    
749
      continue;
750
    }
751

    
752
  return 0;
753
}
754

    
755
struct iface_patt *
756
iface_patt_find(list *l, struct iface *i, struct ifa *a)
757
{
758
  struct iface_patt *p;
759

    
760
  WALK_LIST(p, *l)
761
    if (iface_patt_match(p, i, a))
762
      return p;
763

    
764
  return NULL;
765
}
766

    
767
static int
768
iface_plists_equal(struct iface_patt *pa, struct iface_patt *pb)
769
{
770
  struct iface_patt_node *x, *y;
771

    
772
  x = HEAD(pa->ipn_list);
773
  y = HEAD(pb->ipn_list);
774
  while (x->n.next && y->n.next)
775
    {
776
      if ((x->positive != y->positive) ||
777
          (!x->pattern && y->pattern) ||        /* This nasty lines where written by me... :-( Feela */
778
          (!y->pattern && x->pattern) ||
779
          ((x->pattern != y->pattern) && strcmp(x->pattern, y->pattern)) ||
780
          !net_equal(&x->prefix, &y->prefix))
781
        return 0;
782
      x = (void *) x->n.next;
783
      y = (void *) y->n.next;
784
    }
785
  return (!x->n.next && !y->n.next);
786
}
787

    
788
int
789
iface_patts_equal(list *a, list *b, int (*comp)(struct iface_patt *, struct iface_patt *))
790
{
791
  struct iface_patt *x, *y;
792

    
793
  x = HEAD(*a);
794
  y = HEAD(*b);
795
  while (x->n.next && y->n.next)
796
    {
797
      if (!iface_plists_equal(x, y) ||
798
          (comp && !comp(x, y)))
799
        return 0;
800
      x = (void *) x->n.next;
801
      y = (void *) y->n.next;
802
    }
803
  return (!x->n.next && !y->n.next);
804
}
805

    
806
/*
807
 *  CLI commands.
808
 */
809

    
810
static void
811
if_show_addr(struct ifa *a)
812
{
813
  byte *flg, opp[IPA_MAX_TEXT_LENGTH + 16];
814

    
815
  flg = (a->flags & IA_PRIMARY) ? "Preferred, " : (a->flags & IA_SECONDARY) ? "Secondary, " : "";
816

    
817
  if (ipa_nonzero(a->opposite))
818
    bsprintf(opp, "opposite %I, ", a->opposite);
819
  else
820
    opp[0] = 0;
821

    
822
  cli_msg(-1003, "\t%I/%d (%s%sscope %s)",
823
          a->ip, a->prefix.pxlen, flg, opp, ip_scope_text(a->scope));
824
}
825

    
826
void
827
if_show(void)
828
{
829
  struct iface *i;
830
  struct ifa *a;
831
  char *type;
832

    
833
  WALK_LIST(i, iface_list)
834
    {
835
      if (i->flags & IF_SHUTDOWN)
836
        continue;
837

    
838
      cli_msg(-1001, "%s %s (index=%d)", i->name, (i->flags & IF_UP) ? "Up" : "Down", i->index);
839
      if (!(i->flags & IF_MULTIACCESS))
840
        type = "PtP";
841
      else
842
        type = "MultiAccess";
843
      cli_msg(-1004, "\t%s%s%s Admin%s Link%s%s%s MTU=%d",
844
              type,
845
              (i->flags & IF_BROADCAST) ? " Broadcast" : "",
846
              (i->flags & IF_MULTICAST) ? " Multicast" : "",
847
              (i->flags & IF_ADMIN_UP) ? "Up" : "Down",
848
              (i->flags & IF_LINK_UP) ? "Up" : "Down",
849
              (i->flags & IF_LOOPBACK) ? " Loopback" : "",
850
              (i->flags & IF_IGNORE) ? " Ignored" : "",
851
              i->mtu);
852

    
853
      WALK_LIST(a, i->addrs)
854
        if (a->prefix.type == NET_IP4)
855
          if_show_addr(a);
856

    
857
      WALK_LIST(a, i->addrs)
858
        if (a->prefix.type == NET_IP6)
859
          if_show_addr(a);
860
    }
861
  cli_msg(0, "");
862
}
863

    
864
void
865
if_show_summary(void)
866
{
867
  struct iface *i;
868

    
869
  cli_msg(-2005,  "%-10s %-6s %-18s %s", "Interface", "State", "IPv4 address", "IPv6 address");
870
  WALK_LIST(i, iface_list)
871
    {
872
      byte a4[IPA_MAX_TEXT_LENGTH + 17];
873
      byte a6[IPA_MAX_TEXT_LENGTH + 17];
874

    
875
      if (i->addr4)
876
        bsprintf(a4, "%I/%d", i->addr4->ip, i->addr4->prefix.pxlen);
877
      else
878
        a4[0] = 0;
879

    
880
      if (i->addr6)
881
        bsprintf(a6, "%I/%d", i->addr6->ip, i->addr6->prefix.pxlen);
882
      else
883
        a6[0] = 0;
884

    
885
      cli_msg(-1005, "%-10s %-6s %-18s %s",
886
              i->name, (i->flags & IF_UP) ? "Up" : "Down", a4, a6);
887
    }
888
  cli_msg(0, "");
889
}