diff options
author | 2022-10-25 14:34:17 -0400 | |
---|---|---|
committer | 2022-10-25 14:34:17 -0400 | |
commit | c90e2998f6ddb225efba3538f3e06b8a79f2c023 (patch) | |
tree | 3161a24ada741da5a00b78b04c6749d5e9c86d06 /src/fnettrace-icmp | |
parent | fix nolocal netfilter (diff) | |
download | firejail-c90e2998f6ddb225efba3538f3e06b8a79f2c023.tar.gz firejail-c90e2998f6ddb225efba3538f3e06b8a79f2c023.tar.zst firejail-c90e2998f6ddb225efba3538f3e06b8a79f2c023.zip |
nettrace: various fixes
Diffstat (limited to 'src/fnettrace-icmp')
-rw-r--r-- | src/fnettrace-icmp/main.c | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/src/fnettrace-icmp/main.c b/src/fnettrace-icmp/main.c index 47d61a326..e1e5daa48 100644 --- a/src/fnettrace-icmp/main.c +++ b/src/fnettrace-icmp/main.c | |||
@@ -22,6 +22,8 @@ | |||
22 | #include <time.h> | 22 | #include <time.h> |
23 | #include <linux/filter.h> | 23 | #include <linux/filter.h> |
24 | #include <linux/if_ether.h> | 24 | #include <linux/if_ether.h> |
25 | #include <sys/prctl.h> | ||
26 | #include <signal.h> | ||
25 | #define MAX_BUF_SIZE (64 * 1024) | 27 | #define MAX_BUF_SIZE (64 * 1024) |
26 | 28 | ||
27 | char *type_description[19] = { | 29 | char *type_description[19] = { |
@@ -139,6 +141,19 @@ static void custom_bpf(int sock) { | |||
139 | } | 141 | } |
140 | } | 142 | } |
141 | 143 | ||
144 | static void print_date(void) { | ||
145 | static int day = -1; | ||
146 | time_t now = time(NULL); | ||
147 | struct tm *t = localtime(&now); | ||
148 | |||
149 | if (day != t->tm_yday) { | ||
150 | printf("\nICMP trace for %s", ctime(&now)); | ||
151 | day = t->tm_yday; | ||
152 | } | ||
153 | |||
154 | fflush(0); | ||
155 | } | ||
156 | |||
142 | static void run_trace(void) { | 157 | static void run_trace(void) { |
143 | // grab all Ethernet packets and use a custom BPF filter to get TLS/SNI packets | 158 | // grab all Ethernet packets and use a custom BPF filter to get TLS/SNI packets |
144 | int s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); | 159 | int s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); |
@@ -146,19 +161,24 @@ static void run_trace(void) { | |||
146 | errExit("socket"); | 161 | errExit("socket"); |
147 | custom_bpf(s); | 162 | custom_bpf(s); |
148 | 163 | ||
164 | struct timeval tv; | ||
165 | tv.tv_sec = 10; | ||
166 | tv.tv_usec = 0; | ||
149 | unsigned char buf[MAX_BUF_SIZE]; | 167 | unsigned char buf[MAX_BUF_SIZE]; |
150 | while (1) { | 168 | while (1) { |
151 | fd_set rfds; | 169 | fd_set rfds; |
152 | FD_ZERO(&rfds); | 170 | FD_ZERO(&rfds); |
153 | FD_SET(s, &rfds); | 171 | FD_SET(s, &rfds); |
154 | struct timeval tv; | ||
155 | tv.tv_sec = 1; | ||
156 | tv.tv_usec = 0; | ||
157 | int rv = select(s + 1, &rfds, NULL, NULL, &tv); | 172 | int rv = select(s + 1, &rfds, NULL, NULL, &tv); |
158 | if (rv < 0) | 173 | if (rv < 0) |
159 | errExit("select"); | 174 | errExit("select"); |
160 | else if (rv == 0) | 175 | else if (rv == 0) { |
176 | print_date(); | ||
177 | tv.tv_sec = 10; | ||
178 | tv.tv_usec = 0; | ||
161 | continue; | 179 | continue; |
180 | } | ||
181 | |||
162 | unsigned bytes = recvfrom(s, buf, MAX_BUF_SIZE, 0, NULL, NULL); | 182 | unsigned bytes = recvfrom(s, buf, MAX_BUF_SIZE, 0, NULL, NULL); |
163 | 183 | ||
164 | if (bytes >= (14 + 20 + 2)) { // size of MAC + IP + ICMP code and type fields | 184 | if (bytes >= (14 + 20 + 2)) { // size of MAC + IP + ICMP code and type fields |
@@ -180,7 +200,6 @@ static void run_trace(void) { | |||
180 | close(s); | 200 | close(s); |
181 | } | 201 | } |
182 | 202 | ||
183 | |||
184 | static void usage(void) { | 203 | static void usage(void) { |
185 | printf("Usage: fnettrace-icmp [OPTIONS]\n"); | 204 | printf("Usage: fnettrace-icmp [OPTIONS]\n"); |
186 | printf("Options:\n"); | 205 | printf("Options:\n"); |
@@ -207,8 +226,10 @@ int main(int argc, char **argv) { | |||
207 | return 1; | 226 | return 1; |
208 | } | 227 | } |
209 | 228 | ||
210 | time_t now = time(NULL); | 229 | // kill the process if the parent died |
211 | printf("ICMP trace for %s\n", ctime(&now)); | 230 | prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); |
231 | |||
232 | print_date(); | ||
212 | run_trace(); | 233 | run_trace(); |
213 | 234 | ||
214 | return 0; | 235 | return 0; |