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