xref: /csrg-svn/sys/net/raw_cb.c (revision 7517)
1 /*	raw_cb.c	4.11	82/07/24	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/mbuf.h"
6 #include "../h/socket.h"
7 #include "../h/socketvar.h"
8 #include "../h/mtpr.h"
9 #include "../net/in.h"
10 #include "../net/in_systm.h"
11 #include "../net/if.h"
12 #include "../net/raw_cb.h"
13 #include "../net/pup.h"
14 #include <errno.h>
15 
16 /*
17  * Routines to manage the raw protocol control blocks.
18  *
19  * TODO:
20  *	hash lookups by protocol family/protocol + address family
21  *	take care of unique address problems per AF?
22  *	redo address binding to allow wildcards
23  */
24 
25 /*
26  * Allocate a control block and a nominal amount
27  * of buffer space for the socket.
28  */
29 raw_attach(so, addr)
30 	register struct socket *so;
31 	struct sockaddr *addr;
32 {
33 	struct mbuf *m;
34 	register struct rawcb *rp;
35 
36 	if (ifnet == 0)
37 		return (EADDRNOTAVAIL);
38 	/*
39 	 * Should we verify address not already in use?
40 	 * Some say yes, others no.
41 	 */
42 	if (addr) switch (addr->sa_family) {
43 
44 	case AF_IMPLINK:
45 	case AF_INET:
46 		if (((struct sockaddr_in *)addr)->sin_addr.s_addr &&
47 		    if_ifwithaddr(addr) == 0)
48 			return (EADDRNOTAVAIL);
49 		break;
50 
51 #ifdef PUP
52 	/*
53 	 * Curious, we convert PUP address format to internet
54 	 * to allow us to verify we're asking for an Ethernet
55 	 * interface.  This is wrong, but things are heavily
56 	 * oriented towards the internet addressing scheme, and
57 	 * converting internet to PUP would be very expensive.
58 	 */
59 	case AF_PUP: {
60 		struct sockaddr_pup *spup = (struct sockaddr_pup *)addr;
61 		struct sockaddr_in inpup;
62 
63 		bzero((caddr_t)&inpup, sizeof(inpup));
64 		inpup.sin_family = AF_INET;
65 		inpup.sin_addr.s_net = spup->sp_net;
66 		inpup.sin_addr.s_impno = spup->sp_host;
67 		if (inpup.sin_addr.s_addr &&
68 		    if_ifwithaddr((struct sockaddr *)&inpup) == 0)
69 			return (EADDRNOTAVAIL);
70 		break;
71 	}
72 #endif
73 
74 	default:
75 		return (EAFNOSUPPORT);
76 	}
77 	m = m_getclr(M_DONTWAIT);
78 	if (m == 0)
79 		return (ENOBUFS);
80 	if (sbreserve(&so->so_snd, RAWSNDQ) == 0)
81 		goto bad;
82 	if (sbreserve(&so->so_rcv, RAWRCVQ) == 0)
83 		goto bad2;
84 	rp = mtod(m, struct rawcb *);
85 	rp->rcb_socket = so;
86 	insque(rp, &rawcb);
87 	so->so_pcb = (caddr_t)rp;
88 	rp->rcb_pcb = 0;
89 	if (addr) {
90 		bcopy((caddr_t)addr, (caddr_t)&rp->rcb_laddr, sizeof(*addr));
91 		rp->rcb_flags |= RAW_LADDR;
92 	}
93 	return (0);
94 bad2:
95 	sbrelease(&so->so_snd);
96 bad:
97 	(void) m_free(m);
98 	return (ENOBUFS);
99 }
100 
101 /*
102  * Detach the raw connection block and discard
103  * socket resources.
104  */
105 raw_detach(rp)
106 	register struct rawcb *rp;
107 {
108 	struct socket *so = rp->rcb_socket;
109 
110 	so->so_pcb = 0;
111 	sofree(so);
112 	remque(rp);
113 	(void) m_freem(dtom(rp));
114 }
115 
116 /*
117  * Disconnect and possibly release resources.
118  */
119 raw_disconnect(rp)
120 	struct rawcb *rp;
121 {
122 	rp->rcb_flags &= ~RAW_FADDR;
123 	if (rp->rcb_socket->so_state & SS_NOFDREF)
124 		raw_detach(rp);
125 }
126 
127 /*
128  * Associate a peer's address with a
129  * raw connection block.
130  */
131 raw_connaddr(rp, addr)
132 	struct rawcb *rp;
133 	struct sockaddr *addr;
134 {
135 	bcopy((caddr_t)addr, (caddr_t)&rp->rcb_faddr, sizeof(*addr));
136 	rp->rcb_flags |= RAW_FADDR;
137 }
138