1 /* $OpenBSD: gencode.h,v 1.7 2000/04/26 21:25:52 jakob Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 * 23 * @(#) $Header: /home/cvs/src/lib/libpcap/gencode.h,v 1.7 2000/04/26 21:25:52 jakob Exp $ (LBL) 24 */ 25 26 /* Address qualifiers. */ 27 28 #define Q_HOST 1 29 #define Q_NET 2 30 #define Q_PORT 3 31 #define Q_GATEWAY 4 32 #define Q_PROTO 5 33 #define Q_PROTOCHAIN 6 34 35 /* Protocol qualifiers. */ 36 37 #define Q_LINK 1 38 #define Q_IP 2 39 #define Q_ARP 3 40 #define Q_RARP 4 41 #define Q_TCP 5 42 #define Q_UDP 6 43 #define Q_ICMP 7 44 #define Q_IGMP 8 45 #define Q_IGRP 9 46 47 48 #define Q_ATALK 10 49 #define Q_DECNET 11 50 #define Q_LAT 12 51 #define Q_SCA 13 52 #define Q_MOPRC 14 53 #define Q_MOPDL 15 54 55 56 #define Q_IPV6 16 57 #define Q_ICMPV6 17 58 #define Q_AH 18 59 #define Q_ESP 19 60 61 #define Q_PIM 20 62 63 /* Directional qualifiers. */ 64 65 #define Q_SRC 1 66 #define Q_DST 2 67 #define Q_OR 3 68 #define Q_AND 4 69 70 #define Q_DEFAULT 0 71 #define Q_UNDEF 255 72 73 struct slist; 74 75 struct stmt { 76 int code; 77 struct slist *jt; /*only for relative jump in block*/ 78 struct slist *jf; /*only for relative jump in block*/ 79 bpf_int32 k; 80 }; 81 82 struct slist { 83 struct stmt s; 84 struct slist *next; 85 }; 86 87 /* 88 * A bit vector to represent definition sets. We assume TOT_REGISTERS 89 * is smaller than 8*sizeof(atomset). 90 */ 91 typedef bpf_u_int32 atomset; 92 #define ATOMMASK(n) (1 << (n)) 93 #define ATOMELEM(d, n) (d & ATOMMASK(n)) 94 95 /* 96 * An unbounded set. 97 */ 98 typedef bpf_u_int32 *uset; 99 100 /* 101 * Total number of atomic entities, including accumulator (A) and index (X). 102 * We treat all these guys similarly during flow analysis. 103 */ 104 #define N_ATOMS (BPF_MEMWORDS+2) 105 106 struct edge { 107 int id; 108 int code; 109 uset edom; 110 struct block *succ; 111 struct block *pred; 112 struct edge *next; /* link list of incoming edges for a node */ 113 }; 114 115 struct block { 116 int id; 117 struct slist *stmts; /* side effect stmts */ 118 struct stmt s; /* branch stmt */ 119 int mark; 120 int longjt; /* jt branch requires long jump */ 121 int longjf; /* jf branch requires long jump */ 122 int level; 123 int offset; 124 int sense; 125 struct edge et; 126 struct edge ef; 127 struct block *head; 128 struct block *link; /* link field used by optimizer */ 129 uset dom; 130 uset closure; 131 struct edge *in_edges; 132 atomset def, kill; 133 atomset in_use; 134 atomset out_use; 135 int oval; 136 int val[N_ATOMS]; 137 }; 138 139 struct arth { 140 struct block *b; /* protocol checks */ 141 struct slist *s; /* stmt list */ 142 int regno; /* virtual register number of result */ 143 }; 144 145 struct qual { 146 unsigned char addr; 147 unsigned char proto; 148 unsigned char dir; 149 unsigned char pad; 150 }; 151 152 struct arth *gen_loadi(int); 153 struct arth *gen_load(int, struct arth *, int); 154 struct arth *gen_loadlen(void); 155 struct arth *gen_neg(struct arth *); 156 struct arth *gen_arth(int, struct arth *, struct arth *); 157 158 void gen_and(struct block *, struct block *); 159 void gen_or(struct block *, struct block *); 160 void gen_not(struct block *); 161 162 struct block *gen_scode(const char *, struct qual); 163 struct block *gen_ecode(const u_char *, struct qual); 164 struct block *gen_acode(const u_char *, struct qual); 165 struct block *gen_mcode(const char *, const char *, int, struct qual); 166 #ifdef INET6 167 struct block *gen_mcode6(const char *, const char *, int, struct qual); 168 #endif 169 struct block *gen_ncode(const char *, bpf_u_int32, struct qual); 170 struct block *gen_proto_abbrev(int); 171 struct block *gen_relation(int, struct arth *, struct arth *, int); 172 struct block *gen_less(int); 173 struct block *gen_greater(int); 174 struct block *gen_byteop(int, int, int); 175 struct block *gen_broadcast(int); 176 struct block *gen_multicast(int); 177 struct block *gen_inbound(int); 178 179 void bpf_optimize(struct block **); 180 #ifdef __STDC__ 181 __dead void bpf_error(const char *, ...) 182 __attribute__((volatile, format (printf, 1, 2))); 183 #endif 184 185 void finish_parse(struct block *); 186 char *sdup(const char *); 187 188 struct bpf_insn *icode_to_fcode(struct block *, int *); 189 int pcap_parse(void); 190 void lex_init(char *); 191 void sappend(struct slist *, struct slist *); 192 193 /* XXX */ 194 #define JT(b) ((b)->et.succ) 195 #define JF(b) ((b)->ef.succ) 196 197 extern int no_optimize; 198