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