Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 1 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 2 | * (C) 2002-2006 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> |
Eric Leblond | 41f4689 | 2007-02-07 15:10:09 -0800 | [diff] [blame] | 11 | #include <linux/random.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 12 | #include <linux/ip.h> |
| 13 | #include <linux/udp.h> |
| 14 | |
| 15 | #include <linux/netfilter.h> |
| 16 | #include <net/netfilter/nf_nat.h> |
| 17 | #include <net/netfilter/nf_nat_core.h> |
| 18 | #include <net/netfilter/nf_nat_rule.h> |
| 19 | #include <net/netfilter/nf_nat_protocol.h> |
| 20 | |
| 21 | static int |
| 22 | udp_in_range(const struct nf_conntrack_tuple *tuple, |
| 23 | enum nf_nat_manip_type maniptype, |
| 24 | const union nf_conntrack_man_proto *min, |
| 25 | const union nf_conntrack_man_proto *max) |
| 26 | { |
| 27 | __be16 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 nf_conntrack_tuple *tuple, |
| 40 | const struct nf_nat_range *range, |
| 41 | enum nf_nat_manip_type maniptype, |
| 42 | const struct nf_conn *ct) |
| 43 | { |
| 44 | static u_int16_t port; |
| 45 | __be16 *portptr; |
| 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 | |
Eric Leblond | 41f4689 | 2007-02-07 15:10:09 -0800 | [diff] [blame] | 77 | if (range->flags & IP_NAT_RANGE_PROTO_RANDOM) |
| 78 | port = net_random(); |
| 79 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 80 | for (i = 0; i < range_size; i++, port++) { |
| 81 | *portptr = htons(min + port % range_size); |
| 82 | if (!nf_nat_used_tuple(tuple, ct)) |
| 83 | return 1; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 89 | udp_manip_pkt(struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 90 | unsigned int iphdroff, |
| 91 | const struct nf_conntrack_tuple *tuple, |
| 92 | enum nf_nat_manip_type maniptype) |
| 93 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 94 | struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 95 | struct udphdr *hdr; |
| 96 | unsigned int hdroff = iphdroff + iph->ihl*4; |
| 97 | __be32 oldip, newip; |
| 98 | __be16 *portptr, newport; |
| 99 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 100 | if (!skb_make_writable(skb, hdroff + sizeof(*hdr))) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 101 | return 0; |
| 102 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 103 | iph = (struct iphdr *)(skb->data + iphdroff); |
| 104 | hdr = (struct udphdr *)(skb->data + hdroff); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 105 | |
| 106 | if (maniptype == IP_NAT_MANIP_SRC) { |
| 107 | /* Get rid of src ip and src pt */ |
| 108 | oldip = iph->saddr; |
| 109 | newip = tuple->src.u3.ip; |
| 110 | newport = tuple->src.u.udp.port; |
| 111 | portptr = &hdr->source; |
| 112 | } else { |
| 113 | /* Get rid of dst ip and dst pt */ |
| 114 | oldip = iph->daddr; |
| 115 | newip = tuple->dst.u3.ip; |
| 116 | newport = tuple->dst.u.udp.port; |
| 117 | portptr = &hdr->dest; |
| 118 | } |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 119 | if (hdr->check || skb->ip_summed == CHECKSUM_PARTIAL) { |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame^] | 120 | inet_proto_csum_replace4(&hdr->check, skb, oldip, newip, 1); |
| 121 | inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport, |
| 122 | 0); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 123 | if (!hdr->check) |
| 124 | hdr->check = CSUM_MANGLED_0; |
| 125 | } |
| 126 | *portptr = newport; |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | struct nf_nat_protocol nf_nat_protocol_udp = { |
| 131 | .name = "UDP", |
| 132 | .protonum = IPPROTO_UDP, |
| 133 | .me = THIS_MODULE, |
| 134 | .manip_pkt = udp_manip_pkt, |
| 135 | .in_range = udp_in_range, |
| 136 | .unique_tuple = udp_unique_tuple, |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 137 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 138 | .range_to_nlattr = nf_nat_port_range_to_nlattr, |
| 139 | .nlattr_to_range = nf_nat_port_nlattr_to_range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 140 | #endif |
| 141 | }; |