]>
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" | |
30 | #include "if.h" | |
31 | #include "sockunion.h" | |
32 | #include "prefix.h" | |
718e3744 | 33 | #include "memory.h" |
34 | #include "table.h" | |
35 | #include "buffer.h" | |
36 | #include "str.h" | |
37 | #include "log.h" | |
6b0655a2 | 38 | |
718e3744 | 39 | /* Master list of interfaces. */ |
40 | struct list *iflist; | |
41 | ||
42 | /* One for each program. This structure is needed to store hooks. */ | |
43 | struct if_master | |
44 | { | |
45 | int (*if_new_hook) (struct interface *); | |
46 | int (*if_delete_hook) (struct interface *); | |
47 | } if_master; | |
6b0655a2 | 48 | |
3a0391a9 | 49 | /* Compare interface names, returning an integer greater than, equal to, or |
50 | * less than 0, (following the strcmp convention), according to the | |
51 | * relationship between ifp1 and ifp2. Interface names consist of an | |
52 | * alphabetic prefix and a numeric suffix. The primary sort key is | |
53 | * lexicographic by name, and then numeric by number. No number sorts | |
54 | * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty < | |
55 | * devpty0, de0 < del0 | |
56 | */ | |
106d2fd5 | 57 | int |
58 | if_cmp_func (struct interface *ifp1, struct interface *ifp2) | |
59 | { | |
60 | unsigned int l1, l2; | |
61 | long int x1, x2; | |
62 | char *p1, *p2; | |
63 | int res; | |
64 | ||
65 | p1 = ifp1->name; | |
66 | p2 = ifp2->name; | |
67 | ||
90578521 | 68 | while (*p1 && *p2) { |
106d2fd5 | 69 | /* look up to any number */ |
70 | l1 = strcspn(p1, "0123456789"); | |
71 | l2 = strcspn(p2, "0123456789"); | |
72 | ||
73 | /* name lengths are different -> compare names */ | |
74 | if (l1 != l2) | |
75 | return (strcmp(p1, p2)); | |
76 | ||
3a0391a9 | 77 | /* Note that this relies on all numbers being less than all letters, so |
78 | * that de0 < del0. | |
79 | */ | |
106d2fd5 | 80 | res = strncmp(p1, p2, l1); |
81 | ||
82 | /* names are different -> compare them */ | |
83 | if (res) | |
84 | return res; | |
85 | ||
86 | /* with identical name part, go to numeric part */ | |
106d2fd5 | 87 | p1 += l1; |
88 | p2 += l1; | |
89 | ||
b06c14f2 | 90 | if (!*p1) |
91 | return -1; | |
92 | if (!*p2) | |
93 | return 1; | |
94 | ||
106d2fd5 | 95 | x1 = strtol(p1, &p1, 10); |
96 | x2 = strtol(p2, &p2, 10); | |
97 | ||
98 | /* let's compare numbers now */ | |
99 | if (x1 < x2) | |
100 | return -1; | |
101 | if (x1 > x2) | |
102 | return 1; | |
103 | ||
104 | /* numbers were equal, lets do it again.. | |
105 | (it happens with name like "eth123.456:789") */ | |
106 | } | |
90578521 | 107 | if (*p1) |
108 | return 1; | |
109 | if (*p2) | |
110 | return -1; | |
111 | return 0; | |
106d2fd5 | 112 | } |
113 | ||
718e3744 | 114 | /* Create new interface structure. */ |
718e3744 | 115 | struct interface * |
9035efaa | 116 | if_create (const char *name, int namelen) |
718e3744 | 117 | { |
118 | struct interface *ifp; | |
119 | ||
d2fc8896 | 120 | ifp = XCALLOC (MTYPE_IF, sizeof (struct interface)); |
121 | ifp->ifindex = IFINDEX_INTERNAL; | |
718e3744 | 122 | |
106d2fd5 | 123 | assert (name); |
d2fc8896 | 124 | assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */ |
106d2fd5 | 125 | strncpy (ifp->name, name, namelen); |
d2fc8896 | 126 | ifp->name[namelen] = '\0'; |
e90fbabd | 127 | if (if_lookup_by_name(ifp->name) == NULL) |
128 | listnode_add_sort (iflist, ifp); | |
d2fc8896 | 129 | else |
130 | zlog_err("if_create(%s): corruption detected -- interface with this " | |
131 | "name exists already!", ifp->name); | |
718e3744 | 132 | ifp->connected = list_new (); |
133 | ifp->connected->del = (void (*) (void *)) connected_free; | |
134 | ||
a80beece DS |
135 | ifp->nbr_connected = list_new (); |
136 | ifp->nbr_connected->del = (void (*) (void *)) nbr_connected_free; | |
137 | ||
c9506a0a DS |
138 | /* Enable Link-detection by default */ |
139 | SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION); | |
140 | ||
718e3744 | 141 | if (if_master.if_new_hook) |
142 | (*if_master.if_new_hook) (ifp); | |
143 | ||
144 | return ifp; | |
145 | } | |
146 | ||
d2fc8896 | 147 | /* Delete interface structure. */ |
718e3744 | 148 | void |
d2fc8896 | 149 | if_delete_retain (struct interface *ifp) |
718e3744 | 150 | { |
718e3744 | 151 | if (if_master.if_delete_hook) |
152 | (*if_master.if_delete_hook) (ifp); | |
153 | ||
154 | /* Free connected address list */ | |
2dd04c5d | 155 | list_delete_all_node (ifp->connected); |
a80beece DS |
156 | |
157 | /* Free connected nbr address list */ | |
158 | list_delete_all_node (ifp->nbr_connected); | |
d2fc8896 | 159 | } |
160 | ||
161 | /* Delete and free interface structure. */ | |
162 | void | |
163 | if_delete (struct interface *ifp) | |
164 | { | |
165 | listnode_delete (iflist, ifp); | |
166 | ||
167 | if_delete_retain(ifp); | |
718e3744 | 168 | |
2dd04c5d | 169 | list_free (ifp->connected); |
a80beece | 170 | list_free (ifp->nbr_connected); |
2dd04c5d | 171 | |
718e3744 | 172 | XFREE (MTYPE_IF, ifp); |
173 | } | |
174 | ||
175 | /* Add hook to interface master. */ | |
176 | void | |
177 | if_add_hook (int type, int (*func)(struct interface *ifp)) | |
178 | { | |
179 | switch (type) { | |
180 | case IF_NEW_HOOK: | |
181 | if_master.if_new_hook = func; | |
182 | break; | |
183 | case IF_DELETE_HOOK: | |
184 | if_master.if_delete_hook = func; | |
185 | break; | |
186 | default: | |
187 | break; | |
188 | } | |
189 | } | |
190 | ||
191 | /* Interface existance check by index. */ | |
192 | struct interface * | |
193 | if_lookup_by_index (unsigned int index) | |
194 | { | |
52dc7ee6 | 195 | struct listnode *node; |
718e3744 | 196 | struct interface *ifp; |
197 | ||
1eb8ef25 | 198 | for (ALL_LIST_ELEMENTS_RO(iflist, node, ifp)) |
718e3744 | 199 | { |
718e3744 | 200 | if (ifp->ifindex == index) |
201 | return ifp; | |
202 | } | |
203 | return NULL; | |
204 | } | |
205 | ||
8cc4198f | 206 | const char * |
718e3744 | 207 | ifindex2ifname (unsigned int index) |
208 | { | |
718e3744 | 209 | struct interface *ifp; |
210 | ||
d2fc8896 | 211 | return ((ifp = if_lookup_by_index(index)) != NULL) ? |
8cc4198f | 212 | ifp->name : "unknown"; |
d2fc8896 | 213 | } |
214 | ||
215 | unsigned int | |
216 | ifname2ifindex (const char *name) | |
217 | { | |
218 | struct interface *ifp; | |
219 | ||
3e4ee959 PJ |
220 | return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp->ifindex |
221 | : IFINDEX_INTERNAL; | |
718e3744 | 222 | } |
223 | ||
224 | /* Interface existance check by interface name. */ | |
225 | struct interface * | |
9035efaa | 226 | if_lookup_by_name (const char *name) |
718e3744 | 227 | { |
52dc7ee6 | 228 | struct listnode *node; |
718e3744 | 229 | struct interface *ifp; |
3e4ee959 PJ |
230 | |
231 | if (name) | |
232 | for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp)) | |
233 | { | |
234 | if (strcmp(name, ifp->name) == 0) | |
235 | return ifp; | |
236 | } | |
718e3744 | 237 | return NULL; |
238 | } | |
239 | ||
a349198f | 240 | struct interface * |
241 | if_lookup_by_name_len(const char *name, size_t namelen) | |
242 | { | |
243 | struct listnode *node; | |
1eb8ef25 | 244 | struct interface *ifp; |
a349198f | 245 | |
246 | if (namelen > INTERFACE_NAMSIZ) | |
247 | return NULL; | |
248 | ||
1eb8ef25 | 249 | for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp)) |
a349198f | 250 | { |
a349198f | 251 | if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0')) |
252 | return ifp; | |
253 | } | |
254 | return NULL; | |
255 | } | |
256 | ||
718e3744 | 257 | /* Lookup interface by IPv4 address. */ |
258 | struct interface * | |
259 | if_lookup_exact_address (struct in_addr src) | |
260 | { | |
52dc7ee6 | 261 | struct listnode *node; |
262 | struct listnode *cnode; | |
718e3744 | 263 | struct interface *ifp; |
264 | struct prefix *p; | |
265 | struct connected *c; | |
266 | ||
1eb8ef25 | 267 | for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp)) |
718e3744 | 268 | { |
1eb8ef25 | 269 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c)) |
718e3744 | 270 | { |
718e3744 | 271 | p = c->address; |
272 | ||
273 | if (p && p->family == AF_INET) | |
274 | { | |
275 | if (IPV4_ADDR_SAME (&p->u.prefix4, &src)) | |
276 | return ifp; | |
277 | } | |
278 | } | |
279 | } | |
280 | return NULL; | |
281 | } | |
282 | ||
283 | /* Lookup interface by IPv4 address. */ | |
284 | struct interface * | |
285 | if_lookup_address (struct in_addr src) | |
286 | { | |
52dc7ee6 | 287 | struct listnode *node; |
718e3744 | 288 | struct prefix addr; |
3fb9cd6e | 289 | int bestlen = 0; |
52dc7ee6 | 290 | struct listnode *cnode; |
718e3744 | 291 | struct interface *ifp; |
718e3744 | 292 | struct connected *c; |
293 | struct interface *match; | |
294 | ||
718e3744 | 295 | addr.family = AF_INET; |
296 | addr.u.prefix4 = src; | |
297 | addr.prefixlen = IPV4_MAX_BITLEN; | |
298 | ||
299 | match = NULL; | |
300 | ||
1eb8ef25 | 301 | for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp)) |
718e3744 | 302 | { |
1eb8ef25 | 303 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c)) |
718e3744 | 304 | { |
e4529636 AS |
305 | if (c->address && (c->address->family == AF_INET) && |
306 | prefix_match(CONNECTED_PREFIX(c), &addr) && | |
307 | (c->address->prefixlen > bestlen)) | |
718e3744 | 308 | { |
e4529636 AS |
309 | bestlen = c->address->prefixlen; |
310 | match = ifp; | |
718e3744 | 311 | } |
312 | } | |
313 | } | |
314 | return match; | |
315 | } | |
316 | ||
b81e97a8 DD |
317 | /* Lookup interface by prefix */ |
318 | struct interface * | |
319 | if_lookup_prefix (struct prefix *prefix) | |
320 | { | |
321 | struct listnode *node; | |
322 | struct prefix addr; | |
323 | int bestlen = 0; | |
324 | struct listnode *cnode; | |
325 | struct interface *ifp; | |
326 | struct connected *c; | |
327 | ||
328 | for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp)) | |
329 | { | |
330 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c)) | |
331 | { | |
332 | if (prefix_cmp(c->address, prefix) == 0) | |
333 | { | |
334 | return ifp; | |
335 | } | |
336 | } | |
337 | } | |
338 | return NULL; | |
339 | } | |
340 | ||
718e3744 | 341 | /* Get interface by name if given name interface doesn't exist create |
342 | one. */ | |
343 | struct interface * | |
9035efaa | 344 | if_get_by_name (const char *name) |
718e3744 | 345 | { |
346 | struct interface *ifp; | |
347 | ||
a349198f | 348 | return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp : |
08dbfb69 | 349 | if_create(name, strlen(name)); |
a349198f | 350 | } |
351 | ||
352 | struct interface * | |
353 | if_get_by_name_len(const char *name, size_t namelen) | |
354 | { | |
355 | struct interface *ifp; | |
356 | ||
357 | return ((ifp = if_lookup_by_name_len(name, namelen)) != NULL) ? ifp : | |
358 | if_create(name, namelen); | |
718e3744 | 359 | } |
360 | ||
361 | /* Does interface up ? */ | |
362 | int | |
363 | if_is_up (struct interface *ifp) | |
364 | { | |
365 | return ifp->flags & IFF_UP; | |
366 | } | |
367 | ||
2e3b2e47 | 368 | /* Is interface running? */ |
369 | int | |
370 | if_is_running (struct interface *ifp) | |
371 | { | |
372 | return ifp->flags & IFF_RUNNING; | |
373 | } | |
374 | ||
375 | /* Is the interface operative, eg. either UP & RUNNING | |
376 | or UP & !ZEBRA_INTERFACE_LINK_DETECTION */ | |
377 | int | |
378 | if_is_operative (struct interface *ifp) | |
379 | { | |
380 | return ((ifp->flags & IFF_UP) && | |
381 | (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))); | |
382 | } | |
383 | ||
718e3744 | 384 | /* Is this loopback interface ? */ |
385 | int | |
386 | if_is_loopback (struct interface *ifp) | |
387 | { | |
4ba9b924 | 388 | /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M |
389 | * but Y on platform N? | |
390 | */ | |
391 | return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL)); | |
718e3744 | 392 | } |
393 | ||
394 | /* Does this interface support broadcast ? */ | |
395 | int | |
396 | if_is_broadcast (struct interface *ifp) | |
397 | { | |
398 | return ifp->flags & IFF_BROADCAST; | |
399 | } | |
400 | ||
401 | /* Does this interface support broadcast ? */ | |
402 | int | |
403 | if_is_pointopoint (struct interface *ifp) | |
404 | { | |
405 | return ifp->flags & IFF_POINTOPOINT; | |
406 | } | |
407 | ||
408 | /* Does this interface support multicast ? */ | |
409 | int | |
410 | if_is_multicast (struct interface *ifp) | |
411 | { | |
412 | return ifp->flags & IFF_MULTICAST; | |
413 | } | |
414 | ||
415 | /* Printout flag information into log */ | |
416 | const char * | |
417 | if_flag_dump (unsigned long flag) | |
418 | { | |
419 | int separator = 0; | |
420 | static char logbuf[BUFSIZ]; | |
421 | ||
422 | #define IFF_OUT_LOG(X,STR) \ | |
4ba9b924 | 423 | if (flag & (X)) \ |
718e3744 | 424 | { \ |
425 | if (separator) \ | |
426 | strlcat (logbuf, ",", BUFSIZ); \ | |
427 | else \ | |
428 | separator = 1; \ | |
429 | strlcat (logbuf, STR, BUFSIZ); \ | |
430 | } | |
431 | ||
630c97ce | 432 | strlcpy (logbuf, "<", BUFSIZ); |
718e3744 | 433 | IFF_OUT_LOG (IFF_UP, "UP"); |
434 | IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST"); | |
435 | IFF_OUT_LOG (IFF_DEBUG, "DEBUG"); | |
436 | IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK"); | |
437 | IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT"); | |
438 | IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS"); | |
439 | IFF_OUT_LOG (IFF_RUNNING, "RUNNING"); | |
440 | IFF_OUT_LOG (IFF_NOARP, "NOARP"); | |
441 | IFF_OUT_LOG (IFF_PROMISC, "PROMISC"); | |
442 | IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI"); | |
443 | IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE"); | |
444 | IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX"); | |
445 | IFF_OUT_LOG (IFF_LINK0, "LINK0"); | |
446 | IFF_OUT_LOG (IFF_LINK1, "LINK1"); | |
447 | IFF_OUT_LOG (IFF_LINK2, "LINK2"); | |
448 | IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST"); | |
4ba9b924 | 449 | IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT"); |
450 | IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH"); | |
451 | IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL"); | |
452 | IFF_OUT_LOG (IFF_IPV4, "IPv4"); | |
453 | IFF_OUT_LOG (IFF_IPV6, "IPv6"); | |
718e3744 | 454 | |
455 | strlcat (logbuf, ">", BUFSIZ); | |
456 | ||
457 | return logbuf; | |
630c97ce | 458 | #undef IFF_OUT_LOG |
718e3744 | 459 | } |
460 | ||
461 | /* For debugging */ | |
8cc4198f | 462 | static void |
cedd7f2f | 463 | if_dump (const struct interface *ifp) |
718e3744 | 464 | { |
52dc7ee6 | 465 | struct listnode *node; |
1eb8ef25 | 466 | struct connected *c; |
718e3744 | 467 | |
23be94ea PJ |
468 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, c)) |
469 | zlog_info ("Interface %s index %d metric %d mtu %d " | |
4a7aac1b | 470 | #ifdef HAVE_IPV6 |
23be94ea | 471 | "mtu6 %d " |
4a7aac1b | 472 | #endif /* HAVE_IPV6 */ |
23be94ea PJ |
473 | "%s", |
474 | ifp->name, ifp->ifindex, ifp->metric, ifp->mtu, | |
4a7aac1b | 475 | #ifdef HAVE_IPV6 |
23be94ea | 476 | ifp->mtu6, |
4a7aac1b | 477 | #endif /* HAVE_IPV6 */ |
23be94ea | 478 | if_flag_dump (ifp->flags)); |
718e3744 | 479 | } |
480 | ||
481 | /* Interface printing for all interface. */ | |
482 | void | |
66e5cd87 | 483 | if_dump_all (void) |
718e3744 | 484 | { |
52dc7ee6 | 485 | struct listnode *node; |
1eb8ef25 | 486 | void *p; |
718e3744 | 487 | |
1eb8ef25 | 488 | for (ALL_LIST_ELEMENTS_RO (iflist, node, p)) |
489 | if_dump (p); | |
718e3744 | 490 | } |
491 | ||
492 | DEFUN (interface_desc, | |
493 | interface_desc_cmd, | |
494 | "description .LINE", | |
495 | "Interface specific description\n" | |
496 | "Characters describing this interface\n") | |
497 | { | |
718e3744 | 498 | struct interface *ifp; |
718e3744 | 499 | |
500 | if (argc == 0) | |
501 | return CMD_SUCCESS; | |
502 | ||
503 | ifp = vty->index; | |
504 | if (ifp->desc) | |
3b8b1855 | 505 | XFREE (MTYPE_TMP, ifp->desc); |
506 | ifp->desc = argv_concat(argv, argc, 0); | |
718e3744 | 507 | |
508 | return CMD_SUCCESS; | |
509 | } | |
510 | ||
511 | DEFUN (no_interface_desc, | |
512 | no_interface_desc_cmd, | |
513 | "no description", | |
514 | NO_STR | |
515 | "Interface specific description\n") | |
516 | { | |
517 | struct interface *ifp; | |
518 | ||
519 | ifp = vty->index; | |
520 | if (ifp->desc) | |
0241684e | 521 | XFREE (MTYPE_TMP, ifp->desc); |
718e3744 | 522 | ifp->desc = NULL; |
523 | ||
524 | return CMD_SUCCESS; | |
525 | } | |
6b0655a2 | 526 | |
98954844 PJ |
527 | #ifdef SUNOS_5 |
528 | /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created | |
529 | * a seperate struct interface for each logical interface, so config | |
530 | * file may be full of 'interface fooX:Y'. Solaris however does not | |
531 | * expose logical interfaces via PF_ROUTE, so trying to track logical | |
532 | * interfaces can be fruitless, for that reason Quagga only tracks | |
533 | * the primary IP interface. | |
534 | * | |
535 | * We try accomodate SUNWzebra by: | |
536 | * - looking up the interface name, to see whether it exists, if so | |
537 | * its useable | |
538 | * - for protocol daemons, this could only because zebra told us of | |
539 | * the interface | |
540 | * - for zebra, only because it learnt from kernel | |
541 | * - if not: | |
542 | * - search the name to see if it contains a sub-ipif / logical interface | |
543 | * seperator, the ':' char. If it does: | |
544 | * - text up to that char must be the primary name - get that name. | |
545 | * if not: | |
546 | * - no idea, just get the name in its entirety. | |
547 | */ | |
548 | static struct interface * | |
549 | if_sunwzebra_get (const char *name, size_t nlen) | |
550 | { | |
551 | struct interface *ifp; | |
552 | size_t seppos = 0; | |
718e3744 | 553 | |
98954844 PJ |
554 | if ( (ifp = if_lookup_by_name_len(name, nlen)) != NULL) |
555 | return ifp; | |
556 | ||
557 | /* hunt the primary interface name... */ | |
558 | while (seppos < nlen && name[seppos] != ':') | |
559 | seppos++; | |
560 | ||
561 | /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */ | |
562 | if (seppos < nlen) | |
563 | return if_get_by_name_len (name, seppos); | |
564 | else | |
565 | return if_get_by_name_len (name, nlen); | |
566 | } | |
567 | #endif /* SUNOS_5 */ | |
6b0655a2 | 568 | |
718e3744 | 569 | DEFUN (interface, |
570 | interface_cmd, | |
571 | "interface IFNAME", | |
572 | "Select an interface to configure\n" | |
573 | "Interface's name\n") | |
574 | { | |
575 | struct interface *ifp; | |
d2fc8896 | 576 | size_t sl; |
577 | ||
578 | if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ) | |
579 | { | |
580 | vty_out (vty, "%% Interface name %s is invalid: length exceeds " | |
581 | "%d characters%s", | |
582 | argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE); | |
583 | return CMD_WARNING; | |
584 | } | |
718e3744 | 585 | |
98954844 PJ |
586 | #ifdef SUNOS_5 |
587 | ifp = if_sunwzebra_get (argv[0], sl); | |
588 | #else | |
a349198f | 589 | ifp = if_get_by_name_len(argv[0], sl); |
98954844 | 590 | #endif /* SUNOS_5 */ |
718e3744 | 591 | |
718e3744 | 592 | vty->index = ifp; |
593 | vty->node = INTERFACE_NODE; | |
594 | ||
595 | return CMD_SUCCESS; | |
596 | } | |
597 | ||
32d2463c | 598 | DEFUN_NOSH (no_interface, |
599 | no_interface_cmd, | |
600 | "no interface IFNAME", | |
601 | NO_STR | |
602 | "Delete a pseudo interface's configuration\n" | |
603 | "Interface's name\n") | |
604 | { | |
605 | // deleting interface | |
606 | struct interface *ifp; | |
607 | ||
608 | ifp = if_lookup_by_name (argv[0]); | |
609 | ||
610 | if (ifp == NULL) | |
d2fc8896 | 611 | { |
612 | vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE); | |
613 | return CMD_WARNING; | |
614 | } | |
32d2463c | 615 | |
bfc13532 | 616 | if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
d2fc8896 | 617 | { |
618 | vty_out (vty, "%% Only inactive interfaces can be deleted%s", | |
619 | VTY_NEWLINE); | |
620 | return CMD_WARNING; | |
621 | } | |
32d2463c | 622 | |
623 | if_delete(ifp); | |
624 | ||
625 | return CMD_SUCCESS; | |
626 | } | |
627 | ||
718e3744 | 628 | /* For debug purpose. */ |
629 | DEFUN (show_address, | |
630 | show_address_cmd, | |
631 | "show address", | |
632 | SHOW_STR | |
633 | "address\n") | |
634 | { | |
52dc7ee6 | 635 | struct listnode *node; |
636 | struct listnode *node2; | |
718e3744 | 637 | struct interface *ifp; |
638 | struct connected *ifc; | |
639 | struct prefix *p; | |
640 | ||
1eb8ef25 | 641 | for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp)) |
718e3744 | 642 | { |
1eb8ef25 | 643 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc)) |
718e3744 | 644 | { |
718e3744 | 645 | p = ifc->address; |
646 | ||
647 | if (p->family == AF_INET) | |
648 | vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen, | |
649 | VTY_NEWLINE); | |
650 | } | |
651 | } | |
652 | return CMD_SUCCESS; | |
653 | } | |
654 | ||
655 | /* Allocate connected structure. */ | |
656 | struct connected * | |
8cc4198f | 657 | connected_new (void) |
718e3744 | 658 | { |
393deb9b | 659 | return XCALLOC (MTYPE_CONNECTED, sizeof (struct connected)); |
718e3744 | 660 | } |
661 | ||
a80beece DS |
662 | /* Allocate nbr connected structure. */ |
663 | struct nbr_connected * | |
664 | nbr_connected_new (void) | |
665 | { | |
666 | return XCALLOC (MTYPE_NBR_CONNECTED, sizeof (struct nbr_connected)); | |
667 | } | |
668 | ||
718e3744 | 669 | /* Free connected structure. */ |
670 | void | |
671 | connected_free (struct connected *connected) | |
672 | { | |
673 | if (connected->address) | |
674 | prefix_free (connected->address); | |
675 | ||
676 | if (connected->destination) | |
677 | prefix_free (connected->destination); | |
678 | ||
679 | if (connected->label) | |
9c4f1c6f | 680 | XFREE (MTYPE_CONNECTED_LABEL, connected->label); |
718e3744 | 681 | |
682 | XFREE (MTYPE_CONNECTED, connected); | |
683 | } | |
684 | ||
a80beece DS |
685 | /* Free nbr connected structure. */ |
686 | void | |
687 | nbr_connected_free (struct nbr_connected *connected) | |
688 | { | |
689 | if (connected->address) | |
690 | prefix_free (connected->address); | |
691 | ||
692 | XFREE (MTYPE_NBR_CONNECTED, connected); | |
693 | } | |
694 | ||
695 | /* If same interface nbr address already exists... */ | |
696 | struct nbr_connected * | |
697 | nbr_connected_check (struct interface *ifp, struct prefix *p) | |
698 | { | |
699 | struct nbr_connected *ifc; | |
700 | struct listnode *node; | |
701 | ||
702 | for (ALL_LIST_ELEMENTS_RO (ifp->nbr_connected, node, ifc)) | |
703 | if (prefix_same (ifc->address, p)) | |
704 | return ifc; | |
705 | ||
706 | return NULL; | |
707 | } | |
708 | ||
718e3744 | 709 | /* Print if_addr structure. */ |
8cc4198f | 710 | static void __attribute__ ((unused)) |
718e3744 | 711 | connected_log (struct connected *connected, char *str) |
712 | { | |
713 | struct prefix *p; | |
714 | struct interface *ifp; | |
715 | char logbuf[BUFSIZ]; | |
716 | char buf[BUFSIZ]; | |
717 | ||
718 | ifp = connected->ifp; | |
719 | p = connected->address; | |
720 | ||
721 | snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ", | |
722 | str, ifp->name, prefix_family_str (p), | |
723 | inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), | |
724 | p->prefixlen); | |
725 | ||
726 | p = connected->destination; | |
727 | if (p) | |
728 | { | |
729 | strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), | |
730 | BUFSIZ - strlen(logbuf)); | |
731 | } | |
fc95186c | 732 | zlog (NULL, LOG_INFO, "%s", logbuf); |
718e3744 | 733 | } |
734 | ||
a80beece DS |
735 | /* Print if_addr structure. */ |
736 | static void __attribute__ ((unused)) | |
737 | nbr_connected_log (struct nbr_connected *connected, char *str) | |
738 | { | |
739 | struct prefix *p; | |
740 | struct interface *ifp; | |
741 | char logbuf[BUFSIZ]; | |
742 | char buf[BUFSIZ]; | |
743 | ||
744 | ifp = connected->ifp; | |
745 | p = connected->address; | |
746 | ||
747 | snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ", | |
748 | str, ifp->name, prefix_family_str (p), | |
749 | inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), | |
750 | p->prefixlen); | |
751 | ||
752 | zlog (NULL, LOG_INFO, "%s", logbuf); | |
753 | } | |
754 | ||
718e3744 | 755 | /* If two connected address has same prefix return 1. */ |
8cc4198f | 756 | static int |
718e3744 | 757 | connected_same_prefix (struct prefix *p1, struct prefix *p2) |
758 | { | |
759 | if (p1->family == p2->family) | |
760 | { | |
761 | if (p1->family == AF_INET && | |
762 | IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4)) | |
763 | return 1; | |
764 | #ifdef HAVE_IPV6 | |
765 | if (p1->family == AF_INET6 && | |
766 | IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6)) | |
767 | return 1; | |
768 | #endif /* HAVE_IPV6 */ | |
769 | } | |
770 | return 0; | |
771 | } | |
772 | ||
773 | struct connected * | |
774 | connected_delete_by_prefix (struct interface *ifp, struct prefix *p) | |
775 | { | |
776 | struct listnode *node; | |
777 | struct listnode *next; | |
778 | struct connected *ifc; | |
779 | ||
780 | /* In case of same prefix come, replace it with new one. */ | |
781 | for (node = listhead (ifp->connected); node; node = next) | |
782 | { | |
1eb8ef25 | 783 | ifc = listgetdata (node); |
718e3744 | 784 | next = node->next; |
785 | ||
786 | if (connected_same_prefix (ifc->address, p)) | |
787 | { | |
788 | listnode_delete (ifp->connected, ifc); | |
789 | return ifc; | |
790 | } | |
791 | } | |
792 | return NULL; | |
793 | } | |
794 | ||
727d104b | 795 | /* Find the IPv4 address on our side that will be used when packets |
796 | are sent to dst. */ | |
797 | struct connected * | |
798 | connected_lookup_address (struct interface *ifp, struct in_addr dst) | |
799 | { | |
800 | struct prefix addr; | |
52dc7ee6 | 801 | struct listnode *cnode; |
727d104b | 802 | struct connected *c; |
803 | struct connected *match; | |
804 | ||
727d104b | 805 | addr.family = AF_INET; |
806 | addr.u.prefix4 = dst; | |
807 | addr.prefixlen = IPV4_MAX_BITLEN; | |
808 | ||
809 | match = NULL; | |
810 | ||
1eb8ef25 | 811 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c)) |
727d104b | 812 | { |
e4529636 AS |
813 | if (c->address && (c->address->family == AF_INET) && |
814 | prefix_match(CONNECTED_PREFIX(c), &addr) && | |
815 | (!match || (c->address->prefixlen > match->address->prefixlen))) | |
816 | match = c; | |
727d104b | 817 | } |
818 | return match; | |
819 | } | |
820 | ||
4a7aac1b | 821 | struct connected * |
822 | connected_add_by_prefix (struct interface *ifp, struct prefix *p, | |
823 | struct prefix *destination) | |
824 | { | |
825 | struct connected *ifc; | |
826 | ||
827 | /* Allocate new connected address. */ | |
828 | ifc = connected_new (); | |
829 | ifc->ifp = ifp; | |
830 | ||
831 | /* Fetch interface address */ | |
832 | ifc->address = prefix_new(); | |
833 | memcpy (ifc->address, p, sizeof(struct prefix)); | |
834 | ||
835 | /* Fetch dest address */ | |
3fb9cd6e | 836 | if (destination) |
837 | { | |
838 | ifc->destination = prefix_new(); | |
839 | memcpy (ifc->destination, destination, sizeof(struct prefix)); | |
840 | } | |
4a7aac1b | 841 | |
842 | /* Add connected address to the interface. */ | |
843 | listnode_add (ifp->connected, ifc); | |
844 | return ifc; | |
845 | } | |
846 | ||
718e3744 | 847 | #ifndef HAVE_IF_NAMETOINDEX |
848 | unsigned int | |
849 | if_nametoindex (const char *name) | |
850 | { | |
718e3744 | 851 | struct interface *ifp; |
852 | ||
018546e9 | 853 | return ((ifp = if_lookup_by_name_len(name, strnlen(name, IFNAMSIZ))) != NULL) |
854 | ? ifp->ifindex : 0; | |
718e3744 | 855 | } |
856 | #endif | |
857 | ||
858 | #ifndef HAVE_IF_INDEXTONAME | |
859 | char * | |
860 | if_indextoname (unsigned int ifindex, char *name) | |
861 | { | |
718e3744 | 862 | struct interface *ifp; |
863 | ||
d2fc8896 | 864 | if (!(ifp = if_lookup_by_index(ifindex))) |
865 | return NULL; | |
866 | strncpy (name, ifp->name, IFNAMSIZ); | |
867 | return ifp->name; | |
718e3744 | 868 | } |
869 | #endif | |
6b0655a2 | 870 | |
8cc4198f | 871 | #if 0 /* this route_table of struct connected's is unused |
872 | * however, it would be good to use a route_table rather than | |
873 | * a list.. | |
874 | */ | |
718e3744 | 875 | /* Interface looking up by interface's address. */ |
718e3744 | 876 | /* Interface's IPv4 address reverse lookup table. */ |
877 | struct route_table *ifaddr_ipv4_table; | |
878 | /* struct route_table *ifaddr_ipv6_table; */ | |
879 | ||
8cc4198f | 880 | static void |
718e3744 | 881 | ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp) |
882 | { | |
883 | struct route_node *rn; | |
884 | struct prefix_ipv4 p; | |
885 | ||
886 | p.family = AF_INET; | |
887 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
888 | p.prefix = *ifaddr; | |
889 | ||
890 | rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p); | |
891 | if (rn) | |
892 | { | |
893 | route_unlock_node (rn); | |
894 | zlog_info ("ifaddr_ipv4_add(): address %s is already added", | |
895 | inet_ntoa (*ifaddr)); | |
896 | return; | |
897 | } | |
898 | rn->info = ifp; | |
899 | } | |
900 | ||
8cc4198f | 901 | static void |
718e3744 | 902 | ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp) |
903 | { | |
904 | struct route_node *rn; | |
905 | struct prefix_ipv4 p; | |
906 | ||
907 | p.family = AF_INET; | |
908 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
909 | p.prefix = *ifaddr; | |
910 | ||
911 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); | |
912 | if (! rn) | |
913 | { | |
914 | zlog_info ("ifaddr_ipv4_delete(): can't find address %s", | |
915 | inet_ntoa (*ifaddr)); | |
916 | return; | |
917 | } | |
918 | rn->info = NULL; | |
919 | route_unlock_node (rn); | |
920 | route_unlock_node (rn); | |
921 | } | |
922 | ||
923 | /* Lookup interface by interface's IP address or interface index. */ | |
8cc4198f | 924 | static struct interface * |
718e3744 | 925 | ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex) |
926 | { | |
927 | struct prefix_ipv4 p; | |
928 | struct route_node *rn; | |
929 | struct interface *ifp; | |
718e3744 | 930 | |
931 | if (addr) | |
932 | { | |
933 | p.family = AF_INET; | |
934 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
935 | p.prefix = *addr; | |
936 | ||
937 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); | |
938 | if (! rn) | |
939 | return NULL; | |
940 | ||
941 | ifp = rn->info; | |
942 | route_unlock_node (rn); | |
943 | return ifp; | |
944 | } | |
945 | else | |
d2fc8896 | 946 | return if_lookup_by_index(ifindex); |
718e3744 | 947 | } |
8cc4198f | 948 | #endif /* ifaddr_ipv4_table */ |
718e3744 | 949 | |
950 | /* Initialize interface list. */ | |
951 | void | |
8cc4198f | 952 | if_init (void) |
718e3744 | 953 | { |
954 | iflist = list_new (); | |
8cc4198f | 955 | #if 0 |
718e3744 | 956 | ifaddr_ipv4_table = route_table_init (); |
8cc4198f | 957 | #endif /* ifaddr_ipv4_table */ |
718e3744 | 958 | |
106d2fd5 | 959 | if (iflist) { |
960 | iflist->cmp = (int (*)(void *, void *))if_cmp_func; | |
718e3744 | 961 | return; |
106d2fd5 | 962 | } |
718e3744 | 963 | |
964 | memset (&if_master, 0, sizeof if_master); | |
965 | } | |
4bd045d5 TG |
966 | |
967 | void | |
968 | if_terminate (void) | |
969 | { | |
970 | for (;;) | |
971 | { | |
972 | struct interface *ifp; | |
973 | ||
974 | ifp = listnode_head (iflist); | |
975 | if (ifp == NULL) | |
976 | break; | |
977 | ||
978 | if_delete (ifp); | |
979 | } | |
980 | ||
981 | list_delete (iflist); | |
982 | iflist = NULL; | |
983 | } |