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