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