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