]>
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 | ||
4054ff45 PNA |
420 | static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, |
421 | const struct nf_conn *ct) | |
13eae15a | 422 | { |
41d73ec0 PM |
423 | struct nf_conn_seqadj *seqadj = nfct_seqadj(ct); |
424 | struct nf_ct_seqadj *seq; | |
13eae15a | 425 | |
41d73ec0 | 426 | if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj) |
13eae15a PNA |
427 | return 0; |
428 | ||
41d73ec0 PM |
429 | seq = &seqadj->seq[IP_CT_DIR_ORIGINAL]; |
430 | if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1) | |
13eae15a PNA |
431 | return -1; |
432 | ||
41d73ec0 PM |
433 | seq = &seqadj->seq[IP_CT_DIR_REPLY]; |
434 | if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1) | |
13eae15a PNA |
435 | return -1; |
436 | ||
437 | return 0; | |
438 | } | |
13eae15a | 439 | |
4054ff45 | 440 | static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) |
c1d10adb | 441 | { |
cc1eb431 DM |
442 | if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct))) |
443 | goto nla_put_failure; | |
c1d10adb PNA |
444 | return 0; |
445 | ||
df6fb868 | 446 | nla_put_failure: |
c1d10adb PNA |
447 | return -1; |
448 | } | |
449 | ||
4054ff45 | 450 | static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct) |
c1d10adb | 451 | { |
cc1eb431 DM |
452 | if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)))) |
453 | goto nla_put_failure; | |
c1d10adb PNA |
454 | return 0; |
455 | ||
df6fb868 | 456 | nla_put_failure: |
c1d10adb PNA |
457 | return -1; |
458 | } | |
459 | ||
c1d10adb | 460 | static int |
15e47304 | 461 | ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, |
80e60e67 | 462 | struct nf_conn *ct) |
c1d10adb | 463 | { |
308ac914 | 464 | const struct nf_conntrack_zone *zone; |
c1d10adb PNA |
465 | struct nlmsghdr *nlh; |
466 | struct nfgenmsg *nfmsg; | |
df6fb868 | 467 | struct nlattr *nest_parms; |
15e47304 | 468 | unsigned int flags = portid ? NLM_F_MULTI : 0, event; |
c1d10adb | 469 | |
80e60e67 | 470 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW); |
15e47304 | 471 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
96bcf938 PNA |
472 | if (nlh == NULL) |
473 | goto nlmsg_failure; | |
c1d10adb | 474 | |
96bcf938 | 475 | nfmsg = nlmsg_data(nlh); |
5e8fbe2a | 476 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
477 | nfmsg->version = NFNETLINK_V0; |
478 | nfmsg->res_id = 0; | |
479 | ||
deedb590 DB |
480 | zone = nf_ct_zone(ct); |
481 | ||
df6fb868 PM |
482 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
483 | if (!nest_parms) | |
484 | goto nla_put_failure; | |
f2f3e38c | 485 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 | 486 | goto nla_put_failure; |
deedb590 DB |
487 | if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone, |
488 | NF_CT_ZONE_DIR_ORIG) < 0) | |
489 | goto nla_put_failure; | |
df6fb868 | 490 | nla_nest_end(skb, nest_parms); |
601e68e1 | 491 | |
df6fb868 PM |
492 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
493 | if (!nest_parms) | |
494 | goto nla_put_failure; | |
f2f3e38c | 495 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 | 496 | goto nla_put_failure; |
deedb590 DB |
497 | if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone, |
498 | NF_CT_ZONE_DIR_REPL) < 0) | |
499 | goto nla_put_failure; | |
df6fb868 | 500 | nla_nest_end(skb, nest_parms); |
c1d10adb | 501 | |
deedb590 DB |
502 | if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone, |
503 | NF_CT_DEFAULT_ZONE_DIR) < 0) | |
cc1eb431 | 504 | goto nla_put_failure; |
ef00f89f | 505 | |
c1d10adb PNA |
506 | if (ctnetlink_dump_status(skb, ct) < 0 || |
507 | ctnetlink_dump_timeout(skb, ct) < 0 || | |
4542fa47 | 508 | ctnetlink_dump_acct(skb, ct, type) < 0 || |
a992ca2a | 509 | ctnetlink_dump_timestamp(skb, ct) < 0 || |
c1d10adb PNA |
510 | ctnetlink_dump_protoinfo(skb, ct) < 0 || |
511 | ctnetlink_dump_helpinfo(skb, ct) < 0 || | |
512 | ctnetlink_dump_mark(skb, ct) < 0 || | |
1cc63249 | 513 | ctnetlink_dump_secctx(skb, ct) < 0 || |
0ceabd83 | 514 | ctnetlink_dump_labels(skb, ct) < 0 || |
c1d10adb | 515 | ctnetlink_dump_id(skb, ct) < 0 || |
13eae15a | 516 | ctnetlink_dump_use(skb, ct) < 0 || |
0f417ce9 | 517 | ctnetlink_dump_master(skb, ct) < 0 || |
41d73ec0 | 518 | ctnetlink_dump_ct_seq_adj(skb, ct) < 0) |
df6fb868 | 519 | goto nla_put_failure; |
c1d10adb | 520 | |
96bcf938 | 521 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
522 | return skb->len; |
523 | ||
524 | nlmsg_failure: | |
df6fb868 | 525 | nla_put_failure: |
96bcf938 | 526 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
527 | return -1; |
528 | } | |
529 | ||
4a96300c | 530 | static inline size_t ctnetlink_proto_size(const struct nf_conn *ct) |
2732c4e4 HE |
531 | { |
532 | struct nf_conntrack_l3proto *l3proto; | |
533 | struct nf_conntrack_l4proto *l4proto; | |
03b64f51 PNA |
534 | size_t len = 0; |
535 | ||
536 | rcu_read_lock(); | |
537 | l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct)); | |
538 | len += l3proto->nla_size; | |
539 | ||
540 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
541 | len += l4proto->nla_size; | |
542 | rcu_read_unlock(); | |
543 | ||
544 | return len; | |
545 | } | |
546 | ||
4a96300c | 547 | static inline size_t ctnetlink_acct_size(const struct nf_conn *ct) |
d26e6a02 JP |
548 | { |
549 | if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT)) | |
550 | return 0; | |
551 | return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */ | |
b46f6ded ND |
552 | + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */ |
553 | + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */ | |
d26e6a02 JP |
554 | ; |
555 | } | |
556 | ||
4a96300c | 557 | static inline int ctnetlink_secctx_size(const struct nf_conn *ct) |
1cc63249 | 558 | { |
cba85b53 PNA |
559 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
560 | int len, ret; | |
1cc63249 | 561 | |
cba85b53 PNA |
562 | ret = security_secid_to_secctx(ct->secmark, NULL, &len); |
563 | if (ret) | |
564 | return 0; | |
1cc63249 | 565 | |
cba85b53 PNA |
566 | return nla_total_size(0) /* CTA_SECCTX */ |
567 | + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */ | |
568 | #else | |
569 | return 0; | |
1cc63249 | 570 | #endif |
cba85b53 | 571 | } |
1cc63249 | 572 | |
4a96300c | 573 | static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct) |
a992ca2a PNA |
574 | { |
575 | #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP | |
576 | if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP)) | |
577 | return 0; | |
b46f6ded | 578 | return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t)); |
a992ca2a PNA |
579 | #else |
580 | return 0; | |
581 | #endif | |
582 | } | |
583 | ||
4054ff45 PNA |
584 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
585 | static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct) | |
03b64f51 PNA |
586 | { |
587 | return NLMSG_ALIGN(sizeof(struct nfgenmsg)) | |
588 | + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ | |
589 | + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ | |
590 | + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ | |
591 | + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ | |
592 | + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ | |
593 | + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ | |
f7b13e43 | 594 | + ctnetlink_acct_size(ct) |
a992ca2a | 595 | + ctnetlink_timestamp_size(ct) |
03b64f51 PNA |
596 | + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ |
597 | + nla_total_size(0) /* CTA_PROTOINFO */ | |
598 | + nla_total_size(0) /* CTA_HELP */ | |
599 | + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ | |
cba85b53 | 600 | + ctnetlink_secctx_size(ct) |
d271e8bd | 601 | #ifdef CONFIG_NF_NAT_NEEDED |
03b64f51 PNA |
602 | + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ |
603 | + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ | |
d271e8bd HE |
604 | #endif |
605 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
03b64f51 | 606 | + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ |
4a001068 KM |
607 | #endif |
608 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
deedb590 | 609 | + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */ |
d271e8bd | 610 | #endif |
03b64f51 | 611 | + ctnetlink_proto_size(ct) |
0ceabd83 | 612 | + ctnetlink_label_size(ct) |
03b64f51 | 613 | ; |
2732c4e4 HE |
614 | } |
615 | ||
e34d5c1a PNA |
616 | static int |
617 | ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item) | |
c1d10adb | 618 | { |
308ac914 | 619 | const struct nf_conntrack_zone *zone; |
9592a5c0 | 620 | struct net *net; |
c1d10adb PNA |
621 | struct nlmsghdr *nlh; |
622 | struct nfgenmsg *nfmsg; | |
df6fb868 | 623 | struct nlattr *nest_parms; |
19abb7b0 | 624 | struct nf_conn *ct = item->ct; |
c1d10adb PNA |
625 | struct sk_buff *skb; |
626 | unsigned int type; | |
c1d10adb | 627 | unsigned int flags = 0, group; |
dd7669a9 | 628 | int err; |
c1d10adb PNA |
629 | |
630 | /* ignore our fake conntrack entry */ | |
5bfddbd4 | 631 | if (nf_ct_is_untracked(ct)) |
e34d5c1a | 632 | return 0; |
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 | |
c1d10adb | 655 | type |= NFNL_SUBSYS_CTNETLINK << 8; |
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 | ||
130ffbc2 DB |
911 | ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL); |
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, | |
920 | l3proto->nla_policy); | |
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 | ||
f73e924c PM |
941 | ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy); |
942 | if (ret < 0) | |
943 | return ret; | |
c1d10adb | 944 | |
df6fb868 | 945 | if (!tb[CTA_PROTO_NUM]) |
c1d10adb | 946 | return -EINVAL; |
77236b6e | 947 | tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]); |
c1d10adb | 948 | |
cd91566e FW |
949 | rcu_read_lock(); |
950 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); | |
c1d10adb | 951 | |
f73e924c PM |
952 | if (likely(l4proto->nlattr_to_tuple)) { |
953 | ret = nla_validate_nested(attr, CTA_PROTO_MAX, | |
954 | l4proto->nla_policy); | |
955 | if (ret == 0) | |
956 | ret = l4proto->nlattr_to_tuple(tb, tuple); | |
957 | } | |
c1d10adb | 958 | |
cd91566e | 959 | rcu_read_unlock(); |
601e68e1 | 960 | |
c1d10adb PNA |
961 | return ret; |
962 | } | |
963 | ||
deedb590 DB |
964 | static int |
965 | ctnetlink_parse_zone(const struct nlattr *attr, | |
966 | struct nf_conntrack_zone *zone) | |
967 | { | |
5e8018fc DB |
968 | nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID, |
969 | NF_CT_DEFAULT_ZONE_DIR, 0); | |
deedb590 DB |
970 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
971 | if (attr) | |
972 | zone->id = ntohs(nla_get_be16(attr)); | |
973 | #else | |
974 | if (attr) | |
975 | return -EOPNOTSUPP; | |
976 | #endif | |
977 | return 0; | |
978 | } | |
979 | ||
980 | static int | |
981 | ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type, | |
982 | struct nf_conntrack_zone *zone) | |
983 | { | |
984 | int ret; | |
985 | ||
986 | if (zone->id != NF_CT_DEFAULT_ZONE_ID) | |
987 | return -EINVAL; | |
988 | ||
989 | ret = ctnetlink_parse_zone(attr, zone); | |
990 | if (ret < 0) | |
991 | return ret; | |
992 | ||
993 | if (type == CTA_TUPLE_REPLY) | |
994 | zone->dir = NF_CT_ZONE_DIR_REPL; | |
995 | else | |
996 | zone->dir = NF_CT_ZONE_DIR_ORIG; | |
997 | ||
998 | return 0; | |
999 | } | |
1000 | ||
d0b0268f PM |
1001 | static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = { |
1002 | [CTA_TUPLE_IP] = { .type = NLA_NESTED }, | |
1003 | [CTA_TUPLE_PROTO] = { .type = NLA_NESTED }, | |
deedb590 | 1004 | [CTA_TUPLE_ZONE] = { .type = NLA_U16 }, |
d0b0268f PM |
1005 | }; |
1006 | ||
bb5cf80e | 1007 | static int |
39938324 PM |
1008 | ctnetlink_parse_tuple(const struct nlattr * const cda[], |
1009 | struct nf_conntrack_tuple *tuple, | |
deedb590 DB |
1010 | enum ctattr_type type, u_int8_t l3num, |
1011 | struct nf_conntrack_zone *zone) | |
c1d10adb | 1012 | { |
df6fb868 | 1013 | struct nlattr *tb[CTA_TUPLE_MAX+1]; |
c1d10adb PNA |
1014 | int err; |
1015 | ||
c1d10adb PNA |
1016 | memset(tuple, 0, sizeof(*tuple)); |
1017 | ||
130ffbc2 DB |
1018 | err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy); |
1019 | if (err < 0) | |
1020 | return err; | |
c1d10adb | 1021 | |
df6fb868 | 1022 | if (!tb[CTA_TUPLE_IP]) |
c1d10adb PNA |
1023 | return -EINVAL; |
1024 | ||
1025 | tuple->src.l3num = l3num; | |
1026 | ||
df6fb868 | 1027 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple); |
c1d10adb PNA |
1028 | if (err < 0) |
1029 | return err; | |
1030 | ||
df6fb868 | 1031 | if (!tb[CTA_TUPLE_PROTO]) |
c1d10adb PNA |
1032 | return -EINVAL; |
1033 | ||
df6fb868 | 1034 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple); |
c1d10adb PNA |
1035 | if (err < 0) |
1036 | return err; | |
1037 | ||
deedb590 DB |
1038 | if (tb[CTA_TUPLE_ZONE]) { |
1039 | if (!zone) | |
1040 | return -EINVAL; | |
1041 | ||
1042 | err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE], | |
1043 | type, zone); | |
1044 | if (err < 0) | |
1045 | return err; | |
1046 | } | |
1047 | ||
c1d10adb PNA |
1048 | /* orig and expect tuples get DIR_ORIGINAL */ |
1049 | if (type == CTA_TUPLE_REPLY) | |
1050 | tuple->dst.dir = IP_CT_DIR_REPLY; | |
1051 | else | |
1052 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
1053 | ||
c1d10adb PNA |
1054 | return 0; |
1055 | } | |
1056 | ||
d0b0268f | 1057 | static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = { |
6d1fafca FW |
1058 | [CTA_HELP_NAME] = { .type = NLA_NUL_STRING, |
1059 | .len = NF_CT_HELPER_NAME_LEN - 1 }, | |
d0b0268f PM |
1060 | }; |
1061 | ||
4054ff45 PNA |
1062 | static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name, |
1063 | struct nlattr **helpinfo) | |
c1d10adb | 1064 | { |
130ffbc2 | 1065 | int err; |
df6fb868 | 1066 | struct nlattr *tb[CTA_HELP_MAX+1]; |
c1d10adb | 1067 | |
130ffbc2 DB |
1068 | err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy); |
1069 | if (err < 0) | |
1070 | return err; | |
c1d10adb | 1071 | |
df6fb868 | 1072 | if (!tb[CTA_HELP_NAME]) |
c1d10adb PNA |
1073 | return -EINVAL; |
1074 | ||
df6fb868 | 1075 | *helper_name = nla_data(tb[CTA_HELP_NAME]); |
c1d10adb | 1076 | |
ae243bee PNA |
1077 | if (tb[CTA_HELP_INFO]) |
1078 | *helpinfo = tb[CTA_HELP_INFO]; | |
1079 | ||
c1d10adb PNA |
1080 | return 0; |
1081 | } | |
1082 | ||
f73e924c | 1083 | static const struct nla_policy ct_nla_policy[CTA_MAX+1] = { |
d0b0268f PM |
1084 | [CTA_TUPLE_ORIG] = { .type = NLA_NESTED }, |
1085 | [CTA_TUPLE_REPLY] = { .type = NLA_NESTED }, | |
f73e924c | 1086 | [CTA_STATUS] = { .type = NLA_U32 }, |
d0b0268f PM |
1087 | [CTA_PROTOINFO] = { .type = NLA_NESTED }, |
1088 | [CTA_HELP] = { .type = NLA_NESTED }, | |
1089 | [CTA_NAT_SRC] = { .type = NLA_NESTED }, | |
f73e924c PM |
1090 | [CTA_TIMEOUT] = { .type = NLA_U32 }, |
1091 | [CTA_MARK] = { .type = NLA_U32 }, | |
f73e924c | 1092 | [CTA_ID] = { .type = NLA_U32 }, |
d0b0268f PM |
1093 | [CTA_NAT_DST] = { .type = NLA_NESTED }, |
1094 | [CTA_TUPLE_MASTER] = { .type = NLA_NESTED }, | |
6d1fafca FW |
1095 | [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED }, |
1096 | [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED }, | |
ef00f89f | 1097 | [CTA_ZONE] = { .type = NLA_U16 }, |
0f298a28 | 1098 | [CTA_MARK_MASK] = { .type = NLA_U32 }, |
9b21f6a9 | 1099 | [CTA_LABELS] = { .type = NLA_BINARY, |
d2bf2f34 | 1100 | .len = NF_CT_LABELS_MAX_SIZE }, |
9b21f6a9 | 1101 | [CTA_LABELS_MASK] = { .type = NLA_BINARY, |
d2bf2f34 | 1102 | .len = NF_CT_LABELS_MAX_SIZE }, |
c1d10adb PNA |
1103 | }; |
1104 | ||
866476f3 KE |
1105 | static int ctnetlink_flush_conntrack(struct net *net, |
1106 | const struct nlattr * const cda[], | |
1107 | u32 portid, int report) | |
1108 | { | |
1109 | struct ctnetlink_filter *filter = NULL; | |
1110 | ||
1111 | if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) { | |
1112 | filter = ctnetlink_alloc_filter(cda); | |
1113 | if (IS_ERR(filter)) | |
1114 | return PTR_ERR(filter); | |
1115 | } | |
1116 | ||
1117 | nf_ct_iterate_cleanup(net, ctnetlink_filter_match, filter, | |
1118 | portid, report); | |
1119 | kfree(filter); | |
1120 | ||
1121 | return 0; | |
1122 | } | |
1123 | ||
7b8002a1 PNA |
1124 | static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl, |
1125 | struct sk_buff *skb, | |
1126 | const struct nlmsghdr *nlh, | |
1127 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1128 | { |
1129 | struct nf_conntrack_tuple_hash *h; | |
1130 | struct nf_conntrack_tuple tuple; | |
1131 | struct nf_conn *ct; | |
96bcf938 | 1132 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 1133 | u_int8_t u3 = nfmsg->nfgen_family; |
308ac914 | 1134 | struct nf_conntrack_zone zone; |
ef00f89f PM |
1135 | int err; |
1136 | ||
1137 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); | |
1138 | if (err < 0) | |
1139 | return err; | |
c1d10adb | 1140 | |
df6fb868 | 1141 | if (cda[CTA_TUPLE_ORIG]) |
deedb590 DB |
1142 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, |
1143 | u3, &zone); | |
df6fb868 | 1144 | else if (cda[CTA_TUPLE_REPLY]) |
deedb590 DB |
1145 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, |
1146 | u3, &zone); | |
c1d10adb | 1147 | else { |
866476f3 KE |
1148 | return ctnetlink_flush_conntrack(net, cda, |
1149 | NETLINK_CB(skb).portid, | |
1150 | nlmsg_report(nlh)); | |
c1d10adb PNA |
1151 | } |
1152 | ||
1153 | if (err < 0) | |
1154 | return err; | |
1155 | ||
308ac914 | 1156 | h = nf_conntrack_find_get(net, &zone, &tuple); |
9ea8cfd6 | 1157 | if (!h) |
c1d10adb | 1158 | return -ENOENT; |
c1d10adb PNA |
1159 | |
1160 | ct = nf_ct_tuplehash_to_ctrack(h); | |
601e68e1 | 1161 | |
df6fb868 | 1162 | if (cda[CTA_ID]) { |
77236b6e | 1163 | u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID])); |
7f85f914 | 1164 | if (id != (u32)(unsigned long)ct) { |
c1d10adb PNA |
1165 | nf_ct_put(ct); |
1166 | return -ENOENT; | |
1167 | } | |
601e68e1 | 1168 | } |
c1d10adb | 1169 | |
f330a7fd | 1170 | nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh)); |
c1d10adb | 1171 | nf_ct_put(ct); |
c1d10adb PNA |
1172 | |
1173 | return 0; | |
1174 | } | |
1175 | ||
7b8002a1 PNA |
1176 | static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl, |
1177 | struct sk_buff *skb, | |
1178 | const struct nlmsghdr *nlh, | |
1179 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1180 | { |
1181 | struct nf_conntrack_tuple_hash *h; | |
1182 | struct nf_conntrack_tuple tuple; | |
1183 | struct nf_conn *ct; | |
1184 | struct sk_buff *skb2 = NULL; | |
96bcf938 | 1185 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 1186 | u_int8_t u3 = nfmsg->nfgen_family; |
308ac914 | 1187 | struct nf_conntrack_zone zone; |
ef00f89f | 1188 | int err; |
c1d10adb | 1189 | |
80d326fa PNA |
1190 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
1191 | struct netlink_dump_control c = { | |
1192 | .dump = ctnetlink_dump_table, | |
1193 | .done = ctnetlink_done, | |
1194 | }; | |
866476f3 | 1195 | |
0f298a28 | 1196 | if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) { |
866476f3 | 1197 | struct ctnetlink_filter *filter; |
0f298a28 | 1198 | |
866476f3 KE |
1199 | filter = ctnetlink_alloc_filter(cda); |
1200 | if (IS_ERR(filter)) | |
1201 | return PTR_ERR(filter); | |
0f298a28 | 1202 | |
0f298a28 PNA |
1203 | c.data = filter; |
1204 | } | |
80d326fa PNA |
1205 | return netlink_dump_start(ctnl, skb, nlh, &c); |
1206 | } | |
c1d10adb | 1207 | |
ef00f89f PM |
1208 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); |
1209 | if (err < 0) | |
1210 | return err; | |
1211 | ||
df6fb868 | 1212 | if (cda[CTA_TUPLE_ORIG]) |
deedb590 DB |
1213 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, |
1214 | u3, &zone); | |
df6fb868 | 1215 | else if (cda[CTA_TUPLE_REPLY]) |
deedb590 DB |
1216 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, |
1217 | u3, &zone); | |
c1d10adb PNA |
1218 | else |
1219 | return -EINVAL; | |
1220 | ||
1221 | if (err < 0) | |
1222 | return err; | |
1223 | ||
308ac914 | 1224 | h = nf_conntrack_find_get(net, &zone, &tuple); |
9ea8cfd6 | 1225 | if (!h) |
c1d10adb | 1226 | return -ENOENT; |
9ea8cfd6 | 1227 | |
c1d10adb PNA |
1228 | ct = nf_ct_tuplehash_to_ctrack(h); |
1229 | ||
1230 | err = -ENOMEM; | |
96bcf938 PNA |
1231 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1232 | if (skb2 == NULL) { | |
c1d10adb PNA |
1233 | nf_ct_put(ct); |
1234 | return -ENOMEM; | |
1235 | } | |
c1d10adb | 1236 | |
528a3a6f | 1237 | rcu_read_lock(); |
15e47304 | 1238 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, |
80e60e67 | 1239 | NFNL_MSG_TYPE(nlh->nlmsg_type), ct); |
528a3a6f | 1240 | rcu_read_unlock(); |
c1d10adb PNA |
1241 | nf_ct_put(ct); |
1242 | if (err <= 0) | |
1243 | goto free; | |
1244 | ||
15e47304 | 1245 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT); |
c1d10adb PNA |
1246 | if (err < 0) |
1247 | goto out; | |
1248 | ||
c1d10adb PNA |
1249 | return 0; |
1250 | ||
1251 | free: | |
1252 | kfree_skb(skb2); | |
1253 | out: | |
f31e8d49 PNA |
1254 | /* this avoids a loop in nfnetlink. */ |
1255 | return err == -EAGAIN ? -ENOBUFS : err; | |
c1d10adb PNA |
1256 | } |
1257 | ||
d871befe PNA |
1258 | static int ctnetlink_done_list(struct netlink_callback *cb) |
1259 | { | |
1260 | if (cb->args[1]) | |
1261 | nf_ct_put((struct nf_conn *)cb->args[1]); | |
1262 | return 0; | |
1263 | } | |
1264 | ||
1265 | static int | |
b7779d06 | 1266 | ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying) |
d871befe | 1267 | { |
cd5f336f | 1268 | struct nf_conn *ct, *last; |
d871befe PNA |
1269 | struct nf_conntrack_tuple_hash *h; |
1270 | struct hlist_nulls_node *n; | |
1271 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
1272 | u_int8_t l3proto = nfmsg->nfgen_family; | |
1273 | int res; | |
b7779d06 JDB |
1274 | int cpu; |
1275 | struct hlist_nulls_head *list; | |
1276 | struct net *net = sock_net(skb->sk); | |
d871befe PNA |
1277 | |
1278 | if (cb->args[2]) | |
1279 | return 0; | |
1280 | ||
cd5f336f FW |
1281 | last = (struct nf_conn *)cb->args[1]; |
1282 | ||
b7779d06 JDB |
1283 | for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) { |
1284 | struct ct_pcpu *pcpu; | |
1285 | ||
1286 | if (!cpu_possible(cpu)) | |
d871befe | 1287 | continue; |
b7779d06 JDB |
1288 | |
1289 | pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu); | |
1290 | spin_lock_bh(&pcpu->lock); | |
b7779d06 JDB |
1291 | list = dying ? &pcpu->dying : &pcpu->unconfirmed; |
1292 | restart: | |
1293 | hlist_nulls_for_each_entry(h, n, list, hnnode) { | |
1294 | ct = nf_ct_tuplehash_to_ctrack(h); | |
1295 | if (l3proto && nf_ct_l3num(ct) != l3proto) | |
d871befe | 1296 | continue; |
b7779d06 JDB |
1297 | if (cb->args[1]) { |
1298 | if (ct != last) | |
1299 | continue; | |
1300 | cb->args[1] = 0; | |
1301 | } | |
1302 | rcu_read_lock(); | |
1303 | res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid, | |
1304 | cb->nlh->nlmsg_seq, | |
1305 | NFNL_MSG_TYPE(cb->nlh->nlmsg_type), | |
1306 | ct); | |
1307 | rcu_read_unlock(); | |
1308 | if (res < 0) { | |
cd5f336f FW |
1309 | if (!atomic_inc_not_zero(&ct->ct_general.use)) |
1310 | continue; | |
266155b2 | 1311 | cb->args[0] = cpu; |
b7779d06 JDB |
1312 | cb->args[1] = (unsigned long)ct; |
1313 | spin_unlock_bh(&pcpu->lock); | |
1314 | goto out; | |
1315 | } | |
d871befe | 1316 | } |
b7779d06 JDB |
1317 | if (cb->args[1]) { |
1318 | cb->args[1] = 0; | |
1319 | goto restart; | |
266155b2 | 1320 | } |
b7779d06 | 1321 | spin_unlock_bh(&pcpu->lock); |
d871befe | 1322 | } |
266155b2 | 1323 | cb->args[2] = 1; |
d871befe | 1324 | out: |
d871befe PNA |
1325 | if (last) |
1326 | nf_ct_put(last); | |
1327 | ||
1328 | return skb->len; | |
1329 | } | |
1330 | ||
1331 | static int | |
1332 | ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb) | |
1333 | { | |
b7779d06 | 1334 | return ctnetlink_dump_list(skb, cb, true); |
d871befe PNA |
1335 | } |
1336 | ||
7b8002a1 PNA |
1337 | static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl, |
1338 | struct sk_buff *skb, | |
1339 | const struct nlmsghdr *nlh, | |
1340 | const struct nlattr * const cda[]) | |
d871befe PNA |
1341 | { |
1342 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1343 | struct netlink_dump_control c = { | |
1344 | .dump = ctnetlink_dump_dying, | |
1345 | .done = ctnetlink_done_list, | |
1346 | }; | |
1347 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
1348 | } | |
1349 | ||
1350 | return -EOPNOTSUPP; | |
1351 | } | |
1352 | ||
1353 | static int | |
1354 | ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb) | |
1355 | { | |
b7779d06 | 1356 | return ctnetlink_dump_list(skb, cb, false); |
d871befe PNA |
1357 | } |
1358 | ||
7b8002a1 PNA |
1359 | static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl, |
1360 | struct sk_buff *skb, | |
1361 | const struct nlmsghdr *nlh, | |
1362 | const struct nlattr * const cda[]) | |
d871befe PNA |
1363 | { |
1364 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1365 | struct netlink_dump_control c = { | |
1366 | .dump = ctnetlink_dump_unconfirmed, | |
1367 | .done = ctnetlink_done_list, | |
1368 | }; | |
1369 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
1370 | } | |
1371 | ||
1372 | return -EOPNOTSUPP; | |
1373 | } | |
1374 | ||
67671841 | 1375 | #ifdef CONFIG_NF_NAT_NEEDED |
e6a7d3c0 PNA |
1376 | static int |
1377 | ctnetlink_parse_nat_setup(struct nf_conn *ct, | |
1378 | enum nf_nat_manip_type manip, | |
39938324 | 1379 | const struct nlattr *attr) |
e6a7d3c0 PNA |
1380 | { |
1381 | typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup; | |
c7232c99 | 1382 | int err; |
e6a7d3c0 PNA |
1383 | |
1384 | parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook); | |
1385 | if (!parse_nat_setup) { | |
95a5afca | 1386 | #ifdef CONFIG_MODULES |
e6a7d3c0 | 1387 | rcu_read_unlock(); |
c14b78e7 | 1388 | nfnl_unlock(NFNL_SUBSYS_CTNETLINK); |
c7232c99 | 1389 | if (request_module("nf-nat") < 0) { |
c14b78e7 | 1390 | nfnl_lock(NFNL_SUBSYS_CTNETLINK); |
e6a7d3c0 PNA |
1391 | rcu_read_lock(); |
1392 | return -EOPNOTSUPP; | |
1393 | } | |
c14b78e7 | 1394 | nfnl_lock(NFNL_SUBSYS_CTNETLINK); |
e6a7d3c0 PNA |
1395 | rcu_read_lock(); |
1396 | if (nfnetlink_parse_nat_setup_hook) | |
1397 | return -EAGAIN; | |
1398 | #endif | |
1399 | return -EOPNOTSUPP; | |
1400 | } | |
1401 | ||
c7232c99 PM |
1402 | err = parse_nat_setup(ct, manip, attr); |
1403 | if (err == -EAGAIN) { | |
1404 | #ifdef CONFIG_MODULES | |
1405 | rcu_read_unlock(); | |
c14b78e7 | 1406 | nfnl_unlock(NFNL_SUBSYS_CTNETLINK); |
c7232c99 | 1407 | if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) { |
c14b78e7 | 1408 | nfnl_lock(NFNL_SUBSYS_CTNETLINK); |
c7232c99 PM |
1409 | rcu_read_lock(); |
1410 | return -EOPNOTSUPP; | |
1411 | } | |
c14b78e7 | 1412 | nfnl_lock(NFNL_SUBSYS_CTNETLINK); |
c7232c99 PM |
1413 | rcu_read_lock(); |
1414 | #else | |
1415 | err = -EOPNOTSUPP; | |
1416 | #endif | |
1417 | } | |
1418 | return err; | |
e6a7d3c0 | 1419 | } |
67671841 | 1420 | #endif |
e6a7d3c0 | 1421 | |
bb5cf80e | 1422 | static int |
39938324 | 1423 | ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
1424 | { |
1425 | unsigned long d; | |
77236b6e | 1426 | unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS])); |
c1d10adb PNA |
1427 | d = ct->status ^ status; |
1428 | ||
1429 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) | |
1430 | /* unchangeable */ | |
0adf9d67 | 1431 | return -EBUSY; |
601e68e1 | 1432 | |
c1d10adb PNA |
1433 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) |
1434 | /* SEEN_REPLY bit can only be set */ | |
0adf9d67 | 1435 | return -EBUSY; |
601e68e1 | 1436 | |
c1d10adb PNA |
1437 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) |
1438 | /* ASSURED bit can only be set */ | |
0adf9d67 | 1439 | return -EBUSY; |
c1d10adb | 1440 | |
c1d10adb PNA |
1441 | /* Be careful here, modifying NAT bits can screw up things, |
1442 | * so don't let users modify them directly if they don't pass | |
5b1158e9 | 1443 | * nf_nat_range. */ |
c1d10adb PNA |
1444 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); |
1445 | return 0; | |
1446 | } | |
1447 | ||
e6a7d3c0 | 1448 | static int |
0eba801b | 1449 | ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[]) |
e6a7d3c0 PNA |
1450 | { |
1451 | #ifdef CONFIG_NF_NAT_NEEDED | |
1452 | int ret; | |
1453 | ||
fe337ac2 FW |
1454 | if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC]) |
1455 | return 0; | |
1456 | ||
0eba801b PNA |
1457 | ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST, |
1458 | cda[CTA_NAT_DST]); | |
1459 | if (ret < 0) | |
1460 | return ret; | |
1461 | ||
1462 | ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC, | |
1463 | cda[CTA_NAT_SRC]); | |
1464 | return ret; | |
e6a7d3c0 | 1465 | #else |
0eba801b PNA |
1466 | if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC]) |
1467 | return 0; | |
e6a7d3c0 PNA |
1468 | return -EOPNOTSUPP; |
1469 | #endif | |
1470 | } | |
c1d10adb | 1471 | |
4054ff45 PNA |
1472 | static int ctnetlink_change_helper(struct nf_conn *ct, |
1473 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1474 | { |
1475 | struct nf_conntrack_helper *helper; | |
dc808fe2 | 1476 | struct nf_conn_help *help = nfct_help(ct); |
29fe1b48 | 1477 | char *helpname = NULL; |
ae243bee | 1478 | struct nlattr *helpinfo = NULL; |
c1d10adb PNA |
1479 | int err; |
1480 | ||
ae243bee | 1481 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo); |
c1d10adb PNA |
1482 | if (err < 0) |
1483 | return err; | |
1484 | ||
f95d7a46 KC |
1485 | /* don't change helper of sibling connections */ |
1486 | if (ct->master) { | |
1487 | /* If we try to change the helper to the same thing twice, | |
1488 | * treat the second attempt as a no-op instead of returning | |
1489 | * an error. | |
1490 | */ | |
3173d5b8 LZ |
1491 | err = -EBUSY; |
1492 | if (help) { | |
1493 | rcu_read_lock(); | |
1494 | helper = rcu_dereference(help->helper); | |
1495 | if (helper && !strcmp(helper->name, helpname)) | |
1496 | err = 0; | |
1497 | rcu_read_unlock(); | |
1498 | } | |
1499 | ||
1500 | return err; | |
f95d7a46 KC |
1501 | } |
1502 | ||
df293bbb YK |
1503 | if (!strcmp(helpname, "")) { |
1504 | if (help && help->helper) { | |
c1d10adb PNA |
1505 | /* we had a helper before ... */ |
1506 | nf_ct_remove_expectations(ct); | |
a9b3cd7f | 1507 | RCU_INIT_POINTER(help->helper, NULL); |
c1d10adb | 1508 | } |
df293bbb YK |
1509 | |
1510 | return 0; | |
c1d10adb | 1511 | } |
601e68e1 | 1512 | |
794e6871 PM |
1513 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1514 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1515 | if (helper == NULL) { |
1516 | #ifdef CONFIG_MODULES | |
ca7433df | 1517 | spin_unlock_bh(&nf_conntrack_expect_lock); |
226c0c0e PNA |
1518 | |
1519 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
ca7433df | 1520 | spin_lock_bh(&nf_conntrack_expect_lock); |
226c0c0e PNA |
1521 | return -EOPNOTSUPP; |
1522 | } | |
1523 | ||
ca7433df | 1524 | spin_lock_bh(&nf_conntrack_expect_lock); |
794e6871 PM |
1525 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1526 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1527 | if (helper) |
1528 | return -EAGAIN; | |
1529 | #endif | |
0adf9d67 | 1530 | return -EOPNOTSUPP; |
226c0c0e | 1531 | } |
df293bbb | 1532 | |
ceceae1b | 1533 | if (help) { |
ae243bee PNA |
1534 | if (help->helper == helper) { |
1535 | /* update private helper data if allowed. */ | |
7be54ca4 | 1536 | if (helper->from_nlattr) |
ae243bee | 1537 | helper->from_nlattr(helpinfo, ct); |
ceceae1b | 1538 | return 0; |
fd7462de | 1539 | } else |
ceceae1b | 1540 | return -EBUSY; |
ceceae1b | 1541 | } |
df293bbb | 1542 | |
fd7462de PNA |
1543 | /* we cannot set a helper for an existing conntrack */ |
1544 | return -EOPNOTSUPP; | |
c1d10adb PNA |
1545 | } |
1546 | ||
4054ff45 PNA |
1547 | static int ctnetlink_change_timeout(struct nf_conn *ct, |
1548 | const struct nlattr * const cda[]) | |
c1d10adb | 1549 | { |
77236b6e | 1550 | u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
601e68e1 | 1551 | |
f330a7fd | 1552 | ct->timeout = nfct_time_stamp + timeout * HZ; |
c1d10adb | 1553 | |
f330a7fd FW |
1554 | if (test_bit(IPS_DYING_BIT, &ct->status)) |
1555 | return -ETIME; | |
c1d10adb PNA |
1556 | |
1557 | return 0; | |
1558 | } | |
1559 | ||
d0b0268f PM |
1560 | static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = { |
1561 | [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED }, | |
1562 | [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED }, | |
1563 | [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED }, | |
1564 | }; | |
1565 | ||
4054ff45 PNA |
1566 | static int ctnetlink_change_protoinfo(struct nf_conn *ct, |
1567 | const struct nlattr * const cda[]) | |
c1d10adb | 1568 | { |
39938324 PM |
1569 | const struct nlattr *attr = cda[CTA_PROTOINFO]; |
1570 | struct nlattr *tb[CTA_PROTOINFO_MAX+1]; | |
605dcad6 | 1571 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
1572 | int err = 0; |
1573 | ||
130ffbc2 DB |
1574 | err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy); |
1575 | if (err < 0) | |
1576 | return err; | |
c1d10adb | 1577 | |
cd91566e FW |
1578 | rcu_read_lock(); |
1579 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
fdf70832 PM |
1580 | if (l4proto->from_nlattr) |
1581 | err = l4proto->from_nlattr(tb, ct); | |
cd91566e | 1582 | rcu_read_unlock(); |
c1d10adb PNA |
1583 | |
1584 | return err; | |
1585 | } | |
1586 | ||
41d73ec0 PM |
1587 | static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = { |
1588 | [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 }, | |
1589 | [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 }, | |
1590 | [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 }, | |
d0b0268f PM |
1591 | }; |
1592 | ||
4054ff45 PNA |
1593 | static int change_seq_adj(struct nf_ct_seqadj *seq, |
1594 | const struct nlattr * const attr) | |
13eae15a | 1595 | { |
130ffbc2 | 1596 | int err; |
41d73ec0 | 1597 | struct nlattr *cda[CTA_SEQADJ_MAX+1]; |
13eae15a | 1598 | |
41d73ec0 | 1599 | err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy); |
130ffbc2 DB |
1600 | if (err < 0) |
1601 | return err; | |
13eae15a | 1602 | |
41d73ec0 | 1603 | if (!cda[CTA_SEQADJ_CORRECTION_POS]) |
13eae15a PNA |
1604 | return -EINVAL; |
1605 | ||
41d73ec0 PM |
1606 | seq->correction_pos = |
1607 | ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS])); | |
13eae15a | 1608 | |
41d73ec0 | 1609 | if (!cda[CTA_SEQADJ_OFFSET_BEFORE]) |
13eae15a PNA |
1610 | return -EINVAL; |
1611 | ||
41d73ec0 PM |
1612 | seq->offset_before = |
1613 | ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE])); | |
13eae15a | 1614 | |
41d73ec0 | 1615 | if (!cda[CTA_SEQADJ_OFFSET_AFTER]) |
13eae15a PNA |
1616 | return -EINVAL; |
1617 | ||
41d73ec0 PM |
1618 | seq->offset_after = |
1619 | ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER])); | |
13eae15a PNA |
1620 | |
1621 | return 0; | |
1622 | } | |
1623 | ||
1624 | static int | |
41d73ec0 PM |
1625 | ctnetlink_change_seq_adj(struct nf_conn *ct, |
1626 | const struct nlattr * const cda[]) | |
13eae15a | 1627 | { |
41d73ec0 | 1628 | struct nf_conn_seqadj *seqadj = nfct_seqadj(ct); |
13eae15a | 1629 | int ret = 0; |
13eae15a | 1630 | |
41d73ec0 | 1631 | if (!seqadj) |
13eae15a PNA |
1632 | return 0; |
1633 | ||
41d73ec0 PM |
1634 | if (cda[CTA_SEQ_ADJ_ORIG]) { |
1635 | ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL], | |
1636 | cda[CTA_SEQ_ADJ_ORIG]); | |
13eae15a PNA |
1637 | if (ret < 0) |
1638 | return ret; | |
1639 | ||
1640 | ct->status |= IPS_SEQ_ADJUST; | |
1641 | } | |
1642 | ||
41d73ec0 PM |
1643 | if (cda[CTA_SEQ_ADJ_REPLY]) { |
1644 | ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY], | |
1645 | cda[CTA_SEQ_ADJ_REPLY]); | |
13eae15a PNA |
1646 | if (ret < 0) |
1647 | return ret; | |
1648 | ||
1649 | ct->status |= IPS_SEQ_ADJUST; | |
1650 | } | |
1651 | ||
1652 | return 0; | |
1653 | } | |
13eae15a | 1654 | |
9b21f6a9 FW |
1655 | static int |
1656 | ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[]) | |
1657 | { | |
1658 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
1659 | size_t len = nla_len(cda[CTA_LABELS]); | |
1660 | const void *mask = cda[CTA_LABELS_MASK]; | |
1661 | ||
1662 | if (len & (sizeof(u32)-1)) /* must be multiple of u32 */ | |
1663 | return -EINVAL; | |
1664 | ||
1665 | if (mask) { | |
1666 | if (nla_len(cda[CTA_LABELS_MASK]) == 0 || | |
1667 | nla_len(cda[CTA_LABELS_MASK]) != len) | |
1668 | return -EINVAL; | |
1669 | mask = nla_data(cda[CTA_LABELS_MASK]); | |
1670 | } | |
1671 | ||
1672 | len /= sizeof(u32); | |
1673 | ||
1674 | return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len); | |
1675 | #else | |
1676 | return -EOPNOTSUPP; | |
1677 | #endif | |
1678 | } | |
1679 | ||
c1d10adb | 1680 | static int |
39938324 PM |
1681 | ctnetlink_change_conntrack(struct nf_conn *ct, |
1682 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1683 | { |
1684 | int err; | |
1685 | ||
e098360f PNA |
1686 | /* only allow NAT changes and master assignation for new conntracks */ |
1687 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER]) | |
1688 | return -EOPNOTSUPP; | |
1689 | ||
df6fb868 | 1690 | if (cda[CTA_HELP]) { |
c1d10adb PNA |
1691 | err = ctnetlink_change_helper(ct, cda); |
1692 | if (err < 0) | |
1693 | return err; | |
1694 | } | |
1695 | ||
df6fb868 | 1696 | if (cda[CTA_TIMEOUT]) { |
c1d10adb PNA |
1697 | err = ctnetlink_change_timeout(ct, cda); |
1698 | if (err < 0) | |
1699 | return err; | |
1700 | } | |
1701 | ||
df6fb868 | 1702 | if (cda[CTA_STATUS]) { |
c1d10adb PNA |
1703 | err = ctnetlink_change_status(ct, cda); |
1704 | if (err < 0) | |
1705 | return err; | |
1706 | } | |
1707 | ||
df6fb868 | 1708 | if (cda[CTA_PROTOINFO]) { |
c1d10adb PNA |
1709 | err = ctnetlink_change_protoinfo(ct, cda); |
1710 | if (err < 0) | |
1711 | return err; | |
1712 | } | |
1713 | ||
bcd1e830 | 1714 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1715 | if (cda[CTA_MARK]) |
77236b6e | 1716 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1717 | #endif |
1718 | ||
41d73ec0 PM |
1719 | if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) { |
1720 | err = ctnetlink_change_seq_adj(ct, cda); | |
13eae15a PNA |
1721 | if (err < 0) |
1722 | return err; | |
1723 | } | |
41d73ec0 | 1724 | |
9b21f6a9 FW |
1725 | if (cda[CTA_LABELS]) { |
1726 | err = ctnetlink_attach_labels(ct, cda); | |
1727 | if (err < 0) | |
1728 | return err; | |
1729 | } | |
13eae15a | 1730 | |
c1d10adb PNA |
1731 | return 0; |
1732 | } | |
1733 | ||
f0a3c086 | 1734 | static struct nf_conn * |
308ac914 DB |
1735 | ctnetlink_create_conntrack(struct net *net, |
1736 | const struct nf_conntrack_zone *zone, | |
9592a5c0 | 1737 | const struct nlattr * const cda[], |
c1d10adb | 1738 | struct nf_conntrack_tuple *otuple, |
5faa1f4c | 1739 | struct nf_conntrack_tuple *rtuple, |
7ec47496 | 1740 | u8 u3) |
c1d10adb PNA |
1741 | { |
1742 | struct nf_conn *ct; | |
1743 | int err = -EINVAL; | |
ceceae1b | 1744 | struct nf_conntrack_helper *helper; |
315c34da | 1745 | struct nf_conn_tstamp *tstamp; |
c1d10adb | 1746 | |
ef00f89f | 1747 | ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC); |
cd7fcbf1 | 1748 | if (IS_ERR(ct)) |
f0a3c086 | 1749 | return ERR_PTR(-ENOMEM); |
c1d10adb | 1750 | |
df6fb868 | 1751 | if (!cda[CTA_TIMEOUT]) |
0f5b3e85 | 1752 | goto err1; |
c1d10adb | 1753 | |
f330a7fd | 1754 | ct->timeout = nfct_time_stamp + ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ; |
c1d10adb | 1755 | |
1575e7ea | 1756 | rcu_read_lock(); |
226c0c0e | 1757 | if (cda[CTA_HELP]) { |
29fe1b48 | 1758 | char *helpname = NULL; |
ae243bee PNA |
1759 | struct nlattr *helpinfo = NULL; |
1760 | ||
1761 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo); | |
0f5b3e85 PM |
1762 | if (err < 0) |
1763 | goto err2; | |
226c0c0e | 1764 | |
794e6871 PM |
1765 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1766 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1767 | if (helper == NULL) { |
1768 | rcu_read_unlock(); | |
1769 | #ifdef CONFIG_MODULES | |
1770 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1771 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1772 | goto err1; |
226c0c0e PNA |
1773 | } |
1774 | ||
1775 | rcu_read_lock(); | |
794e6871 PM |
1776 | helper = __nf_conntrack_helper_find(helpname, |
1777 | nf_ct_l3num(ct), | |
1778 | nf_ct_protonum(ct)); | |
226c0c0e | 1779 | if (helper) { |
226c0c0e | 1780 | err = -EAGAIN; |
0f5b3e85 | 1781 | goto err2; |
226c0c0e PNA |
1782 | } |
1783 | rcu_read_unlock(); | |
1784 | #endif | |
1785 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1786 | goto err1; |
226c0c0e PNA |
1787 | } else { |
1788 | struct nf_conn_help *help; | |
1789 | ||
1afc5679 | 1790 | help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC); |
226c0c0e | 1791 | if (help == NULL) { |
226c0c0e | 1792 | err = -ENOMEM; |
0f5b3e85 | 1793 | goto err2; |
226c0c0e | 1794 | } |
ae243bee | 1795 | /* set private helper data if allowed. */ |
7be54ca4 | 1796 | if (helper->from_nlattr) |
ae243bee | 1797 | helper->from_nlattr(helpinfo, ct); |
226c0c0e PNA |
1798 | |
1799 | /* not in hash table yet so not strictly necessary */ | |
a9b3cd7f | 1800 | RCU_INIT_POINTER(help->helper, helper); |
226c0c0e PNA |
1801 | } |
1802 | } else { | |
1803 | /* try an implicit helper assignation */ | |
b2a15a60 | 1804 | err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC); |
0f5b3e85 PM |
1805 | if (err < 0) |
1806 | goto err2; | |
1575e7ea PNA |
1807 | } |
1808 | ||
0eba801b PNA |
1809 | err = ctnetlink_setup_nat(ct, cda); |
1810 | if (err < 0) | |
1811 | goto err2; | |
e6a7d3c0 | 1812 | |
a88e22ad | 1813 | nf_ct_acct_ext_add(ct, GFP_ATOMIC); |
a992ca2a | 1814 | nf_ct_tstamp_ext_add(ct, GFP_ATOMIC); |
a88e22ad | 1815 | nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC); |
c539f017 FW |
1816 | nf_ct_labels_ext_add(ct); |
1817 | ||
a88e22ad PNA |
1818 | /* we must add conntrack extensions before confirmation. */ |
1819 | ct->status |= IPS_CONFIRMED; | |
1820 | ||
1821 | if (cda[CTA_STATUS]) { | |
1822 | err = ctnetlink_change_status(ct, cda); | |
0f5b3e85 PM |
1823 | if (err < 0) |
1824 | goto err2; | |
bbb3357d | 1825 | } |
c1d10adb | 1826 | |
41d73ec0 PM |
1827 | if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) { |
1828 | err = ctnetlink_change_seq_adj(ct, cda); | |
0f5b3e85 PM |
1829 | if (err < 0) |
1830 | goto err2; | |
c969aa7d | 1831 | } |
c969aa7d | 1832 | |
e5fc9e7a | 1833 | memset(&ct->proto, 0, sizeof(ct->proto)); |
df6fb868 | 1834 | if (cda[CTA_PROTOINFO]) { |
c1d10adb | 1835 | err = ctnetlink_change_protoinfo(ct, cda); |
0f5b3e85 PM |
1836 | if (err < 0) |
1837 | goto err2; | |
c1d10adb PNA |
1838 | } |
1839 | ||
bcd1e830 | 1840 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1841 | if (cda[CTA_MARK]) |
77236b6e | 1842 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1843 | #endif |
1844 | ||
5faa1f4c | 1845 | /* setup master conntrack: this is a confirmed expectation */ |
7ec47496 PNA |
1846 | if (cda[CTA_TUPLE_MASTER]) { |
1847 | struct nf_conntrack_tuple master; | |
1848 | struct nf_conntrack_tuple_hash *master_h; | |
1849 | struct nf_conn *master_ct; | |
1850 | ||
deedb590 DB |
1851 | err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, |
1852 | u3, NULL); | |
7ec47496 | 1853 | if (err < 0) |
0f5b3e85 | 1854 | goto err2; |
7ec47496 | 1855 | |
ef00f89f | 1856 | master_h = nf_conntrack_find_get(net, zone, &master); |
7ec47496 PNA |
1857 | if (master_h == NULL) { |
1858 | err = -ENOENT; | |
0f5b3e85 | 1859 | goto err2; |
7ec47496 PNA |
1860 | } |
1861 | master_ct = nf_ct_tuplehash_to_ctrack(master_h); | |
f2a89004 | 1862 | __set_bit(IPS_EXPECTED_BIT, &ct->status); |
5faa1f4c | 1863 | ct->master = master_ct; |
f2a89004 | 1864 | } |
315c34da PNA |
1865 | tstamp = nf_conn_tstamp_find(ct); |
1866 | if (tstamp) | |
d2de875c | 1867 | tstamp->start = ktime_get_real_ns(); |
5faa1f4c | 1868 | |
7d367e06 JK |
1869 | err = nf_conntrack_hash_check_insert(ct); |
1870 | if (err < 0) | |
1871 | goto err2; | |
1872 | ||
58a3c9bb | 1873 | rcu_read_unlock(); |
dafc741c | 1874 | |
f0a3c086 | 1875 | return ct; |
c1d10adb | 1876 | |
0f5b3e85 PM |
1877 | err2: |
1878 | rcu_read_unlock(); | |
1879 | err1: | |
c1d10adb | 1880 | nf_conntrack_free(ct); |
f0a3c086 | 1881 | return ERR_PTR(err); |
c1d10adb PNA |
1882 | } |
1883 | ||
7b8002a1 PNA |
1884 | static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl, |
1885 | struct sk_buff *skb, | |
1886 | const struct nlmsghdr *nlh, | |
1887 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1888 | { |
1889 | struct nf_conntrack_tuple otuple, rtuple; | |
1890 | struct nf_conntrack_tuple_hash *h = NULL; | |
96bcf938 | 1891 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
7d367e06 | 1892 | struct nf_conn *ct; |
c1d10adb | 1893 | u_int8_t u3 = nfmsg->nfgen_family; |
308ac914 | 1894 | struct nf_conntrack_zone zone; |
ef00f89f PM |
1895 | int err; |
1896 | ||
1897 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); | |
1898 | if (err < 0) | |
1899 | return err; | |
c1d10adb | 1900 | |
df6fb868 | 1901 | if (cda[CTA_TUPLE_ORIG]) { |
deedb590 DB |
1902 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, |
1903 | u3, &zone); | |
c1d10adb PNA |
1904 | if (err < 0) |
1905 | return err; | |
1906 | } | |
1907 | ||
df6fb868 | 1908 | if (cda[CTA_TUPLE_REPLY]) { |
deedb590 DB |
1909 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, |
1910 | u3, &zone); | |
c1d10adb PNA |
1911 | if (err < 0) |
1912 | return err; | |
1913 | } | |
1914 | ||
df6fb868 | 1915 | if (cda[CTA_TUPLE_ORIG]) |
308ac914 | 1916 | h = nf_conntrack_find_get(net, &zone, &otuple); |
df6fb868 | 1917 | else if (cda[CTA_TUPLE_REPLY]) |
308ac914 | 1918 | h = nf_conntrack_find_get(net, &zone, &rtuple); |
c1d10adb PNA |
1919 | |
1920 | if (h == NULL) { | |
c1d10adb | 1921 | err = -ENOENT; |
f0a3c086 | 1922 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
fecc1133 | 1923 | enum ip_conntrack_events events; |
5faa1f4c | 1924 | |
442fad94 FW |
1925 | if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY]) |
1926 | return -EINVAL; | |
aa0c2c68 LZ |
1927 | if (otuple.dst.protonum != rtuple.dst.protonum) |
1928 | return -EINVAL; | |
442fad94 | 1929 | |
308ac914 | 1930 | ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple, |
f0a3c086 | 1931 | &rtuple, u3); |
7d367e06 JK |
1932 | if (IS_ERR(ct)) |
1933 | return PTR_ERR(ct); | |
1934 | ||
f0a3c086 | 1935 | err = 0; |
fecc1133 | 1936 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) |
97aae0df | 1937 | events = 1 << IPCT_RELATED; |
fecc1133 | 1938 | else |
97aae0df | 1939 | events = 1 << IPCT_NEW; |
fecc1133 | 1940 | |
9b21f6a9 FW |
1941 | if (cda[CTA_LABELS] && |
1942 | ctnetlink_attach_labels(ct, cda) == 0) | |
1943 | events |= (1 << IPCT_LABEL); | |
1944 | ||
858b3133 PM |
1945 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1946 | (1 << IPCT_ASSURED) | | |
a0891aa6 PNA |
1947 | (1 << IPCT_HELPER) | |
1948 | (1 << IPCT_PROTOINFO) | | |
41d73ec0 | 1949 | (1 << IPCT_SEQADJ) | |
a0891aa6 | 1950 | (1 << IPCT_MARK) | events, |
15e47304 | 1951 | ct, NETLINK_CB(skb).portid, |
a0891aa6 | 1952 | nlmsg_report(nlh)); |
f0a3c086 | 1953 | nf_ct_put(ct); |
7d367e06 | 1954 | } |
5faa1f4c | 1955 | |
c1d10adb PNA |
1956 | return err; |
1957 | } | |
1958 | /* implicit 'else' */ | |
1959 | ||
c1d10adb | 1960 | err = -EEXIST; |
7d367e06 | 1961 | ct = nf_ct_tuplehash_to_ctrack(h); |
ff4ca827 | 1962 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) { |
ca7433df | 1963 | spin_lock_bh(&nf_conntrack_expect_lock); |
19abb7b0 | 1964 | err = ctnetlink_change_conntrack(ct, cda); |
ca7433df | 1965 | spin_unlock_bh(&nf_conntrack_expect_lock); |
19abb7b0 | 1966 | if (err == 0) { |
858b3133 PM |
1967 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1968 | (1 << IPCT_ASSURED) | | |
a0891aa6 | 1969 | (1 << IPCT_HELPER) | |
797a7d66 | 1970 | (1 << IPCT_LABEL) | |
a0891aa6 | 1971 | (1 << IPCT_PROTOINFO) | |
41d73ec0 | 1972 | (1 << IPCT_SEQADJ) | |
a0891aa6 | 1973 | (1 << IPCT_MARK), |
15e47304 | 1974 | ct, NETLINK_CB(skb).portid, |
a0891aa6 | 1975 | nlmsg_report(nlh)); |
7d367e06 | 1976 | } |
ff4ca827 | 1977 | } |
c1d10adb | 1978 | |
7d367e06 | 1979 | nf_ct_put(ct); |
c1d10adb PNA |
1980 | return err; |
1981 | } | |
1982 | ||
392025f8 | 1983 | static int |
15e47304 | 1984 | ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq, |
392025f8 PNA |
1985 | __u16 cpu, const struct ip_conntrack_stat *st) |
1986 | { | |
1987 | struct nlmsghdr *nlh; | |
1988 | struct nfgenmsg *nfmsg; | |
15e47304 | 1989 | unsigned int flags = portid ? NLM_F_MULTI : 0, event; |
392025f8 PNA |
1990 | |
1991 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU); | |
15e47304 | 1992 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
392025f8 PNA |
1993 | if (nlh == NULL) |
1994 | goto nlmsg_failure; | |
1995 | ||
1996 | nfmsg = nlmsg_data(nlh); | |
1997 | nfmsg->nfgen_family = AF_UNSPEC; | |
1998 | nfmsg->version = NFNETLINK_V0; | |
1999 | nfmsg->res_id = htons(cpu); | |
2000 | ||
8e8118f8 | 2001 | if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) || |
392025f8 PNA |
2002 | nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) || |
2003 | nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) || | |
392025f8 PNA |
2004 | nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) || |
2005 | nla_put_be32(skb, CTA_STATS_INSERT_FAILED, | |
2006 | htonl(st->insert_failed)) || | |
2007 | nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) || | |
2008 | nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) || | |
2009 | nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) || | |
2010 | nla_put_be32(skb, CTA_STATS_SEARCH_RESTART, | |
2011 | htonl(st->search_restart))) | |
2012 | goto nla_put_failure; | |
2013 | ||
2014 | nlmsg_end(skb, nlh); | |
2015 | return skb->len; | |
2016 | ||
2017 | nla_put_failure: | |
2018 | nlmsg_failure: | |
2019 | nlmsg_cancel(skb, nlh); | |
2020 | return -1; | |
2021 | } | |
2022 | ||
2023 | static int | |
2024 | ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
2025 | { | |
2026 | int cpu; | |
2027 | struct net *net = sock_net(skb->sk); | |
2028 | ||
2029 | if (cb->args[0] == nr_cpu_ids) | |
2030 | return 0; | |
2031 | ||
2032 | for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) { | |
2033 | const struct ip_conntrack_stat *st; | |
2034 | ||
2035 | if (!cpu_possible(cpu)) | |
2036 | continue; | |
2037 | ||
2038 | st = per_cpu_ptr(net->ct.stat, cpu); | |
2039 | if (ctnetlink_ct_stat_cpu_fill_info(skb, | |
15e47304 | 2040 | NETLINK_CB(cb->skb).portid, |
392025f8 PNA |
2041 | cb->nlh->nlmsg_seq, |
2042 | cpu, st) < 0) | |
2043 | break; | |
2044 | } | |
2045 | cb->args[0] = cpu; | |
2046 | ||
2047 | return skb->len; | |
2048 | } | |
2049 | ||
7b8002a1 PNA |
2050 | static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl, |
2051 | struct sk_buff *skb, | |
2052 | const struct nlmsghdr *nlh, | |
2053 | const struct nlattr * const cda[]) | |
392025f8 PNA |
2054 | { |
2055 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
2056 | struct netlink_dump_control c = { | |
2057 | .dump = ctnetlink_ct_stat_cpu_dump, | |
2058 | }; | |
2059 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
2060 | } | |
2061 | ||
2062 | return 0; | |
2063 | } | |
2064 | ||
2065 | static int | |
15e47304 | 2066 | ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, |
392025f8 PNA |
2067 | struct net *net) |
2068 | { | |
2069 | struct nlmsghdr *nlh; | |
2070 | struct nfgenmsg *nfmsg; | |
15e47304 | 2071 | unsigned int flags = portid ? NLM_F_MULTI : 0, event; |
392025f8 PNA |
2072 | unsigned int nr_conntracks = atomic_read(&net->ct.count); |
2073 | ||
2074 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS); | |
15e47304 | 2075 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
392025f8 PNA |
2076 | if (nlh == NULL) |
2077 | goto nlmsg_failure; | |
2078 | ||
2079 | nfmsg = nlmsg_data(nlh); | |
2080 | nfmsg->nfgen_family = AF_UNSPEC; | |
2081 | nfmsg->version = NFNETLINK_V0; | |
2082 | nfmsg->res_id = 0; | |
2083 | ||
2084 | if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks))) | |
2085 | goto nla_put_failure; | |
2086 | ||
2087 | nlmsg_end(skb, nlh); | |
2088 | return skb->len; | |
2089 | ||
2090 | nla_put_failure: | |
2091 | nlmsg_failure: | |
2092 | nlmsg_cancel(skb, nlh); | |
2093 | return -1; | |
2094 | } | |
2095 | ||
7b8002a1 PNA |
2096 | static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl, |
2097 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
2098 | const struct nlattr * const cda[]) | |
392025f8 PNA |
2099 | { |
2100 | struct sk_buff *skb2; | |
2101 | int err; | |
2102 | ||
2103 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
2104 | if (skb2 == NULL) | |
2105 | return -ENOMEM; | |
2106 | ||
15e47304 | 2107 | err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid, |
392025f8 PNA |
2108 | nlh->nlmsg_seq, |
2109 | NFNL_MSG_TYPE(nlh->nlmsg_type), | |
2110 | sock_net(skb->sk)); | |
2111 | if (err <= 0) | |
2112 | goto free; | |
2113 | ||
15e47304 | 2114 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT); |
392025f8 PNA |
2115 | if (err < 0) |
2116 | goto out; | |
2117 | ||
2118 | return 0; | |
2119 | ||
2120 | free: | |
2121 | kfree_skb(skb2); | |
2122 | out: | |
2123 | /* this avoids a loop in nfnetlink. */ | |
2124 | return err == -EAGAIN ? -ENOBUFS : err; | |
2125 | } | |
2126 | ||
bd077937 PNA |
2127 | static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = { |
2128 | [CTA_EXPECT_MASTER] = { .type = NLA_NESTED }, | |
2129 | [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED }, | |
2130 | [CTA_EXPECT_MASK] = { .type = NLA_NESTED }, | |
2131 | [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 }, | |
2132 | [CTA_EXPECT_ID] = { .type = NLA_U32 }, | |
2133 | [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING, | |
2134 | .len = NF_CT_HELPER_NAME_LEN - 1 }, | |
2135 | [CTA_EXPECT_ZONE] = { .type = NLA_U16 }, | |
2136 | [CTA_EXPECT_FLAGS] = { .type = NLA_U32 }, | |
2137 | [CTA_EXPECT_CLASS] = { .type = NLA_U32 }, | |
2138 | [CTA_EXPECT_NAT] = { .type = NLA_NESTED }, | |
2139 | [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING }, | |
2140 | }; | |
2141 | ||
2142 | static struct nf_conntrack_expect * | |
2143 | ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct, | |
2144 | struct nf_conntrack_helper *helper, | |
2145 | struct nf_conntrack_tuple *tuple, | |
2146 | struct nf_conntrack_tuple *mask); | |
2147 | ||
83f3e94d | 2148 | #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT |
9cb01766 | 2149 | static size_t |
a4b4766c | 2150 | ctnetlink_glue_build_size(const struct nf_conn *ct) |
9cb01766 PNA |
2151 | { |
2152 | return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ | |
2153 | + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ | |
2154 | + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ | |
2155 | + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ | |
2156 | + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ | |
2157 | + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ | |
2158 | + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ | |
2159 | + nla_total_size(0) /* CTA_PROTOINFO */ | |
2160 | + nla_total_size(0) /* CTA_HELP */ | |
2161 | + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ | |
2162 | + ctnetlink_secctx_size(ct) | |
2163 | #ifdef CONFIG_NF_NAT_NEEDED | |
2164 | + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ | |
2165 | + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ | |
2166 | #endif | |
2167 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
2168 | + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ | |
4a001068 KM |
2169 | #endif |
2170 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
deedb590 | 2171 | + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */ |
9cb01766 PNA |
2172 | #endif |
2173 | + ctnetlink_proto_size(ct) | |
2174 | ; | |
2175 | } | |
2176 | ||
224a0597 | 2177 | static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb, |
a4b4766c | 2178 | enum ip_conntrack_info *ctinfo) |
b7bd1809 PNA |
2179 | { |
2180 | struct nf_conn *ct; | |
2181 | ||
2182 | ct = nf_ct_get(skb, ctinfo); | |
2183 | if (ct && nf_ct_is_untracked(ct)) | |
2184 | ct = NULL; | |
2185 | ||
2186 | return ct; | |
2187 | } | |
2188 | ||
a4b4766c | 2189 | static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct) |
9cb01766 | 2190 | { |
308ac914 | 2191 | const struct nf_conntrack_zone *zone; |
9cb01766 PNA |
2192 | struct nlattr *nest_parms; |
2193 | ||
2194 | rcu_read_lock(); | |
deedb590 DB |
2195 | zone = nf_ct_zone(ct); |
2196 | ||
9cb01766 PNA |
2197 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
2198 | if (!nest_parms) | |
2199 | goto nla_put_failure; | |
2200 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) | |
2201 | goto nla_put_failure; | |
deedb590 DB |
2202 | if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone, |
2203 | NF_CT_ZONE_DIR_ORIG) < 0) | |
2204 | goto nla_put_failure; | |
9cb01766 PNA |
2205 | nla_nest_end(skb, nest_parms); |
2206 | ||
2207 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); | |
2208 | if (!nest_parms) | |
2209 | goto nla_put_failure; | |
2210 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) | |
2211 | goto nla_put_failure; | |
deedb590 DB |
2212 | if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone, |
2213 | NF_CT_ZONE_DIR_REPL) < 0) | |
2214 | goto nla_put_failure; | |
9cb01766 PNA |
2215 | nla_nest_end(skb, nest_parms); |
2216 | ||
deedb590 DB |
2217 | if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone, |
2218 | NF_CT_DEFAULT_ZONE_DIR) < 0) | |
308ac914 | 2219 | goto nla_put_failure; |
9cb01766 PNA |
2220 | |
2221 | if (ctnetlink_dump_id(skb, ct) < 0) | |
2222 | goto nla_put_failure; | |
2223 | ||
2224 | if (ctnetlink_dump_status(skb, ct) < 0) | |
2225 | goto nla_put_failure; | |
2226 | ||
2227 | if (ctnetlink_dump_timeout(skb, ct) < 0) | |
2228 | goto nla_put_failure; | |
2229 | ||
2230 | if (ctnetlink_dump_protoinfo(skb, ct) < 0) | |
2231 | goto nla_put_failure; | |
2232 | ||
2233 | if (ctnetlink_dump_helpinfo(skb, ct) < 0) | |
2234 | goto nla_put_failure; | |
2235 | ||
2236 | #ifdef CONFIG_NF_CONNTRACK_SECMARK | |
2237 | if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0) | |
2238 | goto nla_put_failure; | |
2239 | #endif | |
2240 | if (ct->master && ctnetlink_dump_master(skb, ct) < 0) | |
2241 | goto nla_put_failure; | |
2242 | ||
2243 | if ((ct->status & IPS_SEQ_ADJUST) && | |
41d73ec0 | 2244 | ctnetlink_dump_ct_seq_adj(skb, ct) < 0) |
9cb01766 PNA |
2245 | goto nla_put_failure; |
2246 | ||
2247 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
2248 | if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0) | |
2249 | goto nla_put_failure; | |
2250 | #endif | |
0ceabd83 FW |
2251 | if (ctnetlink_dump_labels(skb, ct) < 0) |
2252 | goto nla_put_failure; | |
9cb01766 PNA |
2253 | rcu_read_unlock(); |
2254 | return 0; | |
2255 | ||
2256 | nla_put_failure: | |
2257 | rcu_read_unlock(); | |
2258 | return -ENOSPC; | |
2259 | } | |
2260 | ||
b7bd1809 | 2261 | static int |
a4b4766c KM |
2262 | ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct, |
2263 | enum ip_conntrack_info ctinfo, | |
2264 | u_int16_t ct_attr, u_int16_t ct_info_attr) | |
b7bd1809 PNA |
2265 | { |
2266 | struct nlattr *nest_parms; | |
2267 | ||
2268 | nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED); | |
2269 | if (!nest_parms) | |
2270 | goto nla_put_failure; | |
2271 | ||
a4b4766c | 2272 | if (__ctnetlink_glue_build(skb, ct) < 0) |
b7bd1809 PNA |
2273 | goto nla_put_failure; |
2274 | ||
2275 | nla_nest_end(skb, nest_parms); | |
2276 | ||
2277 | if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo))) | |
2278 | goto nla_put_failure; | |
2279 | ||
2280 | return 0; | |
2281 | ||
2282 | nla_put_failure: | |
2283 | return -ENOSPC; | |
2284 | } | |
2285 | ||
a963d710 KC |
2286 | static int |
2287 | ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[]) | |
2288 | { | |
2289 | unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS])); | |
2290 | unsigned long d = ct->status ^ status; | |
2291 | ||
2292 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) | |
2293 | /* SEEN_REPLY bit can only be set */ | |
2294 | return -EBUSY; | |
2295 | ||
2296 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) | |
2297 | /* ASSURED bit can only be set */ | |
2298 | return -EBUSY; | |
2299 | ||
2300 | /* This check is less strict than ctnetlink_change_status() | |
2301 | * because callers often flip IPS_EXPECTED bits when sending | |
2302 | * an NFQA_CT attribute to the kernel. So ignore the | |
2303 | * unchangeable bits but do not error out. | |
2304 | */ | |
2305 | ct->status = (status & ~IPS_UNCHANGEABLE_MASK) | | |
2306 | (ct->status & IPS_UNCHANGEABLE_MASK); | |
2307 | return 0; | |
2308 | } | |
2309 | ||
9cb01766 | 2310 | static int |
a4b4766c | 2311 | ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct) |
9cb01766 PNA |
2312 | { |
2313 | int err; | |
2314 | ||
2315 | if (cda[CTA_TIMEOUT]) { | |
2316 | err = ctnetlink_change_timeout(ct, cda); | |
2317 | if (err < 0) | |
2318 | return err; | |
2319 | } | |
2320 | if (cda[CTA_STATUS]) { | |
a963d710 | 2321 | err = ctnetlink_update_status(ct, cda); |
9cb01766 PNA |
2322 | if (err < 0) |
2323 | return err; | |
2324 | } | |
2325 | if (cda[CTA_HELP]) { | |
2326 | err = ctnetlink_change_helper(ct, cda); | |
2327 | if (err < 0) | |
2328 | return err; | |
2329 | } | |
9b21f6a9 FW |
2330 | if (cda[CTA_LABELS]) { |
2331 | err = ctnetlink_attach_labels(ct, cda); | |
2332 | if (err < 0) | |
2333 | return err; | |
2334 | } | |
9cb01766 | 2335 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
534473c6 FW |
2336 | if (cda[CTA_MARK]) { |
2337 | u32 mask = 0, mark, newmark; | |
2338 | if (cda[CTA_MARK_MASK]) | |
2339 | mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK])); | |
2340 | ||
2341 | mark = ntohl(nla_get_be32(cda[CTA_MARK])); | |
2342 | newmark = (ct->mark & mask) ^ mark; | |
2343 | if (newmark != ct->mark) | |
2344 | ct->mark = newmark; | |
2345 | } | |
9cb01766 PNA |
2346 | #endif |
2347 | return 0; | |
2348 | } | |
2349 | ||
2350 | static int | |
a4b4766c | 2351 | ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct) |
9cb01766 PNA |
2352 | { |
2353 | struct nlattr *cda[CTA_MAX+1]; | |
68e035c9 | 2354 | int ret; |
9cb01766 | 2355 | |
130ffbc2 DB |
2356 | ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy); |
2357 | if (ret < 0) | |
2358 | return ret; | |
9cb01766 | 2359 | |
ca7433df | 2360 | spin_lock_bh(&nf_conntrack_expect_lock); |
a4b4766c | 2361 | ret = ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct); |
ca7433df | 2362 | spin_unlock_bh(&nf_conntrack_expect_lock); |
68e035c9 PNA |
2363 | |
2364 | return ret; | |
9cb01766 PNA |
2365 | } |
2366 | ||
a4b4766c KM |
2367 | static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda, |
2368 | const struct nf_conn *ct, | |
2369 | struct nf_conntrack_tuple *tuple, | |
2370 | struct nf_conntrack_tuple *mask) | |
bd077937 PNA |
2371 | { |
2372 | int err; | |
2373 | ||
2374 | err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE, | |
deedb590 | 2375 | nf_ct_l3num(ct), NULL); |
bd077937 PNA |
2376 | if (err < 0) |
2377 | return err; | |
2378 | ||
2379 | return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK, | |
deedb590 | 2380 | nf_ct_l3num(ct), NULL); |
bd077937 PNA |
2381 | } |
2382 | ||
2383 | static int | |
a4b4766c KM |
2384 | ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct, |
2385 | u32 portid, u32 report) | |
bd077937 PNA |
2386 | { |
2387 | struct nlattr *cda[CTA_EXPECT_MAX+1]; | |
2388 | struct nf_conntrack_tuple tuple, mask; | |
b7e092c0 | 2389 | struct nf_conntrack_helper *helper = NULL; |
bd077937 PNA |
2390 | struct nf_conntrack_expect *exp; |
2391 | int err; | |
2392 | ||
2393 | err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy); | |
2394 | if (err < 0) | |
2395 | return err; | |
2396 | ||
a4b4766c KM |
2397 | err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda, |
2398 | ct, &tuple, &mask); | |
bd077937 PNA |
2399 | if (err < 0) |
2400 | return err; | |
2401 | ||
2402 | if (cda[CTA_EXPECT_HELP_NAME]) { | |
2403 | const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
2404 | ||
2405 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), | |
2406 | nf_ct_protonum(ct)); | |
2407 | if (helper == NULL) | |
2408 | return -EOPNOTSUPP; | |
2409 | } | |
2410 | ||
2411 | exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct, | |
2412 | helper, &tuple, &mask); | |
2413 | if (IS_ERR(exp)) | |
2414 | return PTR_ERR(exp); | |
2415 | ||
2416 | err = nf_ct_expect_related_report(exp, portid, report); | |
b18bcb00 LZ |
2417 | nf_ct_expect_put(exp); |
2418 | return err; | |
bd077937 PNA |
2419 | } |
2420 | ||
a4b4766c KM |
2421 | static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct, |
2422 | enum ip_conntrack_info ctinfo, int diff) | |
b7bd1809 PNA |
2423 | { |
2424 | if (!(ct->status & IPS_NAT_MASK)) | |
2425 | return; | |
2426 | ||
2427 | nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff); | |
2428 | } | |
2429 | ||
a4b4766c KM |
2430 | static struct nfnl_ct_hook ctnetlink_glue_hook = { |
2431 | .get_ct = ctnetlink_glue_get_ct, | |
2432 | .build_size = ctnetlink_glue_build_size, | |
2433 | .build = ctnetlink_glue_build, | |
2434 | .parse = ctnetlink_glue_parse, | |
2435 | .attach_expect = ctnetlink_glue_attach_expect, | |
2436 | .seq_adjust = ctnetlink_glue_seqadj, | |
9cb01766 | 2437 | }; |
83f3e94d | 2438 | #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */ |
9cb01766 | 2439 | |
601e68e1 YH |
2440 | /*********************************************************************** |
2441 | * EXPECT | |
2442 | ***********************************************************************/ | |
c1d10adb | 2443 | |
4054ff45 PNA |
2444 | static int ctnetlink_exp_dump_tuple(struct sk_buff *skb, |
2445 | const struct nf_conntrack_tuple *tuple, | |
2446 | enum ctattr_expect type) | |
c1d10adb | 2447 | { |
df6fb868 | 2448 | struct nlattr *nest_parms; |
601e68e1 | 2449 | |
df6fb868 PM |
2450 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
2451 | if (!nest_parms) | |
2452 | goto nla_put_failure; | |
c1d10adb | 2453 | if (ctnetlink_dump_tuples(skb, tuple) < 0) |
df6fb868 PM |
2454 | goto nla_put_failure; |
2455 | nla_nest_end(skb, nest_parms); | |
c1d10adb PNA |
2456 | |
2457 | return 0; | |
2458 | ||
df6fb868 | 2459 | nla_put_failure: |
c1d10adb | 2460 | return -1; |
601e68e1 | 2461 | } |
c1d10adb | 2462 | |
4054ff45 PNA |
2463 | static int ctnetlink_exp_dump_mask(struct sk_buff *skb, |
2464 | const struct nf_conntrack_tuple *tuple, | |
2465 | const struct nf_conntrack_tuple_mask *mask) | |
1cde6436 PNA |
2466 | { |
2467 | int ret; | |
2468 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 2469 | struct nf_conntrack_l4proto *l4proto; |
d4156e8c | 2470 | struct nf_conntrack_tuple m; |
df6fb868 | 2471 | struct nlattr *nest_parms; |
d4156e8c PM |
2472 | |
2473 | memset(&m, 0xFF, sizeof(m)); | |
d4156e8c | 2474 | memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3)); |
e578756c PM |
2475 | m.src.u.all = mask->src.u.all; |
2476 | m.dst.protonum = tuple->dst.protonum; | |
d4156e8c | 2477 | |
df6fb868 PM |
2478 | nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED); |
2479 | if (!nest_parms) | |
2480 | goto nla_put_failure; | |
1cde6436 | 2481 | |
3b988ece | 2482 | rcu_read_lock(); |
528a3a6f | 2483 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
d4156e8c | 2484 | ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto); |
3b988ece HS |
2485 | if (ret >= 0) { |
2486 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, | |
2487 | tuple->dst.protonum); | |
d4156e8c | 2488 | ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto); |
3b988ece HS |
2489 | } |
2490 | rcu_read_unlock(); | |
2491 | ||
1cde6436 | 2492 | if (unlikely(ret < 0)) |
df6fb868 | 2493 | goto nla_put_failure; |
1cde6436 | 2494 | |
df6fb868 | 2495 | nla_nest_end(skb, nest_parms); |
1cde6436 PNA |
2496 | |
2497 | return 0; | |
2498 | ||
df6fb868 | 2499 | nla_put_failure: |
1cde6436 PNA |
2500 | return -1; |
2501 | } | |
2502 | ||
c7232c99 PM |
2503 | static const union nf_inet_addr any_addr; |
2504 | ||
bb5cf80e | 2505 | static int |
c1d10adb | 2506 | ctnetlink_exp_dump_expect(struct sk_buff *skb, |
601e68e1 | 2507 | const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
2508 | { |
2509 | struct nf_conn *master = exp->master; | |
c1216382 | 2510 | long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ; |
bc01befd | 2511 | struct nf_conn_help *help; |
076a0ca0 PNA |
2512 | #ifdef CONFIG_NF_NAT_NEEDED |
2513 | struct nlattr *nest_parms; | |
2514 | struct nf_conntrack_tuple nat_tuple = {}; | |
2515 | #endif | |
544d5c7d PNA |
2516 | struct nf_ct_helper_expectfn *expfn; |
2517 | ||
d978e5da PM |
2518 | if (timeout < 0) |
2519 | timeout = 0; | |
c1d10adb PNA |
2520 | |
2521 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) | |
df6fb868 | 2522 | goto nla_put_failure; |
1cde6436 | 2523 | if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) |
df6fb868 | 2524 | goto nla_put_failure; |
c1d10adb PNA |
2525 | if (ctnetlink_exp_dump_tuple(skb, |
2526 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
2527 | CTA_EXPECT_MASTER) < 0) | |
df6fb868 | 2528 | goto nla_put_failure; |
601e68e1 | 2529 | |
076a0ca0 | 2530 | #ifdef CONFIG_NF_NAT_NEEDED |
c7232c99 PM |
2531 | if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) || |
2532 | exp->saved_proto.all) { | |
076a0ca0 PNA |
2533 | nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED); |
2534 | if (!nest_parms) | |
2535 | goto nla_put_failure; | |
2536 | ||
cc1eb431 DM |
2537 | if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir))) |
2538 | goto nla_put_failure; | |
076a0ca0 PNA |
2539 | |
2540 | nat_tuple.src.l3num = nf_ct_l3num(master); | |
c7232c99 | 2541 | nat_tuple.src.u3 = exp->saved_addr; |
076a0ca0 PNA |
2542 | nat_tuple.dst.protonum = nf_ct_protonum(master); |
2543 | nat_tuple.src.u = exp->saved_proto; | |
2544 | ||
2545 | if (ctnetlink_exp_dump_tuple(skb, &nat_tuple, | |
2546 | CTA_EXPECT_NAT_TUPLE) < 0) | |
2547 | goto nla_put_failure; | |
2548 | nla_nest_end(skb, nest_parms); | |
2549 | } | |
2550 | #endif | |
cc1eb431 DM |
2551 | if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) || |
2552 | nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) || | |
2553 | nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) || | |
2554 | nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class))) | |
2555 | goto nla_put_failure; | |
bc01befd PNA |
2556 | help = nfct_help(master); |
2557 | if (help) { | |
2558 | struct nf_conntrack_helper *helper; | |
2559 | ||
2560 | helper = rcu_dereference(help->helper); | |
cc1eb431 DM |
2561 | if (helper && |
2562 | nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name)) | |
2563 | goto nla_put_failure; | |
bc01befd | 2564 | } |
544d5c7d | 2565 | expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn); |
cc1eb431 DM |
2566 | if (expfn != NULL && |
2567 | nla_put_string(skb, CTA_EXPECT_FN, expfn->name)) | |
2568 | goto nla_put_failure; | |
c1d10adb PNA |
2569 | |
2570 | return 0; | |
601e68e1 | 2571 | |
df6fb868 | 2572 | nla_put_failure: |
c1d10adb PNA |
2573 | return -1; |
2574 | } | |
2575 | ||
2576 | static int | |
15e47304 | 2577 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq, |
8b0a231d | 2578 | int event, const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
2579 | { |
2580 | struct nlmsghdr *nlh; | |
2581 | struct nfgenmsg *nfmsg; | |
15e47304 | 2582 | unsigned int flags = portid ? NLM_F_MULTI : 0; |
c1d10adb PNA |
2583 | |
2584 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; | |
15e47304 | 2585 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
96bcf938 PNA |
2586 | if (nlh == NULL) |
2587 | goto nlmsg_failure; | |
c1d10adb | 2588 | |
96bcf938 | 2589 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
2590 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
2591 | nfmsg->version = NFNETLINK_V0; | |
2592 | nfmsg->res_id = 0; | |
2593 | ||
2594 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) | |
df6fb868 | 2595 | goto nla_put_failure; |
c1d10adb | 2596 | |
96bcf938 | 2597 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
2598 | return skb->len; |
2599 | ||
2600 | nlmsg_failure: | |
df6fb868 | 2601 | nla_put_failure: |
96bcf938 | 2602 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
2603 | return -1; |
2604 | } | |
2605 | ||
2606 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
e34d5c1a PNA |
2607 | static int |
2608 | ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item) | |
c1d10adb | 2609 | { |
9592a5c0 AD |
2610 | struct nf_conntrack_expect *exp = item->exp; |
2611 | struct net *net = nf_ct_exp_net(exp); | |
c1d10adb PNA |
2612 | struct nlmsghdr *nlh; |
2613 | struct nfgenmsg *nfmsg; | |
c1d10adb | 2614 | struct sk_buff *skb; |
ebbf41df | 2615 | unsigned int type, group; |
c1d10adb PNA |
2616 | int flags = 0; |
2617 | ||
ebbf41df PNA |
2618 | if (events & (1 << IPEXP_DESTROY)) { |
2619 | type = IPCTNL_MSG_EXP_DELETE; | |
2620 | group = NFNLGRP_CONNTRACK_EXP_DESTROY; | |
2621 | } else if (events & (1 << IPEXP_NEW)) { | |
c1d10adb PNA |
2622 | type = IPCTNL_MSG_EXP_NEW; |
2623 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
ebbf41df | 2624 | group = NFNLGRP_CONNTRACK_EXP_NEW; |
c1d10adb | 2625 | } else |
e34d5c1a | 2626 | return 0; |
c1d10adb | 2627 | |
ebbf41df | 2628 | if (!item->report && !nfnetlink_has_listeners(net, group)) |
e34d5c1a | 2629 | return 0; |
b3a27bfb | 2630 | |
96bcf938 PNA |
2631 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
2632 | if (skb == NULL) | |
150ace0d | 2633 | goto errout; |
c1d10adb | 2634 | |
b633ad5f | 2635 | type |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
15e47304 | 2636 | nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags); |
96bcf938 PNA |
2637 | if (nlh == NULL) |
2638 | goto nlmsg_failure; | |
c1d10adb | 2639 | |
96bcf938 | 2640 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
2641 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
2642 | nfmsg->version = NFNETLINK_V0; | |
2643 | nfmsg->res_id = 0; | |
2644 | ||
528a3a6f | 2645 | rcu_read_lock(); |
c1d10adb | 2646 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
df6fb868 | 2647 | goto nla_put_failure; |
528a3a6f | 2648 | rcu_read_unlock(); |
c1d10adb | 2649 | |
96bcf938 | 2650 | nlmsg_end(skb, nlh); |
15e47304 | 2651 | nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC); |
e34d5c1a | 2652 | return 0; |
c1d10adb | 2653 | |
df6fb868 | 2654 | nla_put_failure: |
528a3a6f | 2655 | rcu_read_unlock(); |
96bcf938 | 2656 | nlmsg_cancel(skb, nlh); |
528a3a6f | 2657 | nlmsg_failure: |
c1d10adb | 2658 | kfree_skb(skb); |
150ace0d | 2659 | errout: |
9592a5c0 | 2660 | nfnetlink_set_err(net, 0, 0, -ENOBUFS); |
e34d5c1a | 2661 | return 0; |
c1d10adb PNA |
2662 | } |
2663 | #endif | |
cf6994c2 PM |
2664 | static int ctnetlink_exp_done(struct netlink_callback *cb) |
2665 | { | |
31f15875 PM |
2666 | if (cb->args[1]) |
2667 | nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]); | |
cf6994c2 PM |
2668 | return 0; |
2669 | } | |
c1d10adb PNA |
2670 | |
2671 | static int | |
2672 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
2673 | { | |
9592a5c0 | 2674 | struct net *net = sock_net(skb->sk); |
cf6994c2 | 2675 | struct nf_conntrack_expect *exp, *last; |
96bcf938 | 2676 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
87711cb8 | 2677 | u_int8_t l3proto = nfmsg->nfgen_family; |
c1d10adb | 2678 | |
7d0742da | 2679 | rcu_read_lock(); |
31f15875 PM |
2680 | last = (struct nf_conntrack_expect *)cb->args[1]; |
2681 | for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) { | |
cf6994c2 | 2682 | restart: |
0a93aaed | 2683 | hlist_for_each_entry(exp, &nf_ct_expect_hash[cb->args[0]], |
31f15875 PM |
2684 | hnode) { |
2685 | if (l3proto && exp->tuple.src.l3num != l3proto) | |
cf6994c2 | 2686 | continue; |
03d7dc5c FW |
2687 | |
2688 | if (!net_eq(nf_ct_net(exp->master), net)) | |
2689 | continue; | |
2690 | ||
31f15875 PM |
2691 | if (cb->args[1]) { |
2692 | if (exp != last) | |
2693 | continue; | |
2694 | cb->args[1] = 0; | |
2695 | } | |
8b0a231d | 2696 | if (ctnetlink_exp_fill_info(skb, |
15e47304 | 2697 | NETLINK_CB(cb->skb).portid, |
31f15875 PM |
2698 | cb->nlh->nlmsg_seq, |
2699 | IPCTNL_MSG_EXP_NEW, | |
8b0a231d | 2700 | exp) < 0) { |
7d0742da PM |
2701 | if (!atomic_inc_not_zero(&exp->use)) |
2702 | continue; | |
31f15875 PM |
2703 | cb->args[1] = (unsigned long)exp; |
2704 | goto out; | |
2705 | } | |
cf6994c2 | 2706 | } |
31f15875 PM |
2707 | if (cb->args[1]) { |
2708 | cb->args[1] = 0; | |
2709 | goto restart; | |
cf6994c2 PM |
2710 | } |
2711 | } | |
601e68e1 | 2712 | out: |
7d0742da | 2713 | rcu_read_unlock(); |
cf6994c2 PM |
2714 | if (last) |
2715 | nf_ct_expect_put(last); | |
c1d10adb | 2716 | |
c1d10adb PNA |
2717 | return skb->len; |
2718 | } | |
2719 | ||
e844a928 PNA |
2720 | static int |
2721 | ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
2722 | { | |
2723 | struct nf_conntrack_expect *exp, *last; | |
2724 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
2725 | struct nf_conn *ct = cb->data; | |
2726 | struct nf_conn_help *help = nfct_help(ct); | |
2727 | u_int8_t l3proto = nfmsg->nfgen_family; | |
2728 | ||
2729 | if (cb->args[0]) | |
2730 | return 0; | |
2731 | ||
2732 | rcu_read_lock(); | |
2733 | last = (struct nf_conntrack_expect *)cb->args[1]; | |
2734 | restart: | |
2735 | hlist_for_each_entry(exp, &help->expectations, lnode) { | |
2736 | if (l3proto && exp->tuple.src.l3num != l3proto) | |
2737 | continue; | |
2738 | if (cb->args[1]) { | |
2739 | if (exp != last) | |
2740 | continue; | |
2741 | cb->args[1] = 0; | |
2742 | } | |
2743 | if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid, | |
2744 | cb->nlh->nlmsg_seq, | |
2745 | IPCTNL_MSG_EXP_NEW, | |
2746 | exp) < 0) { | |
2747 | if (!atomic_inc_not_zero(&exp->use)) | |
2748 | continue; | |
2749 | cb->args[1] = (unsigned long)exp; | |
2750 | goto out; | |
2751 | } | |
2752 | } | |
2753 | if (cb->args[1]) { | |
2754 | cb->args[1] = 0; | |
2755 | goto restart; | |
2756 | } | |
2757 | cb->args[0] = 1; | |
2758 | out: | |
2759 | rcu_read_unlock(); | |
2760 | if (last) | |
2761 | nf_ct_expect_put(last); | |
2762 | ||
2763 | return skb->len; | |
2764 | } | |
2765 | ||
7b8002a1 PNA |
2766 | static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl, |
2767 | struct sk_buff *skb, | |
e844a928 PNA |
2768 | const struct nlmsghdr *nlh, |
2769 | const struct nlattr * const cda[]) | |
2770 | { | |
2771 | int err; | |
e844a928 PNA |
2772 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
2773 | u_int8_t u3 = nfmsg->nfgen_family; | |
2774 | struct nf_conntrack_tuple tuple; | |
2775 | struct nf_conntrack_tuple_hash *h; | |
2776 | struct nf_conn *ct; | |
308ac914 | 2777 | struct nf_conntrack_zone zone; |
e844a928 PNA |
2778 | struct netlink_dump_control c = { |
2779 | .dump = ctnetlink_exp_ct_dump_table, | |
2780 | .done = ctnetlink_exp_done, | |
2781 | }; | |
2782 | ||
deedb590 DB |
2783 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, |
2784 | u3, NULL); | |
e844a928 PNA |
2785 | if (err < 0) |
2786 | return err; | |
2787 | ||
308ac914 DB |
2788 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2789 | if (err < 0) | |
2790 | return err; | |
e844a928 | 2791 | |
308ac914 | 2792 | h = nf_conntrack_find_get(net, &zone, &tuple); |
e844a928 PNA |
2793 | if (!h) |
2794 | return -ENOENT; | |
2795 | ||
2796 | ct = nf_ct_tuplehash_to_ctrack(h); | |
2797 | c.data = ct; | |
2798 | ||
2799 | err = netlink_dump_start(ctnl, skb, nlh, &c); | |
2800 | nf_ct_put(ct); | |
2801 | ||
2802 | return err; | |
2803 | } | |
2804 | ||
7b8002a1 PNA |
2805 | static int ctnetlink_get_expect(struct net *net, struct sock *ctnl, |
2806 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
2807 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
2808 | { |
2809 | struct nf_conntrack_tuple tuple; | |
2810 | struct nf_conntrack_expect *exp; | |
2811 | struct sk_buff *skb2; | |
96bcf938 | 2812 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 2813 | u_int8_t u3 = nfmsg->nfgen_family; |
308ac914 | 2814 | struct nf_conntrack_zone zone; |
ef00f89f | 2815 | int err; |
c1d10adb | 2816 | |
b8f3ab42 | 2817 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
e844a928 | 2818 | if (cda[CTA_EXPECT_MASTER]) |
7b8002a1 | 2819 | return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda); |
e844a928 PNA |
2820 | else { |
2821 | struct netlink_dump_control c = { | |
2822 | .dump = ctnetlink_exp_dump_table, | |
2823 | .done = ctnetlink_exp_done, | |
2824 | }; | |
2825 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
2826 | } | |
c1d10adb PNA |
2827 | } |
2828 | ||
ef00f89f PM |
2829 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2830 | if (err < 0) | |
2831 | return err; | |
2832 | ||
35dba1d7 | 2833 | if (cda[CTA_EXPECT_TUPLE]) |
deedb590 DB |
2834 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, |
2835 | u3, NULL); | |
35dba1d7 | 2836 | else if (cda[CTA_EXPECT_MASTER]) |
deedb590 DB |
2837 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, |
2838 | u3, NULL); | |
c1d10adb PNA |
2839 | else |
2840 | return -EINVAL; | |
2841 | ||
2842 | if (err < 0) | |
2843 | return err; | |
2844 | ||
308ac914 | 2845 | exp = nf_ct_expect_find_get(net, &zone, &tuple); |
c1d10adb PNA |
2846 | if (!exp) |
2847 | return -ENOENT; | |
2848 | ||
df6fb868 | 2849 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 2850 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 2851 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 2852 | nf_ct_expect_put(exp); |
c1d10adb PNA |
2853 | return -ENOENT; |
2854 | } | |
601e68e1 | 2855 | } |
c1d10adb PNA |
2856 | |
2857 | err = -ENOMEM; | |
96bcf938 | 2858 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
81378f72 PNA |
2859 | if (skb2 == NULL) { |
2860 | nf_ct_expect_put(exp); | |
c1d10adb | 2861 | goto out; |
81378f72 | 2862 | } |
4e9b8269 | 2863 | |
528a3a6f | 2864 | rcu_read_lock(); |
15e47304 | 2865 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid, |
8b0a231d | 2866 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp); |
528a3a6f | 2867 | rcu_read_unlock(); |
81378f72 | 2868 | nf_ct_expect_put(exp); |
c1d10adb PNA |
2869 | if (err <= 0) |
2870 | goto free; | |
2871 | ||
15e47304 | 2872 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT); |
81378f72 PNA |
2873 | if (err < 0) |
2874 | goto out; | |
c1d10adb | 2875 | |
81378f72 | 2876 | return 0; |
c1d10adb PNA |
2877 | |
2878 | free: | |
2879 | kfree_skb(skb2); | |
2880 | out: | |
81378f72 PNA |
2881 | /* this avoids a loop in nfnetlink. */ |
2882 | return err == -EAGAIN ? -ENOBUFS : err; | |
c1d10adb PNA |
2883 | } |
2884 | ||
7b8002a1 PNA |
2885 | static int ctnetlink_del_expect(struct net *net, struct sock *ctnl, |
2886 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
2887 | const struct nlattr * const cda[]) | |
c1d10adb | 2888 | { |
31f15875 | 2889 | struct nf_conntrack_expect *exp; |
c1d10adb | 2890 | struct nf_conntrack_tuple tuple; |
96bcf938 | 2891 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
b67bfe0d | 2892 | struct hlist_node *next; |
c1d10adb | 2893 | u_int8_t u3 = nfmsg->nfgen_family; |
308ac914 | 2894 | struct nf_conntrack_zone zone; |
31f15875 | 2895 | unsigned int i; |
c1d10adb PNA |
2896 | int err; |
2897 | ||
df6fb868 | 2898 | if (cda[CTA_EXPECT_TUPLE]) { |
c1d10adb | 2899 | /* delete a single expect by tuple */ |
ef00f89f PM |
2900 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2901 | if (err < 0) | |
2902 | return err; | |
2903 | ||
deedb590 DB |
2904 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, |
2905 | u3, NULL); | |
c1d10adb PNA |
2906 | if (err < 0) |
2907 | return err; | |
2908 | ||
2909 | /* bump usage count to 2 */ | |
308ac914 | 2910 | exp = nf_ct_expect_find_get(net, &zone, &tuple); |
c1d10adb PNA |
2911 | if (!exp) |
2912 | return -ENOENT; | |
2913 | ||
df6fb868 | 2914 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 2915 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 2916 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 2917 | nf_ct_expect_put(exp); |
c1d10adb PNA |
2918 | return -ENOENT; |
2919 | } | |
2920 | } | |
2921 | ||
2922 | /* after list removal, usage count == 1 */ | |
ca7433df | 2923 | spin_lock_bh(&nf_conntrack_expect_lock); |
ebbf41df | 2924 | if (del_timer(&exp->timeout)) { |
15e47304 | 2925 | nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid, |
ebbf41df PNA |
2926 | nlmsg_report(nlh)); |
2927 | nf_ct_expect_put(exp); | |
2928 | } | |
ca7433df | 2929 | spin_unlock_bh(&nf_conntrack_expect_lock); |
601e68e1 | 2930 | /* have to put what we 'get' above. |
c1d10adb | 2931 | * after this line usage count == 0 */ |
6823645d | 2932 | nf_ct_expect_put(exp); |
df6fb868 PM |
2933 | } else if (cda[CTA_EXPECT_HELP_NAME]) { |
2934 | char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
31f15875 | 2935 | struct nf_conn_help *m_help; |
c1d10adb PNA |
2936 | |
2937 | /* delete all expectations for this helper */ | |
ca7433df | 2938 | spin_lock_bh(&nf_conntrack_expect_lock); |
31f15875 | 2939 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
b67bfe0d | 2940 | hlist_for_each_entry_safe(exp, next, |
0a93aaed | 2941 | &nf_ct_expect_hash[i], |
31f15875 | 2942 | hnode) { |
03d7dc5c FW |
2943 | |
2944 | if (!net_eq(nf_ct_exp_net(exp), net)) | |
2945 | continue; | |
2946 | ||
31f15875 | 2947 | m_help = nfct_help(exp->master); |
794e6871 PM |
2948 | if (!strcmp(m_help->helper->name, name) && |
2949 | del_timer(&exp->timeout)) { | |
ebbf41df | 2950 | nf_ct_unlink_expect_report(exp, |
15e47304 | 2951 | NETLINK_CB(skb).portid, |
ebbf41df | 2952 | nlmsg_report(nlh)); |
31f15875 PM |
2953 | nf_ct_expect_put(exp); |
2954 | } | |
c1d10adb PNA |
2955 | } |
2956 | } | |
ca7433df | 2957 | spin_unlock_bh(&nf_conntrack_expect_lock); |
c1d10adb PNA |
2958 | } else { |
2959 | /* This basically means we have to flush everything*/ | |
ca7433df | 2960 | spin_lock_bh(&nf_conntrack_expect_lock); |
31f15875 | 2961 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
b67bfe0d | 2962 | hlist_for_each_entry_safe(exp, next, |
0a93aaed | 2963 | &nf_ct_expect_hash[i], |
31f15875 | 2964 | hnode) { |
03d7dc5c FW |
2965 | |
2966 | if (!net_eq(nf_ct_exp_net(exp), net)) | |
2967 | continue; | |
2968 | ||
31f15875 | 2969 | if (del_timer(&exp->timeout)) { |
ebbf41df | 2970 | nf_ct_unlink_expect_report(exp, |
15e47304 | 2971 | NETLINK_CB(skb).portid, |
ebbf41df | 2972 | nlmsg_report(nlh)); |
31f15875 PM |
2973 | nf_ct_expect_put(exp); |
2974 | } | |
c1d10adb PNA |
2975 | } |
2976 | } | |
ca7433df | 2977 | spin_unlock_bh(&nf_conntrack_expect_lock); |
c1d10adb PNA |
2978 | } |
2979 | ||
2980 | return 0; | |
2981 | } | |
2982 | static int | |
39938324 PM |
2983 | ctnetlink_change_expect(struct nf_conntrack_expect *x, |
2984 | const struct nlattr * const cda[]) | |
c1d10adb | 2985 | { |
9768e1ac KW |
2986 | if (cda[CTA_EXPECT_TIMEOUT]) { |
2987 | if (!del_timer(&x->timeout)) | |
2988 | return -ETIME; | |
2989 | ||
2990 | x->timeout.expires = jiffies + | |
2991 | ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ; | |
2992 | add_timer(&x->timeout); | |
2993 | } | |
2994 | return 0; | |
c1d10adb PNA |
2995 | } |
2996 | ||
076a0ca0 PNA |
2997 | static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = { |
2998 | [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 }, | |
2999 | [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED }, | |
3000 | }; | |
3001 | ||
3002 | static int | |
3003 | ctnetlink_parse_expect_nat(const struct nlattr *attr, | |
3004 | struct nf_conntrack_expect *exp, | |
3005 | u_int8_t u3) | |
3006 | { | |
3007 | #ifdef CONFIG_NF_NAT_NEEDED | |
3008 | struct nlattr *tb[CTA_EXPECT_NAT_MAX+1]; | |
3009 | struct nf_conntrack_tuple nat_tuple = {}; | |
3010 | int err; | |
3011 | ||
130ffbc2 DB |
3012 | err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy); |
3013 | if (err < 0) | |
3014 | return err; | |
076a0ca0 PNA |
3015 | |
3016 | if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE]) | |
3017 | return -EINVAL; | |
3018 | ||
3019 | err = ctnetlink_parse_tuple((const struct nlattr * const *)tb, | |
deedb590 DB |
3020 | &nat_tuple, CTA_EXPECT_NAT_TUPLE, |
3021 | u3, NULL); | |
076a0ca0 PNA |
3022 | if (err < 0) |
3023 | return err; | |
3024 | ||
c7232c99 | 3025 | exp->saved_addr = nat_tuple.src.u3; |
076a0ca0 PNA |
3026 | exp->saved_proto = nat_tuple.src.u; |
3027 | exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR])); | |
3028 | ||
3029 | return 0; | |
3030 | #else | |
3031 | return -EOPNOTSUPP; | |
3032 | #endif | |
3033 | } | |
3034 | ||
0ef71ee1 PNA |
3035 | static struct nf_conntrack_expect * |
3036 | ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct, | |
3037 | struct nf_conntrack_helper *helper, | |
3038 | struct nf_conntrack_tuple *tuple, | |
3039 | struct nf_conntrack_tuple *mask) | |
c1d10adb | 3040 | { |
0ef71ee1 | 3041 | u_int32_t class = 0; |
c1d10adb | 3042 | struct nf_conntrack_expect *exp; |
dc808fe2 | 3043 | struct nf_conn_help *help; |
0ef71ee1 | 3044 | int err; |
660fdb2a | 3045 | |
b8c5e52c PNA |
3046 | if (cda[CTA_EXPECT_CLASS] && helper) { |
3047 | class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS])); | |
0ef71ee1 PNA |
3048 | if (class > helper->expect_class_max) |
3049 | return ERR_PTR(-EINVAL); | |
b8c5e52c | 3050 | } |
6823645d | 3051 | exp = nf_ct_expect_alloc(ct); |
0ef71ee1 PNA |
3052 | if (!exp) |
3053 | return ERR_PTR(-ENOMEM); | |
3054 | ||
bc01befd PNA |
3055 | help = nfct_help(ct); |
3056 | if (!help) { | |
3057 | if (!cda[CTA_EXPECT_TIMEOUT]) { | |
3058 | err = -EINVAL; | |
1310b955 | 3059 | goto err_out; |
bc01befd PNA |
3060 | } |
3061 | exp->timeout.expires = | |
3062 | jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ; | |
601e68e1 | 3063 | |
bc01befd PNA |
3064 | exp->flags = NF_CT_EXPECT_USERSPACE; |
3065 | if (cda[CTA_EXPECT_FLAGS]) { | |
3066 | exp->flags |= | |
3067 | ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); | |
3068 | } | |
3069 | } else { | |
3070 | if (cda[CTA_EXPECT_FLAGS]) { | |
3071 | exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); | |
3072 | exp->flags &= ~NF_CT_EXPECT_USERSPACE; | |
3073 | } else | |
3074 | exp->flags = 0; | |
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 PNA |
3236 | |
3237 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU); | |
15e47304 | 3238 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
392025f8 PNA |
3239 | if (nlh == NULL) |
3240 | goto nlmsg_failure; | |
3241 | ||
3242 | nfmsg = nlmsg_data(nlh); | |
3243 | nfmsg->nfgen_family = AF_UNSPEC; | |
3244 | nfmsg->version = NFNETLINK_V0; | |
3245 | nfmsg->res_id = htons(cpu); | |
3246 | ||
3247 | if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) || | |
3248 | nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) || | |
3249 | nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete))) | |
3250 | goto nla_put_failure; | |
3251 | ||
3252 | nlmsg_end(skb, nlh); | |
3253 | return skb->len; | |
3254 | ||
3255 | nla_put_failure: | |
3256 | nlmsg_failure: | |
3257 | nlmsg_cancel(skb, nlh); | |
3258 | return -1; | |
3259 | } | |
3260 | ||
3261 | static int | |
3262 | ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
3263 | { | |
3264 | int cpu; | |
3265 | struct net *net = sock_net(skb->sk); | |
3266 | ||
3267 | if (cb->args[0] == nr_cpu_ids) | |
3268 | return 0; | |
3269 | ||
3270 | for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) { | |
3271 | const struct ip_conntrack_stat *st; | |
3272 | ||
3273 | if (!cpu_possible(cpu)) | |
3274 | continue; | |
3275 | ||
3276 | st = per_cpu_ptr(net->ct.stat, cpu); | |
15e47304 | 3277 | if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid, |
392025f8 PNA |
3278 | cb->nlh->nlmsg_seq, |
3279 | cpu, st) < 0) | |
3280 | break; | |
3281 | } | |
3282 | cb->args[0] = cpu; | |
3283 | ||
3284 | return skb->len; | |
3285 | } | |
3286 | ||
7b8002a1 PNA |
3287 | static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl, |
3288 | struct sk_buff *skb, | |
3289 | const struct nlmsghdr *nlh, | |
3290 | const struct nlattr * const cda[]) | |
392025f8 PNA |
3291 | { |
3292 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
3293 | struct netlink_dump_control c = { | |
3294 | .dump = ctnetlink_exp_stat_cpu_dump, | |
3295 | }; | |
3296 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
3297 | } | |
3298 | ||
3299 | return 0; | |
3300 | } | |
3301 | ||
c1d10adb | 3302 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
e34d5c1a PNA |
3303 | static struct nf_ct_event_notifier ctnl_notifier = { |
3304 | .fcn = ctnetlink_conntrack_event, | |
c1d10adb PNA |
3305 | }; |
3306 | ||
e34d5c1a PNA |
3307 | static struct nf_exp_event_notifier ctnl_notifier_exp = { |
3308 | .fcn = ctnetlink_expect_event, | |
c1d10adb PNA |
3309 | }; |
3310 | #endif | |
3311 | ||
7c8d4cb4 | 3312 | static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { |
c1d10adb | 3313 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, |
f73e924c PM |
3314 | .attr_count = CTA_MAX, |
3315 | .policy = ct_nla_policy }, | |
c1d10adb | 3316 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
3317 | .attr_count = CTA_MAX, |
3318 | .policy = ct_nla_policy }, | |
c1d10adb | 3319 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, |
f73e924c PM |
3320 | .attr_count = CTA_MAX, |
3321 | .policy = ct_nla_policy }, | |
c1d10adb | 3322 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
3323 | .attr_count = CTA_MAX, |
3324 | .policy = ct_nla_policy }, | |
392025f8 PNA |
3325 | [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu }, |
3326 | [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct }, | |
d871befe PNA |
3327 | [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying }, |
3328 | [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed }, | |
c1d10adb PNA |
3329 | }; |
3330 | ||
7c8d4cb4 | 3331 | static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { |
c1d10adb | 3332 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
f73e924c PM |
3333 | .attr_count = CTA_EXPECT_MAX, |
3334 | .policy = exp_nla_policy }, | |
c1d10adb | 3335 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, |
f73e924c PM |
3336 | .attr_count = CTA_EXPECT_MAX, |
3337 | .policy = exp_nla_policy }, | |
c1d10adb | 3338 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, |
f73e924c PM |
3339 | .attr_count = CTA_EXPECT_MAX, |
3340 | .policy = exp_nla_policy }, | |
392025f8 | 3341 | [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu }, |
c1d10adb PNA |
3342 | }; |
3343 | ||
7c8d4cb4 | 3344 | static const struct nfnetlink_subsystem ctnl_subsys = { |
c1d10adb PNA |
3345 | .name = "conntrack", |
3346 | .subsys_id = NFNL_SUBSYS_CTNETLINK, | |
3347 | .cb_count = IPCTNL_MSG_MAX, | |
3348 | .cb = ctnl_cb, | |
3349 | }; | |
3350 | ||
7c8d4cb4 | 3351 | static const struct nfnetlink_subsystem ctnl_exp_subsys = { |
c1d10adb PNA |
3352 | .name = "conntrack_expect", |
3353 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, | |
3354 | .cb_count = IPCTNL_MSG_EXP_MAX, | |
3355 | .cb = ctnl_exp_cb, | |
3356 | }; | |
3357 | ||
d2483dde | 3358 | MODULE_ALIAS("ip_conntrack_netlink"); |
c1d10adb | 3359 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); |
34f9a2e4 | 3360 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); |
c1d10adb | 3361 | |
70e9942f PNA |
3362 | static int __net_init ctnetlink_net_init(struct net *net) |
3363 | { | |
3364 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
3365 | int ret; | |
3366 | ||
3367 | ret = nf_conntrack_register_notifier(net, &ctnl_notifier); | |
3368 | if (ret < 0) { | |
3369 | pr_err("ctnetlink_init: cannot register notifier.\n"); | |
3370 | goto err_out; | |
3371 | } | |
3372 | ||
3373 | ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp); | |
3374 | if (ret < 0) { | |
3375 | pr_err("ctnetlink_init: cannot expect register notifier.\n"); | |
3376 | goto err_unreg_notifier; | |
3377 | } | |
3378 | #endif | |
3379 | return 0; | |
3380 | ||
3381 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
3382 | err_unreg_notifier: | |
3383 | nf_conntrack_unregister_notifier(net, &ctnl_notifier); | |
3384 | err_out: | |
3385 | return ret; | |
3386 | #endif | |
3387 | } | |
3388 | ||
3389 | static void ctnetlink_net_exit(struct net *net) | |
3390 | { | |
3391 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
3392 | nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp); | |
3393 | nf_conntrack_unregister_notifier(net, &ctnl_notifier); | |
3394 | #endif | |
3395 | } | |
3396 | ||
3397 | static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list) | |
3398 | { | |
3399 | struct net *net; | |
3400 | ||
3401 | list_for_each_entry(net, net_exit_list, exit_list) | |
3402 | ctnetlink_net_exit(net); | |
3403 | } | |
3404 | ||
3405 | static struct pernet_operations ctnetlink_net_ops = { | |
3406 | .init = ctnetlink_net_init, | |
3407 | .exit_batch = ctnetlink_net_exit_batch, | |
3408 | }; | |
3409 | ||
c1d10adb PNA |
3410 | static int __init ctnetlink_init(void) |
3411 | { | |
3412 | int ret; | |
3413 | ||
654d0fbd | 3414 | pr_info("ctnetlink v%s: registering with nfnetlink.\n", version); |
c1d10adb PNA |
3415 | ret = nfnetlink_subsys_register(&ctnl_subsys); |
3416 | if (ret < 0) { | |
654d0fbd | 3417 | pr_err("ctnetlink_init: cannot register with nfnetlink.\n"); |
c1d10adb PNA |
3418 | goto err_out; |
3419 | } | |
3420 | ||
3421 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); | |
3422 | if (ret < 0) { | |
654d0fbd | 3423 | pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n"); |
c1d10adb PNA |
3424 | goto err_unreg_subsys; |
3425 | } | |
3426 | ||
ef6acf68 JL |
3427 | ret = register_pernet_subsys(&ctnetlink_net_ops); |
3428 | if (ret < 0) { | |
70e9942f | 3429 | pr_err("ctnetlink_init: cannot register pernet operations\n"); |
c1d10adb PNA |
3430 | goto err_unreg_exp_subsys; |
3431 | } | |
83f3e94d | 3432 | #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT |
9cb01766 | 3433 | /* setup interaction between nf_queue and nf_conntrack_netlink. */ |
a4b4766c | 3434 | RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook); |
9cb01766 | 3435 | #endif |
c1d10adb PNA |
3436 | return 0; |
3437 | ||
c1d10adb PNA |
3438 | err_unreg_exp_subsys: |
3439 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
c1d10adb PNA |
3440 | err_unreg_subsys: |
3441 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
3442 | err_out: | |
3443 | return ret; | |
3444 | } | |
3445 | ||
3446 | static void __exit ctnetlink_exit(void) | |
3447 | { | |
654d0fbd | 3448 | pr_info("ctnetlink: unregistering from nfnetlink.\n"); |
c1d10adb | 3449 | |
70e9942f | 3450 | unregister_pernet_subsys(&ctnetlink_net_ops); |
c1d10adb PNA |
3451 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); |
3452 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
83f3e94d | 3453 | #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT |
a4b4766c | 3454 | RCU_INIT_POINTER(nfnl_ct_hook, NULL); |
9cb01766 | 3455 | #endif |
3b7dabf0 | 3456 | synchronize_rcu(); |
c1d10adb PNA |
3457 | } |
3458 | ||
3459 | module_init(ctnetlink_init); | |
3460 | module_exit(ctnetlink_exit); |