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 */
101 x1
= strtol(p1
, &p1
, 10);
102 x2
= strtol(p2
, &p2
, 10);
104 /* let's compare numbers now */
110 /* numbers were equal, lets do it again..
111 (it happens with name like "eth123.456:789") */
120 static int if_cmp_func(const struct interface
*ifp1
,
121 const struct interface
*ifp2
)
123 return if_cmp_name_func((char *)ifp1
->name
, (char *)ifp2
->name
);
126 static int if_cmp_index_func(const struct interface
*ifp1
,
127 const struct interface
*ifp2
)
129 return ifp1
->ifindex
- ifp2
->ifindex
;
132 /* Create new interface structure. */
133 struct interface
*if_create(const char *name
, vrf_id_t vrf_id
)
135 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
136 struct interface
*ifp
;
138 ifp
= XCALLOC(MTYPE_IF
, sizeof(struct interface
));
139 ifp
->ifindex
= IFINDEX_INTERNAL
;
142 strlcpy(ifp
->name
, name
, sizeof(ifp
->name
));
143 ifp
->vrf_id
= vrf_id
;
144 IFNAME_RB_INSERT(vrf
, ifp
);
145 ifp
->connected
= list_new();
146 ifp
->connected
->del
= (void (*)(void *))connected_free
;
148 ifp
->nbr_connected
= list_new();
149 ifp
->nbr_connected
->del
= (void (*)(void *))nbr_connected_free
;
151 /* Enable Link-detection by default */
152 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
154 QOBJ_REG(ifp
, interface
);
155 hook_call(if_add
, ifp
);
159 /* Create new interface structure. */
160 void if_update_to_new_vrf(struct interface
*ifp
, vrf_id_t vrf_id
)
164 /* remove interface from old master vrf list */
165 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
167 IFNAME_RB_REMOVE(vrf
, ifp
);
168 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
169 IFINDEX_RB_REMOVE(vrf
, ifp
);
172 ifp
->vrf_id
= vrf_id
;
173 vrf
= vrf_get(ifp
->vrf_id
, NULL
);
175 IFNAME_RB_INSERT(vrf
, ifp
);
176 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
177 IFINDEX_RB_INSERT(vrf
, ifp
);
181 /* Delete interface structure. */
182 void if_delete_retain(struct interface
*ifp
)
184 hook_call(if_del
, ifp
);
187 /* Free connected address list */
188 list_delete_all_node(ifp
->connected
);
190 /* Free connected nbr address list */
191 list_delete_all_node(ifp
->nbr_connected
);
194 /* Delete and free interface structure. */
195 void if_delete(struct interface
*ifp
)
199 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
202 IFNAME_RB_REMOVE(vrf
, ifp
);
203 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
204 IFINDEX_RB_REMOVE(vrf
, ifp
);
206 if_delete_retain(ifp
);
208 list_delete_and_null(&ifp
->connected
);
209 list_delete_and_null(&ifp
->nbr_connected
);
211 if_link_params_free(ifp
);
214 XFREE(MTYPE_TMP
, ifp
->desc
);
216 XFREE(MTYPE_IF
, ifp
);
219 /* Interface existance check by index. */
220 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
223 struct interface if_tmp
;
225 vrf
= vrf_lookup_by_id(vrf_id
);
229 if_tmp
.ifindex
= ifindex
;
230 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
233 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
235 struct interface
*ifp
;
237 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
242 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
244 struct interface
*ifp
;
246 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
251 /* Interface existance check by interface name. */
252 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
254 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
255 struct interface if_tmp
;
258 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
261 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
262 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
265 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
268 struct interface
*ifp
;
270 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
273 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
274 ifp
= if_lookup_by_name(name
, vrf
->vrf_id
);
282 /* Lookup interface by IPv4 address. */
283 struct interface
*if_lookup_exact_address(void *src
, int family
,
286 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
287 struct listnode
*cnode
;
288 struct interface
*ifp
;
292 FOR_ALL_INTERFACES (vrf
, ifp
) {
293 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
296 if (p
&& (p
->family
== family
)) {
297 if (family
== AF_INET
) {
300 (struct in_addr
*)src
))
302 } else if (family
== AF_INET6
) {
305 (struct in6_addr
*)src
))
314 /* Lookup interface by IPv4 address. */
315 struct connected
*if_lookup_address(void *matchaddr
, int family
,
318 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
321 struct listnode
*cnode
;
322 struct interface
*ifp
;
324 struct connected
*match
;
326 if (family
== AF_INET
) {
327 addr
.family
= AF_INET
;
328 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
329 addr
.prefixlen
= IPV4_MAX_BITLEN
;
330 } else if (family
== AF_INET6
) {
331 addr
.family
= AF_INET6
;
332 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
333 addr
.prefixlen
= IPV6_MAX_BITLEN
;
338 FOR_ALL_INTERFACES (vrf
, ifp
) {
339 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
340 if (c
->address
&& (c
->address
->family
== AF_INET
)
341 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
342 && (c
->address
->prefixlen
> bestlen
)) {
343 bestlen
= c
->address
->prefixlen
;
351 /* Lookup interface by prefix */
352 struct interface
*if_lookup_prefix(struct prefix
*prefix
, vrf_id_t vrf_id
)
354 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
355 struct listnode
*cnode
;
356 struct interface
*ifp
;
359 FOR_ALL_INTERFACES (vrf
, ifp
) {
360 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
361 if (prefix_cmp(c
->address
, prefix
) == 0) {
369 /* Get interface by name if given name interface doesn't exist create
371 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
, int vty
)
373 struct interface
*ifp
;
375 ifp
= if_lookup_by_name(name
, vrf_id
);
378 /* Not Found on same VRF. If the interface command
379 * was entered in vty without a VRF (passed as VRF_DEFAULT),
380 * accept the ifp we found. If a vrf was entered and there is
381 * a mismatch, reject it if from vty.
383 ifp
= if_lookup_by_name_all_vrf(name
);
385 return if_create(name
, vrf_id
);
387 if (vrf_id
== VRF_DEFAULT
)
391 /* if vrf backend uses NETNS, then
392 * this should not be considered as an update
393 * then create the new interface
395 if (ifp
->vrf_id
!= vrf_id
&& vrf_is_mapped_on_netns(vrf_id
))
396 return if_create(name
, vrf_id
);
397 /* If it came from the kernel
398 * or by way of zclient, believe it and update
399 * the ifp accordingly.
401 if_update_to_new_vrf(ifp
, vrf_id
);
405 void if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
409 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
412 if (ifp
->ifindex
== ifindex
)
415 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
416 IFINDEX_RB_REMOVE(vrf
, ifp
)
418 ifp
->ifindex
= ifindex
;
420 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
421 IFINDEX_RB_INSERT(vrf
, ifp
)
424 /* Does interface up ? */
425 int if_is_up(struct interface
*ifp
)
427 return ifp
->flags
& IFF_UP
;
430 /* Is interface running? */
431 int if_is_running(struct interface
*ifp
)
433 return ifp
->flags
& IFF_RUNNING
;
436 /* Is the interface operative, eg. either UP & RUNNING
437 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
438 if ptm checking is enabled, then ptm check has passed */
439 int if_is_operative(struct interface
*ifp
)
441 return ((ifp
->flags
& IFF_UP
)
442 && (((ifp
->flags
& IFF_RUNNING
)
443 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
444 || !CHECK_FLAG(ifp
->status
,
445 ZEBRA_INTERFACE_LINKDETECTION
)));
448 /* Is the interface operative, eg. either UP & RUNNING
449 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
450 int if_is_no_ptm_operative(struct interface
*ifp
)
452 return ((ifp
->flags
& IFF_UP
)
453 && ((ifp
->flags
& IFF_RUNNING
)
454 || !CHECK_FLAG(ifp
->status
,
455 ZEBRA_INTERFACE_LINKDETECTION
)));
458 /* Is this loopback interface ? */
459 int if_is_loopback(struct interface
*ifp
)
461 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
462 * but Y on platform N?
464 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
467 /* Check interface is VRF */
468 int if_is_vrf(struct interface
*ifp
)
470 return CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
473 /* Does this interface support broadcast ? */
474 int if_is_broadcast(struct interface
*ifp
)
476 return ifp
->flags
& IFF_BROADCAST
;
479 /* Does this interface support broadcast ? */
480 int if_is_pointopoint(struct interface
*ifp
)
482 return ifp
->flags
& IFF_POINTOPOINT
;
485 /* Does this interface support multicast ? */
486 int if_is_multicast(struct interface
*ifp
)
488 return ifp
->flags
& IFF_MULTICAST
;
491 /* Printout flag information into log */
492 const char *if_flag_dump(unsigned long flag
)
495 static char logbuf
[BUFSIZ
];
497 #define IFF_OUT_LOG(X, STR) \
500 strlcat(logbuf, ",", BUFSIZ); \
503 strlcat(logbuf, STR, BUFSIZ); \
506 strlcpy(logbuf
, "<", BUFSIZ
);
507 IFF_OUT_LOG(IFF_UP
, "UP");
508 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
509 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
510 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
511 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
512 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
513 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
514 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
515 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
516 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
517 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
518 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
519 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
520 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
521 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
522 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
523 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
524 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
525 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
526 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
527 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
529 strlcat(logbuf
, ">", BUFSIZ
);
536 static void if_dump(const struct interface
*ifp
)
538 struct listnode
*node
;
539 struct connected
*c
__attribute__((unused
));
541 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
543 "Interface %s vrf %u index %d metric %d mtu %d "
545 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
,
546 ifp
->mtu
, ifp
->mtu6
, if_flag_dump(ifp
->flags
));
549 /* Interface printing for all interface. */
550 void if_dump_all(void)
555 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
556 FOR_ALL_INTERFACES (vrf
, ifp
)
560 DEFUN (interface_desc
,
562 "description LINE...",
563 "Interface specific description\n"
564 "Characters describing this interface\n")
567 VTY_DECLVAR_CONTEXT(interface
, ifp
);
570 XFREE(MTYPE_TMP
, ifp
->desc
);
571 ifp
->desc
= argv_concat(argv
, argc
, idx_line
);
576 DEFUN (no_interface_desc
,
577 no_interface_desc_cmd
,
580 "Interface specific description\n")
582 VTY_DECLVAR_CONTEXT(interface
, ifp
);
585 XFREE(MTYPE_TMP
, ifp
->desc
);
592 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
593 * a seperate struct interface for each logical interface, so config
594 * file may be full of 'interface fooX:Y'. Solaris however does not
595 * expose logical interfaces via PF_ROUTE, so trying to track logical
596 * interfaces can be fruitless, for that reason Quagga only tracks
597 * the primary IP interface.
599 * We try accomodate SUNWzebra by:
600 * - looking up the interface name, to see whether it exists, if so
602 * - for protocol daemons, this could only because zebra told us of
604 * - for zebra, only because it learnt from kernel
606 * - search the name to see if it contains a sub-ipif / logical interface
607 * seperator, the ':' char. If it does:
608 * - text up to that char must be the primary name - get that name.
610 * - no idea, just get the name in its entirety.
612 static struct interface
*if_sunwzebra_get(char *name
, vrf_id_t vrf_id
)
614 struct interface
*ifp
;
617 if ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
620 /* hunt the primary interface name... */
621 cp
= strchr(name
, ':');
625 return if_get_by_name(name
, vrf_id
, 1);
631 "interface IFNAME [vrf NAME]",
632 "Select an interface to configure\n"
638 const char *ifname
= argv
[idx_ifname
]->arg
;
639 const char *vrfname
= (argc
> 2) ? argv
[idx_vrf
]->arg
: NULL
;
641 struct interface
*ifp
;
642 vrf_id_t vrf_id
= VRF_DEFAULT
;
644 if (strlen(ifname
) > INTERFACE_NAMSIZ
) {
646 "%% Interface name %s is invalid: length exceeds "
648 ifname
, INTERFACE_NAMSIZ
);
649 return CMD_WARNING_CONFIG_FAILED
;
652 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
653 Imagine forward reference of a vrf by name in this interface config */
655 VRF_GET_ID(vrf_id
, vrfname
);
658 ifp
= if_sunwzebra_get(ifname
, vrf_id
);
660 ifp
= if_get_by_name(ifname
, vrf_id
, 1);
664 vty_out(vty
, "%% interface %s not in %s\n", ifname
, vrfname
);
665 return CMD_WARNING_CONFIG_FAILED
;
667 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
672 DEFUN_NOSH (no_interface
,
674 "no interface IFNAME [vrf NAME]",
676 "Delete a pseudo interface's configuration\n"
681 const char *ifname
= argv
[2]->arg
;
682 const char *vrfname
= (argc
> 3) ? argv
[idx_vrf
]->arg
: NULL
;
684 // deleting interface
685 struct interface
*ifp
;
686 vrf_id_t vrf_id
= VRF_DEFAULT
;
689 VRF_GET_ID(vrf_id
, vrfname
);
691 ifp
= if_lookup_by_name(ifname
, vrf_id
);
694 vty_out(vty
, "%% Interface %s does not exist\n", ifname
);
695 return CMD_WARNING_CONFIG_FAILED
;
698 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
699 vty_out(vty
, "%% Only inactive interfaces can be deleted\n");
700 return CMD_WARNING_CONFIG_FAILED
;
708 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
710 struct interface
*ifp
;
711 struct vrf
*vrf
= NULL
;
713 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
714 FOR_ALL_INTERFACES (vrf
, ifp
) {
715 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
720 static const struct cmd_variable_handler if_var_handlers
[] = {
721 {/* "interface NAME" */
722 .varname
= "interface",
723 .completions
= if_autocomplete
},
724 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
725 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
726 {.completions
= NULL
}};
728 void if_cmd_init(void)
730 cmd_variable_handler_register(if_var_handlers
);
732 install_element(CONFIG_NODE
, &interface_cmd
);
733 install_element(CONFIG_NODE
, &no_interface_cmd
);
735 install_default(INTERFACE_NODE
);
736 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
737 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
741 /* For debug purpose. */
744 "show address [vrf NAME]",
750 struct listnode
*node
;
751 struct interface
*ifp
;
752 struct connected
*ifc
;
754 vrf_id_t vrf_id
= VRF_DEFAULT
;
757 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
759 FOR_ALL_INTERFACES (vrf
, ifp
)
761 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
765 if (p
->family
== AF_INET
)
766 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
772 DEFUN (show_address_vrf_all
,
773 show_address_vrf_all_cmd
,
774 "show address vrf all",
777 VRF_ALL_CMD_HELP_STR
)
780 struct listnode
*node
;
781 struct interface
*ifp
;
782 struct connected
*ifc
;
785 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
787 if (RB_EMPTY (if_name_head
, &vrf
->ifaces_by_name
))
790 vty_out (vty
, "\nVRF %u\n\n", vrf
->vrf_id
);
792 FOR_ALL_INTERFACES (vrf
, ifp
)
794 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
798 if (p
->family
== AF_INET
)
799 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
807 /* Allocate connected structure. */
808 struct connected
*connected_new(void)
810 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
813 /* Allocate nbr connected structure. */
814 struct nbr_connected
*nbr_connected_new(void)
816 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
819 /* Free connected structure. */
820 void connected_free(struct connected
*connected
)
822 if (connected
->address
)
823 prefix_free(connected
->address
);
825 if (connected
->destination
)
826 prefix_free(connected
->destination
);
828 if (connected
->label
)
829 XFREE(MTYPE_CONNECTED_LABEL
, connected
->label
);
831 XFREE(MTYPE_CONNECTED
, connected
);
834 /* Free nbr connected structure. */
835 void nbr_connected_free(struct nbr_connected
*connected
)
837 if (connected
->address
)
838 prefix_free(connected
->address
);
840 XFREE(MTYPE_NBR_CONNECTED
, connected
);
843 /* If same interface nbr address already exists... */
844 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
847 struct nbr_connected
*ifc
;
848 struct listnode
*node
;
850 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
851 if (prefix_same(ifc
->address
, p
))
857 /* Print if_addr structure. */
858 static void __attribute__((unused
))
859 connected_log(struct connected
*connected
, char *str
)
862 struct interface
*ifp
;
866 ifp
= connected
->ifp
;
867 p
= connected
->address
;
869 snprintf(logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ", str
,
870 ifp
->name
, ifp
->vrf_id
, prefix_family_str(p
),
871 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
873 p
= connected
->destination
;
875 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
876 BUFSIZ
- strlen(logbuf
));
878 zlog_info("%s", logbuf
);
881 /* Print if_addr structure. */
882 static void __attribute__((unused
))
883 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
886 struct interface
*ifp
;
890 ifp
= connected
->ifp
;
891 p
= connected
->address
;
893 snprintf(logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ", str
, ifp
->name
,
894 prefix_family_str(p
),
895 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
897 zlog_info("%s", logbuf
);
900 /* If two connected address has same prefix return 1. */
901 static int connected_same_prefix(struct prefix
*p1
, struct prefix
*p2
)
903 if (p1
->family
== p2
->family
) {
904 if (p1
->family
== AF_INET
905 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
907 if (p1
->family
== AF_INET6
908 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
914 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
917 struct listnode
*node
;
918 struct listnode
*next
;
919 struct connected
*ifc
;
921 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
922 ifc
= listgetdata(node
);
925 if (connected_same_prefix(ifc
->address
, p
))
931 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
934 struct listnode
*node
;
935 struct listnode
*next
;
936 struct connected
*ifc
;
938 /* In case of same prefix come, replace it with new one. */
939 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
940 ifc
= listgetdata(node
);
943 if (connected_same_prefix(ifc
->address
, p
)) {
944 listnode_delete(ifp
->connected
, ifc
);
951 /* Find the address on our side that will be used when packets
953 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
956 struct listnode
*cnode
;
958 struct connected
*match
;
962 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
963 if (c
->address
&& (c
->address
->family
== addr
->family
)
964 && prefix_match(CONNECTED_PREFIX(c
), addr
)
966 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
972 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
974 struct prefix
*destination
)
976 struct connected
*ifc
;
978 /* Allocate new connected address. */
979 ifc
= connected_new();
982 /* Fetch interface address */
983 ifc
->address
= prefix_new();
984 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
986 /* Fetch dest address */
988 ifc
->destination
= prefix_new();
989 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
992 /* Add connected address to the interface. */
993 listnode_add(ifp
->connected
, ifc
);
997 #if 0 /* this route_table of struct connected's is unused \
998 * however, it would be good to use a route_table rather than \
1001 /* Interface looking up by interface's address. */
1002 /* Interface's IPv4 address reverse lookup table. */
1003 struct route_table
*ifaddr_ipv4_table
;
1004 /* struct route_table *ifaddr_ipv6_table; */
1007 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
1009 struct route_node
*rn
;
1010 struct prefix_ipv4 p
;
1013 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1016 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1019 route_unlock_node (rn
);
1020 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1021 inet_ntoa (*ifaddr
));
1028 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1030 struct route_node
*rn
;
1031 struct prefix_ipv4 p
;
1034 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1037 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1040 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1041 inet_ntoa (*ifaddr
));
1045 route_unlock_node (rn
);
1046 route_unlock_node (rn
);
1049 /* Lookup interface by interface's IP address or interface index. */
1050 static struct interface
*
1051 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1053 struct prefix_ipv4 p
;
1054 struct route_node
*rn
;
1055 struct interface
*ifp
;
1060 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1063 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1068 route_unlock_node (rn
);
1072 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1074 #endif /* ifaddr_ipv4_table */
1076 void if_terminate(struct vrf
*vrf
)
1078 struct interface
*ifp
;
1080 while (!RB_EMPTY(if_name_head
, &vrf
->ifaces_by_name
)) {
1081 ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
);
1084 ifp
->node
->info
= NULL
;
1085 route_unlock_node(ifp
->node
);
1091 const char *if_link_type_str(enum zebra_link_type llt
)
1094 #define llts(T,S) case (T): return (S)
1095 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1096 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1097 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1098 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1099 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1100 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1101 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1102 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1103 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1104 llts(ZEBRA_LLT_ATM
, "ATM");
1105 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1106 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1107 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1108 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1109 llts(ZEBRA_LLT_SLIP
, "SLIP");
1110 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1111 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1112 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1113 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1114 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1115 llts(ZEBRA_LLT_PPP
, "PPP");
1116 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1117 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1118 llts(ZEBRA_LLT_LAPB
, "LAPB");
1119 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1120 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1121 llts(ZEBRA_LLT_FRAD
, "FRAD");
1122 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1123 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1124 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1125 llts(ZEBRA_LLT_FDDI
, "FDDI");
1126 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1127 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1128 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1129 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1130 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1131 llts(ZEBRA_LLT_IRDA
, "IrDA");
1132 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1133 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1134 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1135 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1136 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1137 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1138 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1139 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1140 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1142 zlog_warn("Unknown value %d", llt
);
1143 return "Unknown type!";
1149 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1153 if (ifp
->link_params
!= NULL
)
1154 return ifp
->link_params
;
1156 struct if_link_params
*iflp
=
1157 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1161 /* Set TE metric equal to standard metric */
1162 iflp
->te_metric
= ifp
->metric
;
1164 /* Compute default bandwidth based on interface */
1166 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1167 * TE_KILO_BIT
/ TE_BYTE
);
1169 /* Set Max, Reservable and Unreserved Bandwidth */
1170 iflp
->max_bw
= iflp
->default_bw
;
1171 iflp
->max_rsv_bw
= iflp
->default_bw
;
1172 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1173 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1175 /* Update Link parameters status */
1177 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1179 /* Finally attach newly created Link Parameters */
1180 ifp
->link_params
= iflp
;
1185 void if_link_params_free(struct interface
*ifp
)
1187 if (ifp
->link_params
== NULL
)
1189 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1190 ifp
->link_params
= NULL
;