Statistics
| Branch: | Revision:

iof-bird-daemon / nest / iface.c @ 7e95c05d

History | View | Annotate | Download (16.7 KB)

1 8a48ecb8 Martin Mares
/*
2 4cc78c50 Martin Mares
 *        BIRD -- Management of Interfaces and Neighbor Cache
3 8a48ecb8 Martin Mares
 *
4 85053fce Martin Mares
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 8a48ecb8 Martin Mares
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 3d675cdb Martin Mares
/**
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 6a9f28b0 Martin Mares
#undef LOCAL_DEBUG
27 4cc78c50 Martin Mares
28 8a48ecb8 Martin Mares
#include "nest/bird.h"
29
#include "nest/iface.h"
30 4cc78c50 Martin Mares
#include "nest/protocol.h"
31 ae97b946 Martin Mares
#include "nest/cli.h"
32 8a48ecb8 Martin Mares
#include "lib/resource.h"
33 ed45f2e1 Martin Mares
#include "lib/string.h"
34 4e9498cb Martin Mares
#include "conf/conf.h"
35 8a48ecb8 Martin Mares
36
static pool *if_pool;
37
38 4e9498cb Martin Mares
static void auto_router_id(void);
39
40 4cc78c50 Martin Mares
list iface_list;
41
42 3d675cdb Martin Mares
/**
43
 * ifa_dump - dump interface address
44
 * @a: interface address descriptor
45
 *
46
 * This function dumps contents of an &ifa to the debug output.
47
 */
48 4cc78c50 Martin Mares
void
49 9a158361 Martin Mares
ifa_dump(struct ifa *a)
50
{
51 6a636392 Martin Mares
  debug("\t%I, net %I/%-2d bc %I -> %I%s%s%s\n", a->ip, a->prefix, a->pxlen, a->brd, a->opposite,
52 9a158361 Martin Mares
        (a->flags & IF_UP) ? "" : " DOWN",
53 6a636392 Martin Mares
        (a->flags & IA_PRIMARY) ? "" : " SEC",
54
        (a->flags & IA_UNNUMBERED) ? " UNNUM" : "");
55 9a158361 Martin Mares
}
56
57 3d675cdb Martin Mares
/**
58
 * if_dump - dump interface
59
 * @i: interface to dump
60
 *
61
 * This function dumps all information associated with a given
62
 * network interface to the debug output.
63
 */
64 9a158361 Martin Mares
void
65 4cc78c50 Martin Mares
if_dump(struct iface *i)
66
{
67 9a158361 Martin Mares
  struct ifa *a;
68
69 8a48ecb8 Martin Mares
  debug("IF%d: %s", i->index, i->name);
70 f25cb0ef Ondrej Zajicek
  if (i->flags & IF_SHUTDOWN)
71
    debug(" SHUTDOWN");
72 8a48ecb8 Martin Mares
  if (i->flags & IF_UP)
73
    debug(" UP");
74 bcbd8cc3 Martin Mares
  else
75
    debug(" DOWN");
76 f25cb0ef Ondrej Zajicek
  if (i->flags & IF_ADMIN_UP)
77 bcbd8cc3 Martin Mares
    debug(" LINK-UP");
78 8a48ecb8 Martin Mares
  if (i->flags & IF_MULTIACCESS)
79
    debug(" MA");
80
  if (i->flags & IF_BROADCAST)
81
    debug(" BC");
82
  if (i->flags & IF_MULTICAST)
83
    debug(" MC");
84
  if (i->flags & IF_LOOPBACK)
85
    debug(" LOOP");
86
  if (i->flags & IF_IGNORE)
87
    debug(" IGN");
88 9a158361 Martin Mares
  if (i->flags & IF_TMP_DOWN)
89
    debug(" TDOWN");
90 8a48ecb8 Martin Mares
  debug(" MTU=%d\n", i->mtu);
91 9a158361 Martin Mares
  WALK_LIST(a, i->addrs)
92
    {
93
      ifa_dump(a);
94
      ASSERT((a != i->addr) == !(a->flags & IA_PRIMARY));
95
    }
96 8a48ecb8 Martin Mares
}
97
98 3d675cdb Martin Mares
/**
99
 * if_dump_all - dump all interfaces
100
 *
101
 * This function dumps information about all known network
102
 * interfaces to the debug output.
103
 */
104 8a48ecb8 Martin Mares
void
105
if_dump_all(void)
106
{
107
  struct iface *i;
108
109 620a355a Martin Mares
  debug("Known network interfaces:\n");
110 8a48ecb8 Martin Mares
  WALK_LIST(i, iface_list)
111
    if_dump(i);
112 4e9498cb Martin Mares
  debug("Router ID: %08x\n", config->router_id);
113 8a48ecb8 Martin Mares
}
114
115 9a158361 Martin Mares
static inline unsigned
116
if_what_changed(struct iface *i, struct iface *j)
117 8a48ecb8 Martin Mares
{
118 9a158361 Martin Mares
  unsigned c;
119
120 fe181e7c Ondrej Zajicek
  if (((i->flags ^ j->flags) & ~(IF_UP | IF_SHUTDOWN | IF_UPDATED | IF_ADMIN_UP | IF_LINK_UP | IF_TMP_DOWN | IF_JUST_CREATED))
121 9a158361 Martin Mares
      || i->index != j->index)
122
    return IF_CHANGE_TOO_MUCH;
123
  c = 0;
124
  if ((i->flags ^ j->flags) & IF_UP)
125
    c |= (i->flags & IF_UP) ? IF_CHANGE_DOWN : IF_CHANGE_UP;
126 f25cb0ef Ondrej Zajicek
  if ((i->flags ^ j->flags) & IF_LINK_UP)
127
    c |= IF_CHANGE_LINK;
128 9a158361 Martin Mares
  if (i->mtu != j->mtu)
129
    c |= IF_CHANGE_MTU;
130
  return c;
131 8a48ecb8 Martin Mares
}
132
133
static inline void
134 4cc78c50 Martin Mares
if_copy(struct iface *to, struct iface *from)
135 8a48ecb8 Martin Mares
{
136 9a158361 Martin Mares
  to->flags = from->flags | (to->flags & IF_TMP_DOWN);
137 4cc78c50 Martin Mares
  to->mtu = from->mtu;
138 8a48ecb8 Martin Mares
}
139
140 6a9f28b0 Martin Mares
static inline void
141
ifa_send_notify(struct proto *p, unsigned c, struct ifa *a)
142
{
143
  if (p->ifa_notify)
144
    {
145
      if (p->debug & D_IFACES)
146
        log(L_TRACE "%s < %s address %I/%d on interface %s %s",
147
            p->name, (a->flags & IA_PRIMARY) ? "primary" : "secondary",
148
            a->prefix, a->pxlen, a->iface->name,
149
            (c & IF_CHANGE_UP) ? "added" : "removed");
150
      p->ifa_notify(p, c, a);
151
    }
152
}
153
154 9a158361 Martin Mares
static void
155
ifa_notify_change(unsigned c, struct ifa *a)
156 8a48ecb8 Martin Mares
{
157 9a158361 Martin Mares
  struct proto *p;
158 8a48ecb8 Martin Mares
159 6a9f28b0 Martin Mares
  DBG("IFA change notification (%x) for %s:%I\n", c, a->iface->name, a->ip);
160 f14a4bec Martin Mares
  WALK_LIST(p, active_proto_list)
161 6a9f28b0 Martin Mares
    ifa_send_notify(p, c, a);
162
}
163
164
static inline void
165
if_send_notify(struct proto *p, unsigned c, struct iface *i)
166
{
167
  if (p->if_notify)
168
    {
169
      if (p->debug & D_IFACES)
170
        log(L_TRACE "%s < interface %s %s", p->name, i->name,
171
            (c & IF_CHANGE_UP) ? "goes up" :
172
            (c & IF_CHANGE_DOWN) ? "goes down" :
173
            (c & IF_CHANGE_MTU) ? "changes MTU" :
174 f25cb0ef Ondrej Zajicek
            (c & IF_CHANGE_LINK) ? "changes link" :
175 6a9f28b0 Martin Mares
            (c & IF_CHANGE_CREATE) ? "created" :
176
            "sends unknown event");
177
      p->if_notify(p, c, i);
178
    }
179 8a48ecb8 Martin Mares
}
180
181
static void
182 9a158361 Martin Mares
if_notify_change(unsigned c, struct iface *i)
183 8a48ecb8 Martin Mares
{
184 d9f330c5 Martin Mares
  struct proto *p;
185 9a158361 Martin Mares
  struct ifa *a;
186 d9f330c5 Martin Mares
187 9a158361 Martin Mares
  if (i->flags & IF_JUST_CREATED)
188
    {
189
      i->flags &= ~IF_JUST_CREATED;
190
      c |= IF_CHANGE_CREATE | IF_CHANGE_MTU;
191
    }
192
193 6a9f28b0 Martin Mares
  DBG("Interface change notification (%x) for %s\n", c, i->name);
194 9a220cab Martin Mares
#ifdef LOCAL_DEBUG
195 9a158361 Martin Mares
  if_dump(i);
196 9a220cab Martin Mares
#endif
197 4cc78c50 Martin Mares
198
  if (c & IF_CHANGE_UP)
199 9a158361 Martin Mares
    neigh_if_up(i);
200
  if (c & IF_CHANGE_DOWN)
201
    WALK_LIST(a, i->addrs)
202
      {
203
        a->flags = (i->flags & ~IA_FLAGS) | (a->flags & IA_FLAGS);
204
        ifa_notify_change(IF_CHANGE_DOWN, a);
205
      }
206 4cc78c50 Martin Mares
207 f14a4bec Martin Mares
  WALK_LIST(p, active_proto_list)
208 6a9f28b0 Martin Mares
    if_send_notify(p, c, i);
209 4cc78c50 Martin Mares
210 9a158361 Martin Mares
  if (c & IF_CHANGE_UP)
211
    WALK_LIST(a, i->addrs)
212
      {
213
        a->flags = (i->flags & ~IA_FLAGS) | (a->flags & IA_FLAGS);
214
        ifa_notify_change(IF_CHANGE_UP, a);
215
      }
216 fe181e7c Ondrej Zajicek
217
  if ((c & (IF_CHANGE_UP | IF_CHANGE_DOWN | IF_CHANGE_LINK)) == IF_CHANGE_LINK)
218
    neigh_if_link(i);
219
220 4cc78c50 Martin Mares
  if (c & IF_CHANGE_DOWN)
221 9a158361 Martin Mares
    neigh_if_down(i);
222 8a48ecb8 Martin Mares
}
223
224 9a158361 Martin Mares
static unsigned
225
if_recalc_flags(struct iface *i, unsigned flags)
226
{
227 f25cb0ef Ondrej Zajicek
  if ((flags & (IF_SHUTDOWN | IF_TMP_DOWN)) ||
228
      !(flags & IF_ADMIN_UP) ||
229 9a158361 Martin Mares
      !i->addr)
230
    flags &= ~IF_UP;
231
  else
232
    flags |= IF_UP;
233
  return flags;
234
}
235
236
static void
237
if_change_flags(struct iface *i, unsigned flags)
238
{
239
  unsigned of = i->flags;
240
241
  i->flags = if_recalc_flags(i, flags);
242
  if ((i->flags ^ of) & IF_UP)
243
    if_notify_change((i->flags & IF_UP) ? IF_CHANGE_UP : IF_CHANGE_DOWN, i);
244
}
245
246 3d675cdb Martin Mares
/**
247
 * if_update - update interface status
248
 * @new: new interface status
249
 *
250
 * if_update() is called by the low-level platform dependent code
251
 * whenever it notices an interface change.
252
 *
253 5c18880e Martin Mares
 * There exist two types of interface updates -- synchronous and asynchronous
254 3d675cdb Martin Mares
 * ones. In the synchronous case, the low-level code calls if_start_update(),
255
 * scans all interfaces reported by the OS, uses if_update() and ifa_update()
256
 * to pass them to the core and then it finishes the update sequence by
257
 * calling if_end_update(). When working asynchronously, the sysdep code
258
 * calls if_update() and ifa_update() whenever it notices a change.
259
 *
260
 * if_update() will automatically notify all other modules about the change.
261
 */
262 9a158361 Martin Mares
struct iface *
263 8a48ecb8 Martin Mares
if_update(struct iface *new)
264
{
265
  struct iface *i;
266 4cc78c50 Martin Mares
  unsigned c;
267 8a48ecb8 Martin Mares
268
  WALK_LIST(i, iface_list)
269
    if (!strcmp(new->name, i->name))
270
      {
271 9a158361 Martin Mares
        new->addr = i->addr;
272
        new->flags = if_recalc_flags(new, new->flags);
273
        c = if_what_changed(i, new);
274
        if (c & IF_CHANGE_TOO_MUCH)        /* Changed a lot, convert it to down/up */
275 8a48ecb8 Martin Mares
          {
276 4cc78c50 Martin Mares
            DBG("Interface %s changed too much -- forcing down/up transition\n", i->name);
277 9a158361 Martin Mares
            if_change_flags(i, i->flags | IF_TMP_DOWN);
278 8a48ecb8 Martin Mares
            rem_node(&i->n);
279 a2697f02 Martin Mares
            new->addr = i->addr;
280
            memcpy(&new->addrs, &i->addrs, sizeof(i->addrs));
281
            memcpy(i, new, sizeof(*i));
282 4cc78c50 Martin Mares
            goto newif;
283 8a48ecb8 Martin Mares
          }
284 9a158361 Martin Mares
        else if (c)
285
          {
286
            if_copy(i, new);
287
            if_notify_change(c, i);
288
          }
289 e35ef181 Martin Mares
        i->flags |= IF_UPDATED;
290 9a158361 Martin Mares
        return i;
291 8a48ecb8 Martin Mares
      }
292 4cc78c50 Martin Mares
  i = mb_alloc(if_pool, sizeof(struct iface));
293
  memcpy(i, new, sizeof(*i));
294 9a158361 Martin Mares
  init_list(&i->addrs);
295 a2697f02 Martin Mares
newif:
296 287111fe Martin Mares
  init_list(&i->neighbors);
297 9a158361 Martin Mares
  i->flags |= IF_UPDATED | IF_TMP_DOWN;                /* Tmp down as we don't have addresses yet */
298 8a48ecb8 Martin Mares
  add_tail(&iface_list, &i->n);
299 9a158361 Martin Mares
  return i;
300 8a48ecb8 Martin Mares
}
301
302
void
303 e35ef181 Martin Mares
if_start_update(void)
304
{
305
  struct iface *i;
306 9a158361 Martin Mares
  struct ifa *a;
307 e35ef181 Martin Mares
308
  WALK_LIST(i, iface_list)
309 9a158361 Martin Mares
    {
310
      i->flags &= ~IF_UPDATED;
311
      WALK_LIST(a, i->addrs)
312
        a->flags &= ~IF_UPDATED;
313
    }
314
}
315
316
void
317
if_end_partial_update(struct iface *i)
318
{
319
  if (i->flags & IF_TMP_DOWN)
320
    if_change_flags(i, i->flags & ~IF_TMP_DOWN);
321 e35ef181 Martin Mares
}
322
323
void
324 8a48ecb8 Martin Mares
if_end_update(void)
325
{
326 93a786cb Martin Mares
  struct iface *i;
327 9a158361 Martin Mares
  struct ifa *a, *b;
328 8a48ecb8 Martin Mares
329 4e9498cb Martin Mares
  if (!config->router_id)
330
    auto_router_id();
331
332 8a48ecb8 Martin Mares
  WALK_LIST(i, iface_list)
333 9a158361 Martin Mares
    {
334
      if (!(i->flags & IF_UPDATED))
335 f25cb0ef Ondrej Zajicek
        if_change_flags(i, (i->flags & ~IF_ADMIN_UP) | IF_SHUTDOWN);
336 9a158361 Martin Mares
      else
337
        {
338
          WALK_LIST_DELSAFE(a, b, i->addrs)
339
            if (!(a->flags & IF_UPDATED))
340
              ifa_delete(a);
341
          if_end_partial_update(i);
342
        }
343
    }
344 8a48ecb8 Martin Mares
}
345
346 53434e44 Ondrej Zajicek
void
347
if_flush_ifaces(struct proto *p)
348
{
349
  if (p->debug & D_EVENTS)
350
    log(L_TRACE "%s: Flushing interfaces", p->name);
351
  if_start_update();
352
  if_end_update();
353
}
354
355 3d675cdb Martin Mares
/**
356
 * if_feed_baby - advertise interfaces to a new protocol
357
 * @p: protocol to feed
358
 *
359
 * When a new protocol starts, this function sends it a series
360
 * of notifications about all existing interfaces.
361
 */
362 8a48ecb8 Martin Mares
void
363 47b79306 Martin Mares
if_feed_baby(struct proto *p)
364
{
365
  struct iface *i;
366 9a158361 Martin Mares
  struct ifa *a;
367 47b79306 Martin Mares
368 6a9f28b0 Martin Mares
  if (!p->if_notify && !p->ifa_notify)        /* shortcut */
369 47b79306 Martin Mares
    return;
370 6a9f28b0 Martin Mares
  DBG("Announcing interfaces to new protocol %s\n", p->name);
371 47b79306 Martin Mares
  WALK_LIST(i, iface_list)
372 9a158361 Martin Mares
    {
373 6a9f28b0 Martin Mares
      if_send_notify(p, IF_CHANGE_CREATE | ((i->flags & IF_UP) ? IF_CHANGE_UP : 0), i);
374
      if (i->flags & IF_UP)
375 9a158361 Martin Mares
        WALK_LIST(a, i->addrs)
376 6a9f28b0 Martin Mares
          ifa_send_notify(p, IF_CHANGE_CREATE | IF_CHANGE_UP, a);
377 9a158361 Martin Mares
    }
378 47b79306 Martin Mares
}
379
380 3d675cdb Martin Mares
/**
381
 * if_find_by_index - find interface by ifindex
382
 * @idx: ifindex
383
 *
384
 * This function finds an &iface structure corresponding to an interface
385
 * of the given index @idx. Returns a pointer to the structure or %NULL
386
 * if no such structure exists.
387
 */
388 e35ef181 Martin Mares
struct iface *
389
if_find_by_index(unsigned idx)
390
{
391
  struct iface *i;
392
393
  WALK_LIST(i, iface_list)
394
    if (i->index == idx)
395
      return i;
396
  return NULL;
397
}
398
399 3d675cdb Martin Mares
/**
400
 * if_find_by_name - find interface by name
401
 * @name: interface name
402
 *
403
 * This function finds an &iface structure corresponding to an interface
404
 * of the given name @name. Returns a pointer to the structure or %NULL
405
 * if no such structure exists.
406
 */
407 9a158361 Martin Mares
struct iface *
408
if_find_by_name(char *name)
409
{
410
  struct iface *i;
411
412
  WALK_LIST(i, iface_list)
413
    if (!strcmp(i->name, name))
414
      return i;
415
  return NULL;
416
}
417
418 874b8685 Ondrej Zajicek
struct ifa *kif_choose_primary(struct iface *i);
419
420 9a158361 Martin Mares
static int
421
ifa_recalc_primary(struct iface *i)
422
{
423 874b8685 Ondrej Zajicek
  struct ifa *a = kif_choose_primary(i);
424 9a158361 Martin Mares
425 874b8685 Ondrej Zajicek
  if (a == i->addr)
426
    return 0;
427
428
  if (i->addr)
429
    i->addr->flags &= ~IA_PRIMARY;
430
431
  if (a)
432 9a158361 Martin Mares
    {
433 874b8685 Ondrej Zajicek
      a->flags |= IA_PRIMARY;
434
      rem_node(&a->n);
435
      add_head(&i->addrs, &a->n);
436 9a158361 Martin Mares
    }
437 874b8685 Ondrej Zajicek
438
  i->addr = a;
439
  return 1;
440
}
441
442
void
443
ifa_recalc_all_primary_addresses(void)
444
{
445
  struct iface *i;
446
447
  WALK_LIST(i, iface_list)
448 9a158361 Martin Mares
    {
449 874b8685 Ondrej Zajicek
      if (ifa_recalc_primary(i))
450
        if_change_flags(i, i->flags | IF_TMP_DOWN);
451 9a158361 Martin Mares
    }
452
}
453
454 874b8685 Ondrej Zajicek
455 3d675cdb Martin Mares
/**
456
 * ifa_update - update interface address
457
 * @a: new interface address
458
 *
459
 * This function adds address information to a network
460
 * interface. It's called by the platform dependent code during
461
 * the interface update process described under if_update().
462
 */
463 9a158361 Martin Mares
struct ifa *
464
ifa_update(struct ifa *a)
465
{
466
  struct iface *i = a->iface;
467
  struct ifa *b;
468
469
  WALK_LIST(b, i->addrs)
470
    if (ipa_equal(b->ip, a->ip))
471
      {
472
        if (ipa_equal(b->prefix, a->prefix) &&
473
            b->pxlen == a->pxlen &&
474
            ipa_equal(b->brd, a->brd) &&
475
            ipa_equal(b->opposite, a->opposite) &&
476 6a636392 Martin Mares
            b->scope == a->scope &&
477
            !((b->flags ^ a->flags) & IA_UNNUMBERED))
478 9a158361 Martin Mares
          {
479
            b->flags |= IF_UPDATED;
480
            return b;
481
          }
482
        ifa_delete(b);
483
        break;
484
      }
485 6a636392 Martin Mares
486
  if (!(i->flags & IF_MULTIACCESS) && a->pxlen < BITS_PER_IP_ADDRESS - 2)
487
    log(L_WARN "Strange prefix length %d for point-to-point interface %s", a->pxlen, i->name);
488 85a291ff Martin Mares
#ifndef IPV6
489 6a636392 Martin Mares
  if ((i->flags & IF_BROADCAST) && !ipa_nonzero(a->brd))
490
    log(L_ERR "Missing broadcast address for interface %s", i->name);
491 85a291ff Martin Mares
#endif
492 6a636392 Martin Mares
493 9a158361 Martin Mares
  b = mb_alloc(if_pool, sizeof(struct ifa));
494
  memcpy(b, a, sizeof(struct ifa));
495
  add_tail(&i->addrs, &b->n);
496
  b->flags = (i->flags & ~IA_FLAGS) | (a->flags & IA_FLAGS);
497 874b8685 Ondrej Zajicek
  if (ifa_recalc_primary(i))
498 9a158361 Martin Mares
    if_change_flags(i, i->flags | IF_TMP_DOWN);
499
  if (b->flags & IF_UP)
500
    ifa_notify_change(IF_CHANGE_CREATE | IF_CHANGE_UP, b);
501
  return b;
502
}
503
504 3d675cdb Martin Mares
/**
505
 * ifa_delete - remove interface address
506
 * @a: interface address
507
 *
508
 * This function removes address information from a network
509
 * interface. It's called by the platform dependent code during
510
 * the interface update process described under if_update().
511
 */
512 9a158361 Martin Mares
void
513
ifa_delete(struct ifa *a)
514
{
515
  struct iface *i = a->iface;
516
  struct ifa *b;
517
518
  WALK_LIST(b, i->addrs)
519
    if (ipa_equal(b->ip, a->ip))
520
      {
521
        rem_node(&b->n);
522
        if (b->flags & IF_UP)
523
          {
524
            b->flags &= ~IF_UP;
525
            ifa_notify_change(IF_CHANGE_DOWN, b);
526
          }
527
        if (b->flags & IA_PRIMARY)
528
          {
529
            if_change_flags(i, i->flags | IF_TMP_DOWN);
530
            ifa_recalc_primary(i);
531
          }
532
        mb_free(b);
533 a2697f02 Martin Mares
        return;
534 9a158361 Martin Mares
      }
535
}
536
537 4e9498cb Martin Mares
static void
538 dce26783 Martin Mares
auto_router_id(void)
539 7d832907 Martin Mares
{
540 dce26783 Martin Mares
#ifndef IPV6
541 7d832907 Martin Mares
  struct iface *i, *j;
542
543
  j = NULL;
544
  WALK_LIST(i, iface_list)
545 f25cb0ef Ondrej Zajicek
    if ((i->flags & IF_ADMIN_UP) &&
546
        !(i->flags & (IF_IGNORE | IF_SHUTDOWN)) &&
547 9a158361 Martin Mares
        i->addr &&
548 6a636392 Martin Mares
        !(i->addr->flags & IA_UNNUMBERED) &&
549 9a158361 Martin Mares
        (!j || ipa_to_u32(i->addr->ip) < ipa_to_u32(j->addr->ip)))
550 7d832907 Martin Mares
      j = i;
551 31b3e1bb Martin Mares
  if (!j)
552 4e9498cb Martin Mares
    die("Cannot determine router ID (no suitable network interface found), please configure it manually");
553 6b9fa320 Martin Mares
  log(L_INFO "Guessed router ID %I according to interface %s", j->addr->ip, j->name);
554 9a158361 Martin Mares
  config->router_id = ipa_to_u32(j->addr->ip);
555 dce26783 Martin Mares
#endif
556 7d832907 Martin Mares
}
557
558 3d675cdb Martin Mares
/**
559
 * if_init - initialize interface module
560
 *
561
 * This function is called during BIRD startup to initialize
562
 * all data structures of the interface module.
563
 */
564 7d832907 Martin Mares
void
565 8a48ecb8 Martin Mares
if_init(void)
566
{
567
  if_pool = rp_new(&root_pool, "Interfaces");
568
  init_list(&iface_list);
569 85053fce Martin Mares
  neigh_init(if_pool);
570 8a48ecb8 Martin Mares
}
571 ed45f2e1 Martin Mares
572
/*
573
 *        Interface Pattern Lists
574
 */
575
576 0aad2b92 Ondrej Zajicek
int
577
iface_patt_match(struct iface_patt *ifp, struct iface *i, struct ifa *a)
578 ed45f2e1 Martin Mares
{
579 20e94fb8 Ondrej Zajicek
  struct iface_patt_node *p;
580 ed45f2e1 Martin Mares
581 20e94fb8 Ondrej Zajicek
  WALK_LIST(p, ifp->ipn_list)
582 ed45f2e1 Martin Mares
    {
583
      char *t = p->pattern;
584 20e94fb8 Ondrej Zajicek
      int pos = p->positive;
585
586 8edf2361 Martin Mares
      if (t)
587 ed45f2e1 Martin Mares
        {
588 8edf2361 Martin Mares
          if (*t == '-')
589
            {
590
              t++;
591 20e94fb8 Ondrej Zajicek
              pos = !pos;
592 8edf2361 Martin Mares
            }
593 20e94fb8 Ondrej Zajicek
594 8edf2361 Martin Mares
          if (!patmatch(t, i->name))
595
            continue;
596 ed45f2e1 Martin Mares
        }
597 20e94fb8 Ondrej Zajicek
598 5d53b807 Ondrej Zajicek
      if (p->pxlen == 0)
599
        return pos;
600 20e94fb8 Ondrej Zajicek
601 5d53b807 Ondrej Zajicek
      if (!a)
602
        continue;
603
604
      if (ipa_in_net(a->ip, p->prefix, p->pxlen))
605
        return pos;
606
607
      if ((a->flags & IA_UNNUMBERED) &&
608
          ipa_in_net(a->opposite, p->prefix, p->pxlen))
609
        return pos;
610
          
611
      continue;
612 ed45f2e1 Martin Mares
    }
613 20e94fb8 Ondrej Zajicek
614
  return 0;
615
}
616
617
struct iface_patt *
618 0aad2b92 Ondrej Zajicek
iface_patt_find(list *l, struct iface *i, struct ifa *a)
619 20e94fb8 Ondrej Zajicek
{
620
  struct iface_patt *p;
621
622
  WALK_LIST(p, *l)
623 0aad2b92 Ondrej Zajicek
    if (iface_patt_match(p, i, a))
624 20e94fb8 Ondrej Zajicek
      return p;
625
626 ed45f2e1 Martin Mares
  return NULL;
627
}
628
629 20e94fb8 Ondrej Zajicek
static int
630
iface_plists_equal(struct iface_patt *pa, struct iface_patt *pb)
631
{
632
  struct iface_patt_node *x, *y;
633
634
  x = HEAD(pa->ipn_list);
635
  y = HEAD(pb->ipn_list);
636
  while (x->n.next && y->n.next)
637
    {
638
      if ((x->positive != y->positive) ||
639
          (!x->pattern && y->pattern) ||        /* This nasty lines where written by me... :-( Feela */
640
          (!y->pattern && x->pattern) ||
641
          ((x->pattern != y->pattern) && strcmp(x->pattern, y->pattern)) ||
642
          !ipa_equal(x->prefix, y->prefix) ||
643
          (x->pxlen != y->pxlen))
644
        return 0;
645
      x = (void *) x->n.next;
646
      y = (void *) y->n.next;
647
    }
648
  return (!x->n.next && !y->n.next);
649
}
650
651 ed45f2e1 Martin Mares
int
652
iface_patts_equal(list *a, list *b, int (*comp)(struct iface_patt *, struct iface_patt *))
653
{
654
  struct iface_patt *x, *y;
655
656
  x = HEAD(*a);
657
  y = HEAD(*b);
658
  while (x->n.next && y->n.next)
659
    {
660 20e94fb8 Ondrej Zajicek
      if (!iface_plists_equal(x, y) ||
661
          (comp && !comp(x, y)))
662 ed45f2e1 Martin Mares
        return 0;
663
      x = (void *) x->n.next;
664
      y = (void *) y->n.next;
665
    }
666
  return (!x->n.next && !y->n.next);
667
}
668 ae97b946 Martin Mares
669
/*
670
 *  CLI commands.
671
 */
672
673
static void
674
if_show_addr(struct ifa *a)
675
{
676
  byte broad[STD_ADDRESS_P_LENGTH + 16];
677
  byte opp[STD_ADDRESS_P_LENGTH + 16];
678
679
  if (ipa_nonzero(a->brd))
680
    bsprintf(broad, ", broadcast %I", a->brd);
681
  else
682
    broad[0] = 0;
683
  if (ipa_nonzero(a->opposite))
684
    bsprintf(opp, ", opposite %I", a->opposite);
685
  else
686
    opp[0] = 0;
687 6a636392 Martin Mares
  cli_msg(-1003, "\t%I/%d (%s%s%s, scope %s%s)",
688 ae97b946 Martin Mares
          a->ip, a->pxlen,
689 7b7a7b43 Martin Mares
          (a->flags & IA_PRIMARY) ? "Primary" : (a->flags & IA_SECONDARY) ? "Secondary" : "Unselected",
690 ae97b946 Martin Mares
          broad, opp,
691 6a636392 Martin Mares
          ip_scope_text(a->scope),
692
          (a->flags & IA_UNNUMBERED) ? ", unnumbered" : "");
693 ae97b946 Martin Mares
}
694
695
void
696
if_show(void)
697
{
698
  struct iface *i;
699
  struct ifa *a;
700
  char *type;
701
702
  WALK_LIST(i, iface_list)
703
    {
704 f25cb0ef Ondrej Zajicek
      if (i->flags & IF_SHUTDOWN)
705
        continue;
706
707 0d3e6bce Martin Mares
      cli_msg(-1001, "%s %s (index=%d)", i->name, (i->flags & IF_UP) ? "up" : "DOWN", i->index);
708 6a636392 Martin Mares
      if (!(i->flags & IF_MULTIACCESS))
709 ae97b946 Martin Mares
        type = "PtP";
710
      else
711
        type = "MultiAccess";
712 0d3e6bce Martin Mares
      cli_msg(-1004, "\t%s%s%s Admin%s Link%s%s%s MTU=%d",
713 ae97b946 Martin Mares
              type,
714
              (i->flags & IF_BROADCAST) ? " Broadcast" : "",
715
              (i->flags & IF_MULTICAST) ? " Multicast" : "",
716 f25cb0ef Ondrej Zajicek
              (i->flags & IF_ADMIN_UP) ? "Up" : "Down",
717 ae97b946 Martin Mares
              (i->flags & IF_LINK_UP) ? "Up" : "Down",
718
              (i->flags & IF_LOOPBACK) ? " Loopback" : "",
719
              (i->flags & IF_IGNORE) ? " Ignored" : "",
720
              i->mtu);
721
      if (i->addr)
722
        if_show_addr(i->addr);
723
      WALK_LIST(a, i->addrs)
724
        if (a != i->addr)
725
          if_show_addr(a);
726
    }
727
  cli_msg(0, "");
728
}
729
730
void
731
if_show_summary(void)
732
{
733
  struct iface *i;
734
  byte addr[STD_ADDRESS_P_LENGTH + 16];
735
736 0d3e6bce Martin Mares
  cli_msg(-2005, "interface state address");
737 ae97b946 Martin Mares
  WALK_LIST(i, iface_list)
738
    {
739
      if (i->addr)
740
        bsprintf(addr, "%I/%d", i->addr->ip, i->addr->pxlen);
741
      else
742
        addr[0] = 0;
743 0d3e6bce Martin Mares
      cli_msg(-1005, "%-9s %-5s %s", i->name, (i->flags & IF_UP) ? "up" : "DOWN", addr);
744 ae97b946 Martin Mares
    }
745
  cli_msg(0, "");
746
}