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