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