]>
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"
41 * ifa_dump - dump interface address
42 * @a: interface address descriptor
44 * This function dumps contents of an &ifa to the debug output.
47 ifa_dump(struct ifa
*a
)
49 debug("\t%I, net %N bc %I -> %I%s%s%s\n", a
->ip
, &a
->prefix
, a
->brd
, a
->opposite
,
50 (a
->flags
& IF_UP
) ? "" : " DOWN",
51 (a
->flags
& IA_PRIMARY
) ? "" : " SEC",
52 (a
->flags
& IA_PEER
) ? "PEER" : "");
56 * if_dump - dump interface
57 * @i: interface to dump
59 * This function dumps all information associated with a given
60 * network interface to the debug output.
63 if_dump(struct iface
*i
)
67 debug("IF%d: %s", i
->index
, i
->name
);
68 if (i
->flags
& IF_SHUTDOWN
)
74 if (i
->flags
& IF_ADMIN_UP
)
76 if (i
->flags
& IF_MULTIACCESS
)
78 if (i
->flags
& IF_BROADCAST
)
80 if (i
->flags
& IF_MULTICAST
)
82 if (i
->flags
& IF_LOOPBACK
)
84 if (i
->flags
& IF_IGNORE
)
86 if (i
->flags
& IF_TMP_DOWN
)
88 debug(" MTU=%d\n", i
->mtu
);
89 WALK_LIST(a
, i
->addrs
)
92 ASSERT((a
!= i
->addr
) == !(a
->flags
& IA_PRIMARY
));
97 * if_dump_all - dump all interfaces
99 * This function dumps information about all known network
100 * interfaces to the debug output.
107 debug("Known network interfaces:\n");
108 WALK_LIST(i
, iface_list
)
110 debug("Router ID: %08x\n", config
->router_id
);
113 static inline unsigned
114 if_what_changed(struct iface
*i
, struct iface
*j
)
118 if (((i
->flags
^ j
->flags
) & ~(IF_UP
| IF_SHUTDOWN
| IF_UPDATED
| IF_ADMIN_UP
| IF_LINK_UP
| IF_TMP_DOWN
| IF_JUST_CREATED
))
119 || i
->index
!= j
->index
)
120 return IF_CHANGE_TOO_MUCH
;
122 if ((i
->flags
^ j
->flags
) & IF_UP
)
123 c
|= (i
->flags
& IF_UP
) ? IF_CHANGE_DOWN
: IF_CHANGE_UP
;
124 if ((i
->flags
^ j
->flags
) & IF_LINK_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
)
141 if (p
->ifa_notify
&& (p
->proto_state
!= PS_DOWN
))
143 if (p
->debug
& D_IFACES
)
144 log(L_TRACE
"%s < %s address %N on interface %s %s",
145 p
->name
, (a
->flags
& IA_PRIMARY
) ? "primary" : "secondary",
146 &a
->prefix
, a
->iface
->name
, (c
& IF_CHANGE_UP
) ? "added" : "removed");
147 p
->ifa_notify(p
, c
, a
);
152 ifa_notify_change_(unsigned c
, struct ifa
*a
)
156 DBG("IFA change notification (%x) for %s:%I\n", c
, a
->iface
->name
, a
->ip
);
158 WALK_LIST(p
, proto_list
)
159 ifa_send_notify(p
, c
, a
);
163 ifa_notify_change(unsigned c
, struct ifa
*a
)
165 if (c
& IF_CHANGE_DOWN
)
168 ifa_notify_change_(c
, a
);
170 if (c
& IF_CHANGE_UP
)
175 if_send_notify(struct proto
*p
, unsigned c
, struct iface
*i
)
177 if (p
->if_notify
&& (p
->proto_state
!= PS_DOWN
))
179 if (p
->debug
& D_IFACES
)
180 log(L_TRACE
"%s < interface %s %s", p
->name
, i
->name
,
181 (c
& IF_CHANGE_UP
) ? "goes up" :
182 (c
& IF_CHANGE_DOWN
) ? "goes down" :
183 (c
& IF_CHANGE_MTU
) ? "changes MTU" :
184 (c
& IF_CHANGE_LINK
) ? "changes link" :
185 (c
& IF_CHANGE_CREATE
) ? "created" :
186 "sends unknown event");
187 p
->if_notify(p
, c
, i
);
192 if_notify_change(unsigned c
, struct iface
*i
)
197 if (i
->flags
& IF_JUST_CREATED
)
199 i
->flags
&= ~IF_JUST_CREATED
;
200 c
|= IF_CHANGE_CREATE
| IF_CHANGE_MTU
;
203 DBG("Interface change notification (%x) for %s\n", c
, i
->name
);
208 if (c
& IF_CHANGE_DOWN
)
211 if (c
& IF_CHANGE_DOWN
)
212 WALK_LIST(a
, i
->addrs
)
214 a
->flags
= (i
->flags
& ~IA_FLAGS
) | (a
->flags
& IA_FLAGS
);
215 ifa_notify_change_(IF_CHANGE_DOWN
, a
);
218 WALK_LIST(p
, proto_list
)
219 if_send_notify(p
, c
, i
);
221 if (c
& IF_CHANGE_UP
)
222 WALK_LIST(a
, i
->addrs
)
224 a
->flags
= (i
->flags
& ~IA_FLAGS
) | (a
->flags
& IA_FLAGS
);
225 ifa_notify_change_(IF_CHANGE_UP
, a
);
228 if (c
& IF_CHANGE_UP
)
231 if ((c
& (IF_CHANGE_UP
| IF_CHANGE_DOWN
| IF_CHANGE_LINK
)) == IF_CHANGE_LINK
)
236 if_recalc_flags(struct iface
*i
, unsigned flags
)
238 if ((flags
& (IF_SHUTDOWN
| IF_TMP_DOWN
)) ||
239 !(flags
& IF_ADMIN_UP
) ||
248 if_change_flags(struct iface
*i
, unsigned flags
)
250 unsigned of
= i
->flags
;
252 i
->flags
= if_recalc_flags(i
, flags
);
253 if ((i
->flags
^ of
) & IF_UP
)
254 if_notify_change((i
->flags
& IF_UP
) ? IF_CHANGE_UP
: IF_CHANGE_DOWN
, i
);
258 * if_delete - remove interface
261 * This function is called by the low-level platform dependent code
262 * whenever it notices an interface disappears. It is just a shorthand
267 if_delete(struct iface
*old
)
270 strncpy(f
.name
, old
->name
, sizeof(f
.name
)-1);
271 f
.flags
= IF_SHUTDOWN
;
276 * if_update - update interface status
277 * @new: new interface status
279 * if_update() is called by the low-level platform dependent code
280 * whenever it notices an interface change.
282 * There exist two types of interface updates -- synchronous and asynchronous
283 * ones. In the synchronous case, the low-level code calls if_start_update(),
284 * scans all interfaces reported by the OS, uses if_update() and ifa_update()
285 * to pass them to the core and then it finishes the update sequence by
286 * calling if_end_update(). When working asynchronously, the sysdep code
287 * calls if_update() and ifa_update() whenever it notices a change.
289 * if_update() will automatically notify all other modules about the change.
292 if_update(struct iface
*new)
297 WALK_LIST(i
, iface_list
)
298 if (!strcmp(new->name
, i
->name
))
301 new->flags
= if_recalc_flags(new, new->flags
);
302 c
= if_what_changed(i
, new);
303 if (c
& IF_CHANGE_TOO_MUCH
) /* Changed a lot, convert it to down/up */
305 DBG("Interface %s changed too much -- forcing down/up transition\n", i
->name
);
306 if_change_flags(i
, i
->flags
| IF_TMP_DOWN
);
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 */
317 if_notify_change(c
, i
);
319 i
->flags
|= IF_UPDATED
;
322 i
= mb_alloc(if_pool
, sizeof(struct iface
));
323 memcpy(i
, new, sizeof(*i
));
324 init_list(&i
->addrs
);
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
);
333 if_start_update(void)
338 WALK_LIST(i
, iface_list
)
340 i
->flags
&= ~IF_UPDATED
;
341 WALK_LIST(a
, i
->addrs
)
342 a
->flags
&= ~IF_UPDATED
;
347 if_end_partial_update(struct iface
*i
)
349 if (i
->flags
& IF_TMP_DOWN
)
350 if_change_flags(i
, i
->flags
& ~IF_TMP_DOWN
);
359 WALK_LIST(i
, iface_list
)
361 if (!(i
->flags
& IF_UPDATED
))
362 if_change_flags(i
, (i
->flags
& ~IF_ADMIN_UP
) | IF_SHUTDOWN
);
365 WALK_LIST_DELSAFE(a
, b
, i
->addrs
)
366 if (!(a
->flags
& IF_UPDATED
))
368 if_end_partial_update(i
);
374 if_flush_ifaces(struct proto
*p
)
376 if (p
->debug
& D_EVENTS
)
377 log(L_TRACE
"%s: Flushing interfaces", p
->name
);
383 * if_feed_baby - advertise interfaces to a new protocol
384 * @p: protocol to feed
386 * When a new protocol starts, this function sends it a series
387 * of notifications about all existing interfaces.
390 if_feed_baby(struct proto
*p
)
395 if (!p
->if_notify
&& !p
->ifa_notify
) /* shortcut */
397 DBG("Announcing interfaces to new protocol %s\n", p
->name
);
398 WALK_LIST(i
, iface_list
)
400 if_send_notify(p
, IF_CHANGE_CREATE
| ((i
->flags
& IF_UP
) ? IF_CHANGE_UP
: 0), i
);
401 if (i
->flags
& IF_UP
)
402 WALK_LIST(a
, i
->addrs
)
403 ifa_send_notify(p
, IF_CHANGE_CREATE
| IF_CHANGE_UP
, a
);
408 * if_find_by_index - find interface by ifindex
411 * This function finds an &iface structure corresponding to an interface
412 * of the given index @idx. Returns a pointer to the structure or %NULL
413 * if no such structure exists.
416 if_find_by_index(unsigned idx
)
420 WALK_LIST(i
, iface_list
)
421 if (i
->index
== idx
&& !(i
->flags
& IF_SHUTDOWN
))
427 * if_find_by_name - find interface by name
428 * @name: interface name
430 * This function finds an &iface structure corresponding to an interface
431 * of the given name @name. Returns a pointer to the structure or %NULL
432 * if no such structure exists.
435 if_find_by_name(char *name
)
439 WALK_LIST(i
, iface_list
)
440 if (!strcmp(i
->name
, name
))
446 if_get_by_name(char *name
)
450 if (i
= if_find_by_name(name
))
453 /* No active iface, create a dummy */
454 i
= mb_allocz(if_pool
, sizeof(struct iface
));
455 strncpy(i
->name
, name
, sizeof(i
->name
)-1);
456 i
->flags
= IF_SHUTDOWN
;
457 init_list(&i
->addrs
);
458 init_list(&i
->neighbors
);
459 add_tail(&iface_list
, &i
->n
);
463 struct ifa
*kif_choose_primary(struct iface
*i
);
466 ifa_recalc_primary(struct iface
*i
)
468 struct ifa
*a
= kif_choose_primary(i
);
474 i
->addr
->flags
&= ~IA_PRIMARY
;
478 a
->flags
|= IA_PRIMARY
;
480 add_head(&i
->addrs
, &a
->n
);
488 ifa_recalc_all_primary_addresses(void)
492 WALK_LIST(i
, iface_list
)
494 if (ifa_recalc_primary(i
))
495 if_change_flags(i
, i
->flags
| IF_TMP_DOWN
);
500 ifa_same(struct ifa
*a
, struct ifa
*b
)
502 return ipa_equal(a
->ip
, b
->ip
) && net_equal(&a
->prefix
, &b
->prefix
);
507 * ifa_update - update interface address
508 * @a: new interface address
510 * This function adds address information to a network
511 * interface. It's called by the platform dependent code during
512 * the interface update process described under if_update().
515 ifa_update(struct ifa
*a
)
517 struct iface
*i
= a
->iface
;
520 WALK_LIST(b
, i
->addrs
)
523 if (ipa_equal(b
->brd
, a
->brd
) &&
524 ipa_equal(b
->opposite
, a
->opposite
) &&
525 b
->scope
== a
->scope
&&
526 !((b
->flags
^ a
->flags
) & IA_PEER
))
528 b
->flags
|= IF_UPDATED
;
535 if ((a
->prefix
.type
== NET_IP4
) && (i
->flags
& IF_BROADCAST
) && ipa_zero(a
->brd
))
536 log(L_ERR
"Missing broadcast address for interface %s", i
->name
);
538 b
= mb_alloc(if_pool
, sizeof(struct ifa
));
539 memcpy(b
, a
, sizeof(struct ifa
));
540 add_tail(&i
->addrs
, &b
->n
);
541 b
->flags
= (i
->flags
& ~IA_FLAGS
) | (a
->flags
& IA_FLAGS
);
542 if (ifa_recalc_primary(i
))
543 if_change_flags(i
, i
->flags
| IF_TMP_DOWN
);
544 if (b
->flags
& IF_UP
)
545 ifa_notify_change(IF_CHANGE_CREATE
| IF_CHANGE_UP
, b
);
550 * ifa_delete - remove interface address
551 * @a: interface address
553 * This function removes address information from a network
554 * interface. It's called by the platform dependent code during
555 * the interface update process described under if_update().
558 ifa_delete(struct ifa
*a
)
560 struct iface
*i
= a
->iface
;
563 WALK_LIST(b
, i
->addrs
)
567 if (b
->flags
& IF_UP
)
570 ifa_notify_change(IF_CHANGE_DOWN
, b
);
572 if (b
->flags
& IA_PRIMARY
)
574 if_change_flags(i
, i
->flags
| IF_TMP_DOWN
);
575 ifa_recalc_primary(i
);
583 if_choose_router_id(struct iface_patt
*mask
, u32 old_id
)
589 WALK_LIST(i
, iface_list
)
591 if (!(i
->flags
& IF_ADMIN_UP
) ||
592 (i
->flags
& IF_SHUTDOWN
))
595 WALK_LIST(a
, i
->addrs
)
597 if (a
->prefix
.type
!= NET_IP4
)
600 if (a
->flags
& IA_SECONDARY
)
603 if (a
->scope
<= SCOPE_LINK
)
606 /* Check pattern if specified */
607 if (mask
&& !iface_patt_match(mask
, i
, a
))
610 /* No pattern or pattern matched */
611 if (!b
|| ipa_to_u32(a
->ip
) < ipa_to_u32(b
->ip
))
619 u32 id
= ipa_to_u32(b
->ip
);
621 log(L_INFO
"Chosen router ID %R according to interface %s", id
, b
->iface
->name
);
627 * if_init - initialize interface module
629 * This function is called during BIRD startup to initialize
630 * all data structures of the interface module.
635 if_pool
= rp_new(&root_pool
, "Interfaces");
636 init_list(&iface_list
);
641 * Interface Pattern Lists
645 iface_patt_match(struct iface_patt
*ifp
, struct iface
*i
, struct ifa
*a
)
647 struct iface_patt_node
*p
;
649 WALK_LIST(p
, ifp
->ipn_list
)
651 char *t
= p
->pattern
;
652 int pos
= p
->positive
;
662 if (!patmatch(t
, i
->name
))
666 if (p
->prefix
.pxlen
== 0)
672 if (ipa_in_netX(a
->ip
, &p
->prefix
))
675 if ((a
->flags
& IA_PEER
) &&
676 ipa_in_netX(a
->opposite
, &p
->prefix
))
686 iface_patt_find(list
*l
, struct iface
*i
, struct ifa
*a
)
688 struct iface_patt
*p
;
691 if (iface_patt_match(p
, i
, a
))
698 iface_plists_equal(struct iface_patt
*pa
, struct iface_patt
*pb
)
700 struct iface_patt_node
*x
, *y
;
702 x
= HEAD(pa
->ipn_list
);
703 y
= HEAD(pb
->ipn_list
);
704 while (x
->n
.next
&& y
->n
.next
)
706 if ((x
->positive
!= y
->positive
) ||
707 (!x
->pattern
&& y
->pattern
) || /* This nasty lines where written by me... :-( Feela */
708 (!y
->pattern
&& x
->pattern
) ||
709 ((x
->pattern
!= y
->pattern
) && strcmp(x
->pattern
, y
->pattern
)) ||
710 !net_equal(&x
->prefix
, &y
->prefix
))
712 x
= (void *) x
->n
.next
;
713 y
= (void *) y
->n
.next
;
715 return (!x
->n
.next
&& !y
->n
.next
);
719 iface_patts_equal(list
*a
, list
*b
, int (*comp
)(struct iface_patt
*, struct iface_patt
*))
721 struct iface_patt
*x
, *y
;
725 while (x
->n
.next
&& y
->n
.next
)
727 if (!iface_plists_equal(x
, y
) ||
728 (comp
&& !comp(x
, y
)))
730 x
= (void *) x
->n
.next
;
731 y
= (void *) y
->n
.next
;
733 return (!x
->n
.next
&& !y
->n
.next
);
741 if_show_addr(struct ifa
*a
)
743 byte opp
[IPA_MAX_TEXT_LENGTH
+ 16];
745 if (ipa_nonzero(a
->opposite
))
746 bsprintf(opp
, ", opposite %I", a
->opposite
);
749 cli_msg(-1003, "\t%I/%d (%s%s, scope %s)",
750 a
->ip
, a
->prefix
.pxlen
,
751 (a
->flags
& IA_PRIMARY
) ? "Primary" : (a
->flags
& IA_SECONDARY
) ? "Secondary" : "Unselected",
752 opp
, ip_scope_text(a
->scope
));
762 WALK_LIST(i
, iface_list
)
764 if (i
->flags
& IF_SHUTDOWN
)
767 cli_msg(-1001, "%s %s (index=%d)", i
->name
, (i
->flags
& IF_UP
) ? "up" : "DOWN", i
->index
);
768 if (!(i
->flags
& IF_MULTIACCESS
))
771 type
= "MultiAccess";
772 cli_msg(-1004, "\t%s%s%s Admin%s Link%s%s%s MTU=%d",
774 (i
->flags
& IF_BROADCAST
) ? " Broadcast" : "",
775 (i
->flags
& IF_MULTICAST
) ? " Multicast" : "",
776 (i
->flags
& IF_ADMIN_UP
) ? "Up" : "Down",
777 (i
->flags
& IF_LINK_UP
) ? "Up" : "Down",
778 (i
->flags
& IF_LOOPBACK
) ? " Loopback" : "",
779 (i
->flags
& IF_IGNORE
) ? " Ignored" : "",
782 if_show_addr(i
->addr
);
783 WALK_LIST(a
, i
->addrs
)
791 if_show_summary(void)
794 byte addr
[IPA_MAX_TEXT_LENGTH
+ 16];
796 cli_msg(-2005, "interface state address");
797 WALK_LIST(i
, iface_list
)
800 bsprintf(addr
, "%I/%d", i
->addr
->ip
, i
->addr
->prefix
.pxlen
);
803 cli_msg(-1005, "%-9s %-5s %s", i
->name
, (i
->flags
& IF_UP
) ? "up" : "DOWN", addr
);