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