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_STATIC(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
, vrf_id_t vrf_id
)
137 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
138 struct interface
*ifp
;
140 ifp
= XCALLOC(MTYPE_IF
, sizeof(struct interface
));
141 ifp
->ifindex
= IFINDEX_INTERNAL
;
144 strlcpy(ifp
->name
, name
, sizeof(ifp
->name
));
145 ifp
->vrf_id
= vrf_id
;
146 IFNAME_RB_INSERT(vrf
, ifp
);
147 ifp
->connected
= list_new();
148 ifp
->connected
->del
= (void (*)(void *))connected_free
;
150 ifp
->nbr_connected
= list_new();
151 ifp
->nbr_connected
->del
= (void (*)(void *))nbr_connected_free
;
153 /* Enable Link-detection by default */
154 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
156 QOBJ_REG(ifp
, interface
);
157 hook_call(if_add
, ifp
);
161 /* Create new interface structure. */
162 void if_update_to_new_vrf(struct interface
*ifp
, vrf_id_t vrf_id
)
164 struct vrf
*old_vrf
, *vrf
;
166 /* remove interface from old master vrf list */
167 old_vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
169 IFNAME_RB_REMOVE(old_vrf
, ifp
);
170 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
171 IFINDEX_RB_REMOVE(old_vrf
, ifp
);
174 ifp
->vrf_id
= vrf_id
;
175 vrf
= vrf_get(ifp
->vrf_id
, NULL
);
177 IFNAME_RB_INSERT(vrf
, ifp
);
178 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
179 IFINDEX_RB_INSERT(vrf
, ifp
);
182 * HACK: Change the interface VRF in the running configuration directly,
183 * bypassing the northbound layer. This is necessary to avoid deleting
184 * the interface and readding it in the new VRF, which would have
185 * several implications.
187 if (yang_module_find("frr-interface")) {
188 struct lyd_node
*if_dnode
;
190 pthread_rwlock_wrlock(&running_config
->lock
);
192 if_dnode
= yang_dnode_get(
193 running_config
->dnode
,
194 "/frr-interface:lib/interface[name='%s'][vrf='%s']/vrf",
195 ifp
->name
, old_vrf
->name
);
197 yang_dnode_change_leaf(if_dnode
, vrf
->name
);
198 running_config
->version
++;
201 pthread_rwlock_unlock(&running_config
->lock
);
205 /* Delete interface structure. */
206 void if_delete_retain(struct interface
*ifp
)
208 hook_call(if_del
, ifp
);
211 /* Free connected address list */
212 list_delete_all_node(ifp
->connected
);
214 /* Free connected nbr address list */
215 list_delete_all_node(ifp
->nbr_connected
);
218 /* Delete and free interface structure. */
219 void if_delete(struct interface
*ifp
)
223 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
226 IFNAME_RB_REMOVE(vrf
, ifp
);
227 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
228 IFINDEX_RB_REMOVE(vrf
, ifp
);
230 if_delete_retain(ifp
);
232 list_delete(&ifp
->connected
);
233 list_delete(&ifp
->nbr_connected
);
235 if_link_params_free(ifp
);
237 XFREE(MTYPE_TMP
, ifp
->desc
);
239 XFREE(MTYPE_IF
, ifp
);
242 /* Interface existance check by index. */
243 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
246 struct interface if_tmp
;
248 vrf
= vrf_lookup_by_id(vrf_id
);
252 if_tmp
.ifindex
= ifindex
;
253 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
256 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
258 struct interface
*ifp
;
260 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
265 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
267 struct interface
*ifp
;
269 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
274 /* Interface existance check by interface name. */
275 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
277 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
278 struct interface if_tmp
;
281 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
284 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
285 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
288 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
291 struct interface
*ifp
;
293 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
296 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
297 ifp
= if_lookup_by_name(name
, vrf
->vrf_id
);
305 /* Lookup interface by IPv4 address. */
306 struct interface
*if_lookup_exact_address(void *src
, int family
,
309 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
310 struct listnode
*cnode
;
311 struct interface
*ifp
;
315 FOR_ALL_INTERFACES (vrf
, ifp
) {
316 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
319 if (p
&& (p
->family
== family
)) {
320 if (family
== AF_INET
) {
323 (struct in_addr
*)src
))
325 } else if (family
== AF_INET6
) {
328 (struct in6_addr
*)src
))
337 /* Lookup interface by IPv4 address. */
338 struct connected
*if_lookup_address(void *matchaddr
, int family
,
341 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
344 struct listnode
*cnode
;
345 struct interface
*ifp
;
347 struct connected
*match
;
349 if (family
== AF_INET
) {
350 addr
.family
= AF_INET
;
351 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
352 addr
.prefixlen
= IPV4_MAX_BITLEN
;
353 } else if (family
== AF_INET6
) {
354 addr
.family
= AF_INET6
;
355 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
356 addr
.prefixlen
= IPV6_MAX_BITLEN
;
361 FOR_ALL_INTERFACES (vrf
, ifp
) {
362 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
363 if (c
->address
&& (c
->address
->family
== AF_INET
)
364 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
365 && (c
->address
->prefixlen
> bestlen
)) {
366 bestlen
= c
->address
->prefixlen
;
374 /* Lookup interface by prefix */
375 struct interface
*if_lookup_prefix(struct prefix
*prefix
, vrf_id_t vrf_id
)
377 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
378 struct listnode
*cnode
;
379 struct interface
*ifp
;
382 FOR_ALL_INTERFACES (vrf
, ifp
) {
383 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
384 if (prefix_cmp(c
->address
, prefix
) == 0) {
392 size_t if_lookup_by_hwaddr(const uint8_t *hw_addr
, size_t addrsz
,
393 struct interface
***result
, vrf_id_t vrf_id
)
395 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
397 struct list
*rs
= list_new();
398 struct interface
*ifp
;
400 FOR_ALL_INTERFACES (vrf
, ifp
) {
401 if (ifp
->hw_addr_len
== (int)addrsz
402 && !memcmp(hw_addr
, ifp
->hw_addr
, addrsz
))
403 listnode_add(rs
, ifp
);
407 *result
= XCALLOC(MTYPE_TMP
,
408 sizeof(struct interface
*) * rs
->count
);
409 list_to_array(rs
, (void **)*result
, rs
->count
);
412 int count
= rs
->count
;
420 /* Get interface by name if given name interface doesn't exist create
422 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
)
424 struct interface
*ifp
;
426 switch (vrf_get_backend()) {
427 case VRF_BACKEND_UNKNOWN
:
428 case VRF_BACKEND_NETNS
:
429 ifp
= if_lookup_by_name(name
, vrf_id
);
432 return if_create(name
, vrf_id
);
433 case VRF_BACKEND_VRF_LITE
:
434 ifp
= if_lookup_by_name_all_vrf(name
);
436 if (ifp
->vrf_id
== vrf_id
)
438 /* If it came from the kernel or by way of zclient,
439 * believe it and update the ifp accordingly.
441 if_update_to_new_vrf(ifp
, vrf_id
);
444 return if_create(name
, vrf_id
);
450 void if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
454 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
457 if (ifp
->ifindex
== ifindex
)
460 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
461 IFINDEX_RB_REMOVE(vrf
, ifp
)
463 ifp
->ifindex
= ifindex
;
465 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
466 IFINDEX_RB_INSERT(vrf
, ifp
)
469 /* Does interface up ? */
470 int if_is_up(const struct interface
*ifp
)
472 return ifp
->flags
& IFF_UP
;
475 /* Is interface running? */
476 int if_is_running(const struct interface
*ifp
)
478 return ifp
->flags
& IFF_RUNNING
;
481 /* Is the interface operative, eg. either UP & RUNNING
482 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
483 if ptm checking is enabled, then ptm check has passed */
484 int if_is_operative(const struct interface
*ifp
)
486 return ((ifp
->flags
& IFF_UP
)
487 && (((ifp
->flags
& IFF_RUNNING
)
488 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
489 || !CHECK_FLAG(ifp
->status
,
490 ZEBRA_INTERFACE_LINKDETECTION
)));
493 /* Is the interface operative, eg. either UP & RUNNING
494 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
495 int if_is_no_ptm_operative(const struct interface
*ifp
)
497 return ((ifp
->flags
& IFF_UP
)
498 && ((ifp
->flags
& IFF_RUNNING
)
499 || !CHECK_FLAG(ifp
->status
,
500 ZEBRA_INTERFACE_LINKDETECTION
)));
503 /* Is this loopback interface ? */
504 int if_is_loopback(const struct interface
*ifp
)
506 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
507 * but Y on platform N?
509 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
512 /* Check interface is VRF */
513 int if_is_vrf(const struct interface
*ifp
)
515 return CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
518 bool if_is_loopback_or_vrf(const struct interface
*ifp
)
520 if (if_is_loopback(ifp
) || if_is_vrf(ifp
))
526 /* Does this interface support broadcast ? */
527 int if_is_broadcast(const struct interface
*ifp
)
529 return ifp
->flags
& IFF_BROADCAST
;
532 /* Does this interface support broadcast ? */
533 int if_is_pointopoint(const struct interface
*ifp
)
535 return ifp
->flags
& IFF_POINTOPOINT
;
538 /* Does this interface support multicast ? */
539 int if_is_multicast(const struct interface
*ifp
)
541 return ifp
->flags
& IFF_MULTICAST
;
544 /* Printout flag information into log */
545 const char *if_flag_dump(unsigned long flag
)
548 static char logbuf
[BUFSIZ
];
550 #define IFF_OUT_LOG(X, STR) \
553 strlcat(logbuf, ",", sizeof(logbuf)); \
556 strlcat(logbuf, STR, sizeof(logbuf)); \
559 strlcpy(logbuf
, "<", BUFSIZ
);
560 IFF_OUT_LOG(IFF_UP
, "UP");
561 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
562 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
563 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
564 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
565 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
566 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
567 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
568 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
569 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
570 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
571 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
572 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
573 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
574 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
575 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
576 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
577 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
578 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
579 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
580 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
582 strlcat(logbuf
, ">", sizeof(logbuf
));
589 static void if_dump(const struct interface
*ifp
)
591 struct listnode
*node
;
592 struct connected
*c
__attribute__((unused
));
594 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
596 "Interface %s vrf %u index %d metric %d mtu %d "
598 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
,
599 ifp
->mtu
, ifp
->mtu6
, if_flag_dump(ifp
->flags
));
602 /* Interface printing for all interface. */
603 void if_dump_all(void)
608 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
609 FOR_ALL_INTERFACES (vrf
, ifp
)
614 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
615 * a seperate struct interface for each logical interface, so config
616 * file may be full of 'interface fooX:Y'. Solaris however does not
617 * expose logical interfaces via PF_ROUTE, so trying to track logical
618 * interfaces can be fruitless, for that reason Quagga only tracks
619 * the primary IP interface.
621 * We try accomodate SUNWzebra by:
622 * - looking up the interface name, to see whether it exists, if so
624 * - for protocol daemons, this could only because zebra told us of
626 * - for zebra, only because it learnt from kernel
628 * - search the name to see if it contains a sub-ipif / logical interface
629 * seperator, the ':' char. If it does:
630 * - text up to that char must be the primary name - get that name.
632 * - no idea, just get the name in its entirety.
634 static struct interface
*if_sunwzebra_get(const char *name
, vrf_id_t vrf_id
)
636 struct interface
*ifp
;
639 if ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
642 /* hunt the primary interface name... */
643 cp
= strchr(name
, ':');
647 return if_get_by_name(name
, vrf_id
);
652 /* For debug purpose. */
655 "show address [vrf NAME]",
661 struct listnode
*node
;
662 struct interface
*ifp
;
663 struct connected
*ifc
;
665 vrf_id_t vrf_id
= VRF_DEFAULT
;
668 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
670 FOR_ALL_INTERFACES (vrf
, ifp
)
672 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
676 if (p
->family
== AF_INET
)
677 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
683 DEFUN (show_address_vrf_all
,
684 show_address_vrf_all_cmd
,
685 "show address vrf all",
688 VRF_ALL_CMD_HELP_STR
)
691 struct listnode
*node
;
692 struct interface
*ifp
;
693 struct connected
*ifc
;
696 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
698 if (RB_EMPTY (if_name_head
, &vrf
->ifaces_by_name
))
701 vty_out (vty
, "\nVRF %u\n\n", vrf
->vrf_id
);
703 FOR_ALL_INTERFACES (vrf
, ifp
)
705 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
709 if (p
->family
== AF_INET
)
710 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
718 /* Allocate connected structure. */
719 struct connected
*connected_new(void)
721 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
724 /* Allocate nbr connected structure. */
725 struct nbr_connected
*nbr_connected_new(void)
727 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
730 /* Free connected structure. */
731 void connected_free(struct connected
*connected
)
733 if (connected
->address
)
734 prefix_free(connected
->address
);
736 if (connected
->destination
)
737 prefix_free(connected
->destination
);
739 XFREE(MTYPE_CONNECTED_LABEL
, connected
->label
);
741 XFREE(MTYPE_CONNECTED
, connected
);
744 /* Free nbr connected structure. */
745 void nbr_connected_free(struct nbr_connected
*connected
)
747 if (connected
->address
)
748 prefix_free(connected
->address
);
750 XFREE(MTYPE_NBR_CONNECTED
, connected
);
753 /* If same interface nbr address already exists... */
754 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
757 struct nbr_connected
*ifc
;
758 struct listnode
*node
;
760 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
761 if (prefix_same(ifc
->address
, p
))
767 /* Print if_addr structure. */
768 static void __attribute__((unused
))
769 connected_log(struct connected
*connected
, char *str
)
772 struct interface
*ifp
;
776 ifp
= connected
->ifp
;
777 p
= connected
->address
;
779 snprintf(logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ", str
,
780 ifp
->name
, ifp
->vrf_id
, prefix_family_str(p
),
781 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
783 p
= connected
->destination
;
785 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
786 BUFSIZ
- strlen(logbuf
));
788 zlog_info("%s", logbuf
);
791 /* Print if_addr structure. */
792 static void __attribute__((unused
))
793 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
796 struct interface
*ifp
;
800 ifp
= connected
->ifp
;
801 p
= connected
->address
;
803 snprintf(logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ", str
, ifp
->name
,
804 prefix_family_str(p
),
805 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
807 zlog_info("%s", logbuf
);
810 /* If two connected address has same prefix return 1. */
811 static int connected_same_prefix(struct prefix
*p1
, struct prefix
*p2
)
813 if (p1
->family
== p2
->family
) {
814 if (p1
->family
== AF_INET
815 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
817 if (p1
->family
== AF_INET6
818 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
824 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
827 struct listnode
*node
;
828 struct listnode
*next
;
829 struct connected
*ifc
;
831 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
832 ifc
= listgetdata(node
);
835 if (connected_same_prefix(ifc
->address
, p
))
841 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
844 struct listnode
*node
;
845 struct listnode
*next
;
846 struct connected
*ifc
;
848 /* In case of same prefix come, replace it with new one. */
849 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
850 ifc
= listgetdata(node
);
853 if (connected_same_prefix(ifc
->address
, p
)) {
854 listnode_delete(ifp
->connected
, ifc
);
861 /* Find the address on our side that will be used when packets
863 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
866 struct listnode
*cnode
;
868 struct connected
*match
;
872 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
873 if (c
->address
&& (c
->address
->family
== addr
->family
)
874 && prefix_match(CONNECTED_PREFIX(c
), addr
)
876 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
882 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
884 struct prefix
*destination
)
886 struct connected
*ifc
;
888 /* Allocate new connected address. */
889 ifc
= connected_new();
892 /* Fetch interface address */
893 ifc
->address
= prefix_new();
894 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
896 /* Fetch dest address */
898 ifc
->destination
= prefix_new();
899 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
902 /* Add connected address to the interface. */
903 listnode_add(ifp
->connected
, ifc
);
907 struct connected
*connected_get_linklocal(struct interface
*ifp
)
910 struct connected
*c
= NULL
;
912 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, n
, c
)) {
913 if (c
->address
->family
== AF_INET6
914 && IN6_IS_ADDR_LINKLOCAL(&c
->address
->u
.prefix6
))
920 #if 0 /* this route_table of struct connected's is unused \
921 * however, it would be good to use a route_table rather than \
924 /* Interface looking up by interface's address. */
925 /* Interface's IPv4 address reverse lookup table. */
926 struct route_table
*ifaddr_ipv4_table
;
927 /* struct route_table *ifaddr_ipv6_table; */
930 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
932 struct route_node
*rn
;
933 struct prefix_ipv4 p
;
936 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
939 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
942 route_unlock_node (rn
);
943 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
944 inet_ntoa (*ifaddr
));
951 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
953 struct route_node
*rn
;
954 struct prefix_ipv4 p
;
957 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
960 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
963 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
964 inet_ntoa (*ifaddr
));
968 route_unlock_node (rn
);
969 route_unlock_node (rn
);
972 /* Lookup interface by interface's IP address or interface index. */
973 static struct interface
*
974 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
976 struct prefix_ipv4 p
;
977 struct route_node
*rn
;
978 struct interface
*ifp
;
983 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
986 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
991 route_unlock_node (rn
);
995 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
997 #endif /* ifaddr_ipv4_table */
999 void if_terminate(struct vrf
*vrf
)
1001 struct interface
*ifp
;
1003 while (!RB_EMPTY(if_name_head
, &vrf
->ifaces_by_name
)) {
1004 ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
);
1007 ifp
->node
->info
= NULL
;
1008 route_unlock_node(ifp
->node
);
1014 const char *if_link_type_str(enum zebra_link_type llt
)
1017 #define llts(T,S) case (T): return (S)
1018 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1019 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1020 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1021 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1022 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1023 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1024 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1025 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1026 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1027 llts(ZEBRA_LLT_ATM
, "ATM");
1028 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1029 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1030 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1031 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1032 llts(ZEBRA_LLT_SLIP
, "SLIP");
1033 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1034 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1035 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1036 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1037 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1038 llts(ZEBRA_LLT_PPP
, "PPP");
1039 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1040 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1041 llts(ZEBRA_LLT_LAPB
, "LAPB");
1042 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1043 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1044 llts(ZEBRA_LLT_FRAD
, "FRAD");
1045 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1046 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1047 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1048 llts(ZEBRA_LLT_FDDI
, "FDDI");
1049 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1050 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1051 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1052 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1053 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1054 llts(ZEBRA_LLT_IRDA
, "IrDA");
1055 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1056 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1057 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1058 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1059 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1060 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1061 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1062 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1063 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1065 flog_err(EC_LIB_DEVELOPMENT
, "Unknown value %d", llt
);
1066 return "Unknown type!";
1072 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1076 if (ifp
->link_params
!= NULL
)
1077 return ifp
->link_params
;
1079 struct if_link_params
*iflp
=
1080 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1084 /* Set TE metric equal to standard metric */
1085 iflp
->te_metric
= ifp
->metric
;
1087 /* Compute default bandwidth based on interface */
1089 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1090 * TE_KILO_BIT
/ TE_BYTE
);
1092 /* Set Max, Reservable and Unreserved Bandwidth */
1093 iflp
->max_bw
= iflp
->default_bw
;
1094 iflp
->max_rsv_bw
= iflp
->default_bw
;
1095 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1096 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1098 /* Update Link parameters status */
1100 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1102 /* Finally attach newly created Link Parameters */
1103 ifp
->link_params
= iflp
;
1108 void if_link_params_free(struct interface
*ifp
)
1110 if (ifp
->link_params
== NULL
)
1112 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1113 ifp
->link_params
= NULL
;
1116 /* ----------- CLI commands ----------- */
1119 * XPath: /frr-interface:lib/interface
1121 DEFPY_NOSH (interface
,
1123 "interface IFNAME [vrf NAME$vrfname]",
1124 "Select an interface to configure\n"
1125 "Interface's name\n"
1128 char xpath_list
[XPATH_MAXLEN
];
1130 struct interface
*ifp
;
1134 vrfname
= VRF_DEFAULT_NAME
;
1137 * This command requires special handling to maintain backward
1138 * compatibility. If a VRF name is not specified, it means we're willing
1139 * to accept any interface with the given name on any VRF. If no
1140 * interface is found, then a new one should be created on the default
1143 VRF_GET_ID(vrf_id
, vrfname
, false);
1144 ifp
= if_lookup_by_name_all_vrf(ifname
);
1145 if (ifp
&& ifp
->vrf_id
!= vrf_id
) {
1149 * Special case 1: a VRF name was specified, but the found
1150 * interface is associated to different VRF. Reject the command.
1152 if (vrf_id
!= VRF_DEFAULT
) {
1153 vty_out(vty
, "%% interface %s not in %s vrf\n", ifname
,
1155 return CMD_WARNING_CONFIG_FAILED
;
1159 * Special case 2: a VRF name was *not* specified, and the found
1160 * interface is associated to a VRF other than the default one.
1161 * Update vrf_id and vrfname to account for that.
1163 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
1165 vrf_id
= ifp
->vrf_id
;
1166 vrfname
= vrf
->name
;
1169 snprintf(xpath_list
, sizeof(xpath_list
),
1170 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname
,
1173 nb_cli_enqueue_change(vty
, ".", NB_OP_CREATE
, NULL
);
1174 ret
= nb_cli_apply_changes(vty
, xpath_list
);
1175 if (ret
== CMD_SUCCESS
) {
1176 VTY_PUSH_XPATH(INTERFACE_NODE
, xpath_list
);
1179 * For backward compatibility with old commands we still need
1180 * to use the qobj infrastructure. This can be removed once
1181 * all interface-level commands are converted to the new
1184 ifp
= if_lookup_by_name(ifname
, vrf_id
);
1186 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
1192 DEFPY (no_interface
,
1194 "no interface IFNAME [vrf NAME$vrfname]",
1196 "Delete a pseudo interface's configuration\n"
1197 "Interface's name\n"
1201 vrfname
= VRF_DEFAULT_NAME
;
1203 nb_cli_enqueue_change(vty
, ".", NB_OP_DESTROY
, NULL
);
1205 return nb_cli_apply_changes(
1206 vty
, "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1210 static void cli_show_interface(struct vty
*vty
, struct lyd_node
*dnode
,
1215 vrf
= yang_dnode_get_string(dnode
, "./vrf");
1217 vty_out(vty
, "!\n");
1218 vty_out(vty
, "interface %s", yang_dnode_get_string(dnode
, "./name"));
1219 if (!strmatch(vrf
, VRF_DEFAULT_NAME
))
1220 vty_out(vty
, " vrf %s", vrf
);
1225 * XPath: /frr-interface:lib/interface/description
1227 DEFPY (interface_desc
,
1229 "description LINE...",
1230 "Interface specific description\n"
1231 "Characters describing this interface\n")
1236 desc
= argv_concat(argv
, argc
, 1);
1237 nb_cli_enqueue_change(vty
, "./description", NB_OP_MODIFY
, desc
);
1238 ret
= nb_cli_apply_changes(vty
, NULL
);
1239 XFREE(MTYPE_TMP
, desc
);
1244 DEFPY (no_interface_desc
,
1245 no_interface_desc_cmd
,
1248 "Interface specific description\n")
1250 nb_cli_enqueue_change(vty
, "./description", NB_OP_DESTROY
, NULL
);
1252 return nb_cli_apply_changes(vty
, NULL
);
1255 static void cli_show_interface_desc(struct vty
*vty
, struct lyd_node
*dnode
,
1258 vty_out(vty
, " description %s\n", yang_dnode_get_string(dnode
, NULL
));
1261 /* Interface autocomplete. */
1262 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
1264 struct interface
*ifp
;
1267 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1268 FOR_ALL_INTERFACES (vrf
, ifp
) {
1269 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
1274 static const struct cmd_variable_handler if_var_handlers
[] = {
1275 {/* "interface NAME" */
1276 .varname
= "interface",
1277 .completions
= if_autocomplete
},
1278 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
1279 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
1280 {.completions
= NULL
}};
1282 void if_cmd_init(void)
1284 cmd_variable_handler_register(if_var_handlers
);
1286 install_element(CONFIG_NODE
, &interface_cmd
);
1287 install_element(CONFIG_NODE
, &no_interface_cmd
);
1289 install_default(INTERFACE_NODE
);
1290 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
1291 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
1294 /* ------- Northbound callbacks ------- */
1297 * XPath: /frr-interface:lib/interface
1299 static int lib_interface_create(enum nb_event event
,
1300 const struct lyd_node
*dnode
,
1301 union nb_resource
*resource
)
1304 const char *vrfname
;
1306 struct interface
*ifp
;
1308 ifname
= yang_dnode_get_string(dnode
, "./name");
1309 vrfname
= yang_dnode_get_string(dnode
, "./vrf");
1312 case NB_EV_VALIDATE
:
1313 vrf
= vrf_lookup_by_name(vrfname
);
1315 zlog_warn("%s: VRF %s doesn't exist", __func__
,
1317 return NB_ERR_VALIDATION
;
1319 if (vrf
->vrf_id
== VRF_UNKNOWN
) {
1320 zlog_warn("%s: VRF %s is not active", __func__
,
1322 return NB_ERR_VALIDATION
;
1325 /* if VRF is netns or not yet known - init for instance
1326 * then assumption is that passed config is exact
1327 * then the user intent was not to use an other iface
1329 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE
) {
1330 ifp
= if_lookup_by_name_all_vrf(ifname
);
1331 if (ifp
&& ifp
->vrf_id
!= vrf
->vrf_id
) {
1333 "%s: interface %s already exists in another VRF",
1334 __func__
, ifp
->name
);
1335 return NB_ERR_VALIDATION
;
1343 vrf
= vrf_lookup_by_name(vrfname
);
1346 ifp
= if_sunwzebra_get(ifname
, vrf
->vrf_id
);
1348 ifp
= if_get_by_name(ifname
, vrf
->vrf_id
);
1349 #endif /* SUNOS_5 */
1350 nb_running_set_entry(dnode
, ifp
);
1357 static int lib_interface_destroy(enum nb_event event
,
1358 const struct lyd_node
*dnode
)
1360 struct interface
*ifp
;
1364 case NB_EV_VALIDATE
:
1365 ifp
= nb_running_get_entry(dnode
, NULL
, true);
1366 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1367 zlog_warn("%s: only inactive interfaces can be deleted",
1369 return NB_ERR_VALIDATION
;
1376 ifp
= nb_running_unset_entry(dnode
);
1385 * XPath: /frr-interface:lib/interface/description
1387 static int lib_interface_description_modify(enum nb_event event
,
1388 const struct lyd_node
*dnode
,
1389 union nb_resource
*resource
)
1391 struct interface
*ifp
;
1392 const char *description
;
1394 if (event
!= NB_EV_APPLY
)
1397 ifp
= nb_running_get_entry(dnode
, NULL
, true);
1398 XFREE(MTYPE_TMP
, ifp
->desc
);
1399 description
= yang_dnode_get_string(dnode
, NULL
);
1400 ifp
->desc
= XSTRDUP(MTYPE_TMP
, description
);
1405 static int lib_interface_description_destroy(enum nb_event event
,
1406 const struct lyd_node
*dnode
)
1408 struct interface
*ifp
;
1410 if (event
!= NB_EV_APPLY
)
1413 ifp
= nb_running_get_entry(dnode
, NULL
, true);
1414 XFREE(MTYPE_TMP
, ifp
->desc
);
1419 /* clang-format off */
1420 const struct frr_yang_module_info frr_interface_info
= {
1421 .name
= "frr-interface",
1424 .xpath
= "/frr-interface:lib/interface",
1426 .create
= lib_interface_create
,
1427 .destroy
= lib_interface_destroy
,
1428 .cli_show
= cli_show_interface
,
1432 .xpath
= "/frr-interface:lib/interface/description",
1434 .modify
= lib_interface_description_modify
,
1435 .destroy
= lib_interface_description_destroy
,
1436 .cli_show
= cli_show_interface_desc
,