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