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