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