ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / ipv4 / netfilter / ip_nat_proto_udp.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/udp.h>
14 #include <linux/if.h>
15
16 #include <linux/netfilter_ipv4/ip_nat.h>
17 #include <linux/netfilter_ipv4/ip_nat_core.h>
18 #include <linux/netfilter_ipv4/ip_nat_rule.h>
19 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
20
21 static int
22 udp_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.udp.port;
31         else
32                 port = tuple->dst.u.udp.port;
33
34         return ntohs(port) >= ntohs(min->udp.port)
35                 && ntohs(port) <= ntohs(max->udp.port);
36 }
37
38 static int
39 udp_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, *portptr;
45         unsigned int range_size, min, i;
46
47         if (maniptype == IP_NAT_MANIP_SRC)
48                 portptr = &tuple->src.u.udp.port;
49         else
50                 portptr = &tuple->dst.u.udp.port;
51
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;
57
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.udp.port);
73                 range_size = ntohs(range->max.udp.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         return 0;
82 }
83
84 static int
85 udp_manip_pkt(struct sk_buff **pskb,
86               unsigned int hdroff,
87               const struct ip_conntrack_manip *manip,
88               enum ip_nat_manip_type maniptype)
89 {
90         struct udphdr *hdr;
91         u_int32_t oldip;
92         u_int16_t *portptr;
93
94         if (!skb_ip_make_writable(pskb, hdroff + sizeof(hdr)))
95                 return 0;
96
97         hdr = (void *)(*pskb)->data + hdroff;
98         if (maniptype == IP_NAT_MANIP_SRC) {
99                 /* Get rid of src ip and src pt */
100                 oldip = (*pskb)->nh.iph->saddr;
101                 portptr = &hdr->source;
102         } else {
103                 /* Get rid of dst ip and dst pt */
104                 oldip = (*pskb)->nh.iph->daddr;
105                 portptr = &hdr->dest;
106         }
107         if (hdr->check) /* 0 is a special case meaning no checksum */
108                 hdr->check = ip_nat_cheat_check(~oldip, manip->ip,
109                                         ip_nat_cheat_check(*portptr ^ 0xFFFF,
110                                                            manip->u.udp.port,
111                                                            hdr->check));
112         *portptr = manip->u.udp.port;
113         return 1;
114 }
115
116 static unsigned int
117 udp_print(char *buffer,
118           const struct ip_conntrack_tuple *match,
119           const struct ip_conntrack_tuple *mask)
120 {
121         unsigned int len = 0;
122
123         if (mask->src.u.udp.port)
124                 len += sprintf(buffer + len, "srcpt=%u ",
125                                ntohs(match->src.u.udp.port));
126
127
128         if (mask->dst.u.udp.port)
129                 len += sprintf(buffer + len, "dstpt=%u ",
130                                ntohs(match->dst.u.udp.port));
131
132         return len;
133 }
134
135 static unsigned int
136 udp_print_range(char *buffer, const struct ip_nat_range *range)
137 {
138         if (range->min.udp.port != 0 || range->max.udp.port != 0xFFFF) {
139                 if (range->min.udp.port == range->max.udp.port)
140                         return sprintf(buffer, "port %u ",
141                                        ntohs(range->min.udp.port));
142                 else
143                         return sprintf(buffer, "ports %u-%u ",
144                                        ntohs(range->min.udp.port),
145                                        ntohs(range->max.udp.port));
146         }
147         else return 0;
148 }
149
150 struct ip_nat_protocol ip_nat_protocol_udp
151 = { { NULL, NULL }, "UDP", IPPROTO_UDP,
152     udp_manip_pkt,
153     udp_in_range,
154     udp_unique_tuple,
155     udp_print,
156     udp_print_range
157 };