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