2 * Prefix related functions.
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "sockunion.h"
32 static u_char maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
33 0xf8, 0xfc, 0xfe, 0xff};
35 /* Number of bits in prefix type. */
40 #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff)
42 /* Address Famiy Identifier to Address Family converter. */
49 else if (afi
== AFI_IP6
)
51 #endif /* HAVE_IPV6 */
56 family2afi (int family
)
58 if (family
== AF_INET
)
61 else if (family
== AF_INET6
)
63 #endif /* HAVE_IPV6 */
67 /* If n includes p prefix then return 1 else return 0. */
69 prefix_match (const struct prefix
*n
, const struct prefix
*p
)
74 /* Set both prefix's head pointer. */
75 const u_char
*np
= (const u_char
*)&n
->u
.prefix
;
76 const u_char
*pp
= (const u_char
*)&p
->u
.prefix
;
78 /* If n's prefix is longer than p's one return 0. */
79 if (n
->prefixlen
> p
->prefixlen
)
82 offset
= n
->prefixlen
/ PNBBY
;
83 shift
= n
->prefixlen
% PNBBY
;
86 if (maskbit
[shift
] & (np
[offset
] ^ pp
[offset
]))
90 if (np
[offset
] != pp
[offset
])
95 /* Copy prefix from src to dest. */
97 prefix_copy (struct prefix
*dest
, const struct prefix
*src
)
99 dest
->family
= src
->family
;
100 dest
->prefixlen
= src
->prefixlen
;
102 if (src
->family
== AF_INET
)
103 dest
->u
.prefix4
= src
->u
.prefix4
;
105 else if (src
->family
== AF_INET6
)
106 dest
->u
.prefix6
= src
->u
.prefix6
;
107 #endif /* HAVE_IPV6 */
108 else if (src
->family
== AF_UNSPEC
)
110 dest
->u
.lp
.id
= src
->u
.lp
.id
;
111 dest
->u
.lp
.adv_router
= src
->u
.lp
.adv_router
;
115 zlog (NULL
, LOG_ERR
, "prefix_copy(): Unknown address family %d",
122 * Return 1 if the address/netmask contained in the prefix structure
123 * is the same, and else return 0. For this routine, 'same' requires
124 * that not only the prefix length and the network part be the same,
125 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
126 * the same. Note that this routine has the same return value sense
127 * as '==' (which is different from prefix_cmp).
130 prefix_same (const struct prefix
*p1
, const struct prefix
*p2
)
132 if (p1
->family
== p2
->family
&& p1
->prefixlen
== p2
->prefixlen
)
134 if (p1
->family
== AF_INET
)
135 if (IPV4_ADDR_SAME (&p1
->u
.prefix
, &p2
->u
.prefix
))
138 if (p1
->family
== AF_INET6
)
139 if (IPV6_ADDR_SAME (&p1
->u
.prefix
, &p2
->u
.prefix
))
141 #endif /* HAVE_IPV6 */
147 * Return 0 if the network prefixes represented by the struct prefix
148 * arguments are the same prefix, and 1 otherwise. Network prefixes
149 * are considered the same if the prefix lengths are equal and the
150 * network parts are the same. Host bits (which are considered masked
151 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
152 * 10.0.0.2/8 are considered equivalent by this routine. Note that
153 * this routine has the same return sense as strcmp (which is different
157 prefix_cmp (const struct prefix
*p1
, const struct prefix
*p2
)
162 /* Set both prefix's head pointer. */
163 const u_char
*pp1
= (const u_char
*)&p1
->u
.prefix
;
164 const u_char
*pp2
= (const u_char
*)&p2
->u
.prefix
;
166 if (p1
->family
!= p2
->family
|| p1
->prefixlen
!= p2
->prefixlen
)
169 offset
= p1
->prefixlen
/ 8;
170 shift
= p1
->prefixlen
% 8;
173 if (maskbit
[shift
] & (pp1
[offset
] ^ pp2
[offset
]))
177 if (pp1
[offset
] != pp2
[offset
])
183 /* Return prefix family type string. */
185 prefix_family_str (const struct prefix
*p
)
187 if (p
->family
== AF_INET
)
190 if (p
->family
== AF_INET6
)
192 #endif /* HAVE_IPV6 */
196 /* Allocate new prefix_ipv4 structure. */
200 struct prefix_ipv4
*p
;
202 /* Call prefix_new to allocate a full-size struct prefix to avoid problems
203 where the struct prefix_ipv4 is cast to struct prefix and unallocated
204 bytes were being referenced (e.g. in structure assignments). */
205 p
= (struct prefix_ipv4
*)prefix_new();
210 /* Free prefix_ipv4 structure. */
212 prefix_ipv4_free (struct prefix_ipv4
*p
)
214 prefix_free((struct prefix
*)p
);
217 /* When string format is invalid return 0. */
219 str2prefix_ipv4 (const char *str
, struct prefix_ipv4
*p
)
226 /* Find slash inside string. */
227 pnt
= strchr (str
, '/');
229 /* String doesn't contail slash. */
232 /* Convert string to prefix. */
233 ret
= inet_aton (str
, &p
->prefix
);
237 /* If address doesn't contain slash we assume it host address. */
239 p
->prefixlen
= IPV4_MAX_BITLEN
;
245 cp
= XMALLOC (MTYPE_TMP
, (pnt
- str
) + 1);
246 strncpy (cp
, str
, pnt
- str
);
247 *(cp
+ (pnt
- str
)) = '\0';
248 ret
= inet_aton (cp
, &p
->prefix
);
249 XFREE (MTYPE_TMP
, cp
);
251 /* Get prefix length. */
252 plen
= (u_char
) atoi (++pnt
);
253 if (plen
> IPV4_MAX_PREFIXLEN
)
263 /* Convert masklen into IP address's netmask. */
265 masklen2ip (int masklen
, struct in_addr
*netmask
)
271 memset (netmask
, 0, sizeof (struct in_addr
));
272 pnt
= (unsigned char *) netmask
;
274 offset
= masklen
/ 8;
284 /* Convert IP address's netmask into integer. We assume netmask is
285 sequential one. Argument netmask should be network byte order. */
287 ip_masklen (struct in_addr netmask
)
295 pnt
= (u_char
*) &netmask
;
298 while ((pnt
< end
) && (*pnt
== 0xff))
316 /* Apply mask to IPv4 prefix. */
318 apply_mask_ipv4 (struct prefix_ipv4
*p
)
324 index
= p
->prefixlen
/ 8;
328 pnt
= (u_char
*) &p
->prefix
;
329 offset
= p
->prefixlen
% 8;
331 pnt
[index
] &= maskbit
[offset
];
339 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
341 prefix_ipv4_any (const struct prefix_ipv4
*p
)
343 return (p
->prefix
.s_addr
== 0 && p
->prefixlen
== 0);
348 /* Allocate a new ip version 6 route */
350 prefix_ipv6_new (void)
352 struct prefix_ipv6
*p
;
354 /* Allocate a full-size struct prefix to avoid problems with structure
356 p
= (struct prefix_ipv6
*)prefix_new();
357 p
->family
= AF_INET6
;
361 /* Free prefix for IPv6. */
363 prefix_ipv6_free (struct prefix_ipv6
*p
)
365 prefix_free((struct prefix
*)p
);
368 /* If given string is valid return pin6 else return NULL */
370 str2prefix_ipv6 (const char *str
, struct prefix_ipv6
*p
)
376 pnt
= strchr (str
, '/');
378 /* If string doesn't contain `/' treat it as host route. */
381 ret
= inet_pton (AF_INET6
, str
, &p
->prefix
);
384 p
->prefixlen
= IPV6_MAX_BITLEN
;
390 cp
= XMALLOC (0, (pnt
- str
) + 1);
391 strncpy (cp
, str
, pnt
- str
);
392 *(cp
+ (pnt
- str
)) = '\0';
393 ret
= inet_pton (AF_INET6
, cp
, &p
->prefix
);
397 plen
= (u_char
) atoi (++pnt
);
402 p
->family
= AF_INET6
;
407 /* Convert struct in6_addr netmask into integer.
408 * FIXME return u_char as ip_maskleni() does. */
410 ip6_masklen (struct in6_addr netmask
)
416 pnt
= (unsigned char *) & netmask
;
418 while ((*pnt
== 0xff) && len
< 128)
437 masklen2ip6 (int masklen
, struct in6_addr
*netmask
)
443 memset (netmask
, 0, sizeof (struct in6_addr
));
444 pnt
= (unsigned char *) netmask
;
446 offset
= masklen
/ 8;
457 apply_mask_ipv6 (struct prefix_ipv6
*p
)
463 index
= p
->prefixlen
/ 8;
467 pnt
= (u_char
*) &p
->prefix
;
468 offset
= p
->prefixlen
% 8;
470 pnt
[index
] &= maskbit
[offset
];
479 str2in6_addr (const char *str
, struct in6_addr
*addr
)
484 /* %x must point to unsinged int */
485 for (i
= 0; i
< 16; i
++)
487 sscanf (str
+ (i
* 2), "%02x", &x
);
488 addr
->s6_addr
[i
] = x
& 0xff;
491 #endif /* HAVE_IPV6 */
494 apply_mask (struct prefix
*p
)
499 apply_mask_ipv4 ((struct prefix_ipv4
*)p
);
503 apply_mask_ipv6 ((struct prefix_ipv6
*)p
);
505 #endif /* HAVE_IPV6 */
512 /* Utility function of convert between struct prefix <=> union sockunion.
513 * FIXME This function isn't used anywhere. */
515 sockunion2prefix (const union sockunion
*dest
,
516 const union sockunion
*mask
)
518 if (dest
->sa
.sa_family
== AF_INET
)
520 struct prefix_ipv4
*p
;
522 p
= prefix_ipv4_new ();
524 p
->prefix
= dest
->sin
.sin_addr
;
525 p
->prefixlen
= ip_masklen (mask
->sin
.sin_addr
);
526 return (struct prefix
*) p
;
529 if (dest
->sa
.sa_family
== AF_INET6
)
531 struct prefix_ipv6
*p
;
533 p
= prefix_ipv6_new ();
534 p
->family
= AF_INET6
;
535 p
->prefixlen
= ip6_masklen (mask
->sin6
.sin6_addr
);
536 memcpy (&p
->prefix
, &dest
->sin6
.sin6_addr
, sizeof (struct in6_addr
));
537 return (struct prefix
*) p
;
539 #endif /* HAVE_IPV6 */
543 /* Utility function of convert between struct prefix <=> union sockunion. */
545 sockunion2hostprefix (const union sockunion
*su
)
547 if (su
->sa
.sa_family
== AF_INET
)
549 struct prefix_ipv4
*p
;
551 p
= prefix_ipv4_new ();
553 p
->prefix
= su
->sin
.sin_addr
;
554 p
->prefixlen
= IPV4_MAX_BITLEN
;
555 return (struct prefix
*) p
;
558 if (su
->sa
.sa_family
== AF_INET6
)
560 struct prefix_ipv6
*p
;
562 p
= prefix_ipv6_new ();
563 p
->family
= AF_INET6
;
564 p
->prefixlen
= IPV6_MAX_BITLEN
;
565 memcpy (&p
->prefix
, &su
->sin6
.sin6_addr
, sizeof (struct in6_addr
));
566 return (struct prefix
*) p
;
568 #endif /* HAVE_IPV6 */
573 prefix_blen (const struct prefix
*p
)
578 return IPV4_MAX_BYTELEN
;
582 return IPV6_MAX_BYTELEN
;
584 #endif /* HAVE_IPV6 */
589 /* Generic function for conversion string to struct prefix. */
591 str2prefix (const char *str
, struct prefix
*p
)
595 /* First we try to convert string to struct prefix_ipv4. */
596 ret
= str2prefix_ipv4 (str
, (struct prefix_ipv4
*) p
);
601 /* Next we try to convert string to struct prefix_ipv6. */
602 ret
= str2prefix_ipv6 (str
, (struct prefix_ipv6
*) p
);
605 #endif /* HAVE_IPV6 */
611 prefix2str (const struct prefix
*p
, char *str
, int size
)
615 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
);
616 snprintf (str
, size
, "%s/%d", buf
, p
->prefixlen
);
625 p
= XCALLOC (MTYPE_PREFIX
, sizeof *p
);
629 /* Free prefix structure. */
631 prefix_free (struct prefix
*p
)
633 XFREE (MTYPE_PREFIX
, p
);
636 /* Utility function. Check the string only contains digit
638 * FIXME str.[c|h] would be better place for this function. */
640 all_digit (const char *str
)
642 for (; *str
!= '\0'; str
++)
643 if (!isdigit ((int) *str
))
648 /* Utility function to convert ipv4 prefixes to Classful prefixes */
649 void apply_classful_mask_ipv4 (struct prefix_ipv4
*p
)
652 u_int32_t destination
;
654 destination
= ntohl (p
->prefix
.s_addr
);
656 if (p
->prefixlen
== IPV4_MAX_PREFIXLEN
);
657 /* do nothing for host routes */
658 else if (IN_CLASSC (destination
))
663 else if (IN_CLASSB(destination
))
676 ipv4_network_addr (in_addr_t hostaddr
, int masklen
)
680 masklen2ip (masklen
, &mask
);
681 return hostaddr
& mask
.s_addr
;
685 ipv4_broadcast_addr (in_addr_t hostaddr
, int masklen
)
689 masklen2ip (masklen
, &mask
);
690 return (masklen
!= IPV4_MAX_PREFIXLEN
-1) ?
692 (hostaddr
| ~mask
.s_addr
) :
693 /* special case for /31 */
694 (hostaddr
^ ~mask
.s_addr
);
697 /* Utility function to convert ipv4 netmask to prefixes
698 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
699 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
701 netmask_str2prefix_str (const char *net_str
, const char *mask_str
,
704 struct in_addr network
;
707 u_int32_t destination
;
710 ret
= inet_aton (net_str
, &network
);
716 ret
= inet_aton (mask_str
, &mask
);
720 prefixlen
= ip_masklen (mask
);
724 destination
= ntohl (network
.s_addr
);
726 if (network
.s_addr
== 0)
728 else if (IN_CLASSC (destination
))
730 else if (IN_CLASSB (destination
))
732 else if (IN_CLASSA (destination
))
738 sprintf (prefix_str
, "%s/%d", net_str
, prefixlen
);
744 /* Utility function for making IPv6 address string. */
746 inet6_ntoa (struct in6_addr addr
)
748 static char buf
[INET6_ADDRSTRLEN
];
750 inet_ntop (AF_INET6
, &addr
, buf
, INET6_ADDRSTRLEN
);
753 #endif /* HAVE_IPV6 */