1 /* $OpenBSD: protosw.h,v 1.70 2024/10/29 12:40:17 jsg Exp $ */
2 /* $NetBSD: protosw.h,v 1.10 1996/04/09 20:55:32 cgd Exp $ */
3
4 /*-
5 * Copyright (c) 1982, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)protosw.h 8.1 (Berkeley) 6/2/93
33 */
34
35 /*
36 * Protocol switch table.
37 *
38 * Each protocol has a handle initializing one of these structures,
39 * which is used for protocol-protocol and system-protocol communication.
40 *
41 * A protocol is called through the pr_init entry before any other.
42 * Thereafter it is called every 200ms through the pr_fasttimo entry and
43 * every 500ms through the pr_slowtimo for timer based actions.
44 *
45 * Protocols pass data between themselves as chains of mbufs using
46 * the pr_input and pr_send hooks. Pr_input passes data up (towards
47 * UNIX) and pr_send passes it down (towards the imps); control
48 * information passes up and down on pr_ctlinput and pr_ctloutput.
49 * The protocol is responsible for the space occupied by any the
50 * arguments to these entries and must dispose it.
51 *
52 * The userreq routine interfaces protocols to the system and is
53 * described below.
54 */
55
56 #ifndef _SYS_PROTOSW_H_
57 #define _SYS_PROTOSW_H_
58
59 struct mbuf;
60 struct sockaddr;
61 struct socket;
62 struct domain;
63 struct proc;
64 struct stat;
65 struct ifnet;
66
67 struct pr_usrreqs {
68 int (*pru_attach)(struct socket *, int, int);
69 int (*pru_detach)(struct socket *);
70 int (*pru_bind)(struct socket *, struct mbuf *, struct proc *);
71 int (*pru_listen)(struct socket *);
72 int (*pru_connect)(struct socket *, struct mbuf *);
73 int (*pru_accept)(struct socket *, struct mbuf *);
74 int (*pru_disconnect)(struct socket *);
75 int (*pru_shutdown)(struct socket *);
76 void (*pru_rcvd)(struct socket *);
77 int (*pru_send)(struct socket *, struct mbuf *, struct mbuf *,
78 struct mbuf *);
79 void (*pru_abort)(struct socket *);
80 int (*pru_control)(struct socket *, u_long, caddr_t,
81 struct ifnet *);
82 int (*pru_sense)(struct socket *, struct stat *);
83 int (*pru_rcvoob)(struct socket *, struct mbuf *, int);
84 int (*pru_sendoob)(struct socket *, struct mbuf *, struct mbuf *,
85 struct mbuf *);
86 int (*pru_sockaddr)(struct socket *, struct mbuf *);
87 int (*pru_peeraddr)(struct socket *, struct mbuf *);
88 int (*pru_connect2)(struct socket *, struct socket *);
89 };
90
91 struct protosw {
92 short pr_type; /* socket type used for */
93 const struct domain *pr_domain; /* domain protocol a member of */
94 short pr_protocol; /* protocol number */
95 short pr_flags; /* see below */
96
97 /* protocol-protocol hooks */
98 /* input to protocol (from below) */
99 int (*pr_input)(struct mbuf **, int *, int, int);
100 /* control input (from below) */
101 void (*pr_ctlinput)(int, struct sockaddr *, u_int, void *);
102 /* control output (from above) */
103 int (*pr_ctloutput)(int, struct socket *, int, int, struct mbuf *);
104
105 /* user-protocol hooks */
106 const struct pr_usrreqs *pr_usrreqs;
107
108 /* utility hooks */
109 void (*pr_init)(void); /* initialization hook */
110 void (*pr_fasttimo)(void); /* fast timeout (200ms) */
111 void (*pr_slowtimo)(void); /* slow timeout (500ms) */
112 /* sysctl for protocol */
113 int (*pr_sysctl)(int *, u_int, void *, size_t *, void *, size_t);
114 };
115
116 #define PR_SLOWHZ 2 /* 2 slow timeouts per second */
117 #define PR_FASTHZ 5 /* 5 fast timeouts per second */
118
119 /*
120 * Values for pr_flags.
121 * PR_ADDR requires PR_ATOMIC;
122 * PR_ADDR and PR_CONNREQUIRED are mutually exclusive.
123 */
124 #define PR_ATOMIC 0x0001 /* exchange atomic messages only */
125 #define PR_ADDR 0x0002 /* addresses given with messages */
126 #define PR_CONNREQUIRED 0x0004 /* connection required by protocol */
127 #define PR_WANTRCVD 0x0008 /* want PRU_RCVD calls */
128 #define PR_RIGHTS 0x0010 /* passes capabilities */
129 #define PR_ABRTACPTDIS 0x0020 /* abort on accept(2) to disconnected
130 socket */
131 #define PR_SPLICE 0x0040 /* socket splicing is possible */
132 #define PR_MPINPUT 0x0080 /* input runs with shared netlock */
133 #define PR_MPSOCKET 0x0100 /* socket uses shared netlock */
134 #define PR_MPSYSCTL 0x0200 /* mp-safe sysctl(2) handler */
135
136 /*
137 * The arguments to usrreq are:
138 * (*protosw[].pr_usrreq)(up, req, m, nam, opt);
139 * where up is a (struct socket *), req is one of these requests,
140 * m is a optional mbuf chain containing a message,
141 * nam is an optional mbuf chain containing an address,
142 * and opt is a pointer to a socketopt structure or nil.
143 * The protocol is responsible for disposal of the mbuf chain m,
144 * the caller is responsible for any space held by nam and opt.
145 * A non-zero return from usrreq gives an
146 * UNIX error number which should be passed to higher level software.
147 */
148 #define PRU_ATTACH 0 /* attach protocol to up */
149 #define PRU_DETACH 1 /* detach protocol from up */
150 #define PRU_BIND 2 /* bind socket to address */
151 #define PRU_LISTEN 3 /* listen for connection */
152 #define PRU_CONNECT 4 /* establish connection to peer */
153 #define PRU_ACCEPT 5 /* accept connection from peer */
154 #define PRU_DISCONNECT 6 /* disconnect from peer */
155 #define PRU_SHUTDOWN 7 /* won't send any more data */
156 #define PRU_RCVD 8 /* have taken data; more room now */
157 #define PRU_SEND 9 /* send this data */
158 #define PRU_ABORT 10 /* abort (fast DISCONNECT, DETACH) */
159 #define PRU_CONTROL 11 /* control operations on protocol */
160 #define PRU_SENSE 12 /* return status into m */
161 #define PRU_RCVOOB 13 /* retrieve out of band data */
162 #define PRU_SENDOOB 14 /* send out of band data */
163 #define PRU_SOCKADDR 15 /* fetch socket's address */
164 #define PRU_PEERADDR 16 /* fetch peer's address */
165 #define PRU_CONNECT2 17 /* connect two sockets */
166 /* begin for protocols internal use */
167 #define PRU_FASTTIMO 18 /* 200ms timeout */
168 #define PRU_SLOWTIMO 19 /* 500ms timeout */
169 #define PRU_PROTORCV 20 /* receive from below */
170 #define PRU_PROTOSEND 21 /* send to below */
171
172 #define PRU_NREQ 22
173
174 #ifdef PRUREQUESTS
175 const char *prurequests[] = {
176 "ATTACH", "DETACH", "BIND", "LISTEN",
177 "CONNECT", "ACCEPT", "DISCONNECT", "SHUTDOWN",
178 "RCVD", "SEND", "ABORT", "CONTROL",
179 "SENSE", "RCVOOB", "SENDOOB", "SOCKADDR",
180 "PEERADDR", "CONNECT2", "FASTTIMO", "SLOWTIMO",
181 "PROTORCV", "PROTOSEND",
182 };
183 #endif
184
185 /*
186 * The arguments to the ctlinput routine are
187 * (*protosw[].pr_ctlinput)(cmd, sa, arg);
188 * where cmd is one of the commands below, sa is a pointer to a sockaddr,
189 * and arg is an optional caddr_t argument used within a protocol family.
190 */
191 #define PRC_IFDOWN 0 /* interface transition */
192 #define PRC_ROUTEDEAD 1 /* select new route if possible ??? */
193 #define PRC_MTUINC 2 /* increase in mtu to host */
194 #define PRC_QUENCH2 3 /* DEC congestion bit says slow down */
195 #define PRC_QUENCH 4 /* some one said to slow down */
196 #define PRC_MSGSIZE 5 /* message size forced drop */
197 #define PRC_HOSTDEAD 6 /* host appears to be down */
198 #define PRC_HOSTUNREACH 7 /* deprecated (use PRC_UNREACH_HOST) */
199 #define PRC_UNREACH_NET 8 /* no route to network */
200 #define PRC_UNREACH_HOST 9 /* no route to host */
201 #define PRC_UNREACH_PROTOCOL 10 /* dst says bad protocol */
202 #define PRC_UNREACH_PORT 11 /* bad port # */
203 /* was PRC_UNREACH_NEEDFRAG 12 (use PRC_MSGSIZE) */
204 #define PRC_UNREACH_SRCFAIL 13 /* source route failed */
205 #define PRC_REDIRECT_NET 14 /* net routing redirect */
206 #define PRC_REDIRECT_HOST 15 /* host routing redirect */
207 #define PRC_REDIRECT_TOSNET 16 /* redirect for type of service & net */
208 #define PRC_REDIRECT_TOSHOST 17 /* redirect for tos & host */
209 #define PRC_TIMXCEED_INTRANS 18 /* packet lifetime expired in transit */
210 #define PRC_TIMXCEED_REASS 19 /* lifetime expired on reass q */
211 #define PRC_PARAMPROB 20 /* header incorrect */
212
213 #define PRC_NCMDS 21
214
215 #define PRC_IS_REDIRECT(cmd) \
216 ((cmd) >= PRC_REDIRECT_NET && (cmd) <= PRC_REDIRECT_TOSHOST)
217
218 #ifdef PRCREQUESTS
219 char *prcrequests[] = {
220 "IFDOWN", "ROUTEDEAD", "MTUINC", "DEC-BIT-QUENCH2",
221 "QUENCH", "MSGSIZE", "HOSTDEAD", "#7",
222 "NET-UNREACH", "HOST-UNREACH", "PROTO-UNREACH", "PORT-UNREACH",
223 "#12", "SRCFAIL-UNREACH", "NET-REDIRECT", "HOST-REDIRECT",
224 "TOSNET-REDIRECT", "TOSHOST-REDIRECT", "TX-INTRANS", "TX-REASS",
225 "PARAMPROB"
226 };
227 #endif
228
229 /*
230 * The arguments to ctloutput are:
231 * (*protosw[].pr_ctloutput)(req, so, level, optname, optval);
232 * req is one of the actions listed below, so is a (struct socket *),
233 * level is an indication of which protocol layer the option is intended.
234 * optname is a protocol dependent socket option request,
235 * optval is a pointer to a mbuf-chain pointer, for value-return results.
236 * The protocol is responsible for disposal of the mbuf chain *optval
237 * if supplied,
238 * the caller is responsible for any space held by *optval, when returned.
239 * A non-zero return from usrreq gives an
240 * UNIX error number which should be passed to higher level software.
241 */
242 #define PRCO_GETOPT 0
243 #define PRCO_SETOPT 1
244
245 #define PRCO_NCMDS 2
246
247 #ifdef PRCOREQUESTS
248 char *prcorequests[] = {
249 "GETOPT", "SETOPT",
250 };
251 #endif
252
253 #ifdef _KERNEL
254
255 #include <sys/mbuf.h>
256 #include <sys/socketvar.h>
257
258 const struct protosw *pffindproto(int, int, int);
259 const struct protosw *pffindtype(int, int);
260 const struct domain *pffinddomain(int);
261 void pfctlinput(int, struct sockaddr *);
262
263 extern u_char ip_protox[];
264 extern const struct protosw inetsw[];
265
266 #ifdef INET6
267 extern u_char ip6_protox[];
268 extern const struct protosw inet6sw[];
269 #endif /* INET6 */
270
271 static inline int
pru_attach(struct socket * so,int proto,int wait)272 pru_attach(struct socket *so, int proto, int wait)
273 {
274 return (*so->so_proto->pr_usrreqs->pru_attach)(so, proto, wait);
275 }
276
277 static inline int
pru_detach(struct socket * so)278 pru_detach(struct socket *so)
279 {
280 return (*so->so_proto->pr_usrreqs->pru_detach)(so);
281 }
282
283 static inline int
pru_bind(struct socket * so,struct mbuf * nam,struct proc * p)284 pru_bind(struct socket *so, struct mbuf *nam, struct proc *p)
285 {
286 if (so->so_proto->pr_usrreqs->pru_bind)
287 return (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
288 return (EOPNOTSUPP);
289 }
290
291 static inline int
pru_listen(struct socket * so)292 pru_listen(struct socket *so)
293 {
294 if (so->so_proto->pr_usrreqs->pru_listen)
295 return (*so->so_proto->pr_usrreqs->pru_listen)(so);
296 return (EOPNOTSUPP);
297 }
298
299 static inline int
pru_connect(struct socket * so,struct mbuf * nam)300 pru_connect(struct socket *so, struct mbuf *nam)
301 {
302 if (so->so_proto->pr_usrreqs->pru_connect)
303 return (*so->so_proto->pr_usrreqs->pru_connect)(so, nam);
304 return (EOPNOTSUPP);
305 }
306
307 static inline int
pru_accept(struct socket * so,struct mbuf * nam)308 pru_accept(struct socket *so, struct mbuf *nam)
309 {
310 if (so->so_proto->pr_usrreqs->pru_accept)
311 return (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
312 return (EOPNOTSUPP);
313 }
314
315 static inline int
pru_disconnect(struct socket * so)316 pru_disconnect(struct socket *so)
317 {
318 if (so->so_proto->pr_usrreqs->pru_disconnect)
319 return (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
320 return (EOPNOTSUPP);
321 }
322
323 static inline int
pru_shutdown(struct socket * so)324 pru_shutdown(struct socket *so)
325 {
326 return (*so->so_proto->pr_usrreqs->pru_shutdown)(so);
327 }
328
329 static inline void
pru_rcvd(struct socket * so)330 pru_rcvd(struct socket *so)
331 {
332 (*so->so_proto->pr_usrreqs->pru_rcvd)(so);
333 }
334
335 static inline int
pru_send(struct socket * so,struct mbuf * top,struct mbuf * addr,struct mbuf * control)336 pru_send(struct socket *so, struct mbuf *top, struct mbuf *addr,
337 struct mbuf *control)
338 {
339 return (*so->so_proto->pr_usrreqs->pru_send)(so, top, addr, control);
340 }
341
342 static inline void
pru_abort(struct socket * so)343 pru_abort(struct socket *so)
344 {
345 (*so->so_proto->pr_usrreqs->pru_abort)(so);
346 }
347
348 static inline int
pru_control(struct socket * so,u_long cmd,caddr_t data,struct ifnet * ifp)349 pru_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp)
350 {
351 if (so->so_proto->pr_usrreqs->pru_control)
352 return (*so->so_proto->pr_usrreqs->pru_control)(so,
353 cmd, data, ifp);
354 return (EOPNOTSUPP);
355 }
356
357 static inline int
pru_sense(struct socket * so,struct stat * ub)358 pru_sense(struct socket *so, struct stat *ub)
359 {
360 if (so->so_proto->pr_usrreqs->pru_sense)
361 return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
362 return (0);
363 }
364
365 static inline int
pru_rcvoob(struct socket * so,struct mbuf * m,int flags)366 pru_rcvoob(struct socket *so, struct mbuf *m, int flags)
367 {
368 if (so->so_proto->pr_usrreqs->pru_rcvoob)
369 return (*so->so_proto->pr_usrreqs->pru_rcvoob)(so, m, flags);
370 return (EOPNOTSUPP);
371 }
372
373 static inline int
pru_sendoob(struct socket * so,struct mbuf * top,struct mbuf * addr,struct mbuf * control)374 pru_sendoob(struct socket *so, struct mbuf *top, struct mbuf *addr,
375 struct mbuf *control)
376 {
377 if (so->so_proto->pr_usrreqs->pru_sendoob)
378 return (*so->so_proto->pr_usrreqs->pru_sendoob)(so,
379 top, addr, control);
380 m_freem(top);
381 m_freem(control);
382 return (EOPNOTSUPP);
383 }
384
385 static inline int
pru_sockaddr(struct socket * so,struct mbuf * addr)386 pru_sockaddr(struct socket *so, struct mbuf *addr)
387 {
388 return (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, addr);
389 }
390
391 static inline int
pru_peeraddr(struct socket * so,struct mbuf * addr)392 pru_peeraddr(struct socket *so, struct mbuf *addr)
393 {
394 return (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, addr);
395 }
396
397 static inline int
pru_connect2(struct socket * so1,struct socket * so2)398 pru_connect2(struct socket *so1, struct socket *so2)
399 {
400 if (so1->so_proto->pr_usrreqs->pru_connect2)
401 return (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
402 return (EOPNOTSUPP);
403 }
404
405 #endif
406
407 #endif /* _SYS_PROTOSW_H_ */
408