raw_cb.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1980, 1986, 1993
  5. * The Regents of the University of California.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * @(#)raw_cb.c 8.1 (Berkeley) 6/10/93
  33. * $FreeBSD$
  34. */
  35. #include <sys/param.h>
  36. #include <sys/domain.h>
  37. #include <sys/lock.h>
  38. #include <sys/kernel.h>
  39. #include <sys/malloc.h>
  40. #include <sys/mutex.h>
  41. #include <sys/protosw.h>
  42. #include <sys/socket.h>
  43. #include <sys/socketvar.h>
  44. #include <sys/sysctl.h>
  45. #include <sys/systm.h>
  46. #include <net/if.h>
  47. #include <net/vnet.h>
  48. #include <net/raw_cb.h>
  49. /*
  50. * Routines to manage the raw protocol control blocks.
  51. *
  52. * TODO:
  53. * hash lookups by protocol family/protocol + address family
  54. * take care of unique address problems per AF?
  55. * redo address binding to allow wildcards
  56. */
  57. struct mtx rawcb_mtx;
  58. VNET_DEFINE(struct rawcb_list_head, rawcb_list);
  59. static SYSCTL_NODE(_net, OID_AUTO, raw, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  60. "Raw socket infrastructure");
  61. static u_long raw_sendspace = RAWSNDQ;
  62. SYSCTL_ULONG(_net_raw, OID_AUTO, sendspace, CTLFLAG_RW, &raw_sendspace, 0,
  63. "Default raw socket send space");
  64. static u_long raw_recvspace = RAWRCVQ;
  65. SYSCTL_ULONG(_net_raw, OID_AUTO, recvspace, CTLFLAG_RW, &raw_recvspace, 0,
  66. "Default raw socket receive space");
  67. /*
  68. * Allocate a control block and a nominal amount of buffer space for the
  69. * socket.
  70. */
  71. int
  72. raw_attach(struct socket *so, int proto)
  73. {
  74. struct rawcb *rp = sotorawcb(so);
  75. int error;
  76. /*
  77. * It is assumed that raw_attach is called after space has been
  78. * allocated for the rawcb; consumer protocols may simply allocate
  79. * type struct rawcb, or a wrapper data structure that begins with a
  80. * struct rawcb.
  81. */
  82. KASSERT(rp != NULL, ("raw_attach: rp == NULL"));
  83. error = soreserve(so, raw_sendspace, raw_recvspace);
  84. if (error)
  85. return (error);
  86. rp->rcb_socket = so;
  87. rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family;
  88. rp->rcb_proto.sp_protocol = proto;
  89. mtx_lock(&rawcb_mtx);
  90. LIST_INSERT_HEAD(&V_rawcb_list, rp, list);
  91. mtx_unlock(&rawcb_mtx);
  92. return (0);
  93. }
  94. /*
  95. * Detach the raw connection block and discard socket resources.
  96. */
  97. void
  98. raw_detach(struct rawcb *rp)
  99. {
  100. struct socket *so = rp->rcb_socket;
  101. KASSERT(so->so_pcb == rp, ("raw_detach: so_pcb != rp"));
  102. so->so_pcb = NULL;
  103. mtx_lock(&rawcb_mtx);
  104. LIST_REMOVE(rp, list);
  105. mtx_unlock(&rawcb_mtx);
  106. free((caddr_t)(rp), M_PCB);
  107. }