Home | History | Annotate | Line # | Download | only in net
raw_usrreq.c revision 1.39
      1 /*	$NetBSD: raw_usrreq.c,v 1.39 2014/05/19 02:51:24 rmind 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.39 2014/05/19 02:51:24 rmind 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 void
     58 raw_init(void)
     59 {
     60 	LIST_INIT(&rawcb);
     61 }
     62 
     63 static inline int
     64 equal(const struct sockaddr *a1, const struct sockaddr *a2)
     65 {
     66 	return memcmp(a1, a2, a1->sa_len) == 0;
     67 }
     68 
     69 /*
     70  * raw_input: find the socket associated with the packet and move it over.
     71  * If nothing exists for this packet, drop it.
     72  */
     73 void
     74 raw_input(struct mbuf *m0, ...)
     75 {
     76 	struct rawcb *rp;
     77 	struct mbuf *m = m0;
     78 	struct socket *last;
     79 	va_list ap;
     80 	struct sockproto *proto;
     81 	struct sockaddr *src, *dst;
     82 
     83 	KASSERT(mutex_owned(softnet_lock));
     84 
     85 	va_start(ap, m0);
     86 	proto = va_arg(ap, struct sockproto *);
     87 	src = va_arg(ap, struct sockaddr *);
     88 	dst = va_arg(ap, struct sockaddr *);
     89 	va_end(ap);
     90 
     91 	last = NULL;
     92 	LIST_FOREACH(rp, &rawcb, rcb_list) {
     93 		if (rp->rcb_proto.sp_family != proto->sp_family)
     94 			continue;
     95 		if (rp->rcb_proto.sp_protocol  &&
     96 		    rp->rcb_proto.sp_protocol != proto->sp_protocol)
     97 			continue;
     98 		/*
     99 		 * We assume the lower level routines have
    100 		 * placed the address in a canonical format
    101 		 * suitable for a structure comparison.
    102 		 *
    103 		 * Note that if the lengths are not the same
    104 		 * the comparison will fail at the first byte.
    105 		 */
    106 		if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst))
    107 			continue;
    108 		if (rp->rcb_faddr && !equal(rp->rcb_faddr, src))
    109 			continue;
    110 		if (last != NULL) {
    111 			struct mbuf *n;
    112 			if ((n = m_copy(m, 0, M_COPYALL)) == NULL)
    113 				;
    114 			else if (sbappendaddr(&last->so_rcv, src, n, NULL) == 0)
    115 				/* should notify about lost packet */
    116 				m_freem(n);
    117 			else {
    118 				sorwakeup(last);
    119 			}
    120 		}
    121 		last = rp->rcb_socket;
    122 	}
    123 	if (last == NULL || sbappendaddr(&last->so_rcv, src, m, NULL) == 0)
    124 		m_freem(m);
    125 	else {
    126 		sorwakeup(last);
    127 	}
    128 }
    129 
    130 void *
    131 raw_ctlinput(int cmd, const struct sockaddr *arg, void *d)
    132 {
    133 
    134 	if ((unsigned)cmd >= PRC_NCMDS)
    135 		return NULL;
    136 	return NULL;
    137 	/* INCOMPLETE */
    138 }
    139 
    140 void
    141 raw_setsockaddr(struct rawcb *rp, struct mbuf *nam)
    142 {
    143 
    144 	nam->m_len = rp->rcb_laddr->sa_len;
    145 	memcpy(mtod(nam, void *), rp->rcb_laddr, (size_t)nam->m_len);
    146 }
    147 
    148 void
    149 raw_setpeeraddr(struct rawcb *rp, struct mbuf *nam)
    150 {
    151 
    152 	nam->m_len = rp->rcb_faddr->sa_len;
    153 	memcpy(mtod(nam, void *), rp->rcb_faddr, (size_t)nam->m_len);
    154 }
    155 
    156 int
    157 raw_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    158     struct mbuf *control, struct lwp *l)
    159 {
    160 	struct rawcb *rp = sotorawcb(so);
    161 	int s, error = 0;
    162 
    163 	KASSERT(req != PRU_ATTACH);
    164 	KASSERT(req != PRU_DETACH);
    165 
    166 	if (req == PRU_CONTROL)
    167 		return EOPNOTSUPP;
    168 
    169 	s = splsoftnet();
    170 	KERNEL_LOCK(1, NULL);
    171 
    172 	KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB));
    173 	if (rp == NULL) {
    174 		error = EINVAL;
    175 		goto release;
    176 	}
    177 
    178 	switch (req) {
    179 	/*
    180 	 * If a socket isn't bound to a single address,
    181 	 * the raw input routine will hand it anything
    182 	 * within that protocol family (assuming there's
    183 	 * nothing else around it should go to).
    184 	 */
    185 	case PRU_BIND:
    186 	case PRU_LISTEN:
    187 	case PRU_CONNECT:
    188 	case PRU_CONNECT2:
    189 		error = EOPNOTSUPP;
    190 		break;
    191 
    192 	case PRU_DISCONNECT:
    193 		soisdisconnected(so);
    194 		raw_disconnect(rp);
    195 		break;
    196 
    197 	/*
    198 	 * Mark the connection as being incapable of further input.
    199 	 */
    200 	case PRU_SHUTDOWN:
    201 		socantsendmore(so);
    202 		break;
    203 
    204 	case PRU_RCVD:
    205 		error = EOPNOTSUPP;
    206 		break;
    207 
    208 	/*
    209 	 * Ship a packet out.  The appropriate raw output
    210 	 * routine handles any massaging necessary.
    211 	 */
    212 	case PRU_SEND:
    213 		if (control && control->m_len) {
    214 			m_freem(control);
    215 			m_freem(m);
    216 			error = EINVAL;
    217 			break;
    218 		}
    219 		if (nam) {
    220 			if ((so->so_state & SS_ISCONNECTED) != 0) {
    221 				error = EISCONN;
    222 				goto die;
    223 			}
    224 			error = (*so->so_proto->pr_usrreqs->pr_generic)(so,
    225 			    PRU_CONNECT, NULL, nam, NULL, l);
    226 			if (error) {
    227 			die:
    228 				m_freem(m);
    229 				break;
    230 			}
    231 		} else {
    232 			if ((so->so_state & SS_ISCONNECTED) == 0) {
    233 				error = ENOTCONN;
    234 				goto die;
    235 			}
    236 		}
    237 		error = (*so->so_proto->pr_output)(m, so);
    238 		if (nam)
    239 			raw_disconnect(rp);
    240 		break;
    241 
    242 	case PRU_SENSE:
    243 		/*
    244 		 * stat: don't bother with a blocksize.
    245 		 */
    246 		error = 0;
    247 		break;
    248 
    249 	/*
    250 	 * Not supported.
    251 	 */
    252 	case PRU_RCVOOB:
    253 		error = EOPNOTSUPP;
    254 		break;
    255 
    256 	case PRU_SENDOOB:
    257 		m_freem(control);
    258 		m_freem(m);
    259 		error = EOPNOTSUPP;
    260 		break;
    261 
    262 	case PRU_SOCKADDR:
    263 		if (rp->rcb_laddr == NULL) {
    264 			error = EINVAL;
    265 			break;
    266 		}
    267 		raw_setsockaddr(rp, nam);
    268 		break;
    269 
    270 	case PRU_PEERADDR:
    271 		if (rp->rcb_faddr == NULL) {
    272 			error = ENOTCONN;
    273 			break;
    274 		}
    275 		raw_setpeeraddr(rp, nam);
    276 		break;
    277 
    278 	default:
    279 		panic("raw_usrreq");
    280 	}
    281 
    282 release:
    283 	KERNEL_UNLOCK_ONE(NULL);
    284 	splx(s);
    285 	return (error);
    286 }
    287