1 /* 2 * Copyright (c) 1982, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)uipc_domain.c 8.2 (Berkeley) 10/18/93 34 * $FreeBSD$ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/socket.h> 39 #include <sys/protosw.h> 40 #include <sys/domain.h> 41 #include <sys/mbuf.h> 42 #include <sys/kernel.h> 43 #include <sys/socketvar.h> 44 #include <sys/systm.h> 45 #include <vm/uma.h> 46 47 /* 48 * System initialization 49 * 50 * Note: domain initialization takes place on a per domain basis 51 * as a result of traversing a SYSINIT linker set. Most likely, 52 * each domain would want to call DOMAIN_SET(9) itself, which 53 * would cause the domain to be added just after domaininit() 54 * is called during startup. 55 * 56 * See DOMAIN_SET(9) for details on its use. 57 */ 58 59 static void domaininit(void *); 60 SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL) 61 62 static struct callout pffast_callout; 63 static struct callout pfslow_callout; 64 65 static void pffasttimo(void *); 66 static void pfslowtimo(void *); 67 68 struct domain *domains; 69 70 /* 71 * Add a new protocol domain to the list of supported domains 72 * Note: you cant unload it again because a socket may be using it. 73 * XXX can't fail at this time. 74 */ 75 static void 76 net_init_domain(struct domain *dp) 77 { 78 register struct protosw *pr; 79 int s; 80 81 s = splnet(); 82 if (dp->dom_init) 83 (*dp->dom_init)(); 84 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++){ 85 if (pr->pr_usrreqs == 0) 86 panic("domaininit: %ssw[%d] has no usrreqs!", 87 dp->dom_name, 88 (int)(pr - dp->dom_protosw)); 89 if (pr->pr_init) 90 (*pr->pr_init)(); 91 } 92 /* 93 * update global informatio about maximums 94 */ 95 max_hdr = max_linkhdr + max_protohdr; 96 max_datalen = MHLEN - max_hdr; 97 splx(s); 98 } 99 100 /* 101 * Add a new protocol domain to the list of supported domains 102 * Note: you cant unload it again because a socket may be using it. 103 * XXX can't fail at this time. 104 */ 105 void 106 net_add_domain(void *data) 107 { 108 int s; 109 struct domain *dp; 110 111 dp = (struct domain *)data; 112 s = splnet(); 113 dp->dom_next = domains; 114 domains = dp; 115 splx(s); 116 net_init_domain(dp); 117 } 118 119 /* ARGSUSED*/ 120 static void 121 domaininit(void *dummy) 122 { 123 /* 124 * Before we do any setup, make sure to initialize the 125 * zone allocator we get struct sockets from. 126 */ 127 128 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL, 129 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 130 uma_zone_set_max(socket_zone, maxsockets); 131 132 if (max_linkhdr < 16) /* XXX */ 133 max_linkhdr = 16; 134 135 callout_init(&pffast_callout, 0); 136 callout_init(&pfslow_callout, 0); 137 138 callout_reset(&pffast_callout, 1, pffasttimo, NULL); 139 callout_reset(&pfslow_callout, 1, pfslowtimo, NULL); 140 } 141 142 143 struct protosw * 144 pffindtype(family, type) 145 int family; 146 int type; 147 { 148 register struct domain *dp; 149 register struct protosw *pr; 150 151 for (dp = domains; dp; dp = dp->dom_next) 152 if (dp->dom_family == family) 153 goto found; 154 return (0); 155 found: 156 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 157 if (pr->pr_type && pr->pr_type == type) 158 return (pr); 159 return (0); 160 } 161 162 struct protosw * 163 pffindproto(family, protocol, type) 164 int family; 165 int protocol; 166 int type; 167 { 168 register struct domain *dp; 169 register struct protosw *pr; 170 struct protosw *maybe = 0; 171 172 if (family == 0) 173 return (0); 174 for (dp = domains; dp; dp = dp->dom_next) 175 if (dp->dom_family == family) 176 goto found; 177 return (0); 178 found: 179 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 180 if ((pr->pr_protocol == protocol) && (pr->pr_type == type)) 181 return (pr); 182 183 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW && 184 pr->pr_protocol == 0 && maybe == (struct protosw *)0) 185 maybe = pr; 186 } 187 return (maybe); 188 } 189 190 void 191 pfctlinput(cmd, sa) 192 int cmd; 193 struct sockaddr *sa; 194 { 195 register struct domain *dp; 196 register struct protosw *pr; 197 198 for (dp = domains; dp; dp = dp->dom_next) 199 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 200 if (pr->pr_ctlinput) 201 (*pr->pr_ctlinput)(cmd, sa, (void *)0); 202 } 203 204 void 205 pfctlinput2(cmd, sa, ctlparam) 206 int cmd; 207 struct sockaddr *sa; 208 void *ctlparam; 209 { 210 struct domain *dp; 211 struct protosw *pr; 212 213 if (!sa) 214 return; 215 for (dp = domains; dp; dp = dp->dom_next) { 216 /* 217 * the check must be made by xx_ctlinput() anyways, to 218 * make sure we use data item pointed to by ctlparam in 219 * correct way. the following check is made just for safety. 220 */ 221 if (dp->dom_family != sa->sa_family) 222 continue; 223 224 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 225 if (pr->pr_ctlinput) 226 (*pr->pr_ctlinput)(cmd, sa, ctlparam); 227 } 228 } 229 230 static void 231 pfslowtimo(arg) 232 void *arg; 233 { 234 register struct domain *dp; 235 register struct protosw *pr; 236 237 for (dp = domains; dp; dp = dp->dom_next) 238 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 239 if (pr->pr_slowtimo) 240 (*pr->pr_slowtimo)(); 241 callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL); 242 } 243 244 static void 245 pffasttimo(arg) 246 void *arg; 247 { 248 register struct domain *dp; 249 register struct protosw *pr; 250 251 for (dp = domains; dp; dp = dp->dom_next) 252 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 253 if (pr->pr_fasttimo) 254 (*pr->pr_fasttimo)(); 255 callout_reset(&pffast_callout, hz/5, pffasttimo, NULL); 256 } 257