]>
Commit | Line | Data |
---|---|---|
2736b84e | 1 | /* |
99e7b077 | 2 | * Copyright (c) 2007-2015 Nicira, Inc. |
2736b84e | 3 | * |
a9a29d22 JG |
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 | |
2736b84e JG |
17 | */ |
18 | ||
2be0b371 JG |
19 | #include <linux/kconfig.h> |
20 | #if IS_ENABLED(CONFIG_NET_IPGRE_DEMUX) | |
99e7b077 | 21 | |
dfffaef1 JP |
22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
23 | ||
d1eb60cc JG |
24 | #include <linux/if.h> |
25 | #include <linux/skbuff.h> | |
2736b84e JG |
26 | #include <linux/ip.h> |
27 | #include <linux/if_tunnel.h> | |
28 | #include <linux/if_vlan.h> | |
29 | #include <linux/in.h> | |
5ebaf571 PS |
30 | #include <linux/in_route.h> |
31 | #include <linux/inetdevice.h> | |
32 | #include <linux/jhash.h> | |
33 | #include <linux/list.h> | |
34 | #include <linux/kernel.h> | |
5a38795f | 35 | #include <linux/module.h> |
5ebaf571 PS |
36 | #include <linux/workqueue.h> |
37 | #include <linux/rculist.h> | |
38 | #include <net/route.h> | |
39 | #include <net/xfrm.h> | |
40 | ||
2736b84e | 41 | #include <net/icmp.h> |
2736b84e | 42 | #include <net/ip.h> |
5ebaf571 PS |
43 | #include <net/ip_tunnels.h> |
44 | #include <net/gre.h> | |
99e7b077 PS |
45 | #include <net/net_namespace.h> |
46 | #include <net/netns/generic.h> | |
2736b84e | 47 | #include <net/protocol.h> |
2736b84e | 48 | |
2a4999f3 | 49 | #include "datapath.h" |
2736b84e JG |
50 | #include "vport.h" |
51 | ||
5a38795f | 52 | static struct vport_ops ovs_gre_vport_ops; |
5a38795f | 53 | |
b9298d3f BP |
54 | /* Returns the least-significant 32 bits of a __be64. */ |
55 | static __be32 be64_get_low32(__be64 x) | |
56 | { | |
57 | #ifdef __BIG_ENDIAN | |
8dda8c9b | 58 | return (__force __be32)x; |
b9298d3f | 59 | #else |
8dda8c9b | 60 | return (__force __be32)((__force u64)x >> 32); |
b9298d3f BP |
61 | #endif |
62 | } | |
63 | ||
5ebaf571 | 64 | static __be16 filter_tnl_flags(__be16 flags) |
2de795ad | 65 | { |
5ebaf571 | 66 | return flags & (TUNNEL_CSUM | TUNNEL_KEY); |
2de795ad PS |
67 | } |
68 | ||
5ebaf571 | 69 | static struct sk_buff *__build_header(struct sk_buff *skb, |
99e7b077 | 70 | int tunnel_hlen) |
842cf6f4 | 71 | { |
5ebaf571 | 72 | struct tnl_ptk_info tpi; |
99e7b077 | 73 | const struct ovs_key_ipv4_tunnel *tun_key; |
2736b84e | 74 | |
fb66fbd1 | 75 | tun_key = &OVS_CB(skb)->egress_tun_info->tunnel; |
99e7b077 | 76 | |
5ebaf571 PS |
77 | skb = gre_handle_offloads(skb, !!(tun_key->tun_flags & TUNNEL_CSUM)); |
78 | if (IS_ERR(skb)) | |
93258bd7 | 79 | return skb; |
5214f5c4 | 80 | |
99e7b077 | 81 | tpi.flags = filter_tnl_flags(tun_key->tun_flags); |
5ebaf571 PS |
82 | tpi.proto = htons(ETH_P_TEB); |
83 | tpi.key = be64_get_low32(tun_key->tun_id); | |
99e7b077 | 84 | tpi.seq = 0; |
5ebaf571 PS |
85 | gre_build_header(skb, &tpi, tunnel_hlen); |
86 | ||
87 | return skb; | |
2736b84e JG |
88 | } |
89 | ||
2de795ad | 90 | static __be64 key_to_tunnel_id(__be32 key, __be32 seq) |
b9298d3f BP |
91 | { |
92 | #ifdef __BIG_ENDIAN | |
2de795ad | 93 | return (__force __be64)((__force u64)seq << 32 | (__force u32)key); |
b9298d3f | 94 | #else |
2de795ad | 95 | return (__force __be64)((__force u64)key << 32 | (__force u32)seq); |
b9298d3f BP |
96 | #endif |
97 | } | |
98 | ||
d1eb60cc | 99 | /* Called with rcu_read_lock and BH disabled. */ |
5ebaf571 PS |
100 | static int gre_rcv(struct sk_buff *skb, |
101 | const struct tnl_ptk_info *tpi) | |
2736b84e | 102 | { |
f0cd669f | 103 | struct ovs_tunnel_info tun_info; |
85c9de19 | 104 | struct ovs_net *ovs_net; |
2736b84e | 105 | struct vport *vport; |
b9298d3f | 106 | __be64 key; |
2736b84e | 107 | |
85c9de19 | 108 | ovs_net = net_generic(dev_net(skb->dev), ovs_net_id); |
99e7b077 | 109 | vport = rcu_dereference(ovs_net->vport_net.gre_vport); |
85c9de19 | 110 | if (unlikely(!vport)) |
5ebaf571 PS |
111 | return PACKET_REJECT; |
112 | ||
113 | key = key_to_tunnel_id(tpi->key, tpi->seq); | |
8b7ea2d4 | 114 | ovs_flow_tun_info_init(&tun_info, ip_hdr(skb), 0, 0, key, |
c1fc1411 | 115 | filter_tnl_flags(tpi->flags), NULL, 0); |
2736b84e | 116 | |
f0cd669f | 117 | ovs_vport_receive(vport, skb, &tun_info); |
5ebaf571 PS |
118 | return PACKET_RCVD; |
119 | } | |
120 | ||
43f31ef5 WZ |
121 | /* Called with rcu_read_lock and BH disabled. */ |
122 | static int gre_err(struct sk_buff *skb, u32 info, | |
123 | const struct tnl_ptk_info *tpi) | |
124 | { | |
125 | struct ovs_net *ovs_net; | |
126 | struct vport *vport; | |
127 | ||
128 | ovs_net = net_generic(dev_net(skb->dev), ovs_net_id); | |
99e7b077 | 129 | vport = rcu_dereference(ovs_net->vport_net.gre_vport); |
43f31ef5 WZ |
130 | |
131 | if (unlikely(!vport)) | |
132 | return PACKET_REJECT; | |
133 | else | |
134 | return PACKET_RCVD; | |
135 | } | |
136 | ||
99e7b077 | 137 | static int gre_tnl_send(struct vport *vport, struct sk_buff *skb) |
5ebaf571 | 138 | { |
99e7b077 | 139 | const struct ovs_key_ipv4_tunnel *tun_key; |
5ebaf571 PS |
140 | struct rtable *rt; |
141 | int min_headroom; | |
142 | __be16 df; | |
99e7b077 | 143 | int tunnel_hlen; |
5ebaf571 PS |
144 | __be32 saddr; |
145 | int err; | |
146 | ||
99e7b077 PS |
147 | if (unlikely(!OVS_CB(skb)->egress_tun_info)) { |
148 | err = -EINVAL; | |
149 | goto err_free_skb; | |
150 | } | |
151 | ||
fb66fbd1 | 152 | tun_key = &OVS_CB(skb)->egress_tun_info->tunnel; |
f0cd669f | 153 | saddr = tun_key->ipv4_src; |
5ebaf571 | 154 | rt = find_route(ovs_dp_get_net(vport->dp), |
f0cd669f JG |
155 | &saddr, tun_key->ipv4_dst, |
156 | IPPROTO_GRE, tun_key->ipv4_tos, | |
3025a772 | 157 | skb->mark); |
5ebaf571 PS |
158 | if (IS_ERR(rt)) { |
159 | err = PTR_ERR(rt); | |
99e7b077 | 160 | goto err_free_skb; |
5ebaf571 | 161 | } |
2736b84e | 162 | |
99e7b077 PS |
163 | tunnel_hlen = ip_gre_calc_hlen(tun_key->tun_flags); |
164 | ||
5ebaf571 PS |
165 | min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len |
166 | + tunnel_hlen + sizeof(struct iphdr) | |
efd8a18e | 167 | + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); |
5ebaf571 PS |
168 | if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) { |
169 | int head_delta = SKB_DATA_ALIGN(min_headroom - | |
170 | skb_headroom(skb) + | |
171 | 16); | |
172 | err = pskb_expand_head(skb, max_t(int, head_delta, 0), | |
173 | 0, GFP_ATOMIC); | |
174 | if (unlikely(err)) | |
175 | goto err_free_rt; | |
176 | } | |
2736b84e | 177 | |
99e7b077 PS |
178 | skb = vlan_hwaccel_push_inside(skb); |
179 | if (unlikely(!skb)) { | |
180 | err = -ENOMEM; | |
181 | goto err_free_rt; | |
5ebaf571 | 182 | } |
2736b84e | 183 | |
5ebaf571 | 184 | /* Push Tunnel header. */ |
99e7b077 | 185 | skb = __build_header(skb, tunnel_hlen); |
93258bd7 PS |
186 | if (IS_ERR(skb)) { |
187 | err = PTR_ERR(skb); | |
188 | skb = NULL; | |
5ebaf571 PS |
189 | goto err_free_rt; |
190 | } | |
191 | ||
99e7b077 PS |
192 | df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? |
193 | htons(IP_DF) : 0; | |
194 | ||
f6a0c895 | 195 | skb->ignore_df = 1; |
2736b84e | 196 | |
f6a0c895 | 197 | return iptunnel_xmit(skb->sk, rt, skb, saddr, |
f0cd669f | 198 | tun_key->ipv4_dst, IPPROTO_GRE, |
99e7b077 | 199 | tun_key->ipv4_tos, tun_key->ipv4_ttl, df, false); |
5ebaf571 PS |
200 | err_free_rt: |
201 | ip_rt_put(rt); | |
99e7b077 | 202 | err_free_skb: |
93258bd7 | 203 | kfree_skb(skb); |
5ebaf571 | 204 | return err; |
2736b84e JG |
205 | } |
206 | ||
5ebaf571 | 207 | static struct gre_cisco_protocol gre_protocol = { |
99e7b077 PS |
208 | .handler = gre_rcv, |
209 | .err_handler = gre_err, | |
210 | .priority = 1, | |
2736b84e JG |
211 | }; |
212 | ||
806b46ef | 213 | static int gre_ports; |
fceb2a5b | 214 | static int gre_init(void) |
2736b84e JG |
215 | { |
216 | int err; | |
217 | ||
806b46ef PS |
218 | gre_ports++; |
219 | if (gre_ports > 1) | |
2de795ad PS |
220 | return 0; |
221 | ||
5ebaf571 | 222 | err = gre_cisco_register(&gre_protocol); |
842cf6f4 | 223 | if (err) |
dfffaef1 | 224 | pr_warn("cannot register gre protocol handler\n"); |
2736b84e | 225 | |
2736b84e JG |
226 | return err; |
227 | } | |
228 | ||
d1eb60cc | 229 | static void gre_exit(void) |
2736b84e | 230 | { |
806b46ef PS |
231 | gre_ports--; |
232 | if (gre_ports > 0) | |
2de795ad PS |
233 | return; |
234 | ||
5ebaf571 | 235 | gre_cisco_unregister(&gre_protocol); |
2736b84e JG |
236 | } |
237 | ||
c405d282 PS |
238 | static const char *gre_get_name(const struct vport *vport) |
239 | { | |
240 | return vport_priv(vport); | |
241 | } | |
85c9de19 PS |
242 | |
243 | static struct vport *gre_create(const struct vport_parms *parms) | |
244 | { | |
245 | struct net *net = ovs_dp_get_net(parms->dp); | |
246 | struct ovs_net *ovs_net; | |
247 | struct vport *vport; | |
806b46ef PS |
248 | int err; |
249 | ||
250 | err = gre_init(); | |
251 | if (err) | |
252 | return ERR_PTR(err); | |
85c9de19 PS |
253 | |
254 | ovs_net = net_generic(net, ovs_net_id); | |
806b46ef PS |
255 | if (ovsl_dereference(ovs_net->vport_net.gre_vport)) { |
256 | vport = ERR_PTR(-EEXIST); | |
257 | goto error; | |
258 | } | |
85c9de19 | 259 | |
c405d282 PS |
260 | vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms); |
261 | if (IS_ERR(vport)) | |
806b46ef | 262 | goto error; |
85c9de19 | 263 | |
c405d282 | 264 | strncpy(vport_priv(vport), parms->name, IFNAMSIZ); |
85c9de19 PS |
265 | rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport); |
266 | return vport; | |
806b46ef PS |
267 | |
268 | error: | |
269 | gre_exit(); | |
270 | return vport; | |
85c9de19 PS |
271 | } |
272 | ||
273 | static void gre_tnl_destroy(struct vport *vport) | |
274 | { | |
275 | struct net *net = ovs_dp_get_net(vport->dp); | |
276 | struct ovs_net *ovs_net; | |
277 | ||
278 | ovs_net = net_generic(net, ovs_net_id); | |
279 | ||
317e49b4 | 280 | RCU_INIT_POINTER(ovs_net->vport_net.gre_vport, NULL); |
c405d282 | 281 | ovs_vport_deferred_free(vport); |
806b46ef | 282 | gre_exit(); |
c405d282 PS |
283 | } |
284 | ||
8b7ea2d4 WZ |
285 | static int gre_get_egress_tun_info(struct vport *vport, struct sk_buff *skb, |
286 | struct ovs_tunnel_info *egress_tun_info) | |
287 | { | |
288 | return ovs_tunnel_get_egress_info(egress_tun_info, | |
289 | ovs_dp_get_net(vport->dp), | |
290 | OVS_CB(skb)->egress_tun_info, | |
291 | IPPROTO_GRE, skb->mark, 0, 0); | |
292 | } | |
293 | ||
5a38795f | 294 | static struct vport_ops ovs_gre_vport_ops = { |
99e7b077 PS |
295 | .type = OVS_VPORT_TYPE_GRE, |
296 | .create = gre_create, | |
297 | .destroy = gre_tnl_destroy, | |
298 | .get_name = gre_get_name, | |
299 | .send = gre_tnl_send, | |
8b7ea2d4 | 300 | .get_egress_tun_info = gre_get_egress_tun_info, |
99e7b077 | 301 | .owner = THIS_MODULE, |
2de795ad | 302 | }; |
5a38795f TG |
303 | |
304 | static int __init ovs_gre_tnl_init(void) | |
305 | { | |
99e7b077 | 306 | return ovs_vport_ops_register(&ovs_gre_vport_ops); |
5a38795f TG |
307 | } |
308 | ||
309 | static void __exit ovs_gre_tnl_exit(void) | |
310 | { | |
5a38795f TG |
311 | ovs_vport_ops_unregister(&ovs_gre_vport_ops); |
312 | } | |
313 | ||
314 | module_init(ovs_gre_tnl_init); | |
315 | module_exit(ovs_gre_tnl_exit); | |
316 | ||
317 | MODULE_DESCRIPTION("OVS: GRE switching port"); | |
318 | MODULE_LICENSE("GPL"); | |
319 | MODULE_ALIAS("vport-type-3"); | |
2be0b371 | 320 | #endif |