ip6_offload.c 8.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *	IPV6 GSO/GRO offload support
 *	Linux INET6 implementation
 *
 *	This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/socket.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
15
#include <linux/printk.h>
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

#include <net/protocol.h>
#include <net/ipv6.h>

#include "ip6_offload.h"

static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
{
	const struct net_offload *ops = NULL;

	for (;;) {
		struct ipv6_opt_hdr *opth;
		int len;

		if (proto != NEXTHDR_HOP) {
			ops = rcu_dereference(inet6_offloads[proto]);

			if (unlikely(!ops))
				break;

			if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
				break;
		}

		if (unlikely(!pskb_may_pull(skb, 8)))
			break;

		opth = (void *)skb->data;
		len = ipv6_optlen(opth);

		if (unlikely(!pskb_may_pull(skb, len)))
			break;

		proto = opth->nexthdr;
		__skb_pull(skb, len);
	}

	return proto;
}

static int ipv6_gso_send_check(struct sk_buff *skb)
{
	const struct ipv6hdr *ipv6h;
	const struct net_offload *ops;
	int err = -EINVAL;

	if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
		goto out;

	ipv6h = ipv6_hdr(skb);
	__skb_pull(skb, sizeof(*ipv6h));
	err = -EPROTONOSUPPORT;

	ops = rcu_dereference(inet6_offloads[
		ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);

72
	if (likely(ops && ops->callbacks.gso_send_check)) {
73
		skb_reset_transport_header(skb);
74
		err = ops->callbacks.gso_send_check(skb);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	}

out:
	return err;
}

static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
	netdev_features_t features)
{
	struct sk_buff *segs = ERR_PTR(-EINVAL);
	struct ipv6hdr *ipv6h;
	const struct net_offload *ops;
	int proto;
	struct frag_hdr *fptr;
	unsigned int unfrag_ip6hlen;
	u8 *prevhdr;
	int offset = 0;
92
	bool encap, udpfrag;
93
	int nhoff;
94 95 96 97 98

	if (unlikely(skb_shinfo(skb)->gso_type &
		     ~(SKB_GSO_UDP |
		       SKB_GSO_DODGY |
		       SKB_GSO_TCP_ECN |
99
		       SKB_GSO_GRE |
T
Tom Herbert 已提交
100
		       SKB_GSO_GRE_CSUM |
E
Eric Dumazet 已提交
101
		       SKB_GSO_IPIP |
E
Eric Dumazet 已提交
102
		       SKB_GSO_SIT |
103
		       SKB_GSO_UDP_TUNNEL |
104
		       SKB_GSO_UDP_TUNNEL_CSUM |
S
Simon Horman 已提交
105
		       SKB_GSO_MPLS |
106 107 108 109
		       SKB_GSO_TCPV6 |
		       0)))
		goto out;

110 111
	skb_reset_network_header(skb);
	nhoff = skb_network_header(skb) - skb_mac_header(skb);
112 113 114
	if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
		goto out;

115 116
	encap = SKB_GSO_CB(skb)->encap_level > 0;
	if (encap)
117 118 119
		features = skb->dev->hw_enc_features & netif_skb_features(skb);
	SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);

120 121 122 123 124
	ipv6h = ipv6_hdr(skb);
	__skb_pull(skb, sizeof(*ipv6h));
	segs = ERR_PTR(-EPROTONOSUPPORT);

	proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
E
Eric Dumazet 已提交
125

126 127 128 129 130 131
	if (skb->encapsulation &&
	    skb_shinfo(skb)->gso_type & (SKB_GSO_SIT|SKB_GSO_IPIP))
		udpfrag = proto == IPPROTO_UDP && encap;
	else
		udpfrag = proto == IPPROTO_UDP && !skb->encapsulation;

132
	ops = rcu_dereference(inet6_offloads[proto]);
133
	if (likely(ops && ops->callbacks.gso_segment)) {
134
		skb_reset_transport_header(skb);
135
		segs = ops->callbacks.gso_segment(skb, features);
136 137 138 139 140 141
	}

	if (IS_ERR(segs))
		goto out;

	for (skb = segs; skb; skb = skb->next) {
142 143 144 145
		ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
		ipv6h->payload_len = htons(skb->len - nhoff - sizeof(*ipv6h));
		skb->network_header = (u8 *)ipv6h - skb->head;

146
		if (udpfrag) {
147
			unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
148
			fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen);
149 150 151 152 153 154
			fptr->frag_off = htons(offset);
			if (skb->next != NULL)
				fptr->frag_off |= htons(IP6_MF);
			offset += (ntohs(ipv6h->payload_len) -
				   sizeof(struct frag_hdr));
		}
155 156
		if (encap)
			skb_reset_inner_headers(skb);
157 158 159 160 161 162
	}

out:
	return segs;
}

163 164 165 166 167 168
/* Return the total length of all the extension hdrs, following the same
 * logic in ipv6_gso_pull_exthdrs() when parsing ext-hdrs.
 */
static int ipv6_exthdrs_len(struct ipv6hdr *iph,
			    const struct net_offload **opps)
{
169 170
	struct ipv6_opt_hdr *opth = (void *)iph;
	int len = 0, proto, optlen = sizeof(*iph);
171 172 173 174 175 176 177 178 179 180

	proto = iph->nexthdr;
	for (;;) {
		if (proto != NEXTHDR_HOP) {
			*opps = rcu_dereference(inet6_offloads[proto]);
			if (unlikely(!(*opps)))
				break;
			if (!((*opps)->flags & INET6_PROTO_GSO_EXTHDR))
				break;
		}
181 182
		opth = (void *)opth + optlen;
		optlen = ipv6_optlen(opth);
183 184 185 186 187 188
		len += optlen;
		proto = opth->nexthdr;
	}
	return len;
}

189 190 191 192 193 194 195 196 197 198
static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
					 struct sk_buff *skb)
{
	const struct net_offload *ops;
	struct sk_buff **pp = NULL;
	struct sk_buff *p;
	struct ipv6hdr *iph;
	unsigned int nlen;
	unsigned int hlen;
	unsigned int off;
199
	u16 flush = 1;
200 201 202 203 204 205 206 207 208 209 210
	int proto;

	off = skb_gro_offset(skb);
	hlen = off + sizeof(*iph);
	iph = skb_gro_header_fast(skb, off);
	if (skb_gro_header_hard(skb, hlen)) {
		iph = skb_gro_header_slow(skb, hlen, off);
		if (unlikely(!iph))
			goto out;
	}

211
	skb_set_network_header(skb, off);
212 213 214 215 216 217 218 219
	skb_gro_pull(skb, sizeof(*iph));
	skb_set_transport_header(skb, skb_gro_offset(skb));

	flush += ntohs(iph->payload_len) != skb_gro_len(skb);

	rcu_read_lock();
	proto = iph->nexthdr;
	ops = rcu_dereference(inet6_offloads[proto]);
220
	if (!ops || !ops->callbacks.gro_receive) {
221 222 223 224 225 226 227
		__pskb_pull(skb, skb_gro_offset(skb));
		proto = ipv6_gso_pull_exthdrs(skb, proto);
		skb_gro_pull(skb, -skb_transport_offset(skb));
		skb_reset_transport_header(skb);
		__skb_push(skb, skb_gro_offset(skb));

		ops = rcu_dereference(inet6_offloads[proto]);
228
		if (!ops || !ops->callbacks.gro_receive)
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
			goto out_unlock;

		iph = ipv6_hdr(skb);
	}

	NAPI_GRO_CB(skb)->proto = proto;

	flush--;
	nlen = skb_network_header_len(skb);

	for (p = *head; p; p = p->next) {
		const struct ipv6hdr *iph2;
		__be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */

		if (!NAPI_GRO_CB(p)->same_flow)
			continue;

246
		iph2 = (struct ipv6hdr *)(p->data + off);
247 248
		first_word = *(__be32 *)iph ^ *(__be32 *)iph2 ;

249 250 251 252 253 254 255
		/* All fields must match except length and Traffic Class.
		 * XXX skbs on the gro_list have all been parsed and pulled
		 * already so we don't need to compare nlen
		 * (nlen != (sizeof(*iph2) + ipv6_exthdrs_len(iph2, &ops)))
		 * memcmp() alone below is suffcient, right?
		 */
		 if ((first_word & htonl(0xF00FFFFF)) ||
256 257 258 259 260 261 262 263 264 265 266 267
		    memcmp(&iph->nexthdr, &iph2->nexthdr,
			   nlen - offsetof(struct ipv6hdr, nexthdr))) {
			NAPI_GRO_CB(p)->same_flow = 0;
			continue;
		}
		/* flush if Traffic Class fields are different */
		NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000));
		NAPI_GRO_CB(p)->flush |= flush;
	}

	NAPI_GRO_CB(skb)->flush |= flush;

268
	skb_gro_postpull_rcsum(skb, iph, nlen);
269

270
	pp = ops->callbacks.gro_receive(head, skb);
271 272 273 274 275 276 277 278 279 280

out_unlock:
	rcu_read_unlock();

out:
	NAPI_GRO_CB(skb)->flush |= flush;

	return pp;
}

281
static int ipv6_gro_complete(struct sk_buff *skb, int nhoff)
282 283
{
	const struct net_offload *ops;
284
	struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + nhoff);
285 286
	int err = -ENOSYS;

287
	iph->payload_len = htons(skb->len - nhoff - sizeof(*iph));
288 289

	rcu_read_lock();
290 291

	nhoff += sizeof(*iph) + ipv6_exthdrs_len(iph, &ops);
292
	if (WARN_ON(!ops || !ops->callbacks.gro_complete))
293 294
		goto out_unlock;

295
	err = ops->callbacks.gro_complete(skb, nhoff);
296 297 298 299 300 301 302 303 304

out_unlock:
	rcu_read_unlock();

	return err;
}

static struct packet_offload ipv6_packet_offload __read_mostly = {
	.type = cpu_to_be16(ETH_P_IPV6),
305 306 307 308 309 310
	.callbacks = {
		.gso_send_check = ipv6_gso_send_check,
		.gso_segment = ipv6_gso_segment,
		.gro_receive = ipv6_gro_receive,
		.gro_complete = ipv6_gro_complete,
	},
311 312
};

E
Eric Dumazet 已提交
313 314 315 316 317 318 319
static const struct net_offload sit_offload = {
	.callbacks = {
		.gso_send_check = ipv6_gso_send_check,
		.gso_segment	= ipv6_gso_segment,
	},
};

320
static int __init ipv6_offload_init(void)
321
{
322 323 324 325 326 327 328 329

	if (tcpv6_offload_init() < 0)
		pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
	if (udp_offload_init() < 0)
		pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
	if (ipv6_exthdrs_offload_init() < 0)
		pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);

330
	dev_add_offload(&ipv6_packet_offload);
E
Eric Dumazet 已提交
331 332 333

	inet_add_offload(&sit_offload, IPPROTO_IPV6);

334
	return 0;
335 336
}

337
fs_initcall(ipv6_offload_init);