Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 2 | * (C) 2002-2004 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/netfilter.h> |
| 12 | #include <linux/ip.h> |
| 13 | #include <linux/udp.h> |
| 14 | #include <linux/if.h> |
| 15 | |
| 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> |
| 20 | |
| 21 | static int |
| 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) |
| 26 | { |
| 27 | u_int16_t port; |
| 28 | |
| 29 | if (maniptype == IP_NAT_MANIP_SRC) |
| 30 | port = tuple->src.u.udp.port; |
| 31 | else |
| 32 | port = tuple->dst.u.udp.port; |
| 33 | |
| 34 | return ntohs(port) >= ntohs(min->udp.port) |
| 35 | && ntohs(port) <= ntohs(max->udp.port); |
| 36 | } |
| 37 | |
| 38 | static int |
| 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) |
| 43 | { |
Patrick McHardy | d04b4f8 | 2005-07-22 12:50:29 -0700 | [diff] [blame] | 44 | static u_int16_t port; |
| 45 | u_int16_t *portptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | unsigned int range_size, min, i; |
| 47 | |
| 48 | if (maniptype == IP_NAT_MANIP_SRC) |
| 49 | portptr = &tuple->src.u.udp.port; |
| 50 | else |
| 51 | portptr = &tuple->dst.u.udp.port; |
| 52 | |
| 53 | /* If no range specified... */ |
| 54 | if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) { |
| 55 | /* If it's dst rewrite, can't change port */ |
| 56 | if (maniptype == IP_NAT_MANIP_DST) |
| 57 | return 0; |
| 58 | |
| 59 | if (ntohs(*portptr) < 1024) { |
| 60 | /* Loose convention: >> 512 is credential passing */ |
| 61 | if (ntohs(*portptr)<512) { |
| 62 | min = 1; |
| 63 | range_size = 511 - min + 1; |
| 64 | } else { |
| 65 | min = 600; |
| 66 | range_size = 1023 - min + 1; |
| 67 | } |
| 68 | } else { |
| 69 | min = 1024; |
| 70 | range_size = 65535 - 1024 + 1; |
| 71 | } |
| 72 | } else { |
| 73 | min = ntohs(range->min.udp.port); |
| 74 | range_size = ntohs(range->max.udp.port) - min + 1; |
| 75 | } |
| 76 | |
| 77 | for (i = 0; i < range_size; i++, port++) { |
| 78 | *portptr = htons(min + port % range_size); |
| 79 | if (!ip_nat_used_tuple(tuple, conntrack)) |
| 80 | return 1; |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int |
| 86 | udp_manip_pkt(struct sk_buff **pskb, |
| 87 | unsigned int iphdroff, |
| 88 | const struct ip_conntrack_tuple *tuple, |
| 89 | enum ip_nat_manip_type maniptype) |
| 90 | { |
| 91 | struct iphdr *iph = (struct iphdr *)((*pskb)->data + iphdroff); |
| 92 | struct udphdr *hdr; |
| 93 | unsigned int hdroff = iphdroff + iph->ihl*4; |
| 94 | u32 oldip, newip; |
| 95 | u16 *portptr, newport; |
| 96 | |
Harald Welte | 089af26 | 2005-08-09 19:37:23 -0700 | [diff] [blame] | 97 | if (!skb_make_writable(pskb, hdroff + sizeof(*hdr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return 0; |
| 99 | |
| 100 | iph = (struct iphdr *)((*pskb)->data + iphdroff); |
| 101 | hdr = (struct udphdr *)((*pskb)->data + hdroff); |
| 102 | |
| 103 | if (maniptype == IP_NAT_MANIP_SRC) { |
| 104 | /* Get rid of src ip and src pt */ |
| 105 | oldip = iph->saddr; |
| 106 | newip = tuple->src.ip; |
| 107 | newport = tuple->src.u.udp.port; |
| 108 | portptr = &hdr->source; |
| 109 | } else { |
| 110 | /* Get rid of dst ip and dst pt */ |
| 111 | oldip = iph->daddr; |
| 112 | newip = tuple->dst.ip; |
| 113 | newport = tuple->dst.u.udp.port; |
| 114 | portptr = &hdr->dest; |
| 115 | } |
| 116 | if (hdr->check) /* 0 is a special case meaning no checksum */ |
| 117 | hdr->check = ip_nat_cheat_check(~oldip, newip, |
| 118 | ip_nat_cheat_check(*portptr ^ 0xFFFF, |
| 119 | newport, |
| 120 | hdr->check)); |
| 121 | *portptr = newport; |
| 122 | return 1; |
| 123 | } |
| 124 | |
Patrick McHardy | 373ac73 | 2005-08-09 19:36:53 -0700 | [diff] [blame] | 125 | struct ip_nat_protocol ip_nat_protocol_udp = { |
| 126 | .name = "UDP", |
| 127 | .protonum = IPPROTO_UDP, |
| 128 | .me = THIS_MODULE, |
| 129 | .manip_pkt = udp_manip_pkt, |
| 130 | .in_range = udp_in_range, |
| 131 | .unique_tuple = udp_unique_tuple, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 132 | #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \ |
| 133 | defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE) |
Patrick McHardy | 373ac73 | 2005-08-09 19:36:53 -0700 | [diff] [blame] | 134 | .range_to_nfattr = ip_nat_port_range_to_nfattr, |
| 135 | .nfattr_to_range = ip_nat_port_nfattr_to_range, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 136 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | }; |