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