blob: c43d9564f6a5584474c1c2976d0d99ce55c4da4b [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
paul1eb8ef22005-04-07 07:30:20 +0000186 for (ALL_LIST_ELEMENTS_RO(iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000187 {
paul718e3742002-12-13 20:15:29 +0000188 if (ifp->ifindex == index)
189 return ifp;
190 }
191 return NULL;
192}
193
paul8cc41982005-05-06 21:25:49 +0000194const char *
paul718e3742002-12-13 20:15:29 +0000195ifindex2ifname (unsigned int index)
196{
paul718e3742002-12-13 20:15:29 +0000197 struct interface *ifp;
198
ajsd2fc8892005-04-02 18:38:43 +0000199 return ((ifp = if_lookup_by_index(index)) != NULL) ?
paul8cc41982005-05-06 21:25:49 +0000200 ifp->name : "unknown";
ajsd2fc8892005-04-02 18:38:43 +0000201}
202
203unsigned int
204ifname2ifindex (const char *name)
205{
206 struct interface *ifp;
207
208 return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp->ifindex : 0;
paul718e3742002-12-13 20:15:29 +0000209}
210
211/* Interface existance check by interface name. */
212struct interface *
paul9035efa2004-10-10 11:56:56 +0000213if_lookup_by_name (const char *name)
paul718e3742002-12-13 20:15:29 +0000214{
hasso52dc7ee2004-09-23 19:18:23 +0000215 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000216 struct interface *ifp;
217
paul1eb8ef22005-04-07 07:30:20 +0000218 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000219 {
ajs08dbfb62005-04-03 03:40:52 +0000220 if (strcmp(name, ifp->name) == 0)
paul718e3742002-12-13 20:15:29 +0000221 return ifp;
222 }
223 return NULL;
224}
225
ajsa3491982005-04-02 22:50:38 +0000226struct interface *
227if_lookup_by_name_len(const char *name, size_t namelen)
228{
229 struct listnode *node;
paul1eb8ef22005-04-07 07:30:20 +0000230 struct interface *ifp;
ajsa3491982005-04-02 22:50:38 +0000231
232 if (namelen > INTERFACE_NAMSIZ)
233 return NULL;
234
paul1eb8ef22005-04-07 07:30:20 +0000235 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
ajsa3491982005-04-02 22:50:38 +0000236 {
ajsa3491982005-04-02 22:50:38 +0000237 if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
238 return ifp;
239 }
240 return NULL;
241}
242
paul718e3742002-12-13 20:15:29 +0000243/* Lookup interface by IPv4 address. */
244struct interface *
245if_lookup_exact_address (struct in_addr src)
246{
hasso52dc7ee2004-09-23 19:18:23 +0000247 struct listnode *node;
248 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000249 struct interface *ifp;
250 struct prefix *p;
251 struct connected *c;
252
paul1eb8ef22005-04-07 07:30:20 +0000253 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000254 {
paul1eb8ef22005-04-07 07:30:20 +0000255 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
paul718e3742002-12-13 20:15:29 +0000256 {
paul718e3742002-12-13 20:15:29 +0000257 p = c->address;
258
259 if (p && p->family == AF_INET)
260 {
261 if (IPV4_ADDR_SAME (&p->u.prefix4, &src))
262 return ifp;
263 }
264 }
265 }
266 return NULL;
267}
268
269/* Lookup interface by IPv4 address. */
270struct interface *
271if_lookup_address (struct in_addr src)
272{
hasso52dc7ee2004-09-23 19:18:23 +0000273 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000274 struct prefix addr;
hasso3fb9cd62004-10-19 19:44:43 +0000275 int bestlen = 0;
hasso52dc7ee2004-09-23 19:18:23 +0000276 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000277 struct interface *ifp;
278 struct prefix *p;
279 struct connected *c;
280 struct interface *match;
281
paul718e3742002-12-13 20:15:29 +0000282 addr.family = AF_INET;
283 addr.u.prefix4 = src;
284 addr.prefixlen = IPV4_MAX_BITLEN;
285
286 match = NULL;
287
paul1eb8ef22005-04-07 07:30:20 +0000288 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000289 {
paul1eb8ef22005-04-07 07:30:20 +0000290 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
paul718e3742002-12-13 20:15:29 +0000291 {
hasso3fb9cd62004-10-19 19:44:43 +0000292 if (c->address && (c->address->family == AF_INET))
paul718e3742002-12-13 20:15:29 +0000293 {
hasso3fb9cd62004-10-19 19:44:43 +0000294 if (CONNECTED_POINTOPOINT_HOST(c))
paul718e3742002-12-13 20:15:29 +0000295 {
hasso3fb9cd62004-10-19 19:44:43 +0000296 /* PTP links are conventionally identified
297 by the address of the far end - MAG */
298 if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &src))
paul31a476c2003-09-29 19:54:53 +0000299 return ifp;
paul718e3742002-12-13 20:15:29 +0000300 }
hasso3fb9cd62004-10-19 19:44:43 +0000301 else
paul718e3742002-12-13 20:15:29 +0000302 {
hasso3fb9cd62004-10-19 19:44:43 +0000303 p = c->address;
304
305 if (prefix_match (p, &addr) && p->prefixlen > bestlen)
paul31a476c2003-09-29 19:54:53 +0000306 {
hasso3fb9cd62004-10-19 19:44:43 +0000307 bestlen = p->prefixlen;
paul31a476c2003-09-29 19:54:53 +0000308 match = ifp;
309 }
paul718e3742002-12-13 20:15:29 +0000310 }
311 }
312 }
313 }
314 return match;
315}
316
317/* Get interface by name if given name interface doesn't exist create
318 one. */
319struct interface *
paul9035efa2004-10-10 11:56:56 +0000320if_get_by_name (const char *name)
paul718e3742002-12-13 20:15:29 +0000321{
322 struct interface *ifp;
323
ajsa3491982005-04-02 22:50:38 +0000324 return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp :
ajs08dbfb62005-04-03 03:40:52 +0000325 if_create(name, strlen(name));
ajsa3491982005-04-02 22:50:38 +0000326}
327
328struct interface *
329if_get_by_name_len(const char *name, size_t namelen)
330{
331 struct interface *ifp;
332
333 return ((ifp = if_lookup_by_name_len(name, namelen)) != NULL) ? ifp :
334 if_create(name, namelen);
paul718e3742002-12-13 20:15:29 +0000335}
336
337/* Does interface up ? */
338int
339if_is_up (struct interface *ifp)
340{
341 return ifp->flags & IFF_UP;
342}
343
paul2e3b2e42002-12-13 21:03:13 +0000344/* Is interface running? */
345int
346if_is_running (struct interface *ifp)
347{
348 return ifp->flags & IFF_RUNNING;
349}
350
351/* Is the interface operative, eg. either UP & RUNNING
352 or UP & !ZEBRA_INTERFACE_LINK_DETECTION */
353int
354if_is_operative (struct interface *ifp)
355{
356 return ((ifp->flags & IFF_UP) &&
357 (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
358}
359
paul718e3742002-12-13 20:15:29 +0000360/* Is this loopback interface ? */
361int
362if_is_loopback (struct interface *ifp)
363{
paul4ba9b922004-12-21 22:34:58 +0000364 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
365 * but Y on platform N?
366 */
367 return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL));
paul718e3742002-12-13 20:15:29 +0000368}
369
370/* Does this interface support broadcast ? */
371int
372if_is_broadcast (struct interface *ifp)
373{
374 return ifp->flags & IFF_BROADCAST;
375}
376
377/* Does this interface support broadcast ? */
378int
379if_is_pointopoint (struct interface *ifp)
380{
381 return ifp->flags & IFF_POINTOPOINT;
382}
383
384/* Does this interface support multicast ? */
385int
386if_is_multicast (struct interface *ifp)
387{
388 return ifp->flags & IFF_MULTICAST;
389}
390
391/* Printout flag information into log */
392const char *
393if_flag_dump (unsigned long flag)
394{
395 int separator = 0;
396 static char logbuf[BUFSIZ];
397
398#define IFF_OUT_LOG(X,STR) \
paul4ba9b922004-12-21 22:34:58 +0000399 if (flag & (X)) \
paul718e3742002-12-13 20:15:29 +0000400 { \
401 if (separator) \
402 strlcat (logbuf, ",", BUFSIZ); \
403 else \
404 separator = 1; \
405 strlcat (logbuf, STR, BUFSIZ); \
406 }
407
Paul Jakma630c97c2006-06-15 12:48:17 +0000408 strlcpy (logbuf, "<", BUFSIZ);
paul718e3742002-12-13 20:15:29 +0000409 IFF_OUT_LOG (IFF_UP, "UP");
410 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
411 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
412 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
413 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
414 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
415 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
416 IFF_OUT_LOG (IFF_NOARP, "NOARP");
417 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
418 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
419 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
420 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
421 IFF_OUT_LOG (IFF_LINK0, "LINK0");
422 IFF_OUT_LOG (IFF_LINK1, "LINK1");
423 IFF_OUT_LOG (IFF_LINK2, "LINK2");
424 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
paul4ba9b922004-12-21 22:34:58 +0000425 IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT");
426 IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH");
427 IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL");
428 IFF_OUT_LOG (IFF_IPV4, "IPv4");
429 IFF_OUT_LOG (IFF_IPV6, "IPv6");
paul718e3742002-12-13 20:15:29 +0000430
431 strlcat (logbuf, ">", BUFSIZ);
432
433 return logbuf;
Paul Jakma630c97c2006-06-15 12:48:17 +0000434#undef IFF_OUT_LOG
paul718e3742002-12-13 20:15:29 +0000435}
436
437/* For debugging */
paul8cc41982005-05-06 21:25:49 +0000438static void
paul718e3742002-12-13 20:15:29 +0000439if_dump (struct interface *ifp)
440{
hasso52dc7ee2004-09-23 19:18:23 +0000441 struct listnode *node;
paul1eb8ef22005-04-07 07:30:20 +0000442 struct connected *c;
paul718e3742002-12-13 20:15:29 +0000443
paul4a7aac12004-05-08 05:00:31 +0000444 zlog_info ("Interface %s index %d metric %d mtu %d "
445#ifdef HAVE_IPV6
446 "mtu6 %d "
447#endif /* HAVE_IPV6 */
448 "%s",
paul718e3742002-12-13 20:15:29 +0000449 ifp->name, ifp->ifindex, ifp->metric, ifp->mtu,
paul4a7aac12004-05-08 05:00:31 +0000450#ifdef HAVE_IPV6
451 ifp->mtu6,
452#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +0000453 if_flag_dump (ifp->flags));
454
paul1eb8ef22005-04-07 07:30:20 +0000455 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, c))
paul718e3742002-12-13 20:15:29 +0000456 ;
457}
458
459/* Interface printing for all interface. */
460void
461if_dump_all ()
462{
hasso52dc7ee2004-09-23 19:18:23 +0000463 struct listnode *node;
paul1eb8ef22005-04-07 07:30:20 +0000464 void *p;
paul718e3742002-12-13 20:15:29 +0000465
paul1eb8ef22005-04-07 07:30:20 +0000466 for (ALL_LIST_ELEMENTS_RO (iflist, node, p))
467 if_dump (p);
paul718e3742002-12-13 20:15:29 +0000468}
469
470DEFUN (interface_desc,
471 interface_desc_cmd,
472 "description .LINE",
473 "Interface specific description\n"
474 "Characters describing this interface\n")
475{
paul718e3742002-12-13 20:15:29 +0000476 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000477
478 if (argc == 0)
479 return CMD_SUCCESS;
480
481 ifp = vty->index;
482 if (ifp->desc)
ajs3b8b1852005-01-29 18:19:13 +0000483 XFREE (MTYPE_TMP, ifp->desc);
484 ifp->desc = argv_concat(argv, argc, 0);
paul718e3742002-12-13 20:15:29 +0000485
486 return CMD_SUCCESS;
487}
488
489DEFUN (no_interface_desc,
490 no_interface_desc_cmd,
491 "no description",
492 NO_STR
493 "Interface specific description\n")
494{
495 struct interface *ifp;
496
497 ifp = vty->index;
498 if (ifp->desc)
paul02416842005-10-26 05:05:16 +0000499 XFREE (MTYPE_TMP, ifp->desc);
paul718e3742002-12-13 20:15:29 +0000500 ifp->desc = NULL;
501
502 return CMD_SUCCESS;
503}
504
505
506/* See also wrapper function zebra_interface() in zebra/interface.c */
507DEFUN (interface,
508 interface_cmd,
509 "interface IFNAME",
510 "Select an interface to configure\n"
511 "Interface's name\n")
512{
513 struct interface *ifp;
ajsd2fc8892005-04-02 18:38:43 +0000514 size_t sl;
515
516 if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ)
517 {
518 vty_out (vty, "%% Interface name %s is invalid: length exceeds "
519 "%d characters%s",
520 argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE);
521 return CMD_WARNING;
522 }
paul718e3742002-12-13 20:15:29 +0000523
ajsa3491982005-04-02 22:50:38 +0000524 ifp = if_get_by_name_len(argv[0], sl);
paul718e3742002-12-13 20:15:29 +0000525
paul718e3742002-12-13 20:15:29 +0000526 vty->index = ifp;
527 vty->node = INTERFACE_NODE;
528
529 return CMD_SUCCESS;
530}
531
paul32d24632003-05-23 09:25:20 +0000532DEFUN_NOSH (no_interface,
533 no_interface_cmd,
534 "no interface IFNAME",
535 NO_STR
536 "Delete a pseudo interface's configuration\n"
537 "Interface's name\n")
538{
539 // deleting interface
540 struct interface *ifp;
541
542 ifp = if_lookup_by_name (argv[0]);
543
544 if (ifp == NULL)
ajsd2fc8892005-04-02 18:38:43 +0000545 {
546 vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE);
547 return CMD_WARNING;
548 }
paul32d24632003-05-23 09:25:20 +0000549
paulbfc13532003-05-24 06:40:04 +0000550 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
ajsd2fc8892005-04-02 18:38:43 +0000551 {
552 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
553 VTY_NEWLINE);
554 return CMD_WARNING;
555 }
paul32d24632003-05-23 09:25:20 +0000556
557 if_delete(ifp);
558
559 return CMD_SUCCESS;
560}
561
paul718e3742002-12-13 20:15:29 +0000562/* For debug purpose. */
563DEFUN (show_address,
564 show_address_cmd,
565 "show address",
566 SHOW_STR
567 "address\n")
568{
hasso52dc7ee2004-09-23 19:18:23 +0000569 struct listnode *node;
570 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000571 struct interface *ifp;
572 struct connected *ifc;
573 struct prefix *p;
574
paul1eb8ef22005-04-07 07:30:20 +0000575 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000576 {
paul1eb8ef22005-04-07 07:30:20 +0000577 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
paul718e3742002-12-13 20:15:29 +0000578 {
paul718e3742002-12-13 20:15:29 +0000579 p = ifc->address;
580
581 if (p->family == AF_INET)
582 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
583 VTY_NEWLINE);
584 }
585 }
586 return CMD_SUCCESS;
587}
588
589/* Allocate connected structure. */
590struct connected *
paul8cc41982005-05-06 21:25:49 +0000591connected_new (void)
paul718e3742002-12-13 20:15:29 +0000592{
593 struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected));
594 memset (new, 0, sizeof (struct connected));
595 return new;
596}
597
598/* Free connected structure. */
599void
600connected_free (struct connected *connected)
601{
602 if (connected->address)
603 prefix_free (connected->address);
604
605 if (connected->destination)
606 prefix_free (connected->destination);
607
608 if (connected->label)
paul9c4f1c62005-11-03 11:04:07 +0000609 XFREE (MTYPE_CONNECTED_LABEL, connected->label);
paul718e3742002-12-13 20:15:29 +0000610
611 XFREE (MTYPE_CONNECTED, connected);
612}
613
614/* Print if_addr structure. */
paul8cc41982005-05-06 21:25:49 +0000615static void __attribute__ ((unused))
paul718e3742002-12-13 20:15:29 +0000616connected_log (struct connected *connected, char *str)
617{
618 struct prefix *p;
619 struct interface *ifp;
620 char logbuf[BUFSIZ];
621 char buf[BUFSIZ];
622
623 ifp = connected->ifp;
624 p = connected->address;
625
626 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
627 str, ifp->name, prefix_family_str (p),
628 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
629 p->prefixlen);
630
631 p = connected->destination;
632 if (p)
633 {
634 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
635 BUFSIZ - strlen(logbuf));
636 }
637 zlog (NULL, LOG_INFO, logbuf);
638}
639
640/* If two connected address has same prefix return 1. */
paul8cc41982005-05-06 21:25:49 +0000641static int
paul718e3742002-12-13 20:15:29 +0000642connected_same_prefix (struct prefix *p1, struct prefix *p2)
643{
644 if (p1->family == p2->family)
645 {
646 if (p1->family == AF_INET &&
647 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
648 return 1;
649#ifdef HAVE_IPV6
650 if (p1->family == AF_INET6 &&
651 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
652 return 1;
653#endif /* HAVE_IPV6 */
654 }
655 return 0;
656}
657
658struct connected *
659connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
660{
661 struct listnode *node;
662 struct listnode *next;
663 struct connected *ifc;
664
665 /* In case of same prefix come, replace it with new one. */
666 for (node = listhead (ifp->connected); node; node = next)
667 {
paul1eb8ef22005-04-07 07:30:20 +0000668 ifc = listgetdata (node);
paul718e3742002-12-13 20:15:29 +0000669 next = node->next;
670
671 if (connected_same_prefix (ifc->address, p))
672 {
673 listnode_delete (ifp->connected, ifc);
674 return ifc;
675 }
676 }
677 return NULL;
678}
679
paul727d1042002-12-13 20:50:29 +0000680/* Find the IPv4 address on our side that will be used when packets
681 are sent to dst. */
682struct connected *
683connected_lookup_address (struct interface *ifp, struct in_addr dst)
684{
685 struct prefix addr;
hasso52dc7ee2004-09-23 19:18:23 +0000686 struct listnode *cnode;
paul727d1042002-12-13 20:50:29 +0000687 struct prefix *p;
688 struct connected *c;
689 struct connected *match;
690
paul727d1042002-12-13 20:50:29 +0000691 addr.family = AF_INET;
692 addr.u.prefix4 = dst;
693 addr.prefixlen = IPV4_MAX_BITLEN;
694
695 match = NULL;
696
paul1eb8ef22005-04-07 07:30:20 +0000697 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
paul727d1042002-12-13 20:50:29 +0000698 {
hasso3fb9cd62004-10-19 19:44:43 +0000699 if (c->address && (c->address->family == AF_INET))
700 {
701 if (CONNECTED_POINTOPOINT_HOST(c))
paul727d1042002-12-13 20:50:29 +0000702 {
hasso3fb9cd62004-10-19 19:44:43 +0000703 /* PTP links are conventionally identified
704 by the address of the far end - MAG */
705 if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &dst))
paul727d1042002-12-13 20:50:29 +0000706 return c;
707 }
hasso3fb9cd62004-10-19 19:44:43 +0000708 else
paul727d1042002-12-13 20:50:29 +0000709 {
hasso3fb9cd62004-10-19 19:44:43 +0000710 p = c->address;
711
712 if (prefix_match (p, &addr) &&
713 (!match || (p->prefixlen > match->address->prefixlen)))
714 match = c;
paul727d1042002-12-13 20:50:29 +0000715 }
hasso3fb9cd62004-10-19 19:44:43 +0000716 }
paul727d1042002-12-13 20:50:29 +0000717 }
718 return match;
719}
720
paul4a7aac12004-05-08 05:00:31 +0000721struct connected *
722connected_add_by_prefix (struct interface *ifp, struct prefix *p,
723 struct prefix *destination)
724{
725 struct connected *ifc;
726
727 /* Allocate new connected address. */
728 ifc = connected_new ();
729 ifc->ifp = ifp;
730
731 /* Fetch interface address */
732 ifc->address = prefix_new();
733 memcpy (ifc->address, p, sizeof(struct prefix));
734
735 /* Fetch dest address */
hasso3fb9cd62004-10-19 19:44:43 +0000736 if (destination)
737 {
738 ifc->destination = prefix_new();
739 memcpy (ifc->destination, destination, sizeof(struct prefix));
740 }
paul4a7aac12004-05-08 05:00:31 +0000741
742 /* Add connected address to the interface. */
743 listnode_add (ifp->connected, ifc);
744 return ifc;
745}
746
paul718e3742002-12-13 20:15:29 +0000747#ifndef HAVE_IF_NAMETOINDEX
748unsigned int
749if_nametoindex (const char *name)
750{
paul718e3742002-12-13 20:15:29 +0000751 struct interface *ifp;
752
ajs018546e2005-04-02 23:05:56 +0000753 return ((ifp = if_lookup_by_name_len(name, strnlen(name, IFNAMSIZ))) != NULL)
754 ? ifp->ifindex : 0;
paul718e3742002-12-13 20:15:29 +0000755}
756#endif
757
758#ifndef HAVE_IF_INDEXTONAME
759char *
760if_indextoname (unsigned int ifindex, char *name)
761{
paul718e3742002-12-13 20:15:29 +0000762 struct interface *ifp;
763
ajsd2fc8892005-04-02 18:38:43 +0000764 if (!(ifp = if_lookup_by_index(ifindex)))
765 return NULL;
766 strncpy (name, ifp->name, IFNAMSIZ);
767 return ifp->name;
paul718e3742002-12-13 20:15:29 +0000768}
769#endif
770
paul8cc41982005-05-06 21:25:49 +0000771#if 0 /* this route_table of struct connected's is unused
772 * however, it would be good to use a route_table rather than
773 * a list..
774 */
paul718e3742002-12-13 20:15:29 +0000775/* Interface looking up by interface's address. */
paul718e3742002-12-13 20:15:29 +0000776/* Interface's IPv4 address reverse lookup table. */
777struct route_table *ifaddr_ipv4_table;
778/* struct route_table *ifaddr_ipv6_table; */
779
paul8cc41982005-05-06 21:25:49 +0000780static void
paul718e3742002-12-13 20:15:29 +0000781ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
782{
783 struct route_node *rn;
784 struct prefix_ipv4 p;
785
786 p.family = AF_INET;
787 p.prefixlen = IPV4_MAX_PREFIXLEN;
788 p.prefix = *ifaddr;
789
790 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
791 if (rn)
792 {
793 route_unlock_node (rn);
794 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
795 inet_ntoa (*ifaddr));
796 return;
797 }
798 rn->info = ifp;
799}
800
paul8cc41982005-05-06 21:25:49 +0000801static void
paul718e3742002-12-13 20:15:29 +0000802ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
803{
804 struct route_node *rn;
805 struct prefix_ipv4 p;
806
807 p.family = AF_INET;
808 p.prefixlen = IPV4_MAX_PREFIXLEN;
809 p.prefix = *ifaddr;
810
811 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
812 if (! rn)
813 {
814 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
815 inet_ntoa (*ifaddr));
816 return;
817 }
818 rn->info = NULL;
819 route_unlock_node (rn);
820 route_unlock_node (rn);
821}
822
823/* Lookup interface by interface's IP address or interface index. */
paul8cc41982005-05-06 21:25:49 +0000824static struct interface *
paul718e3742002-12-13 20:15:29 +0000825ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
826{
827 struct prefix_ipv4 p;
828 struct route_node *rn;
829 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000830
831 if (addr)
832 {
833 p.family = AF_INET;
834 p.prefixlen = IPV4_MAX_PREFIXLEN;
835 p.prefix = *addr;
836
837 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
838 if (! rn)
839 return NULL;
840
841 ifp = rn->info;
842 route_unlock_node (rn);
843 return ifp;
844 }
845 else
ajsd2fc8892005-04-02 18:38:43 +0000846 return if_lookup_by_index(ifindex);
paul718e3742002-12-13 20:15:29 +0000847}
paul8cc41982005-05-06 21:25:49 +0000848#endif /* ifaddr_ipv4_table */
paul718e3742002-12-13 20:15:29 +0000849
850/* Initialize interface list. */
851void
paul8cc41982005-05-06 21:25:49 +0000852if_init (void)
paul718e3742002-12-13 20:15:29 +0000853{
854 iflist = list_new ();
paul8cc41982005-05-06 21:25:49 +0000855#if 0
paul718e3742002-12-13 20:15:29 +0000856 ifaddr_ipv4_table = route_table_init ();
paul8cc41982005-05-06 21:25:49 +0000857#endif /* ifaddr_ipv4_table */
paul718e3742002-12-13 20:15:29 +0000858
paul106d2fd2003-08-01 00:24:13 +0000859 if (iflist) {
860 iflist->cmp = (int (*)(void *, void *))if_cmp_func;
paul718e3742002-12-13 20:15:29 +0000861 return;
paul106d2fd2003-08-01 00:24:13 +0000862 }
paul718e3742002-12-13 20:15:29 +0000863
864 memset (&if_master, 0, sizeof if_master);
865}