]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/ipv4/gre_demux.c
openvswitch: Use regular GRE net_device instead of vport
[mirror_ubuntu-artful-kernel.git] / net / ipv4 / gre_demux.c
CommitLineData
00959ade
DK
1/*
2 * GRE over IPv4 demultiplexer driver
3 *
4 * Authors: Dmitry Kozlov (xeb@mail.ru)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
afd46503
JP
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
00959ade 15#include <linux/module.h>
bda7bb46
PS
16#include <linux/if.h>
17#include <linux/icmp.h>
00959ade
DK
18#include <linux/kernel.h>
19#include <linux/kmod.h>
20#include <linux/skbuff.h>
21#include <linux/in.h>
559fafb9 22#include <linux/ip.h>
00959ade 23#include <linux/netdevice.h>
68c33163 24#include <linux/if_tunnel.h>
00959ade
DK
25#include <linux/spinlock.h>
26#include <net/protocol.h>
27#include <net/gre.h>
28
bda7bb46
PS
29#include <net/icmp.h>
30#include <net/route.h>
31#include <net/xfrm.h>
00959ade 32
6f0bcf15 33static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
bda7bb46 34static struct gre_cisco_protocol __rcu *gre_cisco_proto_list[GRE_IP_PROTO_MAX];
00959ade
DK
35
36int gre_add_protocol(const struct gre_protocol *proto, u8 version)
37{
38 if (version >= GREPROTO_MAX)
20fd4d1f 39 return -EINVAL;
00959ade 40
20fd4d1f
PS
41 return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ?
42 0 : -EBUSY;
00959ade
DK
43}
44EXPORT_SYMBOL_GPL(gre_add_protocol);
45
46int gre_del_protocol(const struct gre_protocol *proto, u8 version)
47{
20fd4d1f
PS
48 int ret;
49
00959ade 50 if (version >= GREPROTO_MAX)
20fd4d1f
PS
51 return -EINVAL;
52
53 ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ?
54 0 : -EBUSY;
55
56 if (ret)
57 return ret;
58
00959ade
DK
59 synchronize_rcu();
60 return 0;
00959ade
DK
61}
62EXPORT_SYMBOL_GPL(gre_del_protocol);
63
bda7bb46
PS
64static int parse_gre_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
65 bool *csum_err)
66{
bda7bb46
PS
67 const struct gre_base_hdr *greh;
68 __be32 *options;
69 int hdr_len;
70
71 if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr))))
72 return -EINVAL;
73
1e701f16 74 greh = (struct gre_base_hdr *)skb_transport_header(skb);
bda7bb46
PS
75 if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
76 return -EINVAL;
77
78 tpi->flags = gre_flags_to_tnl_flags(greh->flags);
79 hdr_len = ip_gre_calc_hlen(tpi->flags);
80
81 if (!pskb_may_pull(skb, hdr_len))
82 return -EINVAL;
83
1e701f16 84 greh = (struct gre_base_hdr *)skb_transport_header(skb);
bda7bb46
PS
85 tpi->proto = greh->protocol;
86
87 options = (__be32 *)(greh + 1);
88 if (greh->flags & GRE_CSUM) {
b1036c6a 89 if (skb_checksum_simple_validate(skb)) {
bda7bb46
PS
90 *csum_err = true;
91 return -EINVAL;
92 }
884d338c
TH
93
94 skb_checksum_try_convert(skb, IPPROTO_GRE, 0,
95 null_compute_pseudo);
96
bda7bb46
PS
97 options++;
98 }
99
100 if (greh->flags & GRE_KEY) {
101 tpi->key = *options;
102 options++;
103 } else
104 tpi->key = 0;
105
106 if (unlikely(greh->flags & GRE_SEQ)) {
107 tpi->seq = *options;
108 options++;
109 } else
110 tpi->seq = 0;
111
112 /* WCCP version 1 and 2 protocol decoding.
113 * - Change protocol to IP
114 * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
115 */
116 if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
117 tpi->proto = htons(ETH_P_IP);
118 if ((*(u8 *)options & 0xF0) != 0x40) {
119 hdr_len += 4;
120 if (!pskb_may_pull(skb, hdr_len))
121 return -EINVAL;
122 }
123 }
3d7b46cd
PS
124
125 return iptunnel_pull_header(skb, hdr_len, tpi->proto);
bda7bb46
PS
126}
127
128static int gre_cisco_rcv(struct sk_buff *skb)
129{
130 struct tnl_ptk_info tpi;
131 int i;
132 bool csum_err = false;
133
fbd02dd4
PS
134#ifdef CONFIG_NET_IPGRE_BROADCAST
135 if (ipv4_is_multicast(ip_hdr(skb)->daddr)) {
136 /* Looped back packet, drop it! */
137 if (rt_is_output_route(skb_rtable(skb)))
138 goto drop;
139 }
140#endif
141
bda7bb46
PS
142 if (parse_gre_header(skb, &tpi, &csum_err) < 0)
143 goto drop;
144
145 rcu_read_lock();
146 for (i = 0; i < GRE_IP_PROTO_MAX; i++) {
147 struct gre_cisco_protocol *proto;
148 int ret;
149
150 proto = rcu_dereference(gre_cisco_proto_list[i]);
151 if (!proto)
152 continue;
153 ret = proto->handler(skb, &tpi);
154 if (ret == PACKET_RCVD) {
155 rcu_read_unlock();
156 return 0;
157 }
158 }
159 rcu_read_unlock();
160
161 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
162drop:
163 kfree_skb(skb);
164 return 0;
165}
166
167static void gre_cisco_err(struct sk_buff *skb, u32 info)
168{
169 /* All the routers (except for Linux) return only
170 * 8 bytes of packet payload. It means, that precise relaying of
171 * ICMP in the real Internet is absolutely infeasible.
172 *
173 * Moreover, Cisco "wise men" put GRE key to the third word
174 * in GRE header. It makes impossible maintaining even soft
175 * state for keyed
176 * GRE tunnels with enabled checksum. Tell them "thank you".
177 *
178 * Well, I wonder, rfc1812 was written by Cisco employee,
179 * what the hell these idiots break standards established
180 * by themselves???
181 */
182
183 const int type = icmp_hdr(skb)->type;
184 const int code = icmp_hdr(skb)->code;
185 struct tnl_ptk_info tpi;
186 bool csum_err = false;
187 int i;
188
189 if (parse_gre_header(skb, &tpi, &csum_err)) {
190 if (!csum_err) /* ignore csum errors. */
191 return;
192 }
193
194 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
195 ipv4_update_pmtu(skb, dev_net(skb->dev), info,
196 skb->dev->ifindex, 0, IPPROTO_GRE, 0);
197 return;
198 }
199 if (type == ICMP_REDIRECT) {
200 ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex, 0,
201 IPPROTO_GRE, 0);
202 return;
203 }
204
205 rcu_read_lock();
206 for (i = 0; i < GRE_IP_PROTO_MAX; i++) {
207 struct gre_cisco_protocol *proto;
208
209 proto = rcu_dereference(gre_cisco_proto_list[i]);
210 if (!proto)
211 continue;
212
213 if (proto->err_handler(skb, info, &tpi) == PACKET_RCVD)
214 goto out;
215
216 }
217out:
218 rcu_read_unlock();
219}
220
00959ade
DK
221static int gre_rcv(struct sk_buff *skb)
222{
223 const struct gre_protocol *proto;
224 u8 ver;
225 int ret;
226
227 if (!pskb_may_pull(skb, 12))
228 goto drop;
229
230 ver = skb->data[1]&0x7f;
231 if (ver >= GREPROTO_MAX)
232 goto drop;
233
234 rcu_read_lock();
235 proto = rcu_dereference(gre_proto[ver]);
236 if (!proto || !proto->handler)
237 goto drop_unlock;
238 ret = proto->handler(skb);
239 rcu_read_unlock();
240 return ret;
241
242drop_unlock:
243 rcu_read_unlock();
244drop:
245 kfree_skb(skb);
246 return NET_RX_DROP;
247}
248
249static void gre_err(struct sk_buff *skb, u32 info)
250{
251 const struct gre_protocol *proto;
559fafb9 252 const struct iphdr *iph = (const struct iphdr *)skb->data;
253 u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
00959ade 254
00959ade 255 if (ver >= GREPROTO_MAX)
559fafb9 256 return;
00959ade
DK
257
258 rcu_read_lock();
259 proto = rcu_dereference(gre_proto[ver]);
559fafb9 260 if (proto && proto->err_handler)
261 proto->err_handler(skb, info);
00959ade 262 rcu_read_unlock();
00959ade
DK
263}
264
265static const struct net_protocol net_gre_protocol = {
266 .handler = gre_rcv,
267 .err_handler = gre_err,
268 .netns_ok = 1,
269};
270
bda7bb46
PS
271static const struct gre_protocol ipgre_protocol = {
272 .handler = gre_cisco_rcv,
273 .err_handler = gre_cisco_err,
274};
275
276int gre_cisco_register(struct gre_cisco_protocol *newp)
277{
278 struct gre_cisco_protocol **proto = (struct gre_cisco_protocol **)
279 &gre_cisco_proto_list[newp->priority];
280
281 return (cmpxchg(proto, NULL, newp) == NULL) ? 0 : -EBUSY;
282}
283EXPORT_SYMBOL_GPL(gre_cisco_register);
284
285int gre_cisco_unregister(struct gre_cisco_protocol *del_proto)
286{
287 struct gre_cisco_protocol **proto = (struct gre_cisco_protocol **)
288 &gre_cisco_proto_list[del_proto->priority];
289 int ret;
290
291 ret = (cmpxchg(proto, del_proto, NULL) == del_proto) ? 0 : -EINVAL;
292
293 if (ret)
294 return ret;
295
296 synchronize_net();
297 return 0;
298}
299EXPORT_SYMBOL_GPL(gre_cisco_unregister);
300
00959ade
DK
301static int __init gre_init(void)
302{
afd46503 303 pr_info("GRE over IPv4 demultiplexor driver\n");
00959ade
DK
304
305 if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
afd46503 306 pr_err("can't add protocol\n");
bda7bb46
PS
307 goto err;
308 }
309
310 if (gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO) < 0) {
311 pr_info("%s: can't add ipgre handler\n", __func__);
312 goto err_gre;
00959ade
DK
313 }
314
315 return 0;
bda7bb46
PS
316err_gre:
317 inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
318err:
319 return -EAGAIN;
00959ade
DK
320}
321
322static void __exit gre_exit(void)
323{
bda7bb46 324 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
00959ade
DK
325 inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
326}
327
328module_init(gre_init);
329module_exit(gre_exit);
330
331MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
332MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
333MODULE_LICENSE("GPL");