xref: /original-bsd/sys/kern/uipc_domain.c (revision a88537f5)
1 /*
2  * Copyright (c) 1982, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)uipc_domain.c	8.3 (Berkeley) 02/14/95
8  */
9 
10 #include <sys/param.h>
11 #include <sys/socket.h>
12 #include <sys/protosw.h>
13 #include <sys/domain.h>
14 #include <sys/mbuf.h>
15 #include <sys/time.h>
16 #include <sys/kernel.h>
17 #include <sys/systm.h>
18 #include <sys/proc.h>
19 #include <vm/vm.h>
20 #include <sys/sysctl.h>
21 
22 void	pffasttimo __P((void *));
23 void	pfslowtimo __P((void *));
24 
25 #define	ADDDOMAIN(x)	{ \
26 	extern struct domain __CONCAT(x,domain); \
27 	__CONCAT(x,domain.dom_next) = domains; \
28 	domains = &__CONCAT(x,domain); \
29 }
30 
31 void
domaininit()32 domaininit()
33 {
34 	register struct domain *dp;
35 	register struct protosw *pr;
36 
37 #undef unix
38 #ifndef lint
39 	ADDDOMAIN(unix);
40 	ADDDOMAIN(route);
41 #ifdef INET
42 	ADDDOMAIN(inet);
43 #endif
44 #ifdef NS
45 	ADDDOMAIN(ns);
46 #endif
47 #ifdef ISO
48 	ADDDOMAIN(iso);
49 #endif
50 #ifdef CCITT
51 	ADDDOMAIN(ccitt);
52 #endif
53 #include "imp.h"
54 #if NIMP > 0
55 	ADDDOMAIN(imp);
56 #endif
57 #endif
58 
59 	for (dp = domains; dp; dp = dp->dom_next) {
60 		if (dp->dom_init)
61 			(*dp->dom_init)();
62 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
63 			if (pr->pr_init)
64 				(*pr->pr_init)();
65 	}
66 
67 if (max_linkhdr < 16)		/* XXX */
68 max_linkhdr = 16;
69 	max_hdr = max_linkhdr + max_protohdr;
70 	max_datalen = MHLEN - max_hdr;
71 	timeout(pffasttimo, NULL, 1);
72 	timeout(pfslowtimo, NULL, 1);
73 }
74 
75 struct protosw *
pffindtype(family,type)76 pffindtype(family, type)
77 	int family, type;
78 {
79 	register struct domain *dp;
80 	register struct protosw *pr;
81 
82 	for (dp = domains; dp; dp = dp->dom_next)
83 		if (dp->dom_family == family)
84 			goto found;
85 	return (0);
86 found:
87 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
88 		if (pr->pr_type && pr->pr_type == type)
89 			return (pr);
90 	return (0);
91 }
92 
93 struct protosw *
pffindproto(family,protocol,type)94 pffindproto(family, protocol, type)
95 	int family, protocol, type;
96 {
97 	register struct domain *dp;
98 	register struct protosw *pr;
99 	struct protosw *maybe = 0;
100 
101 	if (family == 0)
102 		return (0);
103 	for (dp = domains; dp; dp = dp->dom_next)
104 		if (dp->dom_family == family)
105 			goto found;
106 	return (0);
107 found:
108 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
109 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
110 			return (pr);
111 
112 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
113 		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
114 			maybe = pr;
115 	}
116 	return (maybe);
117 }
118 
119 int
net_sysctl(name,namelen,oldp,oldlenp,newp,newlen,p)120 net_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
121 	int *name;
122 	u_int namelen;
123 	void *oldp;
124 	size_t *oldlenp;
125 	void *newp;
126 	size_t newlen;
127 	struct proc *p;
128 {
129 	register struct domain *dp;
130 	register struct protosw *pr;
131 	int family, protocol;
132 
133 	/*
134 	 * All sysctl names at this level are nonterminal;
135 	 * next two components are protocol family and protocol number,
136 	 * then at least one addition component.
137 	 */
138 	if (namelen < 3)
139 		return (EISDIR);		/* overloaded */
140 	family = name[0];
141 	protocol = name[1];
142 
143 	if (family == 0)
144 		return (0);
145 	for (dp = domains; dp; dp = dp->dom_next)
146 		if (dp->dom_family == family)
147 			goto found;
148 	return (ENOPROTOOPT);
149 found:
150 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
151 		if (pr->pr_protocol == protocol && pr->pr_sysctl)
152 			return ((*pr->pr_sysctl)(name + 2, namelen - 2,
153 			    oldp, oldlenp, newp, newlen));
154 	return (ENOPROTOOPT);
155 }
156 
157 void
pfctlinput(cmd,sa)158 pfctlinput(cmd, sa)
159 	int cmd;
160 	struct sockaddr *sa;
161 {
162 	register struct domain *dp;
163 	register struct protosw *pr;
164 
165 	for (dp = domains; dp; dp = dp->dom_next)
166 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
167 			if (pr->pr_ctlinput)
168 				(*pr->pr_ctlinput)(cmd, sa, (caddr_t)0);
169 }
170 
171 void
pfslowtimo(arg)172 pfslowtimo(arg)
173 	void *arg;
174 {
175 	register struct domain *dp;
176 	register struct protosw *pr;
177 
178 	for (dp = domains; dp; dp = dp->dom_next)
179 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
180 			if (pr->pr_slowtimo)
181 				(*pr->pr_slowtimo)();
182 	timeout(pfslowtimo, NULL, hz/2);
183 }
184 
185 void
pffasttimo(arg)186 pffasttimo(arg)
187 	void *arg;
188 {
189 	register struct domain *dp;
190 	register struct protosw *pr;
191 
192 	for (dp = domains; dp; dp = dp->dom_next)
193 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
194 			if (pr->pr_fasttimo)
195 				(*pr->pr_fasttimo)();
196 	timeout(pffasttimo, NULL, hz/5);
197 }
198