netfilter: ctnetlink: cleanup conntrack update preliminary checkings
[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         l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
603
604         if (likely(l3proto->nlattr_to_tuple)) {
605                 ret = nla_validate_nested(attr, CTA_IP_MAX,
606                                           l3proto->nla_policy);
607                 if (ret == 0)
608                         ret = l3proto->nlattr_to_tuple(tb, tuple);
609         }
610
611         nf_ct_l3proto_put(l3proto);
612
613         return ret;
614 }
615
616 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
617         [CTA_PROTO_NUM] = { .type = NLA_U8 },
618 };
619
620 static inline int
621 ctnetlink_parse_tuple_proto(struct nlattr *attr,
622                             struct nf_conntrack_tuple *tuple)
623 {
624         struct nlattr *tb[CTA_PROTO_MAX+1];
625         struct nf_conntrack_l4proto *l4proto;
626         int ret = 0;
627
628         ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
629         if (ret < 0)
630                 return ret;
631
632         if (!tb[CTA_PROTO_NUM])
633                 return -EINVAL;
634         tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
635
636         l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
637
638         if (likely(l4proto->nlattr_to_tuple)) {
639                 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
640                                           l4proto->nla_policy);
641                 if (ret == 0)
642                         ret = l4proto->nlattr_to_tuple(tb, tuple);
643         }
644
645         nf_ct_l4proto_put(l4proto);
646
647         return ret;
648 }
649
650 static int
651 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
652                       enum ctattr_tuple type, u_int8_t l3num)
653 {
654         struct nlattr *tb[CTA_TUPLE_MAX+1];
655         int err;
656
657         memset(tuple, 0, sizeof(*tuple));
658
659         nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
660
661         if (!tb[CTA_TUPLE_IP])
662                 return -EINVAL;
663
664         tuple->src.l3num = l3num;
665
666         err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
667         if (err < 0)
668                 return err;
669
670         if (!tb[CTA_TUPLE_PROTO])
671                 return -EINVAL;
672
673         err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
674         if (err < 0)
675                 return err;
676
677         /* orig and expect tuples get DIR_ORIGINAL */
678         if (type == CTA_TUPLE_REPLY)
679                 tuple->dst.dir = IP_CT_DIR_REPLY;
680         else
681                 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
682
683         return 0;
684 }
685
686 static inline int
687 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
688 {
689         struct nlattr *tb[CTA_HELP_MAX+1];
690
691         nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
692
693         if (!tb[CTA_HELP_NAME])
694                 return -EINVAL;
695
696         *helper_name = nla_data(tb[CTA_HELP_NAME]);
697
698         return 0;
699 }
700
701 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
702         [CTA_STATUS]            = { .type = NLA_U32 },
703         [CTA_TIMEOUT]           = { .type = NLA_U32 },
704         [CTA_MARK]              = { .type = NLA_U32 },
705         [CTA_USE]               = { .type = NLA_U32 },
706         [CTA_ID]                = { .type = NLA_U32 },
707 };
708
709 static int
710 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
711                         struct nlmsghdr *nlh, struct nlattr *cda[])
712 {
713         struct nf_conntrack_tuple_hash *h;
714         struct nf_conntrack_tuple tuple;
715         struct nf_conn *ct;
716         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
717         u_int8_t u3 = nfmsg->nfgen_family;
718         int err = 0;
719
720         if (cda[CTA_TUPLE_ORIG])
721                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
722         else if (cda[CTA_TUPLE_REPLY])
723                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
724         else {
725                 /* Flush the whole table */
726                 nf_conntrack_flush(&init_net, 
727                                    NETLINK_CB(skb).pid, 
728                                    nlmsg_report(nlh));
729                 return 0;
730         }
731
732         if (err < 0)
733                 return err;
734
735         h = nf_conntrack_find_get(&init_net, &tuple);
736         if (!h)
737                 return -ENOENT;
738
739         ct = nf_ct_tuplehash_to_ctrack(h);
740
741         if (cda[CTA_ID]) {
742                 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
743                 if (id != (u32)(unsigned long)ct) {
744                         nf_ct_put(ct);
745                         return -ENOENT;
746                 }
747         }
748
749         nf_conntrack_event_report(IPCT_DESTROY,
750                                   ct,
751                                   NETLINK_CB(skb).pid,
752                                   nlmsg_report(nlh));
753
754         /* death_by_timeout would report the event again */
755         set_bit(IPS_DYING_BIT, &ct->status);
756
757         nf_ct_kill(ct);
758         nf_ct_put(ct);
759
760         return 0;
761 }
762
763 static int
764 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
765                         struct nlmsghdr *nlh, struct nlattr *cda[])
766 {
767         struct nf_conntrack_tuple_hash *h;
768         struct nf_conntrack_tuple tuple;
769         struct nf_conn *ct;
770         struct sk_buff *skb2 = NULL;
771         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
772         u_int8_t u3 = nfmsg->nfgen_family;
773         int err = 0;
774
775         if (nlh->nlmsg_flags & NLM_F_DUMP)
776                 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
777                                           ctnetlink_done);
778
779         if (cda[CTA_TUPLE_ORIG])
780                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
781         else if (cda[CTA_TUPLE_REPLY])
782                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
783         else
784                 return -EINVAL;
785
786         if (err < 0)
787                 return err;
788
789         h = nf_conntrack_find_get(&init_net, &tuple);
790         if (!h)
791                 return -ENOENT;
792
793         ct = nf_ct_tuplehash_to_ctrack(h);
794
795         err = -ENOMEM;
796         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
797         if (!skb2) {
798                 nf_ct_put(ct);
799                 return -ENOMEM;
800         }
801
802         rcu_read_lock();
803         err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
804                                   IPCTNL_MSG_CT_NEW, 1, ct);
805         rcu_read_unlock();
806         nf_ct_put(ct);
807         if (err <= 0)
808                 goto free;
809
810         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
811         if (err < 0)
812                 goto out;
813
814         return 0;
815
816 free:
817         kfree_skb(skb2);
818 out:
819         return err;
820 }
821
822 #ifdef CONFIG_NF_NAT_NEEDED
823 static int
824 ctnetlink_parse_nat_setup(struct nf_conn *ct,
825                           enum nf_nat_manip_type manip,
826                           struct nlattr *attr)
827 {
828         typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
829
830         parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
831         if (!parse_nat_setup) {
832 #ifdef CONFIG_MODULES
833                 rcu_read_unlock();
834                 spin_unlock_bh(&nf_conntrack_lock);
835                 nfnl_unlock();
836                 if (request_module("nf-nat-ipv4") < 0) {
837                         nfnl_lock();
838                         spin_lock_bh(&nf_conntrack_lock);
839                         rcu_read_lock();
840                         return -EOPNOTSUPP;
841                 }
842                 nfnl_lock();
843                 spin_lock_bh(&nf_conntrack_lock);
844                 rcu_read_lock();
845                 if (nfnetlink_parse_nat_setup_hook)
846                         return -EAGAIN;
847 #endif
848                 return -EOPNOTSUPP;
849         }
850
851         return parse_nat_setup(ct, manip, attr);
852 }
853 #endif
854
855 static int
856 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
857 {
858         unsigned long d;
859         unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
860         d = ct->status ^ status;
861
862         if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
863                 /* unchangeable */
864                 return -EBUSY;
865
866         if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
867                 /* SEEN_REPLY bit can only be set */
868                 return -EBUSY;
869
870         if (d & IPS_ASSURED && !(status & IPS_ASSURED))
871                 /* ASSURED bit can only be set */
872                 return -EBUSY;
873
874         /* Be careful here, modifying NAT bits can screw up things,
875          * so don't let users modify them directly if they don't pass
876          * nf_nat_range. */
877         ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
878         return 0;
879 }
880
881 static int
882 ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[])
883 {
884 #ifdef CONFIG_NF_NAT_NEEDED
885         int ret;
886
887         if (cda[CTA_NAT_DST]) {
888                 ret = ctnetlink_parse_nat_setup(ct,
889                                                 IP_NAT_MANIP_DST,
890                                                 cda[CTA_NAT_DST]);
891                 if (ret < 0)
892                         return ret;
893         }
894         if (cda[CTA_NAT_SRC]) {
895                 ret = ctnetlink_parse_nat_setup(ct,
896                                                 IP_NAT_MANIP_SRC,
897                                                 cda[CTA_NAT_SRC]);
898                 if (ret < 0)
899                         return ret;
900         }
901         return 0;
902 #else
903         return -EOPNOTSUPP;
904 #endif
905 }
906
907 static inline int
908 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
909 {
910         struct nf_conntrack_helper *helper;
911         struct nf_conn_help *help = nfct_help(ct);
912         char *helpname;
913         int err;
914
915         /* don't change helper of sibling connections */
916         if (ct->master)
917                 return -EBUSY;
918
919         err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
920         if (err < 0)
921                 return err;
922
923         if (!strcmp(helpname, "")) {
924                 if (help && help->helper) {
925                         /* we had a helper before ... */
926                         nf_ct_remove_expectations(ct);
927                         rcu_assign_pointer(help->helper, NULL);
928                 }
929
930                 return 0;
931         }
932
933         helper = __nf_conntrack_helper_find_byname(helpname);
934         if (helper == NULL) {
935 #ifdef CONFIG_MODULES
936                 spin_unlock_bh(&nf_conntrack_lock);
937
938                 if (request_module("nfct-helper-%s", helpname) < 0) {
939                         spin_lock_bh(&nf_conntrack_lock);
940                         return -EOPNOTSUPP;
941                 }
942
943                 spin_lock_bh(&nf_conntrack_lock);
944                 helper = __nf_conntrack_helper_find_byname(helpname);
945                 if (helper)
946                         return -EAGAIN;
947 #endif
948                 return -EOPNOTSUPP;
949         }
950
951         if (help) {
952                 if (help->helper == helper)
953                         return 0;
954                 if (help->helper)
955                         return -EBUSY;
956                 /* need to zero data of old helper */
957                 memset(&help->help, 0, sizeof(help->help));
958         } else {
959                 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
960                 if (help == NULL)
961                         return -ENOMEM;
962         }
963
964         rcu_assign_pointer(help->helper, helper);
965
966         return 0;
967 }
968
969 static inline int
970 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
971 {
972         u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
973
974         if (!del_timer(&ct->timeout))
975                 return -ETIME;
976
977         ct->timeout.expires = jiffies + timeout * HZ;
978         add_timer(&ct->timeout);
979
980         return 0;
981 }
982
983 static inline int
984 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
985 {
986         struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
987         struct nf_conntrack_l4proto *l4proto;
988         int err = 0;
989
990         nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
991
992         l4proto = nf_ct_l4proto_find_get(nf_ct_l3num(ct), nf_ct_protonum(ct));
993         if (l4proto->from_nlattr)
994                 err = l4proto->from_nlattr(tb, ct);
995         nf_ct_l4proto_put(l4proto);
996
997         return err;
998 }
999
1000 #ifdef CONFIG_NF_NAT_NEEDED
1001 static inline int
1002 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1003 {
1004         struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1005
1006         nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1007
1008         if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1009                 return -EINVAL;
1010
1011         natseq->correction_pos =
1012                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1013
1014         if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1015                 return -EINVAL;
1016
1017         natseq->offset_before =
1018                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1019
1020         if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1021                 return -EINVAL;
1022
1023         natseq->offset_after =
1024                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1025
1026         return 0;
1027 }
1028
1029 static int
1030 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1031 {
1032         int ret = 0;
1033         struct nf_conn_nat *nat = nfct_nat(ct);
1034
1035         if (!nat)
1036                 return 0;
1037
1038         if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1039                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1040                                          cda[CTA_NAT_SEQ_ADJ_ORIG]);
1041                 if (ret < 0)
1042                         return ret;
1043
1044                 ct->status |= IPS_SEQ_ADJUST;
1045         }
1046
1047         if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1048                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1049                                          cda[CTA_NAT_SEQ_ADJ_REPLY]);
1050                 if (ret < 0)
1051                         return ret;
1052
1053                 ct->status |= IPS_SEQ_ADJUST;
1054         }
1055
1056         return 0;
1057 }
1058 #endif
1059
1060 static int
1061 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1062 {
1063         int err;
1064
1065         /* only allow NAT changes and master assignation for new conntracks */
1066         if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1067                 return -EOPNOTSUPP;
1068
1069         if (cda[CTA_HELP]) {
1070                 err = ctnetlink_change_helper(ct, cda);
1071                 if (err < 0)
1072                         return err;
1073         }
1074
1075         if (cda[CTA_TIMEOUT]) {
1076                 err = ctnetlink_change_timeout(ct, cda);
1077                 if (err < 0)
1078                         return err;
1079         }
1080
1081         if (cda[CTA_STATUS]) {
1082                 err = ctnetlink_change_status(ct, cda);
1083                 if (err < 0)
1084                         return err;
1085         }
1086
1087         if (cda[CTA_PROTOINFO]) {
1088                 err = ctnetlink_change_protoinfo(ct, cda);
1089                 if (err < 0)
1090                         return err;
1091         }
1092
1093 #if defined(CONFIG_NF_CONNTRACK_MARK)
1094         if (cda[CTA_MARK])
1095                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1096 #endif
1097
1098 #ifdef CONFIG_NF_NAT_NEEDED
1099         if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1100                 err = ctnetlink_change_nat_seq_adj(ct, cda);
1101                 if (err < 0)
1102                         return err;
1103         }
1104 #endif
1105
1106         return 0;
1107 }
1108
1109 static inline void
1110 ctnetlink_event_report(struct nf_conn *ct, u32 pid, int report)
1111 {
1112         unsigned int events = 0;
1113
1114         if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1115                 events |= IPCT_RELATED;
1116         else
1117                 events |= IPCT_NEW;
1118
1119         nf_conntrack_event_report(IPCT_STATUS |
1120                                   IPCT_HELPER |
1121                                   IPCT_REFRESH |
1122                                   IPCT_PROTOINFO |
1123                                   IPCT_NATSEQADJ |
1124                                   IPCT_MARK |
1125                                   events,
1126                                   ct,
1127                                   pid,
1128                                   report);
1129 }
1130
1131 static int
1132 ctnetlink_create_conntrack(struct nlattr *cda[],
1133                            struct nf_conntrack_tuple *otuple,
1134                            struct nf_conntrack_tuple *rtuple,
1135                            u32 pid,
1136                            int report,
1137                            u8 u3)
1138 {
1139         struct nf_conn *ct;
1140         int err = -EINVAL;
1141         struct nf_conntrack_helper *helper;
1142
1143         ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_ATOMIC);
1144         if (IS_ERR(ct))
1145                 return -ENOMEM;
1146
1147         if (!cda[CTA_TIMEOUT])
1148                 goto err;
1149         ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1150
1151         ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1152         ct->status |= IPS_CONFIRMED;
1153
1154         rcu_read_lock();
1155         if (cda[CTA_HELP]) {
1156                 char *helpname;
1157  
1158                 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1159                 if (err < 0) {
1160                         rcu_read_unlock();
1161                         goto err;
1162                 }
1163
1164                 helper = __nf_conntrack_helper_find_byname(helpname);
1165                 if (helper == NULL) {
1166                         rcu_read_unlock();
1167 #ifdef CONFIG_MODULES
1168                         if (request_module("nfct-helper-%s", helpname) < 0) {
1169                                 err = -EOPNOTSUPP;
1170                                 goto err;
1171                         }
1172
1173                         rcu_read_lock();
1174                         helper = __nf_conntrack_helper_find_byname(helpname);
1175                         if (helper) {
1176                                 rcu_read_unlock();
1177                                 err = -EAGAIN;
1178                                 goto err;
1179                         }
1180                         rcu_read_unlock();
1181 #endif
1182                         err = -EOPNOTSUPP;
1183                         goto err;
1184                 } else {
1185                         struct nf_conn_help *help;
1186
1187                         help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1188                         if (help == NULL) {
1189                                 rcu_read_unlock();
1190                                 err = -ENOMEM;
1191                                 goto err;
1192                         }
1193
1194                         /* not in hash table yet so not strictly necessary */
1195                         rcu_assign_pointer(help->helper, helper);
1196                 }
1197         } else {
1198                 /* try an implicit helper assignation */
1199                 err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
1200                 if (err < 0) {
1201                         rcu_read_unlock();
1202                         goto err;
1203                 }
1204         }
1205
1206         if (cda[CTA_STATUS]) {
1207                 err = ctnetlink_change_status(ct, cda);
1208                 if (err < 0) {
1209                         rcu_read_unlock();
1210                         goto err;
1211                 }
1212         }
1213
1214         if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1215                 err = ctnetlink_change_nat(ct, cda);
1216                 if (err < 0) {
1217                         rcu_read_unlock();
1218                         goto err;
1219                 }
1220         }
1221
1222 #ifdef CONFIG_NF_NAT_NEEDED
1223         if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1224                 err = ctnetlink_change_nat_seq_adj(ct, cda);
1225                 if (err < 0) {
1226                         rcu_read_unlock();
1227                         goto err;
1228                 }
1229         }
1230 #endif
1231
1232         if (cda[CTA_PROTOINFO]) {
1233                 err = ctnetlink_change_protoinfo(ct, cda);
1234                 if (err < 0) {
1235                         rcu_read_unlock();
1236                         goto err;
1237                 }
1238         }
1239
1240         nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1241
1242 #if defined(CONFIG_NF_CONNTRACK_MARK)
1243         if (cda[CTA_MARK])
1244                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1245 #endif
1246
1247         /* setup master conntrack: this is a confirmed expectation */
1248         if (cda[CTA_TUPLE_MASTER]) {
1249                 struct nf_conntrack_tuple master;
1250                 struct nf_conntrack_tuple_hash *master_h;
1251                 struct nf_conn *master_ct;
1252
1253                 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1254                 if (err < 0)
1255                         goto err;
1256
1257                 master_h = __nf_conntrack_find(&init_net, &master);
1258                 if (master_h == NULL) {
1259                         err = -ENOENT;
1260                         goto err;
1261                 }
1262                 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1263                 nf_conntrack_get(&master_ct->ct_general);
1264                 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1265                 ct->master = master_ct;
1266         }
1267
1268         nf_conntrack_get(&ct->ct_general);
1269         add_timer(&ct->timeout);
1270         nf_conntrack_hash_insert(ct);
1271         rcu_read_unlock();
1272         ctnetlink_event_report(ct, pid, report);
1273         nf_ct_put(ct);
1274
1275         return 0;
1276
1277 err:
1278         nf_conntrack_free(ct);
1279         return err;
1280 }
1281
1282 static int
1283 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1284                         struct nlmsghdr *nlh, struct nlattr *cda[])
1285 {
1286         struct nf_conntrack_tuple otuple, rtuple;
1287         struct nf_conntrack_tuple_hash *h = NULL;
1288         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1289         u_int8_t u3 = nfmsg->nfgen_family;
1290         int err = 0;
1291
1292         if (cda[CTA_TUPLE_ORIG]) {
1293                 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1294                 if (err < 0)
1295                         return err;
1296         }
1297
1298         if (cda[CTA_TUPLE_REPLY]) {
1299                 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1300                 if (err < 0)
1301                         return err;
1302         }
1303
1304         spin_lock_bh(&nf_conntrack_lock);
1305         if (cda[CTA_TUPLE_ORIG])
1306                 h = __nf_conntrack_find(&init_net, &otuple);
1307         else if (cda[CTA_TUPLE_REPLY])
1308                 h = __nf_conntrack_find(&init_net, &rtuple);
1309
1310         if (h == NULL) {
1311                 err = -ENOENT;
1312                 if (nlh->nlmsg_flags & NLM_F_CREATE)
1313                         err = ctnetlink_create_conntrack(cda,
1314                                                          &otuple,
1315                                                          &rtuple,
1316                                                          NETLINK_CB(skb).pid,
1317                                                          nlmsg_report(nlh),
1318                                                          u3);
1319                 spin_unlock_bh(&nf_conntrack_lock);
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         kfree_skb(skb);
1521         return NOTIFY_DONE;
1522 }
1523 #endif
1524 static int ctnetlink_exp_done(struct netlink_callback *cb)
1525 {
1526         if (cb->args[1])
1527                 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1528         return 0;
1529 }
1530
1531 static int
1532 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1533 {
1534         struct net *net = &init_net;
1535         struct nf_conntrack_expect *exp, *last;
1536         struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1537         struct hlist_node *n;
1538         u_int8_t l3proto = nfmsg->nfgen_family;
1539
1540         rcu_read_lock();
1541         last = (struct nf_conntrack_expect *)cb->args[1];
1542         for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1543 restart:
1544                 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1545                                      hnode) {
1546                         if (l3proto && exp->tuple.src.l3num != l3proto)
1547                                 continue;
1548                         if (cb->args[1]) {
1549                                 if (exp != last)
1550                                         continue;
1551                                 cb->args[1] = 0;
1552                         }
1553                         if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1554                                                     cb->nlh->nlmsg_seq,
1555                                                     IPCTNL_MSG_EXP_NEW,
1556                                                     1, exp) < 0) {
1557                                 if (!atomic_inc_not_zero(&exp->use))
1558                                         continue;
1559                                 cb->args[1] = (unsigned long)exp;
1560                                 goto out;
1561                         }
1562                 }
1563                 if (cb->args[1]) {
1564                         cb->args[1] = 0;
1565                         goto restart;
1566                 }
1567         }
1568 out:
1569         rcu_read_unlock();
1570         if (last)
1571                 nf_ct_expect_put(last);
1572
1573         return skb->len;
1574 }
1575
1576 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1577         [CTA_EXPECT_TIMEOUT]    = { .type = NLA_U32 },
1578         [CTA_EXPECT_ID]         = { .type = NLA_U32 },
1579 };
1580
1581 static int
1582 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1583                      struct nlmsghdr *nlh, struct nlattr *cda[])
1584 {
1585         struct nf_conntrack_tuple tuple;
1586         struct nf_conntrack_expect *exp;
1587         struct sk_buff *skb2;
1588         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1589         u_int8_t u3 = nfmsg->nfgen_family;
1590         int err = 0;
1591
1592         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1593                 return netlink_dump_start(ctnl, skb, nlh,
1594                                           ctnetlink_exp_dump_table,
1595                                           ctnetlink_exp_done);
1596         }
1597
1598         if (cda[CTA_EXPECT_MASTER])
1599                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1600         else
1601                 return -EINVAL;
1602
1603         if (err < 0)
1604                 return err;
1605
1606         exp = nf_ct_expect_find_get(&init_net, &tuple);
1607         if (!exp)
1608                 return -ENOENT;
1609
1610         if (cda[CTA_EXPECT_ID]) {
1611                 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1612                 if (ntohl(id) != (u32)(unsigned long)exp) {
1613                         nf_ct_expect_put(exp);
1614                         return -ENOENT;
1615                 }
1616         }
1617
1618         err = -ENOMEM;
1619         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1620         if (!skb2)
1621                 goto out;
1622
1623         rcu_read_lock();
1624         err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1625                                       nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1626                                       1, exp);
1627         rcu_read_unlock();
1628         if (err <= 0)
1629                 goto free;
1630
1631         nf_ct_expect_put(exp);
1632
1633         return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1634
1635 free:
1636         kfree_skb(skb2);
1637 out:
1638         nf_ct_expect_put(exp);
1639         return err;
1640 }
1641
1642 static int
1643 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1644                      struct nlmsghdr *nlh, struct nlattr *cda[])
1645 {
1646         struct nf_conntrack_expect *exp;
1647         struct nf_conntrack_tuple tuple;
1648         struct nf_conntrack_helper *h;
1649         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1650         struct hlist_node *n, *next;
1651         u_int8_t u3 = nfmsg->nfgen_family;
1652         unsigned int i;
1653         int err;
1654
1655         if (cda[CTA_EXPECT_TUPLE]) {
1656                 /* delete a single expect by tuple */
1657                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1658                 if (err < 0)
1659                         return err;
1660
1661                 /* bump usage count to 2 */
1662                 exp = nf_ct_expect_find_get(&init_net, &tuple);
1663                 if (!exp)
1664                         return -ENOENT;
1665
1666                 if (cda[CTA_EXPECT_ID]) {
1667                         __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1668                         if (ntohl(id) != (u32)(unsigned long)exp) {
1669                                 nf_ct_expect_put(exp);
1670                                 return -ENOENT;
1671                         }
1672                 }
1673
1674                 /* after list removal, usage count == 1 */
1675                 nf_ct_unexpect_related(exp);
1676                 /* have to put what we 'get' above.
1677                  * after this line usage count == 0 */
1678                 nf_ct_expect_put(exp);
1679         } else if (cda[CTA_EXPECT_HELP_NAME]) {
1680                 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1681                 struct nf_conn_help *m_help;
1682
1683                 /* delete all expectations for this helper */
1684                 spin_lock_bh(&nf_conntrack_lock);
1685                 h = __nf_conntrack_helper_find_byname(name);
1686                 if (!h) {
1687                         spin_unlock_bh(&nf_conntrack_lock);
1688                         return -EOPNOTSUPP;
1689                 }
1690                 for (i = 0; i < nf_ct_expect_hsize; i++) {
1691                         hlist_for_each_entry_safe(exp, n, next,
1692                                                   &init_net.ct.expect_hash[i],
1693                                                   hnode) {
1694                                 m_help = nfct_help(exp->master);
1695                                 if (m_help->helper == h
1696                                     && del_timer(&exp->timeout)) {
1697                                         nf_ct_unlink_expect(exp);
1698                                         nf_ct_expect_put(exp);
1699                                 }
1700                         }
1701                 }
1702                 spin_unlock_bh(&nf_conntrack_lock);
1703         } else {
1704                 /* This basically means we have to flush everything*/
1705                 spin_lock_bh(&nf_conntrack_lock);
1706                 for (i = 0; i < nf_ct_expect_hsize; i++) {
1707                         hlist_for_each_entry_safe(exp, n, next,
1708                                                   &init_net.ct.expect_hash[i],
1709                                                   hnode) {
1710                                 if (del_timer(&exp->timeout)) {
1711                                         nf_ct_unlink_expect(exp);
1712                                         nf_ct_expect_put(exp);
1713                                 }
1714                         }
1715                 }
1716                 spin_unlock_bh(&nf_conntrack_lock);
1717         }
1718
1719         return 0;
1720 }
1721 static int
1722 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1723 {
1724         return -EOPNOTSUPP;
1725 }
1726
1727 static int
1728 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3, u32 pid, int report)
1729 {
1730         struct nf_conntrack_tuple tuple, mask, master_tuple;
1731         struct nf_conntrack_tuple_hash *h = NULL;
1732         struct nf_conntrack_expect *exp;
1733         struct nf_conn *ct;
1734         struct nf_conn_help *help;
1735         int err = 0;
1736
1737         /* caller guarantees that those three CTA_EXPECT_* exist */
1738         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1739         if (err < 0)
1740                 return err;
1741         err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1742         if (err < 0)
1743                 return err;
1744         err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1745         if (err < 0)
1746                 return err;
1747
1748         /* Look for master conntrack of this expectation */
1749         h = nf_conntrack_find_get(&init_net, &master_tuple);
1750         if (!h)
1751                 return -ENOENT;
1752         ct = nf_ct_tuplehash_to_ctrack(h);
1753         help = nfct_help(ct);
1754
1755         if (!help || !help->helper) {
1756                 /* such conntrack hasn't got any helper, abort */
1757                 err = -EOPNOTSUPP;
1758                 goto out;
1759         }
1760
1761         exp = nf_ct_expect_alloc(ct);
1762         if (!exp) {
1763                 err = -ENOMEM;
1764                 goto out;
1765         }
1766
1767         exp->expectfn = NULL;
1768         exp->flags = 0;
1769         exp->master = ct;
1770         exp->helper = NULL;
1771         memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1772         memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1773         exp->mask.src.u.all = mask.src.u.all;
1774
1775         err = nf_ct_expect_related_report(exp, pid, report);
1776         nf_ct_expect_put(exp);
1777
1778 out:
1779         nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1780         return err;
1781 }
1782
1783 static int
1784 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1785                      struct nlmsghdr *nlh, struct nlattr *cda[])
1786 {
1787         struct nf_conntrack_tuple tuple;
1788         struct nf_conntrack_expect *exp;
1789         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1790         u_int8_t u3 = nfmsg->nfgen_family;
1791         int err = 0;
1792
1793         if (!cda[CTA_EXPECT_TUPLE]
1794             || !cda[CTA_EXPECT_MASK]
1795             || !cda[CTA_EXPECT_MASTER])
1796                 return -EINVAL;
1797
1798         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1799         if (err < 0)
1800                 return err;
1801
1802         spin_lock_bh(&nf_conntrack_lock);
1803         exp = __nf_ct_expect_find(&init_net, &tuple);
1804
1805         if (!exp) {
1806                 spin_unlock_bh(&nf_conntrack_lock);
1807                 err = -ENOENT;
1808                 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1809                         err = ctnetlink_create_expect(cda,
1810                                                       u3,
1811                                                       NETLINK_CB(skb).pid,
1812                                                       nlmsg_report(nlh));
1813                 }
1814                 return err;
1815         }
1816
1817         err = -EEXIST;
1818         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1819                 err = ctnetlink_change_expect(exp, cda);
1820         spin_unlock_bh(&nf_conntrack_lock);
1821
1822         return err;
1823 }
1824
1825 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1826 static struct notifier_block ctnl_notifier = {
1827         .notifier_call  = ctnetlink_conntrack_event,
1828 };
1829
1830 static struct notifier_block ctnl_notifier_exp = {
1831         .notifier_call  = ctnetlink_expect_event,
1832 };
1833 #endif
1834
1835 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1836         [IPCTNL_MSG_CT_NEW]             = { .call = ctnetlink_new_conntrack,
1837                                             .attr_count = CTA_MAX,
1838                                             .policy = ct_nla_policy },
1839         [IPCTNL_MSG_CT_GET]             = { .call = ctnetlink_get_conntrack,
1840                                             .attr_count = CTA_MAX,
1841                                             .policy = ct_nla_policy },
1842         [IPCTNL_MSG_CT_DELETE]          = { .call = ctnetlink_del_conntrack,
1843                                             .attr_count = CTA_MAX,
1844                                             .policy = ct_nla_policy },
1845         [IPCTNL_MSG_CT_GET_CTRZERO]     = { .call = ctnetlink_get_conntrack,
1846                                             .attr_count = CTA_MAX,
1847                                             .policy = ct_nla_policy },
1848 };
1849
1850 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1851         [IPCTNL_MSG_EXP_GET]            = { .call = ctnetlink_get_expect,
1852                                             .attr_count = CTA_EXPECT_MAX,
1853                                             .policy = exp_nla_policy },
1854         [IPCTNL_MSG_EXP_NEW]            = { .call = ctnetlink_new_expect,
1855                                             .attr_count = CTA_EXPECT_MAX,
1856                                             .policy = exp_nla_policy },
1857         [IPCTNL_MSG_EXP_DELETE]         = { .call = ctnetlink_del_expect,
1858                                             .attr_count = CTA_EXPECT_MAX,
1859                                             .policy = exp_nla_policy },
1860 };
1861
1862 static const struct nfnetlink_subsystem ctnl_subsys = {
1863         .name                           = "conntrack",
1864         .subsys_id                      = NFNL_SUBSYS_CTNETLINK,
1865         .cb_count                       = IPCTNL_MSG_MAX,
1866         .cb                             = ctnl_cb,
1867 };
1868
1869 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1870         .name                           = "conntrack_expect",
1871         .subsys_id                      = NFNL_SUBSYS_CTNETLINK_EXP,
1872         .cb_count                       = IPCTNL_MSG_EXP_MAX,
1873         .cb                             = ctnl_exp_cb,
1874 };
1875
1876 MODULE_ALIAS("ip_conntrack_netlink");
1877 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1878 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1879
1880 static int __init ctnetlink_init(void)
1881 {
1882         int ret;
1883
1884         printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1885         ret = nfnetlink_subsys_register(&ctnl_subsys);
1886         if (ret < 0) {
1887                 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1888                 goto err_out;
1889         }
1890
1891         ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1892         if (ret < 0) {
1893                 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1894                 goto err_unreg_subsys;
1895         }
1896
1897 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1898         ret = nf_conntrack_register_notifier(&ctnl_notifier);
1899         if (ret < 0) {
1900                 printk("ctnetlink_init: cannot register notifier.\n");
1901                 goto err_unreg_exp_subsys;
1902         }
1903
1904         ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1905         if (ret < 0) {
1906                 printk("ctnetlink_init: cannot expect register notifier.\n");
1907                 goto err_unreg_notifier;
1908         }
1909 #endif
1910
1911         return 0;
1912
1913 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1914 err_unreg_notifier:
1915         nf_conntrack_unregister_notifier(&ctnl_notifier);
1916 err_unreg_exp_subsys:
1917         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1918 #endif
1919 err_unreg_subsys:
1920         nfnetlink_subsys_unregister(&ctnl_subsys);
1921 err_out:
1922         return ret;
1923 }
1924
1925 static void __exit ctnetlink_exit(void)
1926 {
1927         printk("ctnetlink: unregistering from nfnetlink.\n");
1928
1929 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1930         nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1931         nf_conntrack_unregister_notifier(&ctnl_notifier);
1932 #endif
1933
1934         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1935         nfnetlink_subsys_unregister(&ctnl_subsys);
1936         return;
1937 }
1938
1939 module_init(ctnetlink_init);
1940 module_exit(ctnetlink_exit);