PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/prefix.h

#
C Header | 237 lines | 150 code | 37 blank | 50 comment | 9 complexity | 13579b5ed9dbebd7d515fbd21e33de84 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
  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. #include "sockunion.h"
  25. /*
  26. * A struct prefix contains an address family, a prefix length, and an
  27. * address. This can represent either a 'network prefix' as defined
  28. * by CIDR, where the 'host bits' of the prefix are 0
  29. * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
  30. * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
  31. * interface.
  32. */
  33. /* IPv4 and IPv6 unified prefix structure. */
  34. struct prefix
  35. {
  36. u_char family;
  37. u_char prefixlen;
  38. union
  39. {
  40. u_char prefix;
  41. struct in_addr prefix4;
  42. #ifdef HAVE_IPV6
  43. struct in6_addr prefix6;
  44. #endif /* HAVE_IPV6 */
  45. struct
  46. {
  47. struct in_addr id;
  48. struct in_addr adv_router;
  49. } lp;
  50. u_char val[8];
  51. uintptr_t ptr;
  52. } u __attribute__ ((aligned (8)));
  53. };
  54. /* IPv4 prefix structure. */
  55. struct prefix_ipv4
  56. {
  57. u_char family;
  58. u_char prefixlen;
  59. struct in_addr prefix __attribute__ ((aligned (8)));
  60. };
  61. /* IPv6 prefix structure. */
  62. #ifdef HAVE_IPV6
  63. struct prefix_ipv6
  64. {
  65. u_char family;
  66. u_char prefixlen;
  67. struct in6_addr prefix __attribute__ ((aligned (8)));
  68. };
  69. #endif /* HAVE_IPV6 */
  70. struct prefix_ls
  71. {
  72. u_char family;
  73. u_char prefixlen;
  74. struct in_addr id __attribute__ ((aligned (8)));
  75. struct in_addr adv_router;
  76. };
  77. /* Prefix for routing distinguisher. */
  78. struct prefix_rd
  79. {
  80. u_char family;
  81. u_char prefixlen;
  82. u_char val[8] __attribute__ ((aligned (8)));
  83. };
  84. /* Prefix for a generic pointer */
  85. struct prefix_ptr
  86. {
  87. u_char family;
  88. u_char prefixlen;
  89. uintptr_t prefix __attribute__ ((aligned (8)));
  90. };
  91. /* helper to get type safety/avoid casts on calls
  92. * (w/o this, functions accepting all prefix types need casts on the caller
  93. * side, which strips type safety since the cast will accept any pointer
  94. * type.)
  95. */
  96. union prefix46ptr
  97. {
  98. struct prefix *p;
  99. struct prefix_ipv4 *p4;
  100. struct prefix_ipv6 *p6;
  101. } __attribute__ ((transparent_union));
  102. union prefix46constptr
  103. {
  104. const struct prefix *p;
  105. const struct prefix_ipv4 *p4;
  106. const struct prefix_ipv6 *p6;
  107. } __attribute__ ((transparent_union));
  108. #ifndef INET_ADDRSTRLEN
  109. #define INET_ADDRSTRLEN 16
  110. #endif /* INET_ADDRSTRLEN */
  111. #ifndef INET6_ADDRSTRLEN
  112. #define INET6_ADDRSTRLEN 46
  113. #endif /* INET6_ADDRSTRLEN */
  114. #ifndef INET6_BUFSIZ
  115. #define INET6_BUFSIZ 51
  116. #endif /* INET6_BUFSIZ */
  117. /* Max bit/byte length of IPv4 address. */
  118. #define IPV4_MAX_BYTELEN 4
  119. #define IPV4_MAX_BITLEN 32
  120. #define IPV4_MAX_PREFIXLEN 32
  121. #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
  122. #define IPV4_ADDR_SAME(D,S) (memcmp ((D), (S), IPV4_MAX_BYTELEN) == 0)
  123. #define IPV4_ADDR_COPY(D,S) memcpy ((D), (S), IPV4_MAX_BYTELEN)
  124. #define IPV4_NET0(a) ((((u_int32_t) (a)) & 0xff000000) == 0x00000000)
  125. #define IPV4_NET127(a) ((((u_int32_t) (a)) & 0xff000000) == 0x7f000000)
  126. #define IPV4_LINKLOCAL(a) ((((u_int32_t) (a)) & 0xffff0000) == 0xa9fe0000)
  127. #define IPV4_CLASS_DE(a) ((((u_int32_t) (a)) & 0xe0000000) == 0xe0000000)
  128. /* Max bit/byte length of IPv6 address. */
  129. #define IPV6_MAX_BYTELEN 16
  130. #define IPV6_MAX_BITLEN 128
  131. #define IPV6_MAX_PREFIXLEN 128
  132. #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
  133. #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
  134. #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
  135. /* Count prefix size from mask length */
  136. #define PSIZE(a) (((a) + 7) / (8))
  137. /* Prefix's family member. */
  138. #define PREFIX_FAMILY(p) ((p)->family)
  139. /* Prototypes. */
  140. extern int afi2family (afi_t);
  141. extern afi_t family2afi (int);
  142. /* Check bit of the prefix. */
  143. extern unsigned int prefix_bit (const u_char *prefix, const u_char prefixlen);
  144. extern unsigned int prefix6_bit (const struct in6_addr *prefix, const u_char prefixlen);
  145. extern struct prefix *prefix_new (void);
  146. extern void prefix_free (struct prefix *);
  147. extern const char *prefix_family_str (const struct prefix *);
  148. extern int prefix_blen (const struct prefix *);
  149. extern int str2prefix (const char *, struct prefix *);
  150. extern int prefix2str (const struct prefix *, char *, int);
  151. extern int prefix_match (const struct prefix *, const struct prefix *);
  152. extern int prefix_same (const struct prefix *, const struct prefix *);
  153. extern int prefix_cmp (const struct prefix *, const struct prefix *);
  154. extern int prefix_common_bits (const struct prefix *, const struct prefix *);
  155. extern void prefix_copy (struct prefix *dest, const struct prefix *src);
  156. extern void apply_mask (struct prefix *);
  157. extern struct prefix *sockunion2prefix (const union sockunion *dest,
  158. const union sockunion *mask);
  159. extern struct prefix *sockunion2hostprefix (const union sockunion *);
  160. extern void prefix2sockunion (const struct prefix *, union sockunion *);
  161. extern struct prefix_ipv4 *prefix_ipv4_new (void);
  162. extern void prefix_ipv4_free (struct prefix_ipv4 *);
  163. extern int str2prefix_ipv4 (const char *, struct prefix_ipv4 *);
  164. extern void apply_mask_ipv4 (struct prefix_ipv4 *);
  165. #define PREFIX_COPY_IPV4(DST, SRC) \
  166. *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC));
  167. extern int prefix_ipv4_any (const struct prefix_ipv4 *);
  168. extern void apply_classful_mask_ipv4 (struct prefix_ipv4 *);
  169. extern u_char ip_masklen (struct in_addr);
  170. extern void masklen2ip (const int, struct in_addr *);
  171. /* returns the network portion of the host address */
  172. extern in_addr_t ipv4_network_addr (in_addr_t hostaddr, int masklen);
  173. /* given the address of a host on a network and the network mask length,
  174. * calculate the broadcast address for that network;
  175. * special treatment for /31: returns the address of the other host
  176. * on the network by flipping the host bit */
  177. extern in_addr_t ipv4_broadcast_addr (in_addr_t hostaddr, int masklen);
  178. extern int netmask_str2prefix_str (const char *, const char *, char *);
  179. #ifdef HAVE_IPV6
  180. extern struct prefix_ipv6 *prefix_ipv6_new (void);
  181. extern void prefix_ipv6_free (struct prefix_ipv6 *);
  182. extern int str2prefix_ipv6 (const char *, struct prefix_ipv6 *);
  183. extern void apply_mask_ipv6 (struct prefix_ipv6 *);
  184. #define PREFIX_COPY_IPV6(DST, SRC) \
  185. *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC));
  186. extern int ip6_masklen (struct in6_addr);
  187. extern void masklen2ip6 (const int, struct in6_addr *);
  188. extern void str2in6_addr (const char *, struct in6_addr *);
  189. extern const char *inet6_ntoa (struct in6_addr);
  190. #endif /* HAVE_IPV6 */
  191. extern int all_digit (const char *);
  192. static inline int ipv4_martian (struct in_addr *addr)
  193. {
  194. in_addr_t ip = addr->s_addr;
  195. if (IPV4_NET0(ip) || IPV4_NET127(ip) || IPV4_CLASS_DE(ip)) {
  196. return 1;
  197. }
  198. return 0;
  199. }
  200. #endif /* _ZEBRA_PREFIX_H */