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