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