]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
2 | * Prefix related functions. | |
3 | * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
7 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * 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 "prefix.h" | |
dc5d0186 | 25 | #include "ipaddr.h" |
718e3744 | 26 | #include "vty.h" |
27 | #include "sockunion.h" | |
28 | #include "memory.h" | |
29 | #include "log.h" | |
7a7761d2 | 30 | #include "jhash.h" |
472878dc | 31 | #include "lib_errors.h" |
d52ec572 | 32 | #include "printfrr.h" |
74e2bd89 | 33 | #include "vxlan.h" |
6b0655a2 | 34 | |
bf8d3d6a DL |
35 | DEFINE_MTYPE_STATIC(LIB, PREFIX, "Prefix"); |
36 | DEFINE_MTYPE_STATIC(LIB, PREFIX_FLOWSPEC, "Prefix Flowspec"); | |
4a1ab8e4 | 37 | |
718e3744 | 38 | /* Maskbit. */ |
d7c0a89a QY |
39 | static const uint8_t maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, |
40 | 0xf8, 0xfc, 0xfe, 0xff}; | |
d62a17ae | 41 | |
718e3744 | 42 | /* Number of bits in prefix type. */ |
43 | #ifndef PNBBY | |
44 | #define PNBBY 8 | |
45 | #endif /* PNBBY */ | |
46 | ||
47 | #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff) | |
48 | ||
a9e08ebc | 49 | int is_zero_mac(const struct ethaddr *mac) |
69b61704 MK |
50 | { |
51 | int i = 0; | |
52 | ||
53 | for (i = 0; i < ETH_ALEN; i++) { | |
54 | if (mac->octet[i]) | |
55 | return 0; | |
56 | } | |
57 | ||
58 | return 1; | |
59 | } | |
60 | ||
c6ec0c74 KA |
61 | bool is_bcast_mac(const struct ethaddr *mac) |
62 | { | |
63 | int i = 0; | |
64 | ||
65 | for (i = 0; i < ETH_ALEN; i++) | |
66 | if (mac->octet[i] != 0xFF) | |
67 | return false; | |
68 | ||
69 | return true; | |
70 | } | |
71 | ||
72 | bool is_mcast_mac(const struct ethaddr *mac) | |
73 | { | |
74 | if ((mac->octet[0] & 0x01) == 0x01) | |
75 | return true; | |
76 | ||
77 | return false; | |
78 | } | |
79 | ||
adeb0672 | 80 | unsigned int prefix_bit(const uint8_t *prefix, const uint16_t bit_index) |
f63f06da | 81 | { |
adeb0672 QY |
82 | unsigned int offset = bit_index / 8; |
83 | unsigned int shift = 7 - (bit_index % 8); | |
d62a17ae | 84 | |
85 | return (prefix[offset] >> shift) & 1; | |
f63f06da PJ |
86 | } |
87 | ||
d62a17ae | 88 | int str2family(const char *string) |
f3ccedaa | 89 | { |
d62a17ae | 90 | if (!strcmp("ipv4", string)) |
91 | return AF_INET; | |
92 | else if (!strcmp("ipv6", string)) | |
93 | return AF_INET6; | |
94 | else if (!strcmp("ethernet", string)) | |
95 | return AF_ETHERNET; | |
b03b8898 DS |
96 | else if (!strcmp("evpn", string)) |
97 | return AF_EVPN; | |
d62a17ae | 98 | return -1; |
f3ccedaa CF |
99 | } |
100 | ||
db2fde34 PZ |
101 | const char *family2str(int family) |
102 | { | |
103 | switch (family) { | |
104 | case AF_INET: | |
105 | return "IPv4"; | |
106 | case AF_INET6: | |
107 | return "IPv6"; | |
108 | case AF_ETHERNET: | |
109 | return "Ethernet"; | |
110 | case AF_EVPN: | |
111 | return "Evpn"; | |
112 | } | |
113 | return "?"; | |
114 | } | |
115 | ||
214d8a60 | 116 | /* Address Family Identifier to Address Family converter. */ |
d62a17ae | 117 | int afi2family(afi_t afi) |
718e3744 | 118 | { |
d62a17ae | 119 | if (afi == AFI_IP) |
120 | return AF_INET; | |
121 | else if (afi == AFI_IP6) | |
122 | return AF_INET6; | |
123 | else if (afi == AFI_L2VPN) | |
124 | return AF_ETHERNET; | |
b03b8898 | 125 | /* NOTE: EVPN code should NOT use this interface. */ |
d62a17ae | 126 | return 0; |
718e3744 | 127 | } |
128 | ||
d62a17ae | 129 | afi_t family2afi(int family) |
718e3744 | 130 | { |
d62a17ae | 131 | if (family == AF_INET) |
132 | return AFI_IP; | |
133 | else if (family == AF_INET6) | |
134 | return AFI_IP6; | |
b03b8898 | 135 | else if (family == AF_ETHERNET || family == AF_EVPN) |
d62a17ae | 136 | return AFI_L2VPN; |
137 | return 0; | |
718e3744 | 138 | } |
139 | ||
d62a17ae | 140 | const char *afi2str(afi_t afi) |
32ac65d9 | 141 | { |
d62a17ae | 142 | switch (afi) { |
143 | case AFI_IP: | |
144 | return "IPv4"; | |
145 | case AFI_IP6: | |
146 | return "IPv6"; | |
147 | case AFI_L2VPN: | |
148 | return "l2vpn"; | |
149 | case AFI_MAX: | |
150 | return "bad-value"; | |
151 | default: | |
152 | break; | |
153 | } | |
154 | return NULL; | |
32ac65d9 LB |
155 | } |
156 | ||
d62a17ae | 157 | const char *safi2str(safi_t safi) |
1ec23d90 | 158 | { |
d62a17ae | 159 | switch (safi) { |
160 | case SAFI_UNICAST: | |
161 | return "unicast"; | |
162 | case SAFI_MULTICAST: | |
163 | return "multicast"; | |
164 | case SAFI_MPLS_VPN: | |
165 | return "vpn"; | |
166 | case SAFI_ENCAP: | |
167 | return "encap"; | |
168 | case SAFI_EVPN: | |
169 | return "evpn"; | |
170 | case SAFI_LABELED_UNICAST: | |
171 | return "labeled-unicast"; | |
7c40bf39 | 172 | case SAFI_FLOWSPEC: |
173 | return "flowspec"; | |
5c525538 RW |
174 | default: |
175 | return "unknown"; | |
d62a17ae | 176 | } |
1ec23d90 LB |
177 | } |
178 | ||
718e3744 | 179 | /* If n includes p prefix then return 1 else return 0. */ |
d62a17ae | 180 | int prefix_match(const struct prefix *n, const struct prefix *p) |
718e3744 | 181 | { |
d62a17ae | 182 | int offset; |
183 | int shift; | |
d7c0a89a | 184 | const uint8_t *np, *pp; |
d62a17ae | 185 | |
186 | /* If n's prefix is longer than p's one return 0. */ | |
187 | if (n->prefixlen > p->prefixlen) | |
188 | return 0; | |
189 | ||
9a14899b PG |
190 | if (n->family == AF_FLOWSPEC) { |
191 | /* prefixlen is unused. look at fs prefix len */ | |
e4552d66 PG |
192 | if (n->u.prefix_flowspec.family != |
193 | p->u.prefix_flowspec.family) | |
194 | return 0; | |
195 | ||
9a14899b PG |
196 | if (n->u.prefix_flowspec.prefixlen > |
197 | p->u.prefix_flowspec.prefixlen) | |
198 | return 0; | |
199 | ||
200 | /* Set both prefix's head pointer. */ | |
201 | np = (const uint8_t *)&n->u.prefix_flowspec.ptr; | |
202 | pp = (const uint8_t *)&p->u.prefix_flowspec.ptr; | |
203 | ||
204 | offset = n->u.prefix_flowspec.prefixlen; | |
205 | ||
206 | while (offset--) | |
207 | if (np[offset] != pp[offset]) | |
208 | return 0; | |
209 | return 1; | |
210 | } | |
211 | ||
d62a17ae | 212 | /* Set both prefix's head pointer. */ |
f0ed6bea | 213 | np = n->u.val; |
214 | pp = p->u.val; | |
d62a17ae | 215 | |
216 | offset = n->prefixlen / PNBBY; | |
217 | shift = n->prefixlen % PNBBY; | |
218 | ||
219 | if (shift) | |
220 | if (maskbit[shift] & (np[offset] ^ pp[offset])) | |
221 | return 0; | |
222 | ||
223 | while (offset--) | |
224 | if (np[offset] != pp[offset]) | |
225 | return 0; | |
226 | return 1; | |
44c69747 LK |
227 | |
228 | } | |
229 | ||
230 | /* | |
231 | * n is a type5 evpn prefix. This function tries to see if there is an | |
232 | * ip-prefix within n which matches prefix p | |
233 | * If n includes p prefix then return 1 else return 0. | |
234 | */ | |
235 | int evpn_type5_prefix_match(const struct prefix *n, const struct prefix *p) | |
236 | { | |
237 | int offset; | |
238 | int shift; | |
239 | int prefixlen; | |
240 | const uint8_t *np, *pp; | |
241 | struct prefix_evpn *evp; | |
242 | ||
243 | if (n->family != AF_EVPN) | |
244 | return 0; | |
245 | ||
246 | evp = (struct prefix_evpn *)n; | |
247 | pp = p->u.val; | |
248 | ||
249 | if ((evp->prefix.route_type != 5) || | |
250 | (p->family == AF_INET6 && !is_evpn_prefix_ipaddr_v6(evp)) || | |
251 | (p->family == AF_INET && !is_evpn_prefix_ipaddr_v4(evp)) || | |
252 | (is_evpn_prefix_ipaddr_none(evp))) | |
253 | return 0; | |
254 | ||
255 | prefixlen = evp->prefix.prefix_addr.ip_prefix_length; | |
256 | np = &evp->prefix.prefix_addr.ip.ip.addr; | |
257 | ||
258 | /* If n's prefix is longer than p's one return 0. */ | |
259 | if (prefixlen > p->prefixlen) | |
260 | return 0; | |
261 | ||
262 | offset = prefixlen / PNBBY; | |
263 | shift = prefixlen % PNBBY; | |
264 | ||
265 | if (shift) | |
266 | if (maskbit[shift] & (np[offset] ^ pp[offset])) | |
267 | return 0; | |
268 | ||
269 | while (offset--) | |
270 | if (np[offset] != pp[offset]) | |
271 | return 0; | |
272 | return 1; | |
273 | ||
3bec29ac DS |
274 | } |
275 | ||
276 | /* If n includes p then return 1 else return 0. Prefix mask is not considered */ | |
d62a17ae | 277 | int prefix_match_network_statement(const struct prefix *n, |
278 | const struct prefix *p) | |
3bec29ac | 279 | { |
d62a17ae | 280 | int offset; |
281 | int shift; | |
d7c0a89a | 282 | const uint8_t *np, *pp; |
3bec29ac | 283 | |
d62a17ae | 284 | /* Set both prefix's head pointer. */ |
f0ed6bea | 285 | np = n->u.val; |
286 | pp = p->u.val; | |
3bec29ac | 287 | |
d62a17ae | 288 | offset = n->prefixlen / PNBBY; |
289 | shift = n->prefixlen % PNBBY; | |
3bec29ac | 290 | |
d62a17ae | 291 | if (shift) |
292 | if (maskbit[shift] & (np[offset] ^ pp[offset])) | |
293 | return 0; | |
3bec29ac | 294 | |
d62a17ae | 295 | while (offset--) |
296 | if (np[offset] != pp[offset]) | |
297 | return 0; | |
298 | return 1; | |
718e3744 | 299 | } |
300 | ||
4937287f DL |
301 | #ifdef __clang_analyzer__ |
302 | #undef prefix_copy /* cf. prefix.h */ | |
303 | #endif | |
304 | ||
9c3a2171 | 305 | void prefix_copy(union prefixptr udest, union prefixconstptr usrc) |
718e3744 | 306 | { |
9c3a2171 DL |
307 | struct prefix *dest = udest.p; |
308 | const struct prefix *src = usrc.p; | |
309 | ||
d62a17ae | 310 | dest->family = src->family; |
311 | dest->prefixlen = src->prefixlen; | |
312 | ||
313 | if (src->family == AF_INET) | |
314 | dest->u.prefix4 = src->u.prefix4; | |
315 | else if (src->family == AF_INET6) | |
316 | dest->u.prefix6 = src->u.prefix6; | |
317 | else if (src->family == AF_ETHERNET) { | |
b03b8898 DS |
318 | memcpy(&dest->u.prefix_eth, &src->u.prefix_eth, |
319 | sizeof(struct ethaddr)); | |
320 | } else if (src->family == AF_EVPN) { | |
d62a17ae | 321 | memcpy(&dest->u.prefix_evpn, &src->u.prefix_evpn, |
322 | sizeof(struct evpn_addr)); | |
323 | } else if (src->family == AF_UNSPEC) { | |
324 | dest->u.lp.id = src->u.lp.id; | |
325 | dest->u.lp.adv_router = src->u.lp.adv_router; | |
9a14899b PG |
326 | } else if (src->family == AF_FLOWSPEC) { |
327 | void *temp; | |
328 | int len; | |
329 | ||
330 | len = src->u.prefix_flowspec.prefixlen; | |
331 | dest->u.prefix_flowspec.prefixlen = | |
332 | src->u.prefix_flowspec.prefixlen; | |
e4552d66 PG |
333 | dest->u.prefix_flowspec.family = |
334 | src->u.prefix_flowspec.family; | |
9a14899b PG |
335 | dest->family = src->family; |
336 | temp = XCALLOC(MTYPE_PREFIX_FLOWSPEC, len); | |
337 | dest->u.prefix_flowspec.ptr = (uintptr_t)temp; | |
338 | memcpy((void *)dest->u.prefix_flowspec.ptr, | |
339 | (void *)src->u.prefix_flowspec.ptr, len); | |
d62a17ae | 340 | } else { |
450971aa | 341 | flog_err(EC_LIB_DEVELOPMENT, |
1c50c1c0 QY |
342 | "prefix_copy(): Unknown address family %d", |
343 | src->family); | |
d62a17ae | 344 | assert(0); |
345 | } | |
718e3744 | 346 | } |
347 | ||
d62a17ae | 348 | /* |
9d24baaa | 349 | * Return 1 if the address/netmask contained in the prefix structure |
350 | * is the same, and else return 0. For this routine, 'same' requires | |
351 | * that not only the prefix length and the network part be the same, | |
352 | * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not | |
353 | * the same. Note that this routine has the same return value sense | |
354 | * as '==' (which is different from prefix_cmp). | |
355 | */ | |
9c3a2171 | 356 | int prefix_same(union prefixconstptr up1, union prefixconstptr up2) |
718e3744 | 357 | { |
9c3a2171 DL |
358 | const struct prefix *p1 = up1.p; |
359 | const struct prefix *p2 = up2.p; | |
360 | ||
d62a17ae | 361 | if ((p1 && !p2) || (!p1 && p2)) |
362 | return 0; | |
363 | ||
364 | if (!p1 && !p2) | |
365 | return 1; | |
366 | ||
367 | if (p1->family == p2->family && p1->prefixlen == p2->prefixlen) { | |
368 | if (p1->family == AF_INET) | |
19aad877 | 369 | if (IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4)) |
d62a17ae | 370 | return 1; |
371 | if (p1->family == AF_INET6) | |
372 | if (IPV6_ADDR_SAME(&p1->u.prefix6.s6_addr, | |
373 | &p2->u.prefix6.s6_addr)) | |
374 | return 1; | |
375 | if (p1->family == AF_ETHERNET) | |
b03b8898 DS |
376 | if (!memcmp(&p1->u.prefix_eth, &p2->u.prefix_eth, |
377 | sizeof(struct ethaddr))) | |
378 | return 1; | |
379 | if (p1->family == AF_EVPN) | |
d62a17ae | 380 | if (!memcmp(&p1->u.prefix_evpn, &p2->u.prefix_evpn, |
381 | sizeof(struct evpn_addr))) | |
382 | return 1; | |
9a14899b | 383 | if (p1->family == AF_FLOWSPEC) { |
e4552d66 PG |
384 | if (p1->u.prefix_flowspec.family != |
385 | p2->u.prefix_flowspec.family) | |
386 | return 0; | |
9a14899b PG |
387 | if (p1->u.prefix_flowspec.prefixlen != |
388 | p2->u.prefix_flowspec.prefixlen) | |
389 | return 0; | |
390 | if (!memcmp(&p1->u.prefix_flowspec.ptr, | |
391 | &p2->u.prefix_flowspec.ptr, | |
392 | p2->u.prefix_flowspec.prefixlen)) | |
393 | return 1; | |
394 | } | |
d62a17ae | 395 | } |
396 | return 0; | |
718e3744 | 397 | } |
398 | ||
9d24baaa | 399 | /* |
1315d74d DL |
400 | * Return -1/0/1 comparing the prefixes in a way that gives a full/linear |
401 | * order. | |
402 | * | |
403 | * Network prefixes are considered the same if the prefix lengths are equal | |
404 | * and the network parts are the same. Host bits (which are considered masked | |
9d24baaa | 405 | * by the prefix length) are not significant. Thus, 10.0.0.1/8 and |
406 | * 10.0.0.2/8 are considered equivalent by this routine. Note that | |
407 | * this routine has the same return sense as strcmp (which is different | |
408 | * from prefix_same). | |
409 | */ | |
9c3a2171 | 410 | int prefix_cmp(union prefixconstptr up1, union prefixconstptr up2) |
718e3744 | 411 | { |
9c3a2171 DL |
412 | const struct prefix *p1 = up1.p; |
413 | const struct prefix *p2 = up2.p; | |
d62a17ae | 414 | int offset; |
415 | int shift; | |
1315d74d | 416 | int i; |
718e3744 | 417 | |
d62a17ae | 418 | /* Set both prefix's head pointer. */ |
9a14899b PG |
419 | const uint8_t *pp1; |
420 | const uint8_t *pp2; | |
718e3744 | 421 | |
9a14899b | 422 | if (p1->family != p2->family) |
1315d74d | 423 | return numcmp(p1->family, p2->family); |
9a14899b PG |
424 | if (p1->family == AF_FLOWSPEC) { |
425 | pp1 = (const uint8_t *)p1->u.prefix_flowspec.ptr; | |
426 | pp2 = (const uint8_t *)p2->u.prefix_flowspec.ptr; | |
718e3744 | 427 | |
e4552d66 PG |
428 | if (p1->u.prefix_flowspec.family != |
429 | p2->u.prefix_flowspec.family) | |
430 | return 1; | |
431 | ||
9a14899b PG |
432 | if (p1->u.prefix_flowspec.prefixlen != |
433 | p2->u.prefix_flowspec.prefixlen) | |
1315d74d DL |
434 | return numcmp(p1->u.prefix_flowspec.prefixlen, |
435 | p2->u.prefix_flowspec.prefixlen); | |
9a14899b PG |
436 | |
437 | offset = p1->u.prefix_flowspec.prefixlen; | |
438 | while (offset--) | |
439 | if (pp1[offset] != pp2[offset]) | |
1315d74d | 440 | return numcmp(pp1[offset], pp2[offset]); |
9a14899b PG |
441 | return 0; |
442 | } | |
f0ed6bea | 443 | pp1 = p1->u.val; |
444 | pp2 = p2->u.val; | |
9a14899b PG |
445 | |
446 | if (p1->prefixlen != p2->prefixlen) | |
1315d74d | 447 | return numcmp(p1->prefixlen, p2->prefixlen); |
d62a17ae | 448 | offset = p1->prefixlen / PNBBY; |
449 | shift = p1->prefixlen % PNBBY; | |
718e3744 | 450 | |
1315d74d DL |
451 | i = memcmp(pp1, pp2, offset); |
452 | if (i) | |
453 | return i; | |
718e3744 | 454 | |
dd5bab0c DS |
455 | /* |
456 | * At this point offset was the same, if we have shift | |
457 | * that means we still have data to compare, if shift is | |
458 | * 0 then we are at the end of the data structure | |
459 | * and should just return, as that we will be accessing | |
460 | * memory beyond the end of the party zone | |
461 | */ | |
462 | if (shift) | |
463 | return numcmp(pp1[offset] & maskbit[shift], | |
464 | pp2[offset] & maskbit[shift]); | |
465 | ||
466 | return 0; | |
718e3744 | 467 | } |
468 | ||
17e52061 DL |
469 | /* |
470 | * Count the number of common bits in 2 prefixes. The prefix length is | |
471 | * ignored for this function; the whole prefix is compared. If the prefix | |
472 | * address families don't match, return -1; otherwise the return value is | |
473 | * in range 0 ... maximum prefix length for the address family. | |
474 | */ | |
d62a17ae | 475 | int prefix_common_bits(const struct prefix *p1, const struct prefix *p2) |
17e52061 | 476 | { |
d62a17ae | 477 | int pos, bit; |
478 | int length = 0; | |
d7c0a89a | 479 | uint8_t xor ; |
d62a17ae | 480 | |
481 | /* Set both prefix's head pointer. */ | |
f0ed6bea | 482 | const uint8_t *pp1 = p1->u.val; |
483 | const uint8_t *pp2 = p2->u.val; | |
d62a17ae | 484 | |
485 | if (p1->family == AF_INET) | |
486 | length = IPV4_MAX_BYTELEN; | |
487 | if (p1->family == AF_INET6) | |
488 | length = IPV6_MAX_BYTELEN; | |
489 | if (p1->family == AF_ETHERNET) | |
b03b8898 DS |
490 | length = ETH_ALEN; |
491 | if (p1->family == AF_EVPN) | |
d62a17ae | 492 | length = 8 * sizeof(struct evpn_addr); |
493 | ||
494 | if (p1->family != p2->family || !length) | |
495 | return -1; | |
496 | ||
497 | for (pos = 0; pos < length; pos++) | |
498 | if (pp1[pos] != pp2[pos]) | |
499 | break; | |
500 | if (pos == length) | |
501 | return pos * 8; | |
502 | ||
503 | xor = pp1[pos] ^ pp2[pos]; | |
504 | for (bit = 0; bit < 8; bit++) | |
505 | if (xor&(1 << (7 - bit))) | |
506 | break; | |
507 | ||
508 | return pos * 8 + bit; | |
17e52061 DL |
509 | } |
510 | ||
718e3744 | 511 | /* Return prefix family type string. */ |
d62a17ae | 512 | const char *prefix_family_str(const struct prefix *p) |
718e3744 | 513 | { |
d62a17ae | 514 | if (p->family == AF_INET) |
515 | return "inet"; | |
516 | if (p->family == AF_INET6) | |
517 | return "inet6"; | |
518 | if (p->family == AF_ETHERNET) | |
519 | return "ether"; | |
b03b8898 DS |
520 | if (p->family == AF_EVPN) |
521 | return "evpn"; | |
d62a17ae | 522 | return "unspec"; |
718e3744 | 523 | } |
524 | ||
525 | /* Allocate new prefix_ipv4 structure. */ | |
4d762f26 | 526 | struct prefix_ipv4 *prefix_ipv4_new(void) |
718e3744 | 527 | { |
d62a17ae | 528 | struct prefix_ipv4 *p; |
529 | ||
530 | /* Call prefix_new to allocate a full-size struct prefix to avoid | |
531 | problems | |
532 | where the struct prefix_ipv4 is cast to struct prefix and unallocated | |
533 | bytes were being referenced (e.g. in structure assignments). */ | |
534 | p = (struct prefix_ipv4 *)prefix_new(); | |
535 | p->family = AF_INET; | |
536 | return p; | |
718e3744 | 537 | } |
538 | ||
539 | /* Free prefix_ipv4 structure. */ | |
63265b5c | 540 | void prefix_ipv4_free(struct prefix_ipv4 **p) |
718e3744 | 541 | { |
63265b5c | 542 | prefix_free((struct prefix **)p); |
718e3744 | 543 | } |
544 | ||
3923b6e3 | 545 | /* If given string is valid return 1 else return 0 */ |
d62a17ae | 546 | int str2prefix_ipv4(const char *str, struct prefix_ipv4 *p) |
718e3744 | 547 | { |
d62a17ae | 548 | int ret; |
549 | int plen; | |
550 | char *pnt; | |
551 | char *cp; | |
552 | ||
553 | /* Find slash inside string. */ | |
554 | pnt = strchr(str, '/'); | |
555 | ||
556 | /* String doesn't contail slash. */ | |
557 | if (pnt == NULL) { | |
558 | /* Convert string to prefix. */ | |
8d920049 | 559 | ret = inet_pton(AF_INET, str, &p->prefix); |
d62a17ae | 560 | if (ret == 0) |
561 | return 0; | |
562 | ||
563 | /* If address doesn't contain slash we assume it host address. | |
564 | */ | |
565 | p->family = AF_INET; | |
566 | p->prefixlen = IPV4_MAX_BITLEN; | |
567 | ||
568 | return ret; | |
569 | } else { | |
570 | cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1); | |
aab9a0a0 | 571 | memcpy(cp, str, pnt - str); |
d62a17ae | 572 | *(cp + (pnt - str)) = '\0'; |
ef231ac7 | 573 | ret = inet_pton(AF_INET, cp, &p->prefix); |
d62a17ae | 574 | XFREE(MTYPE_TMP, cp); |
ef231ac7 RW |
575 | if (ret == 0) |
576 | return 0; | |
d62a17ae | 577 | |
578 | /* Get prefix length. */ | |
d7c0a89a | 579 | plen = (uint8_t)atoi(++pnt); |
936fbaef | 580 | if (plen > IPV4_MAX_BITLEN) |
d62a17ae | 581 | return 0; |
582 | ||
583 | p->family = AF_INET; | |
584 | p->prefixlen = plen; | |
585 | } | |
718e3744 | 586 | |
d62a17ae | 587 | return ret; |
718e3744 | 588 | } |
589 | ||
32ac65d9 | 590 | /* When string format is invalid return 0. */ |
d62a17ae | 591 | int str2prefix_eth(const char *str, struct prefix_eth *p) |
32ac65d9 | 592 | { |
d62a17ae | 593 | int ret = 0; |
594 | int plen = 48; | |
595 | char *pnt; | |
596 | char *cp = NULL; | |
597 | const char *str_addr = str; | |
598 | unsigned int a[6]; | |
599 | int i; | |
0f6476cc | 600 | bool slash = false; |
d62a17ae | 601 | |
3b0f6068 DL |
602 | if (!strcmp(str, "any")) { |
603 | memset(p, 0, sizeof(*p)); | |
604 | p->family = AF_ETHERNET; | |
605 | return 1; | |
606 | } | |
607 | ||
d62a17ae | 608 | /* Find slash inside string. */ |
609 | pnt = strchr(str, '/'); | |
610 | ||
611 | if (pnt) { | |
612 | /* Get prefix length. */ | |
d7c0a89a | 613 | plen = (uint8_t)atoi(++pnt); |
d62a17ae | 614 | if (plen > 48) { |
615 | ret = 0; | |
616 | goto done; | |
617 | } | |
618 | ||
619 | cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1); | |
aab9a0a0 | 620 | memcpy(cp, str, pnt - str); |
d62a17ae | 621 | *(cp + (pnt - str)) = '\0'; |
622 | ||
623 | str_addr = cp; | |
0f6476cc | 624 | slash = true; |
32ac65d9 LB |
625 | } |
626 | ||
d62a17ae | 627 | /* Convert string to prefix. */ |
628 | if (sscanf(str_addr, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, | |
629 | a + 3, a + 4, a + 5) | |
630 | != 6) { | |
631 | ret = 0; | |
632 | goto done; | |
633 | } | |
634 | for (i = 0; i < 6; ++i) { | |
635 | p->eth_addr.octet[i] = a[i] & 0xff; | |
636 | } | |
637 | p->prefixlen = plen; | |
638 | p->family = AF_ETHERNET; | |
0f6476cc DS |
639 | |
640 | /* | |
641 | * special case to allow old configurations to work | |
642 | * Since all zero's is implicitly meant to allow | |
643 | * a comparison to zero, let's assume | |
644 | */ | |
645 | if (!slash && is_zero_mac(&(p->eth_addr))) | |
996c9314 | 646 | p->prefixlen = 0; |
0f6476cc | 647 | |
d62a17ae | 648 | ret = 1; |
32ac65d9 LB |
649 | |
650 | done: | |
0a22ddfb | 651 | XFREE(MTYPE_TMP, cp); |
32ac65d9 | 652 | |
d62a17ae | 653 | return ret; |
32ac65d9 LB |
654 | } |
655 | ||
051954f5 | 656 | /* Convert masklen into IP address's netmask (network byte order). */ |
d62a17ae | 657 | void masklen2ip(const int masklen, struct in_addr *netmask) |
718e3744 | 658 | { |
d62a17ae | 659 | assert(masklen >= 0 && masklen <= IPV4_MAX_BITLEN); |
e96b3121 | 660 | |
d62a17ae | 661 | /* left shift is only defined for less than the size of the type. |
662 | * we unconditionally use long long in case the target platform | |
663 | * has defined behaviour for << 32 (or has a 64-bit left shift) */ | |
e96b3121 | 664 | |
d62a17ae | 665 | if (sizeof(unsigned long long) > 4) |
666 | netmask->s_addr = htonl(0xffffffffULL << (32 - masklen)); | |
667 | else | |
668 | netmask->s_addr = | |
669 | htonl(masklen ? 0xffffffffU << (32 - masklen) : 0); | |
718e3744 | 670 | } |
671 | ||
672 | /* Convert IP address's netmask into integer. We assume netmask is | |
f93eee44 | 673 | * sequential one. Argument netmask should be network byte order. */ |
d7c0a89a | 674 | uint8_t ip_masklen(struct in_addr netmask) |
718e3744 | 675 | { |
d62a17ae | 676 | uint32_t tmp = ~ntohl(netmask.s_addr); |
f93eee44 | 677 | |
61be6e94 QY |
678 | /* |
679 | * clz: count leading zeroes. sadly, the behaviour of this builtin is | |
680 | * undefined for a 0 argument, even though most CPUs give 32 | |
681 | */ | |
f93eee44 | 682 | return tmp ? __builtin_clz(tmp) : 32; |
718e3744 | 683 | } |
684 | ||
caff7905 | 685 | /* Apply mask to IPv4 prefix (network byte order). */ |
d62a17ae | 686 | void apply_mask_ipv4(struct prefix_ipv4 *p) |
718e3744 | 687 | { |
d62a17ae | 688 | struct in_addr mask; |
689 | masklen2ip(p->prefixlen, &mask); | |
690 | p->prefix.s_addr &= mask.s_addr; | |
718e3744 | 691 | } |
692 | ||
693 | /* If prefix is 0.0.0.0/0 then return 1 else return 0. */ | |
d62a17ae | 694 | int prefix_ipv4_any(const struct prefix_ipv4 *p) |
718e3744 | 695 | { |
975a328e | 696 | return (p->prefix.s_addr == INADDR_ANY && p->prefixlen == 0); |
718e3744 | 697 | } |
6b0655a2 | 698 | |
718e3744 | 699 | /* Allocate a new ip version 6 route */ |
d62a17ae | 700 | struct prefix_ipv6 *prefix_ipv6_new(void) |
718e3744 | 701 | { |
d62a17ae | 702 | struct prefix_ipv6 *p; |
718e3744 | 703 | |
d62a17ae | 704 | /* Allocate a full-size struct prefix to avoid problems with structure |
705 | size mismatches. */ | |
706 | p = (struct prefix_ipv6 *)prefix_new(); | |
707 | p->family = AF_INET6; | |
708 | return p; | |
718e3744 | 709 | } |
710 | ||
711 | /* Free prefix for IPv6. */ | |
63265b5c | 712 | void prefix_ipv6_free(struct prefix_ipv6 **p) |
718e3744 | 713 | { |
63265b5c | 714 | prefix_free((struct prefix **)p); |
718e3744 | 715 | } |
716 | ||
3923b6e3 | 717 | /* If given string is valid return 1 else return 0 */ |
d62a17ae | 718 | int str2prefix_ipv6(const char *str, struct prefix_ipv6 *p) |
718e3744 | 719 | { |
d62a17ae | 720 | char *pnt; |
721 | char *cp; | |
722 | int ret; | |
723 | ||
724 | pnt = strchr(str, '/'); | |
725 | ||
726 | /* If string doesn't contain `/' treat it as host route. */ | |
727 | if (pnt == NULL) { | |
728 | ret = inet_pton(AF_INET6, str, &p->prefix); | |
729 | if (ret == 0) | |
730 | return 0; | |
731 | p->prefixlen = IPV6_MAX_BITLEN; | |
732 | } else { | |
733 | int plen; | |
734 | ||
735 | cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1); | |
aab9a0a0 | 736 | memcpy(cp, str, pnt - str); |
d62a17ae | 737 | *(cp + (pnt - str)) = '\0'; |
738 | ret = inet_pton(AF_INET6, cp, &p->prefix); | |
739 | XFREE(MTYPE_TMP, cp); | |
740 | if (ret == 0) | |
741 | return 0; | |
d7c0a89a | 742 | plen = (uint8_t)atoi(++pnt); |
d62a17ae | 743 | if (plen > IPV6_MAX_BITLEN) |
744 | return 0; | |
745 | p->prefixlen = plen; | |
746 | } | |
747 | p->family = AF_INET6; | |
718e3744 | 748 | |
d62a17ae | 749 | return ret; |
718e3744 | 750 | } |
751 | ||
b04c699e | 752 | /* Convert struct in6_addr netmask into integer. |
d7c0a89a | 753 | * FIXME return uint8_t as ip_maskleni() does. */ |
d62a17ae | 754 | int ip6_masklen(struct in6_addr netmask) |
718e3744 | 755 | { |
25d86233 DL |
756 | if (netmask.s6_addr32[0] != 0xffffffffU) |
757 | return __builtin_clz(~ntohl(netmask.s6_addr32[0])); | |
758 | if (netmask.s6_addr32[1] != 0xffffffffU) | |
759 | return __builtin_clz(~ntohl(netmask.s6_addr32[1])) + 32; | |
760 | if (netmask.s6_addr32[2] != 0xffffffffU) | |
761 | return __builtin_clz(~ntohl(netmask.s6_addr32[2])) + 64; | |
762 | if (netmask.s6_addr32[3] != 0xffffffffU) | |
763 | return __builtin_clz(~ntohl(netmask.s6_addr32[3])) + 96; | |
764 | /* note __builtin_clz(0) is undefined */ | |
765 | return 128; | |
718e3744 | 766 | } |
767 | ||
d62a17ae | 768 | void masklen2ip6(const int masklen, struct in6_addr *netmask) |
718e3744 | 769 | { |
d62a17ae | 770 | assert(masklen >= 0 && masklen <= IPV6_MAX_BITLEN); |
25d86233 DL |
771 | |
772 | if (masklen == 0) { | |
773 | /* note << 32 is undefined */ | |
774 | memset(netmask, 0, sizeof(*netmask)); | |
775 | } else if (masklen <= 32) { | |
776 | netmask->s6_addr32[0] = htonl(0xffffffffU << (32 - masklen)); | |
777 | netmask->s6_addr32[1] = 0; | |
778 | netmask->s6_addr32[2] = 0; | |
779 | netmask->s6_addr32[3] = 0; | |
780 | } else if (masklen <= 64) { | |
781 | netmask->s6_addr32[0] = 0xffffffffU; | |
782 | netmask->s6_addr32[1] = htonl(0xffffffffU << (64 - masklen)); | |
783 | netmask->s6_addr32[2] = 0; | |
784 | netmask->s6_addr32[3] = 0; | |
785 | } else if (masklen <= 96) { | |
786 | netmask->s6_addr32[0] = 0xffffffffU; | |
787 | netmask->s6_addr32[1] = 0xffffffffU; | |
788 | netmask->s6_addr32[2] = htonl(0xffffffffU << (96 - masklen)); | |
789 | netmask->s6_addr32[3] = 0; | |
790 | } else { | |
791 | netmask->s6_addr32[0] = 0xffffffffU; | |
792 | netmask->s6_addr32[1] = 0xffffffffU; | |
793 | netmask->s6_addr32[2] = 0xffffffffU; | |
794 | netmask->s6_addr32[3] = htonl(0xffffffffU << (128 - masklen)); | |
795 | } | |
718e3744 | 796 | } |
797 | ||
d62a17ae | 798 | void apply_mask_ipv6(struct prefix_ipv6 *p) |
718e3744 | 799 | { |
d7c0a89a | 800 | uint8_t *pnt; |
d62a17ae | 801 | int index; |
802 | int offset; | |
8c7f49d2 | 803 | |
d62a17ae | 804 | index = p->prefixlen / 8; |
8c7f49d2 | 805 | |
d62a17ae | 806 | if (index < 16) { |
d7c0a89a | 807 | pnt = (uint8_t *)&p->prefix; |
d62a17ae | 808 | offset = p->prefixlen % 8; |
8c7f49d2 | 809 | |
d62a17ae | 810 | pnt[index] &= maskbit[offset]; |
811 | index++; | |
8c7f49d2 | 812 | |
d62a17ae | 813 | while (index < 16) |
814 | pnt[index++] = 0; | |
815 | } | |
718e3744 | 816 | } |
817 | ||
d62a17ae | 818 | void apply_mask(struct prefix *p) |
718e3744 | 819 | { |
d62a17ae | 820 | switch (p->family) { |
821 | case AF_INET: | |
822 | apply_mask_ipv4((struct prefix_ipv4 *)p); | |
823 | break; | |
824 | case AF_INET6: | |
825 | apply_mask_ipv6((struct prefix_ipv6 *)p); | |
826 | break; | |
827 | default: | |
828 | break; | |
829 | } | |
830 | return; | |
718e3744 | 831 | } |
832 | ||
b04c699e | 833 | /* Utility function of convert between struct prefix <=> union sockunion. */ |
d62a17ae | 834 | struct prefix *sockunion2hostprefix(const union sockunion *su, |
835 | struct prefix *prefix) | |
718e3744 | 836 | { |
d62a17ae | 837 | if (su->sa.sa_family == AF_INET) { |
838 | struct prefix_ipv4 *p; | |
839 | ||
840 | p = prefix ? (struct prefix_ipv4 *)prefix : prefix_ipv4_new(); | |
841 | p->family = AF_INET; | |
842 | p->prefix = su->sin.sin_addr; | |
843 | p->prefixlen = IPV4_MAX_BITLEN; | |
844 | return (struct prefix *)p; | |
845 | } | |
846 | if (su->sa.sa_family == AF_INET6) { | |
847 | struct prefix_ipv6 *p; | |
848 | ||
849 | p = prefix ? (struct prefix_ipv6 *)prefix : prefix_ipv6_new(); | |
850 | p->family = AF_INET6; | |
851 | p->prefixlen = IPV6_MAX_BITLEN; | |
852 | memcpy(&p->prefix, &su->sin6.sin6_addr, | |
853 | sizeof(struct in6_addr)); | |
854 | return (struct prefix *)p; | |
855 | } | |
856 | return NULL; | |
718e3744 | 857 | } |
858 | ||
d62a17ae | 859 | void prefix2sockunion(const struct prefix *p, union sockunion *su) |
17e52061 | 860 | { |
d62a17ae | 861 | memset(su, 0, sizeof(*su)); |
862 | ||
863 | su->sa.sa_family = p->family; | |
864 | if (p->family == AF_INET) | |
865 | su->sin.sin_addr = p->u.prefix4; | |
866 | if (p->family == AF_INET6) | |
867 | memcpy(&su->sin6.sin6_addr, &p->u.prefix6, | |
868 | sizeof(struct in6_addr)); | |
17e52061 DL |
869 | } |
870 | ||
d62a17ae | 871 | int prefix_blen(const struct prefix *p) |
718e3744 | 872 | { |
d62a17ae | 873 | switch (p->family) { |
874 | case AF_INET: | |
875 | return IPV4_MAX_BYTELEN; | |
d62a17ae | 876 | case AF_INET6: |
877 | return IPV6_MAX_BYTELEN; | |
d62a17ae | 878 | case AF_ETHERNET: |
7628d862 | 879 | return ETH_ALEN; |
d62a17ae | 880 | } |
881 | return 0; | |
718e3744 | 882 | } |
883 | ||
884 | /* Generic function for conversion string to struct prefix. */ | |
d62a17ae | 885 | int str2prefix(const char *str, struct prefix *p) |
718e3744 | 886 | { |
d62a17ae | 887 | int ret; |
718e3744 | 888 | |
c37a11ad | 889 | if (!str || !p) |
890 | return 0; | |
891 | ||
d62a17ae | 892 | /* First we try to convert string to struct prefix_ipv4. */ |
893 | ret = str2prefix_ipv4(str, (struct prefix_ipv4 *)p); | |
894 | if (ret) | |
895 | return ret; | |
718e3744 | 896 | |
d62a17ae | 897 | /* Next we try to convert string to struct prefix_ipv6. */ |
898 | ret = str2prefix_ipv6(str, (struct prefix_ipv6 *)p); | |
899 | if (ret) | |
900 | return ret; | |
718e3744 | 901 | |
d62a17ae | 902 | /* Next we try to convert string to struct prefix_eth. */ |
903 | ret = str2prefix_eth(str, (struct prefix_eth *)p); | |
904 | if (ret) | |
905 | return ret; | |
32ac65d9 | 906 | |
d62a17ae | 907 | return 0; |
718e3744 | 908 | } |
909 | ||
3714a385 | 910 | static const char *prefixevpn_ead2str(const struct prefix_evpn *p, char *str, |
911 | int size) | |
912 | { | |
f137734b | 913 | uint8_t family; |
8d78eeb5 | 914 | char buf[ESI_STR_LEN]; |
f137734b | 915 | char buf1[INET6_ADDRSTRLEN]; |
8d78eeb5 | 916 | |
f137734b | 917 | family = IS_IPADDR_V4(&p->prefix.ead_addr.ip) ? AF_INET : AF_INET6; |
8d78eeb5 PR |
918 | snprintf(str, size, "[%d]:[%u]:[%s]:[%d]:[%s]", p->prefix.route_type, |
919 | p->prefix.ead_addr.eth_tag, | |
920 | esi_to_str(&p->prefix.ead_addr.esi, buf, sizeof(buf)), | |
f137734b PR |
921 | (family == AF_INET) ? IPV4_MAX_BITLEN : IPV6_MAX_BITLEN, |
922 | inet_ntop(family, &p->prefix.ead_addr.ip.ipaddr_v4, buf1, | |
923 | sizeof(buf1))); | |
3714a385 | 924 | return str; |
925 | } | |
926 | ||
927 | static const char *prefixevpn_macip2str(const struct prefix_evpn *p, char *str, | |
928 | int size) | |
86f1ef44 | 929 | { |
d7c0a89a | 930 | uint8_t family; |
8d78eeb5 PR |
931 | char buf1[ETHER_ADDR_STRLEN]; |
932 | char buf2[PREFIX2STR_BUFFER]; | |
d62a17ae | 933 | |
3714a385 | 934 | if (is_evpn_prefix_ipaddr_none(p)) |
8d78eeb5 PR |
935 | snprintf(str, size, "[%d]:[%d]:[%d]:[%s]", p->prefix.route_type, |
936 | p->prefix.macip_addr.eth_tag, 8 * ETH_ALEN, | |
937 | prefix_mac2str(&p->prefix.macip_addr.mac, buf1, | |
938 | sizeof(buf1))); | |
3714a385 | 939 | else { |
8d78eeb5 PR |
940 | family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET : AF_INET6; |
941 | snprintf(str, size, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]", | |
942 | p->prefix.route_type, p->prefix.macip_addr.eth_tag, | |
943 | 8 * ETH_ALEN, | |
944 | prefix_mac2str(&p->prefix.macip_addr.mac, buf1, | |
945 | sizeof(buf1)), | |
946 | family == AF_INET ? IPV4_MAX_BITLEN : IPV6_MAX_BITLEN, | |
947 | inet_ntop(family, &p->prefix.macip_addr.ip.ip.addr, | |
948 | buf2, PREFIX2STR_BUFFER)); | |
d62a17ae | 949 | } |
3714a385 | 950 | return str; |
951 | } | |
952 | ||
953 | static const char *prefixevpn_imet2str(const struct prefix_evpn *p, char *str, | |
954 | int size) | |
955 | { | |
f137734b PR |
956 | uint8_t family; |
957 | char buf[INET6_ADDRSTRLEN]; | |
958 | ||
959 | family = IS_IPADDR_V4(&p->prefix.imet_addr.ip) ? AF_INET : AF_INET6; | |
8d78eeb5 PR |
960 | snprintf(str, size, "[%d]:[%d]:[%d]:[%s]", p->prefix.route_type, |
961 | p->prefix.imet_addr.eth_tag, | |
f137734b PR |
962 | (family == AF_INET) ? IPV4_MAX_BITLEN : IPV6_MAX_BITLEN, |
963 | inet_ntop(family, &p->prefix.imet_addr.ip.ipaddr_v4, buf, | |
964 | sizeof(buf))); | |
d62a17ae | 965 | |
3714a385 | 966 | return str; |
967 | } | |
968 | ||
969 | static const char *prefixevpn_es2str(const struct prefix_evpn *p, char *str, | |
970 | int size) | |
971 | { | |
f137734b | 972 | uint8_t family; |
50f74cf1 | 973 | char buf[ESI_STR_LEN]; |
f137734b | 974 | char buf1[INET6_ADDRSTRLEN]; |
50f74cf1 | 975 | |
f137734b | 976 | family = IS_IPADDR_V4(&p->prefix.es_addr.ip) ? AF_INET : AF_INET6; |
8d78eeb5 | 977 | snprintf(str, size, "[%d]:[%s]:[%d]:[%s]", p->prefix.route_type, |
50f74cf1 | 978 | esi_to_str(&p->prefix.es_addr.esi, buf, sizeof(buf)), |
f137734b PR |
979 | (family == AF_INET) ? IPV4_MAX_BITLEN : IPV6_MAX_BITLEN, |
980 | inet_ntop(family, &p->prefix.es_addr.ip.ipaddr_v4, buf1, | |
981 | sizeof(buf1))); | |
8d78eeb5 | 982 | |
3714a385 | 983 | return str; |
984 | } | |
985 | ||
986 | static const char *prefixevpn_prefix2str(const struct prefix_evpn *p, char *str, | |
987 | int size) | |
988 | { | |
f137734b PR |
989 | uint8_t family; |
990 | char buf[INET6_ADDRSTRLEN]; | |
991 | ||
992 | family = IS_IPADDR_V4(&p->prefix.prefix_addr.ip) ? AF_INET : AF_INET6; | |
8d78eeb5 | 993 | snprintf(str, size, "[%d]:[%d]:[%d]:[%s]", p->prefix.route_type, |
3714a385 | 994 | p->prefix.prefix_addr.eth_tag, |
3714a385 | 995 | p->prefix.prefix_addr.ip_prefix_length, |
f137734b PR |
996 | inet_ntop(family, &p->prefix.prefix_addr.ip.ipaddr_v4, buf, |
997 | sizeof(buf))); | |
3714a385 | 998 | return str; |
999 | } | |
1000 | ||
1001 | static const char *prefixevpn2str(const struct prefix_evpn *p, char *str, | |
1002 | int size) | |
1003 | { | |
1004 | switch (p->prefix.route_type) { | |
8d78eeb5 | 1005 | case BGP_EVPN_AD_ROUTE: |
3714a385 | 1006 | return prefixevpn_ead2str(p, str, size); |
8d78eeb5 | 1007 | case BGP_EVPN_MAC_IP_ROUTE: |
3714a385 | 1008 | return prefixevpn_macip2str(p, str, size); |
8d78eeb5 | 1009 | case BGP_EVPN_IMET_ROUTE: |
3714a385 | 1010 | return prefixevpn_imet2str(p, str, size); |
8d78eeb5 | 1011 | case BGP_EVPN_ES_ROUTE: |
3714a385 | 1012 | return prefixevpn_es2str(p, str, size); |
8d78eeb5 | 1013 | case BGP_EVPN_IP_PREFIX_ROUTE: |
3714a385 | 1014 | return prefixevpn_prefix2str(p, str, size); |
1015 | default: | |
1016 | snprintf(str, size, "Unsupported EVPN prefix"); | |
1017 | break; | |
1018 | } | |
d62a17ae | 1019 | return str; |
86f1ef44 | 1020 | } |
1021 | ||
d62a17ae | 1022 | const char *prefix2str(union prefixconstptr pu, char *str, int size) |
718e3744 | 1023 | { |
d62a17ae | 1024 | const struct prefix *p = pu.p; |
1025 | char buf[PREFIX2STR_BUFFER]; | |
ec466f65 QY |
1026 | int byte, tmp, a, b; |
1027 | bool z = false; | |
1028 | size_t l; | |
d62a17ae | 1029 | |
1030 | switch (p->family) { | |
1031 | case AF_INET: | |
1032 | case AF_INET6: | |
ec466f65 QY |
1033 | inet_ntop(p->family, &p->u.prefix, buf, sizeof(buf)); |
1034 | l = strlen(buf); | |
1035 | buf[l++] = '/'; | |
1036 | byte = p->prefixlen; | |
0e2d7076 DA |
1037 | tmp = p->prefixlen - 100; |
1038 | if (tmp >= 0) { | |
ec466f65 QY |
1039 | buf[l++] = '1'; |
1040 | z = true; | |
1041 | byte = tmp; | |
1042 | } | |
1043 | b = byte % 10; | |
1044 | a = byte / 10; | |
1045 | if (a || z) | |
1046 | buf[l++] = '0' + a; | |
1047 | buf[l++] = '0' + b; | |
1048 | buf[l] = '\0'; | |
1049 | strlcpy(str, buf, size); | |
d62a17ae | 1050 | break; |
1051 | ||
1052 | case AF_ETHERNET: | |
b03b8898 DS |
1053 | snprintf(str, size, "%s/%d", |
1054 | prefix_mac2str(&p->u.prefix_eth, buf, sizeof(buf)), | |
1055 | p->prefixlen); | |
1056 | break; | |
1057 | ||
1058 | case AF_EVPN: | |
3714a385 | 1059 | prefixevpn2str((const struct prefix_evpn *)p, str, size); |
d62a17ae | 1060 | break; |
1061 | ||
9a14899b | 1062 | case AF_FLOWSPEC: |
ec466f65 | 1063 | strlcpy(str, "FS prefix", size); |
9a14899b PG |
1064 | break; |
1065 | ||
d62a17ae | 1066 | default: |
ec466f65 | 1067 | strlcpy(str, "UNK prefix", size); |
d62a17ae | 1068 | break; |
1069 | } | |
1070 | ||
1071 | return str; | |
718e3744 | 1072 | } |
1073 | ||
c6b6b53b AK |
1074 | void prefix_mcast_inet4_dump(const char *onfail, struct in_addr addr, |
1075 | char *buf, int buf_size) | |
1076 | { | |
1077 | int save_errno = errno; | |
1078 | ||
1079 | if (addr.s_addr == INADDR_ANY) | |
9f73d2c9 | 1080 | strlcpy(buf, "*", buf_size); |
c6b6b53b AK |
1081 | else { |
1082 | if (!inet_ntop(AF_INET, &addr, buf, buf_size)) { | |
1083 | if (onfail) | |
1084 | snprintf(buf, buf_size, "%s", onfail); | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | errno = save_errno; | |
1089 | } | |
1090 | ||
1091 | const char *prefix_sg2str(const struct prefix_sg *sg, char *sg_str) | |
1092 | { | |
1093 | char src_str[INET_ADDRSTRLEN]; | |
1094 | char grp_str[INET_ADDRSTRLEN]; | |
1095 | ||
1096 | prefix_mcast_inet4_dump("<src?>", sg->src, src_str, sizeof(src_str)); | |
1097 | prefix_mcast_inet4_dump("<grp?>", sg->grp, grp_str, sizeof(grp_str)); | |
1098 | snprintf(sg_str, PREFIX_SG_STR_LEN, "(%s,%s)", src_str, grp_str); | |
1099 | ||
1100 | return sg_str; | |
1101 | } | |
1102 | ||
4d762f26 | 1103 | struct prefix *prefix_new(void) |
718e3744 | 1104 | { |
d62a17ae | 1105 | struct prefix *p; |
718e3744 | 1106 | |
0d6f7fd6 | 1107 | p = XCALLOC(MTYPE_PREFIX, sizeof(*p)); |
d62a17ae | 1108 | return p; |
718e3744 | 1109 | } |
1110 | ||
63265b5c DS |
1111 | void prefix_free_lists(void *arg) |
1112 | { | |
1113 | struct prefix *p = arg; | |
1114 | ||
1115 | prefix_free(&p); | |
1116 | } | |
1117 | ||
718e3744 | 1118 | /* Free prefix structure. */ |
63265b5c | 1119 | void prefix_free(struct prefix **p) |
718e3744 | 1120 | { |
63265b5c | 1121 | XFREE(MTYPE_PREFIX, *p); |
718e3744 | 1122 | } |
1123 | ||
718e3744 | 1124 | /* Utility function to convert ipv4 prefixes to Classful prefixes */ |
d62a17ae | 1125 | void apply_classful_mask_ipv4(struct prefix_ipv4 *p) |
718e3744 | 1126 | { |
1127 | ||
d7c0a89a | 1128 | uint32_t destination; |
d62a17ae | 1129 | |
1130 | destination = ntohl(p->prefix.s_addr); | |
1131 | ||
936fbaef | 1132 | if (p->prefixlen == IPV4_MAX_BITLEN) |
d62a17ae | 1133 | ; |
1134 | /* do nothing for host routes */ | |
1135 | else if (IN_CLASSC(destination)) { | |
1136 | p->prefixlen = 24; | |
1137 | apply_mask_ipv4(p); | |
1138 | } else if (IN_CLASSB(destination)) { | |
1139 | p->prefixlen = 16; | |
1140 | apply_mask_ipv4(p); | |
1141 | } else { | |
1142 | p->prefixlen = 8; | |
1143 | apply_mask_ipv4(p); | |
1144 | } | |
718e3744 | 1145 | } |
1146 | ||
d62a17ae | 1147 | in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen) |
3fb9cd6e | 1148 | { |
d62a17ae | 1149 | struct in_addr mask; |
1150 | ||
1151 | masklen2ip(masklen, &mask); | |
936fbaef DA |
1152 | return (masklen != IPV4_MAX_BITLEN - 1) |
1153 | ? | |
1154 | /* normal case */ | |
1155 | (hostaddr | ~mask.s_addr) | |
1156 | : | |
1157 | /* For prefix 31 return 255.255.255.255 (RFC3021) */ | |
1158 | htonl(0xFFFFFFFF); | |
3fb9cd6e | 1159 | } |
1160 | ||
d62a17ae | 1161 | /* Utility function to convert ipv4 netmask to prefixes |
718e3744 | 1162 | ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16" |
1163 | ex.) "1.0.0.0" NULL => "1.0.0.0/8" */ | |
d62a17ae | 1164 | int netmask_str2prefix_str(const char *net_str, const char *mask_str, |
7533cad7 | 1165 | char *prefix_str, size_t prefix_str_len) |
718e3744 | 1166 | { |
d62a17ae | 1167 | struct in_addr network; |
1168 | struct in_addr mask; | |
d7c0a89a QY |
1169 | uint8_t prefixlen; |
1170 | uint32_t destination; | |
d62a17ae | 1171 | int ret; |
1172 | ||
1173 | ret = inet_aton(net_str, &network); | |
1174 | if (!ret) | |
1175 | return 0; | |
1176 | ||
1177 | if (mask_str) { | |
1178 | ret = inet_aton(mask_str, &mask); | |
1179 | if (!ret) | |
1180 | return 0; | |
1181 | ||
1182 | prefixlen = ip_masklen(mask); | |
1183 | } else { | |
1184 | destination = ntohl(network.s_addr); | |
1185 | ||
975a328e | 1186 | if (network.s_addr == INADDR_ANY) |
d62a17ae | 1187 | prefixlen = 0; |
1188 | else if (IN_CLASSC(destination)) | |
1189 | prefixlen = 24; | |
1190 | else if (IN_CLASSB(destination)) | |
1191 | prefixlen = 16; | |
1192 | else if (IN_CLASSA(destination)) | |
1193 | prefixlen = 8; | |
1194 | else | |
1195 | return 0; | |
1196 | } | |
718e3744 | 1197 | |
7533cad7 | 1198 | snprintf(prefix_str, prefix_str_len, "%s/%d", net_str, prefixlen); |
718e3744 | 1199 | |
d62a17ae | 1200 | return 1; |
718e3744 | 1201 | } |
1202 | ||
c215ecaf | 1203 | /* converts to internal representation of mac address |
d62a17ae | 1204 | * returns 1 on success, 0 otherwise |
c215ecaf PG |
1205 | * format accepted: AA:BB:CC:DD:EE:FF |
1206 | * if mac parameter is null, then check only | |
1207 | */ | |
db42a173 | 1208 | int prefix_str2mac(const char *str, struct ethaddr *mac) |
c215ecaf | 1209 | { |
d62a17ae | 1210 | unsigned int a[6]; |
1211 | int i; | |
1212 | ||
1213 | if (!str) | |
1214 | return 0; | |
1215 | ||
1216 | if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, a + 3, | |
1217 | a + 4, a + 5) | |
1218 | != 6) { | |
1219 | /* error in incoming str length */ | |
1220 | return 0; | |
1221 | } | |
1222 | /* valid mac address */ | |
1223 | if (!mac) | |
1224 | return 1; | |
1225 | for (i = 0; i < 6; ++i) | |
1226 | mac->octet[i] = a[i] & 0xff; | |
1227 | return 1; | |
c215ecaf PG |
1228 | } |
1229 | ||
db42a173 | 1230 | char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size) |
c215ecaf | 1231 | { |
d62a17ae | 1232 | char *ptr; |
1233 | ||
1234 | if (!mac) | |
1235 | return NULL; | |
1236 | if (!buf) | |
9f5dc319 | 1237 | ptr = XMALLOC(MTYPE_TMP, ETHER_ADDR_STRLEN * sizeof(char)); |
d62a17ae | 1238 | else { |
1239 | assert(size >= ETHER_ADDR_STRLEN); | |
1240 | ptr = buf; | |
1241 | } | |
1242 | snprintf(ptr, (ETHER_ADDR_STRLEN), "%02x:%02x:%02x:%02x:%02x:%02x", | |
1243 | (uint8_t)mac->octet[0], (uint8_t)mac->octet[1], | |
1244 | (uint8_t)mac->octet[2], (uint8_t)mac->octet[3], | |
1245 | (uint8_t)mac->octet[4], (uint8_t)mac->octet[5]); | |
1246 | return ptr; | |
c215ecaf | 1247 | } |
7a7761d2 | 1248 | |
62b4b3b6 | 1249 | unsigned prefix_hash_key(const void *pp) |
7a7761d2 CF |
1250 | { |
1251 | struct prefix copy; | |
1252 | ||
9a14899b PG |
1253 | if (((struct prefix *)pp)->family == AF_FLOWSPEC) { |
1254 | uint32_t len; | |
1255 | void *temp; | |
1256 | ||
1257 | /* make sure *all* unused bits are zero, | |
1258 | * particularly including alignment / | |
1259 | * padding and unused prefix bytes. | |
1260 | */ | |
1261 | memset(©, 0, sizeof(copy)); | |
1262 | prefix_copy(©, (struct prefix *)pp); | |
1263 | len = jhash((void *)copy.u.prefix_flowspec.ptr, | |
1264 | copy.u.prefix_flowspec.prefixlen, | |
1265 | 0x55aa5a5a); | |
1266 | temp = (void *)copy.u.prefix_flowspec.ptr; | |
1267 | XFREE(MTYPE_PREFIX_FLOWSPEC, temp); | |
1268 | copy.u.prefix_flowspec.ptr = (uintptr_t)NULL; | |
1269 | return len; | |
1270 | } | |
7a7761d2 CF |
1271 | /* make sure *all* unused bits are zero, particularly including |
1272 | * alignment / | |
1273 | * padding and unused prefix bytes. */ | |
1274 | memset(©, 0, sizeof(copy)); | |
1275 | prefix_copy(©, (struct prefix *)pp); | |
996c9314 LB |
1276 | return jhash(©, |
1277 | offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen), | |
1278 | 0x55aa5a5a); | |
7a7761d2 | 1279 | } |
50f74cf1 | 1280 | |
1281 | /* converts to internal representation of esi | |
1282 | * returns 1 on success, 0 otherwise | |
1283 | * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa | |
1284 | * if esi parameter is null, then check only | |
1285 | */ | |
1286 | int str_to_esi(const char *str, esi_t *esi) | |
1287 | { | |
1288 | int i; | |
1289 | unsigned int a[ESI_BYTES]; | |
1290 | ||
1291 | if (!str) | |
1292 | return 0; | |
1293 | ||
1294 | if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x", | |
1295 | a + 0, a + 1, a + 2, a + 3, | |
1296 | a + 4, a + 5, a + 6, a + 7, | |
1297 | a + 8, a + 9) | |
1298 | != ESI_BYTES) { | |
1299 | /* error in incoming str length */ | |
1300 | return 0; | |
1301 | } | |
1302 | ||
1303 | /* valid ESI */ | |
1304 | if (!esi) | |
1305 | return 1; | |
1306 | for (i = 0; i < ESI_BYTES; ++i) | |
1307 | esi->val[i] = a[i] & 0xff; | |
1308 | return 1; | |
1309 | } | |
1310 | ||
1311 | char *esi_to_str(const esi_t *esi, char *buf, int size) | |
1312 | { | |
1313 | char *ptr; | |
1314 | ||
1315 | if (!esi) | |
1316 | return NULL; | |
1317 | if (!buf) | |
9f5dc319 | 1318 | ptr = XMALLOC(MTYPE_TMP, ESI_STR_LEN * sizeof(char)); |
50f74cf1 | 1319 | else { |
1320 | assert(size >= ESI_STR_LEN); | |
1321 | ptr = buf; | |
1322 | } | |
1323 | ||
1324 | snprintf(ptr, ESI_STR_LEN, | |
1325 | "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", | |
1326 | esi->val[0], esi->val[1], esi->val[2], | |
1327 | esi->val[3], esi->val[4], esi->val[5], | |
1328 | esi->val[6], esi->val[7], esi->val[8], | |
1329 | esi->val[9]); | |
1330 | return ptr; | |
1331 | } | |
d52ec572 | 1332 | |
74e2bd89 AK |
1333 | char *evpn_es_df_alg2str(uint8_t df_alg, char *buf, int buf_len) |
1334 | { | |
1335 | switch (df_alg) { | |
1336 | case EVPN_MH_DF_ALG_SERVICE_CARVING: | |
1337 | snprintf(buf, buf_len, "service-carving"); | |
1338 | break; | |
1339 | ||
1340 | case EVPN_MH_DF_ALG_HRW: | |
1341 | snprintf(buf, buf_len, "HRW"); | |
1342 | break; | |
1343 | ||
1344 | case EVPN_MH_DF_ALG_PREF: | |
1345 | snprintf(buf, buf_len, "preference"); | |
1346 | break; | |
1347 | ||
1348 | default: | |
1349 | snprintf(buf, buf_len, "unknown %u", df_alg); | |
1350 | break; | |
1351 | } | |
1352 | ||
1353 | return buf; | |
1354 | } | |
1355 | ||
bd0ab4d8 | 1356 | printfrr_ext_autoreg_p("EA", printfrr_ea) |
3ea79430 DL |
1357 | static ssize_t printfrr_ea(struct fbuf *buf, struct printfrr_eargs *ea, |
1358 | const void *ptr) | |
bd0ab4d8 DL |
1359 | { |
1360 | const struct ethaddr *mac = ptr; | |
212e04e5 | 1361 | char cbuf[ETHER_ADDR_STRLEN]; |
bd0ab4d8 | 1362 | |
212e04e5 | 1363 | if (!mac) |
eba599a3 | 1364 | return bputs(buf, "(null)"); |
8e2c653e | 1365 | |
212e04e5 DL |
1366 | /* need real length even if buffer is too short */ |
1367 | prefix_mac2str(mac, cbuf, sizeof(cbuf)); | |
1368 | return bputs(buf, cbuf); | |
bd0ab4d8 DL |
1369 | } |
1370 | ||
dc5d0186 | 1371 | printfrr_ext_autoreg_p("IA", printfrr_ia) |
3ea79430 DL |
1372 | static ssize_t printfrr_ia(struct fbuf *buf, struct printfrr_eargs *ea, |
1373 | const void *ptr) | |
dc5d0186 DL |
1374 | { |
1375 | const struct ipaddr *ipa = ptr; | |
212e04e5 | 1376 | char cbuf[INET6_ADDRSTRLEN]; |
dc5d0186 | 1377 | |
212e04e5 | 1378 | if (!ipa) |
eba599a3 | 1379 | return bputs(buf, "(null)"); |
8e2c653e | 1380 | |
212e04e5 DL |
1381 | ipaddr2str(ipa, cbuf, sizeof(cbuf)); |
1382 | return bputs(buf, cbuf); | |
dc5d0186 DL |
1383 | } |
1384 | ||
d52ec572 | 1385 | printfrr_ext_autoreg_p("I4", printfrr_i4) |
3ea79430 DL |
1386 | static ssize_t printfrr_i4(struct fbuf *buf, struct printfrr_eargs *ea, |
1387 | const void *ptr) | |
d52ec572 | 1388 | { |
212e04e5 DL |
1389 | char cbuf[INET_ADDRSTRLEN]; |
1390 | ||
1391 | if (!ptr) | |
eba599a3 | 1392 | return bputs(buf, "(null)"); |
8e2c653e | 1393 | |
212e04e5 DL |
1394 | inet_ntop(AF_INET, ptr, cbuf, sizeof(cbuf)); |
1395 | return bputs(buf, cbuf); | |
d52ec572 DL |
1396 | } |
1397 | ||
1398 | printfrr_ext_autoreg_p("I6", printfrr_i6) | |
3ea79430 DL |
1399 | static ssize_t printfrr_i6(struct fbuf *buf, struct printfrr_eargs *ea, |
1400 | const void *ptr) | |
d52ec572 | 1401 | { |
212e04e5 DL |
1402 | char cbuf[INET6_ADDRSTRLEN]; |
1403 | ||
1404 | if (!ptr) | |
eba599a3 | 1405 | return bputs(buf, "(null)"); |
8e2c653e | 1406 | |
212e04e5 DL |
1407 | inet_ntop(AF_INET6, ptr, cbuf, sizeof(cbuf)); |
1408 | return bputs(buf, cbuf); | |
d52ec572 DL |
1409 | } |
1410 | ||
1411 | printfrr_ext_autoreg_p("FX", printfrr_pfx) | |
3ea79430 DL |
1412 | static ssize_t printfrr_pfx(struct fbuf *buf, struct printfrr_eargs *ea, |
1413 | const void *ptr) | |
d52ec572 | 1414 | { |
212e04e5 DL |
1415 | char cbuf[PREFIX_STRLEN]; |
1416 | ||
1417 | if (!ptr) | |
eba599a3 | 1418 | return bputs(buf, "(null)"); |
8e2c653e | 1419 | |
212e04e5 DL |
1420 | prefix2str(ptr, cbuf, sizeof(cbuf)); |
1421 | return bputs(buf, cbuf); | |
d52ec572 DL |
1422 | } |
1423 | ||
d51f8b0f | 1424 | printfrr_ext_autoreg_p("PSG4", printfrr_psg) |
3ea79430 DL |
1425 | static ssize_t printfrr_psg(struct fbuf *buf, struct printfrr_eargs *ea, |
1426 | const void *ptr) | |
d52ec572 DL |
1427 | { |
1428 | const struct prefix_sg *sg = ptr; | |
212e04e5 | 1429 | ssize_t ret = 0; |
d52ec572 | 1430 | |
212e04e5 | 1431 | if (!sg) |
eba599a3 | 1432 | return bputs(buf, "(null)"); |
8e2c653e | 1433 | |
212e04e5 DL |
1434 | if (sg->src.s_addr == INADDR_ANY) |
1435 | ret += bputs(buf, "(*,"); | |
1436 | else | |
1437 | ret += bprintfrr(buf, "(%pI4,", &sg->src); | |
8e2c653e | 1438 | |
212e04e5 DL |
1439 | if (sg->grp.s_addr == INADDR_ANY) |
1440 | ret += bputs(buf, "*)"); | |
1441 | else | |
1442 | ret += bprintfrr(buf, "%pI4)", &sg->grp); | |
d52ec572 | 1443 | |
212e04e5 | 1444 | return ret; |
d52ec572 | 1445 | } |