Home | History | Annotate | Line # | Download | only in netinet6
in6_offload.c revision 1.7.12.1
      1  1.7.12.1  pgoyette /*	$NetBSD: in6_offload.c,v 1.7.12.1 2018/06/25 07:26:07 pgoyette Exp $	*/
      2       1.1      yamt 
      3       1.1      yamt /*-
      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.7.12.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: in6_offload.c,v 1.7.12.1 2018/06/25 07:26:07 pgoyette 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 struct ip6_tso_output_args {
     47       1.1      yamt 	struct ifnet *ifp;
     48       1.1      yamt 	struct ifnet *origifp;
     49       1.4    dyoung 	const struct sockaddr_in6 *dst;
     50       1.1      yamt 	struct rtentry *rt;
     51       1.1      yamt };
     52       1.1      yamt 
     53       1.1      yamt static int ip6_tso_output_callback(void *, struct mbuf *);
     54       1.1      yamt 
     55       1.1      yamt static int
     56       1.1      yamt ip6_tso_output_callback(void *vp, struct mbuf *m)
     57       1.1      yamt {
     58       1.1      yamt 	struct ip6_tso_output_args *args = vp;
     59       1.1      yamt 
     60       1.7     ozaki 	return ip6_if_output(args->ifp, args->origifp, m, args->dst, args->rt);
     61       1.1      yamt }
     62       1.1      yamt 
     63       1.1      yamt int
     64       1.1      yamt ip6_tso_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
     65       1.4    dyoung     const struct sockaddr_in6 *dst, struct rtentry *rt)
     66       1.1      yamt {
     67       1.1      yamt 	struct ip6_tso_output_args args;
     68       1.1      yamt 
     69       1.1      yamt 	args.ifp = ifp;
     70       1.1      yamt 	args.origifp = origifp;
     71       1.1      yamt 	args.dst = dst;
     72       1.1      yamt 	args.rt = rt;
     73       1.1      yamt 
     74       1.1      yamt 	return tcp6_segment(m, ip6_tso_output_callback, &args);
     75       1.1      yamt }
     76       1.1      yamt 
     77       1.1      yamt /*
     78       1.1      yamt  * tcp6_segment: handle M_CSUM_TSOv6 by software.
     79       1.1      yamt  *
     80       1.1      yamt  * => always consume m.
     81       1.1      yamt  * => call output_func with output_arg for each segments.
     82       1.1      yamt  */
     83       1.1      yamt 
     84       1.1      yamt int
     85       1.1      yamt tcp6_segment(struct mbuf *m, int (*output_func)(void *, struct mbuf *),
     86       1.1      yamt     void *output_arg)
     87       1.1      yamt {
     88       1.1      yamt 	int mss;
     89       1.1      yamt 	int iphlen;
     90       1.1      yamt 	int thlen;
     91       1.1      yamt 	int hlen;
     92       1.1      yamt 	int len;
     93       1.1      yamt 	struct ip6_hdr *iph;
     94       1.1      yamt 	struct tcphdr *th;
     95       1.1      yamt 	uint32_t tcpseq;
     96       1.1      yamt 	struct mbuf *hdr = NULL;
     97       1.1      yamt 	struct mbuf *t;
     98       1.1      yamt 	int error = 0;
     99       1.1      yamt 
    100       1.1      yamt 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    101       1.1      yamt 	KASSERT((m->m_pkthdr.csum_flags & M_CSUM_TSOv6) != 0);
    102       1.1      yamt 
    103       1.1      yamt 	m->m_pkthdr.csum_flags = 0;
    104       1.1      yamt 
    105       1.1      yamt 	len = m->m_pkthdr.len;
    106       1.1      yamt 	KASSERT(len >= sizeof(*iph) + sizeof(*th));
    107       1.1      yamt 
    108       1.1      yamt 	if (m->m_len < sizeof(*iph)) {
    109       1.1      yamt 		m = m_pullup(m, sizeof(*iph));
    110       1.1      yamt 		if (m == NULL) {
    111       1.1      yamt 			error = ENOMEM;
    112       1.1      yamt 			goto quit;
    113       1.1      yamt 		}
    114       1.1      yamt 	}
    115       1.1      yamt 	iph = mtod(m, struct ip6_hdr *);
    116       1.1      yamt 	iphlen = sizeof(*iph);
    117       1.1      yamt 	KASSERT((iph->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION);
    118       1.1      yamt 	KASSERT(iph->ip6_nxt == IPPROTO_TCP);
    119       1.1      yamt 
    120       1.1      yamt 	hlen = iphlen + sizeof(*th);
    121       1.1      yamt 	if (m->m_len < hlen) {
    122       1.1      yamt 		m = m_pullup(m, hlen);
    123       1.1      yamt 		if (m == NULL) {
    124       1.1      yamt 			error = ENOMEM;
    125       1.1      yamt 			goto quit;
    126       1.1      yamt 		}
    127       1.1      yamt 	}
    128       1.1      yamt 	th = (void *)(mtod(m, char *) + iphlen);
    129       1.1      yamt 	tcpseq = ntohl(th->th_seq);
    130       1.1      yamt 	thlen = th->th_off * 4;
    131       1.1      yamt 	hlen = iphlen + thlen;
    132       1.1      yamt 
    133       1.1      yamt 	mss = m->m_pkthdr.segsz;
    134       1.1      yamt 	KASSERT(mss != 0);
    135       1.1      yamt 	KASSERT(len > hlen);
    136       1.1      yamt 
    137       1.1      yamt 	t = m_split(m, hlen, M_NOWAIT);
    138       1.1      yamt 	if (t == NULL) {
    139       1.1      yamt 		error = ENOMEM;
    140       1.1      yamt 		goto quit;
    141       1.1      yamt 	}
    142       1.1      yamt 	hdr = m;
    143       1.1      yamt 	m = t;
    144       1.1      yamt 	len -= hlen;
    145       1.1      yamt 	KASSERT(len % mss == 0);
    146       1.1      yamt 	while (len > 0) {
    147       1.1      yamt 		struct mbuf *n;
    148       1.1      yamt 
    149       1.1      yamt 		n = m_dup(hdr, 0, hlen, M_NOWAIT);
    150       1.1      yamt 		if (n == NULL) {
    151       1.1      yamt 			error = ENOMEM;
    152       1.1      yamt 			goto quit;
    153       1.1      yamt 		}
    154       1.1      yamt 		KASSERT(n->m_len == hlen); /* XXX */
    155       1.1      yamt 
    156       1.1      yamt 		t = m_split(m, mss, M_NOWAIT);
    157       1.1      yamt 		if (t == NULL) {
    158       1.1      yamt 			m_freem(n);
    159       1.1      yamt 			error = ENOMEM;
    160       1.1      yamt 			goto quit;
    161       1.1      yamt 		}
    162       1.1      yamt 		m_cat(n, m);
    163       1.1      yamt 		m = t;
    164       1.1      yamt 
    165       1.1      yamt 		KASSERT(n->m_len >= hlen); /* XXX */
    166       1.1      yamt 
    167       1.1      yamt 		n->m_pkthdr.len = hlen + mss;
    168       1.1      yamt 		iph = mtod(n, struct ip6_hdr *);
    169       1.1      yamt 		KASSERT((iph->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION);
    170       1.1      yamt 		iph->ip6_plen = htons(thlen + mss);
    171       1.1      yamt 		th = (void *)(mtod(n, char *) + iphlen);
    172       1.1      yamt 		th->th_seq = htonl(tcpseq);
    173       1.1      yamt 		th->th_sum = 0;
    174       1.1      yamt 		th->th_sum = in6_cksum(n, IPPROTO_TCP, iphlen, thlen + mss);
    175       1.1      yamt 
    176       1.1      yamt 		error = (*output_func)(output_arg, n);
    177       1.1      yamt 		if (error) {
    178       1.1      yamt 			goto quit;
    179       1.1      yamt 		}
    180       1.1      yamt 
    181       1.1      yamt 		tcpseq += mss;
    182       1.1      yamt 		len -= mss;
    183       1.1      yamt 	}
    184       1.1      yamt 
    185       1.1      yamt quit:
    186       1.1      yamt 	if (hdr != NULL) {
    187       1.1      yamt 		m_freem(hdr);
    188       1.1      yamt 	}
    189       1.1      yamt 	if (m != NULL) {
    190       1.1      yamt 		m_freem(m);
    191       1.1      yamt 	}
    192       1.1      yamt 
    193       1.1      yamt 	return error;
    194       1.1      yamt }
    195       1.5      matt 
    196       1.5      matt void
    197       1.5      matt ip6_undefer_csum(struct mbuf *m, size_t hdrlen, int csum_flags)
    198       1.5      matt {
    199       1.6      yamt 	const size_t ip6_plen_offset =
    200       1.6      yamt 	    hdrlen + offsetof(struct ip6_hdr, ip6_plen);
    201       1.6      yamt 	size_t l4hdroff;
    202       1.6      yamt 	size_t l4offset;
    203       1.6      yamt 	uint16_t plen;
    204       1.6      yamt 	uint16_t csum;
    205       1.6      yamt 
    206       1.5      matt 	KASSERT(m->m_flags & M_PKTHDR);
    207       1.5      matt 	KASSERT((m->m_pkthdr.csum_flags & csum_flags) == csum_flags);
    208       1.5      matt 	KASSERT(csum_flags == M_CSUM_UDPv6 || csum_flags == M_CSUM_TCPv6);
    209       1.5      matt 
    210       1.5      matt 	if (__predict_true(hdrlen + sizeof(struct ip6_hdr) <= m->m_len)) {
    211       1.5      matt 		plen = *(uint16_t *)(mtod(m, char *) + ip6_plen_offset);
    212       1.5      matt 	} else {
    213       1.5      matt 		m_copydata(m, ip6_plen_offset, sizeof(plen), &plen);
    214       1.5      matt 	}
    215       1.6      yamt 	plen = ntohs(plen);
    216       1.5      matt 
    217  1.7.12.1  pgoyette 	l4hdroff = M_CSUM_DATA_IPv6_IPHL(m->m_pkthdr.csum_data);
    218       1.6      yamt 	l4offset = hdrlen + l4hdroff;
    219       1.6      yamt 	csum = in6_cksum(m, 0, l4offset, plen - l4hdroff);
    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