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> |
392025f8 | 7 | * (C) 2005-2012 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> |
a992ca2a | 45 | #include <net/netfilter/nf_conntrack_timestamp.h> |
c539f017 | 46 | #include <net/netfilter/nf_conntrack_labels.h> |
5b1158e9 JK |
47 | #ifdef CONFIG_NF_NAT_NEEDED |
48 | #include <net/netfilter/nf_nat_core.h> | |
c7232c99 | 49 | #include <net/netfilter/nf_nat_l4proto.h> |
8c88f87c | 50 | #include <net/netfilter/nf_nat_helper.h> |
5b1158e9 | 51 | #endif |
c1d10adb PNA |
52 | |
53 | #include <linux/netfilter/nfnetlink.h> | |
54 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
55 | ||
56 | MODULE_LICENSE("GPL"); | |
57 | ||
dc808fe2 | 58 | static char __initdata version[] = "0.93"; |
c1d10adb | 59 | |
c1d10adb | 60 | static inline int |
601e68e1 | 61 | ctnetlink_dump_tuples_proto(struct sk_buff *skb, |
1cde6436 | 62 | const struct nf_conntrack_tuple *tuple, |
605dcad6 | 63 | struct nf_conntrack_l4proto *l4proto) |
c1d10adb | 64 | { |
c1d10adb | 65 | int ret = 0; |
df6fb868 | 66 | struct nlattr *nest_parms; |
c1d10adb | 67 | |
df6fb868 PM |
68 | nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED); |
69 | if (!nest_parms) | |
70 | goto nla_put_failure; | |
cc1eb431 DM |
71 | if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum)) |
72 | goto nla_put_failure; | |
c1d10adb | 73 | |
fdf70832 PM |
74 | if (likely(l4proto->tuple_to_nlattr)) |
75 | ret = l4proto->tuple_to_nlattr(skb, tuple); | |
601e68e1 | 76 | |
df6fb868 | 77 | nla_nest_end(skb, nest_parms); |
c1d10adb PNA |
78 | |
79 | return ret; | |
80 | ||
df6fb868 | 81 | nla_put_failure: |
c1d10adb PNA |
82 | return -1; |
83 | } | |
84 | ||
85 | static inline int | |
1cde6436 PNA |
86 | ctnetlink_dump_tuples_ip(struct sk_buff *skb, |
87 | const struct nf_conntrack_tuple *tuple, | |
88 | struct nf_conntrack_l3proto *l3proto) | |
c1d10adb | 89 | { |
c1d10adb | 90 | int ret = 0; |
df6fb868 PM |
91 | struct nlattr *nest_parms; |
92 | ||
93 | nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED); | |
94 | if (!nest_parms) | |
95 | goto nla_put_failure; | |
1cde6436 | 96 | |
fdf70832 PM |
97 | if (likely(l3proto->tuple_to_nlattr)) |
98 | ret = l3proto->tuple_to_nlattr(skb, tuple); | |
1cde6436 | 99 | |
df6fb868 | 100 | nla_nest_end(skb, nest_parms); |
c1d10adb | 101 | |
1cde6436 PNA |
102 | return ret; |
103 | ||
df6fb868 | 104 | nla_put_failure: |
1cde6436 PNA |
105 | return -1; |
106 | } | |
107 | ||
bb5cf80e | 108 | static int |
1cde6436 PNA |
109 | ctnetlink_dump_tuples(struct sk_buff *skb, |
110 | const struct nf_conntrack_tuple *tuple) | |
111 | { | |
112 | int ret; | |
113 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 114 | struct nf_conntrack_l4proto *l4proto; |
1cde6436 | 115 | |
3b988ece | 116 | rcu_read_lock(); |
528a3a6f | 117 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
1cde6436 | 118 | ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto); |
c1d10adb | 119 | |
3b988ece HS |
120 | if (ret >= 0) { |
121 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, | |
122 | tuple->dst.protonum); | |
123 | ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto); | |
124 | } | |
125 | rcu_read_unlock(); | |
c1d10adb | 126 | return ret; |
c1d10adb PNA |
127 | } |
128 | ||
129 | static inline int | |
130 | ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct) | |
131 | { | |
cc1eb431 DM |
132 | if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status))) |
133 | goto nla_put_failure; | |
c1d10adb PNA |
134 | return 0; |
135 | ||
df6fb868 | 136 | nla_put_failure: |
c1d10adb PNA |
137 | return -1; |
138 | } | |
139 | ||
140 | static inline int | |
141 | ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct) | |
142 | { | |
c1216382 | 143 | long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ; |
c1d10adb | 144 | |
77236b6e | 145 | if (timeout < 0) |
c1d10adb | 146 | timeout = 0; |
601e68e1 | 147 | |
cc1eb431 DM |
148 | if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout))) |
149 | goto nla_put_failure; | |
c1d10adb PNA |
150 | return 0; |
151 | ||
df6fb868 | 152 | nla_put_failure: |
c1d10adb PNA |
153 | return -1; |
154 | } | |
155 | ||
156 | static inline int | |
440f0d58 | 157 | ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct) |
c1d10adb | 158 | { |
5e8fbe2a | 159 | struct nf_conntrack_l4proto *l4proto; |
df6fb868 | 160 | struct nlattr *nest_proto; |
c1d10adb PNA |
161 | int ret; |
162 | ||
528a3a6f PNA |
163 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
164 | if (!l4proto->to_nlattr) | |
c1d10adb | 165 | return 0; |
601e68e1 | 166 | |
df6fb868 PM |
167 | nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED); |
168 | if (!nest_proto) | |
169 | goto nla_put_failure; | |
c1d10adb | 170 | |
fdf70832 | 171 | ret = l4proto->to_nlattr(skb, nest_proto, ct); |
c1d10adb | 172 | |
df6fb868 | 173 | nla_nest_end(skb, nest_proto); |
c1d10adb PNA |
174 | |
175 | return ret; | |
176 | ||
df6fb868 | 177 | nla_put_failure: |
c1d10adb PNA |
178 | return -1; |
179 | } | |
180 | ||
181 | static inline int | |
182 | ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct) | |
183 | { | |
df6fb868 | 184 | struct nlattr *nest_helper; |
dc808fe2 | 185 | const struct nf_conn_help *help = nfct_help(ct); |
3c158f7f | 186 | struct nf_conntrack_helper *helper; |
c1d10adb | 187 | |
3c158f7f | 188 | if (!help) |
c1d10adb | 189 | return 0; |
601e68e1 | 190 | |
3c158f7f PM |
191 | helper = rcu_dereference(help->helper); |
192 | if (!helper) | |
193 | goto out; | |
194 | ||
df6fb868 PM |
195 | nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED); |
196 | if (!nest_helper) | |
197 | goto nla_put_failure; | |
cc1eb431 DM |
198 | if (nla_put_string(skb, CTA_HELP_NAME, helper->name)) |
199 | goto nla_put_failure; | |
c1d10adb | 200 | |
fdf70832 PM |
201 | if (helper->to_nlattr) |
202 | helper->to_nlattr(skb, ct); | |
c1d10adb | 203 | |
df6fb868 | 204 | nla_nest_end(skb, nest_helper); |
3c158f7f | 205 | out: |
c1d10adb PNA |
206 | return 0; |
207 | ||
df6fb868 | 208 | nla_put_failure: |
c1d10adb PNA |
209 | return -1; |
210 | } | |
211 | ||
bb5cf80e | 212 | static int |
80e60e67 PNA |
213 | dump_counters(struct sk_buff *skb, u64 pkts, u64 bytes, |
214 | enum ip_conntrack_dir dir) | |
c1d10adb PNA |
215 | { |
216 | enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; | |
df6fb868 | 217 | struct nlattr *nest_count; |
c1d10adb | 218 | |
df6fb868 PM |
219 | nest_count = nla_nest_start(skb, type | NLA_F_NESTED); |
220 | if (!nest_count) | |
221 | goto nla_put_failure; | |
222 | ||
cc1eb431 DM |
223 | if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts)) || |
224 | nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes))) | |
225 | goto nla_put_failure; | |
c1d10adb | 226 | |
df6fb868 | 227 | nla_nest_end(skb, nest_count); |
c1d10adb PNA |
228 | |
229 | return 0; | |
230 | ||
df6fb868 | 231 | nla_put_failure: |
c1d10adb PNA |
232 | return -1; |
233 | } | |
c1d10adb | 234 | |
80e60e67 PNA |
235 | static int |
236 | ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct, | |
237 | enum ip_conntrack_dir dir, int type) | |
238 | { | |
239 | struct nf_conn_counter *acct; | |
240 | u64 pkts, bytes; | |
241 | ||
242 | acct = nf_conn_acct_find(ct); | |
243 | if (!acct) | |
244 | return 0; | |
245 | ||
246 | if (type == IPCTNL_MSG_CT_GET_CTRZERO) { | |
247 | pkts = atomic64_xchg(&acct[dir].packets, 0); | |
248 | bytes = atomic64_xchg(&acct[dir].bytes, 0); | |
249 | } else { | |
250 | pkts = atomic64_read(&acct[dir].packets); | |
251 | bytes = atomic64_read(&acct[dir].bytes); | |
252 | } | |
253 | return dump_counters(skb, pkts, bytes, dir); | |
254 | } | |
255 | ||
a992ca2a PNA |
256 | static int |
257 | ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct) | |
258 | { | |
259 | struct nlattr *nest_count; | |
260 | const struct nf_conn_tstamp *tstamp; | |
261 | ||
262 | tstamp = nf_conn_tstamp_find(ct); | |
263 | if (!tstamp) | |
264 | return 0; | |
265 | ||
266 | nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED); | |
267 | if (!nest_count) | |
268 | goto nla_put_failure; | |
269 | ||
cc1eb431 DM |
270 | if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start)) || |
271 | (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP, | |
272 | cpu_to_be64(tstamp->stop)))) | |
273 | goto nla_put_failure; | |
a992ca2a PNA |
274 | nla_nest_end(skb, nest_count); |
275 | ||
276 | return 0; | |
277 | ||
278 | nla_put_failure: | |
279 | return -1; | |
280 | } | |
281 | ||
c1d10adb PNA |
282 | #ifdef CONFIG_NF_CONNTRACK_MARK |
283 | static inline int | |
284 | ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct) | |
285 | { | |
cc1eb431 DM |
286 | if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark))) |
287 | goto nla_put_failure; | |
c1d10adb PNA |
288 | return 0; |
289 | ||
df6fb868 | 290 | nla_put_failure: |
c1d10adb PNA |
291 | return -1; |
292 | } | |
293 | #else | |
294 | #define ctnetlink_dump_mark(a, b) (0) | |
295 | #endif | |
296 | ||
37fccd85 PNA |
297 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
298 | static inline int | |
1cc63249 | 299 | ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct) |
37fccd85 | 300 | { |
1cc63249 EP |
301 | struct nlattr *nest_secctx; |
302 | int len, ret; | |
303 | char *secctx; | |
304 | ||
305 | ret = security_secid_to_secctx(ct->secmark, &secctx, &len); | |
306 | if (ret) | |
cba85b53 | 307 | return 0; |
1cc63249 EP |
308 | |
309 | ret = -1; | |
310 | nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED); | |
311 | if (!nest_secctx) | |
312 | goto nla_put_failure; | |
37fccd85 | 313 | |
cc1eb431 DM |
314 | if (nla_put_string(skb, CTA_SECCTX_NAME, secctx)) |
315 | goto nla_put_failure; | |
1cc63249 EP |
316 | nla_nest_end(skb, nest_secctx); |
317 | ||
318 | ret = 0; | |
37fccd85 | 319 | nla_put_failure: |
1cc63249 EP |
320 | security_release_secctx(secctx, len); |
321 | return ret; | |
37fccd85 PNA |
322 | } |
323 | #else | |
1cc63249 | 324 | #define ctnetlink_dump_secctx(a, b) (0) |
37fccd85 PNA |
325 | #endif |
326 | ||
0ceabd83 FW |
327 | #ifdef CONFIG_NF_CONNTRACK_LABELS |
328 | static int ctnetlink_label_size(const struct nf_conn *ct) | |
329 | { | |
330 | struct nf_conn_labels *labels = nf_ct_labels_find(ct); | |
331 | ||
332 | if (!labels) | |
333 | return 0; | |
334 | return nla_total_size(labels->words * sizeof(long)); | |
335 | } | |
336 | ||
337 | static int | |
338 | ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct) | |
339 | { | |
340 | struct nf_conn_labels *labels = nf_ct_labels_find(ct); | |
341 | unsigned int len, i; | |
342 | ||
343 | if (!labels) | |
344 | return 0; | |
345 | ||
346 | len = labels->words * sizeof(long); | |
347 | i = 0; | |
348 | do { | |
349 | if (labels->bits[i] != 0) | |
350 | return nla_put(skb, CTA_LABELS, len, labels->bits); | |
351 | i++; | |
352 | } while (i < labels->words); | |
353 | ||
354 | return 0; | |
355 | } | |
356 | #else | |
357 | #define ctnetlink_dump_labels(a, b) (0) | |
358 | #define ctnetlink_label_size(a) (0) | |
359 | #endif | |
360 | ||
0f417ce9 PNA |
361 | #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple) |
362 | ||
363 | static inline int | |
364 | ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct) | |
365 | { | |
366 | struct nlattr *nest_parms; | |
367 | ||
368 | if (!(ct->status & IPS_EXPECTED)) | |
369 | return 0; | |
370 | ||
371 | nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED); | |
372 | if (!nest_parms) | |
373 | goto nla_put_failure; | |
374 | if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0) | |
375 | goto nla_put_failure; | |
376 | nla_nest_end(skb, nest_parms); | |
377 | ||
378 | return 0; | |
379 | ||
380 | nla_put_failure: | |
381 | return -1; | |
382 | } | |
383 | ||
13eae15a | 384 | #ifdef CONFIG_NF_NAT_NEEDED |
bb5cf80e | 385 | static int |
13eae15a PNA |
386 | dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type) |
387 | { | |
13eae15a PNA |
388 | struct nlattr *nest_parms; |
389 | ||
390 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); | |
391 | if (!nest_parms) | |
392 | goto nla_put_failure; | |
393 | ||
cc1eb431 DM |
394 | if (nla_put_be32(skb, CTA_NAT_SEQ_CORRECTION_POS, |
395 | htonl(natseq->correction_pos)) || | |
396 | nla_put_be32(skb, CTA_NAT_SEQ_OFFSET_BEFORE, | |
397 | htonl(natseq->offset_before)) || | |
398 | nla_put_be32(skb, CTA_NAT_SEQ_OFFSET_AFTER, | |
399 | htonl(natseq->offset_after))) | |
400 | goto nla_put_failure; | |
13eae15a PNA |
401 | |
402 | nla_nest_end(skb, nest_parms); | |
403 | ||
404 | return 0; | |
405 | ||
406 | nla_put_failure: | |
407 | return -1; | |
408 | } | |
409 | ||
410 | static inline int | |
411 | ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct) | |
412 | { | |
413 | struct nf_nat_seq *natseq; | |
414 | struct nf_conn_nat *nat = nfct_nat(ct); | |
415 | ||
416 | if (!(ct->status & IPS_SEQ_ADJUST) || !nat) | |
417 | return 0; | |
418 | ||
419 | natseq = &nat->seq[IP_CT_DIR_ORIGINAL]; | |
420 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1) | |
421 | return -1; | |
422 | ||
423 | natseq = &nat->seq[IP_CT_DIR_REPLY]; | |
424 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1) | |
425 | return -1; | |
426 | ||
427 | return 0; | |
428 | } | |
429 | #else | |
430 | #define ctnetlink_dump_nat_seq_adj(a, b) (0) | |
431 | #endif | |
432 | ||
c1d10adb PNA |
433 | static inline int |
434 | ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) | |
435 | { | |
cc1eb431 DM |
436 | if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct))) |
437 | goto nla_put_failure; | |
c1d10adb PNA |
438 | return 0; |
439 | ||
df6fb868 | 440 | nla_put_failure: |
c1d10adb PNA |
441 | return -1; |
442 | } | |
443 | ||
444 | static inline int | |
445 | ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct) | |
446 | { | |
cc1eb431 DM |
447 | if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)))) |
448 | goto nla_put_failure; | |
c1d10adb PNA |
449 | return 0; |
450 | ||
df6fb868 | 451 | nla_put_failure: |
c1d10adb PNA |
452 | return -1; |
453 | } | |
454 | ||
c1d10adb | 455 | static int |
15e47304 | 456 | ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, |
80e60e67 | 457 | struct nf_conn *ct) |
c1d10adb PNA |
458 | { |
459 | struct nlmsghdr *nlh; | |
460 | struct nfgenmsg *nfmsg; | |
df6fb868 | 461 | struct nlattr *nest_parms; |
15e47304 | 462 | unsigned int flags = portid ? NLM_F_MULTI : 0, event; |
c1d10adb | 463 | |
80e60e67 | 464 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW); |
15e47304 | 465 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
96bcf938 PNA |
466 | if (nlh == NULL) |
467 | goto nlmsg_failure; | |
c1d10adb | 468 | |
96bcf938 | 469 | nfmsg = nlmsg_data(nlh); |
5e8fbe2a | 470 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
471 | nfmsg->version = NFNETLINK_V0; |
472 | nfmsg->res_id = 0; | |
473 | ||
df6fb868 PM |
474 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
475 | if (!nest_parms) | |
476 | goto nla_put_failure; | |
f2f3e38c | 477 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
478 | goto nla_put_failure; |
479 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 480 | |
df6fb868 PM |
481 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
482 | if (!nest_parms) | |
483 | goto nla_put_failure; | |
f2f3e38c | 484 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
485 | goto nla_put_failure; |
486 | nla_nest_end(skb, nest_parms); | |
c1d10adb | 487 | |
cc1eb431 DM |
488 | if (nf_ct_zone(ct) && |
489 | nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct)))) | |
490 | goto nla_put_failure; | |
ef00f89f | 491 | |
c1d10adb PNA |
492 | if (ctnetlink_dump_status(skb, ct) < 0 || |
493 | ctnetlink_dump_timeout(skb, ct) < 0 || | |
80e60e67 PNA |
494 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL, type) < 0 || |
495 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY, type) < 0 || | |
a992ca2a | 496 | ctnetlink_dump_timestamp(skb, ct) < 0 || |
c1d10adb PNA |
497 | ctnetlink_dump_protoinfo(skb, ct) < 0 || |
498 | ctnetlink_dump_helpinfo(skb, ct) < 0 || | |
499 | ctnetlink_dump_mark(skb, ct) < 0 || | |
1cc63249 | 500 | ctnetlink_dump_secctx(skb, ct) < 0 || |
0ceabd83 | 501 | ctnetlink_dump_labels(skb, ct) < 0 || |
c1d10adb | 502 | ctnetlink_dump_id(skb, ct) < 0 || |
13eae15a | 503 | ctnetlink_dump_use(skb, ct) < 0 || |
0f417ce9 | 504 | ctnetlink_dump_master(skb, ct) < 0 || |
13eae15a | 505 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
df6fb868 | 506 | goto nla_put_failure; |
c1d10adb | 507 | |
96bcf938 | 508 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
509 | return skb->len; |
510 | ||
511 | nlmsg_failure: | |
df6fb868 | 512 | nla_put_failure: |
96bcf938 | 513 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
514 | return -1; |
515 | } | |
516 | ||
03b64f51 PNA |
517 | static inline size_t |
518 | ctnetlink_proto_size(const struct nf_conn *ct) | |
2732c4e4 HE |
519 | { |
520 | struct nf_conntrack_l3proto *l3proto; | |
521 | struct nf_conntrack_l4proto *l4proto; | |
03b64f51 PNA |
522 | size_t len = 0; |
523 | ||
524 | rcu_read_lock(); | |
525 | l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct)); | |
526 | len += l3proto->nla_size; | |
527 | ||
528 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
529 | len += l4proto->nla_size; | |
530 | rcu_read_unlock(); | |
531 | ||
532 | return len; | |
533 | } | |
534 | ||
d26e6a02 JP |
535 | static inline size_t |
536 | ctnetlink_counters_size(const struct nf_conn *ct) | |
537 | { | |
538 | if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT)) | |
539 | return 0; | |
540 | return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */ | |
541 | + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */ | |
542 | + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */ | |
543 | ; | |
544 | } | |
545 | ||
cba85b53 PNA |
546 | static inline int |
547 | ctnetlink_secctx_size(const struct nf_conn *ct) | |
1cc63249 | 548 | { |
cba85b53 PNA |
549 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
550 | int len, ret; | |
1cc63249 | 551 | |
cba85b53 PNA |
552 | ret = security_secid_to_secctx(ct->secmark, NULL, &len); |
553 | if (ret) | |
554 | return 0; | |
1cc63249 | 555 | |
cba85b53 PNA |
556 | return nla_total_size(0) /* CTA_SECCTX */ |
557 | + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */ | |
558 | #else | |
559 | return 0; | |
1cc63249 | 560 | #endif |
cba85b53 | 561 | } |
1cc63249 | 562 | |
a992ca2a PNA |
563 | static inline size_t |
564 | ctnetlink_timestamp_size(const struct nf_conn *ct) | |
565 | { | |
566 | #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP | |
567 | if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP)) | |
568 | return 0; | |
569 | return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t)); | |
570 | #else | |
571 | return 0; | |
572 | #endif | |
573 | } | |
574 | ||
03b64f51 PNA |
575 | static inline size_t |
576 | ctnetlink_nlmsg_size(const struct nf_conn *ct) | |
577 | { | |
578 | return NLMSG_ALIGN(sizeof(struct nfgenmsg)) | |
579 | + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ | |
580 | + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ | |
581 | + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ | |
582 | + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ | |
583 | + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ | |
584 | + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ | |
d26e6a02 | 585 | + ctnetlink_counters_size(ct) |
a992ca2a | 586 | + ctnetlink_timestamp_size(ct) |
03b64f51 PNA |
587 | + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ |
588 | + nla_total_size(0) /* CTA_PROTOINFO */ | |
589 | + nla_total_size(0) /* CTA_HELP */ | |
590 | + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ | |
cba85b53 | 591 | + ctnetlink_secctx_size(ct) |
d271e8bd | 592 | #ifdef CONFIG_NF_NAT_NEEDED |
03b64f51 PNA |
593 | + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ |
594 | + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ | |
d271e8bd HE |
595 | #endif |
596 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
03b64f51 | 597 | + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ |
d271e8bd | 598 | #endif |
03b64f51 | 599 | + ctnetlink_proto_size(ct) |
0ceabd83 | 600 | + ctnetlink_label_size(ct) |
03b64f51 | 601 | ; |
2732c4e4 HE |
602 | } |
603 | ||
8e36c4b5 | 604 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
e34d5c1a PNA |
605 | static int |
606 | ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item) | |
c1d10adb | 607 | { |
9592a5c0 | 608 | struct net *net; |
c1d10adb PNA |
609 | struct nlmsghdr *nlh; |
610 | struct nfgenmsg *nfmsg; | |
df6fb868 | 611 | struct nlattr *nest_parms; |
19abb7b0 | 612 | struct nf_conn *ct = item->ct; |
c1d10adb PNA |
613 | struct sk_buff *skb; |
614 | unsigned int type; | |
c1d10adb | 615 | unsigned int flags = 0, group; |
dd7669a9 | 616 | int err; |
c1d10adb PNA |
617 | |
618 | /* ignore our fake conntrack entry */ | |
5bfddbd4 | 619 | if (nf_ct_is_untracked(ct)) |
e34d5c1a | 620 | return 0; |
c1d10adb | 621 | |
a0891aa6 | 622 | if (events & (1 << IPCT_DESTROY)) { |
c1d10adb PNA |
623 | type = IPCTNL_MSG_CT_DELETE; |
624 | group = NFNLGRP_CONNTRACK_DESTROY; | |
a0891aa6 | 625 | } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) { |
c1d10adb PNA |
626 | type = IPCTNL_MSG_CT_NEW; |
627 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
c1d10adb | 628 | group = NFNLGRP_CONNTRACK_NEW; |
17e6e4ea | 629 | } else if (events) { |
c1d10adb PNA |
630 | type = IPCTNL_MSG_CT_NEW; |
631 | group = NFNLGRP_CONNTRACK_UPDATE; | |
632 | } else | |
e34d5c1a | 633 | return 0; |
a2427692 | 634 | |
9592a5c0 AD |
635 | net = nf_ct_net(ct); |
636 | if (!item->report && !nfnetlink_has_listeners(net, group)) | |
e34d5c1a | 637 | return 0; |
a2427692 | 638 | |
03b64f51 PNA |
639 | skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC); |
640 | if (skb == NULL) | |
150ace0d | 641 | goto errout; |
c1d10adb | 642 | |
c1d10adb | 643 | type |= NFNL_SUBSYS_CTNETLINK << 8; |
15e47304 | 644 | nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags); |
96bcf938 PNA |
645 | if (nlh == NULL) |
646 | goto nlmsg_failure; | |
c1d10adb | 647 | |
96bcf938 | 648 | nfmsg = nlmsg_data(nlh); |
5e8fbe2a | 649 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
650 | nfmsg->version = NFNETLINK_V0; |
651 | nfmsg->res_id = 0; | |
652 | ||
528a3a6f | 653 | rcu_read_lock(); |
df6fb868 PM |
654 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
655 | if (!nest_parms) | |
656 | goto nla_put_failure; | |
f2f3e38c | 657 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
658 | goto nla_put_failure; |
659 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 660 | |
df6fb868 PM |
661 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
662 | if (!nest_parms) | |
663 | goto nla_put_failure; | |
f2f3e38c | 664 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
665 | goto nla_put_failure; |
666 | nla_nest_end(skb, nest_parms); | |
c1d10adb | 667 | |
cc1eb431 DM |
668 | if (nf_ct_zone(ct) && |
669 | nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct)))) | |
670 | goto nla_put_failure; | |
ef00f89f | 671 | |
1eedf699 EL |
672 | if (ctnetlink_dump_id(skb, ct) < 0) |
673 | goto nla_put_failure; | |
674 | ||
e57dce60 FH |
675 | if (ctnetlink_dump_status(skb, ct) < 0) |
676 | goto nla_put_failure; | |
677 | ||
a0891aa6 | 678 | if (events & (1 << IPCT_DESTROY)) { |
80e60e67 PNA |
679 | if (ctnetlink_dump_counters(skb, ct, |
680 | IP_CT_DIR_ORIGINAL, type) < 0 || | |
681 | ctnetlink_dump_counters(skb, ct, | |
682 | IP_CT_DIR_REPLY, type) < 0 || | |
a992ca2a | 683 | ctnetlink_dump_timestamp(skb, ct) < 0) |
df6fb868 | 684 | goto nla_put_failure; |
7b621c1e | 685 | } else { |
7b621c1e | 686 | if (ctnetlink_dump_timeout(skb, ct) < 0) |
df6fb868 | 687 | goto nla_put_failure; |
7b621c1e | 688 | |
a0891aa6 | 689 | if (events & (1 << IPCT_PROTOINFO) |
7b621c1e | 690 | && ctnetlink_dump_protoinfo(skb, ct) < 0) |
df6fb868 | 691 | goto nla_put_failure; |
7b621c1e | 692 | |
a0891aa6 | 693 | if ((events & (1 << IPCT_HELPER) || nfct_help(ct)) |
7b621c1e | 694 | && ctnetlink_dump_helpinfo(skb, ct) < 0) |
df6fb868 | 695 | goto nla_put_failure; |
7b621c1e | 696 | |
ff660c80 | 697 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
a0891aa6 | 698 | if ((events & (1 << IPCT_SECMARK) || ct->secmark) |
1cc63249 | 699 | && ctnetlink_dump_secctx(skb, ct) < 0) |
37fccd85 | 700 | goto nla_put_failure; |
ff660c80 | 701 | #endif |
0ceabd83 FW |
702 | if (events & (1 << IPCT_LABEL) && |
703 | ctnetlink_dump_labels(skb, ct) < 0) | |
704 | goto nla_put_failure; | |
7b621c1e | 705 | |
a0891aa6 | 706 | if (events & (1 << IPCT_RELATED) && |
0f417ce9 PNA |
707 | ctnetlink_dump_master(skb, ct) < 0) |
708 | goto nla_put_failure; | |
709 | ||
a0891aa6 | 710 | if (events & (1 << IPCT_NATSEQADJ) && |
13eae15a PNA |
711 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
712 | goto nla_put_failure; | |
7b621c1e | 713 | } |
b9a37e0c | 714 | |
a83099a6 | 715 | #ifdef CONFIG_NF_CONNTRACK_MARK |
a0891aa6 | 716 | if ((events & (1 << IPCT_MARK) || ct->mark) |
a83099a6 EL |
717 | && ctnetlink_dump_mark(skb, ct) < 0) |
718 | goto nla_put_failure; | |
719 | #endif | |
528a3a6f | 720 | rcu_read_unlock(); |
a83099a6 | 721 | |
96bcf938 | 722 | nlmsg_end(skb, nlh); |
15e47304 | 723 | err = nfnetlink_send(skb, net, item->portid, group, item->report, |
cd8c20b6 | 724 | GFP_ATOMIC); |
dd7669a9 PNA |
725 | if (err == -ENOBUFS || err == -EAGAIN) |
726 | return -ENOBUFS; | |
727 | ||
e34d5c1a | 728 | return 0; |
c1d10adb | 729 | |
df6fb868 | 730 | nla_put_failure: |
528a3a6f | 731 | rcu_read_unlock(); |
96bcf938 | 732 | nlmsg_cancel(skb, nlh); |
528a3a6f | 733 | nlmsg_failure: |
c1d10adb | 734 | kfree_skb(skb); |
150ace0d | 735 | errout: |
37b7ef72 PNA |
736 | if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0) |
737 | return -ENOBUFS; | |
738 | ||
e34d5c1a | 739 | return 0; |
c1d10adb PNA |
740 | } |
741 | #endif /* CONFIG_NF_CONNTRACK_EVENTS */ | |
742 | ||
743 | static int ctnetlink_done(struct netlink_callback *cb) | |
744 | { | |
89f2e218 PM |
745 | if (cb->args[1]) |
746 | nf_ct_put((struct nf_conn *)cb->args[1]); | |
0f298a28 PNA |
747 | if (cb->data) |
748 | kfree(cb->data); | |
c1d10adb PNA |
749 | return 0; |
750 | } | |
751 | ||
0f298a28 PNA |
752 | struct ctnetlink_dump_filter { |
753 | struct { | |
754 | u_int32_t val; | |
755 | u_int32_t mask; | |
756 | } mark; | |
757 | }; | |
758 | ||
c1d10adb PNA |
759 | static int |
760 | ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
761 | { | |
9592a5c0 | 762 | struct net *net = sock_net(skb->sk); |
89f2e218 | 763 | struct nf_conn *ct, *last; |
c1d10adb | 764 | struct nf_conntrack_tuple_hash *h; |
ea781f19 | 765 | struct hlist_nulls_node *n; |
96bcf938 | 766 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
87711cb8 | 767 | u_int8_t l3proto = nfmsg->nfgen_family; |
3b988ece | 768 | int res; |
0f298a28 PNA |
769 | #ifdef CONFIG_NF_CONNTRACK_MARK |
770 | const struct ctnetlink_dump_filter *filter = cb->data; | |
771 | #endif | |
3b988ece | 772 | |
13ee6ac5 | 773 | spin_lock_bh(&nf_conntrack_lock); |
d205dc40 | 774 | last = (struct nf_conn *)cb->args[1]; |
9ab99d5a | 775 | for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) { |
89f2e218 | 776 | restart: |
13ee6ac5 | 777 | hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]], |
ea781f19 | 778 | hnnode) { |
5b1158e9 | 779 | if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
c1d10adb PNA |
780 | continue; |
781 | ct = nf_ct_tuplehash_to_ctrack(h); | |
87711cb8 PNA |
782 | /* Dump entries of a given L3 protocol number. |
783 | * If it is not specified, ie. l3proto == 0, | |
784 | * then dump everything. */ | |
5e8fbe2a | 785 | if (l3proto && nf_ct_l3num(ct) != l3proto) |
13ee6ac5 | 786 | continue; |
d205dc40 PM |
787 | if (cb->args[1]) { |
788 | if (ct != last) | |
13ee6ac5 | 789 | continue; |
d205dc40 | 790 | cb->args[1] = 0; |
89f2e218 | 791 | } |
0f298a28 PNA |
792 | #ifdef CONFIG_NF_CONNTRACK_MARK |
793 | if (filter && !((ct->mark & filter->mark.mask) == | |
794 | filter->mark.val)) { | |
795 | continue; | |
796 | } | |
797 | #endif | |
3b988ece HS |
798 | rcu_read_lock(); |
799 | res = | |
15e47304 | 800 | ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid, |
3b988ece HS |
801 | cb->nlh->nlmsg_seq, |
802 | NFNL_MSG_TYPE(cb->nlh->nlmsg_type), | |
803 | ct); | |
804 | rcu_read_unlock(); | |
805 | if (res < 0) { | |
c71caf41 | 806 | nf_conntrack_get(&ct->ct_general); |
89f2e218 | 807 | cb->args[1] = (unsigned long)ct; |
c1d10adb | 808 | goto out; |
89f2e218 PM |
809 | } |
810 | } | |
d205dc40 | 811 | if (cb->args[1]) { |
89f2e218 PM |
812 | cb->args[1] = 0; |
813 | goto restart; | |
c1d10adb PNA |
814 | } |
815 | } | |
89f2e218 | 816 | out: |
13ee6ac5 | 817 | spin_unlock_bh(&nf_conntrack_lock); |
d205dc40 PM |
818 | if (last) |
819 | nf_ct_put(last); | |
c1d10adb | 820 | |
c1d10adb PNA |
821 | return skb->len; |
822 | } | |
823 | ||
c1d10adb | 824 | static inline int |
df6fb868 | 825 | ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple) |
c1d10adb | 826 | { |
df6fb868 | 827 | struct nlattr *tb[CTA_IP_MAX+1]; |
c1d10adb PNA |
828 | struct nf_conntrack_l3proto *l3proto; |
829 | int ret = 0; | |
830 | ||
df6fb868 | 831 | nla_parse_nested(tb, CTA_IP_MAX, attr, NULL); |
c1d10adb | 832 | |
cd91566e FW |
833 | rcu_read_lock(); |
834 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); | |
c1d10adb | 835 | |
f73e924c PM |
836 | if (likely(l3proto->nlattr_to_tuple)) { |
837 | ret = nla_validate_nested(attr, CTA_IP_MAX, | |
838 | l3proto->nla_policy); | |
839 | if (ret == 0) | |
840 | ret = l3proto->nlattr_to_tuple(tb, tuple); | |
841 | } | |
c1d10adb | 842 | |
cd91566e | 843 | rcu_read_unlock(); |
c1d10adb | 844 | |
c1d10adb PNA |
845 | return ret; |
846 | } | |
847 | ||
f73e924c PM |
848 | static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = { |
849 | [CTA_PROTO_NUM] = { .type = NLA_U8 }, | |
c1d10adb PNA |
850 | }; |
851 | ||
852 | static inline int | |
df6fb868 | 853 | ctnetlink_parse_tuple_proto(struct nlattr *attr, |
c1d10adb PNA |
854 | struct nf_conntrack_tuple *tuple) |
855 | { | |
df6fb868 | 856 | struct nlattr *tb[CTA_PROTO_MAX+1]; |
605dcad6 | 857 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
858 | int ret = 0; |
859 | ||
f73e924c PM |
860 | ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy); |
861 | if (ret < 0) | |
862 | return ret; | |
c1d10adb | 863 | |
df6fb868 | 864 | if (!tb[CTA_PROTO_NUM]) |
c1d10adb | 865 | return -EINVAL; |
77236b6e | 866 | tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]); |
c1d10adb | 867 | |
cd91566e FW |
868 | rcu_read_lock(); |
869 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); | |
c1d10adb | 870 | |
f73e924c PM |
871 | if (likely(l4proto->nlattr_to_tuple)) { |
872 | ret = nla_validate_nested(attr, CTA_PROTO_MAX, | |
873 | l4proto->nla_policy); | |
874 | if (ret == 0) | |
875 | ret = l4proto->nlattr_to_tuple(tb, tuple); | |
876 | } | |
c1d10adb | 877 | |
cd91566e | 878 | rcu_read_unlock(); |
601e68e1 | 879 | |
c1d10adb PNA |
880 | return ret; |
881 | } | |
882 | ||
d0b0268f PM |
883 | static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = { |
884 | [CTA_TUPLE_IP] = { .type = NLA_NESTED }, | |
885 | [CTA_TUPLE_PROTO] = { .type = NLA_NESTED }, | |
886 | }; | |
887 | ||
bb5cf80e | 888 | static int |
39938324 PM |
889 | ctnetlink_parse_tuple(const struct nlattr * const cda[], |
890 | struct nf_conntrack_tuple *tuple, | |
a00f1f36 | 891 | enum ctattr_type type, u_int8_t l3num) |
c1d10adb | 892 | { |
df6fb868 | 893 | struct nlattr *tb[CTA_TUPLE_MAX+1]; |
c1d10adb PNA |
894 | int err; |
895 | ||
c1d10adb PNA |
896 | memset(tuple, 0, sizeof(*tuple)); |
897 | ||
d0b0268f | 898 | nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy); |
c1d10adb | 899 | |
df6fb868 | 900 | if (!tb[CTA_TUPLE_IP]) |
c1d10adb PNA |
901 | return -EINVAL; |
902 | ||
903 | tuple->src.l3num = l3num; | |
904 | ||
df6fb868 | 905 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple); |
c1d10adb PNA |
906 | if (err < 0) |
907 | return err; | |
908 | ||
df6fb868 | 909 | if (!tb[CTA_TUPLE_PROTO]) |
c1d10adb PNA |
910 | return -EINVAL; |
911 | ||
df6fb868 | 912 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple); |
c1d10adb PNA |
913 | if (err < 0) |
914 | return err; | |
915 | ||
916 | /* orig and expect tuples get DIR_ORIGINAL */ | |
917 | if (type == CTA_TUPLE_REPLY) | |
918 | tuple->dst.dir = IP_CT_DIR_REPLY; | |
919 | else | |
920 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
921 | ||
c1d10adb PNA |
922 | return 0; |
923 | } | |
924 | ||
ef00f89f PM |
925 | static int |
926 | ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone) | |
927 | { | |
928 | if (attr) | |
929 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
930 | *zone = ntohs(nla_get_be16(attr)); | |
931 | #else | |
932 | return -EOPNOTSUPP; | |
933 | #endif | |
934 | else | |
935 | *zone = 0; | |
936 | ||
937 | return 0; | |
938 | } | |
939 | ||
d0b0268f | 940 | static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = { |
6d1fafca FW |
941 | [CTA_HELP_NAME] = { .type = NLA_NUL_STRING, |
942 | .len = NF_CT_HELPER_NAME_LEN - 1 }, | |
d0b0268f PM |
943 | }; |
944 | ||
c1d10adb | 945 | static inline int |
ae243bee PNA |
946 | ctnetlink_parse_help(const struct nlattr *attr, char **helper_name, |
947 | struct nlattr **helpinfo) | |
c1d10adb | 948 | { |
df6fb868 | 949 | struct nlattr *tb[CTA_HELP_MAX+1]; |
c1d10adb | 950 | |
d0b0268f | 951 | nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy); |
c1d10adb | 952 | |
df6fb868 | 953 | if (!tb[CTA_HELP_NAME]) |
c1d10adb PNA |
954 | return -EINVAL; |
955 | ||
df6fb868 | 956 | *helper_name = nla_data(tb[CTA_HELP_NAME]); |
c1d10adb | 957 | |
ae243bee PNA |
958 | if (tb[CTA_HELP_INFO]) |
959 | *helpinfo = tb[CTA_HELP_INFO]; | |
960 | ||
c1d10adb PNA |
961 | return 0; |
962 | } | |
963 | ||
9b21f6a9 | 964 | #define __CTA_LABELS_MAX_LENGTH ((XT_CONNLABEL_MAXBIT + 1) / BITS_PER_BYTE) |
f73e924c | 965 | static const struct nla_policy ct_nla_policy[CTA_MAX+1] = { |
d0b0268f PM |
966 | [CTA_TUPLE_ORIG] = { .type = NLA_NESTED }, |
967 | [CTA_TUPLE_REPLY] = { .type = NLA_NESTED }, | |
f73e924c | 968 | [CTA_STATUS] = { .type = NLA_U32 }, |
d0b0268f PM |
969 | [CTA_PROTOINFO] = { .type = NLA_NESTED }, |
970 | [CTA_HELP] = { .type = NLA_NESTED }, | |
971 | [CTA_NAT_SRC] = { .type = NLA_NESTED }, | |
f73e924c PM |
972 | [CTA_TIMEOUT] = { .type = NLA_U32 }, |
973 | [CTA_MARK] = { .type = NLA_U32 }, | |
f73e924c | 974 | [CTA_ID] = { .type = NLA_U32 }, |
d0b0268f PM |
975 | [CTA_NAT_DST] = { .type = NLA_NESTED }, |
976 | [CTA_TUPLE_MASTER] = { .type = NLA_NESTED }, | |
6d1fafca FW |
977 | [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED }, |
978 | [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED }, | |
ef00f89f | 979 | [CTA_ZONE] = { .type = NLA_U16 }, |
0f298a28 | 980 | [CTA_MARK_MASK] = { .type = NLA_U32 }, |
9b21f6a9 FW |
981 | [CTA_LABELS] = { .type = NLA_BINARY, |
982 | .len = __CTA_LABELS_MAX_LENGTH }, | |
983 | [CTA_LABELS_MASK] = { .type = NLA_BINARY, | |
984 | .len = __CTA_LABELS_MAX_LENGTH }, | |
c1d10adb PNA |
985 | }; |
986 | ||
987 | static int | |
601e68e1 | 988 | ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
989 | const struct nlmsghdr *nlh, |
990 | const struct nlattr * const cda[]) | |
c1d10adb | 991 | { |
9592a5c0 | 992 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
993 | struct nf_conntrack_tuple_hash *h; |
994 | struct nf_conntrack_tuple tuple; | |
995 | struct nf_conn *ct; | |
96bcf938 | 996 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 997 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
998 | u16 zone; |
999 | int err; | |
1000 | ||
1001 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); | |
1002 | if (err < 0) | |
1003 | return err; | |
c1d10adb | 1004 | |
df6fb868 | 1005 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 1006 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 1007 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
1008 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
1009 | else { | |
1010 | /* Flush the whole table */ | |
9592a5c0 | 1011 | nf_conntrack_flush_report(net, |
15e47304 | 1012 | NETLINK_CB(skb).portid, |
274d383b | 1013 | nlmsg_report(nlh)); |
c1d10adb PNA |
1014 | return 0; |
1015 | } | |
1016 | ||
1017 | if (err < 0) | |
1018 | return err; | |
1019 | ||
ef00f89f | 1020 | h = nf_conntrack_find_get(net, zone, &tuple); |
9ea8cfd6 | 1021 | if (!h) |
c1d10adb | 1022 | return -ENOENT; |
c1d10adb PNA |
1023 | |
1024 | ct = nf_ct_tuplehash_to_ctrack(h); | |
601e68e1 | 1025 | |
df6fb868 | 1026 | if (cda[CTA_ID]) { |
77236b6e | 1027 | u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID])); |
7f85f914 | 1028 | if (id != (u32)(unsigned long)ct) { |
c1d10adb PNA |
1029 | nf_ct_put(ct); |
1030 | return -ENOENT; | |
1031 | } | |
601e68e1 | 1032 | } |
c1d10adb | 1033 | |
a16a1647 PNA |
1034 | if (del_timer(&ct->timeout)) { |
1035 | if (nf_conntrack_event_report(IPCT_DESTROY, ct, | |
15e47304 | 1036 | NETLINK_CB(skb).portid, |
a16a1647 PNA |
1037 | nlmsg_report(nlh)) < 0) { |
1038 | nf_ct_delete_from_lists(ct); | |
1039 | /* we failed to report the event, try later */ | |
04dac011 | 1040 | nf_ct_dying_timeout(ct); |
a16a1647 PNA |
1041 | nf_ct_put(ct); |
1042 | return 0; | |
1043 | } | |
1044 | /* death_by_timeout would report the event again */ | |
1045 | set_bit(IPS_DYING_BIT, &ct->status); | |
dd7669a9 | 1046 | nf_ct_delete_from_lists(ct); |
dd7669a9 | 1047 | nf_ct_put(ct); |
dd7669a9 | 1048 | } |
c1d10adb | 1049 | nf_ct_put(ct); |
c1d10adb PNA |
1050 | |
1051 | return 0; | |
1052 | } | |
1053 | ||
1054 | static int | |
601e68e1 | 1055 | ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
1056 | const struct nlmsghdr *nlh, |
1057 | const struct nlattr * const cda[]) | |
c1d10adb | 1058 | { |
9592a5c0 | 1059 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1060 | struct nf_conntrack_tuple_hash *h; |
1061 | struct nf_conntrack_tuple tuple; | |
1062 | struct nf_conn *ct; | |
1063 | struct sk_buff *skb2 = NULL; | |
96bcf938 | 1064 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 1065 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
1066 | u16 zone; |
1067 | int err; | |
c1d10adb | 1068 | |
80d326fa PNA |
1069 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
1070 | struct netlink_dump_control c = { | |
1071 | .dump = ctnetlink_dump_table, | |
1072 | .done = ctnetlink_done, | |
1073 | }; | |
0f298a28 PNA |
1074 | #ifdef CONFIG_NF_CONNTRACK_MARK |
1075 | if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) { | |
1076 | struct ctnetlink_dump_filter *filter; | |
1077 | ||
1078 | filter = kzalloc(sizeof(struct ctnetlink_dump_filter), | |
1079 | GFP_ATOMIC); | |
1080 | if (filter == NULL) | |
1081 | return -ENOMEM; | |
1082 | ||
1083 | filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK])); | |
1084 | filter->mark.mask = | |
1085 | ntohl(nla_get_be32(cda[CTA_MARK_MASK])); | |
1086 | c.data = filter; | |
1087 | } | |
1088 | #endif | |
80d326fa PNA |
1089 | return netlink_dump_start(ctnl, skb, nlh, &c); |
1090 | } | |
c1d10adb | 1091 | |
ef00f89f PM |
1092 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); |
1093 | if (err < 0) | |
1094 | return err; | |
1095 | ||
df6fb868 | 1096 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 1097 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 1098 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
1099 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
1100 | else | |
1101 | return -EINVAL; | |
1102 | ||
1103 | if (err < 0) | |
1104 | return err; | |
1105 | ||
ef00f89f | 1106 | h = nf_conntrack_find_get(net, zone, &tuple); |
9ea8cfd6 | 1107 | if (!h) |
c1d10adb | 1108 | return -ENOENT; |
9ea8cfd6 | 1109 | |
c1d10adb PNA |
1110 | ct = nf_ct_tuplehash_to_ctrack(h); |
1111 | ||
1112 | err = -ENOMEM; | |
96bcf938 PNA |
1113 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1114 | if (skb2 == NULL) { | |
c1d10adb PNA |
1115 | nf_ct_put(ct); |
1116 | return -ENOMEM; | |
1117 | } | |
c1d10adb | 1118 | |
528a3a6f | 1119 | rcu_read_lock(); |
15e47304 | 1120 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, |
80e60e67 | 1121 | NFNL_MSG_TYPE(nlh->nlmsg_type), ct); |
528a3a6f | 1122 | rcu_read_unlock(); |
c1d10adb PNA |
1123 | nf_ct_put(ct); |
1124 | if (err <= 0) | |
1125 | goto free; | |
1126 | ||
15e47304 | 1127 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT); |
c1d10adb PNA |
1128 | if (err < 0) |
1129 | goto out; | |
1130 | ||
c1d10adb PNA |
1131 | return 0; |
1132 | ||
1133 | free: | |
1134 | kfree_skb(skb2); | |
1135 | out: | |
f31e8d49 PNA |
1136 | /* this avoids a loop in nfnetlink. */ |
1137 | return err == -EAGAIN ? -ENOBUFS : err; | |
c1d10adb PNA |
1138 | } |
1139 | ||
d871befe PNA |
1140 | static int ctnetlink_done_list(struct netlink_callback *cb) |
1141 | { | |
1142 | if (cb->args[1]) | |
1143 | nf_ct_put((struct nf_conn *)cb->args[1]); | |
1144 | return 0; | |
1145 | } | |
1146 | ||
1147 | static int | |
1148 | ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, | |
1149 | struct hlist_nulls_head *list) | |
1150 | { | |
1151 | struct nf_conn *ct, *last; | |
1152 | struct nf_conntrack_tuple_hash *h; | |
1153 | struct hlist_nulls_node *n; | |
1154 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
1155 | u_int8_t l3proto = nfmsg->nfgen_family; | |
1156 | int res; | |
1157 | ||
1158 | if (cb->args[2]) | |
1159 | return 0; | |
1160 | ||
1161 | spin_lock_bh(&nf_conntrack_lock); | |
1162 | last = (struct nf_conn *)cb->args[1]; | |
1163 | restart: | |
1164 | hlist_nulls_for_each_entry(h, n, list, hnnode) { | |
1165 | ct = nf_ct_tuplehash_to_ctrack(h); | |
1166 | if (l3proto && nf_ct_l3num(ct) != l3proto) | |
1167 | continue; | |
1168 | if (cb->args[1]) { | |
1169 | if (ct != last) | |
1170 | continue; | |
1171 | cb->args[1] = 0; | |
1172 | } | |
1173 | rcu_read_lock(); | |
1174 | res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid, | |
1175 | cb->nlh->nlmsg_seq, | |
1176 | NFNL_MSG_TYPE(cb->nlh->nlmsg_type), | |
1177 | ct); | |
1178 | rcu_read_unlock(); | |
1179 | if (res < 0) { | |
1180 | nf_conntrack_get(&ct->ct_general); | |
1181 | cb->args[1] = (unsigned long)ct; | |
1182 | goto out; | |
1183 | } | |
1184 | } | |
1185 | if (cb->args[1]) { | |
1186 | cb->args[1] = 0; | |
1187 | goto restart; | |
1188 | } else | |
1189 | cb->args[2] = 1; | |
1190 | out: | |
1191 | spin_unlock_bh(&nf_conntrack_lock); | |
1192 | if (last) | |
1193 | nf_ct_put(last); | |
1194 | ||
1195 | return skb->len; | |
1196 | } | |
1197 | ||
1198 | static int | |
1199 | ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb) | |
1200 | { | |
1201 | struct net *net = sock_net(skb->sk); | |
1202 | ||
1203 | return ctnetlink_dump_list(skb, cb, &net->ct.dying); | |
1204 | } | |
1205 | ||
1206 | static int | |
1207 | ctnetlink_get_ct_dying(struct sock *ctnl, struct sk_buff *skb, | |
1208 | const struct nlmsghdr *nlh, | |
1209 | const struct nlattr * const cda[]) | |
1210 | { | |
1211 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1212 | struct netlink_dump_control c = { | |
1213 | .dump = ctnetlink_dump_dying, | |
1214 | .done = ctnetlink_done_list, | |
1215 | }; | |
1216 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
1217 | } | |
1218 | ||
1219 | return -EOPNOTSUPP; | |
1220 | } | |
1221 | ||
1222 | static int | |
1223 | ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb) | |
1224 | { | |
1225 | struct net *net = sock_net(skb->sk); | |
1226 | ||
1227 | return ctnetlink_dump_list(skb, cb, &net->ct.unconfirmed); | |
1228 | } | |
1229 | ||
1230 | static int | |
1231 | ctnetlink_get_ct_unconfirmed(struct sock *ctnl, struct sk_buff *skb, | |
1232 | const struct nlmsghdr *nlh, | |
1233 | const struct nlattr * const cda[]) | |
1234 | { | |
1235 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1236 | struct netlink_dump_control c = { | |
1237 | .dump = ctnetlink_dump_unconfirmed, | |
1238 | .done = ctnetlink_done_list, | |
1239 | }; | |
1240 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
1241 | } | |
1242 | ||
1243 | return -EOPNOTSUPP; | |
1244 | } | |
1245 | ||
67671841 | 1246 | #ifdef CONFIG_NF_NAT_NEEDED |
e6a7d3c0 PNA |
1247 | static int |
1248 | ctnetlink_parse_nat_setup(struct nf_conn *ct, | |
1249 | enum nf_nat_manip_type manip, | |
39938324 | 1250 | const struct nlattr *attr) |
e6a7d3c0 PNA |
1251 | { |
1252 | typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup; | |
c7232c99 | 1253 | int err; |
e6a7d3c0 PNA |
1254 | |
1255 | parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook); | |
1256 | if (!parse_nat_setup) { | |
95a5afca | 1257 | #ifdef CONFIG_MODULES |
e6a7d3c0 PNA |
1258 | rcu_read_unlock(); |
1259 | nfnl_unlock(); | |
c7232c99 | 1260 | if (request_module("nf-nat") < 0) { |
e6a7d3c0 PNA |
1261 | nfnl_lock(); |
1262 | rcu_read_lock(); | |
1263 | return -EOPNOTSUPP; | |
1264 | } | |
1265 | nfnl_lock(); | |
1266 | rcu_read_lock(); | |
1267 | if (nfnetlink_parse_nat_setup_hook) | |
1268 | return -EAGAIN; | |
1269 | #endif | |
1270 | return -EOPNOTSUPP; | |
1271 | } | |
1272 | ||
c7232c99 PM |
1273 | err = parse_nat_setup(ct, manip, attr); |
1274 | if (err == -EAGAIN) { | |
1275 | #ifdef CONFIG_MODULES | |
1276 | rcu_read_unlock(); | |
c7232c99 PM |
1277 | nfnl_unlock(); |
1278 | if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) { | |
1279 | nfnl_lock(); | |
c7232c99 PM |
1280 | rcu_read_lock(); |
1281 | return -EOPNOTSUPP; | |
1282 | } | |
1283 | nfnl_lock(); | |
c7232c99 PM |
1284 | rcu_read_lock(); |
1285 | #else | |
1286 | err = -EOPNOTSUPP; | |
1287 | #endif | |
1288 | } | |
1289 | return err; | |
e6a7d3c0 | 1290 | } |
67671841 | 1291 | #endif |
e6a7d3c0 | 1292 | |
bb5cf80e | 1293 | static int |
39938324 | 1294 | ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
1295 | { |
1296 | unsigned long d; | |
77236b6e | 1297 | unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS])); |
c1d10adb PNA |
1298 | d = ct->status ^ status; |
1299 | ||
1300 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) | |
1301 | /* unchangeable */ | |
0adf9d67 | 1302 | return -EBUSY; |
601e68e1 | 1303 | |
c1d10adb PNA |
1304 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) |
1305 | /* SEEN_REPLY bit can only be set */ | |
0adf9d67 | 1306 | return -EBUSY; |
601e68e1 | 1307 | |
c1d10adb PNA |
1308 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) |
1309 | /* ASSURED bit can only be set */ | |
0adf9d67 | 1310 | return -EBUSY; |
c1d10adb | 1311 | |
c1d10adb PNA |
1312 | /* Be careful here, modifying NAT bits can screw up things, |
1313 | * so don't let users modify them directly if they don't pass | |
5b1158e9 | 1314 | * nf_nat_range. */ |
c1d10adb PNA |
1315 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); |
1316 | return 0; | |
1317 | } | |
1318 | ||
e6a7d3c0 | 1319 | static int |
39938324 | 1320 | ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[]) |
e6a7d3c0 PNA |
1321 | { |
1322 | #ifdef CONFIG_NF_NAT_NEEDED | |
1323 | int ret; | |
1324 | ||
1325 | if (cda[CTA_NAT_DST]) { | |
1326 | ret = ctnetlink_parse_nat_setup(ct, | |
cbc9f2f4 | 1327 | NF_NAT_MANIP_DST, |
e6a7d3c0 PNA |
1328 | cda[CTA_NAT_DST]); |
1329 | if (ret < 0) | |
1330 | return ret; | |
1331 | } | |
1332 | if (cda[CTA_NAT_SRC]) { | |
1333 | ret = ctnetlink_parse_nat_setup(ct, | |
cbc9f2f4 | 1334 | NF_NAT_MANIP_SRC, |
e6a7d3c0 PNA |
1335 | cda[CTA_NAT_SRC]); |
1336 | if (ret < 0) | |
1337 | return ret; | |
1338 | } | |
1339 | return 0; | |
1340 | #else | |
1341 | return -EOPNOTSUPP; | |
1342 | #endif | |
1343 | } | |
c1d10adb PNA |
1344 | |
1345 | static inline int | |
39938324 | 1346 | ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
1347 | { |
1348 | struct nf_conntrack_helper *helper; | |
dc808fe2 | 1349 | struct nf_conn_help *help = nfct_help(ct); |
29fe1b48 | 1350 | char *helpname = NULL; |
ae243bee | 1351 | struct nlattr *helpinfo = NULL; |
c1d10adb PNA |
1352 | int err; |
1353 | ||
c1d10adb PNA |
1354 | /* don't change helper of sibling connections */ |
1355 | if (ct->master) | |
0adf9d67 | 1356 | return -EBUSY; |
c1d10adb | 1357 | |
ae243bee | 1358 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo); |
c1d10adb PNA |
1359 | if (err < 0) |
1360 | return err; | |
1361 | ||
df293bbb YK |
1362 | if (!strcmp(helpname, "")) { |
1363 | if (help && help->helper) { | |
c1d10adb PNA |
1364 | /* we had a helper before ... */ |
1365 | nf_ct_remove_expectations(ct); | |
a9b3cd7f | 1366 | RCU_INIT_POINTER(help->helper, NULL); |
c1d10adb | 1367 | } |
df293bbb YK |
1368 | |
1369 | return 0; | |
c1d10adb | 1370 | } |
601e68e1 | 1371 | |
794e6871 PM |
1372 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1373 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1374 | if (helper == NULL) { |
1375 | #ifdef CONFIG_MODULES | |
1376 | spin_unlock_bh(&nf_conntrack_lock); | |
1377 | ||
1378 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1379 | spin_lock_bh(&nf_conntrack_lock); | |
1380 | return -EOPNOTSUPP; | |
1381 | } | |
1382 | ||
1383 | spin_lock_bh(&nf_conntrack_lock); | |
794e6871 PM |
1384 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1385 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1386 | if (helper) |
1387 | return -EAGAIN; | |
1388 | #endif | |
0adf9d67 | 1389 | return -EOPNOTSUPP; |
226c0c0e | 1390 | } |
df293bbb | 1391 | |
ceceae1b | 1392 | if (help) { |
ae243bee PNA |
1393 | if (help->helper == helper) { |
1394 | /* update private helper data if allowed. */ | |
7be54ca4 | 1395 | if (helper->from_nlattr) |
ae243bee | 1396 | helper->from_nlattr(helpinfo, ct); |
ceceae1b | 1397 | return 0; |
fd7462de | 1398 | } else |
ceceae1b | 1399 | return -EBUSY; |
ceceae1b | 1400 | } |
df293bbb | 1401 | |
fd7462de PNA |
1402 | /* we cannot set a helper for an existing conntrack */ |
1403 | return -EOPNOTSUPP; | |
c1d10adb PNA |
1404 | } |
1405 | ||
1406 | static inline int | |
39938324 | 1407 | ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb | 1408 | { |
77236b6e | 1409 | u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
601e68e1 | 1410 | |
c1d10adb PNA |
1411 | if (!del_timer(&ct->timeout)) |
1412 | return -ETIME; | |
1413 | ||
1414 | ct->timeout.expires = jiffies + timeout * HZ; | |
1415 | add_timer(&ct->timeout); | |
1416 | ||
1417 | return 0; | |
1418 | } | |
1419 | ||
d0b0268f PM |
1420 | static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = { |
1421 | [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED }, | |
1422 | [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED }, | |
1423 | [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED }, | |
1424 | }; | |
1425 | ||
c1d10adb | 1426 | static inline int |
39938324 | 1427 | ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb | 1428 | { |
39938324 PM |
1429 | const struct nlattr *attr = cda[CTA_PROTOINFO]; |
1430 | struct nlattr *tb[CTA_PROTOINFO_MAX+1]; | |
605dcad6 | 1431 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
1432 | int err = 0; |
1433 | ||
d0b0268f | 1434 | nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy); |
c1d10adb | 1435 | |
cd91566e FW |
1436 | rcu_read_lock(); |
1437 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
fdf70832 PM |
1438 | if (l4proto->from_nlattr) |
1439 | err = l4proto->from_nlattr(tb, ct); | |
cd91566e | 1440 | rcu_read_unlock(); |
c1d10adb PNA |
1441 | |
1442 | return err; | |
1443 | } | |
1444 | ||
13eae15a | 1445 | #ifdef CONFIG_NF_NAT_NEEDED |
d0b0268f PM |
1446 | static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = { |
1447 | [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 }, | |
1448 | [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 }, | |
1449 | [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 }, | |
1450 | }; | |
1451 | ||
13eae15a | 1452 | static inline int |
39938324 | 1453 | change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr) |
13eae15a PNA |
1454 | { |
1455 | struct nlattr *cda[CTA_NAT_SEQ_MAX+1]; | |
1456 | ||
d0b0268f | 1457 | nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy); |
13eae15a PNA |
1458 | |
1459 | if (!cda[CTA_NAT_SEQ_CORRECTION_POS]) | |
1460 | return -EINVAL; | |
1461 | ||
1462 | natseq->correction_pos = | |
77236b6e | 1463 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS])); |
13eae15a PNA |
1464 | |
1465 | if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE]) | |
1466 | return -EINVAL; | |
1467 | ||
1468 | natseq->offset_before = | |
77236b6e | 1469 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE])); |
13eae15a PNA |
1470 | |
1471 | if (!cda[CTA_NAT_SEQ_OFFSET_AFTER]) | |
1472 | return -EINVAL; | |
1473 | ||
1474 | natseq->offset_after = | |
77236b6e | 1475 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER])); |
13eae15a PNA |
1476 | |
1477 | return 0; | |
1478 | } | |
1479 | ||
1480 | static int | |
39938324 PM |
1481 | ctnetlink_change_nat_seq_adj(struct nf_conn *ct, |
1482 | const struct nlattr * const cda[]) | |
13eae15a PNA |
1483 | { |
1484 | int ret = 0; | |
1485 | struct nf_conn_nat *nat = nfct_nat(ct); | |
1486 | ||
1487 | if (!nat) | |
1488 | return 0; | |
1489 | ||
1490 | if (cda[CTA_NAT_SEQ_ADJ_ORIG]) { | |
1491 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL], | |
1492 | cda[CTA_NAT_SEQ_ADJ_ORIG]); | |
1493 | if (ret < 0) | |
1494 | return ret; | |
1495 | ||
1496 | ct->status |= IPS_SEQ_ADJUST; | |
1497 | } | |
1498 | ||
1499 | if (cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1500 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY], | |
1501 | cda[CTA_NAT_SEQ_ADJ_REPLY]); | |
1502 | if (ret < 0) | |
1503 | return ret; | |
1504 | ||
1505 | ct->status |= IPS_SEQ_ADJUST; | |
1506 | } | |
1507 | ||
1508 | return 0; | |
1509 | } | |
1510 | #endif | |
1511 | ||
9b21f6a9 FW |
1512 | static int |
1513 | ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[]) | |
1514 | { | |
1515 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
1516 | size_t len = nla_len(cda[CTA_LABELS]); | |
1517 | const void *mask = cda[CTA_LABELS_MASK]; | |
1518 | ||
1519 | if (len & (sizeof(u32)-1)) /* must be multiple of u32 */ | |
1520 | return -EINVAL; | |
1521 | ||
1522 | if (mask) { | |
1523 | if (nla_len(cda[CTA_LABELS_MASK]) == 0 || | |
1524 | nla_len(cda[CTA_LABELS_MASK]) != len) | |
1525 | return -EINVAL; | |
1526 | mask = nla_data(cda[CTA_LABELS_MASK]); | |
1527 | } | |
1528 | ||
1529 | len /= sizeof(u32); | |
1530 | ||
1531 | return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len); | |
1532 | #else | |
1533 | return -EOPNOTSUPP; | |
1534 | #endif | |
1535 | } | |
1536 | ||
c1d10adb | 1537 | static int |
39938324 PM |
1538 | ctnetlink_change_conntrack(struct nf_conn *ct, |
1539 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1540 | { |
1541 | int err; | |
1542 | ||
e098360f PNA |
1543 | /* only allow NAT changes and master assignation for new conntracks */ |
1544 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER]) | |
1545 | return -EOPNOTSUPP; | |
1546 | ||
df6fb868 | 1547 | if (cda[CTA_HELP]) { |
c1d10adb PNA |
1548 | err = ctnetlink_change_helper(ct, cda); |
1549 | if (err < 0) | |
1550 | return err; | |
1551 | } | |
1552 | ||
df6fb868 | 1553 | if (cda[CTA_TIMEOUT]) { |
c1d10adb PNA |
1554 | err = ctnetlink_change_timeout(ct, cda); |
1555 | if (err < 0) | |
1556 | return err; | |
1557 | } | |
1558 | ||
df6fb868 | 1559 | if (cda[CTA_STATUS]) { |
c1d10adb PNA |
1560 | err = ctnetlink_change_status(ct, cda); |
1561 | if (err < 0) | |
1562 | return err; | |
1563 | } | |
1564 | ||
df6fb868 | 1565 | if (cda[CTA_PROTOINFO]) { |
c1d10adb PNA |
1566 | err = ctnetlink_change_protoinfo(ct, cda); |
1567 | if (err < 0) | |
1568 | return err; | |
1569 | } | |
1570 | ||
bcd1e830 | 1571 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1572 | if (cda[CTA_MARK]) |
77236b6e | 1573 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1574 | #endif |
1575 | ||
13eae15a PNA |
1576 | #ifdef CONFIG_NF_NAT_NEEDED |
1577 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1578 | err = ctnetlink_change_nat_seq_adj(ct, cda); | |
1579 | if (err < 0) | |
1580 | return err; | |
1581 | } | |
1582 | #endif | |
9b21f6a9 FW |
1583 | if (cda[CTA_LABELS]) { |
1584 | err = ctnetlink_attach_labels(ct, cda); | |
1585 | if (err < 0) | |
1586 | return err; | |
1587 | } | |
13eae15a | 1588 | |
c1d10adb PNA |
1589 | return 0; |
1590 | } | |
1591 | ||
f0a3c086 | 1592 | static struct nf_conn * |
ef00f89f | 1593 | ctnetlink_create_conntrack(struct net *net, u16 zone, |
9592a5c0 | 1594 | const struct nlattr * const cda[], |
c1d10adb | 1595 | struct nf_conntrack_tuple *otuple, |
5faa1f4c | 1596 | struct nf_conntrack_tuple *rtuple, |
7ec47496 | 1597 | u8 u3) |
c1d10adb PNA |
1598 | { |
1599 | struct nf_conn *ct; | |
1600 | int err = -EINVAL; | |
ceceae1b | 1601 | struct nf_conntrack_helper *helper; |
315c34da | 1602 | struct nf_conn_tstamp *tstamp; |
c1d10adb | 1603 | |
ef00f89f | 1604 | ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC); |
cd7fcbf1 | 1605 | if (IS_ERR(ct)) |
f0a3c086 | 1606 | return ERR_PTR(-ENOMEM); |
c1d10adb | 1607 | |
df6fb868 | 1608 | if (!cda[CTA_TIMEOUT]) |
0f5b3e85 | 1609 | goto err1; |
77236b6e | 1610 | ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
c1d10adb PNA |
1611 | |
1612 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; | |
c1d10adb | 1613 | |
1575e7ea | 1614 | rcu_read_lock(); |
226c0c0e | 1615 | if (cda[CTA_HELP]) { |
29fe1b48 | 1616 | char *helpname = NULL; |
ae243bee PNA |
1617 | struct nlattr *helpinfo = NULL; |
1618 | ||
1619 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo); | |
0f5b3e85 PM |
1620 | if (err < 0) |
1621 | goto err2; | |
226c0c0e | 1622 | |
794e6871 PM |
1623 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1624 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1625 | if (helper == NULL) { |
1626 | rcu_read_unlock(); | |
1627 | #ifdef CONFIG_MODULES | |
1628 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1629 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1630 | goto err1; |
226c0c0e PNA |
1631 | } |
1632 | ||
1633 | rcu_read_lock(); | |
794e6871 PM |
1634 | helper = __nf_conntrack_helper_find(helpname, |
1635 | nf_ct_l3num(ct), | |
1636 | nf_ct_protonum(ct)); | |
226c0c0e | 1637 | if (helper) { |
226c0c0e | 1638 | err = -EAGAIN; |
0f5b3e85 | 1639 | goto err2; |
226c0c0e PNA |
1640 | } |
1641 | rcu_read_unlock(); | |
1642 | #endif | |
1643 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1644 | goto err1; |
226c0c0e PNA |
1645 | } else { |
1646 | struct nf_conn_help *help; | |
1647 | ||
1afc5679 | 1648 | help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC); |
226c0c0e | 1649 | if (help == NULL) { |
226c0c0e | 1650 | err = -ENOMEM; |
0f5b3e85 | 1651 | goto err2; |
226c0c0e | 1652 | } |
ae243bee | 1653 | /* set private helper data if allowed. */ |
7be54ca4 | 1654 | if (helper->from_nlattr) |
ae243bee | 1655 | helper->from_nlattr(helpinfo, ct); |
226c0c0e PNA |
1656 | |
1657 | /* not in hash table yet so not strictly necessary */ | |
a9b3cd7f | 1658 | RCU_INIT_POINTER(help->helper, helper); |
226c0c0e PNA |
1659 | } |
1660 | } else { | |
1661 | /* try an implicit helper assignation */ | |
b2a15a60 | 1662 | err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC); |
0f5b3e85 PM |
1663 | if (err < 0) |
1664 | goto err2; | |
1575e7ea PNA |
1665 | } |
1666 | ||
a88e22ad PNA |
1667 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) { |
1668 | err = ctnetlink_change_nat(ct, cda); | |
0f5b3e85 PM |
1669 | if (err < 0) |
1670 | goto err2; | |
e6a7d3c0 PNA |
1671 | } |
1672 | ||
a88e22ad | 1673 | nf_ct_acct_ext_add(ct, GFP_ATOMIC); |
a992ca2a | 1674 | nf_ct_tstamp_ext_add(ct, GFP_ATOMIC); |
a88e22ad | 1675 | nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC); |
c539f017 FW |
1676 | nf_ct_labels_ext_add(ct); |
1677 | ||
a88e22ad PNA |
1678 | /* we must add conntrack extensions before confirmation. */ |
1679 | ct->status |= IPS_CONFIRMED; | |
1680 | ||
1681 | if (cda[CTA_STATUS]) { | |
1682 | err = ctnetlink_change_status(ct, cda); | |
0f5b3e85 PM |
1683 | if (err < 0) |
1684 | goto err2; | |
bbb3357d | 1685 | } |
c1d10adb | 1686 | |
c969aa7d PNA |
1687 | #ifdef CONFIG_NF_NAT_NEEDED |
1688 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1689 | err = ctnetlink_change_nat_seq_adj(ct, cda); | |
0f5b3e85 PM |
1690 | if (err < 0) |
1691 | goto err2; | |
c969aa7d PNA |
1692 | } |
1693 | #endif | |
1694 | ||
e5fc9e7a | 1695 | memset(&ct->proto, 0, sizeof(ct->proto)); |
df6fb868 | 1696 | if (cda[CTA_PROTOINFO]) { |
c1d10adb | 1697 | err = ctnetlink_change_protoinfo(ct, cda); |
0f5b3e85 PM |
1698 | if (err < 0) |
1699 | goto err2; | |
c1d10adb PNA |
1700 | } |
1701 | ||
bcd1e830 | 1702 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1703 | if (cda[CTA_MARK]) |
77236b6e | 1704 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1705 | #endif |
1706 | ||
5faa1f4c | 1707 | /* setup master conntrack: this is a confirmed expectation */ |
7ec47496 PNA |
1708 | if (cda[CTA_TUPLE_MASTER]) { |
1709 | struct nf_conntrack_tuple master; | |
1710 | struct nf_conntrack_tuple_hash *master_h; | |
1711 | struct nf_conn *master_ct; | |
1712 | ||
1713 | err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3); | |
1714 | if (err < 0) | |
0f5b3e85 | 1715 | goto err2; |
7ec47496 | 1716 | |
ef00f89f | 1717 | master_h = nf_conntrack_find_get(net, zone, &master); |
7ec47496 PNA |
1718 | if (master_h == NULL) { |
1719 | err = -ENOENT; | |
0f5b3e85 | 1720 | goto err2; |
7ec47496 PNA |
1721 | } |
1722 | master_ct = nf_ct_tuplehash_to_ctrack(master_h); | |
f2a89004 | 1723 | __set_bit(IPS_EXPECTED_BIT, &ct->status); |
5faa1f4c | 1724 | ct->master = master_ct; |
f2a89004 | 1725 | } |
315c34da PNA |
1726 | tstamp = nf_conn_tstamp_find(ct); |
1727 | if (tstamp) | |
1728 | tstamp->start = ktime_to_ns(ktime_get_real()); | |
5faa1f4c | 1729 | |
7d367e06 JK |
1730 | err = nf_conntrack_hash_check_insert(ct); |
1731 | if (err < 0) | |
1732 | goto err2; | |
1733 | ||
58a3c9bb | 1734 | rcu_read_unlock(); |
dafc741c | 1735 | |
f0a3c086 | 1736 | return ct; |
c1d10adb | 1737 | |
0f5b3e85 PM |
1738 | err2: |
1739 | rcu_read_unlock(); | |
1740 | err1: | |
c1d10adb | 1741 | nf_conntrack_free(ct); |
f0a3c086 | 1742 | return ERR_PTR(err); |
c1d10adb PNA |
1743 | } |
1744 | ||
601e68e1 YH |
1745 | static int |
1746 | ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
1747 | const struct nlmsghdr *nlh, |
1748 | const struct nlattr * const cda[]) | |
c1d10adb | 1749 | { |
9592a5c0 | 1750 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1751 | struct nf_conntrack_tuple otuple, rtuple; |
1752 | struct nf_conntrack_tuple_hash *h = NULL; | |
96bcf938 | 1753 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
7d367e06 | 1754 | struct nf_conn *ct; |
c1d10adb | 1755 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
1756 | u16 zone; |
1757 | int err; | |
1758 | ||
1759 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); | |
1760 | if (err < 0) | |
1761 | return err; | |
c1d10adb | 1762 | |
df6fb868 | 1763 | if (cda[CTA_TUPLE_ORIG]) { |
c1d10adb PNA |
1764 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3); |
1765 | if (err < 0) | |
1766 | return err; | |
1767 | } | |
1768 | ||
df6fb868 | 1769 | if (cda[CTA_TUPLE_REPLY]) { |
c1d10adb PNA |
1770 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3); |
1771 | if (err < 0) | |
1772 | return err; | |
1773 | } | |
1774 | ||
df6fb868 | 1775 | if (cda[CTA_TUPLE_ORIG]) |
7d367e06 | 1776 | h = nf_conntrack_find_get(net, zone, &otuple); |
df6fb868 | 1777 | else if (cda[CTA_TUPLE_REPLY]) |
7d367e06 | 1778 | h = nf_conntrack_find_get(net, zone, &rtuple); |
c1d10adb PNA |
1779 | |
1780 | if (h == NULL) { | |
c1d10adb | 1781 | err = -ENOENT; |
f0a3c086 | 1782 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
fecc1133 | 1783 | enum ip_conntrack_events events; |
5faa1f4c | 1784 | |
ef00f89f | 1785 | ct = ctnetlink_create_conntrack(net, zone, cda, &otuple, |
f0a3c086 | 1786 | &rtuple, u3); |
7d367e06 JK |
1787 | if (IS_ERR(ct)) |
1788 | return PTR_ERR(ct); | |
1789 | ||
f0a3c086 | 1790 | err = 0; |
fecc1133 PNA |
1791 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) |
1792 | events = IPCT_RELATED; | |
1793 | else | |
1794 | events = IPCT_NEW; | |
1795 | ||
9b21f6a9 FW |
1796 | if (cda[CTA_LABELS] && |
1797 | ctnetlink_attach_labels(ct, cda) == 0) | |
1798 | events |= (1 << IPCT_LABEL); | |
1799 | ||
858b3133 PM |
1800 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1801 | (1 << IPCT_ASSURED) | | |
a0891aa6 PNA |
1802 | (1 << IPCT_HELPER) | |
1803 | (1 << IPCT_PROTOINFO) | | |
1804 | (1 << IPCT_NATSEQADJ) | | |
1805 | (1 << IPCT_MARK) | events, | |
15e47304 | 1806 | ct, NETLINK_CB(skb).portid, |
a0891aa6 | 1807 | nlmsg_report(nlh)); |
f0a3c086 | 1808 | nf_ct_put(ct); |
7d367e06 | 1809 | } |
5faa1f4c | 1810 | |
c1d10adb PNA |
1811 | return err; |
1812 | } | |
1813 | /* implicit 'else' */ | |
1814 | ||
c1d10adb | 1815 | err = -EEXIST; |
7d367e06 | 1816 | ct = nf_ct_tuplehash_to_ctrack(h); |
ff4ca827 | 1817 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) { |
7d367e06 | 1818 | spin_lock_bh(&nf_conntrack_lock); |
19abb7b0 | 1819 | err = ctnetlink_change_conntrack(ct, cda); |
7d367e06 | 1820 | spin_unlock_bh(&nf_conntrack_lock); |
19abb7b0 | 1821 | if (err == 0) { |
858b3133 PM |
1822 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1823 | (1 << IPCT_ASSURED) | | |
a0891aa6 PNA |
1824 | (1 << IPCT_HELPER) | |
1825 | (1 << IPCT_PROTOINFO) | | |
1826 | (1 << IPCT_NATSEQADJ) | | |
1827 | (1 << IPCT_MARK), | |
15e47304 | 1828 | ct, NETLINK_CB(skb).portid, |
a0891aa6 | 1829 | nlmsg_report(nlh)); |
7d367e06 | 1830 | } |
ff4ca827 | 1831 | } |
c1d10adb | 1832 | |
7d367e06 | 1833 | nf_ct_put(ct); |
c1d10adb PNA |
1834 | return err; |
1835 | } | |
1836 | ||
392025f8 | 1837 | static int |
15e47304 | 1838 | ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq, |
392025f8 PNA |
1839 | __u16 cpu, const struct ip_conntrack_stat *st) |
1840 | { | |
1841 | struct nlmsghdr *nlh; | |
1842 | struct nfgenmsg *nfmsg; | |
15e47304 | 1843 | unsigned int flags = portid ? NLM_F_MULTI : 0, event; |
392025f8 PNA |
1844 | |
1845 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU); | |
15e47304 | 1846 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
392025f8 PNA |
1847 | if (nlh == NULL) |
1848 | goto nlmsg_failure; | |
1849 | ||
1850 | nfmsg = nlmsg_data(nlh); | |
1851 | nfmsg->nfgen_family = AF_UNSPEC; | |
1852 | nfmsg->version = NFNETLINK_V0; | |
1853 | nfmsg->res_id = htons(cpu); | |
1854 | ||
1855 | if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) || | |
1856 | nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) || | |
1857 | nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) || | |
1858 | nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) || | |
1859 | nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) || | |
1860 | nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) || | |
1861 | nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) || | |
1862 | nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) || | |
1863 | nla_put_be32(skb, CTA_STATS_INSERT_FAILED, | |
1864 | htonl(st->insert_failed)) || | |
1865 | nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) || | |
1866 | nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) || | |
1867 | nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) || | |
1868 | nla_put_be32(skb, CTA_STATS_SEARCH_RESTART, | |
1869 | htonl(st->search_restart))) | |
1870 | goto nla_put_failure; | |
1871 | ||
1872 | nlmsg_end(skb, nlh); | |
1873 | return skb->len; | |
1874 | ||
1875 | nla_put_failure: | |
1876 | nlmsg_failure: | |
1877 | nlmsg_cancel(skb, nlh); | |
1878 | return -1; | |
1879 | } | |
1880 | ||
1881 | static int | |
1882 | ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
1883 | { | |
1884 | int cpu; | |
1885 | struct net *net = sock_net(skb->sk); | |
1886 | ||
1887 | if (cb->args[0] == nr_cpu_ids) | |
1888 | return 0; | |
1889 | ||
1890 | for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) { | |
1891 | const struct ip_conntrack_stat *st; | |
1892 | ||
1893 | if (!cpu_possible(cpu)) | |
1894 | continue; | |
1895 | ||
1896 | st = per_cpu_ptr(net->ct.stat, cpu); | |
1897 | if (ctnetlink_ct_stat_cpu_fill_info(skb, | |
15e47304 | 1898 | NETLINK_CB(cb->skb).portid, |
392025f8 PNA |
1899 | cb->nlh->nlmsg_seq, |
1900 | cpu, st) < 0) | |
1901 | break; | |
1902 | } | |
1903 | cb->args[0] = cpu; | |
1904 | ||
1905 | return skb->len; | |
1906 | } | |
1907 | ||
1908 | static int | |
1909 | ctnetlink_stat_ct_cpu(struct sock *ctnl, struct sk_buff *skb, | |
1910 | const struct nlmsghdr *nlh, | |
1911 | const struct nlattr * const cda[]) | |
1912 | { | |
1913 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1914 | struct netlink_dump_control c = { | |
1915 | .dump = ctnetlink_ct_stat_cpu_dump, | |
1916 | }; | |
1917 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
1918 | } | |
1919 | ||
1920 | return 0; | |
1921 | } | |
1922 | ||
1923 | static int | |
15e47304 | 1924 | ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, |
392025f8 PNA |
1925 | struct net *net) |
1926 | { | |
1927 | struct nlmsghdr *nlh; | |
1928 | struct nfgenmsg *nfmsg; | |
15e47304 | 1929 | unsigned int flags = portid ? NLM_F_MULTI : 0, event; |
392025f8 PNA |
1930 | unsigned int nr_conntracks = atomic_read(&net->ct.count); |
1931 | ||
1932 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS); | |
15e47304 | 1933 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
392025f8 PNA |
1934 | if (nlh == NULL) |
1935 | goto nlmsg_failure; | |
1936 | ||
1937 | nfmsg = nlmsg_data(nlh); | |
1938 | nfmsg->nfgen_family = AF_UNSPEC; | |
1939 | nfmsg->version = NFNETLINK_V0; | |
1940 | nfmsg->res_id = 0; | |
1941 | ||
1942 | if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks))) | |
1943 | goto nla_put_failure; | |
1944 | ||
1945 | nlmsg_end(skb, nlh); | |
1946 | return skb->len; | |
1947 | ||
1948 | nla_put_failure: | |
1949 | nlmsg_failure: | |
1950 | nlmsg_cancel(skb, nlh); | |
1951 | return -1; | |
1952 | } | |
1953 | ||
1954 | static int | |
1955 | ctnetlink_stat_ct(struct sock *ctnl, struct sk_buff *skb, | |
1956 | const struct nlmsghdr *nlh, | |
1957 | const struct nlattr * const cda[]) | |
1958 | { | |
1959 | struct sk_buff *skb2; | |
1960 | int err; | |
1961 | ||
1962 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
1963 | if (skb2 == NULL) | |
1964 | return -ENOMEM; | |
1965 | ||
15e47304 | 1966 | err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid, |
392025f8 PNA |
1967 | nlh->nlmsg_seq, |
1968 | NFNL_MSG_TYPE(nlh->nlmsg_type), | |
1969 | sock_net(skb->sk)); | |
1970 | if (err <= 0) | |
1971 | goto free; | |
1972 | ||
15e47304 | 1973 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT); |
392025f8 PNA |
1974 | if (err < 0) |
1975 | goto out; | |
1976 | ||
1977 | return 0; | |
1978 | ||
1979 | free: | |
1980 | kfree_skb(skb2); | |
1981 | out: | |
1982 | /* this avoids a loop in nfnetlink. */ | |
1983 | return err == -EAGAIN ? -ENOBUFS : err; | |
1984 | } | |
1985 | ||
7c622345 | 1986 | #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT |
9cb01766 PNA |
1987 | static size_t |
1988 | ctnetlink_nfqueue_build_size(const struct nf_conn *ct) | |
1989 | { | |
1990 | return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ | |
1991 | + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ | |
1992 | + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ | |
1993 | + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ | |
1994 | + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ | |
1995 | + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ | |
1996 | + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ | |
1997 | + nla_total_size(0) /* CTA_PROTOINFO */ | |
1998 | + nla_total_size(0) /* CTA_HELP */ | |
1999 | + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ | |
2000 | + ctnetlink_secctx_size(ct) | |
2001 | #ifdef CONFIG_NF_NAT_NEEDED | |
2002 | + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ | |
2003 | + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ | |
2004 | #endif | |
2005 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
2006 | + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ | |
2007 | #endif | |
2008 | + ctnetlink_proto_size(ct) | |
2009 | ; | |
2010 | } | |
2011 | ||
2012 | static int | |
2013 | ctnetlink_nfqueue_build(struct sk_buff *skb, struct nf_conn *ct) | |
2014 | { | |
2015 | struct nlattr *nest_parms; | |
2016 | ||
2017 | rcu_read_lock(); | |
2018 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); | |
2019 | if (!nest_parms) | |
2020 | goto nla_put_failure; | |
2021 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) | |
2022 | goto nla_put_failure; | |
2023 | nla_nest_end(skb, nest_parms); | |
2024 | ||
2025 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); | |
2026 | if (!nest_parms) | |
2027 | goto nla_put_failure; | |
2028 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) | |
2029 | goto nla_put_failure; | |
2030 | nla_nest_end(skb, nest_parms); | |
2031 | ||
2032 | if (nf_ct_zone(ct)) { | |
2033 | if (nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct)))) | |
2034 | goto nla_put_failure; | |
2035 | } | |
2036 | ||
2037 | if (ctnetlink_dump_id(skb, ct) < 0) | |
2038 | goto nla_put_failure; | |
2039 | ||
2040 | if (ctnetlink_dump_status(skb, ct) < 0) | |
2041 | goto nla_put_failure; | |
2042 | ||
2043 | if (ctnetlink_dump_timeout(skb, ct) < 0) | |
2044 | goto nla_put_failure; | |
2045 | ||
2046 | if (ctnetlink_dump_protoinfo(skb, ct) < 0) | |
2047 | goto nla_put_failure; | |
2048 | ||
2049 | if (ctnetlink_dump_helpinfo(skb, ct) < 0) | |
2050 | goto nla_put_failure; | |
2051 | ||
2052 | #ifdef CONFIG_NF_CONNTRACK_SECMARK | |
2053 | if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0) | |
2054 | goto nla_put_failure; | |
2055 | #endif | |
2056 | if (ct->master && ctnetlink_dump_master(skb, ct) < 0) | |
2057 | goto nla_put_failure; | |
2058 | ||
2059 | if ((ct->status & IPS_SEQ_ADJUST) && | |
2060 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) | |
2061 | goto nla_put_failure; | |
2062 | ||
2063 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
2064 | if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0) | |
2065 | goto nla_put_failure; | |
2066 | #endif | |
0ceabd83 FW |
2067 | if (ctnetlink_dump_labels(skb, ct) < 0) |
2068 | goto nla_put_failure; | |
9cb01766 PNA |
2069 | rcu_read_unlock(); |
2070 | return 0; | |
2071 | ||
2072 | nla_put_failure: | |
2073 | rcu_read_unlock(); | |
2074 | return -ENOSPC; | |
2075 | } | |
2076 | ||
2077 | static int | |
2078 | ctnetlink_nfqueue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct) | |
2079 | { | |
2080 | int err; | |
2081 | ||
2082 | if (cda[CTA_TIMEOUT]) { | |
2083 | err = ctnetlink_change_timeout(ct, cda); | |
2084 | if (err < 0) | |
2085 | return err; | |
2086 | } | |
2087 | if (cda[CTA_STATUS]) { | |
2088 | err = ctnetlink_change_status(ct, cda); | |
2089 | if (err < 0) | |
2090 | return err; | |
2091 | } | |
2092 | if (cda[CTA_HELP]) { | |
2093 | err = ctnetlink_change_helper(ct, cda); | |
2094 | if (err < 0) | |
2095 | return err; | |
2096 | } | |
9b21f6a9 FW |
2097 | if (cda[CTA_LABELS]) { |
2098 | err = ctnetlink_attach_labels(ct, cda); | |
2099 | if (err < 0) | |
2100 | return err; | |
2101 | } | |
9cb01766 PNA |
2102 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
2103 | if (cda[CTA_MARK]) | |
2104 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); | |
2105 | #endif | |
2106 | return 0; | |
2107 | } | |
2108 | ||
2109 | static int | |
2110 | ctnetlink_nfqueue_parse(const struct nlattr *attr, struct nf_conn *ct) | |
2111 | { | |
2112 | struct nlattr *cda[CTA_MAX+1]; | |
68e035c9 | 2113 | int ret; |
9cb01766 PNA |
2114 | |
2115 | nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy); | |
2116 | ||
68e035c9 PNA |
2117 | spin_lock_bh(&nf_conntrack_lock); |
2118 | ret = ctnetlink_nfqueue_parse_ct((const struct nlattr **)cda, ct); | |
2119 | spin_unlock_bh(&nf_conntrack_lock); | |
2120 | ||
2121 | return ret; | |
9cb01766 PNA |
2122 | } |
2123 | ||
2124 | static struct nfq_ct_hook ctnetlink_nfqueue_hook = { | |
2125 | .build_size = ctnetlink_nfqueue_build_size, | |
2126 | .build = ctnetlink_nfqueue_build, | |
2127 | .parse = ctnetlink_nfqueue_parse, | |
2128 | }; | |
7c622345 | 2129 | #endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */ |
9cb01766 | 2130 | |
601e68e1 YH |
2131 | /*********************************************************************** |
2132 | * EXPECT | |
2133 | ***********************************************************************/ | |
c1d10adb PNA |
2134 | |
2135 | static inline int | |
2136 | ctnetlink_exp_dump_tuple(struct sk_buff *skb, | |
2137 | const struct nf_conntrack_tuple *tuple, | |
2138 | enum ctattr_expect type) | |
2139 | { | |
df6fb868 | 2140 | struct nlattr *nest_parms; |
601e68e1 | 2141 | |
df6fb868 PM |
2142 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
2143 | if (!nest_parms) | |
2144 | goto nla_put_failure; | |
c1d10adb | 2145 | if (ctnetlink_dump_tuples(skb, tuple) < 0) |
df6fb868 PM |
2146 | goto nla_put_failure; |
2147 | nla_nest_end(skb, nest_parms); | |
c1d10adb PNA |
2148 | |
2149 | return 0; | |
2150 | ||
df6fb868 | 2151 | nla_put_failure: |
c1d10adb | 2152 | return -1; |
601e68e1 | 2153 | } |
c1d10adb | 2154 | |
1cde6436 PNA |
2155 | static inline int |
2156 | ctnetlink_exp_dump_mask(struct sk_buff *skb, | |
2157 | const struct nf_conntrack_tuple *tuple, | |
d4156e8c | 2158 | const struct nf_conntrack_tuple_mask *mask) |
1cde6436 PNA |
2159 | { |
2160 | int ret; | |
2161 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 2162 | struct nf_conntrack_l4proto *l4proto; |
d4156e8c | 2163 | struct nf_conntrack_tuple m; |
df6fb868 | 2164 | struct nlattr *nest_parms; |
d4156e8c PM |
2165 | |
2166 | memset(&m, 0xFF, sizeof(m)); | |
d4156e8c | 2167 | memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3)); |
e578756c PM |
2168 | m.src.u.all = mask->src.u.all; |
2169 | m.dst.protonum = tuple->dst.protonum; | |
d4156e8c | 2170 | |
df6fb868 PM |
2171 | nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED); |
2172 | if (!nest_parms) | |
2173 | goto nla_put_failure; | |
1cde6436 | 2174 | |
3b988ece | 2175 | rcu_read_lock(); |
528a3a6f | 2176 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
d4156e8c | 2177 | ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto); |
3b988ece HS |
2178 | if (ret >= 0) { |
2179 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, | |
2180 | tuple->dst.protonum); | |
d4156e8c | 2181 | ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto); |
3b988ece HS |
2182 | } |
2183 | rcu_read_unlock(); | |
2184 | ||
1cde6436 | 2185 | if (unlikely(ret < 0)) |
df6fb868 | 2186 | goto nla_put_failure; |
1cde6436 | 2187 | |
df6fb868 | 2188 | nla_nest_end(skb, nest_parms); |
1cde6436 PNA |
2189 | |
2190 | return 0; | |
2191 | ||
df6fb868 | 2192 | nla_put_failure: |
1cde6436 PNA |
2193 | return -1; |
2194 | } | |
2195 | ||
c7232c99 PM |
2196 | static const union nf_inet_addr any_addr; |
2197 | ||
bb5cf80e | 2198 | static int |
c1d10adb | 2199 | ctnetlink_exp_dump_expect(struct sk_buff *skb, |
601e68e1 | 2200 | const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
2201 | { |
2202 | struct nf_conn *master = exp->master; | |
c1216382 | 2203 | long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ; |
bc01befd | 2204 | struct nf_conn_help *help; |
076a0ca0 PNA |
2205 | #ifdef CONFIG_NF_NAT_NEEDED |
2206 | struct nlattr *nest_parms; | |
2207 | struct nf_conntrack_tuple nat_tuple = {}; | |
2208 | #endif | |
544d5c7d PNA |
2209 | struct nf_ct_helper_expectfn *expfn; |
2210 | ||
d978e5da PM |
2211 | if (timeout < 0) |
2212 | timeout = 0; | |
c1d10adb PNA |
2213 | |
2214 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) | |
df6fb868 | 2215 | goto nla_put_failure; |
1cde6436 | 2216 | if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) |
df6fb868 | 2217 | goto nla_put_failure; |
c1d10adb PNA |
2218 | if (ctnetlink_exp_dump_tuple(skb, |
2219 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
2220 | CTA_EXPECT_MASTER) < 0) | |
df6fb868 | 2221 | goto nla_put_failure; |
601e68e1 | 2222 | |
076a0ca0 | 2223 | #ifdef CONFIG_NF_NAT_NEEDED |
c7232c99 PM |
2224 | if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) || |
2225 | exp->saved_proto.all) { | |
076a0ca0 PNA |
2226 | nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED); |
2227 | if (!nest_parms) | |
2228 | goto nla_put_failure; | |
2229 | ||
cc1eb431 DM |
2230 | if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir))) |
2231 | goto nla_put_failure; | |
076a0ca0 PNA |
2232 | |
2233 | nat_tuple.src.l3num = nf_ct_l3num(master); | |
c7232c99 | 2234 | nat_tuple.src.u3 = exp->saved_addr; |
076a0ca0 PNA |
2235 | nat_tuple.dst.protonum = nf_ct_protonum(master); |
2236 | nat_tuple.src.u = exp->saved_proto; | |
2237 | ||
2238 | if (ctnetlink_exp_dump_tuple(skb, &nat_tuple, | |
2239 | CTA_EXPECT_NAT_TUPLE) < 0) | |
2240 | goto nla_put_failure; | |
2241 | nla_nest_end(skb, nest_parms); | |
2242 | } | |
2243 | #endif | |
cc1eb431 DM |
2244 | if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) || |
2245 | nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) || | |
2246 | nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) || | |
2247 | nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class))) | |
2248 | goto nla_put_failure; | |
bc01befd PNA |
2249 | help = nfct_help(master); |
2250 | if (help) { | |
2251 | struct nf_conntrack_helper *helper; | |
2252 | ||
2253 | helper = rcu_dereference(help->helper); | |
cc1eb431 DM |
2254 | if (helper && |
2255 | nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name)) | |
2256 | goto nla_put_failure; | |
bc01befd | 2257 | } |
544d5c7d | 2258 | expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn); |
cc1eb431 DM |
2259 | if (expfn != NULL && |
2260 | nla_put_string(skb, CTA_EXPECT_FN, expfn->name)) | |
2261 | goto nla_put_failure; | |
c1d10adb PNA |
2262 | |
2263 | return 0; | |
601e68e1 | 2264 | |
df6fb868 | 2265 | nla_put_failure: |
c1d10adb PNA |
2266 | return -1; |
2267 | } | |
2268 | ||
2269 | static int | |
15e47304 | 2270 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq, |
8b0a231d | 2271 | int event, const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
2272 | { |
2273 | struct nlmsghdr *nlh; | |
2274 | struct nfgenmsg *nfmsg; | |
15e47304 | 2275 | unsigned int flags = portid ? NLM_F_MULTI : 0; |
c1d10adb PNA |
2276 | |
2277 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; | |
15e47304 | 2278 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
96bcf938 PNA |
2279 | if (nlh == NULL) |
2280 | goto nlmsg_failure; | |
c1d10adb | 2281 | |
96bcf938 | 2282 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
2283 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
2284 | nfmsg->version = NFNETLINK_V0; | |
2285 | nfmsg->res_id = 0; | |
2286 | ||
2287 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) | |
df6fb868 | 2288 | goto nla_put_failure; |
c1d10adb | 2289 | |
96bcf938 | 2290 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
2291 | return skb->len; |
2292 | ||
2293 | nlmsg_failure: | |
df6fb868 | 2294 | nla_put_failure: |
96bcf938 | 2295 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
2296 | return -1; |
2297 | } | |
2298 | ||
2299 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
e34d5c1a PNA |
2300 | static int |
2301 | ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item) | |
c1d10adb | 2302 | { |
9592a5c0 AD |
2303 | struct nf_conntrack_expect *exp = item->exp; |
2304 | struct net *net = nf_ct_exp_net(exp); | |
c1d10adb PNA |
2305 | struct nlmsghdr *nlh; |
2306 | struct nfgenmsg *nfmsg; | |
c1d10adb | 2307 | struct sk_buff *skb; |
ebbf41df | 2308 | unsigned int type, group; |
c1d10adb PNA |
2309 | int flags = 0; |
2310 | ||
ebbf41df PNA |
2311 | if (events & (1 << IPEXP_DESTROY)) { |
2312 | type = IPCTNL_MSG_EXP_DELETE; | |
2313 | group = NFNLGRP_CONNTRACK_EXP_DESTROY; | |
2314 | } else if (events & (1 << IPEXP_NEW)) { | |
c1d10adb PNA |
2315 | type = IPCTNL_MSG_EXP_NEW; |
2316 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
ebbf41df | 2317 | group = NFNLGRP_CONNTRACK_EXP_NEW; |
c1d10adb | 2318 | } else |
e34d5c1a | 2319 | return 0; |
c1d10adb | 2320 | |
ebbf41df | 2321 | if (!item->report && !nfnetlink_has_listeners(net, group)) |
e34d5c1a | 2322 | return 0; |
b3a27bfb | 2323 | |
96bcf938 PNA |
2324 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
2325 | if (skb == NULL) | |
150ace0d | 2326 | goto errout; |
c1d10adb | 2327 | |
b633ad5f | 2328 | type |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
15e47304 | 2329 | nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags); |
96bcf938 PNA |
2330 | if (nlh == NULL) |
2331 | goto nlmsg_failure; | |
c1d10adb | 2332 | |
96bcf938 | 2333 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
2334 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
2335 | nfmsg->version = NFNETLINK_V0; | |
2336 | nfmsg->res_id = 0; | |
2337 | ||
528a3a6f | 2338 | rcu_read_lock(); |
c1d10adb | 2339 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
df6fb868 | 2340 | goto nla_put_failure; |
528a3a6f | 2341 | rcu_read_unlock(); |
c1d10adb | 2342 | |
96bcf938 | 2343 | nlmsg_end(skb, nlh); |
15e47304 | 2344 | nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC); |
e34d5c1a | 2345 | return 0; |
c1d10adb | 2346 | |
df6fb868 | 2347 | nla_put_failure: |
528a3a6f | 2348 | rcu_read_unlock(); |
96bcf938 | 2349 | nlmsg_cancel(skb, nlh); |
528a3a6f | 2350 | nlmsg_failure: |
c1d10adb | 2351 | kfree_skb(skb); |
150ace0d | 2352 | errout: |
9592a5c0 | 2353 | nfnetlink_set_err(net, 0, 0, -ENOBUFS); |
e34d5c1a | 2354 | return 0; |
c1d10adb PNA |
2355 | } |
2356 | #endif | |
cf6994c2 PM |
2357 | static int ctnetlink_exp_done(struct netlink_callback *cb) |
2358 | { | |
31f15875 PM |
2359 | if (cb->args[1]) |
2360 | nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]); | |
cf6994c2 PM |
2361 | return 0; |
2362 | } | |
c1d10adb PNA |
2363 | |
2364 | static int | |
2365 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
2366 | { | |
9592a5c0 | 2367 | struct net *net = sock_net(skb->sk); |
cf6994c2 | 2368 | struct nf_conntrack_expect *exp, *last; |
96bcf938 | 2369 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
31f15875 | 2370 | struct hlist_node *n; |
87711cb8 | 2371 | u_int8_t l3proto = nfmsg->nfgen_family; |
c1d10adb | 2372 | |
7d0742da | 2373 | rcu_read_lock(); |
31f15875 PM |
2374 | last = (struct nf_conntrack_expect *)cb->args[1]; |
2375 | for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) { | |
cf6994c2 | 2376 | restart: |
9b03f38d | 2377 | hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]], |
31f15875 PM |
2378 | hnode) { |
2379 | if (l3proto && exp->tuple.src.l3num != l3proto) | |
cf6994c2 | 2380 | continue; |
31f15875 PM |
2381 | if (cb->args[1]) { |
2382 | if (exp != last) | |
2383 | continue; | |
2384 | cb->args[1] = 0; | |
2385 | } | |
8b0a231d | 2386 | if (ctnetlink_exp_fill_info(skb, |
15e47304 | 2387 | NETLINK_CB(cb->skb).portid, |
31f15875 PM |
2388 | cb->nlh->nlmsg_seq, |
2389 | IPCTNL_MSG_EXP_NEW, | |
8b0a231d | 2390 | exp) < 0) { |
7d0742da PM |
2391 | if (!atomic_inc_not_zero(&exp->use)) |
2392 | continue; | |
31f15875 PM |
2393 | cb->args[1] = (unsigned long)exp; |
2394 | goto out; | |
2395 | } | |
cf6994c2 | 2396 | } |
31f15875 PM |
2397 | if (cb->args[1]) { |
2398 | cb->args[1] = 0; | |
2399 | goto restart; | |
cf6994c2 PM |
2400 | } |
2401 | } | |
601e68e1 | 2402 | out: |
7d0742da | 2403 | rcu_read_unlock(); |
cf6994c2 PM |
2404 | if (last) |
2405 | nf_ct_expect_put(last); | |
c1d10adb | 2406 | |
c1d10adb PNA |
2407 | return skb->len; |
2408 | } | |
2409 | ||
f73e924c | 2410 | static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = { |
d0b0268f PM |
2411 | [CTA_EXPECT_MASTER] = { .type = NLA_NESTED }, |
2412 | [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED }, | |
2413 | [CTA_EXPECT_MASK] = { .type = NLA_NESTED }, | |
f73e924c PM |
2414 | [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 }, |
2415 | [CTA_EXPECT_ID] = { .type = NLA_U32 }, | |
6d1fafca FW |
2416 | [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING, |
2417 | .len = NF_CT_HELPER_NAME_LEN - 1 }, | |
bcac0dfa | 2418 | [CTA_EXPECT_ZONE] = { .type = NLA_U16 }, |
8b008faf | 2419 | [CTA_EXPECT_FLAGS] = { .type = NLA_U32 }, |
b8c5e52c | 2420 | [CTA_EXPECT_CLASS] = { .type = NLA_U32 }, |
076a0ca0 | 2421 | [CTA_EXPECT_NAT] = { .type = NLA_NESTED }, |
544d5c7d | 2422 | [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING }, |
c1d10adb PNA |
2423 | }; |
2424 | ||
2425 | static int | |
601e68e1 | 2426 | ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
2427 | const struct nlmsghdr *nlh, |
2428 | const struct nlattr * const cda[]) | |
c1d10adb | 2429 | { |
9592a5c0 | 2430 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
2431 | struct nf_conntrack_tuple tuple; |
2432 | struct nf_conntrack_expect *exp; | |
2433 | struct sk_buff *skb2; | |
96bcf938 | 2434 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 2435 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
2436 | u16 zone; |
2437 | int err; | |
c1d10adb | 2438 | |
b8f3ab42 | 2439 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
80d326fa PNA |
2440 | struct netlink_dump_control c = { |
2441 | .dump = ctnetlink_exp_dump_table, | |
2442 | .done = ctnetlink_exp_done, | |
2443 | }; | |
2444 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
c1d10adb PNA |
2445 | } |
2446 | ||
ef00f89f PM |
2447 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2448 | if (err < 0) | |
2449 | return err; | |
2450 | ||
35dba1d7 PNA |
2451 | if (cda[CTA_EXPECT_TUPLE]) |
2452 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
2453 | else if (cda[CTA_EXPECT_MASTER]) | |
c1d10adb PNA |
2454 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3); |
2455 | else | |
2456 | return -EINVAL; | |
2457 | ||
2458 | if (err < 0) | |
2459 | return err; | |
2460 | ||
ef00f89f | 2461 | exp = nf_ct_expect_find_get(net, zone, &tuple); |
c1d10adb PNA |
2462 | if (!exp) |
2463 | return -ENOENT; | |
2464 | ||
df6fb868 | 2465 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 2466 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 2467 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 2468 | nf_ct_expect_put(exp); |
c1d10adb PNA |
2469 | return -ENOENT; |
2470 | } | |
601e68e1 | 2471 | } |
c1d10adb PNA |
2472 | |
2473 | err = -ENOMEM; | |
96bcf938 | 2474 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
81378f72 PNA |
2475 | if (skb2 == NULL) { |
2476 | nf_ct_expect_put(exp); | |
c1d10adb | 2477 | goto out; |
81378f72 | 2478 | } |
4e9b8269 | 2479 | |
528a3a6f | 2480 | rcu_read_lock(); |
15e47304 | 2481 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid, |
8b0a231d | 2482 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp); |
528a3a6f | 2483 | rcu_read_unlock(); |
81378f72 | 2484 | nf_ct_expect_put(exp); |
c1d10adb PNA |
2485 | if (err <= 0) |
2486 | goto free; | |
2487 | ||
15e47304 | 2488 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT); |
81378f72 PNA |
2489 | if (err < 0) |
2490 | goto out; | |
c1d10adb | 2491 | |
81378f72 | 2492 | return 0; |
c1d10adb PNA |
2493 | |
2494 | free: | |
2495 | kfree_skb(skb2); | |
2496 | out: | |
81378f72 PNA |
2497 | /* this avoids a loop in nfnetlink. */ |
2498 | return err == -EAGAIN ? -ENOBUFS : err; | |
c1d10adb PNA |
2499 | } |
2500 | ||
2501 | static int | |
601e68e1 | 2502 | ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
2503 | const struct nlmsghdr *nlh, |
2504 | const struct nlattr * const cda[]) | |
c1d10adb | 2505 | { |
9592a5c0 | 2506 | struct net *net = sock_net(ctnl); |
31f15875 | 2507 | struct nf_conntrack_expect *exp; |
c1d10adb | 2508 | struct nf_conntrack_tuple tuple; |
96bcf938 | 2509 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
31f15875 | 2510 | struct hlist_node *n, *next; |
c1d10adb | 2511 | u_int8_t u3 = nfmsg->nfgen_family; |
31f15875 | 2512 | unsigned int i; |
ef00f89f | 2513 | u16 zone; |
c1d10adb PNA |
2514 | int err; |
2515 | ||
df6fb868 | 2516 | if (cda[CTA_EXPECT_TUPLE]) { |
c1d10adb | 2517 | /* delete a single expect by tuple */ |
ef00f89f PM |
2518 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2519 | if (err < 0) | |
2520 | return err; | |
2521 | ||
c1d10adb PNA |
2522 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
2523 | if (err < 0) | |
2524 | return err; | |
2525 | ||
2526 | /* bump usage count to 2 */ | |
ef00f89f | 2527 | exp = nf_ct_expect_find_get(net, zone, &tuple); |
c1d10adb PNA |
2528 | if (!exp) |
2529 | return -ENOENT; | |
2530 | ||
df6fb868 | 2531 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 2532 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 2533 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 2534 | nf_ct_expect_put(exp); |
c1d10adb PNA |
2535 | return -ENOENT; |
2536 | } | |
2537 | } | |
2538 | ||
2539 | /* after list removal, usage count == 1 */ | |
ebbf41df PNA |
2540 | spin_lock_bh(&nf_conntrack_lock); |
2541 | if (del_timer(&exp->timeout)) { | |
15e47304 | 2542 | nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid, |
ebbf41df PNA |
2543 | nlmsg_report(nlh)); |
2544 | nf_ct_expect_put(exp); | |
2545 | } | |
2546 | spin_unlock_bh(&nf_conntrack_lock); | |
601e68e1 | 2547 | /* have to put what we 'get' above. |
c1d10adb | 2548 | * after this line usage count == 0 */ |
6823645d | 2549 | nf_ct_expect_put(exp); |
df6fb868 PM |
2550 | } else if (cda[CTA_EXPECT_HELP_NAME]) { |
2551 | char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
31f15875 | 2552 | struct nf_conn_help *m_help; |
c1d10adb PNA |
2553 | |
2554 | /* delete all expectations for this helper */ | |
f8ba1aff | 2555 | spin_lock_bh(&nf_conntrack_lock); |
31f15875 PM |
2556 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
2557 | hlist_for_each_entry_safe(exp, n, next, | |
9592a5c0 | 2558 | &net->ct.expect_hash[i], |
31f15875 PM |
2559 | hnode) { |
2560 | m_help = nfct_help(exp->master); | |
794e6871 PM |
2561 | if (!strcmp(m_help->helper->name, name) && |
2562 | del_timer(&exp->timeout)) { | |
ebbf41df | 2563 | nf_ct_unlink_expect_report(exp, |
15e47304 | 2564 | NETLINK_CB(skb).portid, |
ebbf41df | 2565 | nlmsg_report(nlh)); |
31f15875 PM |
2566 | nf_ct_expect_put(exp); |
2567 | } | |
c1d10adb PNA |
2568 | } |
2569 | } | |
f8ba1aff | 2570 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
2571 | } else { |
2572 | /* This basically means we have to flush everything*/ | |
f8ba1aff | 2573 | spin_lock_bh(&nf_conntrack_lock); |
31f15875 PM |
2574 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
2575 | hlist_for_each_entry_safe(exp, n, next, | |
9592a5c0 | 2576 | &net->ct.expect_hash[i], |
31f15875 PM |
2577 | hnode) { |
2578 | if (del_timer(&exp->timeout)) { | |
ebbf41df | 2579 | nf_ct_unlink_expect_report(exp, |
15e47304 | 2580 | NETLINK_CB(skb).portid, |
ebbf41df | 2581 | nlmsg_report(nlh)); |
31f15875 PM |
2582 | nf_ct_expect_put(exp); |
2583 | } | |
c1d10adb PNA |
2584 | } |
2585 | } | |
f8ba1aff | 2586 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
2587 | } |
2588 | ||
2589 | return 0; | |
2590 | } | |
2591 | static int | |
39938324 PM |
2592 | ctnetlink_change_expect(struct nf_conntrack_expect *x, |
2593 | const struct nlattr * const cda[]) | |
c1d10adb | 2594 | { |
9768e1ac KW |
2595 | if (cda[CTA_EXPECT_TIMEOUT]) { |
2596 | if (!del_timer(&x->timeout)) | |
2597 | return -ETIME; | |
2598 | ||
2599 | x->timeout.expires = jiffies + | |
2600 | ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ; | |
2601 | add_timer(&x->timeout); | |
2602 | } | |
2603 | return 0; | |
c1d10adb PNA |
2604 | } |
2605 | ||
076a0ca0 PNA |
2606 | static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = { |
2607 | [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 }, | |
2608 | [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED }, | |
2609 | }; | |
2610 | ||
2611 | static int | |
2612 | ctnetlink_parse_expect_nat(const struct nlattr *attr, | |
2613 | struct nf_conntrack_expect *exp, | |
2614 | u_int8_t u3) | |
2615 | { | |
2616 | #ifdef CONFIG_NF_NAT_NEEDED | |
2617 | struct nlattr *tb[CTA_EXPECT_NAT_MAX+1]; | |
2618 | struct nf_conntrack_tuple nat_tuple = {}; | |
2619 | int err; | |
2620 | ||
2621 | nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy); | |
2622 | ||
2623 | if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE]) | |
2624 | return -EINVAL; | |
2625 | ||
2626 | err = ctnetlink_parse_tuple((const struct nlattr * const *)tb, | |
2627 | &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3); | |
2628 | if (err < 0) | |
2629 | return err; | |
2630 | ||
c7232c99 | 2631 | exp->saved_addr = nat_tuple.src.u3; |
076a0ca0 PNA |
2632 | exp->saved_proto = nat_tuple.src.u; |
2633 | exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR])); | |
2634 | ||
2635 | return 0; | |
2636 | #else | |
2637 | return -EOPNOTSUPP; | |
2638 | #endif | |
2639 | } | |
2640 | ||
c1d10adb | 2641 | static int |
ef00f89f PM |
2642 | ctnetlink_create_expect(struct net *net, u16 zone, |
2643 | const struct nlattr * const cda[], | |
9592a5c0 | 2644 | u_int8_t u3, |
15e47304 | 2645 | u32 portid, int report) |
c1d10adb PNA |
2646 | { |
2647 | struct nf_conntrack_tuple tuple, mask, master_tuple; | |
2648 | struct nf_conntrack_tuple_hash *h = NULL; | |
2649 | struct nf_conntrack_expect *exp; | |
2650 | struct nf_conn *ct; | |
dc808fe2 | 2651 | struct nf_conn_help *help; |
660fdb2a | 2652 | struct nf_conntrack_helper *helper = NULL; |
b8c5e52c | 2653 | u_int32_t class = 0; |
c1d10adb PNA |
2654 | int err = 0; |
2655 | ||
c1d10adb PNA |
2656 | /* caller guarantees that those three CTA_EXPECT_* exist */ |
2657 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
2658 | if (err < 0) | |
2659 | return err; | |
2660 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3); | |
2661 | if (err < 0) | |
2662 | return err; | |
2663 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3); | |
2664 | if (err < 0) | |
2665 | return err; | |
2666 | ||
2667 | /* Look for master conntrack of this expectation */ | |
ef00f89f | 2668 | h = nf_conntrack_find_get(net, zone, &master_tuple); |
c1d10adb PNA |
2669 | if (!h) |
2670 | return -ENOENT; | |
2671 | ct = nf_ct_tuplehash_to_ctrack(h); | |
660fdb2a PNA |
2672 | |
2673 | /* Look for helper of this expectation */ | |
2674 | if (cda[CTA_EXPECT_HELP_NAME]) { | |
2675 | const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
2676 | ||
2677 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), | |
2678 | nf_ct_protonum(ct)); | |
2679 | if (helper == NULL) { | |
2680 | #ifdef CONFIG_MODULES | |
2681 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
2682 | err = -EOPNOTSUPP; | |
2683 | goto out; | |
2684 | } | |
2685 | ||
2686 | helper = __nf_conntrack_helper_find(helpname, | |
2687 | nf_ct_l3num(ct), | |
2688 | nf_ct_protonum(ct)); | |
2689 | if (helper) { | |
2690 | err = -EAGAIN; | |
2691 | goto out; | |
2692 | } | |
2693 | #endif | |
2694 | err = -EOPNOTSUPP; | |
2695 | goto out; | |
2696 | } | |
2697 | } | |
2698 | ||
b8c5e52c PNA |
2699 | if (cda[CTA_EXPECT_CLASS] && helper) { |
2700 | class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS])); | |
2701 | if (class > helper->expect_class_max) { | |
2702 | err = -EINVAL; | |
2703 | goto out; | |
2704 | } | |
2705 | } | |
6823645d | 2706 | exp = nf_ct_expect_alloc(ct); |
c1d10adb PNA |
2707 | if (!exp) { |
2708 | err = -ENOMEM; | |
2709 | goto out; | |
2710 | } | |
bc01befd PNA |
2711 | help = nfct_help(ct); |
2712 | if (!help) { | |
2713 | if (!cda[CTA_EXPECT_TIMEOUT]) { | |
2714 | err = -EINVAL; | |
1310b955 | 2715 | goto err_out; |
bc01befd PNA |
2716 | } |
2717 | exp->timeout.expires = | |
2718 | jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ; | |
601e68e1 | 2719 | |
bc01befd PNA |
2720 | exp->flags = NF_CT_EXPECT_USERSPACE; |
2721 | if (cda[CTA_EXPECT_FLAGS]) { | |
2722 | exp->flags |= | |
2723 | ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); | |
2724 | } | |
2725 | } else { | |
2726 | if (cda[CTA_EXPECT_FLAGS]) { | |
2727 | exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); | |
2728 | exp->flags &= ~NF_CT_EXPECT_USERSPACE; | |
2729 | } else | |
2730 | exp->flags = 0; | |
2731 | } | |
544d5c7d PNA |
2732 | if (cda[CTA_EXPECT_FN]) { |
2733 | const char *name = nla_data(cda[CTA_EXPECT_FN]); | |
2734 | struct nf_ct_helper_expectfn *expfn; | |
2735 | ||
2736 | expfn = nf_ct_helper_expectfn_find_by_name(name); | |
2737 | if (expfn == NULL) { | |
2738 | err = -EINVAL; | |
2739 | goto err_out; | |
2740 | } | |
2741 | exp->expectfn = expfn->expectfn; | |
2742 | } else | |
2743 | exp->expectfn = NULL; | |
601e68e1 | 2744 | |
b8c5e52c | 2745 | exp->class = class; |
c1d10adb | 2746 | exp->master = ct; |
660fdb2a | 2747 | exp->helper = helper; |
c1d10adb | 2748 | memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple)); |
d4156e8c PM |
2749 | memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3)); |
2750 | exp->mask.src.u.all = mask.src.u.all; | |
c1d10adb | 2751 | |
076a0ca0 PNA |
2752 | if (cda[CTA_EXPECT_NAT]) { |
2753 | err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT], | |
2754 | exp, u3); | |
2755 | if (err < 0) | |
2756 | goto err_out; | |
2757 | } | |
15e47304 | 2758 | err = nf_ct_expect_related_report(exp, portid, report); |
076a0ca0 | 2759 | err_out: |
6823645d | 2760 | nf_ct_expect_put(exp); |
601e68e1 | 2761 | out: |
c1d10adb PNA |
2762 | nf_ct_put(nf_ct_tuplehash_to_ctrack(h)); |
2763 | return err; | |
2764 | } | |
2765 | ||
2766 | static int | |
2767 | ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
2768 | const struct nlmsghdr *nlh, |
2769 | const struct nlattr * const cda[]) | |
c1d10adb | 2770 | { |
9592a5c0 | 2771 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
2772 | struct nf_conntrack_tuple tuple; |
2773 | struct nf_conntrack_expect *exp; | |
96bcf938 | 2774 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 2775 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
2776 | u16 zone; |
2777 | int err; | |
c1d10adb | 2778 | |
df6fb868 PM |
2779 | if (!cda[CTA_EXPECT_TUPLE] |
2780 | || !cda[CTA_EXPECT_MASK] | |
2781 | || !cda[CTA_EXPECT_MASTER]) | |
c1d10adb PNA |
2782 | return -EINVAL; |
2783 | ||
ef00f89f PM |
2784 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2785 | if (err < 0) | |
2786 | return err; | |
2787 | ||
c1d10adb PNA |
2788 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
2789 | if (err < 0) | |
2790 | return err; | |
2791 | ||
f8ba1aff | 2792 | spin_lock_bh(&nf_conntrack_lock); |
ef00f89f | 2793 | exp = __nf_ct_expect_find(net, zone, &tuple); |
c1d10adb PNA |
2794 | |
2795 | if (!exp) { | |
f8ba1aff | 2796 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 2797 | err = -ENOENT; |
19abb7b0 | 2798 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
ef00f89f | 2799 | err = ctnetlink_create_expect(net, zone, cda, |
19abb7b0 | 2800 | u3, |
15e47304 | 2801 | NETLINK_CB(skb).portid, |
19abb7b0 PNA |
2802 | nlmsg_report(nlh)); |
2803 | } | |
c1d10adb PNA |
2804 | return err; |
2805 | } | |
2806 | ||
2807 | err = -EEXIST; | |
2808 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) | |
2809 | err = ctnetlink_change_expect(exp, cda); | |
f8ba1aff | 2810 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 2811 | |
c1d10adb PNA |
2812 | return err; |
2813 | } | |
2814 | ||
392025f8 | 2815 | static int |
15e47304 | 2816 | ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu, |
392025f8 PNA |
2817 | const struct ip_conntrack_stat *st) |
2818 | { | |
2819 | struct nlmsghdr *nlh; | |
2820 | struct nfgenmsg *nfmsg; | |
15e47304 | 2821 | unsigned int flags = portid ? NLM_F_MULTI : 0, event; |
392025f8 PNA |
2822 | |
2823 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU); | |
15e47304 | 2824 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
392025f8 PNA |
2825 | if (nlh == NULL) |
2826 | goto nlmsg_failure; | |
2827 | ||
2828 | nfmsg = nlmsg_data(nlh); | |
2829 | nfmsg->nfgen_family = AF_UNSPEC; | |
2830 | nfmsg->version = NFNETLINK_V0; | |
2831 | nfmsg->res_id = htons(cpu); | |
2832 | ||
2833 | if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) || | |
2834 | nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) || | |
2835 | nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete))) | |
2836 | goto nla_put_failure; | |
2837 | ||
2838 | nlmsg_end(skb, nlh); | |
2839 | return skb->len; | |
2840 | ||
2841 | nla_put_failure: | |
2842 | nlmsg_failure: | |
2843 | nlmsg_cancel(skb, nlh); | |
2844 | return -1; | |
2845 | } | |
2846 | ||
2847 | static int | |
2848 | ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
2849 | { | |
2850 | int cpu; | |
2851 | struct net *net = sock_net(skb->sk); | |
2852 | ||
2853 | if (cb->args[0] == nr_cpu_ids) | |
2854 | return 0; | |
2855 | ||
2856 | for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) { | |
2857 | const struct ip_conntrack_stat *st; | |
2858 | ||
2859 | if (!cpu_possible(cpu)) | |
2860 | continue; | |
2861 | ||
2862 | st = per_cpu_ptr(net->ct.stat, cpu); | |
15e47304 | 2863 | if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid, |
392025f8 PNA |
2864 | cb->nlh->nlmsg_seq, |
2865 | cpu, st) < 0) | |
2866 | break; | |
2867 | } | |
2868 | cb->args[0] = cpu; | |
2869 | ||
2870 | return skb->len; | |
2871 | } | |
2872 | ||
2873 | static int | |
2874 | ctnetlink_stat_exp_cpu(struct sock *ctnl, struct sk_buff *skb, | |
2875 | const struct nlmsghdr *nlh, | |
2876 | const struct nlattr * const cda[]) | |
2877 | { | |
2878 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
2879 | struct netlink_dump_control c = { | |
2880 | .dump = ctnetlink_exp_stat_cpu_dump, | |
2881 | }; | |
2882 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
2883 | } | |
2884 | ||
2885 | return 0; | |
2886 | } | |
2887 | ||
c1d10adb | 2888 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
e34d5c1a PNA |
2889 | static struct nf_ct_event_notifier ctnl_notifier = { |
2890 | .fcn = ctnetlink_conntrack_event, | |
c1d10adb PNA |
2891 | }; |
2892 | ||
e34d5c1a PNA |
2893 | static struct nf_exp_event_notifier ctnl_notifier_exp = { |
2894 | .fcn = ctnetlink_expect_event, | |
c1d10adb PNA |
2895 | }; |
2896 | #endif | |
2897 | ||
7c8d4cb4 | 2898 | static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { |
c1d10adb | 2899 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, |
f73e924c PM |
2900 | .attr_count = CTA_MAX, |
2901 | .policy = ct_nla_policy }, | |
c1d10adb | 2902 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
2903 | .attr_count = CTA_MAX, |
2904 | .policy = ct_nla_policy }, | |
c1d10adb | 2905 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, |
f73e924c PM |
2906 | .attr_count = CTA_MAX, |
2907 | .policy = ct_nla_policy }, | |
c1d10adb | 2908 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
2909 | .attr_count = CTA_MAX, |
2910 | .policy = ct_nla_policy }, | |
392025f8 PNA |
2911 | [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu }, |
2912 | [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct }, | |
d871befe PNA |
2913 | [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying }, |
2914 | [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed }, | |
c1d10adb PNA |
2915 | }; |
2916 | ||
7c8d4cb4 | 2917 | static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { |
c1d10adb | 2918 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
f73e924c PM |
2919 | .attr_count = CTA_EXPECT_MAX, |
2920 | .policy = exp_nla_policy }, | |
c1d10adb | 2921 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, |
f73e924c PM |
2922 | .attr_count = CTA_EXPECT_MAX, |
2923 | .policy = exp_nla_policy }, | |
c1d10adb | 2924 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, |
f73e924c PM |
2925 | .attr_count = CTA_EXPECT_MAX, |
2926 | .policy = exp_nla_policy }, | |
392025f8 | 2927 | [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu }, |
c1d10adb PNA |
2928 | }; |
2929 | ||
7c8d4cb4 | 2930 | static const struct nfnetlink_subsystem ctnl_subsys = { |
c1d10adb PNA |
2931 | .name = "conntrack", |
2932 | .subsys_id = NFNL_SUBSYS_CTNETLINK, | |
2933 | .cb_count = IPCTNL_MSG_MAX, | |
2934 | .cb = ctnl_cb, | |
2935 | }; | |
2936 | ||
7c8d4cb4 | 2937 | static const struct nfnetlink_subsystem ctnl_exp_subsys = { |
c1d10adb PNA |
2938 | .name = "conntrack_expect", |
2939 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, | |
2940 | .cb_count = IPCTNL_MSG_EXP_MAX, | |
2941 | .cb = ctnl_exp_cb, | |
2942 | }; | |
2943 | ||
d2483dde | 2944 | MODULE_ALIAS("ip_conntrack_netlink"); |
c1d10adb | 2945 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); |
34f9a2e4 | 2946 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); |
c1d10adb | 2947 | |
70e9942f PNA |
2948 | static int __net_init ctnetlink_net_init(struct net *net) |
2949 | { | |
2950 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2951 | int ret; | |
2952 | ||
2953 | ret = nf_conntrack_register_notifier(net, &ctnl_notifier); | |
2954 | if (ret < 0) { | |
2955 | pr_err("ctnetlink_init: cannot register notifier.\n"); | |
2956 | goto err_out; | |
2957 | } | |
2958 | ||
2959 | ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp); | |
2960 | if (ret < 0) { | |
2961 | pr_err("ctnetlink_init: cannot expect register notifier.\n"); | |
2962 | goto err_unreg_notifier; | |
2963 | } | |
2964 | #endif | |
2965 | return 0; | |
2966 | ||
2967 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2968 | err_unreg_notifier: | |
2969 | nf_conntrack_unregister_notifier(net, &ctnl_notifier); | |
2970 | err_out: | |
2971 | return ret; | |
2972 | #endif | |
2973 | } | |
2974 | ||
2975 | static void ctnetlink_net_exit(struct net *net) | |
2976 | { | |
2977 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2978 | nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp); | |
2979 | nf_conntrack_unregister_notifier(net, &ctnl_notifier); | |
2980 | #endif | |
2981 | } | |
2982 | ||
2983 | static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list) | |
2984 | { | |
2985 | struct net *net; | |
2986 | ||
2987 | list_for_each_entry(net, net_exit_list, exit_list) | |
2988 | ctnetlink_net_exit(net); | |
2989 | } | |
2990 | ||
2991 | static struct pernet_operations ctnetlink_net_ops = { | |
2992 | .init = ctnetlink_net_init, | |
2993 | .exit_batch = ctnetlink_net_exit_batch, | |
2994 | }; | |
2995 | ||
c1d10adb PNA |
2996 | static int __init ctnetlink_init(void) |
2997 | { | |
2998 | int ret; | |
2999 | ||
654d0fbd | 3000 | pr_info("ctnetlink v%s: registering with nfnetlink.\n", version); |
c1d10adb PNA |
3001 | ret = nfnetlink_subsys_register(&ctnl_subsys); |
3002 | if (ret < 0) { | |
654d0fbd | 3003 | pr_err("ctnetlink_init: cannot register with nfnetlink.\n"); |
c1d10adb PNA |
3004 | goto err_out; |
3005 | } | |
3006 | ||
3007 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); | |
3008 | if (ret < 0) { | |
654d0fbd | 3009 | pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n"); |
c1d10adb PNA |
3010 | goto err_unreg_subsys; |
3011 | } | |
3012 | ||
ef6acf68 JL |
3013 | ret = register_pernet_subsys(&ctnetlink_net_ops); |
3014 | if (ret < 0) { | |
70e9942f | 3015 | pr_err("ctnetlink_init: cannot register pernet operations\n"); |
c1d10adb PNA |
3016 | goto err_unreg_exp_subsys; |
3017 | } | |
7c622345 | 3018 | #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT |
9cb01766 PNA |
3019 | /* setup interaction between nf_queue and nf_conntrack_netlink. */ |
3020 | RCU_INIT_POINTER(nfq_ct_hook, &ctnetlink_nfqueue_hook); | |
3021 | #endif | |
c1d10adb PNA |
3022 | return 0; |
3023 | ||
c1d10adb PNA |
3024 | err_unreg_exp_subsys: |
3025 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
c1d10adb PNA |
3026 | err_unreg_subsys: |
3027 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
3028 | err_out: | |
3029 | return ret; | |
3030 | } | |
3031 | ||
3032 | static void __exit ctnetlink_exit(void) | |
3033 | { | |
654d0fbd | 3034 | pr_info("ctnetlink: unregistering from nfnetlink.\n"); |
c1d10adb | 3035 | |
70e9942f | 3036 | unregister_pernet_subsys(&ctnetlink_net_ops); |
c1d10adb PNA |
3037 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); |
3038 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
7c622345 | 3039 | #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT |
9cb01766 PNA |
3040 | RCU_INIT_POINTER(nfq_ct_hook, NULL); |
3041 | #endif | |
c1d10adb PNA |
3042 | } |
3043 | ||
3044 | module_init(ctnetlink_init); | |
3045 | module_exit(ctnetlink_exit); |