commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / net / ipv4 / netfilter / ip_nat_proto_udp.c
1 #include <linux/types.h>
2 #include <linux/init.h>
3 #include <linux/netfilter.h>
4 #include <linux/ip.h>
5 #include <linux/udp.h>
6 #include <linux/if.h>
7
8 #include <linux/netfilter_ipv4/ip_nat.h>
9 #include <linux/netfilter_ipv4/ip_nat_core.h>
10 #include <linux/netfilter_ipv4/ip_nat_rule.h>
11 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
12
13 static int
14 udp_in_range(const struct ip_conntrack_tuple *tuple,
15              enum ip_nat_manip_type maniptype,
16              const union ip_conntrack_manip_proto *min,
17              const union ip_conntrack_manip_proto *max)
18 {
19         u_int16_t port;
20
21         if (maniptype == IP_NAT_MANIP_SRC)
22                 port = tuple->src.u.udp.port;
23         else
24                 port = tuple->dst.u.udp.port;
25
26         return ntohs(port) >= ntohs(min->udp.port)
27                 && ntohs(port) <= ntohs(max->udp.port);
28 }
29
30 static int
31 udp_unique_tuple(struct ip_conntrack_tuple *tuple,
32                  const struct ip_nat_range *range,
33                  enum ip_nat_manip_type maniptype,
34                  const struct ip_conntrack *conntrack)
35 {
36         static u_int16_t port, *portptr;
37         unsigned int range_size, min, i;
38
39         if (maniptype == IP_NAT_MANIP_SRC)
40                 portptr = &tuple->src.u.udp.port;
41         else
42                 portptr = &tuple->dst.u.udp.port;
43
44         /* If no range specified... */
45         if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) {
46                 /* If it's dst rewrite, can't change port */
47                 if (maniptype == IP_NAT_MANIP_DST)
48                         return 0;
49
50                 if (ntohs(*portptr) < 1024) {
51                         /* Loose convention: >> 512 is credential passing */
52                         if (ntohs(*portptr)<512) {
53                                 min = 1;
54                                 range_size = 511 - min + 1;
55                         } else {
56                                 min = 600;
57                                 range_size = 1023 - min + 1;
58                         }
59                 } else {
60                         min = 1024;
61                         range_size = 65535 - 1024 + 1;
62                 }
63         } else {
64                 min = ntohs(range->min.udp.port);
65                 range_size = ntohs(range->max.udp.port) - min + 1;
66         }
67
68         for (i = 0; i < range_size; i++, port++) {
69                 *portptr = htons(min + port % range_size);
70                 if (!ip_nat_used_tuple(tuple, conntrack))
71                         return 1;
72         }
73         return 0;
74 }
75
76 static int
77 udp_manip_pkt(struct sk_buff **pskb,
78               unsigned int hdroff,
79               const struct ip_conntrack_manip *manip,
80               enum ip_nat_manip_type maniptype)
81 {
82         struct udphdr *hdr;
83         u_int32_t oldip;
84         u_int16_t *portptr;
85
86         if (!skb_ip_make_writable(pskb, hdroff + sizeof(hdr)))
87                 return 0;
88
89         hdr = (void *)(*pskb)->data + hdroff;
90         if (maniptype == IP_NAT_MANIP_SRC) {
91                 /* Get rid of src ip and src pt */
92                 oldip = (*pskb)->nh.iph->saddr;
93                 portptr = &hdr->source;
94         } else {
95                 /* Get rid of dst ip and dst pt */
96                 oldip = (*pskb)->nh.iph->daddr;
97                 portptr = &hdr->dest;
98         }
99         if (hdr->check) /* 0 is a special case meaning no checksum */
100                 hdr->check = ip_nat_cheat_check(~oldip, manip->ip,
101                                         ip_nat_cheat_check(*portptr ^ 0xFFFF,
102                                                            manip->u.udp.port,
103                                                            hdr->check));
104         *portptr = manip->u.udp.port;
105         return 1;
106 }
107
108 static unsigned int
109 udp_print(char *buffer,
110           const struct ip_conntrack_tuple *match,
111           const struct ip_conntrack_tuple *mask)
112 {
113         unsigned int len = 0;
114
115         if (mask->src.u.udp.port)
116                 len += sprintf(buffer + len, "srcpt=%u ",
117                                ntohs(match->src.u.udp.port));
118
119
120         if (mask->dst.u.udp.port)
121                 len += sprintf(buffer + len, "dstpt=%u ",
122                                ntohs(match->dst.u.udp.port));
123
124         return len;
125 }
126
127 static unsigned int
128 udp_print_range(char *buffer, const struct ip_nat_range *range)
129 {
130         if (range->min.udp.port != 0 || range->max.udp.port != 0xFFFF) {
131                 if (range->min.udp.port == range->max.udp.port)
132                         return sprintf(buffer, "port %u ",
133                                        ntohs(range->min.udp.port));
134                 else
135                         return sprintf(buffer, "ports %u-%u ",
136                                        ntohs(range->min.udp.port),
137                                        ntohs(range->max.udp.port));
138         }
139         else return 0;
140 }
141
142 struct ip_nat_protocol ip_nat_protocol_udp
143 = { { NULL, NULL }, "UDP", IPPROTO_UDP,
144     udp_manip_pkt,
145     udp_in_range,
146     udp_unique_tuple,
147     udp_print,
148     udp_print_range
149 };