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