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

Linux/Unix编程

开发平台:

Unix_Linux

  1. #ifndef _IPFWADM_CORE_H
  2. #define _IPFWADM_CORE_H
  3. /* Minor modifications to fit on compatibility framework:
  4.    Rusty.Russell@rustcorp.com.au
  5. */
  6. /*
  7.  * IP firewalling code. This is taken from 4.4BSD. Please note the
  8.  * copyright message below. As per the GPL it must be maintained
  9.  * and the licenses thus do not conflict. While this port is subject
  10.  * to the GPL I also place my modifications under the original
  11.  * license in recognition of the original copyright.
  12.  *
  13.  * Ported from BSD to Linux,
  14.  * Alan Cox 22/Nov/1994.
  15.  * Merged and included the FreeBSD-Current changes at Ugen's request
  16.  * (but hey it's a lot cleaner now). Ugen would prefer in some ways
  17.  * we waited for his final product but since Linux 1.2.0 is about to
  18.  * appear it's not practical - Read: It works, it's not clean but please
  19.  * don't consider it to be his standard of finished work.
  20.  * Alan.
  21.  *
  22.  * Fixes:
  23.  * Pauline Middelink : Added masquerading.
  24.  * Jos Vos : Separate input  and output firewall
  25.  * chains, new "insert" and "append"
  26.  * commands to replace "add" commands,
  27.  * add ICMP header to struct ip_fwpkt.
  28.  * Jos Vos : Add support for matching device names.
  29.  * Willy Konynenberg : Add transparent proxying support.
  30.  * Jos Vos : Add options for input/output accounting.
  31.  *
  32.  * All the real work was done by .....
  33.  */
  34. /*
  35.  * Copyright (c) 1993 Daniel Boulet
  36.  * Copyright (c) 1994 Ugen J.S.Antsilevich
  37.  *
  38.  * Redistribution and use in source forms, with and without modification,
  39.  * are permitted provided that this entire comment appears intact.
  40.  *
  41.  * Redistribution in binary form may occur without any restrictions.
  42.  * Obviously, it would be nice if you gave credit where credit is due
  43.  * but requiring it would be too onerous.
  44.  *
  45.  * This software is provided ``AS IS'' without any warranties of any kind.
  46.  */
  47. /*
  48.  *  Format of an IP firewall descriptor
  49.  *
  50.  *  src, dst, src_mask, dst_mask are always stored in network byte order.
  51.  *  flags and num_*_ports are stored in host byte order (of course).
  52.  *  Port numbers are stored in HOST byte order.
  53.  */
  54. #ifdef __KERNEL__
  55. #include <linux/icmp.h>
  56. #include <linux/in.h>
  57. #include <linux/ip.h>
  58. #include <linux/tcp.h>
  59. #include <linux/udp.h>
  60. #endif
  61. struct ip_fw
  62. {
  63. struct ip_fw  *fw_next; /* Next firewall on chain */
  64. struct in_addr fw_src, fw_dst; /* Source and destination IP addr */
  65. struct in_addr fw_smsk, fw_dmsk; /* Mask for src and dest IP addr */
  66. struct in_addr fw_via; /* IP address of interface "via" */
  67. struct net_device *fw_viadev; /* device of interface "via" */
  68. __u16 fw_flg; /* Flags word */
  69. __u16  fw_nsp, fw_ndp; /* N'of src ports and # of dst ports */
  70. /* in ports array (dst ports follow */
  71.      /* src ports; max of 10 ports in all; */
  72.      /* count of 0 means match all ports) */
  73. #define IP_FW_MAX_PORTS 10       /* A reasonable maximum */
  74. __u16 fw_pts[IP_FW_MAX_PORTS]; /* Array of port numbers to match */
  75. unsigned long  fw_pcnt,fw_bcnt; /* Packet and byte counters */
  76. __u8 fw_tosand, fw_tosxor; /* Revised packet priority */
  77. char           fw_vianame[IFNAMSIZ]; /* name of interface "via" */
  78. };
  79. /*
  80.  * Values for "flags" field .
  81.  */
  82. #define IP_FW_F_ALL 0x0000 /* This is a universal packet firewall*/
  83. #define IP_FW_F_TCP 0x0001 /* This is a TCP packet firewall      */
  84. #define IP_FW_F_UDP 0x0002 /* This is a UDP packet firewall      */
  85. #define IP_FW_F_ICMP 0x0003 /* This is a ICMP packet firewall     */
  86. #define IP_FW_F_KIND 0x0003 /* Mask to isolate firewall kind      */
  87. #define IP_FW_F_ACCEPT 0x0004 /* This is an accept firewall (as     *
  88.  *         opposed to a deny firewall)*
  89.  *                                    */
  90. #define IP_FW_F_SRNG 0x0008 /* The first two src ports are a min  *
  91.  * and max range (stored in host byte *
  92.  * order).                            *
  93.  *                                    */
  94. #define IP_FW_F_DRNG 0x0010 /* The first two dst ports are a min  *
  95.  * and max range (stored in host byte *
  96.  * order).                            *
  97.  * (ports[0] <= port <= ports[1])     *
  98.  *                                    */
  99. #define IP_FW_F_PRN 0x0020 /* In verbose mode print this firewall*/
  100. #define IP_FW_F_BIDIR 0x0040 /* For bidirectional firewalls        */
  101. #define IP_FW_F_TCPSYN 0x0080 /* For tcp packets-check SYN only     */
  102. #define IP_FW_F_ICMPRPL 0x0100 /* Send back icmp unreachable packet  */
  103. #define IP_FW_F_MASQ 0x0200 /* Masquerading       */
  104. #define IP_FW_F_TCPACK 0x0400 /* For tcp-packets match if ACK is set*/
  105. #define IP_FW_F_REDIR 0x0800 /* Redirect to local port fw_pts[n]   */
  106. #define IP_FW_F_ACCTIN  0x1000 /* Account incoming packets only.     */
  107. #define IP_FW_F_ACCTOUT 0x2000 /* Account outgoing packets only.     */
  108. #define IP_FW_F_MASK 0x3FFF /* All possible flag bits mask        */
  109. /*
  110.  * New IP firewall options for [gs]etsockopt at the RAW IP level.
  111.  * Unlike BSD Linux inherits IP options so you don't have to use
  112.  * a raw socket for this. Instead we check rights in the calls.
  113.  */
  114. #define IP_FW_BASE_CTL   64 /* base for firewall socket options */
  115. #define IP_FW_COMMAND 0x00FF /* mask for command without chain */
  116. #define IP_FW_TYPE 0x0300 /* mask for type (chain) */
  117. #define IP_FW_SHIFT 8 /* shift count for type (chain) */
  118. #define IP_FW_FWD 0
  119. #define IP_FW_IN 1
  120. #define IP_FW_OUT 2
  121. #define IP_FW_ACCT 3
  122. #define IP_FW_CHAINS 4 /* total number of ip_fw chains */
  123. #define IP_FW_MASQ 5
  124. #define IP_FW_INSERT (IP_FW_BASE_CTL)
  125. #define IP_FW_APPEND (IP_FW_BASE_CTL+1)
  126. #define IP_FW_DELETE (IP_FW_BASE_CTL+2)
  127. #define IP_FW_FLUSH (IP_FW_BASE_CTL+3)
  128. #define IP_FW_ZERO (IP_FW_BASE_CTL+4)
  129. #define IP_FW_POLICY (IP_FW_BASE_CTL+5)
  130. #define IP_FW_CHECK (IP_FW_BASE_CTL+6)
  131. #define IP_FW_MASQ_TIMEOUTS (IP_FW_BASE_CTL+7)
  132. #define IP_FW_INSERT_FWD (IP_FW_INSERT | (IP_FW_FWD << IP_FW_SHIFT))
  133. #define IP_FW_APPEND_FWD (IP_FW_APPEND | (IP_FW_FWD << IP_FW_SHIFT))
  134. #define IP_FW_DELETE_FWD (IP_FW_DELETE | (IP_FW_FWD << IP_FW_SHIFT))
  135. #define IP_FW_FLUSH_FWD (IP_FW_FLUSH  | (IP_FW_FWD << IP_FW_SHIFT))
  136. #define IP_FW_ZERO_FWD (IP_FW_ZERO   | (IP_FW_FWD << IP_FW_SHIFT))
  137. #define IP_FW_POLICY_FWD (IP_FW_POLICY | (IP_FW_FWD << IP_FW_SHIFT))
  138. #define IP_FW_CHECK_FWD (IP_FW_CHECK  | (IP_FW_FWD << IP_FW_SHIFT))
  139. #define IP_FW_INSERT_IN (IP_FW_INSERT | (IP_FW_IN << IP_FW_SHIFT))
  140. #define IP_FW_APPEND_IN (IP_FW_APPEND | (IP_FW_IN << IP_FW_SHIFT))
  141. #define IP_FW_DELETE_IN (IP_FW_DELETE | (IP_FW_IN << IP_FW_SHIFT))
  142. #define IP_FW_FLUSH_IN (IP_FW_FLUSH  | (IP_FW_IN << IP_FW_SHIFT))
  143. #define IP_FW_ZERO_IN (IP_FW_ZERO   | (IP_FW_IN << IP_FW_SHIFT))
  144. #define IP_FW_POLICY_IN (IP_FW_POLICY | (IP_FW_IN << IP_FW_SHIFT))
  145. #define IP_FW_CHECK_IN (IP_FW_CHECK  | (IP_FW_IN << IP_FW_SHIFT))
  146. #define IP_FW_INSERT_OUT (IP_FW_INSERT | (IP_FW_OUT << IP_FW_SHIFT))
  147. #define IP_FW_APPEND_OUT (IP_FW_APPEND | (IP_FW_OUT << IP_FW_SHIFT))
  148. #define IP_FW_DELETE_OUT (IP_FW_DELETE | (IP_FW_OUT << IP_FW_SHIFT))
  149. #define IP_FW_FLUSH_OUT (IP_FW_FLUSH  | (IP_FW_OUT << IP_FW_SHIFT))
  150. #define IP_FW_ZERO_OUT (IP_FW_ZERO   | (IP_FW_OUT << IP_FW_SHIFT))
  151. #define IP_FW_POLICY_OUT (IP_FW_POLICY | (IP_FW_OUT << IP_FW_SHIFT))
  152. #define IP_FW_CHECK_OUT (IP_FW_CHECK  | (IP_FW_OUT << IP_FW_SHIFT))
  153. #define IP_ACCT_INSERT (IP_FW_INSERT | (IP_FW_ACCT << IP_FW_SHIFT))
  154. #define IP_ACCT_APPEND (IP_FW_APPEND | (IP_FW_ACCT << IP_FW_SHIFT))
  155. #define IP_ACCT_DELETE (IP_FW_DELETE | (IP_FW_ACCT << IP_FW_SHIFT))
  156. #define IP_ACCT_FLUSH (IP_FW_FLUSH  | (IP_FW_ACCT << IP_FW_SHIFT))
  157. #define IP_ACCT_ZERO (IP_FW_ZERO   | (IP_FW_ACCT << IP_FW_SHIFT))
  158. #define IP_FW_MASQ_INSERT (IP_FW_INSERT | (IP_FW_MASQ << IP_FW_SHIFT))
  159. #define IP_FW_MASQ_ADD (IP_FW_APPEND | (IP_FW_MASQ << IP_FW_SHIFT))
  160. #define IP_FW_MASQ_DEL (IP_FW_DELETE | (IP_FW_MASQ << IP_FW_SHIFT))
  161. #define IP_FW_MASQ_FLUSH   (IP_FW_FLUSH  | (IP_FW_MASQ << IP_FW_SHIFT))
  162. #define IP_FW_MASQ_INSERT (IP_FW_INSERT | (IP_FW_MASQ << IP_FW_SHIFT))
  163. #define IP_FW_MASQ_ADD (IP_FW_APPEND | (IP_FW_MASQ << IP_FW_SHIFT))
  164. #define IP_FW_MASQ_DEL (IP_FW_DELETE | (IP_FW_MASQ << IP_FW_SHIFT))
  165. #define IP_FW_MASQ_FLUSH   (IP_FW_FLUSH  | (IP_FW_MASQ << IP_FW_SHIFT))
  166. struct ip_fwpkt
  167. {
  168. struct iphdr fwp_iph; /* IP header */
  169. union {
  170. struct tcphdr fwp_tcph; /* TCP header or */
  171. struct udphdr fwp_udph; /* UDP header */
  172. struct icmphdr fwp_icmph; /* ICMP header */
  173. } fwp_protoh;
  174. struct in_addr fwp_via; /* interface address */
  175. char           fwp_vianame[IFNAMSIZ]; /* interface name */
  176. };
  177. #define IP_FW_MASQCTL_MAX 256
  178. #define IP_MASQ_MOD_NMAX  32
  179. struct ip_fw_masqctl
  180. {
  181. int mctl_action;
  182. union {
  183. struct {
  184. char name[IP_MASQ_MOD_NMAX];
  185. char data[1];
  186. } mod;
  187. } u;
  188. };
  189. /*
  190.  * timeouts for ip masquerading
  191.  */
  192. struct ip_fw_masq;
  193. /*
  194.  * Main firewall chains definitions and global var's definitions.
  195.  */
  196. #ifdef __KERNEL__
  197. /* Modes used in the ip_fw_chk() routine. */
  198. #define IP_FW_MODE_FW 0x00 /* kernel firewall check */
  199. #define IP_FW_MODE_ACCT_IN 0x01 /* accounting (incoming) */
  200. #define IP_FW_MODE_ACCT_OUT 0x02 /* accounting (outgoing) */
  201. #define IP_FW_MODE_CHK 0x04 /* check requested by user */
  202. #include <linux/config.h>
  203. #ifdef CONFIG_IP_FIREWALL
  204. extern struct ip_fw *ip_fw_in_chain;
  205. extern struct ip_fw *ip_fw_out_chain;
  206. extern struct ip_fw *ip_fw_fwd_chain;
  207. extern int ip_fw_in_policy;
  208. extern int ip_fw_out_policy;
  209. extern int ip_fw_fwd_policy;
  210. extern int ip_fw_ctl(int, void *, int);
  211. #endif
  212. #ifdef CONFIG_IP_ACCT
  213. extern struct ip_fw *ip_acct_chain;
  214. extern int ip_acct_ctl(int, void *, int);
  215. #endif
  216. #ifdef CONFIG_IP_MASQUERADE
  217. extern int ip_masq_ctl(int, void *, int);
  218. #endif
  219. #ifdef CONFIG_IP_MASQUERADE
  220. extern int ip_masq_ctl(int, void *, int);
  221. #endif
  222. extern int ip_fw_masq_timeouts(void *user, int len);
  223. extern int ip_fw_chk(struct iphdr *, struct net_device *, __u16 *,
  224.      struct ip_fw *, int, int);
  225. #endif /* KERNEL */
  226. #endif /* _IP_FW_H */