xref: /csrg-svn/sys/netinet/in_pcb.c (revision 5240)
1 /* in_pcb.c 4.13 81/12/11 */
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/dir.h"
6 #include "../h/user.h"
7 #include "../h/mbuf.h"
8 #include "../h/socket.h"
9 #include "../h/socketvar.h"
10 #include "../net/in.h"
11 #include "../net/in_systm.h"
12 #include "../net/if.h"
13 #include "../net/in_pcb.h"
14 
15 /*
16  * Routines to manage internet protocol control blocks.
17  *
18  * At PRU_ATTACH time a protocol control block is allocated in
19  * in_pcballoc() and inserted on a doubly-linked list of such blocks
20  * for the protocol.  A port address is either requested (and verified
21  * to not be in use) or assigned at this time.  We also allocate
22  * space in the socket sockbuf structures here, although this is
23  * not a clearly correct place to put this function.
24  *
25  * A connectionless protocol will have its protocol control block
26  * removed at PRU_DETACH time, when the socket will be freed (freeing
27  * the space reserved) and the block will be removed from the list of
28  * blocks for its protocol.
29  *
30  * A connection-based protocol may be connected to a remote peer at
31  * PRU_CONNECT time through the routine in_pcbconnect().  In the normal
32  * case a PRU_DISCONNECT occurs causing a in_pcbdisconnect().
33  * It is also possible that higher-level routines will opt out of the
34  * relationship with the connection before the connection shut down
35  * is complete.  This often occurs in protocols like TCP where we must
36  * hold on to the protocol control block for a unreasonably long time
37  * after the connection is used up to avoid races in later connection
38  * establishment.  To handle this we allow higher-level routines to
39  * disassociate themselves from the socket, marking it SS_USERGONE while
40  * the disconnect is in progress.  We notice that this has happened
41  * when the disconnect is complete, and perform the PRU_DETACH operation,
42  * freeing the socket.
43  *
44  * TODO:
45  *	use hashing
46  */
47 struct	in_addr zeroin_addr;
48 
49 /*
50  * Allocate a protocol control block, space
51  * for send and receive data, and local host information.
52  * Return error.  If no error make socket point at pcb.
53  */
54 in_pcbattach(so, head, sndcc, rcvcc, sin)
55 	struct socket *so;
56 	struct inpcb *head;
57 	int sndcc, rcvcc;
58 	struct sockaddr_in *sin;
59 {
60 	struct mbuf *m;
61 	register struct inpcb *inp;
62 	struct ifnet *ifp;
63 	u_short lport;
64 
65 COUNT(IN_PCBATTACH);
66 	if (sin) {
67 		if (sin->sin_family != AF_INET)
68 			return (EAFNOSUPPORT);
69 		ifp = if_ifwithaddr(sin->sin_addr);
70 		if (ifp == 0)
71 			return (EADDRNOTAVAIL);
72 		lport = sin->sin_port;
73 		if (lport &&
74 		    in_pcblookup(head, zeroin_addr, 0, sin->sin_addr, lport))
75 			return (EADDRINUSE);
76 	} else {
77 		ifp = if_ifwithaddr(ifnet->if_addr);
78 		lport = 0;
79 	}
80 	m = m_getclr(0);
81 	if (m == 0)
82 		return (ENOBUFS);
83 	if (sbreserve(&so->so_snd, sndcc) == 0)
84 		goto bad;
85 	if (sbreserve(&so->so_rcv, rcvcc) == 0)
86 		goto bad2;
87 	inp = mtod(m, struct inpcb *);
88 	inp->inp_head = head;
89 	inp->inp_laddr = ifp->if_addr;
90 	if (lport == 0)
91 		do {
92 			if (head->inp_lport++ < 1024)
93 				head->inp_lport = 1024;
94 			lport = htons(head->inp_lport);
95 		} while (in_pcblookup(head, zeroin_addr, 0, inp->inp_laddr, lport));
96 	inp->inp_lport = lport;
97 	inp->inp_socket = so;
98 	insque(inp, head);
99 	so->so_pcb = (caddr_t)inp;
100 	sin = (struct sockaddr_in *)&so->so_addr;
101 	sin->sin_family = AF_INET;
102 	sin->sin_addr = inp->inp_laddr;
103 	sin->sin_port = inp->inp_lport;
104 	return (0);
105 bad2:
106 	sbrelease(&so->so_snd);
107 bad:
108 	(void) m_free(m);
109 	return (ENOBUFS);
110 }
111 
112 in_pcbconnect(inp, sin)
113 	struct inpcb *inp;
114 	struct sockaddr_in *sin;
115 {
116 	struct inpcb *xp;
117 
118 COUNT(IN_PCBCONNECT);
119 	if (sin->sin_family != AF_INET)
120 		return (EAFNOSUPPORT);
121 	if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0)
122 		return (EADDRNOTAVAIL);
123 	xp = in_pcblookup(inp->inp_head, sin->sin_addr, sin->sin_port, inp->inp_laddr, inp->inp_lport);
124 	if (xp->inp_faddr.s_addr)
125 		return (EADDRINUSE);
126 	inp->inp_faddr = sin->sin_addr;
127 	inp->inp_fport = sin->sin_port;
128 	return (0);
129 }
130 
131 in_pcbdisconnect(inp)
132 	struct inpcb *inp;
133 {
134 
135 COUNT(IN_PCBDISCONNECT);
136 	inp->inp_faddr.s_addr = 0;
137 	if (inp->inp_socket->so_state & SS_USERGONE)
138 		in_pcbdetach(inp);
139 }
140 
141 in_pcbdetach(inp)
142 	struct inpcb *inp;
143 {
144 	struct socket *so = inp->inp_socket;
145 
146 	so->so_pcb = 0;
147 	sofree(so);
148 	remque(inp);
149 	(void) m_free(dtom(inp));
150 }
151 
152 /*
153  * Look for a control block to accept a segment.
154  * First choice is an exact address match.
155  * Second choice is a match of local address, with
156  * unspecified foreign address.
157  */
158 struct inpcb *
159 in_pcblookup(head, faddr, fport, laddr, lport)
160 	struct inpcb *head;
161 	struct in_addr faddr, laddr;
162 	u_short fport, lport;
163 {
164 	register struct inpcb *inp;
165 	struct inpcb *match = 0;
166 
167 	for (inp = head->inp_next; inp != head; inp = inp->inp_next) {
168 		if (inp->inp_laddr.s_addr != laddr.s_addr ||
169 		    inp->inp_lport != lport)
170 			continue;
171 		if (inp->inp_faddr.s_addr == 0) {
172 			match = inp;
173 			continue;
174 		}
175 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
176 		    inp->inp_fport == fport)
177 			return (inp);
178 	}
179 	return (match);
180 }
181