[NETFILTER]: Remove redundant parentheses/braces
[deliverable/linux.git] / net / ipv4 / netfilter / nf_nat_proto_gre.c
CommitLineData
f09943fe
PM
1/*
2 * nf_nat_proto_gre.c
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 *
9 * It has an optional key field, which may help us distinguishing two
10 * connections between the same two hosts.
11 *
12 * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
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
26#include <linux/module.h>
27#include <linux/skbuff.h>
28#include <linux/ip.h>
29
30#include <net/netfilter/nf_nat.h>
31#include <net/netfilter/nf_nat_rule.h>
32#include <net/netfilter/nf_nat_protocol.h>
33#include <linux/netfilter/nf_conntrack_proto_gre.h>
34
35MODULE_LICENSE("GPL");
36MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
37MODULE_DESCRIPTION("Netfilter NAT protocol helper module for GRE");
38
39#if 0
40#define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s: " format, __FILE__, \
41 __FUNCTION__, ## args)
42#else
43#define DEBUGP(x, args...)
44#endif
45
46/* is key in given range between min and max */
47static int
48gre_in_range(const struct nf_conntrack_tuple *tuple,
49 enum nf_nat_manip_type maniptype,
50 const union nf_conntrack_man_proto *min,
51 const union nf_conntrack_man_proto *max)
52{
53 __be16 key;
54
55 if (maniptype == IP_NAT_MANIP_SRC)
56 key = tuple->src.u.gre.key;
57 else
58 key = tuple->dst.u.gre.key;
59
60 return ntohs(key) >= ntohs(min->gre.key) &&
61 ntohs(key) <= ntohs(max->gre.key);
62}
63
64/* generate unique tuple ... */
65static int
66gre_unique_tuple(struct nf_conntrack_tuple *tuple,
67 const struct nf_nat_range *range,
68 enum nf_nat_manip_type maniptype,
69 const struct nf_conn *conntrack)
70{
71 static u_int16_t key;
72 __be16 *keyptr;
73 unsigned int min, i, range_size;
74
c2a1910b
JB
75 /* If there is no master conntrack we are not PPTP,
76 do not change tuples */
77 if (!conntrack->master)
78 return 0;
79
f09943fe
PM
80 if (maniptype == IP_NAT_MANIP_SRC)
81 keyptr = &tuple->src.u.gre.key;
82 else
83 keyptr = &tuple->dst.u.gre.key;
84
85 if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) {
86 DEBUGP("%p: NATing GRE PPTP\n", conntrack);
87 min = 1;
88 range_size = 0xffff;
89 } else {
90 min = ntohs(range->min.gre.key);
91 range_size = ntohs(range->max.gre.key) - min + 1;
92 }
93
94 DEBUGP("min = %u, range_size = %u\n", min, range_size);
95
96 for (i = 0; i < range_size; i++, key++) {
97 *keyptr = htons(min + key % range_size);
98 if (!nf_nat_used_tuple(tuple, conntrack))
99 return 1;
100 }
101
102 DEBUGP("%p: no NAT mapping\n", conntrack);
103 return 0;
104}
105
106/* manipulate a GRE packet according to maniptype */
107static int
108gre_manip_pkt(struct sk_buff **pskb, unsigned int iphdroff,
109 const struct nf_conntrack_tuple *tuple,
110 enum nf_nat_manip_type maniptype)
111{
112 struct gre_hdr *greh;
113 struct gre_hdr_pptp *pgreh;
114 struct iphdr *iph = (struct iphdr *)((*pskb)->data + iphdroff);
115 unsigned int hdroff = iphdroff + iph->ihl * 4;
116
117 /* pgreh includes two optional 32bit fields which are not required
118 * to be there. That's where the magic '8' comes from */
119 if (!skb_make_writable(pskb, hdroff + sizeof(*pgreh) - 8))
120 return 0;
121
122 greh = (void *)(*pskb)->data + hdroff;
123 pgreh = (struct gre_hdr_pptp *)greh;
124
125 /* we only have destination manip of a packet, since 'source key'
126 * is not present in the packet itself */
127 if (maniptype != IP_NAT_MANIP_DST)
128 return 1;
129 switch (greh->version) {
c2a1910b
JB
130 case GRE_VERSION_1701:
131 /* We do not currently NAT any GREv0 packets.
132 * Try to behave like "nf_nat_proto_unknown" */
f09943fe
PM
133 break;
134 case GRE_VERSION_PPTP:
135 DEBUGP("call_id -> 0x%04x\n", ntohs(tuple->dst.u.gre.key));
136 pgreh->call_id = tuple->dst.u.gre.key;
137 break;
138 default:
139 DEBUGP("can't nat unknown GRE version\n");
140 return 0;
141 }
142 return 1;
143}
144
145static struct nf_nat_protocol gre __read_mostly = {
146 .name = "GRE",
147 .protonum = IPPROTO_GRE,
148 .manip_pkt = gre_manip_pkt,
149 .in_range = gre_in_range,
150 .unique_tuple = gre_unique_tuple,
e281db5c 151#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
f09943fe
PM
152 .range_to_nfattr = nf_nat_port_range_to_nfattr,
153 .nfattr_to_range = nf_nat_port_nfattr_to_range,
154#endif
155};
156
157int __init nf_nat_proto_gre_init(void)
158{
159 return nf_nat_protocol_register(&gre);
160}
161
162void __exit nf_nat_proto_gre_fini(void)
163{
164 nf_nat_protocol_unregister(&gre);
165}
166
167module_init(nf_nat_proto_gre_init);
168module_exit(nf_nat_proto_gre_fini);
169
170void nf_nat_need_gre(void)
171{
172 return;
173}
174EXPORT_SYMBOL_GPL(nf_nat_need_gre);
This page took 0.125239 seconds and 5 git commands to generate.