[CIFS] Missing ; from previous fix. Pointed out by Shaggy.
[linux-2.6/verdex.git] / net / ipv4 / netfilter / ip_nat_proto_tcp.c
bloba98e36d2b3c627d66cec58ece9d2b173c98de71d
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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 */
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>
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)
26 u_int16_t port;
28 if (maniptype == IP_NAT_MANIP_SRC)
29 port = tuple->src.u.tcp.port;
30 else
31 port = tuple->dst.u.tcp.port;
33 return ntohs(port) >= ntohs(min->tcp.port)
34 && ntohs(port) <= ntohs(max->tcp.port);
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)
43 static u_int16_t port;
44 u_int16_t *portptr;
45 unsigned int range_size, min, i;
47 if (maniptype == IP_NAT_MANIP_SRC)
48 portptr = &tuple->src.u.tcp.port;
49 else
50 portptr = &tuple->dst.u.tcp.port;
52 /* If no range specified... */
53 if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) {
54 /* If it's dst rewrite, can't change port */
55 if (maniptype == IP_NAT_MANIP_DST)
56 return 0;
58 /* Map privileged onto privileged. */
59 if (ntohs(*portptr) < 1024) {
60 /* Loose convention: >> 512 is credential passing */
61 if (ntohs(*portptr)<512) {
62 min = 1;
63 range_size = 511 - min + 1;
64 } else {
65 min = 600;
66 range_size = 1023 - min + 1;
68 } else {
69 min = 1024;
70 range_size = 65535 - 1024 + 1;
72 } else {
73 min = ntohs(range->min.tcp.port);
74 range_size = ntohs(range->max.tcp.port) - min + 1;
77 for (i = 0; i < range_size; i++, port++) {
78 *portptr = htons(min + port % range_size);
79 if (!ip_nat_used_tuple(tuple, conntrack)) {
80 return 1;
83 return 0;
86 static int
87 tcp_manip_pkt(struct sk_buff **pskb,
88 unsigned int iphdroff,
89 const struct ip_conntrack_tuple *tuple,
90 enum ip_nat_manip_type maniptype)
92 struct iphdr *iph = (struct iphdr *)((*pskb)->data + iphdroff);
93 struct tcphdr *hdr;
94 unsigned int hdroff = iphdroff + iph->ihl*4;
95 u32 oldip, newip;
96 u16 *portptr, newport, oldport;
97 int hdrsize = 8; /* TCP connection tracking guarantees this much */
99 /* this could be a inner header returned in icmp packet; in such
100 cases we cannot update the checksum field since it is outside of
101 the 8 bytes of transport layer headers we are guaranteed */
102 if ((*pskb)->len >= hdroff + sizeof(struct tcphdr))
103 hdrsize = sizeof(struct tcphdr);
105 if (!skb_ip_make_writable(pskb, hdroff + hdrsize))
106 return 0;
108 iph = (struct iphdr *)((*pskb)->data + iphdroff);
109 hdr = (struct tcphdr *)((*pskb)->data + hdroff);
111 if (maniptype == IP_NAT_MANIP_SRC) {
112 /* Get rid of src ip and src pt */
113 oldip = iph->saddr;
114 newip = tuple->src.ip;
115 newport = tuple->src.u.tcp.port;
116 portptr = &hdr->source;
117 } else {
118 /* Get rid of dst ip and dst pt */
119 oldip = iph->daddr;
120 newip = tuple->dst.ip;
121 newport = tuple->dst.u.tcp.port;
122 portptr = &hdr->dest;
125 oldport = *portptr;
126 *portptr = newport;
128 if (hdrsize < sizeof(*hdr))
129 return 1;
131 hdr->check = ip_nat_cheat_check(~oldip, newip,
132 ip_nat_cheat_check(oldport ^ 0xFFFF,
133 newport,
134 hdr->check));
135 return 1;
138 static unsigned int
139 tcp_print(char *buffer,
140 const struct ip_conntrack_tuple *match,
141 const struct ip_conntrack_tuple *mask)
143 unsigned int len = 0;
145 if (mask->src.u.tcp.port)
146 len += sprintf(buffer + len, "srcpt=%u ",
147 ntohs(match->src.u.tcp.port));
150 if (mask->dst.u.tcp.port)
151 len += sprintf(buffer + len, "dstpt=%u ",
152 ntohs(match->dst.u.tcp.port));
154 return len;
157 static unsigned int
158 tcp_print_range(char *buffer, const struct ip_nat_range *range)
160 if (range->min.tcp.port != 0 || range->max.tcp.port != 0xFFFF) {
161 if (range->min.tcp.port == range->max.tcp.port)
162 return sprintf(buffer, "port %u ",
163 ntohs(range->min.tcp.port));
164 else
165 return sprintf(buffer, "ports %u-%u ",
166 ntohs(range->min.tcp.port),
167 ntohs(range->max.tcp.port));
169 else return 0;
172 struct ip_nat_protocol ip_nat_protocol_tcp
173 = { "TCP", IPPROTO_TCP,
174 tcp_manip_pkt,
175 tcp_in_range,
176 tcp_unique_tuple,
177 tcp_print,
178 tcp_print_range