Home | History | Annotate | Line # | Download | only in netinet6
in6_offload.c revision 1.8.2.1
      1 /*	$NetBSD: in6_offload.c,v 1.8.2.1 2019/06/10 22:09:48 christos 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.2.1 2019/06/10 22:09:48 christos 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 /*
     47  * Handle M_CSUM_TSOv6 in software. Split the TCP payload in chunks of
     48  * size MSS, and return mbuf chain consists of them.
     49  */
     50 struct mbuf *
     51 tcp6_segment(struct mbuf *m, int off)
     52 {
     53 	int mss;
     54 	int iphlen;
     55 	int thlen;
     56 	int hlen;
     57 	int len;
     58 	struct ip6_hdr *iph;
     59 	struct tcphdr *th;
     60 	uint32_t tcpseq;
     61 	uint16_t phsum;
     62 	struct mbuf *hdr = NULL;
     63 	struct mbuf *m0 = NULL;
     64 	struct mbuf *prev = NULL;
     65 	struct mbuf *n, *t;
     66 	int nsegs;
     67 
     68 	KASSERT((m->m_flags & M_PKTHDR) != 0);
     69 	KASSERT((m->m_pkthdr.csum_flags & M_CSUM_TSOv6) != 0);
     70 
     71 	m->m_pkthdr.csum_flags = 0;
     72 
     73 	len = m->m_pkthdr.len;
     74 	KASSERT(len >= off + sizeof(*iph) + sizeof(*th));
     75 
     76 	hlen = off + sizeof(*iph);
     77 	if (m->m_len < hlen) {
     78 		m = m_pullup(m, hlen);
     79 		if (m == NULL)
     80 			goto quit;
     81 	}
     82 	iph = (void *)(mtod(m, char *) + off);
     83 	iphlen = sizeof(*iph);
     84 	KASSERT((iph->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION);
     85 	KASSERT(iph->ip6_nxt == IPPROTO_TCP);
     86 
     87 	hlen = off + iphlen + sizeof(*th);
     88 	if (m->m_len < hlen) {
     89 		m = m_pullup(m, hlen);
     90 		if (m == NULL)
     91 			goto quit;
     92 	}
     93 	th = (void *)(mtod(m, char *) + off + iphlen);
     94 	tcpseq = ntohl(th->th_seq);
     95 	thlen = th->th_off * 4;
     96 	hlen = off + iphlen + thlen;
     97 
     98 	mss = m->m_pkthdr.segsz;
     99 	KASSERT(mss != 0);
    100 	KASSERT(len > hlen);
    101 
    102 	t = m_split(m, hlen, M_NOWAIT);
    103 	if (t == NULL)
    104 		goto quit;
    105 	hdr = m;
    106 	m = t;
    107 
    108 	len -= hlen;
    109 	KASSERT(len % mss == 0);
    110 
    111 	iph = (void *)(mtod(hdr, char *) + off);
    112 	iph->ip6_plen = htons(thlen + mss);
    113 	phsum = in6_cksum_phdr(&iph->ip6_src, &iph->ip6_dst, htonl(thlen + mss),
    114 	    htonl(IPPROTO_TCP));
    115 
    116 	for (nsegs = len / mss; nsegs > 0; nsegs--) {
    117 		if (nsegs > 1) {
    118 			n = m_dup(hdr, 0, hlen, M_NOWAIT);
    119 			if (n == NULL)
    120 				goto quit;
    121 		} else
    122 			n = hdr;
    123 		KASSERT(n->m_len == hlen); /* XXX */
    124 
    125 		if (nsegs > 1) {
    126 			t = m_split(m, mss, M_NOWAIT);
    127 			if (t == NULL) {
    128 				m_freem(n);
    129 				goto quit;
    130 			}
    131 		} else
    132 			t = m;
    133 		m_cat(n, m);
    134 		m = t;
    135 
    136 		KASSERT(n->m_len >= hlen); /* XXX */
    137 
    138 		if (m0 == NULL)
    139 			m0 = n;
    140 
    141 		if (prev != NULL)
    142 			prev->m_nextpkt = n;
    143 
    144 		n->m_pkthdr.len = hlen + mss;
    145 		n->m_nextpkt = NULL;	/* XXX */
    146 
    147 		th = (void *)(mtod(n, char *) + off + iphlen);
    148 		th->th_seq = htonl(tcpseq);
    149 		th->th_sum = phsum;
    150 		th->th_sum = in6_cksum(n, 0, off + iphlen, thlen + mss);
    151 
    152 		tcpseq += mss;
    153 		prev = n;
    154 	}
    155 	return m0;
    156 
    157 quit:
    158 	if (hdr != NULL)
    159 		m_freem(hdr);
    160 	if (m != NULL)
    161 		m_freem(m);
    162 	for (m = m0; m != NULL; m = n) {
    163 		n = m->m_nextpkt;
    164 		m_freem(m);
    165 	}
    166 
    167 	return NULL;
    168 }
    169 
    170 int
    171 ip6_tso_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
    172     const struct sockaddr_in6 *dst, struct rtentry *rt)
    173 {
    174 	struct mbuf *n;
    175 	int error = 0;
    176 
    177 	m = tcp6_segment(m, 0);
    178 	if (m == NULL)
    179 		return ENOMEM;
    180 	do {
    181 		n = m->m_nextpkt;
    182 		if (error == 0)
    183 			error = ip6_if_output(ifp, origifp, m, dst, rt);
    184 		else
    185 			m_freem(m);
    186 		m = n;
    187 	} while (m != NULL);
    188 	return error;
    189 }
    190 
    191 /*
    192  * Compute now in software the IP and TCP/UDP checksums. Cancel the
    193  * hardware offloading.
    194  */
    195 void
    196 in6_undefer_cksum(struct mbuf *m, size_t hdrlen, int csum_flags)
    197 {
    198 	const size_t ip6_plen_offset =
    199 	    hdrlen + offsetof(struct ip6_hdr, ip6_plen);
    200 	size_t l4hdroff;
    201 	size_t l4offset;
    202 	uint16_t plen;
    203 	uint16_t csum;
    204 
    205 	KASSERT(m->m_flags & M_PKTHDR);
    206 	KASSERT((m->m_pkthdr.csum_flags & csum_flags) == csum_flags);
    207 	KASSERT(csum_flags == M_CSUM_UDPv6 || csum_flags == M_CSUM_TCPv6);
    208 
    209 	if (__predict_true(hdrlen + sizeof(struct ip6_hdr) <= m->m_len)) {
    210 		plen = *(uint16_t *)(mtod(m, char *) + ip6_plen_offset);
    211 	} else {
    212 		m_copydata(m, ip6_plen_offset, sizeof(plen), &plen);
    213 	}
    214 	plen = ntohs(plen);
    215 
    216 	l4hdroff = M_CSUM_DATA_IPv6_IPHL(m->m_pkthdr.csum_data);
    217 	l4offset = hdrlen + l4hdroff;
    218 	csum = in6_cksum(m, 0, l4offset,
    219 	    plen - (l4hdroff - sizeof(struct ip6_hdr)));
    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 
    235 /*
    236  * Compute now in software the TCP/UDP checksum. Cancel the hardware
    237  * offloading.
    238  */
    239 void
    240 in6_undefer_cksum_tcpudp(struct mbuf *m)
    241 {
    242 	uint16_t csum, offset;
    243 
    244 	KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
    245 	KASSERT((~m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
    246 	KASSERT((m->m_pkthdr.csum_flags
    247 	    & (M_CSUM_UDPv4|M_CSUM_TCPv4|M_CSUM_TSOv4)) == 0);
    248 
    249 	offset = M_CSUM_DATA_IPv6_IPHL(m->m_pkthdr.csum_data);
    250 	csum = in6_cksum(m, 0, offset, m->m_pkthdr.len - offset);
    251 	if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv6) != 0) {
    252 		csum = 0xffff;
    253 	}
    254 
    255 	offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data);
    256 	if ((offset + sizeof(csum)) > m->m_len) {
    257 		m_copyback(m, offset, sizeof(csum), &csum);
    258 	} else {
    259 		*(uint16_t *)(mtod(m, char *) + offset) = csum;
    260 	}
    261 }
    262