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