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