xref: /csrg-svn/sys/kern/uipc_socket.c (revision 4979)
1 /*	uipc_socket.c	4.9	81/11/21	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/dir.h"
6 #include "../h/user.h"
7 #include "../h/proc.h"
8 #include "../h/file.h"
9 #include "../h/inode.h"
10 #include "../h/buf.h"
11 #include "../h/mbuf.h"
12 #include "../h/protosw.h"
13 #include "../h/socket.h"
14 #include "../h/socketvar.h"
15 #include "../h/stat.h"
16 #include "../net/inet.h"
17 #include "../net/inet_systm.h"
18 
19 /*
20  * Socket support routines.
21  *
22  * DEAL WITH INTERRUPT NOTIFICATION.
23  */
24 
25 /*
26  * Create a socket.
27  */
28 socreate(aso, type, asp, asa, options)
29 	struct socket **aso;
30 	int type;
31 	struct sockproto *asp;
32 	struct sockaddr *asa;
33 	int options;
34 {
35 	register struct protosw *prp;
36 	register struct socket *so;
37 	struct mbuf *m;
38 	int pf, proto, error;
39 COUNT(SOCREATE);
40 
41 	/*
42 	 * Use process standard protocol/protocol family if none
43 	 * specified by address argument.
44 	 */
45 	if (asp == 0) {
46 		pf = PF_INET;		/* should be u.u_protof */
47 		proto = 0;
48 	} else {
49 		pf = asp->sp_family;
50 		proto = asp->sp_protocol;
51 	}
52 
53 	/*
54 	 * If protocol specified, look for it, otherwise
55 	 * for a protocol of the correct type in the right family.
56 	 */
57 	if (proto)
58 		prp = pffindproto(pf, proto);
59 	else
60 		prp = pffindtype(pf, type);
61 	if (prp == 0)
62 		return (EPROTONOSUPPORT);
63 
64 	/*
65 	 * Get a socket structure.
66 	 */
67 	m = m_getclr(M_WAIT);
68 	if (m == 0)
69 		return (ENOBUFS);
70 	so = mtod(m, struct socket *);
71 	so->so_options = options;
72 
73 	/*
74 	 * Attach protocol to socket, initializing
75 	 * and reserving resources.
76 	 */
77 	so->so_proto = prp;
78 	error = (*prp->pr_usrreq)(so, PRU_ATTACH, 0, asa);
79 	if (error) {
80 		(void) m_free(dtom(so));
81 		return (error);
82 	}
83 	*aso = so;
84 	return (0);
85 }
86 
87 sofree(so)
88 	struct socket *so;
89 {
90 
91 COUNT(SOFREE);
92 	if (so->so_pcb || (so->so_state & SS_USERGONE) == 0)
93 		return;
94 	sbrelease(&so->so_snd);
95 	sbrelease(&so->so_rcv);
96 	(void) m_free(dtom(so));
97 }
98 
99 /*
100  * Close a socket on last file table reference removal.
101  * Initiate disconnect if connected.
102  * Free socket when disconnect complete.
103  */
104 soclose(so)
105 	register struct socket *so;
106 {
107 	int s = splnet();		/* conservative */
108 
109 COUNT(SOCLOSE);
110 	if (so->so_pcb == 0)
111 		goto discard;
112 	if (so->so_state & SS_ISCONNECTED) {
113 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
114 			u.u_error = sodisconnect(so, (struct sockaddr *)0);
115 			if (u.u_error) {
116 				splx(s);
117 				return;
118 			}
119 		}
120 		if ((so->so_state & SS_ISDISCONNECTING) &&
121 		    (so->so_options & SO_NBIO)) {
122 			u.u_error = EINPROGRESS;
123 			splx(s);
124 			return;
125 		}
126 		while (so->so_state & SS_ISCONNECTED)
127 			sleep((caddr_t)&so->so_timeo, PZERO+1);
128 	}
129 	u.u_error = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, 0, 0);
130 discard:
131 	so->so_state |= SS_USERGONE;
132 	sofree(so);
133 	splx(s);
134 }
135 
136 sosplice(pso, so)
137 	struct socket *pso, *so;
138 {
139 
140 COUNT(SOSPLICE);
141 	if (pso->so_proto->pr_family != PF_LOCAL) {
142 		struct socket *tso;
143 		tso = pso; pso = so; so = tso;
144 	}
145 	if (pso->so_proto->pr_family != PF_LOCAL)
146 		return (EOPNOTSUPP);
147 	/* check types and buffer space */
148 	/* merge buffers */
149 	return (0);
150 }
151 
152 /*ARGSUSED*/
153 sostat(so, sb)
154 	struct socket *so;
155 	struct stat *sb;
156 {
157 
158 COUNT(SOSTAT);
159 	return (EOPNOTSUPP);
160 }
161 
162 /*
163  * Accept connection on a socket.
164  */
165 soaccept(so, asa)
166 	struct socket *so;
167 	struct sockaddr *asa;
168 {
169 	int s = splnet();
170 	int error;
171 
172 COUNT(SOACCEPT);
173 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
174 		error = EISCONN;
175 		goto bad;
176 	}
177 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
178 		error = EINVAL;			/* XXX */
179 		goto bad;
180 	}
181 	error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, 0, (caddr_t)asa);
182 bad:
183 	splx(s);
184 	return (error);
185 }
186 
187 /*
188  * Connect socket to a specified address.
189  * If already connected or connecting, then avoid
190  * the protocol entry, to keep its job simpler.
191  */
192 soconnect(so, asa)
193 	struct socket *so;
194 	struct sockaddr *asa;
195 {
196 	int s = splnet();
197 	int error;
198 
199 COUNT(SOCONNECT);
200 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
201 		error = EISCONN;
202 		goto bad;
203 	}
204 	error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, 0, (caddr_t)asa);
205 bad:
206 	splx(s);
207 	return (error);
208 }
209 
210 /*
211  * Disconnect from a socket.
212  * Address parameter is from system call for later multicast
213  * protocols.  Check to make sure that connected and no disconnect
214  * in progress (for protocol's sake), and then invoke protocol.
215  */
216 sodisconnect(so, asa)
217 	struct socket *so;
218 	struct sockaddr *asa;
219 {
220 	int s = splnet();
221 	int error;
222 
223 COUNT(SODISCONNECT);
224 	if ((so->so_state & SS_ISCONNECTED) == 0) {
225 		error = ENOTCONN;
226 		goto bad;
227 	}
228 	if (so->so_state & SS_ISDISCONNECTING) {
229 		error = EALREADY;
230 		goto bad;
231 	}
232 	error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, 0, asa);
233 bad:
234 	splx(s);
235 	return (error);
236 }
237 
238 /*
239  * Send on a socket.
240  * If send must go all at once and message is larger than
241  * send buffering, then hard error.
242  * Lock against other senders.
243  * If must go all at once and not enough room now, then
244  * inform user that this would block and do nothing.
245  */
246 sosend(so, asa)
247 	register struct socket *so;
248 	struct sockaddr *asa;
249 {
250 	struct mbuf *top = 0;
251 	register struct mbuf *m, **mp = ⊤
252 	register u_int len;
253 	int error = 0, space, s;
254 
255 COUNT(SOSEND);
256 	if (so->so_state & SS_CANTSENDMORE)
257 		return (EPIPE);
258 	if (sosendallatonce(so) && u.u_count > so->so_snd.sb_hiwat)
259 		return (EMSGSIZE);
260 	if ((so->so_snd.sb_flags & SB_LOCK) && (so->so_options & SO_NBIO))
261 		return (EWOULDBLOCK);
262 	sblock(&so->so_snd);
263 #define	snderr(errno)	{ error = errno; splx(s); goto release; }
264 
265 	s = splnet();
266 again:
267 	if ((so->so_state & SS_ISCONNECTED) == 0) {
268 		if (so->so_proto->pr_flags & PR_CONNREQUIRED)
269 			snderr(ENOTCONN);
270 		if (asa == 0)
271 			snderr(EDESTADDRREQ);
272 	}
273 	if (so->so_error)
274 		snderr(so->so_error);
275 	if (top) {
276 		error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, top, asa);
277 		if (error) {
278 			splx(s);
279 			goto release;
280 		}
281 		top = 0;
282 		mp = ⊤
283 	}
284 	if (u.u_count == 0) {
285 		splx(s);
286 		goto release;
287 	}
288 	if (sosendallatonce(so) && sbspace(&so->so_snd) < u.u_count) {
289 		if (so->so_options & SO_NBIO)
290 			snderr(EWOULDBLOCK);
291 		sbunlock(&so->so_snd);
292 		sbwait(&so->so_snd);
293 		splx(s);
294 		goto again;
295 	}
296 	splx(s);
297 	while (u.u_count && (space = sbspace(&so->so_snd)) > 0) {
298 		MGET(m, 1);
299 		if (m == NULL) {
300 			error = ENOBUFS;
301 			m_freem(top);
302 			goto release;
303 		}
304 		if (u.u_count >= PGSIZE && space >= NMBPG) {
305 			register struct mbuf *p;
306 			MPGET(p, 1);
307 			if (p == 0)
308 				goto nopages;
309 			m->m_off = (int)p - (int)m;
310 			len = PGSIZE;
311 		} else {
312 nopages:
313 			m->m_off = MMINOFF;
314 			len = MIN(MLEN, u.u_count);
315 		}
316 		iomove(mtod(m, caddr_t), len, B_WRITE);
317 		m->m_len = len;
318 		*mp = m;
319 		mp = &m->m_next;
320 	}
321 	s = splnet();
322 	goto again;
323 
324 release:
325 	sbunlock(&so->so_snd);
326 	return (error);
327 }
328 
329 soreceive(so, asa)
330 	register struct socket *so;
331 	struct sockaddr *asa;
332 {
333 	register struct mbuf *m, *n;
334 	u_int len;
335 	int eor, s, error = 0;
336 
337 COUNT(SORECEIVE);
338 restart:
339 	sblock(&so->so_rcv);
340 	s = splnet();
341 
342 #define	rcverr(errno)	{ error = errno; splx(s); goto release; }
343 	if (so->so_rcv.sb_cc == 0) {
344 		if ((so->so_state & SS_ISCONNECTED) == 0 &&
345 		    (so->so_proto->pr_flags & PR_CONNREQUIRED))
346 			rcverr(ENOTCONN);
347 		if (so->so_state & SS_CANTRCVMORE) {
348 			splx(s);
349 			goto release;
350 		}
351 		if (so->so_options & SO_NBIO)
352 			rcverr (EWOULDBLOCK);
353 		sbunlock(&so->so_rcv);
354 		sbwait(&so->so_rcv);
355 		goto restart;
356 	}
357 	m = so->so_rcv.sb_mb;
358 	if (m == 0)
359 		panic("receive");
360 	if ((so->so_proto->pr_flags & PR_ADDR)) {
361 		so->so_rcv.sb_mb = m->m_next;
362 		if (asa) {
363 			so->so_rcv.sb_cc -= m->m_len;
364 			len = MIN(m->m_len, sizeof (struct sockaddr));
365 			bcopy(mtod(m, caddr_t), (caddr_t)asa, len);
366 		} else
367 			bzero((caddr_t)asa, sizeof (*asa));
368 		m = so->so_rcv.sb_mb;
369 		if (m == 0)
370 			panic("receive 2");
371 	}
372 	eor = 0;
373 	do {
374 		len = MIN(m->m_len, u.u_count);
375 		if (len == m->m_len) {
376 			eor = (int)m->m_act;
377 			sbfree(&so->so_rcv, m);
378 		}
379 		splx(s);
380 		iomove(mtod(m, caddr_t), len, B_READ);
381 		s = splnet();
382 		if (len == m->m_len) {
383 			MFREE(m, n);
384 		} else {
385 			m->m_off += len;
386 			m->m_len -= len;
387 			so->so_rcv.sb_cc -= len;
388 		}
389 	} while ((m = so->so_rcv.sb_mb) && u.u_count && !eor);
390 	if ((so->so_proto->pr_flags & PR_ATOMIC) && eor == 0)
391 		do {
392 			if (m == 0)
393 				panic("receive 3");
394 			sbfree(&so->so_rcv, m);
395 			eor = (int)m->m_act;
396 			so->so_rcv.sb_mb = m->m_next;
397 			MFREE(m, n);
398 			m = n;
399 		} while (eor == 0);
400 	if ((so->so_proto->pr_flags & PR_WANTRCVD) && so->so_pcb)
401 		(*so->so_proto->pr_usrreq)(so, PRU_RCVD, 0, 0);
402 release:
403 	sbunlock(&so->so_rcv);
404 	splx(s);
405 	return (error);
406 }
407 
408 /*ARGSUSED*/
409 soioctl(so, cmd, cmdp)
410 	register struct socket *so;
411 	int cmd;
412 	register caddr_t cmdp;
413 {
414 
415 COUNT(SOIOCTL);
416 	switch (cmdp) {
417 
418 	}
419 	switch (so->so_type) {
420 
421 	case SOCK_STREAM:
422 		break;
423 
424 	case SOCK_DGRAM:
425 		break;
426 
427 	case SOCK_RDM:
428 		break;
429 
430 	case SOCK_RAW:
431 		break;
432 
433 	}
434 }
435