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