1 // SPDX-License-Identifier: GPL-2.0-only
3 * Network Service Header
5 * Copyright (c) 2017 Red Hat, Inc. -- Jiri Benc <jbenc@redhat.com>
8 #include <linux/module.h>
9 #include <linux/netdevice.h>
10 #include <linux/skbuff.h>
13 #include <net/tun_proto.h>
15 int nsh_push(struct sk_buff *skb, const struct nshhdr *pushed_nh)
18 size_t length = nsh_hdr_len(pushed_nh);
22 next_proto = TUN_P_ETHERNET;
24 next_proto = tun_p_from_eth_p(skb->protocol);
29 /* Add the NSH header */
30 if (skb_cow_head(skb, length) < 0)
33 skb_push(skb, length);
34 nh = (struct nshhdr *)(skb->data);
35 memcpy(nh, pushed_nh, length);
37 skb_postpush_rcsum(skb, nh, length);
39 skb->protocol = htons(ETH_P_NSH);
40 skb_reset_mac_header(skb);
41 skb_reset_network_header(skb);
42 skb_reset_mac_len(skb);
46 EXPORT_SYMBOL_GPL(nsh_push);
48 int nsh_pop(struct sk_buff *skb)
54 if (!pskb_may_pull(skb, NSH_BASE_HDR_LEN))
56 nh = (struct nshhdr *)(skb->data);
57 length = nsh_hdr_len(nh);
58 if (length < NSH_BASE_HDR_LEN)
60 inner_proto = tun_p_to_eth_p(nh->np);
61 if (!pskb_may_pull(skb, length))
67 skb_pull_rcsum(skb, length);
68 skb_reset_mac_header(skb);
69 skb_reset_network_header(skb);
70 skb_reset_mac_len(skb);
71 skb->protocol = inner_proto;
75 EXPORT_SYMBOL_GPL(nsh_pop);
77 static struct sk_buff *nsh_gso_segment(struct sk_buff *skb,
78 netdev_features_t features)
80 struct sk_buff *segs = ERR_PTR(-EINVAL);
81 u16 mac_offset = skb->mac_header;
82 unsigned int nsh_len, mac_len;
85 skb_reset_network_header(skb);
87 mac_len = skb->mac_len;
89 if (unlikely(!pskb_may_pull(skb, NSH_BASE_HDR_LEN)))
91 nsh_len = nsh_hdr_len(nsh_hdr(skb));
92 if (nsh_len < NSH_BASE_HDR_LEN)
94 if (unlikely(!pskb_may_pull(skb, nsh_len)))
97 proto = tun_p_to_eth_p(nsh_hdr(skb)->np);
101 __skb_pull(skb, nsh_len);
103 skb_reset_mac_header(skb);
104 skb->mac_len = proto == htons(ETH_P_TEB) ? ETH_HLEN : 0;
105 skb->protocol = proto;
107 features &= NETIF_F_SG;
108 segs = skb_mac_gso_segment(skb, features);
109 if (IS_ERR_OR_NULL(segs)) {
110 skb_gso_error_unwind(skb, htons(ETH_P_NSH), nsh_len,
111 mac_offset, mac_len);
115 for (skb = segs; skb; skb = skb->next) {
116 skb->protocol = htons(ETH_P_NSH);
117 __skb_push(skb, nsh_len);
118 skb->mac_header = mac_offset;
119 skb->network_header = skb->mac_header + mac_len;
120 skb->mac_len = mac_len;
127 static struct packet_offload nsh_packet_offload __read_mostly = {
128 .type = htons(ETH_P_NSH),
131 .gso_segment = nsh_gso_segment,
135 static int __init nsh_init_module(void)
137 dev_add_offload(&nsh_packet_offload);
141 static void __exit nsh_cleanup_module(void)
143 dev_remove_offload(&nsh_packet_offload);
146 module_init(nsh_init_module);
147 module_exit(nsh_cleanup_module);
149 MODULE_AUTHOR("Jiri Benc <jbenc@redhat.com>");
150 MODULE_DESCRIPTION("NSH protocol");
151 MODULE_LICENSE("GPL v2");