Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
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> | |
9fb9cbb1 YK |
10 | */ |
11 | ||
12 | #include <linux/types.h> | |
9fb9cbb1 YK |
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> | |
605dcad6 | 24 | #include <net/netfilter/nf_conntrack_l4proto.h> |
9fb9cbb1 YK |
25 | #include <net/netfilter/nf_conntrack_core.h> |
26 | #include <net/netfilter/ipv6/nf_conntrack_icmpv6.h> | |
27 | ||
933a41e7 | 28 | static unsigned long nf_ct_icmpv6_timeout __read_mostly = 30*HZ; |
9fb9cbb1 YK |
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 | ||
c1d10adb PNA |
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 | ||
9fb9cbb1 YK |
60 | static int icmpv6_invert_tuple(struct nf_conntrack_tuple *tuple, |
61 | const struct nf_conntrack_tuple *orig) | |
62 | { | |
f16c9107 YK |
63 | int type = orig->dst.u.icmp.type - 128; |
64 | if (type < 0 || type >= sizeof(invmap) || !invmap[type]) | |
9fb9cbb1 YK |
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: | |
1ab1457c YH |
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) */ | |
9fb9cbb1 YK |
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 | }; | |
f16c9107 | 124 | int type = conntrack->tuplehash[0].tuple.dst.u.icmp.type - 128; |
9fb9cbb1 | 125 | |
f16c9107 | 126 | if (type < 0 || type >= sizeof(valid_new) || !valid_new[type]) { |
9fb9cbb1 | 127 | /* Can't create a new ICMPv6 `conn' with this. */ |
f16c9107 YK |
128 | DEBUGP("icmpv6: can't create new conn with type %u\n", |
129 | type + 128); | |
9fb9cbb1 YK |
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 | ||
9fb9cbb1 YK |
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; | |
605dcad6 | 147 | struct nf_conntrack_l4proto *inproto; |
9fb9cbb1 YK |
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 | ||
75202e76 | 171 | if ((inprotoff > skb->len) || (inprotonum == NEXTHDR_FRAGMENT)) { |
9fb9cbb1 YK |
172 | DEBUGP("icmpv6_error: Can't get protocol header in ICMPv6 payload.\n"); |
173 | return -NF_ACCEPT; | |
174 | } | |
175 | ||
923f4902 | 176 | /* rcu_read_lock()ed by nf_hook_slow */ |
605dcad6 | 177 | inproto = __nf_ct_l4proto_find(PF_INET6, inprotonum); |
9fb9cbb1 YK |
178 | |
179 | /* Are they talking about one of our connections? */ | |
180 | if (!nf_ct_get_tuple(skb, inip6off, inprotoff, PF_INET6, inprotonum, | |
181 | &origtuple, &nf_conntrack_l3proto_ipv6, inproto)) { | |
182 | DEBUGP("icmpv6_error: Can't get tuple\n"); | |
183 | return -NF_ACCEPT; | |
184 | } | |
185 | ||
186 | /* Ordinarily, we'd expect the inverted tupleproto, but it's | |
187 | been preserved inside the ICMP. */ | |
188 | if (!nf_ct_invert_tuple(&intuple, &origtuple, | |
189 | &nf_conntrack_l3proto_ipv6, inproto)) { | |
190 | DEBUGP("icmpv6_error: Can't invert tuple\n"); | |
191 | return -NF_ACCEPT; | |
192 | } | |
193 | ||
194 | *ctinfo = IP_CT_RELATED; | |
195 | ||
196 | h = nf_conntrack_find_get(&intuple, NULL); | |
197 | if (!h) { | |
198 | DEBUGP("icmpv6_error: no match\n"); | |
199 | return -NF_ACCEPT; | |
200 | } else { | |
201 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) | |
202 | *ctinfo += IP_CT_IS_REPLY; | |
203 | } | |
204 | ||
205 | /* Update skb to refer to this connection */ | |
206 | skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general; | |
207 | skb->nfctinfo = *ctinfo; | |
208 | return -NF_ACCEPT; | |
209 | } | |
210 | ||
211 | static int | |
212 | icmpv6_error(struct sk_buff *skb, unsigned int dataoff, | |
213 | enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum) | |
214 | { | |
215 | struct icmp6hdr _ih, *icmp6h; | |
216 | ||
217 | icmp6h = skb_header_pointer(skb, dataoff, sizeof(_ih), &_ih); | |
218 | if (icmp6h == NULL) { | |
219 | if (LOG_INVALID(IPPROTO_ICMPV6)) | |
220 | nf_log_packet(PF_INET6, 0, skb, NULL, NULL, NULL, | |
221 | "nf_ct_icmpv6: short packet "); | |
222 | return -NF_ACCEPT; | |
223 | } | |
224 | ||
39a27a35 | 225 | if (nf_conntrack_checksum && hooknum == NF_IP6_PRE_ROUTING && |
96f6bf82 | 226 | nf_ip6_checksum(skb, hooknum, dataoff, IPPROTO_ICMPV6)) { |
9fb9cbb1 YK |
227 | nf_log_packet(PF_INET6, 0, skb, NULL, NULL, NULL, |
228 | "nf_ct_icmpv6: ICMPv6 checksum failed\n"); | |
229 | return -NF_ACCEPT; | |
230 | } | |
231 | ||
9fb9cbb1 YK |
232 | /* is not error message ? */ |
233 | if (icmp6h->icmp6_type >= 128) | |
234 | return NF_ACCEPT; | |
235 | ||
236 | return icmpv6_error_message(skb, dataoff, ctinfo, hooknum); | |
237 | } | |
238 | ||
e281db5c | 239 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
c1d10adb PNA |
240 | |
241 | #include <linux/netfilter/nfnetlink.h> | |
242 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
243 | static int icmpv6_tuple_to_nfattr(struct sk_buff *skb, | |
244 | const struct nf_conntrack_tuple *t) | |
245 | { | |
246 | NFA_PUT(skb, CTA_PROTO_ICMPV6_ID, sizeof(u_int16_t), | |
247 | &t->src.u.icmp.id); | |
248 | NFA_PUT(skb, CTA_PROTO_ICMPV6_TYPE, sizeof(u_int8_t), | |
249 | &t->dst.u.icmp.type); | |
250 | NFA_PUT(skb, CTA_PROTO_ICMPV6_CODE, sizeof(u_int8_t), | |
251 | &t->dst.u.icmp.code); | |
252 | ||
253 | return 0; | |
254 | ||
255 | nfattr_failure: | |
256 | return -1; | |
257 | } | |
258 | ||
259 | static const size_t cta_min_proto[CTA_PROTO_MAX] = { | |
260 | [CTA_PROTO_ICMPV6_TYPE-1] = sizeof(u_int8_t), | |
261 | [CTA_PROTO_ICMPV6_CODE-1] = sizeof(u_int8_t), | |
262 | [CTA_PROTO_ICMPV6_ID-1] = sizeof(u_int16_t) | |
263 | }; | |
264 | ||
265 | static int icmpv6_nfattr_to_tuple(struct nfattr *tb[], | |
266 | struct nf_conntrack_tuple *tuple) | |
267 | { | |
268 | if (!tb[CTA_PROTO_ICMPV6_TYPE-1] | |
269 | || !tb[CTA_PROTO_ICMPV6_CODE-1] | |
270 | || !tb[CTA_PROTO_ICMPV6_ID-1]) | |
271 | return -EINVAL; | |
272 | ||
273 | if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto)) | |
274 | return -EINVAL; | |
275 | ||
276 | tuple->dst.u.icmp.type = | |
277 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMPV6_TYPE-1]); | |
278 | tuple->dst.u.icmp.code = | |
279 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMPV6_CODE-1]); | |
280 | tuple->src.u.icmp.id = | |
bff9a89b | 281 | *(__be16 *)NFA_DATA(tb[CTA_PROTO_ICMPV6_ID-1]); |
c1d10adb PNA |
282 | |
283 | if (tuple->dst.u.icmp.type < 128 | |
284 | || tuple->dst.u.icmp.type - 128 >= sizeof(invmap) | |
285 | || !invmap[tuple->dst.u.icmp.type - 128]) | |
286 | return -EINVAL; | |
287 | ||
288 | return 0; | |
289 | } | |
290 | #endif | |
291 | ||
933a41e7 PM |
292 | #ifdef CONFIG_SYSCTL |
293 | static struct ctl_table_header *icmpv6_sysctl_header; | |
294 | static struct ctl_table icmpv6_sysctl_table[] = { | |
295 | { | |
296 | .ctl_name = NET_NF_CONNTRACK_ICMPV6_TIMEOUT, | |
297 | .procname = "nf_conntrack_icmpv6_timeout", | |
298 | .data = &nf_ct_icmpv6_timeout, | |
299 | .maxlen = sizeof(unsigned int), | |
300 | .mode = 0644, | |
301 | .proc_handler = &proc_dointvec_jiffies, | |
302 | }, | |
303 | { | |
304 | .ctl_name = 0 | |
305 | } | |
306 | }; | |
307 | #endif /* CONFIG_SYSCTL */ | |
308 | ||
605dcad6 | 309 | struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 = |
9fb9cbb1 YK |
310 | { |
311 | .l3proto = PF_INET6, | |
605dcad6 | 312 | .l4proto = IPPROTO_ICMPV6, |
9fb9cbb1 YK |
313 | .name = "icmpv6", |
314 | .pkt_to_tuple = icmpv6_pkt_to_tuple, | |
315 | .invert_tuple = icmpv6_invert_tuple, | |
316 | .print_tuple = icmpv6_print_tuple, | |
317 | .print_conntrack = icmpv6_print_conntrack, | |
318 | .packet = icmpv6_packet, | |
319 | .new = icmpv6_new, | |
320 | .error = icmpv6_error, | |
e281db5c | 321 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
c1d10adb PNA |
322 | .tuple_to_nfattr = icmpv6_tuple_to_nfattr, |
323 | .nfattr_to_tuple = icmpv6_nfattr_to_tuple, | |
324 | #endif | |
933a41e7 PM |
325 | #ifdef CONFIG_SYSCTL |
326 | .ctl_table_header = &icmpv6_sysctl_header, | |
327 | .ctl_table = icmpv6_sysctl_table, | |
328 | #endif | |
9fb9cbb1 YK |
329 | }; |
330 | ||
605dcad6 | 331 | EXPORT_SYMBOL(nf_conntrack_l4proto_icmpv6); |