Home | History | Annotate | Line # | Download | only in net
raw_usrreq.c revision 1.4
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1980, 1986 Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  *
     33  1.2      cgd  *	from: @(#)raw_usrreq.c	7.9 (Berkeley) 6/28/90
     34  1.4  mycroft  *	$Id: raw_usrreq.c,v 1.4 1993/12/18 00:41:01 mycroft Exp $
     35  1.1      cgd  */
     36  1.1      cgd 
     37  1.4  mycroft #include <sys/param.h>
     38  1.4  mycroft #include <sys/socket.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.4  mycroft #include <sys/socket.h>
     43  1.4  mycroft #include <sys/socketvar.h>
     44  1.4  mycroft #include <sys/errno.h>
     45  1.1      cgd 
     46  1.4  mycroft #include <net/if.h>
     47  1.4  mycroft #include <net/route.h>
     48  1.4  mycroft #include <net/netisr.h>
     49  1.4  mycroft #include <net/raw_cb.h>
     50  1.1      cgd 
     51  1.4  mycroft #include <machine/mtpr.h>
     52  1.1      cgd 
     53  1.1      cgd /*
     54  1.1      cgd  * Initialize raw connection block q.
     55  1.1      cgd  */
     56  1.1      cgd raw_init()
     57  1.1      cgd {
     58  1.1      cgd 
     59  1.1      cgd 	rawcb.rcb_next = rawcb.rcb_prev = &rawcb;
     60  1.1      cgd 	rawintrq.ifq_maxlen = IFQ_MAXLEN;
     61  1.1      cgd }
     62  1.1      cgd 
     63  1.1      cgd 
     64  1.1      cgd /*
     65  1.1      cgd  * Raw protocol input routine.  Find the socket
     66  1.1      cgd  * associated with the packet(s) and move them over.  If
     67  1.1      cgd  * nothing exists for this packet, drop it.
     68  1.1      cgd  */
     69  1.1      cgd /*
     70  1.1      cgd  * Raw protocol interface.
     71  1.1      cgd  */
     72  1.1      cgd raw_input(m0, proto, src, dst)
     73  1.1      cgd 	struct mbuf *m0;
     74  1.1      cgd 	register struct sockproto *proto;
     75  1.1      cgd 	struct sockaddr *src, *dst;
     76  1.1      cgd {
     77  1.1      cgd 	register struct rawcb *rp;
     78  1.1      cgd 	register struct mbuf *m = m0;
     79  1.1      cgd 	register int sockets = 0;
     80  1.1      cgd 	struct socket *last;
     81  1.1      cgd 
     82  1.1      cgd 	last = 0;
     83  1.1      cgd 	for (rp = rawcb.rcb_next; rp != &rawcb; rp = rp->rcb_next) {
     84  1.1      cgd 		if (rp->rcb_proto.sp_family != proto->sp_family)
     85  1.1      cgd 			continue;
     86  1.1      cgd 		if (rp->rcb_proto.sp_protocol  &&
     87  1.1      cgd 		    rp->rcb_proto.sp_protocol != proto->sp_protocol)
     88  1.1      cgd 			continue;
     89  1.1      cgd 		/*
     90  1.1      cgd 		 * We assume the lower level routines have
     91  1.1      cgd 		 * placed the address in a canonical format
     92  1.1      cgd 		 * suitable for a structure comparison.
     93  1.1      cgd 		 *
     94  1.1      cgd 		 * Note that if the lengths are not the same
     95  1.1      cgd 		 * the comparison will fail at the first byte.
     96  1.1      cgd 		 */
     97  1.1      cgd #define	equal(a1, a2) \
     98  1.1      cgd   (bcmp((caddr_t)(a1), (caddr_t)(a2), a1->sa_len) == 0)
     99  1.1      cgd 		if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst))
    100  1.1      cgd 			continue;
    101  1.1      cgd 		if (rp->rcb_faddr && !equal(rp->rcb_faddr, src))
    102  1.1      cgd 			continue;
    103  1.1      cgd 		if (last) {
    104  1.1      cgd 			struct mbuf *n;
    105  1.1      cgd 			if (n = m_copy(m, 0, (int)M_COPYALL)) {
    106  1.1      cgd 				if (sbappendaddr(&last->so_rcv, src,
    107  1.1      cgd 				    n, (struct mbuf *)0) == 0)
    108  1.1      cgd 					/* should notify about lost packet */
    109  1.1      cgd 					m_freem(n);
    110  1.1      cgd 				else {
    111  1.1      cgd 					sorwakeup(last);
    112  1.1      cgd 					sockets++;
    113  1.1      cgd 				}
    114  1.1      cgd 			}
    115  1.1      cgd 		}
    116  1.1      cgd 		last = rp->rcb_socket;
    117  1.1      cgd 	}
    118  1.1      cgd 	if (last) {
    119  1.1      cgd 		if (sbappendaddr(&last->so_rcv, src,
    120  1.1      cgd 		    m, (struct mbuf *)0) == 0)
    121  1.1      cgd 			m_freem(m);
    122  1.1      cgd 		else {
    123  1.1      cgd 			sorwakeup(last);
    124  1.1      cgd 			sockets++;
    125  1.1      cgd 		}
    126  1.1      cgd 	} else
    127  1.1      cgd 		m_freem(m);
    128  1.1      cgd 	return (sockets);
    129  1.1      cgd }
    130  1.1      cgd 
    131  1.1      cgd /*ARGSUSED*/
    132  1.1      cgd raw_ctlinput(cmd, arg)
    133  1.1      cgd 	int cmd;
    134  1.1      cgd 	struct sockaddr *arg;
    135  1.1      cgd {
    136  1.1      cgd 
    137  1.1      cgd 	if (cmd < 0 || cmd > PRC_NCMDS)
    138  1.1      cgd 		return;
    139  1.1      cgd 	/* INCOMPLETE */
    140  1.1      cgd }
    141  1.1      cgd 
    142  1.1      cgd /*ARGSUSED*/
    143  1.1      cgd raw_usrreq(so, req, m, nam, control)
    144  1.1      cgd 	struct socket *so;
    145  1.1      cgd 	int req;
    146  1.1      cgd 	struct mbuf *m, *nam, *control;
    147  1.1      cgd {
    148  1.1      cgd 	register struct rawcb *rp = sotorawcb(so);
    149  1.1      cgd 	register int error = 0;
    150  1.1      cgd 	int len;
    151  1.1      cgd 
    152  1.1      cgd 	if (req == PRU_CONTROL)
    153  1.1      cgd 		return (EOPNOTSUPP);
    154  1.1      cgd 	if (control && control->m_len) {
    155  1.1      cgd 		error = EOPNOTSUPP;
    156  1.1      cgd 		goto release;
    157  1.1      cgd 	}
    158  1.1      cgd 	if (rp == 0) {
    159  1.1      cgd 		error = EINVAL;
    160  1.1      cgd 		goto release;
    161  1.1      cgd 	}
    162  1.1      cgd 	switch (req) {
    163  1.1      cgd 
    164  1.1      cgd 	/*
    165  1.1      cgd 	 * Allocate a raw control block and fill in the
    166  1.1      cgd 	 * necessary info to allow packets to be routed to
    167  1.1      cgd 	 * the appropriate raw interface routine.
    168  1.1      cgd 	 */
    169  1.1      cgd 	case PRU_ATTACH:
    170  1.1      cgd 		if ((so->so_state & SS_PRIV) == 0) {
    171  1.1      cgd 			error = EACCES;
    172  1.1      cgd 			break;
    173  1.1      cgd 		}
    174  1.1      cgd 		error = raw_attach(so, (int)nam);
    175  1.1      cgd 		break;
    176  1.1      cgd 
    177  1.1      cgd 	/*
    178  1.1      cgd 	 * Destroy state just before socket deallocation.
    179  1.1      cgd 	 * Flush data or not depending on the options.
    180  1.1      cgd 	 */
    181  1.1      cgd 	case PRU_DETACH:
    182  1.1      cgd 		if (rp == 0) {
    183  1.1      cgd 			error = ENOTCONN;
    184  1.1      cgd 			break;
    185  1.1      cgd 		}
    186  1.1      cgd 		raw_detach(rp);
    187  1.1      cgd 		break;
    188  1.1      cgd 
    189  1.1      cgd #ifdef notdef
    190  1.1      cgd 	/*
    191  1.1      cgd 	 * If a socket isn't bound to a single address,
    192  1.1      cgd 	 * the raw input routine will hand it anything
    193  1.1      cgd 	 * within that protocol family (assuming there's
    194  1.1      cgd 	 * nothing else around it should go to).
    195  1.1      cgd 	 */
    196  1.1      cgd 	case PRU_CONNECT:
    197  1.1      cgd 		if (rp->rcb_faddr) {
    198  1.1      cgd 			error = EISCONN;
    199  1.1      cgd 			break;
    200  1.1      cgd 		}
    201  1.1      cgd 		nam = m_copym(nam, 0, M_COPYALL, M_WAIT);
    202  1.1      cgd 		rp->rcb_faddr = mtod(nam, struct sockaddr *);
    203  1.1      cgd 		soisconnected(so);
    204  1.1      cgd 		break;
    205  1.1      cgd 
    206  1.1      cgd 	case PRU_BIND:
    207  1.1      cgd 		if (rp->rcb_laddr) {
    208  1.1      cgd 			error = EINVAL;			/* XXX */
    209  1.1      cgd 			break;
    210  1.1      cgd 		}
    211  1.1      cgd 		error = raw_bind(so, nam);
    212  1.1      cgd 		break;
    213  1.1      cgd #endif
    214  1.1      cgd 
    215  1.1      cgd 	case PRU_CONNECT2:
    216  1.1      cgd 		error = EOPNOTSUPP;
    217  1.1      cgd 		goto release;
    218  1.1      cgd 
    219  1.1      cgd 	case PRU_DISCONNECT:
    220  1.1      cgd 		if (rp->rcb_faddr == 0) {
    221  1.1      cgd 			error = ENOTCONN;
    222  1.1      cgd 			break;
    223  1.1      cgd 		}
    224  1.1      cgd 		raw_disconnect(rp);
    225  1.1      cgd 		soisdisconnected(so);
    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.1      cgd 	/*
    236  1.1      cgd 	 * Ship a packet out.  The appropriate raw output
    237  1.1      cgd 	 * routine handles any massaging necessary.
    238  1.1      cgd 	 */
    239  1.1      cgd 	case PRU_SEND:
    240  1.1      cgd 		if (nam) {
    241  1.1      cgd 			if (rp->rcb_faddr) {
    242  1.1      cgd 				error = EISCONN;
    243  1.1      cgd 				break;
    244  1.1      cgd 			}
    245  1.1      cgd 			rp->rcb_faddr = mtod(nam, struct sockaddr *);
    246  1.1      cgd 		} else if (rp->rcb_faddr == 0) {
    247  1.1      cgd 			error = ENOTCONN;
    248  1.1      cgd 			break;
    249  1.1      cgd 		}
    250  1.1      cgd 		error = (*so->so_proto->pr_output)(m, so);
    251  1.1      cgd 		m = NULL;
    252  1.1      cgd 		if (nam)
    253  1.1      cgd 			rp->rcb_faddr = 0;
    254  1.1      cgd 		break;
    255  1.1      cgd 
    256  1.1      cgd 	case PRU_ABORT:
    257  1.1      cgd 		raw_disconnect(rp);
    258  1.1      cgd 		sofree(so);
    259  1.1      cgd 		soisdisconnected(so);
    260  1.1      cgd 		break;
    261  1.1      cgd 
    262  1.1      cgd 	case PRU_SENSE:
    263  1.1      cgd 		/*
    264  1.1      cgd 		 * stat: don't bother with a blocksize.
    265  1.1      cgd 		 */
    266  1.1      cgd 		return (0);
    267  1.1      cgd 
    268  1.1      cgd 	/*
    269  1.1      cgd 	 * Not supported.
    270  1.1      cgd 	 */
    271  1.1      cgd 	case PRU_RCVOOB:
    272  1.1      cgd 	case PRU_RCVD:
    273  1.1      cgd 		return(EOPNOTSUPP);
    274  1.1      cgd 
    275  1.1      cgd 	case PRU_LISTEN:
    276  1.1      cgd 	case PRU_ACCEPT:
    277  1.1      cgd 	case PRU_SENDOOB:
    278  1.1      cgd 		error = EOPNOTSUPP;
    279  1.1      cgd 		break;
    280  1.1      cgd 
    281  1.1      cgd 	case PRU_SOCKADDR:
    282  1.1      cgd 		if (rp->rcb_laddr == 0) {
    283  1.1      cgd 			error = EINVAL;
    284  1.1      cgd 			break;
    285  1.1      cgd 		}
    286  1.1      cgd 		len = rp->rcb_laddr->sa_len;
    287  1.1      cgd 		bcopy((caddr_t)rp->rcb_laddr, mtod(nam, caddr_t), (unsigned)len);
    288  1.1      cgd 		nam->m_len = len;
    289  1.1      cgd 		break;
    290  1.1      cgd 
    291  1.1      cgd 	case PRU_PEERADDR:
    292  1.1      cgd 		if (rp->rcb_faddr == 0) {
    293  1.1      cgd 			error = ENOTCONN;
    294  1.1      cgd 			break;
    295  1.1      cgd 		}
    296  1.1      cgd 		len = rp->rcb_faddr->sa_len;
    297  1.1      cgd 		bcopy((caddr_t)rp->rcb_faddr, mtod(nam, caddr_t), (unsigned)len);
    298  1.1      cgd 		nam->m_len = len;
    299  1.1      cgd 		break;
    300  1.1      cgd 
    301  1.1      cgd 	default:
    302  1.1      cgd 		panic("raw_usrreq");
    303  1.1      cgd 	}
    304  1.1      cgd release:
    305  1.1      cgd 	if (m != NULL)
    306  1.1      cgd 		m_freem(m);
    307  1.1      cgd 	return (error);
    308  1.1      cgd }
    309  1.1      cgd 
    310  1.1      cgd rawintr() {} /* XXX - referenced by locore.  will soon go away */
    311