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