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
30 #include "sockunion.h"
37 DEFINE_MTYPE(LIB
, IF
, "Interface")
38 DEFINE_MTYPE_STATIC(LIB
, CONNECTED
, "Connected")
39 DEFINE_MTYPE_STATIC(LIB
, NBR_CONNECTED
, "Neighbor Connected")
40 DEFINE_MTYPE(LIB
, CONNECTED_LABEL
, "Connected interface label")
41 DEFINE_MTYPE_STATIC(LIB
, IF_LINK_PARAMS
, "Informational Link Parameters")
43 static int if_cmp_func(const struct interface
*, const struct interface
*);
44 static int if_cmp_index_func(const struct interface
*ifp1
,
45 const struct interface
*ifp2
);
46 RB_GENERATE(if_name_head
, interface
, name_entry
, if_cmp_func
);
47 RB_GENERATE(if_index_head
, interface
, index_entry
, if_cmp_index_func
);
49 DEFINE_QOBJ_TYPE(interface
)
51 DEFINE_HOOK(if_add
, (struct interface
* ifp
), (ifp
))
52 DEFINE_KOOH(if_del
, (struct interface
* ifp
), (ifp
))
54 /* List of interfaces in only the default VRF */
57 /* Compare interface names, returning an integer greater than, equal to, or
58 * less than 0, (following the strcmp convention), according to the
59 * relationship between ifp1 and ifp2. Interface names consist of an
60 * alphabetic prefix and a numeric suffix. The primary sort key is
61 * lexicographic by name, and then numeric by number. No number sorts
62 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
65 int if_cmp_name_func(char *p1
, char *p2
)
72 /* look up to any number */
73 l1
= strcspn(p1
, "0123456789");
74 l2
= strcspn(p2
, "0123456789");
76 /* name lengths are different -> compare names */
78 return (strcmp(p1
, p2
));
80 /* Note that this relies on all numbers being less than all
84 res
= strncmp(p1
, p2
, l1
);
86 /* names are different -> compare them */
90 /* with identical name part, go to numeric part */
101 x1
= strtol(p1
, &p1
, 10);
102 x2
= strtol(p2
, &p2
, 10);
104 /* let's compare numbers now */
110 /* numbers were equal, lets do it again..
111 (it happens with name like "eth123.456:789") */
120 static int if_cmp_func(const struct interface
*ifp1
,
121 const struct interface
*ifp2
)
123 return if_cmp_name_func((char *)ifp1
->name
, (char *)ifp2
->name
);
126 static int if_cmp_index_func(const struct interface
*ifp1
,
127 const struct interface
*ifp2
)
129 return ifp1
->ifindex
- ifp2
->ifindex
;
132 /* Create new interface structure. */
133 struct interface
*if_create(const char *name
, vrf_id_t vrf_id
)
135 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
136 struct interface
*ifp
;
138 ifp
= XCALLOC(MTYPE_IF
, sizeof(struct interface
));
139 ifp
->ifindex
= IFINDEX_INTERNAL
;
142 strlcpy(ifp
->name
, name
, sizeof(ifp
->name
));
143 ifp
->vrf_id
= vrf_id
;
144 IFNAME_RB_INSERT(vrf
, ifp
);
145 ifp
->connected
= list_new();
146 ifp
->connected
->del
= (void (*)(void *))connected_free
;
148 ifp
->nbr_connected
= list_new();
149 ifp
->nbr_connected
->del
= (void (*)(void *))nbr_connected_free
;
151 /* Enable Link-detection by default */
152 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
154 QOBJ_REG(ifp
, interface
);
155 hook_call(if_add
, ifp
);
159 /* Create new interface structure. */
160 void if_update_to_new_vrf(struct interface
*ifp
, vrf_id_t vrf_id
)
164 /* remove interface from old master vrf list */
165 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
167 IFNAME_RB_REMOVE(vrf
, ifp
);
168 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
169 IFINDEX_RB_REMOVE(vrf
, ifp
);
172 ifp
->vrf_id
= vrf_id
;
173 vrf
= vrf_get(ifp
->vrf_id
, NULL
);
175 IFNAME_RB_INSERT(vrf
, ifp
);
176 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
177 IFINDEX_RB_INSERT(vrf
, ifp
);
181 /* Delete interface structure. */
182 void if_delete_retain(struct interface
*ifp
)
184 hook_call(if_del
, ifp
);
187 /* Free connected address list */
188 list_delete_all_node(ifp
->connected
);
190 /* Free connected nbr address list */
191 list_delete_all_node(ifp
->nbr_connected
);
194 /* Delete and free interface structure. */
195 void if_delete(struct interface
*ifp
)
199 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
202 IFNAME_RB_REMOVE(vrf
, ifp
);
203 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
204 IFINDEX_RB_REMOVE(vrf
, ifp
);
206 if_delete_retain(ifp
);
208 list_delete_and_null(&ifp
->connected
);
209 list_delete_and_null(&ifp
->nbr_connected
);
211 if_link_params_free(ifp
);
214 XFREE(MTYPE_TMP
, ifp
->desc
);
216 XFREE(MTYPE_IF
, ifp
);
219 /* Interface existance check by index. */
220 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
223 struct interface if_tmp
;
225 if (vrf_id
== VRF_UNKNOWN
) {
226 struct interface
*ifp
;
228 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
229 ifp
= if_lookup_by_index(ifindex
, vrf
->vrf_id
);
237 vrf
= vrf_lookup_by_id(vrf_id
);
241 if_tmp
.ifindex
= ifindex
;
242 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
245 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
247 struct interface
*ifp
;
249 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
254 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
256 struct interface
*ifp
;
258 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
263 /* Interface existance check by interface name. */
264 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
266 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
267 struct interface if_tmp
;
270 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
273 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
274 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
277 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
280 struct interface
*ifp
;
282 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
285 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
286 ifp
= if_lookup_by_name(name
, vrf
->vrf_id
);
294 /* Lookup interface by IPv4 address. */
295 struct interface
*if_lookup_exact_address(void *src
, int family
,
298 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
299 struct listnode
*cnode
;
300 struct interface
*ifp
;
304 FOR_ALL_INTERFACES (vrf
, ifp
) {
305 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
308 if (p
&& (p
->family
== family
)) {
309 if (family
== AF_INET
) {
312 (struct in_addr
*)src
))
314 } else if (family
== AF_INET6
) {
317 (struct in6_addr
*)src
))
326 /* Lookup interface by IPv4 address. */
327 struct connected
*if_lookup_address(void *matchaddr
, int family
,
330 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
333 struct listnode
*cnode
;
334 struct interface
*ifp
;
336 struct connected
*match
;
338 if (family
== AF_INET
) {
339 addr
.family
= AF_INET
;
340 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
341 addr
.prefixlen
= IPV4_MAX_BITLEN
;
342 } else if (family
== AF_INET6
) {
343 addr
.family
= AF_INET6
;
344 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
345 addr
.prefixlen
= IPV6_MAX_BITLEN
;
350 FOR_ALL_INTERFACES (vrf
, ifp
) {
351 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
352 if (c
->address
&& (c
->address
->family
== AF_INET
)
353 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
354 && (c
->address
->prefixlen
> bestlen
)) {
355 bestlen
= c
->address
->prefixlen
;
363 /* Lookup interface by prefix */
364 struct interface
*if_lookup_prefix(struct prefix
*prefix
, vrf_id_t vrf_id
)
366 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
367 struct listnode
*cnode
;
368 struct interface
*ifp
;
371 FOR_ALL_INTERFACES (vrf
, ifp
) {
372 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
373 if (prefix_cmp(c
->address
, prefix
) == 0) {
381 /* Get interface by name if given name interface doesn't exist create
383 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
, int vty
)
385 struct interface
*ifp
;
387 ifp
= if_lookup_by_name(name
, vrf_id
);
390 /* Not Found on same VRF. If the interface command
391 * was entered in vty without a VRF (passed as VRF_DEFAULT),
392 * accept the ifp we found. If a vrf was entered and there is
393 * a mismatch, reject it if from vty.
395 ifp
= if_lookup_by_name_all_vrf(name
);
397 return if_create(name
, vrf_id
);
399 if (vrf_id
== VRF_DEFAULT
)
403 /* if vrf backend uses NETNS, then
404 * this should not be considered as an update
405 * then create the new interface
407 if (ifp
->vrf_id
!= vrf_id
&& vrf_is_mapped_on_netns(vrf_id
))
408 return if_create(name
, vrf_id
);
409 /* If it came from the kernel
410 * or by way of zclient, believe it and update
411 * the ifp accordingly.
413 if_update_to_new_vrf(ifp
, 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 /* Does this interface support broadcast ? */
486 int if_is_broadcast(struct interface
*ifp
)
488 return ifp
->flags
& IFF_BROADCAST
;
491 /* Does this interface support broadcast ? */
492 int if_is_pointopoint(struct interface
*ifp
)
494 return ifp
->flags
& IFF_POINTOPOINT
;
497 /* Does this interface support multicast ? */
498 int if_is_multicast(struct interface
*ifp
)
500 return ifp
->flags
& IFF_MULTICAST
;
503 /* Printout flag information into log */
504 const char *if_flag_dump(unsigned long flag
)
507 static char logbuf
[BUFSIZ
];
509 #define IFF_OUT_LOG(X, STR) \
512 strlcat(logbuf, ",", BUFSIZ); \
515 strlcat(logbuf, STR, BUFSIZ); \
518 strlcpy(logbuf
, "<", BUFSIZ
);
519 IFF_OUT_LOG(IFF_UP
, "UP");
520 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
521 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
522 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
523 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
524 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
525 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
526 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
527 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
528 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
529 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
530 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
531 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
532 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
533 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
534 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
535 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
536 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
537 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
538 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
539 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
541 strlcat(logbuf
, ">", BUFSIZ
);
548 static void if_dump(const struct interface
*ifp
)
550 struct listnode
*node
;
551 struct connected
*c
__attribute__((unused
));
553 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
555 "Interface %s vrf %u index %d metric %d mtu %d "
557 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
,
558 ifp
->mtu
, ifp
->mtu6
, if_flag_dump(ifp
->flags
));
561 /* Interface printing for all interface. */
562 void if_dump_all(void)
567 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
568 FOR_ALL_INTERFACES (vrf
, ifp
)
572 DEFUN (interface_desc
,
574 "description LINE...",
575 "Interface specific description\n"
576 "Characters describing this interface\n")
579 VTY_DECLVAR_CONTEXT(interface
, ifp
);
582 XFREE(MTYPE_TMP
, ifp
->desc
);
583 ifp
->desc
= argv_concat(argv
, argc
, idx_line
);
588 DEFUN (no_interface_desc
,
589 no_interface_desc_cmd
,
592 "Interface specific description\n")
594 VTY_DECLVAR_CONTEXT(interface
, ifp
);
597 XFREE(MTYPE_TMP
, ifp
->desc
);
604 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
605 * a seperate struct interface for each logical interface, so config
606 * file may be full of 'interface fooX:Y'. Solaris however does not
607 * expose logical interfaces via PF_ROUTE, so trying to track logical
608 * interfaces can be fruitless, for that reason Quagga only tracks
609 * the primary IP interface.
611 * We try accomodate SUNWzebra by:
612 * - looking up the interface name, to see whether it exists, if so
614 * - for protocol daemons, this could only because zebra told us of
616 * - for zebra, only because it learnt from kernel
618 * - search the name to see if it contains a sub-ipif / logical interface
619 * seperator, the ':' char. If it does:
620 * - text up to that char must be the primary name - get that name.
622 * - no idea, just get the name in its entirety.
624 static struct interface
*if_sunwzebra_get(char *name
, vrf_id_t vrf_id
)
626 struct interface
*ifp
;
629 if ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
632 /* hunt the primary interface name... */
633 cp
= strchr(name
, ':');
637 return if_get_by_name(name
, vrf_id
, 1);
643 "interface IFNAME [vrf NAME]",
644 "Select an interface to configure\n"
650 const char *ifname
= argv
[idx_ifname
]->arg
;
651 const char *vrfname
= (argc
> 2) ? argv
[idx_vrf
]->arg
: NULL
;
653 struct interface
*ifp
;
654 vrf_id_t vrf_id
= VRF_DEFAULT
;
656 if (strlen(ifname
) > INTERFACE_NAMSIZ
) {
658 "%% Interface name %s is invalid: length exceeds "
660 ifname
, INTERFACE_NAMSIZ
);
661 return CMD_WARNING_CONFIG_FAILED
;
664 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
665 Imagine forward reference of a vrf by name in this interface config */
667 VRF_GET_ID(vrf_id
, vrfname
);
670 ifp
= if_sunwzebra_get(ifname
, vrf_id
);
672 ifp
= if_get_by_name(ifname
, vrf_id
, 1);
676 vty_out(vty
, "%% interface %s not in %s\n", ifname
, vrfname
);
677 return CMD_WARNING_CONFIG_FAILED
;
679 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
684 DEFUN_NOSH (no_interface
,
686 "no interface IFNAME [vrf NAME]",
688 "Delete a pseudo interface's configuration\n"
693 const char *ifname
= argv
[2]->arg
;
694 const char *vrfname
= (argc
> 3) ? argv
[idx_vrf
]->arg
: NULL
;
696 // deleting interface
697 struct interface
*ifp
;
698 vrf_id_t vrf_id
= VRF_DEFAULT
;
701 VRF_GET_ID(vrf_id
, vrfname
);
703 ifp
= if_lookup_by_name(ifname
, vrf_id
);
706 vty_out(vty
, "%% Interface %s does not exist\n", ifname
);
707 return CMD_WARNING_CONFIG_FAILED
;
710 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
711 vty_out(vty
, "%% Only inactive interfaces can be deleted\n");
712 return CMD_WARNING_CONFIG_FAILED
;
720 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
722 struct interface
*ifp
;
723 struct vrf
*vrf
= NULL
;
725 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
726 FOR_ALL_INTERFACES (vrf
, ifp
) {
727 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
732 static const struct cmd_variable_handler if_var_handlers
[] = {
733 {/* "interface NAME" */
734 .varname
= "interface",
735 .completions
= if_autocomplete
},
736 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
737 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
738 {.completions
= NULL
}};
740 void if_cmd_init(void)
742 cmd_variable_handler_register(if_var_handlers
);
744 install_element(CONFIG_NODE
, &interface_cmd
);
745 install_element(CONFIG_NODE
, &no_interface_cmd
);
747 install_default(INTERFACE_NODE
);
748 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
749 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
753 /* For debug purpose. */
756 "show address [vrf NAME]",
762 struct listnode
*node
;
763 struct interface
*ifp
;
764 struct connected
*ifc
;
766 vrf_id_t vrf_id
= VRF_DEFAULT
;
769 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
771 FOR_ALL_INTERFACES (vrf
, ifp
)
773 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
777 if (p
->family
== AF_INET
)
778 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
784 DEFUN (show_address_vrf_all
,
785 show_address_vrf_all_cmd
,
786 "show address vrf all",
789 VRF_ALL_CMD_HELP_STR
)
792 struct listnode
*node
;
793 struct interface
*ifp
;
794 struct connected
*ifc
;
797 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
799 if (RB_EMPTY (if_name_head
, &vrf
->ifaces_by_name
))
802 vty_out (vty
, "\nVRF %u\n\n", vrf
->vrf_id
);
804 FOR_ALL_INTERFACES (vrf
, ifp
)
806 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
810 if (p
->family
== AF_INET
)
811 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
819 /* Allocate connected structure. */
820 struct connected
*connected_new(void)
822 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
825 /* Allocate nbr connected structure. */
826 struct nbr_connected
*nbr_connected_new(void)
828 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
831 /* Free connected structure. */
832 void connected_free(struct connected
*connected
)
834 if (connected
->address
)
835 prefix_free(connected
->address
);
837 if (connected
->destination
)
838 prefix_free(connected
->destination
);
840 if (connected
->label
)
841 XFREE(MTYPE_CONNECTED_LABEL
, connected
->label
);
843 XFREE(MTYPE_CONNECTED
, connected
);
846 /* Free nbr connected structure. */
847 void nbr_connected_free(struct nbr_connected
*connected
)
849 if (connected
->address
)
850 prefix_free(connected
->address
);
852 XFREE(MTYPE_NBR_CONNECTED
, connected
);
855 /* If same interface nbr address already exists... */
856 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
859 struct nbr_connected
*ifc
;
860 struct listnode
*node
;
862 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
863 if (prefix_same(ifc
->address
, p
))
869 /* Print if_addr structure. */
870 static void __attribute__((unused
))
871 connected_log(struct connected
*connected
, char *str
)
874 struct interface
*ifp
;
878 ifp
= connected
->ifp
;
879 p
= connected
->address
;
881 snprintf(logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ", str
,
882 ifp
->name
, ifp
->vrf_id
, prefix_family_str(p
),
883 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
885 p
= connected
->destination
;
887 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
888 BUFSIZ
- strlen(logbuf
));
890 zlog_info("%s", logbuf
);
893 /* Print if_addr structure. */
894 static void __attribute__((unused
))
895 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
898 struct interface
*ifp
;
902 ifp
= connected
->ifp
;
903 p
= connected
->address
;
905 snprintf(logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ", str
, ifp
->name
,
906 prefix_family_str(p
),
907 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
909 zlog_info("%s", logbuf
);
912 /* If two connected address has same prefix return 1. */
913 static int connected_same_prefix(struct prefix
*p1
, struct prefix
*p2
)
915 if (p1
->family
== p2
->family
) {
916 if (p1
->family
== AF_INET
917 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
919 if (p1
->family
== AF_INET6
920 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
926 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
929 struct listnode
*node
;
930 struct listnode
*next
;
931 struct connected
*ifc
;
933 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
934 ifc
= listgetdata(node
);
937 if (connected_same_prefix(ifc
->address
, p
))
943 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
946 struct listnode
*node
;
947 struct listnode
*next
;
948 struct connected
*ifc
;
950 /* In case of same prefix come, replace it with new one. */
951 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
952 ifc
= listgetdata(node
);
955 if (connected_same_prefix(ifc
->address
, p
)) {
956 listnode_delete(ifp
->connected
, ifc
);
963 /* Find the address on our side that will be used when packets
965 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
968 struct listnode
*cnode
;
970 struct connected
*match
;
974 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
975 if (c
->address
&& (c
->address
->family
== addr
->family
)
976 && prefix_match(CONNECTED_PREFIX(c
), addr
)
978 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
984 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
986 struct prefix
*destination
)
988 struct connected
*ifc
;
990 /* Allocate new connected address. */
991 ifc
= connected_new();
994 /* Fetch interface address */
995 ifc
->address
= prefix_new();
996 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
998 /* Fetch dest address */
1000 ifc
->destination
= prefix_new();
1001 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
1004 /* Add connected address to the interface. */
1005 listnode_add(ifp
->connected
, ifc
);
1009 #if 0 /* this route_table of struct connected's is unused \
1010 * however, it would be good to use a route_table rather than \
1013 /* Interface looking up by interface's address. */
1014 /* Interface's IPv4 address reverse lookup table. */
1015 struct route_table
*ifaddr_ipv4_table
;
1016 /* struct route_table *ifaddr_ipv6_table; */
1019 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
1021 struct route_node
*rn
;
1022 struct prefix_ipv4 p
;
1025 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1028 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1031 route_unlock_node (rn
);
1032 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1033 inet_ntoa (*ifaddr
));
1040 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1042 struct route_node
*rn
;
1043 struct prefix_ipv4 p
;
1046 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1049 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1052 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1053 inet_ntoa (*ifaddr
));
1057 route_unlock_node (rn
);
1058 route_unlock_node (rn
);
1061 /* Lookup interface by interface's IP address or interface index. */
1062 static struct interface
*
1063 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1065 struct prefix_ipv4 p
;
1066 struct route_node
*rn
;
1067 struct interface
*ifp
;
1072 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1075 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1080 route_unlock_node (rn
);
1084 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1086 #endif /* ifaddr_ipv4_table */
1088 void if_terminate(struct vrf
*vrf
)
1090 struct interface
*ifp
;
1092 while (!RB_EMPTY(if_name_head
, &vrf
->ifaces_by_name
)) {
1093 ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
);
1096 ifp
->node
->info
= NULL
;
1097 route_unlock_node(ifp
->node
);
1103 const char *if_link_type_str(enum zebra_link_type llt
)
1106 #define llts(T,S) case (T): return (S)
1107 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1108 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1109 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1110 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1111 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1112 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1113 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1114 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1115 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1116 llts(ZEBRA_LLT_ATM
, "ATM");
1117 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1118 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1119 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1120 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1121 llts(ZEBRA_LLT_SLIP
, "SLIP");
1122 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1123 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1124 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1125 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1126 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1127 llts(ZEBRA_LLT_PPP
, "PPP");
1128 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1129 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1130 llts(ZEBRA_LLT_LAPB
, "LAPB");
1131 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1132 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1133 llts(ZEBRA_LLT_FRAD
, "FRAD");
1134 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1135 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1136 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1137 llts(ZEBRA_LLT_FDDI
, "FDDI");
1138 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1139 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1140 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1141 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1142 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1143 llts(ZEBRA_LLT_IRDA
, "IrDA");
1144 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1145 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1146 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1147 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1148 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1149 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1150 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1151 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1152 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1154 zlog_warn("Unknown value %d", llt
);
1155 return "Unknown type!";
1161 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1165 if (ifp
->link_params
!= NULL
)
1166 return ifp
->link_params
;
1168 struct if_link_params
*iflp
=
1169 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1173 /* Set TE metric equal to standard metric */
1174 iflp
->te_metric
= ifp
->metric
;
1176 /* Compute default bandwidth based on interface */
1178 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1179 * TE_KILO_BIT
/ TE_BYTE
);
1181 /* Set Max, Reservable and Unreserved Bandwidth */
1182 iflp
->max_bw
= iflp
->default_bw
;
1183 iflp
->max_rsv_bw
= iflp
->default_bw
;
1184 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1185 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1187 /* Update Link parameters status */
1189 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1191 /* Finally attach newly created Link Parameters */
1192 ifp
->link_params
= iflp
;
1197 void if_link_params_free(struct interface
*ifp
)
1199 if (ifp
->link_params
== NULL
)
1201 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1202 ifp
->link_params
= NULL
;