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