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