2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)raw_usrreq.c 8.1 (Berkeley) 6/10/93
33 #include <sys/param.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/protosw.h>
40 #include <sys/signalvar.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
44 #include <sys/systm.h>
46 #include <net/raw_cb.h>
48 MTX_SYSINIT(rawcb_mtx, &rawcb_mtx, "rawcb", MTX_DEF);
51 * Initialize raw connection block q.
57 LIST_INIT(&rawcb_list);
62 * Raw protocol input routine. Find the socket
63 * associated with the packet(s) and move them over. If
64 * nothing exists for this packet, drop it.
67 * Raw protocol interface.
70 raw_input(m0, proto, src, dst)
72 register struct sockproto *proto;
73 struct sockaddr *src, *dst;
75 register struct rawcb *rp;
76 register struct mbuf *m = m0;
81 LIST_FOREACH(rp, &rawcb_list, list) {
82 if (rp->rcb_proto.sp_family != proto->sp_family)
84 if (rp->rcb_proto.sp_protocol &&
85 rp->rcb_proto.sp_protocol != proto->sp_protocol)
88 * We assume the lower level routines have
89 * placed the address in a canonical format
90 * suitable for a structure comparison.
92 * Note that if the lengths are not the same
93 * the comparison will fail at the first byte.
95 #define equal(a1, a2) \
96 (bcmp((caddr_t)(a1), (caddr_t)(a2), a1->sa_len) == 0)
97 if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst))
99 if (rp->rcb_faddr && !equal(rp->rcb_faddr, src))
103 n = m_copy(m, 0, (int)M_COPYALL);
105 if (sbappendaddr(&last->so_rcv, src,
106 n, (struct mbuf *)0) == 0)
107 /* should notify about lost packet */
114 last = rp->rcb_socket;
117 if (sbappendaddr(&last->so_rcv, src,
118 m, (struct mbuf *)0) == 0)
125 mtx_unlock(&rawcb_mtx);
130 raw_ctlinput(cmd, arg, dummy)
132 struct sockaddr *arg;
136 if (cmd < 0 || cmd >= PRC_NCMDS)
142 raw_uabort(struct socket *so)
144 struct rawcb *rp = sotorawcb(so);
149 soisdisconnected(so);
156 /* pru_accept is EOPNOTSUPP */
159 raw_uattach(struct socket *so, int proto, struct thread *td)
161 struct rawcb *rp = sotorawcb(so);
166 if (td && (error = suser(td)) != 0)
168 return raw_attach(so, proto);
172 raw_ubind(struct socket *so, struct sockaddr *nam, struct thread *td)
178 raw_uconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
183 /* pru_connect2 is EOPNOTSUPP */
184 /* pru_control is EOPNOTSUPP */
187 raw_udetach(struct socket *so)
189 struct rawcb *rp = sotorawcb(so);
199 raw_udisconnect(struct socket *so)
201 struct rawcb *rp = sotorawcb(so);
205 if (rp->rcb_faddr == 0) {
209 soisdisconnected(so);
213 /* pru_listen is EOPNOTSUPP */
216 raw_upeeraddr(struct socket *so, struct sockaddr **nam)
218 struct rawcb *rp = sotorawcb(so);
222 if (rp->rcb_faddr == 0) {
225 *nam = sodupsockaddr(rp->rcb_faddr, M_WAITOK);
229 /* pru_rcvd is EOPNOTSUPP */
230 /* pru_rcvoob is EOPNOTSUPP */
233 raw_usend(struct socket *so, int flags, struct mbuf *m,
234 struct sockaddr *nam, struct mbuf *control, struct thread *td)
237 struct rawcb *rp = sotorawcb(so);
244 if (flags & PRUS_OOB) {
249 if (control && control->m_len) {
259 } else if (rp->rcb_faddr == 0) {
263 error = (*so->so_proto->pr_output)(m, so);
273 /* pru_sense is null */
276 raw_ushutdown(struct socket *so)
278 struct rawcb *rp = sotorawcb(so);
287 raw_usockaddr(struct socket *so, struct sockaddr **nam)
289 struct rawcb *rp = sotorawcb(so);
293 if (rp->rcb_laddr == 0)
295 *nam = sodupsockaddr(rp->rcb_laddr, M_WAITOK);
299 struct pr_usrreqs raw_usrreqs = {
300 .pru_abort = raw_uabort,
301 .pru_attach = raw_uattach,
302 .pru_bind = raw_ubind,
303 .pru_connect = raw_uconnect,
304 .pru_detach = raw_udetach,
305 .pru_disconnect = raw_udisconnect,
306 .pru_peeraddr = raw_upeeraddr,
307 .pru_send = raw_usend,
308 .pru_shutdown = raw_ushutdown,
309 .pru_sockaddr = raw_usockaddr,