]>
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> |
0adf9d67 | 7 | * (C) 2005-2008 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> | |
25 | #include <linux/skbuff.h> | |
26 | #include <linux/errno.h> | |
27 | #include <linux/netlink.h> | |
28 | #include <linux/spinlock.h> | |
40a839fd | 29 | #include <linux/interrupt.h> |
c1d10adb PNA |
30 | #include <linux/notifier.h> |
31 | ||
32 | #include <linux/netfilter.h> | |
dc5fc579 | 33 | #include <net/netlink.h> |
c1d10adb PNA |
34 | #include <net/netfilter/nf_conntrack.h> |
35 | #include <net/netfilter/nf_conntrack_core.h> | |
77ab9cff | 36 | #include <net/netfilter/nf_conntrack_expect.h> |
c1d10adb PNA |
37 | #include <net/netfilter/nf_conntrack_helper.h> |
38 | #include <net/netfilter/nf_conntrack_l3proto.h> | |
605dcad6 | 39 | #include <net/netfilter/nf_conntrack_l4proto.h> |
5b1158e9 | 40 | #include <net/netfilter/nf_conntrack_tuple.h> |
58401572 | 41 | #include <net/netfilter/nf_conntrack_acct.h> |
5b1158e9 JK |
42 | #ifdef CONFIG_NF_NAT_NEEDED |
43 | #include <net/netfilter/nf_nat_core.h> | |
44 | #include <net/netfilter/nf_nat_protocol.h> | |
45 | #endif | |
c1d10adb PNA |
46 | |
47 | #include <linux/netfilter/nfnetlink.h> | |
48 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
49 | ||
50 | MODULE_LICENSE("GPL"); | |
51 | ||
dc808fe2 | 52 | static char __initdata version[] = "0.93"; |
c1d10adb | 53 | |
c1d10adb | 54 | static inline int |
601e68e1 | 55 | ctnetlink_dump_tuples_proto(struct sk_buff *skb, |
1cde6436 | 56 | const struct nf_conntrack_tuple *tuple, |
605dcad6 | 57 | struct nf_conntrack_l4proto *l4proto) |
c1d10adb | 58 | { |
c1d10adb | 59 | int ret = 0; |
df6fb868 | 60 | struct nlattr *nest_parms; |
c1d10adb | 61 | |
df6fb868 PM |
62 | nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED); |
63 | if (!nest_parms) | |
64 | goto nla_put_failure; | |
77236b6e | 65 | NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum); |
c1d10adb | 66 | |
fdf70832 PM |
67 | if (likely(l4proto->tuple_to_nlattr)) |
68 | ret = l4proto->tuple_to_nlattr(skb, tuple); | |
601e68e1 | 69 | |
df6fb868 | 70 | nla_nest_end(skb, nest_parms); |
c1d10adb PNA |
71 | |
72 | return ret; | |
73 | ||
df6fb868 | 74 | nla_put_failure: |
c1d10adb PNA |
75 | return -1; |
76 | } | |
77 | ||
78 | static inline int | |
1cde6436 PNA |
79 | ctnetlink_dump_tuples_ip(struct sk_buff *skb, |
80 | const struct nf_conntrack_tuple *tuple, | |
81 | struct nf_conntrack_l3proto *l3proto) | |
c1d10adb | 82 | { |
c1d10adb | 83 | int ret = 0; |
df6fb868 PM |
84 | struct nlattr *nest_parms; |
85 | ||
86 | nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED); | |
87 | if (!nest_parms) | |
88 | goto nla_put_failure; | |
1cde6436 | 89 | |
fdf70832 PM |
90 | if (likely(l3proto->tuple_to_nlattr)) |
91 | ret = l3proto->tuple_to_nlattr(skb, tuple); | |
1cde6436 | 92 | |
df6fb868 | 93 | nla_nest_end(skb, nest_parms); |
c1d10adb | 94 | |
1cde6436 PNA |
95 | return ret; |
96 | ||
df6fb868 | 97 | nla_put_failure: |
1cde6436 PNA |
98 | return -1; |
99 | } | |
100 | ||
bb5cf80e | 101 | static int |
1cde6436 PNA |
102 | ctnetlink_dump_tuples(struct sk_buff *skb, |
103 | const struct nf_conntrack_tuple *tuple) | |
104 | { | |
105 | int ret; | |
106 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 107 | struct nf_conntrack_l4proto *l4proto; |
1cde6436 | 108 | |
528a3a6f | 109 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
1cde6436 | 110 | ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto); |
c1d10adb PNA |
111 | |
112 | if (unlikely(ret < 0)) | |
113 | return ret; | |
114 | ||
528a3a6f | 115 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); |
605dcad6 | 116 | ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto); |
c1d10adb PNA |
117 | |
118 | return ret; | |
c1d10adb PNA |
119 | } |
120 | ||
121 | static inline int | |
122 | ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct) | |
123 | { | |
77236b6e | 124 | NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status)); |
c1d10adb PNA |
125 | return 0; |
126 | ||
df6fb868 | 127 | nla_put_failure: |
c1d10adb PNA |
128 | return -1; |
129 | } | |
130 | ||
131 | static inline int | |
132 | ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct) | |
133 | { | |
77236b6e | 134 | long timeout = (ct->timeout.expires - jiffies) / HZ; |
c1d10adb | 135 | |
77236b6e | 136 | if (timeout < 0) |
c1d10adb | 137 | timeout = 0; |
601e68e1 | 138 | |
77236b6e | 139 | NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout)); |
c1d10adb PNA |
140 | return 0; |
141 | ||
df6fb868 | 142 | nla_put_failure: |
c1d10adb PNA |
143 | return -1; |
144 | } | |
145 | ||
146 | static inline int | |
147 | ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct) | |
148 | { | |
5e8fbe2a | 149 | struct nf_conntrack_l4proto *l4proto; |
df6fb868 | 150 | struct nlattr *nest_proto; |
c1d10adb PNA |
151 | int ret; |
152 | ||
528a3a6f PNA |
153 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
154 | if (!l4proto->to_nlattr) | |
c1d10adb | 155 | return 0; |
601e68e1 | 156 | |
df6fb868 PM |
157 | nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED); |
158 | if (!nest_proto) | |
159 | goto nla_put_failure; | |
c1d10adb | 160 | |
fdf70832 | 161 | ret = l4proto->to_nlattr(skb, nest_proto, ct); |
c1d10adb | 162 | |
df6fb868 | 163 | nla_nest_end(skb, nest_proto); |
c1d10adb PNA |
164 | |
165 | return ret; | |
166 | ||
df6fb868 | 167 | nla_put_failure: |
c1d10adb PNA |
168 | return -1; |
169 | } | |
170 | ||
171 | static inline int | |
172 | ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct) | |
173 | { | |
df6fb868 | 174 | struct nlattr *nest_helper; |
dc808fe2 | 175 | const struct nf_conn_help *help = nfct_help(ct); |
3c158f7f | 176 | struct nf_conntrack_helper *helper; |
c1d10adb | 177 | |
3c158f7f | 178 | if (!help) |
c1d10adb | 179 | return 0; |
601e68e1 | 180 | |
3c158f7f PM |
181 | helper = rcu_dereference(help->helper); |
182 | if (!helper) | |
183 | goto out; | |
184 | ||
df6fb868 PM |
185 | nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED); |
186 | if (!nest_helper) | |
187 | goto nla_put_failure; | |
77236b6e | 188 | NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name); |
c1d10adb | 189 | |
fdf70832 PM |
190 | if (helper->to_nlattr) |
191 | helper->to_nlattr(skb, ct); | |
c1d10adb | 192 | |
df6fb868 | 193 | nla_nest_end(skb, nest_helper); |
3c158f7f | 194 | out: |
c1d10adb PNA |
195 | return 0; |
196 | ||
df6fb868 | 197 | nla_put_failure: |
c1d10adb PNA |
198 | return -1; |
199 | } | |
200 | ||
bb5cf80e | 201 | static int |
c1d10adb PNA |
202 | ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct, |
203 | enum ip_conntrack_dir dir) | |
204 | { | |
205 | enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; | |
df6fb868 | 206 | struct nlattr *nest_count; |
58401572 KPO |
207 | const struct nf_conn_counter *acct; |
208 | ||
209 | acct = nf_conn_acct_find(ct); | |
210 | if (!acct) | |
211 | return 0; | |
c1d10adb | 212 | |
df6fb868 PM |
213 | nest_count = nla_nest_start(skb, type | NLA_F_NESTED); |
214 | if (!nest_count) | |
215 | goto nla_put_failure; | |
216 | ||
58401572 KPO |
217 | NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS, |
218 | cpu_to_be64(acct[dir].packets)); | |
219 | NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES, | |
220 | cpu_to_be64(acct[dir].bytes)); | |
c1d10adb | 221 | |
df6fb868 | 222 | nla_nest_end(skb, nest_count); |
c1d10adb PNA |
223 | |
224 | return 0; | |
225 | ||
df6fb868 | 226 | nla_put_failure: |
c1d10adb PNA |
227 | return -1; |
228 | } | |
c1d10adb PNA |
229 | |
230 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
231 | static inline int | |
232 | ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct) | |
233 | { | |
77236b6e | 234 | NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark)); |
c1d10adb PNA |
235 | return 0; |
236 | ||
df6fb868 | 237 | nla_put_failure: |
c1d10adb PNA |
238 | return -1; |
239 | } | |
240 | #else | |
241 | #define ctnetlink_dump_mark(a, b) (0) | |
242 | #endif | |
243 | ||
37fccd85 PNA |
244 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
245 | static inline int | |
246 | ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct) | |
247 | { | |
77236b6e | 248 | NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark)); |
37fccd85 PNA |
249 | return 0; |
250 | ||
251 | nla_put_failure: | |
252 | return -1; | |
253 | } | |
254 | #else | |
255 | #define ctnetlink_dump_secmark(a, b) (0) | |
256 | #endif | |
257 | ||
0f417ce9 PNA |
258 | #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple) |
259 | ||
260 | static inline int | |
261 | ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct) | |
262 | { | |
263 | struct nlattr *nest_parms; | |
264 | ||
265 | if (!(ct->status & IPS_EXPECTED)) | |
266 | return 0; | |
267 | ||
268 | nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED); | |
269 | if (!nest_parms) | |
270 | goto nla_put_failure; | |
271 | if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0) | |
272 | goto nla_put_failure; | |
273 | nla_nest_end(skb, nest_parms); | |
274 | ||
275 | return 0; | |
276 | ||
277 | nla_put_failure: | |
278 | return -1; | |
279 | } | |
280 | ||
13eae15a | 281 | #ifdef CONFIG_NF_NAT_NEEDED |
bb5cf80e | 282 | static int |
13eae15a PNA |
283 | dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type) |
284 | { | |
13eae15a PNA |
285 | struct nlattr *nest_parms; |
286 | ||
287 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); | |
288 | if (!nest_parms) | |
289 | goto nla_put_failure; | |
290 | ||
77236b6e PM |
291 | NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS, |
292 | htonl(natseq->correction_pos)); | |
293 | NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE, | |
294 | htonl(natseq->offset_before)); | |
295 | NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER, | |
296 | htonl(natseq->offset_after)); | |
13eae15a PNA |
297 | |
298 | nla_nest_end(skb, nest_parms); | |
299 | ||
300 | return 0; | |
301 | ||
302 | nla_put_failure: | |
303 | return -1; | |
304 | } | |
305 | ||
306 | static inline int | |
307 | ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct) | |
308 | { | |
309 | struct nf_nat_seq *natseq; | |
310 | struct nf_conn_nat *nat = nfct_nat(ct); | |
311 | ||
312 | if (!(ct->status & IPS_SEQ_ADJUST) || !nat) | |
313 | return 0; | |
314 | ||
315 | natseq = &nat->seq[IP_CT_DIR_ORIGINAL]; | |
316 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1) | |
317 | return -1; | |
318 | ||
319 | natseq = &nat->seq[IP_CT_DIR_REPLY]; | |
320 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1) | |
321 | return -1; | |
322 | ||
323 | return 0; | |
324 | } | |
325 | #else | |
326 | #define ctnetlink_dump_nat_seq_adj(a, b) (0) | |
327 | #endif | |
328 | ||
c1d10adb PNA |
329 | static inline int |
330 | ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) | |
331 | { | |
77236b6e | 332 | NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct)); |
c1d10adb PNA |
333 | return 0; |
334 | ||
df6fb868 | 335 | nla_put_failure: |
c1d10adb PNA |
336 | return -1; |
337 | } | |
338 | ||
339 | static inline int | |
340 | ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct) | |
341 | { | |
77236b6e | 342 | NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))); |
c1d10adb PNA |
343 | return 0; |
344 | ||
df6fb868 | 345 | nla_put_failure: |
c1d10adb PNA |
346 | return -1; |
347 | } | |
348 | ||
349 | #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple) | |
350 | ||
351 | static int | |
352 | ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, | |
601e68e1 | 353 | int event, int nowait, |
c1d10adb PNA |
354 | const struct nf_conn *ct) |
355 | { | |
356 | struct nlmsghdr *nlh; | |
357 | struct nfgenmsg *nfmsg; | |
df6fb868 | 358 | struct nlattr *nest_parms; |
27a884dc | 359 | unsigned char *b = skb_tail_pointer(skb); |
c1d10adb PNA |
360 | |
361 | event |= NFNL_SUBSYS_CTNETLINK << 8; | |
362 | nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg)); | |
363 | nfmsg = NLMSG_DATA(nlh); | |
364 | ||
365 | nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0; | |
5e8fbe2a | 366 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
367 | nfmsg->version = NFNETLINK_V0; |
368 | nfmsg->res_id = 0; | |
369 | ||
df6fb868 PM |
370 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
371 | if (!nest_parms) | |
372 | goto nla_put_failure; | |
c1d10adb | 373 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
374 | goto nla_put_failure; |
375 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 376 | |
df6fb868 PM |
377 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
378 | if (!nest_parms) | |
379 | goto nla_put_failure; | |
c1d10adb | 380 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
381 | goto nla_put_failure; |
382 | nla_nest_end(skb, nest_parms); | |
c1d10adb PNA |
383 | |
384 | if (ctnetlink_dump_status(skb, ct) < 0 || | |
385 | ctnetlink_dump_timeout(skb, ct) < 0 || | |
386 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || | |
387 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 || | |
388 | ctnetlink_dump_protoinfo(skb, ct) < 0 || | |
389 | ctnetlink_dump_helpinfo(skb, ct) < 0 || | |
390 | ctnetlink_dump_mark(skb, ct) < 0 || | |
37fccd85 | 391 | ctnetlink_dump_secmark(skb, ct) < 0 || |
c1d10adb | 392 | ctnetlink_dump_id(skb, ct) < 0 || |
13eae15a | 393 | ctnetlink_dump_use(skb, ct) < 0 || |
0f417ce9 | 394 | ctnetlink_dump_master(skb, ct) < 0 || |
13eae15a | 395 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
df6fb868 | 396 | goto nla_put_failure; |
c1d10adb | 397 | |
27a884dc | 398 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
c1d10adb PNA |
399 | return skb->len; |
400 | ||
401 | nlmsg_failure: | |
df6fb868 | 402 | nla_put_failure: |
dc5fc579 | 403 | nlmsg_trim(skb, b); |
c1d10adb PNA |
404 | return -1; |
405 | } | |
406 | ||
407 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2732c4e4 HE |
408 | /* |
409 | * The general structure of a ctnetlink event is | |
410 | * | |
411 | * CTA_TUPLE_ORIG | |
412 | * <l3/l4-proto-attributes> | |
413 | * CTA_TUPLE_REPLY | |
414 | * <l3/l4-proto-attributes> | |
415 | * CTA_ID | |
416 | * ... | |
417 | * CTA_PROTOINFO | |
418 | * <l4-proto-attributes> | |
419 | * CTA_TUPLE_MASTER | |
420 | * <l3/l4-proto-attributes> | |
421 | * | |
422 | * Therefore the formular is | |
423 | * | |
424 | * size = sizeof(headers) + sizeof(generic_nlas) + 3 * sizeof(tuple_nlas) | |
425 | * + sizeof(protoinfo_nlas) | |
426 | */ | |
427 | static struct sk_buff * | |
428 | ctnetlink_alloc_skb(const struct nf_conntrack_tuple *tuple, gfp_t gfp) | |
429 | { | |
430 | struct nf_conntrack_l3proto *l3proto; | |
431 | struct nf_conntrack_l4proto *l4proto; | |
432 | int len; | |
433 | ||
434 | #define NLA_TYPE_SIZE(type) nla_total_size(sizeof(type)) | |
435 | ||
436 | /* proto independant part */ | |
437 | len = NLMSG_SPACE(sizeof(struct nfgenmsg)) | |
438 | + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ | |
439 | + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ | |
440 | + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ | |
441 | + 3 * NLA_TYPE_SIZE(u_int8_t) /* CTA_PROTO_NUM */ | |
442 | + NLA_TYPE_SIZE(u_int32_t) /* CTA_ID */ | |
443 | + NLA_TYPE_SIZE(u_int32_t) /* CTA_STATUS */ | |
d271e8bd | 444 | #ifdef CONFIG_NF_CT_ACCT |
2732c4e4 HE |
445 | + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */ |
446 | + 2 * NLA_TYPE_SIZE(uint64_t) /* CTA_COUNTERS_PACKETS */ | |
447 | + 2 * NLA_TYPE_SIZE(uint64_t) /* CTA_COUNTERS_BYTES */ | |
d271e8bd | 448 | #endif |
2732c4e4 HE |
449 | + NLA_TYPE_SIZE(u_int32_t) /* CTA_TIMEOUT */ |
450 | + nla_total_size(0) /* CTA_PROTOINFO */ | |
451 | + nla_total_size(0) /* CTA_HELP */ | |
452 | + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ | |
d271e8bd | 453 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
2732c4e4 | 454 | + NLA_TYPE_SIZE(u_int32_t) /* CTA_SECMARK */ |
d271e8bd HE |
455 | #endif |
456 | #ifdef CONFIG_NF_NAT_NEEDED | |
2732c4e4 HE |
457 | + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ |
458 | + 2 * NLA_TYPE_SIZE(u_int32_t) /* CTA_NAT_SEQ_CORRECTION_POS */ | |
459 | + 2 * NLA_TYPE_SIZE(u_int32_t) /* CTA_NAT_SEQ_CORRECTION_BEFORE */ | |
460 | + 2 * NLA_TYPE_SIZE(u_int32_t) /* CTA_NAT_SEQ_CORRECTION_AFTER */ | |
d271e8bd HE |
461 | #endif |
462 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
463 | + NLA_TYPE_SIZE(u_int32_t) /* CTA_MARK */ | |
464 | #endif | |
465 | ; | |
2732c4e4 HE |
466 | |
467 | #undef NLA_TYPE_SIZE | |
468 | ||
469 | rcu_read_lock(); | |
470 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); | |
471 | len += l3proto->nla_size; | |
472 | ||
473 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); | |
474 | len += l4proto->nla_size; | |
475 | rcu_read_unlock(); | |
476 | ||
477 | return alloc_skb(len, gfp); | |
478 | } | |
479 | ||
c1d10adb | 480 | static int ctnetlink_conntrack_event(struct notifier_block *this, |
601e68e1 | 481 | unsigned long events, void *ptr) |
c1d10adb PNA |
482 | { |
483 | struct nlmsghdr *nlh; | |
484 | struct nfgenmsg *nfmsg; | |
df6fb868 | 485 | struct nlattr *nest_parms; |
19abb7b0 PNA |
486 | struct nf_ct_event *item = (struct nf_ct_event *)ptr; |
487 | struct nf_conn *ct = item->ct; | |
c1d10adb PNA |
488 | struct sk_buff *skb; |
489 | unsigned int type; | |
27a884dc | 490 | sk_buff_data_t b; |
c1d10adb PNA |
491 | unsigned int flags = 0, group; |
492 | ||
493 | /* ignore our fake conntrack entry */ | |
494 | if (ct == &nf_conntrack_untracked) | |
495 | return NOTIFY_DONE; | |
496 | ||
497 | if (events & IPCT_DESTROY) { | |
498 | type = IPCTNL_MSG_CT_DELETE; | |
499 | group = NFNLGRP_CONNTRACK_DESTROY; | |
500 | } else if (events & (IPCT_NEW | IPCT_RELATED)) { | |
501 | type = IPCTNL_MSG_CT_NEW; | |
502 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
c1d10adb | 503 | group = NFNLGRP_CONNTRACK_NEW; |
1a31526b | 504 | } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) { |
c1d10adb PNA |
505 | type = IPCTNL_MSG_CT_NEW; |
506 | group = NFNLGRP_CONNTRACK_UPDATE; | |
507 | } else | |
508 | return NOTIFY_DONE; | |
a2427692 | 509 | |
1f9da256 | 510 | if (!item->report && !nfnetlink_has_listeners(group)) |
a2427692 PM |
511 | return NOTIFY_DONE; |
512 | ||
2732c4e4 | 513 | skb = ctnetlink_alloc_skb(tuple(ct, IP_CT_DIR_ORIGINAL), GFP_ATOMIC); |
c1d10adb PNA |
514 | if (!skb) |
515 | return NOTIFY_DONE; | |
516 | ||
517 | b = skb->tail; | |
518 | ||
519 | type |= NFNL_SUBSYS_CTNETLINK << 8; | |
19abb7b0 | 520 | nlh = NLMSG_PUT(skb, item->pid, 0, type, sizeof(struct nfgenmsg)); |
c1d10adb PNA |
521 | nfmsg = NLMSG_DATA(nlh); |
522 | ||
523 | nlh->nlmsg_flags = flags; | |
5e8fbe2a | 524 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
525 | nfmsg->version = NFNETLINK_V0; |
526 | nfmsg->res_id = 0; | |
527 | ||
528a3a6f | 528 | rcu_read_lock(); |
df6fb868 PM |
529 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
530 | if (!nest_parms) | |
531 | goto nla_put_failure; | |
c1d10adb | 532 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
533 | goto nla_put_failure; |
534 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 535 | |
df6fb868 PM |
536 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
537 | if (!nest_parms) | |
538 | goto nla_put_failure; | |
c1d10adb | 539 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
540 | goto nla_put_failure; |
541 | nla_nest_end(skb, nest_parms); | |
c1d10adb | 542 | |
1eedf699 EL |
543 | if (ctnetlink_dump_id(skb, ct) < 0) |
544 | goto nla_put_failure; | |
545 | ||
e57dce60 FH |
546 | if (ctnetlink_dump_status(skb, ct) < 0) |
547 | goto nla_put_failure; | |
548 | ||
7b621c1e PNA |
549 | if (events & IPCT_DESTROY) { |
550 | if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || | |
551 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0) | |
df6fb868 | 552 | goto nla_put_failure; |
7b621c1e | 553 | } else { |
7b621c1e | 554 | if (ctnetlink_dump_timeout(skb, ct) < 0) |
df6fb868 | 555 | goto nla_put_failure; |
7b621c1e PNA |
556 | |
557 | if (events & IPCT_PROTOINFO | |
558 | && ctnetlink_dump_protoinfo(skb, ct) < 0) | |
df6fb868 | 559 | goto nla_put_failure; |
7b621c1e PNA |
560 | |
561 | if ((events & IPCT_HELPER || nfct_help(ct)) | |
562 | && ctnetlink_dump_helpinfo(skb, ct) < 0) | |
df6fb868 | 563 | goto nla_put_failure; |
7b621c1e | 564 | |
37fccd85 PNA |
565 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
566 | if ((events & IPCT_SECMARK || ct->secmark) | |
567 | && ctnetlink_dump_secmark(skb, ct) < 0) | |
568 | goto nla_put_failure; | |
569 | #endif | |
7b621c1e | 570 | |
0f417ce9 PNA |
571 | if (events & IPCT_RELATED && |
572 | ctnetlink_dump_master(skb, ct) < 0) | |
573 | goto nla_put_failure; | |
574 | ||
13eae15a PNA |
575 | if (events & IPCT_NATSEQADJ && |
576 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) | |
577 | goto nla_put_failure; | |
7b621c1e | 578 | } |
b9a37e0c | 579 | |
a83099a6 EL |
580 | #ifdef CONFIG_NF_CONNTRACK_MARK |
581 | if ((events & IPCT_MARK || ct->mark) | |
582 | && ctnetlink_dump_mark(skb, ct) < 0) | |
583 | goto nla_put_failure; | |
584 | #endif | |
528a3a6f | 585 | rcu_read_unlock(); |
a83099a6 | 586 | |
c1d10adb | 587 | nlh->nlmsg_len = skb->tail - b; |
19abb7b0 | 588 | nfnetlink_send(skb, item->pid, group, item->report); |
c1d10adb PNA |
589 | return NOTIFY_DONE; |
590 | ||
df6fb868 | 591 | nla_put_failure: |
528a3a6f PNA |
592 | rcu_read_unlock(); |
593 | nlmsg_failure: | |
dd5b6ce6 | 594 | nfnetlink_set_err(0, group, -ENOBUFS); |
c1d10adb PNA |
595 | kfree_skb(skb); |
596 | return NOTIFY_DONE; | |
597 | } | |
598 | #endif /* CONFIG_NF_CONNTRACK_EVENTS */ | |
599 | ||
600 | static int ctnetlink_done(struct netlink_callback *cb) | |
601 | { | |
89f2e218 PM |
602 | if (cb->args[1]) |
603 | nf_ct_put((struct nf_conn *)cb->args[1]); | |
c1d10adb PNA |
604 | return 0; |
605 | } | |
606 | ||
607 | static int | |
608 | ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
609 | { | |
89f2e218 | 610 | struct nf_conn *ct, *last; |
c1d10adb | 611 | struct nf_conntrack_tuple_hash *h; |
ea781f19 | 612 | struct hlist_nulls_node *n; |
87711cb8 PNA |
613 | struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh); |
614 | u_int8_t l3proto = nfmsg->nfgen_family; | |
c1d10adb | 615 | |
76507f69 | 616 | rcu_read_lock(); |
d205dc40 | 617 | last = (struct nf_conn *)cb->args[1]; |
89f2e218 PM |
618 | for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) { |
619 | restart: | |
ea781f19 ED |
620 | hlist_nulls_for_each_entry_rcu(h, n, &init_net.ct.hash[cb->args[0]], |
621 | hnnode) { | |
5b1158e9 | 622 | if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
c1d10adb PNA |
623 | continue; |
624 | ct = nf_ct_tuplehash_to_ctrack(h); | |
ea781f19 ED |
625 | if (!atomic_inc_not_zero(&ct->ct_general.use)) |
626 | continue; | |
87711cb8 PNA |
627 | /* Dump entries of a given L3 protocol number. |
628 | * If it is not specified, ie. l3proto == 0, | |
629 | * then dump everything. */ | |
5e8fbe2a | 630 | if (l3proto && nf_ct_l3num(ct) != l3proto) |
ea781f19 | 631 | goto releasect; |
d205dc40 PM |
632 | if (cb->args[1]) { |
633 | if (ct != last) | |
ea781f19 | 634 | goto releasect; |
d205dc40 | 635 | cb->args[1] = 0; |
89f2e218 | 636 | } |
c1d10adb | 637 | if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, |
601e68e1 | 638 | cb->nlh->nlmsg_seq, |
c1d10adb | 639 | IPCTNL_MSG_CT_NEW, |
89f2e218 | 640 | 1, ct) < 0) { |
89f2e218 | 641 | cb->args[1] = (unsigned long)ct; |
c1d10adb | 642 | goto out; |
89f2e218 | 643 | } |
58401572 | 644 | |
01f34848 | 645 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == |
58401572 KPO |
646 | IPCTNL_MSG_CT_GET_CTRZERO) { |
647 | struct nf_conn_counter *acct; | |
648 | ||
649 | acct = nf_conn_acct_find(ct); | |
650 | if (acct) | |
651 | memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX])); | |
652 | } | |
ea781f19 ED |
653 | releasect: |
654 | nf_ct_put(ct); | |
89f2e218 | 655 | } |
d205dc40 | 656 | if (cb->args[1]) { |
89f2e218 PM |
657 | cb->args[1] = 0; |
658 | goto restart; | |
c1d10adb PNA |
659 | } |
660 | } | |
89f2e218 | 661 | out: |
76507f69 | 662 | rcu_read_unlock(); |
d205dc40 PM |
663 | if (last) |
664 | nf_ct_put(last); | |
c1d10adb | 665 | |
c1d10adb PNA |
666 | return skb->len; |
667 | } | |
668 | ||
c1d10adb | 669 | static inline int |
df6fb868 | 670 | ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple) |
c1d10adb | 671 | { |
df6fb868 | 672 | struct nlattr *tb[CTA_IP_MAX+1]; |
c1d10adb PNA |
673 | struct nf_conntrack_l3proto *l3proto; |
674 | int ret = 0; | |
675 | ||
df6fb868 | 676 | nla_parse_nested(tb, CTA_IP_MAX, attr, NULL); |
c1d10adb | 677 | |
cd91566e FW |
678 | rcu_read_lock(); |
679 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); | |
c1d10adb | 680 | |
f73e924c PM |
681 | if (likely(l3proto->nlattr_to_tuple)) { |
682 | ret = nla_validate_nested(attr, CTA_IP_MAX, | |
683 | l3proto->nla_policy); | |
684 | if (ret == 0) | |
685 | ret = l3proto->nlattr_to_tuple(tb, tuple); | |
686 | } | |
c1d10adb | 687 | |
cd91566e | 688 | rcu_read_unlock(); |
c1d10adb | 689 | |
c1d10adb PNA |
690 | return ret; |
691 | } | |
692 | ||
f73e924c PM |
693 | static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = { |
694 | [CTA_PROTO_NUM] = { .type = NLA_U8 }, | |
c1d10adb PNA |
695 | }; |
696 | ||
697 | static inline int | |
df6fb868 | 698 | ctnetlink_parse_tuple_proto(struct nlattr *attr, |
c1d10adb PNA |
699 | struct nf_conntrack_tuple *tuple) |
700 | { | |
df6fb868 | 701 | struct nlattr *tb[CTA_PROTO_MAX+1]; |
605dcad6 | 702 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
703 | int ret = 0; |
704 | ||
f73e924c PM |
705 | ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy); |
706 | if (ret < 0) | |
707 | return ret; | |
c1d10adb | 708 | |
df6fb868 | 709 | if (!tb[CTA_PROTO_NUM]) |
c1d10adb | 710 | return -EINVAL; |
77236b6e | 711 | tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]); |
c1d10adb | 712 | |
cd91566e FW |
713 | rcu_read_lock(); |
714 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); | |
c1d10adb | 715 | |
f73e924c PM |
716 | if (likely(l4proto->nlattr_to_tuple)) { |
717 | ret = nla_validate_nested(attr, CTA_PROTO_MAX, | |
718 | l4proto->nla_policy); | |
719 | if (ret == 0) | |
720 | ret = l4proto->nlattr_to_tuple(tb, tuple); | |
721 | } | |
c1d10adb | 722 | |
cd91566e | 723 | rcu_read_unlock(); |
601e68e1 | 724 | |
c1d10adb PNA |
725 | return ret; |
726 | } | |
727 | ||
bb5cf80e | 728 | static int |
df6fb868 | 729 | ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple, |
c1d10adb PNA |
730 | enum ctattr_tuple type, u_int8_t l3num) |
731 | { | |
df6fb868 | 732 | struct nlattr *tb[CTA_TUPLE_MAX+1]; |
c1d10adb PNA |
733 | int err; |
734 | ||
c1d10adb PNA |
735 | memset(tuple, 0, sizeof(*tuple)); |
736 | ||
df6fb868 | 737 | nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL); |
c1d10adb | 738 | |
df6fb868 | 739 | if (!tb[CTA_TUPLE_IP]) |
c1d10adb PNA |
740 | return -EINVAL; |
741 | ||
742 | tuple->src.l3num = l3num; | |
743 | ||
df6fb868 | 744 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple); |
c1d10adb PNA |
745 | if (err < 0) |
746 | return err; | |
747 | ||
df6fb868 | 748 | if (!tb[CTA_TUPLE_PROTO]) |
c1d10adb PNA |
749 | return -EINVAL; |
750 | ||
df6fb868 | 751 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple); |
c1d10adb PNA |
752 | if (err < 0) |
753 | return err; | |
754 | ||
755 | /* orig and expect tuples get DIR_ORIGINAL */ | |
756 | if (type == CTA_TUPLE_REPLY) | |
757 | tuple->dst.dir = IP_CT_DIR_REPLY; | |
758 | else | |
759 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
760 | ||
c1d10adb PNA |
761 | return 0; |
762 | } | |
763 | ||
c1d10adb | 764 | static inline int |
df6fb868 | 765 | ctnetlink_parse_help(struct nlattr *attr, char **helper_name) |
c1d10adb | 766 | { |
df6fb868 | 767 | struct nlattr *tb[CTA_HELP_MAX+1]; |
c1d10adb | 768 | |
df6fb868 | 769 | nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL); |
c1d10adb | 770 | |
df6fb868 | 771 | if (!tb[CTA_HELP_NAME]) |
c1d10adb PNA |
772 | return -EINVAL; |
773 | ||
df6fb868 | 774 | *helper_name = nla_data(tb[CTA_HELP_NAME]); |
c1d10adb PNA |
775 | |
776 | return 0; | |
777 | } | |
778 | ||
f73e924c PM |
779 | static const struct nla_policy ct_nla_policy[CTA_MAX+1] = { |
780 | [CTA_STATUS] = { .type = NLA_U32 }, | |
781 | [CTA_TIMEOUT] = { .type = NLA_U32 }, | |
782 | [CTA_MARK] = { .type = NLA_U32 }, | |
783 | [CTA_USE] = { .type = NLA_U32 }, | |
784 | [CTA_ID] = { .type = NLA_U32 }, | |
c1d10adb PNA |
785 | }; |
786 | ||
787 | static int | |
601e68e1 | 788 | ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, |
df6fb868 | 789 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
c1d10adb PNA |
790 | { |
791 | struct nf_conntrack_tuple_hash *h; | |
792 | struct nf_conntrack_tuple tuple; | |
793 | struct nf_conn *ct; | |
794 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
795 | u_int8_t u3 = nfmsg->nfgen_family; | |
796 | int err = 0; | |
797 | ||
df6fb868 | 798 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 799 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 800 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
801 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
802 | else { | |
803 | /* Flush the whole table */ | |
19abb7b0 PNA |
804 | nf_conntrack_flush(&init_net, |
805 | NETLINK_CB(skb).pid, | |
806 | nlmsg_report(nlh)); | |
c1d10adb PNA |
807 | return 0; |
808 | } | |
809 | ||
810 | if (err < 0) | |
811 | return err; | |
812 | ||
400dad39 | 813 | h = nf_conntrack_find_get(&init_net, &tuple); |
9ea8cfd6 | 814 | if (!h) |
c1d10adb | 815 | return -ENOENT; |
c1d10adb PNA |
816 | |
817 | ct = nf_ct_tuplehash_to_ctrack(h); | |
601e68e1 | 818 | |
df6fb868 | 819 | if (cda[CTA_ID]) { |
77236b6e | 820 | u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID])); |
7f85f914 | 821 | if (id != (u32)(unsigned long)ct) { |
c1d10adb PNA |
822 | nf_ct_put(ct); |
823 | return -ENOENT; | |
824 | } | |
601e68e1 | 825 | } |
c1d10adb | 826 | |
19abb7b0 PNA |
827 | nf_conntrack_event_report(IPCT_DESTROY, |
828 | ct, | |
829 | NETLINK_CB(skb).pid, | |
830 | nlmsg_report(nlh)); | |
831 | ||
832 | /* death_by_timeout would report the event again */ | |
833 | set_bit(IPS_DYING_BIT, &ct->status); | |
834 | ||
51091764 | 835 | nf_ct_kill(ct); |
c1d10adb | 836 | nf_ct_put(ct); |
c1d10adb PNA |
837 | |
838 | return 0; | |
839 | } | |
840 | ||
841 | static int | |
601e68e1 | 842 | ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, |
df6fb868 | 843 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
c1d10adb PNA |
844 | { |
845 | struct nf_conntrack_tuple_hash *h; | |
846 | struct nf_conntrack_tuple tuple; | |
847 | struct nf_conn *ct; | |
848 | struct sk_buff *skb2 = NULL; | |
849 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
850 | u_int8_t u3 = nfmsg->nfgen_family; | |
851 | int err = 0; | |
852 | ||
58401572 | 853 | if (nlh->nlmsg_flags & NLM_F_DUMP) |
c702e804 TG |
854 | return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table, |
855 | ctnetlink_done); | |
c1d10adb | 856 | |
df6fb868 | 857 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 858 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 859 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
860 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
861 | else | |
862 | return -EINVAL; | |
863 | ||
864 | if (err < 0) | |
865 | return err; | |
866 | ||
400dad39 | 867 | h = nf_conntrack_find_get(&init_net, &tuple); |
9ea8cfd6 | 868 | if (!h) |
c1d10adb | 869 | return -ENOENT; |
9ea8cfd6 | 870 | |
c1d10adb PNA |
871 | ct = nf_ct_tuplehash_to_ctrack(h); |
872 | ||
873 | err = -ENOMEM; | |
874 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
875 | if (!skb2) { | |
876 | nf_ct_put(ct); | |
877 | return -ENOMEM; | |
878 | } | |
c1d10adb | 879 | |
528a3a6f | 880 | rcu_read_lock(); |
601e68e1 | 881 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, |
c1d10adb | 882 | IPCTNL_MSG_CT_NEW, 1, ct); |
528a3a6f | 883 | rcu_read_unlock(); |
c1d10adb PNA |
884 | nf_ct_put(ct); |
885 | if (err <= 0) | |
886 | goto free; | |
887 | ||
888 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
889 | if (err < 0) | |
890 | goto out; | |
891 | ||
c1d10adb PNA |
892 | return 0; |
893 | ||
894 | free: | |
895 | kfree_skb(skb2); | |
896 | out: | |
897 | return err; | |
898 | } | |
899 | ||
67671841 | 900 | #ifdef CONFIG_NF_NAT_NEEDED |
e6a7d3c0 PNA |
901 | static int |
902 | ctnetlink_parse_nat_setup(struct nf_conn *ct, | |
903 | enum nf_nat_manip_type manip, | |
904 | struct nlattr *attr) | |
905 | { | |
906 | typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup; | |
907 | ||
908 | parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook); | |
909 | if (!parse_nat_setup) { | |
95a5afca | 910 | #ifdef CONFIG_MODULES |
e6a7d3c0 | 911 | rcu_read_unlock(); |
748085fc | 912 | spin_unlock_bh(&nf_conntrack_lock); |
e6a7d3c0 PNA |
913 | nfnl_unlock(); |
914 | if (request_module("nf-nat-ipv4") < 0) { | |
915 | nfnl_lock(); | |
748085fc | 916 | spin_lock_bh(&nf_conntrack_lock); |
e6a7d3c0 PNA |
917 | rcu_read_lock(); |
918 | return -EOPNOTSUPP; | |
919 | } | |
920 | nfnl_lock(); | |
748085fc | 921 | spin_lock_bh(&nf_conntrack_lock); |
e6a7d3c0 PNA |
922 | rcu_read_lock(); |
923 | if (nfnetlink_parse_nat_setup_hook) | |
924 | return -EAGAIN; | |
925 | #endif | |
926 | return -EOPNOTSUPP; | |
927 | } | |
928 | ||
929 | return parse_nat_setup(ct, manip, attr); | |
930 | } | |
67671841 | 931 | #endif |
e6a7d3c0 | 932 | |
bb5cf80e | 933 | static int |
df6fb868 | 934 | ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[]) |
c1d10adb PNA |
935 | { |
936 | unsigned long d; | |
77236b6e | 937 | unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS])); |
c1d10adb PNA |
938 | d = ct->status ^ status; |
939 | ||
940 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) | |
941 | /* unchangeable */ | |
0adf9d67 | 942 | return -EBUSY; |
601e68e1 | 943 | |
c1d10adb PNA |
944 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) |
945 | /* SEEN_REPLY bit can only be set */ | |
0adf9d67 | 946 | return -EBUSY; |
601e68e1 | 947 | |
c1d10adb PNA |
948 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) |
949 | /* ASSURED bit can only be set */ | |
0adf9d67 | 950 | return -EBUSY; |
c1d10adb | 951 | |
c1d10adb PNA |
952 | /* Be careful here, modifying NAT bits can screw up things, |
953 | * so don't let users modify them directly if they don't pass | |
5b1158e9 | 954 | * nf_nat_range. */ |
c1d10adb PNA |
955 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); |
956 | return 0; | |
957 | } | |
958 | ||
e6a7d3c0 PNA |
959 | static int |
960 | ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[]) | |
961 | { | |
962 | #ifdef CONFIG_NF_NAT_NEEDED | |
963 | int ret; | |
964 | ||
965 | if (cda[CTA_NAT_DST]) { | |
966 | ret = ctnetlink_parse_nat_setup(ct, | |
967 | IP_NAT_MANIP_DST, | |
968 | cda[CTA_NAT_DST]); | |
969 | if (ret < 0) | |
970 | return ret; | |
971 | } | |
972 | if (cda[CTA_NAT_SRC]) { | |
973 | ret = ctnetlink_parse_nat_setup(ct, | |
974 | IP_NAT_MANIP_SRC, | |
975 | cda[CTA_NAT_SRC]); | |
976 | if (ret < 0) | |
977 | return ret; | |
978 | } | |
979 | return 0; | |
980 | #else | |
981 | return -EOPNOTSUPP; | |
982 | #endif | |
983 | } | |
c1d10adb PNA |
984 | |
985 | static inline int | |
df6fb868 | 986 | ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[]) |
c1d10adb PNA |
987 | { |
988 | struct nf_conntrack_helper *helper; | |
dc808fe2 | 989 | struct nf_conn_help *help = nfct_help(ct); |
c1d10adb PNA |
990 | char *helpname; |
991 | int err; | |
992 | ||
c1d10adb PNA |
993 | /* don't change helper of sibling connections */ |
994 | if (ct->master) | |
0adf9d67 | 995 | return -EBUSY; |
c1d10adb | 996 | |
df6fb868 | 997 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); |
c1d10adb PNA |
998 | if (err < 0) |
999 | return err; | |
1000 | ||
df293bbb YK |
1001 | if (!strcmp(helpname, "")) { |
1002 | if (help && help->helper) { | |
c1d10adb PNA |
1003 | /* we had a helper before ... */ |
1004 | nf_ct_remove_expectations(ct); | |
3c158f7f | 1005 | rcu_assign_pointer(help->helper, NULL); |
c1d10adb | 1006 | } |
df293bbb YK |
1007 | |
1008 | return 0; | |
c1d10adb | 1009 | } |
601e68e1 | 1010 | |
df293bbb | 1011 | helper = __nf_conntrack_helper_find_byname(helpname); |
226c0c0e PNA |
1012 | if (helper == NULL) { |
1013 | #ifdef CONFIG_MODULES | |
1014 | spin_unlock_bh(&nf_conntrack_lock); | |
1015 | ||
1016 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1017 | spin_lock_bh(&nf_conntrack_lock); | |
1018 | return -EOPNOTSUPP; | |
1019 | } | |
1020 | ||
1021 | spin_lock_bh(&nf_conntrack_lock); | |
1022 | helper = __nf_conntrack_helper_find_byname(helpname); | |
1023 | if (helper) | |
1024 | return -EAGAIN; | |
1025 | #endif | |
0adf9d67 | 1026 | return -EOPNOTSUPP; |
226c0c0e | 1027 | } |
df293bbb | 1028 | |
ceceae1b YK |
1029 | if (help) { |
1030 | if (help->helper == helper) | |
1031 | return 0; | |
1032 | if (help->helper) | |
1033 | return -EBUSY; | |
1034 | /* need to zero data of old helper */ | |
1035 | memset(&help->help, 0, sizeof(help->help)); | |
1036 | } else { | |
fab00c5d | 1037 | help = nf_ct_helper_ext_add(ct, GFP_ATOMIC); |
ceceae1b YK |
1038 | if (help == NULL) |
1039 | return -ENOMEM; | |
1040 | } | |
df293bbb | 1041 | |
3c158f7f | 1042 | rcu_assign_pointer(help->helper, helper); |
c1d10adb PNA |
1043 | |
1044 | return 0; | |
1045 | } | |
1046 | ||
1047 | static inline int | |
df6fb868 | 1048 | ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[]) |
c1d10adb | 1049 | { |
77236b6e | 1050 | u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
601e68e1 | 1051 | |
c1d10adb PNA |
1052 | if (!del_timer(&ct->timeout)) |
1053 | return -ETIME; | |
1054 | ||
1055 | ct->timeout.expires = jiffies + timeout * HZ; | |
1056 | add_timer(&ct->timeout); | |
1057 | ||
1058 | return 0; | |
1059 | } | |
1060 | ||
1061 | static inline int | |
df6fb868 | 1062 | ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[]) |
c1d10adb | 1063 | { |
df6fb868 | 1064 | struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO]; |
605dcad6 | 1065 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
1066 | int err = 0; |
1067 | ||
df6fb868 | 1068 | nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL); |
c1d10adb | 1069 | |
cd91566e FW |
1070 | rcu_read_lock(); |
1071 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
fdf70832 PM |
1072 | if (l4proto->from_nlattr) |
1073 | err = l4proto->from_nlattr(tb, ct); | |
cd91566e | 1074 | rcu_read_unlock(); |
c1d10adb PNA |
1075 | |
1076 | return err; | |
1077 | } | |
1078 | ||
13eae15a PNA |
1079 | #ifdef CONFIG_NF_NAT_NEEDED |
1080 | static inline int | |
1081 | change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr) | |
1082 | { | |
1083 | struct nlattr *cda[CTA_NAT_SEQ_MAX+1]; | |
1084 | ||
1085 | nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL); | |
1086 | ||
1087 | if (!cda[CTA_NAT_SEQ_CORRECTION_POS]) | |
1088 | return -EINVAL; | |
1089 | ||
1090 | natseq->correction_pos = | |
77236b6e | 1091 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS])); |
13eae15a PNA |
1092 | |
1093 | if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE]) | |
1094 | return -EINVAL; | |
1095 | ||
1096 | natseq->offset_before = | |
77236b6e | 1097 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE])); |
13eae15a PNA |
1098 | |
1099 | if (!cda[CTA_NAT_SEQ_OFFSET_AFTER]) | |
1100 | return -EINVAL; | |
1101 | ||
1102 | natseq->offset_after = | |
77236b6e | 1103 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER])); |
13eae15a PNA |
1104 | |
1105 | return 0; | |
1106 | } | |
1107 | ||
1108 | static int | |
1109 | ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[]) | |
1110 | { | |
1111 | int ret = 0; | |
1112 | struct nf_conn_nat *nat = nfct_nat(ct); | |
1113 | ||
1114 | if (!nat) | |
1115 | return 0; | |
1116 | ||
1117 | if (cda[CTA_NAT_SEQ_ADJ_ORIG]) { | |
1118 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL], | |
1119 | cda[CTA_NAT_SEQ_ADJ_ORIG]); | |
1120 | if (ret < 0) | |
1121 | return ret; | |
1122 | ||
1123 | ct->status |= IPS_SEQ_ADJUST; | |
1124 | } | |
1125 | ||
1126 | if (cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1127 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY], | |
1128 | cda[CTA_NAT_SEQ_ADJ_REPLY]); | |
1129 | if (ret < 0) | |
1130 | return ret; | |
1131 | ||
1132 | ct->status |= IPS_SEQ_ADJUST; | |
1133 | } | |
1134 | ||
1135 | return 0; | |
1136 | } | |
1137 | #endif | |
1138 | ||
c1d10adb | 1139 | static int |
df6fb868 | 1140 | ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[]) |
c1d10adb PNA |
1141 | { |
1142 | int err; | |
1143 | ||
e098360f PNA |
1144 | /* only allow NAT changes and master assignation for new conntracks */ |
1145 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER]) | |
1146 | return -EOPNOTSUPP; | |
1147 | ||
df6fb868 | 1148 | if (cda[CTA_HELP]) { |
c1d10adb PNA |
1149 | err = ctnetlink_change_helper(ct, cda); |
1150 | if (err < 0) | |
1151 | return err; | |
1152 | } | |
1153 | ||
df6fb868 | 1154 | if (cda[CTA_TIMEOUT]) { |
c1d10adb PNA |
1155 | err = ctnetlink_change_timeout(ct, cda); |
1156 | if (err < 0) | |
1157 | return err; | |
1158 | } | |
1159 | ||
df6fb868 | 1160 | if (cda[CTA_STATUS]) { |
c1d10adb PNA |
1161 | err = ctnetlink_change_status(ct, cda); |
1162 | if (err < 0) | |
1163 | return err; | |
1164 | } | |
1165 | ||
df6fb868 | 1166 | if (cda[CTA_PROTOINFO]) { |
c1d10adb PNA |
1167 | err = ctnetlink_change_protoinfo(ct, cda); |
1168 | if (err < 0) | |
1169 | return err; | |
1170 | } | |
1171 | ||
bcd1e830 | 1172 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1173 | if (cda[CTA_MARK]) |
77236b6e | 1174 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1175 | #endif |
1176 | ||
13eae15a PNA |
1177 | #ifdef CONFIG_NF_NAT_NEEDED |
1178 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1179 | err = ctnetlink_change_nat_seq_adj(ct, cda); | |
1180 | if (err < 0) | |
1181 | return err; | |
1182 | } | |
1183 | #endif | |
1184 | ||
c1d10adb PNA |
1185 | return 0; |
1186 | } | |
1187 | ||
19abb7b0 PNA |
1188 | static inline void |
1189 | ctnetlink_event_report(struct nf_conn *ct, u32 pid, int report) | |
1190 | { | |
1191 | unsigned int events = 0; | |
1192 | ||
1193 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) | |
1194 | events |= IPCT_RELATED; | |
1195 | else | |
1196 | events |= IPCT_NEW; | |
1197 | ||
1198 | nf_conntrack_event_report(IPCT_STATUS | | |
1199 | IPCT_HELPER | | |
1200 | IPCT_REFRESH | | |
1201 | IPCT_PROTOINFO | | |
1202 | IPCT_NATSEQADJ | | |
1203 | IPCT_MARK | | |
1204 | events, | |
1205 | ct, | |
1206 | pid, | |
1207 | report); | |
1208 | } | |
1209 | ||
f0a3c086 | 1210 | static struct nf_conn * |
df6fb868 | 1211 | ctnetlink_create_conntrack(struct nlattr *cda[], |
c1d10adb | 1212 | struct nf_conntrack_tuple *otuple, |
5faa1f4c | 1213 | struct nf_conntrack_tuple *rtuple, |
7ec47496 | 1214 | u8 u3) |
c1d10adb PNA |
1215 | { |
1216 | struct nf_conn *ct; | |
1217 | int err = -EINVAL; | |
ceceae1b | 1218 | struct nf_conntrack_helper *helper; |
c1d10adb | 1219 | |
b54ad409 | 1220 | ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_ATOMIC); |
cd7fcbf1 | 1221 | if (IS_ERR(ct)) |
f0a3c086 | 1222 | return ERR_PTR(-ENOMEM); |
c1d10adb | 1223 | |
df6fb868 | 1224 | if (!cda[CTA_TIMEOUT]) |
0f5b3e85 | 1225 | goto err1; |
77236b6e | 1226 | ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
c1d10adb PNA |
1227 | |
1228 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; | |
1229 | ct->status |= IPS_CONFIRMED; | |
1230 | ||
1575e7ea | 1231 | rcu_read_lock(); |
226c0c0e PNA |
1232 | if (cda[CTA_HELP]) { |
1233 | char *helpname; | |
1234 | ||
1235 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); | |
0f5b3e85 PM |
1236 | if (err < 0) |
1237 | goto err2; | |
226c0c0e PNA |
1238 | |
1239 | helper = __nf_conntrack_helper_find_byname(helpname); | |
1240 | if (helper == NULL) { | |
1241 | rcu_read_unlock(); | |
1242 | #ifdef CONFIG_MODULES | |
1243 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1244 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1245 | goto err1; |
226c0c0e PNA |
1246 | } |
1247 | ||
1248 | rcu_read_lock(); | |
1249 | helper = __nf_conntrack_helper_find_byname(helpname); | |
1250 | if (helper) { | |
226c0c0e | 1251 | err = -EAGAIN; |
0f5b3e85 | 1252 | goto err2; |
226c0c0e PNA |
1253 | } |
1254 | rcu_read_unlock(); | |
1255 | #endif | |
1256 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1257 | goto err1; |
226c0c0e PNA |
1258 | } else { |
1259 | struct nf_conn_help *help; | |
1260 | ||
1261 | help = nf_ct_helper_ext_add(ct, GFP_ATOMIC); | |
1262 | if (help == NULL) { | |
226c0c0e | 1263 | err = -ENOMEM; |
0f5b3e85 | 1264 | goto err2; |
226c0c0e PNA |
1265 | } |
1266 | ||
1267 | /* not in hash table yet so not strictly necessary */ | |
1268 | rcu_assign_pointer(help->helper, helper); | |
1269 | } | |
1270 | } else { | |
1271 | /* try an implicit helper assignation */ | |
1272 | err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC); | |
0f5b3e85 PM |
1273 | if (err < 0) |
1274 | goto err2; | |
1575e7ea PNA |
1275 | } |
1276 | ||
df6fb868 | 1277 | if (cda[CTA_STATUS]) { |
bbb3357d | 1278 | err = ctnetlink_change_status(ct, cda); |
0f5b3e85 PM |
1279 | if (err < 0) |
1280 | goto err2; | |
e6a7d3c0 PNA |
1281 | } |
1282 | ||
1283 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) { | |
1284 | err = ctnetlink_change_nat(ct, cda); | |
0f5b3e85 PM |
1285 | if (err < 0) |
1286 | goto err2; | |
bbb3357d | 1287 | } |
c1d10adb | 1288 | |
c969aa7d PNA |
1289 | #ifdef CONFIG_NF_NAT_NEEDED |
1290 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1291 | err = ctnetlink_change_nat_seq_adj(ct, cda); | |
0f5b3e85 PM |
1292 | if (err < 0) |
1293 | goto err2; | |
c969aa7d PNA |
1294 | } |
1295 | #endif | |
1296 | ||
df6fb868 | 1297 | if (cda[CTA_PROTOINFO]) { |
c1d10adb | 1298 | err = ctnetlink_change_protoinfo(ct, cda); |
0f5b3e85 PM |
1299 | if (err < 0) |
1300 | goto err2; | |
c1d10adb PNA |
1301 | } |
1302 | ||
3ec19255 | 1303 | nf_ct_acct_ext_add(ct, GFP_ATOMIC); |
58401572 | 1304 | |
bcd1e830 | 1305 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1306 | if (cda[CTA_MARK]) |
77236b6e | 1307 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1308 | #endif |
1309 | ||
5faa1f4c | 1310 | /* setup master conntrack: this is a confirmed expectation */ |
7ec47496 PNA |
1311 | if (cda[CTA_TUPLE_MASTER]) { |
1312 | struct nf_conntrack_tuple master; | |
1313 | struct nf_conntrack_tuple_hash *master_h; | |
1314 | struct nf_conn *master_ct; | |
1315 | ||
1316 | err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3); | |
1317 | if (err < 0) | |
0f5b3e85 | 1318 | goto err2; |
7ec47496 | 1319 | |
ea781f19 | 1320 | master_h = nf_conntrack_find_get(&init_net, &master); |
7ec47496 PNA |
1321 | if (master_h == NULL) { |
1322 | err = -ENOENT; | |
0f5b3e85 | 1323 | goto err2; |
7ec47496 PNA |
1324 | } |
1325 | master_ct = nf_ct_tuplehash_to_ctrack(master_h); | |
f2a89004 | 1326 | __set_bit(IPS_EXPECTED_BIT, &ct->status); |
5faa1f4c | 1327 | ct->master = master_ct; |
f2a89004 | 1328 | } |
5faa1f4c | 1329 | |
c1d10adb PNA |
1330 | add_timer(&ct->timeout); |
1331 | nf_conntrack_hash_insert(ct); | |
58a3c9bb | 1332 | rcu_read_unlock(); |
dafc741c | 1333 | |
f0a3c086 | 1334 | return ct; |
c1d10adb | 1335 | |
0f5b3e85 PM |
1336 | err2: |
1337 | rcu_read_unlock(); | |
1338 | err1: | |
c1d10adb | 1339 | nf_conntrack_free(ct); |
f0a3c086 | 1340 | return ERR_PTR(err); |
c1d10adb PNA |
1341 | } |
1342 | ||
601e68e1 YH |
1343 | static int |
1344 | ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, | |
df6fb868 | 1345 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
c1d10adb PNA |
1346 | { |
1347 | struct nf_conntrack_tuple otuple, rtuple; | |
1348 | struct nf_conntrack_tuple_hash *h = NULL; | |
1349 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
1350 | u_int8_t u3 = nfmsg->nfgen_family; | |
1351 | int err = 0; | |
1352 | ||
df6fb868 | 1353 | if (cda[CTA_TUPLE_ORIG]) { |
c1d10adb PNA |
1354 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3); |
1355 | if (err < 0) | |
1356 | return err; | |
1357 | } | |
1358 | ||
df6fb868 | 1359 | if (cda[CTA_TUPLE_REPLY]) { |
c1d10adb PNA |
1360 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3); |
1361 | if (err < 0) | |
1362 | return err; | |
1363 | } | |
1364 | ||
f8ba1aff | 1365 | spin_lock_bh(&nf_conntrack_lock); |
df6fb868 | 1366 | if (cda[CTA_TUPLE_ORIG]) |
400dad39 | 1367 | h = __nf_conntrack_find(&init_net, &otuple); |
df6fb868 | 1368 | else if (cda[CTA_TUPLE_REPLY]) |
400dad39 | 1369 | h = __nf_conntrack_find(&init_net, &rtuple); |
c1d10adb PNA |
1370 | |
1371 | if (h == NULL) { | |
c1d10adb | 1372 | err = -ENOENT; |
f0a3c086 PNA |
1373 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
1374 | struct nf_conn *ct; | |
5faa1f4c | 1375 | |
f0a3c086 PNA |
1376 | ct = ctnetlink_create_conntrack(cda, &otuple, |
1377 | &rtuple, u3); | |
1378 | if (IS_ERR(ct)) { | |
1379 | err = PTR_ERR(ct); | |
5faa1f4c PNA |
1380 | goto out_unlock; |
1381 | } | |
f0a3c086 PNA |
1382 | err = 0; |
1383 | nf_conntrack_get(&ct->ct_general); | |
1384 | spin_unlock_bh(&nf_conntrack_lock); | |
1385 | ctnetlink_event_report(ct, | |
1386 | NETLINK_CB(skb).pid, | |
1387 | nlmsg_report(nlh)); | |
1388 | nf_ct_put(ct); | |
1389 | } else | |
1390 | spin_unlock_bh(&nf_conntrack_lock); | |
5faa1f4c | 1391 | |
c1d10adb PNA |
1392 | return err; |
1393 | } | |
1394 | /* implicit 'else' */ | |
1395 | ||
c1d10adb PNA |
1396 | /* We manipulate the conntrack inside the global conntrack table lock, |
1397 | * so there's no need to increase the refcount */ | |
c1d10adb | 1398 | err = -EEXIST; |
ff4ca827 | 1399 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) { |
19abb7b0 PNA |
1400 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); |
1401 | ||
19abb7b0 PNA |
1402 | err = ctnetlink_change_conntrack(ct, cda); |
1403 | if (err == 0) { | |
1404 | nf_conntrack_get(&ct->ct_general); | |
1405 | spin_unlock_bh(&nf_conntrack_lock); | |
1406 | ctnetlink_event_report(ct, | |
1407 | NETLINK_CB(skb).pid, | |
1408 | nlmsg_report(nlh)); | |
1409 | nf_ct_put(ct); | |
1410 | } else | |
1411 | spin_unlock_bh(&nf_conntrack_lock); | |
1412 | ||
1413 | return err; | |
ff4ca827 | 1414 | } |
c1d10adb PNA |
1415 | |
1416 | out_unlock: | |
f8ba1aff | 1417 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
1418 | return err; |
1419 | } | |
1420 | ||
601e68e1 YH |
1421 | /*********************************************************************** |
1422 | * EXPECT | |
1423 | ***********************************************************************/ | |
c1d10adb PNA |
1424 | |
1425 | static inline int | |
1426 | ctnetlink_exp_dump_tuple(struct sk_buff *skb, | |
1427 | const struct nf_conntrack_tuple *tuple, | |
1428 | enum ctattr_expect type) | |
1429 | { | |
df6fb868 | 1430 | struct nlattr *nest_parms; |
601e68e1 | 1431 | |
df6fb868 PM |
1432 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
1433 | if (!nest_parms) | |
1434 | goto nla_put_failure; | |
c1d10adb | 1435 | if (ctnetlink_dump_tuples(skb, tuple) < 0) |
df6fb868 PM |
1436 | goto nla_put_failure; |
1437 | nla_nest_end(skb, nest_parms); | |
c1d10adb PNA |
1438 | |
1439 | return 0; | |
1440 | ||
df6fb868 | 1441 | nla_put_failure: |
c1d10adb | 1442 | return -1; |
601e68e1 | 1443 | } |
c1d10adb | 1444 | |
1cde6436 PNA |
1445 | static inline int |
1446 | ctnetlink_exp_dump_mask(struct sk_buff *skb, | |
1447 | const struct nf_conntrack_tuple *tuple, | |
d4156e8c | 1448 | const struct nf_conntrack_tuple_mask *mask) |
1cde6436 PNA |
1449 | { |
1450 | int ret; | |
1451 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 1452 | struct nf_conntrack_l4proto *l4proto; |
d4156e8c | 1453 | struct nf_conntrack_tuple m; |
df6fb868 | 1454 | struct nlattr *nest_parms; |
d4156e8c PM |
1455 | |
1456 | memset(&m, 0xFF, sizeof(m)); | |
1457 | m.src.u.all = mask->src.u.all; | |
1458 | memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3)); | |
1459 | ||
df6fb868 PM |
1460 | nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED); |
1461 | if (!nest_parms) | |
1462 | goto nla_put_failure; | |
1cde6436 | 1463 | |
528a3a6f | 1464 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
d4156e8c | 1465 | ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto); |
1cde6436 PNA |
1466 | |
1467 | if (unlikely(ret < 0)) | |
df6fb868 | 1468 | goto nla_put_failure; |
1cde6436 | 1469 | |
528a3a6f | 1470 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); |
d4156e8c | 1471 | ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto); |
1cde6436 | 1472 | if (unlikely(ret < 0)) |
df6fb868 | 1473 | goto nla_put_failure; |
1cde6436 | 1474 | |
df6fb868 | 1475 | nla_nest_end(skb, nest_parms); |
1cde6436 PNA |
1476 | |
1477 | return 0; | |
1478 | ||
df6fb868 | 1479 | nla_put_failure: |
1cde6436 PNA |
1480 | return -1; |
1481 | } | |
1482 | ||
bb5cf80e | 1483 | static int |
c1d10adb | 1484 | ctnetlink_exp_dump_expect(struct sk_buff *skb, |
601e68e1 | 1485 | const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
1486 | { |
1487 | struct nf_conn *master = exp->master; | |
d978e5da PM |
1488 | long timeout = (exp->timeout.expires - jiffies) / HZ; |
1489 | ||
1490 | if (timeout < 0) | |
1491 | timeout = 0; | |
c1d10adb PNA |
1492 | |
1493 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) | |
df6fb868 | 1494 | goto nla_put_failure; |
1cde6436 | 1495 | if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) |
df6fb868 | 1496 | goto nla_put_failure; |
c1d10adb PNA |
1497 | if (ctnetlink_exp_dump_tuple(skb, |
1498 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
1499 | CTA_EXPECT_MASTER) < 0) | |
df6fb868 | 1500 | goto nla_put_failure; |
601e68e1 | 1501 | |
d978e5da | 1502 | NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)); |
77236b6e | 1503 | NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)); |
c1d10adb PNA |
1504 | |
1505 | return 0; | |
601e68e1 | 1506 | |
df6fb868 | 1507 | nla_put_failure: |
c1d10adb PNA |
1508 | return -1; |
1509 | } | |
1510 | ||
1511 | static int | |
1512 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq, | |
601e68e1 YH |
1513 | int event, |
1514 | int nowait, | |
c1d10adb PNA |
1515 | const struct nf_conntrack_expect *exp) |
1516 | { | |
1517 | struct nlmsghdr *nlh; | |
1518 | struct nfgenmsg *nfmsg; | |
27a884dc | 1519 | unsigned char *b = skb_tail_pointer(skb); |
c1d10adb PNA |
1520 | |
1521 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; | |
1522 | nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg)); | |
1523 | nfmsg = NLMSG_DATA(nlh); | |
1524 | ||
1525 | nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0; | |
1526 | nfmsg->nfgen_family = exp->tuple.src.l3num; | |
1527 | nfmsg->version = NFNETLINK_V0; | |
1528 | nfmsg->res_id = 0; | |
1529 | ||
1530 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) | |
df6fb868 | 1531 | goto nla_put_failure; |
c1d10adb | 1532 | |
27a884dc | 1533 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
c1d10adb PNA |
1534 | return skb->len; |
1535 | ||
1536 | nlmsg_failure: | |
df6fb868 | 1537 | nla_put_failure: |
dc5fc579 | 1538 | nlmsg_trim(skb, b); |
c1d10adb PNA |
1539 | return -1; |
1540 | } | |
1541 | ||
1542 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
1543 | static int ctnetlink_expect_event(struct notifier_block *this, | |
1544 | unsigned long events, void *ptr) | |
1545 | { | |
1546 | struct nlmsghdr *nlh; | |
1547 | struct nfgenmsg *nfmsg; | |
19abb7b0 PNA |
1548 | struct nf_exp_event *item = (struct nf_exp_event *)ptr; |
1549 | struct nf_conntrack_expect *exp = item->exp; | |
c1d10adb PNA |
1550 | struct sk_buff *skb; |
1551 | unsigned int type; | |
27a884dc | 1552 | sk_buff_data_t b; |
c1d10adb PNA |
1553 | int flags = 0; |
1554 | ||
1555 | if (events & IPEXP_NEW) { | |
1556 | type = IPCTNL_MSG_EXP_NEW; | |
1557 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
1558 | } else | |
1559 | return NOTIFY_DONE; | |
1560 | ||
1f9da256 PNA |
1561 | if (!item->report && |
1562 | !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW)) | |
b3a27bfb PNA |
1563 | return NOTIFY_DONE; |
1564 | ||
c1d10adb PNA |
1565 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
1566 | if (!skb) | |
1567 | return NOTIFY_DONE; | |
1568 | ||
1569 | b = skb->tail; | |
1570 | ||
b633ad5f | 1571 | type |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
19abb7b0 | 1572 | nlh = NLMSG_PUT(skb, item->pid, 0, type, sizeof(struct nfgenmsg)); |
c1d10adb PNA |
1573 | nfmsg = NLMSG_DATA(nlh); |
1574 | ||
1575 | nlh->nlmsg_flags = flags; | |
1576 | nfmsg->nfgen_family = exp->tuple.src.l3num; | |
1577 | nfmsg->version = NFNETLINK_V0; | |
1578 | nfmsg->res_id = 0; | |
1579 | ||
528a3a6f | 1580 | rcu_read_lock(); |
c1d10adb | 1581 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
df6fb868 | 1582 | goto nla_put_failure; |
528a3a6f | 1583 | rcu_read_unlock(); |
c1d10adb PNA |
1584 | |
1585 | nlh->nlmsg_len = skb->tail - b; | |
19abb7b0 | 1586 | nfnetlink_send(skb, item->pid, NFNLGRP_CONNTRACK_EXP_NEW, item->report); |
c1d10adb PNA |
1587 | return NOTIFY_DONE; |
1588 | ||
df6fb868 | 1589 | nla_put_failure: |
528a3a6f PNA |
1590 | rcu_read_unlock(); |
1591 | nlmsg_failure: | |
dd5b6ce6 | 1592 | nfnetlink_set_err(0, 0, -ENOBUFS); |
c1d10adb PNA |
1593 | kfree_skb(skb); |
1594 | return NOTIFY_DONE; | |
1595 | } | |
1596 | #endif | |
cf6994c2 PM |
1597 | static int ctnetlink_exp_done(struct netlink_callback *cb) |
1598 | { | |
31f15875 PM |
1599 | if (cb->args[1]) |
1600 | nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]); | |
cf6994c2 PM |
1601 | return 0; |
1602 | } | |
c1d10adb PNA |
1603 | |
1604 | static int | |
1605 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
1606 | { | |
9b03f38d | 1607 | struct net *net = &init_net; |
cf6994c2 | 1608 | struct nf_conntrack_expect *exp, *last; |
87711cb8 | 1609 | struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh); |
31f15875 | 1610 | struct hlist_node *n; |
87711cb8 | 1611 | u_int8_t l3proto = nfmsg->nfgen_family; |
c1d10adb | 1612 | |
7d0742da | 1613 | rcu_read_lock(); |
31f15875 PM |
1614 | last = (struct nf_conntrack_expect *)cb->args[1]; |
1615 | for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) { | |
cf6994c2 | 1616 | restart: |
9b03f38d | 1617 | hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]], |
31f15875 PM |
1618 | hnode) { |
1619 | if (l3proto && exp->tuple.src.l3num != l3proto) | |
cf6994c2 | 1620 | continue; |
31f15875 PM |
1621 | if (cb->args[1]) { |
1622 | if (exp != last) | |
1623 | continue; | |
1624 | cb->args[1] = 0; | |
1625 | } | |
1626 | if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid, | |
1627 | cb->nlh->nlmsg_seq, | |
1628 | IPCTNL_MSG_EXP_NEW, | |
1629 | 1, exp) < 0) { | |
7d0742da PM |
1630 | if (!atomic_inc_not_zero(&exp->use)) |
1631 | continue; | |
31f15875 PM |
1632 | cb->args[1] = (unsigned long)exp; |
1633 | goto out; | |
1634 | } | |
cf6994c2 | 1635 | } |
31f15875 PM |
1636 | if (cb->args[1]) { |
1637 | cb->args[1] = 0; | |
1638 | goto restart; | |
cf6994c2 PM |
1639 | } |
1640 | } | |
601e68e1 | 1641 | out: |
7d0742da | 1642 | rcu_read_unlock(); |
cf6994c2 PM |
1643 | if (last) |
1644 | nf_ct_expect_put(last); | |
c1d10adb | 1645 | |
c1d10adb PNA |
1646 | return skb->len; |
1647 | } | |
1648 | ||
f73e924c PM |
1649 | static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = { |
1650 | [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 }, | |
1651 | [CTA_EXPECT_ID] = { .type = NLA_U32 }, | |
c1d10adb PNA |
1652 | }; |
1653 | ||
1654 | static int | |
601e68e1 | 1655 | ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, |
df6fb868 | 1656 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
c1d10adb PNA |
1657 | { |
1658 | struct nf_conntrack_tuple tuple; | |
1659 | struct nf_conntrack_expect *exp; | |
1660 | struct sk_buff *skb2; | |
1661 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
1662 | u_int8_t u3 = nfmsg->nfgen_family; | |
1663 | int err = 0; | |
1664 | ||
c1d10adb | 1665 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
c702e804 TG |
1666 | return netlink_dump_start(ctnl, skb, nlh, |
1667 | ctnetlink_exp_dump_table, | |
cf6994c2 | 1668 | ctnetlink_exp_done); |
c1d10adb PNA |
1669 | } |
1670 | ||
df6fb868 | 1671 | if (cda[CTA_EXPECT_MASTER]) |
c1d10adb PNA |
1672 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3); |
1673 | else | |
1674 | return -EINVAL; | |
1675 | ||
1676 | if (err < 0) | |
1677 | return err; | |
1678 | ||
9b03f38d | 1679 | exp = nf_ct_expect_find_get(&init_net, &tuple); |
c1d10adb PNA |
1680 | if (!exp) |
1681 | return -ENOENT; | |
1682 | ||
df6fb868 | 1683 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 1684 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 1685 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 1686 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1687 | return -ENOENT; |
1688 | } | |
601e68e1 | 1689 | } |
c1d10adb PNA |
1690 | |
1691 | err = -ENOMEM; | |
1692 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
1693 | if (!skb2) | |
1694 | goto out; | |
4e9b8269 | 1695 | |
528a3a6f | 1696 | rcu_read_lock(); |
601e68e1 | 1697 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, |
c1d10adb PNA |
1698 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, |
1699 | 1, exp); | |
528a3a6f | 1700 | rcu_read_unlock(); |
c1d10adb PNA |
1701 | if (err <= 0) |
1702 | goto free; | |
1703 | ||
6823645d | 1704 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1705 | |
1706 | return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
1707 | ||
1708 | free: | |
1709 | kfree_skb(skb2); | |
1710 | out: | |
6823645d | 1711 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1712 | return err; |
1713 | } | |
1714 | ||
1715 | static int | |
601e68e1 | 1716 | ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, |
df6fb868 | 1717 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
c1d10adb | 1718 | { |
31f15875 | 1719 | struct nf_conntrack_expect *exp; |
c1d10adb PNA |
1720 | struct nf_conntrack_tuple tuple; |
1721 | struct nf_conntrack_helper *h; | |
1722 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
31f15875 | 1723 | struct hlist_node *n, *next; |
c1d10adb | 1724 | u_int8_t u3 = nfmsg->nfgen_family; |
31f15875 | 1725 | unsigned int i; |
c1d10adb PNA |
1726 | int err; |
1727 | ||
df6fb868 | 1728 | if (cda[CTA_EXPECT_TUPLE]) { |
c1d10adb PNA |
1729 | /* delete a single expect by tuple */ |
1730 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
1731 | if (err < 0) | |
1732 | return err; | |
1733 | ||
1734 | /* bump usage count to 2 */ | |
9b03f38d | 1735 | exp = nf_ct_expect_find_get(&init_net, &tuple); |
c1d10adb PNA |
1736 | if (!exp) |
1737 | return -ENOENT; | |
1738 | ||
df6fb868 | 1739 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 1740 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 1741 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 1742 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1743 | return -ENOENT; |
1744 | } | |
1745 | } | |
1746 | ||
1747 | /* after list removal, usage count == 1 */ | |
6823645d | 1748 | nf_ct_unexpect_related(exp); |
601e68e1 | 1749 | /* have to put what we 'get' above. |
c1d10adb | 1750 | * after this line usage count == 0 */ |
6823645d | 1751 | nf_ct_expect_put(exp); |
df6fb868 PM |
1752 | } else if (cda[CTA_EXPECT_HELP_NAME]) { |
1753 | char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
31f15875 | 1754 | struct nf_conn_help *m_help; |
c1d10adb PNA |
1755 | |
1756 | /* delete all expectations for this helper */ | |
f8ba1aff | 1757 | spin_lock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
1758 | h = __nf_conntrack_helper_find_byname(name); |
1759 | if (!h) { | |
f8ba1aff | 1760 | spin_unlock_bh(&nf_conntrack_lock); |
0adf9d67 | 1761 | return -EOPNOTSUPP; |
c1d10adb | 1762 | } |
31f15875 PM |
1763 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
1764 | hlist_for_each_entry_safe(exp, n, next, | |
9b03f38d | 1765 | &init_net.ct.expect_hash[i], |
31f15875 PM |
1766 | hnode) { |
1767 | m_help = nfct_help(exp->master); | |
1768 | if (m_help->helper == h | |
1769 | && del_timer(&exp->timeout)) { | |
1770 | nf_ct_unlink_expect(exp); | |
1771 | nf_ct_expect_put(exp); | |
1772 | } | |
c1d10adb PNA |
1773 | } |
1774 | } | |
f8ba1aff | 1775 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
1776 | } else { |
1777 | /* This basically means we have to flush everything*/ | |
f8ba1aff | 1778 | spin_lock_bh(&nf_conntrack_lock); |
31f15875 PM |
1779 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
1780 | hlist_for_each_entry_safe(exp, n, next, | |
9b03f38d | 1781 | &init_net.ct.expect_hash[i], |
31f15875 PM |
1782 | hnode) { |
1783 | if (del_timer(&exp->timeout)) { | |
1784 | nf_ct_unlink_expect(exp); | |
1785 | nf_ct_expect_put(exp); | |
1786 | } | |
c1d10adb PNA |
1787 | } |
1788 | } | |
f8ba1aff | 1789 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
1790 | } |
1791 | ||
1792 | return 0; | |
1793 | } | |
1794 | static int | |
df6fb868 | 1795 | ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[]) |
c1d10adb PNA |
1796 | { |
1797 | return -EOPNOTSUPP; | |
1798 | } | |
1799 | ||
1800 | static int | |
19abb7b0 | 1801 | ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3, u32 pid, int report) |
c1d10adb PNA |
1802 | { |
1803 | struct nf_conntrack_tuple tuple, mask, master_tuple; | |
1804 | struct nf_conntrack_tuple_hash *h = NULL; | |
1805 | struct nf_conntrack_expect *exp; | |
1806 | struct nf_conn *ct; | |
dc808fe2 | 1807 | struct nf_conn_help *help; |
c1d10adb PNA |
1808 | int err = 0; |
1809 | ||
c1d10adb PNA |
1810 | /* caller guarantees that those three CTA_EXPECT_* exist */ |
1811 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
1812 | if (err < 0) | |
1813 | return err; | |
1814 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3); | |
1815 | if (err < 0) | |
1816 | return err; | |
1817 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3); | |
1818 | if (err < 0) | |
1819 | return err; | |
1820 | ||
1821 | /* Look for master conntrack of this expectation */ | |
400dad39 | 1822 | h = nf_conntrack_find_get(&init_net, &master_tuple); |
c1d10adb PNA |
1823 | if (!h) |
1824 | return -ENOENT; | |
1825 | ct = nf_ct_tuplehash_to_ctrack(h); | |
dc808fe2 | 1826 | help = nfct_help(ct); |
c1d10adb | 1827 | |
dc808fe2 | 1828 | if (!help || !help->helper) { |
c1d10adb | 1829 | /* such conntrack hasn't got any helper, abort */ |
bfe29677 | 1830 | err = -EOPNOTSUPP; |
c1d10adb PNA |
1831 | goto out; |
1832 | } | |
1833 | ||
6823645d | 1834 | exp = nf_ct_expect_alloc(ct); |
c1d10adb PNA |
1835 | if (!exp) { |
1836 | err = -ENOMEM; | |
1837 | goto out; | |
1838 | } | |
601e68e1 | 1839 | |
626ba8fb | 1840 | exp->class = 0; |
c1d10adb PNA |
1841 | exp->expectfn = NULL; |
1842 | exp->flags = 0; | |
1843 | exp->master = ct; | |
9457d851 | 1844 | exp->helper = NULL; |
c1d10adb | 1845 | memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple)); |
d4156e8c PM |
1846 | memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3)); |
1847 | exp->mask.src.u.all = mask.src.u.all; | |
c1d10adb | 1848 | |
19abb7b0 | 1849 | err = nf_ct_expect_related_report(exp, pid, report); |
6823645d | 1850 | nf_ct_expect_put(exp); |
c1d10adb | 1851 | |
601e68e1 | 1852 | out: |
c1d10adb PNA |
1853 | nf_ct_put(nf_ct_tuplehash_to_ctrack(h)); |
1854 | return err; | |
1855 | } | |
1856 | ||
1857 | static int | |
1858 | ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb, | |
df6fb868 | 1859 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
c1d10adb PNA |
1860 | { |
1861 | struct nf_conntrack_tuple tuple; | |
1862 | struct nf_conntrack_expect *exp; | |
1863 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
1864 | u_int8_t u3 = nfmsg->nfgen_family; | |
1865 | int err = 0; | |
1866 | ||
df6fb868 PM |
1867 | if (!cda[CTA_EXPECT_TUPLE] |
1868 | || !cda[CTA_EXPECT_MASK] | |
1869 | || !cda[CTA_EXPECT_MASTER]) | |
c1d10adb PNA |
1870 | return -EINVAL; |
1871 | ||
1872 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
1873 | if (err < 0) | |
1874 | return err; | |
1875 | ||
f8ba1aff | 1876 | spin_lock_bh(&nf_conntrack_lock); |
9b03f38d | 1877 | exp = __nf_ct_expect_find(&init_net, &tuple); |
c1d10adb PNA |
1878 | |
1879 | if (!exp) { | |
f8ba1aff | 1880 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 1881 | err = -ENOENT; |
19abb7b0 PNA |
1882 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
1883 | err = ctnetlink_create_expect(cda, | |
1884 | u3, | |
1885 | NETLINK_CB(skb).pid, | |
1886 | nlmsg_report(nlh)); | |
1887 | } | |
c1d10adb PNA |
1888 | return err; |
1889 | } | |
1890 | ||
1891 | err = -EEXIST; | |
1892 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) | |
1893 | err = ctnetlink_change_expect(exp, cda); | |
f8ba1aff | 1894 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 1895 | |
c1d10adb PNA |
1896 | return err; |
1897 | } | |
1898 | ||
1899 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
1900 | static struct notifier_block ctnl_notifier = { | |
1901 | .notifier_call = ctnetlink_conntrack_event, | |
1902 | }; | |
1903 | ||
1904 | static struct notifier_block ctnl_notifier_exp = { | |
1905 | .notifier_call = ctnetlink_expect_event, | |
1906 | }; | |
1907 | #endif | |
1908 | ||
7c8d4cb4 | 1909 | static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { |
c1d10adb | 1910 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, |
f73e924c PM |
1911 | .attr_count = CTA_MAX, |
1912 | .policy = ct_nla_policy }, | |
c1d10adb | 1913 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
1914 | .attr_count = CTA_MAX, |
1915 | .policy = ct_nla_policy }, | |
c1d10adb | 1916 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, |
f73e924c PM |
1917 | .attr_count = CTA_MAX, |
1918 | .policy = ct_nla_policy }, | |
c1d10adb | 1919 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
1920 | .attr_count = CTA_MAX, |
1921 | .policy = ct_nla_policy }, | |
c1d10adb PNA |
1922 | }; |
1923 | ||
7c8d4cb4 | 1924 | static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { |
c1d10adb | 1925 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
f73e924c PM |
1926 | .attr_count = CTA_EXPECT_MAX, |
1927 | .policy = exp_nla_policy }, | |
c1d10adb | 1928 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, |
f73e924c PM |
1929 | .attr_count = CTA_EXPECT_MAX, |
1930 | .policy = exp_nla_policy }, | |
c1d10adb | 1931 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, |
f73e924c PM |
1932 | .attr_count = CTA_EXPECT_MAX, |
1933 | .policy = exp_nla_policy }, | |
c1d10adb PNA |
1934 | }; |
1935 | ||
7c8d4cb4 | 1936 | static const struct nfnetlink_subsystem ctnl_subsys = { |
c1d10adb PNA |
1937 | .name = "conntrack", |
1938 | .subsys_id = NFNL_SUBSYS_CTNETLINK, | |
1939 | .cb_count = IPCTNL_MSG_MAX, | |
1940 | .cb = ctnl_cb, | |
1941 | }; | |
1942 | ||
7c8d4cb4 | 1943 | static const struct nfnetlink_subsystem ctnl_exp_subsys = { |
c1d10adb PNA |
1944 | .name = "conntrack_expect", |
1945 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, | |
1946 | .cb_count = IPCTNL_MSG_EXP_MAX, | |
1947 | .cb = ctnl_exp_cb, | |
1948 | }; | |
1949 | ||
d2483dde | 1950 | MODULE_ALIAS("ip_conntrack_netlink"); |
c1d10adb | 1951 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); |
34f9a2e4 | 1952 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); |
c1d10adb PNA |
1953 | |
1954 | static int __init ctnetlink_init(void) | |
1955 | { | |
1956 | int ret; | |
1957 | ||
1958 | printk("ctnetlink v%s: registering with nfnetlink.\n", version); | |
1959 | ret = nfnetlink_subsys_register(&ctnl_subsys); | |
1960 | if (ret < 0) { | |
1961 | printk("ctnetlink_init: cannot register with nfnetlink.\n"); | |
1962 | goto err_out; | |
1963 | } | |
1964 | ||
1965 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); | |
1966 | if (ret < 0) { | |
1967 | printk("ctnetlink_init: cannot register exp with nfnetlink.\n"); | |
1968 | goto err_unreg_subsys; | |
1969 | } | |
1970 | ||
1971 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
1972 | ret = nf_conntrack_register_notifier(&ctnl_notifier); | |
1973 | if (ret < 0) { | |
1974 | printk("ctnetlink_init: cannot register notifier.\n"); | |
1975 | goto err_unreg_exp_subsys; | |
1976 | } | |
1977 | ||
6823645d | 1978 | ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp); |
c1d10adb PNA |
1979 | if (ret < 0) { |
1980 | printk("ctnetlink_init: cannot expect register notifier.\n"); | |
1981 | goto err_unreg_notifier; | |
1982 | } | |
1983 | #endif | |
1984 | ||
1985 | return 0; | |
1986 | ||
1987 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
1988 | err_unreg_notifier: | |
1989 | nf_conntrack_unregister_notifier(&ctnl_notifier); | |
1990 | err_unreg_exp_subsys: | |
1991 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
1992 | #endif | |
1993 | err_unreg_subsys: | |
1994 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
1995 | err_out: | |
1996 | return ret; | |
1997 | } | |
1998 | ||
1999 | static void __exit ctnetlink_exit(void) | |
2000 | { | |
2001 | printk("ctnetlink: unregistering from nfnetlink.\n"); | |
2002 | ||
2003 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
6823645d | 2004 | nf_ct_expect_unregister_notifier(&ctnl_notifier_exp); |
c1d10adb PNA |
2005 | nf_conntrack_unregister_notifier(&ctnl_notifier); |
2006 | #endif | |
2007 | ||
2008 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
2009 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
2010 | return; | |
2011 | } | |
2012 | ||
2013 | module_init(ctnetlink_init); | |
2014 | module_exit(ctnetlink_exit); |