ofproto: Fix use-after-free error when ports disappear.
[sliver-openvswitch.git] / datapath / vport-gre.c
1 /*
2  * Copyright (c) 2010, 2011 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/if.h>
12 #include <linux/skbuff.h>
13 #include <linux/ip.h>
14 #include <linux/if_tunnel.h>
15 #include <linux/if_vlan.h>
16 #include <linux/in.h>
17
18 #include <net/icmp.h>
19 #include <net/ip.h>
20 #include <net/protocol.h>
21
22 #include "tunnel.h"
23 #include "vport.h"
24 #include "vport-generic.h"
25
26 /*
27  * The GRE header is composed of a series of sections: a base and then a variable
28  * number of options.
29  */
30 #define GRE_HEADER_SECTION 4
31
32 struct gre_base_hdr {
33         __be16 flags;
34         __be16 protocol;
35 };
36
37 static int gre_hdr_len(const struct tnl_mutable_config *mutable)
38 {
39         int len;
40
41         len = GRE_HEADER_SECTION;
42
43         if (mutable->flags & TNL_F_CSUM)
44                 len += GRE_HEADER_SECTION;
45
46         if (mutable->out_key || mutable->flags & TNL_F_OUT_KEY_ACTION)
47                 len += GRE_HEADER_SECTION;
48
49         return len;
50 }
51
52 /* Returns the least-significant 32 bits of a __be64. */
53 static __be32 be64_get_low32(__be64 x)
54 {
55 #ifdef __BIG_ENDIAN
56         return (__force __be32)x;
57 #else
58         return (__force __be32)((__force u64)x >> 32);
59 #endif
60 }
61
62 static void gre_build_header(const struct vport *vport,
63                              const struct tnl_mutable_config *mutable,
64                              void *header)
65 {
66         struct gre_base_hdr *greh = header;
67         __be32 *options = (__be32 *)(greh + 1);
68
69         greh->protocol = htons(ETH_P_TEB);
70         greh->flags = 0;
71
72         if (mutable->flags & TNL_F_CSUM) {
73                 greh->flags |= GRE_CSUM;
74                 *options = 0;
75                 options++;
76         }
77
78         if (mutable->out_key || mutable->flags & TNL_F_OUT_KEY_ACTION)
79                 greh->flags |= GRE_KEY;
80
81         if (mutable->out_key)
82                 *options = be64_get_low32(mutable->out_key);
83 }
84
85 static struct sk_buff *gre_update_header(const struct vport *vport,
86                                          const struct tnl_mutable_config *mutable,
87                                          struct dst_entry *dst,
88                                          struct sk_buff *skb)
89 {
90         __be32 *options = (__be32 *)(skb_network_header(skb) + mutable->tunnel_hlen
91                                                - GRE_HEADER_SECTION);
92
93         /* Work backwards over the options so the checksum is last. */
94         if (mutable->flags & TNL_F_OUT_KEY_ACTION)
95                 *options = be64_get_low32(OVS_CB(skb)->tun_id);
96
97         if (mutable->out_key || mutable->flags & TNL_F_OUT_KEY_ACTION)
98                 options--;
99
100         if (mutable->flags & TNL_F_CSUM)
101                 *(__sum16 *)options = csum_fold(skb_checksum(skb,
102                                                 skb_transport_offset(skb),
103                                                 skb->len - skb_transport_offset(skb),
104                                                 0));
105         /*
106          * Allow our local IP stack to fragment the outer packet even if the
107          * DF bit is set as a last resort.  We also need to force selection of
108          * an IP ID here because Linux will otherwise leave it at 0 if the
109          * packet originally had DF set.
110          */
111         skb->local_df = 1;
112         __ip_select_ident(ip_hdr(skb), dst, 0);
113
114         return skb;
115 }
116
117 /* Zero-extends a __be32 into the least-significant 32 bits of a __be64. */
118 static __be64 be32_extend_to_be64(__be32 x)
119 {
120 #ifdef __BIG_ENDIAN
121         return (__force __be64)x;
122 #else
123         return (__force __be64)((__force u64)x << 32);
124 #endif
125 }
126
127 static int parse_header(struct iphdr *iph, __be16 *flags, __be64 *key)
128 {
129         /* IP and ICMP protocol handlers check that the IHL is valid. */
130         struct gre_base_hdr *greh = (struct gre_base_hdr *)((u8 *)iph + (iph->ihl << 2));
131         __be32 *options = (__be32 *)(greh + 1);
132         int hdr_len;
133
134         *flags = greh->flags;
135
136         if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
137                 return -EINVAL;
138
139         if (unlikely(greh->protocol != htons(ETH_P_TEB)))
140                 return -EINVAL;
141
142         hdr_len = GRE_HEADER_SECTION;
143
144         if (greh->flags & GRE_CSUM) {
145                 hdr_len += GRE_HEADER_SECTION;
146                 options++;
147         }
148
149         if (greh->flags & GRE_KEY) {
150                 hdr_len += GRE_HEADER_SECTION;
151
152                 *key = be32_extend_to_be64(*options);
153                 options++;
154         } else
155                 *key = 0;
156
157         if (unlikely(greh->flags & GRE_SEQ))
158                 hdr_len += GRE_HEADER_SECTION;
159
160         return hdr_len;
161 }
162
163 /* Called with rcu_read_lock and BH disabled. */
164 static void gre_err(struct sk_buff *skb, u32 info)
165 {
166         struct vport *vport;
167         const struct tnl_mutable_config *mutable;
168         const int type = icmp_hdr(skb)->type;
169         const int code = icmp_hdr(skb)->code;
170         int mtu = ntohs(icmp_hdr(skb)->un.frag.mtu);
171
172         struct iphdr *iph;
173         __be16 flags;
174         __be64 key;
175         int tunnel_hdr_len, tot_hdr_len;
176         unsigned int orig_mac_header;
177         unsigned int orig_nw_header;
178
179         if (type != ICMP_DEST_UNREACH || code != ICMP_FRAG_NEEDED)
180                 return;
181
182         /*
183          * The mimimum size packet that we would actually be able to process:
184          * encapsulating IP header, minimum GRE header, Ethernet header,
185          * inner IPv4 header.
186          */
187         if (!pskb_may_pull(skb, sizeof(struct iphdr) + GRE_HEADER_SECTION +
188                                 ETH_HLEN + sizeof(struct iphdr)))
189                 return;
190
191         iph = (struct iphdr *)skb->data;
192
193         tunnel_hdr_len = parse_header(iph, &flags, &key);
194         if (tunnel_hdr_len < 0)
195                 return;
196
197         vport = tnl_find_port(iph->saddr, iph->daddr, key, TNL_T_PROTO_GRE,
198                               &mutable);
199         if (!vport)
200                 return;
201
202         /*
203          * Packets received by this function were previously sent by us, so
204          * any comparisons should be to the output values, not the input.
205          * However, it's not really worth it to have a hash table based on
206          * output keys (especially since ICMP error handling of tunneled packets
207          * isn't that reliable anyways).  Therefore, we do a lookup based on the
208          * out key as if it were the in key and then check to see if the input
209          * and output keys are the same.
210          */
211         if (mutable->key.in_key != mutable->out_key)
212                 return;
213
214         if (!!(mutable->flags & TNL_F_IN_KEY_MATCH) !=
215             !!(mutable->flags & TNL_F_OUT_KEY_ACTION))
216                 return;
217
218         if ((mutable->flags & TNL_F_CSUM) && !(flags & GRE_CSUM))
219                 return;
220
221         tunnel_hdr_len += iph->ihl << 2;
222
223         orig_mac_header = skb_mac_header(skb) - skb->data;
224         orig_nw_header = skb_network_header(skb) - skb->data;
225         skb_set_mac_header(skb, tunnel_hdr_len);
226
227         tot_hdr_len = tunnel_hdr_len + ETH_HLEN;
228
229         skb->protocol = eth_hdr(skb)->h_proto;
230         if (skb->protocol == htons(ETH_P_8021Q)) {
231                 tot_hdr_len += VLAN_HLEN;
232                 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
233         }
234
235         skb_set_network_header(skb, tot_hdr_len);
236         mtu -= tot_hdr_len;
237
238         if (skb->protocol == htons(ETH_P_IP))
239                 tot_hdr_len += sizeof(struct iphdr);
240 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
241         else if (skb->protocol == htons(ETH_P_IPV6))
242                 tot_hdr_len += sizeof(struct ipv6hdr);
243 #endif
244         else
245                 goto out;
246
247         if (!pskb_may_pull(skb, tot_hdr_len))
248                 goto out;
249
250         if (skb->protocol == htons(ETH_P_IP)) {
251                 if (mtu < IP_MIN_MTU) {
252                         if (ntohs(ip_hdr(skb)->tot_len) >= IP_MIN_MTU)
253                                 mtu = IP_MIN_MTU;
254                         else
255                                 goto out;
256                 }
257
258         }
259 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
260         else if (skb->protocol == htons(ETH_P_IPV6)) {
261                 if (mtu < IPV6_MIN_MTU) {
262                         unsigned int packet_length = sizeof(struct ipv6hdr) +
263                                               ntohs(ipv6_hdr(skb)->payload_len);
264
265                         if (packet_length >= IPV6_MIN_MTU
266                             || ntohs(ipv6_hdr(skb)->payload_len) == 0)
267                                 mtu = IPV6_MIN_MTU;
268                         else
269                                 goto out;
270                 }
271         }
272 #endif
273
274         __skb_pull(skb, tunnel_hdr_len);
275         tnl_frag_needed(vport, mutable, skb, mtu, key);
276         __skb_push(skb, tunnel_hdr_len);
277
278 out:
279         skb_set_mac_header(skb, orig_mac_header);
280         skb_set_network_header(skb, orig_nw_header);
281         skb->protocol = htons(ETH_P_IP);
282 }
283
284 static bool check_checksum(struct sk_buff *skb)
285 {
286         struct iphdr *iph = ip_hdr(skb);
287         struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
288         __sum16 csum = 0;
289
290         if (greh->flags & GRE_CSUM) {
291                 switch (skb->ip_summed) {
292                 case CHECKSUM_COMPLETE:
293                         csum = csum_fold(skb->csum);
294
295                         if (!csum)
296                                 break;
297                         /* Fall through. */
298
299                 case CHECKSUM_NONE:
300                         skb->csum = 0;
301                         csum = __skb_checksum_complete(skb);
302                         skb->ip_summed = CHECKSUM_COMPLETE;
303                         break;
304                 }
305         }
306
307         return (csum == 0);
308 }
309
310 /* Called with rcu_read_lock and BH disabled. */
311 static int gre_rcv(struct sk_buff *skb)
312 {
313         struct vport *vport;
314         const struct tnl_mutable_config *mutable;
315         int hdr_len;
316         struct iphdr *iph;
317         __be16 flags;
318         __be64 key;
319
320         if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
321                 goto error;
322
323         if (unlikely(!check_checksum(skb)))
324                 goto error;
325
326         hdr_len = parse_header(ip_hdr(skb), &flags, &key);
327         if (unlikely(hdr_len < 0))
328                 goto error;
329
330         if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
331                 goto error;
332
333         iph = ip_hdr(skb);
334         vport = tnl_find_port(iph->daddr, iph->saddr, key, TNL_T_PROTO_GRE,
335                               &mutable);
336         if (unlikely(!vport)) {
337                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
338                 goto error;
339         }
340
341         if (mutable->flags & TNL_F_IN_KEY_MATCH)
342                 OVS_CB(skb)->tun_id = key;
343         else
344                 OVS_CB(skb)->tun_id = 0;
345
346         __skb_pull(skb, hdr_len);
347         skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
348
349         tnl_rcv(vport, skb, iph->tos);
350         return 0;
351
352 error:
353         kfree_skb(skb);
354         return 0;
355 }
356
357 static const struct tnl_ops gre_tnl_ops = {
358         .tunnel_type    = TNL_T_PROTO_GRE,
359         .ipproto        = IPPROTO_GRE,
360         .hdr_len        = gre_hdr_len,
361         .build_header   = gre_build_header,
362         .update_header  = gre_update_header,
363 };
364
365 static struct vport *gre_create(const struct vport_parms *parms)
366 {
367         return tnl_create(parms, &gre_vport_ops, &gre_tnl_ops);
368 }
369
370 static const struct net_protocol gre_protocol_handlers = {
371         .handler        =       gre_rcv,
372         .err_handler    =       gre_err,
373 };
374
375 static int gre_init(void)
376 {
377         int err;
378
379         err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
380         if (err)
381                 pr_warn("cannot register gre protocol handler\n");
382
383         return err;
384 }
385
386 static void gre_exit(void)
387 {
388         inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
389 }
390
391 const struct vport_ops gre_vport_ops = {
392         .type           = OVS_VPORT_TYPE_GRE,
393         .flags          = VPORT_F_TUN_ID,
394         .init           = gre_init,
395         .exit           = gre_exit,
396         .create         = gre_create,
397         .destroy        = tnl_destroy,
398         .set_addr       = tnl_set_addr,
399         .get_name       = tnl_get_name,
400         .get_addr       = tnl_get_addr,
401         .get_options    = tnl_get_options,
402         .set_options    = tnl_set_options,
403         .get_dev_flags  = vport_gen_get_dev_flags,
404         .is_running     = vport_gen_is_running,
405         .get_operstate  = vport_gen_get_operstate,
406         .send           = tnl_send,
407 };