xref: /csrg-svn/sys/net/raw_usrreq.c (revision 8636)
1*8636Sroot /*	raw_usrreq.c	4.20	82/10/17	*/
25121Swnj 
35121Swnj #include "../h/param.h"
45121Swnj #include "../h/mbuf.h"
55612Swnj #include "../h/protosw.h"
65121Swnj #include "../h/socket.h"
75121Swnj #include "../h/socketvar.h"
8*8636Sroot #include "../vax/mtpr.h"
95121Swnj #include "../net/if.h"
108395Swnj #include "../net/netisr.h"
115612Swnj #include "../net/raw_cb.h"
126509Ssam #include <errno.h>
135121Swnj 
146211Swnj int	rawqmaxlen = IFQ_MAXLEN;
156211Swnj 
165121Swnj /*
175612Swnj  * Initialize raw connection block q.
186211Swnj  */
195612Swnj raw_init()
205612Swnj {
216211Swnj 
225612Swnj 	rawcb.rcb_next = rawcb.rcb_prev = &rawcb;
236211Swnj 	rawintrq.ifq_maxlen = IFQ_MAXLEN;
245612Swnj }
255612Swnj 
265612Swnj /*
275121Swnj  * Raw protocol interface.
285121Swnj  */
296529Ssam raw_input(m0, proto, src, dst)
305612Swnj 	struct mbuf *m0;
316509Ssam 	struct sockproto *proto;
326529Ssam 	struct sockaddr *src, *dst;
335121Swnj {
345612Swnj 	register struct mbuf *m;
355612Swnj 	struct raw_header *rh;
365121Swnj 	int s;
375121Swnj 
385612Swnj 	/*
395612Swnj 	 * Rip off an mbuf for a generic header.
405612Swnj 	 */
415612Swnj 	m = m_get(M_DONTWAIT);
425612Swnj 	if (m == 0) {
435612Swnj 		m_freem(m0);
445612Swnj 		return;
455612Swnj 	}
465612Swnj 	m->m_next = m0;
475612Swnj 	m->m_len = sizeof(struct raw_header);
485612Swnj 	rh = mtod(m, struct raw_header *);
496509Ssam 	rh->raw_dst = *dst;
506509Ssam 	rh->raw_src = *src;
516509Ssam 	rh->raw_proto = *proto;
525612Swnj 
535612Swnj 	/*
545612Swnj 	 * Header now contains enough info to decide
555612Swnj 	 * which socket to place packet in (if any).
565612Swnj 	 * Queue it up for the raw protocol process
575612Swnj 	 * running at software interrupt level.
585612Swnj 	 */
595121Swnj 	s = splimp();
606211Swnj 	if (IF_QFULL(&rawintrq))
616211Swnj 		m_freem(m);
626211Swnj 	else
636211Swnj 		IF_ENQUEUE(&rawintrq, m);
645121Swnj 	splx(s);
656263Swnj 	schednetisr(NETISR_RAW);
665121Swnj }
675121Swnj 
685612Swnj /*
695612Swnj  * Raw protocol input routine.  Process packets entered
705612Swnj  * into the queue at interrupt time.  Find the socket
715612Swnj  * associated with the packet(s) and move them over.  If
725612Swnj  * nothing exists for this packet, drop it.
735612Swnj  */
745121Swnj rawintr()
755121Swnj {
765121Swnj 	int s;
775121Swnj 	struct mbuf *m;
785612Swnj 	register struct rawcb *rp;
796509Ssam 	register struct protosw *lproto;
806529Ssam 	register struct raw_header *rh;
815612Swnj 	struct socket *last;
825121Swnj 
835121Swnj next:
845121Swnj 	s = splimp();
855121Swnj 	IF_DEQUEUE(&rawintrq, m);
865121Swnj 	splx(s);
875121Swnj 	if (m == 0)
885121Swnj 		return;
896509Ssam 	rh = mtod(m, struct raw_header *);
905612Swnj 	last = 0;
915612Swnj 	for (rp = rawcb.rcb_next; rp != &rawcb; rp = rp->rcb_next) {
926509Ssam 		lproto = rp->rcb_socket->so_proto;
936509Ssam 		if (lproto->pr_family != rh->raw_proto.sp_family)
945612Swnj 			continue;
956509Ssam 		if (lproto->pr_protocol &&
966509Ssam 		    lproto->pr_protocol != rh->raw_proto.sp_protocol)
975612Swnj 			continue;
985612Swnj 		/*
995612Swnj 		 * We assume the lower level routines have
1005612Swnj 		 * placed the address in a canonical format
1016509Ssam 		 * suitable for a structure comparison.
1025612Swnj 		 */
1036529Ssam #define equal(a1, a2) \
1046529Ssam 	(bcmp((caddr_t)&(a1), (caddr_t)&(a2), sizeof (struct sockaddr)) == 0)
1056509Ssam 		if ((rp->rcb_flags & RAW_LADDR) &&
1066529Ssam 		    !equal(rp->rcb_laddr, rh->raw_dst))
1075612Swnj 			continue;
1086509Ssam 		if ((rp->rcb_flags & RAW_FADDR) &&
1096529Ssam 		    !equal(rp->rcb_faddr, rh->raw_src))
1106509Ssam 			continue;
1115612Swnj 		if (last) {
1125612Swnj 			struct mbuf *n;
1137517Sroot 			if ((n = m_copy(m->m_next, 0, (int)M_COPYALL)) == 0)
1145612Swnj 				goto nospace;
1156509Ssam 			if (sbappendaddr(&last->so_rcv, &rh->raw_src, n)==0) {
1166509Ssam 				/* should notify about lost packet */
1175646Ssam 				m_freem(n);
1185646Ssam 				goto nospace;
1195646Ssam 			}
1205612Swnj 			sorwakeup(last);
1215612Swnj 		}
1225612Swnj nospace:
1236509Ssam 		last = rp->rcb_socket;
1245612Swnj 	}
1256584Ssam 	if (last) {
1266584Ssam 		m = m_free(m);		/* header */
1276584Ssam 		if (sbappendaddr(&last->so_rcv, &rh->raw_src, m) == 0)
1286584Ssam 			goto drop;
1296584Ssam 		sorwakeup(last);
1306584Ssam 		goto next;
1316584Ssam 	}
1325121Swnj drop:
1335121Swnj 	m_freem(m);
1345121Swnj 	goto next;
1355121Swnj }
1365121Swnj 
137*8636Sroot /*ARGSUSED*/
1386584Ssam raw_ctlinput(cmd, arg)
1396584Ssam 	int cmd;
1406584Ssam 	caddr_t arg;
1416584Ssam {
1426591Ssam 
1436591Ssam 	if (cmd < 0 || cmd > PRC_NCMDS)
1446591Ssam 		return;
145*8636Sroot 	/* INCOMPLETE */
1466584Ssam }
1476584Ssam 
1485121Swnj /*ARGSUSED*/
1498395Swnj raw_usrreq(so, req, m, nam, opt)
1505121Swnj 	struct socket *so;
1515121Swnj 	int req;
1528395Swnj 	struct mbuf *m, *nam;
1538395Swnj 	struct socketopt *opt;
1545121Swnj {
1555612Swnj 	register struct rawcb *rp = sotorawcb(so);
1565612Swnj 	int error = 0;
1575121Swnj 
1585612Swnj 	if (rp == 0 && req != PRU_ATTACH)
1595612Swnj 		return (EINVAL);
1605121Swnj 
1615612Swnj 	switch (req) {
1625612Swnj 
1635612Swnj 	/*
1645612Swnj 	 * Allocate a raw control block and fill in the
1655612Swnj 	 * necessary info to allow packets to be routed to
1665612Swnj 	 * the appropriate raw interface routine.
1675612Swnj 	 */
1685612Swnj 	case PRU_ATTACH:
1696211Swnj 		if ((so->so_state & SS_PRIV) == 0)
1706584Ssam 			return (EACCES);
1715612Swnj 		if (rp)
1726211Swnj 			return (EINVAL);
1738395Swnj 		error = raw_attach(so);
1745612Swnj 		break;
1755612Swnj 
1765612Swnj 	/*
1775612Swnj 	 * Destroy state just before socket deallocation.
1785612Swnj 	 * Flush data or not depending on the options.
1795612Swnj 	 */
1805612Swnj 	case PRU_DETACH:
1815612Swnj 		if (rp == 0)
1825612Swnj 			return (ENOTCONN);
1835612Swnj 		raw_detach(rp);
1845612Swnj 		break;
1855612Swnj 
1865612Swnj 	/*
1875612Swnj 	 * If a socket isn't bound to a single address,
1885612Swnj 	 * the raw input routine will hand it anything
1895612Swnj 	 * within that protocol family (assuming there's
1905612Swnj 	 * nothing else around it should go to).
1915612Swnj 	 */
1925612Swnj 	case PRU_CONNECT:
1936509Ssam 		if (rp->rcb_flags & RAW_FADDR)
1945612Swnj 			return (EISCONN);
1958395Swnj 		raw_connaddr(rp, nam);
1965612Swnj 		soisconnected(so);
1975612Swnj 		break;
1985612Swnj 
1995612Swnj 	case PRU_DISCONNECT:
2006509Ssam 		if ((rp->rcb_flags & RAW_FADDR) == 0)
2015612Swnj 			return (ENOTCONN);
2025612Swnj 		raw_disconnect(rp);
2035612Swnj 		soisdisconnected(so);
2045612Swnj 		break;
2055612Swnj 
2065612Swnj 	/*
2075612Swnj 	 * Mark the connection as being incapable of further input.
2085612Swnj 	 */
2095612Swnj 	case PRU_SHUTDOWN:
2105612Swnj 		socantsendmore(so);
2115612Swnj 		break;
2125612Swnj 
2135612Swnj 	/*
2145612Swnj 	 * Ship a packet out.  The appropriate raw output
2155612Swnj 	 * routine handles any massaging necessary.
2165612Swnj 	 */
2175612Swnj 	case PRU_SEND:
2188395Swnj 		if (nam) {
2196509Ssam 			if (rp->rcb_flags & RAW_FADDR)
2205612Swnj 				return (EISCONN);
2218395Swnj 			raw_connaddr(rp, nam);
2226509Ssam 		} else if ((rp->rcb_flags & RAW_FADDR) == 0)
2235612Swnj 			return (ENOTCONN);
2246505Ssam 		error = (*so->so_proto->pr_output)(m, so);
2258395Swnj 		if (nam)
2266509Ssam 			rp->rcb_flags &= ~RAW_FADDR;
2275612Swnj 		break;
2285612Swnj 
2295612Swnj 	case PRU_ABORT:
2305612Swnj 		raw_disconnect(rp);
2315612Swnj 		sofree(so);
2325612Swnj 		soisdisconnected(so);
2335612Swnj 		break;
2345612Swnj 
2355612Swnj 	/*
2365612Swnj 	 * Not supported.
2375612Swnj 	 */
2385612Swnj 	case PRU_ACCEPT:
2395612Swnj 	case PRU_RCVD:
2405612Swnj 	case PRU_CONTROL:
2415612Swnj 	case PRU_SENSE:
2425612Swnj 	case PRU_RCVOOB:
2435612Swnj 	case PRU_SENDOOB:
2445612Swnj 		error = EOPNOTSUPP;
2455612Swnj 		break;
2465612Swnj 
2476509Ssam 	case PRU_SOCKADDR:
2488395Swnj 		bcopy((caddr_t)&rp->rcb_laddr, mtod(nam, struct sockaddr *),
2498395Swnj 		    sizeof (struct sockaddr));
2508395Swnj 		nam->m_len = sizeof (struct sockaddr);
2516509Ssam 		break;
2526509Ssam 
2535612Swnj 	default:
2545612Swnj 		panic("raw_usrreq");
2555612Swnj 	}
2565612Swnj 	return (error);
2575121Swnj }
258