1/*
2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
29/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30/*
31 * Copyright (c) 1982, 1986, 1993
32 *	The Regents of the University of California.  All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 *    notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 *    notice, this list of conditions and the following disclaimer in the
41 *    documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 *    must display the following acknowledgement:
44 *	This product includes software developed by the University of
45 *	California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 *    may be used to endorse or promote products derived from this software
48 *    without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 *	@(#)protosw.h	8.1 (Berkeley) 6/2/93
63 * $FreeBSD: src/sys/sys/protosw.h,v 1.28.2.2 2001/07/03 11:02:01 ume Exp $
64 */
65
66#ifndef _SYS_PROTOSW_H_
67#define _SYS_PROTOSW_H_
68
69#include <sys/appleapiopts.h>
70#include <sys/cdefs.h>
71
72#define	PR_SLOWHZ	2		/* 2 slow timeouts per second */
73#ifndef __APPLE__
74/*
75 * See rdar://7617868: pr_fasttimo was removed use your own timer or pr_slowtimo instead
76 */
77#define	PR_FASTHZ	5		/* 5 fast timeouts per second */
78#endif
79
80#ifdef PRIVATE
81
82/* Forward declare these structures referenced from prototypes below. */
83struct mbuf;
84struct proc;
85struct sockaddr;
86struct socket;
87struct sockopt;
88struct socket_filter;
89
90/*#ifdef _KERNEL*/
91/*
92 * Protocol switch table.
93 *
94 * Each protocol has a handle initializing one of these structures,
95 * which is used for protocol-protocol and system-protocol communication.
96 *
97 * A protocol is called through the pr_init entry before any other.
98 * Thereafter it is called every 200ms through the pr_fasttimo entry and
99 * every 500ms through the pr_slowtimo for timer based actions.
100 * The system will call the pr_drain entry if it is low on space and
101 * this should throw away any non-critical data.
102 *
103 * Protocols pass data between themselves as chains of mbufs using
104 * the pr_input and pr_output hooks.  Pr_input passes data up (towards
105 * the users) and pr_output passes it down (towards the interfaces); control
106 * information passes up and down on pr_ctlinput and pr_ctloutput.
107 * The protocol is responsible for the space occupied by any the
108 * arguments to these entries and must dispose it.
109 *
110 * The userreq routine interfaces protocols to the system and is
111 * described below.
112 */
113
114#include <sys/socket.h>
115#include <sys/socketvar.h>
116#include <sys/queue.h>
117#ifdef KERNEL
118#include <kern/locks.h>
119#endif /* KERNEL */
120
121#pragma pack(4)
122
123struct protosw {
124	short	pr_type;		/* socket type used for */
125	struct	domain *pr_domain;	/* domain protocol a member of */
126	short	pr_protocol;		/* protocol number */
127	unsigned int pr_flags;		/* see below */
128/* protocol-protocol hooks */
129	void	(*pr_input)(struct mbuf *, int len);
130					/* input to protocol (from below) */
131	int	(*pr_output)(struct mbuf *m, struct socket *so);
132					/* output to protocol (from above) */
133	void	(*pr_ctlinput)(int, struct sockaddr *, void *);
134					/* control input (from below) */
135	int	(*pr_ctloutput)(struct socket *, struct sockopt *);
136					/* control output (from above) */
137/* user-protocol hook */
138	void	*pr_ousrreq;
139/* utility hooks */
140	void	(*pr_init)(void);	/* initialization hook */
141#if __APPLE__
142	void	(*pr_unused)(void);	/* placeholder - fasttimo is removed */
143#else
144	void	(*pr_fasttimo)(void);
145					/* fast timeout (200ms) */
146#endif
147	void	(*pr_slowtimo)(void);
148					/* slow timeout (500ms) */
149	void	(*pr_drain)(void);
150					/* flush any excess space possible */
151#if __APPLE__
152	int	(*pr_sysctl)(int *, u_int, void *, size_t *, void *, size_t);
153					/* sysctl for protocol */
154#endif
155	struct	pr_usrreqs *pr_usrreqs;	/* supersedes pr_usrreq() */
156#if __APPLE__
157	int	(*pr_lock) 	(struct socket *so, int locktype, void *debug); /* lock function for protocol */
158	int	(*pr_unlock) 	(struct socket *so, int locktype, void *debug); /* unlock for protocol */
159#ifdef _KERN_LOCKS_H_
160	lck_mtx_t *	(*pr_getlock) 	(struct socket *so, int locktype);
161#else
162	void *	(*pr_getlock) 	(struct socket *so, int locktype);
163#endif
164#endif
165#if __APPLE__
166/* Implant hooks */
167	TAILQ_HEAD(, socket_filter) pr_filter_head;
168	struct protosw *pr_next;	/* Chain for domain */
169	u_int32_t	reserved[1];		/* Padding for future use */
170#endif
171};
172
173#pragma pack()
174
175/*
176 * Values for pr_flags.
177 * PR_ADDR requires PR_ATOMIC;
178 * PR_ADDR and PR_CONNREQUIRED are mutually exclusive.
179 * PR_IMPLOPCL means that the protocol allows sendto without prior connect,
180 *	and the protocol understands the MSG_EOF flag.  The first property is
181 *	is only relevant if PR_CONNREQUIRED is set (otherwise sendto is allowed
182 *	anyhow).
183 */
184#define	PR_ATOMIC			0x01		/* exchange atomic messages only */
185#define	PR_ADDR			0x02		/* addresses given with messages */
186#define	PR_CONNREQUIRED	0x04		/* connection required by protocol */
187#define	PR_WANTRCVD		0x08		/* want PRU_RCVD calls */
188#define	PR_RIGHTS			0x10		/* passes capabilities */
189#define	PR_IMPLOPCL		0x20		/* implied open/close */
190#define	PR_LASTHDR		0x40		/* enforce ipsec policy; last header */
191#define	PR_PROTOLOCK		0x80		/* protocol takes care of it's own locking */
192#define	PR_PCBLOCK		0x100	/* protocol supports per pcb finer grain locking */
193#define	PR_DISPOSE		0x200	/* protocol requires late lists disposal */
194#define	PR_AGGDRAIN		0x400	/* protocol requires aggressive draining */
195
196/*
197 * The arguments to usrreq are:
198 *	(*protosw[].pr_usrreq)(up, req, m, nam, opt);
199 * where up is a (struct socket *), req is one of these requests,
200 * m is a optional mbuf chain containing a message,
201 * nam is an optional mbuf chain containing an address,
202 * and opt is a pointer to a socketopt structure or nil.
203 * The protocol is responsible for disposal of the mbuf chain m,
204 * the caller is responsible for any space held by nam and opt.
205 * A non-zero return from usrreq gives an
206 * UNIX error number which should be passed to higher level software.
207 */
208#define	PRU_ATTACH		0	/* attach protocol to up */
209#define	PRU_DETACH		1	/* detach protocol from up */
210#define	PRU_BIND		2	/* bind socket to address */
211#define	PRU_LISTEN		3	/* listen for connection */
212#define	PRU_CONNECT		4	/* establish connection to peer */
213#define	PRU_ACCEPT		5	/* accept connection from peer */
214#define	PRU_DISCONNECT		6	/* disconnect from peer */
215#define	PRU_SHUTDOWN		7	/* won't send any more data */
216#define	PRU_RCVD		8	/* have taken data; more room now */
217#define	PRU_SEND		9	/* send this data */
218#define	PRU_ABORT		10	/* abort (fast DISCONNECT, DETATCH) */
219#define	PRU_CONTROL		11	/* control operations on protocol */
220#define	PRU_SENSE		12	/* return status into m */
221#define	PRU_RCVOOB		13	/* retrieve out of band data */
222#define	PRU_SENDOOB		14	/* send out of band data */
223#define	PRU_SOCKADDR		15	/* fetch socket's address */
224#define	PRU_PEERADDR		16	/* fetch peer's address */
225#define	PRU_CONNECT2		17	/* connect two sockets */
226/* begin for protocols internal use */
227#define	PRU_FASTTIMO		18	/* 200ms timeout */
228#define	PRU_SLOWTIMO		19	/* 500ms timeout */
229#define	PRU_PROTORCV		20	/* receive from below */
230#define	PRU_PROTOSEND		21	/* send to below */
231/* end for protocol's internal use */
232#define PRU_SEND_EOF		22	/* send and close */
233#define PRU_NREQ		22
234
235#ifdef PRUREQUESTS
236char *prurequests[] = {
237	"ATTACH",	"DETACH",	"BIND",		"LISTEN",
238	"CONNECT",	"ACCEPT",	"DISCONNECT",	"SHUTDOWN",
239	"RCVD",		"SEND",		"ABORT",	"CONTROL",
240	"SENSE",	"RCVOOB",	"SENDOOB",	"SOCKADDR",
241	"PEERADDR",	"CONNECT2",	"FASTTIMO",	"SLOWTIMO",
242	"PROTORCV",	"PROTOSEND",
243	"SEND_EOF",
244};
245#endif
246
247#ifdef	KERNEL			/* users shouldn't see this decl */
248
249struct ifnet;
250struct stat;
251struct ucred;
252struct uio;
253
254/*
255 * If the ordering here looks odd, that's because it's alphabetical.
256 * Having this structure separated out from the main protoswitch is allegedly
257 * a big (12 cycles per call) lose on high-end CPUs.  We will eventually
258 * migrate this stuff back into the main structure.
259 */
260struct pr_usrreqs {
261	int	(*pru_abort)(struct socket *so);
262	int	(*pru_accept)(struct socket *so, struct sockaddr **nam);
263	int	(*pru_attach)(struct socket *so, int proto, struct proc *p);
264	int	(*pru_bind)(struct socket *so, struct sockaddr *nam,
265				 struct proc *p);
266	int	(*pru_connect)(struct socket *so, struct sockaddr *nam,
267				    struct proc *p);
268	int	(*pru_connect2)(struct socket *so1, struct socket *so2);
269	int	(*pru_control)(struct socket *so, u_long cmd, caddr_t data,
270				    struct ifnet *ifp, struct proc *p);
271	int	(*pru_detach)(struct socket *so);
272	int	(*pru_disconnect)(struct socket *so);
273	int	(*pru_listen)(struct socket *so, struct proc *p);
274	int	(*pru_peeraddr)(struct socket *so, struct sockaddr **nam);
275	int	(*pru_rcvd)(struct socket *so, int flags);
276	int	(*pru_rcvoob)(struct socket *so, struct mbuf *m, int flags);
277	int	(*pru_send)(struct socket *so, int flags, struct mbuf *m,
278				 struct sockaddr *addr, struct mbuf *control,
279				 struct proc *p);
280#define	PRUS_OOB	0x1
281#define	PRUS_EOF	0x2
282#define	PRUS_MORETOCOME	0x4
283	int	(*pru_sense)(struct socket *so, void  *sb, int isstat64);
284	int	(*pru_shutdown)(struct socket *so);
285	int	(*pru_sockaddr)(struct socket *so, struct sockaddr **nam);
286
287	/*
288	 * These three added later, so they are out of order.  They are used
289	 * for shortcutting (fast path input/output) in some protocols.
290	 * XXX - that's a lie, they are not implemented yet
291	 * Rather than calling sosend() etc. directly, calls are made
292	 * through these entry points.  For protocols which still use
293	 * the generic code, these just point to those routines.
294	 */
295	int	(*pru_sosend)(struct socket *so, struct sockaddr *addr,
296				   struct uio *uio, struct mbuf *top,
297				   struct mbuf *control, int flags);
298	int	(*pru_soreceive)(struct socket *so,
299				      struct sockaddr **paddr,
300				      struct uio *uio, struct mbuf **mp0,
301				      struct mbuf **controlp, int *flagsp);
302	int	(*pru_sopoll)(struct socket *so, int events,
303				   struct ucred *cred, void *);
304};
305
306__BEGIN_DECLS
307
308extern int	pru_abort_notsupp(struct socket *so);
309extern int	pru_accept_notsupp(struct socket *so, struct sockaddr **nam);
310extern int	pru_attach_notsupp(struct socket *so, int proto,
311				   struct proc *p);
312extern int	pru_bind_notsupp(struct socket *so, struct sockaddr *nam,
313				 struct proc *p);
314extern int	pru_connect_notsupp(struct socket *so, struct sockaddr *nam,
315				    struct proc *p);
316extern int	pru_connect2_notsupp(struct socket *so1, struct socket *so2);
317extern int	pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
318				    struct ifnet *ifp, struct proc *p);
319extern int	pru_detach_notsupp(struct socket *so);
320extern int	pru_disconnect_notsupp(struct socket *so);
321extern int	pru_listen_notsupp(struct socket *so, struct proc *p);
322extern int	pru_peeraddr_notsupp(struct socket *so,
323				     struct sockaddr **nam);
324extern int	pru_rcvd_notsupp(struct socket *so, int flags);
325extern int	pru_rcvoob_notsupp(struct socket *so, struct mbuf *m,
326				   int flags);
327extern int	pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
328				 struct sockaddr *addr, struct mbuf *control,
329				 struct proc *p);
330extern int	pru_sense_null(struct socket *so, void * sb, int isstat64);
331extern int	pru_shutdown_notsupp(struct socket *so);
332extern int	pru_sockaddr_notsupp(struct socket *so,
333				     struct sockaddr **nam);
334extern int	pru_sosend_notsupp(struct socket *so, struct sockaddr *addr,
335				   struct uio *uio, struct mbuf *top,
336				   struct mbuf *control, int flags);
337extern int	pru_soreceive_notsupp(struct socket *so,
338				      struct sockaddr **paddr,
339				      struct uio *uio, struct mbuf **mp0,
340				      struct mbuf **controlp, int *flagsp);
341extern int	pru_sopoll_notsupp(struct socket *so, int events,
342				   struct ucred *cred, void *);
343
344__END_DECLS
345
346#endif /* KERNEL */
347
348/*
349 * The arguments to the ctlinput routine are
350 *	(*protosw[].pr_ctlinput)(cmd, sa, arg);
351 * where cmd is one of the commands below, sa is a pointer to a sockaddr,
352 * and arg is a `void *' argument used within a protocol family.
353 */
354#define	PRC_IFDOWN		0	/* interface transition */
355#define	PRC_ROUTEDEAD		1	/* select new route if possible ??? */
356#define	PRC_IFUP		2 	/* interface has come back up */
357#define	PRC_QUENCH2		3	/* DEC congestion bit says slow down */
358#define	PRC_QUENCH		4	/* some one said to slow down */
359#define	PRC_MSGSIZE		5	/* message size forced drop */
360#define	PRC_HOSTDEAD		6	/* host appears to be down */
361#define	PRC_HOSTUNREACH		7	/* deprecated (use PRC_UNREACH_HOST) */
362#define	PRC_UNREACH_NET		8	/* no route to network */
363#define	PRC_UNREACH_HOST	9	/* no route to host */
364#define	PRC_UNREACH_PROTOCOL	10	/* dst says bad protocol */
365#define	PRC_UNREACH_PORT	11	/* bad port # */
366/* was	PRC_UNREACH_NEEDFRAG	12	   (use PRC_MSGSIZE) */
367#define	PRC_UNREACH_SRCFAIL	13	/* source route failed */
368#define	PRC_REDIRECT_NET	14	/* net routing redirect */
369#define	PRC_REDIRECT_HOST	15	/* host routing redirect */
370#define	PRC_REDIRECT_TOSNET	16	/* redirect for type of service & net */
371#define	PRC_REDIRECT_TOSHOST	17	/* redirect for tos & host */
372#define	PRC_TIMXCEED_INTRANS	18	/* packet lifetime expired in transit */
373#define	PRC_TIMXCEED_REASS	19	/* lifetime expired on reass q */
374#define	PRC_PARAMPROB		20	/* header incorrect */
375#define	PRC_UNREACH_ADMIN_PROHIB	21	/* packet administrativly prohibited */
376
377#define	PRC_NCMDS		22
378
379#define	PRC_IS_REDIRECT(cmd)	\
380	((cmd) >= PRC_REDIRECT_NET && (cmd) <= PRC_REDIRECT_TOSHOST)
381
382#ifdef PRCREQUESTS
383char	*prcrequests[] = {
384	"IFDOWN", "ROUTEDEAD", "IFUP", "DEC-BIT-QUENCH2",
385	"QUENCH", "MSGSIZE", "HOSTDEAD", "#7",
386	"NET-UNREACH", "HOST-UNREACH", "PROTO-UNREACH", "PORT-UNREACH",
387	"#12", "SRCFAIL-UNREACH", "NET-REDIRECT", "HOST-REDIRECT",
388	"TOSNET-REDIRECT", "TOSHOST-REDIRECT", "TX-INTRANS", "TX-REASS",
389	"PARAMPROB", "ADMIN-UNREACH"
390};
391#endif
392
393/*
394 * The arguments to ctloutput are:
395 *	(*protosw[].pr_ctloutput)(req, so, level, optname, optval, p);
396 * req is one of the actions listed below, so is a (struct socket *),
397 * level is an indication of which protocol layer the option is intended.
398 * optname is a protocol dependent socket option request,
399 * optval is a pointer to a mbuf-chain pointer, for value-return results.
400 * The protocol is responsible for disposal of the mbuf chain *optval
401 * if supplied,
402 * the caller is responsible for any space held by *optval, when returned.
403 * A non-zero return from usrreq gives an
404 * UNIX error number which should be passed to higher level software.
405 */
406#define	PRCO_GETOPT	0
407#define	PRCO_SETOPT	1
408
409#define	PRCO_NCMDS	2
410
411#ifdef PRCOREQUESTS
412char	*prcorequests[] = {
413	"GETOPT", "SETOPT",
414};
415#endif
416
417#ifdef KERNEL
418
419__BEGIN_DECLS
420void domaininit(void) __attribute__((section("__TEXT, initcode")));
421void domainfin(void) __attribute__((section("__TEXT, fincode")));
422
423void	pfctlinput(int, struct sockaddr *);
424void	pfctlinput2(int, struct sockaddr *, void *);
425struct protosw *pffindproto(int family, int protocol, int type);
426struct protosw *pffindproto_locked(int family, int protocol, int type);
427struct protosw *pffindtype(int family, int type);
428
429extern int net_add_proto(struct protosw *, struct domain *);
430extern int net_del_proto(int, int, struct domain *);
431
432extern u_int64_t net_uptime(void);
433__END_DECLS
434
435/* Temp hack to link static domains together */
436
437#define LINK_PROTOS(psw) \
438static void link_ ## psw ## _protos() \
439{ \
440      int i; \
441		 \
442    for (i=0; i < ((sizeof(psw)/sizeof(psw[0])) - 1); i++) \
443	     psw[i].pr_next = &psw[i + 1]; \
444}
445
446#endif
447
448#endif /* PRIVATE */
449#endif	/* !_SYS_PROTOSW_H_ */
450