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