xref: /netbsd-src/sys/netinet6/in6_offload.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: in6_offload.c,v 1.8 2018/06/01 08:56:00 maxv Exp $	*/
2 
3 /*-
4  * Copyright (c)2006 YAMAMOTO Takashi,
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: in6_offload.c,v 1.8 2018/06/01 08:56:00 maxv Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/mbuf.h>
34 
35 #include <net/if.h>
36 
37 #include <netinet/in.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/ip6.h>
40 #include <netinet/tcp.h>
41 #include <netinet6/in6_var.h>
42 #include <netinet6/ip6_var.h>
43 #include <netinet6/nd6.h>
44 #include <netinet6/in6_offload.h>
45 
46 struct ip6_tso_output_args {
47 	struct ifnet *ifp;
48 	struct ifnet *origifp;
49 	const struct sockaddr_in6 *dst;
50 	struct rtentry *rt;
51 };
52 
53 static int ip6_tso_output_callback(void *, struct mbuf *);
54 
55 static int
56 ip6_tso_output_callback(void *vp, struct mbuf *m)
57 {
58 	struct ip6_tso_output_args *args = vp;
59 
60 	return ip6_if_output(args->ifp, args->origifp, m, args->dst, args->rt);
61 }
62 
63 int
64 ip6_tso_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
65     const struct sockaddr_in6 *dst, struct rtentry *rt)
66 {
67 	struct ip6_tso_output_args args;
68 
69 	args.ifp = ifp;
70 	args.origifp = origifp;
71 	args.dst = dst;
72 	args.rt = rt;
73 
74 	return tcp6_segment(m, ip6_tso_output_callback, &args);
75 }
76 
77 /*
78  * tcp6_segment: handle M_CSUM_TSOv6 by software.
79  *
80  * => always consume m.
81  * => call output_func with output_arg for each segments.
82  */
83 
84 int
85 tcp6_segment(struct mbuf *m, int (*output_func)(void *, struct mbuf *),
86     void *output_arg)
87 {
88 	int mss;
89 	int iphlen;
90 	int thlen;
91 	int hlen;
92 	int len;
93 	struct ip6_hdr *iph;
94 	struct tcphdr *th;
95 	uint32_t tcpseq;
96 	struct mbuf *hdr = NULL;
97 	struct mbuf *t;
98 	int error = 0;
99 
100 	KASSERT((m->m_flags & M_PKTHDR) != 0);
101 	KASSERT((m->m_pkthdr.csum_flags & M_CSUM_TSOv6) != 0);
102 
103 	m->m_pkthdr.csum_flags = 0;
104 
105 	len = m->m_pkthdr.len;
106 	KASSERT(len >= sizeof(*iph) + sizeof(*th));
107 
108 	if (m->m_len < sizeof(*iph)) {
109 		m = m_pullup(m, sizeof(*iph));
110 		if (m == NULL) {
111 			error = ENOMEM;
112 			goto quit;
113 		}
114 	}
115 	iph = mtod(m, struct ip6_hdr *);
116 	iphlen = sizeof(*iph);
117 	KASSERT((iph->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION);
118 	KASSERT(iph->ip6_nxt == IPPROTO_TCP);
119 
120 	hlen = iphlen + sizeof(*th);
121 	if (m->m_len < hlen) {
122 		m = m_pullup(m, hlen);
123 		if (m == NULL) {
124 			error = ENOMEM;
125 			goto quit;
126 		}
127 	}
128 	th = (void *)(mtod(m, char *) + iphlen);
129 	tcpseq = ntohl(th->th_seq);
130 	thlen = th->th_off * 4;
131 	hlen = iphlen + thlen;
132 
133 	mss = m->m_pkthdr.segsz;
134 	KASSERT(mss != 0);
135 	KASSERT(len > hlen);
136 
137 	t = m_split(m, hlen, M_NOWAIT);
138 	if (t == NULL) {
139 		error = ENOMEM;
140 		goto quit;
141 	}
142 	hdr = m;
143 	m = t;
144 	len -= hlen;
145 	KASSERT(len % mss == 0);
146 	while (len > 0) {
147 		struct mbuf *n;
148 
149 		n = m_dup(hdr, 0, hlen, M_NOWAIT);
150 		if (n == NULL) {
151 			error = ENOMEM;
152 			goto quit;
153 		}
154 		KASSERT(n->m_len == hlen); /* XXX */
155 
156 		t = m_split(m, mss, M_NOWAIT);
157 		if (t == NULL) {
158 			m_freem(n);
159 			error = ENOMEM;
160 			goto quit;
161 		}
162 		m_cat(n, m);
163 		m = t;
164 
165 		KASSERT(n->m_len >= hlen); /* XXX */
166 
167 		n->m_pkthdr.len = hlen + mss;
168 		iph = mtod(n, struct ip6_hdr *);
169 		KASSERT((iph->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION);
170 		iph->ip6_plen = htons(thlen + mss);
171 		th = (void *)(mtod(n, char *) + iphlen);
172 		th->th_seq = htonl(tcpseq);
173 		th->th_sum = 0;
174 		th->th_sum = in6_cksum(n, IPPROTO_TCP, iphlen, thlen + mss);
175 
176 		error = (*output_func)(output_arg, n);
177 		if (error) {
178 			goto quit;
179 		}
180 
181 		tcpseq += mss;
182 		len -= mss;
183 	}
184 
185 quit:
186 	if (hdr != NULL) {
187 		m_freem(hdr);
188 	}
189 	if (m != NULL) {
190 		m_freem(m);
191 	}
192 
193 	return error;
194 }
195 
196 void
197 ip6_undefer_csum(struct mbuf *m, size_t hdrlen, int csum_flags)
198 {
199 	const size_t ip6_plen_offset =
200 	    hdrlen + offsetof(struct ip6_hdr, ip6_plen);
201 	size_t l4hdroff;
202 	size_t l4offset;
203 	uint16_t plen;
204 	uint16_t csum;
205 
206 	KASSERT(m->m_flags & M_PKTHDR);
207 	KASSERT((m->m_pkthdr.csum_flags & csum_flags) == csum_flags);
208 	KASSERT(csum_flags == M_CSUM_UDPv6 || csum_flags == M_CSUM_TCPv6);
209 
210 	if (__predict_true(hdrlen + sizeof(struct ip6_hdr) <= m->m_len)) {
211 		plen = *(uint16_t *)(mtod(m, char *) + ip6_plen_offset);
212 	} else {
213 		m_copydata(m, ip6_plen_offset, sizeof(plen), &plen);
214 	}
215 	plen = ntohs(plen);
216 
217 	l4hdroff = M_CSUM_DATA_IPv6_IPHL(m->m_pkthdr.csum_data);
218 	l4offset = hdrlen + l4hdroff;
219 	csum = in6_cksum(m, 0, l4offset, plen - l4hdroff);
220 
221 	if (csum == 0 && (csum_flags & M_CSUM_UDPv6) != 0)
222 		csum = 0xffff;
223 
224 	l4offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data);
225 
226 	if (__predict_true((l4offset + sizeof(uint16_t)) <= m->m_len)) {
227 		*(uint16_t *)(mtod(m, char *) + l4offset) = csum;
228 	} else {
229 		m_copyback(m, l4offset, sizeof(csum), (void *) &csum);
230 	}
231 
232 	m->m_pkthdr.csum_flags ^= csum_flags;
233 }
234