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