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