blob: e1a18393bd8601bead8b728fa45cbeccf21d8a03 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Interface functions.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "linklist.h"
26#include "vector.h"
27#include "vty.h"
28#include "command.h"
29#include "if.h"
30#include "sockunion.h"
31#include "prefix.h"
32#include "zebra/connected.h"
33#include "memory.h"
34#include "table.h"
35#include "buffer.h"
36#include "str.h"
37#include "log.h"
38
39/* Master list of interfaces. */
40struct list *iflist;
41
42/* One for each program. This structure is needed to store hooks. */
43struct if_master
44{
45 int (*if_new_hook) (struct interface *);
46 int (*if_delete_hook) (struct interface *);
47} if_master;
48
49/* Create new interface structure. */
50struct interface *
51if_new ()
52{
53 struct interface *ifp;
54
55 ifp = XMALLOC (MTYPE_IF, sizeof (struct interface));
56 memset (ifp, 0, sizeof (struct interface));
57 return ifp;
58}
59
60struct interface *
61if_create ()
62{
63 struct interface *ifp;
64
65 ifp = if_new ();
66
67 listnode_add (iflist, ifp);
68 ifp->connected = list_new ();
69 ifp->connected->del = (void (*) (void *)) connected_free;
70
71 if (if_master.if_new_hook)
72 (*if_master.if_new_hook) (ifp);
73
74 return ifp;
75}
76
77/* Delete and free interface structure. */
78void
79if_delete (struct interface *ifp)
80{
81 listnode_delete (iflist, ifp);
82
83 if (if_master.if_delete_hook)
84 (*if_master.if_delete_hook) (ifp);
85
86 /* Free connected address list */
87 list_delete (ifp->connected);
88
89 XFREE (MTYPE_IF, ifp);
90}
91
92/* Add hook to interface master. */
93void
94if_add_hook (int type, int (*func)(struct interface *ifp))
95{
96 switch (type) {
97 case IF_NEW_HOOK:
98 if_master.if_new_hook = func;
99 break;
100 case IF_DELETE_HOOK:
101 if_master.if_delete_hook = func;
102 break;
103 default:
104 break;
105 }
106}
107
108/* Interface existance check by index. */
109struct interface *
110if_lookup_by_index (unsigned int index)
111{
112 listnode node;
113 struct interface *ifp;
114
115 for (node = listhead (iflist); node; nextnode (node))
116 {
117 ifp = getdata (node);
118 if (ifp->ifindex == index)
119 return ifp;
120 }
121 return NULL;
122}
123
124char *
125ifindex2ifname (unsigned int index)
126{
127 listnode node;
128 struct interface *ifp;
129
130 for (node = listhead (iflist); node; nextnode (node))
131 {
132 ifp = getdata (node);
133 if (ifp->ifindex == index)
134 return ifp->name;
135 }
136 return "unknown";
137}
138
139/* Interface existance check by interface name. */
140struct interface *
141if_lookup_by_name (char *name)
142{
143 listnode node;
144 struct interface *ifp;
145
146 for (node = listhead (iflist); node; nextnode (node))
147 {
148 ifp = getdata (node);
149 if (strncmp (name, ifp->name, sizeof ifp->name) == 0)
150 return ifp;
151 }
152 return NULL;
153}
154
155/* Lookup interface by IPv4 address. */
156struct interface *
157if_lookup_exact_address (struct in_addr src)
158{
159 listnode node;
160 listnode cnode;
161 struct interface *ifp;
162 struct prefix *p;
163 struct connected *c;
164
165 for (node = listhead (iflist); node; nextnode (node))
166 {
167 ifp = getdata (node);
168
169 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
170 {
171 c = getdata (cnode);
172
173 p = c->address;
174
175 if (p && p->family == AF_INET)
176 {
177 if (IPV4_ADDR_SAME (&p->u.prefix4, &src))
178 return ifp;
179 }
180 }
181 }
182 return NULL;
183}
184
185/* Lookup interface by IPv4 address. */
186struct interface *
187if_lookup_address (struct in_addr src)
188{
189 listnode node;
190 struct prefix addr;
191 struct prefix best;
paul00df0c12002-12-13 21:07:36 +0000192 struct prefix peer;
paul718e3742002-12-13 20:15:29 +0000193 listnode cnode;
194 struct interface *ifp;
195 struct prefix *p;
196 struct connected *c;
197 struct interface *match;
paul00df0c12002-12-13 21:07:36 +0000198 int prefixlen;
paul718e3742002-12-13 20:15:29 +0000199
200 /* Zero structures - get rid of rubbish from stack */
201 memset(&addr, 0, sizeof(addr));
202 memset(&best, 0, sizeof(best));
203
204 addr.family = AF_INET;
205 addr.u.prefix4 = src;
206 addr.prefixlen = IPV4_MAX_BITLEN;
207
208 match = NULL;
209
210 for (node = listhead (iflist); node; nextnode (node))
211 {
212 ifp = getdata (node);
213
214 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
215 {
216 c = getdata (cnode);
paul00df0c12002-12-13 21:07:36 +0000217 p = c->address;
paul718e3742002-12-13 20:15:29 +0000218
paul00df0c12002-12-13 21:07:36 +0000219 if (p->family == AF_INET)
paul718e3742002-12-13 20:15:29 +0000220 {
paul00df0c12002-12-13 21:07:36 +0000221 prefixlen = p->prefixlen;
paul718e3742002-12-13 20:15:29 +0000222
paul00df0c12002-12-13 21:07:36 +0000223 if (if_is_pointopoint (ifp) ||
224 prefixlen >= IPV4_MAX_PREFIXLEN - 1)
paul718e3742002-12-13 20:15:29 +0000225 {
paul00df0c12002-12-13 21:07:36 +0000226 peer = *c->destination;
227 peer.prefixlen = prefixlen;
228 p = &peer;
paul718e3742002-12-13 20:15:29 +0000229 }
paul718e3742002-12-13 20:15:29 +0000230
paul00df0c12002-12-13 21:07:36 +0000231 if (prefix_match (p, &addr) && prefixlen > best.prefixlen)
paul718e3742002-12-13 20:15:29 +0000232 {
paul00df0c12002-12-13 21:07:36 +0000233 best = *p;
234 match = ifp;
paul718e3742002-12-13 20:15:29 +0000235 }
236 }
237 }
238 }
239 return match;
240}
241
242/* Get interface by name if given name interface doesn't exist create
243 one. */
244struct interface *
245if_get_by_name (char *name)
246{
247 struct interface *ifp;
248
249 ifp = if_lookup_by_name (name);
250 if (ifp == NULL)
251 {
252 ifp = if_create ();
253 strncpy (ifp->name, name, IFNAMSIZ);
254 }
255 return ifp;
256}
257
258/* Does interface up ? */
259int
260if_is_up (struct interface *ifp)
261{
262 return ifp->flags & IFF_UP;
263}
264
paul2e3b2e42002-12-13 21:03:13 +0000265/* Is interface running? */
266int
267if_is_running (struct interface *ifp)
268{
269 return ifp->flags & IFF_RUNNING;
270}
271
272/* Is the interface operative, eg. either UP & RUNNING
273 or UP & !ZEBRA_INTERFACE_LINK_DETECTION */
274int
275if_is_operative (struct interface *ifp)
276{
277 return ((ifp->flags & IFF_UP) &&
278 (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
279}
280
paul718e3742002-12-13 20:15:29 +0000281/* Is this loopback interface ? */
282int
283if_is_loopback (struct interface *ifp)
284{
285 return ifp->flags & IFF_LOOPBACK;
286}
287
288/* Does this interface support broadcast ? */
289int
290if_is_broadcast (struct interface *ifp)
291{
292 return ifp->flags & IFF_BROADCAST;
293}
294
295/* Does this interface support broadcast ? */
296int
297if_is_pointopoint (struct interface *ifp)
298{
299 return ifp->flags & IFF_POINTOPOINT;
300}
301
302/* Does this interface support multicast ? */
303int
304if_is_multicast (struct interface *ifp)
305{
306 return ifp->flags & IFF_MULTICAST;
307}
308
309/* Printout flag information into log */
310const char *
311if_flag_dump (unsigned long flag)
312{
313 int separator = 0;
314 static char logbuf[BUFSIZ];
315
316#define IFF_OUT_LOG(X,STR) \
317 if ((X) && (flag & (X))) \
318 { \
319 if (separator) \
320 strlcat (logbuf, ",", BUFSIZ); \
321 else \
322 separator = 1; \
323 strlcat (logbuf, STR, BUFSIZ); \
324 }
325
326 strlcpy (logbuf, " <", BUFSIZ);
327 IFF_OUT_LOG (IFF_UP, "UP");
328 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
329 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
330 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
331 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
332 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
333 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
334 IFF_OUT_LOG (IFF_NOARP, "NOARP");
335 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
336 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
337 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
338 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
339 IFF_OUT_LOG (IFF_LINK0, "LINK0");
340 IFF_OUT_LOG (IFF_LINK1, "LINK1");
341 IFF_OUT_LOG (IFF_LINK2, "LINK2");
342 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
343
344 strlcat (logbuf, ">", BUFSIZ);
345
346 return logbuf;
347}
348
349/* For debugging */
350void
351if_dump (struct interface *ifp)
352{
353 listnode node;
354
355 zlog_info ("Interface %s index %d metric %d mtu %d %s",
356 ifp->name, ifp->ifindex, ifp->metric, ifp->mtu,
357 if_flag_dump (ifp->flags));
358
359 for (node = listhead (ifp->connected); node; nextnode (node))
360 ;
361}
362
363/* Interface printing for all interface. */
364void
365if_dump_all ()
366{
367 listnode node;
368
369 for (node = listhead (iflist); node; nextnode (node))
370 if_dump (getdata (node));
371}
372
373DEFUN (interface_desc,
374 interface_desc_cmd,
375 "description .LINE",
376 "Interface specific description\n"
377 "Characters describing this interface\n")
378{
379 int i;
380 struct interface *ifp;
381 struct buffer *b;
382
383 if (argc == 0)
384 return CMD_SUCCESS;
385
386 ifp = vty->index;
387 if (ifp->desc)
388 XFREE (0, ifp->desc);
389
390 b = buffer_new (1024);
391 for (i = 0; i < argc; i++)
392 {
393 buffer_putstr (b, (u_char *)argv[i]);
394 buffer_putc (b, ' ');
395 }
396 buffer_putc (b, '\0');
397
398 ifp->desc = buffer_getstr (b);
399 buffer_free (b);
400
401 return CMD_SUCCESS;
402}
403
404DEFUN (no_interface_desc,
405 no_interface_desc_cmd,
406 "no description",
407 NO_STR
408 "Interface specific description\n")
409{
410 struct interface *ifp;
411
412 ifp = vty->index;
413 if (ifp->desc)
414 XFREE (0, ifp->desc);
415 ifp->desc = NULL;
416
417 return CMD_SUCCESS;
418}
419
420
421/* See also wrapper function zebra_interface() in zebra/interface.c */
422DEFUN (interface,
423 interface_cmd,
424 "interface IFNAME",
425 "Select an interface to configure\n"
426 "Interface's name\n")
427{
428 struct interface *ifp;
429
430 ifp = if_lookup_by_name (argv[0]);
431
432 if (ifp == NULL)
433 {
434 ifp = if_create ();
435 strncpy (ifp->name, argv[0], INTERFACE_NAMSIZ);
436 }
437 vty->index = ifp;
438 vty->node = INTERFACE_NODE;
439
440 return CMD_SUCCESS;
441}
442
443/* For debug purpose. */
444DEFUN (show_address,
445 show_address_cmd,
446 "show address",
447 SHOW_STR
448 "address\n")
449{
450 listnode node;
451 listnode node2;
452 struct interface *ifp;
453 struct connected *ifc;
454 struct prefix *p;
455
456 for (node = listhead (iflist); node; nextnode (node))
457 {
458 ifp = getdata (node);
459
460 for (node2 = listhead (ifp->connected); node2; nextnode (node2))
461 {
462 ifc = getdata (node2);
463 p = ifc->address;
464
465 if (p->family == AF_INET)
466 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
467 VTY_NEWLINE);
468 }
469 }
470 return CMD_SUCCESS;
471}
472
473/* Allocate connected structure. */
474struct connected *
475connected_new ()
476{
477 struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected));
478 memset (new, 0, sizeof (struct connected));
479 return new;
480}
481
482/* Free connected structure. */
483void
484connected_free (struct connected *connected)
485{
486 if (connected->address)
487 prefix_free (connected->address);
488
489 if (connected->destination)
490 prefix_free (connected->destination);
491
492 if (connected->label)
493 free (connected->label);
494
495 XFREE (MTYPE_CONNECTED, connected);
496}
497
498/* Print if_addr structure. */
499void
500connected_log (struct connected *connected, char *str)
501{
502 struct prefix *p;
503 struct interface *ifp;
504 char logbuf[BUFSIZ];
505 char buf[BUFSIZ];
506
507 ifp = connected->ifp;
508 p = connected->address;
509
510 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
511 str, ifp->name, prefix_family_str (p),
512 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
513 p->prefixlen);
514
515 p = connected->destination;
516 if (p)
517 {
518 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
519 BUFSIZ - strlen(logbuf));
520 }
521 zlog (NULL, LOG_INFO, logbuf);
522}
523
524/* If two connected address has same prefix return 1. */
525int
526connected_same_prefix (struct prefix *p1, struct prefix *p2)
527{
528 if (p1->family == p2->family)
529 {
530 if (p1->family == AF_INET &&
531 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
532 return 1;
533#ifdef HAVE_IPV6
534 if (p1->family == AF_INET6 &&
535 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
536 return 1;
537#endif /* HAVE_IPV6 */
538 }
539 return 0;
540}
541
542struct connected *
543connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
544{
545 struct listnode *node;
546 struct listnode *next;
547 struct connected *ifc;
548
549 /* In case of same prefix come, replace it with new one. */
550 for (node = listhead (ifp->connected); node; node = next)
551 {
552 ifc = getdata (node);
553 next = node->next;
554
555 if (connected_same_prefix (ifc->address, p))
556 {
557 listnode_delete (ifp->connected, ifc);
558 return ifc;
559 }
560 }
561 return NULL;
562}
563
paul727d1042002-12-13 20:50:29 +0000564/* Find the IPv4 address on our side that will be used when packets
565 are sent to dst. */
566struct connected *
567connected_lookup_address (struct interface *ifp, struct in_addr dst)
568{
569 struct prefix addr;
570 struct prefix best;
571 listnode cnode;
572 struct prefix *p;
573 struct connected *c;
574 struct connected *match;
575
576 /* Zero structures - get rid of rubbish from stack */
577 memset(&addr, 0, sizeof(addr));
578 memset(&best, 0, sizeof(best));
579
580 addr.family = AF_INET;
581 addr.u.prefix4 = dst;
582 addr.prefixlen = IPV4_MAX_BITLEN;
583
584 match = NULL;
585
586 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
587 {
588 c = getdata (cnode);
589
590 if (if_is_pointopoint (ifp))
591 {
592 p = c->address;
593
594 if (p && p->family == AF_INET)
595 {
596#ifdef OLD_RIB /* PTP links are conventionally identified
597 by the address of the far end - MAG */
598 if (IPV4_ADDR_SAME (&p->u.prefix4, &dst))
599 return c;
600#endif
601 p = c->destination;
602 if (p && IPV4_ADDR_SAME (&p->u.prefix4, &dst))
603 return c;
604 }
605 }
606 else
607 {
608 p = c->address;
609
610 if (p->family == AF_INET)
611 {
612 if (prefix_match (p, &addr) && p->prefixlen > best.prefixlen)
613 {
614 best = *p;
615 match = c;
616 }
617 }
618 }
619 }
620 return match;
621}
622
paul718e3742002-12-13 20:15:29 +0000623/* Check the connected information is PtP style or not. */
624int
625ifc_pointopoint (struct connected *ifc)
626{
627 struct prefix *p;
628 int ptp = 0;
629
630 /* When interface has PtP flag. */
631 if (if_is_pointopoint (ifc->ifp))
632 return 1;
633
634 /* RFC3021 PtP check. */
635 p = ifc->address;
636
637 if (p->family == AF_INET)
638 ptp = (p->prefixlen >= IPV4_MAX_PREFIXLEN - 1);
639#ifdef HAVE_IPV6
640 if (p->family == AF_INET6)
641 ptp = (p->prefixlen >= IPV6_MAX_PREFIXLEN - 1);
642#endif /* HAVE_IPV6 */
643
644 return ptp;
645}
646
647#ifndef HAVE_IF_NAMETOINDEX
648unsigned int
649if_nametoindex (const char *name)
650{
651 listnode node;
652 struct interface *ifp;
653
654 for (node = listhead (iflist); node; nextnode (node))
655 {
656 ifp = getdata (node);
657 if (strcmp (ifp->name, name) == 0)
658 return ifp->ifindex;
659 }
660 return 0;
661}
662#endif
663
664#ifndef HAVE_IF_INDEXTONAME
665char *
666if_indextoname (unsigned int ifindex, char *name)
667{
668 listnode node;
669 struct interface *ifp;
670
671 for (node = listhead (iflist); node; nextnode (node))
672 {
673 ifp = getdata (node);
674 if (ifp->ifindex == ifindex)
675 {
676 memcpy (name, ifp->name, IFNAMSIZ);
677 return ifp->name;
678 }
679 }
680 return NULL;
681}
682#endif
683
684/* Interface looking up by interface's address. */
685
686/* Interface's IPv4 address reverse lookup table. */
687struct route_table *ifaddr_ipv4_table;
688/* struct route_table *ifaddr_ipv6_table; */
689
690void
691ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
692{
693 struct route_node *rn;
694 struct prefix_ipv4 p;
695
696 p.family = AF_INET;
697 p.prefixlen = IPV4_MAX_PREFIXLEN;
698 p.prefix = *ifaddr;
699
700 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
701 if (rn)
702 {
703 route_unlock_node (rn);
704 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
705 inet_ntoa (*ifaddr));
706 return;
707 }
708 rn->info = ifp;
709}
710
711void
712ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
713{
714 struct route_node *rn;
715 struct prefix_ipv4 p;
716
717 p.family = AF_INET;
718 p.prefixlen = IPV4_MAX_PREFIXLEN;
719 p.prefix = *ifaddr;
720
721 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
722 if (! rn)
723 {
724 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
725 inet_ntoa (*ifaddr));
726 return;
727 }
728 rn->info = NULL;
729 route_unlock_node (rn);
730 route_unlock_node (rn);
731}
732
733/* Lookup interface by interface's IP address or interface index. */
734struct interface *
735ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
736{
737 struct prefix_ipv4 p;
738 struct route_node *rn;
739 struct interface *ifp;
740 listnode node;
741
742 if (addr)
743 {
744 p.family = AF_INET;
745 p.prefixlen = IPV4_MAX_PREFIXLEN;
746 p.prefix = *addr;
747
748 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
749 if (! rn)
750 return NULL;
751
752 ifp = rn->info;
753 route_unlock_node (rn);
754 return ifp;
755 }
756 else
757 {
758 for (node = listhead (iflist); node; nextnode (node))
759 {
760 ifp = getdata (node);
761
762 if (ifp->ifindex == ifindex)
763 return ifp;
764 }
765 }
766 return NULL;
767}
768
769/* Initialize interface list. */
770void
771if_init ()
772{
773 iflist = list_new ();
774 ifaddr_ipv4_table = route_table_init ();
775
776 if (iflist)
777 return;
778
779 memset (&if_master, 0, sizeof if_master);
780}