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