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