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