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 DEFINE_QOBJ_TYPE(interface
)
48 /* List of interfaces in only the default VRF */
51 /* One for each program. This structure is needed to store hooks. */
54 int (*if_new_hook
) (struct interface
*);
55 int (*if_delete_hook
) (struct interface
*);
58 /* Compare interface names, returning an integer greater than, equal to, or
59 * less than 0, (following the strcmp convention), according to the
60 * relationship between ifp1 and ifp2. Interface names consist of an
61 * alphabetic prefix and a numeric suffix. The primary sort key is
62 * lexicographic by name, and then numeric by number. No number sorts
63 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
67 if_cmp_name_func (char *p1
, char *p2
)
74 /* look up to any number */
75 l1
= strcspn(p1
, "0123456789");
76 l2
= strcspn(p2
, "0123456789");
78 /* name lengths are different -> compare names */
80 return (strcmp(p1
, p2
));
82 /* Note that this relies on all numbers being less than all letters, so
85 res
= strncmp(p1
, p2
, l1
);
87 /* names are different -> compare them */
91 /* with identical name part, go to numeric part */
100 x1
= strtol(p1
, &p1
, 10);
101 x2
= strtol(p2
, &p2
, 10);
103 /* let's compare numbers now */
109 /* numbers were equal, lets do it again..
110 (it happens with name like "eth123.456:789") */
120 if_cmp_func (struct interface
*ifp1
, struct interface
*ifp2
)
122 return if_cmp_name_func (ifp1
->name
, ifp2
->name
);
125 /* Create new interface structure. */
127 if_create_vrf (const char *name
, int namelen
, vrf_id_t vrf_id
)
129 struct interface
*ifp
;
130 struct list
*intf_list
= vrf_iflist_get (vrf_id
);
132 ifp
= XCALLOC (MTYPE_IF
, sizeof (struct interface
));
133 ifp
->ifindex
= IFINDEX_INTERNAL
;
136 assert (namelen
<= INTERFACE_NAMSIZ
); /* Need space for '\0' at end. */
137 strncpy (ifp
->name
, name
, namelen
);
138 ifp
->name
[namelen
] = '\0';
139 ifp
->vrf_id
= vrf_id
;
140 if (if_lookup_by_name_vrf (ifp
->name
, vrf_id
) == NULL
)
141 listnode_add_sort (intf_list
, ifp
);
143 zlog_err("if_create(%s): corruption detected -- interface with this "
144 "name exists already in VRF %u!", ifp
->name
, vrf_id
);
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
);
156 if (if_master
.if_new_hook
)
157 (*if_master
.if_new_hook
) (ifp
);
163 if_create (const char *name
, int namelen
)
165 return if_create_vrf (name
, namelen
, VRF_DEFAULT
);
168 /* Create new interface structure. */
170 if_update_vrf (struct interface
*ifp
, const char *name
, int namelen
, vrf_id_t vrf_id
)
172 struct list
*intf_list
= vrf_iflist_get (vrf_id
);
174 /* remove interface from old master vrf list */
175 if (vrf_iflist (ifp
->vrf_id
))
176 listnode_delete (vrf_iflist (ifp
->vrf_id
), ifp
);
179 assert (namelen
<= INTERFACE_NAMSIZ
); /* Need space for '\0' at end. */
180 strncpy (ifp
->name
, name
, namelen
);
181 ifp
->name
[namelen
] = '\0';
182 ifp
->vrf_id
= vrf_id
;
183 if (if_lookup_by_name_vrf (ifp
->name
, vrf_id
) == NULL
)
184 listnode_add_sort (intf_list
, ifp
);
186 zlog_err("if_create(%s): corruption detected -- interface with this "
187 "name exists already in VRF %u!", ifp
->name
, vrf_id
);
193 /* Delete interface structure. */
195 if_delete_retain (struct interface
*ifp
)
197 if (if_master
.if_delete_hook
)
198 (*if_master
.if_delete_hook
) (ifp
);
202 /* Free connected address list */
203 list_delete_all_node (ifp
->connected
);
205 /* Free connected nbr address list */
206 list_delete_all_node (ifp
->nbr_connected
);
209 /* Delete and free interface structure. */
211 if_delete (struct interface
*ifp
)
213 listnode_delete (vrf_iflist (ifp
->vrf_id
), ifp
);
215 if_delete_retain(ifp
);
217 list_free (ifp
->connected
);
218 list_free (ifp
->nbr_connected
);
220 if_link_params_free (ifp
);
222 XFREE (MTYPE_IF
, ifp
);
225 /* Add hook to interface master. */
227 if_add_hook (int type
, int (*func
)(struct interface
*ifp
))
231 if_master
.if_new_hook
= func
;
234 if_master
.if_delete_hook
= func
;
241 /* Interface existance check by index. */
243 if_lookup_by_index_vrf (ifindex_t ifindex
, vrf_id_t vrf_id
)
245 struct listnode
*node
;
246 struct interface
*ifp
;
248 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
250 if (ifp
->ifindex
== ifindex
)
257 if_lookup_by_index (ifindex_t ifindex
)
259 return if_lookup_by_index_vrf (ifindex
, VRF_DEFAULT
);
263 ifindex2ifname_vrf (ifindex_t ifindex
, vrf_id_t vrf_id
)
265 struct interface
*ifp
;
267 return ((ifp
= if_lookup_by_index_vrf (ifindex
, vrf_id
)) != NULL
) ?
268 ifp
->name
: "unknown";
272 ifindex2ifname (ifindex_t ifindex
)
274 return ifindex2ifname_vrf (ifindex
, VRF_DEFAULT
);
278 ifname2ifindex_vrf (const char *name
, vrf_id_t vrf_id
)
280 struct interface
*ifp
;
282 return ((ifp
= if_lookup_by_name_vrf (name
, vrf_id
)) != NULL
) ? ifp
->ifindex
287 ifname2ifindex (const char *name
)
289 return ifname2ifindex_vrf (name
, VRF_DEFAULT
);
292 /* Interface existance check by interface name. */
294 if_lookup_by_name_vrf (const char *name
, vrf_id_t vrf_id
)
296 struct listnode
*node
;
297 struct interface
*ifp
;
300 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
302 if (strcmp(name
, ifp
->name
) == 0)
309 if_lookup_by_name_all_vrf (const char *name
)
311 struct interface
*ifp
;
312 struct vrf
*vrf
= NULL
;
315 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
317 vrf
= vrf_iter2vrf (iter
);
318 ifp
= if_lookup_by_name_vrf (name
, vrf
->vrf_id
);
327 if_lookup_by_name (const char *name
)
329 return if_lookup_by_name_vrf (name
, VRF_DEFAULT
);
333 if_lookup_by_name_len_vrf (const char *name
, size_t namelen
, vrf_id_t vrf_id
)
335 struct listnode
*node
;
336 struct interface
*ifp
;
338 if (namelen
> INTERFACE_NAMSIZ
)
341 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
343 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
350 if_lookup_by_name_len(const char *name
, size_t namelen
)
352 return if_lookup_by_name_len_vrf (name
, namelen
, VRF_DEFAULT
);
355 /* Lookup interface by IPv4 address. */
357 if_lookup_exact_address_vrf (void *src
, int family
, vrf_id_t vrf_id
)
359 struct listnode
*node
;
360 struct listnode
*cnode
;
361 struct interface
*ifp
;
365 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
367 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
371 if (p
&& (p
->family
== family
))
373 if (family
== AF_INET
)
375 if (IPV4_ADDR_SAME (&p
->u
.prefix4
, (struct in_addr
*)src
))
378 else if (family
== AF_INET6
)
380 if (IPV6_ADDR_SAME (&p
->u
.prefix4
, (struct in6_addr
*)src
))
390 if_lookup_exact_address (void *src
, int family
)
392 return if_lookup_exact_address_vrf (src
, family
, VRF_DEFAULT
);
395 /* Lookup interface by IPv4 address. */
397 if_lookup_address_vrf (void *matchaddr
, int family
, vrf_id_t vrf_id
)
399 struct listnode
*node
;
402 struct listnode
*cnode
;
403 struct interface
*ifp
;
405 struct interface
*match
;
407 if (family
== AF_INET
)
409 addr
.family
= AF_INET
;
410 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
411 addr
.prefixlen
= IPV4_MAX_BITLEN
;
413 else if (family
== AF_INET6
)
415 addr
.family
= AF_INET6
;
416 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
417 addr
.prefixlen
= IPV6_MAX_BITLEN
;
422 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
424 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
426 if (c
->address
&& (c
->address
->family
== AF_INET
) &&
427 prefix_match(CONNECTED_PREFIX(c
), &addr
) &&
428 (c
->address
->prefixlen
> bestlen
))
430 bestlen
= c
->address
->prefixlen
;
439 if_lookup_address (void *matchaddr
, int family
)
441 return if_lookup_address_vrf (matchaddr
, family
, VRF_DEFAULT
);
444 /* Lookup interface by prefix */
446 if_lookup_prefix_vrf (struct prefix
*prefix
, vrf_id_t vrf_id
)
448 struct listnode
*node
;
449 struct listnode
*cnode
;
450 struct interface
*ifp
;
453 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
455 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
457 if (prefix_cmp(c
->address
, prefix
) == 0)
467 if_lookup_prefix (struct prefix
*prefix
)
469 return if_lookup_prefix_vrf (prefix
, VRF_DEFAULT
);
472 /* Get interface by name if given name interface doesn't exist create
475 if_get_by_name_vrf (const char *name
, vrf_id_t vrf_id
)
477 struct interface
*ifp
;
479 return ((ifp
= if_lookup_by_name_vrf (name
, vrf_id
)) != NULL
) ? ifp
:
480 if_create_vrf (name
, strlen(name
), vrf_id
);
484 if_get_by_name (const char *name
)
486 return if_get_by_name_vrf (name
, VRF_DEFAULT
);
490 if_get_by_name_len_vrf (const char *name
, size_t namelen
, vrf_id_t vrf_id
, int vty
)
492 struct interface
*ifp
;
493 struct listnode
*node
;
494 struct vrf
*vrf
= NULL
;
497 ifp
= if_lookup_by_name_len_vrf (name
, namelen
, vrf_id
);
501 /* Didn't find the interface on that vrf. Defined on a different one? */
502 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
504 vrf
= vrf_iter2vrf(iter
);
505 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf
->vrf_id
), node
, ifp
))
507 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
509 /* Found a match. If the interface command was entered in vty without a
510 * VRF (passed as VRF_DEFAULT), accept the ifp we found. If a vrf was
511 * entered and there is a mismatch, reject it if from vty. If it came
512 * from the kernel by way of zclient, believe it and update
513 * the ifp accordingly.
517 if (vrf_id
== VRF_DEFAULT
)
523 if_update_vrf (ifp
, name
, namelen
, vrf_id
);
529 return (if_create_vrf (name
, namelen
, vrf_id
));
533 if_get_by_name_len (const char *name
, size_t namelen
)
535 return if_get_by_name_len_vrf (name
, namelen
, VRF_DEFAULT
, 0);
538 /* Does interface up ? */
540 if_is_up (struct interface
*ifp
)
542 return ifp
->flags
& IFF_UP
;
545 /* Is interface running? */
547 if_is_running (struct interface
*ifp
)
549 return ifp
->flags
& IFF_RUNNING
;
552 /* Is the interface operative, eg. either UP & RUNNING
553 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
554 if ptm checking is enabled, then ptm check has passed */
556 if_is_operative (struct interface
*ifp
)
558 return ((ifp
->flags
& IFF_UP
) &&
559 (((ifp
->flags
& IFF_RUNNING
) &&
560 (ifp
->ptm_status
|| !ifp
->ptm_enable
)) ||
561 !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
564 /* Is the interface operative, eg. either UP & RUNNING
565 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
567 if_is_no_ptm_operative (struct interface
*ifp
)
569 return ((ifp
->flags
& IFF_UP
) &&
570 ((ifp
->flags
& IFF_RUNNING
) ||
571 !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
574 /* Is this loopback interface ? */
576 if_is_loopback (struct interface
*ifp
)
578 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
579 * but Y on platform N?
581 return (ifp
->flags
& (IFF_LOOPBACK
|IFF_NOXMIT
|IFF_VIRTUAL
));
584 /* Does this interface support broadcast ? */
586 if_is_broadcast (struct interface
*ifp
)
588 return ifp
->flags
& IFF_BROADCAST
;
591 /* Does this interface support broadcast ? */
593 if_is_pointopoint (struct interface
*ifp
)
595 return ifp
->flags
& IFF_POINTOPOINT
;
598 /* Does this interface support multicast ? */
600 if_is_multicast (struct interface
*ifp
)
602 return ifp
->flags
& IFF_MULTICAST
;
605 /* Printout flag information into log */
607 if_flag_dump (unsigned long flag
)
610 static char logbuf
[BUFSIZ
];
612 #define IFF_OUT_LOG(X,STR) \
616 strlcat (logbuf, ",", BUFSIZ); \
619 strlcat (logbuf, STR, BUFSIZ); \
622 strlcpy (logbuf
, "<", BUFSIZ
);
623 IFF_OUT_LOG (IFF_UP
, "UP");
624 IFF_OUT_LOG (IFF_BROADCAST
, "BROADCAST");
625 IFF_OUT_LOG (IFF_DEBUG
, "DEBUG");
626 IFF_OUT_LOG (IFF_LOOPBACK
, "LOOPBACK");
627 IFF_OUT_LOG (IFF_POINTOPOINT
, "POINTOPOINT");
628 IFF_OUT_LOG (IFF_NOTRAILERS
, "NOTRAILERS");
629 IFF_OUT_LOG (IFF_RUNNING
, "RUNNING");
630 IFF_OUT_LOG (IFF_NOARP
, "NOARP");
631 IFF_OUT_LOG (IFF_PROMISC
, "PROMISC");
632 IFF_OUT_LOG (IFF_ALLMULTI
, "ALLMULTI");
633 IFF_OUT_LOG (IFF_OACTIVE
, "OACTIVE");
634 IFF_OUT_LOG (IFF_SIMPLEX
, "SIMPLEX");
635 IFF_OUT_LOG (IFF_LINK0
, "LINK0");
636 IFF_OUT_LOG (IFF_LINK1
, "LINK1");
637 IFF_OUT_LOG (IFF_LINK2
, "LINK2");
638 IFF_OUT_LOG (IFF_MULTICAST
, "MULTICAST");
639 IFF_OUT_LOG (IFF_NOXMIT
, "NOXMIT");
640 IFF_OUT_LOG (IFF_NORTEXCH
, "NORTEXCH");
641 IFF_OUT_LOG (IFF_VIRTUAL
, "VIRTUAL");
642 IFF_OUT_LOG (IFF_IPV4
, "IPv4");
643 IFF_OUT_LOG (IFF_IPV6
, "IPv6");
645 strlcat (logbuf
, ">", BUFSIZ
);
653 if_dump (const struct interface
*ifp
)
655 struct listnode
*node
;
656 struct connected
*c
__attribute__((unused
));
658 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, c
))
659 zlog_info ("Interface %s vrf %u index %d metric %d mtu %d "
661 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
, ifp
->mtu
,
662 ifp
->mtu6
, if_flag_dump (ifp
->flags
));
665 /* Interface printing for all interface. */
669 struct list
*intf_list
;
670 struct listnode
*node
;
674 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
675 if ((intf_list
= vrf_iter2iflist (iter
)) != NULL
)
676 for (ALL_LIST_ELEMENTS_RO (intf_list
, node
, p
))
680 DEFUN (interface_desc
,
682 "description LINE...",
683 "Interface specific description\n"
684 "Characters describing this interface\n")
687 VTY_DECLVAR_CONTEXT (interface
, ifp
);
690 XFREE (MTYPE_TMP
, ifp
->desc
);
691 ifp
->desc
= argv_concat(argv
, argc
, idx_line
);
696 DEFUN (no_interface_desc
,
697 no_interface_desc_cmd
,
700 "Interface specific description\n")
702 VTY_DECLVAR_CONTEXT (interface
, ifp
);
705 XFREE (MTYPE_TMP
, ifp
->desc
);
712 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
713 * a seperate struct interface for each logical interface, so config
714 * file may be full of 'interface fooX:Y'. Solaris however does not
715 * expose logical interfaces via PF_ROUTE, so trying to track logical
716 * interfaces can be fruitless, for that reason Quagga only tracks
717 * the primary IP interface.
719 * We try accomodate SUNWzebra by:
720 * - looking up the interface name, to see whether it exists, if so
722 * - for protocol daemons, this could only because zebra told us of
724 * - for zebra, only because it learnt from kernel
726 * - search the name to see if it contains a sub-ipif / logical interface
727 * seperator, the ':' char. If it does:
728 * - text up to that char must be the primary name - get that name.
730 * - no idea, just get the name in its entirety.
732 static struct interface
*
733 if_sunwzebra_get (const char *name
, size_t nlen
, vrf_id_t vrf_id
)
735 struct interface
*ifp
;
738 if ( (ifp
= if_lookup_by_name_len_vrf (name
, nlen
, vrf_id
)) != NULL
)
741 /* hunt the primary interface name... */
742 while (seppos
< nlen
&& name
[seppos
] != ':')
745 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
747 return if_get_by_name_len_vrf (name
, seppos
, vrf_id
, 1);
749 return if_get_by_name_len_vrf (name
, nlen
, vrf_id
, 1);
755 "interface IFNAME [vrf NAME]",
756 "Select an interface to configure\n"
762 const char *ifname
= argv
[idx_ifname
]->arg
;
763 const char *vrfname
= (argc
> 2) ? argv
[idx_vrf
]->arg
: NULL
;
765 struct interface
*ifp
;
767 vrf_id_t vrf_id
= VRF_DEFAULT
;
769 if ((sl
= strlen(ifname
)) > INTERFACE_NAMSIZ
)
771 vty_out (vty
, "%% Interface name %s is invalid: length exceeds "
773 ifname
, INTERFACE_NAMSIZ
, VTY_NEWLINE
);
777 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
778 Imagine forward reference of a vrf by name in this interface config */
780 VRF_GET_ID (vrf_id
, vrfname
);
783 ifp
= if_sunwzebra_get (ifname
, sl
, vrf_id
);
785 ifp
= if_get_by_name_len_vrf (ifname
, sl
, vrf_id
, 1);
790 vty_out (vty
, "%% interface %s not in %s%s", ifname
, vrfname
, VTY_NEWLINE
);
793 VTY_PUSH_CONTEXT_COMPAT (INTERFACE_NODE
, ifp
);
798 DEFUN_NOSH (no_interface
,
800 "no interface IFNAME [vrf NAME]",
802 "Delete a pseudo interface's configuration\n"
806 const char *ifname
= argv
[2]->arg
;
807 const char *vrfname
= (argc
> 3) ? argv
[3]->arg
: NULL
;
809 // deleting interface
810 struct interface
*ifp
;
811 vrf_id_t vrf_id
= VRF_DEFAULT
;
814 VRF_GET_ID (vrf_id
, vrfname
);
816 ifp
= if_lookup_by_name_vrf (ifname
, vrf_id
);
820 vty_out (vty
, "%% Interface %s does not exist%s", ifname
, VTY_NEWLINE
);
824 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
826 vty_out (vty
, "%% Only inactive interfaces can be deleted%s",
839 "Select a VRF to configure\n"
843 const char *vrfname
= argv
[idx_name
]->arg
;
848 if ((sl
= strlen(vrfname
)) > VRF_NAMSIZ
)
850 vty_out (vty
, "%% VRF name %s is invalid: length exceeds "
852 vrfname
, VRF_NAMSIZ
, VTY_NEWLINE
);
856 vrfp
= vrf_get (VRF_UNKNOWN
, vrfname
);
858 VTY_PUSH_CONTEXT_COMPAT (VRF_NODE
, vrfp
);
867 "Delete a pseudo VRF's configuration\n"
870 const char *vrfname
= argv
[2]->arg
;
874 vrfp
= vrf_list_lookup_by_name (vrfname
);;
878 vty_out (vty
, "%% VRF %s does not exist%s", vrfname
, VTY_NEWLINE
);
882 if (CHECK_FLAG (vrfp
->status
, VRF_ACTIVE
))
884 vty_out (vty
, "%% Only inactive VRFs can be deleted%s",
895 /* For debug purpose. */
898 "show address [vrf NAME]",
904 struct listnode
*node
;
905 struct listnode
*node2
;
906 struct interface
*ifp
;
907 struct connected
*ifc
;
909 vrf_id_t vrf_id
= VRF_DEFAULT
;
912 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
914 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
916 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
920 if (p
->family
== AF_INET
)
921 vty_out (vty
, "%s/%d%s", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
,
928 DEFUN (show_address_vrf_all
,
929 show_address_vrf_all_cmd
,
930 "show address vrf all",
933 VRF_ALL_CMD_HELP_STR
)
935 struct list
*intf_list
;
936 struct listnode
*node
;
937 struct listnode
*node2
;
938 struct interface
*ifp
;
939 struct connected
*ifc
;
943 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
945 intf_list
= vrf_iter2iflist (iter
);
946 if (!intf_list
|| !listcount (intf_list
))
949 vty_out (vty
, "%sVRF %u%s%s", VTY_NEWLINE
, vrf_iter2id (iter
),
950 VTY_NEWLINE
, VTY_NEWLINE
);
952 for (ALL_LIST_ELEMENTS_RO (intf_list
, node
, ifp
))
954 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
958 if (p
->family
== AF_INET
)
959 vty_out (vty
, "%s/%d%s", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
,
967 /* Allocate connected structure. */
971 return XCALLOC (MTYPE_CONNECTED
, sizeof (struct connected
));
974 /* Allocate nbr connected structure. */
975 struct nbr_connected
*
976 nbr_connected_new (void)
978 return XCALLOC (MTYPE_NBR_CONNECTED
, sizeof (struct nbr_connected
));
981 /* Free connected structure. */
983 connected_free (struct connected
*connected
)
985 if (connected
->address
)
986 prefix_free (connected
->address
);
988 if (connected
->destination
)
989 prefix_free (connected
->destination
);
991 if (connected
->label
)
992 XFREE (MTYPE_CONNECTED_LABEL
, connected
->label
);
994 XFREE (MTYPE_CONNECTED
, connected
);
997 /* Free nbr connected structure. */
999 nbr_connected_free (struct nbr_connected
*connected
)
1001 if (connected
->address
)
1002 prefix_free (connected
->address
);
1004 XFREE (MTYPE_NBR_CONNECTED
, connected
);
1007 /* If same interface nbr address already exists... */
1008 struct nbr_connected
*
1009 nbr_connected_check (struct interface
*ifp
, struct prefix
*p
)
1011 struct nbr_connected
*ifc
;
1012 struct listnode
*node
;
1014 for (ALL_LIST_ELEMENTS_RO (ifp
->nbr_connected
, node
, ifc
))
1015 if (prefix_same (ifc
->address
, p
))
1021 /* Print if_addr structure. */
1022 static void __attribute__ ((unused
))
1023 connected_log (struct connected
*connected
, char *str
)
1026 struct interface
*ifp
;
1027 char logbuf
[BUFSIZ
];
1030 ifp
= connected
->ifp
;
1031 p
= connected
->address
;
1033 snprintf (logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ",
1034 str
, ifp
->name
, ifp
->vrf_id
, prefix_family_str (p
),
1035 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1038 p
= connected
->destination
;
1041 strncat (logbuf
, inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1042 BUFSIZ
- strlen(logbuf
));
1044 zlog (NULL
, LOG_INFO
, "%s", logbuf
);
1047 /* Print if_addr structure. */
1048 static void __attribute__ ((unused
))
1049 nbr_connected_log (struct nbr_connected
*connected
, char *str
)
1052 struct interface
*ifp
;
1053 char logbuf
[BUFSIZ
];
1056 ifp
= connected
->ifp
;
1057 p
= connected
->address
;
1059 snprintf (logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ",
1060 str
, ifp
->name
, prefix_family_str (p
),
1061 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1064 zlog (NULL
, LOG_INFO
, "%s", logbuf
);
1067 /* If two connected address has same prefix return 1. */
1069 connected_same_prefix (struct prefix
*p1
, struct prefix
*p2
)
1071 if (p1
->family
== p2
->family
)
1073 if (p1
->family
== AF_INET
&&
1074 IPV4_ADDR_SAME (&p1
->u
.prefix4
, &p2
->u
.prefix4
))
1077 if (p1
->family
== AF_INET6
&&
1078 IPV6_ADDR_SAME (&p1
->u
.prefix6
, &p2
->u
.prefix6
))
1080 #endif /* HAVE_IPV6 */
1086 connected_lookup_prefix_exact (struct interface
*ifp
, struct prefix
*p
)
1088 struct listnode
*node
;
1089 struct listnode
*next
;
1090 struct connected
*ifc
;
1092 for (node
= listhead (ifp
->connected
); node
; node
= next
)
1094 ifc
= listgetdata (node
);
1097 if (connected_same_prefix (ifc
->address
, p
))
1104 connected_delete_by_prefix (struct interface
*ifp
, struct prefix
*p
)
1106 struct listnode
*node
;
1107 struct listnode
*next
;
1108 struct connected
*ifc
;
1110 /* In case of same prefix come, replace it with new one. */
1111 for (node
= listhead (ifp
->connected
); node
; node
= next
)
1113 ifc
= listgetdata (node
);
1116 if (connected_same_prefix (ifc
->address
, p
))
1118 listnode_delete (ifp
->connected
, ifc
);
1125 /* Find the address on our side that will be used when packets
1128 connected_lookup_prefix (struct interface
*ifp
, struct prefix
*addr
)
1130 struct listnode
*cnode
;
1131 struct connected
*c
;
1132 struct connected
*match
;
1136 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
1138 if (c
->address
&& (c
->address
->family
== addr
->family
) &&
1139 prefix_match(CONNECTED_PREFIX(c
), addr
) &&
1140 (!match
|| (c
->address
->prefixlen
> match
->address
->prefixlen
)))
1147 connected_add_by_prefix (struct interface
*ifp
, struct prefix
*p
,
1148 struct prefix
*destination
)
1150 struct connected
*ifc
;
1152 /* Allocate new connected address. */
1153 ifc
= connected_new ();
1156 /* Fetch interface address */
1157 ifc
->address
= prefix_new();
1158 memcpy (ifc
->address
, p
, sizeof(struct prefix
));
1160 /* Fetch dest address */
1163 ifc
->destination
= prefix_new();
1164 memcpy (ifc
->destination
, destination
, sizeof(struct prefix
));
1167 /* Add connected address to the interface. */
1168 listnode_add (ifp
->connected
, ifc
);
1172 #ifndef HAVE_IF_NAMETOINDEX
1174 if_nametoindex (const char *name
)
1176 struct interface
*ifp
;
1178 return ((ifp
= if_lookup_by_name_len(name
, strnlen(name
, IFNAMSIZ
))) != NULL
)
1183 #ifndef HAVE_IF_INDEXTONAME
1185 if_indextoname (ifindex_t ifindex
, char *name
)
1187 struct interface
*ifp
;
1189 if (!(ifp
= if_lookup_by_index(ifindex
)))
1191 strncpy (name
, ifp
->name
, IFNAMSIZ
);
1196 #if 0 /* this route_table of struct connected's is unused
1197 * however, it would be good to use a route_table rather than
1200 /* Interface looking up by interface's address. */
1201 /* Interface's IPv4 address reverse lookup table. */
1202 struct route_table
*ifaddr_ipv4_table
;
1203 /* struct route_table *ifaddr_ipv6_table; */
1206 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
1208 struct route_node
*rn
;
1209 struct prefix_ipv4 p
;
1212 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1215 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1218 route_unlock_node (rn
);
1219 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1220 inet_ntoa (*ifaddr
));
1227 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1229 struct route_node
*rn
;
1230 struct prefix_ipv4 p
;
1233 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1236 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1239 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1240 inet_ntoa (*ifaddr
));
1244 route_unlock_node (rn
);
1245 route_unlock_node (rn
);
1248 /* Lookup interface by interface's IP address or interface index. */
1249 static struct interface
*
1250 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1252 struct prefix_ipv4 p
;
1253 struct route_node
*rn
;
1254 struct interface
*ifp
;
1259 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1262 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1267 route_unlock_node (rn
);
1271 return if_lookup_by_index(ifindex
);
1273 #endif /* ifaddr_ipv4_table */
1275 /* Initialize interface list. */
1277 if_init (struct list
**intf_list
)
1279 *intf_list
= list_new ();
1281 ifaddr_ipv4_table
= route_table_init ();
1282 #endif /* ifaddr_ipv4_table */
1284 (*intf_list
)->cmp
= (int (*)(void *, void *))if_cmp_func
;
1288 if_terminate (struct list
**intf_list
)
1292 struct interface
*ifp
;
1294 ifp
= listnode_head (*intf_list
);
1300 ifp
->node
->info
= NULL
;
1301 route_unlock_node (ifp
->node
);
1307 list_delete (*intf_list
);
1312 if_link_type_str (enum zebra_link_type llt
)
1316 #define llts(T,S) case (T): return (S)
1317 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1318 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1319 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1320 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1321 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1322 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1323 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1324 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1325 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1326 llts(ZEBRA_LLT_ATM
, "ATM");
1327 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1328 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1329 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1330 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1331 llts(ZEBRA_LLT_SLIP
, "SLIP");
1332 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1333 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1334 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1335 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1336 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1337 llts(ZEBRA_LLT_PPP
, "PPP");
1338 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1339 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1340 llts(ZEBRA_LLT_LAPB
, "LAPB");
1341 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1342 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1343 llts(ZEBRA_LLT_FRAD
, "FRAD");
1344 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1345 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1346 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1347 llts(ZEBRA_LLT_FDDI
, "FDDI");
1348 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1349 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1350 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1351 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1352 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1353 llts(ZEBRA_LLT_IRDA
, "IrDA");
1354 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1355 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1356 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1357 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1358 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1359 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1360 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1361 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1362 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1364 zlog_warn ("Unknown value %d", llt
);
1365 return "Unknown type!";
1371 struct if_link_params
*
1372 if_link_params_get (struct interface
*ifp
)
1376 if (ifp
->link_params
!= NULL
)
1377 return ifp
->link_params
;
1379 struct if_link_params
*iflp
= XCALLOC(MTYPE_IF_LINK_PARAMS
,
1380 sizeof (struct if_link_params
));
1381 if (iflp
== NULL
) return NULL
;
1383 /* Set TE metric == standard metric */
1384 iflp
->te_metric
= ifp
->metric
;
1386 /* Compute default bandwidth based on interface */
1387 int bw
= (float)((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1388 * TE_KILO_BIT
/ TE_BYTE
);
1390 /* Set Max, Reservable and Unreserved Bandwidth */
1392 iflp
->max_rsv_bw
= bw
;
1393 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1394 iflp
->unrsv_bw
[i
] = bw
;
1396 /* Update Link parameters status */
1397 iflp
->lp_status
= LP_TE
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1399 /* Finally attach newly created Link Parameters */
1400 ifp
->link_params
= iflp
;
1406 if_link_params_free (struct interface
*ifp
)
1408 if (ifp
->link_params
== NULL
) return;
1409 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1410 ifp
->link_params
= NULL
;