Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 1 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 2 | * (C) 2002-2006 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/init.h> |
| 11 | #include <linux/ip.h> |
| 12 | #include <linux/icmp.h> |
| 13 | |
| 14 | #include <linux/netfilter.h> |
| 15 | #include <net/netfilter/nf_nat.h> |
| 16 | #include <net/netfilter/nf_nat_core.h> |
| 17 | #include <net/netfilter/nf_nat_rule.h> |
| 18 | #include <net/netfilter/nf_nat_protocol.h> |
| 19 | |
| 20 | static int |
| 21 | icmp_in_range(const struct nf_conntrack_tuple *tuple, |
| 22 | enum nf_nat_manip_type maniptype, |
| 23 | const union nf_conntrack_man_proto *min, |
| 24 | const union nf_conntrack_man_proto *max) |
| 25 | { |
| 26 | return ntohs(tuple->src.u.icmp.id) >= ntohs(min->icmp.id) && |
| 27 | ntohs(tuple->src.u.icmp.id) <= ntohs(max->icmp.id); |
| 28 | } |
| 29 | |
| 30 | static int |
| 31 | icmp_unique_tuple(struct nf_conntrack_tuple *tuple, |
| 32 | const struct nf_nat_range *range, |
| 33 | enum nf_nat_manip_type maniptype, |
| 34 | const struct nf_conn *ct) |
| 35 | { |
| 36 | static u_int16_t id; |
| 37 | unsigned int range_size; |
| 38 | unsigned int i; |
| 39 | |
| 40 | range_size = ntohs(range->max.icmp.id) - ntohs(range->min.icmp.id) + 1; |
| 41 | /* If no range specified... */ |
| 42 | if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) |
| 43 | range_size = 0xFFFF; |
| 44 | |
| 45 | for (i = 0; i < range_size; i++, id++) { |
| 46 | tuple->src.u.icmp.id = htons(ntohs(range->min.icmp.id) + |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 47 | (id % range_size)); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 48 | if (!nf_nat_used_tuple(tuple, ct)) |
| 49 | return 1; |
| 50 | } |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame^] | 55 | icmp_manip_pkt(struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 56 | unsigned int iphdroff, |
| 57 | const struct nf_conntrack_tuple *tuple, |
| 58 | enum nf_nat_manip_type maniptype) |
| 59 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame^] | 60 | struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 61 | struct icmphdr *hdr; |
| 62 | unsigned int hdroff = iphdroff + iph->ihl*4; |
| 63 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame^] | 64 | if (!skb_make_writable(skb, hdroff + sizeof(*hdr))) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 65 | return 0; |
| 66 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame^] | 67 | hdr = (struct icmphdr *)(skb->data + hdroff); |
| 68 | nf_proto_csum_replace2(&hdr->checksum, skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 69 | hdr->un.echo.id, tuple->src.u.icmp.id, 0); |
| 70 | hdr->un.echo.id = tuple->src.u.icmp.id; |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | struct nf_nat_protocol nf_nat_protocol_icmp = { |
| 75 | .name = "ICMP", |
| 76 | .protonum = IPPROTO_ICMP, |
| 77 | .me = THIS_MODULE, |
| 78 | .manip_pkt = icmp_manip_pkt, |
| 79 | .in_range = icmp_in_range, |
| 80 | .unique_tuple = icmp_unique_tuple, |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 81 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 82 | .range_to_nlattr = nf_nat_port_range_to_nlattr, |
| 83 | .nlattr_to_range = nf_nat_port_nlattr_to_range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 84 | #endif |
| 85 | }; |