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