Home | History | Annotate | Line # | Download | only in dist
bpf_image.c revision 1.4
      1  1.4  christos /*	$NetBSD: bpf_image.c,v 1.4 2018/09/03 15:26:43 christos Exp $	*/
      2  1.2  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1990, 1991, 1992, 1994, 1995, 1996
      5  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that: (1) source code distributions
      9  1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
     10  1.1  christos  * distributions including binary code include the above copyright notice and
     11  1.1  christos  * this paragraph in its entirety in the documentation or other materials
     12  1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     13  1.1  christos  * features or use of this software display the following acknowledgement:
     14  1.1  christos  * ``This product includes software developed by the University of California,
     15  1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     16  1.1  christos  * the University nor the names of its contributors may be used to endorse
     17  1.1  christos  * or promote products derived from this software without specific prior
     18  1.1  christos  * written permission.
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     20  1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     21  1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     22  1.1  christos  */
     23  1.1  christos 
     24  1.2  christos #include <sys/cdefs.h>
     25  1.4  christos __RCSID("$NetBSD: bpf_image.c,v 1.4 2018/09/03 15:26:43 christos Exp $");
     26  1.1  christos 
     27  1.1  christos #ifdef HAVE_CONFIG_H
     28  1.4  christos #include <config.h>
     29  1.1  christos #endif
     30  1.1  christos 
     31  1.4  christos #include <pcap-types.h>
     32  1.1  christos 
     33  1.1  christos #include <stdio.h>
     34  1.1  christos #include <string.h>
     35  1.1  christos 
     36  1.1  christos #include "pcap-int.h"
     37  1.1  christos 
     38  1.1  christos #ifdef HAVE_OS_PROTO_H
     39  1.1  christos #include "os-proto.h"
     40  1.1  christos #endif
     41  1.1  christos 
     42  1.1  christos char *
     43  1.4  christos bpf_image(const struct bpf_insn *p, int n)
     44  1.1  christos {
     45  1.4  christos 	const char *op;
     46  1.1  christos 	static char image[256];
     47  1.4  christos 	char operand_buf[64];
     48  1.4  christos 	const char *operand;
     49  1.1  christos 
     50  1.1  christos 	switch (p->code) {
     51  1.1  christos 
     52  1.1  christos 	default:
     53  1.1  christos 		op = "unimp";
     54  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "0x%x", p->code);
     55  1.4  christos 		operand = operand_buf;
     56  1.1  christos 		break;
     57  1.1  christos 
     58  1.1  christos 	case BPF_RET|BPF_K:
     59  1.1  christos 		op = "ret";
     60  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
     61  1.4  christos 		operand = operand_buf;
     62  1.1  christos 		break;
     63  1.1  christos 
     64  1.1  christos 	case BPF_RET|BPF_A:
     65  1.1  christos 		op = "ret";
     66  1.4  christos 		operand = "";
     67  1.1  christos 		break;
     68  1.1  christos 
     69  1.1  christos 	case BPF_LD|BPF_W|BPF_ABS:
     70  1.1  christos 		op = "ld";
     71  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "[%d]", p->k);
     72  1.4  christos 		operand = operand_buf;
     73  1.1  christos 		break;
     74  1.1  christos 
     75  1.1  christos 	case BPF_LD|BPF_H|BPF_ABS:
     76  1.1  christos 		op = "ldh";
     77  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "[%d]", p->k);
     78  1.4  christos 		operand = operand_buf;
     79  1.1  christos 		break;
     80  1.1  christos 
     81  1.1  christos 	case BPF_LD|BPF_B|BPF_ABS:
     82  1.1  christos 		op = "ldb";
     83  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "[%d]", p->k);
     84  1.4  christos 		operand = operand_buf;
     85  1.1  christos 		break;
     86  1.1  christos 
     87  1.1  christos 	case BPF_LD|BPF_W|BPF_LEN:
     88  1.1  christos 		op = "ld";
     89  1.4  christos 		operand = "#pktlen";
     90  1.1  christos 		break;
     91  1.1  christos 
     92  1.1  christos 	case BPF_LD|BPF_W|BPF_IND:
     93  1.1  christos 		op = "ld";
     94  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "[x + %d]", p->k);
     95  1.4  christos 		operand = operand_buf;
     96  1.1  christos 		break;
     97  1.1  christos 
     98  1.1  christos 	case BPF_LD|BPF_H|BPF_IND:
     99  1.1  christos 		op = "ldh";
    100  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "[x + %d]", p->k);
    101  1.4  christos 		operand = operand_buf;
    102  1.1  christos 		break;
    103  1.1  christos 
    104  1.1  christos 	case BPF_LD|BPF_B|BPF_IND:
    105  1.1  christos 		op = "ldb";
    106  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "[x + %d]", p->k);
    107  1.4  christos 		operand = operand_buf;
    108  1.1  christos 		break;
    109  1.1  christos 
    110  1.1  christos 	case BPF_LD|BPF_IMM:
    111  1.1  christos 		op = "ld";
    112  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
    113  1.4  christos 		operand = operand_buf;
    114  1.1  christos 		break;
    115  1.1  christos 
    116  1.1  christos 	case BPF_LDX|BPF_IMM:
    117  1.1  christos 		op = "ldx";
    118  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
    119  1.4  christos 		operand = operand_buf;
    120  1.1  christos 		break;
    121  1.1  christos 
    122  1.1  christos 	case BPF_LDX|BPF_MSH|BPF_B:
    123  1.1  christos 		op = "ldxb";
    124  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "4*([%d]&0xf)", p->k);
    125  1.4  christos 		operand = operand_buf;
    126  1.1  christos 		break;
    127  1.1  christos 
    128  1.1  christos 	case BPF_LD|BPF_MEM:
    129  1.1  christos 		op = "ld";
    130  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "M[%d]", p->k);
    131  1.4  christos 		operand = operand_buf;
    132  1.1  christos 		break;
    133  1.1  christos 
    134  1.1  christos 	case BPF_LDX|BPF_MEM:
    135  1.1  christos 		op = "ldx";
    136  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "M[%d]", p->k);
    137  1.4  christos 		operand = operand_buf;
    138  1.1  christos 		break;
    139  1.1  christos 
    140  1.1  christos 	case BPF_ST:
    141  1.1  christos 		op = "st";
    142  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "M[%d]", p->k);
    143  1.4  christos 		operand = operand_buf;
    144  1.1  christos 		break;
    145  1.1  christos 
    146  1.1  christos 	case BPF_STX:
    147  1.1  christos 		op = "stx";
    148  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "M[%d]", p->k);
    149  1.4  christos 		operand = operand_buf;
    150  1.1  christos 		break;
    151  1.1  christos 
    152  1.1  christos 	case BPF_JMP|BPF_JA:
    153  1.1  christos 		op = "ja";
    154  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "%d", n + 1 + p->k);
    155  1.4  christos 		operand = operand_buf;
    156  1.1  christos 		break;
    157  1.1  christos 
    158  1.1  christos 	case BPF_JMP|BPF_JGT|BPF_K:
    159  1.1  christos 		op = "jgt";
    160  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
    161  1.4  christos 		operand = operand_buf;
    162  1.1  christos 		break;
    163  1.1  christos 
    164  1.1  christos 	case BPF_JMP|BPF_JGE|BPF_K:
    165  1.1  christos 		op = "jge";
    166  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
    167  1.4  christos 		operand = operand_buf;
    168  1.1  christos 		break;
    169  1.1  christos 
    170  1.1  christos 	case BPF_JMP|BPF_JEQ|BPF_K:
    171  1.1  christos 		op = "jeq";
    172  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
    173  1.4  christos 		operand = operand_buf;
    174  1.1  christos 		break;
    175  1.1  christos 
    176  1.1  christos 	case BPF_JMP|BPF_JSET|BPF_K:
    177  1.1  christos 		op = "jset";
    178  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
    179  1.4  christos 		operand = operand_buf;
    180  1.1  christos 		break;
    181  1.1  christos 
    182  1.1  christos 	case BPF_JMP|BPF_JGT|BPF_X:
    183  1.1  christos 		op = "jgt";
    184  1.4  christos 		operand = "x";
    185  1.1  christos 		break;
    186  1.1  christos 
    187  1.1  christos 	case BPF_JMP|BPF_JGE|BPF_X:
    188  1.1  christos 		op = "jge";
    189  1.4  christos 		operand = "x";
    190  1.1  christos 		break;
    191  1.1  christos 
    192  1.1  christos 	case BPF_JMP|BPF_JEQ|BPF_X:
    193  1.1  christos 		op = "jeq";
    194  1.4  christos 		operand = "x";
    195  1.1  christos 		break;
    196  1.1  christos 
    197  1.1  christos 	case BPF_JMP|BPF_JSET|BPF_X:
    198  1.1  christos 		op = "jset";
    199  1.4  christos 		operand = "x";
    200  1.1  christos 		break;
    201  1.1  christos 
    202  1.1  christos 	case BPF_ALU|BPF_ADD|BPF_X:
    203  1.1  christos 		op = "add";
    204  1.4  christos 		operand = "x";
    205  1.1  christos 		break;
    206  1.1  christos 
    207  1.1  christos 	case BPF_ALU|BPF_SUB|BPF_X:
    208  1.1  christos 		op = "sub";
    209  1.4  christos 		operand = "x";
    210  1.1  christos 		break;
    211  1.1  christos 
    212  1.1  christos 	case BPF_ALU|BPF_MUL|BPF_X:
    213  1.1  christos 		op = "mul";
    214  1.4  christos 		operand = "x";
    215  1.1  christos 		break;
    216  1.1  christos 
    217  1.1  christos 	case BPF_ALU|BPF_DIV|BPF_X:
    218  1.1  christos 		op = "div";
    219  1.4  christos 		operand = "x";
    220  1.1  christos 		break;
    221  1.1  christos 
    222  1.2  christos 	case BPF_ALU|BPF_MOD|BPF_X:
    223  1.2  christos 		op = "mod";
    224  1.4  christos 		operand = "x";
    225  1.2  christos 		break;
    226  1.2  christos 
    227  1.1  christos 	case BPF_ALU|BPF_AND|BPF_X:
    228  1.1  christos 		op = "and";
    229  1.4  christos 		operand = "x";
    230  1.1  christos 		break;
    231  1.1  christos 
    232  1.1  christos 	case BPF_ALU|BPF_OR|BPF_X:
    233  1.1  christos 		op = "or";
    234  1.4  christos 		operand = "x";
    235  1.1  christos 		break;
    236  1.1  christos 
    237  1.2  christos 	case BPF_ALU|BPF_XOR|BPF_X:
    238  1.2  christos 		op = "xor";
    239  1.4  christos 		operand = "x";
    240  1.2  christos 		break;
    241  1.2  christos 
    242  1.1  christos 	case BPF_ALU|BPF_LSH|BPF_X:
    243  1.1  christos 		op = "lsh";
    244  1.4  christos 		operand = "x";
    245  1.1  christos 		break;
    246  1.1  christos 
    247  1.1  christos 	case BPF_ALU|BPF_RSH|BPF_X:
    248  1.1  christos 		op = "rsh";
    249  1.4  christos 		operand = "x";
    250  1.1  christos 		break;
    251  1.1  christos 
    252  1.1  christos 	case BPF_ALU|BPF_ADD|BPF_K:
    253  1.1  christos 		op = "add";
    254  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
    255  1.4  christos 		operand = operand_buf;
    256  1.1  christos 		break;
    257  1.1  christos 
    258  1.1  christos 	case BPF_ALU|BPF_SUB|BPF_K:
    259  1.1  christos 		op = "sub";
    260  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
    261  1.4  christos 		operand = operand_buf;
    262  1.1  christos 		break;
    263  1.1  christos 
    264  1.1  christos 	case BPF_ALU|BPF_MUL|BPF_K:
    265  1.1  christos 		op = "mul";
    266  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
    267  1.4  christos 		operand = operand_buf;
    268  1.1  christos 		break;
    269  1.1  christos 
    270  1.1  christos 	case BPF_ALU|BPF_DIV|BPF_K:
    271  1.1  christos 		op = "div";
    272  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
    273  1.4  christos 		operand = operand_buf;
    274  1.1  christos 		break;
    275  1.1  christos 
    276  1.2  christos 	case BPF_ALU|BPF_MOD|BPF_K:
    277  1.2  christos 		op = "mod";
    278  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
    279  1.4  christos 		operand = operand_buf;
    280  1.2  christos 		break;
    281  1.2  christos 
    282  1.1  christos 	case BPF_ALU|BPF_AND|BPF_K:
    283  1.1  christos 		op = "and";
    284  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
    285  1.4  christos 		operand = operand_buf;
    286  1.1  christos 		break;
    287  1.1  christos 
    288  1.1  christos 	case BPF_ALU|BPF_OR|BPF_K:
    289  1.1  christos 		op = "or";
    290  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
    291  1.4  christos 		operand = operand_buf;
    292  1.1  christos 		break;
    293  1.1  christos 
    294  1.2  christos 	case BPF_ALU|BPF_XOR|BPF_K:
    295  1.2  christos 		op = "xor";
    296  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
    297  1.4  christos 		operand = operand_buf;
    298  1.2  christos 		break;
    299  1.2  christos 
    300  1.1  christos 	case BPF_ALU|BPF_LSH|BPF_K:
    301  1.1  christos 		op = "lsh";
    302  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
    303  1.4  christos 		operand = operand_buf;
    304  1.1  christos 		break;
    305  1.1  christos 
    306  1.1  christos 	case BPF_ALU|BPF_RSH|BPF_K:
    307  1.1  christos 		op = "rsh";
    308  1.4  christos 		(void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
    309  1.4  christos 		operand = operand_buf;
    310  1.1  christos 		break;
    311  1.1  christos 
    312  1.1  christos 	case BPF_ALU|BPF_NEG:
    313  1.1  christos 		op = "neg";
    314  1.4  christos 		operand = "";
    315  1.1  christos 		break;
    316  1.1  christos 
    317  1.1  christos 	case BPF_MISC|BPF_TAX:
    318  1.1  christos 		op = "tax";
    319  1.4  christos 		operand = "";
    320  1.1  christos 		break;
    321  1.1  christos 
    322  1.1  christos 	case BPF_MISC|BPF_TXA:
    323  1.1  christos 		op = "txa";
    324  1.4  christos 		operand = "";
    325  1.1  christos 		break;
    326  1.1  christos 	}
    327  1.2  christos 	if (BPF_CLASS(p->code) == BPF_JMP && BPF_OP(p->code) != BPF_JA) {
    328  1.3  christos 		(void)pcap_snprintf(image, sizeof image,
    329  1.2  christos 			      "(%03d) %-8s %-16s jt %d\tjf %d",
    330  1.2  christos 			      n, op, operand, n + 1 + p->jt, n + 1 + p->jf);
    331  1.2  christos 	} else {
    332  1.3  christos 		(void)pcap_snprintf(image, sizeof image,
    333  1.2  christos 			      "(%03d) %-8s %s",
    334  1.2  christos 			      n, op, operand);
    335  1.2  christos 	}
    336  1.1  christos 	return image;
    337  1.1  christos }
    338