1/*	$NetBSD: protosw.h,v 1.43 2008/04/24 11:38:39 ad 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. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *	@(#)protosw.h	8.1 (Berkeley) 6/2/93
32 */
33
34#ifndef _SYS_PROTOSW_H_
35#define _SYS_PROTOSW_H_
36
37/*
38 * Protocol switch table.
39 *
40 * Each protocol has a handle initializing one of these structures,
41 * which is used for protocol-protocol and system-protocol communication.
42 *
43 * A protocol is called through the pr_init entry before any other.
44 * Thereafter it is called every 200ms through the pr_fasttimo entry and
45 * every 500ms through the pr_slowtimo for timer based actions.
46 * The system will call the pr_drain entry if it is low on space and
47 * this should throw away any non-critical data.
48 *
49 * Protocols pass data between themselves as chains of mbufs using
50 * the pr_input and pr_output hooks.  Pr_input passes data up (towards
51 * UNIX) and pr_output passes it down (towards the imps); control
52 * information passes up and down on pr_ctlinput and pr_ctloutput.
53 * The protocol is responsible for the space occupied by any the
54 * arguments to these entries and must dispose it.
55 *
56 * The userreq routine interfaces protocols to the system and is
57 * described below.
58 */
59
60struct mbuf;
61struct sockaddr;
62struct socket;
63struct sockopt;
64struct domain;
65struct proc;
66struct lwp;
67
68struct protosw {
69	int 	pr_type;		/* socket type used for */
70	struct	domain *pr_domain;	/* domain protocol a member of */
71	short	pr_protocol;		/* protocol number */
72	short	pr_flags;		/* see below */
73
74/* protocol-protocol hooks */
75	void	(*pr_input)		/* input to protocol (from below) */
76			(struct mbuf *, ...);
77	int	(*pr_output)		/* output to protocol (from above) */
78			(struct mbuf *, ...);
79	void	*(*pr_ctlinput)		/* control input (from below) */
80			(int, const struct sockaddr *, void *);
81	int	(*pr_ctloutput)		/* control output (from above) */
82			(int, struct socket *, struct sockopt *);
83
84/* user-protocol hook */
85	int	(*pr_usrreq)		/* user request: see list below */
86			(struct socket *, int, struct mbuf *,
87			     struct mbuf *, struct mbuf *, struct lwp *);
88
89/* utility hooks */
90	void	(*pr_init)		/* initialization hook */
91			(void);
92
93	void	(*pr_fasttimo)		/* fast timeout (200ms) */
94			(void);
95	void	(*pr_slowtimo)		/* slow timeout (500ms) */
96			(void);
97	void	(*pr_drain)		/* flush any excess space possible */
98			(void);
99};
100
101#define	PR_SLOWHZ	2		/* 2 slow timeouts per second */
102#define	PR_FASTHZ	5		/* 5 fast timeouts per second */
103
104/*
105 * Values for pr_flags.
106 * PR_ADDR requires PR_ATOMIC;
107 * PR_ADDR and PR_CONNREQUIRED are mutually exclusive.
108 */
109#define	PR_ATOMIC	0x01		/* exchange atomic messages only */
110#define	PR_ADDR		0x02		/* addresses given with messages */
111#define	PR_CONNREQUIRED	0x04		/* connection required by protocol */
112#define	PR_WANTRCVD	0x08		/* want PRU_RCVD calls */
113#define	PR_RIGHTS	0x10		/* passes capabilities */
114#define	PR_LISTEN	0x20		/* supports listen(2) and accept(2) */
115#define	PR_LASTHDR	0x40		/* enforce ipsec policy; last header */
116#define	PR_ABRTACPTDIS	0x80		/* abort on accept(2) to disconnected
117					   socket */
118#define PR_PURGEIF	0x100		/* might store struct ifnet pointer;
119					   PRU_PURGEIF must be called on ifnet
120					   deletion */
121
122/*
123 * The arguments to usrreq are:
124 *	(*protosw[].pr_usrreq)(up, req, m, nam, opt, p);
125 * where up is a (struct socket *), req is one of these requests,
126 * m is a optional mbuf chain containing a message,
127 * nam is an optional mbuf chain containing an address,
128 * opt is an optional mbuf containing socket options,
129 * and p is a pointer to the process requesting the action (if any).
130 * The protocol is responsible for disposal of the mbuf chains m and opt,
131 * the caller is responsible for any space held by nam.
132 * A non-zero return from usrreq gives an
133 * UNIX error number which should be passed to higher level software.
134 */
135#define	PRU_ATTACH		0	/* attach protocol to up */
136#define	PRU_DETACH		1	/* detach protocol from up */
137#define	PRU_BIND		2	/* bind socket to address */
138#define	PRU_LISTEN		3	/* listen for connection */
139#define	PRU_CONNECT		4	/* establish connection to peer */
140#define	PRU_ACCEPT		5	/* accept connection from peer */
141#define	PRU_DISCONNECT		6	/* disconnect from peer */
142#define	PRU_SHUTDOWN		7	/* won't send any more data */
143#define	PRU_RCVD		8	/* have taken data; more room now */
144#define	PRU_SEND		9	/* send this data */
145#define	PRU_ABORT		10	/* abort (fast DISCONNECT, DETACH) */
146#define	PRU_CONTROL		11	/* control operations on protocol */
147#define	PRU_SENSE		12	/* return status into m */
148#define	PRU_RCVOOB		13	/* retrieve out of band data */
149#define	PRU_SENDOOB		14	/* send out of band data */
150#define	PRU_SOCKADDR		15	/* fetch socket's address */
151#define	PRU_PEERADDR		16	/* fetch peer's address */
152#define	PRU_CONNECT2		17	/* connect two sockets */
153/* begin for protocols internal use */
154#define	PRU_FASTTIMO		18	/* 200ms timeout */
155#define	PRU_SLOWTIMO		19	/* 500ms timeout */
156#define	PRU_PROTORCV		20	/* receive from below */
157#define	PRU_PROTOSEND		21	/* send to below */
158#define	PRU_PURGEIF		22	/* purge specified if */
159
160#define	PRU_NREQ		23
161
162#ifdef PRUREQUESTS
163static const char * const prurequests[] = {
164	"ATTACH",	"DETACH",	"BIND",		"LISTEN",
165	"CONNECT",	"ACCEPT",	"DISCONNECT",	"SHUTDOWN",
166	"RCVD",		"SEND",		"ABORT",	"CONTROL",
167	"SENSE",	"RCVOOB",	"SENDOOB",	"SOCKADDR",
168	"PEERADDR",	"CONNECT2",	"FASTTIMO",	"SLOWTIMO",
169	"PROTORCV",	"PROTOSEND",	"PURGEIF",
170};
171#endif
172
173/*
174 * The arguments to the ctlinput routine are
175 *	(*protosw[].pr_ctlinput)(cmd, sa, arg);
176 * where cmd is one of the commands below, sa is a pointer to a sockaddr,
177 * and arg is an optional void *argument used within a protocol family.
178 */
179#define	PRC_IFDOWN		0	/* interface transition */
180#define	PRC_ROUTEDEAD		1	/* select new route if possible ??? */
181#define	PRC_QUENCH2		3	/* DEC congestion bit says slow down */
182#define	PRC_QUENCH		4	/* some one said to slow down */
183#define	PRC_MSGSIZE		5	/* message size forced drop */
184#define	PRC_HOSTDEAD		6	/* host appears to be down */
185#define	PRC_HOSTUNREACH		7	/* deprecated (use PRC_UNREACH_HOST) */
186#define	PRC_UNREACH_NET		8	/* no route to network */
187#define	PRC_UNREACH_HOST	9	/* no route to host */
188#define	PRC_UNREACH_PROTOCOL	10	/* dst says bad protocol */
189#define	PRC_UNREACH_PORT	11	/* bad port # */
190/* was	PRC_UNREACH_NEEDFRAG	12	   (use PRC_MSGSIZE) */
191#define	PRC_UNREACH_SRCFAIL	13	/* source route failed */
192#define	PRC_REDIRECT_NET	14	/* net routing redirect */
193#define	PRC_REDIRECT_HOST	15	/* host routing redirect */
194#define	PRC_REDIRECT_TOSNET	16	/* redirect for type of service & net */
195#define	PRC_REDIRECT_TOSHOST	17	/* redirect for tos & host */
196#define	PRC_TIMXCEED_INTRANS	18	/* packet lifetime expired in transit */
197#define	PRC_TIMXCEED_REASS	19	/* lifetime expired on reass q */
198#define	PRC_PARAMPROB		20	/* header incorrect */
199
200#define	PRC_NCMDS		21
201
202#define	PRC_IS_REDIRECT(cmd)	\
203	((cmd) >= PRC_REDIRECT_NET && (cmd) <= PRC_REDIRECT_TOSHOST)
204
205#ifdef PRCREQUESTS
206static const char * const prcrequests[] = {
207	"IFDOWN", "ROUTEDEAD", "#2", "DEC-BIT-QUENCH2",
208	"QUENCH", "MSGSIZE", "HOSTDEAD", "#7",
209	"NET-UNREACH", "HOST-UNREACH", "PROTO-UNREACH", "PORT-UNREACH",
210	"#12", "SRCFAIL-UNREACH", "NET-REDIRECT", "HOST-REDIRECT",
211	"TOSNET-REDIRECT", "TOSHOST-REDIRECT", "TX-INTRANS", "TX-REASS",
212	"PARAMPROB"
213};
214#endif
215
216/*
217 * The arguments to ctloutput are:
218 *	(*protosw[].pr_ctloutput)(req, so, sopt);
219 * req is one of the actions listed below, so is a (struct socket *),
220 * sopt is a (struct sockopt *)
221 * A non-zero return from usrreq gives an
222 * UNIX error number which should be passed to higher level software.
223 */
224#define	PRCO_GETOPT	0
225#define	PRCO_SETOPT	1
226
227#define	PRCO_NCMDS	2
228
229#ifdef PRCOREQUESTS
230static const char * const prcorequests[] = {
231	"GETOPT", "SETOPT",
232};
233#endif
234
235#ifdef _KERNEL
236/*
237 * Monotonically increasing time values for slow and fast timers.
238 */
239extern	u_int pfslowtimo_now;
240extern	u_int pffasttimo_now;
241
242#define	PRT_SLOW_ARM(t, nticks)	(t) = (pfslowtimo_now + (nticks))
243#define	PRT_FAST_ARM(t, nticks)	(t) = (pffasttimo_now + (nticks))
244
245#define	PRT_SLOW_DISARM(t)	(t) = 0
246#define	PRT_FAST_DISARM(t)	(t) = 0
247
248#define	PRT_SLOW_ISARMED(t)	((t) != 0)
249#define	PRT_FAST_ISARMED(t)	((t) != 0)
250
251#define	PRT_SLOW_ISEXPIRED(t)	(PRT_SLOW_ISARMED((t)) && (t) <= pfslowtimo_now)
252#define	PRT_FAST_ISEXPIRED(t)	(PRT_FAST_ISARMED((t)) && (t) <= pffasttimo_now)
253
254struct sockaddr;
255const struct protosw *pffindproto(int, int, int);
256const struct protosw *pffindtype(int, int);
257struct domain *pffinddomain(int);
258void pfctlinput(int, const struct sockaddr *);
259void pfctlinput2(int, const struct sockaddr *, void *);
260
261/*
262 * Wrappers for non-MPSAFE protocols
263 */
264#include <sys/systm.h>	/* kernel_lock */
265
266#define	PR_WRAP_USRREQ(name)				\
267static int						\
268name##_wrapper(struct socket *a, int b, struct mbuf *c,	\
269     struct mbuf *d, struct mbuf *e, struct lwp *f)	\
270{							\
271	int rv;						\
272	KERNEL_LOCK(1, NULL);				\
273	rv = name(a, b, c, d, e, f);			\
274	KERNEL_UNLOCK_ONE(NULL);			\
275	return rv;					\
276}
277
278#define	PR_WRAP_CTLOUTPUT(name)				\
279static int						\
280name##_wrapper(int a, struct socket *b,			\
281    struct sockopt *c)					\
282{							\
283	int rv;						\
284	KERNEL_LOCK(1, NULL);				\
285	rv = name(a, b, c);				\
286	KERNEL_UNLOCK_ONE(NULL);			\
287	return rv;					\
288}
289
290#define	PR_WRAP_CTLINPUT(name)				\
291static void *						\
292name##_wrapper(int a, const struct sockaddr *b, void *c)\
293{							\
294	void *rv;					\
295	KERNEL_LOCK(1, NULL);				\
296	rv = name(a, b, c);				\
297	KERNEL_UNLOCK_ONE(NULL);			\
298	return rv;					\
299}
300
301#endif /* _KERNEL */
302
303#endif /* !_SYS_PROTOSW_H_ */
304