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