]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
18a6dce6 | 2 | /* |
3 | * Router ID for zebra daemon. | |
4 | * | |
d62a17ae | 5 | * Copyright (C) 2004 James R. Leu |
18a6dce6 | 6 | * |
7 | * This file is part of Quagga routing suite. | |
18a6dce6 | 8 | */ |
9 | ||
10 | #include <zebra.h> | |
11 | ||
12 | #include "if.h" | |
13 | #include "vty.h" | |
14 | #include "sockunion.h" | |
15 | #include "prefix.h" | |
16 | #include "stream.h" | |
17 | #include "command.h" | |
18 | #include "memory.h" | |
19 | #include "ioctl.h" | |
20 | #include "connected.h" | |
21 | #include "network.h" | |
22 | #include "log.h" | |
23 | #include "table.h" | |
24 | #include "rib.h" | |
c6ffe645 | 25 | #include "vrf.h" |
18a6dce6 | 26 | |
161e9ab7 | 27 | #include "zebra/zebra_router.h" |
bf094f69 | 28 | #include "zebra/zapi_msg.h" |
7c551956 | 29 | #include "zebra/zebra_vrf.h" |
a1ac18c4 | 30 | #include "zebra/router-id.h" |
6dc686a2 | 31 | #include "zebra/redistribute.h" |
18a6dce6 | 32 | |
d62a17ae | 33 | static struct connected *router_id_find_node(struct list *l, |
34 | struct connected *ifc) | |
18a6dce6 | 35 | { |
d62a17ae | 36 | struct listnode *node; |
37 | struct connected *c; | |
18a6dce6 | 38 | |
d62a17ae | 39 | for (ALL_LIST_ELEMENTS_RO(l, node, c)) |
40 | if (prefix_same(ifc->address, c->address)) | |
41 | return c; | |
1eb8ef25 | 42 | |
d62a17ae | 43 | return NULL; |
18a6dce6 | 44 | } |
45 | ||
d62a17ae | 46 | static int router_id_bad_address(struct connected *ifc) |
18a6dce6 | 47 | { |
d62a17ae | 48 | /* non-redistributable addresses shouldn't be used for RIDs either */ |
49 | if (!zebra_check_addr(ifc->address)) | |
50 | return 1; | |
51 | ||
52 | return 0; | |
18a6dce6 | 53 | } |
54 | ||
98a3fb0a SM |
55 | static bool router_id_v6_is_any(struct prefix *p) |
56 | { | |
57 | return memcmp(&p->u.prefix6, &in6addr_any, sizeof(struct in6_addr)) | |
58 | == 0; | |
59 | } | |
60 | ||
61 | int router_id_get(afi_t afi, struct prefix *p, struct zebra_vrf *zvrf) | |
18a6dce6 | 62 | { |
d62a17ae | 63 | struct listnode *node; |
64 | struct connected *c; | |
98a3fb0a SM |
65 | struct in6_addr *addr = NULL; |
66 | ||
67 | switch (afi) { | |
68 | case AFI_IP: | |
69 | p->u.prefix4.s_addr = INADDR_ANY; | |
70 | p->family = AF_INET; | |
12256b84 | 71 | p->prefixlen = IPV4_MAX_BITLEN; |
98a3fb0a SM |
72 | if (zvrf->rid_user_assigned.u.prefix4.s_addr != INADDR_ANY) |
73 | p->u.prefix4.s_addr = | |
74 | zvrf->rid_user_assigned.u.prefix4.s_addr; | |
75 | else if (!list_isempty(zvrf->rid_lo_sorted_list)) { | |
76 | node = listtail(zvrf->rid_lo_sorted_list); | |
77 | c = listgetdata(node); | |
78 | p->u.prefix4.s_addr = c->address->u.prefix4.s_addr; | |
79 | } else if (!list_isempty(zvrf->rid_all_sorted_list)) { | |
80 | node = listtail(zvrf->rid_all_sorted_list); | |
81 | c = listgetdata(node); | |
82 | p->u.prefix4.s_addr = c->address->u.prefix4.s_addr; | |
83 | } | |
84 | return 0; | |
85 | case AFI_IP6: | |
86 | p->u.prefix6 = in6addr_any; | |
87 | p->family = AF_INET6; | |
13ccce6e | 88 | p->prefixlen = IPV6_MAX_BITLEN; |
98a3fb0a SM |
89 | if (!router_id_v6_is_any(&zvrf->rid6_user_assigned)) |
90 | addr = &zvrf->rid6_user_assigned.u.prefix6; | |
91 | else if (!list_isempty(zvrf->rid6_lo_sorted_list)) { | |
92 | node = listtail(zvrf->rid6_lo_sorted_list); | |
93 | c = listgetdata(node); | |
94 | addr = &c->address->u.prefix6; | |
95 | } else if (!list_isempty(zvrf->rid6_all_sorted_list)) { | |
96 | node = listtail(zvrf->rid6_all_sorted_list); | |
97 | c = listgetdata(node); | |
98 | addr = &c->address->u.prefix6; | |
99 | } | |
100 | if (addr) | |
101 | memcpy(&p->u.prefix6, addr, sizeof(struct in6_addr)); | |
102 | return 0; | |
a98701f0 DS |
103 | case AFI_UNSPEC: |
104 | case AFI_L2VPN: | |
105 | case AFI_MAX: | |
98a3fb0a | 106 | return -1; |
d62a17ae | 107 | } |
a98701f0 DS |
108 | |
109 | assert(!"Reached end of function we should never hit"); | |
18a6dce6 | 110 | } |
111 | ||
98a3fb0a | 112 | static int router_id_set(afi_t afi, struct prefix *p, struct zebra_vrf *zvrf) |
18a6dce6 | 113 | { |
a1f35d7e | 114 | struct prefix after, before; |
d62a17ae | 115 | struct listnode *node; |
116 | struct zserv *client; | |
d62a17ae | 117 | |
a1f35d7e DS |
118 | router_id_get(afi, &before, zvrf); |
119 | ||
98a3fb0a SM |
120 | switch (afi) { |
121 | case AFI_IP: | |
122 | zvrf->rid_user_assigned.u.prefix4.s_addr = p->u.prefix4.s_addr; | |
123 | break; | |
124 | case AFI_IP6: | |
125 | zvrf->rid6_user_assigned.u.prefix6 = p->u.prefix6; | |
126 | break; | |
a98701f0 DS |
127 | case AFI_UNSPEC: |
128 | case AFI_L2VPN: | |
129 | case AFI_MAX: | |
98a3fb0a SM |
130 | return -1; |
131 | } | |
d62a17ae | 132 | |
a1f35d7e DS |
133 | router_id_get(afi, &after, zvrf); |
134 | ||
135 | /* | |
136 | * If we've been told that the router-id is exactly the same | |
137 | * do we need to really do anything here? | |
138 | */ | |
139 | if (prefix_same(&before, &after)) | |
140 | return 0; | |
d62a17ae | 141 | |
161e9ab7 | 142 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) |
a1f35d7e | 143 | zsend_router_id_update(client, afi, &after, zvrf->vrf->vrf_id); |
98a3fb0a SM |
144 | |
145 | return 0; | |
18a6dce6 | 146 | } |
147 | ||
d62a17ae | 148 | void router_id_add_address(struct connected *ifc) |
18a6dce6 | 149 | { |
d62a17ae | 150 | struct list *l = NULL; |
151 | struct listnode *node; | |
152 | struct prefix before; | |
153 | struct prefix after; | |
154 | struct zserv *client; | |
096f7609 | 155 | struct zebra_vrf *zvrf = ifc->ifp->vrf->info; |
98a3fb0a SM |
156 | afi_t afi; |
157 | struct list *rid_lo; | |
158 | struct list *rid_all; | |
d62a17ae | 159 | |
160 | if (router_id_bad_address(ifc)) | |
161 | return; | |
162 | ||
98a3fb0a SM |
163 | switch (ifc->address->family) { |
164 | case AF_INET: | |
165 | afi = AFI_IP; | |
166 | rid_lo = zvrf->rid_lo_sorted_list; | |
167 | rid_all = zvrf->rid_all_sorted_list; | |
168 | break; | |
169 | case AF_INET6: | |
170 | afi = AFI_IP6; | |
171 | rid_lo = zvrf->rid6_lo_sorted_list; | |
172 | rid_all = zvrf->rid6_all_sorted_list; | |
173 | break; | |
174 | default: | |
175 | return; | |
176 | } | |
d62a17ae | 177 | |
98a3fb0a SM |
178 | router_id_get(afi, &before, zvrf); |
179 | ||
180 | l = if_is_loopback(ifc->ifp) ? rid_lo : rid_all; | |
d62a17ae | 181 | |
182 | if (!router_id_find_node(l, ifc)) | |
183 | listnode_add_sort(l, ifc); | |
184 | ||
98a3fb0a | 185 | router_id_get(afi, &after, zvrf); |
d62a17ae | 186 | |
187 | if (prefix_same(&before, &after)) | |
188 | return; | |
189 | ||
161e9ab7 | 190 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) |
98a3fb0a | 191 | zsend_router_id_update(client, afi, &after, zvrf_id(zvrf)); |
18a6dce6 | 192 | } |
193 | ||
d62a17ae | 194 | void router_id_del_address(struct connected *ifc) |
18a6dce6 | 195 | { |
d62a17ae | 196 | struct connected *c; |
197 | struct list *l; | |
198 | struct prefix after; | |
199 | struct prefix before; | |
200 | struct listnode *node; | |
201 | struct zserv *client; | |
096f7609 | 202 | struct zebra_vrf *zvrf = ifc->ifp->vrf->info; |
98a3fb0a SM |
203 | afi_t afi; |
204 | struct list *rid_lo; | |
205 | struct list *rid_all; | |
18a6dce6 | 206 | |
d62a17ae | 207 | if (router_id_bad_address(ifc)) |
208 | return; | |
18a6dce6 | 209 | |
98a3fb0a SM |
210 | switch (ifc->address->family) { |
211 | case AF_INET: | |
212 | afi = AFI_IP; | |
213 | rid_lo = zvrf->rid_lo_sorted_list; | |
214 | rid_all = zvrf->rid_all_sorted_list; | |
215 | break; | |
216 | case AF_INET6: | |
217 | afi = AFI_IP6; | |
218 | rid_lo = zvrf->rid6_lo_sorted_list; | |
219 | rid_all = zvrf->rid6_all_sorted_list; | |
220 | break; | |
221 | default: | |
222 | return; | |
223 | } | |
224 | ||
225 | router_id_get(afi, &before, zvrf); | |
18a6dce6 | 226 | |
106935f6 | 227 | if (if_is_loopback(ifc->ifp)) |
98a3fb0a | 228 | l = rid_lo; |
d62a17ae | 229 | else |
98a3fb0a | 230 | l = rid_all; |
18a6dce6 | 231 | |
d62a17ae | 232 | if ((c = router_id_find_node(l, ifc))) |
233 | listnode_delete(l, c); | |
18a6dce6 | 234 | |
98a3fb0a | 235 | router_id_get(afi, &after, zvrf); |
18a6dce6 | 236 | |
d62a17ae | 237 | if (prefix_same(&before, &after)) |
238 | return; | |
18a6dce6 | 239 | |
161e9ab7 | 240 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) |
98a3fb0a | 241 | zsend_router_id_update(client, afi, &after, zvrf_id(zvrf)); |
18a6dce6 | 242 | } |
243 | ||
03fba42e | 244 | void router_id_write(struct vty *vty, struct zebra_vrf *zvrf) |
18a6dce6 | 245 | { |
03fba42e DS |
246 | char space[2]; |
247 | ||
248 | memset(space, 0, sizeof(space)); | |
249 | ||
250 | if (zvrf_id(zvrf) != VRF_DEFAULT) | |
251 | snprintf(space, sizeof(space), "%s", " "); | |
d62a17ae | 252 | |
03fba42e | 253 | if (zvrf->rid_user_assigned.u.prefix4.s_addr != INADDR_ANY) { |
98a3fb0a SM |
254 | vty_out(vty, "%sip router-id %pI4\n", space, |
255 | &zvrf->rid_user_assigned.u.prefix4); | |
256 | } | |
257 | if (!router_id_v6_is_any(&zvrf->rid6_user_assigned)) { | |
258 | vty_out(vty, "%sipv6 router-id %pI6\n", space, | |
259 | &zvrf->rid_user_assigned.u.prefix6); | |
03fba42e | 260 | } |
18a6dce6 | 261 | } |
262 | ||
98a3fb0a SM |
263 | DEFUN (ip_router_id, |
264 | ip_router_id_cmd, | |
265 | "ip router-id A.B.C.D vrf NAME", | |
266 | IP_STR | |
18a6dce6 | 267 | "Manually set the router-id\n" |
98a3fb0a SM |
268 | "IP address to use for router-id\n" |
269 | VRF_CMD_HELP_STR) | |
18a6dce6 | 270 | { |
98a3fb0a SM |
271 | int idx = 0; |
272 | struct prefix rid; | |
273 | vrf_id_t vrf_id; | |
03fba42e | 274 | struct zebra_vrf *zvrf; |
92300491 | 275 | |
98a3fb0a | 276 | argv_find(argv, argc, "A.B.C.D", &idx); |
18a6dce6 | 277 | |
98a3fb0a | 278 | if (!inet_pton(AF_INET, argv[idx]->arg, &rid.u.prefix4)) |
d62a17ae | 279 | return CMD_WARNING_CONFIG_FAILED; |
18a6dce6 | 280 | |
12256b84 | 281 | rid.prefixlen = IPV4_MAX_BITLEN; |
d62a17ae | 282 | rid.family = AF_INET; |
18a6dce6 | 283 | |
98a3fb0a SM |
284 | argv_find(argv, argc, "NAME", &idx); |
285 | VRF_GET_ID(vrf_id, argv[idx]->arg, false); | |
c6ffe645 | 286 | |
9a7d1e74 | 287 | zvrf = zebra_vrf_lookup_by_id(vrf_id); |
98a3fb0a | 288 | router_id_set(AFI_IP, &rid, zvrf); |
03fba42e DS |
289 | |
290 | return CMD_SUCCESS; | |
291 | } | |
292 | ||
98a3fb0a SM |
293 | ALIAS (ip_router_id, |
294 | router_id_cmd, | |
295 | "router-id A.B.C.D vrf NAME", | |
296 | "Manually set the router-id\n" | |
297 | "IP address to use for router-id\n" | |
298 | VRF_CMD_HELP_STR); | |
299 | ||
300 | DEFUN (ipv6_router_id, | |
301 | ipv6_router_id_cmd, | |
302 | "ipv6 router-id X:X::X:X vrf NAME", | |
303 | IPV6_STR | |
304 | "Manually set the router-id\n" | |
305 | "IPv6 address to use for router-id\n" | |
306 | VRF_CMD_HELP_STR) | |
307 | { | |
308 | int idx = 0; | |
309 | struct prefix rid; | |
310 | vrf_id_t vrf_id; | |
311 | struct zebra_vrf *zvrf; | |
312 | ||
313 | argv_find(argv, argc, "X:X::X:X", &idx); | |
314 | ||
315 | if (!inet_pton(AF_INET6, argv[idx]->arg, &rid.u.prefix6)) | |
316 | return CMD_WARNING_CONFIG_FAILED; | |
317 | ||
13ccce6e | 318 | rid.prefixlen = IPV6_MAX_BITLEN; |
98a3fb0a SM |
319 | rid.family = AF_INET6; |
320 | ||
321 | argv_find(argv, argc, "NAME", &idx); | |
322 | VRF_GET_ID(vrf_id, argv[idx]->arg, false); | |
323 | ||
9a7d1e74 | 324 | zvrf = zebra_vrf_lookup_by_id(vrf_id); |
98a3fb0a SM |
325 | router_id_set(AFI_IP6, &rid, zvrf); |
326 | ||
327 | return CMD_SUCCESS; | |
328 | } | |
329 | ||
330 | ||
331 | DEFUN (ip_router_id_in_vrf, | |
332 | ip_router_id_in_vrf_cmd, | |
333 | "ip router-id A.B.C.D", | |
334 | IP_STR | |
02c671af | 335 | "Manually set the router-id\n" |
03fba42e DS |
336 | "IP address to use for router-id\n") |
337 | { | |
0cbed951 | 338 | ZEBRA_DECLVAR_CONTEXT_VRF(vrf, zvrf); |
98a3fb0a | 339 | int idx = 0; |
03fba42e DS |
340 | struct prefix rid; |
341 | ||
98a3fb0a SM |
342 | argv_find(argv, argc, "A.B.C.D", &idx); |
343 | ||
344 | if (!inet_pton(AF_INET, argv[idx]->arg, &rid.u.prefix4)) | |
03fba42e DS |
345 | return CMD_WARNING_CONFIG_FAILED; |
346 | ||
12256b84 | 347 | rid.prefixlen = IPV4_MAX_BITLEN; |
03fba42e DS |
348 | rid.family = AF_INET; |
349 | ||
98a3fb0a | 350 | router_id_set(AFI_IP, &rid, zvrf); |
18a6dce6 | 351 | |
d62a17ae | 352 | return CMD_SUCCESS; |
18a6dce6 | 353 | } |
354 | ||
98a3fb0a SM |
355 | ALIAS (ip_router_id_in_vrf, |
356 | router_id_in_vrf_cmd, | |
357 | "router-id A.B.C.D", | |
358 | "Manually set the router-id\n" | |
359 | "IP address to use for router-id\n"); | |
360 | ||
361 | DEFUN (ipv6_router_id_in_vrf, | |
362 | ipv6_router_id_in_vrf_cmd, | |
363 | "ipv6 router-id X:X::X:X", | |
364 | IP6_STR | |
02c671af | 365 | "Manually set the IPv6 router-id\n" |
98a3fb0a SM |
366 | "IPV6 address to use for router-id\n") |
367 | { | |
0cbed951 | 368 | ZEBRA_DECLVAR_CONTEXT_VRF(vrf, zvrf); |
98a3fb0a SM |
369 | int idx = 0; |
370 | struct prefix rid; | |
371 | ||
372 | argv_find(argv, argc, "X:X::X:X", &idx); | |
373 | ||
374 | if (!inet_pton(AF_INET6, argv[idx]->arg, &rid.u.prefix6)) | |
375 | return CMD_WARNING_CONFIG_FAILED; | |
376 | ||
13ccce6e | 377 | rid.prefixlen = IPV6_MAX_BITLEN; |
98a3fb0a SM |
378 | rid.family = AF_INET6; |
379 | ||
380 | router_id_set(AFI_IP6, &rid, zvrf); | |
381 | ||
382 | return CMD_SUCCESS; | |
383 | } | |
384 | ||
385 | DEFUN (no_ip_router_id, | |
386 | no_ip_router_id_cmd, | |
387 | "no ip router-id [A.B.C.D vrf NAME]", | |
18a6dce6 | 388 | NO_STR |
98a3fb0a | 389 | IP_STR |
92300491 | 390 | "Remove the manually configured router-id\n" |
98a3fb0a SM |
391 | "IP address to use for router-id\n" |
392 | VRF_CMD_HELP_STR) | |
18a6dce6 | 393 | { |
98a3fb0a | 394 | int idx = 0; |
d62a17ae | 395 | struct prefix rid; |
396 | vrf_id_t vrf_id = VRF_DEFAULT; | |
03fba42e | 397 | struct zebra_vrf *zvrf; |
18a6dce6 | 398 | |
d62a17ae | 399 | rid.u.prefix4.s_addr = 0; |
400 | rid.prefixlen = 0; | |
401 | rid.family = AF_INET; | |
18a6dce6 | 402 | |
98a3fb0a SM |
403 | if (argv_find(argv, argc, "NAME", &idx)) |
404 | VRF_GET_ID(vrf_id, argv[idx]->arg, false); | |
c6ffe645 | 405 | |
9a7d1e74 | 406 | zvrf = zebra_vrf_lookup_by_id(vrf_id); |
98a3fb0a | 407 | router_id_set(AFI_IP, &rid, zvrf); |
03fba42e DS |
408 | |
409 | return CMD_SUCCESS; | |
410 | } | |
411 | ||
98a3fb0a SM |
412 | ALIAS (no_ip_router_id, |
413 | no_router_id_cmd, | |
414 | "no router-id [A.B.C.D vrf NAME]", | |
415 | NO_STR | |
416 | "Remove the manually configured router-id\n" | |
417 | "IP address to use for router-id\n" | |
418 | VRF_CMD_HELP_STR); | |
419 | ||
420 | DEFUN (no_ipv6_router_id, | |
421 | no_ipv6_router_id_cmd, | |
422 | "no ipv6 router-id [X:X::X:X vrf NAME]", | |
423 | NO_STR | |
424 | IPV6_STR | |
425 | "Remove the manually configured IPv6 router-id\n" | |
426 | "IPv6 address to use for router-id\n" | |
427 | VRF_CMD_HELP_STR) | |
428 | { | |
429 | int idx = 0; | |
430 | struct prefix rid; | |
431 | vrf_id_t vrf_id = VRF_DEFAULT; | |
432 | struct zebra_vrf *zvrf; | |
433 | ||
434 | memset(&rid, 0, sizeof(rid)); | |
435 | rid.family = AF_INET; | |
436 | ||
437 | if (argv_find(argv, argc, "NAME", &idx)) | |
438 | VRF_GET_ID(vrf_id, argv[idx]->arg, false); | |
439 | ||
9a7d1e74 | 440 | zvrf = zebra_vrf_lookup_by_id(vrf_id); |
98a3fb0a SM |
441 | router_id_set(AFI_IP6, &rid, zvrf); |
442 | ||
443 | return CMD_SUCCESS; | |
444 | } | |
445 | ||
446 | DEFUN (no_ip_router_id_in_vrf, | |
447 | no_ip_router_id_in_vrf_cmd, | |
448 | "no ip router-id [A.B.C.D]", | |
03fba42e | 449 | NO_STR |
98a3fb0a | 450 | IP_STR |
03fba42e DS |
451 | "Remove the manually configured router-id\n" |
452 | "IP address to use for router-id\n") | |
453 | { | |
0cbed951 | 454 | ZEBRA_DECLVAR_CONTEXT_VRF(vrf, zvrf); |
03fba42e DS |
455 | |
456 | struct prefix rid; | |
457 | ||
458 | rid.u.prefix4.s_addr = 0; | |
459 | rid.prefixlen = 0; | |
460 | rid.family = AF_INET; | |
461 | ||
98a3fb0a | 462 | router_id_set(AFI_IP, &rid, zvrf); |
18a6dce6 | 463 | |
d62a17ae | 464 | return CMD_SUCCESS; |
18a6dce6 | 465 | } |
466 | ||
98a3fb0a SM |
467 | ALIAS (no_ip_router_id_in_vrf, |
468 | no_router_id_in_vrf_cmd, | |
469 | "no router-id [A.B.C.D]", | |
470 | NO_STR | |
471 | "Remove the manually configured router-id\n" | |
472 | "IP address to use for router-id\n"); | |
473 | ||
474 | DEFUN (no_ipv6_router_id_in_vrf, | |
475 | no_ipv6_router_id_in_vrf_cmd, | |
476 | "no ipv6 router-id [X:X::X:X]", | |
477 | NO_STR | |
478 | IP6_STR | |
479 | "Remove the manually configured IPv6 router-id\n" | |
480 | "IPv6 address to use for router-id\n") | |
481 | { | |
0cbed951 | 482 | ZEBRA_DECLVAR_CONTEXT_VRF(vrf, zvrf); |
98a3fb0a SM |
483 | |
484 | struct prefix rid; | |
485 | ||
486 | memset(&rid, 0, sizeof(rid)); | |
487 | rid.family = AF_INET; | |
488 | ||
489 | router_id_set(AFI_IP6, &rid, zvrf); | |
490 | ||
491 | return CMD_SUCCESS; | |
492 | } | |
493 | ||
494 | DEFUN (show_ip_router_id, | |
495 | show_ip_router_id_cmd, | |
496 | "show [ip|ipv6] router-id [vrf NAME]", | |
13b01f2f | 497 | SHOW_STR |
98a3fb0a SM |
498 | IP_STR |
499 | IPV6_STR | |
13b01f2f JAG |
500 | "Show the configured router-id\n" |
501 | VRF_CMD_HELP_STR) | |
502 | { | |
98a3fb0a SM |
503 | int idx = 0; |
504 | vrf_id_t vrf_id = VRF_DEFAULT; | |
505 | struct zebra_vrf *zvrf; | |
506 | const char *vrf_name = "default"; | |
507 | char addr_name[INET6_ADDRSTRLEN]; | |
508 | int is_ipv6 = 0; | |
13b01f2f | 509 | |
98a3fb0a | 510 | is_ipv6 = argv_find(argv, argc, "ipv6", &idx); |
13b01f2f | 511 | |
98a3fb0a SM |
512 | if (argv_find(argv, argc, "NAME", &idx)) { |
513 | VRF_GET_ID(vrf_id, argv[idx]->arg, false); | |
514 | vrf_name = argv[idx]->arg; | |
515 | } | |
13b01f2f | 516 | |
9a7d1e74 | 517 | zvrf = zebra_vrf_lookup_by_id(vrf_id); |
13b01f2f | 518 | |
98a3fb0a SM |
519 | if (zvrf != NULL) { |
520 | if (is_ipv6) { | |
521 | if (router_id_v6_is_any(&zvrf->rid6_user_assigned)) | |
522 | return CMD_SUCCESS; | |
523 | inet_ntop(AF_INET6, &zvrf->rid6_user_assigned.u.prefix6, | |
524 | addr_name, sizeof(addr_name)); | |
525 | } else { | |
3a6290bd DA |
526 | if (zvrf->rid_user_assigned.u.prefix4.s_addr |
527 | == INADDR_ANY) | |
98a3fb0a SM |
528 | return CMD_SUCCESS; |
529 | inet_ntop(AF_INET, &zvrf->rid_user_assigned.u.prefix4, | |
530 | addr_name, sizeof(addr_name)); | |
531 | } | |
532 | ||
533 | vty_out(vty, "zebra:\n"); | |
534 | vty_out(vty, " router-id %s vrf %s\n", addr_name, vrf_name); | |
535 | } | |
13b01f2f | 536 | |
98a3fb0a | 537 | return CMD_SUCCESS; |
13b01f2f | 538 | } |
813d4307 | 539 | |
d62a17ae | 540 | static int router_id_cmp(void *a, void *b) |
18a6dce6 | 541 | { |
d62a17ae | 542 | const struct connected *ifa = (const struct connected *)a; |
543 | const struct connected *ifb = (const struct connected *)b; | |
18a6dce6 | 544 | |
d62a17ae | 545 | return IPV4_ADDR_CMP(&ifa->address->u.prefix4.s_addr, |
546 | &ifb->address->u.prefix4.s_addr); | |
18a6dce6 | 547 | } |
548 | ||
98a3fb0a SM |
549 | static int router_id_v6_cmp(void *a, void *b) |
550 | { | |
551 | const struct connected *ifa = (const struct connected *)a; | |
552 | const struct connected *ifb = (const struct connected *)b; | |
553 | ||
554 | return IPV6_ADDR_CMP(&ifa->address->u.prefix6, | |
555 | &ifb->address->u.prefix6); | |
556 | } | |
557 | ||
d62a17ae | 558 | void router_id_cmd_init(void) |
18a6dce6 | 559 | { |
98a3fb0a | 560 | install_element(CONFIG_NODE, &ip_router_id_cmd); |
d62a17ae | 561 | install_element(CONFIG_NODE, &router_id_cmd); |
98a3fb0a SM |
562 | install_element(CONFIG_NODE, &ipv6_router_id_cmd); |
563 | install_element(CONFIG_NODE, &no_ip_router_id_cmd); | |
d62a17ae | 564 | install_element(CONFIG_NODE, &no_router_id_cmd); |
98a3fb0a SM |
565 | install_element(CONFIG_NODE, &ip_router_id_in_vrf_cmd); |
566 | install_element(VRF_NODE, &ip_router_id_in_vrf_cmd); | |
03fba42e DS |
567 | install_element(CONFIG_NODE, &router_id_in_vrf_cmd); |
568 | install_element(VRF_NODE, &router_id_in_vrf_cmd); | |
98a3fb0a SM |
569 | install_element(CONFIG_NODE, &ipv6_router_id_in_vrf_cmd); |
570 | install_element(VRF_NODE, &ipv6_router_id_in_vrf_cmd); | |
571 | install_element(CONFIG_NODE, &no_ipv6_router_id_cmd); | |
572 | install_element(CONFIG_NODE, &no_ip_router_id_in_vrf_cmd); | |
573 | install_element(VRF_NODE, &no_ip_router_id_in_vrf_cmd); | |
03fba42e DS |
574 | install_element(CONFIG_NODE, &no_router_id_in_vrf_cmd); |
575 | install_element(VRF_NODE, &no_router_id_in_vrf_cmd); | |
98a3fb0a SM |
576 | install_element(CONFIG_NODE, &no_ipv6_router_id_in_vrf_cmd); |
577 | install_element(VRF_NODE, &no_ipv6_router_id_in_vrf_cmd); | |
578 | install_element(VIEW_NODE, &show_ip_router_id_cmd); | |
c6ffe645 FL |
579 | } |
580 | ||
d62a17ae | 581 | void router_id_init(struct zebra_vrf *zvrf) |
c6ffe645 | 582 | { |
d62a17ae | 583 | zvrf->rid_all_sorted_list = &zvrf->_rid_all_sorted_list; |
584 | zvrf->rid_lo_sorted_list = &zvrf->_rid_lo_sorted_list; | |
98a3fb0a SM |
585 | zvrf->rid6_all_sorted_list = &zvrf->_rid6_all_sorted_list; |
586 | zvrf->rid6_lo_sorted_list = &zvrf->_rid6_lo_sorted_list; | |
18a6dce6 | 587 | |
d62a17ae | 588 | memset(zvrf->rid_all_sorted_list, 0, |
589 | sizeof(zvrf->_rid_all_sorted_list)); | |
590 | memset(zvrf->rid_lo_sorted_list, 0, sizeof(zvrf->_rid_lo_sorted_list)); | |
591 | memset(&zvrf->rid_user_assigned, 0, sizeof(zvrf->rid_user_assigned)); | |
98a3fb0a SM |
592 | memset(zvrf->rid6_all_sorted_list, 0, |
593 | sizeof(zvrf->_rid6_all_sorted_list)); | |
594 | memset(zvrf->rid6_lo_sorted_list, 0, | |
595 | sizeof(zvrf->_rid6_lo_sorted_list)); | |
596 | memset(&zvrf->rid6_user_assigned, 0, sizeof(zvrf->rid6_user_assigned)); | |
18a6dce6 | 597 | |
d62a17ae | 598 | zvrf->rid_all_sorted_list->cmp = router_id_cmp; |
599 | zvrf->rid_lo_sorted_list->cmp = router_id_cmp; | |
98a3fb0a SM |
600 | zvrf->rid6_all_sorted_list->cmp = router_id_v6_cmp; |
601 | zvrf->rid6_lo_sorted_list->cmp = router_id_v6_cmp; | |
18a6dce6 | 602 | |
d62a17ae | 603 | zvrf->rid_user_assigned.family = AF_INET; |
12256b84 | 604 | zvrf->rid_user_assigned.prefixlen = IPV4_MAX_BITLEN; |
98a3fb0a | 605 | zvrf->rid6_user_assigned.family = AF_INET6; |
13ccce6e | 606 | zvrf->rid6_user_assigned.prefixlen = IPV6_MAX_BITLEN; |
18a6dce6 | 607 | } |