Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 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> |
Vlad Yasevich | c6b641a | 2012-11-15 08:49:22 +0000 | [diff] [blame] | 15 | #include <linux/printk.h> |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 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 int ipv6_gso_send_check(struct sk_buff *skb) |
| 57 | { |
| 58 | const struct ipv6hdr *ipv6h; |
| 59 | const struct net_offload *ops; |
| 60 | int err = -EINVAL; |
| 61 | |
| 62 | if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h)))) |
| 63 | goto out; |
| 64 | |
| 65 | ipv6h = ipv6_hdr(skb); |
| 66 | __skb_pull(skb, sizeof(*ipv6h)); |
| 67 | err = -EPROTONOSUPPORT; |
| 68 | |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 69 | ops = rcu_dereference(inet6_offloads[ |
| 70 | ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]); |
| 71 | |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 72 | if (likely(ops && ops->callbacks.gso_send_check)) { |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 73 | skb_reset_transport_header(skb); |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 74 | err = ops->callbacks.gso_send_check(skb); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 75 | } |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 76 | |
| 77 | out: |
| 78 | return err; |
| 79 | } |
| 80 | |
| 81 | static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, |
| 82 | netdev_features_t features) |
| 83 | { |
| 84 | struct sk_buff *segs = ERR_PTR(-EINVAL); |
| 85 | struct ipv6hdr *ipv6h; |
| 86 | const struct net_offload *ops; |
| 87 | int proto; |
| 88 | struct frag_hdr *fptr; |
| 89 | unsigned int unfrag_ip6hlen; |
| 90 | u8 *prevhdr; |
| 91 | int offset = 0; |
Hannes Frederic Sowa | 91a48a2 | 2014-02-24 00:48:05 +0100 | [diff] [blame] | 92 | bool encap, udpfrag; |
Eric Dumazet | d3e5e00 | 2013-10-20 20:47:29 -0700 | [diff] [blame] | 93 | int nhoff; |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 94 | |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 95 | if (unlikely(skb_shinfo(skb)->gso_type & |
| 96 | ~(SKB_GSO_UDP | |
| 97 | SKB_GSO_DODGY | |
| 98 | SKB_GSO_TCP_ECN | |
Pravin B Shelar | 68c3316 | 2013-02-14 14:02:41 +0000 | [diff] [blame] | 99 | SKB_GSO_GRE | |
Tom Herbert | 4749c09 | 2014-06-04 17:20:23 -0700 | [diff] [blame] | 100 | SKB_GSO_GRE_CSUM | |
Eric Dumazet | cb32f51 | 2013-10-19 11:42:57 -0700 | [diff] [blame] | 101 | SKB_GSO_IPIP | |
Eric Dumazet | 61c1db7 | 2013-10-20 20:47:30 -0700 | [diff] [blame] | 102 | SKB_GSO_SIT | |
Pravin B Shelar | 7313626 | 2013-03-07 13:21:51 +0000 | [diff] [blame] | 103 | SKB_GSO_UDP_TUNNEL | |
Tom Herbert | 0f4f4ff | 2014-06-04 17:20:16 -0700 | [diff] [blame] | 104 | SKB_GSO_UDP_TUNNEL_CSUM | |
Simon Horman | 0d89d20 | 2013-05-23 21:02:52 +0000 | [diff] [blame] | 105 | SKB_GSO_MPLS | |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 106 | SKB_GSO_TCPV6 | |
| 107 | 0))) |
| 108 | goto out; |
| 109 | |
Eric Dumazet | d3e5e00 | 2013-10-20 20:47:29 -0700 | [diff] [blame] | 110 | skb_reset_network_header(skb); |
| 111 | nhoff = skb_network_header(skb) - skb_mac_header(skb); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 112 | if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h)))) |
| 113 | goto out; |
| 114 | |
Hannes Frederic Sowa | 91a48a2 | 2014-02-24 00:48:05 +0100 | [diff] [blame] | 115 | encap = SKB_GSO_CB(skb)->encap_level > 0; |
| 116 | if (encap) |
Eric Dumazet | d3e5e00 | 2013-10-20 20:47:29 -0700 | [diff] [blame] | 117 | features = skb->dev->hw_enc_features & netif_skb_features(skb); |
| 118 | SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h); |
| 119 | |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 120 | ipv6h = ipv6_hdr(skb); |
| 121 | __skb_pull(skb, sizeof(*ipv6h)); |
| 122 | segs = ERR_PTR(-EPROTONOSUPPORT); |
| 123 | |
| 124 | proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr); |
Eric Dumazet | b917eb1 | 2013-10-18 14:43:55 -0700 | [diff] [blame] | 125 | |
Hannes Frederic Sowa | 91a48a2 | 2014-02-24 00:48:05 +0100 | [diff] [blame] | 126 | if (skb->encapsulation && |
| 127 | skb_shinfo(skb)->gso_type & (SKB_GSO_SIT|SKB_GSO_IPIP)) |
| 128 | udpfrag = proto == IPPROTO_UDP && encap; |
| 129 | else |
| 130 | udpfrag = proto == IPPROTO_UDP && !skb->encapsulation; |
| 131 | |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 132 | ops = rcu_dereference(inet6_offloads[proto]); |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 133 | if (likely(ops && ops->callbacks.gso_segment)) { |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 134 | skb_reset_transport_header(skb); |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 135 | segs = ops->callbacks.gso_segment(skb, features); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 136 | } |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 137 | |
| 138 | if (IS_ERR(segs)) |
| 139 | goto out; |
| 140 | |
| 141 | for (skb = segs; skb; skb = skb->next) { |
Eric Dumazet | d3e5e00 | 2013-10-20 20:47:29 -0700 | [diff] [blame] | 142 | ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff); |
| 143 | ipv6h->payload_len = htons(skb->len - nhoff - sizeof(*ipv6h)); |
Eric Dumazet | d3e5e00 | 2013-10-20 20:47:29 -0700 | [diff] [blame] | 144 | skb->network_header = (u8 *)ipv6h - skb->head; |
| 145 | |
Hannes Frederic Sowa | 91a48a2 | 2014-02-24 00:48:05 +0100 | [diff] [blame] | 146 | if (udpfrag) { |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 147 | unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr); |
Eric Dumazet | d3e5e00 | 2013-10-20 20:47:29 -0700 | [diff] [blame] | 148 | fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 149 | fptr->frag_off = htons(offset); |
| 150 | if (skb->next != NULL) |
| 151 | fptr->frag_off |= htons(IP6_MF); |
| 152 | offset += (ntohs(ipv6h->payload_len) - |
| 153 | sizeof(struct frag_hdr)); |
| 154 | } |
Hannes Frederic Sowa | 91a48a2 | 2014-02-24 00:48:05 +0100 | [diff] [blame] | 155 | if (encap) |
| 156 | skb_reset_inner_headers(skb); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | out: |
| 160 | return segs; |
| 161 | } |
| 162 | |
Jerry Chu | 299603e8 | 2013-12-11 20:53:45 -0800 | [diff] [blame] | 163 | /* Return the total length of all the extension hdrs, following the same |
| 164 | * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs. |
| 165 | */ |
| 166 | static int ipv6_exthdrs_len(struct ipv6hdr *iph, |
| 167 | const struct net_offload **opps) |
| 168 | { |
Jerry Chu | 810c23a | 2013-12-15 18:48:07 -0800 | [diff] [blame] | 169 | struct ipv6_opt_hdr *opth = (void *)iph; |
| 170 | int len = 0, proto, optlen = sizeof(*iph); |
Jerry Chu | 299603e8 | 2013-12-11 20:53:45 -0800 | [diff] [blame] | 171 | |
| 172 | proto = iph->nexthdr; |
| 173 | for (;;) { |
| 174 | if (proto != NEXTHDR_HOP) { |
| 175 | *opps = rcu_dereference(inet6_offloads[proto]); |
| 176 | if (unlikely(!(*opps))) |
| 177 | break; |
| 178 | if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR)) |
| 179 | break; |
| 180 | } |
Jerry Chu | 810c23a | 2013-12-15 18:48:07 -0800 | [diff] [blame] | 181 | opth = (void *)opth + optlen; |
| 182 | optlen = ipv6_optlen(opth); |
Jerry Chu | 299603e8 | 2013-12-11 20:53:45 -0800 | [diff] [blame] | 183 | len += optlen; |
| 184 | proto = opth->nexthdr; |
| 185 | } |
| 186 | return len; |
| 187 | } |
| 188 | |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 189 | static struct sk_buff **ipv6_gro_receive(struct sk_buff **head, |
| 190 | struct sk_buff *skb) |
| 191 | { |
| 192 | const struct net_offload *ops; |
| 193 | struct sk_buff **pp = NULL; |
| 194 | struct sk_buff *p; |
| 195 | struct ipv6hdr *iph; |
| 196 | unsigned int nlen; |
| 197 | unsigned int hlen; |
| 198 | unsigned int off; |
Jerry Chu | bf5a755 | 2014-01-07 10:23:19 -0800 | [diff] [blame] | 199 | u16 flush = 1; |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 200 | int proto; |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 201 | |
| 202 | off = skb_gro_offset(skb); |
| 203 | hlen = off + sizeof(*iph); |
| 204 | iph = skb_gro_header_fast(skb, off); |
| 205 | if (skb_gro_header_hard(skb, hlen)) { |
| 206 | iph = skb_gro_header_slow(skb, hlen, off); |
| 207 | if (unlikely(!iph)) |
| 208 | goto out; |
| 209 | } |
| 210 | |
Jerry Chu | 299603e8 | 2013-12-11 20:53:45 -0800 | [diff] [blame] | 211 | skb_set_network_header(skb, off); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 212 | skb_gro_pull(skb, sizeof(*iph)); |
| 213 | skb_set_transport_header(skb, skb_gro_offset(skb)); |
| 214 | |
| 215 | flush += ntohs(iph->payload_len) != skb_gro_len(skb); |
| 216 | |
| 217 | rcu_read_lock(); |
| 218 | proto = iph->nexthdr; |
| 219 | ops = rcu_dereference(inet6_offloads[proto]); |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 220 | if (!ops || !ops->callbacks.gro_receive) { |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 221 | __pskb_pull(skb, skb_gro_offset(skb)); |
| 222 | proto = ipv6_gso_pull_exthdrs(skb, proto); |
| 223 | skb_gro_pull(skb, -skb_transport_offset(skb)); |
| 224 | skb_reset_transport_header(skb); |
| 225 | __skb_push(skb, skb_gro_offset(skb)); |
| 226 | |
| 227 | ops = rcu_dereference(inet6_offloads[proto]); |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 228 | if (!ops || !ops->callbacks.gro_receive) |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 229 | goto out_unlock; |
| 230 | |
| 231 | iph = ipv6_hdr(skb); |
| 232 | } |
| 233 | |
| 234 | NAPI_GRO_CB(skb)->proto = proto; |
| 235 | |
| 236 | flush--; |
| 237 | nlen = skb_network_header_len(skb); |
| 238 | |
| 239 | for (p = *head; p; p = p->next) { |
| 240 | const struct ipv6hdr *iph2; |
| 241 | __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */ |
| 242 | |
| 243 | if (!NAPI_GRO_CB(p)->same_flow) |
| 244 | continue; |
| 245 | |
Jerry Chu | 299603e8 | 2013-12-11 20:53:45 -0800 | [diff] [blame] | 246 | iph2 = (struct ipv6hdr *)(p->data + off); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 247 | first_word = *(__be32 *)iph ^ *(__be32 *)iph2 ; |
| 248 | |
Jerry Chu | 299603e8 | 2013-12-11 20:53:45 -0800 | [diff] [blame] | 249 | /* All fields must match except length and Traffic Class. |
| 250 | * XXX skbs on the gro_list have all been parsed and pulled |
| 251 | * already so we don't need to compare nlen |
| 252 | * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops))) |
| 253 | * memcmp() alone below is suffcient, right? |
| 254 | */ |
| 255 | if ((first_word & htonl(0xF00FFFFF)) || |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 256 | memcmp(&iph->nexthdr, &iph2->nexthdr, |
| 257 | nlen - offsetof(struct ipv6hdr, nexthdr))) { |
| 258 | NAPI_GRO_CB(p)->same_flow = 0; |
| 259 | continue; |
| 260 | } |
| 261 | /* flush if Traffic Class fields are different */ |
| 262 | NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000)); |
| 263 | NAPI_GRO_CB(p)->flush |= flush; |
| 264 | } |
| 265 | |
| 266 | NAPI_GRO_CB(skb)->flush |= flush; |
| 267 | |
Eric Dumazet | 4de462a | 2014-05-19 21:56:34 -0700 | [diff] [blame] | 268 | skb_gro_postpull_rcsum(skb, iph, nlen); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 269 | |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 270 | pp = ops->callbacks.gro_receive(head, skb); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 271 | |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 272 | out_unlock: |
| 273 | rcu_read_unlock(); |
| 274 | |
| 275 | out: |
| 276 | NAPI_GRO_CB(skb)->flush |= flush; |
| 277 | |
| 278 | return pp; |
| 279 | } |
| 280 | |
Jerry Chu | 299603e8 | 2013-12-11 20:53:45 -0800 | [diff] [blame] | 281 | static int ipv6_gro_complete(struct sk_buff *skb, int nhoff) |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 282 | { |
| 283 | const struct net_offload *ops; |
Jerry Chu | 299603e8 | 2013-12-11 20:53:45 -0800 | [diff] [blame] | 284 | struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 285 | int err = -ENOSYS; |
| 286 | |
Jerry Chu | 299603e8 | 2013-12-11 20:53:45 -0800 | [diff] [blame] | 287 | iph->payload_len = htons(skb->len - nhoff - sizeof(*iph)); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 288 | |
| 289 | rcu_read_lock(); |
Jerry Chu | 299603e8 | 2013-12-11 20:53:45 -0800 | [diff] [blame] | 290 | |
| 291 | nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops); |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 292 | if (WARN_ON(!ops || !ops->callbacks.gro_complete)) |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 293 | goto out_unlock; |
| 294 | |
Jerry Chu | 299603e8 | 2013-12-11 20:53:45 -0800 | [diff] [blame] | 295 | err = ops->callbacks.gro_complete(skb, nhoff); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 296 | |
| 297 | out_unlock: |
| 298 | rcu_read_unlock(); |
| 299 | |
| 300 | return err; |
| 301 | } |
| 302 | |
| 303 | static struct packet_offload ipv6_packet_offload __read_mostly = { |
| 304 | .type = cpu_to_be16(ETH_P_IPV6), |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 305 | .callbacks = { |
| 306 | .gso_send_check = ipv6_gso_send_check, |
| 307 | .gso_segment = ipv6_gso_segment, |
| 308 | .gro_receive = ipv6_gro_receive, |
| 309 | .gro_complete = ipv6_gro_complete, |
| 310 | }, |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 311 | }; |
| 312 | |
Eric Dumazet | 61c1db7 | 2013-10-20 20:47:30 -0700 | [diff] [blame] | 313 | static const struct net_offload sit_offload = { |
| 314 | .callbacks = { |
| 315 | .gso_send_check = ipv6_gso_send_check, |
| 316 | .gso_segment = ipv6_gso_segment, |
| 317 | }, |
| 318 | }; |
| 319 | |
Vlad Yasevich | c6b641a | 2012-11-15 08:49:22 +0000 | [diff] [blame] | 320 | static int __init ipv6_offload_init(void) |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 321 | { |
Vlad Yasevich | c6b641a | 2012-11-15 08:49:22 +0000 | [diff] [blame] | 322 | |
| 323 | if (tcpv6_offload_init() < 0) |
| 324 | pr_crit("%s: Cannot add TCP protocol offload\n", __func__); |
| 325 | if (udp_offload_init() < 0) |
| 326 | pr_crit("%s: Cannot add UDP protocol offload\n", __func__); |
| 327 | if (ipv6_exthdrs_offload_init() < 0) |
| 328 | pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__); |
| 329 | |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 330 | dev_add_offload(&ipv6_packet_offload); |
Eric Dumazet | 61c1db7 | 2013-10-20 20:47:30 -0700 | [diff] [blame] | 331 | |
| 332 | inet_add_offload(&sit_offload, IPPROTO_IPV6); |
| 333 | |
Vlad Yasevich | c6b641a | 2012-11-15 08:49:22 +0000 | [diff] [blame] | 334 | return 0; |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Vlad Yasevich | c6b641a | 2012-11-15 08:49:22 +0000 | [diff] [blame] | 337 | fs_initcall(ipv6_offload_init); |