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