netfilter: nf_conntrack: properly account terminating packets
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
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
1eedf699
EL
475 if (ctnetlink_dump_id(skb, ct) < 0)
476 goto nla_put_failure;
477
7b621c1e
PNA
478 if (events & IPCT_DESTROY) {
479 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
480 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
df6fb868 481 goto nla_put_failure;
7b621c1e
PNA
482 } else {
483 if (ctnetlink_dump_status(skb, ct) < 0)
df6fb868 484 goto nla_put_failure;
7b621c1e
PNA
485
486 if (ctnetlink_dump_timeout(skb, ct) < 0)
df6fb868 487 goto nla_put_failure;
7b621c1e
PNA
488
489 if (events & IPCT_PROTOINFO
490 && ctnetlink_dump_protoinfo(skb, ct) < 0)
df6fb868 491 goto nla_put_failure;
7b621c1e
PNA
492
493 if ((events & IPCT_HELPER || nfct_help(ct))
494 && ctnetlink_dump_helpinfo(skb, ct) < 0)
df6fb868 495 goto nla_put_failure;
7b621c1e 496
37fccd85
PNA
497#ifdef CONFIG_NF_CONNTRACK_SECMARK
498 if ((events & IPCT_SECMARK || ct->secmark)
499 && ctnetlink_dump_secmark(skb, ct) < 0)
500 goto nla_put_failure;
501#endif
7b621c1e
PNA
502
503 if (events & IPCT_COUNTER_FILLING &&
504 (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
505 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0))
df6fb868 506 goto nla_put_failure;
13eae15a 507
0f417ce9
PNA
508 if (events & IPCT_RELATED &&
509 ctnetlink_dump_master(skb, ct) < 0)
510 goto nla_put_failure;
511
13eae15a
PNA
512 if (events & IPCT_NATSEQADJ &&
513 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
514 goto nla_put_failure;
7b621c1e 515 }
b9a37e0c 516
a83099a6
EL
517#ifdef CONFIG_NF_CONNTRACK_MARK
518 if ((events & IPCT_MARK || ct->mark)
519 && ctnetlink_dump_mark(skb, ct) < 0)
520 goto nla_put_failure;
521#endif
522
c1d10adb
PNA
523 nlh->nlmsg_len = skb->tail - b;
524 nfnetlink_send(skb, 0, group, 0);
525 return NOTIFY_DONE;
526
527nlmsg_failure:
df6fb868 528nla_put_failure:
c1d10adb
PNA
529 kfree_skb(skb);
530 return NOTIFY_DONE;
531}
532#endif /* CONFIG_NF_CONNTRACK_EVENTS */
533
534static int ctnetlink_done(struct netlink_callback *cb)
535{
89f2e218
PM
536 if (cb->args[1])
537 nf_ct_put((struct nf_conn *)cb->args[1]);
c1d10adb
PNA
538 return 0;
539}
540
541static int
542ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
543{
89f2e218 544 struct nf_conn *ct, *last;
c1d10adb 545 struct nf_conntrack_tuple_hash *h;
f205c5e0 546 struct hlist_node *n;
87711cb8
PNA
547 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
548 u_int8_t l3proto = nfmsg->nfgen_family;
c1d10adb 549
76507f69 550 rcu_read_lock();
d205dc40 551 last = (struct nf_conn *)cb->args[1];
89f2e218
PM
552 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
553restart:
76507f69
PM
554 hlist_for_each_entry_rcu(h, n, &nf_conntrack_hash[cb->args[0]],
555 hnode) {
5b1158e9 556 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
c1d10adb
PNA
557 continue;
558 ct = nf_ct_tuplehash_to_ctrack(h);
87711cb8
PNA
559 /* Dump entries of a given L3 protocol number.
560 * If it is not specified, ie. l3proto == 0,
561 * then dump everything. */
5e8fbe2a 562 if (l3proto && nf_ct_l3num(ct) != l3proto)
87711cb8 563 continue;
d205dc40
PM
564 if (cb->args[1]) {
565 if (ct != last)
89f2e218 566 continue;
d205dc40 567 cb->args[1] = 0;
89f2e218 568 }
c1d10adb 569 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
601e68e1 570 cb->nlh->nlmsg_seq,
c1d10adb 571 IPCTNL_MSG_CT_NEW,
89f2e218 572 1, ct) < 0) {
76507f69
PM
573 if (!atomic_inc_not_zero(&ct->ct_general.use))
574 continue;
89f2e218 575 cb->args[1] = (unsigned long)ct;
c1d10adb 576 goto out;
89f2e218 577 }
01f34848
PNA
578#ifdef CONFIG_NF_CT_ACCT
579 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
580 IPCTNL_MSG_CT_GET_CTRZERO)
581 memset(&ct->counters, 0, sizeof(ct->counters));
582#endif
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
PNA
605
606 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
607
f73e924c
PM
608 if (likely(l3proto->nlattr_to_tuple)) {
609 ret = nla_validate_nested(attr, CTA_IP_MAX,
610 l3proto->nla_policy);
611 if (ret == 0)
612 ret = l3proto->nlattr_to_tuple(tb, tuple);
613 }
c1d10adb
PNA
614
615 nf_ct_l3proto_put(l3proto);
616
c1d10adb
PNA
617 return ret;
618}
619
f73e924c
PM
620static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
621 [CTA_PROTO_NUM] = { .type = NLA_U8 },
c1d10adb
PNA
622};
623
624static inline int
df6fb868 625ctnetlink_parse_tuple_proto(struct nlattr *attr,
c1d10adb
PNA
626 struct nf_conntrack_tuple *tuple)
627{
df6fb868 628 struct nlattr *tb[CTA_PROTO_MAX+1];
605dcad6 629 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
630 int ret = 0;
631
f73e924c
PM
632 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
633 if (ret < 0)
634 return ret;
c1d10adb 635
df6fb868 636 if (!tb[CTA_PROTO_NUM])
c1d10adb 637 return -EINVAL;
77236b6e 638 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
c1d10adb 639
605dcad6 640 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
c1d10adb 641
f73e924c
PM
642 if (likely(l4proto->nlattr_to_tuple)) {
643 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
644 l4proto->nla_policy);
645 if (ret == 0)
646 ret = l4proto->nlattr_to_tuple(tb, tuple);
647 }
c1d10adb 648
605dcad6 649 nf_ct_l4proto_put(l4proto);
601e68e1 650
c1d10adb
PNA
651 return ret;
652}
653
bb5cf80e 654static int
df6fb868 655ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
c1d10adb
PNA
656 enum ctattr_tuple type, u_int8_t l3num)
657{
df6fb868 658 struct nlattr *tb[CTA_TUPLE_MAX+1];
c1d10adb
PNA
659 int err;
660
c1d10adb
PNA
661 memset(tuple, 0, sizeof(*tuple));
662
df6fb868 663 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
c1d10adb 664
df6fb868 665 if (!tb[CTA_TUPLE_IP])
c1d10adb
PNA
666 return -EINVAL;
667
668 tuple->src.l3num = l3num;
669
df6fb868 670 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
c1d10adb
PNA
671 if (err < 0)
672 return err;
673
df6fb868 674 if (!tb[CTA_TUPLE_PROTO])
c1d10adb
PNA
675 return -EINVAL;
676
df6fb868 677 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
c1d10adb
PNA
678 if (err < 0)
679 return err;
680
681 /* orig and expect tuples get DIR_ORIGINAL */
682 if (type == CTA_TUPLE_REPLY)
683 tuple->dst.dir = IP_CT_DIR_REPLY;
684 else
685 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
686
c1d10adb
PNA
687 return 0;
688}
689
5b1158e9 690#ifdef CONFIG_NF_NAT_NEEDED
f73e924c
PM
691static const struct nla_policy protonat_nla_policy[CTA_PROTONAT_MAX+1] = {
692 [CTA_PROTONAT_PORT_MIN] = { .type = NLA_U16 },
693 [CTA_PROTONAT_PORT_MAX] = { .type = NLA_U16 },
c1d10adb
PNA
694};
695
df6fb868 696static int nfnetlink_parse_nat_proto(struct nlattr *attr,
c1d10adb 697 const struct nf_conn *ct,
5b1158e9 698 struct nf_nat_range *range)
c1d10adb 699{
df6fb868 700 struct nlattr *tb[CTA_PROTONAT_MAX+1];
2b628a08 701 const struct nf_nat_protocol *npt;
f73e924c 702 int err;
c1d10adb 703
f73e924c
PM
704 err = nla_parse_nested(tb, CTA_PROTONAT_MAX, attr, protonat_nla_policy);
705 if (err < 0)
706 return err;
c1d10adb 707
5e8fbe2a 708 npt = nf_nat_proto_find_get(nf_ct_protonum(ct));
ca6a5074
PM
709 if (npt->nlattr_to_range)
710 err = npt->nlattr_to_range(tb, range);
5b1158e9 711 nf_nat_proto_put(npt);
ca6a5074 712 return err;
c1d10adb
PNA
713}
714
f73e924c
PM
715static const struct nla_policy nat_nla_policy[CTA_NAT_MAX+1] = {
716 [CTA_NAT_MINIP] = { .type = NLA_U32 },
717 [CTA_NAT_MAXIP] = { .type = NLA_U32 },
c1d10adb
PNA
718};
719
720static inline int
df6fb868 721nfnetlink_parse_nat(struct nlattr *nat,
5b1158e9 722 const struct nf_conn *ct, struct nf_nat_range *range)
c1d10adb 723{
df6fb868 724 struct nlattr *tb[CTA_NAT_MAX+1];
c1d10adb
PNA
725 int err;
726
c1d10adb 727 memset(range, 0, sizeof(*range));
601e68e1 728
f73e924c
PM
729 err = nla_parse_nested(tb, CTA_NAT_MAX, nat, nat_nla_policy);
730 if (err < 0)
731 return err;
c1d10adb 732
df6fb868 733 if (tb[CTA_NAT_MINIP])
77236b6e 734 range->min_ip = nla_get_be32(tb[CTA_NAT_MINIP]);
c1d10adb 735
df6fb868 736 if (!tb[CTA_NAT_MAXIP])
c1d10adb
PNA
737 range->max_ip = range->min_ip;
738 else
77236b6e 739 range->max_ip = nla_get_be32(tb[CTA_NAT_MAXIP]);
c1d10adb
PNA
740
741 if (range->min_ip)
742 range->flags |= IP_NAT_RANGE_MAP_IPS;
743
df6fb868 744 if (!tb[CTA_NAT_PROTO])
c1d10adb
PNA
745 return 0;
746
df6fb868 747 err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO], ct, range);
c1d10adb
PNA
748 if (err < 0)
749 return err;
750
c1d10adb
PNA
751 return 0;
752}
753#endif
754
755static inline int
df6fb868 756ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
c1d10adb 757{
df6fb868 758 struct nlattr *tb[CTA_HELP_MAX+1];
c1d10adb 759
df6fb868 760 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
c1d10adb 761
df6fb868 762 if (!tb[CTA_HELP_NAME])
c1d10adb
PNA
763 return -EINVAL;
764
df6fb868 765 *helper_name = nla_data(tb[CTA_HELP_NAME]);
c1d10adb
PNA
766
767 return 0;
768}
769
f73e924c
PM
770static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
771 [CTA_STATUS] = { .type = NLA_U32 },
772 [CTA_TIMEOUT] = { .type = NLA_U32 },
773 [CTA_MARK] = { .type = NLA_U32 },
774 [CTA_USE] = { .type = NLA_U32 },
775 [CTA_ID] = { .type = NLA_U32 },
c1d10adb
PNA
776};
777
778static int
601e68e1 779ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
df6fb868 780 struct nlmsghdr *nlh, struct nlattr *cda[])
c1d10adb
PNA
781{
782 struct nf_conntrack_tuple_hash *h;
783 struct nf_conntrack_tuple tuple;
784 struct nf_conn *ct;
785 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
786 u_int8_t u3 = nfmsg->nfgen_family;
787 int err = 0;
788
df6fb868 789 if (cda[CTA_TUPLE_ORIG])
c1d10adb 790 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
df6fb868 791 else if (cda[CTA_TUPLE_REPLY])
c1d10adb
PNA
792 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
793 else {
794 /* Flush the whole table */
795 nf_conntrack_flush();
796 return 0;
797 }
798
799 if (err < 0)
800 return err;
801
330f7db5 802 h = nf_conntrack_find_get(&tuple);
9ea8cfd6 803 if (!h)
c1d10adb 804 return -ENOENT;
c1d10adb
PNA
805
806 ct = nf_ct_tuplehash_to_ctrack(h);
601e68e1 807
df6fb868 808 if (cda[CTA_ID]) {
77236b6e 809 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
7f85f914 810 if (id != (u32)(unsigned long)ct) {
c1d10adb
PNA
811 nf_ct_put(ct);
812 return -ENOENT;
813 }
601e68e1 814 }
c1d10adb 815
51091764 816 nf_ct_kill(ct);
c1d10adb 817 nf_ct_put(ct);
c1d10adb
PNA
818
819 return 0;
820}
821
822static int
601e68e1 823ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
df6fb868 824 struct nlmsghdr *nlh, struct nlattr *cda[])
c1d10adb
PNA
825{
826 struct nf_conntrack_tuple_hash *h;
827 struct nf_conntrack_tuple tuple;
828 struct nf_conn *ct;
829 struct sk_buff *skb2 = NULL;
830 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
831 u_int8_t u3 = nfmsg->nfgen_family;
832 int err = 0;
833
c1d10adb 834 if (nlh->nlmsg_flags & NLM_F_DUMP) {
01f34848
PNA
835#ifndef CONFIG_NF_CT_ACCT
836 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
c1d10adb
PNA
837 return -ENOTSUPP;
838#endif
c702e804
TG
839 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
840 ctnetlink_done);
c1d10adb
PNA
841 }
842
df6fb868 843 if (cda[CTA_TUPLE_ORIG])
c1d10adb 844 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
df6fb868 845 else if (cda[CTA_TUPLE_REPLY])
c1d10adb
PNA
846 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
847 else
848 return -EINVAL;
849
850 if (err < 0)
851 return err;
852
330f7db5 853 h = nf_conntrack_find_get(&tuple);
9ea8cfd6 854 if (!h)
c1d10adb 855 return -ENOENT;
9ea8cfd6 856
c1d10adb
PNA
857 ct = nf_ct_tuplehash_to_ctrack(h);
858
859 err = -ENOMEM;
860 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
861 if (!skb2) {
862 nf_ct_put(ct);
863 return -ENOMEM;
864 }
c1d10adb 865
601e68e1 866 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
c1d10adb
PNA
867 IPCTNL_MSG_CT_NEW, 1, ct);
868 nf_ct_put(ct);
869 if (err <= 0)
870 goto free;
871
872 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
873 if (err < 0)
874 goto out;
875
c1d10adb
PNA
876 return 0;
877
878free:
879 kfree_skb(skb2);
880out:
881 return err;
882}
883
bb5cf80e 884static int
df6fb868 885ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
c1d10adb
PNA
886{
887 unsigned long d;
77236b6e 888 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
c1d10adb
PNA
889 d = ct->status ^ status;
890
891 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
892 /* unchangeable */
0adf9d67 893 return -EBUSY;
601e68e1 894
c1d10adb
PNA
895 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
896 /* SEEN_REPLY bit can only be set */
0adf9d67 897 return -EBUSY;
601e68e1 898
c1d10adb
PNA
899 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
900 /* ASSURED bit can only be set */
0adf9d67 901 return -EBUSY;
c1d10adb 902
df6fb868 903 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
5b1158e9 904#ifndef CONFIG_NF_NAT_NEEDED
0adf9d67 905 return -EOPNOTSUPP;
c1d10adb 906#else
5b1158e9 907 struct nf_nat_range range;
c1d10adb 908
df6fb868
PM
909 if (cda[CTA_NAT_DST]) {
910 if (nfnetlink_parse_nat(cda[CTA_NAT_DST], ct,
3726add7
PM
911 &range) < 0)
912 return -EINVAL;
cc01dcbd 913 if (nf_nat_initialized(ct, IP_NAT_MANIP_DST))
3726add7 914 return -EEXIST;
cc01dcbd 915 nf_nat_setup_info(ct, &range, IP_NAT_MANIP_DST);
3726add7 916 }
df6fb868
PM
917 if (cda[CTA_NAT_SRC]) {
918 if (nfnetlink_parse_nat(cda[CTA_NAT_SRC], ct,
3726add7
PM
919 &range) < 0)
920 return -EINVAL;
cc01dcbd 921 if (nf_nat_initialized(ct, IP_NAT_MANIP_SRC))
3726add7 922 return -EEXIST;
cc01dcbd 923 nf_nat_setup_info(ct, &range, IP_NAT_MANIP_SRC);
3726add7 924 }
c1d10adb
PNA
925#endif
926 }
927
928 /* Be careful here, modifying NAT bits can screw up things,
929 * so don't let users modify them directly if they don't pass
5b1158e9 930 * nf_nat_range. */
c1d10adb
PNA
931 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
932 return 0;
933}
934
935
936static inline int
df6fb868 937ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
c1d10adb
PNA
938{
939 struct nf_conntrack_helper *helper;
dc808fe2 940 struct nf_conn_help *help = nfct_help(ct);
c1d10adb
PNA
941 char *helpname;
942 int err;
943
c1d10adb
PNA
944 /* don't change helper of sibling connections */
945 if (ct->master)
0adf9d67 946 return -EBUSY;
c1d10adb 947
df6fb868 948 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
c1d10adb
PNA
949 if (err < 0)
950 return err;
951
df293bbb
YK
952 if (!strcmp(helpname, "")) {
953 if (help && help->helper) {
c1d10adb
PNA
954 /* we had a helper before ... */
955 nf_ct_remove_expectations(ct);
3c158f7f 956 rcu_assign_pointer(help->helper, NULL);
c1d10adb 957 }
df293bbb
YK
958
959 return 0;
c1d10adb 960 }
601e68e1 961
df293bbb
YK
962 helper = __nf_conntrack_helper_find_byname(helpname);
963 if (helper == NULL)
0adf9d67 964 return -EOPNOTSUPP;
df293bbb 965
ceceae1b
YK
966 if (help) {
967 if (help->helper == helper)
968 return 0;
969 if (help->helper)
970 return -EBUSY;
971 /* need to zero data of old helper */
972 memset(&help->help, 0, sizeof(help->help));
973 } else {
b560580a 974 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
ceceae1b
YK
975 if (help == NULL)
976 return -ENOMEM;
977 }
df293bbb 978
3c158f7f 979 rcu_assign_pointer(help->helper, helper);
c1d10adb
PNA
980
981 return 0;
982}
983
984static inline int
df6fb868 985ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
c1d10adb 986{
77236b6e 987 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
601e68e1 988
c1d10adb
PNA
989 if (!del_timer(&ct->timeout))
990 return -ETIME;
991
992 ct->timeout.expires = jiffies + timeout * HZ;
993 add_timer(&ct->timeout);
994
995 return 0;
996}
997
998static inline int
df6fb868 999ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
c1d10adb 1000{
df6fb868 1001 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
605dcad6 1002 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
1003 int err = 0;
1004
df6fb868 1005 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
c1d10adb 1006
5e8fbe2a 1007 l4proto = nf_ct_l4proto_find_get(nf_ct_l3num(ct), nf_ct_protonum(ct));
fdf70832
PM
1008 if (l4proto->from_nlattr)
1009 err = l4proto->from_nlattr(tb, ct);
605dcad6 1010 nf_ct_l4proto_put(l4proto);
c1d10adb
PNA
1011
1012 return err;
1013}
1014
13eae15a
PNA
1015#ifdef CONFIG_NF_NAT_NEEDED
1016static inline int
1017change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1018{
1019 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1020
1021 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1022
1023 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1024 return -EINVAL;
1025
1026 natseq->correction_pos =
77236b6e 1027 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
13eae15a
PNA
1028
1029 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1030 return -EINVAL;
1031
1032 natseq->offset_before =
77236b6e 1033 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
13eae15a
PNA
1034
1035 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1036 return -EINVAL;
1037
1038 natseq->offset_after =
77236b6e 1039 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
13eae15a
PNA
1040
1041 return 0;
1042}
1043
1044static int
1045ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1046{
1047 int ret = 0;
1048 struct nf_conn_nat *nat = nfct_nat(ct);
1049
1050 if (!nat)
1051 return 0;
1052
1053 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1054 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1055 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1056 if (ret < 0)
1057 return ret;
1058
1059 ct->status |= IPS_SEQ_ADJUST;
1060 }
1061
1062 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1063 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1064 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1065 if (ret < 0)
1066 return ret;
1067
1068 ct->status |= IPS_SEQ_ADJUST;
1069 }
1070
1071 return 0;
1072}
1073#endif
1074
c1d10adb 1075static int
df6fb868 1076ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
c1d10adb
PNA
1077{
1078 int err;
1079
df6fb868 1080 if (cda[CTA_HELP]) {
c1d10adb
PNA
1081 err = ctnetlink_change_helper(ct, cda);
1082 if (err < 0)
1083 return err;
1084 }
1085
df6fb868 1086 if (cda[CTA_TIMEOUT]) {
c1d10adb
PNA
1087 err = ctnetlink_change_timeout(ct, cda);
1088 if (err < 0)
1089 return err;
1090 }
1091
df6fb868 1092 if (cda[CTA_STATUS]) {
c1d10adb
PNA
1093 err = ctnetlink_change_status(ct, cda);
1094 if (err < 0)
1095 return err;
1096 }
1097
df6fb868 1098 if (cda[CTA_PROTOINFO]) {
c1d10adb
PNA
1099 err = ctnetlink_change_protoinfo(ct, cda);
1100 if (err < 0)
1101 return err;
1102 }
1103
bcd1e830 1104#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1105 if (cda[CTA_MARK])
77236b6e 1106 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1107#endif
1108
13eae15a
PNA
1109#ifdef CONFIG_NF_NAT_NEEDED
1110 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1111 err = ctnetlink_change_nat_seq_adj(ct, cda);
1112 if (err < 0)
1113 return err;
1114 }
1115#endif
1116
c1d10adb
PNA
1117 return 0;
1118}
1119
1120static int
df6fb868 1121ctnetlink_create_conntrack(struct nlattr *cda[],
c1d10adb 1122 struct nf_conntrack_tuple *otuple,
5faa1f4c
PNA
1123 struct nf_conntrack_tuple *rtuple,
1124 struct nf_conn *master_ct)
c1d10adb
PNA
1125{
1126 struct nf_conn *ct;
1127 int err = -EINVAL;
dafc741c 1128 struct nf_conn_help *help;
ceceae1b 1129 struct nf_conntrack_helper *helper;
c1d10adb 1130
c1d10adb
PNA
1131 ct = nf_conntrack_alloc(otuple, rtuple);
1132 if (ct == NULL || IS_ERR(ct))
601e68e1 1133 return -ENOMEM;
c1d10adb 1134
df6fb868 1135 if (!cda[CTA_TIMEOUT])
c1d10adb 1136 goto err;
77236b6e 1137 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
c1d10adb
PNA
1138
1139 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1140 ct->status |= IPS_CONFIRMED;
1141
df6fb868 1142 if (cda[CTA_STATUS]) {
bbb3357d
PNA
1143 err = ctnetlink_change_status(ct, cda);
1144 if (err < 0)
1145 goto err;
1146 }
c1d10adb 1147
df6fb868 1148 if (cda[CTA_PROTOINFO]) {
c1d10adb
PNA
1149 err = ctnetlink_change_protoinfo(ct, cda);
1150 if (err < 0)
c54ea3b9 1151 goto err;
c1d10adb
PNA
1152 }
1153
bcd1e830 1154#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1155 if (cda[CTA_MARK])
77236b6e 1156 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1157#endif
1158
58a3c9bb
PM
1159 rcu_read_lock();
1160 helper = __nf_ct_helper_find(rtuple);
ceceae1b 1161 if (helper) {
b560580a 1162 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
ceceae1b 1163 if (help == NULL) {
58a3c9bb 1164 rcu_read_unlock();
ceceae1b
YK
1165 err = -ENOMEM;
1166 goto err;
1167 }
3c158f7f
PM
1168 /* not in hash table yet so not strictly necessary */
1169 rcu_assign_pointer(help->helper, helper);
1170 }
dafc741c 1171
5faa1f4c 1172 /* setup master conntrack: this is a confirmed expectation */
f2a89004
PNA
1173 if (master_ct) {
1174 __set_bit(IPS_EXPECTED_BIT, &ct->status);
5faa1f4c 1175 ct->master = master_ct;
f2a89004 1176 }
5faa1f4c 1177
c1d10adb
PNA
1178 add_timer(&ct->timeout);
1179 nf_conntrack_hash_insert(ct);
58a3c9bb 1180 rcu_read_unlock();
dafc741c 1181
c1d10adb
PNA
1182 return 0;
1183
601e68e1 1184err:
c1d10adb
PNA
1185 nf_conntrack_free(ct);
1186 return err;
1187}
1188
601e68e1
YH
1189static int
1190ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
df6fb868 1191 struct nlmsghdr *nlh, struct nlattr *cda[])
c1d10adb
PNA
1192{
1193 struct nf_conntrack_tuple otuple, rtuple;
1194 struct nf_conntrack_tuple_hash *h = NULL;
1195 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1196 u_int8_t u3 = nfmsg->nfgen_family;
1197 int err = 0;
1198
df6fb868 1199 if (cda[CTA_TUPLE_ORIG]) {
c1d10adb
PNA
1200 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1201 if (err < 0)
1202 return err;
1203 }
1204
df6fb868 1205 if (cda[CTA_TUPLE_REPLY]) {
c1d10adb
PNA
1206 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1207 if (err < 0)
1208 return err;
1209 }
1210
f8ba1aff 1211 spin_lock_bh(&nf_conntrack_lock);
df6fb868 1212 if (cda[CTA_TUPLE_ORIG])
ba419aff 1213 h = __nf_conntrack_find(&otuple);
df6fb868 1214 else if (cda[CTA_TUPLE_REPLY])
ba419aff 1215 h = __nf_conntrack_find(&rtuple);
c1d10adb
PNA
1216
1217 if (h == NULL) {
5faa1f4c
PNA
1218 struct nf_conntrack_tuple master;
1219 struct nf_conntrack_tuple_hash *master_h = NULL;
1220 struct nf_conn *master_ct = NULL;
1221
1222 if (cda[CTA_TUPLE_MASTER]) {
1223 err = ctnetlink_parse_tuple(cda,
1224 &master,
1225 CTA_TUPLE_MASTER,
1226 u3);
1227 if (err < 0)
1d670fdc 1228 goto out_unlock;
5faa1f4c 1229
ba419aff 1230 master_h = __nf_conntrack_find(&master);
5faa1f4c
PNA
1231 if (master_h == NULL) {
1232 err = -ENOENT;
1233 goto out_unlock;
1234 }
1235 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1236 atomic_inc(&master_ct->ct_general.use);
1237 }
1238
f8ba1aff 1239 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1240 err = -ENOENT;
1241 if (nlh->nlmsg_flags & NLM_F_CREATE)
5faa1f4c
PNA
1242 err = ctnetlink_create_conntrack(cda,
1243 &otuple,
1244 &rtuple,
1245 master_ct);
1246 if (err < 0 && master_ct)
1247 nf_ct_put(master_ct);
1248
c1d10adb
PNA
1249 return err;
1250 }
1251 /* implicit 'else' */
1252
c1d10adb
PNA
1253 /* We manipulate the conntrack inside the global conntrack table lock,
1254 * so there's no need to increase the refcount */
c1d10adb 1255 err = -EEXIST;
ff4ca827
PNA
1256 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1257 /* we only allow nat config for new conntracks */
df6fb868 1258 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
0adf9d67 1259 err = -EOPNOTSUPP;
ff4ca827
PNA
1260 goto out_unlock;
1261 }
5faa1f4c
PNA
1262 /* can't link an existing conntrack to a master */
1263 if (cda[CTA_TUPLE_MASTER]) {
0adf9d67 1264 err = -EOPNOTSUPP;
5faa1f4c
PNA
1265 goto out_unlock;
1266 }
ff4ca827
PNA
1267 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h),
1268 cda);
1269 }
c1d10adb
PNA
1270
1271out_unlock:
f8ba1aff 1272 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1273 return err;
1274}
1275
601e68e1
YH
1276/***********************************************************************
1277 * EXPECT
1278 ***********************************************************************/
c1d10adb
PNA
1279
1280static inline int
1281ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1282 const struct nf_conntrack_tuple *tuple,
1283 enum ctattr_expect type)
1284{
df6fb868 1285 struct nlattr *nest_parms;
601e68e1 1286
df6fb868
PM
1287 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1288 if (!nest_parms)
1289 goto nla_put_failure;
c1d10adb 1290 if (ctnetlink_dump_tuples(skb, tuple) < 0)
df6fb868
PM
1291 goto nla_put_failure;
1292 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
1293
1294 return 0;
1295
df6fb868 1296nla_put_failure:
c1d10adb 1297 return -1;
601e68e1 1298}
c1d10adb 1299
1cde6436
PNA
1300static inline int
1301ctnetlink_exp_dump_mask(struct sk_buff *skb,
1302 const struct nf_conntrack_tuple *tuple,
d4156e8c 1303 const struct nf_conntrack_tuple_mask *mask)
1cde6436
PNA
1304{
1305 int ret;
1306 struct nf_conntrack_l3proto *l3proto;
605dcad6 1307 struct nf_conntrack_l4proto *l4proto;
d4156e8c 1308 struct nf_conntrack_tuple m;
df6fb868 1309 struct nlattr *nest_parms;
d4156e8c
PM
1310
1311 memset(&m, 0xFF, sizeof(m));
1312 m.src.u.all = mask->src.u.all;
1313 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1314
df6fb868
PM
1315 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1316 if (!nest_parms)
1317 goto nla_put_failure;
1cde6436
PNA
1318
1319 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
d4156e8c 1320 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1cde6436
PNA
1321 nf_ct_l3proto_put(l3proto);
1322
1323 if (unlikely(ret < 0))
df6fb868 1324 goto nla_put_failure;
1cde6436 1325
605dcad6 1326 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
d4156e8c 1327 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
605dcad6 1328 nf_ct_l4proto_put(l4proto);
1cde6436 1329 if (unlikely(ret < 0))
df6fb868 1330 goto nla_put_failure;
1cde6436 1331
df6fb868 1332 nla_nest_end(skb, nest_parms);
1cde6436
PNA
1333
1334 return 0;
1335
df6fb868 1336nla_put_failure:
1cde6436
PNA
1337 return -1;
1338}
1339
bb5cf80e 1340static int
c1d10adb 1341ctnetlink_exp_dump_expect(struct sk_buff *skb,
601e68e1 1342 const struct nf_conntrack_expect *exp)
c1d10adb
PNA
1343{
1344 struct nf_conn *master = exp->master;
d978e5da
PM
1345 long timeout = (exp->timeout.expires - jiffies) / HZ;
1346
1347 if (timeout < 0)
1348 timeout = 0;
c1d10adb
PNA
1349
1350 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
df6fb868 1351 goto nla_put_failure;
1cde6436 1352 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
df6fb868 1353 goto nla_put_failure;
c1d10adb
PNA
1354 if (ctnetlink_exp_dump_tuple(skb,
1355 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1356 CTA_EXPECT_MASTER) < 0)
df6fb868 1357 goto nla_put_failure;
601e68e1 1358
d978e5da 1359 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
77236b6e 1360 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
c1d10adb
PNA
1361
1362 return 0;
601e68e1 1363
df6fb868 1364nla_put_failure:
c1d10adb
PNA
1365 return -1;
1366}
1367
1368static int
1369ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
601e68e1
YH
1370 int event,
1371 int nowait,
c1d10adb
PNA
1372 const struct nf_conntrack_expect *exp)
1373{
1374 struct nlmsghdr *nlh;
1375 struct nfgenmsg *nfmsg;
27a884dc 1376 unsigned char *b = skb_tail_pointer(skb);
c1d10adb
PNA
1377
1378 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1379 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1380 nfmsg = NLMSG_DATA(nlh);
1381
1382 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1383 nfmsg->nfgen_family = exp->tuple.src.l3num;
1384 nfmsg->version = NFNETLINK_V0;
1385 nfmsg->res_id = 0;
1386
1387 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 1388 goto nla_put_failure;
c1d10adb 1389
27a884dc 1390 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
c1d10adb
PNA
1391 return skb->len;
1392
1393nlmsg_failure:
df6fb868 1394nla_put_failure:
dc5fc579 1395 nlmsg_trim(skb, b);
c1d10adb
PNA
1396 return -1;
1397}
1398
1399#ifdef CONFIG_NF_CONNTRACK_EVENTS
1400static int ctnetlink_expect_event(struct notifier_block *this,
1401 unsigned long events, void *ptr)
1402{
1403 struct nlmsghdr *nlh;
1404 struct nfgenmsg *nfmsg;
1405 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1406 struct sk_buff *skb;
1407 unsigned int type;
27a884dc 1408 sk_buff_data_t b;
c1d10adb
PNA
1409 int flags = 0;
1410
1411 if (events & IPEXP_NEW) {
1412 type = IPCTNL_MSG_EXP_NEW;
1413 flags = NLM_F_CREATE|NLM_F_EXCL;
1414 } else
1415 return NOTIFY_DONE;
1416
b3a27bfb
PNA
1417 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1418 return NOTIFY_DONE;
1419
c1d10adb
PNA
1420 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1421 if (!skb)
1422 return NOTIFY_DONE;
1423
1424 b = skb->tail;
1425
b633ad5f 1426 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
c1d10adb
PNA
1427 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1428 nfmsg = NLMSG_DATA(nlh);
1429
1430 nlh->nlmsg_flags = flags;
1431 nfmsg->nfgen_family = exp->tuple.src.l3num;
1432 nfmsg->version = NFNETLINK_V0;
1433 nfmsg->res_id = 0;
1434
1435 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 1436 goto nla_put_failure;
c1d10adb
PNA
1437
1438 nlh->nlmsg_len = skb->tail - b;
1439 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1440 return NOTIFY_DONE;
1441
1442nlmsg_failure:
df6fb868 1443nla_put_failure:
c1d10adb
PNA
1444 kfree_skb(skb);
1445 return NOTIFY_DONE;
1446}
1447#endif
cf6994c2
PM
1448static int ctnetlink_exp_done(struct netlink_callback *cb)
1449{
31f15875
PM
1450 if (cb->args[1])
1451 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
cf6994c2
PM
1452 return 0;
1453}
c1d10adb
PNA
1454
1455static int
1456ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1457{
cf6994c2 1458 struct nf_conntrack_expect *exp, *last;
87711cb8 1459 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
31f15875 1460 struct hlist_node *n;
87711cb8 1461 u_int8_t l3proto = nfmsg->nfgen_family;
c1d10adb 1462
7d0742da 1463 rcu_read_lock();
31f15875
PM
1464 last = (struct nf_conntrack_expect *)cb->args[1];
1465 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
cf6994c2 1466restart:
31f15875
PM
1467 hlist_for_each_entry(exp, n, &nf_ct_expect_hash[cb->args[0]],
1468 hnode) {
1469 if (l3proto && exp->tuple.src.l3num != l3proto)
cf6994c2 1470 continue;
31f15875
PM
1471 if (cb->args[1]) {
1472 if (exp != last)
1473 continue;
1474 cb->args[1] = 0;
1475 }
1476 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1477 cb->nlh->nlmsg_seq,
1478 IPCTNL_MSG_EXP_NEW,
1479 1, exp) < 0) {
7d0742da
PM
1480 if (!atomic_inc_not_zero(&exp->use))
1481 continue;
31f15875
PM
1482 cb->args[1] = (unsigned long)exp;
1483 goto out;
1484 }
cf6994c2 1485 }
31f15875
PM
1486 if (cb->args[1]) {
1487 cb->args[1] = 0;
1488 goto restart;
cf6994c2
PM
1489 }
1490 }
601e68e1 1491out:
7d0742da 1492 rcu_read_unlock();
cf6994c2
PM
1493 if (last)
1494 nf_ct_expect_put(last);
c1d10adb 1495
c1d10adb
PNA
1496 return skb->len;
1497}
1498
f73e924c
PM
1499static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1500 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1501 [CTA_EXPECT_ID] = { .type = NLA_U32 },
c1d10adb
PNA
1502};
1503
1504static int
601e68e1 1505ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
df6fb868 1506 struct nlmsghdr *nlh, struct nlattr *cda[])
c1d10adb
PNA
1507{
1508 struct nf_conntrack_tuple tuple;
1509 struct nf_conntrack_expect *exp;
1510 struct sk_buff *skb2;
1511 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1512 u_int8_t u3 = nfmsg->nfgen_family;
1513 int err = 0;
1514
c1d10adb 1515 if (nlh->nlmsg_flags & NLM_F_DUMP) {
c702e804
TG
1516 return netlink_dump_start(ctnl, skb, nlh,
1517 ctnetlink_exp_dump_table,
cf6994c2 1518 ctnetlink_exp_done);
c1d10adb
PNA
1519 }
1520
df6fb868 1521 if (cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
1522 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1523 else
1524 return -EINVAL;
1525
1526 if (err < 0)
1527 return err;
1528
6823645d 1529 exp = nf_ct_expect_find_get(&tuple);
c1d10adb
PNA
1530 if (!exp)
1531 return -ENOENT;
1532
df6fb868 1533 if (cda[CTA_EXPECT_ID]) {
77236b6e 1534 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 1535 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 1536 nf_ct_expect_put(exp);
c1d10adb
PNA
1537 return -ENOENT;
1538 }
601e68e1 1539 }
c1d10adb
PNA
1540
1541 err = -ENOMEM;
1542 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1543 if (!skb2)
1544 goto out;
4e9b8269 1545
601e68e1 1546 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
c1d10adb
PNA
1547 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1548 1, exp);
1549 if (err <= 0)
1550 goto free;
1551
6823645d 1552 nf_ct_expect_put(exp);
c1d10adb
PNA
1553
1554 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1555
1556free:
1557 kfree_skb(skb2);
1558out:
6823645d 1559 nf_ct_expect_put(exp);
c1d10adb
PNA
1560 return err;
1561}
1562
1563static int
601e68e1 1564ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
df6fb868 1565 struct nlmsghdr *nlh, struct nlattr *cda[])
c1d10adb 1566{
31f15875 1567 struct nf_conntrack_expect *exp;
c1d10adb
PNA
1568 struct nf_conntrack_tuple tuple;
1569 struct nf_conntrack_helper *h;
1570 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
31f15875 1571 struct hlist_node *n, *next;
c1d10adb 1572 u_int8_t u3 = nfmsg->nfgen_family;
31f15875 1573 unsigned int i;
c1d10adb
PNA
1574 int err;
1575
df6fb868 1576 if (cda[CTA_EXPECT_TUPLE]) {
c1d10adb
PNA
1577 /* delete a single expect by tuple */
1578 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1579 if (err < 0)
1580 return err;
1581
1582 /* bump usage count to 2 */
6823645d 1583 exp = nf_ct_expect_find_get(&tuple);
c1d10adb
PNA
1584 if (!exp)
1585 return -ENOENT;
1586
df6fb868 1587 if (cda[CTA_EXPECT_ID]) {
77236b6e 1588 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 1589 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 1590 nf_ct_expect_put(exp);
c1d10adb
PNA
1591 return -ENOENT;
1592 }
1593 }
1594
1595 /* after list removal, usage count == 1 */
6823645d 1596 nf_ct_unexpect_related(exp);
601e68e1 1597 /* have to put what we 'get' above.
c1d10adb 1598 * after this line usage count == 0 */
6823645d 1599 nf_ct_expect_put(exp);
df6fb868
PM
1600 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1601 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
31f15875 1602 struct nf_conn_help *m_help;
c1d10adb
PNA
1603
1604 /* delete all expectations for this helper */
f8ba1aff 1605 spin_lock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1606 h = __nf_conntrack_helper_find_byname(name);
1607 if (!h) {
f8ba1aff 1608 spin_unlock_bh(&nf_conntrack_lock);
0adf9d67 1609 return -EOPNOTSUPP;
c1d10adb 1610 }
31f15875
PM
1611 for (i = 0; i < nf_ct_expect_hsize; i++) {
1612 hlist_for_each_entry_safe(exp, n, next,
1613 &nf_ct_expect_hash[i],
1614 hnode) {
1615 m_help = nfct_help(exp->master);
1616 if (m_help->helper == h
1617 && del_timer(&exp->timeout)) {
1618 nf_ct_unlink_expect(exp);
1619 nf_ct_expect_put(exp);
1620 }
c1d10adb
PNA
1621 }
1622 }
f8ba1aff 1623 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1624 } else {
1625 /* This basically means we have to flush everything*/
f8ba1aff 1626 spin_lock_bh(&nf_conntrack_lock);
31f15875
PM
1627 for (i = 0; i < nf_ct_expect_hsize; i++) {
1628 hlist_for_each_entry_safe(exp, n, next,
1629 &nf_ct_expect_hash[i],
1630 hnode) {
1631 if (del_timer(&exp->timeout)) {
1632 nf_ct_unlink_expect(exp);
1633 nf_ct_expect_put(exp);
1634 }
c1d10adb
PNA
1635 }
1636 }
f8ba1aff 1637 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1638 }
1639
1640 return 0;
1641}
1642static int
df6fb868 1643ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
c1d10adb
PNA
1644{
1645 return -EOPNOTSUPP;
1646}
1647
1648static int
df6fb868 1649ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3)
c1d10adb
PNA
1650{
1651 struct nf_conntrack_tuple tuple, mask, master_tuple;
1652 struct nf_conntrack_tuple_hash *h = NULL;
1653 struct nf_conntrack_expect *exp;
1654 struct nf_conn *ct;
dc808fe2 1655 struct nf_conn_help *help;
c1d10adb
PNA
1656 int err = 0;
1657
c1d10adb
PNA
1658 /* caller guarantees that those three CTA_EXPECT_* exist */
1659 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1660 if (err < 0)
1661 return err;
1662 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1663 if (err < 0)
1664 return err;
1665 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1666 if (err < 0)
1667 return err;
1668
1669 /* Look for master conntrack of this expectation */
330f7db5 1670 h = nf_conntrack_find_get(&master_tuple);
c1d10adb
PNA
1671 if (!h)
1672 return -ENOENT;
1673 ct = nf_ct_tuplehash_to_ctrack(h);
dc808fe2 1674 help = nfct_help(ct);
c1d10adb 1675
dc808fe2 1676 if (!help || !help->helper) {
c1d10adb
PNA
1677 /* such conntrack hasn't got any helper, abort */
1678 err = -EINVAL;
1679 goto out;
1680 }
1681
6823645d 1682 exp = nf_ct_expect_alloc(ct);
c1d10adb
PNA
1683 if (!exp) {
1684 err = -ENOMEM;
1685 goto out;
1686 }
601e68e1 1687
c1d10adb
PNA
1688 exp->expectfn = NULL;
1689 exp->flags = 0;
1690 exp->master = ct;
9457d851 1691 exp->helper = NULL;
c1d10adb 1692 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
d4156e8c
PM
1693 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1694 exp->mask.src.u.all = mask.src.u.all;
c1d10adb 1695
6823645d
PM
1696 err = nf_ct_expect_related(exp);
1697 nf_ct_expect_put(exp);
c1d10adb 1698
601e68e1 1699out:
c1d10adb
PNA
1700 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1701 return err;
1702}
1703
1704static int
1705ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
df6fb868 1706 struct nlmsghdr *nlh, struct nlattr *cda[])
c1d10adb
PNA
1707{
1708 struct nf_conntrack_tuple tuple;
1709 struct nf_conntrack_expect *exp;
1710 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1711 u_int8_t u3 = nfmsg->nfgen_family;
1712 int err = 0;
1713
df6fb868
PM
1714 if (!cda[CTA_EXPECT_TUPLE]
1715 || !cda[CTA_EXPECT_MASK]
1716 || !cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
1717 return -EINVAL;
1718
1719 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1720 if (err < 0)
1721 return err;
1722
f8ba1aff 1723 spin_lock_bh(&nf_conntrack_lock);
6823645d 1724 exp = __nf_ct_expect_find(&tuple);
c1d10adb
PNA
1725
1726 if (!exp) {
f8ba1aff 1727 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1728 err = -ENOENT;
1729 if (nlh->nlmsg_flags & NLM_F_CREATE)
1730 err = ctnetlink_create_expect(cda, u3);
1731 return err;
1732 }
1733
1734 err = -EEXIST;
1735 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1736 err = ctnetlink_change_expect(exp, cda);
f8ba1aff 1737 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb 1738
c1d10adb
PNA
1739 return err;
1740}
1741
1742#ifdef CONFIG_NF_CONNTRACK_EVENTS
1743static struct notifier_block ctnl_notifier = {
1744 .notifier_call = ctnetlink_conntrack_event,
1745};
1746
1747static struct notifier_block ctnl_notifier_exp = {
1748 .notifier_call = ctnetlink_expect_event,
1749};
1750#endif
1751
7c8d4cb4 1752static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
c1d10adb 1753 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
f73e924c
PM
1754 .attr_count = CTA_MAX,
1755 .policy = ct_nla_policy },
c1d10adb 1756 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
1757 .attr_count = CTA_MAX,
1758 .policy = ct_nla_policy },
c1d10adb 1759 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
f73e924c
PM
1760 .attr_count = CTA_MAX,
1761 .policy = ct_nla_policy },
c1d10adb 1762 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
1763 .attr_count = CTA_MAX,
1764 .policy = ct_nla_policy },
c1d10adb
PNA
1765};
1766
7c8d4cb4 1767static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
c1d10adb 1768 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
f73e924c
PM
1769 .attr_count = CTA_EXPECT_MAX,
1770 .policy = exp_nla_policy },
c1d10adb 1771 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
f73e924c
PM
1772 .attr_count = CTA_EXPECT_MAX,
1773 .policy = exp_nla_policy },
c1d10adb 1774 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
f73e924c
PM
1775 .attr_count = CTA_EXPECT_MAX,
1776 .policy = exp_nla_policy },
c1d10adb
PNA
1777};
1778
7c8d4cb4 1779static const struct nfnetlink_subsystem ctnl_subsys = {
c1d10adb
PNA
1780 .name = "conntrack",
1781 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1782 .cb_count = IPCTNL_MSG_MAX,
1783 .cb = ctnl_cb,
1784};
1785
7c8d4cb4 1786static const struct nfnetlink_subsystem ctnl_exp_subsys = {
c1d10adb
PNA
1787 .name = "conntrack_expect",
1788 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1789 .cb_count = IPCTNL_MSG_EXP_MAX,
1790 .cb = ctnl_exp_cb,
1791};
1792
d2483dde 1793MODULE_ALIAS("ip_conntrack_netlink");
c1d10adb 1794MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
34f9a2e4 1795MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
c1d10adb
PNA
1796
1797static int __init ctnetlink_init(void)
1798{
1799 int ret;
1800
1801 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1802 ret = nfnetlink_subsys_register(&ctnl_subsys);
1803 if (ret < 0) {
1804 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1805 goto err_out;
1806 }
1807
1808 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1809 if (ret < 0) {
1810 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1811 goto err_unreg_subsys;
1812 }
1813
1814#ifdef CONFIG_NF_CONNTRACK_EVENTS
1815 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1816 if (ret < 0) {
1817 printk("ctnetlink_init: cannot register notifier.\n");
1818 goto err_unreg_exp_subsys;
1819 }
1820
6823645d 1821 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
c1d10adb
PNA
1822 if (ret < 0) {
1823 printk("ctnetlink_init: cannot expect register notifier.\n");
1824 goto err_unreg_notifier;
1825 }
1826#endif
1827
1828 return 0;
1829
1830#ifdef CONFIG_NF_CONNTRACK_EVENTS
1831err_unreg_notifier:
1832 nf_conntrack_unregister_notifier(&ctnl_notifier);
1833err_unreg_exp_subsys:
1834 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1835#endif
1836err_unreg_subsys:
1837 nfnetlink_subsys_unregister(&ctnl_subsys);
1838err_out:
1839 return ret;
1840}
1841
1842static void __exit ctnetlink_exit(void)
1843{
1844 printk("ctnetlink: unregistering from nfnetlink.\n");
1845
1846#ifdef CONFIG_NF_CONNTRACK_EVENTS
6823645d 1847 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
c1d10adb
PNA
1848 nf_conntrack_unregister_notifier(&ctnl_notifier);
1849#endif
1850
1851 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1852 nfnetlink_subsys_unregister(&ctnl_subsys);
1853 return;
1854}
1855
1856module_init(ctnetlink_init);
1857module_exit(ctnetlink_exit);