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