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/vm_zone.h> 46 47 /* 48 * System initialization 49 * 50 * Note: domain initialization wants to take place on a per domain basis 51 * as a result of traversing a linker set. Most likely, each domain 52 * want to call a registration function rather than being handled here 53 * in domaininit(). Probably this will look like: 54 * 55 * SYSINIT(unique, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, domain_add, xxx) 56 * 57 * Where 'xxx' is replaced by the address of a parameter struct to be 58 * passed to the doamin_add() function. 59 */ 60 61 static void domaininit __P((void *)); 62 SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL) 63 64 static struct callout pffast_callout; 65 static struct callout pfslow_callout; 66 67 static void pffasttimo __P((void *)); 68 static void pfslowtimo __P((void *)); 69 70 struct domain *domains; 71 72 /* 73 * Add a new protocol domain to the list of supported domains 74 * Note: you cant unload it again because a socket may be using it. 75 * XXX can't fail at this time. 76 */ 77 static void 78 net_init_domain(struct domain *dp) 79 { 80 register struct protosw *pr; 81 int s; 82 83 s = splnet(); 84 if (dp->dom_init) 85 (*dp->dom_init)(); 86 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++){ 87 if (pr->pr_usrreqs == 0) 88 panic("domaininit: %ssw[%d] has no usrreqs!", 89 dp->dom_name, 90 (int)(pr - dp->dom_protosw)); 91 if (pr->pr_init) 92 (*pr->pr_init)(); 93 } 94 /* 95 * update global informatio about maximums 96 */ 97 max_hdr = max_linkhdr + max_protohdr; 98 max_datalen = MHLEN - max_hdr; 99 splx(s); 100 } 101 102 /* 103 * Add a new protocol domain to the list of supported domains 104 * Note: you cant unload it again because a socket may be using it. 105 * XXX can't fail at this time. 106 */ 107 void 108 net_add_domain(void *data) 109 { 110 int s; 111 struct domain *dp; 112 113 dp = (struct domain *)data; 114 s = splnet(); 115 dp->dom_next = domains; 116 domains = dp; 117 splx(s); 118 net_init_domain(dp); 119 } 120 121 /* ARGSUSED*/ 122 static void 123 domaininit(void *dummy) 124 { 125 /* 126 * Before we do any setup, make sure to initialize the 127 * zone allocator we get struct sockets from. The obvious 128 * maximum number of sockets is `maxfiles', but it is possible 129 * to have a socket without an open file (e.g., a connection waiting 130 * to be accept(2)ed). Rather than think up and define a 131 * better value, we just use nmbclusters, since that's what people 132 * are told to increase first when the network runs out of memory. 133 * Perhaps we should have two pools, one of unlimited size 134 * for use during socreate(), and one ZONE_INTERRUPT pool for 135 * use in sonewconn(). 136 */ 137 socket_zone = zinit("socket", sizeof(struct socket), maxsockets, 138 ZONE_INTERRUPT, 0); 139 140 if (max_linkhdr < 16) /* XXX */ 141 max_linkhdr = 16; 142 143 callout_init(&pffast_callout, 0); 144 callout_init(&pfslow_callout, 0); 145 146 callout_reset(&pffast_callout, 1, pffasttimo, NULL); 147 callout_reset(&pfslow_callout, 1, pfslowtimo, NULL); 148 } 149 150 151 struct protosw * 152 pffindtype(family, type) 153 int family; 154 int type; 155 { 156 register struct domain *dp; 157 register struct protosw *pr; 158 159 for (dp = domains; dp; dp = dp->dom_next) 160 if (dp->dom_family == family) 161 goto found; 162 return (0); 163 found: 164 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 165 if (pr->pr_type && pr->pr_type == type) 166 return (pr); 167 return (0); 168 } 169 170 struct protosw * 171 pffindproto(family, protocol, type) 172 int family; 173 int protocol; 174 int type; 175 { 176 register struct domain *dp; 177 register struct protosw *pr; 178 struct protosw *maybe = 0; 179 180 if (family == 0) 181 return (0); 182 for (dp = domains; dp; dp = dp->dom_next) 183 if (dp->dom_family == family) 184 goto found; 185 return (0); 186 found: 187 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 188 if ((pr->pr_protocol == protocol) && (pr->pr_type == type)) 189 return (pr); 190 191 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW && 192 pr->pr_protocol == 0 && maybe == (struct protosw *)0) 193 maybe = pr; 194 } 195 return (maybe); 196 } 197 198 void 199 pfctlinput(cmd, sa) 200 int cmd; 201 struct sockaddr *sa; 202 { 203 register struct domain *dp; 204 register struct protosw *pr; 205 206 for (dp = domains; dp; dp = dp->dom_next) 207 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 208 if (pr->pr_ctlinput) 209 (*pr->pr_ctlinput)(cmd, sa, (void *)0); 210 } 211 212 void 213 pfctlinput2(cmd, sa, ctlparam) 214 int cmd; 215 struct sockaddr *sa; 216 void *ctlparam; 217 { 218 struct domain *dp; 219 struct protosw *pr; 220 221 if (!sa) 222 return; 223 for (dp = domains; dp; dp = dp->dom_next) { 224 /* 225 * the check must be made by xx_ctlinput() anyways, to 226 * make sure we use data item pointed to by ctlparam in 227 * correct way. the following check is made just for safety. 228 */ 229 if (dp->dom_family != sa->sa_family) 230 continue; 231 232 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 233 if (pr->pr_ctlinput) 234 (*pr->pr_ctlinput)(cmd, sa, ctlparam); 235 } 236 } 237 238 static void 239 pfslowtimo(arg) 240 void *arg; 241 { 242 register struct domain *dp; 243 register struct protosw *pr; 244 245 for (dp = domains; dp; dp = dp->dom_next) 246 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 247 if (pr->pr_slowtimo) 248 (*pr->pr_slowtimo)(); 249 callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL); 250 } 251 252 static void 253 pffasttimo(arg) 254 void *arg; 255 { 256 register struct domain *dp; 257 register struct protosw *pr; 258 259 for (dp = domains; dp; dp = dp->dom_next) 260 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 261 if (pr->pr_fasttimo) 262 (*pr->pr_fasttimo)(); 263 callout_reset(&pffast_callout, hz/5, pffasttimo, NULL); 264 } 265