1 1.43 rin /* $NetBSD: kttcp.c,v 1.43 2024/07/05 04:31:50 rin Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /* 4 1.1 thorpej * Copyright (c) 2002 Wasabi Systems, Inc. 5 1.1 thorpej * All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * Written by Frank van der Linden and Jason R. Thorpe for 8 1.1 thorpej * Wasabi Systems, Inc. 9 1.1 thorpej * 10 1.1 thorpej * Redistribution and use in source and binary forms, with or without 11 1.1 thorpej * modification, are permitted provided that the following conditions 12 1.1 thorpej * are met: 13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 14 1.1 thorpej * notice, this list of conditions and the following disclaimer. 15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 17 1.1 thorpej * documentation and/or other materials provided with the distribution. 18 1.1 thorpej * 3. All advertising materials mentioning features or use of this software 19 1.1 thorpej * must display the following acknowledgement: 20 1.1 thorpej * This product includes software developed for the NetBSD Project by 21 1.1 thorpej * Wasabi Systems, Inc. 22 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse 23 1.1 thorpej * or promote products derived from this software without specific prior 24 1.1 thorpej * written permission. 25 1.1 thorpej * 26 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 27 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 30 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE. 37 1.1 thorpej */ 38 1.1 thorpej 39 1.1 thorpej /* 40 1.22 hubertf * kttcp.c -- provides kernel support for testing network testing, 41 1.22 hubertf * see kttcp(4) 42 1.1 thorpej */ 43 1.14 lukem 44 1.14 lukem #include <sys/cdefs.h> 45 1.43 rin __KERNEL_RCSID(0, "$NetBSD: kttcp.c,v 1.43 2024/07/05 04:31:50 rin Exp $"); 46 1.1 thorpej 47 1.1 thorpej #include <sys/param.h> 48 1.1 thorpej #include <sys/types.h> 49 1.1 thorpej #include <sys/ioctl.h> 50 1.1 thorpej #include <sys/file.h> 51 1.1 thorpej #include <sys/filedesc.h> 52 1.1 thorpej #include <sys/conf.h> 53 1.1 thorpej #include <sys/systm.h> 54 1.1 thorpej #include <sys/protosw.h> 55 1.1 thorpej #include <sys/proc.h> 56 1.1 thorpej #include <sys/resourcevar.h> 57 1.1 thorpej #include <sys/signal.h> 58 1.1 thorpej #include <sys/socketvar.h> 59 1.1 thorpej #include <sys/socket.h> 60 1.1 thorpej #include <sys/mbuf.h> 61 1.1 thorpej #include <sys/mount.h> 62 1.1 thorpej #include <sys/syscallargs.h> 63 1.1 thorpej 64 1.1 thorpej #include <dev/kttcpio.h> 65 1.1 thorpej 66 1.38 christos #include "ioconf.h" 67 1.38 christos 68 1.17 christos static int kttcp_send(struct lwp *l, struct kttcp_io_args *); 69 1.17 christos static int kttcp_recv(struct lwp *l, struct kttcp_io_args *); 70 1.1 thorpej static int kttcp_sosend(struct socket *, unsigned long long, 71 1.17 christos unsigned long long *, struct lwp *, int); 72 1.1 thorpej static int kttcp_soreceive(struct socket *, unsigned long long, 73 1.17 christos unsigned long long *, struct lwp *, int *); 74 1.1 thorpej 75 1.5 gehenna dev_type_ioctl(kttcpioctl); 76 1.5 gehenna 77 1.5 gehenna const struct cdevsw kttcp_cdevsw = { 78 1.32 dholland .d_open = nullopen, 79 1.32 dholland .d_close = nullclose, 80 1.32 dholland .d_read = noread, 81 1.32 dholland .d_write = nowrite, 82 1.32 dholland .d_ioctl = kttcpioctl, 83 1.32 dholland .d_stop = nostop, 84 1.32 dholland .d_tty = notty, 85 1.32 dholland .d_poll = nopoll, 86 1.32 dholland .d_mmap = nommap, 87 1.32 dholland .d_kqfilter = nokqfilter, 88 1.35 dholland .d_discard = nodiscard, 89 1.32 dholland .d_flag = D_OTHER 90 1.5 gehenna }; 91 1.1 thorpej 92 1.1 thorpej void 93 1.21 christos kttcpattach(int count) 94 1.1 thorpej { 95 1.1 thorpej /* Do nothing. */ 96 1.1 thorpej } 97 1.1 thorpej 98 1.1 thorpej int 99 1.24 christos kttcpioctl(dev_t dev, u_long cmd, void *data, int flag, 100 1.20 christos struct lwp *l) 101 1.1 thorpej { 102 1.1 thorpej int error; 103 1.1 thorpej 104 1.1 thorpej if ((flag & FWRITE) == 0) 105 1.1 thorpej return EPERM; 106 1.1 thorpej 107 1.1 thorpej switch (cmd) { 108 1.1 thorpej case KTTCP_IO_SEND: 109 1.17 christos error = kttcp_send(l, (struct kttcp_io_args *) data); 110 1.1 thorpej break; 111 1.1 thorpej 112 1.1 thorpej case KTTCP_IO_RECV: 113 1.17 christos error = kttcp_recv(l, (struct kttcp_io_args *) data); 114 1.1 thorpej break; 115 1.1 thorpej 116 1.1 thorpej default: 117 1.1 thorpej return EINVAL; 118 1.1 thorpej } 119 1.1 thorpej 120 1.1 thorpej return error; 121 1.1 thorpej } 122 1.1 thorpej 123 1.1 thorpej static int 124 1.17 christos kttcp_send(struct lwp *l, struct kttcp_io_args *kio) 125 1.1 thorpej { 126 1.26 ad struct socket *so; 127 1.1 thorpej int error; 128 1.1 thorpej struct timeval t0, t1; 129 1.1 thorpej unsigned long long len, done; 130 1.1 thorpej 131 1.1 thorpej if (kio->kio_totalsize >= KTTCP_MAX_XMIT) 132 1.1 thorpej return EINVAL; 133 1.1 thorpej 134 1.26 ad if ((error = fd_getsock(kio->kio_socket, &so)) != 0) 135 1.26 ad return error; 136 1.1 thorpej 137 1.1 thorpej len = kio->kio_totalsize; 138 1.1 thorpej microtime(&t0); 139 1.1 thorpej do { 140 1.26 ad error = kttcp_sosend(so, len, &done, l, 0); 141 1.1 thorpej len -= done; 142 1.1 thorpej } while (error == 0 && len > 0); 143 1.8 briggs 144 1.26 ad fd_putfile(kio->kio_socket); 145 1.8 briggs 146 1.1 thorpej microtime(&t1); 147 1.1 thorpej if (error != 0) 148 1.1 thorpej return error; 149 1.1 thorpej timersub(&t1, &t0, &kio->kio_elapsed); 150 1.1 thorpej 151 1.1 thorpej kio->kio_bytesdone = kio->kio_totalsize - len; 152 1.1 thorpej 153 1.1 thorpej return 0; 154 1.1 thorpej } 155 1.1 thorpej 156 1.1 thorpej static int 157 1.17 christos kttcp_recv(struct lwp *l, struct kttcp_io_args *kio) 158 1.1 thorpej { 159 1.26 ad struct socket *so; 160 1.1 thorpej int error; 161 1.1 thorpej struct timeval t0, t1; 162 1.1 thorpej unsigned long long len, done; 163 1.1 thorpej 164 1.18 mrg done = 0; /* XXX gcc */ 165 1.18 mrg 166 1.1 thorpej if (kio->kio_totalsize > KTTCP_MAX_XMIT) 167 1.1 thorpej return EINVAL; 168 1.1 thorpej 169 1.26 ad if ((error = fd_getsock(kio->kio_socket, &so)) != 0) 170 1.26 ad return error; 171 1.1 thorpej len = kio->kio_totalsize; 172 1.1 thorpej microtime(&t0); 173 1.1 thorpej do { 174 1.26 ad error = kttcp_soreceive(so, len, &done, l, NULL); 175 1.1 thorpej len -= done; 176 1.1 thorpej } while (error == 0 && len > 0 && done > 0); 177 1.8 briggs 178 1.26 ad fd_putfile(kio->kio_socket); 179 1.8 briggs 180 1.1 thorpej microtime(&t1); 181 1.1 thorpej if (error == EPIPE) 182 1.1 thorpej error = 0; 183 1.1 thorpej if (error != 0) 184 1.1 thorpej return error; 185 1.1 thorpej timersub(&t1, &t0, &kio->kio_elapsed); 186 1.1 thorpej 187 1.1 thorpej kio->kio_bytesdone = kio->kio_totalsize - len; 188 1.1 thorpej 189 1.1 thorpej return 0; 190 1.1 thorpej } 191 1.1 thorpej 192 1.1 thorpej #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 193 1.1 thorpej 194 1.1 thorpej /* 195 1.1 thorpej * Slightly changed version of sosend() 196 1.1 thorpej */ 197 1.1 thorpej static int 198 1.1 thorpej kttcp_sosend(struct socket *so, unsigned long long slen, 199 1.17 christos unsigned long long *done, struct lwp *l, int flags) 200 1.1 thorpej { 201 1.1 thorpej struct mbuf **mp, *m, *top; 202 1.1 thorpej long space, len, mlen; 203 1.28 ad int error, dontroute, atomic; 204 1.1 thorpej long long resid; 205 1.1 thorpej 206 1.1 thorpej atomic = sosendallatonce(so); 207 1.1 thorpej resid = slen; 208 1.1 thorpej top = NULL; 209 1.1 thorpej /* 210 1.1 thorpej * In theory resid should be unsigned. 211 1.1 thorpej * However, space must be signed, as it might be less than 0 212 1.1 thorpej * if we over-committed, and we must use a signed comparison 213 1.1 thorpej * of space and resid. On the other hand, a negative resid 214 1.1 thorpej * causes us to loop sending 0-length segments to the protocol. 215 1.1 thorpej */ 216 1.1 thorpej if (resid < 0) { 217 1.1 thorpej error = EINVAL; 218 1.1 thorpej goto out; 219 1.1 thorpej } 220 1.1 thorpej dontroute = 221 1.1 thorpej (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 222 1.1 thorpej (so->so_proto->pr_flags & PR_ATOMIC); 223 1.27 ad l->l_ru.ru_msgsnd++; 224 1.28 ad #define snderr(errno) { error = errno; goto release; } 225 1.28 ad solock(so); 226 1.1 thorpej restart: 227 1.1 thorpej if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0) 228 1.1 thorpej goto out; 229 1.1 thorpej do { 230 1.1 thorpej if (so->so_state & SS_CANTSENDMORE) 231 1.1 thorpej snderr(EPIPE); 232 1.1 thorpej if (so->so_error) { 233 1.1 thorpej error = so->so_error; 234 1.1 thorpej so->so_error = 0; 235 1.1 thorpej goto release; 236 1.1 thorpej } 237 1.1 thorpej if ((so->so_state & SS_ISCONNECTED) == 0) { 238 1.1 thorpej if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 239 1.31 rmind snderr(ENOTCONN); 240 1.31 rmind } else { 241 1.1 thorpej snderr(EDESTADDRREQ); 242 1.31 rmind } 243 1.1 thorpej } 244 1.1 thorpej space = sbspace(&so->so_snd); 245 1.1 thorpej if (flags & MSG_OOB) 246 1.1 thorpej space += 1024; 247 1.1 thorpej if ((atomic && resid > so->so_snd.sb_hiwat)) 248 1.1 thorpej snderr(EMSGSIZE); 249 1.1 thorpej if (space < resid && (atomic || space < so->so_snd.sb_lowat)) { 250 1.29 christos if (so->so_state & SS_NBIO) 251 1.1 thorpej snderr(EWOULDBLOCK); 252 1.2 thorpej SBLASTRECORDCHK(&so->so_rcv, 253 1.2 thorpej "kttcp_soreceive sbwait 1"); 254 1.2 thorpej SBLASTMBUFCHK(&so->so_rcv, 255 1.2 thorpej "kttcp_soreceive sbwait 1"); 256 1.1 thorpej sbunlock(&so->so_snd); 257 1.1 thorpej error = sbwait(&so->so_snd); 258 1.1 thorpej if (error) 259 1.1 thorpej goto out; 260 1.1 thorpej goto restart; 261 1.1 thorpej } 262 1.1 thorpej mp = ⊤ 263 1.1 thorpej do { 264 1.28 ad sounlock(so); 265 1.1 thorpej do { 266 1.1 thorpej if (top == 0) { 267 1.9 matt m = m_gethdr(M_WAIT, MT_DATA); 268 1.1 thorpej mlen = MHLEN; 269 1.1 thorpej m->m_pkthdr.len = 0; 270 1.39 ozaki m_reset_rcvif(m); 271 1.1 thorpej } else { 272 1.9 matt m = m_get(M_WAIT, MT_DATA); 273 1.1 thorpej mlen = MLEN; 274 1.1 thorpej } 275 1.1 thorpej if (resid >= MINCLSIZE && space >= MCLBYTES) { 276 1.9 matt m_clget(m, M_WAIT); 277 1.1 thorpej if ((m->m_flags & M_EXT) == 0) 278 1.1 thorpej goto nopages; 279 1.1 thorpej mlen = MCLBYTES; 280 1.1 thorpej #ifdef MAPPED_MBUFS 281 1.1 thorpej len = lmin(MCLBYTES, resid); 282 1.1 thorpej #else 283 1.1 thorpej if (atomic && top == 0) { 284 1.1 thorpej len = lmin(MCLBYTES - max_hdr, 285 1.1 thorpej resid); 286 1.1 thorpej m->m_data += max_hdr; 287 1.1 thorpej } else 288 1.1 thorpej len = lmin(MCLBYTES, resid); 289 1.1 thorpej #endif 290 1.1 thorpej space -= len; 291 1.1 thorpej } else { 292 1.1 thorpej nopages: 293 1.1 thorpej len = lmin(lmin(mlen, resid), space); 294 1.1 thorpej space -= len; 295 1.1 thorpej /* 296 1.1 thorpej * For datagram protocols, leave room 297 1.1 thorpej * for protocol headers in first mbuf. 298 1.1 thorpej */ 299 1.1 thorpej if (atomic && top == 0 && len < mlen) 300 1.42 maxv m_align(m, len); 301 1.1 thorpej } 302 1.1 thorpej resid -= len; 303 1.1 thorpej m->m_len = len; 304 1.1 thorpej *mp = m; 305 1.1 thorpej top->m_pkthdr.len += len; 306 1.1 thorpej if (error) 307 1.1 thorpej goto release; 308 1.1 thorpej mp = &m->m_next; 309 1.1 thorpej if (resid <= 0) { 310 1.1 thorpej if (flags & MSG_EOR) 311 1.1 thorpej top->m_flags |= M_EOR; 312 1.1 thorpej break; 313 1.1 thorpej } 314 1.1 thorpej } while (space > 0 && atomic); 315 1.28 ad solock(so); 316 1.1 thorpej 317 1.1 thorpej if (so->so_state & SS_CANTSENDMORE) 318 1.1 thorpej snderr(EPIPE); 319 1.1 thorpej if (dontroute) 320 1.1 thorpej so->so_options |= SO_DONTROUTE; 321 1.1 thorpej if (resid > 0) 322 1.1 thorpej so->so_state |= SS_MORETOCOME; 323 1.34 rtr if (flags & MSG_OOB) 324 1.34 rtr error = (*so->so_proto->pr_usrreqs->pr_sendoob)(so, 325 1.34 rtr top, NULL); 326 1.34 rtr else 327 1.36 rtr error = (*so->so_proto->pr_usrreqs->pr_send)(so, 328 1.36 rtr top, NULL, NULL, l); 329 1.1 thorpej if (dontroute) 330 1.1 thorpej so->so_options &= ~SO_DONTROUTE; 331 1.1 thorpej if (resid > 0) 332 1.1 thorpej so->so_state &= ~SS_MORETOCOME; 333 1.1 thorpej top = 0; 334 1.1 thorpej mp = ⊤ 335 1.1 thorpej if (error) 336 1.1 thorpej goto release; 337 1.1 thorpej } while (resid && space > 0); 338 1.1 thorpej } while (resid); 339 1.1 thorpej 340 1.1 thorpej release: 341 1.1 thorpej sbunlock(&so->so_snd); 342 1.1 thorpej out: 343 1.28 ad sounlock(so); 344 1.43 rin m_freem(top); 345 1.1 thorpej *done = slen - resid; 346 1.1 thorpej #if 0 347 1.1 thorpej printf("sosend: error %d slen %llu resid %lld\n", error, slen, resid); 348 1.1 thorpej #endif 349 1.1 thorpej return (error); 350 1.1 thorpej } 351 1.1 thorpej 352 1.1 thorpej static int 353 1.1 thorpej kttcp_soreceive(struct socket *so, unsigned long long slen, 354 1.21 christos unsigned long long *done, struct lwp *l, int *flagsp) 355 1.1 thorpej { 356 1.1 thorpej struct mbuf *m, **mp; 357 1.28 ad int flags, len, error, offset, moff, type; 358 1.1 thorpej long long orig_resid, resid; 359 1.15 matt const struct protosw *pr; 360 1.1 thorpej struct mbuf *nextrecord; 361 1.1 thorpej 362 1.1 thorpej pr = so->so_proto; 363 1.1 thorpej mp = NULL; 364 1.1 thorpej type = 0; 365 1.1 thorpej resid = orig_resid = slen; 366 1.1 thorpej if (flagsp) 367 1.1 thorpej flags = *flagsp &~ MSG_EOR; 368 1.1 thorpej else 369 1.1 thorpej flags = 0; 370 1.1 thorpej if (flags & MSG_OOB) { 371 1.1 thorpej m = m_get(M_WAIT, MT_DATA); 372 1.28 ad solock(so); 373 1.34 rtr error = (*pr->pr_usrreqs->pr_recvoob)(so, m, flags & MSG_PEEK); 374 1.28 ad sounlock(so); 375 1.1 thorpej if (error) 376 1.1 thorpej goto bad; 377 1.1 thorpej do { 378 1.41 riastrad resid -= uimin(resid, m->m_len); 379 1.1 thorpej m = m_free(m); 380 1.1 thorpej } while (resid && error == 0 && m); 381 1.1 thorpej bad: 382 1.43 rin m_freem(m); 383 1.1 thorpej return (error); 384 1.1 thorpej } 385 1.1 thorpej if (mp) 386 1.12 simonb *mp = NULL; 387 1.28 ad solock(so); 388 1.1 thorpej restart: 389 1.1 thorpej if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) 390 1.1 thorpej return (error); 391 1.1 thorpej m = so->so_rcv.sb_mb; 392 1.1 thorpej /* 393 1.1 thorpej * If we have less data than requested, block awaiting more 394 1.1 thorpej * (subject to any timeout) if: 395 1.1 thorpej * 1. the current count is less than the low water mark, 396 1.1 thorpej * 2. MSG_WAITALL is set, and it is possible to do the entire 397 1.1 thorpej * receive operation at once if we block (resid <= hiwat), or 398 1.1 thorpej * 3. MSG_DONTWAIT is not set. 399 1.1 thorpej * If MSG_WAITALL is set but resid is larger than the receive buffer, 400 1.1 thorpej * we have to do the receive in sections, and thus risk returning 401 1.1 thorpej * a short count if a timeout or signal occurs after we start. 402 1.1 thorpej */ 403 1.13 fvdl if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && 404 1.1 thorpej so->so_rcv.sb_cc < resid) && 405 1.1 thorpej (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 406 1.1 thorpej ((flags & MSG_WAITALL) && resid <= so->so_rcv.sb_hiwat)) && 407 1.13 fvdl m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) { 408 1.1 thorpej #ifdef DIAGNOSTIC 409 1.13 fvdl if (m == NULL && so->so_rcv.sb_cc) 410 1.1 thorpej panic("receive 1"); 411 1.1 thorpej #endif 412 1.1 thorpej if (so->so_error) { 413 1.1 thorpej if (m) 414 1.1 thorpej goto dontblock; 415 1.1 thorpej error = so->so_error; 416 1.1 thorpej if ((flags & MSG_PEEK) == 0) 417 1.1 thorpej so->so_error = 0; 418 1.1 thorpej goto release; 419 1.1 thorpej } 420 1.1 thorpej if (so->so_state & SS_CANTRCVMORE) { 421 1.1 thorpej if (m) 422 1.1 thorpej goto dontblock; 423 1.1 thorpej else 424 1.1 thorpej goto release; 425 1.1 thorpej } 426 1.1 thorpej for (; m; m = m->m_next) 427 1.1 thorpej if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 428 1.1 thorpej m = so->so_rcv.sb_mb; 429 1.1 thorpej goto dontblock; 430 1.1 thorpej } 431 1.1 thorpej if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 432 1.1 thorpej (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 433 1.1 thorpej error = ENOTCONN; 434 1.1 thorpej goto release; 435 1.1 thorpej } 436 1.1 thorpej if (resid == 0) 437 1.1 thorpej goto release; 438 1.30 jakllsch if ((so->so_state & SS_NBIO) || 439 1.30 jakllsch (flags & (MSG_DONTWAIT|MSG_NBIO))) { 440 1.1 thorpej error = EWOULDBLOCK; 441 1.1 thorpej goto release; 442 1.1 thorpej } 443 1.1 thorpej sbunlock(&so->so_rcv); 444 1.1 thorpej error = sbwait(&so->so_rcv); 445 1.28 ad if (error) { 446 1.28 ad sounlock(so); 447 1.1 thorpej return (error); 448 1.28 ad } 449 1.1 thorpej goto restart; 450 1.1 thorpej } 451 1.1 thorpej dontblock: 452 1.2 thorpej /* 453 1.2 thorpej * On entry here, m points to the first record of the socket buffer. 454 1.2 thorpej * While we process the initial mbufs containing address and control 455 1.2 thorpej * info, we save a copy of m->m_nextpkt into nextrecord. 456 1.2 thorpej */ 457 1.1 thorpej #ifdef notyet /* XXXX */ 458 1.17 christos if (uio->uio_lwp) 459 1.27 ad uio->uio_lwp->l_ru.ru_msgrcv++; 460 1.1 thorpej #endif 461 1.2 thorpej KASSERT(m == so->so_rcv.sb_mb); 462 1.2 thorpej SBLASTRECORDCHK(&so->so_rcv, "kttcp_soreceive 1"); 463 1.2 thorpej SBLASTMBUFCHK(&so->so_rcv, "kttcp_soreceive 1"); 464 1.1 thorpej nextrecord = m->m_nextpkt; 465 1.1 thorpej if (pr->pr_flags & PR_ADDR) { 466 1.1 thorpej #ifdef DIAGNOSTIC 467 1.1 thorpej if (m->m_type != MT_SONAME) 468 1.1 thorpej panic("receive 1a"); 469 1.1 thorpej #endif 470 1.1 thorpej orig_resid = 0; 471 1.1 thorpej if (flags & MSG_PEEK) { 472 1.1 thorpej m = m->m_next; 473 1.1 thorpej } else { 474 1.1 thorpej sbfree(&so->so_rcv, m); 475 1.40 christos m = so->so_rcv.sb_mb = m_free(m); 476 1.1 thorpej } 477 1.1 thorpej } 478 1.1 thorpej while (m && m->m_type == MT_CONTROL && error == 0) { 479 1.1 thorpej if (flags & MSG_PEEK) { 480 1.1 thorpej m = m->m_next; 481 1.1 thorpej } else { 482 1.1 thorpej sbfree(&so->so_rcv, m); 483 1.40 christos m = so->so_rcv.sb_mb = m_free(m); 484 1.1 thorpej } 485 1.1 thorpej } 486 1.2 thorpej 487 1.2 thorpej /* 488 1.2 thorpej * If m is non-NULL, we have some data to read. From now on, 489 1.2 thorpej * make sure to keep sb_lastrecord consistent when working on 490 1.2 thorpej * the last packet on the chain (nextrecord == NULL) and we 491 1.2 thorpej * change m->m_nextpkt. 492 1.2 thorpej */ 493 1.1 thorpej if (m) { 494 1.2 thorpej if ((flags & MSG_PEEK) == 0) { 495 1.1 thorpej m->m_nextpkt = nextrecord; 496 1.2 thorpej /* 497 1.2 thorpej * If nextrecord == NULL (this is a single chain), 498 1.2 thorpej * then sb_lastrecord may not be valid here if m 499 1.2 thorpej * was changed earlier. 500 1.2 thorpej */ 501 1.2 thorpej if (nextrecord == NULL) { 502 1.2 thorpej KASSERT(so->so_rcv.sb_mb == m); 503 1.2 thorpej so->so_rcv.sb_lastrecord = m; 504 1.2 thorpej } 505 1.2 thorpej } 506 1.1 thorpej type = m->m_type; 507 1.1 thorpej if (type == MT_OOBDATA) 508 1.1 thorpej flags |= MSG_OOB; 509 1.2 thorpej } else { 510 1.2 thorpej if ((flags & MSG_PEEK) == 0) { 511 1.2 thorpej KASSERT(so->so_rcv.sb_mb == m); 512 1.2 thorpej so->so_rcv.sb_mb = nextrecord; 513 1.4 thorpej SB_EMPTY_FIXUP(&so->so_rcv); 514 1.2 thorpej } 515 1.1 thorpej } 516 1.2 thorpej SBLASTRECORDCHK(&so->so_rcv, "kttcp_soreceive 2"); 517 1.2 thorpej SBLASTMBUFCHK(&so->so_rcv, "kttcp_soreceive 2"); 518 1.2 thorpej 519 1.1 thorpej moff = 0; 520 1.1 thorpej offset = 0; 521 1.1 thorpej while (m && resid > 0 && error == 0) { 522 1.1 thorpej if (m->m_type == MT_OOBDATA) { 523 1.1 thorpej if (type != MT_OOBDATA) 524 1.1 thorpej break; 525 1.1 thorpej } else if (type == MT_OOBDATA) 526 1.1 thorpej break; 527 1.1 thorpej #ifdef DIAGNOSTIC 528 1.1 thorpej else if (m->m_type != MT_DATA && m->m_type != MT_HEADER) 529 1.1 thorpej panic("receive 3"); 530 1.1 thorpej #endif 531 1.1 thorpej so->so_state &= ~SS_RCVATMARK; 532 1.1 thorpej len = resid; 533 1.1 thorpej if (so->so_oobmark && len > so->so_oobmark - offset) 534 1.1 thorpej len = so->so_oobmark - offset; 535 1.1 thorpej if (len > m->m_len - moff) 536 1.1 thorpej len = m->m_len - moff; 537 1.1 thorpej /* 538 1.1 thorpej * If mp is set, just pass back the mbufs. 539 1.1 thorpej * Otherwise copy them out via the uio, then free. 540 1.1 thorpej * Sockbuf must be consistent here (points to current mbuf, 541 1.1 thorpej * it points to next record) when we drop priority; 542 1.1 thorpej * we must note any additions to the sockbuf when we 543 1.1 thorpej * block interrupts again. 544 1.1 thorpej */ 545 1.1 thorpej resid -= len; 546 1.1 thorpej if (len == m->m_len - moff) { 547 1.1 thorpej if (m->m_flags & M_EOR) 548 1.1 thorpej flags |= MSG_EOR; 549 1.1 thorpej if (flags & MSG_PEEK) { 550 1.1 thorpej m = m->m_next; 551 1.1 thorpej moff = 0; 552 1.1 thorpej } else { 553 1.1 thorpej nextrecord = m->m_nextpkt; 554 1.1 thorpej sbfree(&so->so_rcv, m); 555 1.1 thorpej if (mp) { 556 1.1 thorpej *mp = m; 557 1.1 thorpej mp = &m->m_next; 558 1.1 thorpej so->so_rcv.sb_mb = m = m->m_next; 559 1.12 simonb *mp = NULL; 560 1.1 thorpej } else { 561 1.40 christos m = so->so_rcv.sb_mb = m_free(m); 562 1.1 thorpej } 563 1.2 thorpej /* 564 1.2 thorpej * If m != NULL, we also know that 565 1.2 thorpej * so->so_rcv.sb_mb != NULL. 566 1.2 thorpej */ 567 1.2 thorpej KASSERT(so->so_rcv.sb_mb == m); 568 1.2 thorpej if (m) { 569 1.1 thorpej m->m_nextpkt = nextrecord; 570 1.2 thorpej if (nextrecord == NULL) 571 1.2 thorpej so->so_rcv.sb_lastrecord = m; 572 1.2 thorpej } else { 573 1.2 thorpej so->so_rcv.sb_mb = nextrecord; 574 1.4 thorpej SB_EMPTY_FIXUP(&so->so_rcv); 575 1.2 thorpej } 576 1.2 thorpej SBLASTRECORDCHK(&so->so_rcv, 577 1.2 thorpej "kttcp_soreceive 3"); 578 1.2 thorpej SBLASTMBUFCHK(&so->so_rcv, 579 1.2 thorpej "kttcp_soreceive 3"); 580 1.1 thorpej } 581 1.1 thorpej } else { 582 1.1 thorpej if (flags & MSG_PEEK) 583 1.1 thorpej moff += len; 584 1.1 thorpej else { 585 1.28 ad if (mp) { 586 1.28 ad sounlock(so); 587 1.1 thorpej *mp = m_copym(m, 0, len, M_WAIT); 588 1.28 ad solock(so); 589 1.28 ad } 590 1.1 thorpej m->m_data += len; 591 1.1 thorpej m->m_len -= len; 592 1.1 thorpej so->so_rcv.sb_cc -= len; 593 1.1 thorpej } 594 1.1 thorpej } 595 1.1 thorpej if (so->so_oobmark) { 596 1.1 thorpej if ((flags & MSG_PEEK) == 0) { 597 1.1 thorpej so->so_oobmark -= len; 598 1.1 thorpej if (so->so_oobmark == 0) { 599 1.1 thorpej so->so_state |= SS_RCVATMARK; 600 1.1 thorpej break; 601 1.1 thorpej } 602 1.1 thorpej } else { 603 1.1 thorpej offset += len; 604 1.1 thorpej if (offset == so->so_oobmark) 605 1.1 thorpej break; 606 1.1 thorpej } 607 1.1 thorpej } 608 1.1 thorpej if (flags & MSG_EOR) 609 1.1 thorpej break; 610 1.1 thorpej /* 611 1.1 thorpej * If the MSG_WAITALL flag is set (for non-atomic socket), 612 1.1 thorpej * we must not quit until "uio->uio_resid == 0" or an error 613 1.1 thorpej * termination. If a signal/timeout occurs, return 614 1.1 thorpej * with a short count but without error. 615 1.1 thorpej * Keep sockbuf locked against other readers. 616 1.1 thorpej */ 617 1.13 fvdl while (flags & MSG_WAITALL && m == NULL && resid > 0 && 618 1.1 thorpej !sosendallatonce(so) && !nextrecord) { 619 1.1 thorpej if (so->so_error || so->so_state & SS_CANTRCVMORE) 620 1.1 thorpej break; 621 1.3 thorpej /* 622 1.3 thorpej * If we are peeking and the socket receive buffer is 623 1.3 thorpej * full, stop since we can't get more data to peek at. 624 1.3 thorpej */ 625 1.3 thorpej if ((flags & MSG_PEEK) && sbspace(&so->so_rcv) <= 0) 626 1.3 thorpej break; 627 1.3 thorpej /* 628 1.3 thorpej * If we've drained the socket buffer, tell the 629 1.3 thorpej * protocol in case it needs to do something to 630 1.3 thorpej * get it filled again. 631 1.3 thorpej */ 632 1.33 rmind if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb) { 633 1.37 rtr (*pr->pr_usrreqs->pr_rcvd)(so, flags, l); 634 1.33 rmind } 635 1.2 thorpej SBLASTRECORDCHK(&so->so_rcv, 636 1.2 thorpej "kttcp_soreceive sbwait 2"); 637 1.2 thorpej SBLASTMBUFCHK(&so->so_rcv, 638 1.2 thorpej "kttcp_soreceive sbwait 2"); 639 1.1 thorpej error = sbwait(&so->so_rcv); 640 1.1 thorpej if (error) { 641 1.1 thorpej sbunlock(&so->so_rcv); 642 1.28 ad sounlock(so); 643 1.1 thorpej return (0); 644 1.1 thorpej } 645 1.1 thorpej if ((m = so->so_rcv.sb_mb) != NULL) 646 1.1 thorpej nextrecord = m->m_nextpkt; 647 1.1 thorpej } 648 1.1 thorpej } 649 1.1 thorpej 650 1.1 thorpej if (m && pr->pr_flags & PR_ATOMIC) { 651 1.1 thorpej flags |= MSG_TRUNC; 652 1.1 thorpej if ((flags & MSG_PEEK) == 0) 653 1.1 thorpej (void) sbdroprecord(&so->so_rcv); 654 1.1 thorpej } 655 1.1 thorpej if ((flags & MSG_PEEK) == 0) { 656 1.13 fvdl if (m == NULL) { 657 1.2 thorpej /* 658 1.4 thorpej * First part is an SB_EMPTY_FIXUP(). Second part 659 1.2 thorpej * makes sure sb_lastrecord is up-to-date if 660 1.2 thorpej * there is still data in the socket buffer. 661 1.2 thorpej */ 662 1.1 thorpej so->so_rcv.sb_mb = nextrecord; 663 1.2 thorpej if (so->so_rcv.sb_mb == NULL) { 664 1.2 thorpej so->so_rcv.sb_mbtail = NULL; 665 1.2 thorpej so->so_rcv.sb_lastrecord = NULL; 666 1.2 thorpej } else if (nextrecord->m_nextpkt == NULL) 667 1.2 thorpej so->so_rcv.sb_lastrecord = nextrecord; 668 1.2 thorpej } 669 1.2 thorpej SBLASTRECORDCHK(&so->so_rcv, "kttcp_soreceive 4"); 670 1.2 thorpej SBLASTMBUFCHK(&so->so_rcv, "kttcp_soreceive 4"); 671 1.33 rmind if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) { 672 1.37 rtr (*pr->pr_usrreqs->pr_rcvd)(so, flags, l); 673 1.33 rmind } 674 1.1 thorpej } 675 1.1 thorpej if (orig_resid == resid && orig_resid && 676 1.1 thorpej (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { 677 1.1 thorpej sbunlock(&so->so_rcv); 678 1.1 thorpej goto restart; 679 1.1 thorpej } 680 1.16 perry 681 1.1 thorpej if (flagsp) 682 1.1 thorpej *flagsp |= flags; 683 1.1 thorpej release: 684 1.1 thorpej sbunlock(&so->so_rcv); 685 1.28 ad sounlock(so); 686 1.1 thorpej *done = slen - resid; 687 1.1 thorpej #if 0 688 1.1 thorpej printf("soreceive: error %d slen %llu resid %lld\n", error, slen, resid); 689 1.1 thorpej #endif 690 1.1 thorpej return (error); 691 1.1 thorpej } 692