12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20d89d203SSimon Horman /*
30d89d203SSimon Horman * MPLS GSO Support
40d89d203SSimon Horman *
50d89d203SSimon Horman * Authors: Simon Horman (horms@verge.net.au)
60d89d203SSimon Horman *
70d89d203SSimon Horman * Based on: GSO portions of net/ipv4/gre.c
80d89d203SSimon Horman */
90d89d203SSimon Horman
100d89d203SSimon Horman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
110d89d203SSimon Horman
120d89d203SSimon Horman #include <linux/err.h>
130d89d203SSimon Horman #include <linux/module.h>
140d89d203SSimon Horman #include <linux/netdev_features.h>
150d89d203SSimon Horman #include <linux/netdevice.h>
160d89d203SSimon Horman #include <linux/skbuff.h>
17d457a0e3SEric Dumazet #include <net/gso.h>
18d348ede3SBalazs Nemeth #include <net/mpls.h>
190d89d203SSimon Horman
mpls_gso_segment(struct sk_buff * skb,netdev_features_t features)200d89d203SSimon Horman static struct sk_buff *mpls_gso_segment(struct sk_buff *skb,
210d89d203SSimon Horman netdev_features_t features)
220d89d203SSimon Horman {
230d89d203SSimon Horman struct sk_buff *segs = ERR_PTR(-EINVAL);
2448d2ab60SDavid Ahern u16 mac_offset = skb->mac_header;
250d89d203SSimon Horman netdev_features_t mpls_features;
2648d2ab60SDavid Ahern u16 mac_len = skb->mac_len;
270d89d203SSimon Horman __be16 mpls_protocol;
2848d2ab60SDavid Ahern unsigned int mpls_hlen;
2948d2ab60SDavid Ahern
30*b456cb98SFlorian Westphal if (!skb_inner_network_header_was_set(skb))
31*b456cb98SFlorian Westphal goto out;
32*b456cb98SFlorian Westphal
3348d2ab60SDavid Ahern skb_reset_network_header(skb);
3448d2ab60SDavid Ahern mpls_hlen = skb_inner_network_header(skb) - skb_network_header(skb);
35d348ede3SBalazs Nemeth if (unlikely(!mpls_hlen || mpls_hlen % MPLS_HLEN))
36d348ede3SBalazs Nemeth goto out;
3748d2ab60SDavid Ahern if (unlikely(!pskb_may_pull(skb, mpls_hlen)))
3848d2ab60SDavid Ahern goto out;
390d89d203SSimon Horman
400d89d203SSimon Horman /* Setup inner SKB. */
410d89d203SSimon Horman mpls_protocol = skb->protocol;
420d89d203SSimon Horman skb->protocol = skb->inner_protocol;
430d89d203SSimon Horman
4448d2ab60SDavid Ahern __skb_pull(skb, mpls_hlen);
4548d2ab60SDavid Ahern
4648d2ab60SDavid Ahern skb->mac_len = 0;
4748d2ab60SDavid Ahern skb_reset_mac_header(skb);
480d89d203SSimon Horman
490d89d203SSimon Horman /* Segment inner packet. */
501e16aa3dSFlorian Westphal mpls_features = skb->dev->mpls_features & features;
510d89d203SSimon Horman segs = skb_mac_gso_segment(skb, mpls_features);
5248d2ab60SDavid Ahern if (IS_ERR_OR_NULL(segs)) {
5348d2ab60SDavid Ahern skb_gso_error_unwind(skb, mpls_protocol, mpls_hlen, mac_offset,
5448d2ab60SDavid Ahern mac_len);
5548d2ab60SDavid Ahern goto out;
5648d2ab60SDavid Ahern }
5748d2ab60SDavid Ahern skb = segs;
580d89d203SSimon Horman
5948d2ab60SDavid Ahern mpls_hlen += mac_len;
6048d2ab60SDavid Ahern do {
6148d2ab60SDavid Ahern skb->mac_len = mac_len;
620d89d203SSimon Horman skb->protocol = mpls_protocol;
630d89d203SSimon Horman
6448d2ab60SDavid Ahern skb_reset_inner_network_header(skb);
655c7cdf33STom Herbert
6648d2ab60SDavid Ahern __skb_push(skb, mpls_hlen);
6748d2ab60SDavid Ahern
6848d2ab60SDavid Ahern skb_reset_mac_header(skb);
6948d2ab60SDavid Ahern skb_set_network_header(skb, mac_len);
7048d2ab60SDavid Ahern } while ((skb = skb->next));
7148d2ab60SDavid Ahern
7248d2ab60SDavid Ahern out:
730d89d203SSimon Horman return segs;
740d89d203SSimon Horman }
750d89d203SSimon Horman
76207895fdSDaniel Borkmann static struct packet_offload mpls_mc_offload __read_mostly = {
770d89d203SSimon Horman .type = cpu_to_be16(ETH_P_MPLS_MC),
78bdef7de4SDavid S. Miller .priority = 15,
790d89d203SSimon Horman .callbacks = {
800d89d203SSimon Horman .gso_segment = mpls_gso_segment,
810d89d203SSimon Horman },
820d89d203SSimon Horman };
830d89d203SSimon Horman
84207895fdSDaniel Borkmann static struct packet_offload mpls_uc_offload __read_mostly = {
850d89d203SSimon Horman .type = cpu_to_be16(ETH_P_MPLS_UC),
86bdef7de4SDavid S. Miller .priority = 15,
870d89d203SSimon Horman .callbacks = {
880d89d203SSimon Horman .gso_segment = mpls_gso_segment,
890d89d203SSimon Horman },
900d89d203SSimon Horman };
910d89d203SSimon Horman
mpls_gso_init(void)920d89d203SSimon Horman static int __init mpls_gso_init(void)
930d89d203SSimon Horman {
940d89d203SSimon Horman pr_info("MPLS GSO support\n");
950d89d203SSimon Horman
960d89d203SSimon Horman dev_add_offload(&mpls_uc_offload);
970d89d203SSimon Horman dev_add_offload(&mpls_mc_offload);
980d89d203SSimon Horman
990d89d203SSimon Horman return 0;
1000d89d203SSimon Horman }
1010d89d203SSimon Horman
mpls_gso_exit(void)1020d89d203SSimon Horman static void __exit mpls_gso_exit(void)
1030d89d203SSimon Horman {
1040d89d203SSimon Horman dev_remove_offload(&mpls_uc_offload);
1050d89d203SSimon Horman dev_remove_offload(&mpls_mc_offload);
1060d89d203SSimon Horman }
1070d89d203SSimon Horman
1080d89d203SSimon Horman module_init(mpls_gso_init);
1090d89d203SSimon Horman module_exit(mpls_gso_exit);
1100d89d203SSimon Horman
1110d89d203SSimon Horman MODULE_DESCRIPTION("MPLS GSO support");
1120d89d203SSimon Horman MODULE_AUTHOR("Simon Horman (horms@verge.net.au)");
1130d89d203SSimon Horman MODULE_LICENSE("GPL");
114