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