]>
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; | |
861 | break; | |
862 | case AF_INET6: | |
863 | return IPV6_MAX_BYTELEN; | |
864 | break; | |
865 | case AF_ETHERNET: | |
7628d862 | 866 | return ETH_ALEN; |
b03b8898 | 867 | break; |
d62a17ae | 868 | } |
869 | return 0; | |
718e3744 | 870 | } |
871 | ||
872 | /* Generic function for conversion string to struct prefix. */ | |
d62a17ae | 873 | int str2prefix(const char *str, struct prefix *p) |
718e3744 | 874 | { |
d62a17ae | 875 | int ret; |
718e3744 | 876 | |
c37a11ad | 877 | if (!str || !p) |
878 | return 0; | |
879 | ||
d62a17ae | 880 | /* First we try to convert string to struct prefix_ipv4. */ |
881 | ret = str2prefix_ipv4(str, (struct prefix_ipv4 *)p); | |
882 | if (ret) | |
883 | return ret; | |
718e3744 | 884 | |
d62a17ae | 885 | /* Next we try to convert string to struct prefix_ipv6. */ |
886 | ret = str2prefix_ipv6(str, (struct prefix_ipv6 *)p); | |
887 | if (ret) | |
888 | return ret; | |
718e3744 | 889 | |
d62a17ae | 890 | /* Next we try to convert string to struct prefix_eth. */ |
891 | ret = str2prefix_eth(str, (struct prefix_eth *)p); | |
892 | if (ret) | |
893 | return ret; | |
32ac65d9 | 894 | |
d62a17ae | 895 | return 0; |
718e3744 | 896 | } |
897 | ||
3714a385 | 898 | static const char *prefixevpn_ead2str(const struct prefix_evpn *p, char *str, |
899 | int size) | |
900 | { | |
901 | snprintf(str, size, "Unsupported EVPN prefix"); | |
902 | return str; | |
903 | } | |
904 | ||
905 | static const char *prefixevpn_macip2str(const struct prefix_evpn *p, char *str, | |
906 | int size) | |
86f1ef44 | 907 | { |
d7c0a89a | 908 | uint8_t family; |
d62a17ae | 909 | char buf[PREFIX2STR_BUFFER]; |
910 | char buf2[ETHER_ADDR_STRLEN]; | |
911 | ||
3714a385 | 912 | if (is_evpn_prefix_ipaddr_none(p)) |
913 | snprintf(str, size, "[%d]:[%s]/%d", | |
914 | p->prefix.route_type, | |
915 | prefix_mac2str(&p->prefix.macip_addr.mac, | |
916 | buf2, sizeof(buf2)), | |
d62a17ae | 917 | p->prefixlen); |
3714a385 | 918 | else { |
919 | family = is_evpn_prefix_ipaddr_v4(p) | |
d62a17ae | 920 | ? AF_INET |
921 | : AF_INET6; | |
3714a385 | 922 | snprintf(str, size, "[%d]:[%s]:[%s]/%d", |
923 | p->prefix.route_type, | |
924 | prefix_mac2str(&p->prefix.macip_addr.mac, | |
925 | buf2, sizeof(buf2)), | |
926 | inet_ntop(family, | |
927 | &p->prefix.macip_addr.ip.ip.addr, | |
928 | buf, PREFIX2STR_BUFFER), | |
929 | p->prefixlen); | |
d62a17ae | 930 | } |
3714a385 | 931 | return str; |
932 | } | |
933 | ||
934 | static const char *prefixevpn_imet2str(const struct prefix_evpn *p, char *str, | |
935 | int size) | |
936 | { | |
937 | uint8_t family; | |
938 | char buf[PREFIX2STR_BUFFER]; | |
d62a17ae | 939 | |
3714a385 | 940 | family = is_evpn_prefix_ipaddr_v4(p) |
941 | ? AF_INET | |
942 | : AF_INET6; | |
943 | snprintf(str, size, "[%d]:[%s]/%d", p->prefix.route_type, | |
944 | inet_ntop(family, | |
945 | &p->prefix.imet_addr.ip.ip.addr, buf, | |
946 | PREFIX2STR_BUFFER), | |
947 | p->prefixlen); | |
948 | return str; | |
949 | } | |
950 | ||
951 | static const char *prefixevpn_es2str(const struct prefix_evpn *p, char *str, | |
952 | int size) | |
953 | { | |
50f74cf1 | 954 | char buf[ESI_STR_LEN]; |
955 | ||
956 | snprintf(str, size, "[%d]:[%s]:[%s]/%d", p->prefix.route_type, | |
957 | esi_to_str(&p->prefix.es_addr.esi, buf, sizeof(buf)), | |
958 | inet_ntoa(p->prefix.es_addr.ip.ipaddr_v4), | |
959 | p->prefixlen); | |
3714a385 | 960 | return str; |
961 | } | |
962 | ||
963 | static const char *prefixevpn_prefix2str(const struct prefix_evpn *p, char *str, | |
964 | int size) | |
965 | { | |
966 | uint8_t family; | |
967 | char buf[PREFIX2STR_BUFFER]; | |
968 | ||
969 | family = is_evpn_prefix_ipaddr_v4(p) | |
970 | ? AF_INET | |
971 | : AF_INET6; | |
972 | snprintf(str, size, "[%d]:[%u][%s/%d]/%d", | |
973 | p->prefix.route_type, | |
974 | p->prefix.prefix_addr.eth_tag, | |
975 | inet_ntop(family, | |
976 | &p->prefix.prefix_addr.ip.ip.addr, buf, | |
977 | PREFIX2STR_BUFFER), | |
978 | p->prefix.prefix_addr.ip_prefix_length, | |
979 | p->prefixlen); | |
980 | return str; | |
981 | } | |
982 | ||
983 | static const char *prefixevpn2str(const struct prefix_evpn *p, char *str, | |
984 | int size) | |
985 | { | |
986 | switch (p->prefix.route_type) { | |
987 | case 1: | |
988 | return prefixevpn_ead2str(p, str, size); | |
989 | case 2: | |
990 | return prefixevpn_macip2str(p, str, size); | |
991 | case 3: | |
992 | return prefixevpn_imet2str(p, str, size); | |
993 | case 4: | |
994 | return prefixevpn_es2str(p, str, size); | |
995 | case 5: | |
996 | return prefixevpn_prefix2str(p, str, size); | |
997 | default: | |
998 | snprintf(str, size, "Unsupported EVPN prefix"); | |
999 | break; | |
1000 | } | |
d62a17ae | 1001 | return str; |
86f1ef44 | 1002 | } |
1003 | ||
d62a17ae | 1004 | const char *prefix2str(union prefixconstptr pu, char *str, int size) |
718e3744 | 1005 | { |
d62a17ae | 1006 | const struct prefix *p = pu.p; |
1007 | char buf[PREFIX2STR_BUFFER]; | |
ec466f65 QY |
1008 | int byte, tmp, a, b; |
1009 | bool z = false; | |
1010 | size_t l; | |
d62a17ae | 1011 | |
1012 | switch (p->family) { | |
1013 | case AF_INET: | |
1014 | case AF_INET6: | |
ec466f65 QY |
1015 | inet_ntop(p->family, &p->u.prefix, buf, sizeof(buf)); |
1016 | l = strlen(buf); | |
1017 | buf[l++] = '/'; | |
1018 | byte = p->prefixlen; | |
1019 | if ((tmp = p->prefixlen - 100) >= 0) { | |
1020 | buf[l++] = '1'; | |
1021 | z = true; | |
1022 | byte = tmp; | |
1023 | } | |
1024 | b = byte % 10; | |
1025 | a = byte / 10; | |
1026 | if (a || z) | |
1027 | buf[l++] = '0' + a; | |
1028 | buf[l++] = '0' + b; | |
1029 | buf[l] = '\0'; | |
1030 | strlcpy(str, buf, size); | |
d62a17ae | 1031 | break; |
1032 | ||
1033 | case AF_ETHERNET: | |
b03b8898 DS |
1034 | snprintf(str, size, "%s/%d", |
1035 | prefix_mac2str(&p->u.prefix_eth, buf, sizeof(buf)), | |
1036 | p->prefixlen); | |
1037 | break; | |
1038 | ||
1039 | case AF_EVPN: | |
3714a385 | 1040 | prefixevpn2str((const struct prefix_evpn *)p, str, size); |
d62a17ae | 1041 | break; |
1042 | ||
9a14899b | 1043 | case AF_FLOWSPEC: |
ec466f65 | 1044 | strlcpy(str, "FS prefix", size); |
9a14899b PG |
1045 | break; |
1046 | ||
d62a17ae | 1047 | default: |
ec466f65 | 1048 | strlcpy(str, "UNK prefix", size); |
d62a17ae | 1049 | break; |
1050 | } | |
1051 | ||
1052 | return str; | |
718e3744 | 1053 | } |
1054 | ||
c6b6b53b AK |
1055 | void prefix_mcast_inet4_dump(const char *onfail, struct in_addr addr, |
1056 | char *buf, int buf_size) | |
1057 | { | |
1058 | int save_errno = errno; | |
1059 | ||
1060 | if (addr.s_addr == INADDR_ANY) | |
9f73d2c9 | 1061 | strlcpy(buf, "*", buf_size); |
c6b6b53b AK |
1062 | else { |
1063 | if (!inet_ntop(AF_INET, &addr, buf, buf_size)) { | |
1064 | if (onfail) | |
1065 | snprintf(buf, buf_size, "%s", onfail); | |
1066 | } | |
1067 | } | |
1068 | ||
1069 | errno = save_errno; | |
1070 | } | |
1071 | ||
1072 | const char *prefix_sg2str(const struct prefix_sg *sg, char *sg_str) | |
1073 | { | |
1074 | char src_str[INET_ADDRSTRLEN]; | |
1075 | char grp_str[INET_ADDRSTRLEN]; | |
1076 | ||
1077 | prefix_mcast_inet4_dump("<src?>", sg->src, src_str, sizeof(src_str)); | |
1078 | prefix_mcast_inet4_dump("<grp?>", sg->grp, grp_str, sizeof(grp_str)); | |
1079 | snprintf(sg_str, PREFIX_SG_STR_LEN, "(%s,%s)", src_str, grp_str); | |
1080 | ||
1081 | return sg_str; | |
1082 | } | |
1083 | ||
4d762f26 | 1084 | struct prefix *prefix_new(void) |
718e3744 | 1085 | { |
d62a17ae | 1086 | struct prefix *p; |
718e3744 | 1087 | |
d62a17ae | 1088 | p = XCALLOC(MTYPE_PREFIX, sizeof *p); |
1089 | return p; | |
718e3744 | 1090 | } |
1091 | ||
63265b5c DS |
1092 | void prefix_free_lists(void *arg) |
1093 | { | |
1094 | struct prefix *p = arg; | |
1095 | ||
1096 | prefix_free(&p); | |
1097 | } | |
1098 | ||
718e3744 | 1099 | /* Free prefix structure. */ |
63265b5c | 1100 | void prefix_free(struct prefix **p) |
718e3744 | 1101 | { |
63265b5c | 1102 | XFREE(MTYPE_PREFIX, *p); |
718e3744 | 1103 | } |
1104 | ||
718e3744 | 1105 | /* Utility function to convert ipv4 prefixes to Classful prefixes */ |
d62a17ae | 1106 | void apply_classful_mask_ipv4(struct prefix_ipv4 *p) |
718e3744 | 1107 | { |
1108 | ||
d7c0a89a | 1109 | uint32_t destination; |
d62a17ae | 1110 | |
1111 | destination = ntohl(p->prefix.s_addr); | |
1112 | ||
1113 | if (p->prefixlen == IPV4_MAX_PREFIXLEN) | |
1114 | ; | |
1115 | /* do nothing for host routes */ | |
1116 | else if (IN_CLASSC(destination)) { | |
1117 | p->prefixlen = 24; | |
1118 | apply_mask_ipv4(p); | |
1119 | } else if (IN_CLASSB(destination)) { | |
1120 | p->prefixlen = 16; | |
1121 | apply_mask_ipv4(p); | |
1122 | } else { | |
1123 | p->prefixlen = 8; | |
1124 | apply_mask_ipv4(p); | |
1125 | } | |
718e3744 | 1126 | } |
1127 | ||
d62a17ae | 1128 | in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen) |
3fb9cd6e | 1129 | { |
d62a17ae | 1130 | struct in_addr mask; |
1131 | ||
1132 | masklen2ip(masklen, &mask); | |
1133 | return (masklen != IPV4_MAX_PREFIXLEN - 1) ? | |
2d48474e TH |
1134 | /* normal case */ |
1135 | (hostaddr | ~mask.s_addr) | |
1136 | : | |
1137 | /* For prefix 31 return 255.255.255.255 (RFC3021) */ | |
1138 | htonl(0xFFFFFFFF); | |
3fb9cd6e | 1139 | } |
1140 | ||
d62a17ae | 1141 | /* Utility function to convert ipv4 netmask to prefixes |
718e3744 | 1142 | ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16" |
1143 | ex.) "1.0.0.0" NULL => "1.0.0.0/8" */ | |
d62a17ae | 1144 | int netmask_str2prefix_str(const char *net_str, const char *mask_str, |
1145 | char *prefix_str) | |
718e3744 | 1146 | { |
d62a17ae | 1147 | struct in_addr network; |
1148 | struct in_addr mask; | |
d7c0a89a QY |
1149 | uint8_t prefixlen; |
1150 | uint32_t destination; | |
d62a17ae | 1151 | int ret; |
1152 | ||
1153 | ret = inet_aton(net_str, &network); | |
1154 | if (!ret) | |
1155 | return 0; | |
1156 | ||
1157 | if (mask_str) { | |
1158 | ret = inet_aton(mask_str, &mask); | |
1159 | if (!ret) | |
1160 | return 0; | |
1161 | ||
1162 | prefixlen = ip_masklen(mask); | |
1163 | } else { | |
1164 | destination = ntohl(network.s_addr); | |
1165 | ||
975a328e | 1166 | if (network.s_addr == INADDR_ANY) |
d62a17ae | 1167 | prefixlen = 0; |
1168 | else if (IN_CLASSC(destination)) | |
1169 | prefixlen = 24; | |
1170 | else if (IN_CLASSB(destination)) | |
1171 | prefixlen = 16; | |
1172 | else if (IN_CLASSA(destination)) | |
1173 | prefixlen = 8; | |
1174 | else | |
1175 | return 0; | |
1176 | } | |
718e3744 | 1177 | |
d62a17ae | 1178 | sprintf(prefix_str, "%s/%d", net_str, prefixlen); |
718e3744 | 1179 | |
d62a17ae | 1180 | return 1; |
718e3744 | 1181 | } |
1182 | ||
5920990f | 1183 | /* Utility function for making IPv6 address string. */ |
d62a17ae | 1184 | const char *inet6_ntoa(struct in6_addr addr) |
5920990f | 1185 | { |
d62a17ae | 1186 | static char buf[INET6_ADDRSTRLEN]; |
5920990f | 1187 | |
d62a17ae | 1188 | inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN); |
1189 | return buf; | |
5920990f | 1190 | } |
c215ecaf | 1191 | |
c215ecaf | 1192 | /* converts to internal representation of mac address |
d62a17ae | 1193 | * returns 1 on success, 0 otherwise |
c215ecaf PG |
1194 | * format accepted: AA:BB:CC:DD:EE:FF |
1195 | * if mac parameter is null, then check only | |
1196 | */ | |
db42a173 | 1197 | int prefix_str2mac(const char *str, struct ethaddr *mac) |
c215ecaf | 1198 | { |
d62a17ae | 1199 | unsigned int a[6]; |
1200 | int i; | |
1201 | ||
1202 | if (!str) | |
1203 | return 0; | |
1204 | ||
1205 | if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, a + 3, | |
1206 | a + 4, a + 5) | |
1207 | != 6) { | |
1208 | /* error in incoming str length */ | |
1209 | return 0; | |
1210 | } | |
1211 | /* valid mac address */ | |
1212 | if (!mac) | |
1213 | return 1; | |
1214 | for (i = 0; i < 6; ++i) | |
1215 | mac->octet[i] = a[i] & 0xff; | |
1216 | return 1; | |
c215ecaf PG |
1217 | } |
1218 | ||
db42a173 | 1219 | char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size) |
c215ecaf | 1220 | { |
d62a17ae | 1221 | char *ptr; |
1222 | ||
1223 | if (!mac) | |
1224 | return NULL; | |
1225 | if (!buf) | |
9f5dc319 | 1226 | ptr = XMALLOC(MTYPE_TMP, ETHER_ADDR_STRLEN * sizeof(char)); |
d62a17ae | 1227 | else { |
1228 | assert(size >= ETHER_ADDR_STRLEN); | |
1229 | ptr = buf; | |
1230 | } | |
1231 | snprintf(ptr, (ETHER_ADDR_STRLEN), "%02x:%02x:%02x:%02x:%02x:%02x", | |
1232 | (uint8_t)mac->octet[0], (uint8_t)mac->octet[1], | |
1233 | (uint8_t)mac->octet[2], (uint8_t)mac->octet[3], | |
1234 | (uint8_t)mac->octet[4], (uint8_t)mac->octet[5]); | |
1235 | return ptr; | |
c215ecaf | 1236 | } |
7a7761d2 | 1237 | |
62b4b3b6 | 1238 | unsigned prefix_hash_key(const void *pp) |
7a7761d2 CF |
1239 | { |
1240 | struct prefix copy; | |
1241 | ||
9a14899b PG |
1242 | if (((struct prefix *)pp)->family == AF_FLOWSPEC) { |
1243 | uint32_t len; | |
1244 | void *temp; | |
1245 | ||
1246 | /* make sure *all* unused bits are zero, | |
1247 | * particularly including alignment / | |
1248 | * padding and unused prefix bytes. | |
1249 | */ | |
1250 | memset(©, 0, sizeof(copy)); | |
1251 | prefix_copy(©, (struct prefix *)pp); | |
1252 | len = jhash((void *)copy.u.prefix_flowspec.ptr, | |
1253 | copy.u.prefix_flowspec.prefixlen, | |
1254 | 0x55aa5a5a); | |
1255 | temp = (void *)copy.u.prefix_flowspec.ptr; | |
1256 | XFREE(MTYPE_PREFIX_FLOWSPEC, temp); | |
1257 | copy.u.prefix_flowspec.ptr = (uintptr_t)NULL; | |
1258 | return len; | |
1259 | } | |
7a7761d2 CF |
1260 | /* make sure *all* unused bits are zero, particularly including |
1261 | * alignment / | |
1262 | * padding and unused prefix bytes. */ | |
1263 | memset(©, 0, sizeof(copy)); | |
1264 | prefix_copy(©, (struct prefix *)pp); | |
996c9314 LB |
1265 | return jhash(©, |
1266 | offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen), | |
1267 | 0x55aa5a5a); | |
7a7761d2 | 1268 | } |
50f74cf1 | 1269 | |
1270 | /* converts to internal representation of esi | |
1271 | * returns 1 on success, 0 otherwise | |
1272 | * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa | |
1273 | * if esi parameter is null, then check only | |
1274 | */ | |
1275 | int str_to_esi(const char *str, esi_t *esi) | |
1276 | { | |
1277 | int i; | |
1278 | unsigned int a[ESI_BYTES]; | |
1279 | ||
1280 | if (!str) | |
1281 | return 0; | |
1282 | ||
1283 | if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x", | |
1284 | a + 0, a + 1, a + 2, a + 3, | |
1285 | a + 4, a + 5, a + 6, a + 7, | |
1286 | a + 8, a + 9) | |
1287 | != ESI_BYTES) { | |
1288 | /* error in incoming str length */ | |
1289 | return 0; | |
1290 | } | |
1291 | ||
1292 | /* valid ESI */ | |
1293 | if (!esi) | |
1294 | return 1; | |
1295 | for (i = 0; i < ESI_BYTES; ++i) | |
1296 | esi->val[i] = a[i] & 0xff; | |
1297 | return 1; | |
1298 | } | |
1299 | ||
1300 | char *esi_to_str(const esi_t *esi, char *buf, int size) | |
1301 | { | |
1302 | char *ptr; | |
1303 | ||
1304 | if (!esi) | |
1305 | return NULL; | |
1306 | if (!buf) | |
9f5dc319 | 1307 | ptr = XMALLOC(MTYPE_TMP, ESI_STR_LEN * sizeof(char)); |
50f74cf1 | 1308 | else { |
1309 | assert(size >= ESI_STR_LEN); | |
1310 | ptr = buf; | |
1311 | } | |
1312 | ||
1313 | snprintf(ptr, ESI_STR_LEN, | |
1314 | "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", | |
1315 | esi->val[0], esi->val[1], esi->val[2], | |
1316 | esi->val[3], esi->val[4], esi->val[5], | |
1317 | esi->val[6], esi->val[7], esi->val[8], | |
1318 | esi->val[9]); | |
1319 | return ptr; | |
1320 | } | |
d52ec572 DL |
1321 | |
1322 | printfrr_ext_autoreg_p("I4", printfrr_i4) | |
1323 | static ssize_t printfrr_i4(char *buf, size_t bsz, const char *fmt, | |
1324 | int prec, const void *ptr) | |
1325 | { | |
1326 | inet_ntop(AF_INET, ptr, buf, bsz); | |
1327 | return 2; | |
1328 | } | |
1329 | ||
1330 | printfrr_ext_autoreg_p("I6", printfrr_i6) | |
1331 | static ssize_t printfrr_i6(char *buf, size_t bsz, const char *fmt, | |
1332 | int prec, const void *ptr) | |
1333 | { | |
1334 | inet_ntop(AF_INET6, ptr, buf, bsz); | |
1335 | return 2; | |
1336 | } | |
1337 | ||
1338 | printfrr_ext_autoreg_p("FX", printfrr_pfx) | |
1339 | static ssize_t printfrr_pfx(char *buf, size_t bsz, const char *fmt, | |
1340 | int prec, const void *ptr) | |
1341 | { | |
1342 | prefix2str(ptr, buf, bsz); | |
1343 | return 2; | |
1344 | } | |
1345 | ||
1346 | printfrr_ext_autoreg_p("SG4", printfrr_psg) | |
1347 | static ssize_t printfrr_psg(char *buf, size_t bsz, const char *fmt, | |
1348 | int prec, const void *ptr) | |
1349 | { | |
1350 | const struct prefix_sg *sg = ptr; | |
1351 | struct fbuf fb = { .buf = buf, .pos = buf, .len = bsz - 1 }; | |
1352 | ||
1353 | if (sg->src.s_addr == INADDR_ANY) | |
1354 | bprintfrr(&fb, "(*,"); | |
1355 | else | |
1356 | bprintfrr(&fb, "(%pI4,", &sg->src); | |
1357 | ||
1358 | if (sg->grp.s_addr == INADDR_ANY) | |
1359 | bprintfrr(&fb, "*)"); | |
1360 | else | |
1361 | bprintfrr(&fb, "%pI4)", &sg->grp); | |
1362 | ||
1363 | fb.pos[0] = '\0'; | |
1364 | return 3; | |
1365 | } |