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