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"
37 #include "northbound_cli.h"
38 #ifndef VTYSH_EXTRACT_PL
39 #include "lib/if_clippy.c"
42 DEFINE_MTYPE(LIB
, IF
, "Interface")
43 DEFINE_MTYPE_STATIC(LIB
, CONNECTED
, "Connected")
44 DEFINE_MTYPE_STATIC(LIB
, NBR_CONNECTED
, "Neighbor Connected")
45 DEFINE_MTYPE(LIB
, CONNECTED_LABEL
, "Connected interface label")
46 DEFINE_MTYPE_STATIC(LIB
, IF_LINK_PARAMS
, "Informational Link Parameters")
48 static int if_cmp_func(const struct interface
*, const struct interface
*);
49 static int if_cmp_index_func(const struct interface
*ifp1
,
50 const struct interface
*ifp2
);
51 RB_GENERATE(if_name_head
, interface
, name_entry
, if_cmp_func
);
52 RB_GENERATE(if_index_head
, interface
, index_entry
, if_cmp_index_func
);
54 DEFINE_QOBJ_TYPE(interface
)
56 DEFINE_HOOK(if_add
, (struct interface
* ifp
), (ifp
))
57 DEFINE_KOOH(if_del
, (struct interface
* ifp
), (ifp
))
59 /* Compare interface names, returning an integer greater than, equal to, or
60 * less than 0, (following the strcmp convention), according to the
61 * relationship between ifp1 and ifp2. Interface names consist of an
62 * alphabetic prefix and a numeric suffix. The primary sort key is
63 * lexicographic by name, and then numeric by number. No number sorts
64 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
67 int if_cmp_name_func(const char *p1
, const char *p2
)
74 /* look up to any number */
75 l1
= strcspn(p1
, "0123456789");
76 l2
= strcspn(p2
, "0123456789");
78 /* name lengths are different -> compare names */
80 return (strcmp(p1
, p2
));
82 /* Note that this relies on all numbers being less than all
86 res
= strncmp(p1
, p2
, l1
);
88 /* names are different -> compare them */
92 /* with identical name part, go to numeric part */
103 x1
= strtol(p1
, (char **)&p1
, 10);
104 x2
= strtol(p2
, (char **)&p2
, 10);
106 /* let's compare numbers now */
112 /* numbers were equal, lets do it again..
113 (it happens with name like "eth123.456:789") */
122 static int if_cmp_func(const struct interface
*ifp1
,
123 const struct interface
*ifp2
)
125 return if_cmp_name_func(ifp1
->name
, ifp2
->name
);
128 static int if_cmp_index_func(const struct interface
*ifp1
,
129 const struct interface
*ifp2
)
131 return ifp1
->ifindex
- ifp2
->ifindex
;
134 /* Create new interface structure. */
135 struct interface
*if_create(const char *name
, struct vrf
*vrf
)
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
));
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 * vrf must be created outside of this routing
163 void if_update_to_new_vrf(struct interface
*ifp
, struct vrf
*vrf
)
168 flog_err(EC_LIB_INTERFACE
, "interface %s. Unknown VRF",
172 /* remove interface from old master vrf list */
175 IFNAME_RB_REMOVE(old_vrf
, ifp
);
176 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
177 IFINDEX_RB_REMOVE(old_vrf
, ifp
);
182 IFNAME_RB_INSERT(vrf
, ifp
);
183 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
184 IFINDEX_RB_INSERT(vrf
, ifp
);
189 * HACK: Change the interface VRF in the running configuration directly,
190 * bypassing the northbound layer. This is necessary to avoid deleting
191 * the interface and readding it in the new VRF, which would have
192 * several implications.
194 if (yang_module_find("frr-interface")) {
195 struct lyd_node
*if_dnode
;
197 pthread_rwlock_wrlock(&running_config
->lock
);
199 if_dnode
= yang_dnode_get(
200 running_config
->dnode
,
201 "/frr-interface:lib/interface[name='%s'][vrf='%s']/vrf",
202 ifp
->name
, old_vrf
->name
);
204 yang_dnode_change_leaf(if_dnode
, vrf
->name
);
205 running_config
->version
++;
208 pthread_rwlock_unlock(&running_config
->lock
);
212 /* Delete interface structure. */
213 void if_delete_retain(struct interface
*ifp
)
215 hook_call(if_del
, ifp
);
218 /* Free connected address list */
219 list_delete_all_node(ifp
->connected
);
221 /* Free connected nbr address list */
222 list_delete_all_node(ifp
->nbr_connected
);
225 /* Delete and free interface structure. */
226 void if_delete(struct interface
*ifp
)
228 struct vrf
*vrf
= ifp
->vrf
;
232 IFNAME_RB_REMOVE(vrf
, ifp
);
233 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
234 IFINDEX_RB_REMOVE(vrf
, ifp
);
236 if_delete_retain(ifp
);
238 list_delete(&ifp
->connected
);
239 list_delete(&ifp
->nbr_connected
);
241 if_link_params_free(ifp
);
243 XFREE(MTYPE_TMP
, ifp
->desc
);
245 XFREE(MTYPE_IF
, ifp
);
248 /* Interface existance check by index. */
249 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
252 struct interface if_tmp
;
254 vrf
= vrf_lookup_by_id(vrf_id
);
258 if_tmp
.ifindex
= ifindex
;
259 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
262 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
264 struct interface
*ifp
;
266 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
271 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
273 struct interface
*ifp
;
274 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
276 return ((ifp
= if_lookup_by_name(name
, vrf
)) != NULL
)
281 /* Interface existance check by interface name. */
282 struct interface
*if_lookup_by_name(const char *name
, struct vrf
*vrf
)
284 struct interface if_tmp
;
287 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
290 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
291 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
294 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
297 struct interface
*ifp
;
299 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
302 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
303 ifp
= if_lookup_by_name(name
, vrf
);
311 /* Lookup interface by IPv4 address. */
312 struct interface
*if_lookup_exact_address(void *src
, int family
,
315 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
316 struct listnode
*cnode
;
317 struct interface
*ifp
;
321 FOR_ALL_INTERFACES (vrf
, ifp
) {
322 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
325 if (p
&& (p
->family
== family
)) {
326 if (family
== AF_INET
) {
329 (struct in_addr
*)src
))
331 } else if (family
== AF_INET6
) {
334 (struct in6_addr
*)src
))
343 /* Lookup interface by IPv4 address. */
344 struct connected
*if_lookup_address(void *matchaddr
, int family
,
347 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
350 struct listnode
*cnode
;
351 struct interface
*ifp
;
353 struct connected
*match
;
355 if (family
== AF_INET
) {
356 addr
.family
= AF_INET
;
357 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
358 addr
.prefixlen
= IPV4_MAX_BITLEN
;
359 } else if (family
== AF_INET6
) {
360 addr
.family
= AF_INET6
;
361 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
362 addr
.prefixlen
= IPV6_MAX_BITLEN
;
367 FOR_ALL_INTERFACES (vrf
, ifp
) {
368 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
369 if (c
->address
&& (c
->address
->family
== AF_INET
)
370 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
371 && (c
->address
->prefixlen
> bestlen
)) {
372 bestlen
= c
->address
->prefixlen
;
380 /* Lookup interface by prefix */
381 struct interface
*if_lookup_prefix(struct prefix
*prefix
, vrf_id_t vrf_id
)
383 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
384 struct listnode
*cnode
;
385 struct interface
*ifp
;
388 FOR_ALL_INTERFACES (vrf
, ifp
) {
389 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
390 if (prefix_cmp(c
->address
, prefix
) == 0) {
398 size_t if_lookup_by_hwaddr(const uint8_t *hw_addr
, size_t addrsz
,
399 struct interface
***result
, vrf_id_t vrf_id
)
401 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
403 struct list
*rs
= list_new();
404 struct interface
*ifp
;
406 FOR_ALL_INTERFACES (vrf
, ifp
) {
407 if (ifp
->hw_addr_len
== (int)addrsz
408 && !memcmp(hw_addr
, ifp
->hw_addr
, addrsz
))
409 listnode_add(rs
, ifp
);
413 *result
= XCALLOC(MTYPE_TMP
,
414 sizeof(struct interface
*) * rs
->count
);
415 list_to_array(rs
, (void **)*result
, rs
->count
);
418 int count
= rs
->count
;
426 /* Get interface by name if given name interface doesn't exist create
428 struct interface
*if_get_by_name(const char *name
, struct vrf
*vrf
)
430 struct interface
*ifp
;
432 switch (vrf_get_backend()) {
433 case VRF_BACKEND_UNKNOWN
:
434 case VRF_BACKEND_NETNS
:
435 ifp
= if_lookup_by_name(name
, vrf
);
438 return if_create(name
, vrf
);
439 case VRF_BACKEND_VRF_LITE
:
440 ifp
= if_lookup_by_name_all_vrf(name
);
444 /* If it came from the kernel or by way of zclient,
445 * believe it and update the ifp accordingly.
447 if_update_to_new_vrf(ifp
, vrf
);
450 return if_create(name
, vrf
);
456 void if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
463 if (ifp
->ifindex
== ifindex
)
466 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
467 IFINDEX_RB_REMOVE(vrf
, ifp
)
469 ifp
->ifindex
= ifindex
;
471 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
472 IFINDEX_RB_INSERT(vrf
, ifp
)
475 /* Does interface up ? */
476 int if_is_up(const struct interface
*ifp
)
478 return ifp
->flags
& IFF_UP
;
481 /* Is interface running? */
482 int if_is_running(const struct interface
*ifp
)
484 return ifp
->flags
& IFF_RUNNING
;
487 /* Is the interface operative, eg. either UP & RUNNING
488 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
489 if ptm checking is enabled, then ptm check has passed */
490 int if_is_operative(const struct interface
*ifp
)
492 return ((ifp
->flags
& IFF_UP
)
493 && (((ifp
->flags
& IFF_RUNNING
)
494 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
495 || !CHECK_FLAG(ifp
->status
,
496 ZEBRA_INTERFACE_LINKDETECTION
)));
499 /* Is the interface operative, eg. either UP & RUNNING
500 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
501 int if_is_no_ptm_operative(const struct interface
*ifp
)
503 return ((ifp
->flags
& IFF_UP
)
504 && ((ifp
->flags
& IFF_RUNNING
)
505 || !CHECK_FLAG(ifp
->status
,
506 ZEBRA_INTERFACE_LINKDETECTION
)));
509 /* Is this loopback interface ? */
510 int if_is_loopback(const struct interface
*ifp
)
512 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
513 * but Y on platform N?
515 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
518 /* Check interface is VRF */
519 int if_is_vrf(const struct interface
*ifp
)
521 return CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
524 bool if_is_loopback_or_vrf(const struct interface
*ifp
)
526 if (if_is_loopback(ifp
) || if_is_vrf(ifp
))
532 /* Does this interface support broadcast ? */
533 int if_is_broadcast(const struct interface
*ifp
)
535 return ifp
->flags
& IFF_BROADCAST
;
538 /* Does this interface support broadcast ? */
539 int if_is_pointopoint(const struct interface
*ifp
)
541 return ifp
->flags
& IFF_POINTOPOINT
;
544 /* Does this interface support multicast ? */
545 int if_is_multicast(const struct interface
*ifp
)
547 return ifp
->flags
& IFF_MULTICAST
;
550 /* Printout flag information into log */
551 const char *if_flag_dump(unsigned long flag
)
554 static char logbuf
[BUFSIZ
];
556 #define IFF_OUT_LOG(X, STR) \
559 strlcat(logbuf, ",", sizeof(logbuf)); \
562 strlcat(logbuf, STR, sizeof(logbuf)); \
565 strlcpy(logbuf
, "<", BUFSIZ
);
566 IFF_OUT_LOG(IFF_UP
, "UP");
567 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
568 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
569 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
570 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
571 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
572 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
573 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
574 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
575 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
576 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
577 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
578 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
579 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
580 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
581 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
582 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
583 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
584 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
585 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
586 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
588 strlcat(logbuf
, ">", sizeof(logbuf
));
595 static void if_dump(const struct interface
*ifp
)
597 struct listnode
*node
;
598 struct connected
*c
__attribute__((unused
));
600 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
602 "Interface %s vrf %u index %d metric %d mtu %d "
604 ifp
->name
, vrf_to_id(ifp
->vrf
),
605 ifp
->ifindex
, ifp
->metric
,
606 ifp
->mtu
, ifp
->mtu6
, if_flag_dump(ifp
->flags
));
609 /* Interface printing for all interface. */
610 void if_dump_all(void)
615 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
616 FOR_ALL_INTERFACES (vrf
, ifp
)
621 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
622 * a seperate struct interface for each logical interface, so config
623 * file may be full of 'interface fooX:Y'. Solaris however does not
624 * expose logical interfaces via PF_ROUTE, so trying to track logical
625 * interfaces can be fruitless, for that reason Quagga only tracks
626 * the primary IP interface.
628 * We try accomodate SUNWzebra by:
629 * - looking up the interface name, to see whether it exists, if so
631 * - for protocol daemons, this could only because zebra told us of
633 * - for zebra, only because it learnt from kernel
635 * - search the name to see if it contains a sub-ipif / logical interface
636 * seperator, the ':' char. If it does:
637 * - text up to that char must be the primary name - get that name.
639 * - no idea, just get the name in its entirety.
641 static struct interface
*if_sunwzebra_get(const char *name
, struct vrf
*vrf
)
643 struct interface
*ifp
;
646 if ((ifp
= if_lookup_by_name(name
, vrf
)) != NULL
)
649 /* hunt the primary interface name... */
650 cp
= strchr(name
, ':');
654 return if_get_by_name(name
, vrf
);
659 /* For debug purpose. */
662 "show address [vrf NAME]",
668 struct listnode
*node
;
669 struct interface
*ifp
;
670 struct connected
*ifc
;
672 vrf_id_t vrf_id
= VRF_DEFAULT
;
675 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
677 FOR_ALL_INTERFACES (vrf
, ifp
)
679 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
683 if (p
->family
== AF_INET
)
684 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
690 DEFUN (show_address_vrf_all
,
691 show_address_vrf_all_cmd
,
692 "show address vrf all",
695 VRF_ALL_CMD_HELP_STR
)
698 struct listnode
*node
;
699 struct interface
*ifp
;
700 struct connected
*ifc
;
703 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
705 if (RB_EMPTY (if_name_head
, &vrf
->ifaces_by_name
))
708 vty_out (vty
, "\nVRF %u\n\n", vrf
->vrf_id
);
710 FOR_ALL_INTERFACES (vrf
, ifp
)
712 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
716 if (p
->family
== AF_INET
)
717 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
725 /* Allocate connected structure. */
726 struct connected
*connected_new(void)
728 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
731 /* Allocate nbr connected structure. */
732 struct nbr_connected
*nbr_connected_new(void)
734 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
737 /* Free connected structure. */
738 void connected_free(struct connected
*connected
)
740 if (connected
->address
)
741 prefix_free(connected
->address
);
743 if (connected
->destination
)
744 prefix_free(connected
->destination
);
746 XFREE(MTYPE_CONNECTED_LABEL
, connected
->label
);
748 XFREE(MTYPE_CONNECTED
, connected
);
751 /* Free nbr connected structure. */
752 void nbr_connected_free(struct nbr_connected
*connected
)
754 if (connected
->address
)
755 prefix_free(connected
->address
);
757 XFREE(MTYPE_NBR_CONNECTED
, connected
);
760 /* If same interface nbr address already exists... */
761 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
764 struct nbr_connected
*ifc
;
765 struct listnode
*node
;
767 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
768 if (prefix_same(ifc
->address
, p
))
774 /* Print if_addr structure. */
775 static void __attribute__((unused
))
776 connected_log(struct connected
*connected
, char *str
)
779 struct interface
*ifp
;
783 ifp
= connected
->ifp
;
784 p
= connected
->address
;
786 snprintf(logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ", str
,
787 ifp
->name
, vrf_to_id(ifp
->vrf
),
788 prefix_family_str(p
),
789 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
791 p
= connected
->destination
;
793 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
794 BUFSIZ
- strlen(logbuf
));
796 zlog_info("%s", logbuf
);
799 /* Print if_addr structure. */
800 static void __attribute__((unused
))
801 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
804 struct interface
*ifp
;
808 ifp
= connected
->ifp
;
809 p
= connected
->address
;
811 snprintf(logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ", str
, ifp
->name
,
812 prefix_family_str(p
),
813 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
815 zlog_info("%s", logbuf
);
818 /* If two connected address has same prefix return 1. */
819 static int connected_same_prefix(struct prefix
*p1
, struct prefix
*p2
)
821 if (p1
->family
== p2
->family
) {
822 if (p1
->family
== AF_INET
823 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
825 if (p1
->family
== AF_INET6
826 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
832 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
835 struct listnode
*node
;
836 struct listnode
*next
;
837 struct connected
*ifc
;
839 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
840 ifc
= listgetdata(node
);
843 if (connected_same_prefix(ifc
->address
, p
))
849 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
852 struct listnode
*node
;
853 struct listnode
*next
;
854 struct connected
*ifc
;
856 /* In case of same prefix come, replace it with new one. */
857 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
858 ifc
= listgetdata(node
);
861 if (connected_same_prefix(ifc
->address
, p
)) {
862 listnode_delete(ifp
->connected
, ifc
);
869 /* Find the address on our side that will be used when packets
871 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
874 struct listnode
*cnode
;
876 struct connected
*match
;
880 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
881 if (c
->address
&& (c
->address
->family
== addr
->family
)
882 && prefix_match(CONNECTED_PREFIX(c
), addr
)
884 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
890 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
892 struct prefix
*destination
)
894 struct connected
*ifc
;
896 /* Allocate new connected address. */
897 ifc
= connected_new();
900 /* Fetch interface address */
901 ifc
->address
= prefix_new();
902 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
904 /* Fetch dest address */
906 ifc
->destination
= prefix_new();
907 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
910 /* Add connected address to the interface. */
911 listnode_add(ifp
->connected
, ifc
);
915 struct connected
*connected_get_linklocal(struct interface
*ifp
)
918 struct connected
*c
= NULL
;
920 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, n
, c
)) {
921 if (c
->address
->family
== AF_INET6
922 && IN6_IS_ADDR_LINKLOCAL(&c
->address
->u
.prefix6
))
928 #if 0 /* this route_table of struct connected's is unused \
929 * however, it would be good to use a route_table rather than \
932 /* Interface looking up by interface's address. */
933 /* Interface's IPv4 address reverse lookup table. */
934 struct route_table
*ifaddr_ipv4_table
;
935 /* struct route_table *ifaddr_ipv6_table; */
938 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
940 struct route_node
*rn
;
941 struct prefix_ipv4 p
;
944 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
947 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
950 route_unlock_node (rn
);
951 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
952 inet_ntoa (*ifaddr
));
959 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
961 struct route_node
*rn
;
962 struct prefix_ipv4 p
;
965 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
968 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
971 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
972 inet_ntoa (*ifaddr
));
976 route_unlock_node (rn
);
977 route_unlock_node (rn
);
980 /* Lookup interface by interface's IP address or interface index. */
981 static struct interface
*
982 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
984 struct prefix_ipv4 p
;
985 struct route_node
*rn
;
986 struct interface
*ifp
;
991 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
994 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
999 route_unlock_node (rn
);
1003 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1005 #endif /* ifaddr_ipv4_table */
1007 void if_terminate(struct vrf
*vrf
)
1009 struct interface
*ifp
;
1011 while (!RB_EMPTY(if_name_head
, &vrf
->ifaces_by_name
)) {
1012 ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
);
1015 ifp
->node
->info
= NULL
;
1016 route_unlock_node(ifp
->node
);
1022 const char *if_link_type_str(enum zebra_link_type llt
)
1025 #define llts(T,S) case (T): return (S)
1026 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1027 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1028 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1029 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1030 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1031 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1032 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1033 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1034 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1035 llts(ZEBRA_LLT_ATM
, "ATM");
1036 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1037 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1038 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1039 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1040 llts(ZEBRA_LLT_SLIP
, "SLIP");
1041 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1042 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1043 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1044 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1045 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1046 llts(ZEBRA_LLT_PPP
, "PPP");
1047 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1048 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1049 llts(ZEBRA_LLT_LAPB
, "LAPB");
1050 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1051 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1052 llts(ZEBRA_LLT_FRAD
, "FRAD");
1053 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1054 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1055 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1056 llts(ZEBRA_LLT_FDDI
, "FDDI");
1057 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1058 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1059 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1060 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1061 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1062 llts(ZEBRA_LLT_IRDA
, "IrDA");
1063 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1064 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1065 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1066 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1067 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1068 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1069 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1070 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1071 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1073 flog_err(EC_LIB_DEVELOPMENT
, "Unknown value %d", llt
);
1074 return "Unknown type!";
1080 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1084 if (ifp
->link_params
!= NULL
)
1085 return ifp
->link_params
;
1087 struct if_link_params
*iflp
=
1088 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1092 /* Set TE metric equal to standard metric */
1093 iflp
->te_metric
= ifp
->metric
;
1095 /* Compute default bandwidth based on interface */
1097 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1098 * TE_KILO_BIT
/ TE_BYTE
);
1100 /* Set Max, Reservable and Unreserved Bandwidth */
1101 iflp
->max_bw
= iflp
->default_bw
;
1102 iflp
->max_rsv_bw
= iflp
->default_bw
;
1103 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1104 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1106 /* Update Link parameters status */
1108 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1110 /* Finally attach newly created Link Parameters */
1111 ifp
->link_params
= iflp
;
1116 void if_link_params_free(struct interface
*ifp
)
1118 if (ifp
->link_params
== NULL
)
1120 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1121 ifp
->link_params
= NULL
;
1124 /* ----------- CLI commands ----------- */
1127 * XPath: /frr-interface:lib/interface
1129 DEFPY_NOSH (interface
,
1131 "interface IFNAME [vrf NAME$vrfname]",
1132 "Select an interface to configure\n"
1133 "Interface's name\n"
1136 char xpath_list
[XPATH_MAXLEN
];
1137 struct interface
*ifp
= NULL
;
1142 vrfname
= VRF_DEFAULT_NAME
;
1145 * This command requires special handling to maintain backward
1146 * compatibility. If a VRF name is not specified, it means we're willing
1147 * to accept any interface with the given name on any VRF. If no
1148 * interface is found, then a new one should be created on the default
1151 VRF_GET_INSTANCE(vrf
, vrfname
, false, true);
1153 * within vrf context, vrf_id may be unknown
1154 * this happens on daemons relying on zebra
1155 * on this specific case, interface creation may
1158 if (vrf
&& (vrf
->vrf_id
== VRF_UNKNOWN
||
1159 vrf_get_backend() == VRF_BACKEND_UNKNOWN
))
1160 ifp
= if_lookup_by_name(ifname
, vrf
);
1162 ifp
= if_lookup_by_name_all_vrf(ifname
);
1163 if (ifp
&& ifp
->vrf
!= vrf
) {
1165 * Special case 1: a VRF name was specified, but the found
1166 * interface is associated to different VRF. Reject the command.
1168 if (vrf
->vrf_id
!= VRF_DEFAULT
) {
1169 vty_out(vty
, "%% interface %s not in %s vrf\n", ifname
,
1171 return CMD_WARNING_CONFIG_FAILED
;
1175 * Special case 2: a VRF name was *not* specified, and the found
1176 * interface is associated to a VRF other than the default one.
1177 * Update vrf_id and vrfname to account for that.
1181 vrfname
= vrf
->name
;
1184 snprintf(xpath_list
, sizeof(xpath_list
),
1185 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname
,
1188 nb_cli_enqueue_change(vty
, ".", NB_OP_CREATE
, NULL
);
1189 ret
= nb_cli_apply_changes(vty
, xpath_list
);
1190 if (ret
== CMD_SUCCESS
) {
1191 VTY_PUSH_XPATH(INTERFACE_NODE
, xpath_list
);
1194 * For backward compatibility with old commands we still need
1195 * to use the qobj infrastructure. This can be removed once
1196 * all interface-level commands are converted to the new
1199 ifp
= if_lookup_by_name(ifname
, vrf
);
1201 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
1207 DEFPY (no_interface
,
1209 "no interface IFNAME [vrf NAME$vrfname]",
1211 "Delete a pseudo interface's configuration\n"
1212 "Interface's name\n"
1216 vrfname
= VRF_DEFAULT_NAME
;
1218 nb_cli_enqueue_change(vty
, ".", NB_OP_DESTROY
, NULL
);
1220 return nb_cli_apply_changes(
1221 vty
, "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1225 static void cli_show_interface(struct vty
*vty
, struct lyd_node
*dnode
,
1230 vrf
= yang_dnode_get_string(dnode
, "./vrf");
1232 vty_out(vty
, "!\n");
1233 vty_out(vty
, "interface %s", yang_dnode_get_string(dnode
, "./name"));
1234 if (!strmatch(vrf
, VRF_DEFAULT_NAME
))
1235 vty_out(vty
, " vrf %s", vrf
);
1240 * XPath: /frr-interface:lib/interface/description
1242 DEFPY (interface_desc
,
1244 "description LINE...",
1245 "Interface specific description\n"
1246 "Characters describing this interface\n")
1251 desc
= argv_concat(argv
, argc
, 1);
1252 nb_cli_enqueue_change(vty
, "./description", NB_OP_MODIFY
, desc
);
1253 ret
= nb_cli_apply_changes(vty
, NULL
);
1254 XFREE(MTYPE_TMP
, desc
);
1259 DEFPY (no_interface_desc
,
1260 no_interface_desc_cmd
,
1263 "Interface specific description\n")
1265 nb_cli_enqueue_change(vty
, "./description", NB_OP_DESTROY
, NULL
);
1267 return nb_cli_apply_changes(vty
, NULL
);
1270 static void cli_show_interface_desc(struct vty
*vty
, struct lyd_node
*dnode
,
1273 vty_out(vty
, " description %s\n", yang_dnode_get_string(dnode
, NULL
));
1276 /* Interface autocomplete. */
1277 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
1279 struct interface
*ifp
;
1282 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1283 FOR_ALL_INTERFACES (vrf
, ifp
) {
1284 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
1289 static const struct cmd_variable_handler if_var_handlers
[] = {
1290 {/* "interface NAME" */
1291 .varname
= "interface",
1292 .completions
= if_autocomplete
},
1293 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
1294 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
1295 {.completions
= NULL
}};
1297 void if_cmd_init(void)
1299 cmd_variable_handler_register(if_var_handlers
);
1301 install_element(CONFIG_NODE
, &interface_cmd
);
1302 install_element(CONFIG_NODE
, &no_interface_cmd
);
1304 install_default(INTERFACE_NODE
);
1305 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
1306 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
1309 /* ------- Northbound callbacks ------- */
1312 * XPath: /frr-interface:lib/interface
1314 static int lib_interface_create(enum nb_event event
,
1315 const struct lyd_node
*dnode
,
1316 union nb_resource
*resource
)
1319 const char *vrfname
;
1321 struct interface
*ifp
= NULL
;
1323 ifname
= yang_dnode_get_string(dnode
, "./name");
1324 vrfname
= yang_dnode_get_string(dnode
, "./vrf");
1327 case NB_EV_VALIDATE
:
1328 vrf
= vrf_lookup_by_name(vrfname
);
1330 zlog_warn("%s: VRF %s doesn't exist", __func__
,
1332 return NB_ERR_VALIDATION
;
1334 if (vrf
->vrf_id
== VRF_UNKNOWN
)
1335 zlog_warn("%s: VRF %s is not active. Using interface however.",
1336 __func__
, vrf
->name
);
1338 /* if VRF is netns or not yet known - init for instance
1339 * then assumption is that passed config is exact
1340 * then the user intent was not to use an other iface
1342 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE
) {
1343 ifp
= if_lookup_by_name_all_vrf(ifname
);
1344 if (ifp
&& ifp
->vrf
!= vrf
) {
1346 "%s: interface %s already exists in another VRF",
1347 __func__
, ifp
->name
);
1348 return NB_ERR_VALIDATION
;
1356 vrf
= vrf_lookup_by_name(vrfname
);
1359 ifp
= if_sunwzebra_get(ifname
, vrf
);
1361 ifp
= if_get_by_name(ifname
, vrf
);
1362 #endif /* SUNOS_5 */
1363 nb_running_set_entry(dnode
, ifp
);
1370 static int lib_interface_destroy(enum nb_event event
,
1371 const struct lyd_node
*dnode
)
1373 struct interface
*ifp
;
1377 case NB_EV_VALIDATE
:
1378 ifp
= nb_running_get_entry(dnode
, NULL
, true);
1379 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1380 zlog_warn("%s: only inactive interfaces can be deleted",
1382 return NB_ERR_VALIDATION
;
1389 ifp
= nb_running_unset_entry(dnode
);
1398 * XPath: /frr-interface:lib/interface/description
1400 static int lib_interface_description_modify(enum nb_event event
,
1401 const struct lyd_node
*dnode
,
1402 union nb_resource
*resource
)
1404 struct interface
*ifp
;
1405 const char *description
;
1407 if (event
!= NB_EV_APPLY
)
1410 ifp
= nb_running_get_entry(dnode
, NULL
, true);
1411 XFREE(MTYPE_TMP
, ifp
->desc
);
1412 description
= yang_dnode_get_string(dnode
, NULL
);
1413 ifp
->desc
= XSTRDUP(MTYPE_TMP
, description
);
1418 static int lib_interface_description_destroy(enum nb_event event
,
1419 const struct lyd_node
*dnode
)
1421 struct interface
*ifp
;
1423 if (event
!= NB_EV_APPLY
)
1426 ifp
= nb_running_get_entry(dnode
, NULL
, true);
1427 XFREE(MTYPE_TMP
, ifp
->desc
);
1432 /* clang-format off */
1433 const struct frr_yang_module_info frr_interface_info
= {
1434 .name
= "frr-interface",
1437 .xpath
= "/frr-interface:lib/interface",
1438 .cbs
.create
= lib_interface_create
,
1439 .cbs
.destroy
= lib_interface_destroy
,
1440 .cbs
.cli_show
= cli_show_interface
,
1443 .xpath
= "/frr-interface:lib/interface/description",
1444 .cbs
.modify
= lib_interface_description_modify
,
1445 .cbs
.destroy
= lib_interface_description_destroy
,
1446 .cbs
.cli_show
= cli_show_interface_desc
,