xref: /dflybsd-src/contrib/libpcap/bpf_image.c (revision e75ef36f1332e115895388cede9dfd24ca1a806c)
11077d0bdSPeter Avalos /*
21077d0bdSPeter Avalos  * Copyright (c) 1990, 1991, 1992, 1994, 1995, 1996
31077d0bdSPeter Avalos  *	The Regents of the University of California.  All rights reserved.
41077d0bdSPeter Avalos  *
51077d0bdSPeter Avalos  * Redistribution and use in source and binary forms, with or without
61077d0bdSPeter Avalos  * modification, are permitted provided that: (1) source code distributions
71077d0bdSPeter Avalos  * retain the above copyright notice and this paragraph in its entirety, (2)
81077d0bdSPeter Avalos  * distributions including binary code include the above copyright notice and
91077d0bdSPeter Avalos  * this paragraph in its entirety in the documentation or other materials
101077d0bdSPeter Avalos  * provided with the distribution, and (3) all advertising materials mentioning
111077d0bdSPeter Avalos  * features or use of this software display the following acknowledgement:
121077d0bdSPeter Avalos  * ``This product includes software developed by the University of California,
131077d0bdSPeter Avalos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
141077d0bdSPeter Avalos  * the University nor the names of its contributors may be used to endorse
151077d0bdSPeter Avalos  * or promote products derived from this software without specific prior
161077d0bdSPeter Avalos  * written permission.
171077d0bdSPeter Avalos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
181077d0bdSPeter Avalos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
191077d0bdSPeter Avalos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
201077d0bdSPeter Avalos  */
211077d0bdSPeter Avalos 
221077d0bdSPeter Avalos #ifdef HAVE_CONFIG_H
233a289941SAaron LI #include <config.h>
241077d0bdSPeter Avalos #endif
251077d0bdSPeter Avalos 
263a289941SAaron LI #include <pcap-types.h>
27a85e14b0SPeter Avalos 
281077d0bdSPeter Avalos #include <stdio.h>
291077d0bdSPeter Avalos #include <string.h>
301077d0bdSPeter Avalos 
31*ea16f64eSAntonio Huete Jimenez #ifdef __linux__
32*ea16f64eSAntonio Huete Jimenez #include <linux/types.h>
33*ea16f64eSAntonio Huete Jimenez #include <linux/if_packet.h>
34*ea16f64eSAntonio Huete Jimenez #include <linux/filter.h>
35*ea16f64eSAntonio Huete Jimenez 
36*ea16f64eSAntonio Huete Jimenez /*
37*ea16f64eSAntonio Huete Jimenez  * We want our versions of these #defines, not Linux's version.
38*ea16f64eSAntonio Huete Jimenez  * (The two should be the same; if not, we have a problem; all BPF
39*ea16f64eSAntonio Huete Jimenez  * implementations *should* be source-compatible supersets of ours.)
40*ea16f64eSAntonio Huete Jimenez  */
41*ea16f64eSAntonio Huete Jimenez #undef BPF_STMT
42*ea16f64eSAntonio Huete Jimenez #undef BPF_JUMP
43*ea16f64eSAntonio Huete Jimenez #endif
44*ea16f64eSAntonio Huete Jimenez 
451077d0bdSPeter Avalos #include "pcap-int.h"
461077d0bdSPeter Avalos 
471077d0bdSPeter Avalos #ifdef HAVE_OS_PROTO_H
481077d0bdSPeter Avalos #include "os-proto.h"
491077d0bdSPeter Avalos #endif
501077d0bdSPeter Avalos 
51*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_OFF
52*ea16f64eSAntonio Huete Jimenez /*
53*ea16f64eSAntonio Huete Jimenez  * Symbolic names for offsets that refer to the special Linux BPF locations.
54*ea16f64eSAntonio Huete Jimenez  */
55*ea16f64eSAntonio Huete Jimenez static const char *offsets[SKF_AD_MAX] = {
56*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_PROTOCOL
57*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_PROTOCOL] = "proto",
58*ea16f64eSAntonio Huete Jimenez #endif
59*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_PKTTYPE
60*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_PKTTYPE] = "type",
61*ea16f64eSAntonio Huete Jimenez #endif
62*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_IFINDEX
63*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_IFINDEX] = "ifidx",
64*ea16f64eSAntonio Huete Jimenez #endif
65*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_NLATTR
66*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_NLATTR] = "nla",
67*ea16f64eSAntonio Huete Jimenez #endif
68*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_NLATTR_NEST
69*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_NLATTR_NEST] = "nlan",
70*ea16f64eSAntonio Huete Jimenez #endif
71*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_MARK
72*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_MARK] = "mark",
73*ea16f64eSAntonio Huete Jimenez #endif
74*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_QUEUE
75*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_QUEUE] = "queue",
76*ea16f64eSAntonio Huete Jimenez #endif
77*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_HATYPE
78*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_HATYPE] = "hatype",
79*ea16f64eSAntonio Huete Jimenez #endif
80*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_RXHASH
81*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_RXHASH] = "rxhash",
82*ea16f64eSAntonio Huete Jimenez #endif
83*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_CPU
84*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_CPU] = "cpu",
85*ea16f64eSAntonio Huete Jimenez #endif
86*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_ALU_XOR_X
87*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_ALU_XOR_X] = "xor_x",
88*ea16f64eSAntonio Huete Jimenez #endif
89*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_VLAN_TAG
90*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_VLAN_TAG] = "vlan_tci",
91*ea16f64eSAntonio Huete Jimenez #endif
92*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_VLAN_TAG_PRESENT
93*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_VLAN_TAG_PRESENT] = "vlanp",
94*ea16f64eSAntonio Huete Jimenez #endif
95*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_PAY_OFFSET
96*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_PAY_OFFSET] = "poff",
97*ea16f64eSAntonio Huete Jimenez #endif
98*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_RANDOM
99*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_RANDOM] = "random",
100*ea16f64eSAntonio Huete Jimenez #endif
101*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_VLAN_TPID
102*ea16f64eSAntonio Huete Jimenez 	[SKF_AD_VLAN_TPID] = "vlan_tpid"
103*ea16f64eSAntonio Huete Jimenez #endif
104*ea16f64eSAntonio Huete Jimenez };
105*ea16f64eSAntonio Huete Jimenez #endif
106*ea16f64eSAntonio Huete Jimenez 
107*ea16f64eSAntonio Huete Jimenez static void
bpf_print_abs_load_operand(char * buf,size_t bufsize,const struct bpf_insn * p)108*ea16f64eSAntonio Huete Jimenez bpf_print_abs_load_operand(char *buf, size_t bufsize, const struct bpf_insn *p)
109*ea16f64eSAntonio Huete Jimenez {
110*ea16f64eSAntonio Huete Jimenez #ifdef SKF_AD_OFF
111*ea16f64eSAntonio Huete Jimenez 	const char *sym;
112*ea16f64eSAntonio Huete Jimenez 
113*ea16f64eSAntonio Huete Jimenez 	/*
114*ea16f64eSAntonio Huete Jimenez 	 * It's an absolute load.
115*ea16f64eSAntonio Huete Jimenez 	 * Is the offset a special Linux offset that we know about?
116*ea16f64eSAntonio Huete Jimenez 	 */
117*ea16f64eSAntonio Huete Jimenez 	if (p->k >= (bpf_u_int32)SKF_AD_OFF &&
118*ea16f64eSAntonio Huete Jimenez 	    p->k < (bpf_u_int32)(SKF_AD_OFF + SKF_AD_MAX) &&
119*ea16f64eSAntonio Huete Jimenez 	    (sym = offsets[p->k - (bpf_u_int32)SKF_AD_OFF]) != NULL) {
120*ea16f64eSAntonio Huete Jimenez 		/*
121*ea16f64eSAntonio Huete Jimenez 		 * Yes.  Print the offset symbolically.
122*ea16f64eSAntonio Huete Jimenez 		 */
123*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(buf, bufsize, "[%s]", sym);
124*ea16f64eSAntonio Huete Jimenez 	} else
125*ea16f64eSAntonio Huete Jimenez #endif
126*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(buf, bufsize, "[%d]", p->k);
127*ea16f64eSAntonio Huete Jimenez }
128*ea16f64eSAntonio Huete Jimenez 
1291077d0bdSPeter Avalos char *
bpf_image(const struct bpf_insn * p,int n)1303a289941SAaron LI bpf_image(const struct bpf_insn *p, int n)
1311077d0bdSPeter Avalos {
1323a289941SAaron LI 	const char *op;
1331077d0bdSPeter Avalos 	static char image[256];
1343a289941SAaron LI 	char operand_buf[64];
1353a289941SAaron LI 	const char *operand;
1361077d0bdSPeter Avalos 
1371077d0bdSPeter Avalos 	switch (p->code) {
1381077d0bdSPeter Avalos 
1391077d0bdSPeter Avalos 	default:
1401077d0bdSPeter Avalos 		op = "unimp";
141*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "0x%x", p->code);
1423a289941SAaron LI 		operand = operand_buf;
1431077d0bdSPeter Avalos 		break;
1441077d0bdSPeter Avalos 
1451077d0bdSPeter Avalos 	case BPF_RET|BPF_K:
1461077d0bdSPeter Avalos 		op = "ret";
147*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
1483a289941SAaron LI 		operand = operand_buf;
1491077d0bdSPeter Avalos 		break;
1501077d0bdSPeter Avalos 
1511077d0bdSPeter Avalos 	case BPF_RET|BPF_A:
1521077d0bdSPeter Avalos 		op = "ret";
1533a289941SAaron LI 		operand = "";
1541077d0bdSPeter Avalos 		break;
1551077d0bdSPeter Avalos 
1561077d0bdSPeter Avalos 	case BPF_LD|BPF_W|BPF_ABS:
1571077d0bdSPeter Avalos 		op = "ld";
158*ea16f64eSAntonio Huete Jimenez 		bpf_print_abs_load_operand(operand_buf, sizeof operand_buf, p);
1593a289941SAaron LI 		operand = operand_buf;
1601077d0bdSPeter Avalos 		break;
1611077d0bdSPeter Avalos 
1621077d0bdSPeter Avalos 	case BPF_LD|BPF_H|BPF_ABS:
1631077d0bdSPeter Avalos 		op = "ldh";
164*ea16f64eSAntonio Huete Jimenez 		bpf_print_abs_load_operand(operand_buf, sizeof operand_buf, p);
1653a289941SAaron LI 		operand = operand_buf;
1661077d0bdSPeter Avalos 		break;
1671077d0bdSPeter Avalos 
1681077d0bdSPeter Avalos 	case BPF_LD|BPF_B|BPF_ABS:
1691077d0bdSPeter Avalos 		op = "ldb";
170*ea16f64eSAntonio Huete Jimenez 		bpf_print_abs_load_operand(operand_buf, sizeof operand_buf, p);
1713a289941SAaron LI 		operand = operand_buf;
1721077d0bdSPeter Avalos 		break;
1731077d0bdSPeter Avalos 
1741077d0bdSPeter Avalos 	case BPF_LD|BPF_W|BPF_LEN:
1751077d0bdSPeter Avalos 		op = "ld";
1763a289941SAaron LI 		operand = "#pktlen";
1771077d0bdSPeter Avalos 		break;
1781077d0bdSPeter Avalos 
1791077d0bdSPeter Avalos 	case BPF_LD|BPF_W|BPF_IND:
1801077d0bdSPeter Avalos 		op = "ld";
181*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "[x + %d]", p->k);
1823a289941SAaron LI 		operand = operand_buf;
1831077d0bdSPeter Avalos 		break;
1841077d0bdSPeter Avalos 
1851077d0bdSPeter Avalos 	case BPF_LD|BPF_H|BPF_IND:
1861077d0bdSPeter Avalos 		op = "ldh";
187*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "[x + %d]", p->k);
1883a289941SAaron LI 		operand = operand_buf;
1891077d0bdSPeter Avalos 		break;
1901077d0bdSPeter Avalos 
1911077d0bdSPeter Avalos 	case BPF_LD|BPF_B|BPF_IND:
1921077d0bdSPeter Avalos 		op = "ldb";
193*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "[x + %d]", p->k);
1943a289941SAaron LI 		operand = operand_buf;
1951077d0bdSPeter Avalos 		break;
1961077d0bdSPeter Avalos 
1971077d0bdSPeter Avalos 	case BPF_LD|BPF_IMM:
1981077d0bdSPeter Avalos 		op = "ld";
199*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
2003a289941SAaron LI 		operand = operand_buf;
2011077d0bdSPeter Avalos 		break;
2021077d0bdSPeter Avalos 
2031077d0bdSPeter Avalos 	case BPF_LDX|BPF_IMM:
2041077d0bdSPeter Avalos 		op = "ldx";
205*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
2063a289941SAaron LI 		operand = operand_buf;
2071077d0bdSPeter Avalos 		break;
2081077d0bdSPeter Avalos 
2091077d0bdSPeter Avalos 	case BPF_LDX|BPF_MSH|BPF_B:
2101077d0bdSPeter Avalos 		op = "ldxb";
211*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "4*([%d]&0xf)", p->k);
2123a289941SAaron LI 		operand = operand_buf;
2131077d0bdSPeter Avalos 		break;
2141077d0bdSPeter Avalos 
2151077d0bdSPeter Avalos 	case BPF_LD|BPF_MEM:
2161077d0bdSPeter Avalos 		op = "ld";
217*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "M[%d]", p->k);
2183a289941SAaron LI 		operand = operand_buf;
2191077d0bdSPeter Avalos 		break;
2201077d0bdSPeter Avalos 
2211077d0bdSPeter Avalos 	case BPF_LDX|BPF_MEM:
2221077d0bdSPeter Avalos 		op = "ldx";
223*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "M[%d]", p->k);
2243a289941SAaron LI 		operand = operand_buf;
2251077d0bdSPeter Avalos 		break;
2261077d0bdSPeter Avalos 
2271077d0bdSPeter Avalos 	case BPF_ST:
2281077d0bdSPeter Avalos 		op = "st";
229*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "M[%d]", p->k);
2303a289941SAaron LI 		operand = operand_buf;
2311077d0bdSPeter Avalos 		break;
2321077d0bdSPeter Avalos 
2331077d0bdSPeter Avalos 	case BPF_STX:
2341077d0bdSPeter Avalos 		op = "stx";
235*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "M[%d]", p->k);
2363a289941SAaron LI 		operand = operand_buf;
2371077d0bdSPeter Avalos 		break;
2381077d0bdSPeter Avalos 
2391077d0bdSPeter Avalos 	case BPF_JMP|BPF_JA:
2401077d0bdSPeter Avalos 		op = "ja";
241*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "%d", n + 1 + p->k);
2423a289941SAaron LI 		operand = operand_buf;
2431077d0bdSPeter Avalos 		break;
2441077d0bdSPeter Avalos 
2451077d0bdSPeter Avalos 	case BPF_JMP|BPF_JGT|BPF_K:
2461077d0bdSPeter Avalos 		op = "jgt";
247*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
2483a289941SAaron LI 		operand = operand_buf;
2491077d0bdSPeter Avalos 		break;
2501077d0bdSPeter Avalos 
2511077d0bdSPeter Avalos 	case BPF_JMP|BPF_JGE|BPF_K:
2521077d0bdSPeter Avalos 		op = "jge";
253*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
2543a289941SAaron LI 		operand = operand_buf;
2551077d0bdSPeter Avalos 		break;
2561077d0bdSPeter Avalos 
2571077d0bdSPeter Avalos 	case BPF_JMP|BPF_JEQ|BPF_K:
2581077d0bdSPeter Avalos 		op = "jeq";
259*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
2603a289941SAaron LI 		operand = operand_buf;
2611077d0bdSPeter Avalos 		break;
2621077d0bdSPeter Avalos 
2631077d0bdSPeter Avalos 	case BPF_JMP|BPF_JSET|BPF_K:
2641077d0bdSPeter Avalos 		op = "jset";
265*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
2663a289941SAaron LI 		operand = operand_buf;
2671077d0bdSPeter Avalos 		break;
2681077d0bdSPeter Avalos 
2691077d0bdSPeter Avalos 	case BPF_JMP|BPF_JGT|BPF_X:
2701077d0bdSPeter Avalos 		op = "jgt";
2713a289941SAaron LI 		operand = "x";
2721077d0bdSPeter Avalos 		break;
2731077d0bdSPeter Avalos 
2741077d0bdSPeter Avalos 	case BPF_JMP|BPF_JGE|BPF_X:
2751077d0bdSPeter Avalos 		op = "jge";
2763a289941SAaron LI 		operand = "x";
2771077d0bdSPeter Avalos 		break;
2781077d0bdSPeter Avalos 
2791077d0bdSPeter Avalos 	case BPF_JMP|BPF_JEQ|BPF_X:
2801077d0bdSPeter Avalos 		op = "jeq";
2813a289941SAaron LI 		operand = "x";
2821077d0bdSPeter Avalos 		break;
2831077d0bdSPeter Avalos 
2841077d0bdSPeter Avalos 	case BPF_JMP|BPF_JSET|BPF_X:
2851077d0bdSPeter Avalos 		op = "jset";
2863a289941SAaron LI 		operand = "x";
2871077d0bdSPeter Avalos 		break;
2881077d0bdSPeter Avalos 
2891077d0bdSPeter Avalos 	case BPF_ALU|BPF_ADD|BPF_X:
2901077d0bdSPeter Avalos 		op = "add";
2913a289941SAaron LI 		operand = "x";
2921077d0bdSPeter Avalos 		break;
2931077d0bdSPeter Avalos 
2941077d0bdSPeter Avalos 	case BPF_ALU|BPF_SUB|BPF_X:
2951077d0bdSPeter Avalos 		op = "sub";
2963a289941SAaron LI 		operand = "x";
2971077d0bdSPeter Avalos 		break;
2981077d0bdSPeter Avalos 
2991077d0bdSPeter Avalos 	case BPF_ALU|BPF_MUL|BPF_X:
3001077d0bdSPeter Avalos 		op = "mul";
3013a289941SAaron LI 		operand = "x";
3021077d0bdSPeter Avalos 		break;
3031077d0bdSPeter Avalos 
3041077d0bdSPeter Avalos 	case BPF_ALU|BPF_DIV|BPF_X:
3051077d0bdSPeter Avalos 		op = "div";
3063a289941SAaron LI 		operand = "x";
3071077d0bdSPeter Avalos 		break;
3081077d0bdSPeter Avalos 
30997a9217aSAntonio Huete Jimenez 	case BPF_ALU|BPF_MOD|BPF_X:
31097a9217aSAntonio Huete Jimenez 		op = "mod";
3113a289941SAaron LI 		operand = "x";
31297a9217aSAntonio Huete Jimenez 		break;
31397a9217aSAntonio Huete Jimenez 
3141077d0bdSPeter Avalos 	case BPF_ALU|BPF_AND|BPF_X:
3151077d0bdSPeter Avalos 		op = "and";
3163a289941SAaron LI 		operand = "x";
3171077d0bdSPeter Avalos 		break;
3181077d0bdSPeter Avalos 
3191077d0bdSPeter Avalos 	case BPF_ALU|BPF_OR|BPF_X:
3201077d0bdSPeter Avalos 		op = "or";
3213a289941SAaron LI 		operand = "x";
3221077d0bdSPeter Avalos 		break;
3231077d0bdSPeter Avalos 
32497a9217aSAntonio Huete Jimenez 	case BPF_ALU|BPF_XOR|BPF_X:
32597a9217aSAntonio Huete Jimenez 		op = "xor";
3263a289941SAaron LI 		operand = "x";
32797a9217aSAntonio Huete Jimenez 		break;
32897a9217aSAntonio Huete Jimenez 
3291077d0bdSPeter Avalos 	case BPF_ALU|BPF_LSH|BPF_X:
3301077d0bdSPeter Avalos 		op = "lsh";
3313a289941SAaron LI 		operand = "x";
3321077d0bdSPeter Avalos 		break;
3331077d0bdSPeter Avalos 
3341077d0bdSPeter Avalos 	case BPF_ALU|BPF_RSH|BPF_X:
3351077d0bdSPeter Avalos 		op = "rsh";
3363a289941SAaron LI 		operand = "x";
3371077d0bdSPeter Avalos 		break;
3381077d0bdSPeter Avalos 
3391077d0bdSPeter Avalos 	case BPF_ALU|BPF_ADD|BPF_K:
3401077d0bdSPeter Avalos 		op = "add";
341*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
3423a289941SAaron LI 		operand = operand_buf;
3431077d0bdSPeter Avalos 		break;
3441077d0bdSPeter Avalos 
3451077d0bdSPeter Avalos 	case BPF_ALU|BPF_SUB|BPF_K:
3461077d0bdSPeter Avalos 		op = "sub";
347*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
3483a289941SAaron LI 		operand = operand_buf;
3491077d0bdSPeter Avalos 		break;
3501077d0bdSPeter Avalos 
3511077d0bdSPeter Avalos 	case BPF_ALU|BPF_MUL|BPF_K:
3521077d0bdSPeter Avalos 		op = "mul";
353*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
3543a289941SAaron LI 		operand = operand_buf;
3551077d0bdSPeter Avalos 		break;
3561077d0bdSPeter Avalos 
3571077d0bdSPeter Avalos 	case BPF_ALU|BPF_DIV|BPF_K:
3581077d0bdSPeter Avalos 		op = "div";
359*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
3603a289941SAaron LI 		operand = operand_buf;
3611077d0bdSPeter Avalos 		break;
3621077d0bdSPeter Avalos 
36397a9217aSAntonio Huete Jimenez 	case BPF_ALU|BPF_MOD|BPF_K:
36497a9217aSAntonio Huete Jimenez 		op = "mod";
365*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
3663a289941SAaron LI 		operand = operand_buf;
36797a9217aSAntonio Huete Jimenez 		break;
36897a9217aSAntonio Huete Jimenez 
3691077d0bdSPeter Avalos 	case BPF_ALU|BPF_AND|BPF_K:
3701077d0bdSPeter Avalos 		op = "and";
371*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
3723a289941SAaron LI 		operand = operand_buf;
3731077d0bdSPeter Avalos 		break;
3741077d0bdSPeter Avalos 
3751077d0bdSPeter Avalos 	case BPF_ALU|BPF_OR|BPF_K:
3761077d0bdSPeter Avalos 		op = "or";
377*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
3783a289941SAaron LI 		operand = operand_buf;
3791077d0bdSPeter Avalos 		break;
3801077d0bdSPeter Avalos 
38197a9217aSAntonio Huete Jimenez 	case BPF_ALU|BPF_XOR|BPF_K:
38297a9217aSAntonio Huete Jimenez 		op = "xor";
383*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
3843a289941SAaron LI 		operand = operand_buf;
38597a9217aSAntonio Huete Jimenez 		break;
38697a9217aSAntonio Huete Jimenez 
3871077d0bdSPeter Avalos 	case BPF_ALU|BPF_LSH|BPF_K:
3881077d0bdSPeter Avalos 		op = "lsh";
389*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
3903a289941SAaron LI 		operand = operand_buf;
3911077d0bdSPeter Avalos 		break;
3921077d0bdSPeter Avalos 
3931077d0bdSPeter Avalos 	case BPF_ALU|BPF_RSH|BPF_K:
3941077d0bdSPeter Avalos 		op = "rsh";
395*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
3963a289941SAaron LI 		operand = operand_buf;
3971077d0bdSPeter Avalos 		break;
3981077d0bdSPeter Avalos 
3991077d0bdSPeter Avalos 	case BPF_ALU|BPF_NEG:
4001077d0bdSPeter Avalos 		op = "neg";
4013a289941SAaron LI 		operand = "";
4021077d0bdSPeter Avalos 		break;
4031077d0bdSPeter Avalos 
4041077d0bdSPeter Avalos 	case BPF_MISC|BPF_TAX:
4051077d0bdSPeter Avalos 		op = "tax";
4063a289941SAaron LI 		operand = "";
4071077d0bdSPeter Avalos 		break;
4081077d0bdSPeter Avalos 
4091077d0bdSPeter Avalos 	case BPF_MISC|BPF_TXA:
4101077d0bdSPeter Avalos 		op = "txa";
4113a289941SAaron LI 		operand = "";
4121077d0bdSPeter Avalos 		break;
4131077d0bdSPeter Avalos 	}
4140e381983SMatthew Dillon 	if (BPF_CLASS(p->code) == BPF_JMP && BPF_OP(p->code) != BPF_JA) {
415*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(image, sizeof image,
4160e381983SMatthew Dillon 			      "(%03d) %-8s %-16s jt %d\tjf %d",
4171077d0bdSPeter Avalos 			      n, op, operand, n + 1 + p->jt, n + 1 + p->jf);
4180e381983SMatthew Dillon 	} else {
419*ea16f64eSAntonio Huete Jimenez 		(void)snprintf(image, sizeof image,
4200e381983SMatthew Dillon 			      "(%03d) %-8s %s",
4210e381983SMatthew Dillon 			      n, op, operand);
4220e381983SMatthew Dillon 	}
4231077d0bdSPeter Avalos 	return image;
4241077d0bdSPeter Avalos }
425