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