| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
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: src/sys/kern/uipc_domain.c,v 1.22.2.1 2001/07/03 11:01:37 ume Exp $ | |
| e3873585 | 35 | * $DragonFly: src/sys/kern/uipc_domain.c,v 1.13 2008/10/27 02:56:30 sephe Exp $ |
| 984263bc MD |
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/kernel.h> | |
| 44 | #include <sys/socketvar.h> | |
| e3873585 | 45 | #include <sys/socketops.h> |
| 984263bc MD |
46 | #include <sys/systm.h> |
| 47 | #include <vm/vm_zone.h> | |
| 48 | ||
| e43a034f MD |
49 | #include <sys/thread2.h> |
| 50 | ||
| 984263bc MD |
51 | /* |
| 52 | * System initialization | |
| 53 | * | |
| 54 | * Note: domain initialization wants to take place on a per domain basis | |
| 55 | * as a result of traversing a linker set. Most likely, each domain | |
| 56 | * want to call a registration function rather than being handled here | |
| 57 | * in domaininit(). Probably this will look like: | |
| 58 | * | |
| 59 | * SYSINIT(unique, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, domain_add, xxx) | |
| 60 | * | |
| 61 | * Where 'xxx' is replaced by the address of a parameter struct to be | |
| 62 | * passed to the doamin_add() function. | |
| 63 | */ | |
| 64 | ||
| 402ed7e1 | 65 | static void domaininit (void *); |
| 984263bc MD |
66 | SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL) |
| 67 | ||
| 402ed7e1 RG |
68 | static void pffasttimo (void *); |
| 69 | static void pfslowtimo (void *); | |
| 984263bc | 70 | |
| 9c70fe43 | 71 | struct domainlist domains; |
| 984263bc | 72 | |
| a330847e JS |
73 | static struct callout pffasttimo_ch; |
| 74 | static struct callout pfslowtimo_ch; | |
| 75 | ||
| 984263bc MD |
76 | /* |
| 77 | * Add a new protocol domain to the list of supported domains | |
| 9c70fe43 | 78 | * Note: you cant unload it again because a socket may be using it. |
| 984263bc MD |
79 | * XXX can't fail at this time. |
| 80 | */ | |
| a95455e5 MD |
81 | #define PRU_NOTSUPP(pu, label) \ |
| 82 | if (pu->pru_ ## label == NULL) \ | |
| 83 | pu->pru_ ## label = pru_ ## label ## _notsupp; | |
| 84 | #define PRU_NULL(pu, label) \ | |
| 85 | if (pu->pru_ ## label == NULL) \ | |
| 86 | pu->pru_ ## label = pru_ ## label ## _null; | |
| 87 | ||
| 984263bc MD |
88 | static void |
| 89 | net_init_domain(struct domain *dp) | |
| 90 | { | |
| 1fd87d54 | 91 | struct protosw *pr; |
| a95455e5 | 92 | struct pr_usrreqs *pu; |
| 984263bc | 93 | |
| e43a034f | 94 | crit_enter(); |
| 984263bc MD |
95 | if (dp->dom_init) |
| 96 | (*dp->dom_init)(); | |
| 517e1666 | 97 | for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { |
| a95455e5 MD |
98 | pu = pr->pr_usrreqs; |
| 99 | if (pu == NULL) | |
| 973c11b9 MD |
100 | panic("domaininit: %ssw[%ld] has no usrreqs!", |
| 101 | dp->dom_name, (long)(pr - dp->dom_protosw)); | |
| a95455e5 MD |
102 | PRU_NOTSUPP(pu, accept); |
| 103 | PRU_NOTSUPP(pu, bind); | |
| 104 | PRU_NOTSUPP(pu, connect); | |
| 105 | PRU_NOTSUPP(pu, connect2); | |
| 106 | PRU_NOTSUPP(pu, control); | |
| 107 | PRU_NOTSUPP(pu, disconnect); | |
| 108 | PRU_NOTSUPP(pu, listen); | |
| 109 | PRU_NOTSUPP(pu, peeraddr); | |
| 110 | PRU_NOTSUPP(pu, rcvd); | |
| 111 | PRU_NOTSUPP(pu, rcvoob); | |
| 112 | PRU_NULL(pu, sense); | |
| 113 | PRU_NOTSUPP(pu, shutdown); | |
| 114 | PRU_NOTSUPP(pu, sockaddr); | |
| 115 | PRU_NOTSUPP(pu, sosend); | |
| 116 | PRU_NOTSUPP(pu, soreceive); | |
| 117 | PRU_NOTSUPP(pu, sopoll); | |
| 118 | PRU_NOTSUPP(pu, ctloutput); | |
| 119 | ||
| 984263bc MD |
120 | if (pr->pr_init) |
| 121 | (*pr->pr_init)(); | |
| 122 | } | |
| 123 | /* | |
| 9c70fe43 | 124 | * update global information about maximums |
| 984263bc MD |
125 | */ |
| 126 | max_hdr = max_linkhdr + max_protohdr; | |
| 127 | max_datalen = MHLEN - max_hdr; | |
| e43a034f | 128 | crit_exit(); |
| 984263bc MD |
129 | } |
| 130 | ||
| 131 | /* | |
| 132 | * Add a new protocol domain to the list of supported domains | |
| 9c70fe43 | 133 | * Note: you cant unload it again because a socket may be using it. |
| 984263bc MD |
134 | * XXX can't fail at this time. |
| 135 | */ | |
| 136 | void | |
| 137 | net_add_domain(void *data) | |
| 138 | { | |
| 9c70fe43 | 139 | struct domain *dp = data; |
| 984263bc | 140 | |
| e43a034f | 141 | crit_enter(); |
| 9c70fe43 | 142 | SLIST_INSERT_HEAD(&domains, dp, dom_next); |
| e43a034f | 143 | crit_exit(); |
| 984263bc MD |
144 | net_init_domain(dp); |
| 145 | } | |
| 146 | ||
| 147 | /* ARGSUSED*/ | |
| 148 | static void | |
| 149 | domaininit(void *dummy) | |
| 150 | { | |
| 984263bc MD |
151 | if (max_linkhdr < 16) /* XXX */ |
| 152 | max_linkhdr = 16; | |
| 153 | ||
| a330847e JS |
154 | callout_init(&pffasttimo_ch); |
| 155 | callout_init(&pfslowtimo_ch); | |
| 156 | callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL); | |
| 157 | callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL); | |
| 984263bc MD |
158 | } |
| 159 | ||
| 160 | ||
| 161 | struct protosw * | |
| 5fe66e68 | 162 | pffindtype(int family, int type) |
| 984263bc | 163 | { |
| 1fd87d54 RG |
164 | struct domain *dp; |
| 165 | struct protosw *pr; | |
| 984263bc | 166 | |
| 9c70fe43 | 167 | SLIST_FOREACH(dp, &domains, dom_next) |
| 984263bc MD |
168 | if (dp->dom_family == family) |
| 169 | goto found; | |
| 5fe66e68 JH |
170 | return (NULL); |
| 171 | ||
| 984263bc MD |
172 | found: |
| 173 | for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) | |
| 174 | if (pr->pr_type && pr->pr_type == type) | |
| 175 | return (pr); | |
| 5fe66e68 | 176 | return (NULL); |
| 984263bc MD |
177 | } |
| 178 | ||
| 179 | struct protosw * | |
| 5fe66e68 | 180 | pffindproto(int family, int protocol, int type) |
| 984263bc | 181 | { |
| 1fd87d54 RG |
182 | struct domain *dp; |
| 183 | struct protosw *pr; | |
| 5fe66e68 | 184 | struct protosw *maybe = NULL; |
| 984263bc MD |
185 | |
| 186 | if (family == 0) | |
| 5fe66e68 | 187 | return (NULL); |
| 9c70fe43 | 188 | SLIST_FOREACH(dp, &domains, dom_next) |
| 984263bc MD |
189 | if (dp->dom_family == family) |
| 190 | goto found; | |
| 5fe66e68 JH |
191 | return (NULL); |
| 192 | ||
| 984263bc MD |
193 | found: |
| 194 | for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { | |
| 195 | if ((pr->pr_protocol == protocol) && (pr->pr_type == type)) | |
| 196 | return (pr); | |
| 197 | ||
| 198 | if (type == SOCK_RAW && pr->pr_type == SOCK_RAW && | |
| 2038fb68 | 199 | pr->pr_protocol == 0 && maybe == NULL) |
| 984263bc MD |
200 | maybe = pr; |
| 201 | } | |
| 202 | return (maybe); | |
| 203 | } | |
| 204 | ||
| 205 | void | |
| 91be174d | 206 | kpfctlinput(int cmd, struct sockaddr *sa) |
| 984263bc | 207 | { |
| 1fd87d54 RG |
208 | struct domain *dp; |
| 209 | struct protosw *pr; | |
| 984263bc | 210 | |
| e3873585 | 211 | SLIST_FOREACH(dp, &domains, dom_next) { |
| 984263bc | 212 | for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) |
| e3873585 SZ |
213 | so_pru_ctlinput(pr, cmd, sa, NULL); |
| 214 | } | |
| 984263bc MD |
215 | } |
| 216 | ||
| 217 | void | |
| 91be174d | 218 | kpfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam) |
| 984263bc MD |
219 | { |
| 220 | struct domain *dp; | |
| 221 | struct protosw *pr; | |
| 222 | ||
| 223 | if (!sa) | |
| 224 | return; | |
| 9c70fe43 | 225 | SLIST_FOREACH(dp, &domains, dom_next) { |
| 984263bc MD |
226 | /* |
| 227 | * the check must be made by xx_ctlinput() anyways, to | |
| 228 | * make sure we use data item pointed to by ctlparam in | |
| 229 | * correct way. the following check is made just for safety. | |
| 230 | */ | |
| 231 | if (dp->dom_family != sa->sa_family) | |
| 232 | continue; | |
| 233 | ||
| 234 | for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) | |
| e3873585 | 235 | so_pru_ctlinput(pr, cmd, sa, ctlparam); |
| 984263bc MD |
236 | } |
| 237 | } | |
| 238 | ||
| 239 | static void | |
| 5fe66e68 | 240 | pfslowtimo(void *arg) |
| 984263bc | 241 | { |
| 1fd87d54 RG |
242 | struct domain *dp; |
| 243 | struct protosw *pr; | |
| 984263bc | 244 | |
| 9c70fe43 | 245 | SLIST_FOREACH(dp, &domains, dom_next) |
| 984263bc MD |
246 | for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) |
| 247 | if (pr->pr_slowtimo) | |
| 248 | (*pr->pr_slowtimo)(); | |
| a330847e | 249 | callout_reset(&pfslowtimo_ch, hz / 2, pfslowtimo, NULL); |
| 984263bc MD |
250 | } |
| 251 | ||
| 252 | static void | |
| 5fe66e68 | 253 | pffasttimo(void *arg) |
| 984263bc | 254 | { |
| 1fd87d54 RG |
255 | struct domain *dp; |
| 256 | struct protosw *pr; | |
| 984263bc | 257 | |
| 9c70fe43 | 258 | SLIST_FOREACH(dp, &domains, dom_next) |
| 984263bc MD |
259 | for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) |
| 260 | if (pr->pr_fasttimo) | |
| 261 | (*pr->pr_fasttimo)(); | |
| a330847e | 262 | callout_reset(&pffasttimo_ch, hz / 5, pffasttimo, NULL); |
| 984263bc | 263 | } |