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