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