]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/uipc_socket2.c
xnu-2050.48.11.tar.gz
[apple/xnu.git] / bsd / kern / uipc_socket2.c
index c53150e51cac0ede1bad9a70ec7dff33c2624795..32b896ee819713702cb61956ceb67f4a514627a3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998-2008 Apple Inc. All rights reserved.
+ * Copyright (c) 1998-2012 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
@@ -76,6 +76,7 @@
 #include <sys/kauth.h>
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
+#include <sys/mcache.h>
 #include <sys/protosw.h>
 #include <sys/stat.h>
 #include <sys/socket.h>
@@ -94,6 +95,8 @@
 #include <security/mac_framework.h>
 #endif
 
+#include <mach/vm_param.h>
+
 /* TODO: this should be in a header file somewhere */
 extern void postevent(struct socket *, struct sockbuf *, int);
 
@@ -122,9 +125,11 @@ u_int32_t  sb_max = SB_MAX;                /* XXX should be static */
 u_int32_t      high_sb_max = SB_MAX;
 
 static u_int32_t sb_efficiency = 8;    /* parameter for sbreserve() */
-__private_extern__ unsigned int total_mb_cnt = 0;
-__private_extern__ unsigned int total_cl_cnt = 0;
-__private_extern__ int sbspace_factor = 8;
+__private_extern__ int32_t total_sbmb_cnt = 0;
+
+/* Control whether to throttle sockets eligible to be throttled */
+__private_extern__ u_int32_t net_io_policy_throttled = 0;
+static int sysctl_io_policy_throttled SYSCTL_HANDLER_ARGS;
 
 /*
  * Procedures to manipulate state flags of socket
@@ -197,6 +202,7 @@ soisconnected(struct socket *so)
                wakeup((caddr_t)&so->so_timeo);
                sorwakeup(so);
                sowwakeup(so);
+               soevent(so, SO_FILT_HINT_LOCKED);
        }
 }
 
@@ -205,6 +211,7 @@ soisdisconnecting(struct socket *so)
 {
        so->so_state &= ~SS_ISCONNECTING;
        so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
+       soevent(so, SO_FILT_HINT_LOCKED);
        sflt_notify(so, sock_evt_disconnecting, NULL);
        wakeup((caddr_t)&so->so_timeo);
        sowwakeup(so);
@@ -216,12 +223,28 @@ soisdisconnected(struct socket *so)
 {
        so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
        so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
+       soevent(so, SO_FILT_HINT_LOCKED);
        sflt_notify(so, sock_evt_disconnected, NULL);
        wakeup((caddr_t)&so->so_timeo);
        sowwakeup(so);
        sorwakeup(so);
 }
 
+/* This function will issue a wakeup like soisdisconnected but it will not
+ * notify the socket filters. This will avoid unlocking the socket
+ * in the midst of closing it.
+ */
+void
+sodisconnectwakeup(struct socket *so)
+{
+       so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
+       so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
+       soevent(so, SO_FILT_HINT_LOCKED);
+       wakeup((caddr_t)&so->so_timeo);
+       sowwakeup(so);
+       sorwakeup(so);
+}
+
 /*
  * When an attempt at a new connection is noted on a socket
  * which accepts connections, sonewconn is called.  If the
@@ -276,7 +299,6 @@ sonewconn_internal(struct socket *head, int connstatus)
                return ((struct socket *)0);
        }
 
-       so->so_head = head;
        so->so_type = head->so_type;
        so->so_options = head->so_options &~ SO_ACCEPTCONN;
        so->so_linger = head->so_linger;
@@ -284,14 +306,21 @@ sonewconn_internal(struct socket *head, int connstatus)
        so->so_proto = head->so_proto;
        so->so_timeo = head->so_timeo;
        so->so_pgid  = head->so_pgid;
-       so->so_uid = head->so_uid;
+       kauth_cred_ref(head->so_cred);
+       so->so_cred = head->so_cred;
+       so->last_pid = head->last_pid;
+       so->last_upid = head->last_upid;
        /* inherit socket options stored in so_flags */
        so->so_flags = head->so_flags & (SOF_NOSIGPIPE |
                                         SOF_NOADDRAVAIL |
                                         SOF_REUSESHAREUID | 
                                         SOF_NOTIFYCONFLICT | 
                                         SOF_BINDRANDOMPORT | 
-                                        SOF_NPX_SETOPTSHUT);
+                                        SOF_NPX_SETOPTSHUT |
+                                        SOF_NODEFUNCT |
+                                        SOF_PRIVILEGED_TRAFFIC_CLASS|
+                                        SOF_NOTSENT_LOWAT |
+                                        SOF_USELRO);
        so->so_usecount = 1;
        so->next_lock_lr = 0;
        so->next_unlock_lr = 0;
@@ -306,11 +335,17 @@ sonewconn_internal(struct socket *head, int connstatus)
        mac_socket_label_associate_accept(head, so);
 #endif
 
+       /* inherit traffic management properties of listener */
+       so->so_traffic_mgt_flags = head->so_traffic_mgt_flags & (TRAFFIC_MGT_SO_BACKGROUND);
+       so->so_background_thread = head->so_background_thread;
+       so->so_traffic_class = head->so_traffic_class;
+
        if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
-               sflt_termsock(so);
                sodealloc(so);
                return ((struct socket *)0);
        }
+       so->so_rcv.sb_flags |= (head->so_rcv.sb_flags & SB_USRSIZE);
+       so->so_snd.sb_flags |= (head->so_snd.sb_flags & SB_USRSIZE);
 
        /*
         * Must be done with head unlocked to avoid deadlock
@@ -320,17 +355,36 @@ sonewconn_internal(struct socket *head, int connstatus)
                socket_unlock(head, 0);
        if (((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL) != 0) ||
            error) {
-               sflt_termsock(so);
                sodealloc(so);
                if (head->so_proto->pr_unlock)
                        socket_lock(head, 0);
                return ((struct socket *)0);
        }
-       if (head->so_proto->pr_unlock)
+       if (head->so_proto->pr_unlock) {
                socket_lock(head, 0);
+               /* Radar 7385998 Recheck that the head is still accepting
+                * to avoid race condition when head is getting closed.
+                */
+               if ((head->so_options & SO_ACCEPTCONN) == 0) {
+                       so->so_state &= ~SS_NOFDREF;
+                       soclose(so);
+                       return ((struct socket *)0);
+               }
+       }
+
 #ifdef __APPLE__
        so->so_proto->pr_domain->dom_refs++;
 #endif
+       /* Insert in head appropriate lists */
+       so->so_head = head;
+
+       /* Since this socket is going to be inserted into the incomp
+        * queue, it can be picked up by another thread in 
+        * tcp_dropdropablreq to get dropped before it is setup.. 
+        * To prevent this race, set in-progress flag which can be
+        * cleared later
+        */
+       so->so_flags |= SOF_INCOMP_INPROGRESS;
 
        if (connstatus) {
                TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
@@ -359,27 +413,7 @@ sonewconn_internal(struct socket *head, int connstatus)
 struct socket *
 sonewconn(struct socket *head, int connstatus, const struct sockaddr *from)
 {
-       int error = 0;
-       struct socket_filter_entry *filter;
-       int filtered = 0;
-
-       for (filter = head->so_filt; filter && (error == 0);
-           filter = filter->sfe_next_onsocket) {
-               if (filter->sfe_filter->sf_filter.sf_connect_in) {
-                       if (filtered == 0) {
-                               filtered = 1;
-                               sflt_use(head);
-                               socket_unlock(head, 0);
-                       }
-                       error = filter->sfe_filter->sf_filter.
-                           sf_connect_in(filter->sfe_cookie, head, from);
-               }
-       }
-       if (filtered != 0) {
-               socket_lock(head, 0);
-               sflt_unuse(head);
-       }
-
+       int error = sflt_connectin(head, from);
        if (error) {
                return (NULL);
        }
@@ -401,6 +435,7 @@ void
 socantsendmore(struct socket *so)
 {
        so->so_state |= SS_CANTSENDMORE;
+       soevent(so, SO_FILT_HINT_LOCKED);
        sflt_notify(so, sock_evt_cantsendmore, NULL);
        sowwakeup(so);
 }
@@ -409,6 +444,7 @@ void
 socantrcvmore(struct socket *so)
 {
        so->so_state |= SS_CANTRCVMORE;
+       soevent(so, SO_FILT_HINT_LOCKED);
        sflt_notify(so, sock_evt_cantrecvmore, NULL);
        sorwakeup(so);
 }
@@ -435,6 +471,7 @@ sbwait(struct sockbuf *sb)
                mutex_held = (*so->so_proto->pr_getlock)(so, 0);
        else
                mutex_held = so->so_proto->pr_domain->dom_mtx;
+       lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
 
        sb->sb_flags |= SB_WAIT;
 
@@ -450,8 +487,13 @@ sbwait(struct sockbuf *sb)
        if (so->so_usecount < 1)
                panic("sbwait: so=%p refcount=%d\n", so, so->so_usecount);
 
-       if ((so->so_state & SS_DRAINING)) {
+       if ((so->so_state & SS_DRAINING) || (so->so_flags & SOF_DEFUNCT)) {
                error = EBADF;
+               if (so->so_flags & SOF_DEFUNCT) {
+                       SODEFUNCTLOG(("%s[%d]: defunct so %p [%d,%d] (%d)\n",
+                           __func__, proc_selfpid(), so, INP_SOCKAF(so),
+                           INP_SOCKTYPE(so), error));
+               }
        }
 
        return (error);
@@ -476,10 +518,13 @@ sb_lock(struct sockbuf *sb)
 
        while (sb->sb_flags & SB_LOCK) {
                sb->sb_flags |= SB_WANT;
+
                if (so->so_proto->pr_getlock != NULL)
                        mutex_held = (*so->so_proto->pr_getlock)(so, 0);
                else
                        mutex_held = so->so_proto->pr_domain->dom_mtx;
+               lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
+
                if (so->so_usecount < 1)
                        panic("sb_lock: so=%p refcount=%d\n", so,
                            so->so_usecount);
@@ -490,6 +535,14 @@ sb_lock(struct sockbuf *sb)
                if (so->so_usecount < 1)
                        panic("sb_lock: 2 so=%p refcount=%d\n", so,
                            so->so_usecount);
+
+               if (error == 0 && (so->so_flags & SOF_DEFUNCT)) {
+                       error = EBADF;
+                       SODEFUNCTLOG(("%s[%d]: defunct so %p [%d,%d] (%d)\n",
+                           __func__, proc_selfpid(), so, INP_SOCKAF(so),
+                           INP_SOCKTYPE(so), error));
+               }
+
                if (error)
                        return (error);
        }
@@ -497,6 +550,15 @@ sb_lock(struct sockbuf *sb)
        return (0);
 }
 
+void
+sbwakeup(struct sockbuf *sb)
+{
+       if (sb->sb_flags & SB_WAIT) {
+               sb->sb_flags &= ~SB_WAIT;
+               wakeup((caddr_t)&sb->sb_cc);
+       }
+}
+
 /*
  * Wakeup processes waiting on a socket buffer.
  * Do asynchronous notification via SIGIO
@@ -505,12 +567,17 @@ sb_lock(struct sockbuf *sb)
 void
 sowakeup(struct socket *so, struct sockbuf *sb)
 {
+       if (so->so_flags & SOF_DEFUNCT) {
+               SODEFUNCTLOG(("%s[%d]: defunct so %p [%d,%d] si 0x%x, "
+                   "fl 0x%x [%s]\n", __func__, proc_selfpid(), so,
+                   INP_SOCKAF(so), INP_SOCKTYPE(so),
+                   (uint32_t)sb->sb_sel.si_flags, (uint16_t)sb->sb_flags,
+                   (sb->sb_flags & SB_RECV) ? "rcv" : "snd"));
+       }
+
        sb->sb_flags &= ~SB_SEL;
        selwakeup(&sb->sb_sel);
-       if (sb->sb_flags & SB_WAIT) {
-               sb->sb_flags &= ~SB_WAIT;
-               wakeup((caddr_t)&sb->sb_cc);
-       }
+       sbwakeup(sb);
        if (so->so_state & SS_ASYNC) {
                if (so->so_pgid < 0)
                        gsignal(-so->so_pgid, SIGIO);
@@ -527,15 +594,15 @@ sowakeup(struct socket *so, struct sockbuf *sb)
                so_upcall = so->so_upcall;
                so_upcallarg = so->so_upcallarg;
                /* Let close know that we're about to do an upcall */
-               so->so_flags |= SOF_UPCALLINUSE;
+               so->so_upcallusecount++;
 
                socket_unlock(so, 0);
                (*so_upcall)(so, so_upcallarg, M_DONTWAIT);
                socket_lock(so, 0);
 
-               so->so_flags &= ~SOF_UPCALLINUSE;
+               so->so_upcallusecount--;
                /* Tell close that it's safe to proceed */
-               if (so->so_flags & SOF_CLOSEWAIT)
+               if (so->so_flags & SOF_CLOSEWAIT && so->so_upcallusecount == 0)
                        wakeup((caddr_t)&so->so_upcall);
        }
 }
@@ -582,8 +649,14 @@ soreserve(struct socket *so, u_int32_t sndcc, u_int32_t rcvcc)
 
        if (sbreserve(&so->so_snd, sndcc) == 0)
                goto bad;
+       else
+               so->so_snd.sb_idealsize = sndcc;
+
        if (sbreserve(&so->so_rcv, rcvcc) == 0)
                goto bad2;
+       else
+               so->so_rcv.sb_idealsize = rcvcc;
+
        if (so->so_rcv.sb_lowat == 0)
                so->so_rcv.sb_lowat = 1;
        if (so->so_snd.sb_lowat == 0)
@@ -677,7 +750,7 @@ sbappend(struct sockbuf *sb, struct mbuf *m)
                return (sbappendrecord(sb, m));
 
        if (sb->sb_flags & SB_RECV) {
-               int error = sflt_data_in(so, NULL, &m, NULL, 0, NULL);
+               int error = sflt_data_in(so, NULL, &m, NULL, 0);
                SBLASTRECORDCHK(sb, "sbappend 2");
                if (error != 0) {
                        if (error != EJUSTRETURN)
@@ -716,7 +789,7 @@ sbappendstream(struct sockbuf *sb, struct mbuf *m)
        }
 
        if (sb->sb_flags & SB_RECV) {
-               int error = sflt_data_in(so, NULL, &m, NULL, 0, NULL);
+               int error = sflt_data_in(so, NULL, &m, NULL, 0);
                SBLASTRECORDCHK(sb, "sbappendstream 1");
                if (error != 0) {
                        if (error != EJUSTRETURN)
@@ -836,7 +909,7 @@ sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
 
        if (sb->sb_flags & SB_RECV) {
                int error = sflt_data_in(sb->sb_so, NULL, &m0, NULL,
-                   sock_data_filt_flag_record, NULL);
+                   sock_data_filt_flag_record);
                if (error != 0) {
                        SBLASTRECORDCHK(sb, "sbappendrecord 1");
                        if (error != EJUSTRETURN)
@@ -887,7 +960,7 @@ sbinsertoob(struct sockbuf *sb, struct mbuf *m0)
 
        if ((sb->sb_flags & SB_RECV) != 0) {
                int error = sflt_data_in(sb->sb_so, NULL, &m0, NULL,
-                   sock_data_filt_flag_oob, NULL);
+                   sock_data_filt_flag_oob);
 
                SBLASTRECORDCHK(sb, "sbinsertoob 2");
                if (error) {
@@ -1032,7 +1105,7 @@ sbappendaddr(struct sockbuf *sb, struct sockaddr *asa, struct mbuf *m0,
        /* Call socket data in filters */
        if ((sb->sb_flags & SB_RECV) != 0) {
                int error;
-               error = sflt_data_in(sb->sb_so, asa, &m0, &control, 0, NULL);
+               error = sflt_data_in(sb->sb_so, asa, &m0, &control, 0);
                SBLASTRECORDCHK(sb, __func__);
                if (error) {
                        if (error != EJUSTRETURN) {
@@ -1127,7 +1200,7 @@ sbappendcontrol(struct sockbuf *sb, struct mbuf   *m0, struct mbuf *control,
        if (sb->sb_flags & SB_RECV) {
                int error;
 
-               error = sflt_data_in(sb->sb_so, NULL, &m0, &control, 0, NULL);
+               error = sflt_data_in(sb->sb_so, NULL, &m0, &control, 0);
                SBLASTRECORDCHK(sb, __func__);
                if (error) {
                        if (error != EJUSTRETURN) {
@@ -1396,6 +1469,7 @@ sbcreatecontrol(caddr_t p, int size, int type, int level)
        if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
                return ((struct mbuf *)NULL);
        cp = mtod(m, struct cmsghdr *);
+       VERIFY(IS_P2ALIGNED(cp, sizeof (u_int32_t)));
        /* XXX check size? */
        (void) memcpy(CMSG_DATA(cp), p, size);
        m->m_len = CMSG_SPACE(size);
@@ -1405,6 +1479,40 @@ sbcreatecontrol(caddr_t p, int size, int type, int level)
        return (m);
 }
 
+struct mbuf**
+sbcreatecontrol_mbuf(caddr_t p, int size, int type, int level, struct mbuf** mp)
+{
+       struct mbuf* m;
+       struct cmsghdr *cp;
+
+       if (*mp == NULL){
+               *mp = sbcreatecontrol(p, size, type, level);
+               return mp;
+       }
+
+       if (CMSG_SPACE((u_int)size) + (*mp)->m_len > MLEN){
+               mp = &(*mp)->m_next;
+               *mp = sbcreatecontrol(p, size, type, level);
+               return mp;
+       }
+
+       m = *mp;
+
+       cp = (struct cmsghdr *)(void *)(mtod(m, char *) + m->m_len);
+       /* CMSG_SPACE ensures 32-bit alignment */
+       VERIFY(IS_P2ALIGNED(cp, sizeof (u_int32_t)));
+       m->m_len += CMSG_SPACE(size);
+
+       /* XXX check size? */
+       (void) memcpy(CMSG_DATA(cp), p, size);
+       cp->cmsg_len = CMSG_LEN(size);
+       cp->cmsg_level = level;
+       cp->cmsg_type = type;
+
+       return mp;
+}
+
+
 /*
  * Some routines that return EOPNOTSUPP for entry points that are not
  * supported by a protocol.  Fill in as needed.
@@ -1618,9 +1726,10 @@ soreadable(struct socket *so)
 int
 sowriteable(struct socket *so)
 {
-       return ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat &&
-           ((so->so_state&SS_ISCONNECTED) ||
-           (so->so_proto->pr_flags&PR_CONNREQUIRED) == 0)) ||
+       return ((!so_wait_for_if_feedback(so) &&
+           sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat &&
+           ((so->so_state & SS_ISCONNECTED) ||
+           (so->so_proto->pr_flags & PR_CONNREQUIRED) == 0)) ||
            (so->so_state & SS_CANTSENDMORE) ||
            so->so_error);
 }
@@ -1630,7 +1739,7 @@ sowriteable(struct socket *so)
 void
 sballoc(struct sockbuf *sb, struct mbuf *m)
 {
-       int cnt = 1;
+       u_int32_t cnt = 1;
        sb->sb_cc += m->m_len; 
        if (m->m_type != MT_DATA && m->m_type != MT_HEADER && 
                m->m_type != MT_OOBDATA)
@@ -1639,9 +1748,10 @@ sballoc(struct sockbuf *sb, struct mbuf *m)
        
        if (m->m_flags & M_EXT) {
                sb->sb_mbcnt += m->m_ext.ext_size; 
-               cnt += m->m_ext.ext_size / MSIZE ;
+               cnt += (m->m_ext.ext_size >> MSIZESHIFT) ;
        }
-       OSAddAtomic(cnt, &total_mb_cnt);
+       OSAddAtomic(cnt, &total_sbmb_cnt);
+       VERIFY(total_sbmb_cnt > 0);
 }
 
 /* adjust counters in sb reflecting freeing of m */
@@ -1649,6 +1759,7 @@ void
 sbfree(struct sockbuf *sb, struct mbuf *m)
 {
        int cnt = -1;
+
        sb->sb_cc -= m->m_len;
        if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&     
                m->m_type != MT_OOBDATA)
@@ -1656,9 +1767,10 @@ sbfree(struct sockbuf *sb, struct mbuf *m)
        sb->sb_mbcnt -= MSIZE; 
        if (m->m_flags & M_EXT) {
                sb->sb_mbcnt -= m->m_ext.ext_size; 
-               cnt -= m->m_ext.ext_size / MSIZE ;
+               cnt -= (m->m_ext.ext_size >> MSIZESHIFT) ;
        }
-       OSAddAtomic(cnt, &total_mb_cnt);
+       OSAddAtomic(cnt, &total_sbmb_cnt);
+       VERIFY(total_sbmb_cnt >= 0);
 }
 
 /*
@@ -1737,6 +1849,14 @@ sowwakeup(struct socket *so)
        if (sb_notify(&so->so_snd))
                sowakeup(so, &so->so_snd);
 }
+
+void
+soevent(struct socket *so, long hint)
+{
+       if (so->so_flags & SOF_KNOTE)
+               KNOTE(&so->so_klist, hint);
+}
+
 #endif /* __APPLE__ */
 
 /*
@@ -1766,12 +1886,12 @@ void
 sotoxsocket(struct socket *so, struct xsocket *xso)
 {
        xso->xso_len = sizeof (*xso);
-       xso->xso_so = (_XSOCKET_PTR(struct socket *))(uintptr_t)so;
+       xso->xso_so = (_XSOCKET_PTR(struct socket *))VM_KERNEL_ADDRPERM(so);
        xso->so_type = so->so_type;
-       xso->so_options = so->so_options;
+       xso->so_options = (short)(so->so_options & 0xffff);
        xso->so_linger = so->so_linger;
        xso->so_state = so->so_state;
-       xso->so_pcb = (_XSOCKET_PTR(caddr_t))(uintptr_t)so->so_pcb;
+       xso->so_pcb = (_XSOCKET_PTR(caddr_t))VM_KERNEL_ADDRPERM(so->so_pcb);
        if (so->so_proto) {
                xso->xso_protocol = so->so_proto->pr_protocol;
                xso->xso_family = so->so_proto->pr_domain->dom_family;
@@ -1787,7 +1907,7 @@ sotoxsocket(struct socket *so, struct xsocket *xso)
        xso->so_oobmark = so->so_oobmark;
        sbtoxsockbuf(&so->so_snd, &xso->so_snd);
        sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
-       xso->so_uid = so->so_uid;
+       xso->so_uid = kauth_cred_getuid(so->so_cred);
 }
 
 
@@ -1797,12 +1917,12 @@ void
 sotoxsocket64(struct socket *so, struct xsocket64 *xso)
 {
         xso->xso_len = sizeof (*xso);
-        xso->xso_so = (u_int64_t)(uintptr_t)so;
+        xso->xso_so = (u_int64_t)VM_KERNEL_ADDRPERM(so);
         xso->so_type = so->so_type;
-        xso->so_options = so->so_options;
+        xso->so_options = (short)(so->so_options & 0xffff);
         xso->so_linger = so->so_linger;
         xso->so_state = so->so_state;
-        xso->so_pcb = (u_int64_t)(uintptr_t)so->so_pcb;
+        xso->so_pcb = (u_int64_t)VM_KERNEL_ADDRPERM(so->so_pcb);
         if (so->so_proto) {
                 xso->xso_protocol = so->so_proto->pr_protocol;
                 xso->xso_family = so->so_proto->pr_domain->dom_family;
@@ -1818,7 +1938,7 @@ sotoxsocket64(struct socket *so, struct xsocket64 *xso)
         xso->so_oobmark = so->so_oobmark;
         sbtoxsockbuf(&so->so_snd, &xso->so_snd);
         sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
-        xso->so_uid = so->so_uid;
+        xso->so_uid = kauth_cred_getuid(so->so_cred);
 }
 
 #endif /* !CONFIG_EMBEDDED */
@@ -1844,11 +1964,35 @@ sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
                xsb->sb_timeo = 1;
 }
 
+/*
+ * Based on the policy set by an all knowing decison maker, throttle sockets
+ * that either have been marked as belonging to "background" process.
+ */
+int
+soisthrottled(struct socket *so)
+{
+       /*
+        * On non-embedded, we rely on implicit throttling by the application,
+        * as we're missing the system-wide "decision maker".
+        */
+       return (
+#if CONFIG_EMBEDDED
+           net_io_policy_throttled &&
+#endif /* CONFIG_EMBEDDED */
+           (so->so_traffic_mgt_flags & TRAFFIC_MGT_SO_BACKGROUND));
+}
+
+int
+soisprivilegedtraffic(struct socket *so)
+{
+       return (so->so_flags & SOF_PRIVILEGED_TRAFFIC_CLASS);
+}
+
 /*
  * Here is the definition of some of the basic objects in the kern.ipc
  * branch of the MIB.
  */
-SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "IPC");
+SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW|CTLFLAG_LOCKED|CTLFLAG_ANYBODY, 0, "IPC");
 
 /* Check that the maximum socket buffer size is within a range */
 
@@ -1871,20 +2015,45 @@ sysctl_sb_max(__unused struct sysctl_oid *oidp, __unused void *arg1,
        return error;
 }
 
-SYSCTL_PROC(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT | CTLFLAG_RW,
+static int
+sysctl_io_policy_throttled SYSCTL_HANDLER_ARGS
+{
+#pragma unused(arg1, arg2)
+       int i, err;
+
+       i = net_io_policy_throttled;
+
+       err = sysctl_handle_int(oidp, &i, 0, req);
+       if (err != 0 || req->newptr == USER_ADDR_NULL)
+               return (err);
+
+       if (i != net_io_policy_throttled)
+               SOTHROTTLELOG(("throttle: network IO policy throttling is "
+                   "now %s\n", i ? "ON" : "OFF"));
+
+       net_io_policy_throttled = i;
+
+       return (err);
+}
+
+SYSCTL_PROC(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
     &sb_max, 0, &sysctl_sb_max, "IU", "Maximum socket buffer size");
 
-SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD,
+SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD | CTLFLAG_LOCKED,
     &maxsockets, 0, "Maximum number of sockets avaliable");
-SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
+SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW | CTLFLAG_LOCKED,
     &sb_efficiency, 0, "");
-SYSCTL_INT(_kern_ipc, OID_AUTO, sbspace_factor, CTLFLAG_RW,
-    &sbspace_factor, 0, "Ratio of mbuf/cluster use for socket layers");
-SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD,
+SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD | CTLFLAG_LOCKED,
     &nmbclusters, 0, "");
-SYSCTL_INT(_kern_ipc, OID_AUTO, njcl, CTLFLAG_RD, &njcl, 0, "");
-SYSCTL_INT(_kern_ipc, OID_AUTO, njclbytes, CTLFLAG_RD, &njclbytes, 0, "");
-SYSCTL_INT(_kern_ipc, KIPC_SOQLIMITCOMPAT, soqlimitcompat, CTLFLAG_RW,
+SYSCTL_INT(_kern_ipc, OID_AUTO, njcl, CTLFLAG_RD | CTLFLAG_LOCKED, &njcl, 0, "");
+SYSCTL_INT(_kern_ipc, OID_AUTO, njclbytes, CTLFLAG_RD | CTLFLAG_LOCKED, &njclbytes, 0, "");
+SYSCTL_INT(_kern_ipc, KIPC_SOQLIMITCOMPAT, soqlimitcompat, CTLFLAG_RW | CTLFLAG_LOCKED,
     &soqlimitcompat, 1, "Enable socket queue limit compatibility");
-SYSCTL_INT(_kern_ipc, OID_AUTO, soqlencomp, CTLFLAG_RW,
+SYSCTL_INT(_kern_ipc, OID_AUTO, soqlencomp, CTLFLAG_RW | CTLFLAG_LOCKED,
     &soqlencomp, 0, "Listen backlog represents only complete queue");
+
+SYSCTL_NODE(_kern_ipc, OID_AUTO, io_policy, CTLFLAG_RW, 0, "network IO policy");
+
+SYSCTL_PROC(_kern_ipc_io_policy, OID_AUTO, throttled,
+    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &net_io_policy_throttled, 0,
+    sysctl_io_policy_throttled, "I", "");