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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/* Copyright (C) 2000-2006 Boris Wesslowski */
/* $Id: netfilter.l,v 1.46 2006/03/08 19:36:03 bw Exp $ */
%option prefix="nf"
%option outfile="netfilter.c"
%option noyywrap
%{
#define YY_NO_UNPUT
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include "main.h"
#include "utils.h"
extern struct options opt;
void nf_parse_start_hn(char *input);
void nf_parse_start(char *input);
void nf_parse_isostart_hn(char *input);
void nf_parse_prefix(char *input, unsigned char mode);
void nf_parse_ip(char *input, unsigned char mode);
void nf_parse_proto(char *input);
%}
MONTH "Jan"|"Feb"|"Mar"|"Apr"|"May"|"Jun"|"Jul"|"Aug"|"Sep"|"Oct"|"Nov"|"Dec"
STRING [a-zA-Z][a-zA-Z0-9.-]*
PREFIX [ -~-]*([ -RT-~-]|[ -XZ-~-]"S")"IN="
LOGHOST [0-9.a-zA-Z()_:-]+
DIGIT [0-9]
NUMBER {DIGIT}+
OCTET {DIGIT}{1,3}
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} nf_parse_start_hn(nftext);
{MONTH}[ ]{1,2}{DIGIT}{1,2}[ ]{DIGIT}{2}:{DIGIT}{2}:{DIGIT}{2} nf_parse_start(nftext);
{DIGIT}{4}[ ]{MONTH}[ ]{1,2}{DIGIT}{1,2}[ ]{DIGIT}{2}:{DIGIT}{2}:{DIGIT}{2}[ ]{LOGHOST} nf_parse_start_hn(nftext+5);
{DIGIT}{4}[ ]{MONTH}[ ]{1,2}{DIGIT}{1,2}[ ]{DIGIT}{2}:{DIGIT}{2}:{DIGIT}{2} nf_parse_start(nftext+5);
{DIGIT}{4}:{DIGIT}{2}:{DIGIT}{2}-{DIGIT}{2}:{DIGIT}{2}:{DIGIT}{2}[ ]{LOGHOST} nf_parse_isostart_hn(nftext);
" kernel: IN="{STRING}? nf_parse_prefix(nftext+12, NF_OPT_NOPREFIX);
" kernel: "{PREFIX}{STRING}? nf_parse_prefix(nftext+9, NF_OPT_PREFIX);
" [kernel] IN="{STRING}? nf_parse_prefix(nftext+13, NF_OPT_NOPREFIX);
" [kernel] "{PREFIX}{STRING}? nf_parse_prefix(nftext+10, NF_OPT_PREFIX);
" klogd: IN="{STRING}? nf_parse_prefix(nftext+11, NF_OPT_NOPREFIX);
" klogd: "{PREFIX}{STRING}? nf_parse_prefix(nftext+8, NF_OPT_PREFIX);
" "{PREFIX}{STRING}? nf_parse_prefix(nftext+1, NF_OPT_PREFIX);
"IN="{STRING}? nf_parse_prefix(nftext+3, NF_OPT_NOPREFIX);
"OUT="{STRING}? /* ignore */
"PHYSIN="{STRING}? /* ignore */
"PHYSOUT="{STRING}? /* ignore */
"MAC="(({HEXDIGIT}{HEXDIGIT}:){13}{HEXDIGIT}{HEXDIGIT})? /* ignore */
"SRC="{OCTET}"."{OCTET}"."{OCTET}"."{OCTET} nf_parse_ip(nftext+4, NF_OPT_SRC);
"DST="{OCTET}"."{OCTET}"."{OCTET}"."{OCTET} nf_parse_ip(nftext+4, NF_OPT_DST);
"LEN="{NUMBER} opt.line->datalen = atoi(nftext+4);
"TOS="({HEXNUM}|"00") /* ignore */
"PREC="{HEXNUM} /* ignore */
"TTL="{NUMBER} /* ignore */
"ID="{NUMBER} /* ignore */
"CE" /* ignore */
"DF" /* ignore */
"MF" /* ignore */
"FRAG:"{NUMBER} /* ignore */
"PROTO="{PROTO} nf_parse_proto(nftext+6);
"INCOMPLETE ["{NUMBER}" bytes]" /* ignore */
"TYPE="{NUMBER} { opt.line->sport = atoi(nftext+5); opt.parser=opt.parser|NF_TYPE; }
"CODE="{NUMBER} { opt.line->dport = atoi(nftext+5); opt.parser=opt.parser|NF_CODE; }
"SEQ="{NUMBER} /* ignore */
"ACK="{NUMBER} /* ignore */
"SPT="{PORT} { opt.line->sport = atoi(nftext+4); opt.parser=opt.parser|NF_SPT; }
"DPT="{PORT} { opt.line->dport = atoi(nftext+4); opt.parser=opt.parser|NF_DPT; }
"WINDOW="{NUMBER} /* ignore */
"RES="{HEXNUM} /* ignore */
"URG" opt.line->flags = opt.line->flags | TCP_URG;
"ACK" opt.line->flags = opt.line->flags | TCP_ACK;
"PSH" opt.line->flags = opt.line->flags | TCP_PSH;
"RST" opt.line->flags = opt.line->flags | TCP_RST;
"SYN" opt.line->flags = opt.line->flags | TCP_SYN;
"FIN" opt.line->flags = opt.line->flags | TCP_FIN;
"ECE" /* ignore */
"CWR" /* ignore */
"URGP="{NUMBER} /* ignore */
"OPT ("[0-9A-F]*")" /* ignore */
"SPI="{HEXNUM} /* ignore */
"GATEWAY="{OCTET}"."{OCTET}"."{OCTET}"."{OCTET} /* ignore */
"TC="{NUMBER} /* ignore */
"HOPLIMIT="{NUMBER} /* ignore */
"FLOWLBL="{NUMBER} /* ignore */
"[".+"]" /* ignore */
[ ]+ /* ignore whitespace */
[\n] return 0;
{STRING} if(opt.verbose) fprintf(stderr, "Unrecognized token: %s\n", nftext);
. if(opt.verbose) fprintf(stderr, "Unrecognized character: %s\n", nftext);
%%
void nf_parse_start_hn(char *input)
{
int retval, day, hour, minute, second;
char smonth[3];
retval = sscanf(input,
"%3s %2d %2d:%2d:%2d %32s",
smonth, &day, &hour, &minute, &second,
opt.line->hostname);
if (retval != 6) {
return;
}
build_time(smonth, day, hour, minute, second);
opt.parser=opt.parser|NF_DATE;
}
void nf_parse_start(char *input)
{
int retval, day, hour, minute, second;
char smonth[3];
retval = sscanf(input,
"%3s %2d %2d:%2d:%2d",
smonth, &day, &hour, &minute, &second);
if (retval != 5) {
return;
}
build_time(smonth, day, hour, minute, second);
xstrncpy(opt.line->hostname, "-", SHORTLEN);
opt.parser=opt.parser|NF_DATE;
}
void nf_parse_isostart_hn(char *input)
{
int retval, year, month, day, hour, minute, second;
struct tm *t;
retval = sscanf(input,
"%4d:%2d:%2d-%2d:%2d:%2d %32s",
&year, &month, &day, &hour, &minute, &second,
opt.line->hostname);
if (retval != 7) {
return;
}
t = xmalloc(sizeof(struct tm));
t->tm_year = year-1900;
t->tm_mon = month-1;
t->tm_mday = day;
t->tm_hour = hour;
t->tm_min = minute;
t->tm_sec = second;
t->tm_isdst = -1;
opt.line->time = mktime(t);
free(t);
opt.parser=opt.parser|NF_DATE;
}
void nf_parse_prefix(char *input, unsigned char mode)
{
size_t i=0;
if(mode == NF_OPT_PREFIX) {
i=strlen(input);
while (i > 0) {
if(input[i] == '=') {
if (input[i-1] == 'N') {
if (input[i-2] == 'I') {
input[i-2] = '\0';
xstrncpy(opt.line->chainlabel, input, SHORTLEN);
xstrncpy(opt.line->interface, input+i+1, SHORTLEN);
break;
}
}
}
i--;
}
} else {
xstrncpy(opt.line->chainlabel, "-", SHORTLEN);
xstrncpy(opt.line->interface, input, SHORTLEN);
}
opt.parser=opt.parser|NF_IN;
}
void nf_parse_ip(char *input, unsigned char mode)
{
if (mode == NF_OPT_SRC) {
if(convert_ip(input, &opt.line->shost) == IN_ADDR_ERROR) return;
opt.parser=opt.parser|NF_SRC;
} else {
if(convert_ip(input, &opt.line->dhost) == IN_ADDR_ERROR) return;
opt.parser=opt.parser|NF_DST;
}
}
void nf_parse_proto(char *input)
{
if(isdigit((int)input[0])) {
opt.line->protocol = atoi(input);
} else {
if(strncmp(input, "TCP", 3) == 0) opt.line->protocol = 6;
else if(strncmp(input, "UDP", 3) == 0) opt.line->protocol = 17;
else if(strncmp(input, "ICMP", 4) == 0) opt.line->protocol = 1;
else if(strncmp(input, "ESP", 3) == 0) opt.line->protocol = 50;
else if(strncmp(input, "AH", 2) == 0) opt.line->protocol = 51;
else if(strncmp(input, "ICMPv6", 6) == 0) opt.line->protocol = 58;
}
if (opt.line->protocol != 0)
opt.parser=opt.parser|NF_PROTO;
}
unsigned char flex_netfilter(char *input, int linenum)
{
opt.parser = 0;
init_line();
nf_scan_string(input);
nflex();
nf_delete_buffer(YY_CURRENT_BUFFER);
xstrncpy(opt.line->branchname, "-", SHORTLEN);
opt.line->count = 1;
if (((opt.line->protocol == 6) || (opt.line->protocol == 17)) && (opt.parser == (NF_DATE|NF_PROTO|NF_IN|NF_SRC|NF_DST|NF_SPT|NF_DPT))) {
return PARSE_OK;
}
if ((opt.line->protocol == 1) && (opt.parser == (NF_DATE|NF_PROTO|NF_IN|NF_SRC|NF_DST|NF_TYPE|NF_CODE))) {
return PARSE_OK;
}
if (((opt.line->protocol == 2) || (opt.line->protocol == 9) || (opt.line->protocol == 41) || (opt.line->protocol == 47) || (opt.line->protocol == 50) || (opt.line->protocol == 54)) && (opt.parser == (NF_DATE|NF_PROTO|NF_IN|NF_SRC|NF_DST))) {
return PARSE_OK;
}
if(opt.verbose)
fprintf(stderr, "netfilter parse error in line %d, ignoring.\n", linenum);
if(opt.verbose == 2)
fprintf(stderr, "input was: \"%s\"\n", input);
return PARSE_WRONG_FORMAT;
}
|