blob: 1482259de9b5d7a5e791f0deb34cedcb1f26bf21 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Simon Horman0d89d202013-05-23 21:02:52 +00002/*
3 * MPLS GSO Support
4 *
5 * Authors: Simon Horman (horms@verge.net.au)
6 *
Simon Horman0d89d202013-05-23 21:02:52 +00007 * 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>
Balazs Nemeth453fff22021-03-09 12:31:01 +010017#include <net/mpls.h>
Simon Horman0d89d202013-05-23 21:02:52 +000018
19static struct sk_buff *mpls_gso_segment(struct sk_buff *skb,
20 netdev_features_t features)
21{
22 struct sk_buff *segs = ERR_PTR(-EINVAL);
David Ahern48d2ab62016-08-24 20:10:44 -070023 u16 mac_offset = skb->mac_header;
Simon Horman0d89d202013-05-23 21:02:52 +000024 netdev_features_t mpls_features;
David Ahern48d2ab62016-08-24 20:10:44 -070025 u16 mac_len = skb->mac_len;
Simon Horman0d89d202013-05-23 21:02:52 +000026 __be16 mpls_protocol;
David Ahern48d2ab62016-08-24 20:10:44 -070027 unsigned int mpls_hlen;
28
29 skb_reset_network_header(skb);
30 mpls_hlen = skb_inner_network_header(skb) - skb_network_header(skb);
Balazs Nemeth453fff22021-03-09 12:31:01 +010031 if (unlikely(!mpls_hlen || mpls_hlen % MPLS_HLEN))
32 goto out;
David Ahern48d2ab62016-08-24 20:10:44 -070033 if (unlikely(!pskb_may_pull(skb, mpls_hlen)))
34 goto out;
Simon Horman0d89d202013-05-23 21:02:52 +000035
Simon Horman0d89d202013-05-23 21:02:52 +000036 /* Setup inner SKB. */
37 mpls_protocol = skb->protocol;
38 skb->protocol = skb->inner_protocol;
39
David Ahern48d2ab62016-08-24 20:10:44 -070040 __skb_pull(skb, mpls_hlen);
41
42 skb->mac_len = 0;
43 skb_reset_mac_header(skb);
Simon Horman0d89d202013-05-23 21:02:52 +000044
45 /* Segment inner packet. */
Florian Westphal1e16aa32014-10-20 13:49:16 +020046 mpls_features = skb->dev->mpls_features & features;
Simon Horman0d89d202013-05-23 21:02:52 +000047 segs = skb_mac_gso_segment(skb, mpls_features);
David Ahern48d2ab62016-08-24 20:10:44 -070048 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;
Simon Horman0d89d202013-05-23 21:02:52 +000054
David Ahern48d2ab62016-08-24 20:10:44 -070055 mpls_hlen += mac_len;
56 do {
57 skb->mac_len = mac_len;
58 skb->protocol = mpls_protocol;
Simon Horman0d89d202013-05-23 21:02:52 +000059
David Ahern48d2ab62016-08-24 20:10:44 -070060 skb_reset_inner_network_header(skb);
Simon Horman0d89d202013-05-23 21:02:52 +000061
David Ahern48d2ab62016-08-24 20:10:44 -070062 __skb_push(skb, mpls_hlen);
Tom Herbert5c7cdf332016-05-18 09:06:09 -070063
David Ahern48d2ab62016-08-24 20:10:44 -070064 skb_reset_mac_header(skb);
65 skb_set_network_header(skb, mac_len);
66 } while ((skb = skb->next));
67
68out:
Simon Horman0d89d202013-05-23 21:02:52 +000069 return segs;
70}
71
Daniel Borkmann207895fd32015-01-29 12:15:03 +010072static struct packet_offload mpls_mc_offload __read_mostly = {
Simon Horman0d89d202013-05-23 21:02:52 +000073 .type = cpu_to_be16(ETH_P_MPLS_MC),
David S. Millerbdef7de2015-06-01 14:56:09 -070074 .priority = 15,
Simon Horman0d89d202013-05-23 21:02:52 +000075 .callbacks = {
Simon Horman0d89d202013-05-23 21:02:52 +000076 .gso_segment = mpls_gso_segment,
77 },
78};
79
Daniel Borkmann207895fd32015-01-29 12:15:03 +010080static struct packet_offload mpls_uc_offload __read_mostly = {
Simon Horman0d89d202013-05-23 21:02:52 +000081 .type = cpu_to_be16(ETH_P_MPLS_UC),
David S. Millerbdef7de2015-06-01 14:56:09 -070082 .priority = 15,
Simon Horman0d89d202013-05-23 21:02:52 +000083 .callbacks = {
Simon Horman0d89d202013-05-23 21:02:52 +000084 .gso_segment = mpls_gso_segment,
85 },
86};
87
88static 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
98static void __exit mpls_gso_exit(void)
99{
100 dev_remove_offload(&mpls_uc_offload);
101 dev_remove_offload(&mpls_mc_offload);
102}
103
104module_init(mpls_gso_init);
105module_exit(mpls_gso_exit);
106
107MODULE_DESCRIPTION("MPLS GSO support");
108MODULE_AUTHOR("Simon Horman (horms@verge.net.au)");
109MODULE_LICENSE("GPL");