Home | History | Annotate | Line # | Download | only in net
raw_usrreq.c revision 1.30.4.3
      1  1.30.4.3      yamt /*	$NetBSD: raw_usrreq.c,v 1.30.4.3 2007/05/07 10:55:54 yamt 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.17     lukem #include <sys/cdefs.h>
     35  1.30.4.3      yamt __KERNEL_RCSID(0, "$NetBSD: raw_usrreq.c,v 1.30.4.3 2007/05/07 10:55:54 yamt Exp $");
     36       1.1       cgd 
     37       1.4   mycroft #include <sys/param.h>
     38       1.4   mycroft #include <sys/mbuf.h>
     39       1.4   mycroft #include <sys/domain.h>
     40       1.4   mycroft #include <sys/protosw.h>
     41       1.7   mycroft #include <sys/socket.h>
     42       1.4   mycroft #include <sys/socketvar.h>
     43       1.4   mycroft #include <sys/errno.h>
     44      1.11  christos #include <sys/systm.h>
     45      1.12   mycroft #include <sys/proc.h>
     46      1.26      elad #include <sys/kauth.h>
     47       1.1       cgd 
     48       1.4   mycroft #include <net/if.h>
     49       1.4   mycroft #include <net/route.h>
     50       1.4   mycroft #include <net/netisr.h>
     51       1.4   mycroft #include <net/raw_cb.h>
     52       1.1       cgd 
     53      1.11  christos #include <machine/stdarg.h>
     54       1.1       cgd /*
     55       1.1       cgd  * Initialize raw connection block q.
     56       1.1       cgd  */
     57       1.7   mycroft void
     58      1.25   thorpej raw_init(void)
     59       1.1       cgd {
     60       1.1       cgd 
     61      1.10   mycroft 	LIST_INIT(&rawcb);
     62       1.1       cgd }
     63       1.1       cgd 
     64  1.30.4.3      yamt static inline int
     65  1.30.4.3      yamt equal(const struct sockaddr *a1, const struct sockaddr *a2)
     66  1.30.4.3      yamt {
     67  1.30.4.3      yamt 	return memcmp(a1, a2, a1->sa_len) == 0;
     68  1.30.4.3      yamt }
     69       1.1       cgd 
     70       1.1       cgd /*
     71       1.1       cgd  * Raw protocol input routine.  Find the socket
     72       1.1       cgd  * associated with the packet(s) and move them over.  If
     73       1.1       cgd  * nothing exists for this packet, drop it.
     74       1.1       cgd  */
     75       1.1       cgd /*
     76       1.1       cgd  * Raw protocol interface.
     77       1.1       cgd  */
     78       1.7   mycroft void
     79      1.11  christos raw_input(struct mbuf *m0, ...)
     80       1.1       cgd {
     81      1.15  augustss 	struct rawcb *rp;
     82      1.15  augustss 	struct mbuf *m = m0;
     83      1.15  augustss 	int sockets = 0;
     84       1.1       cgd 	struct socket *last;
     85      1.11  christos 	va_list ap;
     86      1.15  augustss 	struct sockproto *proto;
     87      1.11  christos 	struct sockaddr *src, *dst;
     88      1.23     perry 
     89      1.11  christos 	va_start(ap, m0);
     90      1.11  christos 	proto = va_arg(ap, struct sockproto *);
     91      1.11  christos 	src = va_arg(ap, struct sockaddr *);
     92      1.11  christos 	dst = va_arg(ap, struct sockaddr *);
     93      1.11  christos 	va_end(ap);
     94       1.1       cgd 
     95  1.30.4.3      yamt 	last = NULL;
     96      1.16      matt 	LIST_FOREACH(rp, &rawcb, rcb_list) {
     97       1.1       cgd 		if (rp->rcb_proto.sp_family != proto->sp_family)
     98       1.1       cgd 			continue;
     99       1.1       cgd 		if (rp->rcb_proto.sp_protocol  &&
    100       1.1       cgd 		    rp->rcb_proto.sp_protocol != proto->sp_protocol)
    101       1.1       cgd 			continue;
    102       1.1       cgd 		/*
    103       1.1       cgd 		 * We assume the lower level routines have
    104       1.1       cgd 		 * placed the address in a canonical format
    105       1.1       cgd 		 * suitable for a structure comparison.
    106       1.1       cgd 		 *
    107       1.1       cgd 		 * Note that if the lengths are not the same
    108       1.1       cgd 		 * the comparison will fail at the first byte.
    109       1.1       cgd 		 */
    110       1.1       cgd 		if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst))
    111       1.1       cgd 			continue;
    112       1.1       cgd 		if (rp->rcb_faddr && !equal(rp->rcb_faddr, src))
    113       1.1       cgd 			continue;
    114  1.30.4.3      yamt 		if (last != NULL) {
    115       1.1       cgd 			struct mbuf *n;
    116  1.30.4.3      yamt 			if ((n = m_copy(m, 0, M_COPYALL)) == NULL)
    117  1.30.4.3      yamt 				;
    118  1.30.4.3      yamt 			else if (sbappendaddr(&last->so_rcv, src, n, NULL) == 0)
    119  1.30.4.3      yamt 				/* should notify about lost packet */
    120  1.30.4.3      yamt 				m_freem(n);
    121  1.30.4.3      yamt 			else {
    122  1.30.4.3      yamt 				sorwakeup(last);
    123  1.30.4.3      yamt 				sockets++;
    124       1.1       cgd 			}
    125       1.1       cgd 		}
    126       1.1       cgd 		last = rp->rcb_socket;
    127       1.1       cgd 	}
    128  1.30.4.3      yamt 	if (last == NULL || sbappendaddr(&last->so_rcv, src, m, NULL) == 0)
    129       1.1       cgd 		m_freem(m);
    130  1.30.4.3      yamt 	else {
    131  1.30.4.3      yamt 		sorwakeup(last);
    132  1.30.4.3      yamt 		sockets++;
    133  1.30.4.3      yamt 	}
    134       1.1       cgd }
    135       1.1       cgd 
    136       1.1       cgd /*ARGSUSED*/
    137      1.11  christos void *
    138  1.30.4.1      yamt raw_ctlinput(int cmd, const struct sockaddr *arg, void *d)
    139       1.1       cgd {
    140       1.1       cgd 
    141      1.21  christos 	if ((unsigned)cmd >= PRC_NCMDS)
    142      1.11  christos 		return NULL;
    143      1.11  christos 	return NULL;
    144       1.1       cgd 	/* INCOMPLETE */
    145       1.1       cgd }
    146       1.1       cgd 
    147      1.13   mycroft void
    148      1.25   thorpej raw_setsockaddr(struct rawcb *rp, struct mbuf *nam)
    149      1.13   mycroft {
    150      1.13   mycroft 
    151      1.13   mycroft 	nam->m_len = rp->rcb_laddr->sa_len;
    152  1.30.4.3      yamt 	memcpy(mtod(nam, void *), rp->rcb_laddr, (size_t)nam->m_len);
    153      1.13   mycroft }
    154      1.13   mycroft 
    155      1.13   mycroft void
    156      1.25   thorpej raw_setpeeraddr(struct rawcb *rp, struct mbuf *nam)
    157      1.13   mycroft {
    158      1.13   mycroft 
    159      1.13   mycroft 	nam->m_len = rp->rcb_faddr->sa_len;
    160  1.30.4.3      yamt 	memcpy(mtod(nam, void *), rp->rcb_faddr, (size_t)nam->m_len);
    161      1.13   mycroft }
    162      1.13   mycroft 
    163       1.1       cgd /*ARGSUSED*/
    164       1.7   mycroft int
    165      1.25   thorpej raw_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    166      1.25   thorpej     struct mbuf *control, struct lwp *l)
    167       1.1       cgd {
    168      1.15  augustss 	struct rawcb *rp;
    169      1.13   mycroft 	int s;
    170      1.15  augustss 	int error = 0;
    171       1.1       cgd 
    172       1.1       cgd 	if (req == PRU_CONTROL)
    173       1.1       cgd 		return (EOPNOTSUPP);
    174      1.13   mycroft 
    175      1.13   mycroft 	s = splsoftnet();
    176      1.13   mycroft 	rp = sotorawcb(so);
    177      1.13   mycroft #ifdef DIAGNOSTIC
    178      1.13   mycroft 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    179      1.13   mycroft 		panic("raw_usrreq: unexpected control mbuf");
    180      1.13   mycroft #endif
    181  1.30.4.3      yamt 	if (rp == NULL && req != PRU_ATTACH) {
    182       1.1       cgd 		error = EINVAL;
    183       1.1       cgd 		goto release;
    184       1.1       cgd 	}
    185      1.13   mycroft 
    186       1.1       cgd 	switch (req) {
    187       1.1       cgd 
    188       1.1       cgd 	/*
    189       1.1       cgd 	 * Allocate a raw control block and fill in the
    190       1.1       cgd 	 * necessary info to allow packets to be routed to
    191       1.1       cgd 	 * the appropriate raw interface routine.
    192       1.1       cgd 	 */
    193       1.1       cgd 	case PRU_ATTACH:
    194      1.29      elad 		if (l == NULL)
    195       1.1       cgd 			break;
    196      1.29      elad 
    197      1.29      elad 		/* XXX: raw socket permissions are checked in socreate() */
    198      1.29      elad 
    199       1.9       cgd 		error = raw_attach(so, (int)(long)nam);
    200       1.1       cgd 		break;
    201       1.1       cgd 
    202       1.1       cgd 	/*
    203       1.1       cgd 	 * Destroy state just before socket deallocation.
    204       1.1       cgd 	 * Flush data or not depending on the options.
    205       1.1       cgd 	 */
    206       1.1       cgd 	case PRU_DETACH:
    207       1.1       cgd 		raw_detach(rp);
    208       1.1       cgd 		break;
    209       1.1       cgd 
    210       1.1       cgd 	/*
    211       1.1       cgd 	 * If a socket isn't bound to a single address,
    212       1.1       cgd 	 * the raw input routine will hand it anything
    213       1.1       cgd 	 * within that protocol family (assuming there's
    214      1.23     perry 	 * nothing else around it should go to).
    215       1.1       cgd 	 */
    216      1.13   mycroft 	case PRU_BIND:
    217      1.13   mycroft 	case PRU_LISTEN:
    218       1.1       cgd 	case PRU_CONNECT:
    219       1.1       cgd 	case PRU_CONNECT2:
    220       1.1       cgd 		error = EOPNOTSUPP;
    221      1.13   mycroft 		break;
    222       1.1       cgd 
    223       1.1       cgd 	case PRU_DISCONNECT:
    224      1.13   mycroft 		soisdisconnected(so);
    225       1.1       cgd 		raw_disconnect(rp);
    226       1.1       cgd 		break;
    227       1.1       cgd 
    228       1.1       cgd 	/*
    229       1.1       cgd 	 * Mark the connection as being incapable of further input.
    230       1.1       cgd 	 */
    231       1.1       cgd 	case PRU_SHUTDOWN:
    232       1.1       cgd 		socantsendmore(so);
    233       1.1       cgd 		break;
    234       1.1       cgd 
    235      1.13   mycroft 	case PRU_RCVD:
    236      1.13   mycroft 		error = EOPNOTSUPP;
    237      1.13   mycroft 		break;
    238      1.13   mycroft 
    239       1.1       cgd 	/*
    240       1.1       cgd 	 * Ship a packet out.  The appropriate raw output
    241       1.1       cgd 	 * routine handles any massaging necessary.
    242       1.1       cgd 	 */
    243       1.1       cgd 	case PRU_SEND:
    244      1.13   mycroft 		if (control && control->m_len) {
    245      1.13   mycroft 			m_freem(control);
    246      1.13   mycroft 			m_freem(m);
    247      1.13   mycroft 			error = EINVAL;
    248      1.13   mycroft 			break;
    249      1.13   mycroft 		}
    250       1.1       cgd 		if (nam) {
    251      1.13   mycroft 			if ((so->so_state & SS_ISCONNECTED) != 0) {
    252       1.1       cgd 				error = EISCONN;
    253      1.13   mycroft 				goto die;
    254      1.13   mycroft 			}
    255      1.13   mycroft 			error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
    256  1.30.4.3      yamt 			    NULL, nam, NULL, l);
    257      1.13   mycroft 			if (error) {
    258      1.13   mycroft 			die:
    259      1.13   mycroft 				m_freem(m);
    260       1.1       cgd 				break;
    261       1.1       cgd 			}
    262      1.13   mycroft 		} else {
    263      1.13   mycroft 			if ((so->so_state & SS_ISCONNECTED) == 0) {
    264      1.13   mycroft 				error = ENOTCONN;
    265      1.13   mycroft 				goto die;
    266      1.13   mycroft 			}
    267       1.1       cgd 		}
    268       1.1       cgd 		error = (*so->so_proto->pr_output)(m, so);
    269       1.1       cgd 		if (nam)
    270      1.13   mycroft 			raw_disconnect(rp);
    271       1.1       cgd 		break;
    272       1.1       cgd 
    273       1.1       cgd 	case PRU_SENSE:
    274       1.1       cgd 		/*
    275       1.1       cgd 		 * stat: don't bother with a blocksize.
    276       1.1       cgd 		 */
    277       1.1       cgd 		return (0);
    278       1.1       cgd 
    279       1.1       cgd 	/*
    280       1.1       cgd 	 * Not supported.
    281       1.1       cgd 	 */
    282       1.1       cgd 	case PRU_RCVOOB:
    283      1.13   mycroft 		error = EOPNOTSUPP;
    284      1.13   mycroft 		break;
    285       1.1       cgd 
    286       1.1       cgd 	case PRU_SENDOOB:
    287      1.13   mycroft 		m_freem(control);
    288      1.13   mycroft 		m_freem(m);
    289       1.1       cgd 		error = EOPNOTSUPP;
    290       1.1       cgd 		break;
    291       1.1       cgd 
    292       1.1       cgd 	case PRU_SOCKADDR:
    293  1.30.4.3      yamt 		if (rp->rcb_laddr == NULL) {
    294       1.1       cgd 			error = EINVAL;
    295       1.1       cgd 			break;
    296       1.1       cgd 		}
    297      1.13   mycroft 		raw_setsockaddr(rp, nam);
    298       1.1       cgd 		break;
    299       1.1       cgd 
    300       1.1       cgd 	case PRU_PEERADDR:
    301  1.30.4.3      yamt 		if (rp->rcb_faddr == NULL) {
    302       1.1       cgd 			error = ENOTCONN;
    303       1.1       cgd 			break;
    304       1.1       cgd 		}
    305      1.13   mycroft 		raw_setpeeraddr(rp, nam);
    306       1.1       cgd 		break;
    307       1.1       cgd 
    308       1.1       cgd 	default:
    309       1.1       cgd 		panic("raw_usrreq");
    310       1.1       cgd 	}
    311      1.13   mycroft 
    312       1.1       cgd release:
    313      1.13   mycroft 	splx(s);
    314       1.1       cgd 	return (error);
    315       1.1       cgd }
    316