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