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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]

use libc::c_void;
#[cfg(not(windows))]
use libc::{c_char, c_int};

#[cfg(windows)]
mod windows_types {
    pub type u_char = u8;
    pub type u_short = u16;
    pub type uint = u32;
    pub type u_long = u32;
    pub type u_int8_t = u8;
    pub type u_int16_t = u16;
    pub type u_int32_t = u32;
    pub type int32_t = i32;
    pub type u_int64_t = u64;
    pub type int64_t = i64;
}

#[cfg(windows)]
use windows_types::*;

#[cfg(not(windows))]
mod unix_types {
    pub type u_int8_t = libc::c_uchar;
    pub type u_int16_t = libc::c_ushort;
    pub type u_int32_t = libc::c_uint;
    pub type int32_t = libc::c_int;
    pub type u_int64_t = libc::c_ulong;
    pub type int64_t = libc::c_long;
}

#[cfg(not(windows))]
use unix_types::*;

#[cfg(windows)]
use winapi::shared::ws2def::WSAData;
#[cfg(windows)]
use winapi::um::winsock2::{WSACleanup, WSAStartup, MAKEWORD};

#[repr(C)]
#[derive(Copy, Clone)]
pub union nbpf_in6_addr {
    pub addr8: [u_int8_t; 16],
    pub addr16: [u_int16_t; 8],
    pub addr32: [u_int32_t; 4],
}

#[repr(C, packed)]
pub union nbpf_ip_addr {
    pub v6: nbpf_in6_addr,
    pub v4: u_int32_t,
}

// Header qualifiers
pub const NBPF_Q_OUTER: u8 = 1;
pub const NBPF_Q_INNER: u8 = 2;

// Protocol qualifiers
pub const NBPF_Q_LINK: u8 = 1;
pub const NBPF_Q_IP: u8 = 2;
pub const NBPF_Q_SCTP: u8 = 3;
pub const NBPF_Q_TCP: u8 = 4;
pub const NBPF_Q_UDP: u8 = 5;
pub const NBPF_Q_IPV6: u8 = 6;
pub const NBPF_Q_GTP: u8 = 7;
pub const NBPF_Q_ICMP: u8 = 8;

// Direction qualifiers
pub const NBPF_Q_SRC: u8 = 1;
pub const NBPF_Q_DST: u8 = 2;
pub const NBPF_Q_OR: u8 = 3;
pub const NBPF_Q_AND: u8 = 4;

// Address qualifiers
pub const NBPF_Q_HOST: u8 = 1;
pub const NBPF_Q_NET: u8 = 2;
pub const NBPF_Q_PORT: u8 = 3;
pub const NBPF_Q_PROTO: u8 = 5;
pub const NBPF_Q_PORTRANGE: u8 = 7;
pub const NBPF_Q_VLAN: u8 = 8;
pub const NBPF_Q_MPLS: u8 = 9;
pub const NBPF_Q_L7PROTO: u8 = 10;
pub const NBPF_Q_PROTO_REL: u8 = 11;
pub const NBPF_Q_CUSTOM: u8 = 12;
pub const NBPF_Q_LOCAL: u8 = 13;
pub const NBPF_Q_REMOTE: u8 = 14;
pub const NBPF_Q_DEVICE: u8 = 15;
pub const NBPF_Q_INTERFACE: u8 = 16;

// Common qualifiers
pub const NBPF_Q_DEFAULT: u8 = 0;
pub const NBPF_Q_UNDEF: u8 = 255;

// Rel Op
pub const NBPF_R_EQ: u8 = 0; // ==
pub const NBPF_R_NE: u8 = 1; // !=
pub const NBPF_R_LT: u8 = 2; // <
pub const NBPF_R_LE: u8 = 4; // <=
pub const NBPF_R_GT: u8 = 3; // >
pub const NBPF_R_GE: u8 = 5; // >=

// Node types
pub const N_EMPTY: u8 = 0;
pub const N_PRIMITIVE: u8 = 1;
pub const N_AND: u8 = 2;
pub const N_OR: u8 = 3;

#[repr(C, packed)]
pub struct nbpf_qualifiers_t {
    pub header: u_int8_t,
    pub protocol: u_int8_t,
    pub direction: u_int8_t,
    pub address: u_int8_t,
}

#[repr(C, packed)]
pub struct nbpf_arth_t {
    pub protocol: c_int,
    pub offset: u_int16_t,
    pub mask: u_int8_t,
}

#[repr(C, packed)]
pub struct nbpf_node_t {
    pub type_: c_int,
    pub level: c_int,
    pub qualifiers: nbpf_qualifiers_t,
    pub not_rule: u_int8_t,
    pub device_id: u_int16_t,
    pub interface_id: u_int16_t,
    pub vlan_id_defined: u_int8_t,
    pub mpls_label_defined: u_int8_t,
    pub __padding: u_int8_t,
    pub vlan_id: u_int16_t,
    pub mpls_label: u_int16_t,
    pub mac: [u_int8_t; 6],
    pub ip6: [u_int8_t; 16],
    pub mask6: [u_int8_t; 16],
    pub ip: u_int32_t,
    pub mask: u_int32_t,
    pub port_from: u_int16_t,
    pub port_to: u_int16_t,
    pub protocol: u_int16_t,
    pub l7protocol: u_int16_t,
    pub byte_match: nbpf_byte_match_t,
    pub custom_key: *mut c_char,
    pub custom_value: *mut c_char,
    pub l: *mut nbpf_node_t,
    pub r: *mut nbpf_node_t,
}

#[repr(C, packed)]
pub struct nbpf_byte_match_t {
    pub protocol: u_int16_t,
    pub offset: u_int16_t,
    pub mask: u_int8_t,
    pub relop: u_int8_t,
    pub value: u_int8_t,
}

pub type nbpf_custom_node_callback = Option<
    unsafe extern "C" fn(key: *const c_char, value: *const c_char, user: *mut c_void) -> c_int,
>;
pub type nbpf_ip_locality_callback = Option<
    unsafe extern "C" fn(ip: *mut nbpf_ip_addr, ip_version: u_int8_t, user: *mut c_void) -> c_int,
>;

#[repr(C, packed)]
pub struct nbpf_tree_t {
    pub root: *mut nbpf_node_t,
    pub compatibility_level: c_int,
    pub default_pass: c_int,
    pub custom_callback: nbpf_custom_node_callback,
    pub locality_callback: nbpf_ip_locality_callback,
}

pub type l7protocol_by_name_func = Option<unsafe extern "C" fn(name: *const c_char) -> c_int>;

#[repr(C, packed)]
pub struct nbpf_pkt_info_tuple_t {
    pub eth_type: u_int16_t,
    pub ip_version: u_int8_t,
    pub l3_proto: u_int8_t,
    pub ip_tos: u_int8_t,
    pub ip_src: nbpf_ip_addr,
    pub ip_dst: nbpf_ip_addr,
    pub l4_src_port: u_int16_t,
    pub l4_dst_port: u_int16_t,
}

#[repr(C, packed)]
pub struct nbpf_pkt_info_t {
    pub device_id: u_int16_t,
    pub interface_id: u_int16_t,
    pub dmac: [u_int8_t; 6],
    pub smac: [u_int8_t; 6],
    pub vlan_id: u_int16_t,
    pub vlan_id_qinq: u_int16_t,
    pub master_l7_proto: u_int16_t,
    pub l7_proto: u_int16_t,
    pub tuple: nbpf_pkt_info_tuple_t,
    pub tunneled_tuple: nbpf_pkt_info_tuple_t,
}

#[repr(C, packed)]
pub struct nbpf_rule_core_fields_byte_match_t {
    pub protocol: u_int16_t,
    pub offset: u_int16_t,
    pub mask: u_int8_t,
    pub relop: u_int8_t,
    pub value: u_int8_t,
    pub next: *mut nbpf_rule_core_fields_byte_match_t,
}

#[repr(C, packed)]
pub struct nbpf_rule_core_fields_t {
    pub not_rule: u_int8_t,
    pub smac: [u_int8_t; 6],
    pub dmac: [u_int8_t; 6],
    pub proto: u_int8_t, // tcp, udp, sctp
    pub ip_version: u_int8_t,
    pub gtp: u_int8_t,
    pub vlan: u_int8_t,
    pub mpls: u_int8_t,
    pub vlan_id: u_int16_t,
    pub l7_proto: u_int16_t,
    pub mpls_label: u_int16_t,
    pub shost: nbpf_ip_addr,
    pub dhost: nbpf_ip_addr,
    pub shost_mask: nbpf_ip_addr,
    pub dhost_mask: nbpf_ip_addr,
    pub sport_low: u_int16_t,
    pub sport_high: u_int16_t,
    pub dport_low: u_int16_t,
    pub dport_high: u_int16_t,
    pub byte_match: *mut nbpf_rule_core_fields_byte_match_t,
}

#[repr(C, packed)]
pub struct nbpf_rule_list_item_t {
    pub fields: nbpf_rule_core_fields_t,
    pub bidirectional: c_int,
    pub next: *mut nbpf_rule_list_item_t,
}

#[repr(C, packed)]
pub struct nbpf_rule_block_list_item_t {
    pub rule_list_head: *mut nbpf_rule_list_item_t,
    pub next: *mut nbpf_rule_block_list_item_t,
}

extern "C" {
    pub fn nbpf_parse(
        bpf_filter: *const c_char,
        l7proto_by_name_callback: l7protocol_by_name_func,
    ) -> *mut nbpf_tree_t;
    pub fn nbpf_match(tree: *const nbpf_tree_t, h: *const nbpf_pkt_info_t) -> c_int;
    pub fn nbpf_free(t: *mut nbpf_tree_t);

    pub fn nbpf_generate_rules(tree: *const nbpf_tree_t) -> *mut nbpf_rule_list_item_t;
    pub fn nbpf_rule_list_free(list: *mut nbpf_rule_list_item_t);

    pub fn nbpf_generate_optimized_rules(
        tree: *const nbpf_tree_t,
    ) -> *mut nbpf_rule_block_list_item_t;
    pub fn nbpf_rule_block_list_free(blocks: *mut nbpf_rule_block_list_item_t);
}

#[cfg(test)]
mod test {
    use libc::c_char;
    use std::ffi::CString;

    use crate::{nbpf_ip_addr, nbpf_tree_t};

    use super::{nbpf_free, nbpf_match, nbpf_parse, nbpf_pkt_info_t, nbpf_pkt_info_tuple_t};

    #[test]
    pub fn test_nbpf_base() {
        unsafe {
            let filter = "not host 192.168.0.1";
            let filter = CString::new(filter).unwrap();
            let tree = nbpf_parse(filter.as_ptr() as *const c_char, None);
            let l4_src_port: u16 = 34;
            let l4_dst_port: u16 = 345;

            let pkt_info = nbpf_pkt_info_t {
                device_id: 0,
                interface_id: 0,
                dmac: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
                smac: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
                vlan_id: 0,
                vlan_id_qinq: 0,
                master_l7_proto: 0,
                l7_proto: 7,
                tuple: nbpf_pkt_info_tuple_t {
                    eth_type: 0x0800,
                    ip_version: 4,
                    l3_proto: 17,
                    ip_tos: 0,
                    ip_src: nbpf_ip_addr { v4: 0x0100000A },
                    ip_dst: nbpf_ip_addr { v4: 0x0100A8C1 },
                    l4_src_port: l4_src_port.to_be(),
                    l4_dst_port: l4_dst_port.to_be(),
                },
                tunneled_tuple: nbpf_pkt_info_tuple_t {
                    eth_type: 0,
                    ip_version: 0,
                    l3_proto: 0,
                    ip_tos: 0,
                    ip_src: nbpf_ip_addr { v4: 0 },
                    ip_dst: nbpf_ip_addr { v4: 0 },
                    l4_src_port: 0,
                    l4_dst_port: 0,
                },
            };

            let matched = nbpf_match(
                tree as *const nbpf_tree_t,
                &pkt_info as *const nbpf_pkt_info_t,
            );

            assert_eq!(matched, 1);
            nbpf_free(tree);
        }
    }

    #[test]
    pub fn test_nbpf_complex() {
        unsafe {
            let filter = "(host 192.168.0.1 and port 3000) or (src host 10.0.0.1 and proto 17)";
            let filter = CString::new(filter).unwrap();
            let tree = nbpf_parse(filter.as_ptr() as *const c_char, None);
            let l4_src_port: u16 = 3000;
            let l4_dst_port: u16 = 345;

            let pkt_info = nbpf_pkt_info_t {
                device_id: 0,
                interface_id: 0,
                dmac: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
                smac: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
                vlan_id: 0,
                vlan_id_qinq: 0,
                master_l7_proto: 0,
                l7_proto: 7,
                tuple: nbpf_pkt_info_tuple_t {
                    eth_type: 0x0800,
                    ip_version: 4,
                    l3_proto: 17,
                    ip_tos: 0,
                    ip_src: nbpf_ip_addr { v4: 0x0100000A },
                    ip_dst: nbpf_ip_addr { v4: 0x0100A8C0 },
                    l4_src_port: l4_src_port.to_be(),
                    l4_dst_port: l4_dst_port.to_be(),
                },
                tunneled_tuple: nbpf_pkt_info_tuple_t {
                    eth_type: 0,
                    ip_version: 0,
                    l3_proto: 0,
                    ip_tos: 0,
                    ip_src: nbpf_ip_addr { v4: 0 },
                    ip_dst: nbpf_ip_addr { v4: 0 },
                    l4_src_port: 0,
                    l4_dst_port: 0,
                },
            };

            let matched = nbpf_match(
                tree as *const nbpf_tree_t,
                &pkt_info as *const nbpf_pkt_info_t,
            );

            assert_eq!(matched, 1);
            nbpf_free(tree);
        }
    }
}