Home | History | Annotate | Line # | Download | only in net
raw_usrreq.c revision 1.11.4.1
      1  1.11.4.1   mycroft /*	$NetBSD: raw_usrreq.c,v 1.11.4.1 1996/12/11 03:43:54 mycroft 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16       1.1       cgd  *    must display the following acknowledgement:
     17       1.1       cgd  *	This product includes software developed by the University of
     18       1.1       cgd  *	California, Berkeley and its contributors.
     19       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20       1.1       cgd  *    may be used to endorse or promote products derived from this software
     21       1.1       cgd  *    without specific prior written permission.
     22       1.1       cgd  *
     23       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.1       cgd  * SUCH DAMAGE.
     34       1.1       cgd  *
     35       1.8       cgd  *	@(#)raw_usrreq.c	8.1 (Berkeley) 6/10/93
     36       1.1       cgd  */
     37       1.1       cgd 
     38       1.4   mycroft #include <sys/param.h>
     39       1.4   mycroft #include <sys/mbuf.h>
     40       1.4   mycroft #include <sys/domain.h>
     41       1.4   mycroft #include <sys/protosw.h>
     42       1.7   mycroft #include <sys/socket.h>
     43       1.4   mycroft #include <sys/socketvar.h>
     44       1.4   mycroft #include <sys/errno.h>
     45      1.11  christos #include <sys/systm.h>
     46  1.11.4.1   mycroft #include <sys/proc.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.1       cgd raw_init()
     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.1       cgd 
     65       1.1       cgd /*
     66       1.1       cgd  * Raw protocol input routine.  Find the socket
     67       1.1       cgd  * associated with the packet(s) and move them over.  If
     68       1.1       cgd  * nothing exists for this packet, drop it.
     69       1.1       cgd  */
     70       1.1       cgd /*
     71       1.1       cgd  * Raw protocol interface.
     72       1.1       cgd  */
     73       1.7   mycroft void
     74      1.11  christos #if __STDC__
     75      1.11  christos raw_input(struct mbuf *m0, ...)
     76      1.11  christos #else
     77      1.11  christos raw_input(m0, va_alist)
     78       1.1       cgd 	struct mbuf *m0;
     79      1.11  christos 	va_dcl
     80      1.11  christos #endif
     81       1.1       cgd {
     82       1.1       cgd 	register struct rawcb *rp;
     83       1.1       cgd 	register struct mbuf *m = m0;
     84       1.1       cgd 	register int sockets = 0;
     85       1.1       cgd 	struct socket *last;
     86      1.11  christos 	va_list ap;
     87      1.11  christos 	register struct sockproto *proto;
     88      1.11  christos 	struct sockaddr *src, *dst;
     89      1.11  christos 
     90      1.11  christos 	va_start(ap, m0);
     91      1.11  christos 	proto = va_arg(ap, struct sockproto *);
     92      1.11  christos 	src = va_arg(ap, struct sockaddr *);
     93      1.11  christos 	dst = va_arg(ap, struct sockaddr *);
     94      1.11  christos 	va_end(ap);
     95       1.1       cgd 
     96       1.1       cgd 	last = 0;
     97      1.10   mycroft 	for (rp = rawcb.lh_first; rp != 0; rp = rp->rcb_list.le_next) {
     98       1.1       cgd 		if (rp->rcb_proto.sp_family != proto->sp_family)
     99       1.1       cgd 			continue;
    100       1.1       cgd 		if (rp->rcb_proto.sp_protocol  &&
    101       1.1       cgd 		    rp->rcb_proto.sp_protocol != proto->sp_protocol)
    102       1.1       cgd 			continue;
    103       1.1       cgd 		/*
    104       1.1       cgd 		 * We assume the lower level routines have
    105       1.1       cgd 		 * placed the address in a canonical format
    106       1.1       cgd 		 * suitable for a structure comparison.
    107       1.1       cgd 		 *
    108       1.1       cgd 		 * Note that if the lengths are not the same
    109       1.1       cgd 		 * the comparison will fail at the first byte.
    110       1.1       cgd 		 */
    111       1.1       cgd #define	equal(a1, a2) \
    112       1.1       cgd   (bcmp((caddr_t)(a1), (caddr_t)(a2), a1->sa_len) == 0)
    113       1.1       cgd 		if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst))
    114       1.1       cgd 			continue;
    115       1.1       cgd 		if (rp->rcb_faddr && !equal(rp->rcb_faddr, src))
    116       1.1       cgd 			continue;
    117       1.1       cgd 		if (last) {
    118       1.1       cgd 			struct mbuf *n;
    119      1.11  christos 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
    120       1.1       cgd 				if (sbappendaddr(&last->so_rcv, src,
    121       1.1       cgd 				    n, (struct mbuf *)0) == 0)
    122       1.1       cgd 					/* should notify about lost packet */
    123       1.1       cgd 					m_freem(n);
    124       1.1       cgd 				else {
    125       1.1       cgd 					sorwakeup(last);
    126       1.1       cgd 					sockets++;
    127       1.1       cgd 				}
    128       1.1       cgd 			}
    129       1.1       cgd 		}
    130       1.1       cgd 		last = rp->rcb_socket;
    131       1.1       cgd 	}
    132       1.1       cgd 	if (last) {
    133       1.1       cgd 		if (sbappendaddr(&last->so_rcv, src,
    134       1.1       cgd 		    m, (struct mbuf *)0) == 0)
    135       1.1       cgd 			m_freem(m);
    136       1.1       cgd 		else {
    137       1.1       cgd 			sorwakeup(last);
    138       1.1       cgd 			sockets++;
    139       1.1       cgd 		}
    140       1.1       cgd 	} else
    141       1.1       cgd 		m_freem(m);
    142       1.1       cgd }
    143       1.1       cgd 
    144       1.1       cgd /*ARGSUSED*/
    145      1.11  christos void *
    146      1.11  christos raw_ctlinput(cmd, arg, d)
    147       1.1       cgd 	int cmd;
    148       1.1       cgd 	struct sockaddr *arg;
    149      1.11  christos 	void *d;
    150       1.1       cgd {
    151       1.1       cgd 
    152       1.1       cgd 	if (cmd < 0 || cmd > PRC_NCMDS)
    153      1.11  christos 		return NULL;
    154      1.11  christos 	return NULL;
    155       1.1       cgd 	/* INCOMPLETE */
    156       1.1       cgd }
    157       1.1       cgd 
    158  1.11.4.1   mycroft void
    159  1.11.4.1   mycroft raw_setsockaddr(rp, nam)
    160  1.11.4.1   mycroft 	register struct rawcb *rp;
    161  1.11.4.1   mycroft 	struct mbuf *nam;
    162  1.11.4.1   mycroft {
    163  1.11.4.1   mycroft 
    164  1.11.4.1   mycroft 	nam->m_len = rp->rcb_laddr->sa_len;
    165  1.11.4.1   mycroft 	bcopy(rp->rcb_laddr, mtod(nam, caddr_t), (size_t)nam->m_len);
    166  1.11.4.1   mycroft }
    167  1.11.4.1   mycroft 
    168  1.11.4.1   mycroft void
    169  1.11.4.1   mycroft raw_setpeeraddr(rp, nam)
    170  1.11.4.1   mycroft 	register struct rawcb *rp;
    171  1.11.4.1   mycroft 	struct mbuf *nam;
    172  1.11.4.1   mycroft {
    173  1.11.4.1   mycroft 
    174  1.11.4.1   mycroft 	nam->m_len = rp->rcb_faddr->sa_len;
    175  1.11.4.1   mycroft 	bcopy(rp->rcb_faddr, mtod(nam, caddr_t), (size_t)nam->m_len);
    176  1.11.4.1   mycroft }
    177  1.11.4.1   mycroft 
    178       1.1       cgd /*ARGSUSED*/
    179       1.7   mycroft int
    180  1.11.4.1   mycroft raw_usrreq(so, req, m, nam, control, p)
    181       1.1       cgd 	struct socket *so;
    182       1.1       cgd 	int req;
    183       1.1       cgd 	struct mbuf *m, *nam, *control;
    184  1.11.4.1   mycroft 	struct proc *p;
    185       1.1       cgd {
    186  1.11.4.1   mycroft 	register struct rawcb *rp;
    187  1.11.4.1   mycroft 	int s;
    188       1.1       cgd 	register int error = 0;
    189       1.1       cgd 
    190       1.1       cgd 	if (req == PRU_CONTROL)
    191       1.1       cgd 		return (EOPNOTSUPP);
    192  1.11.4.1   mycroft 
    193  1.11.4.1   mycroft 	s = splsoftnet();
    194  1.11.4.1   mycroft 	rp = sotorawcb(so);
    195  1.11.4.1   mycroft #ifdef DIAGNOSTIC
    196  1.11.4.1   mycroft 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    197  1.11.4.1   mycroft 		panic("raw_usrreq: unexpected control mbuf");
    198  1.11.4.1   mycroft #endif
    199  1.11.4.1   mycroft 	if (rp == 0 && req != PRU_ATTACH) {
    200       1.1       cgd 		error = EINVAL;
    201       1.1       cgd 		goto release;
    202       1.1       cgd 	}
    203  1.11.4.1   mycroft 
    204       1.1       cgd 	switch (req) {
    205       1.1       cgd 
    206       1.1       cgd 	/*
    207       1.1       cgd 	 * Allocate a raw control block and fill in the
    208       1.1       cgd 	 * necessary info to allow packets to be routed to
    209       1.1       cgd 	 * the appropriate raw interface routine.
    210       1.1       cgd 	 */
    211       1.1       cgd 	case PRU_ATTACH:
    212  1.11.4.1   mycroft 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
    213       1.1       cgd 			error = EACCES;
    214       1.1       cgd 			break;
    215       1.1       cgd 		}
    216       1.9       cgd 		error = raw_attach(so, (int)(long)nam);
    217       1.1       cgd 		break;
    218       1.1       cgd 
    219       1.1       cgd 	/*
    220       1.1       cgd 	 * Destroy state just before socket deallocation.
    221       1.1       cgd 	 * Flush data or not depending on the options.
    222       1.1       cgd 	 */
    223       1.1       cgd 	case PRU_DETACH:
    224       1.1       cgd 		raw_detach(rp);
    225       1.1       cgd 		break;
    226       1.1       cgd 
    227       1.1       cgd 	/*
    228       1.1       cgd 	 * If a socket isn't bound to a single address,
    229       1.1       cgd 	 * the raw input routine will hand it anything
    230       1.1       cgd 	 * within that protocol family (assuming there's
    231       1.1       cgd 	 * nothing else around it should go to).
    232       1.1       cgd 	 */
    233       1.1       cgd 	case PRU_BIND:
    234  1.11.4.1   mycroft 	case PRU_LISTEN:
    235  1.11.4.1   mycroft 	case PRU_CONNECT:
    236       1.1       cgd 	case PRU_CONNECT2:
    237       1.1       cgd 		error = EOPNOTSUPP;
    238  1.11.4.1   mycroft 		break;
    239       1.1       cgd 
    240       1.1       cgd 	case PRU_DISCONNECT:
    241       1.1       cgd 		soisdisconnected(so);
    242  1.11.4.1   mycroft 		raw_disconnect(rp);
    243       1.1       cgd 		break;
    244       1.1       cgd 
    245       1.1       cgd 	/*
    246       1.1       cgd 	 * Mark the connection as being incapable of further input.
    247       1.1       cgd 	 */
    248       1.1       cgd 	case PRU_SHUTDOWN:
    249       1.1       cgd 		socantsendmore(so);
    250       1.1       cgd 		break;
    251       1.1       cgd 
    252  1.11.4.1   mycroft 	case PRU_RCVD:
    253  1.11.4.1   mycroft 		error = EOPNOTSUPP;
    254  1.11.4.1   mycroft 		break;
    255  1.11.4.1   mycroft 
    256       1.1       cgd 	/*
    257       1.1       cgd 	 * Ship a packet out.  The appropriate raw output
    258       1.1       cgd 	 * routine handles any massaging necessary.
    259       1.1       cgd 	 */
    260       1.1       cgd 	case PRU_SEND:
    261  1.11.4.1   mycroft 		if (control && control->m_len) {
    262  1.11.4.1   mycroft 			m_freem(control);
    263  1.11.4.1   mycroft 			m_freem(m);
    264  1.11.4.1   mycroft 			error = EINVAL;
    265  1.11.4.1   mycroft 			break;
    266  1.11.4.1   mycroft 		}
    267       1.1       cgd 		if (nam) {
    268  1.11.4.1   mycroft 			if ((so->so_state & SS_ISCONNECTED) != 0) {
    269       1.1       cgd 				error = EISCONN;
    270  1.11.4.1   mycroft 				goto die;
    271  1.11.4.1   mycroft 			}
    272  1.11.4.1   mycroft 			error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
    273  1.11.4.1   mycroft 			    (struct mbuf *)0, nam, (struct mbuf *)0, p);
    274  1.11.4.1   mycroft 			if (error) {
    275  1.11.4.1   mycroft 			die:
    276  1.11.4.1   mycroft 				m_freem(m);
    277       1.1       cgd 				break;
    278       1.1       cgd 			}
    279  1.11.4.1   mycroft 		} else {
    280  1.11.4.1   mycroft 			if ((so->so_state & SS_ISCONNECTED) == 0) {
    281  1.11.4.1   mycroft 				error = ENOTCONN;
    282  1.11.4.1   mycroft 				goto die;
    283  1.11.4.1   mycroft 			}
    284       1.1       cgd 		}
    285       1.1       cgd 		error = (*so->so_proto->pr_output)(m, so);
    286       1.1       cgd 		if (nam)
    287  1.11.4.1   mycroft 			raw_disconnect(rp);
    288       1.1       cgd 		break;
    289       1.1       cgd 
    290       1.1       cgd 	case PRU_SENSE:
    291       1.1       cgd 		/*
    292       1.1       cgd 		 * stat: don't bother with a blocksize.
    293       1.1       cgd 		 */
    294       1.1       cgd 		return (0);
    295       1.1       cgd 
    296       1.1       cgd 	/*
    297       1.1       cgd 	 * Not supported.
    298       1.1       cgd 	 */
    299       1.1       cgd 	case PRU_RCVOOB:
    300  1.11.4.1   mycroft 		error = EOPNOTSUPP;
    301  1.11.4.1   mycroft 		break;
    302       1.1       cgd 
    303       1.1       cgd 	case PRU_SENDOOB:
    304  1.11.4.1   mycroft 		m_freem(control);
    305  1.11.4.1   mycroft 		m_freem(m);
    306       1.1       cgd 		error = EOPNOTSUPP;
    307       1.1       cgd 		break;
    308       1.1       cgd 
    309       1.1       cgd 	case PRU_SOCKADDR:
    310       1.1       cgd 		if (rp->rcb_laddr == 0) {
    311       1.1       cgd 			error = EINVAL;
    312       1.1       cgd 			break;
    313       1.1       cgd 		}
    314  1.11.4.1   mycroft 		raw_setsockaddr(rp, nam);
    315       1.1       cgd 		break;
    316       1.1       cgd 
    317       1.1       cgd 	case PRU_PEERADDR:
    318       1.1       cgd 		if (rp->rcb_faddr == 0) {
    319       1.1       cgd 			error = ENOTCONN;
    320       1.1       cgd 			break;
    321       1.1       cgd 		}
    322  1.11.4.1   mycroft 		raw_setpeeraddr(rp, nam);
    323       1.1       cgd 		break;
    324       1.1       cgd 
    325       1.1       cgd 	default:
    326       1.1       cgd 		panic("raw_usrreq");
    327       1.1       cgd 	}
    328  1.11.4.1   mycroft 
    329       1.1       cgd release:
    330  1.11.4.1   mycroft 	splx(s);
    331       1.1       cgd 	return (error);
    332       1.1       cgd }
    333