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