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/udp.h>
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>
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
)
29 if (maniptype
== IP_NAT_MANIP_SRC
)
30 port
= tuple
->src
.u
.udp
.port
;
32 port
= tuple
->dst
.u
.udp
.port
;
34 return ntohs(port
) >= ntohs(min
->udp
.port
)
35 && ntohs(port
) <= ntohs(max
->udp
.port
);
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
)
44 static u_int16_t port
, *portptr
;
45 unsigned int range_size
, min
, i
;
47 if (maniptype
== IP_NAT_MANIP_SRC
)
48 portptr
= &tuple
->src
.u
.udp
.port
;
50 portptr
= &tuple
->dst
.u
.udp
.port
;
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
)
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
.udp
.port
);
73 range_size
= ntohs(range
->max
.udp
.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
))
85 udp_manip_pkt(struct sk_buff
**pskb
,
86 unsigned int iphdroff
,
87 const struct ip_conntrack_tuple
*tuple
,
88 enum ip_nat_manip_type maniptype
)
90 struct iphdr
*iph
= (struct iphdr
*)((*pskb
)->data
+ iphdroff
);
92 unsigned int hdroff
= iphdroff
+ iph
->ihl
*4;
94 u16
*portptr
, newport
;
96 if (!skb_ip_make_writable(pskb
, hdroff
+ sizeof(*hdr
)))
99 iph
= (struct iphdr
*)((*pskb
)->data
+ iphdroff
);
100 hdr
= (struct udphdr
*)((*pskb
)->data
+ hdroff
);
102 if (maniptype
== IP_NAT_MANIP_SRC
) {
103 /* Get rid of src ip and src pt */
105 newip
= tuple
->src
.ip
;
106 newport
= tuple
->src
.u
.udp
.port
;
107 portptr
= &hdr
->source
;
109 /* Get rid of dst ip and dst pt */
111 newip
= tuple
->dst
.ip
;
112 newport
= tuple
->dst
.u
.udp
.port
;
113 portptr
= &hdr
->dest
;
115 if (hdr
->check
) /* 0 is a special case meaning no checksum */
116 hdr
->check
= ip_nat_cheat_check(~oldip
, newip
,
117 ip_nat_cheat_check(*portptr
^ 0xFFFF,
125 udp_print(char *buffer
,
126 const struct ip_conntrack_tuple
*match
,
127 const struct ip_conntrack_tuple
*mask
)
129 unsigned int len
= 0;
131 if (mask
->src
.u
.udp
.port
)
132 len
+= sprintf(buffer
+ len
, "srcpt=%u ",
133 ntohs(match
->src
.u
.udp
.port
));
136 if (mask
->dst
.u
.udp
.port
)
137 len
+= sprintf(buffer
+ len
, "dstpt=%u ",
138 ntohs(match
->dst
.u
.udp
.port
));
144 udp_print_range(char *buffer
, const struct ip_nat_range
*range
)
146 if (range
->min
.udp
.port
!= 0 || range
->max
.udp
.port
!= 0xFFFF) {
147 if (range
->min
.udp
.port
== range
->max
.udp
.port
)
148 return sprintf(buffer
, "port %u ",
149 ntohs(range
->min
.udp
.port
));
151 return sprintf(buffer
, "ports %u-%u ",
152 ntohs(range
->min
.udp
.port
),
153 ntohs(range
->max
.udp
.port
));
158 struct ip_nat_protocol ip_nat_protocol_udp
159 = { "UDP", IPPROTO_UDP
,