1 /* $NetBSD: uipc_domain.c,v 1.16 1996/08/14 05:43:35 explorer Exp $ */ 2 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)uipc_domain.c 8.2 (Berkeley) 10/18/93 36 */ 37 38 #include <sys/param.h> 39 #include <sys/socket.h> 40 #include <sys/protosw.h> 41 #include <sys/domain.h> 42 #include <sys/mbuf.h> 43 #include <sys/time.h> 44 #include <sys/kernel.h> 45 #include <sys/systm.h> 46 #include <sys/proc.h> 47 #include <vm/vm.h> 48 #include <sys/sysctl.h> 49 50 void pffasttimo __P((void *)); 51 void pfslowtimo __P((void *)); 52 53 #define ADDDOMAIN(x) { \ 54 extern struct domain __CONCAT(x,domain); \ 55 __CONCAT(x,domain.dom_next) = domains; \ 56 domains = &__CONCAT(x,domain); \ 57 } 58 59 void 60 domaininit() 61 { 62 register struct domain *dp; 63 register struct protosw *pr; 64 65 #undef unix 66 #ifndef lint 67 ADDDOMAIN(unix); 68 ADDDOMAIN(route); 69 #ifdef INET 70 ADDDOMAIN(inet); 71 #endif 72 #ifdef NS 73 ADDDOMAIN(ns); 74 #endif 75 #ifdef ISO 76 ADDDOMAIN(iso); 77 #endif 78 #ifdef CCITT 79 ADDDOMAIN(ccitt); 80 #endif 81 #ifdef NATM 82 ADDDOMAIN(natm); 83 #endif 84 #ifdef notdef /* XXXX */ 85 #include "imp.h" 86 #if NIMP > 0 87 ADDDOMAIN(imp); 88 #endif 89 #endif 90 #endif 91 92 for (dp = domains; dp; dp = dp->dom_next) { 93 if (dp->dom_init) 94 (*dp->dom_init)(); 95 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 96 if (pr->pr_init) 97 (*pr->pr_init)(); 98 } 99 100 if (max_linkhdr < 16) /* XXX */ 101 max_linkhdr = 16; 102 max_hdr = max_linkhdr + max_protohdr; 103 max_datalen = MHLEN - max_hdr; 104 timeout(pffasttimo, NULL, 1); 105 timeout(pfslowtimo, NULL, 1); 106 } 107 108 struct protosw * 109 pffindtype(family, type) 110 int family, type; 111 { 112 register struct domain *dp; 113 register struct protosw *pr; 114 115 for (dp = domains; dp; dp = dp->dom_next) 116 if (dp->dom_family == family) 117 goto found; 118 return (0); 119 found: 120 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 121 if (pr->pr_type && pr->pr_type == type) 122 return (pr); 123 return (0); 124 } 125 126 struct protosw * 127 pffindproto(family, protocol, type) 128 int family, protocol, type; 129 { 130 register struct domain *dp; 131 register struct protosw *pr; 132 struct protosw *maybe = 0; 133 134 if (family == 0) 135 return (0); 136 for (dp = domains; dp; dp = dp->dom_next) 137 if (dp->dom_family == family) 138 goto found; 139 return (0); 140 found: 141 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 142 if ((pr->pr_protocol == protocol) && (pr->pr_type == type)) 143 return (pr); 144 145 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW && 146 pr->pr_protocol == 0 && maybe == (struct protosw *)0) 147 maybe = pr; 148 } 149 return (maybe); 150 } 151 152 int 153 net_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 154 int *name; 155 u_int namelen; 156 void *oldp; 157 size_t *oldlenp; 158 void *newp; 159 size_t newlen; 160 struct proc *p; 161 { 162 register struct domain *dp; 163 register struct protosw *pr; 164 int family, protocol; 165 166 /* 167 * All sysctl names at this level are nonterminal; 168 * next two components are protocol family and protocol number, 169 * then at least one addition component. 170 */ 171 if (namelen < 3) 172 return (EISDIR); /* overloaded */ 173 family = name[0]; 174 protocol = name[1]; 175 176 if (family == 0) 177 return (0); 178 for (dp = domains; dp; dp = dp->dom_next) 179 if (dp->dom_family == family) 180 goto found; 181 return (ENOPROTOOPT); 182 found: 183 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 184 if (pr->pr_protocol == protocol && pr->pr_sysctl) 185 return ((*pr->pr_sysctl)(name + 2, namelen - 2, 186 oldp, oldlenp, newp, newlen)); 187 return (ENOPROTOOPT); 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, NULL); 202 } 203 204 void 205 pfslowtimo(arg) 206 void *arg; 207 { 208 register struct domain *dp; 209 register struct protosw *pr; 210 211 for (dp = domains; dp; dp = dp->dom_next) 212 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 213 if (pr->pr_slowtimo) 214 (*pr->pr_slowtimo)(); 215 timeout(pfslowtimo, NULL, hz/2); 216 } 217 218 void 219 pffasttimo(arg) 220 void *arg; 221 { 222 register struct domain *dp; 223 register struct protosw *pr; 224 225 for (dp = domains; dp; dp = dp->dom_next) 226 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 227 if (pr->pr_fasttimo) 228 (*pr->pr_fasttimo)(); 229 timeout(pffasttimo, NULL, hz/5); 230 } 231