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