1 #include <linux/types.h>
2 #include <linux/init.h>
3 #include <linux/netfilter.h>
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>
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)
21 if (maniptype == IP_NAT_MANIP_SRC)
22 port = tuple->src.u.udp.port;
24 port = tuple->dst.u.udp.port;
26 return ntohs(port) >= ntohs(min->udp.port)
27 && ntohs(port) <= ntohs(max->udp.port);
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)
36 static u_int16_t port, *portptr;
37 unsigned int range_size, min, i;
39 if (maniptype == IP_NAT_MANIP_SRC)
40 portptr = &tuple->src.u.udp.port;
42 portptr = &tuple->dst.u.udp.port;
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)
50 if (ntohs(*portptr) < 1024) {
51 /* Loose convention: >> 512 is credential passing */
52 if (ntohs(*portptr)<512) {
54 range_size = 511 - min + 1;
57 range_size = 1023 - min + 1;
61 range_size = 65535 - 1024 + 1;
64 min = ntohs(range->min.udp.port);
65 range_size = ntohs(range->max.udp.port) - min + 1;
68 for (i = 0; i < range_size; i++, port++) {
69 *portptr = htons(min + port % range_size);
70 if (!ip_nat_used_tuple(tuple, conntrack))
77 udp_manip_pkt(struct sk_buff **pskb,
79 const struct ip_conntrack_manip *manip,
80 enum ip_nat_manip_type maniptype)
86 if (!skb_ip_make_writable(pskb, hdroff + sizeof(hdr)))
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;
95 /* Get rid of dst ip and dst pt */
96 oldip = (*pskb)->nh.iph->daddr;
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,
104 *portptr = manip->u.udp.port;
109 udp_print(char *buffer,
110 const struct ip_conntrack_tuple *match,
111 const struct ip_conntrack_tuple *mask)
113 unsigned int len = 0;
115 if (mask->src.u.udp.port)
116 len += sprintf(buffer + len, "srcpt=%u ",
117 ntohs(match->src.u.udp.port));
120 if (mask->dst.u.udp.port)
121 len += sprintf(buffer + len, "dstpt=%u ",
122 ntohs(match->dst.u.udp.port));
128 udp_print_range(char *buffer, const struct ip_nat_range *range)
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));
135 return sprintf(buffer, "ports %u-%u ",
136 ntohs(range->min.udp.port),
137 ntohs(range->max.udp.port));
142 struct ip_nat_protocol ip_nat_protocol_udp
143 = { { NULL, NULL }, "UDP", IPPROTO_UDP,