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"
40 DEFINE_MTYPE( LIB
, IF
, "Interface")
41 DEFINE_MTYPE_STATIC(LIB
, CONNECTED
, "Connected")
42 DEFINE_MTYPE_STATIC(LIB
, NBR_CONNECTED
, "Neighbor Connected")
43 DEFINE_MTYPE( LIB
, CONNECTED_LABEL
, "Connected interface label")
44 DEFINE_MTYPE_STATIC(LIB
, IF_LINK_PARAMS
, "Informational Link Parameters")
46 /* List of interfaces in only the default VRF */
49 /* One for each program. This structure is needed to store hooks. */
52 int (*if_new_hook
) (struct interface
*);
53 int (*if_delete_hook
) (struct interface
*);
56 /* Compare interface names, returning an integer greater than, equal to, or
57 * less than 0, (following the strcmp convention), according to the
58 * relationship between ifp1 and ifp2. Interface names consist of an
59 * alphabetic prefix and a numeric suffix. The primary sort key is
60 * lexicographic by name, and then numeric by number. No number sorts
61 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
65 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 letters, so
83 res
= strncmp(p1
, p2
, l1
);
85 /* names are different -> compare them */
89 /* with identical name part, go to numeric part */
98 x1
= strtol(p1
, &p1
, 10);
99 x2
= strtol(p2
, &p2
, 10);
101 /* let's compare numbers now */
107 /* numbers were equal, lets do it again..
108 (it happens with name like "eth123.456:789") */
118 if_cmp_func (struct interface
*ifp1
, struct interface
*ifp2
)
120 return if_cmp_name_func (ifp1
->name
, ifp2
->name
);
123 /* Create new interface structure. */
125 if_create_vrf (const char *name
, int namelen
, vrf_id_t vrf_id
)
127 struct interface
*ifp
;
128 struct list
*intf_list
= vrf_iflist_get (vrf_id
);
130 ifp
= XCALLOC (MTYPE_IF
, sizeof (struct interface
));
131 ifp
->ifindex
= IFINDEX_INTERNAL
;
134 assert (namelen
<= INTERFACE_NAMSIZ
); /* Need space for '\0' at end. */
135 strncpy (ifp
->name
, name
, namelen
);
136 ifp
->name
[namelen
] = '\0';
137 ifp
->vrf_id
= vrf_id
;
138 if (if_lookup_by_name_vrf (ifp
->name
, vrf_id
) == NULL
)
139 listnode_add_sort (intf_list
, ifp
);
141 zlog_err("if_create(%s): corruption detected -- interface with this "
142 "name exists already in VRF %u!", ifp
->name
, vrf_id
);
143 ifp
->connected
= list_new ();
144 ifp
->connected
->del
= (void (*) (void *)) connected_free
;
146 ifp
->nbr_connected
= list_new ();
147 ifp
->nbr_connected
->del
= (void (*) (void *)) nbr_connected_free
;
149 /* Enable Link-detection by default */
150 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
152 if (if_master
.if_new_hook
)
153 (*if_master
.if_new_hook
) (ifp
);
159 if_create (const char *name
, int namelen
)
161 return if_create_vrf (name
, namelen
, VRF_DEFAULT
);
164 /* Create new interface structure. */
166 if_update_vrf (struct interface
*ifp
, const char *name
, int namelen
, vrf_id_t vrf_id
)
168 struct list
*intf_list
= vrf_iflist_get (vrf_id
);
170 /* remove interface from old master vrf list */
171 if (vrf_iflist (ifp
->vrf_id
))
172 listnode_delete (vrf_iflist (ifp
->vrf_id
), ifp
);
175 assert (namelen
<= INTERFACE_NAMSIZ
); /* Need space for '\0' at end. */
176 strncpy (ifp
->name
, name
, namelen
);
177 ifp
->name
[namelen
] = '\0';
178 ifp
->vrf_id
= vrf_id
;
179 if (if_lookup_by_name_vrf (ifp
->name
, vrf_id
) == NULL
)
180 listnode_add_sort (intf_list
, ifp
);
182 zlog_err("if_create(%s): corruption detected -- interface with this "
183 "name exists already in VRF %u!", ifp
->name
, vrf_id
);
189 /* Delete interface structure. */
191 if_delete_retain (struct interface
*ifp
)
193 if (if_master
.if_delete_hook
)
194 (*if_master
.if_delete_hook
) (ifp
);
196 /* Free connected address list */
197 list_delete_all_node (ifp
->connected
);
199 /* Free connected nbr address list */
200 list_delete_all_node (ifp
->nbr_connected
);
203 /* Delete and free interface structure. */
205 if_delete (struct interface
*ifp
)
207 listnode_delete (vrf_iflist (ifp
->vrf_id
), ifp
);
209 if_delete_retain(ifp
);
211 list_free (ifp
->connected
);
212 list_free (ifp
->nbr_connected
);
214 if_link_params_free (ifp
);
216 XFREE (MTYPE_IF
, ifp
);
219 /* Add hook to interface master. */
221 if_add_hook (int type
, int (*func
)(struct interface
*ifp
))
225 if_master
.if_new_hook
= func
;
228 if_master
.if_delete_hook
= func
;
235 /* Interface existance check by index. */
237 if_lookup_by_index_vrf (ifindex_t ifindex
, vrf_id_t vrf_id
)
239 struct listnode
*node
;
240 struct interface
*ifp
;
242 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
244 if (ifp
->ifindex
== ifindex
)
251 if_lookup_by_index (ifindex_t ifindex
)
253 return if_lookup_by_index_vrf (ifindex
, VRF_DEFAULT
);
257 ifindex2ifname_vrf (ifindex_t ifindex
, vrf_id_t vrf_id
)
259 struct interface
*ifp
;
261 return ((ifp
= if_lookup_by_index_vrf (ifindex
, vrf_id
)) != NULL
) ?
262 ifp
->name
: "unknown";
266 ifindex2ifname (ifindex_t ifindex
)
268 return ifindex2ifname_vrf (ifindex
, VRF_DEFAULT
);
272 ifname2ifindex_vrf (const char *name
, vrf_id_t vrf_id
)
274 struct interface
*ifp
;
276 return ((ifp
= if_lookup_by_name_vrf (name
, vrf_id
)) != NULL
) ? ifp
->ifindex
281 ifname2ifindex (const char *name
)
283 return ifname2ifindex_vrf (name
, VRF_DEFAULT
);
286 /* Interface existance check by interface name. */
288 if_lookup_by_name_vrf (const char *name
, vrf_id_t vrf_id
)
290 struct listnode
*node
;
291 struct interface
*ifp
;
294 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
296 if (strcmp(name
, ifp
->name
) == 0)
303 if_lookup_by_name_all_vrf (const char *name
)
305 struct interface
*ifp
;
306 struct vrf
*vrf
= NULL
;
309 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
311 vrf
= vrf_iter2vrf (iter
);
312 ifp
= if_lookup_by_name_vrf (name
, vrf
->vrf_id
);
321 if_lookup_by_name (const char *name
)
323 return if_lookup_by_name_vrf (name
, VRF_DEFAULT
);
327 if_lookup_by_name_len_vrf (const char *name
, size_t namelen
, vrf_id_t vrf_id
)
329 struct listnode
*node
;
330 struct interface
*ifp
;
332 if (namelen
> INTERFACE_NAMSIZ
)
335 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
337 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
344 if_lookup_by_name_len(const char *name
, size_t namelen
)
346 return if_lookup_by_name_len_vrf (name
, namelen
, VRF_DEFAULT
);
349 /* Lookup interface by IPv4 address. */
351 if_lookup_exact_address_vrf (void *src
, int family
, vrf_id_t vrf_id
)
353 struct listnode
*node
;
354 struct listnode
*cnode
;
355 struct interface
*ifp
;
359 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
361 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
365 if (p
&& (p
->family
== family
))
367 if (family
== AF_INET
)
369 if (IPV4_ADDR_SAME (&p
->u
.prefix4
, (struct in_addr
*)src
))
372 else if (family
== AF_INET6
)
374 if (IPV6_ADDR_SAME (&p
->u
.prefix4
, (struct in6_addr
*)src
))
384 if_lookup_exact_address (void *src
, int family
)
386 return if_lookup_exact_address_vrf (src
, family
, VRF_DEFAULT
);
389 /* Lookup interface by IPv4 address. */
391 if_lookup_address_vrf (void *matchaddr
, int family
, vrf_id_t vrf_id
)
393 struct listnode
*node
;
396 struct listnode
*cnode
;
397 struct interface
*ifp
;
399 struct interface
*match
;
401 if (family
== AF_INET
)
403 addr
.family
= AF_INET
;
404 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
405 addr
.prefixlen
= IPV4_MAX_BITLEN
;
407 else if (family
== AF_INET6
)
409 addr
.family
= AF_INET6
;
410 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
411 addr
.prefixlen
= IPV6_MAX_BITLEN
;
416 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
418 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
420 if (c
->address
&& (c
->address
->family
== AF_INET
) &&
421 prefix_match(CONNECTED_PREFIX(c
), &addr
) &&
422 (c
->address
->prefixlen
> bestlen
))
424 bestlen
= c
->address
->prefixlen
;
433 if_lookup_address (void *matchaddr
, int family
)
435 return if_lookup_address_vrf (matchaddr
, family
, VRF_DEFAULT
);
438 /* Lookup interface by prefix */
440 if_lookup_prefix_vrf (struct prefix
*prefix
, vrf_id_t vrf_id
)
442 struct listnode
*node
;
443 struct listnode
*cnode
;
444 struct interface
*ifp
;
447 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
449 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
451 if (prefix_cmp(c
->address
, prefix
) == 0)
461 if_lookup_prefix (struct prefix
*prefix
)
463 return if_lookup_prefix_vrf (prefix
, VRF_DEFAULT
);
466 /* Get interface by name if given name interface doesn't exist create
469 if_get_by_name_vrf (const char *name
, vrf_id_t vrf_id
)
471 struct interface
*ifp
;
473 return ((ifp
= if_lookup_by_name_vrf (name
, vrf_id
)) != NULL
) ? ifp
:
474 if_create_vrf (name
, strlen(name
), vrf_id
);
478 if_get_by_name (const char *name
)
480 return if_get_by_name_vrf (name
, VRF_DEFAULT
);
484 if_get_by_name_len_vrf (const char *name
, size_t namelen
, vrf_id_t vrf_id
, int vty
)
486 struct interface
*ifp
;
487 struct listnode
*node
;
488 struct vrf
*vrf
= NULL
;
491 ifp
= if_lookup_by_name_len_vrf (name
, namelen
, vrf_id
);
495 /* Didn't find the interface on that vrf. Defined on a different one? */
496 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
498 vrf
= vrf_iter2vrf(iter
);
499 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf
->vrf_id
), node
, ifp
))
501 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
503 /* Found a match. If the interface command was entered in vty without a
504 * VRF (passed as VRF_DEFAULT), accept the ifp we found. If a vrf was
505 * entered and there is a mismatch, reject it if from vty. If it came
506 * from the kernel by way of zclient, believe it and update
507 * the ifp accordingly.
511 if (vrf_id
== VRF_DEFAULT
)
517 if_update_vrf (ifp
, name
, namelen
, vrf_id
);
523 return (if_create_vrf (name
, namelen
, vrf_id
));
527 if_get_by_name_len (const char *name
, size_t namelen
)
529 return if_get_by_name_len_vrf (name
, namelen
, VRF_DEFAULT
, 0);
532 /* Does interface up ? */
534 if_is_up (struct interface
*ifp
)
536 return ifp
->flags
& IFF_UP
;
539 /* Is interface running? */
541 if_is_running (struct interface
*ifp
)
543 return ifp
->flags
& IFF_RUNNING
;
546 /* Is the interface operative, eg. either UP & RUNNING
547 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
548 if ptm checking is enabled, then ptm check has passed */
550 if_is_operative (struct interface
*ifp
)
552 return ((ifp
->flags
& IFF_UP
) &&
553 (((ifp
->flags
& IFF_RUNNING
) &&
554 (ifp
->ptm_status
|| !ifp
->ptm_enable
)) ||
555 !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
558 /* Is the interface operative, eg. either UP & RUNNING
559 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
561 if_is_no_ptm_operative (struct interface
*ifp
)
563 return ((ifp
->flags
& IFF_UP
) &&
564 ((ifp
->flags
& IFF_RUNNING
) ||
565 !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
568 /* Is this loopback interface ? */
570 if_is_loopback (struct interface
*ifp
)
572 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
573 * but Y on platform N?
575 return (ifp
->flags
& (IFF_LOOPBACK
|IFF_NOXMIT
|IFF_VIRTUAL
));
578 /* Does this interface support broadcast ? */
580 if_is_broadcast (struct interface
*ifp
)
582 return ifp
->flags
& IFF_BROADCAST
;
585 /* Does this interface support broadcast ? */
587 if_is_pointopoint (struct interface
*ifp
)
589 return ifp
->flags
& IFF_POINTOPOINT
;
592 /* Does this interface support multicast ? */
594 if_is_multicast (struct interface
*ifp
)
596 return ifp
->flags
& IFF_MULTICAST
;
599 /* Printout flag information into log */
601 if_flag_dump (unsigned long flag
)
604 static char logbuf
[BUFSIZ
];
606 #define IFF_OUT_LOG(X,STR) \
610 strlcat (logbuf, ",", BUFSIZ); \
613 strlcat (logbuf, STR, BUFSIZ); \
616 strlcpy (logbuf
, "<", BUFSIZ
);
617 IFF_OUT_LOG (IFF_UP
, "UP");
618 IFF_OUT_LOG (IFF_BROADCAST
, "BROADCAST");
619 IFF_OUT_LOG (IFF_DEBUG
, "DEBUG");
620 IFF_OUT_LOG (IFF_LOOPBACK
, "LOOPBACK");
621 IFF_OUT_LOG (IFF_POINTOPOINT
, "POINTOPOINT");
622 IFF_OUT_LOG (IFF_NOTRAILERS
, "NOTRAILERS");
623 IFF_OUT_LOG (IFF_RUNNING
, "RUNNING");
624 IFF_OUT_LOG (IFF_NOARP
, "NOARP");
625 IFF_OUT_LOG (IFF_PROMISC
, "PROMISC");
626 IFF_OUT_LOG (IFF_ALLMULTI
, "ALLMULTI");
627 IFF_OUT_LOG (IFF_OACTIVE
, "OACTIVE");
628 IFF_OUT_LOG (IFF_SIMPLEX
, "SIMPLEX");
629 IFF_OUT_LOG (IFF_LINK0
, "LINK0");
630 IFF_OUT_LOG (IFF_LINK1
, "LINK1");
631 IFF_OUT_LOG (IFF_LINK2
, "LINK2");
632 IFF_OUT_LOG (IFF_MULTICAST
, "MULTICAST");
633 IFF_OUT_LOG (IFF_NOXMIT
, "NOXMIT");
634 IFF_OUT_LOG (IFF_NORTEXCH
, "NORTEXCH");
635 IFF_OUT_LOG (IFF_VIRTUAL
, "VIRTUAL");
636 IFF_OUT_LOG (IFF_IPV4
, "IPv4");
637 IFF_OUT_LOG (IFF_IPV6
, "IPv6");
639 strlcat (logbuf
, ">", BUFSIZ
);
647 if_dump (const struct interface
*ifp
)
649 struct listnode
*node
;
650 struct connected
*c
__attribute__((unused
));
652 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, c
))
653 zlog_info ("Interface %s vrf %u index %d metric %d mtu %d "
655 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
, ifp
->mtu
,
656 ifp
->mtu6
, if_flag_dump (ifp
->flags
));
659 /* Interface printing for all interface. */
663 struct list
*intf_list
;
664 struct listnode
*node
;
668 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
669 if ((intf_list
= vrf_iter2iflist (iter
)) != NULL
)
670 for (ALL_LIST_ELEMENTS_RO (intf_list
, node
, p
))
674 DEFUN (interface_desc
,
676 "description LINE...",
677 "Interface specific description\n"
678 "Characters describing this interface\n")
680 struct interface
*ifp
;
687 XFREE (MTYPE_TMP
, ifp
->desc
);
688 ifp
->desc
= argv_concat(argv
, argc
, 1);
693 DEFUN (no_interface_desc
,
694 no_interface_desc_cmd
,
697 "Interface specific description\n")
699 struct interface
*ifp
;
703 XFREE (MTYPE_TMP
, ifp
->desc
);
710 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
711 * a seperate struct interface for each logical interface, so config
712 * file may be full of 'interface fooX:Y'. Solaris however does not
713 * expose logical interfaces via PF_ROUTE, so trying to track logical
714 * interfaces can be fruitless, for that reason Quagga only tracks
715 * the primary IP interface.
717 * We try accomodate SUNWzebra by:
718 * - looking up the interface name, to see whether it exists, if so
720 * - for protocol daemons, this could only because zebra told us of
722 * - for zebra, only because it learnt from kernel
724 * - search the name to see if it contains a sub-ipif / logical interface
725 * seperator, the ':' char. If it does:
726 * - text up to that char must be the primary name - get that name.
728 * - no idea, just get the name in its entirety.
730 static struct interface
*
731 if_sunwzebra_get (const char *name
, size_t nlen
, vrf_id_t vrf_id
)
733 struct interface
*ifp
;
736 if ( (ifp
= if_lookup_by_name_len_vrf (name
, nlen
, vrf_id
)) != NULL
)
739 /* hunt the primary interface name... */
740 while (seppos
< nlen
&& name
[seppos
] != ':')
743 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
745 return if_get_by_name_len_vrf (name
, seppos
, vrf_id
, 1);
747 return if_get_by_name_len_vrf (name
, nlen
, vrf_id
, 1);
753 "interface IFNAME ["VRF_CMD_STR
"]",
754 "Select an interface to configure\n"
759 const char *ifname
= argv
[idx_ifname
]->arg
;
760 const char *vrfname
= (argc
> 2) ? argv
[3]->arg
: NULL
;
762 struct interface
*ifp
;
764 vrf_id_t vrf_id
= VRF_DEFAULT
;
766 if ((sl
= strlen(ifname
)) > INTERFACE_NAMSIZ
)
768 vty_out (vty
, "%% Interface name %s is invalid: length exceeds "
770 ifname
, INTERFACE_NAMSIZ
, VTY_NEWLINE
);
774 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
775 Imagine forward reference of a vrf by name in this interface config */
777 VRF_GET_ID (vrf_id
, vrfname
);
780 ifp
= if_sunwzebra_get (ifname
, sl
, vrf_id
);
782 ifp
= if_get_by_name_len_vrf (ifname
, sl
, vrf_id
, 1);
787 vty_out (vty
, "%% interface %s not in %s%s", ifname
, vrfname
, VTY_NEWLINE
);
791 vty
->node
= INTERFACE_NODE
;
796 DEFUN_NOSH (no_interface
,
798 "no interface IFNAME [VRF_CMD_STR]",
800 "Delete a pseudo interface's configuration\n"
804 const char *ifname
= argv
[2]->arg
;
805 const char *vrfname
= (argc
> 3) ? argv
[3]->arg
: NULL
;
807 // deleting interface
808 struct interface
*ifp
;
809 vrf_id_t vrf_id
= VRF_DEFAULT
;
812 VRF_GET_ID (vrf_id
, vrfname
);
814 ifp
= if_lookup_by_name_vrf (ifname
, vrf_id
);
818 vty_out (vty
, "%% Interface %s does not exist%s", ifname
, VTY_NEWLINE
);
822 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
824 vty_out (vty
, "%% Only inactive interfaces can be deleted%s",
837 "Select a VRF to configure\n"
841 const char *vrfname
= argv
[idx_name
]->arg
;
846 if ((sl
= strlen(vrfname
)) > VRF_NAMSIZ
)
848 vty_out (vty
, "%% VRF name %s is invalid: length exceeds "
850 vrfname
, VRF_NAMSIZ
, VTY_NEWLINE
);
854 vrfp
= vrf_get (VRF_UNKNOWN
, vrfname
);
857 vty
->node
= VRF_NODE
;
866 "Delete a pseudo VRF's configuration\n"
869 const char *vrfname
= argv
[2]->arg
;
873 vrfp
= vrf_list_lookup_by_name (vrfname
);;
877 vty_out (vty
, "%% VRF %s does not exist%s", vrfname
, VTY_NEWLINE
);
881 if (CHECK_FLAG (vrfp
->status
, VRF_ACTIVE
))
883 vty_out (vty
, "%% Only inactive VRFs can be deleted%s",
894 /* For debug purpose. */
897 "show address [VRF_CMD_STR]",
902 int idx_vrf_cmd_str
= 2;
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
[idx_vrf_cmd_str
]->arg
);
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
,
927 DEFUN (show_address_vrf_all
,
928 show_address_vrf_all_cmd
,
929 "show address " VRF_ALL_CMD_STR
,
932 VRF_ALL_CMD_HELP_STR
)
934 struct list
*intf_list
;
935 struct listnode
*node
;
936 struct listnode
*node2
;
937 struct interface
*ifp
;
938 struct connected
*ifc
;
942 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
944 intf_list
= vrf_iter2iflist (iter
);
945 if (!intf_list
|| !listcount (intf_list
))
948 vty_out (vty
, "%sVRF %u%s%s", VTY_NEWLINE
, vrf_iter2id (iter
),
949 VTY_NEWLINE
, VTY_NEWLINE
);
951 for (ALL_LIST_ELEMENTS_RO (intf_list
, node
, ifp
))
953 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
957 if (p
->family
== AF_INET
)
958 vty_out (vty
, "%s/%d%s", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
,
966 /* Allocate connected structure. */
970 return XCALLOC (MTYPE_CONNECTED
, sizeof (struct connected
));
973 /* Allocate nbr connected structure. */
974 struct nbr_connected
*
975 nbr_connected_new (void)
977 return XCALLOC (MTYPE_NBR_CONNECTED
, sizeof (struct nbr_connected
));
980 /* Free connected structure. */
982 connected_free (struct connected
*connected
)
984 if (connected
->address
)
985 prefix_free (connected
->address
);
987 if (connected
->destination
)
988 prefix_free (connected
->destination
);
990 if (connected
->label
)
991 XFREE (MTYPE_CONNECTED_LABEL
, connected
->label
);
993 XFREE (MTYPE_CONNECTED
, connected
);
996 /* Free nbr connected structure. */
998 nbr_connected_free (struct nbr_connected
*connected
)
1000 if (connected
->address
)
1001 prefix_free (connected
->address
);
1003 XFREE (MTYPE_NBR_CONNECTED
, connected
);
1006 /* If same interface nbr address already exists... */
1007 struct nbr_connected
*
1008 nbr_connected_check (struct interface
*ifp
, struct prefix
*p
)
1010 struct nbr_connected
*ifc
;
1011 struct listnode
*node
;
1013 for (ALL_LIST_ELEMENTS_RO (ifp
->nbr_connected
, node
, ifc
))
1014 if (prefix_same (ifc
->address
, p
))
1020 /* Print if_addr structure. */
1021 static void __attribute__ ((unused
))
1022 connected_log (struct connected
*connected
, char *str
)
1025 struct interface
*ifp
;
1026 char logbuf
[BUFSIZ
];
1029 ifp
= connected
->ifp
;
1030 p
= connected
->address
;
1032 snprintf (logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ",
1033 str
, ifp
->name
, ifp
->vrf_id
, prefix_family_str (p
),
1034 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1037 p
= connected
->destination
;
1040 strncat (logbuf
, inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1041 BUFSIZ
- strlen(logbuf
));
1043 zlog (NULL
, LOG_INFO
, "%s", logbuf
);
1046 /* Print if_addr structure. */
1047 static void __attribute__ ((unused
))
1048 nbr_connected_log (struct nbr_connected
*connected
, char *str
)
1051 struct interface
*ifp
;
1052 char logbuf
[BUFSIZ
];
1055 ifp
= connected
->ifp
;
1056 p
= connected
->address
;
1058 snprintf (logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ",
1059 str
, ifp
->name
, prefix_family_str (p
),
1060 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1063 zlog (NULL
, LOG_INFO
, "%s", logbuf
);
1066 /* If two connected address has same prefix return 1. */
1068 connected_same_prefix (struct prefix
*p1
, struct prefix
*p2
)
1070 if (p1
->family
== p2
->family
)
1072 if (p1
->family
== AF_INET
&&
1073 IPV4_ADDR_SAME (&p1
->u
.prefix4
, &p2
->u
.prefix4
))
1076 if (p1
->family
== AF_INET6
&&
1077 IPV6_ADDR_SAME (&p1
->u
.prefix6
, &p2
->u
.prefix6
))
1079 #endif /* HAVE_IPV6 */
1085 connected_lookup_prefix_exact (struct interface
*ifp
, struct prefix
*p
)
1087 struct listnode
*node
;
1088 struct listnode
*next
;
1089 struct connected
*ifc
;
1091 for (node
= listhead (ifp
->connected
); node
; node
= next
)
1093 ifc
= listgetdata (node
);
1096 if (connected_same_prefix (ifc
->address
, p
))
1103 connected_delete_by_prefix (struct interface
*ifp
, struct prefix
*p
)
1105 struct listnode
*node
;
1106 struct listnode
*next
;
1107 struct connected
*ifc
;
1109 /* In case of same prefix come, replace it with new one. */
1110 for (node
= listhead (ifp
->connected
); node
; node
= next
)
1112 ifc
= listgetdata (node
);
1115 if (connected_same_prefix (ifc
->address
, p
))
1117 listnode_delete (ifp
->connected
, ifc
);
1124 /* Find the address on our side that will be used when packets
1127 connected_lookup_prefix (struct interface
*ifp
, struct prefix
*addr
)
1129 struct listnode
*cnode
;
1130 struct connected
*c
;
1131 struct connected
*match
;
1135 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
1137 if (c
->address
&& (c
->address
->family
== addr
->family
) &&
1138 prefix_match(CONNECTED_PREFIX(c
), addr
) &&
1139 (!match
|| (c
->address
->prefixlen
> match
->address
->prefixlen
)))
1146 connected_add_by_prefix (struct interface
*ifp
, struct prefix
*p
,
1147 struct prefix
*destination
)
1149 struct connected
*ifc
;
1151 /* Allocate new connected address. */
1152 ifc
= connected_new ();
1155 /* Fetch interface address */
1156 ifc
->address
= prefix_new();
1157 memcpy (ifc
->address
, p
, sizeof(struct prefix
));
1159 /* Fetch dest address */
1162 ifc
->destination
= prefix_new();
1163 memcpy (ifc
->destination
, destination
, sizeof(struct prefix
));
1166 /* Add connected address to the interface. */
1167 listnode_add (ifp
->connected
, ifc
);
1171 #ifndef HAVE_IF_NAMETOINDEX
1173 if_nametoindex (const char *name
)
1175 struct interface
*ifp
;
1177 return ((ifp
= if_lookup_by_name_len(name
, strnlen(name
, IFNAMSIZ
))) != NULL
)
1182 #ifndef HAVE_IF_INDEXTONAME
1184 if_indextoname (ifindex_t ifindex
, char *name
)
1186 struct interface
*ifp
;
1188 if (!(ifp
= if_lookup_by_index(ifindex
)))
1190 strncpy (name
, ifp
->name
, IFNAMSIZ
);
1195 #if 0 /* this route_table of struct connected's is unused
1196 * however, it would be good to use a route_table rather than
1199 /* Interface looking up by interface's address. */
1200 /* Interface's IPv4 address reverse lookup table. */
1201 struct route_table
*ifaddr_ipv4_table
;
1202 /* struct route_table *ifaddr_ipv6_table; */
1205 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
1207 struct route_node
*rn
;
1208 struct prefix_ipv4 p
;
1211 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1214 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1217 route_unlock_node (rn
);
1218 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1219 inet_ntoa (*ifaddr
));
1226 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1228 struct route_node
*rn
;
1229 struct prefix_ipv4 p
;
1232 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1235 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1238 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1239 inet_ntoa (*ifaddr
));
1243 route_unlock_node (rn
);
1244 route_unlock_node (rn
);
1247 /* Lookup interface by interface's IP address or interface index. */
1248 static struct interface
*
1249 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1251 struct prefix_ipv4 p
;
1252 struct route_node
*rn
;
1253 struct interface
*ifp
;
1258 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1261 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1266 route_unlock_node (rn
);
1270 return if_lookup_by_index(ifindex
);
1272 #endif /* ifaddr_ipv4_table */
1274 /* Initialize interface list. */
1276 if_init (struct list
**intf_list
)
1278 *intf_list
= list_new ();
1280 ifaddr_ipv4_table
= route_table_init ();
1281 #endif /* ifaddr_ipv4_table */
1283 (*intf_list
)->cmp
= (int (*)(void *, void *))if_cmp_func
;
1287 if_terminate (struct list
**intf_list
)
1291 struct interface
*ifp
;
1293 ifp
= listnode_head (*intf_list
);
1299 ifp
->node
->info
= NULL
;
1300 route_unlock_node (ifp
->node
);
1306 list_delete (*intf_list
);
1311 if_link_type_str (enum zebra_link_type llt
)
1315 #define llts(T,S) case (T): return (S)
1316 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1317 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1318 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1319 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1320 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1321 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1322 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1323 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1324 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1325 llts(ZEBRA_LLT_ATM
, "ATM");
1326 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1327 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1328 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1329 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1330 llts(ZEBRA_LLT_SLIP
, "SLIP");
1331 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1332 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1333 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1334 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1335 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1336 llts(ZEBRA_LLT_PPP
, "PPP");
1337 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1338 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1339 llts(ZEBRA_LLT_LAPB
, "LAPB");
1340 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1341 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1342 llts(ZEBRA_LLT_FRAD
, "FRAD");
1343 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1344 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1345 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1346 llts(ZEBRA_LLT_FDDI
, "FDDI");
1347 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1348 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1349 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1350 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1351 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1352 llts(ZEBRA_LLT_IRDA
, "IrDA");
1353 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1354 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1355 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1356 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1357 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1358 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1359 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1360 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1361 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1363 zlog_warn ("Unknown value %d", llt
);
1364 return "Unknown type!";
1370 struct if_link_params
*
1371 if_link_params_get (struct interface
*ifp
)
1375 if (ifp
->link_params
!= NULL
)
1376 return ifp
->link_params
;
1378 struct if_link_params
*iflp
= XCALLOC(MTYPE_IF_LINK_PARAMS
,
1379 sizeof (struct if_link_params
));
1380 if (iflp
== NULL
) return NULL
;
1382 /* Set TE metric == standard metric */
1383 iflp
->te_metric
= ifp
->metric
;
1385 /* Compute default bandwidth based on interface */
1386 int bw
= (float)((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1387 * TE_KILO_BIT
/ TE_BYTE
);
1389 /* Set Max, Reservable and Unreserved Bandwidth */
1391 iflp
->max_rsv_bw
= bw
;
1392 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1393 iflp
->unrsv_bw
[i
] = bw
;
1395 /* Update Link parameters status */
1396 iflp
->lp_status
= LP_TE
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1398 /* Finally attach newly created Link Parameters */
1399 ifp
->link_params
= iflp
;
1405 if_link_params_free (struct interface
*ifp
)
1407 if (ifp
->link_params
== NULL
) return;
1408 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1409 ifp
->link_params
= NULL
;