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 DEFINE_QOBJ_TYPE(interface
)
45 /* List of interfaces in only the default VRF */
48 /* One for each program. This structure is needed to store hooks. */
50 int (*if_new_hook
)(struct interface
*);
51 int (*if_delete_hook
)(struct interface
*);
56 /* Compare interface names, returning an integer greater than, equal to, or
57 * less than 0, (following the strcmp convention), according to the
58 * relationship between ifp1 and ifp2. Interface names consist of an
59 * alphabetic prefix and a numeric suffix. The primary sort key is
60 * lexicographic by name, and then numeric by number. No number sorts
61 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
64 int if_cmp_name_func(char *p1
, char *p2
)
71 /* look up to any number */
72 l1
= strcspn(p1
, "0123456789");
73 l2
= strcspn(p2
, "0123456789");
75 /* name lengths are different -> compare names */
77 return (strcmp(p1
, p2
));
79 /* Note that this relies on all numbers being less than all
83 res
= strncmp(p1
, p2
, l1
);
85 /* names are different -> compare them */
89 /* with identical name part, go to numeric part */
98 x1
= strtol(p1
, &p1
, 10);
99 x2
= strtol(p2
, &p2
, 10);
101 /* let's compare numbers now */
107 /* numbers were equal, lets do it again..
108 (it happens with name like "eth123.456:789") */
117 static int if_cmp_func(struct interface
*ifp1
, struct interface
*ifp2
)
119 return if_cmp_name_func(ifp1
->name
, ifp2
->name
);
122 /* Create new interface structure. */
123 struct interface
*if_create(const char *name
, int namelen
, vrf_id_t vrf_id
)
125 struct interface
*ifp
;
126 struct list
*intf_list
= vrf_iflist_get(vrf_id
);
128 ifp
= XCALLOC(MTYPE_IF
, sizeof(struct interface
));
129 ifp
->ifindex
= IFINDEX_INTERNAL
;
132 assert(namelen
<= INTERFACE_NAMSIZ
); /* Need space for '\0' at end. */
133 strncpy(ifp
->name
, name
, namelen
);
134 ifp
->name
[namelen
] = '\0';
135 ifp
->vrf_id
= vrf_id
;
136 if (if_lookup_by_name(ifp
->name
, vrf_id
) == NULL
)
137 listnode_add_sort(intf_list
, ifp
);
140 "if_create(%s): corruption detected -- interface with this "
141 "name exists already in VRF %u!",
143 ifp
->connected
= list_new();
144 ifp
->connected
->del
= (void (*)(void *))connected_free
;
146 ifp
->nbr_connected
= list_new();
147 ifp
->nbr_connected
->del
= (void (*)(void *))nbr_connected_free
;
149 /* Enable Link-detection by default */
150 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
152 QOBJ_REG(ifp
, interface
);
154 if (if_master
.if_new_hook
)
155 (*if_master
.if_new_hook
)(ifp
);
160 /* Create new interface structure. */
161 void if_update_to_new_vrf(struct interface
*ifp
, vrf_id_t vrf_id
)
163 struct list
*intf_list
= vrf_iflist_get(vrf_id
);
165 /* remove interface from old master vrf list */
166 if (vrf_iflist(ifp
->vrf_id
))
167 listnode_delete(vrf_iflist(ifp
->vrf_id
), ifp
);
169 ifp
->vrf_id
= vrf_id
;
170 if (if_lookup_by_name(ifp
->name
, vrf_id
) == NULL
)
171 listnode_add_sort(intf_list
, ifp
);
174 "if_create(%s): corruption detected -- interface with this "
175 "name exists already in VRF %u!",
182 /* Delete interface structure. */
183 void if_delete_retain(struct interface
*ifp
)
185 if (if_master
.if_delete_hook
)
186 (*if_master
.if_delete_hook
)(ifp
);
190 /* Free connected address list */
191 list_delete_all_node(ifp
->connected
);
193 /* Free connected nbr address list */
194 list_delete_all_node(ifp
->nbr_connected
);
197 /* Delete and free interface structure. */
198 void if_delete(struct interface
*ifp
)
200 listnode_delete(vrf_iflist(ifp
->vrf_id
), ifp
);
202 if_delete_retain(ifp
);
204 list_free(ifp
->connected
);
205 list_free(ifp
->nbr_connected
);
207 if_link_params_free(ifp
);
209 XFREE(MTYPE_IF
, ifp
);
212 /* Add hook to interface master. */
213 void if_add_hook(int type
, int (*func
)(struct interface
*ifp
))
217 if_master
.if_new_hook
= func
;
220 if_master
.if_delete_hook
= func
;
227 /* Interface existance check by index. */
228 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
230 struct listnode
*node
;
231 struct interface
*ifp
;
233 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), node
, ifp
)) {
234 if (ifp
->ifindex
== ifindex
)
240 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
242 struct interface
*ifp
;
244 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
249 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
251 struct interface
*ifp
;
253 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
258 /* Interface existance check by interface name. */
259 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
261 struct listnode
*node
;
262 struct interface
*ifp
;
265 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), node
, ifp
)) {
266 if (strcmp(name
, ifp
->name
) == 0)
272 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
275 struct interface
*ifp
;
277 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
)
279 ifp
= if_lookup_by_name(name
, vrf
->vrf_id
);
287 struct interface
*if_lookup_by_name_len(const char *name
, size_t namelen
,
290 struct listnode
*node
;
291 struct interface
*ifp
;
293 if (namelen
> INTERFACE_NAMSIZ
)
296 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), node
, ifp
)) {
297 if (!memcmp(name
, ifp
->name
, namelen
)
298 && (ifp
->name
[namelen
] == '\0'))
304 /* Lookup interface by IPv4 address. */
305 struct interface
*if_lookup_exact_address(void *src
, int family
,
308 struct listnode
*node
;
309 struct listnode
*cnode
;
310 struct interface
*ifp
;
314 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), node
, ifp
)) {
315 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
318 if (p
&& (p
->family
== family
)) {
319 if (family
== AF_INET
) {
322 (struct in_addr
*)src
))
324 } else if (family
== AF_INET6
) {
327 (struct in6_addr
*)src
))
336 /* Lookup interface by IPv4 address. */
337 struct connected
*if_lookup_address(void *matchaddr
, int family
,
340 struct listnode
*node
;
343 struct listnode
*cnode
;
344 struct interface
*ifp
;
346 struct connected
*match
;
348 if (family
== AF_INET
) {
349 addr
.family
= AF_INET
;
350 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
351 addr
.prefixlen
= IPV4_MAX_BITLEN
;
352 } else if (family
== AF_INET6
) {
353 addr
.family
= AF_INET6
;
354 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
355 addr
.prefixlen
= IPV6_MAX_BITLEN
;
360 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), node
, ifp
)) {
361 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
362 if (c
->address
&& (c
->address
->family
== AF_INET
)
363 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
364 && (c
->address
->prefixlen
> bestlen
)) {
365 bestlen
= c
->address
->prefixlen
;
373 /* Lookup interface by prefix */
374 struct interface
*if_lookup_prefix(struct prefix
*prefix
, vrf_id_t vrf_id
)
376 struct listnode
*node
;
377 struct listnode
*cnode
;
378 struct interface
*ifp
;
381 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), node
, ifp
)) {
382 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
383 if (prefix_cmp(c
->address
, prefix
) == 0) {
391 /* Get interface by name if given name interface doesn't exist create
393 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
)
395 struct interface
*ifp
;
397 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
399 : if_create(name
, strlen(name
), vrf_id
);
402 struct interface
*if_get_by_name_len(const char *name
, size_t namelen
,
403 vrf_id_t vrf_id
, int vty
)
405 struct interface
*ifp
;
407 struct listnode
*node
;
409 ifp
= if_lookup_by_name_len(name
, namelen
, vrf_id
);
413 /* Didn't find the interface on that vrf. Defined on a different one? */
414 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
)
416 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf
->vrf_id
), node
, ifp
)) {
417 if (!memcmp(name
, ifp
->name
, namelen
)
418 && (ifp
->name
[namelen
] == '\0')) {
419 /* Found a match. If the interface command was
420 * entered in vty without a
421 * VRF (passed as VRF_DEFAULT), accept the ifp
422 * we found. If a vrf was
423 * entered and there is a mismatch, reject it if
424 * from vty. If it came
425 * from the kernel by way of zclient, believe
427 * the ifp accordingly.
430 if (vrf_id
== VRF_DEFAULT
)
434 if_update_to_new_vrf(ifp
, vrf_id
);
440 return (if_create(name
, namelen
, vrf_id
));
443 /* Does interface up ? */
444 int if_is_up(struct interface
*ifp
)
446 return ifp
->flags
& IFF_UP
;
449 /* Is interface running? */
450 int if_is_running(struct interface
*ifp
)
452 return ifp
->flags
& IFF_RUNNING
;
455 /* Is the interface operative, eg. either UP & RUNNING
456 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
457 if ptm checking is enabled, then ptm check has passed */
458 int if_is_operative(struct interface
*ifp
)
460 return ((ifp
->flags
& IFF_UP
)
461 && (((ifp
->flags
& IFF_RUNNING
)
462 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
463 || !CHECK_FLAG(ifp
->status
,
464 ZEBRA_INTERFACE_LINKDETECTION
)));
467 /* Is the interface operative, eg. either UP & RUNNING
468 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
469 int if_is_no_ptm_operative(struct interface
*ifp
)
471 return ((ifp
->flags
& IFF_UP
)
472 && ((ifp
->flags
& IFF_RUNNING
)
473 || !CHECK_FLAG(ifp
->status
,
474 ZEBRA_INTERFACE_LINKDETECTION
)));
477 /* Is this loopback interface ? */
478 int if_is_loopback(struct interface
*ifp
)
480 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
481 * but Y on platform N?
483 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
486 /* Does this interface support broadcast ? */
487 int if_is_broadcast(struct interface
*ifp
)
489 return ifp
->flags
& IFF_BROADCAST
;
492 /* Does this interface support broadcast ? */
493 int if_is_pointopoint(struct interface
*ifp
)
495 return ifp
->flags
& IFF_POINTOPOINT
;
498 /* Does this interface support multicast ? */
499 int if_is_multicast(struct interface
*ifp
)
501 return ifp
->flags
& IFF_MULTICAST
;
504 /* Printout flag information into log */
505 const char *if_flag_dump(unsigned long flag
)
508 static char logbuf
[BUFSIZ
];
510 #define IFF_OUT_LOG(X, STR) \
513 strlcat(logbuf, ",", BUFSIZ); \
516 strlcat(logbuf, STR, BUFSIZ); \
519 strlcpy(logbuf
, "<", BUFSIZ
);
520 IFF_OUT_LOG(IFF_UP
, "UP");
521 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
522 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
523 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
524 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
525 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
526 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
527 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
528 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
529 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
530 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
531 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
532 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
533 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
534 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
535 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
536 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
537 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
538 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
539 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
540 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
542 strlcat(logbuf
, ">", BUFSIZ
);
549 static void if_dump(const struct interface
*ifp
)
551 struct listnode
*node
;
552 struct connected
*c
__attribute__((unused
));
554 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
556 "Interface %s vrf %u index %d metric %d mtu %d "
558 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
,
559 ifp
->mtu
, ifp
->mtu6
, if_flag_dump(ifp
->flags
));
562 /* Interface printing for all interface. */
563 void if_dump_all(void)
566 struct listnode
*node
;
569 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
)
570 if (vrf
->iflist
!= NULL
)
571 for (ALL_LIST_ELEMENTS_RO(vrf
->iflist
, node
, p
))
575 DEFUN (interface_desc
,
577 "description LINE...",
578 "Interface specific description\n"
579 "Characters describing this interface\n")
582 VTY_DECLVAR_CONTEXT(interface
, ifp
);
585 XFREE(MTYPE_TMP
, ifp
->desc
);
586 ifp
->desc
= argv_concat(argv
, argc
, idx_line
);
591 DEFUN (no_interface_desc
,
592 no_interface_desc_cmd
,
595 "Interface specific description\n")
597 VTY_DECLVAR_CONTEXT(interface
, ifp
);
600 XFREE(MTYPE_TMP
, ifp
->desc
);
607 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
608 * a seperate struct interface for each logical interface, so config
609 * file may be full of 'interface fooX:Y'. Solaris however does not
610 * expose logical interfaces via PF_ROUTE, so trying to track logical
611 * interfaces can be fruitless, for that reason Quagga only tracks
612 * the primary IP interface.
614 * We try accomodate SUNWzebra by:
615 * - looking up the interface name, to see whether it exists, if so
617 * - for protocol daemons, this could only because zebra told us of
619 * - for zebra, only because it learnt from kernel
621 * - search the name to see if it contains a sub-ipif / logical interface
622 * seperator, the ':' char. If it does:
623 * - text up to that char must be the primary name - get that name.
625 * - no idea, just get the name in its entirety.
627 static struct interface
*if_sunwzebra_get(const char *name
, size_t nlen
,
630 struct interface
*ifp
;
633 if ((ifp
= if_lookup_by_name_len(name
, nlen
, vrf_id
)) != NULL
)
636 /* hunt the primary interface name... */
637 while (seppos
< nlen
&& name
[seppos
] != ':')
640 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
642 return if_get_by_name_len(name
, seppos
, vrf_id
, 1);
644 return if_get_by_name_len(name
, nlen
, vrf_id
, 1);
650 "interface IFNAME [vrf NAME]",
651 "Select an interface to configure\n"
657 const char *ifname
= argv
[idx_ifname
]->arg
;
658 const char *vrfname
= (argc
> 2) ? argv
[idx_vrf
]->arg
: NULL
;
660 struct interface
*ifp
;
662 vrf_id_t vrf_id
= VRF_DEFAULT
;
664 if ((sl
= strlen(ifname
)) > INTERFACE_NAMSIZ
) {
666 "%% Interface name %s is invalid: length exceeds "
668 ifname
, INTERFACE_NAMSIZ
);
669 return CMD_WARNING_CONFIG_FAILED
;
672 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
673 Imagine forward reference of a vrf by name in this interface config */
675 VRF_GET_ID(vrf_id
, vrfname
);
678 ifp
= if_sunwzebra_get(ifname
, sl
, vrf_id
);
680 ifp
= if_get_by_name_len(ifname
, sl
, vrf_id
, 1);
684 vty_out(vty
, "%% interface %s not in %s\n", ifname
, vrfname
);
685 return CMD_WARNING_CONFIG_FAILED
;
687 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
692 DEFUN_NOSH (no_interface
,
694 "no interface IFNAME [vrf NAME]",
696 "Delete a pseudo interface's configuration\n"
700 const char *ifname
= argv
[2]->arg
;
701 const char *vrfname
= (argc
> 3) ? argv
[3]->arg
: NULL
;
703 // deleting interface
704 struct interface
*ifp
;
705 vrf_id_t vrf_id
= VRF_DEFAULT
;
708 VRF_GET_ID(vrf_id
, vrfname
);
710 ifp
= if_lookup_by_name(ifname
, vrf_id
);
713 vty_out(vty
, "%% Interface %s does not exist\n", ifname
);
714 return CMD_WARNING_CONFIG_FAILED
;
717 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
718 vty_out(vty
, "%% Only inactive interfaces can be deleted\n");
719 return CMD_WARNING_CONFIG_FAILED
;
727 void if_cmd_init(void)
729 install_element(CONFIG_NODE
, &interface_cmd
);
730 install_element(CONFIG_NODE
, &no_interface_cmd
);
732 install_default(INTERFACE_NODE
);
733 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
734 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
738 /* For debug purpose. */
741 "show address [vrf NAME]",
747 struct listnode
*node
;
748 struct listnode
*node2
;
749 struct interface
*ifp
;
750 struct connected
*ifc
;
752 vrf_id_t vrf_id
= VRF_DEFAULT
;
755 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
757 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
759 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
763 if (p
->family
== AF_INET
)
764 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
770 DEFUN (show_address_vrf_all
,
771 show_address_vrf_all_cmd
,
772 "show address vrf all",
775 VRF_ALL_CMD_HELP_STR
)
778 struct listnode
*node
;
779 struct listnode
*node2
;
780 struct interface
*ifp
;
781 struct connected
*ifc
;
784 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
786 if (!vrf
->iflist
|| !listcount (vrf
->iflist
))
789 vty_out (vty
, "\nVRF %u\n\n", vrf
->vrf_id
);
791 for (ALL_LIST_ELEMENTS_RO (vrf
->iflist
, node
, ifp
))
793 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
797 if (p
->family
== AF_INET
)
798 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
806 /* Allocate connected structure. */
807 struct connected
*connected_new(void)
809 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
812 /* Allocate nbr connected structure. */
813 struct nbr_connected
*nbr_connected_new(void)
815 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
818 /* Free connected structure. */
819 void connected_free(struct connected
*connected
)
821 if (connected
->address
)
822 prefix_free(connected
->address
);
824 if (connected
->destination
)
825 prefix_free(connected
->destination
);
827 if (connected
->label
)
828 XFREE(MTYPE_CONNECTED_LABEL
, connected
->label
);
830 XFREE(MTYPE_CONNECTED
, connected
);
833 /* Free nbr connected structure. */
834 void nbr_connected_free(struct nbr_connected
*connected
)
836 if (connected
->address
)
837 prefix_free(connected
->address
);
839 XFREE(MTYPE_NBR_CONNECTED
, connected
);
842 /* If same interface nbr address already exists... */
843 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
846 struct nbr_connected
*ifc
;
847 struct listnode
*node
;
849 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
850 if (prefix_same(ifc
->address
, p
))
856 /* Print if_addr structure. */
857 static void __attribute__((unused
))
858 connected_log(struct connected
*connected
, char *str
)
861 struct interface
*ifp
;
865 ifp
= connected
->ifp
;
866 p
= connected
->address
;
868 snprintf(logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ", str
,
869 ifp
->name
, ifp
->vrf_id
, prefix_family_str(p
),
870 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
872 p
= connected
->destination
;
874 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
875 BUFSIZ
- strlen(logbuf
));
877 zlog_info("%s", logbuf
);
880 /* Print if_addr structure. */
881 static void __attribute__((unused
))
882 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
885 struct interface
*ifp
;
889 ifp
= connected
->ifp
;
890 p
= connected
->address
;
892 snprintf(logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ", str
, ifp
->name
,
893 prefix_family_str(p
),
894 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
896 zlog_info("%s", logbuf
);
899 /* If two connected address has same prefix return 1. */
900 static int connected_same_prefix(struct prefix
*p1
, struct prefix
*p2
)
902 if (p1
->family
== p2
->family
) {
903 if (p1
->family
== AF_INET
904 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
906 if (p1
->family
== AF_INET6
907 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
913 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
916 struct listnode
*node
;
917 struct listnode
*next
;
918 struct connected
*ifc
;
920 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
921 ifc
= listgetdata(node
);
924 if (connected_same_prefix(ifc
->address
, p
))
930 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
933 struct listnode
*node
;
934 struct listnode
*next
;
935 struct connected
*ifc
;
937 /* In case of same prefix come, replace it with new one. */
938 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
939 ifc
= listgetdata(node
);
942 if (connected_same_prefix(ifc
->address
, p
)) {
943 listnode_delete(ifp
->connected
, ifc
);
950 /* Find the address on our side that will be used when packets
952 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
955 struct listnode
*cnode
;
957 struct connected
*match
;
961 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
962 if (c
->address
&& (c
->address
->family
== addr
->family
)
963 && prefix_match(CONNECTED_PREFIX(c
), addr
)
965 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
971 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
973 struct prefix
*destination
)
975 struct connected
*ifc
;
977 /* Allocate new connected address. */
978 ifc
= connected_new();
981 /* Fetch interface address */
982 ifc
->address
= prefix_new();
983 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
985 /* Fetch dest address */
987 ifc
->destination
= prefix_new();
988 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
991 /* Add connected address to the interface. */
992 listnode_add(ifp
->connected
, ifc
);
996 #if 0 /* this route_table of struct connected's is unused \
997 * however, it would be good to use a route_table rather than \
1000 /* Interface looking up by interface's address. */
1001 /* Interface's IPv4 address reverse lookup table. */
1002 struct route_table
*ifaddr_ipv4_table
;
1003 /* struct route_table *ifaddr_ipv6_table; */
1006 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
1008 struct route_node
*rn
;
1009 struct prefix_ipv4 p
;
1012 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1015 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1018 route_unlock_node (rn
);
1019 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1020 inet_ntoa (*ifaddr
));
1027 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1029 struct route_node
*rn
;
1030 struct prefix_ipv4 p
;
1033 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1036 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1039 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1040 inet_ntoa (*ifaddr
));
1044 route_unlock_node (rn
);
1045 route_unlock_node (rn
);
1048 /* Lookup interface by interface's IP address or interface index. */
1049 static struct interface
*
1050 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1052 struct prefix_ipv4 p
;
1053 struct route_node
*rn
;
1054 struct interface
*ifp
;
1059 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1062 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1067 route_unlock_node (rn
);
1071 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1073 #endif /* ifaddr_ipv4_table */
1075 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
1077 struct interface
*ifp
;
1078 struct listnode
*ln
;
1079 struct vrf
*vrf
= NULL
;
1081 RB_FOREACH(vrf
, vrf_name_head
, &vrfs_by_name
)
1083 for (ALL_LIST_ELEMENTS_RO(vrf
->iflist
, ln
, ifp
))
1084 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
1088 static const struct cmd_variable_handler if_var_handlers
[] = {
1089 {/* "interface NAME" */
1090 .varname
= "interface",
1091 .completions
= if_autocomplete
},
1092 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
1093 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
1094 {.completions
= NULL
}};
1096 /* Initialize interface list. */
1097 void if_init(struct list
**intf_list
)
1099 *intf_list
= list_new();
1101 ifaddr_ipv4_table
= route_table_init ();
1102 #endif /* ifaddr_ipv4_table */
1104 (*intf_list
)->cmp
= (int (*)(void *, void *))if_cmp_func
;
1106 cmd_variable_handler_register(if_var_handlers
);
1109 void if_terminate(struct list
**intf_list
)
1112 struct interface
*ifp
;
1114 ifp
= listnode_head(*intf_list
);
1119 ifp
->node
->info
= NULL
;
1120 route_unlock_node(ifp
->node
);
1126 list_delete(*intf_list
);
1130 const char *if_link_type_str(enum zebra_link_type llt
)
1133 #define llts(T,S) case (T): return (S)
1134 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1135 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1136 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1137 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1138 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1139 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1140 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1141 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1142 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1143 llts(ZEBRA_LLT_ATM
, "ATM");
1144 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1145 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1146 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1147 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1148 llts(ZEBRA_LLT_SLIP
, "SLIP");
1149 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1150 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1151 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1152 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1153 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1154 llts(ZEBRA_LLT_PPP
, "PPP");
1155 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1156 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1157 llts(ZEBRA_LLT_LAPB
, "LAPB");
1158 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1159 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1160 llts(ZEBRA_LLT_FRAD
, "FRAD");
1161 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1162 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1163 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1164 llts(ZEBRA_LLT_FDDI
, "FDDI");
1165 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1166 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1167 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1168 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1169 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1170 llts(ZEBRA_LLT_IRDA
, "IrDA");
1171 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1172 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1173 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1174 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1175 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1176 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1177 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1178 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1179 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1181 zlog_warn("Unknown value %d", llt
);
1182 return "Unknown type!";
1188 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1192 if (ifp
->link_params
!= NULL
)
1193 return ifp
->link_params
;
1195 struct if_link_params
*iflp
=
1196 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1200 /* Set TE metric equal to standard metric */
1201 iflp
->te_metric
= ifp
->metric
;
1203 /* Compute default bandwidth based on interface */
1205 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1206 * TE_KILO_BIT
/ TE_BYTE
);
1208 /* Set Max, Reservable and Unreserved Bandwidth */
1209 iflp
->max_bw
= iflp
->default_bw
;
1210 iflp
->max_rsv_bw
= iflp
->default_bw
;
1211 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1212 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1214 /* Update Link parameters status */
1216 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1218 /* Finally attach newly created Link Parameters */
1219 ifp
->link_params
= iflp
;
1224 void if_link_params_free(struct interface
*ifp
)
1226 if (ifp
->link_params
== NULL
)
1228 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1229 ifp
->link_params
= NULL
;