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