]> git.proxmox.com Git - mirror_frr.git/blame - lib/if.c
zebra: Allow json output to give a bit more data
[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"
6b0655a2 37
d62a17ae 38DEFINE_MTYPE(LIB, IF, "Interface")
39DEFINE_MTYPE_STATIC(LIB, CONNECTED, "Connected")
40DEFINE_MTYPE_STATIC(LIB, NBR_CONNECTED, "Neighbor Connected")
41DEFINE_MTYPE(LIB, CONNECTED_LABEL, "Connected interface label")
42DEFINE_MTYPE_STATIC(LIB, IF_LINK_PARAMS, "Informational Link Parameters")
4a1ab8e4 43
f4e14fdb 44static int if_cmp_func(const struct interface *, const struct interface *);
ff880b78
RW
45static int if_cmp_index_func(const struct interface *ifp1,
46 const struct interface *ifp2);
f4e14fdb 47RB_GENERATE(if_name_head, interface, name_entry, if_cmp_func);
ff880b78 48RB_GENERATE(if_index_head, interface, index_entry, if_cmp_index_func);
f4e14fdb 49
e80e7cce
DL
50DEFINE_QOBJ_TYPE(interface)
51
996c9314
LB
52DEFINE_HOOK(if_add, (struct interface * ifp), (ifp))
53DEFINE_KOOH(if_del, (struct interface * ifp), (ifp))
ce19a04a 54
8736158a 55/* List of interfaces in only the default VRF */
244c1cdc 56int ptm_enable = 0;
718e3744 57
3a0391a9 58/* Compare interface names, returning an integer greater than, equal to, or
59 * less than 0, (following the strcmp convention), according to the
60 * relationship between ifp1 and ifp2. Interface names consist of an
61 * alphabetic prefix and a numeric suffix. The primary sort key is
62 * lexicographic by name, and then numeric by number. No number sorts
63 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
64 * devpty0, de0 < del0
d62a17ae 65 */
66int if_cmp_name_func(char *p1, char *p2)
106d2fd5 67{
d62a17ae 68 unsigned int l1, l2;
69 long int x1, x2;
70 int res;
71
72 while (*p1 && *p2) {
73 /* look up to any number */
74 l1 = strcspn(p1, "0123456789");
75 l2 = strcspn(p2, "0123456789");
76
77 /* name lengths are different -> compare names */
78 if (l1 != l2)
79 return (strcmp(p1, p2));
80
81 /* Note that this relies on all numbers being less than all
82 * letters, so
83 * that de0 < del0.
84 */
85 res = strncmp(p1, p2, l1);
86
87 /* names are different -> compare them */
88 if (res)
89 return res;
90
91 /* with identical name part, go to numeric part */
92 p1 += l1;
93 p2 += l1;
94
c9cbbb40
RW
95 if (!*p1 && !*p2)
96 return 0;
d62a17ae 97 if (!*p1)
98 return -1;
99 if (!*p2)
100 return 1;
101
102 x1 = strtol(p1, &p1, 10);
103 x2 = strtol(p2, &p2, 10);
104
105 /* let's compare numbers now */
106 if (x1 < x2)
107 return -1;
108 if (x1 > x2)
109 return 1;
110
111 /* numbers were equal, lets do it again..
112 (it happens with name like "eth123.456:789") */
113 }
114 if (*p1)
115 return 1;
116 if (*p2)
117 return -1;
118 return 0;
106d2fd5 119}
120
f4e14fdb
RW
121static int if_cmp_func(const struct interface *ifp1,
122 const struct interface *ifp2)
974fc9d2 123{
f4e14fdb 124 return if_cmp_name_func((char *)ifp1->name, (char *)ifp2->name);
974fc9d2
DS
125}
126
ff880b78
RW
127static int if_cmp_index_func(const struct interface *ifp1,
128 const struct interface *ifp2)
129{
130 return ifp1->ifindex - ifp2->ifindex;
131}
132
718e3744 133/* Create new interface structure. */
bcc24579 134struct interface *if_create(const char *name, vrf_id_t vrf_id)
718e3744 135{
f4e14fdb 136 struct vrf *vrf = vrf_get(vrf_id, NULL);
d62a17ae 137 struct interface *ifp;
d62a17ae 138
139 ifp = XCALLOC(MTYPE_IF, sizeof(struct interface));
140 ifp->ifindex = IFINDEX_INTERNAL;
141
142 assert(name);
bcc24579 143 strlcpy(ifp->name, name, sizeof(ifp->name));
d62a17ae 144 ifp->vrf_id = vrf_id;
ff880b78 145 IFNAME_RB_INSERT(vrf, ifp);
d62a17ae 146 ifp->connected = list_new();
147 ifp->connected->del = (void (*)(void *))connected_free;
148
149 ifp->nbr_connected = list_new();
150 ifp->nbr_connected->del = (void (*)(void *))nbr_connected_free;
151
152 /* Enable Link-detection by default */
153 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
154
155 QOBJ_REG(ifp, interface);
996c9314 156 hook_call(if_add, ifp);
d62a17ae 157 return ifp;
718e3744 158}
159
f93e3f69 160/* Create new interface structure. */
d62a17ae 161void if_update_to_new_vrf(struct interface *ifp, vrf_id_t vrf_id)
f93e3f69 162{
f4e14fdb 163 struct vrf *vrf;
d62a17ae 164
165 /* remove interface from old master vrf list */
f4e14fdb 166 vrf = vrf_lookup_by_id(ifp->vrf_id);
ff880b78
RW
167 if (vrf) {
168 IFNAME_RB_REMOVE(vrf, ifp);
169 if (ifp->ifindex != IFINDEX_INTERNAL)
170 IFINDEX_RB_REMOVE(vrf, ifp);
171 }
d62a17ae 172
173 ifp->vrf_id = vrf_id;
f4e14fdb 174 vrf = vrf_get(ifp->vrf_id, NULL);
ff880b78
RW
175
176 IFNAME_RB_INSERT(vrf, ifp);
177 if (ifp->ifindex != IFINDEX_INTERNAL)
178 IFINDEX_RB_INSERT(vrf, ifp);
f93e3f69
DS
179}
180
181
d2fc8896 182/* Delete interface structure. */
d62a17ae 183void if_delete_retain(struct interface *ifp)
718e3744 184{
996c9314 185 hook_call(if_del, ifp);
d62a17ae 186 QOBJ_UNREG(ifp);
e80e7cce 187
d62a17ae 188 /* Free connected address list */
189 list_delete_all_node(ifp->connected);
a80beece 190
d62a17ae 191 /* Free connected nbr address list */
192 list_delete_all_node(ifp->nbr_connected);
d2fc8896 193}
194
195/* Delete and free interface structure. */
d62a17ae 196void if_delete(struct interface *ifp)
d2fc8896 197{
5b8524f5
RW
198 struct vrf *vrf;
199
200 vrf = vrf_lookup_by_id(ifp->vrf_id);
201 assert(vrf);
f4e14fdb 202
ff880b78
RW
203 IFNAME_RB_REMOVE(vrf, ifp);
204 if (ifp->ifindex != IFINDEX_INTERNAL)
205 IFINDEX_RB_REMOVE(vrf, ifp);
d2fc8896 206
d62a17ae 207 if_delete_retain(ifp);
718e3744 208
acdf5e25
DS
209 list_delete_and_null(&ifp->connected);
210 list_delete_and_null(&ifp->nbr_connected);
2dd04c5d 211
d62a17ae 212 if_link_params_free(ifp);
16f1b9ee 213
c7974c0f
DS
214 if (ifp->desc)
215 XFREE(MTYPE_TMP, ifp->desc);
216
d62a17ae 217 XFREE(MTYPE_IF, ifp);
718e3744 218}
219
718e3744 220/* Interface existance check by index. */
d62a17ae 221struct interface *if_lookup_by_index(ifindex_t ifindex, vrf_id_t vrf_id)
718e3744 222{
5b8524f5 223 struct vrf *vrf;
ff880b78 224 struct interface if_tmp;
f4e14fdb 225
5b8524f5
RW
226 vrf = vrf_lookup_by_id(vrf_id);
227 if (!vrf)
228 return NULL;
229
ff880b78
RW
230 if_tmp.ifindex = ifindex;
231 return RB_FIND(if_index_head, &vrf->ifaces_by_index, &if_tmp);
718e3744 232}
233
d62a17ae 234const char *ifindex2ifname(ifindex_t ifindex, vrf_id_t vrf_id)
718e3744 235{
d62a17ae 236 struct interface *ifp;
718e3744 237
d62a17ae 238 return ((ifp = if_lookup_by_index(ifindex, vrf_id)) != NULL)
239 ? ifp->name
240 : "unknown";
d2fc8896 241}
242
d62a17ae 243ifindex_t ifname2ifindex(const char *name, vrf_id_t vrf_id)
d2fc8896 244{
d62a17ae 245 struct interface *ifp;
d2fc8896 246
d62a17ae 247 return ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
248 ? ifp->ifindex
249 : IFINDEX_INTERNAL;
718e3744 250}
251
252/* Interface existance check by interface name. */
d62a17ae 253struct interface *if_lookup_by_name(const char *name, vrf_id_t vrf_id)
718e3744 254{
f4e14fdb
RW
255 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
256 struct interface if_tmp;
d62a17ae 257
5b8524f5
RW
258 if (!vrf || !name
259 || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
bcc24579 260 return NULL;
f8962871 261
f4e14fdb
RW
262 strlcpy(if_tmp.name, name, sizeof(if_tmp.name));
263 return RB_FIND(if_name_head, &vrf->ifaces_by_name, &if_tmp);
f8962871
DS
264}
265
bcc24579 266struct interface *if_lookup_by_name_all_vrf(const char *name)
a349198f 267{
bcc24579 268 struct vrf *vrf;
d62a17ae 269 struct interface *ifp;
a349198f 270
bcc24579 271 if (!name || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
d62a17ae 272 return NULL;
a349198f 273
bcc24579
RW
274 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
275 ifp = if_lookup_by_name(name, vrf->vrf_id);
276 if (ifp)
d62a17ae 277 return ifp;
278 }
bcc24579 279
d62a17ae 280 return NULL;
a349198f 281}
282
718e3744 283/* Lookup interface by IPv4 address. */
d62a17ae 284struct interface *if_lookup_exact_address(void *src, int family,
285 vrf_id_t vrf_id)
718e3744 286{
f4e14fdb 287 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 288 struct listnode *cnode;
289 struct interface *ifp;
290 struct prefix *p;
291 struct connected *c;
292
451fda4f 293 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 294 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
295 p = c->address;
296
297 if (p && (p->family == family)) {
298 if (family == AF_INET) {
299 if (IPV4_ADDR_SAME(
300 &p->u.prefix4,
301 (struct in_addr *)src))
302 return ifp;
303 } else if (family == AF_INET6) {
304 if (IPV6_ADDR_SAME(
305 &p->u.prefix6,
306 (struct in6_addr *)src))
307 return ifp;
308 }
309 }
0aabccc0 310 }
718e3744 311 }
d62a17ae 312 return NULL;
718e3744 313}
314
315/* Lookup interface by IPv4 address. */
d62a17ae 316struct connected *if_lookup_address(void *matchaddr, int family,
317 vrf_id_t vrf_id)
718e3744 318{
f4e14fdb 319 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 320 struct prefix addr;
321 int bestlen = 0;
322 struct listnode *cnode;
323 struct interface *ifp;
324 struct connected *c;
325 struct connected *match;
326
327 if (family == AF_INET) {
328 addr.family = AF_INET;
329 addr.u.prefix4 = *((struct in_addr *)matchaddr);
330 addr.prefixlen = IPV4_MAX_BITLEN;
331 } else if (family == AF_INET6) {
332 addr.family = AF_INET6;
333 addr.u.prefix6 = *((struct in6_addr *)matchaddr);
334 addr.prefixlen = IPV6_MAX_BITLEN;
335 }
718e3744 336
d62a17ae 337 match = NULL;
718e3744 338
451fda4f 339 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 340 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
341 if (c->address && (c->address->family == AF_INET)
342 && prefix_match(CONNECTED_PREFIX(c), &addr)
343 && (c->address->prefixlen > bestlen)) {
344 bestlen = c->address->prefixlen;
345 match = c;
346 }
347 }
718e3744 348 }
d62a17ae 349 return match;
718e3744 350}
351
b81e97a8 352/* Lookup interface by prefix */
d62a17ae 353struct interface *if_lookup_prefix(struct prefix *prefix, vrf_id_t vrf_id)
b81e97a8 354{
f4e14fdb 355 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 356 struct listnode *cnode;
357 struct interface *ifp;
358 struct connected *c;
359
451fda4f 360 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 361 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
362 if (prefix_cmp(c->address, prefix) == 0) {
363 return ifp;
364 }
365 }
366 }
367 return NULL;
b81e97a8
DD
368}
369
718e3744 370/* Get interface by name if given name interface doesn't exist create
371 one. */
bcc24579 372struct interface *if_get_by_name(const char *name, vrf_id_t vrf_id, int vty)
718e3744 373{
d62a17ae 374 struct interface *ifp;
718e3744 375
e9e9b115
PG
376 ifp = if_lookup_by_name(name, vrf_id);
377 if (ifp)
378 return ifp;
379 /* Not Found on same VRF. If the interface command
380 * was entered in vty without a VRF (passed as VRF_DEFAULT),
381 * accept the ifp we found. If a vrf was entered and there is
382 * a mismatch, reject it if from vty.
383 */
bcc24579 384 ifp = if_lookup_by_name_all_vrf(name);
e9e9b115
PG
385 if (!ifp)
386 return if_create(name, vrf_id);
387 if (vty) {
388 if (vrf_id == VRF_DEFAULT)
bcc24579 389 return ifp;
e9e9b115 390 return NULL;
85f9da7f 391 }
e9e9b115
PG
392 /* if vrf backend uses NETNS, then
393 * this should not be considered as an update
394 * then create the new interface
395 */
3d4c0b49
PG
396 if (ifp->vrf_id != vrf_id && vrf_is_mapped_on_netns(
397 vrf_lookup_by_id(vrf_id)))
e9e9b115
PG
398 return if_create(name, vrf_id);
399 /* If it came from the kernel
400 * or by way of zclient, believe it and update
401 * the ifp accordingly.
402 */
403 if_update_to_new_vrf(ifp, vrf_id);
404 return ifp;
8736158a
FL
405}
406
ff880b78
RW
407void if_set_index(struct interface *ifp, ifindex_t ifindex)
408{
5b8524f5
RW
409 struct vrf *vrf;
410
411 vrf = vrf_lookup_by_id(ifp->vrf_id);
412 assert(vrf);
ff880b78
RW
413
414 if (ifp->ifindex == ifindex)
415 return;
416
417 if (ifp->ifindex != IFINDEX_INTERNAL)
418 IFINDEX_RB_REMOVE(vrf, ifp)
419
420 ifp->ifindex = ifindex;
421
422 if (ifp->ifindex != IFINDEX_INTERNAL)
423 IFINDEX_RB_INSERT(vrf, ifp)
424}
425
718e3744 426/* Does interface up ? */
d62a17ae 427int if_is_up(struct interface *ifp)
718e3744 428{
d62a17ae 429 return ifp->flags & IFF_UP;
718e3744 430}
431
2e3b2e47 432/* Is interface running? */
d62a17ae 433int if_is_running(struct interface *ifp)
2e3b2e47 434{
d62a17ae 435 return ifp->flags & IFF_RUNNING;
2e3b2e47 436}
437
438/* Is the interface operative, eg. either UP & RUNNING
244c1cdc
DS
439 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
440 if ptm checking is enabled, then ptm check has passed */
d62a17ae 441int if_is_operative(struct interface *ifp)
2e3b2e47 442{
d62a17ae 443 return ((ifp->flags & IFF_UP)
444 && (((ifp->flags & IFF_RUNNING)
445 && (ifp->ptm_status || !ifp->ptm_enable))
446 || !CHECK_FLAG(ifp->status,
447 ZEBRA_INTERFACE_LINKDETECTION)));
244c1cdc
DS
448}
449
450/* Is the interface operative, eg. either UP & RUNNING
451 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
d62a17ae 452int if_is_no_ptm_operative(struct interface *ifp)
244c1cdc 453{
d62a17ae 454 return ((ifp->flags & IFF_UP)
455 && ((ifp->flags & IFF_RUNNING)
456 || !CHECK_FLAG(ifp->status,
457 ZEBRA_INTERFACE_LINKDETECTION)));
2e3b2e47 458}
459
718e3744 460/* Is this loopback interface ? */
d62a17ae 461int if_is_loopback(struct interface *ifp)
718e3744 462{
d62a17ae 463 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
464 * but Y on platform N?
465 */
466 return (ifp->flags & (IFF_LOOPBACK | IFF_NOXMIT | IFF_VIRTUAL));
718e3744 467}
468
0c74bbe0
CS
469/* Check interface is VRF */
470int if_is_vrf(struct interface *ifp)
471{
472 return CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
473}
474
fec4ca19
DS
475bool if_is_loopback_or_vrf(struct interface *ifp)
476{
477 if (if_is_loopback(ifp) || if_is_vrf(ifp))
478 return true;
479
480 return false;
481}
482
718e3744 483/* Does this interface support broadcast ? */
d62a17ae 484int if_is_broadcast(struct interface *ifp)
718e3744 485{
d62a17ae 486 return ifp->flags & IFF_BROADCAST;
718e3744 487}
488
489/* Does this interface support broadcast ? */
d62a17ae 490int if_is_pointopoint(struct interface *ifp)
718e3744 491{
d62a17ae 492 return ifp->flags & IFF_POINTOPOINT;
718e3744 493}
494
495/* Does this interface support multicast ? */
d62a17ae 496int if_is_multicast(struct interface *ifp)
718e3744 497{
d62a17ae 498 return ifp->flags & IFF_MULTICAST;
718e3744 499}
500
501/* Printout flag information into log */
d62a17ae 502const char *if_flag_dump(unsigned long flag)
718e3744 503{
d62a17ae 504 int separator = 0;
505 static char logbuf[BUFSIZ];
506
507#define IFF_OUT_LOG(X, STR) \
508 if (flag & (X)) { \
509 if (separator) \
510 strlcat(logbuf, ",", BUFSIZ); \
511 else \
512 separator = 1; \
513 strlcat(logbuf, STR, BUFSIZ); \
514 }
718e3744 515
d62a17ae 516 strlcpy(logbuf, "<", BUFSIZ);
517 IFF_OUT_LOG(IFF_UP, "UP");
518 IFF_OUT_LOG(IFF_BROADCAST, "BROADCAST");
519 IFF_OUT_LOG(IFF_DEBUG, "DEBUG");
520 IFF_OUT_LOG(IFF_LOOPBACK, "LOOPBACK");
521 IFF_OUT_LOG(IFF_POINTOPOINT, "POINTOPOINT");
522 IFF_OUT_LOG(IFF_NOTRAILERS, "NOTRAILERS");
523 IFF_OUT_LOG(IFF_RUNNING, "RUNNING");
524 IFF_OUT_LOG(IFF_NOARP, "NOARP");
525 IFF_OUT_LOG(IFF_PROMISC, "PROMISC");
526 IFF_OUT_LOG(IFF_ALLMULTI, "ALLMULTI");
527 IFF_OUT_LOG(IFF_OACTIVE, "OACTIVE");
528 IFF_OUT_LOG(IFF_SIMPLEX, "SIMPLEX");
529 IFF_OUT_LOG(IFF_LINK0, "LINK0");
530 IFF_OUT_LOG(IFF_LINK1, "LINK1");
531 IFF_OUT_LOG(IFF_LINK2, "LINK2");
532 IFF_OUT_LOG(IFF_MULTICAST, "MULTICAST");
533 IFF_OUT_LOG(IFF_NOXMIT, "NOXMIT");
534 IFF_OUT_LOG(IFF_NORTEXCH, "NORTEXCH");
535 IFF_OUT_LOG(IFF_VIRTUAL, "VIRTUAL");
536 IFF_OUT_LOG(IFF_IPV4, "IPv4");
537 IFF_OUT_LOG(IFF_IPV6, "IPv6");
538
539 strlcat(logbuf, ">", BUFSIZ);
540
541 return logbuf;
630c97ce 542#undef IFF_OUT_LOG
718e3744 543}
544
545/* For debugging */
d62a17ae 546static void if_dump(const struct interface *ifp)
718e3744 547{
d62a17ae 548 struct listnode *node;
549 struct connected *c __attribute__((unused));
550
551 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, c))
552 zlog_info(
553 "Interface %s vrf %u index %d metric %d mtu %d "
554 "mtu6 %d %s",
555 ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric,
556 ifp->mtu, ifp->mtu6, if_flag_dump(ifp->flags));
718e3744 557}
558
559/* Interface printing for all interface. */
d62a17ae 560void if_dump_all(void)
718e3744 561{
d62a17ae 562 struct vrf *vrf;
f4e14fdb 563 void *ifp;
d62a17ae 564
a2addae8 565 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
451fda4f 566 FOR_ALL_INTERFACES (vrf, ifp)
f4e14fdb 567 if_dump(ifp);
718e3744 568}
569
6147e2c6 570DEFUN (interface_desc,
718e3744 571 interface_desc_cmd,
fcbee690 572 "description LINE...",
718e3744 573 "Interface specific description\n"
574 "Characters describing this interface\n")
575{
d62a17ae 576 int idx_line = 1;
577 VTY_DECLVAR_CONTEXT(interface, ifp);
718e3744 578
d62a17ae 579 if (ifp->desc)
580 XFREE(MTYPE_TMP, ifp->desc);
581 ifp->desc = argv_concat(argv, argc, idx_line);
718e3744 582
d62a17ae 583 return CMD_SUCCESS;
718e3744 584}
585
6147e2c6 586DEFUN (no_interface_desc,
718e3744 587 no_interface_desc_cmd,
588 "no description",
589 NO_STR
590 "Interface specific description\n")
591{
d62a17ae 592 VTY_DECLVAR_CONTEXT(interface, ifp);
718e3744 593
d62a17ae 594 if (ifp->desc)
595 XFREE(MTYPE_TMP, ifp->desc);
596 ifp->desc = NULL;
718e3744 597
d62a17ae 598 return CMD_SUCCESS;
718e3744 599}
6b0655a2 600
98954844
PJ
601#ifdef SUNOS_5
602/* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
603 * a seperate struct interface for each logical interface, so config
604 * file may be full of 'interface fooX:Y'. Solaris however does not
605 * expose logical interfaces via PF_ROUTE, so trying to track logical
606 * interfaces can be fruitless, for that reason Quagga only tracks
607 * the primary IP interface.
608 *
609 * We try accomodate SUNWzebra by:
610 * - looking up the interface name, to see whether it exists, if so
611 * its useable
612 * - for protocol daemons, this could only because zebra told us of
613 * the interface
614 * - for zebra, only because it learnt from kernel
615 * - if not:
616 * - search the name to see if it contains a sub-ipif / logical interface
617 * seperator, the ':' char. If it does:
618 * - text up to that char must be the primary name - get that name.
619 * if not:
620 * - no idea, just get the name in its entirety.
621 */
bcc24579 622static struct interface *if_sunwzebra_get(char *name, vrf_id_t vrf_id)
98954844 623{
d62a17ae 624 struct interface *ifp;
bcc24579 625 char *cp;
d62a17ae 626
bcc24579 627 if ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
d62a17ae 628 return ifp;
629
630 /* hunt the primary interface name... */
bcc24579
RW
631 cp = strchr(name, ':');
632 if (cp)
633 *cp = '\0';
d62a17ae 634
bcc24579 635 return if_get_by_name(name, vrf_id, 1);
98954844
PJ
636}
637#endif /* SUNOS_5 */
6b0655a2 638
91e5b43a 639DEFUN_NOSH (interface,
718e3744 640 interface_cmd,
199d90a1 641 "interface IFNAME [vrf NAME]",
718e3744 642 "Select an interface to configure\n"
fcbee690
QY
643 "Interface's name\n"
644 VRF_CMD_HELP_STR)
718e3744 645{
d62a17ae 646 int idx_ifname = 1;
647 int idx_vrf = 3;
648 const char *ifname = argv[idx_ifname]->arg;
649 const char *vrfname = (argc > 2) ? argv[idx_vrf]->arg : NULL;
650
651 struct interface *ifp;
d62a17ae 652 vrf_id_t vrf_id = VRF_DEFAULT;
653
bcc24579 654 if (strlen(ifname) > INTERFACE_NAMSIZ) {
d62a17ae 655 vty_out(vty,
656 "%% Interface name %s is invalid: length exceeds "
657 "%d characters\n",
658 ifname, INTERFACE_NAMSIZ);
659 return CMD_WARNING_CONFIG_FAILED;
660 }
718e3744 661
d62a17ae 662 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
663 Imagine forward reference of a vrf by name in this interface config */
664 if (vrfname)
ec1db588 665 VRF_GET_ID(vrf_id, vrfname, false);
cd2a8a42 666
98954844 667#ifdef SUNOS_5
bcc24579 668 ifp = if_sunwzebra_get(ifname, vrf_id);
98954844 669#else
bcc24579 670 ifp = if_get_by_name(ifname, vrf_id, 1);
98954844 671#endif /* SUNOS_5 */
718e3744 672
d62a17ae 673 if (!ifp) {
674 vty_out(vty, "%% interface %s not in %s\n", ifname, vrfname);
675 return CMD_WARNING_CONFIG_FAILED;
676 }
677 VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp);
718e3744 678
d62a17ae 679 return CMD_SUCCESS;
718e3744 680}
681
91e5b43a
RW
682DEFUN (no_interface,
683 no_interface_cmd,
684 "no interface IFNAME [vrf NAME]",
685 NO_STR
686 "Delete a pseudo interface's configuration\n"
687 "Interface's name\n"
688 VRF_CMD_HELP_STR)
32d2463c 689{
1505b537 690 int idx_vrf = 4;
d62a17ae 691 const char *ifname = argv[2]->arg;
1505b537 692 const char *vrfname = (argc > 3) ? argv[idx_vrf]->arg : NULL;
fcbee690 693
d62a17ae 694 // deleting interface
695 struct interface *ifp;
696 vrf_id_t vrf_id = VRF_DEFAULT;
32d2463c 697
d62a17ae 698 if (argc > 3)
ec1db588 699 VRF_GET_ID(vrf_id, vrfname, false);
cd2a8a42 700
d62a17ae 701 ifp = if_lookup_by_name(ifname, vrf_id);
32d2463c 702
d62a17ae 703 if (ifp == NULL) {
704 vty_out(vty, "%% Interface %s does not exist\n", ifname);
705 return CMD_WARNING_CONFIG_FAILED;
706 }
32d2463c 707
d62a17ae 708 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
709 vty_out(vty, "%% Only inactive interfaces can be deleted\n");
710 return CMD_WARNING_CONFIG_FAILED;
711 }
32d2463c 712
d62a17ae 713 if_delete(ifp);
32d2463c 714
d62a17ae 715 return CMD_SUCCESS;
32d2463c 716}
717
5d56066e
RW
718static void if_autocomplete(vector comps, struct cmd_token *token)
719{
720 struct interface *ifp;
721 struct vrf *vrf = NULL;
722
723 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
451fda4f 724 FOR_ALL_INTERFACES (vrf, ifp) {
5d56066e
RW
725 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name));
726 }
727 }
728}
729
730static const struct cmd_variable_handler if_var_handlers[] = {
731 {/* "interface NAME" */
732 .varname = "interface",
733 .completions = if_autocomplete},
734 {.tokenname = "IFNAME", .completions = if_autocomplete},
735 {.tokenname = "INTERFACE", .completions = if_autocomplete},
736 {.completions = NULL}};
737
d62a17ae 738void if_cmd_init(void)
0b84f294 739{
5d56066e
RW
740 cmd_variable_handler_register(if_var_handlers);
741
d62a17ae 742 install_element(CONFIG_NODE, &interface_cmd);
743 install_element(CONFIG_NODE, &no_interface_cmd);
0b84f294 744
d62a17ae 745 install_default(INTERFACE_NODE);
746 install_element(INTERFACE_NODE, &interface_desc_cmd);
747 install_element(INTERFACE_NODE, &no_interface_desc_cmd);
0b84f294
DL
748}
749
d7d73ffc 750#if 0
718e3744 751/* For debug purpose. */
752DEFUN (show_address,
753 show_address_cmd,
199d90a1 754 "show address [vrf NAME]",
718e3744 755 SHOW_STR
fcbee690
QY
756 "address\n"
757 VRF_CMD_HELP_STR)
718e3744 758{
abddf075 759 int idx_vrf = 3;
52dc7ee6 760 struct listnode *node;
718e3744 761 struct interface *ifp;
762 struct connected *ifc;
763 struct prefix *p;
8736158a 764 vrf_id_t vrf_id = VRF_DEFAULT;
718e3744 765
fcbee690 766 if (argc > 2)
58749582 767 VRF_GET_ID (vrf_id, argv[idx_vrf]->arg);
8736158a 768
451fda4f 769 FOR_ALL_INTERFACES (vrf, ifp)
718e3744 770 {
f4e14fdb 771 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
718e3744 772 {
718e3744 773 p = ifc->address;
774
775 if (p->family == AF_INET)
55f70b67 776 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
718e3744 777 }
778 }
779 return CMD_SUCCESS;
780}
781
8736158a
FL
782DEFUN (show_address_vrf_all,
783 show_address_vrf_all_cmd,
9ccf14f7 784 "show address vrf all",
8736158a
FL
785 SHOW_STR
786 "address\n"
787 VRF_ALL_CMD_HELP_STR)
788{
1a1a7065 789 struct vrf *vrf;
8736158a 790 struct listnode *node;
8736158a
FL
791 struct interface *ifp;
792 struct connected *ifc;
793 struct prefix *p;
8736158a 794
a62c4901 795 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
8736158a 796 {
f4e14fdb 797 if (RB_EMPTY (if_name_head, &vrf->ifaces_by_name))
8736158a
FL
798 continue;
799
55f70b67 800 vty_out (vty, "\nVRF %u\n\n", vrf->vrf_id);
8736158a 801
451fda4f 802 FOR_ALL_INTERFACES (vrf, ifp)
8736158a 803 {
f4e14fdb 804 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
8736158a
FL
805 {
806 p = ifc->address;
807
808 if (p->family == AF_INET)
55f70b67 809 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
8736158a
FL
810 }
811 }
812 }
813 return CMD_SUCCESS;
814}
d7d73ffc 815#endif
8736158a 816
718e3744 817/* Allocate connected structure. */
d62a17ae 818struct connected *connected_new(void)
718e3744 819{
d62a17ae 820 return XCALLOC(MTYPE_CONNECTED, sizeof(struct connected));
718e3744 821}
822
a80beece 823/* Allocate nbr connected structure. */
d62a17ae 824struct nbr_connected *nbr_connected_new(void)
a80beece 825{
d62a17ae 826 return XCALLOC(MTYPE_NBR_CONNECTED, sizeof(struct nbr_connected));
a80beece
DS
827}
828
718e3744 829/* Free connected structure. */
d62a17ae 830void connected_free(struct connected *connected)
718e3744 831{
d62a17ae 832 if (connected->address)
833 prefix_free(connected->address);
718e3744 834
d62a17ae 835 if (connected->destination)
836 prefix_free(connected->destination);
718e3744 837
d62a17ae 838 if (connected->label)
839 XFREE(MTYPE_CONNECTED_LABEL, connected->label);
718e3744 840
d62a17ae 841 XFREE(MTYPE_CONNECTED, connected);
718e3744 842}
843
a80beece 844/* Free nbr connected structure. */
d62a17ae 845void nbr_connected_free(struct nbr_connected *connected)
a80beece 846{
d62a17ae 847 if (connected->address)
848 prefix_free(connected->address);
a80beece 849
d62a17ae 850 XFREE(MTYPE_NBR_CONNECTED, connected);
a80beece
DS
851}
852
853/* If same interface nbr address already exists... */
d62a17ae 854struct nbr_connected *nbr_connected_check(struct interface *ifp,
855 struct prefix *p)
a80beece 856{
d62a17ae 857 struct nbr_connected *ifc;
858 struct listnode *node;
a80beece 859
d62a17ae 860 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, ifc))
861 if (prefix_same(ifc->address, p))
862 return ifc;
a80beece 863
d62a17ae 864 return NULL;
a80beece
DS
865}
866
718e3744 867/* Print if_addr structure. */
d62a17ae 868static void __attribute__((unused))
869connected_log(struct connected *connected, char *str)
718e3744 870{
d62a17ae 871 struct prefix *p;
872 struct interface *ifp;
873 char logbuf[BUFSIZ];
874 char buf[BUFSIZ];
875
876 ifp = connected->ifp;
877 p = connected->address;
878
879 snprintf(logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ", str,
880 ifp->name, ifp->vrf_id, prefix_family_str(p),
881 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
882
883 p = connected->destination;
884 if (p) {
885 strncat(logbuf, inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
886 BUFSIZ - strlen(logbuf));
887 }
888 zlog_info("%s", logbuf);
718e3744 889}
890
a80beece 891/* Print if_addr structure. */
d62a17ae 892static void __attribute__((unused))
893nbr_connected_log(struct nbr_connected *connected, char *str)
a80beece 894{
d62a17ae 895 struct prefix *p;
896 struct interface *ifp;
897 char logbuf[BUFSIZ];
898 char buf[BUFSIZ];
a80beece 899
d62a17ae 900 ifp = connected->ifp;
901 p = connected->address;
a80beece 902
d62a17ae 903 snprintf(logbuf, BUFSIZ, "%s interface %s %s %s/%d ", str, ifp->name,
904 prefix_family_str(p),
905 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
a80beece 906
d62a17ae 907 zlog_info("%s", logbuf);
a80beece
DS
908}
909
718e3744 910/* If two connected address has same prefix return 1. */
d62a17ae 911static int connected_same_prefix(struct prefix *p1, struct prefix *p2)
718e3744 912{
d62a17ae 913 if (p1->family == p2->family) {
914 if (p1->family == AF_INET
915 && IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
916 return 1;
917 if (p1->family == AF_INET6
918 && IPV6_ADDR_SAME(&p1->u.prefix6, &p2->u.prefix6))
919 return 1;
920 }
921 return 0;
718e3744 922}
923
d62a17ae 924struct connected *connected_lookup_prefix_exact(struct interface *ifp,
925 struct prefix *p)
38485402 926{
d62a17ae 927 struct listnode *node;
928 struct listnode *next;
929 struct connected *ifc;
38485402 930
d62a17ae 931 for (node = listhead(ifp->connected); node; node = next) {
932 ifc = listgetdata(node);
933 next = node->next;
38485402 934
d62a17ae 935 if (connected_same_prefix(ifc->address, p))
936 return ifc;
937 }
938 return NULL;
38485402
DS
939}
940
d62a17ae 941struct connected *connected_delete_by_prefix(struct interface *ifp,
942 struct prefix *p)
718e3744 943{
d62a17ae 944 struct listnode *node;
945 struct listnode *next;
946 struct connected *ifc;
947
948 /* In case of same prefix come, replace it with new one. */
949 for (node = listhead(ifp->connected); node; node = next) {
950 ifc = listgetdata(node);
951 next = node->next;
952
953 if (connected_same_prefix(ifc->address, p)) {
954 listnode_delete(ifp->connected, ifc);
955 return ifc;
956 }
718e3744 957 }
d62a17ae 958 return NULL;
718e3744 959}
960
bd40c341 961/* Find the address on our side that will be used when packets
727d104b 962 are sent to dst. */
d62a17ae 963struct connected *connected_lookup_prefix(struct interface *ifp,
964 struct prefix *addr)
727d104b 965{
d62a17ae 966 struct listnode *cnode;
967 struct connected *c;
968 struct connected *match;
969
970 match = NULL;
971
972 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
973 if (c->address && (c->address->family == addr->family)
974 && prefix_match(CONNECTED_PREFIX(c), addr)
975 && (!match
976 || (c->address->prefixlen > match->address->prefixlen)))
977 match = c;
978 }
979 return match;
727d104b 980}
981
d62a17ae 982struct connected *connected_add_by_prefix(struct interface *ifp,
983 struct prefix *p,
984 struct prefix *destination)
4a7aac1b 985{
d62a17ae 986 struct connected *ifc;
4a7aac1b 987
d62a17ae 988 /* Allocate new connected address. */
989 ifc = connected_new();
990 ifc->ifp = ifp;
4a7aac1b 991
d62a17ae 992 /* Fetch interface address */
993 ifc->address = prefix_new();
994 memcpy(ifc->address, p, sizeof(struct prefix));
4a7aac1b 995
d62a17ae 996 /* Fetch dest address */
997 if (destination) {
998 ifc->destination = prefix_new();
999 memcpy(ifc->destination, destination, sizeof(struct prefix));
1000 }
4a7aac1b 1001
d62a17ae 1002 /* Add connected address to the interface. */
1003 listnode_add(ifp->connected, ifc);
1004 return ifc;
4a7aac1b 1005}
1006
d62a17ae 1007#if 0 /* this route_table of struct connected's is unused \
1008 * however, it would be good to use a route_table rather than \
1009 * a list.. \
1010 */
718e3744 1011/* Interface looking up by interface's address. */
718e3744 1012/* Interface's IPv4 address reverse lookup table. */
1013struct route_table *ifaddr_ipv4_table;
1014/* struct route_table *ifaddr_ipv6_table; */
1015
8cc4198f 1016static void
718e3744 1017ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
1018{
1019 struct route_node *rn;
1020 struct prefix_ipv4 p;
1021
1022 p.family = AF_INET;
1023 p.prefixlen = IPV4_MAX_PREFIXLEN;
1024 p.prefix = *ifaddr;
1025
1026 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
1027 if (rn)
1028 {
1029 route_unlock_node (rn);
1030 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1031 inet_ntoa (*ifaddr));
1032 return;
1033 }
1034 rn->info = ifp;
1035}
1036
8cc4198f 1037static void
718e3744 1038ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
1039{
1040 struct route_node *rn;
1041 struct prefix_ipv4 p;
1042
1043 p.family = AF_INET;
1044 p.prefixlen = IPV4_MAX_PREFIXLEN;
1045 p.prefix = *ifaddr;
1046
1047 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1048 if (! rn)
1049 {
1050 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1051 inet_ntoa (*ifaddr));
1052 return;
1053 }
1054 rn->info = NULL;
1055 route_unlock_node (rn);
1056 route_unlock_node (rn);
1057}
1058
1059/* Lookup interface by interface's IP address or interface index. */
8cc4198f 1060static struct interface *
b892f1dd 1061ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
718e3744 1062{
1063 struct prefix_ipv4 p;
1064 struct route_node *rn;
1065 struct interface *ifp;
718e3744 1066
1067 if (addr)
1068 {
1069 p.family = AF_INET;
1070 p.prefixlen = IPV4_MAX_PREFIXLEN;
1071 p.prefix = *addr;
1072
1073 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1074 if (! rn)
1075 return NULL;
55cd0f61 1076
718e3744 1077 ifp = rn->info;
1078 route_unlock_node (rn);
1079 return ifp;
1080 }
1081 else
7e2b7603 1082 return if_lookup_by_index(ifindex, VRF_DEFAULT);
718e3744 1083}
8cc4198f 1084#endif /* ifaddr_ipv4_table */
718e3744 1085
f4e14fdb 1086void if_terminate(struct vrf *vrf)
4bd045d5 1087{
f4e14fdb 1088 struct interface *ifp;
4bd045d5 1089
55cd0f61
DS
1090 while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
1091 ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name);
1092
d62a17ae 1093 if (ifp->node) {
1094 ifp->node->info = NULL;
1095 route_unlock_node(ifp->node);
1096 }
d62a17ae 1097 if_delete(ifp);
1098 }
4bd045d5 1099}
8ccc7e80 1100
d62a17ae 1101const char *if_link_type_str(enum zebra_link_type llt)
8ccc7e80 1102{
d62a17ae 1103 switch (llt) {
8ccc7e80 1104#define llts(T,S) case (T): return (S)
d62a17ae 1105 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
1106 llts(ZEBRA_LLT_ETHER, "Ethernet");
1107 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
1108 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
1109 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
1110 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
1111 llts(ZEBRA_LLT_ARCNET, "ARCnet");
1112 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
1113 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
1114 llts(ZEBRA_LLT_ATM, "ATM");
1115 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
1116 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
1117 llts(ZEBRA_LLT_EUI64, "EUI-64");
1118 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
1119 llts(ZEBRA_LLT_SLIP, "SLIP");
1120 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
1121 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
1122 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
1123 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
1124 llts(ZEBRA_LLT_X25, "CCITT X.25");
1125 llts(ZEBRA_LLT_PPP, "PPP");
1126 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
1127 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1128 llts(ZEBRA_LLT_LAPB, "LAPB");
1129 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1130 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1131 llts(ZEBRA_LLT_FRAD, "FRAD");
1132 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1133 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1134 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1135 llts(ZEBRA_LLT_FDDI, "FDDI");
1136 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1137 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1138 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1139 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1140 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1141 llts(ZEBRA_LLT_IRDA, "IrDA");
1142 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1143 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1144 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1145 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1146 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1147 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1148 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1149 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1150 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1151 default:
03548ae8 1152 flog_err(LIB_ERR_DEVELOPMENT, "Unknown value %d", llt);
d62a17ae 1153 return "Unknown type!";
8ccc7e80 1154#undef llts
d62a17ae 1155 }
1156 return NULL;
8ccc7e80 1157}
16f1b9ee 1158
d62a17ae 1159struct if_link_params *if_link_params_get(struct interface *ifp)
16f1b9ee 1160{
d62a17ae 1161 int i;
16f1b9ee 1162
d62a17ae 1163 if (ifp->link_params != NULL)
1164 return ifp->link_params;
16f1b9ee 1165
d62a17ae 1166 struct if_link_params *iflp =
1167 XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params));
1168 if (iflp == NULL)
1169 return NULL;
16f1b9ee 1170
d62a17ae 1171 /* Set TE metric equal to standard metric */
1172 iflp->te_metric = ifp->metric;
16f1b9ee 1173
d62a17ae 1174 /* Compute default bandwidth based on interface */
1175 iflp->default_bw =
1176 ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1177 * TE_KILO_BIT / TE_BYTE);
16f1b9ee 1178
d62a17ae 1179 /* Set Max, Reservable and Unreserved Bandwidth */
1180 iflp->max_bw = iflp->default_bw;
1181 iflp->max_rsv_bw = iflp->default_bw;
1182 for (i = 0; i < MAX_CLASS_TYPE; i++)
1183 iflp->unrsv_bw[i] = iflp->default_bw;
16f1b9ee 1184
d62a17ae 1185 /* Update Link parameters status */
1186 iflp->lp_status =
1187 LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
16f1b9ee 1188
d62a17ae 1189 /* Finally attach newly created Link Parameters */
1190 ifp->link_params = iflp;
16f1b9ee 1191
d62a17ae 1192 return iflp;
16f1b9ee
OD
1193}
1194
d62a17ae 1195void if_link_params_free(struct interface *ifp)
16f1b9ee 1196{
d62a17ae 1197 if (ifp->link_params == NULL)
1198 return;
1199 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1200 ifp->link_params = NULL;
16f1b9ee 1201}