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