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
, 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 if_dnode
= yang_dnode_get(
191 running_config
->dnode
,
192 "/frr-interface:lib/interface[name='%s'][vrf='%s']/vrf",
193 ifp
->name
, old_vrf
->name
);
195 yang_dnode_change_leaf(if_dnode
, vrf
->name
);
196 running_config
->version
++;
202 /* Delete interface structure. */
203 void if_delete_retain(struct interface
*ifp
)
205 hook_call(if_del
, ifp
);
208 /* Free connected address list */
209 list_delete_all_node(ifp
->connected
);
211 /* Free connected nbr address list */
212 list_delete_all_node(ifp
->nbr_connected
);
215 /* Delete and free interface structure. */
216 void if_delete(struct interface
*ifp
)
220 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
223 IFNAME_RB_REMOVE(vrf
, ifp
);
224 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
225 IFINDEX_RB_REMOVE(vrf
, ifp
);
227 if_delete_retain(ifp
);
229 list_delete(&ifp
->connected
);
230 list_delete(&ifp
->nbr_connected
);
232 if_link_params_free(ifp
);
234 XFREE(MTYPE_TMP
, ifp
->desc
);
236 XFREE(MTYPE_IF
, ifp
);
239 /* Interface existance check by index. */
240 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
243 struct interface if_tmp
;
245 vrf
= vrf_lookup_by_id(vrf_id
);
249 if_tmp
.ifindex
= ifindex
;
250 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
253 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
255 struct interface
*ifp
;
257 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
262 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
264 struct interface
*ifp
;
266 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
271 /* Interface existance check by interface name. */
272 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
274 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
275 struct interface if_tmp
;
278 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
281 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
282 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
285 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
288 struct interface
*ifp
;
290 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
293 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
294 ifp
= if_lookup_by_name(name
, vrf
->vrf_id
);
302 /* Lookup interface by IPv4 address. */
303 struct interface
*if_lookup_exact_address(void *src
, int family
,
306 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
307 struct listnode
*cnode
;
308 struct interface
*ifp
;
312 FOR_ALL_INTERFACES (vrf
, ifp
) {
313 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
316 if (p
&& (p
->family
== family
)) {
317 if (family
== AF_INET
) {
320 (struct in_addr
*)src
))
322 } else if (family
== AF_INET6
) {
325 (struct in6_addr
*)src
))
334 /* Lookup interface by IPv4 address. */
335 struct connected
*if_lookup_address(void *matchaddr
, int family
,
338 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
341 struct listnode
*cnode
;
342 struct interface
*ifp
;
344 struct connected
*match
;
346 if (family
== AF_INET
) {
347 addr
.family
= AF_INET
;
348 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
349 addr
.prefixlen
= IPV4_MAX_BITLEN
;
350 } else if (family
== AF_INET6
) {
351 addr
.family
= AF_INET6
;
352 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
353 addr
.prefixlen
= IPV6_MAX_BITLEN
;
358 FOR_ALL_INTERFACES (vrf
, ifp
) {
359 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
360 if (c
->address
&& (c
->address
->family
== AF_INET
)
361 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
362 && (c
->address
->prefixlen
> bestlen
)) {
363 bestlen
= c
->address
->prefixlen
;
371 /* Lookup interface by prefix */
372 struct interface
*if_lookup_prefix(struct prefix
*prefix
, vrf_id_t vrf_id
)
374 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
375 struct listnode
*cnode
;
376 struct interface
*ifp
;
379 FOR_ALL_INTERFACES (vrf
, ifp
) {
380 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
381 if (prefix_cmp(c
->address
, prefix
) == 0) {
389 /* Get interface by name if given name interface doesn't exist create
391 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
)
393 struct interface
*ifp
;
395 switch (vrf_get_backend()) {
396 case VRF_BACKEND_UNKNOWN
:
397 case VRF_BACKEND_NETNS
:
398 ifp
= if_lookup_by_name(name
, vrf_id
);
401 return if_create(name
, vrf_id
);
402 case VRF_BACKEND_VRF_LITE
:
403 ifp
= if_lookup_by_name_all_vrf(name
);
405 if (ifp
->vrf_id
== vrf_id
)
407 /* If it came from the kernel or by way of zclient,
408 * believe it and update the ifp accordingly.
410 if_update_to_new_vrf(ifp
, vrf_id
);
413 return if_create(name
, vrf_id
);
419 void if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
423 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
426 if (ifp
->ifindex
== ifindex
)
429 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
430 IFINDEX_RB_REMOVE(vrf
, ifp
)
432 ifp
->ifindex
= ifindex
;
434 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
435 IFINDEX_RB_INSERT(vrf
, ifp
)
438 /* Does interface up ? */
439 int if_is_up(struct interface
*ifp
)
441 return ifp
->flags
& IFF_UP
;
444 /* Is interface running? */
445 int if_is_running(struct interface
*ifp
)
447 return ifp
->flags
& IFF_RUNNING
;
450 /* Is the interface operative, eg. either UP & RUNNING
451 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
452 if ptm checking is enabled, then ptm check has passed */
453 int if_is_operative(struct interface
*ifp
)
455 return ((ifp
->flags
& IFF_UP
)
456 && (((ifp
->flags
& IFF_RUNNING
)
457 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
458 || !CHECK_FLAG(ifp
->status
,
459 ZEBRA_INTERFACE_LINKDETECTION
)));
462 /* Is the interface operative, eg. either UP & RUNNING
463 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
464 int if_is_no_ptm_operative(struct interface
*ifp
)
466 return ((ifp
->flags
& IFF_UP
)
467 && ((ifp
->flags
& IFF_RUNNING
)
468 || !CHECK_FLAG(ifp
->status
,
469 ZEBRA_INTERFACE_LINKDETECTION
)));
472 /* Is this loopback interface ? */
473 int if_is_loopback(struct interface
*ifp
)
475 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
476 * but Y on platform N?
478 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
481 /* Check interface is VRF */
482 int if_is_vrf(struct interface
*ifp
)
484 return CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
487 bool if_is_loopback_or_vrf(struct interface
*ifp
)
489 if (if_is_loopback(ifp
) || if_is_vrf(ifp
))
495 /* Does this interface support broadcast ? */
496 int if_is_broadcast(struct interface
*ifp
)
498 return ifp
->flags
& IFF_BROADCAST
;
501 /* Does this interface support broadcast ? */
502 int if_is_pointopoint(struct interface
*ifp
)
504 return ifp
->flags
& IFF_POINTOPOINT
;
507 /* Does this interface support multicast ? */
508 int if_is_multicast(struct interface
*ifp
)
510 return ifp
->flags
& IFF_MULTICAST
;
513 /* Printout flag information into log */
514 const char *if_flag_dump(unsigned long flag
)
517 static char logbuf
[BUFSIZ
];
519 #define IFF_OUT_LOG(X, STR) \
522 strlcat(logbuf, ",", BUFSIZ); \
525 strlcat(logbuf, STR, BUFSIZ); \
528 strlcpy(logbuf
, "<", BUFSIZ
);
529 IFF_OUT_LOG(IFF_UP
, "UP");
530 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
531 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
532 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
533 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
534 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
535 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
536 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
537 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
538 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
539 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
540 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
541 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
542 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
543 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
544 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
545 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
546 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
547 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
548 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
549 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
551 strlcat(logbuf
, ">", BUFSIZ
);
558 static void if_dump(const struct interface
*ifp
)
560 struct listnode
*node
;
561 struct connected
*c
__attribute__((unused
));
563 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
565 "Interface %s vrf %u index %d metric %d mtu %d "
567 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
,
568 ifp
->mtu
, ifp
->mtu6
, if_flag_dump(ifp
->flags
));
571 /* Interface printing for all interface. */
572 void if_dump_all(void)
577 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
578 FOR_ALL_INTERFACES (vrf
, ifp
)
583 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
584 * a seperate struct interface for each logical interface, so config
585 * file may be full of 'interface fooX:Y'. Solaris however does not
586 * expose logical interfaces via PF_ROUTE, so trying to track logical
587 * interfaces can be fruitless, for that reason Quagga only tracks
588 * the primary IP interface.
590 * We try accomodate SUNWzebra by:
591 * - looking up the interface name, to see whether it exists, if so
593 * - for protocol daemons, this could only because zebra told us of
595 * - for zebra, only because it learnt from kernel
597 * - search the name to see if it contains a sub-ipif / logical interface
598 * seperator, the ':' char. If it does:
599 * - text up to that char must be the primary name - get that name.
601 * - no idea, just get the name in its entirety.
603 static struct interface
*if_sunwzebra_get(const char *name
, vrf_id_t vrf_id
)
605 struct interface
*ifp
;
608 if ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
611 /* hunt the primary interface name... */
612 cp
= strchr(name
, ':');
616 return if_get_by_name(name
, vrf_id
);
621 /* For debug purpose. */
624 "show address [vrf NAME]",
630 struct listnode
*node
;
631 struct interface
*ifp
;
632 struct connected
*ifc
;
634 vrf_id_t vrf_id
= VRF_DEFAULT
;
637 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
639 FOR_ALL_INTERFACES (vrf
, ifp
)
641 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
645 if (p
->family
== AF_INET
)
646 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
652 DEFUN (show_address_vrf_all
,
653 show_address_vrf_all_cmd
,
654 "show address vrf all",
657 VRF_ALL_CMD_HELP_STR
)
660 struct listnode
*node
;
661 struct interface
*ifp
;
662 struct connected
*ifc
;
665 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
667 if (RB_EMPTY (if_name_head
, &vrf
->ifaces_by_name
))
670 vty_out (vty
, "\nVRF %u\n\n", vrf
->vrf_id
);
672 FOR_ALL_INTERFACES (vrf
, ifp
)
674 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
678 if (p
->family
== AF_INET
)
679 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
687 /* Allocate connected structure. */
688 struct connected
*connected_new(void)
690 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
693 /* Allocate nbr connected structure. */
694 struct nbr_connected
*nbr_connected_new(void)
696 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
699 /* Free connected structure. */
700 void connected_free(struct connected
*connected
)
702 if (connected
->address
)
703 prefix_free(connected
->address
);
705 if (connected
->destination
)
706 prefix_free(connected
->destination
);
708 XFREE(MTYPE_CONNECTED_LABEL
, connected
->label
);
710 XFREE(MTYPE_CONNECTED
, connected
);
713 /* Free nbr connected structure. */
714 void nbr_connected_free(struct nbr_connected
*connected
)
716 if (connected
->address
)
717 prefix_free(connected
->address
);
719 XFREE(MTYPE_NBR_CONNECTED
, connected
);
722 /* If same interface nbr address already exists... */
723 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
726 struct nbr_connected
*ifc
;
727 struct listnode
*node
;
729 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
730 if (prefix_same(ifc
->address
, p
))
736 /* Print if_addr structure. */
737 static void __attribute__((unused
))
738 connected_log(struct connected
*connected
, char *str
)
741 struct interface
*ifp
;
745 ifp
= connected
->ifp
;
746 p
= connected
->address
;
748 snprintf(logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ", str
,
749 ifp
->name
, ifp
->vrf_id
, prefix_family_str(p
),
750 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
752 p
= connected
->destination
;
754 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
755 BUFSIZ
- strlen(logbuf
));
757 zlog_info("%s", logbuf
);
760 /* Print if_addr structure. */
761 static void __attribute__((unused
))
762 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
765 struct interface
*ifp
;
769 ifp
= connected
->ifp
;
770 p
= connected
->address
;
772 snprintf(logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ", str
, ifp
->name
,
773 prefix_family_str(p
),
774 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
776 zlog_info("%s", logbuf
);
779 /* If two connected address has same prefix return 1. */
780 static int connected_same_prefix(struct prefix
*p1
, struct prefix
*p2
)
782 if (p1
->family
== p2
->family
) {
783 if (p1
->family
== AF_INET
784 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
786 if (p1
->family
== AF_INET6
787 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
793 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
796 struct listnode
*node
;
797 struct listnode
*next
;
798 struct connected
*ifc
;
800 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
801 ifc
= listgetdata(node
);
804 if (connected_same_prefix(ifc
->address
, p
))
810 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
813 struct listnode
*node
;
814 struct listnode
*next
;
815 struct connected
*ifc
;
817 /* In case of same prefix come, replace it with new one. */
818 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
819 ifc
= listgetdata(node
);
822 if (connected_same_prefix(ifc
->address
, p
)) {
823 listnode_delete(ifp
->connected
, ifc
);
830 /* Find the address on our side that will be used when packets
832 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
835 struct listnode
*cnode
;
837 struct connected
*match
;
841 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
842 if (c
->address
&& (c
->address
->family
== addr
->family
)
843 && prefix_match(CONNECTED_PREFIX(c
), addr
)
845 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
851 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
853 struct prefix
*destination
)
855 struct connected
*ifc
;
857 /* Allocate new connected address. */
858 ifc
= connected_new();
861 /* Fetch interface address */
862 ifc
->address
= prefix_new();
863 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
865 /* Fetch dest address */
867 ifc
->destination
= prefix_new();
868 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
871 /* Add connected address to the interface. */
872 listnode_add(ifp
->connected
, ifc
);
876 #if 0 /* this route_table of struct connected's is unused \
877 * however, it would be good to use a route_table rather than \
880 /* Interface looking up by interface's address. */
881 /* Interface's IPv4 address reverse lookup table. */
882 struct route_table
*ifaddr_ipv4_table
;
883 /* struct route_table *ifaddr_ipv6_table; */
886 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
888 struct route_node
*rn
;
889 struct prefix_ipv4 p
;
892 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
895 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
898 route_unlock_node (rn
);
899 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
900 inet_ntoa (*ifaddr
));
907 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
909 struct route_node
*rn
;
910 struct prefix_ipv4 p
;
913 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
916 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
919 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
920 inet_ntoa (*ifaddr
));
924 route_unlock_node (rn
);
925 route_unlock_node (rn
);
928 /* Lookup interface by interface's IP address or interface index. */
929 static struct interface
*
930 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
932 struct prefix_ipv4 p
;
933 struct route_node
*rn
;
934 struct interface
*ifp
;
939 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
942 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
947 route_unlock_node (rn
);
951 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
953 #endif /* ifaddr_ipv4_table */
955 void if_terminate(struct vrf
*vrf
)
957 struct interface
*ifp
;
959 while (!RB_EMPTY(if_name_head
, &vrf
->ifaces_by_name
)) {
960 ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
);
963 ifp
->node
->info
= NULL
;
964 route_unlock_node(ifp
->node
);
970 const char *if_link_type_str(enum zebra_link_type llt
)
973 #define llts(T,S) case (T): return (S)
974 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
975 llts(ZEBRA_LLT_ETHER
, "Ethernet");
976 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
977 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
978 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
979 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
980 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
981 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
982 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
983 llts(ZEBRA_LLT_ATM
, "ATM");
984 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
985 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
986 llts(ZEBRA_LLT_EUI64
, "EUI-64");
987 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
988 llts(ZEBRA_LLT_SLIP
, "SLIP");
989 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
990 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
991 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
992 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
993 llts(ZEBRA_LLT_X25
, "CCITT X.25");
994 llts(ZEBRA_LLT_PPP
, "PPP");
995 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
996 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
997 llts(ZEBRA_LLT_LAPB
, "LAPB");
998 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
999 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1000 llts(ZEBRA_LLT_FRAD
, "FRAD");
1001 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1002 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1003 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1004 llts(ZEBRA_LLT_FDDI
, "FDDI");
1005 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1006 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1007 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1008 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1009 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1010 llts(ZEBRA_LLT_IRDA
, "IrDA");
1011 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1012 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1013 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1014 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1015 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1016 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1017 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1018 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1019 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1021 flog_err(EC_LIB_DEVELOPMENT
, "Unknown value %d", llt
);
1022 return "Unknown type!";
1028 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1032 if (ifp
->link_params
!= NULL
)
1033 return ifp
->link_params
;
1035 struct if_link_params
*iflp
=
1036 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1040 /* Set TE metric equal to standard metric */
1041 iflp
->te_metric
= ifp
->metric
;
1043 /* Compute default bandwidth based on interface */
1045 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1046 * TE_KILO_BIT
/ TE_BYTE
);
1048 /* Set Max, Reservable and Unreserved Bandwidth */
1049 iflp
->max_bw
= iflp
->default_bw
;
1050 iflp
->max_rsv_bw
= iflp
->default_bw
;
1051 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1052 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1054 /* Update Link parameters status */
1056 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1058 /* Finally attach newly created Link Parameters */
1059 ifp
->link_params
= iflp
;
1064 void if_link_params_free(struct interface
*ifp
)
1066 if (ifp
->link_params
== NULL
)
1068 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1069 ifp
->link_params
= NULL
;
1072 /* ----------- CLI commands ----------- */
1075 * XPath: /frr-interface:lib/interface
1077 DEFPY_NOSH (interface
,
1079 "interface IFNAME [vrf NAME$vrfname]",
1080 "Select an interface to configure\n"
1081 "Interface's name\n"
1084 char xpath_list
[XPATH_MAXLEN
];
1086 struct interface
*ifp
;
1090 vrfname
= VRF_DEFAULT_NAME
;
1093 * This command requires special handling to maintain backward
1094 * compatibility. If a VRF name is not specified, it means we're willing
1095 * to accept any interface with the given name on any VRF. If no
1096 * interface is found, then a new one should be created on the default
1099 VRF_GET_ID(vrf_id
, vrfname
, false);
1100 ifp
= if_lookup_by_name_all_vrf(ifname
);
1101 if (ifp
&& ifp
->vrf_id
!= vrf_id
) {
1105 * Special case 1: a VRF name was specified, but the found
1106 * interface is associated to different VRF. Reject the command.
1108 if (vrf_id
!= VRF_DEFAULT
) {
1109 vty_out(vty
, "%% interface %s not in %s vrf\n", ifname
,
1111 return CMD_WARNING_CONFIG_FAILED
;
1115 * Special case 2: a VRF name was *not* specified, and the found
1116 * interface is associated to a VRF other than the default one.
1117 * Update vrf_id and vrfname to account for that.
1119 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
1121 vrf_id
= ifp
->vrf_id
;
1122 vrfname
= vrf
->name
;
1125 snprintf(xpath_list
, sizeof(xpath_list
),
1126 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname
,
1129 nb_cli_enqueue_change(vty
, ".", NB_OP_CREATE
, NULL
);
1130 ret
= nb_cli_apply_changes(vty
, xpath_list
);
1131 if (ret
== CMD_SUCCESS
) {
1132 VTY_PUSH_XPATH(INTERFACE_NODE
, xpath_list
);
1135 * For backward compatibility with old commands we still need
1136 * to use the qobj infrastructure. This can be removed once
1137 * all interface-level commands are converted to the new
1140 ifp
= if_lookup_by_name(ifname
, vrf_id
);
1142 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
1148 DEFPY (no_interface
,
1150 "no interface IFNAME [vrf NAME$vrfname]",
1152 "Delete a pseudo interface's configuration\n"
1153 "Interface's name\n"
1157 vrfname
= VRF_DEFAULT_NAME
;
1159 nb_cli_enqueue_change(vty
, ".", NB_OP_DESTROY
, NULL
);
1161 return nb_cli_apply_changes(
1162 vty
, "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1166 static void cli_show_interface(struct vty
*vty
, struct lyd_node
*dnode
,
1171 vrf
= yang_dnode_get_string(dnode
, "./vrf");
1173 vty_out(vty
, "!\n");
1174 vty_out(vty
, "interface %s", yang_dnode_get_string(dnode
, "./name"));
1175 if (!strmatch(vrf
, VRF_DEFAULT_NAME
))
1176 vty_out(vty
, " vrf %s", vrf
);
1181 * XPath: /frr-interface:lib/interface/description
1183 DEFPY (interface_desc
,
1185 "description LINE...",
1186 "Interface specific description\n"
1187 "Characters describing this interface\n")
1192 desc
= argv_concat(argv
, argc
, 1);
1193 nb_cli_enqueue_change(vty
, "./description", NB_OP_MODIFY
, desc
);
1194 ret
= nb_cli_apply_changes(vty
, NULL
);
1195 XFREE(MTYPE_TMP
, desc
);
1200 DEFPY (no_interface_desc
,
1201 no_interface_desc_cmd
,
1204 "Interface specific description\n")
1206 nb_cli_enqueue_change(vty
, "./description", NB_OP_DESTROY
, NULL
);
1208 return nb_cli_apply_changes(vty
, NULL
);
1211 static void cli_show_interface_desc(struct vty
*vty
, struct lyd_node
*dnode
,
1214 vty_out(vty
, " description %s\n", yang_dnode_get_string(dnode
, NULL
));
1217 /* Interface autocomplete. */
1218 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
1220 struct interface
*ifp
;
1223 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1224 FOR_ALL_INTERFACES (vrf
, ifp
) {
1225 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
1230 static const struct cmd_variable_handler if_var_handlers
[] = {
1231 {/* "interface NAME" */
1232 .varname
= "interface",
1233 .completions
= if_autocomplete
},
1234 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
1235 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
1236 {.completions
= NULL
}};
1238 void if_cmd_init(void)
1240 cmd_variable_handler_register(if_var_handlers
);
1242 install_element(CONFIG_NODE
, &interface_cmd
);
1243 install_element(CONFIG_NODE
, &no_interface_cmd
);
1245 install_default(INTERFACE_NODE
);
1246 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
1247 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
1250 /* ------- Northbound callbacks ------- */
1253 * XPath: /frr-interface:lib/interface
1255 static int lib_interface_create(enum nb_event event
,
1256 const struct lyd_node
*dnode
,
1257 union nb_resource
*resource
)
1260 const char *vrfname
;
1262 struct interface
*ifp
;
1264 ifname
= yang_dnode_get_string(dnode
, "./name");
1265 vrfname
= yang_dnode_get_string(dnode
, "./vrf");
1268 case NB_EV_VALIDATE
:
1269 vrf
= vrf_lookup_by_name(vrfname
);
1271 zlog_warn("%s: VRF %s doesn't exist", __func__
,
1273 return NB_ERR_VALIDATION
;
1275 if (vrf
->vrf_id
== VRF_UNKNOWN
) {
1276 zlog_warn("%s: VRF %s is not active", __func__
,
1278 return NB_ERR_VALIDATION
;
1281 /* if VRF is netns or not yet known - init for instance
1282 * then assumption is that passed config is exact
1283 * then the user intent was not to use an other iface
1285 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE
) {
1286 ifp
= if_lookup_by_name_all_vrf(ifname
);
1287 if (ifp
&& ifp
->vrf_id
!= vrf
->vrf_id
) {
1289 "%s: interface %s already exists in another VRF",
1290 __func__
, ifp
->name
);
1291 return NB_ERR_VALIDATION
;
1299 vrf
= vrf_lookup_by_name(vrfname
);
1302 ifp
= if_sunwzebra_get(ifname
, vrf
->vrf_id
);
1304 ifp
= if_get_by_name(ifname
, vrf
->vrf_id
);
1305 #endif /* SUNOS_5 */
1306 yang_dnode_set_entry(dnode
, ifp
);
1313 static int lib_interface_delete(enum nb_event event
,
1314 const struct lyd_node
*dnode
)
1316 struct interface
*ifp
;
1318 ifp
= yang_dnode_get_entry(dnode
, true);
1321 case NB_EV_VALIDATE
:
1322 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1323 zlog_warn("%s: only inactive interfaces can be deleted",
1325 return NB_ERR_VALIDATION
;
1340 * XPath: /frr-interface:lib/interface/description
1342 static int lib_interface_description_modify(enum nb_event event
,
1343 const struct lyd_node
*dnode
,
1344 union nb_resource
*resource
)
1346 struct interface
*ifp
;
1347 const char *description
;
1349 if (event
!= NB_EV_APPLY
)
1352 ifp
= yang_dnode_get_entry(dnode
, true);
1353 XFREE(MTYPE_TMP
, ifp
->desc
);
1354 description
= yang_dnode_get_string(dnode
, NULL
);
1355 ifp
->desc
= XSTRDUP(MTYPE_TMP
, description
);
1360 static int lib_interface_description_delete(enum nb_event event
,
1361 const struct lyd_node
*dnode
)
1363 struct interface
*ifp
;
1365 if (event
!= NB_EV_APPLY
)
1368 ifp
= yang_dnode_get_entry(dnode
, true);
1369 XFREE(MTYPE_TMP
, ifp
->desc
);
1374 /* clang-format off */
1375 const struct frr_yang_module_info frr_interface_info
= {
1376 .name
= "frr-interface",
1379 .xpath
= "/frr-interface:lib/interface",
1380 .cbs
.create
= lib_interface_create
,
1381 .cbs
.destroy
= lib_interface_delete
,
1382 .cbs
.cli_show
= cli_show_interface
,
1385 .xpath
= "/frr-interface:lib/interface/description",
1386 .cbs
.modify
= lib_interface_description_modify
,
1387 .cbs
.destroy
= lib_interface_description_delete
,
1388 .cbs
.cli_show
= cli_show_interface_desc
,