]>
Commit | Line | Data |
---|---|---|
5b1158e9 JK |
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> | |
bc3b2d7f | 11 | #include <linux/export.h> |
5b1158e9 JK |
12 | #include <linux/ip.h> |
13 | #include <linux/icmp.h> | |
14 | ||
15 | #include <linux/netfilter.h> | |
16 | #include <net/netfilter/nf_nat.h> | |
17 | #include <net/netfilter/nf_nat_core.h> | |
18 | #include <net/netfilter/nf_nat_rule.h> | |
19 | #include <net/netfilter/nf_nat_protocol.h> | |
20 | ||
f2ea825f | 21 | static bool |
5b1158e9 JK |
22 | icmp_in_range(const struct nf_conntrack_tuple *tuple, |
23 | enum nf_nat_manip_type maniptype, | |
24 | const union nf_conntrack_man_proto *min, | |
25 | const union nf_conntrack_man_proto *max) | |
26 | { | |
27 | return ntohs(tuple->src.u.icmp.id) >= ntohs(min->icmp.id) && | |
28 | ntohs(tuple->src.u.icmp.id) <= ntohs(max->icmp.id); | |
29 | } | |
30 | ||
f43dc98b | 31 | static void |
5b1158e9 JK |
32 | icmp_unique_tuple(struct nf_conntrack_tuple *tuple, |
33 | const struct nf_nat_range *range, | |
34 | enum nf_nat_manip_type maniptype, | |
35 | const struct nf_conn *ct) | |
36 | { | |
37 | static u_int16_t id; | |
38 | unsigned int range_size; | |
39 | unsigned int i; | |
40 | ||
41 | range_size = ntohs(range->max.icmp.id) - ntohs(range->min.icmp.id) + 1; | |
42 | /* If no range specified... */ | |
43 | if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) | |
44 | range_size = 0xFFFF; | |
45 | ||
2452a99d | 46 | for (i = 0; ; ++id) { |
5b1158e9 | 47 | tuple->src.u.icmp.id = htons(ntohs(range->min.icmp.id) + |
e905a9ed | 48 | (id % range_size)); |
2452a99d | 49 | if (++i == range_size || !nf_nat_used_tuple(tuple, ct)) |
f43dc98b | 50 | return; |
5b1158e9 | 51 | } |
f43dc98b | 52 | return; |
5b1158e9 JK |
53 | } |
54 | ||
f2ea825f | 55 | static bool |
3db05fea | 56 | icmp_manip_pkt(struct sk_buff *skb, |
5b1158e9 JK |
57 | unsigned int iphdroff, |
58 | const struct nf_conntrack_tuple *tuple, | |
59 | enum nf_nat_manip_type maniptype) | |
60 | { | |
7cc3864d | 61 | const struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff); |
5b1158e9 JK |
62 | struct icmphdr *hdr; |
63 | unsigned int hdroff = iphdroff + iph->ihl*4; | |
64 | ||
3db05fea | 65 | if (!skb_make_writable(skb, hdroff + sizeof(*hdr))) |
f2ea825f | 66 | return false; |
5b1158e9 | 67 | |
3db05fea | 68 | hdr = (struct icmphdr *)(skb->data + hdroff); |
be0ea7d5 PM |
69 | inet_proto_csum_replace2(&hdr->checksum, skb, |
70 | hdr->un.echo.id, tuple->src.u.icmp.id, 0); | |
5b1158e9 | 71 | hdr->un.echo.id = tuple->src.u.icmp.id; |
f2ea825f | 72 | return true; |
5b1158e9 JK |
73 | } |
74 | ||
2b628a08 | 75 | const struct nf_nat_protocol nf_nat_protocol_icmp = { |
5b1158e9 JK |
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, | |
e281db5c | 81 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
535b57c7 PM |
82 | .range_to_nlattr = nf_nat_proto_range_to_nlattr, |
83 | .nlattr_to_range = nf_nat_proto_nlattr_to_range, | |
5b1158e9 JK |
84 | #endif |
85 | }; |