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 | |
Olivier Dugeon | ae51c9d | 2016-04-19 16:21:46 +0200 | [diff] [blame^] | 171 | if_link_params_free (ifp); |
| 172 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 173 | XFREE (MTYPE_IF, ifp); |
| 174 | } |
| 175 | |
| 176 | /* Add hook to interface master. */ |
| 177 | void |
| 178 | if_add_hook (int type, int (*func)(struct interface *ifp)) |
| 179 | { |
| 180 | switch (type) { |
| 181 | case IF_NEW_HOOK: |
| 182 | if_master.if_new_hook = func; |
| 183 | break; |
| 184 | case IF_DELETE_HOOK: |
| 185 | if_master.if_delete_hook = func; |
| 186 | break; |
| 187 | default: |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /* Interface existance check by index. */ |
| 193 | struct interface * |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 194 | if_lookup_by_index_vrf (ifindex_t ifindex, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 195 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 196 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 197 | struct interface *ifp; |
| 198 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 199 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 200 | { |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 201 | if (ifp->ifindex == ifindex) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 202 | return ifp; |
| 203 | } |
| 204 | return NULL; |
| 205 | } |
| 206 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 207 | struct interface * |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 208 | if_lookup_by_index (ifindex_t ifindex) |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 209 | { |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 210 | return if_lookup_by_index_vrf (ifindex, VRF_DEFAULT); |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 211 | } |
| 212 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 213 | const char * |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 214 | ifindex2ifname_vrf (ifindex_t ifindex, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 215 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 216 | struct interface *ifp; |
| 217 | |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 218 | return ((ifp = if_lookup_by_index_vrf (ifindex, vrf_id)) != NULL) ? |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 219 | ifp->name : "unknown"; |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 222 | const char * |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 223 | ifindex2ifname (ifindex_t ifindex) |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 224 | { |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 225 | return ifindex2ifname_vrf (ifindex, VRF_DEFAULT); |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 228 | ifindex_t |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 229 | ifname2ifindex_vrf (const char *name, vrf_id_t vrf_id) |
| 230 | { |
| 231 | struct interface *ifp; |
| 232 | |
| 233 | return ((ifp = if_lookup_by_name_vrf (name, vrf_id)) != NULL) ? ifp->ifindex |
| 234 | : IFINDEX_INTERNAL; |
| 235 | } |
| 236 | |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 237 | ifindex_t |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 238 | ifname2ifindex (const char *name) |
| 239 | { |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 240 | return ifname2ifindex_vrf (name, VRF_DEFAULT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* Interface existance check by interface name. */ |
| 244 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 245 | if_lookup_by_name_vrf (const char *name, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 246 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 247 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 248 | struct interface *ifp; |
Paul Jakma | 3e4ee95 | 2009-08-06 12:08:50 +0100 | [diff] [blame] | 249 | |
| 250 | if (name) |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 251 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
Paul Jakma | 3e4ee95 | 2009-08-06 12:08:50 +0100 | [diff] [blame] | 252 | { |
| 253 | if (strcmp(name, ifp->name) == 0) |
| 254 | return ifp; |
| 255 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 256 | return NULL; |
| 257 | } |
| 258 | |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 259 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 260 | if_lookup_by_name (const char *name) |
| 261 | { |
| 262 | return if_lookup_by_name_vrf (name, VRF_DEFAULT); |
| 263 | } |
| 264 | |
| 265 | struct interface * |
| 266 | 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] | 267 | { |
| 268 | struct listnode *node; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 269 | struct interface *ifp; |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 270 | |
| 271 | if (namelen > INTERFACE_NAMSIZ) |
| 272 | return NULL; |
| 273 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 274 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 275 | { |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 276 | if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0')) |
| 277 | return ifp; |
| 278 | } |
| 279 | return NULL; |
| 280 | } |
| 281 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 282 | struct interface * |
| 283 | if_lookup_by_name_len(const char *name, size_t namelen) |
| 284 | { |
| 285 | return if_lookup_by_name_len_vrf (name, namelen, VRF_DEFAULT); |
| 286 | } |
| 287 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 288 | /* Lookup interface by IPv4 address. */ |
| 289 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 290 | 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] | 291 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 292 | struct listnode *node; |
| 293 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 294 | struct interface *ifp; |
| 295 | struct prefix *p; |
| 296 | struct connected *c; |
| 297 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 298 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 299 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 300 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 302 | p = c->address; |
| 303 | |
| 304 | if (p && p->family == AF_INET) |
| 305 | { |
| 306 | if (IPV4_ADDR_SAME (&p->u.prefix4, &src)) |
| 307 | return ifp; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | return NULL; |
| 312 | } |
| 313 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 314 | struct interface * |
| 315 | if_lookup_exact_address (struct in_addr src) |
| 316 | { |
| 317 | return if_lookup_exact_address_vrf (src, VRF_DEFAULT); |
| 318 | } |
| 319 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 320 | /* Lookup interface by IPv4 address. */ |
| 321 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 322 | if_lookup_address_vrf (struct in_addr src, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 323 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 324 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 325 | struct prefix addr; |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 326 | int bestlen = 0; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 327 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 328 | struct interface *ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 329 | struct connected *c; |
| 330 | struct interface *match; |
| 331 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 332 | addr.family = AF_INET; |
| 333 | addr.u.prefix4 = src; |
| 334 | addr.prefixlen = IPV4_MAX_BITLEN; |
| 335 | |
| 336 | match = NULL; |
| 337 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 338 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 339 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 340 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 341 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 342 | if (c->address && (c->address->family == AF_INET) && |
| 343 | prefix_match(CONNECTED_PREFIX(c), &addr) && |
| 344 | (c->address->prefixlen > bestlen)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 345 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 346 | bestlen = c->address->prefixlen; |
| 347 | match = ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | } |
| 351 | return match; |
| 352 | } |
| 353 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 354 | struct interface * |
| 355 | if_lookup_address (struct in_addr src) |
| 356 | { |
| 357 | return if_lookup_address_vrf (src, VRF_DEFAULT); |
| 358 | } |
| 359 | |
Dinesh Dutt | b81e97a | 2013-08-24 07:55:50 +0000 | [diff] [blame] | 360 | /* Lookup interface by prefix */ |
| 361 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 362 | if_lookup_prefix_vrf (struct prefix *prefix, vrf_id_t vrf_id) |
Dinesh Dutt | b81e97a | 2013-08-24 07:55:50 +0000 | [diff] [blame] | 363 | { |
| 364 | struct listnode *node; |
Dinesh Dutt | b81e97a | 2013-08-24 07:55:50 +0000 | [diff] [blame] | 365 | struct listnode *cnode; |
| 366 | struct interface *ifp; |
| 367 | struct connected *c; |
| 368 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 369 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
Dinesh Dutt | b81e97a | 2013-08-24 07:55:50 +0000 | [diff] [blame] | 370 | { |
| 371 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c)) |
| 372 | { |
| 373 | if (prefix_cmp(c->address, prefix) == 0) |
| 374 | { |
| 375 | return ifp; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | return NULL; |
| 380 | } |
| 381 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 382 | struct interface * |
| 383 | if_lookup_prefix (struct prefix *prefix) |
| 384 | { |
| 385 | return if_lookup_prefix_vrf (prefix, VRF_DEFAULT); |
| 386 | } |
| 387 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 388 | /* Get interface by name if given name interface doesn't exist create |
| 389 | one. */ |
| 390 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 391 | if_get_by_name_vrf (const char *name, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 392 | { |
| 393 | struct interface *ifp; |
| 394 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 395 | return ((ifp = if_lookup_by_name_vrf (name, vrf_id)) != NULL) ? ifp : |
| 396 | if_create_vrf (name, strlen(name), vrf_id); |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 400 | if_get_by_name (const char *name) |
| 401 | { |
| 402 | return if_get_by_name_vrf (name, VRF_DEFAULT); |
| 403 | } |
| 404 | |
| 405 | struct interface * |
| 406 | 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] | 407 | { |
| 408 | struct interface *ifp; |
| 409 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 410 | return ((ifp = if_lookup_by_name_len_vrf (name, namelen, vrf_id)) != NULL) ? \ |
| 411 | ifp : if_create_vrf (name, namelen, vrf_id); |
| 412 | } |
| 413 | |
| 414 | struct interface * |
| 415 | if_get_by_name_len (const char *name, size_t namelen) |
| 416 | { |
| 417 | return if_get_by_name_len_vrf (name, namelen, VRF_DEFAULT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* Does interface up ? */ |
| 421 | int |
| 422 | if_is_up (struct interface *ifp) |
| 423 | { |
| 424 | return ifp->flags & IFF_UP; |
| 425 | } |
| 426 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 427 | /* Is interface running? */ |
| 428 | int |
| 429 | if_is_running (struct interface *ifp) |
| 430 | { |
| 431 | return ifp->flags & IFF_RUNNING; |
| 432 | } |
| 433 | |
| 434 | /* Is the interface operative, eg. either UP & RUNNING |
| 435 | or UP & !ZEBRA_INTERFACE_LINK_DETECTION */ |
| 436 | int |
| 437 | if_is_operative (struct interface *ifp) |
| 438 | { |
| 439 | return ((ifp->flags & IFF_UP) && |
| 440 | (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))); |
| 441 | } |
| 442 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 443 | /* Is this loopback interface ? */ |
| 444 | int |
| 445 | if_is_loopback (struct interface *ifp) |
| 446 | { |
paul | 4ba9b92 | 2004-12-21 22:34:58 +0000 | [diff] [blame] | 447 | /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M |
| 448 | * but Y on platform N? |
| 449 | */ |
| 450 | return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /* Does this interface support broadcast ? */ |
| 454 | int |
| 455 | if_is_broadcast (struct interface *ifp) |
| 456 | { |
| 457 | return ifp->flags & IFF_BROADCAST; |
| 458 | } |
| 459 | |
| 460 | /* Does this interface support broadcast ? */ |
| 461 | int |
| 462 | if_is_pointopoint (struct interface *ifp) |
| 463 | { |
| 464 | return ifp->flags & IFF_POINTOPOINT; |
| 465 | } |
| 466 | |
| 467 | /* Does this interface support multicast ? */ |
| 468 | int |
| 469 | if_is_multicast (struct interface *ifp) |
| 470 | { |
| 471 | return ifp->flags & IFF_MULTICAST; |
| 472 | } |
| 473 | |
| 474 | /* Printout flag information into log */ |
| 475 | const char * |
| 476 | if_flag_dump (unsigned long flag) |
| 477 | { |
| 478 | int separator = 0; |
| 479 | static char logbuf[BUFSIZ]; |
| 480 | |
| 481 | #define IFF_OUT_LOG(X,STR) \ |
paul | 4ba9b92 | 2004-12-21 22:34:58 +0000 | [diff] [blame] | 482 | if (flag & (X)) \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 483 | { \ |
| 484 | if (separator) \ |
| 485 | strlcat (logbuf, ",", BUFSIZ); \ |
| 486 | else \ |
| 487 | separator = 1; \ |
| 488 | strlcat (logbuf, STR, BUFSIZ); \ |
| 489 | } |
| 490 | |
Paul Jakma | 630c97c | 2006-06-15 12:48:17 +0000 | [diff] [blame] | 491 | strlcpy (logbuf, "<", BUFSIZ); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 492 | IFF_OUT_LOG (IFF_UP, "UP"); |
| 493 | IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST"); |
| 494 | IFF_OUT_LOG (IFF_DEBUG, "DEBUG"); |
| 495 | IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK"); |
| 496 | IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT"); |
| 497 | IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS"); |
| 498 | IFF_OUT_LOG (IFF_RUNNING, "RUNNING"); |
| 499 | IFF_OUT_LOG (IFF_NOARP, "NOARP"); |
| 500 | IFF_OUT_LOG (IFF_PROMISC, "PROMISC"); |
| 501 | IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI"); |
| 502 | IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE"); |
| 503 | IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX"); |
| 504 | IFF_OUT_LOG (IFF_LINK0, "LINK0"); |
| 505 | IFF_OUT_LOG (IFF_LINK1, "LINK1"); |
| 506 | IFF_OUT_LOG (IFF_LINK2, "LINK2"); |
| 507 | IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST"); |
paul | 4ba9b92 | 2004-12-21 22:34:58 +0000 | [diff] [blame] | 508 | IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT"); |
| 509 | IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH"); |
| 510 | IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL"); |
| 511 | IFF_OUT_LOG (IFF_IPV4, "IPv4"); |
| 512 | IFF_OUT_LOG (IFF_IPV6, "IPv6"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 513 | |
| 514 | strlcat (logbuf, ">", BUFSIZ); |
| 515 | |
| 516 | return logbuf; |
Paul Jakma | 630c97c | 2006-06-15 12:48:17 +0000 | [diff] [blame] | 517 | #undef IFF_OUT_LOG |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /* For debugging */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 521 | static void |
Stephen Hemminger | cedd7f2 | 2009-06-12 16:58:49 +0100 | [diff] [blame] | 522 | if_dump (const struct interface *ifp) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 523 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 524 | struct listnode *node; |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 525 | struct connected *c __attribute__((unused)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 526 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 527 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, c)) |
Feng Lu | 2fc97f6 | 2015-05-22 11:39:57 +0200 | [diff] [blame] | 528 | zlog_info ("Interface %s vrf %u index %d metric %d mtu %d " |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 529 | #ifdef HAVE_IPV6 |
Paul Jakma | 23be94e | 2012-01-06 16:07:39 +0000 | [diff] [blame] | 530 | "mtu6 %d " |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 531 | #endif /* HAVE_IPV6 */ |
Paul Jakma | 23be94e | 2012-01-06 16:07:39 +0000 | [diff] [blame] | 532 | "%s", |
Feng Lu | 2fc97f6 | 2015-05-22 11:39:57 +0200 | [diff] [blame] | 533 | ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric, ifp->mtu, |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 534 | #ifdef HAVE_IPV6 |
Paul Jakma | 23be94e | 2012-01-06 16:07:39 +0000 | [diff] [blame] | 535 | ifp->mtu6, |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 536 | #endif /* HAVE_IPV6 */ |
Paul Jakma | 23be94e | 2012-01-06 16:07:39 +0000 | [diff] [blame] | 537 | if_flag_dump (ifp->flags)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /* Interface printing for all interface. */ |
| 541 | void |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 542 | if_dump_all (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 543 | { |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 544 | struct list *intf_list; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 545 | struct listnode *node; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 546 | void *p; |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 547 | vrf_iter_t iter; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 548 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 549 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
| 550 | if ((intf_list = vrf_iter2iflist (iter)) != NULL) |
| 551 | for (ALL_LIST_ELEMENTS_RO (intf_list, node, p)) |
| 552 | if_dump (p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | DEFUN (interface_desc, |
| 556 | interface_desc_cmd, |
| 557 | "description .LINE", |
| 558 | "Interface specific description\n" |
| 559 | "Characters describing this interface\n") |
| 560 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 561 | struct interface *ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 562 | |
| 563 | if (argc == 0) |
| 564 | return CMD_SUCCESS; |
| 565 | |
| 566 | ifp = vty->index; |
| 567 | if (ifp->desc) |
ajs | 3b8b185 | 2005-01-29 18:19:13 +0000 | [diff] [blame] | 568 | XFREE (MTYPE_TMP, ifp->desc); |
| 569 | ifp->desc = argv_concat(argv, argc, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 570 | |
| 571 | return CMD_SUCCESS; |
| 572 | } |
| 573 | |
| 574 | DEFUN (no_interface_desc, |
| 575 | no_interface_desc_cmd, |
| 576 | "no description", |
| 577 | NO_STR |
| 578 | "Interface specific description\n") |
| 579 | { |
| 580 | struct interface *ifp; |
| 581 | |
| 582 | ifp = vty->index; |
| 583 | if (ifp->desc) |
paul | 0241684 | 2005-10-26 05:05:16 +0000 | [diff] [blame] | 584 | XFREE (MTYPE_TMP, ifp->desc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 585 | ifp->desc = NULL; |
| 586 | |
| 587 | return CMD_SUCCESS; |
| 588 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 589 | |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 590 | #ifdef SUNOS_5 |
| 591 | /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created |
| 592 | * a seperate struct interface for each logical interface, so config |
| 593 | * file may be full of 'interface fooX:Y'. Solaris however does not |
| 594 | * expose logical interfaces via PF_ROUTE, so trying to track logical |
| 595 | * interfaces can be fruitless, for that reason Quagga only tracks |
| 596 | * the primary IP interface. |
| 597 | * |
| 598 | * We try accomodate SUNWzebra by: |
| 599 | * - looking up the interface name, to see whether it exists, if so |
| 600 | * its useable |
| 601 | * - for protocol daemons, this could only because zebra told us of |
| 602 | * the interface |
| 603 | * - for zebra, only because it learnt from kernel |
| 604 | * - if not: |
| 605 | * - search the name to see if it contains a sub-ipif / logical interface |
| 606 | * seperator, the ':' char. If it does: |
| 607 | * - text up to that char must be the primary name - get that name. |
| 608 | * if not: |
| 609 | * - no idea, just get the name in its entirety. |
| 610 | */ |
| 611 | static struct interface * |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 612 | 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] | 613 | { |
| 614 | struct interface *ifp; |
| 615 | size_t seppos = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 616 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 617 | 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] | 618 | return ifp; |
| 619 | |
| 620 | /* hunt the primary interface name... */ |
| 621 | while (seppos < nlen && name[seppos] != ':') |
| 622 | seppos++; |
| 623 | |
| 624 | /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */ |
| 625 | if (seppos < nlen) |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 626 | return if_get_by_name_len_vrf (name, seppos, vrf_id); |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 627 | else |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 628 | return if_get_by_name_len_vrf (name, nlen, vrf_id); |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 629 | } |
| 630 | #endif /* SUNOS_5 */ |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 631 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 632 | DEFUN (interface, |
| 633 | interface_cmd, |
| 634 | "interface IFNAME", |
| 635 | "Select an interface to configure\n" |
| 636 | "Interface's name\n") |
| 637 | { |
| 638 | struct interface *ifp; |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 639 | size_t sl; |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 640 | vrf_id_t vrf_id = VRF_DEFAULT; |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 641 | |
| 642 | if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ) |
| 643 | { |
| 644 | vty_out (vty, "%% Interface name %s is invalid: length exceeds " |
| 645 | "%d characters%s", |
| 646 | argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE); |
| 647 | return CMD_WARNING; |
| 648 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 649 | |
Feng Lu | 471ea39 | 2015-05-22 11:40:00 +0200 | [diff] [blame] | 650 | if (argc > 1) |
| 651 | VTY_GET_INTEGER ("VRF ID", vrf_id, argv[1]); |
| 652 | |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 653 | #ifdef SUNOS_5 |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 654 | ifp = if_sunwzebra_get (argv[0], sl, vrf_id); |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 655 | #else |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 656 | ifp = if_get_by_name_len_vrf (argv[0], sl, vrf_id); |
Paul Jakma | 9895484 | 2006-10-15 23:33:50 +0000 | [diff] [blame] | 657 | #endif /* SUNOS_5 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 658 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 659 | vty->index = ifp; |
| 660 | vty->node = INTERFACE_NODE; |
| 661 | |
| 662 | return CMD_SUCCESS; |
| 663 | } |
| 664 | |
Feng Lu | 471ea39 | 2015-05-22 11:40:00 +0200 | [diff] [blame] | 665 | ALIAS (interface, |
| 666 | interface_vrf_cmd, |
| 667 | "interface IFNAME " VRF_CMD_STR, |
| 668 | "Select an interface to configure\n" |
| 669 | "Interface's name\n" |
| 670 | VRF_CMD_HELP_STR) |
| 671 | |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 672 | DEFUN_NOSH (no_interface, |
| 673 | no_interface_cmd, |
| 674 | "no interface IFNAME", |
| 675 | NO_STR |
| 676 | "Delete a pseudo interface's configuration\n" |
| 677 | "Interface's name\n") |
| 678 | { |
| 679 | // deleting interface |
| 680 | struct interface *ifp; |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 681 | vrf_id_t vrf_id = VRF_DEFAULT; |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 682 | |
Feng Lu | 471ea39 | 2015-05-22 11:40:00 +0200 | [diff] [blame] | 683 | if (argc > 1) |
| 684 | VTY_GET_INTEGER ("VRF ID", vrf_id, argv[1]); |
| 685 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 686 | ifp = if_lookup_by_name_vrf (argv[0], vrf_id); |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 687 | |
| 688 | if (ifp == NULL) |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 689 | { |
| 690 | vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE); |
| 691 | return CMD_WARNING; |
| 692 | } |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 693 | |
paul | bfc1353 | 2003-05-24 06:40:04 +0000 | [diff] [blame] | 694 | if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 695 | { |
| 696 | vty_out (vty, "%% Only inactive interfaces can be deleted%s", |
| 697 | VTY_NEWLINE); |
| 698 | return CMD_WARNING; |
| 699 | } |
paul | 32d2463 | 2003-05-23 09:25:20 +0000 | [diff] [blame] | 700 | |
| 701 | if_delete(ifp); |
| 702 | |
| 703 | return CMD_SUCCESS; |
| 704 | } |
| 705 | |
Feng Lu | 471ea39 | 2015-05-22 11:40:00 +0200 | [diff] [blame] | 706 | ALIAS (no_interface, |
| 707 | no_interface_vrf_cmd, |
| 708 | "no interface IFNAME " VRF_CMD_STR, |
| 709 | NO_STR |
| 710 | "Delete a pseudo interface's configuration\n" |
| 711 | "Interface's name\n" |
| 712 | VRF_CMD_HELP_STR) |
| 713 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 714 | /* For debug purpose. */ |
| 715 | DEFUN (show_address, |
| 716 | show_address_cmd, |
| 717 | "show address", |
| 718 | SHOW_STR |
| 719 | "address\n") |
| 720 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 721 | struct listnode *node; |
| 722 | struct listnode *node2; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 723 | struct interface *ifp; |
| 724 | struct connected *ifc; |
| 725 | struct prefix *p; |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 726 | vrf_id_t vrf_id = VRF_DEFAULT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 727 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 728 | if (argc > 0) |
| 729 | VTY_GET_INTEGER ("VRF ID", vrf_id, argv[0]); |
| 730 | |
| 731 | for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 732 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 733 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 734 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 735 | p = ifc->address; |
| 736 | |
| 737 | if (p->family == AF_INET) |
| 738 | vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen, |
| 739 | VTY_NEWLINE); |
| 740 | } |
| 741 | } |
| 742 | return CMD_SUCCESS; |
| 743 | } |
| 744 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 745 | ALIAS (show_address, |
| 746 | show_address_vrf_cmd, |
| 747 | "show address " VRF_CMD_STR, |
| 748 | SHOW_STR |
| 749 | "address\n" |
| 750 | VRF_CMD_HELP_STR) |
| 751 | |
| 752 | DEFUN (show_address_vrf_all, |
| 753 | show_address_vrf_all_cmd, |
| 754 | "show address " VRF_ALL_CMD_STR, |
| 755 | SHOW_STR |
| 756 | "address\n" |
| 757 | VRF_ALL_CMD_HELP_STR) |
| 758 | { |
| 759 | struct list *intf_list; |
| 760 | struct listnode *node; |
| 761 | struct listnode *node2; |
| 762 | struct interface *ifp; |
| 763 | struct connected *ifc; |
| 764 | struct prefix *p; |
| 765 | vrf_iter_t iter; |
| 766 | |
| 767 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
| 768 | { |
| 769 | intf_list = vrf_iter2iflist (iter); |
| 770 | if (!intf_list || !listcount (intf_list)) |
| 771 | continue; |
| 772 | |
| 773 | vty_out (vty, "%sVRF %u%s%s", VTY_NEWLINE, vrf_iter2id (iter), |
| 774 | VTY_NEWLINE, VTY_NEWLINE); |
| 775 | |
| 776 | for (ALL_LIST_ELEMENTS_RO (intf_list, node, ifp)) |
| 777 | { |
| 778 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc)) |
| 779 | { |
| 780 | p = ifc->address; |
| 781 | |
| 782 | if (p->family == AF_INET) |
| 783 | vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen, |
| 784 | VTY_NEWLINE); |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | return CMD_SUCCESS; |
| 789 | } |
| 790 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 791 | /* Allocate connected structure. */ |
| 792 | struct connected * |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 793 | connected_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 794 | { |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 795 | return XCALLOC (MTYPE_CONNECTED, sizeof (struct connected)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /* Free connected structure. */ |
| 799 | void |
| 800 | connected_free (struct connected *connected) |
| 801 | { |
| 802 | if (connected->address) |
| 803 | prefix_free (connected->address); |
| 804 | |
| 805 | if (connected->destination) |
| 806 | prefix_free (connected->destination); |
| 807 | |
| 808 | if (connected->label) |
paul | 9c4f1c6 | 2005-11-03 11:04:07 +0000 | [diff] [blame] | 809 | XFREE (MTYPE_CONNECTED_LABEL, connected->label); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 810 | |
| 811 | XFREE (MTYPE_CONNECTED, connected); |
| 812 | } |
| 813 | |
| 814 | /* Print if_addr structure. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 815 | static void __attribute__ ((unused)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 816 | connected_log (struct connected *connected, char *str) |
| 817 | { |
| 818 | struct prefix *p; |
| 819 | struct interface *ifp; |
| 820 | char logbuf[BUFSIZ]; |
| 821 | char buf[BUFSIZ]; |
| 822 | |
| 823 | ifp = connected->ifp; |
| 824 | p = connected->address; |
| 825 | |
Feng Lu | 2fc97f6 | 2015-05-22 11:39:57 +0200 | [diff] [blame] | 826 | snprintf (logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ", |
| 827 | str, ifp->name, ifp->vrf_id, prefix_family_str (p), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 828 | inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), |
| 829 | p->prefixlen); |
| 830 | |
| 831 | p = connected->destination; |
| 832 | if (p) |
| 833 | { |
| 834 | strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), |
| 835 | BUFSIZ - strlen(logbuf)); |
| 836 | } |
Christian Hammers | fc95186 | 2011-03-23 13:07:55 +0300 | [diff] [blame] | 837 | zlog (NULL, LOG_INFO, "%s", logbuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /* If two connected address has same prefix return 1. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 841 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 842 | connected_same_prefix (struct prefix *p1, struct prefix *p2) |
| 843 | { |
| 844 | if (p1->family == p2->family) |
| 845 | { |
| 846 | if (p1->family == AF_INET && |
| 847 | IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4)) |
| 848 | return 1; |
| 849 | #ifdef HAVE_IPV6 |
| 850 | if (p1->family == AF_INET6 && |
| 851 | IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6)) |
| 852 | return 1; |
| 853 | #endif /* HAVE_IPV6 */ |
| 854 | } |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | struct connected * |
| 859 | connected_delete_by_prefix (struct interface *ifp, struct prefix *p) |
| 860 | { |
| 861 | struct listnode *node; |
| 862 | struct listnode *next; |
| 863 | struct connected *ifc; |
| 864 | |
| 865 | /* In case of same prefix come, replace it with new one. */ |
| 866 | for (node = listhead (ifp->connected); node; node = next) |
| 867 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 868 | ifc = listgetdata (node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 869 | next = node->next; |
| 870 | |
| 871 | if (connected_same_prefix (ifc->address, p)) |
| 872 | { |
| 873 | listnode_delete (ifp->connected, ifc); |
| 874 | return ifc; |
| 875 | } |
| 876 | } |
| 877 | return NULL; |
| 878 | } |
| 879 | |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 880 | /* Find the IPv4 address on our side that will be used when packets |
| 881 | are sent to dst. */ |
| 882 | struct connected * |
| 883 | connected_lookup_address (struct interface *ifp, struct in_addr dst) |
| 884 | { |
| 885 | struct prefix addr; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 886 | struct listnode *cnode; |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 887 | struct connected *c; |
| 888 | struct connected *match; |
| 889 | |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 890 | addr.family = AF_INET; |
| 891 | addr.u.prefix4 = dst; |
| 892 | addr.prefixlen = IPV4_MAX_BITLEN; |
| 893 | |
| 894 | match = NULL; |
| 895 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 896 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c)) |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 897 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 898 | if (c->address && (c->address->family == AF_INET) && |
| 899 | prefix_match(CONNECTED_PREFIX(c), &addr) && |
| 900 | (!match || (c->address->prefixlen > match->address->prefixlen))) |
| 901 | match = c; |
paul | 727d104 | 2002-12-13 20:50:29 +0000 | [diff] [blame] | 902 | } |
| 903 | return match; |
| 904 | } |
| 905 | |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 906 | struct connected * |
| 907 | connected_add_by_prefix (struct interface *ifp, struct prefix *p, |
| 908 | struct prefix *destination) |
| 909 | { |
| 910 | struct connected *ifc; |
| 911 | |
| 912 | /* Allocate new connected address. */ |
| 913 | ifc = connected_new (); |
| 914 | ifc->ifp = ifp; |
| 915 | |
| 916 | /* Fetch interface address */ |
| 917 | ifc->address = prefix_new(); |
| 918 | memcpy (ifc->address, p, sizeof(struct prefix)); |
| 919 | |
| 920 | /* Fetch dest address */ |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 921 | if (destination) |
| 922 | { |
| 923 | ifc->destination = prefix_new(); |
| 924 | memcpy (ifc->destination, destination, sizeof(struct prefix)); |
| 925 | } |
paul | 4a7aac1 | 2004-05-08 05:00:31 +0000 | [diff] [blame] | 926 | |
| 927 | /* Add connected address to the interface. */ |
| 928 | listnode_add (ifp->connected, ifc); |
| 929 | return ifc; |
| 930 | } |
| 931 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 932 | #ifndef HAVE_IF_NAMETOINDEX |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 933 | ifindex_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 934 | if_nametoindex (const char *name) |
| 935 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 936 | struct interface *ifp; |
| 937 | |
ajs | 018546e | 2005-04-02 23:05:56 +0000 | [diff] [blame] | 938 | return ((ifp = if_lookup_by_name_len(name, strnlen(name, IFNAMSIZ))) != NULL) |
| 939 | ? ifp->ifindex : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 940 | } |
| 941 | #endif |
| 942 | |
| 943 | #ifndef HAVE_IF_INDEXTONAME |
| 944 | char * |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 945 | if_indextoname (ifindex_t ifindex, char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 946 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 947 | struct interface *ifp; |
| 948 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 949 | if (!(ifp = if_lookup_by_index(ifindex))) |
| 950 | return NULL; |
| 951 | strncpy (name, ifp->name, IFNAMSIZ); |
| 952 | return ifp->name; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 953 | } |
| 954 | #endif |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 955 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 956 | #if 0 /* this route_table of struct connected's is unused |
| 957 | * however, it would be good to use a route_table rather than |
| 958 | * a list.. |
| 959 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 960 | /* Interface looking up by interface's address. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 961 | /* Interface's IPv4 address reverse lookup table. */ |
| 962 | struct route_table *ifaddr_ipv4_table; |
| 963 | /* struct route_table *ifaddr_ipv6_table; */ |
| 964 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 965 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 966 | ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp) |
| 967 | { |
| 968 | struct route_node *rn; |
| 969 | struct prefix_ipv4 p; |
| 970 | |
| 971 | p.family = AF_INET; |
| 972 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 973 | p.prefix = *ifaddr; |
| 974 | |
| 975 | rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p); |
| 976 | if (rn) |
| 977 | { |
| 978 | route_unlock_node (rn); |
| 979 | zlog_info ("ifaddr_ipv4_add(): address %s is already added", |
| 980 | inet_ntoa (*ifaddr)); |
| 981 | return; |
| 982 | } |
| 983 | rn->info = ifp; |
| 984 | } |
| 985 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 986 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 987 | ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp) |
| 988 | { |
| 989 | struct route_node *rn; |
| 990 | struct prefix_ipv4 p; |
| 991 | |
| 992 | p.family = AF_INET; |
| 993 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 994 | p.prefix = *ifaddr; |
| 995 | |
| 996 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); |
| 997 | if (! rn) |
| 998 | { |
| 999 | zlog_info ("ifaddr_ipv4_delete(): can't find address %s", |
| 1000 | inet_ntoa (*ifaddr)); |
| 1001 | return; |
| 1002 | } |
| 1003 | rn->info = NULL; |
| 1004 | route_unlock_node (rn); |
| 1005 | route_unlock_node (rn); |
| 1006 | } |
| 1007 | |
| 1008 | /* Lookup interface by interface's IP address or interface index. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 1009 | static struct interface * |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 1010 | ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1011 | { |
| 1012 | struct prefix_ipv4 p; |
| 1013 | struct route_node *rn; |
| 1014 | struct interface *ifp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1015 | |
| 1016 | if (addr) |
| 1017 | { |
| 1018 | p.family = AF_INET; |
| 1019 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 1020 | p.prefix = *addr; |
| 1021 | |
| 1022 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); |
| 1023 | if (! rn) |
| 1024 | return NULL; |
| 1025 | |
| 1026 | ifp = rn->info; |
| 1027 | route_unlock_node (rn); |
| 1028 | return ifp; |
| 1029 | } |
| 1030 | else |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 1031 | return if_lookup_by_index(ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1032 | } |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 1033 | #endif /* ifaddr_ipv4_table */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1034 | |
| 1035 | /* Initialize interface list. */ |
| 1036 | void |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 1037 | if_init (vrf_id_t vrf_id, struct list **intf_list) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1038 | { |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 1039 | *intf_list = list_new (); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 1040 | #if 0 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1041 | ifaddr_ipv4_table = route_table_init (); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 1042 | #endif /* ifaddr_ipv4_table */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1043 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 1044 | (*intf_list)->cmp = (int (*)(void *, void *))if_cmp_func; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1045 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 1046 | if (vrf_id == VRF_DEFAULT) |
| 1047 | iflist = *intf_list; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1048 | } |
Tom Goff | 4bd045d | 2010-11-10 13:00:54 -0800 | [diff] [blame] | 1049 | |
| 1050 | void |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 1051 | if_terminate (vrf_id_t vrf_id, struct list **intf_list) |
Tom Goff | 4bd045d | 2010-11-10 13:00:54 -0800 | [diff] [blame] | 1052 | { |
| 1053 | for (;;) |
| 1054 | { |
| 1055 | struct interface *ifp; |
| 1056 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 1057 | ifp = listnode_head (*intf_list); |
Tom Goff | 4bd045d | 2010-11-10 13:00:54 -0800 | [diff] [blame] | 1058 | if (ifp == NULL) |
| 1059 | break; |
| 1060 | |
| 1061 | if_delete (ifp); |
| 1062 | } |
| 1063 | |
Feng Lu | 5a5702f | 2015-05-22 11:39:59 +0200 | [diff] [blame] | 1064 | list_delete (*intf_list); |
| 1065 | *intf_list = NULL; |
| 1066 | |
| 1067 | if (vrf_id == VRF_DEFAULT) |
| 1068 | iflist = NULL; |
Tom Goff | 4bd045d | 2010-11-10 13:00:54 -0800 | [diff] [blame] | 1069 | } |
Timo Teräs | 954c7d6 | 2016-01-15 17:36:33 +0200 | [diff] [blame] | 1070 | |
| 1071 | const char * |
| 1072 | if_link_type_str (enum zebra_link_type llt) |
| 1073 | { |
| 1074 | switch (llt) |
| 1075 | { |
| 1076 | #define llts(T,S) case (T): return (S) |
| 1077 | llts(ZEBRA_LLT_UNKNOWN, "Unknown"); |
| 1078 | llts(ZEBRA_LLT_ETHER, "Ethernet"); |
| 1079 | llts(ZEBRA_LLT_EETHER, "Experimental Ethernet"); |
| 1080 | llts(ZEBRA_LLT_AX25, "AX.25 Level 2"); |
| 1081 | llts(ZEBRA_LLT_PRONET, "PROnet token ring"); |
| 1082 | llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB"); |
| 1083 | llts(ZEBRA_LLT_ARCNET, "ARCnet"); |
| 1084 | llts(ZEBRA_LLT_APPLETLK, "AppleTalk"); |
| 1085 | llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI"); |
| 1086 | llts(ZEBRA_LLT_ATM, "ATM"); |
| 1087 | llts(ZEBRA_LLT_METRICOM, "Metricom STRIP"); |
| 1088 | llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4"); |
| 1089 | llts(ZEBRA_LLT_EUI64, "EUI-64"); |
| 1090 | llts(ZEBRA_LLT_INFINIBAND, "InfiniBand"); |
| 1091 | llts(ZEBRA_LLT_SLIP, "SLIP"); |
| 1092 | llts(ZEBRA_LLT_CSLIP, "Compressed SLIP"); |
| 1093 | llts(ZEBRA_LLT_SLIP6, "SLIPv6"); |
| 1094 | llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6"); |
| 1095 | llts(ZEBRA_LLT_ROSE, "ROSE packet radio"); |
| 1096 | llts(ZEBRA_LLT_X25, "CCITT X.25"); |
| 1097 | llts(ZEBRA_LLT_PPP, "PPP"); |
| 1098 | llts(ZEBRA_LLT_CHDLC, "Cisco HDLC"); |
| 1099 | llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC"); |
| 1100 | llts(ZEBRA_LLT_LAPB, "LAPB"); |
| 1101 | llts(ZEBRA_LLT_IPIP, "IPIP Tunnel"); |
| 1102 | llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel"); |
| 1103 | llts(ZEBRA_LLT_FRAD, "FRAD"); |
| 1104 | llts(ZEBRA_LLT_SKIP, "SKIP vif"); |
| 1105 | llts(ZEBRA_LLT_LOOPBACK, "Loopback"); |
| 1106 | llts(ZEBRA_LLT_LOCALTLK, "Localtalk"); |
| 1107 | llts(ZEBRA_LLT_FDDI, "FDDI"); |
| 1108 | llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT"); |
| 1109 | llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel"); |
| 1110 | llts(ZEBRA_LLT_IPGRE, "GRE over IP"); |
| 1111 | llts(ZEBRA_LLT_PIMREG, "PIMSM registration"); |
| 1112 | llts(ZEBRA_LLT_HIPPI, "HiPPI"); |
| 1113 | llts(ZEBRA_LLT_IRDA, "IrDA"); |
| 1114 | llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP"); |
| 1115 | llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop"); |
| 1116 | llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop"); |
| 1117 | llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric"); |
| 1118 | llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring"); |
| 1119 | llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11"); |
| 1120 | llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap"); |
| 1121 | llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4"); |
| 1122 | llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy"); |
| 1123 | default: |
| 1124 | zlog_warn ("Unknown value %d", llt); |
| 1125 | return "Unknown type!"; |
| 1126 | #undef llts |
| 1127 | } |
| 1128 | return NULL; |
| 1129 | } |
Olivier Dugeon | ae51c9d | 2016-04-19 16:21:46 +0200 | [diff] [blame^] | 1130 | |
| 1131 | struct if_link_params * |
| 1132 | if_link_params_get (struct interface *ifp) |
| 1133 | { |
| 1134 | if (ifp->link_params != NULL) |
| 1135 | return ifp->link_params; |
| 1136 | |
| 1137 | struct if_link_params *iflp = XCALLOC(MTYPE_IF_LINK_PARAMS, |
| 1138 | sizeof (struct if_link_params)); |
| 1139 | if (iflp == NULL) return NULL; |
| 1140 | |
| 1141 | /* Set TE metric == standard metric */ |
| 1142 | iflp->te_metric = ifp->metric; |
| 1143 | |
| 1144 | /* Compute default bandwidth based on interface */ |
| 1145 | int bw = (float)((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH) |
| 1146 | * TE_KILO_BIT / TE_BYTE); |
| 1147 | |
| 1148 | /* Set Max, Reservable and Unreserved Bandwidth */ |
| 1149 | iflp->max_bw = bw; |
| 1150 | iflp->max_rsv_bw = bw; |
| 1151 | for (int i = 0; i < MAX_CLASS_TYPE; i++) |
| 1152 | iflp->unrsv_bw[i] = bw; |
| 1153 | |
| 1154 | /* Update Link parameters status */ |
| 1155 | iflp->lp_status = LP_TE | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW; |
| 1156 | |
| 1157 | /* Finally attach newly created Link Parameters */ |
| 1158 | ifp->link_params = iflp; |
| 1159 | |
| 1160 | return iflp; |
| 1161 | } |
| 1162 | |
| 1163 | void |
| 1164 | if_link_params_free (struct interface *ifp) |
| 1165 | { |
| 1166 | if (ifp->link_params == NULL) return; |
| 1167 | XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params); |
| 1168 | ifp->link_params = NULL; |
| 1169 | } |