Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> |
| 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 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/timer.h> |
| 12 | #include <linux/netfilter.h> |
| 13 | #include <linux/in.h> |
| 14 | #include <linux/icmp.h> |
| 15 | #include <linux/seq_file.h> |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 16 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <net/ip.h> |
| 18 | #include <net/checksum.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/netfilter_ipv4.h> |
| 20 | #include <linux/netfilter_ipv4/ip_conntrack.h> |
| 21 | #include <linux/netfilter_ipv4/ip_conntrack_core.h> |
| 22 | #include <linux/netfilter_ipv4/ip_conntrack_protocol.h> |
| 23 | |
Patrick McHardy | babbdb1 | 2006-01-09 17:48:09 -0800 | [diff] [blame] | 24 | unsigned int ip_ct_icmp_timeout = 30*HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #if 0 |
| 27 | #define DEBUGP printk |
| 28 | #else |
| 29 | #define DEBUGP(format, args...) |
| 30 | #endif |
| 31 | |
| 32 | static int icmp_pkt_to_tuple(const struct sk_buff *skb, |
| 33 | unsigned int dataoff, |
| 34 | struct ip_conntrack_tuple *tuple) |
| 35 | { |
| 36 | struct icmphdr _hdr, *hp; |
| 37 | |
| 38 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); |
| 39 | if (hp == NULL) |
| 40 | return 0; |
| 41 | |
| 42 | tuple->dst.u.icmp.type = hp->type; |
| 43 | tuple->src.u.icmp.id = hp->un.echo.id; |
| 44 | tuple->dst.u.icmp.code = hp->code; |
| 45 | |
| 46 | return 1; |
| 47 | } |
| 48 | |
Yasuyuki Kozakai | 90c4656 | 2006-01-05 12:17:03 -0800 | [diff] [blame] | 49 | /* Add 1; spaces filled with 0. */ |
| 50 | static const u_int8_t invmap[] = { |
| 51 | [ICMP_ECHO] = ICMP_ECHOREPLY + 1, |
| 52 | [ICMP_ECHOREPLY] = ICMP_ECHO + 1, |
| 53 | [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1, |
| 54 | [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1, |
| 55 | [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1, |
| 56 | [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1, |
| 57 | [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1, |
| 58 | [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1 |
| 59 | }; |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | static int icmp_invert_tuple(struct ip_conntrack_tuple *tuple, |
| 62 | const struct ip_conntrack_tuple *orig) |
| 63 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | if (orig->dst.u.icmp.type >= sizeof(invmap) |
| 65 | || !invmap[orig->dst.u.icmp.type]) |
| 66 | return 0; |
| 67 | |
| 68 | tuple->src.u.icmp.id = orig->src.u.icmp.id; |
| 69 | tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1; |
| 70 | tuple->dst.u.icmp.code = orig->dst.u.icmp.code; |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | /* Print out the per-protocol part of the tuple. */ |
| 75 | static int icmp_print_tuple(struct seq_file *s, |
| 76 | const struct ip_conntrack_tuple *tuple) |
| 77 | { |
| 78 | return seq_printf(s, "type=%u code=%u id=%u ", |
| 79 | tuple->dst.u.icmp.type, |
| 80 | tuple->dst.u.icmp.code, |
| 81 | ntohs(tuple->src.u.icmp.id)); |
| 82 | } |
| 83 | |
| 84 | /* Print out the private part of the conntrack. */ |
| 85 | static int icmp_print_conntrack(struct seq_file *s, |
| 86 | const struct ip_conntrack *conntrack) |
| 87 | { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* Returns verdict for packet, or -1 for invalid. */ |
| 92 | static int icmp_packet(struct ip_conntrack *ct, |
| 93 | const struct sk_buff *skb, |
| 94 | enum ip_conntrack_info ctinfo) |
| 95 | { |
| 96 | /* Try to delete connection immediately after all replies: |
| 97 | won't actually vanish as we still have skb, and del_timer |
| 98 | means this will only run once even if count hits zero twice |
| 99 | (theoretically possible with SMP) */ |
| 100 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) { |
| 101 | if (atomic_dec_and_test(&ct->proto.icmp.count) |
| 102 | && del_timer(&ct->timeout)) |
| 103 | ct->timeout.function((unsigned long)ct); |
| 104 | } else { |
| 105 | atomic_inc(&ct->proto.icmp.count); |
Harald Welte | ac3247b | 2005-08-09 19:28:03 -0700 | [diff] [blame] | 106 | ip_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | ip_ct_refresh_acct(ct, ctinfo, skb, ip_ct_icmp_timeout); |
| 108 | } |
| 109 | |
| 110 | return NF_ACCEPT; |
| 111 | } |
| 112 | |
| 113 | /* Called when a new connection for this protocol found. */ |
| 114 | static int icmp_new(struct ip_conntrack *conntrack, |
| 115 | const struct sk_buff *skb) |
| 116 | { |
Yasuyuki Kozakai | 90c4656 | 2006-01-05 12:17:03 -0800 | [diff] [blame] | 117 | static const u_int8_t valid_new[] = { |
| 118 | [ICMP_ECHO] = 1, |
| 119 | [ICMP_TIMESTAMP] = 1, |
| 120 | [ICMP_INFO_REQUEST] = 1, |
| 121 | [ICMP_ADDRESS] = 1 |
| 122 | }; |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) |
| 125 | || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) { |
| 126 | /* Can't create a new ICMP `conn' with this. */ |
| 127 | DEBUGP("icmp: can't create new conn with type %u\n", |
| 128 | conntrack->tuplehash[0].tuple.dst.u.icmp.type); |
| 129 | DUMP_TUPLE(&conntrack->tuplehash[0].tuple); |
| 130 | return 0; |
| 131 | } |
| 132 | atomic_set(&conntrack->proto.icmp.count, 0); |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | static int |
| 137 | icmp_error_message(struct sk_buff *skb, |
| 138 | enum ip_conntrack_info *ctinfo, |
| 139 | unsigned int hooknum) |
| 140 | { |
| 141 | struct ip_conntrack_tuple innertuple, origtuple; |
| 142 | struct { |
| 143 | struct icmphdr icmp; |
| 144 | struct iphdr ip; |
| 145 | } _in, *inside; |
| 146 | struct ip_conntrack_protocol *innerproto; |
| 147 | struct ip_conntrack_tuple_hash *h; |
| 148 | int dataoff; |
| 149 | |
| 150 | IP_NF_ASSERT(skb->nfct == NULL); |
| 151 | |
| 152 | /* Not enough header? */ |
| 153 | inside = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_in), &_in); |
| 154 | if (inside == NULL) |
Yasuyuki Kozakai | d63a928 | 2005-11-09 13:02:45 -0800 | [diff] [blame] | 155 | return -NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
| 157 | /* Ignore ICMP's containing fragments (shouldn't happen) */ |
| 158 | if (inside->ip.frag_off & htons(IP_OFFSET)) { |
| 159 | DEBUGP("icmp_error_track: fragment of proto %u\n", |
| 160 | inside->ip.protocol); |
Yasuyuki Kozakai | d63a928 | 2005-11-09 13:02:45 -0800 | [diff] [blame] | 161 | return -NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 164 | innerproto = ip_conntrack_proto_find_get(inside->ip.protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | dataoff = skb->nh.iph->ihl*4 + sizeof(inside->icmp) + inside->ip.ihl*4; |
| 166 | /* Are they talking about one of our connections? */ |
| 167 | if (!ip_ct_get_tuple(&inside->ip, skb, dataoff, &origtuple, innerproto)) { |
| 168 | DEBUGP("icmp_error: ! get_tuple p=%u", inside->ip.protocol); |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 169 | ip_conntrack_proto_put(innerproto); |
Yasuyuki Kozakai | d63a928 | 2005-11-09 13:02:45 -0800 | [diff] [blame] | 170 | return -NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* Ordinarily, we'd expect the inverted tupleproto, but it's |
| 174 | been preserved inside the ICMP. */ |
| 175 | if (!ip_ct_invert_tuple(&innertuple, &origtuple, innerproto)) { |
| 176 | DEBUGP("icmp_error_track: Can't invert tuple\n"); |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 177 | ip_conntrack_proto_put(innerproto); |
Yasuyuki Kozakai | d63a928 | 2005-11-09 13:02:45 -0800 | [diff] [blame] | 178 | return -NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 180 | ip_conntrack_proto_put(innerproto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | *ctinfo = IP_CT_RELATED; |
| 183 | |
| 184 | h = ip_conntrack_find_get(&innertuple, NULL); |
| 185 | if (!h) { |
| 186 | /* Locally generated ICMPs will match inverted if they |
| 187 | haven't been SNAT'ed yet */ |
| 188 | /* FIXME: NAT code has to handle half-done double NAT --RR */ |
| 189 | if (hooknum == NF_IP_LOCAL_OUT) |
| 190 | h = ip_conntrack_find_get(&origtuple, NULL); |
| 191 | |
| 192 | if (!h) { |
| 193 | DEBUGP("icmp_error_track: no match\n"); |
Yasuyuki Kozakai | d63a928 | 2005-11-09 13:02:45 -0800 | [diff] [blame] | 194 | return -NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | /* Reverse direction from that found */ |
| 197 | if (DIRECTION(h) != IP_CT_DIR_REPLY) |
| 198 | *ctinfo += IP_CT_IS_REPLY; |
| 199 | } else { |
| 200 | if (DIRECTION(h) == IP_CT_DIR_REPLY) |
| 201 | *ctinfo += IP_CT_IS_REPLY; |
| 202 | } |
| 203 | |
| 204 | /* Update skb to refer to this connection */ |
| 205 | skb->nfct = &tuplehash_to_ctrack(h)->ct_general; |
| 206 | skb->nfctinfo = *ctinfo; |
| 207 | return -NF_ACCEPT; |
| 208 | } |
| 209 | |
| 210 | /* Small and modified version of icmp_rcv */ |
| 211 | static int |
| 212 | icmp_error(struct sk_buff *skb, enum ip_conntrack_info *ctinfo, |
| 213 | unsigned int hooknum) |
| 214 | { |
| 215 | struct icmphdr _ih, *icmph; |
| 216 | |
| 217 | /* Not enough header? */ |
| 218 | icmph = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_ih), &_ih); |
| 219 | if (icmph == NULL) { |
| 220 | if (LOG_INVALID(IPPROTO_ICMP)) |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 221 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | "ip_ct_icmp: short packet "); |
| 223 | return -NF_ACCEPT; |
| 224 | } |
| 225 | |
| 226 | /* See ip_conntrack_proto_tcp.c */ |
| 227 | if (hooknum != NF_IP_PRE_ROUTING) |
| 228 | goto checksum_skipped; |
| 229 | |
| 230 | switch (skb->ip_summed) { |
| 231 | case CHECKSUM_HW: |
| 232 | if (!(u16)csum_fold(skb->csum)) |
| 233 | break; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 234 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | case CHECKSUM_NONE: |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 236 | skb->csum = 0; |
| 237 | if (__skb_checksum_complete(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (LOG_INVALID(IPPROTO_ICMP)) |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 239 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | "ip_ct_icmp: bad ICMP checksum "); |
| 241 | return -NF_ACCEPT; |
| 242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | checksum_skipped: |
| 246 | /* |
| 247 | * 18 is the highest 'known' ICMP type. Anything else is a mystery |
| 248 | * |
| 249 | * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently |
| 250 | * discarded. |
| 251 | */ |
| 252 | if (icmph->type > NR_ICMP_TYPES) { |
| 253 | if (LOG_INVALID(IPPROTO_ICMP)) |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 254 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | "ip_ct_icmp: invalid ICMP type "); |
| 256 | return -NF_ACCEPT; |
| 257 | } |
| 258 | |
| 259 | /* Need to track icmp error message? */ |
| 260 | if (icmph->type != ICMP_DEST_UNREACH |
| 261 | && icmph->type != ICMP_SOURCE_QUENCH |
| 262 | && icmph->type != ICMP_TIME_EXCEEDED |
| 263 | && icmph->type != ICMP_PARAMETERPROB |
| 264 | && icmph->type != ICMP_REDIRECT) |
| 265 | return NF_ACCEPT; |
| 266 | |
| 267 | return icmp_error_message(skb, ctinfo, hooknum); |
| 268 | } |
| 269 | |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 270 | #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \ |
| 271 | defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE) |
| 272 | static int icmp_tuple_to_nfattr(struct sk_buff *skb, |
| 273 | const struct ip_conntrack_tuple *t) |
| 274 | { |
| 275 | NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t), |
| 276 | &t->src.u.icmp.id); |
| 277 | NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t), |
| 278 | &t->dst.u.icmp.type); |
| 279 | NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t), |
| 280 | &t->dst.u.icmp.code); |
| 281 | |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 282 | return 0; |
| 283 | |
| 284 | nfattr_failure: |
| 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | static int icmp_nfattr_to_tuple(struct nfattr *tb[], |
| 289 | struct ip_conntrack_tuple *tuple) |
| 290 | { |
| 291 | if (!tb[CTA_PROTO_ICMP_TYPE-1] |
Krzysztof Piotr Oledzki | 439a999 | 2005-11-09 13:04:08 -0800 | [diff] [blame] | 292 | || !tb[CTA_PROTO_ICMP_CODE-1] |
| 293 | || !tb[CTA_PROTO_ICMP_ID-1]) |
Yasuyuki Kozakai | 90c4656 | 2006-01-05 12:17:03 -0800 | [diff] [blame] | 294 | return -EINVAL; |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 295 | |
| 296 | tuple->dst.u.icmp.type = |
| 297 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]); |
| 298 | tuple->dst.u.icmp.code = |
| 299 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]); |
| 300 | tuple->src.u.icmp.id = |
Krzysztof Piotr Oledzki | 5fd52fe | 2005-11-09 13:04:32 -0800 | [diff] [blame] | 301 | *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]); |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 302 | |
Yasuyuki Kozakai | 90c4656 | 2006-01-05 12:17:03 -0800 | [diff] [blame] | 303 | if (tuple->dst.u.icmp.type >= sizeof(invmap) |
| 304 | || !invmap[tuple->dst.u.icmp.type]) |
| 305 | return -EINVAL; |
| 306 | |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | #endif |
| 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | struct ip_conntrack_protocol ip_conntrack_protocol_icmp = |
| 312 | { |
| 313 | .proto = IPPROTO_ICMP, |
| 314 | .name = "icmp", |
| 315 | .pkt_to_tuple = icmp_pkt_to_tuple, |
| 316 | .invert_tuple = icmp_invert_tuple, |
| 317 | .print_tuple = icmp_print_tuple, |
| 318 | .print_conntrack = icmp_print_conntrack, |
| 319 | .packet = icmp_packet, |
| 320 | .new = icmp_new, |
| 321 | .error = icmp_error, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 322 | #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \ |
| 323 | defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE) |
| 324 | .tuple_to_nfattr = icmp_tuple_to_nfattr, |
| 325 | .nfattr_to_tuple = icmp_nfattr_to_tuple, |
| 326 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | }; |