raw_cb.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* $OpenBSD: raw_cb.c,v 1.9 2015/03/14 03:38:51 jsg Exp $ */
  2. /* $NetBSD: raw_cb.c,v 1.9 1996/02/13 22:00:39 christos Exp $ */
  3. /*
  4. * Copyright (c) 1980, 1986, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * @(#)raw_cb.c 8.1 (Berkeley) 6/10/93
  32. */
  33. #include <sys/param.h>
  34. #include <sys/systm.h>
  35. #include <sys/mbuf.h>
  36. #include <sys/socket.h>
  37. #include <sys/socketvar.h>
  38. #include <sys/domain.h>
  39. #include <sys/protosw.h>
  40. #include <sys/errno.h>
  41. #include <net/raw_cb.h>
  42. #include <netinet/in.h>
  43. /*
  44. * Routines to manage the raw protocol control blocks.
  45. *
  46. * TODO:
  47. * hash lookups by protocol family/protocol + address family
  48. * take care of unique address problems per AF?
  49. * redo address binding to allow wildcards
  50. */
  51. u_long raw_sendspace = RAWSNDQ;
  52. u_long raw_recvspace = RAWRCVQ;
  53. struct rawcbhead rawcb;
  54. /*
  55. * Allocate a control block and a nominal amount
  56. * of buffer space for the socket.
  57. */
  58. int
  59. raw_attach(struct socket *so, int proto)
  60. {
  61. struct rawcb *rp = sotorawcb(so);
  62. int error;
  63. /*
  64. * It is assumed that raw_attach is called
  65. * after space has been allocated for the
  66. * rawcb.
  67. */
  68. if (rp == 0)
  69. return (ENOBUFS);
  70. if ((error = soreserve(so, raw_sendspace, raw_recvspace)) != 0)
  71. return (error);
  72. rp->rcb_socket = so;
  73. rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family;
  74. rp->rcb_proto.sp_protocol = proto;
  75. LIST_INSERT_HEAD(&rawcb, rp, rcb_list);
  76. return (0);
  77. }
  78. /*
  79. * Detach the raw connection block and discard
  80. * socket resources.
  81. */
  82. void
  83. raw_detach(struct rawcb *rp)
  84. {
  85. struct socket *so = rp->rcb_socket;
  86. so->so_pcb = 0;
  87. sofree(so);
  88. LIST_REMOVE(rp, rcb_list);
  89. #ifdef notdef
  90. if (rp->rcb_laddr)
  91. m_freem(dtom(rp->rcb_laddr));
  92. rp->rcb_laddr = 0;
  93. #endif
  94. free((caddr_t)(rp), M_PCB, 0);
  95. }
  96. /*
  97. * Disconnect and possibly release resources.
  98. */
  99. void
  100. raw_disconnect(struct rawcb *rp)
  101. {
  102. #ifdef notdef
  103. if (rp->rcb_faddr)
  104. m_freem(dtom(rp->rcb_faddr));
  105. rp->rcb_faddr = 0;
  106. #endif
  107. if (rp->rcb_socket->so_state & SS_NOFDREF)
  108. raw_detach(rp);
  109. }
  110. #ifdef notdef
  111. int
  112. raw_bind(struct socket *so, struct mbuf *nam)
  113. {
  114. struct sockaddr *addr = mtod(nam, struct sockaddr *);
  115. struct rawcb *rp;
  116. if (ifnet == 0)
  117. return (EADDRNOTAVAIL);
  118. rp = sotorawcb(so);
  119. nam = m_copym(nam, 0, M_COPYALL, M_WAITOK);
  120. rp->rcb_laddr = mtod(nam, struct sockaddr *);
  121. return (0);
  122. }
  123. #endif