act_ipt.c 7.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * net/sched/act_ipt.c		iptables target interface
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10
 *
 *TODO: Add other tables. For now we only support the ipv4 table targets
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
11
 * Copyright:	Jamal Hadi Salim (2002-13)
L
Linus Torvalds 已提交
12 13 14 15 16 17 18 19 20 21
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/module.h>
#include <linux/init.h>
22
#include <linux/slab.h>
23
#include <net/netlink.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29 30
#include <net/pkt_sched.h>
#include <linux/tc_act/tc_ipt.h>
#include <net/tc_act/tc_ipt.h>

#include <linux/netfilter_ipv4/ip_tables.h>


31
#define IPT_TAB_MASK     15
L
Linus Torvalds 已提交
32

33
static int ipt_init_target(struct xt_entry_target *t, char *table, unsigned int hook)
L
Linus Torvalds 已提交
34
{
35
	struct xt_tgchk_param par;
36
	struct xt_target *target;
L
Linus Torvalds 已提交
37 38
	int ret = 0;

39 40
	target = xt_request_find_target(AF_INET, t->u.user.name,
					t->u.user.revision);
41 42
	if (IS_ERR(target))
		return PTR_ERR(target);
L
Linus Torvalds 已提交
43 44

	t->u.kernel.target = target;
45 46 47 48 49
	par.table     = table;
	par.entryinfo = NULL;
	par.target    = target;
	par.targinfo  = t->data;
	par.hook_mask = hook;
50
	par.family    = NFPROTO_IPV4;
L
Linus Torvalds 已提交
51

52
	ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
53
	if (ret < 0) {
54
		module_put(t->u.kernel.target->me);
55
		return ret;
56
	}
57
	return 0;
L
Linus Torvalds 已提交
58 59
}

60
static void ipt_destroy_target(struct xt_entry_target *t)
L
Linus Torvalds 已提交
61
{
62 63 64 65 66 67 68
	struct xt_tgdtor_param par = {
		.target   = t->u.kernel.target,
		.targinfo = t->data,
	};
	if (par.target->destroy != NULL)
		par.target->destroy(&par);
	module_put(par.target->me);
L
Linus Torvalds 已提交
69 70
}

W
WANG Cong 已提交
71
static void tcf_ipt_release(struct tc_action *a, int bind)
L
Linus Torvalds 已提交
72
{
73
	struct tcf_ipt *ipt = to_ipt(a);
W
WANG Cong 已提交
74 75 76
	ipt_destroy_target(ipt->tcfi_t);
	kfree(ipt->tcfi_tname);
	kfree(ipt->tcfi_t);
L
Linus Torvalds 已提交
77 78
}

79 80 81 82
static const struct nla_policy ipt_policy[TCA_IPT_MAX + 1] = {
	[TCA_IPT_TABLE]	= { .type = NLA_STRING, .len = IFNAMSIZ },
	[TCA_IPT_HOOK]	= { .type = NLA_U32 },
	[TCA_IPT_INDEX]	= { .type = NLA_U32 },
83
	[TCA_IPT_TARG]	= { .len = sizeof(struct xt_entry_target) },
84 85
};

86
static int tcf_ipt_init(struct net *net, struct nlattr *nla, struct nlattr *est,
87
			struct tc_action *a, int ovr, int bind)
L
Linus Torvalds 已提交
88
{
89
	struct nlattr *tb[TCA_IPT_MAX + 1];
90
	struct tcf_ipt *ipt;
91
	struct xt_entry_target *td, *t;
L
Linus Torvalds 已提交
92 93 94 95 96
	char *tname;
	int ret = 0, err;
	u32 hook = 0;
	u32 index = 0;

97
	if (nla == NULL)
L
Linus Torvalds 已提交
98 99
		return -EINVAL;

100
	err = nla_parse_nested(tb, TCA_IPT_MAX, nla, ipt_policy);
101 102 103
	if (err < 0)
		return err;

104
	if (tb[TCA_IPT_HOOK] == NULL)
L
Linus Torvalds 已提交
105
		return -EINVAL;
106
	if (tb[TCA_IPT_TARG] == NULL)
L
Linus Torvalds 已提交
107
		return -EINVAL;
108

109
	td = (struct xt_entry_target *)nla_data(tb[TCA_IPT_TARG]);
110
	if (nla_len(tb[TCA_IPT_TARG]) < td->u.target_size)
L
Linus Torvalds 已提交
111 112
		return -EINVAL;

113
	if (tb[TCA_IPT_INDEX] != NULL)
114
		index = nla_get_u32(tb[TCA_IPT_INDEX]);
L
Linus Torvalds 已提交
115

116
	if (!tcf_hash_check(index, a, bind) ) {
117
		ret = tcf_hash_create(index, est, a, sizeof(*ipt), bind, false);
118 119
		if (ret)
			return ret;
L
Linus Torvalds 已提交
120 121
		ret = ACT_P_CREATED;
	} else {
122 123
		if (bind)/* dont override defaults */
			return 0;
W
WANG Cong 已提交
124
		tcf_hash_release(a, bind);
125 126

		if (!ovr)
L
Linus Torvalds 已提交
127 128
			return -EEXIST;
	}
129
	ipt = to_ipt(a);
L
Linus Torvalds 已提交
130

131
	hook = nla_get_u32(tb[TCA_IPT_HOOK]);
L
Linus Torvalds 已提交
132 133 134

	err = -ENOMEM;
	tname = kmalloc(IFNAMSIZ, GFP_KERNEL);
135
	if (unlikely(!tname))
L
Linus Torvalds 已提交
136
		goto err1;
137 138
	if (tb[TCA_IPT_TABLE] == NULL ||
	    nla_strlcpy(tname, tb[TCA_IPT_TABLE], IFNAMSIZ) >= IFNAMSIZ)
L
Linus Torvalds 已提交
139 140
		strcpy(tname, "mangle");

141
	t = kmemdup(td, td->u.target_size, GFP_KERNEL);
142
	if (unlikely(!t))
L
Linus Torvalds 已提交
143 144
		goto err2;

E
Eric Dumazet 已提交
145 146
	err = ipt_init_target(t, tname, hook);
	if (err < 0)
L
Linus Torvalds 已提交
147 148
		goto err3;

149
	spin_lock_bh(&ipt->tcf_lock);
L
Linus Torvalds 已提交
150
	if (ret != ACT_P_CREATED) {
151 152 153
		ipt_destroy_target(ipt->tcfi_t);
		kfree(ipt->tcfi_tname);
		kfree(ipt->tcfi_t);
L
Linus Torvalds 已提交
154
	}
155 156 157 158
	ipt->tcfi_tname = tname;
	ipt->tcfi_t     = t;
	ipt->tcfi_hook  = hook;
	spin_unlock_bh(&ipt->tcf_lock);
L
Linus Torvalds 已提交
159
	if (ret == ACT_P_CREATED)
160
		tcf_hash_insert(a);
L
Linus Torvalds 已提交
161 162 163 164 165 166 167
	return ret;

err3:
	kfree(t);
err2:
	kfree(tname);
err1:
168 169
	if (ret == ACT_P_CREATED)
		tcf_hash_cleanup(a, est);
L
Linus Torvalds 已提交
170 171 172
	return err;
}

173
static int tcf_ipt(struct sk_buff *skb, const struct tc_action *a,
174
		   struct tcf_result *res)
L
Linus Torvalds 已提交
175 176
{
	int ret = 0, result = 0;
177
	struct tcf_ipt *ipt = a->priv;
178
	struct xt_action_param par;
L
Linus Torvalds 已提交
179

180 181
	if (skb_unclone(skb, GFP_ATOMIC))
		return TC_ACT_UNSPEC;
L
Linus Torvalds 已提交
182

183
	spin_lock(&ipt->tcf_lock);
L
Linus Torvalds 已提交
184

185
	ipt->tcf_tm.lastuse = jiffies;
186
	bstats_update(&ipt->tcf_bstats, skb);
L
Linus Torvalds 已提交
187 188

	/* yes, we have to worry about both in and out dev
E
Eric Dumazet 已提交
189 190 191
	 * worry later - danger - this API seems to have changed
	 * from earlier kernels
	 */
192
	par.net	     = dev_net(skb->dev);
193 194 195 196 197 198 199
	par.in       = skb->dev;
	par.out      = NULL;
	par.hooknum  = ipt->tcfi_hook;
	par.target   = ipt->tcfi_t->u.kernel.target;
	par.targinfo = ipt->tcfi_t->data;
	ret = par.target->target(skb, &par);

L
Linus Torvalds 已提交
200 201 202 203 204 205
	switch (ret) {
	case NF_ACCEPT:
		result = TC_ACT_OK;
		break;
	case NF_DROP:
		result = TC_ACT_SHOT;
206
		ipt->tcf_qstats.drops++;
L
Linus Torvalds 已提交
207
		break;
208
	case XT_CONTINUE:
L
Linus Torvalds 已提交
209 210 211
		result = TC_ACT_PIPE;
		break;
	default:
212 213
		net_notice_ratelimited("tc filter: Bogus netfilter code %d assume ACCEPT\n",
				       ret);
L
Linus Torvalds 已提交
214 215 216
		result = TC_POLICE_OK;
		break;
	}
217
	spin_unlock(&ipt->tcf_lock);
L
Linus Torvalds 已提交
218 219 220 221
	return result;

}

222
static int tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
L
Linus Torvalds 已提交
223
{
224
	unsigned char *b = skb_tail_pointer(skb);
225
	struct tcf_ipt *ipt = a->priv;
226
	struct xt_entry_target *t;
L
Linus Torvalds 已提交
227 228 229 230
	struct tcf_t tm;
	struct tc_cnt c;

	/* for simple targets kernel size == user size
E
Eric Dumazet 已提交
231 232 233
	 * user name = target name
	 * for foolproof you need to not assume this
	 */
L
Linus Torvalds 已提交
234

235
	t = kmemdup(ipt->tcfi_t, ipt->tcfi_t->u.user.target_size, GFP_ATOMIC);
236
	if (unlikely(!t))
237
		goto nla_put_failure;
L
Linus Torvalds 已提交
238

239 240 241 242
	c.bindcnt = ipt->tcf_bindcnt - bind;
	c.refcnt = ipt->tcf_refcnt - ref;
	strcpy(t->u.user.name, ipt->tcfi_t->u.kernel.target->name);

243 244 245 246 247 248
	if (nla_put(skb, TCA_IPT_TARG, ipt->tcfi_t->u.user.target_size, t) ||
	    nla_put_u32(skb, TCA_IPT_INDEX, ipt->tcf_index) ||
	    nla_put_u32(skb, TCA_IPT_HOOK, ipt->tcfi_hook) ||
	    nla_put(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c) ||
	    nla_put_string(skb, TCA_IPT_TABLE, ipt->tcfi_tname))
		goto nla_put_failure;
249 250 251
	tm.install = jiffies_to_clock_t(jiffies - ipt->tcf_tm.install);
	tm.lastuse = jiffies_to_clock_t(jiffies - ipt->tcf_tm.lastuse);
	tm.expires = jiffies_to_clock_t(ipt->tcf_tm.expires);
252 253
	if (nla_put(skb, TCA_IPT_TM, sizeof (tm), &tm))
		goto nla_put_failure;
L
Linus Torvalds 已提交
254 255 256
	kfree(t);
	return skb->len;

257
nla_put_failure:
258
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
259 260 261 262 263 264 265 266 267 268
	kfree(t);
	return -1;
}

static struct tc_action_ops act_ipt_ops = {
	.kind		=	"ipt",
	.type		=	TCA_ACT_IPT,
	.owner		=	THIS_MODULE,
	.act		=	tcf_ipt,
	.dump		=	tcf_ipt_dump,
269
	.cleanup	=	tcf_ipt_release,
L
Linus Torvalds 已提交
270 271 272
	.init		=	tcf_ipt_init,
};

273 274
static struct tc_action_ops act_xt_ops = {
	.kind		=	"xt",
275
	.type		=	TCA_ACT_XT,
276 277 278
	.owner		=	THIS_MODULE,
	.act		=	tcf_ipt,
	.dump		=	tcf_ipt_dump,
279
	.cleanup	=	tcf_ipt_release,
280 281 282 283
	.init		=	tcf_ipt_init,
};

MODULE_AUTHOR("Jamal Hadi Salim(2002-13)");
L
Linus Torvalds 已提交
284 285
MODULE_DESCRIPTION("Iptables target actions");
MODULE_LICENSE("GPL");
286
MODULE_ALIAS("act_xt");
L
Linus Torvalds 已提交
287

288
static int __init ipt_init_module(void)
L
Linus Torvalds 已提交
289
{
290
	int ret1, ret2;
291

292
	ret1 = tcf_register_action(&act_xt_ops, IPT_TAB_MASK);
293 294
	if (ret1 < 0)
		printk("Failed to load xt action\n");
295
	ret2 = tcf_register_action(&act_ipt_ops, IPT_TAB_MASK);
296 297 298
	if (ret2 < 0)
		printk("Failed to load ipt action\n");

299
	if (ret1 < 0 && ret2 < 0) {
300
		return ret1;
301
	} else
302
		return 0;
L
Linus Torvalds 已提交
303 304
}

305
static void __exit ipt_cleanup_module(void)
L
Linus Torvalds 已提交
306
{
307
	tcf_unregister_action(&act_xt_ops);
L
Linus Torvalds 已提交
308 309 310 311 312
	tcf_unregister_action(&act_ipt_ops);
}

module_init(ipt_init_module);
module_exit(ipt_cleanup_module);