Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [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 | * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> |
| 9 | * - enable working with Layer 3 protocol independent connection tracking. |
| 10 | * |
| 11 | * Derived from net/ipv4/netfilter/ip_conntrack_proto_icmp.c |
| 12 | */ |
| 13 | |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/timer.h> |
| 17 | #include <linux/netfilter.h> |
| 18 | #include <linux/in.h> |
| 19 | #include <linux/icmp.h> |
| 20 | #include <linux/seq_file.h> |
| 21 | #include <net/ip.h> |
| 22 | #include <net/checksum.h> |
| 23 | #include <linux/netfilter_ipv4.h> |
| 24 | #include <net/netfilter/nf_conntrack_tuple.h> |
| 25 | #include <net/netfilter/nf_conntrack_protocol.h> |
| 26 | #include <net/netfilter/nf_conntrack_core.h> |
| 27 | |
| 28 | unsigned long nf_ct_icmp_timeout = 30*HZ; |
| 29 | |
| 30 | #if 0 |
| 31 | #define DEBUGP printk |
| 32 | #else |
| 33 | #define DEBUGP(format, args...) |
| 34 | #endif |
| 35 | |
| 36 | static int icmp_pkt_to_tuple(const struct sk_buff *skb, |
| 37 | unsigned int dataoff, |
| 38 | struct nf_conntrack_tuple *tuple) |
| 39 | { |
| 40 | struct icmphdr _hdr, *hp; |
| 41 | |
| 42 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); |
| 43 | if (hp == NULL) |
| 44 | return 0; |
| 45 | |
| 46 | tuple->dst.u.icmp.type = hp->type; |
| 47 | tuple->src.u.icmp.id = hp->un.echo.id; |
| 48 | tuple->dst.u.icmp.code = hp->code; |
| 49 | |
| 50 | return 1; |
| 51 | } |
| 52 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 53 | /* Add 1; spaces filled with 0. */ |
| 54 | static const u_int8_t invmap[] = { |
| 55 | [ICMP_ECHO] = ICMP_ECHOREPLY + 1, |
| 56 | [ICMP_ECHOREPLY] = ICMP_ECHO + 1, |
| 57 | [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1, |
| 58 | [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1, |
| 59 | [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1, |
| 60 | [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1, |
| 61 | [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1, |
| 62 | [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1 |
| 63 | }; |
| 64 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 65 | static int icmp_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 66 | const struct nf_conntrack_tuple *orig) |
| 67 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 68 | if (orig->dst.u.icmp.type >= sizeof(invmap) |
| 69 | || !invmap[orig->dst.u.icmp.type]) |
| 70 | return 0; |
| 71 | |
| 72 | tuple->src.u.icmp.id = orig->src.u.icmp.id; |
| 73 | tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1; |
| 74 | tuple->dst.u.icmp.code = orig->dst.u.icmp.code; |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | /* Print out the per-protocol part of the tuple. */ |
| 79 | static int icmp_print_tuple(struct seq_file *s, |
| 80 | const struct nf_conntrack_tuple *tuple) |
| 81 | { |
| 82 | return seq_printf(s, "type=%u code=%u id=%u ", |
| 83 | tuple->dst.u.icmp.type, |
| 84 | tuple->dst.u.icmp.code, |
| 85 | ntohs(tuple->src.u.icmp.id)); |
| 86 | } |
| 87 | |
| 88 | /* Print out the private part of the conntrack. */ |
| 89 | static int icmp_print_conntrack(struct seq_file *s, |
| 90 | const struct nf_conn *conntrack) |
| 91 | { |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /* Returns verdict for packet, or -1 for invalid. */ |
| 96 | static int icmp_packet(struct nf_conn *ct, |
| 97 | const struct sk_buff *skb, |
| 98 | unsigned int dataoff, |
| 99 | enum ip_conntrack_info ctinfo, |
| 100 | int pf, |
| 101 | unsigned int hooknum) |
| 102 | { |
| 103 | /* Try to delete connection immediately after all replies: |
| 104 | won't actually vanish as we still have skb, and del_timer |
| 105 | means this will only run once even if count hits zero twice |
| 106 | (theoretically possible with SMP) */ |
| 107 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) { |
| 108 | if (atomic_dec_and_test(&ct->proto.icmp.count) |
| 109 | && del_timer(&ct->timeout)) |
| 110 | ct->timeout.function((unsigned long)ct); |
| 111 | } else { |
| 112 | atomic_inc(&ct->proto.icmp.count); |
| 113 | nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb); |
| 114 | nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout); |
| 115 | } |
| 116 | |
| 117 | return NF_ACCEPT; |
| 118 | } |
| 119 | |
| 120 | /* Called when a new connection for this protocol found. */ |
| 121 | static int icmp_new(struct nf_conn *conntrack, |
| 122 | const struct sk_buff *skb, unsigned int dataoff) |
| 123 | { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 124 | static const u_int8_t valid_new[] = { |
| 125 | [ICMP_ECHO] = 1, |
| 126 | [ICMP_TIMESTAMP] = 1, |
| 127 | [ICMP_INFO_REQUEST] = 1, |
| 128 | [ICMP_ADDRESS] = 1 |
| 129 | }; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 130 | |
| 131 | if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) |
| 132 | || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) { |
| 133 | /* Can't create a new ICMP `conn' with this. */ |
| 134 | DEBUGP("icmp: can't create new conn with type %u\n", |
| 135 | conntrack->tuplehash[0].tuple.dst.u.icmp.type); |
| 136 | NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple); |
| 137 | return 0; |
| 138 | } |
| 139 | atomic_set(&conntrack->proto.icmp.count, 0); |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | extern struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4; |
| 144 | /* Returns conntrack if it dealt with ICMP, and filled in skb fields */ |
| 145 | static int |
| 146 | icmp_error_message(struct sk_buff *skb, |
| 147 | enum ip_conntrack_info *ctinfo, |
| 148 | unsigned int hooknum) |
| 149 | { |
| 150 | struct nf_conntrack_tuple innertuple, origtuple; |
| 151 | struct { |
| 152 | struct icmphdr icmp; |
| 153 | struct iphdr ip; |
| 154 | } _in, *inside; |
| 155 | struct nf_conntrack_protocol *innerproto; |
| 156 | struct nf_conntrack_tuple_hash *h; |
| 157 | int dataoff; |
| 158 | |
| 159 | NF_CT_ASSERT(skb->nfct == NULL); |
| 160 | |
| 161 | /* Not enough header? */ |
| 162 | inside = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_in), &_in); |
| 163 | if (inside == NULL) |
| 164 | return -NF_ACCEPT; |
| 165 | |
| 166 | /* Ignore ICMP's containing fragments (shouldn't happen) */ |
| 167 | if (inside->ip.frag_off & htons(IP_OFFSET)) { |
| 168 | DEBUGP("icmp_error_message: fragment of proto %u\n", |
| 169 | inside->ip.protocol); |
| 170 | return -NF_ACCEPT; |
| 171 | } |
| 172 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 173 | innerproto = __nf_ct_proto_find(PF_INET, inside->ip.protocol); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 174 | dataoff = skb->nh.iph->ihl*4 + sizeof(inside->icmp); |
| 175 | /* Are they talking about one of our connections? */ |
| 176 | if (!nf_ct_get_tuple(skb, dataoff, dataoff + inside->ip.ihl*4, PF_INET, |
| 177 | inside->ip.protocol, &origtuple, |
| 178 | &nf_conntrack_l3proto_ipv4, innerproto)) { |
| 179 | DEBUGP("icmp_error_message: ! get_tuple p=%u", |
| 180 | inside->ip.protocol); |
| 181 | return -NF_ACCEPT; |
| 182 | } |
| 183 | |
| 184 | /* Ordinarily, we'd expect the inverted tupleproto, but it's |
| 185 | been preserved inside the ICMP. */ |
| 186 | if (!nf_ct_invert_tuple(&innertuple, &origtuple, |
| 187 | &nf_conntrack_l3proto_ipv4, innerproto)) { |
| 188 | DEBUGP("icmp_error_message: no match\n"); |
| 189 | return -NF_ACCEPT; |
| 190 | } |
| 191 | |
| 192 | *ctinfo = IP_CT_RELATED; |
| 193 | |
| 194 | h = nf_conntrack_find_get(&innertuple, NULL); |
| 195 | if (!h) { |
| 196 | /* Locally generated ICMPs will match inverted if they |
| 197 | haven't been SNAT'ed yet */ |
| 198 | /* FIXME: NAT code has to handle half-done double NAT --RR */ |
| 199 | if (hooknum == NF_IP_LOCAL_OUT) |
| 200 | h = nf_conntrack_find_get(&origtuple, NULL); |
| 201 | |
| 202 | if (!h) { |
| 203 | DEBUGP("icmp_error_message: no match\n"); |
| 204 | return -NF_ACCEPT; |
| 205 | } |
| 206 | |
| 207 | /* Reverse direction from that found */ |
| 208 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) |
| 209 | *ctinfo += IP_CT_IS_REPLY; |
| 210 | } else { |
| 211 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) |
| 212 | *ctinfo += IP_CT_IS_REPLY; |
| 213 | } |
| 214 | |
| 215 | /* Update skb to refer to this connection */ |
| 216 | skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general; |
| 217 | skb->nfctinfo = *ctinfo; |
| 218 | return -NF_ACCEPT; |
| 219 | } |
| 220 | |
| 221 | /* Small and modified version of icmp_rcv */ |
| 222 | static int |
| 223 | icmp_error(struct sk_buff *skb, unsigned int dataoff, |
| 224 | enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum) |
| 225 | { |
| 226 | struct icmphdr _ih, *icmph; |
| 227 | |
| 228 | /* Not enough header? */ |
| 229 | icmph = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_ih), &_ih); |
| 230 | if (icmph == NULL) { |
| 231 | if (LOG_INVALID(IPPROTO_ICMP)) |
| 232 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL, |
| 233 | "nf_ct_icmp: short packet "); |
| 234 | return -NF_ACCEPT; |
| 235 | } |
| 236 | |
| 237 | /* See ip_conntrack_proto_tcp.c */ |
Patrick McHardy | 96f6bf8 | 2006-04-06 14:19:24 -0700 | [diff] [blame] | 238 | if (hooknum == NF_IP_PRE_ROUTING && |
| 239 | nf_ip_checksum(skb, hooknum, dataoff, 0)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 240 | if (LOG_INVALID(IPPROTO_ICMP)) |
| 241 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL, |
| 242 | "nf_ct_icmp: bad HW ICMP checksum "); |
| 243 | return -NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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)) |
| 254 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL, |
| 255 | "nf_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 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 270 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
| 271 | defined(CONFIG_NF_CT_NETLINK_MODULE) |
| 272 | |
| 273 | #include <linux/netfilter/nfnetlink.h> |
| 274 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 275 | |
| 276 | static int icmp_tuple_to_nfattr(struct sk_buff *skb, |
| 277 | const struct nf_conntrack_tuple *t) |
| 278 | { |
| 279 | NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t), |
| 280 | &t->src.u.icmp.id); |
| 281 | NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t), |
| 282 | &t->dst.u.icmp.type); |
| 283 | NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t), |
| 284 | &t->dst.u.icmp.code); |
| 285 | |
| 286 | return 0; |
| 287 | |
| 288 | nfattr_failure: |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | static const size_t cta_min_proto[CTA_PROTO_MAX] = { |
| 293 | [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t), |
| 294 | [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t), |
| 295 | [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t) |
| 296 | }; |
| 297 | |
| 298 | static int icmp_nfattr_to_tuple(struct nfattr *tb[], |
| 299 | struct nf_conntrack_tuple *tuple) |
| 300 | { |
| 301 | if (!tb[CTA_PROTO_ICMP_TYPE-1] |
| 302 | || !tb[CTA_PROTO_ICMP_CODE-1] |
| 303 | || !tb[CTA_PROTO_ICMP_ID-1]) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto)) |
| 307 | return -EINVAL; |
| 308 | |
| 309 | tuple->dst.u.icmp.type = |
| 310 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]); |
| 311 | tuple->dst.u.icmp.code = |
| 312 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]); |
| 313 | tuple->src.u.icmp.id = |
| 314 | *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]); |
| 315 | |
| 316 | if (tuple->dst.u.icmp.type >= sizeof(invmap) |
| 317 | || !invmap[tuple->dst.u.icmp.type]) |
| 318 | return -EINVAL; |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | #endif |
| 323 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 324 | struct nf_conntrack_protocol nf_conntrack_protocol_icmp = |
| 325 | { |
| 326 | .list = { NULL, NULL }, |
| 327 | .l3proto = PF_INET, |
| 328 | .proto = IPPROTO_ICMP, |
| 329 | .name = "icmp", |
| 330 | .pkt_to_tuple = icmp_pkt_to_tuple, |
| 331 | .invert_tuple = icmp_invert_tuple, |
| 332 | .print_tuple = icmp_print_tuple, |
| 333 | .print_conntrack = icmp_print_conntrack, |
| 334 | .packet = icmp_packet, |
| 335 | .new = icmp_new, |
| 336 | .error = icmp_error, |
| 337 | .destroy = NULL, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 338 | .me = NULL, |
| 339 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
| 340 | defined(CONFIG_NF_CT_NETLINK_MODULE) |
| 341 | .tuple_to_nfattr = icmp_tuple_to_nfattr, |
| 342 | .nfattr_to_tuple = icmp_nfattr_to_tuple, |
| 343 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | EXPORT_SYMBOL(nf_conntrack_protocol_icmp); |