Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
7c4ec3aa | 2 | * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. |
5b519e94 | 3 | * All rights reserved. |
da7c5cc6 | 4 | * |
dbf0c423 | 5 | * %sccs.include.redist.c% |
5b519e94 | 6 | * |
440c48bf | 7 | * @(#)uipc_socket.c 7.31 (Berkeley) %G% |
da7c5cc6 | 8 | */ |
ce9d8eb4 | 9 | |
94368568 | 10 | #include "param.h" |
94368568 JB |
11 | #include "proc.h" |
12 | #include "file.h" | |
2557c1fc | 13 | #include "malloc.h" |
94368568 | 14 | #include "mbuf.h" |
94368568 | 15 | #include "domain.h" |
fc2cae0b | 16 | #include "kernel.h" |
94368568 JB |
17 | #include "protosw.h" |
18 | #include "socket.h" | |
19 | #include "socketvar.h" | |
dff5c020 | 20 | #include "resourcevar.h" |
ce9d8eb4 | 21 | |
ce9d8eb4 | 22 | /* |
cf012934 BJ |
23 | * Socket operation routines. |
24 | * These routines are called by the routines in | |
25 | * sys_socket.c or from a system process, and | |
26 | * implement the semantics of socket operations by | |
27 | * switching out to the protocol specific routines. | |
ce9d8eb4 | 28 | */ |
a8d3bf7f | 29 | /*ARGSUSED*/ |
98422daa | 30 | socreate(dom, aso, type, proto) |
ce9d8eb4 | 31 | struct socket **aso; |
88a7a62a SL |
32 | register int type; |
33 | int proto; | |
ce9d8eb4 | 34 | { |
dff5c020 | 35 | struct proc *p = curproc; /* XXX */ |
ce9d8eb4 BJ |
36 | register struct protosw *prp; |
37 | register struct socket *so; | |
88a7a62a | 38 | register int error; |
cc15ab5d | 39 | |
cc15ab5d | 40 | if (proto) |
8c0650b0 | 41 | prp = pffindproto(dom, proto, type); |
cc15ab5d | 42 | else |
4f083fd7 | 43 | prp = pffindtype(dom, type); |
cc15ab5d BJ |
44 | if (prp == 0) |
45 | return (EPROTONOSUPPORT); | |
cf012934 BJ |
46 | if (prp->pr_type != type) |
47 | return (EPROTOTYPE); | |
a2aebb63 KS |
48 | MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_WAIT); |
49 | bzero((caddr_t)so, sizeof(*so)); | |
4f083fd7 | 50 | so->so_type = type; |
dff5c020 | 51 | if (p->p_ucred->cr_uid == 0) |
62364f0e | 52 | so->so_state = SS_PRIV; |
ce9d8eb4 | 53 | so->so_proto = prp; |
88a7a62a SL |
54 | error = |
55 | (*prp->pr_usrreq)(so, PRU_ATTACH, | |
8c0650b0 | 56 | (struct mbuf *)0, (struct mbuf *)proto, (struct mbuf *)0); |
b91acce4 | 57 | if (error) { |
90aaea96 | 58 | so->so_state |= SS_NOFDREF; |
de48daf3 | 59 | sofree(so); |
cc15ab5d | 60 | return (error); |
ce9d8eb4 BJ |
61 | } |
62 | *aso = so; | |
63 | return (0); | |
64 | } | |
65 | ||
98422daa | 66 | sobind(so, nam) |
cf012934 BJ |
67 | struct socket *so; |
68 | struct mbuf *nam; | |
cf012934 BJ |
69 | { |
70 | int s = splnet(); | |
71 | int error; | |
72 | ||
7c4ec3aa MK |
73 | error = |
74 | (*so->so_proto->pr_usrreq)(so, PRU_BIND, | |
88a7a62a | 75 | (struct mbuf *)0, nam, (struct mbuf *)0); |
cf012934 BJ |
76 | splx(s); |
77 | return (error); | |
78 | } | |
79 | ||
80 | solisten(so, backlog) | |
88a7a62a | 81 | register struct socket *so; |
cf012934 BJ |
82 | int backlog; |
83 | { | |
88a7a62a | 84 | int s = splnet(), error; |
cf012934 | 85 | |
88a7a62a SL |
86 | error = |
87 | (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, | |
88 | (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); | |
cf012934 BJ |
89 | if (error) { |
90 | splx(s); | |
91 | return (error); | |
92 | } | |
629e51da | 93 | if (so->so_q == 0) |
cf012934 | 94 | so->so_options |= SO_ACCEPTCONN; |
cf012934 BJ |
95 | if (backlog < 0) |
96 | backlog = 0; | |
2557c1fc | 97 | so->so_qlimit = min(backlog, SOMAXCONN); |
9e87be97 | 98 | splx(s); |
cf012934 BJ |
99 | return (0); |
100 | } | |
101 | ||
ae921915 | 102 | sofree(so) |
88a7a62a | 103 | register struct socket *so; |
ae921915 BJ |
104 | { |
105 | ||
bb73a14e MK |
106 | if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) |
107 | return; | |
90aaea96 BJ |
108 | if (so->so_head) { |
109 | if (!soqremque(so, 0) && !soqremque(so, 1)) | |
110 | panic("sofree dq"); | |
111 | so->so_head = 0; | |
112 | } | |
4ad99bae | 113 | sbrelease(&so->so_snd); |
88a7a62a | 114 | sorflush(so); |
a2aebb63 | 115 | FREE(so, M_SOCKET); |
ae921915 BJ |
116 | } |
117 | ||
92a533e6 | 118 | /* |
cc15ab5d BJ |
119 | * Close a socket on last file table reference removal. |
120 | * Initiate disconnect if connected. | |
121 | * Free socket when disconnect complete. | |
92a533e6 | 122 | */ |
88a7a62a | 123 | soclose(so) |
92a533e6 | 124 | register struct socket *so; |
92a533e6 | 125 | { |
cc15ab5d | 126 | int s = splnet(); /* conservative */ |
e58562f2 | 127 | int error = 0; |
cc15ab5d | 128 | |
90aaea96 | 129 | if (so->so_options & SO_ACCEPTCONN) { |
629e51da | 130 | while (so->so_q0) |
26225f25 | 131 | (void) soabort(so->so_q0); |
629e51da | 132 | while (so->so_q) |
26225f25 | 133 | (void) soabort(so->so_q); |
90aaea96 | 134 | } |
cc15ab5d BJ |
135 | if (so->so_pcb == 0) |
136 | goto discard; | |
137 | if (so->so_state & SS_ISCONNECTED) { | |
138 | if ((so->so_state & SS_ISDISCONNECTING) == 0) { | |
dedd6629 | 139 | error = sodisconnect(so); |
88a7a62a SL |
140 | if (error) |
141 | goto drop; | |
cc15ab5d | 142 | } |
98422daa | 143 | if (so->so_options & SO_LINGER) { |
b8acc34d | 144 | if ((so->so_state & SS_ISDISCONNECTING) && |
88a7a62a SL |
145 | (so->so_state & SS_NBIO)) |
146 | goto drop; | |
b8acc34d | 147 | while (so->so_state & SS_ISCONNECTED) |
83866636 MK |
148 | if (error = tsleep((caddr_t)&so->so_timeo, |
149 | PSOCK | PCATCH, netcls, so->so_linger)) | |
150 | break; | |
72857acf | 151 | } |
cc15ab5d | 152 | } |
89900a09 | 153 | drop: |
37c0974a | 154 | if (so->so_pcb) { |
88a7a62a SL |
155 | int error2 = |
156 | (*so->so_proto->pr_usrreq)(so, PRU_DETACH, | |
157 | (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); | |
158 | if (error == 0) | |
159 | error = error2; | |
37c0974a | 160 | } |
cc15ab5d | 161 | discard: |
26225f25 SL |
162 | if (so->so_state & SS_NOFDREF) |
163 | panic("soclose: NOFDREF"); | |
90aaea96 | 164 | so->so_state |= SS_NOFDREF; |
4ad99bae | 165 | sofree(so); |
cc15ab5d | 166 | splx(s); |
88a7a62a | 167 | return (error); |
92a533e6 BJ |
168 | } |
169 | ||
26225f25 SL |
170 | /* |
171 | * Must be called at splnet... | |
172 | */ | |
173 | soabort(so) | |
174 | struct socket *so; | |
175 | { | |
26225f25 | 176 | |
88a7a62a SL |
177 | return ( |
178 | (*so->so_proto->pr_usrreq)(so, PRU_ABORT, | |
179 | (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)); | |
92a533e6 BJ |
180 | } |
181 | ||
98422daa | 182 | soaccept(so, nam) |
88a7a62a | 183 | register struct socket *so; |
cf012934 | 184 | struct mbuf *nam; |
2b4b57cd BJ |
185 | { |
186 | int s = splnet(); | |
187 | int error; | |
188 | ||
26225f25 SL |
189 | if ((so->so_state & SS_NOFDREF) == 0) |
190 | panic("soaccept: !NOFDREF"); | |
98422daa | 191 | so->so_state &= ~SS_NOFDREF; |
cf012934 | 192 | error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, |
88a7a62a | 193 | (struct mbuf *)0, nam, (struct mbuf *)0); |
2b4b57cd BJ |
194 | splx(s); |
195 | return (error); | |
196 | } | |
197 | ||
98422daa | 198 | soconnect(so, nam) |
88a7a62a | 199 | register struct socket *so; |
cf012934 | 200 | struct mbuf *nam; |
ce9d8eb4 | 201 | { |
7bcf9d13 | 202 | int s; |
cc15ab5d | 203 | int error; |
ce9d8eb4 | 204 | |
7bcf9d13 MK |
205 | if (so->so_options & SO_ACCEPTCONN) |
206 | return (EOPNOTSUPP); | |
207 | s = splnet(); | |
de2c74a5 MK |
208 | /* |
209 | * If protocol is connection-based, can only connect once. | |
210 | * Otherwise, if connected, try to disconnect first. | |
211 | * This allows user to disconnect by connecting to, e.g., | |
212 | * a null address. | |
213 | */ | |
214 | if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && | |
215 | ((so->so_proto->pr_flags & PR_CONNREQUIRED) || | |
216 | (error = sodisconnect(so)))) | |
cc15ab5d | 217 | error = EISCONN; |
de2c74a5 MK |
218 | else |
219 | error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, | |
220 | (struct mbuf *)0, nam, (struct mbuf *)0); | |
cc15ab5d BJ |
221 | splx(s); |
222 | return (error); | |
ce9d8eb4 BJ |
223 | } |
224 | ||
88a7a62a SL |
225 | soconnect2(so1, so2) |
226 | register struct socket *so1; | |
227 | struct socket *so2; | |
228 | { | |
229 | int s = splnet(); | |
230 | int error; | |
231 | ||
5a48956d SL |
232 | error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, |
233 | (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0); | |
88a7a62a SL |
234 | splx(s); |
235 | return (error); | |
236 | } | |
88a7a62a | 237 | |
dedd6629 | 238 | sodisconnect(so) |
88a7a62a | 239 | register struct socket *so; |
ce9d8eb4 | 240 | { |
cc15ab5d BJ |
241 | int s = splnet(); |
242 | int error; | |
ce9d8eb4 | 243 | |
cc15ab5d BJ |
244 | if ((so->so_state & SS_ISCONNECTED) == 0) { |
245 | error = ENOTCONN; | |
246 | goto bad; | |
ce9d8eb4 | 247 | } |
cc15ab5d BJ |
248 | if (so->so_state & SS_ISDISCONNECTING) { |
249 | error = EALREADY; | |
250 | goto bad; | |
ce9d8eb4 | 251 | } |
cf012934 | 252 | error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, |
dedd6629 | 253 | (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); |
cc15ab5d BJ |
254 | bad: |
255 | splx(s); | |
256 | return (error); | |
ce9d8eb4 BJ |
257 | } |
258 | ||
440c48bf | 259 | #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) |
cc15ab5d BJ |
260 | /* |
261 | * Send on a socket. | |
262 | * If send must go all at once and message is larger than | |
263 | * send buffering, then hard error. | |
264 | * Lock against other senders. | |
265 | * If must go all at once and not enough room now, then | |
266 | * inform user that this would block and do nothing. | |
8250a099 | 267 | * Otherwise, if nonblocking, send as much as possible. |
7c4ec3aa MK |
268 | * The data to be sent is described by "uio" if nonzero, |
269 | * otherwise by the mbuf chain "top" (which must be null | |
270 | * if uio is not). Data provided in mbuf chain must be small | |
271 | * enough to send all at once. | |
272 | * | |
273 | * Returns nonzero on error, timeout or signal; callers | |
274 | * must check for short counts if EINTR/ERESTART are returned. | |
275 | * Data and control buffers are freed on return. | |
cc15ab5d | 276 | */ |
4b9db1f5 | 277 | sosend(so, addr, uio, top, control, flags) |
ce9d8eb4 | 278 | register struct socket *so; |
7c4ec3aa MK |
279 | struct mbuf *addr; |
280 | struct uio *uio; | |
281 | struct mbuf *top; | |
2967f28e | 282 | struct mbuf *control; |
7c4ec3aa | 283 | int flags; |
ce9d8eb4 | 284 | { |
dff5c020 | 285 | struct proc *p = curproc; /* XXX */ |
7c4ec3aa | 286 | struct mbuf **mp; |
2557c1fc | 287 | register struct mbuf *m; |
7c4ec3aa MK |
288 | register long space, len, resid; |
289 | int clen = 0, error, s, dontroute, mlen; | |
290 | int atomic = sosendallatonce(so) || top; | |
ce9d8eb4 | 291 | |
7c4ec3aa MK |
292 | if (uio) |
293 | resid = uio->uio_resid; | |
294 | else | |
295 | resid = top->m_pkthdr.len; | |
88a7a62a SL |
296 | dontroute = |
297 | (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && | |
298 | (so->so_proto->pr_flags & PR_ATOMIC); | |
dff5c020 | 299 | p->p_stats->p_ru.ru_msgsnd++; |
2967f28e | 300 | if (control) |
7c4ec3aa | 301 | clen = control->m_len; |
cc15ab5d BJ |
302 | #define snderr(errno) { error = errno; splx(s); goto release; } |
303 | ||
8250a099 | 304 | restart: |
440c48bf | 305 | if (error = sblock(&so->so_snd, SBLOCKWAIT(flags))) |
7c4ec3aa | 306 | goto out; |
8250a099 MK |
307 | do { |
308 | s = splnet(); | |
af9c562f | 309 | if (so->so_state & SS_CANTSENDMORE) |
8250a099 | 310 | snderr(EPIPE); |
a2aebb63 KS |
311 | if (so->so_error) |
312 | snderr(so->so_error); | |
8250a099 | 313 | if ((so->so_state & SS_ISCONNECTED) == 0) { |
a2aebb63 | 314 | if (so->so_proto->pr_flags & PR_CONNREQUIRED) { |
f3bf27ac KS |
315 | if ((so->so_state & SS_ISCONFIRMING) == 0 && |
316 | !(resid == 0 && clen != 0)) | |
a2aebb63 | 317 | snderr(ENOTCONN); |
7c4ec3aa | 318 | } else if (addr == 0) |
8250a099 MK |
319 | snderr(EDESTADDRREQ); |
320 | } | |
7c4ec3aa | 321 | space = sbspace(&so->so_snd); |
8250a099 | 322 | if (flags & MSG_OOB) |
7c4ec3aa | 323 | space += 1024; |
440c48bf KM |
324 | if (atomic && resid > so->so_snd.sb_hiwat || |
325 | clen > so->so_snd.sb_hiwat) | |
326 | snderr(EMSGSIZE); | |
327 | if (space < resid + clen && uio && | |
7c4ec3aa | 328 | (atomic || space < so->so_snd.sb_lowat || space < clen)) { |
7c4ec3aa MK |
329 | if (so->so_state & SS_NBIO) |
330 | snderr(EWOULDBLOCK); | |
331 | sbunlock(&so->so_snd); | |
4b9db1f5 | 332 | error = sbwait(&so->so_snd); |
7c4ec3aa MK |
333 | splx(s); |
334 | if (error) | |
335 | goto out; | |
336 | goto restart; | |
8250a099 | 337 | } |
4c078bb2 | 338 | splx(s); |
8250a099 | 339 | mp = ⊤ |
7c4ec3aa | 340 | space -= clen; |
4b9db1f5 MK |
341 | do { |
342 | if (uio == NULL) { | |
7c4ec3aa MK |
343 | /* |
344 | * Data is prepackaged in "top". | |
345 | */ | |
346 | resid = 0; | |
347 | if (flags & MSG_EOR) | |
348 | top->m_flags |= M_EOR; | |
4b9db1f5 | 349 | } else do { |
2557c1fc MK |
350 | if (top == 0) { |
351 | MGETHDR(m, M_WAIT, MT_DATA); | |
352 | mlen = MHLEN; | |
353 | m->m_pkthdr.len = 0; | |
354 | m->m_pkthdr.rcvif = (struct ifnet *)0; | |
355 | } else { | |
356 | MGET(m, M_WAIT, MT_DATA); | |
357 | mlen = MLEN; | |
358 | } | |
7c4ec3aa | 359 | if (resid >= MINCLSIZE && space >= MCLBYTES) { |
2557c1fc MK |
360 | MCLGET(m, M_WAIT); |
361 | if ((m->m_flags & M_EXT) == 0) | |
8250a099 | 362 | goto nopages; |
2557c1fc MK |
363 | mlen = MCLBYTES; |
364 | #ifdef MAPPED_MBUFS | |
7c4ec3aa | 365 | len = min(MCLBYTES, resid); |
2557c1fc | 366 | #else |
7c4ec3aa MK |
367 | if (top == 0) { |
368 | len = min(MCLBYTES - max_hdr, resid); | |
369 | m->m_data += max_hdr; | |
415a9324 KS |
370 | } else |
371 | len = min(MCLBYTES, resid); | |
2557c1fc MK |
372 | #endif |
373 | space -= MCLBYTES; | |
8250a099 MK |
374 | } else { |
375 | nopages: | |
7c4ec3aa | 376 | len = min(min(mlen, resid), space); |
8c0650b0 | 377 | space -= len; |
2557c1fc MK |
378 | /* |
379 | * For datagram protocols, leave room | |
380 | * for protocol headers in first mbuf. | |
381 | */ | |
84efcd38 | 382 | if (atomic && top == 0 && len < mlen) |
2557c1fc | 383 | MH_ALIGN(m, len); |
8250a099 | 384 | } |
179cd11f | 385 | error = uiomove(mtod(m, caddr_t), (int)len, uio); |
7c4ec3aa | 386 | resid = uio->uio_resid; |
8250a099 MK |
387 | m->m_len = len; |
388 | *mp = m; | |
2557c1fc | 389 | top->m_pkthdr.len += len; |
8250a099 MK |
390 | if (error) |
391 | goto release; | |
392 | mp = &m->m_next; | |
7c4ec3aa MK |
393 | if (resid <= 0) { |
394 | if (flags & MSG_EOR) | |
2557c1fc | 395 | top->m_flags |= M_EOR; |
af9c562f | 396 | break; |
2557c1fc MK |
397 | } |
398 | } while (space > 0 && atomic); | |
399 | if (dontroute) | |
400 | so->so_options |= SO_DONTROUTE; | |
401 | s = splnet(); /* XXX */ | |
402 | error = (*so->so_proto->pr_usrreq)(so, | |
403 | (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, | |
7c4ec3aa | 404 | top, addr, control); |
2557c1fc MK |
405 | splx(s); |
406 | if (dontroute) | |
407 | so->so_options &= ~SO_DONTROUTE; | |
7c4ec3aa MK |
408 | clen = 0; |
409 | control = 0; | |
2557c1fc MK |
410 | top = 0; |
411 | mp = ⊤ | |
2557c1fc MK |
412 | if (error) |
413 | goto release; | |
7c4ec3aa MK |
414 | } while (resid && space > 0); |
415 | } while (resid); | |
cc15ab5d | 416 | |
ce9d8eb4 | 417 | release: |
cc15ab5d | 418 | sbunlock(&so->so_snd); |
7c4ec3aa | 419 | out: |
0f90f987 BJ |
420 | if (top) |
421 | m_freem(top); | |
7c4ec3aa MK |
422 | if (control) |
423 | m_freem(control); | |
ce9d8eb4 BJ |
424 | return (error); |
425 | } | |
426 | ||
c34d38f4 MK |
427 | /* |
428 | * Implement receive operations on a socket. | |
429 | * We depend on the way that records are added to the sockbuf | |
430 | * by sbappend*. In particular, each record (mbufs linked through m_next) | |
431 | * must begin with an address if the protocol so specifies, | |
7c4ec3aa MK |
432 | * followed by an optional mbuf or mbufs containing ancillary data, |
433 | * and then zero or more mbufs of data. | |
c34d38f4 MK |
434 | * In order to avoid blocking network interrupts for the entire time here, |
435 | * we splx() while doing the actual copy to user space. | |
436 | * Although the sockbuf is locked, new data may still be appended, | |
437 | * and thus we must maintain consistency of the sockbuf during that time. | |
179cd11f | 438 | * |
7c4ec3aa | 439 | * The caller may receive the data as a single mbuf chain by supplying |
4b9db1f5 | 440 | * an mbuf **mp0 for use in returning the chain. The uio is then used |
7c4ec3aa | 441 | * only for the count in uio_resid. |
c34d38f4 | 442 | */ |
4b9db1f5 | 443 | soreceive(so, paddr, uio, mp0, controlp, flagsp) |
ce9d8eb4 | 444 | register struct socket *so; |
7c4ec3aa MK |
445 | struct mbuf **paddr; |
446 | struct uio *uio; | |
4b9db1f5 | 447 | struct mbuf **mp0; |
7c4ec3aa | 448 | struct mbuf **controlp; |
2557c1fc | 449 | int *flagsp; |
ce9d8eb4 | 450 | { |
dff5c020 | 451 | struct proc *p = curproc; /* XXX */ |
4b9db1f5 MK |
452 | register struct mbuf *m, **mp; |
453 | register int flags, len, error, s, offset; | |
88a7a62a | 454 | struct protosw *pr = so->so_proto; |
7c4ec3aa MK |
455 | struct mbuf *nextrecord; |
456 | int moff, type; | |
88a7a62a | 457 | |
4b9db1f5 | 458 | mp = mp0; |
7c4ec3aa MK |
459 | if (paddr) |
460 | *paddr = 0; | |
2557c1fc MK |
461 | if (controlp) |
462 | *controlp = 0; | |
463 | if (flagsp) | |
464 | flags = *flagsp &~ MSG_EOR; | |
179cd11f | 465 | else |
2557c1fc | 466 | flags = 0; |
88a7a62a | 467 | if (flags & MSG_OOB) { |
cce93e4b | 468 | m = m_get(M_WAIT, MT_DATA); |
88a7a62a | 469 | error = (*pr->pr_usrreq)(so, PRU_RCVOOB, |
de2c74a5 | 470 | m, (struct mbuf *)(flags & MSG_PEEK), (struct mbuf *)0); |
a8d3bf7f | 471 | if (error) |
5fe6f9d1 | 472 | goto bad; |
970108c7 | 473 | do { |
7c4ec3aa MK |
474 | error = uiomove(mtod(m, caddr_t), |
475 | (int) min(uio->uio_resid, m->m_len), uio); | |
970108c7 | 476 | m = m_free(m); |
a8d3bf7f | 477 | } while (uio->uio_resid && error == 0 && m); |
5fe6f9d1 | 478 | bad: |
970108c7 | 479 | if (m) |
39d536e6 | 480 | m_freem(m); |
a8d3bf7f | 481 | return (error); |
970108c7 | 482 | } |
7c4ec3aa MK |
483 | if (mp) |
484 | *mp = (struct mbuf *)0; | |
4b9db1f5 | 485 | if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) |
2557c1fc MK |
486 | (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, |
487 | (struct mbuf *)0, (struct mbuf *)0); | |
ce9d8eb4 | 488 | |
cc15ab5d | 489 | restart: |
440c48bf | 490 | if (error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) |
83866636 | 491 | return (error); |
cc15ab5d BJ |
492 | s = splnet(); |
493 | ||
a2aebb63 | 494 | m = so->so_rcv.sb_mb; |
ba4350f5 MK |
495 | /* |
496 | * If we have less data than requested, block awaiting more | |
497 | * (subject to any timeout) if: | |
498 | * 1. the current count is less than the low water mark, or | |
499 | * 2. MSG_WAITALL is set, and it is possible to do the entire | |
500 | * receive operation at once if we block (resid <= hiwat). | |
440c48bf | 501 | * 3. MSG_DONTWAIT is not set |
ba4350f5 MK |
502 | * If MSG_WAITALL is set but resid is larger than the receive buffer, |
503 | * we have to do the receive in sections, and thus risk returning | |
504 | * a short count if a timeout or signal occurs after we start. | |
505 | */ | |
440c48bf KM |
506 | if (m == 0 || ((flags & MSG_DONTWAIT) == 0 && |
507 | so->so_rcv.sb_cc < uio->uio_resid) && | |
ba4350f5 | 508 | (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || |
c05ef6cd KS |
509 | ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat))) |
510 | if (m && (m->m_nextpkt || (m->m_flags & M_EOR) || | |
511 | m->m_type == MT_OOBDATA || m->m_type == MT_CONTROL)) | |
512 | break; | |
7c4ec3aa MK |
513 | #ifdef DIAGNOSTIC |
514 | if (m == 0 && so->so_rcv.sb_cc) | |
a2aebb63 | 515 | panic("receive 1"); |
7c4ec3aa | 516 | #endif |
4c078bb2 | 517 | if (so->so_error) { |
95c435b0 | 518 | if (m) |
d4c3a4dd | 519 | goto dontblock; |
4c078bb2 | 520 | error = so->so_error; |
95c435b0 MK |
521 | if ((flags & MSG_PEEK) == 0) |
522 | so->so_error = 0; | |
4c078bb2 BJ |
523 | goto release; |
524 | } | |
95c435b0 MK |
525 | if (so->so_state & SS_CANTRCVMORE) { |
526 | if (m) | |
d4c3a4dd | 527 | goto dontblock; |
95c435b0 MK |
528 | else |
529 | goto release; | |
530 | } | |
629e51da | 531 | if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && |
f02d4eaa KB |
532 | (so->so_proto->pr_flags & PR_CONNREQUIRED)) { |
533 | error = ENOTCONN; | |
534 | goto release; | |
535 | } | |
4b9db1f5 | 536 | if (uio->uio_resid == 0) |
c34d38f4 | 537 | goto release; |
440c48bf | 538 | if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) { |
f02d4eaa KB |
539 | error = EWOULDBLOCK; |
540 | goto release; | |
541 | } | |
cc15ab5d | 542 | sbunlock(&so->so_rcv); |
4b9db1f5 | 543 | error = sbwait(&so->so_rcv); |
a4f6d93d | 544 | splx(s); |
7c4ec3aa MK |
545 | if (error) |
546 | return (error); | |
cc15ab5d | 547 | goto restart; |
ce9d8eb4 | 548 | } |
dff5c020 | 549 | p->p_stats->p_ru.ru_msgrcv++; |
2557c1fc | 550 | nextrecord = m->m_nextpkt; |
c05ef6cd | 551 | record_eor = m->m_flags & M_EOR; |
88a7a62a | 552 | if (pr->pr_flags & PR_ADDR) { |
7c4ec3aa | 553 | #ifdef DIAGNOSTIC |
c34d38f4 | 554 | if (m->m_type != MT_SONAME) |
261a8548 | 555 | panic("receive 1a"); |
7c4ec3aa | 556 | #endif |
261a8548 | 557 | if (flags & MSG_PEEK) { |
7c4ec3aa MK |
558 | if (paddr) |
559 | *paddr = m_copy(m, 0, m->m_len); | |
c34d38f4 | 560 | m = m->m_next; |
261a8548 | 561 | } else { |
c34d38f4 | 562 | sbfree(&so->so_rcv, m); |
7c4ec3aa MK |
563 | if (paddr) { |
564 | *paddr = m; | |
6ff43975 | 565 | so->so_rcv.sb_mb = m->m_next; |
c34d38f4 | 566 | m->m_next = 0; |
6ff43975 | 567 | m = so->so_rcv.sb_mb; |
c34d38f4 | 568 | } else { |
6ff43975 MK |
569 | MFREE(m, so->so_rcv.sb_mb); |
570 | m = so->so_rcv.sb_mb; | |
c34d38f4 | 571 | } |
88a7a62a | 572 | } |
cc15ab5d | 573 | } |
7c4ec3aa | 574 | while (m && m->m_type == MT_CONTROL && error == 0) { |
2557c1fc MK |
575 | if (flags & MSG_PEEK) { |
576 | if (controlp) | |
577 | *controlp = m_copy(m, 0, m->m_len); | |
578 | m = m->m_next; | |
579 | } else { | |
580 | sbfree(&so->so_rcv, m); | |
581 | if (controlp) { | |
e8f8de91 KS |
582 | if (pr->pr_domain->dom_externalize && |
583 | mtod(m, struct cmsghdr *)->cmsg_type == | |
584 | SCM_RIGHTS) | |
7c4ec3aa | 585 | error = (*pr->pr_domain->dom_externalize)(m); |
2557c1fc MK |
586 | *controlp = m; |
587 | so->so_rcv.sb_mb = m->m_next; | |
588 | m->m_next = 0; | |
589 | m = so->so_rcv.sb_mb; | |
590 | } else { | |
591 | MFREE(m, so->so_rcv.sb_mb); | |
592 | m = so->so_rcv.sb_mb; | |
593 | } | |
594 | } | |
7c4ec3aa MK |
595 | if (controlp) |
596 | controlp = &(*controlp)->m_next; | |
2557c1fc | 597 | } |
7c4ec3aa | 598 | if (m) { |
ba4350f5 MK |
599 | if ((flags & MSG_PEEK) == 0) |
600 | m->m_nextpkt = nextrecord; | |
7c4ec3aa | 601 | type = m->m_type; |
415a9324 KS |
602 | if (type == MT_OOBDATA) |
603 | flags |= MSG_OOB; | |
7c4ec3aa | 604 | } |
970108c7 | 605 | moff = 0; |
dd1ca18d | 606 | offset = 0; |
415a9324 KS |
607 | while (m && uio->uio_resid > 0 && error == 0) { |
608 | if (m->m_type == MT_OOBDATA) { | |
609 | if (type != MT_OOBDATA) | |
610 | break; | |
611 | } else if (type == MT_OOBDATA) | |
612 | break; | |
7c4ec3aa | 613 | #ifdef DIAGNOSTIC |
2557c1fc | 614 | else if (m->m_type != MT_DATA && m->m_type != MT_HEADER) |
c34d38f4 | 615 | panic("receive 3"); |
7c4ec3aa | 616 | #endif |
32a43ee2 | 617 | so->so_state &= ~SS_RCVATMARK; |
4b9db1f5 | 618 | len = uio->uio_resid; |
dd1ca18d MK |
619 | if (so->so_oobmark && len > so->so_oobmark - offset) |
620 | len = so->so_oobmark - offset; | |
8c0650b0 | 621 | if (len > m->m_len - moff) |
970108c7 | 622 | len = m->m_len - moff; |
7c4ec3aa MK |
623 | /* |
624 | * If mp is set, just pass back the mbufs. | |
625 | * Otherwise copy them out via the uio, then free. | |
626 | * Sockbuf must be consistent here (points to current mbuf, | |
627 | * it points to next record) when we drop priority; | |
628 | * we must note any additions to the sockbuf when we | |
629 | * block interrupts again. | |
630 | */ | |
631 | if (mp == 0) { | |
632 | splx(s); | |
633 | error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio); | |
7c4ec3aa | 634 | s = splnet(); |
4b9db1f5 MK |
635 | } else |
636 | uio->uio_resid -= len; | |
8c0650b0 | 637 | if (len == m->m_len - moff) { |
c34d38f4 MK |
638 | if (flags & MSG_PEEK) { |
639 | m = m->m_next; | |
640 | moff = 0; | |
641 | } else { | |
2557c1fc | 642 | nextrecord = m->m_nextpkt; |
6ff43975 | 643 | sbfree(&so->so_rcv, m); |
7c4ec3aa MK |
644 | if (mp) { |
645 | *mp = m; | |
646 | mp = &m->m_next; | |
4b9db1f5 MK |
647 | so->so_rcv.sb_mb = m = m->m_next; |
648 | *mp = (struct mbuf *)0; | |
7c4ec3aa MK |
649 | } else { |
650 | MFREE(m, so->so_rcv.sb_mb); | |
651 | m = so->so_rcv.sb_mb; | |
652 | } | |
6ff43975 | 653 | if (m) |
2557c1fc | 654 | m->m_nextpkt = nextrecord; |
c34d38f4 | 655 | } |
ce9d8eb4 | 656 | } else { |
88a7a62a | 657 | if (flags & MSG_PEEK) |
970108c7 BJ |
658 | moff += len; |
659 | else { | |
4b9db1f5 MK |
660 | if (mp) |
661 | *mp = m_copym(m, 0, len, M_WAIT); | |
2557c1fc | 662 | m->m_data += len; |
970108c7 BJ |
663 | m->m_len -= len; |
664 | so->so_rcv.sb_cc -= len; | |
665 | } | |
ce9d8eb4 | 666 | } |
dd1ca18d MK |
667 | if (so->so_oobmark) { |
668 | if ((flags & MSG_PEEK) == 0) { | |
669 | so->so_oobmark -= len; | |
670 | if (so->so_oobmark == 0) { | |
671 | so->so_state |= SS_RCVATMARK; | |
672 | break; | |
673 | } | |
674 | } else | |
675 | offset += len; | |
970108c7 | 676 | } |
c05ef6cd KS |
677 | if (m == 0 && record_eor) { |
678 | flags |= record_eor; | |
2967f28e | 679 | break; |
c05ef6cd | 680 | } |
7c4ec3aa MK |
681 | /* |
682 | * If the MSG_WAITALL flag is set (for non-atomic socket), | |
4b9db1f5 | 683 | * we must not quit until "uio->uio_resid == 0" or an error |
7c4ec3aa | 684 | * termination. If a signal/timeout occurs, return |
4b9db1f5 | 685 | * with a short count but without error. |
7c4ec3aa MK |
686 | * Keep sockbuf locked against other readers. |
687 | */ | |
4b9db1f5 | 688 | while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 && |
c05ef6cd | 689 | !(flags & MSG_OOB) && !sosendallatonce(so)) { |
95c435b0 MK |
690 | if (so->so_error || so->so_state & SS_CANTRCVMORE) |
691 | break; | |
7c4ec3aa MK |
692 | error = sbwait(&so->so_rcv); |
693 | if (error) { | |
694 | sbunlock(&so->so_rcv); | |
695 | splx(s); | |
7c4ec3aa MK |
696 | return (0); |
697 | } | |
c05ef6cd | 698 | if (m = so->so_rcv.sb_mb) { |
7c4ec3aa | 699 | nextrecord = m->m_nextpkt; |
c05ef6cd KS |
700 | record_eor |= m->m_flags & M_EOR; |
701 | } | |
7c4ec3aa | 702 | } |
261a8548 MK |
703 | } |
704 | if ((flags & MSG_PEEK) == 0) { | |
491e9020 | 705 | if (m == 0) |
261a8548 | 706 | so->so_rcv.sb_mb = nextrecord; |
2557c1fc MK |
707 | else if (pr->pr_flags & PR_ATOMIC) { |
708 | flags |= MSG_TRUNC; | |
6ff43975 | 709 | (void) sbdroprecord(&so->so_rcv); |
2557c1fc | 710 | } |
261a8548 MK |
711 | if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) |
712 | (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, | |
a2aebb63 KS |
713 | (struct mbuf *)flags, (struct mbuf *)0, |
714 | (struct mbuf *)0); | |
261a8548 | 715 | } |
2557c1fc MK |
716 | if (flagsp) |
717 | *flagsp |= flags; | |
cc15ab5d | 718 | release: |
ae921915 | 719 | sbunlock(&so->so_rcv); |
cc15ab5d | 720 | splx(s); |
ae921915 | 721 | return (error); |
92a533e6 BJ |
722 | } |
723 | ||
98422daa | 724 | soshutdown(so, how) |
88a7a62a SL |
725 | register struct socket *so; |
726 | register int how; | |
98422daa | 727 | { |
88a7a62a | 728 | register struct protosw *pr = so->so_proto; |
98422daa SL |
729 | |
730 | how++; | |
88a7a62a SL |
731 | if (how & FREAD) |
732 | sorflush(so); | |
98422daa | 733 | if (how & FWRITE) |
88a7a62a SL |
734 | return ((*pr->pr_usrreq)(so, PRU_SHUTDOWN, |
735 | (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)); | |
98422daa SL |
736 | return (0); |
737 | } | |
738 | ||
88a7a62a SL |
739 | sorflush(so) |
740 | register struct socket *so; | |
741 | { | |
742 | register struct sockbuf *sb = &so->so_rcv; | |
743 | register struct protosw *pr = so->so_proto; | |
744 | register int s; | |
745 | struct sockbuf asb; | |
746 | ||
83866636 | 747 | sb->sb_flags |= SB_NOINTR; |
440c48bf | 748 | (void) sblock(sb, M_WAITOK); |
88a7a62a SL |
749 | s = splimp(); |
750 | socantrcvmore(so); | |
751 | sbunlock(sb); | |
752 | asb = *sb; | |
753 | bzero((caddr_t)sb, sizeof (*sb)); | |
754 | splx(s); | |
261a8548 MK |
755 | if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) |
756 | (*pr->pr_domain->dom_dispose)(asb.sb_mb); | |
88a7a62a SL |
757 | sbrelease(&asb); |
758 | } | |
759 | ||
bc2f5859 | 760 | sosetopt(so, level, optname, m0) |
88a7a62a | 761 | register struct socket *so; |
98422daa | 762 | int level, optname; |
bc2f5859 | 763 | struct mbuf *m0; |
98422daa | 764 | { |
61ec2127 | 765 | int error = 0; |
bc2f5859 | 766 | register struct mbuf *m = m0; |
98422daa | 767 | |
61ec2127 | 768 | if (level != SOL_SOCKET) { |
cbe54390 MK |
769 | if (so->so_proto && so->so_proto->pr_ctloutput) |
770 | return ((*so->so_proto->pr_ctloutput) | |
bc2f5859 | 771 | (PRCO_SETOPT, so, level, optname, &m0)); |
cbe54390 MK |
772 | error = ENOPROTOOPT; |
773 | } else { | |
774 | switch (optname) { | |
98422daa | 775 | |
cbe54390 MK |
776 | case SO_LINGER: |
777 | if (m == NULL || m->m_len != sizeof (struct linger)) { | |
778 | error = EINVAL; | |
779 | goto bad; | |
780 | } | |
781 | so->so_linger = mtod(m, struct linger *)->l_linger; | |
782 | /* fall thru... */ | |
783 | ||
784 | case SO_DEBUG: | |
785 | case SO_KEEPALIVE: | |
786 | case SO_DONTROUTE: | |
787 | case SO_USELOOPBACK: | |
788 | case SO_BROADCAST: | |
789 | case SO_REUSEADDR: | |
97c8f6a8 | 790 | case SO_OOBINLINE: |
cbe54390 MK |
791 | if (m == NULL || m->m_len < sizeof (int)) { |
792 | error = EINVAL; | |
793 | goto bad; | |
794 | } | |
795 | if (*mtod(m, int *)) | |
796 | so->so_options |= optname; | |
797 | else | |
798 | so->so_options &= ~optname; | |
799 | break; | |
98422daa | 800 | |
cbe54390 | 801 | case SO_SNDBUF: |
83866636 | 802 | case SO_RCVBUF: |
7c4ec3aa | 803 | case SO_SNDLOWAT: |
83866636 | 804 | case SO_RCVLOWAT: |
cbe54390 MK |
805 | if (m == NULL || m->m_len < sizeof (int)) { |
806 | error = EINVAL; | |
807 | goto bad; | |
808 | } | |
809 | switch (optname) { | |
810 | ||
811 | case SO_SNDBUF: | |
812 | case SO_RCVBUF: | |
7c4ec3aa MK |
813 | if (sbreserve(optname == SO_SNDBUF ? |
814 | &so->so_snd : &so->so_rcv, | |
815 | (u_long) *mtod(m, int *)) == 0) { | |
cbe54390 MK |
816 | error = ENOBUFS; |
817 | goto bad; | |
818 | } | |
819 | break; | |
820 | ||
821 | case SO_SNDLOWAT: | |
7c4ec3aa MK |
822 | so->so_snd.sb_lowat = *mtod(m, int *); |
823 | break; | |
cbe54390 | 824 | case SO_RCVLOWAT: |
7c4ec3aa | 825 | so->so_rcv.sb_lowat = *mtod(m, int *); |
cbe54390 | 826 | break; |
fc2cae0b MK |
827 | } |
828 | break; | |
829 | ||
830 | case SO_SNDTIMEO: | |
831 | case SO_RCVTIMEO: | |
832 | { | |
833 | struct timeval *tv; | |
834 | short val; | |
835 | ||
836 | if (m == NULL || m->m_len < sizeof (*tv)) { | |
837 | error = EINVAL; | |
838 | goto bad; | |
839 | } | |
840 | tv = mtod(m, struct timeval *); | |
841 | if (tv->tv_sec > SHRT_MAX / hz - hz) { | |
842 | error = EDOM; | |
843 | goto bad; | |
844 | } | |
845 | val = tv->tv_sec * hz + tv->tv_usec / tick; | |
846 | ||
847 | switch (optname) { | |
848 | ||
cbe54390 | 849 | case SO_SNDTIMEO: |
fc2cae0b | 850 | so->so_snd.sb_timeo = val; |
7c4ec3aa | 851 | break; |
cbe54390 | 852 | case SO_RCVTIMEO: |
fc2cae0b | 853 | so->so_rcv.sb_timeo = val; |
cbe54390 MK |
854 | break; |
855 | } | |
856 | break; | |
fc2cae0b | 857 | } |
cbe54390 MK |
858 | |
859 | default: | |
860 | error = ENOPROTOOPT; | |
861 | break; | |
862 | } | |
d4c3a4dd | 863 | m = 0; |
bfedcc73 KS |
864 | if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) |
865 | (void) ((*so->so_proto->pr_ctloutput) | |
866 | (PRCO_SETOPT, so, level, optname, &m0)); | |
98422daa | 867 | } |
61ec2127 SL |
868 | bad: |
869 | if (m) | |
870 | (void) m_free(m); | |
871 | return (error); | |
98422daa SL |
872 | } |
873 | ||
61ec2127 | 874 | sogetopt(so, level, optname, mp) |
88a7a62a | 875 | register struct socket *so; |
98422daa | 876 | int level, optname; |
61ec2127 | 877 | struct mbuf **mp; |
98422daa | 878 | { |
61ec2127 | 879 | register struct mbuf *m; |
98422daa | 880 | |
cbe54390 MK |
881 | if (level != SOL_SOCKET) { |
882 | if (so->so_proto && so->so_proto->pr_ctloutput) { | |
883 | return ((*so->so_proto->pr_ctloutput) | |
884 | (PRCO_GETOPT, so, level, optname, mp)); | |
179cd11f | 885 | } else |
cbe54390 MK |
886 | return (ENOPROTOOPT); |
887 | } else { | |
61ec2127 | 888 | m = m_get(M_WAIT, MT_SOOPTS); |
d6e6eea8 MK |
889 | m->m_len = sizeof (int); |
890 | ||
cbe54390 MK |
891 | switch (optname) { |
892 | ||
893 | case SO_LINGER: | |
894 | m->m_len = sizeof (struct linger); | |
895 | mtod(m, struct linger *)->l_onoff = | |
896 | so->so_options & SO_LINGER; | |
897 | mtod(m, struct linger *)->l_linger = so->so_linger; | |
898 | break; | |
899 | ||
900 | case SO_USELOOPBACK: | |
901 | case SO_DONTROUTE: | |
902 | case SO_DEBUG: | |
903 | case SO_KEEPALIVE: | |
904 | case SO_REUSEADDR: | |
905 | case SO_BROADCAST: | |
97c8f6a8 | 906 | case SO_OOBINLINE: |
cbe54390 MK |
907 | *mtod(m, int *) = so->so_options & optname; |
908 | break; | |
909 | ||
d6e6eea8 MK |
910 | case SO_TYPE: |
911 | *mtod(m, int *) = so->so_type; | |
912 | break; | |
913 | ||
de2c74a5 MK |
914 | case SO_ERROR: |
915 | *mtod(m, int *) = so->so_error; | |
916 | so->so_error = 0; | |
917 | break; | |
918 | ||
cbe54390 MK |
919 | case SO_SNDBUF: |
920 | *mtod(m, int *) = so->so_snd.sb_hiwat; | |
921 | break; | |
98422daa | 922 | |
cbe54390 MK |
923 | case SO_RCVBUF: |
924 | *mtod(m, int *) = so->so_rcv.sb_hiwat; | |
925 | break; | |
926 | ||
927 | case SO_SNDLOWAT: | |
928 | *mtod(m, int *) = so->so_snd.sb_lowat; | |
929 | break; | |
930 | ||
931 | case SO_RCVLOWAT: | |
932 | *mtod(m, int *) = so->so_rcv.sb_lowat; | |
933 | break; | |
934 | ||
935 | case SO_SNDTIMEO: | |
cbe54390 | 936 | case SO_RCVTIMEO: |
fc2cae0b MK |
937 | { |
938 | int val = (optname == SO_SNDTIMEO ? | |
939 | so->so_snd.sb_timeo : so->so_rcv.sb_timeo); | |
940 | ||
941 | m->m_len = sizeof(struct timeval); | |
942 | mtod(m, struct timeval *)->tv_sec = val / hz; | |
943 | mtod(m, struct timeval *)->tv_usec = | |
944 | (val % hz) / tick; | |
cbe54390 | 945 | break; |
fc2cae0b | 946 | } |
cbe54390 MK |
947 | |
948 | default: | |
8011f5df | 949 | (void)m_free(m); |
cbe54390 MK |
950 | return (ENOPROTOOPT); |
951 | } | |
952 | *mp = m; | |
953 | return (0); | |
98422daa | 954 | } |
98422daa SL |
955 | } |
956 | ||
edebca28 | 957 | sohasoutofband(so) |
88a7a62a | 958 | register struct socket *so; |
edebca28 | 959 | { |
3d190e86 | 960 | struct proc *p; |
edebca28 | 961 | |
a2aebb63 KS |
962 | if (so->so_pgid < 0) |
963 | gsignal(-so->so_pgid, SIGURG); | |
964 | else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0) | |
3d190e86 | 965 | psignal(p, SIGURG); |
de2c74a5 MK |
966 | if (so->so_rcv.sb_sel) { |
967 | selwakeup(so->so_rcv.sb_sel, so->so_rcv.sb_flags & SB_COLL); | |
968 | so->so_rcv.sb_sel = 0; | |
969 | so->so_rcv.sb_flags &= ~SB_COLL; | |
970 | } | |
edebca28 | 971 | } |