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