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