2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009 - 2012 Daniel Borkmann.
4 * Copyright 2009, 2010 Emmanuel Roullit.
5 * Copyright 1990-1996 The Regents of the University of
6 * California. All rights reserved. (3-clause BSD license)
7 * Subject to the GPL, version 2.
12 #include <arpa/inet.h>
13 #include <sys/types.h>
22 #define EXTRACT_SHORT(packet) \
23 ((unsigned short) ntohs(*(unsigned short *) packet))
24 #define EXTRACT_LONG(packet) \
25 (ntohl(*(unsigned long *) packet))
28 # define BPF_MEMWORDS 16
31 #define BPF_LD_B (BPF_LD | BPF_B)
32 #define BPF_LD_H (BPF_LD | BPF_H)
33 #define BPF_LD_W (BPF_LD | BPF_W)
34 #define BPF_LDX_B (BPF_LDX | BPF_B)
35 #define BPF_LDX_W (BPF_LDX | BPF_W)
36 #define BPF_JMP_JA (BPF_JMP | BPF_JA)
37 #define BPF_JMP_JEQ (BPF_JMP | BPF_JEQ)
38 #define BPF_JMP_JGT (BPF_JMP | BPF_JGT)
39 #define BPF_JMP_JGE (BPF_JMP | BPF_JGE)
40 #define BPF_JMP_JSET (BPF_JMP | BPF_JSET)
41 #define BPF_ALU_ADD (BPF_ALU | BPF_ADD)
42 #define BPF_ALU_SUB (BPF_ALU | BPF_SUB)
43 #define BPF_ALU_MUL (BPF_ALU | BPF_MUL)
44 #define BPF_ALU_DIV (BPF_ALU | BPF_DIV)
45 #define BPF_ALU_MOD (BPF_ALU | BPF_MOD)
46 #define BPF_ALU_NEG (BPF_ALU | BPF_NEG)
47 #define BPF_ALU_AND (BPF_ALU | BPF_AND)
48 #define BPF_ALU_OR (BPF_ALU | BPF_OR)
49 #define BPF_ALU_XOR (BPF_ALU | BPF_XOR)
50 #define BPF_ALU_LSH (BPF_ALU | BPF_LSH)
51 #define BPF_ALU_RSH (BPF_ALU | BPF_RSH)
52 #define BPF_MISC_TAX (BPF_MISC | BPF_TAX)
53 #define BPF_MISC_TXA (BPF_MISC | BPF_TXA)
55 static const char *op_table
[] = {
64 [BPF_JMP_JEQ
] = "jeq",
65 [BPF_JMP_JGT
] = "jgt",
66 [BPF_JMP_JGE
] = "jge",
67 [BPF_JMP_JSET
] = "jset",
68 [BPF_ALU_ADD
] = "add",
69 [BPF_ALU_SUB
] = "sub",
70 [BPF_ALU_MUL
] = "mul",
71 [BPF_ALU_DIV
] = "div",
72 [BPF_ALU_MOD
] = "mod",
73 [BPF_ALU_NEG
] = "neg",
74 [BPF_ALU_AND
] = "and",
76 [BPF_ALU_XOR
] = "xor",
77 [BPF_ALU_LSH
] = "lsh",
78 [BPF_ALU_RSH
] = "rsh",
80 [BPF_MISC_TAX
] = "tax",
81 [BPF_MISC_TXA
] = "txa",
84 void bpf_dump_op_table(void)
87 for (i
= 0; i
< array_size(op_table
); ++i
) {
89 printf("%s\n", op_table
[i
]);
93 static const char *bpf_dump_linux_k(uint32_t k
)
98 case SKF_AD_OFF
+ SKF_AD_PROTOCOL
:
100 case SKF_AD_OFF
+ SKF_AD_PKTTYPE
:
102 case SKF_AD_OFF
+ SKF_AD_IFINDEX
:
104 case SKF_AD_OFF
+ SKF_AD_NLATTR
:
106 case SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
:
108 case SKF_AD_OFF
+ SKF_AD_MARK
:
110 case SKF_AD_OFF
+ SKF_AD_QUEUE
:
112 case SKF_AD_OFF
+ SKF_AD_HATYPE
:
114 case SKF_AD_OFF
+ SKF_AD_RXHASH
:
116 case SKF_AD_OFF
+ SKF_AD_CPU
:
118 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG
:
120 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
:
122 case SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
:
127 static char *__bpf_dump(const struct sock_filter bpf
, int n
)
130 const char *fmt
, *op
;
131 static char image
[256];
141 case BPF_RET
| BPF_K
:
142 op
= op_table
[BPF_RET
];
145 case BPF_RET
| BPF_A
:
146 op
= op_table
[BPF_RET
];
149 case BPF_RET
| BPF_X
:
150 op
= op_table
[BPF_RET
];
153 case BPF_LD_W
| BPF_ABS
:
154 op
= op_table
[BPF_LD_W
];
155 fmt
= bpf_dump_linux_k(bpf
.k
);
157 case BPF_LD_H
| BPF_ABS
:
158 op
= op_table
[BPF_LD_H
];
159 fmt
= bpf_dump_linux_k(bpf
.k
);
161 case BPF_LD_B
| BPF_ABS
:
162 op
= op_table
[BPF_LD_B
];
163 fmt
= bpf_dump_linux_k(bpf
.k
);
165 case BPF_LD_W
| BPF_LEN
:
166 op
= op_table
[BPF_LD_W
];
169 case BPF_LD_W
| BPF_IND
:
170 op
= op_table
[BPF_LD_W
];
173 case BPF_LD_H
| BPF_IND
:
174 op
= op_table
[BPF_LD_H
];
177 case BPF_LD_B
| BPF_IND
:
178 op
= op_table
[BPF_LD_B
];
181 case BPF_LD
| BPF_IMM
:
182 op
= op_table
[BPF_LD_W
];
185 case BPF_LDX
| BPF_IMM
:
186 op
= op_table
[BPF_LDX
];
189 case BPF_LDX_B
| BPF_MSH
:
190 op
= op_table
[BPF_LDX_B
];
191 fmt
= "4*([%d]&0xf)";
193 case BPF_LD
| BPF_MEM
:
194 op
= op_table
[BPF_LD_W
];
197 case BPF_LDX
| BPF_MEM
:
198 op
= op_table
[BPF_LDX
];
202 op
= op_table
[BPF_ST
];
206 op
= op_table
[BPF_STX
];
210 op
= op_table
[BPF_JMP_JA
];
214 case BPF_JMP_JGT
| BPF_K
:
215 op
= op_table
[BPF_JMP_JGT
];
218 case BPF_JMP_JGE
| BPF_K
:
219 op
= op_table
[BPF_JMP_JGE
];
222 case BPF_JMP_JEQ
| BPF_K
:
223 op
= op_table
[BPF_JMP_JEQ
];
226 case BPF_JMP_JSET
| BPF_K
:
227 op
= op_table
[BPF_JMP_JSET
];
230 case BPF_JMP_JGT
| BPF_X
:
231 op
= op_table
[BPF_JMP_JGT
];
234 case BPF_JMP_JGE
| BPF_X
:
235 op
= op_table
[BPF_JMP_JGE
];
238 case BPF_JMP_JEQ
| BPF_X
:
239 op
= op_table
[BPF_JMP_JEQ
];
242 case BPF_JMP_JSET
| BPF_X
:
243 op
= op_table
[BPF_JMP_JSET
];
246 case BPF_ALU_ADD
| BPF_X
:
247 op
= op_table
[BPF_ALU_ADD
];
250 case BPF_ALU_SUB
| BPF_X
:
251 op
= op_table
[BPF_ALU_SUB
];
254 case BPF_ALU_MUL
| BPF_X
:
255 op
= op_table
[BPF_ALU_MUL
];
258 case BPF_ALU_DIV
| BPF_X
:
259 op
= op_table
[BPF_ALU_DIV
];
262 case BPF_ALU_MOD
| BPF_X
:
263 op
= op_table
[BPF_ALU_MOD
];
266 case BPF_ALU_AND
| BPF_X
:
267 op
= op_table
[BPF_ALU_AND
];
270 case BPF_ALU_OR
| BPF_X
:
271 op
= op_table
[BPF_ALU_OR
];
274 case BPF_ALU_XOR
| BPF_X
:
275 op
= op_table
[BPF_ALU_XOR
];
278 case BPF_ALU_LSH
| BPF_X
:
279 op
= op_table
[BPF_ALU_LSH
];
282 case BPF_ALU_RSH
| BPF_X
:
283 op
= op_table
[BPF_ALU_RSH
];
286 case BPF_ALU_ADD
| BPF_K
:
287 op
= op_table
[BPF_ALU_ADD
];
290 case BPF_ALU_SUB
| BPF_K
:
291 op
= op_table
[BPF_ALU_SUB
];
294 case BPF_ALU_MUL
| BPF_K
:
295 op
= op_table
[BPF_ALU_MUL
];
298 case BPF_ALU_DIV
| BPF_K
:
299 op
= op_table
[BPF_ALU_DIV
];
302 case BPF_ALU_MOD
| BPF_K
:
303 op
= op_table
[BPF_ALU_MOD
];
306 case BPF_ALU_AND
| BPF_K
:
307 op
= op_table
[BPF_ALU_AND
];
310 case BPF_ALU_OR
| BPF_K
:
311 op
= op_table
[BPF_ALU_OR
];
314 case BPF_ALU_XOR
| BPF_K
:
315 op
= op_table
[BPF_ALU_XOR
];
318 case BPF_ALU_LSH
| BPF_K
:
319 op
= op_table
[BPF_ALU_LSH
];
322 case BPF_ALU_RSH
| BPF_K
:
323 op
= op_table
[BPF_ALU_RSH
];
327 op
= op_table
[BPF_ALU_NEG
];
331 op
= op_table
[BPF_MISC_TAX
];
335 op
= op_table
[BPF_MISC_TXA
];
340 slprintf_nocheck(operand
, sizeof(operand
), fmt
, v
);
341 slprintf_nocheck(image
, sizeof(image
),
342 (BPF_CLASS(bpf
.code
) == BPF_JMP
&&
343 BPF_OP(bpf
.code
) != BPF_JA
) ?
344 " L%d: %s %s, L%d, L%d" : " L%d: %s %s",
345 n
, op
, operand
, n
+ 1 + bpf
.jt
, n
+ 1 + bpf
.jf
);
349 void bpf_dump_all(struct sock_fprog
*bpf
)
352 for (i
= 0; i
< bpf
->len
; ++i
)
353 printf("%s\n", __bpf_dump(bpf
->filter
[i
], i
));
356 void bpf_attach_to_sock(int sock
, struct sock_fprog
*bpf
)
360 if (bpf
->filter
[0].code
== BPF_RET
&&
361 bpf
->filter
[0].k
== 0xFFFFFFFF)
364 ret
= setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
,
367 panic("Cannot attach filter to socket!\n");
370 void bpf_detach_from_sock(int sock
)
374 ret
= setsockopt(sock
, SOL_SOCKET
, SO_DETACH_FILTER
,
375 &empty
, sizeof(empty
));
377 panic("Cannot detach filter from socket!\n");
380 int enable_kernel_bpf_jit_compiler(void)
384 char *file
= "/proc/sys/net/core/bpf_jit_enable";
386 fd
= open(file
, O_WRONLY
);
390 ret
= write(fd
, "1", strlen("1"));
396 int __bpf_validate(const struct sock_fprog
*bpf
)
399 const struct sock_filter
*p
;
406 for (i
= 0; i
< bpf
->len
; ++i
) {
408 switch (BPF_CLASS(p
->code
)) {
409 /* Check that memory operations use valid addresses. */
412 switch (BPF_MODE(p
->code
)) {
418 /* There's no maximum packet data size
419 * in userland. The runtime packet length
424 if (p
->k
>= BPF_MEMWORDS
)
435 if (p
->k
>= BPF_MEMWORDS
)
439 switch (BPF_OP(p
->code
)) {
452 /* Check for constant division by 0 (undefined
455 if (BPF_RVAL(p
->code
) == BPF_K
&& p
->k
== 0)
463 /* Check that jumps are within the code block,
464 * and that unconditional branches don't go
465 * backwards as a result of an overflow.
466 * Unconditional branches have a 32-bit offset,
467 * so they could overflow; we check to make
468 * sure they don't. Conditional branches have
469 * an 8-bit offset, and the from address is <=
470 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
471 * is sufficiently small that adding 255 to it
474 * We know that len is <= BPF_MAXINSNS, and we
475 * assume that BPF_MAXINSNS is < the maximum size
476 * of a u_int, so that i + 1 doesn't overflow.
478 * For userland, we don't know that the from
479 * or len are <= BPF_MAXINSNS, but we know that
480 * from <= len, and, except on a 64-bit system,
481 * it's unlikely that len, if it truly reflects
482 * the size of the program we've been handed,
483 * will be anywhere near the maximum size of
484 * a u_int. We also don't check for backward
485 * branches, as we currently support them in
486 * userland for the protochain operation.
489 switch (BPF_OP(p
->code
)) {
491 if (from
+ p
->k
>= bpf
->len
)
498 if (from
+ p
->jt
>= bpf
->len
||
499 from
+ p
->jf
>= bpf
->len
)
513 return BPF_CLASS(bpf
->filter
[bpf
->len
- 1].code
) == BPF_RET
;
516 uint32_t bpf_run_filter(const struct sock_fprog
* fcode
, uint8_t * packet
,
519 /* XXX: caplen == len */
522 struct sock_filter
*bpf
;
523 int32_t mem
[BPF_MEMWORDS
] = { 0, };
525 if (fcode
== NULL
|| fcode
->filter
== NULL
|| fcode
->len
== 0)
538 case BPF_RET
| BPF_K
:
539 return (uint32_t) bpf
->k
;
540 case BPF_RET
| BPF_A
:
542 case BPF_LD_W
| BPF_ABS
:
543 /* No Linux extensions supported here! */
545 if (k
+ sizeof(int32_t) > plen
)
547 A
= EXTRACT_LONG(&packet
[k
]);
549 case BPF_LD_H
| BPF_ABS
:
550 /* No Linux extensions supported here! */
552 if (k
+ sizeof(short) > plen
)
554 A
= EXTRACT_SHORT(&packet
[k
]);
556 case BPF_LD_B
| BPF_ABS
:
557 /* No Linux extensions supported here! */
563 case BPF_LD_W
| BPF_LEN
:
566 case BPF_LDX_W
| BPF_LEN
:
569 case BPF_LD_W
| BPF_IND
:
571 if (k
+ sizeof(int32_t) > plen
)
573 A
= EXTRACT_LONG(&packet
[k
]);
575 case BPF_LD_H
| BPF_IND
:
577 if (k
+ sizeof(short) > plen
)
579 A
= EXTRACT_SHORT(&packet
[k
]);
581 case BPF_LD_B
| BPF_IND
:
587 case BPF_LDX_B
| BPF_MSH
:
591 X
= (packet
[bpf
->k
] & 0xf) << 2;
593 case BPF_LD
| BPF_IMM
:
596 case BPF_LDX
| BPF_IMM
:
599 case BPF_LD
| BPF_MEM
:
602 case BPF_LDX
| BPF_MEM
:
614 case BPF_JMP_JGT
| BPF_K
:
615 bpf
+= (A
> bpf
->k
) ? bpf
->jt
: bpf
->jf
;
617 case BPF_JMP_JGE
| BPF_K
:
618 bpf
+= (A
>= bpf
->k
) ? bpf
->jt
: bpf
->jf
;
620 case BPF_JMP_JEQ
| BPF_K
:
621 bpf
+= (A
== bpf
->k
) ? bpf
->jt
: bpf
->jf
;
623 case BPF_JMP_JSET
| BPF_K
:
624 bpf
+= (A
& bpf
->k
) ? bpf
->jt
: bpf
->jf
;
626 case BPF_JMP_JGT
| BPF_X
:
627 bpf
+= (A
> X
) ? bpf
->jt
: bpf
->jf
;
629 case BPF_JMP_JGE
| BPF_X
:
630 bpf
+= (A
>= X
) ? bpf
->jt
: bpf
->jf
;
632 case BPF_JMP_JEQ
| BPF_X
:
633 bpf
+= (A
== X
) ? bpf
->jt
: bpf
->jf
;
635 case BPF_JMP_JSET
| BPF_X
:
636 bpf
+= (A
& X
) ? bpf
->jt
: bpf
->jf
;
638 case BPF_ALU_ADD
| BPF_X
:
641 case BPF_ALU_SUB
| BPF_X
:
644 case BPF_ALU_MUL
| BPF_X
:
647 case BPF_ALU_DIV
| BPF_X
:
652 case BPF_ALU_MOD
| BPF_X
:
657 case BPF_ALU_AND
| BPF_X
:
660 case BPF_ALU_OR
| BPF_X
:
663 case BPF_ALU_XOR
| BPF_X
:
666 case BPF_ALU_LSH
| BPF_X
:
669 case BPF_ALU_RSH
| BPF_X
:
672 case BPF_ALU_ADD
| BPF_K
:
675 case BPF_ALU_SUB
| BPF_K
:
678 case BPF_ALU_MUL
| BPF_K
:
681 case BPF_ALU_DIV
| BPF_K
:
684 case BPF_ALU_MOD
| BPF_K
:
687 case BPF_ALU_AND
| BPF_K
:
690 case BPF_ALU_OR
| BPF_K
:
693 case BPF_ALU_XOR
| BPF_K
:
696 case BPF_ALU_LSH
| BPF_K
:
699 case BPF_ALU_RSH
| BPF_K
:
715 void bpf_parse_rules(char *rulefile
, struct sock_fprog
*bpf
, uint32_t link_type
)
719 struct sock_filter sf_single
= { 0x06, 0, 0, 0xFFFFFFFF };
722 if (rulefile
== NULL
) {
724 bpf
->filter
= xmalloc(sizeof(sf_single
));
725 fmemcpy(&bpf
->filter
[0], &sf_single
, sizeof(sf_single
));
729 fp
= fopen(rulefile
, "r");
731 bpf_try_compile(rulefile
, bpf
, link_type
);
735 fmemset(buff
, 0, sizeof(buff
));
736 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
737 buff
[sizeof(buff
) - 1] = 0;
738 if (buff
[0] != '{') {
739 fmemset(buff
, 0, sizeof(buff
));
743 fmemset(&sf_single
, 0, sizeof(sf_single
));
744 ret
= sscanf(buff
, "{ 0x%x, %u, %u, 0x%08x },",
745 (unsigned int *) &sf_single
.code
,
746 (unsigned int *) &sf_single
.jt
,
747 (unsigned int *) &sf_single
.jf
,
748 (unsigned int *) &sf_single
.k
);
750 panic("BPF syntax error!\n");
753 bpf
->filter
= xrealloc(bpf
->filter
, 1,
754 bpf
->len
* sizeof(sf_single
));
756 fmemcpy(&bpf
->filter
[bpf
->len
- 1], &sf_single
,
758 fmemset(buff
, 0, sizeof(buff
));
763 if (__bpf_validate(bpf
) == 0)
764 panic("This is not a valid BPF program!\n");