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