]> git.proxmox.com Git - mirror_frr.git/blob - lib/if.c
08d89187427319b7ac525f36a43c0e5ee7916f92
[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 }
1032 if_delete(&ifp);
1033 }
1034 }
1035
1036 const char *if_link_type_str(enum zebra_link_type llt)
1037 {
1038 switch (llt) {
1039 #define llts(T,S) case (T): return (S)
1040 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
1041 llts(ZEBRA_LLT_ETHER, "Ethernet");
1042 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
1043 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
1044 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
1045 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
1046 llts(ZEBRA_LLT_ARCNET, "ARCnet");
1047 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
1048 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
1049 llts(ZEBRA_LLT_ATM, "ATM");
1050 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
1051 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
1052 llts(ZEBRA_LLT_EUI64, "EUI-64");
1053 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
1054 llts(ZEBRA_LLT_SLIP, "SLIP");
1055 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
1056 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
1057 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
1058 llts(ZEBRA_LLT_RSRVD, "Reserved");
1059 llts(ZEBRA_LLT_ADAPT, "Adapt");
1060 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
1061 llts(ZEBRA_LLT_X25, "CCITT X.25");
1062 llts(ZEBRA_LLT_PPP, "PPP");
1063 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
1064 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1065 llts(ZEBRA_LLT_LAPB, "LAPB");
1066 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1067 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1068 llts(ZEBRA_LLT_FRAD, "FRAD");
1069 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1070 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1071 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1072 llts(ZEBRA_LLT_FDDI, "FDDI");
1073 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1074 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1075 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1076 llts(ZEBRA_LLT_IP6GRE, "GRE over IPv6");
1077 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1078 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1079 llts(ZEBRA_LLT_ECONET, "Acorn Econet");
1080 llts(ZEBRA_LLT_IRDA, "IrDA");
1081 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1082 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1083 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1084 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1085 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1086 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1087 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1088 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1089 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1090 #undef llts
1091 }
1092 return NULL;
1093 }
1094
1095 bool if_link_params_cmp(struct if_link_params *iflp1,
1096 struct if_link_params *iflp2)
1097 {
1098 struct if_link_params iflp1_copy, iflp2_copy;
1099
1100 /* Extended admin-groups in if_link_params contain pointers.
1101 * They cannot be compared with memcpy.
1102 * Make copies of if_link_params without ext. admin-groups
1103 * and compare separately the ext. admin-groups.
1104 */
1105 memcpy(&iflp1_copy, iflp1, sizeof(struct if_link_params));
1106 memset(&iflp1_copy.ext_admin_grp, 0, sizeof(struct admin_group));
1107
1108 memcpy(&iflp2_copy, iflp2, sizeof(struct if_link_params));
1109 memset(&iflp2_copy.ext_admin_grp, 0, sizeof(struct admin_group));
1110
1111 if (memcmp(&iflp1_copy, &iflp2_copy, sizeof(struct if_link_params)))
1112 return false;
1113
1114 if (!admin_group_cmp(&iflp1->ext_admin_grp, &iflp2->ext_admin_grp))
1115 return false;
1116
1117 return true;
1118 }
1119
1120 void if_link_params_copy(struct if_link_params *dst, struct if_link_params *src)
1121 {
1122 struct admin_group dst_ag;
1123
1124 /* backup the admin_group structure that contains a pointer */
1125 memcpy(&dst_ag, &dst->ext_admin_grp, sizeof(struct admin_group));
1126 /* copy the if_link_params structure */
1127 memcpy(dst, src, sizeof(struct if_link_params));
1128 /* restore the admin_group structure */
1129 memcpy(&dst->ext_admin_grp, &dst_ag, sizeof(struct admin_group));
1130 /* copy src->ext_admin_grp data to dst->ext_admin_grp data memory */
1131 admin_group_copy(&dst->ext_admin_grp, &src->ext_admin_grp);
1132 }
1133
1134 struct if_link_params *if_link_params_get(struct interface *ifp)
1135 {
1136 return ifp->link_params;
1137 }
1138
1139 struct if_link_params *if_link_params_enable(struct interface *ifp)
1140 {
1141 struct if_link_params *iflp;
1142 int i;
1143
1144 iflp = if_link_params_init(ifp);
1145
1146 /* Compute default bandwidth based on interface */
1147 iflp->default_bw =
1148 ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1149 * TE_MEGA_BIT / TE_BYTE);
1150
1151 /* Set Max, Reservable and Unreserved Bandwidth */
1152 iflp->max_bw = iflp->default_bw;
1153 iflp->max_rsv_bw = iflp->default_bw;
1154 for (i = 0; i < MAX_CLASS_TYPE; i++)
1155 iflp->unrsv_bw[i] = iflp->default_bw;
1156
1157 /* Update Link parameters status */
1158 iflp->lp_status = LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
1159
1160 /* Set TE metric equal to standard metric only if it is set */
1161 if (ifp->metric != 0) {
1162 iflp->te_metric = ifp->metric;
1163 iflp->lp_status |= LP_TE_METRIC;
1164 }
1165
1166 /* Finally attach newly created Link Parameters */
1167 ifp->link_params = iflp;
1168
1169 return iflp;
1170 }
1171
1172 struct if_link_params *if_link_params_init(struct interface *ifp)
1173 {
1174 struct if_link_params *iflp = if_link_params_get(ifp);
1175
1176 if (iflp)
1177 return iflp;
1178
1179 iflp = XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params));
1180
1181 admin_group_init(&iflp->ext_admin_grp);
1182
1183 ifp->link_params = iflp;
1184
1185 return iflp;
1186 }
1187
1188 void if_link_params_free(struct interface *ifp)
1189 {
1190 if (!ifp->link_params)
1191 return;
1192
1193 admin_group_term(&ifp->link_params->ext_admin_grp);
1194 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1195 }
1196
1197 /* ----------- CLI commands ----------- */
1198
1199 /* Guess the VRF of an interface. */
1200 static int vrfname_by_ifname(const char *ifname, const char **vrfname)
1201 {
1202 struct vrf *vrf;
1203 struct interface *ifp;
1204 int count = 0;
1205
1206 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1207 FOR_ALL_INTERFACES (vrf, ifp) {
1208 if (strmatch(ifp->name, ifname)) {
1209 *vrfname = vrf->name;
1210 count++;
1211 }
1212 }
1213 }
1214
1215 return count;
1216 }
1217
1218 /*
1219 * XPath: /frr-interface:lib/interface
1220 */
1221 DEFPY_YANG_NOSH (interface,
1222 interface_cmd,
1223 "interface IFNAME [vrf NAME$vrf_name]",
1224 "Select an interface to configure\n"
1225 "Interface's name\n"
1226 VRF_CMD_HELP_STR)
1227 {
1228 char xpath_list[XPATH_MAXLEN];
1229 struct interface *ifp;
1230 struct vrf *vrf;
1231 int ret, count;
1232
1233 if (vrf_is_backend_netns()) {
1234 /*
1235 * For backward compatibility, if the VRF name is not specified
1236 * and there is exactly one interface with this name in the
1237 * system, use its VRF. Otherwise fallback to the default VRF.
1238 */
1239 if (!vrf_name) {
1240 count = vrfname_by_ifname(ifname, &vrf_name);
1241 if (count != 1)
1242 vrf_name = VRF_DEFAULT_NAME;
1243 }
1244
1245 snprintf(xpath_list, XPATH_MAXLEN,
1246 "/frr-interface:lib/interface[name='%s:%s']", vrf_name,
1247 ifname);
1248 } else {
1249 snprintf(xpath_list, XPATH_MAXLEN,
1250 "/frr-interface:lib/interface[name='%s']", ifname);
1251 }
1252
1253 nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, NULL);
1254 ret = nb_cli_apply_changes_clear_pending(vty, "%s", xpath_list);
1255 if (ret == CMD_SUCCESS) {
1256 VTY_PUSH_XPATH(INTERFACE_NODE, xpath_list);
1257
1258 /*
1259 * For backward compatibility with old commands we still need
1260 * to use the qobj infrastructure. This can be removed once
1261 * all interface-level commands are converted to the new
1262 * northbound model.
1263 */
1264 if (vrf_is_backend_netns()) {
1265 vrf = vrf_lookup_by_name(vrf_name);
1266 if (vrf)
1267 ifp = if_lookup_by_name_vrf(ifname, vrf);
1268 else
1269 ifp = NULL;
1270 } else {
1271 ifp = if_lookup_by_name_all_vrf(ifname);
1272 }
1273 if (ifp)
1274 VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp);
1275 }
1276
1277 return ret;
1278 }
1279
1280 DEFPY_YANG (no_interface,
1281 no_interface_cmd,
1282 "no interface IFNAME [vrf NAME$vrf_name]",
1283 NO_STR
1284 "Delete a pseudo interface's configuration\n"
1285 "Interface's name\n"
1286 VRF_CMD_HELP_STR)
1287 {
1288 char xpath_list[XPATH_MAXLEN];
1289 int count;
1290
1291 if (vrf_is_backend_netns()) {
1292 /*
1293 * For backward compatibility, if the VRF name is not specified
1294 * and there is exactly one interface with this name in the
1295 * system, use its VRF. Otherwise fallback to the default VRF.
1296 */
1297 if (!vrf_name) {
1298 count = vrfname_by_ifname(ifname, &vrf_name);
1299 if (count != 1)
1300 vrf_name = VRF_DEFAULT_NAME;
1301 }
1302
1303 snprintf(xpath_list, XPATH_MAXLEN,
1304 "/frr-interface:lib/interface[name='%s:%s']", vrf_name,
1305 ifname);
1306 } else {
1307 snprintf(xpath_list, XPATH_MAXLEN,
1308 "/frr-interface:lib/interface[name='%s']", ifname);
1309 }
1310
1311 nb_cli_enqueue_change(vty, ".", NB_OP_DESTROY, NULL);
1312
1313 return nb_cli_apply_changes(vty, "%s", xpath_list);
1314 }
1315
1316 static void netns_ifname_split(const char *xpath, char *ifname, char *vrfname)
1317 {
1318 char *delim;
1319 int len;
1320
1321 assert(vrf_is_backend_netns());
1322
1323 delim = strchr(xpath, ':');
1324 assert(delim);
1325
1326 len = delim - xpath;
1327 memcpy(vrfname, xpath, len);
1328 vrfname[len] = 0;
1329
1330 strlcpy(ifname, delim + 1, XPATH_MAXLEN);
1331 }
1332
1333 static void cli_show_interface(struct vty *vty, const struct lyd_node *dnode,
1334 bool show_defaults)
1335 {
1336 vty_out(vty, "!\n");
1337
1338 if (vrf_is_backend_netns()) {
1339 char ifname[XPATH_MAXLEN];
1340 char vrfname[XPATH_MAXLEN];
1341
1342 netns_ifname_split(yang_dnode_get_string(dnode, "./name"),
1343 ifname, vrfname);
1344
1345 vty_out(vty, "interface %s", ifname);
1346 if (!strmatch(vrfname, VRF_DEFAULT_NAME))
1347 vty_out(vty, " vrf %s", vrfname);
1348 } else {
1349 const char *ifname = yang_dnode_get_string(dnode, "./name");
1350
1351 vty_out(vty, "interface %s", ifname);
1352 }
1353
1354 vty_out(vty, "\n");
1355 }
1356
1357 static void cli_show_interface_end(struct vty *vty,
1358 const struct lyd_node *dnode)
1359 {
1360 vty_out(vty, "exit\n");
1361 }
1362
1363 void if_vty_config_start(struct vty *vty, struct interface *ifp)
1364 {
1365 vty_frame(vty, "!\n");
1366 vty_frame(vty, "interface %s", ifp->name);
1367
1368 if (vrf_is_backend_netns() && strcmp(ifp->vrf->name, VRF_DEFAULT_NAME))
1369 vty_frame(vty, " vrf %s", ifp->vrf->name);
1370
1371 vty_frame(vty, "\n");
1372 }
1373
1374 void if_vty_config_end(struct vty *vty)
1375 {
1376 vty_endframe(vty, "exit\n!\n");
1377 }
1378
1379 /*
1380 * XPath: /frr-interface:lib/interface/description
1381 */
1382 DEFPY_YANG (interface_desc,
1383 interface_desc_cmd,
1384 "description LINE...",
1385 "Interface specific description\n"
1386 "Characters describing this interface\n")
1387 {
1388 char *desc;
1389 int ret;
1390
1391 desc = argv_concat(argv, argc, 1);
1392 nb_cli_enqueue_change(vty, "./description", NB_OP_MODIFY, desc);
1393 ret = nb_cli_apply_changes(vty, NULL);
1394 XFREE(MTYPE_TMP, desc);
1395
1396 return ret;
1397 }
1398
1399 DEFPY_YANG (no_interface_desc,
1400 no_interface_desc_cmd,
1401 "no description",
1402 NO_STR
1403 "Interface specific description\n")
1404 {
1405 nb_cli_enqueue_change(vty, "./description", NB_OP_DESTROY, NULL);
1406
1407 return nb_cli_apply_changes(vty, NULL);
1408 }
1409
1410 static void cli_show_interface_desc(struct vty *vty,
1411 const struct lyd_node *dnode,
1412 bool show_defaults)
1413 {
1414 vty_out(vty, " description %s\n", yang_dnode_get_string(dnode, NULL));
1415 }
1416
1417 /* Interface autocomplete. */
1418 static void if_autocomplete(vector comps, struct cmd_token *token)
1419 {
1420 struct interface *ifp;
1421 struct vrf *vrf;
1422
1423 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1424 FOR_ALL_INTERFACES (vrf, ifp) {
1425 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name));
1426 }
1427 }
1428 }
1429
1430 static const struct cmd_variable_handler if_var_handlers[] = {
1431 {/* "interface NAME" */
1432 .varname = "interface",
1433 .completions = if_autocomplete},
1434 {.tokenname = "IFNAME", .completions = if_autocomplete},
1435 {.tokenname = "INTERFACE", .completions = if_autocomplete},
1436 {.completions = NULL}};
1437
1438 static struct cmd_node interface_node = {
1439 .name = "interface",
1440 .node = INTERFACE_NODE,
1441 .parent_node = CONFIG_NODE,
1442 .prompt = "%s(config-if)# ",
1443 };
1444
1445 static int if_config_write_single(const struct lyd_node *dnode, void *arg)
1446 {
1447 nb_cli_show_dnode_cmds(arg, dnode, false);
1448
1449 return YANG_ITER_CONTINUE;
1450 }
1451
1452 static int if_nb_config_write(struct vty *vty)
1453 {
1454 yang_dnode_iterate(if_config_write_single, vty, running_config->dnode,
1455 "/frr-interface:lib/interface");
1456 return 1;
1457 }
1458
1459 void if_cmd_init(int (*config_write)(struct vty *))
1460 {
1461 cmd_variable_handler_register(if_var_handlers);
1462
1463 interface_node.config_write = config_write;
1464 install_node(&interface_node);
1465
1466 install_element(CONFIG_NODE, &interface_cmd);
1467 install_element(CONFIG_NODE, &no_interface_cmd);
1468
1469 install_default(INTERFACE_NODE);
1470 install_element(INTERFACE_NODE, &interface_desc_cmd);
1471 install_element(INTERFACE_NODE, &no_interface_desc_cmd);
1472 }
1473
1474 void if_cmd_init_default(void)
1475 {
1476 if_cmd_init(if_nb_config_write);
1477 }
1478
1479 void if_zapi_callbacks(int (*create)(struct interface *ifp),
1480 int (*up)(struct interface *ifp),
1481 int (*down)(struct interface *ifp),
1482 int (*destroy)(struct interface *ifp))
1483 {
1484 ifp_master.create_hook = create;
1485 ifp_master.up_hook = up;
1486 ifp_master.down_hook = down;
1487 ifp_master.destroy_hook = destroy;
1488 }
1489
1490 /* ------- Northbound callbacks ------- */
1491
1492 /*
1493 * XPath: /frr-interface:lib/interface
1494 */
1495 static int lib_interface_create(struct nb_cb_create_args *args)
1496 {
1497 const char *ifname;
1498 struct interface *ifp;
1499
1500 ifname = yang_dnode_get_string(args->dnode, "./name");
1501
1502 switch (args->event) {
1503 case NB_EV_VALIDATE:
1504 if (vrf_is_backend_netns()) {
1505 char ifname_ns[XPATH_MAXLEN];
1506 char vrfname_ns[XPATH_MAXLEN];
1507
1508 netns_ifname_split(ifname, ifname_ns, vrfname_ns);
1509
1510 if (strlen(ifname_ns) > 16) {
1511 snprintf(
1512 args->errmsg, args->errmsg_len,
1513 "Maximum interface name length is 16 characters");
1514 return NB_ERR_VALIDATION;
1515 }
1516 if (strlen(vrfname_ns) > 36) {
1517 snprintf(
1518 args->errmsg, args->errmsg_len,
1519 "Maximum VRF name length is 36 characters");
1520 return NB_ERR_VALIDATION;
1521 }
1522 } else {
1523 if (strlen(ifname) > 16) {
1524 snprintf(
1525 args->errmsg, args->errmsg_len,
1526 "Maximum interface name length is 16 characters");
1527 return NB_ERR_VALIDATION;
1528 }
1529 }
1530 break;
1531 case NB_EV_PREPARE:
1532 case NB_EV_ABORT:
1533 break;
1534 case NB_EV_APPLY:
1535 if (vrf_is_backend_netns()) {
1536 char ifname_ns[XPATH_MAXLEN];
1537 char vrfname_ns[XPATH_MAXLEN];
1538
1539 netns_ifname_split(ifname, ifname_ns, vrfname_ns);
1540
1541 ifp = if_get_by_name(ifname_ns, VRF_UNKNOWN,
1542 vrfname_ns);
1543 } else {
1544 ifp = if_get_by_name(ifname, VRF_UNKNOWN,
1545 VRF_DEFAULT_NAME);
1546 }
1547
1548 ifp->configured = true;
1549 nb_running_set_entry(args->dnode, ifp);
1550 break;
1551 }
1552
1553 return NB_OK;
1554 }
1555
1556 static int lib_interface_destroy(struct nb_cb_destroy_args *args)
1557 {
1558 struct interface *ifp;
1559 struct vrf *vrf;
1560
1561 switch (args->event) {
1562 case NB_EV_VALIDATE:
1563 ifp = nb_running_get_entry(args->dnode, NULL, true);
1564 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1565 snprintf(args->errmsg, args->errmsg_len,
1566 "only inactive interfaces can be deleted");
1567 return NB_ERR_VALIDATION;
1568 }
1569 break;
1570 case NB_EV_PREPARE:
1571 case NB_EV_ABORT:
1572 break;
1573 case NB_EV_APPLY:
1574 ifp = nb_running_unset_entry(args->dnode);
1575 vrf = ifp->vrf;
1576
1577 ifp->configured = false;
1578 if_delete(&ifp);
1579
1580 if (!vrf_is_enabled(vrf))
1581 vrf_delete(vrf);
1582 break;
1583 }
1584
1585 return NB_OK;
1586 }
1587
1588 /*
1589 * XPath: /frr-interface:lib/interface
1590 */
1591 static const void *lib_interface_get_next(struct nb_cb_get_next_args *args)
1592 {
1593 struct vrf *vrf;
1594 struct interface *pif = (struct interface *)args->list_entry;
1595
1596 if (args->list_entry == NULL) {
1597 vrf = RB_MIN(vrf_name_head, &vrfs_by_name);
1598 assert(vrf);
1599 pif = RB_MIN(if_name_head, &vrf->ifaces_by_name);
1600 } else {
1601 vrf = pif->vrf;
1602 pif = RB_NEXT(if_name_head, pif);
1603 /* if no more interfaces, switch to next vrf */
1604 while (pif == NULL) {
1605 vrf = RB_NEXT(vrf_name_head, vrf);
1606 if (!vrf)
1607 return NULL;
1608 pif = RB_MIN(if_name_head, &vrf->ifaces_by_name);
1609 }
1610 }
1611
1612 return pif;
1613 }
1614
1615 static int lib_interface_get_keys(struct nb_cb_get_keys_args *args)
1616 {
1617 const struct interface *ifp = args->list_entry;
1618
1619 args->keys->num = 1;
1620
1621 if (vrf_is_backend_netns())
1622 snprintf(args->keys->key[0], sizeof(args->keys->key[0]),
1623 "%s:%s", ifp->vrf->name, ifp->name);
1624 else
1625 snprintf(args->keys->key[0], sizeof(args->keys->key[0]), "%s",
1626 ifp->name);
1627
1628 return NB_OK;
1629 }
1630
1631 static const void *
1632 lib_interface_lookup_entry(struct nb_cb_lookup_entry_args *args)
1633 {
1634 if (vrf_is_backend_netns()) {
1635 char ifname[XPATH_MAXLEN];
1636 char vrfname[XPATH_MAXLEN];
1637 struct vrf *vrf;
1638
1639 netns_ifname_split(args->keys->key[0], ifname, vrfname);
1640
1641 vrf = vrf_lookup_by_name(vrfname);
1642
1643 return vrf ? if_lookup_by_name(ifname, vrf->vrf_id) : NULL;
1644 } else {
1645 return if_lookup_by_name_all_vrf(args->keys->key[0]);
1646 }
1647 }
1648
1649 /*
1650 * XPath: /frr-interface:lib/interface/description
1651 */
1652 static int lib_interface_description_modify(struct nb_cb_modify_args *args)
1653 {
1654 struct interface *ifp;
1655 const char *description;
1656
1657 if (args->event != NB_EV_APPLY)
1658 return NB_OK;
1659
1660 ifp = nb_running_get_entry(args->dnode, NULL, true);
1661 XFREE(MTYPE_IFDESC, ifp->desc);
1662 description = yang_dnode_get_string(args->dnode, NULL);
1663 ifp->desc = XSTRDUP(MTYPE_IFDESC, description);
1664
1665 return NB_OK;
1666 }
1667
1668 static int lib_interface_description_destroy(struct nb_cb_destroy_args *args)
1669 {
1670 struct interface *ifp;
1671
1672 if (args->event != NB_EV_APPLY)
1673 return NB_OK;
1674
1675 ifp = nb_running_get_entry(args->dnode, NULL, true);
1676 XFREE(MTYPE_IFDESC, ifp->desc);
1677
1678 return NB_OK;
1679 }
1680
1681 /*
1682 * XPath: /frr-interface:lib/interface/vrf
1683 */
1684 static struct yang_data *
1685 lib_interface_vrf_get_elem(struct nb_cb_get_elem_args *args)
1686 {
1687 const struct interface *ifp = args->list_entry;
1688
1689 return yang_data_new_string(args->xpath, ifp->vrf->name);
1690 }
1691
1692 /*
1693 * XPath: /frr-interface:lib/interface/state/if-index
1694 */
1695 static struct yang_data *
1696 lib_interface_state_if_index_get_elem(struct nb_cb_get_elem_args *args)
1697 {
1698 const struct interface *ifp = args->list_entry;
1699
1700 return yang_data_new_int32(args->xpath, ifp->ifindex);
1701 }
1702
1703 /*
1704 * XPath: /frr-interface:lib/interface/state/mtu
1705 */
1706 static struct yang_data *
1707 lib_interface_state_mtu_get_elem(struct nb_cb_get_elem_args *args)
1708 {
1709 const struct interface *ifp = args->list_entry;
1710
1711 return yang_data_new_uint16(args->xpath, ifp->mtu);
1712 }
1713
1714 /*
1715 * XPath: /frr-interface:lib/interface/state/mtu6
1716 */
1717 static struct yang_data *
1718 lib_interface_state_mtu6_get_elem(struct nb_cb_get_elem_args *args)
1719 {
1720 const struct interface *ifp = args->list_entry;
1721
1722 return yang_data_new_uint32(args->xpath, ifp->mtu6);
1723 }
1724
1725 /*
1726 * XPath: /frr-interface:lib/interface/state/speed
1727 */
1728 static struct yang_data *
1729 lib_interface_state_speed_get_elem(struct nb_cb_get_elem_args *args)
1730 {
1731 const struct interface *ifp = args->list_entry;
1732
1733 return yang_data_new_uint32(args->xpath, ifp->speed);
1734 }
1735
1736 /*
1737 * XPath: /frr-interface:lib/interface/state/metric
1738 */
1739 static struct yang_data *
1740 lib_interface_state_metric_get_elem(struct nb_cb_get_elem_args *args)
1741 {
1742 const struct interface *ifp = args->list_entry;
1743
1744 return yang_data_new_uint32(args->xpath, ifp->metric);
1745 }
1746
1747 /*
1748 * XPath: /frr-interface:lib/interface/state/flags
1749 */
1750 static struct yang_data *
1751 lib_interface_state_flags_get_elem(struct nb_cb_get_elem_args *args)
1752 {
1753 /* TODO: implement me. */
1754 return NULL;
1755 }
1756
1757 /*
1758 * XPath: /frr-interface:lib/interface/state/type
1759 */
1760 static struct yang_data *
1761 lib_interface_state_type_get_elem(struct nb_cb_get_elem_args *args)
1762 {
1763 /* TODO: implement me. */
1764 return NULL;
1765 }
1766
1767 /*
1768 * XPath: /frr-interface:lib/interface/state/phy-address
1769 */
1770 static struct yang_data *
1771 lib_interface_state_phy_address_get_elem(struct nb_cb_get_elem_args *args)
1772 {
1773 const struct interface *ifp = args->list_entry;
1774 struct ethaddr macaddr;
1775
1776 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
1777
1778 return yang_data_new_mac(args->xpath, &macaddr);
1779 }
1780
1781 /* clang-format off */
1782 const struct frr_yang_module_info frr_interface_info = {
1783 .name = "frr-interface",
1784 .nodes = {
1785 {
1786 .xpath = "/frr-interface:lib/interface",
1787 .cbs = {
1788 .create = lib_interface_create,
1789 .destroy = lib_interface_destroy,
1790 .cli_show = cli_show_interface,
1791 .cli_show_end = cli_show_interface_end,
1792 .get_next = lib_interface_get_next,
1793 .get_keys = lib_interface_get_keys,
1794 .lookup_entry = lib_interface_lookup_entry,
1795 },
1796 },
1797 {
1798 .xpath = "/frr-interface:lib/interface/description",
1799 .cbs = {
1800 .modify = lib_interface_description_modify,
1801 .destroy = lib_interface_description_destroy,
1802 .cli_show = cli_show_interface_desc,
1803 },
1804 },
1805 {
1806 .xpath = "/frr-interface:lib/interface/vrf",
1807 .cbs = {
1808 .get_elem = lib_interface_vrf_get_elem,
1809 }
1810 },
1811 {
1812 .xpath = "/frr-interface:lib/interface/state/if-index",
1813 .cbs = {
1814 .get_elem = lib_interface_state_if_index_get_elem,
1815 }
1816 },
1817 {
1818 .xpath = "/frr-interface:lib/interface/state/mtu",
1819 .cbs = {
1820 .get_elem = lib_interface_state_mtu_get_elem,
1821 }
1822 },
1823 {
1824 .xpath = "/frr-interface:lib/interface/state/mtu6",
1825 .cbs = {
1826 .get_elem = lib_interface_state_mtu6_get_elem,
1827 }
1828 },
1829 {
1830 .xpath = "/frr-interface:lib/interface/state/speed",
1831 .cbs = {
1832 .get_elem = lib_interface_state_speed_get_elem,
1833 }
1834 },
1835 {
1836 .xpath = "/frr-interface:lib/interface/state/metric",
1837 .cbs = {
1838 .get_elem = lib_interface_state_metric_get_elem,
1839 }
1840 },
1841 {
1842 .xpath = "/frr-interface:lib/interface/state/flags",
1843 .cbs = {
1844 .get_elem = lib_interface_state_flags_get_elem,
1845 }
1846 },
1847 {
1848 .xpath = "/frr-interface:lib/interface/state/type",
1849 .cbs = {
1850 .get_elem = lib_interface_state_type_get_elem,
1851 }
1852 },
1853 {
1854 .xpath = "/frr-interface:lib/interface/state/phy-address",
1855 .cbs = {
1856 .get_elem = lib_interface_state_phy_address_get_elem,
1857 }
1858 },
1859 {
1860 .xpath = NULL,
1861 },
1862 }
1863 };