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) |
paul | bfc1353 | 2003-05-24 06:40:04 +0000 | [diff] [blame^] | 456 | { |
| 457 | vty_out (vty, "%% Inteface %s does not exist%s", argv[0], VTY_NEWLINE); |
| 458 | return CMD_WARNING; |
| 459 | } |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 460 | |
paul | bfc1353 | 2003-05-24 06:40:04 +0000 | [diff] [blame^] | 461 | if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
| 462 | { |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 463 | vty_out (vty, "%% Only inactive interfaces can be deleted%s", |
| 464 | VTY_NEWLINE); |
| 465 | return CMD_WARNING; |
| 466 | } |
| 467 | |
| 468 | if_delete(ifp); |
| 469 | |
| 470 | return CMD_SUCCESS; |
| 471 | } |
| 472 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 473 | /* For debug purpose. */ |
| 474 | DEFUN (show_address, |
| 475 | show_address_cmd, |
| 476 | "show address", |
| 477 | SHOW_STR |
| 478 | "address\n") |
| 479 | { |
| 480 | listnode node; |
| 481 | listnode node2; |
| 482 | struct interface *ifp; |
| 483 | struct connected *ifc; |
| 484 | struct prefix *p; |
| 485 | |
| 486 | for (node = listhead (iflist); node; nextnode (node)) |
| 487 | { |
| 488 | ifp = getdata (node); |
| 489 | |
| 490 | for (node2 = listhead (ifp->connected); node2; nextnode (node2)) |
| 491 | { |
| 492 | ifc = getdata (node2); |
| 493 | p = ifc->address; |
| 494 | |
| 495 | if (p->family == AF_INET) |
| 496 | vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen, |
| 497 | VTY_NEWLINE); |
| 498 | } |
| 499 | } |
| 500 | return CMD_SUCCESS; |
| 501 | } |
| 502 | |
| 503 | /* Allocate connected structure. */ |
| 504 | struct connected * |
| 505 | connected_new () |
| 506 | { |
| 507 | struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected)); |
| 508 | memset (new, 0, sizeof (struct connected)); |
| 509 | return new; |
| 510 | } |
| 511 | |
| 512 | /* Free connected structure. */ |
| 513 | void |
| 514 | connected_free (struct connected *connected) |
| 515 | { |
| 516 | if (connected->address) |
| 517 | prefix_free (connected->address); |
| 518 | |
| 519 | if (connected->destination) |
| 520 | prefix_free (connected->destination); |
| 521 | |
| 522 | if (connected->label) |
| 523 | free (connected->label); |
| 524 | |
| 525 | XFREE (MTYPE_CONNECTED, connected); |
| 526 | } |
| 527 | |
| 528 | /* Print if_addr structure. */ |
| 529 | void |
| 530 | connected_log (struct connected *connected, char *str) |
| 531 | { |
| 532 | struct prefix *p; |
| 533 | struct interface *ifp; |
| 534 | char logbuf[BUFSIZ]; |
| 535 | char buf[BUFSIZ]; |
| 536 | |
| 537 | ifp = connected->ifp; |
| 538 | p = connected->address; |
| 539 | |
| 540 | snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ", |
| 541 | str, ifp->name, prefix_family_str (p), |
| 542 | inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), |
| 543 | p->prefixlen); |
| 544 | |
| 545 | p = connected->destination; |
| 546 | if (p) |
| 547 | { |
| 548 | strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), |
| 549 | BUFSIZ - strlen(logbuf)); |
| 550 | } |
| 551 | zlog (NULL, LOG_INFO, logbuf); |
| 552 | } |
| 553 | |
| 554 | /* If two connected address has same prefix return 1. */ |
| 555 | int |
| 556 | connected_same_prefix (struct prefix *p1, struct prefix *p2) |
| 557 | { |
| 558 | if (p1->family == p2->family) |
| 559 | { |
| 560 | if (p1->family == AF_INET && |
| 561 | IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4)) |
| 562 | return 1; |
| 563 | #ifdef HAVE_IPV6 |
| 564 | if (p1->family == AF_INET6 && |
| 565 | IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6)) |
| 566 | return 1; |
| 567 | #endif /* HAVE_IPV6 */ |
| 568 | } |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | struct connected * |
| 573 | connected_delete_by_prefix (struct interface *ifp, struct prefix *p) |
| 574 | { |
| 575 | struct listnode *node; |
| 576 | struct listnode *next; |
| 577 | struct connected *ifc; |
| 578 | |
| 579 | /* In case of same prefix come, replace it with new one. */ |
| 580 | for (node = listhead (ifp->connected); node; node = next) |
| 581 | { |
| 582 | ifc = getdata (node); |
| 583 | next = node->next; |
| 584 | |
| 585 | if (connected_same_prefix (ifc->address, p)) |
| 586 | { |
| 587 | listnode_delete (ifp->connected, ifc); |
| 588 | return ifc; |
| 589 | } |
| 590 | } |
| 591 | return NULL; |
| 592 | } |
| 593 | |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 594 | /* Find the IPv4 address on our side that will be used when packets |
| 595 | are sent to dst. */ |
| 596 | struct connected * |
| 597 | connected_lookup_address (struct interface *ifp, struct in_addr dst) |
| 598 | { |
| 599 | struct prefix addr; |
| 600 | struct prefix best; |
| 601 | listnode cnode; |
| 602 | struct prefix *p; |
| 603 | struct connected *c; |
| 604 | struct connected *match; |
| 605 | |
| 606 | /* Zero structures - get rid of rubbish from stack */ |
| 607 | memset(&addr, 0, sizeof(addr)); |
| 608 | memset(&best, 0, sizeof(best)); |
| 609 | |
| 610 | addr.family = AF_INET; |
| 611 | addr.u.prefix4 = dst; |
| 612 | addr.prefixlen = IPV4_MAX_BITLEN; |
| 613 | |
| 614 | match = NULL; |
| 615 | |
| 616 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) |
| 617 | { |
| 618 | c = getdata (cnode); |
| 619 | |
| 620 | if (if_is_pointopoint (ifp)) |
| 621 | { |
| 622 | p = c->address; |
| 623 | |
| 624 | if (p && p->family == AF_INET) |
| 625 | { |
| 626 | #ifdef OLD_RIB /* PTP links are conventionally identified |
| 627 | by the address of the far end - MAG */ |
| 628 | if (IPV4_ADDR_SAME (&p->u.prefix4, &dst)) |
| 629 | return c; |
| 630 | #endif |
| 631 | p = c->destination; |
| 632 | if (p && IPV4_ADDR_SAME (&p->u.prefix4, &dst)) |
| 633 | return c; |
| 634 | } |
| 635 | } |
| 636 | else |
| 637 | { |
| 638 | p = c->address; |
| 639 | |
| 640 | if (p->family == AF_INET) |
| 641 | { |
| 642 | if (prefix_match (p, &addr) && p->prefixlen > best.prefixlen) |
| 643 | { |
| 644 | best = *p; |
| 645 | match = c; |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | return match; |
| 651 | } |
| 652 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 653 | /* Check the connected information is PtP style or not. */ |
| 654 | int |
| 655 | ifc_pointopoint (struct connected *ifc) |
| 656 | { |
| 657 | struct prefix *p; |
| 658 | int ptp = 0; |
| 659 | |
| 660 | /* When interface has PtP flag. */ |
| 661 | if (if_is_pointopoint (ifc->ifp)) |
| 662 | return 1; |
| 663 | |
| 664 | /* RFC3021 PtP check. */ |
| 665 | p = ifc->address; |
| 666 | |
| 667 | if (p->family == AF_INET) |
| 668 | ptp = (p->prefixlen >= IPV4_MAX_PREFIXLEN - 1); |
| 669 | #ifdef HAVE_IPV6 |
| 670 | if (p->family == AF_INET6) |
| 671 | ptp = (p->prefixlen >= IPV6_MAX_PREFIXLEN - 1); |
| 672 | #endif /* HAVE_IPV6 */ |
| 673 | |
| 674 | return ptp; |
| 675 | } |
| 676 | |
| 677 | #ifndef HAVE_IF_NAMETOINDEX |
| 678 | unsigned int |
| 679 | if_nametoindex (const char *name) |
| 680 | { |
| 681 | listnode node; |
| 682 | struct interface *ifp; |
| 683 | |
| 684 | for (node = listhead (iflist); node; nextnode (node)) |
| 685 | { |
| 686 | ifp = getdata (node); |
| 687 | if (strcmp (ifp->name, name) == 0) |
| 688 | return ifp->ifindex; |
| 689 | } |
| 690 | return 0; |
| 691 | } |
| 692 | #endif |
| 693 | |
| 694 | #ifndef HAVE_IF_INDEXTONAME |
| 695 | char * |
| 696 | if_indextoname (unsigned int ifindex, char *name) |
| 697 | { |
| 698 | listnode node; |
| 699 | struct interface *ifp; |
| 700 | |
| 701 | for (node = listhead (iflist); node; nextnode (node)) |
| 702 | { |
| 703 | ifp = getdata (node); |
| 704 | if (ifp->ifindex == ifindex) |
| 705 | { |
| 706 | memcpy (name, ifp->name, IFNAMSIZ); |
| 707 | return ifp->name; |
| 708 | } |
| 709 | } |
| 710 | return NULL; |
| 711 | } |
| 712 | #endif |
| 713 | |
| 714 | /* Interface looking up by interface's address. */ |
| 715 | |
| 716 | /* Interface's IPv4 address reverse lookup table. */ |
| 717 | struct route_table *ifaddr_ipv4_table; |
| 718 | /* struct route_table *ifaddr_ipv6_table; */ |
| 719 | |
| 720 | void |
| 721 | ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp) |
| 722 | { |
| 723 | struct route_node *rn; |
| 724 | struct prefix_ipv4 p; |
| 725 | |
| 726 | p.family = AF_INET; |
| 727 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 728 | p.prefix = *ifaddr; |
| 729 | |
| 730 | rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p); |
| 731 | if (rn) |
| 732 | { |
| 733 | route_unlock_node (rn); |
| 734 | zlog_info ("ifaddr_ipv4_add(): address %s is already added", |
| 735 | inet_ntoa (*ifaddr)); |
| 736 | return; |
| 737 | } |
| 738 | rn->info = ifp; |
| 739 | } |
| 740 | |
| 741 | void |
| 742 | ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp) |
| 743 | { |
| 744 | struct route_node *rn; |
| 745 | struct prefix_ipv4 p; |
| 746 | |
| 747 | p.family = AF_INET; |
| 748 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 749 | p.prefix = *ifaddr; |
| 750 | |
| 751 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); |
| 752 | if (! rn) |
| 753 | { |
| 754 | zlog_info ("ifaddr_ipv4_delete(): can't find address %s", |
| 755 | inet_ntoa (*ifaddr)); |
| 756 | return; |
| 757 | } |
| 758 | rn->info = NULL; |
| 759 | route_unlock_node (rn); |
| 760 | route_unlock_node (rn); |
| 761 | } |
| 762 | |
| 763 | /* Lookup interface by interface's IP address or interface index. */ |
| 764 | struct interface * |
| 765 | ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex) |
| 766 | { |
| 767 | struct prefix_ipv4 p; |
| 768 | struct route_node *rn; |
| 769 | struct interface *ifp; |
| 770 | listnode node; |
| 771 | |
| 772 | if (addr) |
| 773 | { |
| 774 | p.family = AF_INET; |
| 775 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 776 | p.prefix = *addr; |
| 777 | |
| 778 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); |
| 779 | if (! rn) |
| 780 | return NULL; |
| 781 | |
| 782 | ifp = rn->info; |
| 783 | route_unlock_node (rn); |
| 784 | return ifp; |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | for (node = listhead (iflist); node; nextnode (node)) |
| 789 | { |
| 790 | ifp = getdata (node); |
| 791 | |
| 792 | if (ifp->ifindex == ifindex) |
| 793 | return ifp; |
| 794 | } |
| 795 | } |
| 796 | return NULL; |
| 797 | } |
| 798 | |
| 799 | /* Initialize interface list. */ |
| 800 | void |
| 801 | if_init () |
| 802 | { |
| 803 | iflist = list_new (); |
| 804 | ifaddr_ipv4_table = route_table_init (); |
| 805 | |
| 806 | if (iflist) |
| 807 | return; |
| 808 | |
| 809 | memset (&if_master, 0, sizeof if_master); |
| 810 | } |