1 /* 2 * IPV6 GSO/GRO offload support 3 * Linux INET6 implementation 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License 7 * as published by the Free Software Foundation; either version 8 * 2 of the License, or (at your option) any later version. 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/socket.h> 13 #include <linux/netdevice.h> 14 #include <linux/skbuff.h> 15 #include <linux/printk.h> 16 17 #include <net/protocol.h> 18 #include <net/ipv6.h> 19 20 #include "ip6_offload.h" 21 22 static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto) 23 { 24 const struct net_offload *ops = NULL; 25 26 for (;;) { 27 struct ipv6_opt_hdr *opth; 28 int len; 29 30 if (proto != NEXTHDR_HOP) { 31 ops = rcu_dereference(inet6_offloads[proto]); 32 33 if (unlikely(!ops)) 34 break; 35 36 if (!(ops->flags & INET6_PROTO_GSO_EXTHDR)) 37 break; 38 } 39 40 if (unlikely(!pskb_may_pull(skb, 8))) 41 break; 42 43 opth = (void *)skb->data; 44 len = ipv6_optlen(opth); 45 46 if (unlikely(!pskb_may_pull(skb, len))) 47 break; 48 49 proto = opth->nexthdr; 50 __skb_pull(skb, len); 51 } 52 53 return proto; 54 } 55 56 static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, 57 netdev_features_t features) 58 { 59 struct sk_buff *segs = ERR_PTR(-EINVAL); 60 struct ipv6hdr *ipv6h; 61 const struct net_offload *ops; 62 int proto; 63 struct frag_hdr *fptr; 64 unsigned int unfrag_ip6hlen; 65 u8 *prevhdr; 66 int offset = 0; 67 bool encap, udpfrag; 68 int nhoff; 69 70 if (unlikely(skb_shinfo(skb)->gso_type & 71 ~(SKB_GSO_UDP | 72 SKB_GSO_DODGY | 73 SKB_GSO_TCP_ECN | 74 SKB_GSO_GRE | 75 SKB_GSO_GRE_CSUM | 76 SKB_GSO_IPIP | 77 SKB_GSO_SIT | 78 SKB_GSO_UDP_TUNNEL | 79 SKB_GSO_UDP_TUNNEL_CSUM | 80 SKB_GSO_MPLS | 81 SKB_GSO_TCPV6 | 82 0))) 83 goto out; 84 85 skb_reset_network_header(skb); 86 nhoff = skb_network_header(skb) - skb_mac_header(skb); 87 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h)))) 88 goto out; 89 90 encap = SKB_GSO_CB(skb)->encap_level > 0; 91 if (encap) 92 features = skb->dev->hw_enc_features & netif_skb_features(skb); 93 SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h); 94 95 ipv6h = ipv6_hdr(skb); 96 __skb_pull(skb, sizeof(*ipv6h)); 97 segs = ERR_PTR(-EPROTONOSUPPORT); 98 99 proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr); 100 101 if (skb->encapsulation && 102 skb_shinfo(skb)->gso_type & (SKB_GSO_SIT|SKB_GSO_IPIP)) 103 udpfrag = proto == IPPROTO_UDP && encap; 104 else 105 udpfrag = proto == IPPROTO_UDP && !skb->encapsulation; 106 107 ops = rcu_dereference(inet6_offloads[proto]); 108 if (likely(ops && ops->callbacks.gso_segment)) { 109 skb_reset_transport_header(skb); 110 segs = ops->callbacks.gso_segment(skb, features); 111 } 112 113 if (IS_ERR(segs)) 114 goto out; 115 116 for (skb = segs; skb; skb = skb->next) { 117 ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff); 118 ipv6h->payload_len = htons(skb->len - nhoff - sizeof(*ipv6h)); 119 skb->network_header = (u8 *)ipv6h - skb->head; 120 121 if (udpfrag) { 122 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr); 123 fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen); 124 fptr->frag_off = htons(offset); 125 if (skb->next != NULL) 126 fptr->frag_off |= htons(IP6_MF); 127 offset += (ntohs(ipv6h->payload_len) - 128 sizeof(struct frag_hdr)); 129 } 130 if (encap) 131 skb_reset_inner_headers(skb); 132 } 133 134 out: 135 return segs; 136 } 137 138 /* Return the total length of all the extension hdrs, following the same 139 * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs. 140 */ 141 static int ipv6_exthdrs_len(struct ipv6hdr *iph, 142 const struct net_offload **opps) 143 { 144 struct ipv6_opt_hdr *opth = (void *)iph; 145 int len = 0, proto, optlen = sizeof(*iph); 146 147 proto = iph->nexthdr; 148 for (;;) { 149 if (proto != NEXTHDR_HOP) { 150 *opps = rcu_dereference(inet6_offloads[proto]); 151 if (unlikely(!(*opps))) 152 break; 153 if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR)) 154 break; 155 } 156 opth = (void *)opth + optlen; 157 optlen = ipv6_optlen(opth); 158 len += optlen; 159 proto = opth->nexthdr; 160 } 161 return len; 162 } 163 164 static struct sk_buff **ipv6_gro_receive(struct sk_buff **head, 165 struct sk_buff *skb) 166 { 167 const struct net_offload *ops; 168 struct sk_buff **pp = NULL; 169 struct sk_buff *p; 170 struct ipv6hdr *iph; 171 unsigned int nlen; 172 unsigned int hlen; 173 unsigned int off; 174 u16 flush = 1; 175 int proto; 176 177 off = skb_gro_offset(skb); 178 hlen = off + sizeof(*iph); 179 iph = skb_gro_header_fast(skb, off); 180 if (skb_gro_header_hard(skb, hlen)) { 181 iph = skb_gro_header_slow(skb, hlen, off); 182 if (unlikely(!iph)) 183 goto out; 184 } 185 186 skb_set_network_header(skb, off); 187 skb_gro_pull(skb, sizeof(*iph)); 188 skb_set_transport_header(skb, skb_gro_offset(skb)); 189 190 flush += ntohs(iph->payload_len) != skb_gro_len(skb); 191 192 rcu_read_lock(); 193 proto = iph->nexthdr; 194 ops = rcu_dereference(inet6_offloads[proto]); 195 if (!ops || !ops->callbacks.gro_receive) { 196 __pskb_pull(skb, skb_gro_offset(skb)); 197 proto = ipv6_gso_pull_exthdrs(skb, proto); 198 skb_gro_pull(skb, -skb_transport_offset(skb)); 199 skb_reset_transport_header(skb); 200 __skb_push(skb, skb_gro_offset(skb)); 201 202 ops = rcu_dereference(inet6_offloads[proto]); 203 if (!ops || !ops->callbacks.gro_receive) 204 goto out_unlock; 205 206 iph = ipv6_hdr(skb); 207 } 208 209 NAPI_GRO_CB(skb)->proto = proto; 210 211 flush--; 212 nlen = skb_network_header_len(skb); 213 214 for (p = *head; p; p = p->next) { 215 const struct ipv6hdr *iph2; 216 __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */ 217 218 if (!NAPI_GRO_CB(p)->same_flow) 219 continue; 220 221 iph2 = (struct ipv6hdr *)(p->data + off); 222 first_word = *(__be32 *)iph ^ *(__be32 *)iph2; 223 224 /* All fields must match except length and Traffic Class. 225 * XXX skbs on the gro_list have all been parsed and pulled 226 * already so we don't need to compare nlen 227 * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops))) 228 * memcmp() alone below is suffcient, right? 229 */ 230 if ((first_word & htonl(0xF00FFFFF)) || 231 memcmp(&iph->nexthdr, &iph2->nexthdr, 232 nlen - offsetof(struct ipv6hdr, nexthdr))) { 233 NAPI_GRO_CB(p)->same_flow = 0; 234 continue; 235 } 236 /* flush if Traffic Class fields are different */ 237 NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000)); 238 NAPI_GRO_CB(p)->flush |= flush; 239 240 /* Clear flush_id, there's really no concept of ID in IPv6. */ 241 NAPI_GRO_CB(p)->flush_id = 0; 242 } 243 244 NAPI_GRO_CB(skb)->flush |= flush; 245 246 skb_gro_postpull_rcsum(skb, iph, nlen); 247 248 pp = ops->callbacks.gro_receive(head, skb); 249 250 out_unlock: 251 rcu_read_unlock(); 252 253 out: 254 NAPI_GRO_CB(skb)->flush |= flush; 255 256 return pp; 257 } 258 259 static int ipv6_gro_complete(struct sk_buff *skb, int nhoff) 260 { 261 const struct net_offload *ops; 262 struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff); 263 int err = -ENOSYS; 264 265 iph->payload_len = htons(skb->len - nhoff - sizeof(*iph)); 266 267 rcu_read_lock(); 268 269 nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops); 270 if (WARN_ON(!ops || !ops->callbacks.gro_complete)) 271 goto out_unlock; 272 273 err = ops->callbacks.gro_complete(skb, nhoff); 274 275 out_unlock: 276 rcu_read_unlock(); 277 278 return err; 279 } 280 281 static struct packet_offload ipv6_packet_offload __read_mostly = { 282 .type = cpu_to_be16(ETH_P_IPV6), 283 .callbacks = { 284 .gso_segment = ipv6_gso_segment, 285 .gro_receive = ipv6_gro_receive, 286 .gro_complete = ipv6_gro_complete, 287 }, 288 }; 289 290 static const struct net_offload sit_offload = { 291 .callbacks = { 292 .gso_segment = ipv6_gso_segment, 293 .gro_receive = ipv6_gro_receive, 294 .gro_complete = ipv6_gro_complete, 295 }, 296 }; 297 298 static int __init ipv6_offload_init(void) 299 { 300 301 if (tcpv6_offload_init() < 0) 302 pr_crit("%s: Cannot add TCP protocol offload\n", __func__); 303 if (udp_offload_init() < 0) 304 pr_crit("%s: Cannot add UDP protocol offload\n", __func__); 305 if (ipv6_exthdrs_offload_init() < 0) 306 pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__); 307 308 dev_add_offload(&ipv6_packet_offload); 309 310 inet_add_offload(&sit_offload, IPPROTO_IPV6); 311 312 return 0; 313 } 314 315 fs_initcall(ipv6_offload_init); 316