Skip to content

Commit a3116ac

Browse files
kkovaacsdavem330
authored andcommitted
tcp: Port redirection support for TCP
Current TCP code relies on the local port of the listening socket being the same as the destination address of the incoming connection. Port redirection used by many transparent proxying techniques obviously breaks this, so we have to store the original destination port address. This patch extends struct inet_request_sock and stores the incoming destination port value there. It also modifies the handshake code to use that value as the source port when sending reply packets. Signed-off-by: KOVACS Krisztian <hidden@sch.bme.hu> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 86b08d8 commit a3116ac

5 files changed

Lines changed: 6 additions & 2 deletions

File tree

‎include/net/inet_sock.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ struct inet_request_sock {
6161
struct request_sock req;
6262
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6363
u16 inet6_rsk_offset;
64-
/* 2 bytes hole, try to pack */
6564
#endif
65+
__be16 loc_port;
6666
__be32 loc_addr;
6767
__be32 rmt_addr;
6868
__be16 rmt_port;

‎include/net/tcp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,7 @@ static inline void tcp_openreq_init(struct request_sock *req,
976976
ireq->acked = 0;
977977
ireq->ecn_ok = 0;
978978
ireq->rmt_port = tcp_hdr(skb)->source;
979+
ireq->loc_port = tcp_hdr(skb)->dest;
979980
}
980981

981982
extern void tcp_enter_memory_pressure(struct sock *sk);

‎net/ipv4/inet_connection_sock.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ struct sock *inet_csk_clone(struct sock *sk, const struct request_sock *req,
516516
newicsk->icsk_bind_hash = NULL;
517517

518518
inet_sk(newsk)->dport = inet_rsk(req)->rmt_port;
519+
inet_sk(newsk)->num = ntohs(inet_rsk(req)->loc_port);
520+
inet_sk(newsk)->sport = inet_rsk(req)->loc_port;
519521
newsk->sk_write_space = sk_stream_write_space;
520522

521523
newicsk->icsk_retransmits = 0;

‎net/ipv4/syncookies.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
297297
treq->rcv_isn = ntohl(th->seq) - 1;
298298
treq->snt_isn = cookie;
299299
req->mss = mss;
300+
ireq->loc_port = th->dest;
300301
ireq->rmt_port = th->source;
301302
ireq->loc_addr = ip_hdr(skb)->daddr;
302303
ireq->rmt_addr = ip_hdr(skb)->saddr;

‎net/ipv4/tcp_output.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2275,7 +2275,7 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
22752275
th->syn = 1;
22762276
th->ack = 1;
22772277
TCP_ECN_make_synack(req, th);
2278-
th->source = inet_sk(sk)->sport;
2278+
th->source = ireq->loc_port;
22792279
th->dest = ireq->rmt_port;
22802280
/* Setting of flags are superfluous here for callers (and ECE is
22812281
* not even correctly set)

0 commit comments

Comments
 (0)