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
;
30 static int icmpv6_pkt_to_tuple(const struct sk_buff
*skb
,
32 struct nf_conntrack_tuple
*tuple
)
34 struct icmp6hdr _hdr
, *hp
;
36 hp
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
39 tuple
->dst
.u
.icmp
.type
= hp
->icmp6_type
;
40 tuple
->src
.u
.icmp
.id
= hp
->icmp6_identifier
;
41 tuple
->dst
.u
.icmp
.code
= hp
->icmp6_code
;
46 /* Add 1; spaces filled with 0. */
47 static u_int8_t invmap
[] = {
48 [ICMPV6_ECHO_REQUEST
- 128] = ICMPV6_ECHO_REPLY
+ 1,
49 [ICMPV6_ECHO_REPLY
- 128] = ICMPV6_ECHO_REQUEST
+ 1,
50 [ICMPV6_NI_QUERY
- 128] = ICMPV6_NI_QUERY
+ 1,
51 [ICMPV6_NI_REPLY
- 128] = ICMPV6_NI_REPLY
+1
54 static int icmpv6_invert_tuple(struct nf_conntrack_tuple
*tuple
,
55 const struct nf_conntrack_tuple
*orig
)
57 int type
= orig
->dst
.u
.icmp
.type
- 128;
58 if (type
< 0 || type
>= sizeof(invmap
) || !invmap
[type
])
61 tuple
->src
.u
.icmp
.id
= orig
->src
.u
.icmp
.id
;
62 tuple
->dst
.u
.icmp
.type
= invmap
[type
] - 1;
63 tuple
->dst
.u
.icmp
.code
= orig
->dst
.u
.icmp
.code
;
67 /* Print out the per-protocol part of the tuple. */
68 static int icmpv6_print_tuple(struct seq_file
*s
,
69 const struct nf_conntrack_tuple
*tuple
)
71 return seq_printf(s
, "type=%u code=%u id=%u ",
72 tuple
->dst
.u
.icmp
.type
,
73 tuple
->dst
.u
.icmp
.code
,
74 ntohs(tuple
->src
.u
.icmp
.id
));
77 /* Print out the private part of the conntrack. */
78 static int icmpv6_print_conntrack(struct seq_file
*s
,
79 const struct nf_conn
*conntrack
)
84 /* Returns verdict for packet, or -1 for invalid. */
85 static int icmpv6_packet(struct nf_conn
*ct
,
86 const struct sk_buff
*skb
,
88 enum ip_conntrack_info ctinfo
,
92 /* Try to delete connection immediately after all replies:
93 won't actually vanish as we still have skb, and del_timer
94 means this will only run once even if count hits zero twice
95 (theoretically possible with SMP) */
96 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
) {
97 if (atomic_dec_and_test(&ct
->proto
.icmp
.count
)
98 && del_timer(&ct
->timeout
))
99 ct
->timeout
.function((unsigned long)ct
);
101 atomic_inc(&ct
->proto
.icmp
.count
);
102 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE
, skb
);
103 nf_ct_refresh_acct(ct
, ctinfo
, skb
, nf_ct_icmpv6_timeout
);
109 /* Called when a new connection for this protocol found. */
110 static int icmpv6_new(struct nf_conn
*conntrack
,
111 const struct sk_buff
*skb
,
112 unsigned int dataoff
)
114 static u_int8_t valid_new
[] = {
115 [ICMPV6_ECHO_REQUEST
- 128] = 1,
116 [ICMPV6_NI_QUERY
- 128] = 1
118 int type
= conntrack
->tuplehash
[0].tuple
.dst
.u
.icmp
.type
- 128;
120 if (type
< 0 || type
>= sizeof(valid_new
) || !valid_new
[type
]) {
121 /* Can't create a new ICMPv6 `conn' with this. */
122 pr_debug("icmpv6: can't create new conn with type %u\n",
124 NF_CT_DUMP_TUPLE(&conntrack
->tuplehash
[0].tuple
);
127 atomic_set(&conntrack
->proto
.icmp
.count
, 0);
132 icmpv6_error_message(struct sk_buff
*skb
,
133 unsigned int icmp6off
,
134 enum ip_conntrack_info
*ctinfo
,
135 unsigned int hooknum
)
137 struct nf_conntrack_tuple intuple
, origtuple
;
138 struct nf_conntrack_tuple_hash
*h
;
139 struct nf_conntrack_l4proto
*inproto
;
141 NF_CT_ASSERT(skb
->nfct
== NULL
);
143 /* Are they talking about one of our connections? */
144 if (!nf_ct_get_tuplepr(skb
,
145 skb_network_offset(skb
)
146 + sizeof(struct ipv6hdr
)
147 + sizeof(struct icmp6hdr
),
148 PF_INET6
, &origtuple
)) {
149 pr_debug("icmpv6_error: Can't get tuple\n");
153 /* rcu_read_lock()ed by nf_hook_slow */
154 inproto
= __nf_ct_l4proto_find(PF_INET6
, origtuple
.dst
.protonum
);
156 /* Ordinarily, we'd expect the inverted tupleproto, but it's
157 been preserved inside the ICMP. */
158 if (!nf_ct_invert_tuple(&intuple
, &origtuple
,
159 &nf_conntrack_l3proto_ipv6
, inproto
)) {
160 pr_debug("icmpv6_error: Can't invert tuple\n");
164 *ctinfo
= IP_CT_RELATED
;
166 h
= nf_conntrack_find_get(&intuple
);
168 pr_debug("icmpv6_error: no match\n");
171 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
)
172 *ctinfo
+= IP_CT_IS_REPLY
;
175 /* Update skb to refer to this connection */
176 skb
->nfct
= &nf_ct_tuplehash_to_ctrack(h
)->ct_general
;
177 skb
->nfctinfo
= *ctinfo
;
182 icmpv6_error(struct sk_buff
*skb
, unsigned int dataoff
,
183 enum ip_conntrack_info
*ctinfo
, int pf
, unsigned int hooknum
)
185 struct icmp6hdr _ih
, *icmp6h
;
187 icmp6h
= skb_header_pointer(skb
, dataoff
, sizeof(_ih
), &_ih
);
188 if (icmp6h
== NULL
) {
189 if (LOG_INVALID(IPPROTO_ICMPV6
))
190 nf_log_packet(PF_INET6
, 0, skb
, NULL
, NULL
, NULL
,
191 "nf_ct_icmpv6: short packet ");
195 if (nf_conntrack_checksum
&& hooknum
== NF_IP6_PRE_ROUTING
&&
196 nf_ip6_checksum(skb
, hooknum
, dataoff
, IPPROTO_ICMPV6
)) {
197 nf_log_packet(PF_INET6
, 0, skb
, NULL
, NULL
, NULL
,
198 "nf_ct_icmpv6: ICMPv6 checksum failed\n");
202 /* is not error message ? */
203 if (icmp6h
->icmp6_type
>= 128)
206 return icmpv6_error_message(skb
, dataoff
, ctinfo
, hooknum
);
209 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
211 #include <linux/netfilter/nfnetlink.h>
212 #include <linux/netfilter/nfnetlink_conntrack.h>
213 static int icmpv6_tuple_to_nlattr(struct sk_buff
*skb
,
214 const struct nf_conntrack_tuple
*t
)
216 NLA_PUT(skb
, CTA_PROTO_ICMPV6_ID
, sizeof(u_int16_t
),
218 NLA_PUT(skb
, CTA_PROTO_ICMPV6_TYPE
, sizeof(u_int8_t
),
219 &t
->dst
.u
.icmp
.type
);
220 NLA_PUT(skb
, CTA_PROTO_ICMPV6_CODE
, sizeof(u_int8_t
),
221 &t
->dst
.u
.icmp
.code
);
229 static const struct nla_policy icmpv6_nla_policy
[CTA_PROTO_MAX
+1] = {
230 [CTA_PROTO_ICMPV6_TYPE
] = { .type
= NLA_U8
},
231 [CTA_PROTO_ICMPV6_CODE
] = { .type
= NLA_U8
},
232 [CTA_PROTO_ICMPV6_ID
] = { .type
= NLA_U16
},
235 static int icmpv6_nlattr_to_tuple(struct nlattr
*tb
[],
236 struct nf_conntrack_tuple
*tuple
)
238 if (!tb
[CTA_PROTO_ICMPV6_TYPE
]
239 || !tb
[CTA_PROTO_ICMPV6_CODE
]
240 || !tb
[CTA_PROTO_ICMPV6_ID
])
243 tuple
->dst
.u
.icmp
.type
=
244 *(u_int8_t
*)nla_data(tb
[CTA_PROTO_ICMPV6_TYPE
]);
245 tuple
->dst
.u
.icmp
.code
=
246 *(u_int8_t
*)nla_data(tb
[CTA_PROTO_ICMPV6_CODE
]);
247 tuple
->src
.u
.icmp
.id
=
248 *(__be16
*)nla_data(tb
[CTA_PROTO_ICMPV6_ID
]);
250 if (tuple
->dst
.u
.icmp
.type
< 128
251 || tuple
->dst
.u
.icmp
.type
- 128 >= sizeof(invmap
)
252 || !invmap
[tuple
->dst
.u
.icmp
.type
- 128])
260 static struct ctl_table_header
*icmpv6_sysctl_header
;
261 static struct ctl_table icmpv6_sysctl_table
[] = {
263 .procname
= "nf_conntrack_icmpv6_timeout",
264 .data
= &nf_ct_icmpv6_timeout
,
265 .maxlen
= sizeof(unsigned int),
267 .proc_handler
= &proc_dointvec_jiffies
,
273 #endif /* CONFIG_SYSCTL */
275 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 __read_mostly
=
278 .l4proto
= IPPROTO_ICMPV6
,
280 .pkt_to_tuple
= icmpv6_pkt_to_tuple
,
281 .invert_tuple
= icmpv6_invert_tuple
,
282 .print_tuple
= icmpv6_print_tuple
,
283 .print_conntrack
= icmpv6_print_conntrack
,
284 .packet
= icmpv6_packet
,
286 .error
= icmpv6_error
,
287 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
288 .tuple_to_nlattr
= icmpv6_tuple_to_nlattr
,
289 .nlattr_to_tuple
= icmpv6_nlattr_to_tuple
,
290 .nla_policy
= icmpv6_nla_policy
,
293 .ctl_table_header
= &icmpv6_sysctl_header
,
294 .ctl_table
= icmpv6_sysctl_table
,