xref: /openbmc/linux/net/nsh/nsh.c (revision c411ed854584a71b0e86ac3019b60e4789d88086)
1*c411ed85SJiri Benc /*
2*c411ed85SJiri Benc  * Network Service Header
3*c411ed85SJiri Benc  *
4*c411ed85SJiri Benc  * Copyright (c) 2017 Red Hat, Inc. -- Jiri Benc <jbenc@redhat.com>
5*c411ed85SJiri Benc  *
6*c411ed85SJiri Benc  * This program is free software; you can redistribute it and/or modify
7*c411ed85SJiri Benc  * it under the terms of the GNU General Public License version 2 as
8*c411ed85SJiri Benc  * published by the Free Software Foundation.
9*c411ed85SJiri Benc  */
10*c411ed85SJiri Benc 
11*c411ed85SJiri Benc #include <linux/module.h>
12*c411ed85SJiri Benc #include <linux/netdevice.h>
13*c411ed85SJiri Benc #include <linux/skbuff.h>
14*c411ed85SJiri Benc #include <net/nsh.h>
15*c411ed85SJiri Benc #include <net/tun_proto.h>
16*c411ed85SJiri Benc 
17*c411ed85SJiri Benc static struct sk_buff *nsh_gso_segment(struct sk_buff *skb,
18*c411ed85SJiri Benc 				       netdev_features_t features)
19*c411ed85SJiri Benc {
20*c411ed85SJiri Benc 	struct sk_buff *segs = ERR_PTR(-EINVAL);
21*c411ed85SJiri Benc 	unsigned int nsh_len, mac_len;
22*c411ed85SJiri Benc 	__be16 proto;
23*c411ed85SJiri Benc 	int nhoff;
24*c411ed85SJiri Benc 
25*c411ed85SJiri Benc 	skb_reset_network_header(skb);
26*c411ed85SJiri Benc 
27*c411ed85SJiri Benc 	nhoff = skb->network_header - skb->mac_header;
28*c411ed85SJiri Benc 	mac_len = skb->mac_len;
29*c411ed85SJiri Benc 
30*c411ed85SJiri Benc 	if (unlikely(!pskb_may_pull(skb, NSH_BASE_HDR_LEN)))
31*c411ed85SJiri Benc 		goto out;
32*c411ed85SJiri Benc 	nsh_len = nsh_hdr_len(nsh_hdr(skb));
33*c411ed85SJiri Benc 	if (unlikely(!pskb_may_pull(skb, nsh_len)))
34*c411ed85SJiri Benc 		goto out;
35*c411ed85SJiri Benc 
36*c411ed85SJiri Benc 	proto = tun_p_to_eth_p(nsh_hdr(skb)->np);
37*c411ed85SJiri Benc 	if (!proto)
38*c411ed85SJiri Benc 		goto out;
39*c411ed85SJiri Benc 
40*c411ed85SJiri Benc 	__skb_pull(skb, nsh_len);
41*c411ed85SJiri Benc 
42*c411ed85SJiri Benc 	skb_reset_mac_header(skb);
43*c411ed85SJiri Benc 	skb_reset_mac_len(skb);
44*c411ed85SJiri Benc 	skb->protocol = proto;
45*c411ed85SJiri Benc 
46*c411ed85SJiri Benc 	features &= NETIF_F_SG;
47*c411ed85SJiri Benc 	segs = skb_mac_gso_segment(skb, features);
48*c411ed85SJiri Benc 	if (IS_ERR_OR_NULL(segs)) {
49*c411ed85SJiri Benc 		skb_gso_error_unwind(skb, htons(ETH_P_NSH), nsh_len,
50*c411ed85SJiri Benc 				     skb->network_header - nhoff,
51*c411ed85SJiri Benc 				     mac_len);
52*c411ed85SJiri Benc 		goto out;
53*c411ed85SJiri Benc 	}
54*c411ed85SJiri Benc 
55*c411ed85SJiri Benc 	for (skb = segs; skb; skb = skb->next) {
56*c411ed85SJiri Benc 		skb->protocol = htons(ETH_P_NSH);
57*c411ed85SJiri Benc 		__skb_push(skb, nsh_len);
58*c411ed85SJiri Benc 		skb_set_mac_header(skb, -nhoff);
59*c411ed85SJiri Benc 		skb->network_header = skb->mac_header + mac_len;
60*c411ed85SJiri Benc 		skb->mac_len = mac_len;
61*c411ed85SJiri Benc 	}
62*c411ed85SJiri Benc 
63*c411ed85SJiri Benc out:
64*c411ed85SJiri Benc 	return segs;
65*c411ed85SJiri Benc }
66*c411ed85SJiri Benc 
67*c411ed85SJiri Benc static struct packet_offload nsh_packet_offload __read_mostly = {
68*c411ed85SJiri Benc 	.type = htons(ETH_P_NSH),
69*c411ed85SJiri Benc 	.priority = 15,
70*c411ed85SJiri Benc 	.callbacks = {
71*c411ed85SJiri Benc 		.gso_segment = nsh_gso_segment,
72*c411ed85SJiri Benc 	},
73*c411ed85SJiri Benc };
74*c411ed85SJiri Benc 
75*c411ed85SJiri Benc static int __init nsh_init_module(void)
76*c411ed85SJiri Benc {
77*c411ed85SJiri Benc 	dev_add_offload(&nsh_packet_offload);
78*c411ed85SJiri Benc 	return 0;
79*c411ed85SJiri Benc }
80*c411ed85SJiri Benc 
81*c411ed85SJiri Benc static void __exit nsh_cleanup_module(void)
82*c411ed85SJiri Benc {
83*c411ed85SJiri Benc 	dev_remove_offload(&nsh_packet_offload);
84*c411ed85SJiri Benc }
85*c411ed85SJiri Benc 
86*c411ed85SJiri Benc module_init(nsh_init_module);
87*c411ed85SJiri Benc module_exit(nsh_cleanup_module);
88*c411ed85SJiri Benc 
89*c411ed85SJiri Benc MODULE_AUTHOR("Jiri Benc <jbenc@redhat.com>");
90*c411ed85SJiri Benc MODULE_DESCRIPTION("NSH protocol");
91*c411ed85SJiri Benc MODULE_LICENSE("GPL v2");
92