Skip to content

Commit f246ca5

Browse files
ZeyadYasseravagin
authored andcommitted
criu: rename iptables network locking/unlocking functions
Related to the new --network-lock option, other methods for network locking/unlocking will be added as an alternative to iptables like nftables. This option is used in the core network locking/unlocking hooks to decide which method should be used, making it easier to add new methods later smoothly. i.e. - network_lock_internal - network_unlock_internal - lock_connection (renamed from nf_lock_connection) - unlock_connection (renamed from nf_unlock_connection) - unlock_connection_info (renamed from unlock_connection_info) nf_* functions are renamed to iptables_* to avoid confusion with other netfilter methods in the future like nftables. v2: run make indent v3: make error messages more descriptive Signed-off-by: Zeyad Yasser <zeyady98@gmail.com>
1 parent e9d24a2 commit f246ca5

4 files changed

Lines changed: 84 additions & 38 deletions

File tree

criu/include/netfilter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
#define __CR_NETFILTER_H__
33

44
struct inet_sk_desc;
5-
extern int nf_lock_connection(struct inet_sk_desc *);
6-
extern int nf_unlock_connection(struct inet_sk_desc *);
5+
extern int iptables_lock_connection(struct inet_sk_desc *);
6+
extern int iptables_unlock_connection(struct inet_sk_desc *);
77

88
struct inet_sk_info;
9-
extern int nf_unlock_connection_info(struct inet_sk_info *);
9+
extern int iptables_unlock_connection_info(struct inet_sk_info *);
1010

1111
extern void preload_netfilter_modules(void);
1212

criu/net.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3014,7 +3014,7 @@ static int iptables_restore(bool ipv6, char *buf, int size)
30143014
return ret;
30153015
}
30163016

3017-
int network_lock_internal(void)
3017+
static int iptables_network_lock_internal(void)
30183018
{
30193019
char conf[] = "*filter\n"
30203020
":CRIU - [0:0]\n"
@@ -3023,10 +3023,7 @@ int network_lock_internal(void)
30233023
"-A CRIU -m mark --mark " __stringify(SOCCR_MARK) " -j ACCEPT\n"
30243024
"-A CRIU -j DROP\n"
30253025
"COMMIT\n";
3026-
int ret = 0, nsret;
3027-
3028-
if (switch_ns(root_item->pid->real, &net_ns_desc, &nsret))
3029-
return -1;
3026+
int ret = 0;
30303027

30313028
ret |= iptables_restore(false, conf, sizeof(conf) - 1);
30323029
if (kdat.ipv6)
@@ -3039,28 +3036,51 @@ int network_lock_internal(void)
30393036
"option.\n",
30403037
ret);
30413038

3039+
return ret;
3040+
}
3041+
3042+
int network_lock_internal(void)
3043+
{
3044+
int ret = 0, nsret;
3045+
3046+
if (switch_ns(root_item->pid->real, &net_ns_desc, &nsret))
3047+
return -1;
3048+
3049+
if (opts.network_lock_method == NETWORK_LOCK_IPTABLES)
3050+
ret = iptables_network_lock_internal();
3051+
30423052
if (restore_ns(nsret, &net_ns_desc))
30433053
ret = -1;
30443054

30453055
return ret;
30463056
}
30473057

3048-
static int network_unlock_internal(void)
3058+
static int iptables_network_unlock_internal(void)
30493059
{
30503060
char conf[] = "*filter\n"
30513061
":CRIU - [0:0]\n"
30523062
"-D INPUT -j CRIU\n"
30533063
"-D OUTPUT -j CRIU\n"
30543064
"-X CRIU\n"
30553065
"COMMIT\n";
3066+
int ret = 0;
3067+
3068+
ret |= iptables_restore(false, conf, sizeof(conf) - 1);
3069+
if (kdat.ipv6)
3070+
ret |= iptables_restore(true, conf, sizeof(conf) - 1);
3071+
3072+
return ret;
3073+
}
3074+
3075+
static int network_unlock_internal(void)
3076+
{
30563077
int ret = 0, nsret;
30573078

30583079
if (switch_ns(root_item->pid->real, &net_ns_desc, &nsret))
30593080
return -1;
30603081

3061-
ret |= iptables_restore(false, conf, sizeof(conf) - 1);
3062-
if (kdat.ipv6)
3063-
ret |= iptables_restore(true, conf, sizeof(conf) - 1);
3082+
if (opts.network_lock_method == NETWORK_LOCK_IPTABLES)
3083+
ret = iptables_network_unlock_internal();
30643084

30653085
if (restore_ns(nsret, &net_ns_desc))
30663086
ret = -1;

criu/netfilter.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static char buf[512];
2222
* Any brave soul to write it using xtables-devel?
2323
*/
2424

25-
#define NF_CONN_CMD \
25+
#define IPTABLES_CONN_CMD \
2626
"%s %s -t filter %s %s --protocol tcp " \
2727
"-m mark ! --mark " __stringify(SOCCR_MARK) " --source %s --sport %d --destination %s --dport %d -j DROP"
2828

@@ -52,8 +52,8 @@ static int ipv6_addr_mapped(u32 *addr)
5252
return (addr[2] == htonl(0x0000ffff));
5353
}
5454

55-
static int nf_connection_switch_raw(int family, u32 *src_addr, u16 src_port, u32 *dst_addr, u16 dst_port, bool input,
56-
bool lock)
55+
static int iptables_connection_switch_raw(int family, u32 *src_addr, u16 src_port, u32 *dst_addr, u16 dst_port,
56+
bool input, bool lock)
5757
{
5858
char sip[INET_ADDR_LEN], dip[INET_ADDR_LEN];
5959
char *cmd;
@@ -84,7 +84,7 @@ static int nf_connection_switch_raw(int family, u32 *src_addr, u16 src_port, u32
8484
return -1;
8585
}
8686

87-
snprintf(buf, sizeof(buf), NF_CONN_CMD, cmd, kdat.has_xtlocks ? "-w" : "", lock ? "-I" : "-D",
87+
snprintf(buf, sizeof(buf), IPTABLES_CONN_CMD, cmd, kdat.has_xtlocks ? "-w" : "", lock ? "-I" : "-D",
8888
input ? "INPUT" : "OUTPUT", dip, (int)dst_port, sip, (int)src_port);
8989

9090
pr_debug("\tRunning iptables [%s]\n", buf);
@@ -103,41 +103,41 @@ static int nf_connection_switch_raw(int family, u32 *src_addr, u16 src_port, u32
103103
return 0;
104104
}
105105

106-
static int nf_connection_switch(struct inet_sk_desc *sk, bool lock)
106+
static int iptables_connection_switch(struct inet_sk_desc *sk, bool lock)
107107
{
108108
int ret = 0;
109109

110-
ret = nf_connection_switch_raw(sk->sd.family, sk->src_addr, sk->src_port, sk->dst_addr, sk->dst_port, true,
111-
lock);
110+
ret = iptables_connection_switch_raw(sk->sd.family, sk->src_addr, sk->src_port, sk->dst_addr, sk->dst_port,
111+
true, lock);
112112
if (ret)
113113
return -1;
114114

115-
ret = nf_connection_switch_raw(sk->sd.family, sk->dst_addr, sk->dst_port, sk->src_addr, sk->src_port, false,
116-
lock);
115+
ret = iptables_connection_switch_raw(sk->sd.family, sk->dst_addr, sk->dst_port, sk->src_addr, sk->src_port,
116+
false, lock);
117117
if (ret) /* rollback */
118-
nf_connection_switch_raw(sk->sd.family, sk->src_addr, sk->src_port, sk->dst_addr, sk->dst_port, true,
119-
!lock);
118+
iptables_connection_switch_raw(sk->sd.family, sk->src_addr, sk->src_port, sk->dst_addr, sk->dst_port,
119+
true, !lock);
120120
return ret;
121121
}
122122

123-
int nf_lock_connection(struct inet_sk_desc *sk)
123+
int iptables_lock_connection(struct inet_sk_desc *sk)
124124
{
125-
return nf_connection_switch(sk, true);
125+
return iptables_connection_switch(sk, true);
126126
}
127127

128-
int nf_unlock_connection(struct inet_sk_desc *sk)
128+
int iptables_unlock_connection(struct inet_sk_desc *sk)
129129
{
130-
return nf_connection_switch(sk, false);
130+
return iptables_connection_switch(sk, false);
131131
}
132132

133-
int nf_unlock_connection_info(struct inet_sk_info *si)
133+
int iptables_unlock_connection_info(struct inet_sk_info *si)
134134
{
135135
int ret = 0;
136136

137-
ret |= nf_connection_switch_raw(si->ie->family, si->ie->src_addr, si->ie->src_port, si->ie->dst_addr,
138-
si->ie->dst_port, true, false);
139-
ret |= nf_connection_switch_raw(si->ie->family, si->ie->dst_addr, si->ie->dst_port, si->ie->src_addr,
140-
si->ie->src_port, false, false);
137+
ret |= iptables_connection_switch_raw(si->ie->family, si->ie->src_addr, si->ie->src_port, si->ie->dst_addr,
138+
si->ie->dst_port, true, false);
139+
ret |= iptables_connection_switch_raw(si->ie->family, si->ie->dst_addr, si->ie->dst_port, si->ie->src_addr,
140+
si->ie->src_port, false, false);
141141
/*
142142
* rollback nothing in case of any error,
143143
* because nobody checks errors of this function

criu/sk-tcp.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@
3333
static LIST_HEAD(cpt_tcp_repair_sockets);
3434
static LIST_HEAD(rst_tcp_repair_sockets);
3535

36+
static int lock_connection(struct inet_sk_desc *sk)
37+
{
38+
if (opts.network_lock_method == NETWORK_LOCK_IPTABLES)
39+
return iptables_lock_connection(sk);
40+
41+
return -1;
42+
}
43+
44+
static int unlock_connection(struct inet_sk_desc *sk)
45+
{
46+
if (opts.network_lock_method == NETWORK_LOCK_IPTABLES)
47+
return iptables_unlock_connection(sk);
48+
49+
return -1;
50+
}
51+
3652
static int tcp_repair_established(int fd, struct inet_sk_desc *sk)
3753
{
3854
int ret;
@@ -51,9 +67,11 @@ static int tcp_repair_established(int fd, struct inet_sk_desc *sk)
5167
}
5268

5369
if (!(root_ns_mask & CLONE_NEWNET)) {
54-
ret = nf_lock_connection(sk);
55-
if (ret < 0)
70+
ret = lock_connection(sk);
71+
if (ret < 0) {
72+
pr_err("Failed to lock TCP connection %x\n", sk->sd.ino);
5673
goto err2;
74+
}
5775
}
5876

5977
socr = libsoccr_pause(sk->rfd);
@@ -66,7 +84,7 @@ static int tcp_repair_established(int fd, struct inet_sk_desc *sk)
6684

6785
err3:
6886
if (!(root_ns_mask & CLONE_NEWNET))
69-
nf_unlock_connection(sk);
87+
unlock_connection(sk);
7088
err2:
7189
close(sk->rfd);
7290
err1:
@@ -80,9 +98,9 @@ static void tcp_unlock_one(struct inet_sk_desc *sk)
8098
list_del(&sk->rlist);
8199

82100
if (!(root_ns_mask & CLONE_NEWNET)) {
83-
ret = nf_unlock_connection(sk);
101+
ret = unlock_connection(sk);
84102
if (ret < 0)
85-
pr_perror("Failed to unlock TCP connection");
103+
pr_err("Failed to unlock TCP connection %x\n", sk->sd.ino);
86104
}
87105

88106
libsoccr_resume(sk->priv);
@@ -453,6 +471,14 @@ void tcp_locked_conn_add(struct inet_sk_info *ii)
453471
ii->sk_fd = -1;
454472
}
455473

474+
static int unlock_connection_info(struct inet_sk_info *si)
475+
{
476+
if (opts.network_lock_method == NETWORK_LOCK_IPTABLES)
477+
return iptables_unlock_connection_info(si);
478+
479+
return -1;
480+
}
481+
456482
void rst_unlock_tcp_connections(void)
457483
{
458484
struct inet_sk_info *ii;
@@ -465,5 +491,5 @@ void rst_unlock_tcp_connections(void)
465491
return;
466492

467493
list_for_each_entry(ii, &rst_tcp_repair_sockets, rlist)
468-
nf_unlock_connection_info(ii);
494+
unlock_connection_info(ii);
469495
}

0 commit comments

Comments
 (0)