]> git.proxmox.com Git - mirror_frr.git/blob - lib/if.c
lib: dont decode more nexthops than we can handle
[mirror_frr.git] / lib / if.c
1 /*
2 * Interface functions.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "linklist.h"
25 #include "vector.h"
26 #include "lib_errors.h"
27 #include "vty.h"
28 #include "command.h"
29 #include "vrf.h"
30 #include "if.h"
31 #include "sockunion.h"
32 #include "prefix.h"
33 #include "memory.h"
34 #include "table.h"
35 #include "buffer.h"
36 #include "log.h"
37 #include "northbound_cli.h"
38 #ifndef VTYSH_EXTRACT_PL
39 #include "lib/if_clippy.c"
40 #endif
41
42 DEFINE_MTYPE_STATIC(LIB, IF, "Interface")
43 DEFINE_MTYPE_STATIC(LIB, CONNECTED, "Connected")
44 DEFINE_MTYPE_STATIC(LIB, NBR_CONNECTED, "Neighbor Connected")
45 DEFINE_MTYPE(LIB, CONNECTED_LABEL, "Connected interface label")
46 DEFINE_MTYPE_STATIC(LIB, IF_LINK_PARAMS, "Informational Link Parameters")
47
48 static struct interface *if_lookup_by_ifindex(ifindex_t ifindex,
49 vrf_id_t vrf_id);
50 static int if_cmp_func(const struct interface *, const struct interface *);
51 static int if_cmp_index_func(const struct interface *ifp1,
52 const struct interface *ifp2);
53 RB_GENERATE(if_name_head, interface, name_entry, if_cmp_func);
54 RB_GENERATE(if_index_head, interface, index_entry, if_cmp_index_func);
55
56 DEFINE_QOBJ_TYPE(interface)
57
58 DEFINE_HOOK(if_add, (struct interface * ifp), (ifp))
59 DEFINE_KOOH(if_del, (struct interface * ifp), (ifp))
60
61 static struct interface_master{
62 int (*create_hook)(struct interface *ifp);
63 int (*up_hook)(struct interface *ifp);
64 int (*down_hook)(struct interface *ifp);
65 int (*destroy_hook)(struct interface *ifp);
66 } ifp_master = { 0, };
67
68 /* Compare interface names, returning an integer greater than, equal to, or
69 * less than 0, (following the strcmp convention), according to the
70 * relationship between ifp1 and ifp2. Interface names consist of an
71 * alphabetic prefix and a numeric suffix. The primary sort key is
72 * lexicographic by name, and then numeric by number. No number sorts
73 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
74 * devpty0, de0 < del0
75 */
76 int if_cmp_name_func(const char *p1, const char *p2)
77 {
78 unsigned int l1, l2;
79 long int x1, x2;
80 int res;
81
82 while (*p1 && *p2) {
83 /* look up to any number */
84 l1 = strcspn(p1, "0123456789");
85 l2 = strcspn(p2, "0123456789");
86
87 /* name lengths are different -> compare names */
88 if (l1 != l2)
89 return (strcmp(p1, p2));
90
91 /* Note that this relies on all numbers being less than all
92 * letters, so
93 * that de0 < del0.
94 */
95 res = strncmp(p1, p2, l1);
96
97 /* names are different -> compare them */
98 if (res)
99 return res;
100
101 /* with identical name part, go to numeric part */
102 p1 += l1;
103 p2 += l1;
104
105 if (!*p1 && !*p2)
106 return 0;
107 if (!*p1)
108 return -1;
109 if (!*p2)
110 return 1;
111
112 x1 = strtol(p1, (char **)&p1, 10);
113 x2 = strtol(p2, (char **)&p2, 10);
114
115 /* let's compare numbers now */
116 if (x1 < x2)
117 return -1;
118 if (x1 > x2)
119 return 1;
120
121 /* numbers were equal, lets do it again..
122 (it happens with name like "eth123.456:789") */
123 }
124 if (*p1)
125 return 1;
126 if (*p2)
127 return -1;
128 return 0;
129 }
130
131 static int if_cmp_func(const struct interface *ifp1,
132 const struct interface *ifp2)
133 {
134 return if_cmp_name_func(ifp1->name, ifp2->name);
135 }
136
137 static int if_cmp_index_func(const struct interface *ifp1,
138 const struct interface *ifp2)
139 {
140 return ifp1->ifindex - ifp2->ifindex;
141 }
142
143 static void ifp_connected_free(void *arg)
144 {
145 struct connected *c = arg;
146
147 connected_free(&c);
148 }
149
150 /* Create new interface structure. */
151 static struct interface *if_new(vrf_id_t vrf_id)
152 {
153 struct interface *ifp;
154
155 ifp = XCALLOC(MTYPE_IF, sizeof(struct interface));
156
157 ifp->ifindex = IFINDEX_INTERNAL;
158 ifp->name[0] = '\0';
159
160 ifp->vrf_id = vrf_id;
161
162 ifp->connected = list_new();
163 ifp->connected->del = ifp_connected_free;
164
165 ifp->nbr_connected = list_new();
166 ifp->nbr_connected->del = (void (*)(void *))nbr_connected_free;
167
168 /* Enable Link-detection by default */
169 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
170
171 QOBJ_REG(ifp, interface);
172 return ifp;
173 }
174
175 void if_new_via_zapi(struct interface *ifp)
176 {
177 if (ifp_master.create_hook)
178 (*ifp_master.create_hook)(ifp);
179 }
180
181 void if_destroy_via_zapi(struct interface *ifp)
182 {
183 if (ifp_master.destroy_hook)
184 (*ifp_master.destroy_hook)(ifp);
185
186 if_set_index(ifp, IFINDEX_INTERNAL);
187 if (!ifp->configured)
188 if_delete(&ifp);
189 }
190
191 void if_up_via_zapi(struct interface *ifp)
192 {
193 if (ifp_master.up_hook)
194 (*ifp_master.up_hook)(ifp);
195 }
196
197 void if_down_via_zapi(struct interface *ifp)
198 {
199 if (ifp_master.down_hook)
200 (*ifp_master.down_hook)(ifp);
201 }
202
203 struct interface *if_create_name(const char *name, vrf_id_t vrf_id)
204 {
205 struct interface *ifp;
206
207 ifp = if_new(vrf_id);
208
209 if_set_name(ifp, name);
210
211 hook_call(if_add, ifp);
212 return ifp;
213 }
214
215 struct interface *if_create_ifindex(ifindex_t ifindex, vrf_id_t vrf_id)
216 {
217 struct interface *ifp;
218
219 ifp = if_new(vrf_id);
220
221 if_set_index(ifp, ifindex);
222
223 hook_call(if_add, ifp);
224 return ifp;
225 }
226
227 /* Create new interface structure. */
228 void if_update_to_new_vrf(struct interface *ifp, vrf_id_t vrf_id)
229 {
230 struct vrf *old_vrf, *vrf;
231
232 /* remove interface from old master vrf list */
233 old_vrf = vrf_lookup_by_id(ifp->vrf_id);
234 if (old_vrf) {
235 if (ifp->name[0] != '\0')
236 IFNAME_RB_REMOVE(old_vrf, ifp);
237
238 if (ifp->ifindex != IFINDEX_INTERNAL)
239 IFINDEX_RB_REMOVE(old_vrf, ifp);
240 }
241
242 ifp->vrf_id = vrf_id;
243 vrf = vrf_get(ifp->vrf_id, NULL);
244
245 if (ifp->name[0] != '\0')
246 IFNAME_RB_INSERT(vrf, ifp);
247
248 if (ifp->ifindex != IFINDEX_INTERNAL)
249 IFINDEX_RB_INSERT(vrf, ifp);
250
251 /*
252 * HACK: Change the interface VRF in the running configuration directly,
253 * bypassing the northbound layer. This is necessary to avoid deleting
254 * the interface and readding it in the new VRF, which would have
255 * several implications.
256 */
257 if (yang_module_find("frr-interface")) {
258 struct lyd_node *if_dnode;
259
260 if_dnode = yang_dnode_get(
261 running_config->dnode,
262 "/frr-interface:lib/interface[name='%s'][vrf='%s']/vrf",
263 ifp->name, old_vrf->name);
264 if (if_dnode) {
265 nb_running_unset_entry(if_dnode->parent);
266 yang_dnode_change_leaf(if_dnode, vrf->name);
267 nb_running_set_entry(if_dnode->parent, ifp);
268 running_config->version++;
269 }
270 }
271 }
272
273
274 /* Delete interface structure. */
275 void if_delete_retain(struct interface *ifp)
276 {
277 hook_call(if_del, ifp);
278 QOBJ_UNREG(ifp);
279
280 /* Free connected address list */
281 list_delete_all_node(ifp->connected);
282
283 /* Free connected nbr address list */
284 list_delete_all_node(ifp->nbr_connected);
285 }
286
287 /* Delete and free interface structure. */
288 void if_delete(struct interface **ifp)
289 {
290 struct interface *ptr = *ifp;
291 struct vrf *vrf;
292
293 vrf = vrf_lookup_by_id(ptr->vrf_id);
294 assert(vrf);
295
296 IFNAME_RB_REMOVE(vrf, ptr);
297 if (ptr->ifindex != IFINDEX_INTERNAL)
298 IFINDEX_RB_REMOVE(vrf, ptr);
299
300 if_delete_retain(ptr);
301
302 list_delete(&ptr->connected);
303 list_delete(&ptr->nbr_connected);
304
305 if_link_params_free(ptr);
306
307 XFREE(MTYPE_TMP, ptr->desc);
308
309 XFREE(MTYPE_IF, ptr);
310 *ifp = NULL;
311 }
312
313 /* Used only internally to check within VRF only */
314 static struct interface *if_lookup_by_ifindex(ifindex_t ifindex,
315 vrf_id_t vrf_id)
316 {
317 struct vrf *vrf;
318 struct interface if_tmp;
319
320 vrf = vrf_lookup_by_id(vrf_id);
321 if (!vrf)
322 return NULL;
323
324 if_tmp.ifindex = ifindex;
325 return RB_FIND(if_index_head, &vrf->ifaces_by_index, &if_tmp);
326 }
327
328 /* Interface existance check by index. */
329 struct interface *if_lookup_by_index(ifindex_t ifindex, vrf_id_t vrf_id)
330 {
331 switch (vrf_get_backend()) {
332 case VRF_BACKEND_UNKNOWN:
333 case VRF_BACKEND_NETNS:
334 return(if_lookup_by_ifindex(ifindex, vrf_id));
335 case VRF_BACKEND_VRF_LITE:
336 return(if_lookup_by_index_all_vrf(ifindex));
337 }
338 return NULL;
339 }
340
341 const char *ifindex2ifname(ifindex_t ifindex, vrf_id_t vrf_id)
342 {
343 struct interface *ifp;
344
345 return ((ifp = if_lookup_by_index(ifindex, vrf_id)) != NULL)
346 ? ifp->name
347 : "unknown";
348 }
349
350 ifindex_t ifname2ifindex(const char *name, vrf_id_t vrf_id)
351 {
352 struct interface *ifp;
353
354 return ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
355 ? ifp->ifindex
356 : IFINDEX_INTERNAL;
357 }
358
359 /* Interface existance check by interface name. */
360 struct interface *if_lookup_by_name(const char *name, vrf_id_t vrf_id)
361 {
362 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
363 struct interface if_tmp;
364
365 if (!vrf || !name
366 || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
367 return NULL;
368
369 strlcpy(if_tmp.name, name, sizeof(if_tmp.name));
370 return RB_FIND(if_name_head, &vrf->ifaces_by_name, &if_tmp);
371 }
372
373 struct interface *if_lookup_by_name_all_vrf(const char *name)
374 {
375 struct vrf *vrf;
376 struct interface *ifp;
377
378 if (!name || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
379 return NULL;
380
381 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
382 ifp = if_lookup_by_name(name, vrf->vrf_id);
383 if (ifp)
384 return ifp;
385 }
386
387 return NULL;
388 }
389
390 struct interface *if_lookup_by_index_all_vrf(ifindex_t ifindex)
391 {
392 struct vrf *vrf;
393 struct interface *ifp;
394
395 if (ifindex == IFINDEX_INTERNAL)
396 return NULL;
397
398 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
399 ifp = if_lookup_by_ifindex(ifindex, vrf->vrf_id);
400 if (ifp)
401 return ifp;
402 }
403
404 return NULL;
405 }
406
407 /* Lookup interface by IP address. */
408 struct interface *if_lookup_exact_address(void *src, int family,
409 vrf_id_t vrf_id)
410 {
411 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
412 struct listnode *cnode;
413 struct interface *ifp;
414 struct prefix *p;
415 struct connected *c;
416
417 FOR_ALL_INTERFACES (vrf, ifp) {
418 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
419 p = c->address;
420
421 if (p && (p->family == family)) {
422 if (family == AF_INET) {
423 if (IPV4_ADDR_SAME(
424 &p->u.prefix4,
425 (struct in_addr *)src))
426 return ifp;
427 } else if (family == AF_INET6) {
428 if (IPV6_ADDR_SAME(
429 &p->u.prefix6,
430 (struct in6_addr *)src))
431 return ifp;
432 }
433 }
434 }
435 }
436 return NULL;
437 }
438
439 /* Lookup interface by IP address. */
440 struct connected *if_lookup_address(void *matchaddr, int family,
441 vrf_id_t vrf_id)
442 {
443 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
444 struct prefix addr;
445 int bestlen = 0;
446 struct listnode *cnode;
447 struct interface *ifp;
448 struct connected *c;
449 struct connected *match;
450
451 if (family == AF_INET) {
452 addr.family = AF_INET;
453 addr.u.prefix4 = *((struct in_addr *)matchaddr);
454 addr.prefixlen = IPV4_MAX_BITLEN;
455 } else if (family == AF_INET6) {
456 addr.family = AF_INET6;
457 addr.u.prefix6 = *((struct in6_addr *)matchaddr);
458 addr.prefixlen = IPV6_MAX_BITLEN;
459 }
460
461 match = NULL;
462
463 FOR_ALL_INTERFACES (vrf, ifp) {
464 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
465 if (c->address && (c->address->family == AF_INET)
466 && prefix_match(CONNECTED_PREFIX(c), &addr)
467 && (c->address->prefixlen > bestlen)) {
468 bestlen = c->address->prefixlen;
469 match = c;
470 }
471 }
472 }
473 return match;
474 }
475
476 /* Lookup interface by prefix */
477 struct interface *if_lookup_prefix(struct prefix *prefix, vrf_id_t vrf_id)
478 {
479 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
480 struct listnode *cnode;
481 struct interface *ifp;
482 struct connected *c;
483
484 FOR_ALL_INTERFACES (vrf, ifp) {
485 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
486 if (prefix_cmp(c->address, prefix) == 0) {
487 return ifp;
488 }
489 }
490 }
491 return NULL;
492 }
493
494 size_t if_lookup_by_hwaddr(const uint8_t *hw_addr, size_t addrsz,
495 struct interface ***result, vrf_id_t vrf_id)
496 {
497 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
498
499 struct list *rs = list_new();
500 struct interface *ifp;
501
502 FOR_ALL_INTERFACES (vrf, ifp) {
503 if (ifp->hw_addr_len == (int)addrsz
504 && !memcmp(hw_addr, ifp->hw_addr, addrsz))
505 listnode_add(rs, ifp);
506 }
507
508 if (rs->count) {
509 *result = XCALLOC(MTYPE_TMP,
510 sizeof(struct interface *) * rs->count);
511 list_to_array(rs, (void **)*result, rs->count);
512 }
513
514 int count = rs->count;
515
516 list_delete(&rs);
517
518 return count;
519 }
520
521
522 /* Get interface by name if given name interface doesn't exist create
523 one. */
524 struct interface *if_get_by_name(const char *name, vrf_id_t vrf_id)
525 {
526 struct interface *ifp;
527
528 switch (vrf_get_backend()) {
529 case VRF_BACKEND_UNKNOWN:
530 case VRF_BACKEND_NETNS:
531 ifp = if_lookup_by_name(name, vrf_id);
532 if (ifp)
533 return ifp;
534 return if_create_name(name, vrf_id);
535 case VRF_BACKEND_VRF_LITE:
536 ifp = if_lookup_by_name_all_vrf(name);
537 if (ifp) {
538 if (ifp->vrf_id == vrf_id)
539 return ifp;
540 /* If it came from the kernel or by way of zclient,
541 * believe it and update the ifp accordingly.
542 */
543 if_update_to_new_vrf(ifp, vrf_id);
544 return ifp;
545 }
546 return if_create_name(name, vrf_id);
547 }
548
549 return NULL;
550 }
551
552 struct interface *if_get_by_ifindex(ifindex_t ifindex, vrf_id_t vrf_id)
553 {
554 struct interface *ifp;
555
556 switch (vrf_get_backend()) {
557 case VRF_BACKEND_UNKNOWN:
558 case VRF_BACKEND_NETNS:
559 ifp = if_lookup_by_ifindex(ifindex, vrf_id);
560 if (ifp)
561 return ifp;
562 return if_create_ifindex(ifindex, vrf_id);
563 case VRF_BACKEND_VRF_LITE:
564 ifp = if_lookup_by_index_all_vrf(ifindex);
565 if (ifp) {
566 if (ifp->vrf_id == vrf_id)
567 return ifp;
568 /* If it came from the kernel or by way of zclient,
569 * believe it and update the ifp accordingly.
570 */
571 if_update_to_new_vrf(ifp, vrf_id);
572 return ifp;
573 }
574 return if_create_ifindex(ifindex, vrf_id);
575 }
576
577 return NULL;
578 }
579
580 void if_set_index(struct interface *ifp, ifindex_t ifindex)
581 {
582 struct vrf *vrf;
583
584 vrf = vrf_get(ifp->vrf_id, NULL);
585 assert(vrf);
586
587 if (ifp->ifindex == ifindex)
588 return;
589
590 if (ifp->ifindex != IFINDEX_INTERNAL)
591 IFINDEX_RB_REMOVE(vrf, ifp);
592
593 ifp->ifindex = ifindex;
594
595 if (ifp->ifindex != IFINDEX_INTERNAL)
596 IFINDEX_RB_INSERT(vrf, ifp)
597 }
598
599 void if_set_name(struct interface *ifp, const char *name)
600 {
601 struct vrf *vrf;
602
603 vrf = vrf_get(ifp->vrf_id, NULL);
604 assert(vrf);
605
606 if (if_cmp_name_func(ifp->name, name) == 0)
607 return;
608
609 if (ifp->name[0] != '\0')
610 IFNAME_RB_REMOVE(vrf, ifp);
611
612 strlcpy(ifp->name, name, sizeof(ifp->name));
613
614 if (ifp->name[0] != '\0')
615 IFNAME_RB_INSERT(vrf, ifp);
616 }
617
618 /* Does interface up ? */
619 int if_is_up(const struct interface *ifp)
620 {
621 return ifp->flags & IFF_UP;
622 }
623
624 /* Is interface running? */
625 int if_is_running(const struct interface *ifp)
626 {
627 return ifp->flags & IFF_RUNNING;
628 }
629
630 /* Is the interface operative, eg. either UP & RUNNING
631 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
632 if ptm checking is enabled, then ptm check has passed */
633 int if_is_operative(const struct interface *ifp)
634 {
635 return ((ifp->flags & IFF_UP)
636 && (((ifp->flags & IFF_RUNNING)
637 && (ifp->ptm_status || !ifp->ptm_enable))
638 || !CHECK_FLAG(ifp->status,
639 ZEBRA_INTERFACE_LINKDETECTION)));
640 }
641
642 /* Is the interface operative, eg. either UP & RUNNING
643 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
644 int if_is_no_ptm_operative(const struct interface *ifp)
645 {
646 return ((ifp->flags & IFF_UP)
647 && ((ifp->flags & IFF_RUNNING)
648 || !CHECK_FLAG(ifp->status,
649 ZEBRA_INTERFACE_LINKDETECTION)));
650 }
651
652 /* Is this loopback interface ? */
653 int if_is_loopback(const struct interface *ifp)
654 {
655 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
656 * but Y on platform N?
657 */
658 return (ifp->flags & (IFF_LOOPBACK | IFF_NOXMIT | IFF_VIRTUAL));
659 }
660
661 /* Check interface is VRF */
662 int if_is_vrf(const struct interface *ifp)
663 {
664 return CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
665 }
666
667 bool if_is_loopback_or_vrf(const struct interface *ifp)
668 {
669 if (if_is_loopback(ifp) || if_is_vrf(ifp))
670 return true;
671
672 return false;
673 }
674
675 /* Does this interface support broadcast ? */
676 int if_is_broadcast(const struct interface *ifp)
677 {
678 return ifp->flags & IFF_BROADCAST;
679 }
680
681 /* Does this interface support broadcast ? */
682 int if_is_pointopoint(const struct interface *ifp)
683 {
684 return ifp->flags & IFF_POINTOPOINT;
685 }
686
687 /* Does this interface support multicast ? */
688 int if_is_multicast(const struct interface *ifp)
689 {
690 return ifp->flags & IFF_MULTICAST;
691 }
692
693 /* Printout flag information into log */
694 const char *if_flag_dump(unsigned long flag)
695 {
696 int separator = 0;
697 static char logbuf[BUFSIZ];
698
699 #define IFF_OUT_LOG(X, STR) \
700 if (flag & (X)) { \
701 if (separator) \
702 strlcat(logbuf, ",", sizeof(logbuf)); \
703 else \
704 separator = 1; \
705 strlcat(logbuf, STR, sizeof(logbuf)); \
706 }
707
708 strlcpy(logbuf, "<", BUFSIZ);
709 IFF_OUT_LOG(IFF_UP, "UP");
710 IFF_OUT_LOG(IFF_BROADCAST, "BROADCAST");
711 IFF_OUT_LOG(IFF_DEBUG, "DEBUG");
712 IFF_OUT_LOG(IFF_LOOPBACK, "LOOPBACK");
713 IFF_OUT_LOG(IFF_POINTOPOINT, "POINTOPOINT");
714 IFF_OUT_LOG(IFF_NOTRAILERS, "NOTRAILERS");
715 IFF_OUT_LOG(IFF_RUNNING, "RUNNING");
716 IFF_OUT_LOG(IFF_NOARP, "NOARP");
717 IFF_OUT_LOG(IFF_PROMISC, "PROMISC");
718 IFF_OUT_LOG(IFF_ALLMULTI, "ALLMULTI");
719 IFF_OUT_LOG(IFF_OACTIVE, "OACTIVE");
720 IFF_OUT_LOG(IFF_SIMPLEX, "SIMPLEX");
721 IFF_OUT_LOG(IFF_LINK0, "LINK0");
722 IFF_OUT_LOG(IFF_LINK1, "LINK1");
723 IFF_OUT_LOG(IFF_LINK2, "LINK2");
724 IFF_OUT_LOG(IFF_MULTICAST, "MULTICAST");
725 IFF_OUT_LOG(IFF_NOXMIT, "NOXMIT");
726 IFF_OUT_LOG(IFF_NORTEXCH, "NORTEXCH");
727 IFF_OUT_LOG(IFF_VIRTUAL, "VIRTUAL");
728 IFF_OUT_LOG(IFF_IPV4, "IPv4");
729 IFF_OUT_LOG(IFF_IPV6, "IPv6");
730
731 strlcat(logbuf, ">", sizeof(logbuf));
732
733 return logbuf;
734 #undef IFF_OUT_LOG
735 }
736
737 /* For debugging */
738 static void if_dump(const struct interface *ifp)
739 {
740 struct listnode *node;
741 struct connected *c __attribute__((unused));
742
743 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, c))
744 zlog_info(
745 "Interface %s vrf %u index %d metric %d mtu %d "
746 "mtu6 %d %s",
747 ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric,
748 ifp->mtu, ifp->mtu6, if_flag_dump(ifp->flags));
749 }
750
751 /* Interface printing for all interface. */
752 void if_dump_all(void)
753 {
754 struct vrf *vrf;
755 void *ifp;
756
757 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
758 FOR_ALL_INTERFACES (vrf, ifp)
759 if_dump(ifp);
760 }
761
762 #ifdef SUNOS_5
763 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
764 * a seperate struct interface for each logical interface, so config
765 * file may be full of 'interface fooX:Y'. Solaris however does not
766 * expose logical interfaces via PF_ROUTE, so trying to track logical
767 * interfaces can be fruitless, for that reason Quagga only tracks
768 * the primary IP interface.
769 *
770 * We try accomodate SUNWzebra by:
771 * - looking up the interface name, to see whether it exists, if so
772 * its useable
773 * - for protocol daemons, this could only because zebra told us of
774 * the interface
775 * - for zebra, only because it learnt from kernel
776 * - if not:
777 * - search the name to see if it contains a sub-ipif / logical interface
778 * seperator, the ':' char. If it does:
779 * - text up to that char must be the primary name - get that name.
780 * if not:
781 * - no idea, just get the name in its entirety.
782 */
783 static struct interface *if_sunwzebra_get(const char *name, vrf_id_t vrf_id)
784 {
785 struct interface *ifp;
786 char *cp;
787
788 if ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
789 return ifp;
790
791 /* hunt the primary interface name... */
792 cp = strchr(name, ':');
793 if (cp)
794 *cp = '\0';
795
796 return if_get_by_name(name, vrf_id);
797 }
798 #endif /* SUNOS_5 */
799
800 #if 0
801 /* For debug purpose. */
802 DEFUN (show_address,
803 show_address_cmd,
804 "show address [vrf NAME]",
805 SHOW_STR
806 "address\n"
807 VRF_CMD_HELP_STR)
808 {
809 int idx_vrf = 3;
810 struct listnode *node;
811 struct interface *ifp;
812 struct connected *ifc;
813 struct prefix *p;
814 vrf_id_t vrf_id = VRF_DEFAULT;
815
816 if (argc > 2)
817 VRF_GET_ID (vrf_id, argv[idx_vrf]->arg);
818
819 FOR_ALL_INTERFACES (vrf, ifp)
820 {
821 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
822 {
823 p = ifc->address;
824
825 if (p->family == AF_INET)
826 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
827 }
828 }
829 return CMD_SUCCESS;
830 }
831
832 DEFUN (show_address_vrf_all,
833 show_address_vrf_all_cmd,
834 "show address vrf all",
835 SHOW_STR
836 "address\n"
837 VRF_ALL_CMD_HELP_STR)
838 {
839 struct vrf *vrf;
840 struct listnode *node;
841 struct interface *ifp;
842 struct connected *ifc;
843 struct prefix *p;
844
845 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
846 {
847 if (RB_EMPTY (if_name_head, &vrf->ifaces_by_name))
848 continue;
849
850 vty_out (vty, "\nVRF %u\n\n", vrf->vrf_id);
851
852 FOR_ALL_INTERFACES (vrf, ifp)
853 {
854 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
855 {
856 p = ifc->address;
857
858 if (p->family == AF_INET)
859 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
860 }
861 }
862 }
863 return CMD_SUCCESS;
864 }
865 #endif
866
867 /* Allocate connected structure. */
868 struct connected *connected_new(void)
869 {
870 return XCALLOC(MTYPE_CONNECTED, sizeof(struct connected));
871 }
872
873 /* Allocate nbr connected structure. */
874 struct nbr_connected *nbr_connected_new(void)
875 {
876 return XCALLOC(MTYPE_NBR_CONNECTED, sizeof(struct nbr_connected));
877 }
878
879 /* Free connected structure. */
880 void connected_free(struct connected **connected)
881 {
882 struct connected *ptr = *connected;
883
884 if (ptr->address)
885 prefix_free(&ptr->address);
886
887 if (ptr->destination)
888 prefix_free(&ptr->destination);
889
890 XFREE(MTYPE_CONNECTED_LABEL, ptr->label);
891
892 XFREE(MTYPE_CONNECTED, ptr);
893 *connected = NULL;
894 }
895
896 /* Free nbr connected structure. */
897 void nbr_connected_free(struct nbr_connected *connected)
898 {
899 if (connected->address)
900 prefix_free(&connected->address);
901
902 XFREE(MTYPE_NBR_CONNECTED, connected);
903 }
904
905 /* If same interface nbr address already exists... */
906 struct nbr_connected *nbr_connected_check(struct interface *ifp,
907 struct prefix *p)
908 {
909 struct nbr_connected *ifc;
910 struct listnode *node;
911
912 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, ifc))
913 if (prefix_same(ifc->address, p))
914 return ifc;
915
916 return NULL;
917 }
918
919 /* Print if_addr structure. */
920 static void __attribute__((unused))
921 connected_log(struct connected *connected, char *str)
922 {
923 struct prefix *p;
924 struct interface *ifp;
925 char logbuf[BUFSIZ];
926 char buf[BUFSIZ];
927
928 ifp = connected->ifp;
929 p = connected->address;
930
931 snprintf(logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ", str,
932 ifp->name, ifp->vrf_id, prefix_family_str(p),
933 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
934
935 p = connected->destination;
936 if (p) {
937 strncat(logbuf, inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
938 BUFSIZ - strlen(logbuf));
939 }
940 zlog_info("%s", logbuf);
941 }
942
943 /* Print if_addr structure. */
944 static void __attribute__((unused))
945 nbr_connected_log(struct nbr_connected *connected, char *str)
946 {
947 struct prefix *p;
948 struct interface *ifp;
949 char logbuf[BUFSIZ];
950 char buf[BUFSIZ];
951
952 ifp = connected->ifp;
953 p = connected->address;
954
955 snprintf(logbuf, BUFSIZ, "%s interface %s %s %s/%d ", str, ifp->name,
956 prefix_family_str(p),
957 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
958
959 zlog_info("%s", logbuf);
960 }
961
962 /* If two connected address has same prefix return 1. */
963 static int connected_same_prefix(struct prefix *p1, struct prefix *p2)
964 {
965 if (p1->family == p2->family) {
966 if (p1->family == AF_INET
967 && IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
968 return 1;
969 if (p1->family == AF_INET6
970 && IPV6_ADDR_SAME(&p1->u.prefix6, &p2->u.prefix6))
971 return 1;
972 }
973 return 0;
974 }
975
976 /* count the number of connected addresses that are in the given family */
977 unsigned int connected_count_by_family(struct interface *ifp, int family)
978 {
979 struct listnode *cnode;
980 struct connected *connected;
981 unsigned int cnt = 0;
982
983 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected))
984 if (connected->address->family == family)
985 cnt++;
986
987 return cnt;
988 }
989
990 struct connected *connected_lookup_prefix_exact(struct interface *ifp,
991 struct prefix *p)
992 {
993 struct listnode *node;
994 struct listnode *next;
995 struct connected *ifc;
996
997 for (node = listhead(ifp->connected); node; node = next) {
998 ifc = listgetdata(node);
999 next = node->next;
1000
1001 if (connected_same_prefix(ifc->address, p))
1002 return ifc;
1003 }
1004 return NULL;
1005 }
1006
1007 struct connected *connected_delete_by_prefix(struct interface *ifp,
1008 struct prefix *p)
1009 {
1010 struct listnode *node;
1011 struct listnode *next;
1012 struct connected *ifc;
1013
1014 /* In case of same prefix come, replace it with new one. */
1015 for (node = listhead(ifp->connected); node; node = next) {
1016 ifc = listgetdata(node);
1017 next = node->next;
1018
1019 if (connected_same_prefix(ifc->address, p)) {
1020 listnode_delete(ifp->connected, ifc);
1021 return ifc;
1022 }
1023 }
1024 return NULL;
1025 }
1026
1027 /* Find the address on our side that will be used when packets
1028 are sent to dst. */
1029 struct connected *connected_lookup_prefix(struct interface *ifp,
1030 struct prefix *addr)
1031 {
1032 struct listnode *cnode;
1033 struct connected *c;
1034 struct connected *match;
1035
1036 match = NULL;
1037
1038 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
1039 if (c->address && (c->address->family == addr->family)
1040 && prefix_match(CONNECTED_PREFIX(c), addr)
1041 && (!match
1042 || (c->address->prefixlen > match->address->prefixlen)))
1043 match = c;
1044 }
1045 return match;
1046 }
1047
1048 struct connected *connected_add_by_prefix(struct interface *ifp,
1049 struct prefix *p,
1050 struct prefix *destination)
1051 {
1052 struct connected *ifc;
1053
1054 /* Allocate new connected address. */
1055 ifc = connected_new();
1056 ifc->ifp = ifp;
1057
1058 /* Fetch interface address */
1059 ifc->address = prefix_new();
1060 memcpy(ifc->address, p, sizeof(struct prefix));
1061
1062 /* Fetch dest address */
1063 if (destination) {
1064 ifc->destination = prefix_new();
1065 memcpy(ifc->destination, destination, sizeof(struct prefix));
1066 }
1067
1068 /* Add connected address to the interface. */
1069 listnode_add(ifp->connected, ifc);
1070 return ifc;
1071 }
1072
1073 struct connected *connected_get_linklocal(struct interface *ifp)
1074 {
1075 struct listnode *n;
1076 struct connected *c = NULL;
1077
1078 for (ALL_LIST_ELEMENTS_RO(ifp->connected, n, c)) {
1079 if (c->address->family == AF_INET6
1080 && IN6_IS_ADDR_LINKLOCAL(&c->address->u.prefix6))
1081 break;
1082 }
1083 return c;
1084 }
1085
1086 #if 0 /* this route_table of struct connected's is unused \
1087 * however, it would be good to use a route_table rather than \
1088 * a list.. \
1089 */
1090 /* Interface looking up by interface's address. */
1091 /* Interface's IPv4 address reverse lookup table. */
1092 struct route_table *ifaddr_ipv4_table;
1093 /* struct route_table *ifaddr_ipv6_table; */
1094
1095 static void
1096 ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
1097 {
1098 struct route_node *rn;
1099 struct prefix_ipv4 p;
1100
1101 p.family = AF_INET;
1102 p.prefixlen = IPV4_MAX_PREFIXLEN;
1103 p.prefix = *ifaddr;
1104
1105 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
1106 if (rn)
1107 {
1108 route_unlock_node (rn);
1109 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1110 inet_ntoa (*ifaddr));
1111 return;
1112 }
1113 rn->info = ifp;
1114 }
1115
1116 static void
1117 ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
1118 {
1119 struct route_node *rn;
1120 struct prefix_ipv4 p;
1121
1122 p.family = AF_INET;
1123 p.prefixlen = IPV4_MAX_PREFIXLEN;
1124 p.prefix = *ifaddr;
1125
1126 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1127 if (! rn)
1128 {
1129 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1130 inet_ntoa (*ifaddr));
1131 return;
1132 }
1133 rn->info = NULL;
1134 route_unlock_node (rn);
1135 route_unlock_node (rn);
1136 }
1137
1138 /* Lookup interface by interface's IP address or interface index. */
1139 static struct interface *
1140 ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
1141 {
1142 struct prefix_ipv4 p;
1143 struct route_node *rn;
1144 struct interface *ifp;
1145
1146 if (addr)
1147 {
1148 p.family = AF_INET;
1149 p.prefixlen = IPV4_MAX_PREFIXLEN;
1150 p.prefix = *addr;
1151
1152 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1153 if (! rn)
1154 return NULL;
1155
1156 ifp = rn->info;
1157 route_unlock_node (rn);
1158 return ifp;
1159 }
1160 else
1161 return if_lookup_by_index(ifindex, VRF_DEFAULT);
1162 }
1163 #endif /* ifaddr_ipv4_table */
1164
1165 void if_terminate(struct vrf *vrf)
1166 {
1167 struct interface *ifp;
1168
1169 while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
1170 ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name);
1171
1172 if (ifp->node) {
1173 ifp->node->info = NULL;
1174 route_unlock_node(ifp->node);
1175 }
1176 if_delete(&ifp);
1177 }
1178 }
1179
1180 const char *if_link_type_str(enum zebra_link_type llt)
1181 {
1182 switch (llt) {
1183 #define llts(T,S) case (T): return (S)
1184 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
1185 llts(ZEBRA_LLT_ETHER, "Ethernet");
1186 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
1187 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
1188 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
1189 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
1190 llts(ZEBRA_LLT_ARCNET, "ARCnet");
1191 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
1192 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
1193 llts(ZEBRA_LLT_ATM, "ATM");
1194 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
1195 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
1196 llts(ZEBRA_LLT_EUI64, "EUI-64");
1197 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
1198 llts(ZEBRA_LLT_SLIP, "SLIP");
1199 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
1200 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
1201 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
1202 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
1203 llts(ZEBRA_LLT_X25, "CCITT X.25");
1204 llts(ZEBRA_LLT_PPP, "PPP");
1205 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
1206 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1207 llts(ZEBRA_LLT_LAPB, "LAPB");
1208 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1209 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1210 llts(ZEBRA_LLT_FRAD, "FRAD");
1211 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1212 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1213 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1214 llts(ZEBRA_LLT_FDDI, "FDDI");
1215 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1216 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1217 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1218 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1219 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1220 llts(ZEBRA_LLT_IRDA, "IrDA");
1221 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1222 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1223 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1224 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1225 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1226 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1227 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1228 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1229 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1230 default:
1231 flog_err(EC_LIB_DEVELOPMENT, "Unknown value %d", llt);
1232 return "Unknown type!";
1233 #undef llts
1234 }
1235 return NULL;
1236 }
1237
1238 struct if_link_params *if_link_params_get(struct interface *ifp)
1239 {
1240 int i;
1241
1242 if (ifp->link_params != NULL)
1243 return ifp->link_params;
1244
1245 struct if_link_params *iflp =
1246 XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params));
1247 if (iflp == NULL)
1248 return NULL;
1249
1250 /* Set TE metric equal to standard metric */
1251 iflp->te_metric = ifp->metric;
1252
1253 /* Compute default bandwidth based on interface */
1254 iflp->default_bw =
1255 ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1256 * TE_KILO_BIT / TE_BYTE);
1257
1258 /* Set Max, Reservable and Unreserved Bandwidth */
1259 iflp->max_bw = iflp->default_bw;
1260 iflp->max_rsv_bw = iflp->default_bw;
1261 for (i = 0; i < MAX_CLASS_TYPE; i++)
1262 iflp->unrsv_bw[i] = iflp->default_bw;
1263
1264 /* Update Link parameters status */
1265 iflp->lp_status =
1266 LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
1267
1268 /* Finally attach newly created Link Parameters */
1269 ifp->link_params = iflp;
1270
1271 return iflp;
1272 }
1273
1274 void if_link_params_free(struct interface *ifp)
1275 {
1276 if (ifp->link_params == NULL)
1277 return;
1278 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1279 ifp->link_params = NULL;
1280 }
1281
1282 /* ----------- CLI commands ----------- */
1283
1284 /*
1285 * XPath: /frr-interface:lib/interface
1286 */
1287 DEFPY_NOSH (interface,
1288 interface_cmd,
1289 "interface IFNAME [vrf NAME$vrf_name]",
1290 "Select an interface to configure\n"
1291 "Interface's name\n"
1292 VRF_CMD_HELP_STR)
1293 {
1294 char xpath_list[XPATH_MAXLEN];
1295 vrf_id_t vrf_id;
1296 struct interface *ifp;
1297 int ret;
1298
1299 if (!vrf_name)
1300 vrf_name = VRF_DEFAULT_NAME;
1301
1302 /*
1303 * This command requires special handling to maintain backward
1304 * compatibility. If a VRF name is not specified, it means we're willing
1305 * to accept any interface with the given name on any VRF. If no
1306 * interface is found, then a new one should be created on the default
1307 * VRF.
1308 */
1309 VRF_GET_ID(vrf_id, vrf_name, false);
1310 ifp = if_lookup_by_name_all_vrf(ifname);
1311 if (ifp && ifp->vrf_id != vrf_id) {
1312 struct vrf *vrf;
1313
1314 /*
1315 * Special case 1: a VRF name was specified, but the found
1316 * interface is associated to different VRF. Reject the command.
1317 */
1318 if (vrf_id != VRF_DEFAULT) {
1319 vty_out(vty, "%% interface %s not in %s vrf\n", ifname,
1320 vrf_name);
1321 return CMD_WARNING_CONFIG_FAILED;
1322 }
1323
1324 /*
1325 * Special case 2: a VRF name was *not* specified, and the found
1326 * interface is associated to a VRF other than the default one.
1327 * Update vrf_id and vrf_name to account for that.
1328 */
1329 vrf = vrf_lookup_by_id(ifp->vrf_id);
1330 assert(vrf);
1331 vrf_id = ifp->vrf_id;
1332 vrf_name = vrf->name;
1333 }
1334
1335 snprintf(xpath_list, sizeof(xpath_list),
1336 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname,
1337 vrf_name);
1338
1339 nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, NULL);
1340 ret = nb_cli_apply_changes(vty, xpath_list);
1341 if (ret == CMD_SUCCESS) {
1342 VTY_PUSH_XPATH(INTERFACE_NODE, xpath_list);
1343
1344 /*
1345 * For backward compatibility with old commands we still need
1346 * to use the qobj infrastructure. This can be removed once
1347 * all interface-level commands are converted to the new
1348 * northbound model.
1349 */
1350 ifp = if_lookup_by_name(ifname, vrf_id);
1351 if (ifp)
1352 VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp);
1353 }
1354
1355 return ret;
1356 }
1357
1358 DEFPY (no_interface,
1359 no_interface_cmd,
1360 "no interface IFNAME [vrf NAME$vrf_name]",
1361 NO_STR
1362 "Delete a pseudo interface's configuration\n"
1363 "Interface's name\n"
1364 VRF_CMD_HELP_STR)
1365 {
1366 if (!vrf_name)
1367 vrf_name = VRF_DEFAULT_NAME;
1368
1369 nb_cli_enqueue_change(vty, ".", NB_OP_DESTROY, NULL);
1370
1371 return nb_cli_apply_changes(
1372 vty, "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1373 ifname, vrf_name);
1374 }
1375
1376 static void cli_show_interface(struct vty *vty, struct lyd_node *dnode,
1377 bool show_defaults)
1378 {
1379 const char *vrf;
1380
1381 vrf = yang_dnode_get_string(dnode, "./vrf");
1382
1383 vty_out(vty, "!\n");
1384 vty_out(vty, "interface %s", yang_dnode_get_string(dnode, "./name"));
1385 if (!strmatch(vrf, VRF_DEFAULT_NAME))
1386 vty_out(vty, " vrf %s", vrf);
1387 vty_out(vty, "\n");
1388 }
1389
1390 /*
1391 * XPath: /frr-interface:lib/interface/description
1392 */
1393 DEFPY (interface_desc,
1394 interface_desc_cmd,
1395 "description LINE...",
1396 "Interface specific description\n"
1397 "Characters describing this interface\n")
1398 {
1399 char *desc;
1400 int ret;
1401
1402 desc = argv_concat(argv, argc, 1);
1403 nb_cli_enqueue_change(vty, "./description", NB_OP_MODIFY, desc);
1404 ret = nb_cli_apply_changes(vty, NULL);
1405 XFREE(MTYPE_TMP, desc);
1406
1407 return ret;
1408 }
1409
1410 DEFPY (no_interface_desc,
1411 no_interface_desc_cmd,
1412 "no description",
1413 NO_STR
1414 "Interface specific description\n")
1415 {
1416 nb_cli_enqueue_change(vty, "./description", NB_OP_DESTROY, NULL);
1417
1418 return nb_cli_apply_changes(vty, NULL);
1419 }
1420
1421 static void cli_show_interface_desc(struct vty *vty, struct lyd_node *dnode,
1422 bool show_defaults)
1423 {
1424 vty_out(vty, " description %s\n", yang_dnode_get_string(dnode, NULL));
1425 }
1426
1427 /* Interface autocomplete. */
1428 static void if_autocomplete(vector comps, struct cmd_token *token)
1429 {
1430 struct interface *ifp;
1431 struct vrf *vrf;
1432
1433 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1434 FOR_ALL_INTERFACES (vrf, ifp) {
1435 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name));
1436 }
1437 }
1438 }
1439
1440 static const struct cmd_variable_handler if_var_handlers[] = {
1441 {/* "interface NAME" */
1442 .varname = "interface",
1443 .completions = if_autocomplete},
1444 {.tokenname = "IFNAME", .completions = if_autocomplete},
1445 {.tokenname = "INTERFACE", .completions = if_autocomplete},
1446 {.completions = NULL}};
1447
1448 void if_cmd_init(void)
1449 {
1450 cmd_variable_handler_register(if_var_handlers);
1451
1452 install_element(CONFIG_NODE, &interface_cmd);
1453 install_element(CONFIG_NODE, &no_interface_cmd);
1454
1455 install_default(INTERFACE_NODE);
1456 install_element(INTERFACE_NODE, &interface_desc_cmd);
1457 install_element(INTERFACE_NODE, &no_interface_desc_cmd);
1458 }
1459
1460 void if_zapi_callbacks(int (*create)(struct interface *ifp),
1461 int (*up)(struct interface *ifp),
1462 int (*down)(struct interface *ifp),
1463 int (*destroy)(struct interface *ifp))
1464 {
1465 ifp_master.create_hook = create;
1466 ifp_master.up_hook = up;
1467 ifp_master.down_hook = down;
1468 ifp_master.destroy_hook = destroy;
1469 }
1470
1471 /* ------- Northbound callbacks ------- */
1472
1473 /*
1474 * XPath: /frr-interface:lib/interface
1475 */
1476 static int lib_interface_create(enum nb_event event,
1477 const struct lyd_node *dnode,
1478 union nb_resource *resource)
1479 {
1480 const char *ifname;
1481 const char *vrfname;
1482 struct vrf *vrf;
1483 struct interface *ifp;
1484
1485 ifname = yang_dnode_get_string(dnode, "./name");
1486 vrfname = yang_dnode_get_string(dnode, "./vrf");
1487
1488 switch (event) {
1489 case NB_EV_VALIDATE:
1490 vrf = vrf_lookup_by_name(vrfname);
1491 if (!vrf) {
1492 zlog_warn("%s: VRF %s doesn't exist", __func__,
1493 vrfname);
1494 return NB_ERR_VALIDATION;
1495 }
1496 if (vrf->vrf_id == VRF_UNKNOWN) {
1497 zlog_warn("%s: VRF %s is not active", __func__,
1498 vrf->name);
1499 return NB_ERR_VALIDATION;
1500 }
1501
1502 /* if VRF is netns or not yet known - init for instance
1503 * then assumption is that passed config is exact
1504 * then the user intent was not to use an other iface
1505 */
1506 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE) {
1507 ifp = if_lookup_by_name_all_vrf(ifname);
1508 if (ifp && ifp->vrf_id != vrf->vrf_id) {
1509 zlog_warn(
1510 "%s: interface %s already exists in another VRF",
1511 __func__, ifp->name);
1512 return NB_ERR_VALIDATION;
1513 }
1514 }
1515 break;
1516 case NB_EV_PREPARE:
1517 case NB_EV_ABORT:
1518 break;
1519 case NB_EV_APPLY:
1520 vrf = vrf_lookup_by_name(vrfname);
1521 assert(vrf);
1522 #ifdef SUNOS_5
1523 ifp = if_sunwzebra_get(ifname, vrf->vrf_id);
1524 #else
1525 ifp = if_get_by_name(ifname, vrf->vrf_id);
1526 #endif /* SUNOS_5 */
1527
1528 ifp->configured = true;
1529 nb_running_set_entry(dnode, ifp);
1530 break;
1531 }
1532
1533 return NB_OK;
1534 }
1535
1536 static int lib_interface_destroy(enum nb_event event,
1537 const struct lyd_node *dnode)
1538 {
1539 struct interface *ifp;
1540
1541
1542 switch (event) {
1543 case NB_EV_VALIDATE:
1544 ifp = nb_running_get_entry(dnode, NULL, true);
1545 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1546 zlog_warn("%s: only inactive interfaces can be deleted",
1547 __func__);
1548 return NB_ERR_VALIDATION;
1549 }
1550 break;
1551 case NB_EV_PREPARE:
1552 case NB_EV_ABORT:
1553 break;
1554 case NB_EV_APPLY:
1555 ifp = nb_running_unset_entry(dnode);
1556
1557 ifp->configured = false;
1558 if_delete(&ifp);
1559 break;
1560 }
1561
1562 return NB_OK;
1563 }
1564
1565 /*
1566 * XPath: /frr-interface:lib/interface
1567 */
1568 static const void *lib_interface_get_next(const void *parent_list_entry,
1569 const void *list_entry)
1570 {
1571 struct vrf *vrf;
1572 struct interface *pif = (struct interface *)list_entry;
1573
1574 if (list_entry == NULL) {
1575 vrf = RB_MIN(vrf_name_head, &vrfs_by_name);
1576 assert(vrf);
1577 pif = RB_MIN(if_name_head, &vrf->ifaces_by_name);
1578 } else {
1579 vrf = vrf_lookup_by_id(pif->vrf_id);
1580 pif = RB_NEXT(if_name_head, pif);
1581 /* if no more interfaces, switch to next vrf */
1582 while (pif == NULL) {
1583 vrf = RB_NEXT(vrf_name_head, vrf);
1584 if (!vrf)
1585 return NULL;
1586 pif = RB_MIN(if_name_head, &vrf->ifaces_by_name);
1587 }
1588 }
1589
1590 return pif;
1591 }
1592
1593 static int lib_interface_get_keys(const void *list_entry,
1594 struct yang_list_keys *keys)
1595 {
1596 const struct interface *ifp = list_entry;
1597
1598 struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id);
1599
1600 assert(vrf);
1601
1602 keys->num = 2;
1603 strlcpy(keys->key[0], ifp->name, sizeof(keys->key[0]));
1604 strlcpy(keys->key[1], vrf->name, sizeof(keys->key[1]));
1605
1606 return NB_OK;
1607 }
1608
1609 static const void *lib_interface_lookup_entry(const void *parent_list_entry,
1610 const struct yang_list_keys *keys)
1611 {
1612 const char *ifname = keys->key[0];
1613 const char *vrfname = keys->key[1];
1614 struct vrf *vrf = vrf_lookup_by_name(vrfname);
1615
1616 return vrf ? if_lookup_by_name(ifname, vrf->vrf_id) : NULL;
1617 }
1618
1619 /*
1620 * XPath: /frr-interface:lib/interface/description
1621 */
1622 static int lib_interface_description_modify(enum nb_event event,
1623 const struct lyd_node *dnode,
1624 union nb_resource *resource)
1625 {
1626 struct interface *ifp;
1627 const char *description;
1628
1629 if (event != NB_EV_APPLY)
1630 return NB_OK;
1631
1632 ifp = nb_running_get_entry(dnode, NULL, true);
1633 XFREE(MTYPE_TMP, ifp->desc);
1634 description = yang_dnode_get_string(dnode, NULL);
1635 ifp->desc = XSTRDUP(MTYPE_TMP, description);
1636
1637 return NB_OK;
1638 }
1639
1640 static int lib_interface_description_destroy(enum nb_event event,
1641 const struct lyd_node *dnode)
1642 {
1643 struct interface *ifp;
1644
1645 if (event != NB_EV_APPLY)
1646 return NB_OK;
1647
1648 ifp = nb_running_get_entry(dnode, NULL, true);
1649 XFREE(MTYPE_TMP, ifp->desc);
1650
1651 return NB_OK;
1652 }
1653
1654 /* clang-format off */
1655
1656 #if defined(__GNUC__) && ((__GNUC__ - 0) < 5) && !defined(__clang__)
1657 /* gcc versions before 5.x miscalculate the size for structs with variable
1658 * length arrays (they just count it as size 0)
1659 */
1660 struct frr_yang_module_info_size3 {
1661 /* YANG module name. */
1662 const char *name;
1663
1664 /* Northbound callbacks. */
1665 const struct {
1666 /* Data path of this YANG node. */
1667 const char *xpath;
1668
1669 /* Callbacks implemented for this node. */
1670 struct nb_callbacks cbs;
1671
1672 /* Priority - lower priorities are processed first. */
1673 uint32_t priority;
1674 } nodes[3];
1675 };
1676
1677 const struct frr_yang_module_info_size3 frr_interface_info_size3 asm("frr_interface_info") = {
1678 #else
1679 const struct frr_yang_module_info frr_interface_info = {
1680 #endif
1681 .name = "frr-interface",
1682 .nodes = {
1683 {
1684 .xpath = "/frr-interface:lib/interface",
1685 .cbs = {
1686 .create = lib_interface_create,
1687 .destroy = lib_interface_destroy,
1688 .cli_show = cli_show_interface,
1689 .get_next = lib_interface_get_next,
1690 .get_keys = lib_interface_get_keys,
1691 .lookup_entry = lib_interface_lookup_entry,
1692 },
1693 },
1694 {
1695 .xpath = "/frr-interface:lib/interface/description",
1696 .cbs = {
1697 .modify = lib_interface_description_modify,
1698 .destroy = lib_interface_description_destroy,
1699 .cli_show = cli_show_interface_desc,
1700 },
1701 },
1702 {
1703 .xpath = NULL,
1704 },
1705 }
1706 };