blob: 0ea501a2fda5b03458ef0bdc510e36ef7f28660a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Kernel module to match connection tracking information.
2 * Superset of Rusty's minimalistic state match.
3 *
4 * (C) 2001 Marc Boucher (marc@mbsi.ca).
5 *
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 */
10
11#include <linux/module.h>
12#include <linux/skbuff.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080013
14#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/netfilter_ipv4/ip_conntrack.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080016#include <linux/netfilter_ipv4/ip_conntrack_tuple.h>
17#else
18#include <net/netfilter/nf_conntrack.h>
19#endif
20
Harald Welte2e4e6a12006-01-12 13:30:04 -080021#include <linux/netfilter/x_tables.h>
22#include <linux/netfilter/xt_conntrack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24MODULE_LICENSE("GPL");
25MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
26MODULE_DESCRIPTION("iptables connection tracking match module");
Harald Welte2e4e6a12006-01-12 13:30:04 -080027MODULE_ALIAS("ipt_conntrack");
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080029#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int
32match(const struct sk_buff *skb,
33 const struct net_device *in,
34 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080035 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 const void *matchinfo,
37 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080038 unsigned int protoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 int *hotdrop)
40{
Harald Welte2e4e6a12006-01-12 13:30:04 -080041 const struct xt_conntrack_info *sinfo = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct ip_conntrack *ct;
43 enum ip_conntrack_info ctinfo;
44 unsigned int statebit;
45
46 ct = ip_conntrack_get((struct sk_buff *)skb, &ctinfo);
47
Patrick McHardy50b9f1d2006-09-20 11:58:17 -070048#define FWINV(bool, invflg) ((bool) ^ !!(sinfo->invflags & invflg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 if (ct == &ip_conntrack_untracked)
Harald Welte2e4e6a12006-01-12 13:30:04 -080051 statebit = XT_CONNTRACK_STATE_UNTRACKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 else if (ct)
Harald Welte2e4e6a12006-01-12 13:30:04 -080053 statebit = XT_CONNTRACK_STATE_BIT(ctinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 else
Harald Welte2e4e6a12006-01-12 13:30:04 -080055 statebit = XT_CONNTRACK_STATE_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Patrick McHardy50b9f1d2006-09-20 11:58:17 -070057 if (sinfo->flags & XT_CONNTRACK_STATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (ct) {
Patrick McHardy50b9f1d2006-09-20 11:58:17 -070059 if (test_bit(IPS_SRC_NAT_BIT, &ct->status))
Harald Welte2e4e6a12006-01-12 13:30:04 -080060 statebit |= XT_CONNTRACK_STATE_SNAT;
Patrick McHardy50b9f1d2006-09-20 11:58:17 -070061 if (test_bit(IPS_DST_NAT_BIT, &ct->status))
Harald Welte2e4e6a12006-01-12 13:30:04 -080062 statebit |= XT_CONNTRACK_STATE_DNAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
Patrick McHardy50b9f1d2006-09-20 11:58:17 -070064 if (FWINV((statebit & sinfo->statemask) == 0,
65 XT_CONNTRACK_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 0;
67 }
68
Patrick McHardy50b9f1d2006-09-20 11:58:17 -070069 if (ct == NULL) {
70 if (sinfo->flags & ~XT_CONNTRACK_STATE)
71 return 0;
72 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74
Patrick McHardy50b9f1d2006-09-20 11:58:17 -070075 if (sinfo->flags & XT_CONNTRACK_PROTO &&
76 FWINV(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum !=
77 sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum,
78 XT_CONNTRACK_PROTO))
79 return 0;
80
81 if (sinfo->flags & XT_CONNTRACK_ORIGSRC &&
82 FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip &
83 sinfo->sipmsk[IP_CT_DIR_ORIGINAL].s_addr) !=
84 sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip,
85 XT_CONNTRACK_ORIGSRC))
86 return 0;
87
88 if (sinfo->flags & XT_CONNTRACK_ORIGDST &&
89 FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip &
90 sinfo->dipmsk[IP_CT_DIR_ORIGINAL].s_addr) !=
91 sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip,
92 XT_CONNTRACK_ORIGDST))
93 return 0;
94
95 if (sinfo->flags & XT_CONNTRACK_REPLSRC &&
96 FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip &
97 sinfo->sipmsk[IP_CT_DIR_REPLY].s_addr) !=
98 sinfo->tuple[IP_CT_DIR_REPLY].src.ip,
99 XT_CONNTRACK_REPLSRC))
100 return 0;
101
102 if (sinfo->flags & XT_CONNTRACK_REPLDST &&
103 FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip &
104 sinfo->dipmsk[IP_CT_DIR_REPLY].s_addr) !=
105 sinfo->tuple[IP_CT_DIR_REPLY].dst.ip,
106 XT_CONNTRACK_REPLDST))
107 return 0;
108
109 if (sinfo->flags & XT_CONNTRACK_STATUS &&
110 FWINV((ct->status & sinfo->statusmask) == 0,
111 XT_CONNTRACK_STATUS))
112 return 0;
113
114 if (sinfo->flags & XT_CONNTRACK_EXPIRES) {
115 unsigned long expires = timer_pending(&ct->timeout) ?
116 (ct->timeout.expires - jiffies)/HZ : 0;
117
118 if (FWINV(!(expires >= sinfo->expires_min &&
119 expires <= sinfo->expires_max),
120 XT_CONNTRACK_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return 0;
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 1;
124}
125
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800126#else /* CONFIG_IP_NF_CONNTRACK */
127static int
128match(const struct sk_buff *skb,
129 const struct net_device *in,
130 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -0800131 const struct xt_match *match,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800132 const void *matchinfo,
133 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800134 unsigned int protoff,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800135 int *hotdrop)
136{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800137 const struct xt_conntrack_info *sinfo = matchinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800138 struct nf_conn *ct;
139 enum ip_conntrack_info ctinfo;
140 unsigned int statebit;
141
142 ct = nf_ct_get((struct sk_buff *)skb, &ctinfo);
143
144#define FWINV(bool,invflg) ((bool) ^ !!(sinfo->invflags & invflg))
145
146 if (ct == &nf_conntrack_untracked)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800147 statebit = XT_CONNTRACK_STATE_UNTRACKED;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800148 else if (ct)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800149 statebit = XT_CONNTRACK_STATE_BIT(ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800150 else
Harald Welte2e4e6a12006-01-12 13:30:04 -0800151 statebit = XT_CONNTRACK_STATE_INVALID;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800152
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700153 if (sinfo->flags & XT_CONNTRACK_STATE) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800154 if (ct) {
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700155 if (test_bit(IPS_SRC_NAT_BIT, &ct->status))
Harald Welte2e4e6a12006-01-12 13:30:04 -0800156 statebit |= XT_CONNTRACK_STATE_SNAT;
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700157 if (test_bit(IPS_DST_NAT_BIT, &ct->status))
Harald Welte2e4e6a12006-01-12 13:30:04 -0800158 statebit |= XT_CONNTRACK_STATE_DNAT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800159 }
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700160 if (FWINV((statebit & sinfo->statemask) == 0,
161 XT_CONNTRACK_STATE))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800162 return 0;
163 }
164
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700165 if (ct == NULL) {
166 if (sinfo->flags & ~XT_CONNTRACK_STATE)
167 return 0;
168 return 1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800169 }
170
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700171 if (sinfo->flags & XT_CONNTRACK_PROTO &&
172 FWINV(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum !=
173 sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum,
174 XT_CONNTRACK_PROTO))
175 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800176
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700177 if (sinfo->flags & XT_CONNTRACK_ORIGSRC &&
178 FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip &
179 sinfo->sipmsk[IP_CT_DIR_ORIGINAL].s_addr) !=
180 sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip,
181 XT_CONNTRACK_ORIGSRC))
182 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800183
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700184 if (sinfo->flags & XT_CONNTRACK_ORIGDST &&
185 FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip &
186 sinfo->dipmsk[IP_CT_DIR_ORIGINAL].s_addr) !=
187 sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip,
188 XT_CONNTRACK_ORIGDST))
189 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800190
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700191 if (sinfo->flags & XT_CONNTRACK_REPLSRC &&
192 FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip &
193 sinfo->sipmsk[IP_CT_DIR_REPLY].s_addr) !=
194 sinfo->tuple[IP_CT_DIR_REPLY].src.ip,
195 XT_CONNTRACK_REPLSRC))
196 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800197
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700198 if (sinfo->flags & XT_CONNTRACK_REPLDST &&
199 FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip &
200 sinfo->dipmsk[IP_CT_DIR_REPLY].s_addr) !=
201 sinfo->tuple[IP_CT_DIR_REPLY].dst.ip,
202 XT_CONNTRACK_REPLDST))
203 return 0;
204
205 if (sinfo->flags & XT_CONNTRACK_STATUS &&
206 FWINV((ct->status & sinfo->statusmask) == 0,
207 XT_CONNTRACK_STATUS))
208 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800209
Harald Welte2e4e6a12006-01-12 13:30:04 -0800210 if(sinfo->flags & XT_CONNTRACK_EXPIRES) {
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700211 unsigned long expires = timer_pending(&ct->timeout) ?
212 (ct->timeout.expires - jiffies)/HZ : 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800213
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700214 if (FWINV(!(expires >= sinfo->expires_min &&
215 expires <= sinfo->expires_max),
216 XT_CONNTRACK_EXPIRES))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800217 return 0;
218 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800219 return 1;
220}
221
222#endif /* CONFIG_NF_IP_CONNTRACK */
223
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800224static int
225checkentry(const char *tablename,
226 const void *ip,
227 const struct xt_match *match,
228 void *matchinfo,
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800229 unsigned int hook_mask)
230{
231#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
232 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
233 printk(KERN_WARNING "can't load nf_conntrack support for "
234 "proto=%d\n", match->family);
235 return 0;
236 }
237#endif
238 return 1;
239}
240
Patrick McHardy50b9f1d2006-09-20 11:58:17 -0700241static void destroy(const struct xt_match *match, void *matchinfo)
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800242{
243#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
244 nf_ct_l3proto_module_put(match->family);
245#endif
246}
247
Harald Welte2e4e6a12006-01-12 13:30:04 -0800248static struct xt_match conntrack_match = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .name = "conntrack",
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800250 .match = match,
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800251 .checkentry = checkentry,
252 .destroy = destroy,
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800253 .matchsize = sizeof(struct xt_conntrack_info),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800254 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 .me = THIS_MODULE,
256};
257
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800258static int __init xt_conntrack_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800260 need_conntrack();
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700261 return xt_register_match(&conntrack_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800264static void __exit xt_conntrack_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800266 xt_unregister_match(&conntrack_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800269module_init(xt_conntrack_init);
270module_exit(xt_conntrack_fini);