1*e3899b6dSchristos /* $NetBSD: gencode.h,v 1.2 2014/11/19 19:33:30 christos Exp $ */ 2*e3899b6dSchristos 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 2454a6ec8aSchristos /* 2554a6ec8aSchristos * ATM support: 2654a6ec8aSchristos * 2754a6ec8aSchristos * Copyright (c) 1997 Yen Yen Lim and North Dakota State University 2854a6ec8aSchristos * All rights reserved. 2954a6ec8aSchristos * 3054a6ec8aSchristos * Redistribution and use in source and binary forms, with or without 3154a6ec8aSchristos * modification, are permitted provided that the following conditions 3254a6ec8aSchristos * are met: 3354a6ec8aSchristos * 1. Redistributions of source code must retain the above copyright 3454a6ec8aSchristos * notice, this list of conditions and the following disclaimer. 3554a6ec8aSchristos * 2. Redistributions in binary form must reproduce the above copyright 3654a6ec8aSchristos * notice, this list of conditions and the following disclaimer in the 3754a6ec8aSchristos * documentation and/or other materials provided with the distribution. 3854a6ec8aSchristos * 3. All advertising materials mentioning features or use of this software 3954a6ec8aSchristos * must display the following acknowledgement: 4054a6ec8aSchristos * This product includes software developed by Yen Yen Lim and 4154a6ec8aSchristos * North Dakota State University 4254a6ec8aSchristos * 4. The name of the author may not be used to endorse or promote products 4354a6ec8aSchristos * derived from this software without specific prior written permission. 4454a6ec8aSchristos * 4554a6ec8aSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 4654a6ec8aSchristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 4754a6ec8aSchristos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 4854a6ec8aSchristos * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 4954a6ec8aSchristos * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 5054a6ec8aSchristos * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 5154a6ec8aSchristos * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5254a6ec8aSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 5354a6ec8aSchristos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 5454a6ec8aSchristos * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 5554a6ec8aSchristos * POSSIBILITY OF SUCH DAMAGE. 5654a6ec8aSchristos */ 5754a6ec8aSchristos 5854a6ec8aSchristos #ifndef HAVE___ATTRIBUTE__ 5954a6ec8aSchristos #define __attribute__(x) 6054a6ec8aSchristos #endif /* HAVE___ATTRIBUTE__ */ 6154a6ec8aSchristos 6254a6ec8aSchristos /* Address qualifiers. */ 6354a6ec8aSchristos 6454a6ec8aSchristos #define Q_HOST 1 6554a6ec8aSchristos #define Q_NET 2 6654a6ec8aSchristos #define Q_PORT 3 6754a6ec8aSchristos #define Q_GATEWAY 4 6854a6ec8aSchristos #define Q_PROTO 5 6954a6ec8aSchristos #define Q_PROTOCHAIN 6 7054a6ec8aSchristos #define Q_PORTRANGE 7 7154a6ec8aSchristos 7254a6ec8aSchristos /* Protocol qualifiers. */ 7354a6ec8aSchristos 7454a6ec8aSchristos #define Q_LINK 1 7554a6ec8aSchristos #define Q_IP 2 7654a6ec8aSchristos #define Q_ARP 3 7754a6ec8aSchristos #define Q_RARP 4 7854a6ec8aSchristos #define Q_SCTP 5 7954a6ec8aSchristos #define Q_TCP 6 8054a6ec8aSchristos #define Q_UDP 7 8154a6ec8aSchristos #define Q_ICMP 8 8254a6ec8aSchristos #define Q_IGMP 9 8354a6ec8aSchristos #define Q_IGRP 10 8454a6ec8aSchristos 8554a6ec8aSchristos 8654a6ec8aSchristos #define Q_ATALK 11 8754a6ec8aSchristos #define Q_DECNET 12 8854a6ec8aSchristos #define Q_LAT 13 8954a6ec8aSchristos #define Q_SCA 14 9054a6ec8aSchristos #define Q_MOPRC 15 9154a6ec8aSchristos #define Q_MOPDL 16 9254a6ec8aSchristos 9354a6ec8aSchristos 9454a6ec8aSchristos #define Q_IPV6 17 9554a6ec8aSchristos #define Q_ICMPV6 18 9654a6ec8aSchristos #define Q_AH 19 9754a6ec8aSchristos #define Q_ESP 20 9854a6ec8aSchristos 9954a6ec8aSchristos #define Q_PIM 21 10054a6ec8aSchristos #define Q_VRRP 22 10154a6ec8aSchristos 10254a6ec8aSchristos #define Q_AARP 23 10354a6ec8aSchristos 10454a6ec8aSchristos #define Q_ISO 24 10554a6ec8aSchristos #define Q_ESIS 25 10654a6ec8aSchristos #define Q_ISIS 26 10754a6ec8aSchristos #define Q_CLNP 27 10854a6ec8aSchristos 10954a6ec8aSchristos #define Q_STP 28 11054a6ec8aSchristos 11154a6ec8aSchristos #define Q_IPX 29 11254a6ec8aSchristos 11354a6ec8aSchristos #define Q_NETBEUI 30 11454a6ec8aSchristos 11554a6ec8aSchristos /* IS-IS Levels */ 11654a6ec8aSchristos #define Q_ISIS_L1 31 11754a6ec8aSchristos #define Q_ISIS_L2 32 11854a6ec8aSchristos /* PDU types */ 11954a6ec8aSchristos #define Q_ISIS_IIH 33 12054a6ec8aSchristos #define Q_ISIS_LAN_IIH 34 12154a6ec8aSchristos #define Q_ISIS_PTP_IIH 35 12254a6ec8aSchristos #define Q_ISIS_SNP 36 12354a6ec8aSchristos #define Q_ISIS_CSNP 37 12454a6ec8aSchristos #define Q_ISIS_PSNP 38 12554a6ec8aSchristos #define Q_ISIS_LSP 39 12654a6ec8aSchristos 12754a6ec8aSchristos #define Q_RADIO 40 12854a6ec8aSchristos 129774c01f3Schristos #define Q_CARP 41 130774c01f3Schristos 13154a6ec8aSchristos /* Directional qualifiers. */ 13254a6ec8aSchristos 13354a6ec8aSchristos #define Q_SRC 1 13454a6ec8aSchristos #define Q_DST 2 13554a6ec8aSchristos #define Q_OR 3 13654a6ec8aSchristos #define Q_AND 4 13754a6ec8aSchristos #define Q_ADDR1 5 13854a6ec8aSchristos #define Q_ADDR2 6 13954a6ec8aSchristos #define Q_ADDR3 7 14054a6ec8aSchristos #define Q_ADDR4 8 141774c01f3Schristos #define Q_RA 9 142774c01f3Schristos #define Q_TA 10 14354a6ec8aSchristos 14454a6ec8aSchristos #define Q_DEFAULT 0 14554a6ec8aSchristos #define Q_UNDEF 255 14654a6ec8aSchristos 14754a6ec8aSchristos /* ATM types */ 14854a6ec8aSchristos #define A_METAC 22 /* Meta signalling Circuit */ 14954a6ec8aSchristos #define A_BCC 23 /* Broadcast Circuit */ 15054a6ec8aSchristos #define A_OAMF4SC 24 /* Segment OAM F4 Circuit */ 15154a6ec8aSchristos #define A_OAMF4EC 25 /* End-to-End OAM F4 Circuit */ 15254a6ec8aSchristos #define A_SC 26 /* Signalling Circuit*/ 15354a6ec8aSchristos #define A_ILMIC 27 /* ILMI Circuit */ 15454a6ec8aSchristos #define A_OAM 28 /* OAM cells : F4 only */ 15554a6ec8aSchristos #define A_OAMF4 29 /* OAM F4 cells: Segment + End-to-end */ 15654a6ec8aSchristos #define A_LANE 30 /* LANE traffic */ 15754a6ec8aSchristos #define A_LLC 31 /* LLC-encapsulated traffic */ 15854a6ec8aSchristos 15954a6ec8aSchristos /* Based on Q.2931 signalling protocol */ 16054a6ec8aSchristos #define A_SETUP 41 /* Setup message */ 16154a6ec8aSchristos #define A_CALLPROCEED 42 /* Call proceeding message */ 16254a6ec8aSchristos #define A_CONNECT 43 /* Connect message */ 16354a6ec8aSchristos #define A_CONNECTACK 44 /* Connect Ack message */ 16454a6ec8aSchristos #define A_RELEASE 45 /* Release message */ 16554a6ec8aSchristos #define A_RELEASE_DONE 46 /* Release message */ 16654a6ec8aSchristos 16754a6ec8aSchristos /* ATM field types */ 16854a6ec8aSchristos #define A_VPI 51 16954a6ec8aSchristos #define A_VCI 52 17054a6ec8aSchristos #define A_PROTOTYPE 53 17154a6ec8aSchristos #define A_MSGTYPE 54 17254a6ec8aSchristos #define A_CALLREFTYPE 55 17354a6ec8aSchristos 17454a6ec8aSchristos #define A_CONNECTMSG 70 /* returns Q.2931 signalling messages for 17554a6ec8aSchristos establishing and destroying switched 17654a6ec8aSchristos virtual connection */ 17754a6ec8aSchristos #define A_METACONNECT 71 /* returns Q.2931 signalling messages for 17854a6ec8aSchristos establishing and destroying predefined 17954a6ec8aSchristos virtual circuits, such as broadcast 18054a6ec8aSchristos circuit, oamf4 segment circuit, oamf4 18154a6ec8aSchristos end-to-end circuits, ILMI circuits or 18254a6ec8aSchristos connection signalling circuit. */ 18354a6ec8aSchristos 18454a6ec8aSchristos /* MTP2 types */ 18554a6ec8aSchristos #define M_FISU 22 /* FISU */ 18654a6ec8aSchristos #define M_LSSU 23 /* LSSU */ 18754a6ec8aSchristos #define M_MSU 24 /* MSU */ 18854a6ec8aSchristos 189c15d5effSchristos /* MTP2 HSL types */ 190c15d5effSchristos #define MH_FISU 25 /* FISU for HSL */ 191c15d5effSchristos #define MH_LSSU 26 /* LSSU */ 192c15d5effSchristos #define MH_MSU 27 /* MSU */ 193c15d5effSchristos 19454a6ec8aSchristos /* MTP3 field types */ 19554a6ec8aSchristos #define M_SIO 1 19654a6ec8aSchristos #define M_OPC 2 19754a6ec8aSchristos #define M_DPC 3 19854a6ec8aSchristos #define M_SLS 4 19954a6ec8aSchristos 200c15d5effSchristos /* MTP3 field types in case of MTP2 HSL */ 201c15d5effSchristos #define MH_SIO 5 202c15d5effSchristos #define MH_OPC 6 203c15d5effSchristos #define MH_DPC 7 204c15d5effSchristos #define MH_SLS 8 205c15d5effSchristos 20654a6ec8aSchristos 20754a6ec8aSchristos struct slist; 20854a6ec8aSchristos 20954a6ec8aSchristos struct stmt { 21054a6ec8aSchristos int code; 21154a6ec8aSchristos struct slist *jt; /*only for relative jump in block*/ 21254a6ec8aSchristos struct slist *jf; /*only for relative jump in block*/ 21354a6ec8aSchristos bpf_int32 k; 21454a6ec8aSchristos }; 21554a6ec8aSchristos 21654a6ec8aSchristos struct slist { 21754a6ec8aSchristos struct stmt s; 21854a6ec8aSchristos struct slist *next; 21954a6ec8aSchristos }; 22054a6ec8aSchristos 22154a6ec8aSchristos /* 22254a6ec8aSchristos * A bit vector to represent definition sets. We assume TOT_REGISTERS 22354a6ec8aSchristos * is smaller than 8*sizeof(atomset). 22454a6ec8aSchristos */ 22554a6ec8aSchristos typedef bpf_u_int32 atomset; 22654a6ec8aSchristos #define ATOMMASK(n) (1 << (n)) 22754a6ec8aSchristos #define ATOMELEM(d, n) (d & ATOMMASK(n)) 22854a6ec8aSchristos 22954a6ec8aSchristos /* 23054a6ec8aSchristos * An unbounded set. 23154a6ec8aSchristos */ 23254a6ec8aSchristos typedef bpf_u_int32 *uset; 23354a6ec8aSchristos 23454a6ec8aSchristos /* 23554a6ec8aSchristos * Total number of atomic entities, including accumulator (A) and index (X). 23654a6ec8aSchristos * We treat all these guys similarly during flow analysis. 23754a6ec8aSchristos */ 23854a6ec8aSchristos #define N_ATOMS (BPF_MEMWORDS+2) 23954a6ec8aSchristos 24054a6ec8aSchristos struct edge { 24154a6ec8aSchristos int id; 24254a6ec8aSchristos int code; 24354a6ec8aSchristos uset edom; 24454a6ec8aSchristos struct block *succ; 24554a6ec8aSchristos struct block *pred; 24654a6ec8aSchristos struct edge *next; /* link list of incoming edges for a node */ 24754a6ec8aSchristos }; 24854a6ec8aSchristos 24954a6ec8aSchristos struct block { 25054a6ec8aSchristos int id; 25154a6ec8aSchristos struct slist *stmts; /* side effect stmts */ 25254a6ec8aSchristos struct stmt s; /* branch stmt */ 25354a6ec8aSchristos int mark; 254774c01f3Schristos u_int longjt; /* jt branch requires long jump */ 255774c01f3Schristos u_int longjf; /* jf branch requires long jump */ 25654a6ec8aSchristos int level; 25754a6ec8aSchristos int offset; 25854a6ec8aSchristos int sense; 25954a6ec8aSchristos struct edge et; 26054a6ec8aSchristos struct edge ef; 26154a6ec8aSchristos struct block *head; 26254a6ec8aSchristos struct block *link; /* link field used by optimizer */ 26354a6ec8aSchristos uset dom; 26454a6ec8aSchristos uset closure; 26554a6ec8aSchristos struct edge *in_edges; 26654a6ec8aSchristos atomset def, kill; 26754a6ec8aSchristos atomset in_use; 26854a6ec8aSchristos atomset out_use; 26954a6ec8aSchristos int oval; 27054a6ec8aSchristos int val[N_ATOMS]; 27154a6ec8aSchristos }; 27254a6ec8aSchristos 27354a6ec8aSchristos struct arth { 27454a6ec8aSchristos struct block *b; /* protocol checks */ 27554a6ec8aSchristos struct slist *s; /* stmt list */ 27654a6ec8aSchristos int regno; /* virtual register number of result */ 27754a6ec8aSchristos }; 27854a6ec8aSchristos 27954a6ec8aSchristos struct qual { 28054a6ec8aSchristos unsigned char addr; 28154a6ec8aSchristos unsigned char proto; 28254a6ec8aSchristos unsigned char dir; 28354a6ec8aSchristos unsigned char pad; 28454a6ec8aSchristos }; 28554a6ec8aSchristos 28654a6ec8aSchristos struct arth *gen_loadi(int); 28754a6ec8aSchristos struct arth *gen_load(int, struct arth *, int); 28854a6ec8aSchristos struct arth *gen_loadlen(void); 28954a6ec8aSchristos struct arth *gen_neg(struct arth *); 29054a6ec8aSchristos struct arth *gen_arth(int, struct arth *, struct arth *); 29154a6ec8aSchristos 29254a6ec8aSchristos void gen_and(struct block *, struct block *); 29354a6ec8aSchristos void gen_or(struct block *, struct block *); 29454a6ec8aSchristos void gen_not(struct block *); 29554a6ec8aSchristos 29654a6ec8aSchristos struct block *gen_scode(const char *, struct qual); 29754a6ec8aSchristos struct block *gen_ecode(const u_char *, struct qual); 29854a6ec8aSchristos struct block *gen_acode(const u_char *, struct qual); 29954a6ec8aSchristos struct block *gen_mcode(const char *, const char *, int, struct qual); 30054a6ec8aSchristos #ifdef INET6 30154a6ec8aSchristos struct block *gen_mcode6(const char *, const char *, int, struct qual); 30254a6ec8aSchristos #endif 30354a6ec8aSchristos struct block *gen_ncode(const char *, bpf_u_int32, struct qual); 30454a6ec8aSchristos struct block *gen_proto_abbrev(int); 30554a6ec8aSchristos struct block *gen_relation(int, struct arth *, struct arth *, int); 30654a6ec8aSchristos struct block *gen_less(int); 30754a6ec8aSchristos struct block *gen_greater(int); 30854a6ec8aSchristos struct block *gen_byteop(int, int, int); 30954a6ec8aSchristos struct block *gen_broadcast(int); 31054a6ec8aSchristos struct block *gen_multicast(int); 31154a6ec8aSchristos struct block *gen_inbound(int); 31254a6ec8aSchristos 3137dd943c4Schristos struct block *gen_llc(void); 3147dd943c4Schristos struct block *gen_llc_i(void); 3157dd943c4Schristos struct block *gen_llc_s(void); 3167dd943c4Schristos struct block *gen_llc_u(void); 3177dd943c4Schristos struct block *gen_llc_s_subtype(bpf_u_int32); 3187dd943c4Schristos struct block *gen_llc_u_subtype(bpf_u_int32); 3197dd943c4Schristos 32054a6ec8aSchristos struct block *gen_vlan(int); 32154a6ec8aSchristos struct block *gen_mpls(int); 32254a6ec8aSchristos 32354a6ec8aSchristos struct block *gen_pppoed(void); 324c15d5effSchristos struct block *gen_pppoes(int); 32554a6ec8aSchristos 32654a6ec8aSchristos struct block *gen_atmfield_code(int atmfield, bpf_int32 jvalue, bpf_u_int32 jtype, int reverse); 32754a6ec8aSchristos struct block *gen_atmtype_abbrev(int type); 32854a6ec8aSchristos struct block *gen_atmmulti_abbrev(int type); 32954a6ec8aSchristos 33054a6ec8aSchristos struct block *gen_mtp2type_abbrev(int type); 33154a6ec8aSchristos struct block *gen_mtp3field_code(int mtp3field, bpf_u_int32 jvalue, bpf_u_int32 jtype, int reverse); 33254a6ec8aSchristos 33354a6ec8aSchristos struct block *gen_pf_ifname(const char *); 33454a6ec8aSchristos struct block *gen_pf_rnr(int); 33554a6ec8aSchristos struct block *gen_pf_srnr(int); 33654a6ec8aSchristos struct block *gen_pf_ruleset(char *); 33754a6ec8aSchristos struct block *gen_pf_reason(int); 33854a6ec8aSchristos struct block *gen_pf_action(int); 33954a6ec8aSchristos struct block *gen_pf_dir(int); 34054a6ec8aSchristos 34154a6ec8aSchristos struct block *gen_p80211_type(int, int); 34254a6ec8aSchristos struct block *gen_p80211_fcdir(int); 34354a6ec8aSchristos 34454a6ec8aSchristos void bpf_optimize(struct block **); 34554a6ec8aSchristos void bpf_error(const char *, ...) 346c15d5effSchristos __attribute__((noreturn)) 347c15d5effSchristos #ifdef __ATTRIBUTE___FORMAT_OK 348c15d5effSchristos __attribute__((format (printf, 1, 2))) 349c15d5effSchristos #endif /* __ATTRIBUTE___FORMAT_OK */ 350c15d5effSchristos ; 35154a6ec8aSchristos 35254a6ec8aSchristos void finish_parse(struct block *); 35354a6ec8aSchristos char *sdup(const char *); 35454a6ec8aSchristos 355774c01f3Schristos struct bpf_insn *icode_to_fcode(struct block *, u_int *); 35654a6ec8aSchristos int pcap_parse(void); 35754a6ec8aSchristos void lex_init(const char *); 35854a6ec8aSchristos void lex_cleanup(void); 35954a6ec8aSchristos void sappend(struct slist *, struct slist *); 36054a6ec8aSchristos 36154a6ec8aSchristos /* XXX */ 36254a6ec8aSchristos #define JT(b) ((b)->et.succ) 36354a6ec8aSchristos #define JF(b) ((b)->ef.succ) 36454a6ec8aSchristos 36554a6ec8aSchristos extern int no_optimize; 366