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