Skip to content

Commit c389e68

Browse files
feat: Add options to switch IPv4/IPv6 support
1 parent 3ee0a2d commit c389e68

File tree

5 files changed

+69
-25
lines changed

5 files changed

+69
-25
lines changed

include/globvar.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ struct fh_context {
2828
int sockfd;
2929
FILE *logfp;
3030

31+
/* -4 */ int use_ipv4;
32+
/* -6 */ int use_ipv6;
3133
/* -d */ int daemon;
3234
/* -f */ int skipfw;
3335
/* -h */ const char *hostname;

src/globvar.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ struct fh_context g_ctx = {.exit = 0,
2727
.sockfd = -1,
2828
.logfp = NULL,
2929

30+
/* -4 */ .use_ipv4 = 0,
31+
/* -6 */ .use_ipv6 = 0,
3032
/* -d */ .daemon = 0,
3133
/* -f */ .skipfw = 0,
3234
/* -h */ .hostname = NULL,

src/mainfun.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ static void print_usage(const char *name)
5050
"Usage: %s [options]\n"
5151
"\n"
5252
"Options:\n"
53+
" -4 enable IPv4\n"
54+
" -6 enable IPv6\n"
5355
" -d run as a daemon\n"
5456
" -f skip firewall rules\n"
5557
" -h <hostname> hostname for obfuscation (required)\n"
@@ -74,15 +76,24 @@ int main(int argc, char *argv[])
7476
{
7577
unsigned long long tmp;
7678
int res, opt, exitcode;
79+
char *ipproto_info;
7780

7881
if (!argc || !argv[0]) {
7982
return EXIT_FAILURE;
8083
}
8184

8285
exitcode = EXIT_FAILURE;
8386

84-
while ((opt = getopt(argc, argv, "dfh:i:km:n:r:st:w:x:z")) != -1) {
87+
while ((opt = getopt(argc, argv, "46dfh:i:km:n:r:st:w:x:z")) != -1) {
8588
switch (opt) {
89+
case '4':
90+
g_ctx.use_ipv4 = 1;
91+
break;
92+
93+
case '6':
94+
g_ctx.use_ipv6 = 1;
95+
break;
96+
8697
case 'd':
8798
g_ctx.daemon = 1;
8899
break;
@@ -200,6 +211,10 @@ int main(int argc, char *argv[])
200211
return res < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
201212
}
202213

214+
if (!g_ctx.use_ipv4 && !g_ctx.use_ipv6) {
215+
g_ctx.use_ipv4 = g_ctx.use_ipv6 = 1;
216+
}
217+
203218
if (!g_ctx.fwmask) {
204219
g_ctx.fwmask = g_ctx.fwmark;
205220
} else if ((g_ctx.fwmark & g_ctx.fwmask) != g_ctx.fwmark) {
@@ -278,8 +293,15 @@ int main(int argc, char *argv[])
278293
EE("WARNING: setpriority(): %s", strerror(errno));
279294
}
280295

281-
E("listening on %s, netfilter queue number %" PRIu32 "...", g_ctx.iface,
282-
g_ctx.nfqnum);
296+
if (g_ctx.use_ipv4 && !g_ctx.use_ipv6) {
297+
ipproto_info = " (IPv4 only)";
298+
} else if (!g_ctx.use_ipv4 && g_ctx.use_ipv6) {
299+
ipproto_info = " (IPv6 only)";
300+
} else {
301+
ipproto_info = "";
302+
}
303+
E("listening on %s%s, netfilter queue number %" PRIu32 "...", g_ctx.iface,
304+
ipproto_info, g_ctx.nfqnum);
283305

284306
/*
285307
Main Loop

src/nfqueue.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,14 @@ static int callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
210210
}
211211

212212
ethertype = ntohs(ph->hw_protocol);
213-
if (ethertype == ETHERTYPE_IP) {
213+
if (g_ctx.use_ipv4 && ethertype == ETHERTYPE_IP) {
214214
res = fh_pkt4_parse(pkt_data, pkt_len, saddr, daddr, &tcph,
215215
&tcp_payload_len);
216216
if (res < 0) {
217217
EE(T(fh_pkt4_parse));
218218
goto ret_accept;
219219
}
220-
} else if (ethertype == ETHERTYPE_IPV6) {
220+
} else if (g_ctx.use_ipv6 && ethertype == ETHERTYPE_IPV6) {
221221
res = fh_pkt6_parse(pkt_data, pkt_len, saddr, daddr, &tcph,
222222
&tcp_payload_len);
223223
if (res < 0) {

src/nfrules.c

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,36 @@ int fh_nfrules_setup(void)
5454
}
5555

5656
if (g_ctx.use_iptables) {
57-
res = fh_ipt4_setup();
58-
if (res < 0) {
59-
E(T(fh_ipt4_setup));
60-
return -1;
57+
if (g_ctx.use_ipv4) {
58+
res = fh_ipt4_setup();
59+
if (res < 0) {
60+
E(T(fh_ipt4_setup));
61+
return -1;
62+
}
6163
}
6264

63-
res = fh_ipt6_setup();
64-
if (res < 0) {
65-
E(T(fh_ipt6_setup));
66-
return -1;
65+
if (g_ctx.use_ipv6) {
66+
res = fh_ipt6_setup();
67+
if (res < 0) {
68+
E(T(fh_ipt6_setup));
69+
return -1;
70+
}
6771
}
6872
} else {
69-
res = fh_nft4_setup();
70-
if (res < 0) {
71-
E(T(fh_nft4_setup));
72-
return -1;
73+
if (g_ctx.use_ipv4) {
74+
res = fh_nft4_setup();
75+
if (res < 0) {
76+
E(T(fh_nft4_setup));
77+
return -1;
78+
}
7379
}
7480

75-
res = fh_nft6_setup();
76-
if (res < 0) {
77-
E(T(fh_nft6_setup));
78-
return -1;
81+
if (g_ctx.use_ipv6) {
82+
res = fh_nft6_setup();
83+
if (res < 0) {
84+
E(T(fh_nft6_setup));
85+
return -1;
86+
}
7987
}
8088
}
8189

@@ -90,10 +98,20 @@ void fh_nfrules_cleanup(void)
9098
}
9199

92100
if (g_ctx.use_iptables) {
93-
fh_ipt4_cleanup();
94-
fh_ipt6_cleanup();
101+
if (g_ctx.use_ipv4) {
102+
fh_ipt4_cleanup();
103+
}
104+
105+
if (g_ctx.use_ipv6) {
106+
fh_ipt6_cleanup();
107+
}
95108
} else {
96-
fh_nft4_cleanup();
97-
fh_nft6_cleanup();
109+
if (g_ctx.use_ipv4) {
110+
fh_nft4_cleanup();
111+
}
112+
113+
if (g_ctx.use_ipv6) {
114+
fh_nft6_cleanup();
115+
}
98116
}
99117
}

0 commit comments

Comments
 (0)