]> git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.c
Merge pull request #5806 from ton31337/fix/remove_break_after_return
[mirror_frr.git] / lib / prefix.c
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 *
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
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"
29 #include "jhash.h"
30 #include "lib_errors.h"
31 #include "printfrr.h"
32
33 DEFINE_MTYPE_STATIC(LIB, PREFIX, "Prefix")
34 DEFINE_MTYPE_STATIC(LIB, PREFIX_FLOWSPEC, "Prefix Flowspec")
35
36 /* Maskbit. */
37 static const uint8_t maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
38 0xf8, 0xfc, 0xfe, 0xff};
39
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
47 int is_zero_mac(const struct ethaddr *mac)
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
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
78 unsigned int prefix_bit(const uint8_t *prefix, const uint16_t prefixlen)
79 {
80 unsigned int offset = prefixlen / 8;
81 unsigned int shift = 7 - (prefixlen % 8);
82
83 return (prefix[offset] >> shift) & 1;
84 }
85
86 int str2family(const char *string)
87 {
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;
94 else if (!strcmp("evpn", string))
95 return AF_EVPN;
96 return -1;
97 }
98
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
114 /* Address Famiy Identifier to Address Family converter. */
115 int afi2family(afi_t afi)
116 {
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;
123 /* NOTE: EVPN code should NOT use this interface. */
124 return 0;
125 }
126
127 afi_t family2afi(int family)
128 {
129 if (family == AF_INET)
130 return AFI_IP;
131 else if (family == AF_INET6)
132 return AFI_IP6;
133 else if (family == AF_ETHERNET || family == AF_EVPN)
134 return AFI_L2VPN;
135 return 0;
136 }
137
138 const char *afi2str(afi_t afi)
139 {
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;
153 }
154
155 const char *safi2str(safi_t safi)
156 {
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";
170 case SAFI_FLOWSPEC:
171 return "flowspec";
172 default:
173 return "unknown";
174 }
175 }
176
177 /* If n includes p prefix then return 1 else return 0. */
178 int prefix_match(const struct prefix *n, const struct prefix *p)
179 {
180 int offset;
181 int shift;
182 const uint8_t *np, *pp;
183
184 /* If n's prefix is longer than p's one return 0. */
185 if (n->prefixlen > p->prefixlen)
186 return 0;
187
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
206 /* Set both prefix's head pointer. */
207 np = n->u.val;
208 pp = p->u.val;
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;
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
268 }
269
270 /* If n includes p then return 1 else return 0. Prefix mask is not considered */
271 int prefix_match_network_statement(const struct prefix *n,
272 const struct prefix *p)
273 {
274 int offset;
275 int shift;
276 const uint8_t *np, *pp;
277
278 /* Set both prefix's head pointer. */
279 np = n->u.val;
280 pp = p->u.val;
281
282 offset = n->prefixlen / PNBBY;
283 shift = n->prefixlen % PNBBY;
284
285 if (shift)
286 if (maskbit[shift] & (np[offset] ^ pp[offset]))
287 return 0;
288
289 while (offset--)
290 if (np[offset] != pp[offset])
291 return 0;
292 return 1;
293 }
294
295 #ifdef __clang_analyzer__
296 #undef prefix_copy /* cf. prefix.h */
297 #endif
298
299 void prefix_copy(union prefixptr udest, union prefixconstptr usrc)
300 {
301 struct prefix *dest = udest.p;
302 const struct prefix *src = usrc.p;
303
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) {
312 memcpy(&dest->u.prefix_eth, &src->u.prefix_eth,
313 sizeof(struct ethaddr));
314 } else if (src->family == AF_EVPN) {
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;
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);
332 } else {
333 flog_err(EC_LIB_DEVELOPMENT,
334 "prefix_copy(): Unknown address family %d",
335 src->family);
336 assert(0);
337 }
338 }
339
340 /*
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 */
348 int prefix_same(union prefixconstptr up1, union prefixconstptr up2)
349 {
350 const struct prefix *p1 = up1.p;
351 const struct prefix *p2 = up2.p;
352
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)
361 if (IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
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)
368 if (!memcmp(&p1->u.prefix_eth, &p2->u.prefix_eth,
369 sizeof(struct ethaddr)))
370 return 1;
371 if (p1->family == AF_EVPN)
372 if (!memcmp(&p1->u.prefix_evpn, &p2->u.prefix_evpn,
373 sizeof(struct evpn_addr)))
374 return 1;
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 }
384 }
385 return 0;
386 }
387
388 /*
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
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 */
399 int prefix_cmp(union prefixconstptr up1, union prefixconstptr up2)
400 {
401 const struct prefix *p1 = up1.p;
402 const struct prefix *p2 = up2.p;
403 int offset;
404 int shift;
405 int i;
406
407 /* Set both prefix's head pointer. */
408 const uint8_t *pp1;
409 const uint8_t *pp2;
410
411 if (p1->family != p2->family)
412 return numcmp(p1->family, p2->family);
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;
416
417 if (p1->u.prefix_flowspec.prefixlen !=
418 p2->u.prefix_flowspec.prefixlen)
419 return numcmp(p1->u.prefix_flowspec.prefixlen,
420 p2->u.prefix_flowspec.prefixlen);
421
422 offset = p1->u.prefix_flowspec.prefixlen;
423 while (offset--)
424 if (pp1[offset] != pp2[offset])
425 return numcmp(pp1[offset], pp2[offset]);
426 return 0;
427 }
428 pp1 = p1->u.val;
429 pp2 = p2->u.val;
430
431 if (p1->prefixlen != p2->prefixlen)
432 return numcmp(p1->prefixlen, p2->prefixlen);
433 offset = p1->prefixlen / PNBBY;
434 shift = p1->prefixlen % PNBBY;
435
436 i = memcmp(pp1, pp2, offset);
437 if (i)
438 return i;
439
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;
452 }
453
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 */
460 int prefix_common_bits(const struct prefix *p1, const struct prefix *p2)
461 {
462 int pos, bit;
463 int length = 0;
464 uint8_t xor ;
465
466 /* Set both prefix's head pointer. */
467 const uint8_t *pp1 = p1->u.val;
468 const uint8_t *pp2 = p2->u.val;
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)
475 length = ETH_ALEN;
476 if (p1->family == AF_EVPN)
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;
494 }
495
496 /* Return prefix family type string. */
497 const char *prefix_family_str(const struct prefix *p)
498 {
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";
505 if (p->family == AF_EVPN)
506 return "evpn";
507 return "unspec";
508 }
509
510 /* Allocate new prefix_ipv4 structure. */
511 struct prefix_ipv4 *prefix_ipv4_new(void)
512 {
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;
522 }
523
524 /* Free prefix_ipv4 structure. */
525 void prefix_ipv4_free(struct prefix_ipv4 **p)
526 {
527 prefix_free((struct prefix **)p);
528 }
529
530 /* If given string is valid return 1 else return 0 */
531 int str2prefix_ipv4(const char *str, struct prefix_ipv4 *p)
532 {
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. */
544 ret = inet_pton(AF_INET, str, &p->prefix);
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);
556 memcpy(cp, str, pnt - str);
557 *(cp + (pnt - str)) = '\0';
558 ret = inet_pton(AF_INET, cp, &p->prefix);
559 XFREE(MTYPE_TMP, cp);
560 if (ret == 0)
561 return 0;
562
563 /* Get prefix length. */
564 plen = (uint8_t)atoi(++pnt);
565 if (plen > IPV4_MAX_PREFIXLEN)
566 return 0;
567
568 p->family = AF_INET;
569 p->prefixlen = plen;
570 }
571
572 return ret;
573 }
574
575 /* When string format is invalid return 0. */
576 int str2prefix_eth(const char *str, struct prefix_eth *p)
577 {
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;
585 bool slash = false;
586
587 if (!strcmp(str, "any")) {
588 memset(p, 0, sizeof(*p));
589 p->family = AF_ETHERNET;
590 return 1;
591 }
592
593 /* Find slash inside string. */
594 pnt = strchr(str, '/');
595
596 if (pnt) {
597 /* Get prefix length. */
598 plen = (uint8_t)atoi(++pnt);
599 if (plen > 48) {
600 ret = 0;
601 goto done;
602 }
603
604 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
605 memcpy(cp, str, pnt - str);
606 *(cp + (pnt - str)) = '\0';
607
608 str_addr = cp;
609 slash = true;
610 }
611
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;
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)))
631 p->prefixlen = 0;
632
633 ret = 1;
634
635 done:
636 XFREE(MTYPE_TMP, cp);
637
638 return ret;
639 }
640
641 /* Convert masklen into IP address's netmask (network byte order). */
642 void masklen2ip(const int masklen, struct in_addr *netmask)
643 {
644 assert(masklen >= 0 && masklen <= IPV4_MAX_BITLEN);
645
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) */
649
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);
655 }
656
657 /* Convert IP address's netmask into integer. We assume netmask is
658 * sequential one. Argument netmask should be network byte order. */
659 uint8_t ip_masklen(struct in_addr netmask)
660 {
661 uint32_t tmp = ~ntohl(netmask.s_addr);
662
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 */
667 return tmp ? __builtin_clz(tmp) : 32;
668 }
669
670 /* Apply mask to IPv4 prefix (network byte order). */
671 void apply_mask_ipv4(struct prefix_ipv4 *p)
672 {
673 struct in_addr mask;
674 masklen2ip(p->prefixlen, &mask);
675 p->prefix.s_addr &= mask.s_addr;
676 }
677
678 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
679 int prefix_ipv4_any(const struct prefix_ipv4 *p)
680 {
681 return (p->prefix.s_addr == INADDR_ANY && p->prefixlen == 0);
682 }
683
684 /* Allocate a new ip version 6 route */
685 struct prefix_ipv6 *prefix_ipv6_new(void)
686 {
687 struct prefix_ipv6 *p;
688
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;
694 }
695
696 /* Free prefix for IPv6. */
697 void prefix_ipv6_free(struct prefix_ipv6 **p)
698 {
699 prefix_free((struct prefix **)p);
700 }
701
702 /* If given string is valid return 1 else return 0 */
703 int str2prefix_ipv6(const char *str, struct prefix_ipv6 *p)
704 {
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);
721 memcpy(cp, str, pnt - str);
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;
727 plen = (uint8_t)atoi(++pnt);
728 if (plen > IPV6_MAX_BITLEN)
729 return 0;
730 p->prefixlen = plen;
731 }
732 p->family = AF_INET6;
733
734 return ret;
735 }
736
737 /* Convert struct in6_addr netmask into integer.
738 * FIXME return uint8_t as ip_maskleni() does. */
739 int ip6_masklen(struct in6_addr netmask)
740 {
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;
751 }
752
753 void masklen2ip6(const int masklen, struct in6_addr *netmask)
754 {
755 assert(masklen >= 0 && masklen <= IPV6_MAX_BITLEN);
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 }
781 }
782
783 void apply_mask_ipv6(struct prefix_ipv6 *p)
784 {
785 uint8_t *pnt;
786 int index;
787 int offset;
788
789 index = p->prefixlen / 8;
790
791 if (index < 16) {
792 pnt = (uint8_t *)&p->prefix;
793 offset = p->prefixlen % 8;
794
795 pnt[index] &= maskbit[offset];
796 index++;
797
798 while (index < 16)
799 pnt[index++] = 0;
800 }
801 }
802
803 void apply_mask(struct prefix *p)
804 {
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;
816 }
817
818 /* Utility function of convert between struct prefix <=> union sockunion. */
819 struct prefix *sockunion2hostprefix(const union sockunion *su,
820 struct prefix *prefix)
821 {
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;
842 }
843
844 void prefix2sockunion(const struct prefix *p, union sockunion *su)
845 {
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));
854 }
855
856 int prefix_blen(const struct prefix *p)
857 {
858 switch (p->family) {
859 case AF_INET:
860 return IPV4_MAX_BYTELEN;
861 case AF_INET6:
862 return IPV6_MAX_BYTELEN;
863 case AF_ETHERNET:
864 return ETH_ALEN;
865 }
866 return 0;
867 }
868
869 /* Generic function for conversion string to struct prefix. */
870 int str2prefix(const char *str, struct prefix *p)
871 {
872 int ret;
873
874 if (!str || !p)
875 return 0;
876
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;
881
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;
886
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;
891
892 return 0;
893 }
894
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)
904 {
905 uint8_t family;
906 char buf[PREFIX2STR_BUFFER];
907 char buf2[ETHER_ADDR_STRLEN];
908
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)),
914 p->prefixlen);
915 else {
916 family = is_evpn_prefix_ipaddr_v4(p)
917 ? AF_INET
918 : AF_INET6;
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);
927 }
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];
936
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 {
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);
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 }
998 return str;
999 }
1000
1001 const char *prefix2str(union prefixconstptr pu, char *str, int size)
1002 {
1003 const struct prefix *p = pu.p;
1004 char buf[PREFIX2STR_BUFFER];
1005 int byte, tmp, a, b;
1006 bool z = false;
1007 size_t l;
1008
1009 switch (p->family) {
1010 case AF_INET:
1011 case AF_INET6:
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);
1028 break;
1029
1030 case AF_ETHERNET:
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:
1037 prefixevpn2str((const struct prefix_evpn *)p, str, size);
1038 break;
1039
1040 case AF_FLOWSPEC:
1041 strlcpy(str, "FS prefix", size);
1042 break;
1043
1044 default:
1045 strlcpy(str, "UNK prefix", size);
1046 break;
1047 }
1048
1049 return str;
1050 }
1051
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)
1058 strlcpy(buf, "*", buf_size);
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
1081 struct prefix *prefix_new(void)
1082 {
1083 struct prefix *p;
1084
1085 p = XCALLOC(MTYPE_PREFIX, sizeof *p);
1086 return p;
1087 }
1088
1089 void prefix_free_lists(void *arg)
1090 {
1091 struct prefix *p = arg;
1092
1093 prefix_free(&p);
1094 }
1095
1096 /* Free prefix structure. */
1097 void prefix_free(struct prefix **p)
1098 {
1099 XFREE(MTYPE_PREFIX, *p);
1100 }
1101
1102 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1103 void apply_classful_mask_ipv4(struct prefix_ipv4 *p)
1104 {
1105
1106 uint32_t destination;
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 }
1123 }
1124
1125 in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen)
1126 {
1127 struct in_addr mask;
1128
1129 masklen2ip(masklen, &mask);
1130 return (masklen != IPV4_MAX_PREFIXLEN - 1) ?
1131 /* normal case */
1132 (hostaddr | ~mask.s_addr)
1133 :
1134 /* For prefix 31 return 255.255.255.255 (RFC3021) */
1135 htonl(0xFFFFFFFF);
1136 }
1137
1138 /* Utility function to convert ipv4 netmask to prefixes
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" */
1141 int netmask_str2prefix_str(const char *net_str, const char *mask_str,
1142 char *prefix_str)
1143 {
1144 struct in_addr network;
1145 struct in_addr mask;
1146 uint8_t prefixlen;
1147 uint32_t destination;
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
1163 if (network.s_addr == INADDR_ANY)
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 }
1174
1175 sprintf(prefix_str, "%s/%d", net_str, prefixlen);
1176
1177 return 1;
1178 }
1179
1180 /* Utility function for making IPv6 address string. */
1181 const char *inet6_ntoa(struct in6_addr addr)
1182 {
1183 static char buf[INET6_ADDRSTRLEN];
1184
1185 inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN);
1186 return buf;
1187 }
1188
1189 /* converts to internal representation of mac address
1190 * returns 1 on success, 0 otherwise
1191 * format accepted: AA:BB:CC:DD:EE:FF
1192 * if mac parameter is null, then check only
1193 */
1194 int prefix_str2mac(const char *str, struct ethaddr *mac)
1195 {
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;
1214 }
1215
1216 char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size)
1217 {
1218 char *ptr;
1219
1220 if (!mac)
1221 return NULL;
1222 if (!buf)
1223 ptr = XMALLOC(MTYPE_TMP, ETHER_ADDR_STRLEN * sizeof(char));
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;
1233 }
1234
1235 unsigned prefix_hash_key(const void *pp)
1236 {
1237 struct prefix copy;
1238
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(&copy, 0, sizeof(copy));
1248 prefix_copy(&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 }
1257 /* make sure *all* unused bits are zero, particularly including
1258 * alignment /
1259 * padding and unused prefix bytes. */
1260 memset(&copy, 0, sizeof(copy));
1261 prefix_copy(&copy, (struct prefix *)pp);
1262 return jhash(&copy,
1263 offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen),
1264 0x55aa5a5a);
1265 }
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)
1304 ptr = XMALLOC(MTYPE_TMP, ESI_STR_LEN * sizeof(char));
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 }
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 }