]> git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.c
bgpd: RFC compliance wrt invalid RMAC, GWIP, ESI and VNI
[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 break;
862 case AF_INET6:
863 return IPV6_MAX_BYTELEN;
864 break;
865 case AF_ETHERNET:
866 return ETH_ALEN;
867 break;
868 }
869 return 0;
870 }
871
872 /* Generic function for conversion string to struct prefix. */
873 int str2prefix(const char *str, struct prefix *p)
874 {
875 int ret;
876
877 if (!str || !p)
878 return 0;
879
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;
884
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;
889
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;
894
895 return 0;
896 }
897
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)
907 {
908 uint8_t family;
909 char buf[PREFIX2STR_BUFFER];
910 char buf2[ETHER_ADDR_STRLEN];
911
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)),
917 p->prefixlen);
918 else {
919 family = is_evpn_prefix_ipaddr_v4(p)
920 ? AF_INET
921 : AF_INET6;
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);
930 }
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];
939
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 {
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);
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 }
1001 return str;
1002 }
1003
1004 const char *prefix2str(union prefixconstptr pu, char *str, int size)
1005 {
1006 const struct prefix *p = pu.p;
1007 char buf[PREFIX2STR_BUFFER];
1008 int byte, tmp, a, b;
1009 bool z = false;
1010 size_t l;
1011
1012 switch (p->family) {
1013 case AF_INET:
1014 case AF_INET6:
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);
1031 break;
1032
1033 case AF_ETHERNET:
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:
1040 prefixevpn2str((const struct prefix_evpn *)p, str, size);
1041 break;
1042
1043 case AF_FLOWSPEC:
1044 strlcpy(str, "FS prefix", size);
1045 break;
1046
1047 default:
1048 strlcpy(str, "UNK prefix", size);
1049 break;
1050 }
1051
1052 return str;
1053 }
1054
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)
1061 strlcpy(buf, "*", buf_size);
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
1084 struct prefix *prefix_new(void)
1085 {
1086 struct prefix *p;
1087
1088 p = XCALLOC(MTYPE_PREFIX, sizeof *p);
1089 return p;
1090 }
1091
1092 void prefix_free_lists(void *arg)
1093 {
1094 struct prefix *p = arg;
1095
1096 prefix_free(&p);
1097 }
1098
1099 /* Free prefix structure. */
1100 void prefix_free(struct prefix **p)
1101 {
1102 XFREE(MTYPE_PREFIX, *p);
1103 }
1104
1105 /* Utility function to convert ipv4 prefixes to Classful prefixes */
1106 void apply_classful_mask_ipv4(struct prefix_ipv4 *p)
1107 {
1108
1109 uint32_t destination;
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 }
1126 }
1127
1128 in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen)
1129 {
1130 struct in_addr mask;
1131
1132 masklen2ip(masklen, &mask);
1133 return (masklen != IPV4_MAX_PREFIXLEN - 1) ?
1134 /* normal case */
1135 (hostaddr | ~mask.s_addr)
1136 :
1137 /* For prefix 31 return 255.255.255.255 (RFC3021) */
1138 htonl(0xFFFFFFFF);
1139 }
1140
1141 /* Utility function to convert ipv4 netmask to prefixes
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" */
1144 int netmask_str2prefix_str(const char *net_str, const char *mask_str,
1145 char *prefix_str)
1146 {
1147 struct in_addr network;
1148 struct in_addr mask;
1149 uint8_t prefixlen;
1150 uint32_t destination;
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
1166 if (network.s_addr == INADDR_ANY)
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 }
1177
1178 sprintf(prefix_str, "%s/%d", net_str, prefixlen);
1179
1180 return 1;
1181 }
1182
1183 /* Utility function for making IPv6 address string. */
1184 const char *inet6_ntoa(struct in6_addr addr)
1185 {
1186 static char buf[INET6_ADDRSTRLEN];
1187
1188 inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN);
1189 return buf;
1190 }
1191
1192 /* converts to internal representation of mac address
1193 * returns 1 on success, 0 otherwise
1194 * format accepted: AA:BB:CC:DD:EE:FF
1195 * if mac parameter is null, then check only
1196 */
1197 int prefix_str2mac(const char *str, struct ethaddr *mac)
1198 {
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;
1217 }
1218
1219 char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size)
1220 {
1221 char *ptr;
1222
1223 if (!mac)
1224 return NULL;
1225 if (!buf)
1226 ptr = XMALLOC(MTYPE_TMP, ETHER_ADDR_STRLEN * sizeof(char));
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;
1236 }
1237
1238 unsigned prefix_hash_key(const void *pp)
1239 {
1240 struct prefix copy;
1241
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(&copy, 0, sizeof(copy));
1251 prefix_copy(&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 }
1260 /* make sure *all* unused bits are zero, particularly including
1261 * alignment /
1262 * padding and unused prefix bytes. */
1263 memset(&copy, 0, sizeof(copy));
1264 prefix_copy(&copy, (struct prefix *)pp);
1265 return jhash(&copy,
1266 offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen),
1267 0x55aa5a5a);
1268 }
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)
1307 ptr = XMALLOC(MTYPE_TMP, ESI_STR_LEN * sizeof(char));
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 }
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 }