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