1 /*        $NetBSD: raw_usrreq.c,v 1.66 2024/07/05 04:31:53 rin Exp $  */
2 
3 /*
4  * Copyright (c) 1980, 1986, 1993
5  *        The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *        @(#)raw_usrreq.c    8.1 (Berkeley) 6/10/93
32  */
33 
34 /*
35  * Raw protocol interface.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: raw_usrreq.c,v 1.66 2024/07/05 04:31:53 rin Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/mbuf.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/errno.h>
48 #include <sys/systm.h>
49 #include <sys/proc.h>
50 #include <sys/kauth.h>
51 
52 #include <net/if.h>
53 #include <net/route.h>
54 #include <net/raw_cb.h>
55 
56 static inline int
equal(const struct sockaddr * a1,const struct sockaddr * a2)57 equal(const struct sockaddr *a1, const struct sockaddr *a2)
58 {
59           return memcmp(a1, a2, a1->sa_len) == 0;
60 }
61 
62 /*
63  * raw_input: find the socket associated with the packet and move it over.
64  * If nothing exists for this packet, drop it.
65  */
66 void
raw_input(struct mbuf * m0,struct sockproto * proto,struct sockaddr * src,struct sockaddr * dst,struct rawcbhead * rawcbhead)67 raw_input(struct mbuf *m0, struct sockproto *proto, struct sockaddr *src,
68     struct sockaddr *dst, struct rawcbhead *rawcbhead)
69 {
70           struct rawcb *rp;
71           struct mbuf *m = m0;
72           struct socket *last;
73 
74           last = NULL;
75           LIST_FOREACH(rp, rawcbhead, rcb_list) {
76                     if (rp->rcb_proto.sp_family != proto->sp_family)
77                               continue;
78                     if (rp->rcb_proto.sp_protocol  &&
79                         rp->rcb_proto.sp_protocol != proto->sp_protocol)
80                               continue;
81                     /*
82                      * We assume the lower level routines have
83                      * placed the address in a canonical format
84                      * suitable for a structure comparison.
85                      *
86                      * Note that if the lengths are not the same
87                      * the comparison will fail at the first byte.
88                      */
89                     if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst))
90                               continue;
91                     if (rp->rcb_faddr && !equal(rp->rcb_faddr, src))
92                               continue;
93                     /* Run any filtering that may have been installed. */
94                     if (rp->rcb_filter != NULL && rp->rcb_filter(m, proto, rp) != 0)
95                               continue;
96                     if (last != NULL) {
97                               struct mbuf *n;
98 
99                               if ((n = m_copypacket(m, M_DONTWAIT)) == NULL ||
100                                   sbappendaddr(&last->so_rcv, src, n, NULL) == 0)
101                               {
102                                         m_freem(n);
103                                         soroverflow(last);
104                               } else
105                                         sorwakeup(last);
106                     }
107                     last = rp->rcb_socket;
108           }
109           if (last != NULL) {
110                     if (sbappendaddr(&last->so_rcv, src, m, NULL) == 0) {
111                               m_freem(m);
112                               soroverflow(last);
113                     } else
114                               sorwakeup(last);
115           } else {
116                     m_freem(m);
117           }
118 }
119 
120 void *
raw_ctlinput(int cmd,const struct sockaddr * arg,void * d)121 raw_ctlinput(int cmd, const struct sockaddr *arg, void *d)
122 {
123 
124           if ((unsigned)cmd >= PRC_NCMDS)
125                     return NULL;
126           return NULL;
127           /* INCOMPLETE */
128 }
129 
130 void
raw_setsockaddr(struct rawcb * rp,struct sockaddr * nam)131 raw_setsockaddr(struct rawcb *rp, struct sockaddr *nam)
132 {
133 
134           memcpy(nam, rp->rcb_laddr, rp->rcb_laddr->sa_len);
135 }
136 
137 void
raw_setpeeraddr(struct rawcb * rp,struct sockaddr * nam)138 raw_setpeeraddr(struct rawcb *rp, struct sockaddr *nam)
139 {
140 
141           memcpy(nam, rp->rcb_faddr, rp->rcb_faddr->sa_len);
142 }
143 
144 int
raw_send(struct socket * so,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct lwp * l,int (* output)(struct mbuf *,struct socket *))145 raw_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
146     struct mbuf *control, struct lwp *l,
147     int (*output)(struct mbuf *, struct socket *))
148 {
149           struct rawcb *rp = sotorawcb(so);
150           int error = 0;
151 
152           KASSERT(rp != NULL);
153 
154           /*
155            * Ship a packet out.  The appropriate raw output
156            * routine handles any massaging necessary.
157            */
158           if (control && control->m_len) {
159                     m_freem(control);
160                     m_freem(m);
161                     return EINVAL;
162           }
163           if (nam) {
164                     if ((so->so_state & SS_ISCONNECTED) != 0) {
165                               error = EISCONN;
166                               goto die;
167                     }
168                     error = (*so->so_proto->pr_usrreqs->pr_connect)(so, nam, l);
169                     if (error) {
170                     die:
171                               m_freem(m);
172                               return error;
173                     }
174           } else {
175                     if ((so->so_state & SS_ISCONNECTED) == 0) {
176                               error = ENOTCONN;
177                               goto die;
178                     }
179           }
180           error = (*output)(m, so);
181           if (nam)
182                     raw_disconnect(rp);
183 
184           return error;
185 }
186 
187 int
raw_usrreq(struct socket * so,int req,struct mbuf * m,struct mbuf * nam,struct mbuf * control,struct lwp * l)188 raw_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
189     struct mbuf *control, struct lwp *l)
190 {
191 
192           KASSERT(req != PRU_ATTACH);
193           KASSERT(req != PRU_DETACH);
194           KASSERT(req != PRU_ACCEPT);
195           KASSERT(req != PRU_BIND);
196           KASSERT(req != PRU_LISTEN);
197           KASSERT(req != PRU_CONNECT);
198           KASSERT(req != PRU_CONNECT2);
199           KASSERT(req != PRU_DISCONNECT);
200           KASSERT(req != PRU_SHUTDOWN);
201           KASSERT(req != PRU_ABORT);
202           KASSERT(req != PRU_CONTROL);
203           KASSERT(req != PRU_SENSE);
204           KASSERT(req != PRU_PEERADDR);
205           KASSERT(req != PRU_SOCKADDR);
206           KASSERT(req != PRU_RCVD);
207           KASSERT(req != PRU_RCVOOB);
208           KASSERT(req != PRU_SEND);
209           KASSERT(req != PRU_SENDOOB);
210           KASSERT(req != PRU_PURGEIF);
211 
212           if (sotorawcb(so) == NULL)
213                     return EINVAL;
214 
215           panic("raw_usrreq");
216 
217           return 0;
218 }
219