blob: bbf22ab1fa71aeacf29264a12a3f4e0c346c3643 [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
556/* Check the connected information is PtP style or not. */
557int
558ifc_pointopoint (struct connected *ifc)
559{
560 struct prefix *p;
561 int ptp = 0;
562
563 /* When interface has PtP flag. */
564 if (if_is_pointopoint (ifc->ifp))
565 return 1;
566
567 /* RFC3021 PtP check. */
568 p = ifc->address;
569
570 if (p->family == AF_INET)
571 ptp = (p->prefixlen >= IPV4_MAX_PREFIXLEN - 1);
572#ifdef HAVE_IPV6
573 if (p->family == AF_INET6)
574 ptp = (p->prefixlen >= IPV6_MAX_PREFIXLEN - 1);
575#endif /* HAVE_IPV6 */
576
577 return ptp;
578}
579
580#ifndef HAVE_IF_NAMETOINDEX
581unsigned int
582if_nametoindex (const char *name)
583{
584 listnode node;
585 struct interface *ifp;
586
587 for (node = listhead (iflist); node; nextnode (node))
588 {
589 ifp = getdata (node);
590 if (strcmp (ifp->name, name) == 0)
591 return ifp->ifindex;
592 }
593 return 0;
594}
595#endif
596
597#ifndef HAVE_IF_INDEXTONAME
598char *
599if_indextoname (unsigned int ifindex, char *name)
600{
601 listnode node;
602 struct interface *ifp;
603
604 for (node = listhead (iflist); node; nextnode (node))
605 {
606 ifp = getdata (node);
607 if (ifp->ifindex == ifindex)
608 {
609 memcpy (name, ifp->name, IFNAMSIZ);
610 return ifp->name;
611 }
612 }
613 return NULL;
614}
615#endif
616
617/* Interface looking up by interface's address. */
618
619/* Interface's IPv4 address reverse lookup table. */
620struct route_table *ifaddr_ipv4_table;
621/* struct route_table *ifaddr_ipv6_table; */
622
623void
624ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
625{
626 struct route_node *rn;
627 struct prefix_ipv4 p;
628
629 p.family = AF_INET;
630 p.prefixlen = IPV4_MAX_PREFIXLEN;
631 p.prefix = *ifaddr;
632
633 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
634 if (rn)
635 {
636 route_unlock_node (rn);
637 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
638 inet_ntoa (*ifaddr));
639 return;
640 }
641 rn->info = ifp;
642}
643
644void
645ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
646{
647 struct route_node *rn;
648 struct prefix_ipv4 p;
649
650 p.family = AF_INET;
651 p.prefixlen = IPV4_MAX_PREFIXLEN;
652 p.prefix = *ifaddr;
653
654 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
655 if (! rn)
656 {
657 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
658 inet_ntoa (*ifaddr));
659 return;
660 }
661 rn->info = NULL;
662 route_unlock_node (rn);
663 route_unlock_node (rn);
664}
665
666/* Lookup interface by interface's IP address or interface index. */
667struct interface *
668ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
669{
670 struct prefix_ipv4 p;
671 struct route_node *rn;
672 struct interface *ifp;
673 listnode node;
674
675 if (addr)
676 {
677 p.family = AF_INET;
678 p.prefixlen = IPV4_MAX_PREFIXLEN;
679 p.prefix = *addr;
680
681 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
682 if (! rn)
683 return NULL;
684
685 ifp = rn->info;
686 route_unlock_node (rn);
687 return ifp;
688 }
689 else
690 {
691 for (node = listhead (iflist); node; nextnode (node))
692 {
693 ifp = getdata (node);
694
695 if (ifp->ifindex == ifindex)
696 return ifp;
697 }
698 }
699 return NULL;
700}
701
702/* Initialize interface list. */
703void
704if_init ()
705{
706 iflist = list_new ();
707 ifaddr_ipv4_table = route_table_init ();
708
709 if (iflist)
710 return;
711
712 memset (&if_master, 0, sizeof if_master);
713}