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
)
310 struct interface
*ifp
;
311 struct vrf
*vrf
= NULL
;
314 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
316 vrf
= vrf_iter2vrf (iter
);
317 ifp
= if_lookup_by_name_vrf (name
, vrf
->vrf_id
);
326 if_lookup_by_name (const char *name
)
328 return if_lookup_by_name_vrf (name
, VRF_DEFAULT
);
332 if_lookup_by_name_len_vrf (const char *name
, size_t namelen
, vrf_id_t vrf_id
)
334 struct listnode
*node
;
335 struct interface
*ifp
;
337 if (namelen
> INTERFACE_NAMSIZ
)
340 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
342 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
349 if_lookup_by_name_len(const char *name
, size_t namelen
)
351 return if_lookup_by_name_len_vrf (name
, namelen
, VRF_DEFAULT
);
354 /* Lookup interface by IPv4 address. */
356 if_lookup_exact_address_vrf (void *src
, int family
, vrf_id_t vrf_id
)
358 struct listnode
*node
;
359 struct listnode
*cnode
;
360 struct interface
*ifp
;
364 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
366 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
370 if (p
&& (p
->family
== family
))
372 if (family
== AF_INET
)
374 if (IPV4_ADDR_SAME (&p
->u
.prefix4
, (struct in_addr
*)src
))
377 else if (family
== AF_INET6
)
379 if (IPV6_ADDR_SAME (&p
->u
.prefix4
, (struct in6_addr
*)src
))
389 if_lookup_exact_address (void *src
, int family
)
391 return if_lookup_exact_address_vrf (src
, family
, VRF_DEFAULT
);
394 /* Lookup interface by IPv4 address. */
396 if_lookup_address_vrf (void *matchaddr
, int family
, vrf_id_t vrf_id
)
398 struct listnode
*node
;
401 struct listnode
*cnode
;
402 struct interface
*ifp
;
404 struct interface
*match
;
406 if (family
== AF_INET
)
408 addr
.family
= AF_INET
;
409 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
410 addr
.prefixlen
= IPV4_MAX_BITLEN
;
412 else if (family
== AF_INET6
)
414 addr
.family
= AF_INET6
;
415 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
416 addr
.prefixlen
= IPV6_MAX_BITLEN
;
421 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
423 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
425 if (c
->address
&& (c
->address
->family
== AF_INET
) &&
426 prefix_match(CONNECTED_PREFIX(c
), &addr
) &&
427 (c
->address
->prefixlen
> bestlen
))
429 bestlen
= c
->address
->prefixlen
;
438 if_lookup_address (void *matchaddr
, int family
)
440 return if_lookup_address_vrf (matchaddr
, family
, VRF_DEFAULT
);
443 /* Lookup interface by prefix */
445 if_lookup_prefix_vrf (struct prefix
*prefix
, vrf_id_t vrf_id
)
447 struct listnode
*node
;
448 struct listnode
*cnode
;
449 struct interface
*ifp
;
452 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
454 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
456 if (prefix_cmp(c
->address
, prefix
) == 0)
466 if_lookup_prefix (struct prefix
*prefix
)
468 return if_lookup_prefix_vrf (prefix
, VRF_DEFAULT
);
471 /* Get interface by name if given name interface doesn't exist create
474 if_get_by_name_vrf (const char *name
, vrf_id_t vrf_id
)
476 struct interface
*ifp
;
478 return ((ifp
= if_lookup_by_name_vrf (name
, vrf_id
)) != NULL
) ? ifp
:
479 if_create_vrf (name
, strlen(name
), vrf_id
);
483 if_get_by_name (const char *name
)
485 return if_get_by_name_vrf (name
, VRF_DEFAULT
);
489 if_get_by_name_len_vrf (const char *name
, size_t namelen
, vrf_id_t vrf_id
, int vty
)
491 struct interface
*ifp
;
492 struct listnode
*node
;
493 struct vrf
*vrf
= NULL
;
496 ifp
= if_lookup_by_name_len_vrf (name
, namelen
, vrf_id
);
500 /* Didn't find the interface on that vrf. Defined on a different one? */
501 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
503 vrf
= vrf_iter2vrf(iter
);
504 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf
->vrf_id
), node
, ifp
))
506 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
508 /* Found a match. If the interface command was entered in vty without a
509 * VRF (passed as VRF_DEFAULT), accept the ifp we found. If a vrf was
510 * entered and there is a mismatch, reject it if from vty. If it came
511 * from the kernel by way of zclient, believe it and update
512 * the ifp accordingly.
516 if (vrf_id
== VRF_DEFAULT
)
522 if_update_vrf (ifp
, name
, namelen
, vrf_id
);
528 return (if_create_vrf (name
, namelen
, vrf_id
));
532 if_get_by_name_len (const char *name
, size_t namelen
)
534 return if_get_by_name_len_vrf (name
, namelen
, VRF_DEFAULT
, 0);
537 /* Does interface up ? */
539 if_is_up (struct interface
*ifp
)
541 return ifp
->flags
& IFF_UP
;
544 /* Is interface running? */
546 if_is_running (struct interface
*ifp
)
548 return ifp
->flags
& IFF_RUNNING
;
551 /* Is the interface operative, eg. either UP & RUNNING
552 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
553 if ptm checking is enabled, then ptm check has passed */
555 if_is_operative (struct interface
*ifp
)
557 return ((ifp
->flags
& IFF_UP
) &&
558 (((ifp
->flags
& IFF_RUNNING
) &&
559 (ifp
->ptm_status
|| !ifp
->ptm_enable
)) ||
560 !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
563 /* Is the interface operative, eg. either UP & RUNNING
564 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
566 if_is_no_ptm_operative (struct interface
*ifp
)
568 return ((ifp
->flags
& IFF_UP
) &&
569 ((ifp
->flags
& IFF_RUNNING
) ||
570 !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
573 /* Is this loopback interface ? */
575 if_is_loopback (struct interface
*ifp
)
577 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
578 * but Y on platform N?
580 return (ifp
->flags
& (IFF_LOOPBACK
|IFF_NOXMIT
|IFF_VIRTUAL
));
583 /* Does this interface support broadcast ? */
585 if_is_broadcast (struct interface
*ifp
)
587 return ifp
->flags
& IFF_BROADCAST
;
590 /* Does this interface support broadcast ? */
592 if_is_pointopoint (struct interface
*ifp
)
594 return ifp
->flags
& IFF_POINTOPOINT
;
597 /* Does this interface support multicast ? */
599 if_is_multicast (struct interface
*ifp
)
601 return ifp
->flags
& IFF_MULTICAST
;
604 /* Printout flag information into log */
606 if_flag_dump (unsigned long flag
)
609 static char logbuf
[BUFSIZ
];
611 #define IFF_OUT_LOG(X,STR) \
615 strlcat (logbuf, ",", BUFSIZ); \
618 strlcat (logbuf, STR, BUFSIZ); \
621 strlcpy (logbuf
, "<", BUFSIZ
);
622 IFF_OUT_LOG (IFF_UP
, "UP");
623 IFF_OUT_LOG (IFF_BROADCAST
, "BROADCAST");
624 IFF_OUT_LOG (IFF_DEBUG
, "DEBUG");
625 IFF_OUT_LOG (IFF_LOOPBACK
, "LOOPBACK");
626 IFF_OUT_LOG (IFF_POINTOPOINT
, "POINTOPOINT");
627 IFF_OUT_LOG (IFF_NOTRAILERS
, "NOTRAILERS");
628 IFF_OUT_LOG (IFF_RUNNING
, "RUNNING");
629 IFF_OUT_LOG (IFF_NOARP
, "NOARP");
630 IFF_OUT_LOG (IFF_PROMISC
, "PROMISC");
631 IFF_OUT_LOG (IFF_ALLMULTI
, "ALLMULTI");
632 IFF_OUT_LOG (IFF_OACTIVE
, "OACTIVE");
633 IFF_OUT_LOG (IFF_SIMPLEX
, "SIMPLEX");
634 IFF_OUT_LOG (IFF_LINK0
, "LINK0");
635 IFF_OUT_LOG (IFF_LINK1
, "LINK1");
636 IFF_OUT_LOG (IFF_LINK2
, "LINK2");
637 IFF_OUT_LOG (IFF_MULTICAST
, "MULTICAST");
638 IFF_OUT_LOG (IFF_NOXMIT
, "NOXMIT");
639 IFF_OUT_LOG (IFF_NORTEXCH
, "NORTEXCH");
640 IFF_OUT_LOG (IFF_VIRTUAL
, "VIRTUAL");
641 IFF_OUT_LOG (IFF_IPV4
, "IPv4");
642 IFF_OUT_LOG (IFF_IPV6
, "IPv6");
644 strlcat (logbuf
, ">", BUFSIZ
);
652 if_dump (const struct interface
*ifp
)
654 struct listnode
*node
;
655 struct connected
*c
__attribute__((unused
));
657 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, c
))
658 zlog_info ("Interface %s vrf %u index %d metric %d mtu %d "
660 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
, ifp
->mtu
,
661 ifp
->mtu6
, if_flag_dump (ifp
->flags
));
664 /* Interface printing for all interface. */
668 struct list
*intf_list
;
669 struct listnode
*node
;
673 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
674 if ((intf_list
= vrf_iter2iflist (iter
)) != NULL
)
675 for (ALL_LIST_ELEMENTS_RO (intf_list
, node
, p
))
679 DEFUN (interface_desc
,
682 "Interface specific description\n"
683 "Characters describing this interface\n")
685 VTY_DECLVAR_CONTEXT (interface
, ifp
);
691 XFREE (MTYPE_TMP
, ifp
->desc
);
692 ifp
->desc
= argv_concat(argv
, argc
, 0);
697 DEFUN (no_interface_desc
,
698 no_interface_desc_cmd
,
701 "Interface specific description\n")
703 VTY_DECLVAR_CONTEXT (interface
, ifp
);
706 XFREE (MTYPE_TMP
, ifp
->desc
);
713 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
714 * a seperate struct interface for each logical interface, so config
715 * file may be full of 'interface fooX:Y'. Solaris however does not
716 * expose logical interfaces via PF_ROUTE, so trying to track logical
717 * interfaces can be fruitless, for that reason Quagga only tracks
718 * the primary IP interface.
720 * We try accomodate SUNWzebra by:
721 * - looking up the interface name, to see whether it exists, if so
723 * - for protocol daemons, this could only because zebra told us of
725 * - for zebra, only because it learnt from kernel
727 * - search the name to see if it contains a sub-ipif / logical interface
728 * seperator, the ':' char. If it does:
729 * - text up to that char must be the primary name - get that name.
731 * - no idea, just get the name in its entirety.
733 static struct interface
*
734 if_sunwzebra_get (const char *name
, size_t nlen
, vrf_id_t vrf_id
)
736 struct interface
*ifp
;
739 if ( (ifp
= if_lookup_by_name_len_vrf (name
, nlen
, vrf_id
)) != NULL
)
742 /* hunt the primary interface name... */
743 while (seppos
< nlen
&& name
[seppos
] != ':')
746 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
748 return if_get_by_name_len_vrf (name
, seppos
, vrf_id
, 1);
750 return if_get_by_name_len_vrf (name
, nlen
, vrf_id
, 1);
757 "Select an interface to configure\n"
758 "Interface's name\n")
760 struct interface
*ifp
;
762 vrf_id_t vrf_id
= VRF_DEFAULT
;
764 if ((sl
= strlen(argv
[0])) > INTERFACE_NAMSIZ
)
766 vty_out (vty
, "%% Interface name %s is invalid: length exceeds "
768 argv
[0], INTERFACE_NAMSIZ
, VTY_NEWLINE
);
772 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
773 Imagine forward reference of a vrf by name in this interface config */
775 VRF_GET_ID (vrf_id
, argv
[1]);
778 ifp
= if_sunwzebra_get (argv
[0], sl
, vrf_id
);
780 ifp
= if_get_by_name_len_vrf (argv
[0], sl
, vrf_id
, 1);
785 vty_out (vty
, "%% interface %s not in %s%s", argv
[0], argv
[1], VTY_NEWLINE
);
788 VTY_PUSH_CONTEXT_COMPAT (INTERFACE_NODE
, ifp
);
795 "interface IFNAME " VRF_CMD_STR
,
796 "Select an interface to configure\n"
800 DEFUN_NOSH (no_interface
,
802 "no interface IFNAME",
804 "Delete a pseudo interface's configuration\n"
805 "Interface's name\n")
807 // deleting interface
808 struct interface
*ifp
;
809 vrf_id_t vrf_id
= VRF_DEFAULT
;
812 VRF_GET_ID (vrf_id
, argv
[1]);
814 ifp
= if_lookup_by_name_vrf (argv
[0], vrf_id
);
818 vty_out (vty
, "%% Interface %s does not exist%s", argv
[0], VTY_NEWLINE
);
822 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
824 vty_out (vty
, "%% Only inactive interfaces can be deleted%s",
835 no_interface_vrf_cmd
,
836 "no interface IFNAME " VRF_CMD_STR
,
838 "Delete a pseudo interface's configuration\n"
845 "Select a VRF to configure\n"
851 if ((sl
= strlen(argv
[0])) > VRF_NAMSIZ
)
853 vty_out (vty
, "%% VRF name %s is invalid: length exceeds "
855 argv
[0], VRF_NAMSIZ
, VTY_NEWLINE
);
859 vrfp
= vrf_get (VRF_UNKNOWN
, argv
[0]);
861 VTY_PUSH_CONTEXT_COMPAT (VRF_NODE
, vrfp
);
870 "Delete a pseudo VRF's configuration\n"
875 vrfp
= vrf_list_lookup_by_name (argv
[0]);
879 vty_out (vty
, "%% VRF %s does not exist%s", argv
[0], VTY_NEWLINE
);
883 if (CHECK_FLAG (vrfp
->status
, VRF_ACTIVE
))
885 vty_out (vty
, "%% Only inactive VRFs can be deleted%s",
896 /* For debug purpose. */
903 struct listnode
*node
;
904 struct listnode
*node2
;
905 struct interface
*ifp
;
906 struct connected
*ifc
;
908 vrf_id_t vrf_id
= VRF_DEFAULT
;
911 VRF_GET_ID (vrf_id
, argv
[0]);
913 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
915 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
919 if (p
->family
== AF_INET
)
920 vty_out (vty
, "%s/%d%s", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
,
928 show_address_vrf_cmd
,
929 "show address " VRF_CMD_STR
,
934 DEFUN (show_address_vrf_all
,
935 show_address_vrf_all_cmd
,
936 "show address " VRF_ALL_CMD_STR
,
939 VRF_ALL_CMD_HELP_STR
)
941 struct list
*intf_list
;
942 struct listnode
*node
;
943 struct listnode
*node2
;
944 struct interface
*ifp
;
945 struct connected
*ifc
;
949 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
951 intf_list
= vrf_iter2iflist (iter
);
952 if (!intf_list
|| !listcount (intf_list
))
955 vty_out (vty
, "%sVRF %u%s%s", VTY_NEWLINE
, vrf_iter2id (iter
),
956 VTY_NEWLINE
, VTY_NEWLINE
);
958 for (ALL_LIST_ELEMENTS_RO (intf_list
, node
, ifp
))
960 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
964 if (p
->family
== AF_INET
)
965 vty_out (vty
, "%s/%d%s", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
,
973 /* Allocate connected structure. */
977 return XCALLOC (MTYPE_CONNECTED
, sizeof (struct connected
));
980 /* Allocate nbr connected structure. */
981 struct nbr_connected
*
982 nbr_connected_new (void)
984 return XCALLOC (MTYPE_NBR_CONNECTED
, sizeof (struct nbr_connected
));
987 /* Free connected structure. */
989 connected_free (struct connected
*connected
)
991 if (connected
->address
)
992 prefix_free (connected
->address
);
994 if (connected
->destination
)
995 prefix_free (connected
->destination
);
997 if (connected
->label
)
998 XFREE (MTYPE_CONNECTED_LABEL
, connected
->label
);
1000 XFREE (MTYPE_CONNECTED
, connected
);
1003 /* Free nbr connected structure. */
1005 nbr_connected_free (struct nbr_connected
*connected
)
1007 if (connected
->address
)
1008 prefix_free (connected
->address
);
1010 XFREE (MTYPE_NBR_CONNECTED
, connected
);
1013 /* If same interface nbr address already exists... */
1014 struct nbr_connected
*
1015 nbr_connected_check (struct interface
*ifp
, struct prefix
*p
)
1017 struct nbr_connected
*ifc
;
1018 struct listnode
*node
;
1020 for (ALL_LIST_ELEMENTS_RO (ifp
->nbr_connected
, node
, ifc
))
1021 if (prefix_same (ifc
->address
, p
))
1027 /* Print if_addr structure. */
1028 static void __attribute__ ((unused
))
1029 connected_log (struct connected
*connected
, char *str
)
1032 struct interface
*ifp
;
1033 char logbuf
[BUFSIZ
];
1036 ifp
= connected
->ifp
;
1037 p
= connected
->address
;
1039 snprintf (logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ",
1040 str
, ifp
->name
, ifp
->vrf_id
, prefix_family_str (p
),
1041 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1044 p
= connected
->destination
;
1047 strncat (logbuf
, inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1048 BUFSIZ
- strlen(logbuf
));
1050 zlog (NULL
, LOG_INFO
, "%s", logbuf
);
1053 /* Print if_addr structure. */
1054 static void __attribute__ ((unused
))
1055 nbr_connected_log (struct nbr_connected
*connected
, char *str
)
1058 struct interface
*ifp
;
1059 char logbuf
[BUFSIZ
];
1062 ifp
= connected
->ifp
;
1063 p
= connected
->address
;
1065 snprintf (logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ",
1066 str
, ifp
->name
, prefix_family_str (p
),
1067 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1070 zlog (NULL
, LOG_INFO
, "%s", logbuf
);
1073 /* If two connected address has same prefix return 1. */
1075 connected_same_prefix (struct prefix
*p1
, struct prefix
*p2
)
1077 if (p1
->family
== p2
->family
)
1079 if (p1
->family
== AF_INET
&&
1080 IPV4_ADDR_SAME (&p1
->u
.prefix4
, &p2
->u
.prefix4
))
1083 if (p1
->family
== AF_INET6
&&
1084 IPV6_ADDR_SAME (&p1
->u
.prefix6
, &p2
->u
.prefix6
))
1086 #endif /* HAVE_IPV6 */
1092 connected_lookup_prefix_exact (struct interface
*ifp
, struct prefix
*p
)
1094 struct listnode
*node
;
1095 struct listnode
*next
;
1096 struct connected
*ifc
;
1098 for (node
= listhead (ifp
->connected
); node
; node
= next
)
1100 ifc
= listgetdata (node
);
1103 if (connected_same_prefix (ifc
->address
, p
))
1110 connected_delete_by_prefix (struct interface
*ifp
, struct prefix
*p
)
1112 struct listnode
*node
;
1113 struct listnode
*next
;
1114 struct connected
*ifc
;
1116 /* In case of same prefix come, replace it with new one. */
1117 for (node
= listhead (ifp
->connected
); node
; node
= next
)
1119 ifc
= listgetdata (node
);
1122 if (connected_same_prefix (ifc
->address
, p
))
1124 listnode_delete (ifp
->connected
, ifc
);
1131 /* Find the address on our side that will be used when packets
1134 connected_lookup_prefix (struct interface
*ifp
, struct prefix
*addr
)
1136 struct listnode
*cnode
;
1137 struct connected
*c
;
1138 struct connected
*match
;
1142 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
1144 if (c
->address
&& (c
->address
->family
== addr
->family
) &&
1145 prefix_match(CONNECTED_PREFIX(c
), addr
) &&
1146 (!match
|| (c
->address
->prefixlen
> match
->address
->prefixlen
)))
1153 connected_add_by_prefix (struct interface
*ifp
, struct prefix
*p
,
1154 struct prefix
*destination
)
1156 struct connected
*ifc
;
1158 /* Allocate new connected address. */
1159 ifc
= connected_new ();
1162 /* Fetch interface address */
1163 ifc
->address
= prefix_new();
1164 memcpy (ifc
->address
, p
, sizeof(struct prefix
));
1166 /* Fetch dest address */
1169 ifc
->destination
= prefix_new();
1170 memcpy (ifc
->destination
, destination
, sizeof(struct prefix
));
1173 /* Add connected address to the interface. */
1174 listnode_add (ifp
->connected
, ifc
);
1178 #if 0 /* this route_table of struct connected's is unused
1179 * however, it would be good to use a route_table rather than
1182 /* Interface looking up by interface's address. */
1183 /* Interface's IPv4 address reverse lookup table. */
1184 struct route_table
*ifaddr_ipv4_table
;
1185 /* struct route_table *ifaddr_ipv6_table; */
1188 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
1190 struct route_node
*rn
;
1191 struct prefix_ipv4 p
;
1194 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1197 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1200 route_unlock_node (rn
);
1201 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1202 inet_ntoa (*ifaddr
));
1209 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1211 struct route_node
*rn
;
1212 struct prefix_ipv4 p
;
1215 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1218 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1221 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1222 inet_ntoa (*ifaddr
));
1226 route_unlock_node (rn
);
1227 route_unlock_node (rn
);
1230 /* Lookup interface by interface's IP address or interface index. */
1231 static struct interface
*
1232 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1234 struct prefix_ipv4 p
;
1235 struct route_node
*rn
;
1236 struct interface
*ifp
;
1241 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1244 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1249 route_unlock_node (rn
);
1253 return if_lookup_by_index(ifindex
);
1255 #endif /* ifaddr_ipv4_table */
1257 /* Initialize interface list. */
1259 if_init (struct list
**intf_list
)
1261 *intf_list
= list_new ();
1263 ifaddr_ipv4_table
= route_table_init ();
1264 #endif /* ifaddr_ipv4_table */
1266 (*intf_list
)->cmp
= (int (*)(void *, void *))if_cmp_func
;
1270 if_terminate (struct list
**intf_list
)
1274 struct interface
*ifp
;
1276 ifp
= listnode_head (*intf_list
);
1282 ifp
->node
->info
= NULL
;
1283 route_unlock_node (ifp
->node
);
1289 list_delete (*intf_list
);
1294 if_link_type_str (enum zebra_link_type llt
)
1298 #define llts(T,S) case (T): return (S)
1299 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1300 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1301 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1302 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1303 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1304 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1305 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1306 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1307 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1308 llts(ZEBRA_LLT_ATM
, "ATM");
1309 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1310 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1311 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1312 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1313 llts(ZEBRA_LLT_SLIP
, "SLIP");
1314 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1315 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1316 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1317 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1318 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1319 llts(ZEBRA_LLT_PPP
, "PPP");
1320 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1321 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1322 llts(ZEBRA_LLT_LAPB
, "LAPB");
1323 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1324 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1325 llts(ZEBRA_LLT_FRAD
, "FRAD");
1326 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1327 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1328 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1329 llts(ZEBRA_LLT_FDDI
, "FDDI");
1330 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1331 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1332 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1333 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1334 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1335 llts(ZEBRA_LLT_IRDA
, "IrDA");
1336 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1337 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1338 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1339 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1340 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1341 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1342 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1343 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1344 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1346 zlog_warn ("Unknown value %d", llt
);
1347 return "Unknown type!";
1353 struct if_link_params
*
1354 if_link_params_get (struct interface
*ifp
)
1358 if (ifp
->link_params
!= NULL
)
1359 return ifp
->link_params
;
1361 struct if_link_params
*iflp
= XCALLOC(MTYPE_IF_LINK_PARAMS
,
1362 sizeof (struct if_link_params
));
1363 if (iflp
== NULL
) return NULL
;
1365 /* Set TE metric == standard metric */
1366 iflp
->te_metric
= ifp
->metric
;
1368 /* Compute default bandwidth based on interface */
1369 int bw
= (float)((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1370 * TE_KILO_BIT
/ TE_BYTE
);
1372 /* Set Max, Reservable and Unreserved Bandwidth */
1374 iflp
->max_rsv_bw
= bw
;
1375 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1376 iflp
->unrsv_bw
[i
] = bw
;
1378 /* Update Link parameters status */
1379 iflp
->lp_status
= LP_TE
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1381 /* Finally attach newly created Link Parameters */
1382 ifp
->link_params
= iflp
;
1388 if_link_params_free (struct interface
*ifp
)
1390 if (ifp
->link_params
== NULL
) return;
1391 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1392 ifp
->link_params
= NULL
;