xt_conntrack.c 5.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* Kernel module to match connection tracking information.
 * Superset of Rusty's minimalistic state match.
 *
 * (C) 2001  Marc Boucher (marc@mbsi.ca).
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/skbuff.h>
13 14
#include <linux/netfilter/x_tables.h>
#include <linux/netfilter/xt_conntrack.h>
15
#include <net/netfilter/nf_conntrack.h>
L
Linus Torvalds 已提交
16 17 18 19

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
MODULE_DESCRIPTION("iptables connection tracking match module");
20
MODULE_ALIAS("ipt_conntrack");
L
Linus Torvalds 已提交
21

22
static bool
23 24 25
match(const struct sk_buff *skb,
      const struct net_device *in,
      const struct net_device *out,
26
      const struct xt_match *match,
27 28
      const void *matchinfo,
      int offset,
29
      unsigned int protoff,
30
      bool *hotdrop)
31
{
32
	const struct xt_conntrack_info *sinfo = matchinfo;
33
	const struct nf_conn *ct;
34 35 36
	enum ip_conntrack_info ctinfo;
	unsigned int statebit;

37
	ct = nf_ct_get(skb, &ctinfo);
38 39 40 41

#define FWINV(bool,invflg) ((bool) ^ !!(sinfo->invflags & invflg))

	if (ct == &nf_conntrack_untracked)
42
		statebit = XT_CONNTRACK_STATE_UNTRACKED;
43
	else if (ct)
44 45 46 47
		statebit = XT_CONNTRACK_STATE_BIT(ctinfo);
	else
		statebit = XT_CONNTRACK_STATE_INVALID;

48
	if (sinfo->flags & XT_CONNTRACK_STATE) {
49
		if (ct) {
50
			if (test_bit(IPS_SRC_NAT_BIT, &ct->status))
51
				statebit |= XT_CONNTRACK_STATE_SNAT;
52
			if (test_bit(IPS_DST_NAT_BIT, &ct->status))
53
				statebit |= XT_CONNTRACK_STATE_DNAT;
54
		}
55 56
		if (FWINV((statebit & sinfo->statemask) == 0,
			  XT_CONNTRACK_STATE))
57
			return false;
58 59
	}

60 61
	if (ct == NULL) {
		if (sinfo->flags & ~XT_CONNTRACK_STATE)
62 63
			return false;
		return true;
64 65
	}

66 67
	if (sinfo->flags & XT_CONNTRACK_PROTO &&
	    FWINV(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum !=
68
		  sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum,
69
		  XT_CONNTRACK_PROTO))
70
		return false;
71 72 73

	if (sinfo->flags & XT_CONNTRACK_ORIGSRC &&
	    FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip &
74
		   sinfo->sipmsk[IP_CT_DIR_ORIGINAL].s_addr) !=
75 76
		  sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip,
		  XT_CONNTRACK_ORIGSRC))
77
		return false;
78

79 80
	if (sinfo->flags & XT_CONNTRACK_ORIGDST &&
	    FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip &
81
		   sinfo->dipmsk[IP_CT_DIR_ORIGINAL].s_addr) !=
82 83
		  sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip,
		  XT_CONNTRACK_ORIGDST))
84
		return false;
85

86 87
	if (sinfo->flags & XT_CONNTRACK_REPLSRC &&
	    FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip &
88
		   sinfo->sipmsk[IP_CT_DIR_REPLY].s_addr) !=
89 90
		  sinfo->tuple[IP_CT_DIR_REPLY].src.ip,
		  XT_CONNTRACK_REPLSRC))
91
		return false;
92

93 94
	if (sinfo->flags & XT_CONNTRACK_REPLDST &&
	    FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip &
95
		   sinfo->dipmsk[IP_CT_DIR_REPLY].s_addr) !=
96 97
		  sinfo->tuple[IP_CT_DIR_REPLY].dst.ip,
		  XT_CONNTRACK_REPLDST))
98
		return false;
99

100 101
	if (sinfo->flags & XT_CONNTRACK_STATUS &&
	    FWINV((ct->status & sinfo->statusmask) == 0,
102
		  XT_CONNTRACK_STATUS))
103
		return false;
104

105 106 107
	if(sinfo->flags & XT_CONNTRACK_EXPIRES) {
		unsigned long expires = timer_pending(&ct->timeout) ?
					(ct->timeout.expires - jiffies)/HZ : 0;
108

109 110 111
		if (FWINV(!(expires >= sinfo->expires_min &&
			    expires <= sinfo->expires_max),
			  XT_CONNTRACK_EXPIRES))
112
			return false;
113
	}
114
	return true;
115 116
}

117
static bool
118 119 120 121 122 123 124
checkentry(const char *tablename,
	   const void *ip,
	   const struct xt_match *match,
	   void *matchinfo,
	   unsigned int hook_mask)
{
	if (nf_ct_l3proto_try_module_get(match->family) < 0) {
125
		printk(KERN_WARNING "can't load conntrack support for "
126
				    "proto=%d\n", match->family);
127
		return false;
128
	}
129
	return true;
130 131
}

132
static void destroy(const struct xt_match *match, void *matchinfo)
133 134 135 136
{
	nf_ct_l3proto_module_put(match->family);
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
#ifdef CONFIG_COMPAT
struct compat_xt_conntrack_info
{
	compat_uint_t			statemask;
	compat_uint_t			statusmask;
	struct ip_conntrack_old_tuple	tuple[IP_CT_DIR_MAX];
	struct in_addr			sipmsk[IP_CT_DIR_MAX];
	struct in_addr			dipmsk[IP_CT_DIR_MAX];
	compat_ulong_t			expires_min;
	compat_ulong_t			expires_max;
	u_int8_t			flags;
	u_int8_t			invflags;
};

static void compat_from_user(void *dst, void *src)
{
153
	const struct compat_xt_conntrack_info *cm = src;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	struct xt_conntrack_info m = {
		.statemask	= cm->statemask,
		.statusmask	= cm->statusmask,
		.expires_min	= cm->expires_min,
		.expires_max	= cm->expires_max,
		.flags		= cm->flags,
		.invflags	= cm->invflags,
	};
	memcpy(m.tuple, cm->tuple, sizeof(m.tuple));
	memcpy(m.sipmsk, cm->sipmsk, sizeof(m.sipmsk));
	memcpy(m.dipmsk, cm->dipmsk, sizeof(m.dipmsk));
	memcpy(dst, &m, sizeof(m));
}

static int compat_to_user(void __user *dst, void *src)
{
170
	const struct xt_conntrack_info *m = src;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	struct compat_xt_conntrack_info cm = {
		.statemask	= m->statemask,
		.statusmask	= m->statusmask,
		.expires_min	= m->expires_min,
		.expires_max	= m->expires_max,
		.flags		= m->flags,
		.invflags	= m->invflags,
	};
	memcpy(cm.tuple, m->tuple, sizeof(cm.tuple));
	memcpy(cm.sipmsk, m->sipmsk, sizeof(cm.sipmsk));
	memcpy(cm.dipmsk, m->dipmsk, sizeof(cm.dipmsk));
	return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
}
#endif

186
static struct xt_match conntrack_match __read_mostly = {
L
Linus Torvalds 已提交
187
	.name		= "conntrack",
188
	.match		= match,
189 190
	.checkentry	= checkentry,
	.destroy	= destroy,
191
	.matchsize	= sizeof(struct xt_conntrack_info),
192 193 194 195 196
#ifdef CONFIG_COMPAT
	.compatsize	= sizeof(struct compat_xt_conntrack_info),
	.compat_from_user = compat_from_user,
	.compat_to_user	= compat_to_user,
#endif
197
	.family		= AF_INET,
L
Linus Torvalds 已提交
198 199 200
	.me		= THIS_MODULE,
};

201
static int __init xt_conntrack_init(void)
L
Linus Torvalds 已提交
202
{
203
	return xt_register_match(&conntrack_match);
L
Linus Torvalds 已提交
204 205
}

206
static void __exit xt_conntrack_fini(void)
L
Linus Torvalds 已提交
207
{
208
	xt_unregister_match(&conntrack_match);
L
Linus Torvalds 已提交
209 210
}

211 212
module_init(xt_conntrack_init);
module_exit(xt_conntrack_fini);