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