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