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.
31 #include "sockunion.h"
39 /* Master list of interfaces. */
42 /* One for each program. This structure is needed to store hooks. */
45 int (*if_new_hook
) (struct interface
*);
46 int (*if_delete_hook
) (struct interface
*);
49 /* Compare interface names, returning an integer greater than, equal to, or
50 * less than 0, (following the strcmp convention), according to the
51 * relationship between ifp1 and ifp2. Interface names consist of an
52 * alphabetic prefix and a numeric suffix. The primary sort key is
53 * lexicographic by name, and then numeric by number. No number sorts
54 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
58 if_cmp_func (struct interface
*ifp1
, struct interface
*ifp2
)
69 /* look up to any number */
70 l1
= strcspn(p1
, "0123456789");
71 l2
= strcspn(p2
, "0123456789");
73 /* name lengths are different -> compare names */
75 return (strcmp(p1
, p2
));
77 /* Note that this relies on all numbers being less than all letters, so
80 res
= strncmp(p1
, p2
, l1
);
82 /* names are different -> compare them */
86 /* with identical name part, go to numeric part */
95 x1
= strtol(p1
, &p1
, 10);
96 x2
= strtol(p2
, &p2
, 10);
98 /* let's compare numbers now */
104 /* numbers were equal, lets do it again..
105 (it happens with name like "eth123.456:789") */
114 /* Create new interface structure. */
116 if_create (const char *name
, int namelen
)
118 struct interface
*ifp
;
120 ifp
= XCALLOC (MTYPE_IF
, sizeof (struct interface
));
121 ifp
->ifindex
= IFINDEX_INTERNAL
;
124 assert (namelen
<= INTERFACE_NAMSIZ
); /* Need space for '\0' at end. */
125 strncpy (ifp
->name
, name
, namelen
);
126 ifp
->name
[namelen
] = '\0';
127 if (if_lookup_by_name(ifp
->name
) == NULL
)
128 listnode_add_sort (iflist
, ifp
);
130 zlog_err("if_create(%s): corruption detected -- interface with this "
131 "name exists already!", ifp
->name
);
132 ifp
->connected
= list_new ();
133 ifp
->connected
->del
= (void (*) (void *)) connected_free
;
135 if (if_master
.if_new_hook
)
136 (*if_master
.if_new_hook
) (ifp
);
141 /* Delete interface structure. */
143 if_delete_retain (struct interface
*ifp
)
145 if (if_master
.if_delete_hook
)
146 (*if_master
.if_delete_hook
) (ifp
);
148 /* Free connected address list */
149 list_delete (ifp
->connected
);
152 /* Delete and free interface structure. */
154 if_delete (struct interface
*ifp
)
156 listnode_delete (iflist
, ifp
);
158 if_delete_retain(ifp
);
160 XFREE (MTYPE_IF
, ifp
);
163 /* Add hook to interface master. */
165 if_add_hook (int type
, int (*func
)(struct interface
*ifp
))
169 if_master
.if_new_hook
= func
;
172 if_master
.if_delete_hook
= func
;
179 /* Interface existance check by index. */
181 if_lookup_by_index (unsigned int index
)
183 struct listnode
*node
;
184 struct interface
*ifp
;
186 for (ALL_LIST_ELEMENTS_RO(iflist
, node
, ifp
))
188 if (ifp
->ifindex
== index
)
195 ifindex2ifname (unsigned int index
)
197 struct interface
*ifp
;
199 return ((ifp
= if_lookup_by_index(index
)) != NULL
) ?
200 ifp
->name
: "unknown";
204 ifname2ifindex (const char *name
)
206 struct interface
*ifp
;
208 return ((ifp
= if_lookup_by_name(name
)) != NULL
) ? ifp
->ifindex
: 0;
211 /* Interface existance check by interface name. */
213 if_lookup_by_name (const char *name
)
215 struct listnode
*node
;
216 struct interface
*ifp
;
218 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
220 if (strcmp(name
, ifp
->name
) == 0)
227 if_lookup_by_name_len(const char *name
, size_t namelen
)
229 struct listnode
*node
;
230 struct interface
*ifp
;
232 if (namelen
> INTERFACE_NAMSIZ
)
235 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
237 if (!memcmp(name
, ifp
->name
, namelen
) && (ifp
->name
[namelen
] == '\0'))
243 /* Lookup interface by IPv4 address. */
245 if_lookup_exact_address (struct in_addr src
)
247 struct listnode
*node
;
248 struct listnode
*cnode
;
249 struct interface
*ifp
;
253 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
255 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
259 if (p
&& p
->family
== AF_INET
)
261 if (IPV4_ADDR_SAME (&p
->u
.prefix4
, &src
))
269 /* Lookup interface by IPv4 address. */
271 if_lookup_address (struct in_addr src
)
273 struct listnode
*node
;
276 struct listnode
*cnode
;
277 struct interface
*ifp
;
280 struct interface
*match
;
282 addr
.family
= AF_INET
;
283 addr
.u
.prefix4
= src
;
284 addr
.prefixlen
= IPV4_MAX_BITLEN
;
288 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
290 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
292 if (c
->address
&& (c
->address
->family
== AF_INET
))
294 if (CONNECTED_POINTOPOINT_HOST(c
))
296 /* PTP links are conventionally identified
297 by the address of the far end - MAG */
298 if (IPV4_ADDR_SAME (&c
->destination
->u
.prefix4
, &src
))
305 if (prefix_match (p
, &addr
) && p
->prefixlen
> bestlen
)
307 bestlen
= p
->prefixlen
;
317 /* Get interface by name if given name interface doesn't exist create
320 if_get_by_name (const char *name
)
322 struct interface
*ifp
;
324 return ((ifp
= if_lookup_by_name(name
)) != NULL
) ? ifp
:
325 if_create(name
, strlen(name
));
329 if_get_by_name_len(const char *name
, size_t namelen
)
331 struct interface
*ifp
;
333 return ((ifp
= if_lookup_by_name_len(name
, namelen
)) != NULL
) ? ifp
:
334 if_create(name
, namelen
);
337 /* Does interface up ? */
339 if_is_up (struct interface
*ifp
)
341 return ifp
->flags
& IFF_UP
;
344 /* Is interface running? */
346 if_is_running (struct interface
*ifp
)
348 return ifp
->flags
& IFF_RUNNING
;
351 /* Is the interface operative, eg. either UP & RUNNING
352 or UP & !ZEBRA_INTERFACE_LINK_DETECTION */
354 if_is_operative (struct interface
*ifp
)
356 return ((ifp
->flags
& IFF_UP
) &&
357 (ifp
->flags
& IFF_RUNNING
|| !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)));
360 /* Is this loopback interface ? */
362 if_is_loopback (struct interface
*ifp
)
364 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
365 * but Y on platform N?
367 return (ifp
->flags
& (IFF_LOOPBACK
|IFF_NOXMIT
|IFF_VIRTUAL
));
370 /* Does this interface support broadcast ? */
372 if_is_broadcast (struct interface
*ifp
)
374 return ifp
->flags
& IFF_BROADCAST
;
377 /* Does this interface support broadcast ? */
379 if_is_pointopoint (struct interface
*ifp
)
381 return ifp
->flags
& IFF_POINTOPOINT
;
384 /* Does this interface support multicast ? */
386 if_is_multicast (struct interface
*ifp
)
388 return ifp
->flags
& IFF_MULTICAST
;
391 /* Printout flag information into log */
393 if_flag_dump (unsigned long flag
)
396 static char logbuf
[BUFSIZ
];
398 #define IFF_OUT_LOG(X,STR) \
402 strlcat (logbuf, ",", BUFSIZ); \
405 strlcat (logbuf, STR, BUFSIZ); \
408 strlcpy (logbuf
, "<", BUFSIZ
);
409 IFF_OUT_LOG (IFF_UP
, "UP");
410 IFF_OUT_LOG (IFF_BROADCAST
, "BROADCAST");
411 IFF_OUT_LOG (IFF_DEBUG
, "DEBUG");
412 IFF_OUT_LOG (IFF_LOOPBACK
, "LOOPBACK");
413 IFF_OUT_LOG (IFF_POINTOPOINT
, "POINTOPOINT");
414 IFF_OUT_LOG (IFF_NOTRAILERS
, "NOTRAILERS");
415 IFF_OUT_LOG (IFF_RUNNING
, "RUNNING");
416 IFF_OUT_LOG (IFF_NOARP
, "NOARP");
417 IFF_OUT_LOG (IFF_PROMISC
, "PROMISC");
418 IFF_OUT_LOG (IFF_ALLMULTI
, "ALLMULTI");
419 IFF_OUT_LOG (IFF_OACTIVE
, "OACTIVE");
420 IFF_OUT_LOG (IFF_SIMPLEX
, "SIMPLEX");
421 IFF_OUT_LOG (IFF_LINK0
, "LINK0");
422 IFF_OUT_LOG (IFF_LINK1
, "LINK1");
423 IFF_OUT_LOG (IFF_LINK2
, "LINK2");
424 IFF_OUT_LOG (IFF_MULTICAST
, "MULTICAST");
425 IFF_OUT_LOG (IFF_NOXMIT
, "NOXMIT");
426 IFF_OUT_LOG (IFF_NORTEXCH
, "NORTEXCH");
427 IFF_OUT_LOG (IFF_VIRTUAL
, "VIRTUAL");
428 IFF_OUT_LOG (IFF_IPV4
, "IPv4");
429 IFF_OUT_LOG (IFF_IPV6
, "IPv6");
431 strlcat (logbuf
, ">", BUFSIZ
);
439 if_dump (struct interface
*ifp
)
441 struct listnode
*node
;
444 zlog_info ("Interface %s index %d metric %d mtu %d "
447 #endif /* HAVE_IPV6 */
449 ifp
->name
, ifp
->ifindex
, ifp
->metric
, ifp
->mtu
,
452 #endif /* HAVE_IPV6 */
453 if_flag_dump (ifp
->flags
));
455 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, c
))
459 /* Interface printing for all interface. */
463 struct listnode
*node
;
466 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, p
))
470 DEFUN (interface_desc
,
473 "Interface specific description\n"
474 "Characters describing this interface\n")
476 struct interface
*ifp
;
483 XFREE (MTYPE_TMP
, ifp
->desc
);
484 ifp
->desc
= argv_concat(argv
, argc
, 0);
489 DEFUN (no_interface_desc
,
490 no_interface_desc_cmd
,
493 "Interface specific description\n")
495 struct interface
*ifp
;
499 XFREE (MTYPE_TMP
, ifp
->desc
);
506 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
507 * a seperate struct interface for each logical interface, so config
508 * file may be full of 'interface fooX:Y'. Solaris however does not
509 * expose logical interfaces via PF_ROUTE, so trying to track logical
510 * interfaces can be fruitless, for that reason Quagga only tracks
511 * the primary IP interface.
513 * We try accomodate SUNWzebra by:
514 * - looking up the interface name, to see whether it exists, if so
516 * - for protocol daemons, this could only because zebra told us of
518 * - for zebra, only because it learnt from kernel
520 * - search the name to see if it contains a sub-ipif / logical interface
521 * seperator, the ':' char. If it does:
522 * - text up to that char must be the primary name - get that name.
524 * - no idea, just get the name in its entirety.
526 static struct interface
*
527 if_sunwzebra_get (const char *name
, size_t nlen
)
529 struct interface
*ifp
;
532 if ( (ifp
= if_lookup_by_name_len(name
, nlen
)) != NULL
)
535 /* hunt the primary interface name... */
536 while (seppos
< nlen
&& name
[seppos
] != ':')
539 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
541 return if_get_by_name_len (name
, seppos
);
543 return if_get_by_name_len (name
, nlen
);
550 "Select an interface to configure\n"
551 "Interface's name\n")
553 struct interface
*ifp
;
556 if ((sl
= strlen(argv
[0])) > INTERFACE_NAMSIZ
)
558 vty_out (vty
, "%% Interface name %s is invalid: length exceeds "
560 argv
[0], INTERFACE_NAMSIZ
, VTY_NEWLINE
);
565 ifp
= if_sunwzebra_get (argv
[0], sl
);
567 ifp
= if_get_by_name_len(argv
[0], sl
);
571 vty
->node
= INTERFACE_NODE
;
576 DEFUN_NOSH (no_interface
,
578 "no interface IFNAME",
580 "Delete a pseudo interface's configuration\n"
581 "Interface's name\n")
583 // deleting interface
584 struct interface
*ifp
;
586 ifp
= if_lookup_by_name (argv
[0]);
590 vty_out (vty
, "%% Interface %s does not exist%s", argv
[0], VTY_NEWLINE
);
594 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
596 vty_out (vty
, "%% Only inactive interfaces can be deleted%s",
606 /* For debug purpose. */
613 struct listnode
*node
;
614 struct listnode
*node2
;
615 struct interface
*ifp
;
616 struct connected
*ifc
;
619 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
621 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
625 if (p
->family
== AF_INET
)
626 vty_out (vty
, "%s/%d%s", inet_ntoa (p
->u
.prefix4
), p
->prefixlen
,
633 /* Allocate connected structure. */
637 struct connected
*new = XMALLOC (MTYPE_CONNECTED
, sizeof (struct connected
));
638 memset (new, 0, sizeof (struct connected
));
642 /* Free connected structure. */
644 connected_free (struct connected
*connected
)
646 if (connected
->address
)
647 prefix_free (connected
->address
);
649 if (connected
->destination
)
650 prefix_free (connected
->destination
);
652 if (connected
->label
)
653 XFREE (MTYPE_CONNECTED_LABEL
, connected
->label
);
655 XFREE (MTYPE_CONNECTED
, connected
);
658 /* Print if_addr structure. */
659 static void __attribute__ ((unused
))
660 connected_log (struct connected
*connected
, char *str
)
663 struct interface
*ifp
;
667 ifp
= connected
->ifp
;
668 p
= connected
->address
;
670 snprintf (logbuf
, BUFSIZ
, "%s interface %s %s %s/%d ",
671 str
, ifp
->name
, prefix_family_str (p
),
672 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
675 p
= connected
->destination
;
678 strncat (logbuf
, inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
679 BUFSIZ
- strlen(logbuf
));
681 zlog (NULL
, LOG_INFO
, logbuf
);
684 /* If two connected address has same prefix return 1. */
686 connected_same_prefix (struct prefix
*p1
, struct prefix
*p2
)
688 if (p1
->family
== p2
->family
)
690 if (p1
->family
== AF_INET
&&
691 IPV4_ADDR_SAME (&p1
->u
.prefix4
, &p2
->u
.prefix4
))
694 if (p1
->family
== AF_INET6
&&
695 IPV6_ADDR_SAME (&p1
->u
.prefix6
, &p2
->u
.prefix6
))
697 #endif /* HAVE_IPV6 */
703 connected_delete_by_prefix (struct interface
*ifp
, struct prefix
*p
)
705 struct listnode
*node
;
706 struct listnode
*next
;
707 struct connected
*ifc
;
709 /* In case of same prefix come, replace it with new one. */
710 for (node
= listhead (ifp
->connected
); node
; node
= next
)
712 ifc
= listgetdata (node
);
715 if (connected_same_prefix (ifc
->address
, p
))
717 listnode_delete (ifp
->connected
, ifc
);
724 /* Find the IPv4 address on our side that will be used when packets
727 connected_lookup_address (struct interface
*ifp
, struct in_addr dst
)
730 struct listnode
*cnode
;
733 struct connected
*match
;
735 addr
.family
= AF_INET
;
736 addr
.u
.prefix4
= dst
;
737 addr
.prefixlen
= IPV4_MAX_BITLEN
;
741 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, c
))
743 if (c
->address
&& (c
->address
->family
== AF_INET
))
745 if (CONNECTED_POINTOPOINT_HOST(c
))
747 /* PTP links are conventionally identified
748 by the address of the far end - MAG */
749 if (IPV4_ADDR_SAME (&c
->destination
->u
.prefix4
, &dst
))
756 if (prefix_match (p
, &addr
) &&
757 (!match
|| (p
->prefixlen
> match
->address
->prefixlen
)))
766 connected_add_by_prefix (struct interface
*ifp
, struct prefix
*p
,
767 struct prefix
*destination
)
769 struct connected
*ifc
;
771 /* Allocate new connected address. */
772 ifc
= connected_new ();
775 /* Fetch interface address */
776 ifc
->address
= prefix_new();
777 memcpy (ifc
->address
, p
, sizeof(struct prefix
));
779 /* Fetch dest address */
782 ifc
->destination
= prefix_new();
783 memcpy (ifc
->destination
, destination
, sizeof(struct prefix
));
786 /* Add connected address to the interface. */
787 listnode_add (ifp
->connected
, ifc
);
791 #ifndef HAVE_IF_NAMETOINDEX
793 if_nametoindex (const char *name
)
795 struct interface
*ifp
;
797 return ((ifp
= if_lookup_by_name_len(name
, strnlen(name
, IFNAMSIZ
))) != NULL
)
802 #ifndef HAVE_IF_INDEXTONAME
804 if_indextoname (unsigned int ifindex
, char *name
)
806 struct interface
*ifp
;
808 if (!(ifp
= if_lookup_by_index(ifindex
)))
810 strncpy (name
, ifp
->name
, IFNAMSIZ
);
815 #if 0 /* this route_table of struct connected's is unused
816 * however, it would be good to use a route_table rather than
819 /* Interface looking up by interface's address. */
820 /* Interface's IPv4 address reverse lookup table. */
821 struct route_table
*ifaddr_ipv4_table
;
822 /* struct route_table *ifaddr_ipv6_table; */
825 ifaddr_ipv4_add (struct in_addr
*ifaddr
, struct interface
*ifp
)
827 struct route_node
*rn
;
828 struct prefix_ipv4 p
;
831 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
834 rn
= route_node_get (ifaddr_ipv4_table
, (struct prefix
*) &p
);
837 route_unlock_node (rn
);
838 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
839 inet_ntoa (*ifaddr
));
846 ifaddr_ipv4_delete (struct in_addr
*ifaddr
, struct interface
*ifp
)
848 struct route_node
*rn
;
849 struct prefix_ipv4 p
;
852 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
855 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
858 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
859 inet_ntoa (*ifaddr
));
863 route_unlock_node (rn
);
864 route_unlock_node (rn
);
867 /* Lookup interface by interface's IP address or interface index. */
868 static struct interface
*
869 ifaddr_ipv4_lookup (struct in_addr
*addr
, unsigned int ifindex
)
871 struct prefix_ipv4 p
;
872 struct route_node
*rn
;
873 struct interface
*ifp
;
878 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
881 rn
= route_node_lookup (ifaddr_ipv4_table
, (struct prefix
*) &p
);
886 route_unlock_node (rn
);
890 return if_lookup_by_index(ifindex
);
892 #endif /* ifaddr_ipv4_table */
894 /* Initialize interface list. */
898 iflist
= list_new ();
900 ifaddr_ipv4_table
= route_table_init ();
901 #endif /* ifaddr_ipv4_table */
904 iflist
->cmp
= (int (*)(void *, void *))if_cmp_func
;
908 memset (&if_master
, 0, sizeof if_master
);