]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * The contents of this file constitute Original Code as defined in and | |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
11 | * | |
12 | * This Original Code and all software distributed under the License are | |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */ | |
23 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
24 | /* | |
25 | * Copyright (c) 1982, 1986, 1988, 1990, 1993 | |
26 | * The Regents of the University of California. All rights reserved. | |
27 | * | |
28 | * Redistribution and use in source and binary forms, with or without | |
29 | * modification, are permitted provided that the following conditions | |
30 | * are met: | |
31 | * 1. Redistributions of source code must retain the above copyright | |
32 | * notice, this list of conditions and the following disclaimer. | |
33 | * 2. Redistributions in binary form must reproduce the above copyright | |
34 | * notice, this list of conditions and the following disclaimer in the | |
35 | * documentation and/or other materials provided with the distribution. | |
36 | * 3. All advertising materials mentioning features or use of this software | |
37 | * must display the following acknowledgement: | |
38 | * This product includes software developed by the University of | |
39 | * California, Berkeley and its contributors. | |
40 | * 4. Neither the name of the University nor the names of its contributors | |
41 | * may be used to endorse or promote products derived from this software | |
42 | * without specific prior written permission. | |
43 | * | |
44 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
45 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
47 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
54 | * SUCH DAMAGE. | |
55 | * | |
56 | * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93 | |
57 | */ | |
58 | ||
59 | #include <sys/param.h> | |
60 | #include <sys/systm.h> | |
61 | #include <sys/domain.h> | |
62 | #include <sys/kernel.h> | |
63 | #include <sys/proc.h> | |
64 | #include <sys/malloc.h> | |
65 | #include <sys/mbuf.h> | |
66 | #include <sys/protosw.h> | |
67 | #include <sys/stat.h> | |
68 | #include <sys/socket.h> | |
69 | #include <sys/socketvar.h> | |
70 | #include <sys/signalvar.h> | |
71 | #include <sys/sysctl.h> | |
72 | #include <sys/ev.h> | |
73 | ||
fa4905b1 A |
74 | #include <sys/kdebug.h> |
75 | ||
76 | #define DBG_FNC_SBDROP NETDBG_CODE(DBG_NETSOCK, 4) | |
77 | #define DBG_FNC_SBAPPEND NETDBG_CODE(DBG_NETSOCK, 5) | |
78 | ||
79 | ||
1c79356b A |
80 | /* |
81 | * Primitive routines for operating on sockets and socket buffers | |
82 | */ | |
83 | ||
84 | u_long sb_max = SB_MAX; /* XXX should be static */ | |
85 | ||
86 | static u_long sb_efficiency = 8; /* parameter for sbreserve() */ | |
87 | ||
88 | char netcon[] = "netcon"; | |
89 | ||
90 | /* | |
91 | * Procedures to manipulate state flags of socket | |
92 | * and do appropriate wakeups. Normal sequence from the | |
93 | * active (originating) side is that soisconnecting() is | |
94 | * called during processing of connect() call, | |
95 | * resulting in an eventual call to soisconnected() if/when the | |
96 | * connection is established. When the connection is torn down | |
e3027f41 | 97 | * soisdisconnecting() is called during processing of disconnect() call, |
1c79356b A |
98 | * and soisdisconnected() is called when the connection to the peer |
99 | * is totally severed. The semantics of these routines are such that | |
100 | * connectionless protocols can call soisconnected() and soisdisconnected() | |
101 | * only, bypassing the in-progress calls when setting up a ``connection'' | |
102 | * takes no time. | |
103 | * | |
104 | * From the passive side, a socket is created with | |
e3027f41 A |
105 | * two queues of sockets: so_incomp for connections in progress |
106 | * and so_comp for connections already made and awaiting user acceptance. | |
107 | * As a protocol is preparing incoming connections, it creates a socket | |
108 | * structure queued on so_incomp by calling sonewconn(). When the connection | |
1c79356b | 109 | * is established, soisconnected() is called, and transfers the |
e3027f41 | 110 | * socket structure to so_comp, making it available to accept(). |
1c79356b | 111 | * |
e3027f41 A |
112 | * If a socket is closed with sockets on either |
113 | * so_incomp or so_comp, these sockets are dropped. | |
114 | * | |
1c79356b A |
115 | * If higher level protocols are implemented in |
116 | * the kernel, the wakeups done here will sometimes | |
117 | * cause software-interrupt process scheduling. | |
118 | */ | |
119 | ||
120 | void | |
121 | soisconnecting(so) | |
122 | register struct socket *so; | |
123 | { | |
124 | ||
125 | so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); | |
126 | so->so_state |= SS_ISCONNECTING; | |
127 | } | |
128 | ||
129 | void | |
130 | soisconnected(so) | |
131 | register struct socket *so; | |
132 | { register struct kextcb *kp; | |
133 | register struct socket *head = so->so_head; | |
134 | ||
135 | kp = sotokextcb(so); | |
136 | while (kp) | |
137 | { if (kp->e_soif && kp->e_soif->sf_soisconnected) | |
138 | { if ((*kp->e_soif->sf_soisconnected)(so, kp)) | |
139 | return; | |
140 | } | |
141 | kp = kp->e_next; | |
142 | } | |
143 | ||
144 | so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); | |
145 | so->so_state |= SS_ISCONNECTED; | |
146 | if (head && (so->so_state & SS_INCOMP)) { | |
147 | postevent(head,0,EV_RCONN); | |
148 | TAILQ_REMOVE(&head->so_incomp, so, so_list); | |
149 | head->so_incqlen--; | |
150 | so->so_state &= ~SS_INCOMP; | |
151 | TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); | |
152 | so->so_state |= SS_COMP; | |
153 | sorwakeup(head); | |
154 | wakeup((caddr_t)&head->so_timeo); | |
155 | } else { | |
156 | postevent(so,0,EV_WCONN); | |
157 | wakeup((caddr_t)&so->so_timeo); | |
158 | sorwakeup(so); | |
159 | sowwakeup(so); | |
160 | } | |
161 | } | |
162 | ||
163 | void | |
164 | soisdisconnecting(so) | |
165 | register struct socket *so; | |
166 | { register struct kextcb *kp; | |
167 | ||
168 | kp = sotokextcb(so); | |
169 | while (kp) | |
170 | { if (kp->e_soif && kp->e_soif->sf_soisdisconnecting) | |
171 | { if ((*kp->e_soif->sf_soisdisconnecting)(so, kp)) | |
172 | return; | |
173 | } | |
174 | kp = kp->e_next; | |
175 | } | |
176 | ||
177 | so->so_state &= ~SS_ISCONNECTING; | |
178 | so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); | |
179 | wakeup((caddr_t)&so->so_timeo); | |
180 | sowwakeup(so); | |
181 | sorwakeup(so); | |
182 | } | |
183 | ||
184 | void | |
185 | soisdisconnected(so) | |
186 | register struct socket *so; | |
187 | { register struct kextcb *kp; | |
188 | ||
189 | kp = sotokextcb(so); | |
190 | while (kp) | |
191 | { if (kp->e_soif && kp->e_soif->sf_soisdisconnected) | |
192 | { if ((*kp->e_soif->sf_soisdisconnected)(so, kp)) | |
193 | return; | |
194 | } | |
195 | kp = kp->e_next; | |
196 | } | |
197 | ||
198 | so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); | |
199 | so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE); | |
200 | wakeup((caddr_t)&so->so_timeo); | |
201 | sowwakeup(so); | |
202 | sorwakeup(so); | |
203 | } | |
204 | ||
205 | /* | |
206 | * Return a random connection that hasn't been serviced yet and | |
207 | * is eligible for discard. There is a one in qlen chance that | |
208 | * we will return a null, saying that there are no dropable | |
209 | * requests. In this case, the protocol specific code should drop | |
210 | * the new request. This insures fairness. | |
211 | * | |
212 | * This may be used in conjunction with protocol specific queue | |
213 | * congestion routines. | |
214 | */ | |
215 | struct socket * | |
216 | sodropablereq(head) | |
217 | register struct socket *head; | |
218 | { | |
219 | register struct socket *so; | |
220 | unsigned int i, j, qlen; | |
221 | static int rnd; | |
222 | static struct timeval old_runtime; | |
223 | static unsigned int cur_cnt, old_cnt; | |
224 | struct timeval tv; | |
225 | ||
226 | microtime(&tv); | |
227 | if ((i = (tv.tv_sec - old_runtime.tv_sec)) != 0) { | |
228 | old_runtime = tv; | |
229 | old_cnt = cur_cnt / i; | |
230 | cur_cnt = 0; | |
231 | } | |
232 | ||
233 | so = TAILQ_FIRST(&head->so_incomp); | |
234 | if (!so) | |
235 | return (so); | |
236 | ||
237 | qlen = head->so_incqlen; | |
238 | if (++cur_cnt > qlen || old_cnt > qlen) { | |
239 | rnd = (314159 * rnd + 66329) & 0xffff; | |
240 | j = ((qlen + 1) * rnd) >> 16; | |
241 | ||
242 | while (j-- && so) | |
243 | so = TAILQ_NEXT(so, so_list); | |
244 | } | |
245 | ||
246 | return (so); | |
247 | } | |
248 | ||
249 | /* | |
250 | * When an attempt at a new connection is noted on a socket | |
251 | * which accepts connections, sonewconn is called. If the | |
252 | * connection is possible (subject to space constraints, etc.) | |
253 | * then we allocate a new structure, propoerly linked into the | |
254 | * data structure of the original socket, and return this. | |
255 | * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. | |
256 | */ | |
257 | struct socket * | |
258 | sonewconn(head, connstatus) | |
259 | register struct socket *head; | |
260 | int connstatus; | |
261 | { int error = 0; | |
262 | register struct socket *so; | |
263 | register struct kextcb *kp; | |
264 | ||
265 | if (head->so_qlen > 3 * head->so_qlimit / 2) | |
266 | return ((struct socket *)0); | |
0b4e3aa0 | 267 | so = soalloc(1, head->so_proto->pr_domain->dom_family, head->so_type); |
1c79356b A |
268 | if (so == NULL) |
269 | return ((struct socket *)0); | |
270 | ||
271 | kp = sotokextcb(so); | |
272 | while (kp) | |
273 | { if (kp->e_soif && kp->e_soif->sf_sonewconn1) | |
274 | { if ((*kp->e_soif->sf_sonewconn1)(so, connstatus, kp)) | |
275 | return; | |
276 | } | |
277 | kp = kp->e_next; | |
278 | } | |
279 | ||
280 | so->so_head = head; | |
281 | so->so_type = head->so_type; | |
282 | so->so_options = head->so_options &~ SO_ACCEPTCONN; | |
283 | so->so_linger = head->so_linger; | |
284 | so->so_state = head->so_state | SS_NOFDREF; | |
285 | so->so_proto = head->so_proto; | |
286 | so->so_timeo = head->so_timeo; | |
287 | so->so_pgid = head->so_pgid; | |
288 | so->so_uid = head->so_uid; | |
289 | so->so_rcv.sb_flags |= SB_RECV; /* XXX */ | |
fa4905b1 | 290 | |
1c79356b A |
291 | (void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat); |
292 | ||
293 | if (so->so_proto->pr_sfilter.tqh_first) | |
294 | error = sfilter_init(so); | |
295 | if (error == 0 && (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) { | |
296 | sfilter_term(so); | |
297 | sodealloc(so); | |
298 | return ((struct socket *)0); | |
299 | } | |
300 | so->so_proto->pr_domain->dom_refs++; | |
301 | ||
302 | if (connstatus) { | |
303 | TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); | |
304 | so->so_state |= SS_COMP; | |
305 | } else { | |
306 | TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); | |
307 | so->so_state |= SS_INCOMP; | |
308 | head->so_incqlen++; | |
309 | } | |
310 | head->so_qlen++; | |
311 | if (connstatus) { | |
312 | sorwakeup(head); | |
313 | wakeup((caddr_t)&head->so_timeo); | |
314 | so->so_state |= connstatus; | |
315 | } | |
316 | so->so_rcv.sb_so = so->so_snd.sb_so = so; | |
317 | TAILQ_INIT(&so->so_evlist); | |
318 | return (so); | |
319 | } | |
320 | ||
321 | /* | |
322 | * Socantsendmore indicates that no more data will be sent on the | |
323 | * socket; it would normally be applied to a socket when the user | |
324 | * informs the system that no more data is to be sent, by the protocol | |
325 | * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data | |
326 | * will be received, and will normally be applied to the socket by a | |
327 | * protocol when it detects that the peer will send no more data. | |
328 | * Data queued for reading in the socket may yet be read. | |
329 | */ | |
330 | ||
331 | void | |
332 | socantsendmore(so) | |
333 | struct socket *so; | |
334 | { register struct kextcb *kp; | |
335 | ||
336 | kp = sotokextcb(so); | |
337 | while (kp) | |
338 | { if (kp->e_soif && kp->e_soif->sf_socantsendmore) | |
339 | { if ((*kp->e_soif->sf_socantsendmore)(so, kp)) | |
340 | return; | |
341 | } | |
342 | kp = kp->e_next; | |
343 | } | |
344 | ||
345 | ||
346 | so->so_state |= SS_CANTSENDMORE; | |
347 | sowwakeup(so); | |
348 | } | |
349 | ||
350 | void | |
351 | socantrcvmore(so) | |
352 | struct socket *so; | |
353 | { register struct kextcb *kp; | |
354 | ||
355 | kp = sotokextcb(so); | |
356 | while (kp) | |
357 | { if (kp->e_soif && kp->e_soif->sf_socantrcvmore) | |
358 | { if ((*kp->e_soif->sf_socantrcvmore)(so, kp)) | |
359 | return; | |
360 | } | |
361 | kp = kp->e_next; | |
362 | } | |
363 | ||
364 | ||
365 | so->so_state |= SS_CANTRCVMORE; | |
366 | sorwakeup(so); | |
367 | } | |
368 | ||
369 | /* | |
370 | * Wait for data to arrive at/drain from a socket buffer. | |
371 | */ | |
372 | int | |
373 | sbwait(sb) | |
374 | struct sockbuf *sb; | |
375 | { | |
376 | ||
377 | sb->sb_flags |= SB_WAIT; | |
378 | return (tsleep((caddr_t)&sb->sb_cc, | |
379 | (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", | |
380 | sb->sb_timeo)); | |
381 | } | |
382 | ||
383 | /* | |
384 | * Lock a sockbuf already known to be locked; | |
385 | * return any error returned from sleep (EINTR). | |
386 | */ | |
387 | int | |
388 | sb_lock(sb) | |
389 | register struct sockbuf *sb; | |
390 | { | |
391 | int error; | |
392 | ||
393 | while (sb->sb_flags & SB_LOCK) { | |
394 | sb->sb_flags |= SB_WANT; | |
395 | error = tsleep((caddr_t)&sb->sb_flags, | |
396 | (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH, | |
397 | "sblock", 0); | |
398 | if (error) | |
399 | return (error); | |
400 | } | |
401 | sb->sb_flags |= SB_LOCK; | |
402 | return (0); | |
403 | } | |
404 | ||
405 | /* | |
406 | * Wakeup processes waiting on a socket buffer. | |
407 | * Do asynchronous notification via SIGIO | |
408 | * if the socket has the SS_ASYNC flag set. | |
409 | */ | |
410 | void | |
411 | sowakeup(so, sb) | |
412 | register struct socket *so; | |
413 | register struct sockbuf *sb; | |
414 | { | |
415 | struct proc *p = current_proc(); | |
416 | ||
417 | ||
1c79356b | 418 | |
0b4e3aa0 A |
419 | |
420 | sb->sb_flags &= ~SB_SEL; | |
1c79356b | 421 | selwakeup(&sb->sb_sel); |
1c79356b A |
422 | |
423 | if (sb->sb_flags & SB_WAIT) { | |
424 | sb->sb_flags &= ~SB_WAIT; | |
425 | wakeup((caddr_t)&sb->sb_cc); | |
426 | } | |
427 | if (so->so_state & SS_ASYNC) { | |
428 | if (so->so_pgid < 0) | |
429 | gsignal(-so->so_pgid, SIGIO); | |
430 | else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0) | |
431 | psignal(p, SIGIO); | |
432 | } | |
433 | ||
434 | if (sb->sb_flags & SB_UPCALL) | |
435 | (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT); | |
436 | } | |
437 | ||
438 | /* | |
439 | * Socket buffer (struct sockbuf) utility routines. | |
440 | * | |
441 | * Each socket contains two socket buffers: one for sending data and | |
442 | * one for receiving data. Each buffer contains a queue of mbufs, | |
443 | * information about the number of mbufs and amount of data in the | |
444 | * queue, and other fields allowing select() statements and notification | |
445 | * on data availability to be implemented. | |
446 | * | |
447 | * Data stored in a socket buffer is maintained as a list of records. | |
448 | * Each record is a list of mbufs chained together with the m_next | |
449 | * field. Records are chained together with the m_nextpkt field. The upper | |
450 | * level routine soreceive() expects the following conventions to be | |
451 | * observed when placing information in the receive buffer: | |
452 | * | |
453 | * 1. If the protocol requires each message be preceded by the sender's | |
454 | * name, then a record containing that name must be present before | |
455 | * any associated data (mbuf's must be of type MT_SONAME). | |
456 | * 2. If the protocol supports the exchange of ``access rights'' (really | |
457 | * just additional data associated with the message), and there are | |
458 | * ``rights'' to be received, then a record containing this data | |
459 | * should be present (mbuf's must be of type MT_RIGHTS). | |
460 | * 3. If a name or rights record exists, then it must be followed by | |
461 | * a data record, perhaps of zero length. | |
462 | * | |
463 | * Before using a new socket structure it is first necessary to reserve | |
464 | * buffer space to the socket, by calling sbreserve(). This should commit | |
465 | * some of the available buffer space in the system buffer pool for the | |
466 | * socket (currently, it does nothing but enforce limits). The space | |
467 | * should be released by calling sbrelease() when the socket is destroyed. | |
468 | */ | |
469 | ||
470 | int | |
471 | soreserve(so, sndcc, rcvcc) | |
472 | register struct socket *so; | |
473 | u_long sndcc, rcvcc; | |
474 | { | |
475 | register struct kextcb *kp; | |
476 | ||
477 | kp = sotokextcb(so); | |
478 | while (kp) | |
479 | { if (kp->e_soif && kp->e_soif->sf_soreserve) | |
480 | { if ((*kp->e_soif->sf_soreserve)(so, sndcc, rcvcc, kp)) | |
481 | return; | |
482 | } | |
483 | kp = kp->e_next; | |
484 | } | |
485 | ||
486 | if (sbreserve(&so->so_snd, sndcc) == 0) | |
487 | goto bad; | |
488 | if (sbreserve(&so->so_rcv, rcvcc) == 0) | |
489 | goto bad2; | |
490 | if (so->so_rcv.sb_lowat == 0) | |
491 | so->so_rcv.sb_lowat = 1; | |
492 | if (so->so_snd.sb_lowat == 0) | |
493 | so->so_snd.sb_lowat = MCLBYTES; | |
494 | if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) | |
495 | so->so_snd.sb_lowat = so->so_snd.sb_hiwat; | |
496 | return (0); | |
497 | bad2: | |
0b4e3aa0 | 498 | selthreadclear(&so->so_snd.sb_sel); |
1c79356b A |
499 | sbrelease(&so->so_snd); |
500 | bad: | |
501 | return (ENOBUFS); | |
502 | } | |
503 | ||
504 | /* | |
505 | * Allot mbufs to a sockbuf. | |
506 | * Attempt to scale mbmax so that mbcnt doesn't become limiting | |
507 | * if buffering efficiency is near the normal case. | |
508 | */ | |
509 | int | |
510 | sbreserve(sb, cc) | |
511 | struct sockbuf *sb; | |
512 | u_long cc; | |
513 | { | |
514 | if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES)) | |
515 | return (0); | |
516 | sb->sb_hiwat = cc; | |
517 | sb->sb_mbmax = min(cc * sb_efficiency, sb_max); | |
518 | if (sb->sb_lowat > sb->sb_hiwat) | |
519 | sb->sb_lowat = sb->sb_hiwat; | |
520 | return (1); | |
521 | } | |
522 | ||
523 | /* | |
524 | * Free mbufs held by a socket, and reserved mbuf space. | |
525 | */ | |
0b4e3aa0 | 526 | /* WARNING needs to do selthreadclear() before calling this */ |
1c79356b A |
527 | void |
528 | sbrelease(sb) | |
529 | struct sockbuf *sb; | |
530 | { | |
531 | ||
532 | sbflush(sb); | |
533 | sb->sb_hiwat = sb->sb_mbmax = 0; | |
0b4e3aa0 A |
534 | #if 0 |
535 | /* this is getting called with bzeroed sb in sorflush */ | |
1c79356b A |
536 | { |
537 | int oldpri = splimp(); | |
538 | selthreadclear(&sb->sb_sel); | |
539 | splx(oldpri); | |
540 | } | |
0b4e3aa0 | 541 | #endif |
1c79356b A |
542 | } |
543 | ||
544 | /* | |
545 | * Routines to add and remove | |
546 | * data from an mbuf queue. | |
547 | * | |
548 | * The routines sbappend() or sbappendrecord() are normally called to | |
549 | * append new mbufs to a socket buffer, after checking that adequate | |
550 | * space is available, comparing the function sbspace() with the amount | |
551 | * of data to be added. sbappendrecord() differs from sbappend() in | |
552 | * that data supplied is treated as the beginning of a new record. | |
553 | * To place a sender's address, optional access rights, and data in a | |
554 | * socket receive buffer, sbappendaddr() should be used. To place | |
555 | * access rights and data in a socket receive buffer, sbappendrights() | |
556 | * should be used. In either case, the new data begins a new record. | |
557 | * Note that unlike sbappend() and sbappendrecord(), these routines check | |
558 | * for the caller that there will be enough space to store the data. | |
559 | * Each fails if there is not enough space, or if it cannot find mbufs | |
560 | * to store additional information in. | |
561 | * | |
562 | * Reliable protocols may use the socket send buffer to hold data | |
563 | * awaiting acknowledgement. Data is normally copied from a socket | |
564 | * send buffer in a protocol with m_copy for output to a peer, | |
565 | * and then removing the data from the socket buffer with sbdrop() | |
566 | * or sbdroprecord() when the data is acknowledged by the peer. | |
567 | */ | |
568 | ||
569 | /* | |
570 | * Append mbuf chain m to the last record in the | |
571 | * socket buffer sb. The additional space associated | |
572 | * the mbuf chain is recorded in sb. Empty mbufs are | |
573 | * discarded and mbufs are compacted where possible. | |
574 | */ | |
575 | void | |
576 | sbappend(sb, m) | |
577 | struct sockbuf *sb; | |
578 | struct mbuf *m; | |
579 | { register struct kextcb *kp; | |
580 | register struct mbuf *n; | |
581 | ||
fa4905b1 A |
582 | |
583 | KERNEL_DEBUG((DBG_FNC_SBAPPEND | DBG_FUNC_START), sb, m->m_len, 0, 0, 0); | |
584 | ||
1c79356b A |
585 | if (m == 0) |
586 | return; | |
587 | kp = sotokextcb(sbtoso(sb)); | |
588 | while (kp) | |
589 | { if (kp->e_sout && kp->e_sout->su_sbappend) | |
590 | { if ((*kp->e_sout->su_sbappend)(sb, m, kp)) | |
591 | return; | |
592 | } | |
593 | kp = kp->e_next; | |
594 | } | |
595 | ||
596 | if (n = sb->sb_mb) { | |
597 | while (n->m_nextpkt) | |
598 | n = n->m_nextpkt; | |
599 | do { | |
600 | if (n->m_flags & M_EOR) { | |
601 | sbappendrecord(sb, m); /* XXXXXX!!!! */ | |
602 | return; | |
603 | } | |
604 | } while (n->m_next && (n = n->m_next)); | |
605 | } | |
606 | sbcompress(sb, m, n); | |
fa4905b1 A |
607 | |
608 | KERNEL_DEBUG((DBG_FNC_SBAPPEND | DBG_FUNC_END), sb, sb->sb_cc, 0, 0, 0); | |
1c79356b A |
609 | } |
610 | ||
611 | #ifdef SOCKBUF_DEBUG | |
612 | void | |
613 | sbcheck(sb) | |
614 | register struct sockbuf *sb; | |
615 | { | |
616 | register struct mbuf *m; | |
617 | register struct mbuf *n = 0; | |
618 | register u_long len = 0, mbcnt = 0; | |
619 | ||
620 | for (m = sb->sb_mb; m; m = n) { | |
621 | n = m->m_nextpkt; | |
622 | for (; m; m = m->m_next) { | |
623 | len += m->m_len; | |
624 | mbcnt += MSIZE; | |
625 | if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ | |
626 | mbcnt += m->m_ext.ext_size; | |
627 | if (m->m_nextpkt) | |
628 | panic("sbcheck nextpkt"); | |
629 | } | |
630 | if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { | |
631 | printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc, | |
632 | mbcnt, sb->sb_mbcnt); | |
633 | panic("sbcheck"); | |
634 | } | |
635 | } | |
636 | #endif | |
637 | ||
638 | /* | |
639 | * As above, except the mbuf chain | |
640 | * begins a new record. | |
641 | */ | |
642 | void | |
643 | sbappendrecord(sb, m0) | |
644 | register struct sockbuf *sb; | |
645 | register struct mbuf *m0; | |
646 | { | |
647 | register struct mbuf *m; | |
648 | register struct kextcb *kp; | |
649 | ||
650 | if (m0 == 0) | |
651 | return; | |
652 | ||
653 | kp = sotokextcb(sbtoso(sb)); | |
654 | while (kp) | |
655 | { if (kp->e_sout && kp->e_sout->su_sbappendrecord) | |
656 | { if ((*kp->e_sout->su_sbappendrecord)(sb, m0, kp)) | |
657 | return; | |
658 | } | |
659 | kp = kp->e_next; | |
660 | } | |
661 | ||
662 | m = sb->sb_mb; | |
663 | if (m) | |
664 | while (m->m_nextpkt) | |
665 | m = m->m_nextpkt; | |
666 | /* | |
667 | * Put the first mbuf on the queue. | |
668 | * Note this permits zero length records. | |
669 | */ | |
670 | sballoc(sb, m0); | |
671 | if (m) | |
672 | m->m_nextpkt = m0; | |
673 | else | |
674 | sb->sb_mb = m0; | |
675 | m = m0->m_next; | |
676 | m0->m_next = 0; | |
677 | if (m && (m0->m_flags & M_EOR)) { | |
678 | m0->m_flags &= ~M_EOR; | |
679 | m->m_flags |= M_EOR; | |
680 | } | |
681 | sbcompress(sb, m, m0); | |
682 | } | |
683 | ||
684 | /* | |
685 | * As above except that OOB data | |
686 | * is inserted at the beginning of the sockbuf, | |
687 | * but after any other OOB data. | |
688 | */ | |
689 | void | |
690 | sbinsertoob(sb, m0) | |
691 | register struct sockbuf *sb; | |
692 | register struct mbuf *m0; | |
693 | { | |
694 | register struct mbuf *m; | |
695 | register struct mbuf **mp; | |
696 | register struct kextcb *kp; | |
697 | ||
698 | if (m0 == 0) | |
699 | return; | |
700 | ||
701 | kp = sotokextcb(sbtoso(sb)); | |
702 | while (kp) | |
703 | { if (kp->e_sout && kp->e_sout->su_sbinsertoob) | |
704 | { if ((*kp->e_sout->su_sbinsertoob)(sb, m0, kp)) | |
705 | return; | |
706 | } | |
707 | kp = kp->e_next; | |
708 | } | |
709 | ||
710 | for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) { | |
711 | m = *mp; | |
712 | again: | |
713 | switch (m->m_type) { | |
714 | ||
715 | case MT_OOBDATA: | |
716 | continue; /* WANT next train */ | |
717 | ||
718 | case MT_CONTROL: | |
719 | m = m->m_next; | |
720 | if (m) | |
721 | goto again; /* inspect THIS train further */ | |
722 | } | |
723 | break; | |
724 | } | |
725 | /* | |
726 | * Put the first mbuf on the queue. | |
727 | * Note this permits zero length records. | |
728 | */ | |
729 | sballoc(sb, m0); | |
730 | m0->m_nextpkt = *mp; | |
731 | *mp = m0; | |
732 | m = m0->m_next; | |
733 | m0->m_next = 0; | |
734 | if (m && (m0->m_flags & M_EOR)) { | |
735 | m0->m_flags &= ~M_EOR; | |
736 | m->m_flags |= M_EOR; | |
737 | } | |
738 | sbcompress(sb, m, m0); | |
739 | } | |
740 | ||
741 | /* | |
742 | * Append address and data, and optionally, control (ancillary) data | |
743 | * to the receive queue of a socket. If present, | |
744 | * m0 must include a packet header with total length. | |
745 | * Returns 0 if no space in sockbuf or insufficient mbufs. | |
746 | */ | |
747 | int | |
748 | sbappendaddr(sb, asa, m0, control) | |
749 | register struct sockbuf *sb; | |
750 | struct sockaddr *asa; | |
751 | struct mbuf *m0, *control; | |
752 | { | |
753 | register struct mbuf *m, *n; | |
754 | int space = asa->sa_len; | |
755 | register struct kextcb *kp; | |
756 | ||
757 | if (m0 && (m0->m_flags & M_PKTHDR) == 0) | |
758 | panic("sbappendaddr"); | |
759 | ||
760 | kp = sotokextcb(sbtoso(sb)); | |
761 | while (kp) | |
762 | { if (kp->e_sout && kp->e_sout->su_sbappendaddr) | |
763 | { if ((*kp->e_sout->su_sbappendaddr)(sb, asa, m0, control, kp)) | |
764 | return 0; | |
765 | } | |
766 | kp = kp->e_next; | |
767 | } | |
768 | ||
769 | if (m0) | |
770 | space += m0->m_pkthdr.len; | |
771 | for (n = control; n; n = n->m_next) { | |
772 | space += n->m_len; | |
773 | if (n->m_next == 0) /* keep pointer to last control buf */ | |
774 | break; | |
775 | } | |
776 | if (space > sbspace(sb)) | |
777 | return (0); | |
778 | if (asa->sa_len > MLEN) | |
779 | return (0); | |
780 | MGET(m, M_DONTWAIT, MT_SONAME); | |
781 | if (m == 0) | |
782 | return (0); | |
783 | m->m_len = asa->sa_len; | |
784 | bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len); | |
785 | if (n) | |
786 | n->m_next = m0; /* concatenate data to control */ | |
787 | else | |
788 | control = m0; | |
789 | m->m_next = control; | |
790 | for (n = m; n; n = n->m_next) | |
791 | sballoc(sb, n); | |
792 | n = sb->sb_mb; | |
793 | if (n) { | |
794 | while (n->m_nextpkt) | |
795 | n = n->m_nextpkt; | |
796 | n->m_nextpkt = m; | |
797 | } else | |
798 | sb->sb_mb = m; | |
799 | postevent(0,sb,EV_RWBYTES); | |
800 | return (1); | |
801 | } | |
802 | ||
803 | int | |
804 | sbappendcontrol(sb, m0, control) | |
805 | struct sockbuf *sb; | |
806 | struct mbuf *control, *m0; | |
807 | { | |
808 | register struct mbuf *m, *n; | |
809 | int space = 0; | |
810 | register struct kextcb *kp; | |
811 | ||
812 | if (control == 0) | |
813 | panic("sbappendcontrol"); | |
814 | ||
815 | kp = sotokextcb(sbtoso(sb)); | |
816 | while (kp) | |
817 | { if (kp->e_sout && kp->e_sout->su_sbappendcontrol) | |
818 | { if ((*kp->e_sout->su_sbappendcontrol)(sb, m0, control, kp)) | |
819 | return 0; | |
820 | } | |
821 | kp = kp->e_next; | |
822 | } | |
823 | ||
824 | for (m = control; ; m = m->m_next) { | |
825 | space += m->m_len; | |
826 | if (m->m_next == 0) | |
827 | break; | |
828 | } | |
829 | n = m; /* save pointer to last control buffer */ | |
830 | for (m = m0; m; m = m->m_next) | |
831 | space += m->m_len; | |
832 | if (space > sbspace(sb)) | |
833 | return (0); | |
834 | n->m_next = m0; /* concatenate data to control */ | |
835 | for (m = control; m; m = m->m_next) | |
836 | sballoc(sb, m); | |
837 | n = sb->sb_mb; | |
838 | if (n) { | |
839 | while (n->m_nextpkt) | |
840 | n = n->m_nextpkt; | |
841 | n->m_nextpkt = control; | |
842 | } else | |
843 | sb->sb_mb = control; | |
844 | postevent(0,sb,EV_RWBYTES); | |
845 | return (1); | |
846 | } | |
847 | ||
848 | /* | |
849 | * Compress mbuf chain m into the socket | |
850 | * buffer sb following mbuf n. If n | |
851 | * is null, the buffer is presumed empty. | |
852 | */ | |
853 | void | |
854 | sbcompress(sb, m, n) | |
855 | register struct sockbuf *sb; | |
856 | register struct mbuf *m, *n; | |
857 | { | |
858 | register int eor = 0; | |
859 | register struct mbuf *o; | |
860 | ||
861 | while (m) { | |
862 | eor |= m->m_flags & M_EOR; | |
863 | if (m->m_len == 0 && | |
864 | (eor == 0 || | |
865 | (((o = m->m_next) || (o = n)) && | |
866 | o->m_type == m->m_type))) { | |
867 | m = m_free(m); | |
868 | continue; | |
869 | } | |
870 | if (n && (n->m_flags & (M_EXT | M_EOR)) == 0 && | |
871 | (n->m_data + n->m_len + m->m_len) < &n->m_dat[MLEN] && | |
872 | n->m_type == m->m_type) { | |
873 | bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, | |
874 | (unsigned)m->m_len); | |
875 | n->m_len += m->m_len; | |
876 | sb->sb_cc += m->m_len; | |
877 | m = m_free(m); | |
878 | continue; | |
879 | } | |
880 | if (n) | |
881 | n->m_next = m; | |
882 | else | |
883 | sb->sb_mb = m; | |
884 | sballoc(sb, m); | |
885 | n = m; | |
886 | m->m_flags &= ~M_EOR; | |
887 | m = m->m_next; | |
888 | n->m_next = 0; | |
889 | } | |
890 | if (eor) { | |
891 | if (n) | |
892 | n->m_flags |= eor; | |
893 | else | |
894 | printf("semi-panic: sbcompress\n"); | |
895 | } | |
896 | postevent(0,sb, EV_RWBYTES); | |
897 | } | |
898 | ||
899 | /* | |
900 | * Free all mbufs in a sockbuf. | |
901 | * Check that all resources are reclaimed. | |
902 | */ | |
903 | void | |
904 | sbflush(sb) | |
905 | register struct sockbuf *sb; | |
906 | { | |
907 | register struct kextcb *kp; | |
908 | ||
909 | kp = sotokextcb(sbtoso(sb)); | |
910 | while (kp) | |
911 | { if (kp->e_sout && kp->e_sout->su_sbflush) | |
912 | { if ((*kp->e_sout->su_sbflush)(sb, kp)) | |
913 | return; | |
914 | } | |
915 | kp = kp->e_next; | |
916 | } | |
917 | ||
918 | if (sb->sb_flags & SB_LOCK) | |
919 | panic("sbflush: locked"); | |
920 | while (sb->sb_mbcnt && sb->sb_cc) | |
921 | sbdrop(sb, (int)sb->sb_cc); | |
922 | if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt) | |
923 | panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt); | |
924 | postevent(0, sb, EV_RWBYTES); | |
925 | } | |
926 | ||
927 | /* | |
928 | * Drop data from (the front of) a sockbuf. | |
929 | */ | |
930 | void | |
931 | sbdrop(sb, len) | |
932 | register struct sockbuf *sb; | |
933 | register int len; | |
934 | { | |
fa4905b1 A |
935 | register struct mbuf *m, *free_list, *ml; |
936 | struct mbuf *next, *last; | |
1c79356b A |
937 | register struct kextcb *kp; |
938 | ||
fa4905b1 A |
939 | KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_START), sb, len, 0, 0, 0); |
940 | ||
1c79356b A |
941 | kp = sotokextcb(sbtoso(sb)); |
942 | while (kp) | |
943 | { if (kp->e_sout && kp->e_sout->su_sbdrop) | |
944 | { if ((*kp->e_sout->su_sbdrop)(sb, len, kp)) | |
945 | return; | |
946 | } | |
947 | kp = kp->e_next; | |
948 | } | |
1c79356b | 949 | next = (m = sb->sb_mb) ? m->m_nextpkt : 0; |
fa4905b1 A |
950 | free_list = last = m; |
951 | ml = (struct mbuf *)0; | |
952 | ||
1c79356b A |
953 | while (len > 0) { |
954 | if (m == 0) { | |
955 | if (next == 0) | |
956 | panic("sbdrop"); | |
fa4905b1 | 957 | m = last = next; |
1c79356b A |
958 | next = m->m_nextpkt; |
959 | continue; | |
960 | } | |
961 | if (m->m_len > len) { | |
962 | m->m_len -= len; | |
963 | m->m_data += len; | |
964 | sb->sb_cc -= len; | |
965 | break; | |
966 | } | |
967 | len -= m->m_len; | |
968 | sbfree(sb, m); | |
fa4905b1 A |
969 | |
970 | ml = m; | |
971 | m = m->m_next; | |
1c79356b A |
972 | } |
973 | while (m && m->m_len == 0) { | |
974 | sbfree(sb, m); | |
fa4905b1 A |
975 | |
976 | ml = m; | |
977 | m = m->m_next; | |
978 | } | |
979 | if (ml) { | |
980 | ml->m_next = (struct mbuf *)0; | |
981 | last->m_nextpkt = (struct mbuf *)0; | |
982 | m_freem_list(free_list); | |
1c79356b A |
983 | } |
984 | if (m) { | |
985 | sb->sb_mb = m; | |
986 | m->m_nextpkt = next; | |
987 | } else | |
988 | sb->sb_mb = next; | |
fa4905b1 | 989 | |
1c79356b | 990 | postevent(0, sb, EV_RWBYTES); |
fa4905b1 A |
991 | |
992 | KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_END), sb, 0, 0, 0, 0); | |
1c79356b A |
993 | } |
994 | ||
995 | /* | |
996 | * Drop a record off the front of a sockbuf | |
997 | * and move the next record to the front. | |
998 | */ | |
999 | void | |
1000 | sbdroprecord(sb) | |
1001 | register struct sockbuf *sb; | |
1002 | { | |
1003 | register struct mbuf *m, *mn; | |
1004 | register struct kextcb *kp; | |
1005 | ||
1006 | kp = sotokextcb(sbtoso(sb)); | |
1007 | while (kp) | |
1008 | { if (kp->e_sout && kp->e_sout->su_sbdroprecord) | |
1009 | { if ((*kp->e_sout->su_sbdroprecord)(sb, kp)) | |
1010 | return; | |
1011 | } | |
1012 | kp = kp->e_next; | |
1013 | } | |
1014 | ||
1015 | m = sb->sb_mb; | |
1016 | if (m) { | |
1017 | sb->sb_mb = m->m_nextpkt; | |
1018 | do { | |
1019 | sbfree(sb, m); | |
1020 | MFREE(m, mn); | |
1021 | } while (m = mn); | |
1022 | } | |
1023 | postevent(0, sb, EV_RWBYTES); | |
1024 | } | |
1025 | ||
1026 | /* | |
1027 | * Create a "control" mbuf containing the specified data | |
1028 | * with the specified type for presentation on a socket buffer. | |
1029 | */ | |
1030 | struct mbuf * | |
1031 | sbcreatecontrol(p, size, type, level) | |
1032 | caddr_t p; | |
1033 | register int size; | |
1034 | int type, level; | |
1035 | { | |
1036 | register struct cmsghdr *cp; | |
1037 | struct mbuf *m; | |
1038 | ||
1039 | if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) | |
1040 | return ((struct mbuf *) NULL); | |
1041 | cp = mtod(m, struct cmsghdr *); | |
1042 | /* XXX check size? */ | |
1043 | (void)memcpy(CMSG_DATA(cp), p, size); | |
1044 | size += sizeof(*cp); | |
1045 | m->m_len = size; | |
1046 | cp->cmsg_len = size; | |
1047 | cp->cmsg_level = level; | |
1048 | cp->cmsg_type = type; | |
1049 | return (m); | |
1050 | } | |
1051 | ||
1052 | /* | |
1053 | * Some routines that return EOPNOTSUPP for entry points that are not | |
1054 | * supported by a protocol. Fill in as needed. | |
1055 | */ | |
1056 | int | |
1057 | pru_abort_notsupp(struct socket *so) | |
1058 | { | |
1059 | return EOPNOTSUPP; | |
1060 | } | |
1061 | ||
1062 | ||
1063 | int | |
1064 | pru_accept_notsupp(struct socket *so, struct sockaddr **nam) | |
1065 | { | |
1066 | return EOPNOTSUPP; | |
1067 | } | |
1068 | ||
1069 | int | |
1070 | pru_attach_notsupp(struct socket *so, int proto, struct proc *p) | |
1071 | { | |
1072 | return EOPNOTSUPP; | |
1073 | } | |
1074 | ||
1075 | int | |
1076 | pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p) | |
1077 | { | |
1078 | return EOPNOTSUPP; | |
1079 | } | |
1080 | ||
1081 | int | |
1082 | pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p) | |
1083 | { | |
1084 | return EOPNOTSUPP; | |
1085 | } | |
1086 | ||
1087 | int | |
1088 | pru_connect2_notsupp(struct socket *so1, struct socket *so2) | |
1089 | { | |
1090 | return EOPNOTSUPP; | |
1091 | } | |
1092 | ||
1093 | int | |
1094 | pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, | |
1095 | struct ifnet *ifp, struct proc *p) | |
1096 | { | |
1097 | return EOPNOTSUPP; | |
1098 | } | |
1099 | ||
1100 | int | |
1101 | pru_detach_notsupp(struct socket *so) | |
1102 | { | |
1103 | return EOPNOTSUPP; | |
1104 | } | |
1105 | ||
1106 | int | |
1107 | pru_disconnect_notsupp(struct socket *so) | |
1108 | { | |
1109 | return EOPNOTSUPP; | |
1110 | } | |
1111 | ||
1112 | int | |
1113 | pru_listen_notsupp(struct socket *so, struct proc *p) | |
1114 | { | |
1115 | return EOPNOTSUPP; | |
1116 | } | |
1117 | ||
1118 | int | |
1119 | pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam) | |
1120 | { | |
1121 | return EOPNOTSUPP; | |
1122 | } | |
1123 | ||
1124 | int | |
1125 | pru_rcvd_notsupp(struct socket *so, int flags) | |
1126 | { | |
1127 | return EOPNOTSUPP; | |
1128 | } | |
1129 | ||
1130 | int | |
1131 | pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) | |
1132 | { | |
1133 | return EOPNOTSUPP; | |
1134 | } | |
1135 | ||
1136 | int | |
1137 | pru_send_notsupp(struct socket *so, int flags, struct mbuf *m, | |
1138 | struct sockaddr *addr, struct mbuf *control, | |
1139 | struct proc *p) | |
1140 | ||
1141 | { | |
1142 | return EOPNOTSUPP; | |
1143 | } | |
1144 | ||
1145 | ||
1146 | /* | |
1147 | * This isn't really a ``null'' operation, but it's the default one | |
1148 | * and doesn't do anything destructive. | |
1149 | */ | |
1150 | int | |
1151 | pru_sense_null(struct socket *so, struct stat *sb) | |
1152 | { | |
1153 | sb->st_blksize = so->so_snd.sb_hiwat; | |
1154 | return 0; | |
1155 | } | |
1156 | ||
1157 | ||
1158 | int pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, | |
1159 | struct uio *uio, struct mbuf *top, | |
1160 | struct mbuf *control, int flags) | |
1161 | ||
1162 | { | |
1163 | return EOPNOTSUPP; | |
1164 | } | |
1165 | ||
1166 | int pru_soreceive_notsupp(struct socket *so, | |
1167 | struct sockaddr **paddr, | |
1168 | struct uio *uio, struct mbuf **mp0, | |
1169 | struct mbuf **controlp, int *flagsp) | |
1170 | { | |
1171 | return EOPNOTSUPP; | |
1172 | } | |
1173 | ||
1174 | int | |
1175 | ||
1176 | pru_shutdown_notsupp(struct socket *so) | |
1177 | { | |
1178 | return EOPNOTSUPP; | |
1179 | } | |
1180 | ||
1181 | int | |
1182 | pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam) | |
1183 | { | |
1184 | return EOPNOTSUPP; | |
1185 | } | |
1186 | ||
1187 | int pru_sosend(struct socket *so, struct sockaddr *addr, | |
1188 | struct uio *uio, struct mbuf *top, | |
1189 | struct mbuf *control, int flags) | |
1190 | { | |
1191 | return EOPNOTSUPP; | |
1192 | } | |
1193 | ||
1194 | int pru_soreceive(struct socket *so, | |
1195 | struct sockaddr **paddr, | |
1196 | struct uio *uio, struct mbuf **mp0, | |
1197 | struct mbuf **controlp, int *flagsp) | |
1198 | { | |
1199 | return EOPNOTSUPP; | |
1200 | } | |
1201 | ||
1202 | ||
1203 | int pru_sopoll_notsupp(struct socket *so, int events, | |
1204 | struct ucred *cred) | |
1205 | { | |
1206 | return EOPNOTSUPP; | |
1207 | } | |
1208 | ||
1209 | ||
1210 | ||
0b4e3aa0 A |
1211 | /* |
1212 | * Do we need to notify the other side when I/O is possible? | |
1213 | */ | |
1214 | ||
1215 | int | |
1216 | sb_notify(struct sockbuf *sb) | |
1217 | { | |
1218 | return ((sb->sb_flags & (SB_WAIT|SB_SEL|SB_ASYNC|SB_UPCALL)) != 0); | |
1219 | } | |
1220 | ||
1221 | /* | |
1222 | * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? | |
1223 | * This is problematical if the fields are unsigned, as the space might | |
1224 | * still be negative (cc > hiwat or mbcnt > mbmax). Should detect | |
1225 | * overflow and return 0. Should use "lmin" but it doesn't exist now. | |
1226 | */ | |
1227 | long | |
1228 | sbspace(struct sockbuf *sb) | |
1229 | { | |
1230 | return ((long) imin((int)(sb->sb_hiwat - sb->sb_cc), | |
1231 | (int)(sb->sb_mbmax - sb->sb_mbcnt))); | |
1232 | } | |
1233 | ||
1234 | /* do we have to send all at once on a socket? */ | |
1235 | int | |
1236 | sosendallatonce(struct socket *so) | |
1237 | { | |
1238 | return (so->so_proto->pr_flags & PR_ATOMIC); | |
1239 | } | |
1240 | ||
1241 | /* can we read something from so? */ | |
1242 | int | |
1243 | soreadable(struct socket *so) | |
1244 | { | |
1245 | return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat || | |
1246 | (so->so_state & SS_CANTRCVMORE) || | |
1247 | so->so_comp.tqh_first || so->so_error); | |
1248 | } | |
1249 | ||
1250 | /* can we write something to so? */ | |
1251 | ||
1252 | int | |
1253 | sowriteable(struct socket *so) | |
1254 | { | |
1255 | return ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && | |
1256 | ((so->so_state&SS_ISCONNECTED) || | |
1257 | (so->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || | |
1258 | (so->so_state & SS_CANTSENDMORE) || | |
1259 | so->so_error); | |
1260 | } | |
1261 | ||
1262 | /* adjust counters in sb reflecting allocation of m */ | |
1263 | ||
1264 | void | |
1265 | sballoc(struct sockbuf *sb, struct mbuf *m) | |
1266 | { | |
1267 | sb->sb_cc += m->m_len; | |
1268 | sb->sb_mbcnt += MSIZE; | |
1269 | if (m->m_flags & M_EXT) | |
1270 | sb->sb_mbcnt += m->m_ext.ext_size; | |
1271 | } | |
1272 | ||
1273 | /* adjust counters in sb reflecting freeing of m */ | |
1274 | void | |
1275 | sbfree(struct sockbuf *sb, struct mbuf *m) | |
1276 | { | |
1277 | sb->sb_cc -= m->m_len; | |
1278 | sb->sb_mbcnt -= MSIZE; | |
1279 | if (m->m_flags & M_EXT) | |
1280 | sb->sb_mbcnt -= m->m_ext.ext_size; | |
1281 | } | |
1282 | ||
1283 | /* | |
1284 | * Set lock on sockbuf sb; sleep if lock is already held. | |
1285 | * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. | |
1286 | * Returns error without lock if sleep is interrupted. | |
1287 | */ | |
1288 | int | |
1289 | sblock(struct sockbuf *sb, int wf) | |
1290 | { | |
1291 | return(sb->sb_flags & SB_LOCK ? | |
1292 | ((wf == M_WAIT) ? sb_lock(sb) : EWOULDBLOCK) : | |
1293 | (sb->sb_flags |= SB_LOCK), 0); | |
1294 | } | |
1295 | ||
1296 | /* release lock on sockbuf sb */ | |
1297 | void | |
1298 | sbunlock(struct sockbuf *sb) | |
1299 | { | |
1300 | sb->sb_flags &= ~SB_LOCK; | |
1301 | if (sb->sb_flags & SB_WANT) { | |
1302 | sb->sb_flags &= ~SB_WANT; | |
1303 | wakeup((caddr_t)&(sb)->sb_flags); | |
1304 | } | |
1305 | } | |
1306 | ||
1307 | void | |
1308 | sorwakeup(struct socket * so) | |
1309 | { | |
1310 | if (sb_notify(&so->so_rcv)) | |
1311 | sowakeup(so, &so->so_rcv); | |
1312 | } | |
1313 | ||
1314 | void | |
1315 | sowwakeup(struct socket * so) | |
1316 | { | |
1317 | if (sb_notify(&so->so_snd)) | |
1318 | sowakeup(so, &so->so_snd); | |
1319 | } | |
1320 | ||
1c79356b A |
1321 | /* |
1322 | * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. | |
1323 | */ | |
1324 | struct sockaddr * | |
1325 | dup_sockaddr(sa, canwait) | |
1326 | struct sockaddr *sa; | |
1327 | int canwait; | |
1328 | { | |
1329 | struct sockaddr *sa2; | |
1330 | ||
1331 | MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME, | |
1332 | canwait ? M_WAITOK : M_NOWAIT); | |
1333 | if (sa2) | |
1334 | bcopy(sa, sa2, sa->sa_len); | |
1335 | return sa2; | |
1336 | } | |
1337 | ||
1338 | /* | |
1339 | * Create an external-format (``xsocket'') structure using the information | |
1340 | * in the kernel-format socket structure pointed to by so. This is done | |
1341 | * to reduce the spew of irrelevant information over this interface, | |
1342 | * to isolate user code from changes in the kernel structure, and | |
1343 | * potentially to provide information-hiding if we decide that | |
1344 | * some of this information should be hidden from users. | |
1345 | */ | |
1346 | void | |
1347 | sotoxsocket(struct socket *so, struct xsocket *xso) | |
1348 | { | |
1349 | xso->xso_len = sizeof *xso; | |
1350 | xso->xso_so = so; | |
1351 | xso->so_type = so->so_type; | |
1352 | xso->so_options = so->so_options; | |
1353 | xso->so_linger = so->so_linger; | |
1354 | xso->so_state = so->so_state; | |
1355 | xso->so_pcb = so->so_pcb; | |
1356 | xso->xso_protocol = so->so_proto->pr_protocol; | |
1357 | xso->xso_family = so->so_proto->pr_domain->dom_family; | |
1358 | xso->so_qlen = so->so_qlen; | |
1359 | xso->so_incqlen = so->so_incqlen; | |
1360 | xso->so_qlimit = so->so_qlimit; | |
1361 | xso->so_timeo = so->so_timeo; | |
1362 | xso->so_error = so->so_error; | |
1363 | xso->so_pgid = so->so_pgid; | |
1364 | xso->so_oobmark = so->so_oobmark; | |
1365 | sbtoxsockbuf(&so->so_snd, &xso->so_snd); | |
1366 | sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); | |
1367 | xso->so_uid = so->so_uid; | |
1368 | } | |
1369 | ||
1370 | /* | |
1371 | * This does the same for sockbufs. Note that the xsockbuf structure, | |
1372 | * since it is always embedded in a socket, does not include a self | |
1373 | * pointer nor a length. We make this entry point public in case | |
1374 | * some other mechanism needs it. | |
1375 | */ | |
1376 | void | |
1377 | sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) | |
1378 | { | |
1379 | xsb->sb_cc = sb->sb_cc; | |
1380 | xsb->sb_hiwat = sb->sb_hiwat; | |
1381 | xsb->sb_mbcnt = sb->sb_mbcnt; | |
1382 | xsb->sb_mbmax = sb->sb_mbmax; | |
1383 | xsb->sb_lowat = sb->sb_lowat; | |
1384 | xsb->sb_flags = sb->sb_flags; | |
1385 | xsb->sb_timeo = sb->sb_timeo; | |
1386 | } | |
1387 | ||
1388 | /* | |
1389 | * Here is the definition of some of the basic objects in the kern.ipc | |
1390 | * branch of the MIB. | |
1391 | */ | |
1392 | ||
1393 | ||
1394 | SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC"); | |
1395 | ||
1396 | /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ | |
1397 | static int dummy; | |
1398 | SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); | |
1399 | ||
1400 | SYSCTL_INT(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW, &sb_max, 0, ""); | |
1401 | SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD, &maxsockets, 0, ""); | |
1402 | SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, | |
1403 | &sb_efficiency, 0, ""); | |
1404 | SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD, &nmbclusters, 0, ""); | |
1405 |