prefix.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #ifndef _ZEBRA_PREFIX_H
  23. #define _ZEBRA_PREFIX_H
  24. #ifdef SUNOS_5
  25. # include <sys/ethernet.h>
  26. #else
  27. # ifdef GNU_LINUX
  28. # include <net/ethernet.h>
  29. # else
  30. # include <netinet/if_ether.h>
  31. # endif
  32. #endif
  33. #include "sockunion.h"
  34. #ifndef ETHER_ADDR_LEN
  35. #define ETHER_ADDR_LEN ETHERADDRL
  36. #endif
  37. /*
  38. * there isn't a portable ethernet address type. We define our
  39. * own to simplify internal handling
  40. */
  41. struct ethaddr {
  42. u_char octet[ETHER_ADDR_LEN];
  43. } __packed;
  44. /*
  45. * A struct prefix contains an address family, a prefix length, and an
  46. * address. This can represent either a 'network prefix' as defined
  47. * by CIDR, where the 'host bits' of the prefix are 0
  48. * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
  49. * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
  50. * interface.
  51. */
  52. /* different OSes use different names */
  53. #if defined(AF_PACKET)
  54. #define AF_ETHERNET AF_PACKET
  55. #else
  56. #if defined(AF_LINK)
  57. #define AF_ETHERNET AF_LINK
  58. #endif
  59. #endif
  60. /* IPv4 and IPv6 unified prefix structure. */
  61. struct prefix
  62. {
  63. u_char family;
  64. u_char prefixlen;
  65. union
  66. {
  67. u_char prefix;
  68. struct in_addr prefix4;
  69. #ifdef HAVE_IPV6
  70. struct in6_addr prefix6;
  71. #endif /* HAVE_IPV6 */
  72. struct
  73. {
  74. struct in_addr id;
  75. struct in_addr adv_router;
  76. } lp;
  77. struct ethaddr prefix_eth; /* AF_ETHERNET */
  78. u_char val[8];
  79. uintptr_t ptr;
  80. } u __attribute__ ((aligned (8)));
  81. };
  82. /* IPv4 prefix structure. */
  83. struct prefix_ipv4
  84. {
  85. u_char family;
  86. u_char prefixlen;
  87. struct in_addr prefix __attribute__ ((aligned (8)));
  88. };
  89. /* IPv6 prefix structure. */
  90. #ifdef HAVE_IPV6
  91. struct prefix_ipv6
  92. {
  93. u_char family;
  94. u_char prefixlen;
  95. struct in6_addr prefix __attribute__ ((aligned (8)));
  96. };
  97. #endif /* HAVE_IPV6 */
  98. struct prefix_ls
  99. {
  100. u_char family;
  101. u_char prefixlen;
  102. struct in_addr id __attribute__ ((aligned (8)));
  103. struct in_addr adv_router;
  104. };
  105. /* Prefix for routing distinguisher. */
  106. struct prefix_rd
  107. {
  108. u_char family;
  109. u_char prefixlen;
  110. u_char val[8] __attribute__ ((aligned (8)));
  111. };
  112. /* Prefix for ethernet. */
  113. struct prefix_eth
  114. {
  115. u_char family;
  116. u_char prefixlen;
  117. struct ethaddr eth_addr __attribute__ ((aligned (8))); /* AF_ETHERNET */
  118. };
  119. /* Prefix for a generic pointer */
  120. struct prefix_ptr
  121. {
  122. u_char family;
  123. u_char prefixlen;
  124. uintptr_t prefix __attribute__ ((aligned (8)));
  125. };
  126. /* helper to get type safety/avoid casts on calls
  127. * (w/o this, functions accepting all prefix types need casts on the caller
  128. * side, which strips type safety since the cast will accept any pointer
  129. * type.)
  130. */
  131. union prefix46ptr
  132. {
  133. struct prefix *p;
  134. struct prefix_ipv4 *p4;
  135. struct prefix_ipv6 *p6;
  136. } __attribute__ ((transparent_union));
  137. union prefix46constptr
  138. {
  139. const struct prefix *p;
  140. const struct prefix_ipv4 *p4;
  141. const struct prefix_ipv6 *p6;
  142. } __attribute__ ((transparent_union));
  143. #ifndef INET_ADDRSTRLEN
  144. #define INET_ADDRSTRLEN 16
  145. #endif /* INET_ADDRSTRLEN */
  146. #ifndef INET6_ADDRSTRLEN
  147. #define INET6_ADDRSTRLEN 46
  148. #endif /* INET6_ADDRSTRLEN */
  149. #ifndef INET6_BUFSIZ
  150. #define INET6_BUFSIZ 51
  151. #endif /* INET6_BUFSIZ */
  152. /* Maximum prefix string length (IPv6) */
  153. #define PREFIX_STRLEN 51
  154. /* Max bit/byte length of IPv4 address. */
  155. #define IPV4_MAX_BYTELEN 4
  156. #define IPV4_MAX_BITLEN 32
  157. #define IPV4_MAX_PREFIXLEN 32
  158. #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
  159. #define IPV4_ADDR_SAME(D,S) (memcmp ((D), (S), IPV4_MAX_BYTELEN) == 0)
  160. #define IPV4_ADDR_COPY(D,S) memcpy ((D), (S), IPV4_MAX_BYTELEN)
  161. #define IPV4_NET0(a) ((((u_int32_t) (a)) & 0xff000000) == 0x00000000)
  162. #define IPV4_NET127(a) ((((u_int32_t) (a)) & 0xff000000) == 0x7f000000)
  163. #define IPV4_LINKLOCAL(a) ((((u_int32_t) (a)) & 0xffff0000) == 0xa9fe0000)
  164. #define IPV4_CLASS_DE(a) ((((u_int32_t) (a)) & 0xe0000000) == 0xe0000000)
  165. /* Max bit/byte length of IPv6 address. */
  166. #define IPV6_MAX_BYTELEN 16
  167. #define IPV6_MAX_BITLEN 128
  168. #define IPV6_MAX_PREFIXLEN 128
  169. #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
  170. #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
  171. #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
  172. /* Count prefix size from mask length */
  173. #define PSIZE(a) (((a) + 7) / (8))
  174. /* Prefix's family member. */
  175. #define PREFIX_FAMILY(p) ((p)->family)
  176. /* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */
  177. #ifndef s6_addr32
  178. #if defined(SUNOS_5)
  179. /* Some SunOS define s6_addr32 only to kernel */
  180. #define s6_addr32 _S6_un._S6_u32
  181. #else
  182. #define s6_addr32 __u6_addr.__u6_addr32
  183. #endif /* SUNOS_5 */
  184. #endif /*s6_addr32*/
  185. /* Prototypes. */
  186. extern int str2family(const char *);
  187. extern int afi2family (afi_t);
  188. extern afi_t family2afi (int);
  189. extern const char *safi2str(safi_t safi);
  190. extern const char *afi2str(afi_t afi);
  191. /* Check bit of the prefix. */
  192. extern unsigned int prefix_bit (const u_char *prefix, const u_char prefixlen);
  193. extern unsigned int prefix6_bit (const struct in6_addr *prefix, const u_char prefixlen);
  194. extern struct prefix *prefix_new (void);
  195. extern void prefix_free (struct prefix *);
  196. extern const char *prefix_family_str (const struct prefix *);
  197. extern int prefix_blen (const struct prefix *);
  198. extern int str2prefix (const char *, struct prefix *);
  199. extern const char *prefix2str (union prefix46constptr, char *, int);
  200. extern int prefix_match (const struct prefix *, const struct prefix *);
  201. extern int prefix_same (const struct prefix *, const struct prefix *);
  202. extern int prefix_cmp (const struct prefix *, const struct prefix *);
  203. extern int prefix_common_bits (const struct prefix *, const struct prefix *);
  204. extern void prefix_copy (struct prefix *dest, const struct prefix *src);
  205. extern void apply_mask (struct prefix *);
  206. extern struct prefix *sockunion2prefix (const union sockunion *dest,
  207. const union sockunion *mask);
  208. extern struct prefix *sockunion2hostprefix (const union sockunion *, struct prefix *p);
  209. extern void prefix2sockunion (const struct prefix *, union sockunion *);
  210. extern int str2prefix_eth (const char *, struct prefix_eth *);
  211. extern struct prefix_ipv4 *prefix_ipv4_new (void);
  212. extern void prefix_ipv4_free (struct prefix_ipv4 *);
  213. extern int str2prefix_ipv4 (const char *, struct prefix_ipv4 *);
  214. extern void apply_mask_ipv4 (struct prefix_ipv4 *);
  215. #define PREFIX_COPY_IPV4(DST, SRC) \
  216. *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC));
  217. extern int prefix_ipv4_any (const struct prefix_ipv4 *);
  218. extern void apply_classful_mask_ipv4 (struct prefix_ipv4 *);
  219. extern u_char ip_masklen (struct in_addr);
  220. extern void masklen2ip (const int, struct in_addr *);
  221. /* returns the network portion of the host address */
  222. extern in_addr_t ipv4_network_addr (in_addr_t hostaddr, int masklen);
  223. /* given the address of a host on a network and the network mask length,
  224. * calculate the broadcast address for that network;
  225. * special treatment for /31: returns the address of the other host
  226. * on the network by flipping the host bit */
  227. extern in_addr_t ipv4_broadcast_addr (in_addr_t hostaddr, int masklen);
  228. extern int netmask_str2prefix_str (const char *, const char *, char *);
  229. #ifdef HAVE_IPV6
  230. extern struct prefix_ipv6 *prefix_ipv6_new (void);
  231. extern void prefix_ipv6_free (struct prefix_ipv6 *);
  232. extern int str2prefix_ipv6 (const char *, struct prefix_ipv6 *);
  233. extern void apply_mask_ipv6 (struct prefix_ipv6 *);
  234. #define PREFIX_COPY_IPV6(DST, SRC) \
  235. *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC));
  236. extern int ip6_masklen (struct in6_addr);
  237. extern void masklen2ip6 (const int, struct in6_addr *);
  238. extern void str2in6_addr (const char *, struct in6_addr *);
  239. extern const char *inet6_ntoa (struct in6_addr);
  240. #endif /* HAVE_IPV6 */
  241. extern int all_digit (const char *);
  242. static inline int ipv4_martian (struct in_addr *addr)
  243. {
  244. in_addr_t ip = addr->s_addr;
  245. if (IPV4_NET0(ip) || IPV4_NET127(ip) || IPV4_CLASS_DE(ip)) {
  246. return 1;
  247. }
  248. return 0;
  249. }
  250. #endif /* _ZEBRA_PREFIX_H */