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