]> git.proxmox.com Git - ovs.git/blob - datapath/tunnel.c
datapath: Consider tunnels to have no MTU, fixing jumbo frame support.
[ovs.git] / datapath / tunnel.c
1 /*
2 * Copyright (c) 2010, 2011 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
4 *
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
7 */
8
9 #include <linux/if_arp.h>
10 #include <linux/if_ether.h>
11 #include <linux/ip.h>
12 #include <linux/if_vlan.h>
13 #include <linux/in.h>
14 #include <linux/in_route.h>
15 #include <linux/jhash.h>
16 #include <linux/kernel.h>
17 #include <linux/version.h>
18 #include <linux/workqueue.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/route.h>
29 #include <net/xfrm.h>
30
31 #include "actions.h"
32 #include "checksum.h"
33 #include "datapath.h"
34 #include "table.h"
35 #include "tunnel.h"
36 #include "vport.h"
37 #include "vport-generic.h"
38 #include "vport-internal_dev.h"
39
40 #ifdef NEED_CACHE_TIMEOUT
41 /*
42 * On kernels where we can't quickly detect changes in the rest of the system
43 * we use an expiration time to invalidate the cache. A shorter expiration
44 * reduces the length of time that we may potentially blackhole packets while
45 * a longer time increases performance by reducing the frequency that the
46 * cache needs to be rebuilt. A variety of factors may cause the cache to be
47 * invalidated before the expiration time but this is the maximum. The time
48 * is expressed in jiffies.
49 */
50 #define MAX_CACHE_EXP HZ
51 #endif
52
53 /*
54 * Interval to check for and remove caches that are no longer valid. Caches
55 * are checked for validity before they are used for packet encapsulation and
56 * old caches are removed at that time. However, if no packets are sent through
57 * the tunnel then the cache will never be destroyed. Since it holds
58 * references to a number of system objects, the cache will continue to use
59 * system resources by not allowing those objects to be destroyed. The cache
60 * cleaner is periodically run to free invalid caches. It does not
61 * significantly affect system performance. A lower interval will release
62 * resources faster but will itself consume resources by requiring more frequent
63 * checks. A longer interval may result in messages being printed to the kernel
64 * message buffer about unreleased resources. The interval is expressed in
65 * jiffies.
66 */
67 #define CACHE_CLEANER_INTERVAL (5 * HZ)
68
69 #define CACHE_DATA_ALIGN 16
70
71 static struct tbl __rcu *port_table __read_mostly;
72
73 static void cache_cleaner(struct work_struct *work);
74 static DECLARE_DELAYED_WORK(cache_cleaner_wq, cache_cleaner);
75
76 /*
77 * These are just used as an optimization: they don't require any kind of
78 * synchronization because we could have just as easily read the value before
79 * the port change happened.
80 */
81 static unsigned int key_local_remote_ports __read_mostly;
82 static unsigned int key_remote_ports __read_mostly;
83 static unsigned int local_remote_ports __read_mostly;
84 static unsigned int remote_ports __read_mostly;
85
86 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
87 #define rt_dst(rt) (rt->dst)
88 #else
89 #define rt_dst(rt) (rt->u.dst)
90 #endif
91
92 static inline struct vport *tnl_vport_to_vport(const struct tnl_vport *tnl_vport)
93 {
94 return vport_from_priv(tnl_vport);
95 }
96
97 static inline struct tnl_vport *tnl_vport_table_cast(const struct tbl_node *node)
98 {
99 return container_of(node, struct tnl_vport, tbl_node);
100 }
101
102 /* This is analogous to rtnl_dereference for the tunnel cache. It checks that
103 * cache_lock is held, so it is only for update side code.
104 */
105 static inline struct tnl_cache *cache_dereference(struct tnl_vport *tnl_vport)
106 {
107 return rcu_dereference_protected(tnl_vport->cache,
108 lockdep_is_held(&tnl_vport->cache_lock));
109 }
110
111 static inline void schedule_cache_cleaner(void)
112 {
113 schedule_delayed_work(&cache_cleaner_wq, CACHE_CLEANER_INTERVAL);
114 }
115
116 static void free_cache(struct tnl_cache *cache)
117 {
118 if (!cache)
119 return;
120
121 flow_put(cache->flow);
122 ip_rt_put(cache->rt);
123 kfree(cache);
124 }
125
126 static void free_config_rcu(struct rcu_head *rcu)
127 {
128 struct tnl_mutable_config *c = container_of(rcu, struct tnl_mutable_config, rcu);
129 kfree(c);
130 }
131
132 static void free_cache_rcu(struct rcu_head *rcu)
133 {
134 struct tnl_cache *c = container_of(rcu, struct tnl_cache, rcu);
135 free_cache(c);
136 }
137
138 static void assign_config_rcu(struct vport *vport,
139 struct tnl_mutable_config *new_config)
140 {
141 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
142 struct tnl_mutable_config *old_config;
143
144 old_config = rtnl_dereference(tnl_vport->mutable);
145 rcu_assign_pointer(tnl_vport->mutable, new_config);
146 call_rcu(&old_config->rcu, free_config_rcu);
147 }
148
149 static void assign_cache_rcu(struct vport *vport, struct tnl_cache *new_cache)
150 {
151 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
152 struct tnl_cache *old_cache;
153
154 old_cache = cache_dereference(tnl_vport);
155 rcu_assign_pointer(tnl_vport->cache, new_cache);
156
157 if (old_cache)
158 call_rcu(&old_cache->rcu, free_cache_rcu);
159 }
160
161 static unsigned int *find_port_pool(const struct tnl_mutable_config *mutable)
162 {
163 if (mutable->flags & TNL_F_IN_KEY_MATCH) {
164 if (mutable->saddr)
165 return &local_remote_ports;
166 else
167 return &remote_ports;
168 } else {
169 if (mutable->saddr)
170 return &key_local_remote_ports;
171 else
172 return &key_remote_ports;
173 }
174 }
175
176 struct port_lookup_key {
177 const struct tnl_mutable_config *mutable;
178 __be64 key;
179 u32 tunnel_type;
180 __be32 saddr;
181 __be32 daddr;
182 };
183
184 /*
185 * Modifies 'target' to store the rcu_dereferenced pointer that was used to do
186 * the comparision.
187 */
188 static int port_cmp(const struct tbl_node *node, void *target)
189 {
190 const struct tnl_vport *tnl_vport = tnl_vport_table_cast(node);
191 struct port_lookup_key *lookup = target;
192
193 lookup->mutable = rcu_dereference_rtnl(tnl_vport->mutable);
194
195 return (lookup->mutable->tunnel_type == lookup->tunnel_type &&
196 lookup->mutable->daddr == lookup->daddr &&
197 lookup->mutable->in_key == lookup->key &&
198 lookup->mutable->saddr == lookup->saddr);
199 }
200
201 static u32 port_hash(struct port_lookup_key *k)
202 {
203 u32 x = jhash_3words((__force u32)k->saddr, (__force u32)k->daddr,
204 k->tunnel_type, 0);
205 return jhash_2words((__force u64)k->key >> 32, (__force u32)k->key, x);
206 }
207
208 static u32 mutable_hash(const struct tnl_mutable_config *mutable)
209 {
210 struct port_lookup_key lookup;
211
212 lookup.saddr = mutable->saddr;
213 lookup.daddr = mutable->daddr;
214 lookup.key = mutable->in_key;
215 lookup.tunnel_type = mutable->tunnel_type;
216
217 return port_hash(&lookup);
218 }
219
220 static void check_table_empty(void)
221 {
222 struct tbl *old_table = rtnl_dereference(port_table);
223
224 if (tbl_count(old_table) == 0) {
225 cancel_delayed_work_sync(&cache_cleaner_wq);
226 rcu_assign_pointer(port_table, NULL);
227 tbl_deferred_destroy(old_table, NULL);
228 }
229 }
230
231 static int add_port(struct vport *vport)
232 {
233 struct tbl *cur_table = rtnl_dereference(port_table);
234 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
235 int err;
236
237 if (!port_table) {
238 struct tbl *new_table;
239
240 new_table = tbl_create(TBL_MIN_BUCKETS);
241 if (!new_table)
242 return -ENOMEM;
243
244 rcu_assign_pointer(port_table, new_table);
245 schedule_cache_cleaner();
246
247 } else if (tbl_count(cur_table) > tbl_n_buckets(cur_table)) {
248 struct tbl *new_table;
249
250 new_table = tbl_expand(cur_table);
251 if (IS_ERR(new_table))
252 return PTR_ERR(new_table);
253
254 rcu_assign_pointer(port_table, new_table);
255 tbl_deferred_destroy(cur_table, NULL);
256 }
257
258 err = tbl_insert(rtnl_dereference(port_table), &tnl_vport->tbl_node,
259 mutable_hash(rtnl_dereference(tnl_vport->mutable)));
260 if (err) {
261 check_table_empty();
262 return err;
263 }
264
265 (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))++;
266
267 return 0;
268 }
269
270 static int move_port(struct vport *vport, struct tnl_mutable_config *new_mutable)
271 {
272 int err;
273 struct tbl *cur_table = rtnl_dereference(port_table);
274 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
275 u32 hash;
276
277 hash = mutable_hash(new_mutable);
278 if (hash == tnl_vport->tbl_node.hash)
279 goto table_updated;
280
281 /*
282 * Ideally we should make this move atomic to avoid having gaps in
283 * finding tunnels or the possibility of failure. However, if we do
284 * find a tunnel it will always be consistent.
285 */
286 err = tbl_remove(cur_table, &tnl_vport->tbl_node);
287 if (err)
288 return err;
289
290 err = tbl_insert(cur_table, &tnl_vport->tbl_node, hash);
291 if (err) {
292 (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))--;
293 check_table_empty();
294 return err;
295 }
296
297 table_updated:
298 (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))--;
299 assign_config_rcu(vport, new_mutable);
300 (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))++;
301
302 return 0;
303 }
304
305 static int del_port(struct vport *vport)
306 {
307 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
308 int err;
309
310 err = tbl_remove(rtnl_dereference(port_table), &tnl_vport->tbl_node);
311 if (err)
312 return err;
313
314 check_table_empty();
315 (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))--;
316
317 return 0;
318 }
319
320 struct vport *tnl_find_port(__be32 saddr, __be32 daddr, __be64 key,
321 int tunnel_type,
322 const struct tnl_mutable_config **mutable)
323 {
324 struct port_lookup_key lookup;
325 struct tbl *table = rcu_dereference_rtnl(port_table);
326 struct tbl_node *tbl_node;
327
328 if (unlikely(!table))
329 return NULL;
330
331 lookup.saddr = saddr;
332 lookup.daddr = daddr;
333
334 if (tunnel_type & TNL_T_KEY_EXACT) {
335 lookup.key = key;
336 lookup.tunnel_type = tunnel_type & ~TNL_T_KEY_MATCH;
337
338 if (key_local_remote_ports) {
339 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
340 if (tbl_node)
341 goto found;
342 }
343
344 if (key_remote_ports) {
345 lookup.saddr = 0;
346
347 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
348 if (tbl_node)
349 goto found;
350
351 lookup.saddr = saddr;
352 }
353 }
354
355 if (tunnel_type & TNL_T_KEY_MATCH) {
356 lookup.key = 0;
357 lookup.tunnel_type = tunnel_type & ~TNL_T_KEY_EXACT;
358
359 if (local_remote_ports) {
360 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
361 if (tbl_node)
362 goto found;
363 }
364
365 if (remote_ports) {
366 lookup.saddr = 0;
367
368 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
369 if (tbl_node)
370 goto found;
371 }
372 }
373
374 return NULL;
375
376 found:
377 *mutable = lookup.mutable;
378 return tnl_vport_to_vport(tnl_vport_table_cast(tbl_node));
379 }
380
381 static inline void ecn_decapsulate(struct sk_buff *skb)
382 {
383 /* This is accessing the outer IP header of the tunnel, which we've
384 * already validated to be OK. skb->data is currently set to the start
385 * of the inner Ethernet header, and we've validated ETH_HLEN.
386 */
387 if (unlikely(INET_ECN_is_ce(ip_hdr(skb)->tos))) {
388 __be16 protocol = skb->protocol;
389
390 skb_set_network_header(skb, ETH_HLEN);
391
392 if (skb->protocol == htons(ETH_P_8021Q)) {
393 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
394 return;
395
396 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
397 skb_set_network_header(skb, VLAN_ETH_HLEN);
398 }
399
400 if (protocol == htons(ETH_P_IP)) {
401 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
402 + sizeof(struct iphdr))))
403 return;
404
405 IP_ECN_set_ce(ip_hdr(skb));
406 }
407 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
408 else if (protocol == htons(ETH_P_IPV6)) {
409 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
410 + sizeof(struct ipv6hdr))))
411 return;
412
413 IP6_ECN_set_ce(ipv6_hdr(skb));
414 }
415 #endif
416 }
417 }
418
419 /* Called with rcu_read_lock. */
420 void tnl_rcv(struct vport *vport, struct sk_buff *skb)
421 {
422 /* Packets received by this function are in the following state:
423 * - skb->data points to the inner Ethernet header.
424 * - The inner Ethernet header is in the linear data area.
425 * - skb->csum does not include the inner Ethernet header.
426 * - The layer pointers point at the outer headers.
427 */
428
429 struct ethhdr *eh = (struct ethhdr *)skb->data;
430
431 if (likely(ntohs(eh->h_proto) >= 1536))
432 skb->protocol = eh->h_proto;
433 else
434 skb->protocol = htons(ETH_P_802_2);
435
436 skb_dst_drop(skb);
437 nf_reset(skb);
438 secpath_reset(skb);
439
440 ecn_decapsulate(skb);
441 compute_ip_summed(skb, false);
442
443 vport_receive(vport, skb);
444 }
445
446 static bool check_ipv4_address(__be32 addr)
447 {
448 if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr)
449 || ipv4_is_loopback(addr) || ipv4_is_zeronet(addr))
450 return false;
451
452 return true;
453 }
454
455 static bool ipv4_should_icmp(struct sk_buff *skb)
456 {
457 struct iphdr *old_iph = ip_hdr(skb);
458
459 /* Don't respond to L2 broadcast. */
460 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
461 return false;
462
463 /* Don't respond to L3 broadcast or invalid addresses. */
464 if (!check_ipv4_address(old_iph->daddr) ||
465 !check_ipv4_address(old_iph->saddr))
466 return false;
467
468 /* Only respond to the first fragment. */
469 if (old_iph->frag_off & htons(IP_OFFSET))
470 return false;
471
472 /* Don't respond to ICMP error messages. */
473 if (old_iph->protocol == IPPROTO_ICMP) {
474 u8 icmp_type, *icmp_typep;
475
476 icmp_typep = skb_header_pointer(skb, (u8 *)old_iph +
477 (old_iph->ihl << 2) +
478 offsetof(struct icmphdr, type) -
479 skb->data, sizeof(icmp_type),
480 &icmp_type);
481
482 if (!icmp_typep)
483 return false;
484
485 if (*icmp_typep > NR_ICMP_TYPES
486 || (*icmp_typep <= ICMP_PARAMETERPROB
487 && *icmp_typep != ICMP_ECHOREPLY
488 && *icmp_typep != ICMP_ECHO))
489 return false;
490 }
491
492 return true;
493 }
494
495 static void ipv4_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
496 unsigned int mtu, unsigned int payload_length)
497 {
498 struct iphdr *iph, *old_iph = ip_hdr(skb);
499 struct icmphdr *icmph;
500 u8 *payload;
501
502 iph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
503 icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
504 payload = skb_put(nskb, payload_length);
505
506 /* IP */
507 iph->version = 4;
508 iph->ihl = sizeof(struct iphdr) >> 2;
509 iph->tos = (old_iph->tos & IPTOS_TOS_MASK) |
510 IPTOS_PREC_INTERNETCONTROL;
511 iph->tot_len = htons(sizeof(struct iphdr)
512 + sizeof(struct icmphdr)
513 + payload_length);
514 get_random_bytes(&iph->id, sizeof(iph->id));
515 iph->frag_off = 0;
516 iph->ttl = IPDEFTTL;
517 iph->protocol = IPPROTO_ICMP;
518 iph->daddr = old_iph->saddr;
519 iph->saddr = old_iph->daddr;
520
521 ip_send_check(iph);
522
523 /* ICMP */
524 icmph->type = ICMP_DEST_UNREACH;
525 icmph->code = ICMP_FRAG_NEEDED;
526 icmph->un.gateway = htonl(mtu);
527 icmph->checksum = 0;
528
529 nskb->csum = csum_partial((u8 *)icmph, sizeof(struct icmphdr), 0);
530 nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_iph - skb->data,
531 payload, payload_length,
532 nskb->csum);
533 icmph->checksum = csum_fold(nskb->csum);
534 }
535
536 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
537 static bool ipv6_should_icmp(struct sk_buff *skb)
538 {
539 struct ipv6hdr *old_ipv6h = ipv6_hdr(skb);
540 int addr_type;
541 int payload_off = (u8 *)(old_ipv6h + 1) - skb->data;
542 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
543
544 /* Check source address is valid. */
545 addr_type = ipv6_addr_type(&old_ipv6h->saddr);
546 if (addr_type & IPV6_ADDR_MULTICAST || addr_type == IPV6_ADDR_ANY)
547 return false;
548
549 /* Don't reply to unspecified addresses. */
550 if (ipv6_addr_type(&old_ipv6h->daddr) == IPV6_ADDR_ANY)
551 return false;
552
553 /* Don't respond to ICMP error messages. */
554 payload_off = ipv6_skip_exthdr(skb, payload_off, &nexthdr);
555 if (payload_off < 0)
556 return false;
557
558 if (nexthdr == NEXTHDR_ICMP) {
559 u8 icmp_type, *icmp_typep;
560
561 icmp_typep = skb_header_pointer(skb, payload_off +
562 offsetof(struct icmp6hdr,
563 icmp6_type),
564 sizeof(icmp_type), &icmp_type);
565
566 if (!icmp_typep || !(*icmp_typep & ICMPV6_INFOMSG_MASK))
567 return false;
568 }
569
570 return true;
571 }
572
573 static void ipv6_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
574 unsigned int mtu, unsigned int payload_length)
575 {
576 struct ipv6hdr *ipv6h, *old_ipv6h = ipv6_hdr(skb);
577 struct icmp6hdr *icmp6h;
578 u8 *payload;
579
580 ipv6h = (struct ipv6hdr *)skb_put(nskb, sizeof(struct ipv6hdr));
581 icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
582 payload = skb_put(nskb, payload_length);
583
584 /* IPv6 */
585 ipv6h->version = 6;
586 ipv6h->priority = 0;
587 memset(&ipv6h->flow_lbl, 0, sizeof(ipv6h->flow_lbl));
588 ipv6h->payload_len = htons(sizeof(struct icmp6hdr)
589 + payload_length);
590 ipv6h->nexthdr = NEXTHDR_ICMP;
591 ipv6h->hop_limit = IPV6_DEFAULT_HOPLIMIT;
592 ipv6_addr_copy(&ipv6h->daddr, &old_ipv6h->saddr);
593 ipv6_addr_copy(&ipv6h->saddr, &old_ipv6h->daddr);
594
595 /* ICMPv6 */
596 icmp6h->icmp6_type = ICMPV6_PKT_TOOBIG;
597 icmp6h->icmp6_code = 0;
598 icmp6h->icmp6_cksum = 0;
599 icmp6h->icmp6_mtu = htonl(mtu);
600
601 nskb->csum = csum_partial((u8 *)icmp6h, sizeof(struct icmp6hdr), 0);
602 nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_ipv6h - skb->data,
603 payload, payload_length,
604 nskb->csum);
605 icmp6h->icmp6_cksum = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
606 sizeof(struct icmp6hdr)
607 + payload_length,
608 ipv6h->nexthdr, nskb->csum);
609 }
610 #endif /* IPv6 */
611
612 bool tnl_frag_needed(struct vport *vport, const struct tnl_mutable_config *mutable,
613 struct sk_buff *skb, unsigned int mtu, __be64 flow_key)
614 {
615 unsigned int eth_hdr_len = ETH_HLEN;
616 unsigned int total_length = 0, header_length = 0, payload_length;
617 struct ethhdr *eh, *old_eh = eth_hdr(skb);
618 struct sk_buff *nskb;
619
620 /* Sanity check */
621 if (skb->protocol == htons(ETH_P_IP)) {
622 if (mtu < IP_MIN_MTU)
623 return false;
624
625 if (!ipv4_should_icmp(skb))
626 return true;
627 }
628 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
629 else if (skb->protocol == htons(ETH_P_IPV6)) {
630 if (mtu < IPV6_MIN_MTU)
631 return false;
632
633 /*
634 * In theory we should do PMTUD on IPv6 multicast messages but
635 * we don't have an address to send from so just fragment.
636 */
637 if (ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST)
638 return false;
639
640 if (!ipv6_should_icmp(skb))
641 return true;
642 }
643 #endif
644 else
645 return false;
646
647 /* Allocate */
648 if (old_eh->h_proto == htons(ETH_P_8021Q))
649 eth_hdr_len = VLAN_ETH_HLEN;
650
651 payload_length = skb->len - eth_hdr_len;
652 if (skb->protocol == htons(ETH_P_IP)) {
653 header_length = sizeof(struct iphdr) + sizeof(struct icmphdr);
654 total_length = min_t(unsigned int, header_length +
655 payload_length, 576);
656 }
657 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
658 else {
659 header_length = sizeof(struct ipv6hdr) +
660 sizeof(struct icmp6hdr);
661 total_length = min_t(unsigned int, header_length +
662 payload_length, IPV6_MIN_MTU);
663 }
664 #endif
665
666 payload_length = total_length - header_length;
667
668 nskb = dev_alloc_skb(NET_IP_ALIGN + eth_hdr_len + header_length +
669 payload_length);
670 if (!nskb)
671 return false;
672
673 skb_reserve(nskb, NET_IP_ALIGN);
674
675 /* Ethernet / VLAN */
676 eh = (struct ethhdr *)skb_put(nskb, eth_hdr_len);
677 memcpy(eh->h_dest, old_eh->h_source, ETH_ALEN);
678 memcpy(eh->h_source, mutable->eth_addr, ETH_ALEN);
679 nskb->protocol = eh->h_proto = old_eh->h_proto;
680 if (old_eh->h_proto == htons(ETH_P_8021Q)) {
681 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)eh;
682
683 vh->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
684 vh->h_vlan_encapsulated_proto = skb->protocol;
685 }
686 skb_reset_mac_header(nskb);
687
688 /* Protocol */
689 if (skb->protocol == htons(ETH_P_IP))
690 ipv4_build_icmp(skb, nskb, mtu, payload_length);
691 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
692 else
693 ipv6_build_icmp(skb, nskb, mtu, payload_length);
694 #endif
695
696 /*
697 * Assume that flow based keys are symmetric with respect to input
698 * and output and use the key that we were going to put on the
699 * outgoing packet for the fake received packet. If the keys are
700 * not symmetric then PMTUD needs to be disabled since we won't have
701 * any way of synthesizing packets.
702 */
703 if ((mutable->flags & (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION)) ==
704 (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION))
705 OVS_CB(nskb)->tun_id = flow_key;
706
707 compute_ip_summed(nskb, false);
708 vport_receive(vport, nskb);
709
710 return true;
711 }
712
713 static bool check_mtu(struct sk_buff *skb,
714 struct vport *vport,
715 const struct tnl_mutable_config *mutable,
716 const struct rtable *rt, __be16 *frag_offp)
717 {
718 bool pmtud = mutable->flags & TNL_F_PMTUD;
719 __be16 frag_off = 0;
720 int mtu;
721
722 if (pmtud) {
723 frag_off = htons(IP_DF);
724
725 mtu = dst_mtu(&rt_dst(rt))
726 - ETH_HLEN
727 - mutable->tunnel_hlen
728 - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ?
729 VLAN_HLEN : 0);
730 }
731
732 if (skb->protocol == htons(ETH_P_IP)) {
733 struct iphdr *iph = ip_hdr(skb);
734
735 frag_off |= iph->frag_off & htons(IP_DF);
736
737 if (pmtud && iph->frag_off & htons(IP_DF)) {
738 mtu = max(mtu, IP_MIN_MTU);
739
740 if (ntohs(iph->tot_len) > mtu &&
741 tnl_frag_needed(vport, mutable, skb, mtu,
742 OVS_CB(skb)->tun_id))
743 return false;
744 }
745 }
746 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
747 else if (skb->protocol == htons(ETH_P_IPV6)) {
748 unsigned int packet_length = skb->len - ETH_HLEN
749 - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ?
750 VLAN_HLEN : 0);
751
752 /* IPv6 requires PMTUD if the packet is above the minimum MTU. */
753 if (packet_length > IPV6_MIN_MTU)
754 frag_off = htons(IP_DF);
755
756 if (pmtud) {
757 mtu = max(mtu, IPV6_MIN_MTU);
758
759 if (packet_length > mtu &&
760 tnl_frag_needed(vport, mutable, skb, mtu,
761 OVS_CB(skb)->tun_id))
762 return false;
763 }
764 }
765 #endif
766
767 *frag_offp = frag_off;
768 return true;
769 }
770
771 static void create_tunnel_header(const struct vport *vport,
772 const struct tnl_mutable_config *mutable,
773 const struct rtable *rt, void *header)
774 {
775 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
776 struct iphdr *iph = header;
777
778 iph->version = 4;
779 iph->ihl = sizeof(struct iphdr) >> 2;
780 iph->frag_off = htons(IP_DF);
781 iph->protocol = tnl_vport->tnl_ops->ipproto;
782 iph->tos = mutable->tos;
783 iph->daddr = rt->rt_dst;
784 iph->saddr = rt->rt_src;
785 iph->ttl = mutable->ttl;
786 if (!iph->ttl)
787 iph->ttl = dst_metric(&rt_dst(rt), RTAX_HOPLIMIT);
788
789 tnl_vport->tnl_ops->build_header(vport, mutable, iph + 1);
790 }
791
792 static inline void *get_cached_header(const struct tnl_cache *cache)
793 {
794 return (void *)cache + ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN);
795 }
796
797 static inline bool check_cache_valid(const struct tnl_cache *cache,
798 const struct tnl_mutable_config *mutable)
799 {
800 return cache &&
801 #ifdef NEED_CACHE_TIMEOUT
802 time_before(jiffies, cache->expiration) &&
803 #endif
804 #ifdef HAVE_RT_GENID
805 atomic_read(&init_net.ipv4.rt_genid) == cache->rt->rt_genid &&
806 #endif
807 #ifdef HAVE_HH_SEQ
808 rt_dst(cache->rt).hh->hh_lock.sequence == cache->hh_seq &&
809 #endif
810 mutable->seq == cache->mutable_seq &&
811 (!is_internal_dev(rt_dst(cache->rt).dev) ||
812 (cache->flow && !cache->flow->dead));
813 }
814
815 static int cache_cleaner_cb(struct tbl_node *tbl_node, void *aux)
816 {
817 struct tnl_vport *tnl_vport = tnl_vport_table_cast(tbl_node);
818 const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
819 const struct tnl_cache *cache = rcu_dereference(tnl_vport->cache);
820
821 if (cache && !check_cache_valid(cache, mutable) &&
822 spin_trylock_bh(&tnl_vport->cache_lock)) {
823 assign_cache_rcu(tnl_vport_to_vport(tnl_vport), NULL);
824 spin_unlock_bh(&tnl_vport->cache_lock);
825 }
826
827 return 0;
828 }
829
830 static void cache_cleaner(struct work_struct *work)
831 {
832 schedule_cache_cleaner();
833
834 rcu_read_lock();
835 tbl_foreach(rcu_dereference(port_table), cache_cleaner_cb, NULL);
836 rcu_read_unlock();
837 }
838
839 static inline void create_eth_hdr(struct tnl_cache *cache,
840 const struct rtable *rt)
841 {
842 void *cache_data = get_cached_header(cache);
843 int hh_len = rt_dst(rt).hh->hh_len;
844 int hh_off = HH_DATA_ALIGN(rt_dst(rt).hh->hh_len) - hh_len;
845
846 #ifdef HAVE_HH_SEQ
847 unsigned hh_seq;
848
849 do {
850 hh_seq = read_seqbegin(&rt_dst(rt).hh->hh_lock);
851 memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
852 } while (read_seqretry(&rt_dst(rt).hh->hh_lock, hh_seq));
853
854 cache->hh_seq = hh_seq;
855 #else
856 read_lock_bh(&rt_dst(rt).hh->hh_lock);
857 memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
858 read_unlock_bh(&rt_dst(rt).hh->hh_lock);
859 #endif
860 }
861
862 static struct tnl_cache *build_cache(struct vport *vport,
863 const struct tnl_mutable_config *mutable,
864 struct rtable *rt)
865 {
866 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
867 struct tnl_cache *cache;
868 void *cache_data;
869 int cache_len;
870
871 if (!(mutable->flags & TNL_F_HDR_CACHE))
872 return NULL;
873
874 /*
875 * If there is no entry in the ARP cache or if this device does not
876 * support hard header caching just fall back to the IP stack.
877 */
878 if (!rt_dst(rt).hh)
879 return NULL;
880
881 /*
882 * If lock is contended fall back to directly building the header.
883 * We're not going to help performance by sitting here spinning.
884 */
885 if (!spin_trylock_bh(&tnl_vport->cache_lock))
886 return NULL;
887
888 cache = cache_dereference(tnl_vport);
889 if (check_cache_valid(cache, mutable))
890 goto unlock;
891 else
892 cache = NULL;
893
894 cache_len = rt_dst(rt).hh->hh_len + mutable->tunnel_hlen;
895
896 cache = kzalloc(ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN) +
897 cache_len, GFP_ATOMIC);
898 if (!cache)
899 goto unlock;
900
901 cache->len = cache_len;
902
903 create_eth_hdr(cache, rt);
904 cache_data = get_cached_header(cache) + rt_dst(rt).hh->hh_len;
905
906 create_tunnel_header(vport, mutable, rt, cache_data);
907
908 cache->mutable_seq = mutable->seq;
909 cache->rt = rt;
910 #ifdef NEED_CACHE_TIMEOUT
911 cache->expiration = jiffies + tnl_vport->cache_exp_interval;
912 #endif
913
914 if (is_internal_dev(rt_dst(rt).dev)) {
915 struct sw_flow_key flow_key;
916 struct tbl_node *flow_node;
917 struct vport *dst_vport;
918 struct sk_buff *skb;
919 bool is_frag;
920 int err;
921
922 dst_vport = internal_dev_get_vport(rt_dst(rt).dev);
923 if (!dst_vport)
924 goto done;
925
926 skb = alloc_skb(cache->len, GFP_ATOMIC);
927 if (!skb)
928 goto done;
929
930 __skb_put(skb, cache->len);
931 memcpy(skb->data, get_cached_header(cache), cache->len);
932
933 err = flow_extract(skb, dst_vport->port_no, &flow_key, &is_frag);
934
935 kfree_skb(skb);
936 if (err || is_frag)
937 goto done;
938
939 flow_node = tbl_lookup(rcu_dereference(dst_vport->dp->table),
940 &flow_key, flow_hash(&flow_key),
941 flow_cmp);
942 if (flow_node) {
943 struct sw_flow *flow = flow_cast(flow_node);
944
945 cache->flow = flow;
946 flow_hold(flow);
947 }
948 }
949
950 done:
951 assign_cache_rcu(vport, cache);
952
953 unlock:
954 spin_unlock_bh(&tnl_vport->cache_lock);
955
956 return cache;
957 }
958
959 static struct rtable *find_route(struct vport *vport,
960 const struct tnl_mutable_config *mutable,
961 u8 tos, struct tnl_cache **cache)
962 {
963 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
964 struct tnl_cache *cur_cache = rcu_dereference(tnl_vport->cache);
965
966 *cache = NULL;
967 tos = RT_TOS(tos);
968
969 if (likely(tos == mutable->tos && check_cache_valid(cur_cache, mutable))) {
970 *cache = cur_cache;
971 return cur_cache->rt;
972 } else {
973 struct rtable *rt;
974 struct flowi fl = { .nl_u = { .ip4_u =
975 { .daddr = mutable->daddr,
976 .saddr = mutable->saddr,
977 .tos = tos } },
978 .proto = tnl_vport->tnl_ops->ipproto };
979
980 if (unlikely(ip_route_output_key(&init_net, &rt, &fl)))
981 return NULL;
982
983 if (likely(tos == mutable->tos))
984 *cache = build_cache(vport, mutable, rt);
985
986 return rt;
987 }
988 }
989
990 static struct sk_buff *check_headroom(struct sk_buff *skb, int headroom)
991 {
992 if (skb_headroom(skb) < headroom || skb_header_cloned(skb)) {
993 struct sk_buff *nskb = skb_realloc_headroom(skb, headroom + 16);
994 if (unlikely(!nskb)) {
995 kfree_skb(skb);
996 return ERR_PTR(-ENOMEM);
997 }
998
999 set_skb_csum_bits(skb, nskb);
1000
1001 if (skb->sk)
1002 skb_set_owner_w(nskb, skb->sk);
1003
1004 kfree_skb(skb);
1005 return nskb;
1006 }
1007
1008 return skb;
1009 }
1010
1011 static inline bool need_linearize(const struct sk_buff *skb)
1012 {
1013 int i;
1014
1015 if (unlikely(skb_shinfo(skb)->frag_list))
1016 return true;
1017
1018 /*
1019 * Generally speaking we should linearize if there are paged frags.
1020 * However, if all of the refcounts are 1 we know nobody else can
1021 * change them from underneath us and we can skip the linearization.
1022 */
1023 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1024 if (unlikely(page_count(skb_shinfo(skb)->frags[0].page) > 1))
1025 return true;
1026
1027 return false;
1028 }
1029
1030 static struct sk_buff *handle_offloads(struct sk_buff *skb,
1031 const struct tnl_mutable_config *mutable,
1032 const struct rtable *rt)
1033 {
1034 int min_headroom;
1035 int err;
1036
1037 forward_ip_summed(skb);
1038
1039 err = vswitch_skb_checksum_setup(skb);
1040 if (unlikely(err))
1041 goto error_free;
1042
1043 min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
1044 + mutable->tunnel_hlen;
1045
1046 if (skb_is_gso(skb)) {
1047 struct sk_buff *nskb;
1048
1049 /*
1050 * If we are doing GSO on a pskb it is better to make sure that
1051 * the headroom is correct now. We will only have to copy the
1052 * portion in the linear data area and GSO will preserve
1053 * headroom when it creates the segments. This is particularly
1054 * beneficial on Xen where we get a lot of GSO pskbs.
1055 * Conversely, we avoid copying if it is just to get our own
1056 * writable clone because GSO will do the copy for us.
1057 */
1058 if (skb_headroom(skb) < min_headroom) {
1059 skb = check_headroom(skb, min_headroom);
1060 if (IS_ERR(skb)) {
1061 err = PTR_ERR(skb);
1062 goto error;
1063 }
1064 }
1065
1066 nskb = skb_gso_segment(skb, 0);
1067 kfree_skb(skb);
1068 if (IS_ERR(nskb)) {
1069 err = PTR_ERR(nskb);
1070 goto error;
1071 }
1072
1073 skb = nskb;
1074 } else {
1075 skb = check_headroom(skb, min_headroom);
1076 if (IS_ERR(skb)) {
1077 err = PTR_ERR(skb);
1078 goto error;
1079 }
1080
1081 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1082 /*
1083 * Pages aren't locked and could change at any time.
1084 * If this happens after we compute the checksum, the
1085 * checksum will be wrong. We linearize now to avoid
1086 * this problem.
1087 */
1088 if (unlikely(need_linearize(skb))) {
1089 err = __skb_linearize(skb);
1090 if (unlikely(err))
1091 goto error_free;
1092 }
1093
1094 err = skb_checksum_help(skb);
1095 if (unlikely(err))
1096 goto error_free;
1097 } else if (skb->ip_summed == CHECKSUM_COMPLETE)
1098 skb->ip_summed = CHECKSUM_NONE;
1099 }
1100
1101 return skb;
1102
1103 error_free:
1104 kfree_skb(skb);
1105 error:
1106 return ERR_PTR(err);
1107 }
1108
1109 static int send_frags(struct sk_buff *skb,
1110 const struct tnl_mutable_config *mutable)
1111 {
1112 int sent_len;
1113 int err;
1114
1115 sent_len = 0;
1116 while (skb) {
1117 struct sk_buff *next = skb->next;
1118 int frag_len = skb->len - mutable->tunnel_hlen;
1119
1120 skb->next = NULL;
1121 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
1122
1123 err = ip_local_out(skb);
1124 if (likely(net_xmit_eval(err) == 0))
1125 sent_len += frag_len;
1126 else {
1127 skb = next;
1128 goto free_frags;
1129 }
1130
1131 skb = next;
1132 }
1133
1134 return sent_len;
1135
1136 free_frags:
1137 /*
1138 * There's no point in continuing to send fragments once one has been
1139 * dropped so just free the rest. This may help improve the congestion
1140 * that caused the first packet to be dropped.
1141 */
1142 tnl_free_linked_skbs(skb);
1143 return sent_len;
1144 }
1145
1146 int tnl_send(struct vport *vport, struct sk_buff *skb)
1147 {
1148 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1149 const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
1150
1151 enum vport_err_type err = VPORT_E_TX_ERROR;
1152 struct rtable *rt;
1153 struct dst_entry *unattached_dst = NULL;
1154 struct tnl_cache *cache;
1155 int sent_len = 0;
1156 __be16 frag_off = 0;
1157 u8 ttl;
1158 u8 inner_tos;
1159 u8 tos;
1160
1161 /* Validate the protocol headers before we try to use them. */
1162 if (skb->protocol == htons(ETH_P_8021Q)) {
1163 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
1164 goto error_free;
1165
1166 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1167 skb_set_network_header(skb, VLAN_ETH_HLEN);
1168 }
1169
1170 if (skb->protocol == htons(ETH_P_IP)) {
1171 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
1172 + sizeof(struct iphdr))))
1173 skb->protocol = 0;
1174 }
1175 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1176 else if (skb->protocol == htons(ETH_P_IPV6)) {
1177 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
1178 + sizeof(struct ipv6hdr))))
1179 skb->protocol = 0;
1180 }
1181 #endif
1182
1183 /* ToS */
1184 if (skb->protocol == htons(ETH_P_IP))
1185 inner_tos = ip_hdr(skb)->tos;
1186 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1187 else if (skb->protocol == htons(ETH_P_IPV6))
1188 inner_tos = ipv6_get_dsfield(ipv6_hdr(skb));
1189 #endif
1190 else
1191 inner_tos = 0;
1192
1193 if (mutable->flags & TNL_F_TOS_INHERIT)
1194 tos = inner_tos;
1195 else
1196 tos = mutable->tos;
1197
1198 tos = INET_ECN_encapsulate(tos, inner_tos);
1199
1200 /* Route lookup */
1201 rt = find_route(vport, mutable, tos, &cache);
1202 if (unlikely(!rt))
1203 goto error_free;
1204 if (unlikely(!cache))
1205 unattached_dst = &rt_dst(rt);
1206
1207 /* Reset SKB */
1208 nf_reset(skb);
1209 secpath_reset(skb);
1210 skb_dst_drop(skb);
1211
1212 /* Offloading */
1213 skb = handle_offloads(skb, mutable, rt);
1214 if (IS_ERR(skb))
1215 goto error;
1216
1217 /* MTU */
1218 if (unlikely(!check_mtu(skb, vport, mutable, rt, &frag_off))) {
1219 err = VPORT_E_TX_DROPPED;
1220 goto error_free;
1221 }
1222
1223 /*
1224 * If we are over the MTU, allow the IP stack to handle fragmentation.
1225 * Fragmentation is a slow path anyways.
1226 */
1227 if (unlikely(skb->len + mutable->tunnel_hlen > dst_mtu(&rt_dst(rt)) &&
1228 cache)) {
1229 unattached_dst = &rt_dst(rt);
1230 dst_hold(unattached_dst);
1231 cache = NULL;
1232 }
1233
1234 /* TTL */
1235 ttl = mutable->ttl;
1236 if (!ttl)
1237 ttl = dst_metric(&rt_dst(rt), RTAX_HOPLIMIT);
1238
1239 if (mutable->flags & TNL_F_TTL_INHERIT) {
1240 if (skb->protocol == htons(ETH_P_IP))
1241 ttl = ip_hdr(skb)->ttl;
1242 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1243 else if (skb->protocol == htons(ETH_P_IPV6))
1244 ttl = ipv6_hdr(skb)->hop_limit;
1245 #endif
1246 }
1247
1248 while (skb) {
1249 struct iphdr *iph;
1250 struct sk_buff *next_skb = skb->next;
1251 skb->next = NULL;
1252
1253 if (likely(cache)) {
1254 skb_push(skb, cache->len);
1255 memcpy(skb->data, get_cached_header(cache), cache->len);
1256 skb_reset_mac_header(skb);
1257 skb_set_network_header(skb, rt_dst(rt).hh->hh_len);
1258
1259 } else {
1260 skb_push(skb, mutable->tunnel_hlen);
1261 create_tunnel_header(vport, mutable, rt, skb->data);
1262 skb_reset_network_header(skb);
1263
1264 if (next_skb)
1265 skb_dst_set(skb, dst_clone(unattached_dst));
1266 else {
1267 skb_dst_set(skb, unattached_dst);
1268 unattached_dst = NULL;
1269 }
1270 }
1271 skb_set_transport_header(skb, skb_network_offset(skb) + sizeof(struct iphdr));
1272
1273 iph = ip_hdr(skb);
1274 iph->tos = tos;
1275 iph->ttl = ttl;
1276 iph->frag_off = frag_off;
1277 ip_select_ident(iph, &rt_dst(rt), NULL);
1278
1279 skb = tnl_vport->tnl_ops->update_header(vport, mutable, &rt_dst(rt), skb);
1280 if (unlikely(!skb))
1281 goto next;
1282
1283 if (likely(cache)) {
1284 int orig_len = skb->len - cache->len;
1285 struct vport *cache_vport = internal_dev_get_vport(rt_dst(rt).dev);
1286
1287 skb->protocol = htons(ETH_P_IP);
1288 iph = ip_hdr(skb);
1289 iph->tot_len = htons(skb->len - skb_network_offset(skb));
1290 ip_send_check(iph);
1291
1292 if (cache_vport) {
1293 OVS_CB(skb)->flow = cache->flow;
1294 compute_ip_summed(skb, true);
1295 vport_receive(cache_vport, skb);
1296 sent_len += orig_len;
1297 } else {
1298 int xmit_err;
1299
1300 skb->dev = rt_dst(rt).dev;
1301 xmit_err = dev_queue_xmit(skb);
1302
1303 if (likely(net_xmit_eval(xmit_err) == 0))
1304 sent_len += orig_len;
1305 }
1306 } else
1307 sent_len += send_frags(skb, mutable);
1308
1309 next:
1310 skb = next_skb;
1311 }
1312
1313 if (unlikely(sent_len == 0))
1314 vport_record_error(vport, VPORT_E_TX_DROPPED);
1315
1316 goto out;
1317
1318 error_free:
1319 tnl_free_linked_skbs(skb);
1320 error:
1321 dst_release(unattached_dst);
1322 vport_record_error(vport, err);
1323 out:
1324 return sent_len;
1325 }
1326
1327 static const struct nla_policy tnl_policy[ODP_TUNNEL_ATTR_MAX + 1] = {
1328 [ODP_TUNNEL_ATTR_FLAGS] = { .type = NLA_U32 },
1329 [ODP_TUNNEL_ATTR_DST_IPV4] = { .type = NLA_U32 },
1330 [ODP_TUNNEL_ATTR_SRC_IPV4] = { .type = NLA_U32 },
1331 [ODP_TUNNEL_ATTR_OUT_KEY] = { .type = NLA_U64 },
1332 [ODP_TUNNEL_ATTR_IN_KEY] = { .type = NLA_U64 },
1333 [ODP_TUNNEL_ATTR_TOS] = { .type = NLA_U8 },
1334 [ODP_TUNNEL_ATTR_TTL] = { .type = NLA_U8 },
1335 };
1336
1337 /* Sets ODP_TUNNEL_ATTR_* fields in 'mutable', which must initially be zeroed. */
1338 static int tnl_set_config(struct nlattr *options, const struct tnl_ops *tnl_ops,
1339 const struct vport *cur_vport,
1340 struct tnl_mutable_config *mutable)
1341 {
1342 const struct vport *old_vport;
1343 const struct tnl_mutable_config *old_mutable;
1344 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
1345 int err;
1346
1347 if (!options)
1348 return -EINVAL;
1349
1350 err = nla_parse_nested(a, ODP_TUNNEL_ATTR_MAX, options, tnl_policy);
1351 if (err)
1352 return err;
1353
1354 if (!a[ODP_TUNNEL_ATTR_FLAGS] || !a[ODP_TUNNEL_ATTR_DST_IPV4])
1355 return -EINVAL;
1356
1357 mutable->flags = nla_get_u32(a[ODP_TUNNEL_ATTR_FLAGS]) & TNL_F_PUBLIC;
1358
1359 if (a[ODP_TUNNEL_ATTR_SRC_IPV4])
1360 mutable->saddr = nla_get_be32(a[ODP_TUNNEL_ATTR_SRC_IPV4]);
1361 mutable->daddr = nla_get_be32(a[ODP_TUNNEL_ATTR_DST_IPV4]);
1362
1363 if (a[ODP_TUNNEL_ATTR_TOS]) {
1364 mutable->tos = nla_get_u8(a[ODP_TUNNEL_ATTR_TOS]);
1365 if (mutable->tos != RT_TOS(mutable->tos))
1366 return -EINVAL;
1367 }
1368
1369 if (a[ODP_TUNNEL_ATTR_TTL])
1370 mutable->ttl = nla_get_u8(a[ODP_TUNNEL_ATTR_TTL]);
1371
1372 mutable->tunnel_hlen = tnl_ops->hdr_len(mutable);
1373 if (mutable->tunnel_hlen < 0)
1374 return mutable->tunnel_hlen;
1375
1376 mutable->tunnel_hlen += sizeof(struct iphdr);
1377
1378 mutable->tunnel_type = tnl_ops->tunnel_type;
1379 if (!a[ODP_TUNNEL_ATTR_IN_KEY]) {
1380 mutable->tunnel_type |= TNL_T_KEY_MATCH;
1381 mutable->flags |= TNL_F_IN_KEY_MATCH;
1382 } else {
1383 mutable->tunnel_type |= TNL_T_KEY_EXACT;
1384 mutable->in_key = nla_get_be64(a[ODP_TUNNEL_ATTR_IN_KEY]);
1385 }
1386
1387 if (!a[ODP_TUNNEL_ATTR_OUT_KEY])
1388 mutable->flags |= TNL_F_OUT_KEY_ACTION;
1389 else
1390 mutable->out_key = nla_get_be64(a[ODP_TUNNEL_ATTR_OUT_KEY]);
1391
1392 old_vport = tnl_find_port(mutable->saddr, mutable->daddr,
1393 mutable->in_key, mutable->tunnel_type,
1394 &old_mutable);
1395
1396 if (old_vport && old_vport != cur_vport)
1397 return -EEXIST;
1398
1399 return 0;
1400 }
1401
1402 struct vport *tnl_create(const struct vport_parms *parms,
1403 const struct vport_ops *vport_ops,
1404 const struct tnl_ops *tnl_ops)
1405 {
1406 struct vport *vport;
1407 struct tnl_vport *tnl_vport;
1408 struct tnl_mutable_config *mutable;
1409 int initial_frag_id;
1410 int err;
1411
1412 vport = vport_alloc(sizeof(struct tnl_vport), vport_ops, parms);
1413 if (IS_ERR(vport)) {
1414 err = PTR_ERR(vport);
1415 goto error;
1416 }
1417
1418 tnl_vport = tnl_vport_priv(vport);
1419
1420 strcpy(tnl_vport->name, parms->name);
1421 tnl_vport->tnl_ops = tnl_ops;
1422
1423 mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1424 if (!mutable) {
1425 err = -ENOMEM;
1426 goto error_free_vport;
1427 }
1428
1429 vport_gen_rand_ether_addr(mutable->eth_addr);
1430
1431 get_random_bytes(&initial_frag_id, sizeof(int));
1432 atomic_set(&tnl_vport->frag_id, initial_frag_id);
1433
1434 err = tnl_set_config(parms->options, tnl_ops, NULL, mutable);
1435 if (err)
1436 goto error_free_mutable;
1437
1438 spin_lock_init(&tnl_vport->cache_lock);
1439
1440 #ifdef NEED_CACHE_TIMEOUT
1441 tnl_vport->cache_exp_interval = MAX_CACHE_EXP -
1442 (net_random() % (MAX_CACHE_EXP / 2));
1443 #endif
1444
1445 rcu_assign_pointer(tnl_vport->mutable, mutable);
1446
1447 err = add_port(vport);
1448 if (err)
1449 goto error_free_mutable;
1450
1451 return vport;
1452
1453 error_free_mutable:
1454 kfree(mutable);
1455 error_free_vport:
1456 vport_free(vport);
1457 error:
1458 return ERR_PTR(err);
1459 }
1460
1461 int tnl_set_options(struct vport *vport, struct nlattr *options)
1462 {
1463 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1464 const struct tnl_mutable_config *old_mutable;
1465 struct tnl_mutable_config *mutable;
1466 int err;
1467
1468 mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1469 if (!mutable) {
1470 err = -ENOMEM;
1471 goto error;
1472 }
1473
1474 /* Copy fields whose values should be retained. */
1475 old_mutable = rtnl_dereference(tnl_vport->mutable);
1476 mutable->seq = old_mutable->seq + 1;
1477 memcpy(mutable->eth_addr, old_mutable->eth_addr, ETH_ALEN);
1478
1479 /* Parse the others configured by userspace. */
1480 err = tnl_set_config(options, tnl_vport->tnl_ops, vport, mutable);
1481 if (err)
1482 goto error_free;
1483
1484 err = move_port(vport, mutable);
1485 if (err)
1486 goto error_free;
1487
1488 return 0;
1489
1490 error_free:
1491 kfree(mutable);
1492 error:
1493 return err;
1494 }
1495
1496 int tnl_get_options(const struct vport *vport, struct sk_buff *skb)
1497 {
1498 const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1499 const struct tnl_mutable_config *mutable = rcu_dereference_rtnl(tnl_vport->mutable);
1500
1501 NLA_PUT_U32(skb, ODP_TUNNEL_ATTR_FLAGS, mutable->flags & TNL_F_PUBLIC);
1502 NLA_PUT_BE32(skb, ODP_TUNNEL_ATTR_DST_IPV4, mutable->daddr);
1503
1504 if (!(mutable->flags & TNL_F_IN_KEY_MATCH))
1505 NLA_PUT_BE64(skb, ODP_TUNNEL_ATTR_IN_KEY, mutable->in_key);
1506 if (!(mutable->flags & TNL_F_OUT_KEY_ACTION))
1507 NLA_PUT_BE64(skb, ODP_TUNNEL_ATTR_OUT_KEY, mutable->out_key);
1508 if (mutable->saddr)
1509 NLA_PUT_BE32(skb, ODP_TUNNEL_ATTR_SRC_IPV4, mutable->saddr);
1510 if (mutable->tos)
1511 NLA_PUT_U8(skb, ODP_TUNNEL_ATTR_TOS, mutable->tos);
1512 if (mutable->ttl)
1513 NLA_PUT_U8(skb, ODP_TUNNEL_ATTR_TTL, mutable->ttl);
1514
1515 return 0;
1516
1517 nla_put_failure:
1518 return -EMSGSIZE;
1519 }
1520
1521 static void free_port_rcu(struct rcu_head *rcu)
1522 {
1523 struct tnl_vport *tnl_vport = container_of(rcu,
1524 struct tnl_vport, rcu);
1525
1526 free_cache((struct tnl_cache __force *)tnl_vport->cache);
1527 kfree((struct tnl_mutable __force *)tnl_vport->mutable);
1528 vport_free(tnl_vport_to_vport(tnl_vport));
1529 }
1530
1531 int tnl_destroy(struct vport *vport)
1532 {
1533 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1534 const struct tnl_mutable_config *mutable, *old_mutable;
1535
1536 mutable = rtnl_dereference(tnl_vport->mutable);
1537
1538 if (vport == tnl_find_port(mutable->saddr, mutable->daddr,
1539 mutable->in_key, mutable->tunnel_type,
1540 &old_mutable))
1541 del_port(vport);
1542
1543 call_rcu(&tnl_vport->rcu, free_port_rcu);
1544
1545 return 0;
1546 }
1547
1548 int tnl_set_addr(struct vport *vport, const unsigned char *addr)
1549 {
1550 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1551 struct tnl_mutable_config *mutable;
1552
1553 mutable = kmemdup(rtnl_dereference(tnl_vport->mutable),
1554 sizeof(struct tnl_mutable_config), GFP_KERNEL);
1555 if (!mutable)
1556 return -ENOMEM;
1557
1558 memcpy(mutable->eth_addr, addr, ETH_ALEN);
1559 assign_config_rcu(vport, mutable);
1560
1561 return 0;
1562 }
1563
1564 const char *tnl_get_name(const struct vport *vport)
1565 {
1566 const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1567 return tnl_vport->name;
1568 }
1569
1570 const unsigned char *tnl_get_addr(const struct vport *vport)
1571 {
1572 const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1573 return rcu_dereference_rtnl(tnl_vport->mutable)->eth_addr;
1574 }
1575
1576 void tnl_free_linked_skbs(struct sk_buff *skb)
1577 {
1578 if (unlikely(!skb))
1579 return;
1580
1581 while (skb) {
1582 struct sk_buff *next = skb->next;
1583 kfree_skb(skb);
1584 skb = next;
1585 }
1586 }