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