]>
git.ipfire.org Git - thirdparty/bird.git/blob - nest/iface.c
2 * BIRD -- Management of Interfaces and Neighbor Cache
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
12 * The interface module keeps track of all network interfaces in the
13 * system and their addresses.
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
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.
28 #include "nest/bird.h"
29 #include "nest/iface.h"
30 #include "nest/protocol.h"
32 #include "lib/resource.h"
33 #include "lib/string.h"
34 #include "conf/conf.h"
38 static void auto_router_id(void);
43 * ifa_dump - dump interface address
44 * @a: interface address descriptor
46 * This function dumps contents of an &ifa to the debug output.
49 ifa_dump(struct ifa
*a
)
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" : "");
58 * if_dump - dump interface
59 * @i: interface to dump
61 * This function dumps all information associated with a given
62 * network interface to the debug output.
65 if_dump(struct iface
*i
)
69 debug("IF%d: %s", i
->index
, i
->name
);
70 if (i
->flags
& IF_ADMIN_DOWN
)
76 if (i
->flags
& IF_LINK_UP
)
78 if (i
->flags
& IF_MULTIACCESS
)
80 if (i
->flags
& IF_BROADCAST
)
82 if (i
->flags
& IF_MULTICAST
)
84 if (i
->flags
& IF_LOOPBACK
)
86 if (i
->flags
& IF_IGNORE
)
88 if (i
->flags
& IF_TMP_DOWN
)
90 debug(" MTU=%d\n", i
->mtu
);
91 WALK_LIST(a
, i
->addrs
)
94 ASSERT((a
!= i
->addr
) == !(a
->flags
& IA_PRIMARY
));
99 * if_dump_all - dump all interfaces
101 * This function dumps information about all known network
102 * interfaces to the debug output.
109 debug("Known network interfaces:\n");
110 WALK_LIST(i
, iface_list
)
112 debug("Router ID: %08x\n", config
->router_id
);
115 static inline unsigned
116 if_what_changed(struct iface
*i
, struct iface
*j
)
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
;
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
)
132 if_copy(struct iface
*to
, struct iface
*from
)
134 to
->flags
= from
->flags
| (to
->flags
& IF_TMP_DOWN
);
139 ifa_send_notify(struct proto
*p
, unsigned c
, struct ifa
*a
)
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
);
153 ifa_notify_change(unsigned c
, struct ifa
*a
)
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
);
163 if_send_notify(struct proto
*p
, unsigned c
, struct iface
*i
)
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
);
179 if_notify_change(unsigned c
, struct iface
*i
)
184 if (i
->flags
& IF_JUST_CREATED
)
186 i
->flags
&= ~IF_JUST_CREATED
;
187 c
|= IF_CHANGE_CREATE
| IF_CHANGE_MTU
;
190 DBG("Interface change notification (%x) for %s\n", c
, i
->name
);
195 if (c
& IF_CHANGE_UP
)
197 if (c
& IF_CHANGE_DOWN
)
198 WALK_LIST(a
, i
->addrs
)
200 a
->flags
= (i
->flags
& ~IA_FLAGS
) | (a
->flags
& IA_FLAGS
);
201 ifa_notify_change(IF_CHANGE_DOWN
, a
);
204 WALK_LIST(p
, active_proto_list
)
205 if_send_notify(p
, c
, i
);
207 if (c
& IF_CHANGE_UP
)
208 WALK_LIST(a
, i
->addrs
)
210 a
->flags
= (i
->flags
& ~IA_FLAGS
) | (a
->flags
& IA_FLAGS
);
211 ifa_notify_change(IF_CHANGE_UP
, a
);
213 if (c
& IF_CHANGE_DOWN
)
218 if_recalc_flags(struct iface
*i
, unsigned flags
)
220 if ((flags
& (IF_ADMIN_DOWN
| IF_TMP_DOWN
)) ||
221 !(flags
& IF_LINK_UP
) ||
230 if_change_flags(struct iface
*i
, unsigned flags
)
232 unsigned of
= i
->flags
;
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
);
240 * if_update - update interface status
241 * @new: new interface status
243 * if_update() is called by the low-level platform dependent code
244 * whenever it notices an interface change.
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.
253 * if_update() will automatically notify all other modules about the change.
256 if_update(struct iface
*new)
261 WALK_LIST(i
, iface_list
)
262 if (!strcmp(new->name
, i
->name
))
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 */
269 DBG("Interface %s changed too much -- forcing down/up transition\n", i
->name
);
270 if_change_flags(i
, i
->flags
| IF_TMP_DOWN
);
273 memcpy(&new->addrs
, &i
->addrs
, sizeof(i
->addrs
));
274 memcpy(i
, new, sizeof(*i
));
280 if_notify_change(c
, i
);
282 i
->flags
|= IF_UPDATED
;
285 i
= mb_alloc(if_pool
, sizeof(struct iface
));
286 memcpy(i
, new, sizeof(*i
));
287 init_list(&i
->addrs
);
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
);
296 if_start_update(void)
301 WALK_LIST(i
, iface_list
)
303 i
->flags
&= ~IF_UPDATED
;
304 WALK_LIST(a
, i
->addrs
)
305 a
->flags
&= ~IF_UPDATED
;
310 if_end_partial_update(struct iface
*i
)
312 if (i
->flags
& IF_TMP_DOWN
)
313 if_change_flags(i
, i
->flags
& ~IF_TMP_DOWN
);
322 if (!config
->router_id
)
325 WALK_LIST(i
, iface_list
)
327 if (!(i
->flags
& IF_UPDATED
))
328 if_change_flags(i
, (i
->flags
& ~IF_LINK_UP
) | IF_ADMIN_DOWN
);
331 WALK_LIST_DELSAFE(a
, b
, i
->addrs
)
332 if (!(a
->flags
& IF_UPDATED
))
334 if_end_partial_update(i
);
340 if_flush_ifaces(struct proto
*p
)
342 if (p
->debug
& D_EVENTS
)
343 log(L_TRACE
"%s: Flushing interfaces", p
->name
);
349 * if_feed_baby - advertise interfaces to a new protocol
350 * @p: protocol to feed
352 * When a new protocol starts, this function sends it a series
353 * of notifications about all existing interfaces.
356 if_feed_baby(struct proto
*p
)
361 if (!p
->if_notify
&& !p
->ifa_notify
) /* shortcut */
363 DBG("Announcing interfaces to new protocol %s\n", p
->name
);
364 WALK_LIST(i
, iface_list
)
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
);
374 * if_find_by_index - find interface by ifindex
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.
382 if_find_by_index(unsigned idx
)
386 WALK_LIST(i
, iface_list
)
393 * if_find_by_name - find interface by name
394 * @name: interface name
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.
401 if_find_by_name(char *name
)
405 WALK_LIST(i
, iface_list
)
406 if (!strcmp(i
->name
, name
))
411 struct ifa
*kif_choose_primary(struct iface
*i
);
414 ifa_recalc_primary(struct iface
*i
)
416 struct ifa
*a
= kif_choose_primary(i
);
422 i
->addr
->flags
&= ~IA_PRIMARY
;
426 a
->flags
|= IA_PRIMARY
;
428 add_head(&i
->addrs
, &a
->n
);
436 ifa_recalc_all_primary_addresses(void)
440 WALK_LIST(i
, iface_list
)
442 if (ifa_recalc_primary(i
))
443 if_change_flags(i
, i
->flags
| IF_TMP_DOWN
);
449 * ifa_update - update interface address
450 * @a: new interface address
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().
457 ifa_update(struct ifa
*a
)
459 struct iface
*i
= a
->iface
;
462 WALK_LIST(b
, i
->addrs
)
463 if (ipa_equal(b
->ip
, a
->ip
))
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
))
472 b
->flags
|= IF_UPDATED
;
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
);
482 if ((i
->flags
& IF_BROADCAST
) && !ipa_nonzero(a
->brd
))
483 log(L_ERR
"Missing broadcast address for interface %s", i
->name
);
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
);
498 * ifa_delete - remove interface address
499 * @a: interface address
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().
506 ifa_delete(struct ifa
*a
)
508 struct iface
*i
= a
->iface
;
511 WALK_LIST(b
, i
->addrs
)
512 if (ipa_equal(b
->ip
, a
->ip
))
515 if (b
->flags
& IF_UP
)
518 ifa_notify_change(IF_CHANGE_DOWN
, b
);
520 if (b
->flags
& IA_PRIMARY
)
522 if_change_flags(i
, i
->flags
| IF_TMP_DOWN
);
523 ifa_recalc_primary(i
);
537 WALK_LIST(i
, iface_list
)
538 if ((i
->flags
& IF_LINK_UP
) &&
539 !(i
->flags
& (IF_IGNORE
| IF_ADMIN_DOWN
)) &&
541 !(i
->addr
->flags
& IA_UNNUMBERED
) &&
542 (!j
|| ipa_to_u32(i
->addr
->ip
) < ipa_to_u32(j
->addr
->ip
)))
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
);
552 * if_init - initialize interface module
554 * This function is called during BIRD startup to initialize
555 * all data structures of the interface module.
560 if_pool
= rp_new(&root_pool
, "Interfaces");
561 init_list(&iface_list
);
566 * Interface Pattern Lists
570 iface_patt_match(struct iface_patt
*ifp
, struct iface
*i
, struct ifa
*a
)
572 struct iface_patt_node
*p
;
574 WALK_LIST(p
, ifp
->ipn_list
)
576 char *t
= p
->pattern
;
577 int pos
= p
->positive
;
587 if (!patmatch(t
, i
->name
))
597 if (ipa_in_net(a
->ip
, p
->prefix
, p
->pxlen
))
600 if ((a
->flags
& IA_UNNUMBERED
) &&
601 ipa_in_net(a
->opposite
, p
->prefix
, p
->pxlen
))
611 iface_patt_find(list
*l
, struct iface
*i
, struct ifa
*a
)
613 struct iface_patt
*p
;
616 if (iface_patt_match(p
, i
, a
))
623 iface_plists_equal(struct iface_patt
*pa
, struct iface_patt
*pb
)
625 struct iface_patt_node
*x
, *y
;
627 x
= HEAD(pa
->ipn_list
);
628 y
= HEAD(pb
->ipn_list
);
629 while (x
->n
.next
&& y
->n
.next
)
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
))
638 x
= (void *) x
->n
.next
;
639 y
= (void *) y
->n
.next
;
641 return (!x
->n
.next
&& !y
->n
.next
);
645 iface_patts_equal(list
*a
, list
*b
, int (*comp
)(struct iface_patt
*, struct iface_patt
*))
647 struct iface_patt
*x
, *y
;
651 while (x
->n
.next
&& y
->n
.next
)
653 if (!iface_plists_equal(x
, y
) ||
654 (comp
&& !comp(x
, y
)))
656 x
= (void *) x
->n
.next
;
657 y
= (void *) y
->n
.next
;
659 return (!x
->n
.next
&& !y
->n
.next
);
667 if_show_addr(struct ifa
*a
)
669 byte broad
[STD_ADDRESS_P_LENGTH
+ 16];
670 byte opp
[STD_ADDRESS_P_LENGTH
+ 16];
672 if (ipa_nonzero(a
->brd
))
673 bsprintf(broad
, ", broadcast %I", a
->brd
);
676 if (ipa_nonzero(a
->opposite
))
677 bsprintf(opp
, ", opposite %I", a
->opposite
);
680 cli_msg(-1003, "\t%I/%d (%s%s%s, scope %s%s)",
682 (a
->flags
& IA_PRIMARY
) ? "Primary" : (a
->flags
& IA_SECONDARY
) ? "Secondary" : "Unselected",
684 ip_scope_text(a
->scope
),
685 (a
->flags
& IA_UNNUMBERED
) ? ", unnumbered" : "");
695 WALK_LIST(i
, iface_list
)
697 cli_msg(-1001, "%s %s (index=%d)", i
->name
, (i
->flags
& IF_UP
) ? "up" : "DOWN", i
->index
);
698 if (!(i
->flags
& IF_MULTIACCESS
))
701 type
= "MultiAccess";
702 cli_msg(-1004, "\t%s%s%s Admin%s Link%s%s%s MTU=%d",
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" : "",
712 if_show_addr(i
->addr
);
713 WALK_LIST(a
, i
->addrs
)
721 if_show_summary(void)
724 byte addr
[STD_ADDRESS_P_LENGTH
+ 16];
726 cli_msg(-2005, "interface state address");
727 WALK_LIST(i
, iface_list
)
730 bsprintf(addr
, "%I/%d", i
->addr
->ip
, i
->addr
->pxlen
);
733 cli_msg(-1005, "%-9s %-5s %s", i
->name
, (i
->flags
& IF_UP
) ? "up" : "DOWN", addr
);