1/* $OpenBSD: gencode.h,v 1.22 2024/05/21 11:13:08 jsg 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/* Address qualifiers. */ 25 26#define Q_HOST 1 27#define Q_NET 2 28#define Q_PORT 3 29#define Q_GATEWAY 4 30#define Q_PROTO 5 31#define Q_PROTOCHAIN 6 32 33/* Protocol qualifiers. */ 34 35#define Q_LINK 1 36#define Q_IP 2 37#define Q_ARP 3 38#define Q_RARP 4 39#define Q_TCP 5 40#define Q_UDP 6 41#define Q_ICMP 7 42#define Q_IGMP 8 43#define Q_IGRP 9 44 45 46#define Q_ATALK 10 47#define Q_DECNET 11 48#define Q_LAT 12 49#define Q_SCA 13 50#define Q_MOPRC 14 51#define Q_MOPDL 15 52 53 54#define Q_IPV6 16 55#define Q_ICMPV6 17 56#define Q_AH 18 57#define Q_ESP 19 58 59#define Q_PIM 20 60#define Q_STP 21 61 62/* Directional qualifiers. */ 63 64#define Q_SRC 1 65#define Q_DST 2 66#define Q_OR 3 67#define Q_AND 4 68#define Q_ADDR1 5 69#define Q_ADDR2 6 70#define Q_ADDR3 7 71#define Q_ADDR4 8 72 73#define Q_DEFAULT 0 74#define Q_UNDEF 255 75 76struct slist; 77 78struct stmt { 79 int code; 80 struct slist *jt; /*only for relative jump in block*/ 81 struct slist *jf; /*only for relative jump in block*/ 82 bpf_int32 k; 83}; 84 85struct slist { 86 struct stmt s; 87 struct slist *next; 88}; 89 90/* 91 * A bit vector to represent definition sets. We assume TOT_REGISTERS 92 * is smaller than 8*sizeof(atomset). 93 */ 94typedef bpf_u_int32 atomset; 95#define ATOMMASK(n) (1 << (n)) 96#define ATOMELEM(d, n) (d & ATOMMASK(n)) 97 98/* 99 * An unbounded set. 100 */ 101typedef bpf_u_int32 *uset; 102 103/* 104 * Total number of atomic entities, including accumulator (A) and index (X). 105 * We treat all these guys similarly during flow analysis. 106 */ 107#define N_ATOMS (BPF_MEMWORDS+2) 108 109struct edge { 110 int id; 111 int code; 112 uset edom; 113 struct block *succ; 114 struct block *pred; 115 struct edge *next; /* link list of incoming edges for a node */ 116}; 117 118struct block { 119 int id; 120 struct slist *stmts; /* side effect stmts */ 121 struct stmt s; /* branch stmt */ 122 int mark; 123 int longjt; /* jt branch requires long jump */ 124 int longjf; /* jf branch requires long jump */ 125 int level; 126 int offset; 127 int sense; 128 struct edge et; 129 struct edge ef; 130 struct block *head; 131 struct block *link; /* link field used by optimizer */ 132 uset dom; 133 uset closure; 134 struct edge *in_edges; 135 atomset def, kill; 136 atomset in_use; 137 atomset out_use; 138 int oval; 139 int val[N_ATOMS]; 140}; 141 142struct arth { 143 struct block *b; /* protocol checks */ 144 struct slist *s; /* stmt list */ 145 int regno; /* virtual register number of result */ 146}; 147 148struct qual { 149 unsigned char addr; 150 unsigned char proto; 151 unsigned char dir; 152 unsigned char pad; 153}; 154 155struct arth *gen_loadi(int); 156struct arth *gen_load(int, struct arth *, int); 157struct arth *gen_loadlen(void); 158struct arth *gen_loadrnd(void); 159struct arth *gen_neg(struct arth *); 160struct arth *gen_arth(int, struct arth *, struct arth *); 161 162void gen_and(struct block *, struct block *); 163void gen_or(struct block *, struct block *); 164void gen_not(struct block *); 165 166struct block *gen_scode(const char *, struct qual); 167struct block *gen_ecode(const u_char *, struct qual); 168struct block *gen_mcode(const char *, const char *, int, struct qual); 169#ifdef INET6 170struct block *gen_mcode6(const char *, const char *, int, struct qual); 171#endif 172struct block *gen_ncode(const char *, bpf_u_int32, struct qual); 173struct block *gen_proto_abbrev(int); 174struct block *gen_relation(int, struct arth *, struct arth *, int); 175struct block *gen_less(int); 176struct block *gen_greater(int); 177struct block *gen_byteop(int, int, int); 178struct block *gen_broadcast(int); 179struct block *gen_multicast(int); 180struct block *gen_inbound(int); 181struct block *gen_sample(int); 182 183struct block *gen_vlan(int); 184struct block *gen_mpls(int); 185 186struct block *gen_pf_ifname(char *); 187struct block *gen_pf_rnr(int); 188struct block *gen_pf_srnr(int); 189struct block *gen_pf_ruleset(char *); 190struct block *gen_pf_reason(int); 191struct block *gen_pf_action(int); 192 193struct block *gen_p80211_type(int, int); 194struct block *gen_p80211_fcdir(int); 195 196void bpf_optimize(struct block **); 197__dead void bpf_error(const char *, ...) 198 __attribute__((__format__ (printf, 1, 2))); 199 200void finish_parse(struct block *); 201char *sdup(const char *); 202 203struct bpf_insn *icode_to_fcode(struct block *, int *); 204int pcap_parse(void); 205void lex_init(const char *); 206void sappend(struct slist *, struct slist *); 207 208/* XXX */ 209#define JT(b) ((b)->et.succ) 210#define JF(b) ((b)->ef.succ) 211 212extern int no_optimize; 213