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 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 69 rcu_read_lock(); 70 ops = rcu_dereference(inet6_offloads[ 71 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]); 72 73 if (likely(ops && ops->callbacks.gso_send_check)) { 74 skb_reset_transport_header(skb); 75 err = ops->callbacks.gso_send_check(skb); 76 } 77 rcu_read_unlock(); 78 79 out: 80 return err; 81 } 82 83 static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, 84 netdev_features_t features) 85 { 86 struct sk_buff *segs = ERR_PTR(-EINVAL); 87 struct ipv6hdr *ipv6h; 88 const struct net_offload *ops; 89 int proto; 90 struct frag_hdr *fptr; 91 unsigned int unfrag_ip6hlen; 92 u8 *prevhdr; 93 int offset = 0; 94 95 if (!(features & NETIF_F_V6_CSUM)) 96 features &= ~NETIF_F_SG; 97 98 if (unlikely(skb_shinfo(skb)->gso_type & 99 ~(SKB_GSO_UDP | 100 SKB_GSO_DODGY | 101 SKB_GSO_TCP_ECN | 102 SKB_GSO_GRE | 103 SKB_GSO_TCPV6 | 104 0))) 105 goto out; 106 107 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h)))) 108 goto out; 109 110 ipv6h = ipv6_hdr(skb); 111 __skb_pull(skb, sizeof(*ipv6h)); 112 segs = ERR_PTR(-EPROTONOSUPPORT); 113 114 proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr); 115 rcu_read_lock(); 116 ops = rcu_dereference(inet6_offloads[proto]); 117 if (likely(ops && ops->callbacks.gso_segment)) { 118 skb_reset_transport_header(skb); 119 segs = ops->callbacks.gso_segment(skb, features); 120 } 121 rcu_read_unlock(); 122 123 if (IS_ERR(segs)) 124 goto out; 125 126 for (skb = segs; skb; skb = skb->next) { 127 ipv6h = ipv6_hdr(skb); 128 ipv6h->payload_len = htons(skb->len - skb->mac_len - 129 sizeof(*ipv6h)); 130 if (proto == IPPROTO_UDP) { 131 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr); 132 fptr = (struct frag_hdr *)(skb_network_header(skb) + 133 unfrag_ip6hlen); 134 fptr->frag_off = htons(offset); 135 if (skb->next != NULL) 136 fptr->frag_off |= htons(IP6_MF); 137 offset += (ntohs(ipv6h->payload_len) - 138 sizeof(struct frag_hdr)); 139 } 140 } 141 142 out: 143 return segs; 144 } 145 146 static struct sk_buff **ipv6_gro_receive(struct sk_buff **head, 147 struct sk_buff *skb) 148 { 149 const struct net_offload *ops; 150 struct sk_buff **pp = NULL; 151 struct sk_buff *p; 152 struct ipv6hdr *iph; 153 unsigned int nlen; 154 unsigned int hlen; 155 unsigned int off; 156 int flush = 1; 157 int proto; 158 __wsum csum; 159 160 off = skb_gro_offset(skb); 161 hlen = off + sizeof(*iph); 162 iph = skb_gro_header_fast(skb, off); 163 if (skb_gro_header_hard(skb, hlen)) { 164 iph = skb_gro_header_slow(skb, hlen, off); 165 if (unlikely(!iph)) 166 goto out; 167 } 168 169 skb_gro_pull(skb, sizeof(*iph)); 170 skb_set_transport_header(skb, skb_gro_offset(skb)); 171 172 flush += ntohs(iph->payload_len) != skb_gro_len(skb); 173 174 rcu_read_lock(); 175 proto = iph->nexthdr; 176 ops = rcu_dereference(inet6_offloads[proto]); 177 if (!ops || !ops->callbacks.gro_receive) { 178 __pskb_pull(skb, skb_gro_offset(skb)); 179 proto = ipv6_gso_pull_exthdrs(skb, proto); 180 skb_gro_pull(skb, -skb_transport_offset(skb)); 181 skb_reset_transport_header(skb); 182 __skb_push(skb, skb_gro_offset(skb)); 183 184 ops = rcu_dereference(inet6_offloads[proto]); 185 if (!ops || !ops->callbacks.gro_receive) 186 goto out_unlock; 187 188 iph = ipv6_hdr(skb); 189 } 190 191 NAPI_GRO_CB(skb)->proto = proto; 192 193 flush--; 194 nlen = skb_network_header_len(skb); 195 196 for (p = *head; p; p = p->next) { 197 const struct ipv6hdr *iph2; 198 __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */ 199 200 if (!NAPI_GRO_CB(p)->same_flow) 201 continue; 202 203 iph2 = ipv6_hdr(p); 204 first_word = *(__be32 *)iph ^ *(__be32 *)iph2 ; 205 206 /* All fields must match except length and Traffic Class. */ 207 if (nlen != skb_network_header_len(p) || 208 (first_word & htonl(0xF00FFFFF)) || 209 memcmp(&iph->nexthdr, &iph2->nexthdr, 210 nlen - offsetof(struct ipv6hdr, nexthdr))) { 211 NAPI_GRO_CB(p)->same_flow = 0; 212 continue; 213 } 214 /* flush if Traffic Class fields are different */ 215 NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000)); 216 NAPI_GRO_CB(p)->flush |= flush; 217 } 218 219 NAPI_GRO_CB(skb)->flush |= flush; 220 221 csum = skb->csum; 222 skb_postpull_rcsum(skb, iph, skb_network_header_len(skb)); 223 224 pp = ops->callbacks.gro_receive(head, skb); 225 226 skb->csum = csum; 227 228 out_unlock: 229 rcu_read_unlock(); 230 231 out: 232 NAPI_GRO_CB(skb)->flush |= flush; 233 234 return pp; 235 } 236 237 static int ipv6_gro_complete(struct sk_buff *skb) 238 { 239 const struct net_offload *ops; 240 struct ipv6hdr *iph = ipv6_hdr(skb); 241 int err = -ENOSYS; 242 243 iph->payload_len = htons(skb->len - skb_network_offset(skb) - 244 sizeof(*iph)); 245 246 rcu_read_lock(); 247 ops = rcu_dereference(inet6_offloads[NAPI_GRO_CB(skb)->proto]); 248 if (WARN_ON(!ops || !ops->callbacks.gro_complete)) 249 goto out_unlock; 250 251 err = ops->callbacks.gro_complete(skb); 252 253 out_unlock: 254 rcu_read_unlock(); 255 256 return err; 257 } 258 259 static struct packet_offload ipv6_packet_offload __read_mostly = { 260 .type = cpu_to_be16(ETH_P_IPV6), 261 .callbacks = { 262 .gso_send_check = ipv6_gso_send_check, 263 .gso_segment = ipv6_gso_segment, 264 .gro_receive = ipv6_gro_receive, 265 .gro_complete = ipv6_gro_complete, 266 }, 267 }; 268 269 static int __init ipv6_offload_init(void) 270 { 271 272 if (tcpv6_offload_init() < 0) 273 pr_crit("%s: Cannot add TCP protocol offload\n", __func__); 274 if (udp_offload_init() < 0) 275 pr_crit("%s: Cannot add UDP protocol offload\n", __func__); 276 if (ipv6_exthdrs_offload_init() < 0) 277 pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__); 278 279 dev_add_offload(&ipv6_packet_offload); 280 return 0; 281 } 282 283 fs_initcall(ipv6_offload_init); 284