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