Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jbarnes...
[sfrench/cifs-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         if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
586                 return -ENOBUFS;
587
588         return 0;
589 }
590 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
591
592 static int ctnetlink_done(struct netlink_callback *cb)
593 {
594         if (cb->args[1])
595                 nf_ct_put((struct nf_conn *)cb->args[1]);
596         return 0;
597 }
598
599 static int
600 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
601 {
602         struct net *net = sock_net(skb->sk);
603         struct nf_conn *ct, *last;
604         struct nf_conntrack_tuple_hash *h;
605         struct hlist_nulls_node *n;
606         struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
607         u_int8_t l3proto = nfmsg->nfgen_family;
608
609         rcu_read_lock();
610         last = (struct nf_conn *)cb->args[1];
611         for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
612 restart:
613                 hlist_nulls_for_each_entry_rcu(h, n, &net->ct.hash[cb->args[0]],
614                                          hnnode) {
615                         if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
616                                 continue;
617                         ct = nf_ct_tuplehash_to_ctrack(h);
618                         if (!atomic_inc_not_zero(&ct->ct_general.use))
619                                 continue;
620                         /* Dump entries of a given L3 protocol number.
621                          * If it is not specified, ie. l3proto == 0,
622                          * then dump everything. */
623                         if (l3proto && nf_ct_l3num(ct) != l3proto)
624                                 goto releasect;
625                         if (cb->args[1]) {
626                                 if (ct != last)
627                                         goto releasect;
628                                 cb->args[1] = 0;
629                         }
630                         if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
631                                                 cb->nlh->nlmsg_seq,
632                                                 IPCTNL_MSG_CT_NEW, ct) < 0) {
633                                 cb->args[1] = (unsigned long)ct;
634                                 goto out;
635                         }
636
637                         if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
638                                                 IPCTNL_MSG_CT_GET_CTRZERO) {
639                                 struct nf_conn_counter *acct;
640
641                                 acct = nf_conn_acct_find(ct);
642                                 if (acct)
643                                         memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
644                         }
645 releasect:
646                 nf_ct_put(ct);
647                 }
648                 if (cb->args[1]) {
649                         cb->args[1] = 0;
650                         goto restart;
651                 }
652         }
653 out:
654         rcu_read_unlock();
655         if (last)
656                 nf_ct_put(last);
657
658         return skb->len;
659 }
660
661 static inline int
662 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
663 {
664         struct nlattr *tb[CTA_IP_MAX+1];
665         struct nf_conntrack_l3proto *l3proto;
666         int ret = 0;
667
668         nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
669
670         rcu_read_lock();
671         l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
672
673         if (likely(l3proto->nlattr_to_tuple)) {
674                 ret = nla_validate_nested(attr, CTA_IP_MAX,
675                                           l3proto->nla_policy);
676                 if (ret == 0)
677                         ret = l3proto->nlattr_to_tuple(tb, tuple);
678         }
679
680         rcu_read_unlock();
681
682         return ret;
683 }
684
685 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
686         [CTA_PROTO_NUM] = { .type = NLA_U8 },
687 };
688
689 static inline int
690 ctnetlink_parse_tuple_proto(struct nlattr *attr,
691                             struct nf_conntrack_tuple *tuple)
692 {
693         struct nlattr *tb[CTA_PROTO_MAX+1];
694         struct nf_conntrack_l4proto *l4proto;
695         int ret = 0;
696
697         ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
698         if (ret < 0)
699                 return ret;
700
701         if (!tb[CTA_PROTO_NUM])
702                 return -EINVAL;
703         tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
704
705         rcu_read_lock();
706         l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
707
708         if (likely(l4proto->nlattr_to_tuple)) {
709                 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
710                                           l4proto->nla_policy);
711                 if (ret == 0)
712                         ret = l4proto->nlattr_to_tuple(tb, tuple);
713         }
714
715         rcu_read_unlock();
716
717         return ret;
718 }
719
720 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
721         [CTA_TUPLE_IP]          = { .type = NLA_NESTED },
722         [CTA_TUPLE_PROTO]       = { .type = NLA_NESTED },
723 };
724
725 static int
726 ctnetlink_parse_tuple(const struct nlattr * const cda[],
727                       struct nf_conntrack_tuple *tuple,
728                       enum ctattr_tuple type, u_int8_t l3num)
729 {
730         struct nlattr *tb[CTA_TUPLE_MAX+1];
731         int err;
732
733         memset(tuple, 0, sizeof(*tuple));
734
735         nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
736
737         if (!tb[CTA_TUPLE_IP])
738                 return -EINVAL;
739
740         tuple->src.l3num = l3num;
741
742         err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
743         if (err < 0)
744                 return err;
745
746         if (!tb[CTA_TUPLE_PROTO])
747                 return -EINVAL;
748
749         err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
750         if (err < 0)
751                 return err;
752
753         /* orig and expect tuples get DIR_ORIGINAL */
754         if (type == CTA_TUPLE_REPLY)
755                 tuple->dst.dir = IP_CT_DIR_REPLY;
756         else
757                 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
758
759         return 0;
760 }
761
762 static int
763 ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
764 {
765         if (attr)
766 #ifdef CONFIG_NF_CONNTRACK_ZONES
767                 *zone = ntohs(nla_get_be16(attr));
768 #else
769                 return -EOPNOTSUPP;
770 #endif
771         else
772                 *zone = 0;
773
774         return 0;
775 }
776
777 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
778         [CTA_HELP_NAME]         = { .type = NLA_NUL_STRING },
779 };
780
781 static inline int
782 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name)
783 {
784         struct nlattr *tb[CTA_HELP_MAX+1];
785
786         nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
787
788         if (!tb[CTA_HELP_NAME])
789                 return -EINVAL;
790
791         *helper_name = nla_data(tb[CTA_HELP_NAME]);
792
793         return 0;
794 }
795
796 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
797         [CTA_TUPLE_ORIG]        = { .type = NLA_NESTED },
798         [CTA_TUPLE_REPLY]       = { .type = NLA_NESTED },
799         [CTA_STATUS]            = { .type = NLA_U32 },
800         [CTA_PROTOINFO]         = { .type = NLA_NESTED },
801         [CTA_HELP]              = { .type = NLA_NESTED },
802         [CTA_NAT_SRC]           = { .type = NLA_NESTED },
803         [CTA_TIMEOUT]           = { .type = NLA_U32 },
804         [CTA_MARK]              = { .type = NLA_U32 },
805         [CTA_ID]                = { .type = NLA_U32 },
806         [CTA_NAT_DST]           = { .type = NLA_NESTED },
807         [CTA_TUPLE_MASTER]      = { .type = NLA_NESTED },
808         [CTA_ZONE]              = { .type = NLA_U16 },
809 };
810
811 static int
812 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
813                         const struct nlmsghdr *nlh,
814                         const struct nlattr * const cda[])
815 {
816         struct net *net = sock_net(ctnl);
817         struct nf_conntrack_tuple_hash *h;
818         struct nf_conntrack_tuple tuple;
819         struct nf_conn *ct;
820         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
821         u_int8_t u3 = nfmsg->nfgen_family;
822         u16 zone;
823         int err;
824
825         err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
826         if (err < 0)
827                 return err;
828
829         if (cda[CTA_TUPLE_ORIG])
830                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
831         else if (cda[CTA_TUPLE_REPLY])
832                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
833         else {
834                 /* Flush the whole table */
835                 nf_conntrack_flush_report(net,
836                                          NETLINK_CB(skb).pid,
837                                          nlmsg_report(nlh));
838                 return 0;
839         }
840
841         if (err < 0)
842                 return err;
843
844         h = nf_conntrack_find_get(net, zone, &tuple);
845         if (!h)
846                 return -ENOENT;
847
848         ct = nf_ct_tuplehash_to_ctrack(h);
849
850         if (cda[CTA_ID]) {
851                 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
852                 if (id != (u32)(unsigned long)ct) {
853                         nf_ct_put(ct);
854                         return -ENOENT;
855                 }
856         }
857
858         if (nf_conntrack_event_report(IPCT_DESTROY, ct,
859                                       NETLINK_CB(skb).pid,
860                                       nlmsg_report(nlh)) < 0) {
861                 nf_ct_delete_from_lists(ct);
862                 /* we failed to report the event, try later */
863                 nf_ct_insert_dying_list(ct);
864                 nf_ct_put(ct);
865                 return 0;
866         }
867
868         /* death_by_timeout would report the event again */
869         set_bit(IPS_DYING_BIT, &ct->status);
870
871         nf_ct_kill(ct);
872         nf_ct_put(ct);
873
874         return 0;
875 }
876
877 static int
878 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
879                         const struct nlmsghdr *nlh,
880                         const struct nlattr * const cda[])
881 {
882         struct net *net = sock_net(ctnl);
883         struct nf_conntrack_tuple_hash *h;
884         struct nf_conntrack_tuple tuple;
885         struct nf_conn *ct;
886         struct sk_buff *skb2 = NULL;
887         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
888         u_int8_t u3 = nfmsg->nfgen_family;
889         u16 zone;
890         int err;
891
892         if (nlh->nlmsg_flags & NLM_F_DUMP)
893                 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
894                                           ctnetlink_done);
895
896         err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
897         if (err < 0)
898                 return err;
899
900         if (cda[CTA_TUPLE_ORIG])
901                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
902         else if (cda[CTA_TUPLE_REPLY])
903                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
904         else
905                 return -EINVAL;
906
907         if (err < 0)
908                 return err;
909
910         h = nf_conntrack_find_get(net, zone, &tuple);
911         if (!h)
912                 return -ENOENT;
913
914         ct = nf_ct_tuplehash_to_ctrack(h);
915
916         err = -ENOMEM;
917         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
918         if (skb2 == NULL) {
919                 nf_ct_put(ct);
920                 return -ENOMEM;
921         }
922
923         rcu_read_lock();
924         err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
925                                   IPCTNL_MSG_CT_NEW, ct);
926         rcu_read_unlock();
927         nf_ct_put(ct);
928         if (err <= 0)
929                 goto free;
930
931         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
932         if (err < 0)
933                 goto out;
934
935         return 0;
936
937 free:
938         kfree_skb(skb2);
939 out:
940         return err;
941 }
942
943 #ifdef CONFIG_NF_NAT_NEEDED
944 static int
945 ctnetlink_parse_nat_setup(struct nf_conn *ct,
946                           enum nf_nat_manip_type manip,
947                           const struct nlattr *attr)
948 {
949         typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
950
951         parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
952         if (!parse_nat_setup) {
953 #ifdef CONFIG_MODULES
954                 rcu_read_unlock();
955                 spin_unlock_bh(&nf_conntrack_lock);
956                 nfnl_unlock();
957                 if (request_module("nf-nat-ipv4") < 0) {
958                         nfnl_lock();
959                         spin_lock_bh(&nf_conntrack_lock);
960                         rcu_read_lock();
961                         return -EOPNOTSUPP;
962                 }
963                 nfnl_lock();
964                 spin_lock_bh(&nf_conntrack_lock);
965                 rcu_read_lock();
966                 if (nfnetlink_parse_nat_setup_hook)
967                         return -EAGAIN;
968 #endif
969                 return -EOPNOTSUPP;
970         }
971
972         return parse_nat_setup(ct, manip, attr);
973 }
974 #endif
975
976 static int
977 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
978 {
979         unsigned long d;
980         unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
981         d = ct->status ^ status;
982
983         if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
984                 /* unchangeable */
985                 return -EBUSY;
986
987         if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
988                 /* SEEN_REPLY bit can only be set */
989                 return -EBUSY;
990
991         if (d & IPS_ASSURED && !(status & IPS_ASSURED))
992                 /* ASSURED bit can only be set */
993                 return -EBUSY;
994
995         /* Be careful here, modifying NAT bits can screw up things,
996          * so don't let users modify them directly if they don't pass
997          * nf_nat_range. */
998         ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
999         return 0;
1000 }
1001
1002 static int
1003 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1004 {
1005 #ifdef CONFIG_NF_NAT_NEEDED
1006         int ret;
1007
1008         if (cda[CTA_NAT_DST]) {
1009                 ret = ctnetlink_parse_nat_setup(ct,
1010                                                 IP_NAT_MANIP_DST,
1011                                                 cda[CTA_NAT_DST]);
1012                 if (ret < 0)
1013                         return ret;
1014         }
1015         if (cda[CTA_NAT_SRC]) {
1016                 ret = ctnetlink_parse_nat_setup(ct,
1017                                                 IP_NAT_MANIP_SRC,
1018                                                 cda[CTA_NAT_SRC]);
1019                 if (ret < 0)
1020                         return ret;
1021         }
1022         return 0;
1023 #else
1024         return -EOPNOTSUPP;
1025 #endif
1026 }
1027
1028 static inline int
1029 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1030 {
1031         struct nf_conntrack_helper *helper;
1032         struct nf_conn_help *help = nfct_help(ct);
1033         char *helpname = NULL;
1034         int err;
1035
1036         /* don't change helper of sibling connections */
1037         if (ct->master)
1038                 return -EBUSY;
1039
1040         err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1041         if (err < 0)
1042                 return err;
1043
1044         if (!strcmp(helpname, "")) {
1045                 if (help && help->helper) {
1046                         /* we had a helper before ... */
1047                         nf_ct_remove_expectations(ct);
1048                         rcu_assign_pointer(help->helper, NULL);
1049                 }
1050
1051                 return 0;
1052         }
1053
1054         helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1055                                             nf_ct_protonum(ct));
1056         if (helper == NULL) {
1057 #ifdef CONFIG_MODULES
1058                 spin_unlock_bh(&nf_conntrack_lock);
1059
1060                 if (request_module("nfct-helper-%s", helpname) < 0) {
1061                         spin_lock_bh(&nf_conntrack_lock);
1062                         return -EOPNOTSUPP;
1063                 }
1064
1065                 spin_lock_bh(&nf_conntrack_lock);
1066                 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1067                                                     nf_ct_protonum(ct));
1068                 if (helper)
1069                         return -EAGAIN;
1070 #endif
1071                 return -EOPNOTSUPP;
1072         }
1073
1074         if (help) {
1075                 if (help->helper == helper)
1076                         return 0;
1077                 if (help->helper)
1078                         return -EBUSY;
1079                 /* need to zero data of old helper */
1080                 memset(&help->help, 0, sizeof(help->help));
1081         } else {
1082                 /* we cannot set a helper for an existing conntrack */
1083                 return -EOPNOTSUPP;
1084         }
1085
1086         rcu_assign_pointer(help->helper, helper);
1087
1088         return 0;
1089 }
1090
1091 static inline int
1092 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1093 {
1094         u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1095
1096         if (!del_timer(&ct->timeout))
1097                 return -ETIME;
1098
1099         ct->timeout.expires = jiffies + timeout * HZ;
1100         add_timer(&ct->timeout);
1101
1102         return 0;
1103 }
1104
1105 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1106         [CTA_PROTOINFO_TCP]     = { .type = NLA_NESTED },
1107         [CTA_PROTOINFO_DCCP]    = { .type = NLA_NESTED },
1108         [CTA_PROTOINFO_SCTP]    = { .type = NLA_NESTED },
1109 };
1110
1111 static inline int
1112 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1113 {
1114         const struct nlattr *attr = cda[CTA_PROTOINFO];
1115         struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1116         struct nf_conntrack_l4proto *l4proto;
1117         int err = 0;
1118
1119         nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1120
1121         rcu_read_lock();
1122         l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1123         if (l4proto->from_nlattr)
1124                 err = l4proto->from_nlattr(tb, ct);
1125         rcu_read_unlock();
1126
1127         return err;
1128 }
1129
1130 #ifdef CONFIG_NF_NAT_NEEDED
1131 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1132         [CTA_NAT_SEQ_CORRECTION_POS]    = { .type = NLA_U32 },
1133         [CTA_NAT_SEQ_OFFSET_BEFORE]     = { .type = NLA_U32 },
1134         [CTA_NAT_SEQ_OFFSET_AFTER]      = { .type = NLA_U32 },
1135 };
1136
1137 static inline int
1138 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1139 {
1140         struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1141
1142         nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1143
1144         if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1145                 return -EINVAL;
1146
1147         natseq->correction_pos =
1148                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1149
1150         if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1151                 return -EINVAL;
1152
1153         natseq->offset_before =
1154                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1155
1156         if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1157                 return -EINVAL;
1158
1159         natseq->offset_after =
1160                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1161
1162         return 0;
1163 }
1164
1165 static int
1166 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1167                              const struct nlattr * const cda[])
1168 {
1169         int ret = 0;
1170         struct nf_conn_nat *nat = nfct_nat(ct);
1171
1172         if (!nat)
1173                 return 0;
1174
1175         if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1176                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1177                                          cda[CTA_NAT_SEQ_ADJ_ORIG]);
1178                 if (ret < 0)
1179                         return ret;
1180
1181                 ct->status |= IPS_SEQ_ADJUST;
1182         }
1183
1184         if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1185                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1186                                          cda[CTA_NAT_SEQ_ADJ_REPLY]);
1187                 if (ret < 0)
1188                         return ret;
1189
1190                 ct->status |= IPS_SEQ_ADJUST;
1191         }
1192
1193         return 0;
1194 }
1195 #endif
1196
1197 static int
1198 ctnetlink_change_conntrack(struct nf_conn *ct,
1199                            const struct nlattr * const cda[])
1200 {
1201         int err;
1202
1203         /* only allow NAT changes and master assignation for new conntracks */
1204         if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1205                 return -EOPNOTSUPP;
1206
1207         if (cda[CTA_HELP]) {
1208                 err = ctnetlink_change_helper(ct, cda);
1209                 if (err < 0)
1210                         return err;
1211         }
1212
1213         if (cda[CTA_TIMEOUT]) {
1214                 err = ctnetlink_change_timeout(ct, cda);
1215                 if (err < 0)
1216                         return err;
1217         }
1218
1219         if (cda[CTA_STATUS]) {
1220                 err = ctnetlink_change_status(ct, cda);
1221                 if (err < 0)
1222                         return err;
1223         }
1224
1225         if (cda[CTA_PROTOINFO]) {
1226                 err = ctnetlink_change_protoinfo(ct, cda);
1227                 if (err < 0)
1228                         return err;
1229         }
1230
1231 #if defined(CONFIG_NF_CONNTRACK_MARK)
1232         if (cda[CTA_MARK])
1233                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1234 #endif
1235
1236 #ifdef CONFIG_NF_NAT_NEEDED
1237         if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1238                 err = ctnetlink_change_nat_seq_adj(ct, cda);
1239                 if (err < 0)
1240                         return err;
1241         }
1242 #endif
1243
1244         return 0;
1245 }
1246
1247 static struct nf_conn *
1248 ctnetlink_create_conntrack(struct net *net, u16 zone,
1249                            const struct nlattr * const cda[],
1250                            struct nf_conntrack_tuple *otuple,
1251                            struct nf_conntrack_tuple *rtuple,
1252                            u8 u3)
1253 {
1254         struct nf_conn *ct;
1255         int err = -EINVAL;
1256         struct nf_conntrack_helper *helper;
1257
1258         ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1259         if (IS_ERR(ct))
1260                 return ERR_PTR(-ENOMEM);
1261
1262         if (!cda[CTA_TIMEOUT])
1263                 goto err1;
1264         ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1265
1266         ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1267
1268         rcu_read_lock();
1269         if (cda[CTA_HELP]) {
1270                 char *helpname = NULL;
1271  
1272                 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1273                 if (err < 0)
1274                         goto err2;
1275
1276                 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1277                                                     nf_ct_protonum(ct));
1278                 if (helper == NULL) {
1279                         rcu_read_unlock();
1280 #ifdef CONFIG_MODULES
1281                         if (request_module("nfct-helper-%s", helpname) < 0) {
1282                                 err = -EOPNOTSUPP;
1283                                 goto err1;
1284                         }
1285
1286                         rcu_read_lock();
1287                         helper = __nf_conntrack_helper_find(helpname,
1288                                                             nf_ct_l3num(ct),
1289                                                             nf_ct_protonum(ct));
1290                         if (helper) {
1291                                 err = -EAGAIN;
1292                                 goto err2;
1293                         }
1294                         rcu_read_unlock();
1295 #endif
1296                         err = -EOPNOTSUPP;
1297                         goto err1;
1298                 } else {
1299                         struct nf_conn_help *help;
1300
1301                         help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1302                         if (help == NULL) {
1303                                 err = -ENOMEM;
1304                                 goto err2;
1305                         }
1306
1307                         /* not in hash table yet so not strictly necessary */
1308                         rcu_assign_pointer(help->helper, helper);
1309                 }
1310         } else {
1311                 /* try an implicit helper assignation */
1312                 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1313                 if (err < 0)
1314                         goto err2;
1315         }
1316
1317         if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1318                 err = ctnetlink_change_nat(ct, cda);
1319                 if (err < 0)
1320                         goto err2;
1321         }
1322
1323         nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1324         nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1325         /* we must add conntrack extensions before confirmation. */
1326         ct->status |= IPS_CONFIRMED;
1327
1328         if (cda[CTA_STATUS]) {
1329                 err = ctnetlink_change_status(ct, cda);
1330                 if (err < 0)
1331                         goto err2;
1332         }
1333
1334 #ifdef CONFIG_NF_NAT_NEEDED
1335         if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1336                 err = ctnetlink_change_nat_seq_adj(ct, cda);
1337                 if (err < 0)
1338                         goto err2;
1339         }
1340 #endif
1341
1342         if (cda[CTA_PROTOINFO]) {
1343                 err = ctnetlink_change_protoinfo(ct, cda);
1344                 if (err < 0)
1345                         goto err2;
1346         }
1347
1348 #if defined(CONFIG_NF_CONNTRACK_MARK)
1349         if (cda[CTA_MARK])
1350                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1351 #endif
1352
1353         /* setup master conntrack: this is a confirmed expectation */
1354         if (cda[CTA_TUPLE_MASTER]) {
1355                 struct nf_conntrack_tuple master;
1356                 struct nf_conntrack_tuple_hash *master_h;
1357                 struct nf_conn *master_ct;
1358
1359                 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1360                 if (err < 0)
1361                         goto err2;
1362
1363                 master_h = nf_conntrack_find_get(net, zone, &master);
1364                 if (master_h == NULL) {
1365                         err = -ENOENT;
1366                         goto err2;
1367                 }
1368                 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1369                 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1370                 ct->master = master_ct;
1371         }
1372
1373         add_timer(&ct->timeout);
1374         nf_conntrack_hash_insert(ct);
1375         rcu_read_unlock();
1376
1377         return ct;
1378
1379 err2:
1380         rcu_read_unlock();
1381 err1:
1382         nf_conntrack_free(ct);
1383         return ERR_PTR(err);
1384 }
1385
1386 static int
1387 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1388                         const struct nlmsghdr *nlh,
1389                         const struct nlattr * const cda[])
1390 {
1391         struct net *net = sock_net(ctnl);
1392         struct nf_conntrack_tuple otuple, rtuple;
1393         struct nf_conntrack_tuple_hash *h = NULL;
1394         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1395         u_int8_t u3 = nfmsg->nfgen_family;
1396         u16 zone;
1397         int err;
1398
1399         err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1400         if (err < 0)
1401                 return err;
1402
1403         if (cda[CTA_TUPLE_ORIG]) {
1404                 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1405                 if (err < 0)
1406                         return err;
1407         }
1408
1409         if (cda[CTA_TUPLE_REPLY]) {
1410                 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1411                 if (err < 0)
1412                         return err;
1413         }
1414
1415         spin_lock_bh(&nf_conntrack_lock);
1416         if (cda[CTA_TUPLE_ORIG])
1417                 h = __nf_conntrack_find(net, zone, &otuple);
1418         else if (cda[CTA_TUPLE_REPLY])
1419                 h = __nf_conntrack_find(net, zone, &rtuple);
1420
1421         if (h == NULL) {
1422                 err = -ENOENT;
1423                 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1424                         struct nf_conn *ct;
1425                         enum ip_conntrack_events events;
1426
1427                         ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1428                                                         &rtuple, u3);
1429                         if (IS_ERR(ct)) {
1430                                 err = PTR_ERR(ct);
1431                                 goto out_unlock;
1432                         }
1433                         err = 0;
1434                         nf_conntrack_get(&ct->ct_general);
1435                         spin_unlock_bh(&nf_conntrack_lock);
1436                         if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1437                                 events = IPCT_RELATED;
1438                         else
1439                                 events = IPCT_NEW;
1440
1441                         nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1442                                                       (1 << IPCT_ASSURED) |
1443                                                       (1 << IPCT_HELPER) |
1444                                                       (1 << IPCT_PROTOINFO) |
1445                                                       (1 << IPCT_NATSEQADJ) |
1446                                                       (1 << IPCT_MARK) | events,
1447                                                       ct, NETLINK_CB(skb).pid,
1448                                                       nlmsg_report(nlh));
1449                         nf_ct_put(ct);
1450                 } else
1451                         spin_unlock_bh(&nf_conntrack_lock);
1452
1453                 return err;
1454         }
1455         /* implicit 'else' */
1456
1457         /* We manipulate the conntrack inside the global conntrack table lock,
1458          * so there's no need to increase the refcount */
1459         err = -EEXIST;
1460         if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1461                 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1462
1463                 err = ctnetlink_change_conntrack(ct, cda);
1464                 if (err == 0) {
1465                         nf_conntrack_get(&ct->ct_general);
1466                         spin_unlock_bh(&nf_conntrack_lock);
1467                         nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1468                                                       (1 << IPCT_ASSURED) |
1469                                                       (1 << IPCT_HELPER) |
1470                                                       (1 << IPCT_PROTOINFO) |
1471                                                       (1 << IPCT_NATSEQADJ) |
1472                                                       (1 << IPCT_MARK),
1473                                                       ct, NETLINK_CB(skb).pid,
1474                                                       nlmsg_report(nlh));
1475                         nf_ct_put(ct);
1476                 } else
1477                         spin_unlock_bh(&nf_conntrack_lock);
1478
1479                 return err;
1480         }
1481
1482 out_unlock:
1483         spin_unlock_bh(&nf_conntrack_lock);
1484         return err;
1485 }
1486
1487 /***********************************************************************
1488  * EXPECT
1489  ***********************************************************************/
1490
1491 static inline int
1492 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1493                          const struct nf_conntrack_tuple *tuple,
1494                          enum ctattr_expect type)
1495 {
1496         struct nlattr *nest_parms;
1497
1498         nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1499         if (!nest_parms)
1500                 goto nla_put_failure;
1501         if (ctnetlink_dump_tuples(skb, tuple) < 0)
1502                 goto nla_put_failure;
1503         nla_nest_end(skb, nest_parms);
1504
1505         return 0;
1506
1507 nla_put_failure:
1508         return -1;
1509 }
1510
1511 static inline int
1512 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1513                         const struct nf_conntrack_tuple *tuple,
1514                         const struct nf_conntrack_tuple_mask *mask)
1515 {
1516         int ret;
1517         struct nf_conntrack_l3proto *l3proto;
1518         struct nf_conntrack_l4proto *l4proto;
1519         struct nf_conntrack_tuple m;
1520         struct nlattr *nest_parms;
1521
1522         memset(&m, 0xFF, sizeof(m));
1523         memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1524         m.src.u.all = mask->src.u.all;
1525         m.dst.protonum = tuple->dst.protonum;
1526
1527         nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1528         if (!nest_parms)
1529                 goto nla_put_failure;
1530
1531         l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1532         ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1533
1534         if (unlikely(ret < 0))
1535                 goto nla_put_failure;
1536
1537         l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1538         ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1539         if (unlikely(ret < 0))
1540                 goto nla_put_failure;
1541
1542         nla_nest_end(skb, nest_parms);
1543
1544         return 0;
1545
1546 nla_put_failure:
1547         return -1;
1548 }
1549
1550 static int
1551 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1552                           const struct nf_conntrack_expect *exp)
1553 {
1554         struct nf_conn *master = exp->master;
1555         struct nf_conntrack_helper *helper;
1556         long timeout = (exp->timeout.expires - jiffies) / HZ;
1557
1558         if (timeout < 0)
1559                 timeout = 0;
1560
1561         if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1562                 goto nla_put_failure;
1563         if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1564                 goto nla_put_failure;
1565         if (ctnetlink_exp_dump_tuple(skb,
1566                                  &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1567                                  CTA_EXPECT_MASTER) < 0)
1568                 goto nla_put_failure;
1569
1570         NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1571         NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1572         helper = rcu_dereference(nfct_help(master)->helper);
1573         if (helper)
1574                 NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
1575
1576         return 0;
1577
1578 nla_put_failure:
1579         return -1;
1580 }
1581
1582 static int
1583 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1584                         int event, const struct nf_conntrack_expect *exp)
1585 {
1586         struct nlmsghdr *nlh;
1587         struct nfgenmsg *nfmsg;
1588         unsigned int flags = pid ? NLM_F_MULTI : 0;
1589
1590         event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1591         nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1592         if (nlh == NULL)
1593                 goto nlmsg_failure;
1594
1595         nfmsg = nlmsg_data(nlh);
1596         nfmsg->nfgen_family = exp->tuple.src.l3num;
1597         nfmsg->version      = NFNETLINK_V0;
1598         nfmsg->res_id       = 0;
1599
1600         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1601                 goto nla_put_failure;
1602
1603         nlmsg_end(skb, nlh);
1604         return skb->len;
1605
1606 nlmsg_failure:
1607 nla_put_failure:
1608         nlmsg_cancel(skb, nlh);
1609         return -1;
1610 }
1611
1612 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1613 static int
1614 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
1615 {
1616         struct nf_conntrack_expect *exp = item->exp;
1617         struct net *net = nf_ct_exp_net(exp);
1618         struct nlmsghdr *nlh;
1619         struct nfgenmsg *nfmsg;
1620         struct sk_buff *skb;
1621         unsigned int type;
1622         int flags = 0;
1623
1624         if (events & (1 << IPEXP_NEW)) {
1625                 type = IPCTNL_MSG_EXP_NEW;
1626                 flags = NLM_F_CREATE|NLM_F_EXCL;
1627         } else
1628                 return 0;
1629
1630         if (!item->report &&
1631             !nfnetlink_has_listeners(net, NFNLGRP_CONNTRACK_EXP_NEW))
1632                 return 0;
1633
1634         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1635         if (skb == NULL)
1636                 goto errout;
1637
1638         type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1639         nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1640         if (nlh == NULL)
1641                 goto nlmsg_failure;
1642
1643         nfmsg = nlmsg_data(nlh);
1644         nfmsg->nfgen_family = exp->tuple.src.l3num;
1645         nfmsg->version      = NFNETLINK_V0;
1646         nfmsg->res_id       = 0;
1647
1648         rcu_read_lock();
1649         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1650                 goto nla_put_failure;
1651         rcu_read_unlock();
1652
1653         nlmsg_end(skb, nlh);
1654         nfnetlink_send(skb, net, item->pid, NFNLGRP_CONNTRACK_EXP_NEW,
1655                        item->report, GFP_ATOMIC);
1656         return 0;
1657
1658 nla_put_failure:
1659         rcu_read_unlock();
1660         nlmsg_cancel(skb, nlh);
1661 nlmsg_failure:
1662         kfree_skb(skb);
1663 errout:
1664         nfnetlink_set_err(net, 0, 0, -ENOBUFS);
1665         return 0;
1666 }
1667 #endif
1668 static int ctnetlink_exp_done(struct netlink_callback *cb)
1669 {
1670         if (cb->args[1])
1671                 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1672         return 0;
1673 }
1674
1675 static int
1676 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1677 {
1678         struct net *net = sock_net(skb->sk);
1679         struct nf_conntrack_expect *exp, *last;
1680         struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1681         struct hlist_node *n;
1682         u_int8_t l3proto = nfmsg->nfgen_family;
1683
1684         rcu_read_lock();
1685         last = (struct nf_conntrack_expect *)cb->args[1];
1686         for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1687 restart:
1688                 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1689                                      hnode) {
1690                         if (l3proto && exp->tuple.src.l3num != l3proto)
1691                                 continue;
1692                         if (cb->args[1]) {
1693                                 if (exp != last)
1694                                         continue;
1695                                 cb->args[1] = 0;
1696                         }
1697                         if (ctnetlink_exp_fill_info(skb,
1698                                                     NETLINK_CB(cb->skb).pid,
1699                                                     cb->nlh->nlmsg_seq,
1700                                                     IPCTNL_MSG_EXP_NEW,
1701                                                     exp) < 0) {
1702                                 if (!atomic_inc_not_zero(&exp->use))
1703                                         continue;
1704                                 cb->args[1] = (unsigned long)exp;
1705                                 goto out;
1706                         }
1707                 }
1708                 if (cb->args[1]) {
1709                         cb->args[1] = 0;
1710                         goto restart;
1711                 }
1712         }
1713 out:
1714         rcu_read_unlock();
1715         if (last)
1716                 nf_ct_expect_put(last);
1717
1718         return skb->len;
1719 }
1720
1721 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1722         [CTA_EXPECT_MASTER]     = { .type = NLA_NESTED },
1723         [CTA_EXPECT_TUPLE]      = { .type = NLA_NESTED },
1724         [CTA_EXPECT_MASK]       = { .type = NLA_NESTED },
1725         [CTA_EXPECT_TIMEOUT]    = { .type = NLA_U32 },
1726         [CTA_EXPECT_ID]         = { .type = NLA_U32 },
1727         [CTA_EXPECT_HELP_NAME]  = { .type = NLA_NUL_STRING },
1728 };
1729
1730 static int
1731 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1732                      const struct nlmsghdr *nlh,
1733                      const struct nlattr * const cda[])
1734 {
1735         struct net *net = sock_net(ctnl);
1736         struct nf_conntrack_tuple tuple;
1737         struct nf_conntrack_expect *exp;
1738         struct sk_buff *skb2;
1739         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1740         u_int8_t u3 = nfmsg->nfgen_family;
1741         u16 zone;
1742         int err;
1743
1744         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1745                 return netlink_dump_start(ctnl, skb, nlh,
1746                                           ctnetlink_exp_dump_table,
1747                                           ctnetlink_exp_done);
1748         }
1749
1750         err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1751         if (err < 0)
1752                 return err;
1753
1754         if (cda[CTA_EXPECT_MASTER])
1755                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1756         else
1757                 return -EINVAL;
1758
1759         if (err < 0)
1760                 return err;
1761
1762         exp = nf_ct_expect_find_get(net, zone, &tuple);
1763         if (!exp)
1764                 return -ENOENT;
1765
1766         if (cda[CTA_EXPECT_ID]) {
1767                 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1768                 if (ntohl(id) != (u32)(unsigned long)exp) {
1769                         nf_ct_expect_put(exp);
1770                         return -ENOENT;
1771                 }
1772         }
1773
1774         err = -ENOMEM;
1775         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1776         if (skb2 == NULL)
1777                 goto out;
1778
1779         rcu_read_lock();
1780         err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1781                                       nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1782         rcu_read_unlock();
1783         if (err <= 0)
1784                 goto free;
1785
1786         nf_ct_expect_put(exp);
1787
1788         return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1789
1790 free:
1791         kfree_skb(skb2);
1792 out:
1793         nf_ct_expect_put(exp);
1794         return err;
1795 }
1796
1797 static int
1798 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1799                      const struct nlmsghdr *nlh,
1800                      const struct nlattr * const cda[])
1801 {
1802         struct net *net = sock_net(ctnl);
1803         struct nf_conntrack_expect *exp;
1804         struct nf_conntrack_tuple tuple;
1805         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1806         struct hlist_node *n, *next;
1807         u_int8_t u3 = nfmsg->nfgen_family;
1808         unsigned int i;
1809         u16 zone;
1810         int err;
1811
1812         if (cda[CTA_EXPECT_TUPLE]) {
1813                 /* delete a single expect by tuple */
1814                 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1815                 if (err < 0)
1816                         return err;
1817
1818                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1819                 if (err < 0)
1820                         return err;
1821
1822                 /* bump usage count to 2 */
1823                 exp = nf_ct_expect_find_get(net, zone, &tuple);
1824                 if (!exp)
1825                         return -ENOENT;
1826
1827                 if (cda[CTA_EXPECT_ID]) {
1828                         __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1829                         if (ntohl(id) != (u32)(unsigned long)exp) {
1830                                 nf_ct_expect_put(exp);
1831                                 return -ENOENT;
1832                         }
1833                 }
1834
1835                 /* after list removal, usage count == 1 */
1836                 nf_ct_unexpect_related(exp);
1837                 /* have to put what we 'get' above.
1838                  * after this line usage count == 0 */
1839                 nf_ct_expect_put(exp);
1840         } else if (cda[CTA_EXPECT_HELP_NAME]) {
1841                 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1842                 struct nf_conn_help *m_help;
1843
1844                 /* delete all expectations for this helper */
1845                 spin_lock_bh(&nf_conntrack_lock);
1846                 for (i = 0; i < nf_ct_expect_hsize; i++) {
1847                         hlist_for_each_entry_safe(exp, n, next,
1848                                                   &net->ct.expect_hash[i],
1849                                                   hnode) {
1850                                 m_help = nfct_help(exp->master);
1851                                 if (!strcmp(m_help->helper->name, name) &&
1852                                     del_timer(&exp->timeout)) {
1853                                         nf_ct_unlink_expect(exp);
1854                                         nf_ct_expect_put(exp);
1855                                 }
1856                         }
1857                 }
1858                 spin_unlock_bh(&nf_conntrack_lock);
1859         } else {
1860                 /* This basically means we have to flush everything*/
1861                 spin_lock_bh(&nf_conntrack_lock);
1862                 for (i = 0; i < nf_ct_expect_hsize; i++) {
1863                         hlist_for_each_entry_safe(exp, n, next,
1864                                                   &net->ct.expect_hash[i],
1865                                                   hnode) {
1866                                 if (del_timer(&exp->timeout)) {
1867                                         nf_ct_unlink_expect(exp);
1868                                         nf_ct_expect_put(exp);
1869                                 }
1870                         }
1871                 }
1872                 spin_unlock_bh(&nf_conntrack_lock);
1873         }
1874
1875         return 0;
1876 }
1877 static int
1878 ctnetlink_change_expect(struct nf_conntrack_expect *x,
1879                         const struct nlattr * const cda[])
1880 {
1881         return -EOPNOTSUPP;
1882 }
1883
1884 static int
1885 ctnetlink_create_expect(struct net *net, u16 zone,
1886                         const struct nlattr * const cda[],
1887                         u_int8_t u3,
1888                         u32 pid, int report)
1889 {
1890         struct nf_conntrack_tuple tuple, mask, master_tuple;
1891         struct nf_conntrack_tuple_hash *h = NULL;
1892         struct nf_conntrack_expect *exp;
1893         struct nf_conn *ct;
1894         struct nf_conn_help *help;
1895         int err = 0;
1896
1897         /* caller guarantees that those three CTA_EXPECT_* exist */
1898         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1899         if (err < 0)
1900                 return err;
1901         err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1902         if (err < 0)
1903                 return err;
1904         err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1905         if (err < 0)
1906                 return err;
1907
1908         /* Look for master conntrack of this expectation */
1909         h = nf_conntrack_find_get(net, zone, &master_tuple);
1910         if (!h)
1911                 return -ENOENT;
1912         ct = nf_ct_tuplehash_to_ctrack(h);
1913         help = nfct_help(ct);
1914
1915         if (!help || !help->helper) {
1916                 /* such conntrack hasn't got any helper, abort */
1917                 err = -EOPNOTSUPP;
1918                 goto out;
1919         }
1920
1921         exp = nf_ct_expect_alloc(ct);
1922         if (!exp) {
1923                 err = -ENOMEM;
1924                 goto out;
1925         }
1926
1927         exp->class = 0;
1928         exp->expectfn = NULL;
1929         exp->flags = 0;
1930         exp->master = ct;
1931         exp->helper = NULL;
1932         memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1933         memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1934         exp->mask.src.u.all = mask.src.u.all;
1935
1936         err = nf_ct_expect_related_report(exp, pid, report);
1937         nf_ct_expect_put(exp);
1938
1939 out:
1940         nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1941         return err;
1942 }
1943
1944 static int
1945 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1946                      const struct nlmsghdr *nlh,
1947                      const struct nlattr * const cda[])
1948 {
1949         struct net *net = sock_net(ctnl);
1950         struct nf_conntrack_tuple tuple;
1951         struct nf_conntrack_expect *exp;
1952         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1953         u_int8_t u3 = nfmsg->nfgen_family;
1954         u16 zone;
1955         int err;
1956
1957         if (!cda[CTA_EXPECT_TUPLE]
1958             || !cda[CTA_EXPECT_MASK]
1959             || !cda[CTA_EXPECT_MASTER])
1960                 return -EINVAL;
1961
1962         err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1963         if (err < 0)
1964                 return err;
1965
1966         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1967         if (err < 0)
1968                 return err;
1969
1970         spin_lock_bh(&nf_conntrack_lock);
1971         exp = __nf_ct_expect_find(net, zone, &tuple);
1972
1973         if (!exp) {
1974                 spin_unlock_bh(&nf_conntrack_lock);
1975                 err = -ENOENT;
1976                 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1977                         err = ctnetlink_create_expect(net, zone, cda,
1978                                                       u3,
1979                                                       NETLINK_CB(skb).pid,
1980                                                       nlmsg_report(nlh));
1981                 }
1982                 return err;
1983         }
1984
1985         err = -EEXIST;
1986         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1987                 err = ctnetlink_change_expect(exp, cda);
1988         spin_unlock_bh(&nf_conntrack_lock);
1989
1990         return err;
1991 }
1992
1993 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1994 static struct nf_ct_event_notifier ctnl_notifier = {
1995         .fcn = ctnetlink_conntrack_event,
1996 };
1997
1998 static struct nf_exp_event_notifier ctnl_notifier_exp = {
1999         .fcn = ctnetlink_expect_event,
2000 };
2001 #endif
2002
2003 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
2004         [IPCTNL_MSG_CT_NEW]             = { .call = ctnetlink_new_conntrack,
2005                                             .attr_count = CTA_MAX,
2006                                             .policy = ct_nla_policy },
2007         [IPCTNL_MSG_CT_GET]             = { .call = ctnetlink_get_conntrack,
2008                                             .attr_count = CTA_MAX,
2009                                             .policy = ct_nla_policy },
2010         [IPCTNL_MSG_CT_DELETE]          = { .call = ctnetlink_del_conntrack,
2011                                             .attr_count = CTA_MAX,
2012                                             .policy = ct_nla_policy },
2013         [IPCTNL_MSG_CT_GET_CTRZERO]     = { .call = ctnetlink_get_conntrack,
2014                                             .attr_count = CTA_MAX,
2015                                             .policy = ct_nla_policy },
2016 };
2017
2018 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
2019         [IPCTNL_MSG_EXP_GET]            = { .call = ctnetlink_get_expect,
2020                                             .attr_count = CTA_EXPECT_MAX,
2021                                             .policy = exp_nla_policy },
2022         [IPCTNL_MSG_EXP_NEW]            = { .call = ctnetlink_new_expect,
2023                                             .attr_count = CTA_EXPECT_MAX,
2024                                             .policy = exp_nla_policy },
2025         [IPCTNL_MSG_EXP_DELETE]         = { .call = ctnetlink_del_expect,
2026                                             .attr_count = CTA_EXPECT_MAX,
2027                                             .policy = exp_nla_policy },
2028 };
2029
2030 static const struct nfnetlink_subsystem ctnl_subsys = {
2031         .name                           = "conntrack",
2032         .subsys_id                      = NFNL_SUBSYS_CTNETLINK,
2033         .cb_count                       = IPCTNL_MSG_MAX,
2034         .cb                             = ctnl_cb,
2035 };
2036
2037 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
2038         .name                           = "conntrack_expect",
2039         .subsys_id                      = NFNL_SUBSYS_CTNETLINK_EXP,
2040         .cb_count                       = IPCTNL_MSG_EXP_MAX,
2041         .cb                             = ctnl_exp_cb,
2042 };
2043
2044 MODULE_ALIAS("ip_conntrack_netlink");
2045 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
2046 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
2047
2048 static int __init ctnetlink_init(void)
2049 {
2050         int ret;
2051
2052         printk("ctnetlink v%s: registering with nfnetlink.\n", version);
2053         ret = nfnetlink_subsys_register(&ctnl_subsys);
2054         if (ret < 0) {
2055                 printk("ctnetlink_init: cannot register with nfnetlink.\n");
2056                 goto err_out;
2057         }
2058
2059         ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2060         if (ret < 0) {
2061                 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
2062                 goto err_unreg_subsys;
2063         }
2064
2065 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2066         ret = nf_conntrack_register_notifier(&ctnl_notifier);
2067         if (ret < 0) {
2068                 printk("ctnetlink_init: cannot register notifier.\n");
2069                 goto err_unreg_exp_subsys;
2070         }
2071
2072         ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
2073         if (ret < 0) {
2074                 printk("ctnetlink_init: cannot expect register notifier.\n");
2075                 goto err_unreg_notifier;
2076         }
2077 #endif
2078
2079         return 0;
2080
2081 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2082 err_unreg_notifier:
2083         nf_conntrack_unregister_notifier(&ctnl_notifier);
2084 err_unreg_exp_subsys:
2085         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2086 #endif
2087 err_unreg_subsys:
2088         nfnetlink_subsys_unregister(&ctnl_subsys);
2089 err_out:
2090         return ret;
2091 }
2092
2093 static void __exit ctnetlink_exit(void)
2094 {
2095         printk("ctnetlink: unregistering from nfnetlink.\n");
2096
2097 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2098         nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
2099         nf_conntrack_unregister_notifier(&ctnl_notifier);
2100 #endif
2101
2102         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2103         nfnetlink_subsys_unregister(&ctnl_subsys);
2104         return;
2105 }
2106
2107 module_init(ctnetlink_init);
2108 module_exit(ctnetlink_exit);