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