]> git.proxmox.com Git - mirror_frr.git/blame - lib/if.c
*: Convert prefix_free to double pointer
[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)
63265b5c 872 prefix_free(&connected->address);
718e3744 873
d62a17ae 874 if (connected->destination)
63265b5c 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)
63265b5c 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
457ea8d4
GN
962/* count the number of connected addresses that are in the given family */
963unsigned int connected_count_by_family(struct interface *ifp, int family)
964{
965 struct listnode *cnode;
966 struct connected *connected;
967 unsigned int cnt = 0;
968
969 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected))
970 if (connected->address->family == family)
971 cnt++;
972
973 return cnt;
974}
975
d62a17ae 976struct connected *connected_lookup_prefix_exact(struct interface *ifp,
977 struct prefix *p)
38485402 978{
d62a17ae 979 struct listnode *node;
980 struct listnode *next;
981 struct connected *ifc;
38485402 982
d62a17ae 983 for (node = listhead(ifp->connected); node; node = next) {
984 ifc = listgetdata(node);
985 next = node->next;
38485402 986
d62a17ae 987 if (connected_same_prefix(ifc->address, p))
988 return ifc;
989 }
990 return NULL;
38485402
DS
991}
992
d62a17ae 993struct connected *connected_delete_by_prefix(struct interface *ifp,
994 struct prefix *p)
718e3744 995{
d62a17ae 996 struct listnode *node;
997 struct listnode *next;
998 struct connected *ifc;
999
1000 /* In case of same prefix come, replace it with new one. */
1001 for (node = listhead(ifp->connected); node; node = next) {
1002 ifc = listgetdata(node);
1003 next = node->next;
1004
1005 if (connected_same_prefix(ifc->address, p)) {
1006 listnode_delete(ifp->connected, ifc);
1007 return ifc;
1008 }
718e3744 1009 }
d62a17ae 1010 return NULL;
718e3744 1011}
1012
bd40c341 1013/* Find the address on our side that will be used when packets
727d104b 1014 are sent to dst. */
d62a17ae 1015struct connected *connected_lookup_prefix(struct interface *ifp,
1016 struct prefix *addr)
727d104b 1017{
d62a17ae 1018 struct listnode *cnode;
1019 struct connected *c;
1020 struct connected *match;
1021
1022 match = NULL;
1023
1024 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
1025 if (c->address && (c->address->family == addr->family)
1026 && prefix_match(CONNECTED_PREFIX(c), addr)
1027 && (!match
1028 || (c->address->prefixlen > match->address->prefixlen)))
1029 match = c;
1030 }
1031 return match;
727d104b 1032}
1033
d62a17ae 1034struct connected *connected_add_by_prefix(struct interface *ifp,
1035 struct prefix *p,
1036 struct prefix *destination)
4a7aac1b 1037{
d62a17ae 1038 struct connected *ifc;
4a7aac1b 1039
d62a17ae 1040 /* Allocate new connected address. */
1041 ifc = connected_new();
1042 ifc->ifp = ifp;
4a7aac1b 1043
d62a17ae 1044 /* Fetch interface address */
1045 ifc->address = prefix_new();
1046 memcpy(ifc->address, p, sizeof(struct prefix));
4a7aac1b 1047
d62a17ae 1048 /* Fetch dest address */
1049 if (destination) {
1050 ifc->destination = prefix_new();
1051 memcpy(ifc->destination, destination, sizeof(struct prefix));
1052 }
4a7aac1b 1053
d62a17ae 1054 /* Add connected address to the interface. */
1055 listnode_add(ifp->connected, ifc);
1056 return ifc;
4a7aac1b 1057}
1058
667179ca
QY
1059struct connected *connected_get_linklocal(struct interface *ifp)
1060{
1061 struct listnode *n;
1062 struct connected *c = NULL;
1063
1064 for (ALL_LIST_ELEMENTS_RO(ifp->connected, n, c)) {
1065 if (c->address->family == AF_INET6
1066 && IN6_IS_ADDR_LINKLOCAL(&c->address->u.prefix6))
1067 break;
1068 }
1069 return c;
1070}
1071
d62a17ae 1072#if 0 /* this route_table of struct connected's is unused \
1073 * however, it would be good to use a route_table rather than \
1074 * a list.. \
1075 */
718e3744 1076/* Interface looking up by interface's address. */
718e3744 1077/* Interface's IPv4 address reverse lookup table. */
1078struct route_table *ifaddr_ipv4_table;
1079/* struct route_table *ifaddr_ipv6_table; */
1080
8cc4198f 1081static void
718e3744 1082ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
1083{
1084 struct route_node *rn;
1085 struct prefix_ipv4 p;
1086
1087 p.family = AF_INET;
1088 p.prefixlen = IPV4_MAX_PREFIXLEN;
1089 p.prefix = *ifaddr;
1090
1091 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
1092 if (rn)
1093 {
1094 route_unlock_node (rn);
1095 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1096 inet_ntoa (*ifaddr));
1097 return;
1098 }
1099 rn->info = ifp;
1100}
1101
8cc4198f 1102static void
718e3744 1103ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
1104{
1105 struct route_node *rn;
1106 struct prefix_ipv4 p;
1107
1108 p.family = AF_INET;
1109 p.prefixlen = IPV4_MAX_PREFIXLEN;
1110 p.prefix = *ifaddr;
1111
1112 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1113 if (! rn)
1114 {
1115 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1116 inet_ntoa (*ifaddr));
1117 return;
1118 }
1119 rn->info = NULL;
1120 route_unlock_node (rn);
1121 route_unlock_node (rn);
1122}
1123
1124/* Lookup interface by interface's IP address or interface index. */
8cc4198f 1125static struct interface *
b892f1dd 1126ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
718e3744 1127{
1128 struct prefix_ipv4 p;
1129 struct route_node *rn;
1130 struct interface *ifp;
718e3744 1131
1132 if (addr)
1133 {
1134 p.family = AF_INET;
1135 p.prefixlen = IPV4_MAX_PREFIXLEN;
1136 p.prefix = *addr;
1137
1138 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1139 if (! rn)
1140 return NULL;
55cd0f61 1141
718e3744 1142 ifp = rn->info;
1143 route_unlock_node (rn);
1144 return ifp;
1145 }
1146 else
7e2b7603 1147 return if_lookup_by_index(ifindex, VRF_DEFAULT);
718e3744 1148}
8cc4198f 1149#endif /* ifaddr_ipv4_table */
718e3744 1150
f4e14fdb 1151void if_terminate(struct vrf *vrf)
4bd045d5 1152{
f4e14fdb 1153 struct interface *ifp;
4bd045d5 1154
55cd0f61
DS
1155 while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
1156 ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name);
1157
d62a17ae 1158 if (ifp->node) {
1159 ifp->node->info = NULL;
1160 route_unlock_node(ifp->node);
1161 }
d62a17ae 1162 if_delete(ifp);
1163 }
4bd045d5 1164}
8ccc7e80 1165
d62a17ae 1166const char *if_link_type_str(enum zebra_link_type llt)
8ccc7e80 1167{
d62a17ae 1168 switch (llt) {
8ccc7e80 1169#define llts(T,S) case (T): return (S)
d62a17ae 1170 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
1171 llts(ZEBRA_LLT_ETHER, "Ethernet");
1172 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
1173 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
1174 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
1175 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
1176 llts(ZEBRA_LLT_ARCNET, "ARCnet");
1177 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
1178 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
1179 llts(ZEBRA_LLT_ATM, "ATM");
1180 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
1181 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
1182 llts(ZEBRA_LLT_EUI64, "EUI-64");
1183 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
1184 llts(ZEBRA_LLT_SLIP, "SLIP");
1185 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
1186 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
1187 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
1188 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
1189 llts(ZEBRA_LLT_X25, "CCITT X.25");
1190 llts(ZEBRA_LLT_PPP, "PPP");
1191 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
1192 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1193 llts(ZEBRA_LLT_LAPB, "LAPB");
1194 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1195 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1196 llts(ZEBRA_LLT_FRAD, "FRAD");
1197 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1198 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1199 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1200 llts(ZEBRA_LLT_FDDI, "FDDI");
1201 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1202 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1203 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1204 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1205 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1206 llts(ZEBRA_LLT_IRDA, "IrDA");
1207 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1208 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1209 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1210 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1211 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1212 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1213 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1214 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1215 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1216 default:
450971aa 1217 flog_err(EC_LIB_DEVELOPMENT, "Unknown value %d", llt);
d62a17ae 1218 return "Unknown type!";
8ccc7e80 1219#undef llts
d62a17ae 1220 }
1221 return NULL;
8ccc7e80 1222}
16f1b9ee 1223
d62a17ae 1224struct if_link_params *if_link_params_get(struct interface *ifp)
16f1b9ee 1225{
d62a17ae 1226 int i;
16f1b9ee 1227
d62a17ae 1228 if (ifp->link_params != NULL)
1229 return ifp->link_params;
16f1b9ee 1230
d62a17ae 1231 struct if_link_params *iflp =
1232 XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params));
1233 if (iflp == NULL)
1234 return NULL;
16f1b9ee 1235
d62a17ae 1236 /* Set TE metric equal to standard metric */
1237 iflp->te_metric = ifp->metric;
16f1b9ee 1238
d62a17ae 1239 /* Compute default bandwidth based on interface */
1240 iflp->default_bw =
1241 ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1242 * TE_KILO_BIT / TE_BYTE);
16f1b9ee 1243
d62a17ae 1244 /* Set Max, Reservable and Unreserved Bandwidth */
1245 iflp->max_bw = iflp->default_bw;
1246 iflp->max_rsv_bw = iflp->default_bw;
1247 for (i = 0; i < MAX_CLASS_TYPE; i++)
1248 iflp->unrsv_bw[i] = iflp->default_bw;
16f1b9ee 1249
d62a17ae 1250 /* Update Link parameters status */
1251 iflp->lp_status =
1252 LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
16f1b9ee 1253
d62a17ae 1254 /* Finally attach newly created Link Parameters */
1255 ifp->link_params = iflp;
16f1b9ee 1256
d62a17ae 1257 return iflp;
16f1b9ee
OD
1258}
1259
d62a17ae 1260void if_link_params_free(struct interface *ifp)
16f1b9ee 1261{
d62a17ae 1262 if (ifp->link_params == NULL)
1263 return;
1264 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1265 ifp->link_params = NULL;
16f1b9ee 1266}
a4bed468 1267
8f90d89b
RW
1268/* ----------- CLI commands ----------- */
1269
1270/*
1271 * XPath: /frr-interface:lib/interface
1272 */
1273DEFPY_NOSH (interface,
1274 interface_cmd,
e429a2a0 1275 "interface IFNAME [vrf NAME$vrf_name]",
8f90d89b
RW
1276 "Select an interface to configure\n"
1277 "Interface's name\n"
1278 VRF_CMD_HELP_STR)
1279{
1280 char xpath_list[XPATH_MAXLEN];
a36898e7
DS
1281 vrf_id_t vrf_id;
1282 struct interface *ifp;
8f90d89b
RW
1283 int ret;
1284
e429a2a0
IR
1285 if (!vrf_name)
1286 vrf_name = VRF_DEFAULT_NAME;
8f90d89b
RW
1287
1288 /*
1289 * This command requires special handling to maintain backward
1290 * compatibility. If a VRF name is not specified, it means we're willing
1291 * to accept any interface with the given name on any VRF. If no
1292 * interface is found, then a new one should be created on the default
1293 * VRF.
1294 */
e429a2a0 1295 VRF_GET_ID(vrf_id, vrf_name, false);
a36898e7
DS
1296 ifp = if_lookup_by_name_all_vrf(ifname);
1297 if (ifp && ifp->vrf_id != vrf_id) {
1298 struct vrf *vrf;
1299
8f90d89b
RW
1300 /*
1301 * Special case 1: a VRF name was specified, but the found
1302 * interface is associated to different VRF. Reject the command.
1303 */
a36898e7 1304 if (vrf_id != VRF_DEFAULT) {
8f90d89b 1305 vty_out(vty, "%% interface %s not in %s vrf\n", ifname,
e429a2a0 1306 vrf_name);
8f90d89b
RW
1307 return CMD_WARNING_CONFIG_FAILED;
1308 }
1309
1310 /*
1311 * Special case 2: a VRF name was *not* specified, and the found
1312 * interface is associated to a VRF other than the default one.
e429a2a0 1313 * Update vrf_id and vrf_name to account for that.
8f90d89b 1314 */
a36898e7 1315 vrf = vrf_lookup_by_id(ifp->vrf_id);
8f90d89b 1316 assert(vrf);
a36898e7 1317 vrf_id = ifp->vrf_id;
e429a2a0 1318 vrf_name = vrf->name;
8f90d89b
RW
1319 }
1320
1321 snprintf(xpath_list, sizeof(xpath_list),
1322 "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname,
e429a2a0 1323 vrf_name);
8f90d89b 1324
a6233bfc
RW
1325 nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, NULL);
1326 ret = nb_cli_apply_changes(vty, xpath_list);
8f90d89b
RW
1327 if (ret == CMD_SUCCESS) {
1328 VTY_PUSH_XPATH(INTERFACE_NODE, xpath_list);
1329
1330 /*
1331 * For backward compatibility with old commands we still need
1332 * to use the qobj infrastructure. This can be removed once
1333 * all interface-level commands are converted to the new
1334 * northbound model.
1335 */
a36898e7 1336 ifp = if_lookup_by_name(ifname, vrf_id);
8f90d89b
RW
1337 if (ifp)
1338 VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp);
1339 }
1340
1341 return ret;
1342}
1343
1344DEFPY (no_interface,
1345 no_interface_cmd,
e429a2a0 1346 "no interface IFNAME [vrf NAME$vrf_name]",
8f90d89b
RW
1347 NO_STR
1348 "Delete a pseudo interface's configuration\n"
1349 "Interface's name\n"
1350 VRF_CMD_HELP_STR)
1351{
e429a2a0
IR
1352 if (!vrf_name)
1353 vrf_name = VRF_DEFAULT_NAME;
8f90d89b 1354
95ce849b 1355 nb_cli_enqueue_change(vty, ".", NB_OP_DESTROY, NULL);
8f90d89b 1356
a6233bfc
RW
1357 return nb_cli_apply_changes(
1358 vty, "/frr-interface:lib/interface[name='%s'][vrf='%s']",
e429a2a0 1359 ifname, vrf_name);
8f90d89b
RW
1360}
1361
1362static void cli_show_interface(struct vty *vty, struct lyd_node *dnode,
1363 bool show_defaults)
1364{
1365 const char *vrf;
1366
1367 vrf = yang_dnode_get_string(dnode, "./vrf");
1368
1369 vty_out(vty, "!\n");
1370 vty_out(vty, "interface %s", yang_dnode_get_string(dnode, "./name"));
1371 if (!strmatch(vrf, VRF_DEFAULT_NAME))
1372 vty_out(vty, " vrf %s", vrf);
1373 vty_out(vty, "\n");
1374}
1375
1376/*
1377 * XPath: /frr-interface:lib/interface/description
1378 */
1379DEFPY (interface_desc,
1380 interface_desc_cmd,
1381 "description LINE...",
1382 "Interface specific description\n"
1383 "Characters describing this interface\n")
1384{
8f90d89b
RW
1385 char *desc;
1386 int ret;
1387
1388 desc = argv_concat(argv, argc, 1);
a6233bfc
RW
1389 nb_cli_enqueue_change(vty, "./description", NB_OP_MODIFY, desc);
1390 ret = nb_cli_apply_changes(vty, NULL);
8f90d89b
RW
1391 XFREE(MTYPE_TMP, desc);
1392
1393 return ret;
1394}
1395
1396DEFPY (no_interface_desc,
1397 no_interface_desc_cmd,
1398 "no description",
1399 NO_STR
1400 "Interface specific description\n")
1401{
95ce849b 1402 nb_cli_enqueue_change(vty, "./description", NB_OP_DESTROY, NULL);
a6233bfc
RW
1403
1404 return nb_cli_apply_changes(vty, NULL);
8f90d89b
RW
1405}
1406
1407static void cli_show_interface_desc(struct vty *vty, struct lyd_node *dnode,
1408 bool show_defaults)
1409{
1410 vty_out(vty, " description %s\n", yang_dnode_get_string(dnode, NULL));
1411}
1412
1413/* Interface autocomplete. */
1414static void if_autocomplete(vector comps, struct cmd_token *token)
1415{
1416 struct interface *ifp;
1417 struct vrf *vrf;
1418
1419 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1420 FOR_ALL_INTERFACES (vrf, ifp) {
1421 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name));
1422 }
1423 }
1424}
1425
1426static const struct cmd_variable_handler if_var_handlers[] = {
1427 {/* "interface NAME" */
1428 .varname = "interface",
1429 .completions = if_autocomplete},
1430 {.tokenname = "IFNAME", .completions = if_autocomplete},
1431 {.tokenname = "INTERFACE", .completions = if_autocomplete},
1432 {.completions = NULL}};
1433
1434void if_cmd_init(void)
1435{
1436 cmd_variable_handler_register(if_var_handlers);
1437
1438 install_element(CONFIG_NODE, &interface_cmd);
1439 install_element(CONFIG_NODE, &no_interface_cmd);
1440
1441 install_default(INTERFACE_NODE);
1442 install_element(INTERFACE_NODE, &interface_desc_cmd);
1443 install_element(INTERFACE_NODE, &no_interface_desc_cmd);
1444}
1445
138c5a74
DS
1446void if_zapi_callbacks(int (*create)(struct interface *ifp),
1447 int (*up)(struct interface *ifp),
1448 int (*down)(struct interface *ifp),
1449 int (*destroy)(struct interface *ifp))
1450{
1451 ifp_master.create_hook = create;
1452 ifp_master.up_hook = up;
1453 ifp_master.down_hook = down;
1454 ifp_master.destroy_hook = destroy;
1455}
1456
a4bed468
RW
1457/* ------- Northbound callbacks ------- */
1458
1459/*
1460 * XPath: /frr-interface:lib/interface
1461 */
1462static int lib_interface_create(enum nb_event event,
1463 const struct lyd_node *dnode,
1464 union nb_resource *resource)
1465{
8f90d89b
RW
1466 const char *ifname;
1467 const char *vrfname;
1468 struct vrf *vrf;
a36898e7 1469 struct interface *ifp;
8f90d89b
RW
1470
1471 ifname = yang_dnode_get_string(dnode, "./name");
1472 vrfname = yang_dnode_get_string(dnode, "./vrf");
1473
1474 switch (event) {
1475 case NB_EV_VALIDATE:
1476 vrf = vrf_lookup_by_name(vrfname);
1477 if (!vrf) {
1478 zlog_warn("%s: VRF %s doesn't exist", __func__,
1479 vrfname);
1480 return NB_ERR_VALIDATION;
1481 }
a36898e7
DS
1482 if (vrf->vrf_id == VRF_UNKNOWN) {
1483 zlog_warn("%s: VRF %s is not active", __func__,
1484 vrf->name);
1485 return NB_ERR_VALIDATION;
1486 }
72261ecd 1487
b0b97a7f
PG
1488 /* if VRF is netns or not yet known - init for instance
1489 * then assumption is that passed config is exact
1490 * then the user intent was not to use an other iface
1491 */
8f90d89b
RW
1492 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE) {
1493 ifp = if_lookup_by_name_all_vrf(ifname);
a36898e7 1494 if (ifp && ifp->vrf_id != vrf->vrf_id) {
8f90d89b
RW
1495 zlog_warn(
1496 "%s: interface %s already exists in another VRF",
1497 __func__, ifp->name);
1498 return NB_ERR_VALIDATION;
1499 }
1500 }
1501 break;
1502 case NB_EV_PREPARE:
1503 case NB_EV_ABORT:
1504 break;
1505 case NB_EV_APPLY:
1506 vrf = vrf_lookup_by_name(vrfname);
1507 assert(vrf);
1508#ifdef SUNOS_5
a36898e7 1509 ifp = if_sunwzebra_get(ifname, vrf->vrf_id);
8f90d89b 1510#else
a36898e7 1511 ifp = if_get_by_name(ifname, vrf->vrf_id);
8f90d89b 1512#endif /* SUNOS_5 */
1d311a05
DS
1513
1514 ifp->configured = true;
ccd43ada 1515 nb_running_set_entry(dnode, ifp);
8f90d89b
RW
1516 break;
1517 }
1518
a4bed468
RW
1519 return NB_OK;
1520}
1521
6a3fdeec
RW
1522static int lib_interface_destroy(enum nb_event event,
1523 const struct lyd_node *dnode)
a4bed468 1524{
8f90d89b
RW
1525 struct interface *ifp;
1526
8f90d89b
RW
1527
1528 switch (event) {
1529 case NB_EV_VALIDATE:
ccd43ada 1530 ifp = nb_running_get_entry(dnode, NULL, true);
8f90d89b
RW
1531 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1532 zlog_warn("%s: only inactive interfaces can be deleted",
1533 __func__);
1534 return NB_ERR_VALIDATION;
1535 }
1536 break;
1537 case NB_EV_PREPARE:
1538 case NB_EV_ABORT:
1539 break;
1540 case NB_EV_APPLY:
ccd43ada 1541 ifp = nb_running_unset_entry(dnode);
1d311a05
DS
1542
1543 ifp->configured = false;
8f90d89b
RW
1544 if_delete(ifp);
1545 break;
1546 }
1547
a4bed468
RW
1548 return NB_OK;
1549}
1550
f88647ef
QY
1551/*
1552 * XPath: /frr-interface:lib/interface
1553 */
1554static const void *lib_interface_get_next(const void *parent_list_entry,
1555 const void *list_entry)
1556{
1557 struct vrf *vrf;
1558 struct interface *pif = (struct interface *)list_entry;
1559
1560 if (list_entry == NULL) {
1561 vrf = RB_MIN(vrf_name_head, &vrfs_by_name);
1562 assert(vrf);
1563 pif = RB_MIN(if_name_head, &vrf->ifaces_by_name);
1564 } else {
1565 vrf = vrf_lookup_by_id(pif->vrf_id);
1566 pif = RB_NEXT(if_name_head, pif);
1567 /* if no more interfaces, switch to next vrf */
1568 while (pif == NULL) {
1569 vrf = RB_NEXT(vrf_name_head, vrf);
1570 if (!vrf)
1571 return NULL;
1572 pif = RB_MIN(if_name_head, &vrf->ifaces_by_name);
1573 }
1574 }
1575
1576 return pif;
1577}
1578
1579static int lib_interface_get_keys(const void *list_entry,
1580 struct yang_list_keys *keys)
1581{
1582 const struct interface *ifp = list_entry;
1583
1584 struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id);
1585
1586 assert(vrf);
1587
1588 keys->num = 2;
1589 strlcpy(keys->key[0], ifp->name, sizeof(keys->key[0]));
1590 strlcpy(keys->key[1], vrf->name, sizeof(keys->key[1]));
1591
1592 return NB_OK;
1593}
1594
1595static const void *lib_interface_lookup_entry(const void *parent_list_entry,
1596 const struct yang_list_keys *keys)
1597{
1598 const char *ifname = keys->key[0];
1599 const char *vrfname = keys->key[1];
1600 struct vrf *vrf = vrf_lookup_by_name(vrfname);
1601
68a4422d 1602 return vrf ? if_lookup_by_name(ifname, vrf->vrf_id) : NULL;
f88647ef
QY
1603}
1604
a4bed468
RW
1605/*
1606 * XPath: /frr-interface:lib/interface/description
1607 */
1608static int lib_interface_description_modify(enum nb_event event,
1609 const struct lyd_node *dnode,
1610 union nb_resource *resource)
1611{
8f90d89b
RW
1612 struct interface *ifp;
1613 const char *description;
1614
1615 if (event != NB_EV_APPLY)
1616 return NB_OK;
1617
ccd43ada 1618 ifp = nb_running_get_entry(dnode, NULL, true);
0a22ddfb 1619 XFREE(MTYPE_TMP, ifp->desc);
8f90d89b
RW
1620 description = yang_dnode_get_string(dnode, NULL);
1621 ifp->desc = XSTRDUP(MTYPE_TMP, description);
1622
a4bed468
RW
1623 return NB_OK;
1624}
1625
6a3fdeec
RW
1626static int lib_interface_description_destroy(enum nb_event event,
1627 const struct lyd_node *dnode)
a4bed468 1628{
8f90d89b
RW
1629 struct interface *ifp;
1630
1631 if (event != NB_EV_APPLY)
1632 return NB_OK;
1633
ccd43ada 1634 ifp = nb_running_get_entry(dnode, NULL, true);
0a22ddfb 1635 XFREE(MTYPE_TMP, ifp->desc);
8f90d89b 1636
a4bed468
RW
1637 return NB_OK;
1638}
1639
1640/* clang-format off */
1641const struct frr_yang_module_info frr_interface_info = {
1642 .name = "frr-interface",
1643 .nodes = {
1644 {
1645 .xpath = "/frr-interface:lib/interface",
53280f93
DL
1646 .cbs = {
1647 .create = lib_interface_create,
1648 .destroy = lib_interface_destroy,
1649 .cli_show = cli_show_interface,
f88647ef
QY
1650 .get_next = lib_interface_get_next,
1651 .get_keys = lib_interface_get_keys,
1652 .lookup_entry = lib_interface_lookup_entry,
53280f93 1653 },
a4bed468
RW
1654 },
1655 {
1656 .xpath = "/frr-interface:lib/interface/description",
53280f93
DL
1657 .cbs = {
1658 .modify = lib_interface_description_modify,
1659 .destroy = lib_interface_description_destroy,
1660 .cli_show = cli_show_interface_desc,
1661 },
a4bed468
RW
1662 },
1663 {
1664 .xpath = NULL,
1665 },
1666 }
1667};