]>
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> |
392025f8 | 7 | * (C) 2005-2012 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 | 39 | #include <net/netfilter/nf_conntrack_helper.h> |
41d73ec0 | 40 | #include <net/netfilter/nf_conntrack_seqadj.h> |
c1d10adb | 41 | #include <net/netfilter/nf_conntrack_l3proto.h> |
605dcad6 | 42 | #include <net/netfilter/nf_conntrack_l4proto.h> |
5b1158e9 | 43 | #include <net/netfilter/nf_conntrack_tuple.h> |
58401572 | 44 | #include <net/netfilter/nf_conntrack_acct.h> |
ef00f89f | 45 | #include <net/netfilter/nf_conntrack_zones.h> |
a992ca2a | 46 | #include <net/netfilter/nf_conntrack_timestamp.h> |
c539f017 | 47 | #include <net/netfilter/nf_conntrack_labels.h> |
5b1158e9 JK |
48 | #ifdef CONFIG_NF_NAT_NEEDED |
49 | #include <net/netfilter/nf_nat_core.h> | |
c7232c99 | 50 | #include <net/netfilter/nf_nat_l4proto.h> |
8c88f87c | 51 | #include <net/netfilter/nf_nat_helper.h> |
5b1158e9 | 52 | #endif |
c1d10adb PNA |
53 | |
54 | #include <linux/netfilter/nfnetlink.h> | |
55 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
56 | ||
57 | MODULE_LICENSE("GPL"); | |
58 | ||
dc808fe2 | 59 | static char __initdata version[] = "0.93"; |
c1d10adb | 60 | |
c1d10adb | 61 | static inline int |
601e68e1 | 62 | ctnetlink_dump_tuples_proto(struct sk_buff *skb, |
1cde6436 | 63 | const struct nf_conntrack_tuple *tuple, |
605dcad6 | 64 | struct nf_conntrack_l4proto *l4proto) |
c1d10adb | 65 | { |
c1d10adb | 66 | int ret = 0; |
df6fb868 | 67 | struct nlattr *nest_parms; |
c1d10adb | 68 | |
df6fb868 PM |
69 | nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED); |
70 | if (!nest_parms) | |
71 | goto nla_put_failure; | |
cc1eb431 DM |
72 | if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum)) |
73 | goto nla_put_failure; | |
c1d10adb | 74 | |
fdf70832 PM |
75 | if (likely(l4proto->tuple_to_nlattr)) |
76 | ret = l4proto->tuple_to_nlattr(skb, tuple); | |
601e68e1 | 77 | |
df6fb868 | 78 | nla_nest_end(skb, nest_parms); |
c1d10adb PNA |
79 | |
80 | return ret; | |
81 | ||
df6fb868 | 82 | nla_put_failure: |
c1d10adb PNA |
83 | return -1; |
84 | } | |
85 | ||
86 | static inline int | |
1cde6436 PNA |
87 | ctnetlink_dump_tuples_ip(struct sk_buff *skb, |
88 | const struct nf_conntrack_tuple *tuple, | |
89 | struct nf_conntrack_l3proto *l3proto) | |
c1d10adb | 90 | { |
c1d10adb | 91 | int ret = 0; |
df6fb868 PM |
92 | struct nlattr *nest_parms; |
93 | ||
94 | nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED); | |
95 | if (!nest_parms) | |
96 | goto nla_put_failure; | |
1cde6436 | 97 | |
fdf70832 PM |
98 | if (likely(l3proto->tuple_to_nlattr)) |
99 | ret = l3proto->tuple_to_nlattr(skb, tuple); | |
1cde6436 | 100 | |
df6fb868 | 101 | nla_nest_end(skb, nest_parms); |
c1d10adb | 102 | |
1cde6436 PNA |
103 | return ret; |
104 | ||
df6fb868 | 105 | nla_put_failure: |
1cde6436 PNA |
106 | return -1; |
107 | } | |
108 | ||
bb5cf80e | 109 | static int |
1cde6436 PNA |
110 | ctnetlink_dump_tuples(struct sk_buff *skb, |
111 | const struct nf_conntrack_tuple *tuple) | |
112 | { | |
113 | int ret; | |
114 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 115 | struct nf_conntrack_l4proto *l4proto; |
1cde6436 | 116 | |
3b988ece | 117 | rcu_read_lock(); |
528a3a6f | 118 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
1cde6436 | 119 | ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto); |
c1d10adb | 120 | |
3b988ece HS |
121 | if (ret >= 0) { |
122 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, | |
123 | tuple->dst.protonum); | |
124 | ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto); | |
125 | } | |
126 | rcu_read_unlock(); | |
c1d10adb | 127 | return ret; |
c1d10adb PNA |
128 | } |
129 | ||
130 | static inline int | |
131 | ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct) | |
132 | { | |
cc1eb431 DM |
133 | if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status))) |
134 | goto nla_put_failure; | |
c1d10adb PNA |
135 | return 0; |
136 | ||
df6fb868 | 137 | nla_put_failure: |
c1d10adb PNA |
138 | return -1; |
139 | } | |
140 | ||
141 | static inline int | |
142 | ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct) | |
143 | { | |
c1216382 | 144 | long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ; |
c1d10adb | 145 | |
77236b6e | 146 | if (timeout < 0) |
c1d10adb | 147 | timeout = 0; |
601e68e1 | 148 | |
cc1eb431 DM |
149 | if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout))) |
150 | goto nla_put_failure; | |
c1d10adb PNA |
151 | return 0; |
152 | ||
df6fb868 | 153 | nla_put_failure: |
c1d10adb PNA |
154 | return -1; |
155 | } | |
156 | ||
157 | static inline int | |
440f0d58 | 158 | ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct) |
c1d10adb | 159 | { |
5e8fbe2a | 160 | struct nf_conntrack_l4proto *l4proto; |
df6fb868 | 161 | struct nlattr *nest_proto; |
c1d10adb PNA |
162 | int ret; |
163 | ||
528a3a6f PNA |
164 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
165 | if (!l4proto->to_nlattr) | |
c1d10adb | 166 | return 0; |
601e68e1 | 167 | |
df6fb868 PM |
168 | nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED); |
169 | if (!nest_proto) | |
170 | goto nla_put_failure; | |
c1d10adb | 171 | |
fdf70832 | 172 | ret = l4proto->to_nlattr(skb, nest_proto, ct); |
c1d10adb | 173 | |
df6fb868 | 174 | nla_nest_end(skb, nest_proto); |
c1d10adb PNA |
175 | |
176 | return ret; | |
177 | ||
df6fb868 | 178 | nla_put_failure: |
c1d10adb PNA |
179 | return -1; |
180 | } | |
181 | ||
182 | static inline int | |
183 | ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct) | |
184 | { | |
df6fb868 | 185 | struct nlattr *nest_helper; |
dc808fe2 | 186 | const struct nf_conn_help *help = nfct_help(ct); |
3c158f7f | 187 | struct nf_conntrack_helper *helper; |
c1d10adb | 188 | |
3c158f7f | 189 | if (!help) |
c1d10adb | 190 | return 0; |
601e68e1 | 191 | |
3c158f7f PM |
192 | helper = rcu_dereference(help->helper); |
193 | if (!helper) | |
194 | goto out; | |
195 | ||
df6fb868 PM |
196 | nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED); |
197 | if (!nest_helper) | |
198 | goto nla_put_failure; | |
cc1eb431 DM |
199 | if (nla_put_string(skb, CTA_HELP_NAME, helper->name)) |
200 | goto nla_put_failure; | |
c1d10adb | 201 | |
fdf70832 PM |
202 | if (helper->to_nlattr) |
203 | helper->to_nlattr(skb, ct); | |
c1d10adb | 204 | |
df6fb868 | 205 | nla_nest_end(skb, nest_helper); |
3c158f7f | 206 | out: |
c1d10adb PNA |
207 | return 0; |
208 | ||
df6fb868 | 209 | nla_put_failure: |
c1d10adb PNA |
210 | return -1; |
211 | } | |
212 | ||
bb5cf80e | 213 | static int |
4542fa47 HE |
214 | dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct, |
215 | enum ip_conntrack_dir dir, int type) | |
c1d10adb | 216 | { |
4542fa47 HE |
217 | enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; |
218 | struct nf_conn_counter *counter = acct->counter; | |
df6fb868 | 219 | struct nlattr *nest_count; |
4542fa47 | 220 | u64 pkts, bytes; |
c1d10adb | 221 | |
4542fa47 HE |
222 | if (type == IPCTNL_MSG_CT_GET_CTRZERO) { |
223 | pkts = atomic64_xchg(&counter[dir].packets, 0); | |
224 | bytes = atomic64_xchg(&counter[dir].bytes, 0); | |
225 | } else { | |
226 | pkts = atomic64_read(&counter[dir].packets); | |
227 | bytes = atomic64_read(&counter[dir].bytes); | |
228 | } | |
229 | ||
230 | nest_count = nla_nest_start(skb, attr | NLA_F_NESTED); | |
df6fb868 PM |
231 | if (!nest_count) |
232 | goto nla_put_failure; | |
233 | ||
cc1eb431 DM |
234 | if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts)) || |
235 | nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes))) | |
236 | goto nla_put_failure; | |
c1d10adb | 237 | |
df6fb868 | 238 | nla_nest_end(skb, nest_count); |
c1d10adb PNA |
239 | |
240 | return 0; | |
241 | ||
df6fb868 | 242 | nla_put_failure: |
c1d10adb PNA |
243 | return -1; |
244 | } | |
c1d10adb | 245 | |
80e60e67 | 246 | static int |
4542fa47 | 247 | ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type) |
80e60e67 | 248 | { |
4542fa47 | 249 | struct nf_conn_acct *acct = nf_conn_acct_find(ct); |
80e60e67 | 250 | |
80e60e67 PNA |
251 | if (!acct) |
252 | return 0; | |
253 | ||
4542fa47 HE |
254 | if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0) |
255 | return -1; | |
256 | if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0) | |
257 | return -1; | |
258 | ||
259 | return 0; | |
80e60e67 PNA |
260 | } |
261 | ||
a992ca2a PNA |
262 | static int |
263 | ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct) | |
264 | { | |
265 | struct nlattr *nest_count; | |
266 | const struct nf_conn_tstamp *tstamp; | |
267 | ||
268 | tstamp = nf_conn_tstamp_find(ct); | |
269 | if (!tstamp) | |
270 | return 0; | |
271 | ||
272 | nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED); | |
273 | if (!nest_count) | |
274 | goto nla_put_failure; | |
275 | ||
cc1eb431 DM |
276 | if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start)) || |
277 | (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP, | |
278 | cpu_to_be64(tstamp->stop)))) | |
279 | goto nla_put_failure; | |
a992ca2a PNA |
280 | nla_nest_end(skb, nest_count); |
281 | ||
282 | return 0; | |
283 | ||
284 | nla_put_failure: | |
285 | return -1; | |
286 | } | |
287 | ||
c1d10adb PNA |
288 | #ifdef CONFIG_NF_CONNTRACK_MARK |
289 | static inline int | |
290 | ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct) | |
291 | { | |
cc1eb431 DM |
292 | if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark))) |
293 | goto nla_put_failure; | |
c1d10adb PNA |
294 | return 0; |
295 | ||
df6fb868 | 296 | nla_put_failure: |
c1d10adb PNA |
297 | return -1; |
298 | } | |
299 | #else | |
300 | #define ctnetlink_dump_mark(a, b) (0) | |
301 | #endif | |
302 | ||
37fccd85 PNA |
303 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
304 | static inline int | |
1cc63249 | 305 | ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct) |
37fccd85 | 306 | { |
1cc63249 EP |
307 | struct nlattr *nest_secctx; |
308 | int len, ret; | |
309 | char *secctx; | |
310 | ||
311 | ret = security_secid_to_secctx(ct->secmark, &secctx, &len); | |
312 | if (ret) | |
cba85b53 | 313 | return 0; |
1cc63249 EP |
314 | |
315 | ret = -1; | |
316 | nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED); | |
317 | if (!nest_secctx) | |
318 | goto nla_put_failure; | |
37fccd85 | 319 | |
cc1eb431 DM |
320 | if (nla_put_string(skb, CTA_SECCTX_NAME, secctx)) |
321 | goto nla_put_failure; | |
1cc63249 EP |
322 | nla_nest_end(skb, nest_secctx); |
323 | ||
324 | ret = 0; | |
37fccd85 | 325 | nla_put_failure: |
1cc63249 EP |
326 | security_release_secctx(secctx, len); |
327 | return ret; | |
37fccd85 PNA |
328 | } |
329 | #else | |
1cc63249 | 330 | #define ctnetlink_dump_secctx(a, b) (0) |
37fccd85 PNA |
331 | #endif |
332 | ||
0ceabd83 FW |
333 | #ifdef CONFIG_NF_CONNTRACK_LABELS |
334 | static int ctnetlink_label_size(const struct nf_conn *ct) | |
335 | { | |
336 | struct nf_conn_labels *labels = nf_ct_labels_find(ct); | |
337 | ||
338 | if (!labels) | |
339 | return 0; | |
340 | return nla_total_size(labels->words * sizeof(long)); | |
341 | } | |
342 | ||
343 | static int | |
344 | ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct) | |
345 | { | |
346 | struct nf_conn_labels *labels = nf_ct_labels_find(ct); | |
347 | unsigned int len, i; | |
348 | ||
349 | if (!labels) | |
350 | return 0; | |
351 | ||
352 | len = labels->words * sizeof(long); | |
353 | i = 0; | |
354 | do { | |
355 | if (labels->bits[i] != 0) | |
356 | return nla_put(skb, CTA_LABELS, len, labels->bits); | |
357 | i++; | |
358 | } while (i < labels->words); | |
359 | ||
360 | return 0; | |
361 | } | |
362 | #else | |
363 | #define ctnetlink_dump_labels(a, b) (0) | |
364 | #define ctnetlink_label_size(a) (0) | |
365 | #endif | |
366 | ||
0f417ce9 PNA |
367 | #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple) |
368 | ||
369 | static inline int | |
370 | ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct) | |
371 | { | |
372 | struct nlattr *nest_parms; | |
373 | ||
374 | if (!(ct->status & IPS_EXPECTED)) | |
375 | return 0; | |
376 | ||
377 | nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED); | |
378 | if (!nest_parms) | |
379 | goto nla_put_failure; | |
380 | if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0) | |
381 | goto nla_put_failure; | |
382 | nla_nest_end(skb, nest_parms); | |
383 | ||
384 | return 0; | |
385 | ||
386 | nla_put_failure: | |
387 | return -1; | |
388 | } | |
389 | ||
bb5cf80e | 390 | static int |
41d73ec0 | 391 | dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type) |
13eae15a | 392 | { |
13eae15a PNA |
393 | struct nlattr *nest_parms; |
394 | ||
395 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); | |
396 | if (!nest_parms) | |
397 | goto nla_put_failure; | |
398 | ||
41d73ec0 PM |
399 | if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS, |
400 | htonl(seq->correction_pos)) || | |
401 | nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE, | |
402 | htonl(seq->offset_before)) || | |
403 | nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER, | |
404 | htonl(seq->offset_after))) | |
cc1eb431 | 405 | goto nla_put_failure; |
13eae15a PNA |
406 | |
407 | nla_nest_end(skb, nest_parms); | |
408 | ||
409 | return 0; | |
410 | ||
411 | nla_put_failure: | |
412 | return -1; | |
413 | } | |
414 | ||
415 | static inline int | |
41d73ec0 | 416 | ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, const struct nf_conn *ct) |
13eae15a | 417 | { |
41d73ec0 PM |
418 | struct nf_conn_seqadj *seqadj = nfct_seqadj(ct); |
419 | struct nf_ct_seqadj *seq; | |
13eae15a | 420 | |
41d73ec0 | 421 | if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj) |
13eae15a PNA |
422 | return 0; |
423 | ||
41d73ec0 PM |
424 | seq = &seqadj->seq[IP_CT_DIR_ORIGINAL]; |
425 | if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1) | |
13eae15a PNA |
426 | return -1; |
427 | ||
41d73ec0 PM |
428 | seq = &seqadj->seq[IP_CT_DIR_REPLY]; |
429 | if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1) | |
13eae15a PNA |
430 | return -1; |
431 | ||
432 | return 0; | |
433 | } | |
13eae15a | 434 | |
c1d10adb PNA |
435 | static inline int |
436 | ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) | |
437 | { | |
cc1eb431 DM |
438 | if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct))) |
439 | goto nla_put_failure; | |
c1d10adb PNA |
440 | return 0; |
441 | ||
df6fb868 | 442 | nla_put_failure: |
c1d10adb PNA |
443 | return -1; |
444 | } | |
445 | ||
446 | static inline int | |
447 | ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct) | |
448 | { | |
cc1eb431 DM |
449 | if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)))) |
450 | goto nla_put_failure; | |
c1d10adb PNA |
451 | return 0; |
452 | ||
df6fb868 | 453 | nla_put_failure: |
c1d10adb PNA |
454 | return -1; |
455 | } | |
456 | ||
c1d10adb | 457 | static int |
15e47304 | 458 | ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, |
80e60e67 | 459 | struct nf_conn *ct) |
c1d10adb PNA |
460 | { |
461 | struct nlmsghdr *nlh; | |
462 | struct nfgenmsg *nfmsg; | |
df6fb868 | 463 | struct nlattr *nest_parms; |
15e47304 | 464 | unsigned int flags = portid ? NLM_F_MULTI : 0, event; |
c1d10adb | 465 | |
80e60e67 | 466 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW); |
15e47304 | 467 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
96bcf938 PNA |
468 | if (nlh == NULL) |
469 | goto nlmsg_failure; | |
c1d10adb | 470 | |
96bcf938 | 471 | nfmsg = nlmsg_data(nlh); |
5e8fbe2a | 472 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
473 | nfmsg->version = NFNETLINK_V0; |
474 | nfmsg->res_id = 0; | |
475 | ||
df6fb868 PM |
476 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
477 | if (!nest_parms) | |
478 | goto nla_put_failure; | |
f2f3e38c | 479 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
480 | goto nla_put_failure; |
481 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 482 | |
df6fb868 PM |
483 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
484 | if (!nest_parms) | |
485 | goto nla_put_failure; | |
f2f3e38c | 486 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
487 | goto nla_put_failure; |
488 | nla_nest_end(skb, nest_parms); | |
c1d10adb | 489 | |
cc1eb431 DM |
490 | if (nf_ct_zone(ct) && |
491 | nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct)))) | |
492 | goto nla_put_failure; | |
ef00f89f | 493 | |
c1d10adb PNA |
494 | if (ctnetlink_dump_status(skb, ct) < 0 || |
495 | ctnetlink_dump_timeout(skb, ct) < 0 || | |
4542fa47 | 496 | ctnetlink_dump_acct(skb, ct, type) < 0 || |
a992ca2a | 497 | ctnetlink_dump_timestamp(skb, ct) < 0 || |
c1d10adb PNA |
498 | ctnetlink_dump_protoinfo(skb, ct) < 0 || |
499 | ctnetlink_dump_helpinfo(skb, ct) < 0 || | |
500 | ctnetlink_dump_mark(skb, ct) < 0 || | |
1cc63249 | 501 | ctnetlink_dump_secctx(skb, ct) < 0 || |
0ceabd83 | 502 | ctnetlink_dump_labels(skb, ct) < 0 || |
c1d10adb | 503 | ctnetlink_dump_id(skb, ct) < 0 || |
13eae15a | 504 | ctnetlink_dump_use(skb, ct) < 0 || |
0f417ce9 | 505 | ctnetlink_dump_master(skb, ct) < 0 || |
41d73ec0 | 506 | ctnetlink_dump_ct_seq_adj(skb, ct) < 0) |
df6fb868 | 507 | goto nla_put_failure; |
c1d10adb | 508 | |
96bcf938 | 509 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
510 | return skb->len; |
511 | ||
512 | nlmsg_failure: | |
df6fb868 | 513 | nla_put_failure: |
96bcf938 | 514 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
515 | return -1; |
516 | } | |
517 | ||
03b64f51 PNA |
518 | static inline size_t |
519 | ctnetlink_proto_size(const struct nf_conn *ct) | |
2732c4e4 HE |
520 | { |
521 | struct nf_conntrack_l3proto *l3proto; | |
522 | struct nf_conntrack_l4proto *l4proto; | |
03b64f51 PNA |
523 | size_t len = 0; |
524 | ||
525 | rcu_read_lock(); | |
526 | l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct)); | |
527 | len += l3proto->nla_size; | |
528 | ||
529 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
530 | len += l4proto->nla_size; | |
531 | rcu_read_unlock(); | |
532 | ||
533 | return len; | |
534 | } | |
535 | ||
d26e6a02 | 536 | static inline size_t |
f7b13e43 | 537 | ctnetlink_acct_size(const struct nf_conn *ct) |
d26e6a02 JP |
538 | { |
539 | if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT)) | |
540 | return 0; | |
541 | return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */ | |
542 | + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */ | |
543 | + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */ | |
544 | ; | |
545 | } | |
546 | ||
cba85b53 PNA |
547 | static inline int |
548 | ctnetlink_secctx_size(const struct nf_conn *ct) | |
1cc63249 | 549 | { |
cba85b53 PNA |
550 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
551 | int len, ret; | |
1cc63249 | 552 | |
cba85b53 PNA |
553 | ret = security_secid_to_secctx(ct->secmark, NULL, &len); |
554 | if (ret) | |
555 | return 0; | |
1cc63249 | 556 | |
cba85b53 PNA |
557 | return nla_total_size(0) /* CTA_SECCTX */ |
558 | + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */ | |
559 | #else | |
560 | return 0; | |
1cc63249 | 561 | #endif |
cba85b53 | 562 | } |
1cc63249 | 563 | |
a992ca2a PNA |
564 | static inline size_t |
565 | ctnetlink_timestamp_size(const struct nf_conn *ct) | |
566 | { | |
567 | #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP | |
568 | if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP)) | |
569 | return 0; | |
570 | return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t)); | |
571 | #else | |
572 | return 0; | |
573 | #endif | |
574 | } | |
575 | ||
03b64f51 PNA |
576 | static inline size_t |
577 | ctnetlink_nlmsg_size(const struct nf_conn *ct) | |
578 | { | |
579 | return NLMSG_ALIGN(sizeof(struct nfgenmsg)) | |
580 | + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ | |
581 | + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ | |
582 | + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ | |
583 | + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ | |
584 | + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ | |
585 | + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ | |
f7b13e43 | 586 | + ctnetlink_acct_size(ct) |
a992ca2a | 587 | + ctnetlink_timestamp_size(ct) |
03b64f51 PNA |
588 | + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ |
589 | + nla_total_size(0) /* CTA_PROTOINFO */ | |
590 | + nla_total_size(0) /* CTA_HELP */ | |
591 | + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ | |
cba85b53 | 592 | + ctnetlink_secctx_size(ct) |
d271e8bd | 593 | #ifdef CONFIG_NF_NAT_NEEDED |
03b64f51 PNA |
594 | + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ |
595 | + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ | |
d271e8bd HE |
596 | #endif |
597 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
03b64f51 | 598 | + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ |
4a001068 KM |
599 | #endif |
600 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
601 | + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE */ | |
d271e8bd | 602 | #endif |
03b64f51 | 603 | + ctnetlink_proto_size(ct) |
0ceabd83 | 604 | + ctnetlink_label_size(ct) |
03b64f51 | 605 | ; |
2732c4e4 HE |
606 | } |
607 | ||
8e36c4b5 | 608 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
e34d5c1a PNA |
609 | static int |
610 | ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item) | |
c1d10adb | 611 | { |
9592a5c0 | 612 | struct net *net; |
c1d10adb PNA |
613 | struct nlmsghdr *nlh; |
614 | struct nfgenmsg *nfmsg; | |
df6fb868 | 615 | struct nlattr *nest_parms; |
19abb7b0 | 616 | struct nf_conn *ct = item->ct; |
c1d10adb PNA |
617 | struct sk_buff *skb; |
618 | unsigned int type; | |
c1d10adb | 619 | unsigned int flags = 0, group; |
dd7669a9 | 620 | int err; |
c1d10adb PNA |
621 | |
622 | /* ignore our fake conntrack entry */ | |
5bfddbd4 | 623 | if (nf_ct_is_untracked(ct)) |
e34d5c1a | 624 | return 0; |
c1d10adb | 625 | |
a0891aa6 | 626 | if (events & (1 << IPCT_DESTROY)) { |
c1d10adb PNA |
627 | type = IPCTNL_MSG_CT_DELETE; |
628 | group = NFNLGRP_CONNTRACK_DESTROY; | |
a0891aa6 | 629 | } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) { |
c1d10adb PNA |
630 | type = IPCTNL_MSG_CT_NEW; |
631 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
c1d10adb | 632 | group = NFNLGRP_CONNTRACK_NEW; |
17e6e4ea | 633 | } else if (events) { |
c1d10adb PNA |
634 | type = IPCTNL_MSG_CT_NEW; |
635 | group = NFNLGRP_CONNTRACK_UPDATE; | |
636 | } else | |
e34d5c1a | 637 | return 0; |
a2427692 | 638 | |
9592a5c0 AD |
639 | net = nf_ct_net(ct); |
640 | if (!item->report && !nfnetlink_has_listeners(net, group)) | |
e34d5c1a | 641 | return 0; |
a2427692 | 642 | |
03b64f51 PNA |
643 | skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC); |
644 | if (skb == NULL) | |
150ace0d | 645 | goto errout; |
c1d10adb | 646 | |
c1d10adb | 647 | type |= NFNL_SUBSYS_CTNETLINK << 8; |
15e47304 | 648 | nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags); |
96bcf938 PNA |
649 | if (nlh == NULL) |
650 | goto nlmsg_failure; | |
c1d10adb | 651 | |
96bcf938 | 652 | nfmsg = nlmsg_data(nlh); |
5e8fbe2a | 653 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
654 | nfmsg->version = NFNETLINK_V0; |
655 | nfmsg->res_id = 0; | |
656 | ||
528a3a6f | 657 | rcu_read_lock(); |
df6fb868 PM |
658 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
659 | if (!nest_parms) | |
660 | goto nla_put_failure; | |
f2f3e38c | 661 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
662 | goto nla_put_failure; |
663 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 664 | |
df6fb868 PM |
665 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
666 | if (!nest_parms) | |
667 | goto nla_put_failure; | |
f2f3e38c | 668 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
669 | goto nla_put_failure; |
670 | nla_nest_end(skb, nest_parms); | |
c1d10adb | 671 | |
cc1eb431 DM |
672 | if (nf_ct_zone(ct) && |
673 | nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct)))) | |
674 | goto nla_put_failure; | |
ef00f89f | 675 | |
1eedf699 EL |
676 | if (ctnetlink_dump_id(skb, ct) < 0) |
677 | goto nla_put_failure; | |
678 | ||
e57dce60 FH |
679 | if (ctnetlink_dump_status(skb, ct) < 0) |
680 | goto nla_put_failure; | |
681 | ||
a0891aa6 | 682 | if (events & (1 << IPCT_DESTROY)) { |
4542fa47 | 683 | if (ctnetlink_dump_acct(skb, ct, type) < 0 || |
a992ca2a | 684 | ctnetlink_dump_timestamp(skb, ct) < 0) |
df6fb868 | 685 | goto nla_put_failure; |
7b621c1e | 686 | } else { |
7b621c1e | 687 | if (ctnetlink_dump_timeout(skb, ct) < 0) |
df6fb868 | 688 | goto nla_put_failure; |
7b621c1e | 689 | |
a0891aa6 | 690 | if (events & (1 << IPCT_PROTOINFO) |
7b621c1e | 691 | && ctnetlink_dump_protoinfo(skb, ct) < 0) |
df6fb868 | 692 | goto nla_put_failure; |
7b621c1e | 693 | |
a0891aa6 | 694 | if ((events & (1 << IPCT_HELPER) || nfct_help(ct)) |
7b621c1e | 695 | && ctnetlink_dump_helpinfo(skb, ct) < 0) |
df6fb868 | 696 | goto nla_put_failure; |
7b621c1e | 697 | |
ff660c80 | 698 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
a0891aa6 | 699 | if ((events & (1 << IPCT_SECMARK) || ct->secmark) |
1cc63249 | 700 | && ctnetlink_dump_secctx(skb, ct) < 0) |
37fccd85 | 701 | goto nla_put_failure; |
ff660c80 | 702 | #endif |
0ceabd83 FW |
703 | if (events & (1 << IPCT_LABEL) && |
704 | ctnetlink_dump_labels(skb, ct) < 0) | |
705 | goto nla_put_failure; | |
7b621c1e | 706 | |
a0891aa6 | 707 | if (events & (1 << IPCT_RELATED) && |
0f417ce9 PNA |
708 | ctnetlink_dump_master(skb, ct) < 0) |
709 | goto nla_put_failure; | |
710 | ||
41d73ec0 PM |
711 | if (events & (1 << IPCT_SEQADJ) && |
712 | ctnetlink_dump_ct_seq_adj(skb, ct) < 0) | |
13eae15a | 713 | goto nla_put_failure; |
7b621c1e | 714 | } |
b9a37e0c | 715 | |
a83099a6 | 716 | #ifdef CONFIG_NF_CONNTRACK_MARK |
a0891aa6 | 717 | if ((events & (1 << IPCT_MARK) || ct->mark) |
a83099a6 EL |
718 | && ctnetlink_dump_mark(skb, ct) < 0) |
719 | goto nla_put_failure; | |
720 | #endif | |
528a3a6f | 721 | rcu_read_unlock(); |
a83099a6 | 722 | |
96bcf938 | 723 | nlmsg_end(skb, nlh); |
15e47304 | 724 | err = nfnetlink_send(skb, net, item->portid, group, item->report, |
cd8c20b6 | 725 | GFP_ATOMIC); |
dd7669a9 PNA |
726 | if (err == -ENOBUFS || err == -EAGAIN) |
727 | return -ENOBUFS; | |
728 | ||
e34d5c1a | 729 | return 0; |
c1d10adb | 730 | |
df6fb868 | 731 | nla_put_failure: |
528a3a6f | 732 | rcu_read_unlock(); |
96bcf938 | 733 | nlmsg_cancel(skb, nlh); |
528a3a6f | 734 | nlmsg_failure: |
c1d10adb | 735 | kfree_skb(skb); |
150ace0d | 736 | errout: |
37b7ef72 PNA |
737 | if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0) |
738 | return -ENOBUFS; | |
739 | ||
e34d5c1a | 740 | return 0; |
c1d10adb PNA |
741 | } |
742 | #endif /* CONFIG_NF_CONNTRACK_EVENTS */ | |
743 | ||
744 | static int ctnetlink_done(struct netlink_callback *cb) | |
745 | { | |
89f2e218 PM |
746 | if (cb->args[1]) |
747 | nf_ct_put((struct nf_conn *)cb->args[1]); | |
397304b5 | 748 | kfree(cb->data); |
c1d10adb PNA |
749 | return 0; |
750 | } | |
751 | ||
0f298a28 PNA |
752 | struct ctnetlink_dump_filter { |
753 | struct { | |
754 | u_int32_t val; | |
755 | u_int32_t mask; | |
756 | } mark; | |
757 | }; | |
758 | ||
c1d10adb PNA |
759 | static int |
760 | ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
761 | { | |
9592a5c0 | 762 | struct net *net = sock_net(skb->sk); |
89f2e218 | 763 | struct nf_conn *ct, *last; |
c1d10adb | 764 | struct nf_conntrack_tuple_hash *h; |
ea781f19 | 765 | struct hlist_nulls_node *n; |
96bcf938 | 766 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
87711cb8 | 767 | u_int8_t l3proto = nfmsg->nfgen_family; |
3b988ece | 768 | int res; |
93bb0ceb JDB |
769 | spinlock_t *lockp; |
770 | ||
0f298a28 PNA |
771 | #ifdef CONFIG_NF_CONNTRACK_MARK |
772 | const struct ctnetlink_dump_filter *filter = cb->data; | |
773 | #endif | |
3b988ece | 774 | |
d205dc40 | 775 | last = (struct nf_conn *)cb->args[1]; |
93bb0ceb JDB |
776 | |
777 | local_bh_disable(); | |
9ab99d5a | 778 | for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) { |
89f2e218 | 779 | restart: |
93bb0ceb JDB |
780 | lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS]; |
781 | spin_lock(lockp); | |
782 | if (cb->args[0] >= net->ct.htable_size) { | |
783 | spin_unlock(lockp); | |
784 | goto out; | |
785 | } | |
13ee6ac5 | 786 | hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]], |
ea781f19 | 787 | hnnode) { |
5b1158e9 | 788 | if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
c1d10adb PNA |
789 | continue; |
790 | ct = nf_ct_tuplehash_to_ctrack(h); | |
87711cb8 PNA |
791 | /* Dump entries of a given L3 protocol number. |
792 | * If it is not specified, ie. l3proto == 0, | |
793 | * then dump everything. */ | |
5e8fbe2a | 794 | if (l3proto && nf_ct_l3num(ct) != l3proto) |
13ee6ac5 | 795 | continue; |
d205dc40 PM |
796 | if (cb->args[1]) { |
797 | if (ct != last) | |
13ee6ac5 | 798 | continue; |
d205dc40 | 799 | cb->args[1] = 0; |
89f2e218 | 800 | } |
0f298a28 PNA |
801 | #ifdef CONFIG_NF_CONNTRACK_MARK |
802 | if (filter && !((ct->mark & filter->mark.mask) == | |
803 | filter->mark.val)) { | |
804 | continue; | |
805 | } | |
806 | #endif | |
3b988ece HS |
807 | rcu_read_lock(); |
808 | res = | |
15e47304 | 809 | ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid, |
3b988ece HS |
810 | cb->nlh->nlmsg_seq, |
811 | NFNL_MSG_TYPE(cb->nlh->nlmsg_type), | |
812 | ct); | |
813 | rcu_read_unlock(); | |
814 | if (res < 0) { | |
c71caf41 | 815 | nf_conntrack_get(&ct->ct_general); |
89f2e218 | 816 | cb->args[1] = (unsigned long)ct; |
93bb0ceb | 817 | spin_unlock(lockp); |
c1d10adb | 818 | goto out; |
89f2e218 PM |
819 | } |
820 | } | |
93bb0ceb | 821 | spin_unlock(lockp); |
d205dc40 | 822 | if (cb->args[1]) { |
89f2e218 PM |
823 | cb->args[1] = 0; |
824 | goto restart; | |
c1d10adb PNA |
825 | } |
826 | } | |
89f2e218 | 827 | out: |
93bb0ceb | 828 | local_bh_enable(); |
d205dc40 PM |
829 | if (last) |
830 | nf_ct_put(last); | |
c1d10adb | 831 | |
c1d10adb PNA |
832 | return skb->len; |
833 | } | |
834 | ||
c1d10adb | 835 | static inline int |
df6fb868 | 836 | ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple) |
c1d10adb | 837 | { |
df6fb868 | 838 | struct nlattr *tb[CTA_IP_MAX+1]; |
c1d10adb PNA |
839 | struct nf_conntrack_l3proto *l3proto; |
840 | int ret = 0; | |
841 | ||
130ffbc2 DB |
842 | ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL); |
843 | if (ret < 0) | |
844 | return ret; | |
c1d10adb | 845 | |
cd91566e FW |
846 | rcu_read_lock(); |
847 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); | |
c1d10adb | 848 | |
f73e924c PM |
849 | if (likely(l3proto->nlattr_to_tuple)) { |
850 | ret = nla_validate_nested(attr, CTA_IP_MAX, | |
851 | l3proto->nla_policy); | |
852 | if (ret == 0) | |
853 | ret = l3proto->nlattr_to_tuple(tb, tuple); | |
854 | } | |
c1d10adb | 855 | |
cd91566e | 856 | rcu_read_unlock(); |
c1d10adb | 857 | |
c1d10adb PNA |
858 | return ret; |
859 | } | |
860 | ||
f73e924c PM |
861 | static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = { |
862 | [CTA_PROTO_NUM] = { .type = NLA_U8 }, | |
c1d10adb PNA |
863 | }; |
864 | ||
865 | static inline int | |
df6fb868 | 866 | ctnetlink_parse_tuple_proto(struct nlattr *attr, |
c1d10adb PNA |
867 | struct nf_conntrack_tuple *tuple) |
868 | { | |
df6fb868 | 869 | struct nlattr *tb[CTA_PROTO_MAX+1]; |
605dcad6 | 870 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
871 | int ret = 0; |
872 | ||
f73e924c PM |
873 | ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy); |
874 | if (ret < 0) | |
875 | return ret; | |
c1d10adb | 876 | |
df6fb868 | 877 | if (!tb[CTA_PROTO_NUM]) |
c1d10adb | 878 | return -EINVAL; |
77236b6e | 879 | tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]); |
c1d10adb | 880 | |
cd91566e FW |
881 | rcu_read_lock(); |
882 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); | |
c1d10adb | 883 | |
f73e924c PM |
884 | if (likely(l4proto->nlattr_to_tuple)) { |
885 | ret = nla_validate_nested(attr, CTA_PROTO_MAX, | |
886 | l4proto->nla_policy); | |
887 | if (ret == 0) | |
888 | ret = l4proto->nlattr_to_tuple(tb, tuple); | |
889 | } | |
c1d10adb | 890 | |
cd91566e | 891 | rcu_read_unlock(); |
601e68e1 | 892 | |
c1d10adb PNA |
893 | return ret; |
894 | } | |
895 | ||
d0b0268f PM |
896 | static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = { |
897 | [CTA_TUPLE_IP] = { .type = NLA_NESTED }, | |
898 | [CTA_TUPLE_PROTO] = { .type = NLA_NESTED }, | |
899 | }; | |
900 | ||
bb5cf80e | 901 | static int |
39938324 PM |
902 | ctnetlink_parse_tuple(const struct nlattr * const cda[], |
903 | struct nf_conntrack_tuple *tuple, | |
a00f1f36 | 904 | enum ctattr_type type, u_int8_t l3num) |
c1d10adb | 905 | { |
df6fb868 | 906 | struct nlattr *tb[CTA_TUPLE_MAX+1]; |
c1d10adb PNA |
907 | int err; |
908 | ||
c1d10adb PNA |
909 | memset(tuple, 0, sizeof(*tuple)); |
910 | ||
130ffbc2 DB |
911 | err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy); |
912 | if (err < 0) | |
913 | return err; | |
c1d10adb | 914 | |
df6fb868 | 915 | if (!tb[CTA_TUPLE_IP]) |
c1d10adb PNA |
916 | return -EINVAL; |
917 | ||
918 | tuple->src.l3num = l3num; | |
919 | ||
df6fb868 | 920 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple); |
c1d10adb PNA |
921 | if (err < 0) |
922 | return err; | |
923 | ||
df6fb868 | 924 | if (!tb[CTA_TUPLE_PROTO]) |
c1d10adb PNA |
925 | return -EINVAL; |
926 | ||
df6fb868 | 927 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple); |
c1d10adb PNA |
928 | if (err < 0) |
929 | return err; | |
930 | ||
931 | /* orig and expect tuples get DIR_ORIGINAL */ | |
932 | if (type == CTA_TUPLE_REPLY) | |
933 | tuple->dst.dir = IP_CT_DIR_REPLY; | |
934 | else | |
935 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
936 | ||
c1d10adb PNA |
937 | return 0; |
938 | } | |
939 | ||
ef00f89f PM |
940 | static int |
941 | ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone) | |
942 | { | |
943 | if (attr) | |
944 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
945 | *zone = ntohs(nla_get_be16(attr)); | |
946 | #else | |
947 | return -EOPNOTSUPP; | |
948 | #endif | |
949 | else | |
950 | *zone = 0; | |
951 | ||
952 | return 0; | |
953 | } | |
954 | ||
d0b0268f | 955 | static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = { |
6d1fafca FW |
956 | [CTA_HELP_NAME] = { .type = NLA_NUL_STRING, |
957 | .len = NF_CT_HELPER_NAME_LEN - 1 }, | |
d0b0268f PM |
958 | }; |
959 | ||
c1d10adb | 960 | static inline int |
ae243bee PNA |
961 | ctnetlink_parse_help(const struct nlattr *attr, char **helper_name, |
962 | struct nlattr **helpinfo) | |
c1d10adb | 963 | { |
130ffbc2 | 964 | int err; |
df6fb868 | 965 | struct nlattr *tb[CTA_HELP_MAX+1]; |
c1d10adb | 966 | |
130ffbc2 DB |
967 | err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy); |
968 | if (err < 0) | |
969 | return err; | |
c1d10adb | 970 | |
df6fb868 | 971 | if (!tb[CTA_HELP_NAME]) |
c1d10adb PNA |
972 | return -EINVAL; |
973 | ||
df6fb868 | 974 | *helper_name = nla_data(tb[CTA_HELP_NAME]); |
c1d10adb | 975 | |
ae243bee PNA |
976 | if (tb[CTA_HELP_INFO]) |
977 | *helpinfo = tb[CTA_HELP_INFO]; | |
978 | ||
c1d10adb PNA |
979 | return 0; |
980 | } | |
981 | ||
f73e924c | 982 | static const struct nla_policy ct_nla_policy[CTA_MAX+1] = { |
d0b0268f PM |
983 | [CTA_TUPLE_ORIG] = { .type = NLA_NESTED }, |
984 | [CTA_TUPLE_REPLY] = { .type = NLA_NESTED }, | |
f73e924c | 985 | [CTA_STATUS] = { .type = NLA_U32 }, |
d0b0268f PM |
986 | [CTA_PROTOINFO] = { .type = NLA_NESTED }, |
987 | [CTA_HELP] = { .type = NLA_NESTED }, | |
988 | [CTA_NAT_SRC] = { .type = NLA_NESTED }, | |
f73e924c PM |
989 | [CTA_TIMEOUT] = { .type = NLA_U32 }, |
990 | [CTA_MARK] = { .type = NLA_U32 }, | |
f73e924c | 991 | [CTA_ID] = { .type = NLA_U32 }, |
d0b0268f PM |
992 | [CTA_NAT_DST] = { .type = NLA_NESTED }, |
993 | [CTA_TUPLE_MASTER] = { .type = NLA_NESTED }, | |
6d1fafca FW |
994 | [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED }, |
995 | [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED }, | |
ef00f89f | 996 | [CTA_ZONE] = { .type = NLA_U16 }, |
0f298a28 | 997 | [CTA_MARK_MASK] = { .type = NLA_U32 }, |
9b21f6a9 | 998 | [CTA_LABELS] = { .type = NLA_BINARY, |
d2bf2f34 | 999 | .len = NF_CT_LABELS_MAX_SIZE }, |
9b21f6a9 | 1000 | [CTA_LABELS_MASK] = { .type = NLA_BINARY, |
d2bf2f34 | 1001 | .len = NF_CT_LABELS_MAX_SIZE }, |
c1d10adb PNA |
1002 | }; |
1003 | ||
1004 | static int | |
601e68e1 | 1005 | ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
1006 | const struct nlmsghdr *nlh, |
1007 | const struct nlattr * const cda[]) | |
c1d10adb | 1008 | { |
9592a5c0 | 1009 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1010 | struct nf_conntrack_tuple_hash *h; |
1011 | struct nf_conntrack_tuple tuple; | |
1012 | struct nf_conn *ct; | |
96bcf938 | 1013 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 1014 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
1015 | u16 zone; |
1016 | int err; | |
1017 | ||
1018 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); | |
1019 | if (err < 0) | |
1020 | return err; | |
c1d10adb | 1021 | |
df6fb868 | 1022 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 1023 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 1024 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
1025 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
1026 | else { | |
1027 | /* Flush the whole table */ | |
9592a5c0 | 1028 | nf_conntrack_flush_report(net, |
15e47304 | 1029 | NETLINK_CB(skb).portid, |
274d383b | 1030 | nlmsg_report(nlh)); |
c1d10adb PNA |
1031 | return 0; |
1032 | } | |
1033 | ||
1034 | if (err < 0) | |
1035 | return err; | |
1036 | ||
ef00f89f | 1037 | h = nf_conntrack_find_get(net, zone, &tuple); |
9ea8cfd6 | 1038 | if (!h) |
c1d10adb | 1039 | return -ENOENT; |
c1d10adb PNA |
1040 | |
1041 | ct = nf_ct_tuplehash_to_ctrack(h); | |
601e68e1 | 1042 | |
df6fb868 | 1043 | if (cda[CTA_ID]) { |
77236b6e | 1044 | u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID])); |
7f85f914 | 1045 | if (id != (u32)(unsigned long)ct) { |
c1d10adb PNA |
1046 | nf_ct_put(ct); |
1047 | return -ENOENT; | |
1048 | } | |
601e68e1 | 1049 | } |
c1d10adb | 1050 | |
02982c27 FW |
1051 | if (del_timer(&ct->timeout)) |
1052 | nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh)); | |
1053 | ||
c1d10adb | 1054 | nf_ct_put(ct); |
c1d10adb PNA |
1055 | |
1056 | return 0; | |
1057 | } | |
1058 | ||
1059 | static int | |
601e68e1 | 1060 | ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
1061 | const struct nlmsghdr *nlh, |
1062 | const struct nlattr * const cda[]) | |
c1d10adb | 1063 | { |
9592a5c0 | 1064 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1065 | struct nf_conntrack_tuple_hash *h; |
1066 | struct nf_conntrack_tuple tuple; | |
1067 | struct nf_conn *ct; | |
1068 | struct sk_buff *skb2 = NULL; | |
96bcf938 | 1069 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 1070 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
1071 | u16 zone; |
1072 | int err; | |
c1d10adb | 1073 | |
80d326fa PNA |
1074 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
1075 | struct netlink_dump_control c = { | |
1076 | .dump = ctnetlink_dump_table, | |
1077 | .done = ctnetlink_done, | |
1078 | }; | |
0f298a28 PNA |
1079 | #ifdef CONFIG_NF_CONNTRACK_MARK |
1080 | if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) { | |
1081 | struct ctnetlink_dump_filter *filter; | |
1082 | ||
1083 | filter = kzalloc(sizeof(struct ctnetlink_dump_filter), | |
1084 | GFP_ATOMIC); | |
1085 | if (filter == NULL) | |
1086 | return -ENOMEM; | |
1087 | ||
1088 | filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK])); | |
1089 | filter->mark.mask = | |
1090 | ntohl(nla_get_be32(cda[CTA_MARK_MASK])); | |
1091 | c.data = filter; | |
1092 | } | |
1093 | #endif | |
80d326fa PNA |
1094 | return netlink_dump_start(ctnl, skb, nlh, &c); |
1095 | } | |
c1d10adb | 1096 | |
ef00f89f PM |
1097 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); |
1098 | if (err < 0) | |
1099 | return err; | |
1100 | ||
df6fb868 | 1101 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 1102 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 1103 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
1104 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
1105 | else | |
1106 | return -EINVAL; | |
1107 | ||
1108 | if (err < 0) | |
1109 | return err; | |
1110 | ||
ef00f89f | 1111 | h = nf_conntrack_find_get(net, zone, &tuple); |
9ea8cfd6 | 1112 | if (!h) |
c1d10adb | 1113 | return -ENOENT; |
9ea8cfd6 | 1114 | |
c1d10adb PNA |
1115 | ct = nf_ct_tuplehash_to_ctrack(h); |
1116 | ||
1117 | err = -ENOMEM; | |
96bcf938 PNA |
1118 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1119 | if (skb2 == NULL) { | |
c1d10adb PNA |
1120 | nf_ct_put(ct); |
1121 | return -ENOMEM; | |
1122 | } | |
c1d10adb | 1123 | |
528a3a6f | 1124 | rcu_read_lock(); |
15e47304 | 1125 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, |
80e60e67 | 1126 | NFNL_MSG_TYPE(nlh->nlmsg_type), ct); |
528a3a6f | 1127 | rcu_read_unlock(); |
c1d10adb PNA |
1128 | nf_ct_put(ct); |
1129 | if (err <= 0) | |
1130 | goto free; | |
1131 | ||
15e47304 | 1132 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT); |
c1d10adb PNA |
1133 | if (err < 0) |
1134 | goto out; | |
1135 | ||
c1d10adb PNA |
1136 | return 0; |
1137 | ||
1138 | free: | |
1139 | kfree_skb(skb2); | |
1140 | out: | |
f31e8d49 PNA |
1141 | /* this avoids a loop in nfnetlink. */ |
1142 | return err == -EAGAIN ? -ENOBUFS : err; | |
c1d10adb PNA |
1143 | } |
1144 | ||
d871befe PNA |
1145 | static int ctnetlink_done_list(struct netlink_callback *cb) |
1146 | { | |
1147 | if (cb->args[1]) | |
1148 | nf_ct_put((struct nf_conn *)cb->args[1]); | |
1149 | return 0; | |
1150 | } | |
1151 | ||
1152 | static int | |
b7779d06 | 1153 | ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying) |
d871befe | 1154 | { |
cd5f336f | 1155 | struct nf_conn *ct, *last; |
d871befe PNA |
1156 | struct nf_conntrack_tuple_hash *h; |
1157 | struct hlist_nulls_node *n; | |
1158 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
1159 | u_int8_t l3proto = nfmsg->nfgen_family; | |
1160 | int res; | |
b7779d06 JDB |
1161 | int cpu; |
1162 | struct hlist_nulls_head *list; | |
1163 | struct net *net = sock_net(skb->sk); | |
d871befe PNA |
1164 | |
1165 | if (cb->args[2]) | |
1166 | return 0; | |
1167 | ||
cd5f336f FW |
1168 | last = (struct nf_conn *)cb->args[1]; |
1169 | ||
b7779d06 JDB |
1170 | for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) { |
1171 | struct ct_pcpu *pcpu; | |
1172 | ||
1173 | if (!cpu_possible(cpu)) | |
d871befe | 1174 | continue; |
b7779d06 JDB |
1175 | |
1176 | pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu); | |
1177 | spin_lock_bh(&pcpu->lock); | |
b7779d06 JDB |
1178 | list = dying ? &pcpu->dying : &pcpu->unconfirmed; |
1179 | restart: | |
1180 | hlist_nulls_for_each_entry(h, n, list, hnnode) { | |
1181 | ct = nf_ct_tuplehash_to_ctrack(h); | |
1182 | if (l3proto && nf_ct_l3num(ct) != l3proto) | |
d871befe | 1183 | continue; |
b7779d06 JDB |
1184 | if (cb->args[1]) { |
1185 | if (ct != last) | |
1186 | continue; | |
1187 | cb->args[1] = 0; | |
1188 | } | |
1189 | rcu_read_lock(); | |
1190 | res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid, | |
1191 | cb->nlh->nlmsg_seq, | |
1192 | NFNL_MSG_TYPE(cb->nlh->nlmsg_type), | |
1193 | ct); | |
1194 | rcu_read_unlock(); | |
1195 | if (res < 0) { | |
cd5f336f FW |
1196 | if (!atomic_inc_not_zero(&ct->ct_general.use)) |
1197 | continue; | |
266155b2 | 1198 | cb->args[0] = cpu; |
b7779d06 JDB |
1199 | cb->args[1] = (unsigned long)ct; |
1200 | spin_unlock_bh(&pcpu->lock); | |
1201 | goto out; | |
1202 | } | |
d871befe | 1203 | } |
b7779d06 JDB |
1204 | if (cb->args[1]) { |
1205 | cb->args[1] = 0; | |
1206 | goto restart; | |
266155b2 | 1207 | } |
b7779d06 | 1208 | spin_unlock_bh(&pcpu->lock); |
d871befe | 1209 | } |
266155b2 | 1210 | cb->args[2] = 1; |
d871befe | 1211 | out: |
d871befe PNA |
1212 | if (last) |
1213 | nf_ct_put(last); | |
1214 | ||
1215 | return skb->len; | |
1216 | } | |
1217 | ||
1218 | static int | |
1219 | ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb) | |
1220 | { | |
b7779d06 | 1221 | return ctnetlink_dump_list(skb, cb, true); |
d871befe PNA |
1222 | } |
1223 | ||
1224 | static int | |
1225 | ctnetlink_get_ct_dying(struct sock *ctnl, struct sk_buff *skb, | |
1226 | const struct nlmsghdr *nlh, | |
1227 | const struct nlattr * const cda[]) | |
1228 | { | |
1229 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1230 | struct netlink_dump_control c = { | |
1231 | .dump = ctnetlink_dump_dying, | |
1232 | .done = ctnetlink_done_list, | |
1233 | }; | |
1234 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
1235 | } | |
1236 | ||
1237 | return -EOPNOTSUPP; | |
1238 | } | |
1239 | ||
1240 | static int | |
1241 | ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb) | |
1242 | { | |
b7779d06 | 1243 | return ctnetlink_dump_list(skb, cb, false); |
d871befe PNA |
1244 | } |
1245 | ||
1246 | static int | |
1247 | ctnetlink_get_ct_unconfirmed(struct sock *ctnl, struct sk_buff *skb, | |
1248 | const struct nlmsghdr *nlh, | |
1249 | const struct nlattr * const cda[]) | |
1250 | { | |
1251 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1252 | struct netlink_dump_control c = { | |
1253 | .dump = ctnetlink_dump_unconfirmed, | |
1254 | .done = ctnetlink_done_list, | |
1255 | }; | |
1256 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
1257 | } | |
1258 | ||
1259 | return -EOPNOTSUPP; | |
1260 | } | |
1261 | ||
67671841 | 1262 | #ifdef CONFIG_NF_NAT_NEEDED |
e6a7d3c0 PNA |
1263 | static int |
1264 | ctnetlink_parse_nat_setup(struct nf_conn *ct, | |
1265 | enum nf_nat_manip_type manip, | |
39938324 | 1266 | const struct nlattr *attr) |
e6a7d3c0 PNA |
1267 | { |
1268 | typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup; | |
c7232c99 | 1269 | int err; |
e6a7d3c0 PNA |
1270 | |
1271 | parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook); | |
1272 | if (!parse_nat_setup) { | |
95a5afca | 1273 | #ifdef CONFIG_MODULES |
e6a7d3c0 | 1274 | rcu_read_unlock(); |
c14b78e7 | 1275 | nfnl_unlock(NFNL_SUBSYS_CTNETLINK); |
c7232c99 | 1276 | if (request_module("nf-nat") < 0) { |
c14b78e7 | 1277 | nfnl_lock(NFNL_SUBSYS_CTNETLINK); |
e6a7d3c0 PNA |
1278 | rcu_read_lock(); |
1279 | return -EOPNOTSUPP; | |
1280 | } | |
c14b78e7 | 1281 | nfnl_lock(NFNL_SUBSYS_CTNETLINK); |
e6a7d3c0 PNA |
1282 | rcu_read_lock(); |
1283 | if (nfnetlink_parse_nat_setup_hook) | |
1284 | return -EAGAIN; | |
1285 | #endif | |
1286 | return -EOPNOTSUPP; | |
1287 | } | |
1288 | ||
c7232c99 PM |
1289 | err = parse_nat_setup(ct, manip, attr); |
1290 | if (err == -EAGAIN) { | |
1291 | #ifdef CONFIG_MODULES | |
1292 | rcu_read_unlock(); | |
c14b78e7 | 1293 | nfnl_unlock(NFNL_SUBSYS_CTNETLINK); |
c7232c99 | 1294 | if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) { |
c14b78e7 | 1295 | nfnl_lock(NFNL_SUBSYS_CTNETLINK); |
c7232c99 PM |
1296 | rcu_read_lock(); |
1297 | return -EOPNOTSUPP; | |
1298 | } | |
c14b78e7 | 1299 | nfnl_lock(NFNL_SUBSYS_CTNETLINK); |
c7232c99 PM |
1300 | rcu_read_lock(); |
1301 | #else | |
1302 | err = -EOPNOTSUPP; | |
1303 | #endif | |
1304 | } | |
1305 | return err; | |
e6a7d3c0 | 1306 | } |
67671841 | 1307 | #endif |
e6a7d3c0 | 1308 | |
bb5cf80e | 1309 | static int |
39938324 | 1310 | ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
1311 | { |
1312 | unsigned long d; | |
77236b6e | 1313 | unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS])); |
c1d10adb PNA |
1314 | d = ct->status ^ status; |
1315 | ||
1316 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) | |
1317 | /* unchangeable */ | |
0adf9d67 | 1318 | return -EBUSY; |
601e68e1 | 1319 | |
c1d10adb PNA |
1320 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) |
1321 | /* SEEN_REPLY bit can only be set */ | |
0adf9d67 | 1322 | return -EBUSY; |
601e68e1 | 1323 | |
c1d10adb PNA |
1324 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) |
1325 | /* ASSURED bit can only be set */ | |
0adf9d67 | 1326 | return -EBUSY; |
c1d10adb | 1327 | |
c1d10adb PNA |
1328 | /* Be careful here, modifying NAT bits can screw up things, |
1329 | * so don't let users modify them directly if they don't pass | |
5b1158e9 | 1330 | * nf_nat_range. */ |
c1d10adb PNA |
1331 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); |
1332 | return 0; | |
1333 | } | |
1334 | ||
e6a7d3c0 | 1335 | static int |
0eba801b | 1336 | ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[]) |
e6a7d3c0 PNA |
1337 | { |
1338 | #ifdef CONFIG_NF_NAT_NEEDED | |
1339 | int ret; | |
1340 | ||
fe337ac2 FW |
1341 | if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC]) |
1342 | return 0; | |
1343 | ||
0eba801b PNA |
1344 | ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST, |
1345 | cda[CTA_NAT_DST]); | |
1346 | if (ret < 0) | |
1347 | return ret; | |
1348 | ||
1349 | ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC, | |
1350 | cda[CTA_NAT_SRC]); | |
1351 | return ret; | |
e6a7d3c0 | 1352 | #else |
0eba801b PNA |
1353 | if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC]) |
1354 | return 0; | |
e6a7d3c0 PNA |
1355 | return -EOPNOTSUPP; |
1356 | #endif | |
1357 | } | |
c1d10adb PNA |
1358 | |
1359 | static inline int | |
39938324 | 1360 | ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
1361 | { |
1362 | struct nf_conntrack_helper *helper; | |
dc808fe2 | 1363 | struct nf_conn_help *help = nfct_help(ct); |
29fe1b48 | 1364 | char *helpname = NULL; |
ae243bee | 1365 | struct nlattr *helpinfo = NULL; |
c1d10adb PNA |
1366 | int err; |
1367 | ||
c1d10adb PNA |
1368 | /* don't change helper of sibling connections */ |
1369 | if (ct->master) | |
0adf9d67 | 1370 | return -EBUSY; |
c1d10adb | 1371 | |
ae243bee | 1372 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo); |
c1d10adb PNA |
1373 | if (err < 0) |
1374 | return err; | |
1375 | ||
df293bbb YK |
1376 | if (!strcmp(helpname, "")) { |
1377 | if (help && help->helper) { | |
c1d10adb PNA |
1378 | /* we had a helper before ... */ |
1379 | nf_ct_remove_expectations(ct); | |
a9b3cd7f | 1380 | RCU_INIT_POINTER(help->helper, NULL); |
c1d10adb | 1381 | } |
df293bbb YK |
1382 | |
1383 | return 0; | |
c1d10adb | 1384 | } |
601e68e1 | 1385 | |
794e6871 PM |
1386 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1387 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1388 | if (helper == NULL) { |
1389 | #ifdef CONFIG_MODULES | |
ca7433df | 1390 | spin_unlock_bh(&nf_conntrack_expect_lock); |
226c0c0e PNA |
1391 | |
1392 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
ca7433df | 1393 | spin_lock_bh(&nf_conntrack_expect_lock); |
226c0c0e PNA |
1394 | return -EOPNOTSUPP; |
1395 | } | |
1396 | ||
ca7433df | 1397 | spin_lock_bh(&nf_conntrack_expect_lock); |
794e6871 PM |
1398 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1399 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1400 | if (helper) |
1401 | return -EAGAIN; | |
1402 | #endif | |
0adf9d67 | 1403 | return -EOPNOTSUPP; |
226c0c0e | 1404 | } |
df293bbb | 1405 | |
ceceae1b | 1406 | if (help) { |
ae243bee PNA |
1407 | if (help->helper == helper) { |
1408 | /* update private helper data if allowed. */ | |
7be54ca4 | 1409 | if (helper->from_nlattr) |
ae243bee | 1410 | helper->from_nlattr(helpinfo, ct); |
ceceae1b | 1411 | return 0; |
fd7462de | 1412 | } else |
ceceae1b | 1413 | return -EBUSY; |
ceceae1b | 1414 | } |
df293bbb | 1415 | |
fd7462de PNA |
1416 | /* we cannot set a helper for an existing conntrack */ |
1417 | return -EOPNOTSUPP; | |
c1d10adb PNA |
1418 | } |
1419 | ||
1420 | static inline int | |
39938324 | 1421 | ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb | 1422 | { |
77236b6e | 1423 | u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
601e68e1 | 1424 | |
c1d10adb PNA |
1425 | if (!del_timer(&ct->timeout)) |
1426 | return -ETIME; | |
1427 | ||
1428 | ct->timeout.expires = jiffies + timeout * HZ; | |
1429 | add_timer(&ct->timeout); | |
1430 | ||
1431 | return 0; | |
1432 | } | |
1433 | ||
d0b0268f PM |
1434 | static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = { |
1435 | [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED }, | |
1436 | [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED }, | |
1437 | [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED }, | |
1438 | }; | |
1439 | ||
c1d10adb | 1440 | static inline int |
39938324 | 1441 | ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb | 1442 | { |
39938324 PM |
1443 | const struct nlattr *attr = cda[CTA_PROTOINFO]; |
1444 | struct nlattr *tb[CTA_PROTOINFO_MAX+1]; | |
605dcad6 | 1445 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
1446 | int err = 0; |
1447 | ||
130ffbc2 DB |
1448 | err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy); |
1449 | if (err < 0) | |
1450 | return err; | |
c1d10adb | 1451 | |
cd91566e FW |
1452 | rcu_read_lock(); |
1453 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
fdf70832 PM |
1454 | if (l4proto->from_nlattr) |
1455 | err = l4proto->from_nlattr(tb, ct); | |
cd91566e | 1456 | rcu_read_unlock(); |
c1d10adb PNA |
1457 | |
1458 | return err; | |
1459 | } | |
1460 | ||
41d73ec0 PM |
1461 | static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = { |
1462 | [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 }, | |
1463 | [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 }, | |
1464 | [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 }, | |
d0b0268f PM |
1465 | }; |
1466 | ||
13eae15a | 1467 | static inline int |
41d73ec0 | 1468 | change_seq_adj(struct nf_ct_seqadj *seq, const struct nlattr * const attr) |
13eae15a | 1469 | { |
130ffbc2 | 1470 | int err; |
41d73ec0 | 1471 | struct nlattr *cda[CTA_SEQADJ_MAX+1]; |
13eae15a | 1472 | |
41d73ec0 | 1473 | err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy); |
130ffbc2 DB |
1474 | if (err < 0) |
1475 | return err; | |
13eae15a | 1476 | |
41d73ec0 | 1477 | if (!cda[CTA_SEQADJ_CORRECTION_POS]) |
13eae15a PNA |
1478 | return -EINVAL; |
1479 | ||
41d73ec0 PM |
1480 | seq->correction_pos = |
1481 | ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS])); | |
13eae15a | 1482 | |
41d73ec0 | 1483 | if (!cda[CTA_SEQADJ_OFFSET_BEFORE]) |
13eae15a PNA |
1484 | return -EINVAL; |
1485 | ||
41d73ec0 PM |
1486 | seq->offset_before = |
1487 | ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE])); | |
13eae15a | 1488 | |
41d73ec0 | 1489 | if (!cda[CTA_SEQADJ_OFFSET_AFTER]) |
13eae15a PNA |
1490 | return -EINVAL; |
1491 | ||
41d73ec0 PM |
1492 | seq->offset_after = |
1493 | ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER])); | |
13eae15a PNA |
1494 | |
1495 | return 0; | |
1496 | } | |
1497 | ||
1498 | static int | |
41d73ec0 PM |
1499 | ctnetlink_change_seq_adj(struct nf_conn *ct, |
1500 | const struct nlattr * const cda[]) | |
13eae15a | 1501 | { |
41d73ec0 | 1502 | struct nf_conn_seqadj *seqadj = nfct_seqadj(ct); |
13eae15a | 1503 | int ret = 0; |
13eae15a | 1504 | |
41d73ec0 | 1505 | if (!seqadj) |
13eae15a PNA |
1506 | return 0; |
1507 | ||
41d73ec0 PM |
1508 | if (cda[CTA_SEQ_ADJ_ORIG]) { |
1509 | ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL], | |
1510 | cda[CTA_SEQ_ADJ_ORIG]); | |
13eae15a PNA |
1511 | if (ret < 0) |
1512 | return ret; | |
1513 | ||
1514 | ct->status |= IPS_SEQ_ADJUST; | |
1515 | } | |
1516 | ||
41d73ec0 PM |
1517 | if (cda[CTA_SEQ_ADJ_REPLY]) { |
1518 | ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY], | |
1519 | cda[CTA_SEQ_ADJ_REPLY]); | |
13eae15a PNA |
1520 | if (ret < 0) |
1521 | return ret; | |
1522 | ||
1523 | ct->status |= IPS_SEQ_ADJUST; | |
1524 | } | |
1525 | ||
1526 | return 0; | |
1527 | } | |
13eae15a | 1528 | |
9b21f6a9 FW |
1529 | static int |
1530 | ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[]) | |
1531 | { | |
1532 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
1533 | size_t len = nla_len(cda[CTA_LABELS]); | |
1534 | const void *mask = cda[CTA_LABELS_MASK]; | |
1535 | ||
1536 | if (len & (sizeof(u32)-1)) /* must be multiple of u32 */ | |
1537 | return -EINVAL; | |
1538 | ||
1539 | if (mask) { | |
1540 | if (nla_len(cda[CTA_LABELS_MASK]) == 0 || | |
1541 | nla_len(cda[CTA_LABELS_MASK]) != len) | |
1542 | return -EINVAL; | |
1543 | mask = nla_data(cda[CTA_LABELS_MASK]); | |
1544 | } | |
1545 | ||
1546 | len /= sizeof(u32); | |
1547 | ||
1548 | return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len); | |
1549 | #else | |
1550 | return -EOPNOTSUPP; | |
1551 | #endif | |
1552 | } | |
1553 | ||
c1d10adb | 1554 | static int |
39938324 PM |
1555 | ctnetlink_change_conntrack(struct nf_conn *ct, |
1556 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1557 | { |
1558 | int err; | |
1559 | ||
e098360f PNA |
1560 | /* only allow NAT changes and master assignation for new conntracks */ |
1561 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER]) | |
1562 | return -EOPNOTSUPP; | |
1563 | ||
df6fb868 | 1564 | if (cda[CTA_HELP]) { |
c1d10adb PNA |
1565 | err = ctnetlink_change_helper(ct, cda); |
1566 | if (err < 0) | |
1567 | return err; | |
1568 | } | |
1569 | ||
df6fb868 | 1570 | if (cda[CTA_TIMEOUT]) { |
c1d10adb PNA |
1571 | err = ctnetlink_change_timeout(ct, cda); |
1572 | if (err < 0) | |
1573 | return err; | |
1574 | } | |
1575 | ||
df6fb868 | 1576 | if (cda[CTA_STATUS]) { |
c1d10adb PNA |
1577 | err = ctnetlink_change_status(ct, cda); |
1578 | if (err < 0) | |
1579 | return err; | |
1580 | } | |
1581 | ||
df6fb868 | 1582 | if (cda[CTA_PROTOINFO]) { |
c1d10adb PNA |
1583 | err = ctnetlink_change_protoinfo(ct, cda); |
1584 | if (err < 0) | |
1585 | return err; | |
1586 | } | |
1587 | ||
bcd1e830 | 1588 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1589 | if (cda[CTA_MARK]) |
77236b6e | 1590 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1591 | #endif |
1592 | ||
41d73ec0 PM |
1593 | if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) { |
1594 | err = ctnetlink_change_seq_adj(ct, cda); | |
13eae15a PNA |
1595 | if (err < 0) |
1596 | return err; | |
1597 | } | |
41d73ec0 | 1598 | |
9b21f6a9 FW |
1599 | if (cda[CTA_LABELS]) { |
1600 | err = ctnetlink_attach_labels(ct, cda); | |
1601 | if (err < 0) | |
1602 | return err; | |
1603 | } | |
13eae15a | 1604 | |
c1d10adb PNA |
1605 | return 0; |
1606 | } | |
1607 | ||
f0a3c086 | 1608 | static struct nf_conn * |
ef00f89f | 1609 | ctnetlink_create_conntrack(struct net *net, u16 zone, |
9592a5c0 | 1610 | const struct nlattr * const cda[], |
c1d10adb | 1611 | struct nf_conntrack_tuple *otuple, |
5faa1f4c | 1612 | struct nf_conntrack_tuple *rtuple, |
7ec47496 | 1613 | u8 u3) |
c1d10adb PNA |
1614 | { |
1615 | struct nf_conn *ct; | |
1616 | int err = -EINVAL; | |
ceceae1b | 1617 | struct nf_conntrack_helper *helper; |
315c34da | 1618 | struct nf_conn_tstamp *tstamp; |
c1d10adb | 1619 | |
ef00f89f | 1620 | ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC); |
cd7fcbf1 | 1621 | if (IS_ERR(ct)) |
f0a3c086 | 1622 | return ERR_PTR(-ENOMEM); |
c1d10adb | 1623 | |
df6fb868 | 1624 | if (!cda[CTA_TIMEOUT]) |
0f5b3e85 | 1625 | goto err1; |
77236b6e | 1626 | ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
c1d10adb PNA |
1627 | |
1628 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; | |
c1d10adb | 1629 | |
1575e7ea | 1630 | rcu_read_lock(); |
226c0c0e | 1631 | if (cda[CTA_HELP]) { |
29fe1b48 | 1632 | char *helpname = NULL; |
ae243bee PNA |
1633 | struct nlattr *helpinfo = NULL; |
1634 | ||
1635 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo); | |
0f5b3e85 PM |
1636 | if (err < 0) |
1637 | goto err2; | |
226c0c0e | 1638 | |
794e6871 PM |
1639 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1640 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1641 | if (helper == NULL) { |
1642 | rcu_read_unlock(); | |
1643 | #ifdef CONFIG_MODULES | |
1644 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1645 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1646 | goto err1; |
226c0c0e PNA |
1647 | } |
1648 | ||
1649 | rcu_read_lock(); | |
794e6871 PM |
1650 | helper = __nf_conntrack_helper_find(helpname, |
1651 | nf_ct_l3num(ct), | |
1652 | nf_ct_protonum(ct)); | |
226c0c0e | 1653 | if (helper) { |
226c0c0e | 1654 | err = -EAGAIN; |
0f5b3e85 | 1655 | goto err2; |
226c0c0e PNA |
1656 | } |
1657 | rcu_read_unlock(); | |
1658 | #endif | |
1659 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1660 | goto err1; |
226c0c0e PNA |
1661 | } else { |
1662 | struct nf_conn_help *help; | |
1663 | ||
1afc5679 | 1664 | help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC); |
226c0c0e | 1665 | if (help == NULL) { |
226c0c0e | 1666 | err = -ENOMEM; |
0f5b3e85 | 1667 | goto err2; |
226c0c0e | 1668 | } |
ae243bee | 1669 | /* set private helper data if allowed. */ |
7be54ca4 | 1670 | if (helper->from_nlattr) |
ae243bee | 1671 | helper->from_nlattr(helpinfo, ct); |
226c0c0e PNA |
1672 | |
1673 | /* not in hash table yet so not strictly necessary */ | |
a9b3cd7f | 1674 | RCU_INIT_POINTER(help->helper, helper); |
226c0c0e PNA |
1675 | } |
1676 | } else { | |
1677 | /* try an implicit helper assignation */ | |
b2a15a60 | 1678 | err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC); |
0f5b3e85 PM |
1679 | if (err < 0) |
1680 | goto err2; | |
1575e7ea PNA |
1681 | } |
1682 | ||
0eba801b PNA |
1683 | err = ctnetlink_setup_nat(ct, cda); |
1684 | if (err < 0) | |
1685 | goto err2; | |
e6a7d3c0 | 1686 | |
a88e22ad | 1687 | nf_ct_acct_ext_add(ct, GFP_ATOMIC); |
a992ca2a | 1688 | nf_ct_tstamp_ext_add(ct, GFP_ATOMIC); |
a88e22ad | 1689 | nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC); |
c539f017 FW |
1690 | nf_ct_labels_ext_add(ct); |
1691 | ||
a88e22ad PNA |
1692 | /* we must add conntrack extensions before confirmation. */ |
1693 | ct->status |= IPS_CONFIRMED; | |
1694 | ||
1695 | if (cda[CTA_STATUS]) { | |
1696 | err = ctnetlink_change_status(ct, cda); | |
0f5b3e85 PM |
1697 | if (err < 0) |
1698 | goto err2; | |
bbb3357d | 1699 | } |
c1d10adb | 1700 | |
41d73ec0 PM |
1701 | if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) { |
1702 | err = ctnetlink_change_seq_adj(ct, cda); | |
0f5b3e85 PM |
1703 | if (err < 0) |
1704 | goto err2; | |
c969aa7d | 1705 | } |
c969aa7d | 1706 | |
e5fc9e7a | 1707 | memset(&ct->proto, 0, sizeof(ct->proto)); |
df6fb868 | 1708 | if (cda[CTA_PROTOINFO]) { |
c1d10adb | 1709 | err = ctnetlink_change_protoinfo(ct, cda); |
0f5b3e85 PM |
1710 | if (err < 0) |
1711 | goto err2; | |
c1d10adb PNA |
1712 | } |
1713 | ||
bcd1e830 | 1714 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1715 | if (cda[CTA_MARK]) |
77236b6e | 1716 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1717 | #endif |
1718 | ||
5faa1f4c | 1719 | /* setup master conntrack: this is a confirmed expectation */ |
7ec47496 PNA |
1720 | if (cda[CTA_TUPLE_MASTER]) { |
1721 | struct nf_conntrack_tuple master; | |
1722 | struct nf_conntrack_tuple_hash *master_h; | |
1723 | struct nf_conn *master_ct; | |
1724 | ||
1725 | err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3); | |
1726 | if (err < 0) | |
0f5b3e85 | 1727 | goto err2; |
7ec47496 | 1728 | |
ef00f89f | 1729 | master_h = nf_conntrack_find_get(net, zone, &master); |
7ec47496 PNA |
1730 | if (master_h == NULL) { |
1731 | err = -ENOENT; | |
0f5b3e85 | 1732 | goto err2; |
7ec47496 PNA |
1733 | } |
1734 | master_ct = nf_ct_tuplehash_to_ctrack(master_h); | |
f2a89004 | 1735 | __set_bit(IPS_EXPECTED_BIT, &ct->status); |
5faa1f4c | 1736 | ct->master = master_ct; |
f2a89004 | 1737 | } |
315c34da PNA |
1738 | tstamp = nf_conn_tstamp_find(ct); |
1739 | if (tstamp) | |
d2de875c | 1740 | tstamp->start = ktime_get_real_ns(); |
5faa1f4c | 1741 | |
7d367e06 JK |
1742 | err = nf_conntrack_hash_check_insert(ct); |
1743 | if (err < 0) | |
1744 | goto err2; | |
1745 | ||
58a3c9bb | 1746 | rcu_read_unlock(); |
dafc741c | 1747 | |
f0a3c086 | 1748 | return ct; |
c1d10adb | 1749 | |
0f5b3e85 PM |
1750 | err2: |
1751 | rcu_read_unlock(); | |
1752 | err1: | |
c1d10adb | 1753 | nf_conntrack_free(ct); |
f0a3c086 | 1754 | return ERR_PTR(err); |
c1d10adb PNA |
1755 | } |
1756 | ||
601e68e1 YH |
1757 | static int |
1758 | ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
1759 | const struct nlmsghdr *nlh, |
1760 | const struct nlattr * const cda[]) | |
c1d10adb | 1761 | { |
9592a5c0 | 1762 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1763 | struct nf_conntrack_tuple otuple, rtuple; |
1764 | struct nf_conntrack_tuple_hash *h = NULL; | |
96bcf938 | 1765 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
7d367e06 | 1766 | struct nf_conn *ct; |
c1d10adb | 1767 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
1768 | u16 zone; |
1769 | int err; | |
1770 | ||
1771 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); | |
1772 | if (err < 0) | |
1773 | return err; | |
c1d10adb | 1774 | |
df6fb868 | 1775 | if (cda[CTA_TUPLE_ORIG]) { |
c1d10adb PNA |
1776 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3); |
1777 | if (err < 0) | |
1778 | return err; | |
1779 | } | |
1780 | ||
df6fb868 | 1781 | if (cda[CTA_TUPLE_REPLY]) { |
c1d10adb PNA |
1782 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3); |
1783 | if (err < 0) | |
1784 | return err; | |
1785 | } | |
1786 | ||
df6fb868 | 1787 | if (cda[CTA_TUPLE_ORIG]) |
7d367e06 | 1788 | h = nf_conntrack_find_get(net, zone, &otuple); |
df6fb868 | 1789 | else if (cda[CTA_TUPLE_REPLY]) |
7d367e06 | 1790 | h = nf_conntrack_find_get(net, zone, &rtuple); |
c1d10adb PNA |
1791 | |
1792 | if (h == NULL) { | |
c1d10adb | 1793 | err = -ENOENT; |
f0a3c086 | 1794 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
fecc1133 | 1795 | enum ip_conntrack_events events; |
5faa1f4c | 1796 | |
442fad94 FW |
1797 | if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY]) |
1798 | return -EINVAL; | |
1799 | ||
ef00f89f | 1800 | ct = ctnetlink_create_conntrack(net, zone, cda, &otuple, |
f0a3c086 | 1801 | &rtuple, u3); |
7d367e06 JK |
1802 | if (IS_ERR(ct)) |
1803 | return PTR_ERR(ct); | |
1804 | ||
f0a3c086 | 1805 | err = 0; |
fecc1133 PNA |
1806 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) |
1807 | events = IPCT_RELATED; | |
1808 | else | |
1809 | events = IPCT_NEW; | |
1810 | ||
9b21f6a9 FW |
1811 | if (cda[CTA_LABELS] && |
1812 | ctnetlink_attach_labels(ct, cda) == 0) | |
1813 | events |= (1 << IPCT_LABEL); | |
1814 | ||
858b3133 PM |
1815 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1816 | (1 << IPCT_ASSURED) | | |
a0891aa6 PNA |
1817 | (1 << IPCT_HELPER) | |
1818 | (1 << IPCT_PROTOINFO) | | |
41d73ec0 | 1819 | (1 << IPCT_SEQADJ) | |
a0891aa6 | 1820 | (1 << IPCT_MARK) | events, |
15e47304 | 1821 | ct, NETLINK_CB(skb).portid, |
a0891aa6 | 1822 | nlmsg_report(nlh)); |
f0a3c086 | 1823 | nf_ct_put(ct); |
7d367e06 | 1824 | } |
5faa1f4c | 1825 | |
c1d10adb PNA |
1826 | return err; |
1827 | } | |
1828 | /* implicit 'else' */ | |
1829 | ||
c1d10adb | 1830 | err = -EEXIST; |
7d367e06 | 1831 | ct = nf_ct_tuplehash_to_ctrack(h); |
ff4ca827 | 1832 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) { |
ca7433df | 1833 | spin_lock_bh(&nf_conntrack_expect_lock); |
19abb7b0 | 1834 | err = ctnetlink_change_conntrack(ct, cda); |
ca7433df | 1835 | spin_unlock_bh(&nf_conntrack_expect_lock); |
19abb7b0 | 1836 | if (err == 0) { |
858b3133 PM |
1837 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1838 | (1 << IPCT_ASSURED) | | |
a0891aa6 | 1839 | (1 << IPCT_HELPER) | |
797a7d66 | 1840 | (1 << IPCT_LABEL) | |
a0891aa6 | 1841 | (1 << IPCT_PROTOINFO) | |
41d73ec0 | 1842 | (1 << IPCT_SEQADJ) | |
a0891aa6 | 1843 | (1 << IPCT_MARK), |
15e47304 | 1844 | ct, NETLINK_CB(skb).portid, |
a0891aa6 | 1845 | nlmsg_report(nlh)); |
7d367e06 | 1846 | } |
ff4ca827 | 1847 | } |
c1d10adb | 1848 | |
7d367e06 | 1849 | nf_ct_put(ct); |
c1d10adb PNA |
1850 | return err; |
1851 | } | |
1852 | ||
392025f8 | 1853 | static int |
15e47304 | 1854 | ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq, |
392025f8 PNA |
1855 | __u16 cpu, const struct ip_conntrack_stat *st) |
1856 | { | |
1857 | struct nlmsghdr *nlh; | |
1858 | struct nfgenmsg *nfmsg; | |
15e47304 | 1859 | unsigned int flags = portid ? NLM_F_MULTI : 0, event; |
392025f8 PNA |
1860 | |
1861 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU); | |
15e47304 | 1862 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
392025f8 PNA |
1863 | if (nlh == NULL) |
1864 | goto nlmsg_failure; | |
1865 | ||
1866 | nfmsg = nlmsg_data(nlh); | |
1867 | nfmsg->nfgen_family = AF_UNSPEC; | |
1868 | nfmsg->version = NFNETLINK_V0; | |
1869 | nfmsg->res_id = htons(cpu); | |
1870 | ||
1871 | if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) || | |
1872 | nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) || | |
1873 | nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) || | |
1874 | nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) || | |
1875 | nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) || | |
1876 | nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) || | |
1877 | nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) || | |
1878 | nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) || | |
1879 | nla_put_be32(skb, CTA_STATS_INSERT_FAILED, | |
1880 | htonl(st->insert_failed)) || | |
1881 | nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) || | |
1882 | nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) || | |
1883 | nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) || | |
1884 | nla_put_be32(skb, CTA_STATS_SEARCH_RESTART, | |
1885 | htonl(st->search_restart))) | |
1886 | goto nla_put_failure; | |
1887 | ||
1888 | nlmsg_end(skb, nlh); | |
1889 | return skb->len; | |
1890 | ||
1891 | nla_put_failure: | |
1892 | nlmsg_failure: | |
1893 | nlmsg_cancel(skb, nlh); | |
1894 | return -1; | |
1895 | } | |
1896 | ||
1897 | static int | |
1898 | ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
1899 | { | |
1900 | int cpu; | |
1901 | struct net *net = sock_net(skb->sk); | |
1902 | ||
1903 | if (cb->args[0] == nr_cpu_ids) | |
1904 | return 0; | |
1905 | ||
1906 | for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) { | |
1907 | const struct ip_conntrack_stat *st; | |
1908 | ||
1909 | if (!cpu_possible(cpu)) | |
1910 | continue; | |
1911 | ||
1912 | st = per_cpu_ptr(net->ct.stat, cpu); | |
1913 | if (ctnetlink_ct_stat_cpu_fill_info(skb, | |
15e47304 | 1914 | NETLINK_CB(cb->skb).portid, |
392025f8 PNA |
1915 | cb->nlh->nlmsg_seq, |
1916 | cpu, st) < 0) | |
1917 | break; | |
1918 | } | |
1919 | cb->args[0] = cpu; | |
1920 | ||
1921 | return skb->len; | |
1922 | } | |
1923 | ||
1924 | static int | |
1925 | ctnetlink_stat_ct_cpu(struct sock *ctnl, struct sk_buff *skb, | |
1926 | const struct nlmsghdr *nlh, | |
1927 | const struct nlattr * const cda[]) | |
1928 | { | |
1929 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1930 | struct netlink_dump_control c = { | |
1931 | .dump = ctnetlink_ct_stat_cpu_dump, | |
1932 | }; | |
1933 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
1934 | } | |
1935 | ||
1936 | return 0; | |
1937 | } | |
1938 | ||
1939 | static int | |
15e47304 | 1940 | ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, |
392025f8 PNA |
1941 | struct net *net) |
1942 | { | |
1943 | struct nlmsghdr *nlh; | |
1944 | struct nfgenmsg *nfmsg; | |
15e47304 | 1945 | unsigned int flags = portid ? NLM_F_MULTI : 0, event; |
392025f8 PNA |
1946 | unsigned int nr_conntracks = atomic_read(&net->ct.count); |
1947 | ||
1948 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS); | |
15e47304 | 1949 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
392025f8 PNA |
1950 | if (nlh == NULL) |
1951 | goto nlmsg_failure; | |
1952 | ||
1953 | nfmsg = nlmsg_data(nlh); | |
1954 | nfmsg->nfgen_family = AF_UNSPEC; | |
1955 | nfmsg->version = NFNETLINK_V0; | |
1956 | nfmsg->res_id = 0; | |
1957 | ||
1958 | if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks))) | |
1959 | goto nla_put_failure; | |
1960 | ||
1961 | nlmsg_end(skb, nlh); | |
1962 | return skb->len; | |
1963 | ||
1964 | nla_put_failure: | |
1965 | nlmsg_failure: | |
1966 | nlmsg_cancel(skb, nlh); | |
1967 | return -1; | |
1968 | } | |
1969 | ||
1970 | static int | |
1971 | ctnetlink_stat_ct(struct sock *ctnl, struct sk_buff *skb, | |
1972 | const struct nlmsghdr *nlh, | |
1973 | const struct nlattr * const cda[]) | |
1974 | { | |
1975 | struct sk_buff *skb2; | |
1976 | int err; | |
1977 | ||
1978 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
1979 | if (skb2 == NULL) | |
1980 | return -ENOMEM; | |
1981 | ||
15e47304 | 1982 | err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid, |
392025f8 PNA |
1983 | nlh->nlmsg_seq, |
1984 | NFNL_MSG_TYPE(nlh->nlmsg_type), | |
1985 | sock_net(skb->sk)); | |
1986 | if (err <= 0) | |
1987 | goto free; | |
1988 | ||
15e47304 | 1989 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT); |
392025f8 PNA |
1990 | if (err < 0) |
1991 | goto out; | |
1992 | ||
1993 | return 0; | |
1994 | ||
1995 | free: | |
1996 | kfree_skb(skb2); | |
1997 | out: | |
1998 | /* this avoids a loop in nfnetlink. */ | |
1999 | return err == -EAGAIN ? -ENOBUFS : err; | |
2000 | } | |
2001 | ||
bd077937 PNA |
2002 | static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = { |
2003 | [CTA_EXPECT_MASTER] = { .type = NLA_NESTED }, | |
2004 | [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED }, | |
2005 | [CTA_EXPECT_MASK] = { .type = NLA_NESTED }, | |
2006 | [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 }, | |
2007 | [CTA_EXPECT_ID] = { .type = NLA_U32 }, | |
2008 | [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING, | |
2009 | .len = NF_CT_HELPER_NAME_LEN - 1 }, | |
2010 | [CTA_EXPECT_ZONE] = { .type = NLA_U16 }, | |
2011 | [CTA_EXPECT_FLAGS] = { .type = NLA_U32 }, | |
2012 | [CTA_EXPECT_CLASS] = { .type = NLA_U32 }, | |
2013 | [CTA_EXPECT_NAT] = { .type = NLA_NESTED }, | |
2014 | [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING }, | |
2015 | }; | |
2016 | ||
2017 | static struct nf_conntrack_expect * | |
2018 | ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct, | |
2019 | struct nf_conntrack_helper *helper, | |
2020 | struct nf_conntrack_tuple *tuple, | |
2021 | struct nf_conntrack_tuple *mask); | |
2022 | ||
7c622345 | 2023 | #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT |
9cb01766 PNA |
2024 | static size_t |
2025 | ctnetlink_nfqueue_build_size(const struct nf_conn *ct) | |
2026 | { | |
2027 | return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ | |
2028 | + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ | |
2029 | + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ | |
2030 | + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ | |
2031 | + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ | |
2032 | + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ | |
2033 | + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ | |
2034 | + nla_total_size(0) /* CTA_PROTOINFO */ | |
2035 | + nla_total_size(0) /* CTA_HELP */ | |
2036 | + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ | |
2037 | + ctnetlink_secctx_size(ct) | |
2038 | #ifdef CONFIG_NF_NAT_NEEDED | |
2039 | + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ | |
2040 | + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ | |
2041 | #endif | |
2042 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
2043 | + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ | |
4a001068 KM |
2044 | #endif |
2045 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
2046 | + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE */ | |
9cb01766 PNA |
2047 | #endif |
2048 | + ctnetlink_proto_size(ct) | |
2049 | ; | |
2050 | } | |
2051 | ||
2052 | static int | |
2053 | ctnetlink_nfqueue_build(struct sk_buff *skb, struct nf_conn *ct) | |
2054 | { | |
2055 | struct nlattr *nest_parms; | |
2056 | ||
2057 | rcu_read_lock(); | |
2058 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); | |
2059 | if (!nest_parms) | |
2060 | goto nla_put_failure; | |
2061 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) | |
2062 | goto nla_put_failure; | |
2063 | nla_nest_end(skb, nest_parms); | |
2064 | ||
2065 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); | |
2066 | if (!nest_parms) | |
2067 | goto nla_put_failure; | |
2068 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) | |
2069 | goto nla_put_failure; | |
2070 | nla_nest_end(skb, nest_parms); | |
2071 | ||
2072 | if (nf_ct_zone(ct)) { | |
2073 | if (nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct)))) | |
2074 | goto nla_put_failure; | |
2075 | } | |
2076 | ||
2077 | if (ctnetlink_dump_id(skb, ct) < 0) | |
2078 | goto nla_put_failure; | |
2079 | ||
2080 | if (ctnetlink_dump_status(skb, ct) < 0) | |
2081 | goto nla_put_failure; | |
2082 | ||
2083 | if (ctnetlink_dump_timeout(skb, ct) < 0) | |
2084 | goto nla_put_failure; | |
2085 | ||
2086 | if (ctnetlink_dump_protoinfo(skb, ct) < 0) | |
2087 | goto nla_put_failure; | |
2088 | ||
2089 | if (ctnetlink_dump_helpinfo(skb, ct) < 0) | |
2090 | goto nla_put_failure; | |
2091 | ||
2092 | #ifdef CONFIG_NF_CONNTRACK_SECMARK | |
2093 | if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0) | |
2094 | goto nla_put_failure; | |
2095 | #endif | |
2096 | if (ct->master && ctnetlink_dump_master(skb, ct) < 0) | |
2097 | goto nla_put_failure; | |
2098 | ||
2099 | if ((ct->status & IPS_SEQ_ADJUST) && | |
41d73ec0 | 2100 | ctnetlink_dump_ct_seq_adj(skb, ct) < 0) |
9cb01766 PNA |
2101 | goto nla_put_failure; |
2102 | ||
2103 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
2104 | if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0) | |
2105 | goto nla_put_failure; | |
2106 | #endif | |
0ceabd83 FW |
2107 | if (ctnetlink_dump_labels(skb, ct) < 0) |
2108 | goto nla_put_failure; | |
9cb01766 PNA |
2109 | rcu_read_unlock(); |
2110 | return 0; | |
2111 | ||
2112 | nla_put_failure: | |
2113 | rcu_read_unlock(); | |
2114 | return -ENOSPC; | |
2115 | } | |
2116 | ||
2117 | static int | |
2118 | ctnetlink_nfqueue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct) | |
2119 | { | |
2120 | int err; | |
2121 | ||
2122 | if (cda[CTA_TIMEOUT]) { | |
2123 | err = ctnetlink_change_timeout(ct, cda); | |
2124 | if (err < 0) | |
2125 | return err; | |
2126 | } | |
2127 | if (cda[CTA_STATUS]) { | |
2128 | err = ctnetlink_change_status(ct, cda); | |
2129 | if (err < 0) | |
2130 | return err; | |
2131 | } | |
2132 | if (cda[CTA_HELP]) { | |
2133 | err = ctnetlink_change_helper(ct, cda); | |
2134 | if (err < 0) | |
2135 | return err; | |
2136 | } | |
9b21f6a9 FW |
2137 | if (cda[CTA_LABELS]) { |
2138 | err = ctnetlink_attach_labels(ct, cda); | |
2139 | if (err < 0) | |
2140 | return err; | |
2141 | } | |
9cb01766 | 2142 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
534473c6 FW |
2143 | if (cda[CTA_MARK]) { |
2144 | u32 mask = 0, mark, newmark; | |
2145 | if (cda[CTA_MARK_MASK]) | |
2146 | mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK])); | |
2147 | ||
2148 | mark = ntohl(nla_get_be32(cda[CTA_MARK])); | |
2149 | newmark = (ct->mark & mask) ^ mark; | |
2150 | if (newmark != ct->mark) | |
2151 | ct->mark = newmark; | |
2152 | } | |
9cb01766 PNA |
2153 | #endif |
2154 | return 0; | |
2155 | } | |
2156 | ||
2157 | static int | |
2158 | ctnetlink_nfqueue_parse(const struct nlattr *attr, struct nf_conn *ct) | |
2159 | { | |
2160 | struct nlattr *cda[CTA_MAX+1]; | |
68e035c9 | 2161 | int ret; |
9cb01766 | 2162 | |
130ffbc2 DB |
2163 | ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy); |
2164 | if (ret < 0) | |
2165 | return ret; | |
9cb01766 | 2166 | |
ca7433df | 2167 | spin_lock_bh(&nf_conntrack_expect_lock); |
68e035c9 | 2168 | ret = ctnetlink_nfqueue_parse_ct((const struct nlattr **)cda, ct); |
ca7433df | 2169 | spin_unlock_bh(&nf_conntrack_expect_lock); |
68e035c9 PNA |
2170 | |
2171 | return ret; | |
9cb01766 PNA |
2172 | } |
2173 | ||
bd077937 PNA |
2174 | static int ctnetlink_nfqueue_exp_parse(const struct nlattr * const *cda, |
2175 | const struct nf_conn *ct, | |
2176 | struct nf_conntrack_tuple *tuple, | |
2177 | struct nf_conntrack_tuple *mask) | |
2178 | { | |
2179 | int err; | |
2180 | ||
2181 | err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE, | |
2182 | nf_ct_l3num(ct)); | |
2183 | if (err < 0) | |
2184 | return err; | |
2185 | ||
2186 | return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK, | |
2187 | nf_ct_l3num(ct)); | |
2188 | } | |
2189 | ||
2190 | static int | |
2191 | ctnetlink_nfqueue_attach_expect(const struct nlattr *attr, struct nf_conn *ct, | |
2192 | u32 portid, u32 report) | |
2193 | { | |
2194 | struct nlattr *cda[CTA_EXPECT_MAX+1]; | |
2195 | struct nf_conntrack_tuple tuple, mask; | |
b7e092c0 | 2196 | struct nf_conntrack_helper *helper = NULL; |
bd077937 PNA |
2197 | struct nf_conntrack_expect *exp; |
2198 | int err; | |
2199 | ||
2200 | err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy); | |
2201 | if (err < 0) | |
2202 | return err; | |
2203 | ||
2204 | err = ctnetlink_nfqueue_exp_parse((const struct nlattr * const *)cda, | |
2205 | ct, &tuple, &mask); | |
2206 | if (err < 0) | |
2207 | return err; | |
2208 | ||
2209 | if (cda[CTA_EXPECT_HELP_NAME]) { | |
2210 | const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
2211 | ||
2212 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), | |
2213 | nf_ct_protonum(ct)); | |
2214 | if (helper == NULL) | |
2215 | return -EOPNOTSUPP; | |
2216 | } | |
2217 | ||
2218 | exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct, | |
2219 | helper, &tuple, &mask); | |
2220 | if (IS_ERR(exp)) | |
2221 | return PTR_ERR(exp); | |
2222 | ||
2223 | err = nf_ct_expect_related_report(exp, portid, report); | |
2224 | if (err < 0) { | |
2225 | nf_ct_expect_put(exp); | |
2226 | return err; | |
2227 | } | |
2228 | ||
2229 | return 0; | |
2230 | } | |
2231 | ||
9cb01766 PNA |
2232 | static struct nfq_ct_hook ctnetlink_nfqueue_hook = { |
2233 | .build_size = ctnetlink_nfqueue_build_size, | |
2234 | .build = ctnetlink_nfqueue_build, | |
2235 | .parse = ctnetlink_nfqueue_parse, | |
bd077937 | 2236 | .attach_expect = ctnetlink_nfqueue_attach_expect, |
41d73ec0 | 2237 | .seq_adjust = nf_ct_tcp_seqadj_set, |
9cb01766 | 2238 | }; |
7c622345 | 2239 | #endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */ |
9cb01766 | 2240 | |
601e68e1 YH |
2241 | /*********************************************************************** |
2242 | * EXPECT | |
2243 | ***********************************************************************/ | |
c1d10adb PNA |
2244 | |
2245 | static inline int | |
2246 | ctnetlink_exp_dump_tuple(struct sk_buff *skb, | |
2247 | const struct nf_conntrack_tuple *tuple, | |
2248 | enum ctattr_expect type) | |
2249 | { | |
df6fb868 | 2250 | struct nlattr *nest_parms; |
601e68e1 | 2251 | |
df6fb868 PM |
2252 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
2253 | if (!nest_parms) | |
2254 | goto nla_put_failure; | |
c1d10adb | 2255 | if (ctnetlink_dump_tuples(skb, tuple) < 0) |
df6fb868 PM |
2256 | goto nla_put_failure; |
2257 | nla_nest_end(skb, nest_parms); | |
c1d10adb PNA |
2258 | |
2259 | return 0; | |
2260 | ||
df6fb868 | 2261 | nla_put_failure: |
c1d10adb | 2262 | return -1; |
601e68e1 | 2263 | } |
c1d10adb | 2264 | |
1cde6436 PNA |
2265 | static inline int |
2266 | ctnetlink_exp_dump_mask(struct sk_buff *skb, | |
2267 | const struct nf_conntrack_tuple *tuple, | |
d4156e8c | 2268 | const struct nf_conntrack_tuple_mask *mask) |
1cde6436 PNA |
2269 | { |
2270 | int ret; | |
2271 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 2272 | struct nf_conntrack_l4proto *l4proto; |
d4156e8c | 2273 | struct nf_conntrack_tuple m; |
df6fb868 | 2274 | struct nlattr *nest_parms; |
d4156e8c PM |
2275 | |
2276 | memset(&m, 0xFF, sizeof(m)); | |
d4156e8c | 2277 | memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3)); |
e578756c PM |
2278 | m.src.u.all = mask->src.u.all; |
2279 | m.dst.protonum = tuple->dst.protonum; | |
d4156e8c | 2280 | |
df6fb868 PM |
2281 | nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED); |
2282 | if (!nest_parms) | |
2283 | goto nla_put_failure; | |
1cde6436 | 2284 | |
3b988ece | 2285 | rcu_read_lock(); |
528a3a6f | 2286 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
d4156e8c | 2287 | ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto); |
3b988ece HS |
2288 | if (ret >= 0) { |
2289 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, | |
2290 | tuple->dst.protonum); | |
d4156e8c | 2291 | ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto); |
3b988ece HS |
2292 | } |
2293 | rcu_read_unlock(); | |
2294 | ||
1cde6436 | 2295 | if (unlikely(ret < 0)) |
df6fb868 | 2296 | goto nla_put_failure; |
1cde6436 | 2297 | |
df6fb868 | 2298 | nla_nest_end(skb, nest_parms); |
1cde6436 PNA |
2299 | |
2300 | return 0; | |
2301 | ||
df6fb868 | 2302 | nla_put_failure: |
1cde6436 PNA |
2303 | return -1; |
2304 | } | |
2305 | ||
c7232c99 PM |
2306 | static const union nf_inet_addr any_addr; |
2307 | ||
bb5cf80e | 2308 | static int |
c1d10adb | 2309 | ctnetlink_exp_dump_expect(struct sk_buff *skb, |
601e68e1 | 2310 | const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
2311 | { |
2312 | struct nf_conn *master = exp->master; | |
c1216382 | 2313 | long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ; |
bc01befd | 2314 | struct nf_conn_help *help; |
076a0ca0 PNA |
2315 | #ifdef CONFIG_NF_NAT_NEEDED |
2316 | struct nlattr *nest_parms; | |
2317 | struct nf_conntrack_tuple nat_tuple = {}; | |
2318 | #endif | |
544d5c7d PNA |
2319 | struct nf_ct_helper_expectfn *expfn; |
2320 | ||
d978e5da PM |
2321 | if (timeout < 0) |
2322 | timeout = 0; | |
c1d10adb PNA |
2323 | |
2324 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) | |
df6fb868 | 2325 | goto nla_put_failure; |
1cde6436 | 2326 | if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) |
df6fb868 | 2327 | goto nla_put_failure; |
c1d10adb PNA |
2328 | if (ctnetlink_exp_dump_tuple(skb, |
2329 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
2330 | CTA_EXPECT_MASTER) < 0) | |
df6fb868 | 2331 | goto nla_put_failure; |
601e68e1 | 2332 | |
076a0ca0 | 2333 | #ifdef CONFIG_NF_NAT_NEEDED |
c7232c99 PM |
2334 | if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) || |
2335 | exp->saved_proto.all) { | |
076a0ca0 PNA |
2336 | nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED); |
2337 | if (!nest_parms) | |
2338 | goto nla_put_failure; | |
2339 | ||
cc1eb431 DM |
2340 | if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir))) |
2341 | goto nla_put_failure; | |
076a0ca0 PNA |
2342 | |
2343 | nat_tuple.src.l3num = nf_ct_l3num(master); | |
c7232c99 | 2344 | nat_tuple.src.u3 = exp->saved_addr; |
076a0ca0 PNA |
2345 | nat_tuple.dst.protonum = nf_ct_protonum(master); |
2346 | nat_tuple.src.u = exp->saved_proto; | |
2347 | ||
2348 | if (ctnetlink_exp_dump_tuple(skb, &nat_tuple, | |
2349 | CTA_EXPECT_NAT_TUPLE) < 0) | |
2350 | goto nla_put_failure; | |
2351 | nla_nest_end(skb, nest_parms); | |
2352 | } | |
2353 | #endif | |
cc1eb431 DM |
2354 | if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) || |
2355 | nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) || | |
2356 | nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) || | |
2357 | nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class))) | |
2358 | goto nla_put_failure; | |
bc01befd PNA |
2359 | help = nfct_help(master); |
2360 | if (help) { | |
2361 | struct nf_conntrack_helper *helper; | |
2362 | ||
2363 | helper = rcu_dereference(help->helper); | |
cc1eb431 DM |
2364 | if (helper && |
2365 | nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name)) | |
2366 | goto nla_put_failure; | |
bc01befd | 2367 | } |
544d5c7d | 2368 | expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn); |
cc1eb431 DM |
2369 | if (expfn != NULL && |
2370 | nla_put_string(skb, CTA_EXPECT_FN, expfn->name)) | |
2371 | goto nla_put_failure; | |
c1d10adb PNA |
2372 | |
2373 | return 0; | |
601e68e1 | 2374 | |
df6fb868 | 2375 | nla_put_failure: |
c1d10adb PNA |
2376 | return -1; |
2377 | } | |
2378 | ||
2379 | static int | |
15e47304 | 2380 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq, |
8b0a231d | 2381 | int event, const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
2382 | { |
2383 | struct nlmsghdr *nlh; | |
2384 | struct nfgenmsg *nfmsg; | |
15e47304 | 2385 | unsigned int flags = portid ? NLM_F_MULTI : 0; |
c1d10adb PNA |
2386 | |
2387 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; | |
15e47304 | 2388 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
96bcf938 PNA |
2389 | if (nlh == NULL) |
2390 | goto nlmsg_failure; | |
c1d10adb | 2391 | |
96bcf938 | 2392 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
2393 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
2394 | nfmsg->version = NFNETLINK_V0; | |
2395 | nfmsg->res_id = 0; | |
2396 | ||
2397 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) | |
df6fb868 | 2398 | goto nla_put_failure; |
c1d10adb | 2399 | |
96bcf938 | 2400 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
2401 | return skb->len; |
2402 | ||
2403 | nlmsg_failure: | |
df6fb868 | 2404 | nla_put_failure: |
96bcf938 | 2405 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
2406 | return -1; |
2407 | } | |
2408 | ||
2409 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
e34d5c1a PNA |
2410 | static int |
2411 | ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item) | |
c1d10adb | 2412 | { |
9592a5c0 AD |
2413 | struct nf_conntrack_expect *exp = item->exp; |
2414 | struct net *net = nf_ct_exp_net(exp); | |
c1d10adb PNA |
2415 | struct nlmsghdr *nlh; |
2416 | struct nfgenmsg *nfmsg; | |
c1d10adb | 2417 | struct sk_buff *skb; |
ebbf41df | 2418 | unsigned int type, group; |
c1d10adb PNA |
2419 | int flags = 0; |
2420 | ||
ebbf41df PNA |
2421 | if (events & (1 << IPEXP_DESTROY)) { |
2422 | type = IPCTNL_MSG_EXP_DELETE; | |
2423 | group = NFNLGRP_CONNTRACK_EXP_DESTROY; | |
2424 | } else if (events & (1 << IPEXP_NEW)) { | |
c1d10adb PNA |
2425 | type = IPCTNL_MSG_EXP_NEW; |
2426 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
ebbf41df | 2427 | group = NFNLGRP_CONNTRACK_EXP_NEW; |
c1d10adb | 2428 | } else |
e34d5c1a | 2429 | return 0; |
c1d10adb | 2430 | |
ebbf41df | 2431 | if (!item->report && !nfnetlink_has_listeners(net, group)) |
e34d5c1a | 2432 | return 0; |
b3a27bfb | 2433 | |
96bcf938 PNA |
2434 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
2435 | if (skb == NULL) | |
150ace0d | 2436 | goto errout; |
c1d10adb | 2437 | |
b633ad5f | 2438 | type |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
15e47304 | 2439 | nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags); |
96bcf938 PNA |
2440 | if (nlh == NULL) |
2441 | goto nlmsg_failure; | |
c1d10adb | 2442 | |
96bcf938 | 2443 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
2444 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
2445 | nfmsg->version = NFNETLINK_V0; | |
2446 | nfmsg->res_id = 0; | |
2447 | ||
528a3a6f | 2448 | rcu_read_lock(); |
c1d10adb | 2449 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
df6fb868 | 2450 | goto nla_put_failure; |
528a3a6f | 2451 | rcu_read_unlock(); |
c1d10adb | 2452 | |
96bcf938 | 2453 | nlmsg_end(skb, nlh); |
15e47304 | 2454 | nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC); |
e34d5c1a | 2455 | return 0; |
c1d10adb | 2456 | |
df6fb868 | 2457 | nla_put_failure: |
528a3a6f | 2458 | rcu_read_unlock(); |
96bcf938 | 2459 | nlmsg_cancel(skb, nlh); |
528a3a6f | 2460 | nlmsg_failure: |
c1d10adb | 2461 | kfree_skb(skb); |
150ace0d | 2462 | errout: |
9592a5c0 | 2463 | nfnetlink_set_err(net, 0, 0, -ENOBUFS); |
e34d5c1a | 2464 | return 0; |
c1d10adb PNA |
2465 | } |
2466 | #endif | |
cf6994c2 PM |
2467 | static int ctnetlink_exp_done(struct netlink_callback *cb) |
2468 | { | |
31f15875 PM |
2469 | if (cb->args[1]) |
2470 | nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]); | |
cf6994c2 PM |
2471 | return 0; |
2472 | } | |
c1d10adb PNA |
2473 | |
2474 | static int | |
2475 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
2476 | { | |
9592a5c0 | 2477 | struct net *net = sock_net(skb->sk); |
cf6994c2 | 2478 | struct nf_conntrack_expect *exp, *last; |
96bcf938 | 2479 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
87711cb8 | 2480 | u_int8_t l3proto = nfmsg->nfgen_family; |
c1d10adb | 2481 | |
7d0742da | 2482 | rcu_read_lock(); |
31f15875 PM |
2483 | last = (struct nf_conntrack_expect *)cb->args[1]; |
2484 | for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) { | |
cf6994c2 | 2485 | restart: |
b67bfe0d | 2486 | hlist_for_each_entry(exp, &net->ct.expect_hash[cb->args[0]], |
31f15875 PM |
2487 | hnode) { |
2488 | if (l3proto && exp->tuple.src.l3num != l3proto) | |
cf6994c2 | 2489 | continue; |
31f15875 PM |
2490 | if (cb->args[1]) { |
2491 | if (exp != last) | |
2492 | continue; | |
2493 | cb->args[1] = 0; | |
2494 | } | |
8b0a231d | 2495 | if (ctnetlink_exp_fill_info(skb, |
15e47304 | 2496 | NETLINK_CB(cb->skb).portid, |
31f15875 PM |
2497 | cb->nlh->nlmsg_seq, |
2498 | IPCTNL_MSG_EXP_NEW, | |
8b0a231d | 2499 | exp) < 0) { |
7d0742da PM |
2500 | if (!atomic_inc_not_zero(&exp->use)) |
2501 | continue; | |
31f15875 PM |
2502 | cb->args[1] = (unsigned long)exp; |
2503 | goto out; | |
2504 | } | |
cf6994c2 | 2505 | } |
31f15875 PM |
2506 | if (cb->args[1]) { |
2507 | cb->args[1] = 0; | |
2508 | goto restart; | |
cf6994c2 PM |
2509 | } |
2510 | } | |
601e68e1 | 2511 | out: |
7d0742da | 2512 | rcu_read_unlock(); |
cf6994c2 PM |
2513 | if (last) |
2514 | nf_ct_expect_put(last); | |
c1d10adb | 2515 | |
c1d10adb PNA |
2516 | return skb->len; |
2517 | } | |
2518 | ||
e844a928 PNA |
2519 | static int |
2520 | ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
2521 | { | |
2522 | struct nf_conntrack_expect *exp, *last; | |
2523 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
2524 | struct nf_conn *ct = cb->data; | |
2525 | struct nf_conn_help *help = nfct_help(ct); | |
2526 | u_int8_t l3proto = nfmsg->nfgen_family; | |
2527 | ||
2528 | if (cb->args[0]) | |
2529 | return 0; | |
2530 | ||
2531 | rcu_read_lock(); | |
2532 | last = (struct nf_conntrack_expect *)cb->args[1]; | |
2533 | restart: | |
2534 | hlist_for_each_entry(exp, &help->expectations, lnode) { | |
2535 | if (l3proto && exp->tuple.src.l3num != l3proto) | |
2536 | continue; | |
2537 | if (cb->args[1]) { | |
2538 | if (exp != last) | |
2539 | continue; | |
2540 | cb->args[1] = 0; | |
2541 | } | |
2542 | if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid, | |
2543 | cb->nlh->nlmsg_seq, | |
2544 | IPCTNL_MSG_EXP_NEW, | |
2545 | exp) < 0) { | |
2546 | if (!atomic_inc_not_zero(&exp->use)) | |
2547 | continue; | |
2548 | cb->args[1] = (unsigned long)exp; | |
2549 | goto out; | |
2550 | } | |
2551 | } | |
2552 | if (cb->args[1]) { | |
2553 | cb->args[1] = 0; | |
2554 | goto restart; | |
2555 | } | |
2556 | cb->args[0] = 1; | |
2557 | out: | |
2558 | rcu_read_unlock(); | |
2559 | if (last) | |
2560 | nf_ct_expect_put(last); | |
2561 | ||
2562 | return skb->len; | |
2563 | } | |
2564 | ||
2565 | static int ctnetlink_dump_exp_ct(struct sock *ctnl, struct sk_buff *skb, | |
2566 | const struct nlmsghdr *nlh, | |
2567 | const struct nlattr * const cda[]) | |
2568 | { | |
2569 | int err; | |
2570 | struct net *net = sock_net(ctnl); | |
2571 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
2572 | u_int8_t u3 = nfmsg->nfgen_family; | |
2573 | struct nf_conntrack_tuple tuple; | |
2574 | struct nf_conntrack_tuple_hash *h; | |
2575 | struct nf_conn *ct; | |
2576 | u16 zone = 0; | |
2577 | struct netlink_dump_control c = { | |
2578 | .dump = ctnetlink_exp_ct_dump_table, | |
2579 | .done = ctnetlink_exp_done, | |
2580 | }; | |
2581 | ||
2582 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3); | |
2583 | if (err < 0) | |
2584 | return err; | |
2585 | ||
2586 | if (cda[CTA_EXPECT_ZONE]) { | |
2587 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); | |
2588 | if (err < 0) | |
2589 | return err; | |
2590 | } | |
2591 | ||
2592 | h = nf_conntrack_find_get(net, zone, &tuple); | |
2593 | if (!h) | |
2594 | return -ENOENT; | |
2595 | ||
2596 | ct = nf_ct_tuplehash_to_ctrack(h); | |
2597 | c.data = ct; | |
2598 | ||
2599 | err = netlink_dump_start(ctnl, skb, nlh, &c); | |
2600 | nf_ct_put(ct); | |
2601 | ||
2602 | return err; | |
2603 | } | |
2604 | ||
c1d10adb | 2605 | static int |
601e68e1 | 2606 | ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
2607 | const struct nlmsghdr *nlh, |
2608 | const struct nlattr * const cda[]) | |
c1d10adb | 2609 | { |
9592a5c0 | 2610 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
2611 | struct nf_conntrack_tuple tuple; |
2612 | struct nf_conntrack_expect *exp; | |
2613 | struct sk_buff *skb2; | |
96bcf938 | 2614 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 2615 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
2616 | u16 zone; |
2617 | int err; | |
c1d10adb | 2618 | |
b8f3ab42 | 2619 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
e844a928 PNA |
2620 | if (cda[CTA_EXPECT_MASTER]) |
2621 | return ctnetlink_dump_exp_ct(ctnl, skb, nlh, cda); | |
2622 | else { | |
2623 | struct netlink_dump_control c = { | |
2624 | .dump = ctnetlink_exp_dump_table, | |
2625 | .done = ctnetlink_exp_done, | |
2626 | }; | |
2627 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
2628 | } | |
c1d10adb PNA |
2629 | } |
2630 | ||
ef00f89f PM |
2631 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2632 | if (err < 0) | |
2633 | return err; | |
2634 | ||
35dba1d7 PNA |
2635 | if (cda[CTA_EXPECT_TUPLE]) |
2636 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
2637 | else if (cda[CTA_EXPECT_MASTER]) | |
c1d10adb PNA |
2638 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3); |
2639 | else | |
2640 | return -EINVAL; | |
2641 | ||
2642 | if (err < 0) | |
2643 | return err; | |
2644 | ||
ef00f89f | 2645 | exp = nf_ct_expect_find_get(net, zone, &tuple); |
c1d10adb PNA |
2646 | if (!exp) |
2647 | return -ENOENT; | |
2648 | ||
df6fb868 | 2649 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 2650 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 2651 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 2652 | nf_ct_expect_put(exp); |
c1d10adb PNA |
2653 | return -ENOENT; |
2654 | } | |
601e68e1 | 2655 | } |
c1d10adb PNA |
2656 | |
2657 | err = -ENOMEM; | |
96bcf938 | 2658 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
81378f72 PNA |
2659 | if (skb2 == NULL) { |
2660 | nf_ct_expect_put(exp); | |
c1d10adb | 2661 | goto out; |
81378f72 | 2662 | } |
4e9b8269 | 2663 | |
528a3a6f | 2664 | rcu_read_lock(); |
15e47304 | 2665 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid, |
8b0a231d | 2666 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp); |
528a3a6f | 2667 | rcu_read_unlock(); |
81378f72 | 2668 | nf_ct_expect_put(exp); |
c1d10adb PNA |
2669 | if (err <= 0) |
2670 | goto free; | |
2671 | ||
15e47304 | 2672 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT); |
81378f72 PNA |
2673 | if (err < 0) |
2674 | goto out; | |
c1d10adb | 2675 | |
81378f72 | 2676 | return 0; |
c1d10adb PNA |
2677 | |
2678 | free: | |
2679 | kfree_skb(skb2); | |
2680 | out: | |
81378f72 PNA |
2681 | /* this avoids a loop in nfnetlink. */ |
2682 | return err == -EAGAIN ? -ENOBUFS : err; | |
c1d10adb PNA |
2683 | } |
2684 | ||
2685 | static int | |
601e68e1 | 2686 | ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
2687 | const struct nlmsghdr *nlh, |
2688 | const struct nlattr * const cda[]) | |
c1d10adb | 2689 | { |
9592a5c0 | 2690 | struct net *net = sock_net(ctnl); |
31f15875 | 2691 | struct nf_conntrack_expect *exp; |
c1d10adb | 2692 | struct nf_conntrack_tuple tuple; |
96bcf938 | 2693 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
b67bfe0d | 2694 | struct hlist_node *next; |
c1d10adb | 2695 | u_int8_t u3 = nfmsg->nfgen_family; |
31f15875 | 2696 | unsigned int i; |
ef00f89f | 2697 | u16 zone; |
c1d10adb PNA |
2698 | int err; |
2699 | ||
df6fb868 | 2700 | if (cda[CTA_EXPECT_TUPLE]) { |
c1d10adb | 2701 | /* delete a single expect by tuple */ |
ef00f89f PM |
2702 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2703 | if (err < 0) | |
2704 | return err; | |
2705 | ||
c1d10adb PNA |
2706 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
2707 | if (err < 0) | |
2708 | return err; | |
2709 | ||
2710 | /* bump usage count to 2 */ | |
ef00f89f | 2711 | exp = nf_ct_expect_find_get(net, zone, &tuple); |
c1d10adb PNA |
2712 | if (!exp) |
2713 | return -ENOENT; | |
2714 | ||
df6fb868 | 2715 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 2716 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 2717 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 2718 | nf_ct_expect_put(exp); |
c1d10adb PNA |
2719 | return -ENOENT; |
2720 | } | |
2721 | } | |
2722 | ||
2723 | /* after list removal, usage count == 1 */ | |
ca7433df | 2724 | spin_lock_bh(&nf_conntrack_expect_lock); |
ebbf41df | 2725 | if (del_timer(&exp->timeout)) { |
15e47304 | 2726 | nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid, |
ebbf41df PNA |
2727 | nlmsg_report(nlh)); |
2728 | nf_ct_expect_put(exp); | |
2729 | } | |
ca7433df | 2730 | spin_unlock_bh(&nf_conntrack_expect_lock); |
601e68e1 | 2731 | /* have to put what we 'get' above. |
c1d10adb | 2732 | * after this line usage count == 0 */ |
6823645d | 2733 | nf_ct_expect_put(exp); |
df6fb868 PM |
2734 | } else if (cda[CTA_EXPECT_HELP_NAME]) { |
2735 | char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
31f15875 | 2736 | struct nf_conn_help *m_help; |
c1d10adb PNA |
2737 | |
2738 | /* delete all expectations for this helper */ | |
ca7433df | 2739 | spin_lock_bh(&nf_conntrack_expect_lock); |
31f15875 | 2740 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
b67bfe0d | 2741 | hlist_for_each_entry_safe(exp, next, |
9592a5c0 | 2742 | &net->ct.expect_hash[i], |
31f15875 PM |
2743 | hnode) { |
2744 | m_help = nfct_help(exp->master); | |
794e6871 PM |
2745 | if (!strcmp(m_help->helper->name, name) && |
2746 | del_timer(&exp->timeout)) { | |
ebbf41df | 2747 | nf_ct_unlink_expect_report(exp, |
15e47304 | 2748 | NETLINK_CB(skb).portid, |
ebbf41df | 2749 | nlmsg_report(nlh)); |
31f15875 PM |
2750 | nf_ct_expect_put(exp); |
2751 | } | |
c1d10adb PNA |
2752 | } |
2753 | } | |
ca7433df | 2754 | spin_unlock_bh(&nf_conntrack_expect_lock); |
c1d10adb PNA |
2755 | } else { |
2756 | /* This basically means we have to flush everything*/ | |
ca7433df | 2757 | spin_lock_bh(&nf_conntrack_expect_lock); |
31f15875 | 2758 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
b67bfe0d | 2759 | hlist_for_each_entry_safe(exp, next, |
9592a5c0 | 2760 | &net->ct.expect_hash[i], |
31f15875 PM |
2761 | hnode) { |
2762 | if (del_timer(&exp->timeout)) { | |
ebbf41df | 2763 | nf_ct_unlink_expect_report(exp, |
15e47304 | 2764 | NETLINK_CB(skb).portid, |
ebbf41df | 2765 | nlmsg_report(nlh)); |
31f15875 PM |
2766 | nf_ct_expect_put(exp); |
2767 | } | |
c1d10adb PNA |
2768 | } |
2769 | } | |
ca7433df | 2770 | spin_unlock_bh(&nf_conntrack_expect_lock); |
c1d10adb PNA |
2771 | } |
2772 | ||
2773 | return 0; | |
2774 | } | |
2775 | static int | |
39938324 PM |
2776 | ctnetlink_change_expect(struct nf_conntrack_expect *x, |
2777 | const struct nlattr * const cda[]) | |
c1d10adb | 2778 | { |
9768e1ac KW |
2779 | if (cda[CTA_EXPECT_TIMEOUT]) { |
2780 | if (!del_timer(&x->timeout)) | |
2781 | return -ETIME; | |
2782 | ||
2783 | x->timeout.expires = jiffies + | |
2784 | ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ; | |
2785 | add_timer(&x->timeout); | |
2786 | } | |
2787 | return 0; | |
c1d10adb PNA |
2788 | } |
2789 | ||
076a0ca0 PNA |
2790 | static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = { |
2791 | [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 }, | |
2792 | [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED }, | |
2793 | }; | |
2794 | ||
2795 | static int | |
2796 | ctnetlink_parse_expect_nat(const struct nlattr *attr, | |
2797 | struct nf_conntrack_expect *exp, | |
2798 | u_int8_t u3) | |
2799 | { | |
2800 | #ifdef CONFIG_NF_NAT_NEEDED | |
2801 | struct nlattr *tb[CTA_EXPECT_NAT_MAX+1]; | |
2802 | struct nf_conntrack_tuple nat_tuple = {}; | |
2803 | int err; | |
2804 | ||
130ffbc2 DB |
2805 | err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy); |
2806 | if (err < 0) | |
2807 | return err; | |
076a0ca0 PNA |
2808 | |
2809 | if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE]) | |
2810 | return -EINVAL; | |
2811 | ||
2812 | err = ctnetlink_parse_tuple((const struct nlattr * const *)tb, | |
2813 | &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3); | |
2814 | if (err < 0) | |
2815 | return err; | |
2816 | ||
c7232c99 | 2817 | exp->saved_addr = nat_tuple.src.u3; |
076a0ca0 PNA |
2818 | exp->saved_proto = nat_tuple.src.u; |
2819 | exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR])); | |
2820 | ||
2821 | return 0; | |
2822 | #else | |
2823 | return -EOPNOTSUPP; | |
2824 | #endif | |
2825 | } | |
2826 | ||
0ef71ee1 PNA |
2827 | static struct nf_conntrack_expect * |
2828 | ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct, | |
2829 | struct nf_conntrack_helper *helper, | |
2830 | struct nf_conntrack_tuple *tuple, | |
2831 | struct nf_conntrack_tuple *mask) | |
c1d10adb | 2832 | { |
0ef71ee1 | 2833 | u_int32_t class = 0; |
c1d10adb | 2834 | struct nf_conntrack_expect *exp; |
dc808fe2 | 2835 | struct nf_conn_help *help; |
0ef71ee1 | 2836 | int err; |
660fdb2a | 2837 | |
b8c5e52c PNA |
2838 | if (cda[CTA_EXPECT_CLASS] && helper) { |
2839 | class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS])); | |
0ef71ee1 PNA |
2840 | if (class > helper->expect_class_max) |
2841 | return ERR_PTR(-EINVAL); | |
b8c5e52c | 2842 | } |
6823645d | 2843 | exp = nf_ct_expect_alloc(ct); |
0ef71ee1 PNA |
2844 | if (!exp) |
2845 | return ERR_PTR(-ENOMEM); | |
2846 | ||
bc01befd PNA |
2847 | help = nfct_help(ct); |
2848 | if (!help) { | |
2849 | if (!cda[CTA_EXPECT_TIMEOUT]) { | |
2850 | err = -EINVAL; | |
1310b955 | 2851 | goto err_out; |
bc01befd PNA |
2852 | } |
2853 | exp->timeout.expires = | |
2854 | jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ; | |
601e68e1 | 2855 | |
bc01befd PNA |
2856 | exp->flags = NF_CT_EXPECT_USERSPACE; |
2857 | if (cda[CTA_EXPECT_FLAGS]) { | |
2858 | exp->flags |= | |
2859 | ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); | |
2860 | } | |
2861 | } else { | |
2862 | if (cda[CTA_EXPECT_FLAGS]) { | |
2863 | exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); | |
2864 | exp->flags &= ~NF_CT_EXPECT_USERSPACE; | |
2865 | } else | |
2866 | exp->flags = 0; | |
2867 | } | |
544d5c7d PNA |
2868 | if (cda[CTA_EXPECT_FN]) { |
2869 | const char *name = nla_data(cda[CTA_EXPECT_FN]); | |
2870 | struct nf_ct_helper_expectfn *expfn; | |
2871 | ||
2872 | expfn = nf_ct_helper_expectfn_find_by_name(name); | |
2873 | if (expfn == NULL) { | |
2874 | err = -EINVAL; | |
2875 | goto err_out; | |
2876 | } | |
2877 | exp->expectfn = expfn->expectfn; | |
2878 | } else | |
2879 | exp->expectfn = NULL; | |
601e68e1 | 2880 | |
b8c5e52c | 2881 | exp->class = class; |
c1d10adb | 2882 | exp->master = ct; |
660fdb2a | 2883 | exp->helper = helper; |
0ef71ee1 PNA |
2884 | exp->tuple = *tuple; |
2885 | exp->mask.src.u3 = mask->src.u3; | |
2886 | exp->mask.src.u.all = mask->src.u.all; | |
c1d10adb | 2887 | |
076a0ca0 PNA |
2888 | if (cda[CTA_EXPECT_NAT]) { |
2889 | err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT], | |
0ef71ee1 | 2890 | exp, nf_ct_l3num(ct)); |
076a0ca0 PNA |
2891 | if (err < 0) |
2892 | goto err_out; | |
2893 | } | |
0ef71ee1 | 2894 | return exp; |
076a0ca0 | 2895 | err_out: |
6823645d | 2896 | nf_ct_expect_put(exp); |
0ef71ee1 PNA |
2897 | return ERR_PTR(err); |
2898 | } | |
2899 | ||
2900 | static int | |
2901 | ctnetlink_create_expect(struct net *net, u16 zone, | |
2902 | const struct nlattr * const cda[], | |
2903 | u_int8_t u3, u32 portid, int report) | |
2904 | { | |
2905 | struct nf_conntrack_tuple tuple, mask, master_tuple; | |
2906 | struct nf_conntrack_tuple_hash *h = NULL; | |
2907 | struct nf_conntrack_helper *helper = NULL; | |
2908 | struct nf_conntrack_expect *exp; | |
2909 | struct nf_conn *ct; | |
2910 | int err; | |
2911 | ||
2912 | /* caller guarantees that those three CTA_EXPECT_* exist */ | |
2913 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
2914 | if (err < 0) | |
2915 | return err; | |
2916 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3); | |
2917 | if (err < 0) | |
2918 | return err; | |
2919 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3); | |
2920 | if (err < 0) | |
2921 | return err; | |
2922 | ||
2923 | /* Look for master conntrack of this expectation */ | |
2924 | h = nf_conntrack_find_get(net, zone, &master_tuple); | |
2925 | if (!h) | |
2926 | return -ENOENT; | |
2927 | ct = nf_ct_tuplehash_to_ctrack(h); | |
2928 | ||
2929 | if (cda[CTA_EXPECT_HELP_NAME]) { | |
2930 | const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
2931 | ||
2932 | helper = __nf_conntrack_helper_find(helpname, u3, | |
2933 | nf_ct_protonum(ct)); | |
2934 | if (helper == NULL) { | |
2935 | #ifdef CONFIG_MODULES | |
2936 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
2937 | err = -EOPNOTSUPP; | |
2938 | goto err_ct; | |
2939 | } | |
2940 | helper = __nf_conntrack_helper_find(helpname, u3, | |
2941 | nf_ct_protonum(ct)); | |
2942 | if (helper) { | |
2943 | err = -EAGAIN; | |
2944 | goto err_ct; | |
2945 | } | |
2946 | #endif | |
2947 | err = -EOPNOTSUPP; | |
2948 | goto err_ct; | |
2949 | } | |
2950 | } | |
2951 | ||
2952 | exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask); | |
2953 | if (IS_ERR(exp)) { | |
2954 | err = PTR_ERR(exp); | |
2955 | goto err_ct; | |
2956 | } | |
2957 | ||
2958 | err = nf_ct_expect_related_report(exp, portid, report); | |
2959 | if (err < 0) | |
2960 | goto err_exp; | |
2961 | ||
2962 | return 0; | |
2963 | err_exp: | |
2964 | nf_ct_expect_put(exp); | |
2965 | err_ct: | |
2966 | nf_ct_put(ct); | |
c1d10adb PNA |
2967 | return err; |
2968 | } | |
2969 | ||
2970 | static int | |
2971 | ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
2972 | const struct nlmsghdr *nlh, |
2973 | const struct nlattr * const cda[]) | |
c1d10adb | 2974 | { |
9592a5c0 | 2975 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
2976 | struct nf_conntrack_tuple tuple; |
2977 | struct nf_conntrack_expect *exp; | |
96bcf938 | 2978 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 2979 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
2980 | u16 zone; |
2981 | int err; | |
c1d10adb | 2982 | |
df6fb868 PM |
2983 | if (!cda[CTA_EXPECT_TUPLE] |
2984 | || !cda[CTA_EXPECT_MASK] | |
2985 | || !cda[CTA_EXPECT_MASTER]) | |
c1d10adb PNA |
2986 | return -EINVAL; |
2987 | ||
ef00f89f PM |
2988 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2989 | if (err < 0) | |
2990 | return err; | |
2991 | ||
c1d10adb PNA |
2992 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
2993 | if (err < 0) | |
2994 | return err; | |
2995 | ||
ca7433df | 2996 | spin_lock_bh(&nf_conntrack_expect_lock); |
ef00f89f | 2997 | exp = __nf_ct_expect_find(net, zone, &tuple); |
c1d10adb PNA |
2998 | |
2999 | if (!exp) { | |
ca7433df | 3000 | spin_unlock_bh(&nf_conntrack_expect_lock); |
c1d10adb | 3001 | err = -ENOENT; |
19abb7b0 | 3002 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
ef00f89f | 3003 | err = ctnetlink_create_expect(net, zone, cda, |
19abb7b0 | 3004 | u3, |
15e47304 | 3005 | NETLINK_CB(skb).portid, |
19abb7b0 PNA |
3006 | nlmsg_report(nlh)); |
3007 | } | |
c1d10adb PNA |
3008 | return err; |
3009 | } | |
3010 | ||
3011 | err = -EEXIST; | |
3012 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) | |
3013 | err = ctnetlink_change_expect(exp, cda); | |
ca7433df | 3014 | spin_unlock_bh(&nf_conntrack_expect_lock); |
c1d10adb | 3015 | |
c1d10adb PNA |
3016 | return err; |
3017 | } | |
3018 | ||
392025f8 | 3019 | static int |
15e47304 | 3020 | ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu, |
392025f8 PNA |
3021 | const struct ip_conntrack_stat *st) |
3022 | { | |
3023 | struct nlmsghdr *nlh; | |
3024 | struct nfgenmsg *nfmsg; | |
15e47304 | 3025 | unsigned int flags = portid ? NLM_F_MULTI : 0, event; |
392025f8 PNA |
3026 | |
3027 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU); | |
15e47304 | 3028 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
392025f8 PNA |
3029 | if (nlh == NULL) |
3030 | goto nlmsg_failure; | |
3031 | ||
3032 | nfmsg = nlmsg_data(nlh); | |
3033 | nfmsg->nfgen_family = AF_UNSPEC; | |
3034 | nfmsg->version = NFNETLINK_V0; | |
3035 | nfmsg->res_id = htons(cpu); | |
3036 | ||
3037 | if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) || | |
3038 | nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) || | |
3039 | nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete))) | |
3040 | goto nla_put_failure; | |
3041 | ||
3042 | nlmsg_end(skb, nlh); | |
3043 | return skb->len; | |
3044 | ||
3045 | nla_put_failure: | |
3046 | nlmsg_failure: | |
3047 | nlmsg_cancel(skb, nlh); | |
3048 | return -1; | |
3049 | } | |
3050 | ||
3051 | static int | |
3052 | ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
3053 | { | |
3054 | int cpu; | |
3055 | struct net *net = sock_net(skb->sk); | |
3056 | ||
3057 | if (cb->args[0] == nr_cpu_ids) | |
3058 | return 0; | |
3059 | ||
3060 | for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) { | |
3061 | const struct ip_conntrack_stat *st; | |
3062 | ||
3063 | if (!cpu_possible(cpu)) | |
3064 | continue; | |
3065 | ||
3066 | st = per_cpu_ptr(net->ct.stat, cpu); | |
15e47304 | 3067 | if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid, |
392025f8 PNA |
3068 | cb->nlh->nlmsg_seq, |
3069 | cpu, st) < 0) | |
3070 | break; | |
3071 | } | |
3072 | cb->args[0] = cpu; | |
3073 | ||
3074 | return skb->len; | |
3075 | } | |
3076 | ||
3077 | static int | |
3078 | ctnetlink_stat_exp_cpu(struct sock *ctnl, struct sk_buff *skb, | |
3079 | const struct nlmsghdr *nlh, | |
3080 | const struct nlattr * const cda[]) | |
3081 | { | |
3082 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
3083 | struct netlink_dump_control c = { | |
3084 | .dump = ctnetlink_exp_stat_cpu_dump, | |
3085 | }; | |
3086 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
3087 | } | |
3088 | ||
3089 | return 0; | |
3090 | } | |
3091 | ||
c1d10adb | 3092 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
e34d5c1a PNA |
3093 | static struct nf_ct_event_notifier ctnl_notifier = { |
3094 | .fcn = ctnetlink_conntrack_event, | |
c1d10adb PNA |
3095 | }; |
3096 | ||
e34d5c1a PNA |
3097 | static struct nf_exp_event_notifier ctnl_notifier_exp = { |
3098 | .fcn = ctnetlink_expect_event, | |
c1d10adb PNA |
3099 | }; |
3100 | #endif | |
3101 | ||
7c8d4cb4 | 3102 | static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { |
c1d10adb | 3103 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, |
f73e924c PM |
3104 | .attr_count = CTA_MAX, |
3105 | .policy = ct_nla_policy }, | |
c1d10adb | 3106 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
3107 | .attr_count = CTA_MAX, |
3108 | .policy = ct_nla_policy }, | |
c1d10adb | 3109 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, |
f73e924c PM |
3110 | .attr_count = CTA_MAX, |
3111 | .policy = ct_nla_policy }, | |
c1d10adb | 3112 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
3113 | .attr_count = CTA_MAX, |
3114 | .policy = ct_nla_policy }, | |
392025f8 PNA |
3115 | [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu }, |
3116 | [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct }, | |
d871befe PNA |
3117 | [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying }, |
3118 | [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed }, | |
c1d10adb PNA |
3119 | }; |
3120 | ||
7c8d4cb4 | 3121 | static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { |
c1d10adb | 3122 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
f73e924c PM |
3123 | .attr_count = CTA_EXPECT_MAX, |
3124 | .policy = exp_nla_policy }, | |
c1d10adb | 3125 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, |
f73e924c PM |
3126 | .attr_count = CTA_EXPECT_MAX, |
3127 | .policy = exp_nla_policy }, | |
c1d10adb | 3128 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, |
f73e924c PM |
3129 | .attr_count = CTA_EXPECT_MAX, |
3130 | .policy = exp_nla_policy }, | |
392025f8 | 3131 | [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu }, |
c1d10adb PNA |
3132 | }; |
3133 | ||
7c8d4cb4 | 3134 | static const struct nfnetlink_subsystem ctnl_subsys = { |
c1d10adb PNA |
3135 | .name = "conntrack", |
3136 | .subsys_id = NFNL_SUBSYS_CTNETLINK, | |
3137 | .cb_count = IPCTNL_MSG_MAX, | |
3138 | .cb = ctnl_cb, | |
3139 | }; | |
3140 | ||
7c8d4cb4 | 3141 | static const struct nfnetlink_subsystem ctnl_exp_subsys = { |
c1d10adb PNA |
3142 | .name = "conntrack_expect", |
3143 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, | |
3144 | .cb_count = IPCTNL_MSG_EXP_MAX, | |
3145 | .cb = ctnl_exp_cb, | |
3146 | }; | |
3147 | ||
d2483dde | 3148 | MODULE_ALIAS("ip_conntrack_netlink"); |
c1d10adb | 3149 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); |
34f9a2e4 | 3150 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); |
c1d10adb | 3151 | |
70e9942f PNA |
3152 | static int __net_init ctnetlink_net_init(struct net *net) |
3153 | { | |
3154 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
3155 | int ret; | |
3156 | ||
3157 | ret = nf_conntrack_register_notifier(net, &ctnl_notifier); | |
3158 | if (ret < 0) { | |
3159 | pr_err("ctnetlink_init: cannot register notifier.\n"); | |
3160 | goto err_out; | |
3161 | } | |
3162 | ||
3163 | ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp); | |
3164 | if (ret < 0) { | |
3165 | pr_err("ctnetlink_init: cannot expect register notifier.\n"); | |
3166 | goto err_unreg_notifier; | |
3167 | } | |
3168 | #endif | |
3169 | return 0; | |
3170 | ||
3171 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
3172 | err_unreg_notifier: | |
3173 | nf_conntrack_unregister_notifier(net, &ctnl_notifier); | |
3174 | err_out: | |
3175 | return ret; | |
3176 | #endif | |
3177 | } | |
3178 | ||
3179 | static void ctnetlink_net_exit(struct net *net) | |
3180 | { | |
3181 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
3182 | nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp); | |
3183 | nf_conntrack_unregister_notifier(net, &ctnl_notifier); | |
3184 | #endif | |
3185 | } | |
3186 | ||
3187 | static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list) | |
3188 | { | |
3189 | struct net *net; | |
3190 | ||
3191 | list_for_each_entry(net, net_exit_list, exit_list) | |
3192 | ctnetlink_net_exit(net); | |
3193 | } | |
3194 | ||
3195 | static struct pernet_operations ctnetlink_net_ops = { | |
3196 | .init = ctnetlink_net_init, | |
3197 | .exit_batch = ctnetlink_net_exit_batch, | |
3198 | }; | |
3199 | ||
c1d10adb PNA |
3200 | static int __init ctnetlink_init(void) |
3201 | { | |
3202 | int ret; | |
3203 | ||
654d0fbd | 3204 | pr_info("ctnetlink v%s: registering with nfnetlink.\n", version); |
c1d10adb PNA |
3205 | ret = nfnetlink_subsys_register(&ctnl_subsys); |
3206 | if (ret < 0) { | |
654d0fbd | 3207 | pr_err("ctnetlink_init: cannot register with nfnetlink.\n"); |
c1d10adb PNA |
3208 | goto err_out; |
3209 | } | |
3210 | ||
3211 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); | |
3212 | if (ret < 0) { | |
654d0fbd | 3213 | pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n"); |
c1d10adb PNA |
3214 | goto err_unreg_subsys; |
3215 | } | |
3216 | ||
ef6acf68 JL |
3217 | ret = register_pernet_subsys(&ctnetlink_net_ops); |
3218 | if (ret < 0) { | |
70e9942f | 3219 | pr_err("ctnetlink_init: cannot register pernet operations\n"); |
c1d10adb PNA |
3220 | goto err_unreg_exp_subsys; |
3221 | } | |
7c622345 | 3222 | #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT |
9cb01766 PNA |
3223 | /* setup interaction between nf_queue and nf_conntrack_netlink. */ |
3224 | RCU_INIT_POINTER(nfq_ct_hook, &ctnetlink_nfqueue_hook); | |
3225 | #endif | |
c1d10adb PNA |
3226 | return 0; |
3227 | ||
c1d10adb PNA |
3228 | err_unreg_exp_subsys: |
3229 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
c1d10adb PNA |
3230 | err_unreg_subsys: |
3231 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
3232 | err_out: | |
3233 | return ret; | |
3234 | } | |
3235 | ||
3236 | static void __exit ctnetlink_exit(void) | |
3237 | { | |
654d0fbd | 3238 | pr_info("ctnetlink: unregistering from nfnetlink.\n"); |
c1d10adb | 3239 | |
70e9942f | 3240 | unregister_pernet_subsys(&ctnetlink_net_ops); |
c1d10adb PNA |
3241 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); |
3242 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
7c622345 | 3243 | #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT |
9cb01766 PNA |
3244 | RCU_INIT_POINTER(nfq_ct_hook, NULL); |
3245 | #endif | |
c1d10adb PNA |
3246 | } |
3247 | ||
3248 | module_init(ctnetlink_init); | |
3249 | module_exit(ctnetlink_exit); |