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