Skip to content

Commit

Permalink
New filter "ifindex" for LINUX_SLL2 and live Linux captures
Browse files Browse the repository at this point in the history
  • Loading branch information
fenner committed May 12, 2020
1 parent 8c263fd commit 8c811d6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
47 changes: 47 additions & 0 deletions gencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -8175,6 +8175,53 @@ gen_multicast(compiler_state_t *cstate, int proto)
/*NOTREACHED*/
}

struct block *
gen_ifindex(compiler_state_t *cstate, int ifindex)
{
register struct block *b0;

/*
* Catch errors reported by us and routines below us, and return NULL
* on an error.
*/
if (setjmp(cstate->top_ctx))
return (NULL);

/*
* Only some data link types support ifindex qualifiers.
*/
switch (cstate->linktype) {
case DLT_LINUX_SLL2:
/* match packets on this interface */
b0 = gen_cmp(cstate, OR_LINKHDR, 4, BPF_W, ifindex);
break;
default:
#if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
/*
* This is Linux with PF_PACKET support.
* If this is a *live* capture, we can look at
* special meta-data in the filter expression;
* if it's a savefile, we can't.
*/
if (cstate->bpf_pcap->rfile != NULL) {
/* We have a FILE *, so this is a savefile */
bpf_error(cstate, "ifindex not supported on %s when reading savefiles",
pcap_datalink_val_to_description_or_dlt(cstate->linktype));
b0 = NULL;
/*NOTREACHED*/
}
/* match ifindex */
b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_IFINDEX, BPF_W,
ifindex);
#else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
bpf_error(cstate, "ifindex not supported on %s",
pcap_datalink_val_to_description_or_dlt(cstate->linktype));
/*NOTREACHED*/
#endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
}
return (b0);
}

/*
* Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
* Outbound traffic is sent by this machine, while inbound traffic is
Expand Down
1 change: 1 addition & 0 deletions gencode.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ struct block *gen_greater(compiler_state_t *, int);
struct block *gen_byteop(compiler_state_t *, int, int, bpf_u_int32);
struct block *gen_broadcast(compiler_state_t *, int);
struct block *gen_multicast(compiler_state_t *, int);
struct block *gen_ifindex(compiler_state_t *, int);
struct block *gen_inbound(compiler_state_t *, int);

struct block *gen_llc(compiler_state_t *);
Expand Down
2 changes: 2 additions & 0 deletions grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ DIAG_OFF_BISON_BYACC
%token ATALK AARP DECNET LAT SCA MOPRC MOPDL
%token TK_BROADCAST TK_MULTICAST
%token NUM INBOUND OUTBOUND
%token IFINDEX
%token PF_IFNAME PF_RSET PF_RNR PF_SRNR PF_REASON PF_ACTION
%token TYPE SUBTYPE DIR ADDR1 ADDR2 ADDR3 ADDR4 RA TA
%token LINK
Expand Down Expand Up @@ -581,6 +582,7 @@ other: pqual TK_BROADCAST { CHECK_PTR_VAL(($$ = gen_broadcast(cstate, $1))); }
| CBYTE NUM byteop NUM { CHECK_PTR_VAL(($$ = gen_byteop(cstate, $3, $2, $4))); }
| INBOUND { CHECK_PTR_VAL(($$ = gen_inbound(cstate, 0))); }
| OUTBOUND { CHECK_PTR_VAL(($$ = gen_inbound(cstate, 1))); }
| IFINDEX NUM { CHECK_PTR_VAL(($$ = gen_ifindex(cstate, $2))); }
| VLAN pnum { CHECK_PTR_VAL(($$ = gen_vlan(cstate, $2, 1))); }
| VLAN { CHECK_PTR_VAL(($$ = gen_vlan(cstate, 0, 0))); }
| MPLS pnum { CHECK_PTR_VAL(($$ = gen_mpls(cstate, $2, 1))); }
Expand Down
6 changes: 6 additions & 0 deletions pcap-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -5923,6 +5923,12 @@ fix_offset(pcap_t *handle, struct bpf_insn *p)
* special magic kernel offset for that field.
*/
p->k = SKF_AD_OFF + SKF_AD_PROTOCOL;
} else if (p->k == 4) {
/*
* It's the ifindex field; map it to the
* special magic kernel offset for that field.
*/
p->k = SKF_AD_OFF + SKF_AD_IFINDEX;
} else if (p->k == 10) {
/*
* It's the packet type field; map it to the
Expand Down
2 changes: 2 additions & 0 deletions scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ len|length return LEN;
inbound return INBOUND;
outbound return OUTBOUND;

ifindex return IFINDEX;

vlan return VLAN;
mpls return MPLS;
pppoed return PPPOED;
Expand Down

0 comments on commit 8c811d6

Please sign in to comment.