[NETFILTER]: remove unused include file
[hh.org.git] / net / netfilter / xt_conntrack.c
blob39c57e9f756353f6112b78e5126a40e310acfee3
1 /* Kernel module to match connection tracking information.
2 * Superset of Rusty's minimalistic state match.
4 * (C) 2001 Marc Boucher (marc@mbsi.ca).
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
14 #if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
15 #include <linux/netfilter_ipv4/ip_conntrack.h>
16 #include <linux/netfilter_ipv4/ip_conntrack_tuple.h>
17 #else
18 #include <net/netfilter/nf_conntrack.h>
19 #endif
21 #include <linux/netfilter/x_tables.h>
22 #include <linux/netfilter/xt_conntrack.h>
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
26 MODULE_DESCRIPTION("iptables connection tracking match module");
27 MODULE_ALIAS("ipt_conntrack");
29 #if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
31 static int
32 match(const struct sk_buff *skb,
33 const struct net_device *in,
34 const struct net_device *out,
35 const struct xt_match *match,
36 const void *matchinfo,
37 int offset,
38 unsigned int protoff,
39 int *hotdrop)
41 const struct xt_conntrack_info *sinfo = matchinfo;
42 struct ip_conntrack *ct;
43 enum ip_conntrack_info ctinfo;
44 unsigned int statebit;
46 ct = ip_conntrack_get((struct sk_buff *)skb, &ctinfo);
48 #define FWINV(bool,invflg) ((bool) ^ !!(sinfo->invflags & invflg))
50 if (ct == &ip_conntrack_untracked)
51 statebit = XT_CONNTRACK_STATE_UNTRACKED;
52 else if (ct)
53 statebit = XT_CONNTRACK_STATE_BIT(ctinfo);
54 else
55 statebit = XT_CONNTRACK_STATE_INVALID;
57 if(sinfo->flags & XT_CONNTRACK_STATE) {
58 if (ct) {
59 if(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip !=
60 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip)
61 statebit |= XT_CONNTRACK_STATE_SNAT;
63 if(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip !=
64 ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip)
65 statebit |= XT_CONNTRACK_STATE_DNAT;
68 if (FWINV((statebit & sinfo->statemask) == 0, XT_CONNTRACK_STATE))
69 return 0;
72 if(sinfo->flags & XT_CONNTRACK_PROTO) {
73 if (!ct || FWINV(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum != sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum, XT_CONNTRACK_PROTO))
74 return 0;
77 if(sinfo->flags & XT_CONNTRACK_ORIGSRC) {
78 if (!ct || FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip&sinfo->sipmsk[IP_CT_DIR_ORIGINAL].s_addr) != sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip, XT_CONNTRACK_ORIGSRC))
79 return 0;
82 if(sinfo->flags & XT_CONNTRACK_ORIGDST) {
83 if (!ct || FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip&sinfo->dipmsk[IP_CT_DIR_ORIGINAL].s_addr) != sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip, XT_CONNTRACK_ORIGDST))
84 return 0;
87 if(sinfo->flags & XT_CONNTRACK_REPLSRC) {
88 if (!ct || FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip&sinfo->sipmsk[IP_CT_DIR_REPLY].s_addr) != sinfo->tuple[IP_CT_DIR_REPLY].src.ip, XT_CONNTRACK_REPLSRC))
89 return 0;
92 if(sinfo->flags & XT_CONNTRACK_REPLDST) {
93 if (!ct || FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip&sinfo->dipmsk[IP_CT_DIR_REPLY].s_addr) != sinfo->tuple[IP_CT_DIR_REPLY].dst.ip, XT_CONNTRACK_REPLDST))
94 return 0;
97 if(sinfo->flags & XT_CONNTRACK_STATUS) {
98 if (!ct || FWINV((ct->status & sinfo->statusmask) == 0, XT_CONNTRACK_STATUS))
99 return 0;
102 if(sinfo->flags & XT_CONNTRACK_EXPIRES) {
103 unsigned long expires;
105 if(!ct)
106 return 0;
108 expires = timer_pending(&ct->timeout) ? (ct->timeout.expires - jiffies)/HZ : 0;
110 if (FWINV(!(expires >= sinfo->expires_min && expires <= sinfo->expires_max), XT_CONNTRACK_EXPIRES))
111 return 0;
114 return 1;
117 #else /* CONFIG_IP_NF_CONNTRACK */
118 static int
119 match(const struct sk_buff *skb,
120 const struct net_device *in,
121 const struct net_device *out,
122 const struct xt_match *match,
123 const void *matchinfo,
124 int offset,
125 unsigned int protoff,
126 int *hotdrop)
128 const struct xt_conntrack_info *sinfo = matchinfo;
129 struct nf_conn *ct;
130 enum ip_conntrack_info ctinfo;
131 unsigned int statebit;
133 ct = nf_ct_get((struct sk_buff *)skb, &ctinfo);
135 #define FWINV(bool,invflg) ((bool) ^ !!(sinfo->invflags & invflg))
137 if (ct == &nf_conntrack_untracked)
138 statebit = XT_CONNTRACK_STATE_UNTRACKED;
139 else if (ct)
140 statebit = XT_CONNTRACK_STATE_BIT(ctinfo);
141 else
142 statebit = XT_CONNTRACK_STATE_INVALID;
144 if(sinfo->flags & XT_CONNTRACK_STATE) {
145 if (ct) {
146 if(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip !=
147 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip)
148 statebit |= XT_CONNTRACK_STATE_SNAT;
150 if(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip !=
151 ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip)
152 statebit |= XT_CONNTRACK_STATE_DNAT;
155 if (FWINV((statebit & sinfo->statemask) == 0, XT_CONNTRACK_STATE))
156 return 0;
159 if(sinfo->flags & XT_CONNTRACK_PROTO) {
160 if (!ct || FWINV(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum != sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum, XT_CONNTRACK_PROTO))
161 return 0;
164 if(sinfo->flags & XT_CONNTRACK_ORIGSRC) {
165 if (!ct || FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip&sinfo->sipmsk[IP_CT_DIR_ORIGINAL].s_addr) != sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip, XT_CONNTRACK_ORIGSRC))
166 return 0;
169 if(sinfo->flags & XT_CONNTRACK_ORIGDST) {
170 if (!ct || FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip&sinfo->dipmsk[IP_CT_DIR_ORIGINAL].s_addr) != sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip, XT_CONNTRACK_ORIGDST))
171 return 0;
174 if(sinfo->flags & XT_CONNTRACK_REPLSRC) {
175 if (!ct || FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip&sinfo->sipmsk[IP_CT_DIR_REPLY].s_addr) != sinfo->tuple[IP_CT_DIR_REPLY].src.ip, XT_CONNTRACK_REPLSRC))
176 return 0;
179 if(sinfo->flags & XT_CONNTRACK_REPLDST) {
180 if (!ct || FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip&sinfo->dipmsk[IP_CT_DIR_REPLY].s_addr) != sinfo->tuple[IP_CT_DIR_REPLY].dst.ip, XT_CONNTRACK_REPLDST))
181 return 0;
184 if(sinfo->flags & XT_CONNTRACK_STATUS) {
185 if (!ct || FWINV((ct->status & sinfo->statusmask) == 0, XT_CONNTRACK_STATUS))
186 return 0;
189 if(sinfo->flags & XT_CONNTRACK_EXPIRES) {
190 unsigned long expires;
192 if(!ct)
193 return 0;
195 expires = timer_pending(&ct->timeout) ? (ct->timeout.expires - jiffies)/HZ : 0;
197 if (FWINV(!(expires >= sinfo->expires_min && expires <= sinfo->expires_max), XT_CONNTRACK_EXPIRES))
198 return 0;
201 return 1;
204 #endif /* CONFIG_NF_IP_CONNTRACK */
206 static int
207 checkentry(const char *tablename,
208 const void *ip,
209 const struct xt_match *match,
210 void *matchinfo,
211 unsigned int hook_mask)
213 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
214 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
215 printk(KERN_WARNING "can't load nf_conntrack support for "
216 "proto=%d\n", match->family);
217 return 0;
219 #endif
220 return 1;
223 static void
224 destroy(const struct xt_match *match, void *matchinfo)
226 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
227 nf_ct_l3proto_module_put(match->family);
228 #endif
231 static struct xt_match conntrack_match = {
232 .name = "conntrack",
233 .match = match,
234 .checkentry = checkentry,
235 .destroy = destroy,
236 .matchsize = sizeof(struct xt_conntrack_info),
237 .family = AF_INET,
238 .me = THIS_MODULE,
241 static int __init xt_conntrack_init(void)
243 need_conntrack();
244 return xt_register_match(&conntrack_match);
247 static void __exit xt_conntrack_fini(void)
249 xt_unregister_match(&conntrack_match);
252 module_init(xt_conntrack_init);
253 module_exit(xt_conntrack_fini);