blob: 5519b2ae44e96dae8b2e1efcbc77906ccf9a6c2d [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
paul3a0391a2004-07-17 11:51:29 +000049/* Compare interface names, returning an integer greater than, equal to, or
50 * less than 0, (following the strcmp convention), according to the
51 * relationship between ifp1 and ifp2. Interface names consist of an
52 * alphabetic prefix and a numeric suffix. The primary sort key is
53 * lexicographic by name, and then numeric by number. No number sorts
54 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
55 * devpty0, de0 < del0
56 */
paul106d2fd2003-08-01 00:24:13 +000057int
58if_cmp_func (struct interface *ifp1, struct interface *ifp2)
59{
60 unsigned int l1, l2;
61 long int x1, x2;
62 char *p1, *p2;
63 int res;
64
65 p1 = ifp1->name;
66 p2 = ifp2->name;
67
paul90578522003-09-23 23:46:01 +000068 while (*p1 && *p2) {
paul106d2fd2003-08-01 00:24:13 +000069 /* look up to any number */
70 l1 = strcspn(p1, "0123456789");
71 l2 = strcspn(p2, "0123456789");
72
73 /* name lengths are different -> compare names */
74 if (l1 != l2)
75 return (strcmp(p1, p2));
76
paul3a0391a2004-07-17 11:51:29 +000077 /* Note that this relies on all numbers being less than all letters, so
78 * that de0 < del0.
79 */
paul106d2fd2003-08-01 00:24:13 +000080 res = strncmp(p1, p2, l1);
81
82 /* names are different -> compare them */
83 if (res)
84 return res;
85
86 /* with identical name part, go to numeric part */
paul106d2fd2003-08-01 00:24:13 +000087 p1 += l1;
88 p2 += l1;
89
paulb06c14f2004-07-09 12:24:42 +000090 if (!*p1)
91 return -1;
92 if (!*p2)
93 return 1;
94
paul106d2fd2003-08-01 00:24:13 +000095 x1 = strtol(p1, &p1, 10);
96 x2 = strtol(p2, &p2, 10);
97
98 /* let's compare numbers now */
99 if (x1 < x2)
100 return -1;
101 if (x1 > x2)
102 return 1;
103
104 /* numbers were equal, lets do it again..
105 (it happens with name like "eth123.456:789") */
106 }
paul90578522003-09-23 23:46:01 +0000107 if (*p1)
108 return 1;
109 if (*p2)
110 return -1;
111 return 0;
paul106d2fd2003-08-01 00:24:13 +0000112}
113
paul718e3742002-12-13 20:15:29 +0000114/* Create new interface structure. */
115struct interface *
116if_new ()
117{
118 struct interface *ifp;
119
120 ifp = XMALLOC (MTYPE_IF, sizeof (struct interface));
121 memset (ifp, 0, sizeof (struct interface));
122 return ifp;
123}
124
125struct interface *
paul9035efa2004-10-10 11:56:56 +0000126if_create (const char *name, int namelen)
paul718e3742002-12-13 20:15:29 +0000127{
128 struct interface *ifp;
129
130 ifp = if_new ();
131
paul106d2fd2003-08-01 00:24:13 +0000132 assert (name);
133 assert (namelen <= (INTERFACE_NAMSIZ + 1));
134 strncpy (ifp->name, name, namelen);
135 ifp->name[INTERFACE_NAMSIZ] = '\0';
hassoe90fbab2003-12-21 09:51:42 +0000136 if (if_lookup_by_name(ifp->name) == NULL)
137 listnode_add_sort (iflist, ifp);
paul718e3742002-12-13 20:15:29 +0000138 ifp->connected = list_new ();
139 ifp->connected->del = (void (*) (void *)) connected_free;
140
141 if (if_master.if_new_hook)
142 (*if_master.if_new_hook) (ifp);
143
144 return ifp;
145}
146
147/* Delete and free interface structure. */
148void
149if_delete (struct interface *ifp)
150{
151 listnode_delete (iflist, ifp);
152
153 if (if_master.if_delete_hook)
154 (*if_master.if_delete_hook) (ifp);
155
156 /* Free connected address list */
157 list_delete (ifp->connected);
158
159 XFREE (MTYPE_IF, ifp);
160}
161
162/* Add hook to interface master. */
163void
164if_add_hook (int type, int (*func)(struct interface *ifp))
165{
166 switch (type) {
167 case IF_NEW_HOOK:
168 if_master.if_new_hook = func;
169 break;
170 case IF_DELETE_HOOK:
171 if_master.if_delete_hook = func;
172 break;
173 default:
174 break;
175 }
176}
177
178/* Interface existance check by index. */
179struct interface *
180if_lookup_by_index (unsigned int index)
181{
hasso52dc7ee2004-09-23 19:18:23 +0000182 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000183 struct interface *ifp;
184
185 for (node = listhead (iflist); node; nextnode (node))
186 {
187 ifp = getdata (node);
188 if (ifp->ifindex == index)
189 return ifp;
190 }
191 return NULL;
192}
193
194char *
195ifindex2ifname (unsigned int index)
196{
hasso52dc7ee2004-09-23 19:18:23 +0000197 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000198 struct interface *ifp;
199
200 for (node = listhead (iflist); node; nextnode (node))
201 {
202 ifp = getdata (node);
203 if (ifp->ifindex == index)
204 return ifp->name;
205 }
hasso8c328f12004-10-05 21:01:23 +0000206 return (char *) "unknown";
paul718e3742002-12-13 20:15:29 +0000207}
208
209/* Interface existance check by interface name. */
210struct interface *
paul9035efa2004-10-10 11:56:56 +0000211if_lookup_by_name (const char *name)
paul718e3742002-12-13 20:15:29 +0000212{
hasso52dc7ee2004-09-23 19:18:23 +0000213 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000214 struct interface *ifp;
215
216 for (node = listhead (iflist); node; nextnode (node))
217 {
218 ifp = getdata (node);
219 if (strncmp (name, ifp->name, sizeof ifp->name) == 0)
220 return ifp;
221 }
222 return NULL;
223}
224
225/* Lookup interface by IPv4 address. */
226struct interface *
227if_lookup_exact_address (struct in_addr src)
228{
hasso52dc7ee2004-09-23 19:18:23 +0000229 struct listnode *node;
230 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000231 struct interface *ifp;
232 struct prefix *p;
233 struct connected *c;
234
235 for (node = listhead (iflist); node; nextnode (node))
236 {
237 ifp = getdata (node);
238
239 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
240 {
241 c = getdata (cnode);
242
243 p = c->address;
244
245 if (p && p->family == AF_INET)
246 {
247 if (IPV4_ADDR_SAME (&p->u.prefix4, &src))
248 return ifp;
249 }
250 }
251 }
252 return NULL;
253}
254
255/* Lookup interface by IPv4 address. */
256struct interface *
257if_lookup_address (struct in_addr src)
258{
hasso52dc7ee2004-09-23 19:18:23 +0000259 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000260 struct prefix addr;
hasso3fb9cd62004-10-19 19:44:43 +0000261 int bestlen = 0;
hasso52dc7ee2004-09-23 19:18:23 +0000262 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000263 struct interface *ifp;
264 struct prefix *p;
265 struct connected *c;
266 struct interface *match;
267
paul718e3742002-12-13 20:15:29 +0000268 addr.family = AF_INET;
269 addr.u.prefix4 = src;
270 addr.prefixlen = IPV4_MAX_BITLEN;
271
272 match = NULL;
273
274 for (node = listhead (iflist); node; nextnode (node))
275 {
276 ifp = getdata (node);
277
278 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
279 {
280 c = getdata (cnode);
281
hasso3fb9cd62004-10-19 19:44:43 +0000282 if (c->address && (c->address->family == AF_INET))
paul718e3742002-12-13 20:15:29 +0000283 {
hasso3fb9cd62004-10-19 19:44:43 +0000284 if (CONNECTED_POINTOPOINT_HOST(c))
paul718e3742002-12-13 20:15:29 +0000285 {
hasso3fb9cd62004-10-19 19:44:43 +0000286 /* PTP links are conventionally identified
287 by the address of the far end - MAG */
288 if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &src))
paul31a476c2003-09-29 19:54:53 +0000289 return ifp;
paul718e3742002-12-13 20:15:29 +0000290 }
hasso3fb9cd62004-10-19 19:44:43 +0000291 else
paul718e3742002-12-13 20:15:29 +0000292 {
hasso3fb9cd62004-10-19 19:44:43 +0000293 p = c->address;
294
295 if (prefix_match (p, &addr) && p->prefixlen > bestlen)
paul31a476c2003-09-29 19:54:53 +0000296 {
hasso3fb9cd62004-10-19 19:44:43 +0000297 bestlen = p->prefixlen;
paul31a476c2003-09-29 19:54:53 +0000298 match = ifp;
299 }
paul718e3742002-12-13 20:15:29 +0000300 }
301 }
302 }
303 }
304 return match;
305}
306
307/* Get interface by name if given name interface doesn't exist create
308 one. */
309struct interface *
paul9035efa2004-10-10 11:56:56 +0000310if_get_by_name (const char *name)
paul718e3742002-12-13 20:15:29 +0000311{
312 struct interface *ifp;
313
314 ifp = if_lookup_by_name (name);
315 if (ifp == NULL)
paul106d2fd2003-08-01 00:24:13 +0000316 ifp = if_create (name, INTERFACE_NAMSIZ);
paul718e3742002-12-13 20:15:29 +0000317 return ifp;
318}
319
320/* Does interface up ? */
321int
322if_is_up (struct interface *ifp)
323{
324 return ifp->flags & IFF_UP;
325}
326
paul2e3b2e42002-12-13 21:03:13 +0000327/* Is interface running? */
328int
329if_is_running (struct interface *ifp)
330{
331 return ifp->flags & IFF_RUNNING;
332}
333
334/* Is the interface operative, eg. either UP & RUNNING
335 or UP & !ZEBRA_INTERFACE_LINK_DETECTION */
336int
337if_is_operative (struct interface *ifp)
338{
339 return ((ifp->flags & IFF_UP) &&
340 (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
341}
342
paul718e3742002-12-13 20:15:29 +0000343/* Is this loopback interface ? */
344int
345if_is_loopback (struct interface *ifp)
346{
347 return ifp->flags & IFF_LOOPBACK;
348}
349
350/* Does this interface support broadcast ? */
351int
352if_is_broadcast (struct interface *ifp)
353{
354 return ifp->flags & IFF_BROADCAST;
355}
356
357/* Does this interface support broadcast ? */
358int
359if_is_pointopoint (struct interface *ifp)
360{
361 return ifp->flags & IFF_POINTOPOINT;
362}
363
364/* Does this interface support multicast ? */
365int
366if_is_multicast (struct interface *ifp)
367{
368 return ifp->flags & IFF_MULTICAST;
369}
370
371/* Printout flag information into log */
372const char *
373if_flag_dump (unsigned long flag)
374{
375 int separator = 0;
376 static char logbuf[BUFSIZ];
377
378#define IFF_OUT_LOG(X,STR) \
379 if ((X) && (flag & (X))) \
380 { \
381 if (separator) \
382 strlcat (logbuf, ",", BUFSIZ); \
383 else \
384 separator = 1; \
385 strlcat (logbuf, STR, BUFSIZ); \
386 }
387
388 strlcpy (logbuf, " <", BUFSIZ);
389 IFF_OUT_LOG (IFF_UP, "UP");
390 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
391 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
392 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
393 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
394 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
395 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
396 IFF_OUT_LOG (IFF_NOARP, "NOARP");
397 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
398 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
399 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
400 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
401 IFF_OUT_LOG (IFF_LINK0, "LINK0");
402 IFF_OUT_LOG (IFF_LINK1, "LINK1");
403 IFF_OUT_LOG (IFF_LINK2, "LINK2");
404 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
paul4a7aac12004-05-08 05:00:31 +0000405#ifdef SOLARIS_IPV6
406 IFF_OUT_LOG (IFF_IPV4, "IFF_IPv4");
407 IFF_OUT_LOG (IFF_IPV6, "IFF_IPv6");
408#endif /* SOLARIS_IPV6 */
paul718e3742002-12-13 20:15:29 +0000409
410 strlcat (logbuf, ">", BUFSIZ);
411
412 return logbuf;
413}
414
415/* For debugging */
416void
417if_dump (struct interface *ifp)
418{
hasso52dc7ee2004-09-23 19:18:23 +0000419 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000420
paul4a7aac12004-05-08 05:00:31 +0000421 zlog_info ("Interface %s index %d metric %d mtu %d "
422#ifdef HAVE_IPV6
423 "mtu6 %d "
424#endif /* HAVE_IPV6 */
425 "%s",
paul718e3742002-12-13 20:15:29 +0000426 ifp->name, ifp->ifindex, ifp->metric, ifp->mtu,
paul4a7aac12004-05-08 05:00:31 +0000427#ifdef HAVE_IPV6
428 ifp->mtu6,
429#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +0000430 if_flag_dump (ifp->flags));
431
432 for (node = listhead (ifp->connected); node; nextnode (node))
433 ;
434}
435
436/* Interface printing for all interface. */
437void
438if_dump_all ()
439{
hasso52dc7ee2004-09-23 19:18:23 +0000440 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000441
442 for (node = listhead (iflist); node; nextnode (node))
443 if_dump (getdata (node));
444}
445
446DEFUN (interface_desc,
447 interface_desc_cmd,
448 "description .LINE",
449 "Interface specific description\n"
450 "Characters describing this interface\n")
451{
452 int i;
453 struct interface *ifp;
454 struct buffer *b;
455
456 if (argc == 0)
457 return CMD_SUCCESS;
458
459 ifp = vty->index;
460 if (ifp->desc)
461 XFREE (0, ifp->desc);
462
463 b = buffer_new (1024);
464 for (i = 0; i < argc; i++)
465 {
paul02ff83c2004-06-11 11:27:03 +0000466 buffer_putstr (b, argv[i]);
paul718e3742002-12-13 20:15:29 +0000467 buffer_putc (b, ' ');
468 }
469 buffer_putc (b, '\0');
470
471 ifp->desc = buffer_getstr (b);
472 buffer_free (b);
473
474 return CMD_SUCCESS;
475}
476
477DEFUN (no_interface_desc,
478 no_interface_desc_cmd,
479 "no description",
480 NO_STR
481 "Interface specific description\n")
482{
483 struct interface *ifp;
484
485 ifp = vty->index;
486 if (ifp->desc)
487 XFREE (0, ifp->desc);
488 ifp->desc = NULL;
489
490 return CMD_SUCCESS;
491}
492
493
494/* See also wrapper function zebra_interface() in zebra/interface.c */
495DEFUN (interface,
496 interface_cmd,
497 "interface IFNAME",
498 "Select an interface to configure\n"
499 "Interface's name\n")
500{
501 struct interface *ifp;
502
503 ifp = if_lookup_by_name (argv[0]);
504
505 if (ifp == NULL)
paul106d2fd2003-08-01 00:24:13 +0000506 ifp = if_create (argv[0], INTERFACE_NAMSIZ);
paul718e3742002-12-13 20:15:29 +0000507 vty->index = ifp;
508 vty->node = INTERFACE_NODE;
509
510 return CMD_SUCCESS;
511}
512
paul32d24632003-05-23 09:25:20 +0000513DEFUN_NOSH (no_interface,
514 no_interface_cmd,
515 "no interface IFNAME",
516 NO_STR
517 "Delete a pseudo interface's configuration\n"
518 "Interface's name\n")
519{
520 // deleting interface
521 struct interface *ifp;
522
523 ifp = if_lookup_by_name (argv[0]);
524
525 if (ifp == NULL)
paulbfc13532003-05-24 06:40:04 +0000526 {
527 vty_out (vty, "%% Inteface %s does not exist%s", argv[0], VTY_NEWLINE);
528 return CMD_WARNING;
529 }
paul32d24632003-05-23 09:25:20 +0000530
paulbfc13532003-05-24 06:40:04 +0000531 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
532 {
paul32d24632003-05-23 09:25:20 +0000533 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
534 VTY_NEWLINE);
535 return CMD_WARNING;
536 }
537
538 if_delete(ifp);
539
540 return CMD_SUCCESS;
541}
542
paul718e3742002-12-13 20:15:29 +0000543/* For debug purpose. */
544DEFUN (show_address,
545 show_address_cmd,
546 "show address",
547 SHOW_STR
548 "address\n")
549{
hasso52dc7ee2004-09-23 19:18:23 +0000550 struct listnode *node;
551 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000552 struct interface *ifp;
553 struct connected *ifc;
554 struct prefix *p;
555
556 for (node = listhead (iflist); node; nextnode (node))
557 {
558 ifp = getdata (node);
559
560 for (node2 = listhead (ifp->connected); node2; nextnode (node2))
561 {
562 ifc = getdata (node2);
563 p = ifc->address;
564
565 if (p->family == AF_INET)
566 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
567 VTY_NEWLINE);
568 }
569 }
570 return CMD_SUCCESS;
571}
572
573/* Allocate connected structure. */
574struct connected *
575connected_new ()
576{
577 struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected));
578 memset (new, 0, sizeof (struct connected));
579 return new;
580}
581
582/* Free connected structure. */
583void
584connected_free (struct connected *connected)
585{
586 if (connected->address)
587 prefix_free (connected->address);
588
589 if (connected->destination)
590 prefix_free (connected->destination);
591
592 if (connected->label)
593 free (connected->label);
594
595 XFREE (MTYPE_CONNECTED, connected);
596}
597
598/* Print if_addr structure. */
599void
600connected_log (struct connected *connected, char *str)
601{
602 struct prefix *p;
603 struct interface *ifp;
604 char logbuf[BUFSIZ];
605 char buf[BUFSIZ];
606
607 ifp = connected->ifp;
608 p = connected->address;
609
610 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
611 str, ifp->name, prefix_family_str (p),
612 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
613 p->prefixlen);
614
615 p = connected->destination;
616 if (p)
617 {
618 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
619 BUFSIZ - strlen(logbuf));
620 }
621 zlog (NULL, LOG_INFO, logbuf);
622}
623
624/* If two connected address has same prefix return 1. */
625int
626connected_same_prefix (struct prefix *p1, struct prefix *p2)
627{
628 if (p1->family == p2->family)
629 {
630 if (p1->family == AF_INET &&
631 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
632 return 1;
633#ifdef HAVE_IPV6
634 if (p1->family == AF_INET6 &&
635 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
636 return 1;
637#endif /* HAVE_IPV6 */
638 }
639 return 0;
640}
641
642struct connected *
643connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
644{
645 struct listnode *node;
646 struct listnode *next;
647 struct connected *ifc;
648
649 /* In case of same prefix come, replace it with new one. */
650 for (node = listhead (ifp->connected); node; node = next)
651 {
652 ifc = getdata (node);
653 next = node->next;
654
655 if (connected_same_prefix (ifc->address, p))
656 {
657 listnode_delete (ifp->connected, ifc);
658 return ifc;
659 }
660 }
661 return NULL;
662}
663
paul727d1042002-12-13 20:50:29 +0000664/* Find the IPv4 address on our side that will be used when packets
665 are sent to dst. */
666struct connected *
667connected_lookup_address (struct interface *ifp, struct in_addr dst)
668{
669 struct prefix addr;
hasso52dc7ee2004-09-23 19:18:23 +0000670 struct listnode *cnode;
paul727d1042002-12-13 20:50:29 +0000671 struct prefix *p;
672 struct connected *c;
673 struct connected *match;
674
paul727d1042002-12-13 20:50:29 +0000675 addr.family = AF_INET;
676 addr.u.prefix4 = dst;
677 addr.prefixlen = IPV4_MAX_BITLEN;
678
679 match = NULL;
680
681 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
682 {
683 c = getdata (cnode);
684
hasso3fb9cd62004-10-19 19:44:43 +0000685 if (c->address && (c->address->family == AF_INET))
686 {
687 if (CONNECTED_POINTOPOINT_HOST(c))
paul727d1042002-12-13 20:50:29 +0000688 {
hasso3fb9cd62004-10-19 19:44:43 +0000689 /* PTP links are conventionally identified
690 by the address of the far end - MAG */
691 if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &dst))
paul727d1042002-12-13 20:50:29 +0000692 return c;
693 }
hasso3fb9cd62004-10-19 19:44:43 +0000694 else
paul727d1042002-12-13 20:50:29 +0000695 {
hasso3fb9cd62004-10-19 19:44:43 +0000696 p = c->address;
697
698 if (prefix_match (p, &addr) &&
699 (!match || (p->prefixlen > match->address->prefixlen)))
700 match = c;
paul727d1042002-12-13 20:50:29 +0000701 }
hasso3fb9cd62004-10-19 19:44:43 +0000702 }
paul727d1042002-12-13 20:50:29 +0000703 }
704 return match;
705}
706
paul4a7aac12004-05-08 05:00:31 +0000707struct connected *
708connected_add_by_prefix (struct interface *ifp, struct prefix *p,
709 struct prefix *destination)
710{
711 struct connected *ifc;
712
713 /* Allocate new connected address. */
714 ifc = connected_new ();
715 ifc->ifp = ifp;
716
717 /* Fetch interface address */
718 ifc->address = prefix_new();
719 memcpy (ifc->address, p, sizeof(struct prefix));
720
721 /* Fetch dest address */
hasso3fb9cd62004-10-19 19:44:43 +0000722 if (destination)
723 {
724 ifc->destination = prefix_new();
725 memcpy (ifc->destination, destination, sizeof(struct prefix));
726 }
paul4a7aac12004-05-08 05:00:31 +0000727
728 /* Add connected address to the interface. */
729 listnode_add (ifp->connected, ifc);
730 return ifc;
731}
732
paul718e3742002-12-13 20:15:29 +0000733#ifndef HAVE_IF_NAMETOINDEX
734unsigned int
735if_nametoindex (const char *name)
736{
hasso52dc7ee2004-09-23 19:18:23 +0000737 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000738 struct interface *ifp;
739
740 for (node = listhead (iflist); node; nextnode (node))
741 {
742 ifp = getdata (node);
743 if (strcmp (ifp->name, name) == 0)
744 return ifp->ifindex;
745 }
746 return 0;
747}
748#endif
749
750#ifndef HAVE_IF_INDEXTONAME
751char *
752if_indextoname (unsigned int ifindex, char *name)
753{
hasso52dc7ee2004-09-23 19:18:23 +0000754 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000755 struct interface *ifp;
756
757 for (node = listhead (iflist); node; nextnode (node))
758 {
759 ifp = getdata (node);
760 if (ifp->ifindex == ifindex)
761 {
762 memcpy (name, ifp->name, IFNAMSIZ);
763 return ifp->name;
764 }
765 }
766 return NULL;
767}
768#endif
769
770/* Interface looking up by interface's address. */
771
772/* Interface's IPv4 address reverse lookup table. */
773struct route_table *ifaddr_ipv4_table;
774/* struct route_table *ifaddr_ipv6_table; */
775
776void
777ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
778{
779 struct route_node *rn;
780 struct prefix_ipv4 p;
781
782 p.family = AF_INET;
783 p.prefixlen = IPV4_MAX_PREFIXLEN;
784 p.prefix = *ifaddr;
785
786 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
787 if (rn)
788 {
789 route_unlock_node (rn);
790 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
791 inet_ntoa (*ifaddr));
792 return;
793 }
794 rn->info = ifp;
795}
796
797void
798ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
799{
800 struct route_node *rn;
801 struct prefix_ipv4 p;
802
803 p.family = AF_INET;
804 p.prefixlen = IPV4_MAX_PREFIXLEN;
805 p.prefix = *ifaddr;
806
807 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
808 if (! rn)
809 {
810 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
811 inet_ntoa (*ifaddr));
812 return;
813 }
814 rn->info = NULL;
815 route_unlock_node (rn);
816 route_unlock_node (rn);
817}
818
819/* Lookup interface by interface's IP address or interface index. */
820struct interface *
821ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
822{
823 struct prefix_ipv4 p;
824 struct route_node *rn;
825 struct interface *ifp;
hasso52dc7ee2004-09-23 19:18:23 +0000826 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000827
828 if (addr)
829 {
830 p.family = AF_INET;
831 p.prefixlen = IPV4_MAX_PREFIXLEN;
832 p.prefix = *addr;
833
834 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
835 if (! rn)
836 return NULL;
837
838 ifp = rn->info;
839 route_unlock_node (rn);
840 return ifp;
841 }
842 else
843 {
844 for (node = listhead (iflist); node; nextnode (node))
845 {
846 ifp = getdata (node);
847
848 if (ifp->ifindex == ifindex)
849 return ifp;
850 }
851 }
852 return NULL;
853}
854
855/* Initialize interface list. */
856void
857if_init ()
858{
859 iflist = list_new ();
860 ifaddr_ipv4_table = route_table_init ();
861
paul106d2fd2003-08-01 00:24:13 +0000862 if (iflist) {
863 iflist->cmp = (int (*)(void *, void *))if_cmp_func;
paul718e3742002-12-13 20:15:29 +0000864 return;
paul106d2fd2003-08-01 00:24:13 +0000865 }
paul718e3742002-12-13 20:15:29 +0000866
867 memset (&if_master, 0, sizeof if_master);
868}