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+
521static 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}
0 commit comments