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
|
/* Copyright (C) 2000-2004 Boris Wesslowski */
/* $Id: netfilter.l,v 1.36 2004/03/22 21:59:24 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(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 [ -OQ-~-]+|([ -OQ-~-]*P[ -GI-~-]*)|([ -OQ-~-]*PH[ -XZ-~-]*)|([ -OQ-~-]*PHY[ -RT-~-]*)
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(nftext);
" kernel: IN="{STRING}? nf_parse_prefix(nftext+12, NF_OPT_NOPREFIX);
" kernel: "{PREFIX}"IN="{STRING}? nf_parse_prefix(nftext+9, NF_OPT_PREFIX);
" klogd: IN="{STRING}? nf_parse_prefix(nftext+11, NF_OPT_NOPREFIX);
" klogd: "{PREFIX}"IN="{STRING}? nf_parse_prefix(nftext+8, NF_OPT_PREFIX);
" "{PREFIX}"IN="{STRING}? nf_parse_prefix(nftext+1, NF_OPT_PREFIX);
"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} /* 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} /* ignore */
"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(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_prefix(char *input, unsigned char mode)
{
size_t i=0;
if(mode == NF_OPT_PREFIX) {
while (i < strlen(input)) {
if(input[i] == 'I') {
if (input[i+1] == 'N') {
if (input[i+2] == '=') {
input[i] = '\0';
xstrncpy(opt.line->chainlabel, input, SHORTLEN);
xstrncpy(opt.line->interface, input+i+3, 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))) {
return PARSE_OK;
}
if (((opt.line->protocol == 2) || (opt.line->protocol == 9) || (opt.line->protocol == 47) || (opt.line->protocol == 50)) && (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;
}
|