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