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