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" |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 30 | #include "vrf.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | #include "if.h" |
| 32 | #include "sockunion.h" |
| 33 | #include "prefix.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 34 | #include "memory.h" |
| 35 | #include "table.h" |
| 36 | #include "buffer.h" |
| 37 | #include "str.h" |
| 38 | #include "log.h" |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 39 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 40 | /* List of interfaces in only the default VRF */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 41 | struct list *iflist; |
| 42 | |
| 43 | /* One for each program. This structure is needed to store hooks. */ |
| 44 | struct if_master |
| 45 | { |
| 46 | int (*if_new_hook) (struct interface *); |
| 47 | int (*if_delete_hook) (struct interface *); |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 48 | } if_master = {0,}; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 49 | |
paul | 3a0391a | 2004-07-17 11:51:29 +0000 | [diff] [blame] | 50 | /* Compare interface names, returning an integer greater than, equal to, or |
| 51 | * less than 0, (following the strcmp convention), according to the |
| 52 | * relationship between ifp1 and ifp2. Interface names consist of an |
| 53 | * alphabetic prefix and a numeric suffix. The primary sort key is |
| 54 | * lexicographic by name, and then numeric by number. No number sorts |
| 55 | * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty < |
| 56 | * devpty0, de0 < del0 |
| 57 | */ |
paul | 106d2fd | 2003-08-01 00:24:13 +0000 | [diff] [blame] | 58 | int |
| 59 | if_cmp_func (struct interface *ifp1, struct interface *ifp2) |
| 60 | { |
| 61 | unsigned int l1, l2; |
| 62 | long int x1, x2; |
| 63 | char *p1, *p2; |
| 64 | int res; |
| 65 | |
| 66 | p1 = ifp1->name; |
| 67 | p2 = ifp2->name; |
| 68 | |
paul | 9057852 | 2003-09-23 23:46:01 +0000 | [diff] [blame] | 69 | while (*p1 && *p2) { |
paul | 106d2fd | 2003-08-01 00:24:13 +0000 | [diff] [blame] | 70 | /* look up to any number */ |
| 71 | l1 = strcspn(p1, "0123456789"); |
| 72 | l2 = strcspn(p2, "0123456789"); |
| 73 | |
| 74 | /* name lengths are different -> compare names */ |
| 75 | if (l1 != l2) |
| 76 | return (strcmp(p1, p2)); |
| 77 | |
paul | 3a0391a | 2004-07-17 11:51:29 +0000 | [diff] [blame] | 78 | /* Note that this relies on all numbers being less than all letters, so |
| 79 | * that de0 < del0. |
| 80 | */ |
paul | 106d2fd | 2003-08-01 00:24:13 +0000 | [diff] [blame] | 81 | res = strncmp(p1, p2, l1); |
| 82 | |
| 83 | /* names are different -> compare them */ |
| 84 | if (res) |
| 85 | return res; |
| 86 | |
| 87 | /* with identical name part, go to numeric part */ |
paul | 106d2fd | 2003-08-01 00:24:13 +0000 | [diff] [blame] | 88 | p1 += l1; |
| 89 | p2 += l1; |
| 90 | |
paul | b06c14f | 2004-07-09 12:24:42 +0000 | [diff] [blame] | 91 | if (!*p1) |
| 92 | return -1; |
| 93 | if (!*p2) |
| 94 | return 1; |
| 95 | |
paul | 106d2fd | 2003-08-01 00:24:13 +0000 | [diff] [blame] | 96 | x1 = strtol(p1, &p1, 10); |
| 97 | x2 = strtol(p2, &p2, 10); |
| 98 | |
| 99 | /* let's compare numbers now */ |
| 100 | if (x1 < x2) |
| 101 | return -1; |
| 102 | if (x1 > x2) |
| 103 | return 1; |
| 104 | |
| 105 | /* numbers were equal, lets do it again.. |
| 106 | (it happens with name like "eth123.456:789") */ |
| 107 | } |
paul | 9057852 | 2003-09-23 23:46:01 +0000 | [diff] [blame] | 108 | if (*p1) |
| 109 | return 1; |
| 110 | if (*p2) |
| 111 | return -1; |
| 112 | return 0; |
paul | 106d2fd | 2003-08-01 00:24:13 +0000 | [diff] [blame] | 113 | } |
| 114 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 115 | /* Create new interface structure. */ |
| 116 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 117 | if_create_vrf (const char *name, int namelen, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 118 | { |
| 119 | struct interface *ifp; |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 120 | struct list *intf_list = vrf_iflist_get (vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 121 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 122 | ifp = XCALLOC (MTYPE_IF, sizeof (struct interface)); |
| 123 | ifp->ifindex = IFINDEX_INTERNAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | |
paul | 106d2fd | 2003-08-01 00:24:13 +0000 | [diff] [blame] | 125 | assert (name); |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 126 | assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */ |
paul | 106d2fd | 2003-08-01 00:24:13 +0000 | [diff] [blame] | 127 | strncpy (ifp->name, name, namelen); |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 128 | ifp->name[namelen] = '\0'; |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 129 | ifp->vrf_id = vrf_id; |
| 130 | if (if_lookup_by_name_vrf (ifp->name, vrf_id) == NULL) |
| 131 | listnode_add_sort (intf_list, ifp); |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 132 | else |
| 133 | zlog_err("if_create(%s): corruption detected -- interface with this " |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 134 | "name exists already in VRF %u!", ifp->name, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 135 | ifp->connected = list_new (); |
| 136 | ifp->connected->del = (void (*) (void *)) connected_free; |
| 137 | |
| 138 | if (if_master.if_new_hook) |
| 139 | (*if_master.if_new_hook) (ifp); |
| 140 | |
| 141 | return ifp; |
| 142 | } |
| 143 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 144 | struct interface * |
| 145 | if_create (const char *name, int namelen) |
| 146 | { |
| 147 | return if_create_vrf (name, namelen, VRF_DEFAULT); |
| 148 | } |
| 149 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 150 | /* Delete interface structure. */ |
| 151 | void |
| 152 | if_delete_retain (struct interface *ifp) |
| 153 | { |
| 154 | if (if_master.if_delete_hook) |
| 155 | (*if_master.if_delete_hook) (ifp); |
| 156 | |
| 157 | /* Free connected address list */ |
Josh Bailey | 2dd04c5 | 2012-03-21 10:37:03 -0700 | [diff] [blame] | 158 | list_delete_all_node (ifp->connected); |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 159 | } |
| 160 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 161 | /* Delete and free interface structure. */ |
| 162 | void |
| 163 | if_delete (struct interface *ifp) |
| 164 | { |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 165 | listnode_delete (vrf_iflist (ifp->vrf_id), ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 166 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 167 | if_delete_retain(ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 168 | |
Josh Bailey | 2dd04c5 | 2012-03-21 10:37:03 -0700 | [diff] [blame] | 169 | list_free (ifp->connected); |
| 170 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 171 | XFREE (MTYPE_IF, ifp); |
| 172 | } |
| 173 | |
| 174 | /* Add hook to interface master. */ |
| 175 | void |
| 176 | if_add_hook (int type, int (*func)(struct interface *ifp)) |
| 177 | { |
| 178 | switch (type) { |
| 179 | case IF_NEW_HOOK: |
| 180 | if_master.if_new_hook = func; |
| 181 | break; |
| 182 | case IF_DELETE_HOOK: |
| 183 | if_master.if_delete_hook = func; |
| 184 | break; |
| 185 | default: |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /* Interface existance check by index. */ |
| 191 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 192 | if_lookup_by_index_vrf (unsigned int index, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 194 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 195 | struct interface *ifp; |
| 196 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 197 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 198 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 199 | if (ifp->ifindex == index) |
| 200 | return ifp; |
| 201 | } |
| 202 | return NULL; |
| 203 | } |
| 204 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 205 | struct interface * |
| 206 | if_lookup_by_index (unsigned int index) |
| 207 | { |
| 208 | return if_lookup_by_index_vrf (index, VRF_DEFAULT); |
| 209 | } |
| 210 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 211 | const char * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 212 | ifindex2ifname_vrf (unsigned int index, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 214 | struct interface *ifp; |
| 215 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 216 | return ((ifp = if_lookup_by_index_vrf (index, vrf_id)) != NULL) ? |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 217 | ifp->name : "unknown"; |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 220 | const char * |
| 221 | ifindex2ifname (unsigned int index) |
| 222 | { |
| 223 | return ifindex2ifname_vrf (index, VRF_DEFAULT); |
| 224 | } |
| 225 | |
| 226 | unsigned int |
| 227 | ifname2ifindex_vrf (const char *name, vrf_id_t vrf_id) |
| 228 | { |
| 229 | struct interface *ifp; |
| 230 | |
| 231 | return ((ifp = if_lookup_by_name_vrf (name, vrf_id)) != NULL) ? ifp->ifindex |
| 232 | : IFINDEX_INTERNAL; |
| 233 | } |
| 234 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 235 | unsigned int |
| 236 | ifname2ifindex (const char *name) |
| 237 | { |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 238 | return ifname2ifindex_vrf (name, VRF_DEFAULT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* Interface existance check by interface name. */ |
| 242 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 243 | if_lookup_by_name_vrf (const char *name, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 244 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 245 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 246 | struct interface *ifp; |
Paul Jakma | 3e4ee95 | 2009-08-06 12:08:50 +0100 | [diff] [blame] | 247 | |
| 248 | if (name) |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 249 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
Paul Jakma | 3e4ee95 | 2009-08-06 12:08:50 +0100 | [diff] [blame] | 250 | { |
| 251 | if (strcmp(name, ifp->name) == 0) |
| 252 | return ifp; |
| 253 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 254 | return NULL; |
| 255 | } |
| 256 | |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 257 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 258 | if_lookup_by_name (const char *name) |
| 259 | { |
| 260 | return if_lookup_by_name_vrf (name, VRF_DEFAULT); |
| 261 | } |
| 262 | |
| 263 | struct interface * |
| 264 | if_lookup_by_name_len_vrf (const char *name, size_t namelen, vrf_id_t vrf_id) |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 265 | { |
| 266 | struct listnode *node; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 267 | struct interface *ifp; |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 268 | |
| 269 | if (namelen > INTERFACE_NAMSIZ) |
| 270 | return NULL; |
| 271 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 272 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 273 | { |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 274 | if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0')) |
| 275 | return ifp; |
| 276 | } |
| 277 | return NULL; |
| 278 | } |
| 279 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 280 | struct interface * |
| 281 | if_lookup_by_name_len(const char *name, size_t namelen) |
| 282 | { |
| 283 | return if_lookup_by_name_len_vrf (name, namelen, VRF_DEFAULT); |
| 284 | } |
| 285 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 286 | /* Lookup interface by IPv4 address. */ |
| 287 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 288 | if_lookup_exact_address_vrf (struct in_addr src, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 289 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 290 | struct listnode *node; |
| 291 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 292 | struct interface *ifp; |
| 293 | struct prefix *p; |
| 294 | struct connected *c; |
| 295 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 296 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 297 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 298 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 299 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 300 | p = c->address; |
| 301 | |
| 302 | if (p && p->family == AF_INET) |
| 303 | { |
| 304 | if (IPV4_ADDR_SAME (&p->u.prefix4, &src)) |
| 305 | return ifp; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | return NULL; |
| 310 | } |
| 311 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 312 | struct interface * |
| 313 | if_lookup_exact_address (struct in_addr src) |
| 314 | { |
| 315 | return if_lookup_exact_address_vrf (src, VRF_DEFAULT); |
| 316 | } |
| 317 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 318 | /* Lookup interface by IPv4 address. */ |
| 319 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 320 | if_lookup_address_vrf (struct in_addr src, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 321 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 322 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 323 | struct prefix addr; |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 324 | int bestlen = 0; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 325 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | struct interface *ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 327 | struct connected *c; |
| 328 | struct interface *match; |
| 329 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 330 | addr.family = AF_INET; |
| 331 | addr.u.prefix4 = src; |
| 332 | addr.prefixlen = IPV4_MAX_BITLEN; |
| 333 | |
| 334 | match = NULL; |
| 335 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 336 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 337 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 338 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 339 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 340 | if (c->address && (c->address->family == AF_INET) && |
| 341 | prefix_match(CONNECTED_PREFIX(c), &addr) && |
| 342 | (c->address->prefixlen > bestlen)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 343 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 344 | bestlen = c->address->prefixlen; |
| 345 | match = ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | } |
| 349 | return match; |
| 350 | } |
| 351 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 352 | struct interface * |
| 353 | if_lookup_address (struct in_addr src) |
| 354 | { |
| 355 | return if_lookup_address_vrf (src, VRF_DEFAULT); |
| 356 | } |
| 357 | |
Dinesh Dutt | b81e97a | 2013-08-24 07:55:50 +0000 | [diff] [blame] | 358 | /* Lookup interface by prefix */ |
| 359 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 360 | if_lookup_prefix_vrf (struct prefix *prefix, vrf_id_t vrf_id) |
Dinesh Dutt | b81e97a | 2013-08-24 07:55:50 +0000 | [diff] [blame] | 361 | { |
| 362 | struct listnode *node; |
Dinesh Dutt | b81e97a | 2013-08-24 07:55:50 +0000 | [diff] [blame] | 363 | struct listnode *cnode; |
| 364 | struct interface *ifp; |
| 365 | struct connected *c; |
| 366 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 367 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
Dinesh Dutt | b81e97a | 2013-08-24 07:55:50 +0000 | [diff] [blame] | 368 | { |
| 369 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c)) |
| 370 | { |
| 371 | if (prefix_cmp(c->address, prefix) == 0) |
| 372 | { |
| 373 | return ifp; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | return NULL; |
| 378 | } |
| 379 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 380 | struct interface * |
| 381 | if_lookup_prefix (struct prefix *prefix) |
| 382 | { |
| 383 | return if_lookup_prefix_vrf (prefix, VRF_DEFAULT); |
| 384 | } |
| 385 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 386 | /* Get interface by name if given name interface doesn't exist create |
| 387 | one. */ |
| 388 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 389 | if_get_by_name_vrf (const char *name, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 390 | { |
| 391 | struct interface *ifp; |
| 392 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 393 | return ((ifp = if_lookup_by_name_vrf (name, vrf_id)) != NULL) ? ifp : |
| 394 | if_create_vrf (name, strlen(name), vrf_id); |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 398 | if_get_by_name (const char *name) |
| 399 | { |
| 400 | return if_get_by_name_vrf (name, VRF_DEFAULT); |
| 401 | } |
| 402 | |
| 403 | struct interface * |
| 404 | if_get_by_name_len_vrf (const char *name, size_t namelen, vrf_id_t vrf_id) |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 405 | { |
| 406 | struct interface *ifp; |
| 407 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 408 | return ((ifp = if_lookup_by_name_len_vrf (name, namelen, vrf_id)) != NULL) ? \ |
| 409 | ifp : if_create_vrf (name, namelen, vrf_id); |
| 410 | } |
| 411 | |
| 412 | struct interface * |
| 413 | if_get_by_name_len (const char *name, size_t namelen) |
| 414 | { |
| 415 | return if_get_by_name_len_vrf (name, namelen, VRF_DEFAULT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* Does interface up ? */ |
| 419 | int |
| 420 | if_is_up (struct interface *ifp) |
| 421 | { |
| 422 | return ifp->flags & IFF_UP; |
| 423 | } |
| 424 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 425 | /* Is interface running? */ |
| 426 | int |
| 427 | if_is_running (struct interface *ifp) |
| 428 | { |
| 429 | return ifp->flags & IFF_RUNNING; |
| 430 | } |
| 431 | |
| 432 | /* Is the interface operative, eg. either UP & RUNNING |
| 433 | or UP & !ZEBRA_INTERFACE_LINK_DETECTION */ |
| 434 | int |
| 435 | if_is_operative (struct interface *ifp) |
| 436 | { |
| 437 | return ((ifp->flags & IFF_UP) && |
| 438 | (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))); |
| 439 | } |
| 440 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 441 | /* Is this loopback interface ? */ |
| 442 | int |
| 443 | if_is_loopback (struct interface *ifp) |
| 444 | { |
paul | 4ba9b92 | 2004-12-21 22:34:58 +0000 | [diff] [blame] | 445 | /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M |
| 446 | * but Y on platform N? |
| 447 | */ |
| 448 | return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* Does this interface support broadcast ? */ |
| 452 | int |
| 453 | if_is_broadcast (struct interface *ifp) |
| 454 | { |
| 455 | return ifp->flags & IFF_BROADCAST; |
| 456 | } |
| 457 | |
| 458 | /* Does this interface support broadcast ? */ |
| 459 | int |
| 460 | if_is_pointopoint (struct interface *ifp) |
| 461 | { |
| 462 | return ifp->flags & IFF_POINTOPOINT; |
| 463 | } |
| 464 | |
| 465 | /* Does this interface support multicast ? */ |
| 466 | int |
| 467 | if_is_multicast (struct interface *ifp) |
| 468 | { |
| 469 | return ifp->flags & IFF_MULTICAST; |
| 470 | } |
| 471 | |
| 472 | /* Printout flag information into log */ |
| 473 | const char * |
| 474 | if_flag_dump (unsigned long flag) |
| 475 | { |
| 476 | int separator = 0; |
| 477 | static char logbuf[BUFSIZ]; |
| 478 | |
| 479 | #define IFF_OUT_LOG(X,STR) \ |
paul | 4ba9b92 | 2004-12-21 22:34:58 +0000 | [diff] [blame] | 480 | if (flag & (X)) \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 481 | { \ |
| 482 | if (separator) \ |
| 483 | strlcat (logbuf, ",", BUFSIZ); \ |
| 484 | else \ |
| 485 | separator = 1; \ |
| 486 | strlcat (logbuf, STR, BUFSIZ); \ |
| 487 | } |
| 488 | |
Paul Jakma | 630c97c | 2006-06-15 12:48:17 +0000 | [diff] [blame] | 489 | strlcpy (logbuf, "<", BUFSIZ); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 490 | IFF_OUT_LOG (IFF_UP, "UP"); |
| 491 | IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST"); |
| 492 | IFF_OUT_LOG (IFF_DEBUG, "DEBUG"); |
| 493 | IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK"); |
| 494 | IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT"); |
| 495 | IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS"); |
| 496 | IFF_OUT_LOG (IFF_RUNNING, "RUNNING"); |
| 497 | IFF_OUT_LOG (IFF_NOARP, "NOARP"); |
| 498 | IFF_OUT_LOG (IFF_PROMISC, "PROMISC"); |
| 499 | IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI"); |
| 500 | IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE"); |
| 501 | IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX"); |
| 502 | IFF_OUT_LOG (IFF_LINK0, "LINK0"); |
| 503 | IFF_OUT_LOG (IFF_LINK1, "LINK1"); |
| 504 | IFF_OUT_LOG (IFF_LINK2, "LINK2"); |
| 505 | IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST"); |
paul | 4ba9b92 | 2004-12-21 22:34:58 +0000 | [diff] [blame] | 506 | IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT"); |
| 507 | IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH"); |
| 508 | IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL"); |
| 509 | IFF_OUT_LOG (IFF_IPV4, "IPv4"); |
| 510 | IFF_OUT_LOG (IFF_IPV6, "IPv6"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 511 | |
| 512 | strlcat (logbuf, ">", BUFSIZ); |
| 513 | |
| 514 | return logbuf; |
Paul Jakma | 630c97c | 2006-06-15 12:48:17 +0000 | [diff] [blame] | 515 | #undef IFF_OUT_LOG |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* For debugging */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 519 | static void |
Stephen Hemminger | cedd7f2 | 2009-06-12 16:58:49 +0100 | [diff] [blame] | 520 | if_dump (const struct interface *ifp) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 521 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 522 | struct listnode *node; |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 523 | struct connected *c __attribute__((unused)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 524 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 525 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, c)) |
Feng Lu | 2fc97f6 | 2015-05-22 11:39:57 +0200 | [diff] [blame] | 526 | zlog_info ("Interface %s vrf %u index %d metric %d mtu %d " |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 527 | #ifdef HAVE_IPV6 |
Paul Jakma | 23be94e | 2012-01-06 16:07:39 +0000 | [diff] [blame] | 528 | "mtu6 %d " |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 529 | #endif /* HAVE_IPV6 */ |
Paul Jakma | 23be94e | 2012-01-06 16:07:39 +0000 | [diff] [blame] | 530 | "%s", |
Feng Lu | 2fc97f6 | 2015-05-22 11:39:57 +0200 | [diff] [blame] | 531 | ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric, ifp->mtu, |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 532 | #ifdef HAVE_IPV6 |
Paul Jakma | 23be94e | 2012-01-06 16:07:39 +0000 | [diff] [blame] | 533 | ifp->mtu6, |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 534 | #endif /* HAVE_IPV6 */ |
Paul Jakma | 23be94e | 2012-01-06 16:07:39 +0000 | [diff] [blame] | 535 | if_flag_dump (ifp->flags)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* Interface printing for all interface. */ |
| 539 | void |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 540 | if_dump_all (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 541 | { |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 542 | struct list *intf_list; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 543 | struct listnode *node; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 544 | void *p; |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 545 | vrf_iter_t iter; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 546 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 547 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
| 548 | if ((intf_list = vrf_iter2iflist (iter)) != NULL) |
| 549 | for (ALL_LIST_ELEMENTS_RO (intf_list, node, p)) |
| 550 | if_dump (p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | DEFUN (interface_desc, |
| 554 | interface_desc_cmd, |
| 555 | "description .LINE", |
| 556 | "Interface specific description\n" |
| 557 | "Characters describing this interface\n") |
| 558 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 559 | struct interface *ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 560 | |
| 561 | if (argc == 0) |
| 562 | return CMD_SUCCESS; |
| 563 | |
| 564 | ifp = vty->index; |
| 565 | if (ifp->desc) |
ajs | 3b8b185 | 2005-01-29 18:19:13 +0000 | [diff] [blame] | 566 | XFREE (MTYPE_TMP, ifp->desc); |
| 567 | ifp->desc = argv_concat(argv, argc, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 568 | |
| 569 | return CMD_SUCCESS; |
| 570 | } |
| 571 | |
| 572 | DEFUN (no_interface_desc, |
| 573 | no_interface_desc_cmd, |
| 574 | "no description", |
| 575 | NO_STR |
| 576 | "Interface specific description\n") |
| 577 | { |
| 578 | struct interface *ifp; |
| 579 | |
| 580 | ifp = vty->index; |
| 581 | if (ifp->desc) |
paul | 0241684 | 2005-10-26 05:05:16 +0000 | [diff] [blame] | 582 | XFREE (MTYPE_TMP, ifp->desc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 583 | ifp->desc = NULL; |
| 584 | |
| 585 | return CMD_SUCCESS; |
| 586 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 587 | |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 588 | #ifdef SUNOS_5 |
| 589 | /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created |
| 590 | * a seperate struct interface for each logical interface, so config |
| 591 | * file may be full of 'interface fooX:Y'. Solaris however does not |
| 592 | * expose logical interfaces via PF_ROUTE, so trying to track logical |
| 593 | * interfaces can be fruitless, for that reason Quagga only tracks |
| 594 | * the primary IP interface. |
| 595 | * |
| 596 | * We try accomodate SUNWzebra by: |
| 597 | * - looking up the interface name, to see whether it exists, if so |
| 598 | * its useable |
| 599 | * - for protocol daemons, this could only because zebra told us of |
| 600 | * the interface |
| 601 | * - for zebra, only because it learnt from kernel |
| 602 | * - if not: |
| 603 | * - search the name to see if it contains a sub-ipif / logical interface |
| 604 | * seperator, the ':' char. If it does: |
| 605 | * - text up to that char must be the primary name - get that name. |
| 606 | * if not: |
| 607 | * - no idea, just get the name in its entirety. |
| 608 | */ |
| 609 | static struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 610 | if_sunwzebra_get (const char *name, size_t nlen, vrf_id_t vrf_id) |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 611 | { |
| 612 | struct interface *ifp; |
| 613 | size_t seppos = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 614 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 615 | if ( (ifp = if_lookup_by_name_len_vrf (name, nlen, vrf_id)) != NULL) |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 616 | return ifp; |
| 617 | |
| 618 | /* hunt the primary interface name... */ |
| 619 | while (seppos < nlen && name[seppos] != ':') |
| 620 | seppos++; |
| 621 | |
| 622 | /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */ |
| 623 | if (seppos < nlen) |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 624 | return if_get_by_name_len_vrf (name, seppos, vrf_id); |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 625 | else |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 626 | return if_get_by_name_len_vrf (name, nlen, vrf_id); |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 627 | } |
| 628 | #endif /* SUNOS_5 */ |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 629 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 630 | DEFUN (interface, |
| 631 | interface_cmd, |
| 632 | "interface IFNAME", |
| 633 | "Select an interface to configure\n" |
| 634 | "Interface's name\n") |
| 635 | { |
| 636 | struct interface *ifp; |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 637 | size_t sl; |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 638 | vrf_id_t vrf_id = VRF_DEFAULT; |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 639 | |
| 640 | if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ) |
| 641 | { |
| 642 | vty_out (vty, "%% Interface name %s is invalid: length exceeds " |
| 643 | "%d characters%s", |
| 644 | argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE); |
| 645 | return CMD_WARNING; |
| 646 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 647 | |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 648 | #ifdef SUNOS_5 |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 649 | ifp = if_sunwzebra_get (argv[0], sl, vrf_id); |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 650 | #else |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 651 | ifp = if_get_by_name_len_vrf (argv[0], sl, vrf_id); |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 652 | #endif /* SUNOS_5 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 653 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 654 | vty->index = ifp; |
| 655 | vty->node = INTERFACE_NODE; |
| 656 | |
| 657 | return CMD_SUCCESS; |
| 658 | } |
| 659 | |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 660 | DEFUN_NOSH (no_interface, |
| 661 | no_interface_cmd, |
| 662 | "no interface IFNAME", |
| 663 | NO_STR |
| 664 | "Delete a pseudo interface's configuration\n" |
| 665 | "Interface's name\n") |
| 666 | { |
| 667 | // deleting interface |
| 668 | struct interface *ifp; |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 669 | vrf_id_t vrf_id = VRF_DEFAULT; |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 670 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 671 | ifp = if_lookup_by_name_vrf (argv[0], vrf_id); |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 672 | |
| 673 | if (ifp == NULL) |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 674 | { |
| 675 | vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE); |
| 676 | return CMD_WARNING; |
| 677 | } |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 678 | |
paul | bfc1353 | 2003-05-24 06:40:04 +0000 | [diff] [blame] | 679 | if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 680 | { |
| 681 | vty_out (vty, "%% Only inactive interfaces can be deleted%s", |
| 682 | VTY_NEWLINE); |
| 683 | return CMD_WARNING; |
| 684 | } |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 685 | |
| 686 | if_delete(ifp); |
| 687 | |
| 688 | return CMD_SUCCESS; |
| 689 | } |
| 690 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 691 | /* For debug purpose. */ |
| 692 | DEFUN (show_address, |
| 693 | show_address_cmd, |
| 694 | "show address", |
| 695 | SHOW_STR |
| 696 | "address\n") |
| 697 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 698 | struct listnode *node; |
| 699 | struct listnode *node2; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 700 | struct interface *ifp; |
| 701 | struct connected *ifc; |
| 702 | struct prefix *p; |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 703 | vrf_id_t vrf_id = VRF_DEFAULT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 704 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 705 | if (argc > 0) |
| 706 | VTY_GET_INTEGER ("VRF ID", vrf_id, argv[0]); |
| 707 | |
| 708 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 709 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 710 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 711 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 712 | p = ifc->address; |
| 713 | |
| 714 | if (p->family == AF_INET) |
| 715 | vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen, |
| 716 | VTY_NEWLINE); |
| 717 | } |
| 718 | } |
| 719 | return CMD_SUCCESS; |
| 720 | } |
| 721 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 722 | ALIAS (show_address, |
| 723 | show_address_vrf_cmd, |
| 724 | "show address " VRF_CMD_STR, |
| 725 | SHOW_STR |
| 726 | "address\n" |
| 727 | VRF_CMD_HELP_STR) |
| 728 | |
| 729 | DEFUN (show_address_vrf_all, |
| 730 | show_address_vrf_all_cmd, |
| 731 | "show address " VRF_ALL_CMD_STR, |
| 732 | SHOW_STR |
| 733 | "address\n" |
| 734 | VRF_ALL_CMD_HELP_STR) |
| 735 | { |
| 736 | struct list *intf_list; |
| 737 | struct listnode *node; |
| 738 | struct listnode *node2; |
| 739 | struct interface *ifp; |
| 740 | struct connected *ifc; |
| 741 | struct prefix *p; |
| 742 | vrf_iter_t iter; |
| 743 | |
| 744 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
| 745 | { |
| 746 | intf_list = vrf_iter2iflist (iter); |
| 747 | if (!intf_list || !listcount (intf_list)) |
| 748 | continue; |
| 749 | |
| 750 | vty_out (vty, "%sVRF %u%s%s", VTY_NEWLINE, vrf_iter2id (iter), |
| 751 | VTY_NEWLINE, VTY_NEWLINE); |
| 752 | |
| 753 | for (ALL_LIST_ELEMENTS_RO (intf_list, node, ifp)) |
| 754 | { |
| 755 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc)) |
| 756 | { |
| 757 | p = ifc->address; |
| 758 | |
| 759 | if (p->family == AF_INET) |
| 760 | vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen, |
| 761 | VTY_NEWLINE); |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | return CMD_SUCCESS; |
| 766 | } |
| 767 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 768 | /* Allocate connected structure. */ |
| 769 | struct connected * |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 770 | connected_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 771 | { |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 772 | return XCALLOC (MTYPE_CONNECTED, sizeof (struct connected)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* Free connected structure. */ |
| 776 | void |
| 777 | connected_free (struct connected *connected) |
| 778 | { |
| 779 | if (connected->address) |
| 780 | prefix_free (connected->address); |
| 781 | |
| 782 | if (connected->destination) |
| 783 | prefix_free (connected->destination); |
| 784 | |
| 785 | if (connected->label) |
paul | 9c4f1c6 | 2005-11-03 11:04:07 +0000 | [diff] [blame] | 786 | XFREE (MTYPE_CONNECTED_LABEL, connected->label); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 787 | |
| 788 | XFREE (MTYPE_CONNECTED, connected); |
| 789 | } |
| 790 | |
| 791 | /* Print if_addr structure. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 792 | static void __attribute__ ((unused)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 793 | connected_log (struct connected *connected, char *str) |
| 794 | { |
| 795 | struct prefix *p; |
| 796 | struct interface *ifp; |
| 797 | char logbuf[BUFSIZ]; |
| 798 | char buf[BUFSIZ]; |
| 799 | |
| 800 | ifp = connected->ifp; |
| 801 | p = connected->address; |
| 802 | |
Feng Lu | 2fc97f6 | 2015-05-22 11:39:57 +0200 | [diff] [blame] | 803 | snprintf (logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ", |
| 804 | str, ifp->name, ifp->vrf_id, prefix_family_str (p), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 805 | inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), |
| 806 | p->prefixlen); |
| 807 | |
| 808 | p = connected->destination; |
| 809 | if (p) |
| 810 | { |
| 811 | strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), |
| 812 | BUFSIZ - strlen(logbuf)); |
| 813 | } |
Christian Hammers | fc95186 | 2011-03-23 13:07:55 +0300 | [diff] [blame] | 814 | zlog (NULL, LOG_INFO, "%s", logbuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /* If two connected address has same prefix return 1. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 818 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 819 | connected_same_prefix (struct prefix *p1, struct prefix *p2) |
| 820 | { |
| 821 | if (p1->family == p2->family) |
| 822 | { |
| 823 | if (p1->family == AF_INET && |
| 824 | IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4)) |
| 825 | return 1; |
| 826 | #ifdef HAVE_IPV6 |
| 827 | if (p1->family == AF_INET6 && |
| 828 | IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6)) |
| 829 | return 1; |
| 830 | #endif /* HAVE_IPV6 */ |
| 831 | } |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | struct connected * |
| 836 | connected_delete_by_prefix (struct interface *ifp, struct prefix *p) |
| 837 | { |
| 838 | struct listnode *node; |
| 839 | struct listnode *next; |
| 840 | struct connected *ifc; |
| 841 | |
| 842 | /* In case of same prefix come, replace it with new one. */ |
| 843 | for (node = listhead (ifp->connected); node; node = next) |
| 844 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 845 | ifc = listgetdata (node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 846 | next = node->next; |
| 847 | |
| 848 | if (connected_same_prefix (ifc->address, p)) |
| 849 | { |
| 850 | listnode_delete (ifp->connected, ifc); |
| 851 | return ifc; |
| 852 | } |
| 853 | } |
| 854 | return NULL; |
| 855 | } |
| 856 | |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 857 | /* Find the IPv4 address on our side that will be used when packets |
| 858 | are sent to dst. */ |
| 859 | struct connected * |
| 860 | connected_lookup_address (struct interface *ifp, struct in_addr dst) |
| 861 | { |
| 862 | struct prefix addr; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 863 | struct listnode *cnode; |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 864 | struct connected *c; |
| 865 | struct connected *match; |
| 866 | |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 867 | addr.family = AF_INET; |
| 868 | addr.u.prefix4 = dst; |
| 869 | addr.prefixlen = IPV4_MAX_BITLEN; |
| 870 | |
| 871 | match = NULL; |
| 872 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 873 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c)) |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 874 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 875 | if (c->address && (c->address->family == AF_INET) && |
| 876 | prefix_match(CONNECTED_PREFIX(c), &addr) && |
| 877 | (!match || (c->address->prefixlen > match->address->prefixlen))) |
| 878 | match = c; |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 879 | } |
| 880 | return match; |
| 881 | } |
| 882 | |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 883 | struct connected * |
| 884 | connected_add_by_prefix (struct interface *ifp, struct prefix *p, |
| 885 | struct prefix *destination) |
| 886 | { |
| 887 | struct connected *ifc; |
| 888 | |
| 889 | /* Allocate new connected address. */ |
| 890 | ifc = connected_new (); |
| 891 | ifc->ifp = ifp; |
| 892 | |
| 893 | /* Fetch interface address */ |
| 894 | ifc->address = prefix_new(); |
| 895 | memcpy (ifc->address, p, sizeof(struct prefix)); |
| 896 | |
| 897 | /* Fetch dest address */ |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 898 | if (destination) |
| 899 | { |
| 900 | ifc->destination = prefix_new(); |
| 901 | memcpy (ifc->destination, destination, sizeof(struct prefix)); |
| 902 | } |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 903 | |
| 904 | /* Add connected address to the interface. */ |
| 905 | listnode_add (ifp->connected, ifc); |
| 906 | return ifc; |
| 907 | } |
| 908 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 909 | #ifndef HAVE_IF_NAMETOINDEX |
| 910 | unsigned int |
| 911 | if_nametoindex (const char *name) |
| 912 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 913 | struct interface *ifp; |
| 914 | |
ajs | 018546e | 2005-04-02 23:05:56 +0000 | [diff] [blame] | 915 | return ((ifp = if_lookup_by_name_len(name, strnlen(name, IFNAMSIZ))) != NULL) |
| 916 | ? ifp->ifindex : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 917 | } |
| 918 | #endif |
| 919 | |
| 920 | #ifndef HAVE_IF_INDEXTONAME |
| 921 | char * |
| 922 | if_indextoname (unsigned int ifindex, char *name) |
| 923 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 924 | struct interface *ifp; |
| 925 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 926 | if (!(ifp = if_lookup_by_index(ifindex))) |
| 927 | return NULL; |
| 928 | strncpy (name, ifp->name, IFNAMSIZ); |
| 929 | return ifp->name; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 930 | } |
| 931 | #endif |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 932 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 933 | #if 0 /* this route_table of struct connected's is unused |
| 934 | * however, it would be good to use a route_table rather than |
| 935 | * a list.. |
| 936 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 937 | /* Interface looking up by interface's address. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 938 | /* Interface's IPv4 address reverse lookup table. */ |
| 939 | struct route_table *ifaddr_ipv4_table; |
| 940 | /* struct route_table *ifaddr_ipv6_table; */ |
| 941 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 942 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 943 | ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp) |
| 944 | { |
| 945 | struct route_node *rn; |
| 946 | struct prefix_ipv4 p; |
| 947 | |
| 948 | p.family = AF_INET; |
| 949 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 950 | p.prefix = *ifaddr; |
| 951 | |
| 952 | rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p); |
| 953 | if (rn) |
| 954 | { |
| 955 | route_unlock_node (rn); |
| 956 | zlog_info ("ifaddr_ipv4_add(): address %s is already added", |
| 957 | inet_ntoa (*ifaddr)); |
| 958 | return; |
| 959 | } |
| 960 | rn->info = ifp; |
| 961 | } |
| 962 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 963 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 964 | ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp) |
| 965 | { |
| 966 | struct route_node *rn; |
| 967 | struct prefix_ipv4 p; |
| 968 | |
| 969 | p.family = AF_INET; |
| 970 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 971 | p.prefix = *ifaddr; |
| 972 | |
| 973 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); |
| 974 | if (! rn) |
| 975 | { |
| 976 | zlog_info ("ifaddr_ipv4_delete(): can't find address %s", |
| 977 | inet_ntoa (*ifaddr)); |
| 978 | return; |
| 979 | } |
| 980 | rn->info = NULL; |
| 981 | route_unlock_node (rn); |
| 982 | route_unlock_node (rn); |
| 983 | } |
| 984 | |
| 985 | /* Lookup interface by interface's IP address or interface index. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 986 | static struct interface * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 987 | ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex) |
| 988 | { |
| 989 | struct prefix_ipv4 p; |
| 990 | struct route_node *rn; |
| 991 | struct interface *ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 992 | |
| 993 | if (addr) |
| 994 | { |
| 995 | p.family = AF_INET; |
| 996 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 997 | p.prefix = *addr; |
| 998 | |
| 999 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); |
| 1000 | if (! rn) |
| 1001 | return NULL; |
| 1002 | |
| 1003 | ifp = rn->info; |
| 1004 | route_unlock_node (rn); |
| 1005 | return ifp; |
| 1006 | } |
| 1007 | else |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 1008 | return if_lookup_by_index(ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1009 | } |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 1010 | #endif /* ifaddr_ipv4_table */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1011 | |
| 1012 | /* Initialize interface list. */ |
| 1013 | void |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 1014 | if_init (vrf_id_t vrf_id, struct list **intf_list) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1015 | { |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 1016 | *intf_list = list_new (); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 1017 | #if 0 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1018 | ifaddr_ipv4_table = route_table_init (); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 1019 | #endif /* ifaddr_ipv4_table */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1020 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 1021 | (*intf_list)->cmp = (int (*)(void *, void *))if_cmp_func; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1022 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 1023 | if (vrf_id == VRF_DEFAULT) |
| 1024 | iflist = *intf_list; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1025 | } |
Tom Goff | 4bd045d | 2010-11-10 13:00:54 -0800 | [diff] [blame] | 1026 | |
| 1027 | void |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 1028 | if_terminate (vrf_id_t vrf_id, struct list **intf_list) |
Tom Goff | 4bd045d | 2010-11-10 13:00:54 -0800 | [diff] [blame] | 1029 | { |
| 1030 | for (;;) |
| 1031 | { |
| 1032 | struct interface *ifp; |
| 1033 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 1034 | ifp = listnode_head (*intf_list); |
Tom Goff | 4bd045d | 2010-11-10 13:00:54 -0800 | [diff] [blame] | 1035 | if (ifp == NULL) |
| 1036 | break; |
| 1037 | |
| 1038 | if_delete (ifp); |
| 1039 | } |
| 1040 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame^] | 1041 | list_delete (*intf_list); |
| 1042 | *intf_list = NULL; |
| 1043 | |
| 1044 | if (vrf_id == VRF_DEFAULT) |
| 1045 | iflist = NULL; |
Tom Goff | 4bd045d | 2010-11-10 13:00:54 -0800 | [diff] [blame] | 1046 | } |