blib/lib/Linux/PacketFilter.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 84 CC 3 SCAR 15.5
CC
3
Cov
0%
new
12.0
_populate_BPF
2.0
attach
2.0
CRAP 4.7
1 package Linux::PacketFilter;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.01_1';
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. (See below for what “k8” means.)
24 [ 'jmp jeq k8', 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 my %BPF;
68  
69 sub _populate_BPF {
70 %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_n8 => 0x00,
81 k_n16 => 0x00,
82 k_n32 => 0x00,
83 );
84  
85 # ld = to accumulator
86 # ldx = to index
87 # st = accumulator to scratch[k]
88 # stx = index to scratch[k]
89 my @inst = qw( ld ldx st stx alu jmp ret misc );
90 for my $i ( 0 .. $#inst ) {
91 $BPF{ $inst[$i] } = $i;
92 }
93  
94 # Load accumulator:
95 # imm = k
96 # abs = offset into packet
97 # ind = index + k
98 # mem = scratch[k]
99 # len = packet length
100 # msh = IP header length (hack ..)
101 my @code = qw( imm abs ind mem len msh );
102 for my $i ( 0 .. $#code ) {
103 $BPF{ $code[$i] } = ( $i << 5 );
104 }
105  
106 my @alu = qw( add sub mul div or and lsh rsh neg mod xor );
107 for my $i ( 0 .. $#alu ) {
108 $BPF{ $alu[$i] } = ( $i << 4 );
109 }
110  
111 # ja = move forward k
112 # jeq = move (A == k) ? jt : jf
113 # jset = (A & k)
114 my @j = qw( ja jeq jgt jge jset );
115 for my $i ( 0 .. $#j ) {
116 $BPF{ $j[$i] } = ( $i << 4 );
117 }
118  
119 return;
120 }
121  
122 =head1 METHODS
123  
124 =head2 $obj = I<CLASS>->new( @filters )
125  
126 Creates an object that represents an array of instructions for
127 the BPF filter machine. Each @filters member is an array reference
128 that represents a single instruction and has either 2 or 4 members,
129 which correspond with the BPF_STMT and BPF_JUMP macros, respectively.
130  
131 The first member of each array reference is, rather than a number,
132 a space-separated string of options, lower-cased and without the
133 leading C<BPF_>. So where in C you would write:
134  
135 BPF_LD | BPF_W | BPF_ABS
136  
137 ... in this module you write:
138  
139 'ld w abs'
140  
141 The full list of options for a single instruction is:
142  
143 =over
144  
145 =item * C<b>, C<h>, C<w>, C<x>, C<k>, C<k_n>, C<k_N> (See below for
146 an explanation of the last two.)
147  
148 =item * C<ld>, C<ldx>, C<st>, C<stx>, C<alu>, C<jmp>, C<ret>, C<misc>
149  
150 =item * C<imm>, C<abs>, C<ind>, C<mem>, C<len>, C<msh>
151  
152 =item * C<add>, C<sub>, C<mul>, C<div>, C<or>, C<and>, C<lsh>, C<rsh>,
153 C<neg>, C<mod>, C<xor>
154  
155 =item * C<ja>, C<jeq>, C<jgt>, C<jge>, C<jset>
156  
157 =back
158  
159 =head3 Byte order conversion
160  
161 Since it’s common to need to do byte order conversions with
162 packet filtering, Linux::PacketFilter adds a convenience for this:
163 the codes C<k_n> and C<k_N> indicate to encode the given constant value
164 in 16-bit or 32-bit network byte order, respectively.
165  
166 Note that Linux I<always> consumes BPF instruction constants in
167 B<network order>. Thus, if you’re on a little-endian system, to
168 match against numbers that are in host order (e.g., numbers in Netlink
169 headers) you’ll need to do a byte-order conversion.
170  
171 To add to the fun: when BPF compares a 16- or 8-bit number from “k”,
172 it expects to do so from the first available register. This works fine
173 on little-endian systems, but on big-endian systems that means
174 It would be more natural for this module to encode the constants
175 in network order; however, that would also put it at variance with C
176 implementations, which would compromise the usefulness of existing
177 documentation.
178  
179 =cut
180  
181 use constant _is_big_endian => pack('n', 1) eq pack('S', 1);
182  
183 use constant {
184 _INSTR_PACK => 'S CC L',
185  
186 _NETWORK_INSTR_PACK => {
187 'k_n8' => _is_big_endian ? 'S CC N' : 'S CC C x3',
188 'k_n16' => _is_big_endian ? 'S CC N' : 'S CC n x2',
189 'k_n32' => 'S CC N',
190 },
191  
192 _ARRAY_PACK => 'S x![P] P',
193 };
194  
195 use constant _INSTR_LEN => length( pack _INSTR_PACK() );
196  
197 sub new {
198 my $class = shift;
199  
200 _populate_BPF() if !%BPF;
201  
202 my $buf = ("\0" x (_INSTR_LEN() * @_));
203  
204 my $f = 0;
205  
206 for my $filter (@_) {
207 my $code = 0;
208  
209 my $tmpl;
210  
211 for my $piece ( split m<\s+>, $filter->[0] ) {
212 $code |= ($BPF{$piece} // die "Unknown BPF option: “$piece”");
213  
214 $tmpl ||= _NETWORK_INSTR_PACK()->{$piece};
215 }
216  
217 substr(
218 $buf, $f, _INSTR_LEN(),
219 pack(
220 ( $tmpl || _INSTR_PACK() ),
221 $code,
222 (@$filter == 2) ? (0, 0, $filter->[1]) : @{$filter}[2, 3, 1],
223 ),
224 );
225  
226 $f += _INSTR_LEN();
227 }
228  
229 return bless [ pack(_ARRAY_PACK(), 0 + @_, $buf), $buf ], $class;
230 }
231  
232 =head2 $ok = I<OBJ>->attach( $SOCKET )
233  
234 Attaches the filter instructions to the given $SOCKET.
235  
236 Note that this class purposely omits public access to the value that
237 is given to the underlying L<setsockopt(2)> system call. This is because
238 that value contains a pointer to a Perl string. That pointer is only valid
239 during this object’s lifetime, and bad stuff (e.g., segmentation faults)
240 can happen when you give the kernel pointers to strings that Perl has
241 already garbage-collected.
242  
243 The return is the same as the underlying call to Perl’s
244 L<perlfunc/setsockopt> built-in. C<$!> is set as that function leaves it.
245  
246 =cut
247  
248 sub attach {
249 my ($self, $socket) = @_;
250  
251 # For no good reason, Perl require() clobbers $@ and $!.
252 do {
253 local ($@, $!);
254 require Socket;
255 };
256  
257 return setsockopt $socket, Socket::SOL_SOCKET(), Socket::SO_ATTACH_FILTER(), $self->[0];
258 }
259  
260 #----------------------------------------------------------------------
261  
262 1;
263  
264 =head1 AUTHOR
265  
266 Copyright 2019 Gasper Software Consulting (L<http://gaspersoftware.com>)
267  
268 =head1 SEE ALSO
269  
270 L<Linux::SocketFilter::Assembler> suits a similar purpose to this
271 module’s but appears to be geared solely toward PF_PACKET sockets.
272 It also defines its own language for specifying the filters, which I find
273 less helpful than this module’s approach of “porting” the C macros
274 to Perl, thus better capitalizing on existing documention.