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