2 * IPV6 GSO/GRO offload support
3 * Linux INET6 implementation
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.
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>
17 #include <net/protocol.h>
20 #include "ip6_offload.h"
22 static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
24 const struct net_offload *ops = NULL;
27 struct ipv6_opt_hdr *opth;
30 if (proto != NEXTHDR_HOP) {
31 ops = rcu_dereference(inet6_offloads[proto]);
36 if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
40 if (unlikely(!pskb_may_pull(skb, 8)))
43 opth = (void *)skb->data;
44 len = ipv6_optlen(opth);
46 if (unlikely(!pskb_may_pull(skb, len)))
49 opth = (void *)skb->data;
50 proto = opth->nexthdr;
57 static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
58 netdev_features_t features)
60 struct sk_buff *segs = ERR_PTR(-EINVAL);
61 struct ipv6hdr *ipv6h;
62 const struct net_offload *ops;
64 struct frag_hdr *fptr;
65 unsigned int unfrag_ip6hlen;
71 if (unlikely(skb_shinfo(skb)->gso_type &
80 SKB_GSO_UDP_TUNNEL_CSUM |
86 skb_reset_network_header(skb);
87 nhoff = skb_network_header(skb) - skb_mac_header(skb);
88 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
91 encap = SKB_GSO_CB(skb)->encap_level > 0;
93 features &= skb->dev->hw_enc_features;
94 SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);
96 ipv6h = ipv6_hdr(skb);
97 __skb_pull(skb, sizeof(*ipv6h));
98 segs = ERR_PTR(-EPROTONOSUPPORT);
100 proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
102 if (skb->encapsulation &&
103 skb_shinfo(skb)->gso_type & (SKB_GSO_SIT|SKB_GSO_IPIP))
104 udpfrag = proto == IPPROTO_UDP && encap;
106 udpfrag = proto == IPPROTO_UDP && !skb->encapsulation;
108 ops = rcu_dereference(inet6_offloads[proto]);
109 if (likely(ops && ops->callbacks.gso_segment)) {
110 skb_reset_transport_header(skb);
111 segs = ops->callbacks.gso_segment(skb, features);
117 for (skb = segs; skb; skb = skb->next) {
118 ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
119 ipv6h->payload_len = htons(skb->len - nhoff - sizeof(*ipv6h));
120 skb->network_header = (u8 *)ipv6h - skb->head;
123 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
124 fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen);
125 fptr->frag_off = htons(offset);
126 if (skb->next != NULL)
127 fptr->frag_off |= htons(IP6_MF);
128 offset += (ntohs(ipv6h->payload_len) -
129 sizeof(struct frag_hdr));
132 skb_reset_inner_headers(skb);
139 /* Return the total length of all the extension hdrs, following the same
140 * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
142 static int ipv6_exthdrs_len(struct ipv6hdr *iph,
143 const struct net_offload **opps)
145 struct ipv6_opt_hdr *opth = (void *)iph;
146 int len = 0, proto, optlen = sizeof(*iph);
148 proto = iph->nexthdr;
150 if (proto != NEXTHDR_HOP) {
151 *opps = rcu_dereference(inet6_offloads[proto]);
152 if (unlikely(!(*opps)))
154 if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
157 opth = (void *)opth + optlen;
158 optlen = ipv6_optlen(opth);
160 proto = opth->nexthdr;
165 static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
168 const struct net_offload *ops;
169 struct sk_buff **pp = NULL;
178 off = skb_gro_offset(skb);
179 hlen = off + sizeof(*iph);
180 iph = skb_gro_header_fast(skb, off);
181 if (skb_gro_header_hard(skb, hlen)) {
182 iph = skb_gro_header_slow(skb, hlen, off);
187 skb_set_network_header(skb, off);
188 skb_gro_pull(skb, sizeof(*iph));
189 skb_set_transport_header(skb, skb_gro_offset(skb));
191 flush += ntohs(iph->payload_len) != skb_gro_len(skb);
194 proto = iph->nexthdr;
195 ops = rcu_dereference(inet6_offloads[proto]);
196 if (!ops || !ops->callbacks.gro_receive) {
197 __pskb_pull(skb, skb_gro_offset(skb));
198 proto = ipv6_gso_pull_exthdrs(skb, proto);
199 skb_gro_pull(skb, -skb_transport_offset(skb));
200 skb_reset_transport_header(skb);
201 __skb_push(skb, skb_gro_offset(skb));
203 ops = rcu_dereference(inet6_offloads[proto]);
204 if (!ops || !ops->callbacks.gro_receive)
210 NAPI_GRO_CB(skb)->proto = proto;
213 nlen = skb_network_header_len(skb);
215 for (p = *head; p; p = p->next) {
216 const struct ipv6hdr *iph2;
217 __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
219 if (!NAPI_GRO_CB(p)->same_flow)
222 iph2 = (struct ipv6hdr *)(p->data + off);
223 first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
225 /* All fields must match except length and Traffic Class.
226 * XXX skbs on the gro_list have all been parsed and pulled
227 * already so we don't need to compare nlen
228 * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops)))
229 * memcmp() alone below is suffcient, right?
231 if ((first_word & htonl(0xF00FFFFF)) ||
232 memcmp(&iph->nexthdr, &iph2->nexthdr,
233 nlen - offsetof(struct ipv6hdr, nexthdr))) {
234 NAPI_GRO_CB(p)->same_flow = 0;
237 /* flush if Traffic Class fields are different */
238 NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000));
239 NAPI_GRO_CB(p)->flush |= flush;
241 /* Clear flush_id, there's really no concept of ID in IPv6. */
242 NAPI_GRO_CB(p)->flush_id = 0;
245 NAPI_GRO_CB(skb)->flush |= flush;
247 skb_gro_postpull_rcsum(skb, iph, nlen);
249 pp = ops->callbacks.gro_receive(head, skb);
255 NAPI_GRO_CB(skb)->flush |= flush;
260 static int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
262 const struct net_offload *ops;
263 struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff);
266 iph->payload_len = htons(skb->len - nhoff - sizeof(*iph));
270 nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
271 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
274 err = ops->callbacks.gro_complete(skb, nhoff);
282 static struct packet_offload ipv6_packet_offload __read_mostly = {
283 .type = cpu_to_be16(ETH_P_IPV6),
285 .gso_segment = ipv6_gso_segment,
286 .gro_receive = ipv6_gro_receive,
287 .gro_complete = ipv6_gro_complete,
291 static const struct net_offload sit_offload = {
293 .gso_segment = ipv6_gso_segment,
294 .gro_receive = ipv6_gro_receive,
295 .gro_complete = ipv6_gro_complete,
299 static int __init ipv6_offload_init(void)
302 if (tcpv6_offload_init() < 0)
303 pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
304 if (udp_offload_init() < 0)
305 pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
306 if (ipv6_exthdrs_offload_init() < 0)
307 pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
309 dev_add_offload(&ipv6_packet_offload);
311 inet_add_offload(&sit_offload, IPPROTO_IPV6);
316 fs_initcall(ipv6_offload_init);