]> git.proxmox.com Git - mirror_frr.git/blame - lib/if.c
zebra: Use dplane for interface addresses (netlink)
[mirror_frr.git] / lib / if.c
CommitLineData
d62a17ae 1/*
718e3744 2 * Interface functions.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
896014f4 6 *
718e3744 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 *
896014f4
DL
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
718e3744 20 */
21
22#include <zebra.h>
23
24#include "linklist.h"
25#include "vector.h"
4d43f68a 26#include "lib_errors.h"
718e3744 27#include "vty.h"
28#include "command.h"
8736158a 29#include "vrf.h"
718e3744 30#include "if.h"
31#include "sockunion.h"
32#include "prefix.h"
718e3744 33#include "memory.h"
34#include "table.h"
35#include "buffer.h"
718e3744 36#include "log.h"
8f90d89b
RW
37#include "northbound_cli.h"
38#ifndef VTYSH_EXTRACT_PL
39#include "lib/if_clippy.c"
40#endif
6b0655a2 41
d62a17ae 42DEFINE_MTYPE(LIB, IF, "Interface")
43DEFINE_MTYPE_STATIC(LIB, CONNECTED, "Connected")
44DEFINE_MTYPE_STATIC(LIB, NBR_CONNECTED, "Neighbor Connected")
45DEFINE_MTYPE(LIB, CONNECTED_LABEL, "Connected interface label")
46DEFINE_MTYPE_STATIC(LIB, IF_LINK_PARAMS, "Informational Link Parameters")
4a1ab8e4 47
f4e14fdb 48static int if_cmp_func(const struct interface *, const struct interface *);
ff880b78
RW
49static int if_cmp_index_func(const struct interface *ifp1,
50 const struct interface *ifp2);
f4e14fdb 51RB_GENERATE(if_name_head, interface, name_entry, if_cmp_func);
ff880b78 52RB_GENERATE(if_index_head, interface, index_entry, if_cmp_index_func);
f4e14fdb 53
e80e7cce
DL
54DEFINE_QOBJ_TYPE(interface)
55
996c9314
LB
56DEFINE_HOOK(if_add, (struct interface * ifp), (ifp))
57DEFINE_KOOH(if_del, (struct interface * ifp), (ifp))
ce19a04a 58
3a0391a9 59/* Compare interface names, returning an integer greater than, equal to, or
60 * less than 0, (following the strcmp convention), according to the
61 * relationship between ifp1 and ifp2. Interface names consist of an
62 * alphabetic prefix and a numeric suffix. The primary sort key is
63 * lexicographic by name, and then numeric by number. No number sorts
64 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
65 * devpty0, de0 < del0
d62a17ae 66 */
36de6e0e 67int if_cmp_name_func(const char *p1, const char *p2)
106d2fd5 68{
d62a17ae 69 unsigned int l1, l2;
70 long int x1, x2;
71 int res;
72
73 while (*p1 && *p2) {
74 /* look up to any number */
75 l1 = strcspn(p1, "0123456789");
76 l2 = strcspn(p2, "0123456789");
77
78 /* name lengths are different -> compare names */
79 if (l1 != l2)
80 return (strcmp(p1, p2));
81
82 /* Note that this relies on all numbers being less than all
83 * letters, so
84 * that de0 < del0.
85 */
86 res = strncmp(p1, p2, l1);
87
88 /* names are different -> compare them */
89 if (res)
90 return res;
91
92 /* with identical name part, go to numeric part */
93 p1 += l1;
94 p2 += l1;
95
c9cbbb40
RW
96 if (!*p1 && !*p2)
97 return 0;
d62a17ae 98 if (!*p1)
99 return -1;
100 if (!*p2)
101 return 1;
102
36de6e0e
A
103 x1 = strtol(p1, (char **)&p1, 10);
104 x2 = strtol(p2, (char **)&p2, 10);
d62a17ae 105
106 /* let's compare numbers now */
107 if (x1 < x2)
108 return -1;
109 if (x1 > x2)
110 return 1;
111
112 /* numbers were equal, lets do it again..
113 (it happens with name like "eth123.456:789") */
114 }
115 if (*p1)
116 return 1;
117 if (*p2)
118 return -1;
119 return 0;
106d2fd5 120}
121
f4e14fdb
RW
122static int if_cmp_func(const struct interface *ifp1,
123 const struct interface *ifp2)
974fc9d2 124{
36de6e0e 125 return if_cmp_name_func(ifp1->name, ifp2->name);
974fc9d2
DS
126}
127
ff880b78
RW
128static int if_cmp_index_func(const struct interface *ifp1,
129 const struct interface *ifp2)
130{
131 return ifp1->ifindex - ifp2->ifindex;
132}
133
718e3744 134/* Create new interface structure. */
bcc24579 135struct interface *if_create(const char *name, vrf_id_t vrf_id)
718e3744 136{
f4e14fdb 137 struct vrf *vrf = vrf_get(vrf_id, NULL);
d62a17ae 138 struct interface *ifp;
d62a17ae 139
140 ifp = XCALLOC(MTYPE_IF, sizeof(struct interface));
141 ifp->ifindex = IFINDEX_INTERNAL;
142
143 assert(name);
bcc24579 144 strlcpy(ifp->name, name, sizeof(ifp->name));
d62a17ae 145 ifp->vrf_id = vrf_id;
ff880b78 146 IFNAME_RB_INSERT(vrf, ifp);
d62a17ae 147 ifp->connected = list_new();
148 ifp->connected->del = (void (*)(void *))connected_free;
149
150 ifp->nbr_connected = list_new();
151 ifp->nbr_connected->del = (void (*)(void *))nbr_connected_free;
152
153 /* Enable Link-detection by default */
154 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
155
156 QOBJ_REG(ifp, interface);
996c9314 157 hook_call(if_add, ifp);
d62a17ae 158 return ifp;
718e3744 159}
160
f93e3f69 161/* Create new interface structure. */
d62a17ae 162void if_update_to_new_vrf(struct interface *ifp, vrf_id_t vrf_id)
f93e3f69 163{
8f90d89b 164 struct vrf *old_vrf, *vrf;
d62a17ae 165
166 /* remove interface from old master vrf list */
8f90d89b
RW
167 old_vrf = vrf_lookup_by_id(ifp->vrf_id);
168 if (old_vrf) {
169 IFNAME_RB_REMOVE(old_vrf, ifp);
ff880b78 170 if (ifp->ifindex != IFINDEX_INTERNAL)
8f90d89b 171 IFINDEX_RB_REMOVE(old_vrf, ifp);
ff880b78 172 }
d62a17ae 173
174 ifp->vrf_id = vrf_id;
f4e14fdb 175 vrf = vrf_get(ifp->vrf_id, NULL);
ff880b78
RW
176
177 IFNAME_RB_INSERT(vrf, ifp);
178 if (ifp->ifindex != IFINDEX_INTERNAL)
179 IFINDEX_RB_INSERT(vrf, ifp);
8f90d89b
RW
180
181 /*
182 * HACK: Change the interface VRF in the running configuration directly,
183 * bypassing the northbound layer. This is necessary to avoid deleting
184 * the interface and readding it in the new VRF, which would have
185 * several implications.
186 */
187 if (yang_module_find("frr-interface")) {
188 struct lyd_node *if_dnode;
189
190 if_dnode = yang_dnode_get(
191 running_config->dnode,
192 "/frr-interface:lib/interface[name='%s'][vrf='%s']/vrf",
193 ifp->name, old_vrf->name);
194 if (if_dnode) {
195 yang_dnode_change_leaf(if_dnode, vrf->name);
196 running_config->version++;
197 }
198 }
f93e3f69
DS
199}
200
201
d2fc8896 202/* Delete interface structure. */
d62a17ae 203void if_delete_retain(struct interface *ifp)
718e3744 204{
996c9314 205 hook_call(if_del, ifp);
d62a17ae 206 QOBJ_UNREG(ifp);
e80e7cce 207
d62a17ae 208 /* Free connected address list */
209 list_delete_all_node(ifp->connected);
a80beece 210
d62a17ae 211 /* Free connected nbr address list */
212 list_delete_all_node(ifp->nbr_connected);
d2fc8896 213}
214
215/* Delete and free interface structure. */
d62a17ae 216void if_delete(struct interface *ifp)
d2fc8896 217{
5b8524f5
RW
218 struct vrf *vrf;
219
220 vrf = vrf_lookup_by_id(ifp->vrf_id);
221 assert(vrf);
f4e14fdb 222
ff880b78
RW
223 IFNAME_RB_REMOVE(vrf, ifp);
224 if (ifp->ifindex != IFINDEX_INTERNAL)
225 IFINDEX_RB_REMOVE(vrf, ifp);
d2fc8896 226
d62a17ae 227 if_delete_retain(ifp);
718e3744 228
6a154c88
DL
229 list_delete(&ifp->connected);
230 list_delete(&ifp->nbr_connected);
2dd04c5d 231
d62a17ae 232 if_link_params_free(ifp);
16f1b9ee 233
0a22ddfb 234 XFREE(MTYPE_TMP, ifp->desc);
c7974c0f 235
d62a17ae 236 XFREE(MTYPE_IF, ifp);
718e3744 237}
238
718e3744 239/* Interface existance check by index. */
d62a17ae 240struct interface *if_lookup_by_index(ifindex_t ifindex, vrf_id_t vrf_id)
718e3744 241{
5b8524f5 242 struct vrf *vrf;
ff880b78 243 struct interface if_tmp;
f4e14fdb 244
5b8524f5
RW
245 vrf = vrf_lookup_by_id(vrf_id);
246 if (!vrf)
247 return NULL;
248
ff880b78
RW
249 if_tmp.ifindex = ifindex;
250 return RB_FIND(if_index_head, &vrf->ifaces_by_index, &if_tmp);
718e3744 251}
252
d62a17ae 253const char *ifindex2ifname(ifindex_t ifindex, vrf_id_t vrf_id)
718e3744 254{
d62a17ae 255 struct interface *ifp;
718e3744 256
d62a17ae 257 return ((ifp = if_lookup_by_index(ifindex, vrf_id)) != NULL)
258 ? ifp->name
259 : "unknown";
d2fc8896 260}
261
d62a17ae 262ifindex_t ifname2ifindex(const char *name, vrf_id_t vrf_id)
d2fc8896 263{
d62a17ae 264 struct interface *ifp;
d2fc8896 265
d62a17ae 266 return ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
267 ? ifp->ifindex
268 : IFINDEX_INTERNAL;
718e3744 269}
270
271/* Interface existance check by interface name. */
d62a17ae 272struct interface *if_lookup_by_name(const char *name, vrf_id_t vrf_id)
718e3744 273{
f4e14fdb
RW
274 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
275 struct interface if_tmp;
d62a17ae 276
5b8524f5
RW
277 if (!vrf || !name
278 || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
bcc24579 279 return NULL;
f8962871 280
f4e14fdb
RW
281 strlcpy(if_tmp.name, name, sizeof(if_tmp.name));
282 return RB_FIND(if_name_head, &vrf->ifaces_by_name, &if_tmp);
f8962871
DS
283}
284
bcc24579 285struct interface *if_lookup_by_name_all_vrf(const char *name)
a349198f 286{
bcc24579 287 struct vrf *vrf;
d62a17ae 288 struct interface *ifp;
a349198f 289
bcc24579 290 if (!name || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
d62a17ae 291 return NULL;
a349198f 292
bcc24579
RW
293 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
294 ifp = if_lookup_by_name(name, vrf->vrf_id);
295 if (ifp)
d62a17ae 296 return ifp;
297 }
bcc24579 298
d62a17ae 299 return NULL;
a349198f 300}
301
718e3744 302/* Lookup interface by IPv4 address. */
d62a17ae 303struct interface *if_lookup_exact_address(void *src, int family,
304 vrf_id_t vrf_id)
718e3744 305{
f4e14fdb 306 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 307 struct listnode *cnode;
308 struct interface *ifp;
309 struct prefix *p;
310 struct connected *c;
311
451fda4f 312 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 313 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
314 p = c->address;
315
316 if (p && (p->family == family)) {
317 if (family == AF_INET) {
318 if (IPV4_ADDR_SAME(
319 &p->u.prefix4,
320 (struct in_addr *)src))
321 return ifp;
322 } else if (family == AF_INET6) {
323 if (IPV6_ADDR_SAME(
324 &p->u.prefix6,
325 (struct in6_addr *)src))
326 return ifp;
327 }
328 }
0aabccc0 329 }
718e3744 330 }
d62a17ae 331 return NULL;
718e3744 332}
333
334/* Lookup interface by IPv4 address. */
d62a17ae 335struct connected *if_lookup_address(void *matchaddr, int family,
336 vrf_id_t vrf_id)
718e3744 337{
f4e14fdb 338 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 339 struct prefix addr;
340 int bestlen = 0;
341 struct listnode *cnode;
342 struct interface *ifp;
343 struct connected *c;
344 struct connected *match;
345
346 if (family == AF_INET) {
347 addr.family = AF_INET;
348 addr.u.prefix4 = *((struct in_addr *)matchaddr);
349 addr.prefixlen = IPV4_MAX_BITLEN;
350 } else if (family == AF_INET6) {
351 addr.family = AF_INET6;
352 addr.u.prefix6 = *((struct in6_addr *)matchaddr);
353 addr.prefixlen = IPV6_MAX_BITLEN;
354 }
718e3744 355
d62a17ae 356 match = NULL;
718e3744 357
451fda4f 358 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 359 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
360 if (c->address && (c->address->family == AF_INET)
361 && prefix_match(CONNECTED_PREFIX(c), &addr)
362 && (c->address->prefixlen > bestlen)) {
363 bestlen = c->address->prefixlen;
364 match = c;
365 }
366 }
718e3744 367 }
d62a17ae 368 return match;
718e3744 369}
370
b81e97a8 371/* Lookup interface by prefix */
d62a17ae 372struct interface *if_lookup_prefix(struct prefix *prefix, vrf_id_t vrf_id)
b81e97a8 373{
f4e14fdb 374 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 375 struct listnode *cnode;
376 struct interface *ifp;
377 struct connected *c;
378
451fda4f 379 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 380 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
381 if (prefix_cmp(c->address, prefix) == 0) {
382 return ifp;
383 }
384 }
385 }
386 return NULL;
b81e97a8
DD
387}
388
718e3744 389/* Get interface by name if given name interface doesn't exist create
390 one. */
8f90d89b 391struct interface *if_get_by_name(const char *name, vrf_id_t vrf_id)
718e3744 392{
8f90d89b 393 struct interface *ifp;
718e3744 394
8f90d89b 395 switch (vrf_get_backend()) {
b0b97a7f 396 case VRF_BACKEND_UNKNOWN:
8f90d89b 397 case VRF_BACKEND_NETNS:
ee2f2c23
TC
398 ifp = if_lookup_by_name(name, vrf_id);
399 if (ifp)
400 return ifp;
e9e9b115 401 return if_create(name, vrf_id);
8f90d89b
RW
402 case VRF_BACKEND_VRF_LITE:
403 ifp = if_lookup_by_name_all_vrf(name);
404 if (ifp) {
405 if (ifp->vrf_id == vrf_id)
379eb245 406 return ifp;
8f90d89b
RW
407 /* If it came from the kernel or by way of zclient,
408 * believe it and update the ifp accordingly.
409 */
410 if_update_to_new_vrf(ifp, vrf_id);
411 return ifp;
ee2f2c23 412 }
8f90d89b 413 return if_create(name, vrf_id);
85f9da7f 414 }
8f90d89b
RW
415
416 return NULL;
8736158a
FL
417}
418
ff880b78
RW
419void if_set_index(struct interface *ifp, ifindex_t ifindex)
420{
5b8524f5
RW
421 struct vrf *vrf;
422
423 vrf = vrf_lookup_by_id(ifp->vrf_id);
424 assert(vrf);
ff880b78
RW
425
426 if (ifp->ifindex == ifindex)
427 return;
428
429 if (ifp->ifindex != IFINDEX_INTERNAL)
430 IFINDEX_RB_REMOVE(vrf, ifp)
431
432 ifp->ifindex = ifindex;
433
434 if (ifp->ifindex != IFINDEX_INTERNAL)
435 IFINDEX_RB_INSERT(vrf, ifp)
436}
437
718e3744 438/* Does interface up ? */
d62a17ae 439int if_is_up(struct interface *ifp)
718e3744 440{
d62a17ae 441 return ifp->flags & IFF_UP;
718e3744 442}
443
2e3b2e47 444/* Is interface running? */
d62a17ae 445int if_is_running(struct interface *ifp)
2e3b2e47 446{
d62a17ae 447 return ifp->flags & IFF_RUNNING;
2e3b2e47 448}
449
450/* Is the interface operative, eg. either UP & RUNNING
244c1cdc
DS
451 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
452 if ptm checking is enabled, then ptm check has passed */
d62a17ae 453int if_is_operative(struct interface *ifp)
2e3b2e47 454{
d62a17ae 455 return ((ifp->flags & IFF_UP)
456 && (((ifp->flags & IFF_RUNNING)
457 && (ifp->ptm_status || !ifp->ptm_enable))
458 || !CHECK_FLAG(ifp->status,
459 ZEBRA_INTERFACE_LINKDETECTION)));
244c1cdc
DS
460}
461
462/* Is the interface operative, eg. either UP & RUNNING
463 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
d62a17ae 464int if_is_no_ptm_operative(struct interface *ifp)
244c1cdc 465{
d62a17ae 466 return ((ifp->flags & IFF_UP)
467 && ((ifp->flags & IFF_RUNNING)
468 || !CHECK_FLAG(ifp->status,
469 ZEBRA_INTERFACE_LINKDETECTION)));
2e3b2e47 470}
471
718e3744 472/* Is this loopback interface ? */
d62a17ae 473int if_is_loopback(struct interface *ifp)
718e3744 474{
d62a17ae 475 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
476 * but Y on platform N?
477 */
478 return (ifp->flags & (IFF_LOOPBACK | IFF_NOXMIT | IFF_VIRTUAL));
718e3744 479}
480
0c74bbe0
CS
481/* Check interface is VRF */
482int if_is_vrf(struct interface *ifp)
483{
484 return CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
485}
486
fec4ca19
DS
487bool if_is_loopback_or_vrf(struct interface *ifp)
488{
489 if (if_is_loopback(ifp) || if_is_vrf(ifp))
490 return true;
491
492 return false;
493}
494
718e3744 495/* Does this interface support broadcast ? */
d62a17ae 496int if_is_broadcast(struct interface *ifp)
718e3744 497{
d62a17ae 498 return ifp->flags & IFF_BROADCAST;
718e3744 499}
500
501/* Does this interface support broadcast ? */
d62a17ae 502int if_is_pointopoint(struct interface *ifp)
718e3744 503{
d62a17ae 504 return ifp->flags & IFF_POINTOPOINT;
718e3744 505}
506
507/* Does this interface support multicast ? */
d62a17ae 508int if_is_multicast(struct interface *ifp)
718e3744 509{
d62a17ae 510 return ifp->flags & IFF_MULTICAST;
718e3744 511}
512
513/* Printout flag information into log */
d62a17ae 514const char *if_flag_dump(unsigned long flag)
718e3744 515{
d62a17ae 516 int separator = 0;
517 static char logbuf[BUFSIZ];
518
519#define IFF_OUT_LOG(X, STR) \
520 if (flag & (X)) { \
521 if (separator) \
3393df5c 522 strlcat(logbuf, ",", sizeof(logbuf)); \
d62a17ae 523 else \
524 separator = 1; \
3393df5c 525 strlcat(logbuf, STR, sizeof(logbuf)); \
d62a17ae 526 }
718e3744 527
d62a17ae 528 strlcpy(logbuf, "<", BUFSIZ);
529 IFF_OUT_LOG(IFF_UP, "UP");
530 IFF_OUT_LOG(IFF_BROADCAST, "BROADCAST");
531 IFF_OUT_LOG(IFF_DEBUG, "DEBUG");
532 IFF_OUT_LOG(IFF_LOOPBACK, "LOOPBACK");
533 IFF_OUT_LOG(IFF_POINTOPOINT, "POINTOPOINT");
534 IFF_OUT_LOG(IFF_NOTRAILERS, "NOTRAILERS");
535 IFF_OUT_LOG(IFF_RUNNING, "RUNNING");
536 IFF_OUT_LOG(IFF_NOARP, "NOARP");
537 IFF_OUT_LOG(IFF_PROMISC, "PROMISC");
538 IFF_OUT_LOG(IFF_ALLMULTI, "ALLMULTI");
539 IFF_OUT_LOG(IFF_OACTIVE, "OACTIVE");
540 IFF_OUT_LOG(IFF_SIMPLEX, "SIMPLEX");
541 IFF_OUT_LOG(IFF_LINK0, "LINK0");
542 IFF_OUT_LOG(IFF_LINK1, "LINK1");
543 IFF_OUT_LOG(IFF_LINK2, "LINK2");
544 IFF_OUT_LOG(IFF_MULTICAST, "MULTICAST");
545 IFF_OUT_LOG(IFF_NOXMIT, "NOXMIT");
546 IFF_OUT_LOG(IFF_NORTEXCH, "NORTEXCH");
547 IFF_OUT_LOG(IFF_VIRTUAL, "VIRTUAL");
548 IFF_OUT_LOG(IFF_IPV4, "IPv4");
549 IFF_OUT_LOG(IFF_IPV6, "IPv6");
550
3393df5c 551 strlcat(logbuf, ">", sizeof(logbuf));
d62a17ae 552
553 return logbuf;
630c97ce 554#undef IFF_OUT_LOG
718e3744 555}
556
557/* For debugging */
d62a17ae 558static void if_dump(const struct interface *ifp)
718e3744 559{
d62a17ae 560 struct listnode *node;
561 struct connected *c __attribute__((unused));
562
563 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, c))
564 zlog_info(
565 "Interface %s vrf %u index %d metric %d mtu %d "
566 "mtu6 %d %s",
567 ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric,
568 ifp->mtu, ifp->mtu6, if_flag_dump(ifp->flags));
718e3744 569}
570
571/* Interface printing for all interface. */
d62a17ae 572void if_dump_all(void)
718e3744 573{
d62a17ae 574 struct vrf *vrf;
f4e14fdb 575 void *ifp;
d62a17ae 576
a2addae8 577 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
451fda4f 578 FOR_ALL_INTERFACES (vrf, ifp)
f4e14fdb 579 if_dump(ifp);
718e3744 580}
581
98954844
PJ
582#ifdef SUNOS_5
583/* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
584 * a seperate struct interface for each logical interface, so config
585 * file may be full of 'interface fooX:Y'. Solaris however does not
586 * expose logical interfaces via PF_ROUTE, so trying to track logical
587 * interfaces can be fruitless, for that reason Quagga only tracks
588 * the primary IP interface.
589 *
590 * We try accomodate SUNWzebra by:
591 * - looking up the interface name, to see whether it exists, if so
592 * its useable
593 * - for protocol daemons, this could only because zebra told us of
594 * the interface
595 * - for zebra, only because it learnt from kernel
596 * - if not:
597 * - search the name to see if it contains a sub-ipif / logical interface
598 * seperator, the ':' char. If it does:
599 * - text up to that char must be the primary name - get that name.
600 * if not:
601 * - no idea, just get the name in its entirety.
602 */
ae9eebca 603static struct interface *if_sunwzebra_get(const char *name, vrf_id_t vrf_id)
98954844 604{
d62a17ae 605 struct interface *ifp;
bcc24579 606 char *cp;
d62a17ae 607
bcc24579 608 if ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
d62a17ae 609 return ifp;
610
611 /* hunt the primary interface name... */
bcc24579
RW
612 cp = strchr(name, ':');
613 if (cp)
614 *cp = '\0';
d62a17ae 615
8f90d89b 616 return if_get_by_name(name, vrf_id);
98954844
PJ
617}
618#endif /* SUNOS_5 */
6b0655a2 619
d7d73ffc 620#if 0
718e3744 621/* For debug purpose. */
622DEFUN (show_address,
623 show_address_cmd,
199d90a1 624 "show address [vrf NAME]",
718e3744 625 SHOW_STR
fcbee690
QY
626 "address\n"
627 VRF_CMD_HELP_STR)
718e3744 628{
abddf075 629 int idx_vrf = 3;
52dc7ee6 630 struct listnode *node;
718e3744 631 struct interface *ifp;
632 struct connected *ifc;
633 struct prefix *p;
8736158a 634 vrf_id_t vrf_id = VRF_DEFAULT;
718e3744 635
fcbee690 636 if (argc > 2)
58749582 637 VRF_GET_ID (vrf_id, argv[idx_vrf]->arg);
8736158a 638
451fda4f 639 FOR_ALL_INTERFACES (vrf, ifp)
718e3744 640 {
f4e14fdb 641 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
718e3744 642 {
718e3744 643 p = ifc->address;
644
645 if (p->family == AF_INET)
55f70b67 646 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
718e3744 647 }
648 }
649 return CMD_SUCCESS;
650}
651
8736158a
FL
652DEFUN (show_address_vrf_all,
653 show_address_vrf_all_cmd,
9ccf14f7 654 "show address vrf all",
8736158a
FL
655 SHOW_STR
656 "address\n"
657 VRF_ALL_CMD_HELP_STR)
658{
1a1a7065 659 struct vrf *vrf;
8736158a 660 struct listnode *node;
8736158a
FL
661 struct interface *ifp;
662 struct connected *ifc;
663 struct prefix *p;
8736158a 664
a62c4901 665 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
8736158a 666 {
f4e14fdb 667 if (RB_EMPTY (if_name_head, &vrf->ifaces_by_name))
8736158a
FL
668 continue;
669
55f70b67 670 vty_out (vty, "\nVRF %u\n\n", vrf->vrf_id);
8736158a 671
451fda4f 672 FOR_ALL_INTERFACES (vrf, ifp)
8736158a 673 {
f4e14fdb 674 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
8736158a
FL
675 {
676 p = ifc->address;
677
678 if (p->family == AF_INET)
55f70b67 679 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
8736158a
FL
680 }
681 }
682 }
683 return CMD_SUCCESS;
684}
d7d73ffc 685#endif
8736158a 686
718e3744 687/* Allocate connected structure. */
d62a17ae 688struct connected *connected_new(void)
718e3744 689{
d62a17ae 690 return XCALLOC(MTYPE_CONNECTED, sizeof(struct connected));
718e3744 691}
692
a80beece 693/* Allocate nbr connected structure. */
d62a17ae 694struct nbr_connected *nbr_connected_new(void)
a80beece 695{
d62a17ae 696 return XCALLOC(MTYPE_NBR_CONNECTED, sizeof(struct nbr_connected));
a80beece
DS
697}
698
718e3744 699/* Free connected structure. */
d62a17ae 700void connected_free(struct connected *connected)
718e3744 701{
d62a17ae 702 if (connected->address)
703 prefix_free(connected->address);
718e3744 704
d62a17ae 705 if (connected->destination)
706 prefix_free(connected->destination);
718e3744 707
0a22ddfb 708 XFREE(MTYPE_CONNECTED_LABEL, connected->label);
718e3744 709
d62a17ae 710 XFREE(MTYPE_CONNECTED, connected);
718e3744 711}
712
a80beece 713/* Free nbr connected structure. */
d62a17ae 714void nbr_connected_free(struct nbr_connected *connected)
a80beece 715{
d62a17ae 716 if (connected->address)
717 prefix_free(connected->address);
a80beece 718
d62a17ae 719 XFREE(MTYPE_NBR_CONNECTED, connected);
a80beece
DS
720}
721
722/* If same interface nbr address already exists... */
d62a17ae 723struct nbr_connected *nbr_connected_check(struct interface *ifp,
724 struct prefix *p)
a80beece 725{
d62a17ae 726 struct nbr_connected *ifc;
727 struct listnode *node;
a80beece 728
d62a17ae 729 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, ifc))
730 if (prefix_same(ifc->address, p))
731 return ifc;
a80beece 732
d62a17ae 733 return NULL;
a80beece
DS
734}
735
718e3744 736/* Print if_addr structure. */
d62a17ae 737static void __attribute__((unused))
738connected_log(struct connected *connected, char *str)
718e3744 739{
d62a17ae 740 struct prefix *p;
741 struct interface *ifp;
742 char logbuf[BUFSIZ];
743 char buf[BUFSIZ];
744
745 ifp = connected->ifp;
746 p = connected->address;
747
748 snprintf(logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ", str,
749 ifp->name, ifp->vrf_id, prefix_family_str(p),
750 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
751
752 p = connected->destination;
753 if (p) {
754 strncat(logbuf, inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
755 BUFSIZ - strlen(logbuf));
756 }
757 zlog_info("%s", logbuf);
718e3744 758}
759
a80beece 760/* Print if_addr structure. */
d62a17ae 761static void __attribute__((unused))
762nbr_connected_log(struct nbr_connected *connected, char *str)
a80beece 763{
d62a17ae 764 struct prefix *p;
765 struct interface *ifp;
766 char logbuf[BUFSIZ];
767 char buf[BUFSIZ];
a80beece 768
d62a17ae 769 ifp = connected->ifp;
770 p = connected->address;
a80beece 771
d62a17ae 772 snprintf(logbuf, BUFSIZ, "%s interface %s %s %s/%d ", str, ifp->name,
773 prefix_family_str(p),
774 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
a80beece 775
d62a17ae 776 zlog_info("%s", logbuf);
a80beece
DS
777}
778
718e3744 779/* If two connected address has same prefix return 1. */
d62a17ae 780static int connected_same_prefix(struct prefix *p1, struct prefix *p2)
718e3744 781{
d62a17ae 782 if (p1->family == p2->family) {
783 if (p1->family == AF_INET
784 && IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
785 return 1;
786 if (p1->family == AF_INET6
787 && IPV6_ADDR_SAME(&p1->u.prefix6, &p2->u.prefix6))
788 return 1;
789 }
790 return 0;
718e3744 791}
792
d62a17ae 793struct connected *connected_lookup_prefix_exact(struct interface *ifp,
794 struct prefix *p)
38485402 795{
d62a17ae 796 struct listnode *node;
797 struct listnode *next;
798 struct connected *ifc;
38485402 799
d62a17ae 800 for (node = listhead(ifp->connected); node; node = next) {
801 ifc = listgetdata(node);
802 next = node->next;
38485402 803
d62a17ae 804 if (connected_same_prefix(ifc->address, p))
805 return ifc;
806 }
807 return NULL;
38485402
DS
808}
809
d62a17ae 810struct connected *connected_delete_by_prefix(struct interface *ifp,
811 struct prefix *p)
718e3744 812{
d62a17ae 813 struct listnode *node;
814 struct listnode *next;
815 struct connected *ifc;
816
817 /* In case of same prefix come, replace it with new one. */
818 for (node = listhead(ifp->connected); node; node = next) {
819 ifc = listgetdata(node);
820 next = node->next;
821
822 if (connected_same_prefix(ifc->address, p)) {
823 listnode_delete(ifp->connected, ifc);
824 return ifc;
825 }
718e3744 826 }
d62a17ae 827 return NULL;
718e3744 828}
829
bd40c341 830/* Find the address on our side that will be used when packets
727d104b 831 are sent to dst. */
d62a17ae 832struct connected *connected_lookup_prefix(struct interface *ifp,
833 struct prefix *addr)
727d104b 834{
d62a17ae 835 struct listnode *cnode;
836 struct connected *c;
837 struct connected *match;
838
839 match = NULL;
840
841 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
842 if (c->address && (c->address->family == addr->family)
843 && prefix_match(CONNECTED_PREFIX(c), addr)
844 && (!match
845 || (c->address->prefixlen > match->address->prefixlen)))
846 match = c;
847 }
848 return match;
727d104b 849}
850
d62a17ae 851struct connected *connected_add_by_prefix(struct interface *ifp,
852 struct prefix *p,
853 struct prefix *destination)
4a7aac1b 854{
d62a17ae 855 struct connected *ifc;
4a7aac1b 856
d62a17ae 857 /* Allocate new connected address. */
858 ifc = connected_new();
859 ifc->ifp = ifp;
4a7aac1b 860
d62a17ae 861 /* Fetch interface address */
862 ifc->address = prefix_new();
863 memcpy(ifc->address, p, sizeof(struct prefix));
4a7aac1b 864
d62a17ae 865 /* Fetch dest address */
866 if (destination) {
867 ifc->destination = prefix_new();
868 memcpy(ifc->destination, destination, sizeof(struct prefix));
869 }
4a7aac1b 870
d62a17ae 871 /* Add connected address to the interface. */
872 listnode_add(ifp->connected, ifc);
873 return ifc;
4a7aac1b 874}
875
d62a17ae 876#if 0 /* this route_table of struct connected's is unused \
877 * however, it would be good to use a route_table rather than \
878 * a list.. \
879 */
718e3744 880/* Interface looking up by interface's address. */
718e3744 881/* Interface's IPv4 address reverse lookup table. */
882struct route_table *ifaddr_ipv4_table;
883/* struct route_table *ifaddr_ipv6_table; */
884
8cc4198f 885static void
718e3744 886ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
887{
888 struct route_node *rn;
889 struct prefix_ipv4 p;
890
891 p.family = AF_INET;
892 p.prefixlen = IPV4_MAX_PREFIXLEN;
893 p.prefix = *ifaddr;
894
895 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
896 if (rn)
897 {
898 route_unlock_node (rn);
899 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
900 inet_ntoa (*ifaddr));
901 return;
902 }
903 rn->info = ifp;
904}
905
8cc4198f 906static void
718e3744 907ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
908{
909 struct route_node *rn;
910 struct prefix_ipv4 p;
911
912 p.family = AF_INET;
913 p.prefixlen = IPV4_MAX_PREFIXLEN;
914 p.prefix = *ifaddr;
915
916 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
917 if (! rn)
918 {
919 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
920 inet_ntoa (*ifaddr));
921 return;
922 }
923 rn->info = NULL;
924 route_unlock_node (rn);
925 route_unlock_node (rn);
926}
927
928/* Lookup interface by interface's IP address or interface index. */
8cc4198f 929static struct interface *
b892f1dd 930ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
718e3744 931{
932 struct prefix_ipv4 p;
933 struct route_node *rn;
934 struct interface *ifp;
718e3744 935
936 if (addr)
937 {
938 p.family = AF_INET;
939 p.prefixlen = IPV4_MAX_PREFIXLEN;
940 p.prefix = *addr;
941
942 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
943 if (! rn)
944 return NULL;
55cd0f61 945
718e3744 946 ifp = rn->info;
947 route_unlock_node (rn);
948 return ifp;
949 }
950 else
7e2b7603 951 return if_lookup_by_index(ifindex, VRF_DEFAULT);
718e3744 952}
8cc4198f 953#endif /* ifaddr_ipv4_table */
718e3744 954
f4e14fdb 955void if_terminate(struct vrf *vrf)
4bd045d5 956{
f4e14fdb 957 struct interface *ifp;
4bd045d5 958
55cd0f61
DS
959 while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
960 ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name);
961
d62a17ae 962 if (ifp->node) {
963 ifp->node->info = NULL;
964 route_unlock_node(ifp->node);
965 }
d62a17ae 966 if_delete(ifp);
967 }
4bd045d5 968}
8ccc7e80 969
d62a17ae 970const char *if_link_type_str(enum zebra_link_type llt)
8ccc7e80 971{
d62a17ae 972 switch (llt) {
8ccc7e80 973#define llts(T,S) case (T): return (S)
d62a17ae 974 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
975 llts(ZEBRA_LLT_ETHER, "Ethernet");
976 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
977 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
978 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
979 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
980 llts(ZEBRA_LLT_ARCNET, "ARCnet");
981 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
982 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
983 llts(ZEBRA_LLT_ATM, "ATM");
984 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
985 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
986 llts(ZEBRA_LLT_EUI64, "EUI-64");
987 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
988 llts(ZEBRA_LLT_SLIP, "SLIP");
989 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
990 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
991 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
992 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
993 llts(ZEBRA_LLT_X25, "CCITT X.25");
994 llts(ZEBRA_LLT_PPP, "PPP");
995 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
996 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
997 llts(ZEBRA_LLT_LAPB, "LAPB");
998 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
999 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1000 llts(ZEBRA_LLT_FRAD, "FRAD");
1001 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1002 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1003 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1004 llts(ZEBRA_LLT_FDDI, "FDDI");
1005 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1006 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1007 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1008 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1009 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1010 llts(ZEBRA_LLT_IRDA, "IrDA");
1011 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1012 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1013 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1014 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1015 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1016 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1017 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1018 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1019 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1020 default:
450971aa 1021 flog_err(EC_LIB_DEVELOPMENT, "Unknown value %d", llt);
d62a17ae 1022 return "Unknown type!";
8ccc7e80 1023#undef llts
d62a17ae 1024 }
1025 return NULL;
8ccc7e80 1026}
16f1b9ee 1027
d62a17ae 1028struct if_link_params *if_link_params_get(struct interface *ifp)
16f1b9ee 1029{
d62a17ae 1030 int i;
16f1b9ee 1031
d62a17ae 1032 if (ifp->link_params != NULL)
1033 return ifp->link_params;
16f1b9ee 1034
d62a17ae 1035 struct if_link_params *iflp =
1036 XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params));
1037 if (iflp == NULL)
1038 return NULL;
16f1b9ee 1039
d62a17ae 1040 /* Set TE metric equal to standard metric */
1041 iflp->te_metric = ifp->metric;
16f1b9ee 1042
d62a17ae 1043 /* Compute default bandwidth based on interface */
1044 iflp->default_bw =
1045 ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1046 * TE_KILO_BIT / TE_BYTE);
16f1b9ee 1047
d62a17ae 1048 /* Set Max, Reservable and Unreserved Bandwidth */
1049 iflp->max_bw = iflp->default_bw;
1050 iflp->max_rsv_bw = iflp->default_bw;
1051 for (i = 0; i < MAX_CLASS_TYPE; i++)
1052 iflp->unrsv_bw[i] = iflp->default_bw;
16f1b9ee 1053
d62a17ae 1054 /* Update Link parameters status */
1055 iflp->lp_status =
1056 LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
16f1b9ee 1057
d62a17ae 1058 /* Finally attach newly created Link Parameters */
1059 ifp->link_params = iflp;
16f1b9ee 1060
d62a17ae 1061 return iflp;
16f1b9ee
OD
1062}
1063
d62a17ae 1064void if_link_params_free(struct interface *ifp)
16f1b9ee 1065{
d62a17ae 1066 if (ifp->link_params == NULL)
1067 return;
1068 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1069 ifp->link_params = NULL;
16f1b9ee 1070}
a4bed468 1071
8f90d89b
RW
1072/* ----------- CLI commands ----------- */
1073
1074/*
1075 * XPath: /frr-interface:lib/interface
1076 */
1077DEFPY_NOSH (interface,
1078 interface_cmd,
1079 "interface IFNAME [vrf NAME$vrfname]",
1080 "Select an interface to configure\n"
1081 "Interface's name\n"
1082 VRF_CMD_HELP_STR)
1083{
1084 char xpath_list[XPATH_MAXLEN];
8f90d89b
RW
1085 vrf_id_t vrf_id;
1086 struct interface *ifp;
1087 int ret;
1088
1089 if (!vrfname)
1090 vrfname = VRF_DEFAULT_NAME;
1091
1092 /*
1093 * This command requires special handling to maintain backward
1094 * compatibility. If a VRF name is not specified, it means we're willing
1095 * to accept any interface with the given name on any VRF. If no
1096 * interface is found, then a new one should be created on the default
1097 * VRF.
1098 */
1099 VRF_GET_ID(vrf_id, vrfname, false);
1100 ifp = if_lookup_by_name_all_vrf(ifname);
1101 if (ifp && ifp->vrf_id != vrf_id) {
1102 struct vrf *vrf;
1103
1104 /*
1105 * Special case 1: a VRF name was specified, but the found
1106 * interface is associated to different VRF. Reject the command.
1107 */
1108 if (vrf_id != VRF_DEFAULT) {
1109 vty_out(vty, "%% interface %s not in %s vrf\n", ifname,
1110 vrfname);
1111 return CMD_WARNING_CONFIG_FAILED;
1112 }
1113
1114 /*
1115 * Special case 2: a VRF name was *not* specified, and the found
1116 * interface is associated to a VRF other than the default one.
1117 * Update vrf_id and vrfname to account for that.
1118 */
1119 vrf = vrf_lookup_by_id(ifp->vrf_id);
1120 assert(vrf);
1121 vrf_id = ifp->vrf_id;
1122 vrfname = vrf->name;
1123 }
1124
1125 snprintf(xpath_list, sizeof(xpath_list),
1126 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname,
1127 vrfname);
1128
a6233bfc
RW
1129 nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, NULL);
1130 ret = nb_cli_apply_changes(vty, xpath_list);
8f90d89b
RW
1131 if (ret == CMD_SUCCESS) {
1132 VTY_PUSH_XPATH(INTERFACE_NODE, xpath_list);
1133
1134 /*
1135 * For backward compatibility with old commands we still need
1136 * to use the qobj infrastructure. This can be removed once
1137 * all interface-level commands are converted to the new
1138 * northbound model.
1139 */
1140 ifp = if_lookup_by_name(ifname, vrf_id);
1141 if (ifp)
1142 VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp);
1143 }
1144
1145 return ret;
1146}
1147
1148DEFPY (no_interface,
1149 no_interface_cmd,
1150 "no interface IFNAME [vrf NAME$vrfname]",
1151 NO_STR
1152 "Delete a pseudo interface's configuration\n"
1153 "Interface's name\n"
1154 VRF_CMD_HELP_STR)
1155{
8f90d89b
RW
1156 if (!vrfname)
1157 vrfname = VRF_DEFAULT_NAME;
1158
95ce849b 1159 nb_cli_enqueue_change(vty, ".", NB_OP_DESTROY, NULL);
8f90d89b 1160
a6233bfc
RW
1161 return nb_cli_apply_changes(
1162 vty, "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1163 ifname, vrfname);
8f90d89b
RW
1164}
1165
1166static void cli_show_interface(struct vty *vty, struct lyd_node *dnode,
1167 bool show_defaults)
1168{
1169 const char *vrf;
1170
1171 vrf = yang_dnode_get_string(dnode, "./vrf");
1172
1173 vty_out(vty, "!\n");
1174 vty_out(vty, "interface %s", yang_dnode_get_string(dnode, "./name"));
1175 if (!strmatch(vrf, VRF_DEFAULT_NAME))
1176 vty_out(vty, " vrf %s", vrf);
1177 vty_out(vty, "\n");
1178}
1179
1180/*
1181 * XPath: /frr-interface:lib/interface/description
1182 */
1183DEFPY (interface_desc,
1184 interface_desc_cmd,
1185 "description LINE...",
1186 "Interface specific description\n"
1187 "Characters describing this interface\n")
1188{
8f90d89b
RW
1189 char *desc;
1190 int ret;
1191
1192 desc = argv_concat(argv, argc, 1);
a6233bfc
RW
1193 nb_cli_enqueue_change(vty, "./description", NB_OP_MODIFY, desc);
1194 ret = nb_cli_apply_changes(vty, NULL);
8f90d89b
RW
1195 XFREE(MTYPE_TMP, desc);
1196
1197 return ret;
1198}
1199
1200DEFPY (no_interface_desc,
1201 no_interface_desc_cmd,
1202 "no description",
1203 NO_STR
1204 "Interface specific description\n")
1205{
95ce849b 1206 nb_cli_enqueue_change(vty, "./description", NB_OP_DESTROY, NULL);
a6233bfc
RW
1207
1208 return nb_cli_apply_changes(vty, NULL);
8f90d89b
RW
1209}
1210
1211static void cli_show_interface_desc(struct vty *vty, struct lyd_node *dnode,
1212 bool show_defaults)
1213{
1214 vty_out(vty, " description %s\n", yang_dnode_get_string(dnode, NULL));
1215}
1216
1217/* Interface autocomplete. */
1218static void if_autocomplete(vector comps, struct cmd_token *token)
1219{
1220 struct interface *ifp;
1221 struct vrf *vrf;
1222
1223 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1224 FOR_ALL_INTERFACES (vrf, ifp) {
1225 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name));
1226 }
1227 }
1228}
1229
1230static const struct cmd_variable_handler if_var_handlers[] = {
1231 {/* "interface NAME" */
1232 .varname = "interface",
1233 .completions = if_autocomplete},
1234 {.tokenname = "IFNAME", .completions = if_autocomplete},
1235 {.tokenname = "INTERFACE", .completions = if_autocomplete},
1236 {.completions = NULL}};
1237
1238void if_cmd_init(void)
1239{
1240 cmd_variable_handler_register(if_var_handlers);
1241
1242 install_element(CONFIG_NODE, &interface_cmd);
1243 install_element(CONFIG_NODE, &no_interface_cmd);
1244
1245 install_default(INTERFACE_NODE);
1246 install_element(INTERFACE_NODE, &interface_desc_cmd);
1247 install_element(INTERFACE_NODE, &no_interface_desc_cmd);
1248}
1249
a4bed468
RW
1250/* ------- Northbound callbacks ------- */
1251
1252/*
1253 * XPath: /frr-interface:lib/interface
1254 */
1255static int lib_interface_create(enum nb_event event,
1256 const struct lyd_node *dnode,
1257 union nb_resource *resource)
1258{
8f90d89b
RW
1259 const char *ifname;
1260 const char *vrfname;
1261 struct vrf *vrf;
1262 struct interface *ifp;
1263
1264 ifname = yang_dnode_get_string(dnode, "./name");
1265 vrfname = yang_dnode_get_string(dnode, "./vrf");
1266
1267 switch (event) {
1268 case NB_EV_VALIDATE:
1269 vrf = vrf_lookup_by_name(vrfname);
1270 if (!vrf) {
1271 zlog_warn("%s: VRF %s doesn't exist", __func__,
1272 vrfname);
1273 return NB_ERR_VALIDATION;
1274 }
1275 if (vrf->vrf_id == VRF_UNKNOWN) {
1276 zlog_warn("%s: VRF %s is not active", __func__,
1277 vrf->name);
1278 return NB_ERR_VALIDATION;
1279 }
72261ecd 1280
b0b97a7f
PG
1281 /* if VRF is netns or not yet known - init for instance
1282 * then assumption is that passed config is exact
1283 * then the user intent was not to use an other iface
1284 */
8f90d89b
RW
1285 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE) {
1286 ifp = if_lookup_by_name_all_vrf(ifname);
1287 if (ifp && ifp->vrf_id != vrf->vrf_id) {
1288 zlog_warn(
1289 "%s: interface %s already exists in another VRF",
1290 __func__, ifp->name);
1291 return NB_ERR_VALIDATION;
1292 }
1293 }
1294 break;
1295 case NB_EV_PREPARE:
1296 case NB_EV_ABORT:
1297 break;
1298 case NB_EV_APPLY:
1299 vrf = vrf_lookup_by_name(vrfname);
1300 assert(vrf);
1301#ifdef SUNOS_5
1302 ifp = if_sunwzebra_get(ifname, vrf->vrf_id);
1303#else
1304 ifp = if_get_by_name(ifname, vrf->vrf_id);
1305#endif /* SUNOS_5 */
1306 yang_dnode_set_entry(dnode, ifp);
1307 break;
1308 }
1309
a4bed468
RW
1310 return NB_OK;
1311}
1312
6a3fdeec
RW
1313static int lib_interface_destroy(enum nb_event event,
1314 const struct lyd_node *dnode)
a4bed468 1315{
8f90d89b
RW
1316 struct interface *ifp;
1317
25c780a3 1318 ifp = yang_dnode_get_entry(dnode, true);
8f90d89b
RW
1319
1320 switch (event) {
1321 case NB_EV_VALIDATE:
1322 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1323 zlog_warn("%s: only inactive interfaces can be deleted",
1324 __func__);
1325 return NB_ERR_VALIDATION;
1326 }
1327 break;
1328 case NB_EV_PREPARE:
1329 case NB_EV_ABORT:
1330 break;
1331 case NB_EV_APPLY:
1332 if_delete(ifp);
1333 break;
1334 }
1335
a4bed468
RW
1336 return NB_OK;
1337}
1338
1339/*
1340 * XPath: /frr-interface:lib/interface/description
1341 */
1342static int lib_interface_description_modify(enum nb_event event,
1343 const struct lyd_node *dnode,
1344 union nb_resource *resource)
1345{
8f90d89b
RW
1346 struct interface *ifp;
1347 const char *description;
1348
1349 if (event != NB_EV_APPLY)
1350 return NB_OK;
1351
25c780a3 1352 ifp = yang_dnode_get_entry(dnode, true);
0a22ddfb 1353 XFREE(MTYPE_TMP, ifp->desc);
8f90d89b
RW
1354 description = yang_dnode_get_string(dnode, NULL);
1355 ifp->desc = XSTRDUP(MTYPE_TMP, description);
1356
a4bed468
RW
1357 return NB_OK;
1358}
1359
6a3fdeec
RW
1360static int lib_interface_description_destroy(enum nb_event event,
1361 const struct lyd_node *dnode)
a4bed468 1362{
8f90d89b
RW
1363 struct interface *ifp;
1364
1365 if (event != NB_EV_APPLY)
1366 return NB_OK;
1367
25c780a3 1368 ifp = yang_dnode_get_entry(dnode, true);
0a22ddfb 1369 XFREE(MTYPE_TMP, ifp->desc);
8f90d89b 1370
a4bed468
RW
1371 return NB_OK;
1372}
1373
1374/* clang-format off */
1375const struct frr_yang_module_info frr_interface_info = {
1376 .name = "frr-interface",
1377 .nodes = {
1378 {
1379 .xpath = "/frr-interface:lib/interface",
1380 .cbs.create = lib_interface_create,
6a3fdeec 1381 .cbs.destroy = lib_interface_destroy,
8f90d89b 1382 .cbs.cli_show = cli_show_interface,
a4bed468
RW
1383 },
1384 {
1385 .xpath = "/frr-interface:lib/interface/description",
1386 .cbs.modify = lib_interface_description_modify,
6a3fdeec 1387 .cbs.destroy = lib_interface_description_destroy,
8f90d89b 1388 .cbs.cli_show = cli_show_interface_desc,
a4bed468
RW
1389 },
1390 {
1391 .xpath = NULL,
1392 },
1393 }
1394};