Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/kern/uipc_socket.c,v rcsdiff: /ftp/cvs/cvsroot/src/sys/kern/uipc_socket.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.215.4.2 retrieving revision 1.221 diff -u -p -r1.215.4.2 -r1.221 --- src/sys/kern/uipc_socket.c 2013/08/28 23:59:35 1.215.4.2 +++ src/sys/kern/uipc_socket.c 2014/02/25 18:30:11 1.221 @@ -1,4 +1,4 @@ -/* $NetBSD: uipc_socket.c,v 1.215.4.2 2013/08/28 23:59:35 rmind Exp $ */ +/* $NetBSD: uipc_socket.c,v 1.221 2014/02/25 18:30:11 pooka Exp $ */ /*- * Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc. @@ -62,16 +62,8 @@ * @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95 */ -/* - * Socket operation routines. - * - * These routines are called by the routines in sys_socket.c or from a - * system process, and implement the semantics of socket operations by - * switching out to the protocol specific routines. - */ - #include -__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.215.4.2 2013/08/28 23:59:35 rmind Exp $"); +__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.221 2014/02/25 18:30:11 pooka Exp $"); #include "opt_compat_netbsd.h" #include "opt_sock_counters.h" @@ -111,6 +103,7 @@ __KERNEL_RCSID(0, "$NetBSD: uipc_socket. #include #include +MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); MALLOC_DEFINE(M_SONAME, "soname", "socket name"); extern const struct fileops socketops; @@ -423,7 +416,7 @@ socket_listener_cb(kauth_cred_t cred, ka /* Normal users can only drop their own connections. */ struct socket *so = (struct socket *)arg1; - if (proc_uidmatch(cred, so->so_cred) == 0) + if (so->so_cred && proc_uidmatch(cred, so->so_cred) == 0) result = KAUTH_RESULT_ALLOW; break; @@ -486,11 +479,13 @@ soinit1(void) } /* - * socreate: create a new socket of the specified type and the protocol. - * - * => Caller may specify another socket for lock sharing (must not be held). - * => Returns the new socket without lock held. + * Socket operation routines. + * These routines are called by the routines in + * sys_socket.c or from a system process, and + * implement the semantics of socket operations by + * switching out to the protocol specific routines. */ +/*ARGSUSED*/ int socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l, struct socket *lockso) @@ -520,7 +515,7 @@ socreate(int dom, struct socket **aso, i return EPROTOTYPE; return EPROTONOSUPPORT; } - if (prp->pr_usrreqs == NULL) + if (prp->pr_usrreq == NULL) return EPROTONOSUPPORT; if (prp->pr_type != type) return EPROTOTYPE; @@ -538,72 +533,62 @@ socreate(int dom, struct socket **aso, i uid = kauth_cred_geteuid(l->l_cred); so->so_uidinfo = uid_find(uid); so->so_cpid = l->l_proc->p_pid; - - /* - * Lock assigned and taken during pr_attach, unless we share - * the lock with another socket, e.g. socketpair(2) case. - */ - if (lockso) { + if (lockso != NULL) { + /* Caller wants us to share a lock. */ lock = lockso->so_lock; so->so_lock = lock; mutex_obj_hold(lock); + /* XXX Why is this not solock, to match sounlock? */ + mutex_enter(lock); + } else { + /* Lock assigned and taken during PRU_ATTACH. */ } - - error = (*prp->pr_usrreqs->pr_attach)(so, proto); - if (error) { - solock(so); - KASSERT(so->so_pcb == NULL); + error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL, + (struct mbuf *)(long)proto, NULL, l); + KASSERT(solocked(so)); + if (error != 0) { so->so_state |= SS_NOFDREF; sofree(so); return error; } so->so_cred = kauth_cred_dup(l->l_cred); - + sounlock(so); *aso = so; return 0; } -/* - * fsocreate: create a socket and a file descriptor associated with it. - * - * => On success, write file descriptor to fdout and return zero. - * => On failure, return non-zero; *fdout will be undefined. +/* On success, write file descriptor to fdout and return zero. On + * failure, return non-zero; *fdout will be undefined. */ int -fsocreate(int domain, struct socket **sop, int type, int protocol, int *fdout) +fsocreate(int domain, struct socket **sop, int type, int protocol, + struct lwp *l, int *fdout) { - lwp_t *l = curlwp; - int error, fd, flags; - struct socket *so; - struct file *fp; + struct socket *so; + struct file *fp; + int fd, error; + int flags = type & SOCK_FLAGS_MASK; - if ((error = fd_allocfile(&fp, &fd)) != 0) { + type &= ~SOCK_FLAGS_MASK; + if ((error = fd_allocfile(&fp, &fd)) != 0) return error; - } - - flags = type & SOCK_FLAGS_MASK; fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0); fp->f_flag = FREAD|FWRITE|((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)| ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0); fp->f_type = DTYPE_SOCKET; fp->f_ops = &socketops; - - type &= ~SOCK_FLAGS_MASK; error = socreate(domain, &so, type, protocol, l, NULL); if (error != 0) { fd_abort(curproc, fp, fd); - return error; - } - if (flags & SOCK_NONBLOCK) { - so->so_state |= SS_NBIO; - } - - fp->f_data = so; - fd_affix(curproc, fp, fd); - if (sop != NULL) { - *sop = so; + } else { + if (sop != NULL) + *sop = so; + fp->f_data = so; + fd_affix(curproc, fp, fd); + *fdout = fd; + if (flags & SOCK_NONBLOCK) + so->so_state |= SS_NBIO; } - *fdout = fd; return error; } @@ -623,11 +608,10 @@ sofamily(const struct socket *so) int sobind(struct socket *so, struct mbuf *nam, struct lwp *l) { - int error; + int error; solock(so); - error = (*so->so_proto->pr_usrreqs->pr_generic)(so, - PRU_BIND, NULL, nam, NULL, l); + error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, l); sounlock(so); return error; } @@ -635,15 +619,15 @@ sobind(struct socket *so, struct mbuf *n int solisten(struct socket *so, int backlog, struct lwp *l) { - int error; + int error; solock(so); if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) != 0) { - sounlock(so); + sounlock(so); return (EINVAL); } - error = (*so->so_proto->pr_usrreqs->pr_generic)(so, PRU_LISTEN, NULL, + error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, NULL, NULL, l); if (error != 0) { sounlock(so); @@ -701,14 +685,16 @@ sofree(struct socket *so) } /* - * soclose: close a socket on last file table reference removal. - * Initiate disconnect if connected. Free socket when disconnect complete. + * Close a socket on last file table reference removal. + * Initiate disconnect if connected. + * Free socket when disconnect complete. */ int soclose(struct socket *so) { - struct socket *so2; - int error; + struct socket *so2; + int error; + int error2; error = 0; solock(so); @@ -733,7 +719,7 @@ soclose(struct socket *so) break; } } - if (so->so_pcb == NULL) + if (so->so_pcb == 0) goto discard; if (so->so_state & SS_ISCONNECTED) { if ((so->so_state & SS_ISDISCONNECTING) == 0) { @@ -754,14 +740,18 @@ soclose(struct socket *so) } drop: if (so->so_pcb) { - (*so->so_proto->pr_usrreqs->pr_detach)(so); + error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, + NULL, NULL, NULL, NULL); + if (error == 0) + error = error2; } discard: - KASSERT((so->so_state & SS_NOFDREF) == 0); + if (so->so_state & SS_NOFDREF) + panic("soclose: NOFDREF"); kauth_cred_free(so->so_cred); so->so_state |= SS_NOFDREF; sofree(so); - return error; + return (error); } /* @@ -777,8 +767,8 @@ soabort(struct socket *so) KASSERT(so->so_head == NULL); so->so_aborting++; /* XXX */ - error = (*so->so_proto->pr_usrreqs->pr_generic)(so, - PRU_ABORT, NULL, NULL, NULL, NULL); + error = (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, + NULL, NULL, NULL); refs = --so->so_aborting; /* XXX */ if (error || (refs == 0)) { sofree(so); @@ -796,13 +786,13 @@ soaccept(struct socket *so, struct mbuf KASSERT(solocked(so)); error = 0; - KASSERT((so->so_state & SS_NOFDREF) != 0); + if ((so->so_state & SS_NOFDREF) == 0) + panic("soaccept: !NOFDREF"); so->so_state &= ~SS_NOFDREF; - if ((so->so_state & SS_ISDISCONNECTED) == 0 || (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) - error = (*so->so_proto->pr_usrreqs->pr_generic)(so, - PRU_ACCEPT, NULL, nam, NULL, NULL); + error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, + NULL, nam, NULL, NULL); else error = ECONNABORTED; @@ -812,7 +802,7 @@ soaccept(struct socket *so, struct mbuf int soconnect(struct socket *so, struct mbuf *nam, struct lwp *l) { - int error; + int error; KASSERT(solocked(so)); @@ -829,10 +819,9 @@ soconnect(struct socket *so, struct mbuf (error = sodisconnect(so)))) error = EISCONN; else - error = (*so->so_proto->pr_usrreqs->pr_generic)(so, - PRU_CONNECT, NULL, nam, NULL, l); - - return error; + error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, + NULL, nam, NULL, l); + return (error); } int @@ -842,7 +831,7 @@ soconnect2(struct socket *so1, struct so KASSERT(solocked2(so1, so2)); - error = (*so1->so_proto->pr_usrreqs->pr_generic)(so1, PRU_CONNECT2, + error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, NULL, (struct mbuf *)so2, NULL, NULL); return (error); } @@ -859,8 +848,8 @@ sodisconnect(struct socket *so) } else if (so->so_state & SS_ISDISCONNECTING) { error = EALREADY; } else { - error = (*so->so_proto->pr_usrreqs->pr_generic)(so, - PRU_DISCONNECT, NULL, NULL, NULL, NULL); + error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, + NULL, NULL, NULL, NULL); } return (error); } @@ -938,8 +927,7 @@ sosend(struct socket *so, struct mbuf *a } if ((so->so_state & SS_ISCONNECTED) == 0) { if (so->so_proto->pr_flags & PR_CONNREQUIRED) { - if ((so->so_state & SS_ISCONFIRMING) == 0 && - !(resid == 0 && clen != 0)) { + if (resid || clen == 0) { error = ENOTCONN; goto release; } @@ -1057,7 +1045,7 @@ sosend(struct socket *so, struct mbuf *a so->so_options |= SO_DONTROUTE; if (resid > 0) so->so_state |= SS_MORETOCOME; - error = (*so->so_proto->pr_usrreqs->pr_generic)(so, + error = (*so->so_proto->pr_usrreq)(so, (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, top, addr, control, curlwp); if (dontroute) @@ -1171,7 +1159,7 @@ soreceive(struct socket *so, struct mbuf if (flags & MSG_OOB) { m = m_get(M_WAIT, MT_DATA); solock(so); - error = (*pr->pr_usrreqs->pr_generic)(so, PRU_RCVOOB, m, + error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m, (struct mbuf *)(long)(flags & MSG_PEEK), NULL, l); sounlock(so); if (error) @@ -1196,9 +1184,6 @@ soreceive(struct socket *so, struct mbuf */ s = splsoftnet(); solock(so); - if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) - (*pr->pr_usrreqs->pr_generic)(so, PRU_RCVD, NULL, NULL, NULL, l); - restart: if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) { sounlock(so); @@ -1542,7 +1527,7 @@ soreceive(struct socket *so, struct mbuf * get it filled again. */ if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb) - (*pr->pr_usrreqs->pr_generic)(so, PRU_RCVD, + (*pr->pr_usrreq)(so, PRU_RCVD, NULL, (struct mbuf *)(long)flags, NULL, l); SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); @@ -1584,7 +1569,7 @@ soreceive(struct socket *so, struct mbuf SBLASTRECORDCHK(&so->so_rcv, "soreceive 4"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) - (*pr->pr_usrreqs->pr_generic)(so, PRU_RCVD, NULL, + (*pr->pr_usrreq)(so, PRU_RCVD, NULL, (struct mbuf *)(long)flags, NULL, l); } if (orig_resid == uio->uio_resid && orig_resid && @@ -1619,7 +1604,7 @@ soshutdown(struct socket *so, int how) error = 0; } if (how == SHUT_WR || how == SHUT_RDWR) - error = (*pr->pr_usrreqs->pr_generic)(so, PRU_SHUTDOWN, NULL, + error = (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL, NULL, NULL, NULL); return error; @@ -1679,7 +1664,8 @@ sorflush(struct socket *so) static int sosetopt1(struct socket *so, const struct sockopt *sopt) { - int error = EINVAL, optval, opt; + int error = EINVAL, opt; + int optval = 0; /* XXX: gcc */ struct linger l; struct timeval tv; @@ -1921,6 +1907,7 @@ sogetopt1(struct socket *so, struct sock #ifdef SO_OTIMESTAMP case SO_OTIMESTAMP: #endif + case SO_ACCEPTCONN: error = sockopt_setint(sopt, (so->so_options & opt) ? 1 : 0); break; @@ -2438,11 +2425,6 @@ sysctl_kern_socket_setup(void) { KASSERT(socket_sysctllog == NULL); - sysctl_createv(&socket_sysctllog, 0, NULL, NULL, - CTLFLAG_PERMANENT, - CTLTYPE_NODE, "kern", NULL, - NULL, 0, NULL, 0, - CTL_KERN, CTL_EOL); sysctl_createv(&socket_sysctllog, 0, NULL, NULL, CTLFLAG_PERMANENT|CTLFLAG_READWRITE,