]> git.proxmox.com Git - mirror_frr.git/blame - lib/if.c
bgpd: another change to keep indent.py happy
[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
ce19a04a
DL
51DEFINE_HOOK(if_add, (struct interface *ifp), (ifp))
52DEFINE_KOOH(if_del, (struct interface *ifp), (ifp))
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);
ce19a04a 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{
ce19a04a 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
228 RB_FOREACH(vrf, vrf_id_head, &vrfs_by_id) {
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 */
407 if (ifp->vrf_id != vrf_id &&
408 vrf_is_mapped_on_netns(vrf_id))
409 return if_create(name, vrf_id);
410 /* If it came from the kernel
411 * or by way of zclient, believe it and update
412 * the ifp accordingly.
413 */
414 if_update_to_new_vrf(ifp, vrf_id);
415 return ifp;
8736158a
FL
416}
417
ff880b78
RW
418void if_set_index(struct interface *ifp, ifindex_t ifindex)
419{
5b8524f5
RW
420 struct vrf *vrf;
421
422 vrf = vrf_lookup_by_id(ifp->vrf_id);
423 assert(vrf);
ff880b78
RW
424
425 if (ifp->ifindex == ifindex)
426 return;
427
428 if (ifp->ifindex != IFINDEX_INTERNAL)
429 IFINDEX_RB_REMOVE(vrf, ifp)
430
431 ifp->ifindex = ifindex;
432
433 if (ifp->ifindex != IFINDEX_INTERNAL)
434 IFINDEX_RB_INSERT(vrf, ifp)
435}
436
718e3744 437/* Does interface up ? */
d62a17ae 438int if_is_up(struct interface *ifp)
718e3744 439{
d62a17ae 440 return ifp->flags & IFF_UP;
718e3744 441}
442
2e3b2e47 443/* Is interface running? */
d62a17ae 444int if_is_running(struct interface *ifp)
2e3b2e47 445{
d62a17ae 446 return ifp->flags & IFF_RUNNING;
2e3b2e47 447}
448
449/* Is the interface operative, eg. either UP & RUNNING
244c1cdc
DS
450 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
451 if ptm checking is enabled, then ptm check has passed */
d62a17ae 452int if_is_operative(struct interface *ifp)
2e3b2e47 453{
d62a17ae 454 return ((ifp->flags & IFF_UP)
455 && (((ifp->flags & IFF_RUNNING)
456 && (ifp->ptm_status || !ifp->ptm_enable))
457 || !CHECK_FLAG(ifp->status,
458 ZEBRA_INTERFACE_LINKDETECTION)));
244c1cdc
DS
459}
460
461/* Is the interface operative, eg. either UP & RUNNING
462 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
d62a17ae 463int if_is_no_ptm_operative(struct interface *ifp)
244c1cdc 464{
d62a17ae 465 return ((ifp->flags & IFF_UP)
466 && ((ifp->flags & IFF_RUNNING)
467 || !CHECK_FLAG(ifp->status,
468 ZEBRA_INTERFACE_LINKDETECTION)));
2e3b2e47 469}
470
718e3744 471/* Is this loopback interface ? */
d62a17ae 472int if_is_loopback(struct interface *ifp)
718e3744 473{
d62a17ae 474 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
475 * but Y on platform N?
476 */
477 return (ifp->flags & (IFF_LOOPBACK | IFF_NOXMIT | IFF_VIRTUAL));
718e3744 478}
479
480/* Does this interface support broadcast ? */
d62a17ae 481int if_is_broadcast(struct interface *ifp)
718e3744 482{
d62a17ae 483 return ifp->flags & IFF_BROADCAST;
718e3744 484}
485
486/* Does this interface support broadcast ? */
d62a17ae 487int if_is_pointopoint(struct interface *ifp)
718e3744 488{
d62a17ae 489 return ifp->flags & IFF_POINTOPOINT;
718e3744 490}
491
492/* Does this interface support multicast ? */
d62a17ae 493int if_is_multicast(struct interface *ifp)
718e3744 494{
d62a17ae 495 return ifp->flags & IFF_MULTICAST;
718e3744 496}
497
498/* Printout flag information into log */
d62a17ae 499const char *if_flag_dump(unsigned long flag)
718e3744 500{
d62a17ae 501 int separator = 0;
502 static char logbuf[BUFSIZ];
503
504#define IFF_OUT_LOG(X, STR) \
505 if (flag & (X)) { \
506 if (separator) \
507 strlcat(logbuf, ",", BUFSIZ); \
508 else \
509 separator = 1; \
510 strlcat(logbuf, STR, BUFSIZ); \
511 }
718e3744 512
d62a17ae 513 strlcpy(logbuf, "<", BUFSIZ);
514 IFF_OUT_LOG(IFF_UP, "UP");
515 IFF_OUT_LOG(IFF_BROADCAST, "BROADCAST");
516 IFF_OUT_LOG(IFF_DEBUG, "DEBUG");
517 IFF_OUT_LOG(IFF_LOOPBACK, "LOOPBACK");
518 IFF_OUT_LOG(IFF_POINTOPOINT, "POINTOPOINT");
519 IFF_OUT_LOG(IFF_NOTRAILERS, "NOTRAILERS");
520 IFF_OUT_LOG(IFF_RUNNING, "RUNNING");
521 IFF_OUT_LOG(IFF_NOARP, "NOARP");
522 IFF_OUT_LOG(IFF_PROMISC, "PROMISC");
523 IFF_OUT_LOG(IFF_ALLMULTI, "ALLMULTI");
524 IFF_OUT_LOG(IFF_OACTIVE, "OACTIVE");
525 IFF_OUT_LOG(IFF_SIMPLEX, "SIMPLEX");
526 IFF_OUT_LOG(IFF_LINK0, "LINK0");
527 IFF_OUT_LOG(IFF_LINK1, "LINK1");
528 IFF_OUT_LOG(IFF_LINK2, "LINK2");
529 IFF_OUT_LOG(IFF_MULTICAST, "MULTICAST");
530 IFF_OUT_LOG(IFF_NOXMIT, "NOXMIT");
531 IFF_OUT_LOG(IFF_NORTEXCH, "NORTEXCH");
532 IFF_OUT_LOG(IFF_VIRTUAL, "VIRTUAL");
533 IFF_OUT_LOG(IFF_IPV4, "IPv4");
534 IFF_OUT_LOG(IFF_IPV6, "IPv6");
535
536 strlcat(logbuf, ">", BUFSIZ);
537
538 return logbuf;
630c97ce 539#undef IFF_OUT_LOG
718e3744 540}
541
542/* For debugging */
d62a17ae 543static void if_dump(const struct interface *ifp)
718e3744 544{
d62a17ae 545 struct listnode *node;
546 struct connected *c __attribute__((unused));
547
548 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, c))
549 zlog_info(
550 "Interface %s vrf %u index %d metric %d mtu %d "
551 "mtu6 %d %s",
552 ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric,
553 ifp->mtu, ifp->mtu6, if_flag_dump(ifp->flags));
718e3744 554}
555
556/* Interface printing for all interface. */
d62a17ae 557void if_dump_all(void)
718e3744 558{
d62a17ae 559 struct vrf *vrf;
f4e14fdb 560 void *ifp;
d62a17ae 561
a2addae8 562 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
451fda4f 563 FOR_ALL_INTERFACES (vrf, ifp)
f4e14fdb 564 if_dump(ifp);
718e3744 565}
566
6147e2c6 567DEFUN (interface_desc,
718e3744 568 interface_desc_cmd,
fcbee690 569 "description LINE...",
718e3744 570 "Interface specific description\n"
571 "Characters describing this interface\n")
572{
d62a17ae 573 int idx_line = 1;
574 VTY_DECLVAR_CONTEXT(interface, ifp);
718e3744 575
d62a17ae 576 if (ifp->desc)
577 XFREE(MTYPE_TMP, ifp->desc);
578 ifp->desc = argv_concat(argv, argc, idx_line);
718e3744 579
d62a17ae 580 return CMD_SUCCESS;
718e3744 581}
582
6147e2c6 583DEFUN (no_interface_desc,
718e3744 584 no_interface_desc_cmd,
585 "no description",
586 NO_STR
587 "Interface specific description\n")
588{
d62a17ae 589 VTY_DECLVAR_CONTEXT(interface, ifp);
718e3744 590
d62a17ae 591 if (ifp->desc)
592 XFREE(MTYPE_TMP, ifp->desc);
593 ifp->desc = NULL;
718e3744 594
d62a17ae 595 return CMD_SUCCESS;
718e3744 596}
6b0655a2 597
98954844
PJ
598#ifdef SUNOS_5
599/* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
600 * a seperate struct interface for each logical interface, so config
601 * file may be full of 'interface fooX:Y'. Solaris however does not
602 * expose logical interfaces via PF_ROUTE, so trying to track logical
603 * interfaces can be fruitless, for that reason Quagga only tracks
604 * the primary IP interface.
605 *
606 * We try accomodate SUNWzebra by:
607 * - looking up the interface name, to see whether it exists, if so
608 * its useable
609 * - for protocol daemons, this could only because zebra told us of
610 * the interface
611 * - for zebra, only because it learnt from kernel
612 * - if not:
613 * - search the name to see if it contains a sub-ipif / logical interface
614 * seperator, the ':' char. If it does:
615 * - text up to that char must be the primary name - get that name.
616 * if not:
617 * - no idea, just get the name in its entirety.
618 */
bcc24579 619static struct interface *if_sunwzebra_get(char *name, vrf_id_t vrf_id)
98954844 620{
d62a17ae 621 struct interface *ifp;
bcc24579 622 char *cp;
d62a17ae 623
bcc24579 624 if ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
d62a17ae 625 return ifp;
626
627 /* hunt the primary interface name... */
bcc24579
RW
628 cp = strchr(name, ':');
629 if (cp)
630 *cp = '\0';
d62a17ae 631
bcc24579 632 return if_get_by_name(name, vrf_id, 1);
98954844
PJ
633}
634#endif /* SUNOS_5 */
6b0655a2 635
718e3744 636DEFUN (interface,
637 interface_cmd,
199d90a1 638 "interface IFNAME [vrf NAME]",
718e3744 639 "Select an interface to configure\n"
fcbee690
QY
640 "Interface's name\n"
641 VRF_CMD_HELP_STR)
718e3744 642{
d62a17ae 643 int idx_ifname = 1;
644 int idx_vrf = 3;
645 const char *ifname = argv[idx_ifname]->arg;
646 const char *vrfname = (argc > 2) ? argv[idx_vrf]->arg : NULL;
647
648 struct interface *ifp;
d62a17ae 649 vrf_id_t vrf_id = VRF_DEFAULT;
650
bcc24579 651 if (strlen(ifname) > INTERFACE_NAMSIZ) {
d62a17ae 652 vty_out(vty,
653 "%% Interface name %s is invalid: length exceeds "
654 "%d characters\n",
655 ifname, INTERFACE_NAMSIZ);
656 return CMD_WARNING_CONFIG_FAILED;
657 }
718e3744 658
d62a17ae 659 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
660 Imagine forward reference of a vrf by name in this interface config */
661 if (vrfname)
662 VRF_GET_ID(vrf_id, vrfname);
cd2a8a42 663
98954844 664#ifdef SUNOS_5
bcc24579 665 ifp = if_sunwzebra_get(ifname, vrf_id);
98954844 666#else
bcc24579 667 ifp = if_get_by_name(ifname, vrf_id, 1);
98954844 668#endif /* SUNOS_5 */
718e3744 669
d62a17ae 670 if (!ifp) {
671 vty_out(vty, "%% interface %s not in %s\n", ifname, vrfname);
672 return CMD_WARNING_CONFIG_FAILED;
673 }
674 VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp);
718e3744 675
d62a17ae 676 return CMD_SUCCESS;
718e3744 677}
678
32d2463c 679DEFUN_NOSH (no_interface,
680 no_interface_cmd,
199d90a1 681 "no interface IFNAME [vrf NAME]",
32d2463c 682 NO_STR
683 "Delete a pseudo interface's configuration\n"
fcbee690
QY
684 "Interface's name\n"
685 VRF_CMD_HELP_STR)
32d2463c 686{
1505b537 687 int idx_vrf = 4;
d62a17ae 688 const char *ifname = argv[2]->arg;
1505b537 689 const char *vrfname = (argc > 3) ? argv[idx_vrf]->arg : NULL;
fcbee690 690
d62a17ae 691 // deleting interface
692 struct interface *ifp;
693 vrf_id_t vrf_id = VRF_DEFAULT;
32d2463c 694
d62a17ae 695 if (argc > 3)
696 VRF_GET_ID(vrf_id, vrfname);
cd2a8a42 697
d62a17ae 698 ifp = if_lookup_by_name(ifname, vrf_id);
32d2463c 699
d62a17ae 700 if (ifp == NULL) {
701 vty_out(vty, "%% Interface %s does not exist\n", ifname);
702 return CMD_WARNING_CONFIG_FAILED;
703 }
32d2463c 704
d62a17ae 705 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
706 vty_out(vty, "%% Only inactive interfaces can be deleted\n");
707 return CMD_WARNING_CONFIG_FAILED;
708 }
32d2463c 709
d62a17ae 710 if_delete(ifp);
32d2463c 711
d62a17ae 712 return CMD_SUCCESS;
32d2463c 713}
714
5d56066e
RW
715static void if_autocomplete(vector comps, struct cmd_token *token)
716{
717 struct interface *ifp;
718 struct vrf *vrf = NULL;
719
720 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
451fda4f 721 FOR_ALL_INTERFACES (vrf, ifp) {
5d56066e
RW
722 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name));
723 }
724 }
725}
726
727static const struct cmd_variable_handler if_var_handlers[] = {
728 {/* "interface NAME" */
729 .varname = "interface",
730 .completions = if_autocomplete},
731 {.tokenname = "IFNAME", .completions = if_autocomplete},
732 {.tokenname = "INTERFACE", .completions = if_autocomplete},
733 {.completions = NULL}};
734
d62a17ae 735void if_cmd_init(void)
0b84f294 736{
5d56066e
RW
737 cmd_variable_handler_register(if_var_handlers);
738
d62a17ae 739 install_element(CONFIG_NODE, &interface_cmd);
740 install_element(CONFIG_NODE, &no_interface_cmd);
0b84f294 741
d62a17ae 742 install_default(INTERFACE_NODE);
743 install_element(INTERFACE_NODE, &interface_desc_cmd);
744 install_element(INTERFACE_NODE, &no_interface_desc_cmd);
0b84f294
DL
745}
746
d7d73ffc 747#if 0
718e3744 748/* For debug purpose. */
749DEFUN (show_address,
750 show_address_cmd,
199d90a1 751 "show address [vrf NAME]",
718e3744 752 SHOW_STR
fcbee690
QY
753 "address\n"
754 VRF_CMD_HELP_STR)
718e3744 755{
abddf075 756 int idx_vrf = 3;
52dc7ee6 757 struct listnode *node;
718e3744 758 struct interface *ifp;
759 struct connected *ifc;
760 struct prefix *p;
8736158a 761 vrf_id_t vrf_id = VRF_DEFAULT;
718e3744 762
fcbee690 763 if (argc > 2)
58749582 764 VRF_GET_ID (vrf_id, argv[idx_vrf]->arg);
8736158a 765
451fda4f 766 FOR_ALL_INTERFACES (vrf, ifp)
718e3744 767 {
f4e14fdb 768 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
718e3744 769 {
718e3744 770 p = ifc->address;
771
772 if (p->family == AF_INET)
55f70b67 773 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
718e3744 774 }
775 }
776 return CMD_SUCCESS;
777}
778
8736158a
FL
779DEFUN (show_address_vrf_all,
780 show_address_vrf_all_cmd,
9ccf14f7 781 "show address vrf all",
8736158a
FL
782 SHOW_STR
783 "address\n"
784 VRF_ALL_CMD_HELP_STR)
785{
1a1a7065 786 struct vrf *vrf;
8736158a 787 struct listnode *node;
8736158a
FL
788 struct interface *ifp;
789 struct connected *ifc;
790 struct prefix *p;
8736158a 791
a62c4901 792 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
8736158a 793 {
f4e14fdb 794 if (RB_EMPTY (if_name_head, &vrf->ifaces_by_name))
8736158a
FL
795 continue;
796
55f70b67 797 vty_out (vty, "\nVRF %u\n\n", vrf->vrf_id);
8736158a 798
451fda4f 799 FOR_ALL_INTERFACES (vrf, ifp)
8736158a 800 {
f4e14fdb 801 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
8736158a
FL
802 {
803 p = ifc->address;
804
805 if (p->family == AF_INET)
55f70b67 806 vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen);
8736158a
FL
807 }
808 }
809 }
810 return CMD_SUCCESS;
811}
d7d73ffc 812#endif
8736158a 813
718e3744 814/* Allocate connected structure. */
d62a17ae 815struct connected *connected_new(void)
718e3744 816{
d62a17ae 817 return XCALLOC(MTYPE_CONNECTED, sizeof(struct connected));
718e3744 818}
819
a80beece 820/* Allocate nbr connected structure. */
d62a17ae 821struct nbr_connected *nbr_connected_new(void)
a80beece 822{
d62a17ae 823 return XCALLOC(MTYPE_NBR_CONNECTED, sizeof(struct nbr_connected));
a80beece
DS
824}
825
718e3744 826/* Free connected structure. */
d62a17ae 827void connected_free(struct connected *connected)
718e3744 828{
d62a17ae 829 if (connected->address)
830 prefix_free(connected->address);
718e3744 831
d62a17ae 832 if (connected->destination)
833 prefix_free(connected->destination);
718e3744 834
d62a17ae 835 if (connected->label)
836 XFREE(MTYPE_CONNECTED_LABEL, connected->label);
718e3744 837
d62a17ae 838 XFREE(MTYPE_CONNECTED, connected);
718e3744 839}
840
a80beece 841/* Free nbr connected structure. */
d62a17ae 842void nbr_connected_free(struct nbr_connected *connected)
a80beece 843{
d62a17ae 844 if (connected->address)
845 prefix_free(connected->address);
a80beece 846
d62a17ae 847 XFREE(MTYPE_NBR_CONNECTED, connected);
a80beece
DS
848}
849
850/* If same interface nbr address already exists... */
d62a17ae 851struct nbr_connected *nbr_connected_check(struct interface *ifp,
852 struct prefix *p)
a80beece 853{
d62a17ae 854 struct nbr_connected *ifc;
855 struct listnode *node;
a80beece 856
d62a17ae 857 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, ifc))
858 if (prefix_same(ifc->address, p))
859 return ifc;
a80beece 860
d62a17ae 861 return NULL;
a80beece
DS
862}
863
718e3744 864/* Print if_addr structure. */
d62a17ae 865static void __attribute__((unused))
866connected_log(struct connected *connected, char *str)
718e3744 867{
d62a17ae 868 struct prefix *p;
869 struct interface *ifp;
870 char logbuf[BUFSIZ];
871 char buf[BUFSIZ];
872
873 ifp = connected->ifp;
874 p = connected->address;
875
876 snprintf(logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ", str,
877 ifp->name, ifp->vrf_id, prefix_family_str(p),
878 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
879
880 p = connected->destination;
881 if (p) {
882 strncat(logbuf, inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
883 BUFSIZ - strlen(logbuf));
884 }
885 zlog_info("%s", logbuf);
718e3744 886}
887
a80beece 888/* Print if_addr structure. */
d62a17ae 889static void __attribute__((unused))
890nbr_connected_log(struct nbr_connected *connected, char *str)
a80beece 891{
d62a17ae 892 struct prefix *p;
893 struct interface *ifp;
894 char logbuf[BUFSIZ];
895 char buf[BUFSIZ];
a80beece 896
d62a17ae 897 ifp = connected->ifp;
898 p = connected->address;
a80beece 899
d62a17ae 900 snprintf(logbuf, BUFSIZ, "%s interface %s %s %s/%d ", str, ifp->name,
901 prefix_family_str(p),
902 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
a80beece 903
d62a17ae 904 zlog_info("%s", logbuf);
a80beece
DS
905}
906
718e3744 907/* If two connected address has same prefix return 1. */
d62a17ae 908static int connected_same_prefix(struct prefix *p1, struct prefix *p2)
718e3744 909{
d62a17ae 910 if (p1->family == p2->family) {
911 if (p1->family == AF_INET
912 && IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
913 return 1;
914 if (p1->family == AF_INET6
915 && IPV6_ADDR_SAME(&p1->u.prefix6, &p2->u.prefix6))
916 return 1;
917 }
918 return 0;
718e3744 919}
920
d62a17ae 921struct connected *connected_lookup_prefix_exact(struct interface *ifp,
922 struct prefix *p)
38485402 923{
d62a17ae 924 struct listnode *node;
925 struct listnode *next;
926 struct connected *ifc;
38485402 927
d62a17ae 928 for (node = listhead(ifp->connected); node; node = next) {
929 ifc = listgetdata(node);
930 next = node->next;
38485402 931
d62a17ae 932 if (connected_same_prefix(ifc->address, p))
933 return ifc;
934 }
935 return NULL;
38485402
DS
936}
937
d62a17ae 938struct connected *connected_delete_by_prefix(struct interface *ifp,
939 struct prefix *p)
718e3744 940{
d62a17ae 941 struct listnode *node;
942 struct listnode *next;
943 struct connected *ifc;
944
945 /* In case of same prefix come, replace it with new one. */
946 for (node = listhead(ifp->connected); node; node = next) {
947 ifc = listgetdata(node);
948 next = node->next;
949
950 if (connected_same_prefix(ifc->address, p)) {
951 listnode_delete(ifp->connected, ifc);
952 return ifc;
953 }
718e3744 954 }
d62a17ae 955 return NULL;
718e3744 956}
957
bd40c341 958/* Find the address on our side that will be used when packets
727d104b 959 are sent to dst. */
d62a17ae 960struct connected *connected_lookup_prefix(struct interface *ifp,
961 struct prefix *addr)
727d104b 962{
d62a17ae 963 struct listnode *cnode;
964 struct connected *c;
965 struct connected *match;
966
967 match = NULL;
968
969 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
970 if (c->address && (c->address->family == addr->family)
971 && prefix_match(CONNECTED_PREFIX(c), addr)
972 && (!match
973 || (c->address->prefixlen > match->address->prefixlen)))
974 match = c;
975 }
976 return match;
727d104b 977}
978
d62a17ae 979struct connected *connected_add_by_prefix(struct interface *ifp,
980 struct prefix *p,
981 struct prefix *destination)
4a7aac1b 982{
d62a17ae 983 struct connected *ifc;
4a7aac1b 984
d62a17ae 985 /* Allocate new connected address. */
986 ifc = connected_new();
987 ifc->ifp = ifp;
4a7aac1b 988
d62a17ae 989 /* Fetch interface address */
990 ifc->address = prefix_new();
991 memcpy(ifc->address, p, sizeof(struct prefix));
4a7aac1b 992
d62a17ae 993 /* Fetch dest address */
994 if (destination) {
995 ifc->destination = prefix_new();
996 memcpy(ifc->destination, destination, sizeof(struct prefix));
997 }
4a7aac1b 998
d62a17ae 999 /* Add connected address to the interface. */
1000 listnode_add(ifp->connected, ifc);
1001 return ifc;
4a7aac1b 1002}
1003
d62a17ae 1004#if 0 /* this route_table of struct connected's is unused \
1005 * however, it would be good to use a route_table rather than \
1006 * a list.. \
1007 */
718e3744 1008/* Interface looking up by interface's address. */
718e3744 1009/* Interface's IPv4 address reverse lookup table. */
1010struct route_table *ifaddr_ipv4_table;
1011/* struct route_table *ifaddr_ipv6_table; */
1012
8cc4198f 1013static void
718e3744 1014ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
1015{
1016 struct route_node *rn;
1017 struct prefix_ipv4 p;
1018
1019 p.family = AF_INET;
1020 p.prefixlen = IPV4_MAX_PREFIXLEN;
1021 p.prefix = *ifaddr;
1022
1023 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
1024 if (rn)
1025 {
1026 route_unlock_node (rn);
1027 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1028 inet_ntoa (*ifaddr));
1029 return;
1030 }
1031 rn->info = ifp;
1032}
1033
8cc4198f 1034static void
718e3744 1035ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
1036{
1037 struct route_node *rn;
1038 struct prefix_ipv4 p;
1039
1040 p.family = AF_INET;
1041 p.prefixlen = IPV4_MAX_PREFIXLEN;
1042 p.prefix = *ifaddr;
1043
1044 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1045 if (! rn)
1046 {
1047 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1048 inet_ntoa (*ifaddr));
1049 return;
1050 }
1051 rn->info = NULL;
1052 route_unlock_node (rn);
1053 route_unlock_node (rn);
1054}
1055
1056/* Lookup interface by interface's IP address or interface index. */
8cc4198f 1057static struct interface *
b892f1dd 1058ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
718e3744 1059{
1060 struct prefix_ipv4 p;
1061 struct route_node *rn;
1062 struct interface *ifp;
718e3744 1063
1064 if (addr)
1065 {
1066 p.family = AF_INET;
1067 p.prefixlen = IPV4_MAX_PREFIXLEN;
1068 p.prefix = *addr;
1069
1070 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1071 if (! rn)
1072 return NULL;
55cd0f61 1073
718e3744 1074 ifp = rn->info;
1075 route_unlock_node (rn);
1076 return ifp;
1077 }
1078 else
7e2b7603 1079 return if_lookup_by_index(ifindex, VRF_DEFAULT);
718e3744 1080}
8cc4198f 1081#endif /* ifaddr_ipv4_table */
718e3744 1082
f4e14fdb 1083void if_terminate(struct vrf *vrf)
4bd045d5 1084{
f4e14fdb 1085 struct interface *ifp;
4bd045d5 1086
55cd0f61
DS
1087 while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
1088 ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name);
1089
d62a17ae 1090 if (ifp->node) {
1091 ifp->node->info = NULL;
1092 route_unlock_node(ifp->node);
1093 }
d62a17ae 1094 if_delete(ifp);
1095 }
4bd045d5 1096}
8ccc7e80 1097
d62a17ae 1098const char *if_link_type_str(enum zebra_link_type llt)
8ccc7e80 1099{
d62a17ae 1100 switch (llt) {
8ccc7e80 1101#define llts(T,S) case (T): return (S)
d62a17ae 1102 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
1103 llts(ZEBRA_LLT_ETHER, "Ethernet");
1104 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
1105 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
1106 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
1107 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
1108 llts(ZEBRA_LLT_ARCNET, "ARCnet");
1109 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
1110 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
1111 llts(ZEBRA_LLT_ATM, "ATM");
1112 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
1113 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
1114 llts(ZEBRA_LLT_EUI64, "EUI-64");
1115 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
1116 llts(ZEBRA_LLT_SLIP, "SLIP");
1117 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
1118 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
1119 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
1120 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
1121 llts(ZEBRA_LLT_X25, "CCITT X.25");
1122 llts(ZEBRA_LLT_PPP, "PPP");
1123 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
1124 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1125 llts(ZEBRA_LLT_LAPB, "LAPB");
1126 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1127 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1128 llts(ZEBRA_LLT_FRAD, "FRAD");
1129 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1130 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1131 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1132 llts(ZEBRA_LLT_FDDI, "FDDI");
1133 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1134 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1135 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1136 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1137 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1138 llts(ZEBRA_LLT_IRDA, "IrDA");
1139 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1140 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1141 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1142 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1143 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1144 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1145 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1146 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1147 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1148 default:
1149 zlog_warn("Unknown value %d", llt);
1150 return "Unknown type!";
8ccc7e80 1151#undef llts
d62a17ae 1152 }
1153 return NULL;
8ccc7e80 1154}
16f1b9ee 1155
d62a17ae 1156struct if_link_params *if_link_params_get(struct interface *ifp)
16f1b9ee 1157{
d62a17ae 1158 int i;
16f1b9ee 1159
d62a17ae 1160 if (ifp->link_params != NULL)
1161 return ifp->link_params;
16f1b9ee 1162
d62a17ae 1163 struct if_link_params *iflp =
1164 XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params));
1165 if (iflp == NULL)
1166 return NULL;
16f1b9ee 1167
d62a17ae 1168 /* Set TE metric equal to standard metric */
1169 iflp->te_metric = ifp->metric;
16f1b9ee 1170
d62a17ae 1171 /* Compute default bandwidth based on interface */
1172 iflp->default_bw =
1173 ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1174 * TE_KILO_BIT / TE_BYTE);
16f1b9ee 1175
d62a17ae 1176 /* Set Max, Reservable and Unreserved Bandwidth */
1177 iflp->max_bw = iflp->default_bw;
1178 iflp->max_rsv_bw = iflp->default_bw;
1179 for (i = 0; i < MAX_CLASS_TYPE; i++)
1180 iflp->unrsv_bw[i] = iflp->default_bw;
16f1b9ee 1181
d62a17ae 1182 /* Update Link parameters status */
1183 iflp->lp_status =
1184 LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
16f1b9ee 1185
d62a17ae 1186 /* Finally attach newly created Link Parameters */
1187 ifp->link_params = iflp;
16f1b9ee 1188
d62a17ae 1189 return iflp;
16f1b9ee
OD
1190}
1191
d62a17ae 1192void if_link_params_free(struct interface *ifp)
16f1b9ee 1193{
d62a17ae 1194 if (ifp->link_params == NULL)
1195 return;
1196 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1197 ifp->link_params = NULL;
16f1b9ee 1198}