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> |
| 32 | #include <linux/workqueue.h> |
| 33 | #include <linux/rculist.h> |
| 34 | #include <net/route.h> |
| 35 | #include <net/xfrm.h> |
| 36 | |
| 37 | #include <net/icmp.h> |
| 38 | #include <net/ip.h> |
| 39 | #include <net/ip_tunnels.h> |
| 40 | #include <net/gre.h> |
| 41 | #include <net/net_namespace.h> |
| 42 | #include <net/netns/generic.h> |
| 43 | #include <net/protocol.h> |
| 44 | |
| 45 | #include "datapath.h" |
| 46 | #include "vport.h" |
| 47 | |
| 48 | /* Returns the least-significant 32 bits of a __be64. */ |
| 49 | static __be32 be64_get_low32(__be64 x) |
| 50 | { |
| 51 | #ifdef __BIG_ENDIAN |
| 52 | return (__force __be32)x; |
| 53 | #else |
| 54 | return (__force __be32)((__force u64)x >> 32); |
| 55 | #endif |
| 56 | } |
| 57 | |
| 58 | static __be16 filter_tnl_flags(__be16 flags) |
| 59 | { |
| 60 | return flags & (TUNNEL_CSUM | TUNNEL_KEY); |
| 61 | } |
| 62 | |
| 63 | static struct sk_buff *__build_header(struct sk_buff *skb, |
| 64 | int tunnel_hlen) |
| 65 | { |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 66 | const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->egress_tun_key; |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 67 | struct tnl_ptk_info tpi; |
| 68 | |
| 69 | skb = gre_handle_offloads(skb, !!(tun_key->tun_flags & TUNNEL_CSUM)); |
| 70 | if (IS_ERR(skb)) |
| 71 | return NULL; |
| 72 | |
| 73 | tpi.flags = filter_tnl_flags(tun_key->tun_flags); |
| 74 | tpi.proto = htons(ETH_P_TEB); |
| 75 | tpi.key = be64_get_low32(tun_key->tun_id); |
| 76 | tpi.seq = 0; |
| 77 | gre_build_header(skb, &tpi, tunnel_hlen); |
| 78 | |
| 79 | return skb; |
| 80 | } |
| 81 | |
| 82 | static __be64 key_to_tunnel_id(__be32 key, __be32 seq) |
| 83 | { |
| 84 | #ifdef __BIG_ENDIAN |
| 85 | return (__force __be64)((__force u64)seq << 32 | (__force u32)key); |
| 86 | #else |
| 87 | return (__force __be64)((__force u64)key << 32 | (__force u32)seq); |
| 88 | #endif |
| 89 | } |
| 90 | |
| 91 | /* Called with rcu_read_lock and BH disabled. */ |
| 92 | static int gre_rcv(struct sk_buff *skb, |
| 93 | const struct tnl_ptk_info *tpi) |
| 94 | { |
| 95 | struct ovs_key_ipv4_tunnel tun_key; |
| 96 | struct ovs_net *ovs_net; |
| 97 | struct vport *vport; |
| 98 | __be64 key; |
| 99 | |
| 100 | ovs_net = net_generic(dev_net(skb->dev), ovs_net_id); |
| 101 | vport = rcu_dereference(ovs_net->vport_net.gre_vport); |
| 102 | if (unlikely(!vport)) |
| 103 | return PACKET_REJECT; |
| 104 | |
| 105 | key = key_to_tunnel_id(tpi->key, tpi->seq); |
| 106 | ovs_flow_tun_key_init(&tun_key, ip_hdr(skb), key, |
| 107 | filter_tnl_flags(tpi->flags)); |
| 108 | |
| 109 | ovs_vport_receive(vport, skb, &tun_key); |
| 110 | return PACKET_RCVD; |
| 111 | } |
| 112 | |
Wei Zhang | e0bb8c4 | 2014-06-28 12:34:53 -0700 | [diff] [blame] | 113 | /* Called with rcu_read_lock and BH disabled. */ |
| 114 | static int gre_err(struct sk_buff *skb, u32 info, |
| 115 | const struct tnl_ptk_info *tpi) |
| 116 | { |
| 117 | struct ovs_net *ovs_net; |
| 118 | struct vport *vport; |
| 119 | |
| 120 | ovs_net = net_generic(dev_net(skb->dev), ovs_net_id); |
| 121 | vport = rcu_dereference(ovs_net->vport_net.gre_vport); |
| 122 | |
| 123 | if (unlikely(!vport)) |
| 124 | return PACKET_REJECT; |
| 125 | else |
| 126 | return PACKET_RCVD; |
| 127 | } |
| 128 | |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 129 | static int gre_tnl_send(struct vport *vport, struct sk_buff *skb) |
| 130 | { |
| 131 | struct net *net = ovs_dp_get_net(vport->dp); |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 132 | struct ovs_key_ipv4_tunnel *tun_key; |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 133 | struct flowi4 fl; |
| 134 | struct rtable *rt; |
| 135 | int min_headroom; |
| 136 | int tunnel_hlen; |
| 137 | __be16 df; |
| 138 | int err; |
| 139 | |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 140 | if (unlikely(!OVS_CB(skb)->egress_tun_key)) { |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 141 | err = -EINVAL; |
| 142 | goto error; |
| 143 | } |
| 144 | |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 145 | tun_key = OVS_CB(skb)->egress_tun_key; |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 146 | /* Route lookup */ |
| 147 | memset(&fl, 0, sizeof(fl)); |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 148 | fl.daddr = tun_key->ipv4_dst; |
| 149 | fl.saddr = tun_key->ipv4_src; |
| 150 | fl.flowi4_tos = RT_TOS(tun_key->ipv4_tos); |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 151 | fl.flowi4_mark = skb->mark; |
| 152 | fl.flowi4_proto = IPPROTO_GRE; |
| 153 | |
| 154 | rt = ip_route_output_key(net, &fl); |
| 155 | if (IS_ERR(rt)) |
| 156 | return PTR_ERR(rt); |
| 157 | |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 158 | tunnel_hlen = ip_gre_calc_hlen(tun_key->tun_flags); |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 159 | |
| 160 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len |
| 161 | + tunnel_hlen + sizeof(struct iphdr) |
| 162 | + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0); |
| 163 | if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) { |
| 164 | int head_delta = SKB_DATA_ALIGN(min_headroom - |
| 165 | skb_headroom(skb) + |
| 166 | 16); |
| 167 | err = pskb_expand_head(skb, max_t(int, head_delta, 0), |
| 168 | 0, GFP_ATOMIC); |
| 169 | if (unlikely(err)) |
| 170 | goto err_free_rt; |
| 171 | } |
| 172 | |
| 173 | if (vlan_tx_tag_present(skb)) { |
| 174 | if (unlikely(!__vlan_put_tag(skb, |
| 175 | skb->vlan_proto, |
| 176 | vlan_tx_tag_get(skb)))) { |
| 177 | err = -ENOMEM; |
| 178 | goto err_free_rt; |
| 179 | } |
| 180 | skb->vlan_tci = 0; |
| 181 | } |
| 182 | |
| 183 | /* Push Tunnel header. */ |
| 184 | skb = __build_header(skb, tunnel_hlen); |
| 185 | if (unlikely(!skb)) { |
| 186 | err = 0; |
| 187 | goto err_free_rt; |
| 188 | } |
| 189 | |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 190 | df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 191 | htons(IP_DF) : 0; |
| 192 | |
WANG Cong | 60ff746 | 2014-05-04 16:39:18 -0700 | [diff] [blame] | 193 | skb->ignore_df = 1; |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 194 | |
Eric Dumazet | aad8872 | 2014-04-15 13:47:15 -0400 | [diff] [blame] | 195 | return iptunnel_xmit(skb->sk, rt, skb, fl.saddr, |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 196 | tun_key->ipv4_dst, IPPROTO_GRE, |
| 197 | tun_key->ipv4_tos, tun_key->ipv4_ttl, df, false); |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 198 | err_free_rt: |
| 199 | ip_rt_put(rt); |
| 200 | error: |
| 201 | return err; |
| 202 | } |
| 203 | |
| 204 | static struct gre_cisco_protocol gre_protocol = { |
| 205 | .handler = gre_rcv, |
Wei Zhang | e0bb8c4 | 2014-06-28 12:34:53 -0700 | [diff] [blame] | 206 | .err_handler = gre_err, |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 207 | .priority = 1, |
| 208 | }; |
| 209 | |
| 210 | static int gre_ports; |
| 211 | static int gre_init(void) |
| 212 | { |
| 213 | int err; |
| 214 | |
| 215 | gre_ports++; |
| 216 | if (gre_ports > 1) |
| 217 | return 0; |
| 218 | |
| 219 | err = gre_cisco_register(&gre_protocol); |
| 220 | if (err) |
| 221 | pr_warn("cannot register gre protocol handler\n"); |
| 222 | |
| 223 | return err; |
| 224 | } |
| 225 | |
| 226 | static void gre_exit(void) |
| 227 | { |
| 228 | gre_ports--; |
| 229 | if (gre_ports > 0) |
| 230 | return; |
| 231 | |
| 232 | gre_cisco_unregister(&gre_protocol); |
| 233 | } |
| 234 | |
| 235 | static const char *gre_get_name(const struct vport *vport) |
| 236 | { |
| 237 | return vport_priv(vport); |
| 238 | } |
| 239 | |
| 240 | static struct vport *gre_create(const struct vport_parms *parms) |
| 241 | { |
| 242 | struct net *net = ovs_dp_get_net(parms->dp); |
| 243 | struct ovs_net *ovs_net; |
| 244 | struct vport *vport; |
| 245 | int err; |
| 246 | |
| 247 | err = gre_init(); |
| 248 | if (err) |
| 249 | return ERR_PTR(err); |
| 250 | |
| 251 | ovs_net = net_generic(net, ovs_net_id); |
| 252 | if (ovsl_dereference(ovs_net->vport_net.gre_vport)) { |
| 253 | vport = ERR_PTR(-EEXIST); |
| 254 | goto error; |
| 255 | } |
| 256 | |
| 257 | vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms); |
| 258 | if (IS_ERR(vport)) |
| 259 | goto error; |
| 260 | |
| 261 | strncpy(vport_priv(vport), parms->name, IFNAMSIZ); |
| 262 | rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport); |
| 263 | return vport; |
| 264 | |
| 265 | error: |
| 266 | gre_exit(); |
| 267 | return vport; |
| 268 | } |
| 269 | |
| 270 | static void gre_tnl_destroy(struct vport *vport) |
| 271 | { |
| 272 | struct net *net = ovs_dp_get_net(vport->dp); |
| 273 | struct ovs_net *ovs_net; |
| 274 | |
| 275 | ovs_net = net_generic(net, ovs_net_id); |
| 276 | |
Monam Agarwal | 944df8a | 2014-03-24 00:52:43 +0530 | [diff] [blame] | 277 | RCU_INIT_POINTER(ovs_net->vport_net.gre_vport, NULL); |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 278 | ovs_vport_deferred_free(vport); |
| 279 | gre_exit(); |
| 280 | } |
| 281 | |
| 282 | const struct vport_ops ovs_gre_vport_ops = { |
| 283 | .type = OVS_VPORT_TYPE_GRE, |
| 284 | .create = gre_create, |
| 285 | .destroy = gre_tnl_destroy, |
| 286 | .get_name = gre_get_name, |
| 287 | .send = gre_tnl_send, |
| 288 | }; |