1*d5dfe396Schristos /* $NetBSD: gencode.h,v 1.7 2019/10/01 16:02:11 christos Exp $ */ 2e3899b6dSchristos 354a6ec8aSchristos /* 454a6ec8aSchristos * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996 554a6ec8aSchristos * The Regents of the University of California. All rights reserved. 654a6ec8aSchristos * 754a6ec8aSchristos * Redistribution and use in source and binary forms, with or without 854a6ec8aSchristos * modification, are permitted provided that: (1) source code distributions 954a6ec8aSchristos * retain the above copyright notice and this paragraph in its entirety, (2) 1054a6ec8aSchristos * distributions including binary code include the above copyright notice and 1154a6ec8aSchristos * this paragraph in its entirety in the documentation or other materials 1254a6ec8aSchristos * provided with the distribution, and (3) all advertising materials mentioning 1354a6ec8aSchristos * features or use of this software display the following acknowledgement: 1454a6ec8aSchristos * ``This product includes software developed by the University of California, 1554a6ec8aSchristos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 1654a6ec8aSchristos * the University nor the names of its contributors may be used to endorse 1754a6ec8aSchristos * or promote products derived from this software without specific prior 1854a6ec8aSchristos * written permission. 1954a6ec8aSchristos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 2054a6ec8aSchristos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 2154a6ec8aSchristos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 2254a6ec8aSchristos */ 2354a6ec8aSchristos 24e1375535Schristos #include "pcap/funcattrs.h" 25e1375535Schristos 2654a6ec8aSchristos /* 2754a6ec8aSchristos * ATM support: 2854a6ec8aSchristos * 2954a6ec8aSchristos * Copyright (c) 1997 Yen Yen Lim and North Dakota State University 3054a6ec8aSchristos * All rights reserved. 3154a6ec8aSchristos * 3254a6ec8aSchristos * Redistribution and use in source and binary forms, with or without 3354a6ec8aSchristos * modification, are permitted provided that the following conditions 3454a6ec8aSchristos * are met: 3554a6ec8aSchristos * 1. Redistributions of source code must retain the above copyright 3654a6ec8aSchristos * notice, this list of conditions and the following disclaimer. 3754a6ec8aSchristos * 2. Redistributions in binary form must reproduce the above copyright 3854a6ec8aSchristos * notice, this list of conditions and the following disclaimer in the 3954a6ec8aSchristos * documentation and/or other materials provided with the distribution. 4054a6ec8aSchristos * 3. All advertising materials mentioning features or use of this software 4154a6ec8aSchristos * must display the following acknowledgement: 4254a6ec8aSchristos * This product includes software developed by Yen Yen Lim and 4354a6ec8aSchristos * North Dakota State University 4454a6ec8aSchristos * 4. The name of the author may not be used to endorse or promote products 4554a6ec8aSchristos * derived from this software without specific prior written permission. 4654a6ec8aSchristos * 4754a6ec8aSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 4854a6ec8aSchristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 4954a6ec8aSchristos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 5054a6ec8aSchristos * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 5154a6ec8aSchristos * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 5254a6ec8aSchristos * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 5354a6ec8aSchristos * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5454a6ec8aSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 5554a6ec8aSchristos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 5654a6ec8aSchristos * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 5754a6ec8aSchristos * POSSIBILITY OF SUCH DAMAGE. 5854a6ec8aSchristos */ 5954a6ec8aSchristos 6054a6ec8aSchristos /* Address qualifiers. */ 6154a6ec8aSchristos 6254a6ec8aSchristos #define Q_HOST 1 6354a6ec8aSchristos #define Q_NET 2 6454a6ec8aSchristos #define Q_PORT 3 6554a6ec8aSchristos #define Q_GATEWAY 4 6654a6ec8aSchristos #define Q_PROTO 5 6754a6ec8aSchristos #define Q_PROTOCHAIN 6 6854a6ec8aSchristos #define Q_PORTRANGE 7 6954a6ec8aSchristos 7054a6ec8aSchristos /* Protocol qualifiers. */ 7154a6ec8aSchristos 7254a6ec8aSchristos #define Q_LINK 1 7354a6ec8aSchristos #define Q_IP 2 7454a6ec8aSchristos #define Q_ARP 3 7554a6ec8aSchristos #define Q_RARP 4 7654a6ec8aSchristos #define Q_SCTP 5 7754a6ec8aSchristos #define Q_TCP 6 7854a6ec8aSchristos #define Q_UDP 7 7954a6ec8aSchristos #define Q_ICMP 8 8054a6ec8aSchristos #define Q_IGMP 9 8154a6ec8aSchristos #define Q_IGRP 10 8254a6ec8aSchristos 8354a6ec8aSchristos 8454a6ec8aSchristos #define Q_ATALK 11 8554a6ec8aSchristos #define Q_DECNET 12 8654a6ec8aSchristos #define Q_LAT 13 8754a6ec8aSchristos #define Q_SCA 14 8854a6ec8aSchristos #define Q_MOPRC 15 8954a6ec8aSchristos #define Q_MOPDL 16 9054a6ec8aSchristos 9154a6ec8aSchristos 9254a6ec8aSchristos #define Q_IPV6 17 9354a6ec8aSchristos #define Q_ICMPV6 18 9454a6ec8aSchristos #define Q_AH 19 9554a6ec8aSchristos #define Q_ESP 20 9654a6ec8aSchristos 9754a6ec8aSchristos #define Q_PIM 21 9854a6ec8aSchristos #define Q_VRRP 22 9954a6ec8aSchristos 10054a6ec8aSchristos #define Q_AARP 23 10154a6ec8aSchristos 10254a6ec8aSchristos #define Q_ISO 24 10354a6ec8aSchristos #define Q_ESIS 25 10454a6ec8aSchristos #define Q_ISIS 26 10554a6ec8aSchristos #define Q_CLNP 27 10654a6ec8aSchristos 10754a6ec8aSchristos #define Q_STP 28 10854a6ec8aSchristos 10954a6ec8aSchristos #define Q_IPX 29 11054a6ec8aSchristos 11154a6ec8aSchristos #define Q_NETBEUI 30 11254a6ec8aSchristos 11354a6ec8aSchristos /* IS-IS Levels */ 11454a6ec8aSchristos #define Q_ISIS_L1 31 11554a6ec8aSchristos #define Q_ISIS_L2 32 11654a6ec8aSchristos /* PDU types */ 11754a6ec8aSchristos #define Q_ISIS_IIH 33 118*d5dfe396Schristos #define Q_ISIS_SNP 34 119*d5dfe396Schristos #define Q_ISIS_CSNP 35 120*d5dfe396Schristos #define Q_ISIS_PSNP 36 121*d5dfe396Schristos #define Q_ISIS_LSP 37 12254a6ec8aSchristos 123*d5dfe396Schristos #define Q_RADIO 38 12454a6ec8aSchristos 125*d5dfe396Schristos #define Q_CARP 39 126774c01f3Schristos 12754a6ec8aSchristos /* Directional qualifiers. */ 12854a6ec8aSchristos 12954a6ec8aSchristos #define Q_SRC 1 13054a6ec8aSchristos #define Q_DST 2 13154a6ec8aSchristos #define Q_OR 3 13254a6ec8aSchristos #define Q_AND 4 13354a6ec8aSchristos #define Q_ADDR1 5 13454a6ec8aSchristos #define Q_ADDR2 6 13554a6ec8aSchristos #define Q_ADDR3 7 13654a6ec8aSchristos #define Q_ADDR4 8 137774c01f3Schristos #define Q_RA 9 138774c01f3Schristos #define Q_TA 10 13954a6ec8aSchristos 14054a6ec8aSchristos #define Q_DEFAULT 0 14154a6ec8aSchristos #define Q_UNDEF 255 14254a6ec8aSchristos 14354a6ec8aSchristos /* ATM types */ 14454a6ec8aSchristos #define A_METAC 22 /* Meta signalling Circuit */ 14554a6ec8aSchristos #define A_BCC 23 /* Broadcast Circuit */ 14654a6ec8aSchristos #define A_OAMF4SC 24 /* Segment OAM F4 Circuit */ 14754a6ec8aSchristos #define A_OAMF4EC 25 /* End-to-End OAM F4 Circuit */ 14854a6ec8aSchristos #define A_SC 26 /* Signalling Circuit*/ 14954a6ec8aSchristos #define A_ILMIC 27 /* ILMI Circuit */ 15054a6ec8aSchristos #define A_OAM 28 /* OAM cells : F4 only */ 15154a6ec8aSchristos #define A_OAMF4 29 /* OAM F4 cells: Segment + End-to-end */ 15254a6ec8aSchristos #define A_LANE 30 /* LANE traffic */ 15354a6ec8aSchristos #define A_LLC 31 /* LLC-encapsulated traffic */ 15454a6ec8aSchristos 15554a6ec8aSchristos /* Based on Q.2931 signalling protocol */ 15654a6ec8aSchristos #define A_SETUP 41 /* Setup message */ 15754a6ec8aSchristos #define A_CALLPROCEED 42 /* Call proceeding message */ 15854a6ec8aSchristos #define A_CONNECT 43 /* Connect message */ 15954a6ec8aSchristos #define A_CONNECTACK 44 /* Connect Ack message */ 16054a6ec8aSchristos #define A_RELEASE 45 /* Release message */ 16154a6ec8aSchristos #define A_RELEASE_DONE 46 /* Release message */ 16254a6ec8aSchristos 16354a6ec8aSchristos /* ATM field types */ 16454a6ec8aSchristos #define A_VPI 51 16554a6ec8aSchristos #define A_VCI 52 16654a6ec8aSchristos #define A_PROTOTYPE 53 16754a6ec8aSchristos #define A_MSGTYPE 54 16854a6ec8aSchristos #define A_CALLREFTYPE 55 16954a6ec8aSchristos 17054a6ec8aSchristos #define A_CONNECTMSG 70 /* returns Q.2931 signalling messages for 17154a6ec8aSchristos establishing and destroying switched 17254a6ec8aSchristos virtual connection */ 17354a6ec8aSchristos #define A_METACONNECT 71 /* returns Q.2931 signalling messages for 17454a6ec8aSchristos establishing and destroying predefined 17554a6ec8aSchristos virtual circuits, such as broadcast 17654a6ec8aSchristos circuit, oamf4 segment circuit, oamf4 17754a6ec8aSchristos end-to-end circuits, ILMI circuits or 17854a6ec8aSchristos connection signalling circuit. */ 17954a6ec8aSchristos 18054a6ec8aSchristos /* MTP2 types */ 18154a6ec8aSchristos #define M_FISU 22 /* FISU */ 18254a6ec8aSchristos #define M_LSSU 23 /* LSSU */ 18354a6ec8aSchristos #define M_MSU 24 /* MSU */ 18454a6ec8aSchristos 185c15d5effSchristos /* MTP2 HSL types */ 186c15d5effSchristos #define MH_FISU 25 /* FISU for HSL */ 187c15d5effSchristos #define MH_LSSU 26 /* LSSU */ 188c15d5effSchristos #define MH_MSU 27 /* MSU */ 189c15d5effSchristos 19054a6ec8aSchristos /* MTP3 field types */ 19154a6ec8aSchristos #define M_SIO 1 19254a6ec8aSchristos #define M_OPC 2 19354a6ec8aSchristos #define M_DPC 3 19454a6ec8aSchristos #define M_SLS 4 19554a6ec8aSchristos 196c15d5effSchristos /* MTP3 field types in case of MTP2 HSL */ 197c15d5effSchristos #define MH_SIO 5 198c15d5effSchristos #define MH_OPC 6 199c15d5effSchristos #define MH_DPC 7 200c15d5effSchristos #define MH_SLS 8 201c15d5effSchristos 20254a6ec8aSchristos 20354a6ec8aSchristos struct slist; 20454a6ec8aSchristos 20554a6ec8aSchristos struct stmt { 20654a6ec8aSchristos int code; 20754a6ec8aSchristos struct slist *jt; /*only for relative jump in block*/ 20854a6ec8aSchristos struct slist *jf; /*only for relative jump in block*/ 20954a6ec8aSchristos bpf_int32 k; 21054a6ec8aSchristos }; 21154a6ec8aSchristos 21254a6ec8aSchristos struct slist { 21354a6ec8aSchristos struct stmt s; 21454a6ec8aSchristos struct slist *next; 21554a6ec8aSchristos }; 21654a6ec8aSchristos 21754a6ec8aSchristos /* 21854a6ec8aSchristos * A bit vector to represent definition sets. We assume TOT_REGISTERS 21954a6ec8aSchristos * is smaller than 8*sizeof(atomset). 22054a6ec8aSchristos */ 22154a6ec8aSchristos typedef bpf_u_int32 atomset; 22254a6ec8aSchristos #define ATOMMASK(n) (1 << (n)) 22354a6ec8aSchristos #define ATOMELEM(d, n) (d & ATOMMASK(n)) 22454a6ec8aSchristos 22554a6ec8aSchristos /* 22654a6ec8aSchristos * An unbounded set. 22754a6ec8aSchristos */ 22854a6ec8aSchristos typedef bpf_u_int32 *uset; 22954a6ec8aSchristos 23054a6ec8aSchristos /* 23154a6ec8aSchristos * Total number of atomic entities, including accumulator (A) and index (X). 23254a6ec8aSchristos * We treat all these guys similarly during flow analysis. 23354a6ec8aSchristos */ 23454a6ec8aSchristos #define N_ATOMS (BPF_MEMWORDS+2) 23554a6ec8aSchristos 23654a6ec8aSchristos struct edge { 23754a6ec8aSchristos int id; 23854a6ec8aSchristos int code; 23954a6ec8aSchristos uset edom; 24054a6ec8aSchristos struct block *succ; 24154a6ec8aSchristos struct block *pred; 24254a6ec8aSchristos struct edge *next; /* link list of incoming edges for a node */ 24354a6ec8aSchristos }; 24454a6ec8aSchristos 24554a6ec8aSchristos struct block { 24654a6ec8aSchristos int id; 24754a6ec8aSchristos struct slist *stmts; /* side effect stmts */ 24854a6ec8aSchristos struct stmt s; /* branch stmt */ 24954a6ec8aSchristos int mark; 250774c01f3Schristos u_int longjt; /* jt branch requires long jump */ 251774c01f3Schristos u_int longjf; /* jf branch requires long jump */ 25254a6ec8aSchristos int level; 25354a6ec8aSchristos int offset; 25454a6ec8aSchristos int sense; 25554a6ec8aSchristos struct edge et; 25654a6ec8aSchristos struct edge ef; 25754a6ec8aSchristos struct block *head; 25854a6ec8aSchristos struct block *link; /* link field used by optimizer */ 25954a6ec8aSchristos uset dom; 26054a6ec8aSchristos uset closure; 26154a6ec8aSchristos struct edge *in_edges; 26254a6ec8aSchristos atomset def, kill; 26354a6ec8aSchristos atomset in_use; 26454a6ec8aSchristos atomset out_use; 26554a6ec8aSchristos int oval; 26654a6ec8aSchristos int val[N_ATOMS]; 26754a6ec8aSchristos }; 26854a6ec8aSchristos 269e1375535Schristos /* 270e1375535Schristos * A value of 0 for val[i] means the value is unknown. 271e1375535Schristos */ 272e1375535Schristos #define VAL_UNKNOWN 0 273e1375535Schristos 27454a6ec8aSchristos struct arth { 27554a6ec8aSchristos struct block *b; /* protocol checks */ 27654a6ec8aSchristos struct slist *s; /* stmt list */ 27754a6ec8aSchristos int regno; /* virtual register number of result */ 27854a6ec8aSchristos }; 27954a6ec8aSchristos 28054a6ec8aSchristos struct qual { 28154a6ec8aSchristos unsigned char addr; 28254a6ec8aSchristos unsigned char proto; 28354a6ec8aSchristos unsigned char dir; 28454a6ec8aSchristos unsigned char pad; 28554a6ec8aSchristos }; 28654a6ec8aSchristos 28703e25b48Schristos struct _compiler_state; 28803e25b48Schristos 28903e25b48Schristos typedef struct _compiler_state compiler_state_t; 29003e25b48Schristos 29103e25b48Schristos struct arth *gen_loadi(compiler_state_t *, int); 29203e25b48Schristos struct arth *gen_load(compiler_state_t *, int, struct arth *, int); 29303e25b48Schristos struct arth *gen_loadlen(compiler_state_t *); 29403e25b48Schristos struct arth *gen_neg(compiler_state_t *, struct arth *); 29503e25b48Schristos struct arth *gen_arth(compiler_state_t *, int, struct arth *, struct arth *); 29654a6ec8aSchristos 29754a6ec8aSchristos void gen_and(struct block *, struct block *); 29854a6ec8aSchristos void gen_or(struct block *, struct block *); 29954a6ec8aSchristos void gen_not(struct block *); 30054a6ec8aSchristos 30103e25b48Schristos struct block *gen_scode(compiler_state_t *, const char *, struct qual); 302*d5dfe396Schristos struct block *gen_ecode(compiler_state_t *, const char *, struct qual); 303*d5dfe396Schristos struct block *gen_acode(compiler_state_t *, const char *, struct qual); 30403e25b48Schristos struct block *gen_mcode(compiler_state_t *, const char *, const char *, 30503e25b48Schristos unsigned int, struct qual); 30654a6ec8aSchristos #ifdef INET6 30703e25b48Schristos struct block *gen_mcode6(compiler_state_t *, const char *, const char *, 30803e25b48Schristos unsigned int, struct qual); 30954a6ec8aSchristos #endif 31003e25b48Schristos struct block *gen_ncode(compiler_state_t *, const char *, bpf_u_int32, 31103e25b48Schristos struct qual); 31203e25b48Schristos struct block *gen_proto_abbrev(compiler_state_t *, int); 31303e25b48Schristos struct block *gen_relation(compiler_state_t *, int, struct arth *, 31403e25b48Schristos struct arth *, int); 31503e25b48Schristos struct block *gen_less(compiler_state_t *, int); 31603e25b48Schristos struct block *gen_greater(compiler_state_t *, int); 31703e25b48Schristos struct block *gen_byteop(compiler_state_t *, int, int, int); 31803e25b48Schristos struct block *gen_broadcast(compiler_state_t *, int); 31903e25b48Schristos struct block *gen_multicast(compiler_state_t *, int); 32003e25b48Schristos struct block *gen_inbound(compiler_state_t *, int); 32154a6ec8aSchristos 32203e25b48Schristos struct block *gen_llc(compiler_state_t *); 32303e25b48Schristos struct block *gen_llc_i(compiler_state_t *); 32403e25b48Schristos struct block *gen_llc_s(compiler_state_t *); 32503e25b48Schristos struct block *gen_llc_u(compiler_state_t *); 32603e25b48Schristos struct block *gen_llc_s_subtype(compiler_state_t *, bpf_u_int32); 32703e25b48Schristos struct block *gen_llc_u_subtype(compiler_state_t *, bpf_u_int32); 3287dd943c4Schristos 329*d5dfe396Schristos struct block *gen_vlan(compiler_state_t *, bpf_u_int32, int); 330*d5dfe396Schristos struct block *gen_mpls(compiler_state_t *, bpf_u_int32, int); 33154a6ec8aSchristos 33203e25b48Schristos struct block *gen_pppoed(compiler_state_t *); 333*d5dfe396Schristos struct block *gen_pppoes(compiler_state_t *, bpf_u_int32, int); 33454a6ec8aSchristos 335*d5dfe396Schristos struct block *gen_geneve(compiler_state_t *, bpf_u_int32, int); 3366c7e2519Schristos 33703e25b48Schristos struct block *gen_atmfield_code(compiler_state_t *, int, bpf_int32, 33803e25b48Schristos bpf_u_int32, int); 33903e25b48Schristos struct block *gen_atmtype_abbrev(compiler_state_t *, int type); 34003e25b48Schristos struct block *gen_atmmulti_abbrev(compiler_state_t *, int type); 34154a6ec8aSchristos 34203e25b48Schristos struct block *gen_mtp2type_abbrev(compiler_state_t *, int type); 34303e25b48Schristos struct block *gen_mtp3field_code(compiler_state_t *, int, bpf_u_int32, 34403e25b48Schristos bpf_u_int32, int); 34554a6ec8aSchristos 34603e25b48Schristos struct block *gen_pf_ifname(compiler_state_t *, const char *); 34703e25b48Schristos struct block *gen_pf_rnr(compiler_state_t *, int); 34803e25b48Schristos struct block *gen_pf_srnr(compiler_state_t *, int); 34903e25b48Schristos struct block *gen_pf_ruleset(compiler_state_t *, char *); 35003e25b48Schristos struct block *gen_pf_reason(compiler_state_t *, int); 35103e25b48Schristos struct block *gen_pf_action(compiler_state_t *, int); 35254a6ec8aSchristos 35303e25b48Schristos struct block *gen_p80211_type(compiler_state_t *, int, int); 35403e25b48Schristos struct block *gen_p80211_fcdir(compiler_state_t *, int); 35554a6ec8aSchristos 35603e25b48Schristos /* 35703e25b48Schristos * Representation of a program as a tree of blocks, plus current mark. 35803e25b48Schristos * A block is marked if only if its mark equals the current mark. 35903e25b48Schristos * Rather than traverse the code array, marking each item, 'cur_mark' 36003e25b48Schristos * is incremented. This automatically makes each element unmarked. 36103e25b48Schristos */ 36203e25b48Schristos #define isMarked(icp, p) ((p)->mark == (icp)->cur_mark) 36303e25b48Schristos #define unMarkAll(icp) (icp)->cur_mark += 1 36403e25b48Schristos #define Mark(icp, p) ((p)->mark = (icp)->cur_mark) 36503e25b48Schristos 36603e25b48Schristos struct icode { 36703e25b48Schristos struct block *root; 36803e25b48Schristos int cur_mark; 36903e25b48Schristos }; 37003e25b48Schristos 371*d5dfe396Schristos int bpf_optimize(struct icode *, char *); 372*d5dfe396Schristos void bpf_set_error(compiler_state_t *, const char *, ...) 373e1375535Schristos PCAP_PRINTFLIKE(2, 3); 37454a6ec8aSchristos 375*d5dfe396Schristos int finish_parse(compiler_state_t *, struct block *); 37603e25b48Schristos char *sdup(compiler_state_t *, const char *); 37754a6ec8aSchristos 378*d5dfe396Schristos struct bpf_insn *icode_to_fcode(struct icode *, struct block *, u_int *, 379*d5dfe396Schristos char *); 38054a6ec8aSchristos void sappend(struct slist *, struct slist *); 38154a6ec8aSchristos 38203e25b48Schristos /* 38303e25b48Schristos * Older versions of Bison don't put this declaration in 38403e25b48Schristos * grammar.h. 38503e25b48Schristos */ 38603e25b48Schristos int pcap_parse(void *, compiler_state_t *); 38703e25b48Schristos 38854a6ec8aSchristos /* XXX */ 38954a6ec8aSchristos #define JT(b) ((b)->et.succ) 39054a6ec8aSchristos #define JF(b) ((b)->ef.succ) 391