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> |
| 10 | * |
| 11 | * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> |
| 12 | * - ICMPv6 tracking support. Derived from the original ip_conntrack code |
| 13 | * net/ipv4/netfilter/ip_conntrack_proto_icmp.c which had the following |
| 14 | * copyright information: |
| 15 | * (C) 1999-2001 Paul `Rusty' Russell |
| 16 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> |
| 17 | */ |
| 18 | |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/timer.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/netfilter.h> |
| 24 | #include <linux/in6.h> |
| 25 | #include <linux/icmpv6.h> |
| 26 | #include <linux/ipv6.h> |
| 27 | #include <net/ipv6.h> |
| 28 | #include <net/ip6_checksum.h> |
| 29 | #include <linux/seq_file.h> |
| 30 | #include <linux/netfilter_ipv6.h> |
| 31 | #include <net/netfilter/nf_conntrack_tuple.h> |
| 32 | #include <net/netfilter/nf_conntrack_protocol.h> |
| 33 | #include <net/netfilter/nf_conntrack_core.h> |
| 34 | #include <net/netfilter/ipv6/nf_conntrack_icmpv6.h> |
| 35 | |
| 36 | unsigned long nf_ct_icmpv6_timeout = 30*HZ; |
| 37 | |
| 38 | #if 0 |
| 39 | #define DEBUGP printk |
| 40 | #else |
| 41 | #define DEBUGP(format, args...) |
| 42 | #endif |
| 43 | |
| 44 | static int icmpv6_pkt_to_tuple(const struct sk_buff *skb, |
| 45 | unsigned int dataoff, |
| 46 | struct nf_conntrack_tuple *tuple) |
| 47 | { |
| 48 | struct icmp6hdr _hdr, *hp; |
| 49 | |
| 50 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); |
| 51 | if (hp == NULL) |
| 52 | return 0; |
| 53 | tuple->dst.u.icmp.type = hp->icmp6_type; |
| 54 | tuple->src.u.icmp.id = hp->icmp6_identifier; |
| 55 | tuple->dst.u.icmp.code = hp->icmp6_code; |
| 56 | |
| 57 | return 1; |
| 58 | } |
| 59 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 60 | /* Add 1; spaces filled with 0. */ |
| 61 | static u_int8_t invmap[] = { |
| 62 | [ICMPV6_ECHO_REQUEST - 128] = ICMPV6_ECHO_REPLY + 1, |
| 63 | [ICMPV6_ECHO_REPLY - 128] = ICMPV6_ECHO_REQUEST + 1, |
| 64 | [ICMPV6_NI_QUERY - 128] = ICMPV6_NI_QUERY + 1, |
| 65 | [ICMPV6_NI_REPLY - 128] = ICMPV6_NI_REPLY +1 |
| 66 | }; |
| 67 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 68 | static int icmpv6_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 69 | const struct nf_conntrack_tuple *orig) |
| 70 | { |
Yasuyuki Kozakai | f16c910 | 2005-12-05 13:32:50 -0800 | [diff] [blame] | 71 | int type = orig->dst.u.icmp.type - 128; |
| 72 | if (type < 0 || type >= sizeof(invmap) || !invmap[type]) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 73 | return 0; |
| 74 | |
| 75 | tuple->src.u.icmp.id = orig->src.u.icmp.id; |
| 76 | tuple->dst.u.icmp.type = invmap[type] - 1; |
| 77 | tuple->dst.u.icmp.code = orig->dst.u.icmp.code; |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | /* Print out the per-protocol part of the tuple. */ |
| 82 | static int icmpv6_print_tuple(struct seq_file *s, |
| 83 | const struct nf_conntrack_tuple *tuple) |
| 84 | { |
| 85 | return seq_printf(s, "type=%u code=%u id=%u ", |
| 86 | tuple->dst.u.icmp.type, |
| 87 | tuple->dst.u.icmp.code, |
| 88 | ntohs(tuple->src.u.icmp.id)); |
| 89 | } |
| 90 | |
| 91 | /* Print out the private part of the conntrack. */ |
| 92 | static int icmpv6_print_conntrack(struct seq_file *s, |
| 93 | const struct nf_conn *conntrack) |
| 94 | { |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | /* Returns verdict for packet, or -1 for invalid. */ |
| 99 | static int icmpv6_packet(struct nf_conn *ct, |
| 100 | const struct sk_buff *skb, |
| 101 | unsigned int dataoff, |
| 102 | enum ip_conntrack_info ctinfo, |
| 103 | int pf, |
| 104 | unsigned int hooknum) |
| 105 | { |
| 106 | /* Try to delete connection immediately after all replies: |
| 107 | won't actually vanish as we still have skb, and del_timer |
| 108 | means this will only run once even if count hits zero twice |
| 109 | (theoretically possible with SMP) */ |
| 110 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) { |
| 111 | if (atomic_dec_and_test(&ct->proto.icmp.count) |
| 112 | && del_timer(&ct->timeout)) |
| 113 | ct->timeout.function((unsigned long)ct); |
| 114 | } else { |
| 115 | atomic_inc(&ct->proto.icmp.count); |
| 116 | nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb); |
| 117 | nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmpv6_timeout); |
| 118 | } |
| 119 | |
| 120 | return NF_ACCEPT; |
| 121 | } |
| 122 | |
| 123 | /* Called when a new connection for this protocol found. */ |
| 124 | static int icmpv6_new(struct nf_conn *conntrack, |
| 125 | const struct sk_buff *skb, |
| 126 | unsigned int dataoff) |
| 127 | { |
| 128 | static u_int8_t valid_new[] = { |
| 129 | [ICMPV6_ECHO_REQUEST - 128] = 1, |
| 130 | [ICMPV6_NI_QUERY - 128] = 1 |
| 131 | }; |
Yasuyuki Kozakai | f16c910 | 2005-12-05 13:32:50 -0800 | [diff] [blame] | 132 | int type = conntrack->tuplehash[0].tuple.dst.u.icmp.type - 128; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 133 | |
Yasuyuki Kozakai | f16c910 | 2005-12-05 13:32:50 -0800 | [diff] [blame] | 134 | if (type < 0 || type >= sizeof(valid_new) || !valid_new[type]) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 135 | /* Can't create a new ICMPv6 `conn' with this. */ |
Yasuyuki Kozakai | f16c910 | 2005-12-05 13:32:50 -0800 | [diff] [blame] | 136 | DEBUGP("icmpv6: can't create new conn with type %u\n", |
| 137 | type + 128); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 138 | NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple); |
| 139 | return 0; |
| 140 | } |
| 141 | atomic_set(&conntrack->proto.icmp.count, 0); |
| 142 | return 1; |
| 143 | } |
| 144 | |
| 145 | extern int |
| 146 | nf_ct_ipv6_skip_exthdr(struct sk_buff *skb, int start, u8 *nexthdrp, int len); |
| 147 | extern struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6; |
| 148 | static int |
| 149 | icmpv6_error_message(struct sk_buff *skb, |
| 150 | unsigned int icmp6off, |
| 151 | enum ip_conntrack_info *ctinfo, |
| 152 | unsigned int hooknum) |
| 153 | { |
| 154 | struct nf_conntrack_tuple intuple, origtuple; |
| 155 | struct nf_conntrack_tuple_hash *h; |
| 156 | struct icmp6hdr _hdr, *hp; |
| 157 | unsigned int inip6off; |
| 158 | struct nf_conntrack_protocol *inproto; |
| 159 | u_int8_t inprotonum; |
| 160 | unsigned int inprotoff; |
| 161 | |
| 162 | NF_CT_ASSERT(skb->nfct == NULL); |
| 163 | |
| 164 | hp = skb_header_pointer(skb, icmp6off, sizeof(_hdr), &_hdr); |
| 165 | if (hp == NULL) { |
| 166 | DEBUGP("icmpv6_error: Can't get ICMPv6 hdr.\n"); |
| 167 | return -NF_ACCEPT; |
| 168 | } |
| 169 | |
| 170 | inip6off = icmp6off + sizeof(_hdr); |
| 171 | if (skb_copy_bits(skb, inip6off+offsetof(struct ipv6hdr, nexthdr), |
| 172 | &inprotonum, sizeof(inprotonum)) != 0) { |
| 173 | DEBUGP("icmpv6_error: Can't get nexthdr in inner IPv6 header.\n"); |
| 174 | return -NF_ACCEPT; |
| 175 | } |
| 176 | inprotoff = nf_ct_ipv6_skip_exthdr(skb, |
| 177 | inip6off + sizeof(struct ipv6hdr), |
| 178 | &inprotonum, |
| 179 | skb->len - inip6off |
| 180 | - sizeof(struct ipv6hdr)); |
| 181 | |
| 182 | if ((inprotoff < 0) || (inprotoff > skb->len) || |
| 183 | (inprotonum == NEXTHDR_FRAGMENT)) { |
| 184 | DEBUGP("icmpv6_error: Can't get protocol header in ICMPv6 payload.\n"); |
| 185 | return -NF_ACCEPT; |
| 186 | } |
| 187 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 188 | inproto = __nf_ct_proto_find(PF_INET6, inprotonum); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 189 | |
| 190 | /* Are they talking about one of our connections? */ |
| 191 | if (!nf_ct_get_tuple(skb, inip6off, inprotoff, PF_INET6, inprotonum, |
| 192 | &origtuple, &nf_conntrack_l3proto_ipv6, inproto)) { |
| 193 | DEBUGP("icmpv6_error: Can't get tuple\n"); |
| 194 | return -NF_ACCEPT; |
| 195 | } |
| 196 | |
| 197 | /* Ordinarily, we'd expect the inverted tupleproto, but it's |
| 198 | been preserved inside the ICMP. */ |
| 199 | if (!nf_ct_invert_tuple(&intuple, &origtuple, |
| 200 | &nf_conntrack_l3proto_ipv6, inproto)) { |
| 201 | DEBUGP("icmpv6_error: Can't invert tuple\n"); |
| 202 | return -NF_ACCEPT; |
| 203 | } |
| 204 | |
| 205 | *ctinfo = IP_CT_RELATED; |
| 206 | |
| 207 | h = nf_conntrack_find_get(&intuple, NULL); |
| 208 | if (!h) { |
| 209 | DEBUGP("icmpv6_error: no match\n"); |
| 210 | return -NF_ACCEPT; |
| 211 | } else { |
| 212 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) |
| 213 | *ctinfo += IP_CT_IS_REPLY; |
| 214 | } |
| 215 | |
| 216 | /* Update skb to refer to this connection */ |
| 217 | skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general; |
| 218 | skb->nfctinfo = *ctinfo; |
| 219 | return -NF_ACCEPT; |
| 220 | } |
| 221 | |
| 222 | static int |
| 223 | icmpv6_error(struct sk_buff *skb, unsigned int dataoff, |
| 224 | enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum) |
| 225 | { |
| 226 | struct icmp6hdr _ih, *icmp6h; |
| 227 | |
| 228 | icmp6h = skb_header_pointer(skb, dataoff, sizeof(_ih), &_ih); |
| 229 | if (icmp6h == NULL) { |
| 230 | if (LOG_INVALID(IPPROTO_ICMPV6)) |
| 231 | nf_log_packet(PF_INET6, 0, skb, NULL, NULL, NULL, |
| 232 | "nf_ct_icmpv6: short packet "); |
| 233 | return -NF_ACCEPT; |
| 234 | } |
| 235 | |
| 236 | if (hooknum != NF_IP6_PRE_ROUTING) |
| 237 | goto skipped; |
| 238 | |
| 239 | /* Ignore it if the checksum's bogus. */ |
| 240 | if (csum_ipv6_magic(&skb->nh.ipv6h->saddr, &skb->nh.ipv6h->daddr, |
| 241 | skb->len - dataoff, IPPROTO_ICMPV6, |
| 242 | skb_checksum(skb, dataoff, |
| 243 | skb->len - dataoff, 0))) { |
| 244 | nf_log_packet(PF_INET6, 0, skb, NULL, NULL, NULL, |
| 245 | "nf_ct_icmpv6: ICMPv6 checksum failed\n"); |
| 246 | return -NF_ACCEPT; |
| 247 | } |
| 248 | |
| 249 | skipped: |
| 250 | |
| 251 | /* is not error message ? */ |
| 252 | if (icmp6h->icmp6_type >= 128) |
| 253 | return NF_ACCEPT; |
| 254 | |
| 255 | return icmpv6_error_message(skb, dataoff, ctinfo, hooknum); |
| 256 | } |
| 257 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 258 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
| 259 | defined(CONFIG_NF_CT_NETLINK_MODULE) |
| 260 | |
| 261 | #include <linux/netfilter/nfnetlink.h> |
| 262 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 263 | static int icmpv6_tuple_to_nfattr(struct sk_buff *skb, |
| 264 | const struct nf_conntrack_tuple *t) |
| 265 | { |
| 266 | NFA_PUT(skb, CTA_PROTO_ICMPV6_ID, sizeof(u_int16_t), |
| 267 | &t->src.u.icmp.id); |
| 268 | NFA_PUT(skb, CTA_PROTO_ICMPV6_TYPE, sizeof(u_int8_t), |
| 269 | &t->dst.u.icmp.type); |
| 270 | NFA_PUT(skb, CTA_PROTO_ICMPV6_CODE, sizeof(u_int8_t), |
| 271 | &t->dst.u.icmp.code); |
| 272 | |
| 273 | return 0; |
| 274 | |
| 275 | nfattr_failure: |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | static const size_t cta_min_proto[CTA_PROTO_MAX] = { |
| 280 | [CTA_PROTO_ICMPV6_TYPE-1] = sizeof(u_int8_t), |
| 281 | [CTA_PROTO_ICMPV6_CODE-1] = sizeof(u_int8_t), |
| 282 | [CTA_PROTO_ICMPV6_ID-1] = sizeof(u_int16_t) |
| 283 | }; |
| 284 | |
| 285 | static int icmpv6_nfattr_to_tuple(struct nfattr *tb[], |
| 286 | struct nf_conntrack_tuple *tuple) |
| 287 | { |
| 288 | if (!tb[CTA_PROTO_ICMPV6_TYPE-1] |
| 289 | || !tb[CTA_PROTO_ICMPV6_CODE-1] |
| 290 | || !tb[CTA_PROTO_ICMPV6_ID-1]) |
| 291 | return -EINVAL; |
| 292 | |
| 293 | if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto)) |
| 294 | return -EINVAL; |
| 295 | |
| 296 | tuple->dst.u.icmp.type = |
| 297 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMPV6_TYPE-1]); |
| 298 | tuple->dst.u.icmp.code = |
| 299 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMPV6_CODE-1]); |
| 300 | tuple->src.u.icmp.id = |
| 301 | *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_ICMPV6_ID-1]); |
| 302 | |
| 303 | if (tuple->dst.u.icmp.type < 128 |
| 304 | || tuple->dst.u.icmp.type - 128 >= sizeof(invmap) |
| 305 | || !invmap[tuple->dst.u.icmp.type - 128]) |
| 306 | return -EINVAL; |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | #endif |
| 311 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 312 | struct nf_conntrack_protocol nf_conntrack_protocol_icmpv6 = |
| 313 | { |
| 314 | .l3proto = PF_INET6, |
| 315 | .proto = IPPROTO_ICMPV6, |
| 316 | .name = "icmpv6", |
| 317 | .pkt_to_tuple = icmpv6_pkt_to_tuple, |
| 318 | .invert_tuple = icmpv6_invert_tuple, |
| 319 | .print_tuple = icmpv6_print_tuple, |
| 320 | .print_conntrack = icmpv6_print_conntrack, |
| 321 | .packet = icmpv6_packet, |
| 322 | .new = icmpv6_new, |
| 323 | .error = icmpv6_error, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 324 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
| 325 | defined(CONFIG_NF_CT_NETLINK_MODULE) |
| 326 | .tuple_to_nfattr = icmpv6_tuple_to_nfattr, |
| 327 | .nfattr_to_tuple = icmpv6_nfattr_to_tuple, |
| 328 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | EXPORT_SYMBOL(nf_conntrack_protocol_icmpv6); |