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