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