1 /* $OpenBSD: protosw.h,v 1.71 2025/01/05 12:36:48 bluhm 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_MPSYSCTL 0x0200 /* mp-safe sysctl(2) handler */
134
135 /*
136 * The arguments to usrreq are:
137 * (*protosw[].pr_usrreq)(up, req, m, nam, opt);
138 * where up is a (struct socket *), req is one of these requests,
139 * m is a optional mbuf chain containing a message,
140 * nam is an optional mbuf chain containing an address,
141 * and opt is a pointer to a socketopt structure or nil.
142 * The protocol is responsible for disposal of the mbuf chain m,
143 * the caller is responsible for any space held by nam and opt.
144 * A non-zero return from usrreq gives an
145 * UNIX error number which should be passed to higher level software.
146 */
147 #define PRU_ATTACH 0 /* attach protocol to up */
148 #define PRU_DETACH 1 /* detach protocol from up */
149 #define PRU_BIND 2 /* bind socket to address */
150 #define PRU_LISTEN 3 /* listen for connection */
151 #define PRU_CONNECT 4 /* establish connection to peer */
152 #define PRU_ACCEPT 5 /* accept connection from peer */
153 #define PRU_DISCONNECT 6 /* disconnect from peer */
154 #define PRU_SHUTDOWN 7 /* won't send any more data */
155 #define PRU_RCVD 8 /* have taken data; more room now */
156 #define PRU_SEND 9 /* send this data */
157 #define PRU_ABORT 10 /* abort (fast DISCONNECT, DETACH) */
158 #define PRU_CONTROL 11 /* control operations on protocol */
159 #define PRU_SENSE 12 /* return status into m */
160 #define PRU_RCVOOB 13 /* retrieve out of band data */
161 #define PRU_SENDOOB 14 /* send out of band data */
162 #define PRU_SOCKADDR 15 /* fetch socket's address */
163 #define PRU_PEERADDR 16 /* fetch peer's address */
164 #define PRU_CONNECT2 17 /* connect two sockets */
165 /* begin for protocols internal use */
166 #define PRU_FASTTIMO 18 /* 200ms timeout */
167 #define PRU_SLOWTIMO 19 /* 500ms timeout */
168 #define PRU_PROTORCV 20 /* receive from below */
169 #define PRU_PROTOSEND 21 /* send to below */
170
171 #define PRU_NREQ 22
172
173 #ifdef PRUREQUESTS
174 const char *prurequests[] = {
175 "ATTACH", "DETACH", "BIND", "LISTEN",
176 "CONNECT", "ACCEPT", "DISCONNECT", "SHUTDOWN",
177 "RCVD", "SEND", "ABORT", "CONTROL",
178 "SENSE", "RCVOOB", "SENDOOB", "SOCKADDR",
179 "PEERADDR", "CONNECT2", "FASTTIMO", "SLOWTIMO",
180 "PROTORCV", "PROTOSEND",
181 };
182 #endif
183
184 /*
185 * The arguments to the ctlinput routine are
186 * (*protosw[].pr_ctlinput)(cmd, sa, arg);
187 * where cmd is one of the commands below, sa is a pointer to a sockaddr,
188 * and arg is an optional caddr_t argument used within a protocol family.
189 */
190 #define PRC_IFDOWN 0 /* interface transition */
191 #define PRC_ROUTEDEAD 1 /* select new route if possible ??? */
192 #define PRC_MTUINC 2 /* increase in mtu to host */
193 #define PRC_QUENCH2 3 /* DEC congestion bit says slow down */
194 #define PRC_QUENCH 4 /* some one said to slow down */
195 #define PRC_MSGSIZE 5 /* message size forced drop */
196 #define PRC_HOSTDEAD 6 /* host appears to be down */
197 #define PRC_HOSTUNREACH 7 /* deprecated (use PRC_UNREACH_HOST) */
198 #define PRC_UNREACH_NET 8 /* no route to network */
199 #define PRC_UNREACH_HOST 9 /* no route to host */
200 #define PRC_UNREACH_PROTOCOL 10 /* dst says bad protocol */
201 #define PRC_UNREACH_PORT 11 /* bad port # */
202 /* was PRC_UNREACH_NEEDFRAG 12 (use PRC_MSGSIZE) */
203 #define PRC_UNREACH_SRCFAIL 13 /* source route failed */
204 #define PRC_REDIRECT_NET 14 /* net routing redirect */
205 #define PRC_REDIRECT_HOST 15 /* host routing redirect */
206 #define PRC_REDIRECT_TOSNET 16 /* redirect for type of service & net */
207 #define PRC_REDIRECT_TOSHOST 17 /* redirect for tos & host */
208 #define PRC_TIMXCEED_INTRANS 18 /* packet lifetime expired in transit */
209 #define PRC_TIMXCEED_REASS 19 /* lifetime expired on reass q */
210 #define PRC_PARAMPROB 20 /* header incorrect */
211
212 #define PRC_NCMDS 21
213
214 #define PRC_IS_REDIRECT(cmd) \
215 ((cmd) >= PRC_REDIRECT_NET && (cmd) <= PRC_REDIRECT_TOSHOST)
216
217 #ifdef PRCREQUESTS
218 char *prcrequests[] = {
219 "IFDOWN", "ROUTEDEAD", "MTUINC", "DEC-BIT-QUENCH2",
220 "QUENCH", "MSGSIZE", "HOSTDEAD", "#7",
221 "NET-UNREACH", "HOST-UNREACH", "PROTO-UNREACH", "PORT-UNREACH",
222 "#12", "SRCFAIL-UNREACH", "NET-REDIRECT", "HOST-REDIRECT",
223 "TOSNET-REDIRECT", "TOSHOST-REDIRECT", "TX-INTRANS", "TX-REASS",
224 "PARAMPROB"
225 };
226 #endif
227
228 /*
229 * The arguments to ctloutput are:
230 * (*protosw[].pr_ctloutput)(req, so, level, optname, optval);
231 * req is one of the actions listed below, so is a (struct socket *),
232 * level is an indication of which protocol layer the option is intended.
233 * optname is a protocol dependent socket option request,
234 * optval is a pointer to a mbuf-chain pointer, for value-return results.
235 * The protocol is responsible for disposal of the mbuf chain *optval
236 * if supplied,
237 * the caller is responsible for any space held by *optval, when returned.
238 * A non-zero return from usrreq gives an
239 * UNIX error number which should be passed to higher level software.
240 */
241 #define PRCO_GETOPT 0
242 #define PRCO_SETOPT 1
243
244 #define PRCO_NCMDS 2
245
246 #ifdef PRCOREQUESTS
247 char *prcorequests[] = {
248 "GETOPT", "SETOPT",
249 };
250 #endif
251
252 #ifdef _KERNEL
253
254 #include <sys/mbuf.h>
255 #include <sys/socketvar.h>
256
257 const struct protosw *pffindproto(int, int, int);
258 const struct protosw *pffindtype(int, int);
259 const struct domain *pffinddomain(int);
260 void pfctlinput(int, struct sockaddr *);
261
262 extern u_char ip_protox[];
263 extern const struct protosw inetsw[];
264
265 #ifdef INET6
266 extern u_char ip6_protox[];
267 extern const struct protosw inet6sw[];
268 #endif /* INET6 */
269
270 static inline int
pru_attach(struct socket * so,int proto,int wait)271 pru_attach(struct socket *so, int proto, int wait)
272 {
273 return (*so->so_proto->pr_usrreqs->pru_attach)(so, proto, wait);
274 }
275
276 static inline int
pru_detach(struct socket * so)277 pru_detach(struct socket *so)
278 {
279 return (*so->so_proto->pr_usrreqs->pru_detach)(so);
280 }
281
282 static inline int
pru_bind(struct socket * so,struct mbuf * nam,struct proc * p)283 pru_bind(struct socket *so, struct mbuf *nam, struct proc *p)
284 {
285 if (so->so_proto->pr_usrreqs->pru_bind)
286 return (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
287 return (EOPNOTSUPP);
288 }
289
290 static inline int
pru_listen(struct socket * so)291 pru_listen(struct socket *so)
292 {
293 if (so->so_proto->pr_usrreqs->pru_listen)
294 return (*so->so_proto->pr_usrreqs->pru_listen)(so);
295 return (EOPNOTSUPP);
296 }
297
298 static inline int
pru_connect(struct socket * so,struct mbuf * nam)299 pru_connect(struct socket *so, struct mbuf *nam)
300 {
301 if (so->so_proto->pr_usrreqs->pru_connect)
302 return (*so->so_proto->pr_usrreqs->pru_connect)(so, nam);
303 return (EOPNOTSUPP);
304 }
305
306 static inline int
pru_accept(struct socket * so,struct mbuf * nam)307 pru_accept(struct socket *so, struct mbuf *nam)
308 {
309 if (so->so_proto->pr_usrreqs->pru_accept)
310 return (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
311 return (EOPNOTSUPP);
312 }
313
314 static inline int
pru_disconnect(struct socket * so)315 pru_disconnect(struct socket *so)
316 {
317 if (so->so_proto->pr_usrreqs->pru_disconnect)
318 return (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
319 return (EOPNOTSUPP);
320 }
321
322 static inline int
pru_shutdown(struct socket * so)323 pru_shutdown(struct socket *so)
324 {
325 return (*so->so_proto->pr_usrreqs->pru_shutdown)(so);
326 }
327
328 static inline void
pru_rcvd(struct socket * so)329 pru_rcvd(struct socket *so)
330 {
331 (*so->so_proto->pr_usrreqs->pru_rcvd)(so);
332 }
333
334 static inline int
pru_send(struct socket * so,struct mbuf * top,struct mbuf * addr,struct mbuf * control)335 pru_send(struct socket *so, struct mbuf *top, struct mbuf *addr,
336 struct mbuf *control)
337 {
338 return (*so->so_proto->pr_usrreqs->pru_send)(so, top, addr, control);
339 }
340
341 static inline void
pru_abort(struct socket * so)342 pru_abort(struct socket *so)
343 {
344 (*so->so_proto->pr_usrreqs->pru_abort)(so);
345 }
346
347 static inline int
pru_control(struct socket * so,u_long cmd,caddr_t data,struct ifnet * ifp)348 pru_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp)
349 {
350 if (so->so_proto->pr_usrreqs->pru_control)
351 return (*so->so_proto->pr_usrreqs->pru_control)(so,
352 cmd, data, ifp);
353 return (EOPNOTSUPP);
354 }
355
356 static inline int
pru_sense(struct socket * so,struct stat * ub)357 pru_sense(struct socket *so, struct stat *ub)
358 {
359 if (so->so_proto->pr_usrreqs->pru_sense)
360 return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
361 return (0);
362 }
363
364 static inline int
pru_rcvoob(struct socket * so,struct mbuf * m,int flags)365 pru_rcvoob(struct socket *so, struct mbuf *m, int flags)
366 {
367 if (so->so_proto->pr_usrreqs->pru_rcvoob)
368 return (*so->so_proto->pr_usrreqs->pru_rcvoob)(so, m, flags);
369 return (EOPNOTSUPP);
370 }
371
372 static inline int
pru_sendoob(struct socket * so,struct mbuf * top,struct mbuf * addr,struct mbuf * control)373 pru_sendoob(struct socket *so, struct mbuf *top, struct mbuf *addr,
374 struct mbuf *control)
375 {
376 if (so->so_proto->pr_usrreqs->pru_sendoob)
377 return (*so->so_proto->pr_usrreqs->pru_sendoob)(so,
378 top, addr, control);
379 m_freem(top);
380 m_freem(control);
381 return (EOPNOTSUPP);
382 }
383
384 static inline int
pru_sockaddr(struct socket * so,struct mbuf * addr)385 pru_sockaddr(struct socket *so, struct mbuf *addr)
386 {
387 return (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, addr);
388 }
389
390 static inline int
pru_peeraddr(struct socket * so,struct mbuf * addr)391 pru_peeraddr(struct socket *so, struct mbuf *addr)
392 {
393 return (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, addr);
394 }
395
396 static inline int
pru_connect2(struct socket * so1,struct socket * so2)397 pru_connect2(struct socket *so1, struct socket *so2)
398 {
399 if (so1->so_proto->pr_usrreqs->pru_connect2)
400 return (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
401 return (EOPNOTSUPP);
402 }
403
404 #endif
405
406 #endif /* _SYS_PROTOSW_H_ */
407