]>
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 %I/%-2d bc %I -> %I%s%s%s\n", a
->ip
, a
->prefix
, a
->pxlen
, 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
)
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
);
159 WALK_LIST(p
, active_proto_list
)
160 ifa_send_notify(p
, c
, a
);
164 ifa_notify_change(unsigned c
, struct ifa
*a
)
166 if (c
& IF_CHANGE_DOWN
)
169 ifa_notify_change_(c
, a
);
171 if (c
& IF_CHANGE_UP
)
176 if_send_notify(struct proto
*p
, unsigned c
, struct iface
*i
)
180 if (p
->debug
& D_IFACES
)
181 log(L_TRACE
"%s < interface %s %s", p
->name
, i
->name
,
182 (c
& IF_CHANGE_UP
) ? "goes up" :
183 (c
& IF_CHANGE_DOWN
) ? "goes down" :
184 (c
& IF_CHANGE_MTU
) ? "changes MTU" :
185 (c
& IF_CHANGE_LINK
) ? "changes link" :
186 (c
& IF_CHANGE_CREATE
) ? "created" :
187 "sends unknown event");
188 p
->if_notify(p
, c
, i
);
193 if_notify_change(unsigned c
, struct iface
*i
)
198 if (i
->flags
& IF_JUST_CREATED
)
200 i
->flags
&= ~IF_JUST_CREATED
;
201 c
|= IF_CHANGE_CREATE
| IF_CHANGE_MTU
;
204 DBG("Interface change notification (%x) for %s\n", c
, i
->name
);
209 if (c
& IF_CHANGE_DOWN
)
212 if (c
& IF_CHANGE_DOWN
)
213 WALK_LIST(a
, i
->addrs
)
215 a
->flags
= (i
->flags
& ~IA_FLAGS
) | (a
->flags
& IA_FLAGS
);
216 ifa_notify_change_(IF_CHANGE_DOWN
, a
);
219 WALK_LIST(p
, active_proto_list
)
220 if_send_notify(p
, c
, i
);
222 if (c
& IF_CHANGE_UP
)
223 WALK_LIST(a
, i
->addrs
)
225 a
->flags
= (i
->flags
& ~IA_FLAGS
) | (a
->flags
& IA_FLAGS
);
226 ifa_notify_change_(IF_CHANGE_UP
, a
);
229 if (c
& IF_CHANGE_UP
)
232 if ((c
& (IF_CHANGE_UP
| IF_CHANGE_DOWN
| IF_CHANGE_LINK
)) == IF_CHANGE_LINK
)
237 if_recalc_flags(struct iface
*i
, unsigned flags
)
239 if ((flags
& (IF_SHUTDOWN
| IF_TMP_DOWN
)) ||
240 !(flags
& IF_ADMIN_UP
) ||
249 if_change_flags(struct iface
*i
, unsigned flags
)
251 unsigned of
= i
->flags
;
253 i
->flags
= if_recalc_flags(i
, flags
);
254 if ((i
->flags
^ of
) & IF_UP
)
255 if_notify_change((i
->flags
& IF_UP
) ? IF_CHANGE_UP
: IF_CHANGE_DOWN
, i
);
259 * if_delete - remove interface
262 * This function is called by the low-level platform dependent code
263 * whenever it notices an interface disappears. It is just a shorthand
268 if_delete(struct iface
*old
)
271 strncpy(f
.name
, old
->name
, sizeof(f
.name
)-1);
272 f
.flags
= IF_SHUTDOWN
;
277 * if_update - update interface status
278 * @new: new interface status
280 * if_update() is called by the low-level platform dependent code
281 * whenever it notices an interface change.
283 * There exist two types of interface updates -- synchronous and asynchronous
284 * ones. In the synchronous case, the low-level code calls if_start_update(),
285 * scans all interfaces reported by the OS, uses if_update() and ifa_update()
286 * to pass them to the core and then it finishes the update sequence by
287 * calling if_end_update(). When working asynchronously, the sysdep code
288 * calls if_update() and ifa_update() whenever it notices a change.
290 * if_update() will automatically notify all other modules about the change.
293 if_update(struct iface
*new)
298 WALK_LIST(i
, iface_list
)
299 if (!strcmp(new->name
, i
->name
))
302 new->flags
= if_recalc_flags(new, new->flags
);
303 c
= if_what_changed(i
, new);
304 if (c
& IF_CHANGE_TOO_MUCH
) /* Changed a lot, convert it to down/up */
306 DBG("Interface %s changed too much -- forcing down/up transition\n", i
->name
);
307 if_change_flags(i
, i
->flags
| IF_TMP_DOWN
);
310 memcpy(&new->addrs
, &i
->addrs
, sizeof(i
->addrs
));
311 memcpy(i
, new, sizeof(*i
));
312 i
->flags
&= ~IF_UP
; /* IF_TMP_DOWN will be added later */
318 if_notify_change(c
, i
);
320 i
->flags
|= IF_UPDATED
;
323 i
= mb_alloc(if_pool
, sizeof(struct iface
));
324 memcpy(i
, new, sizeof(*i
));
325 init_list(&i
->addrs
);
327 init_list(&i
->neighbors
);
328 i
->flags
|= IF_UPDATED
| IF_TMP_DOWN
; /* Tmp down as we don't have addresses yet */
329 add_tail(&iface_list
, &i
->n
);
334 if_start_update(void)
339 WALK_LIST(i
, iface_list
)
341 i
->flags
&= ~IF_UPDATED
;
342 WALK_LIST(a
, i
->addrs
)
343 a
->flags
&= ~IF_UPDATED
;
348 if_end_partial_update(struct iface
*i
)
350 if (i
->flags
& IF_TMP_DOWN
)
351 if_change_flags(i
, i
->flags
& ~IF_TMP_DOWN
);
360 WALK_LIST(i
, iface_list
)
362 if (!(i
->flags
& IF_UPDATED
))
363 if_change_flags(i
, (i
->flags
& ~IF_ADMIN_UP
) | IF_SHUTDOWN
);
366 WALK_LIST_DELSAFE(a
, b
, i
->addrs
)
367 if (!(a
->flags
& IF_UPDATED
))
369 if_end_partial_update(i
);
375 if_flush_ifaces(struct proto
*p
)
377 if (p
->debug
& D_EVENTS
)
378 log(L_TRACE
"%s: Flushing interfaces", p
->name
);
384 * if_feed_baby - advertise interfaces to a new protocol
385 * @p: protocol to feed
387 * When a new protocol starts, this function sends it a series
388 * of notifications about all existing interfaces.
391 if_feed_baby(struct proto
*p
)
396 if (!p
->if_notify
&& !p
->ifa_notify
) /* shortcut */
398 DBG("Announcing interfaces to new protocol %s\n", p
->name
);
399 WALK_LIST(i
, iface_list
)
401 if_send_notify(p
, IF_CHANGE_CREATE
| ((i
->flags
& IF_UP
) ? IF_CHANGE_UP
: 0), i
);
402 if (i
->flags
& IF_UP
)
403 WALK_LIST(a
, i
->addrs
)
404 ifa_send_notify(p
, IF_CHANGE_CREATE
| IF_CHANGE_UP
, a
);
409 * if_find_by_index - find interface by ifindex
412 * This function finds an &iface structure corresponding to an interface
413 * of the given index @idx. Returns a pointer to the structure or %NULL
414 * if no such structure exists.
417 if_find_by_index(unsigned idx
)
421 WALK_LIST(i
, iface_list
)
422 if (i
->index
== idx
&& !(i
->flags
& IF_SHUTDOWN
))
428 * if_find_by_name - find interface by name
429 * @name: interface name
431 * This function finds an &iface structure corresponding to an interface
432 * of the given name @name. Returns a pointer to the structure or %NULL
433 * if no such structure exists.
436 if_find_by_name(char *name
)
440 WALK_LIST(i
, iface_list
)
441 if (!strcmp(i
->name
, name
))
447 if_get_by_name(char *name
)
451 if (i
= if_find_by_name(name
))
454 /* No active iface, create a dummy */
455 i
= mb_allocz(if_pool
, sizeof(struct iface
));
456 strncpy(i
->name
, name
, sizeof(i
->name
)-1);
457 i
->flags
= IF_SHUTDOWN
;
458 init_list(&i
->addrs
);
459 init_list(&i
->neighbors
);
460 add_tail(&iface_list
, &i
->n
);
464 struct ifa
*kif_choose_primary(struct iface
*i
);
467 ifa_recalc_primary(struct iface
*i
)
469 struct ifa
*a
= kif_choose_primary(i
);
475 i
->addr
->flags
&= ~IA_PRIMARY
;
479 a
->flags
|= IA_PRIMARY
;
481 add_head(&i
->addrs
, &a
->n
);
489 ifa_recalc_all_primary_addresses(void)
493 WALK_LIST(i
, iface_list
)
495 if (ifa_recalc_primary(i
))
496 if_change_flags(i
, i
->flags
| IF_TMP_DOWN
);
501 ifa_same(struct ifa
*a
, struct ifa
*b
)
503 return ipa_equal(a
->ip
, b
->ip
) && ipa_equal(a
->prefix
, b
->prefix
) &&
504 a
->pxlen
== b
->pxlen
;
509 * ifa_update - update interface address
510 * @a: new interface address
512 * This function adds address information to a network
513 * interface. It's called by the platform dependent code during
514 * the interface update process described under if_update().
517 ifa_update(struct ifa
*a
)
519 struct iface
*i
= a
->iface
;
522 WALK_LIST(b
, i
->addrs
)
525 if (ipa_equal(b
->brd
, a
->brd
) &&
526 ipa_equal(b
->opposite
, a
->opposite
) &&
527 b
->scope
== a
->scope
&&
528 !((b
->flags
^ a
->flags
) & IA_PEER
))
530 b
->flags
|= IF_UPDATED
;
538 if ((i
->flags
& IF_BROADCAST
) && !ipa_nonzero(a
->brd
))
539 log(L_ERR
"Missing broadcast address for interface %s", i
->name
);
542 b
= mb_alloc(if_pool
, sizeof(struct ifa
));
543 memcpy(b
, a
, sizeof(struct ifa
));
544 add_tail(&i
->addrs
, &b
->n
);
545 b
->flags
= (i
->flags
& ~IA_FLAGS
) | (a
->flags
& IA_FLAGS
);
546 if (ifa_recalc_primary(i
))
547 if_change_flags(i
, i
->flags
| IF_TMP_DOWN
);
548 if (b
->flags
& IF_UP
)
549 ifa_notify_change(IF_CHANGE_CREATE
| IF_CHANGE_UP
, b
);
554 * ifa_delete - remove interface address
555 * @a: interface address
557 * This function removes address information from a network
558 * interface. It's called by the platform dependent code during
559 * the interface update process described under if_update().
562 ifa_delete(struct ifa
*a
)
564 struct iface
*i
= a
->iface
;
567 WALK_LIST(b
, i
->addrs
)
571 if (b
->flags
& IF_UP
)
574 ifa_notify_change(IF_CHANGE_DOWN
, b
);
576 if (b
->flags
& IA_PRIMARY
)
578 if_change_flags(i
, i
->flags
| IF_TMP_DOWN
);
579 ifa_recalc_primary(i
);
587 if_choose_router_id(struct iface_patt
*mask
, u32 old_id
)
594 WALK_LIST(i
, iface_list
)
596 if (!(i
->flags
& IF_ADMIN_UP
) ||
597 (i
->flags
& IF_SHUTDOWN
))
600 WALK_LIST(a
, i
->addrs
)
602 if (a
->flags
& IA_SECONDARY
)
605 if (a
->scope
<= SCOPE_LINK
)
608 /* Check pattern if specified */
609 if (mask
&& !iface_patt_match(mask
, i
, a
))
612 /* No pattern or pattern matched */
613 if (!b
|| ipa_to_u32(a
->ip
) < ipa_to_u32(b
->ip
))
621 u32 id
= ipa_to_u32(b
->ip
);
623 log(L_INFO
"Chosen router ID %R according to interface %s", id
, b
->iface
->name
);
633 * if_init - initialize interface module
635 * This function is called during BIRD startup to initialize
636 * all data structures of the interface module.
641 if_pool
= rp_new(&root_pool
, "Interfaces");
642 init_list(&iface_list
);
647 * Interface Pattern Lists
651 iface_patt_match(struct iface_patt
*ifp
, struct iface
*i
, struct ifa
*a
)
653 struct iface_patt_node
*p
;
655 WALK_LIST(p
, ifp
->ipn_list
)
657 char *t
= p
->pattern
;
658 int pos
= p
->positive
;
668 if (!patmatch(t
, i
->name
))
678 if (ipa_in_net(a
->ip
, p
->prefix
, p
->pxlen
))
681 if ((a
->flags
& IA_PEER
) &&
682 ipa_in_net(a
->opposite
, p
->prefix
, p
->pxlen
))
692 iface_patt_find(list
*l
, struct iface
*i
, struct ifa
*a
)
694 struct iface_patt
*p
;
697 if (iface_patt_match(p
, i
, a
))
704 iface_plists_equal(struct iface_patt
*pa
, struct iface_patt
*pb
)
706 struct iface_patt_node
*x
, *y
;
708 x
= HEAD(pa
->ipn_list
);
709 y
= HEAD(pb
->ipn_list
);
710 while (x
->n
.next
&& y
->n
.next
)
712 if ((x
->positive
!= y
->positive
) ||
713 (!x
->pattern
&& y
->pattern
) || /* This nasty lines where written by me... :-( Feela */
714 (!y
->pattern
&& x
->pattern
) ||
715 ((x
->pattern
!= y
->pattern
) && strcmp(x
->pattern
, y
->pattern
)) ||
716 !ipa_equal(x
->prefix
, y
->prefix
) ||
717 (x
->pxlen
!= y
->pxlen
))
719 x
= (void *) x
->n
.next
;
720 y
= (void *) y
->n
.next
;
722 return (!x
->n
.next
&& !y
->n
.next
);
726 iface_patts_equal(list
*a
, list
*b
, int (*comp
)(struct iface_patt
*, struct iface_patt
*))
728 struct iface_patt
*x
, *y
;
732 while (x
->n
.next
&& y
->n
.next
)
734 if (!iface_plists_equal(x
, y
) ||
735 (comp
&& !comp(x
, y
)))
737 x
= (void *) x
->n
.next
;
738 y
= (void *) y
->n
.next
;
740 return (!x
->n
.next
&& !y
->n
.next
);
748 if_show_addr(struct ifa
*a
)
750 byte opp
[STD_ADDRESS_P_LENGTH
+ 16];
752 if (ipa_nonzero(a
->opposite
))
753 bsprintf(opp
, ", opposite %I", a
->opposite
);
756 cli_msg(-1003, "\t%I/%d (%s%s, scope %s)",
758 (a
->flags
& IA_PRIMARY
) ? "Primary" : (a
->flags
& IA_SECONDARY
) ? "Secondary" : "Unselected",
759 opp
, ip_scope_text(a
->scope
));
769 WALK_LIST(i
, iface_list
)
771 if (i
->flags
& IF_SHUTDOWN
)
774 cli_msg(-1001, "%s %s (index=%d)", i
->name
, (i
->flags
& IF_UP
) ? "up" : "DOWN", i
->index
);
775 if (!(i
->flags
& IF_MULTIACCESS
))
778 type
= "MultiAccess";
779 cli_msg(-1004, "\t%s%s%s Admin%s Link%s%s%s MTU=%d",
781 (i
->flags
& IF_BROADCAST
) ? " Broadcast" : "",
782 (i
->flags
& IF_MULTICAST
) ? " Multicast" : "",
783 (i
->flags
& IF_ADMIN_UP
) ? "Up" : "Down",
784 (i
->flags
& IF_LINK_UP
) ? "Up" : "Down",
785 (i
->flags
& IF_LOOPBACK
) ? " Loopback" : "",
786 (i
->flags
& IF_IGNORE
) ? " Ignored" : "",
789 if_show_addr(i
->addr
);
790 WALK_LIST(a
, i
->addrs
)
798 if_show_summary(void)
801 byte addr
[STD_ADDRESS_P_LENGTH
+ 16];
803 cli_msg(-2005, "interface state address");
804 WALK_LIST(i
, iface_list
)
807 bsprintf(addr
, "%I/%d", i
->addr
->ip
, i
->addr
->pxlen
);
810 cli_msg(-1005, "%-9s %-5s %s", i
->name
, (i
->flags
& IF_UP
) ? "up" : "DOWN", addr
);