]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/protosw.h
xnu-792.21.3.tar.gz
[apple/xnu.git] / bsd / sys / protosw.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, 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 #define PR_FASTHZ 5 /* 5 fast timeouts per second */
74
75 #ifdef PRIVATE
76
77 /* Forward declare these structures referenced from prototypes below. */
78 struct mbuf;
79 struct proc;
80 struct sockaddr;
81 struct socket;
82 struct sockopt;
83 struct socket_filter;
84
85 /*#ifdef _KERNEL*/
86 /*
87 * Protocol switch table.
88 *
89 * Each protocol has a handle initializing one of these structures,
90 * which is used for protocol-protocol and system-protocol communication.
91 *
92 * A protocol is called through the pr_init entry before any other.
93 * Thereafter it is called every 200ms through the pr_fasttimo entry and
94 * every 500ms through the pr_slowtimo for timer based actions.
95 * The system will call the pr_drain entry if it is low on space and
96 * this should throw away any non-critical data.
97 *
98 * Protocols pass data between themselves as chains of mbufs using
99 * the pr_input and pr_output hooks. Pr_input passes data up (towards
100 * the users) and pr_output passes it down (towards the interfaces); control
101 * information passes up and down on pr_ctlinput and pr_ctloutput.
102 * The protocol is responsible for the space occupied by any the
103 * arguments to these entries and must dispose it.
104 *
105 * The userreq routine interfaces protocols to the system and is
106 * described below.
107 */
108
109 #include <sys/socketvar.h>
110 #include <sys/queue.h>
111 #ifdef KERNEL
112 #include <kern/locks.h>
113 #endif /* KERNEL */
114
115 #if __DARWIN_ALIGN_POWER
116 #pragma options align=power
117 #endif
118
119 struct protosw {
120 short pr_type; /* socket type used for */
121 struct domain *pr_domain; /* domain protocol a member of */
122 short pr_protocol; /* protocol number */
123 unsigned int pr_flags; /* see below */
124 /* protocol-protocol hooks */
125 void (*pr_input)(struct mbuf *, int len);
126 /* input to protocol (from below) */
127 int (*pr_output)(struct mbuf *m, struct socket *so);
128 /* output to protocol (from above) */
129 void (*pr_ctlinput)(int, struct sockaddr *, void *);
130 /* control input (from below) */
131 int (*pr_ctloutput)(struct socket *, struct sockopt *);
132 /* control output (from above) */
133 /* user-protocol hook */
134 void *pr_ousrreq;
135 /* utility hooks */
136 void (*pr_init)(void); /* initialization hook */
137 void (*pr_fasttimo)(void);
138 /* fast timeout (200ms) */
139 void (*pr_slowtimo)(void);
140 /* slow timeout (500ms) */
141 void (*pr_drain)(void);
142 /* flush any excess space possible */
143 #if __APPLE__
144 int (*pr_sysctl)(int *, u_int, void *, size_t *, void *, size_t);
145 /* sysctl for protocol */
146 #endif
147 struct pr_usrreqs *pr_usrreqs; /* supersedes pr_usrreq() */
148 #if __APPLE__
149 int (*pr_lock) (struct socket *so, int locktype, int debug); /* lock function for protocol */
150 int (*pr_unlock) (struct socket *so, int locktype, int debug); /* unlock for protocol */
151 #ifdef _KERN_LOCKS_H_
152 lck_mtx_t * (*pr_getlock) (struct socket *so, int locktype);
153 #else
154 void * (*pr_getlock) (struct socket *so, int locktype);
155 #endif
156 #endif
157 #if __APPLE__
158 /* Implant hooks */
159 TAILQ_HEAD(, socket_filter) pr_filter_head;
160 struct protosw *pr_next; /* Chain for domain */
161 u_long reserved[1]; /* Padding for future use */
162 #endif
163 };
164
165 #if __DARWIN_ALIGN_POWER
166 #pragma options align=reset
167 #endif
168
169 /*
170 * Values for pr_flags.
171 * PR_ADDR requires PR_ATOMIC;
172 * PR_ADDR and PR_CONNREQUIRED are mutually exclusive.
173 * PR_IMPLOPCL means that the protocol allows sendto without prior connect,
174 * and the protocol understands the MSG_EOF flag. The first property is
175 * is only relevant if PR_CONNREQUIRED is set (otherwise sendto is allowed
176 * anyhow).
177 */
178 #define PR_ATOMIC 0x01 /* exchange atomic messages only */
179 #define PR_ADDR 0x02 /* addresses given with messages */
180 #define PR_CONNREQUIRED 0x04 /* connection required by protocol */
181 #define PR_WANTRCVD 0x08 /* want PRU_RCVD calls */
182 #define PR_RIGHTS 0x10 /* passes capabilities */
183 #define PR_IMPLOPCL 0x20 /* implied open/close */
184 #define PR_LASTHDR 0x40 /* enforce ipsec policy; last header */
185 #define PR_PROTOLOCK 0x80 /* protocol takes care of it's own locking */
186 #define PR_PCBLOCK 0x100 /* protocol supports per pcb finer grain locking */
187 #define PR_DISPOSE 0x200 /* protocol requires late lists disposal */
188
189 /*
190 * The arguments to usrreq are:
191 * (*protosw[].pr_usrreq)(up, req, m, nam, opt);
192 * where up is a (struct socket *), req is one of these requests,
193 * m is a optional mbuf chain containing a message,
194 * nam is an optional mbuf chain containing an address,
195 * and opt is a pointer to a socketopt structure or nil.
196 * The protocol is responsible for disposal of the mbuf chain m,
197 * the caller is responsible for any space held by nam and opt.
198 * A non-zero return from usrreq gives an
199 * UNIX error number which should be passed to higher level software.
200 */
201 #define PRU_ATTACH 0 /* attach protocol to up */
202 #define PRU_DETACH 1 /* detach protocol from up */
203 #define PRU_BIND 2 /* bind socket to address */
204 #define PRU_LISTEN 3 /* listen for connection */
205 #define PRU_CONNECT 4 /* establish connection to peer */
206 #define PRU_ACCEPT 5 /* accept connection from peer */
207 #define PRU_DISCONNECT 6 /* disconnect from peer */
208 #define PRU_SHUTDOWN 7 /* won't send any more data */
209 #define PRU_RCVD 8 /* have taken data; more room now */
210 #define PRU_SEND 9 /* send this data */
211 #define PRU_ABORT 10 /* abort (fast DISCONNECT, DETATCH) */
212 #define PRU_CONTROL 11 /* control operations on protocol */
213 #define PRU_SENSE 12 /* return status into m */
214 #define PRU_RCVOOB 13 /* retrieve out of band data */
215 #define PRU_SENDOOB 14 /* send out of band data */
216 #define PRU_SOCKADDR 15 /* fetch socket's address */
217 #define PRU_PEERADDR 16 /* fetch peer's address */
218 #define PRU_CONNECT2 17 /* connect two sockets */
219 /* begin for protocols internal use */
220 #define PRU_FASTTIMO 18 /* 200ms timeout */
221 #define PRU_SLOWTIMO 19 /* 500ms timeout */
222 #define PRU_PROTORCV 20 /* receive from below */
223 #define PRU_PROTOSEND 21 /* send to below */
224 /* end for protocol's internal use */
225 #define PRU_SEND_EOF 22 /* send and close */
226 #define PRU_NREQ 22
227
228 #ifdef PRUREQUESTS
229 char *prurequests[] = {
230 "ATTACH", "DETACH", "BIND", "LISTEN",
231 "CONNECT", "ACCEPT", "DISCONNECT", "SHUTDOWN",
232 "RCVD", "SEND", "ABORT", "CONTROL",
233 "SENSE", "RCVOOB", "SENDOOB", "SOCKADDR",
234 "PEERADDR", "CONNECT2", "FASTTIMO", "SLOWTIMO",
235 "PROTORCV", "PROTOSEND",
236 "SEND_EOF",
237 };
238 #endif
239
240 #ifdef KERNEL /* users shouldn't see this decl */
241
242 struct ifnet;
243 struct stat;
244 struct ucred;
245 struct uio;
246
247 /*
248 * If the ordering here looks odd, that's because it's alphabetical.
249 * Having this structure separated out from the main protoswitch is allegedly
250 * a big (12 cycles per call) lose on high-end CPUs. We will eventually
251 * migrate this stuff back into the main structure.
252 */
253 struct pr_usrreqs {
254 int (*pru_abort)(struct socket *so);
255 int (*pru_accept)(struct socket *so, struct sockaddr **nam);
256 int (*pru_attach)(struct socket *so, int proto, struct proc *p);
257 int (*pru_bind)(struct socket *so, struct sockaddr *nam,
258 struct proc *p);
259 int (*pru_connect)(struct socket *so, struct sockaddr *nam,
260 struct proc *p);
261 int (*pru_connect2)(struct socket *so1, struct socket *so2);
262 int (*pru_control)(struct socket *so, u_long cmd, caddr_t data,
263 struct ifnet *ifp, struct proc *p);
264 int (*pru_detach)(struct socket *so);
265 int (*pru_disconnect)(struct socket *so);
266 int (*pru_listen)(struct socket *so, struct proc *p);
267 int (*pru_peeraddr)(struct socket *so, struct sockaddr **nam);
268 int (*pru_rcvd)(struct socket *so, int flags);
269 int (*pru_rcvoob)(struct socket *so, struct mbuf *m, int flags);
270 int (*pru_send)(struct socket *so, int flags, struct mbuf *m,
271 struct sockaddr *addr, struct mbuf *control,
272 struct proc *p);
273 #define PRUS_OOB 0x1
274 #define PRUS_EOF 0x2
275 #define PRUS_MORETOCOME 0x4
276 int (*pru_sense)(struct socket *so, struct stat *sb);
277 int (*pru_shutdown)(struct socket *so);
278 int (*pru_sockaddr)(struct socket *so, struct sockaddr **nam);
279
280 /*
281 * These three added later, so they are out of order. They are used
282 * for shortcutting (fast path input/output) in some protocols.
283 * XXX - that's a lie, they are not implemented yet
284 * Rather than calling sosend() etc. directly, calls are made
285 * through these entry points. For protocols which still use
286 * the generic code, these just point to those routines.
287 */
288 int (*pru_sosend)(struct socket *so, struct sockaddr *addr,
289 struct uio *uio, struct mbuf *top,
290 struct mbuf *control, int flags);
291 int (*pru_soreceive)(struct socket *so,
292 struct sockaddr **paddr,
293 struct uio *uio, struct mbuf **mp0,
294 struct mbuf **controlp, int *flagsp);
295 int (*pru_sopoll)(struct socket *so, int events,
296 struct ucred *cred, void *);
297 };
298
299 __BEGIN_DECLS
300
301 extern int pru_abort_notsupp(struct socket *so);
302 extern int pru_accept_notsupp(struct socket *so, struct sockaddr **nam);
303 extern int pru_attach_notsupp(struct socket *so, int proto,
304 struct proc *p);
305 extern int pru_bind_notsupp(struct socket *so, struct sockaddr *nam,
306 struct proc *p);
307 extern int pru_connect_notsupp(struct socket *so, struct sockaddr *nam,
308 struct proc *p);
309 extern int pru_connect2_notsupp(struct socket *so1, struct socket *so2);
310 extern int pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
311 struct ifnet *ifp, struct proc *p);
312 extern int pru_detach_notsupp(struct socket *so);
313 extern int pru_disconnect_notsupp(struct socket *so);
314 extern int pru_listen_notsupp(struct socket *so, struct proc *p);
315 extern int pru_peeraddr_notsupp(struct socket *so,
316 struct sockaddr **nam);
317 extern int pru_rcvd_notsupp(struct socket *so, int flags);
318 extern int pru_rcvoob_notsupp(struct socket *so, struct mbuf *m,
319 int flags);
320 extern int pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
321 struct sockaddr *addr, struct mbuf *control,
322 struct proc *p);
323 extern int pru_sense_null(struct socket *so, struct stat *sb);
324 extern int pru_shutdown_notsupp(struct socket *so);
325 extern int pru_sockaddr_notsupp(struct socket *so,
326 struct sockaddr **nam);
327 extern int pru_sosend_notsupp(struct socket *so, struct sockaddr *addr,
328 struct uio *uio, struct mbuf *top,
329 struct mbuf *control, int flags);
330 extern int pru_soreceive_notsupp(struct socket *so,
331 struct sockaddr **paddr,
332 struct uio *uio, struct mbuf **mp0,
333 struct mbuf **controlp, int *flagsp);
334 extern int pru_sopoll_notsupp(struct socket *so, int events,
335 struct ucred *cred, void *);
336
337 __END_DECLS
338
339 #endif /* KERNEL */
340
341 /*
342 * The arguments to the ctlinput routine are
343 * (*protosw[].pr_ctlinput)(cmd, sa, arg);
344 * where cmd is one of the commands below, sa is a pointer to a sockaddr,
345 * and arg is a `void *' argument used within a protocol family.
346 */
347 #define PRC_IFDOWN 0 /* interface transition */
348 #define PRC_ROUTEDEAD 1 /* select new route if possible ??? */
349 #define PRC_IFUP 2 /* interface has come back up */
350 #define PRC_QUENCH2 3 /* DEC congestion bit says slow down */
351 #define PRC_QUENCH 4 /* some one said to slow down */
352 #define PRC_MSGSIZE 5 /* message size forced drop */
353 #define PRC_HOSTDEAD 6 /* host appears to be down */
354 #define PRC_HOSTUNREACH 7 /* deprecated (use PRC_UNREACH_HOST) */
355 #define PRC_UNREACH_NET 8 /* no route to network */
356 #define PRC_UNREACH_HOST 9 /* no route to host */
357 #define PRC_UNREACH_PROTOCOL 10 /* dst says bad protocol */
358 #define PRC_UNREACH_PORT 11 /* bad port # */
359 /* was PRC_UNREACH_NEEDFRAG 12 (use PRC_MSGSIZE) */
360 #define PRC_UNREACH_SRCFAIL 13 /* source route failed */
361 #define PRC_REDIRECT_NET 14 /* net routing redirect */
362 #define PRC_REDIRECT_HOST 15 /* host routing redirect */
363 #define PRC_REDIRECT_TOSNET 16 /* redirect for type of service & net */
364 #define PRC_REDIRECT_TOSHOST 17 /* redirect for tos & host */
365 #define PRC_TIMXCEED_INTRANS 18 /* packet lifetime expired in transit */
366 #define PRC_TIMXCEED_REASS 19 /* lifetime expired on reass q */
367 #define PRC_PARAMPROB 20 /* header incorrect */
368 #define PRC_UNREACH_ADMIN_PROHIB 21 /* packet administrativly prohibited */
369
370 #define PRC_NCMDS 22
371
372 #define PRC_IS_REDIRECT(cmd) \
373 ((cmd) >= PRC_REDIRECT_NET && (cmd) <= PRC_REDIRECT_TOSHOST)
374
375 #ifdef PRCREQUESTS
376 char *prcrequests[] = {
377 "IFDOWN", "ROUTEDEAD", "IFUP", "DEC-BIT-QUENCH2",
378 "QUENCH", "MSGSIZE", "HOSTDEAD", "#7",
379 "NET-UNREACH", "HOST-UNREACH", "PROTO-UNREACH", "PORT-UNREACH",
380 "#12", "SRCFAIL-UNREACH", "NET-REDIRECT", "HOST-REDIRECT",
381 "TOSNET-REDIRECT", "TOSHOST-REDIRECT", "TX-INTRANS", "TX-REASS",
382 "PARAMPROB", "ADMIN-UNREACH"
383 };
384 #endif
385
386 /*
387 * The arguments to ctloutput are:
388 * (*protosw[].pr_ctloutput)(req, so, level, optname, optval, p);
389 * req is one of the actions listed below, so is a (struct socket *),
390 * level is an indication of which protocol layer the option is intended.
391 * optname is a protocol dependent socket option request,
392 * optval is a pointer to a mbuf-chain pointer, for value-return results.
393 * The protocol is responsible for disposal of the mbuf chain *optval
394 * if supplied,
395 * the caller is responsible for any space held by *optval, when returned.
396 * A non-zero return from usrreq gives an
397 * UNIX error number which should be passed to higher level software.
398 */
399 #define PRCO_GETOPT 0
400 #define PRCO_SETOPT 1
401
402 #define PRCO_NCMDS 2
403
404 #ifdef PRCOREQUESTS
405 char *prcorequests[] = {
406 "GETOPT", "SETOPT",
407 };
408 #endif
409
410 #ifdef KERNEL
411
412 __BEGIN_DECLS
413
414 void pfctlinput(int, struct sockaddr *);
415 void pfctlinput2(int, struct sockaddr *, void *);
416 struct protosw *pffindproto(int family, int protocol, int type);
417 struct protosw *pffindproto_locked(int family, int protocol, int type);
418 struct protosw *pffindtype(int family, int type);
419
420 extern int net_add_proto(struct protosw *, struct domain *);
421 extern int net_del_proto(int, int, struct domain *);
422
423 __END_DECLS
424
425 /* Temp hack to link static domains together */
426
427 #define LINK_PROTOS(psw) \
428 static void link_ ## psw ## _protos() \
429 { \
430 int i; \
431 \
432 for (i=0; i < ((sizeof(psw)/sizeof(psw[0])) - 1); i++) \
433 psw[i].pr_next = &psw[i + 1]; \
434 }
435
436 #endif
437
438 #endif /* PRIVATE */
439 #endif /* !_SYS_PROTOSW_H_ */