hc
2024-05-16 8d2a02b24d66aa359e83eebc1ed3c0f85367a1cb
kernel/net/ipv4/ip_gre.c
....@@ -1,13 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Linux NET3: GRE over IP protocol decoder.
34 *
45 * 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
- *
116 */
127
138 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
....@@ -121,8 +116,8 @@
121116 static unsigned int gre_tap_net_id __read_mostly;
122117 static unsigned int erspan_net_id __read_mostly;
123118
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)
126121 {
127122
128123 /* All the routers (except for Linux) return only
....@@ -146,36 +141,6 @@
146141 unsigned int data_len = 0;
147142 struct ip_tunnel *t;
148143
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
-
179144 if (tpi->proto == htons(ETH_P_TEB))
180145 itn = net_generic(net, gre_tap_net_id);
181146 else if (tpi->proto == htons(ETH_P_ERSPAN) ||
....@@ -189,27 +154,59 @@
189154 iph->daddr, iph->saddr, tpi->key);
190155
191156 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
+ }
193188
194189 #if IS_ENABLED(CONFIG_IPV6)
195190 if (tpi->proto == htons(ETH_P_IPV6) &&
196191 !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4 + tpi->hdr_len,
197192 type, data_len))
198
- return;
193
+ return 0;
199194 #endif
200195
201196 if (t->parms.iph.daddr == 0 ||
202197 ipv4_is_multicast(t->parms.iph.daddr))
203
- return;
198
+ return 0;
204199
205200 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
206
- return;
201
+ return 0;
207202
208203 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
209204 t->err_count++;
210205 else
211206 t->err_count = 1;
212207 t->err_time = jiffies;
208
+
209
+ return 0;
213210 }
214211
215212 static void gre_err(struct sk_buff *skb, u32 info)
....@@ -239,16 +236,25 @@
239236
240237 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
241238 ipv4_update_pmtu(skb, dev_net(skb->dev), info,
242
- skb->dev->ifindex, 0, IPPROTO_GRE, 0);
239
+ skb->dev->ifindex, IPPROTO_GRE);
243240 return;
244241 }
245242 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);
248245 return;
249246 }
250247
251248 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;
252258 }
253259
254260 static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
....@@ -265,17 +271,26 @@
265271 int len;
266272
267273 itn = net_generic(net, erspan_net_id);
268
-
269274 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
+ }
276287
277288 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
+
279294 if (unlikely(!pskb_may_pull(skb, len)))
280295 return PACKET_REJECT;
281296
....@@ -343,6 +358,8 @@
343358 iph->saddr, iph->daddr, tpi->key);
344359
345360 if (tunnel) {
361
+ const struct iphdr *tnl_params;
362
+
346363 if (__iptunnel_pull_header(skb, hdr_len, tpi->proto,
347364 raw_proto, false) < 0)
348365 goto drop;
....@@ -351,7 +368,9 @@
351368 skb_pop_mac_header(skb);
352369 else
353370 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) {
355374 __be16 flags;
356375 __be64 tun_id;
357376
....@@ -435,14 +454,12 @@
435454 __be16 proto)
436455 {
437456 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;
441458
442459 /* Push GRE header. */
443460 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);
446463
447464 ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
448465 }
....@@ -452,81 +469,14 @@
452469 return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
453470 }
454471
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
-
520472 static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev,
521473 __be16 proto)
522474 {
523475 struct ip_tunnel *tunnel = netdev_priv(dev);
524476 struct ip_tunnel_info *tun_info;
525477 const struct ip_tunnel_key *key;
526
- struct rtable *rt = NULL;
527
- struct flowi4 fl;
528478 int tunnel_hlen;
529
- __be16 df, flags;
479
+ __be16 flags;
530480
531481 tun_info = skb_tunnel_info(skb);
532482 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
....@@ -536,28 +486,23 @@
536486 key = &tun_info->key;
537487 tunnel_hlen = gre_calc_hlen(key->tun_flags);
538488
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;
542491
543492 /* Push Tunnel header. */
544493 if (gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM)))
545
- goto err_free_rt;
494
+ goto err_free_skb;
546495
547496 flags = tun_info->key.tun_flags &
548497 (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
549498 gre_build_header(skb, tunnel_hlen, flags, proto,
550499 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);
552501
553
- df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
502
+ ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
554503
555
- iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE,
556
- key->tos, key->ttl, df, false);
557504 return;
558505
559
-err_free_rt:
560
- ip_rt_put(rt);
561506 err_free_skb:
562507 kfree_skb(skb);
563508 dev->stats.tx_dropped++;
....@@ -569,14 +514,11 @@
569514 struct ip_tunnel_info *tun_info;
570515 const struct ip_tunnel_key *key;
571516 struct erspan_metadata *md;
572
- struct rtable *rt = NULL;
573517 bool truncate = false;
574
- __be16 df, proto;
575
- struct flowi4 fl;
518
+ __be16 proto;
576519 int tunnel_hlen;
577520 int version;
578521 int nhoff;
579
- int thoff;
580522
581523 tun_info = skb_tunnel_info(skb);
582524 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
....@@ -585,36 +527,41 @@
585527
586528 key = &tun_info->key;
587529 if (!(tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT))
588
- goto err_free_rt;
530
+ goto err_free_skb;
589531 if (tun_info->options_len < sizeof(*md))
590
- goto err_free_rt;
532
+ goto err_free_skb;
591533 md = ip_tunnel_info_opts(tun_info);
592534
593535 /* ERSPAN has fixed 8 byte GRE header */
594536 version = md->version;
595537 tunnel_hlen = 8 + erspan_hdr_len(version);
596538
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;
600541
601542 if (gre_handle_offloads(skb, false))
602
- goto err_free_rt;
543
+ goto err_free_skb;
603544
604545 if (skb->len > dev->mtu + dev->hard_header_len) {
605546 pskb_trim(skb, dev->mtu + dev->hard_header_len);
606547 truncate = true;
607548 }
608549
609
- nhoff = skb_network_header(skb) - skb_mac_header(skb);
550
+ nhoff = skb_network_offset(skb);
610551 if (skb->protocol == htons(ETH_P_IP) &&
611552 (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
612553 truncate = true;
613554
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
+ }
618565
619566 if (version == 1) {
620567 erspan_build_header(skb, ntohl(tunnel_id_to_key32(key->tun_id)),
....@@ -628,20 +575,16 @@
628575 truncate, true);
629576 proto = htons(ETH_P_ERSPAN2);
630577 } else {
631
- goto err_free_rt;
578
+ goto err_free_skb;
632579 }
633580
634581 gre_build_header(skb, 8, TUNNEL_SEQ,
635
- proto, 0, htonl(tunnel->o_seqno++));
582
+ proto, 0, htonl(atomic_fetch_inc(&tunnel->o_seqno)));
636583
637
- df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
584
+ ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
638585
639
- iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE,
640
- key->tos, key->ttl, df, false);
641586 return;
642587
643
-err_free_rt:
644
- ip_rt_put(rt);
645588 err_free_skb:
646589 kfree_skb(skb);
647590 dev->stats.tx_dropped++;
....@@ -650,13 +593,19 @@
650593 static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
651594 {
652595 struct ip_tunnel_info *info = skb_tunnel_info(skb);
596
+ const struct ip_tunnel_key *key;
653597 struct rtable *rt;
654598 struct flowi4 fl4;
655599
656600 if (ip_tunnel_info_af(info) != AF_INET)
657601 return -EINVAL;
658602
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);
660609 if (IS_ERR(rt))
661610 return PTR_ERR(rt);
662611
....@@ -680,21 +629,20 @@
680629 }
681630
682631 if (dev->header_ops) {
683
- const int pull_len = tunnel->hlen + sizeof(struct iphdr);
684
-
685632 if (skb_cow_head(skb, 0))
686633 goto free_skb;
687634
688635 tnl_params = (const struct iphdr *)skb->data;
689636
690
- if (pull_len > skb_transport_offset(skb))
691
- goto free_skb;
692
-
693637 /* Pull skb since ip_tunnel_xmit() needs skb->data pointing
694638 * to gre header.
695639 */
696
- skb_pull(skb, pull_len);
640
+ skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
697641 skb_reset_mac_header(skb);
642
+
643
+ if (skb->ip_summed == CHECKSUM_PARTIAL &&
644
+ skb_checksum_start(skb) < skb->data)
645
+ goto free_skb;
698646 } else {
699647 if (skb_cow_head(skb, dev->needed_headroom))
700648 goto free_skb;
....@@ -741,7 +689,10 @@
741689 }
742690
743691 /* 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) {
745696 erspan_build_header(skb, ntohl(tunnel->parms.o_key),
746697 tunnel->index,
747698 truncate, true);
....@@ -827,45 +778,37 @@
827778 }
828779 }
829780
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)
832783 {
833
- struct ip_tunnel_parm p;
834784 int err;
835785
836
- if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
837
- return -EFAULT;
838
-
839786 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)))
843790 return -EINVAL;
844791 }
845792
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);
848795
849
- err = ip_tunnel_ioctl(dev, &p, cmd);
796
+ err = ip_tunnel_ctl(dev, p, cmd);
850797 if (err)
851798 return err;
852799
853800 if (cmd == SIOCCHGTUNNEL) {
854801 struct ip_tunnel *t = netdev_priv(dev);
855802
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;
858805
859806 if (strcmp(dev->rtnl_link_ops->kind, "erspan"))
860807 ipgre_link_update(dev, true);
861808 }
862809
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);
869812 return 0;
870813 }
871814
....@@ -983,10 +926,11 @@
983926 .ndo_stop = ipgre_close,
984927 #endif
985928 .ndo_start_xmit = ipgre_xmit,
986
- .ndo_do_ioctl = ipgre_tunnel_ioctl,
929
+ .ndo_do_ioctl = ip_tunnel_ioctl,
987930 .ndo_change_mtu = ip_tunnel_change_mtu,
988931 .ndo_get_stats64 = ip_tunnel_get_stats64,
989932 .ndo_get_iflink = ip_tunnel_get_iflink,
933
+ .ndo_tunnel_ctl = ipgre_tunnel_ctl,
990934 };
991935
992936 #define GRE_FEATURES (NETIF_F_SG | \
....@@ -1151,7 +1095,11 @@
11511095 if (ret)
11521096 return ret;
11531097
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 */
11551103 if (data[IFLA_GRE_OFLAGS])
11561104 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
11571105 if (data[IFLA_GRE_IFLAGS])
....@@ -1259,7 +1207,7 @@
12591207 if (data[IFLA_GRE_ERSPAN_VER]) {
12601208 t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
12611209
1262
- if (t->erspan_ver != 1 && t->erspan_ver != 2)
1210
+ if (t->erspan_ver > 2)
12631211 return -EINVAL;
12641212 }
12651213
....@@ -1344,7 +1292,11 @@
13441292 {
13451293 struct ip_tunnel *tunnel = netdev_priv(dev);
13461294
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
+
13481300 tunnel->parms.iph.protocol = IPPROTO_GRE;
13491301 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
13501302 erspan_hdr_len(tunnel->erspan_ver);
....@@ -1378,12 +1330,6 @@
13781330 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
13791331 ip_tunnel_setup(dev, gre_tap_net_id);
13801332 }
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);
13871333
13881334 static int
13891335 ipgre_newlink_encap_setup(struct net_device *dev, struct nlattr *data[])
....@@ -1547,24 +1493,6 @@
15471493 struct ip_tunnel_parm *p = &t->parms;
15481494 __be16 o_flags = p->o_flags;
15491495
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
-
15681496 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
15691497 nla_put_be16(skb, IFLA_GRE_IFLAGS,
15701498 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
....@@ -1605,6 +1533,34 @@
16051533 return -EMSGSIZE;
16061534 }
16071535
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
+
16081564 static void erspan_setup(struct net_device *dev)
16091565 {
16101566 struct ip_tunnel *t = netdev_priv(dev);
....@@ -1624,8 +1580,8 @@
16241580 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
16251581 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
16261582 [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) },
16291585 [IFLA_GRE_TTL] = { .type = NLA_U8 },
16301586 [IFLA_GRE_TOS] = { .type = NLA_U8 },
16311587 [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
....@@ -1683,7 +1639,7 @@
16831639 .changelink = erspan_changelink,
16841640 .dellink = ip_tunnel_dellink,
16851641 .get_size = ipgre_get_size,
1686
- .fill_info = ipgre_fill_info,
1642
+ .fill_info = erspan_fill_info,
16871643 .get_link_net = ip_tunnel_get_link_net,
16881644 };
16891645
....@@ -1699,7 +1655,7 @@
16991655 memset(&tb, 0, sizeof(tb));
17001656
17011657 dev = rtnl_create_link(net, name, name_assign_type,
1702
- &ipgre_tap_ops, tb);
1658
+ &ipgre_tap_ops, tb, NULL);
17031659 if (IS_ERR(dev))
17041660 return dev;
17051661