tcp: snmp stats for Fast Open, SYN rtx, and data pkts

Add the following snmp stats:

TCPFastOpenActiveFail: Fast Open attempts (SYN/data) failed beacuse
the remote does not accept it or the attempts timed out.

TCPSynRetrans: number of SYN and SYN/ACK retransmits to break down
retransmissions into SYN, fast-retransmits, timeout retransmits, etc.

TCPOrigDataSent: number of outgoing packets with original data (excluding
retransmission but including data-in-SYN). This counter is different from
TcpOutSegs because TcpOutSegs also tracks pure ACKs. TCPOrigDataSent is
more useful to track the TCP retransmission rate.

Change TCPFastOpenActive to track only successful Fast Opens to be symmetric to
TCPFastOpenPassive.

Signed-off-by: Yuchung Cheng <ycheng@google.com>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Nandita Dukkipati <nanditad@google.com>
Signed-off-by: Lawrence Brakmo <brakmo@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Conflicts:
	include/uapi/linux/snmp.h
	net/ipv4/proc.c

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
tcp: Use NET_ADD_STATS instead of NET_ADD_STATS_BH in tcp_event_new_data_sent()

Can be invoked from non-BH context.

Based upon a patch by Eric Dumazet.

Fixes: f19c29e3e391 ("tcp: snmp stats for Fast Open, SYN rtx, and data pkts")
Reported-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
ipv6: reuse rt6_need_strict

Move the whole rt6_need_strict as static inline into ip6_route.h,
so that it can be reused

Signed-off-by: Wang Yufen <wangyufen@huawei.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
ipv6: tcp_ipv6 policy route issue

The issue raises when adding policy route, specify a particular
NIC as oif, the policy route did not take effect. The reason is
that fl6.oif is not set and route map failed. From the
tcp_v6_send_response function, if the binding address is linklocal,
fl6.oif is set, but not for global address.

Acked-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
Signed-off-by: Wang Yufen <wangyufen@huawei.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Conflicts:
	net/ipv6/tcp_ipv6.c

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
net: ipv6: Fix oif in TCP SYN+ACK route lookup.

net-next commit 9c76a11, ipv6: tcp_ipv6 policy route issue, had
a boolean logic error that caused incorrect behaviour for TCP
SYN+ACK when oif-based rules are in use. Specifically:

1. If a SYN comes in from a global address, and sk_bound_dev_if
   is not set, the routing lookup has oif set to the interface
   the SYN came in on. Instead, it should have oif unset,
   because for global addresses, the incoming interface doesn't
   necessarily have any bearing on the interface the SYN+ACK is
   sent out on.
2. If a SYN comes in from a link-local address, and
   sk_bound_dev_if is set, the routing lookup has oif set to the
   interface the SYN came in on. Instead, it should have oif set
   to sk_bound_dev_if, because that's what the application
   requested.

Signed-off-by: Lorenzo Colitti <lorenzo@google.com>
Acked-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
net: Generalize checksum_init functions

Create a general __skb_checksum_validate function (actually a
macro) to subsume the various checksum_init functions. This
function can either init the checksum, or do the full validation
(logically checksum_init+skb_check_complete)-- a flag specifies
if full vaidation is performed. Also, there is a flag to the function
to indicate that zero checksums are allowed (to support optional
UDP checksums).

Added several stub functions for calling __skb_checksum_validate.

Signed-off-by: Tom Herbert <therbert@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
net: Call skb_checksum_init in IPv6

Call skb_checksum_init instead of private functions.

Signed-off-by: Tom Herbert <therbert@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
net: Call skb_checksum_init in IPv4

Call skb_checksum_init instead of private functions.

Signed-off-by: Tom Herbert <therbert@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
tcp: move fastopen functions to tcp_fastopen.c

Move common TFO functions that will be used by both v4 and v6
to tcp_fastopen.c. Create a helper tcp_fastopen_queue_check().

Signed-off-by: Yuchung Cheng <ycheng@google.com>
Signed-off-by: Daniel Lee <longinus00@gmail.com>
Signed-off-by: Jerry Chu <hkchu@google.com>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Acked-by: Neal Cardwell <ncardwell@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Conflicts:
	net/ipv4/tcp_ipv4.c

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
tcp: simplify fast open cookie processing

Consolidate various cookie checking and generation code to simplify
the fast open processing. The main goal is to reduce code duplication
in tcp_v4_conn_request() for IPv6 support.

Removes two experimental sysctl flags TFO_SERVER_ALWAYS and
TFO_SERVER_COOKIE_NOT_CHKD used primarily for developmental debugging
purposes.

Signed-off-by: Yuchung Cheng <ycheng@google.com>
Signed-off-by: Daniel Lee <longinus00@gmail.com>
Signed-off-by: Jerry Chu <hkchu@google.com>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Acked-by: Neal Cardwell <ncardwell@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
tcp: use tcp_v4_send_synack on first SYN-ACK

To avoid large code duplication in IPv6, we need to first simplify
the complicate SYN-ACK sending code in tcp_v4_conn_request().

To use tcp_v4(6)_send_synack() to send all SYN-ACKs, we need to
initialize the mini socket's receive window before trying to
create the child socket and/or building the SYN-ACK packet. So we move
that initialization from tcp_make_synack() to tcp_v4_conn_request()
as a new function tcp_openreq_init_req_rwin().

After this refactoring the SYN-ACK sending code is simpler and easier
to implement Fast Open for IPv6.

Signed-off-by: Yuchung Cheng <ycheng@google.com>
Signed-off-by: Daniel Lee <longinus00@gmail.com>
Signed-off-by: Jerry Chu <hkchu@google.com>
Acked-by: Neal Cardwell <ncardwell@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Conflicts:
	net/ipv4/tcp_fastopen.c
	net/ipv4/tcp_ipv4.c
	net/ipv4/tcp_output.c

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
Fix: SYN+ACK w/ MP_JOIN annouces a zero window

Commit "tcp: use tcp_v4_send_synack on first SYN-ACK" introduced
tcp_openreq_init_rwin to extract the window selection out of tcp_make_synack
in tcp_v*_conn_request. We need to reflect this change in mptcp_v*_join_request.

Signed-off-by: Gregory Detal <gregory.detal@uclouvain.be>
Acked-by: Octavian Purdila <octavian.purdila@intel.com>
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
tcp: improve fastopen icmp handling

If a fast open socket is already accepted by the user, it should
be treated like a connected socket to record the ICMP error in
sk_softerr, so the user can fetch it. Do that in both tcp_v4_err
and tcp_v6_err.

Also refactor the sequence window check to improve readability
(e.g., there were two local variables named 'req').

Signed-off-by: Yuchung Cheng <ycheng@google.com>
Signed-off-by: Daniel Lee <longinus00@gmail.com>
Signed-off-by: Jerry Chu <hkchu@google.com>
Acked-by: Neal Cardwell <ncardwell@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Conflicts:
	net/ipv4/tcp_ipv4.c
	net/ipv6/tcp_ipv6.c

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
tcp: IPv6 support for fastopen server

After all the preparatory works, supporting IPv6 in Fast Open is now easy.
We pretty much just mirror v4 code. The only difference is how we
generate the Fast Open cookie for IPv6 sockets. Since Fast Open cookie
is 128 bits and we use AES 128, we use CBC-MAC to encrypt both the
source and destination IPv6 addresses since the cookie is a MAC tag.

Signed-off-by: Daniel Lee <longinus00@gmail.com>
Signed-off-by: Yuchung Cheng <ycheng@google.com>
Signed-off-by: Jerry Chu <hkchu@google.com>
Acked-by: Neal Cardwell <ncardwell@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Conflicts:
	net/ipv6/tcp_ipv6.c

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
tcp: cookie_v4_init_sequence: skb should be const

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
tcp: tcp_v[46]_conn_request: fix snt_synack initialization

Commit 016818d07 (tcp: TCP Fast Open Server - take SYNACK RTT after
completing 3WHS) changes the code to only take a snt_synack timestamp
when a SYNACK transmit or retransmit succeeds. This behaviour is later
broken by commit 843f4a55e (tcp: use tcp_v4_send_synack on first
SYN-ACK), as snt_synack is now updated even if tcp_v4_send_synack
fails.

Also, commit 3a19ce0ee (tcp: IPv6 support for fastopen server) misses
the required IPv6 updates for 016818d07.

This patch makes sure that snt_synack is updated only when the SYNACK
trasnmit/retransmit succeeds, for both IPv4 and IPv6.

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
net: remove inet6_reqsk_alloc

Since pktops is only used for IPv6 only and opts is used for IPv4
only, we can move these fields into a union and this allows us to drop
the inet6_reqsk_alloc function as after this change it becomes
equivalent with inet_reqsk_alloc.

This patch also fixes a kmemcheck issue in the IPv6 stack: the flags
field was not annotated after a request_sock was allocated.

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Conflicts:
	include/net/inet_sock.h
	net/ipv6/tcp_ipv6.c

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
tcp: add init_req method to tcp_request_sock_ops

Move the specific IPv4/IPv6 intializations to a new method in
tcp_request_sock_ops in preparation for unifying tcp_v4_conn_request
and tcp_v6_conn_request.

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Conflicts:
	net/ipv4/tcp_ipv4.c
	net/ipv6/tcp_ipv6.c

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
tcp: add init_cookie_seq method to tcp_request_sock_ops

Move the specific IPv4/IPv6 cookie sequence initialization to a new
method in tcp_request_sock_ops in preparation for unifying
tcp_v4_conn_request and tcp_v6_conn_request.

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>
tcp: add route_req method to tcp_request_sock_ops

Create wrappers with same signature for the IPv4/IPv6 request routing
calls and use these wrappers (via route_req method from
tcp_request_sock_ops) in tcp_v4_conn_request and tcp_v6_conn_request
with the purpose of unifying the two functions in a later patch.

We can later drop the wrapper functions and modify inet_csk_route_req
and inet6_cks_route_req to use the same signature.

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
[backported from net-next]
Signed-off-by: Christoph Paasch <christoph.paasch@uclouvain.be>