1
0
mirror of https://passt.top/passt synced 2024-12-22 05:35:23 +00:00

fwd: Rename port_fwd.[ch] and their contents

Currently port_fwd.[ch] contains helpers related to port forwarding,
particular automatic port forwarding.  We're planning to allow much more
flexible sorts of forwarding, including both port translation and NAT based
on the flow table.  This will subsume the existing port forwarding logic,
so rename port_fwd.[ch] to fwd.[ch] with matching updates to all the names
within.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2024-02-28 22:25:20 +11:00 committed by Stefano Brivio
parent 10376e7a2f
commit 3b9098aa49
9 changed files with 53 additions and 53 deletions

View File

@ -44,19 +44,19 @@ FLAGS += -DARCH=\"$(TARGET_ARCH)\"
FLAGS += -DVERSION=\"$(VERSION)\"
FLAGS += -DDUAL_STACK_SOCKETS=$(DUAL_STACK_SOCKETS)
PASST_SRCS = arch.c arp.c checksum.c conf.c dhcp.c dhcpv6.c flow.c icmp.c \
igmp.c inany.c iov.c isolation.c lineread.c log.c mld.c ndp.c \
netlink.c packet.c passt.c pasta.c pcap.c pif.c port_fwd.c tap.c tcp.c \
PASST_SRCS = arch.c arp.c checksum.c conf.c dhcp.c dhcpv6.c flow.c fwd.c \
icmp.c igmp.c inany.c iov.c isolation.c lineread.c log.c mld.c ndp.c \
netlink.c packet.c passt.c pasta.c pcap.c pif.c tap.c tcp.c \
tcp_splice.c udp.c util.c
QRAP_SRCS = qrap.c
SRCS = $(PASST_SRCS) $(QRAP_SRCS)
MANPAGES = passt.1 pasta.1 qrap.1
PASST_HEADERS = arch.h arp.h checksum.h conf.h dhcp.h dhcpv6.h flow.h \
PASST_HEADERS = arch.h arp.h checksum.h conf.h dhcp.h dhcpv6.h flow.h fwd.h \
flow_table.h icmp.h inany.h iov.h isolation.h lineread.h log.h ndp.h \
netlink.h packet.h passt.h pasta.h pcap.h pif.h port_fwd.h siphash.h \
tap.h tcp.h tcp_conn.h tcp_splice.h udp.h util.h
netlink.h packet.h passt.h pasta.h pcap.h pif.h siphash.h tap.h tcp.h \
tcp_conn.h tcp_splice.h udp.h util.h
HEADERS = $(PASST_HEADERS) seccomp.h
C := \#include <linux/tcp.h>\nstruct tcp_info x = { .tcpi_snd_wnd = 0 };

8
conf.c
View File

@ -109,10 +109,10 @@ static int parse_port_range(const char *s, char **endptr,
* @c: Execution context
* @optname: Short option name, t, T, u, or U
* @optarg: Option argument (port specification)
* @fwd: Pointer to @port_fwd to be updated
* @fwd: Pointer to @fwd_ports to be updated
*/
static void conf_ports(const struct ctx *c, char optname, const char *optarg,
struct port_fwd *fwd)
struct fwd_ports *fwd)
{
char addr_buf[sizeof(struct in6_addr)] = { 0 }, *addr = addr_buf;
char buf[BUFSIZ], *spec, *ifname = NULL, *p;
@ -1158,7 +1158,7 @@ void conf(struct ctx *c, int argc, char **argv)
};
char userns[PATH_MAX] = { 0 }, netns[PATH_MAX] = { 0 };
bool copy_addrs_opt = false, copy_routes_opt = false;
enum port_fwd_mode fwd_default = FWD_NONE;
enum fwd_ports_mode fwd_default = FWD_NONE;
bool v4_only = false, v6_only = false;
struct in6_addr *dns6 = c->ip6.dns;
struct fqdn *dnss = c->dns_search;
@ -1746,7 +1746,7 @@ void conf(struct ctx *c, int argc, char **argv)
if (!c->udp.fwd_out.f.mode)
c->udp.fwd_out.f.mode = fwd_default;
port_fwd_init(c);
fwd_scan_ports_init(c);
if (!c->quiet)
conf_print(c);

View File

@ -6,7 +6,7 @@
* PASTA - Pack A Subtle Tap Abstraction
* for network namespace/tap device mode
*
* port_fwd.c - Port forwarding helpers
* fwd.c - Port forwarding helpers
*
* Copyright Red Hat
* Author: Stefano Brivio <sbrivio@redhat.com>
@ -21,7 +21,7 @@
#include <stdio.h>
#include "util.h"
#include "port_fwd.h"
#include "fwd.h"
#include "passt.h"
#include "lineread.h"
@ -73,11 +73,11 @@ static void procfs_scan_listen(int fd, unsigned int lstate,
}
/**
* port_fwd_scan_tcp() - Scan /proc to update TCP forwarding map
* fwd_scan_ports_tcp() - Scan /proc to update TCP forwarding map
* @fwd: Forwarding information to update
* @rev: Forwarding information for the reverse direction
*/
void port_fwd_scan_tcp(struct port_fwd *fwd, const struct port_fwd *rev)
void fwd_scan_ports_tcp(struct fwd_ports *fwd, const struct fwd_ports *rev)
{
memset(fwd->map, 0, PORT_BITMAP_SIZE);
procfs_scan_listen(fwd->scan4, TCP_LISTEN, fwd->map, rev->map);
@ -85,15 +85,15 @@ void port_fwd_scan_tcp(struct port_fwd *fwd, const struct port_fwd *rev)
}
/**
* port_fwd_scan_udp() - Scan /proc to update UDP forwarding map
* fwd_scan_ports_udp() - Scan /proc to update UDP forwarding map
* @fwd: Forwarding information to update
* @rev: Forwarding information for the reverse direction
* @tcp_fwd: Corresponding TCP forwarding information
* @tcp_rev: TCP forwarding information for the reverse direction
*/
void port_fwd_scan_udp(struct port_fwd *fwd, const struct port_fwd *rev,
const struct port_fwd *tcp_fwd,
const struct port_fwd *tcp_rev)
void fwd_scan_ports_udp(struct fwd_ports *fwd, const struct fwd_ports *rev,
const struct fwd_ports *tcp_fwd,
const struct fwd_ports *tcp_rev)
{
uint8_t exclude[PORT_BITMAP_SIZE];
@ -118,10 +118,10 @@ void port_fwd_scan_udp(struct port_fwd *fwd, const struct port_fwd *rev,
}
/**
* port_fwd_init() - Initial setup for port forwarding
* fwd_scan_ports_init() - Initial setup for automatic port forwarding
* @c: Execution context
*/
void port_fwd_init(struct ctx *c)
void fwd_scan_ports_init(struct ctx *c)
{
const int flags = O_RDONLY | O_CLOEXEC;
@ -133,23 +133,23 @@ void port_fwd_init(struct ctx *c)
if (c->tcp.fwd_in.mode == FWD_AUTO) {
c->tcp.fwd_in.scan4 = open_in_ns(c, "/proc/net/tcp", flags);
c->tcp.fwd_in.scan6 = open_in_ns(c, "/proc/net/tcp6", flags);
port_fwd_scan_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out);
fwd_scan_ports_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out);
}
if (c->udp.fwd_in.f.mode == FWD_AUTO) {
c->udp.fwd_in.f.scan4 = open_in_ns(c, "/proc/net/udp", flags);
c->udp.fwd_in.f.scan6 = open_in_ns(c, "/proc/net/udp6", flags);
port_fwd_scan_udp(&c->udp.fwd_in.f, &c->udp.fwd_out.f,
&c->tcp.fwd_in, &c->tcp.fwd_out);
fwd_scan_ports_udp(&c->udp.fwd_in.f, &c->udp.fwd_out.f,
&c->tcp.fwd_in, &c->tcp.fwd_out);
}
if (c->tcp.fwd_out.mode == FWD_AUTO) {
c->tcp.fwd_out.scan4 = open("/proc/net/tcp", flags);
c->tcp.fwd_out.scan6 = open("/proc/net/tcp6", flags);
port_fwd_scan_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in);
fwd_scan_ports_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in);
}
if (c->udp.fwd_out.f.mode == FWD_AUTO) {
c->udp.fwd_out.f.scan4 = open("/proc/net/udp", flags);
c->udp.fwd_out.f.scan6 = open("/proc/net/udp6", flags);
port_fwd_scan_udp(&c->udp.fwd_out.f, &c->udp.fwd_in.f,
&c->tcp.fwd_out, &c->tcp.fwd_in);
fwd_scan_ports_udp(&c->udp.fwd_out.f, &c->udp.fwd_in.f,
&c->tcp.fwd_out, &c->tcp.fwd_in);
}
}

View File

@ -4,13 +4,13 @@
* Author: David Gibson <david@gibson.dropbear.id.au>
*/
#ifndef PORT_FWD_H
#define PORT_FWD_H
#ifndef FWD_H
#define FWD_H
/* Number of ports for both TCP and UDP */
#define NUM_PORTS (1U << 16)
enum port_fwd_mode {
enum fwd_ports_mode {
FWD_SPEC = 1,
FWD_NONE,
FWD_AUTO,
@ -20,25 +20,25 @@ enum port_fwd_mode {
#define PORT_BITMAP_SIZE DIV_ROUND_UP(NUM_PORTS, 8)
/**
* port_fwd - Describes port forwarding for one protocol and direction
* fwd_ports - Describes port forwarding for one protocol and direction
* @mode: Overall forwarding mode (all, none, auto, specific ports)
* @scan4: /proc/net fd to scan for IPv4 ports when in AUTO mode
* @scan6: /proc/net fd to scan for IPv6 ports when in AUTO mode
* @map: Bitmap describing which ports are forwarded
* @delta: Offset between the original destination and mapped port number
*/
struct port_fwd {
enum port_fwd_mode mode;
struct fwd_ports {
enum fwd_ports_mode mode;
int scan4;
int scan6;
uint8_t map[PORT_BITMAP_SIZE];
in_port_t delta[NUM_PORTS];
};
void port_fwd_scan_tcp(struct port_fwd *fwd, const struct port_fwd *rev);
void port_fwd_scan_udp(struct port_fwd *fwd, const struct port_fwd *rev,
const struct port_fwd *tcp_fwd,
const struct port_fwd *tcp_rev);
void port_fwd_init(struct ctx *c);
void fwd_scan_ports_tcp(struct fwd_ports *fwd, const struct fwd_ports *rev);
void fwd_scan_ports_udp(struct fwd_ports *fwd, const struct fwd_ports *rev,
const struct fwd_ports *tcp_fwd,
const struct fwd_ports *tcp_rev);
void fwd_scan_ports_init(struct ctx *c);
#endif /* PORT_FWD_H */
#endif /* FWD_H */

View File

@ -39,7 +39,7 @@ union epoll_ref;
#include "packet.h"
#include "flow.h"
#include "icmp.h"
#include "port_fwd.h"
#include "fwd.h"
#include "tcp.h"
#include "udp.h"

4
tcp.c
View File

@ -3237,12 +3237,12 @@ void tcp_timer(struct ctx *c, const struct timespec *now)
if (c->mode == MODE_PASTA) {
if (c->tcp.fwd_out.mode == FWD_AUTO) {
port_fwd_scan_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in);
fwd_scan_ports_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in);
NS_CALL(tcp_port_rebind_outbound, c);
}
if (c->tcp.fwd_in.mode == FWD_AUTO) {
port_fwd_scan_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out);
fwd_scan_ports_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out);
tcp_port_rebind(c, false);
}
}

4
tcp.h
View File

@ -59,8 +59,8 @@ union tcp_listen_epoll_ref {
* @pipe_size: Size of pipes for spliced connections
*/
struct tcp_ctx {
struct port_fwd fwd_in;
struct port_fwd fwd_out;
struct fwd_ports fwd_in;
struct fwd_ports fwd_out;
struct timespec timer_run;
#ifdef HAS_SND_WND
int kernel_snd_wnd;

10
udp.c
View File

@ -259,7 +259,7 @@ void udp_portmap_clear(void)
* udp_invert_portmap() - Compute reverse port translations for return packets
* @fwd: Port forwarding configuration to compute reverse map for
*/
static void udp_invert_portmap(struct udp_port_fwd *fwd)
static void udp_invert_portmap(struct udp_fwd_ports *fwd)
{
unsigned int i;
@ -1203,14 +1203,14 @@ void udp_timer(struct ctx *c, const struct timespec *now)
if (c->mode == MODE_PASTA) {
if (c->udp.fwd_out.f.mode == FWD_AUTO) {
port_fwd_scan_udp(&c->udp.fwd_out.f, &c->udp.fwd_in.f,
&c->tcp.fwd_out, &c->tcp.fwd_in);
fwd_scan_ports_udp(&c->udp.fwd_out.f, &c->udp.fwd_in.f,
&c->tcp.fwd_out, &c->tcp.fwd_in);
NS_CALL(udp_port_rebind_outbound, c);
}
if (c->udp.fwd_in.f.mode == FWD_AUTO) {
port_fwd_scan_udp(&c->udp.fwd_in.f, &c->udp.fwd_out.f,
&c->tcp.fwd_in, &c->tcp.fwd_out);
fwd_scan_ports_udp(&c->udp.fwd_in.f, &c->udp.fwd_out.f,
&c->tcp.fwd_in, &c->tcp.fwd_out);
udp_port_rebind(c, false);
}
}

10
udp.h
View File

@ -43,12 +43,12 @@ union udp_epoll_ref {
/**
* udp_port_fwd - UDP specific port forwarding configuration
* udp_fwd_ports - UDP specific port forwarding configuration
* @f: Generic forwarding configuration
* @rdelta: Reversed delta map to translate source ports on return packets
*/
struct udp_port_fwd {
struct port_fwd f;
struct udp_fwd_ports {
struct fwd_ports f;
in_port_t rdelta[NUM_PORTS];
};
@ -59,8 +59,8 @@ struct udp_port_fwd {
* @timer_run: Timestamp of most recent timer run
*/
struct udp_ctx {
struct udp_port_fwd fwd_in;
struct udp_port_fwd fwd_out;
struct udp_fwd_ports fwd_in;
struct udp_fwd_ports fwd_out;
struct timespec timer_run;
};