Skip to content

Commit ec3f702

Browse files
author
Stefan Baranoff
committed
WIP/RFC: add support for manual link layer offset changes
A few pcap-filter expressions implicitly change the link layer offset. There are other less common headers without their own keywords that might require skipping over some bytes in a link layer adjacent header to get to higher layer network headers. This commit adds an "offset" term that takes a + or - value to adjust the link layer offets in the same way VLAN does. This allows for something like `ethertype 0x8926 and offset +6 and vlan 14 and ip host 192.0.2.14` to skip over a vntag header, process a VLAN header, and look for an IPv4 host.
1 parent 0170672 commit ec3f702

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

gencode.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10712,3 +10712,11 @@ gen_atmmulti_abbrev(compiler_state_t *cstate, int type)
1071210712
}
1071310713
return b1;
1071410714
}
10715+
10716+
struct block *gen_offset_adjustment(compiler_state_t *cstate, int n) {
10717+
struct block *b = new_block(cstate, BPF_JMP|BPF_JA);
10718+
b->s.k = 0; // Jump by 0 bytes, effectively a no-op
10719+
cstate->off_linkpl.constant_part += n;
10720+
cstate->off_linktype.constant_part += n;
10721+
return b;
10722+
}

gencode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ struct block *gen_pf_action(compiler_state_t *, int);
378378
struct block *gen_p80211_type(compiler_state_t *, bpf_u_int32, bpf_u_int32);
379379
struct block *gen_p80211_fcdir(compiler_state_t *, bpf_u_int32);
380380

381+
struct block *gen_offset_adjustment(compiler_state_t*, int);
382+
381383
/*
382384
* Representation of a program as a tree of blocks, plus current mark.
383385
* A block is marked if only if its mark equals the current mark.

grammar.y.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,14 @@ DIAG_OFF_BISON_BYACC
401401
%token RADIO
402402
%token FISU LSSU MSU HFISU HLSSU HMSU
403403
%token SIO OPC DPC SLS HSIO HOPC HDPC HSLS
404+
%token OFFSET OFFSET_PLUS OFFSET_MINUS
404405
%token LEX_ERROR
405406

406407
%type <s> ID EID AID
407408
%type <s> HID HID6
408409
%type <h> NUM
409410
%type <i> action reason type subtype type_subtype dir
411+
%type <h> OFFSET_PLUS OFFSET_MINUS
410412

411413
%left OR AND
412414
%nonassoc '!'
@@ -689,6 +691,8 @@ other: pqual TK_BROADCAST { CHECK_PTR_VAL(($$ = gen_broadcast(cstate, $1))); }
689691
| GENEVE { CHECK_PTR_VAL(($$ = gen_geneve(cstate, 0, 0))); }
690692
| VXLAN pnum { CHECK_PTR_VAL(($$ = gen_vxlan(cstate, $2, 1))); }
691693
| VXLAN { CHECK_PTR_VAL(($$ = gen_vxlan(cstate, 0, 0))); }
694+
| OFFSET OFFSET_PLUS { CHECK_PTR_VAL(($$ = gen_offset_adjustment(cstate, $2))); }
695+
| OFFSET OFFSET_MINUS { CHECK_PTR_VAL(($$ = gen_offset_adjustment(cstate, -$2))); }
692696
| pfvar { $$ = $1; }
693697
| pqual p80211 { $$ = $2; }
694698
| pllc { $$ = $1; }
@@ -942,4 +946,5 @@ mtp3fieldvalue: NUM {
942946
mtp3listvalue: mtp3fieldvalue
943947
| mtp3listvalue or mtp3fieldvalue { gen_or($1.b, $3.b); $$ = $3; }
944948
;
949+
945950
%%

scanner.l

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,11 @@ tcp-ack { yylval->h = 0x10; return NUM; }
471471
tcp-urg { yylval->h = 0x20; return NUM; }
472472
tcp-ece { yylval->h = 0x40; return NUM; }
473473
tcp-cwr { yylval->h = 0x80; return NUM; }
474+
475+
offset { return OFFSET; }
476+
\+[0-9]+ { stou(yytext+1, yylval, yyextra); return OFFSET_PLUS; }
477+
-[0-9]+ { stou(yytext+1, yylval, yyextra); return OFFSET_MINUS; }
478+
474479
[A-Za-z0-9]([-_.A-Za-z0-9]*[.A-Za-z0-9])? {
475480
yylval->s = sdup(yyextra, (char *)yytext); return ID; }
476481
"\\"[^ !()\n\t]+ { yylval->s = sdup(yyextra, (char *)yytext + 1); return ID; }

0 commit comments

Comments
 (0)