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