Home | History | Annotate | Line # | Download | only in dist
      1 /*	$NetBSD: gencode.h,v 1.9 2024/09/02 15:33:37 christos 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 
     24 #ifndef gencode_h
     25 #define gencode_h
     26 
     27 #include "pcap/funcattrs.h"
     28 /*
     29  * pcap/bpf.h (a public header) needs u_char, u_short and u_int, which can be
     30  * made available via either pcap-types.h (a private header) or pcap/pcap.h
     31  * (a public header), none of which pcap/bpf.h includes.  Include the private
     32  * header to keep things simple, this way this private header should compile
     33  * even if included early from another file.
     34  */
     35 #include "pcap-types.h"
     36 #ifndef __NetBSD__
     37 #include "pcap/bpf.h" /* bpf_u_int32 and BPF_MEMWORDS */
     38 #else
     39 #include <net/bpf.h> /* bpf_u_int32 and BPF_MEMWORDS */
     40 #endif
     41 
     42 /*
     43  * ATM support:
     44  *
     45  * Copyright (c) 1997 Yen Yen Lim and North Dakota State University
     46  * All rights reserved.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. All advertising materials mentioning features or use of this software
     57  *    must display the following acknowledgement:
     58  *      This product includes software developed by Yen Yen Lim and
     59  *      North Dakota State University
     60  * 4. The name of the author may not be used to endorse or promote products
     61  *    derived from this software without specific prior written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     64  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     65  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     66  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     67  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     68  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     69  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     71  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     72  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     73  * POSSIBILITY OF SUCH DAMAGE.
     74  */
     75 
     76 /* Address qualifiers. */
     77 
     78 #define Q_HOST		1
     79 #define Q_NET		2
     80 #define Q_PORT		3
     81 #define Q_GATEWAY	4
     82 #define Q_PROTO		5
     83 #define Q_PROTOCHAIN	6
     84 #define Q_PORTRANGE	7
     85 
     86 /* Protocol qualifiers. */
     87 
     88 #define Q_LINK		1
     89 #define Q_IP		2
     90 #define Q_ARP		3
     91 #define Q_RARP		4
     92 #define Q_SCTP		5
     93 #define Q_TCP		6
     94 #define Q_UDP		7
     95 #define Q_ICMP		8
     96 #define Q_IGMP		9
     97 #define Q_IGRP		10
     98 
     99 
    100 #define	Q_ATALK		11
    101 #define	Q_DECNET	12
    102 #define	Q_LAT		13
    103 #define Q_SCA		14
    104 #define	Q_MOPRC		15
    105 #define	Q_MOPDL		16
    106 
    107 
    108 #define Q_IPV6		17
    109 #define Q_ICMPV6	18
    110 #define Q_AH		19
    111 #define Q_ESP		20
    112 
    113 #define Q_PIM		21
    114 #define Q_VRRP		22
    115 
    116 #define Q_AARP		23
    117 
    118 #define Q_ISO		24
    119 #define Q_ESIS		25
    120 #define Q_ISIS		26
    121 #define Q_CLNP		27
    122 
    123 #define Q_STP		28
    124 
    125 #define Q_IPX		29
    126 
    127 #define Q_NETBEUI	30
    128 
    129 /* IS-IS Levels */
    130 #define Q_ISIS_L1       31
    131 #define Q_ISIS_L2       32
    132 /* PDU types */
    133 #define Q_ISIS_IIH      33
    134 #define Q_ISIS_SNP      34
    135 #define Q_ISIS_CSNP     35
    136 #define Q_ISIS_PSNP     36
    137 #define Q_ISIS_LSP      37
    138 
    139 #define Q_RADIO		38
    140 
    141 #define Q_CARP		39
    142 
    143 /* Directional qualifiers. */
    144 
    145 #define Q_SRC		1
    146 #define Q_DST		2
    147 #define Q_OR		3
    148 #define Q_AND		4
    149 #define Q_ADDR1		5
    150 #define Q_ADDR2		6
    151 #define Q_ADDR3		7
    152 #define Q_ADDR4		8
    153 #define Q_RA		9
    154 #define Q_TA		10
    155 
    156 #define Q_DEFAULT	0
    157 #define Q_UNDEF		255
    158 
    159 /* ATM types */
    160 #define A_METAC		22	/* Meta signalling Circuit */
    161 #define A_BCC		23	/* Broadcast Circuit */
    162 #define A_OAMF4SC	24	/* Segment OAM F4 Circuit */
    163 #define A_OAMF4EC	25	/* End-to-End OAM F4 Circuit */
    164 #define A_SC		26	/* Signalling Circuit*/
    165 #define A_ILMIC		27	/* ILMI Circuit */
    166 #define A_OAM		28	/* OAM cells : F4 only */
    167 #define A_OAMF4		29	/* OAM F4 cells: Segment + End-to-end */
    168 #define A_LANE		30	/* LANE traffic */
    169 #define A_LLC		31	/* LLC-encapsulated traffic */
    170 
    171 /* Based on Q.2931 signalling protocol */
    172 #define A_SETUP		41	/* Setup message */
    173 #define A_CALLPROCEED	42	/* Call proceeding message */
    174 #define A_CONNECT	43	/* Connect message */
    175 #define A_CONNECTACK	44	/* Connect Ack message */
    176 #define A_RELEASE	45	/* Release message */
    177 #define A_RELEASE_DONE	46	/* Release message */
    178 
    179 /* ATM field types */
    180 #define A_VPI		51
    181 #define A_VCI		52
    182 #define A_PROTOTYPE	53
    183 #define A_MSGTYPE	54
    184 #define A_CALLREFTYPE	55
    185 
    186 #define A_CONNECTMSG	70	/* returns Q.2931 signalling messages for
    187 				   establishing and destroying switched
    188 				   virtual connection */
    189 #define A_METACONNECT	71	/* returns Q.2931 signalling messages for
    190 				   establishing and destroying predefined
    191 				   virtual circuits, such as broadcast
    192 				   circuit, oamf4 segment circuit, oamf4
    193 				   end-to-end circuits, ILMI circuits or
    194 				   connection signalling circuit. */
    195 
    196 /* MTP2 types */
    197 #define M_FISU		22	/* FISU */
    198 #define M_LSSU		23	/* LSSU */
    199 #define M_MSU		24	/* MSU */
    200 
    201 /* MTP2 HSL types */
    202 #define MH_FISU		25	/* FISU for HSL */
    203 #define MH_LSSU		26	/* LSSU */
    204 #define MH_MSU		27	/* MSU */
    205 
    206 /* MTP3 field types */
    207 #define M_SIO		1
    208 #define M_OPC		2
    209 #define M_DPC		3
    210 #define M_SLS		4
    211 
    212 /* MTP3 field types in case of MTP2 HSL */
    213 #define MH_SIO		5
    214 #define MH_OPC		6
    215 #define MH_DPC		7
    216 #define MH_SLS		8
    217 
    218 
    219 struct slist;
    220 
    221 /*
    222  * A single statement, corresponding to an instruction in a block.
    223  */
    224 struct stmt {
    225 	int code;		/* opcode */
    226 	struct slist *jt;	/* only for relative jump in block */
    227 	struct slist *jf;	/* only for relative jump in block */
    228 	bpf_u_int32 k;		/* k field */
    229 };
    230 
    231 struct slist {
    232 	struct stmt s;
    233 	struct slist *next;
    234 };
    235 
    236 /*
    237  * A bit vector to represent definition sets.  We assume TOT_REGISTERS
    238  * is smaller than 8*sizeof(atomset).
    239  */
    240 typedef bpf_u_int32 atomset;
    241 #define ATOMMASK(n) (1 << (n))
    242 #define ATOMELEM(d, n) (d & ATOMMASK(n))
    243 
    244 /*
    245  * An unbounded set.
    246  */
    247 typedef bpf_u_int32 *uset;
    248 
    249 /*
    250  * Total number of atomic entities, including accumulator (A) and index (X).
    251  * We treat all these guys similarly during flow analysis.
    252  */
    253 #define N_ATOMS (BPF_MEMWORDS+2)
    254 
    255 /*
    256  * Control flow graph of a program.
    257  * This corresponds to an edge in the CFG.
    258  * It's a directed graph, so an edge has a predecessor and a successor.
    259  */
    260 struct edge {
    261 	u_int id;
    262 	int code;		/* opcode for branch corresponding to this edge */
    263 	uset edom;
    264 	struct block *succ;	/* successor vertex */
    265 	struct block *pred;	/* predecessor vertex */
    266 	struct edge *next;	/* link list of incoming edges for a node */
    267 };
    268 
    269 /*
    270  * A block is a vertex in the CFG.
    271  * It has a list of statements, with the final statement being a
    272  * branch to successor blocks.
    273  */
    274 struct block {
    275 	u_int id;
    276 	struct slist *stmts;	/* side effect stmts */
    277 	struct stmt s;		/* branch stmt */
    278 	int mark;
    279 	u_int longjt;		/* jt branch requires long jump */
    280 	u_int longjf;		/* jf branch requires long jump */
    281 	int level;
    282 	int offset;
    283 	int sense;
    284 	struct edge et;		/* edge corresponding to the jt branch */
    285 	struct edge ef;		/* edge corresponding to the jf branch */
    286 	struct block *head;
    287 	struct block *link;	/* link field used by optimizer */
    288 	uset dom;
    289 	uset closure;
    290 	struct edge *in_edges;	/* first edge in the set (linked list) of edges with this as a successor */
    291 	atomset def, kill;
    292 	atomset in_use;
    293 	atomset out_use;
    294 	int oval;		/* value ID for value tested in branch stmt */
    295 	bpf_u_int32 val[N_ATOMS];
    296 };
    297 
    298 /*
    299  * A value of 0 for val[i] means the value is unknown.
    300  */
    301 #define VAL_UNKNOWN	0
    302 
    303 struct arth {
    304 	struct block *b;	/* protocol checks */
    305 	struct slist *s;	/* stmt list */
    306 	int regno;		/* virtual register number of result */
    307 };
    308 
    309 struct qual {
    310 	unsigned char addr;
    311 	unsigned char proto;
    312 	unsigned char dir;
    313 	unsigned char pad;
    314 };
    315 
    316 struct _compiler_state;
    317 
    318 typedef struct _compiler_state compiler_state_t;
    319 
    320 struct arth *gen_loadi(compiler_state_t *, bpf_u_int32);
    321 struct arth *gen_load(compiler_state_t *, int, struct arth *, bpf_u_int32);
    322 struct arth *gen_loadlen(compiler_state_t *);
    323 struct arth *gen_neg(compiler_state_t *, struct arth *);
    324 struct arth *gen_arth(compiler_state_t *, int, struct arth *, struct arth *);
    325 
    326 void gen_and(struct block *, struct block *);
    327 void gen_or(struct block *, struct block *);
    328 void gen_not(struct block *);
    329 
    330 struct block *gen_scode(compiler_state_t *, const char *, struct qual);
    331 struct block *gen_ecode(compiler_state_t *, const char *, struct qual);
    332 struct block *gen_acode(compiler_state_t *, const char *, struct qual);
    333 struct block *gen_mcode(compiler_state_t *, const char *, const char *,
    334     bpf_u_int32, struct qual);
    335 #ifdef INET6
    336 struct block *gen_mcode6(compiler_state_t *, const char *, bpf_u_int32,
    337     struct qual);
    338 #endif
    339 struct block *gen_ncode(compiler_state_t *, const char *, bpf_u_int32,
    340     struct qual);
    341 struct block *gen_proto_abbrev(compiler_state_t *, int);
    342 struct block *gen_relation(compiler_state_t *, int, struct arth *,
    343     struct arth *, int);
    344 struct block *gen_less(compiler_state_t *, int);
    345 struct block *gen_greater(compiler_state_t *, int);
    346 struct block *gen_byteop(compiler_state_t *, int, int, bpf_u_int32);
    347 struct block *gen_broadcast(compiler_state_t *, int);
    348 struct block *gen_multicast(compiler_state_t *, int);
    349 struct block *gen_ifindex(compiler_state_t *, int);
    350 struct block *gen_inbound(compiler_state_t *, int);
    351 
    352 struct block *gen_llc(compiler_state_t *);
    353 struct block *gen_llc_i(compiler_state_t *);
    354 struct block *gen_llc_s(compiler_state_t *);
    355 struct block *gen_llc_u(compiler_state_t *);
    356 struct block *gen_llc_s_subtype(compiler_state_t *, bpf_u_int32);
    357 struct block *gen_llc_u_subtype(compiler_state_t *, bpf_u_int32);
    358 
    359 struct block *gen_vlan(compiler_state_t *, bpf_u_int32, int);
    360 struct block *gen_mpls(compiler_state_t *, bpf_u_int32, int);
    361 
    362 struct block *gen_pppoed(compiler_state_t *);
    363 struct block *gen_pppoes(compiler_state_t *, bpf_u_int32, int);
    364 
    365 struct block *gen_geneve(compiler_state_t *, bpf_u_int32, int);
    366 
    367 struct block *gen_atmfield_code(compiler_state_t *, int, bpf_u_int32,
    368     int, int);
    369 struct block *gen_atmtype_abbrev(compiler_state_t *, int);
    370 struct block *gen_atmmulti_abbrev(compiler_state_t *, int);
    371 
    372 struct block *gen_mtp2type_abbrev(compiler_state_t *, int);
    373 struct block *gen_mtp3field_code(compiler_state_t *, int, bpf_u_int32,
    374     int, int);
    375 
    376 struct block *gen_pf_ifname(compiler_state_t *, const char *);
    377 struct block *gen_pf_rnr(compiler_state_t *, int);
    378 struct block *gen_pf_srnr(compiler_state_t *, int);
    379 struct block *gen_pf_ruleset(compiler_state_t *, char *);
    380 struct block *gen_pf_reason(compiler_state_t *, int);
    381 struct block *gen_pf_action(compiler_state_t *, int);
    382 
    383 struct block *gen_p80211_type(compiler_state_t *, bpf_u_int32, bpf_u_int32);
    384 struct block *gen_p80211_fcdir(compiler_state_t *, bpf_u_int32);
    385 
    386 /*
    387  * Representation of a program as a tree of blocks, plus current mark.
    388  * A block is marked if only if its mark equals the current mark.
    389  * Rather than traverse the code array, marking each item, 'cur_mark'
    390  * is incremented.  This automatically makes each element unmarked.
    391  */
    392 #define isMarked(icp, p) ((p)->mark == (icp)->cur_mark)
    393 #define unMarkAll(icp) (icp)->cur_mark += 1
    394 #define Mark(icp, p) ((p)->mark = (icp)->cur_mark)
    395 
    396 struct icode {
    397 	struct block *root;
    398 	int cur_mark;
    399 };
    400 
    401 int bpf_optimize(struct icode *, char *);
    402 void bpf_set_error(compiler_state_t *, const char *, ...)
    403     PCAP_PRINTFLIKE(2, 3);
    404 
    405 int finish_parse(compiler_state_t *, struct block *);
    406 char *sdup(compiler_state_t *, const char *);
    407 
    408 struct bpf_insn *icode_to_fcode(struct icode *, struct block *, u_int *,
    409     char *);
    410 void sappend(struct slist *, struct slist *);
    411 
    412 /*
    413  * Older versions of Bison don't put this declaration in
    414  * grammar.h.
    415  */
    416 int pcap_parse(void *, compiler_state_t *);
    417 
    418 /* XXX */
    419 #define JT(b)  ((b)->et.succ)
    420 #define JF(b)  ((b)->ef.succ)
    421 
    422 #endif /* gencode_h */
    423