This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / net / ipv4 / netfilter / nf_nat_proto_udp.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2006 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/ip.h>
12 #include <linux/udp.h>
13
14 #include <linux/netfilter.h>
15 #include <net/netfilter/nf_nat.h>
16 #include <net/netfilter/nf_nat_core.h>
17 #include <net/netfilter/nf_nat_rule.h>
18 #include <net/netfilter/nf_nat_protocol.h>
19
20 static int
21 udp_in_range(const struct nf_conntrack_tuple *tuple,
22              enum nf_nat_manip_type maniptype,
23              const union nf_conntrack_man_proto *min,
24              const union nf_conntrack_man_proto *max)
25 {
26         __be16 port;
27
28         if (maniptype == IP_NAT_MANIP_SRC)
29                 port = tuple->src.u.udp.port;
30         else
31                 port = tuple->dst.u.udp.port;
32
33         return ntohs(port) >= ntohs(min->udp.port) &&
34                ntohs(port) <= ntohs(max->udp.port);
35 }
36
37 static int
38 udp_unique_tuple(struct nf_conntrack_tuple *tuple,
39                  const struct nf_nat_range *range,
40                  enum nf_nat_manip_type maniptype,
41                  const struct nf_conn *ct)
42 {
43         static u_int16_t port;
44         __be16 *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 (!nf_nat_used_tuple(tuple, ct))
79                         return 1;
80         }
81         return 0;
82 }
83
84 static int
85 udp_manip_pkt(struct sk_buff **pskb,
86               unsigned int iphdroff,
87               const struct nf_conntrack_tuple *tuple,
88               enum nf_nat_manip_type maniptype)
89 {
90         struct iphdr *iph = (struct iphdr *)((*pskb)->data + iphdroff);
91         struct udphdr *hdr;
92         unsigned int hdroff = iphdroff + iph->ihl*4;
93         __be32 oldip, newip;
94         __be16 *portptr, newport;
95
96         if (!skb_make_writable(pskb, hdroff + sizeof(*hdr)))
97                 return 0;
98
99         iph = (struct iphdr *)((*pskb)->data + iphdroff);
100         hdr = (struct udphdr *)((*pskb)->data + hdroff);
101
102         if (maniptype == IP_NAT_MANIP_SRC) {
103                 /* Get rid of src ip and src pt */
104                 oldip = iph->saddr;
105                 newip = tuple->src.u3.ip;
106                 newport = tuple->src.u.udp.port;
107                 portptr = &hdr->source;
108         } else {
109                 /* Get rid of dst ip and dst pt */
110                 oldip = iph->daddr;
111                 newip = tuple->dst.u3.ip;
112                 newport = tuple->dst.u.udp.port;
113                 portptr = &hdr->dest;
114         }
115         if (hdr->check || (*pskb)->ip_summed == CHECKSUM_PARTIAL) {
116                 nf_proto_csum_replace4(&hdr->check, *pskb, oldip, newip, 1);
117                 nf_proto_csum_replace2(&hdr->check, *pskb, *portptr, newport,
118                                        0);
119                 if (!hdr->check)
120                         hdr->check = CSUM_MANGLED_0;
121         }
122         *portptr = newport;
123         return 1;
124 }
125
126 struct nf_nat_protocol nf_nat_protocol_udp = {
127         .name                   = "UDP",
128         .protonum               = IPPROTO_UDP,
129         .me                     = THIS_MODULE,
130         .manip_pkt              = udp_manip_pkt,
131         .in_range               = udp_in_range,
132         .unique_tuple           = udp_unique_tuple,
133 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
134         .range_to_nfattr        = nf_nat_port_range_to_nfattr,
135         .nfattr_to_range        = nf_nat_port_nfattr_to_range,
136 #endif
137 };