netlabel: Label incoming TCP connections correctly in SELinux
[safe/jmp/linux-2.6] / net / ipv4 / tcp_timer.c
index 3355c27..b144a26 100644 (file)
@@ -5,8 +5,6 @@
  *
  *             Implementation of the Transmission Control Protocol(TCP).
  *
- * Version:    $Id: tcp_timer.c,v 1.88 2002/02/01 22:01:04 davem Exp $
- *
  * Authors:    Ross Biro
  *             Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  *             Mark Evans, <evansmp@uhura.aston.ac.uk>
@@ -50,7 +48,7 @@ static void tcp_write_err(struct sock *sk)
        sk->sk_error_report(sk);
 
        tcp_done(sk);
-       NET_INC_STATS_BH(LINUX_MIB_TCPABORTONTIMEOUT);
+       NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT);
 }
 
 /* Do not allow orphaned sockets to eat all our resources.
@@ -67,9 +65,9 @@ static void tcp_write_err(struct sock *sk)
 static int tcp_out_of_resources(struct sock *sk, int do_reset)
 {
        struct tcp_sock *tp = tcp_sk(sk);
-       int orphans = atomic_read(&tcp_orphan_count);
+       int orphans = percpu_counter_read_positive(&tcp_orphan_count);
 
-       /* If peer does not open window for long time, or did not transmit 
+       /* If peer does not open window for long time, or did not transmit
         * anything for long time, penalize it. */
        if ((s32)(tcp_time_stamp - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset)
                orphans <<= 1;
@@ -78,9 +76,7 @@ static int tcp_out_of_resources(struct sock *sk, int do_reset)
        if (sk->sk_err_soft)
                orphans <<= 1;
 
-       if (orphans >= sysctl_tcp_max_orphans ||
-           (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
-            atomic_read(&tcp_memory_allocated) > sysctl_tcp_mem[2])) {
+       if (tcp_too_many_orphans(sk, orphans)) {
                if (net_ratelimit())
                        printk(KERN_INFO "Out of socket memory\n");
 
@@ -93,7 +89,7 @@ static int tcp_out_of_resources(struct sock *sk, int do_reset)
                if (do_reset)
                        tcp_send_active_reset(sk, GFP_ATOMIC);
                tcp_done(sk);
-               NET_INC_STATS_BH(LINUX_MIB_TCPABORTONMEMORY);
+               NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
                return 1;
        }
        return 0;
@@ -116,13 +112,31 @@ static int tcp_orphan_retries(struct sock *sk, int alive)
        return retries;
 }
 
+static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
+{
+       /* Black hole detection */
+       if (sysctl_tcp_mtu_probing) {
+               if (!icsk->icsk_mtup.enabled) {
+                       icsk->icsk_mtup.enabled = 1;
+                       tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
+               } else {
+                       struct tcp_sock *tp = tcp_sk(sk);
+                       int mss;
+
+                       mss = tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low) >> 1;
+                       mss = min(sysctl_tcp_base_mss, mss);
+                       mss = max(mss, 68 - tp->tcp_header_len);
+                       icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
+                       tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
+               }
+       }
+}
+
 /* A write timeout has occurred. Process the after effects. */
 static int tcp_write_timeout(struct sock *sk)
 {
        struct inet_connection_sock *icsk = inet_csk(sk);
-       struct tcp_sock *tp = tcp_sk(sk);
        int retry_until;
-       int mss;
 
        if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
                if (icsk->icsk_retransmits)
@@ -131,18 +145,7 @@ static int tcp_write_timeout(struct sock *sk)
        } else {
                if (icsk->icsk_retransmits >= sysctl_tcp_retries1) {
                        /* Black hole detection */
-                       if (sysctl_tcp_mtu_probing) {
-                               if (!icsk->icsk_mtup.enabled) {
-                                       icsk->icsk_mtup.enabled = 1;
-                                       tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
-                               } else {
-                                       mss = min(sysctl_tcp_base_mss,
-                                                 tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low)/2);
-                                       mss = max(mss, 68 - tp->tcp_header_len);
-                                       icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
-                                       tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
-                               }
-                       }
+                       tcp_mtu_probing(icsk, sk);
 
                        dst_negative_advice(&sk->sk_dst_cache);
                }
@@ -150,7 +153,7 @@ static int tcp_write_timeout(struct sock *sk)
                retry_until = sysctl_tcp_retries2;
                if (sock_flag(sk, SOCK_DEAD)) {
                        const int alive = (icsk->icsk_rto < TCP_RTO_MAX);
+
                        retry_until = tcp_orphan_retries(sk, alive);
 
                        if (tcp_out_of_resources(sk, alive || icsk->icsk_retransmits < retry_until))
@@ -168,7 +171,7 @@ static int tcp_write_timeout(struct sock *sk)
 
 static void tcp_delack_timer(unsigned long data)
 {
-       struct sock *sk = (struct sock*)data;
+       struct sock *sk = (struct sock *)data;
        struct tcp_sock *tp = tcp_sk(sk);
        struct inet_connection_sock *icsk = inet_csk(sk);
 
@@ -176,12 +179,12 @@ static void tcp_delack_timer(unsigned long data)
        if (sock_owned_by_user(sk)) {
                /* Try again later. */
                icsk->icsk_ack.blocked = 1;
-               NET_INC_STATS_BH(LINUX_MIB_DELAYEDACKLOCKED);
+               NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
                sk_reset_timer(sk, &icsk->icsk_delack_timer, jiffies + TCP_DELACK_MIN);
                goto out_unlock;
        }
 
-       sk_stream_mem_reclaim(sk);
+       sk_mem_reclaim_partial(sk);
 
        if (sk->sk_state == TCP_CLOSE || !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
                goto out;
@@ -195,10 +198,10 @@ static void tcp_delack_timer(unsigned long data)
        if (!skb_queue_empty(&tp->ucopy.prequeue)) {
                struct sk_buff *skb;
 
-               NET_INC_STATS_BH(LINUX_MIB_TCPSCHEDULERFAILED);
+               NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSCHEDULERFAILED);
 
                while ((skb = __skb_dequeue(&tp->ucopy.prequeue)) != NULL)
-                       sk->sk_backlog_rcv(sk, skb);
+                       sk_backlog_rcv(sk, skb);
 
                tp->ucopy.memory = 0;
        }
@@ -215,13 +218,13 @@ static void tcp_delack_timer(unsigned long data)
                        icsk->icsk_ack.ato      = TCP_ATO_MIN;
                }
                tcp_send_ack(sk);
-               NET_INC_STATS_BH(LINUX_MIB_DELAYEDACKS);
+               NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKS);
        }
        TCP_CHECK_TIMER(sk);
 
 out:
        if (tcp_memory_pressure)
-               sk_stream_mem_reclaim(sk);
+               sk_mem_reclaim(sk);
 out_unlock:
        bh_unlock_sock(sk);
        sock_put(sk);
@@ -233,7 +236,7 @@ static void tcp_probe_timer(struct sock *sk)
        struct tcp_sock *tp = tcp_sk(sk);
        int max_probes;
 
-       if (tp->packets_out || !sk->sk_send_head) {
+       if (tp->packets_out || !tcp_send_head(sk)) {
                icsk->icsk_probes_out = 0;
                return;
        }
@@ -257,7 +260,7 @@ static void tcp_probe_timer(struct sock *sk)
 
        if (sock_flag(sk, SOCK_DEAD)) {
                const int alive = ((icsk->icsk_rto << icsk->icsk_backoff) < TCP_RTO_MAX);
+
                max_probes = tcp_orphan_retries(sk, alive);
 
                if (tcp_out_of_resources(sk, alive || icsk->icsk_probes_out <= max_probes))
@@ -284,7 +287,7 @@ static void tcp_retransmit_timer(struct sock *sk)
        if (!tp->packets_out)
                goto out;
 
-       BUG_TRAP(!skb_queue_empty(&sk->sk_write_queue));
+       WARN_ON(tcp_write_queue_empty(sk));
 
        if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) &&
            !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) {
@@ -294,19 +297,27 @@ static void tcp_retransmit_timer(struct sock *sk)
                 * we cannot allow such beasts to hang infinitely.
                 */
 #ifdef TCP_DEBUG
-               if (net_ratelimit()) {
-                       struct inet_sock *inet = inet_sk(sk);
-                       printk(KERN_DEBUG "TCP: Treason uncloaked! Peer %u.%u.%u.%u:%u/%u shrinks window %u:%u. Repaired.\n",
-                              NIPQUAD(inet->daddr), ntohs(inet->dport),
+               struct inet_sock *inet = inet_sk(sk);
+               if (sk->sk_family == AF_INET) {
+                       LIMIT_NETDEBUG(KERN_DEBUG "TCP: Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
+                              &inet->daddr, ntohs(inet->dport),
+                              inet->num, tp->snd_una, tp->snd_nxt);
+               }
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+               else if (sk->sk_family == AF_INET6) {
+                       struct ipv6_pinfo *np = inet6_sk(sk);
+                       LIMIT_NETDEBUG(KERN_DEBUG "TCP: Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
+                              &np->daddr, ntohs(inet->dport),
                               inet->num, tp->snd_una, tp->snd_nxt);
                }
 #endif
+#endif
                if (tcp_time_stamp - tp->rcv_tstamp > TCP_RTO_MAX) {
                        tcp_write_err(sk);
                        goto out;
                }
                tcp_enter_loss(sk, 0);
-               tcp_retransmit_skb(sk, skb_peek(&sk->sk_write_queue));
+               tcp_retransmit_skb(sk, tcp_write_queue_head(sk));
                __sk_dst_reset(sk);
                goto out_reset_timer;
        }
@@ -315,24 +326,24 @@ static void tcp_retransmit_timer(struct sock *sk)
                goto out;
 
        if (icsk->icsk_retransmits == 0) {
-               if (icsk->icsk_ca_state == TCP_CA_Disorder ||
-                   icsk->icsk_ca_state == TCP_CA_Recovery) {
-                       if (tp->rx_opt.sack_ok) {
-                               if (icsk->icsk_ca_state == TCP_CA_Recovery)
-                                       NET_INC_STATS_BH(LINUX_MIB_TCPSACKRECOVERYFAIL);
-                               else
-                                       NET_INC_STATS_BH(LINUX_MIB_TCPSACKFAILURES);
-                       } else {
-                               if (icsk->icsk_ca_state == TCP_CA_Recovery)
-                                       NET_INC_STATS_BH(LINUX_MIB_TCPRENORECOVERYFAIL);
-                               else
-                                       NET_INC_STATS_BH(LINUX_MIB_TCPRENOFAILURES);
-                       }
+               int mib_idx;
+
+               if (icsk->icsk_ca_state == TCP_CA_Disorder) {
+                       if (tcp_is_sack(tp))
+                               mib_idx = LINUX_MIB_TCPSACKFAILURES;
+                       else
+                               mib_idx = LINUX_MIB_TCPRENOFAILURES;
+               } else if (icsk->icsk_ca_state == TCP_CA_Recovery) {
+                       if (tcp_is_sack(tp))
+                               mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
+                       else
+                               mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
                } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
-                       NET_INC_STATS_BH(LINUX_MIB_TCPLOSSFAILURES);
+                       mib_idx = LINUX_MIB_TCPLOSSFAILURES;
                } else {
-                       NET_INC_STATS_BH(LINUX_MIB_TCPTIMEOUTS);
+                       mib_idx = LINUX_MIB_TCPTIMEOUTS;
                }
+               NET_INC_STATS_BH(sock_net(sk), mib_idx);
        }
 
        if (tcp_use_frto(sk)) {
@@ -341,7 +352,7 @@ static void tcp_retransmit_timer(struct sock *sk)
                tcp_enter_loss(sk, 0);
        }
 
-       if (tcp_retransmit_skb(sk, skb_peek(&sk->sk_write_queue)) > 0) {
+       if (tcp_retransmit_skb(sk, tcp_write_queue_head(sk)) > 0) {
                /* Retransmission failed because of local congestion,
                 * do not backoff.
                 */
@@ -382,7 +393,7 @@ out:;
 
 static void tcp_write_timer(unsigned long data)
 {
-       struct sock *sk = (struct sock*)data;
+       struct sock *sk = (struct sock *)data;
        struct inet_connection_sock *icsk = inet_csk(sk);
        int event;
 
@@ -415,7 +426,7 @@ static void tcp_write_timer(unsigned long data)
        TCP_CHECK_TIMER(sk);
 
 out:
-       sk_stream_mem_reclaim(sk);
+       sk_mem_reclaim(sk);
 out_unlock:
        bh_unlock_sock(sk);
        sock_put(sk);
@@ -453,7 +464,7 @@ static void tcp_keepalive_timer (unsigned long data)
        /* Only process if socket is not in use. */
        bh_lock_sock(sk);
        if (sock_owned_by_user(sk)) {
-               /* Try again later. */ 
+               /* Try again later. */
                inet_csk_reset_keepalive_timer (sk, HZ/20);
                goto out;
        }
@@ -482,7 +493,7 @@ static void tcp_keepalive_timer (unsigned long data)
        elapsed = keepalive_time_when(tp);
 
        /* It is alive without keepalive 8) */
-       if (tp->packets_out || sk->sk_send_head)
+       if (tp->packets_out || tcp_send_head(sk))
                goto resched;
 
        elapsed = tcp_time_stamp - tp->rcv_tstamp;
@@ -509,13 +520,13 @@ static void tcp_keepalive_timer (unsigned long data)
        }
 
        TCP_CHECK_TIMER(sk);
-       sk_stream_mem_reclaim(sk);
+       sk_mem_reclaim(sk);
 
 resched:
        inet_csk_reset_keepalive_timer (sk, elapsed);
        goto out;
 
-death: 
+death:
        tcp_done(sk);
 
 out: