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