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