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