3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "lib_errors.h"
31 #include "sockunion.h"
38 DEFINE_MTYPE(LIB
, IF
, "Interface")
39 DEFINE_MTYPE_STATIC(LIB
, CONNECTED
, "Connected")
40 DEFINE_MTYPE_STATIC(LIB
, NBR_CONNECTED
, "Neighbor Connected")
41 DEFINE_MTYPE(LIB
, CONNECTED_LABEL
, "Connected interface label")
42 DEFINE_MTYPE_STATIC(LIB
, IF_LINK_PARAMS
, "Informational Link Parameters")
44 static int if_cmp_func(const struct interface
*, const struct interface
*);
45 static int if_cmp_index_func(const struct interface
*ifp1
,
46 const struct interface
*ifp2
);
47 RB_GENERATE(if_name_head
, interface
, name_entry
, if_cmp_func
);
48 RB_GENERATE(if_index_head
, interface
, index_entry
, if_cmp_index_func
);
50 DEFINE_QOBJ_TYPE(interface
)
52 DEFINE_HOOK(if_add
, (struct interface
* ifp
), (ifp
))
53 DEFINE_KOOH(if_del
, (struct interface
* ifp
), (ifp
))
55 /* List of interfaces in only the default VRF */
58 /* Compare interface names, returning an integer greater than, equal to, or
59 * less than 0, (following the strcmp convention), according to the
60 * relationship between ifp1 and ifp2. Interface names consist of an
61 * alphabetic prefix and a numeric suffix. The primary sort key is
62 * lexicographic by name, and then numeric by number. No number sorts
63 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
66 int if_cmp_name_func(const char *p1
, const char *p2
)
73 /* look up to any number */
74 l1
= strcspn(p1
, "0123456789");
75 l2
= strcspn(p2
, "0123456789");
77 /* name lengths are different -> compare names */
79 return (strcmp(p1
, p2
));
81 /* Note that this relies on all numbers being less than all
85 res
= strncmp(p1
, p2
, l1
);
87 /* names are different -> compare them */
91 /* with identical name part, go to numeric part */
102 x1
= strtol(p1
, (char **)&p1
, 10);
103 x2
= strtol(p2
, (char **)&p2
, 10);
105 /* let's compare numbers now */
111 /* numbers were equal, lets do it again..
112 (it happens with name like "eth123.456:789") */
121 static int if_cmp_func(const struct interface
*ifp1
,
122 const struct interface
*ifp2
)
124 return if_cmp_name_func(ifp1
->name
, ifp2
->name
);
127 static int if_cmp_index_func(const struct interface
*ifp1
,
128 const struct interface
*ifp2
)
130 return ifp1
->ifindex
- ifp2
->ifindex
;
133 /* Create new interface structure. */
134 struct interface
*if_create(const char *name
, vrf_id_t vrf_id
)
136 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
137 struct interface
*ifp
;
139 ifp
= XCALLOC(MTYPE_IF
, sizeof(struct interface
));
140 ifp
->ifindex
= IFINDEX_INTERNAL
;
143 strlcpy(ifp
->name
, name
, sizeof(ifp
->name
));
144 ifp
->vrf_id
= vrf_id
;
145 IFNAME_RB_INSERT(vrf
, ifp
);
146 ifp
->connected
= list_new();
147 ifp
->connected
->del
= (void (*)(void *))connected_free
;
149 ifp
->nbr_connected
= list_new();
150 ifp
->nbr_connected
->del
= (void (*)(void *))nbr_connected_free
;
152 /* Enable Link-detection by default */
153 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
155 QOBJ_REG(ifp
, interface
);
156 hook_call(if_add
, ifp
);
160 /* Create new interface structure. */
161 void if_update_to_new_vrf(struct interface
*ifp
, vrf_id_t vrf_id
)
165 /* remove interface from old master vrf list */
166 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
168 IFNAME_RB_REMOVE(vrf
, ifp
);
169 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
170 IFINDEX_RB_REMOVE(vrf
, ifp
);
173 ifp
->vrf_id
= vrf_id
;
174 vrf
= vrf_get(ifp
->vrf_id
, NULL
);
176 IFNAME_RB_INSERT(vrf
, ifp
);
177 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
178 IFINDEX_RB_INSERT(vrf
, ifp
);
182 /* Delete interface structure. */
183 void if_delete_retain(struct interface
*ifp
)
185 hook_call(if_del
, ifp
);
188 /* Free connected address list */
189 list_delete_all_node(ifp
->connected
);
191 /* Free connected nbr address list */
192 list_delete_all_node(ifp
->nbr_connected
);
195 /* Delete and free interface structure. */
196 void if_delete(struct interface
*ifp
)
200 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
203 IFNAME_RB_REMOVE(vrf
, ifp
);
204 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
205 IFINDEX_RB_REMOVE(vrf
, ifp
);
207 if_delete_retain(ifp
);
209 list_delete_and_null(&ifp
->connected
);
210 list_delete_and_null(&ifp
->nbr_connected
);
212 if_link_params_free(ifp
);
215 XFREE(MTYPE_TMP
, ifp
->desc
);
217 XFREE(MTYPE_IF
, ifp
);
220 /* Interface existance check by index. */
221 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
224 struct interface if_tmp
;
226 vrf
= vrf_lookup_by_id(vrf_id
);
230 if_tmp
.ifindex
= ifindex
;
231 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
234 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
236 struct interface
*ifp
;
238 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
243 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
245 struct interface
*ifp
;
247 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
252 /* Interface existance check by interface name. */
253 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
255 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
256 struct interface if_tmp
;
259 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
262 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
263 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
266 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
269 struct interface
*ifp
;
271 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
274 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
275 ifp
= if_lookup_by_name(name
, vrf
->vrf_id
);
283 /* Lookup interface by IPv4 address. */
284 struct interface
*if_lookup_exact_address(void *src
, int family
,
287 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
288 struct listnode
*cnode
;
289 struct interface
*ifp
;
293 FOR_ALL_INTERFACES (vrf
, ifp
) {
294 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
297 if (p
&& (p
->family
== family
)) {
298 if (family
== AF_INET
) {
301 (struct in_addr
*)src
))
303 } else if (family
== AF_INET6
) {
306 (struct in6_addr
*)src
))
315 /* Lookup interface by IPv4 address. */
316 struct connected
*if_lookup_address(void *matchaddr
, int family
,
319 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
322 struct listnode
*cnode
;
323 struct interface
*ifp
;
325 struct connected
*match
;
327 if (family
== AF_INET
) {
328 addr
.family
= AF_INET
;
329 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
330 addr
.prefixlen
= IPV4_MAX_BITLEN
;
331 } else if (family
== AF_INET6
) {
332 addr
.family
= AF_INET6
;
333 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
334 addr
.prefixlen
= IPV6_MAX_BITLEN
;
339 FOR_ALL_INTERFACES (vrf
, ifp
) {
340 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
341 if (c
->address
&& (c
->address
->family
== AF_INET
)
342 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
343 && (c
->address
->prefixlen
> bestlen
)) {
344 bestlen
= c
->address
->prefixlen
;
352 /* Lookup interface by prefix */
353 struct interface
*if_lookup_prefix(struct prefix
*prefix
, vrf_id_t vrf_id
)
355 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
356 struct listnode
*cnode
;
357 struct interface
*ifp
;
360 FOR_ALL_INTERFACES (vrf
, ifp
) {
361 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
362 if (prefix_cmp(c
->address
, prefix
) == 0) {
370 /* Get interface by name if given name interface doesn't exist create
372 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
, int vty
)
374 struct interface
*ifp
= NULL
;
376 if (vrf_is_mapped_on_netns(vrf_lookup_by_id(vrf_id
))) {
377 ifp
= if_lookup_by_name(name
, vrf_id
);
381 /* If the interface command was entered in vty without a
382 * VRF (passed as VRF_DEFAULT), search an interface with
383 * this name in all VRs
385 if (vrf_id
== VRF_DEFAULT
)
386 return if_lookup_by_name_all_vrf(name
);
389 return if_create(name
, vrf_id
);
391 /* vrf is based on vrf-lite */
392 ifp
= if_lookup_by_name_all_vrf(name
);
394 if (ifp
->vrf_id
== vrf_id
)
396 /* Found a match on a different VRF. If the interface command
397 * was entered in vty without a VRF (passed as VRF_DEFAULT),
398 * accept the ifp we found. If a vrf was entered and there is a
399 * mismatch, reject it if from vty. If it came from the kernel
400 * or by way of zclient, believe it and update the ifp
404 if (vrf_id
== VRF_DEFAULT
)
408 /* If it came from the kernel or by way of zclient, believe it
409 * and update the ifp accordingly.
411 if_update_to_new_vrf(ifp
, vrf_id
);
414 return if_create(name
, vrf_id
);
417 void if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
421 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
424 if (ifp
->ifindex
== ifindex
)
427 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
428 IFINDEX_RB_REMOVE(vrf
, ifp
)
430 ifp
->ifindex
= ifindex
;
432 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
433 IFINDEX_RB_INSERT(vrf
, ifp
)
436 /* Does interface up ? */
437 int if_is_up(struct interface
*ifp
)
439 return ifp
->flags
& IFF_UP
;
442 /* Is interface running? */
443 int if_is_running(struct interface
*ifp
)
445 return ifp
->flags
& IFF_RUNNING
;
448 /* Is the interface operative, eg. either UP & RUNNING
449 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
450 if ptm checking is enabled, then ptm check has passed */
451 int if_is_operative(struct interface
*ifp
)
453 return ((ifp
->flags
& IFF_UP
)
454 && (((ifp
->flags
& IFF_RUNNING
)
455 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
456 || !CHECK_FLAG(ifp
->status
,
457 ZEBRA_INTERFACE_LINKDETECTION
)));
460 /* Is the interface operative, eg. either UP & RUNNING
461 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
462 int if_is_no_ptm_operative(struct interface
*ifp
)
464 return ((ifp
->flags
& IFF_UP
)
465 && ((ifp
->flags
& IFF_RUNNING
)
466 || !CHECK_FLAG(ifp
->status
,
467 ZEBRA_INTERFACE_LINKDETECTION
)));
470 /* Is this loopback interface ? */
471 int if_is_loopback(struct interface
*ifp
)
473 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
474 * but Y on platform N?
476 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
479 /* Check interface is VRF */
480 int if_is_vrf(struct interface
*ifp
)
482 return CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
485 bool if_is_loopback_or_vrf(struct interface
*ifp
)
487 if (if_is_loopback(ifp
) || if_is_vrf(ifp
))
493 /* Does this interface support broadcast ? */
494 int if_is_broadcast(struct interface
*ifp
)
496 return ifp
->flags
& IFF_BROADCAST
;
499 /* Does this interface support broadcast ? */
500 int if_is_pointopoint(struct interface
*ifp
)
502 return ifp
->flags
& IFF_POINTOPOINT
;
505 /* Does this interface support multicast ? */
506 int if_is_multicast(struct interface
*ifp
)
508 return ifp
->flags
& IFF_MULTICAST
;
511 /* Printout flag information into log */
512 const char *if_flag_dump(unsigned long flag
)
515 static char logbuf
[BUFSIZ
];
517 #define IFF_OUT_LOG(X, STR) \
520 strlcat(logbuf, ",", BUFSIZ); \
523 strlcat(logbuf, STR, BUFSIZ); \
526 strlcpy(logbuf
, "<", BUFSIZ
);
527 IFF_OUT_LOG(IFF_UP
, "UP");
528 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
529 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
530 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
531 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
532 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
533 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
534 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
535 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
536 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
537 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
538 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
539 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
540 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
541 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
542 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
543 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
544 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
545 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
546 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
547 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
549 strlcat(logbuf
, ">", BUFSIZ
);
556 static void if_dump(const struct interface
*ifp
)
558 struct listnode
*node
;
559 struct connected
*c
__attribute__((unused
));
561 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
563 "Interface %s vrf %u index %d metric %d mtu %d "
565 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
,
566 ifp
->mtu
, ifp
->mtu6
, if_flag_dump(ifp
->flags
));
569 /* Interface printing for all interface. */
570 void if_dump_all(void)
575 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
576 FOR_ALL_INTERFACES (vrf
, ifp
)
580 DEFUN (interface_desc
,
582 "description LINE...",
583 "Interface specific description\n"
584 "Characters describing this interface\n")
587 VTY_DECLVAR_CONTEXT(interface
, ifp
);
590 XFREE(MTYPE_TMP
, ifp
->desc
);
591 ifp
->desc
= argv_concat(argv
, argc
, idx_line
);
596 DEFUN (no_interface_desc
,
597 no_interface_desc_cmd
,
600 "Interface specific description\n")
602 VTY_DECLVAR_CONTEXT(interface
, ifp
);
605 XFREE(MTYPE_TMP
, ifp
->desc
);
612 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
613 * a seperate struct interface for each logical interface, so config
614 * file may be full of 'interface fooX:Y'. Solaris however does not
615 * expose logical interfaces via PF_ROUTE, so trying to track logical
616 * interfaces can be fruitless, for that reason Quagga only tracks
617 * the primary IP interface.
619 * We try accomodate SUNWzebra by:
620 * - looking up the interface name, to see whether it exists, if so
622 * - for protocol daemons, this could only because zebra told us of
624 * - for zebra, only because it learnt from kernel
626 * - search the name to see if it contains a sub-ipif / logical interface
627 * seperator, the ':' char. If it does:
628 * - text up to that char must be the primary name - get that name.
630 * - no idea, just get the name in its entirety.
632 static struct interface
*if_sunwzebra_get(const char *name
, vrf_id_t vrf_id
)
634 struct interface
*ifp
;
637 if ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
640 /* hunt the primary interface name... */
641 cp
= strchr(name
, ':');
645 return if_get_by_name(name
, vrf_id
, 1);
649 DEFUN_NOSH (interface
,
651 "interface IFNAME [vrf NAME]",
652 "Select an interface to configure\n"
658 const char *ifname
= argv
[idx_ifname
]->arg
;
659 const char *vrfname
=
660 (argc
> 2) ? argv
[idx_vrf
]->arg
: VRF_DEFAULT_NAME
;
662 struct interface
*ifp
;
663 vrf_id_t vrf_id
= VRF_DEFAULT
;
665 if (strlen(ifname
) > INTERFACE_NAMSIZ
) {
667 "%% Interface name %s is invalid: length exceeds "
669 ifname
, INTERFACE_NAMSIZ
);
670 return CMD_WARNING_CONFIG_FAILED
;
673 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
674 Imagine forward reference of a vrf by name in this interface config */
676 VRF_GET_ID(vrf_id
, vrfname
, false);
679 ifp
= if_sunwzebra_get(ifname
, vrf_id
);
681 ifp
= if_get_by_name(ifname
, vrf_id
, 1);
685 vty_out(vty
, "%% interface %s not in %s vrf\n", ifname
,
687 return CMD_WARNING_CONFIG_FAILED
;
689 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
696 "no interface IFNAME [vrf NAME]",
698 "Delete a pseudo interface's configuration\n"
703 const char *ifname
= argv
[2]->arg
;
704 const char *vrfname
= (argc
> 3) ? argv
[idx_vrf
]->arg
: NULL
;
706 // deleting interface
707 struct interface
*ifp
;
708 vrf_id_t vrf_id
= VRF_DEFAULT
;
711 VRF_GET_ID(vrf_id
, vrfname
, false);
713 ifp
= if_lookup_by_name(ifname
, vrf_id
);
716 vty_out(vty
, "%% Interface %s does not exist\n", ifname
);
717 return CMD_WARNING_CONFIG_FAILED
;
720 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
721 vty_out(vty
, "%% Only inactive interfaces can be deleted\n");
722 return CMD_WARNING_CONFIG_FAILED
;
730 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
732 struct interface
*ifp
;
733 struct vrf
*vrf
= NULL
;
735 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
736 FOR_ALL_INTERFACES (vrf
, ifp
) {
737 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
742 static const struct cmd_variable_handler if_var_handlers
[] = {
743 {/* "interface NAME" */
744 .varname
= "interface",
745 .completions
= if_autocomplete
},
746 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
747 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
748 {.completions
= NULL
}};
750 void if_cmd_init(void)
752 cmd_variable_handler_register(if_var_handlers
);
754 install_element(CONFIG_NODE
, &interface_cmd
);
755 install_element(CONFIG_NODE
, &no_interface_cmd
);
757 install_default(INTERFACE_NODE
);
758 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
759 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
763 /* For debug purpose. */
766 "show address [vrf NAME]",
772 struct listnode
*node
;
773 struct interface
*ifp
;
774 struct connected
*ifc
;
776 vrf_id_t vrf_id
= VRF_DEFAULT
;
779 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
781 FOR_ALL_INTERFACES (vrf
, ifp
)
783 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
787 if (p
->family
== AF_INET
)
788 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
794 DEFUN (show_address_vrf_all
,
795 show_address_vrf_all_cmd
,
796 "show address vrf all",
799 VRF_ALL_CMD_HELP_STR
)
802 struct listnode
*node
;
803 struct interface
*ifp
;
804 struct connected
*ifc
;
807 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
809 if (RB_EMPTY (if_name_head
, &vrf
->ifaces_by_name
))
812 vty_out (vty
, "\nVRF %u\n\n", vrf
->vrf_id
);
814 FOR_ALL_INTERFACES (vrf
, ifp
)
816 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
820 if (p
->family
== AF_INET
)
821 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
829 /* Allocate connected structure. */
830 struct connected
*connected_new(void)
832 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
835 /* Allocate nbr connected structure. */
836 struct nbr_connected
*nbr_connected_new(void)
838 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
841 /* Free connected structure. */
842 void connected_free(struct connected
*connected
)
844 if (connected
->address
)
845 prefix_free(connected
->address
);
847 if (connected
->destination
)
848 prefix_free(connected
->destination
);
850 if (connected
->label
)
851 XFREE(MTYPE_CONNECTED_LABEL
, connected
->label
);
853 XFREE(MTYPE_CONNECTED
, connected
);
856 /* Free nbr connected structure. */
857 void nbr_connected_free(struct nbr_connected
*connected
)
859 if (connected
->address
)
860 prefix_free(connected
->address
);
862 XFREE(MTYPE_NBR_CONNECTED
, connected
);
865 /* If same interface nbr address already exists... */
866 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
869 struct nbr_connected
*ifc
;
870 struct listnode
*node
;
872 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
873 if (prefix_same(ifc
->address
, p
))
879 /* Print if_addr structure. */
880 static void __attribute__((unused
))
881 connected_log(struct connected
*connected
, char *str
)
884 struct interface
*ifp
;
888 ifp
= connected
->ifp
;
889 p
= connected
->address
;
891 snprintf(logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ", str
,
892 ifp
->name
, ifp
->vrf_id
, prefix_family_str(p
),
893 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
895 p
= connected
->destination
;
897 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
898 BUFSIZ
- strlen(logbuf
));
900 zlog_info("%s", logbuf
);
903 /* Print if_addr structure. */
904 static void __attribute__((unused
))
905 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
908 struct interface
*ifp
;
912 ifp
= connected
->ifp
;
913 p
= connected
->address
;
915 snprintf(logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ", str
, ifp
->name
,
916 prefix_family_str(p
),
917 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
919 zlog_info("%s", logbuf
);
922 /* If two connected address has same prefix return 1. */
923 static int connected_same_prefix(struct prefix
*p1
, struct prefix
*p2
)
925 if (p1
->family
== p2
->family
) {
926 if (p1
->family
== AF_INET
927 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
929 if (p1
->family
== AF_INET6
930 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
936 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
939 struct listnode
*node
;
940 struct listnode
*next
;
941 struct connected
*ifc
;
943 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
944 ifc
= listgetdata(node
);
947 if (connected_same_prefix(ifc
->address
, p
))
953 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
956 struct listnode
*node
;
957 struct listnode
*next
;
958 struct connected
*ifc
;
960 /* In case of same prefix come, replace it with new one. */
961 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
962 ifc
= listgetdata(node
);
965 if (connected_same_prefix(ifc
->address
, p
)) {
966 listnode_delete(ifp
->connected
, ifc
);
973 /* Find the address on our side that will be used when packets
975 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
978 struct listnode
*cnode
;
980 struct connected
*match
;
984 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
985 if (c
->address
&& (c
->address
->family
== addr
->family
)
986 && prefix_match(CONNECTED_PREFIX(c
), addr
)
988 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
994 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
996 struct prefix
*destination
)
998 struct connected
*ifc
;
1000 /* Allocate new connected address. */
1001 ifc
= connected_new();
1004 /* Fetch interface address */
1005 ifc
->address
= prefix_new();
1006 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
1008 /* Fetch dest address */
1010 ifc
->destination
= prefix_new();
1011 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
1014 /* Add connected address to the interface. */
1015 listnode_add(ifp
->connected
, ifc
);
1019 #if 0 /* this route_table of struct connected's is unused \
1020 * however, it would be good to use a route_table rather than \
1023 /* Interface looking up by interface's address. */
1024 /* Interface's IPv4 address reverse lookup table. */
1025 struct route_table
*ifaddr_ipv4_table
;
1026 /* struct route_table *ifaddr_ipv6_table; */
1029 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
1031 struct route_node
*rn
;
1032 struct prefix_ipv4 p
;
1035 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1038 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1041 route_unlock_node (rn
);
1042 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1043 inet_ntoa (*ifaddr
));
1050 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1052 struct route_node
*rn
;
1053 struct prefix_ipv4 p
;
1056 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1059 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1062 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1063 inet_ntoa (*ifaddr
));
1067 route_unlock_node (rn
);
1068 route_unlock_node (rn
);
1071 /* Lookup interface by interface's IP address or interface index. */
1072 static struct interface
*
1073 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1075 struct prefix_ipv4 p
;
1076 struct route_node
*rn
;
1077 struct interface
*ifp
;
1082 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1085 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1090 route_unlock_node (rn
);
1094 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1096 #endif /* ifaddr_ipv4_table */
1098 void if_terminate(struct vrf
*vrf
)
1100 struct interface
*ifp
;
1102 while (!RB_EMPTY(if_name_head
, &vrf
->ifaces_by_name
)) {
1103 ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
);
1106 ifp
->node
->info
= NULL
;
1107 route_unlock_node(ifp
->node
);
1113 const char *if_link_type_str(enum zebra_link_type llt
)
1116 #define llts(T,S) case (T): return (S)
1117 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1118 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1119 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1120 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1121 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1122 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1123 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1124 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1125 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1126 llts(ZEBRA_LLT_ATM
, "ATM");
1127 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1128 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1129 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1130 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1131 llts(ZEBRA_LLT_SLIP
, "SLIP");
1132 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1133 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1134 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1135 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1136 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1137 llts(ZEBRA_LLT_PPP
, "PPP");
1138 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1139 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1140 llts(ZEBRA_LLT_LAPB
, "LAPB");
1141 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1142 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1143 llts(ZEBRA_LLT_FRAD
, "FRAD");
1144 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1145 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1146 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1147 llts(ZEBRA_LLT_FDDI
, "FDDI");
1148 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1149 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1150 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1151 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1152 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1153 llts(ZEBRA_LLT_IRDA
, "IrDA");
1154 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1155 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1156 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1157 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1158 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1159 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1160 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1161 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1162 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1164 flog_err(EC_LIB_DEVELOPMENT
, "Unknown value %d", llt
);
1165 return "Unknown type!";
1171 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1175 if (ifp
->link_params
!= NULL
)
1176 return ifp
->link_params
;
1178 struct if_link_params
*iflp
=
1179 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1183 /* Set TE metric equal to standard metric */
1184 iflp
->te_metric
= ifp
->metric
;
1186 /* Compute default bandwidth based on interface */
1188 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1189 * TE_KILO_BIT
/ TE_BYTE
);
1191 /* Set Max, Reservable and Unreserved Bandwidth */
1192 iflp
->max_bw
= iflp
->default_bw
;
1193 iflp
->max_rsv_bw
= iflp
->default_bw
;
1194 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1195 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1197 /* Update Link parameters status */
1199 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1201 /* Finally attach newly created Link Parameters */
1202 ifp
->link_params
= iflp
;
1207 void if_link_params_free(struct interface
*ifp
)
1209 if (ifp
->link_params
== NULL
)
1211 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1212 ifp
->link_params
= NULL
;