]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | ||
3 | Copyright (C) 2014 Proxmox Server Solutions GmbH | |
4 | ||
5 | This software is written by Proxmox Server Solutions GmbH <support@proxmox.com> | |
6 | ||
7 | This program is free software: you can redistribute it and/or modify | |
8 | it under the terms of the GNU Affero General Public License as published by | |
9 | the Free Software Foundation, either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU Affero General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Affero General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | ||
20 | Author: Dietmar Maurer <dietmar@proxmox.com> | |
21 | ||
22 | */ | |
23 | ||
24 | #define _GNU_SOURCE | |
25 | ||
26 | #include <stdlib.h> | |
27 | #include <stdio.h> | |
28 | #include <errno.h> | |
29 | #include <stdarg.h> | |
30 | #include <string.h> | |
31 | #include <signal.h> | |
32 | #include <sys/signalfd.h> | |
33 | #include <sys/types.h> | |
34 | #include <sys/stat.h> | |
35 | #include <fcntl.h> | |
36 | #include <unistd.h> | |
37 | #include <arpa/inet.h> | |
38 | #include <sys/socket.h> | |
39 | #include <sys/file.h> | |
40 | #include <linux/netlink.h> | |
41 | #include <libnfnetlink/libnfnetlink.h> | |
42 | #include <libnetfilter_log/libnetfilter_log.h> | |
43 | #include <libnetfilter_conntrack/libnetfilter_conntrack.h> | |
44 | #include <netinet/ip.h> | |
45 | #include <netinet/ip_icmp.h> | |
46 | #include <netinet/ip6.h> | |
47 | #include <netinet/icmp6.h> | |
48 | #include <netinet/udp.h> | |
49 | #include <netinet/tcp.h> | |
50 | #include <netinet/if_ether.h> | |
51 | #include <syslog.h> | |
52 | ||
53 | #include <glib.h> | |
54 | ||
55 | static struct nflog_handle *logh = NULL; | |
56 | static struct nlif_handle *nlifh = NULL; | |
57 | static struct nfct_handle *nfcth = NULL; | |
58 | GMainLoop *main_loop; | |
59 | ||
60 | gboolean foreground = FALSE; | |
61 | gboolean debug = FALSE; | |
62 | gboolean conntrack = FALSE; | |
63 | ||
64 | /* | |
65 | ||
66 | LOG FORMAT: | |
67 | ||
68 | Special care was taken to allow fast parsing (and filer messages for a singl VM). | |
69 | ||
70 | <VMID> <LOGLEVEL> <CHAIN> <TIME> <TIMEZONE> <MSG> | |
71 | ||
72 | Example: | |
73 | ||
74 | 117 6 tap117i0-IN 14/Mar/2014:12:47:07 +0100 policy REJECT: IN=vmbr1 ... | |
75 | ||
76 | */ | |
77 | ||
78 | #define LOGFILE "/var/log/pve-firewall.log" | |
79 | ||
80 | #define LOCKFILE "/var/lock/pvefw-logger.lck" | |
81 | #define PIDFILE "/var/run/pvefw-logger.pid" | |
82 | #define LOG_CONNTRACK_FILE "/var/lib/pve-firewall/log_nf_conntrack" | |
83 | ||
84 | #define LQ_LEN 512 | |
85 | #define LE_MAX (512 - 4) // try to fit into 512 bytes | |
86 | ||
87 | #define MAX_CHAIN_LEN 28 | |
88 | ||
89 | struct log_entry { | |
90 | guint32 len; // max LE_MAX chars | |
91 | char buf[LE_MAX]; | |
92 | }; | |
93 | ||
94 | #define STATIC_ASSERT(cond) \ | |
95 | extern void pve_static_assert(int test[(cond) ? 1 : -1]) | |
96 | ||
97 | STATIC_ASSERT(sizeof(struct log_entry) == 512); | |
98 | ||
99 | int outfd = -1; | |
100 | ||
101 | gboolean terminate_threads = FALSE; | |
102 | ||
103 | static gboolean write_pidfile(pid_t pid) | |
104 | { | |
105 | gboolean res; | |
106 | ||
107 | char *strpid = g_strdup_printf("%d\n", pid); | |
108 | res = g_file_set_contents(PIDFILE, strpid, strlen(strpid), NULL); | |
109 | g_free(strpid); | |
110 | ||
111 | return res; | |
112 | } | |
113 | ||
114 | static GAsyncQueue *queue; | |
115 | ||
116 | ssize_t | |
117 | safe_write(int fd, char *buf, size_t count) | |
118 | { | |
119 | ssize_t n; | |
120 | ||
121 | do { | |
122 | n = write(fd, buf, count); | |
123 | } while (n < 0 && errno == EINTR); | |
124 | ||
125 | return n; | |
126 | } | |
127 | ||
128 | static gpointer | |
129 | log_writer_thread(gpointer data) | |
130 | { | |
131 | while (1) { | |
132 | struct log_entry *le = (struct log_entry *)g_async_queue_timeout_pop(queue, 250000); | |
133 | if (le == NULL) { | |
134 | if (terminate_threads) { | |
135 | return NULL; | |
136 | } | |
137 | continue; | |
138 | } | |
139 | ||
140 | if (debug) fputs(le->buf, stdout); | |
141 | ||
142 | int res = safe_write(outfd, le->buf, le->len); | |
143 | ||
144 | g_free(le); | |
145 | ||
146 | if (res < 0) { | |
147 | syslog(3, "writing log failed, stopping daemon - %s", strerror (errno)); | |
148 | g_main_loop_quit(main_loop); | |
149 | return NULL; | |
150 | } | |
151 | } | |
152 | ||
153 | return NULL; | |
154 | } | |
155 | ||
156 | static int skipped_logs = 0; | |
157 | ||
158 | static void log_status_message(guint loglevel, const char *fmt, ...); | |
159 | ||
160 | static void | |
161 | queue_log_entry(struct log_entry *le) | |
162 | { | |
163 | gint len = g_async_queue_length(queue); | |
164 | ||
165 | if (skipped_logs > 0) { | |
166 | if (len >= (LQ_LEN - 1)) { | |
167 | skipped_logs++; | |
168 | } else { | |
169 | int skip_tmp = skipped_logs; | |
170 | skipped_logs = 0; // clear before calling log_status_message() | |
171 | log_status_message(3, "skipped %d log entries (queue full)", skip_tmp); | |
172 | g_async_queue_push(queue, le); | |
173 | } | |
174 | } else { | |
175 | if (len >= LQ_LEN) { | |
176 | skipped_logs++; | |
177 | } else { | |
178 | g_async_queue_push(queue, le); | |
179 | } | |
180 | } | |
181 | } | |
182 | ||
183 | ||
184 | #define LEPRINTF(format, ...) \ | |
185 | do { \ | |
186 | if (le->len < LE_MAX) \ | |
187 | le->len += snprintf(le->buf + le->len, LE_MAX - le->len, format, ##__VA_ARGS__); \ | |
188 | } while (0) | |
189 | #define LEPRINTTIME(sec) \ | |
190 | do { \ | |
191 | time_t tmp_sec = sec; \ | |
192 | if (le->len < (LE_MAX - 30)) \ | |
193 | le->len += strftime(le->buf + le->len, LE_MAX - le->len, "%d/%b/%Y:%H:%M:%S %z ", localtime(&tmp_sec)); \ | |
194 | } while (0) | |
195 | ||
196 | static void | |
197 | log_status_message(guint loglevel, const char *fmt, ...) | |
198 | { | |
199 | va_list ap, ap2; | |
200 | ||
201 | if (loglevel > 7 ) loglevel = 7; // syslog defines level 0-7 | |
202 | ||
203 | struct log_entry *le = g_new0(struct log_entry, 1); | |
204 | ||
205 | LEPRINTF("0 %d - ", loglevel); | |
206 | ||
207 | LEPRINTTIME(time(NULL)); | |
208 | ||
209 | va_start(ap, fmt); | |
210 | va_copy(ap2, ap); | |
211 | le->len += vsnprintf(le->buf + le->len, LE_MAX - le->len, fmt, ap); | |
212 | va_end(ap); | |
213 | ||
214 | LEPRINTF("\n"); | |
215 | ||
216 | queue_log_entry(le); | |
217 | ||
218 | // also log to syslog | |
219 | ||
220 | vsyslog(loglevel, fmt, ap2); | |
221 | va_end(ap2); | |
222 | } | |
223 | ||
224 | static int | |
225 | print_tcp(struct log_entry *le, struct tcphdr *h, int payload_len) | |
226 | { | |
227 | LEPRINTF("PROTO=TCP "); | |
228 | ||
229 | if (payload_len < sizeof(struct tcphdr)) { | |
230 | LEPRINTF("LEN=%d ", payload_len); | |
231 | LEPRINTF("INVALID=LEN "); | |
232 | return -1; | |
233 | } | |
234 | ||
235 | LEPRINTF("SPT=%u DPT=%u ", ntohs(h->source), ntohs(h->dest)); | |
236 | LEPRINTF("SEQ=%u ACK=%u ", ntohl(h->seq), ntohl(h->ack_seq)); | |
237 | LEPRINTF("WINDOW=%u ", ntohs(h->window)); | |
238 | ||
239 | if (h->urg) LEPRINTF("URG "); | |
240 | if (h->ack) LEPRINTF("ACK "); | |
241 | if (h->psh) LEPRINTF("PSH "); | |
242 | if (h->rst) LEPRINTF("RST "); | |
243 | if (h->syn) LEPRINTF("SYN "); | |
244 | if (h->fin) LEPRINTF("FIN "); | |
245 | ||
246 | if (h->urg) LEPRINTF("URGP=%u ",ntohs(h->urg_ptr)); | |
247 | ||
248 | return 0; | |
249 | } | |
250 | ||
251 | static int | |
252 | print_udp(struct log_entry *le, struct udphdr *h, int payload_len) | |
253 | { | |
254 | LEPRINTF("PROTO=UDP "); | |
255 | ||
256 | if (payload_len < sizeof(struct udphdr)) { | |
257 | LEPRINTF("LEN=%d ", payload_len); | |
258 | LEPRINTF("INVALID=LEN "); | |
259 | return -1; | |
260 | } | |
261 | ||
262 | LEPRINTF("SPT=%u DPT=%u LEN=%u", ntohs(h->source), ntohs(h->dest), ntohs(h->len)); | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
267 | static int | |
268 | print_icmp(struct log_entry *le, struct icmphdr *h, int payload_len) | |
269 | { | |
270 | char tmp[INET_ADDRSTRLEN]; | |
271 | u_int32_t gateway; | |
272 | ||
273 | LEPRINTF("PROTO=ICMP "); | |
274 | ||
275 | if (payload_len < sizeof(struct icmphdr)) { | |
276 | LEPRINTF("LEN=%d ", payload_len); | |
277 | LEPRINTF("INVALID=LEN "); | |
278 | return -1; | |
279 | } | |
280 | ||
281 | LEPRINTF("TYPE=%u CODE=%u ", h->type, h->code); | |
282 | ||
283 | switch (h->type) { | |
284 | case ICMP_ECHO: | |
285 | case ICMP_ECHOREPLY: | |
286 | LEPRINTF("ID=%u SEQ=%u ", ntohs(h->un.echo.id), ntohs(h->un.echo.sequence)); | |
287 | break; | |
288 | case ICMP_PARAMETERPROB: | |
289 | LEPRINTF("PARAMETER=%u ", ntohl(h->un.gateway) >> 24); | |
290 | break; | |
291 | case ICMP_REDIRECT: | |
292 | gateway = ntohl(h->un.gateway); | |
293 | inet_ntop(AF_INET, &gateway, tmp, sizeof(tmp)); | |
294 | LEPRINTF("GATEWAY=%s ", tmp); | |
295 | break; | |
296 | case ICMP_DEST_UNREACH: | |
297 | if (h->code == ICMP_FRAG_NEEDED) { | |
298 | LEPRINTF("MTU=%u ", ntohs(h->un.frag.mtu)); | |
299 | } | |
300 | break; | |
301 | } | |
302 | ||
303 | return 0; | |
304 | } | |
305 | ||
306 | /* Section 3.1. SCTP Common Header Format */ | |
307 | typedef struct sctphdr { | |
308 | __be16 source; | |
309 | __be16 dest; | |
310 | __be32 vtag; | |
311 | __be32 checksum; | |
312 | } __attribute__((packed)) sctp_sctphdr_t; | |
313 | ||
314 | static int | |
315 | print_sctp(struct log_entry *le, struct sctphdr *h, int payload_len) | |
316 | { | |
317 | LEPRINTF("PROTO=SCTP "); | |
318 | ||
319 | if (payload_len < sizeof(struct sctphdr)) { | |
320 | LEPRINTF("LEN=%d ", payload_len); | |
321 | LEPRINTF("INVALID=LEN "); | |
322 | return -1; | |
323 | } | |
324 | ||
325 | LEPRINTF("SPT=%u DPT=%u ", ntohs(h->source), ntohs(h->dest)); | |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
330 | static int | |
331 | print_ipproto(struct log_entry *le, char * nexthdr, int payload_len, u_int8_t proto) | |
332 | { | |
333 | switch (proto) { | |
334 | case IPPROTO_TCP: | |
335 | print_tcp(le, (struct tcphdr *)nexthdr, payload_len); | |
336 | break; | |
337 | case IPPROTO_UDP: | |
338 | print_udp(le, (struct udphdr *)nexthdr, payload_len); | |
339 | break; | |
340 | case IPPROTO_ICMP: | |
341 | print_icmp(le, (struct icmphdr *)nexthdr, payload_len); | |
342 | break; | |
343 | case IPPROTO_SCTP: | |
344 | print_sctp(le, (struct sctphdr *)nexthdr, payload_len); | |
345 | break; | |
346 | case IPPROTO_AH: | |
347 | LEPRINTF("PROTO=AH "); | |
348 | break; | |
349 | case IPPROTO_ESP: | |
350 | LEPRINTF("PROTO=ESP "); | |
351 | break; | |
352 | case IPPROTO_IGMP: | |
353 | LEPRINTF("PROTO=IGMP "); | |
354 | break; | |
355 | default: | |
356 | return -1; | |
357 | } | |
358 | return 0; | |
359 | } | |
360 | ||
361 | static int | |
362 | print_iphdr(struct log_entry *le, char * payload, int payload_len) | |
363 | { | |
364 | if (payload_len < sizeof(struct iphdr)) { | |
365 | LEPRINTF("LEN=%d ", payload_len); | |
366 | LEPRINTF("INVALID=LEN "); | |
367 | return -1; | |
368 | } | |
369 | ||
370 | struct iphdr *h = (struct iphdr *)payload; | |
371 | ||
372 | if (payload_len <= (u_int32_t)(h->ihl * 4)) { | |
373 | LEPRINTF("INVALID=IHL "); | |
374 | return -1; | |
375 | } | |
376 | ||
377 | char tmp[INET_ADDRSTRLEN]; | |
378 | ||
379 | inet_ntop(AF_INET, &h->saddr, tmp, sizeof(tmp)); | |
380 | LEPRINTF("SRC=%s ", tmp); | |
381 | inet_ntop(AF_INET, &h->daddr, tmp, sizeof(tmp)); | |
382 | LEPRINTF("DST=%s ", tmp); | |
383 | ||
384 | LEPRINTF("LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ", | |
385 | ntohs(h->tot_len), h->tos & IPTOS_TOS_MASK, | |
386 | h->tos & IPTOS_PREC_MASK, h->ttl, ntohs(h->id)); | |
387 | ||
388 | short ip_off = ntohs(h->frag_off); | |
389 | if (ip_off & IP_OFFMASK) | |
390 | LEPRINTF("FRAG=%u ", ip_off & IP_OFFMASK); | |
391 | ||
392 | if (ip_off & IP_DF) LEPRINTF("DF "); | |
393 | if (ip_off & IP_MF) LEPRINTF("MF "); | |
394 | ||
395 | void *nexthdr = (u_int32_t *)h + h->ihl; | |
396 | payload_len -= h->ihl * 4; | |
397 | ||
398 | if (print_ipproto(le, nexthdr, payload_len, h->protocol) < 0) { | |
399 | LEPRINTF("PROTO=%u ", h->protocol); | |
400 | } | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
405 | static int | |
406 | print_routing(struct log_entry *le, struct ip6_rthdr *rthdr, int payload_len) | |
407 | { | |
408 | char tmp[INET6_ADDRSTRLEN]; | |
409 | LEPRINTF("TYPE=%u SEGMENTS=%u", rthdr->ip6r_type, rthdr->ip6r_segleft); | |
410 | ||
411 | if (payload_len < sizeof(*rthdr) || payload_len < rthdr->ip6r_len*8) { | |
412 | LEPRINTF("LEN=%d ", payload_len); | |
413 | LEPRINTF("INVALID=LEN "); | |
414 | return -1; | |
415 | } | |
416 | ||
417 | if (rthdr->ip6r_type == 0) { | |
418 | /* Route via waypoints (deprecated), this contains a list of waypoints | |
419 | * to visit. (RFC2460 (4.4)) | |
420 | */ | |
421 | struct ip6_rthdr0 *h = (struct ip6_rthdr0*)rthdr; | |
422 | if (rthdr->ip6r_len*8 < sizeof(*h) + rthdr->ip6r_segleft * sizeof(struct in6_addr)) { | |
423 | LEPRINTF("INVALID=SEGMENTS "); | |
424 | return 0; | |
425 | } | |
426 | return 0; | |
427 | } else if (rthdr->ip6r_type == 1) { | |
428 | /* nimrod routing (RFC1992) */ | |
429 | return 0; | |
430 | } else if (rthdr->ip6r_type == 2) { | |
431 | /* RFC3375 (6.4), the layout is like type-0 but with exactly 1 address */ | |
432 | struct ip6_rthdr0 *h = (struct ip6_rthdr0*)rthdr; | |
433 | if (rthdr->ip6r_len*8 < sizeof(*h) + sizeof(struct in6_addr)) { | |
434 | LEPRINTF("LEN=%d ", payload_len); | |
435 | LEPRINTF("INVALID=LEN "); | |
436 | return -1; | |
437 | } | |
438 | inet_ntop(AF_INET6, &h->ip6r0_addr[0], tmp, sizeof(tmp)); | |
439 | LEPRINTF("HOME=%s ", tmp); | |
440 | return 0; | |
441 | } | |
442 | ||
443 | return 0; | |
444 | } | |
445 | ||
446 | static int | |
447 | print_fragment(struct log_entry *le, struct ip6_frag *frag, int payload_len) | |
448 | { | |
449 | u_int16_t offlg; | |
450 | ||
451 | if (payload_len < sizeof(*frag)) { | |
452 | LEPRINTF("LEN=%d ", payload_len); | |
453 | LEPRINTF("INVALID=LEN "); | |
454 | return -1; | |
455 | } | |
456 | ||
457 | offlg = ntohs(frag->ip6f_offlg); | |
458 | LEPRINTF("FRAG=%d ID=%d ", (offlg&0x2FFF), ntohl(frag->ip6f_ident)); | |
459 | if (offlg>>15) { | |
460 | LEPRINTF("MF "); | |
461 | } | |
462 | return 0; | |
463 | } | |
464 | ||
465 | static int | |
466 | print_icmp6(struct log_entry *le, struct icmp6_hdr *h, int payload_len) | |
467 | { | |
468 | struct nd_router_advert *ra; | |
469 | struct nd_neighbor_advert *na; | |
470 | struct nd_redirect *re; | |
471 | char tmp[INET6_ADDRSTRLEN]; | |
472 | ||
473 | if (payload_len < sizeof(struct icmp6_hdr)) { | |
474 | LEPRINTF("LEN=%d ", payload_len); | |
475 | LEPRINTF("INVALID=LEN "); | |
476 | return -1; | |
477 | } | |
478 | ||
479 | LEPRINTF("TYPE=%u CODE=%u ", h->icmp6_type, h->icmp6_code); | |
480 | ||
481 | switch (h->icmp6_type) { | |
482 | case ICMP6_ECHO_REQUEST: | |
483 | case ICMP6_ECHO_REPLY: | |
484 | LEPRINTF("ID=%u SEQ=%u ", ntohs(h->icmp6_id), ntohs(h->icmp6_seq)); | |
485 | break; | |
486 | ||
487 | case ND_ROUTER_SOLICIT: | |
488 | /* can be followed by options, otherwise nothing to print */ | |
489 | break; | |
490 | ||
491 | case ND_ROUTER_ADVERT: | |
492 | ra = (struct nd_router_advert*)h; | |
493 | LEPRINTF("HOPLIMIT=%d ", ra->nd_ra_curhoplimit); | |
494 | /* nd_ra_flags_reserved is only 8 bit, so no swapping here as | |
495 | * opposed to the neighbor advertisement flags (see below). | |
496 | */ | |
497 | LEPRINTF("RA=%02x LIFETIME=%d REACHABLE=%d RETRANSMIT=%d ", | |
498 | ra->nd_ra_flags_reserved, | |
499 | ntohs(ra->nd_ra_router_lifetime), | |
500 | ntohl(ra->nd_ra_reachable), | |
501 | ntohl(ra->nd_ra_retransmit)); | |
502 | /* can be followed by options */ | |
503 | break; | |
504 | ||
505 | case ND_NEIGHBOR_SOLICIT: | |
506 | /* can be followed by options */ | |
507 | break; | |
508 | ||
509 | case ND_NEIGHBOR_ADVERT: | |
510 | na = (struct nd_neighbor_advert*)h; | |
511 | LEPRINTF("NA=%08x ", ntohl(na->nd_na_flags_reserved)); | |
512 | /* can be followed by options */ | |
513 | break; | |
514 | ||
515 | case ND_REDIRECT: | |
516 | re = (struct nd_redirect*)h; | |
517 | inet_ntop(AF_INET6, &re->nd_rd_target, tmp, sizeof(tmp)); | |
518 | LEPRINTF("TARGET=%s ", tmp); | |
519 | inet_ntop(AF_INET6, &re->nd_rd_dst, tmp, sizeof(tmp)); | |
520 | LEPRINTF("GATEWAY=%s ", tmp); | |
521 | /* can be followed by options */ | |
522 | break; | |
523 | ||
524 | case ICMP6_DST_UNREACH: | |
525 | /* CODE shows the type, no extra parameters available in ipv6 */ | |
526 | break; | |
527 | ||
528 | case ICMP6_PACKET_TOO_BIG: | |
529 | LEPRINTF("MTU=%u ", ntohl(h->icmp6_mtu)); | |
530 | break; | |
531 | ||
532 | case ICMP6_TIME_EXCEEDED: | |
533 | /* CODE shows the type (0 = hop limit, 1 = reassembly timed out) */ | |
534 | break; | |
535 | ||
536 | case ICMP6_PARAM_PROB: | |
537 | switch (ntohl(h->icmp6_pptr)) { | |
538 | case ICMP6_PARAMPROB_HEADER: | |
539 | LEPRINTF("PARAMETER=HEADER "); /* erroneous header */ | |
540 | break; | |
541 | case ICMP6_PARAMPROB_NEXTHEADER: | |
542 | LEPRINTF("PARAMETER=NEXTHEADER "); /* bad next-header field */ | |
543 | break; | |
544 | case ICMP6_PARAMPROB_OPTION: | |
545 | LEPRINTF("PARAMETER=OPTION "); /* bad ipv6 option (hop/dst header?) */ | |
546 | break; | |
547 | default: | |
548 | LEPRINTF("PARAMETER=%u ", ntohl(h->icmp6_pptr)); /* unknown */ | |
549 | break; | |
550 | } | |
551 | break; | |
552 | } | |
553 | ||
554 | return 0; | |
555 | } | |
556 | ||
557 | static int | |
558 | check_ip6ext(struct log_entry *le, struct ip6_ext *exthdr, int payload_len) | |
559 | { | |
560 | if (payload_len < sizeof(*exthdr) || | |
561 | payload_len < exthdr->ip6e_len) | |
562 | { | |
563 | LEPRINTF("LEN=%d ", payload_len); | |
564 | LEPRINTF("INVALID=LEN "); | |
565 | return -1; | |
566 | } | |
567 | return 0; | |
568 | } | |
569 | ||
570 | static int | |
571 | print_nexthdr(struct log_entry *le, char *hdr, int payload_len, u_int8_t proto) | |
572 | { | |
573 | while (1) { | |
574 | if (print_ipproto(le, hdr, payload_len, proto) == 0) | |
575 | return 0; | |
576 | ||
577 | struct ip6_ext *exthdr = (struct ip6_ext*)hdr; | |
578 | ||
579 | switch (proto) { | |
580 | /* protocols (these return) */ | |
581 | case IPPROTO_ICMPV6: | |
582 | LEPRINTF("PROTO=ICMPV6 "); | |
583 | if (check_ip6ext(le, exthdr, payload_len) < 0) | |
584 | return -1; | |
585 | if (print_icmp6(le, (struct icmp6_hdr*)(hdr + exthdr->ip6e_len), | |
586 | payload_len - exthdr->ip6e_len) < 0) | |
587 | { | |
588 | return -1; | |
589 | } | |
590 | return 0; | |
591 | ||
592 | /* extension headers (these break to keep iterating) */ | |
593 | case IPPROTO_ROUTING: | |
594 | if (check_ip6ext(le, exthdr, payload_len) < 0) | |
595 | return -1; | |
596 | if (print_routing(le, (struct ip6_rthdr*)hdr, payload_len) < 0) | |
597 | return -1; | |
598 | break; | |
599 | case IPPROTO_FRAGMENT: | |
600 | if (check_ip6ext(le, exthdr, payload_len) < 0) | |
601 | return -1; | |
602 | if (print_fragment(le, (struct ip6_frag*)hdr, payload_len) < 0) | |
603 | return -1; | |
604 | break; | |
605 | case IPPROTO_HOPOPTS: | |
606 | LEPRINTF("NEXTHDR=HOPOPTS "); | |
607 | if (check_ip6ext(le, exthdr, payload_len) < 0) | |
608 | return -1; | |
609 | /* do we want to print these? */ | |
610 | break; | |
611 | case IPPROTO_DSTOPTS: | |
612 | LEPRINTF("NEXTHDR=DSTOPTS "); | |
613 | if (check_ip6ext(le, exthdr, payload_len) < 0) | |
614 | return -1; | |
615 | /* do we want to print these? */ | |
616 | break; | |
617 | case IPPROTO_MH: | |
618 | LEPRINTF("NEXTHDR=MH "); | |
619 | if (check_ip6ext(le, exthdr, payload_len) < 0) | |
620 | return -1; | |
621 | break; | |
622 | ||
623 | /* unknown protocol */ | |
624 | default: | |
625 | LEPRINTF("PROTO=%u ", proto); | |
626 | return 0; /* bail */ | |
627 | } | |
628 | /* next header: */ | |
629 | if (check_ip6ext(le, exthdr, payload_len) < 0) | |
630 | return -1; | |
631 | hdr += exthdr->ip6e_len; | |
632 | payload_len -= exthdr->ip6e_len; | |
633 | } | |
634 | } | |
635 | ||
636 | static int | |
637 | print_ip6hdr(struct log_entry *le, char * payload, int payload_len) | |
638 | { | |
639 | if (payload_len < sizeof(struct ip6_hdr)) { | |
640 | LEPRINTF("LEN=%d ", payload_len); | |
641 | LEPRINTF("INVALID=LEN "); | |
642 | return -1; | |
643 | } | |
644 | ||
645 | struct ip6_hdr *h = (struct ip6_hdr*)payload; | |
646 | ||
647 | char tmp[INET6_ADDRSTRLEN]; | |
648 | inet_ntop(AF_INET6, &h->ip6_src, tmp, sizeof(tmp)); | |
649 | LEPRINTF("SRC=%s ", tmp); | |
650 | inet_ntop(AF_INET6, &h->ip6_dst, tmp, sizeof(tmp)); | |
651 | LEPRINTF("DST=%s ", tmp); | |
652 | ||
653 | LEPRINTF("LEN=%u ", ntohs(h->ip6_plen)); | |
654 | ||
655 | u_int32_t flow = ntohl(h->ip6_flow); | |
656 | LEPRINTF("TC=%d FLOWLBL=%d ", (flow>>20)&0xFF, flow&0xFFFFF); | |
657 | ||
658 | LEPRINTF("HOPLIMIT=%d ", h->ip6_hlim); | |
659 | ||
660 | return print_nexthdr(le, (char *)(h+1), payload_len - sizeof(*h), h->ip6_nxt); | |
661 | } | |
662 | ||
663 | // ebtables -I FORWARD --nflog --nflog-group 0 | |
664 | static int | |
665 | print_arp(struct log_entry *le, struct ether_arp *h, int payload_len) | |
666 | { | |
667 | if (payload_len < sizeof(struct ether_arp)) { | |
668 | LEPRINTF("LEN=%d ", payload_len); | |
669 | LEPRINTF("INVALID=LEN "); | |
670 | return -1; | |
671 | } | |
672 | ||
673 | LEPRINTF("SRC=%u.%u.%u.%u ", h->arp_spa[0], h->arp_spa[1], | |
674 | h->arp_spa[2], h->arp_spa[3]); | |
675 | ||
676 | LEPRINTF("DST=%u.%u.%u.%u ", h->arp_tpa[0], h->arp_tpa[1], | |
677 | h->arp_tpa[2], h->arp_tpa[3]); | |
678 | ||
679 | LEPRINTF("PROTO=ARP "); | |
680 | ||
681 | unsigned short code = ntohs(h->arp_op); | |
682 | switch (code) { | |
683 | case ARPOP_REQUEST: | |
684 | LEPRINTF("REQUEST "); | |
685 | break; | |
686 | case ARPOP_REPLY: | |
687 | LEPRINTF("REPLY MAC=%02x:%02x:%02x:%02x:%02x:%02x ", | |
688 | h->arp_sha[0], h->arp_sha[1], h->arp_sha[2], | |
689 | h->arp_sha[3], h->arp_sha[4], h->arp_sha[5]); | |
690 | break; | |
691 | case ARPOP_NAK: | |
692 | LEPRINTF("NAK "); | |
693 | break; | |
694 | default: | |
695 | LEPRINTF("CODE=%u ", code); | |
696 | } | |
697 | ||
698 | ||
699 | // LEPRINTF("HTYPE=%u ", ntohs(h->arp_hrd)); | |
700 | ||
701 | // LEPRINTF("PTYPE=%u ", ntohs(h->arp_pro)); | |
702 | ||
703 | return 0; | |
704 | } | |
705 | ||
706 | ||
707 | static int print_pkt(struct log_entry *le, struct nflog_data *ldata, u_int8_t family) | |
708 | { | |
709 | u_int32_t mark = nflog_get_nfmark(ldata); | |
710 | u_int32_t indev = nflog_get_indev(ldata); | |
711 | u_int32_t outdev = nflog_get_outdev(ldata); | |
712 | u_int32_t physindev = nflog_get_physindev(ldata); | |
713 | u_int32_t physoutdev = nflog_get_physoutdev(ldata); | |
714 | ||
715 | char *prefix = nflog_get_prefix(ldata); | |
716 | char *payload; | |
717 | char devname[256]; | |
718 | ||
719 | guint32 vmid = 0; | |
720 | ||
721 | guint8 log_level = 6; // info | |
722 | ||
723 | char *chain_name = "-"; | |
724 | ||
725 | if (prefix != NULL) { | |
726 | // Note: parse ":$vmid:$loglevel:$chain: $msg" | |
727 | if (prefix[0] == ':') { | |
728 | char *p = prefix + 1; | |
729 | guint32 tmpid = 0; | |
730 | while(*p >= '0' && *p <= '9') { tmpid *= 10; tmpid += *p - '0'; p++; } | |
731 | ||
732 | if ((*p == ':') && | |
733 | (p[1] >= '0' && p[1] <= '7') && | |
734 | (p[2] == ':')) { | |
735 | ||
736 | guint8 tmp_level = p[1] - '0'; // store for later use | |
737 | char *chain_start = p + 3; // store for later use | |
738 | p = chain_start; | |
739 | while (*p && *p != ':' && *p != ' ') p++; | |
740 | int len = p - chain_start; | |
741 | ||
742 | if (*p == ':' && p[1] == ' ' && len && (len <= MAX_CHAIN_LEN)) { | |
743 | // parsing successful | |
744 | ||
745 | *p = 0; // terminate string | |
746 | ||
747 | vmid = tmpid; | |
748 | log_level = tmp_level; | |
749 | chain_name = chain_start; | |
750 | prefix = p + 2; // the rest | |
751 | } | |
752 | } | |
753 | } | |
754 | } | |
755 | ||
756 | LEPRINTF("%d ", vmid); | |
757 | ||
758 | LEPRINTF("%d ", log_level); | |
759 | ||
760 | LEPRINTF("%s ", chain_name); | |
761 | ||
762 | struct timeval ts; | |
763 | if (nflog_get_timestamp(ldata, &ts) == 0) { | |
764 | LEPRINTTIME(ts.tv_sec); | |
765 | } else { | |
766 | LEPRINTTIME(time(NULL)); | |
767 | } | |
768 | ||
769 | if (prefix != NULL) { | |
770 | LEPRINTF("%s", prefix); | |
771 | } | |
772 | ||
773 | if (indev > 0) { | |
774 | if (nlif_index2name(nlifh, indev, devname) != -1) { | |
775 | LEPRINTF("IN=%s ", devname); | |
776 | } else { | |
777 | LEPRINTF("IN=%u ", indev); | |
778 | } | |
779 | } | |
780 | ||
781 | if (outdev > 0) { | |
782 | if (nlif_index2name(nlifh, outdev, devname) != -1) { | |
783 | LEPRINTF("OUT=%s ", devname); | |
784 | } else { | |
785 | LEPRINTF("OUT=%u ", outdev); | |
786 | } | |
787 | } | |
788 | ||
789 | if (physindev > 0) { | |
790 | if (nlif_index2name(nlifh, physindev, devname) != -1) { | |
791 | LEPRINTF("PHYSIN=%s ", devname); | |
792 | } else { | |
793 | LEPRINTF("PHYSIN=%u ", physindev); | |
794 | } | |
795 | } | |
796 | ||
797 | if (physoutdev > 0) { | |
798 | if (nlif_index2name(nlifh, physoutdev, devname) != -1) { | |
799 | LEPRINTF("PHYSOUT=%s ", devname); | |
800 | } else { | |
801 | LEPRINTF("PHYSOUT=%u ", physoutdev); | |
802 | } | |
803 | } | |
804 | ||
805 | int payload_len = nflog_get_payload(ldata, &payload); | |
806 | ||
807 | int hwhdrlen = nflog_get_msg_packet_hwhdrlen(ldata); | |
808 | if (hwhdrlen > 0) { | |
809 | unsigned char *hwhdr = (unsigned char *)nflog_get_msg_packet_hwhdr(ldata); | |
810 | if (hwhdr != NULL) { | |
811 | int i; | |
812 | LEPRINTF("MAC="); | |
813 | for (i = 0; i < hwhdrlen; i++) { | |
814 | LEPRINTF("%02x", hwhdr[i]); | |
815 | if (i < (hwhdrlen -1 )) LEPRINTF(":"); | |
816 | } | |
817 | LEPRINTF(" "); | |
818 | } | |
819 | } | |
820 | ||
821 | u_int16_t hw_protocol = 0; | |
822 | struct nfulnl_msg_packet_hdr *ph = NULL; | |
823 | ||
824 | switch (family) { | |
825 | case AF_INET: | |
826 | print_iphdr(le, payload, payload_len); | |
827 | break; | |
828 | case AF_INET6: | |
829 | print_ip6hdr(le, payload, payload_len); | |
830 | break; | |
831 | case AF_BRIDGE: | |
832 | ph = nflog_get_msg_packet_hdr(ldata); | |
833 | if (ph) hw_protocol = ntohs(ph->hw_protocol); | |
834 | ||
835 | switch (hw_protocol) { | |
836 | case ETH_P_IP: | |
837 | print_iphdr(le, payload, payload_len); | |
838 | break; | |
839 | case ETH_P_IPV6: | |
840 | print_ip6hdr(le, payload, payload_len); | |
841 | break; | |
842 | case ETH_P_ARP: | |
843 | print_arp(le, (struct ether_arp *)payload, payload_len); | |
844 | break; | |
845 | } | |
846 | break; | |
847 | } | |
848 | ||
849 | if (mark) LEPRINTF("mark=%u ", mark); | |
850 | ||
851 | ||
852 | return 0; | |
853 | ||
854 | } | |
855 | ||
856 | static int | |
857 | nflog_cb(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg, | |
858 | struct nflog_data *nfa, void *data) | |
859 | { | |
860 | struct log_entry *le = g_new0(struct log_entry, 1); | |
861 | ||
862 | print_pkt(le, nfa, nfmsg->nfgen_family); | |
863 | ||
864 | LEPRINTF("\n"); // add newline | |
865 | ||
866 | queue_log_entry(le); | |
867 | ||
868 | return 0; | |
869 | } | |
870 | ||
871 | static gboolean | |
872 | nflog_read_cb(GIOChannel *source, | |
873 | GIOCondition condition, | |
874 | gpointer data) | |
875 | { | |
876 | int rv = 0; | |
877 | gchar buf[8192]; | |
878 | ||
879 | int fd = g_io_channel_unix_get_fd(source); | |
880 | ||
881 | if ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) { | |
882 | nflog_handle_packet(logh, buf, rv); | |
883 | } | |
884 | ||
885 | return TRUE; | |
886 | } | |
887 | ||
888 | static gboolean | |
889 | nlif_read_cb(GIOChannel *source, | |
890 | GIOCondition condition, | |
891 | gpointer data) | |
892 | { | |
893 | static int last_res = 0; | |
894 | int res; | |
895 | ||
896 | if ((res = nlif_catch(nlifh)) < 0) { | |
897 | if (last_res == 0) { // only report once | |
898 | log_status_message(3, "nlif_catch failed (res = %d)", res); | |
899 | } | |
900 | last_res = res; | |
901 | } else { | |
902 | last_res = 0; | |
903 | } | |
904 | ||
905 | return TRUE; | |
906 | } | |
907 | ||
908 | static gboolean | |
909 | signal_read_cb(GIOChannel *source, | |
910 | GIOCondition condition, | |
911 | gpointer data) | |
912 | { | |
913 | int rv = 0; | |
914 | struct signalfd_siginfo si; | |
915 | ||
916 | int fd = g_io_channel_unix_get_fd(source); | |
917 | ||
918 | if ((rv = read(fd, &si, sizeof(si))) && rv >= 0) { | |
919 | terminate_threads = TRUE; | |
920 | log_status_message(5, "received terminate request (signal)"); | |
921 | g_main_loop_quit(main_loop); | |
922 | } | |
923 | ||
924 | return TRUE; | |
925 | } | |
926 | ||
927 | static int | |
928 | nfct_cb(const struct nlmsghdr *nlh, | |
929 | enum nf_conntrack_msg_type type, | |
930 | struct nf_conntrack *ct, | |
931 | void *data) | |
932 | { | |
933 | struct log_entry *le = g_new0(struct log_entry, 1); | |
934 | int len = nfct_snprintf(&le->buf[le->len], LE_MAX - le->len, | |
935 | ct, type, NFCT_O_DEFAULT, | |
936 | NFCT_OF_SHOW_LAYER3|NFCT_OF_TIMESTAMP); | |
937 | le->len += len; | |
938 | ||
939 | if (le->len == LE_MAX) { | |
940 | le->buf[le->len-1] = '\n'; | |
941 | } else { // le->len < LE_MAX | |
942 | le->buf[le->len++] = '\n'; | |
943 | } | |
944 | ||
945 | queue_log_entry(le); | |
946 | ||
947 | return NFCT_CB_STOP; | |
948 | } | |
949 | ||
950 | static gboolean | |
951 | nfct_read_cb(GIOChannel *source, | |
952 | GIOCondition condition, | |
953 | gpointer data) | |
954 | { | |
955 | int res; | |
956 | if ((res = nfct_catch(nfcth)) < 0) { | |
957 | if (errno == ENOBUFS) { | |
958 | log_status_message(3, "nfct_catch returned ENOBUFS: conntrack information may be incomplete"); | |
959 | } else { | |
960 | log_status_message(3, "error catching nfct: %s", strerror(errno)); | |
961 | return FALSE; | |
962 | } | |
963 | } | |
964 | return TRUE; | |
965 | } | |
966 | ||
967 | int | |
968 | main(int argc, char *argv[]) | |
969 | { | |
970 | int lockfd = -1; | |
971 | int sigfd = -1; | |
972 | ||
973 | gboolean wrote_pidfile = FALSE; | |
974 | ||
975 | openlog("pvefw-logger", LOG_CONS|LOG_PID, LOG_DAEMON); | |
976 | ||
977 | GOptionContext *context; | |
978 | ||
979 | GOptionEntry entries[] = { | |
980 | { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "Turn on debug messages", NULL }, | |
981 | { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground, "Do not daemonize server", NULL }, | |
982 | { "conntrack", 0, 0, G_OPTION_ARG_NONE, &conntrack, "Add conntrack logging", NULL }, | |
983 | { NULL }, | |
984 | }; | |
985 | ||
986 | context = g_option_context_new(""); | |
987 | g_option_context_add_main_entries (context, entries, NULL); | |
988 | ||
989 | GError *err = NULL; | |
990 | if (!g_option_context_parse (context, &argc, &argv, &err)) { | |
991 | fprintf(stderr, "error: %s\n", err->message); | |
992 | fprintf(stderr, "%s", g_option_context_get_help(context, FALSE, NULL)); | |
993 | g_error_free (err); | |
994 | exit(-1); | |
995 | } | |
996 | ||
997 | if (optind < argc) { | |
998 | fprintf(stderr, "error: too many arguments\n"); | |
999 | fprintf(stderr, "%s", g_option_context_get_help(context, FALSE, NULL)); | |
1000 | exit(-1); | |
1001 | } | |
1002 | ||
1003 | g_option_context_free(context); | |
1004 | ||
1005 | if (!conntrack) { | |
1006 | int log_nf_conntrackfd = open(LOG_CONNTRACK_FILE, O_RDONLY); | |
1007 | if (log_nf_conntrackfd == -1) { | |
1008 | if (errno != ENOENT) { | |
1009 | fprintf(stderr, "error: failed to open "LOG_CONNTRACK_FILE": %s\n", strerror(errno)); | |
1010 | } | |
1011 | } else { | |
1012 | char c = '0'; | |
1013 | ssize_t bytes = read(log_nf_conntrackfd, &c, sizeof(c)); | |
1014 | if (bytes < 0) { | |
1015 | fprintf(stderr, "error: failed to read value in log_nf_conntrack: %s\n", strerror(errno)); | |
1016 | } else { | |
1017 | conntrack = (c == '1'); | |
1018 | } | |
1019 | } | |
1020 | } | |
1021 | ||
1022 | if (debug) foreground = TRUE; | |
1023 | ||
1024 | if ((lockfd = open(LOCKFILE, O_RDWR|O_CREAT|O_APPEND, 0644)) == -1) { | |
1025 | fprintf(stderr, "unable to create lock '%s': %s\n", LOCKFILE, strerror (errno) ); | |
1026 | exit(-1); | |
1027 | } | |
1028 | ||
1029 | for (int i = 10; i >= 0; i--) { | |
1030 | if (flock(lockfd, LOCK_EX|LOCK_NB) != 0) { | |
1031 | if (!i) { | |
1032 | fprintf(stderr, "unable to aquire lock '%s': %s\n", LOCKFILE, strerror (errno)); | |
1033 | exit(-1); | |
1034 | } | |
1035 | if (i == 10) | |
1036 | fprintf(stderr, "unable to aquire lock '%s' - trying again.\n", LOCKFILE); | |
1037 | ||
1038 | sleep(1); | |
1039 | } | |
1040 | } | |
1041 | ||
1042 | if ((outfd = open(LOGFILE, O_WRONLY|O_CREAT|O_APPEND, 0644)) == -1) { | |
1043 | fprintf(stderr, "unable to open file '%s': %s\n", LOGFILE, strerror (errno)); | |
1044 | exit(-1); | |
1045 | } | |
1046 | ||
1047 | if ((logh = nflog_open()) == NULL) { | |
1048 | fprintf(stderr, "unable to open nflog\n"); | |
1049 | exit(-1); | |
1050 | } | |
1051 | ||
1052 | if (nflog_bind_pf(logh, AF_INET) < 0) { | |
1053 | fprintf(stderr, "nflog_bind_pf AF_INET failed\n"); | |
1054 | exit(-1); | |
1055 | } | |
1056 | ||
1057 | #if 0 | |
1058 | if (!nflog_bind_pf(logh, AF_INET6) <= 0) { | |
1059 | fprintf(stderr, "nflog_bind_pf AF_INET6 failed\n"); | |
1060 | exit(-1); | |
1061 | } | |
1062 | #endif | |
1063 | ||
1064 | if (nflog_bind_pf(logh, AF_BRIDGE) < 0) { | |
1065 | fprintf(stderr, "nflog_bind_pf AF_BRIDGE failed\n"); | |
1066 | exit(-1); | |
1067 | } | |
1068 | ||
1069 | struct nflog_g_handle *qh = nflog_bind_group(logh, 0); | |
1070 | if (!qh) { | |
1071 | fprintf(stderr, "no nflog handle for group 0\n"); | |
1072 | exit(-1); | |
1073 | } | |
1074 | ||
1075 | if (nflog_set_mode(qh, NFULNL_COPY_PACKET, 0xffff) < 0) { | |
1076 | fprintf(stderr, "can't set packet copy mode\n"); | |
1077 | exit(-1); | |
1078 | } | |
1079 | ||
1080 | if ((nlifh = nlif_open()) == NULL) { | |
1081 | fprintf(stderr, "unable to open netlink interface handle\n"); | |
1082 | exit(-1); | |
1083 | } | |
1084 | ||
1085 | if (conntrack) { | |
1086 | if ((nfcth = nfct_open(CONNTRACK, NF_NETLINK_CONNTRACK_NEW|NF_NETLINK_CONNTRACK_DESTROY)) == NULL) { | |
1087 | fprintf(stderr, "unable to open netfilter conntrack\n"); | |
1088 | exit(-1); | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | sigset_t mask; | |
1093 | sigemptyset(&mask); | |
1094 | sigaddset(&mask, SIGINT); | |
1095 | sigaddset(&mask, SIGTERM); | |
1096 | ||
1097 | sigprocmask(SIG_BLOCK, &mask, NULL); | |
1098 | ||
1099 | if ((sigfd = signalfd(-1, &mask, SFD_NONBLOCK)) < 0) { | |
1100 | fprintf(stderr, "unable to open signalfd: %s\n", strerror (errno)); | |
1101 | exit(-1); | |
1102 | } | |
1103 | ||
1104 | if (!foreground) { | |
1105 | pid_t cpid = fork(); | |
1106 | ||
1107 | if (cpid == -1) { | |
1108 | fprintf(stderr, "failed to daemonize program - %s\n", strerror (errno)); | |
1109 | exit(-1); | |
1110 | } else if (cpid) { | |
1111 | write_pidfile(cpid); | |
1112 | _exit(0); | |
1113 | } else { | |
1114 | int nullfd; | |
1115 | ||
1116 | if (chroot("/") != 0) fprintf(stderr, "chroot '/' failed - %s\n", strerror (errno)); | |
1117 | ||
1118 | if ((nullfd = open("/dev/null", O_RDWR, 0)) != -1) { | |
1119 | dup2(nullfd, 0); | |
1120 | dup2(nullfd, 1); | |
1121 | dup2(nullfd, 2); | |
1122 | if (nullfd > 2) | |
1123 | close (nullfd); | |
1124 | } | |
1125 | ||
1126 | setsid(); | |
1127 | } | |
1128 | } else { | |
1129 | write_pidfile(getpid()); | |
1130 | } | |
1131 | ||
1132 | wrote_pidfile = TRUE; | |
1133 | ||
1134 | nflog_callback_register(qh, &nflog_cb, logh); | |
1135 | ||
1136 | queue = g_async_queue_new_full(g_free); | |
1137 | ||
1138 | log_status_message(5, "starting pvefw logger"); | |
1139 | ||
1140 | nlif_query(nlifh); | |
1141 | ||
1142 | GIOChannel *nlif_ch = g_io_channel_unix_new(nlif_fd(nlifh)); | |
1143 | ||
1144 | g_io_add_watch(nlif_ch, G_IO_IN, nlif_read_cb, NULL); | |
1145 | ||
1146 | int logfd = nflog_fd(logh); | |
1147 | GIOChannel *nflog_ch = g_io_channel_unix_new(logfd); | |
1148 | ||
1149 | g_io_add_watch(nflog_ch, G_IO_IN, nflog_read_cb, NULL); | |
1150 | ||
1151 | if (conntrack) { | |
1152 | nfct_callback_register2(nfcth, NFCT_T_NEW|NFCT_T_DESTROY, &nfct_cb, NULL); | |
1153 | int nfctfd = nfct_fd(nfcth); | |
1154 | GIOChannel *nfct_ch = g_io_channel_unix_new(nfctfd); | |
1155 | g_io_add_watch(nfct_ch, G_IO_IN, nfct_read_cb, NULL); | |
1156 | } | |
1157 | ||
1158 | GIOChannel *sig_ch = g_io_channel_unix_new(sigfd); | |
1159 | if (!g_io_add_watch(sig_ch, G_IO_IN, signal_read_cb, NULL)) { | |
1160 | exit(-1); | |
1161 | } | |
1162 | ||
1163 | GThread *wthread = g_thread_new("log_writer_thread", log_writer_thread, NULL); | |
1164 | ||
1165 | main_loop = g_main_loop_new(NULL, TRUE); | |
1166 | ||
1167 | g_main_loop_run(main_loop); | |
1168 | ||
1169 | log_status_message(5, "stopping pvefw logger"); | |
1170 | ||
1171 | g_thread_join(wthread); | |
1172 | ||
1173 | close(outfd); | |
1174 | ||
1175 | if (conntrack) { | |
1176 | nfct_callback_unregister2(nfcth); | |
1177 | nfct_close(nfcth); | |
1178 | } | |
1179 | ||
1180 | nflog_close(logh); | |
1181 | ||
1182 | if (wrote_pidfile) | |
1183 | unlink(PIDFILE); | |
1184 | ||
1185 | exit(0); | |
1186 | } |