paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Prefix structure. |
| 3 | * Copyright (C) 1998 Kunihiro Ishiguro |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2, or (at your option) any |
| 10 | * later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 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 |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #ifndef _ZEBRA_PREFIX_H |
| 24 | #define _ZEBRA_PREFIX_H |
| 25 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 26 | #include "sockunion.h" |
| 27 | |
gdt | 9d24baa | 2004-01-13 14:55:40 +0000 | [diff] [blame] | 28 | /* |
| 29 | * A struct prefix contains an address family, a prefix length, and an |
| 30 | * address. This can represent either a 'network prefix' as defined |
| 31 | * by CIDR, where the 'host bits' of the prefix are 0 |
| 32 | * (e.g. AF_INET:10.0.0.0/8), or an address and netmask |
| 33 | * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an |
| 34 | * interface. |
| 35 | */ |
| 36 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | /* IPv4 and IPv6 unified prefix structure. */ |
| 38 | struct prefix |
| 39 | { |
| 40 | u_char family; |
| 41 | u_char prefixlen; |
| 42 | union |
| 43 | { |
| 44 | u_char prefix; |
| 45 | struct in_addr prefix4; |
| 46 | #ifdef HAVE_IPV6 |
| 47 | struct in6_addr prefix6; |
| 48 | #endif /* HAVE_IPV6 */ |
| 49 | struct |
| 50 | { |
| 51 | struct in_addr id; |
| 52 | struct in_addr adv_router; |
| 53 | } lp; |
| 54 | u_char val[8]; |
Pradosh Mohapatra | b4b359a | 2014-04-28 10:58:06 +0000 | [diff] [blame] | 55 | uintptr_t ptr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 56 | } u __attribute__ ((aligned (8))); |
| 57 | }; |
| 58 | |
| 59 | /* IPv4 prefix structure. */ |
| 60 | struct prefix_ipv4 |
| 61 | { |
| 62 | u_char family; |
| 63 | u_char prefixlen; |
| 64 | struct in_addr prefix __attribute__ ((aligned (8))); |
| 65 | }; |
| 66 | |
| 67 | /* IPv6 prefix structure. */ |
| 68 | #ifdef HAVE_IPV6 |
| 69 | struct prefix_ipv6 |
| 70 | { |
| 71 | u_char family; |
| 72 | u_char prefixlen; |
| 73 | struct in6_addr prefix __attribute__ ((aligned (8))); |
| 74 | }; |
| 75 | #endif /* HAVE_IPV6 */ |
| 76 | |
| 77 | struct prefix_ls |
| 78 | { |
| 79 | u_char family; |
| 80 | u_char prefixlen; |
| 81 | struct in_addr id __attribute__ ((aligned (8))); |
| 82 | struct in_addr adv_router; |
| 83 | }; |
| 84 | |
| 85 | /* Prefix for routing distinguisher. */ |
| 86 | struct prefix_rd |
| 87 | { |
| 88 | u_char family; |
| 89 | u_char prefixlen; |
| 90 | u_char val[8] __attribute__ ((aligned (8))); |
| 91 | }; |
| 92 | |
Pradosh Mohapatra | b4b359a | 2014-04-28 10:58:06 +0000 | [diff] [blame] | 93 | /* Prefix for a generic pointer */ |
| 94 | struct prefix_ptr |
| 95 | { |
| 96 | u_char family; |
| 97 | u_char prefixlen; |
| 98 | uintptr_t prefix __attribute__ ((aligned (8))); |
| 99 | }; |
| 100 | |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 101 | /* helper to get type safety/avoid casts on calls |
| 102 | * (w/o this, functions accepting all prefix types need casts on the caller |
| 103 | * side, which strips type safety since the cast will accept any pointer |
| 104 | * type.) |
| 105 | */ |
| 106 | union prefix46ptr |
| 107 | { |
| 108 | struct prefix *p; |
| 109 | struct prefix_ipv4 *p4; |
| 110 | struct prefix_ipv6 *p6; |
| 111 | } __attribute__ ((transparent_union)); |
| 112 | |
| 113 | union prefix46constptr |
| 114 | { |
| 115 | const struct prefix *p; |
| 116 | const struct prefix_ipv4 *p4; |
| 117 | const struct prefix_ipv6 *p6; |
| 118 | } __attribute__ ((transparent_union)); |
| 119 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 120 | #ifndef INET_ADDRSTRLEN |
| 121 | #define INET_ADDRSTRLEN 16 |
| 122 | #endif /* INET_ADDRSTRLEN */ |
| 123 | |
| 124 | #ifndef INET6_ADDRSTRLEN |
| 125 | #define INET6_ADDRSTRLEN 46 |
| 126 | #endif /* INET6_ADDRSTRLEN */ |
| 127 | |
| 128 | #ifndef INET6_BUFSIZ |
| 129 | #define INET6_BUFSIZ 51 |
| 130 | #endif /* INET6_BUFSIZ */ |
| 131 | |
Timo Teräs | 41eb9a4 | 2015-05-23 11:08:39 +0300 | [diff] [blame] | 132 | /* Maximum prefix string length (IPv6) */ |
| 133 | #define PREFIX_STRLEN 51 |
| 134 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 135 | /* Max bit/byte length of IPv4 address. */ |
| 136 | #define IPV4_MAX_BYTELEN 4 |
| 137 | #define IPV4_MAX_BITLEN 32 |
| 138 | #define IPV4_MAX_PREFIXLEN 32 |
| 139 | #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN) |
| 140 | #define IPV4_ADDR_SAME(D,S) (memcmp ((D), (S), IPV4_MAX_BYTELEN) == 0) |
| 141 | #define IPV4_ADDR_COPY(D,S) memcpy ((D), (S), IPV4_MAX_BYTELEN) |
| 142 | |
| 143 | #define IPV4_NET0(a) ((((u_int32_t) (a)) & 0xff000000) == 0x00000000) |
| 144 | #define IPV4_NET127(a) ((((u_int32_t) (a)) & 0xff000000) == 0x7f000000) |
Paul Jakma | 6dc686a | 2007-04-10 19:24:45 +0000 | [diff] [blame] | 145 | #define IPV4_LINKLOCAL(a) ((((u_int32_t) (a)) & 0xffff0000) == 0xa9fe0000) |
Denis Ovsienko | 733cd9e | 2011-12-17 19:39:30 +0400 | [diff] [blame] | 146 | #define IPV4_CLASS_DE(a) ((((u_int32_t) (a)) & 0xe0000000) == 0xe0000000) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 147 | |
| 148 | /* Max bit/byte length of IPv6 address. */ |
| 149 | #define IPV6_MAX_BYTELEN 16 |
| 150 | #define IPV6_MAX_BITLEN 128 |
| 151 | #define IPV6_MAX_PREFIXLEN 128 |
| 152 | #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN) |
| 153 | #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0) |
| 154 | #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN) |
| 155 | |
| 156 | /* Count prefix size from mask length */ |
| 157 | #define PSIZE(a) (((a) + 7) / (8)) |
| 158 | |
| 159 | /* Prefix's family member. */ |
| 160 | #define PREFIX_FAMILY(p) ((p)->family) |
| 161 | |
David Lamparter | ec38711 | 2015-05-20 22:34:56 +0200 | [diff] [blame] | 162 | /* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */ |
| 163 | #ifndef s6_addr32 |
| 164 | #if defined(SUNOS_5) |
| 165 | /* Some SunOS define s6_addr32 only to kernel */ |
| 166 | #define s6_addr32 _S6_un._S6_u32 |
| 167 | #else |
| 168 | #define s6_addr32 __u6_addr.__u6_addr32 |
| 169 | #endif /* SUNOS_5 */ |
| 170 | #endif /*s6_addr32*/ |
| 171 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | /* Prototypes. */ |
Christian Franke | acf9865 | 2015-11-12 14:24:22 +0100 | [diff] [blame] | 173 | extern int str2family(const char *); |
Michael Lambert | 4c9641b | 2010-07-22 13:20:55 -0400 | [diff] [blame] | 174 | extern int afi2family (afi_t); |
| 175 | extern afi_t family2afi (int); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 176 | |
Paul Jakma | f63f06d | 2011-04-08 12:44:43 +0100 | [diff] [blame] | 177 | /* Check bit of the prefix. */ |
| 178 | extern unsigned int prefix_bit (const u_char *prefix, const u_char prefixlen); |
| 179 | extern unsigned int prefix6_bit (const struct in6_addr *prefix, const u_char prefixlen); |
| 180 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 181 | extern struct prefix *prefix_new (void); |
| 182 | extern void prefix_free (struct prefix *); |
| 183 | extern const char *prefix_family_str (const struct prefix *); |
| 184 | extern int prefix_blen (const struct prefix *); |
| 185 | extern int str2prefix (const char *, struct prefix *); |
Timo Teräs | 41eb9a4 | 2015-05-23 11:08:39 +0300 | [diff] [blame] | 186 | extern const char *prefix2str (union prefix46constptr, char *, int); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 187 | extern int prefix_match (const struct prefix *, const struct prefix *); |
| 188 | extern int prefix_same (const struct prefix *, const struct prefix *); |
| 189 | extern int prefix_cmp (const struct prefix *, const struct prefix *); |
David Lamparter | 17e5206 | 2010-02-02 20:16:35 +0100 | [diff] [blame] | 190 | extern int prefix_common_bits (const struct prefix *, const struct prefix *); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 191 | extern void prefix_copy (struct prefix *dest, const struct prefix *src); |
| 192 | extern void apply_mask (struct prefix *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 194 | extern struct prefix *sockunion2prefix (const union sockunion *dest, |
| 195 | const union sockunion *mask); |
Timo Teräs | c1c69e4 | 2015-05-22 13:40:57 +0300 | [diff] [blame] | 196 | extern struct prefix *sockunion2hostprefix (const union sockunion *, struct prefix *p); |
David Lamparter | 17e5206 | 2010-02-02 20:16:35 +0100 | [diff] [blame] | 197 | extern void prefix2sockunion (const struct prefix *, union sockunion *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 198 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 199 | extern struct prefix_ipv4 *prefix_ipv4_new (void); |
| 200 | extern void prefix_ipv4_free (struct prefix_ipv4 *); |
| 201 | extern int str2prefix_ipv4 (const char *, struct prefix_ipv4 *); |
| 202 | extern void apply_mask_ipv4 (struct prefix_ipv4 *); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 203 | |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 204 | #define PREFIX_COPY_IPV4(DST, SRC) \ |
| 205 | *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC)); |
| 206 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 207 | extern int prefix_ipv4_any (const struct prefix_ipv4 *); |
| 208 | extern void apply_classful_mask_ipv4 (struct prefix_ipv4 *); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 209 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 210 | extern u_char ip_masklen (struct in_addr); |
Denis Ovsienko | 9663386 | 2011-10-08 18:15:21 +0400 | [diff] [blame] | 211 | extern void masklen2ip (const int, struct in_addr *); |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 212 | /* returns the network portion of the host address */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 213 | extern in_addr_t ipv4_network_addr (in_addr_t hostaddr, int masklen); |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 214 | /* given the address of a host on a network and the network mask length, |
| 215 | * calculate the broadcast address for that network; |
| 216 | * special treatment for /31: returns the address of the other host |
| 217 | * on the network by flipping the host bit */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 218 | extern in_addr_t ipv4_broadcast_addr (in_addr_t hostaddr, int masklen); |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 219 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 220 | extern int netmask_str2prefix_str (const char *, const char *, char *); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 221 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 222 | #ifdef HAVE_IPV6 |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 223 | extern struct prefix_ipv6 *prefix_ipv6_new (void); |
| 224 | extern void prefix_ipv6_free (struct prefix_ipv6 *); |
| 225 | extern int str2prefix_ipv6 (const char *, struct prefix_ipv6 *); |
| 226 | extern void apply_mask_ipv6 (struct prefix_ipv6 *); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 227 | |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 228 | #define PREFIX_COPY_IPV6(DST, SRC) \ |
| 229 | *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC)); |
| 230 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 231 | extern int ip6_masklen (struct in6_addr); |
Denis Ovsienko | 21f569e | 2011-10-17 21:11:10 +0400 | [diff] [blame] | 232 | extern void masklen2ip6 (const int, struct in6_addr *); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 233 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 234 | extern void str2in6_addr (const char *, struct in6_addr *); |
| 235 | extern const char *inet6_ntoa (struct in6_addr); |
hasso | 5920990 | 2005-04-05 14:36:49 +0000 | [diff] [blame] | 236 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 237 | #endif /* HAVE_IPV6 */ |
| 238 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 239 | extern int all_digit (const char *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 240 | |
Pradosh Mohapatra | 6ee06fa | 2014-01-12 18:30:13 +0000 | [diff] [blame] | 241 | static inline int ipv4_martian (struct in_addr *addr) |
| 242 | { |
| 243 | in_addr_t ip = addr->s_addr; |
| 244 | |
| 245 | if (IPV4_NET0(ip) || IPV4_NET127(ip) || IPV4_CLASS_DE(ip)) { |
| 246 | return 1; |
| 247 | } |
| 248 | return 0; |
| 249 | } |
| 250 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 251 | #endif /* _ZEBRA_PREFIX_H */ |