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