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