vport: Move 'extern' declarations of vports to header.
[sliver-openvswitch.git] / datapath / vport-gre.c
1 /*
2  * Copyright (c) 2010 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 #include <linux/if_arp.h>
10 #include <linux/if_ether.h>
11 #include <linux/ip.h>
12 #include <linux/if_tunnel.h>
13 #include <linux/if_vlan.h>
14 #include <linux/in.h>
15 #include <linux/in_route.h>
16 #include <linux/jhash.h>
17 #include <linux/kernel.h>
18 #include <linux/version.h>
19
20 #include <net/dsfield.h>
21 #include <net/dst.h>
22 #include <net/icmp.h>
23 #include <net/inet_ecn.h>
24 #include <net/ip.h>
25 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
26 #include <net/ipv6.h>
27 #endif
28 #include <net/protocol.h>
29 #include <net/route.h>
30 #include <net/xfrm.h>
31
32 #include "actions.h"
33 #include "datapath.h"
34 #include "openvswitch/gre.h"
35 #include "table.h"
36 #include "vport.h"
37 #include "vport-generic.h"
38
39 /* The absolute minimum fragment size.  Note that there are many other
40  * definitions of the minimum MTU. */
41 #define IP_MIN_MTU 68
42
43 /* The GRE header is composed of a series of sections: a base and then a variable
44  * number of options. */
45 #define GRE_HEADER_SECTION 4
46
47 struct mutable_config {
48         struct rcu_head rcu;
49
50         unsigned char eth_addr[ETH_ALEN];
51         unsigned int mtu;
52         struct gre_port_config port_config;
53
54         int tunnel_hlen; /* Tunnel header length. */
55 };
56
57 struct gre_vport {
58         struct tbl_node tbl_node;
59
60         char name[IFNAMSIZ];
61
62         /* Protected by RCU. */
63         struct mutable_config *mutable;
64 };
65
66 /* Protected by RCU. */
67 static struct tbl *port_table;
68
69 /* These are just used as an optimization: they don't require any kind of
70  * synchronization because we could have just as easily read the value before
71  * the port change happened. */
72 static unsigned int key_local_remote_ports;
73 static unsigned int key_remote_ports;
74 static unsigned int local_remote_ports;
75 static unsigned int remote_ports;
76
77 static inline struct gre_vport *
78 gre_vport_priv(const struct vport *vport)
79 {
80         return vport_priv(vport);
81 }
82
83 static inline struct vport *
84 gre_vport_to_vport(const struct gre_vport *gre_vport)
85 {
86         return vport_from_priv(gre_vport);
87 }
88
89 static inline struct gre_vport *
90 gre_vport_table_cast(const struct tbl_node *node)
91 {
92         return container_of(node, struct gre_vport, tbl_node);
93 }
94
95 /* RCU callback. */
96 static void
97 free_config(struct rcu_head *rcu)
98 {
99         struct mutable_config *c = container_of(rcu, struct mutable_config, rcu);
100         kfree(c);
101 }
102
103 static void
104 assign_config_rcu(struct vport *vport, struct mutable_config *new_config)
105 {
106         struct gre_vport *gre_vport = gre_vport_priv(vport);
107         struct mutable_config *old_config;
108
109         old_config = rcu_dereference(gre_vport->mutable);
110         rcu_assign_pointer(gre_vport->mutable, new_config);
111         call_rcu(&old_config->rcu, free_config);
112 }
113
114 static unsigned int *
115 find_port_pool(const struct mutable_config *mutable)
116 {
117         if (mutable->port_config.flags & GRE_F_IN_KEY_MATCH) {
118                 if (mutable->port_config.saddr)
119                         return &local_remote_ports;
120                 else
121                         return &remote_ports;
122         } else {
123                 if (mutable->port_config.saddr)
124                         return &key_local_remote_ports;
125                 else
126                         return &key_remote_ports;
127         }
128 }
129
130 enum lookup_key {
131         LOOKUP_SADDR            = 0,
132         LOOKUP_DADDR            = 1,
133         LOOKUP_KEY              = 2,
134         LOOKUP_KEY_MATCH        = 3
135 };
136
137 struct port_lookup_key {
138         u32 vals[4];                    /* Contains enum lookup_key keys. */
139         const struct mutable_config *mutable;
140 };
141
142 /* Modifies 'target' to store the rcu_dereferenced pointer that was used to do
143  * the comparision. */
144 static int
145 port_cmp(const struct tbl_node *node, void *target)
146 {
147         const struct gre_vport *gre_vport = gre_vport_table_cast(node);
148         struct port_lookup_key *lookup = target;
149
150         lookup->mutable = rcu_dereference(gre_vport->mutable);
151
152         return ((lookup->mutable->port_config.flags & GRE_F_IN_KEY_MATCH) ==
153                         lookup->vals[LOOKUP_KEY_MATCH]) &&
154                lookup->mutable->port_config.daddr == lookup->vals[LOOKUP_DADDR] &&
155                lookup->mutable->port_config.in_key == lookup->vals[LOOKUP_KEY] &&
156                lookup->mutable->port_config.saddr == lookup->vals[LOOKUP_SADDR];
157 }
158
159 static u32
160 port_hash(struct port_lookup_key *lookup)
161 {
162         return jhash2(lookup->vals, ARRAY_SIZE(lookup->vals), 0);
163 }
164
165 static int
166 add_port(struct vport *vport)
167 {
168         struct gre_vport *gre_vport = gre_vport_priv(vport);
169         struct port_lookup_key lookup;
170         int err;
171
172         if (!port_table) {
173                 struct tbl *new_table;
174
175                 new_table = tbl_create(0);
176                 if (!new_table)
177                         return -ENOMEM;
178
179                 rcu_assign_pointer(port_table, new_table);
180
181         } else if (tbl_count(port_table) > tbl_n_buckets(port_table)) {
182                 struct tbl *old_table = port_table;
183                 struct tbl *new_table;
184
185                 new_table = tbl_expand(old_table);
186                 if (IS_ERR(new_table))
187                         return PTR_ERR(new_table);
188
189                 rcu_assign_pointer(port_table, new_table);
190                 tbl_deferred_destroy(old_table, NULL);
191         }
192
193         lookup.vals[LOOKUP_SADDR] = gre_vport->mutable->port_config.saddr;
194         lookup.vals[LOOKUP_DADDR] = gre_vport->mutable->port_config.daddr;
195         lookup.vals[LOOKUP_KEY] = gre_vport->mutable->port_config.in_key;
196         lookup.vals[LOOKUP_KEY_MATCH] = gre_vport->mutable->port_config.flags & GRE_F_IN_KEY_MATCH;
197
198         err = tbl_insert(port_table, &gre_vport->tbl_node, port_hash(&lookup));
199         if (err)
200                 return err;
201
202         (*find_port_pool(gre_vport->mutable))++;
203
204         return 0;
205 }
206
207 static int
208 del_port(struct vport *vport)
209 {
210         struct gre_vport *gre_vport = gre_vport_priv(vport);
211         int err;
212
213         err = tbl_remove(port_table, &gre_vport->tbl_node);
214         if (err)
215                 return err;
216
217         (*find_port_pool(gre_vport->mutable))--;
218
219         return 0;
220 }
221
222 #define FIND_PORT_KEY           (1 << 0)
223 #define FIND_PORT_MATCH         (1 << 1)
224 #define FIND_PORT_ANY           (FIND_PORT_KEY | FIND_PORT_MATCH)
225
226 static struct vport *
227 find_port(__be32 saddr, __be32 daddr, __be32 key, int port_type,
228           const struct mutable_config **mutable)
229 {
230         struct port_lookup_key lookup;
231         struct tbl *table = rcu_dereference(port_table);
232         struct tbl_node *tbl_node;
233
234         if (!table)
235                 return NULL;
236
237         lookup.vals[LOOKUP_SADDR] = saddr;
238         lookup.vals[LOOKUP_DADDR] = daddr;
239
240         if (port_type & FIND_PORT_KEY) {
241                 lookup.vals[LOOKUP_KEY] = key;
242                 lookup.vals[LOOKUP_KEY_MATCH] = 0;
243
244                 if (key_local_remote_ports) {
245                         tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
246                         if (tbl_node)
247                                 goto found;
248                 }
249
250                 if (key_remote_ports) {
251                         lookup.vals[LOOKUP_SADDR] = 0;
252
253                         tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
254                         if (tbl_node)
255                                 goto found;
256
257                         lookup.vals[LOOKUP_SADDR] = saddr;
258                 }
259         }
260
261         if (port_type & FIND_PORT_MATCH) {
262                 lookup.vals[LOOKUP_KEY] = 0;
263                 lookup.vals[LOOKUP_KEY_MATCH] = GRE_F_IN_KEY_MATCH;
264
265                 if (local_remote_ports) {
266                         tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
267                         if (tbl_node)
268                                 goto found;
269                 }
270
271                 if (remote_ports) {
272                         lookup.vals[LOOKUP_SADDR] = 0;
273
274                         tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
275                         if (tbl_node)
276                                 goto found;
277                 }
278         }
279
280         return NULL;
281
282 found:
283         *mutable = lookup.mutable;
284         return gre_vport_to_vport(gre_vport_table_cast(tbl_node));
285 }
286
287 static bool
288 check_ipv4_address(__be32 addr)
289 {
290         if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr)
291             || ipv4_is_loopback(addr) || ipv4_is_zeronet(addr))
292                 return false;
293
294         return true;
295 }
296
297 static bool
298 ipv4_should_icmp(struct sk_buff *skb)
299 {
300         struct iphdr *old_iph = ip_hdr(skb);
301
302         /* Don't respond to L2 broadcast. */
303         if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
304                 return false;
305
306         /* Don't respond to L3 broadcast or invalid addresses. */
307         if (!check_ipv4_address(old_iph->daddr) ||
308             !check_ipv4_address(old_iph->saddr))
309                 return false;
310
311         /* Only respond to the first fragment. */
312         if (old_iph->frag_off & htons(IP_OFFSET))
313                 return false;
314
315         /* Don't respond to ICMP error messages. */
316         if (old_iph->protocol == IPPROTO_ICMP) {
317                 u8 icmp_type, *icmp_typep;
318
319                 icmp_typep = skb_header_pointer(skb, (u8 *)old_iph +
320                                                 (old_iph->ihl << 2) +
321                                                 offsetof(struct icmphdr, type) -
322                                                 skb->data, sizeof(icmp_type),
323                                                 &icmp_type);
324
325                 if (!icmp_typep)
326                         return false;
327
328                 if (*icmp_typep > NR_ICMP_TYPES
329                         || (*icmp_typep <= ICMP_PARAMETERPROB
330                                 && *icmp_typep != ICMP_ECHOREPLY
331                                 && *icmp_typep != ICMP_ECHO))
332                         return false;
333         }
334
335         return true;
336 }
337
338 static void
339 ipv4_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
340                 unsigned int mtu, unsigned int payload_length)
341 {
342         struct iphdr *iph, *old_iph = ip_hdr(skb);
343         struct icmphdr *icmph;
344         u8 *payload;
345
346         iph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
347         icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
348         payload = skb_put(nskb, payload_length);
349
350         /* IP */
351         iph->version            =       4;
352         iph->ihl                =       sizeof(struct iphdr) >> 2;
353         iph->tos                =       (old_iph->tos & IPTOS_TOS_MASK) |
354                                         IPTOS_PREC_INTERNETCONTROL;
355         iph->tot_len            =       htons(sizeof(struct iphdr)
356                                               + sizeof(struct icmphdr)
357                                               + payload_length);
358         get_random_bytes(&iph->id, sizeof(iph->id));
359         iph->frag_off           =       0;
360         iph->ttl                =       IPDEFTTL;
361         iph->protocol           =       IPPROTO_ICMP;
362         iph->daddr              =       old_iph->saddr;
363         iph->saddr              =       old_iph->daddr;
364
365         ip_send_check(iph);
366
367         /* ICMP */
368         icmph->type             =       ICMP_DEST_UNREACH;
369         icmph->code             =       ICMP_FRAG_NEEDED;
370         icmph->un.gateway       =       htonl(mtu);
371         icmph->checksum         =       0;
372
373         nskb->csum = csum_partial((u8 *)icmph, sizeof(struct icmphdr), 0);
374         nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_iph - skb->data,
375                                             payload, payload_length,
376                                             nskb->csum);
377         icmph->checksum = csum_fold(nskb->csum);
378 }
379
380 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
381 static bool
382 ipv6_should_icmp(struct sk_buff *skb)
383 {
384         struct ipv6hdr *old_ipv6h = ipv6_hdr(skb);
385         int addr_type;
386         int payload_off = (u8 *)(old_ipv6h + 1) - skb->data;
387         u8 nexthdr = ipv6_hdr(skb)->nexthdr;
388
389         /* Check source address is valid. */
390         addr_type = ipv6_addr_type(&old_ipv6h->saddr);
391         if (addr_type & IPV6_ADDR_MULTICAST || addr_type == IPV6_ADDR_ANY)
392                 return false;
393
394         /* Don't reply to unspecified addresses. */
395         if (ipv6_addr_type(&old_ipv6h->daddr) == IPV6_ADDR_ANY)
396                 return false;
397
398         /* Don't respond to ICMP error messages. */
399         payload_off = ipv6_skip_exthdr(skb, payload_off, &nexthdr);
400         if (payload_off < 0)
401                 return false;
402
403         if (nexthdr == NEXTHDR_ICMP) {
404                 u8 icmp_type, *icmp_typep;
405
406                 icmp_typep = skb_header_pointer(skb, payload_off +
407                                                 offsetof(struct icmp6hdr,
408                                                         icmp6_type),
409                                                 sizeof(icmp_type), &icmp_type);
410
411                 if (!icmp_typep || !(*icmp_typep & ICMPV6_INFOMSG_MASK))
412                         return false;
413         }
414
415         return true;
416 }
417
418 static void
419 ipv6_build_icmp(struct sk_buff *skb, struct sk_buff *nskb, unsigned int mtu,
420                 unsigned int payload_length)
421 {
422         struct ipv6hdr *ipv6h, *old_ipv6h = ipv6_hdr(skb);
423         struct icmp6hdr *icmp6h;
424         u8 *payload;
425
426         ipv6h = (struct ipv6hdr *)skb_put(nskb, sizeof(struct ipv6hdr));
427         icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
428         payload = skb_put(nskb, payload_length);
429
430         /* IPv6 */
431         ipv6h->version          =       6;
432         ipv6h->priority         =       0;
433         memset(&ipv6h->flow_lbl, 0, sizeof(ipv6h->flow_lbl));
434         ipv6h->payload_len      =       htons(sizeof(struct icmp6hdr)
435                                               + payload_length);
436         ipv6h->nexthdr          =       NEXTHDR_ICMP;
437         ipv6h->hop_limit        =       IPV6_DEFAULT_HOPLIMIT;
438         ipv6_addr_copy(&ipv6h->daddr, &old_ipv6h->saddr);
439         ipv6_addr_copy(&ipv6h->saddr, &old_ipv6h->daddr);
440
441         /* ICMPv6 */
442         icmp6h->icmp6_type      =       ICMPV6_PKT_TOOBIG;
443         icmp6h->icmp6_code      =       0;
444         icmp6h->icmp6_cksum     =       0;
445         icmp6h->icmp6_mtu       =       htonl(mtu);
446
447         nskb->csum = csum_partial((u8 *)icmp6h, sizeof(struct icmp6hdr), 0);
448         nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_ipv6h - skb->data,
449                                             payload, payload_length,
450                                             nskb->csum);
451         icmp6h->icmp6_cksum = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
452                                                 sizeof(struct icmp6hdr)
453                                                 + payload_length,
454                                                 ipv6h->nexthdr, nskb->csum);
455 }
456 #endif /* IPv6 */
457
458 static bool
459 send_frag_needed(struct vport *vport, const struct mutable_config *mutable,
460                  struct sk_buff *skb, unsigned int mtu, __be32 flow_key)
461 {
462         unsigned int eth_hdr_len = ETH_HLEN;
463         unsigned int total_length = 0, header_length = 0, payload_length;
464         struct ethhdr *eh, *old_eh = eth_hdr(skb);
465         struct sk_buff *nskb;
466
467         /* Sanity check */
468         if (skb->protocol == htons(ETH_P_IP)) {
469                 if (mtu < IP_MIN_MTU)
470                         return false;
471
472                 if (!ipv4_should_icmp(skb))
473                         return true;
474         }
475 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
476         else if (skb->protocol == htons(ETH_P_IPV6)) {
477                 if (mtu < IPV6_MIN_MTU)
478                         return false;
479
480                 /* In theory we should do PMTUD on IPv6 multicast messages but
481                  * we don't have an address to send from so just fragment. */
482                 if (ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST)
483                         return false;
484
485                 if (!ipv6_should_icmp(skb))
486                         return true;
487         }
488 #endif
489         else
490                 return false;
491
492         /* Allocate */
493         if (old_eh->h_proto == htons(ETH_P_8021Q))
494                 eth_hdr_len = VLAN_ETH_HLEN;
495
496         payload_length = skb->len - eth_hdr_len;
497         if (skb->protocol == htons(ETH_P_IP)) {
498                 header_length = sizeof(struct iphdr) + sizeof(struct icmphdr);
499                 total_length = min_t(unsigned int, header_length +
500                                                    payload_length, 576);
501         }
502 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
503         else {
504                 header_length = sizeof(struct ipv6hdr) +
505                                 sizeof(struct icmp6hdr);
506                 total_length = min_t(unsigned int, header_length +
507                                                   payload_length, IPV6_MIN_MTU);
508         }
509 #endif
510
511         total_length = min(total_length, mutable->mtu);
512         payload_length = total_length - header_length;
513
514         nskb = dev_alloc_skb(NET_IP_ALIGN + eth_hdr_len + header_length +
515                              payload_length);
516         if (!nskb)
517                 return false;
518
519         skb_reserve(nskb, NET_IP_ALIGN);
520
521         /* Ethernet / VLAN */
522         eh = (struct ethhdr *)skb_put(nskb, eth_hdr_len);
523         memcpy(eh->h_dest, old_eh->h_source, ETH_ALEN);
524         memcpy(eh->h_source, mutable->eth_addr, ETH_ALEN);
525         nskb->protocol = eh->h_proto = old_eh->h_proto;
526         if (old_eh->h_proto == htons(ETH_P_8021Q)) {
527                 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)eh;
528
529                 vh->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
530                 vh->h_vlan_encapsulated_proto = skb->protocol;
531         }
532         skb_reset_mac_header(nskb);
533
534         /* Protocol */
535         if (skb->protocol == htons(ETH_P_IP))
536                 ipv4_build_icmp(skb, nskb, mtu, payload_length);
537 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
538         else
539                 ipv6_build_icmp(skb, nskb, mtu, payload_length);
540 #endif
541
542         /* Assume that flow based keys are symmetric with respect to input
543          * and output and use the key that we were going to put on the
544          * outgoing packet for the fake received packet.  If the keys are
545          * not symmetric then PMTUD needs to be disabled since we won't have
546          * any way of synthesizing packets. */
547         if (mutable->port_config.flags & GRE_F_IN_KEY_MATCH &&
548             mutable->port_config.flags & GRE_F_OUT_KEY_ACTION)
549                 OVS_CB(nskb)->tun_id = flow_key;
550
551         compute_ip_summed(nskb, false);
552         vport_receive(vport, nskb);
553
554         return true;
555 }
556
557 static struct sk_buff *
558 check_headroom(struct sk_buff *skb, int headroom)
559 {
560         if (skb_headroom(skb) < headroom ||
561             (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
562                 struct sk_buff *nskb = skb_realloc_headroom(skb, headroom);
563                 if (!nskb) {
564                         kfree_skb(skb);
565                         return ERR_PTR(-ENOMEM);
566                 }
567
568                 set_skb_csum_bits(skb, nskb);
569
570                 if (skb->sk)
571                         skb_set_owner_w(nskb, skb->sk);
572
573                 dev_kfree_skb(skb);
574                 return nskb;
575         }
576
577         return skb;
578 }
579
580 static void
581 create_gre_header(struct sk_buff *skb, const struct mutable_config *mutable)
582 {
583         struct iphdr *iph = ip_hdr(skb);
584         __be16 *flags = (__be16 *)(iph + 1);
585         __be16 *protocol = flags + 1;
586         __be32 *options = (__be32 *)((u8 *)iph + mutable->tunnel_hlen
587                                                - GRE_HEADER_SECTION);
588
589         *protocol = htons(ETH_P_TEB);
590         *flags = 0;
591
592         /* Work backwards over the options so the checksum is last. */
593         if (mutable->port_config.out_key ||
594             mutable->port_config.flags & GRE_F_OUT_KEY_ACTION) {
595                 *flags |= GRE_KEY;
596
597                 if (mutable->port_config.flags & GRE_F_OUT_KEY_ACTION)
598                         *options = OVS_CB(skb)->tun_id;
599                 else
600                         *options = mutable->port_config.out_key;
601
602                 options--;
603         }
604
605         if (mutable->port_config.flags & GRE_F_OUT_CSUM) {
606                 *flags |= GRE_CSUM;
607
608                 *options = 0;
609                 *(__sum16 *)options = csum_fold(skb_checksum(skb,
610                                                 sizeof(struct iphdr),
611                                                 skb->len - sizeof(struct iphdr),
612                                                 0));
613         }
614 }
615
616 static int
617 check_checksum(struct sk_buff *skb)
618 {
619         struct iphdr *iph = ip_hdr(skb);
620         __be16 flags = *(__be16 *)(iph + 1);
621         __sum16 csum = 0;
622
623         if (flags & GRE_CSUM) {
624                 switch (skb->ip_summed) {
625                 case CHECKSUM_COMPLETE:
626                         csum = csum_fold(skb->csum);
627
628                         if (!csum)
629                                 break;
630                         /* Fall through. */
631
632                 case CHECKSUM_NONE:
633                         skb->csum = 0;
634                         csum = __skb_checksum_complete(skb);
635                         skb->ip_summed = CHECKSUM_COMPLETE;
636                         break;
637                 }
638         }
639
640         return (csum == 0);
641 }
642
643 static int
644 parse_gre_header(struct iphdr *iph, __be16 *flags, __be32 *key)
645 {
646         /* IP and ICMP protocol handlers check that the IHL is valid. */
647         __be16 *flagsp = (__be16 *)((u8 *)iph + (iph->ihl << 2));
648         __be16 *protocol = flagsp + 1;
649         __be32 *options = (__be32 *)(protocol + 1);
650         int hdr_len;
651
652         *flags = *flagsp;
653
654         if (*flags & (GRE_VERSION | GRE_ROUTING))
655                 return -EINVAL;
656
657         if (*protocol != htons(ETH_P_TEB))
658                 return -EINVAL;
659
660         hdr_len = GRE_HEADER_SECTION;
661
662         if (*flags & GRE_CSUM) {
663                 hdr_len += GRE_HEADER_SECTION;
664                 options++;
665         }
666
667         if (*flags & GRE_KEY) {
668                 hdr_len += GRE_HEADER_SECTION;
669
670                 *key = *options;
671                 options++;
672         } else
673                 *key = 0;
674
675         if (*flags & GRE_SEQ)
676                 hdr_len += GRE_HEADER_SECTION;
677
678         return hdr_len;
679 }
680
681 static inline u8
682 ecn_encapsulate(u8 tos, struct sk_buff *skb)
683 {
684         u8 inner;
685
686         if (skb->protocol == htons(ETH_P_IP))
687                 inner = ((struct iphdr *)skb_network_header(skb))->tos;
688 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
689         else if (skb->protocol == htons(ETH_P_IPV6))
690                 inner = ipv6_get_dsfield((struct ipv6hdr *)skb_network_header(skb));
691 #endif
692         else
693                 inner = 0;
694
695         return INET_ECN_encapsulate(tos, inner);
696 }
697
698 static inline void
699 ecn_decapsulate(u8 tos, struct sk_buff *skb)
700 {
701         if (INET_ECN_is_ce(tos)) {
702                 __be16 protocol = skb->protocol;
703                 unsigned int nw_header = skb_network_header(skb) - skb->data;
704
705                 if (skb->protocol == htons(ETH_P_8021Q)) {
706                         if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
707                                 return;
708
709                         protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
710                         nw_header += VLAN_HLEN;
711                 }
712
713                 if (protocol == htons(ETH_P_IP)) {
714                         if (unlikely(!pskb_may_pull(skb, nw_header
715                             + sizeof(struct iphdr))))
716                                 return;
717
718                         IP_ECN_set_ce((struct iphdr *)(nw_header + skb->data));
719                 }
720 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
721                 else if (protocol == htons(ETH_P_IPV6)) {
722                         if (unlikely(!pskb_may_pull(skb, nw_header
723                             + sizeof(struct ipv6hdr))))
724                                 return;
725
726                         IP6_ECN_set_ce((struct ipv6hdr *)(nw_header
727                                                           + skb->data));
728                 }
729 #endif
730         }
731 }
732
733 static struct sk_buff *
734 handle_gso(struct sk_buff *skb)
735 {
736         if (skb_is_gso(skb)) {
737                 struct sk_buff *nskb = skb_gso_segment(skb, NETIF_F_SG);
738
739                 dev_kfree_skb(skb);
740                 return nskb;
741         }
742
743         return skb;
744 }
745
746 static int
747 handle_csum_offload(struct sk_buff *skb)
748 {
749         if (skb->ip_summed == CHECKSUM_PARTIAL)
750                 return skb_checksum_help(skb);
751         else {
752                 skb->ip_summed = CHECKSUM_NONE;
753                 return 0;
754         }
755 }
756
757 /* Called with rcu_read_lock. */
758 static void
759 gre_err(struct sk_buff *skb, u32 info)
760 {
761         struct vport *vport;
762         const struct mutable_config *mutable;
763         const int type = icmp_hdr(skb)->type;
764         const int code = icmp_hdr(skb)->code;
765         int mtu = ntohs(icmp_hdr(skb)->un.frag.mtu);
766
767         struct iphdr *iph;
768         __be16 flags;
769         __be32 key;
770         int tunnel_hdr_len, tot_hdr_len;
771         unsigned int orig_mac_header;
772         unsigned int orig_nw_header;
773
774         if (type != ICMP_DEST_UNREACH || code != ICMP_FRAG_NEEDED)
775                 return;
776
777         /* The mimimum size packet that we would actually be able to process:
778          * encapsulating IP header, minimum GRE header, Ethernet header,
779          * inner IPv4 header. */
780         if (!pskb_may_pull(skb, sizeof(struct iphdr) + GRE_HEADER_SECTION +
781                                 ETH_HLEN + sizeof(struct iphdr)))
782                 return;
783
784         iph = (struct iphdr *)skb->data;
785
786         tunnel_hdr_len = parse_gre_header(iph, &flags, &key);
787         if (tunnel_hdr_len < 0)
788                 return;
789
790         vport = find_port(iph->saddr, iph->daddr, key, FIND_PORT_ANY, &mutable);
791         if (!vport)
792                 return;
793
794         /* Packets received by this function were previously sent by us, so
795          * any comparisons should be to the output values, not the input.
796          * However, it's not really worth it to have a hash table based on
797          * output keys (especially since ICMP error handling of tunneled packets
798          * isn't that reliable anyways).  Therefore, we do a lookup based on the
799          * out key as if it were the in key and then check to see if the input
800          * and output keys are the same. */
801         if (mutable->port_config.in_key != mutable->port_config.out_key)
802                 return;
803
804         if (!!(mutable->port_config.flags & GRE_F_IN_KEY_MATCH) !=
805             !!(mutable->port_config.flags & GRE_F_OUT_KEY_ACTION))
806                 return;
807
808         if ((mutable->port_config.flags & GRE_F_OUT_CSUM) && !(flags & GRE_CSUM))
809                 return;
810
811         tunnel_hdr_len += iph->ihl << 2;
812
813         orig_mac_header = skb_mac_header(skb) - skb->data;
814         orig_nw_header = skb_network_header(skb) - skb->data;
815         skb_set_mac_header(skb, tunnel_hdr_len);
816
817         tot_hdr_len = tunnel_hdr_len + ETH_HLEN;
818
819         skb->protocol = eth_hdr(skb)->h_proto;
820         if (skb->protocol == htons(ETH_P_8021Q)) {
821                 tot_hdr_len += VLAN_HLEN;
822                 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
823         }
824
825         skb_set_network_header(skb, tot_hdr_len);
826         mtu -= tot_hdr_len;
827
828         if (skb->protocol == htons(ETH_P_IP))
829                 tot_hdr_len += sizeof(struct iphdr);
830 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
831         else if (skb->protocol == htons(ETH_P_IPV6))
832                 tot_hdr_len += sizeof(struct ipv6hdr);
833 #endif
834         else
835                 goto out;
836
837         if (!pskb_may_pull(skb, tot_hdr_len))
838                 goto out;
839
840         if (skb->protocol == htons(ETH_P_IP)) {
841                 if (mtu < IP_MIN_MTU) {
842                         if (ntohs(ip_hdr(skb)->tot_len) >= IP_MIN_MTU)
843                                 mtu = IP_MIN_MTU;
844                         else
845                                 goto out;
846                 }
847
848         }
849 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
850         else if (skb->protocol == htons(ETH_P_IPV6)) {
851                 if (mtu < IPV6_MIN_MTU) {
852                         unsigned int packet_length = sizeof(struct ipv6hdr) +
853                                               ntohs(ipv6_hdr(skb)->payload_len);
854
855                         if (packet_length >= IPV6_MIN_MTU
856                             || ntohs(ipv6_hdr(skb)->payload_len) == 0)
857                                 mtu = IPV6_MIN_MTU;
858                         else
859                                 goto out;
860                 }
861         }
862 #endif
863
864         __pskb_pull(skb, tunnel_hdr_len);
865         send_frag_needed(vport, mutable, skb, mtu, key);
866         skb_push(skb, tunnel_hdr_len);
867
868 out:
869         skb_set_mac_header(skb, orig_mac_header);
870         skb_set_network_header(skb, orig_nw_header);
871         skb->protocol = htons(ETH_P_IP);
872 }
873
874 /* Called with rcu_read_lock. */
875 static int
876 gre_rcv(struct sk_buff *skb)
877 {
878         struct vport *vport;
879         const struct mutable_config *mutable;
880         int hdr_len;
881         struct iphdr *iph;
882         __be16 flags;
883         __be32 key;
884
885         if (!pskb_may_pull(skb, GRE_HEADER_SECTION + ETH_HLEN))
886                 goto error;
887
888         if (!check_checksum(skb))
889                 goto error;
890
891         iph = ip_hdr(skb);
892
893         hdr_len = parse_gre_header(iph, &flags, &key);
894         if (hdr_len < 0)
895                 goto error;
896
897         vport = find_port(iph->daddr, iph->saddr, key, FIND_PORT_ANY, &mutable);
898         if (!vport) {
899                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
900                 goto error;
901         }
902
903         if ((mutable->port_config.flags & GRE_F_IN_CSUM) && !(flags & GRE_CSUM)) {
904                 vport_record_error(vport, VPORT_E_RX_CRC);
905                 goto error;
906         }
907
908         if (!pskb_pull(skb, hdr_len) || !pskb_may_pull(skb, ETH_HLEN)) {
909                 vport_record_error(vport, VPORT_E_RX_ERROR);
910                 goto error;
911         }
912
913         skb->pkt_type = PACKET_HOST;
914         skb->protocol = eth_type_trans(skb, skb->dev);
915         skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
916
917         skb_dst_drop(skb);
918         nf_reset(skb);
919         secpath_reset(skb);
920         skb_reset_network_header(skb);
921
922         ecn_decapsulate(iph->tos, skb);
923
924         if (mutable->port_config.flags & GRE_F_IN_KEY_MATCH)
925                 OVS_CB(skb)->tun_id = key;
926         else
927                 OVS_CB(skb)->tun_id = 0;
928
929         skb_push(skb, ETH_HLEN);
930         compute_ip_summed(skb, false);
931
932         vport_receive(vport, skb);
933
934         return 0;
935
936 error:
937         kfree_skb(skb);
938         return 0;
939 }
940
941 static int
942 build_packet(struct vport *vport, const struct mutable_config *mutable,
943              struct iphdr *iph, struct rtable *rt, int max_headroom, int mtu,
944              struct sk_buff *skb)
945 {
946         int err;
947         struct iphdr *new_iph;
948         int orig_len = skb->len;
949         __be16 frag_off = iph->frag_off;
950
951         skb = check_headroom(skb, max_headroom);
952         if (unlikely(IS_ERR(skb)))
953                 goto error;
954
955         err = handle_csum_offload(skb);
956         if (err)
957                 goto error_free;
958
959         if (skb->protocol == htons(ETH_P_IP)) {
960                 struct iphdr *old_iph = ip_hdr(skb);
961
962                 if ((old_iph->frag_off & htons(IP_DF)) &&
963                     mtu < ntohs(old_iph->tot_len)) {
964                         if (send_frag_needed(vport, mutable, skb, mtu, OVS_CB(skb)->tun_id))
965                                 goto error_free;
966                 }
967
968         }
969 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
970         else if (skb->protocol == htons(ETH_P_IPV6)) {
971                 unsigned int packet_length = skb->len - ETH_HLEN
972                         - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
973
974                 /* IPv6 requires PMTUD if the packet is above the minimum MTU. */
975                 if (packet_length > IPV6_MIN_MTU)
976                         frag_off = htons(IP_DF);
977
978                 if (mtu < packet_length) {
979                         if (send_frag_needed(vport, mutable, skb, mtu, OVS_CB(skb)->tun_id))
980                                 goto error_free;
981                 }
982         }
983 #endif
984
985         skb_reset_transport_header(skb);
986         new_iph = (struct iphdr *)skb_push(skb, mutable->tunnel_hlen);
987         skb_reset_network_header(skb);
988
989         memcpy(new_iph, iph, sizeof(struct iphdr));
990         new_iph->frag_off = frag_off;
991         ip_select_ident(new_iph, &rt->u.dst, NULL);
992
993         create_gre_header(skb, mutable);
994
995         /* Allow our local IP stack to fragment the outer packet even if the
996          * DF bit is set as a last resort. */
997         skb->local_df = 1;
998
999         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1000         IPCB(skb)->flags = 0;
1001
1002         err = ip_local_out(skb);
1003         if (likely(net_xmit_eval(err) == 0))
1004                 return orig_len;
1005         else {
1006                 vport_record_error(vport, VPORT_E_TX_ERROR);
1007                 return 0;
1008         }
1009
1010 error_free:
1011         kfree_skb(skb);
1012 error:
1013         vport_record_error(vport, VPORT_E_TX_DROPPED);
1014
1015         return 0;
1016 }
1017
1018 static int
1019 gre_send(struct vport *vport, struct sk_buff *skb)
1020 {
1021         struct gre_vport *gre_vport = gre_vport_priv(vport);
1022         const struct mutable_config *mutable = rcu_dereference(gre_vport->mutable);
1023
1024         struct iphdr *old_iph;
1025         int orig_len;
1026         struct iphdr iph;
1027         struct rtable *rt;
1028         int max_headroom;
1029         int mtu;
1030
1031         /* Validate the protocol headers before we try to use them. */
1032         if (skb->protocol == htons(ETH_P_8021Q)) {
1033                 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
1034                         goto error_free;
1035
1036                 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1037                 skb_set_network_header(skb, VLAN_ETH_HLEN);
1038         }
1039
1040         if (skb->protocol == htons(ETH_P_IP)) {
1041                 if (unlikely(!pskb_may_pull(skb, skb_network_header(skb)
1042                     + sizeof(struct iphdr) - skb->data)))
1043                         skb->protocol = 0;
1044         }
1045 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1046         else if (skb->protocol == htons(ETH_P_IPV6)) {
1047                 if (unlikely(!pskb_may_pull(skb, skb_network_header(skb)
1048                     + sizeof(struct ipv6hdr) - skb->data)))
1049                         skb->protocol = 0;
1050         }
1051 #endif
1052         old_iph = ip_hdr(skb);
1053
1054         iph.tos = mutable->port_config.tos;
1055         if (mutable->port_config.flags & GRE_F_TOS_INHERIT) {
1056                 if (skb->protocol == htons(ETH_P_IP))
1057                         iph.tos = old_iph->tos;
1058 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1059                 else if (skb->protocol == htons(ETH_P_IPV6))
1060                         iph.tos = ipv6_get_dsfield(ipv6_hdr(skb));
1061 #endif
1062         }
1063         iph.tos = ecn_encapsulate(iph.tos, skb);
1064
1065         {
1066                 struct flowi fl = { .nl_u = { .ip4_u =
1067                                               { .daddr = mutable->port_config.daddr,
1068                                                 .saddr = mutable->port_config.saddr,
1069                                                 .tos = RT_TOS(iph.tos) } },
1070                                     .proto = IPPROTO_GRE };
1071
1072                 if (ip_route_output_key(&init_net, &rt, &fl))
1073                         goto error_free;
1074         }
1075
1076         iph.ttl = mutable->port_config.ttl;
1077         if (mutable->port_config.flags & GRE_F_TTL_INHERIT) {
1078                 if (skb->protocol == htons(ETH_P_IP))
1079                         iph.ttl = old_iph->ttl;
1080 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1081                 else if (skb->protocol == htons(ETH_P_IPV6))
1082                         iph.ttl = ipv6_hdr(skb)->hop_limit;
1083 #endif
1084         }
1085         if (!iph.ttl)
1086                 iph.ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
1087
1088         iph.frag_off = (mutable->port_config.flags & GRE_F_PMTUD) ? htons(IP_DF) : 0;
1089         if (iph.frag_off)
1090                 mtu = dst_mtu(&rt->u.dst)
1091                         - ETH_HLEN
1092                         - mutable->tunnel_hlen
1093                         - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
1094         else
1095                 mtu = mutable->mtu;
1096
1097         if (skb->protocol == htons(ETH_P_IP)) {
1098                 iph.frag_off |= old_iph->frag_off & htons(IP_DF);
1099                 mtu = max(mtu, IP_MIN_MTU);
1100         }
1101 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1102         else if (skb->protocol == htons(ETH_P_IPV6))
1103                 mtu = max(mtu, IPV6_MIN_MTU);
1104 #endif
1105
1106         iph.version = 4;
1107         iph.ihl = sizeof(struct iphdr) >> 2;
1108         iph.protocol = IPPROTO_GRE;
1109         iph.daddr = rt->rt_dst;
1110         iph.saddr = rt->rt_src;
1111
1112         nf_reset(skb);
1113         secpath_reset(skb);
1114         skb_dst_drop(skb);
1115         skb_dst_set(skb, &rt->u.dst);
1116
1117         /* If we are doing GSO on a pskb it is better to make sure that the
1118          * headroom is correct now.  We will only have to copy the portion in
1119          * the linear data area and GSO will preserve headroom when it creates
1120          * the segments.  This is particularly beneficial on Xen where we get
1121          * lots of GSO pskbs.  Conversely, we delay copying if it is just to
1122          * get our own writable clone because GSO may do the copy for us. */
1123         max_headroom = LL_RESERVED_SPACE(rt->u.dst.dev) + rt->u.dst.header_len
1124                         + mutable->tunnel_hlen;
1125
1126         if (skb_headroom(skb) < max_headroom) {
1127                 skb = check_headroom(skb, max_headroom);
1128                 if (unlikely(IS_ERR(skb))) {
1129                         vport_record_error(vport, VPORT_E_TX_DROPPED);
1130                         goto error;
1131                 }
1132         }
1133
1134         forward_ip_summed(skb);
1135         vswitch_skb_checksum_setup(skb);
1136
1137         skb = handle_gso(skb);
1138         if (unlikely(IS_ERR(skb))) {
1139                 vport_record_error(vport, VPORT_E_TX_DROPPED);
1140                 goto error;
1141         }
1142
1143         /* Process GSO segments.  Try to do any work for the entire packet that
1144          * doesn't involve actually writing to it before this point. */
1145         orig_len = 0;
1146         do {
1147                 struct sk_buff *next_skb = skb->next;
1148                 skb->next = NULL;
1149
1150                 orig_len += build_packet(vport, mutable, &iph, rt, max_headroom, mtu, skb);
1151
1152                 skb = next_skb;
1153         } while (skb);
1154
1155         return orig_len;
1156
1157 error_free:
1158         kfree_skb(skb);
1159         vport_record_error(vport, VPORT_E_TX_ERROR);
1160 error:
1161         return 0;
1162 }
1163
1164 static struct net_protocol gre_protocol_handlers = {
1165         .handler        =       gre_rcv,
1166         .err_handler    =       gre_err,
1167 };
1168
1169 static int
1170 gre_init(void)
1171 {
1172         int err;
1173
1174         err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
1175         if (err)
1176                 printk(KERN_WARNING "openvswitch: cannot register gre protocol handler\n");
1177
1178         return err;
1179 }
1180
1181 static void
1182 gre_exit(void)
1183 {
1184         tbl_destroy(port_table, NULL);
1185         inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
1186 }
1187
1188 static int
1189 set_config(const struct vport *cur_vport, struct mutable_config *mutable,
1190            const void __user *uconfig)
1191 {
1192         const struct vport *old_vport;
1193         const struct mutable_config *old_mutable;
1194         int port_type;
1195
1196         if (copy_from_user(&mutable->port_config, uconfig, sizeof(struct gre_port_config)))
1197                 return -EFAULT;
1198
1199         if (mutable->port_config.daddr == 0)
1200                 return -EINVAL;
1201
1202         if (mutable->port_config.flags & GRE_F_IN_KEY_MATCH) {
1203                 port_type = FIND_PORT_MATCH;
1204                 mutable->port_config.in_key = 0;
1205         } else
1206                 port_type = FIND_PORT_KEY;
1207
1208         old_vport = find_port(mutable->port_config.saddr,
1209                               mutable->port_config.daddr,
1210                               mutable->port_config.in_key, port_type,
1211                               &old_mutable);
1212
1213         if (old_vport && old_vport != cur_vport)
1214                 return -EEXIST;
1215
1216         if (mutable->port_config.flags & GRE_F_OUT_KEY_ACTION)
1217                 mutable->port_config.out_key = 0;
1218
1219         mutable->tunnel_hlen = sizeof(struct iphdr) + GRE_HEADER_SECTION;
1220
1221         if (mutable->port_config.flags & GRE_F_OUT_CSUM)
1222                 mutable->tunnel_hlen += GRE_HEADER_SECTION;
1223
1224         if (mutable->port_config.out_key ||
1225             mutable->port_config.flags & GRE_F_OUT_KEY_ACTION)
1226                 mutable->tunnel_hlen += GRE_HEADER_SECTION;
1227
1228         return 0;
1229 }
1230
1231 static struct vport *
1232 gre_create(const char *name, const void __user *config)
1233 {
1234         struct vport *vport;
1235         struct gre_vport *gre_vport;
1236         int err;
1237
1238         vport = vport_alloc(sizeof(struct gre_vport), &gre_vport_ops);
1239         if (IS_ERR(vport)) {
1240                 err = PTR_ERR(vport);
1241                 goto error;
1242         }
1243
1244         gre_vport = gre_vport_priv(vport);
1245
1246         strcpy(gre_vport->name, name);
1247
1248         gre_vport->mutable = kmalloc(sizeof(struct mutable_config), GFP_KERNEL);
1249         if (!gre_vport->mutable) {
1250                 err = -ENOMEM;
1251                 goto error_free_vport;
1252         }
1253
1254         vport_gen_rand_ether_addr(gre_vport->mutable->eth_addr);
1255         gre_vport->mutable->mtu = ETH_DATA_LEN;
1256
1257         err = set_config(NULL, gre_vport->mutable, config);
1258         if (err)
1259                 goto error_free_mutable;
1260
1261         err = add_port(vport);
1262         if (err)
1263                 goto error_free_mutable;
1264
1265         return vport;
1266
1267 error_free_mutable:
1268         kfree(gre_vport->mutable);
1269 error_free_vport:
1270         vport_free(vport);
1271 error:
1272         return ERR_PTR(err);
1273 }
1274
1275 static int
1276 gre_modify(struct vport *vport, const void __user *config)
1277 {
1278         struct gre_vport *gre_vport = gre_vport_priv(vport);
1279         struct mutable_config *mutable;
1280         int err;
1281         int update_hash = 0;
1282
1283         mutable = kmemdup(gre_vport->mutable, sizeof(struct mutable_config), GFP_KERNEL);
1284         if (!mutable) {
1285                 err = -ENOMEM;
1286                 goto error;
1287         }
1288
1289         err = set_config(vport, mutable, config);
1290         if (err)
1291                 goto error_free;
1292
1293         /* Only remove the port from the hash table if something that would
1294          * affect the lookup has changed. */
1295         if (gre_vport->mutable->port_config.saddr != mutable->port_config.saddr ||
1296             gre_vport->mutable->port_config.daddr != mutable->port_config.daddr ||
1297             gre_vport->mutable->port_config.in_key != mutable->port_config.in_key ||
1298             (gre_vport->mutable->port_config.flags & GRE_F_IN_KEY_MATCH) !=
1299             (mutable->port_config.flags & GRE_F_IN_KEY_MATCH))
1300                 update_hash = 1;
1301
1302
1303         /* This update is not atomic but the lookup uses the config, which
1304          * serves as an inherent double check. */
1305         if (update_hash) {
1306                 err = del_port(vport);
1307                 if (err)
1308                         goto error_free;
1309         }
1310
1311         assign_config_rcu(vport, mutable);
1312
1313         if (update_hash) {
1314                 err = add_port(vport);
1315                 if (err)
1316                         goto error_free;
1317         }
1318
1319         return 0;
1320
1321 error_free:
1322         kfree(mutable);
1323 error:
1324         return err;
1325 }
1326
1327 static int
1328 gre_destroy(struct vport *vport)
1329 {
1330         struct gre_vport *gre_vport = gre_vport_priv(vport);
1331         int port_type;
1332         const struct mutable_config *old_mutable;
1333
1334         /* Do a hash table lookup to make sure that the port exists.  It should
1335          * exist but might not if a modify failed earlier. */
1336         if (gre_vport->mutable->port_config.flags & GRE_F_IN_KEY_MATCH)
1337                 port_type = FIND_PORT_MATCH;
1338         else
1339                 port_type = FIND_PORT_KEY;
1340
1341         if (vport == find_port(gre_vport->mutable->port_config.saddr,
1342             gre_vport->mutable->port_config.daddr,
1343             gre_vport->mutable->port_config.in_key, port_type, &old_mutable))
1344                 del_port(vport);
1345
1346         kfree(gre_vport->mutable);
1347         vport_free(vport);
1348
1349         return 0;
1350 }
1351
1352 static int
1353 gre_set_mtu(struct vport *vport, int mtu)
1354 {
1355         struct gre_vport *gre_vport = gre_vport_priv(vport);
1356         struct mutable_config *mutable;
1357
1358         mutable = kmemdup(gre_vport->mutable, sizeof(struct mutable_config), GFP_KERNEL);
1359         if (!mutable)
1360                 return -ENOMEM;
1361
1362         mutable->mtu = mtu;
1363         assign_config_rcu(vport, mutable);
1364
1365         return 0;
1366 }
1367
1368 static int
1369 gre_set_addr(struct vport *vport, const unsigned char *addr)
1370 {
1371         struct gre_vport *gre_vport = gre_vport_priv(vport);
1372         struct mutable_config *mutable;
1373
1374         mutable = kmemdup(gre_vport->mutable, sizeof(struct mutable_config), GFP_KERNEL);
1375         if (!mutable)
1376                 return -ENOMEM;
1377
1378         memcpy(mutable->eth_addr, addr, ETH_ALEN);
1379         assign_config_rcu(vport, mutable);
1380
1381         return 0;
1382 }
1383
1384
1385 static const char *
1386 gre_get_name(const struct vport *vport)
1387 {
1388         const struct gre_vport *gre_vport = gre_vport_priv(vport);
1389         return gre_vport->name;
1390 }
1391
1392 static const unsigned char *
1393 gre_get_addr(const struct vport *vport)
1394 {
1395         const struct gre_vport *gre_vport = gre_vport_priv(vport);
1396         return rcu_dereference(gre_vport->mutable)->eth_addr;
1397 }
1398
1399 static int
1400 gre_get_mtu(const struct vport *vport)
1401 {
1402         const struct gre_vport *gre_vport = gre_vport_priv(vport);
1403         return rcu_dereference(gre_vport->mutable)->mtu;
1404 }
1405
1406 struct vport_ops gre_vport_ops = {
1407         .type           = "gre",
1408         .flags          = VPORT_F_GEN_STATS | VPORT_F_TUN_ID,
1409         .init           = gre_init,
1410         .exit           = gre_exit,
1411         .create         = gre_create,
1412         .modify         = gre_modify,
1413         .destroy        = gre_destroy,
1414         .set_mtu        = gre_set_mtu,
1415         .set_addr       = gre_set_addr,
1416         .get_name       = gre_get_name,
1417         .get_addr       = gre_get_addr,
1418         .get_dev_flags  = vport_gen_get_dev_flags,
1419         .is_running     = vport_gen_is_running,
1420         .get_operstate  = vport_gen_get_operstate,
1421         .get_mtu        = gre_get_mtu,
1422         .send           = gre_send,
1423 };