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