Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/types.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 24 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 25 | #include <net/netfilter/nf_conntrack_core.h> |
| 26 | #include <net/netfilter/ipv6/nf_conntrack_icmpv6.h> |
| 27 | |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 28 | static unsigned long nf_ct_icmpv6_timeout __read_mostly = 30*HZ; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 29 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 30 | static int icmpv6_pkt_to_tuple(const struct sk_buff *skb, |
| 31 | unsigned int dataoff, |
| 32 | struct nf_conntrack_tuple *tuple) |
| 33 | { |
| 34 | struct icmp6hdr _hdr, *hp; |
| 35 | |
| 36 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); |
| 37 | if (hp == NULL) |
| 38 | return 0; |
| 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; |
| 42 | |
| 43 | return 1; |
| 44 | } |
| 45 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 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 |
| 52 | }; |
| 53 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 54 | static int icmpv6_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 55 | const struct nf_conntrack_tuple *orig) |
| 56 | { |
Yasuyuki Kozakai | f16c910 | 2005-12-05 13:32:50 -0800 | [diff] [blame] | 57 | int type = orig->dst.u.icmp.type - 128; |
| 58 | if (type < 0 || type >= sizeof(invmap) || !invmap[type]) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 59 | return 0; |
| 60 | |
| 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; |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 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) |
| 70 | { |
| 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)); |
| 75 | } |
| 76 | |
| 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) |
| 80 | { |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | /* Returns verdict for packet, or -1 for invalid. */ |
| 85 | static int icmpv6_packet(struct nf_conn *ct, |
| 86 | const struct sk_buff *skb, |
| 87 | unsigned int dataoff, |
| 88 | enum ip_conntrack_info ctinfo, |
| 89 | int pf, |
| 90 | unsigned int hooknum) |
| 91 | { |
| 92 | /* Try to delete connection immediately after all replies: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 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) */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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); |
| 100 | } else { |
| 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); |
| 104 | } |
| 105 | |
| 106 | return NF_ACCEPT; |
| 107 | } |
| 108 | |
| 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) |
| 113 | { |
| 114 | static u_int8_t valid_new[] = { |
| 115 | [ICMPV6_ECHO_REQUEST - 128] = 1, |
| 116 | [ICMPV6_NI_QUERY - 128] = 1 |
| 117 | }; |
Yasuyuki Kozakai | f16c910 | 2005-12-05 13:32:50 -0800 | [diff] [blame] | 118 | int type = conntrack->tuplehash[0].tuple.dst.u.icmp.type - 128; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 119 | |
Yasuyuki Kozakai | f16c910 | 2005-12-05 13:32:50 -0800 | [diff] [blame] | 120 | if (type < 0 || type >= sizeof(valid_new) || !valid_new[type]) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 121 | /* Can't create a new ICMPv6 `conn' with this. */ |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 122 | pr_debug("icmpv6: can't create new conn with type %u\n", |
| 123 | type + 128); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 124 | NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple); |
| 125 | return 0; |
| 126 | } |
| 127 | atomic_set(&conntrack->proto.icmp.count, 0); |
| 128 | return 1; |
| 129 | } |
| 130 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 131 | static int |
| 132 | icmpv6_error_message(struct sk_buff *skb, |
| 133 | unsigned int icmp6off, |
| 134 | enum ip_conntrack_info *ctinfo, |
| 135 | unsigned int hooknum) |
| 136 | { |
| 137 | struct nf_conntrack_tuple intuple, origtuple; |
| 138 | struct nf_conntrack_tuple_hash *h; |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 139 | struct nf_conntrack_l4proto *inproto; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 140 | |
| 141 | NF_CT_ASSERT(skb->nfct == NULL); |
| 142 | |
Yasuyuki Kozakai | e2a3123 | 2007-07-14 20:45:14 -0700 | [diff] [blame] | 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"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 150 | return -NF_ACCEPT; |
| 151 | } |
| 152 | |
Patrick McHardy | 923f490 | 2007-02-12 11:12:57 -0800 | [diff] [blame] | 153 | /* rcu_read_lock()ed by nf_hook_slow */ |
Yasuyuki Kozakai | e2a3123 | 2007-07-14 20:45:14 -0700 | [diff] [blame] | 154 | inproto = __nf_ct_l4proto_find(PF_INET6, origtuple.dst.protonum); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 155 | |
| 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)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 160 | pr_debug("icmpv6_error: Can't invert tuple\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 161 | return -NF_ACCEPT; |
| 162 | } |
| 163 | |
| 164 | *ctinfo = IP_CT_RELATED; |
| 165 | |
Patrick McHardy | 330f7db | 2007-07-07 22:28:42 -0700 | [diff] [blame] | 166 | h = nf_conntrack_find_get(&intuple); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 167 | if (!h) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 168 | pr_debug("icmpv6_error: no match\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 169 | return -NF_ACCEPT; |
| 170 | } else { |
| 171 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) |
| 172 | *ctinfo += IP_CT_IS_REPLY; |
| 173 | } |
| 174 | |
| 175 | /* Update skb to refer to this connection */ |
| 176 | skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general; |
| 177 | skb->nfctinfo = *ctinfo; |
| 178 | return -NF_ACCEPT; |
| 179 | } |
| 180 | |
| 181 | static int |
| 182 | icmpv6_error(struct sk_buff *skb, unsigned int dataoff, |
| 183 | enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum) |
| 184 | { |
| 185 | struct icmp6hdr _ih, *icmp6h; |
| 186 | |
| 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 "); |
| 192 | return -NF_ACCEPT; |
| 193 | } |
| 194 | |
Patrick McHardy | 39a27a3 | 2006-05-29 18:23:54 -0700 | [diff] [blame] | 195 | if (nf_conntrack_checksum && hooknum == NF_IP6_PRE_ROUTING && |
Patrick McHardy | 96f6bf8 | 2006-04-06 14:19:24 -0700 | [diff] [blame] | 196 | nf_ip6_checksum(skb, hooknum, dataoff, IPPROTO_ICMPV6)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 197 | nf_log_packet(PF_INET6, 0, skb, NULL, NULL, NULL, |
| 198 | "nf_ct_icmpv6: ICMPv6 checksum failed\n"); |
| 199 | return -NF_ACCEPT; |
| 200 | } |
| 201 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 202 | /* is not error message ? */ |
| 203 | if (icmp6h->icmp6_type >= 128) |
| 204 | return NF_ACCEPT; |
| 205 | |
| 206 | return icmpv6_error_message(skb, dataoff, ctinfo, hooknum); |
| 207 | } |
| 208 | |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 209 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 210 | |
| 211 | #include <linux/netfilter/nfnetlink.h> |
| 212 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 213 | static int icmpv6_tuple_to_nfattr(struct sk_buff *skb, |
| 214 | const struct nf_conntrack_tuple *t) |
| 215 | { |
| 216 | NFA_PUT(skb, CTA_PROTO_ICMPV6_ID, sizeof(u_int16_t), |
| 217 | &t->src.u.icmp.id); |
| 218 | NFA_PUT(skb, CTA_PROTO_ICMPV6_TYPE, sizeof(u_int8_t), |
| 219 | &t->dst.u.icmp.type); |
| 220 | NFA_PUT(skb, CTA_PROTO_ICMPV6_CODE, sizeof(u_int8_t), |
| 221 | &t->dst.u.icmp.code); |
| 222 | |
| 223 | return 0; |
| 224 | |
| 225 | nfattr_failure: |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | static const size_t cta_min_proto[CTA_PROTO_MAX] = { |
| 230 | [CTA_PROTO_ICMPV6_TYPE-1] = sizeof(u_int8_t), |
| 231 | [CTA_PROTO_ICMPV6_CODE-1] = sizeof(u_int8_t), |
| 232 | [CTA_PROTO_ICMPV6_ID-1] = sizeof(u_int16_t) |
| 233 | }; |
| 234 | |
| 235 | static int icmpv6_nfattr_to_tuple(struct nfattr *tb[], |
| 236 | struct nf_conntrack_tuple *tuple) |
| 237 | { |
| 238 | if (!tb[CTA_PROTO_ICMPV6_TYPE-1] |
| 239 | || !tb[CTA_PROTO_ICMPV6_CODE-1] |
| 240 | || !tb[CTA_PROTO_ICMPV6_ID-1]) |
| 241 | return -EINVAL; |
| 242 | |
| 243 | if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto)) |
| 244 | return -EINVAL; |
| 245 | |
| 246 | tuple->dst.u.icmp.type = |
| 247 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMPV6_TYPE-1]); |
| 248 | tuple->dst.u.icmp.code = |
| 249 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMPV6_CODE-1]); |
| 250 | tuple->src.u.icmp.id = |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 251 | *(__be16 *)NFA_DATA(tb[CTA_PROTO_ICMPV6_ID-1]); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 252 | |
| 253 | if (tuple->dst.u.icmp.type < 128 |
| 254 | || tuple->dst.u.icmp.type - 128 >= sizeof(invmap) |
| 255 | || !invmap[tuple->dst.u.icmp.type - 128]) |
| 256 | return -EINVAL; |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | #endif |
| 261 | |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 262 | #ifdef CONFIG_SYSCTL |
| 263 | static struct ctl_table_header *icmpv6_sysctl_header; |
| 264 | static struct ctl_table icmpv6_sysctl_table[] = { |
| 265 | { |
| 266 | .ctl_name = NET_NF_CONNTRACK_ICMPV6_TIMEOUT, |
| 267 | .procname = "nf_conntrack_icmpv6_timeout", |
| 268 | .data = &nf_ct_icmpv6_timeout, |
| 269 | .maxlen = sizeof(unsigned int), |
| 270 | .mode = 0644, |
| 271 | .proc_handler = &proc_dointvec_jiffies, |
| 272 | }, |
| 273 | { |
| 274 | .ctl_name = 0 |
| 275 | } |
| 276 | }; |
| 277 | #endif /* CONFIG_SYSCTL */ |
| 278 | |
Patrick McHardy | 61075af | 2007-07-14 20:48:19 -0700 | [diff] [blame^] | 279 | struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 __read_mostly = |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 280 | { |
| 281 | .l3proto = PF_INET6, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 282 | .l4proto = IPPROTO_ICMPV6, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 283 | .name = "icmpv6", |
| 284 | .pkt_to_tuple = icmpv6_pkt_to_tuple, |
| 285 | .invert_tuple = icmpv6_invert_tuple, |
| 286 | .print_tuple = icmpv6_print_tuple, |
| 287 | .print_conntrack = icmpv6_print_conntrack, |
| 288 | .packet = icmpv6_packet, |
| 289 | .new = icmpv6_new, |
| 290 | .error = icmpv6_error, |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 291 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 292 | .tuple_to_nfattr = icmpv6_tuple_to_nfattr, |
| 293 | .nfattr_to_tuple = icmpv6_nfattr_to_tuple, |
| 294 | #endif |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 295 | #ifdef CONFIG_SYSCTL |
| 296 | .ctl_table_header = &icmpv6_sysctl_header, |
| 297 | .ctl_table = icmpv6_sysctl_table, |
| 298 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 299 | }; |