Blame view

net/sched/act_ipt.c 10.6 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
  /*
0c6965dd3   Jiri Pirko   sched: fix act fi...
3
   * net/sched/act_ipt.c		iptables target interface
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
   *
   *TODO: Add other tables. For now we only support the ipv4 table targets
   *
0dcffd096   Jamal Hadi Salim   net_sched: act_ip...
7
   * Copyright:	Jamal Hadi Salim (2002-13)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
  #include <linux/types.h>
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
  #include <linux/string.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
  #include <linux/errno.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
  #include <linux/skbuff.h>
  #include <linux/rtnetlink.h>
  #include <linux/module.h>
  #include <linux/init.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
17
  #include <linux/slab.h>
dc5fc579b   Arnaldo Carvalho de Melo   [NETLINK]: Use nl...
18
  #include <net/netlink.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
22
23
  #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>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24

c7d03a00b   Alexey Dobriyan   netns: make struc...
25
  static unsigned int ipt_net_id;
a85a970af   WANG Cong   net_sched: move t...
26
  static struct tc_action_ops act_ipt_ops;
ddf97ccdd   WANG Cong   net_sched: add ne...
27

c7d03a00b   Alexey Dobriyan   netns: make struc...
28
  static unsigned int xt_net_id;
a85a970af   WANG Cong   net_sched: move t...
29
  static struct tc_action_ops act_xt_ops;
ddf97ccdd   WANG Cong   net_sched: add ne...
30

ec0acb093   Xin Long   net: sched: set x...
31
32
  static int ipt_init_target(struct net *net, struct xt_entry_target *t,
  			   char *table, unsigned int hook)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
  {
af5d6dc20   Jan Engelhardt   netfilter: xtable...
34
  	struct xt_tgchk_param par;
e60a13e03   Jan Engelhardt   [NETFILTER]: {ip,...
35
  	struct xt_target *target;
4f8a881ac   Xin Long   net: sched: fix N...
36
  	struct ipt_entry e = {};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
  	int ret = 0;
239a87c87   Patrick McHardy   [NET_SCHED]: act_...
38
39
  	target = xt_request_find_target(AF_INET, t->u.user.name,
  					t->u.user.revision);
d2a7b6bad   Jan Engelhardt   netfilter: xtable...
40
41
  	if (IS_ERR(target))
  		return PTR_ERR(target);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
  	t->u.kernel.target = target;
96d970305   Xin Long   net: sched: set x...
44
  	memset(&par, 0, sizeof(par));
ec0acb093   Xin Long   net: sched: set x...
45
  	par.net       = net;
af5d6dc20   Jan Engelhardt   netfilter: xtable...
46
  	par.table     = table;
4f8a881ac   Xin Long   net: sched: fix N...
47
  	par.entryinfo = &e;
af5d6dc20   Jan Engelhardt   netfilter: xtable...
48
49
50
  	par.target    = target;
  	par.targinfo  = t->data;
  	par.hook_mask = hook;
916a917df   Jan Engelhardt   netfilter: xtable...
51
  	par.family    = NFPROTO_IPV4;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52

916a917df   Jan Engelhardt   netfilter: xtable...
53
  	ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
367c67900   Jan Engelhardt   netfilter: xtable...
54
  	if (ret < 0) {
239a87c87   Patrick McHardy   [NET_SCHED]: act_...
55
  		module_put(t->u.kernel.target->me);
18118cdbf   Patrick McHardy   [NETFILTER]: ipt ...
56
  		return ret;
239a87c87   Patrick McHardy   [NET_SCHED]: act_...
57
  	}
367c67900   Jan Engelhardt   netfilter: xtable...
58
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
  }
981471bd3   Cong Wang   net_sched: fix a ...
60
  static void ipt_destroy_target(struct xt_entry_target *t, struct net *net)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
  {
a2df1648b   Jan Engelhardt   netfilter: xtable...
62
63
64
  	struct xt_tgdtor_param par = {
  		.target   = t->u.kernel.target,
  		.targinfo = t->data,
44ef548f4   Phil Sutter   net: sched: fix a...
65
  		.family   = NFPROTO_IPV4,
981471bd3   Cong Wang   net_sched: fix a ...
66
  		.net      = net,
a2df1648b   Jan Engelhardt   netfilter: xtable...
67
68
69
70
  	};
  	if (par.target->destroy != NULL)
  		par.target->destroy(&par);
  	module_put(par.target->me);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
  }
9a63b255d   Cong Wang   net_sched: remove...
72
  static void tcf_ipt_release(struct tc_action *a)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
  {
86062033f   WANG Cong   net_sched: act: h...
74
  	struct tcf_ipt *ipt = to_ipt(a);
1e46ef176   Davide Caratti   net/sched: fix id...
75
76
  
  	if (ipt->tcfi_t) {
981471bd3   Cong Wang   net_sched: fix a ...
77
  		ipt_destroy_target(ipt->tcfi_t, a->idrinfo->net);
1e46ef176   Davide Caratti   net/sched: fix id...
78
79
  		kfree(ipt->tcfi_t);
  	}
a5b5c958f   WANG Cong   net_sched: act: r...
80
  	kfree(ipt->tcfi_tname);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
  }
53b2bf3f8   Patrick McHardy   [NET_SCHED]: Use ...
82
83
84
85
  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 },
87a2e70db   Jan Engelhardt   netfilter: xtable...
86
  	[TCA_IPT_TARG]	= { .len = sizeof(struct xt_entry_target) },
53b2bf3f8   Patrick McHardy   [NET_SCHED]: Use ...
87
  };
ec0acb093   Xin Long   net: sched: set x...
88
  static int __tcf_ipt_init(struct net *net, unsigned int id, struct nlattr *nla,
a85a970af   WANG Cong   net_sched: move t...
89
  			  struct nlattr *est, struct tc_action **a,
85d0966fa   Davide Caratti   net/sched: prepar...
90
  			  const struct tc_action_ops *ops, int ovr, int bind,
abbb0d336   Vlad Buslov   net: sched: exten...
91
  			  struct tcf_proto *tp, u32 flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  {
ec0acb093   Xin Long   net: sched: set x...
93
  	struct tc_action_net *tn = net_generic(net, id);
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
94
  	struct nlattr *tb[TCA_IPT_MAX + 1];
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
95
  	struct tcf_ipt *ipt;
87a2e70db   Jan Engelhardt   netfilter: xtable...
96
  	struct xt_entry_target *td, *t;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
  	char *tname;
b2313077e   WANG Cong   net_sched: make t...
98
99
  	bool exists = false;
  	int ret = 0, err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
  	u32 hook = 0;
  	u32 index = 0;
cee63723b   Patrick McHardy   [NET_SCHED]: Prop...
102
  	if (nla == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
  		return -EINVAL;
8cb081746   Johannes Berg   netlink: make val...
104
105
  	err = nla_parse_nested_deprecated(tb, TCA_IPT_MAX, nla, ipt_policy,
  					  NULL);
cee63723b   Patrick McHardy   [NET_SCHED]: Prop...
106
107
  	if (err < 0)
  		return err;
a57f19d30   Jamal Hadi Salim   net sched: ipt ac...
108
109
  	if (tb[TCA_IPT_INDEX] != NULL)
  		index = nla_get_u32(tb[TCA_IPT_INDEX]);
0190c1d45   Vlad Buslov   net: sched: atomi...
110
111
112
113
  	err = tcf_idr_check_alloc(tn, &index, a, bind);
  	if (err < 0)
  		return err;
  	exists = err;
a57f19d30   Jamal Hadi Salim   net sched: ipt ac...
114
115
116
117
118
  	if (exists && bind)
  		return 0;
  
  	if (tb[TCA_IPT_HOOK] == NULL || tb[TCA_IPT_TARG] == NULL) {
  		if (exists)
65a206c01   Chris Mi   net/sched: Change...
119
  			tcf_idr_release(*a, bind);
0190c1d45   Vlad Buslov   net: sched: atomi...
120
121
  		else
  			tcf_idr_cleanup(tn, index);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
  		return -EINVAL;
a57f19d30   Jamal Hadi Salim   net sched: ipt ac...
123
  	}
53b2bf3f8   Patrick McHardy   [NET_SCHED]: Use ...
124

87a2e70db   Jan Engelhardt   netfilter: xtable...
125
  	td = (struct xt_entry_target *)nla_data(tb[TCA_IPT_TARG]);
aeadd93f2   Dan Carpenter   net: sched: act_i...
126
  	if (nla_len(tb[TCA_IPT_TARG]) != td->u.target_size) {
d15eccea6   WANG Cong   act_ipt: fix a bi...
127
  		if (exists)
65a206c01   Chris Mi   net/sched: Change...
128
  			tcf_idr_release(*a, bind);
0190c1d45   Vlad Buslov   net: sched: atomi...
129
130
  		else
  			tcf_idr_cleanup(tn, index);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131
  		return -EINVAL;
d15eccea6   WANG Cong   act_ipt: fix a bi...
132
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133

d15eccea6   WANG Cong   act_ipt: fix a bi...
134
  	if (!exists) {
65a206c01   Chris Mi   net/sched: Change...
135
  		ret = tcf_idr_create(tn, index, est, a, ops, bind,
e38226786   Vlad Buslov   net: sched: updat...
136
  				     false, 0);
0190c1d45   Vlad Buslov   net: sched: atomi...
137
138
  		if (ret) {
  			tcf_idr_cleanup(tn, index);
86062033f   WANG Cong   net_sched: act: h...
139
  			return ret;
0190c1d45   Vlad Buslov   net: sched: atomi...
140
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
142
  		ret = ACT_P_CREATED;
  	} else {
1a29321ed   Jamal Hadi Salim   net_sched: act: D...
143
144
  		if (bind)/* dont override defaults */
  			return 0;
1a29321ed   Jamal Hadi Salim   net_sched: act: D...
145

4e8ddd7f1   Vlad Buslov   net: sched: don't...
146
147
  		if (!ovr) {
  			tcf_idr_release(*a, bind);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
  			return -EEXIST;
4e8ddd7f1   Vlad Buslov   net: sched: don't...
149
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
  	}
1587bac49   Patrick McHardy   [NET_SCHED]: Use ...
151
  	hook = nla_get_u32(tb[TCA_IPT_HOOK]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
153
154
  
  	err = -ENOMEM;
  	tname = kmalloc(IFNAMSIZ, GFP_KERNEL);
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
155
  	if (unlikely(!tname))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
  		goto err1;
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
157
158
  	if (tb[TCA_IPT_TABLE] == NULL ||
  	    nla_strlcpy(tname, tb[TCA_IPT_TABLE], IFNAMSIZ) >= IFNAMSIZ)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
159
  		strcpy(tname, "mangle");
c7b1b2497   Arnaldo Carvalho de Melo   [SCHED]: Use kmem...
160
  	t = kmemdup(td, td->u.target_size, GFP_KERNEL);
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
161
  	if (unlikely(!t))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
  		goto err2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163

ec0acb093   Xin Long   net: sched: set x...
164
  	err = ipt_init_target(net, t, tname, hook);
cc7ec456f   Eric Dumazet   net_sched: cleanups
165
  	if (err < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
  		goto err3;
a85a970af   WANG Cong   net_sched: move t...
167
  	ipt = to_ipt(*a);
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
168
  	spin_lock_bh(&ipt->tcf_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
  	if (ret != ACT_P_CREATED) {
981471bd3   Cong Wang   net_sched: fix a ...
170
  		ipt_destroy_target(ipt->tcfi_t, net);
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
171
172
  		kfree(ipt->tcfi_tname);
  		kfree(ipt->tcfi_t);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
  	}
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
174
175
176
177
  	ipt->tcfi_tname = tname;
  	ipt->tcfi_t     = t;
  	ipt->tcfi_hook  = hook;
  	spin_unlock_bh(&ipt->tcf_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
  	if (ret == ACT_P_CREATED)
65a206c01   Chris Mi   net/sched: Change...
179
  		tcf_idr_insert(tn, *a);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
181
182
183
184
185
186
  	return ret;
  
  err3:
  	kfree(t);
  err2:
  	kfree(tname);
  err1:
8f67c90ee   Davide Caratti   net/sched: act_ip...
187
  	tcf_idr_release(*a, bind);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
189
  	return err;
  }
ddf97ccdd   WANG Cong   net_sched: add ne...
190
  static int tcf_ipt_init(struct net *net, struct nlattr *nla,
a85a970af   WANG Cong   net_sched: move t...
191
  			struct nlattr *est, struct tc_action **a, int ovr,
85d0966fa   Davide Caratti   net/sched: prepar...
192
  			int bind, bool rtnl_held, struct tcf_proto *tp,
abbb0d336   Vlad Buslov   net: sched: exten...
193
  			u32 flags, struct netlink_ext_ack *extack)
ddf97ccdd   WANG Cong   net_sched: add ne...
194
  {
ec0acb093   Xin Long   net: sched: set x...
195
  	return __tcf_ipt_init(net, ipt_net_id, nla, est, a, &act_ipt_ops, ovr,
abbb0d336   Vlad Buslov   net: sched: exten...
196
  			      bind, tp, flags);
ddf97ccdd   WANG Cong   net_sched: add ne...
197
198
199
  }
  
  static int tcf_xt_init(struct net *net, struct nlattr *nla,
a85a970af   WANG Cong   net_sched: move t...
200
  		       struct nlattr *est, struct tc_action **a, int ovr,
85d0966fa   Davide Caratti   net/sched: prepar...
201
  		       int bind, bool unlocked, struct tcf_proto *tp,
abbb0d336   Vlad Buslov   net: sched: exten...
202
  		       u32 flags, struct netlink_ext_ack *extack)
ddf97ccdd   WANG Cong   net_sched: add ne...
203
  {
ec0acb093   Xin Long   net: sched: set x...
204
  	return __tcf_ipt_init(net, xt_net_id, nla, est, a, &act_xt_ops, ovr,
abbb0d336   Vlad Buslov   net: sched: exten...
205
  			      bind, tp, flags);
ddf97ccdd   WANG Cong   net_sched: add ne...
206
  }
11b9695b3   Jamal Hadi Salim   net: sched: act_i...
207
208
  static int tcf_ipt_act(struct sk_buff *skb, const struct tc_action *a,
  		       struct tcf_result *res)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
210
  {
  	int ret = 0, result = 0;
a85a970af   WANG Cong   net_sched: move t...
211
  	struct tcf_ipt *ipt = to_ipt(a);
4b560b447   Jan Engelhardt   netfilter: xtable...
212
  	struct xt_action_param par;
613dbd957   Pablo Neira Ayuso   netfilter: x_tabl...
213
214
215
216
217
218
  	struct nf_hook_state state = {
  		.net	= dev_net(skb->dev),
  		.in	= skb->dev,
  		.hook	= ipt->tcfi_hook,
  		.pf	= NFPROTO_IPV4,
  	};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
219

14bbd6a56   Pravin B Shelar   net: Add skb_uncl...
220
221
  	if (skb_unclone(skb, GFP_ATOMIC))
  		return TC_ACT_UNSPEC;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
222

e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
223
  	spin_lock(&ipt->tcf_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224

9c4a4e488   Jamal Hadi Salim   net sched: action...
225
  	tcf_lastuse_update(&ipt->tcf_tm);
bfe0d0298   Eric Dumazet   net_sched: factor...
226
  	bstats_update(&ipt->tcf_bstats, skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
  
  	/* yes, we have to worry about both in and out dev
cc7ec456f   Eric Dumazet   net_sched: cleanups
229
230
231
  	 * worry later - danger - this API seems to have changed
  	 * from earlier kernels
  	 */
613dbd957   Pablo Neira Ayuso   netfilter: x_tabl...
232
  	par.state    = &state;
7eb355865   Jan Engelhardt   netfilter: xtable...
233
234
235
  	par.target   = ipt->tcfi_t->u.kernel.target;
  	par.targinfo = ipt->tcfi_t->data;
  	ret = par.target->target(skb, &par);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
237
238
239
240
241
  	switch (ret) {
  	case NF_ACCEPT:
  		result = TC_ACT_OK;
  		break;
  	case NF_DROP:
  		result = TC_ACT_SHOT;
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
242
  		ipt->tcf_qstats.drops++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
  		break;
243bf6e29   Jan Engelhardt   netfilter: xtable...
244
  	case XT_CONTINUE:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245
246
247
  		result = TC_ACT_PIPE;
  		break;
  	default:
e87cc4728   Joe Perches   net: Convert net_...
248
249
250
  		net_notice_ratelimited("tc filter: Bogus netfilter code %d assume ACCEPT
  ",
  				       ret);
95df1b160   WANG Cong   net_sched: remove...
251
  		result = TC_ACT_OK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
253
  		break;
  	}
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
254
  	spin_unlock(&ipt->tcf_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
256
257
  	return result;
  
  }
0b0f43fe2   Jamal Hadi Salim   net sched: indent...
258
259
  static int tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  			int ref)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  {
27a884dc3   Arnaldo Carvalho de Melo   [SK_BUFF]: Conver...
261
  	unsigned char *b = skb_tail_pointer(skb);
a85a970af   WANG Cong   net_sched: move t...
262
  	struct tcf_ipt *ipt = to_ipt(a);
87a2e70db   Jan Engelhardt   netfilter: xtable...
263
  	struct xt_entry_target *t;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
265
  	struct tcf_t tm;
  	struct tc_cnt c;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
267
  
  	/* for simple targets kernel size == user size
cc7ec456f   Eric Dumazet   net_sched: cleanups
268
269
270
  	 * user name = target name
  	 * for foolproof you need to not assume this
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271

ff25276de   Vlad Buslov   net: sched: act_i...
272
  	spin_lock_bh(&ipt->tcf_lock);
c7b1b2497   Arnaldo Carvalho de Melo   [SCHED]: Use kmem...
273
  	t = kmemdup(ipt->tcfi_t, ipt->tcfi_t->u.user.target_size, GFP_ATOMIC);
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
274
  	if (unlikely(!t))
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
275
  		goto nla_put_failure;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
276

036bb4432   Vlad Buslov   net: sched: chang...
277
278
  	c.bindcnt = atomic_read(&ipt->tcf_bindcnt) - bind;
  	c.refcnt = refcount_read(&ipt->tcf_refcnt) - ref;
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
279
  	strcpy(t->u.user.name, ipt->tcfi_t->u.kernel.target->name);
1b34ec43c   David S. Miller   pkt_sched: Stop u...
280
281
282
283
284
285
  	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;
48d8ee169   Jamal Hadi Salim   net sched actions...
286
287
  
  	tcf_tm_dump(&tm, &ipt->tcf_tm);
9854518ea   Nicolas Dichtel   sched: align nlat...
288
  	if (nla_put_64bit(skb, TCA_IPT_TM, sizeof(tm), &tm, TCA_IPT_PAD))
1b34ec43c   David S. Miller   pkt_sched: Stop u...
289
  		goto nla_put_failure;
48d8ee169   Jamal Hadi Salim   net sched actions...
290

ff25276de   Vlad Buslov   net: sched: act_i...
291
  	spin_unlock_bh(&ipt->tcf_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292
293
  	kfree(t);
  	return skb->len;
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
294
  nla_put_failure:
ff25276de   Vlad Buslov   net: sched: act_i...
295
  	spin_unlock_bh(&ipt->tcf_lock);
dc5fc579b   Arnaldo Carvalho de Melo   [NETLINK]: Use nl...
296
  	nlmsg_trim(skb, b);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
297
298
299
  	kfree(t);
  	return -1;
  }
ddf97ccdd   WANG Cong   net_sched: add ne...
300
301
  static int tcf_ipt_walker(struct net *net, struct sk_buff *skb,
  			  struct netlink_callback *cb, int type,
417801055   Alexander Aring   net: sched: act: ...
302
303
  			  const struct tc_action_ops *ops,
  			  struct netlink_ext_ack *extack)
ddf97ccdd   WANG Cong   net_sched: add ne...
304
305
  {
  	struct tc_action_net *tn = net_generic(net, ipt_net_id);
b36201455   Alexander Aring   net: sched: act: ...
306
  	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
ddf97ccdd   WANG Cong   net_sched: add ne...
307
  }
f061b48c1   Cong Wang   Revert "net: sche...
308
  static int tcf_ipt_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccdd   WANG Cong   net_sched: add ne...
309
310
  {
  	struct tc_action_net *tn = net_generic(net, ipt_net_id);
65a206c01   Chris Mi   net/sched: Change...
311
  	return tcf_idr_search(tn, a, index);
ddf97ccdd   WANG Cong   net_sched: add ne...
312
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
313
314
  static struct tc_action_ops act_ipt_ops = {
  	.kind		=	"ipt",
eddd2cf19   Eli Cohen   net: Change TCA_A...
315
  	.id		=	TCA_ID_IPT,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
316
  	.owner		=	THIS_MODULE,
11b9695b3   Jamal Hadi Salim   net: sched: act_i...
317
  	.act		=	tcf_ipt_act,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
318
  	.dump		=	tcf_ipt_dump,
86062033f   WANG Cong   net_sched: act: h...
319
  	.cleanup	=	tcf_ipt_release,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
  	.init		=	tcf_ipt_init,
ddf97ccdd   WANG Cong   net_sched: add ne...
321
322
  	.walk		=	tcf_ipt_walker,
  	.lookup		=	tcf_ipt_search,
a85a970af   WANG Cong   net_sched: move t...
323
  	.size		=	sizeof(struct tcf_ipt),
ddf97ccdd   WANG Cong   net_sched: add ne...
324
325
326
327
328
  };
  
  static __net_init int ipt_init_net(struct net *net)
  {
  	struct tc_action_net *tn = net_generic(net, ipt_net_id);
981471bd3   Cong Wang   net_sched: fix a ...
329
  	return tc_action_net_init(net, tn, &act_ipt_ops);
ddf97ccdd   WANG Cong   net_sched: add ne...
330
  }
039af9c66   Cong Wang   net_sched: switch...
331
  static void __net_exit ipt_exit_net(struct list_head *net_list)
ddf97ccdd   WANG Cong   net_sched: add ne...
332
  {
039af9c66   Cong Wang   net_sched: switch...
333
  	tc_action_net_exit(net_list, ipt_net_id);
ddf97ccdd   WANG Cong   net_sched: add ne...
334
335
336
337
  }
  
  static struct pernet_operations ipt_net_ops = {
  	.init = ipt_init_net,
039af9c66   Cong Wang   net_sched: switch...
338
  	.exit_batch = ipt_exit_net,
ddf97ccdd   WANG Cong   net_sched: add ne...
339
340
  	.id   = &ipt_net_id,
  	.size = sizeof(struct tc_action_net),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
341
  };
ddf97ccdd   WANG Cong   net_sched: add ne...
342
343
  static int tcf_xt_walker(struct net *net, struct sk_buff *skb,
  			 struct netlink_callback *cb, int type,
417801055   Alexander Aring   net: sched: act: ...
344
345
  			 const struct tc_action_ops *ops,
  			 struct netlink_ext_ack *extack)
ddf97ccdd   WANG Cong   net_sched: add ne...
346
347
  {
  	struct tc_action_net *tn = net_generic(net, xt_net_id);
b36201455   Alexander Aring   net: sched: act: ...
348
  	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
ddf97ccdd   WANG Cong   net_sched: add ne...
349
  }
f061b48c1   Cong Wang   Revert "net: sche...
350
  static int tcf_xt_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccdd   WANG Cong   net_sched: add ne...
351
352
  {
  	struct tc_action_net *tn = net_generic(net, xt_net_id);
65a206c01   Chris Mi   net/sched: Change...
353
  	return tcf_idr_search(tn, a, index);
ddf97ccdd   WANG Cong   net_sched: add ne...
354
  }
0dcffd096   Jamal Hadi Salim   net_sched: act_ip...
355
356
  static struct tc_action_ops act_xt_ops = {
  	.kind		=	"xt",
eddd2cf19   Eli Cohen   net: Change TCA_A...
357
  	.id		=	TCA_ID_XT,
0dcffd096   Jamal Hadi Salim   net_sched: act_ip...
358
  	.owner		=	THIS_MODULE,
11b9695b3   Jamal Hadi Salim   net: sched: act_i...
359
  	.act		=	tcf_ipt_act,
0dcffd096   Jamal Hadi Salim   net_sched: act_ip...
360
  	.dump		=	tcf_ipt_dump,
86062033f   WANG Cong   net_sched: act: h...
361
  	.cleanup	=	tcf_ipt_release,
ddf97ccdd   WANG Cong   net_sched: add ne...
362
363
364
  	.init		=	tcf_xt_init,
  	.walk		=	tcf_xt_walker,
  	.lookup		=	tcf_xt_search,
a85a970af   WANG Cong   net_sched: move t...
365
  	.size		=	sizeof(struct tcf_ipt),
ddf97ccdd   WANG Cong   net_sched: add ne...
366
367
368
369
370
  };
  
  static __net_init int xt_init_net(struct net *net)
  {
  	struct tc_action_net *tn = net_generic(net, xt_net_id);
981471bd3   Cong Wang   net_sched: fix a ...
371
  	return tc_action_net_init(net, tn, &act_xt_ops);
ddf97ccdd   WANG Cong   net_sched: add ne...
372
  }
039af9c66   Cong Wang   net_sched: switch...
373
  static void __net_exit xt_exit_net(struct list_head *net_list)
ddf97ccdd   WANG Cong   net_sched: add ne...
374
  {
039af9c66   Cong Wang   net_sched: switch...
375
  	tc_action_net_exit(net_list, xt_net_id);
ddf97ccdd   WANG Cong   net_sched: add ne...
376
377
378
379
  }
  
  static struct pernet_operations xt_net_ops = {
  	.init = xt_init_net,
039af9c66   Cong Wang   net_sched: switch...
380
  	.exit_batch = xt_exit_net,
ddf97ccdd   WANG Cong   net_sched: add ne...
381
382
  	.id   = &xt_net_id,
  	.size = sizeof(struct tc_action_net),
0dcffd096   Jamal Hadi Salim   net_sched: act_ip...
383
384
385
  };
  
  MODULE_AUTHOR("Jamal Hadi Salim(2002-13)");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386
387
  MODULE_DESCRIPTION("Iptables target actions");
  MODULE_LICENSE("GPL");
0dcffd096   Jamal Hadi Salim   net_sched: act_ip...
388
  MODULE_ALIAS("act_xt");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
389

e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
390
  static int __init ipt_init_module(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
  {
4f1e9d894   WANG Cong   net_sched: act: m...
392
  	int ret1, ret2;
369ba5678   WANG Cong   net_sched: init s...
393

ddf97ccdd   WANG Cong   net_sched: add ne...
394
  	ret1 = tcf_register_action(&act_xt_ops, &xt_net_ops);
0dcffd096   Jamal Hadi Salim   net_sched: act_ip...
395
  	if (ret1 < 0)
ddf97ccdd   WANG Cong   net_sched: add ne...
396
397
398
399
  		pr_err("Failed to load xt action
  ");
  
  	ret2 = tcf_register_action(&act_ipt_ops, &ipt_net_ops);
0dcffd096   Jamal Hadi Salim   net_sched: act_ip...
400
  	if (ret2 < 0)
ddf97ccdd   WANG Cong   net_sched: add ne...
401
402
  		pr_err("Failed to load ipt action
  ");
0dcffd096   Jamal Hadi Salim   net_sched: act_ip...
403

369ba5678   WANG Cong   net_sched: init s...
404
  	if (ret1 < 0 && ret2 < 0) {
0dcffd096   Jamal Hadi Salim   net_sched: act_ip...
405
  		return ret1;
369ba5678   WANG Cong   net_sched: init s...
406
  	} else
0dcffd096   Jamal Hadi Salim   net_sched: act_ip...
407
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
  }
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
409
  static void __exit ipt_cleanup_module(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
  {
ddf97ccdd   WANG Cong   net_sched: add ne...
411
412
  	tcf_unregister_action(&act_ipt_ops, &ipt_net_ops);
  	tcf_unregister_action(&act_xt_ops, &xt_net_ops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
414
415
416
  }
  
  module_init(ipt_init_module);
  module_exit(ipt_cleanup_module);