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 (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 (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 (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 (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 ifname2ifindex (const char *name
, vrf_id_t vrf_id
)
261 struct interface
*ifp
;
263 return ((ifp
= if_lookup_by_name (name
, vrf_id
)) != NULL
) ? ifp
->ifindex
267 /* Interface existance check by interface name. */
269 if_lookup_by_name (const char *name
, vrf_id_t vrf_id
)
271 struct listnode
*node
;
272 struct interface
*ifp
;
275 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
277 if (strcmp(name
, ifp
->name
) == 0)
284 if_lookup_by_name_all_vrf (const char *name
)
287 struct interface
*ifp
;
289 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
291 ifp
= if_lookup_by_name (name
, vrf
->vrf_id
);
300 if_lookup_by_name_len (const char *name
, size_t namelen
, vrf_id_t vrf_id
)
302 struct listnode
*node
;
303 struct interface
*ifp
;
305 if (namelen
> INTERFACE_NAMSIZ
)
308 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
310 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
316 /* Lookup interface by IPv4 address. */
318 if_lookup_exact_address (void *src
, int family
, vrf_id_t vrf_id
)
320 struct listnode
*node
;
321 struct listnode
*cnode
;
322 struct interface
*ifp
;
326 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
328 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
332 if (p
&& (p
->family
== family
))
334 if (family
== AF_INET
)
336 if (IPV4_ADDR_SAME (&p
->u
.prefix4
, (struct in_addr
*)src
))
339 else if (family
== AF_INET6
)
341 if (IPV6_ADDR_SAME (&p
->u
.prefix6
, (struct in6_addr
*)src
))
350 /* Lookup interface by IPv4 address. */
352 if_lookup_address (void *matchaddr
, int family
, vrf_id_t vrf_id
)
354 struct listnode
*node
;
357 struct listnode
*cnode
;
358 struct interface
*ifp
;
360 struct connected
*match
;
362 if (family
== AF_INET
)
364 addr
.family
= AF_INET
;
365 addr
.u
.prefix4
= *((struct in_addr
*)matchaddr
);
366 addr
.prefixlen
= IPV4_MAX_BITLEN
;
368 else if (family
== AF_INET6
)
370 addr
.family
= AF_INET6
;
371 addr
.u
.prefix6
= *((struct in6_addr
*)matchaddr
);
372 addr
.prefixlen
= IPV6_MAX_BITLEN
;
377 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
379 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
381 if (c
->address
&& (c
->address
->family
== AF_INET
) &&
382 prefix_match(CONNECTED_PREFIX(c
), &addr
) &&
383 (c
->address
->prefixlen
> bestlen
))
385 bestlen
= c
->address
->prefixlen
;
393 /* Lookup interface by prefix */
395 if_lookup_prefix (struct prefix
*prefix
, vrf_id_t vrf_id
)
397 struct listnode
*node
;
398 struct listnode
*cnode
;
399 struct interface
*ifp
;
402 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
404 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
406 if (prefix_cmp(c
->address
, prefix
) == 0)
415 /* Get interface by name if given name interface doesn't exist create
418 if_get_by_name (const char *name
, vrf_id_t vrf_id
)
420 struct interface
*ifp
;
422 return ((ifp
= if_lookup_by_name (name
, vrf_id
)) != NULL
) ? ifp
:
423 if_create (name
, strlen(name
), vrf_id
);
427 if_get_by_name_len (const char *name
, size_t namelen
, vrf_id_t vrf_id
, int vty
)
429 struct interface
*ifp
;
431 struct listnode
*node
;
433 ifp
= if_lookup_by_name_len (name
, namelen
, vrf_id
);
437 /* Didn't find the interface on that vrf. Defined on a different one? */
438 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
440 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf
->vrf_id
), node
, ifp
))
442 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
444 /* Found a match. If the interface command was entered in vty without a
445 * VRF (passed as VRF_DEFAULT), accept the ifp we found. If a vrf was
446 * entered and there is a mismatch, reject it if from vty. If it came
447 * from the kernel by way of zclient, believe it and update
448 * the ifp accordingly.
452 if (vrf_id
== VRF_DEFAULT
)
458 if_update (ifp
, name
, namelen
, vrf_id
);
464 return (if_create (name
, namelen
, vrf_id
));
467 /* Does interface up ? */
469 if_is_up (struct interface
*ifp
)
471 return ifp
->flags
& IFF_UP
;
474 /* Is interface running? */
476 if_is_running (struct interface
*ifp
)
478 return ifp
->flags
& IFF_RUNNING
;
481 /* Is the interface operative, eg. either UP & RUNNING
482 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
483 if ptm checking is enabled, then ptm check has passed */
485 if_is_operative (struct interface
*ifp
)
487 return ((ifp
->flags
& IFF_UP
) &&
488 (((ifp
->flags
& IFF_RUNNING
) &&
489 (ifp
->ptm_status
|| !ifp
->ptm_enable
)) ||
490 !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
493 /* Is the interface operative, eg. either UP & RUNNING
494 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
496 if_is_no_ptm_operative (struct interface
*ifp
)
498 return ((ifp
->flags
& IFF_UP
) &&
499 ((ifp
->flags
& IFF_RUNNING
) ||
500 !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
503 /* Is this loopback interface ? */
505 if_is_loopback (struct interface
*ifp
)
507 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
508 * but Y on platform N?
510 return (ifp
->flags
& (IFF_LOOPBACK
|IFF_NOXMIT
|IFF_VIRTUAL
));
513 /* Does this interface support broadcast ? */
515 if_is_broadcast (struct interface
*ifp
)
517 return ifp
->flags
& IFF_BROADCAST
;
520 /* Does this interface support broadcast ? */
522 if_is_pointopoint (struct interface
*ifp
)
524 return ifp
->flags
& IFF_POINTOPOINT
;
527 /* Does this interface support multicast ? */
529 if_is_multicast (struct interface
*ifp
)
531 return ifp
->flags
& IFF_MULTICAST
;
534 /* Printout flag information into log */
536 if_flag_dump (unsigned long flag
)
539 static char logbuf
[BUFSIZ
];
541 #define IFF_OUT_LOG(X,STR) \
545 strlcat (logbuf, ",", BUFSIZ); \
548 strlcat (logbuf, STR, BUFSIZ); \
551 strlcpy (logbuf
, "<", BUFSIZ
);
552 IFF_OUT_LOG (IFF_UP
, "UP");
553 IFF_OUT_LOG (IFF_BROADCAST
, "BROADCAST");
554 IFF_OUT_LOG (IFF_DEBUG
, "DEBUG");
555 IFF_OUT_LOG (IFF_LOOPBACK
, "LOOPBACK");
556 IFF_OUT_LOG (IFF_POINTOPOINT
, "POINTOPOINT");
557 IFF_OUT_LOG (IFF_NOTRAILERS
, "NOTRAILERS");
558 IFF_OUT_LOG (IFF_RUNNING
, "RUNNING");
559 IFF_OUT_LOG (IFF_NOARP
, "NOARP");
560 IFF_OUT_LOG (IFF_PROMISC
, "PROMISC");
561 IFF_OUT_LOG (IFF_ALLMULTI
, "ALLMULTI");
562 IFF_OUT_LOG (IFF_OACTIVE
, "OACTIVE");
563 IFF_OUT_LOG (IFF_SIMPLEX
, "SIMPLEX");
564 IFF_OUT_LOG (IFF_LINK0
, "LINK0");
565 IFF_OUT_LOG (IFF_LINK1
, "LINK1");
566 IFF_OUT_LOG (IFF_LINK2
, "LINK2");
567 IFF_OUT_LOG (IFF_MULTICAST
, "MULTICAST");
568 IFF_OUT_LOG (IFF_NOXMIT
, "NOXMIT");
569 IFF_OUT_LOG (IFF_NORTEXCH
, "NORTEXCH");
570 IFF_OUT_LOG (IFF_VIRTUAL
, "VIRTUAL");
571 IFF_OUT_LOG (IFF_IPV4
, "IPv4");
572 IFF_OUT_LOG (IFF_IPV6
, "IPv6");
574 strlcat (logbuf
, ">", BUFSIZ
);
582 if_dump (const struct interface
*ifp
)
584 struct listnode
*node
;
585 struct connected
*c
__attribute__((unused
));
587 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, c
))
588 zlog_info ("Interface %s vrf %u index %d metric %d mtu %d "
590 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
, ifp
->metric
, ifp
->mtu
,
591 ifp
->mtu6
, if_flag_dump (ifp
->flags
));
594 /* Interface printing for all interface. */
599 struct listnode
*node
;
602 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
603 if (vrf
->iflist
!= NULL
)
604 for (ALL_LIST_ELEMENTS_RO (vrf
->iflist
, node
, p
))
608 DEFUN (interface_desc
,
610 "description LINE...",
611 "Interface specific description\n"
612 "Characters describing this interface\n")
615 VTY_DECLVAR_CONTEXT (interface
, ifp
);
618 XFREE (MTYPE_TMP
, ifp
->desc
);
619 ifp
->desc
= argv_concat(argv
, argc
, idx_line
);
624 DEFUN (no_interface_desc
,
625 no_interface_desc_cmd
,
628 "Interface specific description\n")
630 VTY_DECLVAR_CONTEXT (interface
, ifp
);
633 XFREE (MTYPE_TMP
, ifp
->desc
);
640 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
641 * a seperate struct interface for each logical interface, so config
642 * file may be full of 'interface fooX:Y'. Solaris however does not
643 * expose logical interfaces via PF_ROUTE, so trying to track logical
644 * interfaces can be fruitless, for that reason Quagga only tracks
645 * the primary IP interface.
647 * We try accomodate SUNWzebra by:
648 * - looking up the interface name, to see whether it exists, if so
650 * - for protocol daemons, this could only because zebra told us of
652 * - for zebra, only because it learnt from kernel
654 * - search the name to see if it contains a sub-ipif / logical interface
655 * seperator, the ':' char. If it does:
656 * - text up to that char must be the primary name - get that name.
658 * - no idea, just get the name in its entirety.
660 static struct interface
*
661 if_sunwzebra_get (const char *name
, size_t nlen
, vrf_id_t vrf_id
)
663 struct interface
*ifp
;
666 if ( (ifp
= if_lookup_by_name_len (name
, nlen
, vrf_id
)) != NULL
)
669 /* hunt the primary interface name... */
670 while (seppos
< nlen
&& name
[seppos
] != ':')
673 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
675 return if_get_by_name_len (name
, seppos
, vrf_id
, 1);
677 return if_get_by_name_len (name
, nlen
, vrf_id
, 1);
683 "interface IFNAME [vrf NAME]",
684 "Select an interface to configure\n"
690 const char *ifname
= argv
[idx_ifname
]->arg
;
691 const char *vrfname
= (argc
> 2) ? argv
[idx_vrf
]->arg
: NULL
;
693 struct interface
*ifp
;
695 vrf_id_t vrf_id
= VRF_DEFAULT
;
697 if ((sl
= strlen(ifname
)) > INTERFACE_NAMSIZ
)
699 vty_out (vty
, "%% Interface name %s is invalid: length exceeds "
701 ifname
, INTERFACE_NAMSIZ
, VTY_NEWLINE
);
705 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
706 Imagine forward reference of a vrf by name in this interface config */
708 VRF_GET_ID (vrf_id
, vrfname
);
711 ifp
= if_sunwzebra_get (ifname
, sl
, vrf_id
);
713 ifp
= if_get_by_name_len (ifname
, sl
, vrf_id
, 1);
718 vty_out (vty
, "%% interface %s not in %s%s", ifname
, vrfname
, VTY_NEWLINE
);
721 VTY_PUSH_CONTEXT (INTERFACE_NODE
, ifp
);
726 DEFUN_NOSH (no_interface
,
728 "no interface IFNAME [vrf NAME]",
730 "Delete a pseudo interface's configuration\n"
734 const char *ifname
= argv
[2]->arg
;
735 const char *vrfname
= (argc
> 3) ? argv
[3]->arg
: NULL
;
737 // deleting interface
738 struct interface
*ifp
;
739 vrf_id_t vrf_id
= VRF_DEFAULT
;
742 VRF_GET_ID (vrf_id
, vrfname
);
744 ifp
= if_lookup_by_name (ifname
, vrf_id
);
748 vty_out (vty
, "%% Interface %s does not exist%s", ifname
, VTY_NEWLINE
);
752 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
754 vty_out (vty
, "%% Only inactive interfaces can be deleted%s",
767 install_element (CONFIG_NODE
, &interface_cmd
);
768 install_element (CONFIG_NODE
, &no_interface_cmd
);
770 install_default (INTERFACE_NODE
);
771 install_element (INTERFACE_NODE
, &interface_desc_cmd
);
772 install_element (INTERFACE_NODE
, &no_interface_desc_cmd
);
776 /* For debug purpose. */
779 "show address [vrf NAME]",
785 struct listnode
*node
;
786 struct listnode
*node2
;
787 struct interface
*ifp
;
788 struct connected
*ifc
;
790 vrf_id_t vrf_id
= VRF_DEFAULT
;
793 VRF_GET_ID (vrf_id
, argv
[idx_vrf
]->arg
);
795 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), node
, ifp
))
797 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
801 if (p
->family
== AF_INET
)
802 vty_out (vty
, "%s/%d%s", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
,
809 DEFUN (show_address_vrf_all
,
810 show_address_vrf_all_cmd
,
811 "show address vrf all",
814 VRF_ALL_CMD_HELP_STR
)
817 struct listnode
*node
;
818 struct listnode
*node2
;
819 struct interface
*ifp
;
820 struct connected
*ifc
;
823 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
825 if (!vrf
->iflist
|| !listcount (vrf
->iflist
))
828 vty_out (vty
, "%sVRF %u%s%s", VTY_NEWLINE
, vrf
->vrf_id
, VTY_NEWLINE
,
831 for (ALL_LIST_ELEMENTS_RO (vrf
->iflist
, 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
,
847 /* Allocate connected structure. */
851 return XCALLOC (MTYPE_CONNECTED
, sizeof (struct connected
));
854 /* Allocate nbr connected structure. */
855 struct nbr_connected
*
856 nbr_connected_new (void)
858 return XCALLOC (MTYPE_NBR_CONNECTED
, sizeof (struct nbr_connected
));
861 /* Free connected structure. */
863 connected_free (struct connected
*connected
)
865 if (connected
->address
)
866 prefix_free (connected
->address
);
868 if (connected
->destination
)
869 prefix_free (connected
->destination
);
871 if (connected
->label
)
872 XFREE (MTYPE_CONNECTED_LABEL
, connected
->label
);
874 XFREE (MTYPE_CONNECTED
, connected
);
877 /* Free nbr connected structure. */
879 nbr_connected_free (struct nbr_connected
*connected
)
881 if (connected
->address
)
882 prefix_free (connected
->address
);
884 XFREE (MTYPE_NBR_CONNECTED
, connected
);
887 /* If same interface nbr address already exists... */
888 struct nbr_connected
*
889 nbr_connected_check (struct interface
*ifp
, struct prefix
*p
)
891 struct nbr_connected
*ifc
;
892 struct listnode
*node
;
894 for (ALL_LIST_ELEMENTS_RO (ifp
->nbr_connected
, node
, ifc
))
895 if (prefix_same (ifc
->address
, p
))
901 /* Print if_addr structure. */
902 static void __attribute__ ((unused
))
903 connected_log (struct connected
*connected
, char *str
)
906 struct interface
*ifp
;
910 ifp
= connected
->ifp
;
911 p
= connected
->address
;
913 snprintf (logbuf
, BUFSIZ
, "%s interface %s vrf %u %s %s/%d ",
914 str
, ifp
->name
, ifp
->vrf_id
, prefix_family_str (p
),
915 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
918 p
= connected
->destination
;
921 strncat (logbuf
, inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
922 BUFSIZ
- strlen(logbuf
));
924 zlog_info("%s", logbuf
);
927 /* Print if_addr structure. */
928 static void __attribute__ ((unused
))
929 nbr_connected_log (struct nbr_connected
*connected
, char *str
)
932 struct interface
*ifp
;
936 ifp
= connected
->ifp
;
937 p
= connected
->address
;
939 snprintf (logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ",
940 str
, ifp
->name
, prefix_family_str (p
),
941 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
944 zlog_info("%s", logbuf
);
947 /* If two connected address has same prefix return 1. */
949 connected_same_prefix (struct prefix
*p1
, struct prefix
*p2
)
951 if (p1
->family
== p2
->family
)
953 if (p1
->family
== AF_INET
&&
954 IPV4_ADDR_SAME (&p1
->u
.prefix4
, &p2
->u
.prefix4
))
956 if (p1
->family
== AF_INET6
&&
957 IPV6_ADDR_SAME (&p1
->u
.prefix6
, &p2
->u
.prefix6
))
964 connected_lookup_prefix_exact (struct interface
*ifp
, struct prefix
*p
)
966 struct listnode
*node
;
967 struct listnode
*next
;
968 struct connected
*ifc
;
970 for (node
= listhead (ifp
->connected
); node
; node
= next
)
972 ifc
= listgetdata (node
);
975 if (connected_same_prefix (ifc
->address
, p
))
982 connected_delete_by_prefix (struct interface
*ifp
, struct prefix
*p
)
984 struct listnode
*node
;
985 struct listnode
*next
;
986 struct connected
*ifc
;
988 /* In case of same prefix come, replace it with new one. */
989 for (node
= listhead (ifp
->connected
); node
; node
= next
)
991 ifc
= listgetdata (node
);
994 if (connected_same_prefix (ifc
->address
, p
))
996 listnode_delete (ifp
->connected
, ifc
);
1003 /* Find the address on our side that will be used when packets
1006 connected_lookup_prefix (struct interface
*ifp
, struct prefix
*addr
)
1008 struct listnode
*cnode
;
1009 struct connected
*c
;
1010 struct connected
*match
;
1014 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
1016 if (c
->address
&& (c
->address
->family
== addr
->family
) &&
1017 prefix_match(CONNECTED_PREFIX(c
), addr
) &&
1018 (!match
|| (c
->address
->prefixlen
> match
->address
->prefixlen
)))
1025 connected_add_by_prefix (struct interface
*ifp
, struct prefix
*p
,
1026 struct prefix
*destination
)
1028 struct connected
*ifc
;
1030 /* Allocate new connected address. */
1031 ifc
= connected_new ();
1034 /* Fetch interface address */
1035 ifc
->address
= prefix_new();
1036 memcpy (ifc
->address
, p
, sizeof(struct prefix
));
1038 /* Fetch dest address */
1041 ifc
->destination
= prefix_new();
1042 memcpy (ifc
->destination
, destination
, sizeof(struct prefix
));
1045 /* Add connected address to the interface. */
1046 listnode_add (ifp
->connected
, ifc
);
1050 #if 0 /* this route_table of struct connected's is unused
1051 * however, it would be good to use a route_table rather than
1054 /* Interface looking up by interface's address. */
1055 /* Interface's IPv4 address reverse lookup table. */
1056 struct route_table
*ifaddr_ipv4_table
;
1057 /* struct route_table *ifaddr_ipv6_table; */
1060 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
1062 struct route_node
*rn
;
1063 struct prefix_ipv4 p
;
1066 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1069 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1072 route_unlock_node (rn
);
1073 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1074 inet_ntoa (*ifaddr
));
1081 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
1083 struct route_node
*rn
;
1084 struct prefix_ipv4 p
;
1087 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1090 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1093 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1094 inet_ntoa (*ifaddr
));
1098 route_unlock_node (rn
);
1099 route_unlock_node (rn
);
1102 /* Lookup interface by interface's IP address or interface index. */
1103 static struct interface
*
1104 ifaddr_ipv4_lookup (struct in_addr
*addr
, ifindex_t ifindex
)
1106 struct prefix_ipv4 p
;
1107 struct route_node
*rn
;
1108 struct interface
*ifp
;
1113 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1116 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
1121 route_unlock_node (rn
);
1125 return if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1127 #endif /* ifaddr_ipv4_table */
1129 /* Initialize interface list. */
1131 if_init (struct list
**intf_list
)
1133 *intf_list
= list_new ();
1135 ifaddr_ipv4_table
= route_table_init ();
1136 #endif /* ifaddr_ipv4_table */
1138 (*intf_list
)->cmp
= (int (*)(void *, void *))if_cmp_func
;
1142 if_terminate (struct list
**intf_list
)
1146 struct interface
*ifp
;
1148 ifp
= listnode_head (*intf_list
);
1154 ifp
->node
->info
= NULL
;
1155 route_unlock_node (ifp
->node
);
1161 list_delete (*intf_list
);
1166 if_link_type_str (enum zebra_link_type llt
)
1170 #define llts(T,S) case (T): return (S)
1171 llts(ZEBRA_LLT_UNKNOWN
, "Unknown");
1172 llts(ZEBRA_LLT_ETHER
, "Ethernet");
1173 llts(ZEBRA_LLT_EETHER
, "Experimental Ethernet");
1174 llts(ZEBRA_LLT_AX25
, "AX.25 Level 2");
1175 llts(ZEBRA_LLT_PRONET
, "PROnet token ring");
1176 llts(ZEBRA_LLT_IEEE802
, "IEEE 802.2 Ethernet/TR/TB");
1177 llts(ZEBRA_LLT_ARCNET
, "ARCnet");
1178 llts(ZEBRA_LLT_APPLETLK
, "AppleTalk");
1179 llts(ZEBRA_LLT_DLCI
, "Frame Relay DLCI");
1180 llts(ZEBRA_LLT_ATM
, "ATM");
1181 llts(ZEBRA_LLT_METRICOM
, "Metricom STRIP");
1182 llts(ZEBRA_LLT_IEEE1394
, "IEEE 1394 IPv4");
1183 llts(ZEBRA_LLT_EUI64
, "EUI-64");
1184 llts(ZEBRA_LLT_INFINIBAND
, "InfiniBand");
1185 llts(ZEBRA_LLT_SLIP
, "SLIP");
1186 llts(ZEBRA_LLT_CSLIP
, "Compressed SLIP");
1187 llts(ZEBRA_LLT_SLIP6
, "SLIPv6");
1188 llts(ZEBRA_LLT_CSLIP6
, "Compressed SLIPv6");
1189 llts(ZEBRA_LLT_ROSE
, "ROSE packet radio");
1190 llts(ZEBRA_LLT_X25
, "CCITT X.25");
1191 llts(ZEBRA_LLT_PPP
, "PPP");
1192 llts(ZEBRA_LLT_CHDLC
, "Cisco HDLC");
1193 llts(ZEBRA_LLT_RAWHDLC
, "Raw HDLC");
1194 llts(ZEBRA_LLT_LAPB
, "LAPB");
1195 llts(ZEBRA_LLT_IPIP
, "IPIP Tunnel");
1196 llts(ZEBRA_LLT_IPIP6
, "IPIP6 Tunnel");
1197 llts(ZEBRA_LLT_FRAD
, "FRAD");
1198 llts(ZEBRA_LLT_SKIP
, "SKIP vif");
1199 llts(ZEBRA_LLT_LOOPBACK
, "Loopback");
1200 llts(ZEBRA_LLT_LOCALTLK
, "Localtalk");
1201 llts(ZEBRA_LLT_FDDI
, "FDDI");
1202 llts(ZEBRA_LLT_SIT
, "IPv6-in-IPv4 SIT");
1203 llts(ZEBRA_LLT_IPDDP
, "IP-in-DDP tunnel");
1204 llts(ZEBRA_LLT_IPGRE
, "GRE over IP");
1205 llts(ZEBRA_LLT_PIMREG
, "PIMSM registration");
1206 llts(ZEBRA_LLT_HIPPI
, "HiPPI");
1207 llts(ZEBRA_LLT_IRDA
, "IrDA");
1208 llts(ZEBRA_LLT_FCPP
, "Fibre-Channel PtP");
1209 llts(ZEBRA_LLT_FCAL
, "Fibre-Channel Arbitrated Loop");
1210 llts(ZEBRA_LLT_FCPL
, "Fibre-Channel Public Loop");
1211 llts(ZEBRA_LLT_FCFABRIC
, "Fibre-Channel Fabric");
1212 llts(ZEBRA_LLT_IEEE802_TR
, "IEEE 802.2 Token Ring");
1213 llts(ZEBRA_LLT_IEEE80211
, "IEEE 802.11");
1214 llts(ZEBRA_LLT_IEEE80211_RADIOTAP
, "IEEE 802.11 Radiotap");
1215 llts(ZEBRA_LLT_IEEE802154
, "IEEE 802.15.4");
1216 llts(ZEBRA_LLT_IEEE802154_PHY
, "IEEE 802.15.4 Phy");
1218 zlog_warn ("Unknown value %d", llt
);
1219 return "Unknown type!";
1225 struct if_link_params
*
1226 if_link_params_get (struct interface
*ifp
)
1230 if (ifp
->link_params
!= NULL
)
1231 return ifp
->link_params
;
1233 struct if_link_params
*iflp
= XCALLOC(MTYPE_IF_LINK_PARAMS
,
1234 sizeof (struct if_link_params
));
1235 if (iflp
== NULL
) return NULL
;
1237 /* Set TE metric equal to standard metric */
1238 iflp
->te_metric
= ifp
->metric
;
1240 /* Compute default bandwidth based on interface */
1241 iflp
->default_bw
= ((ifp
->bandwidth
? ifp
->bandwidth
: DEFAULT_BANDWIDTH
)
1242 * TE_KILO_BIT
/ TE_BYTE
);
1244 /* Set Max, Reservable and Unreserved Bandwidth */
1245 iflp
->max_bw
= iflp
->default_bw
;
1246 iflp
->max_rsv_bw
= iflp
->default_bw
;
1247 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1248 iflp
->unrsv_bw
[i
] = iflp
->default_bw
;
1250 /* Update Link parameters status */
1251 iflp
->lp_status
= LP_TE_METRIC
| LP_MAX_BW
| LP_MAX_RSV_BW
| LP_UNRSV_BW
;
1253 /* Finally attach newly created Link Parameters */
1254 ifp
->link_params
= iflp
;
1260 if_link_params_free (struct interface
*ifp
)
1262 if (ifp
->link_params
== NULL
) return;
1263 XFREE(MTYPE_IF_LINK_PARAMS
, ifp
->link_params
);
1264 ifp
->link_params
= NULL
;