blob: e9ef50b76d121a194ca9b04c9c8ac2db87893a8f [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 */
Josh Bailey2dd04c52012-03-21 10:37:03 -0700149 list_delete_all_node (ifp->connected);
ajsd2fc8892005-04-02 18:38:43 +0000150}
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
Josh Bailey2dd04c52012-03-21 10:37:03 -0700160 list_free (ifp->connected);
161
paul718e3742002-12-13 20:15:29 +0000162 XFREE (MTYPE_IF, ifp);
163}
164
165/* Add hook to interface master. */
166void
167if_add_hook (int type, int (*func)(struct interface *ifp))
168{
169 switch (type) {
170 case IF_NEW_HOOK:
171 if_master.if_new_hook = func;
172 break;
173 case IF_DELETE_HOOK:
174 if_master.if_delete_hook = func;
175 break;
176 default:
177 break;
178 }
179}
180
181/* Interface existance check by index. */
182struct interface *
183if_lookup_by_index (unsigned int index)
184{
hasso52dc7ee2004-09-23 19:18:23 +0000185 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000186 struct interface *ifp;
187
paul1eb8ef22005-04-07 07:30:20 +0000188 for (ALL_LIST_ELEMENTS_RO(iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000189 {
paul718e3742002-12-13 20:15:29 +0000190 if (ifp->ifindex == index)
191 return ifp;
192 }
193 return NULL;
194}
195
paul8cc41982005-05-06 21:25:49 +0000196const char *
paul718e3742002-12-13 20:15:29 +0000197ifindex2ifname (unsigned int index)
198{
paul718e3742002-12-13 20:15:29 +0000199 struct interface *ifp;
200
ajsd2fc8892005-04-02 18:38:43 +0000201 return ((ifp = if_lookup_by_index(index)) != NULL) ?
paul8cc41982005-05-06 21:25:49 +0000202 ifp->name : "unknown";
ajsd2fc8892005-04-02 18:38:43 +0000203}
204
205unsigned int
206ifname2ifindex (const char *name)
207{
208 struct interface *ifp;
209
Paul Jakma3e4ee952009-08-06 12:08:50 +0100210 return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp->ifindex
211 : IFINDEX_INTERNAL;
paul718e3742002-12-13 20:15:29 +0000212}
213
214/* Interface existance check by interface name. */
215struct interface *
paul9035efa2004-10-10 11:56:56 +0000216if_lookup_by_name (const char *name)
paul718e3742002-12-13 20:15:29 +0000217{
hasso52dc7ee2004-09-23 19:18:23 +0000218 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000219 struct interface *ifp;
Paul Jakma3e4ee952009-08-06 12:08:50 +0100220
221 if (name)
222 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
223 {
224 if (strcmp(name, ifp->name) == 0)
225 return ifp;
226 }
paul718e3742002-12-13 20:15:29 +0000227 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;
paul1eb8ef22005-04-07 07:30:20 +0000234 struct interface *ifp;
ajsa3491982005-04-02 22:50:38 +0000235
236 if (namelen > INTERFACE_NAMSIZ)
237 return NULL;
238
paul1eb8ef22005-04-07 07:30:20 +0000239 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
ajsa3491982005-04-02 22:50:38 +0000240 {
ajsa3491982005-04-02 22:50:38 +0000241 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
paul1eb8ef22005-04-07 07:30:20 +0000257 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000258 {
paul1eb8ef22005-04-07 07:30:20 +0000259 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
paul718e3742002-12-13 20:15:29 +0000260 {
paul718e3742002-12-13 20:15:29 +0000261 p = c->address;
262
263 if (p && p->family == AF_INET)
264 {
265 if (IPV4_ADDR_SAME (&p->u.prefix4, &src))
266 return ifp;
267 }
268 }
269 }
270 return NULL;
271}
272
273/* Lookup interface by IPv4 address. */
274struct interface *
275if_lookup_address (struct in_addr src)
276{
hasso52dc7ee2004-09-23 19:18:23 +0000277 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000278 struct prefix addr;
hasso3fb9cd62004-10-19 19:44:43 +0000279 int bestlen = 0;
hasso52dc7ee2004-09-23 19:18:23 +0000280 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000281 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000282 struct connected *c;
283 struct interface *match;
284
paul718e3742002-12-13 20:15:29 +0000285 addr.family = AF_INET;
286 addr.u.prefix4 = src;
287 addr.prefixlen = IPV4_MAX_BITLEN;
288
289 match = NULL;
290
paul1eb8ef22005-04-07 07:30:20 +0000291 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000292 {
paul1eb8ef22005-04-07 07:30:20 +0000293 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
paul718e3742002-12-13 20:15:29 +0000294 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000295 if (c->address && (c->address->family == AF_INET) &&
296 prefix_match(CONNECTED_PREFIX(c), &addr) &&
297 (c->address->prefixlen > bestlen))
paul718e3742002-12-13 20:15:29 +0000298 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000299 bestlen = c->address->prefixlen;
300 match = ifp;
paul718e3742002-12-13 20:15:29 +0000301 }
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
ajsa3491982005-04-02 22:50:38 +0000314 return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp :
ajs08dbfb62005-04-03 03:40:52 +0000315 if_create(name, strlen(name));
ajsa3491982005-04-02 22:50:38 +0000316}
317
318struct interface *
319if_get_by_name_len(const char *name, size_t namelen)
320{
321 struct interface *ifp;
322
323 return ((ifp = if_lookup_by_name_len(name, namelen)) != NULL) ? ifp :
324 if_create(name, namelen);
paul718e3742002-12-13 20:15:29 +0000325}
326
327/* Does interface up ? */
328int
329if_is_up (struct interface *ifp)
330{
331 return ifp->flags & IFF_UP;
332}
333
paul2e3b2e42002-12-13 21:03:13 +0000334/* Is interface running? */
335int
336if_is_running (struct interface *ifp)
337{
338 return ifp->flags & IFF_RUNNING;
339}
340
341/* Is the interface operative, eg. either UP & RUNNING
342 or UP & !ZEBRA_INTERFACE_LINK_DETECTION */
343int
344if_is_operative (struct interface *ifp)
345{
346 return ((ifp->flags & IFF_UP) &&
347 (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
348}
349
paul718e3742002-12-13 20:15:29 +0000350/* Is this loopback interface ? */
351int
352if_is_loopback (struct interface *ifp)
353{
paul4ba9b922004-12-21 22:34:58 +0000354 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
355 * but Y on platform N?
356 */
357 return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL));
paul718e3742002-12-13 20:15:29 +0000358}
359
360/* Does this interface support broadcast ? */
361int
362if_is_broadcast (struct interface *ifp)
363{
364 return ifp->flags & IFF_BROADCAST;
365}
366
367/* Does this interface support broadcast ? */
368int
369if_is_pointopoint (struct interface *ifp)
370{
371 return ifp->flags & IFF_POINTOPOINT;
372}
373
374/* Does this interface support multicast ? */
375int
376if_is_multicast (struct interface *ifp)
377{
378 return ifp->flags & IFF_MULTICAST;
379}
380
381/* Printout flag information into log */
382const char *
383if_flag_dump (unsigned long flag)
384{
385 int separator = 0;
386 static char logbuf[BUFSIZ];
387
388#define IFF_OUT_LOG(X,STR) \
paul4ba9b922004-12-21 22:34:58 +0000389 if (flag & (X)) \
paul718e3742002-12-13 20:15:29 +0000390 { \
391 if (separator) \
392 strlcat (logbuf, ",", BUFSIZ); \
393 else \
394 separator = 1; \
395 strlcat (logbuf, STR, BUFSIZ); \
396 }
397
Paul Jakma630c97c2006-06-15 12:48:17 +0000398 strlcpy (logbuf, "<", BUFSIZ);
paul718e3742002-12-13 20:15:29 +0000399 IFF_OUT_LOG (IFF_UP, "UP");
400 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
401 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
402 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
403 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
404 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
405 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
406 IFF_OUT_LOG (IFF_NOARP, "NOARP");
407 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
408 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
409 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
410 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
411 IFF_OUT_LOG (IFF_LINK0, "LINK0");
412 IFF_OUT_LOG (IFF_LINK1, "LINK1");
413 IFF_OUT_LOG (IFF_LINK2, "LINK2");
414 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
paul4ba9b922004-12-21 22:34:58 +0000415 IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT");
416 IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH");
417 IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL");
418 IFF_OUT_LOG (IFF_IPV4, "IPv4");
419 IFF_OUT_LOG (IFF_IPV6, "IPv6");
paul718e3742002-12-13 20:15:29 +0000420
421 strlcat (logbuf, ">", BUFSIZ);
422
423 return logbuf;
Paul Jakma630c97c2006-06-15 12:48:17 +0000424#undef IFF_OUT_LOG
paul718e3742002-12-13 20:15:29 +0000425}
426
427/* For debugging */
paul8cc41982005-05-06 21:25:49 +0000428static void
Stephen Hemmingercedd7f22009-06-12 16:58:49 +0100429if_dump (const struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000430{
hasso52dc7ee2004-09-23 19:18:23 +0000431 struct listnode *node;
paul1eb8ef22005-04-07 07:30:20 +0000432 struct connected *c;
paul718e3742002-12-13 20:15:29 +0000433
paul4a7aac12004-05-08 05:00:31 +0000434 zlog_info ("Interface %s index %d metric %d mtu %d "
435#ifdef HAVE_IPV6
436 "mtu6 %d "
437#endif /* HAVE_IPV6 */
438 "%s",
paul718e3742002-12-13 20:15:29 +0000439 ifp->name, ifp->ifindex, ifp->metric, ifp->mtu,
paul4a7aac12004-05-08 05:00:31 +0000440#ifdef HAVE_IPV6
441 ifp->mtu6,
442#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +0000443 if_flag_dump (ifp->flags));
444
paul1eb8ef22005-04-07 07:30:20 +0000445 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, c))
paul718e3742002-12-13 20:15:29 +0000446 ;
447}
448
449/* Interface printing for all interface. */
450void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800451if_dump_all (void)
paul718e3742002-12-13 20:15:29 +0000452{
hasso52dc7ee2004-09-23 19:18:23 +0000453 struct listnode *node;
paul1eb8ef22005-04-07 07:30:20 +0000454 void *p;
paul718e3742002-12-13 20:15:29 +0000455
paul1eb8ef22005-04-07 07:30:20 +0000456 for (ALL_LIST_ELEMENTS_RO (iflist, node, p))
457 if_dump (p);
paul718e3742002-12-13 20:15:29 +0000458}
459
460DEFUN (interface_desc,
461 interface_desc_cmd,
462 "description .LINE",
463 "Interface specific description\n"
464 "Characters describing this interface\n")
465{
paul718e3742002-12-13 20:15:29 +0000466 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000467
468 if (argc == 0)
469 return CMD_SUCCESS;
470
471 ifp = vty->index;
472 if (ifp->desc)
ajs3b8b1852005-01-29 18:19:13 +0000473 XFREE (MTYPE_TMP, ifp->desc);
474 ifp->desc = argv_concat(argv, argc, 0);
paul718e3742002-12-13 20:15:29 +0000475
476 return CMD_SUCCESS;
477}
478
479DEFUN (no_interface_desc,
480 no_interface_desc_cmd,
481 "no description",
482 NO_STR
483 "Interface specific description\n")
484{
485 struct interface *ifp;
486
487 ifp = vty->index;
488 if (ifp->desc)
paul02416842005-10-26 05:05:16 +0000489 XFREE (MTYPE_TMP, ifp->desc);
paul718e3742002-12-13 20:15:29 +0000490 ifp->desc = NULL;
491
492 return CMD_SUCCESS;
493}
Paul Jakma98954842006-10-15 23:33:50 +0000494
495#ifdef SUNOS_5
496/* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
497 * a seperate struct interface for each logical interface, so config
498 * file may be full of 'interface fooX:Y'. Solaris however does not
499 * expose logical interfaces via PF_ROUTE, so trying to track logical
500 * interfaces can be fruitless, for that reason Quagga only tracks
501 * the primary IP interface.
502 *
503 * We try accomodate SUNWzebra by:
504 * - looking up the interface name, to see whether it exists, if so
505 * its useable
506 * - for protocol daemons, this could only because zebra told us of
507 * the interface
508 * - for zebra, only because it learnt from kernel
509 * - if not:
510 * - search the name to see if it contains a sub-ipif / logical interface
511 * seperator, the ':' char. If it does:
512 * - text up to that char must be the primary name - get that name.
513 * if not:
514 * - no idea, just get the name in its entirety.
515 */
516static struct interface *
517if_sunwzebra_get (const char *name, size_t nlen)
518{
519 struct interface *ifp;
520 size_t seppos = 0;
paul718e3742002-12-13 20:15:29 +0000521
Paul Jakma98954842006-10-15 23:33:50 +0000522 if ( (ifp = if_lookup_by_name_len(name, nlen)) != NULL)
523 return ifp;
524
525 /* hunt the primary interface name... */
526 while (seppos < nlen && name[seppos] != ':')
527 seppos++;
528
529 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
530 if (seppos < nlen)
531 return if_get_by_name_len (name, seppos);
532 else
533 return if_get_by_name_len (name, nlen);
534}
535#endif /* SUNOS_5 */
536
paul718e3742002-12-13 20:15:29 +0000537DEFUN (interface,
538 interface_cmd,
539 "interface IFNAME",
540 "Select an interface to configure\n"
541 "Interface's name\n")
542{
543 struct interface *ifp;
ajsd2fc8892005-04-02 18:38:43 +0000544 size_t sl;
545
546 if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ)
547 {
548 vty_out (vty, "%% Interface name %s is invalid: length exceeds "
549 "%d characters%s",
550 argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE);
551 return CMD_WARNING;
552 }
paul718e3742002-12-13 20:15:29 +0000553
Paul Jakma98954842006-10-15 23:33:50 +0000554#ifdef SUNOS_5
555 ifp = if_sunwzebra_get (argv[0], sl);
556#else
ajsa3491982005-04-02 22:50:38 +0000557 ifp = if_get_by_name_len(argv[0], sl);
Paul Jakma98954842006-10-15 23:33:50 +0000558#endif /* SUNOS_5 */
paul718e3742002-12-13 20:15:29 +0000559
paul718e3742002-12-13 20:15:29 +0000560 vty->index = ifp;
561 vty->node = INTERFACE_NODE;
562
563 return CMD_SUCCESS;
564}
565
paul32d24632003-05-23 09:25:20 +0000566DEFUN_NOSH (no_interface,
567 no_interface_cmd,
568 "no interface IFNAME",
569 NO_STR
570 "Delete a pseudo interface's configuration\n"
571 "Interface's name\n")
572{
573 // deleting interface
574 struct interface *ifp;
575
576 ifp = if_lookup_by_name (argv[0]);
577
578 if (ifp == NULL)
ajsd2fc8892005-04-02 18:38:43 +0000579 {
580 vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE);
581 return CMD_WARNING;
582 }
paul32d24632003-05-23 09:25:20 +0000583
paulbfc13532003-05-24 06:40:04 +0000584 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
ajsd2fc8892005-04-02 18:38:43 +0000585 {
586 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
587 VTY_NEWLINE);
588 return CMD_WARNING;
589 }
paul32d24632003-05-23 09:25:20 +0000590
591 if_delete(ifp);
592
593 return CMD_SUCCESS;
594}
595
paul718e3742002-12-13 20:15:29 +0000596/* For debug purpose. */
597DEFUN (show_address,
598 show_address_cmd,
599 "show address",
600 SHOW_STR
601 "address\n")
602{
hasso52dc7ee2004-09-23 19:18:23 +0000603 struct listnode *node;
604 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000605 struct interface *ifp;
606 struct connected *ifc;
607 struct prefix *p;
608
paul1eb8ef22005-04-07 07:30:20 +0000609 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000610 {
paul1eb8ef22005-04-07 07:30:20 +0000611 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
paul718e3742002-12-13 20:15:29 +0000612 {
paul718e3742002-12-13 20:15:29 +0000613 p = ifc->address;
614
615 if (p->family == AF_INET)
616 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
617 VTY_NEWLINE);
618 }
619 }
620 return CMD_SUCCESS;
621}
622
623/* Allocate connected structure. */
624struct connected *
paul8cc41982005-05-06 21:25:49 +0000625connected_new (void)
paul718e3742002-12-13 20:15:29 +0000626{
Stephen Hemminger393deb92008-08-18 14:13:29 -0700627 return XCALLOC (MTYPE_CONNECTED, sizeof (struct connected));
paul718e3742002-12-13 20:15:29 +0000628}
629
630/* Free connected structure. */
631void
632connected_free (struct connected *connected)
633{
634 if (connected->address)
635 prefix_free (connected->address);
636
637 if (connected->destination)
638 prefix_free (connected->destination);
639
640 if (connected->label)
paul9c4f1c62005-11-03 11:04:07 +0000641 XFREE (MTYPE_CONNECTED_LABEL, connected->label);
paul718e3742002-12-13 20:15:29 +0000642
643 XFREE (MTYPE_CONNECTED, connected);
644}
645
646/* Print if_addr structure. */
paul8cc41982005-05-06 21:25:49 +0000647static void __attribute__ ((unused))
paul718e3742002-12-13 20:15:29 +0000648connected_log (struct connected *connected, char *str)
649{
650 struct prefix *p;
651 struct interface *ifp;
652 char logbuf[BUFSIZ];
653 char buf[BUFSIZ];
654
655 ifp = connected->ifp;
656 p = connected->address;
657
658 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
659 str, ifp->name, prefix_family_str (p),
660 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
661 p->prefixlen);
662
663 p = connected->destination;
664 if (p)
665 {
666 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
667 BUFSIZ - strlen(logbuf));
668 }
Christian Hammersfc951862011-03-23 13:07:55 +0300669 zlog (NULL, LOG_INFO, "%s", logbuf);
paul718e3742002-12-13 20:15:29 +0000670}
671
672/* If two connected address has same prefix return 1. */
paul8cc41982005-05-06 21:25:49 +0000673static int
paul718e3742002-12-13 20:15:29 +0000674connected_same_prefix (struct prefix *p1, struct prefix *p2)
675{
676 if (p1->family == p2->family)
677 {
678 if (p1->family == AF_INET &&
679 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
680 return 1;
681#ifdef HAVE_IPV6
682 if (p1->family == AF_INET6 &&
683 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
684 return 1;
685#endif /* HAVE_IPV6 */
686 }
687 return 0;
688}
689
690struct connected *
691connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
692{
693 struct listnode *node;
694 struct listnode *next;
695 struct connected *ifc;
696
697 /* In case of same prefix come, replace it with new one. */
698 for (node = listhead (ifp->connected); node; node = next)
699 {
paul1eb8ef22005-04-07 07:30:20 +0000700 ifc = listgetdata (node);
paul718e3742002-12-13 20:15:29 +0000701 next = node->next;
702
703 if (connected_same_prefix (ifc->address, p))
704 {
705 listnode_delete (ifp->connected, ifc);
706 return ifc;
707 }
708 }
709 return NULL;
710}
711
paul727d1042002-12-13 20:50:29 +0000712/* Find the IPv4 address on our side that will be used when packets
713 are sent to dst. */
714struct connected *
715connected_lookup_address (struct interface *ifp, struct in_addr dst)
716{
717 struct prefix addr;
hasso52dc7ee2004-09-23 19:18:23 +0000718 struct listnode *cnode;
paul727d1042002-12-13 20:50:29 +0000719 struct connected *c;
720 struct connected *match;
721
paul727d1042002-12-13 20:50:29 +0000722 addr.family = AF_INET;
723 addr.u.prefix4 = dst;
724 addr.prefixlen = IPV4_MAX_BITLEN;
725
726 match = NULL;
727
paul1eb8ef22005-04-07 07:30:20 +0000728 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
paul727d1042002-12-13 20:50:29 +0000729 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000730 if (c->address && (c->address->family == AF_INET) &&
731 prefix_match(CONNECTED_PREFIX(c), &addr) &&
732 (!match || (c->address->prefixlen > match->address->prefixlen)))
733 match = c;
paul727d1042002-12-13 20:50:29 +0000734 }
735 return match;
736}
737
paul4a7aac12004-05-08 05:00:31 +0000738struct connected *
739connected_add_by_prefix (struct interface *ifp, struct prefix *p,
740 struct prefix *destination)
741{
742 struct connected *ifc;
743
744 /* Allocate new connected address. */
745 ifc = connected_new ();
746 ifc->ifp = ifp;
747
748 /* Fetch interface address */
749 ifc->address = prefix_new();
750 memcpy (ifc->address, p, sizeof(struct prefix));
751
752 /* Fetch dest address */
hasso3fb9cd62004-10-19 19:44:43 +0000753 if (destination)
754 {
755 ifc->destination = prefix_new();
756 memcpy (ifc->destination, destination, sizeof(struct prefix));
757 }
paul4a7aac12004-05-08 05:00:31 +0000758
759 /* Add connected address to the interface. */
760 listnode_add (ifp->connected, ifc);
761 return ifc;
762}
763
paul718e3742002-12-13 20:15:29 +0000764#ifndef HAVE_IF_NAMETOINDEX
765unsigned int
766if_nametoindex (const char *name)
767{
paul718e3742002-12-13 20:15:29 +0000768 struct interface *ifp;
769
ajs018546e2005-04-02 23:05:56 +0000770 return ((ifp = if_lookup_by_name_len(name, strnlen(name, IFNAMSIZ))) != NULL)
771 ? 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
paul8cc41982005-05-06 21:25:49 +0000788#if 0 /* this route_table of struct connected's is unused
789 * however, it would be good to use a route_table rather than
790 * a list..
791 */
paul718e3742002-12-13 20:15:29 +0000792/* Interface looking up by interface's address. */
paul718e3742002-12-13 20:15:29 +0000793/* Interface's IPv4 address reverse lookup table. */
794struct route_table *ifaddr_ipv4_table;
795/* struct route_table *ifaddr_ipv6_table; */
796
paul8cc41982005-05-06 21:25:49 +0000797static void
paul718e3742002-12-13 20:15:29 +0000798ifaddr_ipv4_add (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_get (ifaddr_ipv4_table, (struct prefix *) &p);
808 if (rn)
809 {
810 route_unlock_node (rn);
811 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
812 inet_ntoa (*ifaddr));
813 return;
814 }
815 rn->info = ifp;
816}
817
paul8cc41982005-05-06 21:25:49 +0000818static void
paul718e3742002-12-13 20:15:29 +0000819ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
820{
821 struct route_node *rn;
822 struct prefix_ipv4 p;
823
824 p.family = AF_INET;
825 p.prefixlen = IPV4_MAX_PREFIXLEN;
826 p.prefix = *ifaddr;
827
828 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
829 if (! rn)
830 {
831 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
832 inet_ntoa (*ifaddr));
833 return;
834 }
835 rn->info = NULL;
836 route_unlock_node (rn);
837 route_unlock_node (rn);
838}
839
840/* Lookup interface by interface's IP address or interface index. */
paul8cc41982005-05-06 21:25:49 +0000841static struct interface *
paul718e3742002-12-13 20:15:29 +0000842ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
843{
844 struct prefix_ipv4 p;
845 struct route_node *rn;
846 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000847
848 if (addr)
849 {
850 p.family = AF_INET;
851 p.prefixlen = IPV4_MAX_PREFIXLEN;
852 p.prefix = *addr;
853
854 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
855 if (! rn)
856 return NULL;
857
858 ifp = rn->info;
859 route_unlock_node (rn);
860 return ifp;
861 }
862 else
ajsd2fc8892005-04-02 18:38:43 +0000863 return if_lookup_by_index(ifindex);
paul718e3742002-12-13 20:15:29 +0000864}
paul8cc41982005-05-06 21:25:49 +0000865#endif /* ifaddr_ipv4_table */
paul718e3742002-12-13 20:15:29 +0000866
867/* Initialize interface list. */
868void
paul8cc41982005-05-06 21:25:49 +0000869if_init (void)
paul718e3742002-12-13 20:15:29 +0000870{
871 iflist = list_new ();
paul8cc41982005-05-06 21:25:49 +0000872#if 0
paul718e3742002-12-13 20:15:29 +0000873 ifaddr_ipv4_table = route_table_init ();
paul8cc41982005-05-06 21:25:49 +0000874#endif /* ifaddr_ipv4_table */
paul718e3742002-12-13 20:15:29 +0000875
paul106d2fd2003-08-01 00:24:13 +0000876 if (iflist) {
877 iflist->cmp = (int (*)(void *, void *))if_cmp_func;
paul718e3742002-12-13 20:15:29 +0000878 return;
paul106d2fd2003-08-01 00:24:13 +0000879 }
paul718e3742002-12-13 20:15:29 +0000880
881 memset (&if_master, 0, sizeof if_master);
882}
Tom Goff4bd045d2010-11-10 13:00:54 -0800883
884void
885if_terminate (void)
886{
887 for (;;)
888 {
889 struct interface *ifp;
890
891 ifp = listnode_head (iflist);
892 if (ifp == NULL)
893 break;
894
895 if_delete (ifp);
896 }
897
898 list_delete (iflist);
899 iflist = NULL;
900}