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