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