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