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