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_rule.h>
10 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
11 
12 static int
udp_in_range(const struct ip_conntrack_tuple * tuple,enum ip_nat_manip_type maniptype,const union ip_conntrack_manip_proto * min,const union ip_conntrack_manip_proto * max)13 udp_in_range(const struct ip_conntrack_tuple *tuple,
14 	     enum ip_nat_manip_type maniptype,
15 	     const union ip_conntrack_manip_proto *min,
16 	     const union ip_conntrack_manip_proto *max)
17 {
18 	u_int16_t port;
19 
20 	if (maniptype == IP_NAT_MANIP_SRC)
21 		port = tuple->src.u.udp.port;
22 	else
23 		port = tuple->dst.u.udp.port;
24 
25 	return ntohs(port) >= ntohs(min->udp.port)
26 		&& ntohs(port) <= ntohs(max->udp.port);
27 }
28 
29 static int
udp_unique_tuple(struct ip_conntrack_tuple * tuple,const struct ip_nat_range * range,enum ip_nat_manip_type maniptype,const struct ip_conntrack * conntrack)30 udp_unique_tuple(struct ip_conntrack_tuple *tuple,
31 		 const struct ip_nat_range *range,
32 		 enum ip_nat_manip_type maniptype,
33 		 const struct ip_conntrack *conntrack)
34 {
35 	static u_int16_t port = 0;
36 	u_int16_t *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 void
udp_manip_pkt(struct iphdr * iph,size_t len,const struct ip_conntrack_manip * manip,enum ip_nat_manip_type maniptype)77 udp_manip_pkt(struct iphdr *iph, size_t len,
78 	      const struct ip_conntrack_manip *manip,
79 	      enum ip_nat_manip_type maniptype)
80 {
81 	struct udphdr *hdr = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
82 	u_int32_t oldip;
83 	u_int16_t *portptr;
84 
85 	if (maniptype == IP_NAT_MANIP_SRC) {
86 		/* Get rid of src ip and src pt */
87 		oldip = iph->saddr;
88 		portptr = &hdr->source;
89 	} else {
90 		/* Get rid of dst ip and dst pt */
91 		oldip = iph->daddr;
92 		portptr = &hdr->dest;
93 	}
94 	if (hdr->check) /* 0 is a special case meaning no checksum */
95 		hdr->check = ip_nat_cheat_check(~oldip, manip->ip,
96 					ip_nat_cheat_check(*portptr ^ 0xFFFF,
97 							   manip->u.udp.port,
98 							   hdr->check));
99 	*portptr = manip->u.udp.port;
100 }
101 
102 static unsigned int
udp_print(char * buffer,const struct ip_conntrack_tuple * match,const struct ip_conntrack_tuple * mask)103 udp_print(char *buffer,
104 	  const struct ip_conntrack_tuple *match,
105 	  const struct ip_conntrack_tuple *mask)
106 {
107 	unsigned int len = 0;
108 
109 	if (mask->src.u.udp.port)
110 		len += sprintf(buffer + len, "srcpt=%u ",
111 			       ntohs(match->src.u.udp.port));
112 
113 
114 	if (mask->dst.u.udp.port)
115 		len += sprintf(buffer + len, "dstpt=%u ",
116 			       ntohs(match->dst.u.udp.port));
117 
118 	return len;
119 }
120 
121 static unsigned int
udp_print_range(char * buffer,const struct ip_nat_range * range)122 udp_print_range(char *buffer, const struct ip_nat_range *range)
123 {
124 	if (range->min.udp.port != 0 || range->max.udp.port != 0xFFFF) {
125 		if (range->min.udp.port == range->max.udp.port)
126 			return sprintf(buffer, "port %u ",
127 				       ntohs(range->min.udp.port));
128 		else
129 			return sprintf(buffer, "ports %u-%u ",
130 				       ntohs(range->min.udp.port),
131 				       ntohs(range->max.udp.port));
132 	}
133 	else return 0;
134 }
135 
136 struct ip_nat_protocol ip_nat_protocol_udp
137 = { { NULL, NULL }, "UDP", IPPROTO_UDP,
138     udp_manip_pkt,
139     udp_in_range,
140     udp_unique_tuple,
141     udp_print,
142     udp_print_range
143 };
144