File: netfilter.ll

package info (click to toggle)
wflogs 0.9.8-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,220 kB
  • ctags: 3,750
  • sloc: ansic: 12,166; sh: 8,047; cpp: 6,412; lex: 1,525; yacc: 1,430; makefile: 731; sed: 16
file content (223 lines) | stat: -rw-r--r-- 6,868 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* -*-c-*- */
/*
 * WallFire -- a comprehensive firewall administration tool.
 * 
 * Copyright (C) 2001 Herv Eychenne <rv@wallfire.org>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */

%option prefix="netfilter"
%option outfile="netfilter.cc"
%option noyywrap

%{
using namespace std;

#define YY_NO_UNPUT

#include <unistd.h>
#include <string.h>
#include <ctype.h>

#include "netfilter.h"
#include "realtime.h"
#include "common.h"
#include "defs.h"


static void netfilter_parse_prefix(char *input);
static void netfilter_parse_noprefix(char *input);
static void netfilter_parse_proto(char *input);

static wf_logentry* logentry;
%}

MONTH	("Jan"|"Feb"|"Mar"|"Apr"|"May"|"Jun"|"Jul"|"Aug"|"Sep"|"Oct"|"Nov"|"Dec")
STRING	[a-zA-Z][a-zA-Z0-9.-]*
PREFIX	([ -OQ-~-]+|([ -OQ-~-]*P[ -GI-~-]*)|([ -OQ-~-]*PH[ -XZ-~-]*)|([ -OQ-~-]*PHY[ -RT-~-]*))
LOGHOST	[0-9.a-zA-Z_-]*
DIGIT	[0-9]
NUMBER	{DIGIT}+
BYTE	{DIGIT}{1,3}
IPADDR	{BYTE}\.{BYTE}\.{BYTE}\.{BYTE}
PORT	{DIGIT}{1,5}
HEXDIGIT	[0-9a-fA-F]
HEXNUM	"0x"{HEXDIGIT}+
PROTO	("TCP"|"UDP"|"ICMP"|"ESP"|"AH"|"ICMPv6"|{NUMBER})

%%

{MONTH}[ ]{1,2}{DIGIT}{1,2}[ ]{DIGIT}{2}:{DIGIT}{2}:{DIGIT}{2}[ ]{LOGHOST}	parse_date(netfiltertext, logentry);
" kernel: IN="{STRING}?			netfilter_parse_noprefix(netfiltertext+12);
" kernel: "{PREFIX}"IN="{STRING}?	netfilter_parse_prefix(netfiltertext+9);
" klogd: IN="{STRING}?			netfilter_parse_noprefix(netfiltertext+11);
" klogd: "{PREFIX}"IN="{STRING}?	netfilter_parse_prefix(netfiltertext+8);
" "{PREFIX}"IN="{STRING}?		netfilter_parse_prefix(netfiltertext+1);
"OUT="{STRING}?				logentry->output_iface = netfiltertext+4;
"PHYSIN="{STRING}?	/* ignore */
"PHYSOUT="{STRING}?	/* ignore */
 /* MAC is dst_mac:src_mac:08:00 */
"MAC="(({HEXDIGIT}{HEXDIGIT}:){13}{HEXDIGIT}{HEXDIGIT})?	{
  if (netfiltertext[4] != '\0') {
    netfiltertext[21] = '\0';
    netfiltertext[39] = '\0'; /* ignore last 08:00 */
    logentry->smacaddr.set(netfiltertext + 22);
    logentry->dmacaddr.set(netfiltertext + 4);
  }
}
"SRC="{IPADDR}	{
  logentry->sipaddr.set(netfiltertext + 4);
//  convert_ip(netfiltertext + 4, &logentry->shost);
}
"DST="{IPADDR}	{
  logentry->dipaddr.set(netfiltertext + 4);
  //  convert_ip(netfiltertext + 4, &logentry->dhost);
}
"LEN="{NUMBER}		logentry->datalen = atoi(netfiltertext+4);
"TOS="{HEXNUM}		/* ignore */
"PREC="{HEXNUM}		/* ignore */
"TTL="{NUMBER}		/* ignore */
"ID="{NUMBER}		/* ignore */
"CE"			/* ignore */
"DF"			/* ignore */
"MF"			/* ignore */
"FRAG:"{NUMBER}		/* ignore */
"PROTO="{PROTO}		netfilter_parse_proto(netfiltertext+6);
"INCOMPLETE ["{NUMBER}" bytes]"	/* ignore */
"TYPE="{NUMBER}		{ logentry->sport = atoi(netfiltertext+5); }
"CODE="{NUMBER}		{ logentry->dport = atoi(netfiltertext+5); }
"SEQ="{NUMBER}		/* ignore */
"ACK="{NUMBER}		/* ignore */
"SPT="{PORT}		{ logentry->sport = atoi(netfiltertext+4); }
"DPT="{PORT}		{ logentry->dport = atoi(netfiltertext+4); }
"WINDOW="{NUMBER}	/* ignore */
"RES="{HEXNUM}		/* ignore */
"FIN"			logentry->tcpflags |= TCP_FIN;
"SYN"			logentry->tcpflags |= TCP_SYN;
"RST"			logentry->tcpflags |= TCP_RST;
"PSH"			logentry->tcpflags |= TCP_PSH;
"ACK"			logentry->tcpflags |= TCP_ACK;
"URG"			logentry->tcpflags |= TCP_URG;
"ECE"			logentry->tcpflags |= TCP_ECE;
"CWR"			logentry->tcpflags |= TCP_CWR;
"URGP="{NUMBER}		/* ignore */
"OPT ("[0-9A-F]*")"	/* ignore */
"SPI="{HEXNUM}		/* ignore */
"GATEWAY="{IPADDR}	/* ignore */
"TC="{NUMBER}		/* ignore */
"HOPLIMIT="{NUMBER}	/* ignore */
"FLOWLBL="{NUMBER}	/* ignore */
"[".+"]"		/* ignore */
[ ]+			/* ignore whitespace */
[\n]			return 0;
{STRING}		if (verbose) fprintf(stderr, _("Unrecognized token: %s\n"), netfiltertext);
.			if (verbose) fprintf(stderr, _("Unrecognized character: %s\n"), netfiltertext);

%%

static void
netfilter_parse_prefix(char* input) {
  char* pos = strstr(input, "IN=");
  if (pos != NULL) {
    *pos = '\0';
    logentry->chainlabel = input;
    logentry->input_iface = pos + 3;
  }
}

static void
netfilter_parse_noprefix(char *input) {
  logentry->chainlabel = "-";
  logentry->input_iface = input;
}

static void
netfilter_parse_proto(char* input) {
  if (isdigit(input[0]))
    logentry->protocol = atoi(input);
  else {
    if (!strncmp(input, "TCP", 3))
      logentry->protocol = IPPROTO_TCP;
    else if (!strncmp(input, "UDP", 3))
      logentry->protocol = IPPROTO_UDP;
    else if (!strncmp(input, "ICMP", 4))
      logentry->protocol = IPPROTO_ICMP;
    else if (!strncmp(input, "ESP", 3))
      logentry->protocol = IPPROTO_ESP;
    else if (!strncmp(input, "AH", 2))
      logentry->protocol = IPPROTO_AH;
    else if (!strncmp(input, "ICMPv6", 6))
      logentry->protocol = IPPROTO_ICMPV6;
  }
}


wf_inmodule_netfilter::wf_inmodule_netfilter() {
}

bool
wf_inmodule_netfilter::match(const string& line) const {
  //  return (line.find(" OUT=") != string::npos);

  /* First we search "kernel:", then "OUT=". */
  const char str[] = " kernel: ";
  string::size_type i;
  return ((i = line.find(str)) != string::npos &&
	  line.find(" OUT=", i + sizeof(str) - 1) != string::npos);
}

enum wf_logentry_parsing_result
wf_inmodule_netfilter::parse(wf_logentry** retlogentry,
			     const string& line, int linenum) {
  logentry = new wf_logentry();
  if (logentry == NULL)
    return WF_LOGENTRY_PARSING_ERROR;

  netfilter_scan_string(line.c_str());
  netfilterlex();

  logentry->count = 1; /* only one log line per packet */
  logentry->branchname = "-"; /* not defined */
  logentry->format = "netfilter";

  /*
  if ((logentry->protocol == 6 || logentry->protocol == 17) &&
      parser == (NF_DATE | NF_PROTO | NF_IN | NF_SRC | NF_DST | NF_SPT | NF_DPT))
    *retlogentry = logentry;
    return WF_LOGENTRY_PARSING_OK;

  if (logentry->protocol == 1 &&
      parser == (NF_DATE | NF_PROTO | NF_IN | NF_SRC | NF_DST | NF_TYPE))
  */
    *retlogentry = logentry;
    return WF_LOGENTRY_PARSING_OK;

  if (verbose)
    fprintf(stderr, _("netfilter parse error in line %d, ignoring.\n"),
	    linenum);

  delete logentry;
  return WF_LOGENTRY_PARSING_ERROR;
}

extern "C" wf_inmodule*
wf_inmodule_netfilter_init() {
#ifdef ENABLE_NFULOG
  wf_inmodule_netfilter_realtime_init();
#endif
  return new wf_inmodule_netfilter();
}