]> git.proxmox.com Git - ovs.git/blob - datapath/linux/compat/gre.c
7f2b545566c2323e3c726b1c166ebba623590d17
[ovs.git] / datapath / linux / compat / gre.c
1 /*
2 * Copyright (c) 2007-2013 Nicira, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19 #include <linux/version.h>
20 #include <linux/kconfig.h>
21 #include <linux/module.h>
22 #include <linux/if.h>
23 #include <linux/if_tunnel.h>
24 #include <linux/icmp.h>
25 #include <linux/in.h>
26 #include <linux/ip.h>
27 #include <linux/kernel.h>
28 #include <linux/kmod.h>
29 #include <linux/netdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/spinlock.h>
32
33 #include <net/gre.h>
34 #include <net/icmp.h>
35 #include <net/protocol.h>
36 #include <net/route.h>
37 #include <net/xfrm.h>
38
39 #include "gso.h"
40
41 #ifndef USE_UPSTREAM_TUNNEL
42 #if IS_ENABLED(CONFIG_NET_IPGRE_DEMUX)
43
44 static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
45
46 int rpl_gre_add_protocol(const struct gre_protocol *proto, u8 version)
47 {
48 if (version >= GREPROTO_MAX)
49 return -EINVAL;
50
51 return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ?
52 0 : -EBUSY;
53 }
54 EXPORT_SYMBOL_GPL(rpl_gre_add_protocol);
55
56 int rpl_gre_del_protocol(const struct gre_protocol *proto, u8 version)
57 {
58 int ret;
59
60 if (version >= GREPROTO_MAX)
61 return -EINVAL;
62
63 ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ?
64 0 : -EBUSY;
65
66 if (ret)
67 return ret;
68
69 synchronize_rcu();
70 return 0;
71 }
72 EXPORT_SYMBOL_GPL(rpl_gre_del_protocol);
73
74 static int gre_rcv(struct sk_buff *skb)
75 {
76 const struct gre_protocol *proto;
77 u8 ver;
78 int ret;
79
80 if (!pskb_may_pull(skb, 12))
81 goto drop;
82
83 ver = skb->data[1]&0x7f;
84 if (ver >= GREPROTO_MAX)
85 goto drop;
86
87 rcu_read_lock();
88 proto = rcu_dereference(gre_proto[ver]);
89 if (!proto || !proto->handler)
90 goto drop_unlock;
91 ret = proto->handler(skb);
92 rcu_read_unlock();
93 return ret;
94
95 drop_unlock:
96 rcu_read_unlock();
97 drop:
98 kfree_skb(skb);
99 return NET_RX_DROP;
100 }
101
102 static void gre_err(struct sk_buff *skb, u32 info)
103 {
104 const struct gre_protocol *proto;
105 const struct iphdr *iph = (const struct iphdr *)skb->data;
106 u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
107
108 if (ver >= GREPROTO_MAX)
109 return;
110
111 rcu_read_lock();
112 proto = rcu_dereference(gre_proto[ver]);
113 if (proto && proto->err_handler)
114 proto->err_handler(skb, info);
115 rcu_read_unlock();
116 }
117
118 static const struct net_protocol net_gre_protocol = {
119 .handler = gre_rcv,
120 .err_handler = gre_err,
121 .netns_ok = 1,
122 };
123
124 int rpl_gre_init(void)
125 {
126 pr_info("GRE over IPv4 demultiplexor driver\n");
127
128 if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
129 pr_err("can't add protocol\n");
130 return -EAGAIN;
131 }
132 return 0;
133 }
134 EXPORT_SYMBOL_GPL(rpl_gre_init);
135
136 void rpl_gre_exit(void)
137 {
138 inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
139 }
140 EXPORT_SYMBOL_GPL(rpl_gre_exit);
141
142 #define ip_gre_calc_hlen rpl_ip_gre_calc_hlen
143 #define gre_calc_hlen rpl_ip_gre_calc_hlen
144 static int rpl_ip_gre_calc_hlen(__be16 o_flags)
145 {
146 int addend = 4;
147
148 if (o_flags & TUNNEL_CSUM)
149 addend += 4;
150 if (o_flags & TUNNEL_KEY)
151 addend += 4;
152 if (o_flags & TUNNEL_SEQ)
153 addend += 4;
154 return addend;
155 }
156
157 void rpl_gre_build_header(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
158 int hdr_len)
159 {
160 struct gre_base_hdr *greh;
161
162 skb_push(skb, hdr_len);
163
164 skb_reset_transport_header(skb);
165 greh = (struct gre_base_hdr *)skb->data;
166 greh->flags = tnl_flags_to_gre_flags(tpi->flags);
167 greh->protocol = tpi->proto;
168
169 if (tpi->flags&(TUNNEL_KEY|TUNNEL_CSUM|TUNNEL_SEQ)) {
170 __be32 *ptr = (__be32 *)(((u8 *)greh) + hdr_len - 4);
171
172 if (tpi->flags&TUNNEL_SEQ) {
173 *ptr = tpi->seq;
174 ptr--;
175 }
176 if (tpi->flags&TUNNEL_KEY) {
177 *ptr = tpi->key;
178 ptr--;
179 }
180 if (tpi->flags&TUNNEL_CSUM &&
181 !(skb_shinfo(skb)->gso_type &
182 (SKB_GSO_GRE|SKB_GSO_GRE_CSUM))) {
183 *ptr = 0;
184 *(__sum16 *)ptr = csum_fold(skb_checksum(skb, 0,
185 skb->len, 0));
186 }
187 }
188 }
189 EXPORT_SYMBOL_GPL(rpl_gre_build_header);
190
191 /* Fills in tpi and returns header length to be pulled. */
192 int rpl_gre_parse_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
193 bool *csum_err, __be16 proto, int nhs)
194 {
195 const struct gre_base_hdr *greh;
196 __be32 *options;
197 int hdr_len;
198
199 if (unlikely(!pskb_may_pull(skb, nhs + sizeof(struct gre_base_hdr))))
200 return -EINVAL;
201
202 greh = (struct gre_base_hdr *)(skb->data + nhs);
203 if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
204 return -EINVAL;
205
206 tpi->flags = gre_flags_to_tnl_flags(greh->flags);
207 hdr_len = gre_calc_hlen(tpi->flags);
208
209 if (!pskb_may_pull(skb, nhs + hdr_len))
210 return -EINVAL;
211
212 greh = (struct gre_base_hdr *)(skb->data + nhs);
213 tpi->proto = greh->protocol;
214
215 options = (__be32 *)(greh + 1);
216 if (greh->flags & GRE_CSUM) {
217 if (skb_checksum_simple_validate(skb)) {
218 *csum_err = true;
219 return -EINVAL;
220 }
221
222 skb_checksum_try_convert(skb, IPPROTO_GRE, 0,
223 null_compute_pseudo);
224 options++;
225 }
226
227 if (greh->flags & GRE_KEY) {
228 tpi->key = *options;
229 options++;
230 } else {
231 tpi->key = 0;
232 }
233 if (unlikely(greh->flags & GRE_SEQ)) {
234 tpi->seq = *options;
235 options++;
236 } else {
237 tpi->seq = 0;
238 }
239 /* WCCP version 1 and 2 protocol decoding.
240 * - Change protocol to IPv4/IPv6
241 * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
242 */
243 if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
244 tpi->proto = proto;
245 if ((*(u8 *)options & 0xF0) != 0x40)
246 hdr_len += 4;
247 }
248 tpi->hdr_len = hdr_len;
249 return hdr_len;
250 }
251 EXPORT_SYMBOL(rpl_gre_parse_header);
252
253 #endif /* CONFIG_NET_IPGRE_DEMUX */
254 #endif /* USE_UPSTREAM_TUNNEL */