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 /* List of interfaces in only the default VRF */
62 /* Compare interface names, returning an integer greater than, equal to, or
63 * less than 0, (following the strcmp convention), according to the
64 * relationship between ifp1 and ifp2. Interface names consist of an
65 * alphabetic prefix and a numeric suffix. The primary sort key is
66 * lexicographic by name, and then numeric by number. No number sorts
67 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
70 int if_cmp_name_func(const char *p1
, const char *p2
)
77 /* look up to any number */
78 l1
= strcspn(p1
, "0123456789");
79 l2
= strcspn(p2
, "0123456789");
81 /* name lengths are different -> compare names */
83 return (strcmp(p1
, p2
));
85 /* Note that this relies on all numbers being less than all
89 res
= strncmp(p1
, p2
, l1
);
91 /* names are different -> compare them */
95 /* with identical name part, go to numeric part */
106 x1
= strtol(p1
, (char **)&p1
, 10);
107 x2
= strtol(p2
, (char **)&p2
, 10);
109 /* let's compare numbers now */
115 /* numbers were equal, lets do it again..
116 (it happens with name like "eth123.456:789") */
125 static int if_cmp_func(const struct interface
*ifp1
,
126 const struct interface
*ifp2
)
128 return if_cmp_name_func(ifp1
->name
, ifp2
->name
);
131 static int if_cmp_index_func(const struct interface
*ifp1
,
132 const struct interface
*ifp2
)
134 return ifp1
->ifindex
- ifp2
->ifindex
;
137 /* Create new interface structure. */
138 struct interface
*if_create(const char *name
, vrf_id_t vrf_id
)
140 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
141 struct interface
*ifp
;
143 ifp
= XCALLOC(MTYPE_IF
, sizeof(struct interface
));
144 ifp
->ifindex
= IFINDEX_INTERNAL
;
147 strlcpy(ifp
->name
, name
, sizeof(ifp
->name
));
148 ifp
->vrf_id
= vrf_id
;
149 IFNAME_RB_INSERT(vrf
, ifp
);
150 ifp
->connected
= list_new();
151 ifp
->connected
->del
= (void (*)(void *))connected_free
;
153 ifp
->nbr_connected
= list_new();
154 ifp
->nbr_connected
->del
= (void (*)(void *))nbr_connected_free
;
156 /* Enable Link-detection by default */
157 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
159 QOBJ_REG(ifp
, interface
);
160 hook_call(if_add
, ifp
);
164 /* Create new interface structure. */
165 void if_update_to_new_vrf(struct interface
*ifp
, vrf_id_t vrf_id
)
167 struct vrf
*old_vrf
, *vrf
;
169 /* remove interface from old master vrf list */
170 old_vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
172 IFNAME_RB_REMOVE(old_vrf
, ifp
);
173 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
174 IFINDEX_RB_REMOVE(old_vrf
, ifp
);
177 ifp
->vrf_id
= vrf_id
;
178 vrf
= vrf_get(ifp
->vrf_id
, NULL
);
180 IFNAME_RB_INSERT(vrf
, ifp
);
181 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
182 IFINDEX_RB_INSERT(vrf
, ifp
);
185 * HACK: Change the interface VRF in the running configuration directly,
186 * bypassing the northbound layer. This is necessary to avoid deleting
187 * the interface and readding it in the new VRF, which would have
188 * several implications.
190 if (yang_module_find("frr-interface")) {
191 struct lyd_node
*if_dnode
;
193 if_dnode
= yang_dnode_get(
194 running_config
->dnode
,
195 "/frr-interface:lib/interface[name='%s'][vrf='%s']/vrf",
196 ifp
->name
, old_vrf
->name
);
198 yang_dnode_change_leaf(if_dnode
, vrf
->name
);
199 running_config
->version
++;
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
);
238 XFREE(MTYPE_TMP
, ifp
->desc
);
240 XFREE(MTYPE_IF
, ifp
);
243 /* Interface existance check by index. */
244 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
247 struct interface if_tmp
;
249 vrf
= vrf_lookup_by_id(vrf_id
);
253 if_tmp
.ifindex
= ifindex
;
254 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
257 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
259 struct interface
*ifp
;
261 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
266 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
268 struct interface
*ifp
;
270 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
275 /* Interface existance check by interface name. */
276 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
278 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
279 struct interface if_tmp
;
282 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
285 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
286 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
289 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
292 struct interface
*ifp
;
294 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
297 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
298 ifp
= if_lookup_by_name(name
, vrf
->vrf_id
);
306 /* Lookup interface by IPv4 address. */
307 struct interface
*if_lookup_exact_address(void *src
, int family
,
310 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
311 struct listnode
*cnode
;
312 struct interface
*ifp
;
316 FOR_ALL_INTERFACES (vrf
, ifp
) {
317 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
320 if (p
&& (p
->family
== family
)) {
321 if (family
== AF_INET
) {
324 (struct in_addr
*)src
))
326 } else if (family
== AF_INET6
) {
329 (struct in6_addr
*)src
))
338 /* Lookup interface by IPv4 address. */
339 struct connected
*if_lookup_address(void *matchaddr
, int family
,
342 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
345 struct listnode
*cnode
;
346 struct interface
*ifp
;
348 struct connected
*match
;
350 if (family
== AF_INET
) {
351 addr
.family
= AF_INET
;
352 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
353 addr
.prefixlen
= IPV4_MAX_BITLEN
;
354 } else if (family
== AF_INET6
) {
355 addr
.family
= AF_INET6
;
356 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
357 addr
.prefixlen
= IPV6_MAX_BITLEN
;
362 FOR_ALL_INTERFACES (vrf
, ifp
) {
363 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
364 if (c
->address
&& (c
->address
->family
== AF_INET
)
365 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
366 && (c
->address
->prefixlen
> bestlen
)) {
367 bestlen
= c
->address
->prefixlen
;
375 /* Lookup interface by prefix */
376 struct interface
*if_lookup_prefix(struct prefix
*prefix
, vrf_id_t vrf_id
)
378 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
379 struct listnode
*cnode
;
380 struct interface
*ifp
;
383 FOR_ALL_INTERFACES (vrf
, ifp
) {
384 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
385 if (prefix_cmp(c
->address
, prefix
) == 0) {
393 /* Get interface by name if given name interface doesn't exist create
395 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
)
397 struct interface
*ifp
;
399 switch (vrf_get_backend()) {
400 case VRF_BACKEND_NETNS
:
401 ifp
= if_lookup_by_name(name
, vrf_id
);
404 return if_create(name
, vrf_id
);
405 case VRF_BACKEND_VRF_LITE
:
406 ifp
= if_lookup_by_name_all_vrf(name
);
408 if (ifp
->vrf_id
== vrf_id
)
410 /* If it came from the kernel or by way of zclient,
411 * believe it and update the ifp accordingly.
413 if_update_to_new_vrf(ifp
, vrf_id
);
416 return if_create(name
, vrf_id
);
422 void if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
426 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
429 if (ifp
->ifindex
== ifindex
)
432 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
433 IFINDEX_RB_REMOVE(vrf
, ifp
)
435 ifp
->ifindex
= ifindex
;
437 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
438 IFINDEX_RB_INSERT(vrf
, ifp
)
441 /* Does interface up ? */
442 int if_is_up(struct interface
*ifp
)
444 return ifp
->flags
& IFF_UP
;
447 /* Is interface running? */
448 int if_is_running(struct interface
*ifp
)
450 return ifp
->flags
& IFF_RUNNING
;
453 /* Is the interface operative, eg. either UP & RUNNING
454 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
455 if ptm checking is enabled, then ptm check has passed */
456 int if_is_operative(struct interface
*ifp
)
458 return ((ifp
->flags
& IFF_UP
)
459 && (((ifp
->flags
& IFF_RUNNING
)
460 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
461 || !CHECK_FLAG(ifp
->status
,
462 ZEBRA_INTERFACE_LINKDETECTION
)));
465 /* Is the interface operative, eg. either UP & RUNNING
466 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
467 int if_is_no_ptm_operative(struct interface
*ifp
)
469 return ((ifp
->flags
& IFF_UP
)
470 && ((ifp
->flags
& IFF_RUNNING
)
471 || !CHECK_FLAG(ifp
->status
,
472 ZEBRA_INTERFACE_LINKDETECTION
)));
475 /* Is this loopback interface ? */
476 int if_is_loopback(struct interface
*ifp
)
478 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
479 * but Y on platform N?
481 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
484 /* Check interface is VRF */
485 int if_is_vrf(struct interface
*ifp
)
487 return CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
490 bool if_is_loopback_or_vrf(struct interface
*ifp
)
492 if (if_is_loopback(ifp
) || if_is_vrf(ifp
))
498 /* Does this interface support broadcast ? */
499 int if_is_broadcast(struct interface
*ifp
)
501 return ifp
->flags
& IFF_BROADCAST
;
504 /* Does this interface support broadcast ? */
505 int if_is_pointopoint(struct interface
*ifp
)
507 return ifp
->flags
& IFF_POINTOPOINT
;
510 /* Does this interface support multicast ? */
511 int if_is_multicast(struct interface
*ifp
)
513 return ifp
->flags
& IFF_MULTICAST
;
516 /* Printout flag information into log */
517 const char *if_flag_dump(unsigned long flag
)
520 static char logbuf
[BUFSIZ
];
522 #define IFF_OUT_LOG(X, STR) \
525 strlcat(logbuf, ",", BUFSIZ); \
528 strlcat(logbuf, STR, BUFSIZ); \
531 strlcpy(logbuf
, "<", BUFSIZ
);
532 IFF_OUT_LOG(IFF_UP
, "UP");
533 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
534 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
535 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
536 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
537 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
538 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
539 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
540 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
541 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
542 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
543 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
544 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
545 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
546 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
547 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
548 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
549 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
550 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
551 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
552 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
554 strlcat(logbuf
, ">", BUFSIZ
);
561 static void if_dump(const struct interface
*ifp
)
563 struct listnode
*node
;
564 struct connected
*c
__attribute__((unused
));
566 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
568 "Interface %s vrf %u index %d metric %d mtu %d "
570 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
,
571 ifp
->mtu
, ifp
->mtu6
, if_flag_dump(ifp
->flags
));
574 /* Interface printing for all interface. */
575 void if_dump_all(void)
580 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
581 FOR_ALL_INTERFACES (vrf
, ifp
)
586 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
587 * a seperate struct interface for each logical interface, so config
588 * file may be full of 'interface fooX:Y'. Solaris however does not
589 * expose logical interfaces via PF_ROUTE, so trying to track logical
590 * interfaces can be fruitless, for that reason Quagga only tracks
591 * the primary IP interface.
593 * We try accomodate SUNWzebra by:
594 * - looking up the interface name, to see whether it exists, if so
596 * - for protocol daemons, this could only because zebra told us of
598 * - for zebra, only because it learnt from kernel
600 * - search the name to see if it contains a sub-ipif / logical interface
601 * seperator, the ':' char. If it does:
602 * - text up to that char must be the primary name - get that name.
604 * - no idea, just get the name in its entirety.
606 static struct interface
*if_sunwzebra_get(const char *name
, vrf_id_t vrf_id
)
608 struct interface
*ifp
;
611 if ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
614 /* hunt the primary interface name... */
615 cp
= strchr(name
, ':');
619 return if_get_by_name(name
, vrf_id
);
624 /* For debug purpose. */
627 "show address [vrf NAME]",
633 struct listnode
*node
;
634 struct interface
*ifp
;
635 struct connected
*ifc
;
637 vrf_id_t vrf_id
= VRF_DEFAULT
;
640 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
642 FOR_ALL_INTERFACES (vrf
, ifp
)
644 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
648 if (p
->family
== AF_INET
)
649 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
655 DEFUN (show_address_vrf_all
,
656 show_address_vrf_all_cmd
,
657 "show address vrf all",
660 VRF_ALL_CMD_HELP_STR
)
663 struct listnode
*node
;
664 struct interface
*ifp
;
665 struct connected
*ifc
;
668 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
670 if (RB_EMPTY (if_name_head
, &vrf
->ifaces_by_name
))
673 vty_out (vty
, "\nVRF %u\n\n", vrf
->vrf_id
);
675 FOR_ALL_INTERFACES (vrf
, ifp
)
677 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
681 if (p
->family
== AF_INET
)
682 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
690 /* Allocate connected structure. */
691 struct connected
*connected_new(void)
693 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
696 /* Allocate nbr connected structure. */
697 struct nbr_connected
*nbr_connected_new(void)
699 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
702 /* Free connected structure. */
703 void connected_free(struct connected
*connected
)
705 if (connected
->address
)
706 prefix_free(connected
->address
);
708 if (connected
->destination
)
709 prefix_free(connected
->destination
);
711 if (connected
->label
)
712 XFREE(MTYPE_CONNECTED_LABEL
, connected
->label
);
714 XFREE(MTYPE_CONNECTED
, connected
);
717 /* Free nbr connected structure. */
718 void nbr_connected_free(struct nbr_connected
*connected
)
720 if (connected
->address
)
721 prefix_free(connected
->address
);
723 XFREE(MTYPE_NBR_CONNECTED
, connected
);
726 /* If same interface nbr address already exists... */
727 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
730 struct nbr_connected
*ifc
;
731 struct listnode
*node
;
733 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
734 if (prefix_same(ifc
->address
, p
))
740 /* Print if_addr structure. */
741 static void __attribute__((unused
))
742 connected_log(struct connected
*connected
, char *str
)
745 struct interface
*ifp
;
749 ifp
= connected
->ifp
;
750 p
= connected
->address
;
752 snprintf(logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ", str
,
753 ifp
->name
, ifp
->vrf_id
, prefix_family_str(p
),
754 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
756 p
= connected
->destination
;
758 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
759 BUFSIZ
- strlen(logbuf
));
761 zlog_info("%s", logbuf
);
764 /* Print if_addr structure. */
765 static void __attribute__((unused
))
766 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
769 struct interface
*ifp
;
773 ifp
= connected
->ifp
;
774 p
= connected
->address
;
776 snprintf(logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ", str
, ifp
->name
,
777 prefix_family_str(p
),
778 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
780 zlog_info("%s", logbuf
);
783 /* If two connected address has same prefix return 1. */
784 static int connected_same_prefix(struct prefix
*p1
, struct prefix
*p2
)
786 if (p1
->family
== p2
->family
) {
787 if (p1
->family
== AF_INET
788 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
790 if (p1
->family
== AF_INET6
791 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
797 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
800 struct listnode
*node
;
801 struct listnode
*next
;
802 struct connected
*ifc
;
804 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
805 ifc
= listgetdata(node
);
808 if (connected_same_prefix(ifc
->address
, p
))
814 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
817 struct listnode
*node
;
818 struct listnode
*next
;
819 struct connected
*ifc
;
821 /* In case of same prefix come, replace it with new one. */
822 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
823 ifc
= listgetdata(node
);
826 if (connected_same_prefix(ifc
->address
, p
)) {
827 listnode_delete(ifp
->connected
, ifc
);
834 /* Find the address on our side that will be used when packets
836 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
839 struct listnode
*cnode
;
841 struct connected
*match
;
845 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
846 if (c
->address
&& (c
->address
->family
== addr
->family
)
847 && prefix_match(CONNECTED_PREFIX(c
), addr
)
849 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
855 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
857 struct prefix
*destination
)
859 struct connected
*ifc
;
861 /* Allocate new connected address. */
862 ifc
= connected_new();
865 /* Fetch interface address */
866 ifc
->address
= prefix_new();
867 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
869 /* Fetch dest address */
871 ifc
->destination
= prefix_new();
872 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
875 /* Add connected address to the interface. */
876 listnode_add(ifp
->connected
, ifc
);
880 #if 0 /* this route_table of struct connected's is unused \
881 * however, it would be good to use a route_table rather than \
884 /* Interface looking up by interface's address. */
885 /* Interface's IPv4 address reverse lookup table. */
886 struct route_table
*ifaddr_ipv4_table
;
887 /* struct route_table *ifaddr_ipv6_table; */
890 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
892 struct route_node
*rn
;
893 struct prefix_ipv4 p
;
896 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
899 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
902 route_unlock_node (rn
);
903 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
904 inet_ntoa (*ifaddr
));
911 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
913 struct route_node
*rn
;
914 struct prefix_ipv4 p
;
917 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
920 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
923 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
924 inet_ntoa (*ifaddr
));
928 route_unlock_node (rn
);
929 route_unlock_node (rn
);
932 /* Lookup interface by interface's IP address or interface index. */
933 static struct interface
*
934 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
936 struct prefix_ipv4 p
;
937 struct route_node
*rn
;
938 struct interface
*ifp
;
943 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
946 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
951 route_unlock_node (rn
);
955 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
957 #endif /* ifaddr_ipv4_table */
959 void if_terminate(struct vrf
*vrf
)
961 struct interface
*ifp
;
963 while (!RB_EMPTY(if_name_head
, &vrf
->ifaces_by_name
)) {
964 ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
);
967 ifp
->node
->info
= NULL
;
968 route_unlock_node(ifp
->node
);
974 const char *if_link_type_str(enum zebra_link_type llt
)
977 #define llts(T,S) case (T): return (S)
978 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
979 llts(ZEBRA_LLT_ETHER
, "Ethernet");
980 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
981 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
982 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
983 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
984 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
985 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
986 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
987 llts(ZEBRA_LLT_ATM
, "ATM");
988 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
989 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
990 llts(ZEBRA_LLT_EUI64
, "EUI-64");
991 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
992 llts(ZEBRA_LLT_SLIP
, "SLIP");
993 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
994 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
995 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
996 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
997 llts(ZEBRA_LLT_X25
, "CCITT X.25");
998 llts(ZEBRA_LLT_PPP
, "PPP");
999 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1000 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1001 llts(ZEBRA_LLT_LAPB
, "LAPB");
1002 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1003 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1004 llts(ZEBRA_LLT_FRAD
, "FRAD");
1005 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1006 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1007 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1008 llts(ZEBRA_LLT_FDDI
, "FDDI");
1009 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1010 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1011 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1012 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1013 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1014 llts(ZEBRA_LLT_IRDA
, "IrDA");
1015 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1016 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1017 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1018 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1019 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1020 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1021 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1022 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1023 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1025 flog_err(EC_LIB_DEVELOPMENT
, "Unknown value %d", llt
);
1026 return "Unknown type!";
1032 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1036 if (ifp
->link_params
!= NULL
)
1037 return ifp
->link_params
;
1039 struct if_link_params
*iflp
=
1040 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1044 /* Set TE metric equal to standard metric */
1045 iflp
->te_metric
= ifp
->metric
;
1047 /* Compute default bandwidth based on interface */
1049 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1050 * TE_KILO_BIT
/ TE_BYTE
);
1052 /* Set Max, Reservable and Unreserved Bandwidth */
1053 iflp
->max_bw
= iflp
->default_bw
;
1054 iflp
->max_rsv_bw
= iflp
->default_bw
;
1055 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1056 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1058 /* Update Link parameters status */
1060 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1062 /* Finally attach newly created Link Parameters */
1063 ifp
->link_params
= iflp
;
1068 void if_link_params_free(struct interface
*ifp
)
1070 if (ifp
->link_params
== NULL
)
1072 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1073 ifp
->link_params
= NULL
;
1076 /* ----------- CLI commands ----------- */
1079 * XPath: /frr-interface:lib/interface
1081 DEFPY_NOSH (interface
,
1083 "interface IFNAME [vrf NAME$vrfname]",
1084 "Select an interface to configure\n"
1085 "Interface's name\n"
1088 char xpath_list
[XPATH_MAXLEN
];
1089 struct cli_config_change changes
[] = {
1092 .operation
= NB_OP_CREATE
,
1096 struct interface
*ifp
;
1100 vrfname
= VRF_DEFAULT_NAME
;
1103 * This command requires special handling to maintain backward
1104 * compatibility. If a VRF name is not specified, it means we're willing
1105 * to accept any interface with the given name on any VRF. If no
1106 * interface is found, then a new one should be created on the default
1109 VRF_GET_ID(vrf_id
, vrfname
, false);
1110 ifp
= if_lookup_by_name_all_vrf(ifname
);
1111 if (ifp
&& ifp
->vrf_id
!= vrf_id
) {
1115 * Special case 1: a VRF name was specified, but the found
1116 * interface is associated to different VRF. Reject the command.
1118 if (vrf_id
!= VRF_DEFAULT
) {
1119 vty_out(vty
, "%% interface %s not in %s vrf\n", ifname
,
1121 return CMD_WARNING_CONFIG_FAILED
;
1125 * Special case 2: a VRF name was *not* specified, and the found
1126 * interface is associated to a VRF other than the default one.
1127 * Update vrf_id and vrfname to account for that.
1129 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
1131 vrf_id
= ifp
->vrf_id
;
1132 vrfname
= vrf
->name
;
1135 snprintf(xpath_list
, sizeof(xpath_list
),
1136 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname
,
1139 ret
= nb_cli_cfg_change(vty
, xpath_list
, changes
, array_size(changes
));
1140 if (ret
== CMD_SUCCESS
) {
1141 VTY_PUSH_XPATH(INTERFACE_NODE
, xpath_list
);
1144 * For backward compatibility with old commands we still need
1145 * to use the qobj infrastructure. This can be removed once
1146 * all interface-level commands are converted to the new
1149 ifp
= if_lookup_by_name(ifname
, vrf_id
);
1151 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
1157 DEFPY (no_interface
,
1159 "no interface IFNAME [vrf NAME$vrfname]",
1161 "Delete a pseudo interface's configuration\n"
1162 "Interface's name\n"
1165 char xpath_list
[XPATH_MAXLEN
];
1166 struct cli_config_change changes
[] = {
1169 .operation
= NB_OP_DELETE
,
1174 vrfname
= VRF_DEFAULT_NAME
;
1176 snprintf(xpath_list
, sizeof(xpath_list
),
1177 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname
,
1180 return nb_cli_cfg_change(vty
, xpath_list
, changes
, array_size(changes
));
1183 static void cli_show_interface(struct vty
*vty
, struct lyd_node
*dnode
,
1188 vrf
= yang_dnode_get_string(dnode
, "./vrf");
1190 vty_out(vty
, "!\n");
1191 vty_out(vty
, "interface %s", yang_dnode_get_string(dnode
, "./name"));
1192 if (!strmatch(vrf
, VRF_DEFAULT_NAME
))
1193 vty_out(vty
, " vrf %s", vrf
);
1198 * XPath: /frr-interface:lib/interface/description
1200 DEFPY (interface_desc
,
1202 "description LINE...",
1203 "Interface specific description\n"
1204 "Characters describing this interface\n")
1206 struct cli_config_change changes
[] = {
1208 .xpath
= "./description",
1209 .operation
= NB_OP_MODIFY
,
1215 desc
= argv_concat(argv
, argc
, 1);
1216 changes
[0].value
= desc
;
1217 ret
= nb_cli_cfg_change(vty
, NULL
, changes
, array_size(changes
));
1218 XFREE(MTYPE_TMP
, desc
);
1223 DEFPY (no_interface_desc
,
1224 no_interface_desc_cmd
,
1227 "Interface specific description\n")
1229 struct cli_config_change changes
[] = {
1231 .xpath
= "./description",
1232 .operation
= NB_OP_DELETE
,
1236 return nb_cli_cfg_change(vty
, NULL
, changes
, array_size(changes
));
1239 static void cli_show_interface_desc(struct vty
*vty
, struct lyd_node
*dnode
,
1242 vty_out(vty
, " description %s\n", yang_dnode_get_string(dnode
, NULL
));
1245 /* Interface autocomplete. */
1246 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
1248 struct interface
*ifp
;
1251 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1252 FOR_ALL_INTERFACES (vrf
, ifp
) {
1253 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
1258 static const struct cmd_variable_handler if_var_handlers
[] = {
1259 {/* "interface NAME" */
1260 .varname
= "interface",
1261 .completions
= if_autocomplete
},
1262 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
1263 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
1264 {.completions
= NULL
}};
1266 void if_cmd_init(void)
1268 cmd_variable_handler_register(if_var_handlers
);
1270 install_element(CONFIG_NODE
, &interface_cmd
);
1271 install_element(CONFIG_NODE
, &no_interface_cmd
);
1273 install_default(INTERFACE_NODE
);
1274 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
1275 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
1278 /* ------- Northbound callbacks ------- */
1281 * XPath: /frr-interface:lib/interface
1283 static int lib_interface_create(enum nb_event event
,
1284 const struct lyd_node
*dnode
,
1285 union nb_resource
*resource
)
1288 const char *vrfname
;
1290 struct interface
*ifp
;
1292 ifname
= yang_dnode_get_string(dnode
, "./name");
1293 vrfname
= yang_dnode_get_string(dnode
, "./vrf");
1296 case NB_EV_VALIDATE
:
1297 vrf
= vrf_lookup_by_name(vrfname
);
1299 zlog_warn("%s: VRF %s doesn't exist", __func__
,
1301 return NB_ERR_VALIDATION
;
1303 if (vrf
->vrf_id
== VRF_UNKNOWN
) {
1304 zlog_warn("%s: VRF %s is not active", __func__
,
1306 return NB_ERR_VALIDATION
;
1308 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE
) {
1309 ifp
= if_lookup_by_name_all_vrf(ifname
);
1310 if (ifp
&& ifp
->vrf_id
!= vrf
->vrf_id
) {
1312 "%s: interface %s already exists in another VRF",
1313 __func__
, ifp
->name
);
1314 return NB_ERR_VALIDATION
;
1322 vrf
= vrf_lookup_by_name(vrfname
);
1325 ifp
= if_sunwzebra_get(ifname
, vrf
->vrf_id
);
1327 ifp
= if_get_by_name(ifname
, vrf
->vrf_id
);
1328 #endif /* SUNOS_5 */
1329 yang_dnode_set_entry(dnode
, ifp
);
1336 static int lib_interface_delete(enum nb_event event
,
1337 const struct lyd_node
*dnode
)
1339 struct interface
*ifp
;
1341 ifp
= yang_dnode_get_entry(dnode
);
1344 case NB_EV_VALIDATE
:
1345 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1346 zlog_warn("%s: only inactive interfaces can be deleted",
1348 return NB_ERR_VALIDATION
;
1363 * XPath: /frr-interface:lib/interface/description
1365 static int lib_interface_description_modify(enum nb_event event
,
1366 const struct lyd_node
*dnode
,
1367 union nb_resource
*resource
)
1369 struct interface
*ifp
;
1370 const char *description
;
1372 if (event
!= NB_EV_APPLY
)
1375 ifp
= yang_dnode_get_entry(dnode
);
1377 XFREE(MTYPE_TMP
, ifp
->desc
);
1378 description
= yang_dnode_get_string(dnode
, NULL
);
1379 ifp
->desc
= XSTRDUP(MTYPE_TMP
, description
);
1384 static int lib_interface_description_delete(enum nb_event event
,
1385 const struct lyd_node
*dnode
)
1387 struct interface
*ifp
;
1389 if (event
!= NB_EV_APPLY
)
1392 ifp
= yang_dnode_get_entry(dnode
);
1394 XFREE(MTYPE_TMP
, ifp
->desc
);
1399 /* clang-format off */
1400 const struct frr_yang_module_info frr_interface_info
= {
1401 .name
= "frr-interface",
1404 .xpath
= "/frr-interface:lib/interface",
1405 .cbs
.create
= lib_interface_create
,
1406 .cbs
.delete = lib_interface_delete
,
1407 .cbs
.cli_show
= cli_show_interface
,
1410 .xpath
= "/frr-interface:lib/interface/description",
1411 .cbs
.modify
= lib_interface_description_modify
,
1412 .cbs
.delete = lib_interface_description_delete
,
1413 .cbs
.cli_show
= cli_show_interface_desc
,