ip6_tables.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:12k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * 25-Jul-1998 Major changes to allow for ip chain table
  3.  *
  4.  * 3-Jan-2000 Named tables to allow packet selection for different uses.
  5.  */
  6. /*
  7.  *  Format of an IP6 firewall descriptor
  8.  *
  9.  *  src, dst, src_mask, dst_mask are always stored in network byte order.
  10.  *  flags are stored in host byte order (of course).
  11.  *  Port numbers are stored in HOST byte order.
  12.  */
  13. #ifndef _IP6_TABLES_H
  14. #define _IP6_TABLES_H
  15. #ifdef __KERNEL__
  16. #include <linux/if.h>
  17. #include <linux/types.h>
  18. #include <linux/in6.h>
  19. #include <linux/ipv6.h>
  20. #include <linux/skbuff.h>
  21. #endif
  22. #include <linux/netfilter_ipv6.h>
  23. #define IP6T_FUNCTION_MAXNAMELEN 30
  24. #define IP6T_TABLE_MAXNAMELEN 32
  25. /* Yes, Virginia, you have to zero the padding. */
  26. struct ip6t_ip6 {
  27. /* Source and destination IP6 addr */
  28. struct in6_addr src, dst;
  29. /* Mask for src and dest IP6 addr */
  30. struct in6_addr smsk, dmsk;
  31. char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
  32. unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];
  33. /* ARGH, HopByHop uses 0, so can't do 0 = ANY,
  34.    instead IP6T_F_NOPROTO must be set */
  35. u_int16_t proto;
  36. /* TOS to match iff flags & IP6T_F_TOS */
  37. u_int8_t tos;
  38. /* Flags word */
  39. u_int8_t flags;
  40. /* Inverse flags */
  41. u_int8_t invflags;
  42. };
  43. /* FIXME: If alignment in kernel different from userspace? --RR */
  44. struct ip6t_entry_match
  45. {
  46. union {
  47. struct {
  48. u_int16_t match_size;
  49. /* Used by userspace */
  50. char name[IP6T_FUNCTION_MAXNAMELEN];
  51. } user;
  52. struct {
  53. u_int16_t match_size;
  54. /* Used inside the kernel */
  55. struct ip6t_match *match;
  56. } kernel;
  57. /* Total length */
  58. u_int16_t match_size;
  59. } u;
  60. unsigned char data[0];
  61. };
  62. struct ip6t_entry_target
  63. {
  64. union {
  65. struct {
  66. u_int16_t target_size;
  67. /* Used by userspace */
  68. char name[IP6T_FUNCTION_MAXNAMELEN];
  69. } user;
  70. struct {
  71. u_int16_t target_size;
  72. /* Used inside the kernel */
  73. struct ip6t_target *target;
  74. } kernel;
  75. /* Total length */
  76. u_int16_t target_size;
  77. } u;
  78. unsigned char data[0];
  79. };
  80. struct ip6t_standard_target
  81. {
  82. struct ip6t_entry_target target;
  83. int verdict;
  84. };
  85. struct ip6t_counters
  86. {
  87. u_int64_t pcnt, bcnt; /* Packet and byte counters */
  88. };
  89. /* Values for "flag" field in struct ip6t_ip6 (general ip6 structure). */
  90. #define IP6T_F_PROTO 0x01 /* Set if rule cares about upper 
  91.    protocols */
  92. #define IP6T_F_TOS 0x02 /* Match the TOS. */
  93. #define IP6T_F_MASK 0x03 /* All possible flag bits mask. */
  94. /* Values for "inv" field in struct ip6t_ip6. */
  95. #define IP6T_INV_VIA_IN 0x01 /* Invert the sense of IN IFACE. */
  96. #define IP6T_INV_VIA_OUT 0x02 /* Invert the sense of OUT IFACE */
  97. #define IP6T_INV_TOS 0x04 /* Invert the sense of TOS. */
  98. #define IP6T_INV_SRCIP 0x08 /* Invert the sense of SRC IP. */
  99. #define IP6T_INV_DSTIP 0x10 /* Invert the sense of DST OP. */
  100. #define IP6T_INV_FRAG 0x20 /* Invert the sense of FRAG. */
  101. #define IP6T_INV_PROTO 0x40 /* Invert the sense of PROTO. */
  102. #define IP6T_INV_MASK 0x7F /* All possible flag bits mask. */
  103. /* This structure defines each of the firewall rules.  Consists of 3
  104.    parts which are 1) general IP header stuff 2) match specific
  105.    stuff 3) the target to perform if the rule matches */
  106. struct ip6t_entry
  107. {
  108. struct ip6t_ip6 ipv6;
  109. /* Mark with fields that we care about. */
  110. unsigned int nfcache;
  111. /* Size of ipt_entry + matches */
  112. u_int16_t target_offset;
  113. /* Size of ipt_entry + matches + target */
  114. u_int16_t next_offset;
  115. /* Back pointer */
  116. unsigned int comefrom;
  117. /* Packet and byte counters. */
  118. struct ip6t_counters counters;
  119. /* The matches (if any), then the target. */
  120. unsigned char elems[0];
  121. };
  122. /*
  123.  * New IP firewall options for [gs]etsockopt at the RAW IP level.
  124.  * Unlike BSD Linux inherits IP options so you don't have to use
  125.  * a raw socket for this. Instead we check rights in the calls. */
  126. #define IP6T_BASE_CTL 64 /* base for firewall socket options */
  127. #define IP6T_SO_SET_REPLACE (IP6T_BASE_CTL)
  128. #define IP6T_SO_SET_ADD_COUNTERS (IP6T_BASE_CTL + 1)
  129. #define IP6T_SO_SET_MAX IP6T_SO_SET_ADD_COUNTERS
  130. #define IP6T_SO_GET_INFO (IP6T_BASE_CTL)
  131. #define IP6T_SO_GET_ENTRIES (IP6T_BASE_CTL + 1)
  132. #define IP6T_SO_GET_MAX IP6T_SO_GET_ENTRIES
  133. /* CONTINUE verdict for targets */
  134. #define IP6T_CONTINUE 0xFFFFFFFF
  135. /* For standard target */
  136. #define IP6T_RETURN (-NF_MAX_VERDICT - 1)
  137. /* TCP matching stuff */
  138. struct ip6t_tcp
  139. {
  140. u_int16_t spts[2]; /* Source port range. */
  141. u_int16_t dpts[2]; /* Destination port range. */
  142. u_int8_t option; /* TCP Option iff non-zero*/
  143. u_int8_t flg_mask; /* TCP flags mask byte */
  144. u_int8_t flg_cmp; /* TCP flags compare byte */
  145. u_int8_t invflags; /* Inverse flags */
  146. };
  147. /* Values for "inv" field in struct ipt_tcp. */
  148. #define IP6T_TCP_INV_SRCPT 0x01 /* Invert the sense of source ports. */
  149. #define IP6T_TCP_INV_DSTPT 0x02 /* Invert the sense of dest ports. */
  150. #define IP6T_TCP_INV_FLAGS 0x04 /* Invert the sense of TCP flags. */
  151. #define IP6T_TCP_INV_OPTION 0x08 /* Invert the sense of option test. */
  152. #define IP6T_TCP_INV_MASK 0x0F /* All possible flags. */
  153. /* UDP matching stuff */
  154. struct ip6t_udp
  155. {
  156. u_int16_t spts[2]; /* Source port range. */
  157. u_int16_t dpts[2]; /* Destination port range. */
  158. u_int8_t invflags; /* Inverse flags */
  159. };
  160. /* Values for "invflags" field in struct ipt_udp. */
  161. #define IP6T_UDP_INV_SRCPT 0x01 /* Invert the sense of source ports. */
  162. #define IP6T_UDP_INV_DSTPT 0x02 /* Invert the sense of dest ports. */
  163. #define IP6T_UDP_INV_MASK 0x03 /* All possible flags. */
  164. /* ICMP matching stuff */
  165. struct ip6t_icmp
  166. {
  167. u_int8_t type; /* type to match */
  168. u_int8_t code[2]; /* range of code */
  169. u_int8_t invflags; /* Inverse flags */
  170. };
  171. /* Values for "inv" field for struct ipt_icmp. */
  172. #define IP6T_ICMP_INV 0x01 /* Invert the sense of type/code test */
  173. /* The argument to IP6T_SO_GET_INFO */
  174. struct ip6t_getinfo
  175. {
  176. /* Which table: caller fills this in. */
  177. char name[IP6T_TABLE_MAXNAMELEN];
  178. /* Kernel fills these in. */
  179. /* Which hook entry points are valid: bitmask */
  180. unsigned int valid_hooks;
  181. /* Hook entry points: one per netfilter hook. */
  182. unsigned int hook_entry[NF_IP6_NUMHOOKS];
  183. /* Underflow points. */
  184. unsigned int underflow[NF_IP6_NUMHOOKS];
  185. /* Number of entries */
  186. unsigned int num_entries;
  187. /* Size of entries. */
  188. unsigned int size;
  189. };
  190. /* The argument to IP6T_SO_SET_REPLACE. */
  191. struct ip6t_replace
  192. {
  193. /* Which table. */
  194. char name[IP6T_TABLE_MAXNAMELEN];
  195. /* Which hook entry points are valid: bitmask.  You can't
  196.            change this. */
  197. unsigned int valid_hooks;
  198. /* Number of entries */
  199. unsigned int num_entries;
  200. /* Total size of new entries */
  201. unsigned int size;
  202. /* Hook entry points. */
  203. unsigned int hook_entry[NF_IP6_NUMHOOKS];
  204. /* Underflow points. */
  205. unsigned int underflow[NF_IP6_NUMHOOKS];
  206. /* Information about old entries: */
  207. /* Number of counters (must be equal to current number of entries). */
  208. unsigned int num_counters;
  209. /* The old entries' counters. */
  210. struct ip6t_counters *counters;
  211. /* The entries (hang off end: not really an array). */
  212. struct ip6t_entry entries[0];
  213. };
  214. /* The argument to IP6T_SO_ADD_COUNTERS. */
  215. struct ip6t_counters_info
  216. {
  217. /* Which table. */
  218. char name[IP6T_TABLE_MAXNAMELEN];
  219. unsigned int num_counters;
  220. /* The counters (actually `number' of these). */
  221. struct ip6t_counters counters[0];
  222. };
  223. /* The argument to IP6T_SO_GET_ENTRIES. */
  224. struct ip6t_get_entries
  225. {
  226. /* Which table: user fills this in. */
  227. char name[IP6T_TABLE_MAXNAMELEN];
  228. /* User fills this in: total entry size. */
  229. unsigned int size;
  230. /* The entries. */
  231. struct ip6t_entry entrytable[0];
  232. };
  233. /* Standard return verdict, or do jump. */
  234. #define IP6T_STANDARD_TARGET ""
  235. /* Error verdict. */
  236. #define IP6T_ERROR_TARGET "ERROR"
  237. /* Helper functions */
  238. static __inline__ struct ip6t_entry_target *
  239. ip6t_get_target(struct ip6t_entry *e)
  240. {
  241. return (void *)e + e->target_offset;
  242. }
  243. /* fn returns 0 to continue iteration */
  244. #define IP6T_MATCH_ITERATE(e, fn, args...)
  245. ({
  246. unsigned int __i;
  247. int __ret = 0;
  248. struct ip6t_entry_match *__m;
  249. for (__i = sizeof(struct ip6t_entry);
  250.      __i < (e)->target_offset;
  251.      __i += __m->u.match_size) {
  252. __m = (void *)(e) + __i;
  253. __ret = fn(__m , ## args);
  254. if (__ret != 0)
  255. break;
  256. }
  257. __ret;
  258. })
  259. /* fn returns 0 to continue iteration */
  260. #define IP6T_ENTRY_ITERATE(entries, size, fn, args...)
  261. ({
  262. unsigned int __i;
  263. int __ret = 0;
  264. struct ip6t_entry *__e;
  265. for (__i = 0; __i < (size); __i += __e->next_offset) {
  266. __e = (void *)(entries) + __i;
  267. __ret = fn(__e , ## args);
  268. if (__ret != 0)
  269. break;
  270. }
  271. __ret;
  272. })
  273. /*
  274.  * Main firewall chains definitions and global var's definitions.
  275.  */
  276. #ifdef __KERNEL__
  277. #include <linux/init.h>
  278. extern void ip6t_init(void) __init;
  279. struct ip6t_match
  280. {
  281. struct list_head list;
  282. const char name[IP6T_FUNCTION_MAXNAMELEN];
  283. /* Return true or false: return FALSE and set *hotdrop = 1 to
  284.            force immediate packet drop. */
  285. int (*match)(const struct sk_buff *skb,
  286.      const struct net_device *in,
  287.      const struct net_device *out,
  288.      const void *matchinfo,
  289.      int offset,
  290.      const void *hdr,
  291.      u_int16_t datalen,
  292.      int *hotdrop);
  293. /* Called when user tries to insert an entry of this type. */
  294. /* Should return true or false. */
  295. int (*checkentry)(const char *tablename,
  296.   const struct ip6t_ip6 *ip,
  297.   void *matchinfo,
  298.   unsigned int matchinfosize,
  299.   unsigned int hook_mask);
  300. /* Called when entry of this type deleted. */
  301. void (*destroy)(void *matchinfo, unsigned int matchinfosize);
  302. /* Set this to THIS_MODULE if you are a module, otherwise NULL */
  303. struct module *me;
  304. };
  305. /* Registration hooks for targets. */
  306. struct ip6t_target
  307. {
  308. struct list_head list;
  309. const char name[IP6T_FUNCTION_MAXNAMELEN];
  310. /* Returns verdict. */
  311. unsigned int (*target)(struct sk_buff **pskb,
  312.        unsigned int hooknum,
  313.        const struct net_device *in,
  314.        const struct net_device *out,
  315.        const void *targinfo,
  316.        void *userdata);
  317. /* Called when user tries to insert an entry of this type:
  318.            hook_mask is a bitmask of hooks from which it can be
  319.            called. */
  320. /* Should return true or false. */
  321. int (*checkentry)(const char *tablename,
  322.   const struct ip6t_entry *e,
  323.   void *targinfo,
  324.   unsigned int targinfosize,
  325.   unsigned int hook_mask);
  326. /* Called when entry of this type deleted. */
  327. void (*destroy)(void *targinfo, unsigned int targinfosize);
  328. /* Set this to THIS_MODULE if you are a module, otherwise NULL */
  329. struct module *me;
  330. };
  331. extern int ip6t_register_target(struct ip6t_target *target);
  332. extern void ip6t_unregister_target(struct ip6t_target *target);
  333. extern int ip6t_register_match(struct ip6t_match *match);
  334. extern void ip6t_unregister_match(struct ip6t_match *match);
  335. /* Furniture shopping... */
  336. struct ip6t_table
  337. {
  338. struct list_head list;
  339. /* A unique name... */
  340. char name[IP6T_TABLE_MAXNAMELEN];
  341. /* Seed table: copied in register_table */
  342. struct ip6t_replace *table;
  343. /* What hooks you will enter on */
  344. unsigned int valid_hooks;
  345. /* Lock for the curtain */
  346. rwlock_t lock;
  347. /* Man behind the curtain... */
  348. struct ip6t_table_info *private;
  349. /* Set this to THIS_MODULE if you are a module, otherwise NULL */
  350. struct module *me;
  351. };
  352. extern int ip6t_register_table(struct ip6t_table *table);
  353. extern void ip6t_unregister_table(struct ip6t_table *table);
  354. extern unsigned int ip6t_do_table(struct sk_buff **pskb,
  355.   unsigned int hook,
  356.   const struct net_device *in,
  357.   const struct net_device *out,
  358.   struct ip6t_table *table,
  359.   void *userdata);
  360. #define IP6T_ALIGN(s) (((s) + (__alignof__(struct ip6t_entry)-1)) & ~(__alignof__(struct ip6t_entry)-1))
  361. #endif /*__KERNEL__*/
  362. #endif /* _IP6_TABLES_H */