Skip to content

Commit 7f3b6a0

Browse files
committed
Bug fix with unbind and freeing handles. Prevention lock of nflog resources in kernel.
1 parent 9c553f2 commit 7f3b6a0

File tree

7 files changed

+88
-33
lines changed

7 files changed

+88
-33
lines changed
8.32 KB
Binary file not shown.

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ all: tuninetd
44

55
tuninetd: main.o xnflog.o xpcap.o thread.o xtun.o utils.o
66
[ -d ./bin ] || mkdir -p ./bin
7-
gcc main.o xnflog.o xpcap.o thread.o xtun.o utils.o -o ./bin/tuninetd -lpthread -lpcap -lnetfilter_log
7+
gcc main.o xnflog.o xpcap.o thread.o xtun.o utils.o -o ./bin/tuninetd -lpthread -lpcap -lnetfilter_log -lnfnetlink
88

99
main.o: main.c main.h common.h
1010
gcc $(CFLAGS) -c main.c

src/common.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
#include <unistd.h>
1212
#include <time.h>
1313

14-
#define BUFSIZE 2000
1514
#define ON 1
1615
#define OFF 0
1716

1817
#define ERROR 0
1918
#define WARNING 1
2019
#define INFO 2
2120

22-
#define VERSION "\ntuninetd 1.3.0\n"
21+
#define VERSION "\ntuninetd 1.3.1\n"
2322

2423
//global vars.
2524
short int debug;
@@ -45,8 +44,9 @@ struct globcfg_t {
4544
void do_debug(const char *msg, ...);
4645
void message(int, const char *msg, ...);
4746

48-
void sighup_handler(int signo);
49-
void sigusr_handler(int signo);
47+
void sighup_handler(int);
48+
void sigusr_handler(int);
49+
void sigterm_handler(int);
5050
void usage();
5151
void version();
5252

@@ -58,4 +58,6 @@ void *tun_x(void *x_void_ptr);
5858
void *nflog_x(void *x_void_ptr);
5959
void *pcap_x(void *x_void_ptr);
6060

61+
void xnflog_stop();
62+
6163
#endif

src/main.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ int main(int argc, char *argv[])
1414
tim.tv_nsec = 0;
1515

1616
//debug = 1;
17-
18-
if (signal(SIGHUP, sighup_handler) == SIG_ERR) {
19-
message(WARNING, "Warning! Can't catch SIGHUP");
20-
}
21-
22-
if (signal(SIGUSR1, sigusr_handler) == SIG_ERR) {
23-
message(WARNING, "Warning! Can't catch SIGUSR1");
24-
}
17+
18+
signal(SIGTERM, sigterm_handler);
19+
signal(SIGHUP, sighup_handler);
20+
signal(SIGUSR1, sigusr_handler);
21+
signal(SIGINT, sigterm_handler);
2522

2623
while (1) {
2724

src/utils.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,13 @@ void sigusr_handler(int signo)
101101
message(INFO, "- Current status: up (ON), time since last captured packet: %ld sec.", delta < 0 ? 0 : delta);
102102
}
103103
}
104+
105+
void sigterm_handler(int signo)
106+
{
107+
if (globcfg.nf_group >= 0) {
108+
xnflog_stop();
109+
}
110+
111+
exit(0);
112+
113+
}

src/xnflog.c

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
#include "common.h"
2+
#include <libnfnetlink/libnfnetlink.h>
23
#include <libnetfilter_log/libnetfilter_log.h>
34
#include <errno.h>
45

6+
#define BUFSIZE 65536
7+
#define NFNLBUFSIZ 150000
8+
9+
static char *buf;
10+
static struct nflog_handle *h;
11+
static struct nflog_g_handle *qh;
12+
static int fd;
13+
14+
15+
static void setnlbufsiz(unsigned int size, struct nflog_handle *h)
16+
{
17+
//This function returns new buffer size
18+
message(INFO, "NFLOG: adjust nfnl_rcvbufsiz to %u", nfnl_rcvbufsiz(nflog_nfnlh(h), size));
19+
}
20+
521
static int callback(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg, struct nflog_data *ldata, void *data)
622
{
723
if (status == OFF) {
8-
message(INFO, "NFLOG module: executing START command...");
24+
message(INFO, "NFLOG: executing START command...");
925
switch_guard(ON);
1026
}
1127

@@ -14,54 +30,82 @@ static int callback(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg, struct nf
1430
return 0;
1531
}
1632

17-
void *nflog_x(void *x_void_ptr)
33+
34+
void xnflog_start()
1835
{
19-
struct nflog_handle *h;
20-
struct nflog_g_handle *qh;
21-
ssize_t rv;
22-
char buf[4096];
23-
int fd;
36+
buf = calloc(BUFSIZE, sizeof(char));
2437

2538
h = nflog_open();
2639

40+
short int pf_available = 3; //AF_INET, AF_INET6, AF_BRIDGE
41+
2742
if (!h) {
28-
message(ERROR, "Error during nflog_open(). Abort.");
43+
message(ERROR, "NFLOG: error during nflog_open(). Abort.");
2944
exit(1);
3045
}
3146

32-
if (nflog_unbind_pf(h, AF_INET) < 0) {
33-
message(ERROR, "Error nflog_unbind_pf(). Abort.");
34-
exit(1);
35-
}
47+
setnlbufsiz(NFNLBUFSIZ, h);
3648

37-
if (nflog_bind_pf(h, AF_INET) < 0) {
38-
message(ERROR, "Error during nflog_bind_pf(). Abort");
49+
pf_available -= nflog_bind_pf(h, AF_INET) < 0 ? 1 : 0;
50+
pf_available -= nflog_bind_pf(h, AF_INET6) < 0 ? 1 : 0;
51+
pf_available -= nflog_bind_pf(h, AF_BRIDGE) < 0 ? 1 : 0;
52+
53+
if (pf_available == 0) {
54+
message(ERROR, "NFLOG: can't bind to any protocol family (IPv4, IPv6 or BRIDGE)");
3955
exit(1);
4056
}
4157

4258
qh = nflog_bind_group(h, globcfg.nf_group);
4359

4460
if (!qh) {
45-
message(ERROR, "No handle for group %i, can't bind. Abort.", globcfg.nf_group);
61+
message(ERROR, "NFLOG: no handle for group %i, can't bind, errno: %i. Abort.", globcfg.nf_group, errno);
4662
exit(1);
4763
}
4864

4965
if (nflog_set_mode(qh, NFULNL_COPY_PACKET, 0xffff) < 0) {
50-
message(ERROR, "Can't set NFULNL_COPY_PACKET mode. Abort.");
66+
message(ERROR, "NFLOG: can't set NFULNL_COPY_PACKET mode. Abort.");
5167
exit(1);
5268
}
5369

5470
nflog_callback_register(qh, &callback, NULL);
5571

5672
fd = nflog_fd(h);
73+
74+
}
5775

58-
while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) {
59-
nflog_handle_packet(h, buf, rv);
76+
void xnflog_stop()
77+
{
78+
message(INFO, "NFLOG: Shutting down...");
79+
shutdown(fd, SHUT_RD);
80+
nflog_unbind_group(qh);
81+
nflog_unbind_pf(h, AF_INET);
82+
nflog_unbind_pf(h, AF_INET6);
83+
nflog_unbind_pf(h, AF_BRIDGE);
84+
nflog_close(h);
85+
free(buf);
86+
}
87+
88+
89+
void *nflog_x(void *x_void_ptr)
90+
{
91+
xnflog_start();
92+
93+
ssize_t rv;
94+
95+
while ((rv = recv(fd, (void *)buf, BUFSIZE, 0))) {
96+
97+
if (rv >= 0) {
98+
nflog_handle_packet(h, buf, rv);
99+
} else if (errno == ENOBUFS) {
100+
message(WARNING, "NFLOG: warning! No enough nfnlbufsiz...");
101+
} else {
102+
break;
103+
}
60104
}
61105

62-
message(WARNING, "NFLOG module shut down with code %i. Check errno.h for details.", errno);
106+
message(WARNING, "NFLOG: shut down with code %i. Check errno.h for details.", errno);
63107

64-
nflog_close(h);
108+
xnflog_stop();
65109

66110
return 0;
67111
}

src/xtun.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "common.h"
22

3+
#define BUFSIZE 2000
4+
35
static int tun_alloc(char *dev, int flags)
46
{
57
struct ifreq ifr;

0 commit comments

Comments
 (0)