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