]> git.proxmox.com Git - mirror_frr.git/blob - lib/if.c
Merge pull request #2848 from donaldsharp/more_init
[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;
375
376 ifp = if_lookup_by_name(name, vrf_id);
377 if (ifp)
378 return ifp;
379 /* Not Found on same VRF. If the interface command
380 * was entered in vty without a VRF (passed as VRF_DEFAULT),
381 * accept the ifp we found. If a vrf was entered and there is
382 * a mismatch, reject it if from vty.
383 */
384 ifp = if_lookup_by_name_all_vrf(name);
385 if (!ifp)
386 return if_create(name, vrf_id);
387 if (vty) {
388 if (vrf_id == VRF_DEFAULT)
389 return ifp;
390 return NULL;
391 }
392 /* if vrf backend uses NETNS, then
393 * this should not be considered as an update
394 * then create the new interface
395 */
396 if (ifp->vrf_id != vrf_id && vrf_is_mapped_on_netns(vrf_id))
397 return if_create(name, vrf_id);
398 /* If it came from the kernel
399 * or by way of zclient, believe it and update
400 * the ifp accordingly.
401 */
402 if_update_to_new_vrf(ifp, vrf_id);
403 return ifp;
404 }
405
406 void if_set_index(struct interface *ifp, ifindex_t ifindex)
407 {
408 struct vrf *vrf;
409
410 vrf = vrf_lookup_by_id(ifp->vrf_id);
411 assert(vrf);
412
413 if (ifp->ifindex == ifindex)
414 return;
415
416 if (ifp->ifindex != IFINDEX_INTERNAL)
417 IFINDEX_RB_REMOVE(vrf, ifp)
418
419 ifp->ifindex = ifindex;
420
421 if (ifp->ifindex != IFINDEX_INTERNAL)
422 IFINDEX_RB_INSERT(vrf, ifp)
423 }
424
425 /* Does interface up ? */
426 int if_is_up(struct interface *ifp)
427 {
428 return ifp->flags & IFF_UP;
429 }
430
431 /* Is interface running? */
432 int if_is_running(struct interface *ifp)
433 {
434 return ifp->flags & IFF_RUNNING;
435 }
436
437 /* Is the interface operative, eg. either UP & RUNNING
438 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
439 if ptm checking is enabled, then ptm check has passed */
440 int if_is_operative(struct interface *ifp)
441 {
442 return ((ifp->flags & IFF_UP)
443 && (((ifp->flags & IFF_RUNNING)
444 && (ifp->ptm_status || !ifp->ptm_enable))
445 || !CHECK_FLAG(ifp->status,
446 ZEBRA_INTERFACE_LINKDETECTION)));
447 }
448
449 /* Is the interface operative, eg. either UP & RUNNING
450 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
451 int if_is_no_ptm_operative(struct interface *ifp)
452 {
453 return ((ifp->flags & IFF_UP)
454 && ((ifp->flags & IFF_RUNNING)
455 || !CHECK_FLAG(ifp->status,
456 ZEBRA_INTERFACE_LINKDETECTION)));
457 }
458
459 /* Is this loopback interface ? */
460 int if_is_loopback(struct interface *ifp)
461 {
462 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
463 * but Y on platform N?
464 */
465 return (ifp->flags & (IFF_LOOPBACK | IFF_NOXMIT | IFF_VIRTUAL));
466 }
467
468 /* Check interface is VRF */
469 int if_is_vrf(struct interface *ifp)
470 {
471 return CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
472 }
473
474 /* Does this interface support broadcast ? */
475 int if_is_broadcast(struct interface *ifp)
476 {
477 return ifp->flags & IFF_BROADCAST;
478 }
479
480 /* Does this interface support broadcast ? */
481 int if_is_pointopoint(struct interface *ifp)
482 {
483 return ifp->flags & IFF_POINTOPOINT;
484 }
485
486 /* Does this interface support multicast ? */
487 int if_is_multicast(struct interface *ifp)
488 {
489 return ifp->flags & IFF_MULTICAST;
490 }
491
492 /* Printout flag information into log */
493 const char *if_flag_dump(unsigned long flag)
494 {
495 int separator = 0;
496 static char logbuf[BUFSIZ];
497
498 #define IFF_OUT_LOG(X, STR) \
499 if (flag & (X)) { \
500 if (separator) \
501 strlcat(logbuf, ",", BUFSIZ); \
502 else \
503 separator = 1; \
504 strlcat(logbuf, STR, BUFSIZ); \
505 }
506
507 strlcpy(logbuf, "<", BUFSIZ);
508 IFF_OUT_LOG(IFF_UP, "UP");
509 IFF_OUT_LOG(IFF_BROADCAST, "BROADCAST");
510 IFF_OUT_LOG(IFF_DEBUG, "DEBUG");
511 IFF_OUT_LOG(IFF_LOOPBACK, "LOOPBACK");
512 IFF_OUT_LOG(IFF_POINTOPOINT, "POINTOPOINT");
513 IFF_OUT_LOG(IFF_NOTRAILERS, "NOTRAILERS");
514 IFF_OUT_LOG(IFF_RUNNING, "RUNNING");
515 IFF_OUT_LOG(IFF_NOARP, "NOARP");
516 IFF_OUT_LOG(IFF_PROMISC, "PROMISC");
517 IFF_OUT_LOG(IFF_ALLMULTI, "ALLMULTI");
518 IFF_OUT_LOG(IFF_OACTIVE, "OACTIVE");
519 IFF_OUT_LOG(IFF_SIMPLEX, "SIMPLEX");
520 IFF_OUT_LOG(IFF_LINK0, "LINK0");
521 IFF_OUT_LOG(IFF_LINK1, "LINK1");
522 IFF_OUT_LOG(IFF_LINK2, "LINK2");
523 IFF_OUT_LOG(IFF_MULTICAST, "MULTICAST");
524 IFF_OUT_LOG(IFF_NOXMIT, "NOXMIT");
525 IFF_OUT_LOG(IFF_NORTEXCH, "NORTEXCH");
526 IFF_OUT_LOG(IFF_VIRTUAL, "VIRTUAL");
527 IFF_OUT_LOG(IFF_IPV4, "IPv4");
528 IFF_OUT_LOG(IFF_IPV6, "IPv6");
529
530 strlcat(logbuf, ">", BUFSIZ);
531
532 return logbuf;
533 #undef IFF_OUT_LOG
534 }
535
536 /* For debugging */
537 static void if_dump(const struct interface *ifp)
538 {
539 struct listnode *node;
540 struct connected *c __attribute__((unused));
541
542 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, c))
543 zlog_info(
544 "Interface %s vrf %u index %d metric %d mtu %d "
545 "mtu6 %d %s",
546 ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric,
547 ifp->mtu, ifp->mtu6, if_flag_dump(ifp->flags));
548 }
549
550 /* Interface printing for all interface. */
551 void if_dump_all(void)
552 {
553 struct vrf *vrf;
554 void *ifp;
555
556 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
557 FOR_ALL_INTERFACES (vrf, ifp)
558 if_dump(ifp);
559 }
560
561 DEFUN (interface_desc,
562 interface_desc_cmd,
563 "description LINE...",
564 "Interface specific description\n"
565 "Characters describing this interface\n")
566 {
567 int idx_line = 1;
568 VTY_DECLVAR_CONTEXT(interface, ifp);
569
570 if (ifp->desc)
571 XFREE(MTYPE_TMP, ifp->desc);
572 ifp->desc = argv_concat(argv, argc, idx_line);
573
574 return CMD_SUCCESS;
575 }
576
577 DEFUN (no_interface_desc,
578 no_interface_desc_cmd,
579 "no description",
580 NO_STR
581 "Interface specific description\n")
582 {
583 VTY_DECLVAR_CONTEXT(interface, ifp);
584
585 if (ifp->desc)
586 XFREE(MTYPE_TMP, ifp->desc);
587 ifp->desc = NULL;
588
589 return CMD_SUCCESS;
590 }
591
592 #ifdef SUNOS_5
593 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
594 * a seperate struct interface for each logical interface, so config
595 * file may be full of 'interface fooX:Y'. Solaris however does not
596 * expose logical interfaces via PF_ROUTE, so trying to track logical
597 * interfaces can be fruitless, for that reason Quagga only tracks
598 * the primary IP interface.
599 *
600 * We try accomodate SUNWzebra by:
601 * - looking up the interface name, to see whether it exists, if so
602 * its useable
603 * - for protocol daemons, this could only because zebra told us of
604 * the interface
605 * - for zebra, only because it learnt from kernel
606 * - if not:
607 * - search the name to see if it contains a sub-ipif / logical interface
608 * seperator, the ':' char. If it does:
609 * - text up to that char must be the primary name - get that name.
610 * if not:
611 * - no idea, just get the name in its entirety.
612 */
613 static struct interface *if_sunwzebra_get(char *name, vrf_id_t vrf_id)
614 {
615 struct interface *ifp;
616 char *cp;
617
618 if ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
619 return ifp;
620
621 /* hunt the primary interface name... */
622 cp = strchr(name, ':');
623 if (cp)
624 *cp = '\0';
625
626 return if_get_by_name(name, vrf_id, 1);
627 }
628 #endif /* SUNOS_5 */
629
630 DEFUN_NOSH (interface,
631 interface_cmd,
632 "interface IFNAME [vrf NAME]",
633 "Select an interface to configure\n"
634 "Interface's name\n"
635 VRF_CMD_HELP_STR)
636 {
637 int idx_ifname = 1;
638 int idx_vrf = 3;
639 const char *ifname = argv[idx_ifname]->arg;
640 const char *vrfname = (argc > 2) ? argv[idx_vrf]->arg : NULL;
641
642 struct interface *ifp;
643 vrf_id_t vrf_id = VRF_DEFAULT;
644
645 if (strlen(ifname) > INTERFACE_NAMSIZ) {
646 vty_out(vty,
647 "%% Interface name %s is invalid: length exceeds "
648 "%d characters\n",
649 ifname, INTERFACE_NAMSIZ);
650 return CMD_WARNING_CONFIG_FAILED;
651 }
652
653 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
654 Imagine forward reference of a vrf by name in this interface config */
655 if (vrfname)
656 VRF_GET_ID(vrf_id, vrfname, false);
657
658 #ifdef SUNOS_5
659 ifp = if_sunwzebra_get(ifname, vrf_id);
660 #else
661 ifp = if_get_by_name(ifname, vrf_id, 1);
662 #endif /* SUNOS_5 */
663
664 if (!ifp) {
665 vty_out(vty, "%% interface %s not in %s\n", ifname, vrfname);
666 return CMD_WARNING_CONFIG_FAILED;
667 }
668 VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp);
669
670 return CMD_SUCCESS;
671 }
672
673 DEFUN (no_interface,
674 no_interface_cmd,
675 "no interface IFNAME [vrf NAME]",
676 NO_STR
677 "Delete a pseudo interface's configuration\n"
678 "Interface's name\n"
679 VRF_CMD_HELP_STR)
680 {
681 int idx_vrf = 4;
682 const char *ifname = argv[2]->arg;
683 const char *vrfname = (argc > 3) ? argv[idx_vrf]->arg : NULL;
684
685 // deleting interface
686 struct interface *ifp;
687 vrf_id_t vrf_id = VRF_DEFAULT;
688
689 if (argc > 3)
690 VRF_GET_ID(vrf_id, vrfname, false);
691
692 ifp = if_lookup_by_name(ifname, vrf_id);
693
694 if (ifp == NULL) {
695 vty_out(vty, "%% Interface %s does not exist\n", ifname);
696 return CMD_WARNING_CONFIG_FAILED;
697 }
698
699 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
700 vty_out(vty, "%% Only inactive interfaces can be deleted\n");
701 return CMD_WARNING_CONFIG_FAILED;
702 }
703
704 if_delete(ifp);
705
706 return CMD_SUCCESS;
707 }
708
709 static void if_autocomplete(vector comps, struct cmd_token *token)
710 {
711 struct interface *ifp;
712 struct vrf *vrf = NULL;
713
714 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
715 FOR_ALL_INTERFACES (vrf, ifp) {
716 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name));
717 }
718 }
719 }
720
721 static const struct cmd_variable_handler if_var_handlers[] = {
722 {/* "interface NAME" */
723 .varname = "interface",
724 .completions = if_autocomplete},
725 {.tokenname = "IFNAME", .completions = if_autocomplete},
726 {.tokenname = "INTERFACE", .completions = if_autocomplete},
727 {.completions = NULL}};
728
729 void if_cmd_init(void)
730 {
731 cmd_variable_handler_register(if_var_handlers);
732
733 install_element(CONFIG_NODE, &interface_cmd);
734 install_element(CONFIG_NODE, &no_interface_cmd);
735
736 install_default(INTERFACE_NODE);
737 install_element(INTERFACE_NODE, &interface_desc_cmd);
738 install_element(INTERFACE_NODE, &no_interface_desc_cmd);
739 }
740
741 #if 0
742 /* For debug purpose. */
743 DEFUN (show_address,
744 show_address_cmd,
745 "show address [vrf NAME]",
746 SHOW_STR
747 "address\n"
748 VRF_CMD_HELP_STR)
749 {
750 int idx_vrf = 3;
751 struct listnode *node;
752 struct interface *ifp;
753 struct connected *ifc;
754 struct prefix *p;
755 vrf_id_t vrf_id = VRF_DEFAULT;
756
757 if (argc > 2)
758 VRF_GET_ID (vrf_id, argv[idx_vrf]->arg);
759
760 FOR_ALL_INTERFACES (vrf, ifp)
761 {
762 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
763 {
764 p = ifc->address;
765
766 if (p->family == AF_INET)
767 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
768 }
769 }
770 return CMD_SUCCESS;
771 }
772
773 DEFUN (show_address_vrf_all,
774 show_address_vrf_all_cmd,
775 "show address vrf all",
776 SHOW_STR
777 "address\n"
778 VRF_ALL_CMD_HELP_STR)
779 {
780 struct vrf *vrf;
781 struct listnode *node;
782 struct interface *ifp;
783 struct connected *ifc;
784 struct prefix *p;
785
786 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
787 {
788 if (RB_EMPTY (if_name_head, &vrf->ifaces_by_name))
789 continue;
790
791 vty_out (vty, "\nVRF %u\n\n", vrf->vrf_id);
792
793 FOR_ALL_INTERFACES (vrf, ifp)
794 {
795 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
796 {
797 p = ifc->address;
798
799 if (p->family == AF_INET)
800 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
801 }
802 }
803 }
804 return CMD_SUCCESS;
805 }
806 #endif
807
808 /* Allocate connected structure. */
809 struct connected *connected_new(void)
810 {
811 return XCALLOC(MTYPE_CONNECTED, sizeof(struct connected));
812 }
813
814 /* Allocate nbr connected structure. */
815 struct nbr_connected *nbr_connected_new(void)
816 {
817 return XCALLOC(MTYPE_NBR_CONNECTED, sizeof(struct nbr_connected));
818 }
819
820 /* Free connected structure. */
821 void connected_free(struct connected *connected)
822 {
823 if (connected->address)
824 prefix_free(connected->address);
825
826 if (connected->destination)
827 prefix_free(connected->destination);
828
829 if (connected->label)
830 XFREE(MTYPE_CONNECTED_LABEL, connected->label);
831
832 XFREE(MTYPE_CONNECTED, connected);
833 }
834
835 /* Free nbr connected structure. */
836 void nbr_connected_free(struct nbr_connected *connected)
837 {
838 if (connected->address)
839 prefix_free(connected->address);
840
841 XFREE(MTYPE_NBR_CONNECTED, connected);
842 }
843
844 /* If same interface nbr address already exists... */
845 struct nbr_connected *nbr_connected_check(struct interface *ifp,
846 struct prefix *p)
847 {
848 struct nbr_connected *ifc;
849 struct listnode *node;
850
851 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, ifc))
852 if (prefix_same(ifc->address, p))
853 return ifc;
854
855 return NULL;
856 }
857
858 /* Print if_addr structure. */
859 static void __attribute__((unused))
860 connected_log(struct connected *connected, char *str)
861 {
862 struct prefix *p;
863 struct interface *ifp;
864 char logbuf[BUFSIZ];
865 char buf[BUFSIZ];
866
867 ifp = connected->ifp;
868 p = connected->address;
869
870 snprintf(logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ", str,
871 ifp->name, ifp->vrf_id, prefix_family_str(p),
872 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
873
874 p = connected->destination;
875 if (p) {
876 strncat(logbuf, inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
877 BUFSIZ - strlen(logbuf));
878 }
879 zlog_info("%s", logbuf);
880 }
881
882 /* Print if_addr structure. */
883 static void __attribute__((unused))
884 nbr_connected_log(struct nbr_connected *connected, char *str)
885 {
886 struct prefix *p;
887 struct interface *ifp;
888 char logbuf[BUFSIZ];
889 char buf[BUFSIZ];
890
891 ifp = connected->ifp;
892 p = connected->address;
893
894 snprintf(logbuf, BUFSIZ, "%s interface %s %s %s/%d ", str, ifp->name,
895 prefix_family_str(p),
896 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
897
898 zlog_info("%s", logbuf);
899 }
900
901 /* If two connected address has same prefix return 1. */
902 static int connected_same_prefix(struct prefix *p1, struct prefix *p2)
903 {
904 if (p1->family == p2->family) {
905 if (p1->family == AF_INET
906 && IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
907 return 1;
908 if (p1->family == AF_INET6
909 && IPV6_ADDR_SAME(&p1->u.prefix6, &p2->u.prefix6))
910 return 1;
911 }
912 return 0;
913 }
914
915 struct connected *connected_lookup_prefix_exact(struct interface *ifp,
916 struct prefix *p)
917 {
918 struct listnode *node;
919 struct listnode *next;
920 struct connected *ifc;
921
922 for (node = listhead(ifp->connected); node; node = next) {
923 ifc = listgetdata(node);
924 next = node->next;
925
926 if (connected_same_prefix(ifc->address, p))
927 return ifc;
928 }
929 return NULL;
930 }
931
932 struct connected *connected_delete_by_prefix(struct interface *ifp,
933 struct prefix *p)
934 {
935 struct listnode *node;
936 struct listnode *next;
937 struct connected *ifc;
938
939 /* In case of same prefix come, replace it with new one. */
940 for (node = listhead(ifp->connected); node; node = next) {
941 ifc = listgetdata(node);
942 next = node->next;
943
944 if (connected_same_prefix(ifc->address, p)) {
945 listnode_delete(ifp->connected, ifc);
946 return ifc;
947 }
948 }
949 return NULL;
950 }
951
952 /* Find the address on our side that will be used when packets
953 are sent to dst. */
954 struct connected *connected_lookup_prefix(struct interface *ifp,
955 struct prefix *addr)
956 {
957 struct listnode *cnode;
958 struct connected *c;
959 struct connected *match;
960
961 match = NULL;
962
963 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
964 if (c->address && (c->address->family == addr->family)
965 && prefix_match(CONNECTED_PREFIX(c), addr)
966 && (!match
967 || (c->address->prefixlen > match->address->prefixlen)))
968 match = c;
969 }
970 return match;
971 }
972
973 struct connected *connected_add_by_prefix(struct interface *ifp,
974 struct prefix *p,
975 struct prefix *destination)
976 {
977 struct connected *ifc;
978
979 /* Allocate new connected address. */
980 ifc = connected_new();
981 ifc->ifp = ifp;
982
983 /* Fetch interface address */
984 ifc->address = prefix_new();
985 memcpy(ifc->address, p, sizeof(struct prefix));
986
987 /* Fetch dest address */
988 if (destination) {
989 ifc->destination = prefix_new();
990 memcpy(ifc->destination, destination, sizeof(struct prefix));
991 }
992
993 /* Add connected address to the interface. */
994 listnode_add(ifp->connected, ifc);
995 return ifc;
996 }
997
998 #if 0 /* this route_table of struct connected's is unused \
999 * however, it would be good to use a route_table rather than \
1000 * a list.. \
1001 */
1002 /* Interface looking up by interface's address. */
1003 /* Interface's IPv4 address reverse lookup table. */
1004 struct route_table *ifaddr_ipv4_table;
1005 /* struct route_table *ifaddr_ipv6_table; */
1006
1007 static void
1008 ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
1009 {
1010 struct route_node *rn;
1011 struct prefix_ipv4 p;
1012
1013 p.family = AF_INET;
1014 p.prefixlen = IPV4_MAX_PREFIXLEN;
1015 p.prefix = *ifaddr;
1016
1017 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
1018 if (rn)
1019 {
1020 route_unlock_node (rn);
1021 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1022 inet_ntoa (*ifaddr));
1023 return;
1024 }
1025 rn->info = ifp;
1026 }
1027
1028 static void
1029 ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
1030 {
1031 struct route_node *rn;
1032 struct prefix_ipv4 p;
1033
1034 p.family = AF_INET;
1035 p.prefixlen = IPV4_MAX_PREFIXLEN;
1036 p.prefix = *ifaddr;
1037
1038 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1039 if (! rn)
1040 {
1041 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1042 inet_ntoa (*ifaddr));
1043 return;
1044 }
1045 rn->info = NULL;
1046 route_unlock_node (rn);
1047 route_unlock_node (rn);
1048 }
1049
1050 /* Lookup interface by interface's IP address or interface index. */
1051 static struct interface *
1052 ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
1053 {
1054 struct prefix_ipv4 p;
1055 struct route_node *rn;
1056 struct interface *ifp;
1057
1058 if (addr)
1059 {
1060 p.family = AF_INET;
1061 p.prefixlen = IPV4_MAX_PREFIXLEN;
1062 p.prefix = *addr;
1063
1064 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1065 if (! rn)
1066 return NULL;
1067
1068 ifp = rn->info;
1069 route_unlock_node (rn);
1070 return ifp;
1071 }
1072 else
1073 return if_lookup_by_index(ifindex, VRF_DEFAULT);
1074 }
1075 #endif /* ifaddr_ipv4_table */
1076
1077 void if_terminate(struct vrf *vrf)
1078 {
1079 struct interface *ifp;
1080
1081 while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
1082 ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name);
1083
1084 if (ifp->node) {
1085 ifp->node->info = NULL;
1086 route_unlock_node(ifp->node);
1087 }
1088 if_delete(ifp);
1089 }
1090 }
1091
1092 const char *if_link_type_str(enum zebra_link_type llt)
1093 {
1094 switch (llt) {
1095 #define llts(T,S) case (T): return (S)
1096 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
1097 llts(ZEBRA_LLT_ETHER, "Ethernet");
1098 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
1099 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
1100 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
1101 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
1102 llts(ZEBRA_LLT_ARCNET, "ARCnet");
1103 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
1104 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
1105 llts(ZEBRA_LLT_ATM, "ATM");
1106 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
1107 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
1108 llts(ZEBRA_LLT_EUI64, "EUI-64");
1109 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
1110 llts(ZEBRA_LLT_SLIP, "SLIP");
1111 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
1112 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
1113 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
1114 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
1115 llts(ZEBRA_LLT_X25, "CCITT X.25");
1116 llts(ZEBRA_LLT_PPP, "PPP");
1117 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
1118 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1119 llts(ZEBRA_LLT_LAPB, "LAPB");
1120 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1121 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1122 llts(ZEBRA_LLT_FRAD, "FRAD");
1123 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1124 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1125 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1126 llts(ZEBRA_LLT_FDDI, "FDDI");
1127 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1128 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1129 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1130 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1131 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1132 llts(ZEBRA_LLT_IRDA, "IrDA");
1133 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1134 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1135 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1136 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1137 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1138 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1139 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1140 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1141 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1142 default:
1143 zlog_warn("Unknown value %d", llt);
1144 return "Unknown type!";
1145 #undef llts
1146 }
1147 return NULL;
1148 }
1149
1150 struct if_link_params *if_link_params_get(struct interface *ifp)
1151 {
1152 int i;
1153
1154 if (ifp->link_params != NULL)
1155 return ifp->link_params;
1156
1157 struct if_link_params *iflp =
1158 XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params));
1159 if (iflp == NULL)
1160 return NULL;
1161
1162 /* Set TE metric equal to standard metric */
1163 iflp->te_metric = ifp->metric;
1164
1165 /* Compute default bandwidth based on interface */
1166 iflp->default_bw =
1167 ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1168 * TE_KILO_BIT / TE_BYTE);
1169
1170 /* Set Max, Reservable and Unreserved Bandwidth */
1171 iflp->max_bw = iflp->default_bw;
1172 iflp->max_rsv_bw = iflp->default_bw;
1173 for (i = 0; i < MAX_CLASS_TYPE; i++)
1174 iflp->unrsv_bw[i] = iflp->default_bw;
1175
1176 /* Update Link parameters status */
1177 iflp->lp_status =
1178 LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
1179
1180 /* Finally attach newly created Link Parameters */
1181 ifp->link_params = iflp;
1182
1183 return iflp;
1184 }
1185
1186 void if_link_params_free(struct interface *ifp)
1187 {
1188 if (ifp->link_params == NULL)
1189 return;
1190 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1191 ifp->link_params = NULL;
1192 }