1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	MPLS GSO Support
4  *
5  *	Authors: Simon Horman (horms@verge.net.au)
6  *
7  *	Based on: GSO portions of net/ipv4/gre.c
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/err.h>
13 #include <linux/module.h>
14 #include <linux/netdev_features.h>
15 #include <linux/netdevice.h>
16 #include <linux/skbuff.h>
17 #include <net/gso.h>
18 #include <net/mpls.h>
19 
mpls_gso_segment(struct sk_buff * skb,netdev_features_t features)20 static struct sk_buff *mpls_gso_segment(struct sk_buff *skb,
21 				       netdev_features_t features)
22 {
23 	struct sk_buff *segs = ERR_PTR(-EINVAL);
24 	u16 mac_offset = skb->mac_header;
25 	netdev_features_t mpls_features;
26 	u16 mac_len = skb->mac_len;
27 	__be16 mpls_protocol;
28 	unsigned int mpls_hlen;
29 
30 	skb_reset_network_header(skb);
31 	mpls_hlen = skb_inner_network_header(skb) - skb_network_header(skb);
32 	if (unlikely(!mpls_hlen || mpls_hlen % MPLS_HLEN))
33 		goto out;
34 	if (unlikely(!pskb_may_pull(skb, mpls_hlen)))
35 		goto out;
36 
37 	/* Setup inner SKB. */
38 	mpls_protocol = skb->protocol;
39 	skb->protocol = skb->inner_protocol;
40 
41 	__skb_pull(skb, mpls_hlen);
42 
43 	skb->mac_len = 0;
44 	skb_reset_mac_header(skb);
45 
46 	/* Segment inner packet. */
47 	mpls_features = skb->dev->mpls_features & features;
48 	segs = skb_mac_gso_segment(skb, mpls_features);
49 	if (IS_ERR_OR_NULL(segs)) {
50 		skb_gso_error_unwind(skb, mpls_protocol, mpls_hlen, mac_offset,
51 				     mac_len);
52 		goto out;
53 	}
54 	skb = segs;
55 
56 	mpls_hlen += mac_len;
57 	do {
58 		skb->mac_len = mac_len;
59 		skb->protocol = mpls_protocol;
60 
61 		skb_reset_inner_network_header(skb);
62 
63 		__skb_push(skb, mpls_hlen);
64 
65 		skb_reset_mac_header(skb);
66 		skb_set_network_header(skb, mac_len);
67 	} while ((skb = skb->next));
68 
69 out:
70 	return segs;
71 }
72 
73 static struct packet_offload mpls_mc_offload __read_mostly = {
74 	.type = cpu_to_be16(ETH_P_MPLS_MC),
75 	.priority = 15,
76 	.callbacks = {
77 		.gso_segment    =	mpls_gso_segment,
78 	},
79 };
80 
81 static struct packet_offload mpls_uc_offload __read_mostly = {
82 	.type = cpu_to_be16(ETH_P_MPLS_UC),
83 	.priority = 15,
84 	.callbacks = {
85 		.gso_segment    =	mpls_gso_segment,
86 	},
87 };
88 
mpls_gso_init(void)89 static int __init mpls_gso_init(void)
90 {
91 	pr_info("MPLS GSO support\n");
92 
93 	dev_add_offload(&mpls_uc_offload);
94 	dev_add_offload(&mpls_mc_offload);
95 
96 	return 0;
97 }
98 
mpls_gso_exit(void)99 static void __exit mpls_gso_exit(void)
100 {
101 	dev_remove_offload(&mpls_uc_offload);
102 	dev_remove_offload(&mpls_mc_offload);
103 }
104 
105 module_init(mpls_gso_init);
106 module_exit(mpls_gso_exit);
107 
108 MODULE_DESCRIPTION("MPLS GSO support");
109 MODULE_AUTHOR("Simon Horman (horms@verge.net.au)");
110 MODULE_LICENSE("GPL");
111