uipc_domain.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* $OpenBSD: uipc_domain.c,v 1.41 2015/07/17 18:31:08 blambert Exp $ */
  2. /* $NetBSD: uipc_domain.c,v 1.14 1996/02/09 19:00:44 christos Exp $ */
  3. /*
  4. * Copyright (c) 1982, 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. * @(#)uipc_domain.c 8.2 (Berkeley) 10/18/93
  32. */
  33. #include <sys/param.h>
  34. #include <sys/socket.h>
  35. #include <sys/protosw.h>
  36. #include <sys/domain.h>
  37. #include <sys/mbuf.h>
  38. #include <sys/time.h>
  39. #include <sys/systm.h>
  40. #include <sys/sysctl.h>
  41. #include <sys/timeout.h>
  42. #include "bpfilter.h"
  43. #include "pflow.h"
  44. struct domain *domains;
  45. void pffasttimo(void *);
  46. void pfslowtimo(void *);
  47. struct domain * pffinddomain(int);
  48. #define ADDDOMAIN(x) { \
  49. extern struct domain __CONCAT(x,domain); \
  50. __CONCAT(x,domain.dom_next) = domains; \
  51. domains = &__CONCAT(x,domain); \
  52. }
  53. void
  54. domaininit(void)
  55. {
  56. struct domain *dp;
  57. struct protosw *pr;
  58. static struct timeout pffast_timeout;
  59. static struct timeout pfslow_timeout;
  60. #undef unix
  61. /*
  62. * KAME NOTE: ADDDOMAIN(route) is moved to the last part so that
  63. * it will be initialized as the *first* element. confusing!
  64. */
  65. ADDDOMAIN(unix);
  66. ADDDOMAIN(inet);
  67. #ifdef INET6
  68. ADDDOMAIN(inet6);
  69. #endif /* INET6 */
  70. #if defined (KEY) || defined (IPSEC) || defined (TCP_SIGNATURE)
  71. ADDDOMAIN(pfkey);
  72. #endif
  73. #ifdef MPLS
  74. ADDDOMAIN(mpls);
  75. #endif
  76. ADDDOMAIN(route);
  77. for (dp = domains; dp; dp = dp->dom_next) {
  78. if (dp->dom_init)
  79. (*dp->dom_init)();
  80. for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
  81. if (pr->pr_init)
  82. (*pr->pr_init)();
  83. }
  84. if (max_linkhdr < 16) /* XXX */
  85. max_linkhdr = 16;
  86. max_hdr = max_linkhdr + max_protohdr;
  87. timeout_set(&pffast_timeout, pffasttimo, &pffast_timeout);
  88. timeout_set(&pfslow_timeout, pfslowtimo, &pfslow_timeout);
  89. timeout_add(&pffast_timeout, 1);
  90. timeout_add(&pfslow_timeout, 1);
  91. }
  92. struct domain *
  93. pffinddomain(int family)
  94. {
  95. struct domain *dp;
  96. for (dp = domains; dp != NULL; dp = dp->dom_next)
  97. if (dp->dom_family == family)
  98. return (dp);
  99. return (NULL);
  100. }
  101. struct protosw *
  102. pffindtype(int family, int type)
  103. {
  104. struct domain *dp;
  105. struct protosw *pr;
  106. dp = pffinddomain(family);
  107. if (dp == NULL)
  108. return (NULL);
  109. for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
  110. if (pr->pr_type && pr->pr_type == type)
  111. return (pr);
  112. return (NULL);
  113. }
  114. struct protosw *
  115. pffindproto(int family, int protocol, int type)
  116. {
  117. struct domain *dp;
  118. struct protosw *pr;
  119. struct protosw *maybe = NULL;
  120. if (family == 0)
  121. return (NULL);
  122. dp = pffinddomain(family);
  123. if (dp == NULL)
  124. return (NULL);
  125. for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
  126. if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
  127. return (pr);
  128. if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
  129. pr->pr_protocol == 0 && maybe == NULL)
  130. maybe = pr;
  131. }
  132. return (maybe);
  133. }
  134. int
  135. net_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
  136. size_t newlen, struct proc *p)
  137. {
  138. struct domain *dp;
  139. struct protosw *pr;
  140. int family, protocol;
  141. /*
  142. * All sysctl names at this level are nonterminal.
  143. * Usually: next two components are protocol family and protocol
  144. * number, then at least one addition component.
  145. */
  146. if (namelen < 2)
  147. return (EISDIR); /* overloaded */
  148. family = name[0];
  149. if (family == 0)
  150. return (0);
  151. #if NBPFILTER > 0
  152. if (family == PF_BPF)
  153. return (bpf_sysctl(name + 1, namelen - 1, oldp, oldlenp,
  154. newp, newlen));
  155. #endif
  156. #if NPFLOW > 0
  157. if (family == PF_PFLOW)
  158. return (pflow_sysctl(name + 1, namelen - 1, oldp, oldlenp,
  159. newp, newlen));
  160. #endif
  161. #ifdef PIPEX
  162. if (family == PF_PIPEX)
  163. return (pipex_sysctl(name + 1, namelen - 1, oldp, oldlenp,
  164. newp, newlen));
  165. #endif
  166. dp = pffinddomain(family);
  167. if (dp == NULL)
  168. return (ENOPROTOOPT);
  169. #ifdef MPLS
  170. /* XXX WARNING: big fat ugly hack */
  171. /* stupid net.mpls is special as it does not have a protocol */
  172. if (family == PF_MPLS)
  173. return (dp->dom_protosw[0].pr_sysctl(name + 1, namelen - 1,
  174. oldp, oldlenp, newp, newlen));
  175. #endif
  176. if (namelen < 3)
  177. return (EISDIR); /* overloaded */
  178. protocol = name[1];
  179. for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
  180. if (pr->pr_protocol == protocol && pr->pr_sysctl)
  181. return ((*pr->pr_sysctl)(name + 2, namelen - 2,
  182. oldp, oldlenp, newp, newlen));
  183. return (ENOPROTOOPT);
  184. }
  185. void
  186. pfctlinput(int cmd, struct sockaddr *sa)
  187. {
  188. struct domain *dp;
  189. struct protosw *pr;
  190. for (dp = domains; dp; dp = dp->dom_next)
  191. for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
  192. if (pr->pr_ctlinput)
  193. (*pr->pr_ctlinput)(cmd, sa, 0, NULL);
  194. }
  195. void
  196. pfslowtimo(void *arg)
  197. {
  198. struct timeout *to = (struct timeout *)arg;
  199. struct domain *dp;
  200. struct protosw *pr;
  201. for (dp = domains; dp; dp = dp->dom_next)
  202. for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
  203. if (pr->pr_slowtimo)
  204. (*pr->pr_slowtimo)();
  205. timeout_add_msec(to, 500);
  206. }
  207. void
  208. pffasttimo(void *arg)
  209. {
  210. struct timeout *to = (struct timeout *)arg;
  211. struct domain *dp;
  212. struct protosw *pr;
  213. for (dp = domains; dp; dp = dp->dom_next)
  214. for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
  215. if (pr->pr_fasttimo)
  216. (*pr->pr_fasttimo)();
  217. timeout_add_msec(to, 200);
  218. }