Home | History | Annotate | Line # | Download | only in kern
uipc_domain.c revision 1.24
      1 /*	$NetBSD: uipc_domain.c,v 1.24 1998/07/05 22:48:07 jonathan 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.3 (Berkeley) 2/14/95
     36  */
     37 
     38 #include "opt_inet.h"
     39 #include "opt_atalk.h"
     40 #include "opt_ccitt.h"
     41 #include "opt_iso.h"
     42 #include "opt_ns.h"
     43 #include "opt_natm.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/socket.h>
     47 #include <sys/protosw.h>
     48 #include <sys/domain.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/time.h>
     51 #include <sys/kernel.h>
     52 #include <sys/systm.h>
     53 #include <sys/proc.h>
     54 #include <vm/vm.h>
     55 #include <sys/sysctl.h>
     56 
     57 void	pffasttimo __P((void *));
     58 void	pfslowtimo __P((void *));
     59 
     60 /*
     61  * Current time values for fast and slow timeouts.  We can use u_int
     62  * relatively safely.  The fast timer will roll over in 27 years and
     63  * the slow timer in 68 years.
     64  */
     65 u_int	pfslowtimo_now;
     66 u_int	pffasttimo_now;
     67 
     68 #define	ADDDOMAIN(x)	{ \
     69 	extern struct domain __CONCAT(x,domain); \
     70 	__CONCAT(x,domain.dom_next) = domains; \
     71 	domains = &__CONCAT(x,domain); \
     72 }
     73 
     74 void
     75 domaininit()
     76 {
     77 	register struct domain *dp;
     78 	register struct protosw *pr;
     79 
     80 #undef unix
     81 #ifndef lint
     82 	ADDDOMAIN(unix);
     83 	ADDDOMAIN(route);
     84 #ifdef INET
     85 	ADDDOMAIN(inet);
     86 #endif
     87 #ifdef NS
     88 	ADDDOMAIN(ns);
     89 #endif
     90 #ifdef ISO
     91 	ADDDOMAIN(iso);
     92 #endif
     93 #ifdef CCITT
     94 	ADDDOMAIN(ccitt);
     95 #endif
     96 #ifdef NATM
     97 	ADDDOMAIN(natm);
     98 #endif
     99 #ifdef NETATALK
    100 	ADDDOMAIN(atalk);
    101 #endif
    102 #ifdef notdef /* XXXX */
    103 #include "imp.h"
    104 #if NIMP > 0
    105 	ADDDOMAIN(imp);
    106 #endif
    107 #endif
    108 #endif
    109 
    110 	for (dp = domains; dp; dp = dp->dom_next) {
    111 		if (dp->dom_init)
    112 			(*dp->dom_init)();
    113 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    114 			if (pr->pr_init)
    115 				(*pr->pr_init)();
    116 	}
    117 
    118 	if (max_linkhdr < 16)		/* XXX */
    119 		max_linkhdr = 16;
    120 	max_hdr = max_linkhdr + max_protohdr;
    121 	max_datalen = MHLEN - max_hdr;
    122 	timeout(pffasttimo, NULL, 1);
    123 	timeout(pfslowtimo, NULL, 1);
    124 }
    125 
    126 struct protosw *
    127 pffindtype(family, type)
    128 	int family, type;
    129 {
    130 	register struct domain *dp;
    131 	register struct protosw *pr;
    132 
    133 	for (dp = domains; dp; dp = dp->dom_next)
    134 		if (dp->dom_family == family)
    135 			goto found;
    136 	return (0);
    137 found:
    138 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    139 		if (pr->pr_type && pr->pr_type == type)
    140 			return (pr);
    141 	return (0);
    142 }
    143 
    144 struct protosw *
    145 pffindproto(family, protocol, type)
    146 	int family, protocol, type;
    147 {
    148 	register struct domain *dp;
    149 	register struct protosw *pr;
    150 	struct protosw *maybe = 0;
    151 
    152 	if (family == 0)
    153 		return (0);
    154 	for (dp = domains; dp; dp = dp->dom_next)
    155 		if (dp->dom_family == family)
    156 			goto found;
    157 	return (0);
    158 found:
    159 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    160 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
    161 			return (pr);
    162 
    163 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
    164 		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
    165 			maybe = pr;
    166 	}
    167 	return (maybe);
    168 }
    169 
    170 int
    171 net_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    172 	int *name;
    173 	u_int namelen;
    174 	void *oldp;
    175 	size_t *oldlenp;
    176 	void *newp;
    177 	size_t newlen;
    178 	struct proc *p;
    179 {
    180 	register struct domain *dp;
    181 	register struct protosw *pr;
    182 	int family, protocol;
    183 
    184 	/*
    185 	 * All sysctl names at this level are nonterminal;
    186 	 * next two components are protocol family and protocol number,
    187 	 * then at least one addition component.
    188 	 */
    189 	if (namelen < 3)
    190 		return (EISDIR);		/* overloaded */
    191 	family = name[0];
    192 	protocol = name[1];
    193 
    194 	if (family == 0)
    195 		return (0);
    196 	for (dp = domains; dp; dp = dp->dom_next)
    197 		if (dp->dom_family == family)
    198 			goto found;
    199 	return (ENOPROTOOPT);
    200 found:
    201 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    202 		if (pr->pr_protocol == protocol && pr->pr_sysctl)
    203 			return ((*pr->pr_sysctl)(name + 2, namelen - 2,
    204 			    oldp, oldlenp, newp, newlen));
    205 	return (ENOPROTOOPT);
    206 }
    207 
    208 void
    209 pfctlinput(cmd, sa)
    210 	int cmd;
    211 	struct sockaddr *sa;
    212 {
    213 	register struct domain *dp;
    214 	register struct protosw *pr;
    215 
    216 	for (dp = domains; dp; dp = dp->dom_next)
    217 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    218 			if (pr->pr_ctlinput)
    219 				(*pr->pr_ctlinput)(cmd, sa, NULL);
    220 }
    221 
    222 void
    223 pfslowtimo(arg)
    224 	void *arg;
    225 {
    226 	register struct domain *dp;
    227 	register struct protosw *pr;
    228 
    229 	pfslowtimo_now++;
    230 
    231 	for (dp = domains; dp; dp = dp->dom_next)
    232 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    233 			if (pr->pr_slowtimo)
    234 				(*pr->pr_slowtimo)();
    235 	timeout(pfslowtimo, NULL, hz/2);
    236 }
    237 
    238 void
    239 pffasttimo(arg)
    240 	void *arg;
    241 {
    242 	register struct domain *dp;
    243 	register struct protosw *pr;
    244 
    245 	pffasttimo_now++;
    246 
    247 	for (dp = domains; dp; dp = dp->dom_next)
    248 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    249 			if (pr->pr_fasttimo)
    250 				(*pr->pr_fasttimo)();
    251 	timeout(pffasttimo, NULL, hz/5);
    252 }
    253