4 * Copyright (C) 1997, 98 Kunihiro Ishiguro
6 * This file is part of GNU Zebra.
8 * GNU Zebra is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2, or (at your
11 * option) any later version.
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GNU Zebra; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
32 #include "sockunion.h"
39 DEFINE_MTYPE( LIB
, IF
, "Interface")
40 DEFINE_MTYPE_STATIC(LIB
, CONNECTED
, "Connected")
41 DEFINE_MTYPE_STATIC(LIB
, NBR_CONNECTED
, "Neighbor Connected")
42 DEFINE_MTYPE( LIB
, CONNECTED_LABEL
, "Connected interface label")
43 DEFINE_MTYPE_STATIC(LIB
, IF_LINK_PARAMS
, "Informational Link Parameters")
45 DEFINE_QOBJ_TYPE(interface
)
47 /* List of interfaces in only the default VRF */
50 /* One for each program. This structure is needed to store hooks. */
53 int (*if_new_hook
) (struct interface
*);
54 int (*if_delete_hook
) (struct interface
*);
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 <
66 if_cmp_name_func (char *p1
, char *p2
)
73 /* look up to any number */
74 l1
= strcspn(p1
, "0123456789");
75 l2
= strcspn(p2
, "0123456789");
77 /* name lengths are different -> compare names */
79 return (strcmp(p1
, p2
));
81 /* Note that this relies on all numbers being less than all letters, so
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") */
119 if_cmp_func (struct interface
*ifp1
, struct interface
*ifp2
)
121 return if_cmp_name_func (ifp1
->name
, ifp2
->name
);
124 /* Create new interface structure. */
126 if_create_vrf (const char *name
, int namelen
, vrf_id_t vrf_id
)
128 struct interface
*ifp
;
129 struct list
*intf_list
= vrf_iflist_get (vrf_id
);
131 ifp
= XCALLOC (MTYPE_IF
, sizeof (struct interface
));
132 ifp
->ifindex
= IFINDEX_INTERNAL
;
135 assert (namelen
<= INTERFACE_NAMSIZ
); /* Need space for '\0' at end. */
136 strncpy (ifp
->name
, name
, namelen
);
137 ifp
->name
[namelen
] = '\0';
138 ifp
->vrf_id
= vrf_id
;
139 if (if_lookup_by_name_vrf (ifp
->name
, vrf_id
) == NULL
)
140 listnode_add_sort (intf_list
, ifp
);
142 zlog_err("if_create(%s): corruption detected -- interface with this "
143 "name exists already in VRF %u!", ifp
->name
, vrf_id
);
144 ifp
->connected
= list_new ();
145 ifp
->connected
->del
= (void (*) (void *)) connected_free
;
147 ifp
->nbr_connected
= list_new ();
148 ifp
->nbr_connected
->del
= (void (*) (void *)) nbr_connected_free
;
150 /* Enable Link-detection by default */
151 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
153 QOBJ_REG (ifp
, interface
);
155 if (if_master
.if_new_hook
)
156 (*if_master
.if_new_hook
) (ifp
);
162 if_create (const char *name
, int namelen
)
164 return if_create_vrf (name
, namelen
, VRF_DEFAULT
);
167 /* Create new interface structure. */
169 if_update_vrf (struct interface
*ifp
, const char *name
, int namelen
, vrf_id_t vrf_id
)
171 struct list
*intf_list
= vrf_iflist_get (vrf_id
);
173 /* remove interface from old master vrf list */
174 if (vrf_iflist (ifp
->vrf_id
))
175 listnode_delete (vrf_iflist (ifp
->vrf_id
), ifp
);
178 assert (namelen
<= INTERFACE_NAMSIZ
); /* Need space for '\0' at end. */
179 strncpy (ifp
->name
, name
, namelen
);
180 ifp
->name
[namelen
] = '\0';
181 ifp
->vrf_id
= vrf_id
;
182 if (if_lookup_by_name_vrf (ifp
->name
, vrf_id
) == NULL
)
183 listnode_add_sort (intf_list
, ifp
);
185 zlog_err("if_create(%s): corruption detected -- interface with this "
186 "name exists already in VRF %u!", ifp
->name
, vrf_id
);
192 /* Delete interface structure. */
194 if_delete_retain (struct interface
*ifp
)
196 if (if_master
.if_delete_hook
)
197 (*if_master
.if_delete_hook
) (ifp
);
201 /* Free connected address list */
202 list_delete_all_node (ifp
->connected
);
204 /* Free connected nbr address list */
205 list_delete_all_node (ifp
->nbr_connected
);
208 /* Delete and free interface structure. */
210 if_delete (struct interface
*ifp
)
212 listnode_delete (vrf_iflist (ifp
->vrf_id
), ifp
);
214 if_delete_retain(ifp
);
216 list_free (ifp
->connected
);
217 list_free (ifp
->nbr_connected
);
219 if_link_params_free (ifp
);
221 XFREE (MTYPE_IF
, ifp
);
224 /* Add hook to interface master. */
226 if_add_hook (int type
, int (*func
)(struct interface
*ifp
))
230 if_master
.if_new_hook
= func
;
233 if_master
.if_delete_hook
= func
;
240 /* Interface existance check by index. */
242 if_lookup_by_index_vrf (ifindex_t ifindex
, vrf_id_t vrf_id
)
244 struct listnode
*node
;
245 struct interface
*ifp
;
247 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
249 if (ifp
->ifindex
== ifindex
)
256 if_lookup_by_index (ifindex_t ifindex
)
258 return if_lookup_by_index_vrf (ifindex
, VRF_DEFAULT
);
262 ifindex2ifname_vrf (ifindex_t ifindex
, vrf_id_t vrf_id
)
264 struct interface
*ifp
;
266 return ((ifp
= if_lookup_by_index_vrf (ifindex
, vrf_id
)) != NULL
) ?
267 ifp
->name
: "unknown";
271 ifindex2ifname (ifindex_t ifindex
)
273 return ifindex2ifname_vrf (ifindex
, VRF_DEFAULT
);
277 ifname2ifindex_vrf (const char *name
, vrf_id_t vrf_id
)
279 struct interface
*ifp
;
281 return ((ifp
= if_lookup_by_name_vrf (name
, vrf_id
)) != NULL
) ? ifp
->ifindex
286 ifname2ifindex (const char *name
)
288 return ifname2ifindex_vrf (name
, VRF_DEFAULT
);
291 /* Interface existance check by interface name. */
293 if_lookup_by_name_vrf (const char *name
, vrf_id_t vrf_id
)
295 struct listnode
*node
;
296 struct interface
*ifp
;
299 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
301 if (strcmp(name
, ifp
->name
) == 0)
308 if_lookup_by_name_all_vrf (const char *name
)
311 struct interface
*ifp
;
313 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
315 ifp
= if_lookup_by_name_vrf (name
, vrf
->vrf_id
);
324 if_lookup_by_name (const char *name
)
326 return if_lookup_by_name_vrf (name
, VRF_DEFAULT
);
330 if_lookup_by_name_len_vrf (const char *name
, size_t namelen
, vrf_id_t vrf_id
)
332 struct listnode
*node
;
333 struct interface
*ifp
;
335 if (namelen
> INTERFACE_NAMSIZ
)
338 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
340 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
347 if_lookup_by_name_len(const char *name
, size_t namelen
)
349 return if_lookup_by_name_len_vrf (name
, namelen
, VRF_DEFAULT
);
352 /* Lookup interface by IPv4 address. */
354 if_lookup_exact_address_vrf (void *src
, int family
, vrf_id_t vrf_id
)
356 struct listnode
*node
;
357 struct listnode
*cnode
;
358 struct interface
*ifp
;
362 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
364 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
368 if (p
&& (p
->family
== family
))
370 if (family
== AF_INET
)
372 if (IPV4_ADDR_SAME (&p
->u
.prefix4
, (struct in_addr
*)src
))
375 else if (family
== AF_INET6
)
377 if (IPV6_ADDR_SAME (&p
->u
.prefix4
, (struct in6_addr
*)src
))
387 if_lookup_exact_address (void *src
, int family
)
389 return if_lookup_exact_address_vrf (src
, family
, VRF_DEFAULT
);
392 /* Lookup interface by IPv4 address. */
394 if_lookup_address_vrf (void *matchaddr
, int family
, vrf_id_t vrf_id
)
396 struct listnode
*node
;
399 struct listnode
*cnode
;
400 struct interface
*ifp
;
402 struct connected
*match
;
404 if (family
== AF_INET
)
406 addr
.family
= AF_INET
;
407 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
408 addr
.prefixlen
= IPV4_MAX_BITLEN
;
410 else if (family
== AF_INET6
)
412 addr
.family
= AF_INET6
;
413 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
414 addr
.prefixlen
= IPV6_MAX_BITLEN
;
419 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
421 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
423 if (c
->address
&& (c
->address
->family
== AF_INET
) &&
424 prefix_match(CONNECTED_PREFIX(c
), &addr
) &&
425 (c
->address
->prefixlen
> bestlen
))
427 bestlen
= c
->address
->prefixlen
;
436 if_lookup_address (void *matchaddr
, int family
)
438 return if_lookup_address_vrf (matchaddr
, family
, VRF_DEFAULT
);
441 /* Lookup interface by prefix */
443 if_lookup_prefix_vrf (struct prefix
*prefix
, vrf_id_t vrf_id
)
445 struct listnode
*node
;
446 struct listnode
*cnode
;
447 struct interface
*ifp
;
450 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
452 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
454 if (prefix_cmp(c
->address
, prefix
) == 0)
464 if_lookup_prefix (struct prefix
*prefix
)
466 return if_lookup_prefix_vrf (prefix
, VRF_DEFAULT
);
469 /* Get interface by name if given name interface doesn't exist create
472 if_get_by_name_vrf (const char *name
, vrf_id_t vrf_id
)
474 struct interface
*ifp
;
476 return ((ifp
= if_lookup_by_name_vrf (name
, vrf_id
)) != NULL
) ? ifp
:
477 if_create_vrf (name
, strlen(name
), vrf_id
);
481 if_get_by_name (const char *name
)
483 return if_get_by_name_vrf (name
, VRF_DEFAULT
);
487 if_get_by_name_len_vrf (const char *name
, size_t namelen
, vrf_id_t vrf_id
, int vty
)
489 struct interface
*ifp
;
491 struct listnode
*node
;
493 ifp
= if_lookup_by_name_len_vrf (name
, namelen
, vrf_id
);
497 /* Didn't find the interface on that vrf. Defined on a different one? */
498 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
500 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf
->vrf_id
), node
, ifp
))
502 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
504 /* Found a match. If the interface command was entered in vty without a
505 * VRF (passed as VRF_DEFAULT), accept the ifp we found. If a vrf was
506 * entered and there is a mismatch, reject it if from vty. If it came
507 * from the kernel by way of zclient, believe it and update
508 * the ifp accordingly.
512 if (vrf_id
== VRF_DEFAULT
)
518 if_update_vrf (ifp
, name
, namelen
, vrf_id
);
524 return (if_create_vrf (name
, namelen
, vrf_id
));
528 if_get_by_name_len (const char *name
, size_t namelen
)
530 return if_get_by_name_len_vrf (name
, namelen
, VRF_DEFAULT
, 0);
533 /* Does interface up ? */
535 if_is_up (struct interface
*ifp
)
537 return ifp
->flags
& IFF_UP
;
540 /* Is interface running? */
542 if_is_running (struct interface
*ifp
)
544 return ifp
->flags
& IFF_RUNNING
;
547 /* Is the interface operative, eg. either UP & RUNNING
548 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
549 if ptm checking is enabled, then ptm check has passed */
551 if_is_operative (struct interface
*ifp
)
553 return ((ifp
->flags
& IFF_UP
) &&
554 (((ifp
->flags
& IFF_RUNNING
) &&
555 (ifp
->ptm_status
|| !ifp
->ptm_enable
)) ||
556 !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
559 /* Is the interface operative, eg. either UP & RUNNING
560 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
562 if_is_no_ptm_operative (struct interface
*ifp
)
564 return ((ifp
->flags
& IFF_UP
) &&
565 ((ifp
->flags
& IFF_RUNNING
) ||
566 !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
569 /* Is this loopback interface ? */
571 if_is_loopback (struct interface
*ifp
)
573 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
574 * but Y on platform N?
576 return (ifp
->flags
& (IFF_LOOPBACK
|IFF_NOXMIT
|IFF_VIRTUAL
));
579 /* Does this interface support broadcast ? */
581 if_is_broadcast (struct interface
*ifp
)
583 return ifp
->flags
& IFF_BROADCAST
;
586 /* Does this interface support broadcast ? */
588 if_is_pointopoint (struct interface
*ifp
)
590 return ifp
->flags
& IFF_POINTOPOINT
;
593 /* Does this interface support multicast ? */
595 if_is_multicast (struct interface
*ifp
)
597 return ifp
->flags
& IFF_MULTICAST
;
600 /* Printout flag information into log */
602 if_flag_dump (unsigned long flag
)
605 static char logbuf
[BUFSIZ
];
607 #define IFF_OUT_LOG(X,STR) \
611 strlcat (logbuf, ",", BUFSIZ); \
614 strlcat (logbuf, STR, BUFSIZ); \
617 strlcpy (logbuf
, "<", BUFSIZ
);
618 IFF_OUT_LOG (IFF_UP
, "UP");
619 IFF_OUT_LOG (IFF_BROADCAST
, "BROADCAST");
620 IFF_OUT_LOG (IFF_DEBUG
, "DEBUG");
621 IFF_OUT_LOG (IFF_LOOPBACK
, "LOOPBACK");
622 IFF_OUT_LOG (IFF_POINTOPOINT
, "POINTOPOINT");
623 IFF_OUT_LOG (IFF_NOTRAILERS
, "NOTRAILERS");
624 IFF_OUT_LOG (IFF_RUNNING
, "RUNNING");
625 IFF_OUT_LOG (IFF_NOARP
, "NOARP");
626 IFF_OUT_LOG (IFF_PROMISC
, "PROMISC");
627 IFF_OUT_LOG (IFF_ALLMULTI
, "ALLMULTI");
628 IFF_OUT_LOG (IFF_OACTIVE
, "OACTIVE");
629 IFF_OUT_LOG (IFF_SIMPLEX
, "SIMPLEX");
630 IFF_OUT_LOG (IFF_LINK0
, "LINK0");
631 IFF_OUT_LOG (IFF_LINK1
, "LINK1");
632 IFF_OUT_LOG (IFF_LINK2
, "LINK2");
633 IFF_OUT_LOG (IFF_MULTICAST
, "MULTICAST");
634 IFF_OUT_LOG (IFF_NOXMIT
, "NOXMIT");
635 IFF_OUT_LOG (IFF_NORTEXCH
, "NORTEXCH");
636 IFF_OUT_LOG (IFF_VIRTUAL
, "VIRTUAL");
637 IFF_OUT_LOG (IFF_IPV4
, "IPv4");
638 IFF_OUT_LOG (IFF_IPV6
, "IPv6");
640 strlcat (logbuf
, ">", BUFSIZ
);
648 if_dump (const struct interface
*ifp
)
650 struct listnode
*node
;
651 struct connected
*c
__attribute__((unused
));
653 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, c
))
654 zlog_info ("Interface %s vrf %u index %d metric %d mtu %d "
656 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
, ifp
->mtu
,
657 ifp
->mtu6
, if_flag_dump (ifp
->flags
));
660 /* Interface printing for all interface. */
665 struct listnode
*node
;
668 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
669 if (vrf
->iflist
!= NULL
)
670 for (ALL_LIST_ELEMENTS_RO (vrf
->iflist
, node
, p
))
674 DEFUN (interface_desc
,
676 "description LINE...",
677 "Interface specific description\n"
678 "Characters describing this interface\n")
681 VTY_DECLVAR_CONTEXT (interface
, ifp
);
684 XFREE (MTYPE_TMP
, ifp
->desc
);
685 ifp
->desc
= argv_concat(argv
, argc
, idx_line
);
690 DEFUN (no_interface_desc
,
691 no_interface_desc_cmd
,
694 "Interface specific description\n")
696 VTY_DECLVAR_CONTEXT (interface
, ifp
);
699 XFREE (MTYPE_TMP
, ifp
->desc
);
706 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
707 * a seperate struct interface for each logical interface, so config
708 * file may be full of 'interface fooX:Y'. Solaris however does not
709 * expose logical interfaces via PF_ROUTE, so trying to track logical
710 * interfaces can be fruitless, for that reason Quagga only tracks
711 * the primary IP interface.
713 * We try accomodate SUNWzebra by:
714 * - looking up the interface name, to see whether it exists, if so
716 * - for protocol daemons, this could only because zebra told us of
718 * - for zebra, only because it learnt from kernel
720 * - search the name to see if it contains a sub-ipif / logical interface
721 * seperator, the ':' char. If it does:
722 * - text up to that char must be the primary name - get that name.
724 * - no idea, just get the name in its entirety.
726 static struct interface
*
727 if_sunwzebra_get (const char *name
, size_t nlen
, vrf_id_t vrf_id
)
729 struct interface
*ifp
;
732 if ( (ifp
= if_lookup_by_name_len_vrf (name
, nlen
, vrf_id
)) != NULL
)
735 /* hunt the primary interface name... */
736 while (seppos
< nlen
&& name
[seppos
] != ':')
739 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
741 return if_get_by_name_len_vrf (name
, seppos
, vrf_id
, 1);
743 return if_get_by_name_len_vrf (name
, nlen
, vrf_id
, 1);
749 "interface IFNAME [vrf NAME]",
750 "Select an interface to configure\n"
756 const char *ifname
= argv
[idx_ifname
]->arg
;
757 const char *vrfname
= (argc
> 2) ? argv
[idx_vrf
]->arg
: NULL
;
759 struct interface
*ifp
;
761 vrf_id_t vrf_id
= VRF_DEFAULT
;
763 if ((sl
= strlen(ifname
)) > INTERFACE_NAMSIZ
)
765 vty_out (vty
, "%% Interface name %s is invalid: length exceeds "
767 ifname
, INTERFACE_NAMSIZ
, VTY_NEWLINE
);
771 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
772 Imagine forward reference of a vrf by name in this interface config */
774 VRF_GET_ID (vrf_id
, vrfname
);
777 ifp
= if_sunwzebra_get (ifname
, sl
, vrf_id
);
779 ifp
= if_get_by_name_len_vrf (ifname
, sl
, vrf_id
, 1);
784 vty_out (vty
, "%% interface %s not in %s%s", ifname
, vrfname
, VTY_NEWLINE
);
787 VTY_PUSH_CONTEXT (INTERFACE_NODE
, ifp
);
792 DEFUN_NOSH (no_interface
,
794 "no interface IFNAME [vrf NAME]",
796 "Delete a pseudo interface's configuration\n"
800 const char *ifname
= argv
[2]->arg
;
801 const char *vrfname
= (argc
> 3) ? argv
[3]->arg
: NULL
;
803 // deleting interface
804 struct interface
*ifp
;
805 vrf_id_t vrf_id
= VRF_DEFAULT
;
808 VRF_GET_ID (vrf_id
, vrfname
);
810 ifp
= if_lookup_by_name_vrf (ifname
, vrf_id
);
814 vty_out (vty
, "%% Interface %s does not exist%s", ifname
, VTY_NEWLINE
);
818 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
820 vty_out (vty
, "%% Only inactive interfaces can be deleted%s",
833 install_element (CONFIG_NODE
, &interface_cmd
);
834 install_element (CONFIG_NODE
, &no_interface_cmd
);
836 install_default (INTERFACE_NODE
);
837 install_element (INTERFACE_NODE
, &interface_desc_cmd
);
838 install_element (INTERFACE_NODE
, &no_interface_desc_cmd
);
842 /* For debug purpose. */
845 "show address [vrf NAME]",
851 struct listnode
*node
;
852 struct listnode
*node2
;
853 struct interface
*ifp
;
854 struct connected
*ifc
;
856 vrf_id_t vrf_id
= VRF_DEFAULT
;
859 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
861 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
863 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
867 if (p
->family
== AF_INET
)
868 vty_out (vty
, "%s/%d%s", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
,
875 DEFUN (show_address_vrf_all
,
876 show_address_vrf_all_cmd
,
877 "show address vrf all",
880 VRF_ALL_CMD_HELP_STR
)
883 struct listnode
*node
;
884 struct listnode
*node2
;
885 struct interface
*ifp
;
886 struct connected
*ifc
;
889 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
891 if (!vrf
->iflist
|| !listcount (vrf
->iflist
))
894 vty_out (vty
, "%sVRF %u%s%s", VTY_NEWLINE
, vrf
->vrf_id
, VTY_NEWLINE
,
897 for (ALL_LIST_ELEMENTS_RO (vrf
->iflist
, node
, ifp
))
899 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
903 if (p
->family
== AF_INET
)
904 vty_out (vty
, "%s/%d%s", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
,
913 /* Allocate connected structure. */
917 return XCALLOC (MTYPE_CONNECTED
, sizeof (struct connected
));
920 /* Allocate nbr connected structure. */
921 struct nbr_connected
*
922 nbr_connected_new (void)
924 return XCALLOC (MTYPE_NBR_CONNECTED
, sizeof (struct nbr_connected
));
927 /* Free connected structure. */
929 connected_free (struct connected
*connected
)
931 if (connected
->address
)
932 prefix_free (connected
->address
);
934 if (connected
->destination
)
935 prefix_free (connected
->destination
);
937 if (connected
->label
)
938 XFREE (MTYPE_CONNECTED_LABEL
, connected
->label
);
940 XFREE (MTYPE_CONNECTED
, connected
);
943 /* Free nbr connected structure. */
945 nbr_connected_free (struct nbr_connected
*connected
)
947 if (connected
->address
)
948 prefix_free (connected
->address
);
950 XFREE (MTYPE_NBR_CONNECTED
, connected
);
953 /* If same interface nbr address already exists... */
954 struct nbr_connected
*
955 nbr_connected_check (struct interface
*ifp
, struct prefix
*p
)
957 struct nbr_connected
*ifc
;
958 struct listnode
*node
;
960 for (ALL_LIST_ELEMENTS_RO (ifp
->nbr_connected
, node
, ifc
))
961 if (prefix_same (ifc
->address
, p
))
967 /* Print if_addr structure. */
968 static void __attribute__ ((unused
))
969 connected_log (struct connected
*connected
, char *str
)
972 struct interface
*ifp
;
976 ifp
= connected
->ifp
;
977 p
= connected
->address
;
979 snprintf (logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ",
980 str
, ifp
->name
, ifp
->vrf_id
, prefix_family_str (p
),
981 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
984 p
= connected
->destination
;
987 strncat (logbuf
, inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
988 BUFSIZ
- strlen(logbuf
));
990 zlog (NULL
, LOG_INFO
, "%s", logbuf
);
993 /* Print if_addr structure. */
994 static void __attribute__ ((unused
))
995 nbr_connected_log (struct nbr_connected
*connected
, char *str
)
998 struct interface
*ifp
;
1002 ifp
= connected
->ifp
;
1003 p
= connected
->address
;
1005 snprintf (logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ",
1006 str
, ifp
->name
, prefix_family_str (p
),
1007 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1010 zlog (NULL
, LOG_INFO
, "%s", logbuf
);
1013 /* If two connected address has same prefix return 1. */
1015 connected_same_prefix (struct prefix
*p1
, struct prefix
*p2
)
1017 if (p1
->family
== p2
->family
)
1019 if (p1
->family
== AF_INET
&&
1020 IPV4_ADDR_SAME (&p1
->u
.prefix4
, &p2
->u
.prefix4
))
1022 if (p1
->family
== AF_INET6
&&
1023 IPV6_ADDR_SAME (&p1
->u
.prefix6
, &p2
->u
.prefix6
))
1030 connected_lookup_prefix_exact (struct interface
*ifp
, struct prefix
*p
)
1032 struct listnode
*node
;
1033 struct listnode
*next
;
1034 struct connected
*ifc
;
1036 for (node
= listhead (ifp
->connected
); node
; node
= next
)
1038 ifc
= listgetdata (node
);
1041 if (connected_same_prefix (ifc
->address
, p
))
1048 connected_delete_by_prefix (struct interface
*ifp
, struct prefix
*p
)
1050 struct listnode
*node
;
1051 struct listnode
*next
;
1052 struct connected
*ifc
;
1054 /* In case of same prefix come, replace it with new one. */
1055 for (node
= listhead (ifp
->connected
); node
; node
= next
)
1057 ifc
= listgetdata (node
);
1060 if (connected_same_prefix (ifc
->address
, p
))
1062 listnode_delete (ifp
->connected
, ifc
);
1069 /* Find the address on our side that will be used when packets
1072 connected_lookup_prefix (struct interface
*ifp
, struct prefix
*addr
)
1074 struct listnode
*cnode
;
1075 struct connected
*c
;
1076 struct connected
*match
;
1080 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
1082 if (c
->address
&& (c
->address
->family
== addr
->family
) &&
1083 prefix_match(CONNECTED_PREFIX(c
), addr
) &&
1084 (!match
|| (c
->address
->prefixlen
> match
->address
->prefixlen
)))
1091 connected_add_by_prefix (struct interface
*ifp
, struct prefix
*p
,
1092 struct prefix
*destination
)
1094 struct connected
*ifc
;
1096 /* Allocate new connected address. */
1097 ifc
= connected_new ();
1100 /* Fetch interface address */
1101 ifc
->address
= prefix_new();
1102 memcpy (ifc
->address
, p
, sizeof(struct prefix
));
1104 /* Fetch dest address */
1107 ifc
->destination
= prefix_new();
1108 memcpy (ifc
->destination
, destination
, sizeof(struct prefix
));
1111 /* Add connected address to the interface. */
1112 listnode_add (ifp
->connected
, ifc
);
1116 #if 0 /* this route_table of struct connected's is unused
1117 * however, it would be good to use a route_table rather than
1120 /* Interface looking up by interface's address. */
1121 /* Interface's IPv4 address reverse lookup table. */
1122 struct route_table
*ifaddr_ipv4_table
;
1123 /* struct route_table *ifaddr_ipv6_table; */
1126 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
1128 struct route_node
*rn
;
1129 struct prefix_ipv4 p
;
1132 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1135 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1138 route_unlock_node (rn
);
1139 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1140 inet_ntoa (*ifaddr
));
1147 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1149 struct route_node
*rn
;
1150 struct prefix_ipv4 p
;
1153 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1156 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1159 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1160 inet_ntoa (*ifaddr
));
1164 route_unlock_node (rn
);
1165 route_unlock_node (rn
);
1168 /* Lookup interface by interface's IP address or interface index. */
1169 static struct interface
*
1170 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1172 struct prefix_ipv4 p
;
1173 struct route_node
*rn
;
1174 struct interface
*ifp
;
1179 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1182 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1187 route_unlock_node (rn
);
1191 return if_lookup_by_index(ifindex
);
1193 #endif /* ifaddr_ipv4_table */
1195 /* Initialize interface list. */
1197 if_init (struct list
**intf_list
)
1199 *intf_list
= list_new ();
1201 ifaddr_ipv4_table
= route_table_init ();
1202 #endif /* ifaddr_ipv4_table */
1204 (*intf_list
)->cmp
= (int (*)(void *, void *))if_cmp_func
;
1208 if_terminate (struct list
**intf_list
)
1212 struct interface
*ifp
;
1214 ifp
= listnode_head (*intf_list
);
1220 ifp
->node
->info
= NULL
;
1221 route_unlock_node (ifp
->node
);
1227 list_delete (*intf_list
);
1232 if_link_type_str (enum zebra_link_type llt
)
1236 #define llts(T,S) case (T): return (S)
1237 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1238 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1239 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1240 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1241 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1242 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1243 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1244 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1245 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1246 llts(ZEBRA_LLT_ATM
, "ATM");
1247 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1248 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1249 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1250 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1251 llts(ZEBRA_LLT_SLIP
, "SLIP");
1252 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1253 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1254 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1255 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1256 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1257 llts(ZEBRA_LLT_PPP
, "PPP");
1258 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1259 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1260 llts(ZEBRA_LLT_LAPB
, "LAPB");
1261 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1262 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1263 llts(ZEBRA_LLT_FRAD
, "FRAD");
1264 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1265 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1266 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1267 llts(ZEBRA_LLT_FDDI
, "FDDI");
1268 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1269 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1270 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1271 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1272 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1273 llts(ZEBRA_LLT_IRDA
, "IrDA");
1274 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1275 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1276 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1277 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1278 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1279 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1280 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1281 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1282 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1284 zlog_warn ("Unknown value %d", llt
);
1285 return "Unknown type!";
1291 struct if_link_params
*
1292 if_link_params_get (struct interface
*ifp
)
1296 if (ifp
->link_params
!= NULL
)
1297 return ifp
->link_params
;
1299 struct if_link_params
*iflp
= XCALLOC(MTYPE_IF_LINK_PARAMS
,
1300 sizeof (struct if_link_params
));
1301 if (iflp
== NULL
) return NULL
;
1303 /* Set TE metric equal to standard metric */
1304 iflp
->te_metric
= ifp
->metric
;
1306 /* Compute default bandwidth based on interface */
1307 iflp
->default_bw
= ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1308 * TE_KILO_BIT
/ TE_BYTE
);
1310 /* Set Max, Reservable and Unreserved Bandwidth */
1311 iflp
->max_bw
= iflp
->default_bw
;
1312 iflp
->max_rsv_bw
= iflp
->default_bw
;
1313 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1314 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1316 /* Update Link parameters status */
1317 iflp
->lp_status
= LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1319 /* Finally attach newly created Link Parameters */
1320 ifp
->link_params
= iflp
;
1326 if_link_params_free (struct interface
*ifp
)
1328 if (ifp
->link_params
== NULL
) return;
1329 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1330 ifp
->link_params
= NULL
;