blob: dbf4f20276426e6ebda9edf2e92215f9e4d338a6 [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);
ajs08dbfb62005-04-03 03:40:52 +0000222 if (strcmp(name, ifp->name) == 0)
paul718e3742002-12-13 20:15:29 +0000223 return ifp;
224 }
225 return NULL;
226}
227
ajsa3491982005-04-02 22:50:38 +0000228struct interface *
229if_lookup_by_name_len(const char *name, size_t namelen)
230{
231 struct listnode *node;
232
233 if (namelen > INTERFACE_NAMSIZ)
234 return NULL;
235
236 for (node = listhead (iflist); node; nextnode (node))
237 {
238 struct interface *ifp;
239
240 ifp = getdata (node);
241 if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
242 return ifp;
243 }
244 return NULL;
245}
246
paul718e3742002-12-13 20:15:29 +0000247/* Lookup interface by IPv4 address. */
248struct interface *
249if_lookup_exact_address (struct in_addr src)
250{
hasso52dc7ee2004-09-23 19:18:23 +0000251 struct listnode *node;
252 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000253 struct interface *ifp;
254 struct prefix *p;
255 struct connected *c;
256
257 for (node = listhead (iflist); node; nextnode (node))
258 {
259 ifp = getdata (node);
260
261 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
262 {
263 c = getdata (cnode);
264
265 p = c->address;
266
267 if (p && p->family == AF_INET)
268 {
269 if (IPV4_ADDR_SAME (&p->u.prefix4, &src))
270 return ifp;
271 }
272 }
273 }
274 return NULL;
275}
276
277/* Lookup interface by IPv4 address. */
278struct interface *
279if_lookup_address (struct in_addr src)
280{
hasso52dc7ee2004-09-23 19:18:23 +0000281 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000282 struct prefix addr;
hasso3fb9cd62004-10-19 19:44:43 +0000283 int bestlen = 0;
hasso52dc7ee2004-09-23 19:18:23 +0000284 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000285 struct interface *ifp;
286 struct prefix *p;
287 struct connected *c;
288 struct interface *match;
289
paul718e3742002-12-13 20:15:29 +0000290 addr.family = AF_INET;
291 addr.u.prefix4 = src;
292 addr.prefixlen = IPV4_MAX_BITLEN;
293
294 match = NULL;
295
296 for (node = listhead (iflist); node; nextnode (node))
297 {
298 ifp = getdata (node);
299
300 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
301 {
302 c = getdata (cnode);
303
hasso3fb9cd62004-10-19 19:44:43 +0000304 if (c->address && (c->address->family == AF_INET))
paul718e3742002-12-13 20:15:29 +0000305 {
hasso3fb9cd62004-10-19 19:44:43 +0000306 if (CONNECTED_POINTOPOINT_HOST(c))
paul718e3742002-12-13 20:15:29 +0000307 {
hasso3fb9cd62004-10-19 19:44:43 +0000308 /* PTP links are conventionally identified
309 by the address of the far end - MAG */
310 if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &src))
paul31a476c2003-09-29 19:54:53 +0000311 return ifp;
paul718e3742002-12-13 20:15:29 +0000312 }
hasso3fb9cd62004-10-19 19:44:43 +0000313 else
paul718e3742002-12-13 20:15:29 +0000314 {
hasso3fb9cd62004-10-19 19:44:43 +0000315 p = c->address;
316
317 if (prefix_match (p, &addr) && p->prefixlen > bestlen)
paul31a476c2003-09-29 19:54:53 +0000318 {
hasso3fb9cd62004-10-19 19:44:43 +0000319 bestlen = p->prefixlen;
paul31a476c2003-09-29 19:54:53 +0000320 match = ifp;
321 }
paul718e3742002-12-13 20:15:29 +0000322 }
323 }
324 }
325 }
326 return match;
327}
328
329/* Get interface by name if given name interface doesn't exist create
330 one. */
331struct interface *
paul9035efa2004-10-10 11:56:56 +0000332if_get_by_name (const char *name)
paul718e3742002-12-13 20:15:29 +0000333{
334 struct interface *ifp;
335
ajsa3491982005-04-02 22:50:38 +0000336 return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp :
ajs08dbfb62005-04-03 03:40:52 +0000337 if_create(name, strlen(name));
ajsa3491982005-04-02 22:50:38 +0000338}
339
340struct interface *
341if_get_by_name_len(const char *name, size_t namelen)
342{
343 struct interface *ifp;
344
345 return ((ifp = if_lookup_by_name_len(name, namelen)) != NULL) ? ifp :
346 if_create(name, namelen);
paul718e3742002-12-13 20:15:29 +0000347}
348
349/* Does interface up ? */
350int
351if_is_up (struct interface *ifp)
352{
353 return ifp->flags & IFF_UP;
354}
355
paul2e3b2e42002-12-13 21:03:13 +0000356/* Is interface running? */
357int
358if_is_running (struct interface *ifp)
359{
360 return ifp->flags & IFF_RUNNING;
361}
362
363/* Is the interface operative, eg. either UP & RUNNING
364 or UP & !ZEBRA_INTERFACE_LINK_DETECTION */
365int
366if_is_operative (struct interface *ifp)
367{
368 return ((ifp->flags & IFF_UP) &&
369 (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
370}
371
paul718e3742002-12-13 20:15:29 +0000372/* Is this loopback interface ? */
373int
374if_is_loopback (struct interface *ifp)
375{
paul4ba9b922004-12-21 22:34:58 +0000376 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
377 * but Y on platform N?
378 */
379 return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL));
paul718e3742002-12-13 20:15:29 +0000380}
381
382/* Does this interface support broadcast ? */
383int
384if_is_broadcast (struct interface *ifp)
385{
386 return ifp->flags & IFF_BROADCAST;
387}
388
389/* Does this interface support broadcast ? */
390int
391if_is_pointopoint (struct interface *ifp)
392{
393 return ifp->flags & IFF_POINTOPOINT;
394}
395
396/* Does this interface support multicast ? */
397int
398if_is_multicast (struct interface *ifp)
399{
400 return ifp->flags & IFF_MULTICAST;
401}
402
403/* Printout flag information into log */
404const char *
405if_flag_dump (unsigned long flag)
406{
407 int separator = 0;
408 static char logbuf[BUFSIZ];
409
410#define IFF_OUT_LOG(X,STR) \
paul4ba9b922004-12-21 22:34:58 +0000411 if (flag & (X)) \
paul718e3742002-12-13 20:15:29 +0000412 { \
413 if (separator) \
414 strlcat (logbuf, ",", BUFSIZ); \
415 else \
416 separator = 1; \
417 strlcat (logbuf, STR, BUFSIZ); \
418 }
419
420 strlcpy (logbuf, " <", BUFSIZ);
421 IFF_OUT_LOG (IFF_UP, "UP");
422 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
423 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
424 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
425 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
426 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
427 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
428 IFF_OUT_LOG (IFF_NOARP, "NOARP");
429 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
430 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
431 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
432 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
433 IFF_OUT_LOG (IFF_LINK0, "LINK0");
434 IFF_OUT_LOG (IFF_LINK1, "LINK1");
435 IFF_OUT_LOG (IFF_LINK2, "LINK2");
436 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
paul4ba9b922004-12-21 22:34:58 +0000437 IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT");
438 IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH");
439 IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL");
440 IFF_OUT_LOG (IFF_IPV4, "IPv4");
441 IFF_OUT_LOG (IFF_IPV6, "IPv6");
paul718e3742002-12-13 20:15:29 +0000442
443 strlcat (logbuf, ">", BUFSIZ);
444
445 return logbuf;
446}
447
448/* For debugging */
449void
450if_dump (struct interface *ifp)
451{
hasso52dc7ee2004-09-23 19:18:23 +0000452 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000453
paul4a7aac12004-05-08 05:00:31 +0000454 zlog_info ("Interface %s index %d metric %d mtu %d "
455#ifdef HAVE_IPV6
456 "mtu6 %d "
457#endif /* HAVE_IPV6 */
458 "%s",
paul718e3742002-12-13 20:15:29 +0000459 ifp->name, ifp->ifindex, ifp->metric, ifp->mtu,
paul4a7aac12004-05-08 05:00:31 +0000460#ifdef HAVE_IPV6
461 ifp->mtu6,
462#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +0000463 if_flag_dump (ifp->flags));
464
465 for (node = listhead (ifp->connected); node; nextnode (node))
466 ;
467}
468
469/* Interface printing for all interface. */
470void
471if_dump_all ()
472{
hasso52dc7ee2004-09-23 19:18:23 +0000473 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000474
475 for (node = listhead (iflist); node; nextnode (node))
476 if_dump (getdata (node));
477}
478
479DEFUN (interface_desc,
480 interface_desc_cmd,
481 "description .LINE",
482 "Interface specific description\n"
483 "Characters describing this interface\n")
484{
paul718e3742002-12-13 20:15:29 +0000485 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000486
487 if (argc == 0)
488 return CMD_SUCCESS;
489
490 ifp = vty->index;
491 if (ifp->desc)
ajs3b8b1852005-01-29 18:19:13 +0000492 XFREE (MTYPE_TMP, ifp->desc);
493 ifp->desc = argv_concat(argv, argc, 0);
paul718e3742002-12-13 20:15:29 +0000494
495 return CMD_SUCCESS;
496}
497
498DEFUN (no_interface_desc,
499 no_interface_desc_cmd,
500 "no description",
501 NO_STR
502 "Interface specific description\n")
503{
504 struct interface *ifp;
505
506 ifp = vty->index;
507 if (ifp->desc)
508 XFREE (0, ifp->desc);
509 ifp->desc = NULL;
510
511 return CMD_SUCCESS;
512}
513
514
515/* See also wrapper function zebra_interface() in zebra/interface.c */
516DEFUN (interface,
517 interface_cmd,
518 "interface IFNAME",
519 "Select an interface to configure\n"
520 "Interface's name\n")
521{
522 struct interface *ifp;
ajsd2fc8892005-04-02 18:38:43 +0000523 size_t sl;
524
525 if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ)
526 {
527 vty_out (vty, "%% Interface name %s is invalid: length exceeds "
528 "%d characters%s",
529 argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE);
530 return CMD_WARNING;
531 }
paul718e3742002-12-13 20:15:29 +0000532
ajsa3491982005-04-02 22:50:38 +0000533 ifp = if_get_by_name_len(argv[0], sl);
paul718e3742002-12-13 20:15:29 +0000534
paul718e3742002-12-13 20:15:29 +0000535 vty->index = ifp;
536 vty->node = INTERFACE_NODE;
537
538 return CMD_SUCCESS;
539}
540
paul32d24632003-05-23 09:25:20 +0000541DEFUN_NOSH (no_interface,
542 no_interface_cmd,
543 "no interface IFNAME",
544 NO_STR
545 "Delete a pseudo interface's configuration\n"
546 "Interface's name\n")
547{
548 // deleting interface
549 struct interface *ifp;
550
551 ifp = if_lookup_by_name (argv[0]);
552
553 if (ifp == NULL)
ajsd2fc8892005-04-02 18:38:43 +0000554 {
555 vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE);
556 return CMD_WARNING;
557 }
paul32d24632003-05-23 09:25:20 +0000558
paulbfc13532003-05-24 06:40:04 +0000559 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
ajsd2fc8892005-04-02 18:38:43 +0000560 {
561 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
562 VTY_NEWLINE);
563 return CMD_WARNING;
564 }
paul32d24632003-05-23 09:25:20 +0000565
566 if_delete(ifp);
567
568 return CMD_SUCCESS;
569}
570
paul718e3742002-12-13 20:15:29 +0000571/* For debug purpose. */
572DEFUN (show_address,
573 show_address_cmd,
574 "show address",
575 SHOW_STR
576 "address\n")
577{
hasso52dc7ee2004-09-23 19:18:23 +0000578 struct listnode *node;
579 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000580 struct interface *ifp;
581 struct connected *ifc;
582 struct prefix *p;
583
584 for (node = listhead (iflist); node; nextnode (node))
585 {
586 ifp = getdata (node);
587
588 for (node2 = listhead (ifp->connected); node2; nextnode (node2))
589 {
590 ifc = getdata (node2);
591 p = ifc->address;
592
593 if (p->family == AF_INET)
594 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
595 VTY_NEWLINE);
596 }
597 }
598 return CMD_SUCCESS;
599}
600
601/* Allocate connected structure. */
602struct connected *
603connected_new ()
604{
605 struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected));
606 memset (new, 0, sizeof (struct connected));
607 return new;
608}
609
610/* Free connected structure. */
611void
612connected_free (struct connected *connected)
613{
614 if (connected->address)
615 prefix_free (connected->address);
616
617 if (connected->destination)
618 prefix_free (connected->destination);
619
620 if (connected->label)
621 free (connected->label);
622
623 XFREE (MTYPE_CONNECTED, connected);
624}
625
626/* Print if_addr structure. */
627void
628connected_log (struct connected *connected, char *str)
629{
630 struct prefix *p;
631 struct interface *ifp;
632 char logbuf[BUFSIZ];
633 char buf[BUFSIZ];
634
635 ifp = connected->ifp;
636 p = connected->address;
637
638 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
639 str, ifp->name, prefix_family_str (p),
640 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
641 p->prefixlen);
642
643 p = connected->destination;
644 if (p)
645 {
646 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
647 BUFSIZ - strlen(logbuf));
648 }
649 zlog (NULL, LOG_INFO, logbuf);
650}
651
652/* If two connected address has same prefix return 1. */
653int
654connected_same_prefix (struct prefix *p1, struct prefix *p2)
655{
656 if (p1->family == p2->family)
657 {
658 if (p1->family == AF_INET &&
659 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
660 return 1;
661#ifdef HAVE_IPV6
662 if (p1->family == AF_INET6 &&
663 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
664 return 1;
665#endif /* HAVE_IPV6 */
666 }
667 return 0;
668}
669
670struct connected *
671connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
672{
673 struct listnode *node;
674 struct listnode *next;
675 struct connected *ifc;
676
677 /* In case of same prefix come, replace it with new one. */
678 for (node = listhead (ifp->connected); node; node = next)
679 {
680 ifc = getdata (node);
681 next = node->next;
682
683 if (connected_same_prefix (ifc->address, p))
684 {
685 listnode_delete (ifp->connected, ifc);
686 return ifc;
687 }
688 }
689 return NULL;
690}
691
paul727d1042002-12-13 20:50:29 +0000692/* Find the IPv4 address on our side that will be used when packets
693 are sent to dst. */
694struct connected *
695connected_lookup_address (struct interface *ifp, struct in_addr dst)
696{
697 struct prefix addr;
hasso52dc7ee2004-09-23 19:18:23 +0000698 struct listnode *cnode;
paul727d1042002-12-13 20:50:29 +0000699 struct prefix *p;
700 struct connected *c;
701 struct connected *match;
702
paul727d1042002-12-13 20:50:29 +0000703 addr.family = AF_INET;
704 addr.u.prefix4 = dst;
705 addr.prefixlen = IPV4_MAX_BITLEN;
706
707 match = NULL;
708
709 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
710 {
711 c = getdata (cnode);
712
hasso3fb9cd62004-10-19 19:44:43 +0000713 if (c->address && (c->address->family == AF_INET))
714 {
715 if (CONNECTED_POINTOPOINT_HOST(c))
paul727d1042002-12-13 20:50:29 +0000716 {
hasso3fb9cd62004-10-19 19:44:43 +0000717 /* PTP links are conventionally identified
718 by the address of the far end - MAG */
719 if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &dst))
paul727d1042002-12-13 20:50:29 +0000720 return c;
721 }
hasso3fb9cd62004-10-19 19:44:43 +0000722 else
paul727d1042002-12-13 20:50:29 +0000723 {
hasso3fb9cd62004-10-19 19:44:43 +0000724 p = c->address;
725
726 if (prefix_match (p, &addr) &&
727 (!match || (p->prefixlen > match->address->prefixlen)))
728 match = c;
paul727d1042002-12-13 20:50:29 +0000729 }
hasso3fb9cd62004-10-19 19:44:43 +0000730 }
paul727d1042002-12-13 20:50:29 +0000731 }
732 return match;
733}
734
paul4a7aac12004-05-08 05:00:31 +0000735struct connected *
736connected_add_by_prefix (struct interface *ifp, struct prefix *p,
737 struct prefix *destination)
738{
739 struct connected *ifc;
740
741 /* Allocate new connected address. */
742 ifc = connected_new ();
743 ifc->ifp = ifp;
744
745 /* Fetch interface address */
746 ifc->address = prefix_new();
747 memcpy (ifc->address, p, sizeof(struct prefix));
748
749 /* Fetch dest address */
hasso3fb9cd62004-10-19 19:44:43 +0000750 if (destination)
751 {
752 ifc->destination = prefix_new();
753 memcpy (ifc->destination, destination, sizeof(struct prefix));
754 }
paul4a7aac12004-05-08 05:00:31 +0000755
756 /* Add connected address to the interface. */
757 listnode_add (ifp->connected, ifc);
758 return ifc;
759}
760
paul718e3742002-12-13 20:15:29 +0000761#ifndef HAVE_IF_NAMETOINDEX
762unsigned int
763if_nametoindex (const char *name)
764{
paul718e3742002-12-13 20:15:29 +0000765 struct interface *ifp;
766
ajs018546e2005-04-02 23:05:56 +0000767 return ((ifp = if_lookup_by_name_len(name, strnlen(name, IFNAMSIZ))) != NULL)
768 ? ifp->ifindex : 0;
paul718e3742002-12-13 20:15:29 +0000769}
770#endif
771
772#ifndef HAVE_IF_INDEXTONAME
773char *
774if_indextoname (unsigned int ifindex, char *name)
775{
paul718e3742002-12-13 20:15:29 +0000776 struct interface *ifp;
777
ajsd2fc8892005-04-02 18:38:43 +0000778 if (!(ifp = if_lookup_by_index(ifindex)))
779 return NULL;
780 strncpy (name, ifp->name, IFNAMSIZ);
781 return ifp->name;
paul718e3742002-12-13 20:15:29 +0000782}
783#endif
784
785/* Interface looking up by interface's address. */
786
787/* Interface's IPv4 address reverse lookup table. */
788struct route_table *ifaddr_ipv4_table;
789/* struct route_table *ifaddr_ipv6_table; */
790
791void
792ifaddr_ipv4_add (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_get (ifaddr_ipv4_table, (struct prefix *) &p);
802 if (rn)
803 {
804 route_unlock_node (rn);
805 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
806 inet_ntoa (*ifaddr));
807 return;
808 }
809 rn->info = ifp;
810}
811
812void
813ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
814{
815 struct route_node *rn;
816 struct prefix_ipv4 p;
817
818 p.family = AF_INET;
819 p.prefixlen = IPV4_MAX_PREFIXLEN;
820 p.prefix = *ifaddr;
821
822 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
823 if (! rn)
824 {
825 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
826 inet_ntoa (*ifaddr));
827 return;
828 }
829 rn->info = NULL;
830 route_unlock_node (rn);
831 route_unlock_node (rn);
832}
833
834/* Lookup interface by interface's IP address or interface index. */
835struct interface *
836ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
837{
838 struct prefix_ipv4 p;
839 struct route_node *rn;
840 struct interface *ifp;
hasso52dc7ee2004-09-23 19:18:23 +0000841 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000842
843 if (addr)
844 {
845 p.family = AF_INET;
846 p.prefixlen = IPV4_MAX_PREFIXLEN;
847 p.prefix = *addr;
848
849 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
850 if (! rn)
851 return NULL;
852
853 ifp = rn->info;
854 route_unlock_node (rn);
855 return ifp;
856 }
857 else
ajsd2fc8892005-04-02 18:38:43 +0000858 return if_lookup_by_index(ifindex);
paul718e3742002-12-13 20:15:29 +0000859}
860
861/* Initialize interface list. */
862void
863if_init ()
864{
865 iflist = list_new ();
866 ifaddr_ipv4_table = route_table_init ();
867
paul106d2fd2003-08-01 00:24:13 +0000868 if (iflist) {
869 iflist->cmp = (int (*)(void *, void *))if_cmp_func;
paul718e3742002-12-13 20:15:29 +0000870 return;
paul106d2fd2003-08-01 00:24:13 +0000871 }
paul718e3742002-12-13 20:15:29 +0000872
873 memset (&if_master, 0, sizeof if_master);
874}