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