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