Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ip_nat_proto_gre.c - Version 2.0 |
| 3 | * |
| 4 | * NAT protocol helper module for GRE. |
| 5 | * |
| 6 | * GRE is a generic encapsulation protocol, which is generally not very |
| 7 | * suited for NAT, as it has no protocol-specific part as port numbers. |
| 8 | * |
Patrick McHardy | edd5a32 | 2006-09-20 12:07:39 -0700 | [diff] [blame] | 9 | * It has an optional key field, which may help us distinguishing two |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 10 | * connections between the same two hosts. |
| 11 | * |
Patrick McHardy | edd5a32 | 2006-09-20 12:07:39 -0700 | [diff] [blame] | 12 | * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784 |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 13 | * |
| 14 | * PPTP is built on top of a modified version of GRE, and has a mandatory |
| 15 | * field called "CallID", which serves us for the same purpose as the key |
| 16 | * field in plain GRE. |
| 17 | * |
| 18 | * Documentation about PPTP can be found in RFC 2637 |
| 19 | * |
| 20 | * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org> |
| 21 | * |
| 22 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
| 23 | * |
| 24 | */ |
| 25 | |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/ip.h> |
| 28 | #include <linux/netfilter_ipv4/ip_nat.h> |
| 29 | #include <linux/netfilter_ipv4/ip_nat_rule.h> |
| 30 | #include <linux/netfilter_ipv4/ip_nat_protocol.h> |
| 31 | #include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h> |
| 32 | |
| 33 | MODULE_LICENSE("GPL"); |
| 34 | MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>"); |
| 35 | MODULE_DESCRIPTION("Netfilter NAT protocol helper module for GRE"); |
| 36 | |
| 37 | #if 0 |
| 38 | #define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s: " format, __FILE__, \ |
| 39 | __FUNCTION__, ## args) |
| 40 | #else |
| 41 | #define DEBUGP(x, args...) |
| 42 | #endif |
| 43 | |
| 44 | /* is key in given range between min and max */ |
| 45 | static int |
| 46 | gre_in_range(const struct ip_conntrack_tuple *tuple, |
| 47 | enum ip_nat_manip_type maniptype, |
| 48 | const union ip_conntrack_manip_proto *min, |
| 49 | const union ip_conntrack_manip_proto *max) |
| 50 | { |
Alexey Dobriyan | a467704 | 2006-05-19 02:16:29 -0700 | [diff] [blame] | 51 | __be16 key; |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 52 | |
| 53 | if (maniptype == IP_NAT_MANIP_SRC) |
| 54 | key = tuple->src.u.gre.key; |
| 55 | else |
| 56 | key = tuple->dst.u.gre.key; |
| 57 | |
Alexey Dobriyan | a467704 | 2006-05-19 02:16:29 -0700 | [diff] [blame] | 58 | return ntohs(key) >= ntohs(min->gre.key) |
| 59 | && ntohs(key) <= ntohs(max->gre.key); |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* generate unique tuple ... */ |
Patrick McHardy | edd5a32 | 2006-09-20 12:07:39 -0700 | [diff] [blame] | 63 | static int |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 64 | gre_unique_tuple(struct ip_conntrack_tuple *tuple, |
| 65 | const struct ip_nat_range *range, |
| 66 | enum ip_nat_manip_type maniptype, |
| 67 | const struct ip_conntrack *conntrack) |
| 68 | { |
| 69 | static u_int16_t key; |
Patrick McHardy | 955b944 | 2006-09-20 12:08:03 -0700 | [diff] [blame] | 70 | __be16 *keyptr; |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 71 | unsigned int min, i, range_size; |
| 72 | |
| 73 | if (maniptype == IP_NAT_MANIP_SRC) |
| 74 | keyptr = &tuple->src.u.gre.key; |
| 75 | else |
| 76 | keyptr = &tuple->dst.u.gre.key; |
| 77 | |
| 78 | if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) { |
| 79 | DEBUGP("%p: NATing GRE PPTP\n", conntrack); |
| 80 | min = 1; |
| 81 | range_size = 0xffff; |
| 82 | } else { |
Alexey Dobriyan | a467704 | 2006-05-19 02:16:29 -0700 | [diff] [blame] | 83 | min = ntohs(range->min.gre.key); |
| 84 | range_size = ntohs(range->max.gre.key) - min + 1; |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Patrick McHardy | edd5a32 | 2006-09-20 12:07:39 -0700 | [diff] [blame] | 87 | DEBUGP("min = %u, range_size = %u\n", min, range_size); |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 88 | |
| 89 | for (i = 0; i < range_size; i++, key++) { |
Alexey Dobriyan | a467704 | 2006-05-19 02:16:29 -0700 | [diff] [blame] | 90 | *keyptr = htons(min + key % range_size); |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 91 | if (!ip_nat_used_tuple(tuple, conntrack)) |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | DEBUGP("%p: no NAT mapping\n", conntrack); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /* manipulate a GRE packet according to maniptype */ |
| 101 | static int |
| 102 | gre_manip_pkt(struct sk_buff **pskb, |
| 103 | unsigned int iphdroff, |
| 104 | const struct ip_conntrack_tuple *tuple, |
| 105 | enum ip_nat_manip_type maniptype) |
| 106 | { |
| 107 | struct gre_hdr *greh; |
| 108 | struct gre_hdr_pptp *pgreh; |
| 109 | struct iphdr *iph = (struct iphdr *)((*pskb)->data + iphdroff); |
| 110 | unsigned int hdroff = iphdroff + iph->ihl*4; |
| 111 | |
| 112 | /* pgreh includes two optional 32bit fields which are not required |
| 113 | * to be there. That's where the magic '8' comes from */ |
| 114 | if (!skb_make_writable(pskb, hdroff + sizeof(*pgreh)-8)) |
| 115 | return 0; |
| 116 | |
| 117 | greh = (void *)(*pskb)->data + hdroff; |
| 118 | pgreh = (struct gre_hdr_pptp *) greh; |
| 119 | |
Patrick McHardy | edd5a32 | 2006-09-20 12:07:39 -0700 | [diff] [blame] | 120 | /* we only have destination manip of a packet, since 'source key' |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 121 | * is not present in the packet itself */ |
| 122 | if (maniptype == IP_NAT_MANIP_DST) { |
| 123 | /* key manipulation is always dest */ |
| 124 | switch (greh->version) { |
| 125 | case 0: |
| 126 | if (!greh->key) { |
| 127 | DEBUGP("can't nat GRE w/o key\n"); |
| 128 | break; |
| 129 | } |
| 130 | if (greh->csum) { |
| 131 | /* FIXME: Never tested this code... */ |
Al Viro | 43bc0ca | 2006-11-14 21:43:23 -0800 | [diff] [blame] | 132 | nf_proto_csum_replace4(gre_csum(greh), *pskb, |
| 133 | *(gre_key(greh)), |
| 134 | tuple->dst.u.gre.key, 0); |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 135 | } |
| 136 | *(gre_key(greh)) = tuple->dst.u.gre.key; |
| 137 | break; |
| 138 | case GRE_VERSION_PPTP: |
Patrick McHardy | edd5a32 | 2006-09-20 12:07:39 -0700 | [diff] [blame] | 139 | DEBUGP("call_id -> 0x%04x\n", |
Harald Welte | d811552 | 2005-11-03 13:05:20 +0100 | [diff] [blame] | 140 | ntohs(tuple->dst.u.gre.key)); |
| 141 | pgreh->call_id = tuple->dst.u.gre.key; |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 142 | break; |
| 143 | default: |
| 144 | DEBUGP("can't nat unknown GRE version\n"); |
| 145 | return 0; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | return 1; |
| 150 | } |
| 151 | |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 152 | /* nat helper struct */ |
Patrick McHardy | edd5a32 | 2006-09-20 12:07:39 -0700 | [diff] [blame] | 153 | static struct ip_nat_protocol gre = { |
| 154 | .name = "GRE", |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 155 | .protonum = IPPROTO_GRE, |
| 156 | .manip_pkt = gre_manip_pkt, |
| 157 | .in_range = gre_in_range, |
| 158 | .unique_tuple = gre_unique_tuple, |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 159 | #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \ |
| 160 | defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE) |
| 161 | .range_to_nfattr = ip_nat_port_range_to_nfattr, |
| 162 | .nfattr_to_range = ip_nat_port_nfattr_to_range, |
| 163 | #endif |
| 164 | }; |
Patrick McHardy | edd5a32 | 2006-09-20 12:07:39 -0700 | [diff] [blame] | 165 | |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 166 | int __init ip_nat_proto_gre_init(void) |
| 167 | { |
| 168 | return ip_nat_protocol_register(&gre); |
| 169 | } |
| 170 | |
| 171 | void __exit ip_nat_proto_gre_fini(void) |
| 172 | { |
| 173 | ip_nat_protocol_unregister(&gre); |
| 174 | } |