xref: /csrg-svn/sys/net/raw_usrreq.c (revision 6591)
1*6591Ssam /*	raw_usrreq.c	4.16	82/04/25	*/
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"
85121Swnj #include "../h/mtpr.h"
95121Swnj #include "../net/in.h"
105121Swnj #include "../net/in_systm.h"
115121Swnj #include "../net/if.h"
125612Swnj #include "../net/raw_cb.h"
136509Ssam #include <errno.h>
145121Swnj 
156211Swnj int	rawqmaxlen = IFQ_MAXLEN;
166211Swnj 
175121Swnj /*
185612Swnj  * Initialize raw connection block q.
196211Swnj  */
205612Swnj raw_init()
215612Swnj {
226211Swnj 
235612Swnj COUNT(RAW_INIT);
245612Swnj 	rawcb.rcb_next = rawcb.rcb_prev = &rawcb;
256211Swnj 	rawintrq.ifq_maxlen = IFQ_MAXLEN;
265612Swnj }
275612Swnj 
285612Swnj /*
295121Swnj  * Raw protocol interface.
305121Swnj  */
316529Ssam raw_input(m0, proto, src, dst)
325612Swnj 	struct mbuf *m0;
336509Ssam 	struct sockproto *proto;
346529Ssam 	struct sockaddr *src, *dst;
355121Swnj {
365612Swnj 	register struct mbuf *m;
375612Swnj 	struct raw_header *rh;
385121Swnj 	int s;
395121Swnj 
405668Ssam COUNT(RAW_INPUT);
415612Swnj 	/*
425612Swnj 	 * Rip off an mbuf for a generic header.
435612Swnj 	 */
445612Swnj 	m = m_get(M_DONTWAIT);
455612Swnj 	if (m == 0) {
465612Swnj 		m_freem(m0);
475612Swnj 		return;
485612Swnj 	}
495612Swnj 	m->m_next = m0;
505612Swnj 	m->m_off = MMINOFF;
515612Swnj 	m->m_len = sizeof(struct raw_header);
525612Swnj 	rh = mtod(m, struct raw_header *);
536509Ssam 	rh->raw_dst = *dst;
546509Ssam 	rh->raw_src = *src;
556509Ssam 	rh->raw_proto = *proto;
565612Swnj 
575612Swnj 	/*
585612Swnj 	 * Header now contains enough info to decide
595612Swnj 	 * which socket to place packet in (if any).
605612Swnj 	 * Queue it up for the raw protocol process
615612Swnj 	 * running at software interrupt level.
625612Swnj 	 */
635121Swnj 	s = splimp();
646211Swnj 	if (IF_QFULL(&rawintrq))
656211Swnj 		m_freem(m);
666211Swnj 	else
676211Swnj 		IF_ENQUEUE(&rawintrq, m);
685121Swnj 	splx(s);
696263Swnj 	schednetisr(NETISR_RAW);
705121Swnj }
715121Swnj 
725612Swnj /*
735612Swnj  * Raw protocol input routine.  Process packets entered
745612Swnj  * into the queue at interrupt time.  Find the socket
755612Swnj  * associated with the packet(s) and move them over.  If
765612Swnj  * nothing exists for this packet, drop it.
775612Swnj  */
785121Swnj rawintr()
795121Swnj {
805121Swnj 	int s;
815121Swnj 	struct mbuf *m;
825612Swnj 	register struct rawcb *rp;
836509Ssam 	register struct protosw *lproto;
846529Ssam 	register struct raw_header *rh;
855612Swnj 	struct socket *last;
865121Swnj 
875121Swnj COUNT(RAWINTR);
885121Swnj next:
895121Swnj 	s = splimp();
905121Swnj 	IF_DEQUEUE(&rawintrq, m);
915121Swnj 	splx(s);
925121Swnj 	if (m == 0)
935121Swnj 		return;
946509Ssam 	rh = mtod(m, struct raw_header *);
955612Swnj 	last = 0;
965612Swnj 	for (rp = rawcb.rcb_next; rp != &rawcb; rp = rp->rcb_next) {
976509Ssam 		lproto = rp->rcb_socket->so_proto;
986509Ssam 		if (lproto->pr_family != rh->raw_proto.sp_family)
995612Swnj 			continue;
1006509Ssam 		if (lproto->pr_protocol &&
1016509Ssam 		    lproto->pr_protocol != rh->raw_proto.sp_protocol)
1025612Swnj 			continue;
1035612Swnj 		/*
1045612Swnj 		 * We assume the lower level routines have
1055612Swnj 		 * placed the address in a canonical format
1066509Ssam 		 * suitable for a structure comparison.
1075612Swnj 		 */
1086529Ssam #define equal(a1, a2) \
1096529Ssam 	(bcmp((caddr_t)&(a1), (caddr_t)&(a2), sizeof (struct sockaddr)) == 0)
1106509Ssam 		if ((rp->rcb_flags & RAW_LADDR) &&
1116529Ssam 		    !equal(rp->rcb_laddr, rh->raw_dst))
1125612Swnj 			continue;
1136509Ssam 		if ((rp->rcb_flags & RAW_FADDR) &&
1146529Ssam 		    !equal(rp->rcb_faddr, rh->raw_src))
1156509Ssam 			continue;
1165612Swnj 		if (last) {
1175612Swnj 			struct mbuf *n;
1186161Ssam 			if (n = m_copy(m->m_next, 0, (int)M_COPYALL))
1195612Swnj 				goto nospace;
1206509Ssam 			if (sbappendaddr(&last->so_rcv, &rh->raw_src, n)==0) {
1216509Ssam 				/* should notify about lost packet */
1225646Ssam 				m_freem(n);
1235646Ssam 				goto nospace;
1245646Ssam 			}
1255612Swnj 			sorwakeup(last);
1265612Swnj 		}
1275612Swnj nospace:
1286509Ssam 		last = rp->rcb_socket;
1295612Swnj 	}
1306584Ssam 	if (last) {
1316584Ssam 		m = m_free(m);		/* header */
1326584Ssam 		if (sbappendaddr(&last->so_rcv, &rh->raw_src, m) == 0)
1336584Ssam 			goto drop;
1346584Ssam 		sorwakeup(last);
1356584Ssam 		goto next;
1366584Ssam 	}
1375121Swnj drop:
1385121Swnj 	m_freem(m);
1395121Swnj 	goto next;
1405121Swnj }
1415121Swnj 
1426584Ssam raw_ctlinput(cmd, arg)
1436584Ssam 	int cmd;
1446584Ssam 	caddr_t arg;
1456584Ssam {
1466584Ssam COUNT(RAW_CTLINPUT);
147*6591Ssam 
148*6591Ssam 	if (cmd < 0 || cmd > PRC_NCMDS)
149*6591Ssam 		return;
1506584Ssam }
1516584Ssam 
1525121Swnj /*ARGSUSED*/
1535121Swnj raw_usrreq(so, req, m, addr)
1545121Swnj 	struct socket *so;
1555121Swnj 	int req;
1565121Swnj 	struct mbuf *m;
1575121Swnj 	caddr_t addr;
1585121Swnj {
1595612Swnj 	register struct rawcb *rp = sotorawcb(so);
1605612Swnj 	int error = 0;
1615121Swnj 
1625121Swnj COUNT(RAW_USRREQ);
1635612Swnj 	if (rp == 0 && req != PRU_ATTACH)
1645612Swnj 		return (EINVAL);
1655121Swnj 
1665612Swnj 	switch (req) {
1675612Swnj 
1685612Swnj 	/*
1695612Swnj 	 * Allocate a raw control block and fill in the
1705612Swnj 	 * necessary info to allow packets to be routed to
1715612Swnj 	 * the appropriate raw interface routine.
1725612Swnj 	 */
1735612Swnj 	case PRU_ATTACH:
1746211Swnj 		if ((so->so_state & SS_PRIV) == 0)
1756584Ssam 			return (EACCES);
1765612Swnj 		if (rp)
1776211Swnj 			return (EINVAL);
1785612Swnj 		error = raw_attach(so, (struct sockaddr *)addr);
1795612Swnj 		break;
1805612Swnj 
1815612Swnj 	/*
1825612Swnj 	 * Destroy state just before socket deallocation.
1835612Swnj 	 * Flush data or not depending on the options.
1845612Swnj 	 */
1855612Swnj 	case PRU_DETACH:
1865612Swnj 		if (rp == 0)
1875612Swnj 			return (ENOTCONN);
1885612Swnj 		raw_detach(rp);
1895612Swnj 		break;
1905612Swnj 
1915612Swnj 	/*
1925612Swnj 	 * If a socket isn't bound to a single address,
1935612Swnj 	 * the raw input routine will hand it anything
1945612Swnj 	 * within that protocol family (assuming there's
1955612Swnj 	 * nothing else around it should go to).
1965612Swnj 	 */
1975612Swnj 	case PRU_CONNECT:
1986509Ssam 		if (rp->rcb_flags & RAW_FADDR)
1995612Swnj 			return (EISCONN);
2005612Swnj 		raw_connaddr(rp, (struct sockaddr *)addr);
2015612Swnj 		soisconnected(so);
2025612Swnj 		break;
2035612Swnj 
2045612Swnj 	case PRU_DISCONNECT:
2056509Ssam 		if ((rp->rcb_flags & RAW_FADDR) == 0)
2065612Swnj 			return (ENOTCONN);
2075612Swnj 		raw_disconnect(rp);
2085612Swnj 		soisdisconnected(so);
2095612Swnj 		break;
2105612Swnj 
2115612Swnj 	/*
2125612Swnj 	 * Mark the connection as being incapable of further input.
2135612Swnj 	 */
2145612Swnj 	case PRU_SHUTDOWN:
2155612Swnj 		socantsendmore(so);
2165612Swnj 		break;
2175612Swnj 
2185612Swnj 	/*
2195612Swnj 	 * Ship a packet out.  The appropriate raw output
2205612Swnj 	 * routine handles any massaging necessary.
2215612Swnj 	 */
2225612Swnj 	case PRU_SEND:
2235612Swnj 		if (addr) {
2246509Ssam 			if (rp->rcb_flags & RAW_FADDR)
2255612Swnj 				return (EISCONN);
2265612Swnj 			raw_connaddr(rp, (struct sockaddr *)addr);
2276509Ssam 		} else if ((rp->rcb_flags & RAW_FADDR) == 0)
2285612Swnj 			return (ENOTCONN);
2296505Ssam 		error = (*so->so_proto->pr_output)(m, so);
2305612Swnj 		if (addr)
2316509Ssam 			rp->rcb_flags &= ~RAW_FADDR;
2325612Swnj 		break;
2335612Swnj 
2345612Swnj 	case PRU_ABORT:
2355612Swnj 		raw_disconnect(rp);
2365612Swnj 		sofree(so);
2375612Swnj 		soisdisconnected(so);
2385612Swnj 		break;
2395612Swnj 
2405612Swnj 	/*
2415612Swnj 	 * Not supported.
2425612Swnj 	 */
2435612Swnj 	case PRU_ACCEPT:
2445612Swnj 	case PRU_RCVD:
2455612Swnj 	case PRU_CONTROL:
2465612Swnj 	case PRU_SENSE:
2475612Swnj 	case PRU_RCVOOB:
2485612Swnj 	case PRU_SENDOOB:
2495612Swnj 		error = EOPNOTSUPP;
2505612Swnj 		break;
2515612Swnj 
2526509Ssam 	case PRU_SOCKADDR:
2536509Ssam 		bcopy(addr, (caddr_t)&rp->rcb_laddr, sizeof (struct sockaddr));
2546509Ssam 		break;
2556509Ssam 
2565612Swnj 	default:
2575612Swnj 		panic("raw_usrreq");
2585612Swnj 	}
2595612Swnj 	return (error);
2605121Swnj }
261