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