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