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