Home | History | Annotate | Line # | Download | only in net
raw_usrreq.c revision 1.58
      1 /*	$NetBSD: raw_usrreq.c,v 1.58 2017/09/25 01:57:54 ozaki-r 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.58 2017/09/25 01:57:54 ozaki-r 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/netisr.h>
     55 #include <net/raw_cb.h>
     56 
     57 static inline int
     58 equal(const struct sockaddr *a1, const struct sockaddr *a2)
     59 {
     60 	return memcmp(a1, a2, a1->sa_len) == 0;
     61 }
     62 
     63 /*
     64  * raw_input: find the socket associated with the packet and move it over.
     65  * If nothing exists for this packet, drop it.
     66  */
     67 void
     68 raw_input(struct mbuf *m0, ...)
     69 {
     70 	struct rawcb *rp;
     71 	struct mbuf *m = m0;
     72 	struct socket *last;
     73 	va_list ap;
     74 	struct sockproto *proto;
     75 	struct sockaddr *src, *dst;
     76 	struct rawcbhead *rawcbhead;
     77 
     78 	va_start(ap, m0);
     79 	proto = va_arg(ap, struct sockproto *);
     80 	src = va_arg(ap, struct sockaddr *);
     81 	dst = va_arg(ap, struct sockaddr *);
     82 	rawcbhead = va_arg(ap, struct rawcbhead *);
     83 	va_end(ap);
     84 
     85 	last = NULL;
     86 	LIST_FOREACH(rp, rawcbhead, rcb_list) {
     87 		if (rp->rcb_proto.sp_family != proto->sp_family)
     88 			continue;
     89 		if (rp->rcb_proto.sp_protocol  &&
     90 		    rp->rcb_proto.sp_protocol != proto->sp_protocol)
     91 			continue;
     92 		/*
     93 		 * We assume the lower level routines have
     94 		 * placed the address in a canonical format
     95 		 * suitable for a structure comparison.
     96 		 *
     97 		 * Note that if the lengths are not the same
     98 		 * the comparison will fail at the first byte.
     99 		 */
    100 		if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst))
    101 			continue;
    102 		if (rp->rcb_faddr && !equal(rp->rcb_faddr, src))
    103 			continue;
    104 		/* Run any filtering that may have been installed. */
    105 		if (rp->rcb_filter != NULL && rp->rcb_filter(m, proto, rp) != 0)
    106 			continue;
    107 		if (last != NULL) {
    108 			struct mbuf *n;
    109 			if ((n = m_copy(m, 0, M_COPYALL)) == NULL)
    110 				;
    111 			else if (sbappendaddr(&last->so_rcv, src, n, NULL) == 0)
    112 				/* should notify about lost packet */
    113 				m_freem(n);
    114 			else {
    115 				sorwakeup(last);
    116 			}
    117 		}
    118 		last = rp->rcb_socket;
    119 	}
    120 	if (last == NULL || sbappendaddr(&last->so_rcv, src, m, NULL) == 0)
    121 		m_freem(m);
    122 	else {
    123 		sorwakeup(last);
    124 	}
    125 }
    126 
    127 void *
    128 raw_ctlinput(int cmd, const struct sockaddr *arg, void *d)
    129 {
    130 
    131 	if ((unsigned)cmd >= PRC_NCMDS)
    132 		return NULL;
    133 	return NULL;
    134 	/* INCOMPLETE */
    135 }
    136 
    137 void
    138 raw_setsockaddr(struct rawcb *rp, struct sockaddr *nam)
    139 {
    140 
    141 	memcpy(nam, rp->rcb_laddr, rp->rcb_laddr->sa_len);
    142 }
    143 
    144 void
    145 raw_setpeeraddr(struct rawcb *rp, struct sockaddr *nam)
    146 {
    147 
    148 	memcpy(nam, rp->rcb_faddr, rp->rcb_faddr->sa_len);
    149 }
    150 
    151 int
    152 raw_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
    153     struct mbuf *control, struct lwp *l,
    154     int (*output)(struct mbuf *, struct socket *))
    155 {
    156 	struct rawcb *rp = sotorawcb(so);
    157 	int error = 0;
    158 
    159 	KASSERT(rp != NULL);
    160 
    161 	/*
    162 	 * Ship a packet out.  The appropriate raw output
    163 	 * routine handles any massaging necessary.
    164 	 */
    165 	if (control && control->m_len) {
    166 		m_freem(control);
    167 		m_freem(m);
    168 		return EINVAL;
    169 	}
    170 	if (nam) {
    171 		if ((so->so_state & SS_ISCONNECTED) != 0) {
    172 			error = EISCONN;
    173 			goto die;
    174 		}
    175 		error = (*so->so_proto->pr_usrreqs->pr_connect)(so, nam, l);
    176 		if (error) {
    177 		die:
    178 			m_freem(m);
    179 			return error;
    180 		}
    181 	} else {
    182 		if ((so->so_state & SS_ISCONNECTED) == 0) {
    183 			error = ENOTCONN;
    184 			goto die;
    185 		}
    186 	}
    187 	error = (*output)(m, so);
    188 	if (nam)
    189 		raw_disconnect(rp);
    190 
    191 	return error;
    192 }
    193 
    194 int
    195 raw_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    196     struct mbuf *control, struct lwp *l)
    197 {
    198 
    199 	KASSERT(req != PRU_ATTACH);
    200 	KASSERT(req != PRU_DETACH);
    201 	KASSERT(req != PRU_ACCEPT);
    202 	KASSERT(req != PRU_BIND);
    203 	KASSERT(req != PRU_LISTEN);
    204 	KASSERT(req != PRU_CONNECT);
    205 	KASSERT(req != PRU_CONNECT2);
    206 	KASSERT(req != PRU_DISCONNECT);
    207 	KASSERT(req != PRU_SHUTDOWN);
    208 	KASSERT(req != PRU_ABORT);
    209 	KASSERT(req != PRU_CONTROL);
    210 	KASSERT(req != PRU_SENSE);
    211 	KASSERT(req != PRU_PEERADDR);
    212 	KASSERT(req != PRU_SOCKADDR);
    213 	KASSERT(req != PRU_RCVD);
    214 	KASSERT(req != PRU_RCVOOB);
    215 	KASSERT(req != PRU_SEND);
    216 	KASSERT(req != PRU_SENDOOB);
    217 	KASSERT(req != PRU_PURGEIF);
    218 
    219 	if (sotorawcb(so) == NULL)
    220 		return EINVAL;
    221 
    222 	panic("raw_usrreq");
    223 
    224 	return 0;
    225 }
    226