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