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