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
);
213 XFREE(MTYPE_IF
, ifp
);
216 /* Interface existance check by index. */
217 struct interface
*if_lookup_by_index(ifindex_t ifindex
, vrf_id_t vrf_id
)
220 struct interface if_tmp
;
222 vrf
= vrf_lookup_by_id(vrf_id
);
226 if_tmp
.ifindex
= ifindex
;
227 return RB_FIND(if_index_head
, &vrf
->ifaces_by_index
, &if_tmp
);
230 const char *ifindex2ifname(ifindex_t ifindex
, vrf_id_t vrf_id
)
232 struct interface
*ifp
;
234 return ((ifp
= if_lookup_by_index(ifindex
, vrf_id
)) != NULL
)
239 ifindex_t
ifname2ifindex(const char *name
, vrf_id_t vrf_id
)
241 struct interface
*ifp
;
243 return ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
248 /* Interface existance check by interface name. */
249 struct interface
*if_lookup_by_name(const char *name
, vrf_id_t vrf_id
)
251 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
252 struct interface if_tmp
;
255 || strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
258 strlcpy(if_tmp
.name
, name
, sizeof(if_tmp
.name
));
259 return RB_FIND(if_name_head
, &vrf
->ifaces_by_name
, &if_tmp
);
262 struct interface
*if_lookup_by_name_all_vrf(const char *name
)
265 struct interface
*ifp
;
267 if (!name
|| strnlen(name
, INTERFACE_NAMSIZ
) == INTERFACE_NAMSIZ
)
270 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
271 ifp
= if_lookup_by_name(name
, vrf
->vrf_id
);
279 /* Lookup interface by IPv4 address. */
280 struct interface
*if_lookup_exact_address(void *src
, int family
,
283 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
284 struct listnode
*cnode
;
285 struct interface
*ifp
;
289 FOR_ALL_INTERFACES (vrf
, ifp
) {
290 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
293 if (p
&& (p
->family
== family
)) {
294 if (family
== AF_INET
) {
297 (struct in_addr
*)src
))
299 } else if (family
== AF_INET6
) {
302 (struct in6_addr
*)src
))
311 /* Lookup interface by IPv4 address. */
312 struct connected
*if_lookup_address(void *matchaddr
, int family
,
315 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
318 struct listnode
*cnode
;
319 struct interface
*ifp
;
321 struct connected
*match
;
323 if (family
== AF_INET
) {
324 addr
.family
= AF_INET
;
325 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
326 addr
.prefixlen
= IPV4_MAX_BITLEN
;
327 } else if (family
== AF_INET6
) {
328 addr
.family
= AF_INET6
;
329 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
330 addr
.prefixlen
= IPV6_MAX_BITLEN
;
335 FOR_ALL_INTERFACES (vrf
, ifp
) {
336 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
337 if (c
->address
&& (c
->address
->family
== AF_INET
)
338 && prefix_match(CONNECTED_PREFIX(c
), &addr
)
339 && (c
->address
->prefixlen
> bestlen
)) {
340 bestlen
= c
->address
->prefixlen
;
348 /* Lookup interface by prefix */
349 struct interface
*if_lookup_prefix(struct prefix
*prefix
, vrf_id_t vrf_id
)
351 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
352 struct listnode
*cnode
;
353 struct interface
*ifp
;
356 FOR_ALL_INTERFACES (vrf
, ifp
) {
357 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
358 if (prefix_cmp(c
->address
, prefix
) == 0) {
366 /* Get interface by name if given name interface doesn't exist create
368 struct interface
*if_get_by_name(const char *name
, vrf_id_t vrf_id
, int vty
)
370 struct interface
*ifp
;
372 ifp
= if_lookup_by_name_all_vrf(name
);
374 if (ifp
->vrf_id
== vrf_id
)
377 /* Found a match on a different VRF. If the interface command
378 * was entered in vty without a VRF (passed as VRF_DEFAULT),
379 * accept the ifp we found. If a vrf was entered and there is
380 * a mismatch, reject it if from vty. If it came from the kernel
381 * or by way of zclient, believe it and update the ifp
385 if (vrf_id
== VRF_DEFAULT
)
389 if_update_to_new_vrf(ifp
, vrf_id
);
394 return if_create(name
, vrf_id
);
397 void if_set_index(struct interface
*ifp
, ifindex_t ifindex
)
401 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
404 if (ifp
->ifindex
== ifindex
)
407 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
408 IFINDEX_RB_REMOVE(vrf
, ifp
)
410 ifp
->ifindex
= ifindex
;
412 if (ifp
->ifindex
!= IFINDEX_INTERNAL
)
413 IFINDEX_RB_INSERT(vrf
, ifp
)
416 /* Does interface up ? */
417 int if_is_up(struct interface
*ifp
)
419 return ifp
->flags
& IFF_UP
;
422 /* Is interface running? */
423 int if_is_running(struct interface
*ifp
)
425 return ifp
->flags
& IFF_RUNNING
;
428 /* Is the interface operative, eg. either UP & RUNNING
429 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
430 if ptm checking is enabled, then ptm check has passed */
431 int if_is_operative(struct interface
*ifp
)
433 return ((ifp
->flags
& IFF_UP
)
434 && (((ifp
->flags
& IFF_RUNNING
)
435 && (ifp
->ptm_status
|| !ifp
->ptm_enable
))
436 || !CHECK_FLAG(ifp
->status
,
437 ZEBRA_INTERFACE_LINKDETECTION
)));
440 /* Is the interface operative, eg. either UP & RUNNING
441 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
442 int if_is_no_ptm_operative(struct interface
*ifp
)
444 return ((ifp
->flags
& IFF_UP
)
445 && ((ifp
->flags
& IFF_RUNNING
)
446 || !CHECK_FLAG(ifp
->status
,
447 ZEBRA_INTERFACE_LINKDETECTION
)));
450 /* Is this loopback interface ? */
451 int if_is_loopback(struct interface
*ifp
)
453 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
454 * but Y on platform N?
456 return (ifp
->flags
& (IFF_LOOPBACK
| IFF_NOXMIT
| IFF_VIRTUAL
));
459 /* Does this interface support broadcast ? */
460 int if_is_broadcast(struct interface
*ifp
)
462 return ifp
->flags
& IFF_BROADCAST
;
465 /* Does this interface support broadcast ? */
466 int if_is_pointopoint(struct interface
*ifp
)
468 return ifp
->flags
& IFF_POINTOPOINT
;
471 /* Does this interface support multicast ? */
472 int if_is_multicast(struct interface
*ifp
)
474 return ifp
->flags
& IFF_MULTICAST
;
477 /* Printout flag information into log */
478 const char *if_flag_dump(unsigned long flag
)
481 static char logbuf
[BUFSIZ
];
483 #define IFF_OUT_LOG(X, STR) \
486 strlcat(logbuf, ",", BUFSIZ); \
489 strlcat(logbuf, STR, BUFSIZ); \
492 strlcpy(logbuf
, "<", BUFSIZ
);
493 IFF_OUT_LOG(IFF_UP
, "UP");
494 IFF_OUT_LOG(IFF_BROADCAST
, "BROADCAST");
495 IFF_OUT_LOG(IFF_DEBUG
, "DEBUG");
496 IFF_OUT_LOG(IFF_LOOPBACK
, "LOOPBACK");
497 IFF_OUT_LOG(IFF_POINTOPOINT
, "POINTOPOINT");
498 IFF_OUT_LOG(IFF_NOTRAILERS
, "NOTRAILERS");
499 IFF_OUT_LOG(IFF_RUNNING
, "RUNNING");
500 IFF_OUT_LOG(IFF_NOARP
, "NOARP");
501 IFF_OUT_LOG(IFF_PROMISC
, "PROMISC");
502 IFF_OUT_LOG(IFF_ALLMULTI
, "ALLMULTI");
503 IFF_OUT_LOG(IFF_OACTIVE
, "OACTIVE");
504 IFF_OUT_LOG(IFF_SIMPLEX
, "SIMPLEX");
505 IFF_OUT_LOG(IFF_LINK0
, "LINK0");
506 IFF_OUT_LOG(IFF_LINK1
, "LINK1");
507 IFF_OUT_LOG(IFF_LINK2
, "LINK2");
508 IFF_OUT_LOG(IFF_MULTICAST
, "MULTICAST");
509 IFF_OUT_LOG(IFF_NOXMIT
, "NOXMIT");
510 IFF_OUT_LOG(IFF_NORTEXCH
, "NORTEXCH");
511 IFF_OUT_LOG(IFF_VIRTUAL
, "VIRTUAL");
512 IFF_OUT_LOG(IFF_IPV4
, "IPv4");
513 IFF_OUT_LOG(IFF_IPV6
, "IPv6");
515 strlcat(logbuf
, ">", BUFSIZ
);
522 static void if_dump(const struct interface
*ifp
)
524 struct listnode
*node
;
525 struct connected
*c
__attribute__((unused
));
527 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, c
))
529 "Interface %s vrf %u index %d metric %d mtu %d "
531 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
,
532 ifp
->mtu
, ifp
->mtu6
, if_flag_dump(ifp
->flags
));
535 /* Interface printing for all interface. */
536 void if_dump_all(void)
541 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
542 FOR_ALL_INTERFACES (vrf
, ifp
)
546 DEFUN (interface_desc
,
548 "description LINE...",
549 "Interface specific description\n"
550 "Characters describing this interface\n")
553 VTY_DECLVAR_CONTEXT(interface
, ifp
);
556 XFREE(MTYPE_TMP
, ifp
->desc
);
557 ifp
->desc
= argv_concat(argv
, argc
, idx_line
);
562 DEFUN (no_interface_desc
,
563 no_interface_desc_cmd
,
566 "Interface specific description\n")
568 VTY_DECLVAR_CONTEXT(interface
, ifp
);
571 XFREE(MTYPE_TMP
, ifp
->desc
);
578 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
579 * a seperate struct interface for each logical interface, so config
580 * file may be full of 'interface fooX:Y'. Solaris however does not
581 * expose logical interfaces via PF_ROUTE, so trying to track logical
582 * interfaces can be fruitless, for that reason Quagga only tracks
583 * the primary IP interface.
585 * We try accomodate SUNWzebra by:
586 * - looking up the interface name, to see whether it exists, if so
588 * - for protocol daemons, this could only because zebra told us of
590 * - for zebra, only because it learnt from kernel
592 * - search the name to see if it contains a sub-ipif / logical interface
593 * seperator, the ':' char. If it does:
594 * - text up to that char must be the primary name - get that name.
596 * - no idea, just get the name in its entirety.
598 static struct interface
*if_sunwzebra_get(char *name
, vrf_id_t vrf_id
)
600 struct interface
*ifp
;
603 if ((ifp
= if_lookup_by_name(name
, vrf_id
)) != NULL
)
606 /* hunt the primary interface name... */
607 cp
= strchr(name
, ':');
611 return if_get_by_name(name
, vrf_id
, 1);
617 "interface IFNAME [vrf NAME]",
618 "Select an interface to configure\n"
624 const char *ifname
= argv
[idx_ifname
]->arg
;
625 const char *vrfname
= (argc
> 2) ? argv
[idx_vrf
]->arg
: NULL
;
627 struct interface
*ifp
;
628 vrf_id_t vrf_id
= VRF_DEFAULT
;
630 if (strlen(ifname
) > INTERFACE_NAMSIZ
) {
632 "%% Interface name %s is invalid: length exceeds "
634 ifname
, INTERFACE_NAMSIZ
);
635 return CMD_WARNING_CONFIG_FAILED
;
638 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
639 Imagine forward reference of a vrf by name in this interface config */
641 VRF_GET_ID(vrf_id
, vrfname
);
644 ifp
= if_sunwzebra_get(ifname
, vrf_id
);
646 ifp
= if_get_by_name(ifname
, vrf_id
, 1);
650 vty_out(vty
, "%% interface %s not in %s\n", ifname
, vrfname
);
651 return CMD_WARNING_CONFIG_FAILED
;
653 VTY_PUSH_CONTEXT(INTERFACE_NODE
, ifp
);
658 DEFUN_NOSH (no_interface
,
660 "no interface IFNAME [vrf NAME]",
662 "Delete a pseudo interface's configuration\n"
666 const char *ifname
= argv
[2]->arg
;
667 const char *vrfname
= (argc
> 3) ? argv
[3]->arg
: NULL
;
669 // deleting interface
670 struct interface
*ifp
;
671 vrf_id_t vrf_id
= VRF_DEFAULT
;
674 VRF_GET_ID(vrf_id
, vrfname
);
676 ifp
= if_lookup_by_name(ifname
, vrf_id
);
679 vty_out(vty
, "%% Interface %s does not exist\n", ifname
);
680 return CMD_WARNING_CONFIG_FAILED
;
683 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
684 vty_out(vty
, "%% Only inactive interfaces can be deleted\n");
685 return CMD_WARNING_CONFIG_FAILED
;
693 static void if_autocomplete(vector comps
, struct cmd_token
*token
)
695 struct interface
*ifp
;
696 struct vrf
*vrf
= NULL
;
698 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
699 FOR_ALL_INTERFACES (vrf
, ifp
) {
700 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, ifp
->name
));
705 static const struct cmd_variable_handler if_var_handlers
[] = {
706 {/* "interface NAME" */
707 .varname
= "interface",
708 .completions
= if_autocomplete
},
709 {.tokenname
= "IFNAME", .completions
= if_autocomplete
},
710 {.tokenname
= "INTERFACE", .completions
= if_autocomplete
},
711 {.completions
= NULL
}};
713 void if_cmd_init(void)
715 cmd_variable_handler_register(if_var_handlers
);
717 install_element(CONFIG_NODE
, &interface_cmd
);
718 install_element(CONFIG_NODE
, &no_interface_cmd
);
720 install_default(INTERFACE_NODE
);
721 install_element(INTERFACE_NODE
, &interface_desc_cmd
);
722 install_element(INTERFACE_NODE
, &no_interface_desc_cmd
);
726 /* For debug purpose. */
729 "show address [vrf NAME]",
735 struct listnode
*node
;
736 struct interface
*ifp
;
737 struct connected
*ifc
;
739 vrf_id_t vrf_id
= VRF_DEFAULT
;
742 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
744 FOR_ALL_INTERFACES (vrf
, ifp
)
746 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
750 if (p
->family
== AF_INET
)
751 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
757 DEFUN (show_address_vrf_all
,
758 show_address_vrf_all_cmd
,
759 "show address vrf all",
762 VRF_ALL_CMD_HELP_STR
)
765 struct listnode
*node
;
766 struct interface
*ifp
;
767 struct connected
*ifc
;
770 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
772 if (RB_EMPTY (if_name_head
, &vrf
->ifaces_by_name
))
775 vty_out (vty
, "\nVRF %u\n\n", vrf
->vrf_id
);
777 FOR_ALL_INTERFACES (vrf
, ifp
)
779 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
783 if (p
->family
== AF_INET
)
784 vty_out (vty
, "%s/%d\n", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
);
792 /* Allocate connected structure. */
793 struct connected
*connected_new(void)
795 return XCALLOC(MTYPE_CONNECTED
, sizeof(struct connected
));
798 /* Allocate nbr connected structure. */
799 struct nbr_connected
*nbr_connected_new(void)
801 return XCALLOC(MTYPE_NBR_CONNECTED
, sizeof(struct nbr_connected
));
804 /* Free connected structure. */
805 void connected_free(struct connected
*connected
)
807 if (connected
->address
)
808 prefix_free(connected
->address
);
810 if (connected
->destination
)
811 prefix_free(connected
->destination
);
813 if (connected
->label
)
814 XFREE(MTYPE_CONNECTED_LABEL
, connected
->label
);
816 XFREE(MTYPE_CONNECTED
, connected
);
819 /* Free nbr connected structure. */
820 void nbr_connected_free(struct nbr_connected
*connected
)
822 if (connected
->address
)
823 prefix_free(connected
->address
);
825 XFREE(MTYPE_NBR_CONNECTED
, connected
);
828 /* If same interface nbr address already exists... */
829 struct nbr_connected
*nbr_connected_check(struct interface
*ifp
,
832 struct nbr_connected
*ifc
;
833 struct listnode
*node
;
835 for (ALL_LIST_ELEMENTS_RO(ifp
->nbr_connected
, node
, ifc
))
836 if (prefix_same(ifc
->address
, p
))
842 /* Print if_addr structure. */
843 static void __attribute__((unused
))
844 connected_log(struct connected
*connected
, char *str
)
847 struct interface
*ifp
;
851 ifp
= connected
->ifp
;
852 p
= connected
->address
;
854 snprintf(logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ", str
,
855 ifp
->name
, ifp
->vrf_id
, prefix_family_str(p
),
856 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
858 p
= connected
->destination
;
860 strncat(logbuf
, inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
861 BUFSIZ
- strlen(logbuf
));
863 zlog_info("%s", logbuf
);
866 /* Print if_addr structure. */
867 static void __attribute__((unused
))
868 nbr_connected_log(struct nbr_connected
*connected
, char *str
)
871 struct interface
*ifp
;
875 ifp
= connected
->ifp
;
876 p
= connected
->address
;
878 snprintf(logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ", str
, ifp
->name
,
879 prefix_family_str(p
),
880 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
882 zlog_info("%s", logbuf
);
885 /* If two connected address has same prefix return 1. */
886 static int connected_same_prefix(struct prefix
*p1
, struct prefix
*p2
)
888 if (p1
->family
== p2
->family
) {
889 if (p1
->family
== AF_INET
890 && IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
892 if (p1
->family
== AF_INET6
893 && IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
899 struct connected
*connected_lookup_prefix_exact(struct interface
*ifp
,
902 struct listnode
*node
;
903 struct listnode
*next
;
904 struct connected
*ifc
;
906 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
907 ifc
= listgetdata(node
);
910 if (connected_same_prefix(ifc
->address
, p
))
916 struct connected
*connected_delete_by_prefix(struct interface
*ifp
,
919 struct listnode
*node
;
920 struct listnode
*next
;
921 struct connected
*ifc
;
923 /* In case of same prefix come, replace it with new one. */
924 for (node
= listhead(ifp
->connected
); node
; node
= next
) {
925 ifc
= listgetdata(node
);
928 if (connected_same_prefix(ifc
->address
, p
)) {
929 listnode_delete(ifp
->connected
, ifc
);
936 /* Find the address on our side that will be used when packets
938 struct connected
*connected_lookup_prefix(struct interface
*ifp
,
941 struct listnode
*cnode
;
943 struct connected
*match
;
947 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
948 if (c
->address
&& (c
->address
->family
== addr
->family
)
949 && prefix_match(CONNECTED_PREFIX(c
), addr
)
951 || (c
->address
->prefixlen
> match
->address
->prefixlen
)))
957 struct connected
*connected_add_by_prefix(struct interface
*ifp
,
959 struct prefix
*destination
)
961 struct connected
*ifc
;
963 /* Allocate new connected address. */
964 ifc
= connected_new();
967 /* Fetch interface address */
968 ifc
->address
= prefix_new();
969 memcpy(ifc
->address
, p
, sizeof(struct prefix
));
971 /* Fetch dest address */
973 ifc
->destination
= prefix_new();
974 memcpy(ifc
->destination
, destination
, sizeof(struct prefix
));
977 /* Add connected address to the interface. */
978 listnode_add(ifp
->connected
, ifc
);
982 #if 0 /* this route_table of struct connected's is unused \
983 * however, it would be good to use a route_table rather than \
986 /* Interface looking up by interface's address. */
987 /* Interface's IPv4 address reverse lookup table. */
988 struct route_table
*ifaddr_ipv4_table
;
989 /* struct route_table *ifaddr_ipv6_table; */
992 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
994 struct route_node
*rn
;
995 struct prefix_ipv4 p
;
998 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1001 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1004 route_unlock_node (rn
);
1005 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1006 inet_ntoa (*ifaddr
));
1013 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1015 struct route_node
*rn
;
1016 struct prefix_ipv4 p
;
1019 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1022 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1025 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1026 inet_ntoa (*ifaddr
));
1030 route_unlock_node (rn
);
1031 route_unlock_node (rn
);
1034 /* Lookup interface by interface's IP address or interface index. */
1035 static struct interface
*
1036 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1038 struct prefix_ipv4 p
;
1039 struct route_node
*rn
;
1040 struct interface
*ifp
;
1045 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1048 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1053 route_unlock_node (rn
);
1057 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1059 #endif /* ifaddr_ipv4_table */
1061 void if_terminate(struct vrf
*vrf
)
1063 struct interface
*ifp
;
1065 while ((ifp
= RB_ROOT(if_name_head
, &vrf
->ifaces_by_name
)) != NULL
) {
1067 ifp
->node
->info
= NULL
;
1068 route_unlock_node(ifp
->node
);
1074 const char *if_link_type_str(enum zebra_link_type llt
)
1077 #define llts(T,S) case (T): return (S)
1078 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1079 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1080 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1081 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1082 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1083 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1084 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1085 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1086 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1087 llts(ZEBRA_LLT_ATM
, "ATM");
1088 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1089 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1090 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1091 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1092 llts(ZEBRA_LLT_SLIP
, "SLIP");
1093 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1094 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1095 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1096 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1097 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1098 llts(ZEBRA_LLT_PPP
, "PPP");
1099 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1100 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1101 llts(ZEBRA_LLT_LAPB
, "LAPB");
1102 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1103 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1104 llts(ZEBRA_LLT_FRAD
, "FRAD");
1105 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1106 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1107 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1108 llts(ZEBRA_LLT_FDDI
, "FDDI");
1109 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1110 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1111 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1112 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1113 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1114 llts(ZEBRA_LLT_IRDA
, "IrDA");
1115 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1116 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1117 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1118 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1119 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1120 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1121 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1122 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1123 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1125 zlog_warn("Unknown value %d", llt
);
1126 return "Unknown type!";
1132 struct if_link_params
*if_link_params_get(struct interface
*ifp
)
1136 if (ifp
->link_params
!= NULL
)
1137 return ifp
->link_params
;
1139 struct if_link_params
*iflp
=
1140 XCALLOC(MTYPE_IF_LINK_PARAMS
, sizeof(struct if_link_params
));
1144 /* Set TE metric equal to standard metric */
1145 iflp
->te_metric
= ifp
->metric
;
1147 /* Compute default bandwidth based on interface */
1149 ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1150 * TE_KILO_BIT
/ TE_BYTE
);
1152 /* Set Max, Reservable and Unreserved Bandwidth */
1153 iflp
->max_bw
= iflp
->default_bw
;
1154 iflp
->max_rsv_bw
= iflp
->default_bw
;
1155 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1156 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1158 /* Update Link parameters status */
1160 LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1162 /* Finally attach newly created Link Parameters */
1163 ifp
->link_params
= iflp
;
1168 void if_link_params_free(struct interface
*ifp
)
1170 if (ifp
->link_params
== NULL
)
1172 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1173 ifp
->link_params
= NULL
;