]> git.proxmox.com Git - mirror_frr.git/blob - lib/if.c
Merge branch 'stable/3.0' into tmp-3.0-master-merge
[mirror_frr.git] / lib / if.c
1 /*
2 * Interface functions.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "linklist.h"
25 #include "vector.h"
26 #include "vty.h"
27 #include "command.h"
28 #include "vrf.h"
29 #include "if.h"
30 #include "sockunion.h"
31 #include "prefix.h"
32 #include "memory.h"
33 #include "table.h"
34 #include "buffer.h"
35 #include "log.h"
36
37 DEFINE_MTYPE(LIB, IF, "Interface")
38 DEFINE_MTYPE_STATIC(LIB, CONNECTED, "Connected")
39 DEFINE_MTYPE_STATIC(LIB, NBR_CONNECTED, "Neighbor Connected")
40 DEFINE_MTYPE(LIB, CONNECTED_LABEL, "Connected interface label")
41 DEFINE_MTYPE_STATIC(LIB, IF_LINK_PARAMS, "Informational Link Parameters")
42
43 DEFINE_QOBJ_TYPE(interface)
44
45 /* List of interfaces in only the default VRF */
46 int ptm_enable = 0;
47
48 /* One for each program. This structure is needed to store hooks. */
49 struct if_master {
50 int (*if_new_hook)(struct interface *);
51 int (*if_delete_hook)(struct interface *);
52 } if_master = {
53 0,
54 };
55
56 /* Compare interface names, returning an integer greater than, equal to, or
57 * less than 0, (following the strcmp convention), according to the
58 * relationship between ifp1 and ifp2. Interface names consist of an
59 * alphabetic prefix and a numeric suffix. The primary sort key is
60 * lexicographic by name, and then numeric by number. No number sorts
61 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
62 * devpty0, de0 < del0
63 */
64 int if_cmp_name_func(char *p1, char *p2)
65 {
66 unsigned int l1, l2;
67 long int x1, x2;
68 int res;
69
70 while (*p1 && *p2) {
71 /* look up to any number */
72 l1 = strcspn(p1, "0123456789");
73 l2 = strcspn(p2, "0123456789");
74
75 /* name lengths are different -> compare names */
76 if (l1 != l2)
77 return (strcmp(p1, p2));
78
79 /* Note that this relies on all numbers being less than all
80 * letters, so
81 * that de0 < del0.
82 */
83 res = strncmp(p1, p2, l1);
84
85 /* names are different -> compare them */
86 if (res)
87 return res;
88
89 /* with identical name part, go to numeric part */
90 p1 += l1;
91 p2 += l1;
92
93 if (!*p1)
94 return -1;
95 if (!*p2)
96 return 1;
97
98 x1 = strtol(p1, &p1, 10);
99 x2 = strtol(p2, &p2, 10);
100
101 /* let's compare numbers now */
102 if (x1 < x2)
103 return -1;
104 if (x1 > x2)
105 return 1;
106
107 /* numbers were equal, lets do it again..
108 (it happens with name like "eth123.456:789") */
109 }
110 if (*p1)
111 return 1;
112 if (*p2)
113 return -1;
114 return 0;
115 }
116
117 static int if_cmp_func(struct interface *ifp1, struct interface *ifp2)
118 {
119 return if_cmp_name_func(ifp1->name, ifp2->name);
120 }
121
122 /* Create new interface structure. */
123 struct interface *if_create(const char *name, int namelen, vrf_id_t vrf_id)
124 {
125 struct interface *ifp;
126 struct list *intf_list = vrf_iflist_get(vrf_id);
127
128 ifp = XCALLOC(MTYPE_IF, sizeof(struct interface));
129 ifp->ifindex = IFINDEX_INTERNAL;
130
131 assert(name);
132 assert(namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */
133 strncpy(ifp->name, name, namelen);
134 ifp->name[namelen] = '\0';
135 ifp->vrf_id = vrf_id;
136 if (if_lookup_by_name(ifp->name, vrf_id) == NULL)
137 listnode_add_sort(intf_list, ifp);
138 else
139 zlog_err(
140 "if_create(%s): corruption detected -- interface with this "
141 "name exists already in VRF %u!",
142 ifp->name, vrf_id);
143 ifp->connected = list_new();
144 ifp->connected->del = (void (*)(void *))connected_free;
145
146 ifp->nbr_connected = list_new();
147 ifp->nbr_connected->del = (void (*)(void *))nbr_connected_free;
148
149 /* Enable Link-detection by default */
150 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
151
152 QOBJ_REG(ifp, interface);
153
154 if (if_master.if_new_hook)
155 (*if_master.if_new_hook)(ifp);
156
157 return ifp;
158 }
159
160 /* Create new interface structure. */
161 void if_update_to_new_vrf(struct interface *ifp, vrf_id_t vrf_id)
162 {
163 struct list *intf_list = vrf_iflist_get(vrf_id);
164
165 /* remove interface from old master vrf list */
166 if (vrf_iflist(ifp->vrf_id))
167 listnode_delete(vrf_iflist(ifp->vrf_id), ifp);
168
169 ifp->vrf_id = vrf_id;
170 if (if_lookup_by_name(ifp->name, vrf_id) == NULL)
171 listnode_add_sort(intf_list, ifp);
172 else
173 zlog_err(
174 "if_create(%s): corruption detected -- interface with this "
175 "name exists already in VRF %u!",
176 ifp->name, vrf_id);
177
178 return;
179 }
180
181
182 /* Delete interface structure. */
183 void if_delete_retain(struct interface *ifp)
184 {
185 if (if_master.if_delete_hook)
186 (*if_master.if_delete_hook)(ifp);
187
188 QOBJ_UNREG(ifp);
189
190 /* Free connected address list */
191 list_delete_all_node(ifp->connected);
192
193 /* Free connected nbr address list */
194 list_delete_all_node(ifp->nbr_connected);
195 }
196
197 /* Delete and free interface structure. */
198 void if_delete(struct interface *ifp)
199 {
200 listnode_delete(vrf_iflist(ifp->vrf_id), ifp);
201
202 if_delete_retain(ifp);
203
204 list_free(ifp->connected);
205 list_free(ifp->nbr_connected);
206
207 if_link_params_free(ifp);
208
209 XFREE(MTYPE_IF, ifp);
210 }
211
212 /* Add hook to interface master. */
213 void if_add_hook(int type, int (*func)(struct interface *ifp))
214 {
215 switch (type) {
216 case IF_NEW_HOOK:
217 if_master.if_new_hook = func;
218 break;
219 case IF_DELETE_HOOK:
220 if_master.if_delete_hook = func;
221 break;
222 default:
223 break;
224 }
225 }
226
227 /* Interface existance check by index. */
228 struct interface *if_lookup_by_index(ifindex_t ifindex, vrf_id_t vrf_id)
229 {
230 struct listnode *node;
231 struct interface *ifp;
232
233 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), node, ifp)) {
234 if (ifp->ifindex == ifindex)
235 return ifp;
236 }
237 return NULL;
238 }
239
240 const char *ifindex2ifname(ifindex_t ifindex, vrf_id_t vrf_id)
241 {
242 struct interface *ifp;
243
244 return ((ifp = if_lookup_by_index(ifindex, vrf_id)) != NULL)
245 ? ifp->name
246 : "unknown";
247 }
248
249 ifindex_t ifname2ifindex(const char *name, vrf_id_t vrf_id)
250 {
251 struct interface *ifp;
252
253 return ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
254 ? ifp->ifindex
255 : IFINDEX_INTERNAL;
256 }
257
258 /* Interface existance check by interface name. */
259 struct interface *if_lookup_by_name(const char *name, vrf_id_t vrf_id)
260 {
261 struct listnode *node;
262 struct interface *ifp;
263
264 if (name)
265 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), node, ifp)) {
266 if (strcmp(name, ifp->name) == 0)
267 return ifp;
268 }
269 return NULL;
270 }
271
272 struct interface *if_lookup_by_name_all_vrf(const char *name)
273 {
274 struct vrf *vrf;
275 struct interface *ifp;
276
277 RB_FOREACH(vrf, vrf_id_head, &vrfs_by_id)
278 {
279 ifp = if_lookup_by_name(name, vrf->vrf_id);
280 if (ifp)
281 return ifp;
282 }
283
284 return NULL;
285 }
286
287 struct interface *if_lookup_by_name_len(const char *name, size_t namelen,
288 vrf_id_t vrf_id)
289 {
290 struct listnode *node;
291 struct interface *ifp;
292
293 if (namelen > INTERFACE_NAMSIZ)
294 return NULL;
295
296 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), node, ifp)) {
297 if (!memcmp(name, ifp->name, namelen)
298 && (ifp->name[namelen] == '\0'))
299 return ifp;
300 }
301 return NULL;
302 }
303
304 /* Lookup interface by IPv4 address. */
305 struct interface *if_lookup_exact_address(void *src, int family,
306 vrf_id_t vrf_id)
307 {
308 struct listnode *node;
309 struct listnode *cnode;
310 struct interface *ifp;
311 struct prefix *p;
312 struct connected *c;
313
314 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), node, ifp)) {
315 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
316 p = c->address;
317
318 if (p && (p->family == family)) {
319 if (family == AF_INET) {
320 if (IPV4_ADDR_SAME(
321 &p->u.prefix4,
322 (struct in_addr *)src))
323 return ifp;
324 } else if (family == AF_INET6) {
325 if (IPV6_ADDR_SAME(
326 &p->u.prefix6,
327 (struct in6_addr *)src))
328 return ifp;
329 }
330 }
331 }
332 }
333 return NULL;
334 }
335
336 /* Lookup interface by IPv4 address. */
337 struct connected *if_lookup_address(void *matchaddr, int family,
338 vrf_id_t vrf_id)
339 {
340 struct listnode *node;
341 struct prefix addr;
342 int bestlen = 0;
343 struct listnode *cnode;
344 struct interface *ifp;
345 struct connected *c;
346 struct connected *match;
347
348 if (family == AF_INET) {
349 addr.family = AF_INET;
350 addr.u.prefix4 = *((struct in_addr *)matchaddr);
351 addr.prefixlen = IPV4_MAX_BITLEN;
352 } else if (family == AF_INET6) {
353 addr.family = AF_INET6;
354 addr.u.prefix6 = *((struct in6_addr *)matchaddr);
355 addr.prefixlen = IPV6_MAX_BITLEN;
356 }
357
358 match = NULL;
359
360 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), node, ifp)) {
361 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
362 if (c->address && (c->address->family == AF_INET)
363 && prefix_match(CONNECTED_PREFIX(c), &addr)
364 && (c->address->prefixlen > bestlen)) {
365 bestlen = c->address->prefixlen;
366 match = c;
367 }
368 }
369 }
370 return match;
371 }
372
373 /* Lookup interface by prefix */
374 struct interface *if_lookup_prefix(struct prefix *prefix, vrf_id_t vrf_id)
375 {
376 struct listnode *node;
377 struct listnode *cnode;
378 struct interface *ifp;
379 struct connected *c;
380
381 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), node, ifp)) {
382 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
383 if (prefix_cmp(c->address, prefix) == 0) {
384 return ifp;
385 }
386 }
387 }
388 return NULL;
389 }
390
391 /* Get interface by name if given name interface doesn't exist create
392 one. */
393 struct interface *if_get_by_name(const char *name, vrf_id_t vrf_id)
394 {
395 struct interface *ifp;
396
397 return ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
398 ? ifp
399 : if_create(name, strlen(name), vrf_id);
400 }
401
402 struct interface *if_get_by_name_len(const char *name, size_t namelen,
403 vrf_id_t vrf_id, int vty)
404 {
405 struct interface *ifp;
406 struct vrf *vrf;
407 struct listnode *node;
408
409 ifp = if_lookup_by_name_len(name, namelen, vrf_id);
410 if (ifp)
411 return ifp;
412
413 /* Didn't find the interface on that vrf. Defined on a different one? */
414 RB_FOREACH(vrf, vrf_id_head, &vrfs_by_id)
415 {
416 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf->vrf_id), node, ifp)) {
417 if (!memcmp(name, ifp->name, namelen)
418 && (ifp->name[namelen] == '\0')) {
419 /* Found a match. If the interface command was
420 * entered in vty without a
421 * VRF (passed as VRF_DEFAULT), accept the ifp
422 * we found. If a vrf was
423 * entered and there is a mismatch, reject it if
424 * from vty. If it came
425 * from the kernel by way of zclient, believe
426 * it and update
427 * the ifp accordingly.
428 */
429 if (vty) {
430 if (vrf_id == VRF_DEFAULT)
431 return ifp;
432 return NULL;
433 } else {
434 if_update_to_new_vrf(ifp, vrf_id);
435 return ifp;
436 }
437 }
438 }
439 }
440 return (if_create(name, namelen, vrf_id));
441 }
442
443 /* Does interface up ? */
444 int if_is_up(struct interface *ifp)
445 {
446 return ifp->flags & IFF_UP;
447 }
448
449 /* Is interface running? */
450 int if_is_running(struct interface *ifp)
451 {
452 return ifp->flags & IFF_RUNNING;
453 }
454
455 /* Is the interface operative, eg. either UP & RUNNING
456 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
457 if ptm checking is enabled, then ptm check has passed */
458 int if_is_operative(struct interface *ifp)
459 {
460 return ((ifp->flags & IFF_UP)
461 && (((ifp->flags & IFF_RUNNING)
462 && (ifp->ptm_status || !ifp->ptm_enable))
463 || !CHECK_FLAG(ifp->status,
464 ZEBRA_INTERFACE_LINKDETECTION)));
465 }
466
467 /* Is the interface operative, eg. either UP & RUNNING
468 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
469 int if_is_no_ptm_operative(struct interface *ifp)
470 {
471 return ((ifp->flags & IFF_UP)
472 && ((ifp->flags & IFF_RUNNING)
473 || !CHECK_FLAG(ifp->status,
474 ZEBRA_INTERFACE_LINKDETECTION)));
475 }
476
477 /* Is this loopback interface ? */
478 int if_is_loopback(struct interface *ifp)
479 {
480 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
481 * but Y on platform N?
482 */
483 return (ifp->flags & (IFF_LOOPBACK | IFF_NOXMIT | IFF_VIRTUAL));
484 }
485
486 /* Does this interface support broadcast ? */
487 int if_is_broadcast(struct interface *ifp)
488 {
489 return ifp->flags & IFF_BROADCAST;
490 }
491
492 /* Does this interface support broadcast ? */
493 int if_is_pointopoint(struct interface *ifp)
494 {
495 return ifp->flags & IFF_POINTOPOINT;
496 }
497
498 /* Does this interface support multicast ? */
499 int if_is_multicast(struct interface *ifp)
500 {
501 return ifp->flags & IFF_MULTICAST;
502 }
503
504 /* Printout flag information into log */
505 const char *if_flag_dump(unsigned long flag)
506 {
507 int separator = 0;
508 static char logbuf[BUFSIZ];
509
510 #define IFF_OUT_LOG(X, STR) \
511 if (flag & (X)) { \
512 if (separator) \
513 strlcat(logbuf, ",", BUFSIZ); \
514 else \
515 separator = 1; \
516 strlcat(logbuf, STR, BUFSIZ); \
517 }
518
519 strlcpy(logbuf, "<", BUFSIZ);
520 IFF_OUT_LOG(IFF_UP, "UP");
521 IFF_OUT_LOG(IFF_BROADCAST, "BROADCAST");
522 IFF_OUT_LOG(IFF_DEBUG, "DEBUG");
523 IFF_OUT_LOG(IFF_LOOPBACK, "LOOPBACK");
524 IFF_OUT_LOG(IFF_POINTOPOINT, "POINTOPOINT");
525 IFF_OUT_LOG(IFF_NOTRAILERS, "NOTRAILERS");
526 IFF_OUT_LOG(IFF_RUNNING, "RUNNING");
527 IFF_OUT_LOG(IFF_NOARP, "NOARP");
528 IFF_OUT_LOG(IFF_PROMISC, "PROMISC");
529 IFF_OUT_LOG(IFF_ALLMULTI, "ALLMULTI");
530 IFF_OUT_LOG(IFF_OACTIVE, "OACTIVE");
531 IFF_OUT_LOG(IFF_SIMPLEX, "SIMPLEX");
532 IFF_OUT_LOG(IFF_LINK0, "LINK0");
533 IFF_OUT_LOG(IFF_LINK1, "LINK1");
534 IFF_OUT_LOG(IFF_LINK2, "LINK2");
535 IFF_OUT_LOG(IFF_MULTICAST, "MULTICAST");
536 IFF_OUT_LOG(IFF_NOXMIT, "NOXMIT");
537 IFF_OUT_LOG(IFF_NORTEXCH, "NORTEXCH");
538 IFF_OUT_LOG(IFF_VIRTUAL, "VIRTUAL");
539 IFF_OUT_LOG(IFF_IPV4, "IPv4");
540 IFF_OUT_LOG(IFF_IPV6, "IPv6");
541
542 strlcat(logbuf, ">", BUFSIZ);
543
544 return logbuf;
545 #undef IFF_OUT_LOG
546 }
547
548 /* For debugging */
549 static void if_dump(const struct interface *ifp)
550 {
551 struct listnode *node;
552 struct connected *c __attribute__((unused));
553
554 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, c))
555 zlog_info(
556 "Interface %s vrf %u index %d metric %d mtu %d "
557 "mtu6 %d %s",
558 ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric,
559 ifp->mtu, ifp->mtu6, if_flag_dump(ifp->flags));
560 }
561
562 /* Interface printing for all interface. */
563 void if_dump_all(void)
564 {
565 struct vrf *vrf;
566 struct listnode *node;
567 void *p;
568
569 RB_FOREACH(vrf, vrf_id_head, &vrfs_by_id)
570 if (vrf->iflist != NULL)
571 for (ALL_LIST_ELEMENTS_RO(vrf->iflist, node, p))
572 if_dump(p);
573 }
574
575 DEFUN (interface_desc,
576 interface_desc_cmd,
577 "description LINE...",
578 "Interface specific description\n"
579 "Characters describing this interface\n")
580 {
581 int idx_line = 1;
582 VTY_DECLVAR_CONTEXT(interface, ifp);
583
584 if (ifp->desc)
585 XFREE(MTYPE_TMP, ifp->desc);
586 ifp->desc = argv_concat(argv, argc, idx_line);
587
588 return CMD_SUCCESS;
589 }
590
591 DEFUN (no_interface_desc,
592 no_interface_desc_cmd,
593 "no description",
594 NO_STR
595 "Interface specific description\n")
596 {
597 VTY_DECLVAR_CONTEXT(interface, ifp);
598
599 if (ifp->desc)
600 XFREE(MTYPE_TMP, ifp->desc);
601 ifp->desc = NULL;
602
603 return CMD_SUCCESS;
604 }
605
606 #ifdef SUNOS_5
607 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
608 * a seperate struct interface for each logical interface, so config
609 * file may be full of 'interface fooX:Y'. Solaris however does not
610 * expose logical interfaces via PF_ROUTE, so trying to track logical
611 * interfaces can be fruitless, for that reason Quagga only tracks
612 * the primary IP interface.
613 *
614 * We try accomodate SUNWzebra by:
615 * - looking up the interface name, to see whether it exists, if so
616 * its useable
617 * - for protocol daemons, this could only because zebra told us of
618 * the interface
619 * - for zebra, only because it learnt from kernel
620 * - if not:
621 * - search the name to see if it contains a sub-ipif / logical interface
622 * seperator, the ':' char. If it does:
623 * - text up to that char must be the primary name - get that name.
624 * if not:
625 * - no idea, just get the name in its entirety.
626 */
627 static struct interface *if_sunwzebra_get(const char *name, size_t nlen,
628 vrf_id_t vrf_id)
629 {
630 struct interface *ifp;
631 size_t seppos = 0;
632
633 if ((ifp = if_lookup_by_name_len(name, nlen, vrf_id)) != NULL)
634 return ifp;
635
636 /* hunt the primary interface name... */
637 while (seppos < nlen && name[seppos] != ':')
638 seppos++;
639
640 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
641 if (seppos < nlen)
642 return if_get_by_name_len(name, seppos, vrf_id, 1);
643 else
644 return if_get_by_name_len(name, nlen, vrf_id, 1);
645 }
646 #endif /* SUNOS_5 */
647
648 DEFUN (interface,
649 interface_cmd,
650 "interface IFNAME [vrf NAME]",
651 "Select an interface to configure\n"
652 "Interface's name\n"
653 VRF_CMD_HELP_STR)
654 {
655 int idx_ifname = 1;
656 int idx_vrf = 3;
657 const char *ifname = argv[idx_ifname]->arg;
658 const char *vrfname = (argc > 2) ? argv[idx_vrf]->arg : NULL;
659
660 struct interface *ifp;
661 size_t sl;
662 vrf_id_t vrf_id = VRF_DEFAULT;
663
664 if ((sl = strlen(ifname)) > INTERFACE_NAMSIZ) {
665 vty_out(vty,
666 "%% Interface name %s is invalid: length exceeds "
667 "%d characters\n",
668 ifname, INTERFACE_NAMSIZ);
669 return CMD_WARNING_CONFIG_FAILED;
670 }
671
672 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
673 Imagine forward reference of a vrf by name in this interface config */
674 if (vrfname)
675 VRF_GET_ID(vrf_id, vrfname);
676
677 #ifdef SUNOS_5
678 ifp = if_sunwzebra_get(ifname, sl, vrf_id);
679 #else
680 ifp = if_get_by_name_len(ifname, sl, vrf_id, 1);
681 #endif /* SUNOS_5 */
682
683 if (!ifp) {
684 vty_out(vty, "%% interface %s not in %s\n", ifname, vrfname);
685 return CMD_WARNING_CONFIG_FAILED;
686 }
687 VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp);
688
689 return CMD_SUCCESS;
690 }
691
692 DEFUN_NOSH (no_interface,
693 no_interface_cmd,
694 "no interface IFNAME [vrf NAME]",
695 NO_STR
696 "Delete a pseudo interface's configuration\n"
697 "Interface's name\n"
698 VRF_CMD_HELP_STR)
699 {
700 const char *ifname = argv[2]->arg;
701 const char *vrfname = (argc > 3) ? argv[3]->arg : NULL;
702
703 // deleting interface
704 struct interface *ifp;
705 vrf_id_t vrf_id = VRF_DEFAULT;
706
707 if (argc > 3)
708 VRF_GET_ID(vrf_id, vrfname);
709
710 ifp = if_lookup_by_name(ifname, vrf_id);
711
712 if (ifp == NULL) {
713 vty_out(vty, "%% Interface %s does not exist\n", ifname);
714 return CMD_WARNING_CONFIG_FAILED;
715 }
716
717 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
718 vty_out(vty, "%% Only inactive interfaces can be deleted\n");
719 return CMD_WARNING_CONFIG_FAILED;
720 }
721
722 if_delete(ifp);
723
724 return CMD_SUCCESS;
725 }
726
727 void if_cmd_init(void)
728 {
729 install_element(CONFIG_NODE, &interface_cmd);
730 install_element(CONFIG_NODE, &no_interface_cmd);
731
732 install_default(INTERFACE_NODE);
733 install_element(INTERFACE_NODE, &interface_desc_cmd);
734 install_element(INTERFACE_NODE, &no_interface_desc_cmd);
735 }
736
737 #if 0
738 /* For debug purpose. */
739 DEFUN (show_address,
740 show_address_cmd,
741 "show address [vrf NAME]",
742 SHOW_STR
743 "address\n"
744 VRF_CMD_HELP_STR)
745 {
746 int idx_vrf = 3;
747 struct listnode *node;
748 struct listnode *node2;
749 struct interface *ifp;
750 struct connected *ifc;
751 struct prefix *p;
752 vrf_id_t vrf_id = VRF_DEFAULT;
753
754 if (argc > 2)
755 VRF_GET_ID (vrf_id, argv[idx_vrf]->arg);
756
757 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
758 {
759 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
760 {
761 p = ifc->address;
762
763 if (p->family == AF_INET)
764 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
765 }
766 }
767 return CMD_SUCCESS;
768 }
769
770 DEFUN (show_address_vrf_all,
771 show_address_vrf_all_cmd,
772 "show address vrf all",
773 SHOW_STR
774 "address\n"
775 VRF_ALL_CMD_HELP_STR)
776 {
777 struct vrf *vrf;
778 struct listnode *node;
779 struct listnode *node2;
780 struct interface *ifp;
781 struct connected *ifc;
782 struct prefix *p;
783
784 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
785 {
786 if (!vrf->iflist || !listcount (vrf->iflist))
787 continue;
788
789 vty_out (vty, "\nVRF %u\n\n", vrf->vrf_id);
790
791 for (ALL_LIST_ELEMENTS_RO (vrf->iflist, node, ifp))
792 {
793 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
794 {
795 p = ifc->address;
796
797 if (p->family == AF_INET)
798 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
799 }
800 }
801 }
802 return CMD_SUCCESS;
803 }
804 #endif
805
806 /* Allocate connected structure. */
807 struct connected *connected_new(void)
808 {
809 return XCALLOC(MTYPE_CONNECTED, sizeof(struct connected));
810 }
811
812 /* Allocate nbr connected structure. */
813 struct nbr_connected *nbr_connected_new(void)
814 {
815 return XCALLOC(MTYPE_NBR_CONNECTED, sizeof(struct nbr_connected));
816 }
817
818 /* Free connected structure. */
819 void connected_free(struct connected *connected)
820 {
821 if (connected->address)
822 prefix_free(connected->address);
823
824 if (connected->destination)
825 prefix_free(connected->destination);
826
827 if (connected->label)
828 XFREE(MTYPE_CONNECTED_LABEL, connected->label);
829
830 XFREE(MTYPE_CONNECTED, connected);
831 }
832
833 /* Free nbr connected structure. */
834 void nbr_connected_free(struct nbr_connected *connected)
835 {
836 if (connected->address)
837 prefix_free(connected->address);
838
839 XFREE(MTYPE_NBR_CONNECTED, connected);
840 }
841
842 /* If same interface nbr address already exists... */
843 struct nbr_connected *nbr_connected_check(struct interface *ifp,
844 struct prefix *p)
845 {
846 struct nbr_connected *ifc;
847 struct listnode *node;
848
849 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, ifc))
850 if (prefix_same(ifc->address, p))
851 return ifc;
852
853 return NULL;
854 }
855
856 /* Print if_addr structure. */
857 static void __attribute__((unused))
858 connected_log(struct connected *connected, char *str)
859 {
860 struct prefix *p;
861 struct interface *ifp;
862 char logbuf[BUFSIZ];
863 char buf[BUFSIZ];
864
865 ifp = connected->ifp;
866 p = connected->address;
867
868 snprintf(logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ", str,
869 ifp->name, ifp->vrf_id, prefix_family_str(p),
870 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
871
872 p = connected->destination;
873 if (p) {
874 strncat(logbuf, inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
875 BUFSIZ - strlen(logbuf));
876 }
877 zlog_info("%s", logbuf);
878 }
879
880 /* Print if_addr structure. */
881 static void __attribute__((unused))
882 nbr_connected_log(struct nbr_connected *connected, char *str)
883 {
884 struct prefix *p;
885 struct interface *ifp;
886 char logbuf[BUFSIZ];
887 char buf[BUFSIZ];
888
889 ifp = connected->ifp;
890 p = connected->address;
891
892 snprintf(logbuf, BUFSIZ, "%s interface %s %s %s/%d ", str, ifp->name,
893 prefix_family_str(p),
894 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
895
896 zlog_info("%s", logbuf);
897 }
898
899 /* If two connected address has same prefix return 1. */
900 static int connected_same_prefix(struct prefix *p1, struct prefix *p2)
901 {
902 if (p1->family == p2->family) {
903 if (p1->family == AF_INET
904 && IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
905 return 1;
906 if (p1->family == AF_INET6
907 && IPV6_ADDR_SAME(&p1->u.prefix6, &p2->u.prefix6))
908 return 1;
909 }
910 return 0;
911 }
912
913 struct connected *connected_lookup_prefix_exact(struct interface *ifp,
914 struct prefix *p)
915 {
916 struct listnode *node;
917 struct listnode *next;
918 struct connected *ifc;
919
920 for (node = listhead(ifp->connected); node; node = next) {
921 ifc = listgetdata(node);
922 next = node->next;
923
924 if (connected_same_prefix(ifc->address, p))
925 return ifc;
926 }
927 return NULL;
928 }
929
930 struct connected *connected_delete_by_prefix(struct interface *ifp,
931 struct prefix *p)
932 {
933 struct listnode *node;
934 struct listnode *next;
935 struct connected *ifc;
936
937 /* In case of same prefix come, replace it with new one. */
938 for (node = listhead(ifp->connected); node; node = next) {
939 ifc = listgetdata(node);
940 next = node->next;
941
942 if (connected_same_prefix(ifc->address, p)) {
943 listnode_delete(ifp->connected, ifc);
944 return ifc;
945 }
946 }
947 return NULL;
948 }
949
950 /* Find the address on our side that will be used when packets
951 are sent to dst. */
952 struct connected *connected_lookup_prefix(struct interface *ifp,
953 struct prefix *addr)
954 {
955 struct listnode *cnode;
956 struct connected *c;
957 struct connected *match;
958
959 match = NULL;
960
961 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
962 if (c->address && (c->address->family == addr->family)
963 && prefix_match(CONNECTED_PREFIX(c), addr)
964 && (!match
965 || (c->address->prefixlen > match->address->prefixlen)))
966 match = c;
967 }
968 return match;
969 }
970
971 struct connected *connected_add_by_prefix(struct interface *ifp,
972 struct prefix *p,
973 struct prefix *destination)
974 {
975 struct connected *ifc;
976
977 /* Allocate new connected address. */
978 ifc = connected_new();
979 ifc->ifp = ifp;
980
981 /* Fetch interface address */
982 ifc->address = prefix_new();
983 memcpy(ifc->address, p, sizeof(struct prefix));
984
985 /* Fetch dest address */
986 if (destination) {
987 ifc->destination = prefix_new();
988 memcpy(ifc->destination, destination, sizeof(struct prefix));
989 }
990
991 /* Add connected address to the interface. */
992 listnode_add(ifp->connected, ifc);
993 return ifc;
994 }
995
996 #if 0 /* this route_table of struct connected's is unused \
997 * however, it would be good to use a route_table rather than \
998 * a list.. \
999 */
1000 /* Interface looking up by interface's address. */
1001 /* Interface's IPv4 address reverse lookup table. */
1002 struct route_table *ifaddr_ipv4_table;
1003 /* struct route_table *ifaddr_ipv6_table; */
1004
1005 static void
1006 ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
1007 {
1008 struct route_node *rn;
1009 struct prefix_ipv4 p;
1010
1011 p.family = AF_INET;
1012 p.prefixlen = IPV4_MAX_PREFIXLEN;
1013 p.prefix = *ifaddr;
1014
1015 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
1016 if (rn)
1017 {
1018 route_unlock_node (rn);
1019 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1020 inet_ntoa (*ifaddr));
1021 return;
1022 }
1023 rn->info = ifp;
1024 }
1025
1026 static void
1027 ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
1028 {
1029 struct route_node *rn;
1030 struct prefix_ipv4 p;
1031
1032 p.family = AF_INET;
1033 p.prefixlen = IPV4_MAX_PREFIXLEN;
1034 p.prefix = *ifaddr;
1035
1036 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1037 if (! rn)
1038 {
1039 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1040 inet_ntoa (*ifaddr));
1041 return;
1042 }
1043 rn->info = NULL;
1044 route_unlock_node (rn);
1045 route_unlock_node (rn);
1046 }
1047
1048 /* Lookup interface by interface's IP address or interface index. */
1049 static struct interface *
1050 ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
1051 {
1052 struct prefix_ipv4 p;
1053 struct route_node *rn;
1054 struct interface *ifp;
1055
1056 if (addr)
1057 {
1058 p.family = AF_INET;
1059 p.prefixlen = IPV4_MAX_PREFIXLEN;
1060 p.prefix = *addr;
1061
1062 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1063 if (! rn)
1064 return NULL;
1065
1066 ifp = rn->info;
1067 route_unlock_node (rn);
1068 return ifp;
1069 }
1070 else
1071 return if_lookup_by_index(ifindex, VRF_DEFAULT);
1072 }
1073 #endif /* ifaddr_ipv4_table */
1074
1075 static void if_autocomplete(vector comps, struct cmd_token *token)
1076 {
1077 struct interface *ifp;
1078 struct listnode *ln;
1079 struct vrf *vrf = NULL;
1080
1081 RB_FOREACH(vrf, vrf_name_head, &vrfs_by_name)
1082 {
1083 for (ALL_LIST_ELEMENTS_RO(vrf->iflist, ln, ifp))
1084 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name));
1085 }
1086 }
1087
1088 static const struct cmd_variable_handler if_var_handlers[] = {
1089 {/* "interface NAME" */
1090 .varname = "interface",
1091 .completions = if_autocomplete},
1092 {.tokenname = "IFNAME", .completions = if_autocomplete},
1093 {.tokenname = "INTERFACE", .completions = if_autocomplete},
1094 {.completions = NULL}};
1095
1096 /* Initialize interface list. */
1097 void if_init(struct list **intf_list)
1098 {
1099 *intf_list = list_new();
1100 #if 0
1101 ifaddr_ipv4_table = route_table_init ();
1102 #endif /* ifaddr_ipv4_table */
1103
1104 (*intf_list)->cmp = (int (*)(void *, void *))if_cmp_func;
1105
1106 cmd_variable_handler_register(if_var_handlers);
1107 }
1108
1109 void if_terminate(struct list **intf_list)
1110 {
1111 for (;;) {
1112 struct interface *ifp;
1113
1114 ifp = listnode_head(*intf_list);
1115 if (ifp == NULL)
1116 break;
1117
1118 if (ifp->node) {
1119 ifp->node->info = NULL;
1120 route_unlock_node(ifp->node);
1121 }
1122
1123 if_delete(ifp);
1124 }
1125
1126 list_delete(*intf_list);
1127 *intf_list = NULL;
1128 }
1129
1130 const char *if_link_type_str(enum zebra_link_type llt)
1131 {
1132 switch (llt) {
1133 #define llts(T,S) case (T): return (S)
1134 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
1135 llts(ZEBRA_LLT_ETHER, "Ethernet");
1136 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
1137 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
1138 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
1139 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
1140 llts(ZEBRA_LLT_ARCNET, "ARCnet");
1141 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
1142 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
1143 llts(ZEBRA_LLT_ATM, "ATM");
1144 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
1145 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
1146 llts(ZEBRA_LLT_EUI64, "EUI-64");
1147 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
1148 llts(ZEBRA_LLT_SLIP, "SLIP");
1149 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
1150 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
1151 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
1152 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
1153 llts(ZEBRA_LLT_X25, "CCITT X.25");
1154 llts(ZEBRA_LLT_PPP, "PPP");
1155 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
1156 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1157 llts(ZEBRA_LLT_LAPB, "LAPB");
1158 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1159 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1160 llts(ZEBRA_LLT_FRAD, "FRAD");
1161 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1162 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1163 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1164 llts(ZEBRA_LLT_FDDI, "FDDI");
1165 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1166 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1167 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1168 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1169 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1170 llts(ZEBRA_LLT_IRDA, "IrDA");
1171 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1172 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1173 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1174 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1175 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1176 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1177 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1178 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1179 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1180 default:
1181 zlog_warn("Unknown value %d", llt);
1182 return "Unknown type!";
1183 #undef llts
1184 }
1185 return NULL;
1186 }
1187
1188 struct if_link_params *if_link_params_get(struct interface *ifp)
1189 {
1190 int i;
1191
1192 if (ifp->link_params != NULL)
1193 return ifp->link_params;
1194
1195 struct if_link_params *iflp =
1196 XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params));
1197 if (iflp == NULL)
1198 return NULL;
1199
1200 /* Set TE metric equal to standard metric */
1201 iflp->te_metric = ifp->metric;
1202
1203 /* Compute default bandwidth based on interface */
1204 iflp->default_bw =
1205 ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1206 * TE_KILO_BIT / TE_BYTE);
1207
1208 /* Set Max, Reservable and Unreserved Bandwidth */
1209 iflp->max_bw = iflp->default_bw;
1210 iflp->max_rsv_bw = iflp->default_bw;
1211 for (i = 0; i < MAX_CLASS_TYPE; i++)
1212 iflp->unrsv_bw[i] = iflp->default_bw;
1213
1214 /* Update Link parameters status */
1215 iflp->lp_status =
1216 LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
1217
1218 /* Finally attach newly created Link Parameters */
1219 ifp->link_params = iflp;
1220
1221 return iflp;
1222 }
1223
1224 void if_link_params_free(struct interface *ifp)
1225 {
1226 if (ifp->link_params == NULL)
1227 return;
1228 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1229 ifp->link_params = NULL;
1230 }