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