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