paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Interface functions. |
| 3 | * Copyright (C) 1997, 98 Kunihiro Ishiguro |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published |
| 9 | * by the Free Software Foundation; either version 2, or (at your |
| 10 | * option) any later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the |
| 19 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 | * Boston, MA 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
| 25 | #include "linklist.h" |
| 26 | #include "vector.h" |
| 27 | #include "vty.h" |
| 28 | #include "command.h" |
| 29 | #include "if.h" |
| 30 | #include "sockunion.h" |
| 31 | #include "prefix.h" |
| 32 | #include "zebra/connected.h" |
| 33 | #include "memory.h" |
| 34 | #include "table.h" |
| 35 | #include "buffer.h" |
| 36 | #include "str.h" |
| 37 | #include "log.h" |
| 38 | |
| 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 | |
| 49 | /* Create new interface structure. */ |
| 50 | struct interface * |
| 51 | if_new () |
| 52 | { |
| 53 | struct interface *ifp; |
| 54 | |
| 55 | ifp = XMALLOC (MTYPE_IF, sizeof (struct interface)); |
| 56 | memset (ifp, 0, sizeof (struct interface)); |
| 57 | return ifp; |
| 58 | } |
| 59 | |
| 60 | struct interface * |
| 61 | if_create () |
| 62 | { |
| 63 | struct interface *ifp; |
| 64 | |
| 65 | ifp = if_new (); |
| 66 | |
| 67 | listnode_add (iflist, ifp); |
| 68 | ifp->connected = list_new (); |
| 69 | ifp->connected->del = (void (*) (void *)) connected_free; |
| 70 | |
| 71 | if (if_master.if_new_hook) |
| 72 | (*if_master.if_new_hook) (ifp); |
| 73 | |
| 74 | return ifp; |
| 75 | } |
| 76 | |
| 77 | /* Delete and free interface structure. */ |
| 78 | void |
| 79 | if_delete (struct interface *ifp) |
| 80 | { |
| 81 | listnode_delete (iflist, ifp); |
| 82 | |
| 83 | if (if_master.if_delete_hook) |
| 84 | (*if_master.if_delete_hook) (ifp); |
| 85 | |
| 86 | /* Free connected address list */ |
| 87 | list_delete (ifp->connected); |
| 88 | |
| 89 | XFREE (MTYPE_IF, ifp); |
| 90 | } |
| 91 | |
| 92 | /* Add hook to interface master. */ |
| 93 | void |
| 94 | if_add_hook (int type, int (*func)(struct interface *ifp)) |
| 95 | { |
| 96 | switch (type) { |
| 97 | case IF_NEW_HOOK: |
| 98 | if_master.if_new_hook = func; |
| 99 | break; |
| 100 | case IF_DELETE_HOOK: |
| 101 | if_master.if_delete_hook = func; |
| 102 | break; |
| 103 | default: |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* Interface existance check by index. */ |
| 109 | struct interface * |
| 110 | if_lookup_by_index (unsigned int index) |
| 111 | { |
| 112 | listnode node; |
| 113 | struct interface *ifp; |
| 114 | |
| 115 | for (node = listhead (iflist); node; nextnode (node)) |
| 116 | { |
| 117 | ifp = getdata (node); |
| 118 | if (ifp->ifindex == index) |
| 119 | return ifp; |
| 120 | } |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | char * |
| 125 | ifindex2ifname (unsigned int index) |
| 126 | { |
| 127 | listnode node; |
| 128 | struct interface *ifp; |
| 129 | |
| 130 | for (node = listhead (iflist); node; nextnode (node)) |
| 131 | { |
| 132 | ifp = getdata (node); |
| 133 | if (ifp->ifindex == index) |
| 134 | return ifp->name; |
| 135 | } |
| 136 | return "unknown"; |
| 137 | } |
| 138 | |
| 139 | /* Interface existance check by interface name. */ |
| 140 | struct interface * |
| 141 | if_lookup_by_name (char *name) |
| 142 | { |
| 143 | listnode node; |
| 144 | struct interface *ifp; |
| 145 | |
| 146 | for (node = listhead (iflist); node; nextnode (node)) |
| 147 | { |
| 148 | ifp = getdata (node); |
| 149 | if (strncmp (name, ifp->name, sizeof ifp->name) == 0) |
| 150 | return ifp; |
| 151 | } |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | /* Lookup interface by IPv4 address. */ |
| 156 | struct interface * |
| 157 | if_lookup_exact_address (struct in_addr src) |
| 158 | { |
| 159 | listnode node; |
| 160 | listnode cnode; |
| 161 | struct interface *ifp; |
| 162 | struct prefix *p; |
| 163 | struct connected *c; |
| 164 | |
| 165 | for (node = listhead (iflist); node; nextnode (node)) |
| 166 | { |
| 167 | ifp = getdata (node); |
| 168 | |
| 169 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) |
| 170 | { |
| 171 | c = getdata (cnode); |
| 172 | |
| 173 | p = c->address; |
| 174 | |
| 175 | if (p && p->family == AF_INET) |
| 176 | { |
| 177 | if (IPV4_ADDR_SAME (&p->u.prefix4, &src)) |
| 178 | return ifp; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | /* Lookup interface by IPv4 address. */ |
| 186 | struct interface * |
| 187 | if_lookup_address (struct in_addr src) |
| 188 | { |
| 189 | listnode node; |
| 190 | struct prefix addr; |
| 191 | struct prefix best; |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 192 | struct prefix peer; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | listnode cnode; |
| 194 | struct interface *ifp; |
| 195 | struct prefix *p; |
| 196 | struct connected *c; |
| 197 | struct interface *match; |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 198 | int prefixlen; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 199 | |
| 200 | /* Zero structures - get rid of rubbish from stack */ |
| 201 | memset(&addr, 0, sizeof(addr)); |
| 202 | memset(&best, 0, sizeof(best)); |
| 203 | |
| 204 | addr.family = AF_INET; |
| 205 | addr.u.prefix4 = src; |
| 206 | addr.prefixlen = IPV4_MAX_BITLEN; |
| 207 | |
| 208 | match = NULL; |
| 209 | |
| 210 | for (node = listhead (iflist); node; nextnode (node)) |
| 211 | { |
| 212 | ifp = getdata (node); |
| 213 | |
| 214 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) |
| 215 | { |
| 216 | c = getdata (cnode); |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 217 | p = c->address; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 218 | |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 219 | if (p->family == AF_INET) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 220 | { |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 221 | prefixlen = p->prefixlen; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 222 | |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 223 | if (if_is_pointopoint (ifp) || |
| 224 | prefixlen >= IPV4_MAX_PREFIXLEN - 1) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | { |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 226 | peer = *c->destination; |
| 227 | peer.prefixlen = prefixlen; |
| 228 | p = &peer; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 229 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 230 | |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 231 | if (prefix_match (p, &addr) && prefixlen > best.prefixlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 232 | { |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 233 | best = *p; |
| 234 | match = ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | return match; |
| 240 | } |
| 241 | |
| 242 | /* Get interface by name if given name interface doesn't exist create |
| 243 | one. */ |
| 244 | struct interface * |
| 245 | if_get_by_name (char *name) |
| 246 | { |
| 247 | struct interface *ifp; |
| 248 | |
| 249 | ifp = if_lookup_by_name (name); |
| 250 | if (ifp == NULL) |
| 251 | { |
| 252 | ifp = if_create (); |
| 253 | strncpy (ifp->name, name, IFNAMSIZ); |
| 254 | } |
| 255 | return ifp; |
| 256 | } |
| 257 | |
| 258 | /* Does interface up ? */ |
| 259 | int |
| 260 | if_is_up (struct interface *ifp) |
| 261 | { |
| 262 | return ifp->flags & IFF_UP; |
| 263 | } |
| 264 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 265 | /* Is interface running? */ |
| 266 | int |
| 267 | if_is_running (struct interface *ifp) |
| 268 | { |
| 269 | return ifp->flags & IFF_RUNNING; |
| 270 | } |
| 271 | |
| 272 | /* Is the interface operative, eg. either UP & RUNNING |
| 273 | or UP & !ZEBRA_INTERFACE_LINK_DETECTION */ |
| 274 | int |
| 275 | if_is_operative (struct interface *ifp) |
| 276 | { |
| 277 | return ((ifp->flags & IFF_UP) && |
| 278 | (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))); |
| 279 | } |
| 280 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 281 | /* Is this loopback interface ? */ |
| 282 | int |
| 283 | if_is_loopback (struct interface *ifp) |
| 284 | { |
| 285 | return ifp->flags & IFF_LOOPBACK; |
| 286 | } |
| 287 | |
| 288 | /* Does this interface support broadcast ? */ |
| 289 | int |
| 290 | if_is_broadcast (struct interface *ifp) |
| 291 | { |
| 292 | return ifp->flags & IFF_BROADCAST; |
| 293 | } |
| 294 | |
| 295 | /* Does this interface support broadcast ? */ |
| 296 | int |
| 297 | if_is_pointopoint (struct interface *ifp) |
| 298 | { |
| 299 | return ifp->flags & IFF_POINTOPOINT; |
| 300 | } |
| 301 | |
| 302 | /* Does this interface support multicast ? */ |
| 303 | int |
| 304 | if_is_multicast (struct interface *ifp) |
| 305 | { |
| 306 | return ifp->flags & IFF_MULTICAST; |
| 307 | } |
| 308 | |
| 309 | /* Printout flag information into log */ |
| 310 | const char * |
| 311 | if_flag_dump (unsigned long flag) |
| 312 | { |
| 313 | int separator = 0; |
| 314 | static char logbuf[BUFSIZ]; |
| 315 | |
| 316 | #define IFF_OUT_LOG(X,STR) \ |
| 317 | if ((X) && (flag & (X))) \ |
| 318 | { \ |
| 319 | if (separator) \ |
| 320 | strlcat (logbuf, ",", BUFSIZ); \ |
| 321 | else \ |
| 322 | separator = 1; \ |
| 323 | strlcat (logbuf, STR, BUFSIZ); \ |
| 324 | } |
| 325 | |
| 326 | strlcpy (logbuf, " <", BUFSIZ); |
| 327 | IFF_OUT_LOG (IFF_UP, "UP"); |
| 328 | IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST"); |
| 329 | IFF_OUT_LOG (IFF_DEBUG, "DEBUG"); |
| 330 | IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK"); |
| 331 | IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT"); |
| 332 | IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS"); |
| 333 | IFF_OUT_LOG (IFF_RUNNING, "RUNNING"); |
| 334 | IFF_OUT_LOG (IFF_NOARP, "NOARP"); |
| 335 | IFF_OUT_LOG (IFF_PROMISC, "PROMISC"); |
| 336 | IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI"); |
| 337 | IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE"); |
| 338 | IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX"); |
| 339 | IFF_OUT_LOG (IFF_LINK0, "LINK0"); |
| 340 | IFF_OUT_LOG (IFF_LINK1, "LINK1"); |
| 341 | IFF_OUT_LOG (IFF_LINK2, "LINK2"); |
| 342 | IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST"); |
| 343 | |
| 344 | strlcat (logbuf, ">", BUFSIZ); |
| 345 | |
| 346 | return logbuf; |
| 347 | } |
| 348 | |
| 349 | /* For debugging */ |
| 350 | void |
| 351 | if_dump (struct interface *ifp) |
| 352 | { |
| 353 | listnode node; |
| 354 | |
| 355 | zlog_info ("Interface %s index %d metric %d mtu %d %s", |
| 356 | ifp->name, ifp->ifindex, ifp->metric, ifp->mtu, |
| 357 | if_flag_dump (ifp->flags)); |
| 358 | |
| 359 | for (node = listhead (ifp->connected); node; nextnode (node)) |
| 360 | ; |
| 361 | } |
| 362 | |
| 363 | /* Interface printing for all interface. */ |
| 364 | void |
| 365 | if_dump_all () |
| 366 | { |
| 367 | listnode node; |
| 368 | |
| 369 | for (node = listhead (iflist); node; nextnode (node)) |
| 370 | if_dump (getdata (node)); |
| 371 | } |
| 372 | |
| 373 | DEFUN (interface_desc, |
| 374 | interface_desc_cmd, |
| 375 | "description .LINE", |
| 376 | "Interface specific description\n" |
| 377 | "Characters describing this interface\n") |
| 378 | { |
| 379 | int i; |
| 380 | struct interface *ifp; |
| 381 | struct buffer *b; |
| 382 | |
| 383 | if (argc == 0) |
| 384 | return CMD_SUCCESS; |
| 385 | |
| 386 | ifp = vty->index; |
| 387 | if (ifp->desc) |
| 388 | XFREE (0, ifp->desc); |
| 389 | |
| 390 | b = buffer_new (1024); |
| 391 | for (i = 0; i < argc; i++) |
| 392 | { |
| 393 | buffer_putstr (b, (u_char *)argv[i]); |
| 394 | buffer_putc (b, ' '); |
| 395 | } |
| 396 | buffer_putc (b, '\0'); |
| 397 | |
| 398 | ifp->desc = buffer_getstr (b); |
| 399 | buffer_free (b); |
| 400 | |
| 401 | return CMD_SUCCESS; |
| 402 | } |
| 403 | |
| 404 | DEFUN (no_interface_desc, |
| 405 | no_interface_desc_cmd, |
| 406 | "no description", |
| 407 | NO_STR |
| 408 | "Interface specific description\n") |
| 409 | { |
| 410 | struct interface *ifp; |
| 411 | |
| 412 | ifp = vty->index; |
| 413 | if (ifp->desc) |
| 414 | XFREE (0, ifp->desc); |
| 415 | ifp->desc = NULL; |
| 416 | |
| 417 | return CMD_SUCCESS; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | /* See also wrapper function zebra_interface() in zebra/interface.c */ |
| 422 | DEFUN (interface, |
| 423 | interface_cmd, |
| 424 | "interface IFNAME", |
| 425 | "Select an interface to configure\n" |
| 426 | "Interface's name\n") |
| 427 | { |
| 428 | struct interface *ifp; |
| 429 | |
| 430 | ifp = if_lookup_by_name (argv[0]); |
| 431 | |
| 432 | if (ifp == NULL) |
| 433 | { |
| 434 | ifp = if_create (); |
| 435 | strncpy (ifp->name, argv[0], INTERFACE_NAMSIZ); |
| 436 | } |
| 437 | vty->index = ifp; |
| 438 | vty->node = INTERFACE_NODE; |
| 439 | |
| 440 | return CMD_SUCCESS; |
| 441 | } |
| 442 | |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 443 | DEFUN_NOSH (no_interface, |
| 444 | no_interface_cmd, |
| 445 | "no interface IFNAME", |
| 446 | NO_STR |
| 447 | "Delete a pseudo interface's configuration\n" |
| 448 | "Interface's name\n") |
| 449 | { |
| 450 | // deleting interface |
| 451 | struct interface *ifp; |
| 452 | |
| 453 | ifp = if_lookup_by_name (argv[0]); |
| 454 | |
| 455 | if (ifp == NULL) |
| 456 | return CMD_SUCCESS; |
| 457 | |
| 458 | if (if_is_up(ifp)) { |
| 459 | vty_out (vty, "%% Only inactive interfaces can be deleted%s", |
| 460 | VTY_NEWLINE); |
| 461 | return CMD_WARNING; |
| 462 | } |
| 463 | |
| 464 | if_delete(ifp); |
| 465 | |
| 466 | return CMD_SUCCESS; |
| 467 | } |
| 468 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 469 | /* For debug purpose. */ |
| 470 | DEFUN (show_address, |
| 471 | show_address_cmd, |
| 472 | "show address", |
| 473 | SHOW_STR |
| 474 | "address\n") |
| 475 | { |
| 476 | listnode node; |
| 477 | listnode node2; |
| 478 | struct interface *ifp; |
| 479 | struct connected *ifc; |
| 480 | struct prefix *p; |
| 481 | |
| 482 | for (node = listhead (iflist); node; nextnode (node)) |
| 483 | { |
| 484 | ifp = getdata (node); |
| 485 | |
| 486 | for (node2 = listhead (ifp->connected); node2; nextnode (node2)) |
| 487 | { |
| 488 | ifc = getdata (node2); |
| 489 | p = ifc->address; |
| 490 | |
| 491 | if (p->family == AF_INET) |
| 492 | vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen, |
| 493 | VTY_NEWLINE); |
| 494 | } |
| 495 | } |
| 496 | return CMD_SUCCESS; |
| 497 | } |
| 498 | |
| 499 | /* Allocate connected structure. */ |
| 500 | struct connected * |
| 501 | connected_new () |
| 502 | { |
| 503 | struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected)); |
| 504 | memset (new, 0, sizeof (struct connected)); |
| 505 | return new; |
| 506 | } |
| 507 | |
| 508 | /* Free connected structure. */ |
| 509 | void |
| 510 | connected_free (struct connected *connected) |
| 511 | { |
| 512 | if (connected->address) |
| 513 | prefix_free (connected->address); |
| 514 | |
| 515 | if (connected->destination) |
| 516 | prefix_free (connected->destination); |
| 517 | |
| 518 | if (connected->label) |
| 519 | free (connected->label); |
| 520 | |
| 521 | XFREE (MTYPE_CONNECTED, connected); |
| 522 | } |
| 523 | |
| 524 | /* Print if_addr structure. */ |
| 525 | void |
| 526 | connected_log (struct connected *connected, char *str) |
| 527 | { |
| 528 | struct prefix *p; |
| 529 | struct interface *ifp; |
| 530 | char logbuf[BUFSIZ]; |
| 531 | char buf[BUFSIZ]; |
| 532 | |
| 533 | ifp = connected->ifp; |
| 534 | p = connected->address; |
| 535 | |
| 536 | snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ", |
| 537 | str, ifp->name, prefix_family_str (p), |
| 538 | inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), |
| 539 | p->prefixlen); |
| 540 | |
| 541 | p = connected->destination; |
| 542 | if (p) |
| 543 | { |
| 544 | strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), |
| 545 | BUFSIZ - strlen(logbuf)); |
| 546 | } |
| 547 | zlog (NULL, LOG_INFO, logbuf); |
| 548 | } |
| 549 | |
| 550 | /* If two connected address has same prefix return 1. */ |
| 551 | int |
| 552 | connected_same_prefix (struct prefix *p1, struct prefix *p2) |
| 553 | { |
| 554 | if (p1->family == p2->family) |
| 555 | { |
| 556 | if (p1->family == AF_INET && |
| 557 | IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4)) |
| 558 | return 1; |
| 559 | #ifdef HAVE_IPV6 |
| 560 | if (p1->family == AF_INET6 && |
| 561 | IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6)) |
| 562 | return 1; |
| 563 | #endif /* HAVE_IPV6 */ |
| 564 | } |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | struct connected * |
| 569 | connected_delete_by_prefix (struct interface *ifp, struct prefix *p) |
| 570 | { |
| 571 | struct listnode *node; |
| 572 | struct listnode *next; |
| 573 | struct connected *ifc; |
| 574 | |
| 575 | /* In case of same prefix come, replace it with new one. */ |
| 576 | for (node = listhead (ifp->connected); node; node = next) |
| 577 | { |
| 578 | ifc = getdata (node); |
| 579 | next = node->next; |
| 580 | |
| 581 | if (connected_same_prefix (ifc->address, p)) |
| 582 | { |
| 583 | listnode_delete (ifp->connected, ifc); |
| 584 | return ifc; |
| 585 | } |
| 586 | } |
| 587 | return NULL; |
| 588 | } |
| 589 | |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 590 | /* Find the IPv4 address on our side that will be used when packets |
| 591 | are sent to dst. */ |
| 592 | struct connected * |
| 593 | connected_lookup_address (struct interface *ifp, struct in_addr dst) |
| 594 | { |
| 595 | struct prefix addr; |
| 596 | struct prefix best; |
| 597 | listnode cnode; |
| 598 | struct prefix *p; |
| 599 | struct connected *c; |
| 600 | struct connected *match; |
| 601 | |
| 602 | /* Zero structures - get rid of rubbish from stack */ |
| 603 | memset(&addr, 0, sizeof(addr)); |
| 604 | memset(&best, 0, sizeof(best)); |
| 605 | |
| 606 | addr.family = AF_INET; |
| 607 | addr.u.prefix4 = dst; |
| 608 | addr.prefixlen = IPV4_MAX_BITLEN; |
| 609 | |
| 610 | match = NULL; |
| 611 | |
| 612 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) |
| 613 | { |
| 614 | c = getdata (cnode); |
| 615 | |
| 616 | if (if_is_pointopoint (ifp)) |
| 617 | { |
| 618 | p = c->address; |
| 619 | |
| 620 | if (p && p->family == AF_INET) |
| 621 | { |
| 622 | #ifdef OLD_RIB /* PTP links are conventionally identified |
| 623 | by the address of the far end - MAG */ |
| 624 | if (IPV4_ADDR_SAME (&p->u.prefix4, &dst)) |
| 625 | return c; |
| 626 | #endif |
| 627 | p = c->destination; |
| 628 | if (p && IPV4_ADDR_SAME (&p->u.prefix4, &dst)) |
| 629 | return c; |
| 630 | } |
| 631 | } |
| 632 | else |
| 633 | { |
| 634 | p = c->address; |
| 635 | |
| 636 | if (p->family == AF_INET) |
| 637 | { |
| 638 | if (prefix_match (p, &addr) && p->prefixlen > best.prefixlen) |
| 639 | { |
| 640 | best = *p; |
| 641 | match = c; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | return match; |
| 647 | } |
| 648 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 649 | /* Check the connected information is PtP style or not. */ |
| 650 | int |
| 651 | ifc_pointopoint (struct connected *ifc) |
| 652 | { |
| 653 | struct prefix *p; |
| 654 | int ptp = 0; |
| 655 | |
| 656 | /* When interface has PtP flag. */ |
| 657 | if (if_is_pointopoint (ifc->ifp)) |
| 658 | return 1; |
| 659 | |
| 660 | /* RFC3021 PtP check. */ |
| 661 | p = ifc->address; |
| 662 | |
| 663 | if (p->family == AF_INET) |
| 664 | ptp = (p->prefixlen >= IPV4_MAX_PREFIXLEN - 1); |
| 665 | #ifdef HAVE_IPV6 |
| 666 | if (p->family == AF_INET6) |
| 667 | ptp = (p->prefixlen >= IPV6_MAX_PREFIXLEN - 1); |
| 668 | #endif /* HAVE_IPV6 */ |
| 669 | |
| 670 | return ptp; |
| 671 | } |
| 672 | |
| 673 | #ifndef HAVE_IF_NAMETOINDEX |
| 674 | unsigned int |
| 675 | if_nametoindex (const char *name) |
| 676 | { |
| 677 | listnode node; |
| 678 | struct interface *ifp; |
| 679 | |
| 680 | for (node = listhead (iflist); node; nextnode (node)) |
| 681 | { |
| 682 | ifp = getdata (node); |
| 683 | if (strcmp (ifp->name, name) == 0) |
| 684 | return ifp->ifindex; |
| 685 | } |
| 686 | return 0; |
| 687 | } |
| 688 | #endif |
| 689 | |
| 690 | #ifndef HAVE_IF_INDEXTONAME |
| 691 | char * |
| 692 | if_indextoname (unsigned int ifindex, char *name) |
| 693 | { |
| 694 | listnode node; |
| 695 | struct interface *ifp; |
| 696 | |
| 697 | for (node = listhead (iflist); node; nextnode (node)) |
| 698 | { |
| 699 | ifp = getdata (node); |
| 700 | if (ifp->ifindex == ifindex) |
| 701 | { |
| 702 | memcpy (name, ifp->name, IFNAMSIZ); |
| 703 | return ifp->name; |
| 704 | } |
| 705 | } |
| 706 | return NULL; |
| 707 | } |
| 708 | #endif |
| 709 | |
| 710 | /* Interface looking up by interface's address. */ |
| 711 | |
| 712 | /* Interface's IPv4 address reverse lookup table. */ |
| 713 | struct route_table *ifaddr_ipv4_table; |
| 714 | /* struct route_table *ifaddr_ipv6_table; */ |
| 715 | |
| 716 | void |
| 717 | ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp) |
| 718 | { |
| 719 | struct route_node *rn; |
| 720 | struct prefix_ipv4 p; |
| 721 | |
| 722 | p.family = AF_INET; |
| 723 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 724 | p.prefix = *ifaddr; |
| 725 | |
| 726 | rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p); |
| 727 | if (rn) |
| 728 | { |
| 729 | route_unlock_node (rn); |
| 730 | zlog_info ("ifaddr_ipv4_add(): address %s is already added", |
| 731 | inet_ntoa (*ifaddr)); |
| 732 | return; |
| 733 | } |
| 734 | rn->info = ifp; |
| 735 | } |
| 736 | |
| 737 | void |
| 738 | ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp) |
| 739 | { |
| 740 | struct route_node *rn; |
| 741 | struct prefix_ipv4 p; |
| 742 | |
| 743 | p.family = AF_INET; |
| 744 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 745 | p.prefix = *ifaddr; |
| 746 | |
| 747 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); |
| 748 | if (! rn) |
| 749 | { |
| 750 | zlog_info ("ifaddr_ipv4_delete(): can't find address %s", |
| 751 | inet_ntoa (*ifaddr)); |
| 752 | return; |
| 753 | } |
| 754 | rn->info = NULL; |
| 755 | route_unlock_node (rn); |
| 756 | route_unlock_node (rn); |
| 757 | } |
| 758 | |
| 759 | /* Lookup interface by interface's IP address or interface index. */ |
| 760 | struct interface * |
| 761 | ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex) |
| 762 | { |
| 763 | struct prefix_ipv4 p; |
| 764 | struct route_node *rn; |
| 765 | struct interface *ifp; |
| 766 | listnode node; |
| 767 | |
| 768 | if (addr) |
| 769 | { |
| 770 | p.family = AF_INET; |
| 771 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 772 | p.prefix = *addr; |
| 773 | |
| 774 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); |
| 775 | if (! rn) |
| 776 | return NULL; |
| 777 | |
| 778 | ifp = rn->info; |
| 779 | route_unlock_node (rn); |
| 780 | return ifp; |
| 781 | } |
| 782 | else |
| 783 | { |
| 784 | for (node = listhead (iflist); node; nextnode (node)) |
| 785 | { |
| 786 | ifp = getdata (node); |
| 787 | |
| 788 | if (ifp->ifindex == ifindex) |
| 789 | return ifp; |
| 790 | } |
| 791 | } |
| 792 | return NULL; |
| 793 | } |
| 794 | |
| 795 | /* Initialize interface list. */ |
| 796 | void |
| 797 | if_init () |
| 798 | { |
| 799 | iflist = list_new (); |
| 800 | ifaddr_ipv4_table = route_table_init (); |
| 801 | |
| 802 | if (iflist) |
| 803 | return; |
| 804 | |
| 805 | memset (&if_master, 0, sizeof if_master); |
| 806 | } |