Home | History | Annotate | Line # | Download | only in kern
uipc_domain.c revision 1.41.2.5
      1  1.41.2.5     skrll /*	$NetBSD: uipc_domain.c,v 1.41.2.5 2005/01/24 08:35:36 skrll Exp $	*/
      2      1.12       cgd 
      3       1.1       cgd /*
      4      1.11   mycroft  * Copyright (c) 1982, 1986, 1993
      5      1.11   mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.41.2.2     skrll  * 3. Neither the name of the University nor the names of its contributors
     16       1.1       cgd  *    may be used to endorse or promote products derived from this software
     17       1.1       cgd  *    without specific prior written permission.
     18       1.1       cgd  *
     19       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1       cgd  * SUCH DAMAGE.
     30       1.1       cgd  *
     31      1.18      fvdl  *	@(#)uipc_domain.c	8.3 (Berkeley) 2/14/95
     32       1.1       cgd  */
     33      1.36     lukem 
     34      1.36     lukem #include <sys/cdefs.h>
     35  1.41.2.5     skrll __KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.41.2.5 2005/01/24 08:35:36 skrll Exp $");
     36       1.1       cgd 
     37       1.5   mycroft #include <sys/param.h>
     38       1.5   mycroft #include <sys/socket.h>
     39       1.5   mycroft #include <sys/protosw.h>
     40       1.5   mycroft #include <sys/domain.h>
     41       1.5   mycroft #include <sys/mbuf.h>
     42       1.5   mycroft #include <sys/time.h>
     43       1.5   mycroft #include <sys/kernel.h>
     44      1.11   mycroft #include <sys/systm.h>
     45      1.30   thorpej #include <sys/callout.h>
     46  1.41.2.5     skrll #include <sys/queue.h>
     47      1.10       cgd #include <sys/proc.h>
     48      1.10       cgd #include <sys/sysctl.h>
     49      1.13  christos 
     50  1.41.2.2     skrll void	pffasttimo(void *);
     51  1.41.2.2     skrll void	pfslowtimo(void *);
     52      1.37      matt 
     53  1.41.2.5     skrll struct domainhead domains = STAILQ_HEAD_INITIALIZER(domains);
     54      1.11   mycroft 
     55      1.30   thorpej struct callout pffasttimo_ch, pfslowtimo_ch;
     56      1.30   thorpej 
     57      1.19   thorpej /*
     58      1.19   thorpej  * Current time values for fast and slow timeouts.  We can use u_int
     59      1.19   thorpej  * relatively safely.  The fast timer will roll over in 27 years and
     60      1.19   thorpej  * the slow timer in 68 years.
     61      1.19   thorpej  */
     62      1.19   thorpej u_int	pfslowtimo_now;
     63      1.19   thorpej u_int	pffasttimo_now;
     64      1.19   thorpej 
     65       1.4    andrew void
     66       1.1       cgd domaininit()
     67       1.1       cgd {
     68  1.41.2.5     skrll 	__link_set_decl(domains, struct domain);
     69  1.41.2.5     skrll 	struct domain * const * dpp;
     70  1.41.2.5     skrll 	struct domain *rt_domain = NULL;
     71       1.1       cgd 
     72      1.26    itojun 	/*
     73  1.41.2.5     skrll 	 * Add all of the domains.  Make sure the PF_ROUTE
     74  1.41.2.5     skrll 	 * domain is added last.
     75      1.26    itojun 	 */
     76  1.41.2.5     skrll 	__link_set_foreach(dpp, domains) {
     77  1.41.2.5     skrll 		if ((*dpp)->dom_family == PF_ROUTE)
     78  1.41.2.5     skrll 			rt_domain = *dpp;
     79  1.41.2.5     skrll 		else
     80  1.41.2.5     skrll 			domain_attach(*dpp);
     81  1.41.2.5     skrll 	}
     82  1.41.2.5     skrll 	if (rt_domain)
     83  1.41.2.5     skrll 		domain_attach(rt_domain);
     84  1.41.2.5     skrll 
     85  1.41.2.5     skrll 	callout_init(&pffasttimo_ch);
     86  1.41.2.5     skrll 	callout_init(&pfslowtimo_ch);
     87  1.41.2.5     skrll 
     88  1.41.2.5     skrll 	callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
     89  1.41.2.5     skrll 	callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
     90  1.41.2.5     skrll }
     91  1.41.2.5     skrll 
     92  1.41.2.5     skrll void
     93  1.41.2.5     skrll domain_attach(struct domain *dp)
     94  1.41.2.5     skrll {
     95  1.41.2.5     skrll 	const struct protosw *pr;
     96  1.41.2.5     skrll 
     97  1.41.2.5     skrll 	STAILQ_INSERT_TAIL(&domains, dp, dom_link);
     98  1.41.2.5     skrll 
     99  1.41.2.5     skrll 	if (dp->dom_init)
    100  1.41.2.5     skrll 		(*dp->dom_init)();
    101       1.1       cgd 
    102      1.38      matt #ifdef MBUFTRACE
    103  1.41.2.5     skrll 	if (dp->dom_mowner.mo_name[0] == '\0') {
    104  1.41.2.5     skrll 		strncpy(dp->dom_mowner.mo_name, dp->dom_name,
    105  1.41.2.5     skrll 		    sizeof(dp->dom_mowner.mo_name));
    106  1.41.2.5     skrll 		MOWNER_ATTACH(&dp->dom_mowner);
    107  1.41.2.5     skrll 	}
    108      1.38      matt #endif
    109  1.41.2.5     skrll 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    110  1.41.2.5     skrll 		if (pr->pr_init)
    111  1.41.2.5     skrll 			(*pr->pr_init)();
    112       1.1       cgd 	}
    113       1.1       cgd 
    114      1.16  explorer 	if (max_linkhdr < 16)		/* XXX */
    115      1.16  explorer 		max_linkhdr = 16;
    116       1.1       cgd 	max_hdr = max_linkhdr + max_protohdr;
    117       1.1       cgd 	max_datalen = MHLEN - max_hdr;
    118       1.1       cgd }
    119       1.1       cgd 
    120      1.29   thorpej struct domain *
    121  1.41.2.5     skrll pffinddomain(int family)
    122      1.29   thorpej {
    123      1.29   thorpej 	struct domain *dp;
    124      1.29   thorpej 
    125  1.41.2.5     skrll 	DOMAIN_FOREACH(dp)
    126      1.29   thorpej 		if (dp->dom_family == family)
    127      1.29   thorpej 			return (dp);
    128      1.29   thorpej 	return (NULL);
    129      1.29   thorpej }
    130      1.29   thorpej 
    131  1.41.2.2     skrll const struct protosw *
    132  1.41.2.5     skrll pffindtype(int family, int type)
    133       1.1       cgd {
    134      1.29   thorpej 	struct domain *dp;
    135  1.41.2.2     skrll 	const struct protosw *pr;
    136      1.29   thorpej 
    137      1.29   thorpej 	dp = pffinddomain(family);
    138      1.29   thorpej 	if (dp == NULL)
    139      1.29   thorpej 		return (NULL);
    140       1.1       cgd 
    141       1.1       cgd 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    142       1.1       cgd 		if (pr->pr_type && pr->pr_type == type)
    143       1.1       cgd 			return (pr);
    144      1.29   thorpej 
    145      1.29   thorpej 	return (NULL);
    146       1.1       cgd }
    147       1.1       cgd 
    148  1.41.2.2     skrll const struct protosw *
    149  1.41.2.5     skrll pffindproto(int family, int protocol, int type)
    150       1.1       cgd {
    151      1.29   thorpej 	struct domain *dp;
    152  1.41.2.2     skrll 	const struct protosw *pr;
    153  1.41.2.2     skrll 	const struct protosw *maybe = NULL;
    154       1.1       cgd 
    155       1.1       cgd 	if (family == 0)
    156      1.29   thorpej 		return (NULL);
    157      1.29   thorpej 
    158      1.29   thorpej 	dp = pffinddomain(family);
    159      1.29   thorpej 	if (dp == NULL)
    160      1.29   thorpej 		return (NULL);
    161      1.29   thorpej 
    162       1.1       cgd 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    163       1.1       cgd 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
    164       1.1       cgd 			return (pr);
    165       1.1       cgd 
    166       1.1       cgd 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
    167      1.29   thorpej 		    pr->pr_protocol == 0 && maybe == NULL)
    168       1.1       cgd 			maybe = pr;
    169       1.1       cgd 	}
    170       1.1       cgd 	return (maybe);
    171      1.10       cgd }
    172      1.10       cgd 
    173  1.41.2.2     skrll SYSCTL_SETUP(sysctl_net_setup, "sysctl net subtree setup")
    174      1.10       cgd {
    175  1.41.2.2     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    176  1.41.2.2     skrll 		       CTLFLAG_PERMANENT,
    177  1.41.2.2     skrll 		       CTLTYPE_NODE, "net", NULL,
    178  1.41.2.2     skrll 		       NULL, 0, NULL, 0,
    179  1.41.2.2     skrll 		       CTL_NET, CTL_EOL);
    180  1.41.2.2     skrll 
    181  1.41.2.2     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    182  1.41.2.2     skrll 		       CTLFLAG_PERMANENT,
    183  1.41.2.2     skrll 		       CTLTYPE_NODE, "local",
    184  1.41.2.2     skrll 		       SYSCTL_DESCR("PF_LOCAL related settings"),
    185  1.41.2.2     skrll 		       NULL, 0, NULL, 0,
    186  1.41.2.2     skrll 		       CTL_NET, PF_LOCAL, CTL_EOL);
    187      1.10       cgd 
    188      1.10       cgd 	/*
    189  1.41.2.2     skrll 	 * other protocols are expected to have their own setup
    190  1.41.2.2     skrll 	 * routines that will do everything.  we end up not having
    191  1.41.2.2     skrll 	 * anything at all to do.
    192      1.10       cgd 	 */
    193       1.1       cgd }
    194       1.1       cgd 
    195       1.4    andrew void
    196  1.41.2.5     skrll pfctlinput(int cmd, struct sockaddr *sa)
    197       1.1       cgd {
    198      1.31  augustss 	struct domain *dp;
    199  1.41.2.2     skrll 	const struct protosw *pr;
    200       1.1       cgd 
    201  1.41.2.5     skrll 	DOMAIN_FOREACH(dp)
    202       1.1       cgd 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    203       1.1       cgd 			if (pr->pr_ctlinput)
    204      1.13  christos 				(*pr->pr_ctlinput)(cmd, sa, NULL);
    205      1.34    itojun }
    206      1.34    itojun 
    207      1.34    itojun void
    208  1.41.2.5     skrll pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
    209      1.34    itojun {
    210      1.34    itojun 	struct domain *dp;
    211  1.41.2.2     skrll 	const struct protosw *pr;
    212      1.34    itojun 
    213      1.34    itojun 	if (!sa)
    214      1.34    itojun 		return;
    215  1.41.2.5     skrll 
    216  1.41.2.5     skrll 	DOMAIN_FOREACH(dp) {
    217      1.34    itojun 		/*
    218      1.34    itojun 		 * the check must be made by xx_ctlinput() anyways, to
    219      1.34    itojun 		 * make sure we use data item pointed to by ctlparam in
    220      1.34    itojun 		 * correct way.  the following check is made just for safety.
    221      1.34    itojun 		 */
    222      1.34    itojun 		if (dp->dom_family != sa->sa_family)
    223      1.34    itojun 			continue;
    224      1.34    itojun 
    225      1.34    itojun 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    226      1.34    itojun 			if (pr->pr_ctlinput)
    227      1.34    itojun 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
    228      1.34    itojun 	}
    229       1.1       cgd }
    230       1.1       cgd 
    231       1.4    andrew void
    232  1.41.2.5     skrll pfslowtimo(void *arg)
    233       1.1       cgd {
    234      1.31  augustss 	struct domain *dp;
    235  1.41.2.2     skrll 	const struct protosw *pr;
    236       1.1       cgd 
    237      1.19   thorpej 	pfslowtimo_now++;
    238      1.19   thorpej 
    239  1.41.2.5     skrll 	DOMAIN_FOREACH(dp) {
    240       1.1       cgd 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    241       1.1       cgd 			if (pr->pr_slowtimo)
    242       1.1       cgd 				(*pr->pr_slowtimo)();
    243  1.41.2.5     skrll 	}
    244      1.30   thorpej 	callout_reset(&pfslowtimo_ch, hz / 2, pfslowtimo, NULL);
    245       1.1       cgd }
    246       1.1       cgd 
    247       1.4    andrew void
    248  1.41.2.5     skrll pffasttimo(void *arg)
    249       1.1       cgd {
    250      1.31  augustss 	struct domain *dp;
    251  1.41.2.2     skrll 	const struct protosw *pr;
    252      1.19   thorpej 
    253      1.19   thorpej 	pffasttimo_now++;
    254       1.1       cgd 
    255  1.41.2.5     skrll 	DOMAIN_FOREACH(dp) {
    256       1.1       cgd 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    257       1.1       cgd 			if (pr->pr_fasttimo)
    258       1.1       cgd 				(*pr->pr_fasttimo)();
    259  1.41.2.5     skrll 	}
    260      1.30   thorpej 	callout_reset(&pffasttimo_ch, hz / 5, pffasttimo, NULL);
    261       1.1       cgd }
    262