]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c
[PATCH] remove many unneeded #includes of sched.h
[linux-2.6.git] / net / ipv6 / netfilter / nf_conntrack_proto_icmpv6.c
1 /*
2  * Copyright (C)2003,2004 USAGI/WIDE Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Author:
9  *      Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
10  *
11  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
12  *      - ICMPv6 tracking support. Derived from the original ip_conntrack code
13  *        net/ipv4/netfilter/ip_conntrack_proto_icmp.c which had the following
14  *        copyright information:
15  *              (C) 1999-2001 Paul `Rusty' Russell
16  *              (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
17  */
18
19 #include <linux/types.h>
20 #include <linux/timer.h>
21 #include <linux/module.h>
22 #include <linux/netfilter.h>
23 #include <linux/in6.h>
24 #include <linux/icmpv6.h>
25 #include <linux/ipv6.h>
26 #include <net/ipv6.h>
27 #include <net/ip6_checksum.h>
28 #include <linux/seq_file.h>
29 #include <linux/netfilter_ipv6.h>
30 #include <net/netfilter/nf_conntrack_tuple.h>
31 #include <net/netfilter/nf_conntrack_l4proto.h>
32 #include <net/netfilter/nf_conntrack_core.h>
33 #include <net/netfilter/ipv6/nf_conntrack_icmpv6.h>
34
35 static unsigned long nf_ct_icmpv6_timeout __read_mostly = 30*HZ;
36
37 #if 0
38 #define DEBUGP printk
39 #else
40 #define DEBUGP(format, args...)
41 #endif
42
43 static int icmpv6_pkt_to_tuple(const struct sk_buff *skb,
44                                unsigned int dataoff,
45                                struct nf_conntrack_tuple *tuple)
46 {
47         struct icmp6hdr _hdr, *hp;
48
49         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
50         if (hp == NULL)
51                 return 0;
52         tuple->dst.u.icmp.type = hp->icmp6_type;
53         tuple->src.u.icmp.id = hp->icmp6_identifier;
54         tuple->dst.u.icmp.code = hp->icmp6_code;
55
56         return 1;
57 }
58
59 /* Add 1; spaces filled with 0. */
60 static u_int8_t invmap[] = {
61         [ICMPV6_ECHO_REQUEST - 128]     = ICMPV6_ECHO_REPLY + 1,
62         [ICMPV6_ECHO_REPLY - 128]       = ICMPV6_ECHO_REQUEST + 1,
63         [ICMPV6_NI_QUERY - 128]         = ICMPV6_NI_QUERY + 1,
64         [ICMPV6_NI_REPLY - 128]         = ICMPV6_NI_REPLY +1
65 };
66
67 static int icmpv6_invert_tuple(struct nf_conntrack_tuple *tuple,
68                                const struct nf_conntrack_tuple *orig)
69 {
70         int type = orig->dst.u.icmp.type - 128;
71         if (type < 0 || type >= sizeof(invmap) || !invmap[type])
72                 return 0;
73
74         tuple->src.u.icmp.id   = orig->src.u.icmp.id;
75         tuple->dst.u.icmp.type = invmap[type] - 1;
76         tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
77         return 1;
78 }
79
80 /* Print out the per-protocol part of the tuple. */
81 static int icmpv6_print_tuple(struct seq_file *s,
82                               const struct nf_conntrack_tuple *tuple)
83 {
84         return seq_printf(s, "type=%u code=%u id=%u ",
85                           tuple->dst.u.icmp.type,
86                           tuple->dst.u.icmp.code,
87                           ntohs(tuple->src.u.icmp.id));
88 }
89
90 /* Print out the private part of the conntrack. */
91 static int icmpv6_print_conntrack(struct seq_file *s,
92                                   const struct nf_conn *conntrack)
93 {
94         return 0;
95 }
96
97 /* Returns verdict for packet, or -1 for invalid. */
98 static int icmpv6_packet(struct nf_conn *ct,
99                        const struct sk_buff *skb,
100                        unsigned int dataoff,
101                        enum ip_conntrack_info ctinfo,
102                        int pf,
103                        unsigned int hooknum)
104 {
105         /* Try to delete connection immediately after all replies:
106            won't actually vanish as we still have skb, and del_timer
107            means this will only run once even if count hits zero twice
108            (theoretically possible with SMP) */
109         if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
110                 if (atomic_dec_and_test(&ct->proto.icmp.count)
111                     && del_timer(&ct->timeout))
112                         ct->timeout.function((unsigned long)ct);
113         } else {
114                 atomic_inc(&ct->proto.icmp.count);
115                 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
116                 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmpv6_timeout);
117         }
118
119         return NF_ACCEPT;
120 }
121
122 /* Called when a new connection for this protocol found. */
123 static int icmpv6_new(struct nf_conn *conntrack,
124                       const struct sk_buff *skb,
125                       unsigned int dataoff)
126 {
127         static u_int8_t valid_new[] = {
128                 [ICMPV6_ECHO_REQUEST - 128] = 1,
129                 [ICMPV6_NI_QUERY - 128] = 1
130         };
131         int type = conntrack->tuplehash[0].tuple.dst.u.icmp.type - 128;
132
133         if (type < 0 || type >= sizeof(valid_new) || !valid_new[type]) {
134                 /* Can't create a new ICMPv6 `conn' with this. */
135                 DEBUGP("icmpv6: can't create new conn with type %u\n",
136                        type + 128);
137                 NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
138                 return 0;
139         }
140         atomic_set(&conntrack->proto.icmp.count, 0);
141         return 1;
142 }
143
144 static int
145 icmpv6_error_message(struct sk_buff *skb,
146                      unsigned int icmp6off,
147                      enum ip_conntrack_info *ctinfo,
148                      unsigned int hooknum)
149 {
150         struct nf_conntrack_tuple intuple, origtuple;
151         struct nf_conntrack_tuple_hash *h;
152         struct icmp6hdr _hdr, *hp;
153         unsigned int inip6off;
154         struct nf_conntrack_l4proto *inproto;
155         u_int8_t inprotonum;
156         unsigned int inprotoff;
157
158         NF_CT_ASSERT(skb->nfct == NULL);
159
160         hp = skb_header_pointer(skb, icmp6off, sizeof(_hdr), &_hdr);
161         if (hp == NULL) {
162                 DEBUGP("icmpv6_error: Can't get ICMPv6 hdr.\n");
163                 return -NF_ACCEPT;
164         }
165
166         inip6off = icmp6off + sizeof(_hdr);
167         if (skb_copy_bits(skb, inip6off+offsetof(struct ipv6hdr, nexthdr),
168                           &inprotonum, sizeof(inprotonum)) != 0) {
169                 DEBUGP("icmpv6_error: Can't get nexthdr in inner IPv6 header.\n");
170                 return -NF_ACCEPT;
171         }
172         inprotoff = nf_ct_ipv6_skip_exthdr(skb,
173                                            inip6off + sizeof(struct ipv6hdr),
174                                            &inprotonum,
175                                            skb->len - inip6off
176                                                     - sizeof(struct ipv6hdr));
177
178         if ((inprotoff < 0) || (inprotoff > skb->len) ||
179             (inprotonum == NEXTHDR_FRAGMENT)) {
180                 DEBUGP("icmpv6_error: Can't get protocol header in ICMPv6 payload.\n");
181                 return -NF_ACCEPT;
182         }
183
184         /* rcu_read_lock()ed by nf_hook_slow */
185         inproto = __nf_ct_l4proto_find(PF_INET6, inprotonum);
186
187         /* Are they talking about one of our connections? */
188         if (!nf_ct_get_tuple(skb, inip6off, inprotoff, PF_INET6, inprotonum,
189                              &origtuple, &nf_conntrack_l3proto_ipv6, inproto)) {
190                 DEBUGP("icmpv6_error: Can't get tuple\n");
191                 return -NF_ACCEPT;
192         }
193
194         /* Ordinarily, we'd expect the inverted tupleproto, but it's
195            been preserved inside the ICMP. */
196         if (!nf_ct_invert_tuple(&intuple, &origtuple,
197                                 &nf_conntrack_l3proto_ipv6, inproto)) {
198                 DEBUGP("icmpv6_error: Can't invert tuple\n");
199                 return -NF_ACCEPT;
200         }
201
202         *ctinfo = IP_CT_RELATED;
203
204         h = nf_conntrack_find_get(&intuple, NULL);
205         if (!h) {
206                 DEBUGP("icmpv6_error: no match\n");
207                 return -NF_ACCEPT;
208         } else {
209                 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
210                         *ctinfo += IP_CT_IS_REPLY;
211         }
212
213         /* Update skb to refer to this connection */
214         skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
215         skb->nfctinfo = *ctinfo;
216         return -NF_ACCEPT;
217 }
218
219 static int
220 icmpv6_error(struct sk_buff *skb, unsigned int dataoff,
221              enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
222 {
223         struct icmp6hdr _ih, *icmp6h;
224
225         icmp6h = skb_header_pointer(skb, dataoff, sizeof(_ih), &_ih);
226         if (icmp6h == NULL) {
227                 if (LOG_INVALID(IPPROTO_ICMPV6))
228                 nf_log_packet(PF_INET6, 0, skb, NULL, NULL, NULL,
229                               "nf_ct_icmpv6: short packet ");
230                 return -NF_ACCEPT;
231         }
232
233         if (nf_conntrack_checksum && hooknum == NF_IP6_PRE_ROUTING &&
234             nf_ip6_checksum(skb, hooknum, dataoff, IPPROTO_ICMPV6)) {
235                 nf_log_packet(PF_INET6, 0, skb, NULL, NULL, NULL,
236                               "nf_ct_icmpv6: ICMPv6 checksum failed\n");
237                 return -NF_ACCEPT;
238         }
239
240         /* is not error message ? */
241         if (icmp6h->icmp6_type >= 128)
242                 return NF_ACCEPT;
243
244         return icmpv6_error_message(skb, dataoff, ctinfo, hooknum);
245 }
246
247 #if defined(CONFIG_NF_CT_NETLINK) || \
248     defined(CONFIG_NF_CT_NETLINK_MODULE)
249
250 #include <linux/netfilter/nfnetlink.h>
251 #include <linux/netfilter/nfnetlink_conntrack.h>
252 static int icmpv6_tuple_to_nfattr(struct sk_buff *skb,
253                                   const struct nf_conntrack_tuple *t)
254 {
255         NFA_PUT(skb, CTA_PROTO_ICMPV6_ID, sizeof(u_int16_t),
256                 &t->src.u.icmp.id);
257         NFA_PUT(skb, CTA_PROTO_ICMPV6_TYPE, sizeof(u_int8_t),
258                 &t->dst.u.icmp.type);
259         NFA_PUT(skb, CTA_PROTO_ICMPV6_CODE, sizeof(u_int8_t),
260                 &t->dst.u.icmp.code);
261
262         return 0;
263
264 nfattr_failure:
265         return -1;
266 }
267
268 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
269         [CTA_PROTO_ICMPV6_TYPE-1] = sizeof(u_int8_t),
270         [CTA_PROTO_ICMPV6_CODE-1] = sizeof(u_int8_t),
271         [CTA_PROTO_ICMPV6_ID-1]   = sizeof(u_int16_t)
272 };
273
274 static int icmpv6_nfattr_to_tuple(struct nfattr *tb[],
275                                 struct nf_conntrack_tuple *tuple)
276 {
277         if (!tb[CTA_PROTO_ICMPV6_TYPE-1]
278             || !tb[CTA_PROTO_ICMPV6_CODE-1]
279             || !tb[CTA_PROTO_ICMPV6_ID-1])
280                 return -EINVAL;
281
282         if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
283                 return -EINVAL;
284
285         tuple->dst.u.icmp.type =
286                         *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMPV6_TYPE-1]);
287         tuple->dst.u.icmp.code =
288                         *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMPV6_CODE-1]);
289         tuple->src.u.icmp.id =
290                         *(__be16 *)NFA_DATA(tb[CTA_PROTO_ICMPV6_ID-1]);
291
292         if (tuple->dst.u.icmp.type < 128
293             || tuple->dst.u.icmp.type - 128 >= sizeof(invmap)
294             || !invmap[tuple->dst.u.icmp.type - 128])
295                 return -EINVAL;
296
297         return 0;
298 }
299 #endif
300
301 #ifdef CONFIG_SYSCTL
302 static struct ctl_table_header *icmpv6_sysctl_header;
303 static struct ctl_table icmpv6_sysctl_table[] = {
304         {
305                 .ctl_name       = NET_NF_CONNTRACK_ICMPV6_TIMEOUT,
306                 .procname       = "nf_conntrack_icmpv6_timeout",
307                 .data           = &nf_ct_icmpv6_timeout,
308                 .maxlen         = sizeof(unsigned int),
309                 .mode           = 0644,
310                 .proc_handler   = &proc_dointvec_jiffies,
311         },
312         {
313                 .ctl_name       = 0
314         }
315 };
316 #endif /* CONFIG_SYSCTL */
317
318 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 =
319 {
320         .l3proto                = PF_INET6,
321         .l4proto                = IPPROTO_ICMPV6,
322         .name                   = "icmpv6",
323         .pkt_to_tuple           = icmpv6_pkt_to_tuple,
324         .invert_tuple           = icmpv6_invert_tuple,
325         .print_tuple            = icmpv6_print_tuple,
326         .print_conntrack        = icmpv6_print_conntrack,
327         .packet                 = icmpv6_packet,
328         .new                    = icmpv6_new,
329         .error                  = icmpv6_error,
330 #if defined(CONFIG_NF_CT_NETLINK) || \
331     defined(CONFIG_NF_CT_NETLINK_MODULE)
332         .tuple_to_nfattr        = icmpv6_tuple_to_nfattr,
333         .nfattr_to_tuple        = icmpv6_nfattr_to_tuple,
334 #endif
335 #ifdef CONFIG_SYSCTL
336         .ctl_table_header       = &icmpv6_sysctl_header,
337         .ctl_table              = icmpv6_sysctl_table,
338 #endif
339 };
340
341 EXPORT_SYMBOL(nf_conntrack_l4proto_icmpv6);