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; |
| 192 | listnode cnode; |
| 193 | struct interface *ifp; |
| 194 | struct prefix *p; |
| 195 | struct connected *c; |
| 196 | struct interface *match; |
| 197 | |
| 198 | /* Zero structures - get rid of rubbish from stack */ |
| 199 | memset(&addr, 0, sizeof(addr)); |
| 200 | memset(&best, 0, sizeof(best)); |
| 201 | |
| 202 | addr.family = AF_INET; |
| 203 | addr.u.prefix4 = src; |
| 204 | addr.prefixlen = IPV4_MAX_BITLEN; |
| 205 | |
| 206 | match = NULL; |
| 207 | |
| 208 | for (node = listhead (iflist); node; nextnode (node)) |
| 209 | { |
| 210 | ifp = getdata (node); |
| 211 | |
| 212 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) |
| 213 | { |
| 214 | c = getdata (cnode); |
| 215 | |
| 216 | if (if_is_pointopoint (ifp)) |
| 217 | { |
| 218 | p = c->address; |
| 219 | |
| 220 | if (p && p->family == AF_INET) |
| 221 | { |
| 222 | #ifdef OLD_RIB /* PTP links are conventionally identified |
| 223 | by the address of the far end - MAG */ |
| 224 | if (IPV4_ADDR_SAME (&p->u.prefix4, &src)) |
| 225 | return ifp; |
| 226 | #endif |
| 227 | p = c->destination; |
| 228 | if (p && IPV4_ADDR_SAME (&p->u.prefix4, &src)) |
| 229 | return ifp; |
| 230 | } |
| 231 | } |
| 232 | else |
| 233 | { |
| 234 | p = c->address; |
| 235 | |
| 236 | if (p->family == AF_INET) |
| 237 | { |
| 238 | if (prefix_match (p, &addr) && p->prefixlen > best.prefixlen) |
| 239 | { |
| 240 | best = *p; |
| 241 | match = ifp; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | return match; |
| 248 | } |
| 249 | |
| 250 | /* Get interface by name if given name interface doesn't exist create |
| 251 | one. */ |
| 252 | struct interface * |
| 253 | if_get_by_name (char *name) |
| 254 | { |
| 255 | struct interface *ifp; |
| 256 | |
| 257 | ifp = if_lookup_by_name (name); |
| 258 | if (ifp == NULL) |
| 259 | { |
| 260 | ifp = if_create (); |
| 261 | strncpy (ifp->name, name, IFNAMSIZ); |
| 262 | } |
| 263 | return ifp; |
| 264 | } |
| 265 | |
| 266 | /* Does interface up ? */ |
| 267 | int |
| 268 | if_is_up (struct interface *ifp) |
| 269 | { |
| 270 | return ifp->flags & IFF_UP; |
| 271 | } |
| 272 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 273 | /* Is interface running? */ |
| 274 | int |
| 275 | if_is_running (struct interface *ifp) |
| 276 | { |
| 277 | return ifp->flags & IFF_RUNNING; |
| 278 | } |
| 279 | |
| 280 | /* Is the interface operative, eg. either UP & RUNNING |
| 281 | or UP & !ZEBRA_INTERFACE_LINK_DETECTION */ |
| 282 | int |
| 283 | if_is_operative (struct interface *ifp) |
| 284 | { |
| 285 | return ((ifp->flags & IFF_UP) && |
| 286 | (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))); |
| 287 | } |
| 288 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 289 | /* Is this loopback interface ? */ |
| 290 | int |
| 291 | if_is_loopback (struct interface *ifp) |
| 292 | { |
| 293 | return ifp->flags & IFF_LOOPBACK; |
| 294 | } |
| 295 | |
| 296 | /* Does this interface support broadcast ? */ |
| 297 | int |
| 298 | if_is_broadcast (struct interface *ifp) |
| 299 | { |
| 300 | return ifp->flags & IFF_BROADCAST; |
| 301 | } |
| 302 | |
| 303 | /* Does this interface support broadcast ? */ |
| 304 | int |
| 305 | if_is_pointopoint (struct interface *ifp) |
| 306 | { |
| 307 | return ifp->flags & IFF_POINTOPOINT; |
| 308 | } |
| 309 | |
| 310 | /* Does this interface support multicast ? */ |
| 311 | int |
| 312 | if_is_multicast (struct interface *ifp) |
| 313 | { |
| 314 | return ifp->flags & IFF_MULTICAST; |
| 315 | } |
| 316 | |
| 317 | /* Printout flag information into log */ |
| 318 | const char * |
| 319 | if_flag_dump (unsigned long flag) |
| 320 | { |
| 321 | int separator = 0; |
| 322 | static char logbuf[BUFSIZ]; |
| 323 | |
| 324 | #define IFF_OUT_LOG(X,STR) \ |
| 325 | if ((X) && (flag & (X))) \ |
| 326 | { \ |
| 327 | if (separator) \ |
| 328 | strlcat (logbuf, ",", BUFSIZ); \ |
| 329 | else \ |
| 330 | separator = 1; \ |
| 331 | strlcat (logbuf, STR, BUFSIZ); \ |
| 332 | } |
| 333 | |
| 334 | strlcpy (logbuf, " <", BUFSIZ); |
| 335 | IFF_OUT_LOG (IFF_UP, "UP"); |
| 336 | IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST"); |
| 337 | IFF_OUT_LOG (IFF_DEBUG, "DEBUG"); |
| 338 | IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK"); |
| 339 | IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT"); |
| 340 | IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS"); |
| 341 | IFF_OUT_LOG (IFF_RUNNING, "RUNNING"); |
| 342 | IFF_OUT_LOG (IFF_NOARP, "NOARP"); |
| 343 | IFF_OUT_LOG (IFF_PROMISC, "PROMISC"); |
| 344 | IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI"); |
| 345 | IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE"); |
| 346 | IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX"); |
| 347 | IFF_OUT_LOG (IFF_LINK0, "LINK0"); |
| 348 | IFF_OUT_LOG (IFF_LINK1, "LINK1"); |
| 349 | IFF_OUT_LOG (IFF_LINK2, "LINK2"); |
| 350 | IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST"); |
| 351 | |
| 352 | strlcat (logbuf, ">", BUFSIZ); |
| 353 | |
| 354 | return logbuf; |
| 355 | } |
| 356 | |
| 357 | /* For debugging */ |
| 358 | void |
| 359 | if_dump (struct interface *ifp) |
| 360 | { |
| 361 | listnode node; |
| 362 | |
| 363 | zlog_info ("Interface %s index %d metric %d mtu %d %s", |
| 364 | ifp->name, ifp->ifindex, ifp->metric, ifp->mtu, |
| 365 | if_flag_dump (ifp->flags)); |
| 366 | |
| 367 | for (node = listhead (ifp->connected); node; nextnode (node)) |
| 368 | ; |
| 369 | } |
| 370 | |
| 371 | /* Interface printing for all interface. */ |
| 372 | void |
| 373 | if_dump_all () |
| 374 | { |
| 375 | listnode node; |
| 376 | |
| 377 | for (node = listhead (iflist); node; nextnode (node)) |
| 378 | if_dump (getdata (node)); |
| 379 | } |
| 380 | |
| 381 | DEFUN (interface_desc, |
| 382 | interface_desc_cmd, |
| 383 | "description .LINE", |
| 384 | "Interface specific description\n" |
| 385 | "Characters describing this interface\n") |
| 386 | { |
| 387 | int i; |
| 388 | struct interface *ifp; |
| 389 | struct buffer *b; |
| 390 | |
| 391 | if (argc == 0) |
| 392 | return CMD_SUCCESS; |
| 393 | |
| 394 | ifp = vty->index; |
| 395 | if (ifp->desc) |
| 396 | XFREE (0, ifp->desc); |
| 397 | |
| 398 | b = buffer_new (1024); |
| 399 | for (i = 0; i < argc; i++) |
| 400 | { |
| 401 | buffer_putstr (b, (u_char *)argv[i]); |
| 402 | buffer_putc (b, ' '); |
| 403 | } |
| 404 | buffer_putc (b, '\0'); |
| 405 | |
| 406 | ifp->desc = buffer_getstr (b); |
| 407 | buffer_free (b); |
| 408 | |
| 409 | return CMD_SUCCESS; |
| 410 | } |
| 411 | |
| 412 | DEFUN (no_interface_desc, |
| 413 | no_interface_desc_cmd, |
| 414 | "no description", |
| 415 | NO_STR |
| 416 | "Interface specific description\n") |
| 417 | { |
| 418 | struct interface *ifp; |
| 419 | |
| 420 | ifp = vty->index; |
| 421 | if (ifp->desc) |
| 422 | XFREE (0, ifp->desc); |
| 423 | ifp->desc = NULL; |
| 424 | |
| 425 | return CMD_SUCCESS; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | /* See also wrapper function zebra_interface() in zebra/interface.c */ |
| 430 | DEFUN (interface, |
| 431 | interface_cmd, |
| 432 | "interface IFNAME", |
| 433 | "Select an interface to configure\n" |
| 434 | "Interface's name\n") |
| 435 | { |
| 436 | struct interface *ifp; |
| 437 | |
| 438 | ifp = if_lookup_by_name (argv[0]); |
| 439 | |
| 440 | if (ifp == NULL) |
| 441 | { |
| 442 | ifp = if_create (); |
| 443 | strncpy (ifp->name, argv[0], INTERFACE_NAMSIZ); |
| 444 | } |
| 445 | vty->index = ifp; |
| 446 | vty->node = INTERFACE_NODE; |
| 447 | |
| 448 | return CMD_SUCCESS; |
| 449 | } |
| 450 | |
| 451 | /* For debug purpose. */ |
| 452 | DEFUN (show_address, |
| 453 | show_address_cmd, |
| 454 | "show address", |
| 455 | SHOW_STR |
| 456 | "address\n") |
| 457 | { |
| 458 | listnode node; |
| 459 | listnode node2; |
| 460 | struct interface *ifp; |
| 461 | struct connected *ifc; |
| 462 | struct prefix *p; |
| 463 | |
| 464 | for (node = listhead (iflist); node; nextnode (node)) |
| 465 | { |
| 466 | ifp = getdata (node); |
| 467 | |
| 468 | for (node2 = listhead (ifp->connected); node2; nextnode (node2)) |
| 469 | { |
| 470 | ifc = getdata (node2); |
| 471 | p = ifc->address; |
| 472 | |
| 473 | if (p->family == AF_INET) |
| 474 | vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen, |
| 475 | VTY_NEWLINE); |
| 476 | } |
| 477 | } |
| 478 | return CMD_SUCCESS; |
| 479 | } |
| 480 | |
| 481 | /* Allocate connected structure. */ |
| 482 | struct connected * |
| 483 | connected_new () |
| 484 | { |
| 485 | struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected)); |
| 486 | memset (new, 0, sizeof (struct connected)); |
| 487 | return new; |
| 488 | } |
| 489 | |
| 490 | /* Free connected structure. */ |
| 491 | void |
| 492 | connected_free (struct connected *connected) |
| 493 | { |
| 494 | if (connected->address) |
| 495 | prefix_free (connected->address); |
| 496 | |
| 497 | if (connected->destination) |
| 498 | prefix_free (connected->destination); |
| 499 | |
| 500 | if (connected->label) |
| 501 | free (connected->label); |
| 502 | |
| 503 | XFREE (MTYPE_CONNECTED, connected); |
| 504 | } |
| 505 | |
| 506 | /* Print if_addr structure. */ |
| 507 | void |
| 508 | connected_log (struct connected *connected, char *str) |
| 509 | { |
| 510 | struct prefix *p; |
| 511 | struct interface *ifp; |
| 512 | char logbuf[BUFSIZ]; |
| 513 | char buf[BUFSIZ]; |
| 514 | |
| 515 | ifp = connected->ifp; |
| 516 | p = connected->address; |
| 517 | |
| 518 | snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ", |
| 519 | str, ifp->name, prefix_family_str (p), |
| 520 | inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), |
| 521 | p->prefixlen); |
| 522 | |
| 523 | p = connected->destination; |
| 524 | if (p) |
| 525 | { |
| 526 | strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), |
| 527 | BUFSIZ - strlen(logbuf)); |
| 528 | } |
| 529 | zlog (NULL, LOG_INFO, logbuf); |
| 530 | } |
| 531 | |
| 532 | /* If two connected address has same prefix return 1. */ |
| 533 | int |
| 534 | connected_same_prefix (struct prefix *p1, struct prefix *p2) |
| 535 | { |
| 536 | if (p1->family == p2->family) |
| 537 | { |
| 538 | if (p1->family == AF_INET && |
| 539 | IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4)) |
| 540 | return 1; |
| 541 | #ifdef HAVE_IPV6 |
| 542 | if (p1->family == AF_INET6 && |
| 543 | IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6)) |
| 544 | return 1; |
| 545 | #endif /* HAVE_IPV6 */ |
| 546 | } |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | struct connected * |
| 551 | connected_delete_by_prefix (struct interface *ifp, struct prefix *p) |
| 552 | { |
| 553 | struct listnode *node; |
| 554 | struct listnode *next; |
| 555 | struct connected *ifc; |
| 556 | |
| 557 | /* In case of same prefix come, replace it with new one. */ |
| 558 | for (node = listhead (ifp->connected); node; node = next) |
| 559 | { |
| 560 | ifc = getdata (node); |
| 561 | next = node->next; |
| 562 | |
| 563 | if (connected_same_prefix (ifc->address, p)) |
| 564 | { |
| 565 | listnode_delete (ifp->connected, ifc); |
| 566 | return ifc; |
| 567 | } |
| 568 | } |
| 569 | return NULL; |
| 570 | } |
| 571 | |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 572 | /* Find the IPv4 address on our side that will be used when packets |
| 573 | are sent to dst. */ |
| 574 | struct connected * |
| 575 | connected_lookup_address (struct interface *ifp, struct in_addr dst) |
| 576 | { |
| 577 | struct prefix addr; |
| 578 | struct prefix best; |
| 579 | listnode cnode; |
| 580 | struct prefix *p; |
| 581 | struct connected *c; |
| 582 | struct connected *match; |
| 583 | |
| 584 | /* Zero structures - get rid of rubbish from stack */ |
| 585 | memset(&addr, 0, sizeof(addr)); |
| 586 | memset(&best, 0, sizeof(best)); |
| 587 | |
| 588 | addr.family = AF_INET; |
| 589 | addr.u.prefix4 = dst; |
| 590 | addr.prefixlen = IPV4_MAX_BITLEN; |
| 591 | |
| 592 | match = NULL; |
| 593 | |
| 594 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) |
| 595 | { |
| 596 | c = getdata (cnode); |
| 597 | |
| 598 | if (if_is_pointopoint (ifp)) |
| 599 | { |
| 600 | p = c->address; |
| 601 | |
| 602 | if (p && p->family == AF_INET) |
| 603 | { |
| 604 | #ifdef OLD_RIB /* PTP links are conventionally identified |
| 605 | by the address of the far end - MAG */ |
| 606 | if (IPV4_ADDR_SAME (&p->u.prefix4, &dst)) |
| 607 | return c; |
| 608 | #endif |
| 609 | p = c->destination; |
| 610 | if (p && IPV4_ADDR_SAME (&p->u.prefix4, &dst)) |
| 611 | return c; |
| 612 | } |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | p = c->address; |
| 617 | |
| 618 | if (p->family == AF_INET) |
| 619 | { |
| 620 | if (prefix_match (p, &addr) && p->prefixlen > best.prefixlen) |
| 621 | { |
| 622 | best = *p; |
| 623 | match = c; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | return match; |
| 629 | } |
| 630 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 631 | /* Check the connected information is PtP style or not. */ |
| 632 | int |
| 633 | ifc_pointopoint (struct connected *ifc) |
| 634 | { |
| 635 | struct prefix *p; |
| 636 | int ptp = 0; |
| 637 | |
| 638 | /* When interface has PtP flag. */ |
| 639 | if (if_is_pointopoint (ifc->ifp)) |
| 640 | return 1; |
| 641 | |
| 642 | /* RFC3021 PtP check. */ |
| 643 | p = ifc->address; |
| 644 | |
| 645 | if (p->family == AF_INET) |
| 646 | ptp = (p->prefixlen >= IPV4_MAX_PREFIXLEN - 1); |
| 647 | #ifdef HAVE_IPV6 |
| 648 | if (p->family == AF_INET6) |
| 649 | ptp = (p->prefixlen >= IPV6_MAX_PREFIXLEN - 1); |
| 650 | #endif /* HAVE_IPV6 */ |
| 651 | |
| 652 | return ptp; |
| 653 | } |
| 654 | |
| 655 | #ifndef HAVE_IF_NAMETOINDEX |
| 656 | unsigned int |
| 657 | if_nametoindex (const char *name) |
| 658 | { |
| 659 | listnode node; |
| 660 | struct interface *ifp; |
| 661 | |
| 662 | for (node = listhead (iflist); node; nextnode (node)) |
| 663 | { |
| 664 | ifp = getdata (node); |
| 665 | if (strcmp (ifp->name, name) == 0) |
| 666 | return ifp->ifindex; |
| 667 | } |
| 668 | return 0; |
| 669 | } |
| 670 | #endif |
| 671 | |
| 672 | #ifndef HAVE_IF_INDEXTONAME |
| 673 | char * |
| 674 | if_indextoname (unsigned int ifindex, char *name) |
| 675 | { |
| 676 | listnode node; |
| 677 | struct interface *ifp; |
| 678 | |
| 679 | for (node = listhead (iflist); node; nextnode (node)) |
| 680 | { |
| 681 | ifp = getdata (node); |
| 682 | if (ifp->ifindex == ifindex) |
| 683 | { |
| 684 | memcpy (name, ifp->name, IFNAMSIZ); |
| 685 | return ifp->name; |
| 686 | } |
| 687 | } |
| 688 | return NULL; |
| 689 | } |
| 690 | #endif |
| 691 | |
| 692 | /* Interface looking up by interface's address. */ |
| 693 | |
| 694 | /* Interface's IPv4 address reverse lookup table. */ |
| 695 | struct route_table *ifaddr_ipv4_table; |
| 696 | /* struct route_table *ifaddr_ipv6_table; */ |
| 697 | |
| 698 | void |
| 699 | ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp) |
| 700 | { |
| 701 | struct route_node *rn; |
| 702 | struct prefix_ipv4 p; |
| 703 | |
| 704 | p.family = AF_INET; |
| 705 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 706 | p.prefix = *ifaddr; |
| 707 | |
| 708 | rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p); |
| 709 | if (rn) |
| 710 | { |
| 711 | route_unlock_node (rn); |
| 712 | zlog_info ("ifaddr_ipv4_add(): address %s is already added", |
| 713 | inet_ntoa (*ifaddr)); |
| 714 | return; |
| 715 | } |
| 716 | rn->info = ifp; |
| 717 | } |
| 718 | |
| 719 | void |
| 720 | ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp) |
| 721 | { |
| 722 | struct route_node *rn; |
| 723 | struct prefix_ipv4 p; |
| 724 | |
| 725 | p.family = AF_INET; |
| 726 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 727 | p.prefix = *ifaddr; |
| 728 | |
| 729 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); |
| 730 | if (! rn) |
| 731 | { |
| 732 | zlog_info ("ifaddr_ipv4_delete(): can't find address %s", |
| 733 | inet_ntoa (*ifaddr)); |
| 734 | return; |
| 735 | } |
| 736 | rn->info = NULL; |
| 737 | route_unlock_node (rn); |
| 738 | route_unlock_node (rn); |
| 739 | } |
| 740 | |
| 741 | /* Lookup interface by interface's IP address or interface index. */ |
| 742 | struct interface * |
| 743 | ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex) |
| 744 | { |
| 745 | struct prefix_ipv4 p; |
| 746 | struct route_node *rn; |
| 747 | struct interface *ifp; |
| 748 | listnode node; |
| 749 | |
| 750 | if (addr) |
| 751 | { |
| 752 | p.family = AF_INET; |
| 753 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 754 | p.prefix = *addr; |
| 755 | |
| 756 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); |
| 757 | if (! rn) |
| 758 | return NULL; |
| 759 | |
| 760 | ifp = rn->info; |
| 761 | route_unlock_node (rn); |
| 762 | return ifp; |
| 763 | } |
| 764 | else |
| 765 | { |
| 766 | for (node = listhead (iflist); node; nextnode (node)) |
| 767 | { |
| 768 | ifp = getdata (node); |
| 769 | |
| 770 | if (ifp->ifindex == ifindex) |
| 771 | return ifp; |
| 772 | } |
| 773 | } |
| 774 | return NULL; |
| 775 | } |
| 776 | |
| 777 | /* Initialize interface list. */ |
| 778 | void |
| 779 | if_init () |
| 780 | { |
| 781 | iflist = list_new (); |
| 782 | ifaddr_ipv4_table = route_table_init (); |
| 783 | |
| 784 | if (iflist) |
| 785 | return; |
| 786 | |
| 787 | memset (&if_master, 0, sizeof if_master); |
| 788 | } |