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 * |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 116 | if_create (const char *name, int namelen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 117 | { |
| 118 | struct interface *ifp; |
| 119 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 120 | ifp = XCALLOC (MTYPE_IF, sizeof (struct interface)); |
| 121 | ifp->ifindex = IFINDEX_INTERNAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 122 | |
paul | 106d2fd | 2003-08-01 00:24:13 +0000 | [diff] [blame] | 123 | assert (name); |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 124 | assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */ |
paul | 106d2fd | 2003-08-01 00:24:13 +0000 | [diff] [blame] | 125 | strncpy (ifp->name, name, namelen); |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 126 | ifp->name[namelen] = '\0'; |
hasso | e90fbab | 2003-12-21 09:51:42 +0000 | [diff] [blame] | 127 | if (if_lookup_by_name(ifp->name) == NULL) |
| 128 | listnode_add_sort (iflist, ifp); |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 129 | else |
| 130 | zlog_err("if_create(%s): corruption detected -- interface with this " |
| 131 | "name exists already!", ifp->name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 132 | ifp->connected = list_new (); |
| 133 | ifp->connected->del = (void (*) (void *)) connected_free; |
| 134 | |
| 135 | if (if_master.if_new_hook) |
| 136 | (*if_master.if_new_hook) (ifp); |
| 137 | |
| 138 | return ifp; |
| 139 | } |
| 140 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 141 | /* Delete interface structure. */ |
| 142 | void |
| 143 | if_delete_retain (struct interface *ifp) |
| 144 | { |
| 145 | if (if_master.if_delete_hook) |
| 146 | (*if_master.if_delete_hook) (ifp); |
| 147 | |
| 148 | /* Free connected address list */ |
| 149 | list_delete (ifp->connected); |
| 150 | } |
| 151 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | /* Delete and free interface structure. */ |
| 153 | void |
| 154 | if_delete (struct interface *ifp) |
| 155 | { |
| 156 | listnode_delete (iflist, ifp); |
| 157 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 158 | if_delete_retain(ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 159 | |
| 160 | XFREE (MTYPE_IF, ifp); |
| 161 | } |
| 162 | |
| 163 | /* Add hook to interface master. */ |
| 164 | void |
| 165 | if_add_hook (int type, int (*func)(struct interface *ifp)) |
| 166 | { |
| 167 | switch (type) { |
| 168 | case IF_NEW_HOOK: |
| 169 | if_master.if_new_hook = func; |
| 170 | break; |
| 171 | case IF_DELETE_HOOK: |
| 172 | if_master.if_delete_hook = func; |
| 173 | break; |
| 174 | default: |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* Interface existance check by index. */ |
| 180 | struct interface * |
| 181 | if_lookup_by_index (unsigned int index) |
| 182 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 183 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 184 | struct interface *ifp; |
| 185 | |
| 186 | for (node = listhead (iflist); node; nextnode (node)) |
| 187 | { |
| 188 | ifp = getdata (node); |
| 189 | if (ifp->ifindex == index) |
| 190 | return ifp; |
| 191 | } |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | char * |
| 196 | ifindex2ifname (unsigned int index) |
| 197 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 198 | struct interface *ifp; |
| 199 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 200 | return ((ifp = if_lookup_by_index(index)) != NULL) ? |
| 201 | ifp->name : (char *)"unknown"; |
| 202 | } |
| 203 | |
| 204 | unsigned int |
| 205 | ifname2ifindex (const char *name) |
| 206 | { |
| 207 | struct interface *ifp; |
| 208 | |
| 209 | return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp->ifindex : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* Interface existance check by interface name. */ |
| 213 | struct interface * |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 214 | if_lookup_by_name (const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 215 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 216 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 217 | struct interface *ifp; |
| 218 | |
| 219 | for (node = listhead (iflist); node; nextnode (node)) |
| 220 | { |
| 221 | ifp = getdata (node); |
| 222 | if (strncmp (name, ifp->name, sizeof ifp->name) == 0) |
| 223 | return ifp; |
| 224 | } |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | /* Lookup interface by IPv4 address. */ |
| 229 | struct interface * |
| 230 | if_lookup_exact_address (struct in_addr src) |
| 231 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 232 | struct listnode *node; |
| 233 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 234 | struct interface *ifp; |
| 235 | struct prefix *p; |
| 236 | struct connected *c; |
| 237 | |
| 238 | for (node = listhead (iflist); node; nextnode (node)) |
| 239 | { |
| 240 | ifp = getdata (node); |
| 241 | |
| 242 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) |
| 243 | { |
| 244 | c = getdata (cnode); |
| 245 | |
| 246 | p = c->address; |
| 247 | |
| 248 | if (p && p->family == AF_INET) |
| 249 | { |
| 250 | if (IPV4_ADDR_SAME (&p->u.prefix4, &src)) |
| 251 | return ifp; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | /* Lookup interface by IPv4 address. */ |
| 259 | struct interface * |
| 260 | if_lookup_address (struct in_addr src) |
| 261 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 262 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 263 | struct prefix addr; |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 264 | int bestlen = 0; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 265 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 266 | struct interface *ifp; |
| 267 | struct prefix *p; |
| 268 | struct connected *c; |
| 269 | struct interface *match; |
| 270 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 271 | addr.family = AF_INET; |
| 272 | addr.u.prefix4 = src; |
| 273 | addr.prefixlen = IPV4_MAX_BITLEN; |
| 274 | |
| 275 | match = NULL; |
| 276 | |
| 277 | for (node = listhead (iflist); node; nextnode (node)) |
| 278 | { |
| 279 | ifp = getdata (node); |
| 280 | |
| 281 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) |
| 282 | { |
| 283 | c = getdata (cnode); |
| 284 | |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 285 | if (c->address && (c->address->family == AF_INET)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 286 | { |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 287 | if (CONNECTED_POINTOPOINT_HOST(c)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 288 | { |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 289 | /* PTP links are conventionally identified |
| 290 | by the address of the far end - MAG */ |
| 291 | if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &src)) |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 292 | return ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 293 | } |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 294 | else |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 295 | { |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 296 | p = c->address; |
| 297 | |
| 298 | if (prefix_match (p, &addr) && p->prefixlen > bestlen) |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 299 | { |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 300 | bestlen = p->prefixlen; |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 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 * |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 313 | if_get_by_name (const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | { |
paul | 4ba9b92 | 2004-12-21 22:34:58 +0000 | [diff] [blame] | 350 | /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M |
| 351 | * but Y on platform N? |
| 352 | */ |
| 353 | return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* Does this interface support broadcast ? */ |
| 357 | int |
| 358 | if_is_broadcast (struct interface *ifp) |
| 359 | { |
| 360 | return ifp->flags & IFF_BROADCAST; |
| 361 | } |
| 362 | |
| 363 | /* Does this interface support broadcast ? */ |
| 364 | int |
| 365 | if_is_pointopoint (struct interface *ifp) |
| 366 | { |
| 367 | return ifp->flags & IFF_POINTOPOINT; |
| 368 | } |
| 369 | |
| 370 | /* Does this interface support multicast ? */ |
| 371 | int |
| 372 | if_is_multicast (struct interface *ifp) |
| 373 | { |
| 374 | return ifp->flags & IFF_MULTICAST; |
| 375 | } |
| 376 | |
| 377 | /* Printout flag information into log */ |
| 378 | const char * |
| 379 | if_flag_dump (unsigned long flag) |
| 380 | { |
| 381 | int separator = 0; |
| 382 | static char logbuf[BUFSIZ]; |
| 383 | |
| 384 | #define IFF_OUT_LOG(X,STR) \ |
paul | 4ba9b92 | 2004-12-21 22:34:58 +0000 | [diff] [blame] | 385 | if (flag & (X)) \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 386 | { \ |
| 387 | if (separator) \ |
| 388 | strlcat (logbuf, ",", BUFSIZ); \ |
| 389 | else \ |
| 390 | separator = 1; \ |
| 391 | strlcat (logbuf, STR, BUFSIZ); \ |
| 392 | } |
| 393 | |
| 394 | strlcpy (logbuf, " <", BUFSIZ); |
| 395 | IFF_OUT_LOG (IFF_UP, "UP"); |
| 396 | IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST"); |
| 397 | IFF_OUT_LOG (IFF_DEBUG, "DEBUG"); |
| 398 | IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK"); |
| 399 | IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT"); |
| 400 | IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS"); |
| 401 | IFF_OUT_LOG (IFF_RUNNING, "RUNNING"); |
| 402 | IFF_OUT_LOG (IFF_NOARP, "NOARP"); |
| 403 | IFF_OUT_LOG (IFF_PROMISC, "PROMISC"); |
| 404 | IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI"); |
| 405 | IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE"); |
| 406 | IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX"); |
| 407 | IFF_OUT_LOG (IFF_LINK0, "LINK0"); |
| 408 | IFF_OUT_LOG (IFF_LINK1, "LINK1"); |
| 409 | IFF_OUT_LOG (IFF_LINK2, "LINK2"); |
| 410 | IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST"); |
paul | 4ba9b92 | 2004-12-21 22:34:58 +0000 | [diff] [blame] | 411 | IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT"); |
| 412 | IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH"); |
| 413 | IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL"); |
| 414 | IFF_OUT_LOG (IFF_IPV4, "IPv4"); |
| 415 | IFF_OUT_LOG (IFF_IPV6, "IPv6"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 416 | |
| 417 | strlcat (logbuf, ">", BUFSIZ); |
| 418 | |
| 419 | return logbuf; |
| 420 | } |
| 421 | |
| 422 | /* For debugging */ |
| 423 | void |
| 424 | if_dump (struct interface *ifp) |
| 425 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 426 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 427 | |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 428 | zlog_info ("Interface %s index %d metric %d mtu %d " |
| 429 | #ifdef HAVE_IPV6 |
| 430 | "mtu6 %d " |
| 431 | #endif /* HAVE_IPV6 */ |
| 432 | "%s", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 433 | ifp->name, ifp->ifindex, ifp->metric, ifp->mtu, |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 434 | #ifdef HAVE_IPV6 |
| 435 | ifp->mtu6, |
| 436 | #endif /* HAVE_IPV6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 437 | if_flag_dump (ifp->flags)); |
| 438 | |
| 439 | for (node = listhead (ifp->connected); node; nextnode (node)) |
| 440 | ; |
| 441 | } |
| 442 | |
| 443 | /* Interface printing for all interface. */ |
| 444 | void |
| 445 | if_dump_all () |
| 446 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 447 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 448 | |
| 449 | for (node = listhead (iflist); node; nextnode (node)) |
| 450 | if_dump (getdata (node)); |
| 451 | } |
| 452 | |
| 453 | DEFUN (interface_desc, |
| 454 | interface_desc_cmd, |
| 455 | "description .LINE", |
| 456 | "Interface specific description\n" |
| 457 | "Characters describing this interface\n") |
| 458 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 459 | struct interface *ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 460 | |
| 461 | if (argc == 0) |
| 462 | return CMD_SUCCESS; |
| 463 | |
| 464 | ifp = vty->index; |
| 465 | if (ifp->desc) |
ajs | 3b8b185 | 2005-01-29 18:19:13 +0000 | [diff] [blame] | 466 | XFREE (MTYPE_TMP, ifp->desc); |
| 467 | ifp->desc = argv_concat(argv, argc, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 468 | |
| 469 | return CMD_SUCCESS; |
| 470 | } |
| 471 | |
| 472 | DEFUN (no_interface_desc, |
| 473 | no_interface_desc_cmd, |
| 474 | "no description", |
| 475 | NO_STR |
| 476 | "Interface specific description\n") |
| 477 | { |
| 478 | struct interface *ifp; |
| 479 | |
| 480 | ifp = vty->index; |
| 481 | if (ifp->desc) |
| 482 | XFREE (0, ifp->desc); |
| 483 | ifp->desc = NULL; |
| 484 | |
| 485 | return CMD_SUCCESS; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | /* See also wrapper function zebra_interface() in zebra/interface.c */ |
| 490 | DEFUN (interface, |
| 491 | interface_cmd, |
| 492 | "interface IFNAME", |
| 493 | "Select an interface to configure\n" |
| 494 | "Interface's name\n") |
| 495 | { |
| 496 | struct interface *ifp; |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 497 | size_t sl; |
| 498 | |
| 499 | if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ) |
| 500 | { |
| 501 | vty_out (vty, "%% Interface name %s is invalid: length exceeds " |
| 502 | "%d characters%s", |
| 503 | argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE); |
| 504 | return CMD_WARNING; |
| 505 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 506 | |
| 507 | ifp = if_lookup_by_name (argv[0]); |
| 508 | |
| 509 | if (ifp == NULL) |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 510 | ifp = if_create (argv[0], sl); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 511 | vty->index = ifp; |
| 512 | vty->node = INTERFACE_NODE; |
| 513 | |
| 514 | return CMD_SUCCESS; |
| 515 | } |
| 516 | |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 517 | DEFUN_NOSH (no_interface, |
| 518 | no_interface_cmd, |
| 519 | "no interface IFNAME", |
| 520 | NO_STR |
| 521 | "Delete a pseudo interface's configuration\n" |
| 522 | "Interface's name\n") |
| 523 | { |
| 524 | // deleting interface |
| 525 | struct interface *ifp; |
| 526 | |
| 527 | ifp = if_lookup_by_name (argv[0]); |
| 528 | |
| 529 | if (ifp == NULL) |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 530 | { |
| 531 | vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE); |
| 532 | return CMD_WARNING; |
| 533 | } |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 534 | |
paul | bfc1353 | 2003-05-24 06:40:04 +0000 | [diff] [blame] | 535 | if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 536 | { |
| 537 | vty_out (vty, "%% Only inactive interfaces can be deleted%s", |
| 538 | VTY_NEWLINE); |
| 539 | return CMD_WARNING; |
| 540 | } |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 541 | |
| 542 | if_delete(ifp); |
| 543 | |
| 544 | return CMD_SUCCESS; |
| 545 | } |
| 546 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 547 | /* For debug purpose. */ |
| 548 | DEFUN (show_address, |
| 549 | show_address_cmd, |
| 550 | "show address", |
| 551 | SHOW_STR |
| 552 | "address\n") |
| 553 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 554 | struct listnode *node; |
| 555 | struct listnode *node2; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 556 | struct interface *ifp; |
| 557 | struct connected *ifc; |
| 558 | struct prefix *p; |
| 559 | |
| 560 | for (node = listhead (iflist); node; nextnode (node)) |
| 561 | { |
| 562 | ifp = getdata (node); |
| 563 | |
| 564 | for (node2 = listhead (ifp->connected); node2; nextnode (node2)) |
| 565 | { |
| 566 | ifc = getdata (node2); |
| 567 | p = ifc->address; |
| 568 | |
| 569 | if (p->family == AF_INET) |
| 570 | vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen, |
| 571 | VTY_NEWLINE); |
| 572 | } |
| 573 | } |
| 574 | return CMD_SUCCESS; |
| 575 | } |
| 576 | |
| 577 | /* Allocate connected structure. */ |
| 578 | struct connected * |
| 579 | connected_new () |
| 580 | { |
| 581 | struct connected *new = XMALLOC (MTYPE_CONNECTED, sizeof (struct connected)); |
| 582 | memset (new, 0, sizeof (struct connected)); |
| 583 | return new; |
| 584 | } |
| 585 | |
| 586 | /* Free connected structure. */ |
| 587 | void |
| 588 | connected_free (struct connected *connected) |
| 589 | { |
| 590 | if (connected->address) |
| 591 | prefix_free (connected->address); |
| 592 | |
| 593 | if (connected->destination) |
| 594 | prefix_free (connected->destination); |
| 595 | |
| 596 | if (connected->label) |
| 597 | free (connected->label); |
| 598 | |
| 599 | XFREE (MTYPE_CONNECTED, connected); |
| 600 | } |
| 601 | |
| 602 | /* Print if_addr structure. */ |
| 603 | void |
| 604 | connected_log (struct connected *connected, char *str) |
| 605 | { |
| 606 | struct prefix *p; |
| 607 | struct interface *ifp; |
| 608 | char logbuf[BUFSIZ]; |
| 609 | char buf[BUFSIZ]; |
| 610 | |
| 611 | ifp = connected->ifp; |
| 612 | p = connected->address; |
| 613 | |
| 614 | snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ", |
| 615 | str, ifp->name, prefix_family_str (p), |
| 616 | inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), |
| 617 | p->prefixlen); |
| 618 | |
| 619 | p = connected->destination; |
| 620 | if (p) |
| 621 | { |
| 622 | strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), |
| 623 | BUFSIZ - strlen(logbuf)); |
| 624 | } |
| 625 | zlog (NULL, LOG_INFO, logbuf); |
| 626 | } |
| 627 | |
| 628 | /* If two connected address has same prefix return 1. */ |
| 629 | int |
| 630 | connected_same_prefix (struct prefix *p1, struct prefix *p2) |
| 631 | { |
| 632 | if (p1->family == p2->family) |
| 633 | { |
| 634 | if (p1->family == AF_INET && |
| 635 | IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4)) |
| 636 | return 1; |
| 637 | #ifdef HAVE_IPV6 |
| 638 | if (p1->family == AF_INET6 && |
| 639 | IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6)) |
| 640 | return 1; |
| 641 | #endif /* HAVE_IPV6 */ |
| 642 | } |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | struct connected * |
| 647 | connected_delete_by_prefix (struct interface *ifp, struct prefix *p) |
| 648 | { |
| 649 | struct listnode *node; |
| 650 | struct listnode *next; |
| 651 | struct connected *ifc; |
| 652 | |
| 653 | /* In case of same prefix come, replace it with new one. */ |
| 654 | for (node = listhead (ifp->connected); node; node = next) |
| 655 | { |
| 656 | ifc = getdata (node); |
| 657 | next = node->next; |
| 658 | |
| 659 | if (connected_same_prefix (ifc->address, p)) |
| 660 | { |
| 661 | listnode_delete (ifp->connected, ifc); |
| 662 | return ifc; |
| 663 | } |
| 664 | } |
| 665 | return NULL; |
| 666 | } |
| 667 | |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 668 | /* Find the IPv4 address on our side that will be used when packets |
| 669 | are sent to dst. */ |
| 670 | struct connected * |
| 671 | connected_lookup_address (struct interface *ifp, struct in_addr dst) |
| 672 | { |
| 673 | struct prefix addr; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 674 | struct listnode *cnode; |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 675 | struct prefix *p; |
| 676 | struct connected *c; |
| 677 | struct connected *match; |
| 678 | |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 679 | addr.family = AF_INET; |
| 680 | addr.u.prefix4 = dst; |
| 681 | addr.prefixlen = IPV4_MAX_BITLEN; |
| 682 | |
| 683 | match = NULL; |
| 684 | |
| 685 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) |
| 686 | { |
| 687 | c = getdata (cnode); |
| 688 | |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 689 | if (c->address && (c->address->family == AF_INET)) |
| 690 | { |
| 691 | if (CONNECTED_POINTOPOINT_HOST(c)) |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 692 | { |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 693 | /* PTP links are conventionally identified |
| 694 | by the address of the far end - MAG */ |
| 695 | if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &dst)) |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 696 | return c; |
| 697 | } |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 698 | else |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 699 | { |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 700 | p = c->address; |
| 701 | |
| 702 | if (prefix_match (p, &addr) && |
| 703 | (!match || (p->prefixlen > match->address->prefixlen))) |
| 704 | match = c; |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 705 | } |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 706 | } |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 707 | } |
| 708 | return match; |
| 709 | } |
| 710 | |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 711 | struct connected * |
| 712 | connected_add_by_prefix (struct interface *ifp, struct prefix *p, |
| 713 | struct prefix *destination) |
| 714 | { |
| 715 | struct connected *ifc; |
| 716 | |
| 717 | /* Allocate new connected address. */ |
| 718 | ifc = connected_new (); |
| 719 | ifc->ifp = ifp; |
| 720 | |
| 721 | /* Fetch interface address */ |
| 722 | ifc->address = prefix_new(); |
| 723 | memcpy (ifc->address, p, sizeof(struct prefix)); |
| 724 | |
| 725 | /* Fetch dest address */ |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 726 | if (destination) |
| 727 | { |
| 728 | ifc->destination = prefix_new(); |
| 729 | memcpy (ifc->destination, destination, sizeof(struct prefix)); |
| 730 | } |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 731 | |
| 732 | /* Add connected address to the interface. */ |
| 733 | listnode_add (ifp->connected, ifc); |
| 734 | return ifc; |
| 735 | } |
| 736 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 737 | #ifndef HAVE_IF_NAMETOINDEX |
| 738 | unsigned int |
| 739 | if_nametoindex (const char *name) |
| 740 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 741 | struct interface *ifp; |
| 742 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 743 | return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp->ifindex : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 744 | } |
| 745 | #endif |
| 746 | |
| 747 | #ifndef HAVE_IF_INDEXTONAME |
| 748 | char * |
| 749 | if_indextoname (unsigned int ifindex, char *name) |
| 750 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 751 | struct interface *ifp; |
| 752 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 753 | if (!(ifp = if_lookup_by_index(ifindex))) |
| 754 | return NULL; |
| 755 | strncpy (name, ifp->name, IFNAMSIZ); |
| 756 | return ifp->name; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 757 | } |
| 758 | #endif |
| 759 | |
| 760 | /* Interface looking up by interface's address. */ |
| 761 | |
| 762 | /* Interface's IPv4 address reverse lookup table. */ |
| 763 | struct route_table *ifaddr_ipv4_table; |
| 764 | /* struct route_table *ifaddr_ipv6_table; */ |
| 765 | |
| 766 | void |
| 767 | ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp) |
| 768 | { |
| 769 | struct route_node *rn; |
| 770 | struct prefix_ipv4 p; |
| 771 | |
| 772 | p.family = AF_INET; |
| 773 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 774 | p.prefix = *ifaddr; |
| 775 | |
| 776 | rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p); |
| 777 | if (rn) |
| 778 | { |
| 779 | route_unlock_node (rn); |
| 780 | zlog_info ("ifaddr_ipv4_add(): address %s is already added", |
| 781 | inet_ntoa (*ifaddr)); |
| 782 | return; |
| 783 | } |
| 784 | rn->info = ifp; |
| 785 | } |
| 786 | |
| 787 | void |
| 788 | ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp) |
| 789 | { |
| 790 | struct route_node *rn; |
| 791 | struct prefix_ipv4 p; |
| 792 | |
| 793 | p.family = AF_INET; |
| 794 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 795 | p.prefix = *ifaddr; |
| 796 | |
| 797 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); |
| 798 | if (! rn) |
| 799 | { |
| 800 | zlog_info ("ifaddr_ipv4_delete(): can't find address %s", |
| 801 | inet_ntoa (*ifaddr)); |
| 802 | return; |
| 803 | } |
| 804 | rn->info = NULL; |
| 805 | route_unlock_node (rn); |
| 806 | route_unlock_node (rn); |
| 807 | } |
| 808 | |
| 809 | /* Lookup interface by interface's IP address or interface index. */ |
| 810 | struct interface * |
| 811 | ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex) |
| 812 | { |
| 813 | struct prefix_ipv4 p; |
| 814 | struct route_node *rn; |
| 815 | struct interface *ifp; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 816 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 817 | |
| 818 | if (addr) |
| 819 | { |
| 820 | p.family = AF_INET; |
| 821 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 822 | p.prefix = *addr; |
| 823 | |
| 824 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); |
| 825 | if (! rn) |
| 826 | return NULL; |
| 827 | |
| 828 | ifp = rn->info; |
| 829 | route_unlock_node (rn); |
| 830 | return ifp; |
| 831 | } |
| 832 | else |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 833 | return if_lookup_by_index(ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /* Initialize interface list. */ |
| 837 | void |
| 838 | if_init () |
| 839 | { |
| 840 | iflist = list_new (); |
| 841 | ifaddr_ipv4_table = route_table_init (); |
| 842 | |
paul | 106d2fd | 2003-08-01 00:24:13 +0000 | [diff] [blame] | 843 | if (iflist) { |
| 844 | iflist->cmp = (int (*)(void *, void *))if_cmp_func; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 845 | return; |
paul | 106d2fd | 2003-08-01 00:24:13 +0000 | [diff] [blame] | 846 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 847 | |
| 848 | memset (&if_master, 0, sizeof if_master); |
| 849 | } |