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