aboutgitcodebugslistschat
path: root/tcp.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2022-09-24 19:08:17 +1000
committerStefano Brivio <sbrivio@redhat.com>2022-09-24 14:48:35 +0200
commit163dc5f18899808e97b92ddae0314928c903bb4b (patch)
treed2e8984fe9f1b77fbdf5e878173a666c7a66f801 /tcp.c
parent1128fa03fe73092dc68f30d0550283f0897d1f89 (diff)
downloadpasst-163dc5f18899808e97b92ddae0314928c903bb4b.tar
passt-163dc5f18899808e97b92ddae0314928c903bb4b.tar.gz
passt-163dc5f18899808e97b92ddae0314928c903bb4b.tar.bz2
passt-163dc5f18899808e97b92ddae0314928c903bb4b.tar.lz
passt-163dc5f18899808e97b92ddae0314928c903bb4b.tar.xz
passt-163dc5f18899808e97b92ddae0314928c903bb4b.tar.zst
passt-163dc5f18899808e97b92ddae0314928c903bb4b.zip
Consolidate port forwarding configuration into a common structure
The configuration for how to forward ports in and out of the guest/ns is divided between several different variables. For each connect direction and protocol we have a mode in the udp/tcp context structure, a bitmap of which ports to forward also in the context structure and an array of deltas to apply if the outward facing and inward facing port numbers are different. This last is a separate global variable, rather than being in the context structure, for no particular reason. UDP also requires an additional array which has the reverse mapping used for return packets. Consolidate these into a re-used substructure in the context structure. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Diffstat (limited to 'tcp.c')
-rw-r--r--tcp.c42
1 files changed, 19 insertions, 23 deletions
diff --git a/tcp.c b/tcp.c
index 0bdef7f..e44177f 100644
--- a/tcp.c
+++ b/tcp.c
@@ -546,10 +546,6 @@ static const char *tcp_flag_str[] __attribute((__unused__)) = {
"ACK_TO_TAP_DUE", "ACK_FROM_TAP_DUE",
};
-/* Port re-mappings as delta, indexed by original destination port */
-static in_port_t tcp_port_delta_to_tap [USHRT_MAX];
-static in_port_t tcp_port_delta_to_init [USHRT_MAX];
-
/* Listening sockets, used for automatic port forwarding in pasta mode only */
static int tcp_sock_init_lo [USHRT_MAX][IP_VERSIONS];
static int tcp_sock_init_ext [USHRT_MAX][IP_VERSIONS];
@@ -954,22 +950,24 @@ static void conn_event_do(const struct ctx *c, struct tcp_conn *conn,
/**
* tcp_remap_to_tap() - Set delta for port translation toward guest/tap
+ * @c: Execution context
* @port: Original destination port, host order
* @delta: Delta to be added to original destination port
*/
-void tcp_remap_to_tap(in_port_t port, in_port_t delta)
+void tcp_remap_to_tap(struct ctx *c, in_port_t port, in_port_t delta)
{
- tcp_port_delta_to_tap[port] = delta;
+ c->tcp.fwd_in.delta[port] = delta;
}
/**
* tcp_remap_to_tap() - Set delta for port translation toward init namespace
+ * @c: Execution context
* @port: Original destination port, host order
* @delta: Delta to be added to original destination port
*/
-void tcp_remap_to_init(in_port_t port, in_port_t delta)
+void tcp_remap_to_init(struct ctx *c, in_port_t port, in_port_t delta)
{
- tcp_port_delta_to_init[port] = delta;
+ c->tcp.fwd_out.delta[port] = delta;
}
/**
@@ -3109,11 +3107,9 @@ void tcp_sock_init(const struct ctx *c, int ns, sa_family_t af,
int s;
if (ns) {
- tref.tcp.index = (in_port_t)(port +
- tcp_port_delta_to_init[port]);
+ tref.tcp.index = (in_port_t)(port + c->tcp.fwd_out.delta[port]);
} else {
- tref.tcp.index = (in_port_t)(port +
- tcp_port_delta_to_tap[port]);
+ tref.tcp.index = (in_port_t)(port + c->tcp.fwd_in.delta[port]);
}
if (af == AF_INET || af == AF_UNSPEC) {
@@ -3133,7 +3129,7 @@ void tcp_sock_init(const struct ctx *c, int ns, sa_family_t af,
else
s = -1;
- if (c->tcp.fwd_mode_in == FWD_AUTO)
+ if (c->tcp.fwd_in.mode == FWD_AUTO)
tcp_sock_init_ext[port][V4] = s;
}
@@ -3148,7 +3144,7 @@ void tcp_sock_init(const struct ctx *c, int ns, sa_family_t af,
else
s = -1;
- if (c->tcp.fwd_mode_out == FWD_AUTO) {
+ if (c->tcp.fwd_out.mode == FWD_AUTO) {
if (ns)
tcp_sock_ns[port][V4] = s;
else
@@ -3174,7 +3170,7 @@ void tcp_sock_init(const struct ctx *c, int ns, sa_family_t af,
else
s = -1;
- if (c->tcp.fwd_mode_in == FWD_AUTO)
+ if (c->tcp.fwd_in.mode == FWD_AUTO)
tcp_sock_init_ext[port][V6] = s;
}
@@ -3189,7 +3185,7 @@ void tcp_sock_init(const struct ctx *c, int ns, sa_family_t af,
else
s = -1;
- if (c->tcp.fwd_mode_out == FWD_AUTO) {
+ if (c->tcp.fwd_out.mode == FWD_AUTO) {
if (ns)
tcp_sock_ns[port][V6] = s;
else
@@ -3213,7 +3209,7 @@ static int tcp_sock_init_ns(void *arg)
ns_enter(c);
for (port = 0; port < USHRT_MAX; port++) {
- if (!bitmap_isset(c->tcp.port_to_init, port))
+ if (!bitmap_isset(c->tcp.fwd_out.map, port))
continue;
tcp_sock_init(c, 1, AF_UNSPEC, NULL, port);
@@ -3413,7 +3409,7 @@ static int tcp_port_rebind(void *arg)
ns_enter(a->c);
for (port = 0; port < USHRT_MAX; port++) {
- if (!bitmap_isset(a->c->tcp.port_to_init, port)) {
+ if (!bitmap_isset(a->c->tcp.fwd_out.map, port)) {
if (tcp_sock_ns[port][V4] >= 0) {
close(tcp_sock_ns[port][V4]);
tcp_sock_ns[port][V4] = -1;
@@ -3428,7 +3424,7 @@ static int tcp_port_rebind(void *arg)
}
/* Don't loop back our own ports */
- if (bitmap_isset(a->c->tcp.port_to_tap, port))
+ if (bitmap_isset(a->c->tcp.fwd_in.map, port))
continue;
if ((a->c->ifi4 && tcp_sock_ns[port][V4] == -1) ||
@@ -3437,7 +3433,7 @@ static int tcp_port_rebind(void *arg)
}
} else {
for (port = 0; port < USHRT_MAX; port++) {
- if (!bitmap_isset(a->c->tcp.port_to_tap, port)) {
+ if (!bitmap_isset(a->c->tcp.fwd_in.map, port)) {
if (tcp_sock_init_ext[port][V4] >= 0) {
close(tcp_sock_init_ext[port][V4]);
tcp_sock_init_ext[port][V4] = -1;
@@ -3461,7 +3457,7 @@ static int tcp_port_rebind(void *arg)
}
/* Don't loop back our own ports */
- if (bitmap_isset(a->c->tcp.port_to_init, port))
+ if (bitmap_isset(a->c->tcp.fwd_out.map, port))
continue;
if ((a->c->ifi4 && tcp_sock_init_ext[port][V4] == -1) ||
@@ -3489,14 +3485,14 @@ void tcp_timer(struct ctx *c, const struct timespec *ts)
struct tcp_port_detect_arg detect_arg = { c, 0 };
struct tcp_port_rebind_arg rebind_arg = { c, 0 };
- if (c->tcp.fwd_mode_in == FWD_AUTO) {
+ if (c->tcp.fwd_in.mode == FWD_AUTO) {
detect_arg.detect_in_ns = 0;
tcp_port_detect(&detect_arg);
rebind_arg.bind_in_ns = 1;
NS_CALL(tcp_port_rebind, &rebind_arg);
}
- if (c->tcp.fwd_mode_out == FWD_AUTO) {
+ if (c->tcp.fwd_out.mode == FWD_AUTO) {
detect_arg.detect_in_ns = 1;
NS_CALL(tcp_port_detect, &detect_arg);
rebind_arg.bind_in_ns = 0;