1 /* $OpenBSD: uipc_domain.c,v 1.15 2003/06/02 23:28:06 millert Exp $ */ 2 /* $NetBSD: uipc_domain.c,v 1.14 1996/02/09 19:00:44 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1993 6 * The Regents of the University of California. 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 * @(#)uipc_domain.c 8.2 (Berkeley) 10/18/93 33 */ 34 35 #include <sys/param.h> 36 #include <sys/socket.h> 37 #include <sys/protosw.h> 38 #include <sys/domain.h> 39 #include <sys/mbuf.h> 40 #include <sys/time.h> 41 #include <sys/kernel.h> 42 #include <sys/systm.h> 43 #include <sys/proc.h> 44 #include <uvm/uvm_extern.h> 45 #include <sys/sysctl.h> 46 #include <sys/timeout.h> 47 48 struct domain *domains; 49 50 void pffasttimo(void *); 51 void pfslowtimo(void *); 52 #if defined (KEY) || defined (IPSEC) 53 int pfkey_init(void); 54 #endif /* KEY || IPSEC */ 55 56 #define ADDDOMAIN(x) { \ 57 extern struct domain __CONCAT(x,domain); \ 58 __CONCAT(x,domain.dom_next) = domains; \ 59 domains = &__CONCAT(x,domain); \ 60 } 61 62 void 63 domaininit() 64 { 65 struct domain *dp; 66 struct protosw *pr; 67 static struct timeout pffast_timeout; 68 static struct timeout pfslow_timeout; 69 70 #undef unix 71 /* 72 * KAME NOTE: ADDDOMAIN(route) is moved to the last part so that 73 * it will be initialized as the *first* element. confusing! 74 */ 75 #ifndef lint 76 ADDDOMAIN(unix); 77 #ifdef INET 78 ADDDOMAIN(inet); 79 #endif 80 #ifdef INET6 81 ADDDOMAIN(inet6); 82 #endif /* INET6 */ 83 #if defined (KEY) || defined (IPSEC) 84 pfkey_init(); 85 #endif /* KEY || IPSEC */ 86 #ifdef IPX 87 ADDDOMAIN(ipx); 88 #endif 89 #ifdef NETATALK 90 ADDDOMAIN(atalk); 91 #endif 92 #ifdef NS 93 ADDDOMAIN(ns); 94 #endif 95 #ifdef ISO 96 ADDDOMAIN(iso); 97 #endif 98 #ifdef CCITT 99 ADDDOMAIN(ccitt); 100 #endif 101 #ifdef NATM 102 ADDDOMAIN(natm); 103 #endif 104 #ifdef notdef /* XXXX */ 105 #include "imp.h" 106 #if NIMP > 0 107 ADDDOMAIN(imp); 108 #endif 109 #endif 110 #ifdef IPSEC 111 #ifdef __KAME__ 112 ADDDOMAIN(key); 113 #endif 114 #endif 115 ADDDOMAIN(route); 116 #endif 117 118 for (dp = domains; dp; dp = dp->dom_next) { 119 if (dp->dom_init) 120 (*dp->dom_init)(); 121 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 122 if (pr->pr_init) 123 (*pr->pr_init)(); 124 } 125 126 if (max_linkhdr < 16) /* XXX */ 127 max_linkhdr = 16; 128 max_hdr = max_linkhdr + max_protohdr; 129 max_datalen = MHLEN - max_hdr; 130 timeout_set(&pffast_timeout, pffasttimo, &pffast_timeout); 131 timeout_set(&pfslow_timeout, pfslowtimo, &pfslow_timeout); 132 timeout_add(&pffast_timeout, 1); 133 timeout_add(&pfslow_timeout, 1); 134 } 135 136 struct protosw * 137 pffindtype(family, type) 138 int family, type; 139 { 140 register struct domain *dp; 141 register struct protosw *pr; 142 143 for (dp = domains; dp; dp = dp->dom_next) 144 if (dp->dom_family == family) 145 goto found; 146 return (0); 147 found: 148 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 149 if (pr->pr_type && pr->pr_type == type) 150 return (pr); 151 return (0); 152 } 153 154 struct protosw * 155 pffindproto(family, protocol, type) 156 int family, protocol, type; 157 { 158 register struct domain *dp; 159 register struct protosw *pr; 160 struct protosw *maybe = 0; 161 162 if (family == 0) 163 return (0); 164 for (dp = domains; dp; dp = dp->dom_next) 165 if (dp->dom_family == family) 166 goto found; 167 return (0); 168 found: 169 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 170 if ((pr->pr_protocol == protocol) && (pr->pr_type == type)) 171 return (pr); 172 173 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW && 174 pr->pr_protocol == 0 && maybe == (struct protosw *)0) 175 maybe = pr; 176 } 177 return (maybe); 178 } 179 180 int 181 net_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 182 int *name; 183 u_int namelen; 184 void *oldp; 185 size_t *oldlenp; 186 void *newp; 187 size_t newlen; 188 struct proc *p; 189 { 190 register struct domain *dp; 191 register struct protosw *pr; 192 int family, protocol; 193 194 /* 195 * All sysctl names at this level are nonterminal. 196 * PF_KEY: next component is protocol family, and then at least one 197 * additional component. 198 * usually: next two components are protocol family and protocol 199 * number, then at least one addition component. 200 */ 201 if (namelen < 2) 202 return (EISDIR); /* overloaded */ 203 family = name[0]; 204 205 if (family == 0) 206 return (0); 207 for (dp = domains; dp; dp = dp->dom_next) 208 if (dp->dom_family == family) 209 goto found; 210 return (ENOPROTOOPT); 211 found: 212 switch (family) { 213 #ifdef IPSEC 214 #ifdef __KAME__ 215 case PF_KEY: 216 pr = dp->dom_protosw; 217 if (pr->pr_sysctl) 218 return ((*pr->pr_sysctl)(name + 1, namelen - 1, 219 oldp, oldlenp, newp, newlen)); 220 return (ENOPROTOOPT); 221 #endif 222 #endif 223 default: 224 break; 225 } 226 if (namelen < 3) 227 return (EISDIR); /* overloaded */ 228 protocol = name[1]; 229 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 230 if (pr->pr_protocol == protocol && pr->pr_sysctl) 231 return ((*pr->pr_sysctl)(name + 2, namelen - 2, 232 oldp, oldlenp, newp, newlen)); 233 return (ENOPROTOOPT); 234 } 235 236 void 237 pfctlinput(cmd, sa) 238 int cmd; 239 struct sockaddr *sa; 240 { 241 register struct domain *dp; 242 register struct protosw *pr; 243 244 for (dp = domains; dp; dp = dp->dom_next) 245 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 246 if (pr->pr_ctlinput) 247 (*pr->pr_ctlinput)(cmd, sa, NULL); 248 } 249 250 void 251 pfslowtimo(arg) 252 void *arg; 253 { 254 struct timeout *to = (struct timeout *)arg; 255 struct domain *dp; 256 struct protosw *pr; 257 258 for (dp = domains; dp; dp = dp->dom_next) 259 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 260 if (pr->pr_slowtimo) 261 (*pr->pr_slowtimo)(); 262 timeout_add(to, hz/2); 263 } 264 265 void 266 pffasttimo(arg) 267 void *arg; 268 { 269 struct timeout *to = (struct timeout *)arg; 270 struct domain *dp; 271 struct protosw *pr; 272 273 for (dp = domains; dp; dp = dp->dom_next) 274 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 275 if (pr->pr_fasttimo) 276 (*pr->pr_fasttimo)(); 277 timeout_add(to, hz/5); 278 } 279