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