2 * Copyright (C)2003,2004 USAGI/WIDE Project
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.
9 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
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>
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>
28 static unsigned long nf_ct_icmpv6_timeout __read_mostly = 30*HZ;
33 #define DEBUGP(format, args...)
36 static int icmpv6_pkt_to_tuple(const struct sk_buff *skb,
38 struct nf_conntrack_tuple *tuple)
40 struct icmp6hdr _hdr, *hp;
42 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
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;
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
60 static int icmpv6_invert_tuple(struct nf_conntrack_tuple *tuple,
61 const struct nf_conntrack_tuple *orig)
63 int type = orig->dst.u.icmp.type - 128;
64 if (type < 0 || type >= sizeof(invmap) || !invmap[type])
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;
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)
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));
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)
90 /* Returns verdict for packet, or -1 for invalid. */
91 static int icmpv6_packet(struct nf_conn *ct,
92 const struct sk_buff *skb,
94 enum ip_conntrack_info ctinfo,
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);
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);
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)
120 static u_int8_t valid_new[] = {
121 [ICMPV6_ECHO_REQUEST - 128] = 1,
122 [ICMPV6_NI_QUERY - 128] = 1
124 int type = conntrack->tuplehash[0].tuple.dst.u.icmp.type - 128;
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",
130 NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
133 atomic_set(&conntrack->proto.icmp.count, 0);
138 icmpv6_error_message(struct sk_buff *skb,
139 unsigned int icmp6off,
140 enum ip_conntrack_info *ctinfo,
141 unsigned int hooknum)
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;
149 unsigned int inprotoff;
151 NF_CT_ASSERT(skb->nfct == NULL);
153 hp = skb_header_pointer(skb, icmp6off, sizeof(_hdr), &_hdr);
155 DEBUGP("icmpv6_error: Can't get ICMPv6 hdr.\n");
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");
165 inprotoff = nf_ct_ipv6_skip_exthdr(skb,
166 inip6off + sizeof(struct ipv6hdr),
169 - sizeof(struct ipv6hdr));
171 if ((inprotoff < 0) || (inprotoff > skb->len) ||
172 (inprotonum == NEXTHDR_FRAGMENT)) {
173 DEBUGP("icmpv6_error: Can't get protocol header in ICMPv6 payload.\n");
177 /* rcu_read_lock()ed by nf_hook_slow */
178 inproto = __nf_ct_l4proto_find(PF_INET6, inprotonum);
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");
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");
195 *ctinfo = IP_CT_RELATED;
197 h = nf_conntrack_find_get(&intuple, NULL);
199 DEBUGP("icmpv6_error: no match\n");
202 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
203 *ctinfo += IP_CT_IS_REPLY;
206 /* Update skb to refer to this connection */
207 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
208 skb->nfctinfo = *ctinfo;
213 icmpv6_error(struct sk_buff *skb, unsigned int dataoff,
214 enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
216 struct icmp6hdr _ih, *icmp6h;
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 ");
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");
233 /* is not error message ? */
234 if (icmp6h->icmp6_type >= 128)
237 return icmpv6_error_message(skb, dataoff, ctinfo, hooknum);
240 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
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)
247 NFA_PUT(skb, CTA_PROTO_ICMPV6_ID, sizeof(u_int16_t),
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);
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)
266 static int icmpv6_nfattr_to_tuple(struct nfattr *tb[],
267 struct nf_conntrack_tuple *tuple)
269 if (!tb[CTA_PROTO_ICMPV6_TYPE-1]
270 || !tb[CTA_PROTO_ICMPV6_CODE-1]
271 || !tb[CTA_PROTO_ICMPV6_ID-1])
274 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
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]);
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])
294 static struct ctl_table_header *icmpv6_sysctl_header;
295 static struct ctl_table icmpv6_sysctl_table[] = {
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),
302 .proc_handler = &proc_dointvec_jiffies,
308 #endif /* CONFIG_SYSCTL */
310 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 =
313 .l4proto = IPPROTO_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,
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,
327 .ctl_table_header = &icmpv6_sysctl_header,
328 .ctl_table = icmpv6_sysctl_table,
332 EXPORT_SYMBOL(nf_conntrack_l4proto_icmpv6);