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
)
197 struct vrf
*vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
199 IFNAME_RB_REMOVE(vrf
, ifp
);
200 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
201 IFINDEX_RB_REMOVE(vrf
, ifp
);
203 if_delete_retain(ifp
);
205 list_delete_and_null(&ifp
->connected
);
206 list_delete_and_null(&ifp
->nbr_connected
);
208 if_link_params_free(ifp
);
210 XFREE(MTYPE_IF
, ifp
);
213 /* Interface existance check by index. */
214 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
216 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
217 struct interface if_tmp
;
219 if_tmp
.ifindex
= ifindex
;
220 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
223 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
225 struct interface
*ifp
;
227 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
232 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
234 struct interface
*ifp
;
236 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
241 /* Interface existance check by interface name. */
242 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
244 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
245 struct interface if_tmp
;
247 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
250 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
251 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
254 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
257 struct interface
*ifp
;
259 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
262 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
263 ifp
= if_lookup_by_name(name
, vrf
->vrf_id
);
271 /* Lookup interface by IPv4 address. */
272 struct interface
*if_lookup_exact_address(void *src
, int family
,
275 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
276 struct listnode
*cnode
;
277 struct interface
*ifp
;
281 FOR_ALL_INTERFACES (vrf
, ifp
) {
282 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
285 if (p
&& (p
->family
== family
)) {
286 if (family
== AF_INET
) {
289 (struct in_addr
*)src
))
291 } else if (family
== AF_INET6
) {
294 (struct in6_addr
*)src
))
303 /* Lookup interface by IPv4 address. */
304 struct connected
*if_lookup_address(void *matchaddr
, int family
,
307 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
310 struct listnode
*cnode
;
311 struct interface
*ifp
;
313 struct connected
*match
;
315 if (family
== AF_INET
) {
316 addr
.family
= AF_INET
;
317 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
318 addr
.prefixlen
= IPV4_MAX_BITLEN
;
319 } else if (family
== AF_INET6
) {
320 addr
.family
= AF_INET6
;
321 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
322 addr
.prefixlen
= IPV6_MAX_BITLEN
;
327 FOR_ALL_INTERFACES (vrf
, ifp
) {
328 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
329 if (c
->address
&& (c
->address
->family
== AF_INET
)
330 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
331 && (c
->address
->prefixlen
> bestlen
)) {
332 bestlen
= c
->address
->prefixlen
;
340 /* Lookup interface by prefix */
341 struct interface
*if_lookup_prefix(struct prefix
*prefix
, vrf_id_t vrf_id
)
343 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
344 struct listnode
*cnode
;
345 struct interface
*ifp
;
348 FOR_ALL_INTERFACES (vrf
, ifp
) {
349 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
350 if (prefix_cmp(c
->address
, prefix
) == 0) {
358 /* Get interface by name if given name interface doesn't exist create
360 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
, int vty
)
362 struct interface
*ifp
;
364 ifp
= if_lookup_by_name_all_vrf(name
);
366 if (ifp
->vrf_id
== vrf_id
)
369 /* Found a match on a different VRF. If the interface command
370 * was entered in vty without a VRF (passed as VRF_DEFAULT),
371 * accept the ifp we found. If a vrf was entered and there is
372 * a mismatch, reject it if from vty. If it came from the kernel
373 * or by way of zclient, believe it and update the ifp
377 if (vrf_id
== VRF_DEFAULT
)
381 if_update_to_new_vrf(ifp
, vrf_id
);
386 return if_create(name
, vrf_id
);
389 void if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
391 struct vrf
*vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
393 if (ifp
->ifindex
== ifindex
)
396 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
397 IFINDEX_RB_REMOVE(vrf
, ifp
)
399 ifp
->ifindex
= ifindex
;
401 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
402 IFINDEX_RB_INSERT(vrf
, ifp
)
405 /* Does interface up ? */
406 int if_is_up(struct interface
*ifp
)
408 return ifp
->flags
& IFF_UP
;
411 /* Is interface running? */
412 int if_is_running(struct interface
*ifp
)
414 return ifp
->flags
& IFF_RUNNING
;
417 /* Is the interface operative, eg. either UP & RUNNING
418 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
419 if ptm checking is enabled, then ptm check has passed */
420 int if_is_operative(struct interface
*ifp
)
422 return ((ifp
->flags
& IFF_UP
)
423 && (((ifp
->flags
& IFF_RUNNING
)
424 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
425 || !CHECK_FLAG(ifp
->status
,
426 ZEBRA_INTERFACE_LINKDETECTION
)));
429 /* Is the interface operative, eg. either UP & RUNNING
430 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
431 int if_is_no_ptm_operative(struct interface
*ifp
)
433 return ((ifp
->flags
& IFF_UP
)
434 && ((ifp
->flags
& IFF_RUNNING
)
435 || !CHECK_FLAG(ifp
->status
,
436 ZEBRA_INTERFACE_LINKDETECTION
)));
439 /* Is this loopback interface ? */
440 int if_is_loopback(struct interface
*ifp
)
442 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
443 * but Y on platform N?
445 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
448 /* Does this interface support broadcast ? */
449 int if_is_broadcast(struct interface
*ifp
)
451 return ifp
->flags
& IFF_BROADCAST
;
454 /* Does this interface support broadcast ? */
455 int if_is_pointopoint(struct interface
*ifp
)
457 return ifp
->flags
& IFF_POINTOPOINT
;
460 /* Does this interface support multicast ? */
461 int if_is_multicast(struct interface
*ifp
)
463 return ifp
->flags
& IFF_MULTICAST
;
466 /* Printout flag information into log */
467 const char *if_flag_dump(unsigned long flag
)
470 static char logbuf
[BUFSIZ
];
472 #define IFF_OUT_LOG(X, STR) \
475 strlcat(logbuf, ",", BUFSIZ); \
478 strlcat(logbuf, STR, BUFSIZ); \
481 strlcpy(logbuf
, "<", BUFSIZ
);
482 IFF_OUT_LOG(IFF_UP
, "UP");
483 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
484 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
485 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
486 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
487 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
488 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
489 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
490 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
491 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
492 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
493 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
494 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
495 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
496 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
497 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
498 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
499 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
500 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
501 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
502 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
504 strlcat(logbuf
, ">", BUFSIZ
);
511 static void if_dump(const struct interface
*ifp
)
513 struct listnode
*node
;
514 struct connected
*c
__attribute__((unused
));
516 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
518 "Interface %s vrf %u index %d metric %d mtu %d "
520 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
,
521 ifp
->mtu
, ifp
->mtu6
, if_flag_dump(ifp
->flags
));
524 /* Interface printing for all interface. */
525 void if_dump_all(void)
530 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
531 FOR_ALL_INTERFACES (vrf
, ifp
)
535 DEFUN (interface_desc
,
537 "description LINE...",
538 "Interface specific description\n"
539 "Characters describing this interface\n")
542 VTY_DECLVAR_CONTEXT(interface
, ifp
);
545 XFREE(MTYPE_TMP
, ifp
->desc
);
546 ifp
->desc
= argv_concat(argv
, argc
, idx_line
);
551 DEFUN (no_interface_desc
,
552 no_interface_desc_cmd
,
555 "Interface specific description\n")
557 VTY_DECLVAR_CONTEXT(interface
, ifp
);
560 XFREE(MTYPE_TMP
, ifp
->desc
);
567 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
568 * a seperate struct interface for each logical interface, so config
569 * file may be full of 'interface fooX:Y'. Solaris however does not
570 * expose logical interfaces via PF_ROUTE, so trying to track logical
571 * interfaces can be fruitless, for that reason Quagga only tracks
572 * the primary IP interface.
574 * We try accomodate SUNWzebra by:
575 * - looking up the interface name, to see whether it exists, if so
577 * - for protocol daemons, this could only because zebra told us of
579 * - for zebra, only because it learnt from kernel
581 * - search the name to see if it contains a sub-ipif / logical interface
582 * seperator, the ':' char. If it does:
583 * - text up to that char must be the primary name - get that name.
585 * - no idea, just get the name in its entirety.
587 static struct interface
*if_sunwzebra_get(char *name
, vrf_id_t vrf_id
)
589 struct interface
*ifp
;
592 if ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
595 /* hunt the primary interface name... */
596 cp
= strchr(name
, ':');
600 return if_get_by_name(name
, vrf_id
, 1);
606 "interface IFNAME [vrf NAME]",
607 "Select an interface to configure\n"
613 const char *ifname
= argv
[idx_ifname
]->arg
;
614 const char *vrfname
= (argc
> 2) ? argv
[idx_vrf
]->arg
: NULL
;
616 struct interface
*ifp
;
617 vrf_id_t vrf_id
= VRF_DEFAULT
;
619 if (strlen(ifname
) > INTERFACE_NAMSIZ
) {
621 "%% Interface name %s is invalid: length exceeds "
623 ifname
, INTERFACE_NAMSIZ
);
624 return CMD_WARNING_CONFIG_FAILED
;
627 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
628 Imagine forward reference of a vrf by name in this interface config */
630 VRF_GET_ID(vrf_id
, vrfname
);
633 ifp
= if_sunwzebra_get(ifname
, vrf_id
);
635 ifp
= if_get_by_name(ifname
, vrf_id
, 1);
639 vty_out(vty
, "%% interface %s not in %s\n", ifname
, vrfname
);
640 return CMD_WARNING_CONFIG_FAILED
;
642 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
647 DEFUN_NOSH (no_interface
,
649 "no interface IFNAME [vrf NAME]",
651 "Delete a pseudo interface's configuration\n"
655 const char *ifname
= argv
[2]->arg
;
656 const char *vrfname
= (argc
> 3) ? argv
[3]->arg
: NULL
;
658 // deleting interface
659 struct interface
*ifp
;
660 vrf_id_t vrf_id
= VRF_DEFAULT
;
663 VRF_GET_ID(vrf_id
, vrfname
);
665 ifp
= if_lookup_by_name(ifname
, vrf_id
);
668 vty_out(vty
, "%% Interface %s does not exist\n", ifname
);
669 return CMD_WARNING_CONFIG_FAILED
;
672 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
673 vty_out(vty
, "%% Only inactive interfaces can be deleted\n");
674 return CMD_WARNING_CONFIG_FAILED
;
682 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
684 struct interface
*ifp
;
685 struct vrf
*vrf
= NULL
;
687 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
688 FOR_ALL_INTERFACES (vrf
, ifp
) {
689 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
694 static const struct cmd_variable_handler if_var_handlers
[] = {
695 {/* "interface NAME" */
696 .varname
= "interface",
697 .completions
= if_autocomplete
},
698 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
699 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
700 {.completions
= NULL
}};
702 void if_cmd_init(void)
704 cmd_variable_handler_register(if_var_handlers
);
706 install_element(CONFIG_NODE
, &interface_cmd
);
707 install_element(CONFIG_NODE
, &no_interface_cmd
);
709 install_default(INTERFACE_NODE
);
710 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
711 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
715 /* For debug purpose. */
718 "show address [vrf NAME]",
724 struct listnode
*node
;
725 struct interface
*ifp
;
726 struct connected
*ifc
;
728 vrf_id_t vrf_id
= VRF_DEFAULT
;
731 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
733 FOR_ALL_INTERFACES (vrf
, ifp
)
735 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
739 if (p
->family
== AF_INET
)
740 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
746 DEFUN (show_address_vrf_all
,
747 show_address_vrf_all_cmd
,
748 "show address vrf all",
751 VRF_ALL_CMD_HELP_STR
)
754 struct listnode
*node
;
755 struct interface
*ifp
;
756 struct connected
*ifc
;
759 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
761 if (RB_EMPTY (if_name_head
, &vrf
->ifaces_by_name
))
764 vty_out (vty
, "\nVRF %u\n\n", vrf
->vrf_id
);
766 FOR_ALL_INTERFACES (vrf
, ifp
)
768 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
772 if (p
->family
== AF_INET
)
773 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
781 /* Allocate connected structure. */
782 struct connected
*connected_new(void)
784 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
787 /* Allocate nbr connected structure. */
788 struct nbr_connected
*nbr_connected_new(void)
790 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
793 /* Free connected structure. */
794 void connected_free(struct connected
*connected
)
796 if (connected
->address
)
797 prefix_free(connected
->address
);
799 if (connected
->destination
)
800 prefix_free(connected
->destination
);
802 if (connected
->label
)
803 XFREE(MTYPE_CONNECTED_LABEL
, connected
->label
);
805 XFREE(MTYPE_CONNECTED
, connected
);
808 /* Free nbr connected structure. */
809 void nbr_connected_free(struct nbr_connected
*connected
)
811 if (connected
->address
)
812 prefix_free(connected
->address
);
814 XFREE(MTYPE_NBR_CONNECTED
, connected
);
817 /* If same interface nbr address already exists... */
818 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
821 struct nbr_connected
*ifc
;
822 struct listnode
*node
;
824 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
825 if (prefix_same(ifc
->address
, p
))
831 /* Print if_addr structure. */
832 static void __attribute__((unused
))
833 connected_log(struct connected
*connected
, char *str
)
836 struct interface
*ifp
;
840 ifp
= connected
->ifp
;
841 p
= connected
->address
;
843 snprintf(logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ", str
,
844 ifp
->name
, ifp
->vrf_id
, prefix_family_str(p
),
845 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
847 p
= connected
->destination
;
849 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
850 BUFSIZ
- strlen(logbuf
));
852 zlog_info("%s", logbuf
);
855 /* Print if_addr structure. */
856 static void __attribute__((unused
))
857 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
860 struct interface
*ifp
;
864 ifp
= connected
->ifp
;
865 p
= connected
->address
;
867 snprintf(logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ", str
, ifp
->name
,
868 prefix_family_str(p
),
869 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
871 zlog_info("%s", logbuf
);
874 /* If two connected address has same prefix return 1. */
875 static int connected_same_prefix(struct prefix
*p1
, struct prefix
*p2
)
877 if (p1
->family
== p2
->family
) {
878 if (p1
->family
== AF_INET
879 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
881 if (p1
->family
== AF_INET6
882 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
888 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
891 struct listnode
*node
;
892 struct listnode
*next
;
893 struct connected
*ifc
;
895 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
896 ifc
= listgetdata(node
);
899 if (connected_same_prefix(ifc
->address
, p
))
905 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
908 struct listnode
*node
;
909 struct listnode
*next
;
910 struct connected
*ifc
;
912 /* In case of same prefix come, replace it with new one. */
913 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
914 ifc
= listgetdata(node
);
917 if (connected_same_prefix(ifc
->address
, p
)) {
918 listnode_delete(ifp
->connected
, ifc
);
925 /* Find the address on our side that will be used when packets
927 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
930 struct listnode
*cnode
;
932 struct connected
*match
;
936 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
937 if (c
->address
&& (c
->address
->family
== addr
->family
)
938 && prefix_match(CONNECTED_PREFIX(c
), addr
)
940 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
946 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
948 struct prefix
*destination
)
950 struct connected
*ifc
;
952 /* Allocate new connected address. */
953 ifc
= connected_new();
956 /* Fetch interface address */
957 ifc
->address
= prefix_new();
958 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
960 /* Fetch dest address */
962 ifc
->destination
= prefix_new();
963 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
966 /* Add connected address to the interface. */
967 listnode_add(ifp
->connected
, ifc
);
971 #if 0 /* this route_table of struct connected's is unused \
972 * however, it would be good to use a route_table rather than \
975 /* Interface looking up by interface's address. */
976 /* Interface's IPv4 address reverse lookup table. */
977 struct route_table
*ifaddr_ipv4_table
;
978 /* struct route_table *ifaddr_ipv6_table; */
981 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
983 struct route_node
*rn
;
984 struct prefix_ipv4 p
;
987 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
990 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
993 route_unlock_node (rn
);
994 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
995 inet_ntoa (*ifaddr
));
1002 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1004 struct route_node
*rn
;
1005 struct prefix_ipv4 p
;
1008 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1011 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1014 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1015 inet_ntoa (*ifaddr
));
1019 route_unlock_node (rn
);
1020 route_unlock_node (rn
);
1023 /* Lookup interface by interface's IP address or interface index. */
1024 static struct interface
*
1025 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1027 struct prefix_ipv4 p
;
1028 struct route_node
*rn
;
1029 struct interface
*ifp
;
1034 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1037 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1042 route_unlock_node (rn
);
1046 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1048 #endif /* ifaddr_ipv4_table */
1050 void if_terminate(struct vrf
*vrf
)
1052 struct interface
*ifp
;
1054 while ((ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
)) != NULL
) {
1056 ifp
->node
->info
= NULL
;
1057 route_unlock_node(ifp
->node
);
1063 const char *if_link_type_str(enum zebra_link_type llt
)
1066 #define llts(T,S) case (T): return (S)
1067 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1068 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1069 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1070 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1071 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1072 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1073 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1074 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1075 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1076 llts(ZEBRA_LLT_ATM
, "ATM");
1077 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1078 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1079 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1080 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1081 llts(ZEBRA_LLT_SLIP
, "SLIP");
1082 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1083 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1084 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1085 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1086 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1087 llts(ZEBRA_LLT_PPP
, "PPP");
1088 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1089 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1090 llts(ZEBRA_LLT_LAPB
, "LAPB");
1091 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1092 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1093 llts(ZEBRA_LLT_FRAD
, "FRAD");
1094 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1095 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1096 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1097 llts(ZEBRA_LLT_FDDI
, "FDDI");
1098 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1099 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1100 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1101 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1102 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1103 llts(ZEBRA_LLT_IRDA
, "IrDA");
1104 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1105 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1106 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1107 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1108 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1109 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1110 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1111 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1112 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1114 zlog_warn("Unknown value %d", llt
);
1115 return "Unknown type!";
1121 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1125 if (ifp
->link_params
!= NULL
)
1126 return ifp
->link_params
;
1128 struct if_link_params
*iflp
=
1129 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1133 /* Set TE metric equal to standard metric */
1134 iflp
->te_metric
= ifp
->metric
;
1136 /* Compute default bandwidth based on interface */
1138 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1139 * TE_KILO_BIT
/ TE_BYTE
);
1141 /* Set Max, Reservable and Unreserved Bandwidth */
1142 iflp
->max_bw
= iflp
->default_bw
;
1143 iflp
->max_rsv_bw
= iflp
->default_bw
;
1144 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1145 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1147 /* Update Link parameters status */
1149 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1151 /* Finally attach newly created Link Parameters */
1152 ifp
->link_params
= iflp
;
1157 void if_link_params_free(struct interface
*ifp
)
1159 if (ifp
->link_params
== NULL
)
1161 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1162 ifp
->link_params
= NULL
;