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