[lib] remove autogenerated file, update .cvsignore
[jleu-quagga.git] / lib / if.c
blobc43d9564f6a5584474c1c2976d0d99ce55c4da4b
2 /*
3 * Interface functions.
4 * Copyright (C) 1997, 98 Kunihiro Ishiguro
6 * This file is part of GNU Zebra.
7 *
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.
24 #include <zebra.h>
26 #include "linklist.h"
27 #include "vector.h"
28 #include "vty.h"
29 #include "command.h"
30 #include "if.h"
31 #include "sockunion.h"
32 #include "prefix.h"
33 #include "memory.h"
34 #include "table.h"
35 #include "buffer.h"
36 #include "str.h"
37 #include "log.h"
39 /* Master list of interfaces. */
40 struct list *iflist;
42 /* One for each program. This structure is needed to store hooks. */
43 struct if_master
45 int (*if_new_hook) (struct interface *);
46 int (*if_delete_hook) (struct interface *);
47 } if_master;
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 <
55 * devpty0, de0 < del0
56 */
57 int
58 if_cmp_func (struct interface *ifp1, struct interface *ifp2)
60 unsigned int l1, l2;
61 long int x1, x2;
62 char *p1, *p2;
63 int res;
65 p1 = ifp1->name;
66 p2 = ifp2->name;
68 while (*p1 && *p2) {
69 /* look up to any number */
70 l1 = strcspn(p1, "0123456789");
71 l2 = strcspn(p2, "0123456789");
73 /* name lengths are different -> compare names */
74 if (l1 != l2)
75 return (strcmp(p1, p2));
77 /* Note that this relies on all numbers being less than all letters, so
78 * that de0 < del0.
80 res = strncmp(p1, p2, l1);
82 /* names are different -> compare them */
83 if (res)
84 return res;
86 /* with identical name part, go to numeric part */
87 p1 += l1;
88 p2 += l1;
90 if (!*p1)
91 return -1;
92 if (!*p2)
93 return 1;
95 x1 = strtol(p1, &p1, 10);
96 x2 = strtol(p2, &p2, 10);
98 /* let's compare numbers now */
99 if (x1 < x2)
100 return -1;
101 if (x1 > x2)
102 return 1;
104 /* numbers were equal, lets do it again..
105 (it happens with name like "eth123.456:789") */
107 if (*p1)
108 return 1;
109 if (*p2)
110 return -1;
111 return 0;
114 /* Create new interface structure. */
115 struct interface *
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;
123 assert (name);
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);
129 else
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);
138 return ifp;
141 /* Delete interface structure. */
142 void
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. */
153 void
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. */
164 void
165 if_add_hook (int type, int (*func)(struct interface *ifp))
167 switch (type) {
168 case IF_NEW_HOOK:
169 if_master.if_new_hook = func;
170 break;
171 case IF_DELETE_HOOK:
172 if_master.if_delete_hook = func;
173 break;
174 default:
175 break;
179 /* Interface existance check by index. */
180 struct interface *
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)
189 return ifp;
191 return NULL;
194 const char *
195 ifindex2ifname (unsigned int index)
197 struct interface *ifp;
199 return ((ifp = if_lookup_by_index(index)) != NULL) ?
200 ifp->name : "unknown";
203 unsigned int
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. */
212 struct interface *
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)
221 return ifp;
223 return NULL;
226 struct interface *
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)
233 return NULL;
235 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
237 if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
238 return ifp;
240 return NULL;
243 /* Lookup interface by IPv4 address. */
244 struct interface *
245 if_lookup_exact_address (struct in_addr src)
247 struct listnode *node;
248 struct listnode *cnode;
249 struct interface *ifp;
250 struct prefix *p;
251 struct connected *c;
253 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
255 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
257 p = c->address;
259 if (p && p->family == AF_INET)
261 if (IPV4_ADDR_SAME (&p->u.prefix4, &src))
262 return ifp;
266 return NULL;
269 /* Lookup interface by IPv4 address. */
270 struct interface *
271 if_lookup_address (struct in_addr src)
273 struct listnode *node;
274 struct prefix addr;
275 int bestlen = 0;
276 struct listnode *cnode;
277 struct interface *ifp;
278 struct prefix *p;
279 struct connected *c;
280 struct interface *match;
282 addr.family = AF_INET;
283 addr.u.prefix4 = src;
284 addr.prefixlen = IPV4_MAX_BITLEN;
286 match = NULL;
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))
299 return ifp;
301 else
303 p = c->address;
305 if (prefix_match (p, &addr) && p->prefixlen > bestlen)
307 bestlen = p->prefixlen;
308 match = ifp;
314 return match;
317 /* Get interface by name if given name interface doesn't exist create
318 one. */
319 struct interface *
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));
328 struct interface *
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 */
392 const char *
393 if_flag_dump (unsigned long flag)
395 int separator = 0;
396 static char logbuf[BUFSIZ];
398 #define IFF_OUT_LOG(X,STR) \
399 if (flag & (X)) \
401 if (separator) \
402 strlcat (logbuf, ",", BUFSIZ); \
403 else \
404 separator = 1; \
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);
433 return logbuf;
434 #undef IFF_OUT_LOG
437 /* For debugging */
438 static void
439 if_dump (struct interface *ifp)
441 struct listnode *node;
442 struct connected *c;
444 zlog_info ("Interface %s index %d metric %d mtu %d "
445 #ifdef HAVE_IPV6
446 "mtu6 %d "
447 #endif /* HAVE_IPV6 */
448 "%s",
449 ifp->name, ifp->ifindex, ifp->metric, ifp->mtu,
450 #ifdef HAVE_IPV6
451 ifp->mtu6,
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. */
460 void
461 if_dump_all ()
463 struct listnode *node;
464 void *p;
466 for (ALL_LIST_ELEMENTS_RO (iflist, node, p))
467 if_dump (p);
470 DEFUN (interface_desc,
471 interface_desc_cmd,
472 "description .LINE",
473 "Interface specific description\n"
474 "Characters describing this interface\n")
476 struct interface *ifp;
478 if (argc == 0)
479 return CMD_SUCCESS;
481 ifp = vty->index;
482 if (ifp->desc)
483 XFREE (MTYPE_TMP, ifp->desc);
484 ifp->desc = argv_concat(argv, argc, 0);
486 return CMD_SUCCESS;
489 DEFUN (no_interface_desc,
490 no_interface_desc_cmd,
491 "no description",
492 NO_STR
493 "Interface specific description\n")
495 struct interface *ifp;
497 ifp = vty->index;
498 if (ifp->desc)
499 XFREE (MTYPE_TMP, ifp->desc);
500 ifp->desc = NULL;
502 return CMD_SUCCESS;
506 /* See also wrapper function zebra_interface() in zebra/interface.c */
507 DEFUN (interface,
508 interface_cmd,
509 "interface IFNAME",
510 "Select an interface to configure\n"
511 "Interface's name\n")
513 struct interface *ifp;
514 size_t sl;
516 if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ)
518 vty_out (vty, "%% Interface name %s is invalid: length exceeds "
519 "%d characters%s",
520 argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE);
521 return CMD_WARNING;
524 ifp = if_get_by_name_len(argv[0], sl);
526 vty->index = ifp;
527 vty->node = INTERFACE_NODE;
529 return CMD_SUCCESS;
532 DEFUN_NOSH (no_interface,
533 no_interface_cmd,
534 "no interface IFNAME",
535 NO_STR
536 "Delete a pseudo interface's configuration\n"
537 "Interface's name\n")
539 // deleting interface
540 struct interface *ifp;
542 ifp = if_lookup_by_name (argv[0]);
544 if (ifp == NULL)
546 vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE);
547 return CMD_WARNING;
550 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
552 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
553 VTY_NEWLINE);
554 return CMD_WARNING;
557 if_delete(ifp);
559 return CMD_SUCCESS;
562 /* For debug purpose. */
563 DEFUN (show_address,
564 show_address_cmd,
565 "show address",
566 SHOW_STR
567 "address\n")
569 struct listnode *node;
570 struct listnode *node2;
571 struct interface *ifp;
572 struct connected *ifc;
573 struct prefix *p;
575 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
577 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
579 p = ifc->address;
581 if (p->family == AF_INET)
582 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
583 VTY_NEWLINE);
586 return CMD_SUCCESS;
589 /* Allocate connected structure. */
590 struct connected *
591 connected_new (void)
593 struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected));
594 memset (new, 0, sizeof (struct connected));
595 return new;
598 /* Free connected structure. */
599 void
600 connected_free (struct connected *connected)
602 if (connected->address)
603 prefix_free (connected->address);
605 if (connected->destination)
606 prefix_free (connected->destination);
608 if (connected->label)
609 XFREE (MTYPE_CONNECTED_LABEL, connected->label);
611 XFREE (MTYPE_CONNECTED, connected);
614 /* Print if_addr structure. */
615 static void __attribute__ ((unused))
616 connected_log (struct connected *connected, char *str)
618 struct prefix *p;
619 struct interface *ifp;
620 char logbuf[BUFSIZ];
621 char buf[BUFSIZ];
623 ifp = connected->ifp;
624 p = connected->address;
626 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
627 str, ifp->name, prefix_family_str (p),
628 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
629 p->prefixlen);
631 p = connected->destination;
632 if (p)
634 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
635 BUFSIZ - strlen(logbuf));
637 zlog (NULL, LOG_INFO, logbuf);
640 /* If two connected address has same prefix return 1. */
641 static int
642 connected_same_prefix (struct prefix *p1, struct prefix *p2)
644 if (p1->family == p2->family)
646 if (p1->family == AF_INET &&
647 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
648 return 1;
649 #ifdef HAVE_IPV6
650 if (p1->family == AF_INET6 &&
651 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
652 return 1;
653 #endif /* HAVE_IPV6 */
655 return 0;
658 struct connected *
659 connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
661 struct listnode *node;
662 struct listnode *next;
663 struct connected *ifc;
665 /* In case of same prefix come, replace it with new one. */
666 for (node = listhead (ifp->connected); node; node = next)
668 ifc = listgetdata (node);
669 next = node->next;
671 if (connected_same_prefix (ifc->address, p))
673 listnode_delete (ifp->connected, ifc);
674 return ifc;
677 return NULL;
680 /* Find the IPv4 address on our side that will be used when packets
681 are sent to dst. */
682 struct connected *
683 connected_lookup_address (struct interface *ifp, struct in_addr dst)
685 struct prefix addr;
686 struct listnode *cnode;
687 struct prefix *p;
688 struct connected *c;
689 struct connected *match;
691 addr.family = AF_INET;
692 addr.u.prefix4 = dst;
693 addr.prefixlen = IPV4_MAX_BITLEN;
695 match = NULL;
697 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
699 if (c->address && (c->address->family == AF_INET))
701 if (CONNECTED_POINTOPOINT_HOST(c))
703 /* PTP links are conventionally identified
704 by the address of the far end - MAG */
705 if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &dst))
706 return c;
708 else
710 p = c->address;
712 if (prefix_match (p, &addr) &&
713 (!match || (p->prefixlen > match->address->prefixlen)))
714 match = c;
718 return match;
721 struct connected *
722 connected_add_by_prefix (struct interface *ifp, struct prefix *p,
723 struct prefix *destination)
725 struct connected *ifc;
727 /* Allocate new connected address. */
728 ifc = connected_new ();
729 ifc->ifp = ifp;
731 /* Fetch interface address */
732 ifc->address = prefix_new();
733 memcpy (ifc->address, p, sizeof(struct prefix));
735 /* Fetch dest address */
736 if (destination)
738 ifc->destination = prefix_new();
739 memcpy (ifc->destination, destination, sizeof(struct prefix));
742 /* Add connected address to the interface. */
743 listnode_add (ifp->connected, ifc);
744 return ifc;
747 #ifndef HAVE_IF_NAMETOINDEX
748 unsigned int
749 if_nametoindex (const char *name)
751 struct interface *ifp;
753 return ((ifp = if_lookup_by_name_len(name, strnlen(name, IFNAMSIZ))) != NULL)
754 ? ifp->ifindex : 0;
756 #endif
758 #ifndef HAVE_IF_INDEXTONAME
759 char *
760 if_indextoname (unsigned int ifindex, char *name)
762 struct interface *ifp;
764 if (!(ifp = if_lookup_by_index(ifindex)))
765 return NULL;
766 strncpy (name, ifp->name, IFNAMSIZ);
767 return ifp->name;
769 #endif
771 #if 0 /* this route_table of struct connected's is unused
772 * however, it would be good to use a route_table rather than
773 * a list..
775 /* Interface looking up by interface's address. */
776 /* Interface's IPv4 address reverse lookup table. */
777 struct route_table *ifaddr_ipv4_table;
778 /* struct route_table *ifaddr_ipv6_table; */
780 static void
781 ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
783 struct route_node *rn;
784 struct prefix_ipv4 p;
786 p.family = AF_INET;
787 p.prefixlen = IPV4_MAX_PREFIXLEN;
788 p.prefix = *ifaddr;
790 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
791 if (rn)
793 route_unlock_node (rn);
794 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
795 inet_ntoa (*ifaddr));
796 return;
798 rn->info = ifp;
801 static void
802 ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
804 struct route_node *rn;
805 struct prefix_ipv4 p;
807 p.family = AF_INET;
808 p.prefixlen = IPV4_MAX_PREFIXLEN;
809 p.prefix = *ifaddr;
811 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
812 if (! rn)
814 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
815 inet_ntoa (*ifaddr));
816 return;
818 rn->info = NULL;
819 route_unlock_node (rn);
820 route_unlock_node (rn);
823 /* Lookup interface by interface's IP address or interface index. */
824 static struct interface *
825 ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
827 struct prefix_ipv4 p;
828 struct route_node *rn;
829 struct interface *ifp;
831 if (addr)
833 p.family = AF_INET;
834 p.prefixlen = IPV4_MAX_PREFIXLEN;
835 p.prefix = *addr;
837 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
838 if (! rn)
839 return NULL;
841 ifp = rn->info;
842 route_unlock_node (rn);
843 return ifp;
845 else
846 return if_lookup_by_index(ifindex);
848 #endif /* ifaddr_ipv4_table */
850 /* Initialize interface list. */
851 void
852 if_init (void)
854 iflist = list_new ();
855 #if 0
856 ifaddr_ipv4_table = route_table_init ();
857 #endif /* ifaddr_ipv4_table */
859 if (iflist) {
860 iflist->cmp = (int (*)(void *, void *))if_cmp_func;
861 return;
864 memset (&if_master, 0, sizeof if_master);