blob: ff5ec50aaca18c84eb38f36e5d224ba06ef684cb [file] [log] [blame]
paul106d2fd2003-08-01 00:24:13 +00001
paul718e3742002-12-13 20:15:29 +00002/*
3 * Interface functions.
4 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 *
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.
12 *
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.
17 *
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.
22 */
23
24#include <zebra.h>
25
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"
paul718e3742002-12-13 20:15:29 +000033#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
paul106d2fd2003-08-01 00:24:13 +000049/* Compare interface names */
50int
51if_cmp_func (struct interface *ifp1, struct interface *ifp2)
52{
53 unsigned int l1, l2;
54 long int x1, x2;
55 char *p1, *p2;
56 int res;
57
58 p1 = ifp1->name;
59 p2 = ifp2->name;
60
paul90578522003-09-23 23:46:01 +000061 while (*p1 && *p2) {
paul106d2fd2003-08-01 00:24:13 +000062 /* look up to any number */
63 l1 = strcspn(p1, "0123456789");
64 l2 = strcspn(p2, "0123456789");
65
66 /* name lengths are different -> compare names */
67 if (l1 != l2)
68 return (strcmp(p1, p2));
69
70 res = strncmp(p1, p2, l1);
71
72 /* names are different -> compare them */
73 if (res)
74 return res;
75
76 /* with identical name part, go to numeric part */
77
78 p1 += l1;
79 p2 += l1;
80
81 x1 = strtol(p1, &p1, 10);
82 x2 = strtol(p2, &p2, 10);
83
84 /* let's compare numbers now */
85 if (x1 < x2)
86 return -1;
87 if (x1 > x2)
88 return 1;
89
90 /* numbers were equal, lets do it again..
91 (it happens with name like "eth123.456:789") */
92 }
paul90578522003-09-23 23:46:01 +000093 if (*p1)
94 return 1;
95 if (*p2)
96 return -1;
97 return 0;
paul106d2fd2003-08-01 00:24:13 +000098}
99
paul718e3742002-12-13 20:15:29 +0000100/* Create new interface structure. */
101struct interface *
102if_new ()
103{
104 struct interface *ifp;
105
106 ifp = XMALLOC (MTYPE_IF, sizeof (struct interface));
107 memset (ifp, 0, sizeof (struct interface));
108 return ifp;
109}
110
111struct interface *
paul106d2fd2003-08-01 00:24:13 +0000112if_create (char *name, int namelen)
paul718e3742002-12-13 20:15:29 +0000113{
114 struct interface *ifp;
115
116 ifp = if_new ();
117
paul106d2fd2003-08-01 00:24:13 +0000118 assert (name);
119 assert (namelen <= (INTERFACE_NAMSIZ + 1));
120 strncpy (ifp->name, name, namelen);
121 ifp->name[INTERFACE_NAMSIZ] = '\0';
hassoe90fbab2003-12-21 09:51:42 +0000122 if (if_lookup_by_name(ifp->name) == NULL)
123 listnode_add_sort (iflist, ifp);
paul718e3742002-12-13 20:15:29 +0000124 ifp->connected = list_new ();
125 ifp->connected->del = (void (*) (void *)) connected_free;
126
127 if (if_master.if_new_hook)
128 (*if_master.if_new_hook) (ifp);
129
130 return ifp;
131}
132
133/* Delete and free interface structure. */
134void
135if_delete (struct interface *ifp)
136{
137 listnode_delete (iflist, ifp);
138
139 if (if_master.if_delete_hook)
140 (*if_master.if_delete_hook) (ifp);
141
142 /* Free connected address list */
143 list_delete (ifp->connected);
144
145 XFREE (MTYPE_IF, ifp);
146}
147
148/* Add hook to interface master. */
149void
150if_add_hook (int type, int (*func)(struct interface *ifp))
151{
152 switch (type) {
153 case IF_NEW_HOOK:
154 if_master.if_new_hook = func;
155 break;
156 case IF_DELETE_HOOK:
157 if_master.if_delete_hook = func;
158 break;
159 default:
160 break;
161 }
162}
163
164/* Interface existance check by index. */
165struct interface *
166if_lookup_by_index (unsigned int index)
167{
168 listnode node;
169 struct interface *ifp;
170
171 for (node = listhead (iflist); node; nextnode (node))
172 {
173 ifp = getdata (node);
174 if (ifp->ifindex == index)
175 return ifp;
176 }
177 return NULL;
178}
179
180char *
181ifindex2ifname (unsigned int index)
182{
183 listnode node;
184 struct interface *ifp;
185
186 for (node = listhead (iflist); node; nextnode (node))
187 {
188 ifp = getdata (node);
189 if (ifp->ifindex == index)
190 return ifp->name;
191 }
192 return "unknown";
193}
194
195/* Interface existance check by interface name. */
196struct interface *
197if_lookup_by_name (char *name)
198{
199 listnode node;
200 struct interface *ifp;
201
202 for (node = listhead (iflist); node; nextnode (node))
203 {
204 ifp = getdata (node);
205 if (strncmp (name, ifp->name, sizeof ifp->name) == 0)
206 return ifp;
207 }
208 return NULL;
209}
210
211/* Lookup interface by IPv4 address. */
212struct interface *
213if_lookup_exact_address (struct in_addr src)
214{
215 listnode node;
216 listnode cnode;
217 struct interface *ifp;
218 struct prefix *p;
219 struct connected *c;
220
221 for (node = listhead (iflist); node; nextnode (node))
222 {
223 ifp = getdata (node);
224
225 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
226 {
227 c = getdata (cnode);
228
229 p = c->address;
230
231 if (p && p->family == AF_INET)
232 {
233 if (IPV4_ADDR_SAME (&p->u.prefix4, &src))
234 return ifp;
235 }
236 }
237 }
238 return NULL;
239}
240
241/* Lookup interface by IPv4 address. */
242struct interface *
243if_lookup_address (struct in_addr src)
244{
245 listnode node;
246 struct prefix addr;
247 struct prefix best;
248 listnode cnode;
249 struct interface *ifp;
250 struct prefix *p;
251 struct connected *c;
252 struct interface *match;
253
254 /* Zero structures - get rid of rubbish from stack */
255 memset(&addr, 0, sizeof(addr));
256 memset(&best, 0, sizeof(best));
257
258 addr.family = AF_INET;
259 addr.u.prefix4 = src;
260 addr.prefixlen = IPV4_MAX_BITLEN;
261
262 match = NULL;
263
264 for (node = listhead (iflist); node; nextnode (node))
265 {
266 ifp = getdata (node);
267
268 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
269 {
270 c = getdata (cnode);
271
paul31a476c2003-09-29 19:54:53 +0000272 if (if_is_pointopoint (ifp))
paul718e3742002-12-13 20:15:29 +0000273 {
paul31a476c2003-09-29 19:54:53 +0000274 p = c->address;
paul718e3742002-12-13 20:15:29 +0000275
paul31a476c2003-09-29 19:54:53 +0000276 if (p && p->family == AF_INET)
paul718e3742002-12-13 20:15:29 +0000277 {
paul31a476c2003-09-29 19:54:53 +0000278#ifdef OLD_RIB /* PTP links are conventionally identified
279 by the address of the far end - MAG */
280 if (IPV4_ADDR_SAME (&p->u.prefix4, &src))
281 return ifp;
282#endif
283 p = c->destination;
284 if (p && IPV4_ADDR_SAME (&p->u.prefix4, &src))
285 return ifp;
paul718e3742002-12-13 20:15:29 +0000286 }
paul31a476c2003-09-29 19:54:53 +0000287 }
288 else
289 {
290 p = c->address;
paul718e3742002-12-13 20:15:29 +0000291
paul31a476c2003-09-29 19:54:53 +0000292 if (p->family == AF_INET)
paul718e3742002-12-13 20:15:29 +0000293 {
paul31a476c2003-09-29 19:54:53 +0000294 if (prefix_match (p, &addr) && p->prefixlen > best.prefixlen)
295 {
296 best = *p;
297 match = ifp;
298 }
paul718e3742002-12-13 20:15:29 +0000299 }
300 }
301 }
302 }
303 return match;
304}
305
306/* Get interface by name if given name interface doesn't exist create
307 one. */
308struct interface *
309if_get_by_name (char *name)
310{
311 struct interface *ifp;
312
313 ifp = if_lookup_by_name (name);
314 if (ifp == NULL)
paul106d2fd2003-08-01 00:24:13 +0000315 ifp = if_create (name, INTERFACE_NAMSIZ);
paul718e3742002-12-13 20:15:29 +0000316 return ifp;
317}
318
319/* Does interface up ? */
320int
321if_is_up (struct interface *ifp)
322{
323 return ifp->flags & IFF_UP;
324}
325
paul2e3b2e42002-12-13 21:03:13 +0000326/* Is interface running? */
327int
328if_is_running (struct interface *ifp)
329{
330 return ifp->flags & IFF_RUNNING;
331}
332
333/* Is the interface operative, eg. either UP & RUNNING
334 or UP & !ZEBRA_INTERFACE_LINK_DETECTION */
335int
336if_is_operative (struct interface *ifp)
337{
338 return ((ifp->flags & IFF_UP) &&
339 (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
340}
341
paul718e3742002-12-13 20:15:29 +0000342/* Is this loopback interface ? */
343int
344if_is_loopback (struct interface *ifp)
345{
346 return ifp->flags & IFF_LOOPBACK;
347}
348
349/* Does this interface support broadcast ? */
350int
351if_is_broadcast (struct interface *ifp)
352{
353 return ifp->flags & IFF_BROADCAST;
354}
355
356/* Does this interface support broadcast ? */
357int
358if_is_pointopoint (struct interface *ifp)
359{
360 return ifp->flags & IFF_POINTOPOINT;
361}
362
363/* Does this interface support multicast ? */
364int
365if_is_multicast (struct interface *ifp)
366{
367 return ifp->flags & IFF_MULTICAST;
368}
369
370/* Printout flag information into log */
371const char *
372if_flag_dump (unsigned long flag)
373{
374 int separator = 0;
375 static char logbuf[BUFSIZ];
376
377#define IFF_OUT_LOG(X,STR) \
378 if ((X) && (flag & (X))) \
379 { \
380 if (separator) \
381 strlcat (logbuf, ",", BUFSIZ); \
382 else \
383 separator = 1; \
384 strlcat (logbuf, STR, BUFSIZ); \
385 }
386
387 strlcpy (logbuf, " <", BUFSIZ);
388 IFF_OUT_LOG (IFF_UP, "UP");
389 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
390 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
391 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
392 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
393 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
394 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
395 IFF_OUT_LOG (IFF_NOARP, "NOARP");
396 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
397 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
398 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
399 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
400 IFF_OUT_LOG (IFF_LINK0, "LINK0");
401 IFF_OUT_LOG (IFF_LINK1, "LINK1");
402 IFF_OUT_LOG (IFF_LINK2, "LINK2");
403 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
paul4a7aac12004-05-08 05:00:31 +0000404#ifdef SOLARIS_IPV6
405 IFF_OUT_LOG (IFF_IPV4, "IFF_IPv4");
406 IFF_OUT_LOG (IFF_IPV6, "IFF_IPv6");
407#endif /* SOLARIS_IPV6 */
paul718e3742002-12-13 20:15:29 +0000408
409 strlcat (logbuf, ">", BUFSIZ);
410
411 return logbuf;
412}
413
414/* For debugging */
415void
416if_dump (struct interface *ifp)
417{
418 listnode node;
419
paul4a7aac12004-05-08 05:00:31 +0000420 zlog_info ("Interface %s index %d metric %d mtu %d "
421#ifdef HAVE_IPV6
422 "mtu6 %d "
423#endif /* HAVE_IPV6 */
424 "%s",
paul718e3742002-12-13 20:15:29 +0000425 ifp->name, ifp->ifindex, ifp->metric, ifp->mtu,
paul4a7aac12004-05-08 05:00:31 +0000426#ifdef HAVE_IPV6
427 ifp->mtu6,
428#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +0000429 if_flag_dump (ifp->flags));
430
431 for (node = listhead (ifp->connected); node; nextnode (node))
432 ;
433}
434
435/* Interface printing for all interface. */
436void
437if_dump_all ()
438{
439 listnode node;
440
441 for (node = listhead (iflist); node; nextnode (node))
442 if_dump (getdata (node));
443}
444
445DEFUN (interface_desc,
446 interface_desc_cmd,
447 "description .LINE",
448 "Interface specific description\n"
449 "Characters describing this interface\n")
450{
451 int i;
452 struct interface *ifp;
453 struct buffer *b;
454
455 if (argc == 0)
456 return CMD_SUCCESS;
457
458 ifp = vty->index;
459 if (ifp->desc)
460 XFREE (0, ifp->desc);
461
462 b = buffer_new (1024);
463 for (i = 0; i < argc; i++)
464 {
paul02ff83c2004-06-11 11:27:03 +0000465 buffer_putstr (b, argv[i]);
paul718e3742002-12-13 20:15:29 +0000466 buffer_putc (b, ' ');
467 }
468 buffer_putc (b, '\0');
469
470 ifp->desc = buffer_getstr (b);
471 buffer_free (b);
472
473 return CMD_SUCCESS;
474}
475
476DEFUN (no_interface_desc,
477 no_interface_desc_cmd,
478 "no description",
479 NO_STR
480 "Interface specific description\n")
481{
482 struct interface *ifp;
483
484 ifp = vty->index;
485 if (ifp->desc)
486 XFREE (0, ifp->desc);
487 ifp->desc = NULL;
488
489 return CMD_SUCCESS;
490}
491
492
493/* See also wrapper function zebra_interface() in zebra/interface.c */
494DEFUN (interface,
495 interface_cmd,
496 "interface IFNAME",
497 "Select an interface to configure\n"
498 "Interface's name\n")
499{
500 struct interface *ifp;
501
502 ifp = if_lookup_by_name (argv[0]);
503
504 if (ifp == NULL)
paul106d2fd2003-08-01 00:24:13 +0000505 ifp = if_create (argv[0], INTERFACE_NAMSIZ);
paul718e3742002-12-13 20:15:29 +0000506 vty->index = ifp;
507 vty->node = INTERFACE_NODE;
508
509 return CMD_SUCCESS;
510}
511
paul32d24632003-05-23 09:25:20 +0000512DEFUN_NOSH (no_interface,
513 no_interface_cmd,
514 "no interface IFNAME",
515 NO_STR
516 "Delete a pseudo interface's configuration\n"
517 "Interface's name\n")
518{
519 // deleting interface
520 struct interface *ifp;
521
522 ifp = if_lookup_by_name (argv[0]);
523
524 if (ifp == NULL)
paulbfc13532003-05-24 06:40:04 +0000525 {
526 vty_out (vty, "%% Inteface %s does not exist%s", argv[0], VTY_NEWLINE);
527 return CMD_WARNING;
528 }
paul32d24632003-05-23 09:25:20 +0000529
paulbfc13532003-05-24 06:40:04 +0000530 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
531 {
paul32d24632003-05-23 09:25:20 +0000532 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
533 VTY_NEWLINE);
534 return CMD_WARNING;
535 }
536
537 if_delete(ifp);
538
539 return CMD_SUCCESS;
540}
541
paul718e3742002-12-13 20:15:29 +0000542/* For debug purpose. */
543DEFUN (show_address,
544 show_address_cmd,
545 "show address",
546 SHOW_STR
547 "address\n")
548{
549 listnode node;
550 listnode node2;
551 struct interface *ifp;
552 struct connected *ifc;
553 struct prefix *p;
554
555 for (node = listhead (iflist); node; nextnode (node))
556 {
557 ifp = getdata (node);
558
559 for (node2 = listhead (ifp->connected); node2; nextnode (node2))
560 {
561 ifc = getdata (node2);
562 p = ifc->address;
563
564 if (p->family == AF_INET)
565 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
566 VTY_NEWLINE);
567 }
568 }
569 return CMD_SUCCESS;
570}
571
572/* Allocate connected structure. */
573struct connected *
574connected_new ()
575{
576 struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected));
577 memset (new, 0, sizeof (struct connected));
578 return new;
579}
580
581/* Free connected structure. */
582void
583connected_free (struct connected *connected)
584{
585 if (connected->address)
586 prefix_free (connected->address);
587
588 if (connected->destination)
589 prefix_free (connected->destination);
590
591 if (connected->label)
592 free (connected->label);
593
594 XFREE (MTYPE_CONNECTED, connected);
595}
596
597/* Print if_addr structure. */
598void
599connected_log (struct connected *connected, char *str)
600{
601 struct prefix *p;
602 struct interface *ifp;
603 char logbuf[BUFSIZ];
604 char buf[BUFSIZ];
605
606 ifp = connected->ifp;
607 p = connected->address;
608
609 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
610 str, ifp->name, prefix_family_str (p),
611 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
612 p->prefixlen);
613
614 p = connected->destination;
615 if (p)
616 {
617 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
618 BUFSIZ - strlen(logbuf));
619 }
620 zlog (NULL, LOG_INFO, logbuf);
621}
622
623/* If two connected address has same prefix return 1. */
624int
625connected_same_prefix (struct prefix *p1, struct prefix *p2)
626{
627 if (p1->family == p2->family)
628 {
629 if (p1->family == AF_INET &&
630 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
631 return 1;
632#ifdef HAVE_IPV6
633 if (p1->family == AF_INET6 &&
634 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
635 return 1;
636#endif /* HAVE_IPV6 */
637 }
638 return 0;
639}
640
641struct connected *
642connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
643{
644 struct listnode *node;
645 struct listnode *next;
646 struct connected *ifc;
647
648 /* In case of same prefix come, replace it with new one. */
649 for (node = listhead (ifp->connected); node; node = next)
650 {
651 ifc = getdata (node);
652 next = node->next;
653
654 if (connected_same_prefix (ifc->address, p))
655 {
656 listnode_delete (ifp->connected, ifc);
657 return ifc;
658 }
659 }
660 return NULL;
661}
662
paul727d1042002-12-13 20:50:29 +0000663/* Find the IPv4 address on our side that will be used when packets
664 are sent to dst. */
665struct connected *
666connected_lookup_address (struct interface *ifp, struct in_addr dst)
667{
668 struct prefix addr;
669 struct prefix best;
670 listnode cnode;
671 struct prefix *p;
672 struct connected *c;
673 struct connected *match;
674
675 /* Zero structures - get rid of rubbish from stack */
676 memset(&addr, 0, sizeof(addr));
677 memset(&best, 0, sizeof(best));
678
679 addr.family = AF_INET;
680 addr.u.prefix4 = dst;
681 addr.prefixlen = IPV4_MAX_BITLEN;
682
683 match = NULL;
684
685 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
686 {
687 c = getdata (cnode);
688
689 if (if_is_pointopoint (ifp))
690 {
691 p = c->address;
692
693 if (p && p->family == AF_INET)
694 {
695#ifdef OLD_RIB /* PTP links are conventionally identified
696 by the address of the far end - MAG */
697 if (IPV4_ADDR_SAME (&p->u.prefix4, &dst))
698 return c;
699#endif
700 p = c->destination;
701 if (p && IPV4_ADDR_SAME (&p->u.prefix4, &dst))
702 return c;
703 }
704 }
705 else
706 {
707 p = c->address;
708
709 if (p->family == AF_INET)
710 {
711 if (prefix_match (p, &addr) && p->prefixlen > best.prefixlen)
712 {
713 best = *p;
714 match = c;
715 }
716 }
717 }
718 }
719 return match;
720}
721
paul4a7aac12004-05-08 05:00:31 +0000722struct connected *
723connected_add_by_prefix (struct interface *ifp, struct prefix *p,
724 struct prefix *destination)
725{
726 struct connected *ifc;
727
728 /* Allocate new connected address. */
729 ifc = connected_new ();
730 ifc->ifp = ifp;
731
732 /* Fetch interface address */
733 ifc->address = prefix_new();
734 memcpy (ifc->address, p, sizeof(struct prefix));
735
736 /* Fetch dest address */
737 ifc->destination = prefix_new();
738 memcpy (ifc->destination, destination, sizeof(struct prefix));
739
740 /* Add connected address to the interface. */
741 listnode_add (ifp->connected, ifc);
742 return ifc;
743}
744
paul718e3742002-12-13 20:15:29 +0000745#ifndef HAVE_IF_NAMETOINDEX
746unsigned int
747if_nametoindex (const char *name)
748{
749 listnode node;
750 struct interface *ifp;
751
752 for (node = listhead (iflist); node; nextnode (node))
753 {
754 ifp = getdata (node);
755 if (strcmp (ifp->name, name) == 0)
756 return ifp->ifindex;
757 }
758 return 0;
759}
760#endif
761
762#ifndef HAVE_IF_INDEXTONAME
763char *
764if_indextoname (unsigned int ifindex, char *name)
765{
766 listnode node;
767 struct interface *ifp;
768
769 for (node = listhead (iflist); node; nextnode (node))
770 {
771 ifp = getdata (node);
772 if (ifp->ifindex == ifindex)
773 {
774 memcpy (name, ifp->name, IFNAMSIZ);
775 return ifp->name;
776 }
777 }
778 return NULL;
779}
780#endif
781
782/* Interface looking up by interface's address. */
783
784/* Interface's IPv4 address reverse lookup table. */
785struct route_table *ifaddr_ipv4_table;
786/* struct route_table *ifaddr_ipv6_table; */
787
788void
789ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
790{
791 struct route_node *rn;
792 struct prefix_ipv4 p;
793
794 p.family = AF_INET;
795 p.prefixlen = IPV4_MAX_PREFIXLEN;
796 p.prefix = *ifaddr;
797
798 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
799 if (rn)
800 {
801 route_unlock_node (rn);
802 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
803 inet_ntoa (*ifaddr));
804 return;
805 }
806 rn->info = ifp;
807}
808
809void
810ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
811{
812 struct route_node *rn;
813 struct prefix_ipv4 p;
814
815 p.family = AF_INET;
816 p.prefixlen = IPV4_MAX_PREFIXLEN;
817 p.prefix = *ifaddr;
818
819 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
820 if (! rn)
821 {
822 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
823 inet_ntoa (*ifaddr));
824 return;
825 }
826 rn->info = NULL;
827 route_unlock_node (rn);
828 route_unlock_node (rn);
829}
830
831/* Lookup interface by interface's IP address or interface index. */
832struct interface *
833ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
834{
835 struct prefix_ipv4 p;
836 struct route_node *rn;
837 struct interface *ifp;
838 listnode node;
839
840 if (addr)
841 {
842 p.family = AF_INET;
843 p.prefixlen = IPV4_MAX_PREFIXLEN;
844 p.prefix = *addr;
845
846 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
847 if (! rn)
848 return NULL;
849
850 ifp = rn->info;
851 route_unlock_node (rn);
852 return ifp;
853 }
854 else
855 {
856 for (node = listhead (iflist); node; nextnode (node))
857 {
858 ifp = getdata (node);
859
860 if (ifp->ifindex == ifindex)
861 return ifp;
862 }
863 }
864 return NULL;
865}
866
867/* Initialize interface list. */
868void
869if_init ()
870{
871 iflist = list_new ();
872 ifaddr_ipv4_table = route_table_init ();
873
paul106d2fd2003-08-01 00:24:13 +0000874 if (iflist) {
875 iflist->cmp = (int (*)(void *, void *))if_cmp_func;
paul718e3742002-12-13 20:15:29 +0000876 return;
paul106d2fd2003-08-01 00:24:13 +0000877 }
paul718e3742002-12-13 20:15:29 +0000878
879 memset (&if_master, 0, sizeof if_master);
880}