xref: /openbmc/linux/net/ipv4/gre_demux.c (revision cb73ee40b1b381eaf3749e6dbeed567bb38e5258)
1c50cd357SDaniel Borkmann /*
2c50cd357SDaniel Borkmann  *	GRE over IPv4 demultiplexer driver
3c50cd357SDaniel Borkmann  *
4c50cd357SDaniel Borkmann  *	Authors: Dmitry Kozlov (xeb@mail.ru)
5c50cd357SDaniel Borkmann  *
6c50cd357SDaniel Borkmann  *	This program is free software; you can redistribute it and/or
7c50cd357SDaniel Borkmann  *	modify it under the terms of the GNU General Public License
8c50cd357SDaniel Borkmann  *	as published by the Free Software Foundation; either version
9c50cd357SDaniel Borkmann  *	2 of the License, or (at your option) any later version.
10c50cd357SDaniel Borkmann  *
11c50cd357SDaniel Borkmann  */
12c50cd357SDaniel Borkmann 
13c50cd357SDaniel Borkmann #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14c50cd357SDaniel Borkmann 
15c50cd357SDaniel Borkmann #include <linux/module.h>
16c50cd357SDaniel Borkmann #include <linux/if.h>
17c50cd357SDaniel Borkmann #include <linux/icmp.h>
18c50cd357SDaniel Borkmann #include <linux/kernel.h>
19c50cd357SDaniel Borkmann #include <linux/kmod.h>
20c50cd357SDaniel Borkmann #include <linux/skbuff.h>
21c50cd357SDaniel Borkmann #include <linux/in.h>
22c50cd357SDaniel Borkmann #include <linux/ip.h>
23c50cd357SDaniel Borkmann #include <linux/netdevice.h>
24c50cd357SDaniel Borkmann #include <linux/if_tunnel.h>
25c50cd357SDaniel Borkmann #include <linux/spinlock.h>
26c50cd357SDaniel Borkmann #include <net/protocol.h>
27c50cd357SDaniel Borkmann #include <net/gre.h>
28*cb73ee40SLorenzo Bianconi #include <net/erspan.h>
29c50cd357SDaniel Borkmann 
30c50cd357SDaniel Borkmann #include <net/icmp.h>
31c50cd357SDaniel Borkmann #include <net/route.h>
32c50cd357SDaniel Borkmann #include <net/xfrm.h>
33c50cd357SDaniel Borkmann 
34c50cd357SDaniel Borkmann static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
35c50cd357SDaniel Borkmann 
36c50cd357SDaniel Borkmann int gre_add_protocol(const struct gre_protocol *proto, u8 version)
37c50cd357SDaniel Borkmann {
38c50cd357SDaniel Borkmann 	if (version >= GREPROTO_MAX)
39c50cd357SDaniel Borkmann 		return -EINVAL;
40c50cd357SDaniel Borkmann 
41c50cd357SDaniel Borkmann 	return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ?
42c50cd357SDaniel Borkmann 		0 : -EBUSY;
43c50cd357SDaniel Borkmann }
44c50cd357SDaniel Borkmann EXPORT_SYMBOL_GPL(gre_add_protocol);
45c50cd357SDaniel Borkmann 
46c50cd357SDaniel Borkmann int gre_del_protocol(const struct gre_protocol *proto, u8 version)
47c50cd357SDaniel Borkmann {
48c50cd357SDaniel Borkmann 	int ret;
49c50cd357SDaniel Borkmann 
50c50cd357SDaniel Borkmann 	if (version >= GREPROTO_MAX)
51c50cd357SDaniel Borkmann 		return -EINVAL;
52c50cd357SDaniel Borkmann 
53c50cd357SDaniel Borkmann 	ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ?
54c50cd357SDaniel Borkmann 		0 : -EBUSY;
55c50cd357SDaniel Borkmann 
56c50cd357SDaniel Borkmann 	if (ret)
57c50cd357SDaniel Borkmann 		return ret;
58c50cd357SDaniel Borkmann 
59c50cd357SDaniel Borkmann 	synchronize_rcu();
60c50cd357SDaniel Borkmann 	return 0;
61c50cd357SDaniel Borkmann }
62c50cd357SDaniel Borkmann EXPORT_SYMBOL_GPL(gre_del_protocol);
63c50cd357SDaniel Borkmann 
64f132ae7cSJiri Benc /* Fills in tpi and returns header length to be pulled. */
6595f5c64cSTom Herbert int gre_parse_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
66e582615aSEric Dumazet 		     bool *csum_err, __be16 proto, int nhs)
6795f5c64cSTom Herbert {
6895f5c64cSTom Herbert 	const struct gre_base_hdr *greh;
6995f5c64cSTom Herbert 	__be32 *options;
7095f5c64cSTom Herbert 	int hdr_len;
7195f5c64cSTom Herbert 
72e582615aSEric Dumazet 	if (unlikely(!pskb_may_pull(skb, nhs + sizeof(struct gre_base_hdr))))
7395f5c64cSTom Herbert 		return -EINVAL;
7495f5c64cSTom Herbert 
75e582615aSEric Dumazet 	greh = (struct gre_base_hdr *)(skb->data + nhs);
7695f5c64cSTom Herbert 	if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
7795f5c64cSTom Herbert 		return -EINVAL;
7895f5c64cSTom Herbert 
7995f5c64cSTom Herbert 	tpi->flags = gre_flags_to_tnl_flags(greh->flags);
8095f5c64cSTom Herbert 	hdr_len = gre_calc_hlen(tpi->flags);
8195f5c64cSTom Herbert 
82e582615aSEric Dumazet 	if (!pskb_may_pull(skb, nhs + hdr_len))
8395f5c64cSTom Herbert 		return -EINVAL;
8495f5c64cSTom Herbert 
85e582615aSEric Dumazet 	greh = (struct gre_base_hdr *)(skb->data + nhs);
8695f5c64cSTom Herbert 	tpi->proto = greh->protocol;
8795f5c64cSTom Herbert 
8895f5c64cSTom Herbert 	options = (__be32 *)(greh + 1);
8995f5c64cSTom Herbert 	if (greh->flags & GRE_CSUM) {
90b0350d51SHaishuang Yan 		if (!skb_checksum_simple_validate(skb)) {
91b0350d51SHaishuang Yan 			skb_checksum_try_convert(skb, IPPROTO_GRE, 0,
92b0350d51SHaishuang Yan 						 null_compute_pseudo);
93b0350d51SHaishuang Yan 		} else if (csum_err) {
9495f5c64cSTom Herbert 			*csum_err = true;
9595f5c64cSTom Herbert 			return -EINVAL;
9695f5c64cSTom Herbert 		}
9795f5c64cSTom Herbert 
9895f5c64cSTom Herbert 		options++;
9995f5c64cSTom Herbert 	}
10095f5c64cSTom Herbert 
10195f5c64cSTom Herbert 	if (greh->flags & GRE_KEY) {
10295f5c64cSTom Herbert 		tpi->key = *options;
10395f5c64cSTom Herbert 		options++;
10495f5c64cSTom Herbert 	} else {
10595f5c64cSTom Herbert 		tpi->key = 0;
10695f5c64cSTom Herbert 	}
10795f5c64cSTom Herbert 	if (unlikely(greh->flags & GRE_SEQ)) {
10895f5c64cSTom Herbert 		tpi->seq = *options;
10995f5c64cSTom Herbert 		options++;
11095f5c64cSTom Herbert 	} else {
11195f5c64cSTom Herbert 		tpi->seq = 0;
11295f5c64cSTom Herbert 	}
11395f5c64cSTom Herbert 	/* WCCP version 1 and 2 protocol decoding.
114da73b4e9SHaishuang Yan 	 * - Change protocol to IPv4/IPv6
11595f5c64cSTom Herbert 	 * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
11695f5c64cSTom Herbert 	 */
11795f5c64cSTom Herbert 	if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
118da73b4e9SHaishuang Yan 		tpi->proto = proto;
11900b20340SJiri Benc 		if ((*(u8 *)options & 0xF0) != 0x40)
12095f5c64cSTom Herbert 			hdr_len += 4;
12195f5c64cSTom Herbert 	}
1229b8c6d7bSEric Dumazet 	tpi->hdr_len = hdr_len;
123*cb73ee40SLorenzo Bianconi 
124*cb73ee40SLorenzo Bianconi 	/* ERSPAN ver 1 and 2 protocol sets GRE key field
125*cb73ee40SLorenzo Bianconi 	 * to 0 and sets the configured key in the
126*cb73ee40SLorenzo Bianconi 	 * inner erspan header field
127*cb73ee40SLorenzo Bianconi 	 */
128*cb73ee40SLorenzo Bianconi 	if (greh->protocol == htons(ETH_P_ERSPAN) ||
129*cb73ee40SLorenzo Bianconi 	    greh->protocol == htons(ETH_P_ERSPAN2)) {
130*cb73ee40SLorenzo Bianconi 		struct erspan_base_hdr *ershdr;
131*cb73ee40SLorenzo Bianconi 
132*cb73ee40SLorenzo Bianconi 		if (!pskb_may_pull(skb, nhs + hdr_len + sizeof(*ershdr)))
133*cb73ee40SLorenzo Bianconi 			return -EINVAL;
134*cb73ee40SLorenzo Bianconi 
135*cb73ee40SLorenzo Bianconi 		ershdr = (struct erspan_base_hdr *)options;
136*cb73ee40SLorenzo Bianconi 		tpi->key = cpu_to_be32(get_session_id(ershdr));
137*cb73ee40SLorenzo Bianconi 	}
138*cb73ee40SLorenzo Bianconi 
139f132ae7cSJiri Benc 	return hdr_len;
14095f5c64cSTom Herbert }
14195f5c64cSTom Herbert EXPORT_SYMBOL(gre_parse_header);
14295f5c64cSTom Herbert 
143c50cd357SDaniel Borkmann static int gre_rcv(struct sk_buff *skb)
144c50cd357SDaniel Borkmann {
145c50cd357SDaniel Borkmann 	const struct gre_protocol *proto;
146c50cd357SDaniel Borkmann 	u8 ver;
147c50cd357SDaniel Borkmann 	int ret;
148c50cd357SDaniel Borkmann 
149c50cd357SDaniel Borkmann 	if (!pskb_may_pull(skb, 12))
150c50cd357SDaniel Borkmann 		goto drop;
151c50cd357SDaniel Borkmann 
152c50cd357SDaniel Borkmann 	ver = skb->data[1]&0x7f;
153c50cd357SDaniel Borkmann 	if (ver >= GREPROTO_MAX)
154c50cd357SDaniel Borkmann 		goto drop;
155c50cd357SDaniel Borkmann 
156c50cd357SDaniel Borkmann 	rcu_read_lock();
157c50cd357SDaniel Borkmann 	proto = rcu_dereference(gre_proto[ver]);
158c50cd357SDaniel Borkmann 	if (!proto || !proto->handler)
159c50cd357SDaniel Borkmann 		goto drop_unlock;
160c50cd357SDaniel Borkmann 	ret = proto->handler(skb);
161c50cd357SDaniel Borkmann 	rcu_read_unlock();
162c50cd357SDaniel Borkmann 	return ret;
163c50cd357SDaniel Borkmann 
164c50cd357SDaniel Borkmann drop_unlock:
165c50cd357SDaniel Borkmann 	rcu_read_unlock();
166c50cd357SDaniel Borkmann drop:
167c50cd357SDaniel Borkmann 	kfree_skb(skb);
168c50cd357SDaniel Borkmann 	return NET_RX_DROP;
169c50cd357SDaniel Borkmann }
170c50cd357SDaniel Borkmann 
17132bbd879SStefano Brivio static int gre_err(struct sk_buff *skb, u32 info)
172c50cd357SDaniel Borkmann {
173c50cd357SDaniel Borkmann 	const struct gre_protocol *proto;
174c50cd357SDaniel Borkmann 	const struct iphdr *iph = (const struct iphdr *)skb->data;
175c50cd357SDaniel Borkmann 	u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
17632bbd879SStefano Brivio 	int err = 0;
177c50cd357SDaniel Borkmann 
178c50cd357SDaniel Borkmann 	if (ver >= GREPROTO_MAX)
17932bbd879SStefano Brivio 		return -EINVAL;
180c50cd357SDaniel Borkmann 
181c50cd357SDaniel Borkmann 	rcu_read_lock();
182c50cd357SDaniel Borkmann 	proto = rcu_dereference(gre_proto[ver]);
183c50cd357SDaniel Borkmann 	if (proto && proto->err_handler)
184c50cd357SDaniel Borkmann 		proto->err_handler(skb, info);
18532bbd879SStefano Brivio 	else
18632bbd879SStefano Brivio 		err = -EPROTONOSUPPORT;
187c50cd357SDaniel Borkmann 	rcu_read_unlock();
18832bbd879SStefano Brivio 
18932bbd879SStefano Brivio 	return err;
190c50cd357SDaniel Borkmann }
191c50cd357SDaniel Borkmann 
192c50cd357SDaniel Borkmann static const struct net_protocol net_gre_protocol = {
193c50cd357SDaniel Borkmann 	.handler     = gre_rcv,
194c50cd357SDaniel Borkmann 	.err_handler = gre_err,
195c50cd357SDaniel Borkmann 	.netns_ok    = 1,
196c50cd357SDaniel Borkmann };
197c50cd357SDaniel Borkmann 
198c50cd357SDaniel Borkmann static int __init gre_init(void)
199c50cd357SDaniel Borkmann {
200c50cd357SDaniel Borkmann 	pr_info("GRE over IPv4 demultiplexor driver\n");
201c50cd357SDaniel Borkmann 
202c50cd357SDaniel Borkmann 	if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
203c50cd357SDaniel Borkmann 		pr_err("can't add protocol\n");
204c50cd357SDaniel Borkmann 		return -EAGAIN;
205c50cd357SDaniel Borkmann 	}
2069f57c67cSPravin B Shelar 	return 0;
2079f57c67cSPravin B Shelar }
208c50cd357SDaniel Borkmann 
209c50cd357SDaniel Borkmann static void __exit gre_exit(void)
210c50cd357SDaniel Borkmann {
211c50cd357SDaniel Borkmann 	inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
212c50cd357SDaniel Borkmann }
213c50cd357SDaniel Borkmann 
214c50cd357SDaniel Borkmann module_init(gre_init);
215c50cd357SDaniel Borkmann module_exit(gre_exit);
216c50cd357SDaniel Borkmann 
217c50cd357SDaniel Borkmann MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
218c50cd357SDaniel Borkmann MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
219c50cd357SDaniel Borkmann MODULE_LICENSE("GPL");
220