]>
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" | |
38 | \f | |
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; | |
48 | \f | |
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 | ||
135 | if (if_master.if_new_hook) | |
136 | (*if_master.if_new_hook) (ifp); | |
137 | ||
138 | return ifp; | |
139 | } | |
140 | ||
d2fc8896 | 141 | /* Delete interface structure. */ |
718e3744 | 142 | void |
d2fc8896 | 143 | if_delete_retain (struct interface *ifp) |
718e3744 | 144 | { |
718e3744 | 145 | if (if_master.if_delete_hook) |
146 | (*if_master.if_delete_hook) (ifp); | |
147 | ||
148 | /* Free connected address list */ | |
149 | list_delete (ifp->connected); | |
d2fc8896 | 150 | } |
151 | ||
152 | /* Delete and free interface structure. */ | |
153 | void | |
154 | if_delete (struct interface *ifp) | |
155 | { | |
156 | listnode_delete (iflist, ifp); | |
157 | ||
158 | if_delete_retain(ifp); | |
718e3744 | 159 | |
160 | XFREE (MTYPE_IF, ifp); | |
161 | } | |
162 | ||
163 | /* Add hook to interface master. */ | |
164 | void | |
165 | if_add_hook (int type, int (*func)(struct interface *ifp)) | |
166 | { | |
167 | switch (type) { | |
168 | case IF_NEW_HOOK: | |
169 | if_master.if_new_hook = func; | |
170 | break; | |
171 | case IF_DELETE_HOOK: | |
172 | if_master.if_delete_hook = func; | |
173 | break; | |
174 | default: | |
175 | break; | |
176 | } | |
177 | } | |
178 | ||
179 | /* Interface existance check by index. */ | |
180 | struct interface * | |
181 | if_lookup_by_index (unsigned int index) | |
182 | { | |
52dc7ee6 | 183 | struct listnode *node; |
718e3744 | 184 | struct interface *ifp; |
185 | ||
186 | for (node = listhead (iflist); node; nextnode (node)) | |
187 | { | |
188 | ifp = getdata (node); | |
189 | if (ifp->ifindex == index) | |
190 | return ifp; | |
191 | } | |
192 | return NULL; | |
193 | } | |
194 | ||
195 | char * | |
196 | ifindex2ifname (unsigned int index) | |
197 | { | |
718e3744 | 198 | struct interface *ifp; |
199 | ||
d2fc8896 | 200 | return ((ifp = if_lookup_by_index(index)) != NULL) ? |
201 | ifp->name : (char *)"unknown"; | |
202 | } | |
203 | ||
204 | unsigned int | |
205 | ifname2ifindex (const char *name) | |
206 | { | |
207 | struct interface *ifp; | |
208 | ||
209 | return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp->ifindex : 0; | |
718e3744 | 210 | } |
211 | ||
212 | /* Interface existance check by interface name. */ | |
213 | struct interface * | |
9035efaa | 214 | if_lookup_by_name (const char *name) |
718e3744 | 215 | { |
52dc7ee6 | 216 | struct listnode *node; |
718e3744 | 217 | struct interface *ifp; |
218 | ||
219 | for (node = listhead (iflist); node; nextnode (node)) | |
220 | { | |
221 | ifp = getdata (node); | |
08dbfb69 | 222 | if (strcmp(name, ifp->name) == 0) |
718e3744 | 223 | return ifp; |
224 | } | |
225 | return NULL; | |
226 | } | |
227 | ||
a349198f | 228 | struct interface * |
229 | if_lookup_by_name_len(const char *name, size_t namelen) | |
230 | { | |
231 | struct listnode *node; | |
232 | ||
233 | if (namelen > INTERFACE_NAMSIZ) | |
234 | return NULL; | |
235 | ||
236 | for (node = listhead (iflist); node; nextnode (node)) | |
237 | { | |
238 | struct interface *ifp; | |
239 | ||
240 | ifp = getdata (node); | |
241 | if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0')) | |
242 | return ifp; | |
243 | } | |
244 | return NULL; | |
245 | } | |
246 | ||
718e3744 | 247 | /* Lookup interface by IPv4 address. */ |
248 | struct interface * | |
249 | if_lookup_exact_address (struct in_addr src) | |
250 | { | |
52dc7ee6 | 251 | struct listnode *node; |
252 | struct listnode *cnode; | |
718e3744 | 253 | struct interface *ifp; |
254 | struct prefix *p; | |
255 | struct connected *c; | |
256 | ||
257 | for (node = listhead (iflist); node; nextnode (node)) | |
258 | { | |
259 | ifp = getdata (node); | |
260 | ||
261 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) | |
262 | { | |
263 | c = getdata (cnode); | |
264 | ||
265 | p = c->address; | |
266 | ||
267 | if (p && p->family == AF_INET) | |
268 | { | |
269 | if (IPV4_ADDR_SAME (&p->u.prefix4, &src)) | |
270 | return ifp; | |
271 | } | |
272 | } | |
273 | } | |
274 | return NULL; | |
275 | } | |
276 | ||
277 | /* Lookup interface by IPv4 address. */ | |
278 | struct interface * | |
279 | if_lookup_address (struct in_addr src) | |
280 | { | |
52dc7ee6 | 281 | struct listnode *node; |
718e3744 | 282 | struct prefix addr; |
3fb9cd6e | 283 | int bestlen = 0; |
52dc7ee6 | 284 | struct listnode *cnode; |
718e3744 | 285 | struct interface *ifp; |
286 | struct prefix *p; | |
287 | struct connected *c; | |
288 | struct interface *match; | |
289 | ||
718e3744 | 290 | addr.family = AF_INET; |
291 | addr.u.prefix4 = src; | |
292 | addr.prefixlen = IPV4_MAX_BITLEN; | |
293 | ||
294 | match = NULL; | |
295 | ||
296 | for (node = listhead (iflist); node; nextnode (node)) | |
297 | { | |
298 | ifp = getdata (node); | |
299 | ||
300 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) | |
301 | { | |
302 | c = getdata (cnode); | |
303 | ||
3fb9cd6e | 304 | if (c->address && (c->address->family == AF_INET)) |
718e3744 | 305 | { |
3fb9cd6e | 306 | if (CONNECTED_POINTOPOINT_HOST(c)) |
718e3744 | 307 | { |
3fb9cd6e | 308 | /* PTP links are conventionally identified |
309 | by the address of the far end - MAG */ | |
310 | if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &src)) | |
31a476c7 | 311 | return ifp; |
718e3744 | 312 | } |
3fb9cd6e | 313 | else |
718e3744 | 314 | { |
3fb9cd6e | 315 | p = c->address; |
316 | ||
317 | if (prefix_match (p, &addr) && p->prefixlen > bestlen) | |
31a476c7 | 318 | { |
3fb9cd6e | 319 | bestlen = p->prefixlen; |
31a476c7 | 320 | match = ifp; |
321 | } | |
718e3744 | 322 | } |
323 | } | |
324 | } | |
325 | } | |
326 | return match; | |
327 | } | |
328 | ||
329 | /* Get interface by name if given name interface doesn't exist create | |
330 | one. */ | |
331 | struct interface * | |
9035efaa | 332 | if_get_by_name (const char *name) |
718e3744 | 333 | { |
334 | struct interface *ifp; | |
335 | ||
a349198f | 336 | return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp : |
08dbfb69 | 337 | if_create(name, strlen(name)); |
a349198f | 338 | } |
339 | ||
340 | struct interface * | |
341 | if_get_by_name_len(const char *name, size_t namelen) | |
342 | { | |
343 | struct interface *ifp; | |
344 | ||
345 | return ((ifp = if_lookup_by_name_len(name, namelen)) != NULL) ? ifp : | |
346 | if_create(name, namelen); | |
718e3744 | 347 | } |
348 | ||
349 | /* Does interface up ? */ | |
350 | int | |
351 | if_is_up (struct interface *ifp) | |
352 | { | |
353 | return ifp->flags & IFF_UP; | |
354 | } | |
355 | ||
2e3b2e47 | 356 | /* Is interface running? */ |
357 | int | |
358 | if_is_running (struct interface *ifp) | |
359 | { | |
360 | return ifp->flags & IFF_RUNNING; | |
361 | } | |
362 | ||
363 | /* Is the interface operative, eg. either UP & RUNNING | |
364 | or UP & !ZEBRA_INTERFACE_LINK_DETECTION */ | |
365 | int | |
366 | if_is_operative (struct interface *ifp) | |
367 | { | |
368 | return ((ifp->flags & IFF_UP) && | |
369 | (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))); | |
370 | } | |
371 | ||
718e3744 | 372 | /* Is this loopback interface ? */ |
373 | int | |
374 | if_is_loopback (struct interface *ifp) | |
375 | { | |
4ba9b924 | 376 | /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M |
377 | * but Y on platform N? | |
378 | */ | |
379 | return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL)); | |
718e3744 | 380 | } |
381 | ||
382 | /* Does this interface support broadcast ? */ | |
383 | int | |
384 | if_is_broadcast (struct interface *ifp) | |
385 | { | |
386 | return ifp->flags & IFF_BROADCAST; | |
387 | } | |
388 | ||
389 | /* Does this interface support broadcast ? */ | |
390 | int | |
391 | if_is_pointopoint (struct interface *ifp) | |
392 | { | |
393 | return ifp->flags & IFF_POINTOPOINT; | |
394 | } | |
395 | ||
396 | /* Does this interface support multicast ? */ | |
397 | int | |
398 | if_is_multicast (struct interface *ifp) | |
399 | { | |
400 | return ifp->flags & IFF_MULTICAST; | |
401 | } | |
402 | ||
403 | /* Printout flag information into log */ | |
404 | const char * | |
405 | if_flag_dump (unsigned long flag) | |
406 | { | |
407 | int separator = 0; | |
408 | static char logbuf[BUFSIZ]; | |
409 | ||
410 | #define IFF_OUT_LOG(X,STR) \ | |
4ba9b924 | 411 | if (flag & (X)) \ |
718e3744 | 412 | { \ |
413 | if (separator) \ | |
414 | strlcat (logbuf, ",", BUFSIZ); \ | |
415 | else \ | |
416 | separator = 1; \ | |
417 | strlcat (logbuf, STR, BUFSIZ); \ | |
418 | } | |
419 | ||
420 | strlcpy (logbuf, " <", BUFSIZ); | |
421 | IFF_OUT_LOG (IFF_UP, "UP"); | |
422 | IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST"); | |
423 | IFF_OUT_LOG (IFF_DEBUG, "DEBUG"); | |
424 | IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK"); | |
425 | IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT"); | |
426 | IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS"); | |
427 | IFF_OUT_LOG (IFF_RUNNING, "RUNNING"); | |
428 | IFF_OUT_LOG (IFF_NOARP, "NOARP"); | |
429 | IFF_OUT_LOG (IFF_PROMISC, "PROMISC"); | |
430 | IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI"); | |
431 | IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE"); | |
432 | IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX"); | |
433 | IFF_OUT_LOG (IFF_LINK0, "LINK0"); | |
434 | IFF_OUT_LOG (IFF_LINK1, "LINK1"); | |
435 | IFF_OUT_LOG (IFF_LINK2, "LINK2"); | |
436 | IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST"); | |
4ba9b924 | 437 | IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT"); |
438 | IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH"); | |
439 | IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL"); | |
440 | IFF_OUT_LOG (IFF_IPV4, "IPv4"); | |
441 | IFF_OUT_LOG (IFF_IPV6, "IPv6"); | |
718e3744 | 442 | |
443 | strlcat (logbuf, ">", BUFSIZ); | |
444 | ||
445 | return logbuf; | |
446 | } | |
447 | ||
448 | /* For debugging */ | |
449 | void | |
450 | if_dump (struct interface *ifp) | |
451 | { | |
52dc7ee6 | 452 | struct listnode *node; |
718e3744 | 453 | |
4a7aac1b | 454 | zlog_info ("Interface %s index %d metric %d mtu %d " |
455 | #ifdef HAVE_IPV6 | |
456 | "mtu6 %d " | |
457 | #endif /* HAVE_IPV6 */ | |
458 | "%s", | |
718e3744 | 459 | ifp->name, ifp->ifindex, ifp->metric, ifp->mtu, |
4a7aac1b | 460 | #ifdef HAVE_IPV6 |
461 | ifp->mtu6, | |
462 | #endif /* HAVE_IPV6 */ | |
718e3744 | 463 | if_flag_dump (ifp->flags)); |
464 | ||
465 | for (node = listhead (ifp->connected); node; nextnode (node)) | |
466 | ; | |
467 | } | |
468 | ||
469 | /* Interface printing for all interface. */ | |
470 | void | |
471 | if_dump_all () | |
472 | { | |
52dc7ee6 | 473 | struct listnode *node; |
718e3744 | 474 | |
475 | for (node = listhead (iflist); node; nextnode (node)) | |
476 | if_dump (getdata (node)); | |
477 | } | |
478 | ||
479 | DEFUN (interface_desc, | |
480 | interface_desc_cmd, | |
481 | "description .LINE", | |
482 | "Interface specific description\n" | |
483 | "Characters describing this interface\n") | |
484 | { | |
718e3744 | 485 | struct interface *ifp; |
718e3744 | 486 | |
487 | if (argc == 0) | |
488 | return CMD_SUCCESS; | |
489 | ||
490 | ifp = vty->index; | |
491 | if (ifp->desc) | |
3b8b1855 | 492 | XFREE (MTYPE_TMP, ifp->desc); |
493 | ifp->desc = argv_concat(argv, argc, 0); | |
718e3744 | 494 | |
495 | return CMD_SUCCESS; | |
496 | } | |
497 | ||
498 | DEFUN (no_interface_desc, | |
499 | no_interface_desc_cmd, | |
500 | "no description", | |
501 | NO_STR | |
502 | "Interface specific description\n") | |
503 | { | |
504 | struct interface *ifp; | |
505 | ||
506 | ifp = vty->index; | |
507 | if (ifp->desc) | |
508 | XFREE (0, ifp->desc); | |
509 | ifp->desc = NULL; | |
510 | ||
511 | return CMD_SUCCESS; | |
512 | } | |
513 | ||
514 | ||
515 | /* See also wrapper function zebra_interface() in zebra/interface.c */ | |
516 | DEFUN (interface, | |
517 | interface_cmd, | |
518 | "interface IFNAME", | |
519 | "Select an interface to configure\n" | |
520 | "Interface's name\n") | |
521 | { | |
522 | struct interface *ifp; | |
d2fc8896 | 523 | size_t sl; |
524 | ||
525 | if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ) | |
526 | { | |
527 | vty_out (vty, "%% Interface name %s is invalid: length exceeds " | |
528 | "%d characters%s", | |
529 | argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE); | |
530 | return CMD_WARNING; | |
531 | } | |
718e3744 | 532 | |
a349198f | 533 | ifp = if_get_by_name_len(argv[0], sl); |
718e3744 | 534 | |
718e3744 | 535 | vty->index = ifp; |
536 | vty->node = INTERFACE_NODE; | |
537 | ||
538 | return CMD_SUCCESS; | |
539 | } | |
540 | ||
32d2463c | 541 | DEFUN_NOSH (no_interface, |
542 | no_interface_cmd, | |
543 | "no interface IFNAME", | |
544 | NO_STR | |
545 | "Delete a pseudo interface's configuration\n" | |
546 | "Interface's name\n") | |
547 | { | |
548 | // deleting interface | |
549 | struct interface *ifp; | |
550 | ||
551 | ifp = if_lookup_by_name (argv[0]); | |
552 | ||
553 | if (ifp == NULL) | |
d2fc8896 | 554 | { |
555 | vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE); | |
556 | return CMD_WARNING; | |
557 | } | |
32d2463c | 558 | |
bfc13532 | 559 | if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
d2fc8896 | 560 | { |
561 | vty_out (vty, "%% Only inactive interfaces can be deleted%s", | |
562 | VTY_NEWLINE); | |
563 | return CMD_WARNING; | |
564 | } | |
32d2463c | 565 | |
566 | if_delete(ifp); | |
567 | ||
568 | return CMD_SUCCESS; | |
569 | } | |
570 | ||
718e3744 | 571 | /* For debug purpose. */ |
572 | DEFUN (show_address, | |
573 | show_address_cmd, | |
574 | "show address", | |
575 | SHOW_STR | |
576 | "address\n") | |
577 | { | |
52dc7ee6 | 578 | struct listnode *node; |
579 | struct listnode *node2; | |
718e3744 | 580 | struct interface *ifp; |
581 | struct connected *ifc; | |
582 | struct prefix *p; | |
583 | ||
584 | for (node = listhead (iflist); node; nextnode (node)) | |
585 | { | |
586 | ifp = getdata (node); | |
587 | ||
588 | for (node2 = listhead (ifp->connected); node2; nextnode (node2)) | |
589 | { | |
590 | ifc = getdata (node2); | |
591 | p = ifc->address; | |
592 | ||
593 | if (p->family == AF_INET) | |
594 | vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen, | |
595 | VTY_NEWLINE); | |
596 | } | |
597 | } | |
598 | return CMD_SUCCESS; | |
599 | } | |
600 | ||
601 | /* Allocate connected structure. */ | |
602 | struct connected * | |
603 | connected_new () | |
604 | { | |
605 | struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected)); | |
606 | memset (new, 0, sizeof (struct connected)); | |
607 | return new; | |
608 | } | |
609 | ||
610 | /* Free connected structure. */ | |
611 | void | |
612 | connected_free (struct connected *connected) | |
613 | { | |
614 | if (connected->address) | |
615 | prefix_free (connected->address); | |
616 | ||
617 | if (connected->destination) | |
618 | prefix_free (connected->destination); | |
619 | ||
620 | if (connected->label) | |
621 | free (connected->label); | |
622 | ||
623 | XFREE (MTYPE_CONNECTED, connected); | |
624 | } | |
625 | ||
626 | /* Print if_addr structure. */ | |
627 | void | |
628 | connected_log (struct connected *connected, char *str) | |
629 | { | |
630 | struct prefix *p; | |
631 | struct interface *ifp; | |
632 | char logbuf[BUFSIZ]; | |
633 | char buf[BUFSIZ]; | |
634 | ||
635 | ifp = connected->ifp; | |
636 | p = connected->address; | |
637 | ||
638 | snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ", | |
639 | str, ifp->name, prefix_family_str (p), | |
640 | inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), | |
641 | p->prefixlen); | |
642 | ||
643 | p = connected->destination; | |
644 | if (p) | |
645 | { | |
646 | strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), | |
647 | BUFSIZ - strlen(logbuf)); | |
648 | } | |
649 | zlog (NULL, LOG_INFO, logbuf); | |
650 | } | |
651 | ||
652 | /* If two connected address has same prefix return 1. */ | |
653 | int | |
654 | connected_same_prefix (struct prefix *p1, struct prefix *p2) | |
655 | { | |
656 | if (p1->family == p2->family) | |
657 | { | |
658 | if (p1->family == AF_INET && | |
659 | IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4)) | |
660 | return 1; | |
661 | #ifdef HAVE_IPV6 | |
662 | if (p1->family == AF_INET6 && | |
663 | IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6)) | |
664 | return 1; | |
665 | #endif /* HAVE_IPV6 */ | |
666 | } | |
667 | return 0; | |
668 | } | |
669 | ||
670 | struct connected * | |
671 | connected_delete_by_prefix (struct interface *ifp, struct prefix *p) | |
672 | { | |
673 | struct listnode *node; | |
674 | struct listnode *next; | |
675 | struct connected *ifc; | |
676 | ||
677 | /* In case of same prefix come, replace it with new one. */ | |
678 | for (node = listhead (ifp->connected); node; node = next) | |
679 | { | |
680 | ifc = getdata (node); | |
681 | next = node->next; | |
682 | ||
683 | if (connected_same_prefix (ifc->address, p)) | |
684 | { | |
685 | listnode_delete (ifp->connected, ifc); | |
686 | return ifc; | |
687 | } | |
688 | } | |
689 | return NULL; | |
690 | } | |
691 | ||
727d104b | 692 | /* Find the IPv4 address on our side that will be used when packets |
693 | are sent to dst. */ | |
694 | struct connected * | |
695 | connected_lookup_address (struct interface *ifp, struct in_addr dst) | |
696 | { | |
697 | struct prefix addr; | |
52dc7ee6 | 698 | struct listnode *cnode; |
727d104b | 699 | struct prefix *p; |
700 | struct connected *c; | |
701 | struct connected *match; | |
702 | ||
727d104b | 703 | addr.family = AF_INET; |
704 | addr.u.prefix4 = dst; | |
705 | addr.prefixlen = IPV4_MAX_BITLEN; | |
706 | ||
707 | match = NULL; | |
708 | ||
709 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) | |
710 | { | |
711 | c = getdata (cnode); | |
712 | ||
3fb9cd6e | 713 | if (c->address && (c->address->family == AF_INET)) |
714 | { | |
715 | if (CONNECTED_POINTOPOINT_HOST(c)) | |
727d104b | 716 | { |
3fb9cd6e | 717 | /* PTP links are conventionally identified |
718 | by the address of the far end - MAG */ | |
719 | if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &dst)) | |
727d104b | 720 | return c; |
721 | } | |
3fb9cd6e | 722 | else |
727d104b | 723 | { |
3fb9cd6e | 724 | p = c->address; |
725 | ||
726 | if (prefix_match (p, &addr) && | |
727 | (!match || (p->prefixlen > match->address->prefixlen))) | |
728 | match = c; | |
727d104b | 729 | } |
3fb9cd6e | 730 | } |
727d104b | 731 | } |
732 | return match; | |
733 | } | |
734 | ||
4a7aac1b | 735 | struct connected * |
736 | connected_add_by_prefix (struct interface *ifp, struct prefix *p, | |
737 | struct prefix *destination) | |
738 | { | |
739 | struct connected *ifc; | |
740 | ||
741 | /* Allocate new connected address. */ | |
742 | ifc = connected_new (); | |
743 | ifc->ifp = ifp; | |
744 | ||
745 | /* Fetch interface address */ | |
746 | ifc->address = prefix_new(); | |
747 | memcpy (ifc->address, p, sizeof(struct prefix)); | |
748 | ||
749 | /* Fetch dest address */ | |
3fb9cd6e | 750 | if (destination) |
751 | { | |
752 | ifc->destination = prefix_new(); | |
753 | memcpy (ifc->destination, destination, sizeof(struct prefix)); | |
754 | } | |
4a7aac1b | 755 | |
756 | /* Add connected address to the interface. */ | |
757 | listnode_add (ifp->connected, ifc); | |
758 | return ifc; | |
759 | } | |
760 | ||
718e3744 | 761 | #ifndef HAVE_IF_NAMETOINDEX |
762 | unsigned int | |
763 | if_nametoindex (const char *name) | |
764 | { | |
718e3744 | 765 | struct interface *ifp; |
766 | ||
018546e9 | 767 | return ((ifp = if_lookup_by_name_len(name, strnlen(name, IFNAMSIZ))) != NULL) |
768 | ? ifp->ifindex : 0; | |
718e3744 | 769 | } |
770 | #endif | |
771 | ||
772 | #ifndef HAVE_IF_INDEXTONAME | |
773 | char * | |
774 | if_indextoname (unsigned int ifindex, char *name) | |
775 | { | |
718e3744 | 776 | struct interface *ifp; |
777 | ||
d2fc8896 | 778 | if (!(ifp = if_lookup_by_index(ifindex))) |
779 | return NULL; | |
780 | strncpy (name, ifp->name, IFNAMSIZ); | |
781 | return ifp->name; | |
718e3744 | 782 | } |
783 | #endif | |
784 | \f | |
785 | /* Interface looking up by interface's address. */ | |
786 | ||
787 | /* Interface's IPv4 address reverse lookup table. */ | |
788 | struct route_table *ifaddr_ipv4_table; | |
789 | /* struct route_table *ifaddr_ipv6_table; */ | |
790 | ||
791 | void | |
792 | ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp) | |
793 | { | |
794 | struct route_node *rn; | |
795 | struct prefix_ipv4 p; | |
796 | ||
797 | p.family = AF_INET; | |
798 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
799 | p.prefix = *ifaddr; | |
800 | ||
801 | rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p); | |
802 | if (rn) | |
803 | { | |
804 | route_unlock_node (rn); | |
805 | zlog_info ("ifaddr_ipv4_add(): address %s is already added", | |
806 | inet_ntoa (*ifaddr)); | |
807 | return; | |
808 | } | |
809 | rn->info = ifp; | |
810 | } | |
811 | ||
812 | void | |
813 | ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp) | |
814 | { | |
815 | struct route_node *rn; | |
816 | struct prefix_ipv4 p; | |
817 | ||
818 | p.family = AF_INET; | |
819 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
820 | p.prefix = *ifaddr; | |
821 | ||
822 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); | |
823 | if (! rn) | |
824 | { | |
825 | zlog_info ("ifaddr_ipv4_delete(): can't find address %s", | |
826 | inet_ntoa (*ifaddr)); | |
827 | return; | |
828 | } | |
829 | rn->info = NULL; | |
830 | route_unlock_node (rn); | |
831 | route_unlock_node (rn); | |
832 | } | |
833 | ||
834 | /* Lookup interface by interface's IP address or interface index. */ | |
835 | struct interface * | |
836 | ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex) | |
837 | { | |
838 | struct prefix_ipv4 p; | |
839 | struct route_node *rn; | |
840 | struct interface *ifp; | |
52dc7ee6 | 841 | struct listnode *node; |
718e3744 | 842 | |
843 | if (addr) | |
844 | { | |
845 | p.family = AF_INET; | |
846 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
847 | p.prefix = *addr; | |
848 | ||
849 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); | |
850 | if (! rn) | |
851 | return NULL; | |
852 | ||
853 | ifp = rn->info; | |
854 | route_unlock_node (rn); | |
855 | return ifp; | |
856 | } | |
857 | else | |
d2fc8896 | 858 | return if_lookup_by_index(ifindex); |
718e3744 | 859 | } |
860 | ||
861 | /* Initialize interface list. */ | |
862 | void | |
863 | if_init () | |
864 | { | |
865 | iflist = list_new (); | |
866 | ifaddr_ipv4_table = route_table_init (); | |
867 | ||
106d2fd5 | 868 | if (iflist) { |
869 | iflist->cmp = (int (*)(void *, void *))if_cmp_func; | |
718e3744 | 870 | return; |
106d2fd5 | 871 | } |
718e3744 | 872 | |
873 | memset (&if_master, 0, sizeof if_master); | |
874 | } |