From 4708e3146d38df1716f4d05edc3d394d0a994513 Mon Sep 17 00:00:00 2001 From: duxbbo Date: Mon, 18 Jul 2022 06:44:07 +0000 Subject: [PATCH] add tcp support Signed-off-by: duxbbo --- code/net/newip/tcp_nip.c | 1631 +++++++++++++++++++++++++++++ code/net/newip/tcp_nip_input.c | 1694 +++++++++++++++++++++++++++++++ code/net/newip/tcp_nip_output.c | 1257 +++++++++++++++++++++++ 3 files changed, 4582 insertions(+) create mode 100644 code/net/newip/tcp_nip.c create mode 100644 code/net/newip/tcp_nip_input.c create mode 100644 code/net/newip/tcp_nip_output.c diff --git a/code/net/newip/tcp_nip.c b/code/net/newip/tcp_nip.c new file mode 100644 index 0000000..8c27852 --- /dev/null +++ b/code/net/newip/tcp_nip.c @@ -0,0 +1,1631 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * + * NewIP INET + * An implementation of the TCP/IP protocol suite for the LINUX + * operating system. NewIP INET is implemented using the BSD Socket + * interface as the means of communication with the user level. + * + * Implementation of the Transmission Control Protocol(TCP). + * + * TCP over NewIP + * + * Based on net/ipv4/tcp.c + * Based on net/ipv4/tcp_ipv4.c + * Based on net/ipv6/tcp_ipv6.c + * Based on net/core/stream.c + * + * Description of States: + * + * TCP_SYN_SENT sent a connection request, waiting for ack + * + * TCP_SYN_RECV received a connection request, sent ack, + * waiting for final ack in three-way handshake. + * + * TCP_ESTABLISHED connection established + * + * TCP_FIN_WAIT1 our side has shutdown, waiting to complete + * transmission of remaining buffered data + * + * TCP_FIN_WAIT2 all buffered data sent, waiting for remote + * to shutdown + * + * TCP_CLOSING both sides have shutdown but we still have + * data we have to finish sending + * + * TCP_TIME_WAIT timeout to catch resent junk before entering + * closed, can only be entered from FIN_WAIT2 + * or CLOSING. Required because the other end + * may not have gotten our last ACK causing it + * to retransmit the data packet (which we ignore) + * + * TCP_CLOSE_WAIT remote side has shutdown and is waiting for + * us to finish writing our data and to shutdown + * (we have to close() to move on to LAST_ACK) + * + * TCP_LAST_ACK out side has shutdown after remote has + * shutdown. There may still be data in our + * buffer that we have to finish sending + * + * TCP_CLOSE socket is finished + */ +#define pr_fmt(fmt) "NIP-TCP: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "nip_checksum.h" +#include "tcp_nip_parameter.h" + +static const struct inet_connection_sock_af_ops newip_specific; + +static void tcp_nip_push(struct sock *sk, int flags, int mss_now, + int nonagle, int size_goal) +{ + __tcp_nip_push_pending_frames(sk, mss_now, nonagle); +} + +static const unsigned char new_state[16] = { + /* current state: new state: action: */ +[0 /* (Invalid) */] = TCP_CLOSE, +[TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, +[TCP_SYN_SENT] = TCP_CLOSE, +[TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, +[TCP_FIN_WAIT1] = TCP_FIN_WAIT1, +[TCP_FIN_WAIT2] = TCP_FIN_WAIT2, +[TCP_TIME_WAIT] = TCP_CLOSE, +[TCP_CLOSE] = TCP_CLOSE, +[TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, +[TCP_LAST_ACK] = TCP_LAST_ACK, +[TCP_LISTEN] = TCP_CLOSE, +[TCP_CLOSING] = TCP_CLOSING, +[TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ +}; + +bool nip_get_tcp_input_checksum(struct sk_buff *skb) +{ + struct nip_pseudo_header nph = {0}; + + nph.nexthdr = NIPCB(skb)->nexthdr; + nph.saddr = NIPCB(skb)->srcaddr; + nph.daddr = NIPCB(skb)->dstaddr; + + nph.check_len = htons(skb->len); + return nip_check_sum_parse(skb_transport_header(skb), + skb->len, &nph) + == 0xffff ? true : false; +} + +static int tcp_nip_close_state(struct sock *sk) +{ + int next = (int)new_state[sk->sk_state]; + int ns = next & TCP_STATE_MASK; + + tcp_set_state(sk, ns); + + return next & TCP_ACTION_FIN; +} + +void sk_nip_stream_kill_queues(struct sock *sk) +{ + /* First the read buffer. */ + __skb_queue_purge(&sk->sk_receive_queue); + + /* Next, the error queue. */ + __skb_queue_purge(&sk->sk_error_queue); + + /* Next, the write queue. */ + WARN_ON(!skb_queue_empty(&sk->sk_write_queue)); + + WARN_ON(sk->sk_wmem_queued); +} + +void tcp_nip_shutdown(struct sock *sk, int how) +{ + if (!(how & SEND_SHUTDOWN)) + return; + + /* If we've already sent a FIN, or it's a closed state, skip this. */ + if ((1 << sk->sk_state) & + (TCPF_ESTABLISHED | TCPF_SYN_SENT | + TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) { + /* Clear out any half completed packets. FIN if needed. */ + if (tcp_nip_close_state(sk)) + tcp_nip_send_fin(sk); + } +} + +void tcp_nip_close(struct sock *sk, long timeout) +{ + struct sk_buff *skb; + int data_was_unread = 0; + int state; + + lock_sock(sk); + sk->sk_shutdown = SHUTDOWN_MASK; + + DEBUG("%s: sk_state:%d\n", __func__, sk->sk_state); + + if (sk->sk_state == TCP_LISTEN) { + tcp_set_state(sk, TCP_CLOSE); + + inet_csk_listen_stop(sk); + + goto adjudge_to_death; + } + + while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { + u32 len = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq; + + if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) + len--; + data_was_unread += len; + __kfree_skb(skb); + } + + if (sk->sk_state == TCP_CLOSE) + goto adjudge_to_death; + + if (data_was_unread) { + tcp_set_state(sk, TCP_CLOSE); + tcp_nip_send_active_reset(sk, sk->sk_allocation); + } else if (tcp_nip_close_state(sk)) { + /* RED-PEN. Formally speaking, we have broken TCP state + * machine. State transitions: + * + * TCP_ESTABLISHED -> TCP_FIN_WAIT1 + * TCP_SYN_RECV -> TCP_FIN_WAIT1 (forget it, it's impossible) + * TCP_CLOSE_WAIT -> TCP_LAST_ACK + */ + DEBUG("%s: ready to send fin, sk_state:%d\n", __func__, sk->sk_state); + tcp_nip_send_fin(sk); + } + +adjudge_to_death: + state = sk->sk_state; + sock_hold(sk); + sock_orphan(sk); + + /* It is the last release_sock in its life. It will remove backlog. */ + release_sock(sk); + + local_bh_disable(); + bh_lock_sock(sk); + WARN_ON(sock_owned_by_user(sk)); + + this_cpu_dec(*sk->sk_prot->orphan_count); + + if (state != TCP_CLOSE && sk->sk_state == TCP_CLOSE) + goto out; + + if (sk->sk_state == TCP_CLOSE) + inet_csk_destroy_sock(sk); + +out: + bh_unlock_sock(sk); + local_bh_enable(); + sock_put(sk); +} + +/* These states need RST on ABORT according to RFC793 */ +static inline bool tcp_nip_need_reset(int state) +{ + return (1 << state) & + (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_FIN_WAIT1 | + TCPF_FIN_WAIT2 | TCPF_SYN_RECV); +} + +/* Function + * Initialize some of the parameters in request_sock + * Parameter + * req: Request connection control block + * sk_listener: Transmission control block + * skb: Transfer control block buffer + */ +static void tcp_nip_init_req(struct request_sock *req, + const struct sock *sk_listener, + struct sk_buff *skb) +{ + struct inet_request_sock *ireq = inet_rsk(req); + + ireq->ir_nip_rmt_addr = NIPCB(skb)->srcaddr; + ireq->ir_nip_loc_addr = NIPCB(skb)->dstaddr; +} + +/* Function + * Initialize The initialization number SEQ. Calculate the initial serial number of + * the server based on part of the source address source port, part of the destination + * address, and destination port + * Parameter + * skb: Transfer control block buffer + */ +static __u32 tcp_nip_init_sequence(const struct sk_buff *skb) +{ + return secure_tcp_nip_sequence_number(NIPCB(skb)->dstaddr.nip_addr_field32, + NIPCB(skb)->srcaddr.nip_addr_field32, + tcp_hdr(skb)->dest, + tcp_hdr(skb)->source); +} + +static struct dst_entry *tcp_nip_route_req(const struct sock *sk, + struct flowi *fl, + const struct request_sock *req) +{ + struct dst_entry *dst; + struct inet_request_sock *ireq = inet_rsk(req); + struct flow_nip fln; + + fln.daddr = ireq->ir_nip_rmt_addr; + dst = nip_route_output(sock_net(sk), sk, &fln); + return dst; +} + +/* Function + * Functions used by the client transport layer to connect requests + * This parameter is used to set the source address, destination address and interface + * Parameter + * sk: Transmission control block + * uaddr:The destination address + * addr_len:Destination address Length + */ +static int tcp_nip_connect(struct sock *sk, struct sockaddr *uaddr, + int addr_len) +{ + struct sockaddr_nin *usin = (struct sockaddr_nin *)uaddr; + struct inet_sock *inet = inet_sk(sk); + struct tcp_sock *tp = tcp_sk(sk); + __be16 orig_dport; + struct nip_addr *daddr; + struct dst_entry *dst; + int err; + struct ip_options_rcu *inet_opt; + struct inet_timewait_death_row *tcp_death_row; + struct flow_nip fln; + + fln.daddr = usin->sin_addr; + + if (addr_len < sizeof(struct sockaddr_nin)) + return -EINVAL; + + if (usin->sin_family != AF_NINET) + return -EAFNOSUPPORT; + + inet_opt = rcu_dereference_protected(inet->inet_opt, + lockdep_sock_is_held(sk)); + /* Destination ADDRESS and port */ + daddr = &usin->sin_addr; + orig_dport = usin->sin_port; + + /* Find the route and obtain the source address */ + DEBUG("%s, sk->sk_bound_dev_if is %d", __func__, sk->sk_bound_dev_if); + fln.flowin_oif = sk->sk_bound_dev_if; + dst = nip_dst_lookup_flow(sock_net(sk), sk, &fln, NULL); + if (IS_ERR(dst)) { + DEBUG("%s cannot find dst\n", __func__); + err = PTR_ERR(dst); + goto failure; + } + + /* find the actual source addr for sk->sk_nip_rcv_saddr */ + if (nip_addr_eq(&sk->sk_nip_rcv_saddr, &nip_any_addr)) + sk->sk_nip_rcv_saddr = fln.saddr; + fln.saddr = sk->sk_nip_rcv_saddr; + + if (nip_addr_invalid(&fln.daddr)) { + DEBUG("%s: nip daddr invalid.", __func__); + err = -EFAULT; + goto failure; + } + + if (nip_addr_invalid(&fln.saddr)) { + DEBUG("%s: nip saddr invalid.", __func__); + err = -EFAULT; + goto failure; + } + + /* The destination address and port are set to the transport control block */ + inet->inet_dport = usin->sin_port; + sk->sk_nip_daddr = usin->sin_addr; + + inet_csk(sk)->icsk_ext_hdr_len = 0; + if (inet_opt) + inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen; + + tcp_set_state(sk, TCP_SYN_SENT); + sk_set_txhash(sk); + sk_dst_set(sk, dst); + + /* Dynamically bind local ports */ + tcp_death_row = &sock_net(sk)->ipv4.tcp_death_row; + err = ninet_hash_connect(tcp_death_row, sk); + if (err) + goto late_failure; + + /* Class if the transport control block has already been linked */ + if (tp->rx_opt.ts_recent_stamp) { + /* Reset inherited state */ + tp->rx_opt.ts_recent = 0; + tp->rx_opt.ts_recent_stamp = 0; + if (likely(!tp->repair)) + tp->write_seq = 0; + } + + if (!tp->write_seq) + tp->write_seq = + secure_tcp_nip_sequence_number(sk->sk_nip_rcv_saddr.nip_addr_field32, + sk->sk_nip_daddr.nip_addr_field32, + inet->inet_sport, + usin->sin_port); + + inet->inet_id = prandom_u32(); + + /* Call tcp_connect to send the SYN field */ + err = __tcp_nip_connect(sk); + if (err) + goto late_failure; + + return 0; + +/* failure after tcp_set_state(sk, TCP_SYN_SENT) */ +late_failure: + tcp_set_state(sk, TCP_CLOSE); +failure: + sk->sk_route_caps = 0; + inet->inet_dport = 0; + return err; +} + +static void tcp_nip_send_reset(struct sock *sk, struct sk_buff *skb) +{ + const struct tcphdr *th = tcp_hdr(skb); + u32 seq = 0, ack_seq = 0, priority = gfp_any(); + + /* Never send a reset in response to a reset. */ + if (th->rst) + return; + + DEBUG("%s: send RST!\n", __func__); + + if (th->ack) + seq = ntohl(th->ack_seq); + else + ack_seq = ntohl(th->seq) + th->syn + th->fin + skb->len - + (th->doff << 2); + + tcp_nip_actual_send_reset(sk, skb, seq, ack_seq, 0, 1, priority); +} + +/* Function + * function used by the server to send SYN+ACK segments + * Parameter + * sk: Transmission control block + * dst: routing。 + * flowi: Flow control block + * req: Request connection control block + * foc: Fast open options + * synack_type: Type of the SYN+ACK segment + */ +static int tcp_nip_send_synack(const struct sock *sk, struct dst_entry *dst, + struct flowi *fl, + struct request_sock *req, + struct tcp_fastopen_cookie *foc, + enum tcp_synack_type synack_type, + struct sk_buff *syn_skb) +{ + struct sk_buff *skb; + int err = -ENOMEM; + + skb = tcp_nip_make_synack(sk, dst, req, foc, synack_type); + if (skb) { + DEBUG("%s: TCP server create SYN+ACK skb successfully!", __func__); + rcu_read_lock(); + err = nip_send_synack(req, skb); + rcu_read_unlock(); + } + + return err; +} + +static void tcp_nip_reqsk_destructor(struct request_sock *req) +{ + kfree_skb(inet_rsk(req)->nip_pktopts); +} + +struct request_sock_ops tcp_nip_request_sock_ops __read_mostly = { + .family = AF_NINET, + .obj_size = sizeof(struct tcp_nip_request_sock), + .rtx_syn_ack = tcp_nip_rtx_synack, + .send_ack = NULL, + .destructor = tcp_nip_reqsk_destructor, + .send_reset = NULL, + .syn_ack_timeout = NULL, +}; + +static const struct tcp_request_sock_ops tcp_request_sock_newip_ops = { + .mss_clamp = TCP_BASE_MSS, +#ifdef CONFIG_TCP_MD5SIG + .req_md5_lookup = NULL, + .calc_md5_hash = NULL, +#endif + .init_req = tcp_nip_init_req, +#ifdef CONFIG_SYN_COOKIES + .cookie_init_seq = NULL, +#endif + .route_req = tcp_nip_route_req, + .init_seq = tcp_nip_init_sequence, + .send_synack = tcp_nip_send_synack, +}; + +/* Function + * The route cache saves the transport control block from the SKB + * Parameter + * sk: Transmission control block + * skb: Transfer control block buffer + * req: Request connection control block + * dst: routing + * req_unhash: Request connection control block + */ +void ninet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb) +{ + struct dst_entry *dst = skb_dst(skb); + + if (dst && dst_hold_safe(dst)) { + sk->sk_rx_dst = dst; + inet_sk(sk)->rx_dst_ifindex = skb->skb_iif; + } +} + +/* Function + * A function used by the server to process client connection requests + * Parameter + * sk: Transmission control block + * skb: Transfer control block buffer + */ +static int tcp_nip_conn_request(struct sock *sk, struct sk_buff *skb) +{ + return tcp_newip_conn_request(&tcp_nip_request_sock_ops, + &tcp_request_sock_newip_ops, sk, skb); +} + +/* Function + * Create child control blocks + * Parameter + * sk: Transmission control block + * skb: Transfer control block buffer + * req: Request connection control block + * dst: routing + * req_unhash: Request connection control block + */ +static struct sock *tcp_nip_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, + struct request_sock *req, + struct dst_entry *dst, + struct request_sock *req_unhash, + bool *own_req) +{ + struct inet_request_sock *ireq = inet_rsk(req); + bool found_dup_sk = false; + struct tcp_nip_sock *newtcpnipsk; + struct inet_sock *newinet; + struct tcp_sock *newtp; + struct sock *newsk; + struct flow_nip fln; + + if (sk_acceptq_is_full(sk)) + goto out_overflow; + + fln.daddr = ireq->ir_nip_rmt_addr; + if (!dst) { + dst = nip_route_output(sock_net(sk), sk, &fln); + if (!dst) + goto out; + } + + newsk = tcp_nip_create_openreq_child(sk, req, skb); + if (!newsk) + goto out_nonewsk; + + /* Save the received route cache */ + ninet_sk_rx_dst_set(newsk, skb); + + newtcpnipsk = (struct tcp_nip_sock *)newsk; + + newtp = tcp_sk(newsk); + newinet = inet_sk(newsk); + + newsk->sk_nip_daddr = ireq->ir_nip_rmt_addr; + newsk->sk_nip_rcv_saddr = ireq->ir_nip_loc_addr; + + newinet->inet_opt = NULL; + + inet_csk(newsk)->icsk_ext_hdr_len = 0; + + newtp->retrans_stamp = jiffies; + + /* Negotiate MSS */ + newtp->mss_cache = TCP_BASE_MSS; + newtp->nip_out_of_order_queue = NULL; + newtp->advmss = dst_metric_advmss(dst); + if (tcp_sk(sk)->rx_opt.user_mss && + tcp_sk(sk)->rx_opt.user_mss < newtp->advmss) + newtp->advmss = tcp_sk(sk)->rx_opt.user_mss; + + tcp_nip_initialize_rcv_mss(newsk); + if (__inet_inherit_port(sk, newsk) < 0) + goto put_and_exit; + /* Deleting the old sock from the ehash table and adding the new sock to the + * ehash table succeeds *own_req equals true + */ + *own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash), + &found_dup_sk); + + /* newip newsk doesn't save this dst. release it. */ + dst_release(dst); + return newsk; + +out_overflow: + __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS); +out_nonewsk: +out: + /* newip newsk doesn't save this dst. release it. */ + dst_release(dst); + tcp_listendrop(sk); + return NULL; +put_and_exit: + newinet->inet_opt = NULL; + inet_csk_prepare_forced_close(newsk); + tcp_nip_done(newsk); + goto out; +} + +static const struct inet_connection_sock_af_ops newip_specific = { + .queue_xmit = tcp_nip_queue_xmit, + .send_check = NULL, + .rebuild_header = NULL, + .sk_rx_dst_set = ninet_sk_rx_dst_set, + .conn_request = tcp_nip_conn_request, + .syn_recv_sock = tcp_nip_syn_recv_sock, + .net_header_len = 0, + .net_frag_header_len = 0, + .setsockopt = nip_setsockopt, + .getsockopt = nip_getsockopt, + .addr2sockaddr = NULL, + .sockaddr_len = sizeof(struct sockaddr_nin), + + .mtu_reduced = NULL, +}; + +#define MAX_NIP_TCP_KEEPIDLE 32767 +#define MAX_NIP_TCP_KEEPINTVL 32767 +#define MAX_NIP_TCP_KEEPCNT 255 +static int tcp_nip_keepalive_para_update(struct sock *sk, + u32 keepalive_time, + u32 keepalive_intvl, + u8 keepalive_probes) +{ + int val; + struct tcp_sock *tp = tcp_sk(sk); + + /* set keep idle (TCP_KEEPIDLE) */ + val = keepalive_time; + if (val < 1 || val > MAX_NIP_TCP_KEEPIDLE) { + pr_crit("%s keepalive_time(%u) invalid.", __func__, val); + return -EINVAL; + } + + tp->keepalive_time = val; + if (sock_flag(sk, SOCK_KEEPOPEN) && + !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) { + u32 elapsed = keepalive_time_elapsed(tp); + + if (tp->keepalive_time > elapsed) + elapsed = tp->keepalive_time - elapsed; + else + elapsed = 0; + inet_csk_reset_keepalive_timer(sk, elapsed); + } + + /* set keep intvl (TCP_KEEPINTVL) */ + val = keepalive_intvl; + if (val < 1 || val > MAX_NIP_TCP_KEEPINTVL) { + pr_crit("%s keepalive_intvl(%u) invalid.", __func__, val); + return -EINVAL; + } + tp->keepalive_intvl = val; + + /* set keep cnt (TCP_KEEPCNT) */ + val = keepalive_probes; + if (val < 1 || val > MAX_NIP_TCP_KEEPCNT) { + pr_crit("%s keepalive_probes(%u) invalid.", __func__, val); + return -EINVAL; + } + tp->keepalive_probes = val; + + /* enable keepalive (SO_KEEPALIVE) */ + if (sk->sk_prot->keepalive) { + sk->sk_prot->keepalive(sk, 1); + sock_valbool_flag(sk, SOCK_KEEPOPEN, 1); + } else { + pr_crit("%s keepalive func is null.", __func__); + } + + return 0; +} + +void tcp_nip_keepalive_enable(struct sock *sk) +{ + int ret; + struct tcp_sock *tp = tcp_sk(sk); + + if (tp->nip_keepalive_enable) + return; + + ret = tcp_nip_keepalive_para_update(sk, g_nip_keepalive_time, + g_nip_keepalive_intvl, + g_nip_keepalive_probes); + if (ret != 0) { + pr_crit("%s fail", __func__); + return; + } + + pr_crit("%s ok", __func__); + tp->nip_keepalive_enable = true; +} + +void tcp_nip_keepalive_disable(struct sock *sk) +{ + struct tcp_sock *tp = tcp_sk(sk); + + if (!tp->nip_keepalive_enable) + return; + + if (tp->idle_ka_probes_out < g_nip_idle_ka_probes_out) + return; + + /* enable keepalive (SO_KEEPALIVE) */ + if (sk->sk_prot->keepalive) + sk->sk_prot->keepalive(sk, 0); + sock_valbool_flag(sk, SOCK_KEEPOPEN, 0); + + pr_crit("%s ok, idle_ka_probes_out=%u", __func__, g_nip_idle_ka_probes_out); + tp->nip_keepalive_enable = false; +} + +/* Function + * Example Initialize sock information in TCP + * Parameter + * sk: Sock to be initialized + * Note: Currently, this function does not initialize timer, pre-queue, and congestion control, + * and does not allow fast retransmission. No function is set to adjust MSS + */ +static int tcp_nip_init_sock(struct sock *sk) +{ + struct inet_connection_sock *icsk = inet_csk(sk); + struct tcp_sock *tp = tcp_sk(sk); + + tp->out_of_order_queue = RB_ROOT; + tcp_nip_init_xmit_timers(sk); + INIT_LIST_HEAD(&tp->tsq_node); + + icsk->icsk_rto = g_nip_rto == 0 ? TCP_TIMEOUT_INIT : (unsigned int)(HZ / g_nip_rto); + icsk->icsk_rto_min = TCP_RTO_MIN; + icsk->icsk_delack_max = TCP_DELACK_MAX; + tp->mdev_us = jiffies_to_usecs(TCP_TIMEOUT_INIT); + minmax_reset(&tp->rtt_min, tcp_jiffies32, ~0U); + + tp->snd_cwnd = TCP_INIT_CWND; + tp->app_limited = ~0U; + tp->snd_ssthresh = TCP_INFINITE_SSTHRESH; + tp->snd_cwnd_clamp = ~0; + tp->mss_cache = TCP_MSS_DEFAULT; + + tp->sacked_out = 0; + tp->rcv_tstamp = 0; + tp->selective_acks[0].start_seq = 0; + tp->selective_acks[0].end_seq = 0; + tp->ack_retrans_seq = 0; + tp->ack_retrans_num = 0; + tp->nip_ssthresh = g_nip_ssthresh_default; + tp->nip_ssthresh_reset = 0; + tp->nip_keepalive_enable = false; + tp->idle_ka_probes_out = 0; + tp->nip_keepalive_timeout_scale = 0; + + tp->reordering = sock_net(sk)->ipv4.sysctl_tcp_reordering; + tp->tsoffset = 0; + sk->sk_state = TCP_CLOSE; + sk->sk_write_space = sk_stream_write_space; + sock_set_flag(sk, SOCK_USE_WRITE_QUEUE); + + icsk->icsk_sync_mss = tcp_nip_sync_mss; + + WRITE_ONCE(sk->sk_sndbuf, g_nip_sndbuf); // sock_net(sk)->ipv4.sysctl_tcp_wmem[1] + WRITE_ONCE(sk->sk_rcvbuf, g_nip_rcvbuf); // sock_net(sk)->ipv4.sysctl_tcp_rmem[1] + + local_bh_disable(); + sk_sockets_allocated_inc(sk); + local_bh_enable(); + + icsk->icsk_af_ops = &newip_specific; + + return 0; +} + +static void skb_nip_entail(struct sock *sk, struct sk_buff *skb) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); + + skb->csum = 0; + tcb->seq = tp->write_seq; + tcb->end_seq = tp->write_seq; + tcb->tcp_flags = TCPHDR_ACK; + tcb->sacked = 0; + + tcp_nip_add_write_queue_tail(sk, skb); + + sk->sk_wmem_queued += skb->truesize; + sk_mem_charge(sk, skb->truesize); +} + +static unsigned int tcp_xmit_size_goal(struct sock *sk, u32 mss_now, + int large_allowed) +{ + struct tcp_sock *tp = tcp_sk(sk); + u32 new_size_goal, size_goal; + + if (!large_allowed) + return mss_now; + + /* Note : tcp_tso_autosize() will eventually split this later */ + new_size_goal = sk->sk_gso_max_size - 1 - MAX_TCP_HEADER; + new_size_goal = tcp_bound_to_half_wnd(tp, new_size_goal); + + /* We try hard to avoid divides here */ + size_goal = tp->gso_segs * mss_now; + if (unlikely(new_size_goal < size_goal || + new_size_goal >= size_goal + mss_now)) { + tp->gso_segs = min_t(u16, new_size_goal / mss_now, + sk->sk_gso_max_segs); + size_goal = tp->gso_segs * mss_now; + } + + return max(size_goal, mss_now); +} + +int tcp_nip_send_mss(struct sock *sk, int *size_goal, int flags) +{ + int mss_now; + + mss_now = tcp_nip_current_mss(sk); + *size_goal = tcp_xmit_size_goal(sk, mss_now, !(flags & MSG_OOB)); + + DEBUG("%snip_send_mss%d", __func__, mss_now); + return mss_now; +} + +int tcp_nip_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct sk_buff *skb; + int flags, err, copied = 0; + int mss_now = 0, size_goal; + bool process_backlog = false; + long timeo; + + lock_sock(sk); + + flags = msg->msg_flags; + + timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); + + if (((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) && + !tcp_passive_fastopen(sk)) { + err = sk_stream_wait_connect(sk, &timeo); + if (err != 0) + goto do_error; + } + + /* This should be in poll */ + sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); + + copied = 0; + +restart: + mss_now = tcp_nip_send_mss(sk, &size_goal, flags); + + DEBUG("%s: tcp_nip_send_mss %d\n", __func__, mss_now); + + err = -EPIPE; + if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) + goto do_error; + + while (msg_data_left(msg)) { + int copy = 0; + int max = mss_now; + + bool first_skb; + + if (!sk_stream_memory_free(sk)) + goto wait_for_sndbuf; + + if (process_backlog && sk_flush_backlog(sk)) { + process_backlog = false; + goto restart; + } + first_skb = skb_queue_empty(&sk->sk_write_queue); + skb = sk_stream_alloc_skb(sk, mss_now, sk->sk_allocation, first_skb); + if (!skb) + goto wait_for_memory; + + skb->tstamp = 0; + process_backlog = true; + + skb_nip_entail(sk, skb); + copy = mss_now; + max = mss_now; + + /* Try to append data to the end of skb. */ + if (copy > msg_data_left(msg)) + copy = msg_data_left(msg); + + if (skb_availroom(skb) > 0) { + /* We have some space in skb head. Superb! */ + copy = min_t(int, copy, skb_availroom(skb)); + err = skb_add_data_nocache(sk, skb, &msg->msg_iter, copy); + if (err) + goto do_fault; + } else { + DEBUG("%s: msg too big! tcp cannot devide packet now\n", __func__); + goto out; + } + + if (!copied) + TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; + + tp->write_seq += copy; + TCP_SKB_CB(skb)->end_seq += copy; + tcp_skb_pcount_set(skb, 0); + copied += copy; + if (!msg_data_left(msg)) { + if (unlikely(flags & MSG_EOR)) + TCP_SKB_CB(skb)->eor = 1; + goto out; + } + + continue; + +wait_for_sndbuf: + set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); +wait_for_memory: + if (copied) + tcp_nip_push(sk, flags & ~MSG_MORE, mss_now, + TCP_NAGLE_PUSH, size_goal); + + err = sk_stream_wait_memory(sk, &timeo); + if (err != 0) + goto do_error; + + mss_now = tcp_nip_send_mss(sk, &size_goal, flags); + } + +out: + if (copied) + tcp_nip_push(sk, flags, mss_now, tp->nonagle, size_goal); + release_sock(sk); + return copied; + +do_fault: + if (!skb->len) { + tcp_unlink_write_queue(skb, sk); + sk_wmem_free_skb(sk, skb); + } + +do_error: + if (copied) + goto out; + + err = sk_stream_error(sk, flags, err); + /* make sure we wake any epoll edge trigger waiter */ + if (unlikely(skb_queue_len(&sk->sk_write_queue) == 0 && err == -EAGAIN)) + sk->sk_write_space(sk); + release_sock(sk); + return err; +} + +/* Clean up the receive buffer for full frames taken by the user, + * then send an ACK if necessary. COPIED is the number of bytes + * tcp_recvmsg has given to the user so far, it speeds up the + * calculation of whether or not we must ACK for the sake of + * a window update. + */ +void tcp_nip_cleanup_rbuf(struct sock *sk, int copied) +{ + struct tcp_sock *tp = tcp_sk(sk); + bool time_to_ack = false; + + struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); + + WARN(skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq), + "cleanup rbuf bug: copied %X seq %X rcvnxt %X\n", + tp->copied_seq, TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt); + + if (inet_csk_ack_scheduled(sk)) { + const struct inet_connection_sock *icsk = inet_csk(sk); + + if (/* Once-per-two-segments ACK was not sent */ + tp->rcv_nxt - tp->rcv_wup > (g_ack_num * 20 * icsk->icsk_ack.rcv_mss) || + /* If this read emptied read buffer, we send ACK, if + * connection is not bidirectional, user drained + * receive buffer and there was a small segment + * in queue. + */ + (copied > 0 && + ((icsk->icsk_ack.pending & ICSK_ACK_PUSHED2) || + ((icsk->icsk_ack.pending & ICSK_ACK_PUSHED) && + !inet_csk_in_pingpong_mode(sk))) && + !atomic_read(&sk->sk_rmem_alloc))) { + time_to_ack = true; + } + } + + /* We send an ACK if we can now advertise a non-zero window + * which has been raised "significantly". + * + * Even if window raised up to infinity, do not send window open ACK + * in states, where we will not receive more. It is useless. + */ + if (copied > 0 && !time_to_ack && !(sk->sk_shutdown & RCV_SHUTDOWN)) { + __u32 rcv_window_now = tcp_receive_window(tp); + + /* Optimize, __nip_tcp_select_window() is not cheap. */ + if (2 * rcv_window_now <= tp->window_clamp) { + __u32 new_window = __nip_tcp_select_window(sk); + + /* Send ACK now, if this read freed lots of space + * in our buffer. Certainly, new_window is new window. + * We can advertise it now, if it is not less than current one. + * "Lots" means "at least twice" here. + */ + if (new_window && new_window >= 2 * rcv_window_now) + time_to_ack = true; + } + } + if (time_to_ack) + tcp_nip_send_ack(sk); +} + +int tcp_nip_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, + int flags, int *addr_len) +{ + struct tcp_sock *tp = tcp_sk(sk); + int copied = 0; + u32 *seq; + unsigned long used; + int err; + int target; + long timeo; + size_t len_tmp = len; + struct sk_buff *skb, *last; + + lock_sock(sk); + + if (sk->sk_state == TCP_LISTEN) + goto out; + + timeo = sock_rcvtimeo(sk, nonblock); + + seq = &tp->copied_seq; + + target = sock_rcvlowat(sk, flags & MSG_WAITALL, len_tmp); + + do { + u32 offset; + /* Next get a buffer. */ + last = skb_peek_tail(&sk->sk_receive_queue); + skb_queue_walk(&sk->sk_receive_queue, skb) { + last = skb; + /* Now that we have two receive queues this + * shouldn't happen. + */ + if (WARN(before(*seq, TCP_SKB_CB(skb)->seq), + "TCP recvmsg seq # bug: copied %X, seq %X, rcvnxt %X, fl %X\n", + *seq, TCP_SKB_CB(skb)->seq, tp->rcv_nxt, + flags)) + break; + offset = *seq - TCP_SKB_CB(skb)->seq; + if (unlikely(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { + pr_err_once("%s: found a SYN, please report !\n", __func__); + offset--; + } + if (offset < skb->len) + goto found_ok_skb; + if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) + goto found_fin_ok; + /* If the first SKB in the current SK_receive_queue is not the SKB to + * be replicated, then MSG_PEEK should be set in flags + */ + WARN(!(flags & MSG_PEEK), + "TCP recvmsg seq # bug 2: copied %X, seq %X, rcvnxt %X, fl %X\n", + *seq, TCP_SKB_CB(skb)->seq, tp->rcv_nxt, flags); + } + + /* If the program is executed at this point, the SK_receive_queue is finished */ + /* If there is no data in the backlog, stop reading at target */ + if (copied >= target && !sk->sk_backlog.tail) + break; + + if (copied) { + if (sk->sk_err || + sk->sk_state == TCP_CLOSE || + (sk->sk_shutdown & RCV_SHUTDOWN) || + !timeo || + signal_pending(current)) + break; + } else { + if (sock_flag(sk, SOCK_DONE)) + break; + + if (sk->sk_err) { + copied = sock_error(sk); + break; + } + + if (sk->sk_shutdown & RCV_SHUTDOWN) + break; + + if (sk->sk_state == TCP_CLOSE) { + if (!sock_flag(sk, SOCK_DONE)) { + /* This occurs when user tries to read + * from never connected socket. + */ + copied = -ENOTCONN; + break; + } + break; + } + + if (!timeo) { + copied = -EAGAIN; + break; + } + + if (signal_pending(current)) { + copied = sock_intr_errno(timeo); + break; + } + } + + tcp_nip_cleanup_rbuf(sk, copied); + + if (copied >= target) { + /* Do not sleep, just process backlog. */ + release_sock(sk); + lock_sock(sk); + } else { + DEBUG("%s: no enough data receive queue, wait\n", __func__); + sk_wait_data(sk, &timeo, last); + } + continue; +found_ok_skb: + used = skb->len - offset; + if (len_tmp < used) + used = len_tmp; + DEBUG("%s: copy data into msg, len=%ld\n", __func__, used); + if (!(flags & MSG_TRUNC)) { + err = skb_copy_datagram_msg(skb, offset, msg, used); + if (err) { + DEBUG("%s: copy data failed!\n", __func__); + if (!copied) + copied = -EFAULT; + break; + } + } + *seq += used; + len_tmp -= used; + copied += used; + + if (used + offset < skb->len) + continue; + + if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) + goto found_fin_ok; + if (!(flags & MSG_PEEK)) + sk_eat_skb(sk, skb); + continue; + +found_fin_ok: + /* Process the FIN. */ + ++*seq; + if (!(flags & MSG_PEEK)) + sk_eat_skb(sk, skb); + break; + } while (len_tmp > 0); + + /* Clean up data we have read: This will do ACK frames. */ + tcp_nip_cleanup_rbuf(sk, copied); + + release_sock(sk); + return copied; + +out: + release_sock(sk); + return err; +} + +void skb_nip_ofo_queue_purge(struct sock *sk) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct sk_buff *skb; + + while ((skb = tp->nip_out_of_order_queue) != NULL) { + tp->nip_out_of_order_queue = tp->nip_out_of_order_queue->next; + kfree_skb(skb); + } +} + +void tcp_nip_destroy_sock(struct sock *sk) +{ + struct tcp_sock *tp = tcp_sk(sk); + + tcp_nip_clear_xmit_timers(sk); + + tcp_nip_write_queue_purge(sk); + + skb_nip_ofo_queue_purge(sk); + + if (inet_csk(sk)->icsk_bind_hash) + inet_put_port(sk); + + tcp_saved_syn_free(tp); + local_bh_disable(); + sk_sockets_allocated_dec(sk); + local_bh_enable(); +} + +/* Function + * The sock handler for THE LISTEN and ESTABLISHED states is called by tcp_nip_rCV + * Parameter + * skb: Packets received from the network layer + * sk: A SOCK instance needs to be processed + */ +static int tcp_nip_do_rcv(struct sock *sk, struct sk_buff *skb) +{ + DEBUG("%s: received newip tcp skb, sk_state=%d\n", __func__, sk->sk_state); + + if (sk->sk_state == TCP_ESTABLISHED) { + tcp_nip_rcv_established(sk, skb, tcp_hdr(skb), skb->len); + return 0; + } + + /* The connection is established in cookie mode to defend against SYN-flood attacks */ + if (sk->sk_state == TCP_LISTEN) + DEBUG("found TCP_LISTEN SOCK!!!\n"); + + if (tcp_nip_rcv_state_process(sk, skb)) + goto discard; + return 0; + +discard: + kfree_skb(skb); + return 0; +} + +/* Function: + * Fill the TCP header field in SKB into the TCP private control block, + * because the TCP header field in SKB is the network byte order, + * in order to facilitate later call, need to convert the host byte order + * and store in the TCP control block. + * Parameter: + * skb:Packets delivered by the network layer + * th:TCP header field in a packet + */ +static void tcp_nip_fill_cb(struct sk_buff *skb, const struct tcphdr *th) +{ + barrier(); + + TCP_SKB_CB(skb)->seq = ntohl(th->seq); + TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin + + skb->len - th->doff * TCP_NUM_4); + + TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq); + TCP_SKB_CB(skb)->tcp_flags = tcp_flag_byte(th); + TCP_SKB_CB(skb)->tcp_tw_isn = 0; + TCP_SKB_CB(skb)->sacked = 0; +} + +static bool tcp_nip_add_backlog(struct sock *sk, struct sk_buff *skb) +{ + u32 limit = READ_ONCE(sk->sk_rcvbuf) + READ_ONCE(sk->sk_sndbuf); + + /* Only socket owner can try to collapse/prune rx queues + * to reduce memory overhead, so add a little headroom here. + * Few sockets backlog are possibly concurrently non empty. + */ + limit += 64 * 1024; + + /* In case all data was pulled from skb frags (in __pskb_pull_tail()), + * we can fix skb->truesize to its real value to avoid future drops. + * This is valid because skb is not yet charged to the socket. + * It has been noticed pure SACK packets were sometimes dropped + * (if cooked by drivers without copybreak feature). + */ + skb_condense(skb); + + if (unlikely(sk_add_backlog(sk, skb, limit))) { + bh_unlock_sock(sk); + __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPBACKLOGDROP); + DEBUG("%s: insert backlog fail.\n", __func__); + return true; + } + return false; +} + +/* Function + * TCP is the gateway from the network layer to the transport layer + * and receives data packets from the network layer + * Parameter + * skb:Packets delivered by the network layer + */ +static int tcp_nip_rcv(struct sk_buff *skb) +{ + const struct tcphdr *th; + bool refcounted; + struct sock *sk; + int ret; + int dif = skb->skb_iif; + + if (skb->pkt_type != PACKET_HOST) + goto discard_it; + + if (!nip_get_tcp_input_checksum(skb)) + goto discard_it; + + th = (const struct tcphdr *)skb->data; + + if (unlikely(th->doff < sizeof(struct tcphdr) / TCP_NUM_4)) + goto bad_packet; + + sk = __ninet_lookup_skb(&tcp_hashinfo, skb, __tcp_hdrlen(th), + th->source, th->dest, dif, + &refcounted); + if (!sk) + goto no_tcp_socket; + + if (sk->sk_state == TCP_TIME_WAIT) + goto do_time_wait; + if (sk->sk_state == TCP_NEW_SYN_RECV) { + struct request_sock *req = inet_reqsk(sk); + struct sock *nsk; + + DEBUG("%s: TCP server into third shake hands! sk->sk_state:%d", + __func__, sk->sk_state); + sk = req->rsk_listener; + + sock_hold(sk); + refcounted = true; + nsk = NULL; + /* You need to create a new SOCK and enter TCP_SYN_RECV, + * which is then set to Established + */ + if (!tcp_filter(sk, skb)) { + th = (const struct tcphdr *)skb->data; + tcp_nip_fill_cb(skb, th); + nsk = tcp_nip_check_req(sk, skb, req); + } + if (!nsk || nsk == sk) { + DEBUG("%s skb info error and create newsk failure!!!", __func__); + reqsk_put(req); + goto discard_and_relse; + } + if (tcp_nip_child_process(sk, nsk, skb)) { + goto discard_and_relse; + } else { + sock_put(sk); + return 0; + } + } + + tcp_nip_fill_cb(skb, th); + + if (tcp_filter(sk, skb)) + goto discard_and_relse; + th = (const struct tcphdr *)skb->data; + skb->dev = NULL; + + if (sk->sk_state == TCP_LISTEN) { + DEBUG("%s: TCP server into first shake hands! sk->sk_state:%d", + __func__, sk->sk_state); + ret = tcp_nip_do_rcv(sk, skb); + goto put_and_return; + } + bh_lock_sock_nested(sk); + + ret = 0; + if (!sock_owned_by_user(sk)) { + ret = tcp_nip_do_rcv(sk, skb); + } else { + DEBUG("%s: sock locked by user! put packet into backlog\n", + __func__); + if (tcp_nip_add_backlog(sk, skb)) + goto discard_and_relse; + } + + bh_unlock_sock(sk); + +put_and_return: + if (refcounted) + sock_put(sk); + return ret ? -1 : 0; + +no_tcp_socket: + /* Checksum checked, send reset back */ + tcp_nip_send_reset(NULL, skb); + DEBUG("%s: cannot find related tcp sock for skb", __func__); + goto discard_it; +bad_packet: + goto discard_it; +discard_it: + DEBUG("%s: drop tcp newip skb and release it\n", __func__); + kfree_skb(skb); + return 0; + +discard_and_relse: + sk_drops_add(sk, skb); + if (refcounted) + sock_put(sk); + goto discard_it; +/* Handles the SK portion of the interrupt state */ +do_time_wait: + goto discard_it; +} + +static void tcp_nip_early_demux(struct sk_buff *skb) +{ + const struct tcphdr *th; + struct sock *sk; + + if (skb->pkt_type != PACKET_HOST) + return; + + if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct tcphdr))) + return; + + th = tcp_hdr(skb); + if (th->doff < sizeof(struct tcphdr) / 4) + return; + + sk = __ninet_lookup_established(dev_net(skb->dev), &tcp_hashinfo, + &NIPCB(skb)->srcaddr, th->source, + &NIPCB(skb)->dstaddr, ntohs(th->dest), skb->skb_iif); + if (sk) { + DEBUG("%s: find related sock in ehash.", __func__); + skb->sk = sk; + skb->destructor = sock_edemux; + if (sk_fullsock(sk)) { + struct dst_entry *dst = READ_ONCE(sk->sk_rx_dst); + + if (dst && + inet_sk(sk)->rx_dst_ifindex == skb->skb_iif) { + DEBUG("%s: set dst for skb.", __func__); + skb_dst_set_noref(skb, dst); + } + } + } +} + +void tcp_nip_done(struct sock *sk) +{ + struct request_sock *req = tcp_sk(sk)->fastopen_rsk; + + if (sk->sk_state == TCP_SYN_SENT || sk->sk_state == TCP_SYN_RECV) + TCP_INC_STATS(sock_net(sk), TCP_MIB_ATTEMPTFAILS); + + tcp_set_state(sk, TCP_CLOSE); + inet_csk_clear_xmit_timers(sk); + if (req) + reqsk_fastopen_remove(sk, req, false); + + sk->sk_shutdown = SHUTDOWN_MASK; + + if (!sock_flag(sk, SOCK_DEAD)) { + sk->sk_state_change(sk); + } else { + WARN_ON(sk->sk_state != TCP_CLOSE); + WARN_ON(!sock_flag(sk, SOCK_DEAD)); + + /* It cannot be in hash table! */ + WARN_ON(!sk_unhashed(sk)); + + /* If it has not 0 inet_sk(sk)->inet_num, it must be bound */ + WARN_ON(inet_sk(sk)->inet_num && !inet_csk(sk)->icsk_bind_hash); + sk->sk_prot->destroy(sk); + + sk_nip_stream_kill_queues(sk); + + local_bh_disable(); + this_cpu_dec(*sk->sk_prot->orphan_count); + local_bh_enable(); + sock_put(sk); + DEBUG("%s: close sock done!!\n", __func__); + } +} + +/* Function + * Disconnect the connection to the peer end, non-blocking + * Release read/write queue, send RST (not sent yet), clear timer + * Parameter + * sk: Transmission control block + */ +int tcp_nip_disconnect(struct sock *sk, int flags) +{ + struct inet_sock *inet = inet_sk(sk); + struct inet_connection_sock *icsk = inet_csk(sk); + struct tcp_sock *tp = tcp_sk(sk); + int err = 0; + int old_state = sk->sk_state; + + if (old_state != TCP_CLOSE) + tcp_set_state(sk, TCP_CLOSE); + + if (old_state == TCP_LISTEN) { + inet_csk_listen_stop(sk); + } else if (tcp_nip_need_reset(old_state) || + (tp->snd_nxt != tp->write_seq && + (1 << old_state) & (TCPF_CLOSING | TCPF_LAST_ACK))) { + tcp_nip_send_active_reset(sk, gfp_any()); + sk->sk_err = ECONNRESET; + } else if (old_state == TCP_SYN_SENT) { + sk->sk_err = ECONNRESET; + } + + tcp_nip_clear_xmit_timers(sk); + __skb_queue_purge(&sk->sk_receive_queue); + tcp_write_queue_purge(sk); + + inet->inet_dport = 0; + sk->sk_shutdown = 0; + sock_reset_flag(sk, SOCK_DONE); + tp->srtt_us = 0; + tp->write_seq += tp->max_window + TCP_NUM_2; + if (tp->write_seq == 0) + tp->write_seq = 1; + tp->snd_cwnd = TCP_NUM_2; + icsk->icsk_probes_out = 0; + tp->packets_out = 0; + tp->snd_ssthresh = TCP_INFINITE_SSTHRESH; + tp->snd_cwnd_cnt = 0; + tp->window_clamp = 0; + tp->delivered = 0; + tcp_clear_retrans(tp); + tp->total_retrans = 0; + inet_csk_delack_init(sk); + + icsk->icsk_ack.rcv_mss = TCP_MIN_MSS; + sk->sk_send_head = NULL; + memset(&tp->rx_opt, 0, sizeof(tp->rx_opt)); + __sk_dst_reset(sk); + dst_release(sk->sk_rx_dst); + sk->sk_rx_dst = NULL; + tp->segs_in = 0; + tp->segs_out = 0; + tp->bytes_acked = 0; + tp->bytes_received = 0; + tp->data_segs_in = 0; + tp->data_segs_out = 0; + + WARN_ON(inet->inet_num && !icsk->icsk_bind_hash); + + if (sk->sk_frag.page) { + put_page(sk->sk_frag.page); + sk->sk_frag.page = NULL; + sk->sk_frag.offset = 0; + } + + sk->sk_error_report(sk); + return err; +} + +struct proto tcp_nip_prot = { + .name = "NIP_TCP", + .owner = THIS_MODULE, + .close = tcp_nip_close, + .connect = tcp_nip_connect, + .disconnect = tcp_nip_disconnect, + .accept = inet_csk_accept, + .ioctl = tcp_ioctl, + .init = tcp_nip_init_sock, + .destroy = tcp_nip_destroy_sock, + .shutdown = tcp_nip_shutdown, + .setsockopt = tcp_setsockopt, + .getsockopt = tcp_getsockopt, + .keepalive = tcp_set_keepalive, + .recvmsg = tcp_nip_recvmsg, + .sendmsg = tcp_nip_sendmsg, + .sendpage = NULL, + .backlog_rcv = tcp_nip_do_rcv, + .release_cb = tcp_nip_release_cb, + .hash = ninet_hash, + .unhash = ninet_unhash, + .get_port = inet_csk_get_port, + .sockets_allocated = &tcp_sockets_allocated, + .orphan_count = &tcp_orphan_count, + .memory_allocated = &tcp_memory_allocated, + .memory_pressure = &tcp_memory_pressure, + .sysctl_mem = sysctl_tcp_mem, + .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), + .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), + .max_header = MAX_TCP_HEADER, + .obj_size = sizeof(struct tcp_nip_sock), + .rsk_prot = &tcp_nip_request_sock_ops, + .h.hashinfo = &tcp_hashinfo, + .no_autobind = true, +}; + +static const struct ninet_protocol tcp_nip_protocol = { + .early_demux = tcp_nip_early_demux, + .handler = tcp_nip_rcv, + .flags = 0, +}; + +static struct inet_protosw tcp_nip_protosw = { + .type = SOCK_STREAM, + .protocol = IPPROTO_TCP, + .prot = &tcp_nip_prot, + .ops = &ninet_stream_ops, + .flags = INET_PROTOSW_PERMANENT | + INET_PROTOSW_ICSK, +}; + +int __init tcp_nip_init(void) +{ + int ret; + + ret = ninet_add_protocol(&tcp_nip_protocol, IPPROTO_TCP); + if (ret) + goto out; + + /* register ninet protocol */ + ret = ninet_register_protosw(&tcp_nip_protosw); + if (ret) + goto out_nip_tcp_protocol; + +out: + return ret; + +out_nip_tcp_protocol: + ninet_del_protocol(&tcp_nip_protocol, IPPROTO_TCP); + goto out; +} + +void tcp_nip_exit(void) +{ + ninet_unregister_protosw(&tcp_nip_protosw); + ninet_del_protocol(&tcp_nip_protocol, IPPROTO_TCP); +} + diff --git a/code/net/newip/tcp_nip_input.c b/code/net/newip/tcp_nip_input.c new file mode 100644 index 0000000..d37d1d6 --- /dev/null +++ b/code/net/newip/tcp_nip_input.c @@ -0,0 +1,1694 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * + * NewIP INET + * An implementation of the TCP/IP protocol suite for the LINUX + * operating system. NewIP INET is implemented using the BSD Socket + * interface as the means of communication with the user level. + * + * Implementation of the Transmission Control Protocol(TCP). + * + * Based on net/ipv4/tcp_input.c + * Based on net/ipv4/tcp_output.c + * Based on net/ipv4/tcp_minisocks.c + */ +#define pr_fmt(fmt) "NIP-TCP: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include "tcp_nip_parameter.h" + +#define FLAG_DATA 0x01 /* Incoming frame contained data. */ +#define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */ +#define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */ +#define FLAG_RETRANS_DATA_ACKED 0x08 /* some of which was retransmitted. */ +#define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */ +#define FLAG_DATA_SACKED 0x20 /* New SACK. */ +#define FLAG_ECE 0x40 /* ECE in this ACK */ +#define FLAG_LOST_RETRANS 0x80 /* This ACK marks some retransmission lost */ +#define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/ +#define FLAG_ORIG_SACK_ACKED 0x200 /* Never retransmitted data are (s)acked */ +#define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */ +#define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */ +#define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */ +#define FLAG_UPDATE_TS_RECENT 0x4000 /* tcp_replace_ts_recent() */ +#define FLAG_NO_CHALLENGE_ACK 0x8000 /* do not call tcp_send_challenge_ack() */ + +#define FLAG_ACKED (FLAG_DATA_ACKED | FLAG_SYN_ACKED) +#define FLAG_NOT_DUP (FLAG_DATA | FLAG_WIN_UPDATE | FLAG_ACKED) +#define FLAG_CA_ALERT (FLAG_DATA_SACKED | FLAG_ECE) +#define FLAG_FORWARD_PROGRESS (FLAG_ACKED | FLAG_DATA_SACKED) + +#define TCP_REMNANT (TCP_FLAG_FIN | TCP_FLAG_URG | TCP_FLAG_SYN | TCP_FLAG_PSH) +#define TCP_HP_BITS (~(TCP_RESERVED_BITS | TCP_FLAG_PSH)) + +#define REXMIT_NONE 0 /* no loss recovery to do */ +#define REXMIT_LOST 1 /* retransmit packets marked lost */ +#define REXMIT_NEW 2 /* FRTO-style transmit of unsent/new packets */ + +#define TCP_MAX_MSS 1460 + +void tcp_nip_fin(struct sock *sk) +{ + inet_csk_schedule_ack(sk); + + sk->sk_shutdown |= RCV_SHUTDOWN; + sock_set_flag(sk, SOCK_DONE); + + switch (sk->sk_state) { + case TCP_SYN_RECV: + case TCP_ESTABLISHED: + /* Move to CLOSE_WAIT */ + tcp_set_state(sk, TCP_CLOSE_WAIT); + inet_csk(sk)->icsk_ack.pingpong = 1; + break; + + case TCP_CLOSE_WAIT: + case TCP_CLOSING: + /* Received a retransmission of the FIN, do + * nothing. + */ + break; + case TCP_LAST_ACK: + /* RFC793: Remain in the LAST-ACK state. */ + break; + + case TCP_FIN_WAIT1: + /* This case occurs when a simultaneous close + * happens, we must ack the received FIN and + * enter the CLOSING state. + */ + tcp_nip_send_ack(sk); + tcp_set_state(sk, TCP_CLOSING); + break; + case TCP_FIN_WAIT2: + /* Received a FIN -- send ACK and enter TIME_WAIT. */ + tcp_nip_send_ack(sk); + inet_csk_reset_keepalive_timer(sk, TCP_TIMEWAIT_LEN); + break; + default: + /* Only TCP_LISTEN and TCP_CLOSE are left, in these + * cases we should never reach this piece of code. + */ + pr_err("%s: Impossible, sk->sk_state=%d\n", + __func__, sk->sk_state); + break; + } + + if (!sock_flag(sk, SOCK_DEAD)) + sk->sk_state_change(sk); +} + +static void tcp_nip_overlap_handle(struct tcp_sock *tp, struct sk_buff *skb) +{ + u32 diff = tp->rcv_nxt - TCP_SKB_CB(skb)->seq; + struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); + + skb->data += diff; + skb->len -= diff; + tcb->seq += diff; +} + +static void tcp_nip_ofo_queue(struct sock *sk) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct sk_buff *skb; + + while (tp->nip_out_of_order_queue) { + skb = tp->nip_out_of_order_queue; + if (after(TCP_SKB_CB(tp->nip_out_of_order_queue)->seq, tp->rcv_nxt)) + return; + tp->nip_out_of_order_queue = tp->nip_out_of_order_queue->next; + skb->next = NULL; + if (tp->rcv_nxt != TCP_SKB_CB(skb)->seq) + tcp_nip_overlap_handle(tp, skb); + + __skb_queue_tail(&sk->sk_receive_queue, skb); + tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; + + while (tp->nip_out_of_order_queue && + before(TCP_SKB_CB(tp->nip_out_of_order_queue)->end_seq, tp->rcv_nxt)) { + struct sk_buff *tmp_skb = tp->nip_out_of_order_queue; + + tp->nip_out_of_order_queue = tp->nip_out_of_order_queue->next; + tmp_skb->next = NULL; + __kfree_skb(tmp_skb); + } + } +} + + /* Maintain a sort list order by the seq. */ +static void tcp_nip_data_queue_ofo(struct sock *sk, struct sk_buff *skb) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct sk_buff *pre_skb, *cur_skb; + + inet_csk_schedule_ack(sk); + skb->next = NULL; + if (!tp->nip_out_of_order_queue) { + tp->nip_out_of_order_queue = skb; + skb_set_owner_r(skb, sk); + return; + } + pre_skb = tp->nip_out_of_order_queue; + cur_skb = pre_skb->next; + if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(pre_skb)->seq) { + if (TCP_SKB_CB(skb)->end_seq > TCP_SKB_CB(pre_skb)->end_seq) { + skb->next = pre_skb->next; + pre_skb->next = NULL; + skb_set_owner_r(skb, sk); + __kfree_skb(pre_skb); + return; + } + __kfree_skb(skb); + return; + } else if (TCP_SKB_CB(skb)->seq < TCP_SKB_CB(pre_skb)->seq) { + tp->nip_out_of_order_queue = skb; + skb->next = pre_skb; + skb_set_owner_r(skb, sk); + return; + } + while (cur_skb) { + if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(cur_skb)->seq) { + /* Same seq, if skb end_seq is bigger, replace. */ + if (TCP_SKB_CB(skb)->end_seq > TCP_SKB_CB(cur_skb)->end_seq) { + pre_skb->next = skb; + skb->next = cur_skb->next; + cur_skb->next = NULL; + skb_set_owner_r(skb, sk); + __kfree_skb(cur_skb); + } else { + __kfree_skb(skb); + } + return; + } else if (TCP_SKB_CB(skb)->seq < TCP_SKB_CB(cur_skb)->seq) { + pre_skb->next = skb; + skb->next = cur_skb; + skb_set_owner_r(skb, sk); + return; + } + pre_skb = pre_skb->next; + cur_skb = cur_skb->next; + } + pre_skb->next = skb; + skb_set_owner_r(skb, sk); +} + +static void tcp_drop(struct sock *sk, struct sk_buff *skb) +{ + sk_drops_add(sk, skb); + __kfree_skb(skb); +} + +static void tcp_nip_data_queue(struct sock *sk, struct sk_buff *skb) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct inet_connection_sock *icsk = inet_csk(sk); + + if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) { + DEBUG("%s: no data, only handle ack.\n", __func__); + __kfree_skb(skb); + return; + } + + if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) { + if (tcp_receive_window(tp) == 0) + goto out_of_window; + } + + if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_wup + tp->rcv_wnd)) { + DEBUG("seq is %u and %u\n", TCP_SKB_CB(skb)->seq, tp->rcv_nxt); + __kfree_skb(skb); + return; + } + + if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) { +out_of_window: + inet_csk_schedule_ack(sk); + __kfree_skb(skb); + return; + } + icsk->icsk_ack.lrcvtime = tcp_jiffies32; + __skb_pull(skb, tcp_hdr(skb)->doff * TCP_NUM_4); + + if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt || + (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt) && + after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))) { + if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf) { + sk->sk_data_ready(sk); + tcp_drop(sk, skb); + return; + } + + if (TCP_SKB_CB(skb)->seq != tp->rcv_nxt) + tcp_nip_overlap_handle(tp, skb); + + DEBUG("%s: tcp newip packet received. data len:%d\n", __func__, skb->len); + + __skb_queue_tail(&sk->sk_receive_queue, skb); + skb_set_owner_r(skb, sk); + tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; + inet_csk_schedule_ack(sk); + if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) + tcp_nip_fin(sk); + if (tp->nip_out_of_order_queue) + tcp_nip_ofo_queue(sk); + if (!sock_flag(sk, SOCK_DEAD)) + sk->sk_data_ready(sk); + return; + } + tcp_nip_data_queue_ofo(sk, skb); +} + +static inline void tcp_nip_push_pending_frames(struct sock *sk) +{ + if (tcp_nip_send_head(sk)) { + struct tcp_sock *tp = tcp_sk(sk); + u32 cur_mss = tcp_nip_current_mss(sk); // TCP_BASE_MSS + + __tcp_nip_push_pending_frames(sk, cur_mss, tp->nonagle); + } +} + +static void tcp_nip_new_space(struct sock *sk) +{ + sk->sk_write_space(sk); +} + +static void tcp_nip_check_space(struct sock *sk) +{ + /* Invoke memory barrier (annotated prior to checkpatch requirements) */ + smp_mb(); + if (sk->sk_socket && + test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) + tcp_nip_new_space(sk); +} + +static inline void tcp_nip_data_snd_check(struct sock *sk) +{ + tcp_nip_push_pending_frames(sk); + tcp_nip_check_space(sk); +} + +#define TCP_NIP_DELACK_MIN (HZ / 50) +void tcp_nip_send_delayed_ack(struct sock *sk) +{ + struct inet_connection_sock *icsk = inet_csk(sk); + int ato = TCP_NIP_DELACK_MIN; // rtt + unsigned long timeout; + + icsk->icsk_ack.ato = TCP_DELACK_MIN; + + /* Stay within the limit we were given */ + timeout = jiffies + ato; + + /* Use new timeout only if there wasn't a older one earlier. */ + if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) { + if (time_before_eq(icsk->icsk_ack.timeout, + jiffies + (ato >> TCP_NIP_4BYTE_PAYLOAD))) { + tcp_nip_send_ack(sk); + return; + } + + if (!time_before(timeout, icsk->icsk_ack.timeout)) + timeout = icsk->icsk_ack.timeout; + } + icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; + icsk->icsk_ack.timeout = timeout; + sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout); +} + +static void __tcp_nip_ack_snd_check(struct sock *sk, int ofo_possible) +{ + struct tcp_sock *tp = tcp_sk(sk); + + inet_csk(sk)->icsk_ack.rcv_mss = tcp_nip_current_mss(sk); // TCP_BASE_MSS + + /* More than n full frame received... */ + if (((tp->rcv_nxt - tp->rcv_wup) > g_ack_num * inet_csk(sk)->icsk_ack.rcv_mss && + __nip_tcp_select_window(sk) >= tp->rcv_wnd) || + /* We have out of order data. */ + (ofo_possible && tp->nip_out_of_order_queue)) { + tcp_nip_send_ack(sk); + } else { + /* Else, send delayed ack. */ + DEBUG("%s: send delayed ack!!", __func__); + tcp_nip_send_delayed_ack(sk); + } +} + +static inline void tcp_nip_ack_snd_check(struct sock *sk) +{ + if (!inet_csk_ack_scheduled(sk)) { + /* We sent a data segment already. */ + DEBUG("We sent a data segment already.!!\n"); + return; + } + __tcp_nip_ack_snd_check(sk, 1); +} + +static void tcp_nip_snd_una_update(struct tcp_sock *tp, u32 ack) +{ + u32 delta = ack - tp->snd_una; + + sock_owned_by_me((struct sock *)tp); + tp->bytes_acked += delta; + tp->snd_una = ack; +} + +void tcp_nip_rearm_rto(struct sock *sk) +{ + struct tcp_sock *tp = tcp_sk(sk); + + if (!tp->packets_out) { + inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS); + } else { + u32 rto = inet_csk(sk)->icsk_rto; + + inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto, + TCP_RTO_MAX); + } +} + +static int tcp_nip_clean_rtx_queue(struct sock *sk, ktime_t *skb_snd_tstamp) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct sk_buff *skb; + int flag = 0; + struct inet_connection_sock *icsk = inet_csk(sk); + + while ((skb = tcp_write_queue_head(sk)) && skb != tcp_nip_send_head(sk)) { + struct tcp_skb_cb *scb = TCP_SKB_CB(skb); + u32 acked_pcount; + + if (after(scb->end_seq, tp->snd_una)) { + if (tcp_skb_pcount(skb) == 1 || !after(tp->snd_una, scb->seq)) + break; + DEBUG("%s: ack error!\n", __func__); + } else { + prefetchw(skb->next); + acked_pcount = tcp_skb_pcount(skb); + } + + if (likely(!(scb->tcp_flags & TCPHDR_SYN))) { + flag |= FLAG_DATA_ACKED; + } else { + flag |= FLAG_SYN_ACKED; + tp->retrans_stamp = 0; + } + + tp->packets_out -= acked_pcount; + + if (*skb_snd_tstamp == 0) + *skb_snd_tstamp = skb->tstamp; + + tcp_unlink_write_queue(skb, sk); + sk_wmem_free_skb(sk, skb); + } + + if ((*skb_snd_tstamp != 0) && (tp->rcv_tstamp - *skb_snd_tstamp) >= g_rtt_tstamp_rto_up) + icsk->icsk_rto = (unsigned int)(HZ / g_nip_rto_up); + else + icsk->icsk_rto = (unsigned int)(HZ / g_nip_rto); + + if (flag & FLAG_ACKED) + tcp_nip_rearm_rto(sk); + return 0; +} + +/* Function + * Allocate a connection request block that holds connection request information. + * At the same time, initialize the set of operations used to send ACK/RST segments + * during connection, so that these interfaces can be easily called during establishment. + * Set the socket state to TCP_NEW_SYN_RECV + * Parameter + * ops: Request the functional interface of the control block + * sk_listener: Transmission control block + * attach_listener: Whether to set cookies + */ +struct request_sock *ninet_reqsk_alloc(const struct request_sock_ops *ops, + struct sock *sk_listener, + bool attach_listener) +{ + struct request_sock *req = reqsk_alloc(ops, sk_listener, + attach_listener); + + if (req) { + struct inet_request_sock *ireq = inet_rsk(req); + + ireq->ireq_opt = NULL; + ireq->nip_pktopts = NULL; + atomic64_set(&ireq->ir_cookie, 0); + ireq->ireq_state = TCP_NEW_SYN_RECV; + write_pnet(&ireq->ireq_net, sock_net(sk_listener)); + ireq->ireq_family = sk_listener->sk_family; + } + + return req; +} + +static void tcp_nip_drop(struct sock *sk, struct sk_buff *skb) +{ + sk_drops_add(sk, skb); + __kfree_skb(skb); +} + +void tcp_nip_parse_mss(struct tcp_options_received *opt_rx, + const struct tcphdr *th, + const unsigned char *ptr, + int opsize, + int estab) +{ + if (opsize == TCPOLEN_MSS && th->syn && !estab) { + u16 in_mss = get_unaligned_be16(ptr); + + DEBUG("%s: in_mss %d\n", __func__, in_mss); + + if (in_mss) { + if (opt_rx->user_mss && + opt_rx->user_mss < in_mss) + in_mss = opt_rx->user_mss; + opt_rx->mss_clamp = in_mss; + } + } +} + +/* Function + * Look for tcp options. Normally only called on SYN and SYNACK packets. + * Parsing of TCP options in SKB + * Parameter + * skb: Transfer control block buffer + * opt_rx: Saves the structure for TCP options + * estab: WANTCOOKIE + * foc: Len field + */ +void tcp_nip_parse_options(const struct sk_buff *skb, + struct tcp_options_received *opt_rx, int estab, + struct tcp_fastopen_cookie *foc) +{ + const unsigned char *ptr; + const struct tcphdr *th = tcp_hdr(skb); + /* The length of the TCP option = Length of TCP header - The length of the TCP structure */ + int length = (th->doff * 4) - sizeof(struct tcphdr); + + /* A pointer to the option position */ + ptr = (const unsigned char *)(th + 1); + opt_rx->saw_tstamp = 0; + + while (length > 0) { + int opcode = *ptr++; + int opsize; + + switch (opcode) { + case TCPOPT_EOL: + return; + case TCPOPT_NOP: + length--; + continue; + default: + opsize = *ptr++; + if (opsize < 2) /* "2 - silly options" */ + return; + if (opsize > length) + return; /* don't parse partial options */ + switch (opcode) { + case TCPOPT_MSS: + tcp_nip_parse_mss(opt_rx, th, ptr, opsize, estab); + break; + default: + break; + } + ptr += opsize - TCP_NUM_2; + length -= opsize; + } + } +} + +/* Function + * Initializes the connection request block information based + * on the options and sequence number in the received SYN segment + * Parameter + * req: Request connection control block + * rx_opt: Saves the structure for TCP options + * skb: Transfer control block buffer. + * sk: transmission control block. + */ +static void tcp_nip_openreq_init(struct request_sock *req, + const struct tcp_options_received *rx_opt, + struct sk_buff *skb, const struct sock *sk) +{ + struct inet_request_sock *ireq = inet_rsk(req); + + req->rsk_rcv_wnd = 0; + tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq; + tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1; + tcp_rsk(req)->snt_synack = tcp_clock_us(); + tcp_rsk(req)->last_oow_ack_time = 0; + req->mss = rx_opt->mss_clamp; + req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0; + ireq->tstamp_ok = rx_opt->tstamp_ok; + ireq->snd_wscale = rx_opt->snd_wscale; + + if (g_wscale_enable == 1) { + ireq->wscale_ok = 1; + ireq->snd_wscale = g_wscale; // rx_opt->snd_wscale; + ireq->rcv_wscale = g_wscale; + } + + ireq->acked = 0; + ireq->ecn_ok = 0; + ireq->ir_rmt_port = tcp_hdr(skb)->source; + ireq->ir_num = ntohs(tcp_hdr(skb)->dest); + ireq->ir_mark = sk->sk_mark; +} + +/* Function + * Based on listening SOCK and REQ, create a transport control block + * for the new connection and initialize it. + * Parameter + * sk: the listening transmission control block. + * req: Request connection control block + * skb: Transfer control block buffer. + */ +struct sock *tcp_nip_create_openreq_child(const struct sock *sk, + struct request_sock *req, + struct sk_buff *skb) +{ + /* Clone a transport control block and lock the new transport control block */ + struct sock *newsk = inet_csk_clone_lock(sk, req, GFP_ATOMIC); + + if (newsk) { + const struct inet_request_sock *ireq = inet_rsk(req); + struct tcp_request_sock *treq = tcp_rsk(req); + struct inet_connection_sock *newicsk = inet_csk(newsk); + struct tcp_sock *newtp = tcp_sk(newsk); + + /* Now setup tcp_sock */ + newtp->pred_flags = 0; + + /* The variables related to the receiving and sending serial numbers + * are initialized. The second handshake sends an ACK in the SYN+ACK segment + */ + newtp->rcv_wup = treq->rcv_isn + 1; + newtp->copied_seq = treq->rcv_isn + 1; + newtp->rcv_nxt = treq->rcv_isn + 1; + newtp->segs_in = 1; + /* The second handshake sends seq+1 in the SYN+ACK segment */ + newtp->snd_sml = treq->snt_isn + 1; + newtp->snd_una = treq->snt_isn + 1; + newtp->snd_nxt = treq->snt_isn + 1; + newtp->snd_up = treq->snt_isn + 1; + + INIT_LIST_HEAD(&newtp->tsq_node); + + /* The ACK segment number of the send window that + * received the first handshake update + */ + tcp_init_wl(newtp, treq->rcv_isn); + + /* Initialization of delay-related variables */ + minmax_reset(&newtp->rtt_min, tcp_jiffies32, ~0U); + newicsk->icsk_rto = g_nip_rto == 0 ? TCP_TIMEOUT_INIT : (HZ / g_nip_rto); + newicsk->icsk_ack.lrcvtime = tcp_jiffies32; + + /* The congestion control-related variables are initialized */ + newtp->packets_out = 0; + + newtp->snd_ssthresh = TCP_INFINITE_SSTHRESH; + + newtp->lsndtime = tcp_jiffies32; + + newtp->total_retrans = req->num_retrans; + + newtp->snd_cwnd = TCP_INIT_CWND; + + /* There's a bubble in the pipe until at least the first ACK. */ + newtp->app_limited = ~0U; + + /* Initialize several timers */ + tcp_nip_init_xmit_timers(newsk); + newtp->write_seq = treq->snt_isn + 1; + newtp->pushed_seq = treq->snt_isn + 1; + + /* TCP option correlation */ + newtp->rx_opt.saw_tstamp = 0; + + newtp->rx_opt.dsack = 0; + newtp->rx_opt.num_sacks = 0; + + newtp->urg_data = 0; + + newtp->rx_opt.tstamp_ok = ireq->tstamp_ok; + newtp->window_clamp = req->rsk_window_clamp; + newtp->rcv_ssthresh = req->rsk_rcv_wnd; + newtp->rcv_wnd = req->rsk_rcv_wnd; + newtp->rx_opt.wscale_ok = ireq->wscale_ok; + if (newtp->rx_opt.wscale_ok) { + newtp->rx_opt.snd_wscale = ireq->snd_wscale; + newtp->rx_opt.rcv_wscale = ireq->rcv_wscale; + } else { + newtp->rx_opt.snd_wscale = 0; + newtp->rx_opt.rcv_wscale = 0; + newtp->window_clamp = min(newtp->window_clamp, 65535U); + } + newtp->snd_wnd = (ntohs(tcp_hdr(skb)->window) << + newtp->rx_opt.snd_wscale); + newtp->max_window = newtp->snd_wnd; + + if (newtp->rx_opt.tstamp_ok) { + newtp->rx_opt.ts_recent = req->ts_recent; + newtp->rx_opt.ts_recent_stamp = get_seconds(); + newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED; + } else { + newtp->rx_opt.ts_recent_stamp = 0; + newtp->tcp_header_len = sizeof(struct tcphdr); + } + newtp->tsoffset = 0; + + /* Determines the size of the last passed segment */ + if (skb->len >= TCP_MSS_DEFAULT + newtp->tcp_header_len) + newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len; + newtp->rx_opt.mss_clamp = req->mss; + newtp->fastopen_req = NULL; + newtp->fastopen_rsk = NULL; + newtp->syn_data_acked = 0; + newtp->rack.mstamp = 0; + newtp->rack.advanced = 0; + + __TCP_INC_STATS(sock_net(sk), TCP_MIB_PASSIVEOPENS); + } + return newsk; +} + +void tcp_nip_openreq_init_rwin(struct request_sock *req, + const struct sock *sk_listener, + const struct dst_entry *dst) +{ + struct inet_request_sock *ireq = inet_rsk(req); + const struct tcp_sock *tp = tcp_sk(sk_listener); + int full_space = tcp_full_space(sk_listener); + int mss; + u32 window_clamp; + __u8 rcv_wscale; + int sysctl_tcp_nip_window_scaling = 0; + + mss = tcp_mss_clamp(tp, dst_metric_advmss(dst)); + + window_clamp = READ_ONCE(tp->window_clamp); + /* Set this up on the first call only */ + req->rsk_window_clamp = window_clamp ? : dst_metric(dst, RTAX_WINDOW); + + /* limit the window selection if the user enforce a smaller rx buffer */ + if (sk_listener->sk_userlocks & SOCK_RCVBUF_LOCK && + (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0)) + req->rsk_window_clamp = full_space; + + /* tcp_full_space because it is guaranteed to be the first packet */ + tcp_select_initial_window(sk_listener, full_space, + mss - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0), + &req->rsk_rcv_wnd, + &req->rsk_window_clamp, + sysctl_tcp_nip_window_scaling, + &rcv_wscale, + 0); + ireq->rcv_wscale = g_wscale_enable == 1 ? g_wscale : rcv_wscale; +} + +/* Function + * A function used by the server to process client connection requests. + * Parameter + * rsk_ops: Functional interface to request control blocks. + * af_ops: The functional interface of the TCP request block. + * sk: transmission control block. + * skb: Transfer control block buffer. + */ +int tcp_newip_conn_request(struct request_sock_ops *rsk_ops, + const struct tcp_request_sock_ops *af_ops, + struct sock *sk, struct sk_buff *skb) +{ + struct tcp_fastopen_cookie foc = { .len = -1 }; + + __u32 isn = TCP_SKB_CB(skb)->tcp_tw_isn; + /* All received TCP options are resolved into this structure */ + struct tcp_options_received tmp_opt; + struct tcp_sock *tp = tcp_sk(sk); + struct dst_entry *dst = NULL; + struct request_sock *req; + + /* If the half-connection queue length has reached the upper limit, + * the current request is discarded + */ + if (inet_csk_reqsk_queue_is_full(sk) && !isn) { + DEBUG("inet_csk_reqsk_queue_is_full!!!!!\n"); + goto drop; + } + + /* If the queue holds the socket that has completed the connection (full connection queue) + * The length has reached its upper limit + * The current request is discarded + */ + if (sk_acceptq_is_full(sk)) { + NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS); + DEBUG("sk_acceptq_is_full!!!!!\n"); + goto drop; + } + + /* Allocate a connection request block that holds connection request information + * While initializing the connection process + * The set of operations that send ACK/RST segments + * These interfaces can be easily invoked during the setup process. + */ + req = ninet_reqsk_alloc(rsk_ops, sk, true); + if (!req) + goto drop; + + tcp_rsk(req)->af_specific = af_ops; + + tcp_clear_options(&tmp_opt); + /* Maximum MSS negotiated during connection establishment */ + tmp_opt.mss_clamp = af_ops->mss_clamp; + /* The best way to do this is to prink the value of user_mss and see if it is 0 */ + tmp_opt.user_mss = tp->rx_opt.user_mss; + /* Parsing of TCP options in SKB */ + tcp_nip_parse_options(skb, &tmp_opt, 0, false); + + /* Tstamp_ok indicates the TIMESTAMP seen on the received SYN packet */ + tmp_opt.tstamp_ok = tmp_opt.saw_tstamp; + /* Initializes the connection request block information based on the options + * and sequence number in the received SYN segment + */ + tcp_nip_openreq_init(req, &tmp_opt, skb, sk); + + inet_rsk(req)->ir_iif = sk->sk_bound_dev_if; + + af_ops->init_req(req, sk, skb); + + if (!isn) + isn = af_ops->init_seq(skb); + + if (!dst) { + dst = af_ops->route_req(sk, NULL, req); + if (!dst) + goto drop_and_free; + } + + tcp_rsk(req)->snt_isn = isn; + tcp_rsk(req)->txhash = net_tx_rndhash(); + /* Initialize the receive window */ + tcp_nip_openreq_init_rwin(req, sk, dst); + /* Record the syn */ + tcp_rsk(req)->tfo_listener = false; + /* Add a timer to add reQ to the ehash table */ + ninet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT); + + af_ops->send_synack(sk, dst, NULL, req, &foc, TCP_SYNACK_NORMAL, NULL); + + reqsk_put(req); + return 0; + +drop_and_free: + reqsk_free(req); +drop: + tcp_listendrop(sk); + return 0; +} + +static inline bool tcp_nip_paws_check(const struct tcp_options_received *rx_opt, + int paws_win) +{ + if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win) + return true; + if (unlikely(get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)) + return true; + + if (!rx_opt->ts_recent) + return true; + return false; +} + +static inline bool tcp_nip_may_update_window(const struct tcp_sock *tp, + const u32 ack, const u32 ack_seq, + const u32 nwin) +{ + return after(ack, tp->snd_una) || + after(ack_seq, tp->snd_wl1) || + (ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd); +} + +static int tcp_nip_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack, + u32 ack_seq) +{ + struct tcp_sock *tp = tcp_sk(sk); + int flag = 0; + u32 nwin = ntohs(tcp_hdr(skb)->window); + + if (likely(!tcp_hdr(skb)->syn)) + nwin <<= tp->rx_opt.snd_wscale; + + if (tcp_nip_may_update_window(tp, ack, ack_seq, nwin)) { + flag |= FLAG_WIN_UPDATE; + tcp_update_wl(tp, ack_seq); + + if (tp->snd_wnd != nwin) { + tp->snd_wnd = nwin; + tp->pred_flags = 0; + } + } + + return flag; +} + +/* Check whether the ACK returned by the packet is detected + *and whether the peer window is opened + */ +static void tcp_nip_ack_probe(struct sock *sk) +{ + const struct tcp_sock *tp = tcp_sk(sk); + struct inet_connection_sock *icsk = inet_csk(sk); + + if (!after(TCP_SKB_CB(tcp_nip_send_head(sk))->end_seq, tcp_wnd_end(tp))) { + icsk->icsk_backoff = 0; + inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0); + /* Socket must be waked up by subsequent tcp_data_snd_check(). + * This function is not for random using! + */ + } else { + unsigned long when = tcp_probe0_when(sk, TCP_RTO_MAX); + + inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0, + when, TCP_RTO_MAX); + } +} + +#define DUP_ACK 0 +#define NOR_ACK 1 +#define ACK_DEF 2 +static void tcp_nip_ack_retrans(struct sock *sk, u32 ack, int ack_type) +{ + int skb_index = 0; + struct tcp_sock *tp = tcp_sk(sk); + struct sk_buff *skb, *tmp; + const char *ack_str[ACK_DEF] = {"dup", "nor"}; + int index = ack_type == DUP_ACK ? DUP_ACK : NOR_ACK; + + skb_queue_walk_safe(&sk->sk_write_queue, skb, tmp) { + if (skb == tcp_nip_send_head(sk)) { + SSTHRESH_DBG("%s %s ack retrans(%u) end, ack=%u, seq=%u~%u, pkt_out=%u", + __func__, ack_str[index], tp->ack_retrans_num, ack, + tp->selective_acks[0].start_seq, + tp->selective_acks[0].end_seq, tp->packets_out); + tp->selective_acks[0].start_seq = 0; + tp->selective_acks[0].end_seq = 0; + tp->ack_retrans_seq = 0; + tp->ack_retrans_num = 0; + break; + } + + if (TCP_SKB_CB(skb)->seq > tp->selective_acks[0].end_seq) { + SSTHRESH_DBG("%s %s ack retrans(%u) finish, ack=%u, seq=%u~%u, pkt_out=%u", + __func__, ack_str[index], tp->ack_retrans_num, ack, + tp->selective_acks[0].start_seq, + tp->selective_acks[0].end_seq, tp->packets_out); + + tp->selective_acks[0].start_seq = 0; + tp->selective_acks[0].end_seq = 0; + tp->ack_retrans_seq = 0; + tp->ack_retrans_num = 0; + break; + } + + if (TCP_SKB_CB(skb)->seq != tp->ack_retrans_seq) + continue; + + if (skb_index < g_ack_retrans_num) { + tcp_nip_retransmit_skb(sk, skb, 1); + skb_index++; + tp->ack_retrans_num++; + tp->ack_retrans_seq = TCP_SKB_CB(skb)->end_seq; + } else { + RETRANS_DBG("%s %s ack retrans(%u) no end, ack=%u, seq=%u~%u, pkt_out=%u", + __func__, ack_str[index], tp->ack_retrans_num, ack, + tp->selective_acks[0].start_seq, + tp->selective_acks[0].end_seq, tp->packets_out); + break; + } + } +} + +#define DUP_ACK_RETRANS_START_NUM 3 +#define DIVIDEND_UP 3 +#define DIVIDEND_DOWN 5 +static void tcp_nip_dup_ack_retrans(struct sock *sk, u32 ack) +{ + struct tcp_sock *tp = tcp_sk(sk); + + if (tcp_write_queue_head(sk)) { + tp->sacked_out++; + if (tp->sacked_out == DUP_ACK_RETRANS_START_NUM) { + int last_nip_ssthresh = tp->nip_ssthresh; + int nip_ssthresh = (tp->nip_ssthresh * DIVIDEND_UP) / DIVIDEND_DOWN; + + tp->nip_ssthresh = nip_ssthresh < g_ssthresh_low ? + g_ssthresh_low : nip_ssthresh; + if (tp->selective_acks[0].end_seq) + SSTHRESH_DBG("%s last retans(%u) not end, seq=%u~%u, pkt_out=%u", + __func__, tp->ack_retrans_num, + tp->selective_acks[0].start_seq, + tp->selective_acks[0].end_seq, + tp->packets_out); + + SSTHRESH_DBG("%s new dup ack, win %u to %u, seq=%u~%u", + __func__, last_nip_ssthresh, tp->nip_ssthresh, + ack, tp->snd_nxt); + + tp->selective_acks[0].start_seq = ack; + tp->selective_acks[0].end_seq = tp->snd_nxt; + tp->ack_retrans_seq = ack; + tp->ack_retrans_num = 0; + + tcp_nip_ack_retrans(sk, ack, DUP_ACK); + } + } +} + +static void tcp_nip_nor_ack_retrans(struct sock *sk, u32 ack) +{ + struct tcp_sock *tp = tcp_sk(sk); + + if (tp->selective_acks[0].end_seq != 0) { + if (ack >= tp->selective_acks[0].end_seq || + (ack >= ((tp->selective_acks[0].end_seq - tp->selective_acks[0].start_seq) / + g_retrans_seg_end_divisor) + tp->selective_acks[0].start_seq)) { + SSTHRESH_DBG("%s nor ack retrans(%u) resume, seq=%u~%u, pkt_out=%u, ack=%u", + __func__, tp->ack_retrans_num, + tp->selective_acks[0].start_seq, + tp->selective_acks[0].end_seq, tp->packets_out, ack); + tp->selective_acks[0].start_seq = 0; + tp->selective_acks[0].end_seq = 0; + tp->ack_retrans_seq = 0; + tp->ack_retrans_num = 0; + + tp->sacked_out = 0; + return; + } + + tcp_nip_ack_retrans(sk, ack, NOR_ACK); + } + + tp->sacked_out = 0; +} + +static void tcp_nip_ack_calc_ssthresh(struct sock *sk, u32 ack, int icsk_rto_last, + ktime_t skb_snd_tstamp) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct inet_connection_sock *icsk = inet_csk(sk); + int ack_reset = ack / g_nip_ssthresh_reset; + u32 nip_ssthresh; + + if (tp->nip_ssthresh_reset != ack_reset) { + SSTHRESH_DBG("%s ack reset win %u to %u, ack=%u", + __func__, tp->nip_ssthresh, g_ssthresh_low, ack); + tp->nip_ssthresh_reset = ack_reset; + tp->nip_ssthresh = g_ssthresh_low; + } else { + if (skb_snd_tstamp) { + u32 rtt_tstamp = tp->rcv_tstamp - skb_snd_tstamp; + + if (rtt_tstamp >= g_rtt_tstamp_rto_up) { + SSTHRESH_DBG("%s rtt %u >= %u, win %u to %u, rto %u to %u, ack=%u", + __func__, rtt_tstamp, g_rtt_tstamp_rto_up, + tp->nip_ssthresh, g_ssthresh_low_min, + icsk_rto_last, icsk->icsk_rto, ack); + + tp->nip_ssthresh = g_ssthresh_low_min; + } else if (rtt_tstamp >= g_rtt_tstamp_high) { + SSTHRESH_DBG("%s rtt %u >= %u, win %u to %u, ack=%u", + __func__, rtt_tstamp, g_rtt_tstamp_high, + tp->nip_ssthresh, g_ssthresh_low, ack); + + tp->nip_ssthresh = g_ssthresh_low; + } else if (rtt_tstamp >= g_rtt_tstamp_mid_high) { + SSTHRESH_DBG("%s rtt %u >= %u, win %u to %u, ack=%u", + __func__, rtt_tstamp, g_rtt_tstamp_mid_high, + tp->nip_ssthresh, g_ssthresh_mid_low, ack); + + tp->nip_ssthresh = g_ssthresh_mid_low; + } else if (rtt_tstamp >= g_rtt_tstamp_mid_low) { + u32 rtt_tstamp_scale = g_rtt_tstamp_mid_high - rtt_tstamp; + int half_mid_high = g_ssthresh_mid_high / 2; + + nip_ssthresh = half_mid_high + rtt_tstamp_scale * half_mid_high / + (g_rtt_tstamp_mid_high - g_rtt_tstamp_mid_low); + + tp->nip_ssthresh = tp->nip_ssthresh > g_ssthresh_mid_high ? + half_mid_high : tp->nip_ssthresh; + nip_ssthresh = (tp->nip_ssthresh * g_ssthresh_high_step + + nip_ssthresh) / (g_ssthresh_high_step + 1); + + SSTHRESH_DBG("%s rtt %u >= %u, win %u to %u, ack=%u", + __func__, rtt_tstamp, g_rtt_tstamp_mid_low, + tp->nip_ssthresh, nip_ssthresh, ack); + + tp->nip_ssthresh = nip_ssthresh; + } else if (rtt_tstamp != 0) { + nip_ssthresh = (tp->nip_ssthresh * g_ssthresh_high_step + + g_ssthresh_high) / (g_ssthresh_high_step + 1); + + SSTHRESH_DBG("%s rtt %u < %u, win %u to %u, ack=%u", + __func__, rtt_tstamp, g_rtt_tstamp_mid_low, + tp->nip_ssthresh, nip_ssthresh, ack); + + tp->nip_ssthresh = nip_ssthresh; + } + } + } +} + +static int tcp_nip_ack(struct sock *sk, const struct sk_buff *skb, int flag) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct inet_connection_sock *icsk = inet_csk(sk); + u32 prior_snd_una = tp->snd_una; + u32 ack_seq = TCP_SKB_CB(skb)->seq; + u32 ack = TCP_SKB_CB(skb)->ack_seq; + int prior_packets = tp->packets_out; + ktime_t skb_snd_tstamp = 0; + int icsk_rto_last; + + if (before(ack, prior_snd_una)) + return 0; + if (after(ack, tp->snd_nxt)) + return -1; + + flag |= tcp_nip_ack_update_window(sk, skb, ack, ack_seq); + + if (!prior_packets) { + DEBUG("No prior pack and ack is %u\n", ack); + if (tcp_nip_send_head(sk)) + tcp_nip_ack_probe(sk); + } + + icsk->icsk_probes_out = 0; + tp->nip_keepalive_timeout_scale = 0; + tp->rcv_tstamp = tcp_jiffies32; + + if (after(ack, prior_snd_una)) { + icsk->icsk_retransmits = 0; + tp->retrans_stamp = tcp_time_stamp(tp); + tp->rcv_tstamp = tcp_jiffies32; + tcp_nip_snd_una_update(tp, ack); + + icsk_rto_last = icsk->icsk_rto; + tcp_nip_clean_rtx_queue(sk, &skb_snd_tstamp); + + tcp_nip_ack_calc_ssthresh(sk, ack, icsk_rto_last, skb_snd_tstamp); + tcp_nip_nor_ack_retrans(sk, ack); + return 1; + } + + // ack == tp->snd_una + tcp_nip_dup_ack_retrans(sk, ack); + + return 1; +} + +static inline bool tcp_nip_sequence(const struct tcp_sock *tp, u32 seq, u32 end_seq) +{ + /* False is returned if end_seq has been received, + * or if SEq is not behind the receive window + */ + return !before(end_seq, tp->rcv_wup) && + !after(seq, tp->rcv_nxt + tcp_receive_window(tp)); +} + +/* When we get a reset we do this. */ +void tcp_nip_reset(struct sock *sk) +{ + DEBUG("%s: handle RST!", __func__); + + /* We want the right error as BSD sees it (and indeed as we do). */ + switch (sk->sk_state) { + case TCP_SYN_SENT: + sk->sk_err = ECONNREFUSED; + break; + case TCP_CLOSE_WAIT: + sk->sk_err = EPIPE; + break; + case TCP_CLOSE: + return; + default: + sk->sk_err = ECONNRESET; + } + /* This barrier is coupled with smp_rmb() in tcp_poll() */ + smp_wmb(); + + tcp_nip_write_queue_purge(sk); + tcp_nip_done(sk); + + if (!sock_flag(sk, SOCK_DEAD)) + sk->sk_error_report(sk); +} + +/* Reack some incorrect packets, because if you do not ACK these packets, + * they may be retransmitted frequently + */ +static void tcp_nip_send_dupack(struct sock *sk, const struct sk_buff *skb) +{ + struct tcp_sock *tp = tcp_sk(sk); + + if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && + before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { + NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST); + } + DEBUG("[nip]%s send dupack!\n", __func__); + tcp_nip_send_ack(sk); +} + +static bool tcp_nip_reset_check(const struct sock *sk, const struct sk_buff *skb) +{ + struct tcp_sock *tp = tcp_sk(sk); + + return unlikely(TCP_SKB_CB(skb)->seq == (tp->rcv_nxt - 1) && + (1 << sk->sk_state) & (TCPF_CLOSE_WAIT | TCPF_LAST_ACK | + TCPF_CLOSING)); +} + +/* This function is used to process the SYN received in RST packets + * and illegal SEQ packets in ESTABLISHED state. Currently only seQ checks are included + */ +static bool tcp_nip_validate_incoming(struct sock *sk, struct sk_buff *skb, + const struct tcphdr *th, int syn_inerr) +{ + struct tcp_sock *tp = tcp_sk(sk); + bool rst_seq_match = false; + + /* Step 1: check sequence number */ + /* Check for unexpected packets. For some probe packets, + * unexpected packets do not need to be processed, but reply for an ACK + */ + if (!tcp_nip_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) { + DEBUG("%s receive an err seq and seq is %u, ack is %u\n", __func__, + TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq); + if (!th->rst) + tcp_nip_send_dupack(sk, skb); + else if (tcp_nip_reset_check(sk, skb)) + tcp_nip_reset(sk); + goto discard; + } + + /* Step 2: check RST bit */ + if (th->rst) { + if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt || tcp_nip_reset_check(sk, skb)) + rst_seq_match = true; + if (rst_seq_match) + tcp_nip_reset(sk); + goto discard; + } + + return true; + +discard: + tcp_drop(sk, skb); + return false; +} + +void tcp_nip_rcv_established(struct sock *sk, struct sk_buff *skb, + const struct tcphdr *th, unsigned int len) +{ + struct tcp_sock *tp = tcp_sk(sk); + + tcp_mstamp_refresh(tp); + if (!tcp_nip_validate_incoming(sk, skb, th, 1)) + return; + + if (tcp_nip_ack(sk, skb, 0) < 0) + goto discard; + + tcp_nip_data_queue(sk, skb); + tcp_nip_data_snd_check(sk); + tcp_nip_ack_snd_check(sk); + + return; + +discard: + tcp_drop(sk, skb); +} + +static u32 tcp_default_init_rwnd(u32 mss) +{ + u32 init_rwnd = TCP_INIT_CWND * 2; + + if (mss > TCP_MAX_MSS) + init_rwnd = max((TCP_MAX_MSS * init_rwnd) / mss, 2U); + return init_rwnd; +} + +static void tcp_nip_fixup_rcvbuf(struct sock *sk) +{ + u32 mss = TCP_BASE_MSS; + int rcvmem; + + rcvmem = TCP_NUM_2 * SKB_TRUESIZE(mss + MAX_TCP_HEADER) * + tcp_default_init_rwnd(mss); + + if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) + rcvmem <<= TCP_NIP_4BYTE_PAYLOAD; + + if (sk->sk_rcvbuf < rcvmem) + sk->sk_rcvbuf = min(rcvmem, + sock_net(sk)->ipv4.sysctl_tcp_rmem[TCP_ARRAY_INDEX_2]); +} + +#define TCP_NIP_SND_BUF_SIZE 30720 +void tcp_nip_init_buffer_space(struct sock *sk) +{ + int tcp_app_win = sock_net(sk)->ipv4.sysctl_tcp_app_win; + struct tcp_sock *tp = tcp_sk(sk); + int maxwin; + + if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) + tcp_nip_fixup_rcvbuf(sk); + + tp->rcvq_space.space = tp->rcv_wnd; + tcp_mstamp_refresh(tp); + tp->rcvq_space.time = jiffies; + tp->rcvq_space.seq = tp->copied_seq; + maxwin = tcp_full_space(sk); + if (tp->window_clamp >= maxwin) { + tp->window_clamp = maxwin; + if (tcp_app_win && maxwin > TCP_NUM_4 * tp->advmss) + tp->window_clamp = max(maxwin - + (maxwin >> tcp_app_win), + TCP_NUM_4 * tp->advmss); + } + /* Force reservation of one segment. */ + if (tcp_app_win && + tp->window_clamp > TCP_NUM_2 * tp->advmss && + tp->window_clamp + tp->advmss > maxwin) + tp->window_clamp = max(TCP_NUM_2 * tp->advmss, maxwin - tp->advmss); + tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp); + tp->snd_cwnd_stamp = tcp_jiffies32; +} + +void tcp_nip_finish_connect(struct sock *sk, struct sk_buff *skb) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct inet_connection_sock *icsk = inet_csk(sk); + + tcp_set_state(sk, TCP_ESTABLISHED); + icsk->icsk_ack.lrcvtime = tcp_jiffies32; + if (skb) { + icsk->icsk_af_ops->sk_rx_dst_set(sk, skb); + security_inet_conn_established(sk, skb); + } + + tp->lsndtime = tcp_jiffies32; + + tcp_nip_init_buffer_space(sk); +} + +/* Function: + * A function that handles the second handshake + * Parameter: + * sk: transmission control block + * skb: Transfer control block buffer + * Th: TCP header field + */ +static int tcp_nip_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb, + const struct tcphdr *th) +{ + struct inet_connection_sock *icsk = inet_csk(sk); + struct tcp_sock *tp = tcp_sk(sk); + int saved_clamp = tp->rx_opt.mss_clamp; + + /* TCP Option Parsing */ + tcp_nip_parse_options(skb, &tp->rx_opt, 0, NULL); + /* Rcv_tsecr saves the timestamp of the last TCP segment received from the peer end */ + if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) + tp->rx_opt.rcv_tsecr -= tp->tsoffset; + + if (th->ack) { + /* Whether the ACK value is between the initial send sequence number + * and the next sequence number + */ + if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) || + after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) + goto reset_and_undo; + /* Must be within the corresponding time*/ + if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && + !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp, tcp_time_stamp(tp))) { + NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSACTIVEREJECTED); + goto reset_and_undo; + } + + if (th->rst) { + tcp_nip_reset(sk); + goto discard; + } + + if (!th->syn) + goto discard_and_undo; + + tcp_init_wl(tp, TCP_SKB_CB(skb)->seq); + + tcp_nip_ack(sk, skb, FLAG_SLOWPATH); + tp->nip_out_of_order_queue = NULL; + /* The next data number expected to be accepted is +1 */ + tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1; + /* Accept the left margin of the window +1 */ + tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1; + tp->snd_wnd = ntohs(th->window); + + if (g_wscale_enable == 1) { + tp->rx_opt.wscale_ok = 1; + tp->rx_opt.snd_wscale = g_wscale; + tp->rx_opt.rcv_wscale = g_wscale; + } + + if (!tp->rx_opt.wscale_ok) { + tp->rx_opt.snd_wscale = 0; + tp->rx_opt.rcv_wscale = 0; + tp->window_clamp = min(tp->window_clamp, 65535U); + } + + if (tp->rx_opt.saw_tstamp) { + tp->rx_opt.tstamp_ok = 1; + tp->tcp_header_len = + sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED; + tp->advmss -= TCPOLEN_TSTAMP_ALIGNED; + tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval; + tp->rx_opt.ts_recent_stamp = get_seconds(); + } else { + tp->tcp_header_len = sizeof(struct tcphdr); + } + + tp->copied_seq = tp->rcv_nxt; + /* Invoke memory barrier (annotated prior to checkpatch requirements) */ + smp_mb(); + + tcp_nip_sync_mss(sk, icsk->icsk_pmtu_cookie); + tcp_nip_initialize_rcv_mss(sk); + + tcp_nip_finish_connect(sk, skb); + /* Wake up the process */ + if (!sock_flag(sk, SOCK_DEAD)) { + sk->sk_state_change(sk); + rcu_read_lock(); + sock_wake_async(rcu_dereference(sk->sk_wq), SOCK_WAKE_IO, POLL_OUT); + rcu_read_unlock(); + } + + tcp_nip_send_ack(sk); + return -1; +discard: + tcp_drop(sk, skb); + return 0; + } + +discard_and_undo: + tcp_clear_options(&tp->rx_opt); + tp->rx_opt.mss_clamp = saved_clamp; + goto discard; + +reset_and_undo: + tcp_clear_options(&tp->rx_opt); + tp->rx_opt.mss_clamp = saved_clamp; + return 1; +} + +/* Function: + * TCP processing function that is differentiated according to + * different states after receiving data packets + * Parameter: + * sk: transmission control block + * skb: Transfer control block buffer + * Note: Currently this function only has code for handling the first handshake packet + * Implementation of the third handshake ACK to handle the code + */ +int tcp_nip_rcv_state_process(struct sock *sk, struct sk_buff *skb) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct inet_connection_sock *icsk = inet_csk(sk); + const struct tcphdr *th = tcp_hdr(skb); + int queued = 0; + bool acceptable; + + /* Step 1: Connect handshake packet processing */ + switch (sk->sk_state) { + case TCP_CLOSE: + goto discard; + + case TCP_LISTEN: + if (th->ack) + return 1; + + if (th->rst) + goto discard; + + if (th->syn) { + if (th->fin) + goto discard; + + rcu_read_lock(); + local_bh_disable(); + acceptable = icsk->icsk_af_ops->conn_request(sk, skb) >= 0; + local_bh_enable(); + rcu_read_unlock(); + + if (!acceptable) + return 1; + consume_skb(skb); + return 0; + } + goto discard; + case TCP_SYN_SENT: + DEBUG("%s TCP_SYN_SENT!!\n", __func__); + tp->rx_opt.saw_tstamp = 0; + tcp_mstamp_refresh(tp); + queued = tcp_nip_rcv_synsent_state_process(sk, skb, th); + if (queued >= 0) + return queued; + __kfree_skb(skb); + return 0; + } + tcp_mstamp_refresh(tp); + tp->rx_opt.saw_tstamp = 0; + + if (!th->ack && !th->rst && !th->syn) + goto discard; + + if (!tcp_nip_validate_incoming(sk, skb, th, 0)) + return 0; + + acceptable = tcp_nip_ack(sk, skb, 0); + + /* If the third handshake ACK is invalid, 1 is returned + * and the SKB is discarded in tcp_nip_rcv + */ + if (!acceptable) { + if (sk->sk_state == TCP_SYN_RECV) + return 1; + goto discard; + } + + switch (sk->sk_state) { + case TCP_SYN_RECV: + tp->copied_seq = tp->rcv_nxt; + tcp_nip_init_buffer_space(sk); + /* Invoke memory barrier (annotated prior to checkpatch requirements) */ + smp_mb(); + tcp_set_state(sk, TCP_ESTABLISHED); + DEBUG("TCP_ESTABLISHED!!!!!\n"); + sk->sk_state_change(sk); + + /* Sets the part to be sent, and the size of the send window */ + tp->snd_una = TCP_SKB_CB(skb)->ack_seq; + tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale; + tcp_init_wl(tp, TCP_SKB_CB(skb)->seq); + + tp->lsndtime = tcp_jiffies32; + + tcp_initialize_rcv_mss(sk); + break; + case TCP_FIN_WAIT1: { + if (tp->snd_una != tp->write_seq) { + DEBUG("%s: tp->snd_una != tp->write_seq!!\n", __func__); + break; + } + + tcp_set_state(sk, TCP_FIN_WAIT2); + sk->sk_shutdown |= SEND_SHUTDOWN; + + if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && + after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) { + tcp_nip_done(sk); + DEBUG("%s: received payload packets, call tcp_nip_done.\n", __func__); + return 1; + } + + DEBUG("%s: TCP_FIN_WAIT1: recvd ack for fin.Wait for fin from other side.\n", + __func__); + inet_csk_reset_keepalive_timer(sk, TCP_NIP_CSK_KEEPALIVE_CYCLE * HZ); + + break; + } + + case TCP_CLOSING: + if (tp->snd_una == tp->write_seq) { + DEBUG("%s: TCP_CLOSING: recvd ack for fin.Ready to destroy.\n", __func__); + inet_csk_reset_keepalive_timer(sk, TCP_TIMEWAIT_LEN); + goto discard; + } + break; + case TCP_LAST_ACK: + DEBUG("tcp_nip_rcv_state_process_2: TCP_LAST_ACK\n"); + if (tp->snd_una == tp->write_seq) { + DEBUG("%s: LAST_ACK: recvd ack for fin.Directly destroy.\n", __func__); + tcp_nip_done(sk); + goto discard; + } + break; + } + + switch (sk->sk_state) { + case TCP_CLOSE_WAIT: + DEBUG("%s: into TCP_CLOSE_WAIT, rst = %d, seq = %u, end_seq = %u, rcv_nxt = %u\n", + __func__, th->rst, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->seq, tp->rcv_nxt); + fallthrough; + case TCP_CLOSING: + case TCP_LAST_ACK: + if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { + DEBUG("%s: break in TCP_LAST_ACK\n", __func__); + break; + } + DEBUG("tcp_nip_rcv_state_process_3: TCP_LAST_ACK_2\n"); + fallthrough; + case TCP_FIN_WAIT1: + case TCP_FIN_WAIT2: + /* Reset is required according to RFC 1122. + * Do not enter the reset process temporarily + */ + if (sk->sk_shutdown & RCV_SHUTDOWN) { + if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && + after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) { + tcp_nip_reset(sk); + DEBUG("%s: call tcp_nip_reset\n", __func__); + return 1; + } + } + fallthrough; + case TCP_ESTABLISHED: + tcp_nip_data_queue(sk, skb); + queued = 1; + break; + } + + if (sk->sk_state != TCP_CLOSE) { + tcp_nip_data_snd_check(sk); + tcp_nip_ack_snd_check(sk); + } + + if (!queued) { +discard: + tcp_nip_drop(sk, skb); + } + return 0; +} + +/* Function + * Initialize RCV_MSS + * Parameter + * sk: transmission control block + */ +void tcp_nip_initialize_rcv_mss(struct sock *sk) +{ + const struct tcp_sock *tp = tcp_sk(sk); + unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache); + + hint = min(hint, tp->rcv_wnd / TCP_NUM_2); + hint = min(hint, TCP_MSS_DEFAULT); + hint = max(hint, TCP_MIN_MSS); + + inet_csk(sk)->icsk_ack.rcv_mss = hint; +} + +/* Function + * Handle the third handshake ACK and return the new control block successfully. + * Is the core process for handling ACKS. + * (1)Create a child control block. Note that the state of the child control + * block is TCP_SYN_RECV + * This is different from the TCP_NEW_SYN_RECV control block created when syn was received. + * (2)Remove the request control block from the incomplete connection queue + * and add it to the completed connection queue + * Parameter + * sk: transmission control block + * skb: Transfer control block buffer + * req: Request connection control block + */ +struct sock *tcp_nip_check_req(struct sock *sk, struct sk_buff *skb, + struct request_sock *req) +{ + struct tcp_options_received tmp_opt; + struct sock *child; + const struct tcphdr *th = tcp_hdr(skb); + __be32 flg = tcp_flag_word(th) & (TCP_FLAG_RST | TCP_FLAG_SYN | TCP_FLAG_ACK); + bool own_req; + + tmp_opt.saw_tstamp = 0; + /* Check whether the TCP option exists */ + if (th->doff > (sizeof(struct tcphdr) >> TCP_NIP_4BYTE_PAYLOAD)) { + /* Parsing TCP options */ + tcp_nip_parse_options(skb, &tmp_opt, 0, NULL); + } + + /* ACK but the serial number does not match, + * return to the original control block, no processing outside + */ + if ((flg & TCP_FLAG_ACK) && + (TCP_SKB_CB(skb)->ack_seq != + tcp_rsk(req)->snt_isn + 1)) { + DEBUG("%s ack_seq is wrong!", __func__); + return sk; + } + + /* The above process guarantees that there is an ACK, if not, return directly */ + if (!(flg & TCP_FLAG_ACK)) { + DEBUG("%s No TCP_FLAG_ACK !!!!", __func__); + return NULL; + } + + /* The ack is valid and the child control block is created. + * Note that the state of the child control block is TCP_SYN_RECV + */ + child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL, + req, &own_req); + if (!child) { + DEBUG("%s No listen_overflow!!!!", __func__); + goto listen_overflow; + } + DEBUG("%s creat child sock successfully!", __func__); + + sock_rps_save_rxhash(child, skb); + /* Calculate the time spent synack-ack in three handshakes */ + tcp_synack_rtt_meas(child, req); + /* Delete the original control block from the incomplete queue + * and add it to the completed queue + */ + return inet_csk_complete_hashdance(sk, child, req, own_req); + +listen_overflow: + if (!sock_net(sk)->ipv4.sysctl_tcp_abort_on_overflow) { + inet_rsk(req)->acked = 1; + return NULL; + } + return NULL; +} + diff --git a/code/net/newip/tcp_nip_output.c b/code/net/newip/tcp_nip_output.c new file mode 100644 index 0000000..c9030b4 --- /dev/null +++ b/code/net/newip/tcp_nip_output.c @@ -0,0 +1,1257 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * + * NewIP INET + * An implementation of the TCP/IP protocol suite for the LINUX + * operating system. NewIP INET is implemented using the BSD Socket + * interface as the means of communication with the user level. + * + * Implementation of the Transmission Control Protocol(TCP). + * + * Based on net/ipv4/tcp_output.c + * Based on net/ipv4/tcp_minisocks.c + */ +#define pr_fmt(fmt) "NIP-TCP: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include "nip_hdr.h" +#include "nip_checksum.h" +#include "tcp_nip_parameter.h" + +#define OPTION_SACK_ADVERTISE BIT(0) +#define OPTION_TS BIT(1) +#define OPTION_MD5 BIT(2) +#define OPTION_WSCALE BIT(3) +#define OPTION_FAST_OPEN_COOKIE BIT(8) + +/* Store the options contained in TCP when sending TCP packets */ +struct tcp_nip_out_options { + u16 options; /* bit field of OPTION_* */ + u16 mss; /* If it is zero, the MSS option is disabled */ + + u8 ws; /* window scale, 0 to disable, If the window is enlarged, + * 0 indicates that the option is disabled + */ + u8 hash_size; /* bytes in hash_location */ + __u8 *hash_location; /* temporary pointer, overloaded */ + __u32 tsval, tsecr; /* need to include OPTION_TS */ +}; + +static bool tcp_nip_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle, + int push_one, gfp_t gfp); + +static void tcp_nip_event_data_sent(struct tcp_sock *tp, + struct sock *sk) +{ +} + +/* Calculate MSS not accounting any TCP options. */ +static inline int __tcp_nip_mtu_to_mss(struct sock *sk, int pmtu) +{ + const struct tcp_sock *tp = tcp_sk(sk); + const struct inet_connection_sock *icsk = inet_csk(sk); + int mss_now; + + /* Calculate base mss without TCP options: It is MMS_S - sizeof(tcphdr) of rfc1122 */ + mss_now = pmtu - NIP_HDR_MAX - sizeof(struct tcphdr); + + /* IPv6 adds a frag_hdr in case RTAX_FEATURE_ALLFRAG is set */ + if (icsk->icsk_af_ops->net_frag_header_len) { + const struct dst_entry *dst = __sk_dst_get(sk); + + if (dst && dst_allfrag(dst)) + mss_now -= icsk->icsk_af_ops->net_frag_header_len; + } + + /* Clamp it (mss_clamp does not include tcp options) */ + if (mss_now > tp->rx_opt.mss_clamp) + mss_now = tp->rx_opt.mss_clamp; + + /* Now subtract optional transport overhead */ + mss_now -= icsk->icsk_ext_hdr_len; + + /* Then reserve room for full set of TCP options and 8 bytes of data */ + mss_now = max(mss_now, sock_net(sk)->ipv4.sysctl_tcp_min_snd_mss); + return mss_now; +} + +/* Calculate MSS. Not accounting for SACKs here. */ +int tcp_nip_mtu_to_mss(struct sock *sk, int pmtu) +{ + /* Subtract TCP options size, not including SACKs */ + return __tcp_nip_mtu_to_mss(sk, pmtu) - + (tcp_sk(sk)->tcp_header_len - sizeof(struct tcphdr)); +} + +/* Inverse of above */ +int tcp_nip_mss_to_mtu(struct sock *sk, int mss) +{ + const struct tcp_sock *tp = tcp_sk(sk); + const struct inet_connection_sock *icsk = inet_csk(sk); + int mtu; + + mtu = mss + + tp->tcp_header_len + + icsk->icsk_ext_hdr_len + + NIP_HDR_MAX; + /* IPv6 adds a frag_hdr in case RTAX_FEATURE_ALLFRAG is set */ + if (icsk->icsk_af_ops->net_frag_header_len) { + const struct dst_entry *dst = __sk_dst_get(sk); + + if (dst && dst_allfrag(dst)) + mtu += icsk->icsk_af_ops->net_frag_header_len; + } + return mtu; +} + +static inline void tcp_advance_send_head(struct sock *sk, const struct sk_buff *skb) +{ + if (tcp_skb_is_last(sk, skb)) + sk->sk_send_head = NULL; + else + sk->sk_send_head = skb_queue_next(&sk->sk_write_queue, skb); +} + +static void tcp_nip_event_new_data_sent(struct sock *sk, struct sk_buff *skb) +{ + struct inet_connection_sock *icsk = inet_csk(sk); + struct tcp_sock *tp = tcp_sk(sk); + unsigned int prior_packets = tp->packets_out; + + tcp_advance_send_head(sk, skb); + tp->snd_nxt = TCP_SKB_CB(skb)->end_seq; + tp->packets_out += tcp_skb_pcount(skb); + if (!prior_packets || icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS || + icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { + tcp_nip_rearm_rto(sk); + } +} + +void __tcp_nip_push_pending_frames(struct sock *sk, unsigned int cur_mss, + int nonagle) +{ + if (unlikely(sk->sk_state == TCP_CLOSE)) + return; + + if (tcp_nip_write_xmit(sk, cur_mss, nonagle, 0, + sk_gfp_mask(sk, GFP_ATOMIC))) { + DEBUG("%s check probe0 timer!\n", __func__); + tcp_nip_check_probe_timer(sk); + } +} + +u32 __nip_tcp_select_window(struct sock *sk) +{ + struct inet_connection_sock *icsk = inet_csk(sk); + struct tcp_sock *tp = tcp_sk(sk); + int mss = tcp_nip_current_mss(sk); // TCP_BASE_MSS + int free_space; + int allowed_space; + int full_space; + int window; + + if (g_rcv_win_max) { + allowed_space = g_rcv_win_max; + full_space = allowed_space; + WRITE_ONCE(sk->sk_rcvbuf, g_nip_rcvbuf); + free_space = tcp_space(sk); + } else { + allowed_space = tcp_full_space(sk); + full_space = min_t(int, tp->window_clamp, allowed_space); + free_space = tcp_space(sk); + } + + if (unlikely(mss > full_space)) { + mss = full_space; + if (mss <= 0) + return 0; + } + + if (free_space < (full_space >> 1)) { + icsk->icsk_ack.quick = 0; + + free_space = round_down(free_space, 1 << tp->rx_opt.rcv_wscale); + if (free_space < (allowed_space >> TCP_NUM_4) || free_space < mss) + return 0; + } + + if (g_nip_tcp_rcv_win_enable) { + if (g_ssthresh_enable == 1) + free_space = free_space > tp->nip_ssthresh ? tp->nip_ssthresh : free_space; + else + free_space = free_space > tp->rcv_ssthresh ? tp->rcv_ssthresh : free_space; + } else { + free_space = free_space > g_ssthresh_high ? g_ssthresh_high : free_space; + } + + window = tp->rcv_wnd; + if (tp->rx_opt.rcv_wscale) { + window = free_space; + if (((window >> tp->rx_opt.rcv_wscale) << tp->rx_opt.rcv_wscale) != window) + window = (((window >> tp->rx_opt.rcv_wscale) + 1) + << tp->rx_opt.rcv_wscale); + } else { + if (window <= free_space - mss || window > free_space) + window = (free_space / mss) * mss; + else if (mss == full_space && + free_space > window + (full_space >> 1)) + window = free_space; + } + return window; +} + +static u16 nip_tcp_select_window(struct sock *sk) +{ + struct tcp_sock *tp = tcp_sk(sk); + u32 old_win = tp->rcv_wnd; + u32 cur_win = tcp_receive_window(tp); + u32 new_win = __nip_tcp_select_window(sk); + + if (new_win < cur_win) { + if (new_win == 0) + NET_INC_STATS(sock_net(sk), + LINUX_MIB_TCPWANTZEROWINDOWADV); + new_win = ALIGN(cur_win, 1 << tp->rx_opt.rcv_wscale); + } + tp->rcv_wnd = new_win; + tp->rcv_wup = tp->rcv_nxt; + + if (!tp->rx_opt.rcv_wscale && + sock_net(sk)->ipv4.sysctl_tcp_workaround_signed_windows) + new_win = min(new_win, MAX_TCP_WINDOW); + else + new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale)); + + new_win >>= tp->rx_opt.rcv_wscale; + if (new_win == 0) { + tp->pred_flags = 0; + if (old_win) + NET_INC_STATS(sock_net(sk), + LINUX_MIB_TCPTOZEROWINDOWADV); + } else if (old_win == 0) { + NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFROMZEROWINDOWADV); + } + + return new_win; +} + +/* Function + * Initialize transport layer parameters. + * Parameter + * sk: transmission control block. + */ +static void tcp_nip_connect_init(struct sock *sk) +{ + const struct dst_entry *dst = __sk_dst_get(sk); + struct tcp_sock *tp = tcp_sk(sk); + __u8 rcv_wscale = 0; + int sysctl_tcp_nip_window_scaling = 0; + + /* Header structure length + timestamp length */ + tp->tcp_header_len = sizeof(struct tcphdr); + if (sock_net(sk)->ipv4.sysctl_tcp_timestamps) + tp->tcp_header_len += TCPOLEN_TSTAMP_ALIGNED; + + if (tp->rx_opt.user_mss) + tp->rx_opt.mss_clamp = tp->rx_opt.user_mss; + tp->max_window = 0; + + tcp_mtup_init(sk); + tp->rx_opt.mss_clamp = tcp_nip_sync_mss(sk, dst_mtu(dst)); + + if (!tp->window_clamp) + tp->window_clamp = dst_metric(dst, RTAX_WINDOW); + tp->advmss = tcp_mss_clamp(tp, dst_metric_advmss(dst)); + + tcp_initialize_rcv_mss(sk); + + /* Initialization window */ + tcp_select_initial_window(sk, tcp_full_space(sk), + tp->advmss - (tp->rx_opt.ts_recent_stamp ? + tp->tcp_header_len - sizeof(struct tcphdr) : 0), + &tp->rcv_wnd, + &tp->window_clamp, + sysctl_tcp_nip_window_scaling, + &rcv_wscale, + 0); + + tp->rx_opt.rcv_wscale = g_wscale_enable == 1 ? g_wscale : rcv_wscale; + tp->rcv_ssthresh = tp->rcv_wnd; + + sk->sk_err = 0; + sock_reset_flag(sk, SOCK_DONE); + tp->snd_wnd = 0; + tp->snd_wl1 = 0; + tcp_write_queue_purge(sk); + + tp->snd_una = tp->write_seq; + tp->snd_sml = tp->write_seq; + tp->snd_up = tp->write_seq; + tp->snd_nxt = tp->write_seq; + + tp->rcv_nxt = 0; + tp->rcv_wup = tp->rcv_nxt; + tp->copied_seq = tp->rcv_nxt; + inet_csk(sk)->icsk_rto = g_nip_rto == 0 ? TCP_TIMEOUT_INIT : (HZ / g_nip_rto); + inet_csk(sk)->icsk_retransmits = 0; + tcp_clear_retrans(tp); +} + +static void tcp_nip_init_nondata_skb(struct sk_buff *skb, u32 seq, u8 flags) +{ + skb->ip_summed = CHECKSUM_PARTIAL; + skb->csum = 0; + + TCP_SKB_CB(skb)->tcp_flags = flags; + TCP_SKB_CB(skb)->sacked = 0; + + tcp_skb_pcount_set(skb, 1); + + TCP_SKB_CB(skb)->seq = seq; + if (flags & (TCPHDR_SYN | TCPHDR_FIN)) + seq++; + TCP_SKB_CB(skb)->end_seq = seq; +} + +#define OPTION_TS BIT(1) +#define OPTION_WSCALE BIT(3) + +static void tcp_nip_connect_queue_skb(struct sock *sk, struct sk_buff *skb) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); + + tcb->end_seq += skb->len; + __skb_header_release(skb); + __skb_queue_tail(&sk->sk_write_queue, skb); + sk->sk_wmem_queued += skb->truesize; + sk_mem_charge(sk, skb->truesize); + WRITE_ONCE(tp->write_seq, tcb->end_seq); + tp->packets_out += tcp_skb_pcount(skb); +} + +static __u16 tcp_nip_advertise_mss(struct sock *sk) +{ + struct tcp_sock *tp = tcp_sk(sk); + const struct dst_entry *dst = __sk_dst_get(sk); + int mss = tp->advmss; + + if (dst) { + unsigned int metric = dst_metric_advmss(dst); + + if (metric < mss) { + mss = metric; + tp->advmss = mss; + } + } + + return (__u16)mss; +} + +/* Compute TCP options for SYN packets. This is not the final + * network wire format yet. + */ +static unsigned int tcp_nip_syn_options(struct sock *sk, struct sk_buff *skb, + struct tcp_nip_out_options *opts) +{ + unsigned int remaining = MAX_TCP_OPTION_SPACE; + + opts->mss = tcp_nip_advertise_mss(sk); + DEBUG("advertise mss%d", opts->mss); + remaining -= TCPOLEN_MSS_ALIGNED; + + return MAX_TCP_OPTION_SPACE - remaining; +} + +/* Compute TCP options for ESTABLISHED sockets. This is not the + * final wire format yet. + */ +static unsigned int tcp_nip_established_options(struct sock *sk, struct sk_buff *skb, + struct tcp_nip_out_options *opts) +{ + struct tcp_sock *tp = tcp_sk(sk); + unsigned int size = 0; + + opts->options = 0; + + if (likely(tp->rx_opt.tstamp_ok)) { + opts->options |= OPTION_TS; + opts->tsval = skb ? tcp_skb_timestamp(skb) + tp->tsoffset : 0; + opts->tsecr = tp->rx_opt.ts_recent; + size += TCPOLEN_TSTAMP_ALIGNED; + } + return size; +} + +/* Function + * Put the parameters from the TCP option into SKB. + * Write previously computed TCP options to the packet. + * Parameter + * ptr: pointer to TCP options in SKB. + * tp: transmission control block. + * opts: structure to be sent to temporarily load TCP options. + */ +static void tcp_nip_options_write(__be32 *ptr, struct tcp_sock *tp, + struct tcp_nip_out_options *opts) +{ + if (unlikely(opts->mss)) { + *ptr++ = htonl((TCPOPT_MSS << TCP_OPT_MSS_PAYLOAD) | + (TCPOLEN_MSS << TCP_OLEN_MSS_PAYLOAD) | + opts->mss); + } +} + +static inline void tcp_nip_event_ack_sent(struct sock *sk, unsigned int pkts, + u32 rcv_nxt) +{ + struct tcp_sock *tp = tcp_sk(sk); + + if (unlikely(rcv_nxt != tp->rcv_nxt)) + return; + inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK); +} + +unsigned short nip_get_output_checksum_tcp(struct sk_buff *skb, struct nip_addr src_addr, + struct nip_addr dst_addr) +{ + struct nip_pseudo_header nph = {0}; + u8 *tcp_hdr = skb_transport_header(skb); + + nph.nexthdr = IPPROTO_TCP; + nph.saddr = src_addr; + nph.daddr = dst_addr; + + nph.check_len = htons(skb->len); + return nip_check_sum_build(tcp_hdr, skb->len, &nph); +} + +static int __tcp_nip_transmit_skb(struct sock *sk, struct sk_buff *skb, + int clone_it, gfp_t gfp_mask, u32 rcv_nxt) +{ + const struct inet_connection_sock *icsk = inet_csk(sk); + struct inet_sock *inet; + struct tcp_sock *tp = tcp_sk(sk); + struct tcp_skb_cb *tcb; + struct tcp_nip_out_options opts; + unsigned int tcp_options_size, tcp_header_size; + struct sk_buff *oskb = NULL; + struct tcphdr *th; + int err = 0; + __be16 len; + unsigned short check = 0; + + if (skb->tstamp == 0) + skb->tstamp = tcp_jiffies32; + + if (clone_it) { + TCP_SKB_CB(skb)->tx.in_flight = TCP_SKB_CB(skb)->end_seq + - tp->snd_una; + oskb = skb; + + tcp_skb_tsorted_save(oskb) { + if (unlikely(skb_cloned(oskb))) + skb = pskb_copy(oskb, gfp_mask); + else + skb = skb_clone(oskb, gfp_mask); + } tcp_skb_tsorted_restore(oskb); + + if (unlikely(!skb)) + return -ENOBUFS; + } + + inet = inet_sk(sk); + tcb = TCP_SKB_CB(skb); + memset(&opts, 0, sizeof(opts)); + + if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) + tcp_options_size = tcp_nip_syn_options(sk, skb, &opts); + else + tcp_options_size = tcp_nip_established_options(sk, skb, &opts); + tcp_header_size = tcp_options_size + sizeof(struct tcphdr); + + skb->ooo_okay = sk_wmem_alloc_get(sk) < SKB_TRUESIZE(1); + /* The data pointer moves up */ + skb_push(skb, tcp_header_size); + skb_reset_transport_header(skb); + + /* Disassociate the control block */ + skb_orphan(skb); + + /* Establishes associations with control blocks */ + skb->sk = sk; + skb->destructor = skb_is_tcp_pure_ack(skb) ? __sock_wfree : tcp_wfree; + skb_set_hash_from_sk(skb, sk); + /* Increase allocated memory */ + refcount_add(skb->truesize, &sk->sk_wmem_alloc); + DEBUG("th->inet_sport==%u, th->inet_dport==%u\n", + ntohs(inet->inet_sport), ntohs(inet->inet_dport)); + DEBUG("sk->sk_rcvbuf==%d, sk->sk_rmem_alloc==%d\n", + sk->sk_rcvbuf, atomic_read(&sk->sk_rmem_alloc)); + /* Build TCP header and checksum it. */ + th = (struct tcphdr *)skb->data; + th->source = inet->inet_sport; + th->dest = inet->inet_dport; + th->seq = htonl(tcb->seq); + th->ack_seq = htonl(rcv_nxt); + /* TCP's header offset is measured in 4 bytes, so moving two to the right + * means dividing by 4. In addition, according to the position of the offset + * field in the packet, the offset field is at the beginning of a short type, + * accounting for 4 bits. Therefore, the offset field should be shifted 12 bits + * to the left + */ + len = htons(((tcp_header_size >> TCP_NIP_4BYTE_PAYLOAD) << TCP_HDR_LEN_POS_PAYLOAD) | + tcb->tcp_flags); + *(((__be16 *)th) + TCP_HDR_LEN_OFFSET) = len; + + th->check = 0; + th->urg_ptr = 0; + + /* Write TCP option */ + tcp_nip_options_write((__be32 *)(th + 1), tp, &opts); + + /* Window Settings */ + if (likely(!(tcb->tcp_flags & TCPHDR_SYN))) + th->window = htons(nip_tcp_select_window(sk)); + else + th->window = htons(min(tp->rcv_wnd, TCP_NIP_WINDOW_MAX)); + + /* Fill in checksum */ + check = nip_get_output_checksum_tcp(skb, sk->sk_nip_rcv_saddr, sk->sk_nip_daddr); + th->check = htons(check); + + if (likely(tcb->tcp_flags & TCPHDR_ACK)) + tcp_nip_event_ack_sent(sk, tcp_skb_pcount(skb), rcv_nxt); + + /* There's data to send */ + if (skb->len != tcp_header_size) { + tcp_nip_event_data_sent(tp, sk); + tp->data_segs_out += tcp_skb_pcount(skb); + } + + memset(skb->cb, 0, sizeof(struct ninet_skb_parm)); + err = icsk->icsk_af_ops->queue_xmit(sk, skb, &inet->cork.fl); + return err; +} + +/* Function + * TCP's transport layer sends code that builds and initializes the TCP header + * Construct the SK_buff call transport layer to network layer interface + * Parameter + * sk: Transmission control block. + * skb: Structure stores all information about network datagrams + */ +int tcp_nip_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it, + gfp_t gfp_mask) +{ + return __tcp_nip_transmit_skb(sk, skb, clone_it, gfp_mask, + tcp_sk(sk)->rcv_nxt); +} + +static void tcp_nip_queue_skb(struct sock *sk, struct sk_buff *skb) +{ + struct tcp_sock *tp = tcp_sk(sk); + + /* Advance write_seq and place onto the write_queue. */ + tp->write_seq = TCP_SKB_CB(skb)->end_seq; + tcp_nip_add_write_queue_tail(sk, skb); + sk->sk_wmem_queued += skb->truesize; + sk_mem_charge(sk, skb->truesize); +} + +/* Function + * A function used by the client transport layer to connect requests. + * Parameter + * sk: transmission control block. + */ +int __tcp_nip_connect(struct sock *sk) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct sk_buff *buff; + int err; + + tcp_nip_connect_init(sk); + buff = sk_stream_alloc_skb(sk, 0, sk->sk_allocation, true); + if (unlikely(!buff)) + return -ENOBUFS; + + /* Initializes the SYN flag bit */ + tcp_nip_init_nondata_skb(buff, tp->write_seq++, TCPHDR_SYN); + tcp_mstamp_refresh(tp); + tp->retrans_stamp = tcp_time_stamp(tp); + tcp_nip_init_xmit_timers(sk); + + tcp_nip_connect_queue_skb(sk, buff); + + /* Send off SYN */ + err = tcp_nip_transmit_skb(sk, buff, 1, sk->sk_allocation); + if (err == -ECONNREFUSED) + return err; + + tp->snd_nxt = tp->write_seq; + tp->pushed_seq = tp->write_seq; + buff = tcp_nip_send_head(sk); + + TCP_INC_STATS(sock_net(sk), TCP_MIB_ACTIVEOPENS); + + /* Timer for repeating the SYN until an answer. */ + inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, + inet_csk(sk)->icsk_rto, TCP_RTO_MAX); + + return 0; +} + +unsigned int tcp_nip_sync_mss(struct sock *sk, u32 pmtu) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct inet_connection_sock *icsk = inet_csk(sk); + int mss_now; + + if (icsk->icsk_mtup.search_high > pmtu) + icsk->icsk_mtup.search_high = pmtu; + + mss_now = tcp_nip_mtu_to_mss(sk, pmtu); + DEBUG("%s: sync mtu_to_mss %d\n", __func__, mss_now); + mss_now = tcp_bound_to_half_wnd(tp, mss_now); + DEBUG("%s: sync bound to half wnd %d\n", __func__, mss_now); + + /* And store cached results */ + icsk->icsk_pmtu_cookie = pmtu; + if (icsk->icsk_mtup.enabled) + mss_now = min(mss_now, tcp_nip_mtu_to_mss(sk, icsk->icsk_mtup.search_low)); + tp->mss_cache = mss_now; + + DEBUG("%s: sync final mss %d\n", __func__, mss_now); + + return mss_now; +} + +unsigned int tcp_nip_current_mss(struct sock *sk) +{ + const struct tcp_sock *tp = tcp_sk(sk); + const struct dst_entry *dst = __sk_dst_get(sk); + u32 mss_now; + unsigned int header_len; + + struct tcp_nip_out_options opts; + + mss_now = tp->mss_cache; + + DEBUG("%s: mss_cache %d\n", __func__, mss_now); + + if (dst) { + u32 mtu = dst_mtu(dst); + + if (mtu != inet_csk(sk)->icsk_pmtu_cookie) + mss_now = tcp_nip_sync_mss(sk, mtu); + DEBUG("%s: mtu %d\n", __func__, mtu); + } + + header_len = tcp_nip_established_options(sk, NULL, &opts) + + sizeof(struct tcphdr); + + if (header_len != tp->tcp_header_len) { + int delta = (int)header_len - tp->tcp_header_len; + + mss_now -= delta; + } + DEBUG("%s:after sync_mss%d\n", __func__, mss_now); + return mss_now; +} + +/* Function: + * Set up TCP options for SYN-ACKs. + * Initializes the TCP option for the SYN-ACK segment. Returns the SIZE of the TCP header. + * Parameter + * req: Request connection control block. + * mss: maximum segment length. + * skb: Transfer control block buffer. + * opts: stores the options contained in TCP packets when they are sent. + * foc: Fast Open option. + * synack_type: type of SYN+ACK segment. + */ +static unsigned int tcp_nip_synack_options(struct request_sock *req, + unsigned int mss, struct sk_buff *skb, + struct tcp_nip_out_options *opts, + const struct tcp_md5sig_key *md5, + struct tcp_fastopen_cookie *foc, + enum tcp_synack_type synack_type) +{ + struct inet_request_sock *ireq = inet_rsk(req); + unsigned int remaining = MAX_TCP_OPTION_SPACE; + + /* We always send an MSS option. */ + opts->mss = mss; + remaining -= TCPOLEN_MSS_ALIGNED; + + if (likely(ireq->tstamp_ok)) { + opts->options |= OPTION_TS; + opts->tsval = tcp_skb_timestamp(skb); + opts->tsecr = req->ts_recent; + remaining -= TCPOLEN_TSTAMP_ALIGNED; + } + return MAX_TCP_OPTION_SPACE - remaining; +} + +/* Function + * The SYN + ACK segment is constructed based on the current transport control block, + * routing information, and request information. + * Parameter + * sk: transmission control block. + * dst: routing. + * req: Request connection control block. + * foc: Fast Open option. + * synack_type: type of SYN+ACK segment. + */ +struct sk_buff *tcp_nip_make_synack(const struct sock *sk, struct dst_entry *dst, + struct request_sock *req, + struct tcp_fastopen_cookie *foc, + enum tcp_synack_type synack_type) +{ + struct inet_request_sock *ireq = inet_rsk(req); + const struct tcp_sock *tp = tcp_sk(sk); + struct tcp_md5sig_key *md5 = NULL; + struct tcp_nip_out_options opts; + struct sk_buff *skb; + int tcp_header_size; + struct tcphdr *th; + u16 user_mss; + int mss; + unsigned short check = 0; + + skb = alloc_skb(MAX_TCP_HEADER, 0); + if (unlikely(!skb)) { + dst_release(dst); + return NULL; + } + + /* Reserve space for headers. */ + skb_reserve(skb, MAX_TCP_HEADER); + + switch (synack_type) { + case TCP_SYNACK_NORMAL: + /* Release the original SKB and treat itself as the SKB of the current SK */ + skb_set_owner_w(skb, req_to_sk(req)); + break; + default: + break; + } + skb_dst_set(skb, dst); + /* set skb priority from sk */ + skb->priority = sk->sk_priority; + + mss = dst_metric_advmss(dst); + user_mss = READ_ONCE(tp->rx_opt.user_mss); + if (user_mss && user_mss < mss) + mss = user_mss; + + /* Clear the options and set the associated timestamp */ + memset(&opts, 0, sizeof(opts)); + skb->skb_mstamp_ns = tcp_clock_us(); + + /* Get the TCP header size, then set the size and reset the transport layer header */ + skb_set_hash(skb, tcp_rsk(req)->txhash, PKT_HASH_TYPE_L4); + tcp_header_size = tcp_nip_synack_options(req, mss, skb, &opts, md5, + foc, synack_type) + sizeof(*th); + skb_push(skb, tcp_header_size); + skb_reset_transport_header(skb); + + /* Clear the TCP header and set the fields of the TCP header */ + th = (struct tcphdr *)skb->data; + memset(th, 0, sizeof(struct tcphdr)); + th->syn = 1; + th->ack = 1; + if (inet_rsk(req)->ecn_ok) + th->ece = 1; + th->source = htons(ireq->ir_num); + th->dest = ireq->ir_rmt_port; + skb->ip_summed = CHECKSUM_PARTIAL; + th->seq = htonl(tcp_rsk(req)->snt_isn); + th->ack_seq = htonl(tcp_rsk(req)->rcv_nxt); + th->check = 0; + + th->window = htons(min(req->rsk_rcv_wnd, 65535U)); + + tcp_nip_options_write((__be32 *)(th + 1), NULL, &opts); + /* TCP data offset, divided by 4 because doff is a 32-bit word + * That is, words four bytes long are counted in units + */ + th->doff = (tcp_header_size >> 2); + __TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS); + + /* Fill in checksum */ + check = nip_get_output_checksum_tcp(skb, ireq->ir_nip_loc_addr, ireq->ir_nip_rmt_addr); + th->check = htons(check); + + /* Do not fool tcpdump (if any), clean our debris */ + skb->tstamp = 0; + return skb; +} + +/* Function + * Send SKB packets with SYN+ACK segments to the network layer. + * Parameter + * req: Request connection control block. + * skb: Transfer control block buffer. + */ +int __nip_send_synack(struct request_sock *req, struct sk_buff *skb) +{ + struct inet_request_sock *ireq = inet_rsk(req); /* 连接请求块 */ + int err = -EFAULT; + int csummode = CHECKSUM_NONE; + struct nip_addr *saddr, *daddr; + struct nip_hdr_encap head = {0}; + unsigned char hdr_buf[NIP_HDR_MAX]; /* Cache the newIP header */ + + skb->protocol = htons(ETH_P_NEWIP); + skb->ip_summed = csummode; + skb->csum = 0; + saddr = &ireq->ir_nip_loc_addr; + daddr = &ireq->ir_nip_rmt_addr; + + head.saddr = *saddr; + head.daddr = *daddr; + head.ttl = NIP_DEFAULT_TTL; + head.nexthdr = IPPROTO_TCP; + head.hdr_buf = hdr_buf; + nip_hdr_comm_encap(&head); + head.total_len = head.hdr_buf_pos + skb->len; + nip_update_total_len(&head, htons(head.total_len)); + + skb_push(skb, head.hdr_buf_pos); + memcpy(skb->data, head.hdr_buf, head.hdr_buf_pos); + skb_reset_network_header(skb); + NIPCB(skb)->srcaddr = *saddr; + NIPCB(skb)->dstaddr = *daddr; + NIPCB(skb)->nexthdr = head.nexthdr; + + head.total_len = skb->len; + err = nip_send_skb(skb); + if (err) + DEBUG("%s: failed to send skb, skb->len=%u", __func__, head.total_len); + else + DEBUG("%s: send skb ok, skb->len=%u", __func__, head.total_len); + + return err; +} + +int nip_send_synack(struct request_sock *req, struct sk_buff *skb) +{ + return __nip_send_synack(req, skb); +} + +/* Function: + * Creates a subtransport block to complete the establishment of the three-way handshake + * Parameter: + * parent: indicates the parent transmission control block + * child: indicates the child transmission control block + * skb: Transfer control block buffer + */ +int tcp_nip_child_process(struct sock *parent, struct sock *child, + struct sk_buff *skb) +{ + int ret = 0; + int state = child->sk_state; + /* Child is not occupied by the user process */ + if (!sock_owned_by_user(child)) { + ret = tcp_nip_rcv_state_process(child, skb); + /* At this point the state of the child has been migrated, + * waking up the process on the listening socket, + * which may be blocked due to Accept + */ + if (state == TCP_SYN_RECV && child->sk_state != state) + parent->sk_data_ready(parent); + } else { + __sk_add_backlog(child, skb); + } + bh_unlock_sock(child); + sock_put(child); + return ret; +} + +static inline __u32 tcp_nip_acceptable_seq(const struct sock *sk) +{ + const struct tcp_sock *tp = tcp_sk(sk); + + if (!before(tcp_wnd_end(tp), tp->snd_nxt)) + return tp->snd_nxt; + else + return tcp_wnd_end(tp); +} + +/* Function: + * The client sends an ACK + * Parameter: + * sk: transmission control block + * rcv_nxt: serial number to be accepted + */ +void __tcp_nip_send_ack(struct sock *sk, u32 rcv_nxt) +{ + struct sk_buff *buff; + + if (sk->sk_state == TCP_CLOSE) + return; + + buff = alloc_skb(MAX_TCP_HEADER, + sk_gfp_mask(sk, GFP_ATOMIC | __GFP_NOWARN)); + + /* Reserve space for the header. */ + skb_reserve(buff, MAX_TCP_HEADER); + /* Initialize SKB without data */ + tcp_nip_init_nondata_skb(buff, tcp_nip_acceptable_seq(sk), TCPHDR_ACK); + + /* Mark pure ack,skb->truesize set to 2 */ + skb_set_tcp_pure_ack(buff); + + /* Record the timestamp and send the SKB. */ + __tcp_nip_transmit_skb(sk, buff, 0, (__force gfp_t)0, rcv_nxt); +} + +void tcp_nip_send_ack(struct sock *sk) +{ + __tcp_nip_send_ack(sk, tcp_sk(sk)->rcv_nxt); +} + +void tcp_nip_send_fin(struct sock *sk) +{ + struct sk_buff *skb, *tskb = tcp_write_queue_tail(sk); + struct tcp_sock *tp = tcp_sk(sk); + u32 cur_mss; + + DEBUG("%s: send fin!\n", __func__); + /* Set the fin position of the last packet to 1 */ + if (tskb && tcp_nip_send_head(sk)) { +coalesce: + TCP_SKB_CB(tskb)->tcp_flags |= TCPHDR_FIN; + TCP_SKB_CB(tskb)->end_seq++; + tp->write_seq++; + } else { + skb = alloc_skb_fclone(MAX_TCP_HEADER, sk->sk_allocation); + if (unlikely(!skb)) { + if (tskb) + goto coalesce; + return; + } + skb_reserve(skb, MAX_TCP_HEADER); + + tcp_nip_init_nondata_skb(skb, tp->write_seq, + TCPHDR_ACK | TCPHDR_FIN); + tcp_nip_queue_skb(sk, skb); + } + + cur_mss = tcp_nip_current_mss(sk); // TCP_BASE_MSS + __tcp_nip_push_pending_frames(sk, cur_mss, TCP_NAGLE_OFF); +} + +void tcp_nip_send_active_reset(struct sock *sk, gfp_t priority) +{ + struct sk_buff *skb; + + DEBUG("%s: send RST!\n", __func__); + /* NOTE: No TCP options attached and we never retransmit this. */ + skb = alloc_skb(MAX_TCP_HEADER, priority); + if (!skb) { + DEBUG("%s: alloc_skb failed.\n", __func__); + return; + } + /* Reserve space for headers and prepare control bits. */ + skb_reserve(skb, MAX_TCP_HEADER); + tcp_nip_init_nondata_skb(skb, tcp_nip_acceptable_seq(sk), + TCPHDR_ACK | TCPHDR_RST); + /* Send it off. */ + tcp_nip_transmit_skb(sk, skb, 0, priority); +} + +static bool tcp_nip_snd_wnd_test(const struct tcp_sock *tp, + const struct sk_buff *skb, + unsigned int cur_mss) +{ + u32 end_seq = TCP_SKB_CB(skb)->end_seq; + + if (skb->len > cur_mss) + end_seq = TCP_SKB_CB(skb)->seq + cur_mss; + + return !after(end_seq, tcp_wnd_end(tp)); +} + +static void tcp_nip_set_skb_tso_segs(struct sk_buff *skb, unsigned int mss_now) +{ + if (skb->len <= mss_now || skb->ip_summed == CHECKSUM_NONE) { + /* Avoid the costly divide in the normal + * non-TSO case. + */ + tcp_skb_pcount_set(skb, 1); + TCP_SKB_CB(skb)->tcp_gso_size = 0; + } else { + tcp_skb_pcount_set(skb, DIV_ROUND_UP(skb->len, mss_now)); + TCP_SKB_CB(skb)->tcp_gso_size = mss_now; + } +} + +static int tcp_nip_init_tso_segs(struct sk_buff *skb, unsigned int mss_now) +{ + int tso_segs = tcp_skb_pcount(skb); + + if (!tso_segs || (tso_segs > 1 && tcp_skb_mss(skb) != mss_now)) { + tcp_nip_set_skb_tso_segs(skb, mss_now); + tso_segs = tcp_skb_pcount(skb); + } + return tso_segs; +} + +static bool tcp_nip_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle, + int push_one, gfp_t gfp) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct sk_buff *skb; + u32 snd_num = g_nip_tcp_snd_win_enable ? (tp->nip_ssthresh / mss_now) : 0xFFFFFFFF; + u32 last_nip_ssthresh = tp->nip_ssthresh; + + tcp_nip_keepalive_enable(sk); + tp->idle_ka_probes_out = 0; + + tcp_mstamp_refresh(tp); + + if (tp->rcv_tstamp) { + u32 tstamp = tcp_jiffies32 - tp->rcv_tstamp; + + if (tstamp >= g_ack_to_nxt_snd_tstamp) { + tp->nip_ssthresh = g_ssthresh_low_min; + snd_num = tp->nip_ssthresh / mss_now; + SSTHRESH_DBG("%s new snd tstamp %u >= %u, ssthresh %u to %u, snd_num=%u", + __func__, tstamp, g_ack_to_nxt_snd_tstamp, + last_nip_ssthresh, tp->nip_ssthresh, snd_num); + } + } + + while ((skb = tcp_nip_send_head(sk)) && (snd_num--)) { + DEBUG("%s:tcp_nip_send_head head found!\n", __func__); + tcp_nip_init_tso_segs(skb, mss_now); + if (unlikely(!tcp_nip_snd_wnd_test(tp, skb, mss_now))) + break; + + if (unlikely(tcp_nip_transmit_skb(sk, skb, 1, gfp))) + break; + + tcp_nip_event_new_data_sent(sk, skb); + + if (push_one) + break; + } + return !tp->packets_out && tcp_nip_send_head(sk); +} + +int tcp_nip_rtx_synack(const struct sock *sk, struct request_sock *req) +{ + const struct tcp_request_sock_ops *af_ops = tcp_rsk(req)->af_specific; + int res; + struct dst_entry *dst; + + dst = af_ops->route_req(sk, NULL, req); + tcp_rsk(req)->txhash = net_tx_rndhash(); + + res = af_ops->send_synack(sk, dst, NULL, req, NULL, TCP_SYNACK_NORMAL, + NULL); + + return res; +} + +static void tcp_nip_adjust_pcount(struct sock *sk, const struct sk_buff *skb, int decr) +{ + struct tcp_sock *tp = tcp_sk(sk); + + tp->packets_out -= decr; +} + +int __tcp_nip_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs) +{ + struct tcp_sock *tp = tcp_sk(sk); + unsigned int cur_mss; + int diff, len, err; + + if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) { + if (unlikely(before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))) { + WARN_ON_ONCE(1); + return -EINVAL; + } + if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq)) + return -ENOMEM; + } + + cur_mss = tcp_nip_current_mss(sk); // TCP_BASE_MSS + + if (!before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp)) && + TCP_SKB_CB(skb)->seq != tp->snd_una) + return -EAGAIN; + + len = cur_mss * segs; + if (skb->len > len) { + if (tcp_fragment(sk, TCP_FRAG_IN_WRITE_QUEUE, + skb, len, cur_mss, GFP_ATOMIC)) + return -ENOMEM; /* We'll try again later. */ + } else { + diff = tcp_skb_pcount(skb); + tcp_nip_set_skb_tso_segs(skb, cur_mss); + diff -= tcp_skb_pcount(skb); + if (diff) + tcp_nip_adjust_pcount(sk, skb, diff); + } + + err = tcp_nip_transmit_skb(sk, skb, 1, GFP_ATOMIC); + if (likely(!err)) { + segs = tcp_skb_pcount(skb); + + tp->total_retrans += segs; + } + return err; +} + +int tcp_nip_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs) +{ + struct tcp_sock *tp = tcp_sk(sk); + int err = __tcp_nip_retransmit_skb(sk, skb, segs); + + if (err == 0) { + TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS; + tp->retrans_out += tcp_skb_pcount(skb); + + /* Save stamp of the first retransmit. */ + if (!tp->retrans_stamp) + tp->retrans_stamp = tcp_skb_timestamp(skb); + } else if (err != -EBUSY) { + NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRETRANSFAIL); + } + + return err; +} + +#define TCP_NIP_DEFERRED_ALL ((1UL << TCP_TSQ_DEFERRED) | \ + (1UL << TCP_NIP_WRITE_TIMER_DEFERRED) | \ + (1UL << TCP_NIP_DELACK_TIMER_DEFERRED) | \ + (1UL << TCP_MTU_REDUCED_DEFERRED)) + +void tcp_nip_release_cb(struct sock *sk) +{ + unsigned long flags, nflags; + + /* perform an atomic operation only if at least one flag is set */ + do { + flags = sk->sk_tsq_flags; + if (!(flags & TCP_NIP_DEFERRED_ALL)) + return; + nflags = flags & ~TCP_NIP_DEFERRED_ALL; + } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags); + + sock_release_ownership(sk); + if (flags & (1UL << TCP_NIP_WRITE_TIMER_DEFERRED)) { + tcp_nip_write_timer_handler(sk); + __sock_put(sk); + } + if (flags & (1UL << TCP_NIP_DELACK_TIMER_DEFERRED)) { + tcp_nip_delack_timer_handler(sk); + __sock_put(sk); + } + if (flags & (1UL << TCP_MTU_REDUCED_DEFERRED)) { + inet_csk(sk)->icsk_af_ops->mtu_reduced(sk); + __sock_put(sk); + } +} + +static int tcp_nip_xmit_probe_skb(struct sock *sk, int urgent, int mib) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct sk_buff *skb; + + /* We don't queue it, tcp_transmit_skb() sets ownership. */ + skb = alloc_skb(MAX_TCP_HEADER, + sk_gfp_mask(sk, GFP_ATOMIC | __GFP_NOWARN)); + if (!skb) + return -1; + + /* Reserve space for headers and set control bits. */ + skb_reserve(skb, MAX_TCP_HEADER); + + tcp_nip_init_nondata_skb(skb, tp->snd_una - !urgent, TCPHDR_ACK); + + NET_INC_STATS(sock_net(sk), mib); + DEBUG("[nip]%s: send probe packet!\n", __func__); + return tcp_nip_transmit_skb(sk, skb, 0, (__force gfp_t)0); +} + +int tcp_nip_write_wakeup(struct sock *sk, int mib) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct sk_buff *skb; + + if (sk->sk_state == TCP_CLOSE) + return -1; + + skb = tcp_nip_send_head(sk); + /* If the serial number of the next packet is in the sending window */ + if (skb && before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp))) { + int err; + unsigned int mss = tcp_nip_current_mss(sk); + unsigned int seg_size = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; + + if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq)) + tp->pushed_seq = TCP_SKB_CB(skb)->end_seq; + /* If the current window size is not enough to send a complete packet */ + if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq) { + TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH; + err = tcp_fragment(sk, TCP_FRAG_IN_WRITE_QUEUE, + skb, seg_size, mss, GFP_ATOMIC); + if (err) { + DEBUG("[nip]:tcp_fragment return err = %d!\n", err); + return -1; + } + } + err = tcp_nip_transmit_skb(sk, skb, 1, GFP_ATOMIC); + if (!err) + tcp_nip_event_new_data_sent(sk, skb); + return err; + } else { + return tcp_nip_xmit_probe_skb(sk, 0, mib); + } +} + +/* The 0 window probe packet is sent */ +void tcp_nip_send_probe0(struct sock *sk) +{ + struct inet_connection_sock *icsk = inet_csk(sk); + struct tcp_sock *tp = tcp_sk(sk); + struct net *net = sock_net(sk); + unsigned long probe_max; + int err; + /* An ACK packet with snd_UNa-1 and length 0 is sent as a zero-window detection packet */ + err = tcp_nip_write_wakeup(sk, LINUX_MIB_TCPWINPROBE); + + /* If there are packets to be sent on the network and no packets to be + * sent in the send queue, the packet is returned directly + */ + if (tp->packets_out || !tcp_nip_send_head(sk)) { + /* Cancel probe timer, if it is not required. */ + icsk->icsk_probes_out = 0; + icsk->icsk_backoff = 0; + return; + } + + if (err <= 0) { + if (icsk->icsk_backoff < net->ipv4.sysctl_tcp_retries2) + icsk->icsk_backoff++; + icsk->icsk_probes_out++; /* Number of probes +1 */ + probe_max = TCP_RTO_MAX; + } else { + if (!icsk->icsk_probes_out) + icsk->icsk_probes_out = 1; + probe_max = TCP_RESOURCE_PROBE_INTERVAL; + } + inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0, + tcp_probe0_when(sk, probe_max), + TCP_RTO_MAX); +} -- Gitee