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