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