.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * Linux NET3: GRE over IP protocol decoder. |
---|
3 | 4 | * |
---|
4 | 5 | * Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru) |
---|
5 | | - * |
---|
6 | | - * This program is free software; you can redistribute it and/or |
---|
7 | | - * modify it under the terms of the GNU General Public License |
---|
8 | | - * as published by the Free Software Foundation; either version |
---|
9 | | - * 2 of the License, or (at your option) any later version. |
---|
10 | | - * |
---|
11 | 6 | */ |
---|
12 | 7 | |
---|
13 | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
---|
.. | .. |
---|
121 | 116 | static unsigned int gre_tap_net_id __read_mostly; |
---|
122 | 117 | static unsigned int erspan_net_id __read_mostly; |
---|
123 | 118 | |
---|
124 | | -static void ipgre_err(struct sk_buff *skb, u32 info, |
---|
125 | | - const struct tnl_ptk_info *tpi) |
---|
| 119 | +static int ipgre_err(struct sk_buff *skb, u32 info, |
---|
| 120 | + const struct tnl_ptk_info *tpi) |
---|
126 | 121 | { |
---|
127 | 122 | |
---|
128 | 123 | /* All the routers (except for Linux) return only |
---|
.. | .. |
---|
146 | 141 | unsigned int data_len = 0; |
---|
147 | 142 | struct ip_tunnel *t; |
---|
148 | 143 | |
---|
149 | | - switch (type) { |
---|
150 | | - default: |
---|
151 | | - case ICMP_PARAMETERPROB: |
---|
152 | | - return; |
---|
153 | | - |
---|
154 | | - case ICMP_DEST_UNREACH: |
---|
155 | | - switch (code) { |
---|
156 | | - case ICMP_SR_FAILED: |
---|
157 | | - case ICMP_PORT_UNREACH: |
---|
158 | | - /* Impossible event. */ |
---|
159 | | - return; |
---|
160 | | - default: |
---|
161 | | - /* All others are translated to HOST_UNREACH. |
---|
162 | | - rfc2003 contains "deep thoughts" about NET_UNREACH, |
---|
163 | | - I believe they are just ether pollution. --ANK |
---|
164 | | - */ |
---|
165 | | - break; |
---|
166 | | - } |
---|
167 | | - break; |
---|
168 | | - |
---|
169 | | - case ICMP_TIME_EXCEEDED: |
---|
170 | | - if (code != ICMP_EXC_TTL) |
---|
171 | | - return; |
---|
172 | | - data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */ |
---|
173 | | - break; |
---|
174 | | - |
---|
175 | | - case ICMP_REDIRECT: |
---|
176 | | - break; |
---|
177 | | - } |
---|
178 | | - |
---|
179 | 144 | if (tpi->proto == htons(ETH_P_TEB)) |
---|
180 | 145 | itn = net_generic(net, gre_tap_net_id); |
---|
181 | 146 | else if (tpi->proto == htons(ETH_P_ERSPAN) || |
---|
.. | .. |
---|
189 | 154 | iph->daddr, iph->saddr, tpi->key); |
---|
190 | 155 | |
---|
191 | 156 | if (!t) |
---|
192 | | - return; |
---|
| 157 | + return -ENOENT; |
---|
| 158 | + |
---|
| 159 | + switch (type) { |
---|
| 160 | + default: |
---|
| 161 | + case ICMP_PARAMETERPROB: |
---|
| 162 | + return 0; |
---|
| 163 | + |
---|
| 164 | + case ICMP_DEST_UNREACH: |
---|
| 165 | + switch (code) { |
---|
| 166 | + case ICMP_SR_FAILED: |
---|
| 167 | + case ICMP_PORT_UNREACH: |
---|
| 168 | + /* Impossible event. */ |
---|
| 169 | + return 0; |
---|
| 170 | + default: |
---|
| 171 | + /* All others are translated to HOST_UNREACH. |
---|
| 172 | + rfc2003 contains "deep thoughts" about NET_UNREACH, |
---|
| 173 | + I believe they are just ether pollution. --ANK |
---|
| 174 | + */ |
---|
| 175 | + break; |
---|
| 176 | + } |
---|
| 177 | + break; |
---|
| 178 | + |
---|
| 179 | + case ICMP_TIME_EXCEEDED: |
---|
| 180 | + if (code != ICMP_EXC_TTL) |
---|
| 181 | + return 0; |
---|
| 182 | + data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */ |
---|
| 183 | + break; |
---|
| 184 | + |
---|
| 185 | + case ICMP_REDIRECT: |
---|
| 186 | + break; |
---|
| 187 | + } |
---|
193 | 188 | |
---|
194 | 189 | #if IS_ENABLED(CONFIG_IPV6) |
---|
195 | 190 | if (tpi->proto == htons(ETH_P_IPV6) && |
---|
196 | 191 | !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4 + tpi->hdr_len, |
---|
197 | 192 | type, data_len)) |
---|
198 | | - return; |
---|
| 193 | + return 0; |
---|
199 | 194 | #endif |
---|
200 | 195 | |
---|
201 | 196 | if (t->parms.iph.daddr == 0 || |
---|
202 | 197 | ipv4_is_multicast(t->parms.iph.daddr)) |
---|
203 | | - return; |
---|
| 198 | + return 0; |
---|
204 | 199 | |
---|
205 | 200 | if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED) |
---|
206 | | - return; |
---|
| 201 | + return 0; |
---|
207 | 202 | |
---|
208 | 203 | if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO)) |
---|
209 | 204 | t->err_count++; |
---|
210 | 205 | else |
---|
211 | 206 | t->err_count = 1; |
---|
212 | 207 | t->err_time = jiffies; |
---|
| 208 | + |
---|
| 209 | + return 0; |
---|
213 | 210 | } |
---|
214 | 211 | |
---|
215 | 212 | static void gre_err(struct sk_buff *skb, u32 info) |
---|
.. | .. |
---|
239 | 236 | |
---|
240 | 237 | if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) { |
---|
241 | 238 | ipv4_update_pmtu(skb, dev_net(skb->dev), info, |
---|
242 | | - skb->dev->ifindex, 0, IPPROTO_GRE, 0); |
---|
| 239 | + skb->dev->ifindex, IPPROTO_GRE); |
---|
243 | 240 | return; |
---|
244 | 241 | } |
---|
245 | 242 | if (type == ICMP_REDIRECT) { |
---|
246 | | - ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex, 0, |
---|
247 | | - IPPROTO_GRE, 0); |
---|
| 243 | + ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex, |
---|
| 244 | + IPPROTO_GRE); |
---|
248 | 245 | return; |
---|
249 | 246 | } |
---|
250 | 247 | |
---|
251 | 248 | ipgre_err(skb, info, &tpi); |
---|
| 249 | +} |
---|
| 250 | + |
---|
| 251 | +static bool is_erspan_type1(int gre_hdr_len) |
---|
| 252 | +{ |
---|
| 253 | + /* Both ERSPAN type I (version 0) and type II (version 1) use |
---|
| 254 | + * protocol 0x88BE, but the type I has only 4-byte GRE header, |
---|
| 255 | + * while type II has 8-byte. |
---|
| 256 | + */ |
---|
| 257 | + return gre_hdr_len == 4; |
---|
252 | 258 | } |
---|
253 | 259 | |
---|
254 | 260 | static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi, |
---|
.. | .. |
---|
265 | 271 | int len; |
---|
266 | 272 | |
---|
267 | 273 | itn = net_generic(net, erspan_net_id); |
---|
268 | | - |
---|
269 | 274 | iph = ip_hdr(skb); |
---|
270 | | - ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len); |
---|
271 | | - ver = ershdr->ver; |
---|
272 | | - |
---|
273 | | - tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, |
---|
274 | | - tpi->flags | TUNNEL_KEY, |
---|
275 | | - iph->saddr, iph->daddr, tpi->key); |
---|
| 275 | + if (is_erspan_type1(gre_hdr_len)) { |
---|
| 276 | + ver = 0; |
---|
| 277 | + tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, |
---|
| 278 | + tpi->flags | TUNNEL_NO_KEY, |
---|
| 279 | + iph->saddr, iph->daddr, 0); |
---|
| 280 | + } else { |
---|
| 281 | + ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len); |
---|
| 282 | + ver = ershdr->ver; |
---|
| 283 | + tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, |
---|
| 284 | + tpi->flags | TUNNEL_KEY, |
---|
| 285 | + iph->saddr, iph->daddr, tpi->key); |
---|
| 286 | + } |
---|
276 | 287 | |
---|
277 | 288 | if (tunnel) { |
---|
278 | | - len = gre_hdr_len + erspan_hdr_len(ver); |
---|
| 289 | + if (is_erspan_type1(gre_hdr_len)) |
---|
| 290 | + len = gre_hdr_len; |
---|
| 291 | + else |
---|
| 292 | + len = gre_hdr_len + erspan_hdr_len(ver); |
---|
| 293 | + |
---|
279 | 294 | if (unlikely(!pskb_may_pull(skb, len))) |
---|
280 | 295 | return PACKET_REJECT; |
---|
281 | 296 | |
---|
.. | .. |
---|
343 | 358 | iph->saddr, iph->daddr, tpi->key); |
---|
344 | 359 | |
---|
345 | 360 | if (tunnel) { |
---|
| 361 | + const struct iphdr *tnl_params; |
---|
| 362 | + |
---|
346 | 363 | if (__iptunnel_pull_header(skb, hdr_len, tpi->proto, |
---|
347 | 364 | raw_proto, false) < 0) |
---|
348 | 365 | goto drop; |
---|
.. | .. |
---|
351 | 368 | skb_pop_mac_header(skb); |
---|
352 | 369 | else |
---|
353 | 370 | skb_reset_mac_header(skb); |
---|
354 | | - if (tunnel->collect_md) { |
---|
| 371 | + |
---|
| 372 | + tnl_params = &tunnel->parms.iph; |
---|
| 373 | + if (tunnel->collect_md || tnl_params->daddr == 0) { |
---|
355 | 374 | __be16 flags; |
---|
356 | 375 | __be64 tun_id; |
---|
357 | 376 | |
---|
.. | .. |
---|
435 | 454 | __be16 proto) |
---|
436 | 455 | { |
---|
437 | 456 | struct ip_tunnel *tunnel = netdev_priv(dev); |
---|
438 | | - |
---|
439 | | - if (tunnel->parms.o_flags & TUNNEL_SEQ) |
---|
440 | | - tunnel->o_seqno++; |
---|
| 457 | + __be16 flags = tunnel->parms.o_flags; |
---|
441 | 458 | |
---|
442 | 459 | /* Push GRE header. */ |
---|
443 | 460 | gre_build_header(skb, tunnel->tun_hlen, |
---|
444 | | - tunnel->parms.o_flags, proto, tunnel->parms.o_key, |
---|
445 | | - htonl(tunnel->o_seqno)); |
---|
| 461 | + flags, proto, tunnel->parms.o_key, |
---|
| 462 | + (flags & TUNNEL_SEQ) ? htonl(atomic_fetch_inc(&tunnel->o_seqno)) : 0); |
---|
446 | 463 | |
---|
447 | 464 | ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol); |
---|
448 | 465 | } |
---|
.. | .. |
---|
452 | 469 | return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE); |
---|
453 | 470 | } |
---|
454 | 471 | |
---|
455 | | -static struct rtable *gre_get_rt(struct sk_buff *skb, |
---|
456 | | - struct net_device *dev, |
---|
457 | | - struct flowi4 *fl, |
---|
458 | | - const struct ip_tunnel_key *key) |
---|
459 | | -{ |
---|
460 | | - struct net *net = dev_net(dev); |
---|
461 | | - |
---|
462 | | - memset(fl, 0, sizeof(*fl)); |
---|
463 | | - fl->daddr = key->u.ipv4.dst; |
---|
464 | | - fl->saddr = key->u.ipv4.src; |
---|
465 | | - fl->flowi4_tos = RT_TOS(key->tos); |
---|
466 | | - fl->flowi4_mark = skb->mark; |
---|
467 | | - fl->flowi4_proto = IPPROTO_GRE; |
---|
468 | | - |
---|
469 | | - return ip_route_output_key(net, fl); |
---|
470 | | -} |
---|
471 | | - |
---|
472 | | -static struct rtable *prepare_fb_xmit(struct sk_buff *skb, |
---|
473 | | - struct net_device *dev, |
---|
474 | | - struct flowi4 *fl, |
---|
475 | | - int tunnel_hlen) |
---|
476 | | -{ |
---|
477 | | - struct ip_tunnel_info *tun_info; |
---|
478 | | - const struct ip_tunnel_key *key; |
---|
479 | | - struct rtable *rt = NULL; |
---|
480 | | - int min_headroom; |
---|
481 | | - bool use_cache; |
---|
482 | | - int err; |
---|
483 | | - |
---|
484 | | - tun_info = skb_tunnel_info(skb); |
---|
485 | | - key = &tun_info->key; |
---|
486 | | - use_cache = ip_tunnel_dst_cache_usable(skb, tun_info); |
---|
487 | | - |
---|
488 | | - if (use_cache) |
---|
489 | | - rt = dst_cache_get_ip4(&tun_info->dst_cache, &fl->saddr); |
---|
490 | | - if (!rt) { |
---|
491 | | - rt = gre_get_rt(skb, dev, fl, key); |
---|
492 | | - if (IS_ERR(rt)) |
---|
493 | | - goto err_free_skb; |
---|
494 | | - if (use_cache) |
---|
495 | | - dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst, |
---|
496 | | - fl->saddr); |
---|
497 | | - } |
---|
498 | | - |
---|
499 | | - min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len |
---|
500 | | - + tunnel_hlen + sizeof(struct iphdr); |
---|
501 | | - if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) { |
---|
502 | | - int head_delta = SKB_DATA_ALIGN(min_headroom - |
---|
503 | | - skb_headroom(skb) + |
---|
504 | | - 16); |
---|
505 | | - err = pskb_expand_head(skb, max_t(int, head_delta, 0), |
---|
506 | | - 0, GFP_ATOMIC); |
---|
507 | | - if (unlikely(err)) |
---|
508 | | - goto err_free_rt; |
---|
509 | | - } |
---|
510 | | - return rt; |
---|
511 | | - |
---|
512 | | -err_free_rt: |
---|
513 | | - ip_rt_put(rt); |
---|
514 | | -err_free_skb: |
---|
515 | | - kfree_skb(skb); |
---|
516 | | - dev->stats.tx_dropped++; |
---|
517 | | - return NULL; |
---|
518 | | -} |
---|
519 | | - |
---|
520 | 472 | static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev, |
---|
521 | 473 | __be16 proto) |
---|
522 | 474 | { |
---|
523 | 475 | struct ip_tunnel *tunnel = netdev_priv(dev); |
---|
524 | 476 | struct ip_tunnel_info *tun_info; |
---|
525 | 477 | const struct ip_tunnel_key *key; |
---|
526 | | - struct rtable *rt = NULL; |
---|
527 | | - struct flowi4 fl; |
---|
528 | 478 | int tunnel_hlen; |
---|
529 | | - __be16 df, flags; |
---|
| 479 | + __be16 flags; |
---|
530 | 480 | |
---|
531 | 481 | tun_info = skb_tunnel_info(skb); |
---|
532 | 482 | if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || |
---|
.. | .. |
---|
536 | 486 | key = &tun_info->key; |
---|
537 | 487 | tunnel_hlen = gre_calc_hlen(key->tun_flags); |
---|
538 | 488 | |
---|
539 | | - rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen); |
---|
540 | | - if (!rt) |
---|
541 | | - return; |
---|
| 489 | + if (skb_cow_head(skb, dev->needed_headroom)) |
---|
| 490 | + goto err_free_skb; |
---|
542 | 491 | |
---|
543 | 492 | /* Push Tunnel header. */ |
---|
544 | 493 | if (gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM))) |
---|
545 | | - goto err_free_rt; |
---|
| 494 | + goto err_free_skb; |
---|
546 | 495 | |
---|
547 | 496 | flags = tun_info->key.tun_flags & |
---|
548 | 497 | (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ); |
---|
549 | 498 | gre_build_header(skb, tunnel_hlen, flags, proto, |
---|
550 | 499 | tunnel_id_to_key32(tun_info->key.tun_id), |
---|
551 | | - (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++) : 0); |
---|
| 500 | + (flags & TUNNEL_SEQ) ? htonl(atomic_fetch_inc(&tunnel->o_seqno)) : 0); |
---|
552 | 501 | |
---|
553 | | - df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; |
---|
| 502 | + ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen); |
---|
554 | 503 | |
---|
555 | | - iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE, |
---|
556 | | - key->tos, key->ttl, df, false); |
---|
557 | 504 | return; |
---|
558 | 505 | |
---|
559 | | -err_free_rt: |
---|
560 | | - ip_rt_put(rt); |
---|
561 | 506 | err_free_skb: |
---|
562 | 507 | kfree_skb(skb); |
---|
563 | 508 | dev->stats.tx_dropped++; |
---|
.. | .. |
---|
569 | 514 | struct ip_tunnel_info *tun_info; |
---|
570 | 515 | const struct ip_tunnel_key *key; |
---|
571 | 516 | struct erspan_metadata *md; |
---|
572 | | - struct rtable *rt = NULL; |
---|
573 | 517 | bool truncate = false; |
---|
574 | | - __be16 df, proto; |
---|
575 | | - struct flowi4 fl; |
---|
| 518 | + __be16 proto; |
---|
576 | 519 | int tunnel_hlen; |
---|
577 | 520 | int version; |
---|
578 | 521 | int nhoff; |
---|
579 | | - int thoff; |
---|
580 | 522 | |
---|
581 | 523 | tun_info = skb_tunnel_info(skb); |
---|
582 | 524 | if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || |
---|
.. | .. |
---|
585 | 527 | |
---|
586 | 528 | key = &tun_info->key; |
---|
587 | 529 | if (!(tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT)) |
---|
588 | | - goto err_free_rt; |
---|
| 530 | + goto err_free_skb; |
---|
589 | 531 | if (tun_info->options_len < sizeof(*md)) |
---|
590 | | - goto err_free_rt; |
---|
| 532 | + goto err_free_skb; |
---|
591 | 533 | md = ip_tunnel_info_opts(tun_info); |
---|
592 | 534 | |
---|
593 | 535 | /* ERSPAN has fixed 8 byte GRE header */ |
---|
594 | 536 | version = md->version; |
---|
595 | 537 | tunnel_hlen = 8 + erspan_hdr_len(version); |
---|
596 | 538 | |
---|
597 | | - rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen); |
---|
598 | | - if (!rt) |
---|
599 | | - return; |
---|
| 539 | + if (skb_cow_head(skb, dev->needed_headroom)) |
---|
| 540 | + goto err_free_skb; |
---|
600 | 541 | |
---|
601 | 542 | if (gre_handle_offloads(skb, false)) |
---|
602 | | - goto err_free_rt; |
---|
| 543 | + goto err_free_skb; |
---|
603 | 544 | |
---|
604 | 545 | if (skb->len > dev->mtu + dev->hard_header_len) { |
---|
605 | 546 | pskb_trim(skb, dev->mtu + dev->hard_header_len); |
---|
606 | 547 | truncate = true; |
---|
607 | 548 | } |
---|
608 | 549 | |
---|
609 | | - nhoff = skb_network_header(skb) - skb_mac_header(skb); |
---|
| 550 | + nhoff = skb_network_offset(skb); |
---|
610 | 551 | if (skb->protocol == htons(ETH_P_IP) && |
---|
611 | 552 | (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff)) |
---|
612 | 553 | truncate = true; |
---|
613 | 554 | |
---|
614 | | - thoff = skb_transport_header(skb) - skb_mac_header(skb); |
---|
615 | | - if (skb->protocol == htons(ETH_P_IPV6) && |
---|
616 | | - (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff)) |
---|
617 | | - truncate = true; |
---|
| 555 | + if (skb->protocol == htons(ETH_P_IPV6)) { |
---|
| 556 | + int thoff; |
---|
| 557 | + |
---|
| 558 | + if (skb_transport_header_was_set(skb)) |
---|
| 559 | + thoff = skb_transport_offset(skb); |
---|
| 560 | + else |
---|
| 561 | + thoff = nhoff + sizeof(struct ipv6hdr); |
---|
| 562 | + if (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff) |
---|
| 563 | + truncate = true; |
---|
| 564 | + } |
---|
618 | 565 | |
---|
619 | 566 | if (version == 1) { |
---|
620 | 567 | erspan_build_header(skb, ntohl(tunnel_id_to_key32(key->tun_id)), |
---|
.. | .. |
---|
628 | 575 | truncate, true); |
---|
629 | 576 | proto = htons(ETH_P_ERSPAN2); |
---|
630 | 577 | } else { |
---|
631 | | - goto err_free_rt; |
---|
| 578 | + goto err_free_skb; |
---|
632 | 579 | } |
---|
633 | 580 | |
---|
634 | 581 | gre_build_header(skb, 8, TUNNEL_SEQ, |
---|
635 | | - proto, 0, htonl(tunnel->o_seqno++)); |
---|
| 582 | + proto, 0, htonl(atomic_fetch_inc(&tunnel->o_seqno))); |
---|
636 | 583 | |
---|
637 | | - df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; |
---|
| 584 | + ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen); |
---|
638 | 585 | |
---|
639 | | - iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE, |
---|
640 | | - key->tos, key->ttl, df, false); |
---|
641 | 586 | return; |
---|
642 | 587 | |
---|
643 | | -err_free_rt: |
---|
644 | | - ip_rt_put(rt); |
---|
645 | 588 | err_free_skb: |
---|
646 | 589 | kfree_skb(skb); |
---|
647 | 590 | dev->stats.tx_dropped++; |
---|
.. | .. |
---|
650 | 593 | static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
---|
651 | 594 | { |
---|
652 | 595 | struct ip_tunnel_info *info = skb_tunnel_info(skb); |
---|
| 596 | + const struct ip_tunnel_key *key; |
---|
653 | 597 | struct rtable *rt; |
---|
654 | 598 | struct flowi4 fl4; |
---|
655 | 599 | |
---|
656 | 600 | if (ip_tunnel_info_af(info) != AF_INET) |
---|
657 | 601 | return -EINVAL; |
---|
658 | 602 | |
---|
659 | | - rt = gre_get_rt(skb, dev, &fl4, &info->key); |
---|
| 603 | + key = &info->key; |
---|
| 604 | + ip_tunnel_init_flow(&fl4, IPPROTO_GRE, key->u.ipv4.dst, key->u.ipv4.src, |
---|
| 605 | + tunnel_id_to_key32(key->tun_id), |
---|
| 606 | + key->tos & ~INET_ECN_MASK, 0, skb->mark, |
---|
| 607 | + skb_get_hash(skb)); |
---|
| 608 | + rt = ip_route_output_key(dev_net(dev), &fl4); |
---|
660 | 609 | if (IS_ERR(rt)) |
---|
661 | 610 | return PTR_ERR(rt); |
---|
662 | 611 | |
---|
.. | .. |
---|
680 | 629 | } |
---|
681 | 630 | |
---|
682 | 631 | if (dev->header_ops) { |
---|
683 | | - const int pull_len = tunnel->hlen + sizeof(struct iphdr); |
---|
684 | | - |
---|
685 | 632 | if (skb_cow_head(skb, 0)) |
---|
686 | 633 | goto free_skb; |
---|
687 | 634 | |
---|
688 | 635 | tnl_params = (const struct iphdr *)skb->data; |
---|
689 | 636 | |
---|
690 | | - if (pull_len > skb_transport_offset(skb)) |
---|
691 | | - goto free_skb; |
---|
692 | | - |
---|
693 | 637 | /* Pull skb since ip_tunnel_xmit() needs skb->data pointing |
---|
694 | 638 | * to gre header. |
---|
695 | 639 | */ |
---|
696 | | - skb_pull(skb, pull_len); |
---|
| 640 | + skb_pull(skb, tunnel->hlen + sizeof(struct iphdr)); |
---|
697 | 641 | skb_reset_mac_header(skb); |
---|
| 642 | + |
---|
| 643 | + if (skb->ip_summed == CHECKSUM_PARTIAL && |
---|
| 644 | + skb_checksum_start(skb) < skb->data) |
---|
| 645 | + goto free_skb; |
---|
698 | 646 | } else { |
---|
699 | 647 | if (skb_cow_head(skb, dev->needed_headroom)) |
---|
700 | 648 | goto free_skb; |
---|
.. | .. |
---|
741 | 689 | } |
---|
742 | 690 | |
---|
743 | 691 | /* Push ERSPAN header */ |
---|
744 | | - if (tunnel->erspan_ver == 1) { |
---|
| 692 | + if (tunnel->erspan_ver == 0) { |
---|
| 693 | + proto = htons(ETH_P_ERSPAN); |
---|
| 694 | + tunnel->parms.o_flags &= ~TUNNEL_SEQ; |
---|
| 695 | + } else if (tunnel->erspan_ver == 1) { |
---|
745 | 696 | erspan_build_header(skb, ntohl(tunnel->parms.o_key), |
---|
746 | 697 | tunnel->index, |
---|
747 | 698 | truncate, true); |
---|
.. | .. |
---|
827 | 778 | } |
---|
828 | 779 | } |
---|
829 | 780 | |
---|
830 | | -static int ipgre_tunnel_ioctl(struct net_device *dev, |
---|
831 | | - struct ifreq *ifr, int cmd) |
---|
| 781 | +static int ipgre_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p, |
---|
| 782 | + int cmd) |
---|
832 | 783 | { |
---|
833 | | - struct ip_tunnel_parm p; |
---|
834 | 784 | int err; |
---|
835 | 785 | |
---|
836 | | - if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
---|
837 | | - return -EFAULT; |
---|
838 | | - |
---|
839 | 786 | if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) { |
---|
840 | | - if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE || |
---|
841 | | - p.iph.ihl != 5 || (p.iph.frag_off & htons(~IP_DF)) || |
---|
842 | | - ((p.i_flags | p.o_flags) & (GRE_VERSION | GRE_ROUTING))) |
---|
| 787 | + if (p->iph.version != 4 || p->iph.protocol != IPPROTO_GRE || |
---|
| 788 | + p->iph.ihl != 5 || (p->iph.frag_off & htons(~IP_DF)) || |
---|
| 789 | + ((p->i_flags | p->o_flags) & (GRE_VERSION | GRE_ROUTING))) |
---|
843 | 790 | return -EINVAL; |
---|
844 | 791 | } |
---|
845 | 792 | |
---|
846 | | - p.i_flags = gre_flags_to_tnl_flags(p.i_flags); |
---|
847 | | - p.o_flags = gre_flags_to_tnl_flags(p.o_flags); |
---|
| 793 | + p->i_flags = gre_flags_to_tnl_flags(p->i_flags); |
---|
| 794 | + p->o_flags = gre_flags_to_tnl_flags(p->o_flags); |
---|
848 | 795 | |
---|
849 | | - err = ip_tunnel_ioctl(dev, &p, cmd); |
---|
| 796 | + err = ip_tunnel_ctl(dev, p, cmd); |
---|
850 | 797 | if (err) |
---|
851 | 798 | return err; |
---|
852 | 799 | |
---|
853 | 800 | if (cmd == SIOCCHGTUNNEL) { |
---|
854 | 801 | struct ip_tunnel *t = netdev_priv(dev); |
---|
855 | 802 | |
---|
856 | | - t->parms.i_flags = p.i_flags; |
---|
857 | | - t->parms.o_flags = p.o_flags; |
---|
| 803 | + t->parms.i_flags = p->i_flags; |
---|
| 804 | + t->parms.o_flags = p->o_flags; |
---|
858 | 805 | |
---|
859 | 806 | if (strcmp(dev->rtnl_link_ops->kind, "erspan")) |
---|
860 | 807 | ipgre_link_update(dev, true); |
---|
861 | 808 | } |
---|
862 | 809 | |
---|
863 | | - p.i_flags = gre_tnl_flags_to_gre_flags(p.i_flags); |
---|
864 | | - p.o_flags = gre_tnl_flags_to_gre_flags(p.o_flags); |
---|
865 | | - |
---|
866 | | - if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) |
---|
867 | | - return -EFAULT; |
---|
868 | | - |
---|
| 810 | + p->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags); |
---|
| 811 | + p->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags); |
---|
869 | 812 | return 0; |
---|
870 | 813 | } |
---|
871 | 814 | |
---|
.. | .. |
---|
983 | 926 | .ndo_stop = ipgre_close, |
---|
984 | 927 | #endif |
---|
985 | 928 | .ndo_start_xmit = ipgre_xmit, |
---|
986 | | - .ndo_do_ioctl = ipgre_tunnel_ioctl, |
---|
| 929 | + .ndo_do_ioctl = ip_tunnel_ioctl, |
---|
987 | 930 | .ndo_change_mtu = ip_tunnel_change_mtu, |
---|
988 | 931 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
---|
989 | 932 | .ndo_get_iflink = ip_tunnel_get_iflink, |
---|
| 933 | + .ndo_tunnel_ctl = ipgre_tunnel_ctl, |
---|
990 | 934 | }; |
---|
991 | 935 | |
---|
992 | 936 | #define GRE_FEATURES (NETIF_F_SG | \ |
---|
.. | .. |
---|
1151 | 1095 | if (ret) |
---|
1152 | 1096 | return ret; |
---|
1153 | 1097 | |
---|
1154 | | - /* ERSPAN should only have GRE sequence and key flag */ |
---|
| 1098 | + if (data[IFLA_GRE_ERSPAN_VER] && |
---|
| 1099 | + nla_get_u8(data[IFLA_GRE_ERSPAN_VER]) == 0) |
---|
| 1100 | + return 0; |
---|
| 1101 | + |
---|
| 1102 | + /* ERSPAN type II/III should only have GRE sequence and key flag */ |
---|
1155 | 1103 | if (data[IFLA_GRE_OFLAGS]) |
---|
1156 | 1104 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); |
---|
1157 | 1105 | if (data[IFLA_GRE_IFLAGS]) |
---|
.. | .. |
---|
1259 | 1207 | if (data[IFLA_GRE_ERSPAN_VER]) { |
---|
1260 | 1208 | t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]); |
---|
1261 | 1209 | |
---|
1262 | | - if (t->erspan_ver != 1 && t->erspan_ver != 2) |
---|
| 1210 | + if (t->erspan_ver > 2) |
---|
1263 | 1211 | return -EINVAL; |
---|
1264 | 1212 | } |
---|
1265 | 1213 | |
---|
.. | .. |
---|
1344 | 1292 | { |
---|
1345 | 1293 | struct ip_tunnel *tunnel = netdev_priv(dev); |
---|
1346 | 1294 | |
---|
1347 | | - tunnel->tun_hlen = 8; |
---|
| 1295 | + if (tunnel->erspan_ver == 0) |
---|
| 1296 | + tunnel->tun_hlen = 4; /* 4-byte GRE hdr. */ |
---|
| 1297 | + else |
---|
| 1298 | + tunnel->tun_hlen = 8; /* 8-byte GRE hdr. */ |
---|
| 1299 | + |
---|
1348 | 1300 | tunnel->parms.iph.protocol = IPPROTO_GRE; |
---|
1349 | 1301 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + |
---|
1350 | 1302 | erspan_hdr_len(tunnel->erspan_ver); |
---|
.. | .. |
---|
1378 | 1330 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
---|
1379 | 1331 | ip_tunnel_setup(dev, gre_tap_net_id); |
---|
1380 | 1332 | } |
---|
1381 | | - |
---|
1382 | | -bool is_gretap_dev(const struct net_device *dev) |
---|
1383 | | -{ |
---|
1384 | | - return dev->netdev_ops == &gre_tap_netdev_ops; |
---|
1385 | | -} |
---|
1386 | | -EXPORT_SYMBOL_GPL(is_gretap_dev); |
---|
1387 | 1333 | |
---|
1388 | 1334 | static int |
---|
1389 | 1335 | ipgre_newlink_encap_setup(struct net_device *dev, struct nlattr *data[]) |
---|
.. | .. |
---|
1547 | 1493 | struct ip_tunnel_parm *p = &t->parms; |
---|
1548 | 1494 | __be16 o_flags = p->o_flags; |
---|
1549 | 1495 | |
---|
1550 | | - if (t->erspan_ver == 1 || t->erspan_ver == 2) { |
---|
1551 | | - if (!t->collect_md) |
---|
1552 | | - o_flags |= TUNNEL_KEY; |
---|
1553 | | - |
---|
1554 | | - if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, t->erspan_ver)) |
---|
1555 | | - goto nla_put_failure; |
---|
1556 | | - |
---|
1557 | | - if (t->erspan_ver == 1) { |
---|
1558 | | - if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index)) |
---|
1559 | | - goto nla_put_failure; |
---|
1560 | | - } else { |
---|
1561 | | - if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, t->dir)) |
---|
1562 | | - goto nla_put_failure; |
---|
1563 | | - if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, t->hwid)) |
---|
1564 | | - goto nla_put_failure; |
---|
1565 | | - } |
---|
1566 | | - } |
---|
1567 | | - |
---|
1568 | 1496 | if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || |
---|
1569 | 1497 | nla_put_be16(skb, IFLA_GRE_IFLAGS, |
---|
1570 | 1498 | gre_tnl_flags_to_gre_flags(p->i_flags)) || |
---|
.. | .. |
---|
1605 | 1533 | return -EMSGSIZE; |
---|
1606 | 1534 | } |
---|
1607 | 1535 | |
---|
| 1536 | +static int erspan_fill_info(struct sk_buff *skb, const struct net_device *dev) |
---|
| 1537 | +{ |
---|
| 1538 | + struct ip_tunnel *t = netdev_priv(dev); |
---|
| 1539 | + |
---|
| 1540 | + if (t->erspan_ver <= 2) { |
---|
| 1541 | + if (t->erspan_ver != 0 && !t->collect_md) |
---|
| 1542 | + t->parms.o_flags |= TUNNEL_KEY; |
---|
| 1543 | + |
---|
| 1544 | + if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, t->erspan_ver)) |
---|
| 1545 | + goto nla_put_failure; |
---|
| 1546 | + |
---|
| 1547 | + if (t->erspan_ver == 1) { |
---|
| 1548 | + if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index)) |
---|
| 1549 | + goto nla_put_failure; |
---|
| 1550 | + } else if (t->erspan_ver == 2) { |
---|
| 1551 | + if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, t->dir)) |
---|
| 1552 | + goto nla_put_failure; |
---|
| 1553 | + if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, t->hwid)) |
---|
| 1554 | + goto nla_put_failure; |
---|
| 1555 | + } |
---|
| 1556 | + } |
---|
| 1557 | + |
---|
| 1558 | + return ipgre_fill_info(skb, dev); |
---|
| 1559 | + |
---|
| 1560 | +nla_put_failure: |
---|
| 1561 | + return -EMSGSIZE; |
---|
| 1562 | +} |
---|
| 1563 | + |
---|
1608 | 1564 | static void erspan_setup(struct net_device *dev) |
---|
1609 | 1565 | { |
---|
1610 | 1566 | struct ip_tunnel *t = netdev_priv(dev); |
---|
.. | .. |
---|
1624 | 1580 | [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, |
---|
1625 | 1581 | [IFLA_GRE_IKEY] = { .type = NLA_U32 }, |
---|
1626 | 1582 | [IFLA_GRE_OKEY] = { .type = NLA_U32 }, |
---|
1627 | | - [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, |
---|
1628 | | - [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
---|
| 1583 | + [IFLA_GRE_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) }, |
---|
| 1584 | + [IFLA_GRE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) }, |
---|
1629 | 1585 | [IFLA_GRE_TTL] = { .type = NLA_U8 }, |
---|
1630 | 1586 | [IFLA_GRE_TOS] = { .type = NLA_U8 }, |
---|
1631 | 1587 | [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 }, |
---|
.. | .. |
---|
1683 | 1639 | .changelink = erspan_changelink, |
---|
1684 | 1640 | .dellink = ip_tunnel_dellink, |
---|
1685 | 1641 | .get_size = ipgre_get_size, |
---|
1686 | | - .fill_info = ipgre_fill_info, |
---|
| 1642 | + .fill_info = erspan_fill_info, |
---|
1687 | 1643 | .get_link_net = ip_tunnel_get_link_net, |
---|
1688 | 1644 | }; |
---|
1689 | 1645 | |
---|
.. | .. |
---|
1699 | 1655 | memset(&tb, 0, sizeof(tb)); |
---|
1700 | 1656 | |
---|
1701 | 1657 | dev = rtnl_create_link(net, name, name_assign_type, |
---|
1702 | | - &ipgre_tap_ops, tb); |
---|
| 1658 | + &ipgre_tap_ops, tb, NULL); |
---|
1703 | 1659 | if (IS_ERR(dev)) |
---|
1704 | 1660 | return dev; |
---|
1705 | 1661 | |
---|