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 (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
);
161 /* Create new interface structure. */
163 if_update_vrf (struct interface
*ifp
, const char *name
, int namelen
, vrf_id_t vrf_id
)
165 struct list
*intf_list
= vrf_iflist_get (vrf_id
);
167 /* remove interface from old master vrf list */
168 if (vrf_iflist (ifp
->vrf_id
))
169 listnode_delete (vrf_iflist (ifp
->vrf_id
), ifp
);
172 assert (namelen
<= INTERFACE_NAMSIZ
); /* Need space for '\0' at end. */
173 strncpy (ifp
->name
, name
, namelen
);
174 ifp
->name
[namelen
] = '\0';
175 ifp
->vrf_id
= vrf_id
;
176 if (if_lookup_by_name_vrf (ifp
->name
, vrf_id
) == NULL
)
177 listnode_add_sort (intf_list
, ifp
);
179 zlog_err("if_create(%s): corruption detected -- interface with this "
180 "name exists already in VRF %u!", ifp
->name
, vrf_id
);
186 /* Delete interface structure. */
188 if_delete_retain (struct interface
*ifp
)
190 if (if_master
.if_delete_hook
)
191 (*if_master
.if_delete_hook
) (ifp
);
195 /* Free connected address list */
196 list_delete_all_node (ifp
->connected
);
198 /* Free connected nbr address list */
199 list_delete_all_node (ifp
->nbr_connected
);
202 /* Delete and free interface structure. */
204 if_delete (struct interface
*ifp
)
206 listnode_delete (vrf_iflist (ifp
->vrf_id
), ifp
);
208 if_delete_retain(ifp
);
210 list_free (ifp
->connected
);
211 list_free (ifp
->nbr_connected
);
213 if_link_params_free (ifp
);
215 XFREE (MTYPE_IF
, ifp
);
218 /* Add hook to interface master. */
220 if_add_hook (int type
, int (*func
)(struct interface
*ifp
))
224 if_master
.if_new_hook
= func
;
227 if_master
.if_delete_hook
= func
;
234 /* Interface existance check by index. */
236 if_lookup_by_index (ifindex_t ifindex
, vrf_id_t vrf_id
)
238 struct listnode
*node
;
239 struct interface
*ifp
;
241 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
243 if (ifp
->ifindex
== ifindex
)
250 ifindex2ifname_vrf (ifindex_t ifindex
, vrf_id_t vrf_id
)
252 struct interface
*ifp
;
254 return ((ifp
= if_lookup_by_index (ifindex
, vrf_id
)) != NULL
) ?
255 ifp
->name
: "unknown";
259 ifindex2ifname (ifindex_t ifindex
)
261 return ifindex2ifname_vrf (ifindex
, VRF_DEFAULT
);
265 ifname2ifindex_vrf (const char *name
, vrf_id_t vrf_id
)
267 struct interface
*ifp
;
269 return ((ifp
= if_lookup_by_name_vrf (name
, vrf_id
)) != NULL
) ? ifp
->ifindex
274 ifname2ifindex (const char *name
)
276 return ifname2ifindex_vrf (name
, VRF_DEFAULT
);
279 /* Interface existance check by interface name. */
281 if_lookup_by_name_vrf (const char *name
, vrf_id_t vrf_id
)
283 struct listnode
*node
;
284 struct interface
*ifp
;
287 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
289 if (strcmp(name
, ifp
->name
) == 0)
296 if_lookup_by_name_all_vrf (const char *name
)
299 struct interface
*ifp
;
301 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
303 ifp
= if_lookup_by_name_vrf (name
, vrf
->vrf_id
);
312 if_lookup_by_name (const char *name
)
314 return if_lookup_by_name_vrf (name
, VRF_DEFAULT
);
318 if_lookup_by_name_len (const char *name
, size_t namelen
, vrf_id_t vrf_id
)
320 struct listnode
*node
;
321 struct interface
*ifp
;
323 if (namelen
> INTERFACE_NAMSIZ
)
326 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
328 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
334 /* Lookup interface by IPv4 address. */
336 if_lookup_exact_address (void *src
, int family
, vrf_id_t vrf_id
)
338 struct listnode
*node
;
339 struct listnode
*cnode
;
340 struct interface
*ifp
;
344 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
346 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
350 if (p
&& (p
->family
== family
))
352 if (family
== AF_INET
)
354 if (IPV4_ADDR_SAME (&p
->u
.prefix4
, (struct in_addr
*)src
))
357 else if (family
== AF_INET6
)
359 if (IPV6_ADDR_SAME (&p
->u
.prefix6
, (struct in6_addr
*)src
))
368 /* Lookup interface by IPv4 address. */
370 if_lookup_address (void *matchaddr
, int family
, vrf_id_t vrf_id
)
372 struct listnode
*node
;
375 struct listnode
*cnode
;
376 struct interface
*ifp
;
378 struct connected
*match
;
380 if (family
== AF_INET
)
382 addr
.family
= AF_INET
;
383 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
384 addr
.prefixlen
= IPV4_MAX_BITLEN
;
386 else if (family
== AF_INET6
)
388 addr
.family
= AF_INET6
;
389 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
390 addr
.prefixlen
= IPV6_MAX_BITLEN
;
395 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
397 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
399 if (c
->address
&& (c
->address
->family
== AF_INET
) &&
400 prefix_match(CONNECTED_PREFIX(c
), &addr
) &&
401 (c
->address
->prefixlen
> bestlen
))
403 bestlen
= c
->address
->prefixlen
;
411 /* Lookup interface by prefix */
413 if_lookup_prefix_vrf (struct prefix
*prefix
, vrf_id_t vrf_id
)
415 struct listnode
*node
;
416 struct listnode
*cnode
;
417 struct interface
*ifp
;
420 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
422 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
424 if (prefix_cmp(c
->address
, prefix
) == 0)
434 if_lookup_prefix (struct prefix
*prefix
)
436 return if_lookup_prefix_vrf (prefix
, VRF_DEFAULT
);
439 /* Get interface by name if given name interface doesn't exist create
442 if_get_by_name_vrf (const char *name
, vrf_id_t vrf_id
)
444 struct interface
*ifp
;
446 return ((ifp
= if_lookup_by_name_vrf (name
, vrf_id
)) != NULL
) ? ifp
:
447 if_create (name
, strlen(name
), vrf_id
);
451 if_get_by_name (const char *name
)
453 return if_get_by_name_vrf (name
, VRF_DEFAULT
);
457 if_get_by_name_len_vrf (const char *name
, size_t namelen
, vrf_id_t vrf_id
, int vty
)
459 struct interface
*ifp
;
461 struct listnode
*node
;
463 ifp
= if_lookup_by_name_len (name
, namelen
, vrf_id
);
467 /* Didn't find the interface on that vrf. Defined on a different one? */
468 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
470 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf
->vrf_id
), node
, ifp
))
472 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
474 /* Found a match. If the interface command was entered in vty without a
475 * VRF (passed as VRF_DEFAULT), accept the ifp we found. If a vrf was
476 * entered and there is a mismatch, reject it if from vty. If it came
477 * from the kernel by way of zclient, believe it and update
478 * the ifp accordingly.
482 if (vrf_id
== VRF_DEFAULT
)
488 if_update_vrf (ifp
, name
, namelen
, vrf_id
);
494 return (if_create (name
, namelen
, vrf_id
));
498 if_get_by_name_len (const char *name
, size_t namelen
)
500 return if_get_by_name_len_vrf (name
, namelen
, VRF_DEFAULT
, 0);
503 /* Does interface up ? */
505 if_is_up (struct interface
*ifp
)
507 return ifp
->flags
& IFF_UP
;
510 /* Is interface running? */
512 if_is_running (struct interface
*ifp
)
514 return ifp
->flags
& IFF_RUNNING
;
517 /* Is the interface operative, eg. either UP & RUNNING
518 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
519 if ptm checking is enabled, then ptm check has passed */
521 if_is_operative (struct interface
*ifp
)
523 return ((ifp
->flags
& IFF_UP
) &&
524 (((ifp
->flags
& IFF_RUNNING
) &&
525 (ifp
->ptm_status
|| !ifp
->ptm_enable
)) ||
526 !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
529 /* Is the interface operative, eg. either UP & RUNNING
530 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
532 if_is_no_ptm_operative (struct interface
*ifp
)
534 return ((ifp
->flags
& IFF_UP
) &&
535 ((ifp
->flags
& IFF_RUNNING
) ||
536 !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
539 /* Is this loopback interface ? */
541 if_is_loopback (struct interface
*ifp
)
543 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
544 * but Y on platform N?
546 return (ifp
->flags
& (IFF_LOOPBACK
|IFF_NOXMIT
|IFF_VIRTUAL
));
549 /* Does this interface support broadcast ? */
551 if_is_broadcast (struct interface
*ifp
)
553 return ifp
->flags
& IFF_BROADCAST
;
556 /* Does this interface support broadcast ? */
558 if_is_pointopoint (struct interface
*ifp
)
560 return ifp
->flags
& IFF_POINTOPOINT
;
563 /* Does this interface support multicast ? */
565 if_is_multicast (struct interface
*ifp
)
567 return ifp
->flags
& IFF_MULTICAST
;
570 /* Printout flag information into log */
572 if_flag_dump (unsigned long flag
)
575 static char logbuf
[BUFSIZ
];
577 #define IFF_OUT_LOG(X,STR) \
581 strlcat (logbuf, ",", BUFSIZ); \
584 strlcat (logbuf, STR, BUFSIZ); \
587 strlcpy (logbuf
, "<", BUFSIZ
);
588 IFF_OUT_LOG (IFF_UP
, "UP");
589 IFF_OUT_LOG (IFF_BROADCAST
, "BROADCAST");
590 IFF_OUT_LOG (IFF_DEBUG
, "DEBUG");
591 IFF_OUT_LOG (IFF_LOOPBACK
, "LOOPBACK");
592 IFF_OUT_LOG (IFF_POINTOPOINT
, "POINTOPOINT");
593 IFF_OUT_LOG (IFF_NOTRAILERS
, "NOTRAILERS");
594 IFF_OUT_LOG (IFF_RUNNING
, "RUNNING");
595 IFF_OUT_LOG (IFF_NOARP
, "NOARP");
596 IFF_OUT_LOG (IFF_PROMISC
, "PROMISC");
597 IFF_OUT_LOG (IFF_ALLMULTI
, "ALLMULTI");
598 IFF_OUT_LOG (IFF_OACTIVE
, "OACTIVE");
599 IFF_OUT_LOG (IFF_SIMPLEX
, "SIMPLEX");
600 IFF_OUT_LOG (IFF_LINK0
, "LINK0");
601 IFF_OUT_LOG (IFF_LINK1
, "LINK1");
602 IFF_OUT_LOG (IFF_LINK2
, "LINK2");
603 IFF_OUT_LOG (IFF_MULTICAST
, "MULTICAST");
604 IFF_OUT_LOG (IFF_NOXMIT
, "NOXMIT");
605 IFF_OUT_LOG (IFF_NORTEXCH
, "NORTEXCH");
606 IFF_OUT_LOG (IFF_VIRTUAL
, "VIRTUAL");
607 IFF_OUT_LOG (IFF_IPV4
, "IPv4");
608 IFF_OUT_LOG (IFF_IPV6
, "IPv6");
610 strlcat (logbuf
, ">", BUFSIZ
);
618 if_dump (const struct interface
*ifp
)
620 struct listnode
*node
;
621 struct connected
*c
__attribute__((unused
));
623 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, c
))
624 zlog_info ("Interface %s vrf %u index %d metric %d mtu %d "
626 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
, ifp
->mtu
,
627 ifp
->mtu6
, if_flag_dump (ifp
->flags
));
630 /* Interface printing for all interface. */
635 struct listnode
*node
;
638 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
639 if (vrf
->iflist
!= NULL
)
640 for (ALL_LIST_ELEMENTS_RO (vrf
->iflist
, node
, p
))
644 DEFUN (interface_desc
,
646 "description LINE...",
647 "Interface specific description\n"
648 "Characters describing this interface\n")
651 VTY_DECLVAR_CONTEXT (interface
, ifp
);
654 XFREE (MTYPE_TMP
, ifp
->desc
);
655 ifp
->desc
= argv_concat(argv
, argc
, idx_line
);
660 DEFUN (no_interface_desc
,
661 no_interface_desc_cmd
,
664 "Interface specific description\n")
666 VTY_DECLVAR_CONTEXT (interface
, ifp
);
669 XFREE (MTYPE_TMP
, ifp
->desc
);
676 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
677 * a seperate struct interface for each logical interface, so config
678 * file may be full of 'interface fooX:Y'. Solaris however does not
679 * expose logical interfaces via PF_ROUTE, so trying to track logical
680 * interfaces can be fruitless, for that reason Quagga only tracks
681 * the primary IP interface.
683 * We try accomodate SUNWzebra by:
684 * - looking up the interface name, to see whether it exists, if so
686 * - for protocol daemons, this could only because zebra told us of
688 * - for zebra, only because it learnt from kernel
690 * - search the name to see if it contains a sub-ipif / logical interface
691 * seperator, the ':' char. If it does:
692 * - text up to that char must be the primary name - get that name.
694 * - no idea, just get the name in its entirety.
696 static struct interface
*
697 if_sunwzebra_get (const char *name
, size_t nlen
, vrf_id_t vrf_id
)
699 struct interface
*ifp
;
702 if ( (ifp
= if_lookup_by_name_len (name
, nlen
, vrf_id
)) != NULL
)
705 /* hunt the primary interface name... */
706 while (seppos
< nlen
&& name
[seppos
] != ':')
709 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
711 return if_get_by_name_len_vrf (name
, seppos
, vrf_id
, 1);
713 return if_get_by_name_len_vrf (name
, nlen
, vrf_id
, 1);
719 "interface IFNAME [vrf NAME]",
720 "Select an interface to configure\n"
726 const char *ifname
= argv
[idx_ifname
]->arg
;
727 const char *vrfname
= (argc
> 2) ? argv
[idx_vrf
]->arg
: NULL
;
729 struct interface
*ifp
;
731 vrf_id_t vrf_id
= VRF_DEFAULT
;
733 if ((sl
= strlen(ifname
)) > INTERFACE_NAMSIZ
)
735 vty_out (vty
, "%% Interface name %s is invalid: length exceeds "
737 ifname
, INTERFACE_NAMSIZ
, VTY_NEWLINE
);
741 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
742 Imagine forward reference of a vrf by name in this interface config */
744 VRF_GET_ID (vrf_id
, vrfname
);
747 ifp
= if_sunwzebra_get (ifname
, sl
, vrf_id
);
749 ifp
= if_get_by_name_len_vrf (ifname
, sl
, vrf_id
, 1);
754 vty_out (vty
, "%% interface %s not in %s%s", ifname
, vrfname
, VTY_NEWLINE
);
757 VTY_PUSH_CONTEXT (INTERFACE_NODE
, ifp
);
762 DEFUN_NOSH (no_interface
,
764 "no interface IFNAME [vrf NAME]",
766 "Delete a pseudo interface's configuration\n"
770 const char *ifname
= argv
[2]->arg
;
771 const char *vrfname
= (argc
> 3) ? argv
[3]->arg
: NULL
;
773 // deleting interface
774 struct interface
*ifp
;
775 vrf_id_t vrf_id
= VRF_DEFAULT
;
778 VRF_GET_ID (vrf_id
, vrfname
);
780 ifp
= if_lookup_by_name_vrf (ifname
, vrf_id
);
784 vty_out (vty
, "%% Interface %s does not exist%s", ifname
, VTY_NEWLINE
);
788 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
790 vty_out (vty
, "%% Only inactive interfaces can be deleted%s",
803 install_element (CONFIG_NODE
, &interface_cmd
);
804 install_element (CONFIG_NODE
, &no_interface_cmd
);
806 install_default (INTERFACE_NODE
);
807 install_element (INTERFACE_NODE
, &interface_desc_cmd
);
808 install_element (INTERFACE_NODE
, &no_interface_desc_cmd
);
812 /* For debug purpose. */
815 "show address [vrf NAME]",
821 struct listnode
*node
;
822 struct listnode
*node2
;
823 struct interface
*ifp
;
824 struct connected
*ifc
;
826 vrf_id_t vrf_id
= VRF_DEFAULT
;
829 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
831 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
833 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
837 if (p
->family
== AF_INET
)
838 vty_out (vty
, "%s/%d%s", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
,
845 DEFUN (show_address_vrf_all
,
846 show_address_vrf_all_cmd
,
847 "show address vrf all",
850 VRF_ALL_CMD_HELP_STR
)
853 struct listnode
*node
;
854 struct listnode
*node2
;
855 struct interface
*ifp
;
856 struct connected
*ifc
;
859 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
861 if (!vrf
->iflist
|| !listcount (vrf
->iflist
))
864 vty_out (vty
, "%sVRF %u%s%s", VTY_NEWLINE
, vrf
->vrf_id
, VTY_NEWLINE
,
867 for (ALL_LIST_ELEMENTS_RO (vrf
->iflist
, node
, ifp
))
869 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
873 if (p
->family
== AF_INET
)
874 vty_out (vty
, "%s/%d%s", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
,
883 /* Allocate connected structure. */
887 return XCALLOC (MTYPE_CONNECTED
, sizeof (struct connected
));
890 /* Allocate nbr connected structure. */
891 struct nbr_connected
*
892 nbr_connected_new (void)
894 return XCALLOC (MTYPE_NBR_CONNECTED
, sizeof (struct nbr_connected
));
897 /* Free connected structure. */
899 connected_free (struct connected
*connected
)
901 if (connected
->address
)
902 prefix_free (connected
->address
);
904 if (connected
->destination
)
905 prefix_free (connected
->destination
);
907 if (connected
->label
)
908 XFREE (MTYPE_CONNECTED_LABEL
, connected
->label
);
910 XFREE (MTYPE_CONNECTED
, connected
);
913 /* Free nbr connected structure. */
915 nbr_connected_free (struct nbr_connected
*connected
)
917 if (connected
->address
)
918 prefix_free (connected
->address
);
920 XFREE (MTYPE_NBR_CONNECTED
, connected
);
923 /* If same interface nbr address already exists... */
924 struct nbr_connected
*
925 nbr_connected_check (struct interface
*ifp
, struct prefix
*p
)
927 struct nbr_connected
*ifc
;
928 struct listnode
*node
;
930 for (ALL_LIST_ELEMENTS_RO (ifp
->nbr_connected
, node
, ifc
))
931 if (prefix_same (ifc
->address
, p
))
937 /* Print if_addr structure. */
938 static void __attribute__ ((unused
))
939 connected_log (struct connected
*connected
, char *str
)
942 struct interface
*ifp
;
946 ifp
= connected
->ifp
;
947 p
= connected
->address
;
949 snprintf (logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ",
950 str
, ifp
->name
, ifp
->vrf_id
, prefix_family_str (p
),
951 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
954 p
= connected
->destination
;
957 strncat (logbuf
, inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
958 BUFSIZ
- strlen(logbuf
));
960 zlog_info("%s", logbuf
);
963 /* Print if_addr structure. */
964 static void __attribute__ ((unused
))
965 nbr_connected_log (struct nbr_connected
*connected
, char *str
)
968 struct interface
*ifp
;
972 ifp
= connected
->ifp
;
973 p
= connected
->address
;
975 snprintf (logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ",
976 str
, ifp
->name
, prefix_family_str (p
),
977 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
980 zlog_info("%s", logbuf
);
983 /* If two connected address has same prefix return 1. */
985 connected_same_prefix (struct prefix
*p1
, struct prefix
*p2
)
987 if (p1
->family
== p2
->family
)
989 if (p1
->family
== AF_INET
&&
990 IPV4_ADDR_SAME (&p1
->u
.prefix4
, &p2
->u
.prefix4
))
992 if (p1
->family
== AF_INET6
&&
993 IPV6_ADDR_SAME (&p1
->u
.prefix6
, &p2
->u
.prefix6
))
1000 connected_lookup_prefix_exact (struct interface
*ifp
, struct prefix
*p
)
1002 struct listnode
*node
;
1003 struct listnode
*next
;
1004 struct connected
*ifc
;
1006 for (node
= listhead (ifp
->connected
); node
; node
= next
)
1008 ifc
= listgetdata (node
);
1011 if (connected_same_prefix (ifc
->address
, p
))
1018 connected_delete_by_prefix (struct interface
*ifp
, struct prefix
*p
)
1020 struct listnode
*node
;
1021 struct listnode
*next
;
1022 struct connected
*ifc
;
1024 /* In case of same prefix come, replace it with new one. */
1025 for (node
= listhead (ifp
->connected
); node
; node
= next
)
1027 ifc
= listgetdata (node
);
1030 if (connected_same_prefix (ifc
->address
, p
))
1032 listnode_delete (ifp
->connected
, ifc
);
1039 /* Find the address on our side that will be used when packets
1042 connected_lookup_prefix (struct interface
*ifp
, struct prefix
*addr
)
1044 struct listnode
*cnode
;
1045 struct connected
*c
;
1046 struct connected
*match
;
1050 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
1052 if (c
->address
&& (c
->address
->family
== addr
->family
) &&
1053 prefix_match(CONNECTED_PREFIX(c
), addr
) &&
1054 (!match
|| (c
->address
->prefixlen
> match
->address
->prefixlen
)))
1061 connected_add_by_prefix (struct interface
*ifp
, struct prefix
*p
,
1062 struct prefix
*destination
)
1064 struct connected
*ifc
;
1066 /* Allocate new connected address. */
1067 ifc
= connected_new ();
1070 /* Fetch interface address */
1071 ifc
->address
= prefix_new();
1072 memcpy (ifc
->address
, p
, sizeof(struct prefix
));
1074 /* Fetch dest address */
1077 ifc
->destination
= prefix_new();
1078 memcpy (ifc
->destination
, destination
, sizeof(struct prefix
));
1081 /* Add connected address to the interface. */
1082 listnode_add (ifp
->connected
, ifc
);
1086 #if 0 /* this route_table of struct connected's is unused
1087 * however, it would be good to use a route_table rather than
1090 /* Interface looking up by interface's address. */
1091 /* Interface's IPv4 address reverse lookup table. */
1092 struct route_table
*ifaddr_ipv4_table
;
1093 /* struct route_table *ifaddr_ipv6_table; */
1096 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
1098 struct route_node
*rn
;
1099 struct prefix_ipv4 p
;
1102 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1105 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1108 route_unlock_node (rn
);
1109 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1110 inet_ntoa (*ifaddr
));
1117 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1119 struct route_node
*rn
;
1120 struct prefix_ipv4 p
;
1123 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1126 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1129 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1130 inet_ntoa (*ifaddr
));
1134 route_unlock_node (rn
);
1135 route_unlock_node (rn
);
1138 /* Lookup interface by interface's IP address or interface index. */
1139 static struct interface
*
1140 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1142 struct prefix_ipv4 p
;
1143 struct route_node
*rn
;
1144 struct interface
*ifp
;
1149 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1152 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1157 route_unlock_node (rn
);
1161 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1163 #endif /* ifaddr_ipv4_table */
1165 /* Initialize interface list. */
1167 if_init (struct list
**intf_list
)
1169 *intf_list
= list_new ();
1171 ifaddr_ipv4_table
= route_table_init ();
1172 #endif /* ifaddr_ipv4_table */
1174 (*intf_list
)->cmp
= (int (*)(void *, void *))if_cmp_func
;
1178 if_terminate (struct list
**intf_list
)
1182 struct interface
*ifp
;
1184 ifp
= listnode_head (*intf_list
);
1190 ifp
->node
->info
= NULL
;
1191 route_unlock_node (ifp
->node
);
1197 list_delete (*intf_list
);
1202 if_link_type_str (enum zebra_link_type llt
)
1206 #define llts(T,S) case (T): return (S)
1207 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1208 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1209 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1210 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1211 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1212 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1213 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1214 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1215 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1216 llts(ZEBRA_LLT_ATM
, "ATM");
1217 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1218 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1219 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1220 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1221 llts(ZEBRA_LLT_SLIP
, "SLIP");
1222 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1223 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1224 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1225 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1226 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1227 llts(ZEBRA_LLT_PPP
, "PPP");
1228 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1229 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1230 llts(ZEBRA_LLT_LAPB
, "LAPB");
1231 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1232 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1233 llts(ZEBRA_LLT_FRAD
, "FRAD");
1234 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1235 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1236 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1237 llts(ZEBRA_LLT_FDDI
, "FDDI");
1238 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1239 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1240 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1241 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1242 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1243 llts(ZEBRA_LLT_IRDA
, "IrDA");
1244 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1245 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1246 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1247 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1248 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1249 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1250 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1251 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1252 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1254 zlog_warn ("Unknown value %d", llt
);
1255 return "Unknown type!";
1261 struct if_link_params
*
1262 if_link_params_get (struct interface
*ifp
)
1266 if (ifp
->link_params
!= NULL
)
1267 return ifp
->link_params
;
1269 struct if_link_params
*iflp
= XCALLOC(MTYPE_IF_LINK_PARAMS
,
1270 sizeof (struct if_link_params
));
1271 if (iflp
== NULL
) return NULL
;
1273 /* Set TE metric equal to standard metric */
1274 iflp
->te_metric
= ifp
->metric
;
1276 /* Compute default bandwidth based on interface */
1277 iflp
->default_bw
= ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1278 * TE_KILO_BIT
/ TE_BYTE
);
1280 /* Set Max, Reservable and Unreserved Bandwidth */
1281 iflp
->max_bw
= iflp
->default_bw
;
1282 iflp
->max_rsv_bw
= iflp
->default_bw
;
1283 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1284 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1286 /* Update Link parameters status */
1287 iflp
->lp_status
= LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1289 /* Finally attach newly created Link Parameters */
1290 ifp
->link_params
= iflp
;
1296 if_link_params_free (struct interface
*ifp
)
1298 if (ifp
->link_params
== NULL
) return;
1299 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1300 ifp
->link_params
= NULL
;