blob: 02d60dfbab803110cbbc7a3ae3c69ec5ef527660 [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/*
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>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080010 */
11
12#include <linux/types.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080013#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>
Martin Josefsson605dcad2006-11-29 02:35:06 +010024#include <net/netfilter/nf_conntrack_l4proto.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080025#include <net/netfilter/nf_conntrack_core.h>
26#include <net/netfilter/ipv6/nf_conntrack_icmpv6.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080027#include <net/netfilter/nf_log.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080028
Patrick McHardy933a41e2006-11-29 02:35:18 +010029static unsigned long nf_ct_icmpv6_timeout __read_mostly = 30*HZ;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080030
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080031static int icmpv6_pkt_to_tuple(const struct sk_buff *skb,
32 unsigned int dataoff,
33 struct nf_conntrack_tuple *tuple)
34{
35 struct icmp6hdr _hdr, *hp;
36
37 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
38 if (hp == NULL)
39 return 0;
40 tuple->dst.u.icmp.type = hp->icmp6_type;
41 tuple->src.u.icmp.id = hp->icmp6_identifier;
42 tuple->dst.u.icmp.code = hp->icmp6_code;
43
44 return 1;
45}
46
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080047/* Add 1; spaces filled with 0. */
48static u_int8_t invmap[] = {
49 [ICMPV6_ECHO_REQUEST - 128] = ICMPV6_ECHO_REPLY + 1,
50 [ICMPV6_ECHO_REPLY - 128] = ICMPV6_ECHO_REQUEST + 1,
51 [ICMPV6_NI_QUERY - 128] = ICMPV6_NI_QUERY + 1,
52 [ICMPV6_NI_REPLY - 128] = ICMPV6_NI_REPLY +1
53};
54
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080055static int icmpv6_invert_tuple(struct nf_conntrack_tuple *tuple,
56 const struct nf_conntrack_tuple *orig)
57{
Yasuyuki Kozakaif16c9102005-12-05 13:32:50 -080058 int type = orig->dst.u.icmp.type - 128;
59 if (type < 0 || type >= sizeof(invmap) || !invmap[type])
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080060 return 0;
61
62 tuple->src.u.icmp.id = orig->src.u.icmp.id;
63 tuple->dst.u.icmp.type = invmap[type] - 1;
64 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
65 return 1;
66}
67
68/* Print out the per-protocol part of the tuple. */
69static int icmpv6_print_tuple(struct seq_file *s,
70 const struct nf_conntrack_tuple *tuple)
71{
72 return seq_printf(s, "type=%u code=%u id=%u ",
73 tuple->dst.u.icmp.type,
74 tuple->dst.u.icmp.code,
75 ntohs(tuple->src.u.icmp.id));
76}
77
78/* Print out the private part of the conntrack. */
79static int icmpv6_print_conntrack(struct seq_file *s,
80 const struct nf_conn *conntrack)
81{
82 return 0;
83}
84
85/* Returns verdict for packet, or -1 for invalid. */
86static int icmpv6_packet(struct nf_conn *ct,
87 const struct sk_buff *skb,
88 unsigned int dataoff,
89 enum ip_conntrack_info ctinfo,
90 int pf,
91 unsigned int hooknum)
92{
93 /* Try to delete connection immediately after all replies:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090094 won't actually vanish as we still have skb, and del_timer
95 means this will only run once even if count hits zero twice
96 (theoretically possible with SMP) */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080097 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
98 if (atomic_dec_and_test(&ct->proto.icmp.count)
99 && del_timer(&ct->timeout))
100 ct->timeout.function((unsigned long)ct);
101 } else {
102 atomic_inc(&ct->proto.icmp.count);
103 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
104 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmpv6_timeout);
105 }
106
107 return NF_ACCEPT;
108}
109
110/* Called when a new connection for this protocol found. */
111static int icmpv6_new(struct nf_conn *conntrack,
112 const struct sk_buff *skb,
113 unsigned int dataoff)
114{
115 static u_int8_t valid_new[] = {
116 [ICMPV6_ECHO_REQUEST - 128] = 1,
117 [ICMPV6_NI_QUERY - 128] = 1
118 };
Yasuyuki Kozakaif16c9102005-12-05 13:32:50 -0800119 int type = conntrack->tuplehash[0].tuple.dst.u.icmp.type - 128;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800120
Yasuyuki Kozakaif16c9102005-12-05 13:32:50 -0800121 if (type < 0 || type >= sizeof(valid_new) || !valid_new[type]) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800122 /* Can't create a new ICMPv6 `conn' with this. */
Patrick McHardy0d537782007-07-07 22:39:38 -0700123 pr_debug("icmpv6: can't create new conn with type %u\n",
124 type + 128);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800125 NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
126 return 0;
127 }
128 atomic_set(&conntrack->proto.icmp.count, 0);
129 return 1;
130}
131
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800132static int
133icmpv6_error_message(struct sk_buff *skb,
134 unsigned int icmp6off,
135 enum ip_conntrack_info *ctinfo,
136 unsigned int hooknum)
137{
138 struct nf_conntrack_tuple intuple, origtuple;
139 struct nf_conntrack_tuple_hash *h;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100140 struct nf_conntrack_l4proto *inproto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800141
142 NF_CT_ASSERT(skb->nfct == NULL);
143
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700144 /* Are they talking about one of our connections? */
145 if (!nf_ct_get_tuplepr(skb,
146 skb_network_offset(skb)
147 + sizeof(struct ipv6hdr)
148 + sizeof(struct icmp6hdr),
149 PF_INET6, &origtuple)) {
150 pr_debug("icmpv6_error: Can't get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800151 return -NF_ACCEPT;
152 }
153
Patrick McHardy923f4902007-02-12 11:12:57 -0800154 /* rcu_read_lock()ed by nf_hook_slow */
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700155 inproto = __nf_ct_l4proto_find(PF_INET6, origtuple.dst.protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800156
157 /* Ordinarily, we'd expect the inverted tupleproto, but it's
158 been preserved inside the ICMP. */
159 if (!nf_ct_invert_tuple(&intuple, &origtuple,
160 &nf_conntrack_l3proto_ipv6, inproto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700161 pr_debug("icmpv6_error: Can't invert tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800162 return -NF_ACCEPT;
163 }
164
165 *ctinfo = IP_CT_RELATED;
166
Patrick McHardy330f7db2007-07-07 22:28:42 -0700167 h = nf_conntrack_find_get(&intuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800168 if (!h) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700169 pr_debug("icmpv6_error: no match\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800170 return -NF_ACCEPT;
171 } else {
172 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
173 *ctinfo += IP_CT_IS_REPLY;
174 }
175
176 /* Update skb to refer to this connection */
177 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
178 skb->nfctinfo = *ctinfo;
179 return -NF_ACCEPT;
180}
181
182static int
183icmpv6_error(struct sk_buff *skb, unsigned int dataoff,
184 enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
185{
186 struct icmp6hdr _ih, *icmp6h;
187
188 icmp6h = skb_header_pointer(skb, dataoff, sizeof(_ih), &_ih);
189 if (icmp6h == NULL) {
190 if (LOG_INVALID(IPPROTO_ICMPV6))
191 nf_log_packet(PF_INET6, 0, skb, NULL, NULL, NULL,
192 "nf_ct_icmpv6: short packet ");
193 return -NF_ACCEPT;
194 }
195
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800196 if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700197 nf_ip6_checksum(skb, hooknum, dataoff, IPPROTO_ICMPV6)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800198 nf_log_packet(PF_INET6, 0, skb, NULL, NULL, NULL,
199 "nf_ct_icmpv6: ICMPv6 checksum failed\n");
200 return -NF_ACCEPT;
201 }
202
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800203 /* is not error message ? */
204 if (icmp6h->icmp6_type >= 128)
205 return NF_ACCEPT;
206
207 return icmpv6_error_message(skb, dataoff, ctinfo, hooknum);
208}
209
Patrick McHardye281db5c2007-03-04 15:57:25 -0800210#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800211
212#include <linux/netfilter/nfnetlink.h>
213#include <linux/netfilter/nfnetlink_conntrack.h>
Patrick McHardyfdf70832007-09-28 14:37:41 -0700214static int icmpv6_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800215 const struct nf_conntrack_tuple *t)
216{
Patrick McHardy77236b62007-12-17 22:29:45 -0800217 NLA_PUT_BE16(skb, CTA_PROTO_ICMPV6_ID, t->src.u.icmp.id);
218 NLA_PUT_U8(skb, CTA_PROTO_ICMPV6_TYPE, t->dst.u.icmp.type);
219 NLA_PUT_U8(skb, CTA_PROTO_ICMPV6_CODE, t->dst.u.icmp.code);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800220
221 return 0;
222
Patrick McHardydf6fb862007-09-28 14:37:03 -0700223nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800224 return -1;
225}
226
Patrick McHardyf73e9242007-09-28 14:39:55 -0700227static const struct nla_policy icmpv6_nla_policy[CTA_PROTO_MAX+1] = {
228 [CTA_PROTO_ICMPV6_TYPE] = { .type = NLA_U8 },
229 [CTA_PROTO_ICMPV6_CODE] = { .type = NLA_U8 },
230 [CTA_PROTO_ICMPV6_ID] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800231};
232
Patrick McHardyfdf70832007-09-28 14:37:41 -0700233static int icmpv6_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800234 struct nf_conntrack_tuple *tuple)
235{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700236 if (!tb[CTA_PROTO_ICMPV6_TYPE]
237 || !tb[CTA_PROTO_ICMPV6_CODE]
238 || !tb[CTA_PROTO_ICMPV6_ID])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800239 return -EINVAL;
240
Patrick McHardy77236b62007-12-17 22:29:45 -0800241 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMPV6_TYPE]);
242 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMPV6_CODE]);
243 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMPV6_ID]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800244
245 if (tuple->dst.u.icmp.type < 128
246 || tuple->dst.u.icmp.type - 128 >= sizeof(invmap)
247 || !invmap[tuple->dst.u.icmp.type - 128])
248 return -EINVAL;
249
250 return 0;
251}
252#endif
253
Patrick McHardy933a41e2006-11-29 02:35:18 +0100254#ifdef CONFIG_SYSCTL
255static struct ctl_table_header *icmpv6_sysctl_header;
256static struct ctl_table icmpv6_sysctl_table[] = {
257 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100258 .procname = "nf_conntrack_icmpv6_timeout",
259 .data = &nf_ct_icmpv6_timeout,
260 .maxlen = sizeof(unsigned int),
261 .mode = 0644,
262 .proc_handler = &proc_dointvec_jiffies,
263 },
264 {
265 .ctl_name = 0
266 }
267};
268#endif /* CONFIG_SYSCTL */
269
Patrick McHardy61075af2007-07-14 20:48:19 -0700270struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 __read_mostly =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800271{
272 .l3proto = PF_INET6,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100273 .l4proto = IPPROTO_ICMPV6,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800274 .name = "icmpv6",
275 .pkt_to_tuple = icmpv6_pkt_to_tuple,
276 .invert_tuple = icmpv6_invert_tuple,
277 .print_tuple = icmpv6_print_tuple,
278 .print_conntrack = icmpv6_print_conntrack,
279 .packet = icmpv6_packet,
280 .new = icmpv6_new,
281 .error = icmpv6_error,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800282#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700283 .tuple_to_nlattr = icmpv6_tuple_to_nlattr,
284 .nlattr_to_tuple = icmpv6_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700285 .nla_policy = icmpv6_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800286#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100287#ifdef CONFIG_SYSCTL
288 .ctl_table_header = &icmpv6_sysctl_header,
289 .ctl_table = icmpv6_sysctl_table,
290#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800291};