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