]> git.proxmox.com Git - mirror_frr.git/blob - lib/if.c
Merge pull request #5163 from ton31337/fix/do_not_reconnect_if_prefix_overflow_7.1
[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 /* Get interface by name if given name interface doesn't exist create
393 one. */
394 struct interface *if_get_by_name(const char *name, vrf_id_t vrf_id)
395 {
396 struct interface *ifp;
397
398 switch (vrf_get_backend()) {
399 case VRF_BACKEND_UNKNOWN:
400 case VRF_BACKEND_NETNS:
401 ifp = if_lookup_by_name(name, vrf_id);
402 if (ifp)
403 return ifp;
404 return if_create(name, vrf_id);
405 case VRF_BACKEND_VRF_LITE:
406 ifp = if_lookup_by_name_all_vrf(name);
407 if (ifp) {
408 if (ifp->vrf_id == vrf_id)
409 return ifp;
410 /* If it came from the kernel or by way of zclient,
411 * believe it and update the ifp accordingly.
412 */
413 if_update_to_new_vrf(ifp, vrf_id);
414 return ifp;
415 }
416 return if_create(name, vrf_id);
417 }
418
419 return NULL;
420 }
421
422 void if_set_index(struct interface *ifp, ifindex_t ifindex)
423 {
424 struct vrf *vrf;
425
426 vrf = vrf_lookup_by_id(ifp->vrf_id);
427 assert(vrf);
428
429 if (ifp->ifindex == ifindex)
430 return;
431
432 if (ifp->ifindex != IFINDEX_INTERNAL)
433 IFINDEX_RB_REMOVE(vrf, ifp)
434
435 ifp->ifindex = ifindex;
436
437 if (ifp->ifindex != IFINDEX_INTERNAL)
438 IFINDEX_RB_INSERT(vrf, ifp)
439 }
440
441 /* Does interface up ? */
442 int if_is_up(const struct interface *ifp)
443 {
444 return ifp->flags & IFF_UP;
445 }
446
447 /* Is interface running? */
448 int if_is_running(const struct interface *ifp)
449 {
450 return ifp->flags & IFF_RUNNING;
451 }
452
453 /* Is the interface operative, eg. either UP & RUNNING
454 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
455 if ptm checking is enabled, then ptm check has passed */
456 int if_is_operative(const struct interface *ifp)
457 {
458 return ((ifp->flags & IFF_UP)
459 && (((ifp->flags & IFF_RUNNING)
460 && (ifp->ptm_status || !ifp->ptm_enable))
461 || !CHECK_FLAG(ifp->status,
462 ZEBRA_INTERFACE_LINKDETECTION)));
463 }
464
465 /* Is the interface operative, eg. either UP & RUNNING
466 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
467 int if_is_no_ptm_operative(const struct interface *ifp)
468 {
469 return ((ifp->flags & IFF_UP)
470 && ((ifp->flags & IFF_RUNNING)
471 || !CHECK_FLAG(ifp->status,
472 ZEBRA_INTERFACE_LINKDETECTION)));
473 }
474
475 /* Is this loopback interface ? */
476 int if_is_loopback(const struct interface *ifp)
477 {
478 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
479 * but Y on platform N?
480 */
481 return (ifp->flags & (IFF_LOOPBACK | IFF_NOXMIT | IFF_VIRTUAL));
482 }
483
484 /* Check interface is VRF */
485 int if_is_vrf(const struct interface *ifp)
486 {
487 return CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
488 }
489
490 bool if_is_loopback_or_vrf(const struct interface *ifp)
491 {
492 if (if_is_loopback(ifp) || if_is_vrf(ifp))
493 return true;
494
495 return false;
496 }
497
498 /* Does this interface support broadcast ? */
499 int if_is_broadcast(const struct interface *ifp)
500 {
501 return ifp->flags & IFF_BROADCAST;
502 }
503
504 /* Does this interface support broadcast ? */
505 int if_is_pointopoint(const struct interface *ifp)
506 {
507 return ifp->flags & IFF_POINTOPOINT;
508 }
509
510 /* Does this interface support multicast ? */
511 int if_is_multicast(const struct interface *ifp)
512 {
513 return ifp->flags & IFF_MULTICAST;
514 }
515
516 /* Printout flag information into log */
517 const char *if_flag_dump(unsigned long flag)
518 {
519 int separator = 0;
520 static char logbuf[BUFSIZ];
521
522 #define IFF_OUT_LOG(X, STR) \
523 if (flag & (X)) { \
524 if (separator) \
525 strlcat(logbuf, ",", sizeof(logbuf)); \
526 else \
527 separator = 1; \
528 strlcat(logbuf, STR, sizeof(logbuf)); \
529 }
530
531 strlcpy(logbuf, "<", BUFSIZ);
532 IFF_OUT_LOG(IFF_UP, "UP");
533 IFF_OUT_LOG(IFF_BROADCAST, "BROADCAST");
534 IFF_OUT_LOG(IFF_DEBUG, "DEBUG");
535 IFF_OUT_LOG(IFF_LOOPBACK, "LOOPBACK");
536 IFF_OUT_LOG(IFF_POINTOPOINT, "POINTOPOINT");
537 IFF_OUT_LOG(IFF_NOTRAILERS, "NOTRAILERS");
538 IFF_OUT_LOG(IFF_RUNNING, "RUNNING");
539 IFF_OUT_LOG(IFF_NOARP, "NOARP");
540 IFF_OUT_LOG(IFF_PROMISC, "PROMISC");
541 IFF_OUT_LOG(IFF_ALLMULTI, "ALLMULTI");
542 IFF_OUT_LOG(IFF_OACTIVE, "OACTIVE");
543 IFF_OUT_LOG(IFF_SIMPLEX, "SIMPLEX");
544 IFF_OUT_LOG(IFF_LINK0, "LINK0");
545 IFF_OUT_LOG(IFF_LINK1, "LINK1");
546 IFF_OUT_LOG(IFF_LINK2, "LINK2");
547 IFF_OUT_LOG(IFF_MULTICAST, "MULTICAST");
548 IFF_OUT_LOG(IFF_NOXMIT, "NOXMIT");
549 IFF_OUT_LOG(IFF_NORTEXCH, "NORTEXCH");
550 IFF_OUT_LOG(IFF_VIRTUAL, "VIRTUAL");
551 IFF_OUT_LOG(IFF_IPV4, "IPv4");
552 IFF_OUT_LOG(IFF_IPV6, "IPv6");
553
554 strlcat(logbuf, ">", sizeof(logbuf));
555
556 return logbuf;
557 #undef IFF_OUT_LOG
558 }
559
560 /* For debugging */
561 static void if_dump(const struct interface *ifp)
562 {
563 struct listnode *node;
564 struct connected *c __attribute__((unused));
565
566 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, c))
567 zlog_info(
568 "Interface %s vrf %u index %d metric %d mtu %d "
569 "mtu6 %d %s",
570 ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric,
571 ifp->mtu, ifp->mtu6, if_flag_dump(ifp->flags));
572 }
573
574 /* Interface printing for all interface. */
575 void if_dump_all(void)
576 {
577 struct vrf *vrf;
578 void *ifp;
579
580 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
581 FOR_ALL_INTERFACES (vrf, ifp)
582 if_dump(ifp);
583 }
584
585 #ifdef SUNOS_5
586 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
587 * a seperate struct interface for each logical interface, so config
588 * file may be full of 'interface fooX:Y'. Solaris however does not
589 * expose logical interfaces via PF_ROUTE, so trying to track logical
590 * interfaces can be fruitless, for that reason Quagga only tracks
591 * the primary IP interface.
592 *
593 * We try accomodate SUNWzebra by:
594 * - looking up the interface name, to see whether it exists, if so
595 * its useable
596 * - for protocol daemons, this could only because zebra told us of
597 * the interface
598 * - for zebra, only because it learnt from kernel
599 * - if not:
600 * - search the name to see if it contains a sub-ipif / logical interface
601 * seperator, the ':' char. If it does:
602 * - text up to that char must be the primary name - get that name.
603 * if not:
604 * - no idea, just get the name in its entirety.
605 */
606 static struct interface *if_sunwzebra_get(const char *name, vrf_id_t vrf_id)
607 {
608 struct interface *ifp;
609 char *cp;
610
611 if ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
612 return ifp;
613
614 /* hunt the primary interface name... */
615 cp = strchr(name, ':');
616 if (cp)
617 *cp = '\0';
618
619 return if_get_by_name(name, vrf_id);
620 }
621 #endif /* SUNOS_5 */
622
623 #if 0
624 /* For debug purpose. */
625 DEFUN (show_address,
626 show_address_cmd,
627 "show address [vrf NAME]",
628 SHOW_STR
629 "address\n"
630 VRF_CMD_HELP_STR)
631 {
632 int idx_vrf = 3;
633 struct listnode *node;
634 struct interface *ifp;
635 struct connected *ifc;
636 struct prefix *p;
637 vrf_id_t vrf_id = VRF_DEFAULT;
638
639 if (argc > 2)
640 VRF_GET_ID (vrf_id, argv[idx_vrf]->arg);
641
642 FOR_ALL_INTERFACES (vrf, ifp)
643 {
644 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
645 {
646 p = ifc->address;
647
648 if (p->family == AF_INET)
649 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
650 }
651 }
652 return CMD_SUCCESS;
653 }
654
655 DEFUN (show_address_vrf_all,
656 show_address_vrf_all_cmd,
657 "show address vrf all",
658 SHOW_STR
659 "address\n"
660 VRF_ALL_CMD_HELP_STR)
661 {
662 struct vrf *vrf;
663 struct listnode *node;
664 struct interface *ifp;
665 struct connected *ifc;
666 struct prefix *p;
667
668 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
669 {
670 if (RB_EMPTY (if_name_head, &vrf->ifaces_by_name))
671 continue;
672
673 vty_out (vty, "\nVRF %u\n\n", vrf->vrf_id);
674
675 FOR_ALL_INTERFACES (vrf, ifp)
676 {
677 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
678 {
679 p = ifc->address;
680
681 if (p->family == AF_INET)
682 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
683 }
684 }
685 }
686 return CMD_SUCCESS;
687 }
688 #endif
689
690 /* Allocate connected structure. */
691 struct connected *connected_new(void)
692 {
693 return XCALLOC(MTYPE_CONNECTED, sizeof(struct connected));
694 }
695
696 /* Allocate nbr connected structure. */
697 struct nbr_connected *nbr_connected_new(void)
698 {
699 return XCALLOC(MTYPE_NBR_CONNECTED, sizeof(struct nbr_connected));
700 }
701
702 /* Free connected structure. */
703 void connected_free(struct connected *connected)
704 {
705 if (connected->address)
706 prefix_free(connected->address);
707
708 if (connected->destination)
709 prefix_free(connected->destination);
710
711 XFREE(MTYPE_CONNECTED_LABEL, connected->label);
712
713 XFREE(MTYPE_CONNECTED, connected);
714 }
715
716 /* Free nbr connected structure. */
717 void nbr_connected_free(struct nbr_connected *connected)
718 {
719 if (connected->address)
720 prefix_free(connected->address);
721
722 XFREE(MTYPE_NBR_CONNECTED, connected);
723 }
724
725 /* If same interface nbr address already exists... */
726 struct nbr_connected *nbr_connected_check(struct interface *ifp,
727 struct prefix *p)
728 {
729 struct nbr_connected *ifc;
730 struct listnode *node;
731
732 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, ifc))
733 if (prefix_same(ifc->address, p))
734 return ifc;
735
736 return NULL;
737 }
738
739 /* Print if_addr structure. */
740 static void __attribute__((unused))
741 connected_log(struct connected *connected, char *str)
742 {
743 struct prefix *p;
744 struct interface *ifp;
745 char logbuf[BUFSIZ];
746 char buf[BUFSIZ];
747
748 ifp = connected->ifp;
749 p = connected->address;
750
751 snprintf(logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ", str,
752 ifp->name, ifp->vrf_id, prefix_family_str(p),
753 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
754
755 p = connected->destination;
756 if (p) {
757 strncat(logbuf, inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
758 BUFSIZ - strlen(logbuf));
759 }
760 zlog_info("%s", logbuf);
761 }
762
763 /* Print if_addr structure. */
764 static void __attribute__((unused))
765 nbr_connected_log(struct nbr_connected *connected, char *str)
766 {
767 struct prefix *p;
768 struct interface *ifp;
769 char logbuf[BUFSIZ];
770 char buf[BUFSIZ];
771
772 ifp = connected->ifp;
773 p = connected->address;
774
775 snprintf(logbuf, BUFSIZ, "%s interface %s %s %s/%d ", str, ifp->name,
776 prefix_family_str(p),
777 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
778
779 zlog_info("%s", logbuf);
780 }
781
782 /* If two connected address has same prefix return 1. */
783 static int connected_same_prefix(struct prefix *p1, struct prefix *p2)
784 {
785 if (p1->family == p2->family) {
786 if (p1->family == AF_INET
787 && IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
788 return 1;
789 if (p1->family == AF_INET6
790 && IPV6_ADDR_SAME(&p1->u.prefix6, &p2->u.prefix6))
791 return 1;
792 }
793 return 0;
794 }
795
796 struct connected *connected_lookup_prefix_exact(struct interface *ifp,
797 struct prefix *p)
798 {
799 struct listnode *node;
800 struct listnode *next;
801 struct connected *ifc;
802
803 for (node = listhead(ifp->connected); node; node = next) {
804 ifc = listgetdata(node);
805 next = node->next;
806
807 if (connected_same_prefix(ifc->address, p))
808 return ifc;
809 }
810 return NULL;
811 }
812
813 struct connected *connected_delete_by_prefix(struct interface *ifp,
814 struct prefix *p)
815 {
816 struct listnode *node;
817 struct listnode *next;
818 struct connected *ifc;
819
820 /* In case of same prefix come, replace it with new one. */
821 for (node = listhead(ifp->connected); node; node = next) {
822 ifc = listgetdata(node);
823 next = node->next;
824
825 if (connected_same_prefix(ifc->address, p)) {
826 listnode_delete(ifp->connected, ifc);
827 return ifc;
828 }
829 }
830 return NULL;
831 }
832
833 /* Find the address on our side that will be used when packets
834 are sent to dst. */
835 struct connected *connected_lookup_prefix(struct interface *ifp,
836 struct prefix *addr)
837 {
838 struct listnode *cnode;
839 struct connected *c;
840 struct connected *match;
841
842 match = NULL;
843
844 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
845 if (c->address && (c->address->family == addr->family)
846 && prefix_match(CONNECTED_PREFIX(c), addr)
847 && (!match
848 || (c->address->prefixlen > match->address->prefixlen)))
849 match = c;
850 }
851 return match;
852 }
853
854 struct connected *connected_add_by_prefix(struct interface *ifp,
855 struct prefix *p,
856 struct prefix *destination)
857 {
858 struct connected *ifc;
859
860 /* Allocate new connected address. */
861 ifc = connected_new();
862 ifc->ifp = ifp;
863
864 /* Fetch interface address */
865 ifc->address = prefix_new();
866 memcpy(ifc->address, p, sizeof(struct prefix));
867
868 /* Fetch dest address */
869 if (destination) {
870 ifc->destination = prefix_new();
871 memcpy(ifc->destination, destination, sizeof(struct prefix));
872 }
873
874 /* Add connected address to the interface. */
875 listnode_add(ifp->connected, ifc);
876 return ifc;
877 }
878
879 #if 0 /* this route_table of struct connected's is unused \
880 * however, it would be good to use a route_table rather than \
881 * a list.. \
882 */
883 /* Interface looking up by interface's address. */
884 /* Interface's IPv4 address reverse lookup table. */
885 struct route_table *ifaddr_ipv4_table;
886 /* struct route_table *ifaddr_ipv6_table; */
887
888 static void
889 ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
890 {
891 struct route_node *rn;
892 struct prefix_ipv4 p;
893
894 p.family = AF_INET;
895 p.prefixlen = IPV4_MAX_PREFIXLEN;
896 p.prefix = *ifaddr;
897
898 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
899 if (rn)
900 {
901 route_unlock_node (rn);
902 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
903 inet_ntoa (*ifaddr));
904 return;
905 }
906 rn->info = ifp;
907 }
908
909 static void
910 ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
911 {
912 struct route_node *rn;
913 struct prefix_ipv4 p;
914
915 p.family = AF_INET;
916 p.prefixlen = IPV4_MAX_PREFIXLEN;
917 p.prefix = *ifaddr;
918
919 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
920 if (! rn)
921 {
922 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
923 inet_ntoa (*ifaddr));
924 return;
925 }
926 rn->info = NULL;
927 route_unlock_node (rn);
928 route_unlock_node (rn);
929 }
930
931 /* Lookup interface by interface's IP address or interface index. */
932 static struct interface *
933 ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
934 {
935 struct prefix_ipv4 p;
936 struct route_node *rn;
937 struct interface *ifp;
938
939 if (addr)
940 {
941 p.family = AF_INET;
942 p.prefixlen = IPV4_MAX_PREFIXLEN;
943 p.prefix = *addr;
944
945 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
946 if (! rn)
947 return NULL;
948
949 ifp = rn->info;
950 route_unlock_node (rn);
951 return ifp;
952 }
953 else
954 return if_lookup_by_index(ifindex, VRF_DEFAULT);
955 }
956 #endif /* ifaddr_ipv4_table */
957
958 void if_terminate(struct vrf *vrf)
959 {
960 struct interface *ifp;
961
962 while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
963 ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name);
964
965 if (ifp->node) {
966 ifp->node->info = NULL;
967 route_unlock_node(ifp->node);
968 }
969 if_delete(ifp);
970 }
971 }
972
973 const char *if_link_type_str(enum zebra_link_type llt)
974 {
975 switch (llt) {
976 #define llts(T,S) case (T): return (S)
977 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
978 llts(ZEBRA_LLT_ETHER, "Ethernet");
979 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
980 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
981 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
982 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
983 llts(ZEBRA_LLT_ARCNET, "ARCnet");
984 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
985 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
986 llts(ZEBRA_LLT_ATM, "ATM");
987 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
988 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
989 llts(ZEBRA_LLT_EUI64, "EUI-64");
990 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
991 llts(ZEBRA_LLT_SLIP, "SLIP");
992 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
993 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
994 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
995 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
996 llts(ZEBRA_LLT_X25, "CCITT X.25");
997 llts(ZEBRA_LLT_PPP, "PPP");
998 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
999 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1000 llts(ZEBRA_LLT_LAPB, "LAPB");
1001 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1002 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1003 llts(ZEBRA_LLT_FRAD, "FRAD");
1004 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1005 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1006 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1007 llts(ZEBRA_LLT_FDDI, "FDDI");
1008 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1009 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1010 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1011 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1012 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1013 llts(ZEBRA_LLT_IRDA, "IrDA");
1014 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1015 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1016 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1017 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1018 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1019 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1020 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1021 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1022 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1023 default:
1024 flog_err(EC_LIB_DEVELOPMENT, "Unknown value %d", llt);
1025 return "Unknown type!";
1026 #undef llts
1027 }
1028 return NULL;
1029 }
1030
1031 struct if_link_params *if_link_params_get(struct interface *ifp)
1032 {
1033 int i;
1034
1035 if (ifp->link_params != NULL)
1036 return ifp->link_params;
1037
1038 struct if_link_params *iflp =
1039 XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params));
1040 if (iflp == NULL)
1041 return NULL;
1042
1043 /* Set TE metric equal to standard metric */
1044 iflp->te_metric = ifp->metric;
1045
1046 /* Compute default bandwidth based on interface */
1047 iflp->default_bw =
1048 ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1049 * TE_KILO_BIT / TE_BYTE);
1050
1051 /* Set Max, Reservable and Unreserved Bandwidth */
1052 iflp->max_bw = iflp->default_bw;
1053 iflp->max_rsv_bw = iflp->default_bw;
1054 for (i = 0; i < MAX_CLASS_TYPE; i++)
1055 iflp->unrsv_bw[i] = iflp->default_bw;
1056
1057 /* Update Link parameters status */
1058 iflp->lp_status =
1059 LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
1060
1061 /* Finally attach newly created Link Parameters */
1062 ifp->link_params = iflp;
1063
1064 return iflp;
1065 }
1066
1067 void if_link_params_free(struct interface *ifp)
1068 {
1069 if (ifp->link_params == NULL)
1070 return;
1071 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1072 ifp->link_params = NULL;
1073 }
1074
1075 /* ----------- CLI commands ----------- */
1076
1077 /*
1078 * XPath: /frr-interface:lib/interface
1079 */
1080 DEFPY_NOSH (interface,
1081 interface_cmd,
1082 "interface IFNAME [vrf NAME$vrfname]",
1083 "Select an interface to configure\n"
1084 "Interface's name\n"
1085 VRF_CMD_HELP_STR)
1086 {
1087 char xpath_list[XPATH_MAXLEN];
1088 vrf_id_t vrf_id;
1089 struct interface *ifp;
1090 int ret;
1091
1092 if (!vrfname)
1093 vrfname = VRF_DEFAULT_NAME;
1094
1095 /*
1096 * This command requires special handling to maintain backward
1097 * compatibility. If a VRF name is not specified, it means we're willing
1098 * to accept any interface with the given name on any VRF. If no
1099 * interface is found, then a new one should be created on the default
1100 * VRF.
1101 */
1102 VRF_GET_ID(vrf_id, vrfname, false);
1103 ifp = if_lookup_by_name_all_vrf(ifname);
1104 if (ifp && ifp->vrf_id != vrf_id) {
1105 struct vrf *vrf;
1106
1107 /*
1108 * Special case 1: a VRF name was specified, but the found
1109 * interface is associated to different VRF. Reject the command.
1110 */
1111 if (vrf_id != VRF_DEFAULT) {
1112 vty_out(vty, "%% interface %s not in %s vrf\n", ifname,
1113 vrfname);
1114 return CMD_WARNING_CONFIG_FAILED;
1115 }
1116
1117 /*
1118 * Special case 2: a VRF name was *not* specified, and the found
1119 * interface is associated to a VRF other than the default one.
1120 * Update vrf_id and vrfname to account for that.
1121 */
1122 vrf = vrf_lookup_by_id(ifp->vrf_id);
1123 assert(vrf);
1124 vrf_id = ifp->vrf_id;
1125 vrfname = vrf->name;
1126 }
1127
1128 snprintf(xpath_list, sizeof(xpath_list),
1129 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname,
1130 vrfname);
1131
1132 nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, NULL);
1133 ret = nb_cli_apply_changes(vty, xpath_list);
1134 if (ret == CMD_SUCCESS) {
1135 VTY_PUSH_XPATH(INTERFACE_NODE, xpath_list);
1136
1137 /*
1138 * For backward compatibility with old commands we still need
1139 * to use the qobj infrastructure. This can be removed once
1140 * all interface-level commands are converted to the new
1141 * northbound model.
1142 */
1143 ifp = if_lookup_by_name(ifname, vrf_id);
1144 if (ifp)
1145 VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp);
1146 }
1147
1148 return ret;
1149 }
1150
1151 DEFPY (no_interface,
1152 no_interface_cmd,
1153 "no interface IFNAME [vrf NAME$vrfname]",
1154 NO_STR
1155 "Delete a pseudo interface's configuration\n"
1156 "Interface's name\n"
1157 VRF_CMD_HELP_STR)
1158 {
1159 if (!vrfname)
1160 vrfname = VRF_DEFAULT_NAME;
1161
1162 nb_cli_enqueue_change(vty, ".", NB_OP_DESTROY, NULL);
1163
1164 return nb_cli_apply_changes(
1165 vty, "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1166 ifname, vrfname);
1167 }
1168
1169 static void cli_show_interface(struct vty *vty, struct lyd_node *dnode,
1170 bool show_defaults)
1171 {
1172 const char *vrf;
1173
1174 vrf = yang_dnode_get_string(dnode, "./vrf");
1175
1176 vty_out(vty, "!\n");
1177 vty_out(vty, "interface %s", yang_dnode_get_string(dnode, "./name"));
1178 if (!strmatch(vrf, VRF_DEFAULT_NAME))
1179 vty_out(vty, " vrf %s", vrf);
1180 vty_out(vty, "\n");
1181 }
1182
1183 /*
1184 * XPath: /frr-interface:lib/interface/description
1185 */
1186 DEFPY (interface_desc,
1187 interface_desc_cmd,
1188 "description LINE...",
1189 "Interface specific description\n"
1190 "Characters describing this interface\n")
1191 {
1192 char *desc;
1193 int ret;
1194
1195 desc = argv_concat(argv, argc, 1);
1196 nb_cli_enqueue_change(vty, "./description", NB_OP_MODIFY, desc);
1197 ret = nb_cli_apply_changes(vty, NULL);
1198 XFREE(MTYPE_TMP, desc);
1199
1200 return ret;
1201 }
1202
1203 DEFPY (no_interface_desc,
1204 no_interface_desc_cmd,
1205 "no description",
1206 NO_STR
1207 "Interface specific description\n")
1208 {
1209 nb_cli_enqueue_change(vty, "./description", NB_OP_DESTROY, NULL);
1210
1211 return nb_cli_apply_changes(vty, NULL);
1212 }
1213
1214 static void cli_show_interface_desc(struct vty *vty, struct lyd_node *dnode,
1215 bool show_defaults)
1216 {
1217 vty_out(vty, " description %s\n", yang_dnode_get_string(dnode, NULL));
1218 }
1219
1220 /* Interface autocomplete. */
1221 static void if_autocomplete(vector comps, struct cmd_token *token)
1222 {
1223 struct interface *ifp;
1224 struct vrf *vrf;
1225
1226 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1227 FOR_ALL_INTERFACES (vrf, ifp) {
1228 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name));
1229 }
1230 }
1231 }
1232
1233 static const struct cmd_variable_handler if_var_handlers[] = {
1234 {/* "interface NAME" */
1235 .varname = "interface",
1236 .completions = if_autocomplete},
1237 {.tokenname = "IFNAME", .completions = if_autocomplete},
1238 {.tokenname = "INTERFACE", .completions = if_autocomplete},
1239 {.completions = NULL}};
1240
1241 void if_cmd_init(void)
1242 {
1243 cmd_variable_handler_register(if_var_handlers);
1244
1245 install_element(CONFIG_NODE, &interface_cmd);
1246 install_element(CONFIG_NODE, &no_interface_cmd);
1247
1248 install_default(INTERFACE_NODE);
1249 install_element(INTERFACE_NODE, &interface_desc_cmd);
1250 install_element(INTERFACE_NODE, &no_interface_desc_cmd);
1251 }
1252
1253 /* ------- Northbound callbacks ------- */
1254
1255 /*
1256 * XPath: /frr-interface:lib/interface
1257 */
1258 static int lib_interface_create(enum nb_event event,
1259 const struct lyd_node *dnode,
1260 union nb_resource *resource)
1261 {
1262 const char *ifname;
1263 const char *vrfname;
1264 struct vrf *vrf;
1265 struct interface *ifp;
1266
1267 ifname = yang_dnode_get_string(dnode, "./name");
1268 vrfname = yang_dnode_get_string(dnode, "./vrf");
1269
1270 switch (event) {
1271 case NB_EV_VALIDATE:
1272 vrf = vrf_lookup_by_name(vrfname);
1273 if (!vrf) {
1274 zlog_warn("%s: VRF %s doesn't exist", __func__,
1275 vrfname);
1276 return NB_ERR_VALIDATION;
1277 }
1278 if (vrf->vrf_id == VRF_UNKNOWN) {
1279 zlog_warn("%s: VRF %s is not active", __func__,
1280 vrf->name);
1281 return NB_ERR_VALIDATION;
1282 }
1283
1284 /* if VRF is netns or not yet known - init for instance
1285 * then assumption is that passed config is exact
1286 * then the user intent was not to use an other iface
1287 */
1288 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE) {
1289 ifp = if_lookup_by_name_all_vrf(ifname);
1290 if (ifp && ifp->vrf_id != vrf->vrf_id) {
1291 zlog_warn(
1292 "%s: interface %s already exists in another VRF",
1293 __func__, ifp->name);
1294 return NB_ERR_VALIDATION;
1295 }
1296 }
1297 break;
1298 case NB_EV_PREPARE:
1299 case NB_EV_ABORT:
1300 break;
1301 case NB_EV_APPLY:
1302 vrf = vrf_lookup_by_name(vrfname);
1303 assert(vrf);
1304 #ifdef SUNOS_5
1305 ifp = if_sunwzebra_get(ifname, vrf->vrf_id);
1306 #else
1307 ifp = if_get_by_name(ifname, vrf->vrf_id);
1308 #endif /* SUNOS_5 */
1309 nb_running_set_entry(dnode, ifp);
1310 break;
1311 }
1312
1313 return NB_OK;
1314 }
1315
1316 static int lib_interface_destroy(enum nb_event event,
1317 const struct lyd_node *dnode)
1318 {
1319 struct interface *ifp;
1320
1321
1322 switch (event) {
1323 case NB_EV_VALIDATE:
1324 ifp = nb_running_get_entry(dnode, NULL, true);
1325 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1326 zlog_warn("%s: only inactive interfaces can be deleted",
1327 __func__);
1328 return NB_ERR_VALIDATION;
1329 }
1330 break;
1331 case NB_EV_PREPARE:
1332 case NB_EV_ABORT:
1333 break;
1334 case NB_EV_APPLY:
1335 ifp = nb_running_unset_entry(dnode);
1336 if_delete(ifp);
1337 break;
1338 }
1339
1340 return NB_OK;
1341 }
1342
1343 /*
1344 * XPath: /frr-interface:lib/interface/description
1345 */
1346 static int lib_interface_description_modify(enum nb_event event,
1347 const struct lyd_node *dnode,
1348 union nb_resource *resource)
1349 {
1350 struct interface *ifp;
1351 const char *description;
1352
1353 if (event != NB_EV_APPLY)
1354 return NB_OK;
1355
1356 ifp = nb_running_get_entry(dnode, NULL, true);
1357 XFREE(MTYPE_TMP, ifp->desc);
1358 description = yang_dnode_get_string(dnode, NULL);
1359 ifp->desc = XSTRDUP(MTYPE_TMP, description);
1360
1361 return NB_OK;
1362 }
1363
1364 static int lib_interface_description_destroy(enum nb_event event,
1365 const struct lyd_node *dnode)
1366 {
1367 struct interface *ifp;
1368
1369 if (event != NB_EV_APPLY)
1370 return NB_OK;
1371
1372 ifp = nb_running_get_entry(dnode, NULL, true);
1373 XFREE(MTYPE_TMP, ifp->desc);
1374
1375 return NB_OK;
1376 }
1377
1378 /* clang-format off */
1379 const struct frr_yang_module_info frr_interface_info = {
1380 .name = "frr-interface",
1381 .nodes = {
1382 {
1383 .xpath = "/frr-interface:lib/interface",
1384 .cbs.create = lib_interface_create,
1385 .cbs.destroy = lib_interface_destroy,
1386 .cbs.cli_show = cli_show_interface,
1387 },
1388 {
1389 .xpath = "/frr-interface:lib/interface/description",
1390 .cbs.modify = lib_interface_description_modify,
1391 .cbs.destroy = lib_interface_description_destroy,
1392 .cbs.cli_show = cli_show_interface_desc,
1393 },
1394 {
1395 .xpath = NULL,
1396 },
1397 }
1398 };