LCOV - code coverage report
Current view: top level - net/ipv4 - tcp_ipv4.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 136 843 16.1 %
Date: 2020-09-30 20:25:40 Functions: 8 54 14.8 %
Branches: 32 510 6.3 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-or-later
       2                 :            : /*
       3                 :            :  * INET         An implementation of the TCP/IP protocol suite for the LINUX
       4                 :            :  *              operating system.  INET is implemented using the  BSD Socket
       5                 :            :  *              interface as the means of communication with the user level.
       6                 :            :  *
       7                 :            :  *              Implementation of the Transmission Control Protocol(TCP).
       8                 :            :  *
       9                 :            :  *              IPv4 specific functions
      10                 :            :  *
      11                 :            :  *              code split from:
      12                 :            :  *              linux/ipv4/tcp.c
      13                 :            :  *              linux/ipv4/tcp_input.c
      14                 :            :  *              linux/ipv4/tcp_output.c
      15                 :            :  *
      16                 :            :  *              See tcp.c for author information
      17                 :            :  */
      18                 :            : 
      19                 :            : /*
      20                 :            :  * Changes:
      21                 :            :  *              David S. Miller :       New socket lookup architecture.
      22                 :            :  *                                      This code is dedicated to John Dyson.
      23                 :            :  *              David S. Miller :       Change semantics of established hash,
      24                 :            :  *                                      half is devoted to TIME_WAIT sockets
      25                 :            :  *                                      and the rest go in the other half.
      26                 :            :  *              Andi Kleen :            Add support for syncookies and fixed
      27                 :            :  *                                      some bugs: ip options weren't passed to
      28                 :            :  *                                      the TCP layer, missed a check for an
      29                 :            :  *                                      ACK bit.
      30                 :            :  *              Andi Kleen :            Implemented fast path mtu discovery.
      31                 :            :  *                                      Fixed many serious bugs in the
      32                 :            :  *                                      request_sock handling and moved
      33                 :            :  *                                      most of it into the af independent code.
      34                 :            :  *                                      Added tail drop and some other bugfixes.
      35                 :            :  *                                      Added new listen semantics.
      36                 :            :  *              Mike McLagan    :       Routing by source
      37                 :            :  *      Juan Jose Ciarlante:            ip_dynaddr bits
      38                 :            :  *              Andi Kleen:             various fixes.
      39                 :            :  *      Vitaly E. Lavrov        :       Transparent proxy revived after year
      40                 :            :  *                                      coma.
      41                 :            :  *      Andi Kleen              :       Fix new listen.
      42                 :            :  *      Andi Kleen              :       Fix accept error reporting.
      43                 :            :  *      YOSHIFUJI Hideaki @USAGI and:   Support IPV6_V6ONLY socket option, which
      44                 :            :  *      Alexey Kuznetsov                allow both IPv4 and IPv6 sockets to bind
      45                 :            :  *                                      a single port at the same time.
      46                 :            :  */
      47                 :            : 
      48                 :            : #define pr_fmt(fmt) "TCP: " fmt
      49                 :            : 
      50                 :            : #include <linux/bottom_half.h>
      51                 :            : #include <linux/types.h>
      52                 :            : #include <linux/fcntl.h>
      53                 :            : #include <linux/module.h>
      54                 :            : #include <linux/random.h>
      55                 :            : #include <linux/cache.h>
      56                 :            : #include <linux/jhash.h>
      57                 :            : #include <linux/init.h>
      58                 :            : #include <linux/times.h>
      59                 :            : #include <linux/slab.h>
      60                 :            : 
      61                 :            : #include <net/net_namespace.h>
      62                 :            : #include <net/icmp.h>
      63                 :            : #include <net/inet_hashtables.h>
      64                 :            : #include <net/tcp.h>
      65                 :            : #include <net/transp_v6.h>
      66                 :            : #include <net/ipv6.h>
      67                 :            : #include <net/inet_common.h>
      68                 :            : #include <net/timewait_sock.h>
      69                 :            : #include <net/xfrm.h>
      70                 :            : #include <net/secure_seq.h>
      71                 :            : #include <net/busy_poll.h>
      72                 :            : 
      73                 :            : #include <linux/inet.h>
      74                 :            : #include <linux/ipv6.h>
      75                 :            : #include <linux/stddef.h>
      76                 :            : #include <linux/proc_fs.h>
      77                 :            : #include <linux/seq_file.h>
      78                 :            : #include <linux/inetdevice.h>
      79                 :            : 
      80                 :            : #include <crypto/hash.h>
      81                 :            : #include <linux/scatterlist.h>
      82                 :            : 
      83                 :            : #include <trace/events/tcp.h>
      84                 :            : 
      85                 :            : #ifdef CONFIG_TCP_MD5SIG
      86                 :            : static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
      87                 :            :                                __be32 daddr, __be32 saddr, const struct tcphdr *th);
      88                 :            : #endif
      89                 :            : 
      90                 :            : struct inet_hashinfo tcp_hashinfo;
      91                 :            : EXPORT_SYMBOL(tcp_hashinfo);
      92                 :            : 
      93                 :          0 : static u32 tcp_v4_init_seq(const struct sk_buff *skb)
      94                 :            : {
      95                 :          0 :         return secure_tcp_seq(ip_hdr(skb)->daddr,
      96                 :            :                               ip_hdr(skb)->saddr,
      97                 :            :                               tcp_hdr(skb)->dest,
      98                 :            :                               tcp_hdr(skb)->source);
      99                 :            : }
     100                 :            : 
     101                 :          0 : static u32 tcp_v4_init_ts_off(const struct net *net, const struct sk_buff *skb)
     102                 :            : {
     103                 :          0 :         return secure_tcp_ts_off(net, ip_hdr(skb)->daddr, ip_hdr(skb)->saddr);
     104                 :            : }
     105                 :            : 
     106                 :          0 : int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
     107                 :            : {
     108                 :            :         const struct inet_timewait_sock *tw = inet_twsk(sktw);
     109                 :            :         const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
     110                 :            :         struct tcp_sock *tp = tcp_sk(sk);
     111                 :          0 :         int reuse = sock_net(sk)->ipv4.sysctl_tcp_tw_reuse;
     112                 :            : 
     113         [ #  # ]:          0 :         if (reuse == 2) {
     114                 :            :                 /* Still does not detect *everything* that goes through
     115                 :            :                  * lo, since we require a loopback src or dst address
     116                 :            :                  * or direct binding to 'lo' interface.
     117                 :            :                  */
     118                 :            :                 bool loopback = false;
     119         [ #  # ]:          0 :                 if (tw->tw_bound_dev_if == LOOPBACK_IFINDEX)
     120                 :            :                         loopback = true;
     121                 :            : #if IS_ENABLED(CONFIG_IPV6)
     122         [ #  # ]:          0 :                 if (tw->tw_family == AF_INET6) {
     123   [ #  #  #  # ]:          0 :                         if (ipv6_addr_loopback(&tw->tw_v6_daddr) ||
     124         [ #  # ]:          0 :                             (ipv6_addr_v4mapped(&tw->tw_v6_daddr) &&
     125         [ #  # ]:          0 :                              (tw->tw_v6_daddr.s6_addr[12] == 127)) ||
     126         [ #  # ]:          0 :                             ipv6_addr_loopback(&tw->tw_v6_rcv_saddr) ||
     127         [ #  # ]:          0 :                             (ipv6_addr_v4mapped(&tw->tw_v6_rcv_saddr) &&
     128                 :          0 :                              (tw->tw_v6_rcv_saddr.s6_addr[12] == 127)))
     129                 :            :                                 loopback = true;
     130                 :            :                 } else
     131                 :            : #endif
     132                 :            :                 {
     133   [ #  #  #  # ]:          0 :                         if (ipv4_is_loopback(tw->tw_daddr) ||
     134                 :          0 :                             ipv4_is_loopback(tw->tw_rcv_saddr))
     135                 :            :                                 loopback = true;
     136                 :            :                 }
     137         [ #  # ]:          0 :                 if (!loopback)
     138                 :            :                         reuse = 0;
     139                 :            :         }
     140                 :            : 
     141                 :            :         /* With PAWS, it is safe from the viewpoint
     142                 :            :            of data integrity. Even without PAWS it is safe provided sequence
     143                 :            :            spaces do not overlap i.e. at data rates <= 80Mbit/sec.
     144                 :            : 
     145                 :            :            Actually, the idea is close to VJ's one, only timestamp cache is
     146                 :            :            held not per host, but per port pair and TW bucket is used as state
     147                 :            :            holder.
     148                 :            : 
     149                 :            :            If TW bucket has been already destroyed we fall back to VJ's scheme
     150                 :            :            and use initial timestamp retrieved from peer table.
     151                 :            :          */
     152   [ #  #  #  # ]:          0 :         if (tcptw->tw_ts_recent_stamp &&
     153   [ #  #  #  # ]:          0 :             (!twp || (reuse && time_after32(ktime_get_seconds(),
     154                 :            :                                             tcptw->tw_ts_recent_stamp)))) {
     155                 :            :                 /* In case of repair and re-using TIME-WAIT sockets we still
     156                 :            :                  * want to be sure that it is safe as above but honor the
     157                 :            :                  * sequence numbers and time stamps set as part of the repair
     158                 :            :                  * process.
     159                 :            :                  *
     160                 :            :                  * Without this check re-using a TIME-WAIT socket with TCP
     161                 :            :                  * repair would accumulate a -1 on the repair assigned
     162                 :            :                  * sequence number. The first time it is reused the sequence
     163                 :            :                  * is -1, the second time -2, etc. This fixes that issue
     164                 :            :                  * without appearing to create any others.
     165                 :            :                  */
     166         [ #  # ]:          0 :                 if (likely(!tp->repair)) {
     167                 :          0 :                         u32 seq = tcptw->tw_snd_nxt + 65535 + 2;
     168                 :            : 
     169         [ #  # ]:          0 :                         if (!seq)
     170                 :            :                                 seq = 1;
     171                 :            :                         WRITE_ONCE(tp->write_seq, seq);
     172                 :          0 :                         tp->rx_opt.ts_recent    = tcptw->tw_ts_recent;
     173                 :          0 :                         tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
     174                 :            :                 }
     175                 :            :                 sock_hold(sktw);
     176                 :          0 :                 return 1;
     177                 :            :         }
     178                 :            : 
     179                 :            :         return 0;
     180                 :            : }
     181                 :            : EXPORT_SYMBOL_GPL(tcp_twsk_unique);
     182                 :            : 
     183                 :          0 : static int tcp_v4_pre_connect(struct sock *sk, struct sockaddr *uaddr,
     184                 :            :                               int addr_len)
     185                 :            : {
     186                 :            :         /* This check is replicated from tcp_v4_connect() and intended to
     187                 :            :          * prevent BPF program called below from accessing bytes that are out
     188                 :            :          * of the bound specified by user in addr_len.
     189                 :            :          */
     190         [ #  # ]:          0 :         if (addr_len < sizeof(struct sockaddr_in))
     191                 :            :                 return -EINVAL;
     192                 :            : 
     193                 :            :         sock_owned_by_me(sk);
     194                 :            : 
     195         [ #  # ]:          0 :         return BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr);
     196                 :            : }
     197                 :            : 
     198                 :            : /* This will initiate an outgoing connection. */
     199                 :          0 : int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
     200                 :            : {
     201                 :            :         struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
     202                 :            :         struct inet_sock *inet = inet_sk(sk);
     203                 :            :         struct tcp_sock *tp = tcp_sk(sk);
     204                 :            :         __be16 orig_sport, orig_dport;
     205                 :            :         __be32 daddr, nexthop;
     206                 :            :         struct flowi4 *fl4;
     207                 :            :         struct rtable *rt;
     208                 :            :         int err;
     209                 :            :         struct ip_options_rcu *inet_opt;
     210                 :          0 :         struct inet_timewait_death_row *tcp_death_row = &sock_net(sk)->ipv4.tcp_death_row;
     211                 :            : 
     212         [ #  # ]:          0 :         if (addr_len < sizeof(struct sockaddr_in))
     213                 :            :                 return -EINVAL;
     214                 :            : 
     215         [ #  # ]:          0 :         if (usin->sin_family != AF_INET)
     216                 :            :                 return -EAFNOSUPPORT;
     217                 :            : 
     218                 :          0 :         nexthop = daddr = usin->sin_addr.s_addr;
     219                 :          0 :         inet_opt = rcu_dereference_protected(inet->inet_opt,
     220                 :            :                                              lockdep_sock_is_held(sk));
     221   [ #  #  #  # ]:          0 :         if (inet_opt && inet_opt->opt.srr) {
     222         [ #  # ]:          0 :                 if (!daddr)
     223                 :            :                         return -EINVAL;
     224                 :          0 :                 nexthop = inet_opt->opt.faddr;
     225                 :            :         }
     226                 :            : 
     227                 :          0 :         orig_sport = inet->inet_sport;
     228                 :          0 :         orig_dport = usin->sin_port;
     229                 :          0 :         fl4 = &inet->cork.fl.u.ip4;
     230                 :          0 :         rt = ip_route_connect(fl4, nexthop, inet->inet_saddr,
     231                 :          0 :                               RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
     232                 :            :                               IPPROTO_TCP,
     233                 :            :                               orig_sport, orig_dport, sk);
     234         [ #  # ]:          0 :         if (IS_ERR(rt)) {
     235                 :          0 :                 err = PTR_ERR(rt);
     236         [ #  # ]:          0 :                 if (err == -ENETUNREACH)
     237                 :          0 :                         IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
     238                 :          0 :                 return err;
     239                 :            :         }
     240                 :            : 
     241         [ #  # ]:          0 :         if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
     242                 :            :                 ip_rt_put(rt);
     243                 :          0 :                 return -ENETUNREACH;
     244                 :            :         }
     245                 :            : 
     246   [ #  #  #  # ]:          0 :         if (!inet_opt || !inet_opt->opt.srr)
     247                 :          0 :                 daddr = fl4->daddr;
     248                 :            : 
     249         [ #  # ]:          0 :         if (!inet->inet_saddr)
     250                 :          0 :                 inet->inet_saddr = fl4->saddr;
     251                 :          0 :         sk_rcv_saddr_set(sk, inet->inet_saddr);
     252                 :            : 
     253   [ #  #  #  # ]:          0 :         if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) {
     254                 :            :                 /* Reset inherited state */
     255                 :          0 :                 tp->rx_opt.ts_recent    = 0;
     256                 :          0 :                 tp->rx_opt.ts_recent_stamp = 0;
     257         [ #  # ]:          0 :                 if (likely(!tp->repair))
     258                 :            :                         WRITE_ONCE(tp->write_seq, 0);
     259                 :            :         }
     260                 :            : 
     261                 :          0 :         inet->inet_dport = usin->sin_port;
     262                 :            :         sk_daddr_set(sk, daddr);
     263                 :            : 
     264                 :          0 :         inet_csk(sk)->icsk_ext_hdr_len = 0;
     265         [ #  # ]:          0 :         if (inet_opt)
     266                 :          0 :                 inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
     267                 :            : 
     268                 :          0 :         tp->rx_opt.mss_clamp = TCP_MSS_DEFAULT;
     269                 :            : 
     270                 :            :         /* Socket identity is still unknown (sport may be zero).
     271                 :            :          * However we set state to SYN-SENT and not releasing socket
     272                 :            :          * lock select source port, enter ourselves into the hash tables and
     273                 :            :          * complete initialization after this.
     274                 :            :          */
     275                 :          0 :         tcp_set_state(sk, TCP_SYN_SENT);
     276                 :          0 :         err = inet_hash_connect(tcp_death_row, sk);
     277         [ #  # ]:          0 :         if (err)
     278                 :            :                 goto failure;
     279                 :            : 
     280                 :            :         sk_set_txhash(sk);
     281                 :            : 
     282                 :          0 :         rt = ip_route_newports(fl4, rt, orig_sport, orig_dport,
     283                 :            :                                inet->inet_sport, inet->inet_dport, sk);
     284         [ #  # ]:          0 :         if (IS_ERR(rt)) {
     285                 :          0 :                 err = PTR_ERR(rt);
     286                 :            :                 rt = NULL;
     287                 :          0 :                 goto failure;
     288                 :            :         }
     289                 :            :         /* OK, now commit destination to socket.  */
     290                 :          0 :         sk->sk_gso_type = SKB_GSO_TCPV4;
     291                 :          0 :         sk_setup_caps(sk, &rt->dst);
     292                 :            :         rt = NULL;
     293                 :            : 
     294         [ #  # ]:          0 :         if (likely(!tp->repair)) {
     295         [ #  # ]:          0 :                 if (!tp->write_seq)
     296                 :          0 :                         WRITE_ONCE(tp->write_seq,
     297                 :            :                                    secure_tcp_seq(inet->inet_saddr,
     298                 :            :                                                   inet->inet_daddr,
     299                 :            :                                                   inet->inet_sport,
     300                 :            :                                                   usin->sin_port));
     301                 :          0 :                 tp->tsoffset = secure_tcp_ts_off(sock_net(sk),
     302                 :            :                                                  inet->inet_saddr,
     303                 :            :                                                  inet->inet_daddr);
     304                 :            :         }
     305                 :            : 
     306                 :          0 :         inet->inet_id = prandom_u32();
     307                 :            : 
     308         [ #  # ]:          0 :         if (tcp_fastopen_defer_connect(sk, &err))
     309                 :          0 :                 return err;
     310         [ #  # ]:          0 :         if (err)
     311                 :            :                 goto failure;
     312                 :            : 
     313                 :          0 :         err = tcp_connect(sk);
     314                 :            : 
     315         [ #  # ]:          0 :         if (err)
     316                 :            :                 goto failure;
     317                 :            : 
     318                 :            :         return 0;
     319                 :            : 
     320                 :            : failure:
     321                 :            :         /*
     322                 :            :          * This unhashes the socket and releases the local port,
     323                 :            :          * if necessary.
     324                 :            :          */
     325                 :          0 :         tcp_set_state(sk, TCP_CLOSE);
     326                 :            :         ip_rt_put(rt);
     327                 :          0 :         sk->sk_route_caps = 0;
     328                 :          0 :         inet->inet_dport = 0;
     329                 :          0 :         return err;
     330                 :            : }
     331                 :            : EXPORT_SYMBOL(tcp_v4_connect);
     332                 :            : 
     333                 :            : /*
     334                 :            :  * This routine reacts to ICMP_FRAG_NEEDED mtu indications as defined in RFC1191.
     335                 :            :  * It can be called through tcp_release_cb() if socket was owned by user
     336                 :            :  * at the time tcp_v4_err() was called to handle ICMP message.
     337                 :            :  */
     338                 :          0 : void tcp_v4_mtu_reduced(struct sock *sk)
     339                 :            : {
     340                 :            :         struct inet_sock *inet = inet_sk(sk);
     341                 :            :         struct dst_entry *dst;
     342                 :            :         u32 mtu;
     343                 :            : 
     344         [ #  # ]:          0 :         if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
     345                 :            :                 return;
     346                 :          0 :         mtu = tcp_sk(sk)->mtu_info;
     347                 :          0 :         dst = inet_csk_update_pmtu(sk, mtu);
     348         [ #  # ]:          0 :         if (!dst)
     349                 :            :                 return;
     350                 :            : 
     351                 :            :         /* Something is about to be wrong... Remember soft error
     352                 :            :          * for the case, if this connection will not able to recover.
     353                 :            :          */
     354   [ #  #  #  # ]:          0 :         if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
     355                 :          0 :                 sk->sk_err_soft = EMSGSIZE;
     356                 :            : 
     357                 :            :         mtu = dst_mtu(dst);
     358                 :            : 
     359   [ #  #  #  # ]:          0 :         if (inet->pmtudisc != IP_PMTUDISC_DONT &&
     360         [ #  # ]:          0 :             ip_sk_accept_pmtu(sk) &&
     361                 :          0 :             inet_csk(sk)->icsk_pmtu_cookie > mtu) {
     362                 :          0 :                 tcp_sync_mss(sk, mtu);
     363                 :            : 
     364                 :            :                 /* Resend the TCP packet because it's
     365                 :            :                  * clear that the old packet has been
     366                 :            :                  * dropped. This is the new "fast" path mtu
     367                 :            :                  * discovery.
     368                 :            :                  */
     369                 :          0 :                 tcp_simple_retransmit(sk);
     370                 :            :         } /* else let the usual retransmit timer handle it */
     371                 :            : }
     372                 :            : EXPORT_SYMBOL(tcp_v4_mtu_reduced);
     373                 :            : 
     374                 :          0 : static void do_redirect(struct sk_buff *skb, struct sock *sk)
     375                 :            : {
     376                 :          0 :         struct dst_entry *dst = __sk_dst_check(sk, 0);
     377                 :            : 
     378         [ #  # ]:          0 :         if (dst)
     379                 :          0 :                 dst->ops->redirect(dst, sk, skb);
     380                 :          0 : }
     381                 :            : 
     382                 :            : 
     383                 :            : /* handle ICMP messages on TCP_NEW_SYN_RECV request sockets */
     384                 :          0 : void tcp_req_err(struct sock *sk, u32 seq, bool abort)
     385                 :            : {
     386                 :            :         struct request_sock *req = inet_reqsk(sk);
     387                 :            :         struct net *net = sock_net(sk);
     388                 :            : 
     389                 :            :         /* ICMPs are not backlogged, hence we cannot get
     390                 :            :          * an established socket here.
     391                 :            :          */
     392         [ #  # ]:          0 :         if (seq != tcp_rsk(req)->snt_isn) {
     393                 :          0 :                 __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
     394         [ #  # ]:          0 :         } else if (abort) {
     395                 :            :                 /*
     396                 :            :                  * Still in SYN_RECV, just remove it silently.
     397                 :            :                  * There is no good way to pass the error to the newly
     398                 :            :                  * created socket, and POSIX does not want network
     399                 :            :                  * errors returned from accept().
     400                 :            :                  */
     401                 :          0 :                 inet_csk_reqsk_queue_drop(req->rsk_listener, req);
     402                 :          0 :                 tcp_listendrop(req->rsk_listener);
     403                 :            :         }
     404                 :          0 :         reqsk_put(req);
     405                 :          0 : }
     406                 :            : EXPORT_SYMBOL(tcp_req_err);
     407                 :            : 
     408                 :            : /*
     409                 :            :  * This routine is called by the ICMP module when it gets some
     410                 :            :  * sort of error condition.  If err < 0 then the socket should
     411                 :            :  * be closed and the error returned to the user.  If err > 0
     412                 :            :  * it's just the icmp type << 8 | icmp code.  After adjustment
     413                 :            :  * header points to the first 8 bytes of the tcp header.  We need
     414                 :            :  * to find the appropriate port.
     415                 :            :  *
     416                 :            :  * The locking strategy used here is very "optimistic". When
     417                 :            :  * someone else accesses the socket the ICMP is just dropped
     418                 :            :  * and for some paths there is no check at all.
     419                 :            :  * A more general error queue to queue errors for later handling
     420                 :            :  * is probably better.
     421                 :            :  *
     422                 :            :  */
     423                 :            : 
     424                 :          0 : int tcp_v4_err(struct sk_buff *icmp_skb, u32 info)
     425                 :            : {
     426                 :          0 :         const struct iphdr *iph = (const struct iphdr *)icmp_skb->data;
     427                 :          0 :         struct tcphdr *th = (struct tcphdr *)(icmp_skb->data + (iph->ihl << 2));
     428                 :            :         struct inet_connection_sock *icsk;
     429                 :            :         struct tcp_sock *tp;
     430                 :            :         struct inet_sock *inet;
     431                 :          0 :         const int type = icmp_hdr(icmp_skb)->type;
     432                 :          0 :         const int code = icmp_hdr(icmp_skb)->code;
     433                 :            :         struct sock *sk;
     434                 :            :         struct sk_buff *skb;
     435                 :            :         struct request_sock *fastopen;
     436                 :            :         u32 seq, snd_una;
     437                 :            :         s32 remaining;
     438                 :            :         u32 delta_us;
     439                 :            :         int err;
     440                 :          0 :         struct net *net = dev_net(icmp_skb->dev);
     441                 :            : 
     442                 :          0 :         sk = __inet_lookup_established(net, &tcp_hashinfo, iph->daddr,
     443                 :            :                                        th->dest, iph->saddr, ntohs(th->source),
     444                 :            :                                        inet_iif(icmp_skb), 0);
     445         [ #  # ]:          0 :         if (!sk) {
     446                 :          0 :                 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
     447                 :          0 :                 return -ENOENT;
     448                 :            :         }
     449         [ #  # ]:          0 :         if (sk->sk_state == TCP_TIME_WAIT) {
     450                 :          0 :                 inet_twsk_put(inet_twsk(sk));
     451                 :          0 :                 return 0;
     452                 :            :         }
     453                 :          0 :         seq = ntohl(th->seq);
     454         [ #  # ]:          0 :         if (sk->sk_state == TCP_NEW_SYN_RECV) {
     455                 :          0 :                 tcp_req_err(sk, seq, type == ICMP_PARAMETERPROB ||
     456   [ #  #  #  # ]:          0 :                                      type == ICMP_TIME_EXCEEDED ||
     457                 :          0 :                                      (type == ICMP_DEST_UNREACH &&
     458                 :          0 :                                       (code == ICMP_NET_UNREACH ||
     459                 :            :                                        code == ICMP_HOST_UNREACH)));
     460                 :          0 :                 return 0;
     461                 :            :         }
     462                 :            : 
     463                 :            :         bh_lock_sock(sk);
     464                 :            :         /* If too many ICMPs get dropped on busy
     465                 :            :          * servers this needs to be solved differently.
     466                 :            :          * We do take care of PMTU discovery (RFC1191) special case :
     467                 :            :          * we can receive locally generated ICMP messages while socket is held.
     468                 :            :          */
     469         [ #  # ]:          0 :         if (sock_owned_by_user(sk)) {
     470         [ #  # ]:          0 :                 if (!(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED))
     471                 :          0 :                         __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
     472                 :            :         }
     473         [ #  # ]:          0 :         if (sk->sk_state == TCP_CLOSE)
     474                 :            :                 goto out;
     475                 :            : 
     476         [ #  # ]:          0 :         if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) {
     477                 :          0 :                 __NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
     478                 :            :                 goto out;
     479                 :            :         }
     480                 :            : 
     481                 :            :         icsk = inet_csk(sk);
     482                 :            :         tp = tcp_sk(sk);
     483                 :            :         /* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
     484                 :          0 :         fastopen = rcu_dereference(tp->fastopen_rsk);
     485         [ #  # ]:          0 :         snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una;
     486   [ #  #  #  # ]:          0 :         if (sk->sk_state != TCP_LISTEN &&
     487                 :          0 :             !between(seq, snd_una, tp->snd_nxt)) {
     488                 :          0 :                 __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
     489                 :            :                 goto out;
     490                 :            :         }
     491                 :            : 
     492   [ #  #  #  #  :          0 :         switch (type) {
                      # ]
     493                 :            :         case ICMP_REDIRECT:
     494         [ #  # ]:          0 :                 if (!sock_owned_by_user(sk))
     495                 :          0 :                         do_redirect(icmp_skb, sk);
     496                 :            :                 goto out;
     497                 :            :         case ICMP_SOURCE_QUENCH:
     498                 :            :                 /* Just silently ignore these. */
     499                 :            :                 goto out;
     500                 :            :         case ICMP_PARAMETERPROB:
     501                 :            :                 err = EPROTO;
     502                 :            :                 break;
     503                 :            :         case ICMP_DEST_UNREACH:
     504         [ #  # ]:          0 :                 if (code > NR_ICMP_UNREACH)
     505                 :            :                         goto out;
     506                 :            : 
     507         [ #  # ]:          0 :                 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
     508                 :            :                         /* We are not interested in TCP_LISTEN and open_requests
     509                 :            :                          * (SYN-ACKs send out by Linux are always <576bytes so
     510                 :            :                          * they should go through unfragmented).
     511                 :            :                          */
     512         [ #  # ]:          0 :                         if (sk->sk_state == TCP_LISTEN)
     513                 :            :                                 goto out;
     514                 :            : 
     515                 :          0 :                         tp->mtu_info = info;
     516         [ #  # ]:          0 :                         if (!sock_owned_by_user(sk)) {
     517                 :          0 :                                 tcp_v4_mtu_reduced(sk);
     518                 :            :                         } else {
     519         [ #  # ]:          0 :                                 if (!test_and_set_bit(TCP_MTU_REDUCED_DEFERRED, &sk->sk_tsq_flags))
     520                 :            :                                         sock_hold(sk);
     521                 :            :                         }
     522                 :            :                         goto out;
     523                 :            :                 }
     524                 :            : 
     525                 :          0 :                 err = icmp_err_convert[code].errno;
     526                 :            :                 /* check if icmp_skb allows revert of backoff
     527                 :            :                  * (see draft-zimmermann-tcp-lcd) */
     528         [ #  # ]:          0 :                 if (code != ICMP_NET_UNREACH && code != ICMP_HOST_UNREACH)
     529                 :            :                         break;
     530   [ #  #  #  #  :          0 :                 if (seq != tp->snd_una  || !icsk->icsk_retransmits ||
                   #  # ]
     531         [ #  # ]:          0 :                     !icsk->icsk_backoff || fastopen)
     532                 :            :                         break;
     533                 :            : 
     534         [ #  # ]:          0 :                 if (sock_owned_by_user(sk))
     535                 :            :                         break;
     536                 :            : 
     537                 :            :                 skb = tcp_rtx_queue_head(sk);
     538   [ #  #  #  #  :          0 :                 if (WARN_ON_ONCE(!skb))
                   #  # ]
     539                 :            :                         break;
     540                 :            : 
     541                 :          0 :                 icsk->icsk_backoff--;
     542         [ #  # ]:          0 :                 icsk->icsk_rto = tp->srtt_us ? __tcp_set_rto(tp) :
     543                 :            :                                                TCP_TIMEOUT_INIT;
     544                 :          0 :                 icsk->icsk_rto = inet_csk_rto_backoff(icsk, TCP_RTO_MAX);
     545                 :            : 
     546                 :            : 
     547                 :          0 :                 tcp_mstamp_refresh(tp);
     548                 :          0 :                 delta_us = (u32)(tp->tcp_mstamp - tcp_skb_timestamp_us(skb));
     549                 :          0 :                 remaining = icsk->icsk_rto -
     550                 :            :                             usecs_to_jiffies(delta_us);
     551                 :            : 
     552         [ #  # ]:          0 :                 if (remaining > 0) {
     553                 :          0 :                         inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
     554                 :            :                                                   remaining, TCP_RTO_MAX);
     555                 :            :                 } else {
     556                 :            :                         /* RTO revert clocked out retransmission.
     557                 :            :                          * Will retransmit now */
     558                 :          0 :                         tcp_retransmit_timer(sk);
     559                 :            :                 }
     560                 :            : 
     561                 :            :                 break;
     562                 :            :         case ICMP_TIME_EXCEEDED:
     563                 :            :                 err = EHOSTUNREACH;
     564                 :          0 :                 break;
     565                 :            :         default:
     566                 :            :                 goto out;
     567                 :            :         }
     568                 :            : 
     569         [ #  # ]:          0 :         switch (sk->sk_state) {
     570                 :            :         case TCP_SYN_SENT:
     571                 :            :         case TCP_SYN_RECV:
     572                 :            :                 /* Only in fast or simultaneous open. If a fast open socket is
     573                 :            :                  * is already accepted it is treated as a connected one below.
     574                 :            :                  */
     575   [ #  #  #  # ]:          0 :                 if (fastopen && !fastopen->sk)
     576                 :            :                         break;
     577                 :            : 
     578         [ #  # ]:          0 :                 if (!sock_owned_by_user(sk)) {
     579                 :          0 :                         sk->sk_err = err;
     580                 :            : 
     581                 :          0 :                         sk->sk_error_report(sk);
     582                 :            : 
     583                 :          0 :                         tcp_done(sk);
     584                 :            :                 } else {
     585                 :          0 :                         sk->sk_err_soft = err;
     586                 :            :                 }
     587                 :            :                 goto out;
     588                 :            :         }
     589                 :            : 
     590                 :            :         /* If we've already connected we will keep trying
     591                 :            :          * until we time out, or the user gives up.
     592                 :            :          *
     593                 :            :          * rfc1122 4.2.3.9 allows to consider as hard errors
     594                 :            :          * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
     595                 :            :          * but it is obsoleted by pmtu discovery).
     596                 :            :          *
     597                 :            :          * Note, that in modern internet, where routing is unreliable
     598                 :            :          * and in each dark corner broken firewalls sit, sending random
     599                 :            :          * errors ordered by their masters even this two messages finally lose
     600                 :            :          * their original sense (even Linux sends invalid PORT_UNREACHs)
     601                 :            :          *
     602                 :            :          * Now we are in compliance with RFCs.
     603                 :            :          *                                                      --ANK (980905)
     604                 :            :          */
     605                 :            : 
     606                 :            :         inet = inet_sk(sk);
     607   [ #  #  #  # ]:          0 :         if (!sock_owned_by_user(sk) && inet->recverr) {
     608                 :          0 :                 sk->sk_err = err;
     609                 :          0 :                 sk->sk_error_report(sk);
     610                 :            :         } else  { /* Only an error on timeout */
     611                 :          0 :                 sk->sk_err_soft = err;
     612                 :            :         }
     613                 :            : 
     614                 :            : out:
     615                 :            :         bh_unlock_sock(sk);
     616                 :          0 :         sock_put(sk);
     617                 :          0 :         return 0;
     618                 :            : }
     619                 :            : 
     620                 :          0 : void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr)
     621                 :            : {
     622                 :            :         struct tcphdr *th = tcp_hdr(skb);
     623                 :            : 
     624                 :          0 :         th->check = ~tcp_v4_check(skb->len, saddr, daddr, 0);
     625                 :          0 :         skb->csum_start = skb_transport_header(skb) - skb->head;
     626                 :          0 :         skb->csum_offset = offsetof(struct tcphdr, check);
     627                 :          0 : }
     628                 :            : 
     629                 :            : /* This routine computes an IPv4 TCP checksum. */
     630                 :          0 : void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)
     631                 :            : {
     632                 :            :         const struct inet_sock *inet = inet_sk(sk);
     633                 :            : 
     634                 :          0 :         __tcp_v4_send_check(skb, inet->inet_saddr, inet->inet_daddr);
     635                 :          0 : }
     636                 :            : EXPORT_SYMBOL(tcp_v4_send_check);
     637                 :            : 
     638                 :            : /*
     639                 :            :  *      This routine will send an RST to the other tcp.
     640                 :            :  *
     641                 :            :  *      Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
     642                 :            :  *                    for reset.
     643                 :            :  *      Answer: if a packet caused RST, it is not for a socket
     644                 :            :  *              existing in our system, if it is matched to a socket,
     645                 :            :  *              it is just duplicate segment or bug in other side's TCP.
     646                 :            :  *              So that we build reply only basing on parameters
     647                 :            :  *              arrived with segment.
     648                 :            :  *      Exception: precedence violation. We do not implement it in any case.
     649                 :            :  */
     650                 :            : 
     651                 :          2 : static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
     652                 :            : {
     653                 :            :         const struct tcphdr *th = tcp_hdr(skb);
     654                 :            :         struct {
     655                 :            :                 struct tcphdr th;
     656                 :            : #ifdef CONFIG_TCP_MD5SIG
     657                 :            :                 __be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)];
     658                 :            : #endif
     659                 :            :         } rep;
     660                 :            :         struct ip_reply_arg arg;
     661                 :            : #ifdef CONFIG_TCP_MD5SIG
     662                 :            :         struct tcp_md5sig_key *key = NULL;
     663                 :            :         const __u8 *hash_location = NULL;
     664                 :            :         unsigned char newhash[16];
     665                 :            :         int genhash;
     666                 :            :         struct sock *sk1 = NULL;
     667                 :            : #endif
     668                 :            :         u64 transmit_time = 0;
     669                 :            :         struct sock *ctl_sk;
     670                 :            :         struct net *net;
     671                 :            : 
     672                 :            :         /* Never send a reset in response to a reset. */
     673         [ +  - ]:          2 :         if (th->rst)
     674                 :          0 :                 return;
     675                 :            : 
     676                 :            :         /* If sk not NULL, it means we did a successful lookup and incoming
     677                 :            :          * route had to be correct. prequeue might have dropped our dst.
     678                 :            :          */
     679   [ +  -  +  - ]:          4 :         if (!sk && skb_rtable(skb)->rt_type != RTN_LOCAL)
     680                 :            :                 return;
     681                 :            : 
     682                 :            :         /* Swap the send and the receive. */
     683                 :          2 :         memset(&rep, 0, sizeof(rep));
     684                 :          2 :         rep.th.dest   = th->source;
     685                 :          2 :         rep.th.source = th->dest;
     686                 :          2 :         rep.th.doff   = sizeof(struct tcphdr) / 4;
     687                 :          2 :         rep.th.rst    = 1;
     688                 :            : 
     689         [ +  - ]:          2 :         if (th->ack) {
     690                 :          2 :                 rep.th.seq = th->ack_seq;
     691                 :            :         } else {
     692                 :          0 :                 rep.th.ack = 1;
     693                 :          0 :                 rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
     694                 :            :                                        skb->len - (th->doff << 2));
     695                 :            :         }
     696                 :            : 
     697                 :          2 :         memset(&arg, 0, sizeof(arg));
     698                 :          2 :         arg.iov[0].iov_base = (unsigned char *)&rep;
     699                 :          2 :         arg.iov[0].iov_len  = sizeof(rep.th);
     700                 :            : 
     701         [ -  + ]:          4 :         net = sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev);
     702                 :            : #ifdef CONFIG_TCP_MD5SIG
     703                 :            :         rcu_read_lock();
     704                 :            :         hash_location = tcp_parse_md5sig_option(th);
     705                 :            :         if (sk && sk_fullsock(sk)) {
     706                 :            :                 key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)
     707                 :            :                                         &ip_hdr(skb)->saddr, AF_INET);
     708                 :            :         } else if (hash_location) {
     709                 :            :                 /*
     710                 :            :                  * active side is lost. Try to find listening socket through
     711                 :            :                  * source port, and then find md5 key through listening socket.
     712                 :            :                  * we are not loose security here:
     713                 :            :                  * Incoming packet is checked with md5 hash with finding key,
     714                 :            :                  * no RST generated if md5 hash doesn't match.
     715                 :            :                  */
     716                 :            :                 sk1 = __inet_lookup_listener(net, &tcp_hashinfo, NULL, 0,
     717                 :            :                                              ip_hdr(skb)->saddr,
     718                 :            :                                              th->source, ip_hdr(skb)->daddr,
     719                 :            :                                              ntohs(th->source), inet_iif(skb),
     720                 :            :                                              tcp_v4_sdif(skb));
     721                 :            :                 /* don't send rst if it can't find key */
     722                 :            :                 if (!sk1)
     723                 :            :                         goto out;
     724                 :            : 
     725                 :            :                 key = tcp_md5_do_lookup(sk1, (union tcp_md5_addr *)
     726                 :            :                                         &ip_hdr(skb)->saddr, AF_INET);
     727                 :            :                 if (!key)
     728                 :            :                         goto out;
     729                 :            : 
     730                 :            : 
     731                 :            :                 genhash = tcp_v4_md5_hash_skb(newhash, key, NULL, skb);
     732                 :            :                 if (genhash || memcmp(hash_location, newhash, 16) != 0)
     733                 :            :                         goto out;
     734                 :            : 
     735                 :            :         }
     736                 :            : 
     737                 :            :         if (key) {
     738                 :            :                 rep.opt[0] = htonl((TCPOPT_NOP << 24) |
     739                 :            :                                    (TCPOPT_NOP << 16) |
     740                 :            :                                    (TCPOPT_MD5SIG << 8) |
     741                 :            :                                    TCPOLEN_MD5SIG);
     742                 :            :                 /* Update length and the length the header thinks exists */
     743                 :            :                 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
     744                 :            :                 rep.th.doff = arg.iov[0].iov_len / 4;
     745                 :            : 
     746                 :            :                 tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[1],
     747                 :            :                                      key, ip_hdr(skb)->saddr,
     748                 :            :                                      ip_hdr(skb)->daddr, &rep.th);
     749                 :            :         }
     750                 :            : #endif
     751                 :          4 :         arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
     752                 :            :                                       ip_hdr(skb)->saddr, /* XXX */
     753                 :            :                                       arg.iov[0].iov_len, IPPROTO_TCP, 0);
     754                 :          2 :         arg.csumoffset = offsetof(struct tcphdr, check) / 2;
     755   [ -  +  #  # ]:          2 :         arg.flags = (sk && inet_sk_transparent(sk)) ? IP_REPLY_ARG_NOSRCCHECK : 0;
     756                 :            : 
     757                 :            :         /* When socket is gone, all binding information is lost.
     758                 :            :          * routing might fail in this case. No choice here, if we choose to force
     759                 :            :          * input interface, we will misroute in case of asymmetric route.
     760                 :            :          */
     761         [ -  + ]:          2 :         if (sk) {
     762                 :          0 :                 arg.bound_dev_if = sk->sk_bound_dev_if;
     763         [ #  # ]:          0 :                 if (sk_fullsock(sk))
     764                 :          0 :                         trace_tcp_send_reset(sk, skb);
     765                 :            :         }
     766                 :            : 
     767                 :            :         BUILD_BUG_ON(offsetof(struct sock, sk_bound_dev_if) !=
     768                 :            :                      offsetof(struct inet_timewait_sock, tw_bound_dev_if));
     769                 :            : 
     770                 :          2 :         arg.tos = ip_hdr(skb)->tos;
     771   [ -  +  #  # ]:          4 :         arg.uid = sock_net_uid(net, sk && sk_fullsock(sk) ? sk : NULL);
     772                 :            :         local_bh_disable();
     773                 :          6 :         ctl_sk = this_cpu_read(*net->ipv4.tcp_sk);
     774         [ -  + ]:          2 :         if (sk) {
     775                 :          0 :                 ctl_sk->sk_mark = (sk->sk_state == TCP_TIME_WAIT) ?
     776         [ #  # ]:          0 :                                    inet_twsk(sk)->tw_mark : sk->sk_mark;
     777                 :          0 :                 ctl_sk->sk_priority = (sk->sk_state == TCP_TIME_WAIT) ?
     778         [ #  # ]:          0 :                                    inet_twsk(sk)->tw_priority : sk->sk_priority;
     779                 :          0 :                 transmit_time = tcp_transmit_time(sk);
     780                 :            :         }
     781                 :          2 :         ip_send_unicast_reply(ctl_sk,
     782                 :          2 :                               skb, &TCP_SKB_CB(skb)->header.h4.opt,
     783                 :            :                               ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
     784                 :            :                               &arg, arg.iov[0].iov_len,
     785                 :            :                               transmit_time);
     786                 :            : 
     787                 :          2 :         ctl_sk->sk_mark = 0;
     788                 :          4 :         __TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
     789                 :          4 :         __TCP_INC_STATS(net, TCP_MIB_OUTRSTS);
     790                 :            :         local_bh_enable();
     791                 :            : 
     792                 :            : #ifdef CONFIG_TCP_MD5SIG
     793                 :            : out:
     794                 :            :         rcu_read_unlock();
     795                 :            : #endif
     796                 :            : }
     797                 :            : 
     798                 :            : /* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
     799                 :            :    outside socket context is ugly, certainly. What can I do?
     800                 :            :  */
     801                 :            : 
     802                 :          0 : static void tcp_v4_send_ack(const struct sock *sk,
     803                 :            :                             struct sk_buff *skb, u32 seq, u32 ack,
     804                 :            :                             u32 win, u32 tsval, u32 tsecr, int oif,
     805                 :            :                             struct tcp_md5sig_key *key,
     806                 :            :                             int reply_flags, u8 tos)
     807                 :            : {
     808                 :            :         const struct tcphdr *th = tcp_hdr(skb);
     809                 :            :         struct {
     810                 :            :                 struct tcphdr th;
     811                 :            :                 __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
     812                 :            : #ifdef CONFIG_TCP_MD5SIG
     813                 :            :                            + (TCPOLEN_MD5SIG_ALIGNED >> 2)
     814                 :            : #endif
     815                 :            :                         ];
     816                 :            :         } rep;
     817                 :            :         struct net *net = sock_net(sk);
     818                 :            :         struct ip_reply_arg arg;
     819                 :            :         struct sock *ctl_sk;
     820                 :            :         u64 transmit_time;
     821                 :            : 
     822                 :          0 :         memset(&rep.th, 0, sizeof(struct tcphdr));
     823                 :          0 :         memset(&arg, 0, sizeof(arg));
     824                 :            : 
     825                 :          0 :         arg.iov[0].iov_base = (unsigned char *)&rep;
     826                 :          0 :         arg.iov[0].iov_len  = sizeof(rep.th);
     827         [ #  # ]:          0 :         if (tsecr) {
     828                 :          0 :                 rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
     829                 :            :                                    (TCPOPT_TIMESTAMP << 8) |
     830                 :            :                                    TCPOLEN_TIMESTAMP);
     831                 :          0 :                 rep.opt[1] = htonl(tsval);
     832                 :          0 :                 rep.opt[2] = htonl(tsecr);
     833                 :          0 :                 arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
     834                 :            :         }
     835                 :            : 
     836                 :            :         /* Swap the send and the receive. */
     837                 :          0 :         rep.th.dest    = th->source;
     838                 :          0 :         rep.th.source  = th->dest;
     839                 :          0 :         rep.th.doff    = arg.iov[0].iov_len / 4;
     840                 :          0 :         rep.th.seq     = htonl(seq);
     841                 :          0 :         rep.th.ack_seq = htonl(ack);
     842                 :          0 :         rep.th.ack     = 1;
     843                 :          0 :         rep.th.window  = htons(win);
     844                 :            : 
     845                 :            : #ifdef CONFIG_TCP_MD5SIG
     846                 :            :         if (key) {
     847                 :            :                 int offset = (tsecr) ? 3 : 0;
     848                 :            : 
     849                 :            :                 rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
     850                 :            :                                           (TCPOPT_NOP << 16) |
     851                 :            :                                           (TCPOPT_MD5SIG << 8) |
     852                 :            :                                           TCPOLEN_MD5SIG);
     853                 :            :                 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
     854                 :            :                 rep.th.doff = arg.iov[0].iov_len/4;
     855                 :            : 
     856                 :            :                 tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[offset],
     857                 :            :                                     key, ip_hdr(skb)->saddr,
     858                 :            :                                     ip_hdr(skb)->daddr, &rep.th);
     859                 :            :         }
     860                 :            : #endif
     861                 :          0 :         arg.flags = reply_flags;
     862                 :          0 :         arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
     863                 :            :                                       ip_hdr(skb)->saddr, /* XXX */
     864                 :            :                                       arg.iov[0].iov_len, IPPROTO_TCP, 0);
     865                 :          0 :         arg.csumoffset = offsetof(struct tcphdr, check) / 2;
     866         [ #  # ]:          0 :         if (oif)
     867                 :          0 :                 arg.bound_dev_if = oif;
     868                 :          0 :         arg.tos = tos;
     869         [ #  # ]:          0 :         arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL);
     870                 :            :         local_bh_disable();
     871                 :          0 :         ctl_sk = this_cpu_read(*net->ipv4.tcp_sk);
     872                 :          0 :         ctl_sk->sk_mark = (sk->sk_state == TCP_TIME_WAIT) ?
     873         [ #  # ]:          0 :                            inet_twsk(sk)->tw_mark : sk->sk_mark;
     874                 :          0 :         ctl_sk->sk_priority = (sk->sk_state == TCP_TIME_WAIT) ?
     875         [ #  # ]:          0 :                            inet_twsk(sk)->tw_priority : sk->sk_priority;
     876                 :          0 :         transmit_time = tcp_transmit_time(sk);
     877                 :          0 :         ip_send_unicast_reply(ctl_sk,
     878                 :          0 :                               skb, &TCP_SKB_CB(skb)->header.h4.opt,
     879                 :            :                               ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
     880                 :            :                               &arg, arg.iov[0].iov_len,
     881                 :            :                               transmit_time);
     882                 :            : 
     883                 :          0 :         ctl_sk->sk_mark = 0;
     884                 :          0 :         __TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
     885                 :            :         local_bh_enable();
     886                 :          0 : }
     887                 :            : 
     888                 :          0 : static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
     889                 :            : {
     890                 :            :         struct inet_timewait_sock *tw = inet_twsk(sk);
     891                 :            :         struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
     892                 :            : 
     893                 :          0 :         tcp_v4_send_ack(sk, skb,
     894                 :            :                         tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
     895                 :          0 :                         tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
     896                 :          0 :                         tcp_time_stamp_raw() + tcptw->tw_ts_offset,
     897                 :            :                         tcptw->tw_ts_recent,
     898                 :            :                         tw->tw_bound_dev_if,
     899                 :            :                         tcp_twsk_md5_key(tcptw),
     900                 :          0 :                         tw->tw_transparent ? IP_REPLY_ARG_NOSRCCHECK : 0,
     901                 :            :                         tw->tw_tos
     902                 :            :                         );
     903                 :            : 
     904                 :          0 :         inet_twsk_put(tw);
     905                 :          0 : }
     906                 :            : 
     907                 :          0 : static void tcp_v4_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
     908                 :            :                                   struct request_sock *req)
     909                 :            : {
     910                 :            :         /* sk->sk_state == TCP_LISTEN -> for regular TCP_SYN_RECV
     911                 :            :          * sk->sk_state == TCP_SYN_RECV -> for Fast Open.
     912                 :            :          */
     913         [ #  # ]:          0 :         u32 seq = (sk->sk_state == TCP_LISTEN) ? tcp_rsk(req)->snt_isn + 1 :
     914                 :            :                                              tcp_sk(sk)->snd_nxt;
     915                 :            : 
     916                 :            :         /* RFC 7323 2.3
     917                 :            :          * The window field (SEG.WND) of every outgoing segment, with the
     918                 :            :          * exception of <SYN> segments, MUST be right-shifted by
     919                 :            :          * Rcv.Wind.Shift bits:
     920                 :            :          */
     921                 :          0 :         tcp_v4_send_ack(sk, skb, seq,
     922                 :            :                         tcp_rsk(req)->rcv_nxt,
     923                 :          0 :                         req->rsk_rcv_wnd >> inet_rsk(req)->rcv_wscale,
     924                 :          0 :                         tcp_time_stamp_raw() + tcp_rsk(req)->ts_off,
     925                 :            :                         req->ts_recent,
     926                 :            :                         0,
     927                 :            :                         tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&ip_hdr(skb)->saddr,
     928                 :            :                                           AF_INET),
     929                 :          0 :                         inet_rsk(req)->no_srccheck ? IP_REPLY_ARG_NOSRCCHECK : 0,
     930                 :            :                         ip_hdr(skb)->tos);
     931                 :          0 : }
     932                 :            : 
     933                 :            : /*
     934                 :            :  *      Send a SYN-ACK after having received a SYN.
     935                 :            :  *      This still operates on a request_sock only, not on a big
     936                 :            :  *      socket.
     937                 :            :  */
     938                 :          0 : static int tcp_v4_send_synack(const struct sock *sk, struct dst_entry *dst,
     939                 :            :                               struct flowi *fl,
     940                 :            :                               struct request_sock *req,
     941                 :            :                               struct tcp_fastopen_cookie *foc,
     942                 :            :                               enum tcp_synack_type synack_type)
     943                 :            : {
     944                 :            :         const struct inet_request_sock *ireq = inet_rsk(req);
     945                 :            :         struct flowi4 fl4;
     946                 :            :         int err = -1;
     947                 :            :         struct sk_buff *skb;
     948                 :            : 
     949                 :            :         /* First, grab a route. */
     950   [ #  #  #  # ]:          0 :         if (!dst && (dst = inet_csk_route_req(sk, &fl4, req)) == NULL)
     951                 :            :                 return -1;
     952                 :            : 
     953                 :          0 :         skb = tcp_make_synack(sk, dst, req, foc, synack_type);
     954                 :            : 
     955         [ #  # ]:          0 :         if (skb) {
     956                 :          0 :                 __tcp_v4_send_check(skb, ireq->ir_loc_addr, ireq->ir_rmt_addr);
     957                 :            : 
     958                 :            :                 rcu_read_lock();
     959                 :          0 :                 err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
     960                 :            :                                             ireq->ir_rmt_addr,
     961                 :          0 :                                             rcu_dereference(ireq->ireq_opt));
     962                 :            :                 rcu_read_unlock();
     963         [ #  # ]:          0 :                 err = net_xmit_eval(err);
     964                 :            :         }
     965                 :            : 
     966                 :          0 :         return err;
     967                 :            : }
     968                 :            : 
     969                 :            : /*
     970                 :            :  *      IPv4 request_sock destructor.
     971                 :            :  */
     972                 :          0 : static void tcp_v4_reqsk_destructor(struct request_sock *req)
     973                 :            : {
     974                 :          0 :         kfree(rcu_dereference_protected(inet_rsk(req)->ireq_opt, 1));
     975                 :          0 : }
     976                 :            : 
     977                 :            : #ifdef CONFIG_TCP_MD5SIG
     978                 :            : /*
     979                 :            :  * RFC2385 MD5 checksumming requires a mapping of
     980                 :            :  * IP address->MD5 Key.
     981                 :            :  * We need to maintain these in the sk structure.
     982                 :            :  */
     983                 :            : 
     984                 :            : DEFINE_STATIC_KEY_FALSE(tcp_md5_needed);
     985                 :            : EXPORT_SYMBOL(tcp_md5_needed);
     986                 :            : 
     987                 :            : /* Find the Key structure for an address.  */
     988                 :            : struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk,
     989                 :            :                                            const union tcp_md5_addr *addr,
     990                 :            :                                            int family)
     991                 :            : {
     992                 :            :         const struct tcp_sock *tp = tcp_sk(sk);
     993                 :            :         struct tcp_md5sig_key *key;
     994                 :            :         const struct tcp_md5sig_info *md5sig;
     995                 :            :         __be32 mask;
     996                 :            :         struct tcp_md5sig_key *best_match = NULL;
     997                 :            :         bool match;
     998                 :            : 
     999                 :            :         /* caller either holds rcu_read_lock() or socket lock */
    1000                 :            :         md5sig = rcu_dereference_check(tp->md5sig_info,
    1001                 :            :                                        lockdep_sock_is_held(sk));
    1002                 :            :         if (!md5sig)
    1003                 :            :                 return NULL;
    1004                 :            : 
    1005                 :            :         hlist_for_each_entry_rcu(key, &md5sig->head, node) {
    1006                 :            :                 if (key->family != family)
    1007                 :            :                         continue;
    1008                 :            : 
    1009                 :            :                 if (family == AF_INET) {
    1010                 :            :                         mask = inet_make_mask(key->prefixlen);
    1011                 :            :                         match = (key->addr.a4.s_addr & mask) ==
    1012                 :            :                                 (addr->a4.s_addr & mask);
    1013                 :            : #if IS_ENABLED(CONFIG_IPV6)
    1014                 :            :                 } else if (family == AF_INET6) {
    1015                 :            :                         match = ipv6_prefix_equal(&key->addr.a6, &addr->a6,
    1016                 :            :                                                   key->prefixlen);
    1017                 :            : #endif
    1018                 :            :                 } else {
    1019                 :            :                         match = false;
    1020                 :            :                 }
    1021                 :            : 
    1022                 :            :                 if (match && (!best_match ||
    1023                 :            :                               key->prefixlen > best_match->prefixlen))
    1024                 :            :                         best_match = key;
    1025                 :            :         }
    1026                 :            :         return best_match;
    1027                 :            : }
    1028                 :            : EXPORT_SYMBOL(__tcp_md5_do_lookup);
    1029                 :            : 
    1030                 :            : static struct tcp_md5sig_key *tcp_md5_do_lookup_exact(const struct sock *sk,
    1031                 :            :                                                       const union tcp_md5_addr *addr,
    1032                 :            :                                                       int family, u8 prefixlen)
    1033                 :            : {
    1034                 :            :         const struct tcp_sock *tp = tcp_sk(sk);
    1035                 :            :         struct tcp_md5sig_key *key;
    1036                 :            :         unsigned int size = sizeof(struct in_addr);
    1037                 :            :         const struct tcp_md5sig_info *md5sig;
    1038                 :            : 
    1039                 :            :         /* caller either holds rcu_read_lock() or socket lock */
    1040                 :            :         md5sig = rcu_dereference_check(tp->md5sig_info,
    1041                 :            :                                        lockdep_sock_is_held(sk));
    1042                 :            :         if (!md5sig)
    1043                 :            :                 return NULL;
    1044                 :            : #if IS_ENABLED(CONFIG_IPV6)
    1045                 :            :         if (family == AF_INET6)
    1046                 :            :                 size = sizeof(struct in6_addr);
    1047                 :            : #endif
    1048                 :            :         hlist_for_each_entry_rcu(key, &md5sig->head, node) {
    1049                 :            :                 if (key->family != family)
    1050                 :            :                         continue;
    1051                 :            :                 if (!memcmp(&key->addr, addr, size) &&
    1052                 :            :                     key->prefixlen == prefixlen)
    1053                 :            :                         return key;
    1054                 :            :         }
    1055                 :            :         return NULL;
    1056                 :            : }
    1057                 :            : 
    1058                 :            : struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
    1059                 :            :                                          const struct sock *addr_sk)
    1060                 :            : {
    1061                 :            :         const union tcp_md5_addr *addr;
    1062                 :            : 
    1063                 :            :         addr = (const union tcp_md5_addr *)&addr_sk->sk_daddr;
    1064                 :            :         return tcp_md5_do_lookup(sk, addr, AF_INET);
    1065                 :            : }
    1066                 :            : EXPORT_SYMBOL(tcp_v4_md5_lookup);
    1067                 :            : 
    1068                 :            : /* This can be called on a newly created socket, from other files */
    1069                 :            : int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
    1070                 :            :                    int family, u8 prefixlen, const u8 *newkey, u8 newkeylen,
    1071                 :            :                    gfp_t gfp)
    1072                 :            : {
    1073                 :            :         /* Add Key to the list */
    1074                 :            :         struct tcp_md5sig_key *key;
    1075                 :            :         struct tcp_sock *tp = tcp_sk(sk);
    1076                 :            :         struct tcp_md5sig_info *md5sig;
    1077                 :            : 
    1078                 :            :         key = tcp_md5_do_lookup_exact(sk, addr, family, prefixlen);
    1079                 :            :         if (key) {
    1080                 :            :                 /* Pre-existing entry - just update that one.
    1081                 :            :                  * Note that the key might be used concurrently.
    1082                 :            :                  */
    1083                 :            :                 memcpy(key->key, newkey, newkeylen);
    1084                 :            : 
    1085                 :            :                 /* Pairs with READ_ONCE() in tcp_md5_hash_key().
    1086                 :            :                  * Also note that a reader could catch new key->keylen value
    1087                 :            :                  * but old key->key[], this is the reason we use __GFP_ZERO
    1088                 :            :                  * at sock_kmalloc() time below these lines.
    1089                 :            :                  */
    1090                 :            :                 WRITE_ONCE(key->keylen, newkeylen);
    1091                 :            : 
    1092                 :            :                 return 0;
    1093                 :            :         }
    1094                 :            : 
    1095                 :            :         md5sig = rcu_dereference_protected(tp->md5sig_info,
    1096                 :            :                                            lockdep_sock_is_held(sk));
    1097                 :            :         if (!md5sig) {
    1098                 :            :                 md5sig = kmalloc(sizeof(*md5sig), gfp);
    1099                 :            :                 if (!md5sig)
    1100                 :            :                         return -ENOMEM;
    1101                 :            : 
    1102                 :            :                 sk_nocaps_add(sk, NETIF_F_GSO_MASK);
    1103                 :            :                 INIT_HLIST_HEAD(&md5sig->head);
    1104                 :            :                 rcu_assign_pointer(tp->md5sig_info, md5sig);
    1105                 :            :         }
    1106                 :            : 
    1107                 :            :         key = sock_kmalloc(sk, sizeof(*key), gfp | __GFP_ZERO);
    1108                 :            :         if (!key)
    1109                 :            :                 return -ENOMEM;
    1110                 :            :         if (!tcp_alloc_md5sig_pool()) {
    1111                 :            :                 sock_kfree_s(sk, key, sizeof(*key));
    1112                 :            :                 return -ENOMEM;
    1113                 :            :         }
    1114                 :            : 
    1115                 :            :         memcpy(key->key, newkey, newkeylen);
    1116                 :            :         key->keylen = newkeylen;
    1117                 :            :         key->family = family;
    1118                 :            :         key->prefixlen = prefixlen;
    1119                 :            :         memcpy(&key->addr, addr,
    1120                 :            :                (family == AF_INET6) ? sizeof(struct in6_addr) :
    1121                 :            :                                       sizeof(struct in_addr));
    1122                 :            :         hlist_add_head_rcu(&key->node, &md5sig->head);
    1123                 :            :         return 0;
    1124                 :            : }
    1125                 :            : EXPORT_SYMBOL(tcp_md5_do_add);
    1126                 :            : 
    1127                 :            : int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr, int family,
    1128                 :            :                    u8 prefixlen)
    1129                 :            : {
    1130                 :            :         struct tcp_md5sig_key *key;
    1131                 :            : 
    1132                 :            :         key = tcp_md5_do_lookup_exact(sk, addr, family, prefixlen);
    1133                 :            :         if (!key)
    1134                 :            :                 return -ENOENT;
    1135                 :            :         hlist_del_rcu(&key->node);
    1136                 :            :         atomic_sub(sizeof(*key), &sk->sk_omem_alloc);
    1137                 :            :         kfree_rcu(key, rcu);
    1138                 :            :         return 0;
    1139                 :            : }
    1140                 :            : EXPORT_SYMBOL(tcp_md5_do_del);
    1141                 :            : 
    1142                 :            : static void tcp_clear_md5_list(struct sock *sk)
    1143                 :            : {
    1144                 :            :         struct tcp_sock *tp = tcp_sk(sk);
    1145                 :            :         struct tcp_md5sig_key *key;
    1146                 :            :         struct hlist_node *n;
    1147                 :            :         struct tcp_md5sig_info *md5sig;
    1148                 :            : 
    1149                 :            :         md5sig = rcu_dereference_protected(tp->md5sig_info, 1);
    1150                 :            : 
    1151                 :            :         hlist_for_each_entry_safe(key, n, &md5sig->head, node) {
    1152                 :            :                 hlist_del_rcu(&key->node);
    1153                 :            :                 atomic_sub(sizeof(*key), &sk->sk_omem_alloc);
    1154                 :            :                 kfree_rcu(key, rcu);
    1155                 :            :         }
    1156                 :            : }
    1157                 :            : 
    1158                 :            : static int tcp_v4_parse_md5_keys(struct sock *sk, int optname,
    1159                 :            :                                  char __user *optval, int optlen)
    1160                 :            : {
    1161                 :            :         struct tcp_md5sig cmd;
    1162                 :            :         struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr;
    1163                 :            :         u8 prefixlen = 32;
    1164                 :            : 
    1165                 :            :         if (optlen < sizeof(cmd))
    1166                 :            :                 return -EINVAL;
    1167                 :            : 
    1168                 :            :         if (copy_from_user(&cmd, optval, sizeof(cmd)))
    1169                 :            :                 return -EFAULT;
    1170                 :            : 
    1171                 :            :         if (sin->sin_family != AF_INET)
    1172                 :            :                 return -EINVAL;
    1173                 :            : 
    1174                 :            :         if (optname == TCP_MD5SIG_EXT &&
    1175                 :            :             cmd.tcpm_flags & TCP_MD5SIG_FLAG_PREFIX) {
    1176                 :            :                 prefixlen = cmd.tcpm_prefixlen;
    1177                 :            :                 if (prefixlen > 32)
    1178                 :            :                         return -EINVAL;
    1179                 :            :         }
    1180                 :            : 
    1181                 :            :         if (!cmd.tcpm_keylen)
    1182                 :            :                 return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin->sin_addr.s_addr,
    1183                 :            :                                       AF_INET, prefixlen);
    1184                 :            : 
    1185                 :            :         if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
    1186                 :            :                 return -EINVAL;
    1187                 :            : 
    1188                 :            :         return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin->sin_addr.s_addr,
    1189                 :            :                               AF_INET, prefixlen, cmd.tcpm_key, cmd.tcpm_keylen,
    1190                 :            :                               GFP_KERNEL);
    1191                 :            : }
    1192                 :            : 
    1193                 :            : static int tcp_v4_md5_hash_headers(struct tcp_md5sig_pool *hp,
    1194                 :            :                                    __be32 daddr, __be32 saddr,
    1195                 :            :                                    const struct tcphdr *th, int nbytes)
    1196                 :            : {
    1197                 :            :         struct tcp4_pseudohdr *bp;
    1198                 :            :         struct scatterlist sg;
    1199                 :            :         struct tcphdr *_th;
    1200                 :            : 
    1201                 :            :         bp = hp->scratch;
    1202                 :            :         bp->saddr = saddr;
    1203                 :            :         bp->daddr = daddr;
    1204                 :            :         bp->pad = 0;
    1205                 :            :         bp->protocol = IPPROTO_TCP;
    1206                 :            :         bp->len = cpu_to_be16(nbytes);
    1207                 :            : 
    1208                 :            :         _th = (struct tcphdr *)(bp + 1);
    1209                 :            :         memcpy(_th, th, sizeof(*th));
    1210                 :            :         _th->check = 0;
    1211                 :            : 
    1212                 :            :         sg_init_one(&sg, bp, sizeof(*bp) + sizeof(*th));
    1213                 :            :         ahash_request_set_crypt(hp->md5_req, &sg, NULL,
    1214                 :            :                                 sizeof(*bp) + sizeof(*th));
    1215                 :            :         return crypto_ahash_update(hp->md5_req);
    1216                 :            : }
    1217                 :            : 
    1218                 :            : static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
    1219                 :            :                                __be32 daddr, __be32 saddr, const struct tcphdr *th)
    1220                 :            : {
    1221                 :            :         struct tcp_md5sig_pool *hp;
    1222                 :            :         struct ahash_request *req;
    1223                 :            : 
    1224                 :            :         hp = tcp_get_md5sig_pool();
    1225                 :            :         if (!hp)
    1226                 :            :                 goto clear_hash_noput;
    1227                 :            :         req = hp->md5_req;
    1228                 :            : 
    1229                 :            :         if (crypto_ahash_init(req))
    1230                 :            :                 goto clear_hash;
    1231                 :            :         if (tcp_v4_md5_hash_headers(hp, daddr, saddr, th, th->doff << 2))
    1232                 :            :                 goto clear_hash;
    1233                 :            :         if (tcp_md5_hash_key(hp, key))
    1234                 :            :                 goto clear_hash;
    1235                 :            :         ahash_request_set_crypt(req, NULL, md5_hash, 0);
    1236                 :            :         if (crypto_ahash_final(req))
    1237                 :            :                 goto clear_hash;
    1238                 :            : 
    1239                 :            :         tcp_put_md5sig_pool();
    1240                 :            :         return 0;
    1241                 :            : 
    1242                 :            : clear_hash:
    1243                 :            :         tcp_put_md5sig_pool();
    1244                 :            : clear_hash_noput:
    1245                 :            :         memset(md5_hash, 0, 16);
    1246                 :            :         return 1;
    1247                 :            : }
    1248                 :            : 
    1249                 :            : int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
    1250                 :            :                         const struct sock *sk,
    1251                 :            :                         const struct sk_buff *skb)
    1252                 :            : {
    1253                 :            :         struct tcp_md5sig_pool *hp;
    1254                 :            :         struct ahash_request *req;
    1255                 :            :         const struct tcphdr *th = tcp_hdr(skb);
    1256                 :            :         __be32 saddr, daddr;
    1257                 :            : 
    1258                 :            :         if (sk) { /* valid for establish/request sockets */
    1259                 :            :                 saddr = sk->sk_rcv_saddr;
    1260                 :            :                 daddr = sk->sk_daddr;
    1261                 :            :         } else {
    1262                 :            :                 const struct iphdr *iph = ip_hdr(skb);
    1263                 :            :                 saddr = iph->saddr;
    1264                 :            :                 daddr = iph->daddr;
    1265                 :            :         }
    1266                 :            : 
    1267                 :            :         hp = tcp_get_md5sig_pool();
    1268                 :            :         if (!hp)
    1269                 :            :                 goto clear_hash_noput;
    1270                 :            :         req = hp->md5_req;
    1271                 :            : 
    1272                 :            :         if (crypto_ahash_init(req))
    1273                 :            :                 goto clear_hash;
    1274                 :            : 
    1275                 :            :         if (tcp_v4_md5_hash_headers(hp, daddr, saddr, th, skb->len))
    1276                 :            :                 goto clear_hash;
    1277                 :            :         if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
    1278                 :            :                 goto clear_hash;
    1279                 :            :         if (tcp_md5_hash_key(hp, key))
    1280                 :            :                 goto clear_hash;
    1281                 :            :         ahash_request_set_crypt(req, NULL, md5_hash, 0);
    1282                 :            :         if (crypto_ahash_final(req))
    1283                 :            :                 goto clear_hash;
    1284                 :            : 
    1285                 :            :         tcp_put_md5sig_pool();
    1286                 :            :         return 0;
    1287                 :            : 
    1288                 :            : clear_hash:
    1289                 :            :         tcp_put_md5sig_pool();
    1290                 :            : clear_hash_noput:
    1291                 :            :         memset(md5_hash, 0, 16);
    1292                 :            :         return 1;
    1293                 :            : }
    1294                 :            : EXPORT_SYMBOL(tcp_v4_md5_hash_skb);
    1295                 :            : 
    1296                 :            : #endif
    1297                 :            : 
    1298                 :            : /* Called with rcu_read_lock() */
    1299                 :            : static bool tcp_v4_inbound_md5_hash(const struct sock *sk,
    1300                 :            :                                     const struct sk_buff *skb)
    1301                 :            : {
    1302                 :            : #ifdef CONFIG_TCP_MD5SIG
    1303                 :            :         /*
    1304                 :            :          * This gets called for each TCP segment that arrives
    1305                 :            :          * so we want to be efficient.
    1306                 :            :          * We have 3 drop cases:
    1307                 :            :          * o No MD5 hash and one expected.
    1308                 :            :          * o MD5 hash and we're not expecting one.
    1309                 :            :          * o MD5 hash and its wrong.
    1310                 :            :          */
    1311                 :            :         const __u8 *hash_location = NULL;
    1312                 :            :         struct tcp_md5sig_key *hash_expected;
    1313                 :            :         const struct iphdr *iph = ip_hdr(skb);
    1314                 :            :         const struct tcphdr *th = tcp_hdr(skb);
    1315                 :            :         int genhash;
    1316                 :            :         unsigned char newhash[16];
    1317                 :            : 
    1318                 :            :         hash_expected = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&iph->saddr,
    1319                 :            :                                           AF_INET);
    1320                 :            :         hash_location = tcp_parse_md5sig_option(th);
    1321                 :            : 
    1322                 :            :         /* We've parsed the options - do we have a hash? */
    1323                 :            :         if (!hash_expected && !hash_location)
    1324                 :            :                 return false;
    1325                 :            : 
    1326                 :            :         if (hash_expected && !hash_location) {
    1327                 :            :                 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
    1328                 :            :                 return true;
    1329                 :            :         }
    1330                 :            : 
    1331                 :            :         if (!hash_expected && hash_location) {
    1332                 :            :                 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
    1333                 :            :                 return true;
    1334                 :            :         }
    1335                 :            : 
    1336                 :            :         /* Okay, so this is hash_expected and hash_location -
    1337                 :            :          * so we need to calculate the checksum.
    1338                 :            :          */
    1339                 :            :         genhash = tcp_v4_md5_hash_skb(newhash,
    1340                 :            :                                       hash_expected,
    1341                 :            :                                       NULL, skb);
    1342                 :            : 
    1343                 :            :         if (genhash || memcmp(hash_location, newhash, 16) != 0) {
    1344                 :            :                 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5FAILURE);
    1345                 :            :                 net_info_ratelimited("MD5 Hash failed for (%pI4, %d)->(%pI4, %d)%s\n",
    1346                 :            :                                      &iph->saddr, ntohs(th->source),
    1347                 :            :                                      &iph->daddr, ntohs(th->dest),
    1348                 :            :                                      genhash ? " tcp_v4_calc_md5_hash failed"
    1349                 :            :                                      : "");
    1350                 :            :                 return true;
    1351                 :            :         }
    1352                 :            :         return false;
    1353                 :            : #endif
    1354                 :            :         return false;
    1355                 :            : }
    1356                 :            : 
    1357                 :          0 : static void tcp_v4_init_req(struct request_sock *req,
    1358                 :            :                             const struct sock *sk_listener,
    1359                 :            :                             struct sk_buff *skb)
    1360                 :            : {
    1361                 :            :         struct inet_request_sock *ireq = inet_rsk(req);
    1362                 :            :         struct net *net = sock_net(sk_listener);
    1363                 :            : 
    1364                 :          0 :         sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
    1365                 :          0 :         sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
    1366                 :          0 :         RCU_INIT_POINTER(ireq->ireq_opt, tcp_v4_save_options(net, skb));
    1367                 :          0 : }
    1368                 :            : 
    1369                 :          0 : static struct dst_entry *tcp_v4_route_req(const struct sock *sk,
    1370                 :            :                                           struct flowi *fl,
    1371                 :            :                                           const struct request_sock *req)
    1372                 :            : {
    1373                 :          0 :         return inet_csk_route_req(sk, &fl->u.ip4, req);
    1374                 :            : }
    1375                 :            : 
    1376                 :            : struct request_sock_ops tcp_request_sock_ops __read_mostly = {
    1377                 :            :         .family         =       PF_INET,
    1378                 :            :         .obj_size       =       sizeof(struct tcp_request_sock),
    1379                 :            :         .rtx_syn_ack    =       tcp_rtx_synack,
    1380                 :            :         .send_ack       =       tcp_v4_reqsk_send_ack,
    1381                 :            :         .destructor     =       tcp_v4_reqsk_destructor,
    1382                 :            :         .send_reset     =       tcp_v4_send_reset,
    1383                 :            :         .syn_ack_timeout =      tcp_syn_ack_timeout,
    1384                 :            : };
    1385                 :            : 
    1386                 :            : static const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
    1387                 :            :         .mss_clamp      =       TCP_MSS_DEFAULT,
    1388                 :            : #ifdef CONFIG_TCP_MD5SIG
    1389                 :            :         .req_md5_lookup =       tcp_v4_md5_lookup,
    1390                 :            :         .calc_md5_hash  =       tcp_v4_md5_hash_skb,
    1391                 :            : #endif
    1392                 :            :         .init_req       =       tcp_v4_init_req,
    1393                 :            : #ifdef CONFIG_SYN_COOKIES
    1394                 :            :         .cookie_init_seq =      cookie_v4_init_sequence,
    1395                 :            : #endif
    1396                 :            :         .route_req      =       tcp_v4_route_req,
    1397                 :            :         .init_seq       =       tcp_v4_init_seq,
    1398                 :            :         .init_ts_off    =       tcp_v4_init_ts_off,
    1399                 :            :         .send_synack    =       tcp_v4_send_synack,
    1400                 :            : };
    1401                 :            : 
    1402                 :          0 : int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
    1403                 :            : {
    1404                 :            :         /* Never answer to SYNs send to broadcast or multicast */
    1405         [ #  # ]:          0 :         if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
    1406                 :            :                 goto drop;
    1407                 :            : 
    1408                 :          0 :         return tcp_conn_request(&tcp_request_sock_ops,
    1409                 :            :                                 &tcp_request_sock_ipv4_ops, sk, skb);
    1410                 :            : 
    1411                 :            : drop:
    1412                 :          0 :         tcp_listendrop(sk);
    1413                 :          0 :         return 0;
    1414                 :            : }
    1415                 :            : EXPORT_SYMBOL(tcp_v4_conn_request);
    1416                 :            : 
    1417                 :            : 
    1418                 :            : /*
    1419                 :            :  * The three way handshake has completed - we got a valid synack -
    1420                 :            :  * now create the new socket.
    1421                 :            :  */
    1422                 :          0 : struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
    1423                 :            :                                   struct request_sock *req,
    1424                 :            :                                   struct dst_entry *dst,
    1425                 :            :                                   struct request_sock *req_unhash,
    1426                 :            :                                   bool *own_req)
    1427                 :            : {
    1428                 :            :         struct inet_request_sock *ireq;
    1429                 :            :         struct inet_sock *newinet;
    1430                 :            :         struct tcp_sock *newtp;
    1431                 :            :         struct sock *newsk;
    1432                 :            : #ifdef CONFIG_TCP_MD5SIG
    1433                 :            :         struct tcp_md5sig_key *key;
    1434                 :            : #endif
    1435                 :            :         struct ip_options_rcu *inet_opt;
    1436                 :            : 
    1437         [ #  # ]:          0 :         if (sk_acceptq_is_full(sk))
    1438                 :            :                 goto exit_overflow;
    1439                 :            : 
    1440                 :          0 :         newsk = tcp_create_openreq_child(sk, req, skb);
    1441         [ #  # ]:          0 :         if (!newsk)
    1442                 :            :                 goto exit_nonewsk;
    1443                 :            : 
    1444                 :          0 :         newsk->sk_gso_type = SKB_GSO_TCPV4;
    1445                 :          0 :         inet_sk_rx_dst_set(newsk, skb);
    1446                 :            : 
    1447                 :            :         newtp                 = tcp_sk(newsk);
    1448                 :            :         newinet               = inet_sk(newsk);
    1449                 :            :         ireq                  = inet_rsk(req);
    1450                 :          0 :         sk_daddr_set(newsk, ireq->ir_rmt_addr);
    1451                 :          0 :         sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
    1452                 :          0 :         newsk->sk_bound_dev_if = ireq->ir_iif;
    1453                 :          0 :         newinet->inet_saddr   = ireq->ir_loc_addr;
    1454                 :          0 :         inet_opt              = rcu_dereference(ireq->ireq_opt);
    1455                 :            :         RCU_INIT_POINTER(newinet->inet_opt, inet_opt);
    1456                 :          0 :         newinet->mc_index     = inet_iif(skb);
    1457                 :          0 :         newinet->mc_ttl            = ip_hdr(skb)->ttl;
    1458                 :          0 :         newinet->rcv_tos      = ip_hdr(skb)->tos;
    1459                 :          0 :         inet_csk(newsk)->icsk_ext_hdr_len = 0;
    1460         [ #  # ]:          0 :         if (inet_opt)
    1461                 :          0 :                 inet_csk(newsk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
    1462                 :          0 :         newinet->inet_id = prandom_u32();
    1463                 :            : 
    1464         [ #  # ]:          0 :         if (!dst) {
    1465                 :          0 :                 dst = inet_csk_route_child_sock(sk, newsk, req);
    1466         [ #  # ]:          0 :                 if (!dst)
    1467                 :            :                         goto put_and_exit;
    1468                 :            :         } else {
    1469                 :            :                 /* syncookie case : see end of cookie_v4_check() */
    1470                 :            :         }
    1471                 :          0 :         sk_setup_caps(newsk, dst);
    1472                 :            : 
    1473                 :          0 :         tcp_ca_openreq_child(newsk, dst);
    1474                 :            : 
    1475                 :          0 :         tcp_sync_mss(newsk, dst_mtu(dst));
    1476                 :          0 :         newtp->advmss = tcp_mss_clamp(tcp_sk(sk), dst_metric_advmss(dst));
    1477                 :            : 
    1478                 :          0 :         tcp_initialize_rcv_mss(newsk);
    1479                 :            : 
    1480                 :            : #ifdef CONFIG_TCP_MD5SIG
    1481                 :            :         /* Copy over the MD5 key from the original socket */
    1482                 :            :         key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&newinet->inet_daddr,
    1483                 :            :                                 AF_INET);
    1484                 :            :         if (key) {
    1485                 :            :                 /*
    1486                 :            :                  * We're using one, so create a matching key
    1487                 :            :                  * on the newsk structure. If we fail to get
    1488                 :            :                  * memory, then we end up not copying the key
    1489                 :            :                  * across. Shucks.
    1490                 :            :                  */
    1491                 :            :                 tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newinet->inet_daddr,
    1492                 :            :                                AF_INET, 32, key->key, key->keylen, GFP_ATOMIC);
    1493                 :            :                 sk_nocaps_add(newsk, NETIF_F_GSO_MASK);
    1494                 :            :         }
    1495                 :            : #endif
    1496                 :            : 
    1497         [ #  # ]:          0 :         if (__inet_inherit_port(sk, newsk) < 0)
    1498                 :            :                 goto put_and_exit;
    1499                 :          0 :         *own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash));
    1500         [ #  # ]:          0 :         if (likely(*own_req)) {
    1501                 :            :                 tcp_move_syn(newtp, req);
    1502                 :          0 :                 ireq->ireq_opt = NULL;
    1503                 :            :         } else {
    1504                 :          0 :                 newinet->inet_opt = NULL;
    1505                 :            :         }
    1506                 :          0 :         return newsk;
    1507                 :            : 
    1508                 :            : exit_overflow:
    1509                 :          0 :         NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
    1510                 :            : exit_nonewsk:
    1511                 :          0 :         dst_release(dst);
    1512                 :            : exit:
    1513                 :          0 :         tcp_listendrop(sk);
    1514                 :          0 :         return NULL;
    1515                 :            : put_and_exit:
    1516                 :          0 :         newinet->inet_opt = NULL;
    1517                 :          0 :         inet_csk_prepare_forced_close(newsk);
    1518                 :          0 :         tcp_done(newsk);
    1519                 :          0 :         goto exit;
    1520                 :            : }
    1521                 :            : EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
    1522                 :            : 
    1523                 :          0 : static struct sock *tcp_v4_cookie_check(struct sock *sk, struct sk_buff *skb)
    1524                 :            : {
    1525                 :            : #ifdef CONFIG_SYN_COOKIES
    1526                 :            :         const struct tcphdr *th = tcp_hdr(skb);
    1527                 :            : 
    1528         [ #  # ]:          0 :         if (!th->syn)
    1529                 :          0 :                 sk = cookie_v4_check(sk, skb);
    1530                 :            : #endif
    1531                 :          0 :         return sk;
    1532                 :            : }
    1533                 :            : 
    1534                 :          0 : u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph,
    1535                 :            :                          struct tcphdr *th, u32 *cookie)
    1536                 :            : {
    1537                 :          0 :         u16 mss = 0;
    1538                 :            : #ifdef CONFIG_SYN_COOKIES
    1539                 :          0 :         mss = tcp_get_syncookie_mss(&tcp_request_sock_ops,
    1540                 :            :                                     &tcp_request_sock_ipv4_ops, sk, th);
    1541         [ #  # ]:          0 :         if (mss) {
    1542                 :          0 :                 *cookie = __cookie_v4_init_sequence(iph, th, &mss);
    1543                 :          0 :                 tcp_synq_overflow(sk);
    1544                 :            :         }
    1545                 :            : #endif
    1546                 :          0 :         return mss;
    1547                 :            : }
    1548                 :            : 
    1549                 :            : /* The socket must have it's spinlock held when we get
    1550                 :            :  * here, unless it is a TCP_LISTEN socket.
    1551                 :            :  *
    1552                 :            :  * We have a potential double-lock case here, so even when
    1553                 :            :  * doing backlog processing we use the BH locking scheme.
    1554                 :            :  * This is because we cannot sleep with the original spinlock
    1555                 :            :  * held.
    1556                 :            :  */
    1557                 :          0 : int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
    1558                 :            : {
    1559                 :            :         struct sock *rsk;
    1560                 :            : 
    1561         [ #  # ]:          0 :         if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
    1562                 :          0 :                 struct dst_entry *dst = sk->sk_rx_dst;
    1563                 :            : 
    1564                 :            :                 sock_rps_save_rxhash(sk, skb);
    1565                 :            :                 sk_mark_napi_id(sk, skb);
    1566         [ #  # ]:          0 :                 if (dst) {
    1567   [ #  #  #  # ]:          0 :                         if (inet_sk(sk)->rx_dst_ifindex != skb->skb_iif ||
    1568                 :          0 :                             !dst->ops->check(dst, 0)) {
    1569                 :          0 :                                 dst_release(dst);
    1570                 :          0 :                                 sk->sk_rx_dst = NULL;
    1571                 :            :                         }
    1572                 :            :                 }
    1573                 :          0 :                 tcp_rcv_established(sk, skb);
    1574                 :          0 :                 return 0;
    1575                 :            :         }
    1576                 :            : 
    1577         [ #  # ]:          0 :         if (tcp_checksum_complete(skb))
    1578                 :            :                 goto csum_err;
    1579                 :            : 
    1580         [ #  # ]:          0 :         if (sk->sk_state == TCP_LISTEN) {
    1581                 :          0 :                 struct sock *nsk = tcp_v4_cookie_check(sk, skb);
    1582                 :            : 
    1583         [ #  # ]:          0 :                 if (!nsk)
    1584                 :            :                         goto discard;
    1585         [ #  # ]:          0 :                 if (nsk != sk) {
    1586         [ #  # ]:          0 :                         if (tcp_child_process(sk, nsk, skb)) {
    1587                 :            :                                 rsk = nsk;
    1588                 :            :                                 goto reset;
    1589                 :            :                         }
    1590                 :            :                         return 0;
    1591                 :            :                 }
    1592                 :            :         } else
    1593                 :            :                 sock_rps_save_rxhash(sk, skb);
    1594                 :            : 
    1595         [ #  # ]:          0 :         if (tcp_rcv_state_process(sk, skb)) {
    1596                 :            :                 rsk = sk;
    1597                 :            :                 goto reset;
    1598                 :            :         }
    1599                 :            :         return 0;
    1600                 :            : 
    1601                 :            : reset:
    1602                 :          0 :         tcp_v4_send_reset(rsk, skb);
    1603                 :            : discard:
    1604                 :          0 :         kfree_skb(skb);
    1605                 :            :         /* Be careful here. If this function gets more complicated and
    1606                 :            :          * gcc suffers from register pressure on the x86, sk (in %ebx)
    1607                 :            :          * might be destroyed here. This current version compiles correctly,
    1608                 :            :          * but you have been warned.
    1609                 :            :          */
    1610                 :          0 :         return 0;
    1611                 :            : 
    1612                 :            : csum_err:
    1613                 :          0 :         TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
    1614                 :          0 :         TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
    1615                 :            :         goto discard;
    1616                 :            : }
    1617                 :            : EXPORT_SYMBOL(tcp_v4_do_rcv);
    1618                 :            : 
    1619                 :          2 : int tcp_v4_early_demux(struct sk_buff *skb)
    1620                 :            : {
    1621                 :            :         const struct iphdr *iph;
    1622                 :            :         const struct tcphdr *th;
    1623                 :            :         struct sock *sk;
    1624                 :            : 
    1625         [ +  - ]:          2 :         if (skb->pkt_type != PACKET_HOST)
    1626                 :            :                 return 0;
    1627                 :            : 
    1628         [ +  - ]:          2 :         if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct tcphdr)))
    1629                 :            :                 return 0;
    1630                 :            : 
    1631                 :            :         iph = ip_hdr(skb);
    1632                 :            :         th = tcp_hdr(skb);
    1633                 :            : 
    1634         [ +  - ]:          2 :         if (th->doff < sizeof(struct tcphdr) / 4)
    1635                 :            :                 return 0;
    1636                 :            : 
    1637                 :          6 :         sk = __inet_lookup_established(dev_net(skb->dev), &tcp_hashinfo,
    1638                 :            :                                        iph->saddr, th->source,
    1639                 :            :                                        iph->daddr, ntohs(th->dest),
    1640                 :            :                                        skb->skb_iif, inet_sdif(skb));
    1641         [ -  + ]:          2 :         if (sk) {
    1642                 :          0 :                 skb->sk = sk;
    1643                 :          0 :                 skb->destructor = sock_edemux;
    1644         [ #  # ]:          0 :                 if (sk_fullsock(sk)) {
    1645                 :          0 :                         struct dst_entry *dst = READ_ONCE(sk->sk_rx_dst);
    1646                 :            : 
    1647         [ #  # ]:          0 :                         if (dst)
    1648                 :            :                                 dst = dst_check(dst, 0);
    1649   [ #  #  #  # ]:          0 :                         if (dst &&
    1650                 :          0 :                             inet_sk(sk)->rx_dst_ifindex == skb->skb_iif)
    1651                 :            :                                 skb_dst_set_noref(skb, dst);
    1652                 :            :                 }
    1653                 :            :         }
    1654                 :            :         return 0;
    1655                 :            : }
    1656                 :            : 
    1657                 :          0 : bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb)
    1658                 :            : {
    1659                 :          0 :         u32 limit = READ_ONCE(sk->sk_rcvbuf) + READ_ONCE(sk->sk_sndbuf);
    1660                 :            :         struct skb_shared_info *shinfo;
    1661                 :            :         const struct tcphdr *th;
    1662                 :            :         struct tcphdr *thtail;
    1663                 :            :         struct sk_buff *tail;
    1664                 :            :         unsigned int hdrlen;
    1665                 :            :         bool fragstolen;
    1666                 :            :         u32 gso_segs;
    1667                 :            :         int delta;
    1668                 :            : 
    1669                 :            :         /* In case all data was pulled from skb frags (in __pskb_pull_tail()),
    1670                 :            :          * we can fix skb->truesize to its real value to avoid future drops.
    1671                 :            :          * This is valid because skb is not yet charged to the socket.
    1672                 :            :          * It has been noticed pure SACK packets were sometimes dropped
    1673                 :            :          * (if cooked by drivers without copybreak feature).
    1674                 :            :          */
    1675                 :          0 :         skb_condense(skb);
    1676                 :            : 
    1677                 :          0 :         skb_dst_drop(skb);
    1678                 :            : 
    1679         [ #  # ]:          0 :         if (unlikely(tcp_checksum_complete(skb))) {
    1680                 :            :                 bh_unlock_sock(sk);
    1681                 :          0 :                 __TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
    1682                 :          0 :                 __TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
    1683                 :          0 :                 return true;
    1684                 :            :         }
    1685                 :            : 
    1686                 :            :         /* Attempt coalescing to last skb in backlog, even if we are
    1687                 :            :          * above the limits.
    1688                 :            :          * This is okay because skb capacity is limited to MAX_SKB_FRAGS.
    1689                 :            :          */
    1690                 :          0 :         th = (const struct tcphdr *)skb->data;
    1691                 :          0 :         hdrlen = th->doff * 4;
    1692                 :            :         shinfo = skb_shinfo(skb);
    1693                 :            : 
    1694         [ #  # ]:          0 :         if (!shinfo->gso_size)
    1695                 :          0 :                 shinfo->gso_size = skb->len - hdrlen;
    1696                 :            : 
    1697         [ #  # ]:          0 :         if (!shinfo->gso_segs)
    1698                 :          0 :                 shinfo->gso_segs = 1;
    1699                 :            : 
    1700                 :          0 :         tail = sk->sk_backlog.tail;
    1701         [ #  # ]:          0 :         if (!tail)
    1702                 :            :                 goto no_coalesce;
    1703                 :          0 :         thtail = (struct tcphdr *)tail->data;
    1704                 :            : 
    1705   [ #  #  #  # ]:          0 :         if (TCP_SKB_CB(tail)->end_seq != TCP_SKB_CB(skb)->seq ||
    1706         [ #  # ]:          0 :             TCP_SKB_CB(tail)->ip_dsfield != TCP_SKB_CB(skb)->ip_dsfield ||
    1707                 :          0 :             ((TCP_SKB_CB(tail)->tcp_flags |
    1708         [ #  # ]:          0 :               TCP_SKB_CB(skb)->tcp_flags) & (TCPHDR_SYN | TCPHDR_RST | TCPHDR_URG)) ||
    1709                 :            :             !((TCP_SKB_CB(tail)->tcp_flags &
    1710         [ #  # ]:          0 :               TCP_SKB_CB(skb)->tcp_flags) & TCPHDR_ACK) ||
    1711                 :            :             ((TCP_SKB_CB(tail)->tcp_flags ^
    1712         [ #  # ]:          0 :               TCP_SKB_CB(skb)->tcp_flags) & (TCPHDR_ECE | TCPHDR_CWR)) ||
    1713                 :            : #ifdef CONFIG_TLS_DEVICE
    1714                 :            :             tail->decrypted != skb->decrypted ||
    1715                 :            : #endif
    1716         [ #  # ]:          0 :             thtail->doff != th->doff ||
    1717                 :          0 :             memcmp(thtail + 1, th + 1, hdrlen - sizeof(*th)))
    1718                 :            :                 goto no_coalesce;
    1719                 :            : 
    1720                 :          0 :         __skb_pull(skb, hdrlen);
    1721         [ #  # ]:          0 :         if (skb_try_coalesce(tail, skb, &fragstolen, &delta)) {
    1722                 :          0 :                 thtail->window = th->window;
    1723                 :            : 
    1724                 :          0 :                 TCP_SKB_CB(tail)->end_seq = TCP_SKB_CB(skb)->end_seq;
    1725                 :            : 
    1726         [ #  # ]:          0 :                 if (after(TCP_SKB_CB(skb)->ack_seq, TCP_SKB_CB(tail)->ack_seq))
    1727                 :          0 :                         TCP_SKB_CB(tail)->ack_seq = TCP_SKB_CB(skb)->ack_seq;
    1728                 :            : 
    1729                 :            :                 /* We have to update both TCP_SKB_CB(tail)->tcp_flags and
    1730                 :            :                  * thtail->fin, so that the fast path in tcp_rcv_established()
    1731                 :            :                  * is not entered if we append a packet with a FIN.
    1732                 :            :                  * SYN, RST, URG are not present.
    1733                 :            :                  * ACK is set on both packets.
    1734                 :            :                  * PSH : we do not really care in TCP stack,
    1735                 :            :                  *       at least for 'GRO' packets.
    1736                 :            :                  */
    1737                 :          0 :                 thtail->fin |= th->fin;
    1738                 :          0 :                 TCP_SKB_CB(tail)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags;
    1739                 :            : 
    1740         [ #  # ]:          0 :                 if (TCP_SKB_CB(skb)->has_rxtstamp) {
    1741                 :          0 :                         TCP_SKB_CB(tail)->has_rxtstamp = true;
    1742                 :          0 :                         tail->tstamp = skb->tstamp;
    1743                 :          0 :                         skb_hwtstamps(tail)->hwtstamp = skb_hwtstamps(skb)->hwtstamp;
    1744                 :            :                 }
    1745                 :            : 
    1746                 :            :                 /* Not as strict as GRO. We only need to carry mss max value */
    1747         [ #  # ]:          0 :                 skb_shinfo(tail)->gso_size = max(shinfo->gso_size,
    1748                 :            :                                                  skb_shinfo(tail)->gso_size);
    1749                 :            : 
    1750                 :          0 :                 gso_segs = skb_shinfo(tail)->gso_segs + shinfo->gso_segs;
    1751                 :          0 :                 skb_shinfo(tail)->gso_segs = min_t(u32, gso_segs, 0xFFFF);
    1752                 :            : 
    1753                 :          0 :                 sk->sk_backlog.len += delta;
    1754                 :          0 :                 __NET_INC_STATS(sock_net(sk),
    1755                 :            :                                 LINUX_MIB_TCPBACKLOGCOALESCE);
    1756                 :          0 :                 kfree_skb_partial(skb, fragstolen);
    1757                 :          0 :                 return false;
    1758                 :            :         }
    1759                 :            :         __skb_push(skb, hdrlen);
    1760                 :            : 
    1761                 :            : no_coalesce:
    1762                 :            :         /* Only socket owner can try to collapse/prune rx queues
    1763                 :            :          * to reduce memory overhead, so add a little headroom here.
    1764                 :            :          * Few sockets backlog are possibly concurrently non empty.
    1765                 :            :          */
    1766                 :          0 :         limit += 64*1024;
    1767                 :            : 
    1768         [ #  # ]:          0 :         if (unlikely(sk_add_backlog(sk, skb, limit))) {
    1769                 :            :                 bh_unlock_sock(sk);
    1770                 :          0 :                 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPBACKLOGDROP);
    1771                 :          0 :                 return true;
    1772                 :            :         }
    1773                 :            :         return false;
    1774                 :            : }
    1775                 :            : EXPORT_SYMBOL(tcp_add_backlog);
    1776                 :            : 
    1777                 :          0 : int tcp_filter(struct sock *sk, struct sk_buff *skb)
    1778                 :            : {
    1779                 :          0 :         struct tcphdr *th = (struct tcphdr *)skb->data;
    1780                 :            : 
    1781                 :          0 :         return sk_filter_trim_cap(sk, skb, th->doff * 4);
    1782                 :            : }
    1783                 :            : EXPORT_SYMBOL(tcp_filter);
    1784                 :            : 
    1785                 :            : static void tcp_v4_restore_cb(struct sk_buff *skb)
    1786                 :            : {
    1787                 :          0 :         memmove(IPCB(skb), &TCP_SKB_CB(skb)->header.h4,
    1788                 :            :                 sizeof(struct inet_skb_parm));
    1789                 :            : }
    1790                 :            : 
    1791                 :          2 : static void tcp_v4_fill_cb(struct sk_buff *skb, const struct iphdr *iph,
    1792                 :            :                            const struct tcphdr *th)
    1793                 :            : {
    1794                 :            :         /* This is tricky : We move IPCB at its correct location into TCP_SKB_CB()
    1795                 :            :          * barrier() makes sure compiler wont play fool^Waliasing games.
    1796                 :            :          */
    1797                 :          2 :         memmove(&TCP_SKB_CB(skb)->header.h4, IPCB(skb),
    1798                 :            :                 sizeof(struct inet_skb_parm));
    1799                 :          2 :         barrier();
    1800                 :            : 
    1801                 :          2 :         TCP_SKB_CB(skb)->seq = ntohl(th->seq);
    1802                 :          6 :         TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
    1803                 :          4 :                                     skb->len - th->doff * 4);
    1804                 :          2 :         TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
    1805                 :          2 :         TCP_SKB_CB(skb)->tcp_flags = tcp_flag_byte(th);
    1806                 :          2 :         TCP_SKB_CB(skb)->tcp_tw_isn = 0;
    1807                 :          2 :         TCP_SKB_CB(skb)->ip_dsfield = ipv4_get_dsfield(iph);
    1808                 :          2 :         TCP_SKB_CB(skb)->sacked       = 0;
    1809                 :          2 :         TCP_SKB_CB(skb)->has_rxtstamp =
    1810   [ +  -  +  - ]:          4 :                         skb->tstamp || skb_hwtstamps(skb)->hwtstamp;
    1811                 :          2 : }
    1812                 :            : 
    1813                 :            : /*
    1814                 :            :  *      From tcp_input.c
    1815                 :            :  */
    1816                 :            : 
    1817                 :          2 : int tcp_v4_rcv(struct sk_buff *skb)
    1818                 :            : {
    1819                 :          2 :         struct net *net = dev_net(skb->dev);
    1820                 :            :         struct sk_buff *skb_to_free;
    1821                 :            :         int sdif = inet_sdif(skb);
    1822                 :            :         const struct iphdr *iph;
    1823                 :            :         const struct tcphdr *th;
    1824                 :            :         bool refcounted;
    1825                 :            :         struct sock *sk;
    1826                 :            :         int ret;
    1827                 :            : 
    1828         [ +  - ]:          2 :         if (skb->pkt_type != PACKET_HOST)
    1829                 :            :                 goto discard_it;
    1830                 :            : 
    1831                 :            :         /* Count it even if it's bad */
    1832                 :          4 :         __TCP_INC_STATS(net, TCP_MIB_INSEGS);
    1833                 :            : 
    1834         [ +  - ]:          2 :         if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
    1835                 :            :                 goto discard_it;
    1836                 :            : 
    1837                 :          2 :         th = (const struct tcphdr *)skb->data;
    1838                 :            : 
    1839         [ +  - ]:          2 :         if (unlikely(th->doff < sizeof(struct tcphdr) / 4))
    1840                 :            :                 goto bad_packet;
    1841         [ +  - ]:          2 :         if (!pskb_may_pull(skb, th->doff * 4))
    1842                 :            :                 goto discard_it;
    1843                 :            : 
    1844                 :            :         /* An explanation is required here, I think.
    1845                 :            :          * Packet length and doff are validated by header prediction,
    1846                 :            :          * provided case of th->doff==0 is eliminated.
    1847                 :            :          * So, we defer the checks. */
    1848                 :            : 
    1849   [ +  -  +  - ]:          4 :         if (skb_checksum_init(skb, IPPROTO_TCP, inet_compute_pseudo))
    1850                 :            :                 goto csum_error;
    1851                 :            : 
    1852                 :          2 :         th = (const struct tcphdr *)skb->data;
    1853                 :            :         iph = ip_hdr(skb);
    1854                 :            : lookup:
    1855                 :          2 :         sk = __inet_lookup_skb(&tcp_hashinfo, skb, __tcp_hdrlen(th), th->source,
    1856                 :            :                                th->dest, sdif, &refcounted);
    1857         [ -  + ]:          2 :         if (!sk)
    1858                 :            :                 goto no_tcp_socket;
    1859                 :            : 
    1860                 :            : process:
    1861         [ #  # ]:          0 :         if (sk->sk_state == TCP_TIME_WAIT)
    1862                 :            :                 goto do_time_wait;
    1863                 :            : 
    1864         [ #  # ]:          0 :         if (sk->sk_state == TCP_NEW_SYN_RECV) {
    1865                 :            :                 struct request_sock *req = inet_reqsk(sk);
    1866                 :          0 :                 bool req_stolen = false;
    1867                 :            :                 struct sock *nsk;
    1868                 :            : 
    1869                 :          0 :                 sk = req->rsk_listener;
    1870                 :            :                 if (unlikely(tcp_v4_inbound_md5_hash(sk, skb))) {
    1871                 :            :                         sk_drops_add(sk, skb);
    1872                 :            :                         reqsk_put(req);
    1873                 :            :                         goto discard_it;
    1874                 :            :                 }
    1875         [ #  # ]:          0 :                 if (tcp_checksum_complete(skb)) {
    1876                 :          0 :                         reqsk_put(req);
    1877                 :          0 :                         goto csum_error;
    1878                 :            :                 }
    1879         [ #  # ]:          0 :                 if (unlikely(sk->sk_state != TCP_LISTEN)) {
    1880                 :          0 :                         inet_csk_reqsk_queue_drop_and_put(sk, req);
    1881                 :          0 :                         goto lookup;
    1882                 :            :                 }
    1883                 :            :                 /* We own a reference on the listener, increase it again
    1884                 :            :                  * as we might lose it too soon.
    1885                 :            :                  */
    1886                 :            :                 sock_hold(sk);
    1887                 :          0 :                 refcounted = true;
    1888                 :            :                 nsk = NULL;
    1889         [ #  # ]:          0 :                 if (!tcp_filter(sk, skb)) {
    1890                 :          0 :                         th = (const struct tcphdr *)skb->data;
    1891                 :            :                         iph = ip_hdr(skb);
    1892                 :          0 :                         tcp_v4_fill_cb(skb, iph, th);
    1893                 :          0 :                         nsk = tcp_check_req(sk, skb, req, false, &req_stolen);
    1894                 :            :                 }
    1895         [ #  # ]:          0 :                 if (!nsk) {
    1896                 :          0 :                         reqsk_put(req);
    1897         [ #  # ]:          0 :                         if (req_stolen) {
    1898                 :            :                                 /* Another cpu got exclusive access to req
    1899                 :            :                                  * and created a full blown socket.
    1900                 :            :                                  * Try to feed this packet to this socket
    1901                 :            :                                  * instead of discarding it.
    1902                 :            :                                  */
    1903                 :            :                                 tcp_v4_restore_cb(skb);
    1904                 :          0 :                                 sock_put(sk);
    1905                 :          0 :                                 goto lookup;
    1906                 :            :                         }
    1907                 :          0 :                         goto discard_and_relse;
    1908                 :            :                 }
    1909         [ #  # ]:          0 :                 if (nsk == sk) {
    1910                 :          0 :                         reqsk_put(req);
    1911                 :            :                         tcp_v4_restore_cb(skb);
    1912         [ #  # ]:          0 :                 } else if (tcp_child_process(sk, nsk, skb)) {
    1913                 :          0 :                         tcp_v4_send_reset(nsk, skb);
    1914                 :          0 :                         goto discard_and_relse;
    1915                 :            :                 } else {
    1916                 :          0 :                         sock_put(sk);
    1917                 :          0 :                         return 0;
    1918                 :            :                 }
    1919                 :            :         }
    1920         [ #  # ]:          0 :         if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) {
    1921                 :          0 :                 __NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
    1922                 :            :                 goto discard_and_relse;
    1923                 :            :         }
    1924                 :            : 
    1925         [ #  # ]:          0 :         if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
    1926                 :            :                 goto discard_and_relse;
    1927                 :            : 
    1928                 :            :         if (tcp_v4_inbound_md5_hash(sk, skb))
    1929                 :            :                 goto discard_and_relse;
    1930                 :            : 
    1931                 :            :         nf_reset_ct(skb);
    1932                 :            : 
    1933         [ #  # ]:          0 :         if (tcp_filter(sk, skb))
    1934                 :            :                 goto discard_and_relse;
    1935                 :          0 :         th = (const struct tcphdr *)skb->data;
    1936                 :            :         iph = ip_hdr(skb);
    1937                 :          0 :         tcp_v4_fill_cb(skb, iph, th);
    1938                 :            : 
    1939                 :          0 :         skb->dev = NULL;
    1940                 :            : 
    1941         [ #  # ]:          0 :         if (sk->sk_state == TCP_LISTEN) {
    1942                 :          0 :                 ret = tcp_v4_do_rcv(sk, skb);
    1943                 :          0 :                 goto put_and_return;
    1944                 :            :         }
    1945                 :            : 
    1946                 :            :         sk_incoming_cpu_update(sk);
    1947                 :            : 
    1948                 :          0 :         bh_lock_sock_nested(sk);
    1949                 :          0 :         tcp_segs_in(tcp_sk(sk), skb);
    1950                 :            :         ret = 0;
    1951         [ #  # ]:          0 :         if (!sock_owned_by_user(sk)) {
    1952                 :          0 :                 skb_to_free = sk->sk_rx_skb_cache;
    1953                 :          0 :                 sk->sk_rx_skb_cache = NULL;
    1954                 :          0 :                 ret = tcp_v4_do_rcv(sk, skb);
    1955                 :            :         } else {
    1956         [ #  # ]:          0 :                 if (tcp_add_backlog(sk, skb))
    1957                 :            :                         goto discard_and_relse;
    1958                 :            :                 skb_to_free = NULL;
    1959                 :            :         }
    1960                 :            :         bh_unlock_sock(sk);
    1961         [ #  # ]:          0 :         if (skb_to_free)
    1962                 :          0 :                 __kfree_skb(skb_to_free);
    1963                 :            : 
    1964                 :            : put_and_return:
    1965         [ #  # ]:          0 :         if (refcounted)
    1966                 :          0 :                 sock_put(sk);
    1967                 :            : 
    1968                 :          0 :         return ret;
    1969                 :            : 
    1970                 :            : no_tcp_socket:
    1971         [ +  - ]:          2 :         if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
    1972                 :            :                 goto discard_it;
    1973                 :            : 
    1974                 :          2 :         tcp_v4_fill_cb(skb, iph, th);
    1975                 :            : 
    1976         [ -  + ]:          2 :         if (tcp_checksum_complete(skb)) {
    1977                 :            : csum_error:
    1978                 :          0 :                 __TCP_INC_STATS(net, TCP_MIB_CSUMERRORS);
    1979                 :            : bad_packet:
    1980                 :          0 :                 __TCP_INC_STATS(net, TCP_MIB_INERRS);
    1981                 :            :         } else {
    1982                 :          2 :                 tcp_v4_send_reset(NULL, skb);
    1983                 :            :         }
    1984                 :            : 
    1985                 :            : discard_it:
    1986                 :            :         /* Discard frame. */
    1987                 :          2 :         kfree_skb(skb);
    1988                 :          2 :         return 0;
    1989                 :            : 
    1990                 :            : discard_and_relse:
    1991                 :          0 :         sk_drops_add(sk, skb);
    1992         [ #  # ]:          0 :         if (refcounted)
    1993                 :          0 :                 sock_put(sk);
    1994                 :            :         goto discard_it;
    1995                 :            : 
    1996                 :            : do_time_wait:
    1997         [ #  # ]:          0 :         if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
    1998                 :          0 :                 inet_twsk_put(inet_twsk(sk));
    1999                 :          0 :                 goto discard_it;
    2000                 :            :         }
    2001                 :            : 
    2002                 :          0 :         tcp_v4_fill_cb(skb, iph, th);
    2003                 :            : 
    2004         [ #  # ]:          0 :         if (tcp_checksum_complete(skb)) {
    2005                 :          0 :                 inet_twsk_put(inet_twsk(sk));
    2006                 :          0 :                 goto csum_error;
    2007                 :            :         }
    2008   [ #  #  #  # ]:          0 :         switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
    2009                 :            :         case TCP_TW_SYN: {
    2010                 :          0 :                 struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
    2011                 :            :                                                         &tcp_hashinfo, skb,
    2012                 :            :                                                         __tcp_hdrlen(th),
    2013                 :            :                                                         iph->saddr, th->source,
    2014                 :            :                                                         iph->daddr, th->dest,
    2015                 :            :                                                         inet_iif(skb),
    2016                 :            :                                                         sdif);
    2017         [ #  # ]:          0 :                 if (sk2) {
    2018                 :          0 :                         inet_twsk_deschedule_put(inet_twsk(sk));
    2019                 :            :                         sk = sk2;
    2020                 :            :                         tcp_v4_restore_cb(skb);
    2021                 :          0 :                         refcounted = false;
    2022                 :          0 :                         goto process;
    2023                 :            :                 }
    2024                 :            :         }
    2025                 :            :                 /* to ACK */
    2026                 :            :                 /* fall through */
    2027                 :            :         case TCP_TW_ACK:
    2028                 :          0 :                 tcp_v4_timewait_ack(sk, skb);
    2029                 :          0 :                 break;
    2030                 :            :         case TCP_TW_RST:
    2031                 :          0 :                 tcp_v4_send_reset(sk, skb);
    2032                 :          0 :                 inet_twsk_deschedule_put(inet_twsk(sk));
    2033                 :          0 :                 goto discard_it;
    2034                 :            :         case TCP_TW_SUCCESS:;
    2035                 :            :         }
    2036                 :            :         goto discard_it;
    2037                 :            : }
    2038                 :            : 
    2039                 :            : static struct timewait_sock_ops tcp_timewait_sock_ops = {
    2040                 :            :         .twsk_obj_size  = sizeof(struct tcp_timewait_sock),
    2041                 :            :         .twsk_unique    = tcp_twsk_unique,
    2042                 :            :         .twsk_destructor= tcp_twsk_destructor,
    2043                 :            : };
    2044                 :            : 
    2045                 :          0 : void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)
    2046                 :            : {
    2047                 :            :         struct dst_entry *dst = skb_dst(skb);
    2048                 :            : 
    2049   [ #  #  #  # ]:          0 :         if (dst && dst_hold_safe(dst)) {
    2050                 :          0 :                 sk->sk_rx_dst = dst;
    2051                 :          0 :                 inet_sk(sk)->rx_dst_ifindex = skb->skb_iif;
    2052                 :            :         }
    2053                 :          0 : }
    2054                 :            : EXPORT_SYMBOL(inet_sk_rx_dst_set);
    2055                 :            : 
    2056                 :            : const struct inet_connection_sock_af_ops ipv4_specific = {
    2057                 :            :         .queue_xmit        = ip_queue_xmit,
    2058                 :            :         .send_check        = tcp_v4_send_check,
    2059                 :            :         .rebuild_header    = inet_sk_rebuild_header,
    2060                 :            :         .sk_rx_dst_set     = inet_sk_rx_dst_set,
    2061                 :            :         .conn_request      = tcp_v4_conn_request,
    2062                 :            :         .syn_recv_sock     = tcp_v4_syn_recv_sock,
    2063                 :            :         .net_header_len    = sizeof(struct iphdr),
    2064                 :            :         .setsockopt        = ip_setsockopt,
    2065                 :            :         .getsockopt        = ip_getsockopt,
    2066                 :            :         .addr2sockaddr     = inet_csk_addr2sockaddr,
    2067                 :            :         .sockaddr_len      = sizeof(struct sockaddr_in),
    2068                 :            : #ifdef CONFIG_COMPAT
    2069                 :            :         .compat_setsockopt = compat_ip_setsockopt,
    2070                 :            :         .compat_getsockopt = compat_ip_getsockopt,
    2071                 :            : #endif
    2072                 :            :         .mtu_reduced       = tcp_v4_mtu_reduced,
    2073                 :            : };
    2074                 :            : EXPORT_SYMBOL(ipv4_specific);
    2075                 :            : 
    2076                 :            : #ifdef CONFIG_TCP_MD5SIG
    2077                 :            : static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
    2078                 :            :         .md5_lookup             = tcp_v4_md5_lookup,
    2079                 :            :         .calc_md5_hash          = tcp_v4_md5_hash_skb,
    2080                 :            :         .md5_parse              = tcp_v4_parse_md5_keys,
    2081                 :            : };
    2082                 :            : #endif
    2083                 :            : 
    2084                 :            : /* NOTE: A lot of things set to zero explicitly by call to
    2085                 :            :  *       sk_alloc() so need not be done here.
    2086                 :            :  */
    2087                 :          0 : static int tcp_v4_init_sock(struct sock *sk)
    2088                 :            : {
    2089                 :            :         struct inet_connection_sock *icsk = inet_csk(sk);
    2090                 :            : 
    2091                 :          0 :         tcp_init_sock(sk);
    2092                 :            : 
    2093                 :          0 :         icsk->icsk_af_ops = &ipv4_specific;
    2094                 :            : 
    2095                 :            : #ifdef CONFIG_TCP_MD5SIG
    2096                 :            :         tcp_sk(sk)->af_specific = &tcp_sock_ipv4_specific;
    2097                 :            : #endif
    2098                 :            : 
    2099                 :          0 :         return 0;
    2100                 :            : }
    2101                 :            : 
    2102                 :          0 : void tcp_v4_destroy_sock(struct sock *sk)
    2103                 :            : {
    2104                 :            :         struct tcp_sock *tp = tcp_sk(sk);
    2105                 :            : 
    2106                 :          0 :         trace_tcp_destroy_sock(sk);
    2107                 :            : 
    2108                 :          0 :         tcp_clear_xmit_timers(sk);
    2109                 :            : 
    2110                 :          0 :         tcp_cleanup_congestion_control(sk);
    2111                 :            : 
    2112                 :          0 :         tcp_cleanup_ulp(sk);
    2113                 :            : 
    2114                 :            :         /* Cleanup up the write buffer. */
    2115                 :          0 :         tcp_write_queue_purge(sk);
    2116                 :            : 
    2117                 :            :         /* Check if we want to disable active TFO */
    2118                 :          0 :         tcp_fastopen_active_disable_ofo_check(sk);
    2119                 :            : 
    2120                 :            :         /* Cleans up our, hopefully empty, out_of_order_queue. */
    2121                 :          0 :         skb_rbtree_purge(&tp->out_of_order_queue);
    2122                 :            : 
    2123                 :            : #ifdef CONFIG_TCP_MD5SIG
    2124                 :            :         /* Clean up the MD5 key list, if any */
    2125                 :            :         if (tp->md5sig_info) {
    2126                 :            :                 tcp_clear_md5_list(sk);
    2127                 :            :                 kfree_rcu(rcu_dereference_protected(tp->md5sig_info, 1), rcu);
    2128                 :            :                 tp->md5sig_info = NULL;
    2129                 :            :         }
    2130                 :            : #endif
    2131                 :            : 
    2132                 :            :         /* Clean up a referenced TCP bind bucket. */
    2133         [ #  # ]:          0 :         if (inet_csk(sk)->icsk_bind_hash)
    2134                 :          0 :                 inet_put_port(sk);
    2135                 :            : 
    2136         [ #  # ]:          0 :         BUG_ON(rcu_access_pointer(tp->fastopen_rsk));
    2137                 :            : 
    2138                 :            :         /* If socket is aborted during connect operation */
    2139                 :          0 :         tcp_free_fastopen_req(tp);
    2140                 :          0 :         tcp_fastopen_destroy_cipher(sk);
    2141                 :            :         tcp_saved_syn_free(tp);
    2142                 :            : 
    2143                 :          0 :         sk_sockets_allocated_dec(sk);
    2144                 :          0 : }
    2145                 :            : EXPORT_SYMBOL(tcp_v4_destroy_sock);
    2146                 :            : 
    2147                 :            : #ifdef CONFIG_PROC_FS
    2148                 :            : /* Proc filesystem TCP sock list dumping. */
    2149                 :            : 
    2150                 :            : /*
    2151                 :            :  * Get next listener socket follow cur.  If cur is NULL, get first socket
    2152                 :            :  * starting from bucket given in st->bucket; when st->bucket is zero the
    2153                 :            :  * very first socket in the hash table is returned.
    2154                 :            :  */
    2155                 :          0 : static void *listening_get_next(struct seq_file *seq, void *cur)
    2156                 :            : {
    2157                 :          0 :         struct tcp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file));
    2158                 :          0 :         struct tcp_iter_state *st = seq->private;
    2159                 :            :         struct net *net = seq_file_net(seq);
    2160                 :            :         struct inet_listen_hashbucket *ilb;
    2161                 :            :         struct hlist_nulls_node *node;
    2162                 :            :         struct sock *sk = cur;
    2163                 :            : 
    2164         [ #  # ]:          0 :         if (!sk) {
    2165                 :            : get_head:
    2166                 :          0 :                 ilb = &tcp_hashinfo.listening_hash[st->bucket];
    2167                 :            :                 spin_lock(&ilb->lock);
    2168                 :          0 :                 sk = sk_nulls_head(&ilb->nulls_head);
    2169                 :          0 :                 st->offset = 0;
    2170                 :          0 :                 goto get_sk;
    2171                 :            :         }
    2172                 :          0 :         ilb = &tcp_hashinfo.listening_hash[st->bucket];
    2173                 :          0 :         ++st->num;
    2174                 :          0 :         ++st->offset;
    2175                 :            : 
    2176                 :            :         sk = sk_nulls_next(sk);
    2177                 :            : get_sk:
    2178   [ #  #  #  # ]:          0 :         sk_nulls_for_each_from(sk, node) {
    2179         [ #  # ]:          0 :                 if (!net_eq(sock_net(sk), net))
    2180                 :          0 :                         continue;
    2181         [ #  # ]:          0 :                 if (sk->sk_family == afinfo->family)
    2182                 :          0 :                         return sk;
    2183                 :            :         }
    2184                 :            :         spin_unlock(&ilb->lock);
    2185                 :          0 :         st->offset = 0;
    2186         [ #  # ]:          0 :         if (++st->bucket < INET_LHTABLE_SIZE)
    2187                 :            :                 goto get_head;
    2188                 :            :         return NULL;
    2189                 :            : }
    2190                 :            : 
    2191                 :          0 : static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
    2192                 :            : {
    2193                 :          0 :         struct tcp_iter_state *st = seq->private;
    2194                 :            :         void *rc;
    2195                 :            : 
    2196                 :          0 :         st->bucket = 0;
    2197                 :          0 :         st->offset = 0;
    2198                 :          0 :         rc = listening_get_next(seq, NULL);
    2199                 :            : 
    2200   [ #  #  #  # ]:          0 :         while (rc && *pos) {
    2201                 :          0 :                 rc = listening_get_next(seq, rc);
    2202                 :          0 :                 --*pos;
    2203                 :            :         }
    2204                 :          0 :         return rc;
    2205                 :            : }
    2206                 :            : 
    2207                 :            : static inline bool empty_bucket(const struct tcp_iter_state *st)
    2208                 :            : {
    2209                 :          0 :         return hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].chain);
    2210                 :            : }
    2211                 :            : 
    2212                 :            : /*
    2213                 :            :  * Get first established socket starting from bucket given in st->bucket.
    2214                 :            :  * If st->bucket is zero, the very first socket in the hash is returned.
    2215                 :            :  */
    2216                 :          0 : static void *established_get_first(struct seq_file *seq)
    2217                 :            : {
    2218                 :          0 :         struct tcp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file));
    2219                 :          0 :         struct tcp_iter_state *st = seq->private;
    2220                 :            :         struct net *net = seq_file_net(seq);
    2221                 :            :         void *rc = NULL;
    2222                 :            : 
    2223                 :          0 :         st->offset = 0;
    2224         [ #  # ]:          0 :         for (; st->bucket <= tcp_hashinfo.ehash_mask; ++st->bucket) {
    2225                 :            :                 struct sock *sk;
    2226                 :            :                 struct hlist_nulls_node *node;
    2227                 :            :                 spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
    2228                 :            : 
    2229                 :            :                 /* Lockless fast path for the common case of empty buckets */
    2230         [ #  # ]:          0 :                 if (empty_bucket(st))
    2231                 :          0 :                         continue;
    2232                 :            : 
    2233                 :            :                 spin_lock_bh(lock);
    2234         [ #  # ]:          0 :                 sk_nulls_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
    2235   [ #  #  #  # ]:          0 :                         if (sk->sk_family != afinfo->family ||
    2236                 :            :                             !net_eq(sock_net(sk), net)) {
    2237                 :          0 :                                 continue;
    2238                 :            :                         }
    2239                 :          0 :                         rc = sk;
    2240                 :          0 :                         goto out;
    2241                 :            :                 }
    2242                 :            :                 spin_unlock_bh(lock);
    2243                 :            :         }
    2244                 :            : out:
    2245                 :          0 :         return rc;
    2246                 :            : }
    2247                 :            : 
    2248                 :          0 : static void *established_get_next(struct seq_file *seq, void *cur)
    2249                 :            : {
    2250                 :          0 :         struct tcp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file));
    2251                 :            :         struct sock *sk = cur;
    2252                 :            :         struct hlist_nulls_node *node;
    2253                 :          0 :         struct tcp_iter_state *st = seq->private;
    2254                 :            :         struct net *net = seq_file_net(seq);
    2255                 :            : 
    2256                 :          0 :         ++st->num;
    2257                 :          0 :         ++st->offset;
    2258                 :            : 
    2259                 :            :         sk = sk_nulls_next(sk);
    2260                 :            : 
    2261   [ #  #  #  # ]:          0 :         sk_nulls_for_each_from(sk, node) {
    2262   [ #  #  #  # ]:          0 :                 if (sk->sk_family == afinfo->family &&
    2263                 :            :                     net_eq(sock_net(sk), net))
    2264                 :          0 :                         return sk;
    2265                 :            :         }
    2266                 :            : 
    2267                 :          0 :         spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
    2268                 :          0 :         ++st->bucket;
    2269                 :          0 :         return established_get_first(seq);
    2270                 :            : }
    2271                 :            : 
    2272                 :          0 : static void *established_get_idx(struct seq_file *seq, loff_t pos)
    2273                 :            : {
    2274                 :          0 :         struct tcp_iter_state *st = seq->private;
    2275                 :            :         void *rc;
    2276                 :            : 
    2277                 :          0 :         st->bucket = 0;
    2278                 :          0 :         rc = established_get_first(seq);
    2279                 :            : 
    2280         [ #  # ]:          0 :         while (rc && pos) {
    2281                 :          0 :                 rc = established_get_next(seq, rc);
    2282                 :          0 :                 --pos;
    2283                 :            :         }
    2284                 :          0 :         return rc;
    2285                 :            : }
    2286                 :            : 
    2287                 :          0 : static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
    2288                 :            : {
    2289                 :            :         void *rc;
    2290                 :          0 :         struct tcp_iter_state *st = seq->private;
    2291                 :            : 
    2292                 :          0 :         st->state = TCP_SEQ_STATE_LISTENING;
    2293                 :          0 :         rc        = listening_get_idx(seq, &pos);
    2294                 :            : 
    2295         [ #  # ]:          0 :         if (!rc) {
    2296                 :          0 :                 st->state = TCP_SEQ_STATE_ESTABLISHED;
    2297                 :          0 :                 rc        = established_get_idx(seq, pos);
    2298                 :            :         }
    2299                 :            : 
    2300                 :          0 :         return rc;
    2301                 :            : }
    2302                 :            : 
    2303                 :          0 : static void *tcp_seek_last_pos(struct seq_file *seq)
    2304                 :            : {
    2305                 :          0 :         struct tcp_iter_state *st = seq->private;
    2306                 :          0 :         int offset = st->offset;
    2307                 :          0 :         int orig_num = st->num;
    2308                 :            :         void *rc = NULL;
    2309                 :            : 
    2310      [ #  #  # ]:          0 :         switch (st->state) {
    2311                 :            :         case TCP_SEQ_STATE_LISTENING:
    2312         [ #  # ]:          0 :                 if (st->bucket >= INET_LHTABLE_SIZE)
    2313                 :            :                         break;
    2314                 :          0 :                 st->state = TCP_SEQ_STATE_LISTENING;
    2315                 :          0 :                 rc = listening_get_next(seq, NULL);
    2316   [ #  #  #  # ]:          0 :                 while (offset-- && rc)
    2317                 :          0 :                         rc = listening_get_next(seq, rc);
    2318         [ #  # ]:          0 :                 if (rc)
    2319                 :            :                         break;
    2320                 :          0 :                 st->bucket = 0;
    2321                 :          0 :                 st->state = TCP_SEQ_STATE_ESTABLISHED;
    2322                 :            :                 /* Fallthrough */
    2323                 :            :         case TCP_SEQ_STATE_ESTABLISHED:
    2324         [ #  # ]:          0 :                 if (st->bucket > tcp_hashinfo.ehash_mask)
    2325                 :            :                         break;
    2326                 :          0 :                 rc = established_get_first(seq);
    2327   [ #  #  #  # ]:          0 :                 while (offset-- && rc)
    2328                 :          0 :                         rc = established_get_next(seq, rc);
    2329                 :            :         }
    2330                 :            : 
    2331                 :          0 :         st->num = orig_num;
    2332                 :            : 
    2333                 :          0 :         return rc;
    2334                 :            : }
    2335                 :            : 
    2336                 :          0 : void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
    2337                 :            : {
    2338                 :          0 :         struct tcp_iter_state *st = seq->private;
    2339                 :            :         void *rc;
    2340                 :            : 
    2341   [ #  #  #  # ]:          0 :         if (*pos && *pos == st->last_pos) {
    2342                 :          0 :                 rc = tcp_seek_last_pos(seq);
    2343         [ #  # ]:          0 :                 if (rc)
    2344                 :            :                         goto out;
    2345                 :            :         }
    2346                 :            : 
    2347                 :          0 :         st->state = TCP_SEQ_STATE_LISTENING;
    2348                 :          0 :         st->num = 0;
    2349                 :          0 :         st->bucket = 0;
    2350                 :          0 :         st->offset = 0;
    2351         [ #  # ]:          0 :         rc = *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
    2352                 :            : 
    2353                 :            : out:
    2354                 :          0 :         st->last_pos = *pos;
    2355                 :          0 :         return rc;
    2356                 :            : }
    2357                 :            : EXPORT_SYMBOL(tcp_seq_start);
    2358                 :            : 
    2359                 :          0 : void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
    2360                 :            : {
    2361                 :          0 :         struct tcp_iter_state *st = seq->private;
    2362                 :            :         void *rc = NULL;
    2363                 :            : 
    2364         [ #  # ]:          0 :         if (v == SEQ_START_TOKEN) {
    2365                 :          0 :                 rc = tcp_get_idx(seq, 0);
    2366                 :          0 :                 goto out;
    2367                 :            :         }
    2368                 :            : 
    2369      [ #  #  # ]:          0 :         switch (st->state) {
    2370                 :            :         case TCP_SEQ_STATE_LISTENING:
    2371                 :          0 :                 rc = listening_get_next(seq, v);
    2372         [ #  # ]:          0 :                 if (!rc) {
    2373                 :          0 :                         st->state = TCP_SEQ_STATE_ESTABLISHED;
    2374                 :          0 :                         st->bucket = 0;
    2375                 :          0 :                         st->offset = 0;
    2376                 :          0 :                         rc        = established_get_first(seq);
    2377                 :            :                 }
    2378                 :            :                 break;
    2379                 :            :         case TCP_SEQ_STATE_ESTABLISHED:
    2380                 :          0 :                 rc = established_get_next(seq, v);
    2381                 :          0 :                 break;
    2382                 :            :         }
    2383                 :            : out:
    2384                 :          0 :         ++*pos;
    2385                 :          0 :         st->last_pos = *pos;
    2386                 :          0 :         return rc;
    2387                 :            : }
    2388                 :            : EXPORT_SYMBOL(tcp_seq_next);
    2389                 :            : 
    2390                 :          0 : void tcp_seq_stop(struct seq_file *seq, void *v)
    2391                 :            : {
    2392                 :          0 :         struct tcp_iter_state *st = seq->private;
    2393                 :            : 
    2394      [ #  #  # ]:          0 :         switch (st->state) {
    2395                 :            :         case TCP_SEQ_STATE_LISTENING:
    2396         [ #  # ]:          0 :                 if (v != SEQ_START_TOKEN)
    2397                 :          0 :                         spin_unlock(&tcp_hashinfo.listening_hash[st->bucket].lock);
    2398                 :            :                 break;
    2399                 :            :         case TCP_SEQ_STATE_ESTABLISHED:
    2400         [ #  # ]:          0 :                 if (v)
    2401                 :          0 :                         spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
    2402                 :            :                 break;
    2403                 :            :         }
    2404                 :          0 : }
    2405                 :            : EXPORT_SYMBOL(tcp_seq_stop);
    2406                 :            : 
    2407                 :          0 : static void get_openreq4(const struct request_sock *req,
    2408                 :            :                          struct seq_file *f, int i)
    2409                 :            : {
    2410                 :            :         const struct inet_request_sock *ireq = inet_rsk(req);
    2411                 :          0 :         long delta = req->rsk_timer.expires - jiffies;
    2412                 :            : 
    2413                 :          0 :         seq_printf(f, "%4d: %08X:%04X %08X:%04X"
    2414                 :            :                 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %u %d %pK",
    2415                 :            :                 i,
    2416                 :            :                 ireq->ir_loc_addr,
    2417                 :          0 :                 ireq->ir_num,
    2418                 :            :                 ireq->ir_rmt_addr,
    2419                 :          0 :                 ntohs(ireq->ir_rmt_port),
    2420                 :            :                 TCP_SYN_RECV,
    2421                 :            :                 0, 0, /* could print option size, but that is af dependent. */
    2422                 :            :                 1,    /* timers active (only the expire timer) */
    2423                 :            :                 jiffies_delta_to_clock_t(delta),
    2424                 :          0 :                 req->num_timeout,
    2425                 :            :                 from_kuid_munged(seq_user_ns(f),
    2426                 :            :                                  sock_i_uid(req->rsk_listener)),
    2427                 :            :                 0,  /* non standard timer */
    2428                 :            :                 0, /* open_requests have no inode */
    2429                 :            :                 0,
    2430                 :            :                 req);
    2431                 :          0 : }
    2432                 :            : 
    2433                 :          0 : static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i)
    2434                 :            : {
    2435                 :            :         int timer_active;
    2436                 :            :         unsigned long timer_expires;
    2437                 :            :         const struct tcp_sock *tp = tcp_sk(sk);
    2438                 :            :         const struct inet_connection_sock *icsk = inet_csk(sk);
    2439                 :            :         const struct inet_sock *inet = inet_sk(sk);
    2440                 :            :         const struct fastopen_queue *fastopenq = &icsk->icsk_accept_queue.fastopenq;
    2441                 :          0 :         __be32 dest = inet->inet_daddr;
    2442                 :          0 :         __be32 src = inet->inet_rcv_saddr;
    2443                 :          0 :         __u16 destp = ntohs(inet->inet_dport);
    2444                 :          0 :         __u16 srcp = ntohs(inet->inet_sport);
    2445                 :            :         int rx_queue;
    2446                 :            :         int state;
    2447                 :            : 
    2448         [ #  # ]:          0 :         if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
    2449         [ #  # ]:          0 :             icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
    2450                 :            :             icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
    2451                 :            :                 timer_active    = 1;
    2452                 :          0 :                 timer_expires   = icsk->icsk_timeout;
    2453         [ #  # ]:          0 :         } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
    2454                 :            :                 timer_active    = 4;
    2455                 :          0 :                 timer_expires   = icsk->icsk_timeout;
    2456         [ #  # ]:          0 :         } else if (timer_pending(&sk->sk_timer)) {
    2457                 :            :                 timer_active    = 2;
    2458                 :          0 :                 timer_expires   = sk->sk_timer.expires;
    2459                 :            :         } else {
    2460                 :            :                 timer_active    = 0;
    2461                 :          0 :                 timer_expires = jiffies;
    2462                 :            :         }
    2463                 :            : 
    2464                 :            :         state = inet_sk_state_load(sk);
    2465         [ #  # ]:          0 :         if (state == TCP_LISTEN)
    2466                 :          0 :                 rx_queue = sk->sk_ack_backlog;
    2467                 :            :         else
    2468                 :            :                 /* Because we don't lock the socket,
    2469                 :            :                  * we might find a transient negative value.
    2470                 :            :                  */
    2471                 :          0 :                 rx_queue = max_t(int, READ_ONCE(tp->rcv_nxt) -
    2472                 :            :                                       READ_ONCE(tp->copied_seq), 0);
    2473                 :            : 
    2474         [ #  # ]:          0 :         seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
    2475                 :            :                         "%08X %5u %8d %lu %d %pK %lu %lu %u %u %d",
    2476                 :            :                 i, src, srcp, dest, destp, state,
    2477                 :          0 :                 READ_ONCE(tp->write_seq) - tp->snd_una,
    2478                 :            :                 rx_queue,
    2479                 :            :                 timer_active,
    2480                 :          0 :                 jiffies_delta_to_clock_t(timer_expires - jiffies),
    2481                 :          0 :                 icsk->icsk_retransmits,
    2482                 :            :                 from_kuid_munged(seq_user_ns(f), sock_i_uid(sk)),
    2483                 :          0 :                 icsk->icsk_probes_out,
    2484                 :            :                 sock_i_ino(sk),
    2485                 :            :                 refcount_read(&sk->sk_refcnt), sk,
    2486                 :          0 :                 jiffies_to_clock_t(icsk->icsk_rto),
    2487                 :          0 :                 jiffies_to_clock_t(icsk->icsk_ack.ato),
    2488                 :          0 :                 (icsk->icsk_ack.quick << 1) | inet_csk_in_pingpong_mode(sk),
    2489                 :            :                 tp->snd_cwnd,
    2490                 :            :                 state == TCP_LISTEN ?
    2491                 :          0 :                     fastopenq->max_qlen :
    2492         [ #  # ]:          0 :                     (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh));
    2493                 :          0 : }
    2494                 :            : 
    2495                 :          0 : static void get_timewait4_sock(const struct inet_timewait_sock *tw,
    2496                 :            :                                struct seq_file *f, int i)
    2497                 :            : {
    2498                 :          0 :         long delta = tw->tw_timer.expires - jiffies;
    2499                 :            :         __be32 dest, src;
    2500                 :            :         __u16 destp, srcp;
    2501                 :            : 
    2502                 :          0 :         dest  = tw->tw_daddr;
    2503                 :          0 :         src   = tw->tw_rcv_saddr;
    2504                 :          0 :         destp = ntohs(tw->tw_dport);
    2505                 :          0 :         srcp  = ntohs(tw->tw_sport);
    2506                 :            : 
    2507                 :          0 :         seq_printf(f, "%4d: %08X:%04X %08X:%04X"
    2508                 :            :                 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK",
    2509                 :          0 :                 i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
    2510                 :            :                 3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
    2511                 :            :                 refcount_read(&tw->tw_refcnt), tw);
    2512                 :          0 : }
    2513                 :            : 
    2514                 :            : #define TMPSZ 150
    2515                 :            : 
    2516                 :          0 : static int tcp4_seq_show(struct seq_file *seq, void *v)
    2517                 :            : {
    2518                 :            :         struct tcp_iter_state *st;
    2519                 :            :         struct sock *sk = v;
    2520                 :            : 
    2521                 :            :         seq_setwidth(seq, TMPSZ - 1);
    2522         [ #  # ]:          0 :         if (v == SEQ_START_TOKEN) {
    2523                 :          0 :                 seq_puts(seq, "  sl  local_address rem_address   st tx_queue "
    2524                 :            :                            "rx_queue tr tm->when retrnsmt   uid  timeout "
    2525                 :            :                            "inode");
    2526                 :          0 :                 goto out;
    2527                 :            :         }
    2528                 :          0 :         st = seq->private;
    2529                 :            : 
    2530         [ #  # ]:          0 :         if (sk->sk_state == TCP_TIME_WAIT)
    2531                 :          0 :                 get_timewait4_sock(v, seq, st->num);
    2532         [ #  # ]:          0 :         else if (sk->sk_state == TCP_NEW_SYN_RECV)
    2533                 :          0 :                 get_openreq4(v, seq, st->num);
    2534                 :            :         else
    2535                 :          0 :                 get_tcp4_sock(v, seq, st->num);
    2536                 :            : out:
    2537                 :          0 :         seq_pad(seq, '\n');
    2538                 :          0 :         return 0;
    2539                 :            : }
    2540                 :            : 
    2541                 :            : static const struct seq_operations tcp4_seq_ops = {
    2542                 :            :         .show           = tcp4_seq_show,
    2543                 :            :         .start          = tcp_seq_start,
    2544                 :            :         .next           = tcp_seq_next,
    2545                 :            :         .stop           = tcp_seq_stop,
    2546                 :            : };
    2547                 :            : 
    2548                 :            : static struct tcp_seq_afinfo tcp4_seq_afinfo = {
    2549                 :            :         .family         = AF_INET,
    2550                 :            : };
    2551                 :            : 
    2552                 :        207 : static int __net_init tcp4_proc_init_net(struct net *net)
    2553                 :            : {
    2554         [ +  - ]:        207 :         if (!proc_create_net_data("tcp", 0444, net->proc_net, &tcp4_seq_ops,
    2555                 :            :                         sizeof(struct tcp_iter_state), &tcp4_seq_afinfo))
    2556                 :            :                 return -ENOMEM;
    2557                 :        207 :         return 0;
    2558                 :            : }
    2559                 :            : 
    2560                 :          0 : static void __net_exit tcp4_proc_exit_net(struct net *net)
    2561                 :            : {
    2562                 :          0 :         remove_proc_entry("tcp", net->proc_net);
    2563                 :          0 : }
    2564                 :            : 
    2565                 :            : static struct pernet_operations tcp4_net_ops = {
    2566                 :            :         .init = tcp4_proc_init_net,
    2567                 :            :         .exit = tcp4_proc_exit_net,
    2568                 :            : };
    2569                 :            : 
    2570                 :        207 : int __init tcp4_proc_init(void)
    2571                 :            : {
    2572                 :        207 :         return register_pernet_subsys(&tcp4_net_ops);
    2573                 :            : }
    2574                 :            : 
    2575                 :          0 : void tcp4_proc_exit(void)
    2576                 :            : {
    2577                 :          0 :         unregister_pernet_subsys(&tcp4_net_ops);
    2578                 :          0 : }
    2579                 :            : #endif /* CONFIG_PROC_FS */
    2580                 :            : 
    2581                 :            : struct proto tcp_prot = {
    2582                 :            :         .name                   = "TCP",
    2583                 :            :         .owner                  = THIS_MODULE,
    2584                 :            :         .close                  = tcp_close,
    2585                 :            :         .pre_connect            = tcp_v4_pre_connect,
    2586                 :            :         .connect                = tcp_v4_connect,
    2587                 :            :         .disconnect             = tcp_disconnect,
    2588                 :            :         .accept                 = inet_csk_accept,
    2589                 :            :         .ioctl                  = tcp_ioctl,
    2590                 :            :         .init                   = tcp_v4_init_sock,
    2591                 :            :         .destroy                = tcp_v4_destroy_sock,
    2592                 :            :         .shutdown               = tcp_shutdown,
    2593                 :            :         .setsockopt             = tcp_setsockopt,
    2594                 :            :         .getsockopt             = tcp_getsockopt,
    2595                 :            :         .keepalive              = tcp_set_keepalive,
    2596                 :            :         .recvmsg                = tcp_recvmsg,
    2597                 :            :         .sendmsg                = tcp_sendmsg,
    2598                 :            :         .sendpage               = tcp_sendpage,
    2599                 :            :         .backlog_rcv            = tcp_v4_do_rcv,
    2600                 :            :         .release_cb             = tcp_release_cb,
    2601                 :            :         .hash                   = inet_hash,
    2602                 :            :         .unhash                 = inet_unhash,
    2603                 :            :         .get_port               = inet_csk_get_port,
    2604                 :            :         .enter_memory_pressure  = tcp_enter_memory_pressure,
    2605                 :            :         .leave_memory_pressure  = tcp_leave_memory_pressure,
    2606                 :            :         .stream_memory_free     = tcp_stream_memory_free,
    2607                 :            :         .sockets_allocated      = &tcp_sockets_allocated,
    2608                 :            :         .orphan_count           = &tcp_orphan_count,
    2609                 :            :         .memory_allocated       = &tcp_memory_allocated,
    2610                 :            :         .memory_pressure        = &tcp_memory_pressure,
    2611                 :            :         .sysctl_mem             = sysctl_tcp_mem,
    2612                 :            :         .sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_tcp_wmem),
    2613                 :            :         .sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_tcp_rmem),
    2614                 :            :         .max_header             = MAX_TCP_HEADER,
    2615                 :            :         .obj_size               = sizeof(struct tcp_sock),
    2616                 :            :         .slab_flags             = SLAB_TYPESAFE_BY_RCU,
    2617                 :            :         .twsk_prot              = &tcp_timewait_sock_ops,
    2618                 :            :         .rsk_prot               = &tcp_request_sock_ops,
    2619                 :            :         .h.hashinfo             = &tcp_hashinfo,
    2620                 :            :         .no_autobind            = true,
    2621                 :            : #ifdef CONFIG_COMPAT
    2622                 :            :         .compat_setsockopt      = compat_tcp_setsockopt,
    2623                 :            :         .compat_getsockopt      = compat_tcp_getsockopt,
    2624                 :            : #endif
    2625                 :            :         .diag_destroy           = tcp_abort,
    2626                 :            : };
    2627                 :            : EXPORT_SYMBOL(tcp_prot);
    2628                 :            : 
    2629                 :          0 : static void __net_exit tcp_sk_exit(struct net *net)
    2630                 :            : {
    2631                 :            :         int cpu;
    2632                 :            : 
    2633         [ #  # ]:          0 :         if (net->ipv4.tcp_congestion_control)
    2634                 :          0 :                 module_put(net->ipv4.tcp_congestion_control->owner);
    2635                 :            : 
    2636         [ #  # ]:          0 :         for_each_possible_cpu(cpu)
    2637                 :          0 :                 inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.tcp_sk, cpu));
    2638                 :          0 :         free_percpu(net->ipv4.tcp_sk);
    2639                 :          0 : }
    2640                 :            : 
    2641                 :        207 : static int __net_init tcp_sk_init(struct net *net)
    2642                 :            : {
    2643                 :            :         int res, cpu, cnt;
    2644                 :            : 
    2645                 :        207 :         net->ipv4.tcp_sk = alloc_percpu(struct sock *);
    2646         [ +  - ]:        207 :         if (!net->ipv4.tcp_sk)
    2647                 :            :                 return -ENOMEM;
    2648                 :            : 
    2649         [ +  + ]:       1035 :         for_each_possible_cpu(cpu) {
    2650                 :            :                 struct sock *sk;
    2651                 :            : 
    2652                 :        828 :                 res = inet_ctl_sock_create(&sk, PF_INET, SOCK_RAW,
    2653                 :            :                                            IPPROTO_TCP, net);
    2654         [ +  - ]:        828 :                 if (res)
    2655                 :            :                         goto fail;
    2656                 :        828 :                 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
    2657                 :            : 
    2658                 :            :                 /* Please enforce IP_DF and IPID==0 for RST and
    2659                 :            :                  * ACK sent in SYN-RECV and TIME-WAIT state.
    2660                 :            :                  */
    2661                 :        828 :                 inet_sk(sk)->pmtudisc = IP_PMTUDISC_DO;
    2662                 :            : 
    2663                 :        828 :                 *per_cpu_ptr(net->ipv4.tcp_sk, cpu) = sk;
    2664                 :            :         }
    2665                 :            : 
    2666                 :        207 :         net->ipv4.sysctl_tcp_ecn = 2;
    2667                 :        207 :         net->ipv4.sysctl_tcp_ecn_fallback = 1;
    2668                 :            : 
    2669                 :        207 :         net->ipv4.sysctl_tcp_base_mss = TCP_BASE_MSS;
    2670                 :        207 :         net->ipv4.sysctl_tcp_min_snd_mss = TCP_MIN_SND_MSS;
    2671                 :        207 :         net->ipv4.sysctl_tcp_probe_threshold = TCP_PROBE_THRESHOLD;
    2672                 :        207 :         net->ipv4.sysctl_tcp_probe_interval = TCP_PROBE_INTERVAL;
    2673                 :        207 :         net->ipv4.sysctl_tcp_mtu_probe_floor = TCP_MIN_SND_MSS;
    2674                 :            : 
    2675                 :        207 :         net->ipv4.sysctl_tcp_keepalive_time = TCP_KEEPALIVE_TIME;
    2676                 :        207 :         net->ipv4.sysctl_tcp_keepalive_probes = TCP_KEEPALIVE_PROBES;
    2677                 :        207 :         net->ipv4.sysctl_tcp_keepalive_intvl = TCP_KEEPALIVE_INTVL;
    2678                 :            : 
    2679                 :        207 :         net->ipv4.sysctl_tcp_syn_retries = TCP_SYN_RETRIES;
    2680                 :        207 :         net->ipv4.sysctl_tcp_synack_retries = TCP_SYNACK_RETRIES;
    2681                 :        207 :         net->ipv4.sysctl_tcp_syncookies = 1;
    2682                 :        207 :         net->ipv4.sysctl_tcp_reordering = TCP_FASTRETRANS_THRESH;
    2683                 :        207 :         net->ipv4.sysctl_tcp_retries1 = TCP_RETR1;
    2684                 :        207 :         net->ipv4.sysctl_tcp_retries2 = TCP_RETR2;
    2685                 :        207 :         net->ipv4.sysctl_tcp_orphan_retries = 0;
    2686                 :        207 :         net->ipv4.sysctl_tcp_fin_timeout = TCP_FIN_TIMEOUT;
    2687                 :        207 :         net->ipv4.sysctl_tcp_notsent_lowat = UINT_MAX;
    2688                 :        207 :         net->ipv4.sysctl_tcp_tw_reuse = 2;
    2689                 :            : 
    2690                 :        207 :         cnt = tcp_hashinfo.ehash_mask + 1;
    2691                 :        207 :         net->ipv4.tcp_death_row.sysctl_max_tw_buckets = cnt / 2;
    2692                 :        207 :         net->ipv4.tcp_death_row.hashinfo = &tcp_hashinfo;
    2693                 :            : 
    2694                 :        207 :         net->ipv4.sysctl_max_syn_backlog = max(128, cnt / 128);
    2695                 :        207 :         net->ipv4.sysctl_tcp_sack = 1;
    2696                 :        207 :         net->ipv4.sysctl_tcp_window_scaling = 1;
    2697                 :        207 :         net->ipv4.sysctl_tcp_timestamps = 1;
    2698                 :        207 :         net->ipv4.sysctl_tcp_early_retrans = 3;
    2699                 :        207 :         net->ipv4.sysctl_tcp_recovery = TCP_RACK_LOSS_DETECTION;
    2700                 :        207 :         net->ipv4.sysctl_tcp_slow_start_after_idle = 1; /* By default, RFC2861 behavior.  */
    2701                 :        207 :         net->ipv4.sysctl_tcp_retrans_collapse = 1;
    2702                 :        207 :         net->ipv4.sysctl_tcp_max_reordering = 300;
    2703                 :        207 :         net->ipv4.sysctl_tcp_dsack = 1;
    2704                 :        207 :         net->ipv4.sysctl_tcp_app_win = 31;
    2705                 :        207 :         net->ipv4.sysctl_tcp_adv_win_scale = 1;
    2706                 :        207 :         net->ipv4.sysctl_tcp_frto = 2;
    2707                 :        207 :         net->ipv4.sysctl_tcp_moderate_rcvbuf = 1;
    2708                 :            :         /* This limits the percentage of the congestion window which we
    2709                 :            :          * will allow a single TSO frame to consume.  Building TSO frames
    2710                 :            :          * which are too large can cause TCP streams to be bursty.
    2711                 :            :          */
    2712                 :        207 :         net->ipv4.sysctl_tcp_tso_win_divisor = 3;
    2713                 :            :         /* Default TSQ limit of 16 TSO segments */
    2714                 :        207 :         net->ipv4.sysctl_tcp_limit_output_bytes = 16 * 65536;
    2715                 :            :         /* rfc5961 challenge ack rate limiting */
    2716                 :        207 :         net->ipv4.sysctl_tcp_challenge_ack_limit = 1000;
    2717                 :        207 :         net->ipv4.sysctl_tcp_min_tso_segs = 2;
    2718                 :        207 :         net->ipv4.sysctl_tcp_min_rtt_wlen = 300;
    2719                 :        207 :         net->ipv4.sysctl_tcp_autocorking = 1;
    2720                 :        207 :         net->ipv4.sysctl_tcp_invalid_ratelimit = HZ/2;
    2721                 :        207 :         net->ipv4.sysctl_tcp_pacing_ss_ratio = 200;
    2722                 :        207 :         net->ipv4.sysctl_tcp_pacing_ca_ratio = 120;
    2723         [ -  + ]:        207 :         if (net != &init_net) {
    2724                 :          0 :                 memcpy(net->ipv4.sysctl_tcp_rmem,
    2725                 :            :                        init_net.ipv4.sysctl_tcp_rmem,
    2726                 :            :                        sizeof(init_net.ipv4.sysctl_tcp_rmem));
    2727                 :          0 :                 memcpy(net->ipv4.sysctl_tcp_wmem,
    2728                 :            :                        init_net.ipv4.sysctl_tcp_wmem,
    2729                 :            :                        sizeof(init_net.ipv4.sysctl_tcp_wmem));
    2730                 :            :         }
    2731                 :        207 :         net->ipv4.sysctl_tcp_comp_sack_delay_ns = NSEC_PER_MSEC;
    2732                 :        207 :         net->ipv4.sysctl_tcp_comp_sack_nr = 44;
    2733                 :        207 :         net->ipv4.sysctl_tcp_fastopen = TFO_CLIENT_ENABLE;
    2734                 :        207 :         spin_lock_init(&net->ipv4.tcp_fastopen_ctx_lock);
    2735                 :        207 :         net->ipv4.sysctl_tcp_fastopen_blackhole_timeout = 60 * 60;
    2736                 :            :         atomic_set(&net->ipv4.tfo_active_disable_times, 0);
    2737                 :            : 
    2738                 :            :         /* Reno is always built in */
    2739   [ -  +  #  # ]:        207 :         if (!net_eq(net, &init_net) &&
    2740                 :          0 :             try_module_get(init_net.ipv4.tcp_congestion_control->owner))
    2741                 :          0 :                 net->ipv4.tcp_congestion_control = init_net.ipv4.tcp_congestion_control;
    2742                 :            :         else
    2743                 :        207 :                 net->ipv4.tcp_congestion_control = &tcp_reno;
    2744                 :            : 
    2745                 :            :         return 0;
    2746                 :            : fail:
    2747                 :          0 :         tcp_sk_exit(net);
    2748                 :            : 
    2749                 :          0 :         return res;
    2750                 :            : }
    2751                 :            : 
    2752                 :          0 : static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list)
    2753                 :            : {
    2754                 :            :         struct net *net;
    2755                 :            : 
    2756                 :          0 :         inet_twsk_purge(&tcp_hashinfo, AF_INET);
    2757                 :            : 
    2758         [ #  # ]:          0 :         list_for_each_entry(net, net_exit_list, exit_list)
    2759                 :          0 :                 tcp_fastopen_ctx_destroy(net);
    2760                 :          0 : }
    2761                 :            : 
    2762                 :            : static struct pernet_operations __net_initdata tcp_sk_ops = {
    2763                 :            :        .init       = tcp_sk_init,
    2764                 :            :        .exit       = tcp_sk_exit,
    2765                 :            :        .exit_batch = tcp_sk_exit_batch,
    2766                 :            : };
    2767                 :            : 
    2768                 :        207 : void __init tcp_v4_init(void)
    2769                 :            : {
    2770         [ -  + ]:        207 :         if (register_pernet_subsys(&tcp_sk_ops))
    2771                 :          0 :                 panic("Failed to create the TCP control socket.\n");
    2772                 :        207 : }

Generated by: LCOV version 1.14