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