]>
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 | |
eaf58ba9 | 42 | DEFINE_MTYPE_STATIC(LIB, IF, "Interface") |
d62a17ae | 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 | |
47b474b5 DD |
48 | static struct interface *if_lookup_by_ifindex(ifindex_t ifindex, |
49 | vrf_id_t vrf_id); | |
f4e14fdb | 50 | static int if_cmp_func(const struct interface *, const struct interface *); |
ff880b78 RW |
51 | static int if_cmp_index_func(const struct interface *ifp1, |
52 | const struct interface *ifp2); | |
f4e14fdb | 53 | RB_GENERATE(if_name_head, interface, name_entry, if_cmp_func); |
ff880b78 | 54 | RB_GENERATE(if_index_head, interface, index_entry, if_cmp_index_func); |
f4e14fdb | 55 | |
e80e7cce DL |
56 | DEFINE_QOBJ_TYPE(interface) |
57 | ||
996c9314 LB |
58 | DEFINE_HOOK(if_add, (struct interface * ifp), (ifp)) |
59 | DEFINE_KOOH(if_del, (struct interface * ifp), (ifp)) | |
ce19a04a | 60 | |
1b3e9a21 | 61 | static struct interface_master{ |
138c5a74 DS |
62 | int (*create_hook)(struct interface *ifp); |
63 | int (*up_hook)(struct interface *ifp); | |
64 | int (*down_hook)(struct interface *ifp); | |
65 | int (*destroy_hook)(struct interface *ifp); | |
66 | } ifp_master = { 0, }; | |
67 | ||
3a0391a9 | 68 | /* Compare interface names, returning an integer greater than, equal to, or |
69 | * less than 0, (following the strcmp convention), according to the | |
70 | * relationship between ifp1 and ifp2. Interface names consist of an | |
71 | * alphabetic prefix and a numeric suffix. The primary sort key is | |
72 | * lexicographic by name, and then numeric by number. No number sorts | |
73 | * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty < | |
74 | * devpty0, de0 < del0 | |
d62a17ae | 75 | */ |
36de6e0e | 76 | int if_cmp_name_func(const char *p1, const char *p2) |
106d2fd5 | 77 | { |
d62a17ae | 78 | unsigned int l1, l2; |
79 | long int x1, x2; | |
80 | int res; | |
81 | ||
82 | while (*p1 && *p2) { | |
83 | /* look up to any number */ | |
84 | l1 = strcspn(p1, "0123456789"); | |
85 | l2 = strcspn(p2, "0123456789"); | |
86 | ||
87 | /* name lengths are different -> compare names */ | |
88 | if (l1 != l2) | |
89 | return (strcmp(p1, p2)); | |
90 | ||
91 | /* Note that this relies on all numbers being less than all | |
92 | * letters, so | |
93 | * that de0 < del0. | |
94 | */ | |
95 | res = strncmp(p1, p2, l1); | |
96 | ||
97 | /* names are different -> compare them */ | |
98 | if (res) | |
99 | return res; | |
100 | ||
101 | /* with identical name part, go to numeric part */ | |
102 | p1 += l1; | |
103 | p2 += l1; | |
104 | ||
c9cbbb40 RW |
105 | if (!*p1 && !*p2) |
106 | return 0; | |
d62a17ae | 107 | if (!*p1) |
108 | return -1; | |
109 | if (!*p2) | |
110 | return 1; | |
111 | ||
36de6e0e A |
112 | x1 = strtol(p1, (char **)&p1, 10); |
113 | x2 = strtol(p2, (char **)&p2, 10); | |
d62a17ae | 114 | |
115 | /* let's compare numbers now */ | |
116 | if (x1 < x2) | |
117 | return -1; | |
118 | if (x1 > x2) | |
119 | return 1; | |
120 | ||
121 | /* numbers were equal, lets do it again.. | |
122 | (it happens with name like "eth123.456:789") */ | |
123 | } | |
124 | if (*p1) | |
125 | return 1; | |
126 | if (*p2) | |
127 | return -1; | |
128 | return 0; | |
106d2fd5 | 129 | } |
130 | ||
f4e14fdb RW |
131 | static int if_cmp_func(const struct interface *ifp1, |
132 | const struct interface *ifp2) | |
974fc9d2 | 133 | { |
36de6e0e | 134 | return if_cmp_name_func(ifp1->name, ifp2->name); |
974fc9d2 DS |
135 | } |
136 | ||
ff880b78 RW |
137 | static int if_cmp_index_func(const struct interface *ifp1, |
138 | const struct interface *ifp2) | |
139 | { | |
e33d7b6a QY |
140 | if (ifp1->ifindex == ifp2->ifindex) |
141 | return 0; | |
142 | else if (ifp1->ifindex > ifp2->ifindex) | |
143 | return 1; | |
144 | else | |
145 | return -1; | |
ff880b78 RW |
146 | } |
147 | ||
721c0857 DS |
148 | static void ifp_connected_free(void *arg) |
149 | { | |
150 | struct connected *c = arg; | |
151 | ||
152 | connected_free(&c); | |
153 | } | |
154 | ||
718e3744 | 155 | /* Create new interface structure. */ |
d5c65bf1 | 156 | static struct interface *if_new(vrf_id_t vrf_id) |
718e3744 | 157 | { |
d62a17ae | 158 | struct interface *ifp; |
d62a17ae | 159 | |
160 | ifp = XCALLOC(MTYPE_IF, sizeof(struct interface)); | |
ea7ec261 | 161 | |
d5c65bf1 SW |
162 | ifp->ifindex = IFINDEX_INTERNAL; |
163 | ifp->name[0] = '\0'; | |
ea7ec261 | 164 | |
d5c65bf1 | 165 | ifp->vrf_id = vrf_id; |
ea7ec261 | 166 | |
d62a17ae | 167 | ifp->connected = list_new(); |
721c0857 | 168 | ifp->connected->del = ifp_connected_free; |
d62a17ae | 169 | |
170 | ifp->nbr_connected = list_new(); | |
171 | ifp->nbr_connected->del = (void (*)(void *))nbr_connected_free; | |
172 | ||
173 | /* Enable Link-detection by default */ | |
174 | SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION); | |
175 | ||
176 | QOBJ_REG(ifp, interface); | |
d62a17ae | 177 | return ifp; |
718e3744 | 178 | } |
179 | ||
ef7bd2a3 DS |
180 | void if_new_via_zapi(struct interface *ifp) |
181 | { | |
182 | if (ifp_master.create_hook) | |
183 | (*ifp_master.create_hook)(ifp); | |
184 | } | |
185 | ||
3c3c3252 DS |
186 | void if_destroy_via_zapi(struct interface *ifp) |
187 | { | |
188 | if (ifp_master.destroy_hook) | |
189 | (*ifp_master.destroy_hook)(ifp); | |
190 | ||
191 | if_set_index(ifp, IFINDEX_INTERNAL); | |
26f8f6fe | 192 | if (!ifp->configured) |
f609709a | 193 | if_delete(&ifp); |
3c3c3252 DS |
194 | } |
195 | ||
ddbf3e60 DS |
196 | void if_up_via_zapi(struct interface *ifp) |
197 | { | |
198 | if (ifp_master.up_hook) | |
199 | (*ifp_master.up_hook)(ifp); | |
200 | } | |
201 | ||
b0b69e59 DS |
202 | void if_down_via_zapi(struct interface *ifp) |
203 | { | |
204 | if (ifp_master.down_hook) | |
205 | (*ifp_master.down_hook)(ifp); | |
206 | } | |
207 | ||
d5c65bf1 | 208 | struct interface *if_create_name(const char *name, vrf_id_t vrf_id) |
ea7ec261 | 209 | { |
d5c65bf1 SW |
210 | struct interface *ifp; |
211 | ||
212 | ifp = if_new(vrf_id); | |
213 | ||
214 | if_set_name(ifp, name); | |
215 | ||
216 | hook_call(if_add, ifp); | |
217 | return ifp; | |
ea7ec261 DD |
218 | } |
219 | ||
1f7a68a2 PG |
220 | struct interface *if_create_ifindex(ifindex_t ifindex, vrf_id_t vrf_id, |
221 | char *optional_name) | |
ea7ec261 | 222 | { |
d5c65bf1 SW |
223 | struct interface *ifp; |
224 | ||
225 | ifp = if_new(vrf_id); | |
226 | ||
227 | if_set_index(ifp, ifindex); | |
1f7a68a2 PG |
228 | if (optional_name) |
229 | if_set_name(ifp, optional_name); | |
d5c65bf1 SW |
230 | hook_call(if_add, ifp); |
231 | return ifp; | |
ea7ec261 DD |
232 | } |
233 | ||
a36898e7 DS |
234 | /* Create new interface structure. */ |
235 | void if_update_to_new_vrf(struct interface *ifp, vrf_id_t vrf_id) | |
f93e3f69 | 236 | { |
a36898e7 | 237 | struct vrf *old_vrf, *vrf; |
d62a17ae | 238 | |
239 | /* remove interface from old master vrf list */ | |
a36898e7 | 240 | old_vrf = vrf_lookup_by_id(ifp->vrf_id); |
8f90d89b | 241 | if (old_vrf) { |
9685b1e4 SW |
242 | if (ifp->name[0] != '\0') |
243 | IFNAME_RB_REMOVE(old_vrf, ifp); | |
244 | ||
ff880b78 | 245 | if (ifp->ifindex != IFINDEX_INTERNAL) |
8f90d89b | 246 | IFINDEX_RB_REMOVE(old_vrf, ifp); |
ff880b78 | 247 | } |
d62a17ae | 248 | |
a36898e7 DS |
249 | ifp->vrf_id = vrf_id; |
250 | vrf = vrf_get(ifp->vrf_id, NULL); | |
ff880b78 | 251 | |
9685b1e4 SW |
252 | if (ifp->name[0] != '\0') |
253 | IFNAME_RB_INSERT(vrf, ifp); | |
254 | ||
ff880b78 RW |
255 | if (ifp->ifindex != IFINDEX_INTERNAL) |
256 | IFINDEX_RB_INSERT(vrf, ifp); | |
8f90d89b RW |
257 | |
258 | /* | |
259 | * HACK: Change the interface VRF in the running configuration directly, | |
260 | * bypassing the northbound layer. This is necessary to avoid deleting | |
261 | * the interface and readding it in the new VRF, which would have | |
262 | * several implications. | |
263 | */ | |
264 | if (yang_module_find("frr-interface")) { | |
265 | struct lyd_node *if_dnode; | |
f7c20aa1 QY |
266 | char oldpath[XPATH_MAXLEN]; |
267 | char newpath[XPATH_MAXLEN]; | |
8f90d89b | 268 | |
8685be73 RW |
269 | if_dnode = yang_dnode_get( |
270 | running_config->dnode, | |
271 | "/frr-interface:lib/interface[name='%s'][vrf='%s']/vrf", | |
272 | ifp->name, old_vrf->name); | |
f7c20aa1 | 273 | |
8685be73 | 274 | if (if_dnode) { |
f7c20aa1 QY |
275 | yang_dnode_get_path(if_dnode->parent, oldpath, |
276 | sizeof(oldpath)); | |
8685be73 | 277 | yang_dnode_change_leaf(if_dnode, vrf->name); |
f7c20aa1 QY |
278 | yang_dnode_get_path(if_dnode->parent, newpath, |
279 | sizeof(newpath)); | |
280 | nb_running_move_tree(oldpath, newpath); | |
8685be73 | 281 | running_config->version++; |
8f90d89b RW |
282 | } |
283 | } | |
f93e3f69 DS |
284 | } |
285 | ||
8685be73 | 286 | |
d2fc8896 | 287 | /* Delete interface structure. */ |
d62a17ae | 288 | void if_delete_retain(struct interface *ifp) |
718e3744 | 289 | { |
996c9314 | 290 | hook_call(if_del, ifp); |
d62a17ae | 291 | QOBJ_UNREG(ifp); |
e80e7cce | 292 | |
d62a17ae | 293 | /* Free connected address list */ |
294 | list_delete_all_node(ifp->connected); | |
a80beece | 295 | |
d62a17ae | 296 | /* Free connected nbr address list */ |
297 | list_delete_all_node(ifp->nbr_connected); | |
d2fc8896 | 298 | } |
299 | ||
300 | /* Delete and free interface structure. */ | |
f609709a | 301 | void if_delete(struct interface **ifp) |
d2fc8896 | 302 | { |
f609709a | 303 | struct interface *ptr = *ifp; |
a36898e7 | 304 | struct vrf *vrf; |
5b8524f5 | 305 | |
f609709a | 306 | vrf = vrf_lookup_by_id(ptr->vrf_id); |
5b8524f5 | 307 | assert(vrf); |
f4e14fdb | 308 | |
f609709a DS |
309 | IFNAME_RB_REMOVE(vrf, ptr); |
310 | if (ptr->ifindex != IFINDEX_INTERNAL) | |
311 | IFINDEX_RB_REMOVE(vrf, ptr); | |
d2fc8896 | 312 | |
f609709a | 313 | if_delete_retain(ptr); |
718e3744 | 314 | |
f609709a DS |
315 | list_delete(&ptr->connected); |
316 | list_delete(&ptr->nbr_connected); | |
2dd04c5d | 317 | |
f609709a | 318 | if_link_params_free(ptr); |
16f1b9ee | 319 | |
f609709a | 320 | XFREE(MTYPE_TMP, ptr->desc); |
c7974c0f | 321 | |
f609709a DS |
322 | XFREE(MTYPE_IF, ptr); |
323 | *ifp = NULL; | |
718e3744 | 324 | } |
325 | ||
47b474b5 DD |
326 | /* Used only internally to check within VRF only */ |
327 | static struct interface *if_lookup_by_ifindex(ifindex_t ifindex, | |
328 | vrf_id_t vrf_id) | |
718e3744 | 329 | { |
5b8524f5 | 330 | struct vrf *vrf; |
ff880b78 | 331 | struct interface if_tmp; |
f4e14fdb | 332 | |
5b8524f5 RW |
333 | vrf = vrf_lookup_by_id(vrf_id); |
334 | if (!vrf) | |
335 | return NULL; | |
336 | ||
ff880b78 RW |
337 | if_tmp.ifindex = ifindex; |
338 | return RB_FIND(if_index_head, &vrf->ifaces_by_index, &if_tmp); | |
718e3744 | 339 | } |
340 | ||
47b474b5 DD |
341 | /* Interface existance check by index. */ |
342 | struct interface *if_lookup_by_index(ifindex_t ifindex, vrf_id_t vrf_id) | |
343 | { | |
344 | switch (vrf_get_backend()) { | |
345 | case VRF_BACKEND_UNKNOWN: | |
346 | case VRF_BACKEND_NETNS: | |
347 | return(if_lookup_by_ifindex(ifindex, vrf_id)); | |
348 | case VRF_BACKEND_VRF_LITE: | |
349 | return(if_lookup_by_index_all_vrf(ifindex)); | |
350 | } | |
351 | return NULL; | |
352 | } | |
353 | ||
d62a17ae | 354 | const char *ifindex2ifname(ifindex_t ifindex, vrf_id_t vrf_id) |
718e3744 | 355 | { |
d62a17ae | 356 | struct interface *ifp; |
718e3744 | 357 | |
d62a17ae | 358 | return ((ifp = if_lookup_by_index(ifindex, vrf_id)) != NULL) |
359 | ? ifp->name | |
360 | : "unknown"; | |
d2fc8896 | 361 | } |
362 | ||
d62a17ae | 363 | ifindex_t ifname2ifindex(const char *name, vrf_id_t vrf_id) |
d2fc8896 | 364 | { |
d62a17ae | 365 | struct interface *ifp; |
d2fc8896 | 366 | |
a36898e7 | 367 | return ((ifp = if_lookup_by_name(name, vrf_id)) != NULL) |
d62a17ae | 368 | ? ifp->ifindex |
369 | : IFINDEX_INTERNAL; | |
718e3744 | 370 | } |
371 | ||
372 | /* Interface existance check by interface name. */ | |
a36898e7 | 373 | struct interface *if_lookup_by_name(const char *name, vrf_id_t vrf_id) |
718e3744 | 374 | { |
a36898e7 | 375 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
f4e14fdb | 376 | struct interface if_tmp; |
d62a17ae | 377 | |
5b8524f5 RW |
378 | if (!vrf || !name |
379 | || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ) | |
bcc24579 | 380 | return NULL; |
f8962871 | 381 | |
f4e14fdb RW |
382 | strlcpy(if_tmp.name, name, sizeof(if_tmp.name)); |
383 | return RB_FIND(if_name_head, &vrf->ifaces_by_name, &if_tmp); | |
f8962871 DS |
384 | } |
385 | ||
bcc24579 | 386 | struct interface *if_lookup_by_name_all_vrf(const char *name) |
a349198f | 387 | { |
bcc24579 | 388 | struct vrf *vrf; |
d62a17ae | 389 | struct interface *ifp; |
a349198f | 390 | |
bcc24579 | 391 | if (!name || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ) |
d62a17ae | 392 | return NULL; |
a349198f | 393 | |
bcc24579 | 394 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
a36898e7 | 395 | ifp = if_lookup_by_name(name, vrf->vrf_id); |
bcc24579 | 396 | if (ifp) |
d62a17ae | 397 | return ifp; |
398 | } | |
bcc24579 | 399 | |
d62a17ae | 400 | return NULL; |
a349198f | 401 | } |
402 | ||
ea7ec261 DD |
403 | struct interface *if_lookup_by_index_all_vrf(ifindex_t ifindex) |
404 | { | |
405 | struct vrf *vrf; | |
406 | struct interface *ifp; | |
407 | ||
408 | if (ifindex == IFINDEX_INTERNAL) | |
409 | return NULL; | |
410 | ||
411 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { | |
47b474b5 | 412 | ifp = if_lookup_by_ifindex(ifindex, vrf->vrf_id); |
ea7ec261 DD |
413 | if (ifp) |
414 | return ifp; | |
415 | } | |
416 | ||
417 | return NULL; | |
418 | } | |
419 | ||
3923b6e3 | 420 | /* Lookup interface by IP address. */ |
0b700537 | 421 | struct interface *if_lookup_exact_address(const void *src, int family, |
d62a17ae | 422 | vrf_id_t vrf_id) |
718e3744 | 423 | { |
f4e14fdb | 424 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
d62a17ae | 425 | struct listnode *cnode; |
426 | struct interface *ifp; | |
427 | struct prefix *p; | |
428 | struct connected *c; | |
429 | ||
451fda4f | 430 | FOR_ALL_INTERFACES (vrf, ifp) { |
d62a17ae | 431 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) { |
432 | p = c->address; | |
433 | ||
434 | if (p && (p->family == family)) { | |
435 | if (family == AF_INET) { | |
436 | if (IPV4_ADDR_SAME( | |
437 | &p->u.prefix4, | |
438 | (struct in_addr *)src)) | |
439 | return ifp; | |
440 | } else if (family == AF_INET6) { | |
441 | if (IPV6_ADDR_SAME( | |
442 | &p->u.prefix6, | |
443 | (struct in6_addr *)src)) | |
444 | return ifp; | |
445 | } | |
446 | } | |
0aabccc0 | 447 | } |
718e3744 | 448 | } |
d62a17ae | 449 | return NULL; |
718e3744 | 450 | } |
451 | ||
3923b6e3 | 452 | /* Lookup interface by IP address. */ |
0b700537 | 453 | struct connected *if_lookup_address(const void *matchaddr, int family, |
d62a17ae | 454 | vrf_id_t vrf_id) |
718e3744 | 455 | { |
f4e14fdb | 456 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
d62a17ae | 457 | struct prefix addr; |
458 | int bestlen = 0; | |
459 | struct listnode *cnode; | |
460 | struct interface *ifp; | |
461 | struct connected *c; | |
462 | struct connected *match; | |
463 | ||
464 | if (family == AF_INET) { | |
465 | addr.family = AF_INET; | |
466 | addr.u.prefix4 = *((struct in_addr *)matchaddr); | |
467 | addr.prefixlen = IPV4_MAX_BITLEN; | |
468 | } else if (family == AF_INET6) { | |
469 | addr.family = AF_INET6; | |
470 | addr.u.prefix6 = *((struct in6_addr *)matchaddr); | |
471 | addr.prefixlen = IPV6_MAX_BITLEN; | |
472 | } | |
718e3744 | 473 | |
d62a17ae | 474 | match = NULL; |
718e3744 | 475 | |
451fda4f | 476 | FOR_ALL_INTERFACES (vrf, ifp) { |
d62a17ae | 477 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) { |
478 | if (c->address && (c->address->family == AF_INET) | |
479 | && prefix_match(CONNECTED_PREFIX(c), &addr) | |
480 | && (c->address->prefixlen > bestlen)) { | |
481 | bestlen = c->address->prefixlen; | |
482 | match = c; | |
483 | } | |
484 | } | |
718e3744 | 485 | } |
d62a17ae | 486 | return match; |
718e3744 | 487 | } |
488 | ||
b81e97a8 | 489 | /* Lookup interface by prefix */ |
0b700537 | 490 | struct interface *if_lookup_prefix(const struct prefix *prefix, vrf_id_t vrf_id) |
b81e97a8 | 491 | { |
f4e14fdb | 492 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
d62a17ae | 493 | struct listnode *cnode; |
494 | struct interface *ifp; | |
495 | struct connected *c; | |
496 | ||
451fda4f | 497 | FOR_ALL_INTERFACES (vrf, ifp) { |
d62a17ae | 498 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) { |
499 | if (prefix_cmp(c->address, prefix) == 0) { | |
500 | return ifp; | |
501 | } | |
502 | } | |
503 | } | |
504 | return NULL; | |
b81e97a8 DD |
505 | } |
506 | ||
dad18a2f QY |
507 | size_t if_lookup_by_hwaddr(const uint8_t *hw_addr, size_t addrsz, |
508 | struct interface ***result, vrf_id_t vrf_id) | |
509 | { | |
510 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); | |
511 | ||
512 | struct list *rs = list_new(); | |
513 | struct interface *ifp; | |
514 | ||
515 | FOR_ALL_INTERFACES (vrf, ifp) { | |
516 | if (ifp->hw_addr_len == (int)addrsz | |
517 | && !memcmp(hw_addr, ifp->hw_addr, addrsz)) | |
518 | listnode_add(rs, ifp); | |
519 | } | |
520 | ||
521 | if (rs->count) { | |
522 | *result = XCALLOC(MTYPE_TMP, | |
523 | sizeof(struct interface *) * rs->count); | |
524 | list_to_array(rs, (void **)*result, rs->count); | |
525 | } | |
526 | ||
527 | int count = rs->count; | |
528 | ||
529 | list_delete(&rs); | |
530 | ||
531 | return count; | |
532 | } | |
533 | ||
534 | ||
718e3744 | 535 | /* Get interface by name if given name interface doesn't exist create |
536 | one. */ | |
a36898e7 | 537 | struct interface *if_get_by_name(const char *name, vrf_id_t vrf_id) |
718e3744 | 538 | { |
8f90d89b | 539 | struct interface *ifp; |
718e3744 | 540 | |
8f90d89b | 541 | switch (vrf_get_backend()) { |
b0b97a7f | 542 | case VRF_BACKEND_UNKNOWN: |
8f90d89b | 543 | case VRF_BACKEND_NETNS: |
a36898e7 | 544 | ifp = if_lookup_by_name(name, vrf_id); |
ee2f2c23 TC |
545 | if (ifp) |
546 | return ifp; | |
d5c65bf1 | 547 | return if_create_name(name, vrf_id); |
8f90d89b RW |
548 | case VRF_BACKEND_VRF_LITE: |
549 | ifp = if_lookup_by_name_all_vrf(name); | |
550 | if (ifp) { | |
a36898e7 | 551 | if (ifp->vrf_id == vrf_id) |
379eb245 | 552 | return ifp; |
8f90d89b RW |
553 | /* If it came from the kernel or by way of zclient, |
554 | * believe it and update the ifp accordingly. | |
555 | */ | |
a36898e7 | 556 | if_update_to_new_vrf(ifp, vrf_id); |
8f90d89b | 557 | return ifp; |
ee2f2c23 | 558 | } |
d5c65bf1 | 559 | return if_create_name(name, vrf_id); |
85f9da7f | 560 | } |
8f90d89b RW |
561 | |
562 | return NULL; | |
8736158a FL |
563 | } |
564 | ||
1f7a68a2 PG |
565 | struct interface *if_get_by_ifindex(ifindex_t ifindex, vrf_id_t vrf_id, |
566 | char *optional_name) | |
ea7ec261 DD |
567 | { |
568 | struct interface *ifp; | |
569 | ||
570 | switch (vrf_get_backend()) { | |
571 | case VRF_BACKEND_UNKNOWN: | |
572 | case VRF_BACKEND_NETNS: | |
47b474b5 | 573 | ifp = if_lookup_by_ifindex(ifindex, vrf_id); |
ea7ec261 DD |
574 | if (ifp) |
575 | return ifp; | |
1f7a68a2 | 576 | return if_create_ifindex(ifindex, vrf_id, optional_name); |
ea7ec261 DD |
577 | case VRF_BACKEND_VRF_LITE: |
578 | ifp = if_lookup_by_index_all_vrf(ifindex); | |
579 | if (ifp) { | |
580 | if (ifp->vrf_id == vrf_id) | |
581 | return ifp; | |
582 | /* If it came from the kernel or by way of zclient, | |
583 | * believe it and update the ifp accordingly. | |
584 | */ | |
585 | if_update_to_new_vrf(ifp, vrf_id); | |
586 | return ifp; | |
587 | } | |
1f7a68a2 | 588 | return if_create_ifindex(ifindex, vrf_id, optional_name); |
ea7ec261 DD |
589 | } |
590 | ||
591 | return NULL; | |
592 | } | |
593 | ||
67f81586 | 594 | int if_set_index(struct interface *ifp, ifindex_t ifindex) |
ff880b78 | 595 | { |
5b8524f5 RW |
596 | struct vrf *vrf; |
597 | ||
67f81586 QY |
598 | if (ifp->ifindex == ifindex) |
599 | return 0; | |
600 | ||
d5c65bf1 | 601 | vrf = vrf_get(ifp->vrf_id, NULL); |
5b8524f5 | 602 | assert(vrf); |
ff880b78 | 603 | |
67f81586 QY |
604 | /* |
605 | * If there is already an interface with this ifindex, we will collide | |
606 | * on insertion, so don't even try. | |
607 | */ | |
608 | if (if_lookup_by_ifindex(ifindex, ifp->vrf_id)) | |
609 | return -1; | |
ff880b78 RW |
610 | |
611 | if (ifp->ifindex != IFINDEX_INTERNAL) | |
d5c65bf1 | 612 | IFINDEX_RB_REMOVE(vrf, ifp); |
ff880b78 RW |
613 | |
614 | ifp->ifindex = ifindex; | |
615 | ||
67f81586 QY |
616 | if (ifp->ifindex != IFINDEX_INTERNAL) { |
617 | /* | |
618 | * This should never happen, since we checked if there was | |
619 | * already an interface with the desired ifindex at the top of | |
620 | * the function. Nevertheless. | |
621 | */ | |
622 | if (IFINDEX_RB_INSERT(vrf, ifp)) | |
623 | return -1; | |
624 | } | |
625 | ||
626 | return 0; | |
ff880b78 RW |
627 | } |
628 | ||
d5c65bf1 SW |
629 | void if_set_name(struct interface *ifp, const char *name) |
630 | { | |
631 | struct vrf *vrf; | |
632 | ||
633 | vrf = vrf_get(ifp->vrf_id, NULL); | |
634 | assert(vrf); | |
635 | ||
636 | if (if_cmp_name_func(ifp->name, name) == 0) | |
637 | return; | |
638 | ||
639 | if (ifp->name[0] != '\0') | |
640 | IFNAME_RB_REMOVE(vrf, ifp); | |
641 | ||
642 | strlcpy(ifp->name, name, sizeof(ifp->name)); | |
643 | ||
644 | if (ifp->name[0] != '\0') | |
645 | IFNAME_RB_INSERT(vrf, ifp); | |
646 | } | |
647 | ||
718e3744 | 648 | /* Does interface up ? */ |
6339042c | 649 | int if_is_up(const struct interface *ifp) |
718e3744 | 650 | { |
d62a17ae | 651 | return ifp->flags & IFF_UP; |
718e3744 | 652 | } |
653 | ||
2e3b2e47 | 654 | /* Is interface running? */ |
6339042c | 655 | int if_is_running(const struct interface *ifp) |
2e3b2e47 | 656 | { |
d62a17ae | 657 | return ifp->flags & IFF_RUNNING; |
2e3b2e47 | 658 | } |
659 | ||
660 | /* Is the interface operative, eg. either UP & RUNNING | |
244c1cdc DS |
661 | or UP & !ZEBRA_INTERFACE_LINK_DETECTION and |
662 | if ptm checking is enabled, then ptm check has passed */ | |
6339042c | 663 | int if_is_operative(const struct interface *ifp) |
2e3b2e47 | 664 | { |
d62a17ae | 665 | return ((ifp->flags & IFF_UP) |
666 | && (((ifp->flags & IFF_RUNNING) | |
667 | && (ifp->ptm_status || !ifp->ptm_enable)) | |
668 | || !CHECK_FLAG(ifp->status, | |
669 | ZEBRA_INTERFACE_LINKDETECTION))); | |
244c1cdc DS |
670 | } |
671 | ||
672 | /* Is the interface operative, eg. either UP & RUNNING | |
673 | or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */ | |
6339042c | 674 | int if_is_no_ptm_operative(const struct interface *ifp) |
244c1cdc | 675 | { |
d62a17ae | 676 | return ((ifp->flags & IFF_UP) |
677 | && ((ifp->flags & IFF_RUNNING) | |
678 | || !CHECK_FLAG(ifp->status, | |
679 | ZEBRA_INTERFACE_LINKDETECTION))); | |
2e3b2e47 | 680 | } |
681 | ||
718e3744 | 682 | /* Is this loopback interface ? */ |
6339042c | 683 | int if_is_loopback(const struct interface *ifp) |
718e3744 | 684 | { |
d62a17ae | 685 | /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M |
686 | * but Y on platform N? | |
687 | */ | |
688 | return (ifp->flags & (IFF_LOOPBACK | IFF_NOXMIT | IFF_VIRTUAL)); | |
718e3744 | 689 | } |
690 | ||
0c74bbe0 | 691 | /* Check interface is VRF */ |
6339042c | 692 | int if_is_vrf(const struct interface *ifp) |
0c74bbe0 CS |
693 | { |
694 | return CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
695 | } | |
696 | ||
6339042c | 697 | bool if_is_loopback_or_vrf(const struct interface *ifp) |
fec4ca19 DS |
698 | { |
699 | if (if_is_loopback(ifp) || if_is_vrf(ifp)) | |
700 | return true; | |
701 | ||
702 | return false; | |
703 | } | |
704 | ||
718e3744 | 705 | /* Does this interface support broadcast ? */ |
6339042c | 706 | int if_is_broadcast(const struct interface *ifp) |
718e3744 | 707 | { |
d62a17ae | 708 | return ifp->flags & IFF_BROADCAST; |
718e3744 | 709 | } |
710 | ||
711 | /* Does this interface support broadcast ? */ | |
6339042c | 712 | int if_is_pointopoint(const struct interface *ifp) |
718e3744 | 713 | { |
d62a17ae | 714 | return ifp->flags & IFF_POINTOPOINT; |
718e3744 | 715 | } |
716 | ||
717 | /* Does this interface support multicast ? */ | |
6339042c | 718 | int if_is_multicast(const struct interface *ifp) |
718e3744 | 719 | { |
d62a17ae | 720 | return ifp->flags & IFF_MULTICAST; |
718e3744 | 721 | } |
722 | ||
723 | /* Printout flag information into log */ | |
d62a17ae | 724 | const char *if_flag_dump(unsigned long flag) |
718e3744 | 725 | { |
d62a17ae | 726 | int separator = 0; |
727 | static char logbuf[BUFSIZ]; | |
728 | ||
729 | #define IFF_OUT_LOG(X, STR) \ | |
730 | if (flag & (X)) { \ | |
731 | if (separator) \ | |
3393df5c | 732 | strlcat(logbuf, ",", sizeof(logbuf)); \ |
d62a17ae | 733 | else \ |
734 | separator = 1; \ | |
3393df5c | 735 | strlcat(logbuf, STR, sizeof(logbuf)); \ |
d62a17ae | 736 | } |
718e3744 | 737 | |
d62a17ae | 738 | strlcpy(logbuf, "<", BUFSIZ); |
739 | IFF_OUT_LOG(IFF_UP, "UP"); | |
740 | IFF_OUT_LOG(IFF_BROADCAST, "BROADCAST"); | |
741 | IFF_OUT_LOG(IFF_DEBUG, "DEBUG"); | |
742 | IFF_OUT_LOG(IFF_LOOPBACK, "LOOPBACK"); | |
743 | IFF_OUT_LOG(IFF_POINTOPOINT, "POINTOPOINT"); | |
744 | IFF_OUT_LOG(IFF_NOTRAILERS, "NOTRAILERS"); | |
745 | IFF_OUT_LOG(IFF_RUNNING, "RUNNING"); | |
746 | IFF_OUT_LOG(IFF_NOARP, "NOARP"); | |
747 | IFF_OUT_LOG(IFF_PROMISC, "PROMISC"); | |
748 | IFF_OUT_LOG(IFF_ALLMULTI, "ALLMULTI"); | |
749 | IFF_OUT_LOG(IFF_OACTIVE, "OACTIVE"); | |
750 | IFF_OUT_LOG(IFF_SIMPLEX, "SIMPLEX"); | |
751 | IFF_OUT_LOG(IFF_LINK0, "LINK0"); | |
752 | IFF_OUT_LOG(IFF_LINK1, "LINK1"); | |
753 | IFF_OUT_LOG(IFF_LINK2, "LINK2"); | |
754 | IFF_OUT_LOG(IFF_MULTICAST, "MULTICAST"); | |
755 | IFF_OUT_LOG(IFF_NOXMIT, "NOXMIT"); | |
756 | IFF_OUT_LOG(IFF_NORTEXCH, "NORTEXCH"); | |
757 | IFF_OUT_LOG(IFF_VIRTUAL, "VIRTUAL"); | |
758 | IFF_OUT_LOG(IFF_IPV4, "IPv4"); | |
759 | IFF_OUT_LOG(IFF_IPV6, "IPv6"); | |
760 | ||
3393df5c | 761 | strlcat(logbuf, ">", sizeof(logbuf)); |
d62a17ae | 762 | |
763 | return logbuf; | |
630c97ce | 764 | #undef IFF_OUT_LOG |
718e3744 | 765 | } |
766 | ||
767 | /* For debugging */ | |
d62a17ae | 768 | static void if_dump(const struct interface *ifp) |
718e3744 | 769 | { |
d62a17ae | 770 | struct listnode *node; |
771 | struct connected *c __attribute__((unused)); | |
772 | ||
a94fbcca DS |
773 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, c)) { |
774 | struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id); | |
775 | ||
d62a17ae | 776 | zlog_info( |
a94fbcca | 777 | "Interface %s vrf %s(%u) index %d metric %d mtu %d " |
d62a17ae | 778 | "mtu6 %d %s", |
a94fbcca DS |
779 | ifp->name, VRF_LOGNAME(vrf), ifp->vrf_id, ifp->ifindex, |
780 | ifp->metric, ifp->mtu, ifp->mtu6, | |
781 | if_flag_dump(ifp->flags)); | |
782 | } | |
718e3744 | 783 | } |
784 | ||
785 | /* Interface printing for all interface. */ | |
d62a17ae | 786 | void if_dump_all(void) |
718e3744 | 787 | { |
d62a17ae | 788 | struct vrf *vrf; |
f4e14fdb | 789 | void *ifp; |
d62a17ae | 790 | |
a2addae8 | 791 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) |
451fda4f | 792 | FOR_ALL_INTERFACES (vrf, ifp) |
f4e14fdb | 793 | if_dump(ifp); |
718e3744 | 794 | } |
795 | ||
98954844 PJ |
796 | #ifdef SUNOS_5 |
797 | /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created | |
798 | * a seperate struct interface for each logical interface, so config | |
799 | * file may be full of 'interface fooX:Y'. Solaris however does not | |
800 | * expose logical interfaces via PF_ROUTE, so trying to track logical | |
801 | * interfaces can be fruitless, for that reason Quagga only tracks | |
802 | * the primary IP interface. | |
803 | * | |
804 | * We try accomodate SUNWzebra by: | |
805 | * - looking up the interface name, to see whether it exists, if so | |
806 | * its useable | |
807 | * - for protocol daemons, this could only because zebra told us of | |
808 | * the interface | |
809 | * - for zebra, only because it learnt from kernel | |
810 | * - if not: | |
811 | * - search the name to see if it contains a sub-ipif / logical interface | |
812 | * seperator, the ':' char. If it does: | |
813 | * - text up to that char must be the primary name - get that name. | |
814 | * if not: | |
815 | * - no idea, just get the name in its entirety. | |
816 | */ | |
a36898e7 | 817 | static struct interface *if_sunwzebra_get(const char *name, vrf_id_t vrf_id) |
98954844 | 818 | { |
d62a17ae | 819 | struct interface *ifp; |
bcc24579 | 820 | char *cp; |
d62a17ae | 821 | |
a36898e7 | 822 | if ((ifp = if_lookup_by_name(name, vrf_id)) != NULL) |
d62a17ae | 823 | return ifp; |
824 | ||
825 | /* hunt the primary interface name... */ | |
bcc24579 RW |
826 | cp = strchr(name, ':'); |
827 | if (cp) | |
828 | *cp = '\0'; | |
d62a17ae | 829 | |
a36898e7 | 830 | return if_get_by_name(name, vrf_id); |
98954844 PJ |
831 | } |
832 | #endif /* SUNOS_5 */ | |
6b0655a2 | 833 | |
d7d73ffc | 834 | #if 0 |
718e3744 | 835 | /* For debug purpose. */ |
836 | DEFUN (show_address, | |
837 | show_address_cmd, | |
199d90a1 | 838 | "show address [vrf NAME]", |
718e3744 | 839 | SHOW_STR |
fcbee690 QY |
840 | "address\n" |
841 | VRF_CMD_HELP_STR) | |
718e3744 | 842 | { |
ac2914d3 DS |
843 | int idx_vrf = 3; |
844 | struct listnode *node; | |
845 | struct interface *ifp; | |
846 | struct connected *ifc; | |
847 | struct prefix *p; | |
848 | vrf_id_t vrf_id = VRF_DEFAULT; | |
849 | ||
850 | if (argc > 2) | |
851 | VRF_GET_ID (vrf_id, argv[idx_vrf]->arg); | |
718e3744 | 852 | |
ac2914d3 DS |
853 | FOR_ALL_INTERFACES (vrf, ifp) { |
854 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc)) { | |
855 | p = ifc->address; | |
856 | ||
857 | if (p->family == AF_INET) | |
858 | vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen); | |
859 | } | |
718e3744 | 860 | } |
ac2914d3 | 861 | return CMD_SUCCESS; |
718e3744 | 862 | } |
863 | ||
8736158a FL |
864 | DEFUN (show_address_vrf_all, |
865 | show_address_vrf_all_cmd, | |
9ccf14f7 | 866 | "show address vrf all", |
8736158a FL |
867 | SHOW_STR |
868 | "address\n" | |
869 | VRF_ALL_CMD_HELP_STR) | |
870 | { | |
ac2914d3 DS |
871 | struct vrf *vrf; |
872 | struct listnode *node; | |
873 | struct interface *ifp; | |
874 | struct connected *ifc; | |
875 | struct prefix *p; | |
876 | ||
877 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) | |
878 | { | |
879 | if (RB_EMPTY (if_name_head, &vrf->ifaces_by_name)) | |
880 | continue; | |
881 | ||
a94fbcca DS |
882 | vty_out (vty, "\nVRF %s(%u)\n\n", |
883 | VRF_LOGNAME(vrf), vrf->vrf_id); | |
ac2914d3 DS |
884 | |
885 | FOR_ALL_INTERFACES (vrf, ifp) { | |
886 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc)) { | |
887 | p = ifc->address; | |
888 | ||
889 | if (p->family == AF_INET) | |
890 | vty_out (vty, "%s/%d\n", inet_ntoa (p->u.prefix4), p->prefixlen); | |
891 | } | |
892 | } | |
893 | } | |
894 | return CMD_SUCCESS; | |
8736158a | 895 | } |
d7d73ffc | 896 | #endif |
8736158a | 897 | |
718e3744 | 898 | /* Allocate connected structure. */ |
d62a17ae | 899 | struct connected *connected_new(void) |
718e3744 | 900 | { |
d62a17ae | 901 | return XCALLOC(MTYPE_CONNECTED, sizeof(struct connected)); |
718e3744 | 902 | } |
903 | ||
a80beece | 904 | /* Allocate nbr connected structure. */ |
d62a17ae | 905 | struct nbr_connected *nbr_connected_new(void) |
a80beece | 906 | { |
d62a17ae | 907 | return XCALLOC(MTYPE_NBR_CONNECTED, sizeof(struct nbr_connected)); |
a80beece DS |
908 | } |
909 | ||
718e3744 | 910 | /* Free connected structure. */ |
721c0857 | 911 | void connected_free(struct connected **connected) |
718e3744 | 912 | { |
721c0857 DS |
913 | struct connected *ptr = *connected; |
914 | ||
8fa77bc6 DA |
915 | prefix_free(&ptr->address); |
916 | prefix_free(&ptr->destination); | |
718e3744 | 917 | |
721c0857 | 918 | XFREE(MTYPE_CONNECTED_LABEL, ptr->label); |
718e3744 | 919 | |
721c0857 DS |
920 | XFREE(MTYPE_CONNECTED, ptr); |
921 | *connected = NULL; | |
718e3744 | 922 | } |
923 | ||
a80beece | 924 | /* Free nbr connected structure. */ |
d62a17ae | 925 | void nbr_connected_free(struct nbr_connected *connected) |
a80beece | 926 | { |
d62a17ae | 927 | if (connected->address) |
63265b5c | 928 | prefix_free(&connected->address); |
a80beece | 929 | |
d62a17ae | 930 | XFREE(MTYPE_NBR_CONNECTED, connected); |
a80beece DS |
931 | } |
932 | ||
933 | /* If same interface nbr address already exists... */ | |
d62a17ae | 934 | struct nbr_connected *nbr_connected_check(struct interface *ifp, |
935 | struct prefix *p) | |
a80beece | 936 | { |
d62a17ae | 937 | struct nbr_connected *ifc; |
938 | struct listnode *node; | |
a80beece | 939 | |
d62a17ae | 940 | for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, ifc)) |
941 | if (prefix_same(ifc->address, p)) | |
942 | return ifc; | |
a80beece | 943 | |
d62a17ae | 944 | return NULL; |
a80beece DS |
945 | } |
946 | ||
718e3744 | 947 | /* Print if_addr structure. */ |
d62a17ae | 948 | static void __attribute__((unused)) |
949 | connected_log(struct connected *connected, char *str) | |
718e3744 | 950 | { |
d62a17ae | 951 | struct prefix *p; |
952 | struct interface *ifp; | |
a94fbcca | 953 | struct vrf *vrf; |
d62a17ae | 954 | char logbuf[BUFSIZ]; |
955 | char buf[BUFSIZ]; | |
956 | ||
957 | ifp = connected->ifp; | |
958 | p = connected->address; | |
959 | ||
a94fbcca | 960 | vrf = vrf_lookup_by_id(ifp->vrf_id); |
772270f3 QY |
961 | snprintf(logbuf, sizeof(logbuf), "%s interface %s vrf %s(%u) %s %s/%d ", |
962 | str, ifp->name, VRF_LOGNAME(vrf), ifp->vrf_id, | |
963 | prefix_family_str(p), | |
d62a17ae | 964 | inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen); |
965 | ||
966 | p = connected->destination; | |
967 | if (p) { | |
968 | strncat(logbuf, inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), | |
969 | BUFSIZ - strlen(logbuf)); | |
970 | } | |
971 | zlog_info("%s", logbuf); | |
718e3744 | 972 | } |
973 | ||
a80beece | 974 | /* Print if_addr structure. */ |
d62a17ae | 975 | static void __attribute__((unused)) |
976 | nbr_connected_log(struct nbr_connected *connected, char *str) | |
a80beece | 977 | { |
d62a17ae | 978 | struct prefix *p; |
979 | struct interface *ifp; | |
980 | char logbuf[BUFSIZ]; | |
981 | char buf[BUFSIZ]; | |
a80beece | 982 | |
d62a17ae | 983 | ifp = connected->ifp; |
984 | p = connected->address; | |
a80beece | 985 | |
772270f3 QY |
986 | snprintf(logbuf, sizeof(logbuf), "%s interface %s %s %s/%d ", str, |
987 | ifp->name, prefix_family_str(p), | |
d62a17ae | 988 | inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen); |
a80beece | 989 | |
d62a17ae | 990 | zlog_info("%s", logbuf); |
a80beece DS |
991 | } |
992 | ||
718e3744 | 993 | /* If two connected address has same prefix return 1. */ |
0b700537 RW |
994 | static int connected_same_prefix(const struct prefix *p1, |
995 | const struct prefix *p2) | |
718e3744 | 996 | { |
d62a17ae | 997 | if (p1->family == p2->family) { |
998 | if (p1->family == AF_INET | |
999 | && IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4)) | |
1000 | return 1; | |
1001 | if (p1->family == AF_INET6 | |
1002 | && IPV6_ADDR_SAME(&p1->u.prefix6, &p2->u.prefix6)) | |
1003 | return 1; | |
1004 | } | |
1005 | return 0; | |
718e3744 | 1006 | } |
1007 | ||
457ea8d4 GN |
1008 | /* count the number of connected addresses that are in the given family */ |
1009 | unsigned int connected_count_by_family(struct interface *ifp, int family) | |
1010 | { | |
1011 | struct listnode *cnode; | |
1012 | struct connected *connected; | |
1013 | unsigned int cnt = 0; | |
1014 | ||
1015 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) | |
1016 | if (connected->address->family == family) | |
1017 | cnt++; | |
1018 | ||
1019 | return cnt; | |
1020 | } | |
1021 | ||
d62a17ae | 1022 | struct connected *connected_lookup_prefix_exact(struct interface *ifp, |
0b700537 | 1023 | const struct prefix *p) |
38485402 | 1024 | { |
d62a17ae | 1025 | struct listnode *node; |
1026 | struct listnode *next; | |
1027 | struct connected *ifc; | |
38485402 | 1028 | |
d62a17ae | 1029 | for (node = listhead(ifp->connected); node; node = next) { |
1030 | ifc = listgetdata(node); | |
1031 | next = node->next; | |
38485402 | 1032 | |
d62a17ae | 1033 | if (connected_same_prefix(ifc->address, p)) |
1034 | return ifc; | |
1035 | } | |
1036 | return NULL; | |
38485402 DS |
1037 | } |
1038 | ||
d62a17ae | 1039 | struct connected *connected_delete_by_prefix(struct interface *ifp, |
1040 | struct prefix *p) | |
718e3744 | 1041 | { |
d62a17ae | 1042 | struct listnode *node; |
1043 | struct listnode *next; | |
1044 | struct connected *ifc; | |
1045 | ||
1046 | /* In case of same prefix come, replace it with new one. */ | |
1047 | for (node = listhead(ifp->connected); node; node = next) { | |
1048 | ifc = listgetdata(node); | |
1049 | next = node->next; | |
1050 | ||
1051 | if (connected_same_prefix(ifc->address, p)) { | |
1052 | listnode_delete(ifp->connected, ifc); | |
1053 | return ifc; | |
1054 | } | |
718e3744 | 1055 | } |
d62a17ae | 1056 | return NULL; |
718e3744 | 1057 | } |
1058 | ||
bd40c341 | 1059 | /* Find the address on our side that will be used when packets |
727d104b | 1060 | are sent to dst. */ |
d62a17ae | 1061 | struct connected *connected_lookup_prefix(struct interface *ifp, |
0b700537 | 1062 | const struct prefix *addr) |
727d104b | 1063 | { |
d62a17ae | 1064 | struct listnode *cnode; |
1065 | struct connected *c; | |
1066 | struct connected *match; | |
1067 | ||
1068 | match = NULL; | |
1069 | ||
1070 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) { | |
1071 | if (c->address && (c->address->family == addr->family) | |
1072 | && prefix_match(CONNECTED_PREFIX(c), addr) | |
1073 | && (!match | |
1074 | || (c->address->prefixlen > match->address->prefixlen))) | |
1075 | match = c; | |
1076 | } | |
1077 | return match; | |
727d104b | 1078 | } |
1079 | ||
d62a17ae | 1080 | struct connected *connected_add_by_prefix(struct interface *ifp, |
1081 | struct prefix *p, | |
1082 | struct prefix *destination) | |
4a7aac1b | 1083 | { |
d62a17ae | 1084 | struct connected *ifc; |
4a7aac1b | 1085 | |
d62a17ae | 1086 | /* Allocate new connected address. */ |
1087 | ifc = connected_new(); | |
1088 | ifc->ifp = ifp; | |
4a7aac1b | 1089 | |
d62a17ae | 1090 | /* Fetch interface address */ |
1091 | ifc->address = prefix_new(); | |
1092 | memcpy(ifc->address, p, sizeof(struct prefix)); | |
4a7aac1b | 1093 | |
d62a17ae | 1094 | /* Fetch dest address */ |
1095 | if (destination) { | |
1096 | ifc->destination = prefix_new(); | |
1097 | memcpy(ifc->destination, destination, sizeof(struct prefix)); | |
1098 | } | |
4a7aac1b | 1099 | |
d62a17ae | 1100 | /* Add connected address to the interface. */ |
1101 | listnode_add(ifp->connected, ifc); | |
1102 | return ifc; | |
4a7aac1b | 1103 | } |
1104 | ||
667179ca QY |
1105 | struct connected *connected_get_linklocal(struct interface *ifp) |
1106 | { | |
1107 | struct listnode *n; | |
1108 | struct connected *c = NULL; | |
1109 | ||
1110 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, n, c)) { | |
1111 | if (c->address->family == AF_INET6 | |
1112 | && IN6_IS_ADDR_LINKLOCAL(&c->address->u.prefix6)) | |
1113 | break; | |
1114 | } | |
1115 | return c; | |
1116 | } | |
1117 | ||
d62a17ae | 1118 | #if 0 /* this route_table of struct connected's is unused \ |
1119 | * however, it would be good to use a route_table rather than \ | |
1120 | * a list.. \ | |
1121 | */ | |
718e3744 | 1122 | /* Interface looking up by interface's address. */ |
718e3744 | 1123 | /* Interface's IPv4 address reverse lookup table. */ |
1124 | struct route_table *ifaddr_ipv4_table; | |
1125 | /* struct route_table *ifaddr_ipv6_table; */ | |
1126 | ||
8cc4198f | 1127 | static void |
718e3744 | 1128 | ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp) |
1129 | { | |
1130 | struct route_node *rn; | |
1131 | struct prefix_ipv4 p; | |
1132 | ||
1133 | p.family = AF_INET; | |
1134 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
1135 | p.prefix = *ifaddr; | |
1136 | ||
1137 | rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p); | |
1138 | if (rn) | |
1139 | { | |
1140 | route_unlock_node (rn); | |
1141 | zlog_info ("ifaddr_ipv4_add(): address %s is already added", | |
1142 | inet_ntoa (*ifaddr)); | |
1143 | return; | |
1144 | } | |
1145 | rn->info = ifp; | |
1146 | } | |
1147 | ||
8cc4198f | 1148 | static void |
718e3744 | 1149 | ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp) |
1150 | { | |
1151 | struct route_node *rn; | |
1152 | struct prefix_ipv4 p; | |
1153 | ||
1154 | p.family = AF_INET; | |
1155 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
1156 | p.prefix = *ifaddr; | |
1157 | ||
1158 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); | |
1159 | if (! rn) | |
1160 | { | |
1161 | zlog_info ("ifaddr_ipv4_delete(): can't find address %s", | |
1162 | inet_ntoa (*ifaddr)); | |
1163 | return; | |
1164 | } | |
1165 | rn->info = NULL; | |
1166 | route_unlock_node (rn); | |
1167 | route_unlock_node (rn); | |
1168 | } | |
1169 | ||
1170 | /* Lookup interface by interface's IP address or interface index. */ | |
8cc4198f | 1171 | static struct interface * |
b892f1dd | 1172 | ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex) |
718e3744 | 1173 | { |
1174 | struct prefix_ipv4 p; | |
1175 | struct route_node *rn; | |
1176 | struct interface *ifp; | |
718e3744 | 1177 | |
1178 | if (addr) | |
1179 | { | |
1180 | p.family = AF_INET; | |
1181 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
1182 | p.prefix = *addr; | |
1183 | ||
1184 | rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); | |
1185 | if (! rn) | |
1186 | return NULL; | |
55cd0f61 | 1187 | |
718e3744 | 1188 | ifp = rn->info; |
1189 | route_unlock_node (rn); | |
1190 | return ifp; | |
1191 | } | |
1192 | else | |
7e2b7603 | 1193 | return if_lookup_by_index(ifindex, VRF_DEFAULT); |
718e3744 | 1194 | } |
8cc4198f | 1195 | #endif /* ifaddr_ipv4_table */ |
718e3744 | 1196 | |
f4e14fdb | 1197 | void if_terminate(struct vrf *vrf) |
4bd045d5 | 1198 | { |
f4e14fdb | 1199 | struct interface *ifp; |
4bd045d5 | 1200 | |
55cd0f61 DS |
1201 | while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) { |
1202 | ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name); | |
1203 | ||
d62a17ae | 1204 | if (ifp->node) { |
1205 | ifp->node->info = NULL; | |
1206 | route_unlock_node(ifp->node); | |
1207 | } | |
f609709a | 1208 | if_delete(&ifp); |
d62a17ae | 1209 | } |
4bd045d5 | 1210 | } |
8ccc7e80 | 1211 | |
d62a17ae | 1212 | const char *if_link_type_str(enum zebra_link_type llt) |
8ccc7e80 | 1213 | { |
d62a17ae | 1214 | switch (llt) { |
8ccc7e80 | 1215 | #define llts(T,S) case (T): return (S) |
d62a17ae | 1216 | llts(ZEBRA_LLT_UNKNOWN, "Unknown"); |
1217 | llts(ZEBRA_LLT_ETHER, "Ethernet"); | |
1218 | llts(ZEBRA_LLT_EETHER, "Experimental Ethernet"); | |
1219 | llts(ZEBRA_LLT_AX25, "AX.25 Level 2"); | |
1220 | llts(ZEBRA_LLT_PRONET, "PROnet token ring"); | |
1221 | llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB"); | |
1222 | llts(ZEBRA_LLT_ARCNET, "ARCnet"); | |
1223 | llts(ZEBRA_LLT_APPLETLK, "AppleTalk"); | |
1224 | llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI"); | |
1225 | llts(ZEBRA_LLT_ATM, "ATM"); | |
1226 | llts(ZEBRA_LLT_METRICOM, "Metricom STRIP"); | |
1227 | llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4"); | |
1228 | llts(ZEBRA_LLT_EUI64, "EUI-64"); | |
1229 | llts(ZEBRA_LLT_INFINIBAND, "InfiniBand"); | |
1230 | llts(ZEBRA_LLT_SLIP, "SLIP"); | |
1231 | llts(ZEBRA_LLT_CSLIP, "Compressed SLIP"); | |
1232 | llts(ZEBRA_LLT_SLIP6, "SLIPv6"); | |
1233 | llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6"); | |
1234 | llts(ZEBRA_LLT_ROSE, "ROSE packet radio"); | |
1235 | llts(ZEBRA_LLT_X25, "CCITT X.25"); | |
1236 | llts(ZEBRA_LLT_PPP, "PPP"); | |
1237 | llts(ZEBRA_LLT_CHDLC, "Cisco HDLC"); | |
1238 | llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC"); | |
1239 | llts(ZEBRA_LLT_LAPB, "LAPB"); | |
1240 | llts(ZEBRA_LLT_IPIP, "IPIP Tunnel"); | |
1241 | llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel"); | |
1242 | llts(ZEBRA_LLT_FRAD, "FRAD"); | |
1243 | llts(ZEBRA_LLT_SKIP, "SKIP vif"); | |
1244 | llts(ZEBRA_LLT_LOOPBACK, "Loopback"); | |
1245 | llts(ZEBRA_LLT_LOCALTLK, "Localtalk"); | |
1246 | llts(ZEBRA_LLT_FDDI, "FDDI"); | |
1247 | llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT"); | |
1248 | llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel"); | |
1249 | llts(ZEBRA_LLT_IPGRE, "GRE over IP"); | |
1250 | llts(ZEBRA_LLT_PIMREG, "PIMSM registration"); | |
1251 | llts(ZEBRA_LLT_HIPPI, "HiPPI"); | |
1252 | llts(ZEBRA_LLT_IRDA, "IrDA"); | |
1253 | llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP"); | |
1254 | llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop"); | |
1255 | llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop"); | |
1256 | llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric"); | |
1257 | llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring"); | |
1258 | llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11"); | |
1259 | llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap"); | |
1260 | llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4"); | |
1261 | llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy"); | |
1262 | default: | |
450971aa | 1263 | flog_err(EC_LIB_DEVELOPMENT, "Unknown value %d", llt); |
d62a17ae | 1264 | return "Unknown type!"; |
8ccc7e80 | 1265 | #undef llts |
d62a17ae | 1266 | } |
1267 | return NULL; | |
8ccc7e80 | 1268 | } |
16f1b9ee | 1269 | |
d62a17ae | 1270 | struct if_link_params *if_link_params_get(struct interface *ifp) |
16f1b9ee | 1271 | { |
d62a17ae | 1272 | int i; |
16f1b9ee | 1273 | |
d62a17ae | 1274 | if (ifp->link_params != NULL) |
1275 | return ifp->link_params; | |
16f1b9ee | 1276 | |
d62a17ae | 1277 | struct if_link_params *iflp = |
1278 | XCALLOC(MTYPE_IF_LINK_PARAMS, sizeof(struct if_link_params)); | |
16f1b9ee | 1279 | |
d62a17ae | 1280 | /* Set TE metric equal to standard metric */ |
1281 | iflp->te_metric = ifp->metric; | |
16f1b9ee | 1282 | |
d62a17ae | 1283 | /* Compute default bandwidth based on interface */ |
1284 | iflp->default_bw = | |
1285 | ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH) | |
5eb567ed | 1286 | * TE_MEGA_BIT / TE_BYTE); |
16f1b9ee | 1287 | |
d62a17ae | 1288 | /* Set Max, Reservable and Unreserved Bandwidth */ |
1289 | iflp->max_bw = iflp->default_bw; | |
1290 | iflp->max_rsv_bw = iflp->default_bw; | |
1291 | for (i = 0; i < MAX_CLASS_TYPE; i++) | |
1292 | iflp->unrsv_bw[i] = iflp->default_bw; | |
16f1b9ee | 1293 | |
d62a17ae | 1294 | /* Update Link parameters status */ |
1295 | iflp->lp_status = | |
1296 | LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW; | |
16f1b9ee | 1297 | |
d62a17ae | 1298 | /* Finally attach newly created Link Parameters */ |
1299 | ifp->link_params = iflp; | |
16f1b9ee | 1300 | |
d62a17ae | 1301 | return iflp; |
16f1b9ee OD |
1302 | } |
1303 | ||
d62a17ae | 1304 | void if_link_params_free(struct interface *ifp) |
16f1b9ee | 1305 | { |
d62a17ae | 1306 | XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params); |
16f1b9ee | 1307 | } |
a4bed468 | 1308 | |
8f90d89b RW |
1309 | /* ----------- CLI commands ----------- */ |
1310 | ||
1311 | /* | |
1312 | * XPath: /frr-interface:lib/interface | |
1313 | */ | |
1314 | DEFPY_NOSH (interface, | |
1315 | interface_cmd, | |
e429a2a0 | 1316 | "interface IFNAME [vrf NAME$vrf_name]", |
8f90d89b RW |
1317 | "Select an interface to configure\n" |
1318 | "Interface's name\n" | |
1319 | VRF_CMD_HELP_STR) | |
1320 | { | |
1321 | char xpath_list[XPATH_MAXLEN]; | |
a36898e7 DS |
1322 | vrf_id_t vrf_id; |
1323 | struct interface *ifp; | |
8f90d89b RW |
1324 | int ret; |
1325 | ||
e429a2a0 IR |
1326 | if (!vrf_name) |
1327 | vrf_name = VRF_DEFAULT_NAME; | |
8f90d89b RW |
1328 | |
1329 | /* | |
1330 | * This command requires special handling to maintain backward | |
1331 | * compatibility. If a VRF name is not specified, it means we're willing | |
1332 | * to accept any interface with the given name on any VRF. If no | |
1333 | * interface is found, then a new one should be created on the default | |
1334 | * VRF. | |
1335 | */ | |
e429a2a0 | 1336 | VRF_GET_ID(vrf_id, vrf_name, false); |
a36898e7 DS |
1337 | ifp = if_lookup_by_name_all_vrf(ifname); |
1338 | if (ifp && ifp->vrf_id != vrf_id) { | |
1339 | struct vrf *vrf; | |
1340 | ||
8f90d89b RW |
1341 | /* |
1342 | * Special case 1: a VRF name was specified, but the found | |
1343 | * interface is associated to different VRF. Reject the command. | |
1344 | */ | |
a36898e7 | 1345 | if (vrf_id != VRF_DEFAULT) { |
8f90d89b | 1346 | vty_out(vty, "%% interface %s not in %s vrf\n", ifname, |
e429a2a0 | 1347 | vrf_name); |
8f90d89b RW |
1348 | return CMD_WARNING_CONFIG_FAILED; |
1349 | } | |
1350 | ||
1351 | /* | |
1352 | * Special case 2: a VRF name was *not* specified, and the found | |
1353 | * interface is associated to a VRF other than the default one. | |
e429a2a0 | 1354 | * Update vrf_id and vrf_name to account for that. |
8f90d89b | 1355 | */ |
a36898e7 | 1356 | vrf = vrf_lookup_by_id(ifp->vrf_id); |
8f90d89b | 1357 | assert(vrf); |
a36898e7 | 1358 | vrf_id = ifp->vrf_id; |
e429a2a0 | 1359 | vrf_name = vrf->name; |
8f90d89b RW |
1360 | } |
1361 | ||
1362 | snprintf(xpath_list, sizeof(xpath_list), | |
1363 | "/frr-interface:lib/interface[name='%s'][vrf='%s']", ifname, | |
e429a2a0 | 1364 | vrf_name); |
8f90d89b | 1365 | |
a6233bfc RW |
1366 | nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, NULL); |
1367 | ret = nb_cli_apply_changes(vty, xpath_list); | |
8f90d89b RW |
1368 | if (ret == CMD_SUCCESS) { |
1369 | VTY_PUSH_XPATH(INTERFACE_NODE, xpath_list); | |
1370 | ||
1371 | /* | |
1372 | * For backward compatibility with old commands we still need | |
1373 | * to use the qobj infrastructure. This can be removed once | |
1374 | * all interface-level commands are converted to the new | |
1375 | * northbound model. | |
1376 | */ | |
a36898e7 | 1377 | ifp = if_lookup_by_name(ifname, vrf_id); |
8f90d89b RW |
1378 | if (ifp) |
1379 | VTY_PUSH_CONTEXT(INTERFACE_NODE, ifp); | |
1380 | } | |
1381 | ||
1382 | return ret; | |
1383 | } | |
1384 | ||
1385 | DEFPY (no_interface, | |
1386 | no_interface_cmd, | |
e429a2a0 | 1387 | "no interface IFNAME [vrf NAME$vrf_name]", |
8f90d89b RW |
1388 | NO_STR |
1389 | "Delete a pseudo interface's configuration\n" | |
1390 | "Interface's name\n" | |
1391 | VRF_CMD_HELP_STR) | |
1392 | { | |
e429a2a0 IR |
1393 | if (!vrf_name) |
1394 | vrf_name = VRF_DEFAULT_NAME; | |
8f90d89b | 1395 | |
95ce849b | 1396 | nb_cli_enqueue_change(vty, ".", NB_OP_DESTROY, NULL); |
8f90d89b | 1397 | |
a6233bfc RW |
1398 | return nb_cli_apply_changes( |
1399 | vty, "/frr-interface:lib/interface[name='%s'][vrf='%s']", | |
e429a2a0 | 1400 | ifname, vrf_name); |
8f90d89b RW |
1401 | } |
1402 | ||
1403 | static void cli_show_interface(struct vty *vty, struct lyd_node *dnode, | |
1404 | bool show_defaults) | |
1405 | { | |
1406 | const char *vrf; | |
1407 | ||
1408 | vrf = yang_dnode_get_string(dnode, "./vrf"); | |
1409 | ||
1410 | vty_out(vty, "!\n"); | |
1411 | vty_out(vty, "interface %s", yang_dnode_get_string(dnode, "./name")); | |
1412 | if (!strmatch(vrf, VRF_DEFAULT_NAME)) | |
1413 | vty_out(vty, " vrf %s", vrf); | |
1414 | vty_out(vty, "\n"); | |
1415 | } | |
1416 | ||
1417 | /* | |
1418 | * XPath: /frr-interface:lib/interface/description | |
1419 | */ | |
1420 | DEFPY (interface_desc, | |
1421 | interface_desc_cmd, | |
1422 | "description LINE...", | |
1423 | "Interface specific description\n" | |
1424 | "Characters describing this interface\n") | |
1425 | { | |
8f90d89b RW |
1426 | char *desc; |
1427 | int ret; | |
1428 | ||
1429 | desc = argv_concat(argv, argc, 1); | |
a6233bfc RW |
1430 | nb_cli_enqueue_change(vty, "./description", NB_OP_MODIFY, desc); |
1431 | ret = nb_cli_apply_changes(vty, NULL); | |
8f90d89b RW |
1432 | XFREE(MTYPE_TMP, desc); |
1433 | ||
1434 | return ret; | |
1435 | } | |
1436 | ||
1437 | DEFPY (no_interface_desc, | |
1438 | no_interface_desc_cmd, | |
1439 | "no description", | |
1440 | NO_STR | |
1441 | "Interface specific description\n") | |
1442 | { | |
95ce849b | 1443 | nb_cli_enqueue_change(vty, "./description", NB_OP_DESTROY, NULL); |
a6233bfc RW |
1444 | |
1445 | return nb_cli_apply_changes(vty, NULL); | |
8f90d89b RW |
1446 | } |
1447 | ||
1448 | static void cli_show_interface_desc(struct vty *vty, struct lyd_node *dnode, | |
1449 | bool show_defaults) | |
1450 | { | |
1451 | vty_out(vty, " description %s\n", yang_dnode_get_string(dnode, NULL)); | |
1452 | } | |
1453 | ||
1454 | /* Interface autocomplete. */ | |
1455 | static void if_autocomplete(vector comps, struct cmd_token *token) | |
1456 | { | |
1457 | struct interface *ifp; | |
1458 | struct vrf *vrf; | |
1459 | ||
1460 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) { | |
1461 | FOR_ALL_INTERFACES (vrf, ifp) { | |
1462 | vector_set(comps, XSTRDUP(MTYPE_COMPLETION, ifp->name)); | |
1463 | } | |
1464 | } | |
1465 | } | |
1466 | ||
1467 | static const struct cmd_variable_handler if_var_handlers[] = { | |
1468 | {/* "interface NAME" */ | |
1469 | .varname = "interface", | |
1470 | .completions = if_autocomplete}, | |
1471 | {.tokenname = "IFNAME", .completions = if_autocomplete}, | |
1472 | {.tokenname = "INTERFACE", .completions = if_autocomplete}, | |
1473 | {.completions = NULL}}; | |
1474 | ||
1475 | void if_cmd_init(void) | |
1476 | { | |
1477 | cmd_variable_handler_register(if_var_handlers); | |
1478 | ||
1479 | install_element(CONFIG_NODE, &interface_cmd); | |
1480 | install_element(CONFIG_NODE, &no_interface_cmd); | |
1481 | ||
1482 | install_default(INTERFACE_NODE); | |
1483 | install_element(INTERFACE_NODE, &interface_desc_cmd); | |
1484 | install_element(INTERFACE_NODE, &no_interface_desc_cmd); | |
1485 | } | |
1486 | ||
138c5a74 DS |
1487 | void if_zapi_callbacks(int (*create)(struct interface *ifp), |
1488 | int (*up)(struct interface *ifp), | |
1489 | int (*down)(struct interface *ifp), | |
1490 | int (*destroy)(struct interface *ifp)) | |
1491 | { | |
1492 | ifp_master.create_hook = create; | |
1493 | ifp_master.up_hook = up; | |
1494 | ifp_master.down_hook = down; | |
1495 | ifp_master.destroy_hook = destroy; | |
1496 | } | |
1497 | ||
a4bed468 RW |
1498 | /* ------- Northbound callbacks ------- */ |
1499 | ||
1500 | /* | |
1501 | * XPath: /frr-interface:lib/interface | |
1502 | */ | |
60ee8be1 | 1503 | static int lib_interface_create(struct nb_cb_create_args *args) |
a4bed468 | 1504 | { |
8f90d89b RW |
1505 | const char *ifname; |
1506 | const char *vrfname; | |
1507 | struct vrf *vrf; | |
a36898e7 | 1508 | struct interface *ifp; |
8f90d89b | 1509 | |
60ee8be1 RW |
1510 | ifname = yang_dnode_get_string(args->dnode, "./name"); |
1511 | vrfname = yang_dnode_get_string(args->dnode, "./vrf"); | |
8f90d89b | 1512 | |
60ee8be1 | 1513 | switch (args->event) { |
8f90d89b RW |
1514 | case NB_EV_VALIDATE: |
1515 | vrf = vrf_lookup_by_name(vrfname); | |
1516 | if (!vrf) { | |
1517 | zlog_warn("%s: VRF %s doesn't exist", __func__, | |
1518 | vrfname); | |
1519 | return NB_ERR_VALIDATION; | |
1520 | } | |
a36898e7 DS |
1521 | if (vrf->vrf_id == VRF_UNKNOWN) { |
1522 | zlog_warn("%s: VRF %s is not active", __func__, | |
1523 | vrf->name); | |
1524 | return NB_ERR_VALIDATION; | |
1525 | } | |
72261ecd | 1526 | |
b0b97a7f PG |
1527 | /* if VRF is netns or not yet known - init for instance |
1528 | * then assumption is that passed config is exact | |
1529 | * then the user intent was not to use an other iface | |
1530 | */ | |
8f90d89b RW |
1531 | if (vrf_get_backend() == VRF_BACKEND_VRF_LITE) { |
1532 | ifp = if_lookup_by_name_all_vrf(ifname); | |
a36898e7 | 1533 | if (ifp && ifp->vrf_id != vrf->vrf_id) { |
8f90d89b RW |
1534 | zlog_warn( |
1535 | "%s: interface %s already exists in another VRF", | |
1536 | __func__, ifp->name); | |
1537 | return NB_ERR_VALIDATION; | |
1538 | } | |
1539 | } | |
1540 | break; | |
1541 | case NB_EV_PREPARE: | |
1542 | case NB_EV_ABORT: | |
1543 | break; | |
1544 | case NB_EV_APPLY: | |
1545 | vrf = vrf_lookup_by_name(vrfname); | |
1546 | assert(vrf); | |
1547 | #ifdef SUNOS_5 | |
a36898e7 | 1548 | ifp = if_sunwzebra_get(ifname, vrf->vrf_id); |
8f90d89b | 1549 | #else |
a36898e7 | 1550 | ifp = if_get_by_name(ifname, vrf->vrf_id); |
8f90d89b | 1551 | #endif /* SUNOS_5 */ |
1d311a05 DS |
1552 | |
1553 | ifp->configured = true; | |
60ee8be1 | 1554 | nb_running_set_entry(args->dnode, ifp); |
8f90d89b RW |
1555 | break; |
1556 | } | |
1557 | ||
a4bed468 RW |
1558 | return NB_OK; |
1559 | } | |
1560 | ||
60ee8be1 | 1561 | static int lib_interface_destroy(struct nb_cb_destroy_args *args) |
a4bed468 | 1562 | { |
8f90d89b RW |
1563 | struct interface *ifp; |
1564 | ||
8f90d89b | 1565 | |
60ee8be1 | 1566 | switch (args->event) { |
8f90d89b | 1567 | case NB_EV_VALIDATE: |
60ee8be1 | 1568 | ifp = nb_running_get_entry(args->dnode, NULL, true); |
8f90d89b | 1569 | if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) { |
10bdc68f RW |
1570 | snprintf(args->errmsg, args->errmsg_len, |
1571 | "only inactive interfaces can be deleted"); | |
8f90d89b RW |
1572 | return NB_ERR_VALIDATION; |
1573 | } | |
1574 | break; | |
1575 | case NB_EV_PREPARE: | |
1576 | case NB_EV_ABORT: | |
1577 | break; | |
1578 | case NB_EV_APPLY: | |
60ee8be1 | 1579 | ifp = nb_running_unset_entry(args->dnode); |
1d311a05 DS |
1580 | |
1581 | ifp->configured = false; | |
f609709a | 1582 | if_delete(&ifp); |
8f90d89b RW |
1583 | break; |
1584 | } | |
1585 | ||
a4bed468 RW |
1586 | return NB_OK; |
1587 | } | |
1588 | ||
f88647ef QY |
1589 | /* |
1590 | * XPath: /frr-interface:lib/interface | |
1591 | */ | |
60ee8be1 | 1592 | static const void *lib_interface_get_next(struct nb_cb_get_next_args *args) |
f88647ef QY |
1593 | { |
1594 | struct vrf *vrf; | |
60ee8be1 | 1595 | struct interface *pif = (struct interface *)args->list_entry; |
f88647ef | 1596 | |
60ee8be1 | 1597 | if (args->list_entry == NULL) { |
f88647ef QY |
1598 | vrf = RB_MIN(vrf_name_head, &vrfs_by_name); |
1599 | assert(vrf); | |
1600 | pif = RB_MIN(if_name_head, &vrf->ifaces_by_name); | |
1601 | } else { | |
1602 | vrf = vrf_lookup_by_id(pif->vrf_id); | |
1603 | pif = RB_NEXT(if_name_head, pif); | |
1604 | /* if no more interfaces, switch to next vrf */ | |
1605 | while (pif == NULL) { | |
1606 | vrf = RB_NEXT(vrf_name_head, vrf); | |
1607 | if (!vrf) | |
1608 | return NULL; | |
1609 | pif = RB_MIN(if_name_head, &vrf->ifaces_by_name); | |
1610 | } | |
1611 | } | |
1612 | ||
1613 | return pif; | |
1614 | } | |
1615 | ||
60ee8be1 | 1616 | static int lib_interface_get_keys(struct nb_cb_get_keys_args *args) |
f88647ef | 1617 | { |
60ee8be1 | 1618 | const struct interface *ifp = args->list_entry; |
f88647ef QY |
1619 | |
1620 | struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id); | |
1621 | ||
1622 | assert(vrf); | |
1623 | ||
60ee8be1 RW |
1624 | args->keys->num = 2; |
1625 | strlcpy(args->keys->key[0], ifp->name, sizeof(args->keys->key[0])); | |
1626 | strlcpy(args->keys->key[1], vrf->name, sizeof(args->keys->key[1])); | |
f88647ef QY |
1627 | |
1628 | return NB_OK; | |
1629 | } | |
1630 | ||
60ee8be1 RW |
1631 | static const void * |
1632 | lib_interface_lookup_entry(struct nb_cb_lookup_entry_args *args) | |
f88647ef | 1633 | { |
60ee8be1 RW |
1634 | const char *ifname = args->keys->key[0]; |
1635 | const char *vrfname = args->keys->key[1]; | |
f88647ef QY |
1636 | struct vrf *vrf = vrf_lookup_by_name(vrfname); |
1637 | ||
68a4422d | 1638 | return vrf ? if_lookup_by_name(ifname, vrf->vrf_id) : NULL; |
f88647ef QY |
1639 | } |
1640 | ||
a4bed468 RW |
1641 | /* |
1642 | * XPath: /frr-interface:lib/interface/description | |
1643 | */ | |
60ee8be1 | 1644 | static int lib_interface_description_modify(struct nb_cb_modify_args *args) |
a4bed468 | 1645 | { |
8f90d89b RW |
1646 | struct interface *ifp; |
1647 | const char *description; | |
1648 | ||
60ee8be1 | 1649 | if (args->event != NB_EV_APPLY) |
8f90d89b RW |
1650 | return NB_OK; |
1651 | ||
60ee8be1 | 1652 | ifp = nb_running_get_entry(args->dnode, NULL, true); |
0a22ddfb | 1653 | XFREE(MTYPE_TMP, ifp->desc); |
60ee8be1 | 1654 | description = yang_dnode_get_string(args->dnode, NULL); |
8f90d89b RW |
1655 | ifp->desc = XSTRDUP(MTYPE_TMP, description); |
1656 | ||
a4bed468 RW |
1657 | return NB_OK; |
1658 | } | |
1659 | ||
60ee8be1 | 1660 | static int lib_interface_description_destroy(struct nb_cb_destroy_args *args) |
a4bed468 | 1661 | { |
8f90d89b RW |
1662 | struct interface *ifp; |
1663 | ||
60ee8be1 | 1664 | if (args->event != NB_EV_APPLY) |
8f90d89b RW |
1665 | return NB_OK; |
1666 | ||
60ee8be1 | 1667 | ifp = nb_running_get_entry(args->dnode, NULL, true); |
0a22ddfb | 1668 | XFREE(MTYPE_TMP, ifp->desc); |
8f90d89b | 1669 | |
a4bed468 RW |
1670 | return NB_OK; |
1671 | } | |
1672 | ||
4e0a7355 CS |
1673 | /* |
1674 | * XPath: /frr-interface:lib/interface/state/if-index | |
1675 | */ | |
60ee8be1 RW |
1676 | static struct yang_data * |
1677 | lib_interface_state_if_index_get_elem(struct nb_cb_get_elem_args *args) | |
4e0a7355 | 1678 | { |
60ee8be1 | 1679 | const struct interface *ifp = args->list_entry; |
4e0a7355 | 1680 | |
60ee8be1 | 1681 | return yang_data_new_int32(args->xpath, ifp->ifindex); |
4e0a7355 CS |
1682 | } |
1683 | ||
1684 | /* | |
1685 | * XPath: /frr-interface:lib/interface/state/mtu | |
1686 | */ | |
60ee8be1 RW |
1687 | static struct yang_data * |
1688 | lib_interface_state_mtu_get_elem(struct nb_cb_get_elem_args *args) | |
4e0a7355 | 1689 | { |
60ee8be1 | 1690 | const struct interface *ifp = args->list_entry; |
4e0a7355 | 1691 | |
60ee8be1 | 1692 | return yang_data_new_uint16(args->xpath, ifp->mtu); |
4e0a7355 CS |
1693 | } |
1694 | ||
1695 | /* | |
1696 | * XPath: /frr-interface:lib/interface/state/mtu6 | |
1697 | */ | |
60ee8be1 RW |
1698 | static struct yang_data * |
1699 | lib_interface_state_mtu6_get_elem(struct nb_cb_get_elem_args *args) | |
4e0a7355 | 1700 | { |
60ee8be1 | 1701 | const struct interface *ifp = args->list_entry; |
4e0a7355 | 1702 | |
60ee8be1 | 1703 | return yang_data_new_uint32(args->xpath, ifp->mtu6); |
4e0a7355 CS |
1704 | } |
1705 | ||
1706 | /* | |
1707 | * XPath: /frr-interface:lib/interface/state/speed | |
1708 | */ | |
60ee8be1 RW |
1709 | static struct yang_data * |
1710 | lib_interface_state_speed_get_elem(struct nb_cb_get_elem_args *args) | |
4e0a7355 | 1711 | { |
60ee8be1 | 1712 | const struct interface *ifp = args->list_entry; |
4e0a7355 | 1713 | |
60ee8be1 | 1714 | return yang_data_new_uint32(args->xpath, ifp->speed); |
4e0a7355 CS |
1715 | } |
1716 | ||
1717 | /* | |
1718 | * XPath: /frr-interface:lib/interface/state/metric | |
1719 | */ | |
60ee8be1 RW |
1720 | static struct yang_data * |
1721 | lib_interface_state_metric_get_elem(struct nb_cb_get_elem_args *args) | |
4e0a7355 | 1722 | { |
60ee8be1 | 1723 | const struct interface *ifp = args->list_entry; |
4e0a7355 | 1724 | |
60ee8be1 | 1725 | return yang_data_new_uint32(args->xpath, ifp->metric); |
4e0a7355 CS |
1726 | } |
1727 | ||
1728 | /* | |
1729 | * XPath: /frr-interface:lib/interface/state/flags | |
1730 | */ | |
60ee8be1 RW |
1731 | static struct yang_data * |
1732 | lib_interface_state_flags_get_elem(struct nb_cb_get_elem_args *args) | |
4e0a7355 CS |
1733 | { |
1734 | /* TODO: implement me. */ | |
1735 | return NULL; | |
1736 | } | |
1737 | ||
1738 | /* | |
1739 | * XPath: /frr-interface:lib/interface/state/type | |
1740 | */ | |
60ee8be1 RW |
1741 | static struct yang_data * |
1742 | lib_interface_state_type_get_elem(struct nb_cb_get_elem_args *args) | |
4e0a7355 CS |
1743 | { |
1744 | /* TODO: implement me. */ | |
1745 | return NULL; | |
1746 | } | |
1747 | ||
1748 | /* | |
1749 | * XPath: /frr-interface:lib/interface/state/phy-address | |
1750 | */ | |
60ee8be1 RW |
1751 | static struct yang_data * |
1752 | lib_interface_state_phy_address_get_elem(struct nb_cb_get_elem_args *args) | |
4e0a7355 | 1753 | { |
60ee8be1 | 1754 | const struct interface *ifp = args->list_entry; |
4e0a7355 CS |
1755 | struct ethaddr macaddr; |
1756 | ||
1757 | memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN); | |
1758 | ||
60ee8be1 | 1759 | return yang_data_new_mac(args->xpath, &macaddr); |
4e0a7355 CS |
1760 | } |
1761 | ||
a4bed468 RW |
1762 | /* clang-format off */ |
1763 | const struct frr_yang_module_info frr_interface_info = { | |
1764 | .name = "frr-interface", | |
1765 | .nodes = { | |
1766 | { | |
1767 | .xpath = "/frr-interface:lib/interface", | |
53280f93 DL |
1768 | .cbs = { |
1769 | .create = lib_interface_create, | |
1770 | .destroy = lib_interface_destroy, | |
1771 | .cli_show = cli_show_interface, | |
f88647ef QY |
1772 | .get_next = lib_interface_get_next, |
1773 | .get_keys = lib_interface_get_keys, | |
1774 | .lookup_entry = lib_interface_lookup_entry, | |
53280f93 | 1775 | }, |
a4bed468 RW |
1776 | }, |
1777 | { | |
1778 | .xpath = "/frr-interface:lib/interface/description", | |
53280f93 DL |
1779 | .cbs = { |
1780 | .modify = lib_interface_description_modify, | |
1781 | .destroy = lib_interface_description_destroy, | |
1782 | .cli_show = cli_show_interface_desc, | |
1783 | }, | |
a4bed468 | 1784 | }, |
4e0a7355 CS |
1785 | { |
1786 | .xpath = "/frr-interface:lib/interface/state/if-index", | |
1787 | .cbs = { | |
1788 | .get_elem = lib_interface_state_if_index_get_elem, | |
1789 | } | |
1790 | }, | |
1791 | { | |
1792 | .xpath = "/frr-interface:lib/interface/state/mtu", | |
1793 | .cbs = { | |
1794 | .get_elem = lib_interface_state_mtu_get_elem, | |
1795 | } | |
1796 | }, | |
1797 | { | |
1798 | .xpath = "/frr-interface:lib/interface/state/mtu6", | |
1799 | .cbs = { | |
1800 | .get_elem = lib_interface_state_mtu6_get_elem, | |
1801 | } | |
1802 | }, | |
1803 | { | |
1804 | .xpath = "/frr-interface:lib/interface/state/speed", | |
1805 | .cbs = { | |
1806 | .get_elem = lib_interface_state_speed_get_elem, | |
1807 | } | |
1808 | }, | |
1809 | { | |
1810 | .xpath = "/frr-interface:lib/interface/state/metric", | |
1811 | .cbs = { | |
1812 | .get_elem = lib_interface_state_metric_get_elem, | |
1813 | } | |
1814 | }, | |
1815 | { | |
1816 | .xpath = "/frr-interface:lib/interface/state/flags", | |
1817 | .cbs = { | |
1818 | .get_elem = lib_interface_state_flags_get_elem, | |
1819 | } | |
1820 | }, | |
1821 | { | |
1822 | .xpath = "/frr-interface:lib/interface/state/type", | |
1823 | .cbs = { | |
1824 | .get_elem = lib_interface_state_type_get_elem, | |
1825 | } | |
1826 | }, | |
1827 | { | |
1828 | .xpath = "/frr-interface:lib/interface/state/phy-address", | |
1829 | .cbs = { | |
1830 | .get_elem = lib_interface_state_phy_address_get_elem, | |
1831 | } | |
1832 | }, | |
a4bed468 RW |
1833 | { |
1834 | .xpath = NULL, | |
1835 | }, | |
1836 | } | |
1837 | }; |