Home | History | Annotate | Line # | Download | only in kern
uipc_domain.c revision 1.14
      1 /*	$NetBSD: uipc_domain.c,v 1.14 1996/02/09 19:00:44 christos 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 notdef /* XXXX */
     82 #include "imp.h"
     83 #if NIMP > 0
     84 	ADDDOMAIN(imp);
     85 #endif
     86 #endif
     87 #endif
     88 
     89 	for (dp = domains; dp; dp = dp->dom_next) {
     90 		if (dp->dom_init)
     91 			(*dp->dom_init)();
     92 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
     93 			if (pr->pr_init)
     94 				(*pr->pr_init)();
     95 	}
     96 
     97 if (max_linkhdr < 16)		/* XXX */
     98 max_linkhdr = 16;
     99 	max_hdr = max_linkhdr + max_protohdr;
    100 	max_datalen = MHLEN - max_hdr;
    101 	timeout(pffasttimo, NULL, 1);
    102 	timeout(pfslowtimo, NULL, 1);
    103 }
    104 
    105 struct protosw *
    106 pffindtype(family, type)
    107 	int family, type;
    108 {
    109 	register struct domain *dp;
    110 	register struct protosw *pr;
    111 
    112 	for (dp = domains; dp; dp = dp->dom_next)
    113 		if (dp->dom_family == family)
    114 			goto found;
    115 	return (0);
    116 found:
    117 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    118 		if (pr->pr_type && pr->pr_type == type)
    119 			return (pr);
    120 	return (0);
    121 }
    122 
    123 struct protosw *
    124 pffindproto(family, protocol, type)
    125 	int family, protocol, type;
    126 {
    127 	register struct domain *dp;
    128 	register struct protosw *pr;
    129 	struct protosw *maybe = 0;
    130 
    131 	if (family == 0)
    132 		return (0);
    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_protocol == protocol) && (pr->pr_type == type))
    140 			return (pr);
    141 
    142 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
    143 		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
    144 			maybe = pr;
    145 	}
    146 	return (maybe);
    147 }
    148 
    149 int
    150 net_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    151 	int *name;
    152 	u_int namelen;
    153 	void *oldp;
    154 	size_t *oldlenp;
    155 	void *newp;
    156 	size_t newlen;
    157 	struct proc *p;
    158 {
    159 	register struct domain *dp;
    160 	register struct protosw *pr;
    161 	int family, protocol;
    162 
    163 	/*
    164 	 * All sysctl names at this level are nonterminal;
    165 	 * next two components are protocol family and protocol number,
    166 	 * then at least one addition component.
    167 	 */
    168 	if (namelen < 3)
    169 		return (EISDIR);		/* overloaded */
    170 	family = name[0];
    171 	protocol = name[1];
    172 
    173 	if (family == 0)
    174 		return (0);
    175 	for (dp = domains; dp; dp = dp->dom_next)
    176 		if (dp->dom_family == family)
    177 			goto found;
    178 	return (ENOPROTOOPT);
    179 found:
    180 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    181 		if (pr->pr_protocol == protocol && pr->pr_sysctl)
    182 			return ((*pr->pr_sysctl)(name + 2, namelen - 2,
    183 			    oldp, oldlenp, newp, newlen));
    184 	return (ENOPROTOOPT);
    185 }
    186 
    187 void
    188 pfctlinput(cmd, sa)
    189 	int cmd;
    190 	struct sockaddr *sa;
    191 {
    192 	register struct domain *dp;
    193 	register struct protosw *pr;
    194 
    195 	for (dp = domains; dp; dp = dp->dom_next)
    196 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    197 			if (pr->pr_ctlinput)
    198 				(*pr->pr_ctlinput)(cmd, sa, NULL);
    199 }
    200 
    201 void
    202 pfslowtimo(arg)
    203 	void *arg;
    204 {
    205 	register struct domain *dp;
    206 	register struct protosw *pr;
    207 
    208 	for (dp = domains; dp; dp = dp->dom_next)
    209 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    210 			if (pr->pr_slowtimo)
    211 				(*pr->pr_slowtimo)();
    212 	timeout(pfslowtimo, NULL, hz/2);
    213 }
    214 
    215 void
    216 pffasttimo(arg)
    217 	void *arg;
    218 {
    219 	register struct domain *dp;
    220 	register struct protosw *pr;
    221 
    222 	for (dp = domains; dp; dp = dp->dom_next)
    223 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    224 			if (pr->pr_fasttimo)
    225 				(*pr->pr_fasttimo)();
    226 	timeout(pffasttimo, NULL, hz/5);
    227 }
    228