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