blob: 0934e40d941a2a4067ed4839e32c416dcdafd374 [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;
192 listnode cnode;
193 struct interface *ifp;
194 struct prefix *p;
195 struct connected *c;
196 struct interface *match;
197
198 /* Zero structures - get rid of rubbish from stack */
199 memset(&addr, 0, sizeof(addr));
200 memset(&best, 0, sizeof(best));
201
202 addr.family = AF_INET;
203 addr.u.prefix4 = src;
204 addr.prefixlen = IPV4_MAX_BITLEN;
205
206 match = NULL;
207
208 for (node = listhead (iflist); node; nextnode (node))
209 {
210 ifp = getdata (node);
211
212 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
213 {
214 c = getdata (cnode);
215
216 if (if_is_pointopoint (ifp))
217 {
218 p = c->address;
219
220 if (p && p->family == AF_INET)
221 {
222#ifdef OLD_RIB /* PTP links are conventionally identified
223 by the address of the far end - MAG */
224 if (IPV4_ADDR_SAME (&p->u.prefix4, &src))
225 return ifp;
226#endif
227 p = c->destination;
228 if (p && IPV4_ADDR_SAME (&p->u.prefix4, &src))
229 return ifp;
230 }
231 }
232 else
233 {
234 p = c->address;
235
236 if (p->family == AF_INET)
237 {
238 if (prefix_match (p, &addr) && p->prefixlen > best.prefixlen)
239 {
240 best = *p;
241 match = ifp;
242 }
243 }
244 }
245 }
246 }
247 return match;
248}
249
250/* Get interface by name if given name interface doesn't exist create
251 one. */
252struct interface *
253if_get_by_name (char *name)
254{
255 struct interface *ifp;
256
257 ifp = if_lookup_by_name (name);
258 if (ifp == NULL)
259 {
260 ifp = if_create ();
261 strncpy (ifp->name, name, IFNAMSIZ);
262 }
263 return ifp;
264}
265
266/* Does interface up ? */
267int
268if_is_up (struct interface *ifp)
269{
270 return ifp->flags & IFF_UP;
271}
272
273/* Is this loopback interface ? */
274int
275if_is_loopback (struct interface *ifp)
276{
277 return ifp->flags & IFF_LOOPBACK;
278}
279
280/* Does this interface support broadcast ? */
281int
282if_is_broadcast (struct interface *ifp)
283{
284 return ifp->flags & IFF_BROADCAST;
285}
286
287/* Does this interface support broadcast ? */
288int
289if_is_pointopoint (struct interface *ifp)
290{
291 return ifp->flags & IFF_POINTOPOINT;
292}
293
294/* Does this interface support multicast ? */
295int
296if_is_multicast (struct interface *ifp)
297{
298 return ifp->flags & IFF_MULTICAST;
299}
300
301/* Printout flag information into log */
302const char *
303if_flag_dump (unsigned long flag)
304{
305 int separator = 0;
306 static char logbuf[BUFSIZ];
307
308#define IFF_OUT_LOG(X,STR) \
309 if ((X) && (flag & (X))) \
310 { \
311 if (separator) \
312 strlcat (logbuf, ",", BUFSIZ); \
313 else \
314 separator = 1; \
315 strlcat (logbuf, STR, BUFSIZ); \
316 }
317
318 strlcpy (logbuf, " <", BUFSIZ);
319 IFF_OUT_LOG (IFF_UP, "UP");
320 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
321 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
322 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
323 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
324 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
325 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
326 IFF_OUT_LOG (IFF_NOARP, "NOARP");
327 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
328 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
329 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
330 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
331 IFF_OUT_LOG (IFF_LINK0, "LINK0");
332 IFF_OUT_LOG (IFF_LINK1, "LINK1");
333 IFF_OUT_LOG (IFF_LINK2, "LINK2");
334 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
335
336 strlcat (logbuf, ">", BUFSIZ);
337
338 return logbuf;
339}
340
341/* For debugging */
342void
343if_dump (struct interface *ifp)
344{
345 listnode node;
346
347 zlog_info ("Interface %s index %d metric %d mtu %d %s",
348 ifp->name, ifp->ifindex, ifp->metric, ifp->mtu,
349 if_flag_dump (ifp->flags));
350
351 for (node = listhead (ifp->connected); node; nextnode (node))
352 ;
353}
354
355/* Interface printing for all interface. */
356void
357if_dump_all ()
358{
359 listnode node;
360
361 for (node = listhead (iflist); node; nextnode (node))
362 if_dump (getdata (node));
363}
364
365DEFUN (interface_desc,
366 interface_desc_cmd,
367 "description .LINE",
368 "Interface specific description\n"
369 "Characters describing this interface\n")
370{
371 int i;
372 struct interface *ifp;
373 struct buffer *b;
374
375 if (argc == 0)
376 return CMD_SUCCESS;
377
378 ifp = vty->index;
379 if (ifp->desc)
380 XFREE (0, ifp->desc);
381
382 b = buffer_new (1024);
383 for (i = 0; i < argc; i++)
384 {
385 buffer_putstr (b, (u_char *)argv[i]);
386 buffer_putc (b, ' ');
387 }
388 buffer_putc (b, '\0');
389
390 ifp->desc = buffer_getstr (b);
391 buffer_free (b);
392
393 return CMD_SUCCESS;
394}
395
396DEFUN (no_interface_desc,
397 no_interface_desc_cmd,
398 "no description",
399 NO_STR
400 "Interface specific description\n")
401{
402 struct interface *ifp;
403
404 ifp = vty->index;
405 if (ifp->desc)
406 XFREE (0, ifp->desc);
407 ifp->desc = NULL;
408
409 return CMD_SUCCESS;
410}
411
412
413/* See also wrapper function zebra_interface() in zebra/interface.c */
414DEFUN (interface,
415 interface_cmd,
416 "interface IFNAME",
417 "Select an interface to configure\n"
418 "Interface's name\n")
419{
420 struct interface *ifp;
421
422 ifp = if_lookup_by_name (argv[0]);
423
424 if (ifp == NULL)
425 {
426 ifp = if_create ();
427 strncpy (ifp->name, argv[0], INTERFACE_NAMSIZ);
428 }
429 vty->index = ifp;
430 vty->node = INTERFACE_NODE;
431
432 return CMD_SUCCESS;
433}
434
435/* For debug purpose. */
436DEFUN (show_address,
437 show_address_cmd,
438 "show address",
439 SHOW_STR
440 "address\n")
441{
442 listnode node;
443 listnode node2;
444 struct interface *ifp;
445 struct connected *ifc;
446 struct prefix *p;
447
448 for (node = listhead (iflist); node; nextnode (node))
449 {
450 ifp = getdata (node);
451
452 for (node2 = listhead (ifp->connected); node2; nextnode (node2))
453 {
454 ifc = getdata (node2);
455 p = ifc->address;
456
457 if (p->family == AF_INET)
458 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
459 VTY_NEWLINE);
460 }
461 }
462 return CMD_SUCCESS;
463}
464
465/* Allocate connected structure. */
466struct connected *
467connected_new ()
468{
469 struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected));
470 memset (new, 0, sizeof (struct connected));
471 return new;
472}
473
474/* Free connected structure. */
475void
476connected_free (struct connected *connected)
477{
478 if (connected->address)
479 prefix_free (connected->address);
480
481 if (connected->destination)
482 prefix_free (connected->destination);
483
484 if (connected->label)
485 free (connected->label);
486
487 XFREE (MTYPE_CONNECTED, connected);
488}
489
490/* Print if_addr structure. */
491void
492connected_log (struct connected *connected, char *str)
493{
494 struct prefix *p;
495 struct interface *ifp;
496 char logbuf[BUFSIZ];
497 char buf[BUFSIZ];
498
499 ifp = connected->ifp;
500 p = connected->address;
501
502 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
503 str, ifp->name, prefix_family_str (p),
504 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
505 p->prefixlen);
506
507 p = connected->destination;
508 if (p)
509 {
510 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
511 BUFSIZ - strlen(logbuf));
512 }
513 zlog (NULL, LOG_INFO, logbuf);
514}
515
516/* If two connected address has same prefix return 1. */
517int
518connected_same_prefix (struct prefix *p1, struct prefix *p2)
519{
520 if (p1->family == p2->family)
521 {
522 if (p1->family == AF_INET &&
523 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
524 return 1;
525#ifdef HAVE_IPV6
526 if (p1->family == AF_INET6 &&
527 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
528 return 1;
529#endif /* HAVE_IPV6 */
530 }
531 return 0;
532}
533
534struct connected *
535connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
536{
537 struct listnode *node;
538 struct listnode *next;
539 struct connected *ifc;
540
541 /* In case of same prefix come, replace it with new one. */
542 for (node = listhead (ifp->connected); node; node = next)
543 {
544 ifc = getdata (node);
545 next = node->next;
546
547 if (connected_same_prefix (ifc->address, p))
548 {
549 listnode_delete (ifp->connected, ifc);
550 return ifc;
551 }
552 }
553 return NULL;
554}
555
paul727d1042002-12-13 20:50:29 +0000556/* Find the IPv4 address on our side that will be used when packets
557 are sent to dst. */
558struct connected *
559connected_lookup_address (struct interface *ifp, struct in_addr dst)
560{
561 struct prefix addr;
562 struct prefix best;
563 listnode cnode;
564 struct prefix *p;
565 struct connected *c;
566 struct connected *match;
567
568 /* Zero structures - get rid of rubbish from stack */
569 memset(&addr, 0, sizeof(addr));
570 memset(&best, 0, sizeof(best));
571
572 addr.family = AF_INET;
573 addr.u.prefix4 = dst;
574 addr.prefixlen = IPV4_MAX_BITLEN;
575
576 match = NULL;
577
578 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
579 {
580 c = getdata (cnode);
581
582 if (if_is_pointopoint (ifp))
583 {
584 p = c->address;
585
586 if (p && p->family == AF_INET)
587 {
588#ifdef OLD_RIB /* PTP links are conventionally identified
589 by the address of the far end - MAG */
590 if (IPV4_ADDR_SAME (&p->u.prefix4, &dst))
591 return c;
592#endif
593 p = c->destination;
594 if (p && IPV4_ADDR_SAME (&p->u.prefix4, &dst))
595 return c;
596 }
597 }
598 else
599 {
600 p = c->address;
601
602 if (p->family == AF_INET)
603 {
604 if (prefix_match (p, &addr) && p->prefixlen > best.prefixlen)
605 {
606 best = *p;
607 match = c;
608 }
609 }
610 }
611 }
612 return match;
613}
614
paul718e3742002-12-13 20:15:29 +0000615/* Check the connected information is PtP style or not. */
616int
617ifc_pointopoint (struct connected *ifc)
618{
619 struct prefix *p;
620 int ptp = 0;
621
622 /* When interface has PtP flag. */
623 if (if_is_pointopoint (ifc->ifp))
624 return 1;
625
626 /* RFC3021 PtP check. */
627 p = ifc->address;
628
629 if (p->family == AF_INET)
630 ptp = (p->prefixlen >= IPV4_MAX_PREFIXLEN - 1);
631#ifdef HAVE_IPV6
632 if (p->family == AF_INET6)
633 ptp = (p->prefixlen >= IPV6_MAX_PREFIXLEN - 1);
634#endif /* HAVE_IPV6 */
635
636 return ptp;
637}
638
639#ifndef HAVE_IF_NAMETOINDEX
640unsigned int
641if_nametoindex (const char *name)
642{
643 listnode node;
644 struct interface *ifp;
645
646 for (node = listhead (iflist); node; nextnode (node))
647 {
648 ifp = getdata (node);
649 if (strcmp (ifp->name, name) == 0)
650 return ifp->ifindex;
651 }
652 return 0;
653}
654#endif
655
656#ifndef HAVE_IF_INDEXTONAME
657char *
658if_indextoname (unsigned int ifindex, char *name)
659{
660 listnode node;
661 struct interface *ifp;
662
663 for (node = listhead (iflist); node; nextnode (node))
664 {
665 ifp = getdata (node);
666 if (ifp->ifindex == ifindex)
667 {
668 memcpy (name, ifp->name, IFNAMSIZ);
669 return ifp->name;
670 }
671 }
672 return NULL;
673}
674#endif
675
676/* Interface looking up by interface's address. */
677
678/* Interface's IPv4 address reverse lookup table. */
679struct route_table *ifaddr_ipv4_table;
680/* struct route_table *ifaddr_ipv6_table; */
681
682void
683ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
684{
685 struct route_node *rn;
686 struct prefix_ipv4 p;
687
688 p.family = AF_INET;
689 p.prefixlen = IPV4_MAX_PREFIXLEN;
690 p.prefix = *ifaddr;
691
692 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
693 if (rn)
694 {
695 route_unlock_node (rn);
696 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
697 inet_ntoa (*ifaddr));
698 return;
699 }
700 rn->info = ifp;
701}
702
703void
704ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
705{
706 struct route_node *rn;
707 struct prefix_ipv4 p;
708
709 p.family = AF_INET;
710 p.prefixlen = IPV4_MAX_PREFIXLEN;
711 p.prefix = *ifaddr;
712
713 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
714 if (! rn)
715 {
716 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
717 inet_ntoa (*ifaddr));
718 return;
719 }
720 rn->info = NULL;
721 route_unlock_node (rn);
722 route_unlock_node (rn);
723}
724
725/* Lookup interface by interface's IP address or interface index. */
726struct interface *
727ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
728{
729 struct prefix_ipv4 p;
730 struct route_node *rn;
731 struct interface *ifp;
732 listnode node;
733
734 if (addr)
735 {
736 p.family = AF_INET;
737 p.prefixlen = IPV4_MAX_PREFIXLEN;
738 p.prefix = *addr;
739
740 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
741 if (! rn)
742 return NULL;
743
744 ifp = rn->info;
745 route_unlock_node (rn);
746 return ifp;
747 }
748 else
749 {
750 for (node = listhead (iflist); node; nextnode (node))
751 {
752 ifp = getdata (node);
753
754 if (ifp->ifindex == ifindex)
755 return ifp;
756 }
757 }
758 return NULL;
759}
760
761/* Initialize interface list. */
762void
763if_init ()
764{
765 iflist = list_new ();
766 ifaddr_ipv4_table = route_table_init ();
767
768 if (iflist)
769 return;
770
771 memset (&if_master, 0, sizeof if_master);
772}