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