^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Network Service Header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2017 Red Hat, Inc. -- Jiri Benc <jbenc@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <net/nsh.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/tun_proto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) int nsh_push(struct sk_buff *skb, const struct nshhdr *pushed_nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct nshhdr *nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) size_t length = nsh_hdr_len(pushed_nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) u8 next_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) if (skb->mac_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) next_proto = TUN_P_ETHERNET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) next_proto = tun_p_from_eth_p(skb->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (!next_proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* Add the NSH header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (skb_cow_head(skb, length) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) skb_push(skb, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) nh = (struct nshhdr *)(skb->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) memcpy(nh, pushed_nh, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) nh->np = next_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) skb_postpush_rcsum(skb, nh, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) skb->protocol = htons(ETH_P_NSH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) skb_reset_mac_len(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) EXPORT_SYMBOL_GPL(nsh_push);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int nsh_pop(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct nshhdr *nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) size_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) __be16 inner_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (!pskb_may_pull(skb, NSH_BASE_HDR_LEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) nh = (struct nshhdr *)(skb->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) length = nsh_hdr_len(nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (length < NSH_BASE_HDR_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) inner_proto = tun_p_to_eth_p(nh->np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!pskb_may_pull(skb, length))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!inner_proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) skb_pull_rcsum(skb, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) skb_reset_mac_len(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) skb->protocol = inner_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) EXPORT_SYMBOL_GPL(nsh_pop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static struct sk_buff *nsh_gso_segment(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) netdev_features_t features)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct sk_buff *segs = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned int nsh_len, mac_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) __be16 proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int nhoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) nhoff = skb->network_header - skb->mac_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) mac_len = skb->mac_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (unlikely(!pskb_may_pull(skb, NSH_BASE_HDR_LEN)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) nsh_len = nsh_hdr_len(nsh_hdr(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (nsh_len < NSH_BASE_HDR_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (unlikely(!pskb_may_pull(skb, nsh_len)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) proto = tun_p_to_eth_p(nsh_hdr(skb)->np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) __skb_pull(skb, nsh_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) skb->mac_len = proto == htons(ETH_P_TEB) ? ETH_HLEN : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) skb->protocol = proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) features &= NETIF_F_SG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) segs = skb_mac_gso_segment(skb, features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (IS_ERR_OR_NULL(segs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) skb_gso_error_unwind(skb, htons(ETH_P_NSH), nsh_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) skb->network_header - nhoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) mac_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) for (skb = segs; skb; skb = skb->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) skb->protocol = htons(ETH_P_NSH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) __skb_push(skb, nsh_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) skb_set_mac_header(skb, -nhoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) skb->network_header = skb->mac_header + mac_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) skb->mac_len = mac_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static struct packet_offload nsh_packet_offload __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .type = htons(ETH_P_NSH),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) .priority = 15,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .callbacks = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .gso_segment = nsh_gso_segment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int __init nsh_init_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) dev_add_offload(&nsh_packet_offload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static void __exit nsh_cleanup_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) dev_remove_offload(&nsh_packet_offload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) module_init(nsh_init_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) module_exit(nsh_cleanup_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) MODULE_AUTHOR("Jiri Benc <jbenc@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) MODULE_DESCRIPTION("NSH protocol");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) MODULE_LICENSE("GPL v2");