xref: /csrg-svn/sys/net/raw_cb.c (revision 23160)
1*23160Smckusick /*
2*23160Smckusick  * Copyright (c) 1980 Regents of the University of California.
3*23160Smckusick  * All rights reserved.  The Berkeley software License Agreement
4*23160Smckusick  * specifies the terms and conditions for redistribution.
5*23160Smckusick  *
6*23160Smckusick  *	@(#)raw_cb.c	6.6 (Berkeley) 06/08/85
7*23160Smckusick  */
85635Sroot 
917066Sbloom #include "param.h"
1017066Sbloom #include "systm.h"
1117066Sbloom #include "mbuf.h"
1217066Sbloom #include "socket.h"
1317066Sbloom #include "socketvar.h"
1421769Skarels #include "domain.h"
1521769Skarels #include "protosw.h"
1617066Sbloom #include "errno.h"
1710890Ssam 
1817066Sbloom #include "if.h"
1917066Sbloom #include "route.h"
2017066Sbloom #include "raw_cb.h"
2112783Ssam #include "../netinet/in.h"
2219948Sbloom #ifdef PUP
2312783Ssam #include "../netpup/pup.h"
2419948Sbloom #endif
255635Sroot 
2610890Ssam #include "../vax/mtpr.h"
2710890Ssam 
285635Sroot /*
295635Sroot  * Routines to manage the raw protocol control blocks.
305635Sroot  *
315635Sroot  * TODO:
325635Sroot  *	hash lookups by protocol family/protocol + address family
336339Ssam  *	take care of unique address problems per AF?
346045Swnj  *	redo address binding to allow wildcards
355635Sroot  */
365635Sroot 
375635Sroot /*
385635Sroot  * Allocate a control block and a nominal amount
395635Sroot  * of buffer space for the socket.
405635Sroot  */
4121769Skarels raw_attach(so, proto)
425635Sroot 	register struct socket *so;
4321769Skarels 	int proto;
445635Sroot {
455635Sroot 	struct mbuf *m;
465635Sroot 	register struct rawcb *rp;
475635Sroot 
489638Ssam 	m = m_getclr(M_DONTWAIT, MT_PCB);
495635Sroot 	if (m == 0)
505635Sroot 		return (ENOBUFS);
515635Sroot 	if (sbreserve(&so->so_snd, RAWSNDQ) == 0)
525635Sroot 		goto bad;
535635Sroot 	if (sbreserve(&so->so_rcv, RAWRCVQ) == 0)
545635Sroot 		goto bad2;
555635Sroot 	rp = mtod(m, struct rawcb *);
565635Sroot 	rp->rcb_socket = so;
575635Sroot 	so->so_pcb = (caddr_t)rp;
585635Sroot 	rp->rcb_pcb = 0;
5921769Skarels 	rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family;
6021769Skarels 	rp->rcb_proto.sp_protocol = proto;
6121769Skarels 	insque(rp, &rawcb);
625635Sroot 	return (0);
635635Sroot bad2:
645635Sroot 	sbrelease(&so->so_snd);
655635Sroot bad:
665635Sroot 	(void) m_free(m);
675635Sroot 	return (ENOBUFS);
685635Sroot }
695635Sroot 
705635Sroot /*
715635Sroot  * Detach the raw connection block and discard
725635Sroot  * socket resources.
735635Sroot  */
745635Sroot raw_detach(rp)
755635Sroot 	register struct rawcb *rp;
765635Sroot {
775635Sroot 	struct socket *so = rp->rcb_socket;
785635Sroot 
795635Sroot 	so->so_pcb = 0;
805635Sroot 	sofree(so);
815635Sroot 	remque(rp);
828975Sroot 	m_freem(dtom(rp));
835635Sroot }
845635Sroot 
855635Sroot /*
865635Sroot  * Disconnect and possibly release resources.
875635Sroot  */
885635Sroot raw_disconnect(rp)
895635Sroot 	struct rawcb *rp;
905635Sroot {
918975Sroot 
926509Ssam 	rp->rcb_flags &= ~RAW_FADDR;
937517Sroot 	if (rp->rcb_socket->so_state & SS_NOFDREF)
945635Sroot 		raw_detach(rp);
955635Sroot }
965635Sroot 
978394Swnj raw_bind(so, nam)
988394Swnj 	register struct socket *so;
998394Swnj 	struct mbuf *nam;
1008394Swnj {
1018394Swnj 	struct sockaddr *addr = mtod(nam, struct sockaddr *);
1028394Swnj 	register struct rawcb *rp;
1038394Swnj 
1048394Swnj 	if (ifnet == 0)
1058394Swnj 		return (EADDRNOTAVAIL);
1068394Swnj /* BEGIN DUBIOUS */
1078394Swnj 	/*
1088394Swnj 	 * Should we verify address not already in use?
1098394Swnj 	 * Some say yes, others no.
1108394Swnj 	 */
1118394Swnj 	switch (addr->sa_family) {
1128394Swnj 
11312783Ssam #ifdef INET
1148394Swnj 	case AF_IMPLINK:
11512783Ssam 	case AF_INET: {
1168394Swnj 		if (((struct sockaddr_in *)addr)->sin_addr.s_addr &&
11718409Skarels 		    ifa_ifwithaddr(addr) == 0)
1188394Swnj 			return (EADDRNOTAVAIL);
1198394Swnj 		break;
12012783Ssam 	}
12112783Ssam #endif
1228394Swnj 
1238394Swnj #ifdef PUP
1248394Swnj 	/*
1258394Swnj 	 * Curious, we convert PUP address format to internet
1268394Swnj 	 * to allow us to verify we're asking for an Ethernet
1278394Swnj 	 * interface.  This is wrong, but things are heavily
1288394Swnj 	 * oriented towards the internet addressing scheme, and
1298394Swnj 	 * converting internet to PUP would be very expensive.
1308394Swnj 	 */
1318394Swnj 	case AF_PUP: {
1328394Swnj 		struct sockaddr_pup *spup = (struct sockaddr_pup *)addr;
1338394Swnj 		struct sockaddr_in inpup;
1348394Swnj 
1359184Ssam 		bzero((caddr_t)&inpup, (unsigned)sizeof(inpup));
1368394Swnj 		inpup.sin_family = AF_INET;
13718409Skarels 		inpup.sin_addr = in_makeaddr(spup->spup_net, spup->spup_host);
1388394Swnj 		if (inpup.sin_addr.s_addr &&
13918409Skarels 		    ifa_ifwithaddr((struct sockaddr *)&inpup) == 0)
1408394Swnj 			return (EADDRNOTAVAIL);
1418394Swnj 		break;
1428394Swnj 	}
1438394Swnj #endif
1448394Swnj 
1458394Swnj 	default:
1468394Swnj 		return (EAFNOSUPPORT);
1478394Swnj 	}
1488394Swnj /* END DUBIOUS */
1499184Ssam 	rp = sotorawcb(so);
1508394Swnj 	bcopy((caddr_t)addr, (caddr_t)&rp->rcb_laddr, sizeof (*addr));
1518394Swnj 	rp->rcb_flags |= RAW_LADDR;
1528394Swnj 	return (0);
1538394Swnj }
1548394Swnj 
1555635Sroot /*
1565635Sroot  * Associate a peer's address with a
1575635Sroot  * raw connection block.
1585635Sroot  */
1598394Swnj raw_connaddr(rp, nam)
1605635Sroot 	struct rawcb *rp;
1618394Swnj 	struct mbuf *nam;
1625635Sroot {
1638394Swnj 	struct sockaddr *addr = mtod(nam, struct sockaddr *);
1648394Swnj 
1656509Ssam 	bcopy((caddr_t)addr, (caddr_t)&rp->rcb_faddr, sizeof(*addr));
1666509Ssam 	rp->rcb_flags |= RAW_FADDR;
1675635Sroot }
168