]> git.proxmox.com Git - mirror_frr.git/blame - lib/prefix.c
Merge pull request #5628 from donaldsharp/rtm_getneigh
[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"
25#include "vty.h"
26#include "sockunion.h"
27#include "memory.h"
28#include "log.h"
7a7761d2 29#include "jhash.h"
472878dc 30#include "lib_errors.h"
d52ec572 31#include "printfrr.h"
6b0655a2 32
4a1ab8e4 33DEFINE_MTYPE_STATIC(LIB, PREFIX, "Prefix")
ecc4d697 34DEFINE_MTYPE_STATIC(LIB, PREFIX_FLOWSPEC, "Prefix Flowspec")
4a1ab8e4 35
718e3744 36/* Maskbit. */
d7c0a89a
QY
37static const uint8_t maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
38 0xf8, 0xfc, 0xfe, 0xff};
d62a17ae 39
718e3744 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
a9e08ebc 47int is_zero_mac(const struct ethaddr *mac)
69b61704
MK
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
f93eee44 59unsigned int prefix_bit(const uint8_t *prefix, const uint16_t prefixlen)
f63f06da 60{
d62a17ae 61 unsigned int offset = prefixlen / 8;
62 unsigned int shift = 7 - (prefixlen % 8);
63
64 return (prefix[offset] >> shift) & 1;
f63f06da
PJ
65}
66
d62a17ae 67int str2family(const char *string)
f3ccedaa 68{
d62a17ae 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;
b03b8898
DS
75 else if (!strcmp("evpn", string))
76 return AF_EVPN;
d62a17ae 77 return -1;
f3ccedaa
CF
78}
79
db2fde34
PZ
80const 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
718e3744 95/* Address Famiy Identifier to Address Family converter. */
d62a17ae 96int afi2family(afi_t afi)
718e3744 97{
d62a17ae 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;
b03b8898 104 /* NOTE: EVPN code should NOT use this interface. */
d62a17ae 105 return 0;
718e3744 106}
107
d62a17ae 108afi_t family2afi(int family)
718e3744 109{
d62a17ae 110 if (family == AF_INET)
111 return AFI_IP;
112 else if (family == AF_INET6)
113 return AFI_IP6;
b03b8898 114 else if (family == AF_ETHERNET || family == AF_EVPN)
d62a17ae 115 return AFI_L2VPN;
116 return 0;
718e3744 117}
118
d62a17ae 119const char *afi2str(afi_t afi)
32ac65d9 120{
d62a17ae 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;
32ac65d9
LB
134}
135
d62a17ae 136const char *safi2str(safi_t safi)
1ec23d90 137{
d62a17ae 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";
7c40bf39 151 case SAFI_FLOWSPEC:
152 return "flowspec";
5c525538
RW
153 default:
154 return "unknown";
d62a17ae 155 }
1ec23d90
LB
156}
157
718e3744 158/* If n includes p prefix then return 1 else return 0. */
d62a17ae 159int prefix_match(const struct prefix *n, const struct prefix *p)
718e3744 160{
d62a17ae 161 int offset;
162 int shift;
d7c0a89a 163 const uint8_t *np, *pp;
d62a17ae 164
165 /* If n's prefix is longer than p's one return 0. */
166 if (n->prefixlen > p->prefixlen)
167 return 0;
168
9a14899b
PG
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
d62a17ae 187 /* Set both prefix's head pointer. */
f0ed6bea 188 np = n->u.val;
189 pp = p->u.val;
d62a17ae 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;
44c69747
LK
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 */
210int 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
3bec29ac
DS
249}
250
251/* If n includes p then return 1 else return 0. Prefix mask is not considered */
d62a17ae 252int prefix_match_network_statement(const struct prefix *n,
253 const struct prefix *p)
3bec29ac 254{
d62a17ae 255 int offset;
256 int shift;
d7c0a89a 257 const uint8_t *np, *pp;
3bec29ac 258
d62a17ae 259 /* Set both prefix's head pointer. */
f0ed6bea 260 np = n->u.val;
261 pp = p->u.val;
3bec29ac 262
d62a17ae 263 offset = n->prefixlen / PNBBY;
264 shift = n->prefixlen % PNBBY;
3bec29ac 265
d62a17ae 266 if (shift)
267 if (maskbit[shift] & (np[offset] ^ pp[offset]))
268 return 0;
3bec29ac 269
d62a17ae 270 while (offset--)
271 if (np[offset] != pp[offset])
272 return 0;
273 return 1;
718e3744 274}
275
4937287f
DL
276#ifdef __clang_analyzer__
277#undef prefix_copy /* cf. prefix.h */
278#endif
279
9c3a2171 280void prefix_copy(union prefixptr udest, union prefixconstptr usrc)
718e3744 281{
9c3a2171
DL
282 struct prefix *dest = udest.p;
283 const struct prefix *src = usrc.p;
284
d62a17ae 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) {
b03b8898
DS
293 memcpy(&dest->u.prefix_eth, &src->u.prefix_eth,
294 sizeof(struct ethaddr));
295 } else if (src->family == AF_EVPN) {
d62a17ae 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;
9a14899b
PG
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);
d62a17ae 313 } else {
450971aa 314 flog_err(EC_LIB_DEVELOPMENT,
1c50c1c0
QY
315 "prefix_copy(): Unknown address family %d",
316 src->family);
d62a17ae 317 assert(0);
318 }
718e3744 319}
320
d62a17ae 321/*
9d24baaa 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 */
9c3a2171 329int prefix_same(union prefixconstptr up1, union prefixconstptr up2)
718e3744 330{
9c3a2171
DL
331 const struct prefix *p1 = up1.p;
332 const struct prefix *p2 = up2.p;
333
d62a17ae 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)
19aad877 342 if (IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
d62a17ae 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)
b03b8898
DS
349 if (!memcmp(&p1->u.prefix_eth, &p2->u.prefix_eth,
350 sizeof(struct ethaddr)))
351 return 1;
352 if (p1->family == AF_EVPN)
d62a17ae 353 if (!memcmp(&p1->u.prefix_evpn, &p2->u.prefix_evpn,
354 sizeof(struct evpn_addr)))
355 return 1;
9a14899b
PG
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 }
d62a17ae 365 }
366 return 0;
718e3744 367}
368
9d24baaa 369/*
1315d74d
DL
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
9d24baaa 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 */
9c3a2171 380int prefix_cmp(union prefixconstptr up1, union prefixconstptr up2)
718e3744 381{
9c3a2171
DL
382 const struct prefix *p1 = up1.p;
383 const struct prefix *p2 = up2.p;
d62a17ae 384 int offset;
385 int shift;
1315d74d 386 int i;
718e3744 387
d62a17ae 388 /* Set both prefix's head pointer. */
9a14899b
PG
389 const uint8_t *pp1;
390 const uint8_t *pp2;
718e3744 391
9a14899b 392 if (p1->family != p2->family)
1315d74d 393 return numcmp(p1->family, p2->family);
9a14899b
PG
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;
718e3744 397
9a14899b
PG
398 if (p1->u.prefix_flowspec.prefixlen !=
399 p2->u.prefix_flowspec.prefixlen)
1315d74d
DL
400 return numcmp(p1->u.prefix_flowspec.prefixlen,
401 p2->u.prefix_flowspec.prefixlen);
9a14899b
PG
402
403 offset = p1->u.prefix_flowspec.prefixlen;
404 while (offset--)
405 if (pp1[offset] != pp2[offset])
1315d74d 406 return numcmp(pp1[offset], pp2[offset]);
9a14899b
PG
407 return 0;
408 }
f0ed6bea 409 pp1 = p1->u.val;
410 pp2 = p2->u.val;
9a14899b
PG
411
412 if (p1->prefixlen != p2->prefixlen)
1315d74d 413 return numcmp(p1->prefixlen, p2->prefixlen);
d62a17ae 414 offset = p1->prefixlen / PNBBY;
415 shift = p1->prefixlen % PNBBY;
718e3744 416
1315d74d
DL
417 i = memcmp(pp1, pp2, offset);
418 if (i)
419 return i;
718e3744 420
dd5bab0c
DS
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;
718e3744 433}
434
17e52061
DL
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 */
d62a17ae 441int prefix_common_bits(const struct prefix *p1, const struct prefix *p2)
17e52061 442{
d62a17ae 443 int pos, bit;
444 int length = 0;
d7c0a89a 445 uint8_t xor ;
d62a17ae 446
447 /* Set both prefix's head pointer. */
f0ed6bea 448 const uint8_t *pp1 = p1->u.val;
449 const uint8_t *pp2 = p2->u.val;
d62a17ae 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)
b03b8898
DS
456 length = ETH_ALEN;
457 if (p1->family == AF_EVPN)
d62a17ae 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;
17e52061
DL
475}
476
718e3744 477/* Return prefix family type string. */
d62a17ae 478const char *prefix_family_str(const struct prefix *p)
718e3744 479{
d62a17ae 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";
b03b8898
DS
486 if (p->family == AF_EVPN)
487 return "evpn";
d62a17ae 488 return "unspec";
718e3744 489}
490
491/* Allocate new prefix_ipv4 structure. */
4d762f26 492struct prefix_ipv4 *prefix_ipv4_new(void)
718e3744 493{
d62a17ae 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;
718e3744 503}
504
505/* Free prefix_ipv4 structure. */
63265b5c 506void prefix_ipv4_free(struct prefix_ipv4 **p)
718e3744 507{
63265b5c 508 prefix_free((struct prefix **)p);
718e3744 509}
510
3923b6e3 511/* If given string is valid return 1 else return 0 */
d62a17ae 512int str2prefix_ipv4(const char *str, struct prefix_ipv4 *p)
718e3744 513{
d62a17ae 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. */
8d920049 525 ret = inet_pton(AF_INET, str, &p->prefix);
d62a17ae 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);
aab9a0a0 537 memcpy(cp, str, pnt - str);
d62a17ae 538 *(cp + (pnt - str)) = '\0';
ef231ac7 539 ret = inet_pton(AF_INET, cp, &p->prefix);
d62a17ae 540 XFREE(MTYPE_TMP, cp);
ef231ac7
RW
541 if (ret == 0)
542 return 0;
d62a17ae 543
544 /* Get prefix length. */
d7c0a89a 545 plen = (uint8_t)atoi(++pnt);
d62a17ae 546 if (plen > IPV4_MAX_PREFIXLEN)
547 return 0;
548
549 p->family = AF_INET;
550 p->prefixlen = plen;
551 }
718e3744 552
d62a17ae 553 return ret;
718e3744 554}
555
32ac65d9 556/* When string format is invalid return 0. */
d62a17ae 557int str2prefix_eth(const char *str, struct prefix_eth *p)
32ac65d9 558{
d62a17ae 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;
0f6476cc 566 bool slash = false;
d62a17ae 567
3b0f6068
DL
568 if (!strcmp(str, "any")) {
569 memset(p, 0, sizeof(*p));
570 p->family = AF_ETHERNET;
571 return 1;
572 }
573
d62a17ae 574 /* Find slash inside string. */
575 pnt = strchr(str, '/');
576
577 if (pnt) {
578 /* Get prefix length. */
d7c0a89a 579 plen = (uint8_t)atoi(++pnt);
d62a17ae 580 if (plen > 48) {
581 ret = 0;
582 goto done;
583 }
584
585 cp = XMALLOC(MTYPE_TMP, (pnt - str) + 1);
aab9a0a0 586 memcpy(cp, str, pnt - str);
d62a17ae 587 *(cp + (pnt - str)) = '\0';
588
589 str_addr = cp;
0f6476cc 590 slash = true;
32ac65d9
LB
591 }
592
d62a17ae 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;
0f6476cc
DS
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)))
996c9314 612 p->prefixlen = 0;
0f6476cc 613
d62a17ae 614 ret = 1;
32ac65d9
LB
615
616done:
0a22ddfb 617 XFREE(MTYPE_TMP, cp);
32ac65d9 618
d62a17ae 619 return ret;
32ac65d9
LB
620}
621
051954f5 622/* Convert masklen into IP address's netmask (network byte order). */
d62a17ae 623void masklen2ip(const int masklen, struct in_addr *netmask)
718e3744 624{
d62a17ae 625 assert(masklen >= 0 && masklen <= IPV4_MAX_BITLEN);
e96b3121 626
d62a17ae 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) */
e96b3121 630
d62a17ae 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);
718e3744 636}
637
638/* Convert IP address's netmask into integer. We assume netmask is
f93eee44 639 * sequential one. Argument netmask should be network byte order. */
d7c0a89a 640uint8_t ip_masklen(struct in_addr netmask)
718e3744 641{
d62a17ae 642 uint32_t tmp = ~ntohl(netmask.s_addr);
f93eee44 643
61be6e94
QY
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 */
f93eee44 648 return tmp ? __builtin_clz(tmp) : 32;
718e3744 649}
650
caff7905 651/* Apply mask to IPv4 prefix (network byte order). */
d62a17ae 652void apply_mask_ipv4(struct prefix_ipv4 *p)
718e3744 653{
d62a17ae 654 struct in_addr mask;
655 masklen2ip(p->prefixlen, &mask);
656 p->prefix.s_addr &= mask.s_addr;
718e3744 657}
658
659/* If prefix is 0.0.0.0/0 then return 1 else return 0. */
d62a17ae 660int prefix_ipv4_any(const struct prefix_ipv4 *p)
718e3744 661{
d62a17ae 662 return (p->prefix.s_addr == 0 && p->prefixlen == 0);
718e3744 663}
6b0655a2 664
718e3744 665/* Allocate a new ip version 6 route */
d62a17ae 666struct prefix_ipv6 *prefix_ipv6_new(void)
718e3744 667{
d62a17ae 668 struct prefix_ipv6 *p;
718e3744 669
d62a17ae 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;
718e3744 675}
676
677/* Free prefix for IPv6. */
63265b5c 678void prefix_ipv6_free(struct prefix_ipv6 **p)
718e3744 679{
63265b5c 680 prefix_free((struct prefix **)p);
718e3744 681}
682
3923b6e3 683/* If given string is valid return 1 else return 0 */
d62a17ae 684int str2prefix_ipv6(const char *str, struct prefix_ipv6 *p)
718e3744 685{
d62a17ae 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);
aab9a0a0 702 memcpy(cp, str, pnt - str);
d62a17ae 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;
d7c0a89a 708 plen = (uint8_t)atoi(++pnt);
d62a17ae 709 if (plen > IPV6_MAX_BITLEN)
710 return 0;
711 p->prefixlen = plen;
712 }
713 p->family = AF_INET6;
718e3744 714
d62a17ae 715 return ret;
718e3744 716}
717
b04c699e 718/* Convert struct in6_addr netmask into integer.
d7c0a89a 719 * FIXME return uint8_t as ip_maskleni() does. */
d62a17ae 720int ip6_masklen(struct in6_addr netmask)
718e3744 721{
25d86233
DL
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;
718e3744 732}
733
d62a17ae 734void masklen2ip6(const int masklen, struct in6_addr *netmask)
718e3744 735{
d62a17ae 736 assert(masklen >= 0 && masklen <= IPV6_MAX_BITLEN);
25d86233
DL
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 }
718e3744 762}
763
d62a17ae 764void apply_mask_ipv6(struct prefix_ipv6 *p)
718e3744 765{
d7c0a89a 766 uint8_t *pnt;
d62a17ae 767 int index;
768 int offset;
8c7f49d2 769
d62a17ae 770 index = p->prefixlen / 8;
8c7f49d2 771
d62a17ae 772 if (index < 16) {
d7c0a89a 773 pnt = (uint8_t *)&p->prefix;
d62a17ae 774 offset = p->prefixlen % 8;
8c7f49d2 775
d62a17ae 776 pnt[index] &= maskbit[offset];
777 index++;
8c7f49d2 778
d62a17ae 779 while (index < 16)
780 pnt[index++] = 0;
781 }
718e3744 782}
783
d62a17ae 784void apply_mask(struct prefix *p)
718e3744 785{
d62a17ae 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;
718e3744 797}
798
b04c699e 799/* Utility function of convert between struct prefix <=> union sockunion. */
d62a17ae 800struct prefix *sockunion2hostprefix(const union sockunion *su,
801 struct prefix *prefix)
718e3744 802{
d62a17ae 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;
718e3744 823}
824
d62a17ae 825void prefix2sockunion(const struct prefix *p, union sockunion *su)
17e52061 826{
d62a17ae 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));
17e52061
DL
835}
836
d62a17ae 837int prefix_blen(const struct prefix *p)
718e3744 838{
d62a17ae 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:
7628d862 847 return ETH_ALEN;
b03b8898 848 break;
d62a17ae 849 }
850 return 0;
718e3744 851}
852
853/* Generic function for conversion string to struct prefix. */
d62a17ae 854int str2prefix(const char *str, struct prefix *p)
718e3744 855{
d62a17ae 856 int ret;
718e3744 857
c37a11ad 858 if (!str || !p)
859 return 0;
860
d62a17ae 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;
718e3744 865
d62a17ae 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;
718e3744 870
d62a17ae 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;
32ac65d9 875
d62a17ae 876 return 0;
718e3744 877}
878
3714a385 879static 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
886static const char *prefixevpn_macip2str(const struct prefix_evpn *p, char *str,
887 int size)
86f1ef44 888{
d7c0a89a 889 uint8_t family;
d62a17ae 890 char buf[PREFIX2STR_BUFFER];
891 char buf2[ETHER_ADDR_STRLEN];
892
3714a385 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)),
d62a17ae 898 p->prefixlen);
3714a385 899 else {
900 family = is_evpn_prefix_ipaddr_v4(p)
d62a17ae 901 ? AF_INET
902 : AF_INET6;
3714a385 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);
d62a17ae 911 }
3714a385 912 return str;
913}
914
915static const char *prefixevpn_imet2str(const struct prefix_evpn *p, char *str,
916 int size)
917{
918 uint8_t family;
919 char buf[PREFIX2STR_BUFFER];
d62a17ae 920
3714a385 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
932static const char *prefixevpn_es2str(const struct prefix_evpn *p, char *str,
933 int size)
934{
50f74cf1 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);
3714a385 941 return str;
942}
943
944static 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
964static 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 }
d62a17ae 982 return str;
86f1ef44 983}
984
d62a17ae 985const char *prefix2str(union prefixconstptr pu, char *str, int size)
718e3744 986{
d62a17ae 987 const struct prefix *p = pu.p;
988 char buf[PREFIX2STR_BUFFER];
ec466f65
QY
989 int byte, tmp, a, b;
990 bool z = false;
991 size_t l;
d62a17ae 992
993 switch (p->family) {
994 case AF_INET:
995 case AF_INET6:
ec466f65
QY
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);
d62a17ae 1012 break;
1013
1014 case AF_ETHERNET:
b03b8898
DS
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:
3714a385 1021 prefixevpn2str((const struct prefix_evpn *)p, str, size);
d62a17ae 1022 break;
1023
9a14899b 1024 case AF_FLOWSPEC:
ec466f65 1025 strlcpy(str, "FS prefix", size);
9a14899b
PG
1026 break;
1027
d62a17ae 1028 default:
ec466f65 1029 strlcpy(str, "UNK prefix", size);
d62a17ae 1030 break;
1031 }
1032
1033 return str;
718e3744 1034}
1035
c6b6b53b
AK
1036void 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)
9f73d2c9 1042 strlcpy(buf, "*", buf_size);
c6b6b53b
AK
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
1053const 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
4d762f26 1065struct prefix *prefix_new(void)
718e3744 1066{
d62a17ae 1067 struct prefix *p;
718e3744 1068
d62a17ae 1069 p = XCALLOC(MTYPE_PREFIX, sizeof *p);
1070 return p;
718e3744 1071}
1072
63265b5c
DS
1073void prefix_free_lists(void *arg)
1074{
1075 struct prefix *p = arg;
1076
1077 prefix_free(&p);
1078}
1079
718e3744 1080/* Free prefix structure. */
63265b5c 1081void prefix_free(struct prefix **p)
718e3744 1082{
63265b5c
DS
1083 XFREE(MTYPE_PREFIX, *p);
1084 *p = NULL;
718e3744 1085}
1086
718e3744 1087/* Utility function to convert ipv4 prefixes to Classful prefixes */
d62a17ae 1088void apply_classful_mask_ipv4(struct prefix_ipv4 *p)
718e3744 1089{
1090
d7c0a89a 1091 uint32_t destination;
d62a17ae 1092
1093 destination = ntohl(p->prefix.s_addr);
1094
1095 if (p->prefixlen == IPV4_MAX_PREFIXLEN)
1096 ;
1097 /* do nothing for host routes */
1098 else if (IN_CLASSC(destination)) {
1099 p->prefixlen = 24;
1100 apply_mask_ipv4(p);
1101 } else if (IN_CLASSB(destination)) {
1102 p->prefixlen = 16;
1103 apply_mask_ipv4(p);
1104 } else {
1105 p->prefixlen = 8;
1106 apply_mask_ipv4(p);
1107 }
718e3744 1108}
1109
d62a17ae 1110in_addr_t ipv4_broadcast_addr(in_addr_t hostaddr, int masklen)
3fb9cd6e 1111{
d62a17ae 1112 struct in_addr mask;
1113
1114 masklen2ip(masklen, &mask);
1115 return (masklen != IPV4_MAX_PREFIXLEN - 1) ?
1116 /* normal case */
1117 (hostaddr | ~mask.s_addr)
1118 :
1119 /* special case for /31 */
1120 (hostaddr ^ ~mask.s_addr);
3fb9cd6e 1121}
1122
d62a17ae 1123/* Utility function to convert ipv4 netmask to prefixes
718e3744 1124 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
1125 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
d62a17ae 1126int netmask_str2prefix_str(const char *net_str, const char *mask_str,
1127 char *prefix_str)
718e3744 1128{
d62a17ae 1129 struct in_addr network;
1130 struct in_addr mask;
d7c0a89a
QY
1131 uint8_t prefixlen;
1132 uint32_t destination;
d62a17ae 1133 int ret;
1134
1135 ret = inet_aton(net_str, &network);
1136 if (!ret)
1137 return 0;
1138
1139 if (mask_str) {
1140 ret = inet_aton(mask_str, &mask);
1141 if (!ret)
1142 return 0;
1143
1144 prefixlen = ip_masklen(mask);
1145 } else {
1146 destination = ntohl(network.s_addr);
1147
1148 if (network.s_addr == 0)
1149 prefixlen = 0;
1150 else if (IN_CLASSC(destination))
1151 prefixlen = 24;
1152 else if (IN_CLASSB(destination))
1153 prefixlen = 16;
1154 else if (IN_CLASSA(destination))
1155 prefixlen = 8;
1156 else
1157 return 0;
1158 }
718e3744 1159
d62a17ae 1160 sprintf(prefix_str, "%s/%d", net_str, prefixlen);
718e3744 1161
d62a17ae 1162 return 1;
718e3744 1163}
1164
5920990f 1165/* Utility function for making IPv6 address string. */
d62a17ae 1166const char *inet6_ntoa(struct in6_addr addr)
5920990f 1167{
d62a17ae 1168 static char buf[INET6_ADDRSTRLEN];
5920990f 1169
d62a17ae 1170 inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN);
1171 return buf;
5920990f 1172}
c215ecaf 1173
c215ecaf 1174/* converts to internal representation of mac address
d62a17ae 1175 * returns 1 on success, 0 otherwise
c215ecaf
PG
1176 * format accepted: AA:BB:CC:DD:EE:FF
1177 * if mac parameter is null, then check only
1178 */
db42a173 1179int prefix_str2mac(const char *str, struct ethaddr *mac)
c215ecaf 1180{
d62a17ae 1181 unsigned int a[6];
1182 int i;
1183
1184 if (!str)
1185 return 0;
1186
1187 if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, a + 2, a + 3,
1188 a + 4, a + 5)
1189 != 6) {
1190 /* error in incoming str length */
1191 return 0;
1192 }
1193 /* valid mac address */
1194 if (!mac)
1195 return 1;
1196 for (i = 0; i < 6; ++i)
1197 mac->octet[i] = a[i] & 0xff;
1198 return 1;
c215ecaf
PG
1199}
1200
db42a173 1201char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size)
c215ecaf 1202{
d62a17ae 1203 char *ptr;
1204
1205 if (!mac)
1206 return NULL;
1207 if (!buf)
9f5dc319 1208 ptr = XMALLOC(MTYPE_TMP, ETHER_ADDR_STRLEN * sizeof(char));
d62a17ae 1209 else {
1210 assert(size >= ETHER_ADDR_STRLEN);
1211 ptr = buf;
1212 }
1213 snprintf(ptr, (ETHER_ADDR_STRLEN), "%02x:%02x:%02x:%02x:%02x:%02x",
1214 (uint8_t)mac->octet[0], (uint8_t)mac->octet[1],
1215 (uint8_t)mac->octet[2], (uint8_t)mac->octet[3],
1216 (uint8_t)mac->octet[4], (uint8_t)mac->octet[5]);
1217 return ptr;
c215ecaf 1218}
7a7761d2 1219
62b4b3b6 1220unsigned prefix_hash_key(const void *pp)
7a7761d2
CF
1221{
1222 struct prefix copy;
1223
9a14899b
PG
1224 if (((struct prefix *)pp)->family == AF_FLOWSPEC) {
1225 uint32_t len;
1226 void *temp;
1227
1228 /* make sure *all* unused bits are zero,
1229 * particularly including alignment /
1230 * padding and unused prefix bytes.
1231 */
1232 memset(&copy, 0, sizeof(copy));
1233 prefix_copy(&copy, (struct prefix *)pp);
1234 len = jhash((void *)copy.u.prefix_flowspec.ptr,
1235 copy.u.prefix_flowspec.prefixlen,
1236 0x55aa5a5a);
1237 temp = (void *)copy.u.prefix_flowspec.ptr;
1238 XFREE(MTYPE_PREFIX_FLOWSPEC, temp);
1239 copy.u.prefix_flowspec.ptr = (uintptr_t)NULL;
1240 return len;
1241 }
7a7761d2
CF
1242 /* make sure *all* unused bits are zero, particularly including
1243 * alignment /
1244 * padding and unused prefix bytes. */
1245 memset(&copy, 0, sizeof(copy));
1246 prefix_copy(&copy, (struct prefix *)pp);
996c9314
LB
1247 return jhash(&copy,
1248 offsetof(struct prefix, u.prefix) + PSIZE(copy.prefixlen),
1249 0x55aa5a5a);
7a7761d2 1250}
50f74cf1 1251
1252/* converts to internal representation of esi
1253 * returns 1 on success, 0 otherwise
1254 * format accepted: aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
1255 * if esi parameter is null, then check only
1256 */
1257int str_to_esi(const char *str, esi_t *esi)
1258{
1259 int i;
1260 unsigned int a[ESI_BYTES];
1261
1262 if (!str)
1263 return 0;
1264
1265 if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
1266 a + 0, a + 1, a + 2, a + 3,
1267 a + 4, a + 5, a + 6, a + 7,
1268 a + 8, a + 9)
1269 != ESI_BYTES) {
1270 /* error in incoming str length */
1271 return 0;
1272 }
1273
1274 /* valid ESI */
1275 if (!esi)
1276 return 1;
1277 for (i = 0; i < ESI_BYTES; ++i)
1278 esi->val[i] = a[i] & 0xff;
1279 return 1;
1280}
1281
1282char *esi_to_str(const esi_t *esi, char *buf, int size)
1283{
1284 char *ptr;
1285
1286 if (!esi)
1287 return NULL;
1288 if (!buf)
9f5dc319 1289 ptr = XMALLOC(MTYPE_TMP, ESI_STR_LEN * sizeof(char));
50f74cf1 1290 else {
1291 assert(size >= ESI_STR_LEN);
1292 ptr = buf;
1293 }
1294
1295 snprintf(ptr, ESI_STR_LEN,
1296 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1297 esi->val[0], esi->val[1], esi->val[2],
1298 esi->val[3], esi->val[4], esi->val[5],
1299 esi->val[6], esi->val[7], esi->val[8],
1300 esi->val[9]);
1301 return ptr;
1302}
d52ec572
DL
1303
1304printfrr_ext_autoreg_p("I4", printfrr_i4)
1305static ssize_t printfrr_i4(char *buf, size_t bsz, const char *fmt,
1306 int prec, const void *ptr)
1307{
1308 inet_ntop(AF_INET, ptr, buf, bsz);
1309 return 2;
1310}
1311
1312printfrr_ext_autoreg_p("I6", printfrr_i6)
1313static ssize_t printfrr_i6(char *buf, size_t bsz, const char *fmt,
1314 int prec, const void *ptr)
1315{
1316 inet_ntop(AF_INET6, ptr, buf, bsz);
1317 return 2;
1318}
1319
1320printfrr_ext_autoreg_p("FX", printfrr_pfx)
1321static ssize_t printfrr_pfx(char *buf, size_t bsz, const char *fmt,
1322 int prec, const void *ptr)
1323{
1324 prefix2str(ptr, buf, bsz);
1325 return 2;
1326}
1327
1328printfrr_ext_autoreg_p("SG4", printfrr_psg)
1329static ssize_t printfrr_psg(char *buf, size_t bsz, const char *fmt,
1330 int prec, const void *ptr)
1331{
1332 const struct prefix_sg *sg = ptr;
1333 struct fbuf fb = { .buf = buf, .pos = buf, .len = bsz - 1 };
1334
1335 if (sg->src.s_addr == INADDR_ANY)
1336 bprintfrr(&fb, "(*,");
1337 else
1338 bprintfrr(&fb, "(%pI4,", &sg->src);
1339
1340 if (sg->grp.s_addr == INADDR_ANY)
1341 bprintfrr(&fb, "*)");
1342 else
1343 bprintfrr(&fb, "%pI4)", &sg->grp);
1344
1345 fb.pos[0] = '\0';
1346 return 3;
1347}