blob: 2b46a1bb9d02345dc8e908aad4359efb8719a8c0 [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"
David Lamparter6b0655a2014-06-04 06:53:35 +020038
paul718e3742002-12-13 20:15:29 +000039/* 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;
David Lamparter6b0655a2014-06-04 06:53:35 +020048
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
Dinesh G Dutt77322442014-09-30 12:39:24 -0700135 /* Enable Link-detection by default */
136 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
137
paul718e3742002-12-13 20:15:29 +0000138 if (if_master.if_new_hook)
139 (*if_master.if_new_hook) (ifp);
140
141 return ifp;
142}
143
ajsd2fc8892005-04-02 18:38:43 +0000144/* Delete interface structure. */
145void
146if_delete_retain (struct interface *ifp)
147{
148 if (if_master.if_delete_hook)
149 (*if_master.if_delete_hook) (ifp);
150
151 /* Free connected address list */
Josh Bailey2dd04c52012-03-21 10:37:03 -0700152 list_delete_all_node (ifp->connected);
ajsd2fc8892005-04-02 18:38:43 +0000153}
154
paul718e3742002-12-13 20:15:29 +0000155/* Delete and free interface structure. */
156void
157if_delete (struct interface *ifp)
158{
159 listnode_delete (iflist, ifp);
160
ajsd2fc8892005-04-02 18:38:43 +0000161 if_delete_retain(ifp);
paul718e3742002-12-13 20:15:29 +0000162
Josh Bailey2dd04c52012-03-21 10:37:03 -0700163 list_free (ifp->connected);
164
paul718e3742002-12-13 20:15:29 +0000165 XFREE (MTYPE_IF, ifp);
166}
167
168/* Add hook to interface master. */
169void
170if_add_hook (int type, int (*func)(struct interface *ifp))
171{
172 switch (type) {
173 case IF_NEW_HOOK:
174 if_master.if_new_hook = func;
175 break;
176 case IF_DELETE_HOOK:
177 if_master.if_delete_hook = func;
178 break;
179 default:
180 break;
181 }
182}
183
184/* Interface existance check by index. */
185struct interface *
186if_lookup_by_index (unsigned int index)
187{
hasso52dc7ee2004-09-23 19:18:23 +0000188 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000189 struct interface *ifp;
190
paul1eb8ef22005-04-07 07:30:20 +0000191 for (ALL_LIST_ELEMENTS_RO(iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000192 {
paul718e3742002-12-13 20:15:29 +0000193 if (ifp->ifindex == index)
194 return ifp;
195 }
196 return NULL;
197}
198
paul8cc41982005-05-06 21:25:49 +0000199const char *
paul718e3742002-12-13 20:15:29 +0000200ifindex2ifname (unsigned int index)
201{
paul718e3742002-12-13 20:15:29 +0000202 struct interface *ifp;
203
ajsd2fc8892005-04-02 18:38:43 +0000204 return ((ifp = if_lookup_by_index(index)) != NULL) ?
paul8cc41982005-05-06 21:25:49 +0000205 ifp->name : "unknown";
ajsd2fc8892005-04-02 18:38:43 +0000206}
207
208unsigned int
209ifname2ifindex (const char *name)
210{
211 struct interface *ifp;
212
Paul Jakma3e4ee952009-08-06 12:08:50 +0100213 return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp->ifindex
214 : IFINDEX_INTERNAL;
paul718e3742002-12-13 20:15:29 +0000215}
216
217/* Interface existance check by interface name. */
218struct interface *
paul9035efa2004-10-10 11:56:56 +0000219if_lookup_by_name (const char *name)
paul718e3742002-12-13 20:15:29 +0000220{
hasso52dc7ee2004-09-23 19:18:23 +0000221 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000222 struct interface *ifp;
Paul Jakma3e4ee952009-08-06 12:08:50 +0100223
224 if (name)
225 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
226 {
227 if (strcmp(name, ifp->name) == 0)
228 return ifp;
229 }
paul718e3742002-12-13 20:15:29 +0000230 return NULL;
231}
232
ajsa3491982005-04-02 22:50:38 +0000233struct interface *
234if_lookup_by_name_len(const char *name, size_t namelen)
235{
236 struct listnode *node;
paul1eb8ef22005-04-07 07:30:20 +0000237 struct interface *ifp;
ajsa3491982005-04-02 22:50:38 +0000238
239 if (namelen > INTERFACE_NAMSIZ)
240 return NULL;
241
paul1eb8ef22005-04-07 07:30:20 +0000242 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
ajsa3491982005-04-02 22:50:38 +0000243 {
ajsa3491982005-04-02 22:50:38 +0000244 if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
245 return ifp;
246 }
247 return NULL;
248}
249
paul718e3742002-12-13 20:15:29 +0000250/* Lookup interface by IPv4 address. */
251struct interface *
252if_lookup_exact_address (struct in_addr src)
253{
hasso52dc7ee2004-09-23 19:18:23 +0000254 struct listnode *node;
255 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000256 struct interface *ifp;
257 struct prefix *p;
258 struct connected *c;
259
paul1eb8ef22005-04-07 07:30:20 +0000260 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000261 {
paul1eb8ef22005-04-07 07:30:20 +0000262 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
paul718e3742002-12-13 20:15:29 +0000263 {
paul718e3742002-12-13 20:15:29 +0000264 p = c->address;
265
266 if (p && p->family == AF_INET)
267 {
268 if (IPV4_ADDR_SAME (&p->u.prefix4, &src))
269 return ifp;
270 }
271 }
272 }
273 return NULL;
274}
275
276/* Lookup interface by IPv4 address. */
277struct interface *
278if_lookup_address (struct in_addr src)
279{
hasso52dc7ee2004-09-23 19:18:23 +0000280 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000281 struct prefix addr;
hasso3fb9cd62004-10-19 19:44:43 +0000282 int bestlen = 0;
hasso52dc7ee2004-09-23 19:18:23 +0000283 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000284 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000285 struct connected *c;
286 struct interface *match;
287
paul718e3742002-12-13 20:15:29 +0000288 addr.family = AF_INET;
289 addr.u.prefix4 = src;
290 addr.prefixlen = IPV4_MAX_BITLEN;
291
292 match = NULL;
293
paul1eb8ef22005-04-07 07:30:20 +0000294 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000295 {
paul1eb8ef22005-04-07 07:30:20 +0000296 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
paul718e3742002-12-13 20:15:29 +0000297 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000298 if (c->address && (c->address->family == AF_INET) &&
299 prefix_match(CONNECTED_PREFIX(c), &addr) &&
300 (c->address->prefixlen > bestlen))
paul718e3742002-12-13 20:15:29 +0000301 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000302 bestlen = c->address->prefixlen;
303 match = ifp;
paul718e3742002-12-13 20:15:29 +0000304 }
305 }
306 }
307 return match;
308}
309
Dinesh Duttb81e97a2013-08-24 07:55:50 +0000310/* Lookup interface by prefix */
311struct interface *
312if_lookup_prefix (struct prefix *prefix)
313{
314 struct listnode *node;
Dinesh Duttb81e97a2013-08-24 07:55:50 +0000315 struct listnode *cnode;
316 struct interface *ifp;
317 struct connected *c;
318
319 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
320 {
321 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
322 {
323 if (prefix_cmp(c->address, prefix) == 0)
324 {
325 return ifp;
326 }
327 }
328 }
329 return NULL;
330}
331
paul718e3742002-12-13 20:15:29 +0000332/* Get interface by name if given name interface doesn't exist create
333 one. */
334struct interface *
paul9035efa2004-10-10 11:56:56 +0000335if_get_by_name (const char *name)
paul718e3742002-12-13 20:15:29 +0000336{
337 struct interface *ifp;
338
ajsa3491982005-04-02 22:50:38 +0000339 return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp :
ajs08dbfb62005-04-03 03:40:52 +0000340 if_create(name, strlen(name));
ajsa3491982005-04-02 22:50:38 +0000341}
342
343struct interface *
344if_get_by_name_len(const char *name, size_t namelen)
345{
346 struct interface *ifp;
347
348 return ((ifp = if_lookup_by_name_len(name, namelen)) != NULL) ? ifp :
349 if_create(name, namelen);
paul718e3742002-12-13 20:15:29 +0000350}
351
352/* Does interface up ? */
353int
354if_is_up (struct interface *ifp)
355{
356 return ifp->flags & IFF_UP;
357}
358
paul2e3b2e42002-12-13 21:03:13 +0000359/* Is interface running? */
360int
361if_is_running (struct interface *ifp)
362{
363 return ifp->flags & IFF_RUNNING;
364}
365
366/* Is the interface operative, eg. either UP & RUNNING
367 or UP & !ZEBRA_INTERFACE_LINK_DETECTION */
368int
369if_is_operative (struct interface *ifp)
370{
371 return ((ifp->flags & IFF_UP) &&
372 (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
373}
374
paul718e3742002-12-13 20:15:29 +0000375/* Is this loopback interface ? */
376int
377if_is_loopback (struct interface *ifp)
378{
paul4ba9b922004-12-21 22:34:58 +0000379 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
380 * but Y on platform N?
381 */
382 return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL));
paul718e3742002-12-13 20:15:29 +0000383}
384
385/* Does this interface support broadcast ? */
386int
387if_is_broadcast (struct interface *ifp)
388{
389 return ifp->flags & IFF_BROADCAST;
390}
391
392/* Does this interface support broadcast ? */
393int
394if_is_pointopoint (struct interface *ifp)
395{
396 return ifp->flags & IFF_POINTOPOINT;
397}
398
399/* Does this interface support multicast ? */
400int
401if_is_multicast (struct interface *ifp)
402{
403 return ifp->flags & IFF_MULTICAST;
404}
405
406/* Printout flag information into log */
407const char *
408if_flag_dump (unsigned long flag)
409{
410 int separator = 0;
411 static char logbuf[BUFSIZ];
412
413#define IFF_OUT_LOG(X,STR) \
paul4ba9b922004-12-21 22:34:58 +0000414 if (flag & (X)) \
paul718e3742002-12-13 20:15:29 +0000415 { \
416 if (separator) \
417 strlcat (logbuf, ",", BUFSIZ); \
418 else \
419 separator = 1; \
420 strlcat (logbuf, STR, BUFSIZ); \
421 }
422
Paul Jakma630c97c2006-06-15 12:48:17 +0000423 strlcpy (logbuf, "<", BUFSIZ);
paul718e3742002-12-13 20:15:29 +0000424 IFF_OUT_LOG (IFF_UP, "UP");
425 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
426 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
427 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
428 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
429 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
430 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
431 IFF_OUT_LOG (IFF_NOARP, "NOARP");
432 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
433 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
434 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
435 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
436 IFF_OUT_LOG (IFF_LINK0, "LINK0");
437 IFF_OUT_LOG (IFF_LINK1, "LINK1");
438 IFF_OUT_LOG (IFF_LINK2, "LINK2");
439 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
paul4ba9b922004-12-21 22:34:58 +0000440 IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT");
441 IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH");
442 IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL");
443 IFF_OUT_LOG (IFF_IPV4, "IPv4");
444 IFF_OUT_LOG (IFF_IPV6, "IPv6");
paul718e3742002-12-13 20:15:29 +0000445
446 strlcat (logbuf, ">", BUFSIZ);
447
448 return logbuf;
Paul Jakma630c97c2006-06-15 12:48:17 +0000449#undef IFF_OUT_LOG
paul718e3742002-12-13 20:15:29 +0000450}
451
452/* For debugging */
paul8cc41982005-05-06 21:25:49 +0000453static void
Stephen Hemmingercedd7f22009-06-12 16:58:49 +0100454if_dump (const struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000455{
hasso52dc7ee2004-09-23 19:18:23 +0000456 struct listnode *node;
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100457 struct connected *c __attribute__((unused));
paul718e3742002-12-13 20:15:29 +0000458
paul1eb8ef22005-04-07 07:30:20 +0000459 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, c))
Paul Jakma23be94e2012-01-06 16:07:39 +0000460 zlog_info ("Interface %s index %d metric %d mtu %d "
paul4a7aac12004-05-08 05:00:31 +0000461#ifdef HAVE_IPV6
Paul Jakma23be94e2012-01-06 16:07:39 +0000462 "mtu6 %d "
paul4a7aac12004-05-08 05:00:31 +0000463#endif /* HAVE_IPV6 */
Paul Jakma23be94e2012-01-06 16:07:39 +0000464 "%s",
465 ifp->name, ifp->ifindex, ifp->metric, ifp->mtu,
paul4a7aac12004-05-08 05:00:31 +0000466#ifdef HAVE_IPV6
Paul Jakma23be94e2012-01-06 16:07:39 +0000467 ifp->mtu6,
paul4a7aac12004-05-08 05:00:31 +0000468#endif /* HAVE_IPV6 */
Paul Jakma23be94e2012-01-06 16:07:39 +0000469 if_flag_dump (ifp->flags));
paul718e3742002-12-13 20:15:29 +0000470}
471
472/* Interface printing for all interface. */
473void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800474if_dump_all (void)
paul718e3742002-12-13 20:15:29 +0000475{
hasso52dc7ee2004-09-23 19:18:23 +0000476 struct listnode *node;
paul1eb8ef22005-04-07 07:30:20 +0000477 void *p;
paul718e3742002-12-13 20:15:29 +0000478
paul1eb8ef22005-04-07 07:30:20 +0000479 for (ALL_LIST_ELEMENTS_RO (iflist, node, p))
480 if_dump (p);
paul718e3742002-12-13 20:15:29 +0000481}
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)
paul02416842005-10-26 05:05:16 +0000512 XFREE (MTYPE_TMP, ifp->desc);
paul718e3742002-12-13 20:15:29 +0000513 ifp->desc = NULL;
514
515 return CMD_SUCCESS;
516}
David Lamparter6b0655a2014-06-04 06:53:35 +0200517
Paul Jakma98954842006-10-15 23:33:50 +0000518#ifdef SUNOS_5
519/* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
520 * a seperate struct interface for each logical interface, so config
521 * file may be full of 'interface fooX:Y'. Solaris however does not
522 * expose logical interfaces via PF_ROUTE, so trying to track logical
523 * interfaces can be fruitless, for that reason Quagga only tracks
524 * the primary IP interface.
525 *
526 * We try accomodate SUNWzebra by:
527 * - looking up the interface name, to see whether it exists, if so
528 * its useable
529 * - for protocol daemons, this could only because zebra told us of
530 * the interface
531 * - for zebra, only because it learnt from kernel
532 * - if not:
533 * - search the name to see if it contains a sub-ipif / logical interface
534 * seperator, the ':' char. If it does:
535 * - text up to that char must be the primary name - get that name.
536 * if not:
537 * - no idea, just get the name in its entirety.
538 */
539static struct interface *
540if_sunwzebra_get (const char *name, size_t nlen)
541{
542 struct interface *ifp;
543 size_t seppos = 0;
paul718e3742002-12-13 20:15:29 +0000544
Paul Jakma98954842006-10-15 23:33:50 +0000545 if ( (ifp = if_lookup_by_name_len(name, nlen)) != NULL)
546 return ifp;
547
548 /* hunt the primary interface name... */
549 while (seppos < nlen && name[seppos] != ':')
550 seppos++;
551
552 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
553 if (seppos < nlen)
554 return if_get_by_name_len (name, seppos);
555 else
556 return if_get_by_name_len (name, nlen);
557}
558#endif /* SUNOS_5 */
David Lamparter6b0655a2014-06-04 06:53:35 +0200559
paul718e3742002-12-13 20:15:29 +0000560DEFUN (interface,
561 interface_cmd,
562 "interface IFNAME",
563 "Select an interface to configure\n"
564 "Interface's name\n")
565{
566 struct interface *ifp;
ajsd2fc8892005-04-02 18:38:43 +0000567 size_t sl;
568
569 if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ)
570 {
571 vty_out (vty, "%% Interface name %s is invalid: length exceeds "
572 "%d characters%s",
573 argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE);
574 return CMD_WARNING;
575 }
paul718e3742002-12-13 20:15:29 +0000576
Paul Jakma98954842006-10-15 23:33:50 +0000577#ifdef SUNOS_5
578 ifp = if_sunwzebra_get (argv[0], sl);
579#else
ajsa3491982005-04-02 22:50:38 +0000580 ifp = if_get_by_name_len(argv[0], sl);
Paul Jakma98954842006-10-15 23:33:50 +0000581#endif /* SUNOS_5 */
paul718e3742002-12-13 20:15:29 +0000582
paul718e3742002-12-13 20:15:29 +0000583 vty->index = ifp;
584 vty->node = INTERFACE_NODE;
585
586 return CMD_SUCCESS;
587}
588
paul32d24632003-05-23 09:25:20 +0000589DEFUN_NOSH (no_interface,
590 no_interface_cmd,
591 "no interface IFNAME",
592 NO_STR
593 "Delete a pseudo interface's configuration\n"
594 "Interface's name\n")
595{
596 // deleting interface
597 struct interface *ifp;
598
599 ifp = if_lookup_by_name (argv[0]);
600
601 if (ifp == NULL)
ajsd2fc8892005-04-02 18:38:43 +0000602 {
603 vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE);
604 return CMD_WARNING;
605 }
paul32d24632003-05-23 09:25:20 +0000606
paulbfc13532003-05-24 06:40:04 +0000607 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
ajsd2fc8892005-04-02 18:38:43 +0000608 {
609 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
610 VTY_NEWLINE);
611 return CMD_WARNING;
612 }
paul32d24632003-05-23 09:25:20 +0000613
614 if_delete(ifp);
615
616 return CMD_SUCCESS;
617}
618
paul718e3742002-12-13 20:15:29 +0000619/* For debug purpose. */
620DEFUN (show_address,
621 show_address_cmd,
622 "show address",
623 SHOW_STR
624 "address\n")
625{
hasso52dc7ee2004-09-23 19:18:23 +0000626 struct listnode *node;
627 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000628 struct interface *ifp;
629 struct connected *ifc;
630 struct prefix *p;
631
paul1eb8ef22005-04-07 07:30:20 +0000632 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000633 {
paul1eb8ef22005-04-07 07:30:20 +0000634 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
paul718e3742002-12-13 20:15:29 +0000635 {
paul718e3742002-12-13 20:15:29 +0000636 p = ifc->address;
637
638 if (p->family == AF_INET)
639 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
640 VTY_NEWLINE);
641 }
642 }
643 return CMD_SUCCESS;
644}
645
646/* Allocate connected structure. */
647struct connected *
paul8cc41982005-05-06 21:25:49 +0000648connected_new (void)
paul718e3742002-12-13 20:15:29 +0000649{
Stephen Hemminger393deb92008-08-18 14:13:29 -0700650 return XCALLOC (MTYPE_CONNECTED, sizeof (struct connected));
paul718e3742002-12-13 20:15:29 +0000651}
652
653/* Free connected structure. */
654void
655connected_free (struct connected *connected)
656{
657 if (connected->address)
658 prefix_free (connected->address);
659
660 if (connected->destination)
661 prefix_free (connected->destination);
662
663 if (connected->label)
paul9c4f1c62005-11-03 11:04:07 +0000664 XFREE (MTYPE_CONNECTED_LABEL, connected->label);
paul718e3742002-12-13 20:15:29 +0000665
666 XFREE (MTYPE_CONNECTED, connected);
667}
668
669/* Print if_addr structure. */
paul8cc41982005-05-06 21:25:49 +0000670static void __attribute__ ((unused))
paul718e3742002-12-13 20:15:29 +0000671connected_log (struct connected *connected, char *str)
672{
673 struct prefix *p;
674 struct interface *ifp;
675 char logbuf[BUFSIZ];
676 char buf[BUFSIZ];
677
678 ifp = connected->ifp;
679 p = connected->address;
680
681 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
682 str, ifp->name, prefix_family_str (p),
683 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
684 p->prefixlen);
685
686 p = connected->destination;
687 if (p)
688 {
689 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
690 BUFSIZ - strlen(logbuf));
691 }
Christian Hammersfc951862011-03-23 13:07:55 +0300692 zlog (NULL, LOG_INFO, "%s", logbuf);
paul718e3742002-12-13 20:15:29 +0000693}
694
695/* If two connected address has same prefix return 1. */
paul8cc41982005-05-06 21:25:49 +0000696static int
paul718e3742002-12-13 20:15:29 +0000697connected_same_prefix (struct prefix *p1, struct prefix *p2)
698{
699 if (p1->family == p2->family)
700 {
701 if (p1->family == AF_INET &&
702 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
703 return 1;
704#ifdef HAVE_IPV6
705 if (p1->family == AF_INET6 &&
706 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
707 return 1;
708#endif /* HAVE_IPV6 */
709 }
710 return 0;
711}
712
713struct connected *
714connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
715{
716 struct listnode *node;
717 struct listnode *next;
718 struct connected *ifc;
719
720 /* In case of same prefix come, replace it with new one. */
721 for (node = listhead (ifp->connected); node; node = next)
722 {
paul1eb8ef22005-04-07 07:30:20 +0000723 ifc = listgetdata (node);
paul718e3742002-12-13 20:15:29 +0000724 next = node->next;
725
726 if (connected_same_prefix (ifc->address, p))
727 {
728 listnode_delete (ifp->connected, ifc);
729 return ifc;
730 }
731 }
732 return NULL;
733}
734
paul727d1042002-12-13 20:50:29 +0000735/* Find the IPv4 address on our side that will be used when packets
736 are sent to dst. */
737struct connected *
738connected_lookup_address (struct interface *ifp, struct in_addr dst)
739{
740 struct prefix addr;
hasso52dc7ee2004-09-23 19:18:23 +0000741 struct listnode *cnode;
paul727d1042002-12-13 20:50:29 +0000742 struct connected *c;
743 struct connected *match;
744
paul727d1042002-12-13 20:50:29 +0000745 addr.family = AF_INET;
746 addr.u.prefix4 = dst;
747 addr.prefixlen = IPV4_MAX_BITLEN;
748
749 match = NULL;
750
paul1eb8ef22005-04-07 07:30:20 +0000751 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
paul727d1042002-12-13 20:50:29 +0000752 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000753 if (c->address && (c->address->family == AF_INET) &&
754 prefix_match(CONNECTED_PREFIX(c), &addr) &&
755 (!match || (c->address->prefixlen > match->address->prefixlen)))
756 match = c;
paul727d1042002-12-13 20:50:29 +0000757 }
758 return match;
759}
760
paul4a7aac12004-05-08 05:00:31 +0000761struct connected *
762connected_add_by_prefix (struct interface *ifp, struct prefix *p,
763 struct prefix *destination)
764{
765 struct connected *ifc;
766
767 /* Allocate new connected address. */
768 ifc = connected_new ();
769 ifc->ifp = ifp;
770
771 /* Fetch interface address */
772 ifc->address = prefix_new();
773 memcpy (ifc->address, p, sizeof(struct prefix));
774
775 /* Fetch dest address */
hasso3fb9cd62004-10-19 19:44:43 +0000776 if (destination)
777 {
778 ifc->destination = prefix_new();
779 memcpy (ifc->destination, destination, sizeof(struct prefix));
780 }
paul4a7aac12004-05-08 05:00:31 +0000781
782 /* Add connected address to the interface. */
783 listnode_add (ifp->connected, ifc);
784 return ifc;
785}
786
paul718e3742002-12-13 20:15:29 +0000787#ifndef HAVE_IF_NAMETOINDEX
788unsigned int
789if_nametoindex (const char *name)
790{
paul718e3742002-12-13 20:15:29 +0000791 struct interface *ifp;
792
ajs018546e2005-04-02 23:05:56 +0000793 return ((ifp = if_lookup_by_name_len(name, strnlen(name, IFNAMSIZ))) != NULL)
794 ? ifp->ifindex : 0;
paul718e3742002-12-13 20:15:29 +0000795}
796#endif
797
798#ifndef HAVE_IF_INDEXTONAME
799char *
800if_indextoname (unsigned int ifindex, char *name)
801{
paul718e3742002-12-13 20:15:29 +0000802 struct interface *ifp;
803
ajsd2fc8892005-04-02 18:38:43 +0000804 if (!(ifp = if_lookup_by_index(ifindex)))
805 return NULL;
806 strncpy (name, ifp->name, IFNAMSIZ);
807 return ifp->name;
paul718e3742002-12-13 20:15:29 +0000808}
809#endif
David Lamparter6b0655a2014-06-04 06:53:35 +0200810
paul8cc41982005-05-06 21:25:49 +0000811#if 0 /* this route_table of struct connected's is unused
812 * however, it would be good to use a route_table rather than
813 * a list..
814 */
paul718e3742002-12-13 20:15:29 +0000815/* Interface looking up by interface's address. */
paul718e3742002-12-13 20:15:29 +0000816/* Interface's IPv4 address reverse lookup table. */
817struct route_table *ifaddr_ipv4_table;
818/* struct route_table *ifaddr_ipv6_table; */
819
paul8cc41982005-05-06 21:25:49 +0000820static void
paul718e3742002-12-13 20:15:29 +0000821ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
822{
823 struct route_node *rn;
824 struct prefix_ipv4 p;
825
826 p.family = AF_INET;
827 p.prefixlen = IPV4_MAX_PREFIXLEN;
828 p.prefix = *ifaddr;
829
830 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
831 if (rn)
832 {
833 route_unlock_node (rn);
834 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
835 inet_ntoa (*ifaddr));
836 return;
837 }
838 rn->info = ifp;
839}
840
paul8cc41982005-05-06 21:25:49 +0000841static void
paul718e3742002-12-13 20:15:29 +0000842ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
843{
844 struct route_node *rn;
845 struct prefix_ipv4 p;
846
847 p.family = AF_INET;
848 p.prefixlen = IPV4_MAX_PREFIXLEN;
849 p.prefix = *ifaddr;
850
851 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
852 if (! rn)
853 {
854 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
855 inet_ntoa (*ifaddr));
856 return;
857 }
858 rn->info = NULL;
859 route_unlock_node (rn);
860 route_unlock_node (rn);
861}
862
863/* Lookup interface by interface's IP address or interface index. */
paul8cc41982005-05-06 21:25:49 +0000864static struct interface *
paul718e3742002-12-13 20:15:29 +0000865ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
866{
867 struct prefix_ipv4 p;
868 struct route_node *rn;
869 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000870
871 if (addr)
872 {
873 p.family = AF_INET;
874 p.prefixlen = IPV4_MAX_PREFIXLEN;
875 p.prefix = *addr;
876
877 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
878 if (! rn)
879 return NULL;
880
881 ifp = rn->info;
882 route_unlock_node (rn);
883 return ifp;
884 }
885 else
ajsd2fc8892005-04-02 18:38:43 +0000886 return if_lookup_by_index(ifindex);
paul718e3742002-12-13 20:15:29 +0000887}
paul8cc41982005-05-06 21:25:49 +0000888#endif /* ifaddr_ipv4_table */
paul718e3742002-12-13 20:15:29 +0000889
890/* Initialize interface list. */
891void
paul8cc41982005-05-06 21:25:49 +0000892if_init (void)
paul718e3742002-12-13 20:15:29 +0000893{
894 iflist = list_new ();
paul8cc41982005-05-06 21:25:49 +0000895#if 0
paul718e3742002-12-13 20:15:29 +0000896 ifaddr_ipv4_table = route_table_init ();
paul8cc41982005-05-06 21:25:49 +0000897#endif /* ifaddr_ipv4_table */
paul718e3742002-12-13 20:15:29 +0000898
paul106d2fd2003-08-01 00:24:13 +0000899 if (iflist) {
900 iflist->cmp = (int (*)(void *, void *))if_cmp_func;
paul718e3742002-12-13 20:15:29 +0000901 return;
paul106d2fd2003-08-01 00:24:13 +0000902 }
paul718e3742002-12-13 20:15:29 +0000903
904 memset (&if_master, 0, sizeof if_master);
905}
Tom Goff4bd045d2010-11-10 13:00:54 -0800906
907void
908if_terminate (void)
909{
910 for (;;)
911 {
912 struct interface *ifp;
913
914 ifp = listnode_head (iflist);
915 if (ifp == NULL)
916 break;
917
918 if_delete (ifp);
919 }
920
921 list_delete (iflist);
922 iflist = NULL;
923}