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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  Format of an ARP firewall descriptor
  3.  *
  4.  *  src, tgt, src_mask, tgt_mask, arpop, arpop_mask are always stored in
  5.  * network byte order.
  6.  *  flags are stored in host byte order (of course).
  7.  */
  8. #ifndef _ARPTABLES_H
  9. #define _ARPTABLES_H
  10. #ifdef __KERNEL__
  11. #include <linux/if.h>
  12. #include <linux/types.h>
  13. #include <linux/in.h>
  14. #include <linux/if_arp.h>
  15. #include <linux/skbuff.h>
  16. #endif
  17. #include <linux/netfilter_arp.h>
  18. #define ARPT_FUNCTION_MAXNAMELEN 30
  19. #define ARPT_TABLE_MAXNAMELEN 32
  20. #define ARPT_DEV_ADDR_LEN_MAX 16
  21. struct arpt_devaddr_info {
  22. char addr[ARPT_DEV_ADDR_LEN_MAX];
  23. char mask[ARPT_DEV_ADDR_LEN_MAX];
  24. };
  25. /* Yes, Virginia, you have to zero the padding. */
  26. struct arpt_arp {
  27. /* Source and target IP addr */
  28. struct in_addr src, tgt;
  29. /* Mask for src and target IP addr */
  30. struct in_addr smsk, tmsk;
  31. /* Device hw address length, src+target device addresses */
  32. u_int8_t arhln, arhln_mask;
  33. struct arpt_devaddr_info src_devaddr;
  34. struct arpt_devaddr_info tgt_devaddr;
  35. /* ARP operation code. */
  36. u_int16_t arpop, arpop_mask;
  37. /* ARP hardware address and protocol address format. */
  38. u_int16_t arhrd, arhrd_mask;
  39. u_int16_t arpro, arpro_mask;
  40. /* The protocol address length is only accepted if it is 4
  41.  * so there is no use in offering a way to do filtering on it.
  42.  */
  43. char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
  44. unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];
  45. /* Flags word */
  46. u_int8_t flags;
  47. /* Inverse flags */
  48. u_int16_t invflags;
  49. };
  50. struct arpt_entry_target
  51. {
  52. union {
  53. struct {
  54. u_int16_t target_size;
  55. /* Used by userspace */
  56. char name[ARPT_FUNCTION_MAXNAMELEN];
  57. } user;
  58. struct {
  59. u_int16_t target_size;
  60. /* Used inside the kernel */
  61. struct arpt_target *target;
  62. } kernel;
  63. /* Total length */
  64. u_int16_t target_size;
  65. } u;
  66. unsigned char data[0];
  67. };
  68. struct arpt_standard_target
  69. {
  70. struct arpt_entry_target target;
  71. int verdict;
  72. };
  73. struct arpt_counters
  74. {
  75. u_int64_t pcnt, bcnt; /* Packet and byte counters */
  76. };
  77. /* Values for "flag" field in struct arpt_ip (general arp structure).
  78.  * No flags defined yet.
  79.  */
  80. #define ARPT_F_MASK 0x00 /* All possible flag bits mask. */
  81. /* Values for "inv" field in struct arpt_arp. */
  82. #define ARPT_INV_VIA_IN 0x0001 /* Invert the sense of IN IFACE. */
  83. #define ARPT_INV_VIA_OUT 0x0002 /* Invert the sense of OUT IFACE */
  84. #define ARPT_INV_SRCIP 0x0004 /* Invert the sense of SRC IP. */
  85. #define ARPT_INV_TGTIP 0x0008 /* Invert the sense of TGT IP. */
  86. #define ARPT_INV_SRCDEVADDR 0x0010 /* Invert the sense of SRC DEV ADDR. */
  87. #define ARPT_INV_TGTDEVADDR 0x0020 /* Invert the sense of TGT DEV ADDR. */
  88. #define ARPT_INV_ARPOP 0x0040 /* Invert the sense of ARP OP. */
  89. #define ARPT_INV_ARPHRD 0x0080 /* Invert the sense of ARP HRD. */
  90. #define ARPT_INV_ARPPRO 0x0100 /* Invert the sense of ARP PRO. */
  91. #define ARPT_INV_ARPHLN 0x0200 /* Invert the sense of ARP HLN. */
  92. #define ARPT_INV_MASK 0x007F /* All possible flag bits mask. */
  93. /* This structure defines each of the firewall rules.  Consists of 3
  94.    parts which are 1) general ARP header stuff 2) match specific
  95.    stuff 3) the target to perform if the rule matches */
  96. struct arpt_entry
  97. {
  98. struct arpt_arp arp;
  99. /* Size of arpt_entry + matches */
  100. u_int16_t target_offset;
  101. /* Size of arpt_entry + matches + target */
  102. u_int16_t next_offset;
  103. /* Back pointer */
  104. unsigned int comefrom;
  105. /* Packet and byte counters. */
  106. struct arpt_counters counters;
  107. /* The matches (if any), then the target. */
  108. unsigned char elems[0];
  109. };
  110. /*
  111.  * New IP firewall options for [gs]etsockopt at the RAW IP level.
  112.  * Unlike BSD Linux inherits IP options so you don't have to use a raw
  113.  * socket for this. Instead we check rights in the calls.
  114.  */
  115. #define ARPT_BASE_CTL 96 /* base for firewall socket options */
  116. #define ARPT_SO_SET_REPLACE (ARPT_BASE_CTL)
  117. #define ARPT_SO_SET_ADD_COUNTERS (ARPT_BASE_CTL + 1)
  118. #define ARPT_SO_SET_MAX ARPT_SO_SET_ADD_COUNTERS
  119. #define ARPT_SO_GET_INFO (ARPT_BASE_CTL)
  120. #define ARPT_SO_GET_ENTRIES (ARPT_BASE_CTL + 1)
  121. #define ARPT_SO_GET_MAX ARPT_SO_GET_ENTRIES
  122. /* CONTINUE verdict for targets */
  123. #define ARPT_CONTINUE 0xFFFFFFFF
  124. /* For standard target */
  125. #define ARPT_RETURN (-NF_MAX_VERDICT - 1)
  126. /* The argument to ARPT_SO_GET_INFO */
  127. struct arpt_getinfo
  128. {
  129. /* Which table: caller fills this in. */
  130. char name[ARPT_TABLE_MAXNAMELEN];
  131. /* Kernel fills these in. */
  132. /* Which hook entry points are valid: bitmask */
  133. unsigned int valid_hooks;
  134. /* Hook entry points: one per netfilter hook. */
  135. unsigned int hook_entry[NF_ARP_NUMHOOKS];
  136. /* Underflow points. */
  137. unsigned int underflow[NF_ARP_NUMHOOKS];
  138. /* Number of entries */
  139. unsigned int num_entries;
  140. /* Size of entries. */
  141. unsigned int size;
  142. };
  143. /* The argument to ARPT_SO_SET_REPLACE. */
  144. struct arpt_replace
  145. {
  146. /* Which table. */
  147. char name[ARPT_TABLE_MAXNAMELEN];
  148. /* Which hook entry points are valid: bitmask.  You can't
  149.            change this. */
  150. unsigned int valid_hooks;
  151. /* Number of entries */
  152. unsigned int num_entries;
  153. /* Total size of new entries */
  154. unsigned int size;
  155. /* Hook entry points. */
  156. unsigned int hook_entry[NF_ARP_NUMHOOKS];
  157. /* Underflow points. */
  158. unsigned int underflow[NF_ARP_NUMHOOKS];
  159. /* Information about old entries: */
  160. /* Number of counters (must be equal to current number of entries). */
  161. unsigned int num_counters;
  162. /* The old entries' counters. */
  163. struct arpt_counters *counters;
  164. /* The entries (hang off end: not really an array). */
  165. struct arpt_entry entries[0];
  166. };
  167. /* The argument to ARPT_SO_ADD_COUNTERS. */
  168. struct arpt_counters_info
  169. {
  170. /* Which table. */
  171. char name[ARPT_TABLE_MAXNAMELEN];
  172. unsigned int num_counters;
  173. /* The counters (actually `number' of these). */
  174. struct arpt_counters counters[0];
  175. };
  176. /* The argument to ARPT_SO_GET_ENTRIES. */
  177. struct arpt_get_entries
  178. {
  179. /* Which table: user fills this in. */
  180. char name[ARPT_TABLE_MAXNAMELEN];
  181. /* User fills this in: total entry size. */
  182. unsigned int size;
  183. /* The entries. */
  184. struct arpt_entry entrytable[0];
  185. };
  186. /* Standard return verdict, or do jump. */
  187. #define ARPT_STANDARD_TARGET ""
  188. /* Error verdict. */
  189. #define ARPT_ERROR_TARGET "ERROR"
  190. /* Helper functions */
  191. static __inline__ struct arpt_entry_target *arpt_get_target(struct arpt_entry *e)
  192. {
  193. return (void *)e + e->target_offset;
  194. }
  195. /* fn returns 0 to continue iteration */
  196. #define ARPT_ENTRY_ITERATE(entries, size, fn, args...)
  197. ({
  198. unsigned int __i;
  199. int __ret = 0;
  200. struct arpt_entry *__entry;
  201. for (__i = 0; __i < (size); __i += __entry->next_offset) { 
  202. __entry = (void *)(entries) + __i;
  203. __ret = fn(__entry , ## args);
  204. if (__ret != 0)
  205. break;
  206. }
  207. __ret;
  208. })
  209. /*
  210.  * Main firewall chains definitions and global var's definitions.
  211.  */
  212. #ifdef __KERNEL__
  213. /* Registration hooks for targets. */
  214. struct arpt_target
  215. {
  216. struct list_head list;
  217. const char name[ARPT_FUNCTION_MAXNAMELEN];
  218. /* Returns verdict. */
  219. unsigned int (*target)(struct sk_buff **pskb,
  220.        unsigned int hooknum,
  221.        const struct net_device *in,
  222.        const struct net_device *out,
  223.        const void *targinfo,
  224.        void *userdata);
  225. /* Called when user tries to insert an entry of this type:
  226.            hook_mask is a bitmask of hooks from which it can be
  227.            called. */
  228. /* Should return true or false. */
  229. int (*checkentry)(const char *tablename,
  230.   const struct arpt_entry *e,
  231.   void *targinfo,
  232.   unsigned int targinfosize,
  233.   unsigned int hook_mask);
  234. /* Called when entry of this type deleted. */
  235. void (*destroy)(void *targinfo, unsigned int targinfosize);
  236. /* Set this to THIS_MODULE if you are a module, otherwise NULL */
  237. struct module *me;
  238. };
  239. extern int arpt_register_target(struct arpt_target *target);
  240. extern void arpt_unregister_target(struct arpt_target *target);
  241. /* Furniture shopping... */
  242. struct arpt_table
  243. {
  244. struct list_head list;
  245. /* A unique name... */
  246. char name[ARPT_TABLE_MAXNAMELEN];
  247. /* Seed table: copied in register_table */
  248. struct arpt_replace *table;
  249. /* What hooks you will enter on */
  250. unsigned int valid_hooks;
  251. /* Lock for the curtain */
  252. rwlock_t lock;
  253. /* Man behind the curtain... */
  254. struct arpt_table_info *private;
  255. /* Set this to THIS_MODULE if you are a module, otherwise NULL */
  256. struct module *me;
  257. };
  258. extern int arpt_register_table(struct arpt_table *table);
  259. extern void arpt_unregister_table(struct arpt_table *table);
  260. extern unsigned int arpt_do_table(struct sk_buff **pskb,
  261.   unsigned int hook,
  262.   const struct net_device *in,
  263.   const struct net_device *out,
  264.   struct arpt_table *table,
  265.   void *userdata);
  266. #define ARPT_ALIGN(s) (((s) + (__alignof__(struct arpt_entry)-1)) & ~(__alignof__(struct arpt_entry)-1))
  267. #endif /*__KERNEL__*/
  268. #endif /* _ARPTABLES_H */