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