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