1*e1cb6e2eSjsg /* $OpenBSD: gencode.h,v 1.22 2024/05/21 11:13:08 jsg Exp $ */ 2df930be7Sderaadt 3df930be7Sderaadt /* 49b113833Smickey * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996 5df930be7Sderaadt * The Regents of the University of California. All rights reserved. 6df930be7Sderaadt * 7df930be7Sderaadt * Redistribution and use in source and binary forms, with or without 8df930be7Sderaadt * modification, are permitted provided that: (1) source code distributions 9df930be7Sderaadt * retain the above copyright notice and this paragraph in its entirety, (2) 10df930be7Sderaadt * distributions including binary code include the above copyright notice and 11df930be7Sderaadt * this paragraph in its entirety in the documentation or other materials 12df930be7Sderaadt * provided with the distribution, and (3) all advertising materials mentioning 13df930be7Sderaadt * features or use of this software display the following acknowledgement: 14df930be7Sderaadt * ``This product includes software developed by the University of California, 15df930be7Sderaadt * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16df930be7Sderaadt * the University nor the names of its contributors may be used to endorse 17df930be7Sderaadt * or promote products derived from this software without specific prior 18df930be7Sderaadt * written permission. 19df930be7Sderaadt * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20df930be7Sderaadt * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21df930be7Sderaadt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22df930be7Sderaadt */ 23df930be7Sderaadt 249b113833Smickey /* Address qualifiers. */ 25df930be7Sderaadt 26df930be7Sderaadt #define Q_HOST 1 27df930be7Sderaadt #define Q_NET 2 28df930be7Sderaadt #define Q_PORT 3 29df930be7Sderaadt #define Q_GATEWAY 4 30df930be7Sderaadt #define Q_PROTO 5 31a9b0695fSjakob #define Q_PROTOCHAIN 6 32df930be7Sderaadt 33df930be7Sderaadt /* Protocol qualifiers. */ 34df930be7Sderaadt 35df930be7Sderaadt #define Q_LINK 1 36df930be7Sderaadt #define Q_IP 2 37df930be7Sderaadt #define Q_ARP 3 38df930be7Sderaadt #define Q_RARP 4 39df930be7Sderaadt #define Q_TCP 5 40df930be7Sderaadt #define Q_UDP 6 41df930be7Sderaadt #define Q_ICMP 7 429b113833Smickey #define Q_IGMP 8 4301efc7efSderaadt #define Q_IGRP 9 44df930be7Sderaadt 4501efc7efSderaadt 4601efc7efSderaadt #define Q_ATALK 10 4701efc7efSderaadt #define Q_DECNET 11 4801efc7efSderaadt #define Q_LAT 12 4901efc7efSderaadt #define Q_SCA 13 5001efc7efSderaadt #define Q_MOPRC 14 5101efc7efSderaadt #define Q_MOPDL 15 52df930be7Sderaadt 53a9b0695fSjakob 54a9b0695fSjakob #define Q_IPV6 16 55a9b0695fSjakob #define Q_ICMPV6 17 56a9b0695fSjakob #define Q_AH 18 57a9b0695fSjakob #define Q_ESP 19 58a9b0695fSjakob 59a9b0695fSjakob #define Q_PIM 20 60f562933eSmpf #define Q_STP 21 61a9b0695fSjakob 629b113833Smickey /* Directional qualifiers. */ 63df930be7Sderaadt 64df930be7Sderaadt #define Q_SRC 1 65df930be7Sderaadt #define Q_DST 2 66df930be7Sderaadt #define Q_OR 3 67df930be7Sderaadt #define Q_AND 4 68d42a50a0Sreyk #define Q_ADDR1 5 69d42a50a0Sreyk #define Q_ADDR2 6 70d42a50a0Sreyk #define Q_ADDR3 7 71d42a50a0Sreyk #define Q_ADDR4 8 72df930be7Sderaadt 73df930be7Sderaadt #define Q_DEFAULT 0 74df930be7Sderaadt #define Q_UNDEF 255 75df930be7Sderaadt 76a9b0695fSjakob struct slist; 77a9b0695fSjakob 78df930be7Sderaadt struct stmt { 79df930be7Sderaadt int code; 80a9b0695fSjakob struct slist *jt; /*only for relative jump in block*/ 81a9b0695fSjakob struct slist *jf; /*only for relative jump in block*/ 829b113833Smickey bpf_int32 k; 83df930be7Sderaadt }; 84df930be7Sderaadt 85df930be7Sderaadt struct slist { 86df930be7Sderaadt struct stmt s; 87df930be7Sderaadt struct slist *next; 88df930be7Sderaadt }; 89df930be7Sderaadt 90df930be7Sderaadt /* 91df930be7Sderaadt * A bit vector to represent definition sets. We assume TOT_REGISTERS 92df930be7Sderaadt * is smaller than 8*sizeof(atomset). 93df930be7Sderaadt */ 949b113833Smickey typedef bpf_u_int32 atomset; 95df930be7Sderaadt #define ATOMMASK(n) (1 << (n)) 96df930be7Sderaadt #define ATOMELEM(d, n) (d & ATOMMASK(n)) 97df930be7Sderaadt 98df930be7Sderaadt /* 99df930be7Sderaadt * An unbounded set. 100df930be7Sderaadt */ 1019b113833Smickey typedef bpf_u_int32 *uset; 102df930be7Sderaadt 103df930be7Sderaadt /* 104df930be7Sderaadt * Total number of atomic entities, including accumulator (A) and index (X). 105df930be7Sderaadt * We treat all these guys similarly during flow analysis. 106df930be7Sderaadt */ 107df930be7Sderaadt #define N_ATOMS (BPF_MEMWORDS+2) 108df930be7Sderaadt 109df930be7Sderaadt struct edge { 110df930be7Sderaadt int id; 111df930be7Sderaadt int code; 112df930be7Sderaadt uset edom; 113df930be7Sderaadt struct block *succ; 114df930be7Sderaadt struct block *pred; 115df930be7Sderaadt struct edge *next; /* link list of incoming edges for a node */ 116df930be7Sderaadt }; 117df930be7Sderaadt 118df930be7Sderaadt struct block { 119df930be7Sderaadt int id; 120df930be7Sderaadt struct slist *stmts; /* side effect stmts */ 121df930be7Sderaadt struct stmt s; /* branch stmt */ 122df930be7Sderaadt int mark; 1239b113833Smickey int longjt; /* jt branch requires long jump */ 1249b113833Smickey int longjf; /* jf branch requires long jump */ 125df930be7Sderaadt int level; 126df930be7Sderaadt int offset; 127df930be7Sderaadt int sense; 128df930be7Sderaadt struct edge et; 129df930be7Sderaadt struct edge ef; 130df930be7Sderaadt struct block *head; 131df930be7Sderaadt struct block *link; /* link field used by optimizer */ 132df930be7Sderaadt uset dom; 133df930be7Sderaadt uset closure; 134df930be7Sderaadt struct edge *in_edges; 135df930be7Sderaadt atomset def, kill; 136df930be7Sderaadt atomset in_use; 137df930be7Sderaadt atomset out_use; 1389b113833Smickey int oval; 1399b113833Smickey int val[N_ATOMS]; 140df930be7Sderaadt }; 141df930be7Sderaadt 142df930be7Sderaadt struct arth { 143df930be7Sderaadt struct block *b; /* protocol checks */ 144df930be7Sderaadt struct slist *s; /* stmt list */ 145df930be7Sderaadt int regno; /* virtual register number of result */ 146df930be7Sderaadt }; 147df930be7Sderaadt 148df930be7Sderaadt struct qual { 149df930be7Sderaadt unsigned char addr; 150df930be7Sderaadt unsigned char proto; 151df930be7Sderaadt unsigned char dir; 152df930be7Sderaadt unsigned char pad; 153df930be7Sderaadt }; 154df930be7Sderaadt 155df930be7Sderaadt struct arth *gen_loadi(int); 156df930be7Sderaadt struct arth *gen_load(int, struct arth *, int); 157df930be7Sderaadt struct arth *gen_loadlen(void); 158a8e9f808Sdlg struct arth *gen_loadrnd(void); 159df930be7Sderaadt struct arth *gen_neg(struct arth *); 160df930be7Sderaadt struct arth *gen_arth(int, struct arth *, struct arth *); 161df930be7Sderaadt 162df930be7Sderaadt void gen_and(struct block *, struct block *); 163df930be7Sderaadt void gen_or(struct block *, struct block *); 164df930be7Sderaadt void gen_not(struct block *); 165df930be7Sderaadt 16601efc7efSderaadt struct block *gen_scode(const char *, struct qual); 16701efc7efSderaadt struct block *gen_ecode(const u_char *, struct qual); 16801efc7efSderaadt struct block *gen_mcode(const char *, const char *, int, struct qual); 169a9b0695fSjakob #ifdef INET6 170a9b0695fSjakob struct block *gen_mcode6(const char *, const char *, int, struct qual); 171a9b0695fSjakob #endif 17201efc7efSderaadt struct block *gen_ncode(const char *, bpf_u_int32, struct qual); 173df930be7Sderaadt struct block *gen_proto_abbrev(int); 174df930be7Sderaadt struct block *gen_relation(int, struct arth *, struct arth *, int); 175df930be7Sderaadt struct block *gen_less(int); 176df930be7Sderaadt struct block *gen_greater(int); 177df930be7Sderaadt struct block *gen_byteop(int, int, int); 178df930be7Sderaadt struct block *gen_broadcast(int); 179df930be7Sderaadt struct block *gen_multicast(int); 180df930be7Sderaadt struct block *gen_inbound(int); 1812cba36b5Sdlg struct block *gen_sample(int); 182df930be7Sderaadt 183febc6dcdSdtucker struct block *gen_vlan(int); 184ff52dd4aSdenis struct block *gen_mpls(int); 185febc6dcdSdtucker 186e83aed87Sfrantzen struct block *gen_pf_ifname(char *); 187e83aed87Sfrantzen struct block *gen_pf_rnr(int); 188263bcd0cScanacar struct block *gen_pf_srnr(int); 189263bcd0cScanacar struct block *gen_pf_ruleset(char *); 190e83aed87Sfrantzen struct block *gen_pf_reason(int); 191e83aed87Sfrantzen struct block *gen_pf_action(int); 192e83aed87Sfrantzen 193d42a50a0Sreyk struct block *gen_p80211_type(int, int); 194d42a50a0Sreyk struct block *gen_p80211_fcdir(int); 195d42a50a0Sreyk 196df930be7Sderaadt void bpf_optimize(struct block **); 1979b113833Smickey __dead void bpf_error(const char *, ...) 198e389019dSmillert __attribute__((__format__ (printf, 1, 2))); 199df930be7Sderaadt 200df930be7Sderaadt void finish_parse(struct block *); 20101efc7efSderaadt char *sdup(const char *); 202df930be7Sderaadt 203df930be7Sderaadt struct bpf_insn *icode_to_fcode(struct block *, int *); 204df930be7Sderaadt int pcap_parse(void); 205aa4ed2b9Ssthen void lex_init(const char *); 206df930be7Sderaadt void sappend(struct slist *, struct slist *); 207df930be7Sderaadt 208df930be7Sderaadt /* XXX */ 209df930be7Sderaadt #define JT(b) ((b)->et.succ) 210df930be7Sderaadt #define JF(b) ((b)->ef.succ) 211a9b0695fSjakob 212a9b0695fSjakob extern int no_optimize; 213