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.
9 #include <linux/types.h>
10 #include <linux/init.h>
11 #include <linux/netfilter.h>
13 #include <linux/tcp.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>
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)
28 if (maniptype == IP_NAT_MANIP_SRC)
29 port = tuple->src.u.tcp.port;
31 port = tuple->dst.u.tcp.port;
33 return ntohs(port) >= ntohs(min->tcp.port)
34 && ntohs(port) <= ntohs(max->tcp.port);
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, *portptr;
44 unsigned int range_size, min, i;
46 if (maniptype == IP_NAT_MANIP_SRC)
47 portptr = &tuple->src.u.tcp.port;
49 portptr = &tuple->dst.u.tcp.port;
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)
57 /* Map privileged onto privileged. */
58 if (ntohs(*portptr) < 1024) {
59 /* Loose convention: >> 512 is credential passing */
60 if (ntohs(*portptr)<512) {
62 range_size = 511 - min + 1;
65 range_size = 1023 - min + 1;
69 range_size = 65535 - 1024 + 1;
72 min = ntohs(range->min.tcp.port);
73 range_size = ntohs(range->max.tcp.port) - min + 1;
76 for (i = 0; i < range_size; i++, port++) {
77 *portptr = htons(min + port % range_size);
78 if (!ip_nat_used_tuple(tuple, conntrack)) {
86 tcp_manip_pkt(struct sk_buff **pskb,
88 const struct ip_conntrack_manip *manip,
89 enum ip_nat_manip_type maniptype)
93 u_int16_t *portptr, oldport;
94 int hdrsize = 8; /* TCP connection tracking guarantees this much */
96 /* this could be a inner header returned in icmp packet; in such
97 cases we cannot update the checksum field since it is outside of
98 the 8 bytes of transport layer headers we are guaranteed */
99 if ((*pskb)->len >= hdroff + sizeof(struct tcphdr))
100 hdrsize = sizeof(struct tcphdr);
102 if (!skb_ip_make_writable(pskb, hdroff + hdrsize))
105 hdr = (void *)(*pskb)->data + hdroff;
107 if (maniptype == IP_NAT_MANIP_SRC) {
108 /* Get rid of src ip and src pt */
109 oldip = (*pskb)->nh.iph->saddr;
110 portptr = &hdr->source;
112 /* Get rid of dst ip and dst pt */
113 oldip = (*pskb)->nh.iph->daddr;
114 portptr = &hdr->dest;
118 *portptr = manip->u.tcp.port;
120 if (hdrsize < sizeof(*hdr))
123 hdr->check = ip_nat_cheat_check(~oldip, manip->ip,
124 ip_nat_cheat_check(oldport ^ 0xFFFF,
131 tcp_print(char *buffer,
132 const struct ip_conntrack_tuple *match,
133 const struct ip_conntrack_tuple *mask)
135 unsigned int len = 0;
137 if (mask->src.u.tcp.port)
138 len += sprintf(buffer + len, "srcpt=%u ",
139 ntohs(match->src.u.tcp.port));
142 if (mask->dst.u.tcp.port)
143 len += sprintf(buffer + len, "dstpt=%u ",
144 ntohs(match->dst.u.tcp.port));
150 tcp_print_range(char *buffer, const struct ip_nat_range *range)
152 if (range->min.tcp.port != 0 || range->max.tcp.port != 0xFFFF) {
153 if (range->min.tcp.port == range->max.tcp.port)
154 return sprintf(buffer, "port %u ",
155 ntohs(range->min.tcp.port));
157 return sprintf(buffer, "ports %u-%u ",
158 ntohs(range->min.tcp.port),
159 ntohs(range->max.tcp.port));
164 struct ip_nat_protocol ip_nat_protocol_tcp
165 = { { NULL, NULL }, "TCP", IPPROTO_TCP,