blob: 212b236c1e63f244d0b15ce503d4b9acf3541f49 [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 *
paul9035efa2004-10-10 11:56:56 +0000116if_create (const char *name, int namelen)
paul718e3742002-12-13 20:15:29 +0000117{
118 struct interface *ifp;
119
ajsd2fc8892005-04-02 18:38:43 +0000120 ifp = XCALLOC (MTYPE_IF, sizeof (struct interface));
121 ifp->ifindex = IFINDEX_INTERNAL;
paul718e3742002-12-13 20:15:29 +0000122
paul106d2fd2003-08-01 00:24:13 +0000123 assert (name);
ajsd2fc8892005-04-02 18:38:43 +0000124 assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */
paul106d2fd2003-08-01 00:24:13 +0000125 strncpy (ifp->name, name, namelen);
ajsd2fc8892005-04-02 18:38:43 +0000126 ifp->name[namelen] = '\0';
hassoe90fbab2003-12-21 09:51:42 +0000127 if (if_lookup_by_name(ifp->name) == NULL)
128 listnode_add_sort (iflist, ifp);
ajsd2fc8892005-04-02 18:38:43 +0000129 else
130 zlog_err("if_create(%s): corruption detected -- interface with this "
131 "name exists already!", ifp->name);
paul718e3742002-12-13 20:15:29 +0000132 ifp->connected = list_new ();
133 ifp->connected->del = (void (*) (void *)) connected_free;
134
135 if (if_master.if_new_hook)
136 (*if_master.if_new_hook) (ifp);
137
138 return ifp;
139}
140
ajsd2fc8892005-04-02 18:38:43 +0000141/* Delete interface structure. */
142void
143if_delete_retain (struct interface *ifp)
144{
145 if (if_master.if_delete_hook)
146 (*if_master.if_delete_hook) (ifp);
147
148 /* Free connected address list */
149 list_delete (ifp->connected);
150}
151
paul718e3742002-12-13 20:15:29 +0000152/* Delete and free interface structure. */
153void
154if_delete (struct interface *ifp)
155{
156 listnode_delete (iflist, ifp);
157
ajsd2fc8892005-04-02 18:38:43 +0000158 if_delete_retain(ifp);
paul718e3742002-12-13 20:15:29 +0000159
160 XFREE (MTYPE_IF, ifp);
161}
162
163/* Add hook to interface master. */
164void
165if_add_hook (int type, int (*func)(struct interface *ifp))
166{
167 switch (type) {
168 case IF_NEW_HOOK:
169 if_master.if_new_hook = func;
170 break;
171 case IF_DELETE_HOOK:
172 if_master.if_delete_hook = func;
173 break;
174 default:
175 break;
176 }
177}
178
179/* Interface existance check by index. */
180struct interface *
181if_lookup_by_index (unsigned int index)
182{
hasso52dc7ee2004-09-23 19:18:23 +0000183 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000184 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;
191 }
192 return NULL;
193}
194
195char *
196ifindex2ifname (unsigned int index)
197{
paul718e3742002-12-13 20:15:29 +0000198 struct interface *ifp;
199
ajsd2fc8892005-04-02 18:38:43 +0000200 return ((ifp = if_lookup_by_index(index)) != NULL) ?
201 ifp->name : (char *)"unknown";
202}
203
204unsigned int
205ifname2ifindex (const char *name)
206{
207 struct interface *ifp;
208
209 return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp->ifindex : 0;
paul718e3742002-12-13 20:15:29 +0000210}
211
212/* Interface existance check by interface name. */
213struct interface *
paul9035efa2004-10-10 11:56:56 +0000214if_lookup_by_name (const char *name)
paul718e3742002-12-13 20:15:29 +0000215{
hasso52dc7ee2004-09-23 19:18:23 +0000216 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000217 struct interface *ifp;
218
219 for (node = listhead (iflist); node; nextnode (node))
220 {
221 ifp = getdata (node);
ajsa3491982005-04-02 22:50:38 +0000222 /* Change this to strcmp once improper uses of this function
223 have been replaced with calls to if_lookup_by_name_len. */
paul718e3742002-12-13 20:15:29 +0000224 if (strncmp (name, ifp->name, sizeof ifp->name) == 0)
225 return ifp;
226 }
227 return NULL;
228}
229
ajsa3491982005-04-02 22:50:38 +0000230struct interface *
231if_lookup_by_name_len(const char *name, size_t namelen)
232{
233 struct listnode *node;
234
235 if (namelen > INTERFACE_NAMSIZ)
236 return NULL;
237
238 for (node = listhead (iflist); node; nextnode (node))
239 {
240 struct interface *ifp;
241
242 ifp = getdata (node);
243 if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
244 return ifp;
245 }
246 return NULL;
247}
248
paul718e3742002-12-13 20:15:29 +0000249/* Lookup interface by IPv4 address. */
250struct interface *
251if_lookup_exact_address (struct in_addr src)
252{
hasso52dc7ee2004-09-23 19:18:23 +0000253 struct listnode *node;
254 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000255 struct interface *ifp;
256 struct prefix *p;
257 struct connected *c;
258
259 for (node = listhead (iflist); node; nextnode (node))
260 {
261 ifp = getdata (node);
262
263 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
264 {
265 c = getdata (cnode);
266
267 p = c->address;
268
269 if (p && p->family == AF_INET)
270 {
271 if (IPV4_ADDR_SAME (&p->u.prefix4, &src))
272 return ifp;
273 }
274 }
275 }
276 return NULL;
277}
278
279/* Lookup interface by IPv4 address. */
280struct interface *
281if_lookup_address (struct in_addr src)
282{
hasso52dc7ee2004-09-23 19:18:23 +0000283 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000284 struct prefix addr;
hasso3fb9cd62004-10-19 19:44:43 +0000285 int bestlen = 0;
hasso52dc7ee2004-09-23 19:18:23 +0000286 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000287 struct interface *ifp;
288 struct prefix *p;
289 struct connected *c;
290 struct interface *match;
291
paul718e3742002-12-13 20:15:29 +0000292 addr.family = AF_INET;
293 addr.u.prefix4 = src;
294 addr.prefixlen = IPV4_MAX_BITLEN;
295
296 match = NULL;
297
298 for (node = listhead (iflist); node; nextnode (node))
299 {
300 ifp = getdata (node);
301
302 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
303 {
304 c = getdata (cnode);
305
hasso3fb9cd62004-10-19 19:44:43 +0000306 if (c->address && (c->address->family == AF_INET))
paul718e3742002-12-13 20:15:29 +0000307 {
hasso3fb9cd62004-10-19 19:44:43 +0000308 if (CONNECTED_POINTOPOINT_HOST(c))
paul718e3742002-12-13 20:15:29 +0000309 {
hasso3fb9cd62004-10-19 19:44:43 +0000310 /* PTP links are conventionally identified
311 by the address of the far end - MAG */
312 if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &src))
paul31a476c2003-09-29 19:54:53 +0000313 return ifp;
paul718e3742002-12-13 20:15:29 +0000314 }
hasso3fb9cd62004-10-19 19:44:43 +0000315 else
paul718e3742002-12-13 20:15:29 +0000316 {
hasso3fb9cd62004-10-19 19:44:43 +0000317 p = c->address;
318
319 if (prefix_match (p, &addr) && p->prefixlen > bestlen)
paul31a476c2003-09-29 19:54:53 +0000320 {
hasso3fb9cd62004-10-19 19:44:43 +0000321 bestlen = p->prefixlen;
paul31a476c2003-09-29 19:54:53 +0000322 match = ifp;
323 }
paul718e3742002-12-13 20:15:29 +0000324 }
325 }
326 }
327 }
328 return match;
329}
330
331/* Get interface by name if given name interface doesn't exist create
332 one. */
333struct interface *
paul9035efa2004-10-10 11:56:56 +0000334if_get_by_name (const char *name)
paul718e3742002-12-13 20:15:29 +0000335{
336 struct interface *ifp;
337
ajsa3491982005-04-02 22:50:38 +0000338 /* Replace 2nd arg to if_create with strlen(name) once improper uses of
339 this function have been replaced with calls to if_get_by_name_len. */
340 return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp :
341 if_create(name, INTERFACE_NAMSIZ);
342}
343
344struct interface *
345if_get_by_name_len(const char *name, size_t namelen)
346{
347 struct interface *ifp;
348
349 return ((ifp = if_lookup_by_name_len(name, namelen)) != NULL) ? ifp :
350 if_create(name, namelen);
paul718e3742002-12-13 20:15:29 +0000351}
352
353/* Does interface up ? */
354int
355if_is_up (struct interface *ifp)
356{
357 return ifp->flags & IFF_UP;
358}
359
paul2e3b2e42002-12-13 21:03:13 +0000360/* Is interface running? */
361int
362if_is_running (struct interface *ifp)
363{
364 return ifp->flags & IFF_RUNNING;
365}
366
367/* Is the interface operative, eg. either UP & RUNNING
368 or UP & !ZEBRA_INTERFACE_LINK_DETECTION */
369int
370if_is_operative (struct interface *ifp)
371{
372 return ((ifp->flags & IFF_UP) &&
373 (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
374}
375
paul718e3742002-12-13 20:15:29 +0000376/* Is this loopback interface ? */
377int
378if_is_loopback (struct interface *ifp)
379{
paul4ba9b922004-12-21 22:34:58 +0000380 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
381 * but Y on platform N?
382 */
383 return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL));
paul718e3742002-12-13 20:15:29 +0000384}
385
386/* Does this interface support broadcast ? */
387int
388if_is_broadcast (struct interface *ifp)
389{
390 return ifp->flags & IFF_BROADCAST;
391}
392
393/* Does this interface support broadcast ? */
394int
395if_is_pointopoint (struct interface *ifp)
396{
397 return ifp->flags & IFF_POINTOPOINT;
398}
399
400/* Does this interface support multicast ? */
401int
402if_is_multicast (struct interface *ifp)
403{
404 return ifp->flags & IFF_MULTICAST;
405}
406
407/* Printout flag information into log */
408const char *
409if_flag_dump (unsigned long flag)
410{
411 int separator = 0;
412 static char logbuf[BUFSIZ];
413
414#define IFF_OUT_LOG(X,STR) \
paul4ba9b922004-12-21 22:34:58 +0000415 if (flag & (X)) \
paul718e3742002-12-13 20:15:29 +0000416 { \
417 if (separator) \
418 strlcat (logbuf, ",", BUFSIZ); \
419 else \
420 separator = 1; \
421 strlcat (logbuf, STR, BUFSIZ); \
422 }
423
424 strlcpy (logbuf, " <", BUFSIZ);
425 IFF_OUT_LOG (IFF_UP, "UP");
426 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
427 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
428 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
429 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
430 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
431 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
432 IFF_OUT_LOG (IFF_NOARP, "NOARP");
433 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
434 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
435 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
436 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
437 IFF_OUT_LOG (IFF_LINK0, "LINK0");
438 IFF_OUT_LOG (IFF_LINK1, "LINK1");
439 IFF_OUT_LOG (IFF_LINK2, "LINK2");
440 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
paul4ba9b922004-12-21 22:34:58 +0000441 IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT");
442 IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH");
443 IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL");
444 IFF_OUT_LOG (IFF_IPV4, "IPv4");
445 IFF_OUT_LOG (IFF_IPV6, "IPv6");
paul718e3742002-12-13 20:15:29 +0000446
447 strlcat (logbuf, ">", BUFSIZ);
448
449 return logbuf;
450}
451
452/* For debugging */
453void
454if_dump (struct interface *ifp)
455{
hasso52dc7ee2004-09-23 19:18:23 +0000456 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000457
paul4a7aac12004-05-08 05:00:31 +0000458 zlog_info ("Interface %s index %d metric %d mtu %d "
459#ifdef HAVE_IPV6
460 "mtu6 %d "
461#endif /* HAVE_IPV6 */
462 "%s",
paul718e3742002-12-13 20:15:29 +0000463 ifp->name, ifp->ifindex, ifp->metric, ifp->mtu,
paul4a7aac12004-05-08 05:00:31 +0000464#ifdef HAVE_IPV6
465 ifp->mtu6,
466#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +0000467 if_flag_dump (ifp->flags));
468
469 for (node = listhead (ifp->connected); node; nextnode (node))
470 ;
471}
472
473/* Interface printing for all interface. */
474void
475if_dump_all ()
476{
hasso52dc7ee2004-09-23 19:18:23 +0000477 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000478
479 for (node = listhead (iflist); node; nextnode (node))
480 if_dump (getdata (node));
481}
482
483DEFUN (interface_desc,
484 interface_desc_cmd,
485 "description .LINE",
486 "Interface specific description\n"
487 "Characters describing this interface\n")
488{
paul718e3742002-12-13 20:15:29 +0000489 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000490
491 if (argc == 0)
492 return CMD_SUCCESS;
493
494 ifp = vty->index;
495 if (ifp->desc)
ajs3b8b1852005-01-29 18:19:13 +0000496 XFREE (MTYPE_TMP, ifp->desc);
497 ifp->desc = argv_concat(argv, argc, 0);
paul718e3742002-12-13 20:15:29 +0000498
499 return CMD_SUCCESS;
500}
501
502DEFUN (no_interface_desc,
503 no_interface_desc_cmd,
504 "no description",
505 NO_STR
506 "Interface specific description\n")
507{
508 struct interface *ifp;
509
510 ifp = vty->index;
511 if (ifp->desc)
512 XFREE (0, ifp->desc);
513 ifp->desc = NULL;
514
515 return CMD_SUCCESS;
516}
517
518
519/* See also wrapper function zebra_interface() in zebra/interface.c */
520DEFUN (interface,
521 interface_cmd,
522 "interface IFNAME",
523 "Select an interface to configure\n"
524 "Interface's name\n")
525{
526 struct interface *ifp;
ajsd2fc8892005-04-02 18:38:43 +0000527 size_t sl;
528
529 if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ)
530 {
531 vty_out (vty, "%% Interface name %s is invalid: length exceeds "
532 "%d characters%s",
533 argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE);
534 return CMD_WARNING;
535 }
paul718e3742002-12-13 20:15:29 +0000536
ajsa3491982005-04-02 22:50:38 +0000537 ifp = if_get_by_name_len(argv[0], sl);
paul718e3742002-12-13 20:15:29 +0000538
paul718e3742002-12-13 20:15:29 +0000539 vty->index = ifp;
540 vty->node = INTERFACE_NODE;
541
542 return CMD_SUCCESS;
543}
544
paul32d24632003-05-23 09:25:20 +0000545DEFUN_NOSH (no_interface,
546 no_interface_cmd,
547 "no interface IFNAME",
548 NO_STR
549 "Delete a pseudo interface's configuration\n"
550 "Interface's name\n")
551{
552 // deleting interface
553 struct interface *ifp;
554
555 ifp = if_lookup_by_name (argv[0]);
556
557 if (ifp == NULL)
ajsd2fc8892005-04-02 18:38:43 +0000558 {
559 vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE);
560 return CMD_WARNING;
561 }
paul32d24632003-05-23 09:25:20 +0000562
paulbfc13532003-05-24 06:40:04 +0000563 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
ajsd2fc8892005-04-02 18:38:43 +0000564 {
565 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
566 VTY_NEWLINE);
567 return CMD_WARNING;
568 }
paul32d24632003-05-23 09:25:20 +0000569
570 if_delete(ifp);
571
572 return CMD_SUCCESS;
573}
574
paul718e3742002-12-13 20:15:29 +0000575/* For debug purpose. */
576DEFUN (show_address,
577 show_address_cmd,
578 "show address",
579 SHOW_STR
580 "address\n")
581{
hasso52dc7ee2004-09-23 19:18:23 +0000582 struct listnode *node;
583 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000584 struct interface *ifp;
585 struct connected *ifc;
586 struct prefix *p;
587
588 for (node = listhead (iflist); node; nextnode (node))
589 {
590 ifp = getdata (node);
591
592 for (node2 = listhead (ifp->connected); node2; nextnode (node2))
593 {
594 ifc = getdata (node2);
595 p = ifc->address;
596
597 if (p->family == AF_INET)
598 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
599 VTY_NEWLINE);
600 }
601 }
602 return CMD_SUCCESS;
603}
604
605/* Allocate connected structure. */
606struct connected *
607connected_new ()
608{
609 struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected));
610 memset (new, 0, sizeof (struct connected));
611 return new;
612}
613
614/* Free connected structure. */
615void
616connected_free (struct connected *connected)
617{
618 if (connected->address)
619 prefix_free (connected->address);
620
621 if (connected->destination)
622 prefix_free (connected->destination);
623
624 if (connected->label)
625 free (connected->label);
626
627 XFREE (MTYPE_CONNECTED, connected);
628}
629
630/* Print if_addr structure. */
631void
632connected_log (struct connected *connected, char *str)
633{
634 struct prefix *p;
635 struct interface *ifp;
636 char logbuf[BUFSIZ];
637 char buf[BUFSIZ];
638
639 ifp = connected->ifp;
640 p = connected->address;
641
642 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
643 str, ifp->name, prefix_family_str (p),
644 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
645 p->prefixlen);
646
647 p = connected->destination;
648 if (p)
649 {
650 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
651 BUFSIZ - strlen(logbuf));
652 }
653 zlog (NULL, LOG_INFO, logbuf);
654}
655
656/* If two connected address has same prefix return 1. */
657int
658connected_same_prefix (struct prefix *p1, struct prefix *p2)
659{
660 if (p1->family == p2->family)
661 {
662 if (p1->family == AF_INET &&
663 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
664 return 1;
665#ifdef HAVE_IPV6
666 if (p1->family == AF_INET6 &&
667 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
668 return 1;
669#endif /* HAVE_IPV6 */
670 }
671 return 0;
672}
673
674struct connected *
675connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
676{
677 struct listnode *node;
678 struct listnode *next;
679 struct connected *ifc;
680
681 /* In case of same prefix come, replace it with new one. */
682 for (node = listhead (ifp->connected); node; node = next)
683 {
684 ifc = getdata (node);
685 next = node->next;
686
687 if (connected_same_prefix (ifc->address, p))
688 {
689 listnode_delete (ifp->connected, ifc);
690 return ifc;
691 }
692 }
693 return NULL;
694}
695
paul727d1042002-12-13 20:50:29 +0000696/* Find the IPv4 address on our side that will be used when packets
697 are sent to dst. */
698struct connected *
699connected_lookup_address (struct interface *ifp, struct in_addr dst)
700{
701 struct prefix addr;
hasso52dc7ee2004-09-23 19:18:23 +0000702 struct listnode *cnode;
paul727d1042002-12-13 20:50:29 +0000703 struct prefix *p;
704 struct connected *c;
705 struct connected *match;
706
paul727d1042002-12-13 20:50:29 +0000707 addr.family = AF_INET;
708 addr.u.prefix4 = dst;
709 addr.prefixlen = IPV4_MAX_BITLEN;
710
711 match = NULL;
712
713 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
714 {
715 c = getdata (cnode);
716
hasso3fb9cd62004-10-19 19:44:43 +0000717 if (c->address && (c->address->family == AF_INET))
718 {
719 if (CONNECTED_POINTOPOINT_HOST(c))
paul727d1042002-12-13 20:50:29 +0000720 {
hasso3fb9cd62004-10-19 19:44:43 +0000721 /* PTP links are conventionally identified
722 by the address of the far end - MAG */
723 if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &dst))
paul727d1042002-12-13 20:50:29 +0000724 return c;
725 }
hasso3fb9cd62004-10-19 19:44:43 +0000726 else
paul727d1042002-12-13 20:50:29 +0000727 {
hasso3fb9cd62004-10-19 19:44:43 +0000728 p = c->address;
729
730 if (prefix_match (p, &addr) &&
731 (!match || (p->prefixlen > match->address->prefixlen)))
732 match = c;
paul727d1042002-12-13 20:50:29 +0000733 }
hasso3fb9cd62004-10-19 19:44:43 +0000734 }
paul727d1042002-12-13 20:50:29 +0000735 }
736 return match;
737}
738
paul4a7aac12004-05-08 05:00:31 +0000739struct connected *
740connected_add_by_prefix (struct interface *ifp, struct prefix *p,
741 struct prefix *destination)
742{
743 struct connected *ifc;
744
745 /* Allocate new connected address. */
746 ifc = connected_new ();
747 ifc->ifp = ifp;
748
749 /* Fetch interface address */
750 ifc->address = prefix_new();
751 memcpy (ifc->address, p, sizeof(struct prefix));
752
753 /* Fetch dest address */
hasso3fb9cd62004-10-19 19:44:43 +0000754 if (destination)
755 {
756 ifc->destination = prefix_new();
757 memcpy (ifc->destination, destination, sizeof(struct prefix));
758 }
paul4a7aac12004-05-08 05:00:31 +0000759
760 /* Add connected address to the interface. */
761 listnode_add (ifp->connected, ifc);
762 return ifc;
763}
764
paul718e3742002-12-13 20:15:29 +0000765#ifndef HAVE_IF_NAMETOINDEX
766unsigned int
767if_nametoindex (const char *name)
768{
paul718e3742002-12-13 20:15:29 +0000769 struct interface *ifp;
770
ajsd2fc8892005-04-02 18:38:43 +0000771 return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp->ifindex : 0;
paul718e3742002-12-13 20:15:29 +0000772}
773#endif
774
775#ifndef HAVE_IF_INDEXTONAME
776char *
777if_indextoname (unsigned int ifindex, char *name)
778{
paul718e3742002-12-13 20:15:29 +0000779 struct interface *ifp;
780
ajsd2fc8892005-04-02 18:38:43 +0000781 if (!(ifp = if_lookup_by_index(ifindex)))
782 return NULL;
783 strncpy (name, ifp->name, IFNAMSIZ);
784 return ifp->name;
paul718e3742002-12-13 20:15:29 +0000785}
786#endif
787
788/* Interface looking up by interface's address. */
789
790/* Interface's IPv4 address reverse lookup table. */
791struct route_table *ifaddr_ipv4_table;
792/* struct route_table *ifaddr_ipv6_table; */
793
794void
795ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
796{
797 struct route_node *rn;
798 struct prefix_ipv4 p;
799
800 p.family = AF_INET;
801 p.prefixlen = IPV4_MAX_PREFIXLEN;
802 p.prefix = *ifaddr;
803
804 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
805 if (rn)
806 {
807 route_unlock_node (rn);
808 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
809 inet_ntoa (*ifaddr));
810 return;
811 }
812 rn->info = ifp;
813}
814
815void
816ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
817{
818 struct route_node *rn;
819 struct prefix_ipv4 p;
820
821 p.family = AF_INET;
822 p.prefixlen = IPV4_MAX_PREFIXLEN;
823 p.prefix = *ifaddr;
824
825 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
826 if (! rn)
827 {
828 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
829 inet_ntoa (*ifaddr));
830 return;
831 }
832 rn->info = NULL;
833 route_unlock_node (rn);
834 route_unlock_node (rn);
835}
836
837/* Lookup interface by interface's IP address or interface index. */
838struct interface *
839ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
840{
841 struct prefix_ipv4 p;
842 struct route_node *rn;
843 struct interface *ifp;
hasso52dc7ee2004-09-23 19:18:23 +0000844 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000845
846 if (addr)
847 {
848 p.family = AF_INET;
849 p.prefixlen = IPV4_MAX_PREFIXLEN;
850 p.prefix = *addr;
851
852 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
853 if (! rn)
854 return NULL;
855
856 ifp = rn->info;
857 route_unlock_node (rn);
858 return ifp;
859 }
860 else
ajsd2fc8892005-04-02 18:38:43 +0000861 return if_lookup_by_index(ifindex);
paul718e3742002-12-13 20:15:29 +0000862}
863
864/* Initialize interface list. */
865void
866if_init ()
867{
868 iflist = list_new ();
869 ifaddr_ipv4_table = route_table_init ();
870
paul106d2fd2003-08-01 00:24:13 +0000871 if (iflist) {
872 iflist->cmp = (int (*)(void *, void *))if_cmp_func;
paul718e3742002-12-13 20:15:29 +0000873 return;
paul106d2fd2003-08-01 00:24:13 +0000874 }
paul718e3742002-12-13 20:15:29 +0000875
876 memset (&if_master, 0, sizeof if_master);
877}