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

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

26
#undef LOCAL_DEBUG
27

28 29
#include "nest/bird.h"
#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 38

static pool *if_pool;

39 40
list iface_list;

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

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

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

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

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

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

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

123
  if (((i->flags ^ j->flags) & ~(IF_UP | IF_SHUTDOWN | IF_UPDATED | IF_ADMIN_UP | IF_LINK_UP | IF_TMP_DOWN | IF_JUST_CREATED))
Ondřej Zajíček's avatar
Ondřej Zajíček committed
124
      || (i->index != j->index) || (i->master != j->master))
125 126 127 128
    return IF_CHANGE_TOO_MUCH;
  c = 0;
  if ((i->flags ^ j->flags) & IF_UP)
    c |= (i->flags & IF_UP) ? IF_CHANGE_DOWN : IF_CHANGE_UP;
129 130
  if ((i->flags ^ j->flags) & IF_LINK_UP)
    c |= IF_CHANGE_LINK;
131 132 133
  if (i->mtu != j->mtu)
    c |= IF_CHANGE_MTU;
  return c;
134 135 136
}

static inline void
137
if_copy(struct iface *to, struct iface *from)
138
{
139
  to->flags = from->flags | (to->flags & IF_TMP_DOWN);
140
  to->mtu = from->mtu;
Ondřej Zajíček's avatar
Ondřej Zajíček committed
141 142
  to->master_index = from->master_index;
  to->master = from->master;
143 144
}

145 146 147
static inline void
ifa_send_notify(struct proto *p, unsigned c, struct ifa *a)
{
148 149 150
  if (p->ifa_notify &&
      (p->proto_state != PS_DOWN) &&
      (!p->vrf || p->vrf == a->iface->master))
151 152
    {
      if (p->debug & D_IFACES)
153 154
	log(L_TRACE "%s < address %N on interface %s %s",
	    p->name, &a->prefix, a->iface->name,
155 156 157 158 159
	    (c & IF_CHANGE_UP) ? "added" : "removed");
      p->ifa_notify(p, c, a);
    }
}

160
static void
161
ifa_notify_change_(unsigned c, struct ifa *a)
162
{
163
  struct proto *p;
164

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

167
  WALK_LIST(p, proto_list)
168 169 170
    ifa_send_notify(p, c, a);
}

171 172 173
static inline void
ifa_notify_change(unsigned c, struct ifa *a)
{
174 175 176 177 178 179 180
  if (c & IF_CHANGE_DOWN)
    neigh_ifa_update(a);

  ifa_notify_change_(c, a);

  if (c & IF_CHANGE_UP)
    neigh_ifa_update(a);
181 182
}

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

static void
204
if_notify_change(unsigned c, struct iface *i)
205
{
206
  struct proto *p;
207
  struct ifa *a;
208

209 210 211 212 213 214
  if (i->flags & IF_JUST_CREATED)
    {
      i->flags &= ~IF_JUST_CREATED;
      c |= IF_CHANGE_CREATE | IF_CHANGE_MTU;
    }

215
  DBG("Interface change notification (%x) for %s\n", c, i->name);
216
#ifdef LOCAL_DEBUG
217
  if_dump(i);
218
#endif
219

220 221
  if (c & IF_CHANGE_DOWN)
    neigh_if_down(i);
222

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

227
  WALK_LIST(p, proto_list)
228
    if_send_notify(p, c, i);
229

230 231
  if (c & IF_CHANGE_UP)
    WALK_LIST(a, i->addrs)
232
      ifa_notify_change_(IF_CHANGE_UP, a);
233

234 235 236
  if (c & IF_CHANGE_UP)
    neigh_if_up(i);

237 238
  if ((c & (IF_CHANGE_UP | IF_CHANGE_DOWN | IF_CHANGE_LINK)) == IF_CHANGE_LINK)
    neigh_if_link(i);
239 240
}

241 242
static uint
if_recalc_flags(struct iface *i UNUSED, uint flags)
243
{
244 245 246
  if ((flags & IF_ADMIN_UP) &&
      !(flags & (IF_SHUTDOWN | IF_TMP_DOWN)) &&
      !(i->master_index && !i->master))
247
    flags |= IF_UP;
248 249 250
  else
    flags &= ~IF_UP;

251 252 253 254
  return flags;
}

static void
255
if_change_flags(struct iface *i, uint flags)
256
{
257
  uint of = i->flags;
258
  i->flags = if_recalc_flags(i, flags);
259

260 261 262 263
  if ((i->flags ^ of) & IF_UP)
    if_notify_change((i->flags & IF_UP) ? IF_CHANGE_UP : IF_CHANGE_DOWN, i);
}

264
/**
265 266
 * if_delete - remove interface
 * @old: interface
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
 *
 * This function is called by the low-level platform dependent code
 * whenever it notices an interface disappears. It is just a shorthand
 * for if_update().
 */

void
if_delete(struct iface *old)
{
  struct iface f = {};
  strncpy(f.name, old->name, sizeof(f.name)-1);
  f.flags = IF_SHUTDOWN;
  if_update(&f);
}

Martin Mareš's avatar
Martin Mareš committed
282 283 284 285 286 287 288
/**
 * if_update - update interface status
 * @new: new interface status
 *
 * if_update() is called by the low-level platform dependent code
 * whenever it notices an interface change.
 *
289
 * There exist two types of interface updates -- synchronous and asynchronous
Martin Mareš's avatar
Martin Mareš committed
290 291 292 293 294 295 296 297
 * ones. In the synchronous case, the low-level code calls if_start_update(),
 * scans all interfaces reported by the OS, uses if_update() and ifa_update()
 * to pass them to the core and then it finishes the update sequence by
 * calling if_end_update(). When working asynchronously, the sysdep code
 * calls if_update() and ifa_update() whenever it notices a change.
 *
 * if_update() will automatically notify all other modules about the change.
 */
298
struct iface *
299 300 301
if_update(struct iface *new)
{
  struct iface *i;
302
  unsigned c;
303 304 305 306

  WALK_LIST(i, iface_list)
    if (!strcmp(new->name, i->name))
      {
307 308 309
	new->flags = if_recalc_flags(new, new->flags);
	c = if_what_changed(i, new);
	if (c & IF_CHANGE_TOO_MUCH)	/* Changed a lot, convert it to down/up */
310
	  {
311
	    DBG("Interface %s changed too much -- forcing down/up transition\n", i->name);
312
	    if_change_flags(i, i->flags | IF_TMP_DOWN);
313
	    rem_node(&i->n);
314 315 316 317
	    new->addr4 = i->addr4;
	    new->addr6 = i->addr6;
	    new->llv6 = i->llv6;
	    new->sysdep = i->sysdep;
318 319
	    memcpy(&new->addrs, &i->addrs, sizeof(i->addrs));
	    memcpy(i, new, sizeof(*i));
320
	    i->flags &= ~IF_UP;		/* IF_TMP_DOWN will be added later */
321
	    goto newif;
322
	  }
323 324 325 326 327

	if_copy(i, new);
	if (c)
	  if_notify_change(c, i);

328
	i->flags |= IF_UPDATED;
329
	return i;
330
      }
331 332
  i = mb_alloc(if_pool, sizeof(struct iface));
  memcpy(i, new, sizeof(*i));
333
  init_list(&i->addrs);
334
newif:
335
  init_list(&i->neighbors);
336
  i->flags |= IF_UPDATED | IF_TMP_DOWN;		/* Tmp down as we don't have addresses yet */
337
  add_tail(&iface_list, &i->n);
338
  return i;
339 340
}

341 342 343 344
void
if_start_update(void)
{
  struct iface *i;
345
  struct ifa *a;
346 347

  WALK_LIST(i, iface_list)
348 349 350
    {
      i->flags &= ~IF_UPDATED;
      WALK_LIST(a, i->addrs)
351
	a->flags &= ~IA_UPDATED;
352 353 354 355 356 357
    }
}

void
if_end_partial_update(struct iface *i)
{
358 359 360
  if (i->flags & IF_NEEDS_RECALC)
    if_recalc_preferred(i);

361 362
  if (i->flags & IF_TMP_DOWN)
    if_change_flags(i, i->flags & ~IF_TMP_DOWN);
363 364
}

365 366 367
void
if_end_update(void)
{
368
  struct iface *i;
369
  struct ifa *a, *b;
370 371

  WALK_LIST(i, iface_list)
372 373
    {
      if (!(i->flags & IF_UPDATED))
374
	if_change_flags(i, (i->flags & ~IF_ADMIN_UP) | IF_SHUTDOWN);
375 376 377
      else
	{
	  WALK_LIST_DELSAFE(a, b, i->addrs)
378
	    if (!(a->flags & IA_UPDATED))
379 380 381 382
	      ifa_delete(a);
	  if_end_partial_update(i);
	}
    }
383 384
}

385 386 387 388 389 390 391 392 393
void
if_flush_ifaces(struct proto *p)
{
  if (p->debug & D_EVENTS)
    log(L_TRACE "%s: Flushing interfaces", p->name);
  if_start_update();
  if_end_update();
}

Martin Mareš's avatar
Martin Mareš committed
394 395 396 397 398 399 400
/**
 * if_feed_baby - advertise interfaces to a new protocol
 * @p: protocol to feed
 *
 * When a new protocol starts, this function sends it a series
 * of notifications about all existing interfaces.
 */
401 402 403 404
void
if_feed_baby(struct proto *p)
{
  struct iface *i;
405
  struct ifa *a;
406

407
  if (!p->if_notify && !p->ifa_notify)	/* shortcut */
408
    return;
409
  DBG("Announcing interfaces to new protocol %s\n", p->name);
410
  WALK_LIST(i, iface_list)
411
    {
412 413
      if_send_notify(p, IF_CHANGE_CREATE | ((i->flags & IF_UP) ? IF_CHANGE_UP : 0), i);
      if (i->flags & IF_UP)
414
	WALK_LIST(a, i->addrs)
415
	  ifa_send_notify(p, IF_CHANGE_CREATE | IF_CHANGE_UP, a);
416
    }
417 418
}

Martin Mareš's avatar
Martin Mareš committed
419 420 421 422 423 424 425 426
/**
 * if_find_by_index - find interface by ifindex
 * @idx: ifindex
 *
 * This function finds an &iface structure corresponding to an interface
 * of the given index @idx. Returns a pointer to the structure or %NULL
 * if no such structure exists.
 */
427 428 429 430 431 432
struct iface *
if_find_by_index(unsigned idx)
{
  struct iface *i;

  WALK_LIST(i, iface_list)
433
    if (i->index == idx && !(i->flags & IF_SHUTDOWN))
434 435 436 437
      return i;
  return NULL;
}

Martin Mareš's avatar
Martin Mareš committed
438 439 440 441 442 443 444 445
/**
 * if_find_by_name - find interface by name
 * @name: interface name
 *
 * This function finds an &iface structure corresponding to an interface
 * of the given name @name. Returns a pointer to the structure or %NULL
 * if no such structure exists.
 */
446 447 448 449 450 451
struct iface *
if_find_by_name(char *name)
{
  struct iface *i;

  WALK_LIST(i, iface_list)
452
    if (!strcmp(i->name, name) && !(i->flags & IF_SHUTDOWN))
453 454 455 456
      return i;
  return NULL;
}

457 458 459 460 461
struct iface *
if_get_by_name(char *name)
{
  struct iface *i;

462 463 464
  WALK_LIST(i, iface_list)
    if (!strcmp(i->name, name))
      return i;
465 466 467 468 469 470 471 472 473 474 475

  /* No active iface, create a dummy */
  i = mb_allocz(if_pool, sizeof(struct iface));
  strncpy(i->name, name, sizeof(i->name)-1);
  i->flags = IF_SHUTDOWN;
  init_list(&i->addrs);
  init_list(&i->neighbors);
  add_tail(&iface_list, &i->n);
  return i;
}

476 477 478 479 480 481 482
static inline void
if_set_preferred(struct ifa **pos, struct ifa *new)
{
  if (*pos)
    (*pos)->flags &= ~IA_PRIMARY;
  if (new)
    new->flags |= IA_PRIMARY;
483

484 485 486 487 488
  *pos = new;
}

static void
if_recalc_preferred(struct iface *i)
489
{
490 491 492 493 494 495 496
  /*
   * Preferred address selection priority:
   * 1) Address configured in Device protocol
   * 2) Sysdep IPv4 address (BSD)
   * 3) Old preferred address
   * 4) First address in list
   */
497

498 499 500 501 502 503 504
  struct kif_iface_config *ic = kif_get_iface_config(i);
  struct ifa *a4 = i->addr4, *a6 = i->addr6, *ll = i->llv6;
  ip_addr pref_v4 = ic->pref_v4;
  uint change = 0;

  if (kif_update_sysdep_addr(i))
    change |= IF_CHANGE_SYSDEP;
505

506 507 508
  /* BSD sysdep address */
  if (ipa_zero(pref_v4) && ip4_nonzero(i->sysdep))
    pref_v4 = ipa_from_ip4(i->sysdep);
509

510 511
  struct ifa *a;
  WALK_LIST(a, i->addrs)
512
    {
513 514 515 516 517 518 519 520 521 522 523 524 525 526
      /* Secondary address is never selected */
      if (a->flags & IA_SECONDARY)
	continue;

      if (ipa_is_ip4(a->ip)) {
	if (!a4 || ipa_equal(a->ip, pref_v4))
	  a4 = a;
      } else if (!ipa_is_link_local(a->ip)) {
	if (!a6 || ipa_equal(a->ip, ic->pref_v6))
	  a6 = a;
      } else {
	if (!ll || ipa_equal(a->ip, ic->pref_ll))
	  ll = a;
      }
527
    }
528

529
  if ((a4 != i->addr4) || (i->flags & IF_LOST_ADDR4))
530 531 532 533 534
  {
    if_set_preferred(&i->addr4, a4);
    change |= IF_CHANGE_ADDR4;
  }

535
  if ((a6 != i->addr6) || (i->flags & IF_LOST_ADDR6))
536 537 538 539 540
  {
    if_set_preferred(&i->addr6, a6);
    change |= IF_CHANGE_ADDR6;
  }

541
  if ((ll != i->llv6) || (i->flags & IF_LOST_LLV6))
542 543 544 545 546
  {
    if_set_preferred(&i->llv6, ll);
    change |= IF_CHANGE_LLV6;
  }

547
  i->flags &= ~(IF_NEEDS_RECALC | IF_LOST_ADDR4 | IF_LOST_ADDR6 | IF_LOST_LLV6);
548 549

  if (change)
550
    if_notify_change(change, i);
551 552 553
}

void
554
if_recalc_all_preferred_addresses(void)
555 556 557 558
{
  struct iface *i;

  WALK_LIST(i, iface_list)
559 560 561 562 563 564
  {
    if_recalc_preferred(i);

    if (i->flags & IF_TMP_DOWN)
      if_change_flags(i, i->flags & ~IF_TMP_DOWN);
  }
565 566
}

567 568 569
static inline int
ifa_same(struct ifa *a, struct ifa *b)
{
570
  return ipa_equal(a->ip, b->ip) && net_equal(&a->prefix, &b->prefix);
571 572
}

573

Martin Mareš's avatar
Martin Mareš committed
574 575 576 577 578 579 580 581
/**
 * ifa_update - update interface address
 * @a: new interface address
 *
 * This function adds address information to a network
 * interface. It's called by the platform dependent code during
 * the interface update process described under if_update().
 */
582 583 584 585 586 587 588
struct ifa *
ifa_update(struct ifa *a)
{
  struct iface *i = a->iface;
  struct ifa *b;

  WALK_LIST(b, i->addrs)
589
    if (ifa_same(b, a))
590
      {
591
	if (ipa_equal(b->brd, a->brd) &&
592
	    ipa_equal(b->opposite, a->opposite) &&
593
	    b->scope == a->scope &&
594
	    !((b->flags ^ a->flags) & IA_PEER))
595
	  {
596
	    b->flags |= IA_UPDATED;
597 598 599 600 601
	    return b;
	  }
	ifa_delete(b);
	break;
      }
602

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

606 607 608
  b = mb_alloc(if_pool, sizeof(struct ifa));
  memcpy(b, a, sizeof(struct ifa));
  add_tail(&i->addrs, &b->n);
609 610 611 612
  b->flags |= IA_UPDATED;

  i->flags |= IF_NEEDS_RECALC;
  if (i->flags & IF_UP)
613 614 615 616
    ifa_notify_change(IF_CHANGE_CREATE | IF_CHANGE_UP, b);
  return b;
}

Martin Mareš's avatar
Martin Mareš committed
617 618 619 620 621 622 623 624
/**
 * ifa_delete - remove interface address
 * @a: interface address
 *
 * This function removes address information from a network
 * interface. It's called by the platform dependent code during
 * the interface update process described under if_update().
 */
625 626 627 628 629 630 631
void
ifa_delete(struct ifa *a)
{
  struct iface *i = a->iface;
  struct ifa *b;

  WALK_LIST(b, i->addrs)
632
    if (ifa_same(b, a))
633 634
      {
	rem_node(&b->n);
635

636 637
	if (b->flags & IA_PRIMARY)
	  {
638 639 640 641
	    /*
	     * We unlink deleted preferred address and mark for recalculation.
	     * FIXME: This could break if we make iface scan non-atomic, as
	     * protocols still could use the freed address until they get
642 643
	     * if_notify from preferred route recalculation. We should fix and
	     * simplify this in the future by having struct ifa refcounted
644
	     */
645 646 647
	    if (b == i->addr4) { i->addr4 = NULL; i->flags |= IF_LOST_ADDR4; }
	    if (b == i->addr6) { i->addr6 = NULL; i->flags |= IF_LOST_ADDR6; }
	    if (b == i->llv6)  { i->llv6 = NULL;  i->flags |= IF_LOST_LLV6; }
648
	    i->flags |= IF_NEEDS_RECALC;
649
	  }
650 651 652 653

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

654
	mb_free(b);
655
	return;
656 657 658
      }
}

659 660
u32
if_choose_router_id(struct iface_patt *mask, u32 old_id)
661
{
662 663
  struct iface *i;
  struct ifa *a, *b;
664

665
  b = NULL;
666
  WALK_LIST(i, iface_list)
667 668
    {
      if (!(i->flags & IF_ADMIN_UP) ||
669
	  (i->flags & IF_SHUTDOWN))
670 671 672 673
	continue;

      WALK_LIST(a, i->addrs)
	{
674 675 676
	  if (a->prefix.type != NET_IP4)
	    continue;

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	  if (a->flags & IA_SECONDARY)
	    continue;

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

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

	  /* No pattern or pattern matched */
	  if (!b || ipa_to_u32(a->ip) < ipa_to_u32(b->ip))
	    b = a;
	}
    }

  if (!b)
    return 0;

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

  return id;
701 702
}

Martin Mareš's avatar
Martin Mareš committed
703 704 705 706 707 708
/**
 * if_init - initialize interface module
 *
 * This function is called during BIRD startup to initialize
 * all data structures of the interface module.
 */
709 710 711 712 713
void
if_init(void)
{
  if_pool = rp_new(&root_pool, "Interfaces");
  init_list(&iface_list);
714
  neigh_init(if_pool);
715
}
716 717 718 719 720

/*
 *	Interface Pattern Lists
 */

721 722
int
iface_patt_match(struct iface_patt *ifp, struct iface *i, struct ifa *a)
723
{
724
  struct iface_patt_node *p;
725

726
  WALK_LIST(p, ifp->ipn_list)
727 728
    {
      char *t = p->pattern;
729 730
      int pos = p->positive;

731
      if (t)
732
	{
733 734 735
	  if (*t == '-')
	    {
	      t++;
736
	      pos = !pos;
737
	    }
738

739 740
	  if (!patmatch(t, i->name))
	    continue;
741
	}
742

743
      if (p->prefix.pxlen == 0)
744
	return pos;
745

746 747 748
      if (!a)
	continue;

749
      if (ipa_in_netX(a->ip, &p->prefix))
750 751
	return pos;

752
      if ((a->flags & IA_PEER) &&
753
	  ipa_in_netX(a->opposite, &p->prefix))
754
	return pos;
755

756
      continue;
757
    }
758 759 760 761 762

  return 0;
}

struct iface_patt *
763
iface_patt_find(list *l, struct iface *i, struct ifa *a)
764 765 766 767
{
  struct iface_patt *p;

  WALK_LIST(p, *l)
768
    if (iface_patt_match(p, i, a))
769 770
      return p;

771 772 773
  return NULL;
}

774 775 776 777 778 779 780 781 782 783 784 785 786
static int
iface_plists_equal(struct iface_patt *pa, struct iface_patt *pb)
{
  struct iface_patt_node *x, *y;

  x = HEAD(pa->ipn_list);
  y = HEAD(pb->ipn_list);
  while (x->n.next && y->n.next)
    {
      if ((x->positive != y->positive) ||
	  (!x->pattern && y->pattern) ||	/* This nasty lines where written by me... :-( Feela */
	  (!y->pattern && x->pattern) ||
	  ((x->pattern != y->pattern) && strcmp(x->pattern, y->pattern)) ||
787
	  !net_equal(&x->prefix, &y->prefix))
788 789 790 791 792 793 794
	return 0;
      x = (void *) x->n.next;
      y = (void *) y->n.next;
    }
  return (!x->n.next && !y->n.next);
}

795 796 797 798 799 800 801 802 803
int
iface_patts_equal(list *a, list *b, int (*comp)(struct iface_patt *, struct iface_patt *))
{
  struct iface_patt *x, *y;

  x = HEAD(*a);
  y = HEAD(*b);
  while (x->n.next && y->n.next)
    {
804 805
      if (!iface_plists_equal(x, y) ||
	  (comp && !comp(x, y)))
806 807 808 809 810 811
	return 0;
      x = (void *) x->n.next;
      y = (void *) y->n.next;
    }
  return (!x->n.next && !y->n.next);
}
812 813 814 815 816 817 818 819

/*
 *  CLI commands.
 */

static void
if_show_addr(struct ifa *a)
{
820 821 822
  byte *flg, opp[IPA_MAX_TEXT_LENGTH + 16];

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

  if (ipa_nonzero(a->opposite))
825
    bsprintf(opp, "opposite %I, ", a->opposite);
826 827
  else
    opp[0] = 0;
828 829 830

  cli_msg(-1003, "\t%I/%d (%s%sscope %s)",
	  a->ip, a->prefix.pxlen, flg, opp, ip_scope_text(a->scope));
831 832 833 834 835 836 837 838 839 840 841
}

void
if_show(void)
{
  struct iface *i;
  struct ifa *a;
  char *type;

  WALK_LIST(i, iface_list)
    {
842 843 844
      if (i->flags & IF_SHUTDOWN)
	continue;

Ondřej Zajíček's avatar
Ondřej Zajíček committed
845 846 847 848 849 850
      char mbuf[16 + sizeof(i->name)] = {};
      if (i->master)
	bsprintf(mbuf, " master=%s", i->master->name);
      else if (i->master_index)
	bsprintf(mbuf, " master=#%u", i->master_index);

Ondřej Zajíček's avatar
Ondřej Zajíček committed
851
      cli_msg(-1001, "%s %s (index=%d%s)", i->name, (i->flags & IF_UP) ? "up" : "down", i->index, mbuf);
852
      if (!(i->flags & IF_MULTIACCESS))
853 854 855
	type = "PtP";
      else
	type = "MultiAccess";
856
      cli_msg(-1004, "\t%s%s%s Admin%s Link%s%s%s MTU=%d",
857 858 859
	      type,
	      (i->flags & IF_BROADCAST) ? " Broadcast" : "",
	      (i->flags & IF_MULTICAST) ? " Multicast" : "",
860
	      (i->flags & IF_ADMIN_UP) ? "Up" : "Down",
861 862 863 864
	      (i->flags & IF_LINK_UP) ? "Up" : "Down",
	      (i->flags & IF_LOOPBACK) ? " Loopback" : "",
	      (i->flags & IF_IGNORE) ? " Ignored" : "",
	      i->mtu);
865 866 867 868 869

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

870
      WALK_LIST(a, i->addrs)
871
	if (a->prefix.type == NET_IP6)
872 873 874 875 876 877 878 879 880 881
	  if_show_addr(a);
    }
  cli_msg(0, "");
}

void
if_show_summary(void)
{
  struct iface *i;

882
  cli_msg(-2005, "%-10s %-6s %-18s %s", "Interface", "State", "IPv4 address", "IPv6 address");
883 884
  WALK_LIST(i, iface_list)
    {
885 886 887
      byte a4[IPA_MAX_TEXT_LENGTH + 17];
      byte a6[IPA_MAX_TEXT_LENGTH + 17];

888 889 890
      if (i->flags & IF_SHUTDOWN)
	continue;

891 892
      if (i->addr4)
	bsprintf(a4, "%I/%d", i->addr4->ip, i->addr4->prefix.pxlen);
893
      else
894 895 896 897 898 899 900 901
	a4[0] = 0;

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

      cli_msg(-1005, "%-10s %-6s %-18s %s",
Ondřej Zajíček's avatar
Ondřej Zajíček committed
902
	      i->name, (i->flags & IF_UP) ? "up" : "down", a4, a6);
903 904 905
    }
  cli_msg(0, "");
}