xref: /csrg-svn/sys/net/raw_usrreq.c (revision 25221)
123162Smckusick /*
223162Smckusick  * Copyright (c) 1980 Regents of the University of California.
323162Smckusick  * All rights reserved.  The Berkeley software License Agreement
423162Smckusick  * specifies the terms and conditions for redistribution.
523162Smckusick  *
6*25221Skarels  *	@(#)raw_usrreq.c	6.9 (Berkeley) 10/17/85
723162Smckusick  */
85121Swnj 
917037Sbloom #include "param.h"
1017037Sbloom #include "mbuf.h"
1117037Sbloom #include "domain.h"
1217037Sbloom #include "protosw.h"
1317037Sbloom #include "socket.h"
1417037Sbloom #include "socketvar.h"
1517037Sbloom #include "errno.h"
1610890Ssam 
1717037Sbloom #include "if.h"
1817037Sbloom #include "route.h"
1917037Sbloom #include "netisr.h"
2017037Sbloom #include "raw_cb.h"
215121Swnj 
2210890Ssam #include "../vax/mtpr.h"
2310890Ssam 
245121Swnj /*
255612Swnj  * Initialize raw connection block q.
266211Swnj  */
275612Swnj raw_init()
285612Swnj {
296211Swnj 
305612Swnj 	rawcb.rcb_next = rawcb.rcb_prev = &rawcb;
316211Swnj 	rawintrq.ifq_maxlen = IFQ_MAXLEN;
325612Swnj }
335612Swnj 
345612Swnj /*
355121Swnj  * Raw protocol interface.
365121Swnj  */
376529Ssam raw_input(m0, proto, src, dst)
385612Swnj 	struct mbuf *m0;
396509Ssam 	struct sockproto *proto;
406529Ssam 	struct sockaddr *src, *dst;
415121Swnj {
425612Swnj 	register struct mbuf *m;
435612Swnj 	struct raw_header *rh;
445121Swnj 	int s;
455121Swnj 
465612Swnj 	/*
475612Swnj 	 * Rip off an mbuf for a generic header.
485612Swnj 	 */
499638Ssam 	m = m_get(M_DONTWAIT, MT_HEADER);
505612Swnj 	if (m == 0) {
515612Swnj 		m_freem(m0);
525612Swnj 		return;
535612Swnj 	}
545612Swnj 	m->m_next = m0;
555612Swnj 	m->m_len = sizeof(struct raw_header);
565612Swnj 	rh = mtod(m, struct raw_header *);
576509Ssam 	rh->raw_dst = *dst;
586509Ssam 	rh->raw_src = *src;
596509Ssam 	rh->raw_proto = *proto;
605612Swnj 
615612Swnj 	/*
625612Swnj 	 * Header now contains enough info to decide
635612Swnj 	 * which socket to place packet in (if any).
645612Swnj 	 * Queue it up for the raw protocol process
655612Swnj 	 * running at software interrupt level.
665612Swnj 	 */
675121Swnj 	s = splimp();
686211Swnj 	if (IF_QFULL(&rawintrq))
696211Swnj 		m_freem(m);
706211Swnj 	else
716211Swnj 		IF_ENQUEUE(&rawintrq, m);
725121Swnj 	splx(s);
736263Swnj 	schednetisr(NETISR_RAW);
745121Swnj }
755121Swnj 
765612Swnj /*
775612Swnj  * Raw protocol input routine.  Process packets entered
785612Swnj  * into the queue at interrupt time.  Find the socket
795612Swnj  * associated with the packet(s) and move them over.  If
805612Swnj  * nothing exists for this packet, drop it.
815612Swnj  */
825121Swnj rawintr()
835121Swnj {
845121Swnj 	int s;
855121Swnj 	struct mbuf *m;
865612Swnj 	register struct rawcb *rp;
876529Ssam 	register struct raw_header *rh;
885612Swnj 	struct socket *last;
895121Swnj 
905121Swnj next:
915121Swnj 	s = splimp();
925121Swnj 	IF_DEQUEUE(&rawintrq, m);
935121Swnj 	splx(s);
945121Swnj 	if (m == 0)
955121Swnj 		return;
966509Ssam 	rh = mtod(m, struct raw_header *);
975612Swnj 	last = 0;
985612Swnj 	for (rp = rawcb.rcb_next; rp != &rawcb; rp = rp->rcb_next) {
9921769Skarels 		if (rp->rcb_proto.sp_family != rh->raw_proto.sp_family)
1005612Swnj 			continue;
10121769Skarels 		if (rp->rcb_proto.sp_protocol  &&
10221769Skarels 		    rp->rcb_proto.sp_protocol != rh->raw_proto.sp_protocol)
1035612Swnj 			continue;
1045612Swnj 		/*
1055612Swnj 		 * We assume the lower level routines have
1065612Swnj 		 * placed the address in a canonical format
1076509Ssam 		 * suitable for a structure comparison.
1085612Swnj 		 */
1096529Ssam #define equal(a1, a2) \
1106529Ssam 	(bcmp((caddr_t)&(a1), (caddr_t)&(a2), sizeof (struct sockaddr)) == 0)
1116509Ssam 		if ((rp->rcb_flags & RAW_LADDR) &&
1126529Ssam 		    !equal(rp->rcb_laddr, rh->raw_dst))
1135612Swnj 			continue;
1146509Ssam 		if ((rp->rcb_flags & RAW_FADDR) &&
1156529Ssam 		    !equal(rp->rcb_faddr, rh->raw_src))
1166509Ssam 			continue;
1175612Swnj 		if (last) {
1185612Swnj 			struct mbuf *n;
11921531Skarels 			if (n = m_copy(m->m_next, 0, (int)M_COPYALL)) {
12021531Skarels 				if (sbappendaddr(&last->so_rcv, &rh->raw_src,
12121531Skarels 				    n, (struct mbuf *)0) == 0)
12221531Skarels 					/* should notify about lost packet */
12321531Skarels 					m_freem(n);
12421531Skarels 				else
12521531Skarels 					sorwakeup(last);
1265646Ssam 			}
1275612Swnj 		}
1286509Ssam 		last = rp->rcb_socket;
1295612Swnj 	}
1306584Ssam 	if (last) {
13112783Ssam 		if (sbappendaddr(&last->so_rcv, &rh->raw_src,
13221531Skarels 		    m->m_next, (struct mbuf *)0) == 0)
13321531Skarels 			m_freem(m->m_next);
13421531Skarels 		else
13521531Skarels 			sorwakeup(last);
13621531Skarels 		(void) m_free(m);		/* header */
13721531Skarels 	} else
13821531Skarels 		m_freem(m);
1395121Swnj 	goto next;
1405121Swnj }
1415121Swnj 
1428636Sroot /*ARGSUSED*/
1436584Ssam raw_ctlinput(cmd, arg)
1446584Ssam 	int cmd;
1456584Ssam 	caddr_t arg;
1466584Ssam {
1476591Ssam 
1486591Ssam 	if (cmd < 0 || cmd > PRC_NCMDS)
1496591Ssam 		return;
1508636Sroot 	/* INCOMPLETE */
1516584Ssam }
1526584Ssam 
1535121Swnj /*ARGSUSED*/
15412783Ssam raw_usrreq(so, req, m, nam, rights)
1555121Swnj 	struct socket *so;
1565121Swnj 	int req;
15712783Ssam 	struct mbuf *m, *nam, *rights;
1585121Swnj {
1595612Swnj 	register struct rawcb *rp = sotorawcb(so);
16012783Ssam 	register int error = 0;
1615121Swnj 
16212783Ssam 	if (rights && rights->m_len) {
16312783Ssam 		error = EOPNOTSUPP;
16412783Ssam 		goto release;
16512783Ssam 	}
16612783Ssam 	if (rp == 0 && req != PRU_ATTACH) {
16712783Ssam 		error = EINVAL;
16812783Ssam 		goto release;
16912783Ssam 	}
1705612Swnj 	switch (req) {
1715612Swnj 
1725612Swnj 	/*
1735612Swnj 	 * Allocate a raw control block and fill in the
1745612Swnj 	 * necessary info to allow packets to be routed to
1755612Swnj 	 * the appropriate raw interface routine.
1765612Swnj 	 */
1775612Swnj 	case PRU_ATTACH:
17812783Ssam 		if ((so->so_state & SS_PRIV) == 0) {
17912783Ssam 			error = EACCES;
18013116Ssam 			break;
18112783Ssam 		}
18212783Ssam 		if (rp) {
18312783Ssam 			error = EINVAL;
18413116Ssam 			break;
18512783Ssam 		}
18621769Skarels 		error = raw_attach(so, (int)nam);
1875612Swnj 		break;
1885612Swnj 
1895612Swnj 	/*
1905612Swnj 	 * Destroy state just before socket deallocation.
1915612Swnj 	 * Flush data or not depending on the options.
1925612Swnj 	 */
1935612Swnj 	case PRU_DETACH:
19412783Ssam 		if (rp == 0) {
19512783Ssam 			error = ENOTCONN;
19613116Ssam 			break;
19712783Ssam 		}
1985612Swnj 		raw_detach(rp);
1995612Swnj 		break;
2005612Swnj 
2015612Swnj 	/*
2025612Swnj 	 * If a socket isn't bound to a single address,
2035612Swnj 	 * the raw input routine will hand it anything
2045612Swnj 	 * within that protocol family (assuming there's
2055612Swnj 	 * nothing else around it should go to).
2065612Swnj 	 */
2075612Swnj 	case PRU_CONNECT:
20812783Ssam 		if (rp->rcb_flags & RAW_FADDR) {
20912783Ssam 			error = EISCONN;
21013116Ssam 			break;
21112783Ssam 		}
2128395Swnj 		raw_connaddr(rp, nam);
2135612Swnj 		soisconnected(so);
2145612Swnj 		break;
2155612Swnj 
21613116Ssam 	case PRU_CONNECT2:
21713116Ssam 		error = EOPNOTSUPP;
21813116Ssam 		goto release;
21913116Ssam 
22012783Ssam 	case PRU_BIND:
22112783Ssam 		if (rp->rcb_flags & RAW_LADDR) {
22212783Ssam 			error = EINVAL;			/* XXX */
22313116Ssam 			break;
22412783Ssam 		}
22512783Ssam 		error = raw_bind(so, nam);
22612783Ssam 		break;
22712783Ssam 
2285612Swnj 	case PRU_DISCONNECT:
22912783Ssam 		if ((rp->rcb_flags & RAW_FADDR) == 0) {
23012783Ssam 			error = ENOTCONN;
23113116Ssam 			break;
23212783Ssam 		}
23313451Ssam 		if (rp->rcb_route.ro_rt)
23413451Ssam 			rtfree(rp->rcb_route.ro_rt);
2355612Swnj 		raw_disconnect(rp);
2365612Swnj 		soisdisconnected(so);
2375612Swnj 		break;
2385612Swnj 
2395612Swnj 	/*
2405612Swnj 	 * Mark the connection as being incapable of further input.
2415612Swnj 	 */
2425612Swnj 	case PRU_SHUTDOWN:
2435612Swnj 		socantsendmore(so);
2445612Swnj 		break;
2455612Swnj 
2465612Swnj 	/*
2475612Swnj 	 * Ship a packet out.  The appropriate raw output
2485612Swnj 	 * routine handles any massaging necessary.
2495612Swnj 	 */
2505612Swnj 	case PRU_SEND:
2518395Swnj 		if (nam) {
25212783Ssam 			if (rp->rcb_flags & RAW_FADDR) {
25312783Ssam 				error = EISCONN;
25413116Ssam 				break;
25512783Ssam 			}
2568395Swnj 			raw_connaddr(rp, nam);
25712783Ssam 		} else if ((rp->rcb_flags & RAW_FADDR) == 0) {
25812783Ssam 			error = ENOTCONN;
25913116Ssam 			break;
26012783Ssam 		}
26113451Ssam 		/*
26213451Ssam 		 * Check for routing.  If new foreign address, or
26313451Ssam 		 * no route presently in use, try to allocate new
26413451Ssam 		 * route.  On failure, just hand packet to output
26513451Ssam 		 * routine anyway in case it can handle it.
26613451Ssam 		 */
26713451Ssam 		if ((rp->rcb_flags & RAW_DONTROUTE) == 0)
26813451Ssam 			if (!equal(rp->rcb_faddr, rp->rcb_route.ro_dst) ||
26913451Ssam 			    rp->rcb_route.ro_rt == 0) {
27016773Sbloom 				if (rp->rcb_route.ro_rt) {
27116773Sbloom 					RTFREE(rp->rcb_route.ro_rt);
27216773Sbloom 					rp->rcb_route.ro_rt = NULL;
27316773Sbloom 				}
27413451Ssam 				rp->rcb_route.ro_dst = rp->rcb_faddr;
27513451Ssam 				rtalloc(&rp->rcb_route);
27613451Ssam 			}
2776505Ssam 		error = (*so->so_proto->pr_output)(m, so);
27812783Ssam 		m = NULL;
2798395Swnj 		if (nam)
2806509Ssam 			rp->rcb_flags &= ~RAW_FADDR;
2815612Swnj 		break;
2825612Swnj 
2835612Swnj 	case PRU_ABORT:
2845612Swnj 		raw_disconnect(rp);
2855612Swnj 		sofree(so);
2865612Swnj 		soisdisconnected(so);
2875612Swnj 		break;
2885612Swnj 
28916974Skarels 	case PRU_SENSE:
29016974Skarels 		/*
29116974Skarels 		 * stat: don't bother with a blocksize.
29216974Skarels 		 */
29316974Skarels 		return (0);
29416974Skarels 
2955612Swnj 	/*
2965612Swnj 	 * Not supported.
2975612Swnj 	 */
29816773Sbloom 	case PRU_CONTROL:
29916773Sbloom 	case PRU_RCVOOB:
3005612Swnj 	case PRU_RCVD:
30116773Sbloom 		return(EOPNOTSUPP);
30216773Sbloom 
303*25221Skarels 	case PRU_LISTEN:
30416773Sbloom 	case PRU_ACCEPT:
3055612Swnj 	case PRU_SENDOOB:
3065612Swnj 		error = EOPNOTSUPP;
3075612Swnj 		break;
3085612Swnj 
3096509Ssam 	case PRU_SOCKADDR:
3108723Sroot 		bcopy((caddr_t)&rp->rcb_laddr, mtod(nam, caddr_t),
3118395Swnj 		    sizeof (struct sockaddr));
3128395Swnj 		nam->m_len = sizeof (struct sockaddr);
3136509Ssam 		break;
3146509Ssam 
31514122Ssam 	case PRU_PEERADDR:
31614122Ssam 		bcopy((caddr_t)&rp->rcb_faddr, mtod(nam, caddr_t),
31714122Ssam 		    sizeof (struct sockaddr));
31814122Ssam 		nam->m_len = sizeof (struct sockaddr);
31914122Ssam 		break;
32014122Ssam 
3215612Swnj 	default:
3225612Swnj 		panic("raw_usrreq");
3235612Swnj 	}
32412783Ssam release:
32512783Ssam 	if (m != NULL)
32612783Ssam 		m_freem(m);
3275612Swnj 	return (error);
3285121Swnj }
329