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