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