]>
Commit | Line | Data |
---|---|---|
c1d10adb PNA |
1 | /* Connection tracking via netlink socket. Allows for user space |
2 | * protocol helpers and general trouble making from userspace. | |
3 | * | |
4 | * (C) 2001 by Jay Schulist <jschlst@samba.org> | |
dc808fe2 | 5 | * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org> |
c1d10adb | 6 | * (C) 2003 by Patrick Mchardy <kaber@trash.net> |
0adf9d67 | 7 | * (C) 2005-2008 by Pablo Neira Ayuso <pablo@netfilter.org> |
c1d10adb | 8 | * |
601e68e1 | 9 | * Initial connection tracking via netlink development funded and |
c1d10adb PNA |
10 | * generally made possible by Network Robots, Inc. (www.networkrobots.com) |
11 | * | |
12 | * Further development of this code funded by Astaro AG (http://www.astaro.com) | |
13 | * | |
14 | * This software may be used and distributed according to the terms | |
15 | * of the GNU General Public License, incorporated herein by reference. | |
c1d10adb PNA |
16 | */ |
17 | ||
18 | #include <linux/init.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/kernel.h> | |
711bbdd6 | 21 | #include <linux/rculist.h> |
ea781f19 | 22 | #include <linux/rculist_nulls.h> |
c1d10adb PNA |
23 | #include <linux/types.h> |
24 | #include <linux/timer.h> | |
1cc63249 | 25 | #include <linux/security.h> |
c1d10adb PNA |
26 | #include <linux/skbuff.h> |
27 | #include <linux/errno.h> | |
28 | #include <linux/netlink.h> | |
29 | #include <linux/spinlock.h> | |
40a839fd | 30 | #include <linux/interrupt.h> |
5a0e3ad6 | 31 | #include <linux/slab.h> |
c1d10adb PNA |
32 | |
33 | #include <linux/netfilter.h> | |
dc5fc579 | 34 | #include <net/netlink.h> |
9592a5c0 | 35 | #include <net/sock.h> |
c1d10adb PNA |
36 | #include <net/netfilter/nf_conntrack.h> |
37 | #include <net/netfilter/nf_conntrack_core.h> | |
77ab9cff | 38 | #include <net/netfilter/nf_conntrack_expect.h> |
c1d10adb PNA |
39 | #include <net/netfilter/nf_conntrack_helper.h> |
40 | #include <net/netfilter/nf_conntrack_l3proto.h> | |
605dcad6 | 41 | #include <net/netfilter/nf_conntrack_l4proto.h> |
5b1158e9 | 42 | #include <net/netfilter/nf_conntrack_tuple.h> |
58401572 | 43 | #include <net/netfilter/nf_conntrack_acct.h> |
ef00f89f | 44 | #include <net/netfilter/nf_conntrack_zones.h> |
5b1158e9 JK |
45 | #ifdef CONFIG_NF_NAT_NEEDED |
46 | #include <net/netfilter/nf_nat_core.h> | |
47 | #include <net/netfilter/nf_nat_protocol.h> | |
48 | #endif | |
c1d10adb PNA |
49 | |
50 | #include <linux/netfilter/nfnetlink.h> | |
51 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
52 | ||
53 | MODULE_LICENSE("GPL"); | |
54 | ||
dc808fe2 | 55 | static char __initdata version[] = "0.93"; |
c1d10adb | 56 | |
c1d10adb | 57 | static inline int |
601e68e1 | 58 | ctnetlink_dump_tuples_proto(struct sk_buff *skb, |
1cde6436 | 59 | const struct nf_conntrack_tuple *tuple, |
605dcad6 | 60 | struct nf_conntrack_l4proto *l4proto) |
c1d10adb | 61 | { |
c1d10adb | 62 | int ret = 0; |
df6fb868 | 63 | struct nlattr *nest_parms; |
c1d10adb | 64 | |
df6fb868 PM |
65 | nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED); |
66 | if (!nest_parms) | |
67 | goto nla_put_failure; | |
77236b6e | 68 | NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum); |
c1d10adb | 69 | |
fdf70832 PM |
70 | if (likely(l4proto->tuple_to_nlattr)) |
71 | ret = l4proto->tuple_to_nlattr(skb, tuple); | |
601e68e1 | 72 | |
df6fb868 | 73 | nla_nest_end(skb, nest_parms); |
c1d10adb PNA |
74 | |
75 | return ret; | |
76 | ||
df6fb868 | 77 | nla_put_failure: |
c1d10adb PNA |
78 | return -1; |
79 | } | |
80 | ||
81 | static inline int | |
1cde6436 PNA |
82 | ctnetlink_dump_tuples_ip(struct sk_buff *skb, |
83 | const struct nf_conntrack_tuple *tuple, | |
84 | struct nf_conntrack_l3proto *l3proto) | |
c1d10adb | 85 | { |
c1d10adb | 86 | int ret = 0; |
df6fb868 PM |
87 | struct nlattr *nest_parms; |
88 | ||
89 | nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED); | |
90 | if (!nest_parms) | |
91 | goto nla_put_failure; | |
1cde6436 | 92 | |
fdf70832 PM |
93 | if (likely(l3proto->tuple_to_nlattr)) |
94 | ret = l3proto->tuple_to_nlattr(skb, tuple); | |
1cde6436 | 95 | |
df6fb868 | 96 | nla_nest_end(skb, nest_parms); |
c1d10adb | 97 | |
1cde6436 PNA |
98 | return ret; |
99 | ||
df6fb868 | 100 | nla_put_failure: |
1cde6436 PNA |
101 | return -1; |
102 | } | |
103 | ||
bb5cf80e | 104 | static int |
1cde6436 PNA |
105 | ctnetlink_dump_tuples(struct sk_buff *skb, |
106 | const struct nf_conntrack_tuple *tuple) | |
107 | { | |
108 | int ret; | |
109 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 110 | struct nf_conntrack_l4proto *l4proto; |
1cde6436 | 111 | |
528a3a6f | 112 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
1cde6436 | 113 | ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto); |
c1d10adb PNA |
114 | |
115 | if (unlikely(ret < 0)) | |
116 | return ret; | |
117 | ||
528a3a6f | 118 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); |
605dcad6 | 119 | ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto); |
c1d10adb PNA |
120 | |
121 | return ret; | |
c1d10adb PNA |
122 | } |
123 | ||
124 | static inline int | |
125 | ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct) | |
126 | { | |
77236b6e | 127 | NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status)); |
c1d10adb PNA |
128 | return 0; |
129 | ||
df6fb868 | 130 | nla_put_failure: |
c1d10adb PNA |
131 | return -1; |
132 | } | |
133 | ||
134 | static inline int | |
135 | ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct) | |
136 | { | |
77236b6e | 137 | long timeout = (ct->timeout.expires - jiffies) / HZ; |
c1d10adb | 138 | |
77236b6e | 139 | if (timeout < 0) |
c1d10adb | 140 | timeout = 0; |
601e68e1 | 141 | |
77236b6e | 142 | NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout)); |
c1d10adb PNA |
143 | return 0; |
144 | ||
df6fb868 | 145 | nla_put_failure: |
c1d10adb PNA |
146 | return -1; |
147 | } | |
148 | ||
149 | static inline int | |
440f0d58 | 150 | ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct) |
c1d10adb | 151 | { |
5e8fbe2a | 152 | struct nf_conntrack_l4proto *l4proto; |
df6fb868 | 153 | struct nlattr *nest_proto; |
c1d10adb PNA |
154 | int ret; |
155 | ||
528a3a6f PNA |
156 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
157 | if (!l4proto->to_nlattr) | |
c1d10adb | 158 | return 0; |
601e68e1 | 159 | |
df6fb868 PM |
160 | nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED); |
161 | if (!nest_proto) | |
162 | goto nla_put_failure; | |
c1d10adb | 163 | |
fdf70832 | 164 | ret = l4proto->to_nlattr(skb, nest_proto, ct); |
c1d10adb | 165 | |
df6fb868 | 166 | nla_nest_end(skb, nest_proto); |
c1d10adb PNA |
167 | |
168 | return ret; | |
169 | ||
df6fb868 | 170 | nla_put_failure: |
c1d10adb PNA |
171 | return -1; |
172 | } | |
173 | ||
174 | static inline int | |
175 | ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct) | |
176 | { | |
df6fb868 | 177 | struct nlattr *nest_helper; |
dc808fe2 | 178 | const struct nf_conn_help *help = nfct_help(ct); |
3c158f7f | 179 | struct nf_conntrack_helper *helper; |
c1d10adb | 180 | |
3c158f7f | 181 | if (!help) |
c1d10adb | 182 | return 0; |
601e68e1 | 183 | |
3c158f7f PM |
184 | helper = rcu_dereference(help->helper); |
185 | if (!helper) | |
186 | goto out; | |
187 | ||
df6fb868 PM |
188 | nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED); |
189 | if (!nest_helper) | |
190 | goto nla_put_failure; | |
77236b6e | 191 | NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name); |
c1d10adb | 192 | |
fdf70832 PM |
193 | if (helper->to_nlattr) |
194 | helper->to_nlattr(skb, ct); | |
c1d10adb | 195 | |
df6fb868 | 196 | nla_nest_end(skb, nest_helper); |
3c158f7f | 197 | out: |
c1d10adb PNA |
198 | return 0; |
199 | ||
df6fb868 | 200 | nla_put_failure: |
c1d10adb PNA |
201 | return -1; |
202 | } | |
203 | ||
bb5cf80e | 204 | static int |
c1d10adb PNA |
205 | ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct, |
206 | enum ip_conntrack_dir dir) | |
207 | { | |
208 | enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; | |
df6fb868 | 209 | struct nlattr *nest_count; |
58401572 KPO |
210 | const struct nf_conn_counter *acct; |
211 | ||
212 | acct = nf_conn_acct_find(ct); | |
213 | if (!acct) | |
214 | return 0; | |
c1d10adb | 215 | |
df6fb868 PM |
216 | nest_count = nla_nest_start(skb, type | NLA_F_NESTED); |
217 | if (!nest_count) | |
218 | goto nla_put_failure; | |
219 | ||
58401572 KPO |
220 | NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS, |
221 | cpu_to_be64(acct[dir].packets)); | |
222 | NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES, | |
223 | cpu_to_be64(acct[dir].bytes)); | |
c1d10adb | 224 | |
df6fb868 | 225 | nla_nest_end(skb, nest_count); |
c1d10adb PNA |
226 | |
227 | return 0; | |
228 | ||
df6fb868 | 229 | nla_put_failure: |
c1d10adb PNA |
230 | return -1; |
231 | } | |
c1d10adb PNA |
232 | |
233 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
234 | static inline int | |
235 | ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct) | |
236 | { | |
77236b6e | 237 | NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark)); |
c1d10adb PNA |
238 | return 0; |
239 | ||
df6fb868 | 240 | nla_put_failure: |
c1d10adb PNA |
241 | return -1; |
242 | } | |
243 | #else | |
244 | #define ctnetlink_dump_mark(a, b) (0) | |
245 | #endif | |
246 | ||
37fccd85 PNA |
247 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
248 | static inline int | |
1cc63249 | 249 | ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct) |
37fccd85 | 250 | { |
1cc63249 EP |
251 | struct nlattr *nest_secctx; |
252 | int len, ret; | |
253 | char *secctx; | |
254 | ||
255 | ret = security_secid_to_secctx(ct->secmark, &secctx, &len); | |
256 | if (ret) | |
cba85b53 | 257 | return 0; |
1cc63249 EP |
258 | |
259 | ret = -1; | |
260 | nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED); | |
261 | if (!nest_secctx) | |
262 | goto nla_put_failure; | |
37fccd85 | 263 | |
1cc63249 EP |
264 | NLA_PUT_STRING(skb, CTA_SECCTX_NAME, secctx); |
265 | nla_nest_end(skb, nest_secctx); | |
266 | ||
267 | ret = 0; | |
37fccd85 | 268 | nla_put_failure: |
1cc63249 EP |
269 | security_release_secctx(secctx, len); |
270 | return ret; | |
37fccd85 PNA |
271 | } |
272 | #else | |
1cc63249 | 273 | #define ctnetlink_dump_secctx(a, b) (0) |
37fccd85 PNA |
274 | #endif |
275 | ||
0f417ce9 PNA |
276 | #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple) |
277 | ||
278 | static inline int | |
279 | ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct) | |
280 | { | |
281 | struct nlattr *nest_parms; | |
282 | ||
283 | if (!(ct->status & IPS_EXPECTED)) | |
284 | return 0; | |
285 | ||
286 | nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED); | |
287 | if (!nest_parms) | |
288 | goto nla_put_failure; | |
289 | if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0) | |
290 | goto nla_put_failure; | |
291 | nla_nest_end(skb, nest_parms); | |
292 | ||
293 | return 0; | |
294 | ||
295 | nla_put_failure: | |
296 | return -1; | |
297 | } | |
298 | ||
13eae15a | 299 | #ifdef CONFIG_NF_NAT_NEEDED |
bb5cf80e | 300 | static int |
13eae15a PNA |
301 | dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type) |
302 | { | |
13eae15a PNA |
303 | struct nlattr *nest_parms; |
304 | ||
305 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); | |
306 | if (!nest_parms) | |
307 | goto nla_put_failure; | |
308 | ||
77236b6e PM |
309 | NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS, |
310 | htonl(natseq->correction_pos)); | |
311 | NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE, | |
312 | htonl(natseq->offset_before)); | |
313 | NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER, | |
314 | htonl(natseq->offset_after)); | |
13eae15a PNA |
315 | |
316 | nla_nest_end(skb, nest_parms); | |
317 | ||
318 | return 0; | |
319 | ||
320 | nla_put_failure: | |
321 | return -1; | |
322 | } | |
323 | ||
324 | static inline int | |
325 | ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct) | |
326 | { | |
327 | struct nf_nat_seq *natseq; | |
328 | struct nf_conn_nat *nat = nfct_nat(ct); | |
329 | ||
330 | if (!(ct->status & IPS_SEQ_ADJUST) || !nat) | |
331 | return 0; | |
332 | ||
333 | natseq = &nat->seq[IP_CT_DIR_ORIGINAL]; | |
334 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1) | |
335 | return -1; | |
336 | ||
337 | natseq = &nat->seq[IP_CT_DIR_REPLY]; | |
338 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1) | |
339 | return -1; | |
340 | ||
341 | return 0; | |
342 | } | |
343 | #else | |
344 | #define ctnetlink_dump_nat_seq_adj(a, b) (0) | |
345 | #endif | |
346 | ||
c1d10adb PNA |
347 | static inline int |
348 | ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) | |
349 | { | |
77236b6e | 350 | NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct)); |
c1d10adb PNA |
351 | return 0; |
352 | ||
df6fb868 | 353 | nla_put_failure: |
c1d10adb PNA |
354 | return -1; |
355 | } | |
356 | ||
357 | static inline int | |
358 | ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct) | |
359 | { | |
77236b6e | 360 | NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))); |
c1d10adb PNA |
361 | return 0; |
362 | ||
df6fb868 | 363 | nla_put_failure: |
c1d10adb PNA |
364 | return -1; |
365 | } | |
366 | ||
c1d10adb PNA |
367 | static int |
368 | ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, | |
440f0d58 | 369 | int event, struct nf_conn *ct) |
c1d10adb PNA |
370 | { |
371 | struct nlmsghdr *nlh; | |
372 | struct nfgenmsg *nfmsg; | |
df6fb868 | 373 | struct nlattr *nest_parms; |
96bcf938 | 374 | unsigned int flags = pid ? NLM_F_MULTI : 0; |
c1d10adb PNA |
375 | |
376 | event |= NFNL_SUBSYS_CTNETLINK << 8; | |
96bcf938 PNA |
377 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags); |
378 | if (nlh == NULL) | |
379 | goto nlmsg_failure; | |
c1d10adb | 380 | |
96bcf938 | 381 | nfmsg = nlmsg_data(nlh); |
5e8fbe2a | 382 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
383 | nfmsg->version = NFNETLINK_V0; |
384 | nfmsg->res_id = 0; | |
385 | ||
df6fb868 PM |
386 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
387 | if (!nest_parms) | |
388 | goto nla_put_failure; | |
f2f3e38c | 389 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
390 | goto nla_put_failure; |
391 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 392 | |
df6fb868 PM |
393 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
394 | if (!nest_parms) | |
395 | goto nla_put_failure; | |
f2f3e38c | 396 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
397 | goto nla_put_failure; |
398 | nla_nest_end(skb, nest_parms); | |
c1d10adb | 399 | |
ef00f89f PM |
400 | if (nf_ct_zone(ct)) |
401 | NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct))); | |
402 | ||
c1d10adb PNA |
403 | if (ctnetlink_dump_status(skb, ct) < 0 || |
404 | ctnetlink_dump_timeout(skb, ct) < 0 || | |
405 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || | |
406 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 || | |
407 | ctnetlink_dump_protoinfo(skb, ct) < 0 || | |
408 | ctnetlink_dump_helpinfo(skb, ct) < 0 || | |
409 | ctnetlink_dump_mark(skb, ct) < 0 || | |
1cc63249 | 410 | ctnetlink_dump_secctx(skb, ct) < 0 || |
c1d10adb | 411 | ctnetlink_dump_id(skb, ct) < 0 || |
13eae15a | 412 | ctnetlink_dump_use(skb, ct) < 0 || |
0f417ce9 | 413 | ctnetlink_dump_master(skb, ct) < 0 || |
13eae15a | 414 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
df6fb868 | 415 | goto nla_put_failure; |
c1d10adb | 416 | |
96bcf938 | 417 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
418 | return skb->len; |
419 | ||
420 | nlmsg_failure: | |
df6fb868 | 421 | nla_put_failure: |
96bcf938 | 422 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
423 | return -1; |
424 | } | |
425 | ||
426 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
03b64f51 PNA |
427 | static inline size_t |
428 | ctnetlink_proto_size(const struct nf_conn *ct) | |
2732c4e4 HE |
429 | { |
430 | struct nf_conntrack_l3proto *l3proto; | |
431 | struct nf_conntrack_l4proto *l4proto; | |
03b64f51 PNA |
432 | size_t len = 0; |
433 | ||
434 | rcu_read_lock(); | |
435 | l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct)); | |
436 | len += l3proto->nla_size; | |
437 | ||
438 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
439 | len += l4proto->nla_size; | |
440 | rcu_read_unlock(); | |
441 | ||
442 | return len; | |
443 | } | |
444 | ||
d26e6a02 JP |
445 | static inline size_t |
446 | ctnetlink_counters_size(const struct nf_conn *ct) | |
447 | { | |
448 | if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT)) | |
449 | return 0; | |
450 | return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */ | |
451 | + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */ | |
452 | + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */ | |
453 | ; | |
454 | } | |
455 | ||
cba85b53 PNA |
456 | static inline int |
457 | ctnetlink_secctx_size(const struct nf_conn *ct) | |
1cc63249 | 458 | { |
cba85b53 PNA |
459 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
460 | int len, ret; | |
1cc63249 | 461 | |
cba85b53 PNA |
462 | ret = security_secid_to_secctx(ct->secmark, NULL, &len); |
463 | if (ret) | |
464 | return 0; | |
1cc63249 | 465 | |
cba85b53 PNA |
466 | return nla_total_size(0) /* CTA_SECCTX */ |
467 | + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */ | |
468 | #else | |
469 | return 0; | |
1cc63249 | 470 | #endif |
cba85b53 | 471 | } |
1cc63249 | 472 | |
03b64f51 PNA |
473 | static inline size_t |
474 | ctnetlink_nlmsg_size(const struct nf_conn *ct) | |
475 | { | |
476 | return NLMSG_ALIGN(sizeof(struct nfgenmsg)) | |
477 | + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ | |
478 | + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ | |
479 | + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ | |
480 | + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ | |
481 | + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ | |
482 | + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ | |
d26e6a02 | 483 | + ctnetlink_counters_size(ct) |
03b64f51 PNA |
484 | + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ |
485 | + nla_total_size(0) /* CTA_PROTOINFO */ | |
486 | + nla_total_size(0) /* CTA_HELP */ | |
487 | + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ | |
cba85b53 | 488 | + ctnetlink_secctx_size(ct) |
d271e8bd | 489 | #ifdef CONFIG_NF_NAT_NEEDED |
03b64f51 PNA |
490 | + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ |
491 | + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ | |
d271e8bd HE |
492 | #endif |
493 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
03b64f51 | 494 | + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ |
d271e8bd | 495 | #endif |
03b64f51 PNA |
496 | + ctnetlink_proto_size(ct) |
497 | ; | |
2732c4e4 HE |
498 | } |
499 | ||
e34d5c1a PNA |
500 | static int |
501 | ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item) | |
c1d10adb | 502 | { |
9592a5c0 | 503 | struct net *net; |
c1d10adb PNA |
504 | struct nlmsghdr *nlh; |
505 | struct nfgenmsg *nfmsg; | |
df6fb868 | 506 | struct nlattr *nest_parms; |
19abb7b0 | 507 | struct nf_conn *ct = item->ct; |
c1d10adb PNA |
508 | struct sk_buff *skb; |
509 | unsigned int type; | |
c1d10adb | 510 | unsigned int flags = 0, group; |
dd7669a9 | 511 | int err; |
c1d10adb PNA |
512 | |
513 | /* ignore our fake conntrack entry */ | |
5bfddbd4 | 514 | if (nf_ct_is_untracked(ct)) |
e34d5c1a | 515 | return 0; |
c1d10adb | 516 | |
a0891aa6 | 517 | if (events & (1 << IPCT_DESTROY)) { |
c1d10adb PNA |
518 | type = IPCTNL_MSG_CT_DELETE; |
519 | group = NFNLGRP_CONNTRACK_DESTROY; | |
a0891aa6 | 520 | } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) { |
c1d10adb PNA |
521 | type = IPCTNL_MSG_CT_NEW; |
522 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
c1d10adb | 523 | group = NFNLGRP_CONNTRACK_NEW; |
17e6e4ea | 524 | } else if (events) { |
c1d10adb PNA |
525 | type = IPCTNL_MSG_CT_NEW; |
526 | group = NFNLGRP_CONNTRACK_UPDATE; | |
527 | } else | |
e34d5c1a | 528 | return 0; |
a2427692 | 529 | |
9592a5c0 AD |
530 | net = nf_ct_net(ct); |
531 | if (!item->report && !nfnetlink_has_listeners(net, group)) | |
e34d5c1a | 532 | return 0; |
a2427692 | 533 | |
03b64f51 PNA |
534 | skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC); |
535 | if (skb == NULL) | |
150ace0d | 536 | goto errout; |
c1d10adb | 537 | |
c1d10adb | 538 | type |= NFNL_SUBSYS_CTNETLINK << 8; |
96bcf938 PNA |
539 | nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags); |
540 | if (nlh == NULL) | |
541 | goto nlmsg_failure; | |
c1d10adb | 542 | |
96bcf938 | 543 | nfmsg = nlmsg_data(nlh); |
5e8fbe2a | 544 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
545 | nfmsg->version = NFNETLINK_V0; |
546 | nfmsg->res_id = 0; | |
547 | ||
528a3a6f | 548 | rcu_read_lock(); |
df6fb868 PM |
549 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
550 | if (!nest_parms) | |
551 | goto nla_put_failure; | |
f2f3e38c | 552 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
553 | goto nla_put_failure; |
554 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 555 | |
df6fb868 PM |
556 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
557 | if (!nest_parms) | |
558 | goto nla_put_failure; | |
f2f3e38c | 559 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
560 | goto nla_put_failure; |
561 | nla_nest_end(skb, nest_parms); | |
c1d10adb | 562 | |
ef00f89f PM |
563 | if (nf_ct_zone(ct)) |
564 | NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct))); | |
565 | ||
1eedf699 EL |
566 | if (ctnetlink_dump_id(skb, ct) < 0) |
567 | goto nla_put_failure; | |
568 | ||
e57dce60 FH |
569 | if (ctnetlink_dump_status(skb, ct) < 0) |
570 | goto nla_put_failure; | |
571 | ||
a0891aa6 | 572 | if (events & (1 << IPCT_DESTROY)) { |
7b621c1e PNA |
573 | if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || |
574 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0) | |
df6fb868 | 575 | goto nla_put_failure; |
7b621c1e | 576 | } else { |
7b621c1e | 577 | if (ctnetlink_dump_timeout(skb, ct) < 0) |
df6fb868 | 578 | goto nla_put_failure; |
7b621c1e | 579 | |
a0891aa6 | 580 | if (events & (1 << IPCT_PROTOINFO) |
7b621c1e | 581 | && ctnetlink_dump_protoinfo(skb, ct) < 0) |
df6fb868 | 582 | goto nla_put_failure; |
7b621c1e | 583 | |
a0891aa6 | 584 | if ((events & (1 << IPCT_HELPER) || nfct_help(ct)) |
7b621c1e | 585 | && ctnetlink_dump_helpinfo(skb, ct) < 0) |
df6fb868 | 586 | goto nla_put_failure; |
7b621c1e | 587 | |
ff660c80 | 588 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
a0891aa6 | 589 | if ((events & (1 << IPCT_SECMARK) || ct->secmark) |
1cc63249 | 590 | && ctnetlink_dump_secctx(skb, ct) < 0) |
37fccd85 | 591 | goto nla_put_failure; |
ff660c80 | 592 | #endif |
7b621c1e | 593 | |
a0891aa6 | 594 | if (events & (1 << IPCT_RELATED) && |
0f417ce9 PNA |
595 | ctnetlink_dump_master(skb, ct) < 0) |
596 | goto nla_put_failure; | |
597 | ||
a0891aa6 | 598 | if (events & (1 << IPCT_NATSEQADJ) && |
13eae15a PNA |
599 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
600 | goto nla_put_failure; | |
7b621c1e | 601 | } |
b9a37e0c | 602 | |
a83099a6 | 603 | #ifdef CONFIG_NF_CONNTRACK_MARK |
a0891aa6 | 604 | if ((events & (1 << IPCT_MARK) || ct->mark) |
a83099a6 EL |
605 | && ctnetlink_dump_mark(skb, ct) < 0) |
606 | goto nla_put_failure; | |
607 | #endif | |
528a3a6f | 608 | rcu_read_unlock(); |
a83099a6 | 609 | |
96bcf938 | 610 | nlmsg_end(skb, nlh); |
9592a5c0 | 611 | err = nfnetlink_send(skb, net, item->pid, group, item->report, |
cd8c20b6 | 612 | GFP_ATOMIC); |
dd7669a9 PNA |
613 | if (err == -ENOBUFS || err == -EAGAIN) |
614 | return -ENOBUFS; | |
615 | ||
e34d5c1a | 616 | return 0; |
c1d10adb | 617 | |
df6fb868 | 618 | nla_put_failure: |
528a3a6f | 619 | rcu_read_unlock(); |
96bcf938 | 620 | nlmsg_cancel(skb, nlh); |
528a3a6f | 621 | nlmsg_failure: |
c1d10adb | 622 | kfree_skb(skb); |
150ace0d | 623 | errout: |
37b7ef72 PNA |
624 | if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0) |
625 | return -ENOBUFS; | |
626 | ||
e34d5c1a | 627 | return 0; |
c1d10adb PNA |
628 | } |
629 | #endif /* CONFIG_NF_CONNTRACK_EVENTS */ | |
630 | ||
631 | static int ctnetlink_done(struct netlink_callback *cb) | |
632 | { | |
89f2e218 PM |
633 | if (cb->args[1]) |
634 | nf_ct_put((struct nf_conn *)cb->args[1]); | |
c1d10adb PNA |
635 | return 0; |
636 | } | |
637 | ||
638 | static int | |
639 | ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
640 | { | |
9592a5c0 | 641 | struct net *net = sock_net(skb->sk); |
89f2e218 | 642 | struct nf_conn *ct, *last; |
c1d10adb | 643 | struct nf_conntrack_tuple_hash *h; |
ea781f19 | 644 | struct hlist_nulls_node *n; |
96bcf938 | 645 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
87711cb8 | 646 | u_int8_t l3proto = nfmsg->nfgen_family; |
c1d10adb | 647 | |
76507f69 | 648 | rcu_read_lock(); |
d205dc40 | 649 | last = (struct nf_conn *)cb->args[1]; |
9ab99d5a | 650 | for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) { |
89f2e218 | 651 | restart: |
9592a5c0 | 652 | hlist_nulls_for_each_entry_rcu(h, n, &net->ct.hash[cb->args[0]], |
ea781f19 | 653 | hnnode) { |
5b1158e9 | 654 | if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
c1d10adb PNA |
655 | continue; |
656 | ct = nf_ct_tuplehash_to_ctrack(h); | |
ea781f19 ED |
657 | if (!atomic_inc_not_zero(&ct->ct_general.use)) |
658 | continue; | |
87711cb8 PNA |
659 | /* Dump entries of a given L3 protocol number. |
660 | * If it is not specified, ie. l3proto == 0, | |
661 | * then dump everything. */ | |
5e8fbe2a | 662 | if (l3proto && nf_ct_l3num(ct) != l3proto) |
ea781f19 | 663 | goto releasect; |
d205dc40 PM |
664 | if (cb->args[1]) { |
665 | if (ct != last) | |
ea781f19 | 666 | goto releasect; |
d205dc40 | 667 | cb->args[1] = 0; |
89f2e218 | 668 | } |
c1d10adb | 669 | if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, |
601e68e1 | 670 | cb->nlh->nlmsg_seq, |
8b0a231d | 671 | IPCTNL_MSG_CT_NEW, ct) < 0) { |
89f2e218 | 672 | cb->args[1] = (unsigned long)ct; |
c1d10adb | 673 | goto out; |
89f2e218 | 674 | } |
58401572 | 675 | |
01f34848 | 676 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == |
58401572 KPO |
677 | IPCTNL_MSG_CT_GET_CTRZERO) { |
678 | struct nf_conn_counter *acct; | |
679 | ||
680 | acct = nf_conn_acct_find(ct); | |
681 | if (acct) | |
682 | memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX])); | |
683 | } | |
ea781f19 ED |
684 | releasect: |
685 | nf_ct_put(ct); | |
89f2e218 | 686 | } |
d205dc40 | 687 | if (cb->args[1]) { |
89f2e218 PM |
688 | cb->args[1] = 0; |
689 | goto restart; | |
c1d10adb PNA |
690 | } |
691 | } | |
89f2e218 | 692 | out: |
76507f69 | 693 | rcu_read_unlock(); |
d205dc40 PM |
694 | if (last) |
695 | nf_ct_put(last); | |
c1d10adb | 696 | |
c1d10adb PNA |
697 | return skb->len; |
698 | } | |
699 | ||
c1d10adb | 700 | static inline int |
df6fb868 | 701 | ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple) |
c1d10adb | 702 | { |
df6fb868 | 703 | struct nlattr *tb[CTA_IP_MAX+1]; |
c1d10adb PNA |
704 | struct nf_conntrack_l3proto *l3proto; |
705 | int ret = 0; | |
706 | ||
df6fb868 | 707 | nla_parse_nested(tb, CTA_IP_MAX, attr, NULL); |
c1d10adb | 708 | |
cd91566e FW |
709 | rcu_read_lock(); |
710 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); | |
c1d10adb | 711 | |
f73e924c PM |
712 | if (likely(l3proto->nlattr_to_tuple)) { |
713 | ret = nla_validate_nested(attr, CTA_IP_MAX, | |
714 | l3proto->nla_policy); | |
715 | if (ret == 0) | |
716 | ret = l3proto->nlattr_to_tuple(tb, tuple); | |
717 | } | |
c1d10adb | 718 | |
cd91566e | 719 | rcu_read_unlock(); |
c1d10adb | 720 | |
c1d10adb PNA |
721 | return ret; |
722 | } | |
723 | ||
f73e924c PM |
724 | static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = { |
725 | [CTA_PROTO_NUM] = { .type = NLA_U8 }, | |
c1d10adb PNA |
726 | }; |
727 | ||
728 | static inline int | |
df6fb868 | 729 | ctnetlink_parse_tuple_proto(struct nlattr *attr, |
c1d10adb PNA |
730 | struct nf_conntrack_tuple *tuple) |
731 | { | |
df6fb868 | 732 | struct nlattr *tb[CTA_PROTO_MAX+1]; |
605dcad6 | 733 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
734 | int ret = 0; |
735 | ||
f73e924c PM |
736 | ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy); |
737 | if (ret < 0) | |
738 | return ret; | |
c1d10adb | 739 | |
df6fb868 | 740 | if (!tb[CTA_PROTO_NUM]) |
c1d10adb | 741 | return -EINVAL; |
77236b6e | 742 | tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]); |
c1d10adb | 743 | |
cd91566e FW |
744 | rcu_read_lock(); |
745 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); | |
c1d10adb | 746 | |
f73e924c PM |
747 | if (likely(l4proto->nlattr_to_tuple)) { |
748 | ret = nla_validate_nested(attr, CTA_PROTO_MAX, | |
749 | l4proto->nla_policy); | |
750 | if (ret == 0) | |
751 | ret = l4proto->nlattr_to_tuple(tb, tuple); | |
752 | } | |
c1d10adb | 753 | |
cd91566e | 754 | rcu_read_unlock(); |
601e68e1 | 755 | |
c1d10adb PNA |
756 | return ret; |
757 | } | |
758 | ||
d0b0268f PM |
759 | static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = { |
760 | [CTA_TUPLE_IP] = { .type = NLA_NESTED }, | |
761 | [CTA_TUPLE_PROTO] = { .type = NLA_NESTED }, | |
762 | }; | |
763 | ||
bb5cf80e | 764 | static int |
39938324 PM |
765 | ctnetlink_parse_tuple(const struct nlattr * const cda[], |
766 | struct nf_conntrack_tuple *tuple, | |
c1d10adb PNA |
767 | enum ctattr_tuple type, u_int8_t l3num) |
768 | { | |
df6fb868 | 769 | struct nlattr *tb[CTA_TUPLE_MAX+1]; |
c1d10adb PNA |
770 | int err; |
771 | ||
c1d10adb PNA |
772 | memset(tuple, 0, sizeof(*tuple)); |
773 | ||
d0b0268f | 774 | nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy); |
c1d10adb | 775 | |
df6fb868 | 776 | if (!tb[CTA_TUPLE_IP]) |
c1d10adb PNA |
777 | return -EINVAL; |
778 | ||
779 | tuple->src.l3num = l3num; | |
780 | ||
df6fb868 | 781 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple); |
c1d10adb PNA |
782 | if (err < 0) |
783 | return err; | |
784 | ||
df6fb868 | 785 | if (!tb[CTA_TUPLE_PROTO]) |
c1d10adb PNA |
786 | return -EINVAL; |
787 | ||
df6fb868 | 788 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple); |
c1d10adb PNA |
789 | if (err < 0) |
790 | return err; | |
791 | ||
792 | /* orig and expect tuples get DIR_ORIGINAL */ | |
793 | if (type == CTA_TUPLE_REPLY) | |
794 | tuple->dst.dir = IP_CT_DIR_REPLY; | |
795 | else | |
796 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
797 | ||
c1d10adb PNA |
798 | return 0; |
799 | } | |
800 | ||
ef00f89f PM |
801 | static int |
802 | ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone) | |
803 | { | |
804 | if (attr) | |
805 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
806 | *zone = ntohs(nla_get_be16(attr)); | |
807 | #else | |
808 | return -EOPNOTSUPP; | |
809 | #endif | |
810 | else | |
811 | *zone = 0; | |
812 | ||
813 | return 0; | |
814 | } | |
815 | ||
d0b0268f PM |
816 | static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = { |
817 | [CTA_HELP_NAME] = { .type = NLA_NUL_STRING }, | |
818 | }; | |
819 | ||
c1d10adb | 820 | static inline int |
39938324 | 821 | ctnetlink_parse_help(const struct nlattr *attr, char **helper_name) |
c1d10adb | 822 | { |
df6fb868 | 823 | struct nlattr *tb[CTA_HELP_MAX+1]; |
c1d10adb | 824 | |
d0b0268f | 825 | nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy); |
c1d10adb | 826 | |
df6fb868 | 827 | if (!tb[CTA_HELP_NAME]) |
c1d10adb PNA |
828 | return -EINVAL; |
829 | ||
df6fb868 | 830 | *helper_name = nla_data(tb[CTA_HELP_NAME]); |
c1d10adb PNA |
831 | |
832 | return 0; | |
833 | } | |
834 | ||
f73e924c | 835 | static const struct nla_policy ct_nla_policy[CTA_MAX+1] = { |
d0b0268f PM |
836 | [CTA_TUPLE_ORIG] = { .type = NLA_NESTED }, |
837 | [CTA_TUPLE_REPLY] = { .type = NLA_NESTED }, | |
f73e924c | 838 | [CTA_STATUS] = { .type = NLA_U32 }, |
d0b0268f PM |
839 | [CTA_PROTOINFO] = { .type = NLA_NESTED }, |
840 | [CTA_HELP] = { .type = NLA_NESTED }, | |
841 | [CTA_NAT_SRC] = { .type = NLA_NESTED }, | |
f73e924c PM |
842 | [CTA_TIMEOUT] = { .type = NLA_U32 }, |
843 | [CTA_MARK] = { .type = NLA_U32 }, | |
f73e924c | 844 | [CTA_ID] = { .type = NLA_U32 }, |
d0b0268f PM |
845 | [CTA_NAT_DST] = { .type = NLA_NESTED }, |
846 | [CTA_TUPLE_MASTER] = { .type = NLA_NESTED }, | |
ef00f89f | 847 | [CTA_ZONE] = { .type = NLA_U16 }, |
c1d10adb PNA |
848 | }; |
849 | ||
850 | static int | |
601e68e1 | 851 | ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
852 | const struct nlmsghdr *nlh, |
853 | const struct nlattr * const cda[]) | |
c1d10adb | 854 | { |
9592a5c0 | 855 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
856 | struct nf_conntrack_tuple_hash *h; |
857 | struct nf_conntrack_tuple tuple; | |
858 | struct nf_conn *ct; | |
96bcf938 | 859 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 860 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
861 | u16 zone; |
862 | int err; | |
863 | ||
864 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); | |
865 | if (err < 0) | |
866 | return err; | |
c1d10adb | 867 | |
df6fb868 | 868 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 869 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 870 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
871 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
872 | else { | |
873 | /* Flush the whole table */ | |
9592a5c0 | 874 | nf_conntrack_flush_report(net, |
274d383b PNA |
875 | NETLINK_CB(skb).pid, |
876 | nlmsg_report(nlh)); | |
c1d10adb PNA |
877 | return 0; |
878 | } | |
879 | ||
880 | if (err < 0) | |
881 | return err; | |
882 | ||
ef00f89f | 883 | h = nf_conntrack_find_get(net, zone, &tuple); |
9ea8cfd6 | 884 | if (!h) |
c1d10adb | 885 | return -ENOENT; |
c1d10adb PNA |
886 | |
887 | ct = nf_ct_tuplehash_to_ctrack(h); | |
601e68e1 | 888 | |
df6fb868 | 889 | if (cda[CTA_ID]) { |
77236b6e | 890 | u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID])); |
7f85f914 | 891 | if (id != (u32)(unsigned long)ct) { |
c1d10adb PNA |
892 | nf_ct_put(ct); |
893 | return -ENOENT; | |
894 | } | |
601e68e1 | 895 | } |
c1d10adb | 896 | |
dd7669a9 PNA |
897 | if (nf_conntrack_event_report(IPCT_DESTROY, ct, |
898 | NETLINK_CB(skb).pid, | |
899 | nlmsg_report(nlh)) < 0) { | |
900 | nf_ct_delete_from_lists(ct); | |
901 | /* we failed to report the event, try later */ | |
902 | nf_ct_insert_dying_list(ct); | |
903 | nf_ct_put(ct); | |
904 | return 0; | |
905 | } | |
19abb7b0 PNA |
906 | |
907 | /* death_by_timeout would report the event again */ | |
908 | set_bit(IPS_DYING_BIT, &ct->status); | |
909 | ||
51091764 | 910 | nf_ct_kill(ct); |
c1d10adb | 911 | nf_ct_put(ct); |
c1d10adb PNA |
912 | |
913 | return 0; | |
914 | } | |
915 | ||
916 | static int | |
601e68e1 | 917 | ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
918 | const struct nlmsghdr *nlh, |
919 | const struct nlattr * const cda[]) | |
c1d10adb | 920 | { |
9592a5c0 | 921 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
922 | struct nf_conntrack_tuple_hash *h; |
923 | struct nf_conntrack_tuple tuple; | |
924 | struct nf_conn *ct; | |
925 | struct sk_buff *skb2 = NULL; | |
96bcf938 | 926 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 927 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
928 | u16 zone; |
929 | int err; | |
c1d10adb | 930 | |
58401572 | 931 | if (nlh->nlmsg_flags & NLM_F_DUMP) |
c702e804 TG |
932 | return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table, |
933 | ctnetlink_done); | |
c1d10adb | 934 | |
ef00f89f PM |
935 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); |
936 | if (err < 0) | |
937 | return err; | |
938 | ||
df6fb868 | 939 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 940 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 941 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
942 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
943 | else | |
944 | return -EINVAL; | |
945 | ||
946 | if (err < 0) | |
947 | return err; | |
948 | ||
ef00f89f | 949 | h = nf_conntrack_find_get(net, zone, &tuple); |
9ea8cfd6 | 950 | if (!h) |
c1d10adb | 951 | return -ENOENT; |
9ea8cfd6 | 952 | |
c1d10adb PNA |
953 | ct = nf_ct_tuplehash_to_ctrack(h); |
954 | ||
955 | err = -ENOMEM; | |
96bcf938 PNA |
956 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
957 | if (skb2 == NULL) { | |
c1d10adb PNA |
958 | nf_ct_put(ct); |
959 | return -ENOMEM; | |
960 | } | |
c1d10adb | 961 | |
528a3a6f | 962 | rcu_read_lock(); |
601e68e1 | 963 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, |
8b0a231d | 964 | IPCTNL_MSG_CT_NEW, ct); |
528a3a6f | 965 | rcu_read_unlock(); |
c1d10adb PNA |
966 | nf_ct_put(ct); |
967 | if (err <= 0) | |
968 | goto free; | |
969 | ||
970 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
971 | if (err < 0) | |
972 | goto out; | |
973 | ||
c1d10adb PNA |
974 | return 0; |
975 | ||
976 | free: | |
977 | kfree_skb(skb2); | |
978 | out: | |
979 | return err; | |
980 | } | |
981 | ||
67671841 | 982 | #ifdef CONFIG_NF_NAT_NEEDED |
e6a7d3c0 PNA |
983 | static int |
984 | ctnetlink_parse_nat_setup(struct nf_conn *ct, | |
985 | enum nf_nat_manip_type manip, | |
39938324 | 986 | const struct nlattr *attr) |
e6a7d3c0 PNA |
987 | { |
988 | typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup; | |
989 | ||
990 | parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook); | |
991 | if (!parse_nat_setup) { | |
95a5afca | 992 | #ifdef CONFIG_MODULES |
e6a7d3c0 | 993 | rcu_read_unlock(); |
748085fc | 994 | spin_unlock_bh(&nf_conntrack_lock); |
e6a7d3c0 PNA |
995 | nfnl_unlock(); |
996 | if (request_module("nf-nat-ipv4") < 0) { | |
997 | nfnl_lock(); | |
748085fc | 998 | spin_lock_bh(&nf_conntrack_lock); |
e6a7d3c0 PNA |
999 | rcu_read_lock(); |
1000 | return -EOPNOTSUPP; | |
1001 | } | |
1002 | nfnl_lock(); | |
748085fc | 1003 | spin_lock_bh(&nf_conntrack_lock); |
e6a7d3c0 PNA |
1004 | rcu_read_lock(); |
1005 | if (nfnetlink_parse_nat_setup_hook) | |
1006 | return -EAGAIN; | |
1007 | #endif | |
1008 | return -EOPNOTSUPP; | |
1009 | } | |
1010 | ||
1011 | return parse_nat_setup(ct, manip, attr); | |
1012 | } | |
67671841 | 1013 | #endif |
e6a7d3c0 | 1014 | |
bb5cf80e | 1015 | static int |
39938324 | 1016 | ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
1017 | { |
1018 | unsigned long d; | |
77236b6e | 1019 | unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS])); |
c1d10adb PNA |
1020 | d = ct->status ^ status; |
1021 | ||
1022 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) | |
1023 | /* unchangeable */ | |
0adf9d67 | 1024 | return -EBUSY; |
601e68e1 | 1025 | |
c1d10adb PNA |
1026 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) |
1027 | /* SEEN_REPLY bit can only be set */ | |
0adf9d67 | 1028 | return -EBUSY; |
601e68e1 | 1029 | |
c1d10adb PNA |
1030 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) |
1031 | /* ASSURED bit can only be set */ | |
0adf9d67 | 1032 | return -EBUSY; |
c1d10adb | 1033 | |
c1d10adb PNA |
1034 | /* Be careful here, modifying NAT bits can screw up things, |
1035 | * so don't let users modify them directly if they don't pass | |
5b1158e9 | 1036 | * nf_nat_range. */ |
c1d10adb PNA |
1037 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); |
1038 | return 0; | |
1039 | } | |
1040 | ||
e6a7d3c0 | 1041 | static int |
39938324 | 1042 | ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[]) |
e6a7d3c0 PNA |
1043 | { |
1044 | #ifdef CONFIG_NF_NAT_NEEDED | |
1045 | int ret; | |
1046 | ||
1047 | if (cda[CTA_NAT_DST]) { | |
1048 | ret = ctnetlink_parse_nat_setup(ct, | |
1049 | IP_NAT_MANIP_DST, | |
1050 | cda[CTA_NAT_DST]); | |
1051 | if (ret < 0) | |
1052 | return ret; | |
1053 | } | |
1054 | if (cda[CTA_NAT_SRC]) { | |
1055 | ret = ctnetlink_parse_nat_setup(ct, | |
1056 | IP_NAT_MANIP_SRC, | |
1057 | cda[CTA_NAT_SRC]); | |
1058 | if (ret < 0) | |
1059 | return ret; | |
1060 | } | |
1061 | return 0; | |
1062 | #else | |
1063 | return -EOPNOTSUPP; | |
1064 | #endif | |
1065 | } | |
c1d10adb PNA |
1066 | |
1067 | static inline int | |
39938324 | 1068 | ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
1069 | { |
1070 | struct nf_conntrack_helper *helper; | |
dc808fe2 | 1071 | struct nf_conn_help *help = nfct_help(ct); |
29fe1b48 | 1072 | char *helpname = NULL; |
c1d10adb PNA |
1073 | int err; |
1074 | ||
c1d10adb PNA |
1075 | /* don't change helper of sibling connections */ |
1076 | if (ct->master) | |
0adf9d67 | 1077 | return -EBUSY; |
c1d10adb | 1078 | |
df6fb868 | 1079 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); |
c1d10adb PNA |
1080 | if (err < 0) |
1081 | return err; | |
1082 | ||
df293bbb YK |
1083 | if (!strcmp(helpname, "")) { |
1084 | if (help && help->helper) { | |
c1d10adb PNA |
1085 | /* we had a helper before ... */ |
1086 | nf_ct_remove_expectations(ct); | |
3c158f7f | 1087 | rcu_assign_pointer(help->helper, NULL); |
c1d10adb | 1088 | } |
df293bbb YK |
1089 | |
1090 | return 0; | |
c1d10adb | 1091 | } |
601e68e1 | 1092 | |
794e6871 PM |
1093 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1094 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1095 | if (helper == NULL) { |
1096 | #ifdef CONFIG_MODULES | |
1097 | spin_unlock_bh(&nf_conntrack_lock); | |
1098 | ||
1099 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1100 | spin_lock_bh(&nf_conntrack_lock); | |
1101 | return -EOPNOTSUPP; | |
1102 | } | |
1103 | ||
1104 | spin_lock_bh(&nf_conntrack_lock); | |
794e6871 PM |
1105 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1106 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1107 | if (helper) |
1108 | return -EAGAIN; | |
1109 | #endif | |
0adf9d67 | 1110 | return -EOPNOTSUPP; |
226c0c0e | 1111 | } |
df293bbb | 1112 | |
ceceae1b YK |
1113 | if (help) { |
1114 | if (help->helper == helper) | |
1115 | return 0; | |
1116 | if (help->helper) | |
1117 | return -EBUSY; | |
1118 | /* need to zero data of old helper */ | |
1119 | memset(&help->help, 0, sizeof(help->help)); | |
1120 | } else { | |
a88e22ad PNA |
1121 | /* we cannot set a helper for an existing conntrack */ |
1122 | return -EOPNOTSUPP; | |
ceceae1b | 1123 | } |
df293bbb | 1124 | |
3c158f7f | 1125 | rcu_assign_pointer(help->helper, helper); |
c1d10adb PNA |
1126 | |
1127 | return 0; | |
1128 | } | |
1129 | ||
1130 | static inline int | |
39938324 | 1131 | ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb | 1132 | { |
77236b6e | 1133 | u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
601e68e1 | 1134 | |
c1d10adb PNA |
1135 | if (!del_timer(&ct->timeout)) |
1136 | return -ETIME; | |
1137 | ||
1138 | ct->timeout.expires = jiffies + timeout * HZ; | |
1139 | add_timer(&ct->timeout); | |
1140 | ||
1141 | return 0; | |
1142 | } | |
1143 | ||
d0b0268f PM |
1144 | static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = { |
1145 | [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED }, | |
1146 | [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED }, | |
1147 | [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED }, | |
1148 | }; | |
1149 | ||
c1d10adb | 1150 | static inline int |
39938324 | 1151 | ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb | 1152 | { |
39938324 PM |
1153 | const struct nlattr *attr = cda[CTA_PROTOINFO]; |
1154 | struct nlattr *tb[CTA_PROTOINFO_MAX+1]; | |
605dcad6 | 1155 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
1156 | int err = 0; |
1157 | ||
d0b0268f | 1158 | nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy); |
c1d10adb | 1159 | |
cd91566e FW |
1160 | rcu_read_lock(); |
1161 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
fdf70832 PM |
1162 | if (l4proto->from_nlattr) |
1163 | err = l4proto->from_nlattr(tb, ct); | |
cd91566e | 1164 | rcu_read_unlock(); |
c1d10adb PNA |
1165 | |
1166 | return err; | |
1167 | } | |
1168 | ||
13eae15a | 1169 | #ifdef CONFIG_NF_NAT_NEEDED |
d0b0268f PM |
1170 | static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = { |
1171 | [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 }, | |
1172 | [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 }, | |
1173 | [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 }, | |
1174 | }; | |
1175 | ||
13eae15a | 1176 | static inline int |
39938324 | 1177 | change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr) |
13eae15a PNA |
1178 | { |
1179 | struct nlattr *cda[CTA_NAT_SEQ_MAX+1]; | |
1180 | ||
d0b0268f | 1181 | nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy); |
13eae15a PNA |
1182 | |
1183 | if (!cda[CTA_NAT_SEQ_CORRECTION_POS]) | |
1184 | return -EINVAL; | |
1185 | ||
1186 | natseq->correction_pos = | |
77236b6e | 1187 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS])); |
13eae15a PNA |
1188 | |
1189 | if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE]) | |
1190 | return -EINVAL; | |
1191 | ||
1192 | natseq->offset_before = | |
77236b6e | 1193 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE])); |
13eae15a PNA |
1194 | |
1195 | if (!cda[CTA_NAT_SEQ_OFFSET_AFTER]) | |
1196 | return -EINVAL; | |
1197 | ||
1198 | natseq->offset_after = | |
77236b6e | 1199 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER])); |
13eae15a PNA |
1200 | |
1201 | return 0; | |
1202 | } | |
1203 | ||
1204 | static int | |
39938324 PM |
1205 | ctnetlink_change_nat_seq_adj(struct nf_conn *ct, |
1206 | const struct nlattr * const cda[]) | |
13eae15a PNA |
1207 | { |
1208 | int ret = 0; | |
1209 | struct nf_conn_nat *nat = nfct_nat(ct); | |
1210 | ||
1211 | if (!nat) | |
1212 | return 0; | |
1213 | ||
1214 | if (cda[CTA_NAT_SEQ_ADJ_ORIG]) { | |
1215 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL], | |
1216 | cda[CTA_NAT_SEQ_ADJ_ORIG]); | |
1217 | if (ret < 0) | |
1218 | return ret; | |
1219 | ||
1220 | ct->status |= IPS_SEQ_ADJUST; | |
1221 | } | |
1222 | ||
1223 | if (cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1224 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY], | |
1225 | cda[CTA_NAT_SEQ_ADJ_REPLY]); | |
1226 | if (ret < 0) | |
1227 | return ret; | |
1228 | ||
1229 | ct->status |= IPS_SEQ_ADJUST; | |
1230 | } | |
1231 | ||
1232 | return 0; | |
1233 | } | |
1234 | #endif | |
1235 | ||
c1d10adb | 1236 | static int |
39938324 PM |
1237 | ctnetlink_change_conntrack(struct nf_conn *ct, |
1238 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1239 | { |
1240 | int err; | |
1241 | ||
e098360f PNA |
1242 | /* only allow NAT changes and master assignation for new conntracks */ |
1243 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER]) | |
1244 | return -EOPNOTSUPP; | |
1245 | ||
df6fb868 | 1246 | if (cda[CTA_HELP]) { |
c1d10adb PNA |
1247 | err = ctnetlink_change_helper(ct, cda); |
1248 | if (err < 0) | |
1249 | return err; | |
1250 | } | |
1251 | ||
df6fb868 | 1252 | if (cda[CTA_TIMEOUT]) { |
c1d10adb PNA |
1253 | err = ctnetlink_change_timeout(ct, cda); |
1254 | if (err < 0) | |
1255 | return err; | |
1256 | } | |
1257 | ||
df6fb868 | 1258 | if (cda[CTA_STATUS]) { |
c1d10adb PNA |
1259 | err = ctnetlink_change_status(ct, cda); |
1260 | if (err < 0) | |
1261 | return err; | |
1262 | } | |
1263 | ||
df6fb868 | 1264 | if (cda[CTA_PROTOINFO]) { |
c1d10adb PNA |
1265 | err = ctnetlink_change_protoinfo(ct, cda); |
1266 | if (err < 0) | |
1267 | return err; | |
1268 | } | |
1269 | ||
bcd1e830 | 1270 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1271 | if (cda[CTA_MARK]) |
77236b6e | 1272 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1273 | #endif |
1274 | ||
13eae15a PNA |
1275 | #ifdef CONFIG_NF_NAT_NEEDED |
1276 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1277 | err = ctnetlink_change_nat_seq_adj(ct, cda); | |
1278 | if (err < 0) | |
1279 | return err; | |
1280 | } | |
1281 | #endif | |
1282 | ||
c1d10adb PNA |
1283 | return 0; |
1284 | } | |
1285 | ||
f0a3c086 | 1286 | static struct nf_conn * |
ef00f89f | 1287 | ctnetlink_create_conntrack(struct net *net, u16 zone, |
9592a5c0 | 1288 | const struct nlattr * const cda[], |
c1d10adb | 1289 | struct nf_conntrack_tuple *otuple, |
5faa1f4c | 1290 | struct nf_conntrack_tuple *rtuple, |
7ec47496 | 1291 | u8 u3) |
c1d10adb PNA |
1292 | { |
1293 | struct nf_conn *ct; | |
1294 | int err = -EINVAL; | |
ceceae1b | 1295 | struct nf_conntrack_helper *helper; |
c1d10adb | 1296 | |
ef00f89f | 1297 | ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC); |
cd7fcbf1 | 1298 | if (IS_ERR(ct)) |
f0a3c086 | 1299 | return ERR_PTR(-ENOMEM); |
c1d10adb | 1300 | |
df6fb868 | 1301 | if (!cda[CTA_TIMEOUT]) |
0f5b3e85 | 1302 | goto err1; |
77236b6e | 1303 | ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
c1d10adb PNA |
1304 | |
1305 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; | |
c1d10adb | 1306 | |
1575e7ea | 1307 | rcu_read_lock(); |
226c0c0e | 1308 | if (cda[CTA_HELP]) { |
29fe1b48 | 1309 | char *helpname = NULL; |
226c0c0e PNA |
1310 | |
1311 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); | |
0f5b3e85 PM |
1312 | if (err < 0) |
1313 | goto err2; | |
226c0c0e | 1314 | |
794e6871 PM |
1315 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1316 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1317 | if (helper == NULL) { |
1318 | rcu_read_unlock(); | |
1319 | #ifdef CONFIG_MODULES | |
1320 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1321 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1322 | goto err1; |
226c0c0e PNA |
1323 | } |
1324 | ||
1325 | rcu_read_lock(); | |
794e6871 PM |
1326 | helper = __nf_conntrack_helper_find(helpname, |
1327 | nf_ct_l3num(ct), | |
1328 | nf_ct_protonum(ct)); | |
226c0c0e | 1329 | if (helper) { |
226c0c0e | 1330 | err = -EAGAIN; |
0f5b3e85 | 1331 | goto err2; |
226c0c0e PNA |
1332 | } |
1333 | rcu_read_unlock(); | |
1334 | #endif | |
1335 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1336 | goto err1; |
226c0c0e PNA |
1337 | } else { |
1338 | struct nf_conn_help *help; | |
1339 | ||
1340 | help = nf_ct_helper_ext_add(ct, GFP_ATOMIC); | |
1341 | if (help == NULL) { | |
226c0c0e | 1342 | err = -ENOMEM; |
0f5b3e85 | 1343 | goto err2; |
226c0c0e PNA |
1344 | } |
1345 | ||
1346 | /* not in hash table yet so not strictly necessary */ | |
1347 | rcu_assign_pointer(help->helper, helper); | |
1348 | } | |
1349 | } else { | |
1350 | /* try an implicit helper assignation */ | |
b2a15a60 | 1351 | err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC); |
0f5b3e85 PM |
1352 | if (err < 0) |
1353 | goto err2; | |
1575e7ea PNA |
1354 | } |
1355 | ||
a88e22ad PNA |
1356 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) { |
1357 | err = ctnetlink_change_nat(ct, cda); | |
0f5b3e85 PM |
1358 | if (err < 0) |
1359 | goto err2; | |
e6a7d3c0 PNA |
1360 | } |
1361 | ||
a88e22ad PNA |
1362 | nf_ct_acct_ext_add(ct, GFP_ATOMIC); |
1363 | nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC); | |
1364 | /* we must add conntrack extensions before confirmation. */ | |
1365 | ct->status |= IPS_CONFIRMED; | |
1366 | ||
1367 | if (cda[CTA_STATUS]) { | |
1368 | err = ctnetlink_change_status(ct, cda); | |
0f5b3e85 PM |
1369 | if (err < 0) |
1370 | goto err2; | |
bbb3357d | 1371 | } |
c1d10adb | 1372 | |
c969aa7d PNA |
1373 | #ifdef CONFIG_NF_NAT_NEEDED |
1374 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1375 | err = ctnetlink_change_nat_seq_adj(ct, cda); | |
0f5b3e85 PM |
1376 | if (err < 0) |
1377 | goto err2; | |
c969aa7d PNA |
1378 | } |
1379 | #endif | |
1380 | ||
e5fc9e7a | 1381 | memset(&ct->proto, 0, sizeof(ct->proto)); |
df6fb868 | 1382 | if (cda[CTA_PROTOINFO]) { |
c1d10adb | 1383 | err = ctnetlink_change_protoinfo(ct, cda); |
0f5b3e85 PM |
1384 | if (err < 0) |
1385 | goto err2; | |
c1d10adb PNA |
1386 | } |
1387 | ||
bcd1e830 | 1388 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1389 | if (cda[CTA_MARK]) |
77236b6e | 1390 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1391 | #endif |
1392 | ||
5faa1f4c | 1393 | /* setup master conntrack: this is a confirmed expectation */ |
7ec47496 PNA |
1394 | if (cda[CTA_TUPLE_MASTER]) { |
1395 | struct nf_conntrack_tuple master; | |
1396 | struct nf_conntrack_tuple_hash *master_h; | |
1397 | struct nf_conn *master_ct; | |
1398 | ||
1399 | err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3); | |
1400 | if (err < 0) | |
0f5b3e85 | 1401 | goto err2; |
7ec47496 | 1402 | |
ef00f89f | 1403 | master_h = nf_conntrack_find_get(net, zone, &master); |
7ec47496 PNA |
1404 | if (master_h == NULL) { |
1405 | err = -ENOENT; | |
0f5b3e85 | 1406 | goto err2; |
7ec47496 PNA |
1407 | } |
1408 | master_ct = nf_ct_tuplehash_to_ctrack(master_h); | |
f2a89004 | 1409 | __set_bit(IPS_EXPECTED_BIT, &ct->status); |
5faa1f4c | 1410 | ct->master = master_ct; |
f2a89004 | 1411 | } |
5faa1f4c | 1412 | |
c1d10adb PNA |
1413 | add_timer(&ct->timeout); |
1414 | nf_conntrack_hash_insert(ct); | |
58a3c9bb | 1415 | rcu_read_unlock(); |
dafc741c | 1416 | |
f0a3c086 | 1417 | return ct; |
c1d10adb | 1418 | |
0f5b3e85 PM |
1419 | err2: |
1420 | rcu_read_unlock(); | |
1421 | err1: | |
c1d10adb | 1422 | nf_conntrack_free(ct); |
f0a3c086 | 1423 | return ERR_PTR(err); |
c1d10adb PNA |
1424 | } |
1425 | ||
601e68e1 YH |
1426 | static int |
1427 | ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
1428 | const struct nlmsghdr *nlh, |
1429 | const struct nlattr * const cda[]) | |
c1d10adb | 1430 | { |
9592a5c0 | 1431 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1432 | struct nf_conntrack_tuple otuple, rtuple; |
1433 | struct nf_conntrack_tuple_hash *h = NULL; | |
96bcf938 | 1434 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 1435 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
1436 | u16 zone; |
1437 | int err; | |
1438 | ||
1439 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); | |
1440 | if (err < 0) | |
1441 | return err; | |
c1d10adb | 1442 | |
df6fb868 | 1443 | if (cda[CTA_TUPLE_ORIG]) { |
c1d10adb PNA |
1444 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3); |
1445 | if (err < 0) | |
1446 | return err; | |
1447 | } | |
1448 | ||
df6fb868 | 1449 | if (cda[CTA_TUPLE_REPLY]) { |
c1d10adb PNA |
1450 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3); |
1451 | if (err < 0) | |
1452 | return err; | |
1453 | } | |
1454 | ||
f8ba1aff | 1455 | spin_lock_bh(&nf_conntrack_lock); |
df6fb868 | 1456 | if (cda[CTA_TUPLE_ORIG]) |
ef00f89f | 1457 | h = __nf_conntrack_find(net, zone, &otuple); |
df6fb868 | 1458 | else if (cda[CTA_TUPLE_REPLY]) |
ef00f89f | 1459 | h = __nf_conntrack_find(net, zone, &rtuple); |
c1d10adb PNA |
1460 | |
1461 | if (h == NULL) { | |
c1d10adb | 1462 | err = -ENOENT; |
f0a3c086 PNA |
1463 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
1464 | struct nf_conn *ct; | |
fecc1133 | 1465 | enum ip_conntrack_events events; |
5faa1f4c | 1466 | |
ef00f89f | 1467 | ct = ctnetlink_create_conntrack(net, zone, cda, &otuple, |
f0a3c086 PNA |
1468 | &rtuple, u3); |
1469 | if (IS_ERR(ct)) { | |
1470 | err = PTR_ERR(ct); | |
5faa1f4c PNA |
1471 | goto out_unlock; |
1472 | } | |
f0a3c086 PNA |
1473 | err = 0; |
1474 | nf_conntrack_get(&ct->ct_general); | |
1475 | spin_unlock_bh(&nf_conntrack_lock); | |
fecc1133 PNA |
1476 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) |
1477 | events = IPCT_RELATED; | |
1478 | else | |
1479 | events = IPCT_NEW; | |
1480 | ||
858b3133 PM |
1481 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1482 | (1 << IPCT_ASSURED) | | |
a0891aa6 PNA |
1483 | (1 << IPCT_HELPER) | |
1484 | (1 << IPCT_PROTOINFO) | | |
1485 | (1 << IPCT_NATSEQADJ) | | |
1486 | (1 << IPCT_MARK) | events, | |
1487 | ct, NETLINK_CB(skb).pid, | |
1488 | nlmsg_report(nlh)); | |
f0a3c086 PNA |
1489 | nf_ct_put(ct); |
1490 | } else | |
1491 | spin_unlock_bh(&nf_conntrack_lock); | |
5faa1f4c | 1492 | |
c1d10adb PNA |
1493 | return err; |
1494 | } | |
1495 | /* implicit 'else' */ | |
1496 | ||
c1d10adb PNA |
1497 | /* We manipulate the conntrack inside the global conntrack table lock, |
1498 | * so there's no need to increase the refcount */ | |
c1d10adb | 1499 | err = -EEXIST; |
ff4ca827 | 1500 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) { |
19abb7b0 PNA |
1501 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); |
1502 | ||
19abb7b0 PNA |
1503 | err = ctnetlink_change_conntrack(ct, cda); |
1504 | if (err == 0) { | |
1505 | nf_conntrack_get(&ct->ct_general); | |
1506 | spin_unlock_bh(&nf_conntrack_lock); | |
858b3133 PM |
1507 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1508 | (1 << IPCT_ASSURED) | | |
a0891aa6 PNA |
1509 | (1 << IPCT_HELPER) | |
1510 | (1 << IPCT_PROTOINFO) | | |
1511 | (1 << IPCT_NATSEQADJ) | | |
1512 | (1 << IPCT_MARK), | |
1513 | ct, NETLINK_CB(skb).pid, | |
1514 | nlmsg_report(nlh)); | |
19abb7b0 PNA |
1515 | nf_ct_put(ct); |
1516 | } else | |
1517 | spin_unlock_bh(&nf_conntrack_lock); | |
1518 | ||
1519 | return err; | |
ff4ca827 | 1520 | } |
c1d10adb PNA |
1521 | |
1522 | out_unlock: | |
f8ba1aff | 1523 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
1524 | return err; |
1525 | } | |
1526 | ||
601e68e1 YH |
1527 | /*********************************************************************** |
1528 | * EXPECT | |
1529 | ***********************************************************************/ | |
c1d10adb PNA |
1530 | |
1531 | static inline int | |
1532 | ctnetlink_exp_dump_tuple(struct sk_buff *skb, | |
1533 | const struct nf_conntrack_tuple *tuple, | |
1534 | enum ctattr_expect type) | |
1535 | { | |
df6fb868 | 1536 | struct nlattr *nest_parms; |
601e68e1 | 1537 | |
df6fb868 PM |
1538 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
1539 | if (!nest_parms) | |
1540 | goto nla_put_failure; | |
c1d10adb | 1541 | if (ctnetlink_dump_tuples(skb, tuple) < 0) |
df6fb868 PM |
1542 | goto nla_put_failure; |
1543 | nla_nest_end(skb, nest_parms); | |
c1d10adb PNA |
1544 | |
1545 | return 0; | |
1546 | ||
df6fb868 | 1547 | nla_put_failure: |
c1d10adb | 1548 | return -1; |
601e68e1 | 1549 | } |
c1d10adb | 1550 | |
1cde6436 PNA |
1551 | static inline int |
1552 | ctnetlink_exp_dump_mask(struct sk_buff *skb, | |
1553 | const struct nf_conntrack_tuple *tuple, | |
d4156e8c | 1554 | const struct nf_conntrack_tuple_mask *mask) |
1cde6436 PNA |
1555 | { |
1556 | int ret; | |
1557 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 1558 | struct nf_conntrack_l4proto *l4proto; |
d4156e8c | 1559 | struct nf_conntrack_tuple m; |
df6fb868 | 1560 | struct nlattr *nest_parms; |
d4156e8c PM |
1561 | |
1562 | memset(&m, 0xFF, sizeof(m)); | |
d4156e8c | 1563 | memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3)); |
e578756c PM |
1564 | m.src.u.all = mask->src.u.all; |
1565 | m.dst.protonum = tuple->dst.protonum; | |
d4156e8c | 1566 | |
df6fb868 PM |
1567 | nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED); |
1568 | if (!nest_parms) | |
1569 | goto nla_put_failure; | |
1cde6436 | 1570 | |
528a3a6f | 1571 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
d4156e8c | 1572 | ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto); |
1cde6436 PNA |
1573 | |
1574 | if (unlikely(ret < 0)) | |
df6fb868 | 1575 | goto nla_put_failure; |
1cde6436 | 1576 | |
528a3a6f | 1577 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); |
d4156e8c | 1578 | ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto); |
1cde6436 | 1579 | if (unlikely(ret < 0)) |
df6fb868 | 1580 | goto nla_put_failure; |
1cde6436 | 1581 | |
df6fb868 | 1582 | nla_nest_end(skb, nest_parms); |
1cde6436 PNA |
1583 | |
1584 | return 0; | |
1585 | ||
df6fb868 | 1586 | nla_put_failure: |
1cde6436 PNA |
1587 | return -1; |
1588 | } | |
1589 | ||
bb5cf80e | 1590 | static int |
c1d10adb | 1591 | ctnetlink_exp_dump_expect(struct sk_buff *skb, |
601e68e1 | 1592 | const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
1593 | { |
1594 | struct nf_conn *master = exp->master; | |
d978e5da | 1595 | long timeout = (exp->timeout.expires - jiffies) / HZ; |
bc01befd | 1596 | struct nf_conn_help *help; |
d978e5da PM |
1597 | |
1598 | if (timeout < 0) | |
1599 | timeout = 0; | |
c1d10adb PNA |
1600 | |
1601 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) | |
df6fb868 | 1602 | goto nla_put_failure; |
1cde6436 | 1603 | if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) |
df6fb868 | 1604 | goto nla_put_failure; |
c1d10adb PNA |
1605 | if (ctnetlink_exp_dump_tuple(skb, |
1606 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
1607 | CTA_EXPECT_MASTER) < 0) | |
df6fb868 | 1608 | goto nla_put_failure; |
601e68e1 | 1609 | |
d978e5da | 1610 | NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)); |
77236b6e | 1611 | NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)); |
8b008faf | 1612 | NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)); |
bc01befd PNA |
1613 | help = nfct_help(master); |
1614 | if (help) { | |
1615 | struct nf_conntrack_helper *helper; | |
1616 | ||
1617 | helper = rcu_dereference(help->helper); | |
1618 | if (helper) | |
1619 | NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name); | |
1620 | } | |
c1d10adb PNA |
1621 | |
1622 | return 0; | |
601e68e1 | 1623 | |
df6fb868 | 1624 | nla_put_failure: |
c1d10adb PNA |
1625 | return -1; |
1626 | } | |
1627 | ||
1628 | static int | |
1629 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq, | |
8b0a231d | 1630 | int event, const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
1631 | { |
1632 | struct nlmsghdr *nlh; | |
1633 | struct nfgenmsg *nfmsg; | |
96bcf938 | 1634 | unsigned int flags = pid ? NLM_F_MULTI : 0; |
c1d10adb PNA |
1635 | |
1636 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; | |
96bcf938 PNA |
1637 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags); |
1638 | if (nlh == NULL) | |
1639 | goto nlmsg_failure; | |
c1d10adb | 1640 | |
96bcf938 | 1641 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
1642 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
1643 | nfmsg->version = NFNETLINK_V0; | |
1644 | nfmsg->res_id = 0; | |
1645 | ||
1646 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) | |
df6fb868 | 1647 | goto nla_put_failure; |
c1d10adb | 1648 | |
96bcf938 | 1649 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
1650 | return skb->len; |
1651 | ||
1652 | nlmsg_failure: | |
df6fb868 | 1653 | nla_put_failure: |
96bcf938 | 1654 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
1655 | return -1; |
1656 | } | |
1657 | ||
1658 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
e34d5c1a PNA |
1659 | static int |
1660 | ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item) | |
c1d10adb | 1661 | { |
9592a5c0 AD |
1662 | struct nf_conntrack_expect *exp = item->exp; |
1663 | struct net *net = nf_ct_exp_net(exp); | |
c1d10adb PNA |
1664 | struct nlmsghdr *nlh; |
1665 | struct nfgenmsg *nfmsg; | |
c1d10adb | 1666 | struct sk_buff *skb; |
ebbf41df | 1667 | unsigned int type, group; |
c1d10adb PNA |
1668 | int flags = 0; |
1669 | ||
ebbf41df PNA |
1670 | if (events & (1 << IPEXP_DESTROY)) { |
1671 | type = IPCTNL_MSG_EXP_DELETE; | |
1672 | group = NFNLGRP_CONNTRACK_EXP_DESTROY; | |
1673 | } else if (events & (1 << IPEXP_NEW)) { | |
c1d10adb PNA |
1674 | type = IPCTNL_MSG_EXP_NEW; |
1675 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
ebbf41df | 1676 | group = NFNLGRP_CONNTRACK_EXP_NEW; |
c1d10adb | 1677 | } else |
e34d5c1a | 1678 | return 0; |
c1d10adb | 1679 | |
ebbf41df | 1680 | if (!item->report && !nfnetlink_has_listeners(net, group)) |
e34d5c1a | 1681 | return 0; |
b3a27bfb | 1682 | |
96bcf938 PNA |
1683 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
1684 | if (skb == NULL) | |
150ace0d | 1685 | goto errout; |
c1d10adb | 1686 | |
b633ad5f | 1687 | type |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
96bcf938 PNA |
1688 | nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags); |
1689 | if (nlh == NULL) | |
1690 | goto nlmsg_failure; | |
c1d10adb | 1691 | |
96bcf938 | 1692 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
1693 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
1694 | nfmsg->version = NFNETLINK_V0; | |
1695 | nfmsg->res_id = 0; | |
1696 | ||
528a3a6f | 1697 | rcu_read_lock(); |
c1d10adb | 1698 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
df6fb868 | 1699 | goto nla_put_failure; |
528a3a6f | 1700 | rcu_read_unlock(); |
c1d10adb | 1701 | |
96bcf938 | 1702 | nlmsg_end(skb, nlh); |
ebbf41df | 1703 | nfnetlink_send(skb, net, item->pid, group, item->report, GFP_ATOMIC); |
e34d5c1a | 1704 | return 0; |
c1d10adb | 1705 | |
df6fb868 | 1706 | nla_put_failure: |
528a3a6f | 1707 | rcu_read_unlock(); |
96bcf938 | 1708 | nlmsg_cancel(skb, nlh); |
528a3a6f | 1709 | nlmsg_failure: |
c1d10adb | 1710 | kfree_skb(skb); |
150ace0d | 1711 | errout: |
9592a5c0 | 1712 | nfnetlink_set_err(net, 0, 0, -ENOBUFS); |
e34d5c1a | 1713 | return 0; |
c1d10adb PNA |
1714 | } |
1715 | #endif | |
cf6994c2 PM |
1716 | static int ctnetlink_exp_done(struct netlink_callback *cb) |
1717 | { | |
31f15875 PM |
1718 | if (cb->args[1]) |
1719 | nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]); | |
cf6994c2 PM |
1720 | return 0; |
1721 | } | |
c1d10adb PNA |
1722 | |
1723 | static int | |
1724 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
1725 | { | |
9592a5c0 | 1726 | struct net *net = sock_net(skb->sk); |
cf6994c2 | 1727 | struct nf_conntrack_expect *exp, *last; |
96bcf938 | 1728 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
31f15875 | 1729 | struct hlist_node *n; |
87711cb8 | 1730 | u_int8_t l3proto = nfmsg->nfgen_family; |
c1d10adb | 1731 | |
7d0742da | 1732 | rcu_read_lock(); |
31f15875 PM |
1733 | last = (struct nf_conntrack_expect *)cb->args[1]; |
1734 | for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) { | |
cf6994c2 | 1735 | restart: |
9b03f38d | 1736 | hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]], |
31f15875 PM |
1737 | hnode) { |
1738 | if (l3proto && exp->tuple.src.l3num != l3proto) | |
cf6994c2 | 1739 | continue; |
31f15875 PM |
1740 | if (cb->args[1]) { |
1741 | if (exp != last) | |
1742 | continue; | |
1743 | cb->args[1] = 0; | |
1744 | } | |
8b0a231d PNA |
1745 | if (ctnetlink_exp_fill_info(skb, |
1746 | NETLINK_CB(cb->skb).pid, | |
31f15875 PM |
1747 | cb->nlh->nlmsg_seq, |
1748 | IPCTNL_MSG_EXP_NEW, | |
8b0a231d | 1749 | exp) < 0) { |
7d0742da PM |
1750 | if (!atomic_inc_not_zero(&exp->use)) |
1751 | continue; | |
31f15875 PM |
1752 | cb->args[1] = (unsigned long)exp; |
1753 | goto out; | |
1754 | } | |
cf6994c2 | 1755 | } |
31f15875 PM |
1756 | if (cb->args[1]) { |
1757 | cb->args[1] = 0; | |
1758 | goto restart; | |
cf6994c2 PM |
1759 | } |
1760 | } | |
601e68e1 | 1761 | out: |
7d0742da | 1762 | rcu_read_unlock(); |
cf6994c2 PM |
1763 | if (last) |
1764 | nf_ct_expect_put(last); | |
c1d10adb | 1765 | |
c1d10adb PNA |
1766 | return skb->len; |
1767 | } | |
1768 | ||
f73e924c | 1769 | static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = { |
d0b0268f PM |
1770 | [CTA_EXPECT_MASTER] = { .type = NLA_NESTED }, |
1771 | [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED }, | |
1772 | [CTA_EXPECT_MASK] = { .type = NLA_NESTED }, | |
f73e924c PM |
1773 | [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 }, |
1774 | [CTA_EXPECT_ID] = { .type = NLA_U32 }, | |
d0b0268f | 1775 | [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING }, |
bcac0dfa | 1776 | [CTA_EXPECT_ZONE] = { .type = NLA_U16 }, |
8b008faf | 1777 | [CTA_EXPECT_FLAGS] = { .type = NLA_U32 }, |
c1d10adb PNA |
1778 | }; |
1779 | ||
1780 | static int | |
601e68e1 | 1781 | ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
1782 | const struct nlmsghdr *nlh, |
1783 | const struct nlattr * const cda[]) | |
c1d10adb | 1784 | { |
9592a5c0 | 1785 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1786 | struct nf_conntrack_tuple tuple; |
1787 | struct nf_conntrack_expect *exp; | |
1788 | struct sk_buff *skb2; | |
96bcf938 | 1789 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 1790 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
1791 | u16 zone; |
1792 | int err; | |
c1d10adb | 1793 | |
c1d10adb | 1794 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
c702e804 TG |
1795 | return netlink_dump_start(ctnl, skb, nlh, |
1796 | ctnetlink_exp_dump_table, | |
cf6994c2 | 1797 | ctnetlink_exp_done); |
c1d10adb PNA |
1798 | } |
1799 | ||
ef00f89f PM |
1800 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
1801 | if (err < 0) | |
1802 | return err; | |
1803 | ||
df6fb868 | 1804 | if (cda[CTA_EXPECT_MASTER]) |
c1d10adb PNA |
1805 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3); |
1806 | else | |
1807 | return -EINVAL; | |
1808 | ||
1809 | if (err < 0) | |
1810 | return err; | |
1811 | ||
ef00f89f | 1812 | exp = nf_ct_expect_find_get(net, zone, &tuple); |
c1d10adb PNA |
1813 | if (!exp) |
1814 | return -ENOENT; | |
1815 | ||
df6fb868 | 1816 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 1817 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 1818 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 1819 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1820 | return -ENOENT; |
1821 | } | |
601e68e1 | 1822 | } |
c1d10adb PNA |
1823 | |
1824 | err = -ENOMEM; | |
96bcf938 PNA |
1825 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1826 | if (skb2 == NULL) | |
c1d10adb | 1827 | goto out; |
4e9b8269 | 1828 | |
528a3a6f | 1829 | rcu_read_lock(); |
601e68e1 | 1830 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, |
8b0a231d | 1831 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp); |
528a3a6f | 1832 | rcu_read_unlock(); |
c1d10adb PNA |
1833 | if (err <= 0) |
1834 | goto free; | |
1835 | ||
6823645d | 1836 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1837 | |
1838 | return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
1839 | ||
1840 | free: | |
1841 | kfree_skb(skb2); | |
1842 | out: | |
6823645d | 1843 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1844 | return err; |
1845 | } | |
1846 | ||
1847 | static int | |
601e68e1 | 1848 | ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
1849 | const struct nlmsghdr *nlh, |
1850 | const struct nlattr * const cda[]) | |
c1d10adb | 1851 | { |
9592a5c0 | 1852 | struct net *net = sock_net(ctnl); |
31f15875 | 1853 | struct nf_conntrack_expect *exp; |
c1d10adb | 1854 | struct nf_conntrack_tuple tuple; |
96bcf938 | 1855 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
31f15875 | 1856 | struct hlist_node *n, *next; |
c1d10adb | 1857 | u_int8_t u3 = nfmsg->nfgen_family; |
31f15875 | 1858 | unsigned int i; |
ef00f89f | 1859 | u16 zone; |
c1d10adb PNA |
1860 | int err; |
1861 | ||
df6fb868 | 1862 | if (cda[CTA_EXPECT_TUPLE]) { |
c1d10adb | 1863 | /* delete a single expect by tuple */ |
ef00f89f PM |
1864 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
1865 | if (err < 0) | |
1866 | return err; | |
1867 | ||
c1d10adb PNA |
1868 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
1869 | if (err < 0) | |
1870 | return err; | |
1871 | ||
1872 | /* bump usage count to 2 */ | |
ef00f89f | 1873 | exp = nf_ct_expect_find_get(net, zone, &tuple); |
c1d10adb PNA |
1874 | if (!exp) |
1875 | return -ENOENT; | |
1876 | ||
df6fb868 | 1877 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 1878 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 1879 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 1880 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1881 | return -ENOENT; |
1882 | } | |
1883 | } | |
1884 | ||
1885 | /* after list removal, usage count == 1 */ | |
ebbf41df PNA |
1886 | spin_lock_bh(&nf_conntrack_lock); |
1887 | if (del_timer(&exp->timeout)) { | |
1888 | nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).pid, | |
1889 | nlmsg_report(nlh)); | |
1890 | nf_ct_expect_put(exp); | |
1891 | } | |
1892 | spin_unlock_bh(&nf_conntrack_lock); | |
601e68e1 | 1893 | /* have to put what we 'get' above. |
c1d10adb | 1894 | * after this line usage count == 0 */ |
6823645d | 1895 | nf_ct_expect_put(exp); |
df6fb868 PM |
1896 | } else if (cda[CTA_EXPECT_HELP_NAME]) { |
1897 | char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
31f15875 | 1898 | struct nf_conn_help *m_help; |
c1d10adb PNA |
1899 | |
1900 | /* delete all expectations for this helper */ | |
f8ba1aff | 1901 | spin_lock_bh(&nf_conntrack_lock); |
31f15875 PM |
1902 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
1903 | hlist_for_each_entry_safe(exp, n, next, | |
9592a5c0 | 1904 | &net->ct.expect_hash[i], |
31f15875 PM |
1905 | hnode) { |
1906 | m_help = nfct_help(exp->master); | |
794e6871 PM |
1907 | if (!strcmp(m_help->helper->name, name) && |
1908 | del_timer(&exp->timeout)) { | |
ebbf41df PNA |
1909 | nf_ct_unlink_expect_report(exp, |
1910 | NETLINK_CB(skb).pid, | |
1911 | nlmsg_report(nlh)); | |
31f15875 PM |
1912 | nf_ct_expect_put(exp); |
1913 | } | |
c1d10adb PNA |
1914 | } |
1915 | } | |
f8ba1aff | 1916 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
1917 | } else { |
1918 | /* This basically means we have to flush everything*/ | |
f8ba1aff | 1919 | spin_lock_bh(&nf_conntrack_lock); |
31f15875 PM |
1920 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
1921 | hlist_for_each_entry_safe(exp, n, next, | |
9592a5c0 | 1922 | &net->ct.expect_hash[i], |
31f15875 PM |
1923 | hnode) { |
1924 | if (del_timer(&exp->timeout)) { | |
ebbf41df PNA |
1925 | nf_ct_unlink_expect_report(exp, |
1926 | NETLINK_CB(skb).pid, | |
1927 | nlmsg_report(nlh)); | |
31f15875 PM |
1928 | nf_ct_expect_put(exp); |
1929 | } | |
c1d10adb PNA |
1930 | } |
1931 | } | |
f8ba1aff | 1932 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
1933 | } |
1934 | ||
1935 | return 0; | |
1936 | } | |
1937 | static int | |
39938324 PM |
1938 | ctnetlink_change_expect(struct nf_conntrack_expect *x, |
1939 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1940 | { |
1941 | return -EOPNOTSUPP; | |
1942 | } | |
1943 | ||
1944 | static int | |
ef00f89f PM |
1945 | ctnetlink_create_expect(struct net *net, u16 zone, |
1946 | const struct nlattr * const cda[], | |
9592a5c0 | 1947 | u_int8_t u3, |
39938324 | 1948 | u32 pid, int report) |
c1d10adb PNA |
1949 | { |
1950 | struct nf_conntrack_tuple tuple, mask, master_tuple; | |
1951 | struct nf_conntrack_tuple_hash *h = NULL; | |
1952 | struct nf_conntrack_expect *exp; | |
1953 | struct nf_conn *ct; | |
dc808fe2 | 1954 | struct nf_conn_help *help; |
c1d10adb PNA |
1955 | int err = 0; |
1956 | ||
c1d10adb PNA |
1957 | /* caller guarantees that those three CTA_EXPECT_* exist */ |
1958 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
1959 | if (err < 0) | |
1960 | return err; | |
1961 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3); | |
1962 | if (err < 0) | |
1963 | return err; | |
1964 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3); | |
1965 | if (err < 0) | |
1966 | return err; | |
1967 | ||
1968 | /* Look for master conntrack of this expectation */ | |
ef00f89f | 1969 | h = nf_conntrack_find_get(net, zone, &master_tuple); |
c1d10adb PNA |
1970 | if (!h) |
1971 | return -ENOENT; | |
1972 | ct = nf_ct_tuplehash_to_ctrack(h); | |
6823645d | 1973 | exp = nf_ct_expect_alloc(ct); |
c1d10adb PNA |
1974 | if (!exp) { |
1975 | err = -ENOMEM; | |
1976 | goto out; | |
1977 | } | |
bc01befd PNA |
1978 | help = nfct_help(ct); |
1979 | if (!help) { | |
1980 | if (!cda[CTA_EXPECT_TIMEOUT]) { | |
1981 | err = -EINVAL; | |
1982 | goto out; | |
1983 | } | |
1984 | exp->timeout.expires = | |
1985 | jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ; | |
601e68e1 | 1986 | |
bc01befd PNA |
1987 | exp->flags = NF_CT_EXPECT_USERSPACE; |
1988 | if (cda[CTA_EXPECT_FLAGS]) { | |
1989 | exp->flags |= | |
1990 | ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); | |
1991 | } | |
1992 | } else { | |
1993 | if (cda[CTA_EXPECT_FLAGS]) { | |
1994 | exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); | |
1995 | exp->flags &= ~NF_CT_EXPECT_USERSPACE; | |
1996 | } else | |
1997 | exp->flags = 0; | |
1998 | } | |
601e68e1 | 1999 | |
626ba8fb | 2000 | exp->class = 0; |
c1d10adb | 2001 | exp->expectfn = NULL; |
c1d10adb | 2002 | exp->master = ct; |
9457d851 | 2003 | exp->helper = NULL; |
c1d10adb | 2004 | memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple)); |
d4156e8c PM |
2005 | memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3)); |
2006 | exp->mask.src.u.all = mask.src.u.all; | |
c1d10adb | 2007 | |
19abb7b0 | 2008 | err = nf_ct_expect_related_report(exp, pid, report); |
6823645d | 2009 | nf_ct_expect_put(exp); |
c1d10adb | 2010 | |
601e68e1 | 2011 | out: |
c1d10adb PNA |
2012 | nf_ct_put(nf_ct_tuplehash_to_ctrack(h)); |
2013 | return err; | |
2014 | } | |
2015 | ||
2016 | static int | |
2017 | ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
2018 | const struct nlmsghdr *nlh, |
2019 | const struct nlattr * const cda[]) | |
c1d10adb | 2020 | { |
9592a5c0 | 2021 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
2022 | struct nf_conntrack_tuple tuple; |
2023 | struct nf_conntrack_expect *exp; | |
96bcf938 | 2024 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 2025 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
2026 | u16 zone; |
2027 | int err; | |
c1d10adb | 2028 | |
df6fb868 PM |
2029 | if (!cda[CTA_EXPECT_TUPLE] |
2030 | || !cda[CTA_EXPECT_MASK] | |
2031 | || !cda[CTA_EXPECT_MASTER]) | |
c1d10adb PNA |
2032 | return -EINVAL; |
2033 | ||
ef00f89f PM |
2034 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2035 | if (err < 0) | |
2036 | return err; | |
2037 | ||
c1d10adb PNA |
2038 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
2039 | if (err < 0) | |
2040 | return err; | |
2041 | ||
f8ba1aff | 2042 | spin_lock_bh(&nf_conntrack_lock); |
ef00f89f | 2043 | exp = __nf_ct_expect_find(net, zone, &tuple); |
c1d10adb PNA |
2044 | |
2045 | if (!exp) { | |
f8ba1aff | 2046 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 2047 | err = -ENOENT; |
19abb7b0 | 2048 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
ef00f89f | 2049 | err = ctnetlink_create_expect(net, zone, cda, |
19abb7b0 PNA |
2050 | u3, |
2051 | NETLINK_CB(skb).pid, | |
2052 | nlmsg_report(nlh)); | |
2053 | } | |
c1d10adb PNA |
2054 | return err; |
2055 | } | |
2056 | ||
2057 | err = -EEXIST; | |
2058 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) | |
2059 | err = ctnetlink_change_expect(exp, cda); | |
f8ba1aff | 2060 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 2061 | |
c1d10adb PNA |
2062 | return err; |
2063 | } | |
2064 | ||
2065 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
e34d5c1a PNA |
2066 | static struct nf_ct_event_notifier ctnl_notifier = { |
2067 | .fcn = ctnetlink_conntrack_event, | |
c1d10adb PNA |
2068 | }; |
2069 | ||
e34d5c1a PNA |
2070 | static struct nf_exp_event_notifier ctnl_notifier_exp = { |
2071 | .fcn = ctnetlink_expect_event, | |
c1d10adb PNA |
2072 | }; |
2073 | #endif | |
2074 | ||
7c8d4cb4 | 2075 | static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { |
c1d10adb | 2076 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, |
f73e924c PM |
2077 | .attr_count = CTA_MAX, |
2078 | .policy = ct_nla_policy }, | |
c1d10adb | 2079 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
2080 | .attr_count = CTA_MAX, |
2081 | .policy = ct_nla_policy }, | |
c1d10adb | 2082 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, |
f73e924c PM |
2083 | .attr_count = CTA_MAX, |
2084 | .policy = ct_nla_policy }, | |
c1d10adb | 2085 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
2086 | .attr_count = CTA_MAX, |
2087 | .policy = ct_nla_policy }, | |
c1d10adb PNA |
2088 | }; |
2089 | ||
7c8d4cb4 | 2090 | static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { |
c1d10adb | 2091 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
f73e924c PM |
2092 | .attr_count = CTA_EXPECT_MAX, |
2093 | .policy = exp_nla_policy }, | |
c1d10adb | 2094 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, |
f73e924c PM |
2095 | .attr_count = CTA_EXPECT_MAX, |
2096 | .policy = exp_nla_policy }, | |
c1d10adb | 2097 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, |
f73e924c PM |
2098 | .attr_count = CTA_EXPECT_MAX, |
2099 | .policy = exp_nla_policy }, | |
c1d10adb PNA |
2100 | }; |
2101 | ||
7c8d4cb4 | 2102 | static const struct nfnetlink_subsystem ctnl_subsys = { |
c1d10adb PNA |
2103 | .name = "conntrack", |
2104 | .subsys_id = NFNL_SUBSYS_CTNETLINK, | |
2105 | .cb_count = IPCTNL_MSG_MAX, | |
2106 | .cb = ctnl_cb, | |
2107 | }; | |
2108 | ||
7c8d4cb4 | 2109 | static const struct nfnetlink_subsystem ctnl_exp_subsys = { |
c1d10adb PNA |
2110 | .name = "conntrack_expect", |
2111 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, | |
2112 | .cb_count = IPCTNL_MSG_EXP_MAX, | |
2113 | .cb = ctnl_exp_cb, | |
2114 | }; | |
2115 | ||
d2483dde | 2116 | MODULE_ALIAS("ip_conntrack_netlink"); |
c1d10adb | 2117 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); |
34f9a2e4 | 2118 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); |
c1d10adb PNA |
2119 | |
2120 | static int __init ctnetlink_init(void) | |
2121 | { | |
2122 | int ret; | |
2123 | ||
654d0fbd | 2124 | pr_info("ctnetlink v%s: registering with nfnetlink.\n", version); |
c1d10adb PNA |
2125 | ret = nfnetlink_subsys_register(&ctnl_subsys); |
2126 | if (ret < 0) { | |
654d0fbd | 2127 | pr_err("ctnetlink_init: cannot register with nfnetlink.\n"); |
c1d10adb PNA |
2128 | goto err_out; |
2129 | } | |
2130 | ||
2131 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); | |
2132 | if (ret < 0) { | |
654d0fbd | 2133 | pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n"); |
c1d10adb PNA |
2134 | goto err_unreg_subsys; |
2135 | } | |
2136 | ||
2137 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2138 | ret = nf_conntrack_register_notifier(&ctnl_notifier); | |
2139 | if (ret < 0) { | |
654d0fbd | 2140 | pr_err("ctnetlink_init: cannot register notifier.\n"); |
c1d10adb PNA |
2141 | goto err_unreg_exp_subsys; |
2142 | } | |
2143 | ||
6823645d | 2144 | ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp); |
c1d10adb | 2145 | if (ret < 0) { |
654d0fbd | 2146 | pr_err("ctnetlink_init: cannot expect register notifier.\n"); |
c1d10adb PNA |
2147 | goto err_unreg_notifier; |
2148 | } | |
2149 | #endif | |
2150 | ||
2151 | return 0; | |
2152 | ||
2153 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2154 | err_unreg_notifier: | |
2155 | nf_conntrack_unregister_notifier(&ctnl_notifier); | |
2156 | err_unreg_exp_subsys: | |
2157 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
2158 | #endif | |
2159 | err_unreg_subsys: | |
2160 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
2161 | err_out: | |
2162 | return ret; | |
2163 | } | |
2164 | ||
2165 | static void __exit ctnetlink_exit(void) | |
2166 | { | |
654d0fbd | 2167 | pr_info("ctnetlink: unregistering from nfnetlink.\n"); |
c1d10adb | 2168 | |
bc01befd | 2169 | nf_ct_remove_userspace_expectations(); |
c1d10adb | 2170 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
6823645d | 2171 | nf_ct_expect_unregister_notifier(&ctnl_notifier_exp); |
c1d10adb PNA |
2172 | nf_conntrack_unregister_notifier(&ctnl_notifier); |
2173 | #endif | |
2174 | ||
2175 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
2176 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
c1d10adb PNA |
2177 | } |
2178 | ||
2179 | module_init(ctnetlink_init); | |
2180 | module_exit(ctnetlink_exit); |