]> git.proxmox.com Git - mirror_frr.git/blob - lib/if.c
lib: Add const to _count() in new list APIs
[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 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 struct connected *connected_get_linklocal(struct interface *ifp)
908 {
909 struct listnode *n;
910 struct connected *c = NULL;
911
912 for (ALL_LIST_ELEMENTS_RO(ifp->connected, n, c)) {
913 if (c->address->family == AF_INET6
914 && IN6_IS_ADDR_LINKLOCAL(&c->address->u.prefix6))
915 break;
916 }
917 return c;
918 }
919
920 #if 0 /* this route_table of struct connected's is unused \
921 * however, it would be good to use a route_table rather than \
922 * a list.. \
923 */
924 /* Interface looking up by interface's address. */
925 /* Interface's IPv4 address reverse lookup table. */
926 struct route_table *ifaddr_ipv4_table;
927 /* struct route_table *ifaddr_ipv6_table; */
928
929 static void
930 ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
931 {
932 struct route_node *rn;
933 struct prefix_ipv4 p;
934
935 p.family = AF_INET;
936 p.prefixlen = IPV4_MAX_PREFIXLEN;
937 p.prefix = *ifaddr;
938
939 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
940 if (rn)
941 {
942 route_unlock_node (rn);
943 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
944 inet_ntoa (*ifaddr));
945 return;
946 }
947 rn->info = ifp;
948 }
949
950 static void
951 ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
952 {
953 struct route_node *rn;
954 struct prefix_ipv4 p;
955
956 p.family = AF_INET;
957 p.prefixlen = IPV4_MAX_PREFIXLEN;
958 p.prefix = *ifaddr;
959
960 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
961 if (! rn)
962 {
963 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
964 inet_ntoa (*ifaddr));
965 return;
966 }
967 rn->info = NULL;
968 route_unlock_node (rn);
969 route_unlock_node (rn);
970 }
971
972 /* Lookup interface by interface's IP address or interface index. */
973 static struct interface *
974 ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
975 {
976 struct prefix_ipv4 p;
977 struct route_node *rn;
978 struct interface *ifp;
979
980 if (addr)
981 {
982 p.family = AF_INET;
983 p.prefixlen = IPV4_MAX_PREFIXLEN;
984 p.prefix = *addr;
985
986 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
987 if (! rn)
988 return NULL;
989
990 ifp = rn->info;
991 route_unlock_node (rn);
992 return ifp;
993 }
994 else
995 return if_lookup_by_index(ifindex, VRF_DEFAULT);
996 }
997 #endif /* ifaddr_ipv4_table */
998
999 void if_terminate(struct vrf *vrf)
1000 {
1001 struct interface *ifp;
1002
1003 while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
1004 ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name);
1005
1006 if (ifp->node) {
1007 ifp->node->info = NULL;
1008 route_unlock_node(ifp->node);
1009 }
1010 if_delete(ifp);
1011 }
1012 }
1013
1014 const char *if_link_type_str(enum zebra_link_type llt)
1015 {
1016 switch (llt) {
1017 #define llts(T,S) case (T): return (S)
1018 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
1019 llts(ZEBRA_LLT_ETHER, "Ethernet");
1020 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
1021 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
1022 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
1023 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
1024 llts(ZEBRA_LLT_ARCNET, "ARCnet");
1025 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
1026 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
1027 llts(ZEBRA_LLT_ATM, "ATM");
1028 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
1029 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
1030 llts(ZEBRA_LLT_EUI64, "EUI-64");
1031 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
1032 llts(ZEBRA_LLT_SLIP, "SLIP");
1033 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
1034 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
1035 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
1036 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
1037 llts(ZEBRA_LLT_X25, "CCITT X.25");
1038 llts(ZEBRA_LLT_PPP, "PPP");
1039 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
1040 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1041 llts(ZEBRA_LLT_LAPB, "LAPB");
1042 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1043 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1044 llts(ZEBRA_LLT_FRAD, "FRAD");
1045 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1046 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1047 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1048 llts(ZEBRA_LLT_FDDI, "FDDI");
1049 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1050 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1051 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1052 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1053 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1054 llts(ZEBRA_LLT_IRDA, "IrDA");
1055 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1056 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1057 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1058 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1059 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1060 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1061 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1062 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1063 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1064 default:
1065 flog_err(EC_LIB_DEVELOPMENT, "Unknown value %d", llt);
1066 return "Unknown type!";
1067 #undef llts
1068 }
1069 return NULL;
1070 }
1071
1072 struct if_link_params *if_link_params_get(struct interface *ifp)
1073 {
1074 int i;
1075
1076 if (ifp->link_params != NULL)
1077 return ifp->link_params;
1078
1079 struct if_link_params *iflp =
1080 XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params));
1081 if (iflp == NULL)
1082 return NULL;
1083
1084 /* Set TE metric equal to standard metric */
1085 iflp->te_metric = ifp->metric;
1086
1087 /* Compute default bandwidth based on interface */
1088 iflp->default_bw =
1089 ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1090 * TE_KILO_BIT / TE_BYTE);
1091
1092 /* Set Max, Reservable and Unreserved Bandwidth */
1093 iflp->max_bw = iflp->default_bw;
1094 iflp->max_rsv_bw = iflp->default_bw;
1095 for (i = 0; i < MAX_CLASS_TYPE; i++)
1096 iflp->unrsv_bw[i] = iflp->default_bw;
1097
1098 /* Update Link parameters status */
1099 iflp->lp_status =
1100 LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
1101
1102 /* Finally attach newly created Link Parameters */
1103 ifp->link_params = iflp;
1104
1105 return iflp;
1106 }
1107
1108 void if_link_params_free(struct interface *ifp)
1109 {
1110 if (ifp->link_params == NULL)
1111 return;
1112 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1113 ifp->link_params = NULL;
1114 }
1115
1116 /* ----------- CLI commands ----------- */
1117
1118 /*
1119 * XPath: /frr-interface:lib/interface
1120 */
1121 DEFPY_NOSH (interface,
1122 interface_cmd,
1123 "interface IFNAME [vrf NAME$vrfname]",
1124 "Select an interface to configure\n"
1125 "Interface's name\n"
1126 VRF_CMD_HELP_STR)
1127 {
1128 char xpath_list[XPATH_MAXLEN];
1129 vrf_id_t vrf_id;
1130 struct interface *ifp;
1131 int ret;
1132
1133 if (!vrfname)
1134 vrfname = VRF_DEFAULT_NAME;
1135
1136 /*
1137 * This command requires special handling to maintain backward
1138 * compatibility. If a VRF name is not specified, it means we're willing
1139 * to accept any interface with the given name on any VRF. If no
1140 * interface is found, then a new one should be created on the default
1141 * VRF.
1142 */
1143 VRF_GET_ID(vrf_id, vrfname, false);
1144 ifp = if_lookup_by_name_all_vrf(ifname);
1145 if (ifp && ifp->vrf_id != vrf_id) {
1146 struct vrf *vrf;
1147
1148 /*
1149 * Special case 1: a VRF name was specified, but the found
1150 * interface is associated to different VRF. Reject the command.
1151 */
1152 if (vrf_id != VRF_DEFAULT) {
1153 vty_out(vty, "%% interface %s not in %s vrf\n", ifname,
1154 vrfname);
1155 return CMD_WARNING_CONFIG_FAILED;
1156 }
1157
1158 /*
1159 * Special case 2: a VRF name was *not* specified, and the found
1160 * interface is associated to a VRF other than the default one.
1161 * Update vrf_id and vrfname to account for that.
1162 */
1163 vrf = vrf_lookup_by_id(ifp->vrf_id);
1164 assert(vrf);
1165 vrf_id = ifp->vrf_id;
1166 vrfname = vrf->name;
1167 }
1168
1169 snprintf(xpath_list, sizeof(xpath_list),
1170 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname,
1171 vrfname);
1172
1173 nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, NULL);
1174 ret = nb_cli_apply_changes(vty, xpath_list);
1175 if (ret == CMD_SUCCESS) {
1176 VTY_PUSH_XPATH(INTERFACE_NODE, xpath_list);
1177
1178 /*
1179 * For backward compatibility with old commands we still need
1180 * to use the qobj infrastructure. This can be removed once
1181 * all interface-level commands are converted to the new
1182 * northbound model.
1183 */
1184 ifp = if_lookup_by_name(ifname, vrf_id);
1185 if (ifp)
1186 VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp);
1187 }
1188
1189 return ret;
1190 }
1191
1192 DEFPY (no_interface,
1193 no_interface_cmd,
1194 "no interface IFNAME [vrf NAME$vrfname]",
1195 NO_STR
1196 "Delete a pseudo interface's configuration\n"
1197 "Interface's name\n"
1198 VRF_CMD_HELP_STR)
1199 {
1200 if (!vrfname)
1201 vrfname = VRF_DEFAULT_NAME;
1202
1203 nb_cli_enqueue_change(vty, ".", NB_OP_DESTROY, NULL);
1204
1205 return nb_cli_apply_changes(
1206 vty, "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1207 ifname, vrfname);
1208 }
1209
1210 static void cli_show_interface(struct vty *vty, struct lyd_node *dnode,
1211 bool show_defaults)
1212 {
1213 const char *vrf;
1214
1215 vrf = yang_dnode_get_string(dnode, "./vrf");
1216
1217 vty_out(vty, "!\n");
1218 vty_out(vty, "interface %s", yang_dnode_get_string(dnode, "./name"));
1219 if (!strmatch(vrf, VRF_DEFAULT_NAME))
1220 vty_out(vty, " vrf %s", vrf);
1221 vty_out(vty, "\n");
1222 }
1223
1224 /*
1225 * XPath: /frr-interface:lib/interface/description
1226 */
1227 DEFPY (interface_desc,
1228 interface_desc_cmd,
1229 "description LINE...",
1230 "Interface specific description\n"
1231 "Characters describing this interface\n")
1232 {
1233 char *desc;
1234 int ret;
1235
1236 desc = argv_concat(argv, argc, 1);
1237 nb_cli_enqueue_change(vty, "./description", NB_OP_MODIFY, desc);
1238 ret = nb_cli_apply_changes(vty, NULL);
1239 XFREE(MTYPE_TMP, desc);
1240
1241 return ret;
1242 }
1243
1244 DEFPY (no_interface_desc,
1245 no_interface_desc_cmd,
1246 "no description",
1247 NO_STR
1248 "Interface specific description\n")
1249 {
1250 nb_cli_enqueue_change(vty, "./description", NB_OP_DESTROY, NULL);
1251
1252 return nb_cli_apply_changes(vty, NULL);
1253 }
1254
1255 static void cli_show_interface_desc(struct vty *vty, struct lyd_node *dnode,
1256 bool show_defaults)
1257 {
1258 vty_out(vty, " description %s\n", yang_dnode_get_string(dnode, NULL));
1259 }
1260
1261 /* Interface autocomplete. */
1262 static void if_autocomplete(vector comps, struct cmd_token *token)
1263 {
1264 struct interface *ifp;
1265 struct vrf *vrf;
1266
1267 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1268 FOR_ALL_INTERFACES (vrf, ifp) {
1269 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name));
1270 }
1271 }
1272 }
1273
1274 static const struct cmd_variable_handler if_var_handlers[] = {
1275 {/* "interface NAME" */
1276 .varname = "interface",
1277 .completions = if_autocomplete},
1278 {.tokenname = "IFNAME", .completions = if_autocomplete},
1279 {.tokenname = "INTERFACE", .completions = if_autocomplete},
1280 {.completions = NULL}};
1281
1282 void if_cmd_init(void)
1283 {
1284 cmd_variable_handler_register(if_var_handlers);
1285
1286 install_element(CONFIG_NODE, &interface_cmd);
1287 install_element(CONFIG_NODE, &no_interface_cmd);
1288
1289 install_default(INTERFACE_NODE);
1290 install_element(INTERFACE_NODE, &interface_desc_cmd);
1291 install_element(INTERFACE_NODE, &no_interface_desc_cmd);
1292 }
1293
1294 /* ------- Northbound callbacks ------- */
1295
1296 /*
1297 * XPath: /frr-interface:lib/interface
1298 */
1299 static int lib_interface_create(enum nb_event event,
1300 const struct lyd_node *dnode,
1301 union nb_resource *resource)
1302 {
1303 const char *ifname;
1304 const char *vrfname;
1305 struct vrf *vrf;
1306 struct interface *ifp;
1307
1308 ifname = yang_dnode_get_string(dnode, "./name");
1309 vrfname = yang_dnode_get_string(dnode, "./vrf");
1310
1311 switch (event) {
1312 case NB_EV_VALIDATE:
1313 vrf = vrf_lookup_by_name(vrfname);
1314 if (!vrf) {
1315 zlog_warn("%s: VRF %s doesn't exist", __func__,
1316 vrfname);
1317 return NB_ERR_VALIDATION;
1318 }
1319 if (vrf->vrf_id == VRF_UNKNOWN) {
1320 zlog_warn("%s: VRF %s is not active", __func__,
1321 vrf->name);
1322 return NB_ERR_VALIDATION;
1323 }
1324
1325 /* if VRF is netns or not yet known - init for instance
1326 * then assumption is that passed config is exact
1327 * then the user intent was not to use an other iface
1328 */
1329 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE) {
1330 ifp = if_lookup_by_name_all_vrf(ifname);
1331 if (ifp && ifp->vrf_id != vrf->vrf_id) {
1332 zlog_warn(
1333 "%s: interface %s already exists in another VRF",
1334 __func__, ifp->name);
1335 return NB_ERR_VALIDATION;
1336 }
1337 }
1338 break;
1339 case NB_EV_PREPARE:
1340 case NB_EV_ABORT:
1341 break;
1342 case NB_EV_APPLY:
1343 vrf = vrf_lookup_by_name(vrfname);
1344 assert(vrf);
1345 #ifdef SUNOS_5
1346 ifp = if_sunwzebra_get(ifname, vrf->vrf_id);
1347 #else
1348 ifp = if_get_by_name(ifname, vrf->vrf_id);
1349 #endif /* SUNOS_5 */
1350 nb_running_set_entry(dnode, ifp);
1351 break;
1352 }
1353
1354 return NB_OK;
1355 }
1356
1357 static int lib_interface_destroy(enum nb_event event,
1358 const struct lyd_node *dnode)
1359 {
1360 struct interface *ifp;
1361
1362
1363 switch (event) {
1364 case NB_EV_VALIDATE:
1365 ifp = nb_running_get_entry(dnode, NULL, true);
1366 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1367 zlog_warn("%s: only inactive interfaces can be deleted",
1368 __func__);
1369 return NB_ERR_VALIDATION;
1370 }
1371 break;
1372 case NB_EV_PREPARE:
1373 case NB_EV_ABORT:
1374 break;
1375 case NB_EV_APPLY:
1376 ifp = nb_running_unset_entry(dnode);
1377 if_delete(ifp);
1378 break;
1379 }
1380
1381 return NB_OK;
1382 }
1383
1384 /*
1385 * XPath: /frr-interface:lib/interface/description
1386 */
1387 static int lib_interface_description_modify(enum nb_event event,
1388 const struct lyd_node *dnode,
1389 union nb_resource *resource)
1390 {
1391 struct interface *ifp;
1392 const char *description;
1393
1394 if (event != NB_EV_APPLY)
1395 return NB_OK;
1396
1397 ifp = nb_running_get_entry(dnode, NULL, true);
1398 XFREE(MTYPE_TMP, ifp->desc);
1399 description = yang_dnode_get_string(dnode, NULL);
1400 ifp->desc = XSTRDUP(MTYPE_TMP, description);
1401
1402 return NB_OK;
1403 }
1404
1405 static int lib_interface_description_destroy(enum nb_event event,
1406 const struct lyd_node *dnode)
1407 {
1408 struct interface *ifp;
1409
1410 if (event != NB_EV_APPLY)
1411 return NB_OK;
1412
1413 ifp = nb_running_get_entry(dnode, NULL, true);
1414 XFREE(MTYPE_TMP, ifp->desc);
1415
1416 return NB_OK;
1417 }
1418
1419 /* clang-format off */
1420 const struct frr_yang_module_info frr_interface_info = {
1421 .name = "frr-interface",
1422 .nodes = {
1423 {
1424 .xpath = "/frr-interface:lib/interface",
1425 .cbs = {
1426 .create = lib_interface_create,
1427 .destroy = lib_interface_destroy,
1428 .cli_show = cli_show_interface,
1429 },
1430 },
1431 {
1432 .xpath = "/frr-interface:lib/interface/description",
1433 .cbs = {
1434 .modify = lib_interface_description_modify,
1435 .destroy = lib_interface_description_destroy,
1436 .cli_show = cli_show_interface_desc,
1437 },
1438 },
1439 {
1440 .xpath = NULL,
1441 },
1442 }
1443 };