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