35 */ 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/domain.h> 40#include <sys/kernel.h> 41#include <sys/proc.h> 42#include <sys/malloc.h> 43#include <sys/mbuf.h> 44#include <sys/protosw.h> 45#include <sys/stat.h> 46#include <sys/socket.h> 47#include <sys/socketvar.h> 48#include <sys/signalvar.h> 49#include <sys/sysctl.h> 50 51/* 52 * Primitive routines for operating on sockets and socket buffers 53 */ 54 55u_long sb_max = SB_MAX; /* XXX should be static */ 56 57static u_long sb_efficiency = 8; /* parameter for sbreserve() */ 58 59/* 60 * Procedures to manipulate state flags of socket 61 * and do appropriate wakeups. Normal sequence from the 62 * active (originating) side is that soisconnecting() is 63 * called during processing of connect() call, 64 * resulting in an eventual call to soisconnected() if/when the 65 * connection is established. When the connection is torn down 66 * soisdisconnecting() is called during processing of disconnect() call, 67 * and soisdisconnected() is called when the connection to the peer 68 * is totally severed. The semantics of these routines are such that 69 * connectionless protocols can call soisconnected() and soisdisconnected() 70 * only, bypassing the in-progress calls when setting up a ``connection'' 71 * takes no time. 72 * 73 * From the passive side, a socket is created with 74 * two queues of sockets: so_q0 for connections in progress 75 * and so_q for connections already made and awaiting user acceptance. 76 * As a protocol is preparing incoming connections, it creates a socket 77 * structure queued on so_q0 by calling sonewconn(). When the connection 78 * is established, soisconnected() is called, and transfers the 79 * socket structure to so_q, making it available to accept(). 80 * 81 * If a socket is closed with sockets on either 82 * so_q0 or so_q, these sockets are dropped. 83 * 84 * If higher level protocols are implemented in 85 * the kernel, the wakeups done here will sometimes 86 * cause software-interrupt process scheduling. 87 */ 88 89void 90soisconnecting(so) 91 register struct socket *so; 92{ 93 94 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 95 so->so_state |= SS_ISCONNECTING; 96} 97 98void 99soisconnected(so) 100 register struct socket *so; 101{ 102 register struct socket *head = so->so_head; 103 104 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 105 so->so_state |= SS_ISCONNECTED; 106 if (head && (so->so_state & SS_INCOMP)) { 107 TAILQ_REMOVE(&head->so_incomp, so, so_list); 108 head->so_incqlen--; 109 so->so_state &= ~SS_INCOMP; 110 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 111 so->so_state |= SS_COMP; 112 sorwakeup(head); 113 wakeup_one(&head->so_timeo); 114 } else { 115 wakeup(&so->so_timeo); 116 sorwakeup(so); 117 sowwakeup(so); 118 } 119} 120 121void 122soisdisconnecting(so) 123 register struct socket *so; 124{ 125 126 so->so_state &= ~SS_ISCONNECTING; 127 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 128 wakeup((caddr_t)&so->so_timeo); 129 sowwakeup(so); 130 sorwakeup(so); 131} 132 133void 134soisdisconnected(so) 135 register struct socket *so; 136{ 137 138 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 139 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); 140 wakeup((caddr_t)&so->so_timeo); 141 sowwakeup(so); 142 sorwakeup(so); 143} 144 145/* 146 * Return a random connection that hasn't been serviced yet and 147 * is eligible for discard. There is a one in qlen chance that 148 * we will return a null, saying that there are no dropable 149 * requests. In this case, the protocol specific code should drop 150 * the new request. This insures fairness. 151 * 152 * This may be used in conjunction with protocol specific queue 153 * congestion routines. 154 */ 155struct socket * 156sodropablereq(head) 157 register struct socket *head; 158{ 159 register struct socket *so; 160 unsigned int i, j, qlen; 161 static int rnd; 162 static struct timeval old_runtime; 163 static unsigned int cur_cnt, old_cnt; 164 struct timeval tv; 165 166 getmicrouptime(&tv); 167 if ((i = (tv.tv_sec - old_runtime.tv_sec)) != 0) { 168 old_runtime = tv; 169 old_cnt = cur_cnt / i; 170 cur_cnt = 0; 171 } 172 173 so = TAILQ_FIRST(&head->so_incomp); 174 if (!so) 175 return (so); 176 177 qlen = head->so_incqlen; 178 if (++cur_cnt > qlen || old_cnt > qlen) { 179 rnd = (314159 * rnd + 66329) & 0xffff; 180 j = ((qlen + 1) * rnd) >> 16; 181 182 while (j-- && so) 183 so = TAILQ_NEXT(so, so_list); 184 } 185 186 return (so); 187} 188 189/* 190 * When an attempt at a new connection is noted on a socket 191 * which accepts connections, sonewconn is called. If the 192 * connection is possible (subject to space constraints, etc.) 193 * then we allocate a new structure, propoerly linked into the 194 * data structure of the original socket, and return this. 195 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. 196 */ 197struct socket * 198sonewconn(head, connstatus) 199 register struct socket *head; 200 int connstatus; 201{ 202 register struct socket *so; 203 204 if (head->so_qlen > 3 * head->so_qlimit / 2) 205 return ((struct socket *)0); 206 so = soalloc(0); 207 if (so == NULL) 208 return ((struct socket *)0); 209 so->so_head = head; 210 so->so_type = head->so_type; 211 so->so_options = head->so_options &~ SO_ACCEPTCONN; 212 so->so_linger = head->so_linger; 213 so->so_state = head->so_state | SS_NOFDREF; 214 so->so_proto = head->so_proto; 215 so->so_timeo = head->so_timeo; 216 so->so_uid = head->so_uid; 217 (void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat); 218 219 if ((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) { 220 sodealloc(so); 221 return ((struct socket *)0); 222 } 223 224 if (connstatus) { 225 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 226 so->so_state |= SS_COMP; 227 } else { 228 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); 229 so->so_state |= SS_INCOMP; 230 head->so_incqlen++; 231 } 232 head->so_qlen++; 233 if (connstatus) { 234 sorwakeup(head); 235 wakeup((caddr_t)&head->so_timeo); 236 so->so_state |= connstatus; 237 } 238 return (so); 239} 240 241/* 242 * Socantsendmore indicates that no more data will be sent on the 243 * socket; it would normally be applied to a socket when the user 244 * informs the system that no more data is to be sent, by the protocol 245 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 246 * will be received, and will normally be applied to the socket by a 247 * protocol when it detects that the peer will send no more data. 248 * Data queued for reading in the socket may yet be read. 249 */ 250 251void 252socantsendmore(so) 253 struct socket *so; 254{ 255 256 so->so_state |= SS_CANTSENDMORE; 257 sowwakeup(so); 258} 259 260void 261socantrcvmore(so) 262 struct socket *so; 263{ 264 265 so->so_state |= SS_CANTRCVMORE; 266 sorwakeup(so); 267} 268 269/* 270 * Wait for data to arrive at/drain from a socket buffer. 271 */ 272int 273sbwait(sb) 274 struct sockbuf *sb; 275{ 276 277 sb->sb_flags |= SB_WAIT; 278 return (tsleep((caddr_t)&sb->sb_cc, 279 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", 280 sb->sb_timeo)); 281} 282 283/* 284 * Lock a sockbuf already known to be locked; 285 * return any error returned from sleep (EINTR). 286 */ 287int 288sb_lock(sb) 289 register struct sockbuf *sb; 290{ 291 int error; 292 293 while (sb->sb_flags & SB_LOCK) { 294 sb->sb_flags |= SB_WANT; 295 error = tsleep((caddr_t)&sb->sb_flags, 296 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH, 297 "sblock", 0); 298 if (error) 299 return (error); 300 } 301 sb->sb_flags |= SB_LOCK; 302 return (0); 303} 304 305/* 306 * Wakeup processes waiting on a socket buffer. 307 * Do asynchronous notification via SIGIO 308 * if the socket has the SS_ASYNC flag set. 309 */ 310void 311sowakeup(so, sb) 312 register struct socket *so; 313 register struct sockbuf *sb; 314{ 315 selwakeup(&sb->sb_sel); 316 sb->sb_flags &= ~SB_SEL; 317 if (sb->sb_flags & SB_WAIT) { 318 sb->sb_flags &= ~SB_WAIT; 319 wakeup((caddr_t)&sb->sb_cc); 320 } 321 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) 322 pgsigio(so->so_sigio, SIGIO, 0); 323 if (sb->sb_flags & SB_UPCALL) 324 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT); 325} 326 327/* 328 * Socket buffer (struct sockbuf) utility routines. 329 * 330 * Each socket contains two socket buffers: one for sending data and 331 * one for receiving data. Each buffer contains a queue of mbufs, 332 * information about the number of mbufs and amount of data in the 333 * queue, and other fields allowing select() statements and notification 334 * on data availability to be implemented. 335 * 336 * Data stored in a socket buffer is maintained as a list of records. 337 * Each record is a list of mbufs chained together with the m_next 338 * field. Records are chained together with the m_nextpkt field. The upper 339 * level routine soreceive() expects the following conventions to be 340 * observed when placing information in the receive buffer: 341 * 342 * 1. If the protocol requires each message be preceded by the sender's 343 * name, then a record containing that name must be present before 344 * any associated data (mbuf's must be of type MT_SONAME). 345 * 2. If the protocol supports the exchange of ``access rights'' (really 346 * just additional data associated with the message), and there are 347 * ``rights'' to be received, then a record containing this data 348 * should be present (mbuf's must be of type MT_RIGHTS). 349 * 3. If a name or rights record exists, then it must be followed by 350 * a data record, perhaps of zero length. 351 * 352 * Before using a new socket structure it is first necessary to reserve 353 * buffer space to the socket, by calling sbreserve(). This should commit 354 * some of the available buffer space in the system buffer pool for the 355 * socket (currently, it does nothing but enforce limits). The space 356 * should be released by calling sbrelease() when the socket is destroyed. 357 */ 358 359int 360soreserve(so, sndcc, rcvcc) 361 register struct socket *so; 362 u_long sndcc, rcvcc; 363{ 364 365 if (sbreserve(&so->so_snd, sndcc) == 0) 366 goto bad; 367 if (sbreserve(&so->so_rcv, rcvcc) == 0) 368 goto bad2; 369 if (so->so_rcv.sb_lowat == 0) 370 so->so_rcv.sb_lowat = 1; 371 if (so->so_snd.sb_lowat == 0) 372 so->so_snd.sb_lowat = MCLBYTES; 373 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 374 so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 375 return (0); 376bad2: 377 sbrelease(&so->so_snd); 378bad: 379 return (ENOBUFS); 380} 381 382/* 383 * Allot mbufs to a sockbuf. 384 * Attempt to scale mbmax so that mbcnt doesn't become limiting 385 * if buffering efficiency is near the normal case. 386 */ 387int 388sbreserve(sb, cc) 389 struct sockbuf *sb; 390 u_long cc; 391{ 392 if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES)) 393 return (0); 394 sb->sb_hiwat = cc; 395 sb->sb_mbmax = min(cc * sb_efficiency, sb_max); 396 if (sb->sb_lowat > sb->sb_hiwat) 397 sb->sb_lowat = sb->sb_hiwat; 398 return (1); 399} 400 401/* 402 * Free mbufs held by a socket, and reserved mbuf space. 403 */ 404void 405sbrelease(sb) 406 struct sockbuf *sb; 407{ 408 409 sbflush(sb); 410 sb->sb_hiwat = sb->sb_mbmax = 0; 411} 412 413/* 414 * Routines to add and remove 415 * data from an mbuf queue. 416 * 417 * The routines sbappend() or sbappendrecord() are normally called to 418 * append new mbufs to a socket buffer, after checking that adequate 419 * space is available, comparing the function sbspace() with the amount 420 * of data to be added. sbappendrecord() differs from sbappend() in 421 * that data supplied is treated as the beginning of a new record. 422 * To place a sender's address, optional access rights, and data in a 423 * socket receive buffer, sbappendaddr() should be used. To place 424 * access rights and data in a socket receive buffer, sbappendrights() 425 * should be used. In either case, the new data begins a new record. 426 * Note that unlike sbappend() and sbappendrecord(), these routines check 427 * for the caller that there will be enough space to store the data. 428 * Each fails if there is not enough space, or if it cannot find mbufs 429 * to store additional information in. 430 * 431 * Reliable protocols may use the socket send buffer to hold data 432 * awaiting acknowledgement. Data is normally copied from a socket 433 * send buffer in a protocol with m_copy for output to a peer, 434 * and then removing the data from the socket buffer with sbdrop() 435 * or sbdroprecord() when the data is acknowledged by the peer. 436 */ 437 438/* 439 * Append mbuf chain m to the last record in the 440 * socket buffer sb. The additional space associated 441 * the mbuf chain is recorded in sb. Empty mbufs are 442 * discarded and mbufs are compacted where possible. 443 */ 444void 445sbappend(sb, m) 446 struct sockbuf *sb; 447 struct mbuf *m; 448{ 449 register struct mbuf *n; 450 451 if (m == 0) 452 return; 453 n = sb->sb_mb; 454 if (n) { 455 while (n->m_nextpkt) 456 n = n->m_nextpkt; 457 do { 458 if (n->m_flags & M_EOR) { 459 sbappendrecord(sb, m); /* XXXXXX!!!! */ 460 return; 461 } 462 } while (n->m_next && (n = n->m_next)); 463 } 464 sbcompress(sb, m, n); 465} 466 467#ifdef SOCKBUF_DEBUG 468void 469sbcheck(sb) 470 register struct sockbuf *sb; 471{ 472 register struct mbuf *m; 473 register struct mbuf *n = 0; 474 register u_long len = 0, mbcnt = 0; 475 476 for (m = sb->sb_mb; m; m = n) { 477 n = m->m_nextpkt; 478 for (; m; m = m->m_next) { 479 len += m->m_len; 480 mbcnt += MSIZE; 481 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 482 mbcnt += m->m_ext.ext_size; 483 } 484 } 485 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 486 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc, 487 mbcnt, sb->sb_mbcnt); 488 panic("sbcheck"); 489 } 490} 491#endif 492 493/* 494 * As above, except the mbuf chain 495 * begins a new record. 496 */ 497void 498sbappendrecord(sb, m0) 499 register struct sockbuf *sb; 500 register struct mbuf *m0; 501{ 502 register struct mbuf *m; 503 504 if (m0 == 0) 505 return; 506 m = sb->sb_mb; 507 if (m) 508 while (m->m_nextpkt) 509 m = m->m_nextpkt; 510 /* 511 * Put the first mbuf on the queue. 512 * Note this permits zero length records. 513 */ 514 sballoc(sb, m0); 515 if (m) 516 m->m_nextpkt = m0; 517 else 518 sb->sb_mb = m0; 519 m = m0->m_next; 520 m0->m_next = 0; 521 if (m && (m0->m_flags & M_EOR)) { 522 m0->m_flags &= ~M_EOR; 523 m->m_flags |= M_EOR; 524 } 525 sbcompress(sb, m, m0); 526} 527 528/* 529 * As above except that OOB data 530 * is inserted at the beginning of the sockbuf, 531 * but after any other OOB data. 532 */ 533void 534sbinsertoob(sb, m0) 535 register struct sockbuf *sb; 536 register struct mbuf *m0; 537{ 538 register struct mbuf *m; 539 register struct mbuf **mp; 540 541 if (m0 == 0) 542 return; 543 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) { 544 m = *mp; 545 again: 546 switch (m->m_type) { 547 548 case MT_OOBDATA: 549 continue; /* WANT next train */ 550 551 case MT_CONTROL: 552 m = m->m_next; 553 if (m) 554 goto again; /* inspect THIS train further */ 555 } 556 break; 557 } 558 /* 559 * Put the first mbuf on the queue. 560 * Note this permits zero length records. 561 */ 562 sballoc(sb, m0); 563 m0->m_nextpkt = *mp; 564 *mp = m0; 565 m = m0->m_next; 566 m0->m_next = 0; 567 if (m && (m0->m_flags & M_EOR)) { 568 m0->m_flags &= ~M_EOR; 569 m->m_flags |= M_EOR; 570 } 571 sbcompress(sb, m, m0); 572} 573 574/* 575 * Append address and data, and optionally, control (ancillary) data 576 * to the receive queue of a socket. If present, 577 * m0 must include a packet header with total length. 578 * Returns 0 if no space in sockbuf or insufficient mbufs. 579 */ 580int 581sbappendaddr(sb, asa, m0, control) 582 register struct sockbuf *sb; 583 struct sockaddr *asa; 584 struct mbuf *m0, *control; 585{ 586 register struct mbuf *m, *n; 587 int space = asa->sa_len; 588 589if (m0 && (m0->m_flags & M_PKTHDR) == 0) 590panic("sbappendaddr"); 591 if (m0) 592 space += m0->m_pkthdr.len; 593 for (n = control; n; n = n->m_next) { 594 space += n->m_len; 595 if (n->m_next == 0) /* keep pointer to last control buf */ 596 break; 597 } 598 if (space > sbspace(sb)) 599 return (0); 600 if (asa->sa_len > MLEN) 601 return (0); 602 MGET(m, M_DONTWAIT, MT_SONAME); 603 if (m == 0) 604 return (0); 605 m->m_len = asa->sa_len; 606 bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len); 607 if (n) 608 n->m_next = m0; /* concatenate data to control */ 609 else 610 control = m0; 611 m->m_next = control; 612 for (n = m; n; n = n->m_next) 613 sballoc(sb, n); 614 n = sb->sb_mb; 615 if (n) { 616 while (n->m_nextpkt) 617 n = n->m_nextpkt; 618 n->m_nextpkt = m; 619 } else 620 sb->sb_mb = m; 621 return (1); 622} 623 624int 625sbappendcontrol(sb, m0, control) 626 struct sockbuf *sb; 627 struct mbuf *control, *m0; 628{ 629 register struct mbuf *m, *n; 630 int space = 0; 631 632 if (control == 0) 633 panic("sbappendcontrol"); 634 for (m = control; ; m = m->m_next) { 635 space += m->m_len; 636 if (m->m_next == 0) 637 break; 638 } 639 n = m; /* save pointer to last control buffer */ 640 for (m = m0; m; m = m->m_next) 641 space += m->m_len; 642 if (space > sbspace(sb)) 643 return (0); 644 n->m_next = m0; /* concatenate data to control */ 645 for (m = control; m; m = m->m_next) 646 sballoc(sb, m); 647 n = sb->sb_mb; 648 if (n) { 649 while (n->m_nextpkt) 650 n = n->m_nextpkt; 651 n->m_nextpkt = control; 652 } else 653 sb->sb_mb = control; 654 return (1); 655} 656 657/* 658 * Compress mbuf chain m into the socket 659 * buffer sb following mbuf n. If n 660 * is null, the buffer is presumed empty. 661 */ 662void 663sbcompress(sb, m, n) 664 register struct sockbuf *sb; 665 register struct mbuf *m, *n; 666{ 667 register int eor = 0; 668 register struct mbuf *o; 669 670 while (m) { 671 eor |= m->m_flags & M_EOR; 672 if (m->m_len == 0 && 673 (eor == 0 || 674 (((o = m->m_next) || (o = n)) && 675 o->m_type == m->m_type))) { 676 m = m_free(m); 677 continue; 678 } 679 if (n && (n->m_flags & (M_EXT | M_EOR)) == 0 && 680 (n->m_data + n->m_len + m->m_len) < &n->m_dat[MLEN] && 681 n->m_type == m->m_type) { 682 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 683 (unsigned)m->m_len); 684 n->m_len += m->m_len; 685 sb->sb_cc += m->m_len; 686 m = m_free(m); 687 continue; 688 } 689 if (n) 690 n->m_next = m; 691 else 692 sb->sb_mb = m; 693 sballoc(sb, m); 694 n = m; 695 m->m_flags &= ~M_EOR; 696 m = m->m_next; 697 n->m_next = 0; 698 } 699 if (eor) { 700 if (n) 701 n->m_flags |= eor; 702 else 703 printf("semi-panic: sbcompress\n"); 704 } 705} 706 707/* 708 * Free all mbufs in a sockbuf. 709 * Check that all resources are reclaimed. 710 */ 711void 712sbflush(sb) 713 register struct sockbuf *sb; 714{ 715 716 if (sb->sb_flags & SB_LOCK) 717 panic("sbflush: locked"); 718 while (sb->sb_mbcnt && sb->sb_cc) 719 sbdrop(sb, (int)sb->sb_cc); 720 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt) 721 panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt); 722} 723 724/* 725 * Drop data from (the front of) a sockbuf. 726 */ 727void 728sbdrop(sb, len) 729 register struct sockbuf *sb; 730 register int len; 731{ 732 register struct mbuf *m, *mn; 733 struct mbuf *next; 734 735 next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 736 while (len > 0) { 737 if (m == 0) { 738 if (next == 0) 739 panic("sbdrop"); 740 m = next; 741 next = m->m_nextpkt; 742 continue; 743 } 744 if (m->m_len > len) { 745 m->m_len -= len; 746 m->m_data += len; 747 sb->sb_cc -= len; 748 break; 749 } 750 len -= m->m_len; 751 sbfree(sb, m); 752 MFREE(m, mn); 753 m = mn; 754 } 755 while (m && m->m_len == 0) { 756 sbfree(sb, m); 757 MFREE(m, mn); 758 m = mn; 759 } 760 if (m) { 761 sb->sb_mb = m; 762 m->m_nextpkt = next; 763 } else 764 sb->sb_mb = next; 765} 766 767/* 768 * Drop a record off the front of a sockbuf 769 * and move the next record to the front. 770 */ 771void 772sbdroprecord(sb) 773 register struct sockbuf *sb; 774{ 775 register struct mbuf *m, *mn; 776 777 m = sb->sb_mb; 778 if (m) { 779 sb->sb_mb = m->m_nextpkt; 780 do { 781 sbfree(sb, m); 782 MFREE(m, mn); 783 m = mn; 784 } while (m); 785 } 786} 787 788/* 789 * Create a "control" mbuf containing the specified data 790 * with the specified type for presentation on a socket buffer. 791 */ 792struct mbuf * 793sbcreatecontrol(p, size, type, level) 794 caddr_t p; 795 register int size; 796 int type, level; 797{ 798 register struct cmsghdr *cp; 799 struct mbuf *m; 800 801 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) 802 return ((struct mbuf *) NULL); 803 cp = mtod(m, struct cmsghdr *); 804 /* XXX check size? */ 805 (void)memcpy(CMSG_DATA(cp), p, size); 806 size += sizeof(*cp); 807 m->m_len = size; 808 cp->cmsg_len = size; 809 cp->cmsg_level = level; 810 cp->cmsg_type = type; 811 return (m); 812} 813 814/* 815 * Some routines that return EOPNOTSUPP for entry points that are not 816 * supported by a protocol. Fill in as needed. 817 */ 818int 819pru_accept_notsupp(struct socket *so, struct sockaddr **nam) 820{ 821 return EOPNOTSUPP; 822} 823 824int 825pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p) 826{ 827 return EOPNOTSUPP; 828} 829 830int 831pru_connect2_notsupp(struct socket *so1, struct socket *so2) 832{ 833 return EOPNOTSUPP; 834} 835 836int 837pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, 838 struct ifnet *ifp, struct proc *p) 839{ 840 return EOPNOTSUPP; 841} 842 843int 844pru_listen_notsupp(struct socket *so, struct proc *p) 845{ 846 return EOPNOTSUPP; 847} 848 849int 850pru_rcvd_notsupp(struct socket *so, int flags) 851{ 852 return EOPNOTSUPP; 853} 854 855int 856pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) 857{ 858 return EOPNOTSUPP; 859} 860 861/* 862 * This isn't really a ``null'' operation, but it's the default one 863 * and doesn't do anything destructive. 864 */ 865int 866pru_sense_null(struct socket *so, struct stat *sb) 867{ 868 sb->st_blksize = so->so_snd.sb_hiwat; 869 return 0; 870} 871 872/* 873 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. 874 */ 875struct sockaddr * 876dup_sockaddr(sa, canwait) 877 struct sockaddr *sa; 878 int canwait; 879{ 880 struct sockaddr *sa2; 881 882 MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME, 883 canwait ? M_WAITOK : M_NOWAIT); 884 if (sa2) 885 bcopy(sa, sa2, sa->sa_len); 886 return sa2; 887} 888 889/* 890 * Create an external-format (``xsocket'') structure using the information 891 * in the kernel-format socket structure pointed to by so. This is done 892 * to reduce the spew of irrelevant information over this interface, 893 * to isolate user code from changes in the kernel structure, and 894 * potentially to provide information-hiding if we decide that 895 * some of this information should be hidden from users. 896 */ 897void 898sotoxsocket(struct socket *so, struct xsocket *xso) 899{ 900 xso->xso_len = sizeof *xso; 901 xso->xso_so = so; 902 xso->so_type = so->so_type; 903 xso->so_options = so->so_options; 904 xso->so_linger = so->so_linger; 905 xso->so_state = so->so_state; 906 xso->so_pcb = so->so_pcb; 907 xso->xso_protocol = so->so_proto->pr_protocol; 908 xso->xso_family = so->so_proto->pr_domain->dom_family; 909 xso->so_qlen = so->so_qlen; 910 xso->so_incqlen = so->so_incqlen; 911 xso->so_qlimit = so->so_qlimit; 912 xso->so_timeo = so->so_timeo; 913 xso->so_error = so->so_error; 914 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0; 915 xso->so_oobmark = so->so_oobmark; 916 sbtoxsockbuf(&so->so_snd, &xso->so_snd); 917 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 918 xso->so_uid = so->so_uid; 919} 920 921/* 922 * This does the same for sockbufs. Note that the xsockbuf structure, 923 * since it is always embedded in a socket, does not include a self 924 * pointer nor a length. We make this entry point public in case 925 * some other mechanism needs it. 926 */ 927void 928sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) 929{ 930 xsb->sb_cc = sb->sb_cc; 931 xsb->sb_hiwat = sb->sb_hiwat; 932 xsb->sb_mbcnt = sb->sb_mbcnt; 933 xsb->sb_mbmax = sb->sb_mbmax; 934 xsb->sb_lowat = sb->sb_lowat; 935 xsb->sb_flags = sb->sb_flags; 936 xsb->sb_timeo = sb->sb_timeo; 937} 938 939/* 940 * Here is the definition of some of the basic objects in the kern.ipc 941 * branch of the MIB. 942 */ 943SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC"); 944 945/* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ 946static int dummy; 947SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); 948
|