/*1* Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 19962* The Regents of the University of California. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that: (1) source code distributions6* retain the above copyright notice and this paragraph in its entirety, (2)7* distributions including binary code include the above copyright notice and8* this paragraph in its entirety in the documentation or other materials9* provided with the distribution, and (3) all advertising materials mentioning10* features or use of this software display the following acknowledgement:11* ``This product includes software developed by the University of California,12* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of13* the University nor the names of its contributors may be used to endorse14* or promote products derived from this software without specific prior15* written permission.16* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED17* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF18* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.19*/2021#ifndef gencode_h22#define gencode_h2324#include "pcap/funcattrs.h"25/*26* pcap/bpf.h (a public header) needs u_char, u_short and u_int, which can be27* made available via either pcap-types.h (a private header) or pcap/pcap.h28* (a public header), none of which pcap/bpf.h includes. Include the private29* header to keep things simple, this way this private header should compile30* even if included early from another file.31*/32#include "pcap-types.h"33#include "pcap/bpf.h" /* bpf_u_int32 and BPF_MEMWORDS */3435/*36* ATM support:37*38* Copyright (c) 1997 Yen Yen Lim and North Dakota State University39* All rights reserved.40*41* Redistribution and use in source and binary forms, with or without42* modification, are permitted provided that the following conditions43* are met:44* 1. Redistributions of source code must retain the above copyright45* notice, this list of conditions and the following disclaimer.46* 2. Redistributions in binary form must reproduce the above copyright47* notice, this list of conditions and the following disclaimer in the48* documentation and/or other materials provided with the distribution.49* 3. All advertising materials mentioning features or use of this software50* must display the following acknowledgement:51* This product includes software developed by Yen Yen Lim and52* North Dakota State University53* 4. The name of the author may not be used to endorse or promote products54* derived from this software without specific prior written permission.55*56* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR57* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED58* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE59* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,60* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES61* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR62* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)63* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,64* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN65* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE66* POSSIBILITY OF SUCH DAMAGE.67*/6869/* Address qualifiers. */7071#define Q_HOST 172#define Q_NET 273#define Q_PORT 374#define Q_GATEWAY 475#define Q_PROTO 576#define Q_PROTOCHAIN 677#define Q_PORTRANGE 77879/* Protocol qualifiers. */8081#define Q_LINK 182#define Q_IP 283#define Q_ARP 384#define Q_RARP 485#define Q_SCTP 586#define Q_TCP 687#define Q_UDP 788#define Q_ICMP 889#define Q_IGMP 990#define Q_IGRP 10919293#define Q_ATALK 1194#define Q_DECNET 1295#define Q_LAT 1396#define Q_SCA 1497#define Q_MOPRC 1598#define Q_MOPDL 1699100101#define Q_IPV6 17102#define Q_ICMPV6 18103#define Q_AH 19104#define Q_ESP 20105106#define Q_PIM 21107#define Q_VRRP 22108109#define Q_AARP 23110111#define Q_ISO 24112#define Q_ESIS 25113#define Q_ISIS 26114#define Q_CLNP 27115116#define Q_STP 28117118#define Q_IPX 29119120#define Q_NETBEUI 30121122/* IS-IS Levels */123#define Q_ISIS_L1 31124#define Q_ISIS_L2 32125/* PDU types */126#define Q_ISIS_IIH 33127#define Q_ISIS_SNP 34128#define Q_ISIS_CSNP 35129#define Q_ISIS_PSNP 36130#define Q_ISIS_LSP 37131132#define Q_RADIO 38133134#define Q_CARP 39135136/* Directional qualifiers. */137138#define Q_SRC 1139#define Q_DST 2140#define Q_OR 3141#define Q_AND 4142#define Q_ADDR1 5143#define Q_ADDR2 6144#define Q_ADDR3 7145#define Q_ADDR4 8146#define Q_RA 9147#define Q_TA 10148149#define Q_DEFAULT 0150#define Q_UNDEF 255151152/* ATM types */153#define A_METAC 22 /* Meta signalling Circuit */154#define A_BCC 23 /* Broadcast Circuit */155#define A_OAMF4SC 24 /* Segment OAM F4 Circuit */156#define A_OAMF4EC 25 /* End-to-End OAM F4 Circuit */157#define A_SC 26 /* Signalling Circuit*/158#define A_ILMIC 27 /* ILMI Circuit */159#define A_OAM 28 /* OAM cells : F4 only */160#define A_OAMF4 29 /* OAM F4 cells: Segment + End-to-end */161#define A_LANE 30 /* LANE traffic */162#define A_LLC 31 /* LLC-encapsulated traffic */163164/* Based on Q.2931 signalling protocol */165#define A_SETUP 41 /* Setup message */166#define A_CALLPROCEED 42 /* Call proceeding message */167#define A_CONNECT 43 /* Connect message */168#define A_CONNECTACK 44 /* Connect Ack message */169#define A_RELEASE 45 /* Release message */170#define A_RELEASE_DONE 46 /* Release message */171172/* ATM field types */173#define A_VPI 51174#define A_VCI 52175#define A_PROTOTYPE 53176#define A_MSGTYPE 54177#define A_CALLREFTYPE 55178179#define A_CONNECTMSG 70 /* returns Q.2931 signalling messages for180establishing and destroying switched181virtual connection */182#define A_METACONNECT 71 /* returns Q.2931 signalling messages for183establishing and destroying predefined184virtual circuits, such as broadcast185circuit, oamf4 segment circuit, oamf4186end-to-end circuits, ILMI circuits or187connection signalling circuit. */188189/* MTP2 types */190#define M_FISU 22 /* FISU */191#define M_LSSU 23 /* LSSU */192#define M_MSU 24 /* MSU */193194/* MTP2 HSL types */195#define MH_FISU 25 /* FISU for HSL */196#define MH_LSSU 26 /* LSSU */197#define MH_MSU 27 /* MSU */198199/* MTP3 field types */200#define M_SIO 1201#define M_OPC 2202#define M_DPC 3203#define M_SLS 4204205/* MTP3 field types in case of MTP2 HSL */206#define MH_SIO 5207#define MH_OPC 6208#define MH_DPC 7209#define MH_SLS 8210211212struct slist;213214/*215* A single statement, corresponding to an instruction in a block.216*/217struct stmt {218int code; /* opcode */219struct slist *jt; /* only for relative jump in block */220struct slist *jf; /* only for relative jump in block */221bpf_u_int32 k; /* k field */222};223224struct slist {225struct stmt s;226struct slist *next;227};228229/*230* A bit vector to represent definition sets. We assume TOT_REGISTERS231* is smaller than 8*sizeof(atomset).232*/233typedef bpf_u_int32 atomset;234#define ATOMMASK(n) (1 << (n))235#define ATOMELEM(d, n) (d & ATOMMASK(n))236237/*238* An unbounded set.239*/240typedef bpf_u_int32 *uset;241242/*243* Total number of atomic entities, including accumulator (A) and index (X).244* We treat all these guys similarly during flow analysis.245*/246#define N_ATOMS (BPF_MEMWORDS+2)247248/*249* Control flow graph of a program.250* This corresponds to an edge in the CFG.251* It's a directed graph, so an edge has a predecessor and a successor.252*/253struct edge {254u_int id;255int code; /* opcode for branch corresponding to this edge */256uset edom;257struct block *succ; /* successor vertex */258struct block *pred; /* predecessor vertex */259struct edge *next; /* link list of incoming edges for a node */260};261262/*263* A block is a vertex in the CFG.264* It has a list of statements, with the final statement being a265* branch to successor blocks.266*/267struct block {268u_int id;269struct slist *stmts; /* side effect stmts */270struct stmt s; /* branch stmt */271int mark;272u_int longjt; /* jt branch requires long jump */273u_int longjf; /* jf branch requires long jump */274int level;275int offset;276int sense;277struct edge et; /* edge corresponding to the jt branch */278struct edge ef; /* edge corresponding to the jf branch */279struct block *head;280struct block *link; /* link field used by optimizer */281uset dom;282uset closure;283struct edge *in_edges; /* first edge in the set (linked list) of edges with this as a successor */284atomset def, kill;285atomset in_use;286atomset out_use;287int oval; /* value ID for value tested in branch stmt */288bpf_u_int32 val[N_ATOMS];289};290291/*292* A value of 0 for val[i] means the value is unknown.293*/294#define VAL_UNKNOWN 0295296struct arth {297struct block *b; /* protocol checks */298struct slist *s; /* stmt list */299int regno; /* virtual register number of result */300};301302struct qual {303unsigned char addr;304unsigned char proto;305unsigned char dir;306unsigned char pad;307};308309struct _compiler_state;310311typedef struct _compiler_state compiler_state_t;312313struct arth *gen_loadi(compiler_state_t *, bpf_u_int32);314struct arth *gen_load(compiler_state_t *, int, struct arth *, bpf_u_int32);315struct arth *gen_loadlen(compiler_state_t *);316struct arth *gen_neg(compiler_state_t *, struct arth *);317struct arth *gen_arth(compiler_state_t *, int, struct arth *, struct arth *);318319void gen_and(struct block *, struct block *);320void gen_or(struct block *, struct block *);321void gen_not(struct block *);322323struct block *gen_scode(compiler_state_t *, const char *, struct qual);324struct block *gen_ecode(compiler_state_t *, const char *, struct qual);325struct block *gen_acode(compiler_state_t *, const char *, struct qual);326struct block *gen_mcode(compiler_state_t *, const char *, const char *,327bpf_u_int32, struct qual);328#ifdef INET6329struct block *gen_mcode6(compiler_state_t *, const char *, bpf_u_int32,330struct qual);331#endif332struct block *gen_ncode(compiler_state_t *, const char *, bpf_u_int32,333struct qual);334struct block *gen_proto_abbrev(compiler_state_t *, int);335struct block *gen_relation(compiler_state_t *, int, struct arth *,336struct arth *, int);337struct block *gen_less(compiler_state_t *, int);338struct block *gen_greater(compiler_state_t *, int);339struct block *gen_byteop(compiler_state_t *, int, int, bpf_u_int32);340struct block *gen_broadcast(compiler_state_t *, int);341struct block *gen_multicast(compiler_state_t *, int);342struct block *gen_ifindex(compiler_state_t *, int);343struct block *gen_inbound(compiler_state_t *, int);344345struct block *gen_llc(compiler_state_t *);346struct block *gen_llc_i(compiler_state_t *);347struct block *gen_llc_s(compiler_state_t *);348struct block *gen_llc_u(compiler_state_t *);349struct block *gen_llc_s_subtype(compiler_state_t *, bpf_u_int32);350struct block *gen_llc_u_subtype(compiler_state_t *, bpf_u_int32);351352struct block *gen_vlan(compiler_state_t *, bpf_u_int32, int);353struct block *gen_mpls(compiler_state_t *, bpf_u_int32, int);354355struct block *gen_pppoed(compiler_state_t *);356struct block *gen_pppoes(compiler_state_t *, bpf_u_int32, int);357358struct block *gen_geneve(compiler_state_t *, bpf_u_int32, int);359360struct block *gen_atmfield_code(compiler_state_t *, int, bpf_u_int32,361int, int);362struct block *gen_atmtype_abbrev(compiler_state_t *, int);363struct block *gen_atmmulti_abbrev(compiler_state_t *, int);364365struct block *gen_mtp2type_abbrev(compiler_state_t *, int);366struct block *gen_mtp3field_code(compiler_state_t *, int, bpf_u_int32,367int, int);368369struct block *gen_pf_ifname(compiler_state_t *, const char *);370struct block *gen_pf_rnr(compiler_state_t *, int);371struct block *gen_pf_srnr(compiler_state_t *, int);372struct block *gen_pf_ruleset(compiler_state_t *, char *);373struct block *gen_pf_reason(compiler_state_t *, int);374struct block *gen_pf_action(compiler_state_t *, int);375376struct block *gen_p80211_type(compiler_state_t *, bpf_u_int32, bpf_u_int32);377struct block *gen_p80211_fcdir(compiler_state_t *, bpf_u_int32);378379/*380* Representation of a program as a tree of blocks, plus current mark.381* A block is marked if only if its mark equals the current mark.382* Rather than traverse the code array, marking each item, 'cur_mark'383* is incremented. This automatically makes each element unmarked.384*/385#define isMarked(icp, p) ((p)->mark == (icp)->cur_mark)386#define unMarkAll(icp) (icp)->cur_mark += 1387#define Mark(icp, p) ((p)->mark = (icp)->cur_mark)388389struct icode {390struct block *root;391int cur_mark;392};393394int bpf_optimize(struct icode *, char *);395void bpf_set_error(compiler_state_t *, const char *, ...)396PCAP_PRINTFLIKE(2, 3);397398int finish_parse(compiler_state_t *, struct block *);399char *sdup(compiler_state_t *, const char *);400401struct bpf_insn *icode_to_fcode(struct icode *, struct block *, u_int *,402char *);403void sappend(struct slist *, struct slist *);404405/*406* Older versions of Bison don't put this declaration in407* grammar.h.408*/409int pcap_parse(void *, compiler_state_t *);410411/* XXX */412#define JT(b) ((b)->et.succ)413#define JF(b) ((b)->ef.succ)414415#endif /* gencode_h */416417418