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