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