| 1 | package Linux::PacketFilter; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.01'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =encoding utf-8 | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | Linux::PacketFilter - Simple interface to Linux packet filtering | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | # Reject any packet that starts with a period: | ||||||||||||||||||||
| 17 | my $filter = Linux::PacketFilter->new( | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # Load the accumulator with the 1st byte in the packet: | ||||||||||||||||||||
| 20 | [ 'ld b abs', 0 ], | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | # If the accumulator value is an ASCII period, continue; | ||||||||||||||||||||
| 23 | # otherwise, skip a line. | ||||||||||||||||||||
| 24 | [ 'jmp jeq k', ord('.'), 0, 1 ], | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | # If we continued, we’ll get here and thus reject the packet. | ||||||||||||||||||||
| 27 | [ ret => 0 ], | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # If we get here, we skipped a line above. That means | ||||||||||||||||||||
| 30 | # the packet’s first byte wasn’t an ASCII period, | ||||||||||||||||||||
| 31 | # so we'll return the full packet. | ||||||||||||||||||||
| 32 | [ ret => 0xffffffff ], | ||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | $filter->apply( $socket ); | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | This module is a simple, small, pure-Perl compiler for Linux’s | ||||||||||||||||||||
| 40 | “classic” Berkeley Packet Filter (BPF) implementation. | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =head1 HOW TO USE THIS MODULE | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | If you’re familiar with BPF already, the SYNOPSIS above should mostly make | ||||||||||||||||||||
| 45 | sense “out-of-the-box”. If you’re new to BPF, though, take heart; it’s | ||||||||||||||||||||
| 46 | fairly straightforward. | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | The best source I have found for learning about BPF itself is | ||||||||||||||||||||
| 49 | L<bpf(4) in the BSD man pages|https://man.openbsd.org/bpf.4#Filter_machine>; | ||||||||||||||||||||
| 50 | see the section entitled B<FILTER MACHINE>. | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | Linux-specific implementation notes are available in the kernel | ||||||||||||||||||||
| 53 | source tree at L</Documentation/networking/filter.txt|https://www.kernel.org/doc/Documentation/networking/filter.txt>. This contains a lot of detail | ||||||||||||||||||||
| 54 | about uses for BPF that don't pertain to packet filtering, though. | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | L<Here is another helpful guide.|https://web.archive.org/web/20130125231050/http://netsplit.com/2011/02/09/the-proc-connector-and-socket-filters/> Take | ||||||||||||||||||||
| 57 | especial note of the need to convert between network and host byte order. | ||||||||||||||||||||
| 58 | (See below for a convenience that this module provides for this conversion.) | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | You might also take interest in L<the original BPF white paper|http://www.tcpdump.org/papers/bpf-usenix93.pdf>. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | B<NOTE:> This module works with Linux’s I<“classic”> BPF, not the | ||||||||||||||||||||
| 63 | much more powerful (and complex) “extended” BPF. | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 2 | my %BPF; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 2 | ▶ | sub _populate_BPF { | ||||||||||||||||||
|
Subroutine
_populate_BPF: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 70 | 2 | %BPF = ( | |||||||||||||||||||
| 71 | w => 0x00, # 32-bit word | ||||||||||||||||||||
| 72 | h => 0x08, # 16-bit half-word | ||||||||||||||||||||
| 73 | b => 0x10, # 8-bit byte | ||||||||||||||||||||
| 74 | # dw => 0x18, # 64-bit double word | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | k => 0x00, # given constant | ||||||||||||||||||||
| 77 | x => 0x08, # index register | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | # Conveniences: | ||||||||||||||||||||
| 80 | k_n => 0x00, | ||||||||||||||||||||
| 81 | k_N => 0x00, | ||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # ld = to accumulator | ||||||||||||||||||||
| 85 | # ldx = to index | ||||||||||||||||||||
| 86 | # st = accumulator to scratch[k] | ||||||||||||||||||||
| 87 | # stx = index to scratch[k] | ||||||||||||||||||||
| 88 | 2 | my @inst = qw( ld ldx st stx alu jmp ret misc ); | |||||||||||||||||||
| 89 | 2 | for my $i ( 0 .. $#inst ) { | |||||||||||||||||||
| 90 | 16 | $BPF{ $inst[$i] } = $i; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | # Load accumulator: | ||||||||||||||||||||
| 94 | # imm = k | ||||||||||||||||||||
| 95 | # abs = offset into packet | ||||||||||||||||||||
| 96 | # ind = index + k | ||||||||||||||||||||
| 97 | # mem = scratch[k] | ||||||||||||||||||||
| 98 | # len = packet length | ||||||||||||||||||||
| 99 | # msh = IP header length (hack ..) | ||||||||||||||||||||
| 100 | 2 | my @code = qw( imm abs ind mem len msh ); | |||||||||||||||||||
| 101 | 2 | for my $i ( 0 .. $#code ) { | |||||||||||||||||||
| 102 | 12 | $BPF{ $code[$i] } = ( $i << 5 ); | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 2 | my @alu = qw( add sub mul div or and lsh rsh neg mod xor ); | |||||||||||||||||||
| 106 | 2 | for my $i ( 0 .. $#alu ) { | |||||||||||||||||||
| 107 | 22 | $BPF{ $alu[$i] } = ( $i << 4 ); | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # ja = move forward k | ||||||||||||||||||||
| 111 | # jeq = move (A == k) ? jt : jf | ||||||||||||||||||||
| 112 | # jset = (A & k) | ||||||||||||||||||||
| 113 | 2 | my @j = qw( ja jeq jgt jge jset ); | |||||||||||||||||||
| 114 | 2 | for my $i ( 0 .. $#j ) { | |||||||||||||||||||
| 115 | 10 | $BPF{ $j[$i] } = ( $i << 4 ); | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 2 | return; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | =head1 METHODS | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =head2 $obj = I<CLASS>->new( @filters ) | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | Creates an object that represents an array of instructions for | ||||||||||||||||||||
| 126 | the BPF filter machine. Each @filters member is an array reference | ||||||||||||||||||||
| 127 | that represents a single instruction and has either 2 or 4 members, | ||||||||||||||||||||
| 128 | which correspond with the BPF_STMT and BPF_JUMP macros, respectively. | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | The first member of each array reference is, rather than a number, | ||||||||||||||||||||
| 131 | a space-separated string of options, lower-cased and without the | ||||||||||||||||||||
| 132 | leading C<BPF_>. So where in C you would write: | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | BPF_LD | BPF_W | BPF_ABS | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | … in this module you write: | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 'ld w abs' | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | The full list of options for a single instruction is: | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =over | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =item * C<b>, C<h>, C<w> | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =item * C<x>, C<k>, C<k_n>, C<k_N> (See below for | ||||||||||||||||||||
| 147 | an explanation of the last two.) | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =item * C<ld>, C<ldx>, C<st>, C<stx>, C<alu>, C<jmp>, C<ret>, C<misc> | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =item * C<imm>, C<abs>, C<ind>, C<mem>, C<len>, C<msh> | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =item * C<add>, C<sub>, C<mul>, C<div>, C<or>, C<and>, C<lsh>, C<rsh>, | ||||||||||||||||||||
| 154 | C<neg>, C<mod>, C<xor> | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =item * C<ja>, C<jeq>, C<jgt>, C<jge>, C<jset> | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =back | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =head3 Byte order conversion | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | Since it’s common to need to do byte order conversions with | ||||||||||||||||||||
| 163 | packet filtering, Linux::PacketFilter adds a convenience for this: | ||||||||||||||||||||
| 164 | the codes C<k_n> and C<k_N> indicate to encode the given constant value | ||||||||||||||||||||
| 165 | in 16-bit or 32-bit network byte order, respectively. These have the same | ||||||||||||||||||||
| 166 | effect as calling C<htons(3)> and C<htonl(3)> in C. | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | B<NOTE:> Linux’s exact behavior regarding byte order in BPF isn’t | ||||||||||||||||||||
| 169 | always clear, and this module is only tested thus far on little-endian | ||||||||||||||||||||
| 170 | systems. It seems that only certain operations, like C<jeq>, require the | ||||||||||||||||||||
| 171 | conversion. | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | =cut | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 2 | ▶ | use constant _is_big_endian => pack('n', 1) eq pack('S', 1); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 2 | ▶ | use constant { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 178 | 2 | _INSTR_PACK => 'S CC L', | |||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | _NETWORK_INSTR_PACK => { | ||||||||||||||||||||
| 181 | 'k_n' => _is_big_endian ? 'S CC N' : 'S CC n x2', | ||||||||||||||||||||
| 182 | 'k_N' => 'S CC N', | ||||||||||||||||||||
| 183 | }, | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | _ARRAY_PACK => 'S x![P] P', | ||||||||||||||||||||
| 186 | 2 | }; | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 2 | ▶ | use constant _INSTR_LEN => length( pack _INSTR_PACK() ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 7 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||
| 191 | 7 | my $class = shift; | |||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 7 | ▶ | _populate_BPF() if !%BPF; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 7 | my $buf = ("\0" x (_INSTR_LEN() * @_)); | |||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 7 | my $f = 0; | |||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 7 | for my $filter (@_) { | |||||||||||||||||||
| 200 | 26 | my $code = 0; | |||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | 26 | my $tmpl; | |||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | 26 | for my $piece ( split m<\s+>, $filter->[0] ) { | |||||||||||||||||||
| 205 | 64 | ▶ | $code |= ($BPF{$piece} // die "Unknown BPF option: “$piece”"); | ||||||||||||||||||
|
Condition50%1/2
$BPF{$piece} // die("Unknown BPF option: \342\200\234$piece\342\200\235")
MC/DC0%0/1
$BPF{$piece} // die("Unknown BPF option: \342\200\234$piece\342\200\235")
$BPF{$piece}
Truth table50%1/2
A
A= $BPF{$piece}
| |||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 64 | ▶ | $tmpl ||= _NETWORK_INSTR_PACK()->{$piece}; | ||||||||||||||||||
|
Condition66%2/3
$tmpl ||= {"k_N" => "S CC N","k_n" => "S CC n x2"}->{$piece}
MC/DC50%1/2
$tmpl ||= {"k_N" => "S CC N","k_n" => "S CC n x2"}->{$piece}
$tmpl
{"k_N" => "S CC N","k_n" => "S CC n x2"}->{$piece}
Truth table66%2/3
A ||= B
A= $tmplB= {"k_N" => "S CC N","k_n" => "S CC n x2"}->{$piece}
| |||||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | substr( | ||||||||||||||||||||
| 211 | $buf, $f, _INSTR_LEN(), | ||||||||||||||||||||
| 212 | pack( | ||||||||||||||||||||
| 213 | ( $tmpl || _INSTR_PACK() ), | ||||||||||||||||||||
| 214 | $code, | ||||||||||||||||||||
| 215 | 26 | ▶ | (@$filter == 2) ? (0, 0, $filter->[1]) : @{$filter}[2, 3, 1], | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$tmpl || 'S CC L'
MC/DC100%1/1
$tmpl || 'S CC L'
$tmpl
Truth table100%2/2
A
A= $tmpl
| |||||||||||||||||||||
| 216 | ), | ||||||||||||||||||||
| 217 | ); | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 26 | $f += _INSTR_LEN(); | |||||||||||||||||||
| 220 | } | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 7 | return bless [ pack(_ARRAY_PACK(), 0 + @_, $buf), $buf ], $class; | |||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =head2 $ok = I<OBJ>->attach( $SOCKET ) | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | Attaches the filter instructions to the given $SOCKET. | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | Note that this class purposely omits public access to the value that | ||||||||||||||||||||
| 230 | is given to the underlying L<setsockopt(2)> system call. This is because | ||||||||||||||||||||
| 231 | that value contains a pointer to a Perl string. That pointer is only valid | ||||||||||||||||||||
| 232 | during this object’s lifetime, and bad stuff (e.g., segmentation faults) | ||||||||||||||||||||
| 233 | can happen when you give the kernel pointers to strings that Perl has | ||||||||||||||||||||
| 234 | already garbage-collected. | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | The return is the same as the underlying call to Perl’s | ||||||||||||||||||||
| 237 | L<perlfunc/setsockopt> built-in. C<$!> is set as that function leaves it. | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =cut | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | 6 | ▶ | sub attach { | ||||||||||||||||||
|
Subroutine
attach: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 242 | 6 | my ($self, $socket) = @_; | |||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | # For no good reason, Perl require() clobbers $@ and $!. | ||||||||||||||||||||
| 245 | 6 | do { | |||||||||||||||||||
| 246 | 6 | local ($@, $!); | |||||||||||||||||||
| 247 | 6 | require Socket; | |||||||||||||||||||
| 248 | }; | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | 6 | return setsockopt $socket, Socket::SOL_SOCKET(), Socket::SO_ATTACH_FILTER(), $self->[0]; | |||||||||||||||||||
| 251 | } | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | #---------------------------------------------------------------------- | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | 2 | 1; | |||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | =head1 AUTHOR | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | Copyright 2019 Gasper Software Consulting (L<http://gaspersoftware.com>) | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | =head1 SEE ALSO | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | L<Linux::SocketFilter::Assembler> suits a similar purpose to this | ||||||||||||||||||||
| 264 | module’s but appears to be geared solely toward PF_PACKET sockets. | ||||||||||||||||||||
| 265 | It also defines its own language for specifying the filters, which I find | ||||||||||||||||||||
| 266 | less helpful than this module’s approach of “porting” the C macros | ||||||||||||||||||||
| 267 | to Perl, thus better capitalizing on existing documention. | ||||||||||||||||||||