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