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