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