Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/holtmann/bluet...
[linux-2.6] / net / netfilter / nf_conntrack_netlink.c
1 /* Connection tracking via netlink socket. Allows for user space
2  * protocol helpers and general trouble making from userspace.
3  *
4  * (C) 2001 by Jay Schulist <jschlst@samba.org>
5  * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
6  * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7  * (C) 2005-2008 by Pablo Neira Ayuso <pablo@netfilter.org>
8  *
9  * Initial connection tracking via netlink development funded and
10  * generally made possible by Network Robots, Inc. (www.networkrobots.com)
11  *
12  * Further development of this code funded by Astaro AG (http://www.astaro.com)
13  *
14  * This software may be used and distributed according to the terms
15  * of the GNU General Public License, incorporated herein by reference.
16  */
17
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/rculist.h>
22 #include <linux/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, &nf_conntrack_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 #ifdef CONFIG_NF_NAT_NEEDED
693 static const struct nla_policy protonat_nla_policy[CTA_PROTONAT_MAX+1] = {
694         [CTA_PROTONAT_PORT_MIN] = { .type = NLA_U16 },
695         [CTA_PROTONAT_PORT_MAX] = { .type = NLA_U16 },
696 };
697
698 static int nfnetlink_parse_nat_proto(struct nlattr *attr,
699                                      const struct nf_conn *ct,
700                                      struct nf_nat_range *range)
701 {
702         struct nlattr *tb[CTA_PROTONAT_MAX+1];
703         const struct nf_nat_protocol *npt;
704         int err;
705
706         err = nla_parse_nested(tb, CTA_PROTONAT_MAX, attr, protonat_nla_policy);
707         if (err < 0)
708                 return err;
709
710         npt = nf_nat_proto_find_get(nf_ct_protonum(ct));
711         if (npt->nlattr_to_range)
712                 err = npt->nlattr_to_range(tb, range);
713         nf_nat_proto_put(npt);
714         return err;
715 }
716
717 static const struct nla_policy nat_nla_policy[CTA_NAT_MAX+1] = {
718         [CTA_NAT_MINIP]         = { .type = NLA_U32 },
719         [CTA_NAT_MAXIP]         = { .type = NLA_U32 },
720 };
721
722 static inline int
723 nfnetlink_parse_nat(struct nlattr *nat,
724                     const struct nf_conn *ct, struct nf_nat_range *range)
725 {
726         struct nlattr *tb[CTA_NAT_MAX+1];
727         int err;
728
729         memset(range, 0, sizeof(*range));
730
731         err = nla_parse_nested(tb, CTA_NAT_MAX, nat, nat_nla_policy);
732         if (err < 0)
733                 return err;
734
735         if (tb[CTA_NAT_MINIP])
736                 range->min_ip = nla_get_be32(tb[CTA_NAT_MINIP]);
737
738         if (!tb[CTA_NAT_MAXIP])
739                 range->max_ip = range->min_ip;
740         else
741                 range->max_ip = nla_get_be32(tb[CTA_NAT_MAXIP]);
742
743         if (range->min_ip)
744                 range->flags |= IP_NAT_RANGE_MAP_IPS;
745
746         if (!tb[CTA_NAT_PROTO])
747                 return 0;
748
749         err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO], ct, range);
750         if (err < 0)
751                 return err;
752
753         return 0;
754 }
755 #endif
756
757 static inline int
758 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
759 {
760         struct nlattr *tb[CTA_HELP_MAX+1];
761
762         nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
763
764         if (!tb[CTA_HELP_NAME])
765                 return -EINVAL;
766
767         *helper_name = nla_data(tb[CTA_HELP_NAME]);
768
769         return 0;
770 }
771
772 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
773         [CTA_STATUS]            = { .type = NLA_U32 },
774         [CTA_TIMEOUT]           = { .type = NLA_U32 },
775         [CTA_MARK]              = { .type = NLA_U32 },
776         [CTA_USE]               = { .type = NLA_U32 },
777         [CTA_ID]                = { .type = NLA_U32 },
778 };
779
780 static int
781 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
782                         struct nlmsghdr *nlh, struct nlattr *cda[])
783 {
784         struct nf_conntrack_tuple_hash *h;
785         struct nf_conntrack_tuple tuple;
786         struct nf_conn *ct;
787         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
788         u_int8_t u3 = nfmsg->nfgen_family;
789         int err = 0;
790
791         if (cda[CTA_TUPLE_ORIG])
792                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
793         else if (cda[CTA_TUPLE_REPLY])
794                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
795         else {
796                 /* Flush the whole table */
797                 nf_conntrack_flush();
798                 return 0;
799         }
800
801         if (err < 0)
802                 return err;
803
804         h = nf_conntrack_find_get(&tuple);
805         if (!h)
806                 return -ENOENT;
807
808         ct = nf_ct_tuplehash_to_ctrack(h);
809
810         if (cda[CTA_ID]) {
811                 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
812                 if (id != (u32)(unsigned long)ct) {
813                         nf_ct_put(ct);
814                         return -ENOENT;
815                 }
816         }
817
818         nf_ct_kill(ct);
819         nf_ct_put(ct);
820
821         return 0;
822 }
823
824 static int
825 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
826                         struct nlmsghdr *nlh, struct nlattr *cda[])
827 {
828         struct nf_conntrack_tuple_hash *h;
829         struct nf_conntrack_tuple tuple;
830         struct nf_conn *ct;
831         struct sk_buff *skb2 = NULL;
832         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
833         u_int8_t u3 = nfmsg->nfgen_family;
834         int err = 0;
835
836         if (nlh->nlmsg_flags & NLM_F_DUMP)
837                 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
838                                           ctnetlink_done);
839
840         if (cda[CTA_TUPLE_ORIG])
841                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
842         else if (cda[CTA_TUPLE_REPLY])
843                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
844         else
845                 return -EINVAL;
846
847         if (err < 0)
848                 return err;
849
850         h = nf_conntrack_find_get(&tuple);
851         if (!h)
852                 return -ENOENT;
853
854         ct = nf_ct_tuplehash_to_ctrack(h);
855
856         err = -ENOMEM;
857         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
858         if (!skb2) {
859                 nf_ct_put(ct);
860                 return -ENOMEM;
861         }
862
863         err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
864                                   IPCTNL_MSG_CT_NEW, 1, ct);
865         nf_ct_put(ct);
866         if (err <= 0)
867                 goto free;
868
869         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
870         if (err < 0)
871                 goto out;
872
873         return 0;
874
875 free:
876         kfree_skb(skb2);
877 out:
878         return err;
879 }
880
881 static int
882 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
883 {
884         unsigned long d;
885         unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
886         d = ct->status ^ status;
887
888         if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
889                 /* unchangeable */
890                 return -EBUSY;
891
892         if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
893                 /* SEEN_REPLY bit can only be set */
894                 return -EBUSY;
895
896         if (d & IPS_ASSURED && !(status & IPS_ASSURED))
897                 /* ASSURED bit can only be set */
898                 return -EBUSY;
899
900         if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
901 #ifndef CONFIG_NF_NAT_NEEDED
902                 return -EOPNOTSUPP;
903 #else
904                 struct nf_nat_range range;
905
906                 if (cda[CTA_NAT_DST]) {
907                         if (nfnetlink_parse_nat(cda[CTA_NAT_DST], ct,
908                                                 &range) < 0)
909                                 return -EINVAL;
910                         if (nf_nat_initialized(ct, IP_NAT_MANIP_DST))
911                                 return -EEXIST;
912                         nf_nat_setup_info(ct, &range, IP_NAT_MANIP_DST);
913                 }
914                 if (cda[CTA_NAT_SRC]) {
915                         if (nfnetlink_parse_nat(cda[CTA_NAT_SRC], ct,
916                                                 &range) < 0)
917                                 return -EINVAL;
918                         if (nf_nat_initialized(ct, IP_NAT_MANIP_SRC))
919                                 return -EEXIST;
920                         nf_nat_setup_info(ct, &range, IP_NAT_MANIP_SRC);
921                 }
922 #endif
923         }
924
925         /* Be careful here, modifying NAT bits can screw up things,
926          * so don't let users modify them directly if they don't pass
927          * nf_nat_range. */
928         ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
929         return 0;
930 }
931
932
933 static inline int
934 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
935 {
936         struct nf_conntrack_helper *helper;
937         struct nf_conn_help *help = nfct_help(ct);
938         char *helpname;
939         int err;
940
941         /* don't change helper of sibling connections */
942         if (ct->master)
943                 return -EBUSY;
944
945         err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
946         if (err < 0)
947                 return err;
948
949         if (!strcmp(helpname, "")) {
950                 if (help && help->helper) {
951                         /* we had a helper before ... */
952                         nf_ct_remove_expectations(ct);
953                         rcu_assign_pointer(help->helper, NULL);
954                 }
955
956                 return 0;
957         }
958
959         helper = __nf_conntrack_helper_find_byname(helpname);
960         if (helper == NULL)
961                 return -EOPNOTSUPP;
962
963         if (help) {
964                 if (help->helper == helper)
965                         return 0;
966                 if (help->helper)
967                         return -EBUSY;
968                 /* need to zero data of old helper */
969                 memset(&help->help, 0, sizeof(help->help));
970         } else {
971                 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
972                 if (help == NULL)
973                         return -ENOMEM;
974         }
975
976         rcu_assign_pointer(help->helper, helper);
977
978         return 0;
979 }
980
981 static inline int
982 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
983 {
984         u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
985
986         if (!del_timer(&ct->timeout))
987                 return -ETIME;
988
989         ct->timeout.expires = jiffies + timeout * HZ;
990         add_timer(&ct->timeout);
991
992         return 0;
993 }
994
995 static inline int
996 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
997 {
998         struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
999         struct nf_conntrack_l4proto *l4proto;
1000         int err = 0;
1001
1002         nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
1003
1004         l4proto = nf_ct_l4proto_find_get(nf_ct_l3num(ct), nf_ct_protonum(ct));
1005         if (l4proto->from_nlattr)
1006                 err = l4proto->from_nlattr(tb, ct);
1007         nf_ct_l4proto_put(l4proto);
1008
1009         return err;
1010 }
1011
1012 #ifdef CONFIG_NF_NAT_NEEDED
1013 static inline int
1014 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1015 {
1016         struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1017
1018         nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1019
1020         if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1021                 return -EINVAL;
1022
1023         natseq->correction_pos =
1024                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1025
1026         if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1027                 return -EINVAL;
1028
1029         natseq->offset_before =
1030                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1031
1032         if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1033                 return -EINVAL;
1034
1035         natseq->offset_after =
1036                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1037
1038         return 0;
1039 }
1040
1041 static int
1042 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1043 {
1044         int ret = 0;
1045         struct nf_conn_nat *nat = nfct_nat(ct);
1046
1047         if (!nat)
1048                 return 0;
1049
1050         if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1051                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1052                                          cda[CTA_NAT_SEQ_ADJ_ORIG]);
1053                 if (ret < 0)
1054                         return ret;
1055
1056                 ct->status |= IPS_SEQ_ADJUST;
1057         }
1058
1059         if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1060                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1061                                          cda[CTA_NAT_SEQ_ADJ_REPLY]);
1062                 if (ret < 0)
1063                         return ret;
1064
1065                 ct->status |= IPS_SEQ_ADJUST;
1066         }
1067
1068         return 0;
1069 }
1070 #endif
1071
1072 static int
1073 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1074 {
1075         int err;
1076
1077         if (cda[CTA_HELP]) {
1078                 err = ctnetlink_change_helper(ct, cda);
1079                 if (err < 0)
1080                         return err;
1081         }
1082
1083         if (cda[CTA_TIMEOUT]) {
1084                 err = ctnetlink_change_timeout(ct, cda);
1085                 if (err < 0)
1086                         return err;
1087         }
1088
1089         if (cda[CTA_STATUS]) {
1090                 err = ctnetlink_change_status(ct, cda);
1091                 if (err < 0)
1092                         return err;
1093         }
1094
1095         if (cda[CTA_PROTOINFO]) {
1096                 err = ctnetlink_change_protoinfo(ct, cda);
1097                 if (err < 0)
1098                         return err;
1099         }
1100
1101 #if defined(CONFIG_NF_CONNTRACK_MARK)
1102         if (cda[CTA_MARK])
1103                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1104 #endif
1105
1106 #ifdef CONFIG_NF_NAT_NEEDED
1107         if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1108                 err = ctnetlink_change_nat_seq_adj(ct, cda);
1109                 if (err < 0)
1110                         return err;
1111         }
1112 #endif
1113
1114         return 0;
1115 }
1116
1117 static int
1118 ctnetlink_create_conntrack(struct nlattr *cda[],
1119                            struct nf_conntrack_tuple *otuple,
1120                            struct nf_conntrack_tuple *rtuple,
1121                            struct nf_conn *master_ct)
1122 {
1123         struct nf_conn *ct;
1124         int err = -EINVAL;
1125         struct nf_conn_help *help;
1126         struct nf_conntrack_helper *helper;
1127
1128         ct = nf_conntrack_alloc(otuple, rtuple, GFP_KERNEL);
1129         if (ct == NULL || IS_ERR(ct))
1130                 return -ENOMEM;
1131
1132         if (!cda[CTA_TIMEOUT])
1133                 goto err;
1134         ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1135
1136         ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1137         ct->status |= IPS_CONFIRMED;
1138
1139         if (cda[CTA_STATUS]) {
1140                 err = ctnetlink_change_status(ct, cda);
1141                 if (err < 0)
1142                         goto err;
1143         }
1144
1145         if (cda[CTA_PROTOINFO]) {
1146                 err = ctnetlink_change_protoinfo(ct, cda);
1147                 if (err < 0)
1148                         goto err;
1149         }
1150
1151         nf_ct_acct_ext_add(ct, GFP_KERNEL);
1152
1153 #if defined(CONFIG_NF_CONNTRACK_MARK)
1154         if (cda[CTA_MARK])
1155                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1156 #endif
1157
1158         rcu_read_lock();
1159         helper = __nf_ct_helper_find(rtuple);
1160         if (helper) {
1161                 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
1162                 if (help == NULL) {
1163                         rcu_read_unlock();
1164                         err = -ENOMEM;
1165                         goto err;
1166                 }
1167                 /* not in hash table yet so not strictly necessary */
1168                 rcu_assign_pointer(help->helper, helper);
1169         }
1170
1171         /* setup master conntrack: this is a confirmed expectation */
1172         if (master_ct) {
1173                 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1174                 ct->master = master_ct;
1175         }
1176
1177         add_timer(&ct->timeout);
1178         nf_conntrack_hash_insert(ct);
1179         rcu_read_unlock();
1180
1181         return 0;
1182
1183 err:
1184         nf_conntrack_free(ct);
1185         return err;
1186 }
1187
1188 static int
1189 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1190                         struct nlmsghdr *nlh, struct nlattr *cda[])
1191 {
1192         struct nf_conntrack_tuple otuple, rtuple;
1193         struct nf_conntrack_tuple_hash *h = NULL;
1194         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1195         u_int8_t u3 = nfmsg->nfgen_family;
1196         int err = 0;
1197
1198         if (cda[CTA_TUPLE_ORIG]) {
1199                 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1200                 if (err < 0)
1201                         return err;
1202         }
1203
1204         if (cda[CTA_TUPLE_REPLY]) {
1205                 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1206                 if (err < 0)
1207                         return err;
1208         }
1209
1210         spin_lock_bh(&nf_conntrack_lock);
1211         if (cda[CTA_TUPLE_ORIG])
1212                 h = __nf_conntrack_find(&otuple);
1213         else if (cda[CTA_TUPLE_REPLY])
1214                 h = __nf_conntrack_find(&rtuple);
1215
1216         if (h == NULL) {
1217                 struct nf_conntrack_tuple master;
1218                 struct nf_conntrack_tuple_hash *master_h = NULL;
1219                 struct nf_conn *master_ct = NULL;
1220
1221                 if (cda[CTA_TUPLE_MASTER]) {
1222                         err = ctnetlink_parse_tuple(cda,
1223                                                     &master,
1224                                                     CTA_TUPLE_MASTER,
1225                                                     u3);
1226                         if (err < 0)
1227                                 goto out_unlock;
1228
1229                         master_h = __nf_conntrack_find(&master);
1230                         if (master_h == NULL) {
1231                                 err = -ENOENT;
1232                                 goto out_unlock;
1233                         }
1234                         master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1235                         atomic_inc(&master_ct->ct_general.use);
1236                 }
1237
1238                 spin_unlock_bh(&nf_conntrack_lock);
1239                 err = -ENOENT;
1240                 if (nlh->nlmsg_flags & NLM_F_CREATE)
1241                         err = ctnetlink_create_conntrack(cda,
1242                                                          &otuple,
1243                                                          &rtuple,
1244                                                          master_ct);
1245                 if (err < 0 && master_ct)
1246                         nf_ct_put(master_ct);
1247
1248                 return err;
1249         }
1250         /* implicit 'else' */
1251
1252         /* We manipulate the conntrack inside the global conntrack table lock,
1253          * so there's no need to increase the refcount */
1254         err = -EEXIST;
1255         if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1256                 /* we only allow nat config for new conntracks */
1257                 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1258                         err = -EOPNOTSUPP;
1259                         goto out_unlock;
1260                 }
1261                 /* can't link an existing conntrack to a master */
1262                 if (cda[CTA_TUPLE_MASTER]) {
1263                         err = -EOPNOTSUPP;
1264                         goto out_unlock;
1265                 }
1266                 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h),
1267                                                  cda);
1268         }
1269
1270 out_unlock:
1271         spin_unlock_bh(&nf_conntrack_lock);
1272         return err;
1273 }
1274
1275 /***********************************************************************
1276  * EXPECT
1277  ***********************************************************************/
1278
1279 static inline int
1280 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1281                          const struct nf_conntrack_tuple *tuple,
1282                          enum ctattr_expect type)
1283 {
1284         struct nlattr *nest_parms;
1285
1286         nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1287         if (!nest_parms)
1288                 goto nla_put_failure;
1289         if (ctnetlink_dump_tuples(skb, tuple) < 0)
1290                 goto nla_put_failure;
1291         nla_nest_end(skb, nest_parms);
1292
1293         return 0;
1294
1295 nla_put_failure:
1296         return -1;
1297 }
1298
1299 static inline int
1300 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1301                         const struct nf_conntrack_tuple *tuple,
1302                         const struct nf_conntrack_tuple_mask *mask)
1303 {
1304         int ret;
1305         struct nf_conntrack_l3proto *l3proto;
1306         struct nf_conntrack_l4proto *l4proto;
1307         struct nf_conntrack_tuple m;
1308         struct nlattr *nest_parms;
1309
1310         memset(&m, 0xFF, sizeof(m));
1311         m.src.u.all = mask->src.u.all;
1312         memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1313
1314         nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1315         if (!nest_parms)
1316                 goto nla_put_failure;
1317
1318         l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1319         ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1320         nf_ct_l3proto_put(l3proto);
1321
1322         if (unlikely(ret < 0))
1323                 goto nla_put_failure;
1324
1325         l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1326         ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1327         nf_ct_l4proto_put(l4proto);
1328         if (unlikely(ret < 0))
1329                 goto nla_put_failure;
1330
1331         nla_nest_end(skb, nest_parms);
1332
1333         return 0;
1334
1335 nla_put_failure:
1336         return -1;
1337 }
1338
1339 static int
1340 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1341                           const struct nf_conntrack_expect *exp)
1342 {
1343         struct nf_conn *master = exp->master;
1344         long timeout = (exp->timeout.expires - jiffies) / HZ;
1345
1346         if (timeout < 0)
1347                 timeout = 0;
1348
1349         if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1350                 goto nla_put_failure;
1351         if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1352                 goto nla_put_failure;
1353         if (ctnetlink_exp_dump_tuple(skb,
1354                                  &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1355                                  CTA_EXPECT_MASTER) < 0)
1356                 goto nla_put_failure;
1357
1358         NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1359         NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1360
1361         return 0;
1362
1363 nla_put_failure:
1364         return -1;
1365 }
1366
1367 static int
1368 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1369                     int event,
1370                     int nowait,
1371                     const struct nf_conntrack_expect *exp)
1372 {
1373         struct nlmsghdr *nlh;
1374         struct nfgenmsg *nfmsg;
1375         unsigned char *b = skb_tail_pointer(skb);
1376
1377         event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1378         nlh    = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1379         nfmsg  = NLMSG_DATA(nlh);
1380
1381         nlh->nlmsg_flags    = (nowait && pid) ? NLM_F_MULTI : 0;
1382         nfmsg->nfgen_family = exp->tuple.src.l3num;
1383         nfmsg->version      = NFNETLINK_V0;
1384         nfmsg->res_id       = 0;
1385
1386         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1387                 goto nla_put_failure;
1388
1389         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1390         return skb->len;
1391
1392 nlmsg_failure:
1393 nla_put_failure:
1394         nlmsg_trim(skb, b);
1395         return -1;
1396 }
1397
1398 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1399 static int ctnetlink_expect_event(struct notifier_block *this,
1400                                   unsigned long events, void *ptr)
1401 {
1402         struct nlmsghdr *nlh;
1403         struct nfgenmsg *nfmsg;
1404         struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1405         struct sk_buff *skb;
1406         unsigned int type;
1407         sk_buff_data_t b;
1408         int flags = 0;
1409
1410         if (events & IPEXP_NEW) {
1411                 type = IPCTNL_MSG_EXP_NEW;
1412                 flags = NLM_F_CREATE|NLM_F_EXCL;
1413         } else
1414                 return NOTIFY_DONE;
1415
1416         if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1417                 return NOTIFY_DONE;
1418
1419         skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1420         if (!skb)
1421                 return NOTIFY_DONE;
1422
1423         b = skb->tail;
1424
1425         type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1426         nlh   = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1427         nfmsg = NLMSG_DATA(nlh);
1428
1429         nlh->nlmsg_flags    = flags;
1430         nfmsg->nfgen_family = exp->tuple.src.l3num;
1431         nfmsg->version      = NFNETLINK_V0;
1432         nfmsg->res_id       = 0;
1433
1434         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1435                 goto nla_put_failure;
1436
1437         nlh->nlmsg_len = skb->tail - b;
1438         nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1439         return NOTIFY_DONE;
1440
1441 nlmsg_failure:
1442 nla_put_failure:
1443         kfree_skb(skb);
1444         return NOTIFY_DONE;
1445 }
1446 #endif
1447 static int ctnetlink_exp_done(struct netlink_callback *cb)
1448 {
1449         if (cb->args[1])
1450                 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1451         return 0;
1452 }
1453
1454 static int
1455 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1456 {
1457         struct nf_conntrack_expect *exp, *last;
1458         struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1459         struct hlist_node *n;
1460         u_int8_t l3proto = nfmsg->nfgen_family;
1461
1462         rcu_read_lock();
1463         last = (struct nf_conntrack_expect *)cb->args[1];
1464         for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1465 restart:
1466                 hlist_for_each_entry(exp, n, &nf_ct_expect_hash[cb->args[0]],
1467                                      hnode) {
1468                         if (l3proto && exp->tuple.src.l3num != l3proto)
1469                                 continue;
1470                         if (cb->args[1]) {
1471                                 if (exp != last)
1472                                         continue;
1473                                 cb->args[1] = 0;
1474                         }
1475                         if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1476                                                     cb->nlh->nlmsg_seq,
1477                                                     IPCTNL_MSG_EXP_NEW,
1478                                                     1, exp) < 0) {
1479                                 if (!atomic_inc_not_zero(&exp->use))
1480                                         continue;
1481                                 cb->args[1] = (unsigned long)exp;
1482                                 goto out;
1483                         }
1484                 }
1485                 if (cb->args[1]) {
1486                         cb->args[1] = 0;
1487                         goto restart;
1488                 }
1489         }
1490 out:
1491         rcu_read_unlock();
1492         if (last)
1493                 nf_ct_expect_put(last);
1494
1495         return skb->len;
1496 }
1497
1498 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1499         [CTA_EXPECT_TIMEOUT]    = { .type = NLA_U32 },
1500         [CTA_EXPECT_ID]         = { .type = NLA_U32 },
1501 };
1502
1503 static int
1504 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1505                      struct nlmsghdr *nlh, struct nlattr *cda[])
1506 {
1507         struct nf_conntrack_tuple tuple;
1508         struct nf_conntrack_expect *exp;
1509         struct sk_buff *skb2;
1510         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1511         u_int8_t u3 = nfmsg->nfgen_family;
1512         int err = 0;
1513
1514         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1515                 return netlink_dump_start(ctnl, skb, nlh,
1516                                           ctnetlink_exp_dump_table,
1517                                           ctnetlink_exp_done);
1518         }
1519
1520         if (cda[CTA_EXPECT_MASTER])
1521                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1522         else
1523                 return -EINVAL;
1524
1525         if (err < 0)
1526                 return err;
1527
1528         exp = nf_ct_expect_find_get(&tuple);
1529         if (!exp)
1530                 return -ENOENT;
1531
1532         if (cda[CTA_EXPECT_ID]) {
1533                 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1534                 if (ntohl(id) != (u32)(unsigned long)exp) {
1535                         nf_ct_expect_put(exp);
1536                         return -ENOENT;
1537                 }
1538         }
1539
1540         err = -ENOMEM;
1541         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1542         if (!skb2)
1543                 goto out;
1544
1545         err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1546                                       nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1547                                       1, exp);
1548         if (err <= 0)
1549                 goto free;
1550
1551         nf_ct_expect_put(exp);
1552
1553         return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1554
1555 free:
1556         kfree_skb(skb2);
1557 out:
1558         nf_ct_expect_put(exp);
1559         return err;
1560 }
1561
1562 static int
1563 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1564                      struct nlmsghdr *nlh, struct nlattr *cda[])
1565 {
1566         struct nf_conntrack_expect *exp;
1567         struct nf_conntrack_tuple tuple;
1568         struct nf_conntrack_helper *h;
1569         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1570         struct hlist_node *n, *next;
1571         u_int8_t u3 = nfmsg->nfgen_family;
1572         unsigned int i;
1573         int err;
1574
1575         if (cda[CTA_EXPECT_TUPLE]) {
1576                 /* delete a single expect by tuple */
1577                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1578                 if (err < 0)
1579                         return err;
1580
1581                 /* bump usage count to 2 */
1582                 exp = nf_ct_expect_find_get(&tuple);
1583                 if (!exp)
1584                         return -ENOENT;
1585
1586                 if (cda[CTA_EXPECT_ID]) {
1587                         __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1588                         if (ntohl(id) != (u32)(unsigned long)exp) {
1589                                 nf_ct_expect_put(exp);
1590                                 return -ENOENT;
1591                         }
1592                 }
1593
1594                 /* after list removal, usage count == 1 */
1595                 nf_ct_unexpect_related(exp);
1596                 /* have to put what we 'get' above.
1597                  * after this line usage count == 0 */
1598                 nf_ct_expect_put(exp);
1599         } else if (cda[CTA_EXPECT_HELP_NAME]) {
1600                 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1601                 struct nf_conn_help *m_help;
1602
1603                 /* delete all expectations for this helper */
1604                 spin_lock_bh(&nf_conntrack_lock);
1605                 h = __nf_conntrack_helper_find_byname(name);
1606                 if (!h) {
1607                         spin_unlock_bh(&nf_conntrack_lock);
1608                         return -EOPNOTSUPP;
1609                 }
1610                 for (i = 0; i < nf_ct_expect_hsize; i++) {
1611                         hlist_for_each_entry_safe(exp, n, next,
1612                                                   &nf_ct_expect_hash[i],
1613                                                   hnode) {
1614                                 m_help = nfct_help(exp->master);
1615                                 if (m_help->helper == h
1616                                     && del_timer(&exp->timeout)) {
1617                                         nf_ct_unlink_expect(exp);
1618                                         nf_ct_expect_put(exp);
1619                                 }
1620                         }
1621                 }
1622                 spin_unlock_bh(&nf_conntrack_lock);
1623         } else {
1624                 /* This basically means we have to flush everything*/
1625                 spin_lock_bh(&nf_conntrack_lock);
1626                 for (i = 0; i < nf_ct_expect_hsize; i++) {
1627                         hlist_for_each_entry_safe(exp, n, next,
1628                                                   &nf_ct_expect_hash[i],
1629                                                   hnode) {
1630                                 if (del_timer(&exp->timeout)) {
1631                                         nf_ct_unlink_expect(exp);
1632                                         nf_ct_expect_put(exp);
1633                                 }
1634                         }
1635                 }
1636                 spin_unlock_bh(&nf_conntrack_lock);
1637         }
1638
1639         return 0;
1640 }
1641 static int
1642 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1643 {
1644         return -EOPNOTSUPP;
1645 }
1646
1647 static int
1648 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3)
1649 {
1650         struct nf_conntrack_tuple tuple, mask, master_tuple;
1651         struct nf_conntrack_tuple_hash *h = NULL;
1652         struct nf_conntrack_expect *exp;
1653         struct nf_conn *ct;
1654         struct nf_conn_help *help;
1655         int err = 0;
1656
1657         /* caller guarantees that those three CTA_EXPECT_* exist */
1658         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1659         if (err < 0)
1660                 return err;
1661         err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1662         if (err < 0)
1663                 return err;
1664         err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1665         if (err < 0)
1666                 return err;
1667
1668         /* Look for master conntrack of this expectation */
1669         h = nf_conntrack_find_get(&master_tuple);
1670         if (!h)
1671                 return -ENOENT;
1672         ct = nf_ct_tuplehash_to_ctrack(h);
1673         help = nfct_help(ct);
1674
1675         if (!help || !help->helper) {
1676                 /* such conntrack hasn't got any helper, abort */
1677                 err = -EINVAL;
1678                 goto out;
1679         }
1680
1681         exp = nf_ct_expect_alloc(ct);
1682         if (!exp) {
1683                 err = -ENOMEM;
1684                 goto out;
1685         }
1686
1687         exp->expectfn = NULL;
1688         exp->flags = 0;
1689         exp->master = ct;
1690         exp->helper = NULL;
1691         memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1692         memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1693         exp->mask.src.u.all = mask.src.u.all;
1694
1695         err = nf_ct_expect_related(exp);
1696         nf_ct_expect_put(exp);
1697
1698 out:
1699         nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1700         return err;
1701 }
1702
1703 static int
1704 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1705                      struct nlmsghdr *nlh, struct nlattr *cda[])
1706 {
1707         struct nf_conntrack_tuple tuple;
1708         struct nf_conntrack_expect *exp;
1709         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1710         u_int8_t u3 = nfmsg->nfgen_family;
1711         int err = 0;
1712
1713         if (!cda[CTA_EXPECT_TUPLE]
1714             || !cda[CTA_EXPECT_MASK]
1715             || !cda[CTA_EXPECT_MASTER])
1716                 return -EINVAL;
1717
1718         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1719         if (err < 0)
1720                 return err;
1721
1722         spin_lock_bh(&nf_conntrack_lock);
1723         exp = __nf_ct_expect_find(&tuple);
1724
1725         if (!exp) {
1726                 spin_unlock_bh(&nf_conntrack_lock);
1727                 err = -ENOENT;
1728                 if (nlh->nlmsg_flags & NLM_F_CREATE)
1729                         err = ctnetlink_create_expect(cda, u3);
1730                 return err;
1731         }
1732
1733         err = -EEXIST;
1734         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1735                 err = ctnetlink_change_expect(exp, cda);
1736         spin_unlock_bh(&nf_conntrack_lock);
1737
1738         return err;
1739 }
1740
1741 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1742 static struct notifier_block ctnl_notifier = {
1743         .notifier_call  = ctnetlink_conntrack_event,
1744 };
1745
1746 static struct notifier_block ctnl_notifier_exp = {
1747         .notifier_call  = ctnetlink_expect_event,
1748 };
1749 #endif
1750
1751 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1752         [IPCTNL_MSG_CT_NEW]             = { .call = ctnetlink_new_conntrack,
1753                                             .attr_count = CTA_MAX,
1754                                             .policy = ct_nla_policy },
1755         [IPCTNL_MSG_CT_GET]             = { .call = ctnetlink_get_conntrack,
1756                                             .attr_count = CTA_MAX,
1757                                             .policy = ct_nla_policy },
1758         [IPCTNL_MSG_CT_DELETE]          = { .call = ctnetlink_del_conntrack,
1759                                             .attr_count = CTA_MAX,
1760                                             .policy = ct_nla_policy },
1761         [IPCTNL_MSG_CT_GET_CTRZERO]     = { .call = ctnetlink_get_conntrack,
1762                                             .attr_count = CTA_MAX,
1763                                             .policy = ct_nla_policy },
1764 };
1765
1766 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1767         [IPCTNL_MSG_EXP_GET]            = { .call = ctnetlink_get_expect,
1768                                             .attr_count = CTA_EXPECT_MAX,
1769                                             .policy = exp_nla_policy },
1770         [IPCTNL_MSG_EXP_NEW]            = { .call = ctnetlink_new_expect,
1771                                             .attr_count = CTA_EXPECT_MAX,
1772                                             .policy = exp_nla_policy },
1773         [IPCTNL_MSG_EXP_DELETE]         = { .call = ctnetlink_del_expect,
1774                                             .attr_count = CTA_EXPECT_MAX,
1775                                             .policy = exp_nla_policy },
1776 };
1777
1778 static const struct nfnetlink_subsystem ctnl_subsys = {
1779         .name                           = "conntrack",
1780         .subsys_id                      = NFNL_SUBSYS_CTNETLINK,
1781         .cb_count                       = IPCTNL_MSG_MAX,
1782         .cb                             = ctnl_cb,
1783 };
1784
1785 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1786         .name                           = "conntrack_expect",
1787         .subsys_id                      = NFNL_SUBSYS_CTNETLINK_EXP,
1788         .cb_count                       = IPCTNL_MSG_EXP_MAX,
1789         .cb                             = ctnl_exp_cb,
1790 };
1791
1792 MODULE_ALIAS("ip_conntrack_netlink");
1793 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1794 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1795
1796 static int __init ctnetlink_init(void)
1797 {
1798         int ret;
1799
1800         printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1801         ret = nfnetlink_subsys_register(&ctnl_subsys);
1802         if (ret < 0) {
1803                 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1804                 goto err_out;
1805         }
1806
1807         ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1808         if (ret < 0) {
1809                 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1810                 goto err_unreg_subsys;
1811         }
1812
1813 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1814         ret = nf_conntrack_register_notifier(&ctnl_notifier);
1815         if (ret < 0) {
1816                 printk("ctnetlink_init: cannot register notifier.\n");
1817                 goto err_unreg_exp_subsys;
1818         }
1819
1820         ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1821         if (ret < 0) {
1822                 printk("ctnetlink_init: cannot expect register notifier.\n");
1823                 goto err_unreg_notifier;
1824         }
1825 #endif
1826
1827         return 0;
1828
1829 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1830 err_unreg_notifier:
1831         nf_conntrack_unregister_notifier(&ctnl_notifier);
1832 err_unreg_exp_subsys:
1833         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1834 #endif
1835 err_unreg_subsys:
1836         nfnetlink_subsys_unregister(&ctnl_subsys);
1837 err_out:
1838         return ret;
1839 }
1840
1841 static void __exit ctnetlink_exit(void)
1842 {
1843         printk("ctnetlink: unregistering from nfnetlink.\n");
1844
1845 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1846         nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1847         nf_conntrack_unregister_notifier(&ctnl_notifier);
1848 #endif
1849
1850         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1851         nfnetlink_subsys_unregister(&ctnl_subsys);
1852         return;
1853 }
1854
1855 module_init(ctnetlink_init);
1856 module_exit(ctnetlink_exit);