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