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