kernel: Fix error for dup2 if the new file descriptor is out of bounds.
[dragonfly.git] / sys / sys / protosw.h
1 /*-
2  * Copyright (c) 1982, 1986, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)protosw.h   8.1 (Berkeley) 6/2/93
34  * $FreeBSD: src/sys/sys/protosw.h,v 1.28.2.2 2001/07/03 11:02:01 ume Exp $
35  */
36
37 #ifndef _SYS_PROTOSW_H_
38 #define _SYS_PROTOSW_H_
39
40 #ifndef _SYS_TYPES_H_
41 #include <sys/types.h>
42 #endif
43
44 /* Forward declare these structures referenced from prototypes below. */
45 struct mbuf;
46 struct thread;
47 struct sockaddr;
48 struct socket;
49 struct sockopt;
50
51 struct pr_output_info {
52         pid_t   p_pid;
53 };
54
55 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
56
57 /*
58  * netmsg_t union of possible netmsgs typically sent to protocol threads.
59  */
60 typedef union netmsg *netmsg_t;
61
62 /*
63  * Protocol switch table.
64  *
65  * Each protocol has a handle initializing one of these structures,
66  * which is used for protocol-protocol and system-protocol communication.
67  *
68  * A protocol is called through the pr_init entry before any other.
69  * Thereafter it is called every 200ms through the pr_fasttimo entry and
70  * every 500ms through the pr_slowtimo for timer based actions.
71  * The system will call the pr_drain entry if it is low on space and
72  * this should throw away any non-critical data.
73  *
74  * Protocols pass data between themselves as chains of mbufs using
75  * the pr_input and pr_output hooks.  Pr_input passes data up (towards
76  * the users) and pr_output passes it down (towards the interfaces); control
77  * information passes up and down on pr_ctlinput and pr_ctloutput.
78  * The protocol is responsible for the space occupied by any the
79  * arguments to these entries and must dispose it.
80  *
81  * In retrospect, it would be a lot nicer to use an interface
82  * similar to the vnode VOP interface.
83  */
84 struct protosw {
85         short   pr_type;                /* socket type used for */
86         const struct domain *pr_domain; /* domain protocol a member of */
87         short   pr_protocol;            /* protocol number */
88         short   pr_flags;               /* see below */
89
90         struct lwkt_port *(*pr_initport)(void);
91                                         /* initial msgport */
92
93         /*
94          * Protocol hooks.  These are typically called directly within the
95          * context of a protocol thread based on the toeplitz hash.
96          *
97          * pr_input() is called using the port supplied by the toeplitz
98          *            hash via the netisr port function.
99          *
100          * pr_ctlinput() is called using the port supplied by pr_ctlport
101          *
102          * pr_ctloutput() and pr_output() are typically called
103          */
104         int     (*pr_input)(struct mbuf **, int *, int);
105                                         /* input to protocol (from below) */
106         int     (*pr_output)(struct mbuf *, struct socket *, ...);
107                                         /* output to protocol (from above) */
108         void    (*pr_ctlinput)(netmsg_t);
109                                         /* control input (from below) */
110         void    (*pr_ctloutput)(netmsg_t);
111                                         /* control output (from above) */
112         struct lwkt_port *(*pr_ctlport)(int, struct sockaddr *, void *);
113
114         /*
115          * Utility hooks, not called with any particular context.
116          */
117         void    (*pr_init) (void);      /* initialization hook */
118         void    (*pr_fasttimo) (void);  /* fast timeout (200ms) */
119         void    (*pr_slowtimo) (void);  /* slow timeout (500ms) */
120         void    (*pr_drain) (void);     /* flush any excess space possible */
121
122         struct  pr_usrreqs *pr_usrreqs; /* messaged requests to proto thread */
123 };
124
125 #endif
126
127 #define PR_SLOWHZ       2               /* 2 slow timeouts per second */
128 #define PR_FASTHZ       5               /* 5 fast timeouts per second */
129
130 /*
131  * Values for pr_flags.
132  * PR_ADDR requires PR_ATOMIC;
133  * PR_ADDR and PR_CONNREQUIRED are mutually exclusive.
134  * PR_IMPLOPCL means that the protocol allows sendto without prior connect,
135  *      and the protocol understands the MSG_EOF flag.  The first property is
136  *      is only relevant if PR_CONNREQUIRED is set (otherwise sendto is allowed
137  *      anyhow).
138  */
139 #define PR_ATOMIC       0x01            /* exchange atomic messages only */
140 #define PR_ADDR         0x02            /* addresses given with messages */
141 #define PR_CONNREQUIRED 0x04            /* connection required by protocol */
142 #define PR_WANTRCVD     0x08            /* want PRU_RCVD calls */
143 #define PR_RIGHTS       0x10            /* passes capabilities */
144 #define PR_IMPLOPCL     0x20            /* implied open/close */
145 #define PR_LASTHDR      0x40            /* enforce ipsec policy; last header */
146 #define PR_ADDR_OPT     0x80            /* allow addresses during delivery */
147 #define PR_MPSAFE       0x0100          /* protocal is MPSAFE */
148 #define PR_SYNC_PORT    0x0200          /* synchronous port (no proto thrds) */
149 #define PR_ASYNC_SEND   0x0400          /* async pru_send */
150 #define PR_ASYNC_RCVD   0x0800          /* async pru_rcvd */
151 #define PR_ASEND_HOLDTD 0x1000          /* async pru_send hold orig thread */
152 #define PR_ACONN_HOLDTD 0x2000          /* async pru_connect hold orig thread */
153
154 /*
155  * The arguments to usrreq are:
156  *      (*protosw[].pr_usrreq)(up, req, m, nam, opt);
157  * where up is a (struct socket *), req is one of these requests,
158  * m is a optional mbuf chain containing a message,
159  * nam is an optional mbuf chain containing an address,
160  * and opt is a pointer to a socketopt structure or nil.
161  * The protocol is responsible for disposal of the mbuf chain m,
162  * the caller is responsible for any space held by nam and opt.
163  * A non-zero return from usrreq gives an
164  * UNIX error number which should be passed to higher level software.
165  */
166 #define PRU_ATTACH              0       /* attach protocol to up */
167 #define PRU_DETACH              1       /* detach protocol from up */
168 #define PRU_BIND                2       /* bind socket to address */
169 #define PRU_LISTEN              3       /* listen for connection */
170 #define PRU_CONNECT             4       /* establish connection to peer */
171 #define PRU_ACCEPT              5       /* accept connection from peer */
172 #define PRU_DISCONNECT          6       /* disconnect from peer */
173 #define PRU_SHUTDOWN            7       /* won't send any more data */
174 #define PRU_RCVD                8       /* have taken data; more room now */
175 #define PRU_SEND                9       /* send this data */
176 #define PRU_ABORT               10      /* abort (fast DISCONNECT, DETATCH) */
177 #define PRU_CONTROL             11      /* control operations on protocol */
178 #define PRU_SENSE               12      /* return status into m */
179 #define PRU_RCVOOB              13      /* retrieve out of band data */
180 #define PRU_SENDOOB             14      /* send out of band data */
181 #define PRU_SOCKADDR            15      /* fetch socket's address */
182 #define PRU_PEERADDR            16      /* fetch peer's address */
183 #define PRU_CONNECT2            17      /* connect two sockets */
184 #define PRU_RESERVED1           18      /* formerly PRU_SOPOLL */
185 /* begin for protocols internal use */
186 #define PRU_FASTTIMO            19      /* 200ms timeout */
187 #define PRU_SLOWTIMO            20      /* 500ms timeout */
188 #define PRU_PROTORCV            21      /* receive from below */
189 #define PRU_PROTOSEND           22      /* send to below */
190 /* end for protocol's internal use */
191 #define PRU_SEND_EOF            23      /* send and close */
192 #define PRU_PRED                24
193 #define PRU_CTLOUTPUT           25      /* get/set opts */
194 #define PRU_NREQ                26
195
196 #ifdef PRUREQUESTS
197 const char *prurequests[] = {
198         "ATTACH",       "DETACH",       "BIND",         "LISTEN",
199         "CONNECT",      "ACCEPT",       "DISCONNECT",   "SHUTDOWN",
200         "RCVD",         "SEND",         "ABORT",        "CONTROL",
201         "SENSE",        "RCVOOB",       "SENDOOB",      "SOCKADDR",
202         "PEERADDR",     "CONNECT2",     "",
203         "FASTTIMO",     "SLOWTIMO",     "PROTORCV",     "PROTOSEND",
204         "SEND_EOF",     "PREDICATE"
205 };
206 #endif
207
208 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
209
210 struct ifnet;
211 struct stat;
212 struct ucred;
213 struct uio;
214 struct sockbuf;
215
216 struct pru_attach_info {
217         struct rlimit *sb_rlimit;
218         struct ucred *p_ucred;
219         struct vnode *fd_rdir;
220 };
221
222 /*
223  * These are netmsg'd requests almost universally in the context of the
224  * appropriate protocol thread.  Exceptions:
225  *
226  *      pru_sosend() - called synchronously from user context, typically
227  *                     runs generic kernel code and then messages via
228  *                     pru_send().
229  *
230  *      pru_soreceive() - called synchronously from user context.  Typically
231  *                        runs generic kernel code and remains synchronous.
232  *
233  *      pru_preconnect() - called synchronously from user context.  Typically
234  *                         prepares for later asynchronous pru_connect, e.g.
235  *                         sets ISCONNECTING.  Non-NULL means asynchronous
236  *                         pru_connect is supported.
237  */
238 struct pr_usrreqs {
239         void    (*pru_abort) (netmsg_t msg);
240         void    (*pru_accept) (netmsg_t msg);
241         void    (*pru_attach) (netmsg_t msg);
242         void    (*pru_bind) (netmsg_t msg);
243         void    (*pru_connect) (netmsg_t msg);
244         void    (*pru_connect2) (netmsg_t msg);
245         void    (*pru_control) (netmsg_t msg);
246         void    (*pru_detach) (netmsg_t msg);
247         void    (*pru_disconnect) (netmsg_t msg);
248         void    (*pru_listen) (netmsg_t msg);
249         void    (*pru_peeraddr) (netmsg_t msg);
250         void    (*pru_rcvd) (netmsg_t msg);
251         void    (*pru_rcvoob) (netmsg_t msg);
252         void    (*pru_send) (netmsg_t msg);
253         void    (*pru_sense) (netmsg_t msg);
254         void    (*pru_shutdown) (netmsg_t msg);
255         void    (*pru_sockaddr) (netmsg_t msg);
256
257         /*
258          * These are direct calls.  Note that sosend() will sometimes
259          * be converted into an implied connect (pru_connect) with the
260          * mbufs and flags forwarded in pru_connect's netmsg.  It is
261          * otherwise typically converted to a send (pru_send).
262          *
263          * soreceive() typically remains synchronous in the user's context.
264          *
265          * Any converted calls are netmsg's to the socket's protocol thread.
266          */
267         int     (*pru_sosend) (struct socket *so, struct sockaddr *addr,
268                                    struct uio *uio, struct mbuf *top,
269                                    struct mbuf *control, int flags,
270                                    struct thread *td);
271         int     (*pru_soreceive) (struct socket *so, 
272                                       struct sockaddr **paddr,
273                                       struct uio *uio,
274                                       struct sockbuf *sio,
275                                       struct mbuf **controlp, int *flagsp);
276
277         /*
278          * Synchronously called by protocol thread.  Typically save the
279          * foreign address into socket.so_faddr.
280          */
281         void    (*pru_savefaddr) (struct socket *so,
282                                       const struct sockaddr *addr);
283
284         /* synchronously called by user thread. */
285         int     (*pru_preconnect) (struct socket *so,
286                                       const struct sockaddr *addr,
287                                       struct thread *td);
288 };
289
290 typedef int (*pru_sosend_fn_t) (struct socket *so, struct sockaddr *addr,
291                                         struct uio *uio, struct mbuf *top,
292                                         struct mbuf *control, int flags,
293                                         struct thread *td);
294 typedef int (*pru_soreceive_fn_t) (struct socket *so, struct sockaddr **paddr,
295                                         struct uio *uio,
296                                         struct sockbuf *sio,
297                                         struct mbuf **controlp,
298                                         int *flagsp);
299
300 void    pr_generic_notsupp(netmsg_t msg);
301 void    pru_sense_null(netmsg_t msg);
302
303 int     pru_sosend_notsupp(struct socket *so, struct sockaddr *addr,
304                                 struct uio *uio, struct mbuf *top,
305                                 struct mbuf *control, int flags,
306                                 struct thread *td);
307 int     pru_soreceive_notsupp(struct socket *so,
308                                 struct sockaddr **paddr,
309                                 struct uio *uio,
310                                 struct sockbuf *sio,
311                                 struct mbuf **controlp, int *flagsp);
312
313 struct lwkt_port *cpu0_soport(struct socket *, struct sockaddr *,
314                               struct mbuf **);
315 struct lwkt_port *cpu0_ctlport(int, struct sockaddr *, void *);
316
317 #endif /* _KERNEL || _KERNEL_STRUCTURES */
318
319 /*
320  * The arguments to the ctlinput routine are
321  *      (*protosw[].pr_ctlinput)(cmd, sa, arg);
322  * where cmd is one of the commands below, sa is a pointer to a sockaddr,
323  * and arg is a `void *' argument used within a protocol family.
324  */
325 #define PRC_IFDOWN              0       /* interface transition */
326 #define PRC_ROUTEDEAD           1       /* select new route if possible ??? */
327 #define PRC_IFUP                2       /* interface has come back up */
328 #define PRC_QUENCH2             3       /* DEC congestion bit says slow down */
329 #define PRC_QUENCH              4       /* some one said to slow down */
330 #define PRC_MSGSIZE             5       /* message size forced drop */
331 #define PRC_HOSTDEAD            6       /* host appears to be down */
332 #define PRC_HOSTUNREACH         7       /* deprecated (use PRC_UNREACH_HOST) */
333 #define PRC_UNREACH_NET         8       /* no route to network */
334 #define PRC_UNREACH_HOST        9       /* no route to host */
335 #define PRC_UNREACH_PROTOCOL    10      /* dst says bad protocol */
336 #define PRC_UNREACH_PORT        11      /* bad port # */
337 /* was  PRC_UNREACH_NEEDFRAG    12         (use PRC_MSGSIZE) */
338 #define PRC_UNREACH_SRCFAIL     13      /* source route failed */
339 #define PRC_REDIRECT_NET        14      /* net routing redirect */
340 #define PRC_REDIRECT_HOST       15      /* host routing redirect */
341 #define PRC_REDIRECT_TOSNET     16      /* redirect for type of service & net */
342 #define PRC_REDIRECT_TOSHOST    17      /* redirect for tos & host */
343 #define PRC_TIMXCEED_INTRANS    18      /* packet lifetime expired in transit */
344 #define PRC_TIMXCEED_REASS      19      /* lifetime expired on reass q */
345 #define PRC_PARAMPROB           20      /* header incorrect */
346 #define PRC_UNREACH_ADMIN_PROHIB        21      /* packet administrativly prohibited */
347
348 #define PRC_NCMDS               22
349
350 #define PRC_IS_REDIRECT(cmd)    \
351         ((cmd) >= PRC_REDIRECT_NET && (cmd) <= PRC_REDIRECT_TOSHOST)
352
353 #ifdef PRCREQUESTS
354 const char *prcrequests[] = {
355         "IFDOWN", "ROUTEDEAD", "IFUP", "DEC-BIT-QUENCH2",
356         "QUENCH", "MSGSIZE", "HOSTDEAD", "#7",
357         "NET-UNREACH", "HOST-UNREACH", "PROTO-UNREACH", "PORT-UNREACH",
358         "#12", "SRCFAIL-UNREACH", "NET-REDIRECT", "HOST-REDIRECT",
359         "TOSNET-REDIRECT", "TOSHOST-REDIRECT", "TX-INTRANS", "TX-REASS",
360         "PARAMPROB", "ADMIN-UNREACH"
361 };
362 #endif
363
364 /*
365  * The arguments to ctloutput are:
366  *      (*protosw[].pr_ctloutput)(req, so, level, optname, optval, p);
367  * req is one of the actions listed below, so is a (struct socket *),
368  * level is an indication of which protocol layer the option is intended.
369  * optname is a protocol dependent socket option request,
370  * optval is a pointer to a mbuf-chain pointer, for value-return results.
371  * The protocol is responsible for disposal of the mbuf chain *optval
372  * if supplied,
373  * the caller is responsible for any space held by *optval, when returned.
374  * A non-zero return from usrreq gives an
375  * UNIX error number which should be passed to higher level software.
376  */
377 #define PRCO_GETOPT     0
378 #define PRCO_SETOPT     1
379
380 #define PRCO_NCMDS      2
381
382 #ifdef PRCOREQUESTS
383 const char *prcorequests[] = {
384         "GETOPT", "SETOPT",
385 };
386 #endif
387
388 /*
389  * Kernel prototypes
390  */
391 #ifdef _KERNEL
392
393 void    kpfctlinput (int, struct sockaddr *);
394 void    kpfctlinput2 (int, struct sockaddr *, void *);
395 struct protosw *pffindproto (int family, int protocol, int type);
396 struct protosw *pffindtype (int family, int type);
397
398 #define PR_GET_MPLOCK(_pr) \
399 do { \
400         if (((_pr)->pr_flags & PR_MPSAFE) == 0) \
401                 get_mplock(); \
402 } while (0)
403
404 #define PR_REL_MPLOCK(_pr) \
405 do { \
406         if (((_pr)->pr_flags & PR_MPSAFE) == 0) \
407                 rel_mplock(); \
408 } while (0)
409
410 #endif  /* _KERNEL */
411
412 #endif  /* _SYS_PROTOSW_H_ */