Home | History | Annotate | Line # | Download | only in netinet6
      1  1.13    rin /*	$NetBSD: in6_offload.c,v 1.13 2024/07/05 04:31:54 rin Exp $	*/
      2   1.1   yamt 
      3  1.10   maxv /*
      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.13    rin __KERNEL_RCSID(0, "$NetBSD: in6_offload.c,v 1.13 2024/07/05 04:31:54 rin 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.10   maxv /*
     47  1.10   maxv  * Handle M_CSUM_TSOv6 in software. Split the TCP payload in chunks of
     48  1.12    rin  * size MSS, and return mbuf chain consists of them.
     49  1.10   maxv  */
     50  1.12    rin struct mbuf *
     51  1.12    rin 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.12    rin 	uint16_t phsum;
     62   1.1   yamt 	struct mbuf *hdr = NULL;
     63  1.12    rin 	struct mbuf *m0 = NULL;
     64  1.12    rin 	struct mbuf *prev = NULL;
     65  1.12    rin 	struct mbuf *n, *t;
     66  1.12    rin 	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.12    rin 	KASSERT(len >= off + sizeof(*iph) + sizeof(*th));
     75   1.1   yamt 
     76  1.12    rin 	hlen = off + sizeof(*iph);
     77  1.12    rin 	if (m->m_len < hlen) {
     78  1.12    rin 		m = m_pullup(m, hlen);
     79  1.12    rin 		if (m == NULL)
     80   1.1   yamt 			goto quit;
     81   1.1   yamt 	}
     82  1.12    rin 	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.12    rin 	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.12    rin 		if (m == NULL)
     91   1.1   yamt 			goto quit;
     92   1.1   yamt 	}
     93  1.12    rin 	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.12    rin 	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.12    rin 	if (t == NULL)
    104   1.1   yamt 		goto quit;
    105   1.1   yamt 	hdr = m;
    106   1.1   yamt 	m = t;
    107  1.12    rin 
    108   1.1   yamt 	len -= hlen;
    109   1.1   yamt 	KASSERT(len % mss == 0);
    110   1.1   yamt 
    111  1.12    rin 	iph = (void *)(mtod(hdr, char *) + off);
    112  1.12    rin 	iph->ip6_plen = htons(thlen + mss);
    113  1.12    rin 	phsum = in6_cksum_phdr(&iph->ip6_src, &iph->ip6_dst, htonl(thlen + mss),
    114  1.12    rin 	    htonl(IPPROTO_TCP));
    115  1.12    rin 
    116  1.12    rin 	for (nsegs = len / mss; nsegs > 0; nsegs--) {
    117  1.12    rin 		if (nsegs > 1) {
    118  1.12    rin 			n = m_dup(hdr, 0, hlen, M_NOWAIT);
    119  1.12    rin 			if (n == NULL)
    120  1.12    rin 				goto quit;
    121  1.12    rin 		} else
    122  1.12    rin 			n = hdr;
    123   1.1   yamt 		KASSERT(n->m_len == hlen); /* XXX */
    124   1.1   yamt 
    125  1.12    rin 		if (nsegs > 1) {
    126  1.12    rin 			t = m_split(m, mss, M_NOWAIT);
    127  1.12    rin 			if (t == NULL) {
    128  1.12    rin 				m_freem(n);
    129  1.12    rin 				goto quit;
    130  1.12    rin 			}
    131  1.12    rin 		} else
    132  1.12    rin 			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.12    rin 		if (m0 == NULL)
    139  1.12    rin 			m0 = n;
    140  1.12    rin 
    141  1.12    rin 		if (prev != NULL)
    142  1.12    rin 			prev->m_nextpkt = n;
    143  1.12    rin 
    144   1.1   yamt 		n->m_pkthdr.len = hlen + mss;
    145  1.12    rin 		n->m_nextpkt = NULL;	/* XXX */
    146  1.12    rin 
    147  1.12    rin 		th = (void *)(mtod(n, char *) + off + iphlen);
    148   1.1   yamt 		th->th_seq = htonl(tcpseq);
    149  1.12    rin 		th->th_sum = phsum;
    150  1.12    rin 		th->th_sum = in6_cksum(n, 0, off + iphlen, thlen + mss);
    151   1.1   yamt 
    152   1.1   yamt 		tcpseq += mss;
    153  1.12    rin 		prev = n;
    154   1.1   yamt 	}
    155  1.12    rin 	return m0;
    156   1.1   yamt 
    157   1.1   yamt quit:
    158  1.13    rin 	m_freem(hdr);
    159  1.13    rin 	m_freem(m);
    160  1.12    rin 	for (m = m0; m != NULL; m = n) {
    161  1.12    rin 		n = m->m_nextpkt;
    162   1.1   yamt 		m_freem(m);
    163   1.1   yamt 	}
    164   1.1   yamt 
    165  1.12    rin 	return NULL;
    166   1.1   yamt }
    167   1.5   matt 
    168  1.10   maxv int
    169  1.10   maxv ip6_tso_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
    170  1.10   maxv     const struct sockaddr_in6 *dst, struct rtentry *rt)
    171  1.10   maxv {
    172  1.12    rin 	struct mbuf *n;
    173  1.12    rin 	int error = 0;
    174  1.12    rin 
    175  1.12    rin 	m = tcp6_segment(m, 0);
    176  1.12    rin 	if (m == NULL)
    177  1.12    rin 		return ENOMEM;
    178  1.12    rin 	do {
    179  1.12    rin 		n = m->m_nextpkt;
    180  1.12    rin 		if (error == 0)
    181  1.12    rin 			error = ip6_if_output(ifp, origifp, m, dst, rt);
    182  1.12    rin 		else
    183  1.12    rin 			m_freem(m);
    184  1.12    rin 		m = n;
    185  1.12    rin 	} while (m != NULL);
    186  1.12    rin 	return error;
    187  1.10   maxv }
    188  1.10   maxv 
    189   1.9   maxv /*
    190   1.9   maxv  * Compute now in software the IP and TCP/UDP checksums. Cancel the
    191   1.9   maxv  * hardware offloading.
    192   1.9   maxv  */
    193   1.5   matt void
    194   1.9   maxv in6_undefer_cksum(struct mbuf *m, size_t hdrlen, int csum_flags)
    195   1.5   matt {
    196   1.6   yamt 	const size_t ip6_plen_offset =
    197   1.6   yamt 	    hdrlen + offsetof(struct ip6_hdr, ip6_plen);
    198   1.6   yamt 	size_t l4hdroff;
    199   1.6   yamt 	size_t l4offset;
    200   1.6   yamt 	uint16_t plen;
    201   1.6   yamt 	uint16_t csum;
    202   1.6   yamt 
    203   1.5   matt 	KASSERT(m->m_flags & M_PKTHDR);
    204   1.5   matt 	KASSERT((m->m_pkthdr.csum_flags & csum_flags) == csum_flags);
    205   1.5   matt 	KASSERT(csum_flags == M_CSUM_UDPv6 || csum_flags == M_CSUM_TCPv6);
    206   1.5   matt 
    207   1.5   matt 	if (__predict_true(hdrlen + sizeof(struct ip6_hdr) <= m->m_len)) {
    208   1.5   matt 		plen = *(uint16_t *)(mtod(m, char *) + ip6_plen_offset);
    209   1.5   matt 	} else {
    210   1.5   matt 		m_copydata(m, ip6_plen_offset, sizeof(plen), &plen);
    211   1.5   matt 	}
    212   1.6   yamt 	plen = ntohs(plen);
    213   1.5   matt 
    214   1.8   maxv 	l4hdroff = M_CSUM_DATA_IPv6_IPHL(m->m_pkthdr.csum_data);
    215   1.6   yamt 	l4offset = hdrlen + l4hdroff;
    216  1.11    rin 	csum = in6_cksum(m, 0, l4offset,
    217  1.11    rin 	    plen - (l4hdroff - sizeof(struct ip6_hdr)));
    218   1.5   matt 
    219   1.5   matt 	if (csum == 0 && (csum_flags & M_CSUM_UDPv6) != 0)
    220   1.5   matt 		csum = 0xffff;
    221   1.5   matt 
    222   1.5   matt 	l4offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data);
    223   1.5   matt 
    224   1.5   matt 	if (__predict_true((l4offset + sizeof(uint16_t)) <= m->m_len)) {
    225   1.5   matt 		*(uint16_t *)(mtod(m, char *) + l4offset) = csum;
    226   1.5   matt 	} else {
    227   1.5   matt 		m_copyback(m, l4offset, sizeof(csum), (void *) &csum);
    228   1.5   matt 	}
    229   1.5   matt 
    230   1.5   matt 	m->m_pkthdr.csum_flags ^= csum_flags;
    231   1.5   matt }
    232   1.9   maxv 
    233   1.9   maxv /*
    234   1.9   maxv  * Compute now in software the TCP/UDP checksum. Cancel the hardware
    235   1.9   maxv  * offloading.
    236   1.9   maxv  */
    237   1.9   maxv void
    238   1.9   maxv in6_undefer_cksum_tcpudp(struct mbuf *m)
    239   1.9   maxv {
    240   1.9   maxv 	uint16_t csum, offset;
    241   1.9   maxv 
    242   1.9   maxv 	KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
    243   1.9   maxv 	KASSERT((~m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
    244   1.9   maxv 	KASSERT((m->m_pkthdr.csum_flags
    245   1.9   maxv 	    & (M_CSUM_UDPv4|M_CSUM_TCPv4|M_CSUM_TSOv4)) == 0);
    246   1.9   maxv 
    247   1.9   maxv 	offset = M_CSUM_DATA_IPv6_IPHL(m->m_pkthdr.csum_data);
    248   1.9   maxv 	csum = in6_cksum(m, 0, offset, m->m_pkthdr.len - offset);
    249   1.9   maxv 	if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv6) != 0) {
    250   1.9   maxv 		csum = 0xffff;
    251   1.9   maxv 	}
    252   1.9   maxv 
    253   1.9   maxv 	offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data);
    254   1.9   maxv 	if ((offset + sizeof(csum)) > m->m_len) {
    255   1.9   maxv 		m_copyback(m, offset, sizeof(csum), &csum);
    256   1.9   maxv 	} else {
    257   1.9   maxv 		*(uint16_t *)(mtod(m, char *) + offset) = csum;
    258   1.9   maxv 	}
    259   1.9   maxv }
    260