]>
Commit | Line | Data |
---|---|---|
d62a17ae | 1 | /* |
718e3744 | 2 | * Interface functions. |
3 | * Copyright (C) 1997, 98 Kunihiro Ishiguro | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
896014f4 | 6 | * |
718e3744 | 7 | * GNU Zebra is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation; either version 2, or (at your | |
10 | * option) any later version. | |
11 | * | |
12 | * GNU Zebra is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "linklist.h" | |
25 | #include "vector.h" | |
4d43f68a | 26 | #include "lib_errors.h" |
718e3744 | 27 | #include "vty.h" |
28 | #include "command.h" | |
8736158a | 29 | #include "vrf.h" |
718e3744 | 30 | #include "if.h" |
31 | #include "sockunion.h" | |
32 | #include "prefix.h" | |
718e3744 | 33 | #include "memory.h" |
34 | #include "table.h" | |
35 | #include "buffer.h" | |
718e3744 | 36 | #include "log.h" |
8f90d89b RW |
37 | #include "northbound_cli.h" |
38 | #ifndef VTYSH_EXTRACT_PL | |
39 | #include "lib/if_clippy.c" | |
40 | #endif | |
6b0655a2 | 41 | |
d62a17ae | 42 | DEFINE_MTYPE(LIB, IF, "Interface") |
43 | DEFINE_MTYPE_STATIC(LIB, CONNECTED, "Connected") | |
44 | DEFINE_MTYPE_STATIC(LIB, NBR_CONNECTED, "Neighbor Connected") | |
45 | DEFINE_MTYPE(LIB, CONNECTED_LABEL, "Connected interface label") | |
46 | DEFINE_MTYPE_STATIC(LIB, IF_LINK_PARAMS, "Informational Link Parameters") | |
4a1ab8e4 | 47 | |
f4e14fdb | 48 | static int if_cmp_func(const struct interface *, const struct interface *); |
ff880b78 RW |
49 | static int if_cmp_index_func(const struct interface *ifp1, |
50 | const struct interface *ifp2); | |
f4e14fdb | 51 | RB_GENERATE(if_name_head, interface, name_entry, if_cmp_func); |
ff880b78 | 52 | RB_GENERATE(if_index_head, interface, index_entry, if_cmp_index_func); |
f4e14fdb | 53 | |
e80e7cce DL |
54 | DEFINE_QOBJ_TYPE(interface) |
55 | ||
996c9314 LB |
56 | DEFINE_HOOK(if_add, (struct interface * ifp), (ifp)) |
57 | DEFINE_KOOH(if_del, (struct interface * ifp), (ifp)) | |
ce19a04a | 58 | |
8736158a | 59 | /* List of interfaces in only the default VRF */ |
244c1cdc | 60 | int ptm_enable = 0; |
718e3744 | 61 | |
3a0391a9 | 62 | /* Compare interface names, returning an integer greater than, equal to, or |
63 | * less than 0, (following the strcmp convention), according to the | |
64 | * relationship between ifp1 and ifp2. Interface names consist of an | |
65 | * alphabetic prefix and a numeric suffix. The primary sort key is | |
66 | * lexicographic by name, and then numeric by number. No number sorts | |
67 | * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty < | |
68 | * devpty0, de0 < del0 | |
d62a17ae | 69 | */ |
36de6e0e | 70 | int if_cmp_name_func(const char *p1, const char *p2) |
106d2fd5 | 71 | { |
d62a17ae | 72 | unsigned int l1, l2; |
73 | long int x1, x2; | |
74 | int res; | |
75 | ||
76 | while (*p1 && *p2) { | |
77 | /* look up to any number */ | |
78 | l1 = strcspn(p1, "0123456789"); | |
79 | l2 = strcspn(p2, "0123456789"); | |
80 | ||
81 | /* name lengths are different -> compare names */ | |
82 | if (l1 != l2) | |
83 | return (strcmp(p1, p2)); | |
84 | ||
85 | /* Note that this relies on all numbers being less than all | |
86 | * letters, so | |
87 | * that de0 < del0. | |
88 | */ | |
89 | res = strncmp(p1, p2, l1); | |
90 | ||
91 | /* names are different -> compare them */ | |
92 | if (res) | |
93 | return res; | |
94 | ||
95 | /* with identical name part, go to numeric part */ | |
96 | p1 += l1; | |
97 | p2 += l1; | |
98 | ||
c9cbbb40 RW |
99 | if (!*p1 && !*p2) |
100 | return 0; | |
d62a17ae | 101 | if (!*p1) |
102 | return -1; | |
103 | if (!*p2) | |
104 | return 1; | |
105 | ||
36de6e0e A |
106 | x1 = strtol(p1, (char **)&p1, 10); |
107 | x2 = strtol(p2, (char **)&p2, 10); | |
d62a17ae | 108 | |
109 | /* let's compare numbers now */ | |
110 | if (x1 < x2) | |
111 | return -1; | |
112 | if (x1 > x2) | |
113 | return 1; | |
114 | ||
115 | /* numbers were equal, lets do it again.. | |
116 | (it happens with name like "eth123.456:789") */ | |
117 | } | |
118 | if (*p1) | |
119 | return 1; | |
120 | if (*p2) | |
121 | return -1; | |
122 | return 0; | |
106d2fd5 | 123 | } |
124 | ||
f4e14fdb RW |
125 | static int if_cmp_func(const struct interface *ifp1, |
126 | const struct interface *ifp2) | |
974fc9d2 | 127 | { |
36de6e0e | 128 | return if_cmp_name_func(ifp1->name, ifp2->name); |
974fc9d2 DS |
129 | } |
130 | ||
ff880b78 RW |
131 | static int if_cmp_index_func(const struct interface *ifp1, |
132 | const struct interface *ifp2) | |
133 | { | |
134 | return ifp1->ifindex - ifp2->ifindex; | |
135 | } | |
136 | ||
718e3744 | 137 | /* Create new interface structure. */ |
bcc24579 | 138 | struct interface *if_create(const char *name, vrf_id_t vrf_id) |
718e3744 | 139 | { |
f4e14fdb | 140 | struct vrf *vrf = vrf_get(vrf_id, NULL); |
d62a17ae | 141 | struct interface *ifp; |
d62a17ae | 142 | |
143 | ifp = XCALLOC(MTYPE_IF, sizeof(struct interface)); | |
144 | ifp->ifindex = IFINDEX_INTERNAL; | |
145 | ||
146 | assert(name); | |
bcc24579 | 147 | strlcpy(ifp->name, name, sizeof(ifp->name)); |
d62a17ae | 148 | ifp->vrf_id = vrf_id; |
ff880b78 | 149 | IFNAME_RB_INSERT(vrf, ifp); |
d62a17ae | 150 | ifp->connected = list_new(); |
151 | ifp->connected->del = (void (*)(void *))connected_free; | |
152 | ||
153 | ifp->nbr_connected = list_new(); | |
154 | ifp->nbr_connected->del = (void (*)(void *))nbr_connected_free; | |
155 | ||
156 | /* Enable Link-detection by default */ | |
157 | SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION); | |
158 | ||
159 | QOBJ_REG(ifp, interface); | |
996c9314 | 160 | hook_call(if_add, ifp); |
d62a17ae | 161 | return ifp; |
718e3744 | 162 | } |
163 | ||
f93e3f69 | 164 | /* Create new interface structure. */ |
d62a17ae | 165 | void if_update_to_new_vrf(struct interface *ifp, vrf_id_t vrf_id) |
f93e3f69 | 166 | { |
8f90d89b | 167 | struct vrf *old_vrf, *vrf; |
d62a17ae | 168 | |
169 | /* remove interface from old master vrf list */ | |
8f90d89b RW |
170 | old_vrf = vrf_lookup_by_id(ifp->vrf_id); |
171 | if (old_vrf) { | |
172 | IFNAME_RB_REMOVE(old_vrf, ifp); | |
ff880b78 | 173 | if (ifp->ifindex != IFINDEX_INTERNAL) |
8f90d89b | 174 | IFINDEX_RB_REMOVE(old_vrf, ifp); |
ff880b78 | 175 | } |
d62a17ae | 176 | |
177 | ifp->vrf_id = vrf_id; | |
f4e14fdb | 178 | vrf = vrf_get(ifp->vrf_id, NULL); |
ff880b78 RW |
179 | |
180 | IFNAME_RB_INSERT(vrf, ifp); | |
181 | if (ifp->ifindex != IFINDEX_INTERNAL) | |
182 | IFINDEX_RB_INSERT(vrf, ifp); | |
8f90d89b RW |
183 | |
184 | /* | |
185 | * HACK: Change the interface VRF in the running configuration directly, | |
186 | * bypassing the northbound layer. This is necessary to avoid deleting | |
187 | * the interface and readding it in the new VRF, which would have | |
188 | * several implications. | |
189 | */ | |
190 | if (yang_module_find("frr-interface")) { | |
191 | struct lyd_node *if_dnode; | |
192 | ||
193 | if_dnode = yang_dnode_get( | |
194 | running_config->dnode, | |
195 | "/frr-interface:lib/interface[name='%s'][vrf='%s']/vrf", | |
196 | ifp->name, old_vrf->name); | |
197 | if (if_dnode) { | |
198 | yang_dnode_change_leaf(if_dnode, vrf->name); | |
199 | running_config->version++; | |
200 | } | |
201 | } | |
f93e3f69 DS |
202 | } |
203 | ||
204 | ||
d2fc8896 | 205 | /* Delete interface structure. */ |
d62a17ae | 206 | void if_delete_retain(struct interface *ifp) |
718e3744 | 207 | { |
996c9314 | 208 | hook_call(if_del, ifp); |
d62a17ae | 209 | QOBJ_UNREG(ifp); |
e80e7cce | 210 | |
d62a17ae | 211 | /* Free connected address list */ |
212 | list_delete_all_node(ifp->connected); | |
a80beece | 213 | |
d62a17ae | 214 | /* Free connected nbr address list */ |
215 | list_delete_all_node(ifp->nbr_connected); | |
d2fc8896 | 216 | } |
217 | ||
218 | /* Delete and free interface structure. */ | |
d62a17ae | 219 | void if_delete(struct interface *ifp) |
d2fc8896 | 220 | { |
5b8524f5 RW |
221 | struct vrf *vrf; |
222 | ||
223 | vrf = vrf_lookup_by_id(ifp->vrf_id); | |
224 | assert(vrf); | |
f4e14fdb | 225 | |
ff880b78 RW |
226 | IFNAME_RB_REMOVE(vrf, ifp); |
227 | if (ifp->ifindex != IFINDEX_INTERNAL) | |
228 | IFINDEX_RB_REMOVE(vrf, ifp); | |
d2fc8896 | 229 | |
d62a17ae | 230 | if_delete_retain(ifp); |
718e3744 | 231 | |
6a154c88 DL |
232 | list_delete(&ifp->connected); |
233 | list_delete(&ifp->nbr_connected); | |
2dd04c5d | 234 | |
d62a17ae | 235 | if_link_params_free(ifp); |
16f1b9ee | 236 | |
c7974c0f DS |
237 | if (ifp->desc) |
238 | XFREE(MTYPE_TMP, ifp->desc); | |
239 | ||
d62a17ae | 240 | XFREE(MTYPE_IF, ifp); |
718e3744 | 241 | } |
242 | ||
718e3744 | 243 | /* Interface existance check by index. */ |
d62a17ae | 244 | struct interface *if_lookup_by_index(ifindex_t ifindex, vrf_id_t vrf_id) |
718e3744 | 245 | { |
5b8524f5 | 246 | struct vrf *vrf; |
ff880b78 | 247 | struct interface if_tmp; |
f4e14fdb | 248 | |
5b8524f5 RW |
249 | vrf = vrf_lookup_by_id(vrf_id); |
250 | if (!vrf) | |
251 | return NULL; | |
252 | ||
ff880b78 RW |
253 | if_tmp.ifindex = ifindex; |
254 | return RB_FIND(if_index_head, &vrf->ifaces_by_index, &if_tmp); | |
718e3744 | 255 | } |
256 | ||
d62a17ae | 257 | const char *ifindex2ifname(ifindex_t ifindex, vrf_id_t vrf_id) |
718e3744 | 258 | { |
d62a17ae | 259 | struct interface *ifp; |
718e3744 | 260 | |
d62a17ae | 261 | return ((ifp = if_lookup_by_index(ifindex, vrf_id)) != NULL) |
262 | ? ifp->name | |
263 | : "unknown"; | |
d2fc8896 | 264 | } |
265 | ||
d62a17ae | 266 | ifindex_t ifname2ifindex(const char *name, vrf_id_t vrf_id) |
d2fc8896 | 267 | { |
d62a17ae | 268 | struct interface *ifp; |
d2fc8896 | 269 | |
d62a17ae | 270 | return ((ifp = if_lookup_by_name(name, vrf_id)) != NULL) |
271 | ? ifp->ifindex | |
272 | : IFINDEX_INTERNAL; | |
718e3744 | 273 | } |
274 | ||
275 | /* Interface existance check by interface name. */ | |
d62a17ae | 276 | struct interface *if_lookup_by_name(const char *name, vrf_id_t vrf_id) |
718e3744 | 277 | { |
f4e14fdb RW |
278 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
279 | struct interface if_tmp; | |
d62a17ae | 280 | |
5b8524f5 RW |
281 | if (!vrf || !name |
282 | || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ) | |
bcc24579 | 283 | return NULL; |
f8962871 | 284 | |
f4e14fdb RW |
285 | strlcpy(if_tmp.name, name, sizeof(if_tmp.name)); |
286 | return RB_FIND(if_name_head, &vrf->ifaces_by_name, &if_tmp); | |
f8962871 DS |
287 | } |
288 | ||
bcc24579 | 289 | struct interface *if_lookup_by_name_all_vrf(const char *name) |
a349198f | 290 | { |
bcc24579 | 291 | struct vrf *vrf; |
d62a17ae | 292 | struct interface *ifp; |
a349198f | 293 | |
bcc24579 | 294 | if (!name || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ) |
d62a17ae | 295 | return NULL; |
a349198f | 296 | |
bcc24579 RW |
297 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
298 | ifp = if_lookup_by_name(name, vrf->vrf_id); | |
299 | if (ifp) | |
d62a17ae | 300 | return ifp; |
301 | } | |
bcc24579 | 302 | |
d62a17ae | 303 | return NULL; |
a349198f | 304 | } |
305 | ||
718e3744 | 306 | /* Lookup interface by IPv4 address. */ |
d62a17ae | 307 | struct interface *if_lookup_exact_address(void *src, int family, |
308 | vrf_id_t vrf_id) | |
718e3744 | 309 | { |
f4e14fdb | 310 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
d62a17ae | 311 | struct listnode *cnode; |
312 | struct interface *ifp; | |
313 | struct prefix *p; | |
314 | struct connected *c; | |
315 | ||
451fda4f | 316 | FOR_ALL_INTERFACES (vrf, ifp) { |
d62a17ae | 317 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) { |
318 | p = c->address; | |
319 | ||
320 | if (p && (p->family == family)) { | |
321 | if (family == AF_INET) { | |
322 | if (IPV4_ADDR_SAME( | |
323 | &p->u.prefix4, | |
324 | (struct in_addr *)src)) | |
325 | return ifp; | |
326 | } else if (family == AF_INET6) { | |
327 | if (IPV6_ADDR_SAME( | |
328 | &p->u.prefix6, | |
329 | (struct in6_addr *)src)) | |
330 | return ifp; | |
331 | } | |
332 | } | |
0aabccc0 | 333 | } |
718e3744 | 334 | } |
d62a17ae | 335 | return NULL; |
718e3744 | 336 | } |
337 | ||
338 | /* Lookup interface by IPv4 address. */ | |
d62a17ae | 339 | struct connected *if_lookup_address(void *matchaddr, int family, |
340 | vrf_id_t vrf_id) | |
718e3744 | 341 | { |
f4e14fdb | 342 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
d62a17ae | 343 | struct prefix addr; |
344 | int bestlen = 0; | |
345 | struct listnode *cnode; | |
346 | struct interface *ifp; | |
347 | struct connected *c; | |
348 | struct connected *match; | |
349 | ||
350 | if (family == AF_INET) { | |
351 | addr.family = AF_INET; | |
352 | addr.u.prefix4 = *((struct in_addr *)matchaddr); | |
353 | addr.prefixlen = IPV4_MAX_BITLEN; | |
354 | } else if (family == AF_INET6) { | |
355 | addr.family = AF_INET6; | |
356 | addr.u.prefix6 = *((struct in6_addr *)matchaddr); | |
357 | addr.prefixlen = IPV6_MAX_BITLEN; | |
358 | } | |
718e3744 | 359 | |
d62a17ae | 360 | match = NULL; |
718e3744 | 361 | |
451fda4f | 362 | FOR_ALL_INTERFACES (vrf, ifp) { |
d62a17ae | 363 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) { |
364 | if (c->address && (c->address->family == AF_INET) | |
365 | && prefix_match(CONNECTED_PREFIX(c), &addr) | |
366 | && (c->address->prefixlen > bestlen)) { | |
367 | bestlen = c->address->prefixlen; | |
368 | match = c; | |
369 | } | |
370 | } | |
718e3744 | 371 | } |
d62a17ae | 372 | return match; |
718e3744 | 373 | } |
374 | ||
b81e97a8 | 375 | /* Lookup interface by prefix */ |
d62a17ae | 376 | struct interface *if_lookup_prefix(struct prefix *prefix, vrf_id_t vrf_id) |
b81e97a8 | 377 | { |
f4e14fdb | 378 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
d62a17ae | 379 | struct listnode *cnode; |
380 | struct interface *ifp; | |
381 | struct connected *c; | |
382 | ||
451fda4f | 383 | FOR_ALL_INTERFACES (vrf, ifp) { |
d62a17ae | 384 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) { |
385 | if (prefix_cmp(c->address, prefix) == 0) { | |
386 | return ifp; | |
387 | } | |
388 | } | |
389 | } | |
390 | return NULL; | |
b81e97a8 DD |
391 | } |
392 | ||
718e3744 | 393 | /* Get interface by name if given name interface doesn't exist create |
394 | one. */ | |
8f90d89b | 395 | struct interface *if_get_by_name(const char *name, vrf_id_t vrf_id) |
718e3744 | 396 | { |
8f90d89b | 397 | struct interface *ifp; |
718e3744 | 398 | |
8f90d89b RW |
399 | switch (vrf_get_backend()) { |
400 | case VRF_BACKEND_NETNS: | |
ee2f2c23 TC |
401 | ifp = if_lookup_by_name(name, vrf_id); |
402 | if (ifp) | |
403 | return ifp; | |
e9e9b115 | 404 | return if_create(name, vrf_id); |
8f90d89b RW |
405 | case VRF_BACKEND_VRF_LITE: |
406 | ifp = if_lookup_by_name_all_vrf(name); | |
407 | if (ifp) { | |
408 | if (ifp->vrf_id == vrf_id) | |
379eb245 | 409 | return ifp; |
8f90d89b RW |
410 | /* If it came from the kernel or by way of zclient, |
411 | * believe it and update the ifp accordingly. | |
412 | */ | |
413 | if_update_to_new_vrf(ifp, vrf_id); | |
414 | return ifp; | |
ee2f2c23 | 415 | } |
8f90d89b | 416 | return if_create(name, vrf_id); |
85f9da7f | 417 | } |
8f90d89b RW |
418 | |
419 | return NULL; | |
8736158a FL |
420 | } |
421 | ||
ff880b78 RW |
422 | void if_set_index(struct interface *ifp, ifindex_t ifindex) |
423 | { | |
5b8524f5 RW |
424 | struct vrf *vrf; |
425 | ||
426 | vrf = vrf_lookup_by_id(ifp->vrf_id); | |
427 | assert(vrf); | |
ff880b78 RW |
428 | |
429 | if (ifp->ifindex == ifindex) | |
430 | return; | |
431 | ||
432 | if (ifp->ifindex != IFINDEX_INTERNAL) | |
433 | IFINDEX_RB_REMOVE(vrf, ifp) | |
434 | ||
435 | ifp->ifindex = ifindex; | |
436 | ||
437 | if (ifp->ifindex != IFINDEX_INTERNAL) | |
438 | IFINDEX_RB_INSERT(vrf, ifp) | |
439 | } | |
440 | ||
718e3744 | 441 | /* Does interface up ? */ |
d62a17ae | 442 | int if_is_up(struct interface *ifp) |
718e3744 | 443 | { |
d62a17ae | 444 | return ifp->flags & IFF_UP; |
718e3744 | 445 | } |
446 | ||
2e3b2e47 | 447 | /* Is interface running? */ |
d62a17ae | 448 | int if_is_running(struct interface *ifp) |
2e3b2e47 | 449 | { |
d62a17ae | 450 | return ifp->flags & IFF_RUNNING; |
2e3b2e47 | 451 | } |
452 | ||
453 | /* Is the interface operative, eg. either UP & RUNNING | |
244c1cdc DS |
454 | or UP & !ZEBRA_INTERFACE_LINK_DETECTION and |
455 | if ptm checking is enabled, then ptm check has passed */ | |
d62a17ae | 456 | int if_is_operative(struct interface *ifp) |
2e3b2e47 | 457 | { |
d62a17ae | 458 | return ((ifp->flags & IFF_UP) |
459 | && (((ifp->flags & IFF_RUNNING) | |
460 | && (ifp->ptm_status || !ifp->ptm_enable)) | |
461 | || !CHECK_FLAG(ifp->status, | |
462 | ZEBRA_INTERFACE_LINKDETECTION))); | |
244c1cdc DS |
463 | } |
464 | ||
465 | /* Is the interface operative, eg. either UP & RUNNING | |
466 | or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */ | |
d62a17ae | 467 | int if_is_no_ptm_operative(struct interface *ifp) |
244c1cdc | 468 | { |
d62a17ae | 469 | return ((ifp->flags & IFF_UP) |
470 | && ((ifp->flags & IFF_RUNNING) | |
471 | || !CHECK_FLAG(ifp->status, | |
472 | ZEBRA_INTERFACE_LINKDETECTION))); | |
2e3b2e47 | 473 | } |
474 | ||
718e3744 | 475 | /* Is this loopback interface ? */ |
d62a17ae | 476 | int if_is_loopback(struct interface *ifp) |
718e3744 | 477 | { |
d62a17ae | 478 | /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M |
479 | * but Y on platform N? | |
480 | */ | |
481 | return (ifp->flags & (IFF_LOOPBACK | IFF_NOXMIT | IFF_VIRTUAL)); | |
718e3744 | 482 | } |
483 | ||
0c74bbe0 CS |
484 | /* Check interface is VRF */ |
485 | int if_is_vrf(struct interface *ifp) | |
486 | { | |
487 | return CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
488 | } | |
489 | ||
fec4ca19 DS |
490 | bool if_is_loopback_or_vrf(struct interface *ifp) |
491 | { | |
492 | if (if_is_loopback(ifp) || if_is_vrf(ifp)) | |
493 | return true; | |
494 | ||
495 | return false; | |
496 | } | |
497 | ||
718e3744 | 498 | /* Does this interface support broadcast ? */ |
d62a17ae | 499 | int if_is_broadcast(struct interface *ifp) |
718e3744 | 500 | { |
d62a17ae | 501 | return ifp->flags & IFF_BROADCAST; |
718e3744 | 502 | } |
503 | ||
504 | /* Does this interface support broadcast ? */ | |
d62a17ae | 505 | int if_is_pointopoint(struct interface *ifp) |
718e3744 | 506 | { |
d62a17ae | 507 | return ifp->flags & IFF_POINTOPOINT; |
718e3744 | 508 | } |
509 | ||
510 | /* Does this interface support multicast ? */ | |
d62a17ae | 511 | int if_is_multicast(struct interface *ifp) |
718e3744 | 512 | { |
d62a17ae | 513 | return ifp->flags & IFF_MULTICAST; |
718e3744 | 514 | } |
515 | ||
516 | /* Printout flag information into log */ | |
d62a17ae | 517 | const char *if_flag_dump(unsigned long flag) |
718e3744 | 518 | { |
d62a17ae | 519 | int separator = 0; |
520 | static char logbuf[BUFSIZ]; | |
521 | ||
522 | #define IFF_OUT_LOG(X, STR) \ | |
523 | if (flag & (X)) { \ | |
524 | if (separator) \ | |
525 | strlcat(logbuf, ",", BUFSIZ); \ | |
526 | else \ | |
527 | separator = 1; \ | |
528 | strlcat(logbuf, STR, BUFSIZ); \ | |
529 | } | |
718e3744 | 530 | |
d62a17ae | 531 | strlcpy(logbuf, "<", BUFSIZ); |
532 | IFF_OUT_LOG(IFF_UP, "UP"); | |
533 | IFF_OUT_LOG(IFF_BROADCAST, "BROADCAST"); | |
534 | IFF_OUT_LOG(IFF_DEBUG, "DEBUG"); | |
535 | IFF_OUT_LOG(IFF_LOOPBACK, "LOOPBACK"); | |
536 | IFF_OUT_LOG(IFF_POINTOPOINT, "POINTOPOINT"); | |
537 | IFF_OUT_LOG(IFF_NOTRAILERS, "NOTRAILERS"); | |
538 | IFF_OUT_LOG(IFF_RUNNING, "RUNNING"); | |
539 | IFF_OUT_LOG(IFF_NOARP, "NOARP"); | |
540 | IFF_OUT_LOG(IFF_PROMISC, "PROMISC"); | |
541 | IFF_OUT_LOG(IFF_ALLMULTI, "ALLMULTI"); | |
542 | IFF_OUT_LOG(IFF_OACTIVE, "OACTIVE"); | |
543 | IFF_OUT_LOG(IFF_SIMPLEX, "SIMPLEX"); | |
544 | IFF_OUT_LOG(IFF_LINK0, "LINK0"); | |
545 | IFF_OUT_LOG(IFF_LINK1, "LINK1"); | |
546 | IFF_OUT_LOG(IFF_LINK2, "LINK2"); | |
547 | IFF_OUT_LOG(IFF_MULTICAST, "MULTICAST"); | |
548 | IFF_OUT_LOG(IFF_NOXMIT, "NOXMIT"); | |
549 | IFF_OUT_LOG(IFF_NORTEXCH, "NORTEXCH"); | |
550 | IFF_OUT_LOG(IFF_VIRTUAL, "VIRTUAL"); | |
551 | IFF_OUT_LOG(IFF_IPV4, "IPv4"); | |
552 | IFF_OUT_LOG(IFF_IPV6, "IPv6"); | |
553 | ||
554 | strlcat(logbuf, ">", BUFSIZ); | |
555 | ||
556 | return logbuf; | |
630c97ce | 557 | #undef IFF_OUT_LOG |
718e3744 | 558 | } |
559 | ||
560 | /* For debugging */ | |
d62a17ae | 561 | static void if_dump(const struct interface *ifp) |
718e3744 | 562 | { |
d62a17ae | 563 | struct listnode *node; |
564 | struct connected *c __attribute__((unused)); | |
565 | ||
566 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, c)) | |
567 | zlog_info( | |
568 | "Interface %s vrf %u index %d metric %d mtu %d " | |
569 | "mtu6 %d %s", | |
570 | ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric, | |
571 | ifp->mtu, ifp->mtu6, if_flag_dump(ifp->flags)); | |
718e3744 | 572 | } |
573 | ||
574 | /* Interface printing for all interface. */ | |
d62a17ae | 575 | void if_dump_all(void) |
718e3744 | 576 | { |
d62a17ae | 577 | struct vrf *vrf; |
f4e14fdb | 578 | void *ifp; |
d62a17ae | 579 | |
a2addae8 | 580 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) |
451fda4f | 581 | FOR_ALL_INTERFACES (vrf, ifp) |
f4e14fdb | 582 | if_dump(ifp); |
718e3744 | 583 | } |
584 | ||
98954844 PJ |
585 | #ifdef SUNOS_5 |
586 | /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created | |
587 | * a seperate struct interface for each logical interface, so config | |
588 | * file may be full of 'interface fooX:Y'. Solaris however does not | |
589 | * expose logical interfaces via PF_ROUTE, so trying to track logical | |
590 | * interfaces can be fruitless, for that reason Quagga only tracks | |
591 | * the primary IP interface. | |
592 | * | |
593 | * We try accomodate SUNWzebra by: | |
594 | * - looking up the interface name, to see whether it exists, if so | |
595 | * its useable | |
596 | * - for protocol daemons, this could only because zebra told us of | |
597 | * the interface | |
598 | * - for zebra, only because it learnt from kernel | |
599 | * - if not: | |
600 | * - search the name to see if it contains a sub-ipif / logical interface | |
601 | * seperator, the ':' char. If it does: | |
602 | * - text up to that char must be the primary name - get that name. | |
603 | * if not: | |
604 | * - no idea, just get the name in its entirety. | |
605 | */ | |
ae9eebca | 606 | static struct interface *if_sunwzebra_get(const char *name, vrf_id_t vrf_id) |
98954844 | 607 | { |
d62a17ae | 608 | struct interface *ifp; |
bcc24579 | 609 | char *cp; |
d62a17ae | 610 | |
bcc24579 | 611 | if ((ifp = if_lookup_by_name(name, vrf_id)) != NULL) |
d62a17ae | 612 | return ifp; |
613 | ||
614 | /* hunt the primary interface name... */ | |
bcc24579 RW |
615 | cp = strchr(name, ':'); |
616 | if (cp) | |
617 | *cp = '\0'; | |
d62a17ae | 618 | |
8f90d89b | 619 | return if_get_by_name(name, vrf_id); |
98954844 PJ |
620 | } |
621 | #endif /* SUNOS_5 */ | |
6b0655a2 | 622 | |
d7d73ffc | 623 | #if 0 |
718e3744 | 624 | /* For debug purpose. */ |
625 | DEFUN (show_address, | |
626 | show_address_cmd, | |
199d90a1 | 627 | "show address [vrf NAME]", |
718e3744 | 628 | SHOW_STR |
fcbee690 QY |
629 | "address\n" |
630 | VRF_CMD_HELP_STR) | |
718e3744 | 631 | { |
abddf075 | 632 | int idx_vrf = 3; |
52dc7ee6 | 633 | struct listnode *node; |
718e3744 | 634 | struct interface *ifp; |
635 | struct connected *ifc; | |
636 | struct prefix *p; | |
8736158a | 637 | vrf_id_t vrf_id = VRF_DEFAULT; |
718e3744 | 638 | |
fcbee690 | 639 | if (argc > 2) |
58749582 | 640 | VRF_GET_ID (vrf_id, argv[idx_vrf]->arg); |
8736158a | 641 | |
451fda4f | 642 | FOR_ALL_INTERFACES (vrf, ifp) |
718e3744 | 643 | { |
f4e14fdb | 644 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc)) |
718e3744 | 645 | { |
718e3744 | 646 | p = ifc->address; |
647 | ||
648 | if (p->family == AF_INET) | |
55f70b67 | 649 | vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen); |
718e3744 | 650 | } |
651 | } | |
652 | return CMD_SUCCESS; | |
653 | } | |
654 | ||
8736158a FL |
655 | DEFUN (show_address_vrf_all, |
656 | show_address_vrf_all_cmd, | |
9ccf14f7 | 657 | "show address vrf all", |
8736158a FL |
658 | SHOW_STR |
659 | "address\n" | |
660 | VRF_ALL_CMD_HELP_STR) | |
661 | { | |
1a1a7065 | 662 | struct vrf *vrf; |
8736158a | 663 | struct listnode *node; |
8736158a FL |
664 | struct interface *ifp; |
665 | struct connected *ifc; | |
666 | struct prefix *p; | |
8736158a | 667 | |
a62c4901 | 668 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) |
8736158a | 669 | { |
f4e14fdb | 670 | if (RB_EMPTY (if_name_head, &vrf->ifaces_by_name)) |
8736158a FL |
671 | continue; |
672 | ||
55f70b67 | 673 | vty_out (vty, "\nVRF %u\n\n", vrf->vrf_id); |
8736158a | 674 | |
451fda4f | 675 | FOR_ALL_INTERFACES (vrf, ifp) |
8736158a | 676 | { |
f4e14fdb | 677 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc)) |
8736158a FL |
678 | { |
679 | p = ifc->address; | |
680 | ||
681 | if (p->family == AF_INET) | |
55f70b67 | 682 | vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen); |
8736158a FL |
683 | } |
684 | } | |
685 | } | |
686 | return CMD_SUCCESS; | |
687 | } | |
d7d73ffc | 688 | #endif |
8736158a | 689 | |
718e3744 | 690 | /* Allocate connected structure. */ |
d62a17ae | 691 | struct connected *connected_new(void) |
718e3744 | 692 | { |
d62a17ae | 693 | return XCALLOC(MTYPE_CONNECTED, sizeof(struct connected)); |
718e3744 | 694 | } |
695 | ||
a80beece | 696 | /* Allocate nbr connected structure. */ |
d62a17ae | 697 | struct nbr_connected *nbr_connected_new(void) |
a80beece | 698 | { |
d62a17ae | 699 | return XCALLOC(MTYPE_NBR_CONNECTED, sizeof(struct nbr_connected)); |
a80beece DS |
700 | } |
701 | ||
718e3744 | 702 | /* Free connected structure. */ |
d62a17ae | 703 | void connected_free(struct connected *connected) |
718e3744 | 704 | { |
d62a17ae | 705 | if (connected->address) |
706 | prefix_free(connected->address); | |
718e3744 | 707 | |
d62a17ae | 708 | if (connected->destination) |
709 | prefix_free(connected->destination); | |
718e3744 | 710 | |
d62a17ae | 711 | if (connected->label) |
712 | XFREE(MTYPE_CONNECTED_LABEL, connected->label); | |
718e3744 | 713 | |
d62a17ae | 714 | XFREE(MTYPE_CONNECTED, connected); |
718e3744 | 715 | } |
716 | ||
a80beece | 717 | /* Free nbr connected structure. */ |
d62a17ae | 718 | void nbr_connected_free(struct nbr_connected *connected) |
a80beece | 719 | { |
d62a17ae | 720 | if (connected->address) |
721 | prefix_free(connected->address); | |
a80beece | 722 | |
d62a17ae | 723 | XFREE(MTYPE_NBR_CONNECTED, connected); |
a80beece DS |
724 | } |
725 | ||
726 | /* If same interface nbr address already exists... */ | |
d62a17ae | 727 | struct nbr_connected *nbr_connected_check(struct interface *ifp, |
728 | struct prefix *p) | |
a80beece | 729 | { |
d62a17ae | 730 | struct nbr_connected *ifc; |
731 | struct listnode *node; | |
a80beece | 732 | |
d62a17ae | 733 | for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, ifc)) |
734 | if (prefix_same(ifc->address, p)) | |
735 | return ifc; | |
a80beece | 736 | |
d62a17ae | 737 | return NULL; |
a80beece DS |
738 | } |
739 | ||
718e3744 | 740 | /* Print if_addr structure. */ |
d62a17ae | 741 | static void __attribute__((unused)) |
742 | connected_log(struct connected *connected, char *str) | |
718e3744 | 743 | { |
d62a17ae | 744 | struct prefix *p; |
745 | struct interface *ifp; | |
746 | char logbuf[BUFSIZ]; | |
747 | char buf[BUFSIZ]; | |
748 | ||
749 | ifp = connected->ifp; | |
750 | p = connected->address; | |
751 | ||
752 | snprintf(logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ", str, | |
753 | ifp->name, ifp->vrf_id, prefix_family_str(p), | |
754 | inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen); | |
755 | ||
756 | p = connected->destination; | |
757 | if (p) { | |
758 | strncat(logbuf, inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), | |
759 | BUFSIZ - strlen(logbuf)); | |
760 | } | |
761 | zlog_info("%s", logbuf); | |
718e3744 | 762 | } |
763 | ||
a80beece | 764 | /* Print if_addr structure. */ |
d62a17ae | 765 | static void __attribute__((unused)) |
766 | nbr_connected_log(struct nbr_connected *connected, char *str) | |
a80beece | 767 | { |
d62a17ae | 768 | struct prefix *p; |
769 | struct interface *ifp; | |
770 | char logbuf[BUFSIZ]; | |
771 | char buf[BUFSIZ]; | |
a80beece | 772 | |
d62a17ae | 773 | ifp = connected->ifp; |
774 | p = connected->address; | |
a80beece | 775 | |
d62a17ae | 776 | snprintf(logbuf, BUFSIZ, "%s interface %s %s %s/%d ", str, ifp->name, |
777 | prefix_family_str(p), | |
778 | inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen); | |
a80beece | 779 | |
d62a17ae | 780 | zlog_info("%s", logbuf); |
a80beece DS |
781 | } |
782 | ||
718e3744 | 783 | /* If two connected address has same prefix return 1. */ |
d62a17ae | 784 | static int connected_same_prefix(struct prefix *p1, struct prefix *p2) |
718e3744 | 785 | { |
d62a17ae | 786 | if (p1->family == p2->family) { |
787 | if (p1->family == AF_INET | |
788 | && IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4)) | |
789 | return 1; | |
790 | if (p1->family == AF_INET6 | |
791 | && IPV6_ADDR_SAME(&p1->u.prefix6, &p2->u.prefix6)) | |
792 | return 1; | |
793 | } | |
794 | return 0; | |
718e3744 | 795 | } |
796 | ||
d62a17ae | 797 | struct connected *connected_lookup_prefix_exact(struct interface *ifp, |
798 | struct prefix *p) | |
38485402 | 799 | { |
d62a17ae | 800 | struct listnode *node; |
801 | struct listnode *next; | |
802 | struct connected *ifc; | |
38485402 | 803 | |
d62a17ae | 804 | for (node = listhead(ifp->connected); node; node = next) { |
805 | ifc = listgetdata(node); | |
806 | next = node->next; | |
38485402 | 807 | |
d62a17ae | 808 | if (connected_same_prefix(ifc->address, p)) |
809 | return ifc; | |
810 | } | |
811 | return NULL; | |
38485402 DS |
812 | } |
813 | ||
d62a17ae | 814 | struct connected *connected_delete_by_prefix(struct interface *ifp, |
815 | struct prefix *p) | |
718e3744 | 816 | { |
d62a17ae | 817 | struct listnode *node; |
818 | struct listnode *next; | |
819 | struct connected *ifc; | |
820 | ||
821 | /* In case of same prefix come, replace it with new one. */ | |
822 | for (node = listhead(ifp->connected); node; node = next) { | |
823 | ifc = listgetdata(node); | |
824 | next = node->next; | |
825 | ||
826 | if (connected_same_prefix(ifc->address, p)) { | |
827 | listnode_delete(ifp->connected, ifc); | |
828 | return ifc; | |
829 | } | |
718e3744 | 830 | } |
d62a17ae | 831 | return NULL; |
718e3744 | 832 | } |
833 | ||
bd40c341 | 834 | /* Find the address on our side that will be used when packets |
727d104b | 835 | are sent to dst. */ |
d62a17ae | 836 | struct connected *connected_lookup_prefix(struct interface *ifp, |
837 | struct prefix *addr) | |
727d104b | 838 | { |
d62a17ae | 839 | struct listnode *cnode; |
840 | struct connected *c; | |
841 | struct connected *match; | |
842 | ||
843 | match = NULL; | |
844 | ||
845 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) { | |
846 | if (c->address && (c->address->family == addr->family) | |
847 | && prefix_match(CONNECTED_PREFIX(c), addr) | |
848 | && (!match | |
849 | || (c->address->prefixlen > match->address->prefixlen))) | |
850 | match = c; | |
851 | } | |
852 | return match; | |
727d104b | 853 | } |
854 | ||
d62a17ae | 855 | struct connected *connected_add_by_prefix(struct interface *ifp, |
856 | struct prefix *p, | |
857 | struct prefix *destination) | |
4a7aac1b | 858 | { |
d62a17ae | 859 | struct connected *ifc; |
4a7aac1b | 860 | |
d62a17ae | 861 | /* Allocate new connected address. */ |
862 | ifc = connected_new(); | |
863 | ifc->ifp = ifp; | |
4a7aac1b | 864 | |
d62a17ae | 865 | /* Fetch interface address */ |
866 | ifc->address = prefix_new(); | |
867 | memcpy(ifc->address, p, sizeof(struct prefix)); | |
4a7aac1b | 868 | |
d62a17ae | 869 | /* Fetch dest address */ |
870 | if (destination) { | |
871 | ifc->destination = prefix_new(); | |
872 | memcpy(ifc->destination, destination, sizeof(struct prefix)); | |
873 | } | |
4a7aac1b | 874 | |
d62a17ae | 875 | /* Add connected address to the interface. */ |
876 | listnode_add(ifp->connected, ifc); | |
877 | return ifc; | |
4a7aac1b | 878 | } |
879 | ||
d62a17ae | 880 | #if 0 /* this route_table of struct connected's is unused \ |
881 | * however, it would be good to use a route_table rather than \ | |
882 | * a list.. \ | |
883 | */ | |
718e3744 | 884 | /* Interface looking up by interface's address. */ |
718e3744 | 885 | /* Interface's IPv4 address reverse lookup table. */ |
886 | struct route_table *ifaddr_ipv4_table; | |
887 | /* struct route_table *ifaddr_ipv6_table; */ | |
888 | ||
8cc4198f | 889 | static void |
718e3744 | 890 | ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp) |
891 | { | |
892 | struct route_node *rn; | |
893 | struct prefix_ipv4 p; | |
894 | ||
895 | p.family = AF_INET; | |
896 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
897 | p.prefix = *ifaddr; | |
898 | ||
899 | rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p); | |
900 | if (rn) | |
901 | { | |
902 | route_unlock_node (rn); | |
903 | zlog_info ("ifaddr_ipv4_add(): address %s is already added", | |
904 | inet_ntoa (*ifaddr)); | |
905 | return; | |
906 | } | |
907 | rn->info = ifp; | |
908 | } | |
909 | ||
8cc4198f | 910 | static void |
718e3744 | 911 | ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp) |
912 | { | |
913 | struct route_node *rn; | |
914 | struct prefix_ipv4 p; | |
915 | ||
916 | p.family = AF_INET; | |
917 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
918 | p.prefix = *ifaddr; | |
919 | ||
920 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); | |
921 | if (! rn) | |
922 | { | |
923 | zlog_info ("ifaddr_ipv4_delete(): can't find address %s", | |
924 | inet_ntoa (*ifaddr)); | |
925 | return; | |
926 | } | |
927 | rn->info = NULL; | |
928 | route_unlock_node (rn); | |
929 | route_unlock_node (rn); | |
930 | } | |
931 | ||
932 | /* Lookup interface by interface's IP address or interface index. */ | |
8cc4198f | 933 | static struct interface * |
b892f1dd | 934 | ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex) |
718e3744 | 935 | { |
936 | struct prefix_ipv4 p; | |
937 | struct route_node *rn; | |
938 | struct interface *ifp; | |
718e3744 | 939 | |
940 | if (addr) | |
941 | { | |
942 | p.family = AF_INET; | |
943 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
944 | p.prefix = *addr; | |
945 | ||
946 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); | |
947 | if (! rn) | |
948 | return NULL; | |
55cd0f61 | 949 | |
718e3744 | 950 | ifp = rn->info; |
951 | route_unlock_node (rn); | |
952 | return ifp; | |
953 | } | |
954 | else | |
7e2b7603 | 955 | return if_lookup_by_index(ifindex, VRF_DEFAULT); |
718e3744 | 956 | } |
8cc4198f | 957 | #endif /* ifaddr_ipv4_table */ |
718e3744 | 958 | |
f4e14fdb | 959 | void if_terminate(struct vrf *vrf) |
4bd045d5 | 960 | { |
f4e14fdb | 961 | struct interface *ifp; |
4bd045d5 | 962 | |
55cd0f61 DS |
963 | while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) { |
964 | ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name); | |
965 | ||
d62a17ae | 966 | if (ifp->node) { |
967 | ifp->node->info = NULL; | |
968 | route_unlock_node(ifp->node); | |
969 | } | |
d62a17ae | 970 | if_delete(ifp); |
971 | } | |
4bd045d5 | 972 | } |
8ccc7e80 | 973 | |
d62a17ae | 974 | const char *if_link_type_str(enum zebra_link_type llt) |
8ccc7e80 | 975 | { |
d62a17ae | 976 | switch (llt) { |
8ccc7e80 | 977 | #define llts(T,S) case (T): return (S) |
d62a17ae | 978 | llts(ZEBRA_LLT_UNKNOWN, "Unknown"); |
979 | llts(ZEBRA_LLT_ETHER, "Ethernet"); | |
980 | llts(ZEBRA_LLT_EETHER, "Experimental Ethernet"); | |
981 | llts(ZEBRA_LLT_AX25, "AX.25 Level 2"); | |
982 | llts(ZEBRA_LLT_PRONET, "PROnet token ring"); | |
983 | llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB"); | |
984 | llts(ZEBRA_LLT_ARCNET, "ARCnet"); | |
985 | llts(ZEBRA_LLT_APPLETLK, "AppleTalk"); | |
986 | llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI"); | |
987 | llts(ZEBRA_LLT_ATM, "ATM"); | |
988 | llts(ZEBRA_LLT_METRICOM, "Metricom STRIP"); | |
989 | llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4"); | |
990 | llts(ZEBRA_LLT_EUI64, "EUI-64"); | |
991 | llts(ZEBRA_LLT_INFINIBAND, "InfiniBand"); | |
992 | llts(ZEBRA_LLT_SLIP, "SLIP"); | |
993 | llts(ZEBRA_LLT_CSLIP, "Compressed SLIP"); | |
994 | llts(ZEBRA_LLT_SLIP6, "SLIPv6"); | |
995 | llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6"); | |
996 | llts(ZEBRA_LLT_ROSE, "ROSE packet radio"); | |
997 | llts(ZEBRA_LLT_X25, "CCITT X.25"); | |
998 | llts(ZEBRA_LLT_PPP, "PPP"); | |
999 | llts(ZEBRA_LLT_CHDLC, "Cisco HDLC"); | |
1000 | llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC"); | |
1001 | llts(ZEBRA_LLT_LAPB, "LAPB"); | |
1002 | llts(ZEBRA_LLT_IPIP, "IPIP Tunnel"); | |
1003 | llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel"); | |
1004 | llts(ZEBRA_LLT_FRAD, "FRAD"); | |
1005 | llts(ZEBRA_LLT_SKIP, "SKIP vif"); | |
1006 | llts(ZEBRA_LLT_LOOPBACK, "Loopback"); | |
1007 | llts(ZEBRA_LLT_LOCALTLK, "Localtalk"); | |
1008 | llts(ZEBRA_LLT_FDDI, "FDDI"); | |
1009 | llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT"); | |
1010 | llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel"); | |
1011 | llts(ZEBRA_LLT_IPGRE, "GRE over IP"); | |
1012 | llts(ZEBRA_LLT_PIMREG, "PIMSM registration"); | |
1013 | llts(ZEBRA_LLT_HIPPI, "HiPPI"); | |
1014 | llts(ZEBRA_LLT_IRDA, "IrDA"); | |
1015 | llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP"); | |
1016 | llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop"); | |
1017 | llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop"); | |
1018 | llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric"); | |
1019 | llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring"); | |
1020 | llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11"); | |
1021 | llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap"); | |
1022 | llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4"); | |
1023 | llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy"); | |
1024 | default: | |
450971aa | 1025 | flog_err(EC_LIB_DEVELOPMENT, "Unknown value %d", llt); |
d62a17ae | 1026 | return "Unknown type!"; |
8ccc7e80 | 1027 | #undef llts |
d62a17ae | 1028 | } |
1029 | return NULL; | |
8ccc7e80 | 1030 | } |
16f1b9ee | 1031 | |
d62a17ae | 1032 | struct if_link_params *if_link_params_get(struct interface *ifp) |
16f1b9ee | 1033 | { |
d62a17ae | 1034 | int i; |
16f1b9ee | 1035 | |
d62a17ae | 1036 | if (ifp->link_params != NULL) |
1037 | return ifp->link_params; | |
16f1b9ee | 1038 | |
d62a17ae | 1039 | struct if_link_params *iflp = |
1040 | XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params)); | |
1041 | if (iflp == NULL) | |
1042 | return NULL; | |
16f1b9ee | 1043 | |
d62a17ae | 1044 | /* Set TE metric equal to standard metric */ |
1045 | iflp->te_metric = ifp->metric; | |
16f1b9ee | 1046 | |
d62a17ae | 1047 | /* Compute default bandwidth based on interface */ |
1048 | iflp->default_bw = | |
1049 | ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH) | |
1050 | * TE_KILO_BIT / TE_BYTE); | |
16f1b9ee | 1051 | |
d62a17ae | 1052 | /* Set Max, Reservable and Unreserved Bandwidth */ |
1053 | iflp->max_bw = iflp->default_bw; | |
1054 | iflp->max_rsv_bw = iflp->default_bw; | |
1055 | for (i = 0; i < MAX_CLASS_TYPE; i++) | |
1056 | iflp->unrsv_bw[i] = iflp->default_bw; | |
16f1b9ee | 1057 | |
d62a17ae | 1058 | /* Update Link parameters status */ |
1059 | iflp->lp_status = | |
1060 | LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW; | |
16f1b9ee | 1061 | |
d62a17ae | 1062 | /* Finally attach newly created Link Parameters */ |
1063 | ifp->link_params = iflp; | |
16f1b9ee | 1064 | |
d62a17ae | 1065 | return iflp; |
16f1b9ee OD |
1066 | } |
1067 | ||
d62a17ae | 1068 | void if_link_params_free(struct interface *ifp) |
16f1b9ee | 1069 | { |
d62a17ae | 1070 | if (ifp->link_params == NULL) |
1071 | return; | |
1072 | XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params); | |
1073 | ifp->link_params = NULL; | |
16f1b9ee | 1074 | } |
a4bed468 | 1075 | |
8f90d89b RW |
1076 | /* ----------- CLI commands ----------- */ |
1077 | ||
1078 | /* | |
1079 | * XPath: /frr-interface:lib/interface | |
1080 | */ | |
1081 | DEFPY_NOSH (interface, | |
1082 | interface_cmd, | |
1083 | "interface IFNAME [vrf NAME$vrfname]", | |
1084 | "Select an interface to configure\n" | |
1085 | "Interface's name\n" | |
1086 | VRF_CMD_HELP_STR) | |
1087 | { | |
1088 | char xpath_list[XPATH_MAXLEN]; | |
8f90d89b RW |
1089 | vrf_id_t vrf_id; |
1090 | struct interface *ifp; | |
1091 | int ret; | |
1092 | ||
1093 | if (!vrfname) | |
1094 | vrfname = VRF_DEFAULT_NAME; | |
1095 | ||
1096 | /* | |
1097 | * This command requires special handling to maintain backward | |
1098 | * compatibility. If a VRF name is not specified, it means we're willing | |
1099 | * to accept any interface with the given name on any VRF. If no | |
1100 | * interface is found, then a new one should be created on the default | |
1101 | * VRF. | |
1102 | */ | |
1103 | VRF_GET_ID(vrf_id, vrfname, false); | |
1104 | ifp = if_lookup_by_name_all_vrf(ifname); | |
1105 | if (ifp && ifp->vrf_id != vrf_id) { | |
1106 | struct vrf *vrf; | |
1107 | ||
1108 | /* | |
1109 | * Special case 1: a VRF name was specified, but the found | |
1110 | * interface is associated to different VRF. Reject the command. | |
1111 | */ | |
1112 | if (vrf_id != VRF_DEFAULT) { | |
1113 | vty_out(vty, "%% interface %s not in %s vrf\n", ifname, | |
1114 | vrfname); | |
1115 | return CMD_WARNING_CONFIG_FAILED; | |
1116 | } | |
1117 | ||
1118 | /* | |
1119 | * Special case 2: a VRF name was *not* specified, and the found | |
1120 | * interface is associated to a VRF other than the default one. | |
1121 | * Update vrf_id and vrfname to account for that. | |
1122 | */ | |
1123 | vrf = vrf_lookup_by_id(ifp->vrf_id); | |
1124 | assert(vrf); | |
1125 | vrf_id = ifp->vrf_id; | |
1126 | vrfname = vrf->name; | |
1127 | } | |
1128 | ||
1129 | snprintf(xpath_list, sizeof(xpath_list), | |
1130 | "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname, | |
1131 | vrfname); | |
1132 | ||
a6233bfc RW |
1133 | nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, NULL); |
1134 | ret = nb_cli_apply_changes(vty, xpath_list); | |
8f90d89b RW |
1135 | if (ret == CMD_SUCCESS) { |
1136 | VTY_PUSH_XPATH(INTERFACE_NODE, xpath_list); | |
1137 | ||
1138 | /* | |
1139 | * For backward compatibility with old commands we still need | |
1140 | * to use the qobj infrastructure. This can be removed once | |
1141 | * all interface-level commands are converted to the new | |
1142 | * northbound model. | |
1143 | */ | |
1144 | ifp = if_lookup_by_name(ifname, vrf_id); | |
1145 | if (ifp) | |
1146 | VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp); | |
1147 | } | |
1148 | ||
1149 | return ret; | |
1150 | } | |
1151 | ||
1152 | DEFPY (no_interface, | |
1153 | no_interface_cmd, | |
1154 | "no interface IFNAME [vrf NAME$vrfname]", | |
1155 | NO_STR | |
1156 | "Delete a pseudo interface's configuration\n" | |
1157 | "Interface's name\n" | |
1158 | VRF_CMD_HELP_STR) | |
1159 | { | |
8f90d89b RW |
1160 | if (!vrfname) |
1161 | vrfname = VRF_DEFAULT_NAME; | |
1162 | ||
a6233bfc | 1163 | nb_cli_enqueue_change(vty, ".", NB_OP_DELETE, NULL); |
8f90d89b | 1164 | |
a6233bfc RW |
1165 | return nb_cli_apply_changes( |
1166 | vty, "/frr-interface:lib/interface[name='%s'][vrf='%s']", | |
1167 | ifname, vrfname); | |
8f90d89b RW |
1168 | } |
1169 | ||
1170 | static void cli_show_interface(struct vty *vty, struct lyd_node *dnode, | |
1171 | bool show_defaults) | |
1172 | { | |
1173 | const char *vrf; | |
1174 | ||
1175 | vrf = yang_dnode_get_string(dnode, "./vrf"); | |
1176 | ||
1177 | vty_out(vty, "!\n"); | |
1178 | vty_out(vty, "interface %s", yang_dnode_get_string(dnode, "./name")); | |
1179 | if (!strmatch(vrf, VRF_DEFAULT_NAME)) | |
1180 | vty_out(vty, " vrf %s", vrf); | |
1181 | vty_out(vty, "\n"); | |
1182 | } | |
1183 | ||
1184 | /* | |
1185 | * XPath: /frr-interface:lib/interface/description | |
1186 | */ | |
1187 | DEFPY (interface_desc, | |
1188 | interface_desc_cmd, | |
1189 | "description LINE...", | |
1190 | "Interface specific description\n" | |
1191 | "Characters describing this interface\n") | |
1192 | { | |
8f90d89b RW |
1193 | char *desc; |
1194 | int ret; | |
1195 | ||
1196 | desc = argv_concat(argv, argc, 1); | |
a6233bfc RW |
1197 | nb_cli_enqueue_change(vty, "./description", NB_OP_MODIFY, desc); |
1198 | ret = nb_cli_apply_changes(vty, NULL); | |
8f90d89b RW |
1199 | XFREE(MTYPE_TMP, desc); |
1200 | ||
1201 | return ret; | |
1202 | } | |
1203 | ||
1204 | DEFPY (no_interface_desc, | |
1205 | no_interface_desc_cmd, | |
1206 | "no description", | |
1207 | NO_STR | |
1208 | "Interface specific description\n") | |
1209 | { | |
a6233bfc RW |
1210 | nb_cli_enqueue_change(vty, "./description", NB_OP_DELETE, NULL); |
1211 | ||
1212 | return nb_cli_apply_changes(vty, NULL); | |
8f90d89b RW |
1213 | } |
1214 | ||
1215 | static void cli_show_interface_desc(struct vty *vty, struct lyd_node *dnode, | |
1216 | bool show_defaults) | |
1217 | { | |
1218 | vty_out(vty, " description %s\n", yang_dnode_get_string(dnode, NULL)); | |
1219 | } | |
1220 | ||
1221 | /* Interface autocomplete. */ | |
1222 | static void if_autocomplete(vector comps, struct cmd_token *token) | |
1223 | { | |
1224 | struct interface *ifp; | |
1225 | struct vrf *vrf; | |
1226 | ||
1227 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) { | |
1228 | FOR_ALL_INTERFACES (vrf, ifp) { | |
1229 | vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name)); | |
1230 | } | |
1231 | } | |
1232 | } | |
1233 | ||
1234 | static const struct cmd_variable_handler if_var_handlers[] = { | |
1235 | {/* "interface NAME" */ | |
1236 | .varname = "interface", | |
1237 | .completions = if_autocomplete}, | |
1238 | {.tokenname = "IFNAME", .completions = if_autocomplete}, | |
1239 | {.tokenname = "INTERFACE", .completions = if_autocomplete}, | |
1240 | {.completions = NULL}}; | |
1241 | ||
1242 | void if_cmd_init(void) | |
1243 | { | |
1244 | cmd_variable_handler_register(if_var_handlers); | |
1245 | ||
1246 | install_element(CONFIG_NODE, &interface_cmd); | |
1247 | install_element(CONFIG_NODE, &no_interface_cmd); | |
1248 | ||
1249 | install_default(INTERFACE_NODE); | |
1250 | install_element(INTERFACE_NODE, &interface_desc_cmd); | |
1251 | install_element(INTERFACE_NODE, &no_interface_desc_cmd); | |
1252 | } | |
1253 | ||
a4bed468 RW |
1254 | /* ------- Northbound callbacks ------- */ |
1255 | ||
1256 | /* | |
1257 | * XPath: /frr-interface:lib/interface | |
1258 | */ | |
1259 | static int lib_interface_create(enum nb_event event, | |
1260 | const struct lyd_node *dnode, | |
1261 | union nb_resource *resource) | |
1262 | { | |
8f90d89b RW |
1263 | const char *ifname; |
1264 | const char *vrfname; | |
1265 | struct vrf *vrf; | |
1266 | struct interface *ifp; | |
1267 | ||
1268 | ifname = yang_dnode_get_string(dnode, "./name"); | |
1269 | vrfname = yang_dnode_get_string(dnode, "./vrf"); | |
1270 | ||
1271 | switch (event) { | |
1272 | case NB_EV_VALIDATE: | |
1273 | vrf = vrf_lookup_by_name(vrfname); | |
1274 | if (!vrf) { | |
1275 | zlog_warn("%s: VRF %s doesn't exist", __func__, | |
1276 | vrfname); | |
1277 | return NB_ERR_VALIDATION; | |
1278 | } | |
1279 | if (vrf->vrf_id == VRF_UNKNOWN) { | |
1280 | zlog_warn("%s: VRF %s is not active", __func__, | |
1281 | vrf->name); | |
1282 | return NB_ERR_VALIDATION; | |
1283 | } | |
1284 | if (vrf_get_backend() == VRF_BACKEND_VRF_LITE) { | |
1285 | ifp = if_lookup_by_name_all_vrf(ifname); | |
1286 | if (ifp && ifp->vrf_id != vrf->vrf_id) { | |
1287 | zlog_warn( | |
1288 | "%s: interface %s already exists in another VRF", | |
1289 | __func__, ifp->name); | |
1290 | return NB_ERR_VALIDATION; | |
1291 | } | |
1292 | } | |
1293 | break; | |
1294 | case NB_EV_PREPARE: | |
1295 | case NB_EV_ABORT: | |
1296 | break; | |
1297 | case NB_EV_APPLY: | |
1298 | vrf = vrf_lookup_by_name(vrfname); | |
1299 | assert(vrf); | |
1300 | #ifdef SUNOS_5 | |
1301 | ifp = if_sunwzebra_get(ifname, vrf->vrf_id); | |
1302 | #else | |
1303 | ifp = if_get_by_name(ifname, vrf->vrf_id); | |
1304 | #endif /* SUNOS_5 */ | |
1305 | yang_dnode_set_entry(dnode, ifp); | |
1306 | break; | |
1307 | } | |
1308 | ||
a4bed468 RW |
1309 | return NB_OK; |
1310 | } | |
1311 | ||
1312 | static int lib_interface_delete(enum nb_event event, | |
1313 | const struct lyd_node *dnode) | |
1314 | { | |
8f90d89b RW |
1315 | struct interface *ifp; |
1316 | ||
25c780a3 | 1317 | ifp = yang_dnode_get_entry(dnode, true); |
8f90d89b RW |
1318 | |
1319 | switch (event) { | |
1320 | case NB_EV_VALIDATE: | |
1321 | if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) { | |
1322 | zlog_warn("%s: only inactive interfaces can be deleted", | |
1323 | __func__); | |
1324 | return NB_ERR_VALIDATION; | |
1325 | } | |
1326 | break; | |
1327 | case NB_EV_PREPARE: | |
1328 | case NB_EV_ABORT: | |
1329 | break; | |
1330 | case NB_EV_APPLY: | |
1331 | if_delete(ifp); | |
1332 | break; | |
1333 | } | |
1334 | ||
a4bed468 RW |
1335 | return NB_OK; |
1336 | } | |
1337 | ||
1338 | /* | |
1339 | * XPath: /frr-interface:lib/interface/description | |
1340 | */ | |
1341 | static int lib_interface_description_modify(enum nb_event event, | |
1342 | const struct lyd_node *dnode, | |
1343 | union nb_resource *resource) | |
1344 | { | |
8f90d89b RW |
1345 | struct interface *ifp; |
1346 | const char *description; | |
1347 | ||
1348 | if (event != NB_EV_APPLY) | |
1349 | return NB_OK; | |
1350 | ||
25c780a3 | 1351 | ifp = yang_dnode_get_entry(dnode, true); |
8f90d89b RW |
1352 | if (ifp->desc) |
1353 | XFREE(MTYPE_TMP, ifp->desc); | |
1354 | description = yang_dnode_get_string(dnode, NULL); | |
1355 | ifp->desc = XSTRDUP(MTYPE_TMP, description); | |
1356 | ||
a4bed468 RW |
1357 | return NB_OK; |
1358 | } | |
1359 | ||
1360 | static int lib_interface_description_delete(enum nb_event event, | |
1361 | const struct lyd_node *dnode) | |
1362 | { | |
8f90d89b RW |
1363 | struct interface *ifp; |
1364 | ||
1365 | if (event != NB_EV_APPLY) | |
1366 | return NB_OK; | |
1367 | ||
25c780a3 | 1368 | ifp = yang_dnode_get_entry(dnode, true); |
8f90d89b RW |
1369 | if (ifp->desc) |
1370 | XFREE(MTYPE_TMP, ifp->desc); | |
1371 | ||
a4bed468 RW |
1372 | return NB_OK; |
1373 | } | |
1374 | ||
1375 | /* clang-format off */ | |
1376 | const struct frr_yang_module_info frr_interface_info = { | |
1377 | .name = "frr-interface", | |
1378 | .nodes = { | |
1379 | { | |
1380 | .xpath = "/frr-interface:lib/interface", | |
1381 | .cbs.create = lib_interface_create, | |
1382 | .cbs.delete = lib_interface_delete, | |
8f90d89b | 1383 | .cbs.cli_show = cli_show_interface, |
a4bed468 RW |
1384 | }, |
1385 | { | |
1386 | .xpath = "/frr-interface:lib/interface/description", | |
1387 | .cbs.modify = lib_interface_description_modify, | |
1388 | .cbs.delete = lib_interface_description_delete, | |
8f90d89b | 1389 | .cbs.cli_show = cli_show_interface_desc, |
a4bed468 RW |
1390 | }, |
1391 | { | |
1392 | .xpath = NULL, | |
1393 | }, | |
1394 | } | |
1395 | }; |