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