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