3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "sockunion.h"
37 DEFINE_MTYPE(LIB
, IF
, "Interface")
38 DEFINE_MTYPE_STATIC(LIB
, CONNECTED
, "Connected")
39 DEFINE_MTYPE_STATIC(LIB
, NBR_CONNECTED
, "Neighbor Connected")
40 DEFINE_MTYPE(LIB
, CONNECTED_LABEL
, "Connected interface label")
41 DEFINE_MTYPE_STATIC(LIB
, IF_LINK_PARAMS
, "Informational Link Parameters")
43 static int if_cmp_func(const struct interface
*, const struct interface
*);
44 static int if_cmp_index_func(const struct interface
*ifp1
,
45 const struct interface
*ifp2
);
46 RB_GENERATE(if_name_head
, interface
, name_entry
, if_cmp_func
);
47 RB_GENERATE(if_index_head
, interface
, index_entry
, if_cmp_index_func
);
49 DEFINE_QOBJ_TYPE(interface
)
51 DEFINE_HOOK(if_add
, (struct interface
*ifp
), (ifp
))
52 DEFINE_KOOH(if_del
, (struct interface
*ifp
), (ifp
))
54 /* List of interfaces in only the default VRF */
57 /* Compare interface names, returning an integer greater than, equal to, or
58 * less than 0, (following the strcmp convention), according to the
59 * relationship between ifp1 and ifp2. Interface names consist of an
60 * alphabetic prefix and a numeric suffix. The primary sort key is
61 * lexicographic by name, and then numeric by number. No number sorts
62 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
65 int if_cmp_name_func(char *p1
, char *p2
)
72 /* look up to any number */
73 l1
= strcspn(p1
, "0123456789");
74 l2
= strcspn(p2
, "0123456789");
76 /* name lengths are different -> compare names */
78 return (strcmp(p1
, p2
));
80 /* Note that this relies on all numbers being less than all
84 res
= strncmp(p1
, p2
, l1
);
86 /* names are different -> compare them */
90 /* with identical name part, go to numeric part */
99 x1
= strtol(p1
, &p1
, 10);
100 x2
= strtol(p2
, &p2
, 10);
102 /* let's compare numbers now */
108 /* numbers were equal, lets do it again..
109 (it happens with name like "eth123.456:789") */
118 static int if_cmp_func(const struct interface
*ifp1
,
119 const struct interface
*ifp2
)
121 return if_cmp_name_func((char *)ifp1
->name
, (char *)ifp2
->name
);
124 static int if_cmp_index_func(const struct interface
*ifp1
,
125 const struct interface
*ifp2
)
127 return ifp1
->ifindex
- ifp2
->ifindex
;
130 /* Create new interface structure. */
131 struct interface
*if_create(const char *name
, vrf_id_t vrf_id
)
133 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
134 struct interface
*ifp
;
136 ifp
= XCALLOC(MTYPE_IF
, sizeof(struct interface
));
137 ifp
->ifindex
= IFINDEX_INTERNAL
;
140 strlcpy(ifp
->name
, name
, sizeof(ifp
->name
));
141 ifp
->vrf_id
= vrf_id
;
142 IFNAME_RB_INSERT(vrf
, ifp
);
143 ifp
->connected
= list_new();
144 ifp
->connected
->del
= (void (*)(void *))connected_free
;
146 ifp
->nbr_connected
= list_new();
147 ifp
->nbr_connected
->del
= (void (*)(void *))nbr_connected_free
;
149 /* Enable Link-detection by default */
150 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
152 QOBJ_REG(ifp
, interface
);
153 hook_call(if_add
, ifp
);
157 /* Create new interface structure. */
158 void if_update_to_new_vrf(struct interface
*ifp
, vrf_id_t vrf_id
)
162 /* remove interface from old master vrf list */
163 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
165 IFNAME_RB_REMOVE(vrf
, ifp
);
166 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
167 IFINDEX_RB_REMOVE(vrf
, ifp
);
170 ifp
->vrf_id
= vrf_id
;
171 vrf
= vrf_get(ifp
->vrf_id
, NULL
);
173 IFNAME_RB_INSERT(vrf
, ifp
);
174 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
175 IFINDEX_RB_INSERT(vrf
, ifp
);
179 /* Delete interface structure. */
180 void if_delete_retain(struct interface
*ifp
)
182 hook_call(if_del
, ifp
);
185 /* Free connected address list */
186 list_delete_all_node(ifp
->connected
);
188 /* Free connected nbr address list */
189 list_delete_all_node(ifp
->nbr_connected
);
192 /* Delete and free interface structure. */
193 void if_delete(struct interface
*ifp
)
195 struct vrf
*vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
197 IFNAME_RB_REMOVE(vrf
, ifp
);
198 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
199 IFINDEX_RB_REMOVE(vrf
, ifp
);
201 if_delete_retain(ifp
);
203 list_delete_and_null(&ifp
->connected
);
204 list_delete_and_null(&ifp
->nbr_connected
);
206 if_link_params_free(ifp
);
208 XFREE(MTYPE_IF
, ifp
);
211 /* Interface existance check by index. */
212 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
214 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
215 struct interface if_tmp
;
217 if_tmp
.ifindex
= ifindex
;
218 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
221 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
223 struct interface
*ifp
;
225 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
230 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
232 struct interface
*ifp
;
234 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
239 /* Interface existance check by interface name. */
240 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
242 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
243 struct interface if_tmp
;
245 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
248 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
249 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
252 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
255 struct interface
*ifp
;
257 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
260 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
261 ifp
= if_lookup_by_name(name
, vrf
->vrf_id
);
269 /* Lookup interface by IPv4 address. */
270 struct interface
*if_lookup_exact_address(void *src
, int family
,
273 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
274 struct listnode
*cnode
;
275 struct interface
*ifp
;
279 RB_FOREACH (ifp
, if_name_head
, &vrf
->ifaces_by_name
) {
280 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
283 if (p
&& (p
->family
== family
)) {
284 if (family
== AF_INET
) {
287 (struct in_addr
*)src
))
289 } else if (family
== AF_INET6
) {
292 (struct in6_addr
*)src
))
301 /* Lookup interface by IPv4 address. */
302 struct connected
*if_lookup_address(void *matchaddr
, int family
,
305 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
308 struct listnode
*cnode
;
309 struct interface
*ifp
;
311 struct connected
*match
;
313 if (family
== AF_INET
) {
314 addr
.family
= AF_INET
;
315 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
316 addr
.prefixlen
= IPV4_MAX_BITLEN
;
317 } else if (family
== AF_INET6
) {
318 addr
.family
= AF_INET6
;
319 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
320 addr
.prefixlen
= IPV6_MAX_BITLEN
;
325 RB_FOREACH (ifp
, if_name_head
, &vrf
->ifaces_by_name
) {
326 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
327 if (c
->address
&& (c
->address
->family
== AF_INET
)
328 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
329 && (c
->address
->prefixlen
> bestlen
)) {
330 bestlen
= c
->address
->prefixlen
;
338 /* Lookup interface by prefix */
339 struct interface
*if_lookup_prefix(struct prefix
*prefix
, vrf_id_t vrf_id
)
341 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
342 struct listnode
*cnode
;
343 struct interface
*ifp
;
346 RB_FOREACH (ifp
, if_name_head
, &vrf
->ifaces_by_name
) {
347 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
348 if (prefix_cmp(c
->address
, prefix
) == 0) {
356 /* Get interface by name if given name interface doesn't exist create
358 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
, int vty
)
360 struct interface
*ifp
;
362 ifp
= if_lookup_by_name_all_vrf(name
);
364 if (ifp
->vrf_id
== vrf_id
)
367 /* Found a match on a different VRF. If the interface command
368 * was entered in vty without a VRF (passed as VRF_DEFAULT),
369 * accept the ifp we found. If a vrf was entered and there is
370 * a mismatch, reject it if from vty. If it came from the kernel
371 * or by way of zclient, believe it and update the ifp
375 if (vrf_id
== VRF_DEFAULT
)
379 if_update_to_new_vrf(ifp
, vrf_id
);
384 return if_create(name
, vrf_id
);
387 void if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
389 struct vrf
*vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
391 if (ifp
->ifindex
== ifindex
)
394 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
395 IFINDEX_RB_REMOVE(vrf
, ifp
)
397 ifp
->ifindex
= ifindex
;
399 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
400 IFINDEX_RB_INSERT(vrf
, ifp
)
403 /* Does interface up ? */
404 int if_is_up(struct interface
*ifp
)
406 return ifp
->flags
& IFF_UP
;
409 /* Is interface running? */
410 int if_is_running(struct interface
*ifp
)
412 return ifp
->flags
& IFF_RUNNING
;
415 /* Is the interface operative, eg. either UP & RUNNING
416 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
417 if ptm checking is enabled, then ptm check has passed */
418 int if_is_operative(struct interface
*ifp
)
420 return ((ifp
->flags
& IFF_UP
)
421 && (((ifp
->flags
& IFF_RUNNING
)
422 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
423 || !CHECK_FLAG(ifp
->status
,
424 ZEBRA_INTERFACE_LINKDETECTION
)));
427 /* Is the interface operative, eg. either UP & RUNNING
428 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
429 int if_is_no_ptm_operative(struct interface
*ifp
)
431 return ((ifp
->flags
& IFF_UP
)
432 && ((ifp
->flags
& IFF_RUNNING
)
433 || !CHECK_FLAG(ifp
->status
,
434 ZEBRA_INTERFACE_LINKDETECTION
)));
437 /* Is this loopback interface ? */
438 int if_is_loopback(struct interface
*ifp
)
440 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
441 * but Y on platform N?
443 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
446 /* Does this interface support broadcast ? */
447 int if_is_broadcast(struct interface
*ifp
)
449 return ifp
->flags
& IFF_BROADCAST
;
452 /* Does this interface support broadcast ? */
453 int if_is_pointopoint(struct interface
*ifp
)
455 return ifp
->flags
& IFF_POINTOPOINT
;
458 /* Does this interface support multicast ? */
459 int if_is_multicast(struct interface
*ifp
)
461 return ifp
->flags
& IFF_MULTICAST
;
464 /* Printout flag information into log */
465 const char *if_flag_dump(unsigned long flag
)
468 static char logbuf
[BUFSIZ
];
470 #define IFF_OUT_LOG(X, STR) \
473 strlcat(logbuf, ",", BUFSIZ); \
476 strlcat(logbuf, STR, BUFSIZ); \
479 strlcpy(logbuf
, "<", BUFSIZ
);
480 IFF_OUT_LOG(IFF_UP
, "UP");
481 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
482 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
483 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
484 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
485 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
486 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
487 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
488 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
489 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
490 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
491 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
492 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
493 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
494 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
495 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
496 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
497 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
498 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
499 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
500 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
502 strlcat(logbuf
, ">", BUFSIZ
);
509 static void if_dump(const struct interface
*ifp
)
511 struct listnode
*node
;
512 struct connected
*c
__attribute__((unused
));
514 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
516 "Interface %s vrf %u index %d metric %d mtu %d "
518 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
,
519 ifp
->mtu
, ifp
->mtu6
, if_flag_dump(ifp
->flags
));
522 /* Interface printing for all interface. */
523 void if_dump_all(void)
528 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
529 RB_FOREACH (ifp
, if_name_head
, &vrf
->ifaces_by_name
)
533 DEFUN (interface_desc
,
535 "description LINE...",
536 "Interface specific description\n"
537 "Characters describing this interface\n")
540 VTY_DECLVAR_CONTEXT(interface
, ifp
);
543 XFREE(MTYPE_TMP
, ifp
->desc
);
544 ifp
->desc
= argv_concat(argv
, argc
, idx_line
);
549 DEFUN (no_interface_desc
,
550 no_interface_desc_cmd
,
553 "Interface specific description\n")
555 VTY_DECLVAR_CONTEXT(interface
, ifp
);
558 XFREE(MTYPE_TMP
, ifp
->desc
);
565 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
566 * a seperate struct interface for each logical interface, so config
567 * file may be full of 'interface fooX:Y'. Solaris however does not
568 * expose logical interfaces via PF_ROUTE, so trying to track logical
569 * interfaces can be fruitless, for that reason Quagga only tracks
570 * the primary IP interface.
572 * We try accomodate SUNWzebra by:
573 * - looking up the interface name, to see whether it exists, if so
575 * - for protocol daemons, this could only because zebra told us of
577 * - for zebra, only because it learnt from kernel
579 * - search the name to see if it contains a sub-ipif / logical interface
580 * seperator, the ':' char. If it does:
581 * - text up to that char must be the primary name - get that name.
583 * - no idea, just get the name in its entirety.
585 static struct interface
*if_sunwzebra_get(char *name
, vrf_id_t vrf_id
)
587 struct interface
*ifp
;
590 if ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
593 /* hunt the primary interface name... */
594 cp
= strchr(name
, ':');
598 return if_get_by_name(name
, vrf_id
, 1);
604 "interface IFNAME [vrf NAME]",
605 "Select an interface to configure\n"
611 const char *ifname
= argv
[idx_ifname
]->arg
;
612 const char *vrfname
= (argc
> 2) ? argv
[idx_vrf
]->arg
: NULL
;
614 struct interface
*ifp
;
615 vrf_id_t vrf_id
= VRF_DEFAULT
;
617 if (strlen(ifname
) > INTERFACE_NAMSIZ
) {
619 "%% Interface name %s is invalid: length exceeds "
621 ifname
, INTERFACE_NAMSIZ
);
622 return CMD_WARNING_CONFIG_FAILED
;
625 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
626 Imagine forward reference of a vrf by name in this interface config */
628 VRF_GET_ID(vrf_id
, vrfname
);
631 ifp
= if_sunwzebra_get(ifname
, vrf_id
);
633 ifp
= if_get_by_name(ifname
, vrf_id
, 1);
637 vty_out(vty
, "%% interface %s not in %s\n", ifname
, vrfname
);
638 return CMD_WARNING_CONFIG_FAILED
;
640 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
645 DEFUN_NOSH (no_interface
,
647 "no interface IFNAME [vrf NAME]",
649 "Delete a pseudo interface's configuration\n"
653 const char *ifname
= argv
[2]->arg
;
654 const char *vrfname
= (argc
> 3) ? argv
[3]->arg
: NULL
;
656 // deleting interface
657 struct interface
*ifp
;
658 vrf_id_t vrf_id
= VRF_DEFAULT
;
661 VRF_GET_ID(vrf_id
, vrfname
);
663 ifp
= if_lookup_by_name(ifname
, vrf_id
);
666 vty_out(vty
, "%% Interface %s does not exist\n", ifname
);
667 return CMD_WARNING_CONFIG_FAILED
;
670 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
671 vty_out(vty
, "%% Only inactive interfaces can be deleted\n");
672 return CMD_WARNING_CONFIG_FAILED
;
680 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
682 struct interface
*ifp
;
683 struct vrf
*vrf
= NULL
;
685 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
686 RB_FOREACH (ifp
, if_name_head
, &vrf
->ifaces_by_name
) {
687 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
692 static const struct cmd_variable_handler if_var_handlers
[] = {
693 {/* "interface NAME" */
694 .varname
= "interface",
695 .completions
= if_autocomplete
},
696 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
697 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
698 {.completions
= NULL
}};
700 void if_cmd_init(void)
702 cmd_variable_handler_register(if_var_handlers
);
704 install_element(CONFIG_NODE
, &interface_cmd
);
705 install_element(CONFIG_NODE
, &no_interface_cmd
);
707 install_default(INTERFACE_NODE
);
708 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
709 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
713 /* For debug purpose. */
716 "show address [vrf NAME]",
722 struct listnode
*node
;
723 struct interface
*ifp
;
724 struct connected
*ifc
;
726 vrf_id_t vrf_id
= VRF_DEFAULT
;
729 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
731 RB_FOREACH (ifp
, if_name_head
, &vrf
->ifaces_by_name
)
733 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
737 if (p
->family
== AF_INET
)
738 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
744 DEFUN (show_address_vrf_all
,
745 show_address_vrf_all_cmd
,
746 "show address vrf all",
749 VRF_ALL_CMD_HELP_STR
)
752 struct listnode
*node
;
753 struct interface
*ifp
;
754 struct connected
*ifc
;
757 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
759 if (RB_EMPTY (if_name_head
, &vrf
->ifaces_by_name
))
762 vty_out (vty
, "\nVRF %u\n\n", vrf
->vrf_id
);
764 RB_FOREACH (ifp
, if_name_head
, &vrf
->ifaces_by_name
)
766 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
770 if (p
->family
== AF_INET
)
771 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
779 /* Allocate connected structure. */
780 struct connected
*connected_new(void)
782 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
785 /* Allocate nbr connected structure. */
786 struct nbr_connected
*nbr_connected_new(void)
788 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
791 /* Free connected structure. */
792 void connected_free(struct connected
*connected
)
794 if (connected
->address
)
795 prefix_free(connected
->address
);
797 if (connected
->destination
)
798 prefix_free(connected
->destination
);
800 if (connected
->label
)
801 XFREE(MTYPE_CONNECTED_LABEL
, connected
->label
);
803 XFREE(MTYPE_CONNECTED
, connected
);
806 /* Free nbr connected structure. */
807 void nbr_connected_free(struct nbr_connected
*connected
)
809 if (connected
->address
)
810 prefix_free(connected
->address
);
812 XFREE(MTYPE_NBR_CONNECTED
, connected
);
815 /* If same interface nbr address already exists... */
816 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
819 struct nbr_connected
*ifc
;
820 struct listnode
*node
;
822 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
823 if (prefix_same(ifc
->address
, p
))
829 /* Print if_addr structure. */
830 static void __attribute__((unused
))
831 connected_log(struct connected
*connected
, char *str
)
834 struct interface
*ifp
;
838 ifp
= connected
->ifp
;
839 p
= connected
->address
;
841 snprintf(logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ", str
,
842 ifp
->name
, ifp
->vrf_id
, prefix_family_str(p
),
843 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
845 p
= connected
->destination
;
847 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
848 BUFSIZ
- strlen(logbuf
));
850 zlog_info("%s", logbuf
);
853 /* Print if_addr structure. */
854 static void __attribute__((unused
))
855 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
858 struct interface
*ifp
;
862 ifp
= connected
->ifp
;
863 p
= connected
->address
;
865 snprintf(logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ", str
, ifp
->name
,
866 prefix_family_str(p
),
867 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
869 zlog_info("%s", logbuf
);
872 /* If two connected address has same prefix return 1. */
873 static int connected_same_prefix(struct prefix
*p1
, struct prefix
*p2
)
875 if (p1
->family
== p2
->family
) {
876 if (p1
->family
== AF_INET
877 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
879 if (p1
->family
== AF_INET6
880 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
886 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
889 struct listnode
*node
;
890 struct listnode
*next
;
891 struct connected
*ifc
;
893 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
894 ifc
= listgetdata(node
);
897 if (connected_same_prefix(ifc
->address
, p
))
903 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
906 struct listnode
*node
;
907 struct listnode
*next
;
908 struct connected
*ifc
;
910 /* In case of same prefix come, replace it with new one. */
911 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
912 ifc
= listgetdata(node
);
915 if (connected_same_prefix(ifc
->address
, p
)) {
916 listnode_delete(ifp
->connected
, ifc
);
923 /* Find the address on our side that will be used when packets
925 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
928 struct listnode
*cnode
;
930 struct connected
*match
;
934 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
935 if (c
->address
&& (c
->address
->family
== addr
->family
)
936 && prefix_match(CONNECTED_PREFIX(c
), addr
)
938 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
944 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
946 struct prefix
*destination
)
948 struct connected
*ifc
;
950 /* Allocate new connected address. */
951 ifc
= connected_new();
954 /* Fetch interface address */
955 ifc
->address
= prefix_new();
956 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
958 /* Fetch dest address */
960 ifc
->destination
= prefix_new();
961 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
964 /* Add connected address to the interface. */
965 listnode_add(ifp
->connected
, ifc
);
969 #if 0 /* this route_table of struct connected's is unused \
970 * however, it would be good to use a route_table rather than \
973 /* Interface looking up by interface's address. */
974 /* Interface's IPv4 address reverse lookup table. */
975 struct route_table
*ifaddr_ipv4_table
;
976 /* struct route_table *ifaddr_ipv6_table; */
979 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
981 struct route_node
*rn
;
982 struct prefix_ipv4 p
;
985 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
988 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
991 route_unlock_node (rn
);
992 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
993 inet_ntoa (*ifaddr
));
1000 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1002 struct route_node
*rn
;
1003 struct prefix_ipv4 p
;
1006 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1009 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1012 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1013 inet_ntoa (*ifaddr
));
1017 route_unlock_node (rn
);
1018 route_unlock_node (rn
);
1021 /* Lookup interface by interface's IP address or interface index. */
1022 static struct interface
*
1023 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1025 struct prefix_ipv4 p
;
1026 struct route_node
*rn
;
1027 struct interface
*ifp
;
1032 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1035 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1040 route_unlock_node (rn
);
1044 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1046 #endif /* ifaddr_ipv4_table */
1048 void if_terminate(struct vrf
*vrf
)
1050 struct interface
*ifp
;
1052 while ((ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
)) != NULL
) {
1054 ifp
->node
->info
= NULL
;
1055 route_unlock_node(ifp
->node
);
1061 const char *if_link_type_str(enum zebra_link_type llt
)
1064 #define llts(T,S) case (T): return (S)
1065 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1066 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1067 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1068 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1069 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1070 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1071 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1072 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1073 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1074 llts(ZEBRA_LLT_ATM
, "ATM");
1075 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1076 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1077 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1078 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1079 llts(ZEBRA_LLT_SLIP
, "SLIP");
1080 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1081 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1082 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1083 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1084 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1085 llts(ZEBRA_LLT_PPP
, "PPP");
1086 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1087 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1088 llts(ZEBRA_LLT_LAPB
, "LAPB");
1089 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1090 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1091 llts(ZEBRA_LLT_FRAD
, "FRAD");
1092 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1093 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1094 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1095 llts(ZEBRA_LLT_FDDI
, "FDDI");
1096 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1097 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1098 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1099 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1100 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1101 llts(ZEBRA_LLT_IRDA
, "IrDA");
1102 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1103 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1104 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1105 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1106 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1107 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1108 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1109 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1110 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1112 zlog_warn("Unknown value %d", llt
);
1113 return "Unknown type!";
1119 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1123 if (ifp
->link_params
!= NULL
)
1124 return ifp
->link_params
;
1126 struct if_link_params
*iflp
=
1127 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1131 /* Set TE metric equal to standard metric */
1132 iflp
->te_metric
= ifp
->metric
;
1134 /* Compute default bandwidth based on interface */
1136 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1137 * TE_KILO_BIT
/ TE_BYTE
);
1139 /* Set Max, Reservable and Unreserved Bandwidth */
1140 iflp
->max_bw
= iflp
->default_bw
;
1141 iflp
->max_rsv_bw
= iflp
->default_bw
;
1142 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1143 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1145 /* Update Link parameters status */
1147 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1149 /* Finally attach newly created Link Parameters */
1150 ifp
->link_params
= iflp
;
1155 void if_link_params_free(struct interface
*ifp
)
1157 if (ifp
->link_params
== NULL
)
1159 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1160 ifp
->link_params
= NULL
;