| 1 | /* $NetBSD: in6_offload.c,v 1.6 2011/04/25 22:07:57 yamt 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.6 2011/04/25 22:07:57 yamt 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/nd6.h> |
| 43 | #include <netinet6/in6_offload.h> |
| 44 | |
| 45 | struct ip6_tso_output_args { |
| 46 | struct ifnet *ifp; |
| 47 | struct ifnet *origifp; |
| 48 | const struct sockaddr_in6 *dst; |
| 49 | struct rtentry *rt; |
| 50 | }; |
| 51 | |
| 52 | static int ip6_tso_output_callback(void *, struct mbuf *); |
| 53 | |
| 54 | static int |
| 55 | ip6_tso_output_callback(void *vp, struct mbuf *m) |
| 56 | { |
| 57 | struct ip6_tso_output_args *args = vp; |
| 58 | |
| 59 | return nd6_output(args->ifp, args->origifp, m, args->dst, args->rt); |
| 60 | } |
| 61 | |
| 62 | int |
| 63 | ip6_tso_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m, |
| 64 | const struct sockaddr_in6 *dst, struct rtentry *rt) |
| 65 | { |
| 66 | struct ip6_tso_output_args args; |
| 67 | |
| 68 | args.ifp = ifp; |
| 69 | args.origifp = origifp; |
| 70 | args.dst = dst; |
| 71 | args.rt = rt; |
| 72 | |
| 73 | return tcp6_segment(m, ip6_tso_output_callback, &args); |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * tcp6_segment: handle M_CSUM_TSOv6 by software. |
| 78 | * |
| 79 | * => always consume m. |
| 80 | * => call output_func with output_arg for each segments. |
| 81 | */ |
| 82 | |
| 83 | int |
| 84 | tcp6_segment(struct mbuf *m, int (*output_func)(void *, struct mbuf *), |
| 85 | void *output_arg) |
| 86 | { |
| 87 | int mss; |
| 88 | int iphlen; |
| 89 | int thlen; |
| 90 | int hlen; |
| 91 | int len; |
| 92 | struct ip6_hdr *iph; |
| 93 | struct tcphdr *th; |
| 94 | uint32_t tcpseq; |
| 95 | struct mbuf *hdr = NULL; |
| 96 | struct mbuf *t; |
| 97 | int error = 0; |
| 98 | |
| 99 | KASSERT((m->m_flags & M_PKTHDR) != 0); |
| 100 | KASSERT((m->m_pkthdr.csum_flags & M_CSUM_TSOv6) != 0); |
| 101 | |
| 102 | m->m_pkthdr.csum_flags = 0; |
| 103 | |
| 104 | len = m->m_pkthdr.len; |
| 105 | KASSERT(len >= sizeof(*iph) + sizeof(*th)); |
| 106 | |
| 107 | if (m->m_len < sizeof(*iph)) { |
| 108 | m = m_pullup(m, sizeof(*iph)); |
| 109 | if (m == NULL) { |
| 110 | error = ENOMEM; |
| 111 | goto quit; |
| 112 | } |
| 113 | } |
| 114 | iph = mtod(m, struct ip6_hdr *); |
| 115 | iphlen = sizeof(*iph); |
| 116 | KASSERT((iph->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION); |
| 117 | KASSERT(iph->ip6_nxt == IPPROTO_TCP); |
| 118 | |
| 119 | hlen = iphlen + sizeof(*th); |
| 120 | if (m->m_len < hlen) { |
| 121 | m = m_pullup(m, hlen); |
| 122 | if (m == NULL) { |
| 123 | error = ENOMEM; |
| 124 | goto quit; |
| 125 | } |
| 126 | } |
| 127 | th = (void *)(mtod(m, char *) + iphlen); |
| 128 | tcpseq = ntohl(th->th_seq); |
| 129 | thlen = th->th_off * 4; |
| 130 | hlen = iphlen + thlen; |
| 131 | |
| 132 | mss = m->m_pkthdr.segsz; |
| 133 | KASSERT(mss != 0); |
| 134 | KASSERT(len > hlen); |
| 135 | |
| 136 | t = m_split(m, hlen, M_NOWAIT); |
| 137 | if (t == NULL) { |
| 138 | error = ENOMEM; |
| 139 | goto quit; |
| 140 | } |
| 141 | hdr = m; |
| 142 | m = t; |
| 143 | len -= hlen; |
| 144 | KASSERT(len % mss == 0); |
| 145 | while (len > 0) { |
| 146 | struct mbuf *n; |
| 147 | |
| 148 | n = m_dup(hdr, 0, hlen, M_NOWAIT); |
| 149 | if (n == NULL) { |
| 150 | error = ENOMEM; |
| 151 | goto quit; |
| 152 | } |
| 153 | KASSERT(n->m_len == hlen); /* XXX */ |
| 154 | |
| 155 | t = m_split(m, mss, M_NOWAIT); |
| 156 | if (t == NULL) { |
| 157 | m_freem(n); |
| 158 | error = ENOMEM; |
| 159 | goto quit; |
| 160 | } |
| 161 | m_cat(n, m); |
| 162 | m = t; |
| 163 | |
| 164 | KASSERT(n->m_len >= hlen); /* XXX */ |
| 165 | |
| 166 | n->m_pkthdr.len = hlen + mss; |
| 167 | iph = mtod(n, struct ip6_hdr *); |
| 168 | KASSERT((iph->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION); |
| 169 | iph->ip6_plen = htons(thlen + mss); |
| 170 | th = (void *)(mtod(n, char *) + iphlen); |
| 171 | th->th_seq = htonl(tcpseq); |
| 172 | th->th_sum = 0; |
| 173 | th->th_sum = in6_cksum(n, IPPROTO_TCP, iphlen, thlen + mss); |
| 174 | |
| 175 | error = (*output_func)(output_arg, n); |
| 176 | if (error) { |
| 177 | goto quit; |
| 178 | } |
| 179 | |
| 180 | tcpseq += mss; |
| 181 | len -= mss; |
| 182 | } |
| 183 | |
| 184 | quit: |
| 185 | if (hdr != NULL) { |
| 186 | m_freem(hdr); |
| 187 | } |
| 188 | if (m != NULL) { |
| 189 | m_freem(m); |
| 190 | } |
| 191 | |
| 192 | return error; |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | ip6_undefer_csum(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_HL(m->m_pkthdr.csum_data); |
| 217 | l4offset = hdrlen + l4hdroff; |
| 218 | csum = in6_cksum(m, 0, l4offset, plen - l4hdroff); |
| 219 | |
| 220 | if (csum == 0 && (csum_flags & M_CSUM_UDPv6) != 0) |
| 221 | csum = 0xffff; |
| 222 | |
| 223 | l4offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data); |
| 224 | |
| 225 | if (__predict_true((l4offset + sizeof(uint16_t)) <= m->m_len)) { |
| 226 | *(uint16_t *)(mtod(m, char *) + l4offset) = csum; |
| 227 | } else { |
| 228 | m_copyback(m, l4offset, sizeof(csum), (void *) &csum); |
| 229 | } |
| 230 | |
| 231 | m->m_pkthdr.csum_flags ^= csum_flags; |
| 232 | } |
| 233 | |