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