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