]>
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]; |
9ab99d5a | 602 | for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) { |
89f2e218 | 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 | ||
d0b0268f PM |
711 | static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = { |
712 | [CTA_TUPLE_IP] = { .type = NLA_NESTED }, | |
713 | [CTA_TUPLE_PROTO] = { .type = NLA_NESTED }, | |
714 | }; | |
715 | ||
bb5cf80e | 716 | static int |
39938324 PM |
717 | ctnetlink_parse_tuple(const struct nlattr * const cda[], |
718 | struct nf_conntrack_tuple *tuple, | |
c1d10adb PNA |
719 | enum ctattr_tuple type, u_int8_t l3num) |
720 | { | |
df6fb868 | 721 | struct nlattr *tb[CTA_TUPLE_MAX+1]; |
c1d10adb PNA |
722 | int err; |
723 | ||
c1d10adb PNA |
724 | memset(tuple, 0, sizeof(*tuple)); |
725 | ||
d0b0268f | 726 | nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy); |
c1d10adb | 727 | |
df6fb868 | 728 | if (!tb[CTA_TUPLE_IP]) |
c1d10adb PNA |
729 | return -EINVAL; |
730 | ||
731 | tuple->src.l3num = l3num; | |
732 | ||
df6fb868 | 733 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple); |
c1d10adb PNA |
734 | if (err < 0) |
735 | return err; | |
736 | ||
df6fb868 | 737 | if (!tb[CTA_TUPLE_PROTO]) |
c1d10adb PNA |
738 | return -EINVAL; |
739 | ||
df6fb868 | 740 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple); |
c1d10adb PNA |
741 | if (err < 0) |
742 | return err; | |
743 | ||
744 | /* orig and expect tuples get DIR_ORIGINAL */ | |
745 | if (type == CTA_TUPLE_REPLY) | |
746 | tuple->dst.dir = IP_CT_DIR_REPLY; | |
747 | else | |
748 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
749 | ||
c1d10adb PNA |
750 | return 0; |
751 | } | |
752 | ||
d0b0268f PM |
753 | static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = { |
754 | [CTA_HELP_NAME] = { .type = NLA_NUL_STRING }, | |
755 | }; | |
756 | ||
c1d10adb | 757 | static inline int |
39938324 | 758 | ctnetlink_parse_help(const struct nlattr *attr, char **helper_name) |
c1d10adb | 759 | { |
df6fb868 | 760 | struct nlattr *tb[CTA_HELP_MAX+1]; |
c1d10adb | 761 | |
d0b0268f | 762 | nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy); |
c1d10adb | 763 | |
df6fb868 | 764 | if (!tb[CTA_HELP_NAME]) |
c1d10adb PNA |
765 | return -EINVAL; |
766 | ||
df6fb868 | 767 | *helper_name = nla_data(tb[CTA_HELP_NAME]); |
c1d10adb PNA |
768 | |
769 | return 0; | |
770 | } | |
771 | ||
f73e924c | 772 | static const struct nla_policy ct_nla_policy[CTA_MAX+1] = { |
d0b0268f PM |
773 | [CTA_TUPLE_ORIG] = { .type = NLA_NESTED }, |
774 | [CTA_TUPLE_REPLY] = { .type = NLA_NESTED }, | |
f73e924c | 775 | [CTA_STATUS] = { .type = NLA_U32 }, |
d0b0268f PM |
776 | [CTA_PROTOINFO] = { .type = NLA_NESTED }, |
777 | [CTA_HELP] = { .type = NLA_NESTED }, | |
778 | [CTA_NAT_SRC] = { .type = NLA_NESTED }, | |
f73e924c PM |
779 | [CTA_TIMEOUT] = { .type = NLA_U32 }, |
780 | [CTA_MARK] = { .type = NLA_U32 }, | |
f73e924c | 781 | [CTA_ID] = { .type = NLA_U32 }, |
d0b0268f PM |
782 | [CTA_NAT_DST] = { .type = NLA_NESTED }, |
783 | [CTA_TUPLE_MASTER] = { .type = NLA_NESTED }, | |
c1d10adb PNA |
784 | }; |
785 | ||
786 | static int | |
601e68e1 | 787 | ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
788 | const struct nlmsghdr *nlh, |
789 | const struct nlattr * const cda[]) | |
c1d10adb | 790 | { |
9592a5c0 | 791 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
792 | struct nf_conntrack_tuple_hash *h; |
793 | struct nf_conntrack_tuple tuple; | |
794 | struct nf_conn *ct; | |
96bcf938 | 795 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
796 | u_int8_t u3 = nfmsg->nfgen_family; |
797 | int err = 0; | |
798 | ||
df6fb868 | 799 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 800 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 801 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
802 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
803 | else { | |
804 | /* Flush the whole table */ | |
9592a5c0 | 805 | nf_conntrack_flush_report(net, |
274d383b PNA |
806 | NETLINK_CB(skb).pid, |
807 | nlmsg_report(nlh)); | |
c1d10adb PNA |
808 | return 0; |
809 | } | |
810 | ||
811 | if (err < 0) | |
812 | return err; | |
813 | ||
5d0aa2cc | 814 | h = nf_conntrack_find_get(net, 0, &tuple); |
9ea8cfd6 | 815 | if (!h) |
c1d10adb | 816 | return -ENOENT; |
c1d10adb PNA |
817 | |
818 | ct = nf_ct_tuplehash_to_ctrack(h); | |
601e68e1 | 819 | |
df6fb868 | 820 | if (cda[CTA_ID]) { |
77236b6e | 821 | u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID])); |
7f85f914 | 822 | if (id != (u32)(unsigned long)ct) { |
c1d10adb PNA |
823 | nf_ct_put(ct); |
824 | return -ENOENT; | |
825 | } | |
601e68e1 | 826 | } |
c1d10adb | 827 | |
dd7669a9 PNA |
828 | if (nf_conntrack_event_report(IPCT_DESTROY, ct, |
829 | NETLINK_CB(skb).pid, | |
830 | nlmsg_report(nlh)) < 0) { | |
831 | nf_ct_delete_from_lists(ct); | |
832 | /* we failed to report the event, try later */ | |
833 | nf_ct_insert_dying_list(ct); | |
834 | nf_ct_put(ct); | |
835 | return 0; | |
836 | } | |
19abb7b0 PNA |
837 | |
838 | /* death_by_timeout would report the event again */ | |
839 | set_bit(IPS_DYING_BIT, &ct->status); | |
840 | ||
51091764 | 841 | nf_ct_kill(ct); |
c1d10adb | 842 | nf_ct_put(ct); |
c1d10adb PNA |
843 | |
844 | return 0; | |
845 | } | |
846 | ||
847 | static int | |
601e68e1 | 848 | ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
849 | const struct nlmsghdr *nlh, |
850 | const struct nlattr * const cda[]) | |
c1d10adb | 851 | { |
9592a5c0 | 852 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
853 | struct nf_conntrack_tuple_hash *h; |
854 | struct nf_conntrack_tuple tuple; | |
855 | struct nf_conn *ct; | |
856 | struct sk_buff *skb2 = NULL; | |
96bcf938 | 857 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
858 | u_int8_t u3 = nfmsg->nfgen_family; |
859 | int err = 0; | |
860 | ||
58401572 | 861 | if (nlh->nlmsg_flags & NLM_F_DUMP) |
c702e804 TG |
862 | return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table, |
863 | ctnetlink_done); | |
c1d10adb | 864 | |
df6fb868 | 865 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 866 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 867 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
868 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
869 | else | |
870 | return -EINVAL; | |
871 | ||
872 | if (err < 0) | |
873 | return err; | |
874 | ||
5d0aa2cc | 875 | h = nf_conntrack_find_get(net, 0, &tuple); |
9ea8cfd6 | 876 | if (!h) |
c1d10adb | 877 | return -ENOENT; |
9ea8cfd6 | 878 | |
c1d10adb PNA |
879 | ct = nf_ct_tuplehash_to_ctrack(h); |
880 | ||
881 | err = -ENOMEM; | |
96bcf938 PNA |
882 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
883 | if (skb2 == NULL) { | |
c1d10adb PNA |
884 | nf_ct_put(ct); |
885 | return -ENOMEM; | |
886 | } | |
c1d10adb | 887 | |
528a3a6f | 888 | rcu_read_lock(); |
601e68e1 | 889 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, |
8b0a231d | 890 | IPCTNL_MSG_CT_NEW, ct); |
528a3a6f | 891 | rcu_read_unlock(); |
c1d10adb PNA |
892 | nf_ct_put(ct); |
893 | if (err <= 0) | |
894 | goto free; | |
895 | ||
896 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
897 | if (err < 0) | |
898 | goto out; | |
899 | ||
c1d10adb PNA |
900 | return 0; |
901 | ||
902 | free: | |
903 | kfree_skb(skb2); | |
904 | out: | |
905 | return err; | |
906 | } | |
907 | ||
67671841 | 908 | #ifdef CONFIG_NF_NAT_NEEDED |
e6a7d3c0 PNA |
909 | static int |
910 | ctnetlink_parse_nat_setup(struct nf_conn *ct, | |
911 | enum nf_nat_manip_type manip, | |
39938324 | 912 | const struct nlattr *attr) |
e6a7d3c0 PNA |
913 | { |
914 | typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup; | |
915 | ||
916 | parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook); | |
917 | if (!parse_nat_setup) { | |
95a5afca | 918 | #ifdef CONFIG_MODULES |
e6a7d3c0 | 919 | rcu_read_unlock(); |
748085fc | 920 | spin_unlock_bh(&nf_conntrack_lock); |
e6a7d3c0 PNA |
921 | nfnl_unlock(); |
922 | if (request_module("nf-nat-ipv4") < 0) { | |
923 | nfnl_lock(); | |
748085fc | 924 | spin_lock_bh(&nf_conntrack_lock); |
e6a7d3c0 PNA |
925 | rcu_read_lock(); |
926 | return -EOPNOTSUPP; | |
927 | } | |
928 | nfnl_lock(); | |
748085fc | 929 | spin_lock_bh(&nf_conntrack_lock); |
e6a7d3c0 PNA |
930 | rcu_read_lock(); |
931 | if (nfnetlink_parse_nat_setup_hook) | |
932 | return -EAGAIN; | |
933 | #endif | |
934 | return -EOPNOTSUPP; | |
935 | } | |
936 | ||
937 | return parse_nat_setup(ct, manip, attr); | |
938 | } | |
67671841 | 939 | #endif |
e6a7d3c0 | 940 | |
bb5cf80e | 941 | static int |
39938324 | 942 | ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
943 | { |
944 | unsigned long d; | |
77236b6e | 945 | unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS])); |
c1d10adb PNA |
946 | d = ct->status ^ status; |
947 | ||
948 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) | |
949 | /* unchangeable */ | |
0adf9d67 | 950 | return -EBUSY; |
601e68e1 | 951 | |
c1d10adb PNA |
952 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) |
953 | /* SEEN_REPLY bit can only be set */ | |
0adf9d67 | 954 | return -EBUSY; |
601e68e1 | 955 | |
c1d10adb PNA |
956 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) |
957 | /* ASSURED bit can only be set */ | |
0adf9d67 | 958 | return -EBUSY; |
c1d10adb | 959 | |
c1d10adb PNA |
960 | /* Be careful here, modifying NAT bits can screw up things, |
961 | * so don't let users modify them directly if they don't pass | |
5b1158e9 | 962 | * nf_nat_range. */ |
c1d10adb PNA |
963 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); |
964 | return 0; | |
965 | } | |
966 | ||
e6a7d3c0 | 967 | static int |
39938324 | 968 | ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[]) |
e6a7d3c0 PNA |
969 | { |
970 | #ifdef CONFIG_NF_NAT_NEEDED | |
971 | int ret; | |
972 | ||
973 | if (cda[CTA_NAT_DST]) { | |
974 | ret = ctnetlink_parse_nat_setup(ct, | |
975 | IP_NAT_MANIP_DST, | |
976 | cda[CTA_NAT_DST]); | |
977 | if (ret < 0) | |
978 | return ret; | |
979 | } | |
980 | if (cda[CTA_NAT_SRC]) { | |
981 | ret = ctnetlink_parse_nat_setup(ct, | |
982 | IP_NAT_MANIP_SRC, | |
983 | cda[CTA_NAT_SRC]); | |
984 | if (ret < 0) | |
985 | return ret; | |
986 | } | |
987 | return 0; | |
988 | #else | |
989 | return -EOPNOTSUPP; | |
990 | #endif | |
991 | } | |
c1d10adb PNA |
992 | |
993 | static inline int | |
39938324 | 994 | ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
995 | { |
996 | struct nf_conntrack_helper *helper; | |
dc808fe2 | 997 | struct nf_conn_help *help = nfct_help(ct); |
29fe1b48 | 998 | char *helpname = NULL; |
c1d10adb PNA |
999 | int err; |
1000 | ||
c1d10adb PNA |
1001 | /* don't change helper of sibling connections */ |
1002 | if (ct->master) | |
0adf9d67 | 1003 | return -EBUSY; |
c1d10adb | 1004 | |
df6fb868 | 1005 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); |
c1d10adb PNA |
1006 | if (err < 0) |
1007 | return err; | |
1008 | ||
df293bbb YK |
1009 | if (!strcmp(helpname, "")) { |
1010 | if (help && help->helper) { | |
c1d10adb PNA |
1011 | /* we had a helper before ... */ |
1012 | nf_ct_remove_expectations(ct); | |
3c158f7f | 1013 | rcu_assign_pointer(help->helper, NULL); |
c1d10adb | 1014 | } |
df293bbb YK |
1015 | |
1016 | return 0; | |
c1d10adb | 1017 | } |
601e68e1 | 1018 | |
794e6871 PM |
1019 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1020 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1021 | if (helper == NULL) { |
1022 | #ifdef CONFIG_MODULES | |
1023 | spin_unlock_bh(&nf_conntrack_lock); | |
1024 | ||
1025 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1026 | spin_lock_bh(&nf_conntrack_lock); | |
1027 | return -EOPNOTSUPP; | |
1028 | } | |
1029 | ||
1030 | spin_lock_bh(&nf_conntrack_lock); | |
794e6871 PM |
1031 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1032 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1033 | if (helper) |
1034 | return -EAGAIN; | |
1035 | #endif | |
0adf9d67 | 1036 | return -EOPNOTSUPP; |
226c0c0e | 1037 | } |
df293bbb | 1038 | |
ceceae1b YK |
1039 | if (help) { |
1040 | if (help->helper == helper) | |
1041 | return 0; | |
1042 | if (help->helper) | |
1043 | return -EBUSY; | |
1044 | /* need to zero data of old helper */ | |
1045 | memset(&help->help, 0, sizeof(help->help)); | |
1046 | } else { | |
fab00c5d | 1047 | help = nf_ct_helper_ext_add(ct, GFP_ATOMIC); |
ceceae1b YK |
1048 | if (help == NULL) |
1049 | return -ENOMEM; | |
1050 | } | |
df293bbb | 1051 | |
3c158f7f | 1052 | rcu_assign_pointer(help->helper, helper); |
c1d10adb PNA |
1053 | |
1054 | return 0; | |
1055 | } | |
1056 | ||
1057 | static inline int | |
39938324 | 1058 | ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb | 1059 | { |
77236b6e | 1060 | u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
601e68e1 | 1061 | |
c1d10adb PNA |
1062 | if (!del_timer(&ct->timeout)) |
1063 | return -ETIME; | |
1064 | ||
1065 | ct->timeout.expires = jiffies + timeout * HZ; | |
1066 | add_timer(&ct->timeout); | |
1067 | ||
1068 | return 0; | |
1069 | } | |
1070 | ||
d0b0268f PM |
1071 | static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = { |
1072 | [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED }, | |
1073 | [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED }, | |
1074 | [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED }, | |
1075 | }; | |
1076 | ||
c1d10adb | 1077 | static inline int |
39938324 | 1078 | ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb | 1079 | { |
39938324 PM |
1080 | const struct nlattr *attr = cda[CTA_PROTOINFO]; |
1081 | struct nlattr *tb[CTA_PROTOINFO_MAX+1]; | |
605dcad6 | 1082 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
1083 | int err = 0; |
1084 | ||
d0b0268f | 1085 | nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy); |
c1d10adb | 1086 | |
cd91566e FW |
1087 | rcu_read_lock(); |
1088 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
fdf70832 PM |
1089 | if (l4proto->from_nlattr) |
1090 | err = l4proto->from_nlattr(tb, ct); | |
cd91566e | 1091 | rcu_read_unlock(); |
c1d10adb PNA |
1092 | |
1093 | return err; | |
1094 | } | |
1095 | ||
13eae15a | 1096 | #ifdef CONFIG_NF_NAT_NEEDED |
d0b0268f PM |
1097 | static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = { |
1098 | [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 }, | |
1099 | [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 }, | |
1100 | [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 }, | |
1101 | }; | |
1102 | ||
13eae15a | 1103 | static inline int |
39938324 | 1104 | change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr) |
13eae15a PNA |
1105 | { |
1106 | struct nlattr *cda[CTA_NAT_SEQ_MAX+1]; | |
1107 | ||
d0b0268f | 1108 | nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy); |
13eae15a PNA |
1109 | |
1110 | if (!cda[CTA_NAT_SEQ_CORRECTION_POS]) | |
1111 | return -EINVAL; | |
1112 | ||
1113 | natseq->correction_pos = | |
77236b6e | 1114 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS])); |
13eae15a PNA |
1115 | |
1116 | if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE]) | |
1117 | return -EINVAL; | |
1118 | ||
1119 | natseq->offset_before = | |
77236b6e | 1120 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE])); |
13eae15a PNA |
1121 | |
1122 | if (!cda[CTA_NAT_SEQ_OFFSET_AFTER]) | |
1123 | return -EINVAL; | |
1124 | ||
1125 | natseq->offset_after = | |
77236b6e | 1126 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER])); |
13eae15a PNA |
1127 | |
1128 | return 0; | |
1129 | } | |
1130 | ||
1131 | static int | |
39938324 PM |
1132 | ctnetlink_change_nat_seq_adj(struct nf_conn *ct, |
1133 | const struct nlattr * const cda[]) | |
13eae15a PNA |
1134 | { |
1135 | int ret = 0; | |
1136 | struct nf_conn_nat *nat = nfct_nat(ct); | |
1137 | ||
1138 | if (!nat) | |
1139 | return 0; | |
1140 | ||
1141 | if (cda[CTA_NAT_SEQ_ADJ_ORIG]) { | |
1142 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL], | |
1143 | cda[CTA_NAT_SEQ_ADJ_ORIG]); | |
1144 | if (ret < 0) | |
1145 | return ret; | |
1146 | ||
1147 | ct->status |= IPS_SEQ_ADJUST; | |
1148 | } | |
1149 | ||
1150 | if (cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1151 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY], | |
1152 | cda[CTA_NAT_SEQ_ADJ_REPLY]); | |
1153 | if (ret < 0) | |
1154 | return ret; | |
1155 | ||
1156 | ct->status |= IPS_SEQ_ADJUST; | |
1157 | } | |
1158 | ||
1159 | return 0; | |
1160 | } | |
1161 | #endif | |
1162 | ||
c1d10adb | 1163 | static int |
39938324 PM |
1164 | ctnetlink_change_conntrack(struct nf_conn *ct, |
1165 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1166 | { |
1167 | int err; | |
1168 | ||
e098360f PNA |
1169 | /* only allow NAT changes and master assignation for new conntracks */ |
1170 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER]) | |
1171 | return -EOPNOTSUPP; | |
1172 | ||
df6fb868 | 1173 | if (cda[CTA_HELP]) { |
c1d10adb PNA |
1174 | err = ctnetlink_change_helper(ct, cda); |
1175 | if (err < 0) | |
1176 | return err; | |
1177 | } | |
1178 | ||
df6fb868 | 1179 | if (cda[CTA_TIMEOUT]) { |
c1d10adb PNA |
1180 | err = ctnetlink_change_timeout(ct, cda); |
1181 | if (err < 0) | |
1182 | return err; | |
1183 | } | |
1184 | ||
df6fb868 | 1185 | if (cda[CTA_STATUS]) { |
c1d10adb PNA |
1186 | err = ctnetlink_change_status(ct, cda); |
1187 | if (err < 0) | |
1188 | return err; | |
1189 | } | |
1190 | ||
df6fb868 | 1191 | if (cda[CTA_PROTOINFO]) { |
c1d10adb PNA |
1192 | err = ctnetlink_change_protoinfo(ct, cda); |
1193 | if (err < 0) | |
1194 | return err; | |
1195 | } | |
1196 | ||
bcd1e830 | 1197 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1198 | if (cda[CTA_MARK]) |
77236b6e | 1199 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1200 | #endif |
1201 | ||
13eae15a PNA |
1202 | #ifdef CONFIG_NF_NAT_NEEDED |
1203 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1204 | err = ctnetlink_change_nat_seq_adj(ct, cda); | |
1205 | if (err < 0) | |
1206 | return err; | |
1207 | } | |
1208 | #endif | |
1209 | ||
c1d10adb PNA |
1210 | return 0; |
1211 | } | |
1212 | ||
f0a3c086 | 1213 | static struct nf_conn * |
9592a5c0 AD |
1214 | ctnetlink_create_conntrack(struct net *net, |
1215 | const struct nlattr * const cda[], | |
c1d10adb | 1216 | struct nf_conntrack_tuple *otuple, |
5faa1f4c | 1217 | struct nf_conntrack_tuple *rtuple, |
7ec47496 | 1218 | u8 u3) |
c1d10adb PNA |
1219 | { |
1220 | struct nf_conn *ct; | |
1221 | int err = -EINVAL; | |
ceceae1b | 1222 | struct nf_conntrack_helper *helper; |
c1d10adb | 1223 | |
5d0aa2cc | 1224 | ct = nf_conntrack_alloc(net, 0, otuple, rtuple, GFP_ATOMIC); |
cd7fcbf1 | 1225 | if (IS_ERR(ct)) |
f0a3c086 | 1226 | return ERR_PTR(-ENOMEM); |
c1d10adb | 1227 | |
df6fb868 | 1228 | if (!cda[CTA_TIMEOUT]) |
0f5b3e85 | 1229 | goto err1; |
77236b6e | 1230 | ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
c1d10adb PNA |
1231 | |
1232 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; | |
1233 | ct->status |= IPS_CONFIRMED; | |
1234 | ||
1575e7ea | 1235 | rcu_read_lock(); |
226c0c0e | 1236 | if (cda[CTA_HELP]) { |
29fe1b48 | 1237 | char *helpname = NULL; |
226c0c0e PNA |
1238 | |
1239 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); | |
0f5b3e85 PM |
1240 | if (err < 0) |
1241 | goto err2; | |
226c0c0e | 1242 | |
794e6871 PM |
1243 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1244 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1245 | if (helper == NULL) { |
1246 | rcu_read_unlock(); | |
1247 | #ifdef CONFIG_MODULES | |
1248 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1249 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1250 | goto err1; |
226c0c0e PNA |
1251 | } |
1252 | ||
1253 | rcu_read_lock(); | |
794e6871 PM |
1254 | helper = __nf_conntrack_helper_find(helpname, |
1255 | nf_ct_l3num(ct), | |
1256 | nf_ct_protonum(ct)); | |
226c0c0e | 1257 | if (helper) { |
226c0c0e | 1258 | err = -EAGAIN; |
0f5b3e85 | 1259 | goto err2; |
226c0c0e PNA |
1260 | } |
1261 | rcu_read_unlock(); | |
1262 | #endif | |
1263 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1264 | goto err1; |
226c0c0e PNA |
1265 | } else { |
1266 | struct nf_conn_help *help; | |
1267 | ||
1268 | help = nf_ct_helper_ext_add(ct, GFP_ATOMIC); | |
1269 | if (help == NULL) { | |
226c0c0e | 1270 | err = -ENOMEM; |
0f5b3e85 | 1271 | goto err2; |
226c0c0e PNA |
1272 | } |
1273 | ||
1274 | /* not in hash table yet so not strictly necessary */ | |
1275 | rcu_assign_pointer(help->helper, helper); | |
1276 | } | |
1277 | } else { | |
1278 | /* try an implicit helper assignation */ | |
b2a15a60 | 1279 | err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC); |
0f5b3e85 PM |
1280 | if (err < 0) |
1281 | goto err2; | |
1575e7ea PNA |
1282 | } |
1283 | ||
df6fb868 | 1284 | if (cda[CTA_STATUS]) { |
bbb3357d | 1285 | err = ctnetlink_change_status(ct, cda); |
0f5b3e85 PM |
1286 | if (err < 0) |
1287 | goto err2; | |
e6a7d3c0 PNA |
1288 | } |
1289 | ||
1290 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) { | |
1291 | err = ctnetlink_change_nat(ct, cda); | |
0f5b3e85 PM |
1292 | if (err < 0) |
1293 | goto err2; | |
bbb3357d | 1294 | } |
c1d10adb | 1295 | |
c969aa7d PNA |
1296 | #ifdef CONFIG_NF_NAT_NEEDED |
1297 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1298 | err = ctnetlink_change_nat_seq_adj(ct, cda); | |
0f5b3e85 PM |
1299 | if (err < 0) |
1300 | goto err2; | |
c969aa7d PNA |
1301 | } |
1302 | #endif | |
1303 | ||
df6fb868 | 1304 | if (cda[CTA_PROTOINFO]) { |
c1d10adb | 1305 | err = ctnetlink_change_protoinfo(ct, cda); |
0f5b3e85 PM |
1306 | if (err < 0) |
1307 | goto err2; | |
c1d10adb PNA |
1308 | } |
1309 | ||
3ec19255 | 1310 | nf_ct_acct_ext_add(ct, GFP_ATOMIC); |
0cebe4b4 | 1311 | nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC); |
58401572 | 1312 | |
bcd1e830 | 1313 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1314 | if (cda[CTA_MARK]) |
77236b6e | 1315 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1316 | #endif |
1317 | ||
5faa1f4c | 1318 | /* setup master conntrack: this is a confirmed expectation */ |
7ec47496 PNA |
1319 | if (cda[CTA_TUPLE_MASTER]) { |
1320 | struct nf_conntrack_tuple master; | |
1321 | struct nf_conntrack_tuple_hash *master_h; | |
1322 | struct nf_conn *master_ct; | |
1323 | ||
1324 | err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3); | |
1325 | if (err < 0) | |
0f5b3e85 | 1326 | goto err2; |
7ec47496 | 1327 | |
5d0aa2cc | 1328 | master_h = nf_conntrack_find_get(net, 0, &master); |
7ec47496 PNA |
1329 | if (master_h == NULL) { |
1330 | err = -ENOENT; | |
0f5b3e85 | 1331 | goto err2; |
7ec47496 PNA |
1332 | } |
1333 | master_ct = nf_ct_tuplehash_to_ctrack(master_h); | |
f2a89004 | 1334 | __set_bit(IPS_EXPECTED_BIT, &ct->status); |
5faa1f4c | 1335 | ct->master = master_ct; |
f2a89004 | 1336 | } |
5faa1f4c | 1337 | |
c1d10adb PNA |
1338 | add_timer(&ct->timeout); |
1339 | nf_conntrack_hash_insert(ct); | |
58a3c9bb | 1340 | rcu_read_unlock(); |
dafc741c | 1341 | |
f0a3c086 | 1342 | return ct; |
c1d10adb | 1343 | |
0f5b3e85 PM |
1344 | err2: |
1345 | rcu_read_unlock(); | |
1346 | err1: | |
c1d10adb | 1347 | nf_conntrack_free(ct); |
f0a3c086 | 1348 | return ERR_PTR(err); |
c1d10adb PNA |
1349 | } |
1350 | ||
601e68e1 YH |
1351 | static int |
1352 | ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
1353 | const struct nlmsghdr *nlh, |
1354 | const struct nlattr * const cda[]) | |
c1d10adb | 1355 | { |
9592a5c0 | 1356 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1357 | struct nf_conntrack_tuple otuple, rtuple; |
1358 | struct nf_conntrack_tuple_hash *h = NULL; | |
96bcf938 | 1359 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
1360 | u_int8_t u3 = nfmsg->nfgen_family; |
1361 | int err = 0; | |
1362 | ||
df6fb868 | 1363 | if (cda[CTA_TUPLE_ORIG]) { |
c1d10adb PNA |
1364 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3); |
1365 | if (err < 0) | |
1366 | return err; | |
1367 | } | |
1368 | ||
df6fb868 | 1369 | if (cda[CTA_TUPLE_REPLY]) { |
c1d10adb PNA |
1370 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3); |
1371 | if (err < 0) | |
1372 | return err; | |
1373 | } | |
1374 | ||
f8ba1aff | 1375 | spin_lock_bh(&nf_conntrack_lock); |
df6fb868 | 1376 | if (cda[CTA_TUPLE_ORIG]) |
5d0aa2cc | 1377 | h = __nf_conntrack_find(net, 0, &otuple); |
df6fb868 | 1378 | else if (cda[CTA_TUPLE_REPLY]) |
5d0aa2cc | 1379 | h = __nf_conntrack_find(net, 0, &rtuple); |
c1d10adb PNA |
1380 | |
1381 | if (h == NULL) { | |
c1d10adb | 1382 | err = -ENOENT; |
f0a3c086 PNA |
1383 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
1384 | struct nf_conn *ct; | |
fecc1133 | 1385 | enum ip_conntrack_events events; |
5faa1f4c | 1386 | |
9592a5c0 | 1387 | ct = ctnetlink_create_conntrack(net, cda, &otuple, |
f0a3c086 PNA |
1388 | &rtuple, u3); |
1389 | if (IS_ERR(ct)) { | |
1390 | err = PTR_ERR(ct); | |
5faa1f4c PNA |
1391 | goto out_unlock; |
1392 | } | |
f0a3c086 PNA |
1393 | err = 0; |
1394 | nf_conntrack_get(&ct->ct_general); | |
1395 | spin_unlock_bh(&nf_conntrack_lock); | |
fecc1133 PNA |
1396 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) |
1397 | events = IPCT_RELATED; | |
1398 | else | |
1399 | events = IPCT_NEW; | |
1400 | ||
858b3133 PM |
1401 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1402 | (1 << IPCT_ASSURED) | | |
a0891aa6 PNA |
1403 | (1 << IPCT_HELPER) | |
1404 | (1 << IPCT_PROTOINFO) | | |
1405 | (1 << IPCT_NATSEQADJ) | | |
1406 | (1 << IPCT_MARK) | events, | |
1407 | ct, NETLINK_CB(skb).pid, | |
1408 | nlmsg_report(nlh)); | |
f0a3c086 PNA |
1409 | nf_ct_put(ct); |
1410 | } else | |
1411 | spin_unlock_bh(&nf_conntrack_lock); | |
5faa1f4c | 1412 | |
c1d10adb PNA |
1413 | return err; |
1414 | } | |
1415 | /* implicit 'else' */ | |
1416 | ||
c1d10adb PNA |
1417 | /* We manipulate the conntrack inside the global conntrack table lock, |
1418 | * so there's no need to increase the refcount */ | |
c1d10adb | 1419 | err = -EEXIST; |
ff4ca827 | 1420 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) { |
19abb7b0 PNA |
1421 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); |
1422 | ||
19abb7b0 PNA |
1423 | err = ctnetlink_change_conntrack(ct, cda); |
1424 | if (err == 0) { | |
1425 | nf_conntrack_get(&ct->ct_general); | |
1426 | spin_unlock_bh(&nf_conntrack_lock); | |
858b3133 PM |
1427 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1428 | (1 << IPCT_ASSURED) | | |
a0891aa6 PNA |
1429 | (1 << IPCT_HELPER) | |
1430 | (1 << IPCT_PROTOINFO) | | |
1431 | (1 << IPCT_NATSEQADJ) | | |
1432 | (1 << IPCT_MARK), | |
1433 | ct, NETLINK_CB(skb).pid, | |
1434 | nlmsg_report(nlh)); | |
19abb7b0 PNA |
1435 | nf_ct_put(ct); |
1436 | } else | |
1437 | spin_unlock_bh(&nf_conntrack_lock); | |
1438 | ||
1439 | return err; | |
ff4ca827 | 1440 | } |
c1d10adb PNA |
1441 | |
1442 | out_unlock: | |
f8ba1aff | 1443 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
1444 | return err; |
1445 | } | |
1446 | ||
601e68e1 YH |
1447 | /*********************************************************************** |
1448 | * EXPECT | |
1449 | ***********************************************************************/ | |
c1d10adb PNA |
1450 | |
1451 | static inline int | |
1452 | ctnetlink_exp_dump_tuple(struct sk_buff *skb, | |
1453 | const struct nf_conntrack_tuple *tuple, | |
1454 | enum ctattr_expect type) | |
1455 | { | |
df6fb868 | 1456 | struct nlattr *nest_parms; |
601e68e1 | 1457 | |
df6fb868 PM |
1458 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
1459 | if (!nest_parms) | |
1460 | goto nla_put_failure; | |
c1d10adb | 1461 | if (ctnetlink_dump_tuples(skb, tuple) < 0) |
df6fb868 PM |
1462 | goto nla_put_failure; |
1463 | nla_nest_end(skb, nest_parms); | |
c1d10adb PNA |
1464 | |
1465 | return 0; | |
1466 | ||
df6fb868 | 1467 | nla_put_failure: |
c1d10adb | 1468 | return -1; |
601e68e1 | 1469 | } |
c1d10adb | 1470 | |
1cde6436 PNA |
1471 | static inline int |
1472 | ctnetlink_exp_dump_mask(struct sk_buff *skb, | |
1473 | const struct nf_conntrack_tuple *tuple, | |
d4156e8c | 1474 | const struct nf_conntrack_tuple_mask *mask) |
1cde6436 PNA |
1475 | { |
1476 | int ret; | |
1477 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 1478 | struct nf_conntrack_l4proto *l4proto; |
d4156e8c | 1479 | struct nf_conntrack_tuple m; |
df6fb868 | 1480 | struct nlattr *nest_parms; |
d4156e8c PM |
1481 | |
1482 | memset(&m, 0xFF, sizeof(m)); | |
d4156e8c | 1483 | memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3)); |
e578756c PM |
1484 | m.src.u.all = mask->src.u.all; |
1485 | m.dst.protonum = tuple->dst.protonum; | |
d4156e8c | 1486 | |
df6fb868 PM |
1487 | nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED); |
1488 | if (!nest_parms) | |
1489 | goto nla_put_failure; | |
1cde6436 | 1490 | |
528a3a6f | 1491 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
d4156e8c | 1492 | ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto); |
1cde6436 PNA |
1493 | |
1494 | if (unlikely(ret < 0)) | |
df6fb868 | 1495 | goto nla_put_failure; |
1cde6436 | 1496 | |
528a3a6f | 1497 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); |
d4156e8c | 1498 | ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto); |
1cde6436 | 1499 | if (unlikely(ret < 0)) |
df6fb868 | 1500 | goto nla_put_failure; |
1cde6436 | 1501 | |
df6fb868 | 1502 | nla_nest_end(skb, nest_parms); |
1cde6436 PNA |
1503 | |
1504 | return 0; | |
1505 | ||
df6fb868 | 1506 | nla_put_failure: |
1cde6436 PNA |
1507 | return -1; |
1508 | } | |
1509 | ||
bb5cf80e | 1510 | static int |
c1d10adb | 1511 | ctnetlink_exp_dump_expect(struct sk_buff *skb, |
601e68e1 | 1512 | const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
1513 | { |
1514 | struct nf_conn *master = exp->master; | |
d1e7a03f | 1515 | struct nf_conntrack_helper *helper; |
d978e5da PM |
1516 | long timeout = (exp->timeout.expires - jiffies) / HZ; |
1517 | ||
1518 | if (timeout < 0) | |
1519 | timeout = 0; | |
c1d10adb PNA |
1520 | |
1521 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) | |
df6fb868 | 1522 | goto nla_put_failure; |
1cde6436 | 1523 | if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) |
df6fb868 | 1524 | goto nla_put_failure; |
c1d10adb PNA |
1525 | if (ctnetlink_exp_dump_tuple(skb, |
1526 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
1527 | CTA_EXPECT_MASTER) < 0) | |
df6fb868 | 1528 | goto nla_put_failure; |
601e68e1 | 1529 | |
d978e5da | 1530 | NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)); |
77236b6e | 1531 | NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)); |
d1e7a03f PM |
1532 | helper = rcu_dereference(nfct_help(master)->helper); |
1533 | if (helper) | |
1534 | NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name); | |
c1d10adb PNA |
1535 | |
1536 | return 0; | |
601e68e1 | 1537 | |
df6fb868 | 1538 | nla_put_failure: |
c1d10adb PNA |
1539 | return -1; |
1540 | } | |
1541 | ||
1542 | static int | |
1543 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq, | |
8b0a231d | 1544 | int event, const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
1545 | { |
1546 | struct nlmsghdr *nlh; | |
1547 | struct nfgenmsg *nfmsg; | |
96bcf938 | 1548 | unsigned int flags = pid ? NLM_F_MULTI : 0; |
c1d10adb PNA |
1549 | |
1550 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; | |
96bcf938 PNA |
1551 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags); |
1552 | if (nlh == NULL) | |
1553 | goto nlmsg_failure; | |
c1d10adb | 1554 | |
96bcf938 | 1555 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
1556 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
1557 | nfmsg->version = NFNETLINK_V0; | |
1558 | nfmsg->res_id = 0; | |
1559 | ||
1560 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) | |
df6fb868 | 1561 | goto nla_put_failure; |
c1d10adb | 1562 | |
96bcf938 | 1563 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
1564 | return skb->len; |
1565 | ||
1566 | nlmsg_failure: | |
df6fb868 | 1567 | nla_put_failure: |
96bcf938 | 1568 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
1569 | return -1; |
1570 | } | |
1571 | ||
1572 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
e34d5c1a PNA |
1573 | static int |
1574 | ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item) | |
c1d10adb | 1575 | { |
9592a5c0 AD |
1576 | struct nf_conntrack_expect *exp = item->exp; |
1577 | struct net *net = nf_ct_exp_net(exp); | |
c1d10adb PNA |
1578 | struct nlmsghdr *nlh; |
1579 | struct nfgenmsg *nfmsg; | |
c1d10adb PNA |
1580 | struct sk_buff *skb; |
1581 | unsigned int type; | |
c1d10adb PNA |
1582 | int flags = 0; |
1583 | ||
a0891aa6 | 1584 | if (events & (1 << IPEXP_NEW)) { |
c1d10adb PNA |
1585 | type = IPCTNL_MSG_EXP_NEW; |
1586 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
1587 | } else | |
e34d5c1a | 1588 | return 0; |
c1d10adb | 1589 | |
1f9da256 | 1590 | if (!item->report && |
9592a5c0 | 1591 | !nfnetlink_has_listeners(net, NFNLGRP_CONNTRACK_EXP_NEW)) |
e34d5c1a | 1592 | return 0; |
b3a27bfb | 1593 | |
96bcf938 PNA |
1594 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
1595 | if (skb == NULL) | |
150ace0d | 1596 | goto errout; |
c1d10adb | 1597 | |
b633ad5f | 1598 | type |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
96bcf938 PNA |
1599 | nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags); |
1600 | if (nlh == NULL) | |
1601 | goto nlmsg_failure; | |
c1d10adb | 1602 | |
96bcf938 | 1603 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
1604 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
1605 | nfmsg->version = NFNETLINK_V0; | |
1606 | nfmsg->res_id = 0; | |
1607 | ||
528a3a6f | 1608 | rcu_read_lock(); |
c1d10adb | 1609 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
df6fb868 | 1610 | goto nla_put_failure; |
528a3a6f | 1611 | rcu_read_unlock(); |
c1d10adb | 1612 | |
96bcf938 | 1613 | nlmsg_end(skb, nlh); |
9592a5c0 | 1614 | nfnetlink_send(skb, net, item->pid, NFNLGRP_CONNTRACK_EXP_NEW, |
e34d5c1a PNA |
1615 | item->report, GFP_ATOMIC); |
1616 | return 0; | |
c1d10adb | 1617 | |
df6fb868 | 1618 | nla_put_failure: |
528a3a6f | 1619 | rcu_read_unlock(); |
96bcf938 | 1620 | nlmsg_cancel(skb, nlh); |
528a3a6f | 1621 | nlmsg_failure: |
c1d10adb | 1622 | kfree_skb(skb); |
150ace0d | 1623 | errout: |
9592a5c0 | 1624 | nfnetlink_set_err(net, 0, 0, -ENOBUFS); |
e34d5c1a | 1625 | return 0; |
c1d10adb PNA |
1626 | } |
1627 | #endif | |
cf6994c2 PM |
1628 | static int ctnetlink_exp_done(struct netlink_callback *cb) |
1629 | { | |
31f15875 PM |
1630 | if (cb->args[1]) |
1631 | nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]); | |
cf6994c2 PM |
1632 | return 0; |
1633 | } | |
c1d10adb PNA |
1634 | |
1635 | static int | |
1636 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
1637 | { | |
9592a5c0 | 1638 | struct net *net = sock_net(skb->sk); |
cf6994c2 | 1639 | struct nf_conntrack_expect *exp, *last; |
96bcf938 | 1640 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
31f15875 | 1641 | struct hlist_node *n; |
87711cb8 | 1642 | u_int8_t l3proto = nfmsg->nfgen_family; |
c1d10adb | 1643 | |
7d0742da | 1644 | rcu_read_lock(); |
31f15875 PM |
1645 | last = (struct nf_conntrack_expect *)cb->args[1]; |
1646 | for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) { | |
cf6994c2 | 1647 | restart: |
9b03f38d | 1648 | hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]], |
31f15875 PM |
1649 | hnode) { |
1650 | if (l3proto && exp->tuple.src.l3num != l3proto) | |
cf6994c2 | 1651 | continue; |
31f15875 PM |
1652 | if (cb->args[1]) { |
1653 | if (exp != last) | |
1654 | continue; | |
1655 | cb->args[1] = 0; | |
1656 | } | |
8b0a231d PNA |
1657 | if (ctnetlink_exp_fill_info(skb, |
1658 | NETLINK_CB(cb->skb).pid, | |
31f15875 PM |
1659 | cb->nlh->nlmsg_seq, |
1660 | IPCTNL_MSG_EXP_NEW, | |
8b0a231d | 1661 | exp) < 0) { |
7d0742da PM |
1662 | if (!atomic_inc_not_zero(&exp->use)) |
1663 | continue; | |
31f15875 PM |
1664 | cb->args[1] = (unsigned long)exp; |
1665 | goto out; | |
1666 | } | |
cf6994c2 | 1667 | } |
31f15875 PM |
1668 | if (cb->args[1]) { |
1669 | cb->args[1] = 0; | |
1670 | goto restart; | |
cf6994c2 PM |
1671 | } |
1672 | } | |
601e68e1 | 1673 | out: |
7d0742da | 1674 | rcu_read_unlock(); |
cf6994c2 PM |
1675 | if (last) |
1676 | nf_ct_expect_put(last); | |
c1d10adb | 1677 | |
c1d10adb PNA |
1678 | return skb->len; |
1679 | } | |
1680 | ||
f73e924c | 1681 | static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = { |
d0b0268f PM |
1682 | [CTA_EXPECT_MASTER] = { .type = NLA_NESTED }, |
1683 | [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED }, | |
1684 | [CTA_EXPECT_MASK] = { .type = NLA_NESTED }, | |
f73e924c PM |
1685 | [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 }, |
1686 | [CTA_EXPECT_ID] = { .type = NLA_U32 }, | |
d0b0268f | 1687 | [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING }, |
c1d10adb PNA |
1688 | }; |
1689 | ||
1690 | static int | |
601e68e1 | 1691 | ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
1692 | const struct nlmsghdr *nlh, |
1693 | const struct nlattr * const cda[]) | |
c1d10adb | 1694 | { |
9592a5c0 | 1695 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1696 | struct nf_conntrack_tuple tuple; |
1697 | struct nf_conntrack_expect *exp; | |
1698 | struct sk_buff *skb2; | |
96bcf938 | 1699 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
1700 | u_int8_t u3 = nfmsg->nfgen_family; |
1701 | int err = 0; | |
1702 | ||
c1d10adb | 1703 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
c702e804 TG |
1704 | return netlink_dump_start(ctnl, skb, nlh, |
1705 | ctnetlink_exp_dump_table, | |
cf6994c2 | 1706 | ctnetlink_exp_done); |
c1d10adb PNA |
1707 | } |
1708 | ||
df6fb868 | 1709 | if (cda[CTA_EXPECT_MASTER]) |
c1d10adb PNA |
1710 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3); |
1711 | else | |
1712 | return -EINVAL; | |
1713 | ||
1714 | if (err < 0) | |
1715 | return err; | |
1716 | ||
5d0aa2cc | 1717 | exp = nf_ct_expect_find_get(net, 0, &tuple); |
c1d10adb PNA |
1718 | if (!exp) |
1719 | return -ENOENT; | |
1720 | ||
df6fb868 | 1721 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 1722 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 1723 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 1724 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1725 | return -ENOENT; |
1726 | } | |
601e68e1 | 1727 | } |
c1d10adb PNA |
1728 | |
1729 | err = -ENOMEM; | |
96bcf938 PNA |
1730 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1731 | if (skb2 == NULL) | |
c1d10adb | 1732 | goto out; |
4e9b8269 | 1733 | |
528a3a6f | 1734 | rcu_read_lock(); |
601e68e1 | 1735 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, |
8b0a231d | 1736 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp); |
528a3a6f | 1737 | rcu_read_unlock(); |
c1d10adb PNA |
1738 | if (err <= 0) |
1739 | goto free; | |
1740 | ||
6823645d | 1741 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1742 | |
1743 | return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
1744 | ||
1745 | free: | |
1746 | kfree_skb(skb2); | |
1747 | out: | |
6823645d | 1748 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1749 | return err; |
1750 | } | |
1751 | ||
1752 | static int | |
601e68e1 | 1753 | ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
1754 | const struct nlmsghdr *nlh, |
1755 | const struct nlattr * const cda[]) | |
c1d10adb | 1756 | { |
9592a5c0 | 1757 | struct net *net = sock_net(ctnl); |
31f15875 | 1758 | struct nf_conntrack_expect *exp; |
c1d10adb | 1759 | struct nf_conntrack_tuple tuple; |
96bcf938 | 1760 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
31f15875 | 1761 | struct hlist_node *n, *next; |
c1d10adb | 1762 | u_int8_t u3 = nfmsg->nfgen_family; |
31f15875 | 1763 | unsigned int i; |
c1d10adb PNA |
1764 | int err; |
1765 | ||
df6fb868 | 1766 | if (cda[CTA_EXPECT_TUPLE]) { |
c1d10adb PNA |
1767 | /* delete a single expect by tuple */ |
1768 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
1769 | if (err < 0) | |
1770 | return err; | |
1771 | ||
1772 | /* bump usage count to 2 */ | |
5d0aa2cc | 1773 | exp = nf_ct_expect_find_get(net, 0, &tuple); |
c1d10adb PNA |
1774 | if (!exp) |
1775 | return -ENOENT; | |
1776 | ||
df6fb868 | 1777 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 1778 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 1779 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 1780 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1781 | return -ENOENT; |
1782 | } | |
1783 | } | |
1784 | ||
1785 | /* after list removal, usage count == 1 */ | |
6823645d | 1786 | nf_ct_unexpect_related(exp); |
601e68e1 | 1787 | /* have to put what we 'get' above. |
c1d10adb | 1788 | * after this line usage count == 0 */ |
6823645d | 1789 | nf_ct_expect_put(exp); |
df6fb868 PM |
1790 | } else if (cda[CTA_EXPECT_HELP_NAME]) { |
1791 | char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
31f15875 | 1792 | struct nf_conn_help *m_help; |
c1d10adb PNA |
1793 | |
1794 | /* delete all expectations for this helper */ | |
f8ba1aff | 1795 | spin_lock_bh(&nf_conntrack_lock); |
31f15875 PM |
1796 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
1797 | hlist_for_each_entry_safe(exp, n, next, | |
9592a5c0 | 1798 | &net->ct.expect_hash[i], |
31f15875 PM |
1799 | hnode) { |
1800 | m_help = nfct_help(exp->master); | |
794e6871 PM |
1801 | if (!strcmp(m_help->helper->name, name) && |
1802 | del_timer(&exp->timeout)) { | |
31f15875 PM |
1803 | nf_ct_unlink_expect(exp); |
1804 | nf_ct_expect_put(exp); | |
1805 | } | |
c1d10adb PNA |
1806 | } |
1807 | } | |
f8ba1aff | 1808 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
1809 | } else { |
1810 | /* This basically means we have to flush everything*/ | |
f8ba1aff | 1811 | spin_lock_bh(&nf_conntrack_lock); |
31f15875 PM |
1812 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
1813 | hlist_for_each_entry_safe(exp, n, next, | |
9592a5c0 | 1814 | &net->ct.expect_hash[i], |
31f15875 PM |
1815 | hnode) { |
1816 | if (del_timer(&exp->timeout)) { | |
1817 | nf_ct_unlink_expect(exp); | |
1818 | nf_ct_expect_put(exp); | |
1819 | } | |
c1d10adb PNA |
1820 | } |
1821 | } | |
f8ba1aff | 1822 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
1823 | } |
1824 | ||
1825 | return 0; | |
1826 | } | |
1827 | static int | |
39938324 PM |
1828 | ctnetlink_change_expect(struct nf_conntrack_expect *x, |
1829 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1830 | { |
1831 | return -EOPNOTSUPP; | |
1832 | } | |
1833 | ||
1834 | static int | |
9592a5c0 AD |
1835 | ctnetlink_create_expect(struct net *net, const struct nlattr * const cda[], |
1836 | u_int8_t u3, | |
39938324 | 1837 | u32 pid, int report) |
c1d10adb PNA |
1838 | { |
1839 | struct nf_conntrack_tuple tuple, mask, master_tuple; | |
1840 | struct nf_conntrack_tuple_hash *h = NULL; | |
1841 | struct nf_conntrack_expect *exp; | |
1842 | struct nf_conn *ct; | |
dc808fe2 | 1843 | struct nf_conn_help *help; |
c1d10adb PNA |
1844 | int err = 0; |
1845 | ||
c1d10adb PNA |
1846 | /* caller guarantees that those three CTA_EXPECT_* exist */ |
1847 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
1848 | if (err < 0) | |
1849 | return err; | |
1850 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3); | |
1851 | if (err < 0) | |
1852 | return err; | |
1853 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3); | |
1854 | if (err < 0) | |
1855 | return err; | |
1856 | ||
1857 | /* Look for master conntrack of this expectation */ | |
5d0aa2cc | 1858 | h = nf_conntrack_find_get(net, 0, &master_tuple); |
c1d10adb PNA |
1859 | if (!h) |
1860 | return -ENOENT; | |
1861 | ct = nf_ct_tuplehash_to_ctrack(h); | |
dc808fe2 | 1862 | help = nfct_help(ct); |
c1d10adb | 1863 | |
dc808fe2 | 1864 | if (!help || !help->helper) { |
c1d10adb | 1865 | /* such conntrack hasn't got any helper, abort */ |
bfe29677 | 1866 | err = -EOPNOTSUPP; |
c1d10adb PNA |
1867 | goto out; |
1868 | } | |
1869 | ||
6823645d | 1870 | exp = nf_ct_expect_alloc(ct); |
c1d10adb PNA |
1871 | if (!exp) { |
1872 | err = -ENOMEM; | |
1873 | goto out; | |
1874 | } | |
601e68e1 | 1875 | |
626ba8fb | 1876 | exp->class = 0; |
c1d10adb PNA |
1877 | exp->expectfn = NULL; |
1878 | exp->flags = 0; | |
1879 | exp->master = ct; | |
9457d851 | 1880 | exp->helper = NULL; |
c1d10adb | 1881 | memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple)); |
d4156e8c PM |
1882 | memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3)); |
1883 | exp->mask.src.u.all = mask.src.u.all; | |
c1d10adb | 1884 | |
19abb7b0 | 1885 | err = nf_ct_expect_related_report(exp, pid, report); |
6823645d | 1886 | nf_ct_expect_put(exp); |
c1d10adb | 1887 | |
601e68e1 | 1888 | out: |
c1d10adb PNA |
1889 | nf_ct_put(nf_ct_tuplehash_to_ctrack(h)); |
1890 | return err; | |
1891 | } | |
1892 | ||
1893 | static int | |
1894 | ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
1895 | const struct nlmsghdr *nlh, |
1896 | const struct nlattr * const cda[]) | |
c1d10adb | 1897 | { |
9592a5c0 | 1898 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1899 | struct nf_conntrack_tuple tuple; |
1900 | struct nf_conntrack_expect *exp; | |
96bcf938 | 1901 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
1902 | u_int8_t u3 = nfmsg->nfgen_family; |
1903 | int err = 0; | |
1904 | ||
df6fb868 PM |
1905 | if (!cda[CTA_EXPECT_TUPLE] |
1906 | || !cda[CTA_EXPECT_MASK] | |
1907 | || !cda[CTA_EXPECT_MASTER]) | |
c1d10adb PNA |
1908 | return -EINVAL; |
1909 | ||
1910 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
1911 | if (err < 0) | |
1912 | return err; | |
1913 | ||
f8ba1aff | 1914 | spin_lock_bh(&nf_conntrack_lock); |
5d0aa2cc | 1915 | exp = __nf_ct_expect_find(net, 0, &tuple); |
c1d10adb PNA |
1916 | |
1917 | if (!exp) { | |
f8ba1aff | 1918 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 1919 | err = -ENOENT; |
19abb7b0 | 1920 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
9592a5c0 | 1921 | err = ctnetlink_create_expect(net, cda, |
19abb7b0 PNA |
1922 | u3, |
1923 | NETLINK_CB(skb).pid, | |
1924 | nlmsg_report(nlh)); | |
1925 | } | |
c1d10adb PNA |
1926 | return err; |
1927 | } | |
1928 | ||
1929 | err = -EEXIST; | |
1930 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) | |
1931 | err = ctnetlink_change_expect(exp, cda); | |
f8ba1aff | 1932 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 1933 | |
c1d10adb PNA |
1934 | return err; |
1935 | } | |
1936 | ||
1937 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
e34d5c1a PNA |
1938 | static struct nf_ct_event_notifier ctnl_notifier = { |
1939 | .fcn = ctnetlink_conntrack_event, | |
c1d10adb PNA |
1940 | }; |
1941 | ||
e34d5c1a PNA |
1942 | static struct nf_exp_event_notifier ctnl_notifier_exp = { |
1943 | .fcn = ctnetlink_expect_event, | |
c1d10adb PNA |
1944 | }; |
1945 | #endif | |
1946 | ||
7c8d4cb4 | 1947 | static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { |
c1d10adb | 1948 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, |
f73e924c PM |
1949 | .attr_count = CTA_MAX, |
1950 | .policy = ct_nla_policy }, | |
c1d10adb | 1951 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
1952 | .attr_count = CTA_MAX, |
1953 | .policy = ct_nla_policy }, | |
c1d10adb | 1954 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, |
f73e924c PM |
1955 | .attr_count = CTA_MAX, |
1956 | .policy = ct_nla_policy }, | |
c1d10adb | 1957 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
1958 | .attr_count = CTA_MAX, |
1959 | .policy = ct_nla_policy }, | |
c1d10adb PNA |
1960 | }; |
1961 | ||
7c8d4cb4 | 1962 | static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { |
c1d10adb | 1963 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
f73e924c PM |
1964 | .attr_count = CTA_EXPECT_MAX, |
1965 | .policy = exp_nla_policy }, | |
c1d10adb | 1966 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, |
f73e924c PM |
1967 | .attr_count = CTA_EXPECT_MAX, |
1968 | .policy = exp_nla_policy }, | |
c1d10adb | 1969 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, |
f73e924c PM |
1970 | .attr_count = CTA_EXPECT_MAX, |
1971 | .policy = exp_nla_policy }, | |
c1d10adb PNA |
1972 | }; |
1973 | ||
7c8d4cb4 | 1974 | static const struct nfnetlink_subsystem ctnl_subsys = { |
c1d10adb PNA |
1975 | .name = "conntrack", |
1976 | .subsys_id = NFNL_SUBSYS_CTNETLINK, | |
1977 | .cb_count = IPCTNL_MSG_MAX, | |
1978 | .cb = ctnl_cb, | |
1979 | }; | |
1980 | ||
7c8d4cb4 | 1981 | static const struct nfnetlink_subsystem ctnl_exp_subsys = { |
c1d10adb PNA |
1982 | .name = "conntrack_expect", |
1983 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, | |
1984 | .cb_count = IPCTNL_MSG_EXP_MAX, | |
1985 | .cb = ctnl_exp_cb, | |
1986 | }; | |
1987 | ||
d2483dde | 1988 | MODULE_ALIAS("ip_conntrack_netlink"); |
c1d10adb | 1989 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); |
34f9a2e4 | 1990 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); |
c1d10adb PNA |
1991 | |
1992 | static int __init ctnetlink_init(void) | |
1993 | { | |
1994 | int ret; | |
1995 | ||
1996 | printk("ctnetlink v%s: registering with nfnetlink.\n", version); | |
1997 | ret = nfnetlink_subsys_register(&ctnl_subsys); | |
1998 | if (ret < 0) { | |
1999 | printk("ctnetlink_init: cannot register with nfnetlink.\n"); | |
2000 | goto err_out; | |
2001 | } | |
2002 | ||
2003 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); | |
2004 | if (ret < 0) { | |
2005 | printk("ctnetlink_init: cannot register exp with nfnetlink.\n"); | |
2006 | goto err_unreg_subsys; | |
2007 | } | |
2008 | ||
2009 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2010 | ret = nf_conntrack_register_notifier(&ctnl_notifier); | |
2011 | if (ret < 0) { | |
2012 | printk("ctnetlink_init: cannot register notifier.\n"); | |
2013 | goto err_unreg_exp_subsys; | |
2014 | } | |
2015 | ||
6823645d | 2016 | ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp); |
c1d10adb PNA |
2017 | if (ret < 0) { |
2018 | printk("ctnetlink_init: cannot expect register notifier.\n"); | |
2019 | goto err_unreg_notifier; | |
2020 | } | |
2021 | #endif | |
2022 | ||
2023 | return 0; | |
2024 | ||
2025 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2026 | err_unreg_notifier: | |
2027 | nf_conntrack_unregister_notifier(&ctnl_notifier); | |
2028 | err_unreg_exp_subsys: | |
2029 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
2030 | #endif | |
2031 | err_unreg_subsys: | |
2032 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
2033 | err_out: | |
2034 | return ret; | |
2035 | } | |
2036 | ||
2037 | static void __exit ctnetlink_exit(void) | |
2038 | { | |
2039 | printk("ctnetlink: unregistering from nfnetlink.\n"); | |
2040 | ||
2041 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
6823645d | 2042 | nf_ct_expect_unregister_notifier(&ctnl_notifier_exp); |
c1d10adb PNA |
2043 | nf_conntrack_unregister_notifier(&ctnl_notifier); |
2044 | #endif | |
2045 | ||
2046 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
2047 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
2048 | return; | |
2049 | } | |
2050 | ||
2051 | module_init(ctnetlink_init); | |
2052 | module_exit(ctnetlink_exit); |