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