blob: 68e6e4a086474e5904a417a2abb17b4cb1bba35c [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"
33#include "zebra/connected.h"
34#include "memory.h"
35#include "table.h"
36#include "buffer.h"
37#include "str.h"
38#include "log.h"
39
40/* Master list of interfaces. */
41struct list *iflist;
42
43/* One for each program. This structure is needed to store hooks. */
44struct if_master
45{
46 int (*if_new_hook) (struct interface *);
47 int (*if_delete_hook) (struct interface *);
48} if_master;
49
paul106d2fd2003-08-01 00:24:13 +000050/* Compare interface names */
51int
52if_cmp_func (struct interface *ifp1, struct interface *ifp2)
53{
54 unsigned int l1, l2;
55 long int x1, x2;
56 char *p1, *p2;
57 int res;
58
59 p1 = ifp1->name;
60 p2 = ifp2->name;
61
paul90578522003-09-23 23:46:01 +000062 while (*p1 && *p2) {
paul106d2fd2003-08-01 00:24:13 +000063 /* look up to any number */
64 l1 = strcspn(p1, "0123456789");
65 l2 = strcspn(p2, "0123456789");
66
67 /* name lengths are different -> compare names */
68 if (l1 != l2)
69 return (strcmp(p1, p2));
70
71 res = strncmp(p1, p2, l1);
72
73 /* names are different -> compare them */
74 if (res)
75 return res;
76
77 /* with identical name part, go to numeric part */
78
79 p1 += l1;
80 p2 += l1;
81
82 x1 = strtol(p1, &p1, 10);
83 x2 = strtol(p2, &p2, 10);
84
85 /* let's compare numbers now */
86 if (x1 < x2)
87 return -1;
88 if (x1 > x2)
89 return 1;
90
91 /* numbers were equal, lets do it again..
92 (it happens with name like "eth123.456:789") */
93 }
paul90578522003-09-23 23:46:01 +000094 if (*p1)
95 return 1;
96 if (*p2)
97 return -1;
98 return 0;
paul106d2fd2003-08-01 00:24:13 +000099}
100
paul718e3742002-12-13 20:15:29 +0000101/* Create new interface structure. */
102struct interface *
103if_new ()
104{
105 struct interface *ifp;
106
107 ifp = XMALLOC (MTYPE_IF, sizeof (struct interface));
108 memset (ifp, 0, sizeof (struct interface));
109 return ifp;
110}
111
112struct interface *
paul106d2fd2003-08-01 00:24:13 +0000113if_create (char *name, int namelen)
paul718e3742002-12-13 20:15:29 +0000114{
115 struct interface *ifp;
116
117 ifp = if_new ();
118
paul106d2fd2003-08-01 00:24:13 +0000119 assert (name);
120 assert (namelen <= (INTERFACE_NAMSIZ + 1));
121 strncpy (ifp->name, name, namelen);
122 ifp->name[INTERFACE_NAMSIZ] = '\0';
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;
paul00df0c12002-12-13 21:07:36 +0000248 struct prefix peer;
paul718e3742002-12-13 20:15:29 +0000249 listnode cnode;
250 struct interface *ifp;
251 struct prefix *p;
252 struct connected *c;
253 struct interface *match;
paul00df0c12002-12-13 21:07:36 +0000254 int prefixlen;
paul718e3742002-12-13 20:15:29 +0000255
256 /* Zero structures - get rid of rubbish from stack */
257 memset(&addr, 0, sizeof(addr));
258 memset(&best, 0, sizeof(best));
259
260 addr.family = AF_INET;
261 addr.u.prefix4 = src;
262 addr.prefixlen = IPV4_MAX_BITLEN;
263
264 match = NULL;
265
266 for (node = listhead (iflist); node; nextnode (node))
267 {
268 ifp = getdata (node);
269
270 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
271 {
272 c = getdata (cnode);
paul00df0c12002-12-13 21:07:36 +0000273 p = c->address;
paul718e3742002-12-13 20:15:29 +0000274
paul00df0c12002-12-13 21:07:36 +0000275 if (p->family == AF_INET)
paul718e3742002-12-13 20:15:29 +0000276 {
paul00df0c12002-12-13 21:07:36 +0000277 prefixlen = p->prefixlen;
paul718e3742002-12-13 20:15:29 +0000278
paul00df0c12002-12-13 21:07:36 +0000279 if (if_is_pointopoint (ifp) ||
280 prefixlen >= IPV4_MAX_PREFIXLEN - 1)
paul718e3742002-12-13 20:15:29 +0000281 {
paul00df0c12002-12-13 21:07:36 +0000282 peer = *c->destination;
283 peer.prefixlen = prefixlen;
284 p = &peer;
paul718e3742002-12-13 20:15:29 +0000285 }
paul718e3742002-12-13 20:15:29 +0000286
paul00df0c12002-12-13 21:07:36 +0000287 if (prefix_match (p, &addr) && prefixlen > best.prefixlen)
paul718e3742002-12-13 20:15:29 +0000288 {
paul00df0c12002-12-13 21:07:36 +0000289 best = *p;
290 match = ifp;
paul718e3742002-12-13 20:15:29 +0000291 }
292 }
293 }
294 }
295 return match;
296}
297
298/* Get interface by name if given name interface doesn't exist create
299 one. */
300struct interface *
301if_get_by_name (char *name)
302{
303 struct interface *ifp;
304
305 ifp = if_lookup_by_name (name);
306 if (ifp == NULL)
paul106d2fd2003-08-01 00:24:13 +0000307 ifp = if_create (name, INTERFACE_NAMSIZ);
paul718e3742002-12-13 20:15:29 +0000308 return ifp;
309}
310
311/* Does interface up ? */
312int
313if_is_up (struct interface *ifp)
314{
315 return ifp->flags & IFF_UP;
316}
317
paul2e3b2e42002-12-13 21:03:13 +0000318/* Is interface running? */
319int
320if_is_running (struct interface *ifp)
321{
322 return ifp->flags & IFF_RUNNING;
323}
324
325/* Is the interface operative, eg. either UP & RUNNING
326 or UP & !ZEBRA_INTERFACE_LINK_DETECTION */
327int
328if_is_operative (struct interface *ifp)
329{
330 return ((ifp->flags & IFF_UP) &&
331 (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
332}
333
paul718e3742002-12-13 20:15:29 +0000334/* Is this loopback interface ? */
335int
336if_is_loopback (struct interface *ifp)
337{
338 return ifp->flags & IFF_LOOPBACK;
339}
340
341/* Does this interface support broadcast ? */
342int
343if_is_broadcast (struct interface *ifp)
344{
345 return ifp->flags & IFF_BROADCAST;
346}
347
348/* Does this interface support broadcast ? */
349int
350if_is_pointopoint (struct interface *ifp)
351{
352 return ifp->flags & IFF_POINTOPOINT;
353}
354
355/* Does this interface support multicast ? */
356int
357if_is_multicast (struct interface *ifp)
358{
359 return ifp->flags & IFF_MULTICAST;
360}
361
362/* Printout flag information into log */
363const char *
364if_flag_dump (unsigned long flag)
365{
366 int separator = 0;
367 static char logbuf[BUFSIZ];
368
369#define IFF_OUT_LOG(X,STR) \
370 if ((X) && (flag & (X))) \
371 { \
372 if (separator) \
373 strlcat (logbuf, ",", BUFSIZ); \
374 else \
375 separator = 1; \
376 strlcat (logbuf, STR, BUFSIZ); \
377 }
378
379 strlcpy (logbuf, " <", BUFSIZ);
380 IFF_OUT_LOG (IFF_UP, "UP");
381 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
382 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
383 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
384 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
385 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
386 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
387 IFF_OUT_LOG (IFF_NOARP, "NOARP");
388 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
389 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
390 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
391 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
392 IFF_OUT_LOG (IFF_LINK0, "LINK0");
393 IFF_OUT_LOG (IFF_LINK1, "LINK1");
394 IFF_OUT_LOG (IFF_LINK2, "LINK2");
395 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
396
397 strlcat (logbuf, ">", BUFSIZ);
398
399 return logbuf;
400}
401
402/* For debugging */
403void
404if_dump (struct interface *ifp)
405{
406 listnode node;
407
408 zlog_info ("Interface %s index %d metric %d mtu %d %s",
409 ifp->name, ifp->ifindex, ifp->metric, ifp->mtu,
410 if_flag_dump (ifp->flags));
411
412 for (node = listhead (ifp->connected); node; nextnode (node))
413 ;
414}
415
416/* Interface printing for all interface. */
417void
418if_dump_all ()
419{
420 listnode node;
421
422 for (node = listhead (iflist); node; nextnode (node))
423 if_dump (getdata (node));
424}
425
426DEFUN (interface_desc,
427 interface_desc_cmd,
428 "description .LINE",
429 "Interface specific description\n"
430 "Characters describing this interface\n")
431{
432 int i;
433 struct interface *ifp;
434 struct buffer *b;
435
436 if (argc == 0)
437 return CMD_SUCCESS;
438
439 ifp = vty->index;
440 if (ifp->desc)
441 XFREE (0, ifp->desc);
442
443 b = buffer_new (1024);
444 for (i = 0; i < argc; i++)
445 {
446 buffer_putstr (b, (u_char *)argv[i]);
447 buffer_putc (b, ' ');
448 }
449 buffer_putc (b, '\0');
450
451 ifp->desc = buffer_getstr (b);
452 buffer_free (b);
453
454 return CMD_SUCCESS;
455}
456
457DEFUN (no_interface_desc,
458 no_interface_desc_cmd,
459 "no description",
460 NO_STR
461 "Interface specific description\n")
462{
463 struct interface *ifp;
464
465 ifp = vty->index;
466 if (ifp->desc)
467 XFREE (0, ifp->desc);
468 ifp->desc = NULL;
469
470 return CMD_SUCCESS;
471}
472
473
474/* See also wrapper function zebra_interface() in zebra/interface.c */
475DEFUN (interface,
476 interface_cmd,
477 "interface IFNAME",
478 "Select an interface to configure\n"
479 "Interface's name\n")
480{
481 struct interface *ifp;
482
483 ifp = if_lookup_by_name (argv[0]);
484
485 if (ifp == NULL)
paul106d2fd2003-08-01 00:24:13 +0000486 ifp = if_create (argv[0], INTERFACE_NAMSIZ);
paul718e3742002-12-13 20:15:29 +0000487 vty->index = ifp;
488 vty->node = INTERFACE_NODE;
489
490 return CMD_SUCCESS;
491}
492
paul32d24632003-05-23 09:25:20 +0000493DEFUN_NOSH (no_interface,
494 no_interface_cmd,
495 "no interface IFNAME",
496 NO_STR
497 "Delete a pseudo interface's configuration\n"
498 "Interface's name\n")
499{
500 // deleting interface
501 struct interface *ifp;
502
503 ifp = if_lookup_by_name (argv[0]);
504
505 if (ifp == NULL)
paulbfc13532003-05-24 06:40:04 +0000506 {
507 vty_out (vty, "%% Inteface %s does not exist%s", argv[0], VTY_NEWLINE);
508 return CMD_WARNING;
509 }
paul32d24632003-05-23 09:25:20 +0000510
paulbfc13532003-05-24 06:40:04 +0000511 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
512 {
paul32d24632003-05-23 09:25:20 +0000513 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
514 VTY_NEWLINE);
515 return CMD_WARNING;
516 }
517
518 if_delete(ifp);
519
520 return CMD_SUCCESS;
521}
522
paul718e3742002-12-13 20:15:29 +0000523/* For debug purpose. */
524DEFUN (show_address,
525 show_address_cmd,
526 "show address",
527 SHOW_STR
528 "address\n")
529{
530 listnode node;
531 listnode node2;
532 struct interface *ifp;
533 struct connected *ifc;
534 struct prefix *p;
535
536 for (node = listhead (iflist); node; nextnode (node))
537 {
538 ifp = getdata (node);
539
540 for (node2 = listhead (ifp->connected); node2; nextnode (node2))
541 {
542 ifc = getdata (node2);
543 p = ifc->address;
544
545 if (p->family == AF_INET)
546 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
547 VTY_NEWLINE);
548 }
549 }
550 return CMD_SUCCESS;
551}
552
553/* Allocate connected structure. */
554struct connected *
555connected_new ()
556{
557 struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected));
558 memset (new, 0, sizeof (struct connected));
559 return new;
560}
561
562/* Free connected structure. */
563void
564connected_free (struct connected *connected)
565{
566 if (connected->address)
567 prefix_free (connected->address);
568
569 if (connected->destination)
570 prefix_free (connected->destination);
571
572 if (connected->label)
573 free (connected->label);
574
575 XFREE (MTYPE_CONNECTED, connected);
576}
577
578/* Print if_addr structure. */
579void
580connected_log (struct connected *connected, char *str)
581{
582 struct prefix *p;
583 struct interface *ifp;
584 char logbuf[BUFSIZ];
585 char buf[BUFSIZ];
586
587 ifp = connected->ifp;
588 p = connected->address;
589
590 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
591 str, ifp->name, prefix_family_str (p),
592 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
593 p->prefixlen);
594
595 p = connected->destination;
596 if (p)
597 {
598 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
599 BUFSIZ - strlen(logbuf));
600 }
601 zlog (NULL, LOG_INFO, logbuf);
602}
603
604/* If two connected address has same prefix return 1. */
605int
606connected_same_prefix (struct prefix *p1, struct prefix *p2)
607{
608 if (p1->family == p2->family)
609 {
610 if (p1->family == AF_INET &&
611 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
612 return 1;
613#ifdef HAVE_IPV6
614 if (p1->family == AF_INET6 &&
615 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
616 return 1;
617#endif /* HAVE_IPV6 */
618 }
619 return 0;
620}
621
622struct connected *
623connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
624{
625 struct listnode *node;
626 struct listnode *next;
627 struct connected *ifc;
628
629 /* In case of same prefix come, replace it with new one. */
630 for (node = listhead (ifp->connected); node; node = next)
631 {
632 ifc = getdata (node);
633 next = node->next;
634
635 if (connected_same_prefix (ifc->address, p))
636 {
637 listnode_delete (ifp->connected, ifc);
638 return ifc;
639 }
640 }
641 return NULL;
642}
643
paul727d1042002-12-13 20:50:29 +0000644/* Find the IPv4 address on our side that will be used when packets
645 are sent to dst. */
646struct connected *
647connected_lookup_address (struct interface *ifp, struct in_addr dst)
648{
649 struct prefix addr;
650 struct prefix best;
651 listnode cnode;
652 struct prefix *p;
653 struct connected *c;
654 struct connected *match;
655
656 /* Zero structures - get rid of rubbish from stack */
657 memset(&addr, 0, sizeof(addr));
658 memset(&best, 0, sizeof(best));
659
660 addr.family = AF_INET;
661 addr.u.prefix4 = dst;
662 addr.prefixlen = IPV4_MAX_BITLEN;
663
664 match = NULL;
665
666 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
667 {
668 c = getdata (cnode);
669
670 if (if_is_pointopoint (ifp))
671 {
672 p = c->address;
673
674 if (p && p->family == AF_INET)
675 {
676#ifdef OLD_RIB /* PTP links are conventionally identified
677 by the address of the far end - MAG */
678 if (IPV4_ADDR_SAME (&p->u.prefix4, &dst))
679 return c;
680#endif
681 p = c->destination;
682 if (p && IPV4_ADDR_SAME (&p->u.prefix4, &dst))
683 return c;
684 }
685 }
686 else
687 {
688 p = c->address;
689
690 if (p->family == AF_INET)
691 {
692 if (prefix_match (p, &addr) && p->prefixlen > best.prefixlen)
693 {
694 best = *p;
695 match = c;
696 }
697 }
698 }
699 }
700 return match;
701}
702
paul718e3742002-12-13 20:15:29 +0000703/* Check the connected information is PtP style or not. */
704int
705ifc_pointopoint (struct connected *ifc)
706{
707 struct prefix *p;
708 int ptp = 0;
709
710 /* When interface has PtP flag. */
711 if (if_is_pointopoint (ifc->ifp))
712 return 1;
713
714 /* RFC3021 PtP check. */
715 p = ifc->address;
716
717 if (p->family == AF_INET)
718 ptp = (p->prefixlen >= IPV4_MAX_PREFIXLEN - 1);
719#ifdef HAVE_IPV6
720 if (p->family == AF_INET6)
721 ptp = (p->prefixlen >= IPV6_MAX_PREFIXLEN - 1);
722#endif /* HAVE_IPV6 */
723
724 return ptp;
725}
726
727#ifndef HAVE_IF_NAMETOINDEX
728unsigned int
729if_nametoindex (const char *name)
730{
731 listnode node;
732 struct interface *ifp;
733
734 for (node = listhead (iflist); node; nextnode (node))
735 {
736 ifp = getdata (node);
737 if (strcmp (ifp->name, name) == 0)
738 return ifp->ifindex;
739 }
740 return 0;
741}
742#endif
743
744#ifndef HAVE_IF_INDEXTONAME
745char *
746if_indextoname (unsigned int ifindex, char *name)
747{
748 listnode node;
749 struct interface *ifp;
750
751 for (node = listhead (iflist); node; nextnode (node))
752 {
753 ifp = getdata (node);
754 if (ifp->ifindex == ifindex)
755 {
756 memcpy (name, ifp->name, IFNAMSIZ);
757 return ifp->name;
758 }
759 }
760 return NULL;
761}
762#endif
763
764/* Interface looking up by interface's address. */
765
766/* Interface's IPv4 address reverse lookup table. */
767struct route_table *ifaddr_ipv4_table;
768/* struct route_table *ifaddr_ipv6_table; */
769
770void
771ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
772{
773 struct route_node *rn;
774 struct prefix_ipv4 p;
775
776 p.family = AF_INET;
777 p.prefixlen = IPV4_MAX_PREFIXLEN;
778 p.prefix = *ifaddr;
779
780 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
781 if (rn)
782 {
783 route_unlock_node (rn);
784 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
785 inet_ntoa (*ifaddr));
786 return;
787 }
788 rn->info = ifp;
789}
790
791void
792ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
793{
794 struct route_node *rn;
795 struct prefix_ipv4 p;
796
797 p.family = AF_INET;
798 p.prefixlen = IPV4_MAX_PREFIXLEN;
799 p.prefix = *ifaddr;
800
801 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
802 if (! rn)
803 {
804 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
805 inet_ntoa (*ifaddr));
806 return;
807 }
808 rn->info = NULL;
809 route_unlock_node (rn);
810 route_unlock_node (rn);
811}
812
813/* Lookup interface by interface's IP address or interface index. */
814struct interface *
815ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
816{
817 struct prefix_ipv4 p;
818 struct route_node *rn;
819 struct interface *ifp;
820 listnode node;
821
822 if (addr)
823 {
824 p.family = AF_INET;
825 p.prefixlen = IPV4_MAX_PREFIXLEN;
826 p.prefix = *addr;
827
828 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
829 if (! rn)
830 return NULL;
831
832 ifp = rn->info;
833 route_unlock_node (rn);
834 return ifp;
835 }
836 else
837 {
838 for (node = listhead (iflist); node; nextnode (node))
839 {
840 ifp = getdata (node);
841
842 if (ifp->ifindex == ifindex)
843 return ifp;
844 }
845 }
846 return NULL;
847}
848
849/* Initialize interface list. */
850void
851if_init ()
852{
853 iflist = list_new ();
854 ifaddr_ipv4_table = route_table_init ();
855
paul106d2fd2003-08-01 00:24:13 +0000856 if (iflist) {
857 iflist->cmp = (int (*)(void *, void *))if_cmp_func;
paul718e3742002-12-13 20:15:29 +0000858 return;
paul106d2fd2003-08-01 00:24:13 +0000859 }
paul718e3742002-12-13 20:15:29 +0000860
861 memset (&if_master, 0, sizeof if_master);
862}