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