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