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