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/init.h> |
| 11 | #include <linux/netfilter.h> |
| 12 | #include <linux/ip.h> |
| 13 | #include <linux/tcp.h> |
| 14 | #include <linux/if.h> |
| 15 | #include <linux/netfilter_ipv4/ip_nat.h> |
| 16 | #include <linux/netfilter_ipv4/ip_nat_rule.h> |
| 17 | #include <linux/netfilter_ipv4/ip_nat_protocol.h> |
| 18 | #include <linux/netfilter_ipv4/ip_nat_core.h> |
| 19 | |
| 20 | static int |
| 21 | tcp_in_range(const struct ip_conntrack_tuple *tuple, |
| 22 | enum ip_nat_manip_type maniptype, |
| 23 | const union ip_conntrack_manip_proto *min, |
| 24 | const union ip_conntrack_manip_proto *max) |
| 25 | { |
| 26 | u_int16_t port; |
| 27 | |
| 28 | if (maniptype == IP_NAT_MANIP_SRC) |
| 29 | port = tuple->src.u.tcp.port; |
| 30 | else |
| 31 | port = tuple->dst.u.tcp.port; |
| 32 | |
| 33 | return ntohs(port) >= ntohs(min->tcp.port) |
| 34 | && ntohs(port) <= ntohs(max->tcp.port); |
| 35 | } |
| 36 | |
| 37 | static int |
| 38 | tcp_unique_tuple(struct ip_conntrack_tuple *tuple, |
| 39 | const struct ip_nat_range *range, |
| 40 | enum ip_nat_manip_type maniptype, |
| 41 | const struct ip_conntrack *conntrack) |
| 42 | { |
| 43 | static u_int16_t port, *portptr; |
| 44 | unsigned int range_size, min, i; |
| 45 | |
| 46 | if (maniptype == IP_NAT_MANIP_SRC) |
| 47 | portptr = &tuple->src.u.tcp.port; |
| 48 | else |
| 49 | portptr = &tuple->dst.u.tcp.port; |
| 50 | |
| 51 | /* If no range specified... */ |
| 52 | if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) { |
| 53 | /* If it's dst rewrite, can't change port */ |
| 54 | if (maniptype == IP_NAT_MANIP_DST) |
| 55 | return 0; |
| 56 | |
| 57 | /* Map privileged onto privileged. */ |
| 58 | if (ntohs(*portptr) < 1024) { |
| 59 | /* Loose convention: >> 512 is credential passing */ |
| 60 | if (ntohs(*portptr)<512) { |
| 61 | min = 1; |
| 62 | range_size = 511 - min + 1; |
| 63 | } else { |
| 64 | min = 600; |
| 65 | range_size = 1023 - min + 1; |
| 66 | } |
| 67 | } else { |
| 68 | min = 1024; |
| 69 | range_size = 65535 - 1024 + 1; |
| 70 | } |
| 71 | } else { |
| 72 | min = ntohs(range->min.tcp.port); |
| 73 | range_size = ntohs(range->max.tcp.port) - min + 1; |
| 74 | } |
| 75 | |
| 76 | for (i = 0; i < range_size; i++, port++) { |
| 77 | *portptr = htons(min + port % range_size); |
| 78 | if (!ip_nat_used_tuple(tuple, conntrack)) { |
| 79 | return 1; |
| 80 | } |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int |
| 86 | tcp_manip_pkt(struct sk_buff **pskb, |
| 87 | unsigned int iphdroff, |
| 88 | const struct ip_conntrack_tuple *tuple, |
| 89 | enum ip_nat_manip_type maniptype) |
| 90 | { |
| 91 | struct iphdr *iph = (struct iphdr *)((*pskb)->data + iphdroff); |
| 92 | struct tcphdr *hdr; |
| 93 | unsigned int hdroff = iphdroff + iph->ihl*4; |
| 94 | u32 oldip, newip; |
| 95 | u16 *portptr, newport, oldport; |
| 96 | int hdrsize = 8; /* TCP connection tracking guarantees this much */ |
| 97 | |
| 98 | /* this could be a inner header returned in icmp packet; in such |
| 99 | cases we cannot update the checksum field since it is outside of |
| 100 | the 8 bytes of transport layer headers we are guaranteed */ |
| 101 | if ((*pskb)->len >= hdroff + sizeof(struct tcphdr)) |
| 102 | hdrsize = sizeof(struct tcphdr); |
| 103 | |
| 104 | if (!skb_ip_make_writable(pskb, hdroff + hdrsize)) |
| 105 | return 0; |
| 106 | |
| 107 | iph = (struct iphdr *)((*pskb)->data + iphdroff); |
| 108 | hdr = (struct tcphdr *)((*pskb)->data + hdroff); |
| 109 | |
| 110 | if (maniptype == IP_NAT_MANIP_SRC) { |
| 111 | /* Get rid of src ip and src pt */ |
| 112 | oldip = iph->saddr; |
| 113 | newip = tuple->src.ip; |
| 114 | newport = tuple->src.u.tcp.port; |
| 115 | portptr = &hdr->source; |
| 116 | } else { |
| 117 | /* Get rid of dst ip and dst pt */ |
| 118 | oldip = iph->daddr; |
| 119 | newip = tuple->dst.ip; |
| 120 | newport = tuple->dst.u.tcp.port; |
| 121 | portptr = &hdr->dest; |
| 122 | } |
| 123 | |
| 124 | oldport = *portptr; |
| 125 | *portptr = newport; |
| 126 | |
| 127 | if (hdrsize < sizeof(*hdr)) |
| 128 | return 1; |
| 129 | |
| 130 | hdr->check = ip_nat_cheat_check(~oldip, newip, |
| 131 | ip_nat_cheat_check(oldport ^ 0xFFFF, |
| 132 | newport, |
| 133 | hdr->check)); |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | static unsigned int |
| 138 | tcp_print(char *buffer, |
| 139 | const struct ip_conntrack_tuple *match, |
| 140 | const struct ip_conntrack_tuple *mask) |
| 141 | { |
| 142 | unsigned int len = 0; |
| 143 | |
| 144 | if (mask->src.u.tcp.port) |
| 145 | len += sprintf(buffer + len, "srcpt=%u ", |
| 146 | ntohs(match->src.u.tcp.port)); |
| 147 | |
| 148 | |
| 149 | if (mask->dst.u.tcp.port) |
| 150 | len += sprintf(buffer + len, "dstpt=%u ", |
| 151 | ntohs(match->dst.u.tcp.port)); |
| 152 | |
| 153 | return len; |
| 154 | } |
| 155 | |
| 156 | static unsigned int |
| 157 | tcp_print_range(char *buffer, const struct ip_nat_range *range) |
| 158 | { |
| 159 | if (range->min.tcp.port != 0 || range->max.tcp.port != 0xFFFF) { |
| 160 | if (range->min.tcp.port == range->max.tcp.port) |
| 161 | return sprintf(buffer, "port %u ", |
| 162 | ntohs(range->min.tcp.port)); |
| 163 | else |
| 164 | return sprintf(buffer, "ports %u-%u ", |
| 165 | ntohs(range->min.tcp.port), |
| 166 | ntohs(range->max.tcp.port)); |
| 167 | } |
| 168 | else return 0; |
| 169 | } |
| 170 | |
| 171 | struct ip_nat_protocol ip_nat_protocol_tcp |
| 172 | = { "TCP", IPPROTO_TCP, |
| 173 | tcp_manip_pkt, |
| 174 | tcp_in_range, |
| 175 | tcp_unique_tuple, |
| 176 | tcp_print, |
| 177 | tcp_print_range |
| 178 | }; |