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; |
Cong Wang | d949d82 | 2013-08-31 13:44:37 +0800 | [diff] [blame] | 92 | bool tunnel; |
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 | |
Eric Dumazet | cb32f51 | 2013-10-19 11:42:57 -0700 | [diff] [blame] | 100 | SKB_GSO_IPIP | |
Eric Dumazet | 61c1db7 | 2013-10-20 20:47:30 -0700 | [diff] [blame] | 101 | SKB_GSO_SIT | |
Pravin B Shelar | 7313626 | 2013-03-07 13:21:51 +0000 | [diff] [blame] | 102 | SKB_GSO_UDP_TUNNEL | |
Simon Horman | 0d89d20 | 2013-05-23 21:02:52 +0000 | [diff] [blame] | 103 | SKB_GSO_MPLS | |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 104 | SKB_GSO_TCPV6 | |
| 105 | 0))) |
| 106 | goto out; |
| 107 | |
Eric Dumazet | d3e5e00 | 2013-10-20 20:47:29 -0700 | [diff] [blame] | 108 | skb_reset_network_header(skb); |
| 109 | nhoff = skb_network_header(skb) - skb_mac_header(skb); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 110 | if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h)))) |
| 111 | goto out; |
| 112 | |
Eric Dumazet | d3e5e00 | 2013-10-20 20:47:29 -0700 | [diff] [blame] | 113 | tunnel = SKB_GSO_CB(skb)->encap_level > 0; |
| 114 | if (tunnel) |
| 115 | features = skb->dev->hw_enc_features & netif_skb_features(skb); |
| 116 | SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h); |
| 117 | |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 118 | ipv6h = ipv6_hdr(skb); |
| 119 | __skb_pull(skb, sizeof(*ipv6h)); |
| 120 | segs = ERR_PTR(-EPROTONOSUPPORT); |
| 121 | |
| 122 | proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr); |
Eric Dumazet | b917eb1 | 2013-10-18 14:43:55 -0700 | [diff] [blame] | 123 | |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 124 | ops = rcu_dereference(inet6_offloads[proto]); |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 125 | if (likely(ops && ops->callbacks.gso_segment)) { |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 126 | skb_reset_transport_header(skb); |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 127 | segs = ops->callbacks.gso_segment(skb, features); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 128 | } |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 129 | |
| 130 | if (IS_ERR(segs)) |
| 131 | goto out; |
| 132 | |
| 133 | for (skb = segs; skb; skb = skb->next) { |
Eric Dumazet | d3e5e00 | 2013-10-20 20:47:29 -0700 | [diff] [blame] | 134 | ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff); |
| 135 | ipv6h->payload_len = htons(skb->len - nhoff - sizeof(*ipv6h)); |
| 136 | if (tunnel) { |
| 137 | skb_reset_inner_headers(skb); |
| 138 | skb->encapsulation = 1; |
| 139 | } |
| 140 | skb->network_header = (u8 *)ipv6h - skb->head; |
| 141 | |
Cong Wang | d949d82 | 2013-08-31 13:44:37 +0800 | [diff] [blame] | 142 | if (!tunnel && proto == IPPROTO_UDP) { |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 143 | unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr); |
Eric Dumazet | d3e5e00 | 2013-10-20 20:47:29 -0700 | [diff] [blame] | 144 | fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 145 | fptr->frag_off = htons(offset); |
| 146 | if (skb->next != NULL) |
| 147 | fptr->frag_off |= htons(IP6_MF); |
| 148 | offset += (ntohs(ipv6h->payload_len) - |
| 149 | sizeof(struct frag_hdr)); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | out: |
| 154 | return segs; |
| 155 | } |
| 156 | |
| 157 | static struct sk_buff **ipv6_gro_receive(struct sk_buff **head, |
| 158 | struct sk_buff *skb) |
| 159 | { |
| 160 | const struct net_offload *ops; |
| 161 | struct sk_buff **pp = NULL; |
| 162 | struct sk_buff *p; |
| 163 | struct ipv6hdr *iph; |
| 164 | unsigned int nlen; |
| 165 | unsigned int hlen; |
| 166 | unsigned int off; |
| 167 | int flush = 1; |
| 168 | int proto; |
| 169 | __wsum csum; |
| 170 | |
| 171 | off = skb_gro_offset(skb); |
| 172 | hlen = off + sizeof(*iph); |
| 173 | iph = skb_gro_header_fast(skb, off); |
| 174 | if (skb_gro_header_hard(skb, hlen)) { |
| 175 | iph = skb_gro_header_slow(skb, hlen, off); |
| 176 | if (unlikely(!iph)) |
| 177 | goto out; |
| 178 | } |
| 179 | |
| 180 | skb_gro_pull(skb, sizeof(*iph)); |
| 181 | skb_set_transport_header(skb, skb_gro_offset(skb)); |
| 182 | |
| 183 | flush += ntohs(iph->payload_len) != skb_gro_len(skb); |
| 184 | |
| 185 | rcu_read_lock(); |
| 186 | proto = iph->nexthdr; |
| 187 | ops = rcu_dereference(inet6_offloads[proto]); |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 188 | if (!ops || !ops->callbacks.gro_receive) { |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 189 | __pskb_pull(skb, skb_gro_offset(skb)); |
| 190 | proto = ipv6_gso_pull_exthdrs(skb, proto); |
| 191 | skb_gro_pull(skb, -skb_transport_offset(skb)); |
| 192 | skb_reset_transport_header(skb); |
| 193 | __skb_push(skb, skb_gro_offset(skb)); |
| 194 | |
| 195 | ops = rcu_dereference(inet6_offloads[proto]); |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 196 | if (!ops || !ops->callbacks.gro_receive) |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 197 | goto out_unlock; |
| 198 | |
| 199 | iph = ipv6_hdr(skb); |
| 200 | } |
| 201 | |
| 202 | NAPI_GRO_CB(skb)->proto = proto; |
| 203 | |
| 204 | flush--; |
| 205 | nlen = skb_network_header_len(skb); |
| 206 | |
| 207 | for (p = *head; p; p = p->next) { |
| 208 | const struct ipv6hdr *iph2; |
| 209 | __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */ |
| 210 | |
| 211 | if (!NAPI_GRO_CB(p)->same_flow) |
| 212 | continue; |
| 213 | |
| 214 | iph2 = ipv6_hdr(p); |
| 215 | first_word = *(__be32 *)iph ^ *(__be32 *)iph2 ; |
| 216 | |
| 217 | /* All fields must match except length and Traffic Class. */ |
| 218 | if (nlen != skb_network_header_len(p) || |
| 219 | (first_word & htonl(0xF00FFFFF)) || |
| 220 | memcmp(&iph->nexthdr, &iph2->nexthdr, |
| 221 | nlen - offsetof(struct ipv6hdr, nexthdr))) { |
| 222 | NAPI_GRO_CB(p)->same_flow = 0; |
| 223 | continue; |
| 224 | } |
| 225 | /* flush if Traffic Class fields are different */ |
| 226 | NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000)); |
| 227 | NAPI_GRO_CB(p)->flush |= flush; |
| 228 | } |
| 229 | |
| 230 | NAPI_GRO_CB(skb)->flush |= flush; |
| 231 | |
| 232 | csum = skb->csum; |
| 233 | skb_postpull_rcsum(skb, iph, skb_network_header_len(skb)); |
| 234 | |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 235 | pp = ops->callbacks.gro_receive(head, skb); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 236 | |
| 237 | skb->csum = csum; |
| 238 | |
| 239 | out_unlock: |
| 240 | rcu_read_unlock(); |
| 241 | |
| 242 | out: |
| 243 | NAPI_GRO_CB(skb)->flush |= flush; |
| 244 | |
| 245 | return pp; |
| 246 | } |
| 247 | |
| 248 | static int ipv6_gro_complete(struct sk_buff *skb) |
| 249 | { |
| 250 | const struct net_offload *ops; |
| 251 | struct ipv6hdr *iph = ipv6_hdr(skb); |
| 252 | int err = -ENOSYS; |
| 253 | |
| 254 | iph->payload_len = htons(skb->len - skb_network_offset(skb) - |
| 255 | sizeof(*iph)); |
| 256 | |
| 257 | rcu_read_lock(); |
| 258 | ops = rcu_dereference(inet6_offloads[NAPI_GRO_CB(skb)->proto]); |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 259 | if (WARN_ON(!ops || !ops->callbacks.gro_complete)) |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 260 | goto out_unlock; |
| 261 | |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 262 | err = ops->callbacks.gro_complete(skb); |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 263 | |
| 264 | out_unlock: |
| 265 | rcu_read_unlock(); |
| 266 | |
| 267 | return err; |
| 268 | } |
| 269 | |
| 270 | static struct packet_offload ipv6_packet_offload __read_mostly = { |
| 271 | .type = cpu_to_be16(ETH_P_IPV6), |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 272 | .callbacks = { |
| 273 | .gso_send_check = ipv6_gso_send_check, |
| 274 | .gso_segment = ipv6_gso_segment, |
| 275 | .gro_receive = ipv6_gro_receive, |
| 276 | .gro_complete = ipv6_gro_complete, |
| 277 | }, |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
Eric Dumazet | 61c1db7 | 2013-10-20 20:47:30 -0700 | [diff] [blame] | 280 | static const struct net_offload sit_offload = { |
| 281 | .callbacks = { |
| 282 | .gso_send_check = ipv6_gso_send_check, |
| 283 | .gso_segment = ipv6_gso_segment, |
| 284 | }, |
| 285 | }; |
| 286 | |
Vlad Yasevich | c6b641a | 2012-11-15 08:49:22 +0000 | [diff] [blame] | 287 | static int __init ipv6_offload_init(void) |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 288 | { |
Vlad Yasevich | c6b641a | 2012-11-15 08:49:22 +0000 | [diff] [blame] | 289 | |
| 290 | if (tcpv6_offload_init() < 0) |
| 291 | pr_crit("%s: Cannot add TCP protocol offload\n", __func__); |
| 292 | if (udp_offload_init() < 0) |
| 293 | pr_crit("%s: Cannot add UDP protocol offload\n", __func__); |
| 294 | if (ipv6_exthdrs_offload_init() < 0) |
| 295 | pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__); |
| 296 | |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 297 | dev_add_offload(&ipv6_packet_offload); |
Eric Dumazet | 61c1db7 | 2013-10-20 20:47:30 -0700 | [diff] [blame] | 298 | |
| 299 | inet_add_offload(&sit_offload, IPPROTO_IPV6); |
| 300 | |
Vlad Yasevich | c6b641a | 2012-11-15 08:49:22 +0000 | [diff] [blame] | 301 | return 0; |
Vlad Yasevich | d1da932 | 2012-11-15 08:49:16 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Vlad Yasevich | c6b641a | 2012-11-15 08:49:22 +0000 | [diff] [blame] | 304 | fs_initcall(ipv6_offload_init); |