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