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