1 /* 2 pmacct (Promiscuous mode IP Accounting package) 3 pmacct is Copyright (C) 2003-2019 by Paolo Lucente 4 */ 5 6 /* 7 * Originally based on Quagga prefix structure which is: 8 * 9 * Copyright (C) 1998 Kunihiro Ishiguro 10 * 11 * GNU Zebra is free software; you can redistribute it and/or modify it 12 * under the terms of the GNU General Public License as published by the 13 * Free Software Foundation; either version 2, or (at your option) any 14 * later version. 15 * 16 * GNU Zebra is distributed in the hope that it will be useful, but 17 * WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 * General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with GNU Zebra; see the file COPYING. If not, write to the Free 23 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 24 * 02111-1307, USA. 25 */ 26 27 #ifndef _BGP_PREFIX_H_ 28 #define _BGP_PREFIX_H_ 29 30 #define PREFIX_MASKSTRLEN 4 31 #define PREFIX_STRLEN (INET6_ADDRSTRLEN + PREFIX_MASKSTRLEN) 32 33 /* 34 * A struct prefix contains an address family, a prefix length, and an 35 * address. This can represent either a 'network prefix' as defined 36 * by CIDR, where the 'host bits' of the prefix are 0 37 * (e.g. AF_INET:10.0.0.0/8), or an address and netmask 38 * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an 39 * interface. 40 */ 41 42 /* IPv4 and IPv6 unified prefix structure. */ 43 struct prefix 44 { 45 u_char family; 46 u_char prefixlen; 47 union 48 { 49 u_char prefix; 50 struct in_addr prefix4; 51 struct in6_addr prefix6; 52 } u __attribute__ ((aligned (8))); 53 }; 54 55 /* IPv4 prefix structure. */ 56 struct prefix_ipv4 57 { 58 u_char family; 59 u_char prefixlen; 60 struct in_addr prefix __attribute__ ((aligned (8))); 61 }; 62 63 /* IPv6 prefix structure. */ 64 struct prefix_ipv6 65 { 66 u_char family; 67 u_char prefixlen; 68 struct in6_addr prefix __attribute__ ((aligned (8))); 69 }; 70 71 #ifndef INET_ADDRSTRLEN 72 #define INET_ADDRSTRLEN 16 73 #endif /* INET_ADDRSTRLEN */ 74 75 #ifndef INET6_ADDRSTRLEN 76 #define INET6_ADDRSTRLEN 46 77 #endif /* INET6_ADDRSTRLEN */ 78 79 #ifndef INET6_BUFSIZ 80 #define INET6_BUFSIZ 51 81 #endif /* INET6_BUFSIZ */ 82 83 /* Max bit/byte length of IPv4 address. */ 84 #define IPV4_MAX_BYTELEN 4 85 #define IPV4_MAX_BITLEN 32 86 #define IPV4_MAX_PREFIXLEN 32 87 #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN) 88 #define IPV4_ADDR_SAME(D,S) (memcmp ((D), (S), IPV4_MAX_BYTELEN) == 0) 89 #define IPV4_ADDR_COPY(D,S) memcpy ((D), (S), IPV4_MAX_BYTELEN) 90 91 #define IPV4_NET0(a) ((((u_int32_t) (a)) & 0xff000000) == 0x00000000) 92 #define IPV4_NET127(a) ((((u_int32_t) (a)) & 0xff000000) == 0x7f000000) 93 #define IPV4_LINKLOCAL(a) ((((u_int32_t) (a)) & 0xffff0000) == 0xa9fe0000) 94 95 /* Max bit/byte length of IPv6 address. */ 96 #define IPV6_MAX_BYTELEN 16 97 #define IPV6_MAX_BITLEN 128 98 #define IPV6_MAX_PREFIXLEN 128 99 #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN) 100 #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0) 101 #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN) 102 103 /* Count prefix size from mask length */ 104 #define PSIZE(a) (((a) + 7) / (8)) 105 106 /* Prefix's family member. */ 107 #define PREFIX_FAMILY(p) ((p)->family) 108 109 /* Prototypes. */ 110 extern int afi2family (int); 111 extern int family2afi (int); 112 113 extern struct prefix *prefix_new (void); 114 extern void prefix_free (struct prefix *); 115 extern const char *prefix_family_str (const struct prefix *); 116 extern int prefix_blen (const struct prefix *); 117 extern int str2prefix (const char *, struct prefix *); 118 extern int prefix2str (const struct prefix *, char *, int); 119 extern int prefix_match (const struct prefix *, const struct prefix *); 120 extern int prefix_same (const struct prefix *, const struct prefix *); 121 extern int prefix_cmp (const struct prefix *, const struct prefix *); 122 extern void prefix_copy (struct prefix *dest, const struct prefix *src); 123 extern void apply_mask (struct prefix *); 124 125 extern struct prefix_ipv4 *prefix_ipv4_new (void); 126 extern void prefix_ipv4_free (struct prefix_ipv4 *); 127 extern int str2prefix_ipv4 (const char *, struct prefix_ipv4 *); 128 extern void apply_mask_ipv4 (struct prefix_ipv4 *); 129 130 #define PREFIX_COPY_IPV4(DST, SRC) \ 131 *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC)); 132 133 extern int prefix_ipv4_any (const struct prefix_ipv4 *); 134 extern void apply_classful_mask_ipv4 (struct prefix_ipv4 *); 135 136 extern u_char ip_masklen (struct in_addr); 137 extern void masklen2ip (int, struct in_addr *); 138 /* returns the network portion of the host address */ 139 extern in_addr_t ipv4_network_addr (in_addr_t hostaddr, int masklen); 140 /* given the address of a host on a network and the network mask length, 141 * calculate the broadcast address for that network; 142 * special treatment for /31: returns the address of the other host 143 * on the network by flipping the host bit */ 144 extern in_addr_t ipv4_broadcast_addr (in_addr_t hostaddr, int masklen); 145 146 extern int netmask_str2prefix_str (const char *, const char *, char *); 147 148 extern struct prefix_ipv6 *prefix_ipv6_new (void); 149 extern void prefix_ipv6_free (struct prefix_ipv6 *); 150 extern int str2prefix_ipv6 (const char *, struct prefix_ipv6 *); 151 extern void apply_mask_ipv6 (struct prefix_ipv6 *); 152 153 #define PREFIX_COPY_IPV6(DST, SRC) \ 154 *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC)); 155 156 extern int ip6_masklen (struct in6_addr); 157 extern void masklen2ip6 (int, struct in6_addr *); 158 159 extern void str2in6_addr (const char *, struct in6_addr *); 160 extern const char *inet6_ntoa (struct in6_addr); 161 162 extern int all_digit (const char *); 163 164 #endif 165