Skip to content

Commit cf1109e

Browse files
committed
Version 1.4.0
1 parent e4ee5cf commit cf1109e

21 files changed

Lines changed: 954 additions & 355 deletions

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tuninetd changelog
2+
--------
3+
**1.4.0**
4+
* protocol decoder for IPv4, IPv6, Ethernet, 802.1Q and combinations (README section 5)
5+
* tun/tap listener is a separate daemon now, named *tuntapd* (README section 6)
6+
* *pcap* sensor not hard-coupled with tun/tap interface anymore, could be used on any
7+
* *nflog* and *pcap* sensors can work simultaneously

README.md

100755100644
Lines changed: 83 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,132 @@
11
# tuninetd
22

3-
Simple yet powerful event emitter by **tun/tap** (with/without **pcap** filter) or **nflog** source.
3+
Network event emitter with **pcap** and **nflog** sensors.
44

5-
Could be used as: VPN dispatcher, simplified detection system, by demand service handler, tricky lock etc...
5+
Could be used as VPN dispatcher, by demand service handler, remote launcher etc...
66

7-
### 1. How it works:
8-
#### 1.1. tun/tap + pcap mode:
9-
You should create and configure tun/tap device first, then run **tuninetd**. It starts listening on this interface, until network traffic will be detected. After that, interface immediately releasing and specified command (with -c) will execute. From now on, daemon in monitoring state.
107

11-
---
12-
>For example:
8+
### 1. How it works
9+
10+
There are two events which **tuninetd** emits. "start", when network activity is detected, and "stop" if sensors no receive packets for certain amount of time. Both of events processed by external executable written on language whatever you like.
11+
12+
#### 1.1. pcap sensor
13+
You should configure network device first, then run **tuninetd** with **-i** flag and **-f** for filter (optional). It starts listening on the interface, until network traffic will be detected. After that command defined with **-c** will be executed.
14+
1315
```sh
14-
# tuninetd -i tun0 -c /path/to/launcher
16+
# tuninetd -i tun0 -f "! host 1.2.3.4" -c /path/to/launcher -t 3600
1517
```
1618
>then "start" command from **tuninetd** will be:
1719
```sh
1820
# /path/to/launcher start > /dev/null 2>&1
1921
```
20-
>"stop" command in the same manner.
21-
---
2222

23-
After -t seconds of interface idle (no packets through), tuninetd send "stop" command by path that defined with -c, and start listening interface by itself again.
23+
After **-t** seconds of idle (no packets), **tuninetd** runs "stop" command and wait for activity again to start process over.
2424

25-
Since **tuninetd** based on **libpcap**, you can specify capture filter. To test pcap rules might use tcpdump which is based on the same library.
25+
Since **tuninetd** based on **libpcap**, it's a good idea to play with filters using **tcpdump** first, which is based on the same library.
2626

27-
>**! Notice !** *Modern Linux distributions periodically send 'icmpv6 router solicitation' packets, which cause tuninetd keep or change state. This situation appears in tun/tap mode without pcap filter applied.*
27+
>**! Notice !** *Modern Linux distributions periodically send 'icmpv6 router solicitation' packets and other broadcast messages, which force tuninetd keep or change its state. So, using filters highly recommended to prevent unexpected behavior even on **tun** devices*
2828
29-
#### 1.2. nflog mode:
29+
#### 1.2. nflog sensor
3030

31-
In general, behavior the same as tun/tap in part of start/stop. You could simply use netfilter nfgroup (*iptables **NFLOG** target*) to reading packets from. No binding to tun/tap device nor libpcap sensor. This is more lightweight mode and, because of that, - more reliable.
31+
In general, behavior the same as pcap in terms of start/stop events. You could simply use netfilter nfgroup (*iptables **NFLOG** target*) to capture packets from, and "filter" already in nflog rule(s). No binding to certain network interface required. This is preferable mode since straightforward, lightweight and flexible
32+
33+
```sh
34+
# tuninetd -n 1 -c /path/to/launcher
35+
```
36+
#### 1.3. pcap + nflog
37+
You could use both sensors at the same time. In this case, event will be triggered from first sensor which receive a network packets. And yes, both sensors should be idle for **-t** seconds, before "stop" event fired
38+
```sh
39+
# tuninetd -i enp3s0 -f "arp and host 4.3.2.1" -n 1 -c /path/to/executable/toggletunnel.sh
40+
```
3241

3342
### 2. Installation:
34-
If you're using Debian/Ubuntu please check deb-packages folder. Choose appropriate architecture, then run following command with root privileges:
43+
If you're using Debian/Ubuntu, please check deb-packages folder. Run following with root privileges:
3544
```sh
3645
# dpkg -i tuninetd_ver_arch.deb
3746
# apt-get -f install
3847
```
39-
To install from sources download src folder. In case Debian/Ubuntu, you should also install **build-essential**, **libpcap-dev** and **libnetfilter-log-dev** packages first. To build tuninetd just run:<br/>
48+
To install from sources, please download src folder. In case Debian/Ubuntu, don't forget to install **build-essential**, **libpcap-dev** and **libnetfilter-log-dev** packages first.<br/>
4049
```sh
4150
# cd /download/folder/src
4251
# make
4352
```
4453

45-
Congrats! Tuninend ready to use. Check ./bin folder.
54+
Congrats! Tuninend ready to use, check ./bin folder.
4655

47-
### 3. Usage:
48-
#### 3.1. Launch:
56+
### 3. Usage
57+
#### 3.1. Launch
4958

50-
```sh
51-
# tuninetd {-i <ifname> | -n <nflog-group>} -c <path> [-m <iftype>] [-f <filter>] [-t <ttl>] [-d]
59+
```
60+
tuninetd -i <ifname> -c <path> [-f <filter>] [-n <nflog-group>] [-t <ttl>] [-d]
61+
tuninetd -n <nflog-group> -c <path> [-i <ifname> [-f <filter>]] [-t <ttl>] [-d]
62+
63+
-i <ifname>: network interface to use with pcap. Must be up and configured.
64+
-c <path>: to executable, will be run with 'start' and 'stop' parameter accordingly.
65+
-n <nflog-group>: netfilter nflog group number (0 - 65535)
66+
-f <filter>: specify pcap filter if needed, similar to tcpdump. Default none (all packets)
67+
-t <ttl>: seconds of interface idle before 'stop' command will be run. Default 600.
68+
-d: daemonize process. Check for errors before use.
69+
70+
-h: print help
71+
-v: print version
5272
```
5373

54-
**-i \<ifname>**: interface to use (tun or tap). Must be up and configured.<br/>
55-
**-n \<nflog-group>**: iptables NFLOG group ('-i', '-m' and '-f' will be ignored).<br/>
56-
**-c \<path>**: will execute with 'start' and 'stop' parameter.<br/>
57-
**-m \<iftype>**: 'tun' or 'tap' mode. By default 'tun', should be set properly.<br/>
58-
**-f \<filter>**: specify pcap filter, similar to tcpdump<br/>
59-
**-t \<ttl>**: seconds since last packet before 'stop' command (default is 600).<br/>
60-
**-d**: daemonize process<br/>
61-
**-h**: print this help
62-
63-
---
64-
65-
#### 3.2. Signals:
66-
SIGHUP (-1): switch tuninetd to standby mode (deadlock resolving)<br/>
67-
SIGUSR1 (-10): write to syslog current state (debug information)
68-
69-
74+
#### 3.2. Signals
75+
SIGHUP (-1): don't wait ttl, jump to "stop" event right now<br/>
76+
SIGUSR1 (-10): write to syslog current configuration and state
7077

71-
### 4. Examples:
78+
### 4. Examples
7279
Before launching as a daemon make sure there is no errors. In daemon mode tuninetd write status messages and errors to syslog.
7380

7481
```sh
75-
# tuninetd -i tun0 -c /test/runtunnel.sh -f "! host 1.2.3.4" -t 3600 -d
76-
# tuninetd -n 2 -c /test/runtunnel.sh -t 3600 -d
82+
# tuninetd -n 1 -c /path/to/executable/toggletunnel.sh
83+
# tuninetd -i tap0 -c /path/to/executable/toggleservice.sh
84+
# tuninetd -i tun0 -f "! host 1.2.3.4" -c /path/to/executable/somebinary -t 3600 -d
85+
# tuninetd -i enp3s0 -f "arp and host 4.3.2.1" -n 1 -c /path/to/executable/run.py
7786
```
7887

79-
Check ```example``` folder to find some shell scripts.
88+
Check ```example``` folder to find some scripts.
8089

81-
To create and bring up ```tun``` device could be used following commands:
82-
```sh
83-
# ip tuntap add dev tun0 mode tun
84-
# ip link set tun0 up
85-
```
90+
### 5. Logging
8691

87-
For more information about routing and configuring network devices, I strongly suggest LARTC how-to.
92+
Here some syslog example with brief packet info which caused "start" event:
93+
```
94+
Nov 1 21:32:14 router1 tuninetd: Success! Tuninetd has been started with PID: 23686
95+
Nov 1 21:32:14 router1 tuninetd: Binding to interface enp3s0
96+
Nov 1 21:32:14 router1 tuninetd: Start listening nflog-group 1
97+
Nov 1 21:32:14 router1 tuninetd: NFLOG: adjust nfnl_rcvbufsiz to 300000
98+
Nov 1 21:48:34 router1 tuninetd: NFLOG: executing START command...
99+
Nov 1 21:48:35 router1 tuninetd: |- IPv4 192.168.1.1 > 13.107.42.14, NXT_HDR: 0x06 (TCP)
100+
Nov 1 21:48:35 router1 tuninetd: |- MAC: 1b:1c:0d:45:a9:e1, DevIndex: 2
101+
Nov 1 22:08:59 router1 tuninetd: CORE: executing STOP command...
102+
Nov 1 22:36:07 router1 tuninetd: PCAP: executing START command...
103+
Nov 1 22:36:08 router1 tuninetd: |- IPv6 fe80::f66d:4ff:fe64:1124 > ff02::2, NXT_HDR: 0x3A (IPv6-ICMP)
104+
Nov 1 22:36:08 router1 tuninetd: |- MAC: f4:6d:04:64:11:24 > 33:33:00:00:00:02, 802.1Q VID: 3, EtherType: 0x86DD (IPv6)
88105
89-
<br/>
106+
```
90107

91-
---
92108
```sh
93109
# killall -10 tuninetd
94110
```
95-
syslog:<br/>
96111

97-
>Oct 20 18:42:20 router1 tuninetd: SIGUSR1 caught:<br/>
98-
>Oct 20 18:42:20 router1 tuninetd: - Capture engine: nflog group 1<br/>
99-
>Oct 20 18:42:20 router1 tuninetd: - cmd_path = /etc/tuninetd/toggletunnel.sh<br/>
100-
>Oct 20 18:42:20 router1 tuninetd: - TTL = 600 sec.<br/>
101-
>Oct 20 18:42:20 router1 tuninetd: - Current status: up (ON), time since last captured packet: 2 sec.
102-
---
112+
```
113+
Nov 1 22:42:17 router1 tuninetd: SIGUSR1 caught:
114+
Nov 1 22:42:17 router1 tuninetd: - Capture engine: pcap, enp3s0
115+
Nov 1 22:42:17 router1 tuninetd: -- Pcap filter: "ip6"
116+
Nov 1 22:42:17 router1 tuninetd: - Capture engine: nflog group 1
117+
Nov 1 22:42:17 router1 tuninetd: - cmd_path = /etc/tuninetd/toggletunnel.sh
118+
Nov 1 22:42:17 router1 tuninetd: - TTL = 600 sec.
119+
Nov 1 22:42:17 router1 tuninetd: - Current status: up (ON), time since last captured packet: 127 sec.
120+
121+
```
122+
123+
### 6. Tuntapd
124+
I've found **tuntapd** in this package, what this for?
103125

126+
Well, if you're about to use tun/tap device with pcap sensor, you need some program binded to the interface, or pcap can't capture any packets. In some cases, network services release tun/tap when shutting down. Tuntapd could be used to keep device alive for pcap. Start tuntapd from your executable by "stop" event handler, after desired service go down and vise-versa.
104127

105-
### License:
128+
### License
106129
MIT
107-
### Author:
130+
### Author
108131
Paul aka root4root \<root4root at gmail dot com><br/>
109-
**Any comment/suggestions are welcomed.**
132+
**Any comments and suggestions are welcomed.**
12.5 KB
Binary file not shown.

src/Makefile

100755100644
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1+
COMPILER = gcc
12
CFLAGS = -Wall -O2
23

3-
all: tuninetd
4+
all: tuninetd tuntapd
45

5-
tuninetd: main.o xnflog.o xpcap.o thread.o xtun.o utils.o
6+
tuninetd: tuninetd.o xnflog.o xpcap.o thread.o logger.o net.o
67
[ -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 -lnfnetlink
8+
$(COMPILER) tuninetd.o xnflog.o xpcap.o thread.o logger.o net.o -o ./bin/tuninetd -lpthread -lpcap -lnetfilter_log -lnfnetlink
89

9-
main.o: main.c main.h common.h
10-
gcc $(CFLAGS) -c main.c
10+
tuninetd.o: tuninetd.c tuninetd.h common.h
11+
$(COMPILER) $(CFLAGS) -c tuninetd.c
1112

1213
xnflog.o: xnflog.c common.h
13-
gcc $(CFLAGS) -c xnflog.c
14+
$(COMPILER) $(CFLAGS) -c xnflog.c
1415

1516
xpcap.o: xpcap.c common.h
16-
gcc $(CFLAGS) -c xpcap.c
17+
$(COMPILER) $(CFLAGS) -c xpcap.c
1718

1819
thread.o: thread.c common.h
19-
gcc $(CFLAGS) -c thread.c
20+
$(COMPILER) $(CFLAGS) -c thread.c
2021

21-
xtun.o: xtun.c common.h
22-
gcc $(CFLAGS) -c xtun.c
22+
logger.o: logger.c logger.h
23+
$(COMPILER) $(CFLAGS) -c logger.c
2324

24-
utils.o: utils.c common.h
25-
gcc $(CFLAGS) -c utils.c
25+
net.o: net.c net.h
26+
$(COMPILER) $(CFLAGS) -c net.c
27+
28+
tuntapd: tuntapd.o logger.o net.o
29+
$(COMPILER) tuntapd.o logger.o net.o -o ./bin/tuntapd
30+
31+
tuntapd.o: tuntapd.c tuntapd.h
32+
$(COMPILER) $(CFLAGS) -c tuntapd.c
2633

2734
clean:
2835
rm -f *.o

src/common.h

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
1-
#ifndef H_TUNINETD_COMMON
2-
#define H_TUNINETD_COMMON
1+
#ifndef COMMON_H_
2+
#define COMMON_H_
33

4-
#include <fcntl.h>
54
#include <stdio.h>
65
#include <stdlib.h>
76
#include <stdint.h>
87
#include <string.h>
9-
#include <sys/ioctl.h>
10-
#include <net/if.h>
11-
#include <linux/if_tun.h>
128
#include <unistd.h>
139
#include <time.h>
10+
#include <stdatomic.h>
11+
12+
#include "logger.h"
1413

1514
#define ON 1
1615
#define OFF 0
1716

18-
#define ERROR 0
19-
#define WARNING 1
20-
#define INFO 2
21-
22-
#define VERSION "\ntuninetd 1.3.1\n"
23-
24-
//global vars.
25-
extern short int debug;
17+
//glob vars--
2618
extern short int status;
27-
extern unsigned long ts;
19+
extern atomic_ulong ts; // @suppress("Type cannot be resolved")
2820
extern unsigned long curts;
2921

3022
extern struct globcfg_t {
@@ -38,27 +30,11 @@ extern struct globcfg_t {
3830
long nf_group;
3931
int dev_mode;
4032
long ttl;
41-
} globcfg;
42-
43-
44-
//from utils.c
45-
void do_debug(const char *msg, ...);
46-
void message(int, const char *msg, ...);
47-
48-
void sighup_handler(int);
49-
void sigusr_handler(int);
50-
void sigterm_handler(int);
51-
void usage();
52-
void version();
53-
54-
//from thread.c
55-
void switch_guard(short action);
56-
void thread_init();
57-
58-
void *tun_x(void *x_void_ptr);
59-
void *nflog_x(void *x_void_ptr);
60-
void *pcap_x(void *x_void_ptr);
33+
} globcfg; //--glob vars
6134

62-
void xnflog_stop();
35+
//from thread.c--
36+
void switch_guard(short action); //Used: tuninetd.c, xnflog.c, xpcap.c
37+
void thread_init(); //Used: tuninetd.c
38+
//--from thread.c
6339

6440
#endif

src/logger.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdarg.h>
2+
#include <syslog.h>
3+
4+
#include "common.h"
5+
6+
#define DEBUG_MODE 0
7+
8+
void do_debug(const char *msg, ...)
9+
{
10+
if (DEBUG_MODE) {
11+
va_list argp;
12+
va_start(argp, msg);
13+
vfprintf(stderr, msg, argp);
14+
va_end(argp);
15+
}
16+
}
17+
18+
void message(int mylogpriority, const char *msg, ...)
19+
{
20+
int syslogpriority;
21+
22+
if (mylogpriority == ERROR) {
23+
syslogpriority = LOG_ERR;
24+
} else if (mylogpriority == WARNING) {
25+
syslogpriority = LOG_WARNING;
26+
} else {
27+
syslogpriority = LOG_INFO;
28+
}
29+
30+
va_list argp;
31+
va_start(argp, msg);
32+
33+
if (globcfg.isdaemon == 0) {
34+
vfprintf(stderr, msg, argp);
35+
vfprintf(stderr, "\n", NULL);
36+
} else {
37+
openlog("tuninetd", 0, LOG_USER);
38+
vsyslog(syslogpriority, msg, argp);
39+
closelog();
40+
}
41+
42+
va_end(argp);
43+
}

src/logger.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef LOGGER_H_
2+
#define LOGGER_H_
3+
4+
#define ERROR 0
5+
#define WARNING 1
6+
#define INFO 2
7+
8+
void do_debug(const char *, ...);
9+
void message(int, const char *, ...);
10+
11+
12+
#endif /* LOGGER_H_ */

src/main.h

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)