1 /* uipc_socket.c 4.16 81/12/02 */ 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/in.h" 17 #include "../net/in_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_UNIX) { 142 struct socket *tso; 143 tso = pso; pso = so; so = tso; 144 } 145 if (pso->so_proto->pr_family != PF_UNIX) 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_error) { 268 error = so->so_error; 269 so->so_error = 0; 270 splx(s); 271 goto release; 272 } 273 if ((so->so_state & SS_ISCONNECTED) == 0) { 274 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 275 snderr(ENOTCONN); 276 if (asa == 0) 277 snderr(EDESTADDRREQ); 278 } 279 if (top) { 280 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, top, asa); 281 if (error) { 282 splx(s); 283 goto release; 284 } 285 top = 0; 286 mp = ⊤ 287 } 288 if (u.u_count == 0) { 289 splx(s); 290 goto release; 291 } 292 space = sbspace(&so->so_snd); 293 if (space == 0 || sosendallatonce(so) && space < u.u_count) { 294 if (so->so_options & SO_NBIO) 295 snderr(EWOULDBLOCK); 296 sbunlock(&so->so_snd); 297 sbwait(&so->so_snd); 298 splx(s); 299 goto again; 300 } 301 splx(s); 302 while (u.u_count && space > 0) { 303 MGET(m, 1); 304 if (m == NULL) { 305 error = ENOBUFS; 306 m_freem(top); 307 goto release; 308 } 309 if (u.u_count >= CLBYTES && space >= CLBYTES) { 310 register struct mbuf *p; 311 MCLGET(p, 1); 312 if (p == 0) 313 goto nopages; 314 m->m_off = (int)p - (int)m; 315 len = CLBYTES; 316 } else { 317 nopages: 318 m->m_off = MMINOFF; 319 len = MIN(MLEN, u.u_count); 320 } 321 iomove(mtod(m, caddr_t), len, B_WRITE); 322 m->m_len = len; 323 *mp = m; 324 mp = &m->m_next; 325 space = sbspace(&so->so_snd); 326 } 327 s = splnet(); 328 goto again; 329 330 release: 331 sbunlock(&so->so_snd); 332 return (error); 333 } 334 335 soreceive(so, asa) 336 register struct socket *so; 337 struct sockaddr *asa; 338 { 339 register struct mbuf *m, *n; 340 u_int len; 341 int eor, s, error = 0; 342 343 COUNT(SORECEIVE); 344 restart: 345 sblock(&so->so_rcv); 346 s = splnet(); 347 348 #define rcverr(errno) { error = errno; splx(s); goto release; } 349 if (so->so_rcv.sb_cc == 0) { 350 if (so->so_error) { 351 error = so->so_error; 352 so->so_error = 0; 353 splx(s); 354 goto release; 355 } 356 if (so->so_state & SS_CANTRCVMORE) { 357 splx(s); 358 goto release; 359 } 360 if ((so->so_state & SS_ISCONNECTED) == 0 && 361 (so->so_proto->pr_flags & PR_CONNREQUIRED)) 362 rcverr(ENOTCONN); 363 if (so->so_options & SO_NBIO) 364 rcverr(EWOULDBLOCK); 365 sbunlock(&so->so_rcv); 366 sbwait(&so->so_rcv); 367 splx(s); 368 goto restart; 369 } 370 m = so->so_rcv.sb_mb; 371 if (m == 0) 372 panic("receive"); 373 if (so->so_proto->pr_flags & PR_ADDR) { 374 if (m->m_len != sizeof (struct sockaddr)) 375 panic("soreceive addr"); 376 if (asa) 377 bcopy(mtod(m, caddr_t), (caddr_t)asa, sizeof (*asa)); 378 so->so_rcv.sb_cc -= m->m_len; 379 so->so_rcv.sb_mbcnt -= MSIZE; 380 m = m_free(m); 381 if (m == 0) 382 panic("receive 2"); 383 so->so_rcv.sb_mb = m; 384 } 385 eor = 0; 386 do { 387 len = MIN(m->m_len, u.u_count); 388 if (len == m->m_len) { 389 eor = (int)m->m_act; 390 sbfree(&so->so_rcv, m); 391 } 392 splx(s); 393 iomove(mtod(m, caddr_t), len, B_READ); 394 s = splnet(); 395 if (len == m->m_len) { 396 MFREE(m, n); 397 so->so_rcv.sb_mb = n; 398 } else { 399 m->m_off += len; 400 m->m_len -= len; 401 so->so_rcv.sb_cc -= len; 402 } 403 } while ((m = so->so_rcv.sb_mb) && u.u_count && !eor); 404 if ((so->so_proto->pr_flags & PR_ATOMIC) && eor == 0) 405 do { 406 if (m == 0) 407 panic("receive 3"); 408 sbfree(&so->so_rcv, m); 409 eor = (int)m->m_act; 410 so->so_rcv.sb_mb = m->m_next; 411 MFREE(m, n); 412 m = n; 413 } while (eor == 0); 414 if ((so->so_proto->pr_flags & PR_WANTRCVD) && so->so_pcb) 415 (*so->so_proto->pr_usrreq)(so, PRU_RCVD, 0, 0); 416 release: 417 sbunlock(&so->so_rcv); 418 splx(s); 419 return (error); 420 } 421 422 /*ARGSUSED*/ 423 soioctl(so, cmd, cmdp) 424 register struct socket *so; 425 int cmd; 426 register caddr_t cmdp; 427 { 428 429 COUNT(SOIOCTL); 430 switch (cmdp) { 431 432 } 433 switch (so->so_type) { 434 435 case SOCK_STREAM: 436 break; 437 438 case SOCK_DGRAM: 439 break; 440 441 case SOCK_RDM: 442 break; 443 444 case SOCK_RAW: 445 break; 446 447 } 448 } 449