blob: 2ca8c94bb0d3e59e54de95d7d8218bd46f2e309b [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
paul32d24632003-05-23 09:25:20 +0000443DEFUN_NOSH (no_interface,
444 no_interface_cmd,
445 "no interface IFNAME",
446 NO_STR
447 "Delete a pseudo interface's configuration\n"
448 "Interface's name\n")
449{
450 // deleting interface
451 struct interface *ifp;
452
453 ifp = if_lookup_by_name (argv[0]);
454
455 if (ifp == NULL)
456 return CMD_SUCCESS;
457
458 if (if_is_up(ifp)) {
459 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
460 VTY_NEWLINE);
461 return CMD_WARNING;
462 }
463
464 if_delete(ifp);
465
466 return CMD_SUCCESS;
467}
468
paul718e3742002-12-13 20:15:29 +0000469/* For debug purpose. */
470DEFUN (show_address,
471 show_address_cmd,
472 "show address",
473 SHOW_STR
474 "address\n")
475{
476 listnode node;
477 listnode node2;
478 struct interface *ifp;
479 struct connected *ifc;
480 struct prefix *p;
481
482 for (node = listhead (iflist); node; nextnode (node))
483 {
484 ifp = getdata (node);
485
486 for (node2 = listhead (ifp->connected); node2; nextnode (node2))
487 {
488 ifc = getdata (node2);
489 p = ifc->address;
490
491 if (p->family == AF_INET)
492 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
493 VTY_NEWLINE);
494 }
495 }
496 return CMD_SUCCESS;
497}
498
499/* Allocate connected structure. */
500struct connected *
501connected_new ()
502{
503 struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected));
504 memset (new, 0, sizeof (struct connected));
505 return new;
506}
507
508/* Free connected structure. */
509void
510connected_free (struct connected *connected)
511{
512 if (connected->address)
513 prefix_free (connected->address);
514
515 if (connected->destination)
516 prefix_free (connected->destination);
517
518 if (connected->label)
519 free (connected->label);
520
521 XFREE (MTYPE_CONNECTED, connected);
522}
523
524/* Print if_addr structure. */
525void
526connected_log (struct connected *connected, char *str)
527{
528 struct prefix *p;
529 struct interface *ifp;
530 char logbuf[BUFSIZ];
531 char buf[BUFSIZ];
532
533 ifp = connected->ifp;
534 p = connected->address;
535
536 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
537 str, ifp->name, prefix_family_str (p),
538 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
539 p->prefixlen);
540
541 p = connected->destination;
542 if (p)
543 {
544 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
545 BUFSIZ - strlen(logbuf));
546 }
547 zlog (NULL, LOG_INFO, logbuf);
548}
549
550/* If two connected address has same prefix return 1. */
551int
552connected_same_prefix (struct prefix *p1, struct prefix *p2)
553{
554 if (p1->family == p2->family)
555 {
556 if (p1->family == AF_INET &&
557 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
558 return 1;
559#ifdef HAVE_IPV6
560 if (p1->family == AF_INET6 &&
561 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
562 return 1;
563#endif /* HAVE_IPV6 */
564 }
565 return 0;
566}
567
568struct connected *
569connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
570{
571 struct listnode *node;
572 struct listnode *next;
573 struct connected *ifc;
574
575 /* In case of same prefix come, replace it with new one. */
576 for (node = listhead (ifp->connected); node; node = next)
577 {
578 ifc = getdata (node);
579 next = node->next;
580
581 if (connected_same_prefix (ifc->address, p))
582 {
583 listnode_delete (ifp->connected, ifc);
584 return ifc;
585 }
586 }
587 return NULL;
588}
589
paul727d1042002-12-13 20:50:29 +0000590/* Find the IPv4 address on our side that will be used when packets
591 are sent to dst. */
592struct connected *
593connected_lookup_address (struct interface *ifp, struct in_addr dst)
594{
595 struct prefix addr;
596 struct prefix best;
597 listnode cnode;
598 struct prefix *p;
599 struct connected *c;
600 struct connected *match;
601
602 /* Zero structures - get rid of rubbish from stack */
603 memset(&addr, 0, sizeof(addr));
604 memset(&best, 0, sizeof(best));
605
606 addr.family = AF_INET;
607 addr.u.prefix4 = dst;
608 addr.prefixlen = IPV4_MAX_BITLEN;
609
610 match = NULL;
611
612 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
613 {
614 c = getdata (cnode);
615
616 if (if_is_pointopoint (ifp))
617 {
618 p = c->address;
619
620 if (p && p->family == AF_INET)
621 {
622#ifdef OLD_RIB /* PTP links are conventionally identified
623 by the address of the far end - MAG */
624 if (IPV4_ADDR_SAME (&p->u.prefix4, &dst))
625 return c;
626#endif
627 p = c->destination;
628 if (p && IPV4_ADDR_SAME (&p->u.prefix4, &dst))
629 return c;
630 }
631 }
632 else
633 {
634 p = c->address;
635
636 if (p->family == AF_INET)
637 {
638 if (prefix_match (p, &addr) && p->prefixlen > best.prefixlen)
639 {
640 best = *p;
641 match = c;
642 }
643 }
644 }
645 }
646 return match;
647}
648
paul718e3742002-12-13 20:15:29 +0000649/* Check the connected information is PtP style or not. */
650int
651ifc_pointopoint (struct connected *ifc)
652{
653 struct prefix *p;
654 int ptp = 0;
655
656 /* When interface has PtP flag. */
657 if (if_is_pointopoint (ifc->ifp))
658 return 1;
659
660 /* RFC3021 PtP check. */
661 p = ifc->address;
662
663 if (p->family == AF_INET)
664 ptp = (p->prefixlen >= IPV4_MAX_PREFIXLEN - 1);
665#ifdef HAVE_IPV6
666 if (p->family == AF_INET6)
667 ptp = (p->prefixlen >= IPV6_MAX_PREFIXLEN - 1);
668#endif /* HAVE_IPV6 */
669
670 return ptp;
671}
672
673#ifndef HAVE_IF_NAMETOINDEX
674unsigned int
675if_nametoindex (const char *name)
676{
677 listnode node;
678 struct interface *ifp;
679
680 for (node = listhead (iflist); node; nextnode (node))
681 {
682 ifp = getdata (node);
683 if (strcmp (ifp->name, name) == 0)
684 return ifp->ifindex;
685 }
686 return 0;
687}
688#endif
689
690#ifndef HAVE_IF_INDEXTONAME
691char *
692if_indextoname (unsigned int ifindex, char *name)
693{
694 listnode node;
695 struct interface *ifp;
696
697 for (node = listhead (iflist); node; nextnode (node))
698 {
699 ifp = getdata (node);
700 if (ifp->ifindex == ifindex)
701 {
702 memcpy (name, ifp->name, IFNAMSIZ);
703 return ifp->name;
704 }
705 }
706 return NULL;
707}
708#endif
709
710/* Interface looking up by interface's address. */
711
712/* Interface's IPv4 address reverse lookup table. */
713struct route_table *ifaddr_ipv4_table;
714/* struct route_table *ifaddr_ipv6_table; */
715
716void
717ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
718{
719 struct route_node *rn;
720 struct prefix_ipv4 p;
721
722 p.family = AF_INET;
723 p.prefixlen = IPV4_MAX_PREFIXLEN;
724 p.prefix = *ifaddr;
725
726 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
727 if (rn)
728 {
729 route_unlock_node (rn);
730 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
731 inet_ntoa (*ifaddr));
732 return;
733 }
734 rn->info = ifp;
735}
736
737void
738ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
739{
740 struct route_node *rn;
741 struct prefix_ipv4 p;
742
743 p.family = AF_INET;
744 p.prefixlen = IPV4_MAX_PREFIXLEN;
745 p.prefix = *ifaddr;
746
747 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
748 if (! rn)
749 {
750 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
751 inet_ntoa (*ifaddr));
752 return;
753 }
754 rn->info = NULL;
755 route_unlock_node (rn);
756 route_unlock_node (rn);
757}
758
759/* Lookup interface by interface's IP address or interface index. */
760struct interface *
761ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
762{
763 struct prefix_ipv4 p;
764 struct route_node *rn;
765 struct interface *ifp;
766 listnode node;
767
768 if (addr)
769 {
770 p.family = AF_INET;
771 p.prefixlen = IPV4_MAX_PREFIXLEN;
772 p.prefix = *addr;
773
774 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
775 if (! rn)
776 return NULL;
777
778 ifp = rn->info;
779 route_unlock_node (rn);
780 return ifp;
781 }
782 else
783 {
784 for (node = listhead (iflist); node; nextnode (node))
785 {
786 ifp = getdata (node);
787
788 if (ifp->ifindex == ifindex)
789 return ifp;
790 }
791 }
792 return NULL;
793}
794
795/* Initialize interface list. */
796void
797if_init ()
798{
799 iflist = list_new ();
800 ifaddr_ipv4_table = route_table_init ();
801
802 if (iflist)
803 return;
804
805 memset (&if_master, 0, sizeof if_master);
806}