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