]>
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> |
70e9942f | 7 | * (C) 2005-2011 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> | |
1cc63249 | 25 | #include <linux/security.h> |
c1d10adb PNA |
26 | #include <linux/skbuff.h> |
27 | #include <linux/errno.h> | |
28 | #include <linux/netlink.h> | |
29 | #include <linux/spinlock.h> | |
40a839fd | 30 | #include <linux/interrupt.h> |
5a0e3ad6 | 31 | #include <linux/slab.h> |
c1d10adb PNA |
32 | |
33 | #include <linux/netfilter.h> | |
dc5fc579 | 34 | #include <net/netlink.h> |
9592a5c0 | 35 | #include <net/sock.h> |
c1d10adb PNA |
36 | #include <net/netfilter/nf_conntrack.h> |
37 | #include <net/netfilter/nf_conntrack_core.h> | |
77ab9cff | 38 | #include <net/netfilter/nf_conntrack_expect.h> |
c1d10adb PNA |
39 | #include <net/netfilter/nf_conntrack_helper.h> |
40 | #include <net/netfilter/nf_conntrack_l3proto.h> | |
605dcad6 | 41 | #include <net/netfilter/nf_conntrack_l4proto.h> |
5b1158e9 | 42 | #include <net/netfilter/nf_conntrack_tuple.h> |
58401572 | 43 | #include <net/netfilter/nf_conntrack_acct.h> |
ef00f89f | 44 | #include <net/netfilter/nf_conntrack_zones.h> |
a992ca2a | 45 | #include <net/netfilter/nf_conntrack_timestamp.h> |
5b1158e9 JK |
46 | #ifdef CONFIG_NF_NAT_NEEDED |
47 | #include <net/netfilter/nf_nat_core.h> | |
48 | #include <net/netfilter/nf_nat_protocol.h> | |
49 | #endif | |
c1d10adb PNA |
50 | |
51 | #include <linux/netfilter/nfnetlink.h> | |
52 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
53 | ||
54 | MODULE_LICENSE("GPL"); | |
55 | ||
dc808fe2 | 56 | static char __initdata version[] = "0.93"; |
c1d10adb | 57 | |
c1d10adb | 58 | static inline int |
601e68e1 | 59 | ctnetlink_dump_tuples_proto(struct sk_buff *skb, |
1cde6436 | 60 | const struct nf_conntrack_tuple *tuple, |
605dcad6 | 61 | struct nf_conntrack_l4proto *l4proto) |
c1d10adb | 62 | { |
c1d10adb | 63 | int ret = 0; |
df6fb868 | 64 | struct nlattr *nest_parms; |
c1d10adb | 65 | |
df6fb868 PM |
66 | nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED); |
67 | if (!nest_parms) | |
68 | goto nla_put_failure; | |
cc1eb431 DM |
69 | if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum)) |
70 | goto nla_put_failure; | |
c1d10adb | 71 | |
fdf70832 PM |
72 | if (likely(l4proto->tuple_to_nlattr)) |
73 | ret = l4proto->tuple_to_nlattr(skb, tuple); | |
601e68e1 | 74 | |
df6fb868 | 75 | nla_nest_end(skb, nest_parms); |
c1d10adb PNA |
76 | |
77 | return ret; | |
78 | ||
df6fb868 | 79 | nla_put_failure: |
c1d10adb PNA |
80 | return -1; |
81 | } | |
82 | ||
83 | static inline int | |
1cde6436 PNA |
84 | ctnetlink_dump_tuples_ip(struct sk_buff *skb, |
85 | const struct nf_conntrack_tuple *tuple, | |
86 | struct nf_conntrack_l3proto *l3proto) | |
c1d10adb | 87 | { |
c1d10adb | 88 | int ret = 0; |
df6fb868 PM |
89 | struct nlattr *nest_parms; |
90 | ||
91 | nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED); | |
92 | if (!nest_parms) | |
93 | goto nla_put_failure; | |
1cde6436 | 94 | |
fdf70832 PM |
95 | if (likely(l3proto->tuple_to_nlattr)) |
96 | ret = l3proto->tuple_to_nlattr(skb, tuple); | |
1cde6436 | 97 | |
df6fb868 | 98 | nla_nest_end(skb, nest_parms); |
c1d10adb | 99 | |
1cde6436 PNA |
100 | return ret; |
101 | ||
df6fb868 | 102 | nla_put_failure: |
1cde6436 PNA |
103 | return -1; |
104 | } | |
105 | ||
bb5cf80e | 106 | static int |
1cde6436 PNA |
107 | ctnetlink_dump_tuples(struct sk_buff *skb, |
108 | const struct nf_conntrack_tuple *tuple) | |
109 | { | |
110 | int ret; | |
111 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 112 | struct nf_conntrack_l4proto *l4proto; |
1cde6436 | 113 | |
3b988ece | 114 | rcu_read_lock(); |
528a3a6f | 115 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
1cde6436 | 116 | ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto); |
c1d10adb | 117 | |
3b988ece HS |
118 | if (ret >= 0) { |
119 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, | |
120 | tuple->dst.protonum); | |
121 | ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto); | |
122 | } | |
123 | rcu_read_unlock(); | |
c1d10adb | 124 | return ret; |
c1d10adb PNA |
125 | } |
126 | ||
127 | static inline int | |
128 | ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct) | |
129 | { | |
cc1eb431 DM |
130 | if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status))) |
131 | goto nla_put_failure; | |
c1d10adb PNA |
132 | return 0; |
133 | ||
df6fb868 | 134 | nla_put_failure: |
c1d10adb PNA |
135 | return -1; |
136 | } | |
137 | ||
138 | static inline int | |
139 | ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct) | |
140 | { | |
c1216382 | 141 | long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ; |
c1d10adb | 142 | |
77236b6e | 143 | if (timeout < 0) |
c1d10adb | 144 | timeout = 0; |
601e68e1 | 145 | |
cc1eb431 DM |
146 | if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout))) |
147 | goto nla_put_failure; | |
c1d10adb PNA |
148 | return 0; |
149 | ||
df6fb868 | 150 | nla_put_failure: |
c1d10adb PNA |
151 | return -1; |
152 | } | |
153 | ||
154 | static inline int | |
440f0d58 | 155 | ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct) |
c1d10adb | 156 | { |
5e8fbe2a | 157 | struct nf_conntrack_l4proto *l4proto; |
df6fb868 | 158 | struct nlattr *nest_proto; |
c1d10adb PNA |
159 | int ret; |
160 | ||
528a3a6f PNA |
161 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
162 | if (!l4proto->to_nlattr) | |
c1d10adb | 163 | return 0; |
601e68e1 | 164 | |
df6fb868 PM |
165 | nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED); |
166 | if (!nest_proto) | |
167 | goto nla_put_failure; | |
c1d10adb | 168 | |
fdf70832 | 169 | ret = l4proto->to_nlattr(skb, nest_proto, ct); |
c1d10adb | 170 | |
df6fb868 | 171 | nla_nest_end(skb, nest_proto); |
c1d10adb PNA |
172 | |
173 | return ret; | |
174 | ||
df6fb868 | 175 | nla_put_failure: |
c1d10adb PNA |
176 | return -1; |
177 | } | |
178 | ||
179 | static inline int | |
180 | ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct) | |
181 | { | |
df6fb868 | 182 | struct nlattr *nest_helper; |
dc808fe2 | 183 | const struct nf_conn_help *help = nfct_help(ct); |
3c158f7f | 184 | struct nf_conntrack_helper *helper; |
c1d10adb | 185 | |
3c158f7f | 186 | if (!help) |
c1d10adb | 187 | return 0; |
601e68e1 | 188 | |
3c158f7f PM |
189 | helper = rcu_dereference(help->helper); |
190 | if (!helper) | |
191 | goto out; | |
192 | ||
df6fb868 PM |
193 | nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED); |
194 | if (!nest_helper) | |
195 | goto nla_put_failure; | |
cc1eb431 DM |
196 | if (nla_put_string(skb, CTA_HELP_NAME, helper->name)) |
197 | goto nla_put_failure; | |
c1d10adb | 198 | |
fdf70832 PM |
199 | if (helper->to_nlattr) |
200 | helper->to_nlattr(skb, ct); | |
c1d10adb | 201 | |
df6fb868 | 202 | nla_nest_end(skb, nest_helper); |
3c158f7f | 203 | out: |
c1d10adb PNA |
204 | return 0; |
205 | ||
df6fb868 | 206 | nla_put_failure: |
c1d10adb PNA |
207 | return -1; |
208 | } | |
209 | ||
bb5cf80e | 210 | static int |
80e60e67 PNA |
211 | dump_counters(struct sk_buff *skb, u64 pkts, u64 bytes, |
212 | enum ip_conntrack_dir dir) | |
c1d10adb PNA |
213 | { |
214 | enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; | |
df6fb868 | 215 | struct nlattr *nest_count; |
c1d10adb | 216 | |
df6fb868 PM |
217 | nest_count = nla_nest_start(skb, type | NLA_F_NESTED); |
218 | if (!nest_count) | |
219 | goto nla_put_failure; | |
220 | ||
cc1eb431 DM |
221 | if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts)) || |
222 | nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes))) | |
223 | goto nla_put_failure; | |
c1d10adb | 224 | |
df6fb868 | 225 | nla_nest_end(skb, nest_count); |
c1d10adb PNA |
226 | |
227 | return 0; | |
228 | ||
df6fb868 | 229 | nla_put_failure: |
c1d10adb PNA |
230 | return -1; |
231 | } | |
c1d10adb | 232 | |
80e60e67 PNA |
233 | static int |
234 | ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct, | |
235 | enum ip_conntrack_dir dir, int type) | |
236 | { | |
237 | struct nf_conn_counter *acct; | |
238 | u64 pkts, bytes; | |
239 | ||
240 | acct = nf_conn_acct_find(ct); | |
241 | if (!acct) | |
242 | return 0; | |
243 | ||
244 | if (type == IPCTNL_MSG_CT_GET_CTRZERO) { | |
245 | pkts = atomic64_xchg(&acct[dir].packets, 0); | |
246 | bytes = atomic64_xchg(&acct[dir].bytes, 0); | |
247 | } else { | |
248 | pkts = atomic64_read(&acct[dir].packets); | |
249 | bytes = atomic64_read(&acct[dir].bytes); | |
250 | } | |
251 | return dump_counters(skb, pkts, bytes, dir); | |
252 | } | |
253 | ||
a992ca2a PNA |
254 | static int |
255 | ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct) | |
256 | { | |
257 | struct nlattr *nest_count; | |
258 | const struct nf_conn_tstamp *tstamp; | |
259 | ||
260 | tstamp = nf_conn_tstamp_find(ct); | |
261 | if (!tstamp) | |
262 | return 0; | |
263 | ||
264 | nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED); | |
265 | if (!nest_count) | |
266 | goto nla_put_failure; | |
267 | ||
cc1eb431 DM |
268 | if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start)) || |
269 | (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP, | |
270 | cpu_to_be64(tstamp->stop)))) | |
271 | goto nla_put_failure; | |
a992ca2a PNA |
272 | nla_nest_end(skb, nest_count); |
273 | ||
274 | return 0; | |
275 | ||
276 | nla_put_failure: | |
277 | return -1; | |
278 | } | |
279 | ||
c1d10adb PNA |
280 | #ifdef CONFIG_NF_CONNTRACK_MARK |
281 | static inline int | |
282 | ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct) | |
283 | { | |
cc1eb431 DM |
284 | if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark))) |
285 | goto nla_put_failure; | |
c1d10adb PNA |
286 | return 0; |
287 | ||
df6fb868 | 288 | nla_put_failure: |
c1d10adb PNA |
289 | return -1; |
290 | } | |
291 | #else | |
292 | #define ctnetlink_dump_mark(a, b) (0) | |
293 | #endif | |
294 | ||
37fccd85 PNA |
295 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
296 | static inline int | |
1cc63249 | 297 | ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct) |
37fccd85 | 298 | { |
1cc63249 EP |
299 | struct nlattr *nest_secctx; |
300 | int len, ret; | |
301 | char *secctx; | |
302 | ||
303 | ret = security_secid_to_secctx(ct->secmark, &secctx, &len); | |
304 | if (ret) | |
cba85b53 | 305 | return 0; |
1cc63249 EP |
306 | |
307 | ret = -1; | |
308 | nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED); | |
309 | if (!nest_secctx) | |
310 | goto nla_put_failure; | |
37fccd85 | 311 | |
cc1eb431 DM |
312 | if (nla_put_string(skb, CTA_SECCTX_NAME, secctx)) |
313 | goto nla_put_failure; | |
1cc63249 EP |
314 | nla_nest_end(skb, nest_secctx); |
315 | ||
316 | ret = 0; | |
37fccd85 | 317 | nla_put_failure: |
1cc63249 EP |
318 | security_release_secctx(secctx, len); |
319 | return ret; | |
37fccd85 PNA |
320 | } |
321 | #else | |
1cc63249 | 322 | #define ctnetlink_dump_secctx(a, b) (0) |
37fccd85 PNA |
323 | #endif |
324 | ||
0f417ce9 PNA |
325 | #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple) |
326 | ||
327 | static inline int | |
328 | ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct) | |
329 | { | |
330 | struct nlattr *nest_parms; | |
331 | ||
332 | if (!(ct->status & IPS_EXPECTED)) | |
333 | return 0; | |
334 | ||
335 | nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED); | |
336 | if (!nest_parms) | |
337 | goto nla_put_failure; | |
338 | if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0) | |
339 | goto nla_put_failure; | |
340 | nla_nest_end(skb, nest_parms); | |
341 | ||
342 | return 0; | |
343 | ||
344 | nla_put_failure: | |
345 | return -1; | |
346 | } | |
347 | ||
13eae15a | 348 | #ifdef CONFIG_NF_NAT_NEEDED |
bb5cf80e | 349 | static int |
13eae15a PNA |
350 | dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type) |
351 | { | |
13eae15a PNA |
352 | struct nlattr *nest_parms; |
353 | ||
354 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); | |
355 | if (!nest_parms) | |
356 | goto nla_put_failure; | |
357 | ||
cc1eb431 DM |
358 | if (nla_put_be32(skb, CTA_NAT_SEQ_CORRECTION_POS, |
359 | htonl(natseq->correction_pos)) || | |
360 | nla_put_be32(skb, CTA_NAT_SEQ_OFFSET_BEFORE, | |
361 | htonl(natseq->offset_before)) || | |
362 | nla_put_be32(skb, CTA_NAT_SEQ_OFFSET_AFTER, | |
363 | htonl(natseq->offset_after))) | |
364 | goto nla_put_failure; | |
13eae15a PNA |
365 | |
366 | nla_nest_end(skb, nest_parms); | |
367 | ||
368 | return 0; | |
369 | ||
370 | nla_put_failure: | |
371 | return -1; | |
372 | } | |
373 | ||
374 | static inline int | |
375 | ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct) | |
376 | { | |
377 | struct nf_nat_seq *natseq; | |
378 | struct nf_conn_nat *nat = nfct_nat(ct); | |
379 | ||
380 | if (!(ct->status & IPS_SEQ_ADJUST) || !nat) | |
381 | return 0; | |
382 | ||
383 | natseq = &nat->seq[IP_CT_DIR_ORIGINAL]; | |
384 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1) | |
385 | return -1; | |
386 | ||
387 | natseq = &nat->seq[IP_CT_DIR_REPLY]; | |
388 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1) | |
389 | return -1; | |
390 | ||
391 | return 0; | |
392 | } | |
393 | #else | |
394 | #define ctnetlink_dump_nat_seq_adj(a, b) (0) | |
395 | #endif | |
396 | ||
c1d10adb PNA |
397 | static inline int |
398 | ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) | |
399 | { | |
cc1eb431 DM |
400 | if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct))) |
401 | goto nla_put_failure; | |
c1d10adb PNA |
402 | return 0; |
403 | ||
df6fb868 | 404 | nla_put_failure: |
c1d10adb PNA |
405 | return -1; |
406 | } | |
407 | ||
408 | static inline int | |
409 | ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct) | |
410 | { | |
cc1eb431 DM |
411 | if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)))) |
412 | goto nla_put_failure; | |
c1d10adb PNA |
413 | return 0; |
414 | ||
df6fb868 | 415 | nla_put_failure: |
c1d10adb PNA |
416 | return -1; |
417 | } | |
418 | ||
c1d10adb | 419 | static int |
80e60e67 PNA |
420 | ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, u32 type, |
421 | struct nf_conn *ct) | |
c1d10adb PNA |
422 | { |
423 | struct nlmsghdr *nlh; | |
424 | struct nfgenmsg *nfmsg; | |
df6fb868 | 425 | struct nlattr *nest_parms; |
80e60e67 | 426 | unsigned int flags = pid ? NLM_F_MULTI : 0, event; |
c1d10adb | 427 | |
80e60e67 | 428 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW); |
96bcf938 PNA |
429 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags); |
430 | if (nlh == NULL) | |
431 | goto nlmsg_failure; | |
c1d10adb | 432 | |
96bcf938 | 433 | nfmsg = nlmsg_data(nlh); |
5e8fbe2a | 434 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
435 | nfmsg->version = NFNETLINK_V0; |
436 | nfmsg->res_id = 0; | |
437 | ||
df6fb868 PM |
438 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
439 | if (!nest_parms) | |
440 | goto nla_put_failure; | |
f2f3e38c | 441 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
442 | goto nla_put_failure; |
443 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 444 | |
df6fb868 PM |
445 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
446 | if (!nest_parms) | |
447 | goto nla_put_failure; | |
f2f3e38c | 448 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
449 | goto nla_put_failure; |
450 | nla_nest_end(skb, nest_parms); | |
c1d10adb | 451 | |
cc1eb431 DM |
452 | if (nf_ct_zone(ct) && |
453 | nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct)))) | |
454 | goto nla_put_failure; | |
ef00f89f | 455 | |
c1d10adb PNA |
456 | if (ctnetlink_dump_status(skb, ct) < 0 || |
457 | ctnetlink_dump_timeout(skb, ct) < 0 || | |
80e60e67 PNA |
458 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL, type) < 0 || |
459 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY, type) < 0 || | |
a992ca2a | 460 | ctnetlink_dump_timestamp(skb, ct) < 0 || |
c1d10adb PNA |
461 | ctnetlink_dump_protoinfo(skb, ct) < 0 || |
462 | ctnetlink_dump_helpinfo(skb, ct) < 0 || | |
463 | ctnetlink_dump_mark(skb, ct) < 0 || | |
1cc63249 | 464 | ctnetlink_dump_secctx(skb, ct) < 0 || |
c1d10adb | 465 | ctnetlink_dump_id(skb, ct) < 0 || |
13eae15a | 466 | ctnetlink_dump_use(skb, ct) < 0 || |
0f417ce9 | 467 | ctnetlink_dump_master(skb, ct) < 0 || |
13eae15a | 468 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
df6fb868 | 469 | goto nla_put_failure; |
c1d10adb | 470 | |
96bcf938 | 471 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
472 | return skb->len; |
473 | ||
474 | nlmsg_failure: | |
df6fb868 | 475 | nla_put_failure: |
96bcf938 | 476 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
477 | return -1; |
478 | } | |
479 | ||
480 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
03b64f51 PNA |
481 | static inline size_t |
482 | ctnetlink_proto_size(const struct nf_conn *ct) | |
2732c4e4 HE |
483 | { |
484 | struct nf_conntrack_l3proto *l3proto; | |
485 | struct nf_conntrack_l4proto *l4proto; | |
03b64f51 PNA |
486 | size_t len = 0; |
487 | ||
488 | rcu_read_lock(); | |
489 | l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct)); | |
490 | len += l3proto->nla_size; | |
491 | ||
492 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
493 | len += l4proto->nla_size; | |
494 | rcu_read_unlock(); | |
495 | ||
496 | return len; | |
497 | } | |
498 | ||
d26e6a02 JP |
499 | static inline size_t |
500 | ctnetlink_counters_size(const struct nf_conn *ct) | |
501 | { | |
502 | if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT)) | |
503 | return 0; | |
504 | return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */ | |
505 | + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */ | |
506 | + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */ | |
507 | ; | |
508 | } | |
509 | ||
cba85b53 PNA |
510 | static inline int |
511 | ctnetlink_secctx_size(const struct nf_conn *ct) | |
1cc63249 | 512 | { |
cba85b53 PNA |
513 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
514 | int len, ret; | |
1cc63249 | 515 | |
cba85b53 PNA |
516 | ret = security_secid_to_secctx(ct->secmark, NULL, &len); |
517 | if (ret) | |
518 | return 0; | |
1cc63249 | 519 | |
cba85b53 PNA |
520 | return nla_total_size(0) /* CTA_SECCTX */ |
521 | + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */ | |
522 | #else | |
523 | return 0; | |
1cc63249 | 524 | #endif |
cba85b53 | 525 | } |
1cc63249 | 526 | |
a992ca2a PNA |
527 | static inline size_t |
528 | ctnetlink_timestamp_size(const struct nf_conn *ct) | |
529 | { | |
530 | #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP | |
531 | if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP)) | |
532 | return 0; | |
533 | return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t)); | |
534 | #else | |
535 | return 0; | |
536 | #endif | |
537 | } | |
538 | ||
03b64f51 PNA |
539 | static inline size_t |
540 | ctnetlink_nlmsg_size(const struct nf_conn *ct) | |
541 | { | |
542 | return NLMSG_ALIGN(sizeof(struct nfgenmsg)) | |
543 | + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ | |
544 | + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ | |
545 | + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ | |
546 | + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ | |
547 | + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ | |
548 | + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ | |
d26e6a02 | 549 | + ctnetlink_counters_size(ct) |
a992ca2a | 550 | + ctnetlink_timestamp_size(ct) |
03b64f51 PNA |
551 | + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ |
552 | + nla_total_size(0) /* CTA_PROTOINFO */ | |
553 | + nla_total_size(0) /* CTA_HELP */ | |
554 | + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ | |
cba85b53 | 555 | + ctnetlink_secctx_size(ct) |
d271e8bd | 556 | #ifdef CONFIG_NF_NAT_NEEDED |
03b64f51 PNA |
557 | + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ |
558 | + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ | |
d271e8bd HE |
559 | #endif |
560 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
03b64f51 | 561 | + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ |
d271e8bd | 562 | #endif |
03b64f51 PNA |
563 | + ctnetlink_proto_size(ct) |
564 | ; | |
2732c4e4 HE |
565 | } |
566 | ||
e34d5c1a PNA |
567 | static int |
568 | ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item) | |
c1d10adb | 569 | { |
9592a5c0 | 570 | struct net *net; |
c1d10adb PNA |
571 | struct nlmsghdr *nlh; |
572 | struct nfgenmsg *nfmsg; | |
df6fb868 | 573 | struct nlattr *nest_parms; |
19abb7b0 | 574 | struct nf_conn *ct = item->ct; |
c1d10adb PNA |
575 | struct sk_buff *skb; |
576 | unsigned int type; | |
c1d10adb | 577 | unsigned int flags = 0, group; |
dd7669a9 | 578 | int err; |
c1d10adb PNA |
579 | |
580 | /* ignore our fake conntrack entry */ | |
5bfddbd4 | 581 | if (nf_ct_is_untracked(ct)) |
e34d5c1a | 582 | return 0; |
c1d10adb | 583 | |
a0891aa6 | 584 | if (events & (1 << IPCT_DESTROY)) { |
c1d10adb PNA |
585 | type = IPCTNL_MSG_CT_DELETE; |
586 | group = NFNLGRP_CONNTRACK_DESTROY; | |
a0891aa6 | 587 | } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) { |
c1d10adb PNA |
588 | type = IPCTNL_MSG_CT_NEW; |
589 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
c1d10adb | 590 | group = NFNLGRP_CONNTRACK_NEW; |
17e6e4ea | 591 | } else if (events) { |
c1d10adb PNA |
592 | type = IPCTNL_MSG_CT_NEW; |
593 | group = NFNLGRP_CONNTRACK_UPDATE; | |
594 | } else | |
e34d5c1a | 595 | return 0; |
a2427692 | 596 | |
9592a5c0 AD |
597 | net = nf_ct_net(ct); |
598 | if (!item->report && !nfnetlink_has_listeners(net, group)) | |
e34d5c1a | 599 | return 0; |
a2427692 | 600 | |
03b64f51 PNA |
601 | skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC); |
602 | if (skb == NULL) | |
150ace0d | 603 | goto errout; |
c1d10adb | 604 | |
c1d10adb | 605 | type |= NFNL_SUBSYS_CTNETLINK << 8; |
96bcf938 PNA |
606 | nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags); |
607 | if (nlh == NULL) | |
608 | goto nlmsg_failure; | |
c1d10adb | 609 | |
96bcf938 | 610 | nfmsg = nlmsg_data(nlh); |
5e8fbe2a | 611 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
612 | nfmsg->version = NFNETLINK_V0; |
613 | nfmsg->res_id = 0; | |
614 | ||
528a3a6f | 615 | rcu_read_lock(); |
df6fb868 PM |
616 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
617 | if (!nest_parms) | |
618 | goto nla_put_failure; | |
f2f3e38c | 619 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
620 | goto nla_put_failure; |
621 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 622 | |
df6fb868 PM |
623 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
624 | if (!nest_parms) | |
625 | goto nla_put_failure; | |
f2f3e38c | 626 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
627 | goto nla_put_failure; |
628 | nla_nest_end(skb, nest_parms); | |
c1d10adb | 629 | |
cc1eb431 DM |
630 | if (nf_ct_zone(ct) && |
631 | nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct)))) | |
632 | goto nla_put_failure; | |
ef00f89f | 633 | |
1eedf699 EL |
634 | if (ctnetlink_dump_id(skb, ct) < 0) |
635 | goto nla_put_failure; | |
636 | ||
e57dce60 FH |
637 | if (ctnetlink_dump_status(skb, ct) < 0) |
638 | goto nla_put_failure; | |
639 | ||
a0891aa6 | 640 | if (events & (1 << IPCT_DESTROY)) { |
80e60e67 PNA |
641 | if (ctnetlink_dump_counters(skb, ct, |
642 | IP_CT_DIR_ORIGINAL, type) < 0 || | |
643 | ctnetlink_dump_counters(skb, ct, | |
644 | IP_CT_DIR_REPLY, type) < 0 || | |
a992ca2a | 645 | ctnetlink_dump_timestamp(skb, ct) < 0) |
df6fb868 | 646 | goto nla_put_failure; |
7b621c1e | 647 | } else { |
7b621c1e | 648 | if (ctnetlink_dump_timeout(skb, ct) < 0) |
df6fb868 | 649 | goto nla_put_failure; |
7b621c1e | 650 | |
a0891aa6 | 651 | if (events & (1 << IPCT_PROTOINFO) |
7b621c1e | 652 | && ctnetlink_dump_protoinfo(skb, ct) < 0) |
df6fb868 | 653 | goto nla_put_failure; |
7b621c1e | 654 | |
a0891aa6 | 655 | if ((events & (1 << IPCT_HELPER) || nfct_help(ct)) |
7b621c1e | 656 | && ctnetlink_dump_helpinfo(skb, ct) < 0) |
df6fb868 | 657 | goto nla_put_failure; |
7b621c1e | 658 | |
ff660c80 | 659 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
a0891aa6 | 660 | if ((events & (1 << IPCT_SECMARK) || ct->secmark) |
1cc63249 | 661 | && ctnetlink_dump_secctx(skb, ct) < 0) |
37fccd85 | 662 | goto nla_put_failure; |
ff660c80 | 663 | #endif |
7b621c1e | 664 | |
a0891aa6 | 665 | if (events & (1 << IPCT_RELATED) && |
0f417ce9 PNA |
666 | ctnetlink_dump_master(skb, ct) < 0) |
667 | goto nla_put_failure; | |
668 | ||
a0891aa6 | 669 | if (events & (1 << IPCT_NATSEQADJ) && |
13eae15a PNA |
670 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
671 | goto nla_put_failure; | |
7b621c1e | 672 | } |
b9a37e0c | 673 | |
a83099a6 | 674 | #ifdef CONFIG_NF_CONNTRACK_MARK |
a0891aa6 | 675 | if ((events & (1 << IPCT_MARK) || ct->mark) |
a83099a6 EL |
676 | && ctnetlink_dump_mark(skb, ct) < 0) |
677 | goto nla_put_failure; | |
678 | #endif | |
528a3a6f | 679 | rcu_read_unlock(); |
a83099a6 | 680 | |
96bcf938 | 681 | nlmsg_end(skb, nlh); |
9592a5c0 | 682 | err = nfnetlink_send(skb, net, item->pid, group, item->report, |
cd8c20b6 | 683 | GFP_ATOMIC); |
dd7669a9 PNA |
684 | if (err == -ENOBUFS || err == -EAGAIN) |
685 | return -ENOBUFS; | |
686 | ||
e34d5c1a | 687 | return 0; |
c1d10adb | 688 | |
df6fb868 | 689 | nla_put_failure: |
528a3a6f | 690 | rcu_read_unlock(); |
96bcf938 | 691 | nlmsg_cancel(skb, nlh); |
528a3a6f | 692 | nlmsg_failure: |
c1d10adb | 693 | kfree_skb(skb); |
150ace0d | 694 | errout: |
37b7ef72 PNA |
695 | if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0) |
696 | return -ENOBUFS; | |
697 | ||
e34d5c1a | 698 | return 0; |
c1d10adb PNA |
699 | } |
700 | #endif /* CONFIG_NF_CONNTRACK_EVENTS */ | |
701 | ||
702 | static int ctnetlink_done(struct netlink_callback *cb) | |
703 | { | |
89f2e218 PM |
704 | if (cb->args[1]) |
705 | nf_ct_put((struct nf_conn *)cb->args[1]); | |
0f298a28 PNA |
706 | if (cb->data) |
707 | kfree(cb->data); | |
c1d10adb PNA |
708 | return 0; |
709 | } | |
710 | ||
0f298a28 PNA |
711 | struct ctnetlink_dump_filter { |
712 | struct { | |
713 | u_int32_t val; | |
714 | u_int32_t mask; | |
715 | } mark; | |
716 | }; | |
717 | ||
c1d10adb PNA |
718 | static int |
719 | ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
720 | { | |
9592a5c0 | 721 | struct net *net = sock_net(skb->sk); |
89f2e218 | 722 | struct nf_conn *ct, *last; |
c1d10adb | 723 | struct nf_conntrack_tuple_hash *h; |
ea781f19 | 724 | struct hlist_nulls_node *n; |
96bcf938 | 725 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
87711cb8 | 726 | u_int8_t l3proto = nfmsg->nfgen_family; |
3b988ece | 727 | int res; |
0f298a28 PNA |
728 | #ifdef CONFIG_NF_CONNTRACK_MARK |
729 | const struct ctnetlink_dump_filter *filter = cb->data; | |
730 | #endif | |
3b988ece | 731 | |
13ee6ac5 | 732 | spin_lock_bh(&nf_conntrack_lock); |
d205dc40 | 733 | last = (struct nf_conn *)cb->args[1]; |
9ab99d5a | 734 | for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) { |
89f2e218 | 735 | restart: |
13ee6ac5 | 736 | hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]], |
ea781f19 | 737 | hnnode) { |
5b1158e9 | 738 | if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
c1d10adb PNA |
739 | continue; |
740 | ct = nf_ct_tuplehash_to_ctrack(h); | |
87711cb8 PNA |
741 | /* Dump entries of a given L3 protocol number. |
742 | * If it is not specified, ie. l3proto == 0, | |
743 | * then dump everything. */ | |
5e8fbe2a | 744 | if (l3proto && nf_ct_l3num(ct) != l3proto) |
13ee6ac5 | 745 | continue; |
d205dc40 PM |
746 | if (cb->args[1]) { |
747 | if (ct != last) | |
13ee6ac5 | 748 | continue; |
d205dc40 | 749 | cb->args[1] = 0; |
89f2e218 | 750 | } |
0f298a28 PNA |
751 | #ifdef CONFIG_NF_CONNTRACK_MARK |
752 | if (filter && !((ct->mark & filter->mark.mask) == | |
753 | filter->mark.val)) { | |
754 | continue; | |
755 | } | |
756 | #endif | |
3b988ece HS |
757 | rcu_read_lock(); |
758 | res = | |
759 | ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, | |
760 | cb->nlh->nlmsg_seq, | |
761 | NFNL_MSG_TYPE(cb->nlh->nlmsg_type), | |
762 | ct); | |
763 | rcu_read_unlock(); | |
764 | if (res < 0) { | |
c71caf41 | 765 | nf_conntrack_get(&ct->ct_general); |
89f2e218 | 766 | cb->args[1] = (unsigned long)ct; |
c1d10adb | 767 | goto out; |
89f2e218 PM |
768 | } |
769 | } | |
d205dc40 | 770 | if (cb->args[1]) { |
89f2e218 PM |
771 | cb->args[1] = 0; |
772 | goto restart; | |
c1d10adb PNA |
773 | } |
774 | } | |
89f2e218 | 775 | out: |
13ee6ac5 | 776 | spin_unlock_bh(&nf_conntrack_lock); |
d205dc40 PM |
777 | if (last) |
778 | nf_ct_put(last); | |
c1d10adb | 779 | |
c1d10adb PNA |
780 | return skb->len; |
781 | } | |
782 | ||
c1d10adb | 783 | static inline int |
df6fb868 | 784 | ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple) |
c1d10adb | 785 | { |
df6fb868 | 786 | struct nlattr *tb[CTA_IP_MAX+1]; |
c1d10adb PNA |
787 | struct nf_conntrack_l3proto *l3proto; |
788 | int ret = 0; | |
789 | ||
df6fb868 | 790 | nla_parse_nested(tb, CTA_IP_MAX, attr, NULL); |
c1d10adb | 791 | |
cd91566e FW |
792 | rcu_read_lock(); |
793 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); | |
c1d10adb | 794 | |
f73e924c PM |
795 | if (likely(l3proto->nlattr_to_tuple)) { |
796 | ret = nla_validate_nested(attr, CTA_IP_MAX, | |
797 | l3proto->nla_policy); | |
798 | if (ret == 0) | |
799 | ret = l3proto->nlattr_to_tuple(tb, tuple); | |
800 | } | |
c1d10adb | 801 | |
cd91566e | 802 | rcu_read_unlock(); |
c1d10adb | 803 | |
c1d10adb PNA |
804 | return ret; |
805 | } | |
806 | ||
f73e924c PM |
807 | static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = { |
808 | [CTA_PROTO_NUM] = { .type = NLA_U8 }, | |
c1d10adb PNA |
809 | }; |
810 | ||
811 | static inline int | |
df6fb868 | 812 | ctnetlink_parse_tuple_proto(struct nlattr *attr, |
c1d10adb PNA |
813 | struct nf_conntrack_tuple *tuple) |
814 | { | |
df6fb868 | 815 | struct nlattr *tb[CTA_PROTO_MAX+1]; |
605dcad6 | 816 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
817 | int ret = 0; |
818 | ||
f73e924c PM |
819 | ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy); |
820 | if (ret < 0) | |
821 | return ret; | |
c1d10adb | 822 | |
df6fb868 | 823 | if (!tb[CTA_PROTO_NUM]) |
c1d10adb | 824 | return -EINVAL; |
77236b6e | 825 | tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]); |
c1d10adb | 826 | |
cd91566e FW |
827 | rcu_read_lock(); |
828 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); | |
c1d10adb | 829 | |
f73e924c PM |
830 | if (likely(l4proto->nlattr_to_tuple)) { |
831 | ret = nla_validate_nested(attr, CTA_PROTO_MAX, | |
832 | l4proto->nla_policy); | |
833 | if (ret == 0) | |
834 | ret = l4proto->nlattr_to_tuple(tb, tuple); | |
835 | } | |
c1d10adb | 836 | |
cd91566e | 837 | rcu_read_unlock(); |
601e68e1 | 838 | |
c1d10adb PNA |
839 | return ret; |
840 | } | |
841 | ||
d0b0268f PM |
842 | static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = { |
843 | [CTA_TUPLE_IP] = { .type = NLA_NESTED }, | |
844 | [CTA_TUPLE_PROTO] = { .type = NLA_NESTED }, | |
845 | }; | |
846 | ||
bb5cf80e | 847 | static int |
39938324 PM |
848 | ctnetlink_parse_tuple(const struct nlattr * const cda[], |
849 | struct nf_conntrack_tuple *tuple, | |
a00f1f36 | 850 | enum ctattr_type type, u_int8_t l3num) |
c1d10adb | 851 | { |
df6fb868 | 852 | struct nlattr *tb[CTA_TUPLE_MAX+1]; |
c1d10adb PNA |
853 | int err; |
854 | ||
c1d10adb PNA |
855 | memset(tuple, 0, sizeof(*tuple)); |
856 | ||
d0b0268f | 857 | nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy); |
c1d10adb | 858 | |
df6fb868 | 859 | if (!tb[CTA_TUPLE_IP]) |
c1d10adb PNA |
860 | return -EINVAL; |
861 | ||
862 | tuple->src.l3num = l3num; | |
863 | ||
df6fb868 | 864 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple); |
c1d10adb PNA |
865 | if (err < 0) |
866 | return err; | |
867 | ||
df6fb868 | 868 | if (!tb[CTA_TUPLE_PROTO]) |
c1d10adb PNA |
869 | return -EINVAL; |
870 | ||
df6fb868 | 871 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple); |
c1d10adb PNA |
872 | if (err < 0) |
873 | return err; | |
874 | ||
875 | /* orig and expect tuples get DIR_ORIGINAL */ | |
876 | if (type == CTA_TUPLE_REPLY) | |
877 | tuple->dst.dir = IP_CT_DIR_REPLY; | |
878 | else | |
879 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
880 | ||
c1d10adb PNA |
881 | return 0; |
882 | } | |
883 | ||
ef00f89f PM |
884 | static int |
885 | ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone) | |
886 | { | |
887 | if (attr) | |
888 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
889 | *zone = ntohs(nla_get_be16(attr)); | |
890 | #else | |
891 | return -EOPNOTSUPP; | |
892 | #endif | |
893 | else | |
894 | *zone = 0; | |
895 | ||
896 | return 0; | |
897 | } | |
898 | ||
d0b0268f PM |
899 | static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = { |
900 | [CTA_HELP_NAME] = { .type = NLA_NUL_STRING }, | |
901 | }; | |
902 | ||
c1d10adb | 903 | static inline int |
39938324 | 904 | ctnetlink_parse_help(const struct nlattr *attr, char **helper_name) |
c1d10adb | 905 | { |
df6fb868 | 906 | struct nlattr *tb[CTA_HELP_MAX+1]; |
c1d10adb | 907 | |
d0b0268f | 908 | nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy); |
c1d10adb | 909 | |
df6fb868 | 910 | if (!tb[CTA_HELP_NAME]) |
c1d10adb PNA |
911 | return -EINVAL; |
912 | ||
df6fb868 | 913 | *helper_name = nla_data(tb[CTA_HELP_NAME]); |
c1d10adb PNA |
914 | |
915 | return 0; | |
916 | } | |
917 | ||
f73e924c | 918 | static const struct nla_policy ct_nla_policy[CTA_MAX+1] = { |
d0b0268f PM |
919 | [CTA_TUPLE_ORIG] = { .type = NLA_NESTED }, |
920 | [CTA_TUPLE_REPLY] = { .type = NLA_NESTED }, | |
f73e924c | 921 | [CTA_STATUS] = { .type = NLA_U32 }, |
d0b0268f PM |
922 | [CTA_PROTOINFO] = { .type = NLA_NESTED }, |
923 | [CTA_HELP] = { .type = NLA_NESTED }, | |
924 | [CTA_NAT_SRC] = { .type = NLA_NESTED }, | |
f73e924c PM |
925 | [CTA_TIMEOUT] = { .type = NLA_U32 }, |
926 | [CTA_MARK] = { .type = NLA_U32 }, | |
f73e924c | 927 | [CTA_ID] = { .type = NLA_U32 }, |
d0b0268f PM |
928 | [CTA_NAT_DST] = { .type = NLA_NESTED }, |
929 | [CTA_TUPLE_MASTER] = { .type = NLA_NESTED }, | |
ef00f89f | 930 | [CTA_ZONE] = { .type = NLA_U16 }, |
0f298a28 | 931 | [CTA_MARK_MASK] = { .type = NLA_U32 }, |
c1d10adb PNA |
932 | }; |
933 | ||
934 | static int | |
601e68e1 | 935 | ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
936 | const struct nlmsghdr *nlh, |
937 | const struct nlattr * const cda[]) | |
c1d10adb | 938 | { |
9592a5c0 | 939 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
940 | struct nf_conntrack_tuple_hash *h; |
941 | struct nf_conntrack_tuple tuple; | |
942 | struct nf_conn *ct; | |
96bcf938 | 943 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 944 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
945 | u16 zone; |
946 | int err; | |
947 | ||
948 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); | |
949 | if (err < 0) | |
950 | return err; | |
c1d10adb | 951 | |
df6fb868 | 952 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 953 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 954 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
955 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
956 | else { | |
957 | /* Flush the whole table */ | |
9592a5c0 | 958 | nf_conntrack_flush_report(net, |
274d383b PNA |
959 | NETLINK_CB(skb).pid, |
960 | nlmsg_report(nlh)); | |
c1d10adb PNA |
961 | return 0; |
962 | } | |
963 | ||
964 | if (err < 0) | |
965 | return err; | |
966 | ||
ef00f89f | 967 | h = nf_conntrack_find_get(net, zone, &tuple); |
9ea8cfd6 | 968 | if (!h) |
c1d10adb | 969 | return -ENOENT; |
c1d10adb PNA |
970 | |
971 | ct = nf_ct_tuplehash_to_ctrack(h); | |
601e68e1 | 972 | |
df6fb868 | 973 | if (cda[CTA_ID]) { |
77236b6e | 974 | u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID])); |
7f85f914 | 975 | if (id != (u32)(unsigned long)ct) { |
c1d10adb PNA |
976 | nf_ct_put(ct); |
977 | return -ENOENT; | |
978 | } | |
601e68e1 | 979 | } |
c1d10adb | 980 | |
a16a1647 PNA |
981 | if (del_timer(&ct->timeout)) { |
982 | if (nf_conntrack_event_report(IPCT_DESTROY, ct, | |
983 | NETLINK_CB(skb).pid, | |
984 | nlmsg_report(nlh)) < 0) { | |
985 | nf_ct_delete_from_lists(ct); | |
986 | /* we failed to report the event, try later */ | |
987 | nf_ct_insert_dying_list(ct); | |
988 | nf_ct_put(ct); | |
989 | return 0; | |
990 | } | |
991 | /* death_by_timeout would report the event again */ | |
992 | set_bit(IPS_DYING_BIT, &ct->status); | |
dd7669a9 | 993 | nf_ct_delete_from_lists(ct); |
dd7669a9 | 994 | nf_ct_put(ct); |
dd7669a9 | 995 | } |
c1d10adb | 996 | nf_ct_put(ct); |
c1d10adb PNA |
997 | |
998 | return 0; | |
999 | } | |
1000 | ||
1001 | static int | |
601e68e1 | 1002 | ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
1003 | const struct nlmsghdr *nlh, |
1004 | const struct nlattr * const cda[]) | |
c1d10adb | 1005 | { |
9592a5c0 | 1006 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1007 | struct nf_conntrack_tuple_hash *h; |
1008 | struct nf_conntrack_tuple tuple; | |
1009 | struct nf_conn *ct; | |
1010 | struct sk_buff *skb2 = NULL; | |
96bcf938 | 1011 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 1012 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
1013 | u16 zone; |
1014 | int err; | |
c1d10adb | 1015 | |
80d326fa PNA |
1016 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
1017 | struct netlink_dump_control c = { | |
1018 | .dump = ctnetlink_dump_table, | |
1019 | .done = ctnetlink_done, | |
1020 | }; | |
0f298a28 PNA |
1021 | #ifdef CONFIG_NF_CONNTRACK_MARK |
1022 | if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) { | |
1023 | struct ctnetlink_dump_filter *filter; | |
1024 | ||
1025 | filter = kzalloc(sizeof(struct ctnetlink_dump_filter), | |
1026 | GFP_ATOMIC); | |
1027 | if (filter == NULL) | |
1028 | return -ENOMEM; | |
1029 | ||
1030 | filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK])); | |
1031 | filter->mark.mask = | |
1032 | ntohl(nla_get_be32(cda[CTA_MARK_MASK])); | |
1033 | c.data = filter; | |
1034 | } | |
1035 | #endif | |
80d326fa PNA |
1036 | return netlink_dump_start(ctnl, skb, nlh, &c); |
1037 | } | |
c1d10adb | 1038 | |
ef00f89f PM |
1039 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); |
1040 | if (err < 0) | |
1041 | return err; | |
1042 | ||
df6fb868 | 1043 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 1044 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 1045 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
1046 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
1047 | else | |
1048 | return -EINVAL; | |
1049 | ||
1050 | if (err < 0) | |
1051 | return err; | |
1052 | ||
ef00f89f | 1053 | h = nf_conntrack_find_get(net, zone, &tuple); |
9ea8cfd6 | 1054 | if (!h) |
c1d10adb | 1055 | return -ENOENT; |
9ea8cfd6 | 1056 | |
c1d10adb PNA |
1057 | ct = nf_ct_tuplehash_to_ctrack(h); |
1058 | ||
1059 | err = -ENOMEM; | |
96bcf938 PNA |
1060 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1061 | if (skb2 == NULL) { | |
c1d10adb PNA |
1062 | nf_ct_put(ct); |
1063 | return -ENOMEM; | |
1064 | } | |
c1d10adb | 1065 | |
528a3a6f | 1066 | rcu_read_lock(); |
601e68e1 | 1067 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, |
80e60e67 | 1068 | NFNL_MSG_TYPE(nlh->nlmsg_type), ct); |
528a3a6f | 1069 | rcu_read_unlock(); |
c1d10adb PNA |
1070 | nf_ct_put(ct); |
1071 | if (err <= 0) | |
1072 | goto free; | |
1073 | ||
1074 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
1075 | if (err < 0) | |
1076 | goto out; | |
1077 | ||
c1d10adb PNA |
1078 | return 0; |
1079 | ||
1080 | free: | |
1081 | kfree_skb(skb2); | |
1082 | out: | |
f31e8d49 PNA |
1083 | /* this avoids a loop in nfnetlink. */ |
1084 | return err == -EAGAIN ? -ENOBUFS : err; | |
c1d10adb PNA |
1085 | } |
1086 | ||
67671841 | 1087 | #ifdef CONFIG_NF_NAT_NEEDED |
e6a7d3c0 PNA |
1088 | static int |
1089 | ctnetlink_parse_nat_setup(struct nf_conn *ct, | |
1090 | enum nf_nat_manip_type manip, | |
39938324 | 1091 | const struct nlattr *attr) |
e6a7d3c0 PNA |
1092 | { |
1093 | typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup; | |
1094 | ||
1095 | parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook); | |
1096 | if (!parse_nat_setup) { | |
95a5afca | 1097 | #ifdef CONFIG_MODULES |
e6a7d3c0 PNA |
1098 | rcu_read_unlock(); |
1099 | nfnl_unlock(); | |
1100 | if (request_module("nf-nat-ipv4") < 0) { | |
1101 | nfnl_lock(); | |
1102 | rcu_read_lock(); | |
1103 | return -EOPNOTSUPP; | |
1104 | } | |
1105 | nfnl_lock(); | |
1106 | rcu_read_lock(); | |
1107 | if (nfnetlink_parse_nat_setup_hook) | |
1108 | return -EAGAIN; | |
1109 | #endif | |
1110 | return -EOPNOTSUPP; | |
1111 | } | |
1112 | ||
1113 | return parse_nat_setup(ct, manip, attr); | |
1114 | } | |
67671841 | 1115 | #endif |
e6a7d3c0 | 1116 | |
bb5cf80e | 1117 | static int |
39938324 | 1118 | ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
1119 | { |
1120 | unsigned long d; | |
77236b6e | 1121 | unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS])); |
c1d10adb PNA |
1122 | d = ct->status ^ status; |
1123 | ||
1124 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) | |
1125 | /* unchangeable */ | |
0adf9d67 | 1126 | return -EBUSY; |
601e68e1 | 1127 | |
c1d10adb PNA |
1128 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) |
1129 | /* SEEN_REPLY bit can only be set */ | |
0adf9d67 | 1130 | return -EBUSY; |
601e68e1 | 1131 | |
c1d10adb PNA |
1132 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) |
1133 | /* ASSURED bit can only be set */ | |
0adf9d67 | 1134 | return -EBUSY; |
c1d10adb | 1135 | |
c1d10adb PNA |
1136 | /* Be careful here, modifying NAT bits can screw up things, |
1137 | * so don't let users modify them directly if they don't pass | |
5b1158e9 | 1138 | * nf_nat_range. */ |
c1d10adb PNA |
1139 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); |
1140 | return 0; | |
1141 | } | |
1142 | ||
e6a7d3c0 | 1143 | static int |
39938324 | 1144 | ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[]) |
e6a7d3c0 PNA |
1145 | { |
1146 | #ifdef CONFIG_NF_NAT_NEEDED | |
1147 | int ret; | |
1148 | ||
1149 | if (cda[CTA_NAT_DST]) { | |
1150 | ret = ctnetlink_parse_nat_setup(ct, | |
cbc9f2f4 | 1151 | NF_NAT_MANIP_DST, |
e6a7d3c0 PNA |
1152 | cda[CTA_NAT_DST]); |
1153 | if (ret < 0) | |
1154 | return ret; | |
1155 | } | |
1156 | if (cda[CTA_NAT_SRC]) { | |
1157 | ret = ctnetlink_parse_nat_setup(ct, | |
cbc9f2f4 | 1158 | NF_NAT_MANIP_SRC, |
e6a7d3c0 PNA |
1159 | cda[CTA_NAT_SRC]); |
1160 | if (ret < 0) | |
1161 | return ret; | |
1162 | } | |
1163 | return 0; | |
1164 | #else | |
1165 | return -EOPNOTSUPP; | |
1166 | #endif | |
1167 | } | |
c1d10adb PNA |
1168 | |
1169 | static inline int | |
39938324 | 1170 | ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
1171 | { |
1172 | struct nf_conntrack_helper *helper; | |
dc808fe2 | 1173 | struct nf_conn_help *help = nfct_help(ct); |
29fe1b48 | 1174 | char *helpname = NULL; |
c1d10adb PNA |
1175 | int err; |
1176 | ||
c1d10adb PNA |
1177 | /* don't change helper of sibling connections */ |
1178 | if (ct->master) | |
0adf9d67 | 1179 | return -EBUSY; |
c1d10adb | 1180 | |
df6fb868 | 1181 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); |
c1d10adb PNA |
1182 | if (err < 0) |
1183 | return err; | |
1184 | ||
df293bbb YK |
1185 | if (!strcmp(helpname, "")) { |
1186 | if (help && help->helper) { | |
c1d10adb PNA |
1187 | /* we had a helper before ... */ |
1188 | nf_ct_remove_expectations(ct); | |
a9b3cd7f | 1189 | RCU_INIT_POINTER(help->helper, NULL); |
c1d10adb | 1190 | } |
df293bbb YK |
1191 | |
1192 | return 0; | |
c1d10adb | 1193 | } |
601e68e1 | 1194 | |
794e6871 PM |
1195 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1196 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1197 | if (helper == NULL) { |
1198 | #ifdef CONFIG_MODULES | |
1199 | spin_unlock_bh(&nf_conntrack_lock); | |
1200 | ||
1201 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1202 | spin_lock_bh(&nf_conntrack_lock); | |
1203 | return -EOPNOTSUPP; | |
1204 | } | |
1205 | ||
1206 | spin_lock_bh(&nf_conntrack_lock); | |
794e6871 PM |
1207 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1208 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1209 | if (helper) |
1210 | return -EAGAIN; | |
1211 | #endif | |
0adf9d67 | 1212 | return -EOPNOTSUPP; |
226c0c0e | 1213 | } |
df293bbb | 1214 | |
ceceae1b YK |
1215 | if (help) { |
1216 | if (help->helper == helper) | |
1217 | return 0; | |
1218 | if (help->helper) | |
1219 | return -EBUSY; | |
1220 | /* need to zero data of old helper */ | |
1afc5679 | 1221 | memset(help->data, 0, help->helper->data_len); |
ceceae1b | 1222 | } else { |
a88e22ad PNA |
1223 | /* we cannot set a helper for an existing conntrack */ |
1224 | return -EOPNOTSUPP; | |
ceceae1b | 1225 | } |
df293bbb | 1226 | |
cf778b00 | 1227 | rcu_assign_pointer(help->helper, helper); |
c1d10adb PNA |
1228 | |
1229 | return 0; | |
1230 | } | |
1231 | ||
1232 | static inline int | |
39938324 | 1233 | ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb | 1234 | { |
77236b6e | 1235 | u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
601e68e1 | 1236 | |
c1d10adb PNA |
1237 | if (!del_timer(&ct->timeout)) |
1238 | return -ETIME; | |
1239 | ||
1240 | ct->timeout.expires = jiffies + timeout * HZ; | |
1241 | add_timer(&ct->timeout); | |
1242 | ||
1243 | return 0; | |
1244 | } | |
1245 | ||
d0b0268f PM |
1246 | static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = { |
1247 | [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED }, | |
1248 | [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED }, | |
1249 | [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED }, | |
1250 | }; | |
1251 | ||
c1d10adb | 1252 | static inline int |
39938324 | 1253 | ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb | 1254 | { |
39938324 PM |
1255 | const struct nlattr *attr = cda[CTA_PROTOINFO]; |
1256 | struct nlattr *tb[CTA_PROTOINFO_MAX+1]; | |
605dcad6 | 1257 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
1258 | int err = 0; |
1259 | ||
d0b0268f | 1260 | nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy); |
c1d10adb | 1261 | |
cd91566e FW |
1262 | rcu_read_lock(); |
1263 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
fdf70832 PM |
1264 | if (l4proto->from_nlattr) |
1265 | err = l4proto->from_nlattr(tb, ct); | |
cd91566e | 1266 | rcu_read_unlock(); |
c1d10adb PNA |
1267 | |
1268 | return err; | |
1269 | } | |
1270 | ||
13eae15a | 1271 | #ifdef CONFIG_NF_NAT_NEEDED |
d0b0268f PM |
1272 | static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = { |
1273 | [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 }, | |
1274 | [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 }, | |
1275 | [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 }, | |
1276 | }; | |
1277 | ||
13eae15a | 1278 | static inline int |
39938324 | 1279 | change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr) |
13eae15a PNA |
1280 | { |
1281 | struct nlattr *cda[CTA_NAT_SEQ_MAX+1]; | |
1282 | ||
d0b0268f | 1283 | nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy); |
13eae15a PNA |
1284 | |
1285 | if (!cda[CTA_NAT_SEQ_CORRECTION_POS]) | |
1286 | return -EINVAL; | |
1287 | ||
1288 | natseq->correction_pos = | |
77236b6e | 1289 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS])); |
13eae15a PNA |
1290 | |
1291 | if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE]) | |
1292 | return -EINVAL; | |
1293 | ||
1294 | natseq->offset_before = | |
77236b6e | 1295 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE])); |
13eae15a PNA |
1296 | |
1297 | if (!cda[CTA_NAT_SEQ_OFFSET_AFTER]) | |
1298 | return -EINVAL; | |
1299 | ||
1300 | natseq->offset_after = | |
77236b6e | 1301 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER])); |
13eae15a PNA |
1302 | |
1303 | return 0; | |
1304 | } | |
1305 | ||
1306 | static int | |
39938324 PM |
1307 | ctnetlink_change_nat_seq_adj(struct nf_conn *ct, |
1308 | const struct nlattr * const cda[]) | |
13eae15a PNA |
1309 | { |
1310 | int ret = 0; | |
1311 | struct nf_conn_nat *nat = nfct_nat(ct); | |
1312 | ||
1313 | if (!nat) | |
1314 | return 0; | |
1315 | ||
1316 | if (cda[CTA_NAT_SEQ_ADJ_ORIG]) { | |
1317 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL], | |
1318 | cda[CTA_NAT_SEQ_ADJ_ORIG]); | |
1319 | if (ret < 0) | |
1320 | return ret; | |
1321 | ||
1322 | ct->status |= IPS_SEQ_ADJUST; | |
1323 | } | |
1324 | ||
1325 | if (cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1326 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY], | |
1327 | cda[CTA_NAT_SEQ_ADJ_REPLY]); | |
1328 | if (ret < 0) | |
1329 | return ret; | |
1330 | ||
1331 | ct->status |= IPS_SEQ_ADJUST; | |
1332 | } | |
1333 | ||
1334 | return 0; | |
1335 | } | |
1336 | #endif | |
1337 | ||
c1d10adb | 1338 | static int |
39938324 PM |
1339 | ctnetlink_change_conntrack(struct nf_conn *ct, |
1340 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1341 | { |
1342 | int err; | |
1343 | ||
e098360f PNA |
1344 | /* only allow NAT changes and master assignation for new conntracks */ |
1345 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER]) | |
1346 | return -EOPNOTSUPP; | |
1347 | ||
df6fb868 | 1348 | if (cda[CTA_HELP]) { |
c1d10adb PNA |
1349 | err = ctnetlink_change_helper(ct, cda); |
1350 | if (err < 0) | |
1351 | return err; | |
1352 | } | |
1353 | ||
df6fb868 | 1354 | if (cda[CTA_TIMEOUT]) { |
c1d10adb PNA |
1355 | err = ctnetlink_change_timeout(ct, cda); |
1356 | if (err < 0) | |
1357 | return err; | |
1358 | } | |
1359 | ||
df6fb868 | 1360 | if (cda[CTA_STATUS]) { |
c1d10adb PNA |
1361 | err = ctnetlink_change_status(ct, cda); |
1362 | if (err < 0) | |
1363 | return err; | |
1364 | } | |
1365 | ||
df6fb868 | 1366 | if (cda[CTA_PROTOINFO]) { |
c1d10adb PNA |
1367 | err = ctnetlink_change_protoinfo(ct, cda); |
1368 | if (err < 0) | |
1369 | return err; | |
1370 | } | |
1371 | ||
bcd1e830 | 1372 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1373 | if (cda[CTA_MARK]) |
77236b6e | 1374 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1375 | #endif |
1376 | ||
13eae15a PNA |
1377 | #ifdef CONFIG_NF_NAT_NEEDED |
1378 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1379 | err = ctnetlink_change_nat_seq_adj(ct, cda); | |
1380 | if (err < 0) | |
1381 | return err; | |
1382 | } | |
1383 | #endif | |
1384 | ||
c1d10adb PNA |
1385 | return 0; |
1386 | } | |
1387 | ||
f0a3c086 | 1388 | static struct nf_conn * |
ef00f89f | 1389 | ctnetlink_create_conntrack(struct net *net, u16 zone, |
9592a5c0 | 1390 | const struct nlattr * const cda[], |
c1d10adb | 1391 | struct nf_conntrack_tuple *otuple, |
5faa1f4c | 1392 | struct nf_conntrack_tuple *rtuple, |
7ec47496 | 1393 | u8 u3) |
c1d10adb PNA |
1394 | { |
1395 | struct nf_conn *ct; | |
1396 | int err = -EINVAL; | |
ceceae1b | 1397 | struct nf_conntrack_helper *helper; |
315c34da | 1398 | struct nf_conn_tstamp *tstamp; |
c1d10adb | 1399 | |
ef00f89f | 1400 | ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC); |
cd7fcbf1 | 1401 | if (IS_ERR(ct)) |
f0a3c086 | 1402 | return ERR_PTR(-ENOMEM); |
c1d10adb | 1403 | |
df6fb868 | 1404 | if (!cda[CTA_TIMEOUT]) |
0f5b3e85 | 1405 | goto err1; |
77236b6e | 1406 | ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
c1d10adb PNA |
1407 | |
1408 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; | |
c1d10adb | 1409 | |
1575e7ea | 1410 | rcu_read_lock(); |
226c0c0e | 1411 | if (cda[CTA_HELP]) { |
29fe1b48 | 1412 | char *helpname = NULL; |
226c0c0e PNA |
1413 | |
1414 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); | |
0f5b3e85 PM |
1415 | if (err < 0) |
1416 | goto err2; | |
226c0c0e | 1417 | |
794e6871 PM |
1418 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1419 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1420 | if (helper == NULL) { |
1421 | rcu_read_unlock(); | |
1422 | #ifdef CONFIG_MODULES | |
1423 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1424 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1425 | goto err1; |
226c0c0e PNA |
1426 | } |
1427 | ||
1428 | rcu_read_lock(); | |
794e6871 PM |
1429 | helper = __nf_conntrack_helper_find(helpname, |
1430 | nf_ct_l3num(ct), | |
1431 | nf_ct_protonum(ct)); | |
226c0c0e | 1432 | if (helper) { |
226c0c0e | 1433 | err = -EAGAIN; |
0f5b3e85 | 1434 | goto err2; |
226c0c0e PNA |
1435 | } |
1436 | rcu_read_unlock(); | |
1437 | #endif | |
1438 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1439 | goto err1; |
226c0c0e PNA |
1440 | } else { |
1441 | struct nf_conn_help *help; | |
1442 | ||
1afc5679 | 1443 | help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC); |
226c0c0e | 1444 | if (help == NULL) { |
226c0c0e | 1445 | err = -ENOMEM; |
0f5b3e85 | 1446 | goto err2; |
226c0c0e PNA |
1447 | } |
1448 | ||
1449 | /* not in hash table yet so not strictly necessary */ | |
a9b3cd7f | 1450 | RCU_INIT_POINTER(help->helper, helper); |
226c0c0e PNA |
1451 | } |
1452 | } else { | |
1453 | /* try an implicit helper assignation */ | |
b2a15a60 | 1454 | err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC); |
0f5b3e85 PM |
1455 | if (err < 0) |
1456 | goto err2; | |
1575e7ea PNA |
1457 | } |
1458 | ||
a88e22ad PNA |
1459 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) { |
1460 | err = ctnetlink_change_nat(ct, cda); | |
0f5b3e85 PM |
1461 | if (err < 0) |
1462 | goto err2; | |
e6a7d3c0 PNA |
1463 | } |
1464 | ||
a88e22ad | 1465 | nf_ct_acct_ext_add(ct, GFP_ATOMIC); |
a992ca2a | 1466 | nf_ct_tstamp_ext_add(ct, GFP_ATOMIC); |
a88e22ad PNA |
1467 | nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC); |
1468 | /* we must add conntrack extensions before confirmation. */ | |
1469 | ct->status |= IPS_CONFIRMED; | |
1470 | ||
1471 | if (cda[CTA_STATUS]) { | |
1472 | err = ctnetlink_change_status(ct, cda); | |
0f5b3e85 PM |
1473 | if (err < 0) |
1474 | goto err2; | |
bbb3357d | 1475 | } |
c1d10adb | 1476 | |
c969aa7d PNA |
1477 | #ifdef CONFIG_NF_NAT_NEEDED |
1478 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1479 | err = ctnetlink_change_nat_seq_adj(ct, cda); | |
0f5b3e85 PM |
1480 | if (err < 0) |
1481 | goto err2; | |
c969aa7d PNA |
1482 | } |
1483 | #endif | |
1484 | ||
e5fc9e7a | 1485 | memset(&ct->proto, 0, sizeof(ct->proto)); |
df6fb868 | 1486 | if (cda[CTA_PROTOINFO]) { |
c1d10adb | 1487 | err = ctnetlink_change_protoinfo(ct, cda); |
0f5b3e85 PM |
1488 | if (err < 0) |
1489 | goto err2; | |
c1d10adb PNA |
1490 | } |
1491 | ||
bcd1e830 | 1492 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1493 | if (cda[CTA_MARK]) |
77236b6e | 1494 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1495 | #endif |
1496 | ||
5faa1f4c | 1497 | /* setup master conntrack: this is a confirmed expectation */ |
7ec47496 PNA |
1498 | if (cda[CTA_TUPLE_MASTER]) { |
1499 | struct nf_conntrack_tuple master; | |
1500 | struct nf_conntrack_tuple_hash *master_h; | |
1501 | struct nf_conn *master_ct; | |
1502 | ||
1503 | err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3); | |
1504 | if (err < 0) | |
0f5b3e85 | 1505 | goto err2; |
7ec47496 | 1506 | |
ef00f89f | 1507 | master_h = nf_conntrack_find_get(net, zone, &master); |
7ec47496 PNA |
1508 | if (master_h == NULL) { |
1509 | err = -ENOENT; | |
0f5b3e85 | 1510 | goto err2; |
7ec47496 PNA |
1511 | } |
1512 | master_ct = nf_ct_tuplehash_to_ctrack(master_h); | |
f2a89004 | 1513 | __set_bit(IPS_EXPECTED_BIT, &ct->status); |
5faa1f4c | 1514 | ct->master = master_ct; |
f2a89004 | 1515 | } |
315c34da PNA |
1516 | tstamp = nf_conn_tstamp_find(ct); |
1517 | if (tstamp) | |
1518 | tstamp->start = ktime_to_ns(ktime_get_real()); | |
5faa1f4c | 1519 | |
7d367e06 JK |
1520 | err = nf_conntrack_hash_check_insert(ct); |
1521 | if (err < 0) | |
1522 | goto err2; | |
1523 | ||
58a3c9bb | 1524 | rcu_read_unlock(); |
dafc741c | 1525 | |
f0a3c086 | 1526 | return ct; |
c1d10adb | 1527 | |
0f5b3e85 PM |
1528 | err2: |
1529 | rcu_read_unlock(); | |
1530 | err1: | |
c1d10adb | 1531 | nf_conntrack_free(ct); |
f0a3c086 | 1532 | return ERR_PTR(err); |
c1d10adb PNA |
1533 | } |
1534 | ||
601e68e1 YH |
1535 | static int |
1536 | ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
1537 | const struct nlmsghdr *nlh, |
1538 | const struct nlattr * const cda[]) | |
c1d10adb | 1539 | { |
9592a5c0 | 1540 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1541 | struct nf_conntrack_tuple otuple, rtuple; |
1542 | struct nf_conntrack_tuple_hash *h = NULL; | |
96bcf938 | 1543 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
7d367e06 | 1544 | struct nf_conn *ct; |
c1d10adb | 1545 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
1546 | u16 zone; |
1547 | int err; | |
1548 | ||
1549 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); | |
1550 | if (err < 0) | |
1551 | return err; | |
c1d10adb | 1552 | |
df6fb868 | 1553 | if (cda[CTA_TUPLE_ORIG]) { |
c1d10adb PNA |
1554 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3); |
1555 | if (err < 0) | |
1556 | return err; | |
1557 | } | |
1558 | ||
df6fb868 | 1559 | if (cda[CTA_TUPLE_REPLY]) { |
c1d10adb PNA |
1560 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3); |
1561 | if (err < 0) | |
1562 | return err; | |
1563 | } | |
1564 | ||
df6fb868 | 1565 | if (cda[CTA_TUPLE_ORIG]) |
7d367e06 | 1566 | h = nf_conntrack_find_get(net, zone, &otuple); |
df6fb868 | 1567 | else if (cda[CTA_TUPLE_REPLY]) |
7d367e06 | 1568 | h = nf_conntrack_find_get(net, zone, &rtuple); |
c1d10adb PNA |
1569 | |
1570 | if (h == NULL) { | |
c1d10adb | 1571 | err = -ENOENT; |
f0a3c086 | 1572 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
fecc1133 | 1573 | enum ip_conntrack_events events; |
5faa1f4c | 1574 | |
ef00f89f | 1575 | ct = ctnetlink_create_conntrack(net, zone, cda, &otuple, |
f0a3c086 | 1576 | &rtuple, u3); |
7d367e06 JK |
1577 | if (IS_ERR(ct)) |
1578 | return PTR_ERR(ct); | |
1579 | ||
f0a3c086 | 1580 | err = 0; |
fecc1133 PNA |
1581 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) |
1582 | events = IPCT_RELATED; | |
1583 | else | |
1584 | events = IPCT_NEW; | |
1585 | ||
858b3133 PM |
1586 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1587 | (1 << IPCT_ASSURED) | | |
a0891aa6 PNA |
1588 | (1 << IPCT_HELPER) | |
1589 | (1 << IPCT_PROTOINFO) | | |
1590 | (1 << IPCT_NATSEQADJ) | | |
1591 | (1 << IPCT_MARK) | events, | |
1592 | ct, NETLINK_CB(skb).pid, | |
1593 | nlmsg_report(nlh)); | |
f0a3c086 | 1594 | nf_ct_put(ct); |
7d367e06 | 1595 | } |
5faa1f4c | 1596 | |
c1d10adb PNA |
1597 | return err; |
1598 | } | |
1599 | /* implicit 'else' */ | |
1600 | ||
c1d10adb | 1601 | err = -EEXIST; |
7d367e06 | 1602 | ct = nf_ct_tuplehash_to_ctrack(h); |
ff4ca827 | 1603 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) { |
7d367e06 | 1604 | spin_lock_bh(&nf_conntrack_lock); |
19abb7b0 | 1605 | err = ctnetlink_change_conntrack(ct, cda); |
7d367e06 | 1606 | spin_unlock_bh(&nf_conntrack_lock); |
19abb7b0 | 1607 | if (err == 0) { |
858b3133 PM |
1608 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1609 | (1 << IPCT_ASSURED) | | |
a0891aa6 PNA |
1610 | (1 << IPCT_HELPER) | |
1611 | (1 << IPCT_PROTOINFO) | | |
1612 | (1 << IPCT_NATSEQADJ) | | |
1613 | (1 << IPCT_MARK), | |
1614 | ct, NETLINK_CB(skb).pid, | |
1615 | nlmsg_report(nlh)); | |
7d367e06 | 1616 | } |
ff4ca827 | 1617 | } |
c1d10adb | 1618 | |
7d367e06 | 1619 | nf_ct_put(ct); |
c1d10adb PNA |
1620 | return err; |
1621 | } | |
1622 | ||
9cb01766 PNA |
1623 | #if defined(CONFIG_NETFILTER_NETLINK_QUEUE) || \ |
1624 | defined(CONFIG_NETFILTER_NETLINK_QUEUE_MODULE) | |
1625 | static size_t | |
1626 | ctnetlink_nfqueue_build_size(const struct nf_conn *ct) | |
1627 | { | |
1628 | return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ | |
1629 | + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ | |
1630 | + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ | |
1631 | + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ | |
1632 | + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ | |
1633 | + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ | |
1634 | + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ | |
1635 | + nla_total_size(0) /* CTA_PROTOINFO */ | |
1636 | + nla_total_size(0) /* CTA_HELP */ | |
1637 | + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ | |
1638 | + ctnetlink_secctx_size(ct) | |
1639 | #ifdef CONFIG_NF_NAT_NEEDED | |
1640 | + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ | |
1641 | + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ | |
1642 | #endif | |
1643 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
1644 | + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ | |
1645 | #endif | |
1646 | + ctnetlink_proto_size(ct) | |
1647 | ; | |
1648 | } | |
1649 | ||
1650 | static int | |
1651 | ctnetlink_nfqueue_build(struct sk_buff *skb, struct nf_conn *ct) | |
1652 | { | |
1653 | struct nlattr *nest_parms; | |
1654 | ||
1655 | rcu_read_lock(); | |
1656 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); | |
1657 | if (!nest_parms) | |
1658 | goto nla_put_failure; | |
1659 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) | |
1660 | goto nla_put_failure; | |
1661 | nla_nest_end(skb, nest_parms); | |
1662 | ||
1663 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); | |
1664 | if (!nest_parms) | |
1665 | goto nla_put_failure; | |
1666 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) | |
1667 | goto nla_put_failure; | |
1668 | nla_nest_end(skb, nest_parms); | |
1669 | ||
1670 | if (nf_ct_zone(ct)) { | |
1671 | if (nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct)))) | |
1672 | goto nla_put_failure; | |
1673 | } | |
1674 | ||
1675 | if (ctnetlink_dump_id(skb, ct) < 0) | |
1676 | goto nla_put_failure; | |
1677 | ||
1678 | if (ctnetlink_dump_status(skb, ct) < 0) | |
1679 | goto nla_put_failure; | |
1680 | ||
1681 | if (ctnetlink_dump_timeout(skb, ct) < 0) | |
1682 | goto nla_put_failure; | |
1683 | ||
1684 | if (ctnetlink_dump_protoinfo(skb, ct) < 0) | |
1685 | goto nla_put_failure; | |
1686 | ||
1687 | if (ctnetlink_dump_helpinfo(skb, ct) < 0) | |
1688 | goto nla_put_failure; | |
1689 | ||
1690 | #ifdef CONFIG_NF_CONNTRACK_SECMARK | |
1691 | if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0) | |
1692 | goto nla_put_failure; | |
1693 | #endif | |
1694 | if (ct->master && ctnetlink_dump_master(skb, ct) < 0) | |
1695 | goto nla_put_failure; | |
1696 | ||
1697 | if ((ct->status & IPS_SEQ_ADJUST) && | |
1698 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) | |
1699 | goto nla_put_failure; | |
1700 | ||
1701 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
1702 | if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0) | |
1703 | goto nla_put_failure; | |
1704 | #endif | |
1705 | rcu_read_unlock(); | |
1706 | return 0; | |
1707 | ||
1708 | nla_put_failure: | |
1709 | rcu_read_unlock(); | |
1710 | return -ENOSPC; | |
1711 | } | |
1712 | ||
1713 | static int | |
1714 | ctnetlink_nfqueue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct) | |
1715 | { | |
1716 | int err; | |
1717 | ||
1718 | if (cda[CTA_TIMEOUT]) { | |
1719 | err = ctnetlink_change_timeout(ct, cda); | |
1720 | if (err < 0) | |
1721 | return err; | |
1722 | } | |
1723 | if (cda[CTA_STATUS]) { | |
1724 | err = ctnetlink_change_status(ct, cda); | |
1725 | if (err < 0) | |
1726 | return err; | |
1727 | } | |
1728 | if (cda[CTA_HELP]) { | |
1729 | err = ctnetlink_change_helper(ct, cda); | |
1730 | if (err < 0) | |
1731 | return err; | |
1732 | } | |
1733 | #if defined(CONFIG_NF_CONNTRACK_MARK) | |
1734 | if (cda[CTA_MARK]) | |
1735 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); | |
1736 | #endif | |
1737 | return 0; | |
1738 | } | |
1739 | ||
1740 | static int | |
1741 | ctnetlink_nfqueue_parse(const struct nlattr *attr, struct nf_conn *ct) | |
1742 | { | |
1743 | struct nlattr *cda[CTA_MAX+1]; | |
1744 | ||
1745 | nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy); | |
1746 | ||
1747 | return ctnetlink_nfqueue_parse_ct((const struct nlattr **)cda, ct); | |
1748 | } | |
1749 | ||
1750 | static struct nfq_ct_hook ctnetlink_nfqueue_hook = { | |
1751 | .build_size = ctnetlink_nfqueue_build_size, | |
1752 | .build = ctnetlink_nfqueue_build, | |
1753 | .parse = ctnetlink_nfqueue_parse, | |
1754 | }; | |
1755 | #endif /* CONFIG_NETFILTER_NETLINK_QUEUE */ | |
1756 | ||
601e68e1 YH |
1757 | /*********************************************************************** |
1758 | * EXPECT | |
1759 | ***********************************************************************/ | |
c1d10adb PNA |
1760 | |
1761 | static inline int | |
1762 | ctnetlink_exp_dump_tuple(struct sk_buff *skb, | |
1763 | const struct nf_conntrack_tuple *tuple, | |
1764 | enum ctattr_expect type) | |
1765 | { | |
df6fb868 | 1766 | struct nlattr *nest_parms; |
601e68e1 | 1767 | |
df6fb868 PM |
1768 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
1769 | if (!nest_parms) | |
1770 | goto nla_put_failure; | |
c1d10adb | 1771 | if (ctnetlink_dump_tuples(skb, tuple) < 0) |
df6fb868 PM |
1772 | goto nla_put_failure; |
1773 | nla_nest_end(skb, nest_parms); | |
c1d10adb PNA |
1774 | |
1775 | return 0; | |
1776 | ||
df6fb868 | 1777 | nla_put_failure: |
c1d10adb | 1778 | return -1; |
601e68e1 | 1779 | } |
c1d10adb | 1780 | |
1cde6436 PNA |
1781 | static inline int |
1782 | ctnetlink_exp_dump_mask(struct sk_buff *skb, | |
1783 | const struct nf_conntrack_tuple *tuple, | |
d4156e8c | 1784 | const struct nf_conntrack_tuple_mask *mask) |
1cde6436 PNA |
1785 | { |
1786 | int ret; | |
1787 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 1788 | struct nf_conntrack_l4proto *l4proto; |
d4156e8c | 1789 | struct nf_conntrack_tuple m; |
df6fb868 | 1790 | struct nlattr *nest_parms; |
d4156e8c PM |
1791 | |
1792 | memset(&m, 0xFF, sizeof(m)); | |
d4156e8c | 1793 | memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3)); |
e578756c PM |
1794 | m.src.u.all = mask->src.u.all; |
1795 | m.dst.protonum = tuple->dst.protonum; | |
d4156e8c | 1796 | |
df6fb868 PM |
1797 | nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED); |
1798 | if (!nest_parms) | |
1799 | goto nla_put_failure; | |
1cde6436 | 1800 | |
3b988ece | 1801 | rcu_read_lock(); |
528a3a6f | 1802 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
d4156e8c | 1803 | ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto); |
3b988ece HS |
1804 | if (ret >= 0) { |
1805 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, | |
1806 | tuple->dst.protonum); | |
d4156e8c | 1807 | ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto); |
3b988ece HS |
1808 | } |
1809 | rcu_read_unlock(); | |
1810 | ||
1cde6436 | 1811 | if (unlikely(ret < 0)) |
df6fb868 | 1812 | goto nla_put_failure; |
1cde6436 | 1813 | |
df6fb868 | 1814 | nla_nest_end(skb, nest_parms); |
1cde6436 PNA |
1815 | |
1816 | return 0; | |
1817 | ||
df6fb868 | 1818 | nla_put_failure: |
1cde6436 PNA |
1819 | return -1; |
1820 | } | |
1821 | ||
bb5cf80e | 1822 | static int |
c1d10adb | 1823 | ctnetlink_exp_dump_expect(struct sk_buff *skb, |
601e68e1 | 1824 | const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
1825 | { |
1826 | struct nf_conn *master = exp->master; | |
c1216382 | 1827 | long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ; |
bc01befd | 1828 | struct nf_conn_help *help; |
076a0ca0 PNA |
1829 | #ifdef CONFIG_NF_NAT_NEEDED |
1830 | struct nlattr *nest_parms; | |
1831 | struct nf_conntrack_tuple nat_tuple = {}; | |
1832 | #endif | |
544d5c7d PNA |
1833 | struct nf_ct_helper_expectfn *expfn; |
1834 | ||
d978e5da PM |
1835 | if (timeout < 0) |
1836 | timeout = 0; | |
c1d10adb PNA |
1837 | |
1838 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) | |
df6fb868 | 1839 | goto nla_put_failure; |
1cde6436 | 1840 | if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) |
df6fb868 | 1841 | goto nla_put_failure; |
c1d10adb PNA |
1842 | if (ctnetlink_exp_dump_tuple(skb, |
1843 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
1844 | CTA_EXPECT_MASTER) < 0) | |
df6fb868 | 1845 | goto nla_put_failure; |
601e68e1 | 1846 | |
076a0ca0 PNA |
1847 | #ifdef CONFIG_NF_NAT_NEEDED |
1848 | if (exp->saved_ip || exp->saved_proto.all) { | |
1849 | nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED); | |
1850 | if (!nest_parms) | |
1851 | goto nla_put_failure; | |
1852 | ||
cc1eb431 DM |
1853 | if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir))) |
1854 | goto nla_put_failure; | |
076a0ca0 PNA |
1855 | |
1856 | nat_tuple.src.l3num = nf_ct_l3num(master); | |
1857 | nat_tuple.src.u3.ip = exp->saved_ip; | |
1858 | nat_tuple.dst.protonum = nf_ct_protonum(master); | |
1859 | nat_tuple.src.u = exp->saved_proto; | |
1860 | ||
1861 | if (ctnetlink_exp_dump_tuple(skb, &nat_tuple, | |
1862 | CTA_EXPECT_NAT_TUPLE) < 0) | |
1863 | goto nla_put_failure; | |
1864 | nla_nest_end(skb, nest_parms); | |
1865 | } | |
1866 | #endif | |
cc1eb431 DM |
1867 | if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) || |
1868 | nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) || | |
1869 | nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) || | |
1870 | nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class))) | |
1871 | goto nla_put_failure; | |
bc01befd PNA |
1872 | help = nfct_help(master); |
1873 | if (help) { | |
1874 | struct nf_conntrack_helper *helper; | |
1875 | ||
1876 | helper = rcu_dereference(help->helper); | |
cc1eb431 DM |
1877 | if (helper && |
1878 | nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name)) | |
1879 | goto nla_put_failure; | |
bc01befd | 1880 | } |
544d5c7d | 1881 | expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn); |
cc1eb431 DM |
1882 | if (expfn != NULL && |
1883 | nla_put_string(skb, CTA_EXPECT_FN, expfn->name)) | |
1884 | goto nla_put_failure; | |
c1d10adb PNA |
1885 | |
1886 | return 0; | |
601e68e1 | 1887 | |
df6fb868 | 1888 | nla_put_failure: |
c1d10adb PNA |
1889 | return -1; |
1890 | } | |
1891 | ||
1892 | static int | |
1893 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq, | |
8b0a231d | 1894 | int event, const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
1895 | { |
1896 | struct nlmsghdr *nlh; | |
1897 | struct nfgenmsg *nfmsg; | |
96bcf938 | 1898 | unsigned int flags = pid ? NLM_F_MULTI : 0; |
c1d10adb PNA |
1899 | |
1900 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; | |
96bcf938 PNA |
1901 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags); |
1902 | if (nlh == NULL) | |
1903 | goto nlmsg_failure; | |
c1d10adb | 1904 | |
96bcf938 | 1905 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
1906 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
1907 | nfmsg->version = NFNETLINK_V0; | |
1908 | nfmsg->res_id = 0; | |
1909 | ||
1910 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) | |
df6fb868 | 1911 | goto nla_put_failure; |
c1d10adb | 1912 | |
96bcf938 | 1913 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
1914 | return skb->len; |
1915 | ||
1916 | nlmsg_failure: | |
df6fb868 | 1917 | nla_put_failure: |
96bcf938 | 1918 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
1919 | return -1; |
1920 | } | |
1921 | ||
1922 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
e34d5c1a PNA |
1923 | static int |
1924 | ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item) | |
c1d10adb | 1925 | { |
9592a5c0 AD |
1926 | struct nf_conntrack_expect *exp = item->exp; |
1927 | struct net *net = nf_ct_exp_net(exp); | |
c1d10adb PNA |
1928 | struct nlmsghdr *nlh; |
1929 | struct nfgenmsg *nfmsg; | |
c1d10adb | 1930 | struct sk_buff *skb; |
ebbf41df | 1931 | unsigned int type, group; |
c1d10adb PNA |
1932 | int flags = 0; |
1933 | ||
ebbf41df PNA |
1934 | if (events & (1 << IPEXP_DESTROY)) { |
1935 | type = IPCTNL_MSG_EXP_DELETE; | |
1936 | group = NFNLGRP_CONNTRACK_EXP_DESTROY; | |
1937 | } else if (events & (1 << IPEXP_NEW)) { | |
c1d10adb PNA |
1938 | type = IPCTNL_MSG_EXP_NEW; |
1939 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
ebbf41df | 1940 | group = NFNLGRP_CONNTRACK_EXP_NEW; |
c1d10adb | 1941 | } else |
e34d5c1a | 1942 | return 0; |
c1d10adb | 1943 | |
ebbf41df | 1944 | if (!item->report && !nfnetlink_has_listeners(net, group)) |
e34d5c1a | 1945 | return 0; |
b3a27bfb | 1946 | |
96bcf938 PNA |
1947 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
1948 | if (skb == NULL) | |
150ace0d | 1949 | goto errout; |
c1d10adb | 1950 | |
b633ad5f | 1951 | type |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
96bcf938 PNA |
1952 | nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags); |
1953 | if (nlh == NULL) | |
1954 | goto nlmsg_failure; | |
c1d10adb | 1955 | |
96bcf938 | 1956 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
1957 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
1958 | nfmsg->version = NFNETLINK_V0; | |
1959 | nfmsg->res_id = 0; | |
1960 | ||
528a3a6f | 1961 | rcu_read_lock(); |
c1d10adb | 1962 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
df6fb868 | 1963 | goto nla_put_failure; |
528a3a6f | 1964 | rcu_read_unlock(); |
c1d10adb | 1965 | |
96bcf938 | 1966 | nlmsg_end(skb, nlh); |
ebbf41df | 1967 | nfnetlink_send(skb, net, item->pid, group, item->report, GFP_ATOMIC); |
e34d5c1a | 1968 | return 0; |
c1d10adb | 1969 | |
df6fb868 | 1970 | nla_put_failure: |
528a3a6f | 1971 | rcu_read_unlock(); |
96bcf938 | 1972 | nlmsg_cancel(skb, nlh); |
528a3a6f | 1973 | nlmsg_failure: |
c1d10adb | 1974 | kfree_skb(skb); |
150ace0d | 1975 | errout: |
9592a5c0 | 1976 | nfnetlink_set_err(net, 0, 0, -ENOBUFS); |
e34d5c1a | 1977 | return 0; |
c1d10adb PNA |
1978 | } |
1979 | #endif | |
cf6994c2 PM |
1980 | static int ctnetlink_exp_done(struct netlink_callback *cb) |
1981 | { | |
31f15875 PM |
1982 | if (cb->args[1]) |
1983 | nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]); | |
cf6994c2 PM |
1984 | return 0; |
1985 | } | |
c1d10adb PNA |
1986 | |
1987 | static int | |
1988 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
1989 | { | |
9592a5c0 | 1990 | struct net *net = sock_net(skb->sk); |
cf6994c2 | 1991 | struct nf_conntrack_expect *exp, *last; |
96bcf938 | 1992 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
31f15875 | 1993 | struct hlist_node *n; |
87711cb8 | 1994 | u_int8_t l3proto = nfmsg->nfgen_family; |
c1d10adb | 1995 | |
7d0742da | 1996 | rcu_read_lock(); |
31f15875 PM |
1997 | last = (struct nf_conntrack_expect *)cb->args[1]; |
1998 | for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) { | |
cf6994c2 | 1999 | restart: |
9b03f38d | 2000 | hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]], |
31f15875 PM |
2001 | hnode) { |
2002 | if (l3proto && exp->tuple.src.l3num != l3proto) | |
cf6994c2 | 2003 | continue; |
31f15875 PM |
2004 | if (cb->args[1]) { |
2005 | if (exp != last) | |
2006 | continue; | |
2007 | cb->args[1] = 0; | |
2008 | } | |
8b0a231d PNA |
2009 | if (ctnetlink_exp_fill_info(skb, |
2010 | NETLINK_CB(cb->skb).pid, | |
31f15875 PM |
2011 | cb->nlh->nlmsg_seq, |
2012 | IPCTNL_MSG_EXP_NEW, | |
8b0a231d | 2013 | exp) < 0) { |
7d0742da PM |
2014 | if (!atomic_inc_not_zero(&exp->use)) |
2015 | continue; | |
31f15875 PM |
2016 | cb->args[1] = (unsigned long)exp; |
2017 | goto out; | |
2018 | } | |
cf6994c2 | 2019 | } |
31f15875 PM |
2020 | if (cb->args[1]) { |
2021 | cb->args[1] = 0; | |
2022 | goto restart; | |
cf6994c2 PM |
2023 | } |
2024 | } | |
601e68e1 | 2025 | out: |
7d0742da | 2026 | rcu_read_unlock(); |
cf6994c2 PM |
2027 | if (last) |
2028 | nf_ct_expect_put(last); | |
c1d10adb | 2029 | |
c1d10adb PNA |
2030 | return skb->len; |
2031 | } | |
2032 | ||
f73e924c | 2033 | static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = { |
d0b0268f PM |
2034 | [CTA_EXPECT_MASTER] = { .type = NLA_NESTED }, |
2035 | [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED }, | |
2036 | [CTA_EXPECT_MASK] = { .type = NLA_NESTED }, | |
f73e924c PM |
2037 | [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 }, |
2038 | [CTA_EXPECT_ID] = { .type = NLA_U32 }, | |
d0b0268f | 2039 | [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING }, |
bcac0dfa | 2040 | [CTA_EXPECT_ZONE] = { .type = NLA_U16 }, |
8b008faf | 2041 | [CTA_EXPECT_FLAGS] = { .type = NLA_U32 }, |
b8c5e52c | 2042 | [CTA_EXPECT_CLASS] = { .type = NLA_U32 }, |
076a0ca0 | 2043 | [CTA_EXPECT_NAT] = { .type = NLA_NESTED }, |
544d5c7d | 2044 | [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING }, |
c1d10adb PNA |
2045 | }; |
2046 | ||
2047 | static int | |
601e68e1 | 2048 | ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
2049 | const struct nlmsghdr *nlh, |
2050 | const struct nlattr * const cda[]) | |
c1d10adb | 2051 | { |
9592a5c0 | 2052 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
2053 | struct nf_conntrack_tuple tuple; |
2054 | struct nf_conntrack_expect *exp; | |
2055 | struct sk_buff *skb2; | |
96bcf938 | 2056 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 2057 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
2058 | u16 zone; |
2059 | int err; | |
c1d10adb | 2060 | |
b8f3ab42 | 2061 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
80d326fa PNA |
2062 | struct netlink_dump_control c = { |
2063 | .dump = ctnetlink_exp_dump_table, | |
2064 | .done = ctnetlink_exp_done, | |
2065 | }; | |
2066 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
c1d10adb PNA |
2067 | } |
2068 | ||
ef00f89f PM |
2069 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2070 | if (err < 0) | |
2071 | return err; | |
2072 | ||
35dba1d7 PNA |
2073 | if (cda[CTA_EXPECT_TUPLE]) |
2074 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
2075 | else if (cda[CTA_EXPECT_MASTER]) | |
c1d10adb PNA |
2076 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3); |
2077 | else | |
2078 | return -EINVAL; | |
2079 | ||
2080 | if (err < 0) | |
2081 | return err; | |
2082 | ||
ef00f89f | 2083 | exp = nf_ct_expect_find_get(net, zone, &tuple); |
c1d10adb PNA |
2084 | if (!exp) |
2085 | return -ENOENT; | |
2086 | ||
df6fb868 | 2087 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 2088 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 2089 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 2090 | nf_ct_expect_put(exp); |
c1d10adb PNA |
2091 | return -ENOENT; |
2092 | } | |
601e68e1 | 2093 | } |
c1d10adb PNA |
2094 | |
2095 | err = -ENOMEM; | |
96bcf938 | 2096 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
81378f72 PNA |
2097 | if (skb2 == NULL) { |
2098 | nf_ct_expect_put(exp); | |
c1d10adb | 2099 | goto out; |
81378f72 | 2100 | } |
4e9b8269 | 2101 | |
528a3a6f | 2102 | rcu_read_lock(); |
601e68e1 | 2103 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, |
8b0a231d | 2104 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp); |
528a3a6f | 2105 | rcu_read_unlock(); |
81378f72 | 2106 | nf_ct_expect_put(exp); |
c1d10adb PNA |
2107 | if (err <= 0) |
2108 | goto free; | |
2109 | ||
81378f72 PNA |
2110 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); |
2111 | if (err < 0) | |
2112 | goto out; | |
c1d10adb | 2113 | |
81378f72 | 2114 | return 0; |
c1d10adb PNA |
2115 | |
2116 | free: | |
2117 | kfree_skb(skb2); | |
2118 | out: | |
81378f72 PNA |
2119 | /* this avoids a loop in nfnetlink. */ |
2120 | return err == -EAGAIN ? -ENOBUFS : err; | |
c1d10adb PNA |
2121 | } |
2122 | ||
2123 | static int | |
601e68e1 | 2124 | ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
2125 | const struct nlmsghdr *nlh, |
2126 | const struct nlattr * const cda[]) | |
c1d10adb | 2127 | { |
9592a5c0 | 2128 | struct net *net = sock_net(ctnl); |
31f15875 | 2129 | struct nf_conntrack_expect *exp; |
c1d10adb | 2130 | struct nf_conntrack_tuple tuple; |
96bcf938 | 2131 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
31f15875 | 2132 | struct hlist_node *n, *next; |
c1d10adb | 2133 | u_int8_t u3 = nfmsg->nfgen_family; |
31f15875 | 2134 | unsigned int i; |
ef00f89f | 2135 | u16 zone; |
c1d10adb PNA |
2136 | int err; |
2137 | ||
df6fb868 | 2138 | if (cda[CTA_EXPECT_TUPLE]) { |
c1d10adb | 2139 | /* delete a single expect by tuple */ |
ef00f89f PM |
2140 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2141 | if (err < 0) | |
2142 | return err; | |
2143 | ||
c1d10adb PNA |
2144 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
2145 | if (err < 0) | |
2146 | return err; | |
2147 | ||
2148 | /* bump usage count to 2 */ | |
ef00f89f | 2149 | exp = nf_ct_expect_find_get(net, zone, &tuple); |
c1d10adb PNA |
2150 | if (!exp) |
2151 | return -ENOENT; | |
2152 | ||
df6fb868 | 2153 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 2154 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 2155 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 2156 | nf_ct_expect_put(exp); |
c1d10adb PNA |
2157 | return -ENOENT; |
2158 | } | |
2159 | } | |
2160 | ||
2161 | /* after list removal, usage count == 1 */ | |
ebbf41df PNA |
2162 | spin_lock_bh(&nf_conntrack_lock); |
2163 | if (del_timer(&exp->timeout)) { | |
2164 | nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).pid, | |
2165 | nlmsg_report(nlh)); | |
2166 | nf_ct_expect_put(exp); | |
2167 | } | |
2168 | spin_unlock_bh(&nf_conntrack_lock); | |
601e68e1 | 2169 | /* have to put what we 'get' above. |
c1d10adb | 2170 | * after this line usage count == 0 */ |
6823645d | 2171 | nf_ct_expect_put(exp); |
df6fb868 PM |
2172 | } else if (cda[CTA_EXPECT_HELP_NAME]) { |
2173 | char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
31f15875 | 2174 | struct nf_conn_help *m_help; |
c1d10adb PNA |
2175 | |
2176 | /* delete all expectations for this helper */ | |
f8ba1aff | 2177 | spin_lock_bh(&nf_conntrack_lock); |
31f15875 PM |
2178 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
2179 | hlist_for_each_entry_safe(exp, n, next, | |
9592a5c0 | 2180 | &net->ct.expect_hash[i], |
31f15875 PM |
2181 | hnode) { |
2182 | m_help = nfct_help(exp->master); | |
794e6871 PM |
2183 | if (!strcmp(m_help->helper->name, name) && |
2184 | del_timer(&exp->timeout)) { | |
ebbf41df PNA |
2185 | nf_ct_unlink_expect_report(exp, |
2186 | NETLINK_CB(skb).pid, | |
2187 | nlmsg_report(nlh)); | |
31f15875 PM |
2188 | nf_ct_expect_put(exp); |
2189 | } | |
c1d10adb PNA |
2190 | } |
2191 | } | |
f8ba1aff | 2192 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
2193 | } else { |
2194 | /* This basically means we have to flush everything*/ | |
f8ba1aff | 2195 | spin_lock_bh(&nf_conntrack_lock); |
31f15875 PM |
2196 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
2197 | hlist_for_each_entry_safe(exp, n, next, | |
9592a5c0 | 2198 | &net->ct.expect_hash[i], |
31f15875 PM |
2199 | hnode) { |
2200 | if (del_timer(&exp->timeout)) { | |
ebbf41df PNA |
2201 | nf_ct_unlink_expect_report(exp, |
2202 | NETLINK_CB(skb).pid, | |
2203 | nlmsg_report(nlh)); | |
31f15875 PM |
2204 | nf_ct_expect_put(exp); |
2205 | } | |
c1d10adb PNA |
2206 | } |
2207 | } | |
f8ba1aff | 2208 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
2209 | } |
2210 | ||
2211 | return 0; | |
2212 | } | |
2213 | static int | |
39938324 PM |
2214 | ctnetlink_change_expect(struct nf_conntrack_expect *x, |
2215 | const struct nlattr * const cda[]) | |
c1d10adb | 2216 | { |
9768e1ac KW |
2217 | if (cda[CTA_EXPECT_TIMEOUT]) { |
2218 | if (!del_timer(&x->timeout)) | |
2219 | return -ETIME; | |
2220 | ||
2221 | x->timeout.expires = jiffies + | |
2222 | ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ; | |
2223 | add_timer(&x->timeout); | |
2224 | } | |
2225 | return 0; | |
c1d10adb PNA |
2226 | } |
2227 | ||
076a0ca0 PNA |
2228 | static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = { |
2229 | [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 }, | |
2230 | [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED }, | |
2231 | }; | |
2232 | ||
2233 | static int | |
2234 | ctnetlink_parse_expect_nat(const struct nlattr *attr, | |
2235 | struct nf_conntrack_expect *exp, | |
2236 | u_int8_t u3) | |
2237 | { | |
2238 | #ifdef CONFIG_NF_NAT_NEEDED | |
2239 | struct nlattr *tb[CTA_EXPECT_NAT_MAX+1]; | |
2240 | struct nf_conntrack_tuple nat_tuple = {}; | |
2241 | int err; | |
2242 | ||
2243 | nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy); | |
2244 | ||
2245 | if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE]) | |
2246 | return -EINVAL; | |
2247 | ||
2248 | err = ctnetlink_parse_tuple((const struct nlattr * const *)tb, | |
2249 | &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3); | |
2250 | if (err < 0) | |
2251 | return err; | |
2252 | ||
2253 | exp->saved_ip = nat_tuple.src.u3.ip; | |
2254 | exp->saved_proto = nat_tuple.src.u; | |
2255 | exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR])); | |
2256 | ||
2257 | return 0; | |
2258 | #else | |
2259 | return -EOPNOTSUPP; | |
2260 | #endif | |
2261 | } | |
2262 | ||
c1d10adb | 2263 | static int |
ef00f89f PM |
2264 | ctnetlink_create_expect(struct net *net, u16 zone, |
2265 | const struct nlattr * const cda[], | |
9592a5c0 | 2266 | u_int8_t u3, |
39938324 | 2267 | u32 pid, int report) |
c1d10adb PNA |
2268 | { |
2269 | struct nf_conntrack_tuple tuple, mask, master_tuple; | |
2270 | struct nf_conntrack_tuple_hash *h = NULL; | |
2271 | struct nf_conntrack_expect *exp; | |
2272 | struct nf_conn *ct; | |
dc808fe2 | 2273 | struct nf_conn_help *help; |
660fdb2a | 2274 | struct nf_conntrack_helper *helper = NULL; |
b8c5e52c | 2275 | u_int32_t class = 0; |
c1d10adb PNA |
2276 | int err = 0; |
2277 | ||
c1d10adb PNA |
2278 | /* caller guarantees that those three CTA_EXPECT_* exist */ |
2279 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
2280 | if (err < 0) | |
2281 | return err; | |
2282 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3); | |
2283 | if (err < 0) | |
2284 | return err; | |
2285 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3); | |
2286 | if (err < 0) | |
2287 | return err; | |
2288 | ||
2289 | /* Look for master conntrack of this expectation */ | |
ef00f89f | 2290 | h = nf_conntrack_find_get(net, zone, &master_tuple); |
c1d10adb PNA |
2291 | if (!h) |
2292 | return -ENOENT; | |
2293 | ct = nf_ct_tuplehash_to_ctrack(h); | |
660fdb2a PNA |
2294 | |
2295 | /* Look for helper of this expectation */ | |
2296 | if (cda[CTA_EXPECT_HELP_NAME]) { | |
2297 | const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
2298 | ||
2299 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), | |
2300 | nf_ct_protonum(ct)); | |
2301 | if (helper == NULL) { | |
2302 | #ifdef CONFIG_MODULES | |
2303 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
2304 | err = -EOPNOTSUPP; | |
2305 | goto out; | |
2306 | } | |
2307 | ||
2308 | helper = __nf_conntrack_helper_find(helpname, | |
2309 | nf_ct_l3num(ct), | |
2310 | nf_ct_protonum(ct)); | |
2311 | if (helper) { | |
2312 | err = -EAGAIN; | |
2313 | goto out; | |
2314 | } | |
2315 | #endif | |
2316 | err = -EOPNOTSUPP; | |
2317 | goto out; | |
2318 | } | |
2319 | } | |
2320 | ||
b8c5e52c PNA |
2321 | if (cda[CTA_EXPECT_CLASS] && helper) { |
2322 | class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS])); | |
2323 | if (class > helper->expect_class_max) { | |
2324 | err = -EINVAL; | |
2325 | goto out; | |
2326 | } | |
2327 | } | |
6823645d | 2328 | exp = nf_ct_expect_alloc(ct); |
c1d10adb PNA |
2329 | if (!exp) { |
2330 | err = -ENOMEM; | |
2331 | goto out; | |
2332 | } | |
bc01befd PNA |
2333 | help = nfct_help(ct); |
2334 | if (!help) { | |
2335 | if (!cda[CTA_EXPECT_TIMEOUT]) { | |
2336 | err = -EINVAL; | |
2337 | goto out; | |
2338 | } | |
2339 | exp->timeout.expires = | |
2340 | jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ; | |
601e68e1 | 2341 | |
bc01befd PNA |
2342 | exp->flags = NF_CT_EXPECT_USERSPACE; |
2343 | if (cda[CTA_EXPECT_FLAGS]) { | |
2344 | exp->flags |= | |
2345 | ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); | |
2346 | } | |
2347 | } else { | |
2348 | if (cda[CTA_EXPECT_FLAGS]) { | |
2349 | exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); | |
2350 | exp->flags &= ~NF_CT_EXPECT_USERSPACE; | |
2351 | } else | |
2352 | exp->flags = 0; | |
2353 | } | |
544d5c7d PNA |
2354 | if (cda[CTA_EXPECT_FN]) { |
2355 | const char *name = nla_data(cda[CTA_EXPECT_FN]); | |
2356 | struct nf_ct_helper_expectfn *expfn; | |
2357 | ||
2358 | expfn = nf_ct_helper_expectfn_find_by_name(name); | |
2359 | if (expfn == NULL) { | |
2360 | err = -EINVAL; | |
2361 | goto err_out; | |
2362 | } | |
2363 | exp->expectfn = expfn->expectfn; | |
2364 | } else | |
2365 | exp->expectfn = NULL; | |
601e68e1 | 2366 | |
b8c5e52c | 2367 | exp->class = class; |
c1d10adb | 2368 | exp->master = ct; |
660fdb2a | 2369 | exp->helper = helper; |
c1d10adb | 2370 | memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple)); |
d4156e8c PM |
2371 | memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3)); |
2372 | exp->mask.src.u.all = mask.src.u.all; | |
c1d10adb | 2373 | |
076a0ca0 PNA |
2374 | if (cda[CTA_EXPECT_NAT]) { |
2375 | err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT], | |
2376 | exp, u3); | |
2377 | if (err < 0) | |
2378 | goto err_out; | |
2379 | } | |
19abb7b0 | 2380 | err = nf_ct_expect_related_report(exp, pid, report); |
076a0ca0 | 2381 | err_out: |
6823645d | 2382 | nf_ct_expect_put(exp); |
601e68e1 | 2383 | out: |
c1d10adb PNA |
2384 | nf_ct_put(nf_ct_tuplehash_to_ctrack(h)); |
2385 | return err; | |
2386 | } | |
2387 | ||
2388 | static int | |
2389 | ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
2390 | const struct nlmsghdr *nlh, |
2391 | const struct nlattr * const cda[]) | |
c1d10adb | 2392 | { |
9592a5c0 | 2393 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
2394 | struct nf_conntrack_tuple tuple; |
2395 | struct nf_conntrack_expect *exp; | |
96bcf938 | 2396 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 2397 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
2398 | u16 zone; |
2399 | int err; | |
c1d10adb | 2400 | |
df6fb868 PM |
2401 | if (!cda[CTA_EXPECT_TUPLE] |
2402 | || !cda[CTA_EXPECT_MASK] | |
2403 | || !cda[CTA_EXPECT_MASTER]) | |
c1d10adb PNA |
2404 | return -EINVAL; |
2405 | ||
ef00f89f PM |
2406 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2407 | if (err < 0) | |
2408 | return err; | |
2409 | ||
c1d10adb PNA |
2410 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
2411 | if (err < 0) | |
2412 | return err; | |
2413 | ||
f8ba1aff | 2414 | spin_lock_bh(&nf_conntrack_lock); |
ef00f89f | 2415 | exp = __nf_ct_expect_find(net, zone, &tuple); |
c1d10adb PNA |
2416 | |
2417 | if (!exp) { | |
f8ba1aff | 2418 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 2419 | err = -ENOENT; |
19abb7b0 | 2420 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
ef00f89f | 2421 | err = ctnetlink_create_expect(net, zone, cda, |
19abb7b0 PNA |
2422 | u3, |
2423 | NETLINK_CB(skb).pid, | |
2424 | nlmsg_report(nlh)); | |
2425 | } | |
c1d10adb PNA |
2426 | return err; |
2427 | } | |
2428 | ||
2429 | err = -EEXIST; | |
2430 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) | |
2431 | err = ctnetlink_change_expect(exp, cda); | |
f8ba1aff | 2432 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 2433 | |
c1d10adb PNA |
2434 | return err; |
2435 | } | |
2436 | ||
2437 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
e34d5c1a PNA |
2438 | static struct nf_ct_event_notifier ctnl_notifier = { |
2439 | .fcn = ctnetlink_conntrack_event, | |
c1d10adb PNA |
2440 | }; |
2441 | ||
e34d5c1a PNA |
2442 | static struct nf_exp_event_notifier ctnl_notifier_exp = { |
2443 | .fcn = ctnetlink_expect_event, | |
c1d10adb PNA |
2444 | }; |
2445 | #endif | |
2446 | ||
7c8d4cb4 | 2447 | static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { |
c1d10adb | 2448 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, |
f73e924c PM |
2449 | .attr_count = CTA_MAX, |
2450 | .policy = ct_nla_policy }, | |
c1d10adb | 2451 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
2452 | .attr_count = CTA_MAX, |
2453 | .policy = ct_nla_policy }, | |
c1d10adb | 2454 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, |
f73e924c PM |
2455 | .attr_count = CTA_MAX, |
2456 | .policy = ct_nla_policy }, | |
c1d10adb | 2457 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
2458 | .attr_count = CTA_MAX, |
2459 | .policy = ct_nla_policy }, | |
c1d10adb PNA |
2460 | }; |
2461 | ||
7c8d4cb4 | 2462 | static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { |
c1d10adb | 2463 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
f73e924c PM |
2464 | .attr_count = CTA_EXPECT_MAX, |
2465 | .policy = exp_nla_policy }, | |
c1d10adb | 2466 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, |
f73e924c PM |
2467 | .attr_count = CTA_EXPECT_MAX, |
2468 | .policy = exp_nla_policy }, | |
c1d10adb | 2469 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, |
f73e924c PM |
2470 | .attr_count = CTA_EXPECT_MAX, |
2471 | .policy = exp_nla_policy }, | |
c1d10adb PNA |
2472 | }; |
2473 | ||
7c8d4cb4 | 2474 | static const struct nfnetlink_subsystem ctnl_subsys = { |
c1d10adb PNA |
2475 | .name = "conntrack", |
2476 | .subsys_id = NFNL_SUBSYS_CTNETLINK, | |
2477 | .cb_count = IPCTNL_MSG_MAX, | |
2478 | .cb = ctnl_cb, | |
2479 | }; | |
2480 | ||
7c8d4cb4 | 2481 | static const struct nfnetlink_subsystem ctnl_exp_subsys = { |
c1d10adb PNA |
2482 | .name = "conntrack_expect", |
2483 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, | |
2484 | .cb_count = IPCTNL_MSG_EXP_MAX, | |
2485 | .cb = ctnl_exp_cb, | |
2486 | }; | |
2487 | ||
d2483dde | 2488 | MODULE_ALIAS("ip_conntrack_netlink"); |
c1d10adb | 2489 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); |
34f9a2e4 | 2490 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); |
c1d10adb | 2491 | |
70e9942f PNA |
2492 | static int __net_init ctnetlink_net_init(struct net *net) |
2493 | { | |
2494 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2495 | int ret; | |
2496 | ||
2497 | ret = nf_conntrack_register_notifier(net, &ctnl_notifier); | |
2498 | if (ret < 0) { | |
2499 | pr_err("ctnetlink_init: cannot register notifier.\n"); | |
2500 | goto err_out; | |
2501 | } | |
2502 | ||
2503 | ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp); | |
2504 | if (ret < 0) { | |
2505 | pr_err("ctnetlink_init: cannot expect register notifier.\n"); | |
2506 | goto err_unreg_notifier; | |
2507 | } | |
2508 | #endif | |
2509 | return 0; | |
2510 | ||
2511 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2512 | err_unreg_notifier: | |
2513 | nf_conntrack_unregister_notifier(net, &ctnl_notifier); | |
2514 | err_out: | |
2515 | return ret; | |
2516 | #endif | |
2517 | } | |
2518 | ||
2519 | static void ctnetlink_net_exit(struct net *net) | |
2520 | { | |
2521 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2522 | nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp); | |
2523 | nf_conntrack_unregister_notifier(net, &ctnl_notifier); | |
2524 | #endif | |
2525 | } | |
2526 | ||
2527 | static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list) | |
2528 | { | |
2529 | struct net *net; | |
2530 | ||
2531 | list_for_each_entry(net, net_exit_list, exit_list) | |
2532 | ctnetlink_net_exit(net); | |
2533 | } | |
2534 | ||
2535 | static struct pernet_operations ctnetlink_net_ops = { | |
2536 | .init = ctnetlink_net_init, | |
2537 | .exit_batch = ctnetlink_net_exit_batch, | |
2538 | }; | |
2539 | ||
c1d10adb PNA |
2540 | static int __init ctnetlink_init(void) |
2541 | { | |
2542 | int ret; | |
2543 | ||
654d0fbd | 2544 | pr_info("ctnetlink v%s: registering with nfnetlink.\n", version); |
c1d10adb PNA |
2545 | ret = nfnetlink_subsys_register(&ctnl_subsys); |
2546 | if (ret < 0) { | |
654d0fbd | 2547 | pr_err("ctnetlink_init: cannot register with nfnetlink.\n"); |
c1d10adb PNA |
2548 | goto err_out; |
2549 | } | |
2550 | ||
2551 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); | |
2552 | if (ret < 0) { | |
654d0fbd | 2553 | pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n"); |
c1d10adb PNA |
2554 | goto err_unreg_subsys; |
2555 | } | |
2556 | ||
70e9942f PNA |
2557 | if (register_pernet_subsys(&ctnetlink_net_ops)) { |
2558 | pr_err("ctnetlink_init: cannot register pernet operations\n"); | |
c1d10adb PNA |
2559 | goto err_unreg_exp_subsys; |
2560 | } | |
9cb01766 PNA |
2561 | #if defined(CONFIG_NETFILTER_NETLINK_QUEUE) || \ |
2562 | defined(CONFIG_NETFILTER_NETLINK_QUEUE_MODULE) | |
2563 | /* setup interaction between nf_queue and nf_conntrack_netlink. */ | |
2564 | RCU_INIT_POINTER(nfq_ct_hook, &ctnetlink_nfqueue_hook); | |
2565 | #endif | |
c1d10adb PNA |
2566 | return 0; |
2567 | ||
c1d10adb PNA |
2568 | err_unreg_exp_subsys: |
2569 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
c1d10adb PNA |
2570 | err_unreg_subsys: |
2571 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
2572 | err_out: | |
2573 | return ret; | |
2574 | } | |
2575 | ||
2576 | static void __exit ctnetlink_exit(void) | |
2577 | { | |
654d0fbd | 2578 | pr_info("ctnetlink: unregistering from nfnetlink.\n"); |
c1d10adb | 2579 | |
70e9942f | 2580 | unregister_pernet_subsys(&ctnetlink_net_ops); |
c1d10adb PNA |
2581 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); |
2582 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
9cb01766 PNA |
2583 | #if defined(CONFIG_NETFILTER_NETLINK_QUEUE) || \ |
2584 | defined(CONFIG_NETFILTER_NETLINK_QUEUE_MODULE) | |
2585 | RCU_INIT_POINTER(nfq_ct_hook, NULL); | |
2586 | #endif | |
c1d10adb PNA |
2587 | } |
2588 | ||
2589 | module_init(ctnetlink_init); | |
2590 | module_exit(ctnetlink_exit); |