25
25
#include <linux/udp.h>
26
26
#include <linux/if_arp.h>
27
27
#include <linux/mroute.h>
28
+ #include <linux/if_vlan.h>
28
29
#include <linux/init.h>
29
30
#include <linux/in6.h>
30
31
#include <linux/inetdevice.h>
47
48
#include <net/netns/generic.h>
48
49
#include <net/rtnetlink.h>
49
50
#include <net/gre.h>
51
+ #include <net/dst_metadata.h>
50
52
51
53
#if IS_ENABLED (CONFIG_IPV6 )
52
54
#include <net/ipv6.h>
@@ -200,9 +202,29 @@ static int ipgre_err(struct sk_buff *skb, u32 info,
200
202
return PACKET_RCVD ;
201
203
}
202
204
205
+ static __be64 key_to_tunnel_id (__be32 key )
206
+ {
207
+ #ifdef __BIG_ENDIAN
208
+ return (__force __be64 )((__force u32 )key );
209
+ #else
210
+ return (__force __be64 )((__force u64 )key << 32 );
211
+ #endif
212
+ }
213
+
214
+ /* Returns the least-significant 32 bits of a __be64. */
215
+ static __be32 tunnel_id_to_key (__be64 x )
216
+ {
217
+ #ifdef __BIG_ENDIAN
218
+ return (__force __be32 )x ;
219
+ #else
220
+ return (__force __be32 )((__force u64 )x >> 32 );
221
+ #endif
222
+ }
223
+
203
224
static int ipgre_rcv (struct sk_buff * skb , const struct tnl_ptk_info * tpi )
204
225
{
205
226
struct net * net = dev_net (skb -> dev );
227
+ struct metadata_dst * tun_dst = NULL ;
206
228
struct ip_tunnel_net * itn ;
207
229
const struct iphdr * iph ;
208
230
struct ip_tunnel * tunnel ;
@@ -218,40 +240,162 @@ static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
218
240
219
241
if (tunnel ) {
220
242
skb_pop_mac_header (skb );
221
- ip_tunnel_rcv (tunnel , skb , tpi , log_ecn_error );
243
+ if (tunnel -> collect_md ) {
244
+ struct ip_tunnel_info * info ;
245
+
246
+ tun_dst = metadata_dst_alloc (0 , GFP_ATOMIC );
247
+ if (!tun_dst )
248
+ return PACKET_REJECT ;
249
+
250
+ info = & tun_dst -> u .tun_info ;
251
+ info -> key .ipv4_src = iph -> saddr ;
252
+ info -> key .ipv4_dst = iph -> daddr ;
253
+ info -> key .ipv4_tos = iph -> tos ;
254
+ info -> key .ipv4_ttl = iph -> ttl ;
255
+
256
+ info -> mode = IP_TUNNEL_INFO_RX ;
257
+ info -> key .tun_flags = tpi -> flags &
258
+ (TUNNEL_CSUM | TUNNEL_KEY );
259
+ info -> key .tun_id = key_to_tunnel_id (tpi -> key );
260
+
261
+ info -> key .tp_src = 0 ;
262
+ info -> key .tp_dst = 0 ;
263
+ }
264
+
265
+ ip_tunnel_rcv (tunnel , skb , tpi , tun_dst , log_ecn_error );
222
266
return PACKET_RCVD ;
223
267
}
224
268
return PACKET_REJECT ;
225
269
}
226
270
271
+ static void build_header (struct sk_buff * skb , int hdr_len , __be16 flags ,
272
+ __be16 proto , __be32 key , __be32 seq )
273
+ {
274
+ struct gre_base_hdr * greh ;
275
+
276
+ skb_push (skb , hdr_len );
277
+
278
+ skb_reset_transport_header (skb );
279
+ greh = (struct gre_base_hdr * )skb -> data ;
280
+ greh -> flags = tnl_flags_to_gre_flags (flags );
281
+ greh -> protocol = proto ;
282
+
283
+ if (flags & (TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_SEQ )) {
284
+ __be32 * ptr = (__be32 * )(((u8 * )greh ) + hdr_len - 4 );
285
+
286
+ if (flags & TUNNEL_SEQ ) {
287
+ * ptr = seq ;
288
+ ptr -- ;
289
+ }
290
+ if (flags & TUNNEL_KEY ) {
291
+ * ptr = key ;
292
+ ptr -- ;
293
+ }
294
+ if (flags & TUNNEL_CSUM &&
295
+ !(skb_shinfo (skb )-> gso_type &
296
+ (SKB_GSO_GRE | SKB_GSO_GRE_CSUM ))) {
297
+ * ptr = 0 ;
298
+ * (__sum16 * )ptr = csum_fold (skb_checksum (skb , 0 ,
299
+ skb -> len , 0 ));
300
+ }
301
+ }
302
+ }
303
+
227
304
static void __gre_xmit (struct sk_buff * skb , struct net_device * dev ,
228
305
const struct iphdr * tnl_params ,
229
306
__be16 proto )
230
307
{
231
308
struct ip_tunnel * tunnel = netdev_priv (dev );
232
- struct tnl_ptk_info tpi ;
233
309
234
- tpi .flags = tunnel -> parms .o_flags ;
235
- tpi .proto = proto ;
236
- tpi .key = tunnel -> parms .o_key ;
237
310
if (tunnel -> parms .o_flags & TUNNEL_SEQ )
238
311
tunnel -> o_seqno ++ ;
239
- tpi .seq = htonl (tunnel -> o_seqno );
240
312
241
313
/* Push GRE header. */
242
- gre_build_header (skb , & tpi , tunnel -> tun_hlen );
243
-
244
- skb_set_inner_protocol (skb , tpi .proto );
314
+ build_header (skb , tunnel -> tun_hlen , tunnel -> parms .o_flags ,
315
+ proto , tunnel -> parms .o_key , htonl (tunnel -> o_seqno ));
245
316
317
+ skb_set_inner_protocol (skb , proto );
246
318
ip_tunnel_xmit (skb , dev , tnl_params , tnl_params -> protocol );
247
319
}
248
320
321
+ static void gre_fb_xmit (struct sk_buff * skb , struct net_device * dev )
322
+ {
323
+ struct ip_tunnel_info * tun_info ;
324
+ struct net * net = dev_net (dev );
325
+ const struct ip_tunnel_key * key ;
326
+ struct flowi4 fl ;
327
+ struct rtable * rt ;
328
+ int min_headroom ;
329
+ int tunnel_hlen ;
330
+ __be16 df , flags ;
331
+ int err ;
332
+
333
+ tun_info = skb_tunnel_info (skb , AF_INET );
334
+ if (unlikely (!tun_info || tun_info -> mode != IP_TUNNEL_INFO_TX ))
335
+ goto err_free_skb ;
336
+
337
+ key = & tun_info -> key ;
338
+ memset (& fl , 0 , sizeof (fl ));
339
+ fl .daddr = key -> ipv4_dst ;
340
+ fl .saddr = key -> ipv4_src ;
341
+ fl .flowi4_tos = RT_TOS (key -> ipv4_tos );
342
+ fl .flowi4_mark = skb -> mark ;
343
+ fl .flowi4_proto = IPPROTO_GRE ;
344
+
345
+ rt = ip_route_output_key (net , & fl );
346
+ if (IS_ERR (rt ))
347
+ goto err_free_skb ;
348
+
349
+ tunnel_hlen = ip_gre_calc_hlen (key -> tun_flags );
350
+
351
+ min_headroom = LL_RESERVED_SPACE (rt -> dst .dev ) + rt -> dst .header_len
352
+ + tunnel_hlen + sizeof (struct iphdr );
353
+ if (skb_headroom (skb ) < min_headroom || skb_header_cloned (skb )) {
354
+ int head_delta = SKB_DATA_ALIGN (min_headroom -
355
+ skb_headroom (skb ) +
356
+ 16 );
357
+ err = pskb_expand_head (skb , max_t (int , head_delta , 0 ),
358
+ 0 , GFP_ATOMIC );
359
+ if (unlikely (err ))
360
+ goto err_free_rt ;
361
+ }
362
+
363
+ /* Push Tunnel header. */
364
+ skb = gre_handle_offloads (skb , !!(tun_info -> key .tun_flags & TUNNEL_CSUM ));
365
+ if (IS_ERR (skb )) {
366
+ skb = NULL ;
367
+ goto err_free_rt ;
368
+ }
369
+
370
+ flags = tun_info -> key .tun_flags & (TUNNEL_CSUM | TUNNEL_KEY );
371
+ build_header (skb , tunnel_hlen , flags , htons (ETH_P_TEB ),
372
+ tunnel_id_to_key (tun_info -> key .tun_id ), 0 );
373
+
374
+ df = key -> tun_flags & TUNNEL_DONT_FRAGMENT ? htons (IP_DF ) : 0 ;
375
+ err = iptunnel_xmit (skb -> sk , rt , skb , fl .saddr ,
376
+ key -> ipv4_dst , IPPROTO_GRE ,
377
+ key -> ipv4_tos , key -> ipv4_ttl , df , false);
378
+ iptunnel_xmit_stats (err , & dev -> stats , dev -> tstats );
379
+ return ;
380
+
381
+ err_free_rt :
382
+ ip_rt_put (rt );
383
+ err_free_skb :
384
+ kfree_skb (skb );
385
+ dev -> stats .tx_dropped ++ ;
386
+ }
387
+
249
388
static netdev_tx_t ipgre_xmit (struct sk_buff * skb ,
250
389
struct net_device * dev )
251
390
{
252
391
struct ip_tunnel * tunnel = netdev_priv (dev );
253
392
const struct iphdr * tnl_params ;
254
393
394
+ if (tunnel -> collect_md ) {
395
+ gre_fb_xmit (skb , dev );
396
+ return NETDEV_TX_OK ;
397
+ }
398
+
255
399
if (dev -> header_ops ) {
256
400
/* Need space for new headers */
257
401
if (skb_cow_head (skb , dev -> needed_headroom -
@@ -277,7 +421,6 @@ static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
277
421
goto out ;
278
422
279
423
__gre_xmit (skb , dev , tnl_params , skb -> protocol );
280
-
281
424
return NETDEV_TX_OK ;
282
425
283
426
free_skb :
@@ -292,6 +435,11 @@ static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
292
435
{
293
436
struct ip_tunnel * tunnel = netdev_priv (dev );
294
437
438
+ if (tunnel -> collect_md ) {
439
+ gre_fb_xmit (skb , dev );
440
+ return NETDEV_TX_OK ;
441
+ }
442
+
295
443
skb = gre_handle_offloads (skb , !!(tunnel -> parms .o_flags & TUNNEL_CSUM ));
296
444
if (IS_ERR (skb ))
297
445
goto out ;
@@ -300,7 +448,6 @@ static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
300
448
goto free_skb ;
301
449
302
450
__gre_xmit (skb , dev , & tunnel -> parms .iph , htons (ETH_P_TEB ));
303
-
304
451
return NETDEV_TX_OK ;
305
452
306
453
free_skb :
@@ -596,8 +743,10 @@ static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
596
743
return ipgre_tunnel_validate (tb , data );
597
744
}
598
745
599
- static void ipgre_netlink_parms (struct nlattr * data [], struct nlattr * tb [],
600
- struct ip_tunnel_parm * parms )
746
+ static void ipgre_netlink_parms (struct net_device * dev ,
747
+ struct nlattr * data [],
748
+ struct nlattr * tb [],
749
+ struct ip_tunnel_parm * parms )
601
750
{
602
751
memset (parms , 0 , sizeof (* parms ));
603
752
@@ -635,6 +784,12 @@ static void ipgre_netlink_parms(struct nlattr *data[], struct nlattr *tb[],
635
784
636
785
if (!data [IFLA_GRE_PMTUDISC ] || nla_get_u8 (data [IFLA_GRE_PMTUDISC ]))
637
786
parms -> iph .frag_off = htons (IP_DF );
787
+
788
+ if (data [IFLA_GRE_COLLECT_METADATA ]) {
789
+ struct ip_tunnel * t = netdev_priv (dev );
790
+
791
+ t -> collect_md = true;
792
+ }
638
793
}
639
794
640
795
/* This function returns true when ENCAP attributes are present in the nl msg */
@@ -712,7 +867,7 @@ static int ipgre_newlink(struct net *src_net, struct net_device *dev,
712
867
return err ;
713
868
}
714
869
715
- ipgre_netlink_parms (data , tb , & p );
870
+ ipgre_netlink_parms (dev , data , tb , & p );
716
871
return ip_tunnel_newlink (dev , tb , & p );
717
872
}
718
873
@@ -730,7 +885,7 @@ static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
730
885
return err ;
731
886
}
732
887
733
- ipgre_netlink_parms (data , tb , & p );
888
+ ipgre_netlink_parms (dev , data , tb , & p );
734
889
return ip_tunnel_changelink (dev , tb , & p );
735
890
}
736
891
@@ -765,6 +920,8 @@ static size_t ipgre_get_size(const struct net_device *dev)
765
920
nla_total_size (2 ) +
766
921
/* IFLA_GRE_ENCAP_DPORT */
767
922
nla_total_size (2 ) +
923
+ /* IFLA_GRE_COLLECT_METADATA */
924
+ nla_total_size (0 ) +
768
925
0 ;
769
926
}
770
927
@@ -796,6 +953,11 @@ static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
796
953
t -> encap .flags ))
797
954
goto nla_put_failure ;
798
955
956
+ if (t -> collect_md ) {
957
+ if (nla_put_flag (skb , IFLA_GRE_COLLECT_METADATA ))
958
+ goto nla_put_failure ;
959
+ }
960
+
799
961
return 0 ;
800
962
801
963
nla_put_failure :
@@ -817,6 +979,7 @@ static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
817
979
[IFLA_GRE_ENCAP_FLAGS ] = { .type = NLA_U16 },
818
980
[IFLA_GRE_ENCAP_SPORT ] = { .type = NLA_U16 },
819
981
[IFLA_GRE_ENCAP_DPORT ] = { .type = NLA_U16 },
982
+ [IFLA_GRE_COLLECT_METADATA ] = { .type = NLA_FLAG },
820
983
};
821
984
822
985
static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
@@ -851,7 +1014,7 @@ static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
851
1014
852
1015
static int __net_init ipgre_tap_init_net (struct net * net )
853
1016
{
854
- return ip_tunnel_init_net (net , gre_tap_net_id , & ipgre_tap_ops , NULL );
1017
+ return ip_tunnel_init_net (net , gre_tap_net_id , & ipgre_tap_ops , "gretap0" );
855
1018
}
856
1019
857
1020
static void __net_exit ipgre_tap_exit_net (struct net * net )
0 commit comments