Coverage Report

Created: 2023-01-17 06:11

/src/libpcap/gencode.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3
 *  The Regents of the University of California.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that: (1) source code distributions
7
 * retain the above copyright notice and this paragraph in its entirety, (2)
8
 * distributions including binary code include the above copyright notice and
9
 * this paragraph in its entirety in the documentation or other materials
10
 * provided with the distribution, and (3) all advertising materials mentioning
11
 * features or use of this software display the following acknowledgement:
12
 * ``This product includes software developed by the University of California,
13
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14
 * the University nor the names of its contributors may be used to endorse
15
 * or promote products derived from this software without specific prior
16
 * written permission.
17
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20
 */
21
22
#ifdef HAVE_CONFIG_H
23
#include <config.h>
24
#endif
25
26
#ifdef _WIN32
27
  #include <ws2tcpip.h>
28
#else
29
  #include <sys/socket.h>
30
31
  #ifdef __NetBSD__
32
    #include <sys/param.h>
33
  #endif
34
35
  #include <netinet/in.h>
36
  #include <arpa/inet.h>
37
#endif /* _WIN32 */
38
39
#include <stdlib.h>
40
#include <string.h>
41
#include <memory.h>
42
#include <setjmp.h>
43
#include <stdarg.h>
44
#include <stdio.h>
45
46
#ifdef MSDOS
47
#include "pcap-dos.h"
48
#endif
49
50
#include "pcap-int.h"
51
52
#include "extract.h"
53
54
#include "ethertype.h"
55
#include "nlpid.h"
56
#include "llc.h"
57
#include "gencode.h"
58
#include "ieee80211.h"
59
#include "atmuni31.h"
60
#include "sunatmpos.h"
61
#include "pflog.h"
62
#include "ppp.h"
63
#include "pcap/sll.h"
64
#include "pcap/ipnet.h"
65
#include "arcnet.h"
66
#include "diag-control.h"
67
68
#include "scanner.h"
69
70
#if defined(linux)
71
#include <linux/types.h>
72
#include <linux/if_packet.h>
73
#include <linux/filter.h>
74
#endif
75
76
#ifndef offsetof
77
#define offsetof(s, e) ((size_t)&((s *)0)->e)
78
#endif
79
80
#ifdef _WIN32
81
  #ifdef INET6
82
    #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
83
/* IPv6 address */
84
struct in6_addr
85
  {
86
    union
87
      {
88
  uint8_t   u6_addr8[16];
89
  uint16_t  u6_addr16[8];
90
  uint32_t  u6_addr32[4];
91
      } in6_u;
92
#define s6_addr     in6_u.u6_addr8
93
#define s6_addr16   in6_u.u6_addr16
94
#define s6_addr32   in6_u.u6_addr32
95
#define s6_addr64   in6_u.u6_addr64
96
  };
97
98
typedef unsigned short  sa_family_t;
99
100
#define __SOCKADDR_COMMON(sa_prefix) \
101
  sa_family_t sa_prefix##family
102
103
/* Ditto, for IPv6.  */
104
struct sockaddr_in6
105
  {
106
    __SOCKADDR_COMMON (sin6_);
107
    uint16_t sin6_port;   /* Transport layer port # */
108
    uint32_t sin6_flowinfo; /* IPv6 flow information */
109
    struct in6_addr sin6_addr;  /* IPv6 address */
110
  };
111
112
      #ifndef EAI_ADDRFAMILY
113
struct addrinfo {
114
  int ai_flags; /* AI_PASSIVE, AI_CANONNAME */
115
  int ai_family;  /* PF_xxx */
116
  int ai_socktype;  /* SOCK_xxx */
117
  int ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
118
  size_t  ai_addrlen; /* length of ai_addr */
119
  char  *ai_canonname;  /* canonical name for hostname */
120
  struct sockaddr *ai_addr; /* binary address */
121
  struct addrinfo *ai_next; /* next structure in linked list */
122
};
123
      #endif /* EAI_ADDRFAMILY */
124
    #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
125
  #endif /* INET6 */
126
#else /* _WIN32 */
127
  #include <netdb.h>  /* for "struct addrinfo" */
128
#endif /* _WIN32 */
129
#include <pcap/namedb.h>
130
131
#include "nametoaddr.h"
132
133
24.4k
#define ETHERMTU  1500
134
135
#ifndef IPPROTO_HOPOPTS
136
#define IPPROTO_HOPOPTS 0
137
#endif
138
#ifndef IPPROTO_ROUTING
139
#define IPPROTO_ROUTING 43
140
#endif
141
#ifndef IPPROTO_FRAGMENT
142
#define IPPROTO_FRAGMENT 44
143
#endif
144
#ifndef IPPROTO_DSTOPTS
145
#define IPPROTO_DSTOPTS 60
146
#endif
147
#ifndef IPPROTO_SCTP
148
#define IPPROTO_SCTP 132
149
#endif
150
151
0
#define GENEVE_PORT 6081
152
153
#ifdef HAVE_OS_PROTO_H
154
#include "os-proto.h"
155
#endif
156
157
294k
#define JMP(c) ((c)|BPF_JMP|BPF_K)
158
159
/*
160
 * "Push" the current value of the link-layer header type and link-layer
161
 * header offset onto a "stack", and set a new value.  (It's not a
162
 * full-blown stack; we keep only the top two items.)
163
 */
164
427
#define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
165
427
{ \
166
427
  (cs)->prevlinktype = (cs)->linktype; \
167
427
  (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
168
427
  (cs)->linktype = (new_linktype); \
169
427
  (cs)->off_linkhdr.is_variable = (new_is_variable); \
170
427
  (cs)->off_linkhdr.constant_part = (new_constant_part); \
171
427
  (cs)->off_linkhdr.reg = (new_reg); \
172
427
  (cs)->is_geneve = 0; \
173
427
}
174
175
/*
176
 * Offset "not set" value.
177
 */
178
152k
#define OFFSET_NOT_SET  0xffffffffU
179
180
/*
181
 * Absolute offsets, which are offsets from the beginning of the raw
182
 * packet data, are, in the general case, the sum of a variable value
183
 * and a constant value; the variable value may be absent, in which
184
 * case the offset is only the constant value, and the constant value
185
 * may be zero, in which case the offset is only the variable value.
186
 *
187
 * bpf_abs_offset is a structure containing all that information:
188
 *
189
 *   is_variable is 1 if there's a variable part.
190
 *
191
 *   constant_part is the constant part of the value, possibly zero;
192
 *
193
 *   if is_variable is 1, reg is the register number for a register
194
 *   containing the variable value if the register has been assigned,
195
 *   and -1 otherwise.
196
 */
197
typedef struct {
198
  int is_variable;
199
  u_int constant_part;
200
  int reg;
201
} bpf_abs_offset;
202
203
/*
204
 * Value passed to gen_load_a() to indicate what the offset argument
205
 * is relative to the beginning of.
206
 */
207
enum e_offrel {
208
  OR_PACKET,    /* full packet data */
209
  OR_LINKHDR,   /* link-layer header */
210
  OR_PREVLINKHDR,   /* previous link-layer header */
211
  OR_LLC,     /* 802.2 LLC header */
212
  OR_PREVMPLSHDR,   /* previous MPLS header */
213
  OR_LINKTYPE,    /* link-layer type */
214
  OR_LINKPL,    /* link-layer payload */
215
  OR_LINKPL_NOSNAP, /* link-layer payload, with no SNAP header at the link layer */
216
  OR_TRAN_IPV4,   /* transport-layer header, with IPv4 network layer */
217
  OR_TRAN_IPV6    /* transport-layer header, with IPv6 network layer */
218
};
219
220
/*
221
 * We divy out chunks of memory rather than call malloc each time so
222
 * we don't have to worry about leaking memory.  It's probably
223
 * not a big deal if all this memory was wasted but if this ever
224
 * goes into a library that would probably not be a good idea.
225
 *
226
 * XXX - this *is* in a library....
227
 */
228
482k
#define NCHUNKS 16
229
23.0k
#define CHUNK0SIZE 1024
230
struct chunk {
231
  size_t n_left;
232
  void *m;
233
};
234
235
/* Code generator state */
236
237
struct _compiler_state {
238
  jmp_buf top_ctx;
239
  pcap_t *bpf_pcap;
240
  int error_set;
241
242
  struct icode ic;
243
244
  int snaplen;
245
246
  int linktype;
247
  int prevlinktype;
248
  int outermostlinktype;
249
250
  bpf_u_int32 netmask;
251
  int no_optimize;
252
253
  /* Hack for handling VLAN and MPLS stacks. */
254
  u_int label_stack_depth;
255
  u_int vlan_stack_depth;
256
257
  /* XXX */
258
  u_int pcap_fddipad;
259
260
  /*
261
   * As errors are handled by a longjmp, anything allocated must
262
   * be freed in the longjmp handler, so it must be reachable
263
   * from that handler.
264
   *
265
   * One thing that's allocated is the result of pcap_nametoaddrinfo();
266
   * it must be freed with freeaddrinfo().  This variable points to
267
   * any addrinfo structure that would need to be freed.
268
   */
269
  struct addrinfo *ai;
270
271
  /*
272
   * Another thing that's allocated is the result of pcap_ether_aton();
273
   * it must be freed with free().  This variable points to any
274
   * address that would need to be freed.
275
   */
276
  u_char *e;
277
278
  /*
279
   * Various code constructs need to know the layout of the packet.
280
   * These values give the necessary offsets from the beginning
281
   * of the packet data.
282
   */
283
284
  /*
285
   * Absolute offset of the beginning of the link-layer header.
286
   */
287
  bpf_abs_offset off_linkhdr;
288
289
  /*
290
   * If we're checking a link-layer header for a packet encapsulated
291
   * in another protocol layer, this is the equivalent information
292
   * for the previous layers' link-layer header from the beginning
293
   * of the raw packet data.
294
   */
295
  bpf_abs_offset off_prevlinkhdr;
296
297
  /*
298
   * This is the equivalent information for the outermost layers'
299
   * link-layer header.
300
   */
301
  bpf_abs_offset off_outermostlinkhdr;
302
303
  /*
304
   * Absolute offset of the beginning of the link-layer payload.
305
   */
306
  bpf_abs_offset off_linkpl;
307
308
  /*
309
   * "off_linktype" is the offset to information in the link-layer
310
   * header giving the packet type. This is an absolute offset
311
   * from the beginning of the packet.
312
   *
313
   * For Ethernet, it's the offset of the Ethernet type field; this
314
   * means that it must have a value that skips VLAN tags.
315
   *
316
   * For link-layer types that always use 802.2 headers, it's the
317
   * offset of the LLC header; this means that it must have a value
318
   * that skips VLAN tags.
319
   *
320
   * For PPP, it's the offset of the PPP type field.
321
   *
322
   * For Cisco HDLC, it's the offset of the CHDLC type field.
323
   *
324
   * For BSD loopback, it's the offset of the AF_ value.
325
   *
326
   * For Linux cooked sockets, it's the offset of the type field.
327
   *
328
   * off_linktype.constant_part is set to OFFSET_NOT_SET for no
329
   * encapsulation, in which case, IP is assumed.
330
   */
331
  bpf_abs_offset off_linktype;
332
333
  /*
334
   * TRUE if the link layer includes an ATM pseudo-header.
335
   */
336
  int is_atm;
337
338
  /*
339
   * TRUE if "geneve" appeared in the filter; it causes us to
340
   * generate code that checks for a Geneve header and assume
341
   * that later filters apply to the encapsulated payload.
342
   */
343
  int is_geneve;
344
345
  /*
346
   * TRUE if we need variable length part of VLAN offset
347
   */
348
  int is_vlan_vloffset;
349
350
  /*
351
   * These are offsets for the ATM pseudo-header.
352
   */
353
  u_int off_vpi;
354
  u_int off_vci;
355
  u_int off_proto;
356
357
  /*
358
   * These are offsets for the MTP2 fields.
359
   */
360
  u_int off_li;
361
  u_int off_li_hsl;
362
363
  /*
364
   * These are offsets for the MTP3 fields.
365
   */
366
  u_int off_sio;
367
  u_int off_opc;
368
  u_int off_dpc;
369
  u_int off_sls;
370
371
  /*
372
   * This is the offset of the first byte after the ATM pseudo_header,
373
   * or -1 if there is no ATM pseudo-header.
374
   */
375
  u_int off_payload;
376
377
  /*
378
   * These are offsets to the beginning of the network-layer header.
379
   * They are relative to the beginning of the link-layer payload
380
   * (i.e., they don't include off_linkhdr.constant_part or
381
   * off_linkpl.constant_part).
382
   *
383
   * If the link layer never uses 802.2 LLC:
384
   *
385
   *  "off_nl" and "off_nl_nosnap" are the same.
386
   *
387
   * If the link layer always uses 802.2 LLC:
388
   *
389
   *  "off_nl" is the offset if there's a SNAP header following
390
   *  the 802.2 header;
391
   *
392
   *  "off_nl_nosnap" is the offset if there's no SNAP header.
393
   *
394
   * If the link layer is Ethernet:
395
   *
396
   *  "off_nl" is the offset if the packet is an Ethernet II packet
397
   *  (we assume no 802.3+802.2+SNAP);
398
   *
399
   *  "off_nl_nosnap" is the offset if the packet is an 802.3 packet
400
   *  with an 802.2 header following it.
401
   */
402
  u_int off_nl;
403
  u_int off_nl_nosnap;
404
405
  /*
406
   * Here we handle simple allocation of the scratch registers.
407
   * If too many registers are alloc'd, the allocator punts.
408
   */
409
  int regused[BPF_MEMWORDS];
410
  int curreg;
411
412
  /*
413
   * Memory chunks.
414
   */
415
  struct chunk chunks[NCHUNKS];
416
  int cur_chunk;
417
};
418
419
/*
420
 * For use by routines outside this file.
421
 */
422
/* VARARGS */
423
void
424
bpf_set_error(compiler_state_t *cstate, const char *fmt, ...)
425
2.33k
{
426
2.33k
  va_list ap;
427
428
  /*
429
   * If we've already set an error, don't override it.
430
   * The lexical analyzer reports some errors by setting
431
   * the error and then returning a LEX_ERROR token, which
432
   * is not recognized by any grammar rule, and thus forces
433
   * the parse to stop.  We don't want the error reported
434
   * by the lexical analyzer to be overwritten by the syntax
435
   * error.
436
   */
437
2.33k
  if (!cstate->error_set) {
438
2.27k
    va_start(ap, fmt);
439
2.27k
    (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE,
440
2.27k
        fmt, ap);
441
2.27k
    va_end(ap);
442
2.27k
    cstate->error_set = 1;
443
2.27k
  }
444
2.33k
}
445
446
/*
447
 * For use *ONLY* in routines in this file.
448
 */
449
static void PCAP_NORETURN bpf_error(compiler_state_t *, const char *, ...)
450
    PCAP_PRINTFLIKE(2, 3);
451
452
/* VARARGS */
453
static void PCAP_NORETURN
454
bpf_error(compiler_state_t *cstate, const char *fmt, ...)
455
1.87k
{
456
1.87k
  va_list ap;
457
458
1.87k
  va_start(ap, fmt);
459
1.87k
  (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE,
460
1.87k
      fmt, ap);
461
1.87k
  va_end(ap);
462
1.87k
  longjmp(cstate->top_ctx, 1);
463
  /*NOTREACHED*/
464
#ifdef _AIX
465
  PCAP_UNREACHABLE
466
#endif /* _AIX */
467
1.87k
}
468
469
static int init_linktype(compiler_state_t *, pcap_t *);
470
471
static void init_regs(compiler_state_t *);
472
static int alloc_reg(compiler_state_t *);
473
static void free_reg(compiler_state_t *, int);
474
475
static void initchunks(compiler_state_t *cstate);
476
static void *newchunk_nolongjmp(compiler_state_t *cstate, size_t);
477
static void *newchunk(compiler_state_t *cstate, size_t);
478
static void freechunks(compiler_state_t *cstate);
479
static inline struct block *new_block(compiler_state_t *cstate, int);
480
static inline struct slist *new_stmt(compiler_state_t *cstate, int);
481
static struct block *gen_retblk(compiler_state_t *cstate, int);
482
static inline void syntax(compiler_state_t *cstate);
483
484
static void backpatch(struct block *, struct block *);
485
static void merge(struct block *, struct block *);
486
static struct block *gen_cmp(compiler_state_t *, enum e_offrel, u_int,
487
    u_int, bpf_u_int32);
488
static struct block *gen_cmp_gt(compiler_state_t *, enum e_offrel, u_int,
489
    u_int, bpf_u_int32);
490
static struct block *gen_cmp_ge(compiler_state_t *, enum e_offrel, u_int,
491
    u_int, bpf_u_int32);
492
static struct block *gen_cmp_lt(compiler_state_t *, enum e_offrel, u_int,
493
    u_int, bpf_u_int32);
494
static struct block *gen_cmp_le(compiler_state_t *, enum e_offrel, u_int,
495
    u_int, bpf_u_int32);
496
static struct block *gen_mcmp(compiler_state_t *, enum e_offrel, u_int,
497
    u_int, bpf_u_int32, bpf_u_int32);
498
static struct block *gen_bcmp(compiler_state_t *, enum e_offrel, u_int,
499
    u_int, const u_char *);
500
static struct block *gen_ncmp(compiler_state_t *, enum e_offrel, u_int,
501
    u_int, bpf_u_int32, int, int, bpf_u_int32);
502
static struct slist *gen_load_absoffsetrel(compiler_state_t *, bpf_abs_offset *,
503
    u_int, u_int);
504
static struct slist *gen_load_a(compiler_state_t *, enum e_offrel, u_int,
505
    u_int);
506
static struct slist *gen_loadx_iphdrlen(compiler_state_t *);
507
static struct block *gen_uncond(compiler_state_t *, int);
508
static inline struct block *gen_true(compiler_state_t *);
509
static inline struct block *gen_false(compiler_state_t *);
510
static struct block *gen_ether_linktype(compiler_state_t *, bpf_u_int32);
511
static struct block *gen_ipnet_linktype(compiler_state_t *, bpf_u_int32);
512
static struct block *gen_linux_sll_linktype(compiler_state_t *, bpf_u_int32);
513
static struct slist *gen_load_pflog_llprefixlen(compiler_state_t *);
514
static struct slist *gen_load_prism_llprefixlen(compiler_state_t *);
515
static struct slist *gen_load_avs_llprefixlen(compiler_state_t *);
516
static struct slist *gen_load_radiotap_llprefixlen(compiler_state_t *);
517
static struct slist *gen_load_ppi_llprefixlen(compiler_state_t *);
518
static void insert_compute_vloffsets(compiler_state_t *, struct block *);
519
static struct slist *gen_abs_offset_varpart(compiler_state_t *,
520
    bpf_abs_offset *);
521
static bpf_u_int32 ethertype_to_ppptype(bpf_u_int32);
522
static struct block *gen_linktype(compiler_state_t *, bpf_u_int32);
523
static struct block *gen_snap(compiler_state_t *, bpf_u_int32, bpf_u_int32);
524
static struct block *gen_llc_linktype(compiler_state_t *, bpf_u_int32);
525
static struct block *gen_hostop(compiler_state_t *, bpf_u_int32, bpf_u_int32,
526
    int, bpf_u_int32, u_int, u_int);
527
#ifdef INET6
528
static struct block *gen_hostop6(compiler_state_t *, struct in6_addr *,
529
    struct in6_addr *, int, bpf_u_int32, u_int, u_int);
530
#endif
531
static struct block *gen_ahostop(compiler_state_t *, const u_char *, int);
532
static struct block *gen_ehostop(compiler_state_t *, const u_char *, int);
533
static struct block *gen_fhostop(compiler_state_t *, const u_char *, int);
534
static struct block *gen_thostop(compiler_state_t *, const u_char *, int);
535
static struct block *gen_wlanhostop(compiler_state_t *, const u_char *, int);
536
static struct block *gen_ipfchostop(compiler_state_t *, const u_char *, int);
537
static struct block *gen_dnhostop(compiler_state_t *, bpf_u_int32, int);
538
static struct block *gen_mpls_linktype(compiler_state_t *, bpf_u_int32);
539
static struct block *gen_host(compiler_state_t *, bpf_u_int32, bpf_u_int32,
540
    int, int, int);
541
#ifdef INET6
542
static struct block *gen_host6(compiler_state_t *, struct in6_addr *,
543
    struct in6_addr *, int, int, int);
544
#endif
545
#ifndef INET6
546
static struct block *gen_gateway(compiler_state_t *, const u_char *,
547
    struct addrinfo *, int, int);
548
#endif
549
static struct block *gen_ipfrag(compiler_state_t *);
550
static struct block *gen_portatom(compiler_state_t *, int, bpf_u_int32);
551
static struct block *gen_portrangeatom(compiler_state_t *, u_int, bpf_u_int32,
552
    bpf_u_int32);
553
static struct block *gen_portatom6(compiler_state_t *, int, bpf_u_int32);
554
static struct block *gen_portrangeatom6(compiler_state_t *, u_int, bpf_u_int32,
555
    bpf_u_int32);
556
static struct block *gen_portop(compiler_state_t *, u_int, u_int, int);
557
static struct block *gen_port(compiler_state_t *, u_int, int, int);
558
static struct block *gen_portrangeop(compiler_state_t *, u_int, u_int,
559
    bpf_u_int32, int);
560
static struct block *gen_portrange(compiler_state_t *, u_int, u_int, int, int);
561
struct block *gen_portop6(compiler_state_t *, u_int, u_int, int);
562
static struct block *gen_port6(compiler_state_t *, u_int, int, int);
563
static struct block *gen_portrangeop6(compiler_state_t *, u_int, u_int,
564
    bpf_u_int32, int);
565
static struct block *gen_portrange6(compiler_state_t *, u_int, u_int, int, int);
566
static int lookup_proto(compiler_state_t *, const char *, int);
567
#if !defined(NO_PROTOCHAIN)
568
static struct block *gen_protochain(compiler_state_t *, bpf_u_int32, int);
569
#endif /* !defined(NO_PROTOCHAIN) */
570
static struct block *gen_proto(compiler_state_t *, bpf_u_int32, int, int);
571
static struct slist *xfer_to_x(compiler_state_t *, struct arth *);
572
static struct slist *xfer_to_a(compiler_state_t *, struct arth *);
573
static struct block *gen_mac_multicast(compiler_state_t *, int);
574
static struct block *gen_len(compiler_state_t *, int, int);
575
static struct block *gen_check_802_11_data_frame(compiler_state_t *);
576
static struct block *gen_geneve_ll_check(compiler_state_t *cstate);
577
578
static struct block *gen_ppi_dlt_check(compiler_state_t *);
579
static struct block *gen_atmfield_code_internal(compiler_state_t *, int,
580
    bpf_u_int32, int, int);
581
static struct block *gen_atmtype_llc(compiler_state_t *);
582
static struct block *gen_msg_abbrev(compiler_state_t *, int type);
583
584
static void
585
initchunks(compiler_state_t *cstate)
586
13.5k
{
587
13.5k
  int i;
588
589
229k
  for (i = 0; i < NCHUNKS; i++) {
590
216k
    cstate->chunks[i].n_left = 0;
591
216k
    cstate->chunks[i].m = NULL;
592
216k
  }
593
13.5k
  cstate->cur_chunk = 0;
594
13.5k
}
595
596
static void *
597
newchunk_nolongjmp(compiler_state_t *cstate, size_t n)
598
1.04M
{
599
1.04M
  struct chunk *cp;
600
1.04M
  int k;
601
1.04M
  size_t size;
602
603
1.04M
#ifndef __NetBSD__
604
  /* XXX Round up to nearest long. */
605
1.04M
  n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
606
#else
607
  /* XXX Round up to structure boundary. */
608
  n = ALIGN(n);
609
#endif
610
611
1.04M
  cp = &cstate->chunks[cstate->cur_chunk];
612
1.04M
  if (n > cp->n_left) {
613
23.0k
    ++cp;
614
23.0k
    k = ++cstate->cur_chunk;
615
23.0k
    if (k >= NCHUNKS) {
616
0
      bpf_set_error(cstate, "out of memory");
617
0
      return (NULL);
618
0
    }
619
23.0k
    size = CHUNK0SIZE << k;
620
23.0k
    cp->m = (void *)malloc(size);
621
23.0k
    if (cp->m == NULL) {
622
0
      bpf_set_error(cstate, "out of memory");
623
0
      return (NULL);
624
0
    }
625
23.0k
    memset((char *)cp->m, 0, size);
626
23.0k
    cp->n_left = size;
627
23.0k
    if (n > size) {
628
0
      bpf_set_error(cstate, "out of memory");
629
0
      return (NULL);
630
0
    }
631
23.0k
  }
632
1.04M
  cp->n_left -= n;
633
1.04M
  return (void *)((char *)cp->m + cp->n_left);
634
1.04M
}
635
636
static void *
637
newchunk(compiler_state_t *cstate, size_t n)
638
1.03M
{
639
1.03M
  void *p;
640
641
1.03M
  p = newchunk_nolongjmp(cstate, n);
642
1.03M
  if (p == NULL) {
643
0
    longjmp(cstate->top_ctx, 1);
644
    /*NOTREACHED*/
645
0
  }
646
1.03M
  return (p);
647
1.03M
}
648
649
static void
650
freechunks(compiler_state_t *cstate)
651
13.5k
{
652
13.5k
  int i;
653
654
229k
  for (i = 0; i < NCHUNKS; ++i)
655
216k
    if (cstate->chunks[i].m != NULL)
656
23.0k
      free(cstate->chunks[i].m);
657
13.5k
}
658
659
/*
660
 * A strdup whose allocations are freed after code generation is over.
661
 * This is used by the lexical analyzer, so it can't longjmp; it just
662
 * returns NULL on an allocation error, and the callers must check
663
 * for it.
664
 */
665
char *
666
sdup(compiler_state_t *cstate, const char *s)
667
6.26k
{
668
6.26k
  size_t n = strlen(s) + 1;
669
6.26k
  char *cp = newchunk_nolongjmp(cstate, n);
670
671
6.26k
  if (cp == NULL)
672
0
    return (NULL);
673
6.26k
  pcap_strlcpy(cp, s, n);
674
6.26k
  return (cp);
675
6.26k
}
676
677
static inline struct block *
678
new_block(compiler_state_t *cstate, int code)
679
312k
{
680
312k
  struct block *p;
681
682
312k
  p = (struct block *)newchunk(cstate, sizeof(*p));
683
312k
  p->s.code = code;
684
312k
  p->head = p;
685
686
312k
  return p;
687
312k
}
688
689
static inline struct slist *
690
new_stmt(compiler_state_t *cstate, int code)
691
698k
{
692
698k
  struct slist *p;
693
694
698k
  p = (struct slist *)newchunk(cstate, sizeof(*p));
695
698k
  p->s.code = code;
696
697
698k
  return p;
698
698k
}
699
700
static struct block *
701
gen_retblk(compiler_state_t *cstate, int v)
702
16.9k
{
703
16.9k
  struct block *b = new_block(cstate, BPF_RET|BPF_K);
704
705
16.9k
  b->s.k = v;
706
16.9k
  return b;
707
16.9k
}
708
709
static inline PCAP_NORETURN_DEF void
710
syntax(compiler_state_t *cstate)
711
135
{
712
135
  bpf_error(cstate, "syntax error in filter expression");
713
135
}
714
715
int
716
pcap_compile(pcap_t *p, struct bpf_program *program,
717
       const char *buf, int optimize, bpf_u_int32 mask)
718
13.5k
{
719
#ifdef _WIN32
720
  static int done = 0;
721
#endif
722
13.5k
  compiler_state_t cstate;
723
13.5k
  const char * volatile xbuf = buf;
724
13.5k
  yyscan_t scanner = NULL;
725
13.5k
  volatile YY_BUFFER_STATE in_buffer = NULL;
726
13.5k
  u_int len;
727
13.5k
  int  rc;
728
729
  /*
730
   * If this pcap_t hasn't been activated, it doesn't have a
731
   * link-layer type, so we can't use it.
732
   */
733
13.5k
  if (!p->activated) {
734
0
    snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
735
0
        "not-yet-activated pcap_t passed to pcap_compile");
736
0
    return (PCAP_ERROR);
737
0
  }
738
739
#ifdef _WIN32
740
  if (!done)
741
    pcap_wsockinit();
742
  done = 1;
743
#endif
744
745
#ifdef ENABLE_REMOTE
746
  /*
747
   * If the device on which we're capturing need to be notified
748
   * that a new filter is being compiled, do so.
749
   *
750
   * This allows them to save a copy of it, in case, for example,
751
   * they're implementing a form of remote packet capture, and
752
   * want the remote machine to filter out the packets in which
753
   * it's sending the packets it's captured.
754
   *
755
   * XXX - the fact that we happen to be compiling a filter
756
   * doesn't necessarily mean we'll be installing it as the
757
   * filter for this pcap_t; we might be running it from userland
758
   * on captured packets to do packet classification.  We really
759
   * need a better way of handling this, but this is all that
760
   * the WinPcap remote capture code did.
761
   */
762
  if (p->save_current_filter_op != NULL)
763
    (p->save_current_filter_op)(p, buf);
764
#endif
765
766
13.5k
  initchunks(&cstate);
767
13.5k
  cstate.no_optimize = 0;
768
13.5k
#ifdef INET6
769
13.5k
  cstate.ai = NULL;
770
13.5k
#endif
771
13.5k
  cstate.e = NULL;
772
13.5k
  cstate.ic.root = NULL;
773
13.5k
  cstate.ic.cur_mark = 0;
774
13.5k
  cstate.bpf_pcap = p;
775
13.5k
  cstate.error_set = 0;
776
13.5k
  init_regs(&cstate);
777
778
13.5k
  cstate.netmask = mask;
779
780
13.5k
  cstate.snaplen = pcap_snapshot(p);
781
13.5k
  if (cstate.snaplen == 0) {
782
0
    snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
783
0
       "snaplen of 0 rejects all packets");
784
0
    rc = PCAP_ERROR;
785
0
    goto quit;
786
0
  }
787
788
13.5k
  if (pcap_lex_init(&scanner) != 0)
789
0
    pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
790
0
        errno, "can't initialize scanner");
791
13.5k
  in_buffer = pcap__scan_string(xbuf ? xbuf : "", scanner);
792
793
  /*
794
   * Associate the compiler state with the lexical analyzer
795
   * state.
796
   */
797
13.5k
  pcap_set_extra(&cstate, scanner);
798
799
13.5k
  if (init_linktype(&cstate, p) == -1) {
800
82
    rc = PCAP_ERROR;
801
82
    goto quit;
802
82
  }
803
13.4k
  if (pcap_parse(scanner, &cstate) != 0) {
804
4.07k
#ifdef INET6
805
4.07k
    if (cstate.ai != NULL)
806
274
      freeaddrinfo(cstate.ai);
807
4.07k
#endif
808
4.07k
    if (cstate.e != NULL)
809
45
      free(cstate.e);
810
4.07k
    rc = PCAP_ERROR;
811
4.07k
    goto quit;
812
4.07k
  }
813
814
9.36k
  if (cstate.ic.root == NULL) {
815
    /*
816
     * Catch errors reported by gen_retblk().
817
     */
818
2.45k
    if (setjmp(cstate.top_ctx)) {
819
0
      rc = PCAP_ERROR;
820
0
      goto quit;
821
0
    }
822
2.45k
    cstate.ic.root = gen_retblk(&cstate, cstate.snaplen);
823
2.45k
  }
824
825
9.36k
  if (optimize && !cstate.no_optimize) {
826
8.73k
    if (bpf_optimize(&cstate.ic, p->errbuf) == -1) {
827
      /* Failure */
828
66
      rc = PCAP_ERROR;
829
66
      goto quit;
830
66
    }
831
8.67k
    if (cstate.ic.root == NULL ||
832
8.67k
        (cstate.ic.root->s.code == (BPF_RET|BPF_K) && cstate.ic.root->s.k == 0)) {
833
1.14k
      (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
834
1.14k
          "expression rejects all packets");
835
1.14k
      rc = PCAP_ERROR;
836
1.14k
      goto quit;
837
1.14k
    }
838
8.67k
  }
839
8.15k
  program->bf_insns = icode_to_fcode(&cstate.ic,
840
8.15k
      cstate.ic.root, &len, p->errbuf);
841
8.15k
  if (program->bf_insns == NULL) {
842
    /* Failure */
843
0
    rc = PCAP_ERROR;
844
0
    goto quit;
845
0
  }
846
8.15k
  program->bf_len = len;
847
848
8.15k
  rc = 0;  /* We're all okay */
849
850
13.5k
quit:
851
  /*
852
   * Clean up everything for the lexical analyzer.
853
   */
854
13.5k
  if (in_buffer != NULL)
855
13.5k
    pcap__delete_buffer(in_buffer, scanner);
856
13.5k
  if (scanner != NULL)
857
13.5k
    pcap_lex_destroy(scanner);
858
859
  /*
860
   * Clean up our own allocated memory.
861
   */
862
13.5k
  freechunks(&cstate);
863
864
13.5k
  return (rc);
865
8.15k
}
866
867
/*
868
 * entry point for using the compiler with no pcap open
869
 * pass in all the stuff that is needed explicitly instead.
870
 */
871
int
872
pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
873
        struct bpf_program *program,
874
       const char *buf, int optimize, bpf_u_int32 mask)
875
0
{
876
0
  pcap_t *p;
877
0
  int ret;
878
879
0
  p = pcap_open_dead(linktype_arg, snaplen_arg);
880
0
  if (p == NULL)
881
0
    return (PCAP_ERROR);
882
0
  ret = pcap_compile(p, program, buf, optimize, mask);
883
0
  pcap_close(p);
884
0
  return (ret);
885
0
}
886
887
/*
888
 * Clean up a "struct bpf_program" by freeing all the memory allocated
889
 * in it.
890
 */
891
void
892
pcap_freecode(struct bpf_program *program)
893
21.6k
{
894
21.6k
  program->bf_len = 0;
895
21.6k
  if (program->bf_insns != NULL) {
896
8.15k
    free((char *)program->bf_insns);
897
8.15k
    program->bf_insns = NULL;
898
8.15k
  }
899
21.6k
}
900
901
/*
902
 * Backpatch the blocks in 'list' to 'target'.  The 'sense' field indicates
903
 * which of the jt and jf fields has been resolved and which is a pointer
904
 * back to another unresolved block (or nil).  At least one of the fields
905
 * in each block is already resolved.
906
 */
907
static void
908
backpatch(struct block *list, struct block *target)
909
300k
{
910
300k
  struct block *next;
911
912
863k
  while (list) {
913
563k
    if (!list->sense) {
914
281k
      next = JT(list);
915
281k
      JT(list) = target;
916
282k
    } else {
917
282k
      next = JF(list);
918
282k
      JF(list) = target;
919
282k
    }
920
563k
    list = next;
921
563k
  }
922
300k
}
923
924
/*
925
 * Merge the lists in b0 and b1, using the 'sense' field to indicate
926
 * which of jt and jf is the link.
927
 */
928
static void
929
merge(struct block *b0, struct block *b1)
930
285k
{
931
285k
  register struct block **p = &b0;
932
933
  /* Find end of list. */
934
785k
  while (*p)
935
499k
    p = !((*p)->sense) ? &JT(*p) : &JF(*p);
936
937
  /* Concatenate the lists. */
938
285k
  *p = b1;
939
285k
}
940
941
int
942
finish_parse(compiler_state_t *cstate, struct block *p)
943
7.23k
{
944
7.23k
  struct block *ppi_dlt_check;
945
946
  /*
947
   * Catch errors reported by us and routines below us, and return -1
948
   * on an error.
949
   */
950
7.23k
  if (setjmp(cstate->top_ctx))
951
1
    return (-1);
952
953
  /*
954
   * Insert before the statements of the first (root) block any
955
   * statements needed to load the lengths of any variable-length
956
   * headers into registers.
957
   *
958
   * XXX - a fancier strategy would be to insert those before the
959
   * statements of all blocks that use those lengths and that
960
   * have no predecessors that use them, so that we only compute
961
   * the lengths if we need them.  There might be even better
962
   * approaches than that.
963
   *
964
   * However, those strategies would be more complicated, and
965
   * as we don't generate code to compute a length if the
966
   * program has no tests that use the length, and as most
967
   * tests will probably use those lengths, we would just
968
   * postpone computing the lengths so that it's not done
969
   * for tests that fail early, and it's not clear that's
970
   * worth the effort.
971
   */
972
7.23k
  insert_compute_vloffsets(cstate, p->head);
973
974
  /*
975
   * For DLT_PPI captures, generate a check of the per-packet
976
   * DLT value to make sure it's DLT_IEEE802_11.
977
   *
978
   * XXX - TurboCap cards use DLT_PPI for Ethernet.
979
   * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
980
   * with appropriate Ethernet information and use that rather
981
   * than using something such as DLT_PPI where you don't know
982
   * the link-layer header type until runtime, which, in the
983
   * general case, would force us to generate both Ethernet *and*
984
   * 802.11 code (*and* anything else for which PPI is used)
985
   * and choose between them early in the BPF program?
986
   */
987
7.23k
  ppi_dlt_check = gen_ppi_dlt_check(cstate);
988
7.23k
  if (ppi_dlt_check != NULL)
989
191
    gen_and(ppi_dlt_check, p);
990
991
7.23k
  backpatch(p, gen_retblk(cstate, cstate->snaplen));
992
7.23k
  p->sense = !p->sense;
993
7.23k
  backpatch(p, gen_retblk(cstate, 0));
994
7.23k
  cstate->ic.root = p->head;
995
7.23k
  return (0);
996
7.23k
}
997
998
void
999
gen_and(struct block *b0, struct block *b1)
1000
178k
{
1001
178k
  backpatch(b0, b1->head);
1002
178k
  b0->sense = !b0->sense;
1003
178k
  b1->sense = !b1->sense;
1004
178k
  merge(b1, b0);
1005
178k
  b1->sense = !b1->sense;
1006
178k
  b1->head = b0->head;
1007
178k
}
1008
1009
void
1010
gen_or(struct block *b0, struct block *b1)
1011
107k
{
1012
107k
  b0->sense = !b0->sense;
1013
107k
  backpatch(b0, b1->head);
1014
107k
  b0->sense = !b0->sense;
1015
107k
  merge(b1, b0);
1016
107k
  b1->head = b0->head;
1017
107k
}
1018
1019
void
1020
gen_not(struct block *b)
1021
38.2k
{
1022
38.2k
  b->sense = !b->sense;
1023
38.2k
}
1024
1025
static struct block *
1026
gen_cmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1027
    u_int size, bpf_u_int32 v)
1028
156k
{
1029
156k
  return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
1030
156k
}
1031
1032
static struct block *
1033
gen_cmp_gt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1034
    u_int size, bpf_u_int32 v)
1035
2.04k
{
1036
2.04k
  return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
1037
2.04k
}
1038
1039
static struct block *
1040
gen_cmp_ge(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1041
    u_int size, bpf_u_int32 v)
1042
0
{
1043
0
  return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
1044
0
}
1045
1046
static struct block *
1047
gen_cmp_lt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1048
    u_int size, bpf_u_int32 v)
1049
39
{
1050
39
  return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
1051
39
}
1052
1053
static struct block *
1054
gen_cmp_le(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1055
    u_int size, bpf_u_int32 v)
1056
0
{
1057
0
  return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
1058
0
}
1059
1060
static struct block *
1061
gen_mcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1062
    u_int size, bpf_u_int32 v, bpf_u_int32 mask)
1063
67.6k
{
1064
67.6k
  return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 0, v);
1065
67.6k
}
1066
1067
static struct block *
1068
gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1069
    u_int size, const u_char *v)
1070
3.68k
{
1071
3.68k
  register struct block *b, *tmp;
1072
1073
3.68k
  b = NULL;
1074
7.42k
  while (size >= 4) {
1075
3.74k
    register const u_char *p = &v[size - 4];
1076
1077
3.74k
    tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W,
1078
3.74k
        EXTRACT_BE_U_4(p));
1079
3.74k
    if (b != NULL)
1080
456
      gen_and(b, tmp);
1081
3.74k
    b = tmp;
1082
3.74k
    size -= 4;
1083
3.74k
  }
1084
6.56k
  while (size >= 2) {
1085
2.88k
    register const u_char *p = &v[size - 2];
1086
1087
2.88k
    tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H,
1088
2.88k
        EXTRACT_BE_U_2(p));
1089
2.88k
    if (b != NULL)
1090
2.82k
      gen_and(b, tmp);
1091
2.88k
    b = tmp;
1092
2.88k
    size -= 2;
1093
2.88k
  }
1094
3.68k
  if (size > 0) {
1095
389
    tmp = gen_cmp(cstate, offrel, offset, BPF_B, v[0]);
1096
389
    if (b != NULL)
1097
56
      gen_and(b, tmp);
1098
389
    b = tmp;
1099
389
  }
1100
3.68k
  return b;
1101
3.68k
}
1102
1103
/*
1104
 * AND the field of size "size" at offset "offset" relative to the header
1105
 * specified by "offrel" with "mask", and compare it with the value "v"
1106
 * with the test specified by "jtype"; if "reverse" is true, the test
1107
 * should test the opposite of "jtype".
1108
 */
1109
static struct block *
1110
gen_ncmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1111
    u_int size, bpf_u_int32 mask, int jtype, int reverse,
1112
    bpf_u_int32 v)
1113
230k
{
1114
230k
  struct slist *s, *s2;
1115
230k
  struct block *b;
1116
1117
230k
  s = gen_load_a(cstate, offrel, offset, size);
1118
1119
230k
  if (mask != 0xffffffff) {
1120
48.2k
    s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
1121
48.2k
    s2->s.k = mask;
1122
48.2k
    sappend(s, s2);
1123
48.2k
  }
1124
1125
230k
  b = new_block(cstate, JMP(jtype));
1126
230k
  b->stmts = s;
1127
230k
  b->s.k = v;
1128
230k
  if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
1129
620
    gen_not(b);
1130
230k
  return b;
1131
230k
}
1132
1133
static int
1134
init_linktype(compiler_state_t *cstate, pcap_t *p)
1135
13.5k
{
1136
13.5k
  cstate->pcap_fddipad = p->fddipad;
1137
1138
  /*
1139
   * We start out with only one link-layer header.
1140
   */
1141
13.5k
  cstate->outermostlinktype = pcap_datalink(p);
1142
13.5k
  cstate->off_outermostlinkhdr.constant_part = 0;
1143
13.5k
  cstate->off_outermostlinkhdr.is_variable = 0;
1144
13.5k
  cstate->off_outermostlinkhdr.reg = -1;
1145
1146
13.5k
  cstate->prevlinktype = cstate->outermostlinktype;
1147
13.5k
  cstate->off_prevlinkhdr.constant_part = 0;
1148
13.5k
  cstate->off_prevlinkhdr.is_variable = 0;
1149
13.5k
  cstate->off_prevlinkhdr.reg = -1;
1150
1151
13.5k
  cstate->linktype = cstate->outermostlinktype;
1152
13.5k
  cstate->off_linkhdr.constant_part = 0;
1153
13.5k
  cstate->off_linkhdr.is_variable = 0;
1154
13.5k
  cstate->off_linkhdr.reg = -1;
1155
1156
  /*
1157
   * XXX
1158
   */
1159
13.5k
  cstate->off_linkpl.constant_part = 0;
1160
13.5k
  cstate->off_linkpl.is_variable = 0;
1161
13.5k
  cstate->off_linkpl.reg = -1;
1162
1163
13.5k
  cstate->off_linktype.constant_part = 0;
1164
13.5k
  cstate->off_linktype.is_variable = 0;
1165
13.5k
  cstate->off_linktype.reg = -1;
1166
1167
  /*
1168
   * Assume it's not raw ATM with a pseudo-header, for now.
1169
   */
1170
13.5k
  cstate->is_atm = 0;
1171
13.5k
  cstate->off_vpi = OFFSET_NOT_SET;
1172
13.5k
  cstate->off_vci = OFFSET_NOT_SET;
1173
13.5k
  cstate->off_proto = OFFSET_NOT_SET;
1174
13.5k
  cstate->off_payload = OFFSET_NOT_SET;
1175
1176
  /*
1177
   * And not Geneve.
1178
   */
1179
13.5k
  cstate->is_geneve = 0;
1180
1181
  /*
1182
   * No variable length VLAN offset by default
1183
   */
1184
13.5k
  cstate->is_vlan_vloffset = 0;
1185
1186
  /*
1187
   * And assume we're not doing SS7.
1188
   */
1189
13.5k
  cstate->off_li = OFFSET_NOT_SET;
1190
13.5k
  cstate->off_li_hsl = OFFSET_NOT_SET;
1191
13.5k
  cstate->off_sio = OFFSET_NOT_SET;
1192
13.5k
  cstate->off_opc = OFFSET_NOT_SET;
1193
13.5k
  cstate->off_dpc = OFFSET_NOT_SET;
1194
13.5k
  cstate->off_sls = OFFSET_NOT_SET;
1195
1196
13.5k
  cstate->label_stack_depth = 0;
1197
13.5k
  cstate->vlan_stack_depth = 0;
1198
1199
13.5k
  switch (cstate->linktype) {
1200
1201
233
  case DLT_ARCNET:
1202
233
    cstate->off_linktype.constant_part = 2;
1203
233
    cstate->off_linkpl.constant_part = 6;
1204
233
    cstate->off_nl = 0;   /* XXX in reality, variable! */
1205
233
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1206
233
    break;
1207
1208
107
  case DLT_ARCNET_LINUX:
1209
107
    cstate->off_linktype.constant_part = 4;
1210
107
    cstate->off_linkpl.constant_part = 8;
1211
107
    cstate->off_nl = 0;   /* XXX in reality, variable! */
1212
107
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1213
107
    break;
1214
1215
666
  case DLT_EN10MB:
1216
666
    cstate->off_linktype.constant_part = 12;
1217
666
    cstate->off_linkpl.constant_part = 14;  /* Ethernet header length */
1218
666
    cstate->off_nl = 0;   /* Ethernet II */
1219
666
    cstate->off_nl_nosnap = 3;  /* 802.3+802.2 */
1220
666
    break;
1221
1222
142
  case DLT_SLIP:
1223
    /*
1224
     * SLIP doesn't have a link level type.  The 16 byte
1225
     * header is hacked into our SLIP driver.
1226
     */
1227
142
    cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1228
142
    cstate->off_linkpl.constant_part = 16;
1229
142
    cstate->off_nl = 0;
1230
142
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1231
142
    break;
1232
1233
91
  case DLT_SLIP_BSDOS:
1234
    /* XXX this may be the same as the DLT_PPP_BSDOS case */
1235
91
    cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1236
    /* XXX end */
1237
91
    cstate->off_linkpl.constant_part = 24;
1238
91
    cstate->off_nl = 0;
1239
91
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1240
91
    break;
1241
1242
2.92k
  case DLT_NULL:
1243
2.99k
  case DLT_LOOP:
1244
2.99k
    cstate->off_linktype.constant_part = 0;
1245
2.99k
    cstate->off_linkpl.constant_part = 4;
1246
2.99k
    cstate->off_nl = 0;
1247
2.99k
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1248
2.99k
    break;
1249
1250
61
  case DLT_ENC:
1251
61
    cstate->off_linktype.constant_part = 0;
1252
61
    cstate->off_linkpl.constant_part = 12;
1253
61
    cstate->off_nl = 0;
1254
61
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1255
61
    break;
1256
1257
137
  case DLT_PPP:
1258
184
  case DLT_PPP_PPPD:
1259
258
  case DLT_C_HDLC:   /* BSD/OS Cisco HDLC */
1260
410
  case DLT_HDLC:     /* NetBSD (Cisco) HDLC */
1261
599
  case DLT_PPP_SERIAL:   /* NetBSD sync/async serial PPP */
1262
599
    cstate->off_linktype.constant_part = 2; /* skip HDLC-like framing */
1263
599
    cstate->off_linkpl.constant_part = 4; /* skip HDLC-like framing and protocol field */
1264
599
    cstate->off_nl = 0;
1265
599
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1266
599
    break;
1267
1268
112
  case DLT_PPP_ETHER:
1269
    /*
1270
     * This does no include the Ethernet header, and
1271
     * only covers session state.
1272
     */
1273
112
    cstate->off_linktype.constant_part = 6;
1274
112
    cstate->off_linkpl.constant_part = 8;
1275
112
    cstate->off_nl = 0;
1276
112
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1277
112
    break;
1278
1279
411
  case DLT_PPP_BSDOS:
1280
411
    cstate->off_linktype.constant_part = 5;
1281
411
    cstate->off_linkpl.constant_part = 24;
1282
411
    cstate->off_nl = 0;
1283
411
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1284
411
    break;
1285
1286
105
  case DLT_FDDI:
1287
    /*
1288
     * FDDI doesn't really have a link-level type field.
1289
     * We set "off_linktype" to the offset of the LLC header.
1290
     *
1291
     * To check for Ethernet types, we assume that SSAP = SNAP
1292
     * is being used and pick out the encapsulated Ethernet type.
1293
     * XXX - should we generate code to check for SNAP?
1294
     */
1295
105
    cstate->off_linktype.constant_part = 13;
1296
105
    cstate->off_linktype.constant_part += cstate->pcap_fddipad;
1297
105
    cstate->off_linkpl.constant_part = 13;  /* FDDI MAC header length */
1298
105
    cstate->off_linkpl.constant_part += cstate->pcap_fddipad;
1299
105
    cstate->off_nl = 8;   /* 802.2+SNAP */
1300
105
    cstate->off_nl_nosnap = 3;  /* 802.2 */
1301
105
    break;
1302
1303
130
  case DLT_IEEE802:
1304
    /*
1305
     * Token Ring doesn't really have a link-level type field.
1306
     * We set "off_linktype" to the offset of the LLC header.
1307
     *
1308
     * To check for Ethernet types, we assume that SSAP = SNAP
1309
     * is being used and pick out the encapsulated Ethernet type.
1310
     * XXX - should we generate code to check for SNAP?
1311
     *
1312
     * XXX - the header is actually variable-length.
1313
     * Some various Linux patched versions gave 38
1314
     * as "off_linktype" and 40 as "off_nl"; however,
1315
     * if a token ring packet has *no* routing
1316
     * information, i.e. is not source-routed, the correct
1317
     * values are 20 and 22, as they are in the vanilla code.
1318
     *
1319
     * A packet is source-routed iff the uppermost bit
1320
     * of the first byte of the source address, at an
1321
     * offset of 8, has the uppermost bit set.  If the
1322
     * packet is source-routed, the total number of bytes
1323
     * of routing information is 2 plus bits 0x1F00 of
1324
     * the 16-bit value at an offset of 14 (shifted right
1325
     * 8 - figure out which byte that is).
1326
     */
1327
130
    cstate->off_linktype.constant_part = 14;
1328
130
    cstate->off_linkpl.constant_part = 14;  /* Token Ring MAC header length */
1329
130
    cstate->off_nl = 8;   /* 802.2+SNAP */
1330
130
    cstate->off_nl_nosnap = 3;  /* 802.2 */
1331
130
    break;
1332
1333
308
  case DLT_PRISM_HEADER:
1334
483
  case DLT_IEEE802_11_RADIO_AVS:
1335
752
  case DLT_IEEE802_11_RADIO:
1336
752
    cstate->off_linkhdr.is_variable = 1;
1337
    /* Fall through, 802.11 doesn't have a variable link
1338
     * prefix but is otherwise the same. */
1339
    /* FALLTHROUGH */
1340
1341
1.08k
  case DLT_IEEE802_11:
1342
    /*
1343
     * 802.11 doesn't really have a link-level type field.
1344
     * We set "off_linktype.constant_part" to the offset of
1345
     * the LLC header.
1346
     *
1347
     * To check for Ethernet types, we assume that SSAP = SNAP
1348
     * is being used and pick out the encapsulated Ethernet type.
1349
     * XXX - should we generate code to check for SNAP?
1350
     *
1351
     * We also handle variable-length radio headers here.
1352
     * The Prism header is in theory variable-length, but in
1353
     * practice it's always 144 bytes long.  However, some
1354
     * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1355
     * sometimes or always supply an AVS header, so we
1356
     * have to check whether the radio header is a Prism
1357
     * header or an AVS header, so, in practice, it's
1358
     * variable-length.
1359
     */
1360
1.08k
    cstate->off_linktype.constant_part = 24;
1361
1.08k
    cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */
1362
1.08k
    cstate->off_linkpl.is_variable = 1;
1363
1.08k
    cstate->off_nl = 8;   /* 802.2+SNAP */
1364
1.08k
    cstate->off_nl_nosnap = 3;  /* 802.2 */
1365
1.08k
    break;
1366
1367
243
  case DLT_PPI:
1368
    /*
1369
     * At the moment we treat PPI the same way that we treat
1370
     * normal Radiotap encoded packets. The difference is in
1371
     * the function that generates the code at the beginning
1372
     * to compute the header length.  Since this code generator
1373
     * of PPI supports bare 802.11 encapsulation only (i.e.
1374
     * the encapsulated DLT should be DLT_IEEE802_11) we
1375
     * generate code to check for this too.
1376
     */
1377
243
    cstate->off_linktype.constant_part = 24;
1378
243
    cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */
1379
243
    cstate->off_linkpl.is_variable = 1;
1380
243
    cstate->off_linkhdr.is_variable = 1;
1381
243
    cstate->off_nl = 8;   /* 802.2+SNAP */
1382
243
    cstate->off_nl_nosnap = 3;  /* 802.2 */
1383
243
    break;
1384
1385
85
  case DLT_ATM_RFC1483:
1386
145
  case DLT_ATM_CLIP: /* Linux ATM defines this */
1387
    /*
1388
     * assume routed, non-ISO PDUs
1389
     * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1390
     *
1391
     * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1392
     * or PPP with the PPP NLPID (e.g., PPPoA)?  The
1393
     * latter would presumably be treated the way PPPoE
1394
     * should be, so you can do "pppoe and udp port 2049"
1395
     * or "pppoa and tcp port 80" and have it check for
1396
     * PPPo{A,E} and a PPP protocol of IP and....
1397
     */
1398
145
    cstate->off_linktype.constant_part = 0;
1399
145
    cstate->off_linkpl.constant_part = 0; /* packet begins with LLC header */
1400
145
    cstate->off_nl = 8;   /* 802.2+SNAP */
1401
145
    cstate->off_nl_nosnap = 3;  /* 802.2 */
1402
145
    break;
1403
1404
241
  case DLT_SUNATM:
1405
    /*
1406
     * Full Frontal ATM; you get AALn PDUs with an ATM
1407
     * pseudo-header.
1408
     */
1409
241
    cstate->is_atm = 1;
1410
241
    cstate->off_vpi = SUNATM_VPI_POS;
1411
241
    cstate->off_vci = SUNATM_VCI_POS;
1412
241
    cstate->off_proto = PROTO_POS;
1413
241
    cstate->off_payload = SUNATM_PKT_BEGIN_POS;
1414
241
    cstate->off_linktype.constant_part = cstate->off_payload;
1415
241
    cstate->off_linkpl.constant_part = cstate->off_payload; /* if LLC-encapsulated */
1416
241
    cstate->off_nl = 8;   /* 802.2+SNAP */
1417
241
    cstate->off_nl_nosnap = 3;  /* 802.2 */
1418
241
    break;
1419
1420
177
  case DLT_RAW:
1421
424
  case DLT_IPV4:
1422
528
  case DLT_IPV6:
1423
528
    cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1424
528
    cstate->off_linkpl.constant_part = 0;
1425
528
    cstate->off_nl = 0;
1426
528
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1427
528
    break;
1428
1429
408
  case DLT_LINUX_SLL: /* fake header for Linux cooked socket v1 */
1430
408
    cstate->off_linktype.constant_part = 14;
1431
408
    cstate->off_linkpl.constant_part = 16;
1432
408
    cstate->off_nl = 0;
1433
408
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1434
408
    break;
1435
1436
159
  case DLT_LINUX_SLL2: /* fake header for Linux cooked socket v2 */
1437
159
    cstate->off_linktype.constant_part = 0;
1438
159
    cstate->off_linkpl.constant_part = 20;
1439
159
    cstate->off_nl = 0;
1440
159
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1441
159
    break;
1442
1443
107
  case DLT_LTALK:
1444
    /*
1445
     * LocalTalk does have a 1-byte type field in the LLAP header,
1446
     * but really it just indicates whether there is a "short" or
1447
     * "long" DDP packet following.
1448
     */
1449
107
    cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1450
107
    cstate->off_linkpl.constant_part = 0;
1451
107
    cstate->off_nl = 0;
1452
107
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1453
107
    break;
1454
1455
98
  case DLT_IP_OVER_FC:
1456
    /*
1457
     * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1458
     * link-level type field.  We set "off_linktype" to the
1459
     * offset of the LLC header.
1460
     *
1461
     * To check for Ethernet types, we assume that SSAP = SNAP
1462
     * is being used and pick out the encapsulated Ethernet type.
1463
     * XXX - should we generate code to check for SNAP? RFC
1464
     * 2625 says SNAP should be used.
1465
     */
1466
98
    cstate->off_linktype.constant_part = 16;
1467
98
    cstate->off_linkpl.constant_part = 16;
1468
98
    cstate->off_nl = 8;   /* 802.2+SNAP */
1469
98
    cstate->off_nl_nosnap = 3;  /* 802.2 */
1470
98
    break;
1471
1472
177
  case DLT_FRELAY:
1473
    /*
1474
     * XXX - we should set this to handle SNAP-encapsulated
1475
     * frames (NLPID of 0x80).
1476
     */
1477
177
    cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1478
177
    cstate->off_linkpl.constant_part = 0;
1479
177
    cstate->off_nl = 0;
1480
177
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1481
177
    break;
1482
1483
                /*
1484
                 * the only BPF-interesting FRF.16 frames are non-control frames;
1485
                 * Frame Relay has a variable length link-layer
1486
                 * so lets start with offset 4 for now and increments later on (FIXME);
1487
                 */
1488
22
  case DLT_MFR:
1489
22
    cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1490
22
    cstate->off_linkpl.constant_part = 0;
1491
22
    cstate->off_nl = 4;
1492
22
    cstate->off_nl_nosnap = 0;  /* XXX - for now -> no 802.2 LLC */
1493
22
    break;
1494
1495
29
  case DLT_APPLE_IP_OVER_IEEE1394:
1496
29
    cstate->off_linktype.constant_part = 16;
1497
29
    cstate->off_linkpl.constant_part = 18;
1498
29
    cstate->off_nl = 0;
1499
29
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1500
29
    break;
1501
1502
51
  case DLT_SYMANTEC_FIREWALL:
1503
51
    cstate->off_linktype.constant_part = 6;
1504
51
    cstate->off_linkpl.constant_part = 44;
1505
51
    cstate->off_nl = 0;   /* Ethernet II */
1506
51
    cstate->off_nl_nosnap = 0;  /* XXX - what does it do with 802.3 packets? */
1507
51
    break;
1508
1509
934
  case DLT_PFLOG:
1510
934
    cstate->off_linktype.constant_part = 0;
1511
934
    cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */
1512
934
    cstate->off_linkpl.is_variable = 1;
1513
934
    cstate->off_nl = 0;
1514
934
    cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
1515
934
    break;
1516
1517
24
        case DLT_JUNIPER_MFR:
1518
52
        case DLT_JUNIPER_MLFR:
1519
80
        case DLT_JUNIPER_MLPPP:
1520
104
        case DLT_JUNIPER_PPP:
1521
128
        case DLT_JUNIPER_CHDLC:
1522
150
        case DLT_JUNIPER_FRELAY:
1523
150
    cstate->off_linktype.constant_part = 4;
1524
150
    cstate->off_linkpl.constant_part = 4;
1525
150
    cstate->off_nl = 0;
1526
150
    cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1527
150
                break;
1528
1529
29
  case DLT_JUNIPER_ATM1:
1530
29
    cstate->off_linktype.constant_part = 4;   /* in reality variable between 4-8 */
1531
29
    cstate->off_linkpl.constant_part = 4; /* in reality variable between 4-8 */
1532
29
    cstate->off_nl = 0;
1533
29
    cstate->off_nl_nosnap = 10;
1534
29
    break;
1535
1536
26
  case DLT_JUNIPER_ATM2:
1537
26
    cstate->off_linktype.constant_part = 8;   /* in reality variable between 8-12 */
1538
26
    cstate->off_linkpl.constant_part = 8; /* in reality variable between 8-12 */
1539
26
    cstate->off_nl = 0;
1540
26
    cstate->off_nl_nosnap = 10;
1541
26
    break;
1542
1543
    /* frames captured on a Juniper PPPoE service PIC
1544
     * contain raw ethernet frames */
1545
21
  case DLT_JUNIPER_PPPOE:
1546
92
        case DLT_JUNIPER_ETHER:
1547
92
    cstate->off_linkpl.constant_part = 14;
1548
92
    cstate->off_linktype.constant_part = 16;
1549
92
    cstate->off_nl = 18;    /* Ethernet II */
1550
92
    cstate->off_nl_nosnap = 21; /* 802.3+802.2 */
1551
92
    break;
1552
1553
14
  case DLT_JUNIPER_PPPOE_ATM:
1554
14
    cstate->off_linktype.constant_part = 4;
1555
14
    cstate->off_linkpl.constant_part = 6;
1556
14
    cstate->off_nl = 0;
1557
14
    cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1558
14
    break;
1559
1560
25
  case DLT_JUNIPER_GGSN:
1561
25
    cstate->off_linktype.constant_part = 6;
1562
25
    cstate->off_linkpl.constant_part = 12;
1563
25
    cstate->off_nl = 0;
1564
25
    cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1565
25
    break;
1566
1567
35
  case DLT_JUNIPER_ES:
1568
35
    cstate->off_linktype.constant_part = 6;
1569
35
    cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */
1570
35
    cstate->off_nl = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */
1571
35
    cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1572
35
    break;
1573
1574
25
  case DLT_JUNIPER_MONITOR:
1575
25
    cstate->off_linktype.constant_part = 12;
1576
25
    cstate->off_linkpl.constant_part = 12;
1577
25
    cstate->off_nl = 0;     /* raw IP/IP6 header */
1578
25
    cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1579
25
    break;
1580
1581
41
  case DLT_BACNET_MS_TP:
1582
41
    cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1583
41
    cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1584
41
    cstate->off_nl = OFFSET_NOT_SET;
1585
41
    cstate->off_nl_nosnap = OFFSET_NOT_SET;
1586
41
    break;
1587
1588
26
  case DLT_JUNIPER_SERVICES:
1589
26
    cstate->off_linktype.constant_part = 12;
1590
26
    cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */
1591
26
    cstate->off_nl = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */
1592
26
    cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1593
26
    break;
1594
1595
23
  case DLT_JUNIPER_VP:
1596
23
    cstate->off_linktype.constant_part = 18;
1597
23
    cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1598
23
    cstate->off_nl = OFFSET_NOT_SET;
1599
23
    cstate->off_nl_nosnap = OFFSET_NOT_SET;
1600
23
    break;
1601
1602
26
  case DLT_JUNIPER_ST:
1603
26
    cstate->off_linktype.constant_part = 18;
1604
26
    cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1605
26
    cstate->off_nl = OFFSET_NOT_SET;
1606
26
    cstate->off_nl_nosnap = OFFSET_NOT_SET;
1607
26
    break;
1608
1609
24
  case DLT_JUNIPER_ISM:
1610
24
    cstate->off_linktype.constant_part = 8;
1611
24
    cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1612
24
    cstate->off_nl = OFFSET_NOT_SET;
1613
24
    cstate->off_nl_nosnap = OFFSET_NOT_SET;
1614
24
    break;
1615
1616
17
  case DLT_JUNIPER_VS:
1617
42
  case DLT_JUNIPER_SRX_E2E:
1618
72
  case DLT_JUNIPER_FIBRECHANNEL:
1619
100
  case DLT_JUNIPER_ATM_CEMIC:
1620
100
    cstate->off_linktype.constant_part = 8;
1621
100
    cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1622
100
    cstate->off_nl = OFFSET_NOT_SET;
1623
100
    cstate->off_nl_nosnap = OFFSET_NOT_SET;
1624
100
    break;
1625
1626
118
  case DLT_MTP2:
1627
118
    cstate->off_li = 2;
1628
118
    cstate->off_li_hsl = 4;
1629
118
    cstate->off_sio = 3;
1630
118
    cstate->off_opc = 4;
1631
118
    cstate->off_dpc = 4;
1632
118
    cstate->off_sls = 7;
1633
118
    cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1634
118
    cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1635
118
    cstate->off_nl = OFFSET_NOT_SET;
1636
118
    cstate->off_nl_nosnap = OFFSET_NOT_SET;
1637
118
    break;
1638
1639
84
  case DLT_MTP2_WITH_PHDR:
1640
84
    cstate->off_li = 6;
1641
84
    cstate->off_li_hsl = 8;
1642
84
    cstate->off_sio = 7;
1643
84
    cstate->off_opc = 8;
1644
84
    cstate->off_dpc = 8;
1645
84
    cstate->off_sls = 11;
1646
84
    cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1647
84
    cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1648
84
    cstate->off_nl = OFFSET_NOT_SET;
1649
84
    cstate->off_nl_nosnap = OFFSET_NOT_SET;
1650
84
    break;
1651
1652
238
  case DLT_ERF:
1653
238
    cstate->off_li = 22;
1654
238
    cstate->off_li_hsl = 24;
1655
238
    cstate->off_sio = 23;
1656
238
    cstate->off_opc = 24;
1657
238
    cstate->off_dpc = 24;
1658
238
    cstate->off_sls = 27;
1659
238
    cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1660
238
    cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1661
238
    cstate->off_nl = OFFSET_NOT_SET;
1662
238
    cstate->off_nl_nosnap = OFFSET_NOT_SET;
1663
238
    break;
1664
1665
43
  case DLT_PFSYNC:
1666
43
    cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1667
43
    cstate->off_linkpl.constant_part = 4;
1668
43
    cstate->off_nl = 0;
1669
43
    cstate->off_nl_nosnap = 0;
1670
43
    break;
1671
1672
8
  case DLT_AX25_KISS:
1673
    /*
1674
     * Currently, only raw "link[N:M]" filtering is supported.
1675
     */
1676
8
    cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */
1677
8
    cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1678
8
    cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */
1679
8
    cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1680
8
    break;
1681
1682
127
  case DLT_IPNET:
1683
127
    cstate->off_linktype.constant_part = 1;
1684
127
    cstate->off_linkpl.constant_part = 24;  /* ipnet header length */
1685
127
    cstate->off_nl = 0;
1686
127
    cstate->off_nl_nosnap = OFFSET_NOT_SET;
1687
127
    break;
1688
1689
120
  case DLT_NETANALYZER:
1690
120
    cstate->off_linkhdr.constant_part = 4;  /* Ethernet header is past 4-byte pseudo-header */
1691
120
    cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
1692
120
    cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14;  /* pseudo-header+Ethernet header length */
1693
120
    cstate->off_nl = 0;   /* Ethernet II */
1694
120
    cstate->off_nl_nosnap = 3;  /* 802.3+802.2 */
1695
120
    break;
1696
1697
116
  case DLT_NETANALYZER_TRANSPARENT:
1698
116
    cstate->off_linkhdr.constant_part = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1699
116
    cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
1700
116
    cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14;  /* pseudo-header+preamble+SFD+Ethernet header length */
1701
116
    cstate->off_nl = 0;   /* Ethernet II */
1702
116
    cstate->off_nl_nosnap = 3;  /* 802.3+802.2 */
1703
116
    break;
1704
1705
2.15k
  default:
1706
    /*
1707
     * For values in the range in which we've assigned new
1708
     * DLT_ values, only raw "link[N:M]" filtering is supported.
1709
     */
1710
2.15k
    if (cstate->linktype >= DLT_MATCHING_MIN &&
1711
2.15k
        cstate->linktype <= DLT_MATCHING_MAX) {
1712
2.06k
      cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1713
2.06k
      cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1714
2.06k
      cstate->off_nl = OFFSET_NOT_SET;
1715
2.06k
      cstate->off_nl_nosnap = OFFSET_NOT_SET;
1716
2.06k
    } else {
1717
82
      bpf_set_error(cstate, "unknown data link type %d (min %d, max %d)",
1718
82
          cstate->linktype, DLT_MATCHING_MIN, DLT_MATCHING_MAX);
1719
82
      return (-1);
1720
82
    }
1721
2.06k
    break;
1722
13.5k
  }
1723
1724
13.4k
  cstate->off_outermostlinkhdr = cstate->off_prevlinkhdr = cstate->off_linkhdr;
1725
13.4k
  return (0);
1726
13.5k
}
1727
1728
/*
1729
 * Load a value relative to the specified absolute offset.
1730
 */
1731
static struct slist *
1732
gen_load_absoffsetrel(compiler_state_t *cstate, bpf_abs_offset *abs_offset,
1733
    u_int offset, u_int size)
1734
258k
{
1735
258k
  struct slist *s, *s2;
1736
1737
258k
  s = gen_abs_offset_varpart(cstate, abs_offset);
1738
1739
  /*
1740
   * If "s" is non-null, it has code to arrange that the X register
1741
   * contains the variable part of the absolute offset, so we
1742
   * generate a load relative to that, with an offset of
1743
   * abs_offset->constant_part + offset.
1744
   *
1745
   * Otherwise, we can do an absolute load with an offset of
1746
   * abs_offset->constant_part + offset.
1747
   */
1748
258k
  if (s != NULL) {
1749
    /*
1750
     * "s" points to a list of statements that puts the
1751
     * variable part of the absolute offset into the X register.
1752
     * Do an indirect load, to use the X register as an offset.
1753
     */
1754
88.8k
    s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);
1755
88.8k
    s2->s.k = abs_offset->constant_part + offset;
1756
88.8k
    sappend(s, s2);
1757
169k
  } else {
1758
    /*
1759
     * There is no variable part of the absolute offset, so
1760
     * just do an absolute load.
1761
     */
1762
169k
    s = new_stmt(cstate, BPF_LD|BPF_ABS|size);
1763
169k
    s->s.k = abs_offset->constant_part + offset;
1764
169k
  }
1765
258k
  return s;
1766
258k
}
1767
1768
/*
1769
 * Load a value relative to the beginning of the specified header.
1770
 */
1771
static struct slist *
1772
gen_load_a(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1773
    u_int size)
1774
278k
{
1775
278k
  struct slist *s, *s2;
1776
1777
  /*
1778
   * Squelch warnings from compilers that *don't* assume that
1779
   * offrel always has a valid enum value and therefore don't
1780
   * assume that we'll always go through one of the case arms.
1781
   *
1782
   * If we have a default case, compilers that *do* assume that
1783
   * will then complain about the default case code being
1784
   * unreachable.
1785
   *
1786
   * Damned if you do, damned if you don't.
1787
   */
1788
278k
  s = NULL;
1789
1790
278k
  switch (offrel) {
1791
1792
2.19k
  case OR_PACKET:
1793
2.19k
                s = new_stmt(cstate, BPF_LD|BPF_ABS|size);
1794
2.19k
                s->s.k = offset;
1795
2.19k
    break;
1796
1797
70.0k
  case OR_LINKHDR:
1798
70.0k
    s = gen_load_absoffsetrel(cstate, &cstate->off_linkhdr, offset, size);
1799
70.0k
    break;
1800
1801
937
  case OR_PREVLINKHDR:
1802
937
    s = gen_load_absoffsetrel(cstate, &cstate->off_prevlinkhdr, offset, size);
1803
937
    break;
1804
1805
24.4k
  case OR_LLC:
1806
24.4k
    s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, offset, size);
1807
24.4k
    break;
1808
1809
156
  case OR_PREVMPLSHDR:
1810
156
    s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl - 4 + offset, size);
1811
156
    break;
1812
1813
102k
  case OR_LINKPL:
1814
102k
    s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + offset, size);
1815
102k
    break;
1816
1817
11.8k
  case OR_LINKPL_NOSNAP:
1818
11.8k
    s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl_nosnap + offset, size);
1819
11.8k
    break;
1820
1821
29.6k
  case OR_LINKTYPE:
1822
29.6k
    s = gen_load_absoffsetrel(cstate, &cstate->off_linktype, offset, size);
1823
29.6k
    break;
1824
1825
18.3k
  case OR_TRAN_IPV4:
1826
    /*
1827
     * Load the X register with the length of the IPv4 header
1828
     * (plus the offset of the link-layer header, if it's
1829
     * preceded by a variable-length header such as a radio
1830
     * header), in bytes.
1831
     */
1832
18.3k
    s = gen_loadx_iphdrlen(cstate);
1833
1834
    /*
1835
     * Load the item at {offset of the link-layer payload} +
1836
     * {offset, relative to the start of the link-layer
1837
     * paylod, of the IPv4 header} + {length of the IPv4 header} +
1838
     * {specified offset}.
1839
     *
1840
     * If the offset of the link-layer payload is variable,
1841
     * the variable part of that offset is included in the
1842
     * value in the X register, and we include the constant
1843
     * part in the offset of the load.
1844
     */
1845
18.3k
    s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);
1846
18.3k
    s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + offset;
1847
18.3k
    sappend(s, s2);
1848
18.3k
    break;
1849
1850
18.3k
  case OR_TRAN_IPV6:
1851
18.3k
    s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + 40 + offset, size);
1852
18.3k
    break;
1853
278k
  }
1854
278k
  return s;
1855
278k
}
1856
1857
/*
1858
 * Generate code to load into the X register the sum of the length of
1859
 * the IPv4 header and the variable part of the offset of the link-layer
1860
 * payload.
1861
 */
1862
static struct slist *
1863
gen_loadx_iphdrlen(compiler_state_t *cstate)
1864
19.7k
{
1865
19.7k
  struct slist *s, *s2;
1866
1867
19.7k
  s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
1868
19.7k
  if (s != NULL) {
1869
    /*
1870
     * The offset of the link-layer payload has a variable
1871
     * part.  "s" points to a list of statements that put
1872
     * the variable part of that offset into the X register.
1873
     *
1874
     * The 4*([k]&0xf) addressing mode can't be used, as we
1875
     * don't have a constant offset, so we have to load the
1876
     * value in question into the A register and add to it
1877
     * the value from the X register.
1878
     */
1879
7.80k
    s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
1880
7.80k
    s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
1881
7.80k
    sappend(s, s2);
1882
7.80k
    s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
1883
7.80k
    s2->s.k = 0xf;
1884
7.80k
    sappend(s, s2);
1885
7.80k
    s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
1886
7.80k
    s2->s.k = 2;
1887
7.80k
    sappend(s, s2);
1888
1889
    /*
1890
     * The A register now contains the length of the IP header.
1891
     * We need to add to it the variable part of the offset of
1892
     * the link-layer payload, which is still in the X
1893
     * register, and move the result into the X register.
1894
     */
1895
7.80k
    sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
1896
7.80k
    sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
1897
11.9k
  } else {
1898
    /*
1899
     * The offset of the link-layer payload is a constant,
1900
     * so no code was generated to load the (non-existent)
1901
     * variable part of that offset.
1902
     *
1903
     * This means we can use the 4*([k]&0xf) addressing
1904
     * mode.  Load the length of the IPv4 header, which
1905
     * is at an offset of cstate->off_nl from the beginning of
1906
     * the link-layer payload, and thus at an offset of
1907
     * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1908
     * of the raw packet data, using that addressing mode.
1909
     */
1910
11.9k
    s = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
1911
11.9k
    s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
1912
11.9k
  }
1913
19.7k
  return s;
1914
19.7k
}
1915
1916
1917
static struct block *
1918
gen_uncond(compiler_state_t *cstate, int rsense)
1919
11.1k
{
1920
11.1k
  struct block *b;
1921
11.1k
  struct slist *s;
1922
1923
11.1k
  s = new_stmt(cstate, BPF_LD|BPF_IMM);
1924
11.1k
  s->s.k = !rsense;
1925
11.1k
  b = new_block(cstate, JMP(BPF_JEQ));
1926
11.1k
  b->stmts = s;
1927
1928
11.1k
  return b;
1929
11.1k
}
1930
1931
static inline struct block *
1932
gen_true(compiler_state_t *cstate)
1933
1.08k
{
1934
1.08k
  return gen_uncond(cstate, 1);
1935
1.08k
}
1936
1937
static inline struct block *
1938
gen_false(compiler_state_t *cstate)
1939
10.0k
{
1940
10.0k
  return gen_uncond(cstate, 0);
1941
10.0k
}
1942
1943
/*
1944
 * Byte-swap a 32-bit number.
1945
 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1946
 * big-endian platforms.)
1947
 */
1948
2.53k
#define SWAPLONG(y) \
1949
2.53k
((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1950
1951
/*
1952
 * Generate code to match a particular packet type.
1953
 *
1954
 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1955
 * value, if <= ETHERMTU.  We use that to determine whether to
1956
 * match the type/length field or to check the type/length field for
1957
 * a value <= ETHERMTU to see whether it's a type field and then do
1958
 * the appropriate test.
1959
 */
1960
static struct block *
1961
gen_ether_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
1962
4.81k
{
1963
4.81k
  struct block *b0, *b1;
1964
1965
4.81k
  switch (ll_proto) {
1966
1967
1.27k
  case LLCSAP_ISONS:
1968
1.33k
  case LLCSAP_IP:
1969
1.37k
  case LLCSAP_NETBEUI:
1970
    /*
1971
     * OSI protocols and NetBEUI always use 802.2 encapsulation,
1972
     * so we check the DSAP and SSAP.
1973
     *
1974
     * LLCSAP_IP checks for IP-over-802.2, rather
1975
     * than IP-over-Ethernet or IP-over-SNAP.
1976
     *
1977
     * XXX - should we check both the DSAP and the
1978
     * SSAP, like this, or should we check just the
1979
     * DSAP, as we do for other types <= ETHERMTU
1980
     * (i.e., other SAP values)?
1981
     */
1982
1.37k
    b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
1983
1.37k
    gen_not(b0);
1984
1.37k
    b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto);
1985
1.37k
    gen_and(b0, b1);
1986
1.37k
    return b1;
1987
1988
132
  case LLCSAP_IPX:
1989
    /*
1990
     * Check for;
1991
     *
1992
     *  Ethernet_II frames, which are Ethernet
1993
     *  frames with a frame type of ETHERTYPE_IPX;
1994
     *
1995
     *  Ethernet_802.3 frames, which are 802.3
1996
     *  frames (i.e., the type/length field is
1997
     *  a length field, <= ETHERMTU, rather than
1998
     *  a type field) with the first two bytes
1999
     *  after the Ethernet/802.3 header being
2000
     *  0xFFFF;
2001
     *
2002
     *  Ethernet_802.2 frames, which are 802.3
2003
     *  frames with an 802.2 LLC header and
2004
     *  with the IPX LSAP as the DSAP in the LLC
2005
     *  header;
2006
     *
2007
     *  Ethernet_SNAP frames, which are 802.3
2008
     *  frames with an LLC header and a SNAP
2009
     *  header and with an OUI of 0x000000
2010
     *  (encapsulated Ethernet) and a protocol
2011
     *  ID of ETHERTYPE_IPX in the SNAP header.
2012
     *
2013
     * XXX - should we generate the same code both
2014
     * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2015
     */
2016
2017
    /*
2018
     * This generates code to check both for the
2019
     * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2020
     */
2021
132
    b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
2022
132
    b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF);
2023
132
    gen_or(b0, b1);
2024
2025
    /*
2026
     * Now we add code to check for SNAP frames with
2027
     * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2028
     */
2029
132
    b0 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);
2030
132
    gen_or(b0, b1);
2031
2032
    /*
2033
     * Now we generate code to check for 802.3
2034
     * frames in general.
2035
     */
2036
132
    b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
2037
132
    gen_not(b0);
2038
2039
    /*
2040
     * Now add the check for 802.3 frames before the
2041
     * check for Ethernet_802.2 and Ethernet_802.3,
2042
     * as those checks should only be done on 802.3
2043
     * frames, not on Ethernet frames.
2044
     */
2045
132
    gen_and(b0, b1);
2046
2047
    /*
2048
     * Now add the check for Ethernet_II frames, and
2049
     * do that before checking for the other frame
2050
     * types.
2051
     */
2052
132
    b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX);
2053
132
    gen_or(b0, b1);
2054
132
    return b1;
2055
2056
39
  case ETHERTYPE_ATALK:
2057
81
  case ETHERTYPE_AARP:
2058
    /*
2059
     * EtherTalk (AppleTalk protocols on Ethernet link
2060
     * layer) may use 802.2 encapsulation.
2061
     */
2062
2063
    /*
2064
     * Check for 802.2 encapsulation (EtherTalk phase 2?);
2065
     * we check for an Ethernet type field less than
2066
     * 1500, which means it's an 802.3 length field.
2067
     */
2068
81
    b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
2069
81
    gen_not(b0);
2070
2071
    /*
2072
     * 802.2-encapsulated ETHERTYPE_ATALK packets are
2073
     * SNAP packets with an organization code of
2074
     * 0x080007 (Apple, for Appletalk) and a protocol
2075
     * type of ETHERTYPE_ATALK (Appletalk).
2076
     *
2077
     * 802.2-encapsulated ETHERTYPE_AARP packets are
2078
     * SNAP packets with an organization code of
2079
     * 0x000000 (encapsulated Ethernet) and a protocol
2080
     * type of ETHERTYPE_AARP (Appletalk ARP).
2081
     */
2082
81
    if (ll_proto == ETHERTYPE_ATALK)
2083
39
      b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
2084
42
    else  /* ll_proto == ETHERTYPE_AARP */
2085
42
      b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);
2086
81
    gen_and(b0, b1);
2087
2088
    /*
2089
     * Check for Ethernet encapsulation (Ethertalk
2090
     * phase 1?); we just check for the Ethernet
2091
     * protocol type.
2092
     */
2093
81
    b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
2094
2095
81
    gen_or(b0, b1);
2096
81
    return b1;
2097
2098
3.22k
  default:
2099
3.22k
    if (ll_proto <= ETHERMTU) {
2100
      /*
2101
       * This is an LLC SAP value, so the frames
2102
       * that match would be 802.2 frames.
2103
       * Check that the frame is an 802.2 frame
2104
       * (i.e., that the length/type field is
2105
       * a length field, <= ETHERMTU) and
2106
       * then check the DSAP.
2107
       */
2108
136
      b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
2109
136
      gen_not(b0);
2110
136
      b1 = gen_cmp(cstate, OR_LINKTYPE, 2, BPF_B, ll_proto);
2111
136
      gen_and(b0, b1);
2112
136
      return b1;
2113
3.09k
    } else {
2114
      /*
2115
       * This is an Ethernet type, so compare
2116
       * the length/type field with it (if
2117
       * the frame is an 802.2 frame, the length
2118
       * field will be <= ETHERMTU, and, as
2119
       * "ll_proto" is > ETHERMTU, this test
2120
       * will fail and the frame won't match,
2121
       * which is what we want).
2122
       */
2123
3.09k
      return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
2124
3.09k
    }
2125
4.81k
  }
2126
4.81k
}
2127
2128
static struct block *
2129
gen_loopback_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
2130
10.3k
{
2131
  /*
2132
   * For DLT_NULL, the link-layer header is a 32-bit word
2133
   * containing an AF_ value in *host* byte order, and for
2134
   * DLT_ENC, the link-layer header begins with a 32-bit
2135
   * word containing an AF_ value in host byte order.
2136
   *
2137
   * In addition, if we're reading a saved capture file,
2138
   * the host byte order in the capture may not be the
2139
   * same as the host byte order on this machine.
2140
   *
2141
   * For DLT_LOOP, the link-layer header is a 32-bit
2142
   * word containing an AF_ value in *network* byte order.
2143
   */
2144
10.3k
  if (cstate->linktype == DLT_NULL || cstate->linktype == DLT_ENC) {
2145
    /*
2146
     * The AF_ value is in host byte order, but the BPF
2147
     * interpreter will convert it to network byte order.
2148
     *
2149
     * If this is a save file, and it's from a machine
2150
     * with the opposite byte order to ours, we byte-swap
2151
     * the AF_ value.
2152
     *
2153
     * Then we run it through "htonl()", and generate
2154
     * code to compare against the result.
2155
     */
2156
9.69k
    if (cstate->bpf_pcap->rfile != NULL && cstate->bpf_pcap->swapped)
2157
2.07k
      ll_proto = SWAPLONG(ll_proto);
2158
9.69k
    ll_proto = htonl(ll_proto);
2159
9.69k
  }
2160
10.3k
  return (gen_cmp(cstate, OR_LINKHDR, 0, BPF_W, ll_proto));
2161
10.3k
}
2162
2163
/*
2164
 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2165
 * or IPv6 then we have an error.
2166
 */
2167
static struct block *
2168
gen_ipnet_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
2169
1.16k
{
2170
1.16k
  switch (ll_proto) {
2171
2172
332
  case ETHERTYPE_IP:
2173
332
    return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET);
2174
    /*NOTREACHED*/
2175
2176
87
  case ETHERTYPE_IPV6:
2177
87
    return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET6);
2178
    /*NOTREACHED*/
2179
2180
748
  default:
2181
748
    break;
2182
1.16k
  }
2183
2184
748
  return gen_false(cstate);
2185
1.16k
}
2186
2187
/*
2188
 * Generate code to match a particular packet type.
2189
 *
2190
 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2191
 * value, if <= ETHERMTU.  We use that to determine whether to
2192
 * match the type field or to check the type field for the special
2193
 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2194
 */
2195
static struct block *
2196
gen_linux_sll_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
2197
1.02k
{
2198
1.02k
  struct block *b0, *b1;
2199
2200
1.02k
  switch (ll_proto) {
2201
2202
251
  case LLCSAP_ISONS:
2203
317
  case LLCSAP_IP:
2204
353
  case LLCSAP_NETBEUI:
2205
    /*
2206
     * OSI protocols and NetBEUI always use 802.2 encapsulation,
2207
     * so we check the DSAP and SSAP.
2208
     *
2209
     * LLCSAP_IP checks for IP-over-802.2, rather
2210
     * than IP-over-Ethernet or IP-over-SNAP.
2211
     *
2212
     * XXX - should we check both the DSAP and the
2213
     * SSAP, like this, or should we check just the
2214
     * DSAP, as we do for other types <= ETHERMTU
2215
     * (i.e., other SAP values)?
2216
     */
2217
353
    b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2218
353
    b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto);
2219
353
    gen_and(b0, b1);
2220
353
    return b1;
2221
2222
77
  case LLCSAP_IPX:
2223
    /*
2224
     *  Ethernet_II frames, which are Ethernet
2225
     *  frames with a frame type of ETHERTYPE_IPX;
2226
     *
2227
     *  Ethernet_802.3 frames, which have a frame
2228
     *  type of LINUX_SLL_P_802_3;
2229
     *
2230
     *  Ethernet_802.2 frames, which are 802.3
2231
     *  frames with an 802.2 LLC header (i.e, have
2232
     *  a frame type of LINUX_SLL_P_802_2) and
2233
     *  with the IPX LSAP as the DSAP in the LLC
2234
     *  header;
2235
     *
2236
     *  Ethernet_SNAP frames, which are 802.3
2237
     *  frames with an LLC header and a SNAP
2238
     *  header and with an OUI of 0x000000
2239
     *  (encapsulated Ethernet) and a protocol
2240
     *  ID of ETHERTYPE_IPX in the SNAP header.
2241
     *
2242
     * First, do the checks on LINUX_SLL_P_802_2
2243
     * frames; generate the check for either
2244
     * Ethernet_802.2 or Ethernet_SNAP frames, and
2245
     * then put a check for LINUX_SLL_P_802_2 frames
2246
     * before it.
2247
     */
2248
77
    b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
2249
77
    b1 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);
2250
77
    gen_or(b0, b1);
2251
77
    b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2252
77
    gen_and(b0, b1);
2253
2254
    /*
2255
     * Now check for 802.3 frames and OR that with
2256
     * the previous test.
2257
     */
2258
77
    b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_3);
2259
77
    gen_or(b0, b1);
2260
2261
    /*
2262
     * Now add the check for Ethernet_II frames, and
2263
     * do that before checking for the other frame
2264
     * types.
2265
     */
2266
77
    b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX);
2267
77
    gen_or(b0, b1);
2268
77
    return b1;
2269
2270
34
  case ETHERTYPE_ATALK:
2271
68
  case ETHERTYPE_AARP:
2272
    /*
2273
     * EtherTalk (AppleTalk protocols on Ethernet link
2274
     * layer) may use 802.2 encapsulation.
2275
     */
2276
2277
    /*
2278
     * Check for 802.2 encapsulation (EtherTalk phase 2?);
2279
     * we check for the 802.2 protocol type in the
2280
     * "Ethernet type" field.
2281
     */
2282
68
    b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2283
2284
    /*
2285
     * 802.2-encapsulated ETHERTYPE_ATALK packets are
2286
     * SNAP packets with an organization code of
2287
     * 0x080007 (Apple, for Appletalk) and a protocol
2288
     * type of ETHERTYPE_ATALK (Appletalk).
2289
     *
2290
     * 802.2-encapsulated ETHERTYPE_AARP packets are
2291
     * SNAP packets with an organization code of
2292
     * 0x000000 (encapsulated Ethernet) and a protocol
2293
     * type of ETHERTYPE_AARP (Appletalk ARP).
2294
     */
2295
68
    if (ll_proto == ETHERTYPE_ATALK)
2296
34
      b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
2297
34
    else  /* ll_proto == ETHERTYPE_AARP */
2298
34
      b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);
2299
68
    gen_and(b0, b1);
2300
2301
    /*
2302
     * Check for Ethernet encapsulation (Ethertalk
2303
     * phase 1?); we just check for the Ethernet
2304
     * protocol type.
2305
     */
2306
68
    b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
2307
2308
68
    gen_or(b0, b1);
2309
68
    return b1;
2310
2311
531
  default:
2312
531
    if (ll_proto <= ETHERMTU) {
2313
      /*
2314
       * This is an LLC SAP value, so the frames
2315
       * that match would be 802.2 frames.
2316
       * Check for the 802.2 protocol type
2317
       * in the "Ethernet type" field, and
2318
       * then check the DSAP.
2319
       */
2320
91
      b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2321
91
      b1 = gen_cmp(cstate, OR_LINKHDR, cstate->off_linkpl.constant_part, BPF_B,
2322
91
           ll_proto);
2323
91
      gen_and(b0, b1);
2324
91
      return b1;
2325
440
    } else {
2326
      /*
2327
       * This is an Ethernet type, so compare
2328
       * the length/type field with it (if
2329
       * the frame is an 802.2 frame, the length
2330
       * field will be <= ETHERMTU, and, as
2331
       * "ll_proto" is > ETHERMTU, this test
2332
       * will fail and the frame won't match,
2333
       * which is what we want).
2334
       */
2335
440
      return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
2336
440
    }
2337
1.02k
  }
2338
1.02k
}
2339
2340
/*
2341
 * Load a value relative to the beginning of the link-layer header after the
2342
 * pflog header.
2343
 */
2344
static struct slist *
2345
gen_load_pflog_llprefixlen(compiler_state_t *cstate)
2346
543
{
2347
543
  struct slist *s1, *s2;
2348
2349
  /*
2350
   * Generate code to load the length of the pflog header into
2351
   * the register assigned to hold that length, if one has been
2352
   * assigned.  (If one hasn't been assigned, no code we've
2353
   * generated uses that prefix, so we don't need to generate any
2354
   * code to load it.)
2355
   */
2356
543
  if (cstate->off_linkpl.reg != -1) {
2357
    /*
2358
     * The length is in the first byte of the header.
2359
     */
2360
417
    s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2361
417
    s1->s.k = 0;
2362
2363
    /*
2364
     * Round it up to a multiple of 4.
2365
     * Add 3, and clear the lower 2 bits.
2366
     */
2367
417
    s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
2368
417
    s2->s.k = 3;
2369
417
    sappend(s1, s2);
2370
417
    s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
2371
417
    s2->s.k = 0xfffffffc;
2372
417
    sappend(s1, s2);
2373
2374
    /*
2375
     * Now allocate a register to hold that value and store
2376
     * it.
2377
     */
2378
417
    s2 = new_stmt(cstate, BPF_ST);
2379
417
    s2->s.k = cstate->off_linkpl.reg;
2380
417
    sappend(s1, s2);
2381
2382
    /*
2383
     * Now move it into the X register.
2384
     */
2385
417
    s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2386
417
    sappend(s1, s2);
2387
2388
417
    return (s1);
2389
417
  } else
2390
126
    return (NULL);
2391
543
}
2392
2393
static struct slist *
2394
gen_load_prism_llprefixlen(compiler_state_t *cstate)
2395
229
{
2396
229
  struct slist *s1, *s2;
2397
229
  struct slist *sjeq_avs_cookie;
2398
229
  struct slist *sjcommon;
2399
2400
  /*
2401
   * This code is not compatible with the optimizer, as
2402
   * we are generating jmp instructions within a normal
2403
   * slist of instructions
2404
   */
2405
229
  cstate->no_optimize = 1;
2406
2407
  /*
2408
   * Generate code to load the length of the radio header into
2409
   * the register assigned to hold that length, if one has been
2410
   * assigned.  (If one hasn't been assigned, no code we've
2411
   * generated uses that prefix, so we don't need to generate any
2412
   * code to load it.)
2413
   *
2414
   * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2415
   * or always use the AVS header rather than the Prism header.
2416
   * We load a 4-byte big-endian value at the beginning of the
2417
   * raw packet data, and see whether, when masked with 0xFFFFF000,
2418
   * it's equal to 0x80211000.  If so, that indicates that it's
2419
   * an AVS header (the masked-out bits are the version number).
2420
   * Otherwise, it's a Prism header.
2421
   *
2422
   * XXX - the Prism header is also, in theory, variable-length,
2423
   * but no known software generates headers that aren't 144
2424
   * bytes long.
2425
   */
2426
229
  if (cstate->off_linkhdr.reg != -1) {
2427
    /*
2428
     * Load the cookie.
2429
     */
2430
105
    s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2431
105
    s1->s.k = 0;
2432
2433
    /*
2434
     * AND it with 0xFFFFF000.
2435
     */
2436
105
    s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
2437
105
    s2->s.k = 0xFFFFF000;
2438
105
    sappend(s1, s2);
2439
2440
    /*
2441
     * Compare with 0x80211000.
2442
     */
2443
105
    sjeq_avs_cookie = new_stmt(cstate, JMP(BPF_JEQ));
2444
105
    sjeq_avs_cookie->s.k = 0x80211000;
2445
105
    sappend(s1, sjeq_avs_cookie);
2446
2447
    /*
2448
     * If it's AVS:
2449
     *
2450
     * The 4 bytes at an offset of 4 from the beginning of
2451
     * the AVS header are the length of the AVS header.
2452
     * That field is big-endian.
2453
     */
2454
105
    s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2455
105
    s2->s.k = 4;
2456
105
    sappend(s1, s2);
2457
105
    sjeq_avs_cookie->s.jt = s2;
2458
2459
    /*
2460
     * Now jump to the code to allocate a register
2461
     * into which to save the header length and
2462
     * store the length there.  (The "jump always"
2463
     * instruction needs to have the k field set;
2464
     * it's added to the PC, so, as we're jumping
2465
     * over a single instruction, it should be 1.)
2466
     */
2467
105
    sjcommon = new_stmt(cstate, JMP(BPF_JA));
2468
105
    sjcommon->s.k = 1;
2469
105
    sappend(s1, sjcommon);
2470
2471
    /*
2472
     * Now for the code that handles the Prism header.
2473
     * Just load the length of the Prism header (144)
2474
     * into the A register.  Have the test for an AVS
2475
     * header branch here if we don't have an AVS header.
2476
     */
2477
105
    s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);
2478
105
    s2->s.k = 144;
2479
105
    sappend(s1, s2);
2480
105
    sjeq_avs_cookie->s.jf = s2;
2481
2482
    /*
2483
     * Now allocate a register to hold that value and store
2484
     * it.  The code for the AVS header will jump here after
2485
     * loading the length of the AVS header.
2486
     */
2487
105
    s2 = new_stmt(cstate, BPF_ST);
2488
105
    s2->s.k = cstate->off_linkhdr.reg;
2489
105
    sappend(s1, s2);
2490
105
    sjcommon->s.jf = s2;
2491
2492
    /*
2493
     * Now move it into the X register.
2494
     */
2495
105
    s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2496
105
    sappend(s1, s2);
2497
2498
105
    return (s1);
2499
105
  } else
2500
124
    return (NULL);
2501
229
}
2502
2503
static struct slist *
2504
gen_load_avs_llprefixlen(compiler_state_t *cstate)
2505
127
{
2506
127
  struct slist *s1, *s2;
2507
2508
  /*
2509
   * Generate code to load the length of the AVS header into
2510
   * the register assigned to hold that length, if one has been
2511
   * assigned.  (If one hasn't been assigned, no code we've
2512
   * generated uses that prefix, so we don't need to generate any
2513
   * code to load it.)
2514
   */
2515
127
  if (cstate->off_linkhdr.reg != -1) {
2516
    /*
2517
     * The 4 bytes at an offset of 4 from the beginning of
2518
     * the AVS header are the length of the AVS header.
2519
     * That field is big-endian.
2520
     */
2521
112
    s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2522
112
    s1->s.k = 4;
2523
2524
    /*
2525
     * Now allocate a register to hold that value and store
2526
     * it.
2527
     */
2528
112
    s2 = new_stmt(cstate, BPF_ST);
2529
112
    s2->s.k = cstate->off_linkhdr.reg;
2530
112
    sappend(s1, s2);
2531
2532
    /*
2533
     * Now move it into the X register.
2534
     */
2535
112
    s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2536
112
    sappend(s1, s2);
2537
2538
112
    return (s1);
2539
112
  } else
2540
15
    return (NULL);
2541
127
}
2542
2543
static struct slist *
2544
gen_load_radiotap_llprefixlen(compiler_state_t *cstate)
2545
186
{
2546
186
  struct slist *s1, *s2;
2547
2548
  /*
2549
   * Generate code to load the length of the radiotap header into
2550
   * the register assigned to hold that length, if one has been
2551
   * assigned.  (If one hasn't been assigned, no code we've
2552
   * generated uses that prefix, so we don't need to generate any
2553
   * code to load it.)
2554
   */
2555
186
  if (cstate->off_linkhdr.reg != -1) {
2556
    /*
2557
     * The 2 bytes at offsets of 2 and 3 from the beginning
2558
     * of the radiotap header are the length of the radiotap
2559
     * header; unfortunately, it's little-endian, so we have
2560
     * to load it a byte at a time and construct the value.
2561
     */
2562
2563
    /*
2564
     * Load the high-order byte, at an offset of 3, shift it
2565
     * left a byte, and put the result in the X register.
2566
     */
2567
170
    s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2568
170
    s1->s.k = 3;
2569
170
    s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
2570
170
    sappend(s1, s2);
2571
170
    s2->s.k = 8;
2572
170
    s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2573
170
    sappend(s1, s2);
2574
2575
    /*
2576
     * Load the next byte, at an offset of 2, and OR the
2577
     * value from the X register into it.
2578
     */
2579
170
    s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2580
170
    sappend(s1, s2);
2581
170
    s2->s.k = 2;
2582
170
    s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);
2583
170
    sappend(s1, s2);
2584
2585
    /*
2586
     * Now allocate a register to hold that value and store
2587
     * it.
2588
     */
2589
170
    s2 = new_stmt(cstate, BPF_ST);
2590
170
    s2->s.k = cstate->off_linkhdr.reg;
2591
170
    sappend(s1, s2);
2592
2593
    /*
2594
     * Now move it into the X register.
2595
     */
2596
170
    s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2597
170
    sappend(s1, s2);
2598
2599
170
    return (s1);
2600
170
  } else
2601
16
    return (NULL);
2602
186
}
2603
2604
/*
2605
 * At the moment we treat PPI as normal Radiotap encoded
2606
 * packets. The difference is in the function that generates
2607
 * the code at the beginning to compute the header length.
2608
 * Since this code generator of PPI supports bare 802.11
2609
 * encapsulation only (i.e. the encapsulated DLT should be
2610
 * DLT_IEEE802_11) we generate code to check for this too;
2611
 * that's done in finish_parse().
2612
 */
2613
static struct slist *
2614
gen_load_ppi_llprefixlen(compiler_state_t *cstate)
2615
193
{
2616
193
  struct slist *s1, *s2;
2617
2618
  /*
2619
   * Generate code to load the length of the radiotap header
2620
   * into the register assigned to hold that length, if one has
2621
   * been assigned.
2622
   */
2623
193
  if (cstate->off_linkhdr.reg != -1) {
2624
    /*
2625
     * The 2 bytes at offsets of 2 and 3 from the beginning
2626
     * of the radiotap header are the length of the radiotap
2627
     * header; unfortunately, it's little-endian, so we have
2628
     * to load it a byte at a time and construct the value.
2629
     */
2630
2631
    /*
2632
     * Load the high-order byte, at an offset of 3, shift it
2633
     * left a byte, and put the result in the X register.
2634
     */
2635
142
    s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2636
142
    s1->s.k = 3;
2637
142
    s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
2638
142
    sappend(s1, s2);
2639
142
    s2->s.k = 8;
2640
142
    s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2641
142
    sappend(s1, s2);
2642
2643
    /*
2644
     * Load the next byte, at an offset of 2, and OR the
2645
     * value from the X register into it.
2646
     */
2647
142
    s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2648
142
    sappend(s1, s2);
2649
142
    s2->s.k = 2;
2650
142
    s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);
2651
142
    sappend(s1, s2);
2652
2653
    /*
2654
     * Now allocate a register to hold that value and store
2655
     * it.
2656
     */
2657
142
    s2 = new_stmt(cstate, BPF_ST);
2658
142
    s2->s.k = cstate->off_linkhdr.reg;
2659
142
    sappend(s1, s2);
2660
2661
    /*
2662
     * Now move it into the X register.
2663
     */
2664
142
    s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2665
142
    sappend(s1, s2);
2666
2667
142
    return (s1);
2668
142
  } else
2669
51
    return (NULL);
2670
193
}
2671
2672
/*
2673
 * Load a value relative to the beginning of the link-layer header after the 802.11
2674
 * header, i.e. LLC_SNAP.
2675
 * The link-layer header doesn't necessarily begin at the beginning
2676
 * of the packet data; there might be a variable-length prefix containing
2677
 * radio information.
2678
 */
2679
static struct slist *
2680
gen_load_802_11_header_len(compiler_state_t *cstate, struct slist *s, struct slist *snext)
2681
935
{
2682
935
  struct slist *s2;
2683
935
  struct slist *sjset_data_frame_1;
2684
935
  struct slist *sjset_data_frame_2;
2685
935
  struct slist *sjset_qos;
2686
935
  struct slist *sjset_radiotap_flags_present;
2687
935
  struct slist *sjset_radiotap_ext_present;
2688
935
  struct slist *sjset_radiotap_tsft_present;
2689
935
  struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
2690
935
  struct slist *s_roundup;
2691
2692
935
  if (cstate->off_linkpl.reg == -1) {
2693
    /*
2694
     * No register has been assigned to the offset of
2695
     * the link-layer payload, which means nobody needs
2696
     * it; don't bother computing it - just return
2697
     * what we already have.
2698
     */
2699
464
    return (s);
2700
464
  }
2701
2702
  /*
2703
   * This code is not compatible with the optimizer, as
2704
   * we are generating jmp instructions within a normal
2705
   * slist of instructions
2706
   */
2707
471
  cstate->no_optimize = 1;
2708
2709
  /*
2710
   * If "s" is non-null, it has code to arrange that the X register
2711
   * contains the length of the prefix preceding the link-layer
2712
   * header.
2713
   *
2714
   * Otherwise, the length of the prefix preceding the link-layer
2715
   * header is "off_outermostlinkhdr.constant_part".
2716
   */
2717
471
  if (s == NULL) {
2718
    /*
2719
     * There is no variable-length header preceding the
2720
     * link-layer header.
2721
     *
2722
     * Load the length of the fixed-length prefix preceding
2723
     * the link-layer header (if any) into the X register,
2724
     * and store it in the cstate->off_linkpl.reg register.
2725
     * That length is off_outermostlinkhdr.constant_part.
2726
     */
2727
156
    s = new_stmt(cstate, BPF_LDX|BPF_IMM);
2728
156
    s->s.k = cstate->off_outermostlinkhdr.constant_part;
2729
156
  }
2730
2731
  /*
2732
   * The X register contains the offset of the beginning of the
2733
   * link-layer header; add 24, which is the minimum length
2734
   * of the MAC header for a data frame, to that, and store it
2735
   * in cstate->off_linkpl.reg, and then load the Frame Control field,
2736
   * which is at the offset in the X register, with an indexed load.
2737
   */
2738
471
  s2 = new_stmt(cstate, BPF_MISC|BPF_TXA);
2739
471
  sappend(s, s2);
2740
471
  s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
2741
471
  s2->s.k = 24;
2742
471
  sappend(s, s2);
2743
471
  s2 = new_stmt(cstate, BPF_ST);
2744
471
  s2->s.k = cstate->off_linkpl.reg;
2745
471
  sappend(s, s2);
2746
2747
471
  s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
2748
471
  s2->s.k = 0;
2749
471
  sappend(s, s2);
2750
2751
  /*
2752
   * Check the Frame Control field to see if this is a data frame;
2753
   * a data frame has the 0x08 bit (b3) in that field set and the
2754
   * 0x04 bit (b2) clear.
2755
   */
2756
471
  sjset_data_frame_1 = new_stmt(cstate, JMP(BPF_JSET));
2757
471
  sjset_data_frame_1->s.k = 0x08;
2758
471
  sappend(s, sjset_data_frame_1);
2759
2760
  /*
2761
   * If b3 is set, test b2, otherwise go to the first statement of
2762
   * the rest of the program.
2763
   */
2764
471
  sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(cstate, JMP(BPF_JSET));
2765
471
  sjset_data_frame_2->s.k = 0x04;
2766
471
  sappend(s, sjset_data_frame_2);
2767
471
  sjset_data_frame_1->s.jf = snext;
2768
2769
  /*
2770
   * If b2 is not set, this is a data frame; test the QoS bit.
2771
   * Otherwise, go to the first statement of the rest of the
2772
   * program.
2773
   */
2774
471
  sjset_data_frame_2->s.jt = snext;
2775
471
  sjset_data_frame_2->s.jf = sjset_qos = new_stmt(cstate, JMP(BPF_JSET));
2776
471
  sjset_qos->s.k = 0x80;  /* QoS bit */
2777
471
  sappend(s, sjset_qos);
2778
2779
  /*
2780
   * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2781
   * field.
2782
   * Otherwise, go to the first statement of the rest of the
2783
   * program.
2784
   */
2785
471
  sjset_qos->s.jt = s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
2786
471
  s2->s.k = cstate->off_linkpl.reg;
2787
471
  sappend(s, s2);
2788
471
  s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
2789
471
  s2->s.k = 2;
2790
471
  sappend(s, s2);
2791
471
  s2 = new_stmt(cstate, BPF_ST);
2792
471
  s2->s.k = cstate->off_linkpl.reg;
2793
471
  sappend(s, s2);
2794
2795
  /*
2796
   * If we have a radiotap header, look at it to see whether
2797
   * there's Atheros padding between the MAC-layer header
2798
   * and the payload.
2799
   *
2800
   * Note: all of the fields in the radiotap header are
2801
   * little-endian, so we byte-swap all of the values
2802
   * we test against, as they will be loaded as big-endian
2803
   * values.
2804
   *
2805
   * XXX - in the general case, we would have to scan through
2806
   * *all* the presence bits, if there's more than one word of
2807
   * presence bits.  That would require a loop, meaning that
2808
   * we wouldn't be able to run the filter in the kernel.
2809
   *
2810
   * We assume here that the Atheros adapters that insert the
2811
   * annoying padding don't have multiple antennae and therefore
2812
   * do not generate radiotap headers with multiple presence words.
2813
   */
2814
471
  if (cstate->linktype == DLT_IEEE802_11_RADIO) {
2815
    /*
2816
     * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2817
     * in the first presence flag word?
2818
     */
2819
89
    sjset_qos->s.jf = s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_W);
2820
89
    s2->s.k = 4;
2821
89
    sappend(s, s2);
2822
2823
89
    sjset_radiotap_flags_present = new_stmt(cstate, JMP(BPF_JSET));
2824
89
    sjset_radiotap_flags_present->s.k = SWAPLONG(0x00000002);
2825
89
    sappend(s, sjset_radiotap_flags_present);
2826
2827
    /*
2828
     * If not, skip all of this.
2829
     */
2830
89
    sjset_radiotap_flags_present->s.jf = snext;
2831
2832
    /*
2833
     * Otherwise, is the "extension" bit set in that word?
2834
     */
2835
89
    sjset_radiotap_ext_present = new_stmt(cstate, JMP(BPF_JSET));
2836
89
    sjset_radiotap_ext_present->s.k = SWAPLONG(0x80000000);
2837
89
    sappend(s, sjset_radiotap_ext_present);
2838
89
    sjset_radiotap_flags_present->s.jt = sjset_radiotap_ext_present;
2839
2840
    /*
2841
     * If so, skip all of this.
2842
     */
2843
89
    sjset_radiotap_ext_present->s.jt = snext;
2844
2845
    /*
2846
     * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2847
     */
2848
89
    sjset_radiotap_tsft_present = new_stmt(cstate, JMP(BPF_JSET));
2849
89
    sjset_radiotap_tsft_present->s.k = SWAPLONG(0x00000001);
2850
89
    sappend(s, sjset_radiotap_tsft_present);
2851
89
    sjset_radiotap_ext_present->s.jf = sjset_radiotap_tsft_present;
2852
2853
    /*
2854
     * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2855
     * at an offset of 16 from the beginning of the raw packet
2856
     * data (8 bytes for the radiotap header and 8 bytes for
2857
     * the TSFT field).
2858
     *
2859
     * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2860
     * is set.
2861
     */
2862
89
    s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
2863
89
    s2->s.k = 16;
2864
89
    sappend(s, s2);
2865
89
    sjset_radiotap_tsft_present->s.jt = s2;
2866
2867
89
    sjset_tsft_datapad = new_stmt(cstate, JMP(BPF_JSET));
2868
89
    sjset_tsft_datapad->s.k = 0x20;
2869
89
    sappend(s, sjset_tsft_datapad);
2870
2871
    /*
2872
     * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2873
     * at an offset of 8 from the beginning of the raw packet
2874
     * data (8 bytes for the radiotap header).
2875
     *
2876
     * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2877
     * is set.
2878
     */
2879
89
    s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
2880
89
    s2->s.k = 8;
2881
89
    sappend(s, s2);
2882
89
    sjset_radiotap_tsft_present->s.jf = s2;
2883
2884
89
    sjset_notsft_datapad = new_stmt(cstate, JMP(BPF_JSET));
2885
89
    sjset_notsft_datapad->s.k = 0x20;
2886
89
    sappend(s, sjset_notsft_datapad);
2887
2888
    /*
2889
     * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2890
     * set, round the length of the 802.11 header to
2891
     * a multiple of 4.  Do that by adding 3 and then
2892
     * dividing by and multiplying by 4, which we do by
2893
     * ANDing with ~3.
2894
     */
2895
89
    s_roundup = new_stmt(cstate, BPF_LD|BPF_MEM);
2896
89
    s_roundup->s.k = cstate->off_linkpl.reg;
2897
89
    sappend(s, s_roundup);
2898
89
    s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
2899
89
    s2->s.k = 3;
2900
89
    sappend(s, s2);
2901
89
    s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_IMM);
2902
89
    s2->s.k = (bpf_u_int32)~3;
2903
89
    sappend(s, s2);
2904
89
    s2 = new_stmt(cstate, BPF_ST);
2905
89
    s2->s.k = cstate->off_linkpl.reg;
2906
89
    sappend(s, s2);
2907
2908
89
    sjset_tsft_datapad->s.jt = s_roundup;
2909
89
    sjset_tsft_datapad->s.jf = snext;
2910
89
    sjset_notsft_datapad->s.jt = s_roundup;
2911
89
    sjset_notsft_datapad->s.jf = snext;
2912
89
  } else
2913
382
    sjset_qos->s.jf = snext;
2914
2915
471
  return s;
2916
935
}
2917
2918
static void
2919
insert_compute_vloffsets(compiler_state_t *cstate, struct block *b)
2920
7.23k
{
2921
7.23k
  struct slist *s;
2922
2923
  /* There is an implicit dependency between the link
2924
   * payload and link header since the payload computation
2925
   * includes the variable part of the header. Therefore,
2926
   * if nobody else has allocated a register for the link
2927
   * header and we need it, do it now. */
2928
7.23k
  if (cstate->off_linkpl.reg != -1 && cstate->off_linkhdr.is_variable &&
2929
7.23k
      cstate->off_linkhdr.reg == -1)
2930
18
    cstate->off_linkhdr.reg = alloc_reg(cstate);
2931
2932
  /*
2933
   * For link-layer types that have a variable-length header
2934
   * preceding the link-layer header, generate code to load
2935
   * the offset of the link-layer header into the register
2936
   * assigned to that offset, if any.
2937
   *
2938
   * XXX - this, and the next switch statement, won't handle
2939
   * encapsulation of 802.11 or 802.11+radio information in
2940
   * some other protocol stack.  That's significantly more
2941
   * complicated.
2942
   */
2943
7.23k
  switch (cstate->outermostlinktype) {
2944
2945
229
  case DLT_PRISM_HEADER:
2946
229
    s = gen_load_prism_llprefixlen(cstate);
2947
229
    break;
2948
2949
127
  case DLT_IEEE802_11_RADIO_AVS:
2950
127
    s = gen_load_avs_llprefixlen(cstate);
2951
127
    break;
2952
2953
186
  case DLT_IEEE802_11_RADIO:
2954
186
    s = gen_load_radiotap_llprefixlen(cstate);
2955
186
    break;
2956
2957
193
  case DLT_PPI:
2958
193
    s = gen_load_ppi_llprefixlen(cstate);
2959
193
    break;
2960
2961
6.49k
  default:
2962
6.49k
    s = NULL;
2963
6.49k
    break;
2964
7.23k
  }
2965
2966
  /*
2967
   * For link-layer types that have a variable-length link-layer
2968
   * header, generate code to load the offset of the link-layer
2969
   * payload into the register assigned to that offset, if any.
2970
   */
2971
7.23k
  switch (cstate->outermostlinktype) {
2972
2973
200
  case DLT_IEEE802_11:
2974
429
  case DLT_PRISM_HEADER:
2975
556
  case DLT_IEEE802_11_RADIO_AVS:
2976
742
  case DLT_IEEE802_11_RADIO:
2977
935
  case DLT_PPI:
2978
935
    s = gen_load_802_11_header_len(cstate, s, b->stmts);
2979
935
    break;
2980
2981
543
  case DLT_PFLOG:
2982
543
    s = gen_load_pflog_llprefixlen(cstate);
2983
543
    break;
2984
7.23k
  }
2985
2986
  /*
2987
   * If there is no initialization yet and we need variable
2988
   * length offsets for VLAN, initialize them to zero
2989
   */
2990
7.23k
  if (s == NULL && cstate->is_vlan_vloffset) {
2991
0
    struct slist *s2;
2992
2993
0
    if (cstate->off_linkpl.reg == -1)
2994
0
      cstate->off_linkpl.reg = alloc_reg(cstate);
2995
0
    if (cstate->off_linktype.reg == -1)
2996
0
      cstate->off_linktype.reg = alloc_reg(cstate);
2997
2998
0
    s = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);
2999
0
    s->s.k = 0;
3000
0
    s2 = new_stmt(cstate, BPF_ST);
3001
0
    s2->s.k = cstate->off_linkpl.reg;
3002
0
    sappend(s, s2);
3003
0
    s2 = new_stmt(cstate, BPF_ST);
3004
0
    s2->s.k = cstate->off_linktype.reg;
3005
0
    sappend(s, s2);
3006
0
  }
3007
3008
  /*
3009
   * If we have any offset-loading code, append all the
3010
   * existing statements in the block to those statements,
3011
   * and make the resulting list the list of statements
3012
   * for the block.
3013
   */
3014
7.23k
  if (s != NULL) {
3015
1.10k
    sappend(s, b->stmts);
3016
1.10k
    b->stmts = s;
3017
1.10k
  }
3018
7.23k
}
3019
3020
static struct block *
3021
gen_ppi_dlt_check(compiler_state_t *cstate)
3022
7.23k
{
3023
7.23k
  struct slist *s_load_dlt;
3024
7.23k
  struct block *b;
3025
3026
7.23k
  if (cstate->linktype == DLT_PPI)
3027
191
  {
3028
    /* Create the statements that check for the DLT
3029
     */
3030
191
    s_load_dlt = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
3031
191
    s_load_dlt->s.k = 4;
3032
3033
191
    b = new_block(cstate, JMP(BPF_JEQ));
3034
3035
191
    b->stmts = s_load_dlt;
3036
191
    b->s.k = SWAPLONG(DLT_IEEE802_11);
3037
191
  }
3038
7.04k
  else
3039
7.04k
  {
3040
7.04k
    b = NULL;
3041
7.04k
  }
3042
3043
7.23k
  return b;
3044
7.23k
}
3045
3046
/*
3047
 * Take an absolute offset, and:
3048
 *
3049
 *    if it has no variable part, return NULL;
3050
 *
3051
 *    if it has a variable part, generate code to load the register
3052
 *    containing that variable part into the X register, returning
3053
 *    a pointer to that code - if no register for that offset has
3054
 *    been allocated, allocate it first.
3055
 *
3056
 * (The code to set that register will be generated later, but will
3057
 * be placed earlier in the code sequence.)
3058
 */
3059
static struct slist *
3060
gen_abs_offset_varpart(compiler_state_t *cstate, bpf_abs_offset *off)
3061
280k
{
3062
280k
  struct slist *s;
3063
3064
280k
  if (off->is_variable) {
3065
97.3k
    if (off->reg == -1) {
3066
      /*
3067
       * We haven't yet assigned a register for the
3068
       * variable part of the offset of the link-layer
3069
       * header; allocate one.
3070
       */
3071
2.15k
      off->reg = alloc_reg(cstate);
3072
2.15k
    }
3073
3074
    /*
3075
     * Load the register containing the variable part of the
3076
     * offset of the link-layer header into the X register.
3077
     */
3078
97.3k
    s = new_stmt(cstate, BPF_LDX|BPF_MEM);
3079
97.3k
    s->s.k = off->reg;
3080
97.3k
    return s;
3081
183k
  } else {
3082
    /*
3083
     * That offset isn't variable, there's no variable part,
3084
     * so we don't need to generate any code.
3085
     */
3086
183k
    return NULL;
3087
183k
  }
3088
280k
}
3089
3090
/*
3091
 * Map an Ethernet type to the equivalent PPP type.
3092
 */
3093
static bpf_u_int32
3094
ethertype_to_ppptype(bpf_u_int32 ll_proto)
3095
8.23k
{
3096
8.23k
  switch (ll_proto) {
3097
3098
1.07k
  case ETHERTYPE_IP:
3099
1.07k
    ll_proto = PPP_IP;
3100
1.07k
    break;
3101
3102
1.23k
  case ETHERTYPE_IPV6:
3103
1.23k
    ll_proto = PPP_IPV6;
3104
1.23k
    break;
3105
3106
703
  case ETHERTYPE_DN:
3107
703
    ll_proto = PPP_DECNET;
3108
703
    break;
3109
3110
69
  case ETHERTYPE_ATALK:
3111
69
    ll_proto = PPP_APPLE;
3112
69
    break;
3113
3114
69
  case ETHERTYPE_NS:
3115
69
    ll_proto = PPP_NS;
3116
69
    break;
3117
3118
874
  case LLCSAP_ISONS:
3119
874
    ll_proto = PPP_OSI;
3120
874
    break;
3121
3122
143
  case LLCSAP_8021D:
3123
    /*
3124
     * I'm assuming the "Bridging PDU"s that go
3125
     * over PPP are Spanning Tree Protocol
3126
     * Bridging PDUs.
3127
     */
3128
143
    ll_proto = PPP_BRPDU;
3129
143
    break;
3130
3131
160
  case LLCSAP_IPX:
3132
160
    ll_proto = PPP_IPX;
3133
160
    break;
3134
8.23k
  }
3135
8.23k
  return (ll_proto);
3136
8.23k
}
3137
3138
/*
3139
 * Generate any tests that, for encapsulation of a link-layer packet
3140
 * inside another protocol stack, need to be done to check for those
3141
 * link-layer packets (and that haven't already been done by a check
3142
 * for that encapsulation).
3143
 */
3144
static struct block *
3145
gen_prevlinkhdr_check(compiler_state_t *cstate)
3146
5.06k
{
3147
5.06k
  struct block *b0;
3148
3149
5.06k
  if (cstate->is_geneve)
3150
0
    return gen_geneve_ll_check(cstate);
3151
3152
5.06k
  switch (cstate->prevlinktype) {
3153
3154
937
  case DLT_SUNATM:
3155
    /*
3156
     * This is LANE-encapsulated Ethernet; check that the LANE
3157
     * packet doesn't begin with an LE Control marker, i.e.
3158
     * that it's data, not a control message.
3159
     *
3160
     * (We've already generated a test for LANE.)
3161
     */
3162
937
    b0 = gen_cmp(cstate, OR_PREVLINKHDR, SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
3163
937
    gen_not(b0);
3164
937
    return b0;
3165
3166
4.12k
  default:
3167
    /*
3168
     * No such tests are necessary.
3169
     */
3170
4.12k
    return NULL;
3171
5.06k
  }
3172
  /*NOTREACHED*/
3173
5.06k
}
3174
3175
/*
3176
 * The three different values we should check for when checking for an
3177
 * IPv6 packet with DLT_NULL.
3178
 */
3179
2.30k
#define BSD_AFNUM_INET6_BSD 24  /* NetBSD, OpenBSD, BSD/OS, Npcap */
3180
2.30k
#define BSD_AFNUM_INET6_FREEBSD 28  /* FreeBSD */
3181
2.30k
#define BSD_AFNUM_INET6_DARWIN  30  /* macOS, iOS, other Darwin-based OSes */
3182
3183
/*
3184
 * Generate code to match a particular packet type by matching the
3185
 * link-layer type field or fields in the 802.2 LLC header.
3186
 *
3187
 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3188
 * value, if <= ETHERMTU.
3189
 */
3190
static struct block *
3191
gen_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
3192
72.7k
{
3193
72.7k
  struct block *b0, *b1, *b2;
3194
72.7k
  const char *description;
3195
3196
  /* are we checking MPLS-encapsulated packets? */
3197
72.7k
  if (cstate->label_stack_depth > 0)
3198
788
    return gen_mpls_linktype(cstate, ll_proto);
3199
3200
72.0k
  switch (cstate->linktype) {
3201
3202
3.56k
  case DLT_EN10MB:
3203
4.14k
  case DLT_NETANALYZER:
3204
4.81k
  case DLT_NETANALYZER_TRANSPARENT:
3205
    /* Geneve has an EtherType regardless of whether there is an
3206
     * L2 header. */
3207
4.81k
    if (!cstate->is_geneve)
3208
4.81k
      b0 = gen_prevlinkhdr_check(cstate);
3209
0
    else
3210
0
      b0 = NULL;
3211
3212
4.81k
    b1 = gen_ether_linktype(cstate, ll_proto);
3213
4.81k
    if (b0 != NULL)
3214
891
      gen_and(b0, b1);
3215
4.81k
    return b1;
3216
    /*NOTREACHED*/
3217
3218
580
  case DLT_C_HDLC:
3219
1.39k
  case DLT_HDLC:
3220
1.39k
    switch (ll_proto) {
3221
3222
72
    case LLCSAP_ISONS:
3223
72
      ll_proto = (ll_proto << 8 | LLCSAP_ISONS);
3224
      /* fall through */
3225
3226
1.39k
    default:
3227
1.39k
      return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
3228
      /*NOTREACHED*/
3229
1.39k
    }
3230
3231
7.15k
  case DLT_IEEE802_11:
3232
10.6k
  case DLT_PRISM_HEADER:
3233
12.0k
  case DLT_IEEE802_11_RADIO_AVS:
3234
14.5k
  case DLT_IEEE802_11_RADIO:
3235
16.9k
  case DLT_PPI:
3236
    /*
3237
     * Check that we have a data frame.
3238
     */
3239
16.9k
    b0 = gen_check_802_11_data_frame(cstate);
3240
3241
    /*
3242
     * Now check for the specified link-layer type.
3243
     */
3244
16.9k
    b1 = gen_llc_linktype(cstate, ll_proto);
3245
16.9k
    gen_and(b0, b1);
3246
16.9k
    return b1;
3247
    /*NOTREACHED*/
3248
3249
389
  case DLT_FDDI:
3250
    /*
3251
     * XXX - check for LLC frames.
3252
     */
3253
389
    return gen_llc_linktype(cstate, ll_proto);
3254
    /*NOTREACHED*/
3255
3256
522
  case DLT_IEEE802:
3257
    /*
3258
     * XXX - check for LLC PDUs, as per IEEE 802.5.
3259
     */
3260
522
    return gen_llc_linktype(cstate, ll_proto);
3261
    /*NOTREACHED*/
3262
3263
619
  case DLT_ATM_RFC1483:
3264
1.00k
  case DLT_ATM_CLIP:
3265
1.46k
  case DLT_IP_OVER_FC:
3266
1.46k
    return gen_llc_linktype(cstate, ll_proto);
3267
    /*NOTREACHED*/
3268
3269
1.39k
  case DLT_SUNATM:
3270
    /*
3271
     * Check for an LLC-encapsulated version of this protocol;
3272
     * if we were checking for LANE, linktype would no longer
3273
     * be DLT_SUNATM.
3274
     *
3275
     * Check for LLC encapsulation and then check the protocol.
3276
     */
3277
1.39k
    b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3278
1.39k
    b1 = gen_llc_linktype(cstate, ll_proto);
3279
1.39k
    gen_and(b0, b1);
3280
1.39k
    return b1;
3281
    /*NOTREACHED*/
3282
3283
1.02k
  case DLT_LINUX_SLL:
3284
1.02k
    return gen_linux_sll_linktype(cstate, ll_proto);
3285
    /*NOTREACHED*/
3286
3287
919
  case DLT_SLIP:
3288
1.97k
  case DLT_SLIP_BSDOS:
3289
3.18k
  case DLT_RAW:
3290
    /*
3291
     * These types don't provide any type field; packets
3292
     * are always IPv4 or IPv6.
3293
     *
3294
     * XXX - for IPv4, check for a version number of 4, and,
3295
     * for IPv6, check for a version number of 6?
3296
     */
3297
3.18k
    switch (ll_proto) {
3298
3299
1.10k
    case ETHERTYPE_IP:
3300
      /* Check for a version number of 4. */
3301
1.10k
      return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x40, 0xF0);
3302
3303
425
    case ETHERTYPE_IPV6:
3304
      /* Check for a version number of 6. */
3305
425
      return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x60, 0xF0);
3306
3307
1.64k
    default:
3308
1.64k
      return gen_false(cstate); /* always false */
3309
3.18k
    }
3310
    /*NOTREACHED*/
3311
3312
1.36k
  case DLT_IPV4:
3313
    /*
3314
     * Raw IPv4, so no type field.
3315
     */
3316
1.36k
    if (ll_proto == ETHERTYPE_IP)
3317
535
      return gen_true(cstate);  /* always true */
3318
3319
    /* Checking for something other than IPv4; always false */
3320
834
    return gen_false(cstate);
3321
    /*NOTREACHED*/
3322
3323
761
  case DLT_IPV6:
3324
    /*
3325
     * Raw IPv6, so no type field.
3326
     */
3327
761
    if (ll_proto == ETHERTYPE_IPV6)
3328
200
      return gen_true(cstate);  /* always true */
3329
3330
    /* Checking for something other than IPv6; always false */
3331
561
    return gen_false(cstate);
3332
    /*NOTREACHED*/
3333
3334
1.68k
  case DLT_PPP:
3335
2.26k
  case DLT_PPP_PPPD:
3336
3.75k
  case DLT_PPP_SERIAL:
3337
4.83k
  case DLT_PPP_ETHER:
3338
    /*
3339
     * We use Ethernet protocol types inside libpcap;
3340
     * map them to the corresponding PPP protocol types.
3341
     */
3342
4.83k
    return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,
3343
4.83k
        ethertype_to_ppptype(ll_proto));
3344
    /*NOTREACHED*/
3345
3346
5.58k
  case DLT_PPP_BSDOS:
3347
    /*
3348
     * We use Ethernet protocol types inside libpcap;
3349
     * map them to the corresponding PPP protocol types.
3350
     */
3351
5.58k
    switch (ll_proto) {
3352
3353
2.17k
    case ETHERTYPE_IP:
3354
      /*
3355
       * Also check for Van Jacobson-compressed IP.
3356
       * XXX - do this for other forms of PPP?
3357
       */
3358
2.17k
      b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_IP);
3359
2.17k
      b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJC);
3360
2.17k
      gen_or(b0, b1);
3361
2.17k
      b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJNC);
3362
2.17k
      gen_or(b1, b0);
3363
2.17k
      return b0;
3364
3365
3.40k
    default:
3366
3.40k
      return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,
3367
3.40k
          ethertype_to_ppptype(ll_proto));
3368
5.58k
    }
3369
    /*NOTREACHED*/
3370
3371
7.44k
  case DLT_NULL:
3372
8.32k
  case DLT_LOOP:
3373
8.88k
  case DLT_ENC:
3374
8.88k
    switch (ll_proto) {
3375
3376
3.42k
    case ETHERTYPE_IP:
3377
3.42k
      return (gen_loopback_linktype(cstate, AF_INET));
3378
3379
2.30k
    case ETHERTYPE_IPV6:
3380
      /*
3381
       * AF_ values may, unfortunately, be platform-
3382
       * dependent; AF_INET isn't, because everybody
3383
       * used 4.2BSD's value, but AF_INET6 is, because
3384
       * 4.2BSD didn't have a value for it (given that
3385
       * IPv6 didn't exist back in the early 1980's),
3386
       * and they all picked their own values.
3387
       *
3388
       * This means that, if we're reading from a
3389
       * savefile, we need to check for all the
3390
       * possible values.
3391
       *
3392
       * If we're doing a live capture, we only need
3393
       * to check for this platform's value; however,
3394
       * Npcap uses 24, which isn't Windows's AF_INET6
3395
       * value.  (Given the multiple different values,
3396
       * programs that read pcap files shouldn't be
3397
       * checking for their platform's AF_INET6 value
3398
       * anyway, they should check for all of the
3399
       * possible values. and they might as well do
3400
       * that even for live captures.)
3401
       */
3402
2.30k
      if (cstate->bpf_pcap->rfile != NULL) {
3403
        /*
3404
         * Savefile - check for all three
3405
         * possible IPv6 values.
3406
         */
3407
2.30k
        b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_BSD);
3408
2.30k
        b1 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_FREEBSD);
3409
2.30k
        gen_or(b0, b1);
3410
2.30k
        b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_DARWIN);
3411
2.30k
        gen_or(b0, b1);
3412
2.30k
        return (b1);
3413
2.30k
      } else {
3414
        /*
3415
         * Live capture, so we only need to
3416
         * check for the value used on this
3417
         * platform.
3418
         */
3419
#ifdef _WIN32
3420
        /*
3421
         * Npcap doesn't use Windows's AF_INET6,
3422
         * as that collides with AF_IPX on
3423
         * some BSDs (both have the value 23).
3424
         * Instead, it uses 24.
3425
         */
3426
        return (gen_loopback_linktype(cstate, 24));
3427
#else /* _WIN32 */
3428
0
#ifdef AF_INET6
3429
0
        return (gen_loopback_linktype(cstate, AF_INET6));
3430
#else /* AF_INET6 */
3431
        /*
3432
         * I guess this platform doesn't support
3433
         * IPv6, so we just reject all packets.
3434
         */
3435
        return gen_false(cstate);
3436
#endif /* AF_INET6 */
3437
0
#endif /* _WIN32 */
3438
0
      }
3439
3440
3.15k
    default:
3441
      /*
3442
       * Not a type on which we support filtering.
3443
       * XXX - support those that have AF_ values
3444
       * #defined on this platform, at least?
3445
       */
3446
3.15k
      return gen_false(cstate);
3447
8.88k
    }
3448
3449
4.50k
  case DLT_PFLOG:
3450
    /*
3451
     * af field is host byte order in contrast to the rest of
3452
     * the packet.
3453
     */
3454
4.50k
    if (ll_proto == ETHERTYPE_IP)
3455
1.95k
      return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),
3456
1.95k
          BPF_B, AF_INET));
3457
2.54k
    else if (ll_proto == ETHERTYPE_IPV6)
3458
1.04k
      return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),
3459
1.04k
          BPF_B, AF_INET6));
3460
1.50k
    else
3461
1.50k
      return gen_false(cstate);
3462
    /*NOTREACHED*/
3463
3464
2.39k
  case DLT_ARCNET:
3465
4.00k
  case DLT_ARCNET_LINUX:
3466
    /*
3467
     * XXX should we check for first fragment if the protocol
3468
     * uses PHDS?
3469
     */
3470
4.00k
    switch (ll_proto) {
3471
3472
362
    default:
3473
362
      return gen_false(cstate);
3474
3475
147
    case ETHERTYPE_IPV6:
3476
147
      return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3477
147
        ARCTYPE_INET6));
3478
3479
1.25k
    case ETHERTYPE_IP:
3480
1.25k
      b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3481
1.25k
          ARCTYPE_IP);
3482
1.25k
      b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3483
1.25k
          ARCTYPE_IP_OLD);
3484
1.25k
      gen_or(b0, b1);
3485
1.25k
      return (b1);
3486
3487
1.10k
    case ETHERTYPE_ARP:
3488
1.10k
      b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3489
1.10k
          ARCTYPE_ARP);
3490
1.10k
      b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3491
1.10k
          ARCTYPE_ARP_OLD);
3492
1.10k
      gen_or(b0, b1);
3493
1.10k
      return (b1);
3494
3495
1.09k
    case ETHERTYPE_REVARP:
3496
1.09k
      return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3497
1.09k
          ARCTYPE_REVARP));
3498
3499
34
    case ETHERTYPE_ATALK:
3500
34
      return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3501
34
          ARCTYPE_ATALK));
3502
4.00k
    }
3503
    /*NOTREACHED*/
3504
3505
447
  case DLT_LTALK:
3506
447
    switch (ll_proto) {
3507
35
    case ETHERTYPE_ATALK:
3508
35
      return gen_true(cstate);
3509
412
    default:
3510
412
      return gen_false(cstate);
3511
447
    }
3512
    /*NOTREACHED*/
3513
3514
1.47k
  case DLT_FRELAY:
3515
    /*
3516
     * XXX - assumes a 2-byte Frame Relay header with
3517
     * DLCI and flags.  What if the address is longer?
3518
     */
3519
1.47k
    switch (ll_proto) {
3520
3521
450
    case ETHERTYPE_IP:
3522
      /*
3523
       * Check for the special NLPID for IP.
3524
       */
3525
450
      return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0xcc);
3526
3527
157
    case ETHERTYPE_IPV6:
3528
      /*
3529
       * Check for the special NLPID for IPv6.
3530
       */
3531
157
      return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0x8e);
3532
3533
69
    case LLCSAP_ISONS:
3534
      /*
3535
       * Check for several OSI protocols.
3536
       *
3537
       * Frame Relay packets typically have an OSI
3538
       * NLPID at the beginning; we check for each
3539
       * of them.
3540
       *
3541
       * What we check for is the NLPID and a frame
3542
       * control field of UI, i.e. 0x03 followed
3543
       * by the NLPID.
3544
       */
3545
69
      b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3546
69
      b1 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3547
69
      b2 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3548
69
      gen_or(b1, b2);
3549
69
      gen_or(b0, b2);
3550
69
      return b2;
3551
3552
795
    default:
3553
795
      return gen_false(cstate);
3554
1.47k
    }
3555
    /*NOTREACHED*/
3556
3557
1
  case DLT_MFR:
3558
1
    bpf_error(cstate, "Multi-link Frame Relay link-layer type filtering not implemented");
3559
3560
294
        case DLT_JUNIPER_MFR:
3561
795
        case DLT_JUNIPER_MLFR:
3562
1.03k
        case DLT_JUNIPER_MLPPP:
3563
1.30k
  case DLT_JUNIPER_ATM1:
3564
1.53k
  case DLT_JUNIPER_ATM2:
3565
1.79k
  case DLT_JUNIPER_PPPOE:
3566
2.00k
  case DLT_JUNIPER_PPPOE_ATM:
3567
2.22k
        case DLT_JUNIPER_GGSN:
3568
2.81k
        case DLT_JUNIPER_ES:
3569
3.23k
        case DLT_JUNIPER_MONITOR:
3570
3.54k
        case DLT_JUNIPER_SERVICES:
3571
4.06k
        case DLT_JUNIPER_ETHER:
3572
4.40k
        case DLT_JUNIPER_PPP:
3573
4.64k
        case DLT_JUNIPER_FRELAY:
3574
4.87k
        case DLT_JUNIPER_CHDLC:
3575
5.16k
        case DLT_JUNIPER_VP:
3576
5.48k
        case DLT_JUNIPER_ST:
3577
5.83k
        case DLT_JUNIPER_ISM:
3578
6.08k
        case DLT_JUNIPER_VS:
3579
6.34k
        case DLT_JUNIPER_SRX_E2E:
3580
6.69k
        case DLT_JUNIPER_FIBRECHANNEL:
3581
6.94k
  case DLT_JUNIPER_ATM_CEMIC:
3582
3583
    /* just lets verify the magic number for now -
3584
     * on ATM we may have up to 6 different encapsulations on the wire
3585
     * and need a lot of heuristics to figure out that the payload
3586
     * might be;
3587
     *
3588
     * FIXME encapsulation specific BPF_ filters
3589
     */
3590
6.94k
    return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3591
3592
387
  case DLT_BACNET_MS_TP:
3593
387
    return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x55FF0000, 0xffff0000);
3594
3595
1.16k
  case DLT_IPNET:
3596
1.16k
    return gen_ipnet_linktype(cstate, ll_proto);
3597
3598
2
  case DLT_LINUX_IRDA:
3599
2
    bpf_error(cstate, "IrDA link-layer type filtering not implemented");
3600
3601
1
  case DLT_DOCSIS:
3602
1
    bpf_error(cstate, "DOCSIS link-layer type filtering not implemented");
3603
3604
1
  case DLT_MTP2:
3605
3
  case DLT_MTP2_WITH_PHDR:
3606
3
    bpf_error(cstate, "MTP2 link-layer type filtering not implemented");
3607
3608
1
  case DLT_ERF:
3609
1
    bpf_error(cstate, "ERF link-layer type filtering not implemented");
3610
3611
1
  case DLT_PFSYNC:
3612
1
    bpf_error(cstate, "PFSYNC link-layer type filtering not implemented");
3613
3614
1
  case DLT_LINUX_LAPD:
3615
1
    bpf_error(cstate, "LAPD link-layer type filtering not implemented");
3616
3617
5
  case DLT_USB_FREEBSD:
3618
12
  case DLT_USB_LINUX:
3619
14
  case DLT_USB_LINUX_MMAPPED:
3620
15
  case DLT_USBPCAP:
3621
15
    bpf_error(cstate, "USB link-layer type filtering not implemented");
3622
3623
1
  case DLT_BLUETOOTH_HCI_H4:
3624
3
  case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
3625
3
    bpf_error(cstate, "Bluetooth link-layer type filtering not implemented");
3626
3627
1
  case DLT_CAN20B:
3628
2
  case DLT_CAN_SOCKETCAN:
3629
2
    bpf_error(cstate, "CAN link-layer type filtering not implemented");
3630
3631
1
  case DLT_IEEE802_15_4:
3632
2
  case DLT_IEEE802_15_4_LINUX:
3633
3
  case DLT_IEEE802_15_4_NONASK_PHY:
3634
4
  case DLT_IEEE802_15_4_NOFCS:
3635
5
  case DLT_IEEE802_15_4_TAP:
3636
5
    bpf_error(cstate, "IEEE 802.15.4 link-layer type filtering not implemented");
3637
3638
1
  case DLT_IEEE802_16_MAC_CPS_RADIO:
3639
1
    bpf_error(cstate, "IEEE 802.16 link-layer type filtering not implemented");
3640
3641
3
  case DLT_SITA:
3642
3
    bpf_error(cstate, "SITA link-layer type filtering not implemented");
3643
3644
1
  case DLT_RAIF1:
3645
1
    bpf_error(cstate, "RAIF1 link-layer type filtering not implemented");
3646
3647
2
  case DLT_IPMB_KONTRON:
3648
4
  case DLT_IPMB_LINUX:
3649
4
    bpf_error(cstate, "IPMB link-layer type filtering not implemented");
3650
3651
2
  case DLT_AX25_KISS:
3652
2
    bpf_error(cstate, "AX.25 link-layer type filtering not implemented");
3653
3654
1
  case DLT_NFLOG:
3655
    /* Using the fixed-size NFLOG header it is possible to tell only
3656
     * the address family of the packet, other meaningful data is
3657
     * either missing or behind TLVs.
3658
     */
3659
1
    bpf_error(cstate, "NFLOG link-layer type filtering not implemented");
3660
3661
404
  default:
3662
    /*
3663
     * Does this link-layer header type have a field
3664
     * indicating the type of the next protocol?  If
3665
     * so, off_linktype.constant_part will be the offset of that
3666
     * field in the packet; if not, it will be OFFSET_NOT_SET.
3667
     */
3668
404
    if (cstate->off_linktype.constant_part != OFFSET_NOT_SET) {
3669
      /*
3670
       * Yes; assume it's an Ethernet type.  (If
3671
       * it's not, it needs to be handled specially
3672
       * above.)
3673
       */
3674
347
      return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
3675
      /*NOTREACHED */
3676
347
    } else {
3677
      /*
3678
       * No; report an error.
3679
       */
3680
57
      description = pcap_datalink_val_to_description_or_dlt(cstate->linktype);
3681
57
      bpf_error(cstate, "%s link-layer type filtering not implemented",
3682
57
          description);
3683
      /*NOTREACHED */
3684
57
    }
3685
72.0k
  }
3686
72.0k
}
3687
3688
/*
3689
 * Check for an LLC SNAP packet with a given organization code and
3690
 * protocol type; we check the entire contents of the 802.2 LLC and
3691
 * snap headers, checking for DSAP and SSAP of SNAP and a control
3692
 * field of 0x03 in the LLC header, and for the specified organization
3693
 * code and protocol type in the SNAP header.
3694
 */
3695
static struct block *
3696
gen_snap(compiler_state_t *cstate, bpf_u_int32 orgcode, bpf_u_int32 ptype)
3697
453
{
3698
453
  u_char snapblock[8];
3699
3700
453
  snapblock[0] = LLCSAP_SNAP;   /* DSAP = SNAP */
3701
453
  snapblock[1] = LLCSAP_SNAP;   /* SSAP = SNAP */
3702
453
  snapblock[2] = 0x03;      /* control = UI */
3703
453
  snapblock[3] = (u_char)(orgcode >> 16); /* upper 8 bits of organization code */
3704
453
  snapblock[4] = (u_char)(orgcode >> 8);  /* middle 8 bits of organization code */
3705
453
  snapblock[5] = (u_char)(orgcode >> 0);  /* lower 8 bits of organization code */
3706
453
  snapblock[6] = (u_char)(ptype >> 8);  /* upper 8 bits of protocol type */
3707
453
  snapblock[7] = (u_char)(ptype >> 0);  /* lower 8 bits of protocol type */
3708
453
  return gen_bcmp(cstate, OR_LLC, 0, 8, snapblock);
3709
453
}
3710
3711
/*
3712
 * Generate code to match frames with an LLC header.
3713
 */
3714
static struct block *
3715
gen_llc_internal(compiler_state_t *cstate)
3716
1.21k
{
3717
1.21k
  struct block *b0, *b1;
3718
3719
1.21k
  switch (cstate->linktype) {
3720
3721
274
  case DLT_EN10MB:
3722
    /*
3723
     * We check for an Ethernet type field less than
3724
     * 1500, which means it's an 802.3 length field.
3725
     */
3726
274
    b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
3727
274
    gen_not(b0);
3728
3729
    /*
3730
     * Now check for the purported DSAP and SSAP not being
3731
     * 0xFF, to rule out NetWare-over-802.3.
3732
     */
3733
274
    b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF);
3734
274
    gen_not(b1);
3735
274
    gen_and(b0, b1);
3736
274
    return b1;
3737
3738
88
  case DLT_SUNATM:
3739
    /*
3740
     * We check for LLC traffic.
3741
     */
3742
88
    b0 = gen_atmtype_llc(cstate);
3743
88
    return b0;
3744
3745
83
  case DLT_IEEE802: /* Token Ring */
3746
    /*
3747
     * XXX - check for LLC frames.
3748
     */
3749
83
    return gen_true(cstate);
3750
3751
76
  case DLT_FDDI:
3752
    /*
3753
     * XXX - check for LLC frames.
3754
     */
3755
76
    return gen_true(cstate);
3756
3757
155
  case DLT_ATM_RFC1483:
3758
    /*
3759
     * For LLC encapsulation, these are defined to have an
3760
     * 802.2 LLC header.
3761
     *
3762
     * For VC encapsulation, they don't, but there's no
3763
     * way to check for that; the protocol used on the VC
3764
     * is negotiated out of band.
3765
     */
3766
155
    return gen_true(cstate);
3767
3768
162
  case DLT_IEEE802_11:
3769
280
  case DLT_PRISM_HEADER:
3770
359
  case DLT_IEEE802_11_RADIO:
3771
449
  case DLT_IEEE802_11_RADIO_AVS:
3772
529
  case DLT_PPI:
3773
    /*
3774
     * Check that we have a data frame.
3775
     */
3776
529
    b0 = gen_check_802_11_data_frame(cstate);
3777
529
    return b0;
3778
3779
10
  default:
3780
10
    bpf_error(cstate, "'llc' not supported for %s",
3781
10
        pcap_datalink_val_to_description_or_dlt(cstate->linktype));
3782
    /*NOTREACHED*/
3783
1.21k
  }
3784
1.21k
}
3785
3786
struct block *
3787
gen_llc(compiler_state_t *cstate)
3788
723
{
3789
  /*
3790
   * Catch errors reported by us and routines below us, and return NULL
3791
   * on an error.
3792
   */
3793
723
  if (setjmp(cstate->top_ctx))
3794
4
    return (NULL);
3795
3796
719
  return gen_llc_internal(cstate);
3797
723
}
3798
3799
struct block *
3800
gen_llc_i(compiler_state_t *cstate)
3801
90
{
3802
90
  struct block *b0, *b1;
3803
90
  struct slist *s;
3804
3805
  /*
3806
   * Catch errors reported by us and routines below us, and return NULL
3807
   * on an error.
3808
   */
3809
90
  if (setjmp(cstate->top_ctx))
3810
1
    return (NULL);
3811
3812
  /*
3813
   * Check whether this is an LLC frame.
3814
   */
3815
89
  b0 = gen_llc_internal(cstate);
3816
3817
  /*
3818
   * Load the control byte and test the low-order bit; it must
3819
   * be clear for I frames.
3820
   */
3821
89
  s = gen_load_a(cstate, OR_LLC, 2, BPF_B);
3822
89
  b1 = new_block(cstate, JMP(BPF_JSET));
3823
89
  b1->s.k = 0x01;
3824
89
  b1->stmts = s;
3825
89
  gen_not(b1);
3826
89
  gen_and(b0, b1);
3827
89
  return b1;
3828
90
}
3829
3830
struct block *
3831
gen_llc_s(compiler_state_t *cstate)
3832
100
{
3833
100
  struct block *b0, *b1;
3834
3835
  /*
3836
   * Catch errors reported by us and routines below us, and return NULL
3837
   * on an error.
3838
   */
3839
100
  if (setjmp(cstate->top_ctx))
3840
1
    return (NULL);
3841
3842
  /*
3843
   * Check whether this is an LLC frame.
3844
   */
3845
99
  b0 = gen_llc_internal(cstate);
3846
3847
  /*
3848
   * Now compare the low-order 2 bit of the control byte against
3849
   * the appropriate value for S frames.
3850
   */
3851
99
  b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03);
3852
99
  gen_and(b0, b1);
3853
99
  return b1;
3854
100
}
3855
3856
struct block *
3857
gen_llc_u(compiler_state_t *cstate)
3858
112
{
3859
112
  struct block *b0, *b1;
3860
3861
  /*
3862
   * Catch errors reported by us and routines below us, and return NULL
3863
   * on an error.
3864
   */
3865
112
  if (setjmp(cstate->top_ctx))
3866
1
    return (NULL);
3867
3868
  /*
3869
   * Check whether this is an LLC frame.
3870
   */
3871
111
  b0 = gen_llc_internal(cstate);
3872
3873
  /*
3874
   * Now compare the low-order 2 bit of the control byte against
3875
   * the appropriate value for U frames.
3876
   */
3877
111
  b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03);
3878
111
  gen_and(b0, b1);
3879
111
  return b1;
3880
112
}
3881
3882
struct block *
3883
gen_llc_s_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
3884
97
{
3885
97
  struct block *b0, *b1;
3886
3887
  /*
3888
   * Catch errors reported by us and routines below us, and return NULL
3889
   * on an error.
3890
   */
3891
97
  if (setjmp(cstate->top_ctx))
3892
2
    return (NULL);
3893
3894
  /*
3895
   * Check whether this is an LLC frame.
3896
   */
3897
95
  b0 = gen_llc_internal(cstate);
3898
3899
  /*
3900
   * Now check for an S frame with the appropriate type.
3901
   */
3902
95
  b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK);
3903
95
  gen_and(b0, b1);
3904
95
  return b1;
3905
97
}
3906
3907
struct block *
3908
gen_llc_u_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
3909
93
{
3910
93
  struct block *b0, *b1;
3911
3912
  /*
3913
   * Catch errors reported by us and routines below us, and return NULL
3914
   * on an error.
3915
   */
3916
93
  if (setjmp(cstate->top_ctx))
3917
1
    return (NULL);
3918
3919
  /*
3920
   * Check whether this is an LLC frame.
3921
   */
3922
92
  b0 = gen_llc_internal(cstate);
3923
3924
  /*
3925
   * Now check for a U frame with the appropriate type.
3926
   */
3927
92
  b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK);
3928
92
  gen_and(b0, b1);
3929
92
  return b1;
3930
93
}
3931
3932
/*
3933
 * Generate code to match a particular packet type, for link-layer types
3934
 * using 802.2 LLC headers.
3935
 *
3936
 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3937
 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3938
 *
3939
 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3940
 * value, if <= ETHERMTU.  We use that to determine whether to
3941
 * match the DSAP or both DSAP and LSAP or to check the OUI and
3942
 * protocol ID in a SNAP header.
3943
 */
3944
static struct block *
3945
gen_llc_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
3946
20.7k
{
3947
  /*
3948
   * XXX - handle token-ring variable-length header.
3949
   */
3950
20.7k
  switch (ll_proto) {
3951
3952
72
  case LLCSAP_IP:
3953
1.86k
  case LLCSAP_ISONS:
3954
1.89k
  case LLCSAP_NETBEUI:
3955
    /*
3956
     * XXX - should we check both the DSAP and the
3957
     * SSAP, like this, or should we check just the
3958
     * DSAP, as we do for other SAP values?
3959
     */
3960
1.89k
    return gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_u_int32)
3961
1.89k
           ((ll_proto << 8) | ll_proto));
3962
3963
72
  case LLCSAP_IPX:
3964
    /*
3965
     * XXX - are there ever SNAP frames for IPX on
3966
     * non-Ethernet 802.x networks?
3967
     */
3968
72
    return gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
3969
3970
95
  case ETHERTYPE_ATALK:
3971
    /*
3972
     * 802.2-encapsulated ETHERTYPE_ATALK packets are
3973
     * SNAP packets with an organization code of
3974
     * 0x080007 (Apple, for Appletalk) and a protocol
3975
     * type of ETHERTYPE_ATALK (Appletalk).
3976
     *
3977
     * XXX - check for an organization code of
3978
     * encapsulated Ethernet as well?
3979
     */
3980
95
    return gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
3981
3982
18.7k
  default:
3983
    /*
3984
     * XXX - we don't have to check for IPX 802.3
3985
     * here, but should we check for the IPX Ethertype?
3986
     */
3987
18.7k
    if (ll_proto <= ETHERMTU) {
3988
      /*
3989
       * This is an LLC SAP value, so check
3990
       * the DSAP.
3991
       */
3992
104
      return gen_cmp(cstate, OR_LLC, 0, BPF_B, ll_proto);
3993
18.6k
    } else {
3994
      /*
3995
       * This is an Ethernet type; we assume that it's
3996
       * unlikely that it'll appear in the right place
3997
       * at random, and therefore check only the
3998
       * location that would hold the Ethernet type
3999
       * in a SNAP frame with an organization code of
4000
       * 0x000000 (encapsulated Ethernet).
4001
       *
4002
       * XXX - if we were to check for the SNAP DSAP and
4003
       * LSAP, as per XXX, and were also to check for an
4004
       * organization code of 0x000000 (encapsulated
4005
       * Ethernet), we'd do
4006
       *
4007
       *  return gen_snap(cstate, 0x000000, ll_proto);
4008
       *
4009
       * here; for now, we don't, as per the above.
4010
       * I don't know whether it's worth the extra CPU
4011
       * time to do the right check or not.
4012
       */
4013
18.6k
      return gen_cmp(cstate, OR_LLC, 6, BPF_H, ll_proto);
4014
18.6k
    }
4015
20.7k
  }
4016
20.7k
}
4017
4018
static struct block *
4019
gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
4020
    int dir, bpf_u_int32 ll_proto, u_int src_off, u_int dst_off)
4021
60.6k
{
4022
60.6k
  struct block *b0, *b1;
4023
60.6k
  u_int offset;
4024
4025
60.6k
  switch (dir) {
4026
4027
20.3k
  case Q_SRC:
4028
20.3k
    offset = src_off;
4029
20.3k
    break;
4030
4031
20.6k
  case Q_DST:
4032
20.6k
    offset = dst_off;
4033
20.6k
    break;
4034
4035
1.69k
  case Q_AND:
4036
1.69k
    b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4037
1.69k
    b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4038
1.69k
    gen_and(b0, b1);
4039
1.69k
    return b1;
4040
4041
17.6k
  case Q_DEFAULT:
4042
17.8k
  case Q_OR:
4043
17.8k
    b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4044
17.8k
    b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4045
17.8k
    gen_or(b0, b1);
4046
17.8k
    return b1;
4047
4048
2
  case Q_ADDR1:
4049
2
    bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4050
    /*NOTREACHED*/
4051
4052
2
  case Q_ADDR2:
4053
2
    bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4054
    /*NOTREACHED*/
4055
4056
2
  case Q_ADDR3:
4057
2
    bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4058
    /*NOTREACHED*/
4059
4060
2
  case Q_ADDR4:
4061
2
    bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4062
    /*NOTREACHED*/
4063
4064
6
  case Q_RA:
4065
6
    bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4066
    /*NOTREACHED*/
4067
4068
6
  case Q_TA:
4069
6
    bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4070
    /*NOTREACHED*/
4071
4072
0
  default:
4073
0
    abort();
4074
    /*NOTREACHED*/
4075
60.6k
  }
4076
41.0k
  b0 = gen_linktype(cstate, ll_proto);
4077
41.0k
  b1 = gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, addr, mask);
4078
41.0k
  gen_and(b0, b1);
4079
41.0k
  return b1;
4080
60.6k
}
4081
4082
#ifdef INET6
4083
static struct block *
4084
gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr,
4085
    struct in6_addr *mask, int dir, bpf_u_int32 ll_proto, u_int src_off,
4086
    u_int dst_off)
4087
3.42k
{
4088
3.42k
  struct block *b0, *b1;
4089
3.42k
  u_int offset;
4090
3.42k
  uint32_t *a, *m;
4091
4092
3.42k
  switch (dir) {
4093
4094
1.11k
  case Q_SRC:
4095
1.11k
    offset = src_off;
4096
1.11k
    break;
4097
4098
1.21k
  case Q_DST:
4099
1.21k
    offset = dst_off;
4100
1.21k
    break;
4101
4102
134
  case Q_AND:
4103
134
    b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4104
134
    b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4105
134
    gen_and(b0, b1);
4106
134
    return b1;
4107
4108
829
  case Q_DEFAULT:
4109
949
  case Q_OR:
4110
949
    b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4111
949
    b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4112
949
    gen_or(b0, b1);
4113
949
    return b1;
4114
4115
1
  case Q_ADDR1:
4116
1
    bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4117
    /*NOTREACHED*/
4118
4119
1
  case Q_ADDR2:
4120
1
    bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4121
    /*NOTREACHED*/
4122
4123
1
  case Q_ADDR3:
4124
1
    bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4125
    /*NOTREACHED*/
4126
4127
1
  case Q_ADDR4:
4128
1
    bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4129
    /*NOTREACHED*/
4130
4131
2
  case Q_RA:
4132
2
    bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4133
    /*NOTREACHED*/
4134
4135
2
  case Q_TA:
4136
2
    bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4137
    /*NOTREACHED*/
4138
4139
0
  default:
4140
0
    abort();
4141
    /*NOTREACHED*/
4142
3.42k
  }
4143
  /* this order is important */
4144
2.33k
  a = (uint32_t *)addr;
4145
2.33k
  m = (uint32_t *)mask;
4146
2.33k
  b1 = gen_mcmp(cstate, OR_LINKPL, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
4147
2.33k
  b0 = gen_mcmp(cstate, OR_LINKPL, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
4148
2.33k
  gen_and(b0, b1);
4149
2.33k
  b0 = gen_mcmp(cstate, OR_LINKPL, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
4150
2.33k
  gen_and(b0, b1);
4151
2.33k
  b0 = gen_mcmp(cstate, OR_LINKPL, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
4152
2.33k
  gen_and(b0, b1);
4153
2.33k
  b0 = gen_linktype(cstate, ll_proto);
4154
2.33k
  gen_and(b0, b1);
4155
2.33k
  return b1;
4156
3.42k
}
4157
#endif
4158
4159
static struct block *
4160
gen_ehostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4161
512
{
4162
512
  register struct block *b0, *b1;
4163
4164
512
  switch (dir) {
4165
205
  case Q_SRC:
4166
205
    return gen_bcmp(cstate, OR_LINKHDR, 6, 6, eaddr);
4167
4168
167
  case Q_DST:
4169
167
    return gen_bcmp(cstate, OR_LINKHDR, 0, 6, eaddr);
4170
4171
59
  case Q_AND:
4172
59
    b0 = gen_ehostop(cstate, eaddr, Q_SRC);
4173
59
    b1 = gen_ehostop(cstate, eaddr, Q_DST);
4174
59
    gen_and(b0, b1);
4175
59
    return b1;
4176
4177
34
  case Q_DEFAULT:
4178
73
  case Q_OR:
4179
73
    b0 = gen_ehostop(cstate, eaddr, Q_SRC);
4180
73
    b1 = gen_ehostop(cstate, eaddr, Q_DST);
4181
73
    gen_or(b0, b1);
4182
73
    return b1;
4183
4184
1
  case Q_ADDR1:
4185
1
    bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4186
    /*NOTREACHED*/
4187
4188
1
  case Q_ADDR2:
4189
1
    bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4190
    /*NOTREACHED*/
4191
4192
1
  case Q_ADDR3:
4193
1
    bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4194
    /*NOTREACHED*/
4195
4196
1
  case Q_ADDR4:
4197
1
    bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4198
    /*NOTREACHED*/
4199
4200
3
  case Q_RA:
4201
3
    bpf_error(cstate, "'ra' is only supported on 802.11 with 802.11 headers");
4202
    /*NOTREACHED*/
4203
4204
1
  case Q_TA:
4205
1
    bpf_error(cstate, "'ta' is only supported on 802.11 with 802.11 headers");
4206
    /*NOTREACHED*/
4207
512
  }
4208
0
  abort();
4209
  /*NOTREACHED*/
4210
512
}
4211
4212
/*
4213
 * Like gen_ehostop, but for DLT_FDDI
4214
 */
4215
static struct block *
4216
gen_fhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4217
383
{
4218
383
  struct block *b0, *b1;
4219
4220
383
  switch (dir) {
4221
136
  case Q_SRC:
4222
136
    return gen_bcmp(cstate, OR_LINKHDR, 6 + 1 + cstate->pcap_fddipad, 6, eaddr);
4223
4224
139
  case Q_DST:
4225
139
    return gen_bcmp(cstate, OR_LINKHDR, 0 + 1 + cstate->pcap_fddipad, 6, eaddr);
4226
4227
34
  case Q_AND:
4228
34
    b0 = gen_fhostop(cstate, eaddr, Q_SRC);
4229
34
    b1 = gen_fhostop(cstate, eaddr, Q_DST);
4230
34
    gen_and(b0, b1);
4231
34
    return b1;
4232
4233
34
  case Q_DEFAULT:
4234
68
  case Q_OR:
4235
68
    b0 = gen_fhostop(cstate, eaddr, Q_SRC);
4236
68
    b1 = gen_fhostop(cstate, eaddr, Q_DST);
4237
68
    gen_or(b0, b1);
4238
68
    return b1;
4239
4240
1
  case Q_ADDR1:
4241
1
    bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4242
    /*NOTREACHED*/
4243
4244
1
  case Q_ADDR2:
4245
1
    bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4246
    /*NOTREACHED*/
4247
4248
1
  case Q_ADDR3:
4249
1
    bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4250
    /*NOTREACHED*/
4251
4252
1
  case Q_ADDR4:
4253
1
    bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4254
    /*NOTREACHED*/
4255
4256
1
  case Q_RA:
4257
1
    bpf_error(cstate, "'ra' is only supported on 802.11");
4258
    /*NOTREACHED*/
4259
4260
1
  case Q_TA:
4261
1
    bpf_error(cstate, "'ta' is only supported on 802.11");
4262
    /*NOTREACHED*/
4263
383
  }
4264
0
  abort();
4265
  /*NOTREACHED*/
4266
383
}
4267
4268
/*
4269
 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4270
 */
4271
static struct block *
4272
gen_thostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4273
391
{
4274
391
  register struct block *b0, *b1;
4275
4276
391
  switch (dir) {
4277
139
  case Q_SRC:
4278
139
    return gen_bcmp(cstate, OR_LINKHDR, 8, 6, eaddr);
4279
4280
140
  case Q_DST:
4281
140
    return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
4282
4283
34
  case Q_AND:
4284
34
    b0 = gen_thostop(cstate, eaddr, Q_SRC);
4285
34
    b1 = gen_thostop(cstate, eaddr, Q_DST);
4286
34
    gen_and(b0, b1);
4287
34
    return b1;
4288
4289
36
  case Q_DEFAULT:
4290
71
  case Q_OR:
4291
71
    b0 = gen_thostop(cstate, eaddr, Q_SRC);
4292
71
    b1 = gen_thostop(cstate, eaddr, Q_DST);
4293
71
    gen_or(b0, b1);
4294
71
    return b1;
4295
4296
1
  case Q_ADDR1:
4297
1
    bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4298
    /*NOTREACHED*/
4299
4300
1
  case Q_ADDR2:
4301
1
    bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4302
    /*NOTREACHED*/
4303
4304
1
  case Q_ADDR3:
4305
1
    bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4306
    /*NOTREACHED*/
4307
4308
1
  case Q_ADDR4:
4309
1
    bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4310
    /*NOTREACHED*/
4311
4312
2
  case Q_RA:
4313
2
    bpf_error(cstate, "'ra' is only supported on 802.11");
4314
    /*NOTREACHED*/
4315
4316
1
  case Q_TA:
4317
1
    bpf_error(cstate, "'ta' is only supported on 802.11");
4318
    /*NOTREACHED*/
4319
391
  }
4320
0
  abort();
4321
  /*NOTREACHED*/
4322
391
}
4323
4324
/*
4325
 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4326
 * various 802.11 + radio headers.
4327
 */
4328
static struct block *
4329
gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4330
819
{
4331
819
  register struct block *b0, *b1, *b2;
4332
819
  register struct slist *s;
4333
4334
#ifdef ENABLE_WLAN_FILTERING_PATCH
4335
  /*
4336
   * TODO GV 20070613
4337
   * We need to disable the optimizer because the optimizer is buggy
4338
   * and wipes out some LD instructions generated by the below
4339
   * code to validate the Frame Control bits
4340
   */
4341
  cstate->no_optimize = 1;
4342
#endif /* ENABLE_WLAN_FILTERING_PATCH */
4343
4344
819
  switch (dir) {
4345
204
  case Q_SRC:
4346
    /*
4347
     * Oh, yuk.
4348
     *
4349
     *  For control frames, there is no SA.
4350
     *
4351
     *  For management frames, SA is at an
4352
     *  offset of 10 from the beginning of
4353
     *  the packet.
4354
     *
4355
     *  For data frames, SA is at an offset
4356
     *  of 10 from the beginning of the packet
4357
     *  if From DS is clear, at an offset of
4358
     *  16 from the beginning of the packet
4359
     *  if From DS is set and To DS is clear,
4360
     *  and an offset of 24 from the beginning
4361
     *  of the packet if From DS is set and To DS
4362
     *  is set.
4363
     */
4364
4365
    /*
4366
     * Generate the tests to be done for data frames
4367
     * with From DS set.
4368
     *
4369
     * First, check for To DS set, i.e. check "link[1] & 0x01".
4370
     */
4371
204
    s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4372
204
    b1 = new_block(cstate, JMP(BPF_JSET));
4373
204
    b1->s.k = 0x01; /* To DS */
4374
204
    b1->stmts = s;
4375
4376
    /*
4377
     * If To DS is set, the SA is at 24.
4378
     */
4379
204
    b0 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
4380
204
    gen_and(b1, b0);
4381
4382
    /*
4383
     * Now, check for To DS not set, i.e. check
4384
     * "!(link[1] & 0x01)".
4385
     */
4386
204
    s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4387
204
    b2 = new_block(cstate, JMP(BPF_JSET));
4388
204
    b2->s.k = 0x01; /* To DS */
4389
204
    b2->stmts = s;
4390
204
    gen_not(b2);
4391
4392
    /*
4393
     * If To DS is not set, the SA is at 16.
4394
     */
4395
204
    b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4396
204
    gen_and(b2, b1);
4397
4398
    /*
4399
     * Now OR together the last two checks.  That gives
4400
     * the complete set of checks for data frames with
4401
     * From DS set.
4402
     */
4403
204
    gen_or(b1, b0);
4404
4405
    /*
4406
     * Now check for From DS being set, and AND that with
4407
     * the ORed-together checks.
4408
     */
4409
204
    s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4410
204
    b1 = new_block(cstate, JMP(BPF_JSET));
4411
204
    b1->s.k = 0x02; /* From DS */
4412
204
    b1->stmts = s;
4413
204
    gen_and(b1, b0);
4414
4415
    /*
4416
     * Now check for data frames with From DS not set.
4417
     */
4418
204
    s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4419
204
    b2 = new_block(cstate, JMP(BPF_JSET));
4420
204
    b2->s.k = 0x02; /* From DS */
4421
204
    b2->stmts = s;
4422
204
    gen_not(b2);
4423
4424
    /*
4425
     * If From DS isn't set, the SA is at 10.
4426
     */
4427
204
    b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4428
204
    gen_and(b2, b1);
4429
4430
    /*
4431
     * Now OR together the checks for data frames with
4432
     * From DS not set and for data frames with From DS
4433
     * set; that gives the checks done for data frames.
4434
     */
4435
204
    gen_or(b1, b0);
4436
4437
    /*
4438
     * Now check for a data frame.
4439
     * I.e, check "link[0] & 0x08".
4440
     */
4441
204
    s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4442
204
    b1 = new_block(cstate, JMP(BPF_JSET));
4443
204
    b1->s.k = 0x08;
4444
204
    b1->stmts = s;
4445
4446
    /*
4447
     * AND that with the checks done for data frames.
4448
     */
4449
204
    gen_and(b1, b0);
4450
4451
    /*
4452
     * If the high-order bit of the type value is 0, this
4453
     * is a management frame.
4454
     * I.e, check "!(link[0] & 0x08)".
4455
     */
4456
204
    s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4457
204
    b2 = new_block(cstate, JMP(BPF_JSET));
4458
204
    b2->s.k = 0x08;
4459
204
    b2->stmts = s;
4460
204
    gen_not(b2);
4461
4462
    /*
4463
     * For management frames, the SA is at 10.
4464
     */
4465
204
    b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4466
204
    gen_and(b2, b1);
4467
4468
    /*
4469
     * OR that with the checks done for data frames.
4470
     * That gives the checks done for management and
4471
     * data frames.
4472
     */
4473
204
    gen_or(b1, b0);
4474
4475
    /*
4476
     * If the low-order bit of the type value is 1,
4477
     * this is either a control frame or a frame
4478
     * with a reserved type, and thus not a
4479
     * frame with an SA.
4480
     *
4481
     * I.e., check "!(link[0] & 0x04)".
4482
     */
4483
204
    s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4484
204
    b1 = new_block(cstate, JMP(BPF_JSET));
4485
204
    b1->s.k = 0x04;
4486
204
    b1->stmts = s;
4487
204
    gen_not(b1);
4488
4489
    /*
4490
     * AND that with the checks for data and management
4491
     * frames.
4492
     */
4493
204
    gen_and(b1, b0);
4494
204
    return b0;
4495
4496
165
  case Q_DST:
4497
    /*
4498
     * Oh, yuk.
4499
     *
4500
     *  For control frames, there is no DA.
4501
     *
4502
     *  For management frames, DA is at an
4503
     *  offset of 4 from the beginning of
4504
     *  the packet.
4505
     *
4506
     *  For data frames, DA is at an offset
4507
     *  of 4 from the beginning of the packet
4508
     *  if To DS is clear and at an offset of
4509
     *  16 from the beginning of the packet
4510
     *  if To DS is set.
4511
     */
4512
4513
    /*
4514
     * Generate the tests to be done for data frames.
4515
     *
4516
     * First, check for To DS set, i.e. "link[1] & 0x01".
4517
     */
4518
165
    s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4519
165
    b1 = new_block(cstate, JMP(BPF_JSET));
4520
165
    b1->s.k = 0x01; /* To DS */
4521
165
    b1->stmts = s;
4522
4523
    /*
4524
     * If To DS is set, the DA is at 16.
4525
     */
4526
165
    b0 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4527
165
    gen_and(b1, b0);
4528
4529
    /*
4530
     * Now, check for To DS not set, i.e. check
4531
     * "!(link[1] & 0x01)".
4532
     */
4533
165
    s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4534
165
    b2 = new_block(cstate, JMP(BPF_JSET));
4535
165
    b2->s.k = 0x01; /* To DS */
4536
165
    b2->stmts = s;
4537
165
    gen_not(b2);
4538
4539
    /*
4540
     * If To DS is not set, the DA is at 4.
4541
     */
4542
165
    b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4543
165
    gen_and(b2, b1);
4544
4545
    /*
4546
     * Now OR together the last two checks.  That gives
4547
     * the complete set of checks for data frames.
4548
     */
4549
165
    gen_or(b1, b0);
4550
4551
    /*
4552
     * Now check for a data frame.
4553
     * I.e, check "link[0] & 0x08".
4554
     */
4555
165
    s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4556
165
    b1 = new_block(cstate, JMP(BPF_JSET));
4557
165
    b1->s.k = 0x08;
4558
165
    b1->stmts = s;
4559
4560
    /*
4561
     * AND that with the checks done for data frames.
4562
     */
4563
165
    gen_and(b1, b0);
4564
4565
    /*
4566
     * If the high-order bit of the type value is 0, this
4567
     * is a management frame.
4568
     * I.e, check "!(link[0] & 0x08)".
4569
     */
4570
165
    s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4571
165
    b2 = new_block(cstate, JMP(BPF_JSET));
4572
165
    b2->s.k = 0x08;
4573
165
    b2->stmts = s;
4574
165
    gen_not(b2);
4575
4576
    /*
4577
     * For management frames, the DA is at 4.
4578
     */
4579
165
    b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4580
165
    gen_and(b2, b1);
4581
4582
    /*
4583
     * OR that with the checks done for data frames.
4584
     * That gives the checks done for management and
4585
     * data frames.
4586
     */
4587
165
    gen_or(b1, b0);
4588
4589
    /*
4590
     * If the low-order bit of the type value is 1,
4591
     * this is either a control frame or a frame
4592
     * with a reserved type, and thus not a
4593
     * frame with an SA.
4594
     *
4595
     * I.e., check "!(link[0] & 0x04)".
4596
     */
4597
165
    s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4598
165
    b1 = new_block(cstate, JMP(BPF_JSET));
4599
165
    b1->s.k = 0x04;
4600
165
    b1->stmts = s;
4601
165
    gen_not(b1);
4602
4603
    /*
4604
     * AND that with the checks for data and management
4605
     * frames.
4606
     */
4607
165
    gen_and(b1, b0);
4608
165
    return b0;
4609
4610
55
  case Q_AND:
4611
55
    b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
4612
55
    b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
4613
55
    gen_and(b0, b1);
4614
55
    return b1;
4615
4616
47
  case Q_DEFAULT:
4617
81
  case Q_OR:
4618
81
    b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
4619
81
    b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
4620
81
    gen_or(b0, b1);
4621
81
    return b1;
4622
4623
  /*
4624
   * XXX - add BSSID keyword?
4625
   */
4626
51
  case Q_ADDR1:
4627
51
    return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr));
4628
4629
52
  case Q_ADDR2:
4630
    /*
4631
     * Not present in CTS or ACK control frames.
4632
     */
4633
52
    b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4634
52
      IEEE80211_FC0_TYPE_MASK);
4635
52
    gen_not(b0);
4636
52
    b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4637
52
      IEEE80211_FC0_SUBTYPE_MASK);
4638
52
    gen_not(b1);
4639
52
    b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4640
52
      IEEE80211_FC0_SUBTYPE_MASK);
4641
52
    gen_not(b2);
4642
52
    gen_and(b1, b2);
4643
52
    gen_or(b0, b2);
4644
52
    b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4645
52
    gen_and(b2, b1);
4646
52
    return b1;
4647
4648
44
  case Q_ADDR3:
4649
    /*
4650
     * Not present in control frames.
4651
     */
4652
44
    b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4653
44
      IEEE80211_FC0_TYPE_MASK);
4654
44
    gen_not(b0);
4655
44
    b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4656
44
    gen_and(b0, b1);
4657
44
    return b1;
4658
4659
44
  case Q_ADDR4:
4660
    /*
4661
     * Present only if the direction mask has both "From DS"
4662
     * and "To DS" set.  Neither control frames nor management
4663
     * frames should have both of those set, so we don't
4664
     * check the frame type.
4665
     */
4666
44
    b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B,
4667
44
      IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
4668
44
    b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
4669
44
    gen_and(b0, b1);
4670
44
    return b1;
4671
4672
62
  case Q_RA:
4673
    /*
4674
     * Not present in management frames; addr1 in other
4675
     * frames.
4676
     */
4677
4678
    /*
4679
     * If the high-order bit of the type value is 0, this
4680
     * is a management frame.
4681
     * I.e, check "(link[0] & 0x08)".
4682
     */
4683
62
    s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4684
62
    b1 = new_block(cstate, JMP(BPF_JSET));
4685
62
    b1->s.k = 0x08;
4686
62
    b1->stmts = s;
4687
4688
    /*
4689
     * Check addr1.
4690
     */
4691
62
    b0 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4692
4693
    /*
4694
     * AND that with the check of addr1.
4695
     */
4696
62
    gen_and(b1, b0);
4697
62
    return (b0);
4698
4699
61
  case Q_TA:
4700
    /*
4701
     * Not present in management frames; addr2, if present,
4702
     * in other frames.
4703
     */
4704
4705
    /*
4706
     * Not present in CTS or ACK control frames.
4707
     */
4708
61
    b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4709
61
      IEEE80211_FC0_TYPE_MASK);
4710
61
    gen_not(b0);
4711
61
    b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4712
61
      IEEE80211_FC0_SUBTYPE_MASK);
4713
61
    gen_not(b1);
4714
61
    b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4715
61
      IEEE80211_FC0_SUBTYPE_MASK);
4716
61
    gen_not(b2);
4717
61
    gen_and(b1, b2);
4718
61
    gen_or(b0, b2);
4719
4720
    /*
4721
     * If the high-order bit of the type value is 0, this
4722
     * is a management frame.
4723
     * I.e, check "(link[0] & 0x08)".
4724
     */
4725
61
    s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4726
61
    b1 = new_block(cstate, JMP(BPF_JSET));
4727
61
    b1->s.k = 0x08;
4728
61
    b1->stmts = s;
4729
4730
    /*
4731
     * AND that with the check for frames other than
4732
     * CTS and ACK frames.
4733
     */
4734
61
    gen_and(b1, b2);
4735
4736
    /*
4737
     * Check addr2.
4738
     */
4739
61
    b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4740
61
    gen_and(b2, b1);
4741
61
    return b1;
4742
819
  }
4743
0
  abort();
4744
  /*NOTREACHED*/
4745
819
}
4746
4747
/*
4748
 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4749
 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4750
 * as the RFC states.)
4751
 */
4752
static struct block *
4753
gen_ipfchostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4754
381
{
4755
381
  register struct block *b0, *b1;
4756
4757
381
  switch (dir) {
4758
136
  case Q_SRC:
4759
136
    return gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4760
4761
137
  case Q_DST:
4762
137
    return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
4763
4764
34
  case Q_AND:
4765
34
    b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
4766
34
    b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
4767
34
    gen_and(b0, b1);
4768
34
    return b1;
4769
4770
34
  case Q_DEFAULT:
4771
68
  case Q_OR:
4772
68
    b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
4773
68
    b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
4774
68
    gen_or(b0, b1);
4775
68
    return b1;
4776
4777
1
  case Q_ADDR1:
4778
1
    bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4779
    /*NOTREACHED*/
4780
4781
1
  case Q_ADDR2:
4782
1
    bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4783
    /*NOTREACHED*/
4784
4785
1
  case Q_ADDR3:
4786
1
    bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4787
    /*NOTREACHED*/
4788
4789
1
  case Q_ADDR4:
4790
1
    bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4791
    /*NOTREACHED*/
4792
4793
1
  case Q_RA:
4794
1
    bpf_error(cstate, "'ra' is only supported on 802.11");
4795
    /*NOTREACHED*/
4796
4797
1
  case Q_TA:
4798
1
    bpf_error(cstate, "'ta' is only supported on 802.11");
4799
    /*NOTREACHED*/
4800
381
  }
4801
0
  abort();
4802
  /*NOTREACHED*/
4803
381
}
4804
4805
/*
4806
 * This is quite tricky because there may be pad bytes in front of the
4807
 * DECNET header, and then there are two possible data packet formats that
4808
 * carry both src and dst addresses, plus 5 packet types in a format that
4809
 * carries only the src node, plus 2 types that use a different format and
4810
 * also carry just the src node.
4811
 *
4812
 * Yuck.
4813
 *
4814
 * Instead of doing those all right, we just look for data packets with
4815
 * 0 or 1 bytes of padding.  If you want to look at other packets, that
4816
 * will require a lot more hacking.
4817
 *
4818
 * To add support for filtering on DECNET "areas" (network numbers)
4819
 * one would want to add a "mask" argument to this routine.  That would
4820
 * make the filter even more inefficient, although one could be clever
4821
 * and not generate masking instructions if the mask is 0xFFFF.
4822
 */
4823
static struct block *
4824
gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir)
4825
2.02k
{
4826
2.02k
  struct block *b0, *b1, *b2, *tmp;
4827
2.02k
  u_int offset_lh;  /* offset if long header is received */
4828
2.02k
  u_int offset_sh;  /* offset if short header is received */
4829
4830
2.02k
  switch (dir) {
4831
4832
695
  case Q_DST:
4833
695
    offset_sh = 1;  /* follows flags */
4834
695
    offset_lh = 7;  /* flgs,darea,dsubarea,HIORD */
4835
695
    break;
4836
4837
668
  case Q_SRC:
4838
668
    offset_sh = 3;  /* follows flags, dstnode */
4839
668
    offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4840
668
    break;
4841
4842
118
  case Q_AND:
4843
    /* Inefficient because we do our Calvinball dance twice */
4844
118
    b0 = gen_dnhostop(cstate, addr, Q_SRC);
4845
118
    b1 = gen_dnhostop(cstate, addr, Q_DST);
4846
118
    gen_and(b0, b1);
4847
118
    return b1;
4848
4849
267
  case Q_DEFAULT:
4850
537
  case Q_OR:
4851
    /* Inefficient because we do our Calvinball dance twice */
4852
537
    b0 = gen_dnhostop(cstate, addr, Q_SRC);
4853
537
    b1 = gen_dnhostop(cstate, addr, Q_DST);
4854
537
    gen_or(b0, b1);
4855
537
    return b1;
4856
4857
1
  case Q_ADDR1:
4858
1
    bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4859
    /*NOTREACHED*/
4860
4861
1
  case Q_ADDR2:
4862
1
    bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4863
    /*NOTREACHED*/
4864
4865
1
  case Q_ADDR3:
4866
1
    bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4867
    /*NOTREACHED*/
4868
4869
1
  case Q_ADDR4:
4870
1
    bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4871
    /*NOTREACHED*/
4872
4873
1
  case Q_RA:
4874
1
    bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4875
    /*NOTREACHED*/
4876
4877
1
  case Q_TA:
4878
1
    bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4879
    /*NOTREACHED*/
4880
4881
0
  default:
4882
0
    abort();
4883
    /*NOTREACHED*/
4884
2.02k
  }
4885
1.36k
  b0 = gen_linktype(cstate, ETHERTYPE_DN);
4886
  /* Check for pad = 1, long header case */
4887
1.36k
  tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
4888
1.36k
      (bpf_u_int32)ntohs(0x0681), (bpf_u_int32)ntohs(0x07FF));
4889
1.36k
  b1 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_lh,
4890
1.36k
      BPF_H, (bpf_u_int32)ntohs((u_short)addr));
4891
1.36k
  gen_and(tmp, b1);
4892
  /* Check for pad = 0, long header case */
4893
1.36k
  tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x06,
4894
1.36k
      (bpf_u_int32)0x7);
4895
1.36k
  b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_lh, BPF_H,
4896
1.36k
      (bpf_u_int32)ntohs((u_short)addr));
4897
1.36k
  gen_and(tmp, b2);
4898
1.36k
  gen_or(b2, b1);
4899
  /* Check for pad = 1, short header case */
4900
1.36k
  tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
4901
1.36k
      (bpf_u_int32)ntohs(0x0281), (bpf_u_int32)ntohs(0x07FF));
4902
1.36k
  b2 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_sh, BPF_H,
4903
1.36k
      (bpf_u_int32)ntohs((u_short)addr));
4904
1.36k
  gen_and(tmp, b2);
4905
1.36k
  gen_or(b2, b1);
4906
  /* Check for pad = 0, short header case */
4907
1.36k
  tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x02,
4908
1.36k
      (bpf_u_int32)0x7);
4909
1.36k
  b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_sh, BPF_H,
4910
1.36k
      (bpf_u_int32)ntohs((u_short)addr));
4911
1.36k
  gen_and(tmp, b2);
4912
1.36k
  gen_or(b2, b1);
4913
4914
  /* Combine with test for cstate->linktype */
4915
1.36k
  gen_and(b0, b1);
4916
1.36k
  return b1;
4917
2.02k
}
4918
4919
/*
4920
 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4921
 * test the bottom-of-stack bit, and then check the version number
4922
 * field in the IP header.
4923
 */
4924
static struct block *
4925
gen_mpls_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
4926
788
{
4927
788
  struct block *b0, *b1;
4928
4929
788
        switch (ll_proto) {
4930
4931
545
        case ETHERTYPE_IP:
4932
                /* match the bottom-of-stack bit */
4933
545
                b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
4934
                /* match the IPv4 version number */
4935
545
                b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x40, 0xf0);
4936
545
                gen_and(b0, b1);
4937
545
                return b1;
4938
4939
144
        case ETHERTYPE_IPV6:
4940
                /* match the bottom-of-stack bit */
4941
144
                b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
4942
                /* match the IPv4 version number */
4943
144
                b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x60, 0xf0);
4944
144
                gen_and(b0, b1);
4945
144
                return b1;
4946
4947
99
        default:
4948
               /* FIXME add other L3 proto IDs */
4949
99
               bpf_error(cstate, "unsupported protocol over mpls");
4950
               /*NOTREACHED*/
4951
788
        }
4952
788
}
4953
4954
static struct block *
4955
gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
4956
    int proto, int dir, int type)
4957
29.4k
{
4958
29.4k
  struct block *b0, *b1;
4959
29.4k
  const char *typestr;
4960
4961
29.4k
  if (type == Q_NET)
4962
22.9k
    typestr = "net";
4963
6.50k
  else
4964
6.50k
    typestr = "host";
4965
4966
29.4k
  switch (proto) {
4967
4968
7.20k
  case Q_DEFAULT:
4969
7.20k
    b0 = gen_host(cstate, addr, mask, Q_IP, dir, type);
4970
    /*
4971
     * Only check for non-IPv4 addresses if we're not
4972
     * checking MPLS-encapsulated packets.
4973
     */
4974
7.20k
    if (cstate->label_stack_depth == 0) {
4975
6.92k
      b1 = gen_host(cstate, addr, mask, Q_ARP, dir, type);
4976
6.92k
      gen_or(b0, b1);
4977
6.92k
      b0 = gen_host(cstate, addr, mask, Q_RARP, dir, type);
4978
6.92k
      gen_or(b1, b0);
4979
6.92k
    }
4980
7.20k
    return b0;
4981
4982
1
  case Q_LINK:
4983
1
    bpf_error(cstate, "link-layer modifier applied to %s", typestr);
4984
4985
7.39k
  case Q_IP:
4986
7.39k
    return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_IP, 12, 16);
4987
4988
7.01k
  case Q_RARP:
4989
7.01k
    return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
4990
4991
7.03k
  case Q_ARP:
4992
7.03k
    return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_ARP, 14, 24);
4993
4994
1
  case Q_SCTP:
4995
1
    bpf_error(cstate, "'sctp' modifier applied to %s", typestr);
4996
4997
1
  case Q_TCP:
4998
1
    bpf_error(cstate, "'tcp' modifier applied to %s", typestr);
4999
5000
1
  case Q_UDP:
5001
1
    bpf_error(cstate, "'udp' modifier applied to %s", typestr);
5002
5003
2
  case Q_ICMP:
5004
2
    bpf_error(cstate, "'icmp' modifier applied to %s", typestr);
5005
5006
2
  case Q_IGMP:
5007
2
    bpf_error(cstate, "'igmp' modifier applied to %s", typestr);
5008
5009
1
  case Q_IGRP:
5010
1
    bpf_error(cstate, "'igrp' modifier applied to %s", typestr);
5011
5012
1
  case Q_ATALK:
5013
1
    bpf_error(cstate, "AppleTalk host filtering not implemented");
5014
5015
715
  case Q_DECNET:
5016
715
    return gen_dnhostop(cstate, addr, dir);
5017
5018
2
  case Q_LAT:
5019
2
    bpf_error(cstate, "LAT host filtering not implemented");
5020
5021
2
  case Q_SCA:
5022
2
    bpf_error(cstate, "SCA host filtering not implemented");
5023
5024
2
  case Q_MOPRC:
5025
2
    bpf_error(cstate, "MOPRC host filtering not implemented");
5026
5027
1
  case Q_MOPDL:
5028
1
    bpf_error(cstate, "MOPDL host filtering not implemented");
5029
5030
1
  case Q_IPV6:
5031
1
    bpf_error(cstate, "'ip6' modifier applied to ip host");
5032
5033
1
  case Q_ICMPV6:
5034
1
    bpf_error(cstate, "'icmp6' modifier applied to %s", typestr);
5035
5036
2
  case Q_AH:
5037
2
    bpf_error(cstate, "'ah' modifier applied to %s", typestr);
5038
5039
1
  case Q_ESP:
5040
1
    bpf_error(cstate, "'esp' modifier applied to %s", typestr);
5041
5042
2
  case Q_PIM:
5043
2
    bpf_error(cstate, "'pim' modifier applied to %s", typestr);
5044
5045
1
  case Q_VRRP:
5046
1
    bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);
5047
5048
1
  case Q_AARP:
5049
1
    bpf_error(cstate, "AARP host filtering not implemented");
5050
5051
1
  case Q_ISO:
5052
1
    bpf_error(cstate, "ISO host filtering not implemented");
5053
5054
1
  case Q_ESIS:
5055
1
    bpf_error(cstate, "'esis' modifier applied to %s", typestr);
5056
5057
1
  case Q_ISIS:
5058
1
    bpf_error(cstate, "'isis' modifier applied to %s", typestr);
5059
5060
1
  case Q_CLNP:
5061
1
    bpf_error(cstate, "'clnp' modifier applied to %s", typestr);
5062
5063
1
  case Q_STP:
5064
1
    bpf_error(cstate, "'stp' modifier applied to %s", typestr);
5065
5066
2
  case Q_IPX:
5067
2
    bpf_error(cstate, "IPX host filtering not implemented");
5068
5069
2
  case Q_NETBEUI:
5070
2
    bpf_error(cstate, "'netbeui' modifier applied to %s", typestr);
5071
5072
2
  case Q_ISIS_L1:
5073
2
    bpf_error(cstate, "'l1' modifier applied to %s", typestr);
5074
5075
2
  case Q_ISIS_L2:
5076
2
    bpf_error(cstate, "'l2' modifier applied to %s", typestr);
5077
5078
1
  case Q_ISIS_IIH:
5079
1
    bpf_error(cstate, "'iih' modifier applied to %s", typestr);
5080
5081
2
  case Q_ISIS_SNP:
5082
2
    bpf_error(cstate, "'snp' modifier applied to %s", typestr);
5083
5084
2
  case Q_ISIS_CSNP:
5085
2
    bpf_error(cstate, "'csnp' modifier applied to %s", typestr);
5086
5087
1
  case Q_ISIS_PSNP:
5088
1
    bpf_error(cstate, "'psnp' modifier applied to %s", typestr);
5089
5090
1
  case Q_ISIS_LSP:
5091
1
    bpf_error(cstate, "'lsp' modifier applied to %s", typestr);
5092
5093
1
  case Q_RADIO:
5094
1
    bpf_error(cstate, "'radio' modifier applied to %s", typestr);
5095
5096
1
  case Q_CARP:
5097
1
    bpf_error(cstate, "'carp' modifier applied to %s", typestr);
5098
5099
0
  default:
5100
0
    abort();
5101
29.4k
  }
5102
  /*NOTREACHED*/
5103
29.4k
}
5104
5105
#ifdef INET6
5106
static struct block *
5107
gen_host6(compiler_state_t *cstate, struct in6_addr *addr,
5108
    struct in6_addr *mask, int proto, int dir, int type)
5109
2.39k
{
5110
2.39k
  const char *typestr;
5111
5112
2.39k
  if (type == Q_NET)
5113
1.41k
    typestr = "net";
5114
983
  else
5115
983
    typestr = "host";
5116
5117
2.39k
  switch (proto) {
5118
5119
1.10k
  case Q_DEFAULT:
5120
1.10k
    return gen_host6(cstate, addr, mask, Q_IPV6, dir, type);
5121
5122
1
  case Q_LINK:
5123
1
    bpf_error(cstate, "link-layer modifier applied to ip6 %s", typestr);
5124
5125
1
  case Q_IP:
5126
1
    bpf_error(cstate, "'ip' modifier applied to ip6 %s", typestr);
5127
5128
1
  case Q_RARP:
5129
1
    bpf_error(cstate, "'rarp' modifier applied to ip6 %s", typestr);
5130
5131
1
  case Q_ARP:
5132
1
    bpf_error(cstate, "'arp' modifier applied to ip6 %s", typestr);
5133
5134
1
  case Q_SCTP:
5135
1
    bpf_error(cstate, "'sctp' modifier applied to ip6 %s", typestr);
5136
5137
1
  case Q_TCP:
5138
1
    bpf_error(cstate, "'tcp' modifier applied to ip6 %s", typestr);
5139
5140
1
  case Q_UDP:
5141
1
    bpf_error(cstate, "'udp' modifier applied to ip6 %s", typestr);
5142
5143
1
  case Q_ICMP:
5144
1
    bpf_error(cstate, "'icmp' modifier applied to ip6 %s", typestr);
5145
5146
1
  case Q_IGMP:
5147
1
    bpf_error(cstate, "'igmp' modifier applied to ip6 %s", typestr);
5148
5149
1
  case Q_IGRP:
5150
1
    bpf_error(cstate, "'igrp' modifier applied to ip6 %s", typestr);
5151
5152
1
  case Q_ATALK:
5153
1
    bpf_error(cstate, "AppleTalk modifier applied to ip6 %s", typestr);
5154
5155
1
  case Q_DECNET:
5156
1
    bpf_error(cstate, "'decnet' modifier applied to ip6 %s", typestr);
5157
5158
1
  case Q_LAT:
5159
1
    bpf_error(cstate, "'lat' modifier applied to ip6 %s", typestr);
5160
5161
1
  case Q_SCA:
5162
1
    bpf_error(cstate, "'sca' modifier applied to ip6 %s", typestr);
5163
5164
1
  case Q_MOPRC:
5165
1
    bpf_error(cstate, "'moprc' modifier applied to ip6 %s", typestr);
5166
5167
1
  case Q_MOPDL:
5168
1
    bpf_error(cstate, "'mopdl' modifier applied to ip6 %s", typestr);
5169
5170
1.25k
  case Q_IPV6:
5171
1.25k
    return gen_hostop6(cstate, addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
5172
5173
1
  case Q_ICMPV6:
5174
1
    bpf_error(cstate, "'icmp6' modifier applied to ip6 %s", typestr);
5175
5176
1
  case Q_AH:
5177
1
    bpf_error(cstate, "'ah' modifier applied to ip6 %s", typestr);
5178
5179
1
  case Q_ESP:
5180
1
    bpf_error(cstate, "'esp' modifier applied to ip6 %s", typestr);
5181
5182
1
  case Q_PIM:
5183
1
    bpf_error(cstate, "'pim' modifier applied to ip6 %s", typestr);
5184
5185
1
  case Q_VRRP:
5186
1
    bpf_error(cstate, "'vrrp' modifier applied to ip6 %s", typestr);
5187
5188
1
  case Q_AARP:
5189
1
    bpf_error(cstate, "'aarp' modifier applied to ip6 %s", typestr);
5190
5191
1
  case Q_ISO:
5192
1
    bpf_error(cstate, "'iso' modifier applied to ip6 %s", typestr);
5193
5194
1
  case Q_ESIS:
5195
1
    bpf_error(cstate, "'esis' modifier applied to ip6 %s", typestr);
5196
5197
1
  case Q_ISIS:
5198
1
    bpf_error(cstate, "'isis' modifier applied to ip6 %s", typestr);
5199
5200
1
  case Q_CLNP:
5201
1
    bpf_error(cstate, "'clnp' modifier applied to ip6 %s", typestr);
5202
5203
1
  case Q_STP:
5204
1
    bpf_error(cstate, "'stp' modifier applied to ip6 %s", typestr);
5205
5206
1
  case Q_IPX:
5207
1
    bpf_error(cstate, "'ipx' modifier applied to ip6 %s", typestr);
5208
5209
1
  case Q_NETBEUI:
5210
1
    bpf_error(cstate, "'netbeui' modifier applied to ip6 %s", typestr);
5211
5212
1
  case Q_ISIS_L1:
5213
1
    bpf_error(cstate, "'l1' modifier applied to ip6 %s", typestr);
5214
5215
1
  case Q_ISIS_L2:
5216
1
    bpf_error(cstate, "'l2' modifier applied to ip6 %s", typestr);
5217
5218
1
  case Q_ISIS_IIH:
5219
1
    bpf_error(cstate, "'iih' modifier applied to ip6 %s", typestr);
5220
5221
1
  case Q_ISIS_SNP:
5222
1
    bpf_error(cstate, "'snp' modifier applied to ip6 %s", typestr);
5223
5224
1
  case Q_ISIS_CSNP:
5225
1
    bpf_error(cstate, "'csnp' modifier applied to ip6 %s", typestr);
5226
5227
1
  case Q_ISIS_PSNP:
5228
1
    bpf_error(cstate, "'psnp' modifier applied to ip6 %s", typestr);
5229
5230
1
  case Q_ISIS_LSP:
5231
1
    bpf_error(cstate, "'lsp' modifier applied to ip6 %s", typestr);
5232
5233
1
  case Q_RADIO:
5234
1
    bpf_error(cstate, "'radio' modifier applied to ip6 %s", typestr);
5235
5236
1
  case Q_CARP:
5237
1
    bpf_error(cstate, "'carp' modifier applied to ip6 %s", typestr);
5238
5239
0
  default:
5240
0
    abort();
5241
2.39k
  }
5242
  /*NOTREACHED*/
5243
2.39k
}
5244
#endif
5245
5246
#ifndef INET6
5247
static struct block *
5248
gen_gateway(compiler_state_t *cstate, const u_char *eaddr,
5249
    struct addrinfo *alist, int proto, int dir)
5250
{
5251
  struct block *b0, *b1, *tmp;
5252
  struct addrinfo *ai;
5253
  struct sockaddr_in *sin;
5254
5255
  if (dir != 0)
5256
    bpf_error(cstate, "direction applied to 'gateway'");
5257
5258
  switch (proto) {
5259
  case Q_DEFAULT:
5260
  case Q_IP:
5261
  case Q_ARP:
5262
  case Q_RARP:
5263
    switch (cstate->linktype) {
5264
    case DLT_EN10MB:
5265
    case DLT_NETANALYZER:
5266
    case DLT_NETANALYZER_TRANSPARENT:
5267
      b1 = gen_prevlinkhdr_check(cstate);
5268
      b0 = gen_ehostop(cstate, eaddr, Q_OR);
5269
      if (b1 != NULL)
5270
        gen_and(b1, b0);
5271
      break;
5272
    case DLT_FDDI:
5273
      b0 = gen_fhostop(cstate, eaddr, Q_OR);
5274
      break;
5275
    case DLT_IEEE802:
5276
      b0 = gen_thostop(cstate, eaddr, Q_OR);
5277
      break;
5278
    case DLT_IEEE802_11:
5279
    case DLT_PRISM_HEADER:
5280
    case DLT_IEEE802_11_RADIO_AVS:
5281
    case DLT_IEEE802_11_RADIO:
5282
    case DLT_PPI:
5283
      b0 = gen_wlanhostop(cstate, eaddr, Q_OR);
5284
      break;
5285
    case DLT_SUNATM:
5286
      /*
5287
       * This is LLC-multiplexed traffic; if it were
5288
       * LANE, cstate->linktype would have been set to
5289
       * DLT_EN10MB.
5290
       */
5291
      bpf_error(cstate,
5292
          "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5293
      break;
5294
    case DLT_IP_OVER_FC:
5295
      b0 = gen_ipfchostop(cstate, eaddr, Q_OR);
5296
      break;
5297
    default:
5298
      bpf_error(cstate,
5299
          "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5300
    }
5301
    b1 = NULL;
5302
    for (ai = alist; ai != NULL; ai = ai->ai_next) {
5303
      /*
5304
       * Does it have an address?
5305
       */
5306
      if (ai->ai_addr != NULL) {
5307
        /*
5308
         * Yes.  Is it an IPv4 address?
5309
         */
5310
        if (ai->ai_addr->sa_family == AF_INET) {
5311
          /*
5312
           * Generate an entry for it.
5313
           */
5314
          sin = (struct sockaddr_in *)ai->ai_addr;
5315
          tmp = gen_host(cstate,
5316
              ntohl(sin->sin_addr.s_addr),
5317
              0xffffffff, proto, Q_OR, Q_HOST);
5318
          /*
5319
           * Is it the *first* IPv4 address?
5320
           */
5321
          if (b1 == NULL) {
5322
            /*
5323
             * Yes, so start with it.
5324
             */
5325
            b1 = tmp;
5326
          } else {
5327
            /*
5328
             * No, so OR it into the
5329
             * existing set of
5330
             * addresses.
5331
             */
5332
            gen_or(b1, tmp);
5333
            b1 = tmp;
5334
          }
5335
        }
5336
      }
5337
    }
5338
    if (b1 == NULL) {
5339
      /*
5340
       * No IPv4 addresses found.
5341
       */
5342
      return (NULL);
5343
    }
5344
    gen_not(b1);
5345
    gen_and(b0, b1);
5346
    return b1;
5347
  }
5348
  bpf_error(cstate, "illegal modifier of 'gateway'");
5349
  /*NOTREACHED*/
5350
}
5351
#endif
5352
5353
static struct block *
5354
gen_proto_abbrev_internal(compiler_state_t *cstate, int proto)
5355
8.13k
{
5356
8.13k
  struct block *b0;
5357
8.13k
  struct block *b1;
5358
5359
8.13k
  switch (proto) {
5360
5361
80
  case Q_SCTP:
5362
80
    b1 = gen_proto(cstate, IPPROTO_SCTP, Q_DEFAULT, Q_DEFAULT);
5363
80
    break;
5364
5365
305
  case Q_TCP:
5366
305
    b1 = gen_proto(cstate, IPPROTO_TCP, Q_DEFAULT, Q_DEFAULT);
5367
305
    break;
5368
5369
393
  case Q_UDP:
5370
393
    b1 = gen_proto(cstate, IPPROTO_UDP, Q_DEFAULT, Q_DEFAULT);
5371
393
    break;
5372
5373
112
  case Q_ICMP:
5374
112
    b1 = gen_proto(cstate, IPPROTO_ICMP, Q_IP, Q_DEFAULT);
5375
112
    break;
5376
5377
#ifndef IPPROTO_IGMP
5378
#define IPPROTO_IGMP  2
5379
#endif
5380
5381
50
  case Q_IGMP:
5382
50
    b1 = gen_proto(cstate, IPPROTO_IGMP, Q_IP, Q_DEFAULT);
5383
50
    break;
5384
5385
0
#ifndef IPPROTO_IGRP
5386
92
#define IPPROTO_IGRP  9
5387
0
#endif
5388
92
  case Q_IGRP:
5389
92
    b1 = gen_proto(cstate, IPPROTO_IGRP, Q_IP, Q_DEFAULT);
5390
92
    break;
5391
5392
#ifndef IPPROTO_PIM
5393
#define IPPROTO_PIM 103
5394
#endif
5395
5396
440
  case Q_PIM:
5397
440
    b1 = gen_proto(cstate, IPPROTO_PIM, Q_DEFAULT, Q_DEFAULT);
5398
440
    break;
5399
5400
0
#ifndef IPPROTO_VRRP
5401
45
#define IPPROTO_VRRP  112
5402
0
#endif
5403
5404
45
  case Q_VRRP:
5405
45
    b1 = gen_proto(cstate, IPPROTO_VRRP, Q_IP, Q_DEFAULT);
5406
45
    break;
5407
5408
0
#ifndef IPPROTO_CARP
5409
82
#define IPPROTO_CARP  112
5410
0
#endif
5411
5412
82
  case Q_CARP:
5413
82
    b1 = gen_proto(cstate, IPPROTO_CARP, Q_IP, Q_DEFAULT);
5414
82
    break;
5415
5416
2.53k
  case Q_IP:
5417
2.53k
    b1 = gen_linktype(cstate, ETHERTYPE_IP);
5418
2.53k
    break;
5419
5420
82
  case Q_ARP:
5421
82
    b1 = gen_linktype(cstate, ETHERTYPE_ARP);
5422
82
    break;
5423
5424
47
  case Q_RARP:
5425
47
    b1 = gen_linktype(cstate, ETHERTYPE_REVARP);
5426
47
    break;
5427
5428
2
  case Q_LINK:
5429
2
    bpf_error(cstate, "link layer applied in wrong context");
5430
5431
317
  case Q_ATALK:
5432
317
    b1 = gen_linktype(cstate, ETHERTYPE_ATALK);
5433
317
    break;
5434
5435
83
  case Q_AARP:
5436
83
    b1 = gen_linktype(cstate, ETHERTYPE_AARP);
5437
83
    break;
5438
5439
73
  case Q_DECNET:
5440
73
    b1 = gen_linktype(cstate, ETHERTYPE_DN);
5441
73
    break;
5442
5443
107
  case Q_SCA:
5444
107
    b1 = gen_linktype(cstate, ETHERTYPE_SCA);
5445
107
    break;
5446
5447
56
  case Q_LAT:
5448
56
    b1 = gen_linktype(cstate, ETHERTYPE_LAT);
5449
56
    break;
5450
5451
39
  case Q_MOPDL:
5452
39
    b1 = gen_linktype(cstate, ETHERTYPE_MOPDL);
5453
39
    break;
5454
5455
68
  case Q_MOPRC:
5456
68
    b1 = gen_linktype(cstate, ETHERTYPE_MOPRC);
5457
68
    break;
5458
5459
335
  case Q_IPV6:
5460
335
    b1 = gen_linktype(cstate, ETHERTYPE_IPV6);
5461
335
    break;
5462
5463
#ifndef IPPROTO_ICMPV6
5464
#define IPPROTO_ICMPV6  58
5465
#endif
5466
24
  case Q_ICMPV6:
5467
24
    b1 = gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
5468
24
    break;
5469
5470
#ifndef IPPROTO_AH
5471
#define IPPROTO_AH  51
5472
#endif
5473
235
  case Q_AH:
5474
235
    b1 = gen_proto(cstate, IPPROTO_AH, Q_DEFAULT, Q_DEFAULT);
5475
235
    break;
5476
5477
#ifndef IPPROTO_ESP
5478
#define IPPROTO_ESP 50
5479
#endif
5480
36
  case Q_ESP:
5481
36
    b1 = gen_proto(cstate, IPPROTO_ESP, Q_DEFAULT, Q_DEFAULT);
5482
36
    break;
5483
5484
162
  case Q_ISO:
5485
162
    b1 = gen_linktype(cstate, LLCSAP_ISONS);
5486
162
    break;
5487
5488
105
  case Q_ESIS:
5489
105
    b1 = gen_proto(cstate, ISO9542_ESIS, Q_ISO, Q_DEFAULT);
5490
105
    break;
5491
5492
122
  case Q_ISIS:
5493
122
    b1 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5494
122
    break;
5495
5496
528
  case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
5497
528
    b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
5498
528
    b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
5499
528
    gen_or(b0, b1);
5500
528
    b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
5501
528
    gen_or(b0, b1);
5502
528
    b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5503
528
    gen_or(b0, b1);
5504
528
    b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5505
528
    gen_or(b0, b1);
5506
528
    break;
5507
5508
419
  case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
5509
419
    b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
5510
419
    b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
5511
419
    gen_or(b0, b1);
5512
419
    b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
5513
419
    gen_or(b0, b1);
5514
419
    b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5515
419
    gen_or(b0, b1);
5516
419
    b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5517
419
    gen_or(b0, b1);
5518
419
    break;
5519
5520
94
  case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
5521
94
    b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
5522
94
    b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
5523
94
    gen_or(b0, b1);
5524
94
    b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
5525
94
    gen_or(b0, b1);
5526
94
    break;
5527
5528
66
  case Q_ISIS_LSP:
5529
66
    b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
5530
66
    b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
5531
66
    gen_or(b0, b1);
5532
66
    break;
5533
5534
123
  case Q_ISIS_SNP:
5535
123
    b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5536
123
    b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5537
123
    gen_or(b0, b1);
5538
123
    b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5539
123
    gen_or(b0, b1);
5540
123
    b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5541
123
    gen_or(b0, b1);
5542
123
    break;
5543
5544
76
  case Q_ISIS_CSNP:
5545
76
    b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5546
76
    b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5547
76
    gen_or(b0, b1);
5548
76
    break;
5549
5550
71
  case Q_ISIS_PSNP:
5551
71
    b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5552
71
    b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5553
71
    gen_or(b0, b1);
5554
71
    break;
5555
5556
40
  case Q_CLNP:
5557
40
    b1 = gen_proto(cstate, ISO8473_CLNP, Q_ISO, Q_DEFAULT);
5558
40
    break;
5559
5560
202
  case Q_STP:
5561
202
    b1 = gen_linktype(cstate, LLCSAP_8021D);
5562
202
    break;
5563
5564
439
  case Q_IPX:
5565
439
    b1 = gen_linktype(cstate, LLCSAP_IPX);
5566
439
    break;
5567
5568
41
  case Q_NETBEUI:
5569
41
    b1 = gen_linktype(cstate, LLCSAP_NETBEUI);
5570
41
    break;
5571
5572
1
  case Q_RADIO:
5573
1
    bpf_error(cstate, "'radio' is not a valid protocol type");
5574
5575
0
  default:
5576
0
    abort();
5577
8.13k
  }
5578
8.06k
  return b1;
5579
8.13k
}
5580
5581
struct block *
5582
gen_proto_abbrev(compiler_state_t *cstate, int proto)
5583
3.73k
{
5584
  /*
5585
   * Catch errors reported by us and routines below us, and return NULL
5586
   * on an error.
5587
   */
5588
3.73k
  if (setjmp(cstate->top_ctx))
5589
59
    return (NULL);
5590
5591
3.67k
  return gen_proto_abbrev_internal(cstate, proto);
5592
3.73k
}
5593
5594
static struct block *
5595
gen_ipfrag(compiler_state_t *cstate)
5596
10.9k
{
5597
10.9k
  struct slist *s;
5598
10.9k
  struct block *b;
5599
5600
  /* not IPv4 frag other than the first frag */
5601
10.9k
  s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H);
5602
10.9k
  b = new_block(cstate, JMP(BPF_JSET));
5603
10.9k
  b->s.k = 0x1fff;
5604
10.9k
  b->stmts = s;
5605
10.9k
  gen_not(b);
5606
5607
10.9k
  return b;
5608
10.9k
}
5609
5610
/*
5611
 * Generate a comparison to a port value in the transport-layer header
5612
 * at the specified offset from the beginning of that header.
5613
 *
5614
 * XXX - this handles a variable-length prefix preceding the link-layer
5615
 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5616
 * variable-length link-layer headers (such as Token Ring or 802.11
5617
 * headers).
5618
 */
5619
static struct block *
5620
gen_portatom(compiler_state_t *cstate, int off, bpf_u_int32 v)
5621
18.3k
{
5622
18.3k
  return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v);
5623
18.3k
}
5624
5625
static struct block *
5626
gen_portatom6(compiler_state_t *cstate, int off, bpf_u_int32 v)
5627
18.3k
{
5628
18.3k
  return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v);
5629
18.3k
}
5630
5631
static struct block *
5632
gen_portop(compiler_state_t *cstate, u_int port, u_int proto, int dir)
5633
9.44k
{
5634
9.44k
  struct block *b0, *b1, *tmp;
5635
5636
  /* ip proto 'proto' and not a fragment other than the first fragment */
5637
9.44k
  tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);
5638
9.44k
  b0 = gen_ipfrag(cstate);
5639
9.44k
  gen_and(tmp, b0);
5640
5641
9.44k
  switch (dir) {
5642
246
  case Q_SRC:
5643
246
    b1 = gen_portatom(cstate, 0, port);
5644
246
    break;
5645
5646
297
  case Q_DST:
5647
297
    b1 = gen_portatom(cstate, 2, port);
5648
297
    break;
5649
5650
753
  case Q_AND:
5651
753
    tmp = gen_portatom(cstate, 0, port);
5652
753
    b1 = gen_portatom(cstate, 2, port);
5653
753
    gen_and(tmp, b1);
5654
753
    break;
5655
5656
7.75k
  case Q_DEFAULT:
5657
8.14k
  case Q_OR:
5658
8.14k
    tmp = gen_portatom(cstate, 0, port);
5659
8.14k
    b1 = gen_portatom(cstate, 2, port);
5660
8.14k
    gen_or(tmp, b1);
5661
8.14k
    break;
5662
5663
1
  case Q_ADDR1:
5664
1
    bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for ports");
5665
    /*NOTREACHED*/
5666
5667
1
  case Q_ADDR2:
5668
1
    bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for ports");
5669
    /*NOTREACHED*/
5670
5671
1
  case Q_ADDR3:
5672
1
    bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for ports");
5673
    /*NOTREACHED*/
5674
5675
1
  case Q_ADDR4:
5676
1
    bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for ports");
5677
    /*NOTREACHED*/
5678
5679
1
  case Q_RA:
5680
1
    bpf_error(cstate, "'ra' is not a valid qualifier for ports");
5681
    /*NOTREACHED*/
5682
5683
1
  case Q_TA:
5684
1
    bpf_error(cstate, "'ta' is not a valid qualifier for ports");
5685
    /*NOTREACHED*/
5686
5687
0
  default:
5688
0
    abort();
5689
    /*NOTREACHED*/
5690
9.44k
  }
5691
9.43k
  gen_and(b0, b1);
5692
5693
9.43k
  return b1;
5694
9.44k
}
5695
5696
static struct block *
5697
gen_port(compiler_state_t *cstate, u_int port, int ip_proto, int dir)
5698
3.43k
{
5699
3.43k
  struct block *b0, *b1, *tmp;
5700
5701
  /*
5702
   * ether proto ip
5703
   *
5704
   * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5705
   * not LLC encapsulation with LLCSAP_IP.
5706
   *
5707
   * For IEEE 802 networks - which includes 802.5 token ring
5708
   * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5709
   * says that SNAP encapsulation is used, not LLC encapsulation
5710
   * with LLCSAP_IP.
5711
   *
5712
   * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5713
   * RFC 2225 say that SNAP encapsulation is used, not LLC
5714
   * encapsulation with LLCSAP_IP.
5715
   *
5716
   * So we always check for ETHERTYPE_IP.
5717
   */
5718
3.43k
  b0 = gen_linktype(cstate, ETHERTYPE_IP);
5719
5720
3.43k
  switch (ip_proto) {
5721
127
  case IPPROTO_UDP:
5722
267
  case IPPROTO_TCP:
5723
417
  case IPPROTO_SCTP:
5724
417
    b1 = gen_portop(cstate, port, (u_int)ip_proto, dir);
5725
417
    break;
5726
5727
3.01k
  case PROTO_UNDEF:
5728
3.01k
    tmp = gen_portop(cstate, port, IPPROTO_TCP, dir);
5729
3.01k
    b1 = gen_portop(cstate, port, IPPROTO_UDP, dir);
5730
3.01k
    gen_or(tmp, b1);
5731
3.01k
    tmp = gen_portop(cstate, port, IPPROTO_SCTP, dir);
5732
3.01k
    gen_or(tmp, b1);
5733
3.01k
    break;
5734
5735
0
  default:
5736
0
    abort();
5737
3.43k
  }
5738
3.42k
  gen_and(b0, b1);
5739
3.42k
  return b1;
5740
3.43k
}
5741
5742
struct block *
5743
gen_portop6(compiler_state_t *cstate, u_int port, u_int proto, int dir)
5744
9.43k
{
5745
9.43k
  struct block *b0, *b1, *tmp;
5746
5747
  /* ip6 proto 'proto' */
5748
  /* XXX - catch the first fragment of a fragmented packet? */
5749
9.43k
  b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);
5750
5751
9.43k
  switch (dir) {
5752
246
  case Q_SRC:
5753
246
    b1 = gen_portatom6(cstate, 0, port);
5754
246
    break;
5755
5756
297
  case Q_DST:
5757
297
    b1 = gen_portatom6(cstate, 2, port);
5758
297
    break;
5759
5760
753
  case Q_AND:
5761
753
    tmp = gen_portatom6(cstate, 0, port);
5762
753
    b1 = gen_portatom6(cstate, 2, port);
5763
753
    gen_and(tmp, b1);
5764
753
    break;
5765
5766
7.75k
  case Q_DEFAULT:
5767
8.14k
  case Q_OR:
5768
8.14k
    tmp = gen_portatom6(cstate, 0, port);
5769
8.14k
    b1 = gen_portatom6(cstate, 2, port);
5770
8.14k
    gen_or(tmp, b1);
5771
8.14k
    break;
5772
5773
0
  default:
5774
0
    abort();
5775
9.43k
  }
5776
9.43k
  gen_and(b0, b1);
5777
5778
9.43k
  return b1;
5779
9.43k
}
5780
5781
static struct block *
5782
gen_port6(compiler_state_t *cstate, u_int port, int ip_proto, int dir)
5783
3.42k
{
5784
3.42k
  struct block *b0, *b1, *tmp;
5785
5786
  /* link proto ip6 */
5787
3.42k
  b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
5788
5789
3.42k
  switch (ip_proto) {
5790
127
  case IPPROTO_UDP:
5791
267
  case IPPROTO_TCP:
5792
417
  case IPPROTO_SCTP:
5793
417
    b1 = gen_portop6(cstate, port, (u_int)ip_proto, dir);
5794
417
    break;
5795
5796
3.00k
  case PROTO_UNDEF:
5797
3.00k
    tmp = gen_portop6(cstate, port, IPPROTO_TCP, dir);
5798
3.00k
    b1 = gen_portop6(cstate, port, IPPROTO_UDP, dir);
5799
3.00k
    gen_or(tmp, b1);
5800
3.00k
    tmp = gen_portop6(cstate, port, IPPROTO_SCTP, dir);
5801
3.00k
    gen_or(tmp, b1);
5802
3.00k
    break;
5803
5804
0
  default:
5805
0
    abort();
5806
3.42k
  }
5807
3.42k
  gen_and(b0, b1);
5808
3.42k
  return b1;
5809
3.42k
}
5810
5811
/* gen_portrange code */
5812
static struct block *
5813
gen_portrangeatom(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,
5814
    bpf_u_int32 v2)
5815
0
{
5816
0
  struct block *b1, *b2;
5817
5818
0
  if (v1 > v2) {
5819
    /*
5820
     * Reverse the order of the ports, so v1 is the lower one.
5821
     */
5822
0
    bpf_u_int32 vtemp;
5823
5824
0
    vtemp = v1;
5825
0
    v1 = v2;
5826
0
    v2 = vtemp;
5827
0
  }
5828
5829
0
  b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, v1);
5830
0
  b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, v2);
5831
5832
0
  gen_and(b1, b2);
5833
5834
0
  return b2;
5835
0
}
5836
5837
static struct block *
5838
gen_portrangeop(compiler_state_t *cstate, u_int port1, u_int port2,
5839
    bpf_u_int32 proto, int dir)
5840
0
{
5841
0
  struct block *b0, *b1, *tmp;
5842
5843
  /* ip proto 'proto' and not a fragment other than the first fragment */
5844
0
  tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);
5845
0
  b0 = gen_ipfrag(cstate);
5846
0
  gen_and(tmp, b0);
5847
5848
0
  switch (dir) {
5849
0
  case Q_SRC:
5850
0
    b1 = gen_portrangeatom(cstate, 0, port1, port2);
5851
0
    break;
5852
5853
0
  case Q_DST:
5854
0
    b1 = gen_portrangeatom(cstate, 2, port1, port2);
5855
0
    break;
5856
5857
0
  case Q_AND:
5858
0
    tmp = gen_portrangeatom(cstate, 0, port1, port2);
5859
0
    b1 = gen_portrangeatom(cstate, 2, port1, port2);
5860
0
    gen_and(tmp, b1);
5861
0
    break;
5862
5863
0
  case Q_DEFAULT:
5864
0
  case Q_OR:
5865
0
    tmp = gen_portrangeatom(cstate, 0, port1, port2);
5866
0
    b1 = gen_portrangeatom(cstate, 2, port1, port2);
5867
0
    gen_or(tmp, b1);
5868
0
    break;
5869
5870
0
  case Q_ADDR1:
5871
0
    bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5872
    /*NOTREACHED*/
5873
5874
0
  case Q_ADDR2:
5875
0
    bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5876
    /*NOTREACHED*/
5877
5878
0
  case Q_ADDR3:
5879
0
    bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5880
    /*NOTREACHED*/
5881
5882
0
  case Q_ADDR4:
5883
0
    bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5884
    /*NOTREACHED*/
5885
5886
0
  case Q_RA:
5887
0
    bpf_error(cstate, "'ra' is not a valid qualifier for port ranges");
5888
    /*NOTREACHED*/
5889
5890
0
  case Q_TA:
5891
0
    bpf_error(cstate, "'ta' is not a valid qualifier for port ranges");
5892
    /*NOTREACHED*/
5893
5894
0
  default:
5895
0
    abort();
5896
    /*NOTREACHED*/
5897
0
  }
5898
0
  gen_and(b0, b1);
5899
5900
0
  return b1;
5901
0
}
5902
5903
static struct block *
5904
gen_portrange(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,
5905
    int dir)
5906
0
{
5907
0
  struct block *b0, *b1, *tmp;
5908
5909
  /* link proto ip */
5910
0
  b0 = gen_linktype(cstate, ETHERTYPE_IP);
5911
5912
0
  switch (ip_proto) {
5913
0
  case IPPROTO_UDP:
5914
0
  case IPPROTO_TCP:
5915
0
  case IPPROTO_SCTP:
5916
0
    b1 = gen_portrangeop(cstate, port1, port2, (bpf_u_int32)ip_proto,
5917
0
        dir);
5918
0
    break;
5919
5920
0
  case PROTO_UNDEF:
5921
0
    tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_TCP, dir);
5922
0
    b1 = gen_portrangeop(cstate, port1, port2, IPPROTO_UDP, dir);
5923
0
    gen_or(tmp, b1);
5924
0
    tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_SCTP, dir);
5925
0
    gen_or(tmp, b1);
5926
0
    break;
5927
5928
0
  default:
5929
0
    abort();
5930
0
  }
5931
0
  gen_and(b0, b1);
5932
0
  return b1;
5933
0
}
5934
5935
static struct block *
5936
gen_portrangeatom6(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,
5937
    bpf_u_int32 v2)
5938
0
{
5939
0
  struct block *b1, *b2;
5940
5941
0
  if (v1 > v2) {
5942
    /*
5943
     * Reverse the order of the ports, so v1 is the lower one.
5944
     */
5945
0
    bpf_u_int32 vtemp;
5946
5947
0
    vtemp = v1;
5948
0
    v1 = v2;
5949
0
    v2 = vtemp;
5950
0
  }
5951
5952
0
  b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, v1);
5953
0
  b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, v2);
5954
5955
0
  gen_and(b1, b2);
5956
5957
0
  return b2;
5958
0
}
5959
5960
static struct block *
5961
gen_portrangeop6(compiler_state_t *cstate, u_int port1, u_int port2,
5962
    bpf_u_int32 proto, int dir)
5963
0
{
5964
0
  struct block *b0, *b1, *tmp;
5965
5966
  /* ip6 proto 'proto' */
5967
  /* XXX - catch the first fragment of a fragmented packet? */
5968
0
  b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);
5969
5970
0
  switch (dir) {
5971
0
  case Q_SRC:
5972
0
    b1 = gen_portrangeatom6(cstate, 0, port1, port2);
5973
0
    break;
5974
5975
0
  case Q_DST:
5976
0
    b1 = gen_portrangeatom6(cstate, 2, port1, port2);
5977
0
    break;
5978
5979
0
  case Q_AND:
5980
0
    tmp = gen_portrangeatom6(cstate, 0, port1, port2);
5981
0
    b1 = gen_portrangeatom6(cstate, 2, port1, port2);
5982
0
    gen_and(tmp, b1);
5983
0
    break;
5984
5985
0
  case Q_DEFAULT:
5986
0
  case Q_OR:
5987
0
    tmp = gen_portrangeatom6(cstate, 0, port1, port2);
5988
0
    b1 = gen_portrangeatom6(cstate, 2, port1, port2);
5989
0
    gen_or(tmp, b1);
5990
0
    break;
5991
5992
0
  default:
5993
0
    abort();
5994
0
  }
5995
0
  gen_and(b0, b1);
5996
5997
0
  return b1;
5998
0
}
5999
6000
static struct block *
6001
gen_portrange6(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,
6002
    int dir)
6003
0
{
6004
0
  struct block *b0, *b1, *tmp;
6005
6006
  /* link proto ip6 */
6007
0
  b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6008
6009
0
  switch (ip_proto) {
6010
0
  case IPPROTO_UDP:
6011
0
  case IPPROTO_TCP:
6012
0
  case IPPROTO_SCTP:
6013
0
    b1 = gen_portrangeop6(cstate, port1, port2, (bpf_u_int32)ip_proto,
6014
0
        dir);
6015
0
    break;
6016
6017
0
  case PROTO_UNDEF:
6018
0
    tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_TCP, dir);
6019
0
    b1 = gen_portrangeop6(cstate, port1, port2, IPPROTO_UDP, dir);
6020
0
    gen_or(tmp, b1);
6021
0
    tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_SCTP, dir);
6022
0
    gen_or(tmp, b1);
6023
0
    break;
6024
6025
0
  default:
6026
0
    abort();
6027
0
  }
6028
0
  gen_and(b0, b1);
6029
0
  return b1;
6030
0
}
6031
6032
static int
6033
lookup_proto(compiler_state_t *cstate, const char *name, int proto)
6034
782
{
6035
782
  register int v;
6036
6037
782
  switch (proto) {
6038
6039
299
  case Q_DEFAULT:
6040
405
  case Q_IP:
6041
445
  case Q_IPV6:
6042
445
    v = pcap_nametoproto(name);
6043
445
    if (v == PROTO_UNDEF)
6044
0
      bpf_error(cstate, "unknown ip proto '%s'", name);
6045
445
    break;
6046
6047
445
  case Q_LINK:
6048
    /* XXX should look up h/w protocol type based on cstate->linktype */
6049
225
    v = pcap_nametoeproto(name);
6050
225
    if (v == PROTO_UNDEF) {
6051
176
      v = pcap_nametollc(name);
6052
176
      if (v == PROTO_UNDEF)
6053
109
        bpf_error(cstate, "unknown ether proto '%s'", name);
6054
176
    }
6055
116
    break;
6056
6057
116
  case Q_ISO:
6058
110
    if (strcmp(name, "esis") == 0)
6059
11
      v = ISO9542_ESIS;
6060
99
    else if (strcmp(name, "isis") == 0)
6061
19
      v = ISO10589_ISIS;
6062
80
    else if (strcmp(name, "clnp") == 0)
6063
22
      v = ISO8473_CLNP;
6064
58
    else
6065
58
      bpf_error(cstate, "unknown osi proto '%s'", name);
6066
52
    break;
6067
6068
52
  default:
6069
2
    v = PROTO_UNDEF;
6070
2
    break;
6071
782
  }
6072
615
  return v;
6073
782
}
6074
6075
#if !defined(NO_PROTOCHAIN)
6076
static struct block *
6077
gen_protochain(compiler_state_t *cstate, bpf_u_int32 v, int proto)
6078
1.31k
{
6079
1.31k
  struct block *b0, *b;
6080
1.31k
  struct slist *s[100];
6081
1.31k
  int fix2, fix3, fix4, fix5;
6082
1.31k
  int ahcheck, again, end;
6083
1.31k
  int i, max;
6084
1.31k
  int reg2 = alloc_reg(cstate);
6085
6086
1.31k
  memset(s, 0, sizeof(s));
6087
1.31k
  fix3 = fix4 = fix5 = 0;
6088
6089
1.31k
  switch (proto) {
6090
570
  case Q_IP:
6091
965
  case Q_IPV6:
6092
965
    break;
6093
344
  case Q_DEFAULT:
6094
344
    b0 = gen_protochain(cstate, v, Q_IP);
6095
344
    b = gen_protochain(cstate, v, Q_IPV6);
6096
344
    gen_or(b0, b);
6097
344
    return b;
6098
1
  default:
6099
1
    bpf_error(cstate, "bad protocol applied for 'protochain'");
6100
    /*NOTREACHED*/
6101
1.31k
  }
6102
6103
  /*
6104
   * We don't handle variable-length prefixes before the link-layer
6105
   * header, or variable-length link-layer headers, here yet.
6106
   * We might want to add BPF instructions to do the protochain
6107
   * work, to simplify that and, on platforms that have a BPF
6108
   * interpreter with the new instructions, let the filtering
6109
   * be done in the kernel.  (We already require a modified BPF
6110
   * engine to do the protochain stuff, to support backward
6111
   * branches, and backward branch support is unlikely to appear
6112
   * in kernel BPF engines.)
6113
   */
6114
965
  if (cstate->off_linkpl.is_variable)
6115
1
    bpf_error(cstate, "'protochain' not supported with variable length headers");
6116
6117
  /*
6118
   * To quote a comment in optimize.c:
6119
   *
6120
   * "These data structures are used in a Cocke and Shwarz style
6121
   * value numbering scheme.  Since the flowgraph is acyclic,
6122
   * exit values can be propagated from a node's predecessors
6123
   * provided it is uniquely defined."
6124
   *
6125
   * "Acyclic" means "no backward branches", which means "no
6126
   * loops", so we have to turn the optimizer off.
6127
   */
6128
964
  cstate->no_optimize = 1;
6129
6130
  /*
6131
   * s[0] is a dummy entry to protect other BPF insn from damage
6132
   * by s[fix] = foo with uninitialized variable "fix".  It is somewhat
6133
   * hard to find interdependency made by jump table fixup.
6134
   */
6135
964
  i = 0;
6136
964
  s[i] = new_stmt(cstate, 0); /*dummy*/
6137
964
  i++;
6138
6139
964
  switch (proto) {
6140
569
  case Q_IP:
6141
569
    b0 = gen_linktype(cstate, ETHERTYPE_IP);
6142
6143
    /* A = ip->ip_p */
6144
569
    s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
6145
569
    s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 9;
6146
569
    i++;
6147
    /* X = ip->ip_hl << 2 */
6148
569
    s[i] = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
6149
569
    s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6150
569
    i++;
6151
569
    break;
6152
6153
395
  case Q_IPV6:
6154
395
    b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6155
6156
    /* A = ip6->ip_nxt */
6157
395
    s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
6158
395
    s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 6;
6159
395
    i++;
6160
    /* X = sizeof(struct ip6_hdr) */
6161
395
    s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM);
6162
395
    s[i]->s.k = 40;
6163
395
    i++;
6164
395
    break;
6165
6166
0
  default:
6167
0
    bpf_error(cstate, "unsupported proto to gen_protochain");
6168
    /*NOTREACHED*/
6169
964
  }
6170
6171
  /* again: if (A == v) goto end; else fall through; */
6172
964
  again = i;
6173
964
  s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6174
964
  s[i]->s.k = v;
6175
964
  s[i]->s.jt = NULL;    /*later*/
6176
964
  s[i]->s.jf = NULL;    /*update in next stmt*/
6177
964
  fix5 = i;
6178
964
  i++;
6179
6180
#ifndef IPPROTO_NONE
6181
#define IPPROTO_NONE  59
6182
#endif
6183
  /* if (A == IPPROTO_NONE) goto end */
6184
964
  s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6185
964
  s[i]->s.jt = NULL;  /*later*/
6186
964
  s[i]->s.jf = NULL;  /*update in next stmt*/
6187
964
  s[i]->s.k = IPPROTO_NONE;
6188
964
  s[fix5]->s.jf = s[i];
6189
964
  fix2 = i;
6190
964
  i++;
6191
6192
964
  if (proto == Q_IPV6) {
6193
395
    int v6start, v6end, v6advance, j;
6194
6195
395
    v6start = i;
6196
    /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6197
395
    s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6198
395
    s[i]->s.jt = NULL;  /*later*/
6199
395
    s[i]->s.jf = NULL;  /*update in next stmt*/
6200
395
    s[i]->s.k = IPPROTO_HOPOPTS;
6201
395
    s[fix2]->s.jf = s[i];
6202
395
    i++;
6203
    /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6204
395
    s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6205
395
    s[i]->s.jt = NULL;  /*later*/
6206
395
    s[i]->s.jf = NULL;  /*update in next stmt*/
6207
395
    s[i]->s.k = IPPROTO_DSTOPTS;
6208
395
    i++;
6209
    /* if (A == IPPROTO_ROUTING) goto v6advance */
6210
395
    s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6211
395
    s[i]->s.jt = NULL;  /*later*/
6212
395
    s[i]->s.jf = NULL;  /*update in next stmt*/
6213
395
    s[i]->s.k = IPPROTO_ROUTING;
6214
395
    i++;
6215
    /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6216
395
    s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6217
395
    s[i]->s.jt = NULL;  /*later*/
6218
395
    s[i]->s.jf = NULL;  /*later*/
6219
395
    s[i]->s.k = IPPROTO_FRAGMENT;
6220
395
    fix3 = i;
6221
395
    v6end = i;
6222
395
    i++;
6223
6224
    /* v6advance: */
6225
395
    v6advance = i;
6226
6227
    /*
6228
     * in short,
6229
     * A = P[X + packet head];
6230
     * X = X + (P[X + packet head + 1] + 1) * 8;
6231
     */
6232
    /* A = P[X + packet head] */
6233
395
    s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6234
395
    s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6235
395
    i++;
6236
    /* MEM[reg2] = A */
6237
395
    s[i] = new_stmt(cstate, BPF_ST);
6238
395
    s[i]->s.k = reg2;
6239
395
    i++;
6240
    /* A = P[X + packet head + 1]; */
6241
395
    s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6242
395
    s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1;
6243
395
    i++;
6244
    /* A += 1 */
6245
395
    s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6246
395
    s[i]->s.k = 1;
6247
395
    i++;
6248
    /* A *= 8 */
6249
395
    s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
6250
395
    s[i]->s.k = 8;
6251
395
    i++;
6252
    /* A += X */
6253
395
    s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
6254
395
    s[i]->s.k = 0;
6255
395
    i++;
6256
    /* X = A; */
6257
395
    s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6258
395
    i++;
6259
    /* A = MEM[reg2] */
6260
395
    s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
6261
395
    s[i]->s.k = reg2;
6262
395
    i++;
6263
6264
    /* goto again; (must use BPF_JA for backward jump) */
6265
395
    s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
6266
395
    s[i]->s.k = again - i - 1;
6267
395
    s[i - 1]->s.jf = s[i];
6268
395
    i++;
6269
6270
    /* fixup */
6271
1.97k
    for (j = v6start; j <= v6end; j++)
6272
1.58k
      s[j]->s.jt = s[v6advance];
6273
569
  } else {
6274
    /* nop */
6275
569
    s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6276
569
    s[i]->s.k = 0;
6277
569
    s[fix2]->s.jf = s[i];
6278
569
    i++;
6279
569
  }
6280
6281
  /* ahcheck: */
6282
964
  ahcheck = i;
6283
  /* if (A == IPPROTO_AH) then fall through; else goto end; */
6284
964
  s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6285
964
  s[i]->s.jt = NULL;  /*later*/
6286
964
  s[i]->s.jf = NULL;  /*later*/
6287
964
  s[i]->s.k = IPPROTO_AH;
6288
964
  if (fix3)
6289
395
    s[fix3]->s.jf = s[ahcheck];
6290
964
  fix4 = i;
6291
964
  i++;
6292
6293
  /*
6294
   * in short,
6295
   * A = P[X];
6296
   * X = X + (P[X + 1] + 2) * 4;
6297
   */
6298
  /* A = X */
6299
964
  s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
6300
964
  i++;
6301
  /* A = P[X + packet head]; */
6302
964
  s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6303
964
  s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6304
964
  i++;
6305
  /* MEM[reg2] = A */
6306
964
  s[i] = new_stmt(cstate, BPF_ST);
6307
964
  s[i]->s.k = reg2;
6308
964
  i++;
6309
  /* A = X */
6310
964
  s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
6311
964
  i++;
6312
  /* A += 1 */
6313
964
  s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6314
964
  s[i]->s.k = 1;
6315
964
  i++;
6316
  /* X = A */
6317
964
  s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6318
964
  i++;
6319
  /* A = P[X + packet head] */
6320
964
  s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6321
964
  s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6322
964
  i++;
6323
  /* A += 2 */
6324
964
  s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6325
964
  s[i]->s.k = 2;
6326
964
  i++;
6327
  /* A *= 4 */
6328
964
  s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
6329
964
  s[i]->s.k = 4;
6330
964
  i++;
6331
  /* X = A; */
6332
964
  s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6333
964
  i++;
6334
  /* A = MEM[reg2] */
6335
964
  s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
6336
964
  s[i]->s.k = reg2;
6337
964
  i++;
6338
6339
  /* goto again; (must use BPF_JA for backward jump) */
6340
964
  s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
6341
964
  s[i]->s.k = again - i - 1;
6342
964
  i++;
6343
6344
  /* end: nop */
6345
964
  end = i;
6346
964
  s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6347
964
  s[i]->s.k = 0;
6348
964
  s[fix2]->s.jt = s[end];
6349
964
  s[fix4]->s.jf = s[end];
6350
964
  s[fix5]->s.jt = s[end];
6351
964
  i++;
6352
6353
  /*
6354
   * make slist chain
6355
   */
6356
964
  max = i;
6357
24.0k
  for (i = 0; i < max - 1; i++)
6358
23.0k
    s[i]->next = s[i + 1];
6359
964
  s[max - 1]->next = NULL;
6360
6361
  /*
6362
   * emit final check
6363
   */
6364
964
  b = new_block(cstate, JMP(BPF_JEQ));
6365
964
  b->stmts = s[1];  /*remember, s[0] is dummy*/
6366
964
  b->s.k = v;
6367
6368
964
  free_reg(cstate, reg2);
6369
6370
964
  gen_and(b0, b);
6371
964
  return b;
6372
964
}
6373
#endif /* !defined(NO_PROTOCHAIN) */
6374
6375
static struct block *
6376
gen_check_802_11_data_frame(compiler_state_t *cstate)
6377
17.5k
{
6378
17.5k
  struct slist *s;
6379
17.5k
  struct block *b0, *b1;
6380
6381
  /*
6382
   * A data frame has the 0x08 bit (b3) in the frame control field set
6383
   * and the 0x04 bit (b2) clear.
6384
   */
6385
17.5k
  s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
6386
17.5k
  b0 = new_block(cstate, JMP(BPF_JSET));
6387
17.5k
  b0->s.k = 0x08;
6388
17.5k
  b0->stmts = s;
6389
6390
17.5k
  s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
6391
17.5k
  b1 = new_block(cstate, JMP(BPF_JSET));
6392
17.5k
  b1->s.k = 0x04;
6393
17.5k
  b1->stmts = s;
6394
17.5k
  gen_not(b1);
6395
6396
17.5k
  gen_and(b1, b0);
6397
6398
17.5k
  return b0;
6399
17.5k
}
6400
6401
/*
6402
 * Generate code that checks whether the packet is a packet for protocol
6403
 * <proto> and whether the type field in that protocol's header has
6404
 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6405
 * IP packet and checks the protocol number in the IP header against <v>.
6406
 *
6407
 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6408
 * against Q_IP and Q_IPV6.
6409
 */
6410
static struct block *
6411
gen_proto(compiler_state_t *cstate, bpf_u_int32 v, int proto, int dir)
6412
21.6k
{
6413
21.6k
  struct block *b0, *b1;
6414
21.6k
  struct block *b2;
6415
6416
21.6k
  if (dir != Q_DEFAULT)
6417
0
    bpf_error(cstate, "direction applied to 'proto'");
6418
6419
21.6k
  switch (proto) {
6420
2.23k
  case Q_DEFAULT:
6421
2.23k
    b0 = gen_proto(cstate, v, Q_IP, dir);
6422
2.23k
    b1 = gen_proto(cstate, v, Q_IPV6, dir);
6423
2.23k
    gen_or(b0, b1);
6424
2.23k
    return b1;
6425
6426
2.03k
  case Q_LINK:
6427
2.03k
    return gen_linktype(cstate, v);
6428
6429
2.83k
  case Q_IP:
6430
    /*
6431
     * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6432
     * not LLC encapsulation with LLCSAP_IP.
6433
     *
6434
     * For IEEE 802 networks - which includes 802.5 token ring
6435
     * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6436
     * says that SNAP encapsulation is used, not LLC encapsulation
6437
     * with LLCSAP_IP.
6438
     *
6439
     * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6440
     * RFC 2225 say that SNAP encapsulation is used, not LLC
6441
     * encapsulation with LLCSAP_IP.
6442
     *
6443
     * So we always check for ETHERTYPE_IP.
6444
     */
6445
2.83k
    b0 = gen_linktype(cstate, ETHERTYPE_IP);
6446
2.83k
    b1 = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, v);
6447
2.83k
    gen_and(b0, b1);
6448
2.83k
    return b1;
6449
6450
1
  case Q_ARP:
6451
1
    bpf_error(cstate, "arp does not encapsulate another protocol");
6452
    /*NOTREACHED*/
6453
6454
1
  case Q_RARP:
6455
1
    bpf_error(cstate, "rarp does not encapsulate another protocol");
6456
    /*NOTREACHED*/
6457
6458
1
  case Q_SCTP:
6459
1
    bpf_error(cstate, "'sctp proto' is bogus");
6460
    /*NOTREACHED*/
6461
6462
1
  case Q_TCP:
6463
1
    bpf_error(cstate, "'tcp proto' is bogus");
6464
    /*NOTREACHED*/
6465
6466
1
  case Q_UDP:
6467
1
    bpf_error(cstate, "'udp proto' is bogus");
6468
    /*NOTREACHED*/
6469
6470
1
  case Q_ICMP:
6471
1
    bpf_error(cstate, "'icmp proto' is bogus");
6472
    /*NOTREACHED*/
6473
6474
1
  case Q_IGMP:
6475
1
    bpf_error(cstate, "'igmp proto' is bogus");
6476
    /*NOTREACHED*/
6477
6478
1
  case Q_IGRP:
6479
1
    bpf_error(cstate, "'igrp proto' is bogus");
6480
    /*NOTREACHED*/
6481
6482
1
  case Q_ATALK:
6483
1
    bpf_error(cstate, "AppleTalk encapsulation is not specifiable");
6484
    /*NOTREACHED*/
6485
6486
1
  case Q_DECNET:
6487
1
    bpf_error(cstate, "DECNET encapsulation is not specifiable");
6488
    /*NOTREACHED*/
6489
6490
1
  case Q_LAT:
6491
1
    bpf_error(cstate, "LAT does not encapsulate another protocol");
6492
    /*NOTREACHED*/
6493
6494
1
  case Q_SCA:
6495
1
    bpf_error(cstate, "SCA does not encapsulate another protocol");
6496
    /*NOTREACHED*/
6497
6498
1
  case Q_MOPRC:
6499
1
    bpf_error(cstate, "MOPRC does not encapsulate another protocol");
6500
    /*NOTREACHED*/
6501
6502
1
  case Q_MOPDL:
6503
1
    bpf_error(cstate, "MOPDL does not encapsulate another protocol");
6504
    /*NOTREACHED*/
6505
6506
2.37k
  case Q_IPV6:
6507
2.37k
    b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6508
    /*
6509
     * Also check for a fragment header before the final
6510
     * header.
6511
     */
6512
2.37k
    b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, IPPROTO_FRAGMENT);
6513
2.37k
    b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, v);
6514
2.37k
    gen_and(b2, b1);
6515
2.37k
    b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, v);
6516
2.37k
    gen_or(b2, b1);
6517
2.37k
    gen_and(b0, b1);
6518
2.37k
    return b1;
6519
6520
1
  case Q_ICMPV6:
6521
1
    bpf_error(cstate, "'icmp6 proto' is bogus");
6522
    /*NOTREACHED*/
6523
6524
1
  case Q_AH:
6525
1
    bpf_error(cstate, "'ah proto' is bogus");
6526
    /*NOTREACHED*/
6527
6528
1
  case Q_ESP:
6529
1
    bpf_error(cstate, "'esp proto' is bogus");
6530
    /*NOTREACHED*/
6531
6532
1
  case Q_PIM:
6533
1
    bpf_error(cstate, "'pim proto' is bogus");
6534
    /*NOTREACHED*/
6535
6536
1
  case Q_VRRP:
6537
1
    bpf_error(cstate, "'vrrp proto' is bogus");
6538
    /*NOTREACHED*/
6539
6540
1
  case Q_AARP:
6541
1
    bpf_error(cstate, "'aarp proto' is bogus");
6542
    /*NOTREACHED*/
6543
6544
6.23k
  case Q_ISO:
6545
6.23k
    switch (cstate->linktype) {
6546
6547
252
    case DLT_FRELAY:
6548
      /*
6549
       * Frame Relay packets typically have an OSI
6550
       * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6551
       * generates code to check for all the OSI
6552
       * NLPIDs, so calling it and then adding a check
6553
       * for the particular NLPID for which we're
6554
       * looking is bogus, as we can just check for
6555
       * the NLPID.
6556
       *
6557
       * What we check for is the NLPID and a frame
6558
       * control field value of UI, i.e. 0x03 followed
6559
       * by the NLPID.
6560
       *
6561
       * XXX - assumes a 2-byte Frame Relay header with
6562
       * DLCI and flags.  What if the address is longer?
6563
       *
6564
       * XXX - what about SNAP-encapsulated frames?
6565
       */
6566
252
      return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v);
6567
      /*NOTREACHED*/
6568
6569
249
    case DLT_C_HDLC:
6570
510
    case DLT_HDLC:
6571
      /*
6572
       * Cisco uses an Ethertype lookalike - for OSI,
6573
       * it's 0xfefe.
6574
       */
6575
510
      b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS);
6576
      /* OSI in C-HDLC is stuffed with a fudge byte */
6577
510
      b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, v);
6578
510
      gen_and(b0, b1);
6579
510
      return b1;
6580
6581
5.46k
    default:
6582
5.46k
      b0 = gen_linktype(cstate, LLCSAP_ISONS);
6583
5.46k
      b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, v);
6584
5.46k
      gen_and(b0, b1);
6585
5.46k
      return b1;
6586
6.23k
    }
6587
6588
1
  case Q_ESIS:
6589
1
    bpf_error(cstate, "'esis proto' is bogus");
6590
    /*NOTREACHED*/
6591
6592
5.91k
  case Q_ISIS:
6593
5.91k
    b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
6594
    /*
6595
     * 4 is the offset of the PDU type relative to the IS-IS
6596
     * header.
6597
     */
6598
5.91k
    b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, v);
6599
5.91k
    gen_and(b0, b1);
6600
5.91k
    return b1;
6601
6602
1
  case Q_CLNP:
6603
1
    bpf_error(cstate, "'clnp proto' is not supported");
6604
    /*NOTREACHED*/
6605
6606
1
  case Q_STP:
6607
1
    bpf_error(cstate, "'stp proto' is bogus");
6608
    /*NOTREACHED*/
6609
6610
1
  case Q_IPX:
6611
1
    bpf_error(cstate, "'ipx proto' is bogus");
6612
    /*NOTREACHED*/
6613
6614
1
  case Q_NETBEUI:
6615
1
    bpf_error(cstate, "'netbeui proto' is bogus");
6616
    /*NOTREACHED*/
6617
6618
1
  case Q_ISIS_L1:
6619
1
    bpf_error(cstate, "'l1 proto' is bogus");
6620
    /*NOTREACHED*/
6621
6622
1
  case Q_ISIS_L2:
6623
1
    bpf_error(cstate, "'l2 proto' is bogus");
6624
    /*NOTREACHED*/
6625
6626
1
  case Q_ISIS_IIH:
6627
1
    bpf_error(cstate, "'iih proto' is bogus");
6628
    /*NOTREACHED*/
6629
6630
1
  case Q_ISIS_SNP:
6631
1
    bpf_error(cstate, "'snp proto' is bogus");
6632
    /*NOTREACHED*/
6633
6634
1
  case Q_ISIS_CSNP:
6635
1
    bpf_error(cstate, "'csnp proto' is bogus");
6636
    /*NOTREACHED*/
6637
6638
0
  case Q_ISIS_PSNP:
6639
0
    bpf_error(cstate, "'psnp proto' is bogus");
6640
    /*NOTREACHED*/
6641
6642
1
  case Q_ISIS_LSP:
6643
1
    bpf_error(cstate, "'lsp proto' is bogus");
6644
    /*NOTREACHED*/
6645
6646
1
  case Q_RADIO:
6647
1
    bpf_error(cstate, "'radio proto' is bogus");
6648
    /*NOTREACHED*/
6649
6650
1
  case Q_CARP:
6651
1
    bpf_error(cstate, "'carp proto' is bogus");
6652
    /*NOTREACHED*/
6653
6654
0
  default:
6655
0
    abort();
6656
    /*NOTREACHED*/
6657
21.6k
  }
6658
  /*NOTREACHED*/
6659
21.6k
}
6660
6661
struct block *
6662
gen_scode(compiler_state_t *cstate, const char *name, struct qual q)
6663
1.94k
{
6664
1.94k
  int proto = q.proto;
6665
1.94k
  int dir = q.dir;
6666
1.94k
  int tproto;
6667
1.94k
  u_char *eaddr;
6668
1.94k
  bpf_u_int32 mask, addr;
6669
1.94k
  struct addrinfo *res, *res0;
6670
1.94k
  struct sockaddr_in *sin4;
6671
1.94k
#ifdef INET6
6672
1.94k
  int tproto6;
6673
1.94k
  struct sockaddr_in6 *sin6;
6674
1.94k
  struct in6_addr mask128;
6675
1.94k
#endif /*INET6*/
6676
1.94k
  struct block *b, *tmp;
6677
1.94k
  int port, real_proto;
6678
1.94k
  int port1, port2;
6679
6680
  /*
6681
   * Catch errors reported by us and routines below us, and return NULL
6682
   * on an error.
6683
   */
6684
1.94k
  if (setjmp(cstate->top_ctx))
6685
697
    return (NULL);
6686
6687
1.25k
  switch (q.addr) {
6688
6689
7
  case Q_NET:
6690
7
    addr = pcap_nametonetaddr(name);
6691
7
    if (addr == 0)
6692
7
      bpf_error(cstate, "unknown network '%s'", name);
6693
    /* Left justify network addr and calculate its network mask */
6694
0
    mask = 0xffffffff;
6695
0
    while (addr && (addr & 0xff000000) == 0) {
6696
0
      addr <<= 8;
6697
0
      mask <<= 8;
6698
0
    }
6699
0
    return gen_host(cstate, addr, mask, proto, dir, q.addr);
6700
6701
426
  case Q_DEFAULT:
6702
471
  case Q_HOST:
6703
471
    if (proto == Q_LINK) {
6704
42
      switch (cstate->linktype) {
6705
6706
3
      case DLT_EN10MB:
6707
4
      case DLT_NETANALYZER:
6708
6
      case DLT_NETANALYZER_TRANSPARENT:
6709
6
        eaddr = pcap_ether_hostton(name);
6710
6
        if (eaddr == NULL)
6711
6
          bpf_error(cstate,
6712
6
              "unknown ether host '%s'", name);
6713
0
        tmp = gen_prevlinkhdr_check(cstate);
6714
0
        b = gen_ehostop(cstate, eaddr, dir);
6715
0
        if (tmp != NULL)
6716
0
          gen_and(tmp, b);
6717
0
        free(eaddr);
6718
0
        return b;
6719
6720
2
      case DLT_FDDI:
6721
2
        eaddr = pcap_ether_hostton(name);
6722
2
        if (eaddr == NULL)
6723
2
          bpf_error(cstate,
6724
2
              "unknown FDDI host '%s'", name);
6725
0
        b = gen_fhostop(cstate, eaddr, dir);
6726
0
        free(eaddr);
6727
0
        return b;
6728
6729
5
      case DLT_IEEE802:
6730
5
        eaddr = pcap_ether_hostton(name);
6731
5
        if (eaddr == NULL)
6732
5
          bpf_error(cstate,
6733
5
              "unknown token ring host '%s'", name);
6734
0
        b = gen_thostop(cstate, eaddr, dir);
6735
0
        free(eaddr);
6736
0
        return b;
6737
6738
4
      case DLT_IEEE802_11:
6739
11
      case DLT_PRISM_HEADER:
6740
15
      case DLT_IEEE802_11_RADIO_AVS:
6741
16
      case DLT_IEEE802_11_RADIO:
6742
22
      case DLT_PPI:
6743
22
        eaddr = pcap_ether_hostton(name);
6744
22
        if (eaddr == NULL)
6745
22
          bpf_error(cstate,
6746
22
              "unknown 802.11 host '%s'", name);
6747
0
        b = gen_wlanhostop(cstate, eaddr, dir);
6748
0
        free(eaddr);
6749
0
        return b;
6750
6751
4
      case DLT_IP_OVER_FC:
6752
4
        eaddr = pcap_ether_hostton(name);
6753
4
        if (eaddr == NULL)
6754
4
          bpf_error(cstate,
6755
4
              "unknown Fibre Channel host '%s'", name);
6756
0
        b = gen_ipfchostop(cstate, eaddr, dir);
6757
0
        free(eaddr);
6758
0
        return b;
6759
42
      }
6760
6761
3
      bpf_error(cstate, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6762
429
    } else if (proto == Q_DECNET) {
6763
2
      unsigned short dn_addr;
6764
6765
2
      if (!__pcap_nametodnaddr(name, &dn_addr)) {
6766
#ifdef  DECNETLIB
6767
        bpf_error(cstate, "unknown decnet host name '%s'\n", name);
6768
#else
6769
2
        bpf_error(cstate, "decnet name support not included, '%s' cannot be translated\n",
6770
2
          name);
6771
2
#endif
6772
2
      }
6773
      /*
6774
       * I don't think DECNET hosts can be multihomed, so
6775
       * there is no need to build up a list of addresses
6776
       */
6777
0
      return (gen_host(cstate, dn_addr, 0, proto, dir, q.addr));
6778
427
    } else {
6779
427
#ifdef INET6
6780
427
      memset(&mask128, 0xff, sizeof(mask128));
6781
427
#endif
6782
427
      res0 = res = pcap_nametoaddrinfo(name);
6783
427
      if (res == NULL)
6784
14
        bpf_error(cstate, "unknown host '%s'", name);
6785
413
      cstate->ai = res;
6786
413
      b = tmp = NULL;
6787
413
      tproto = proto;
6788
413
#ifdef INET6
6789
413
      tproto6 = proto;
6790
413
#endif
6791
413
      if (cstate->off_linktype.constant_part == OFFSET_NOT_SET &&
6792
413
          tproto == Q_DEFAULT) {
6793
136
        tproto = Q_IP;
6794
136
#ifdef INET6
6795
136
        tproto6 = Q_IPV6;
6796
136
#endif
6797
136
      }
6798
795
      for (res = res0; res; res = res->ai_next) {
6799
413
        switch (res->ai_family) {
6800
247
        case AF_INET:
6801
247
#ifdef INET6
6802
247
          if (tproto == Q_IPV6)
6803
1
            continue;
6804
246
#endif
6805
6806
246
          sin4 = (struct sockaddr_in *)
6807
246
            res->ai_addr;
6808
246
          tmp = gen_host(cstate, ntohl(sin4->sin_addr.s_addr),
6809
246
            0xffffffff, tproto, dir, q.addr);
6810
246
          break;
6811
0
#ifdef INET6
6812
166
        case AF_INET6:
6813
166
          if (tproto6 == Q_IP)
6814
1
            continue;
6815
6816
165
          sin6 = (struct sockaddr_in6 *)
6817
165
            res->ai_addr;
6818
165
          tmp = gen_host6(cstate, &sin6->sin6_addr,
6819
165
            &mask128, tproto6, dir, q.addr);
6820
165
          break;
6821
0
#endif
6822
0
        default:
6823
0
          continue;
6824
413
        }
6825
380
        if (b)
6826
0
          gen_or(b, tmp);
6827
380
        b = tmp;
6828
380
      }
6829
382
      cstate->ai = NULL;
6830
382
      freeaddrinfo(res0);
6831
382
      if (b == NULL) {
6832
2
        bpf_error(cstate, "unknown host '%s'%s", name,
6833
2
            (proto == Q_DEFAULT)
6834
2
          ? ""
6835
2
          : " for specified address family");
6836
2
      }
6837
380
      return b;
6838
382
    }
6839
6840
565
  case Q_PORT:
6841
565
    if (proto != Q_DEFAULT &&
6842
565
        proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6843
8
      bpf_error(cstate, "illegal qualifier of 'port'");
6844
557
    if (pcap_nametoport(name, &port, &real_proto) == 0)
6845
256
      bpf_error(cstate, "unknown port '%s'", name);
6846
301
    if (proto == Q_UDP) {
6847
50
      if (real_proto == IPPROTO_TCP)
6848
0
        bpf_error(cstate, "port '%s' is tcp", name);
6849
50
      else if (real_proto == IPPROTO_SCTP)
6850
0
        bpf_error(cstate, "port '%s' is sctp", name);
6851
50
      else
6852
        /* override PROTO_UNDEF */
6853
50
        real_proto = IPPROTO_UDP;
6854
50
    }
6855
301
    if (proto == Q_TCP) {
6856
51
      if (real_proto == IPPROTO_UDP)
6857
0
        bpf_error(cstate, "port '%s' is udp", name);
6858
6859
51
      else if (real_proto == IPPROTO_SCTP)
6860
0
        bpf_error(cstate, "port '%s' is sctp", name);
6861
51
      else
6862
        /* override PROTO_UNDEF */
6863
51
        real_proto = IPPROTO_TCP;
6864
51
    }
6865
301
    if (proto == Q_SCTP) {
6866
94
      if (real_proto == IPPROTO_UDP)
6867
0
        bpf_error(cstate, "port '%s' is udp", name);
6868
6869
94
      else if (real_proto == IPPROTO_TCP)
6870
0
        bpf_error(cstate, "port '%s' is tcp", name);
6871
94
      else
6872
        /* override PROTO_UNDEF */
6873
94
        real_proto = IPPROTO_SCTP;
6874
94
    }
6875
301
    if (port < 0)
6876
0
      bpf_error(cstate, "illegal port number %d < 0", port);
6877
301
    if (port > 65535)
6878
0
      bpf_error(cstate, "illegal port number %d > 65535", port);
6879
301
    b = gen_port(cstate, port, real_proto, dir);
6880
301
    gen_or(gen_port6(cstate, port, real_proto, dir), b);
6881
301
    return b;
6882
6883
0
  case Q_PORTRANGE:
6884
0
    if (proto != Q_DEFAULT &&
6885
0
        proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6886
0
      bpf_error(cstate, "illegal qualifier of 'portrange'");
6887
0
    if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
6888
0
      bpf_error(cstate, "unknown port in range '%s'", name);
6889
0
    if (proto == Q_UDP) {
6890
0
      if (real_proto == IPPROTO_TCP)
6891
0
        bpf_error(cstate, "port in range '%s' is tcp", name);
6892
0
      else if (real_proto == IPPROTO_SCTP)
6893
0
        bpf_error(cstate, "port in range '%s' is sctp", name);
6894
0
      else
6895
        /* override PROTO_UNDEF */
6896
0
        real_proto = IPPROTO_UDP;
6897
0
    }
6898
0
    if (proto == Q_TCP) {
6899
0
      if (real_proto == IPPROTO_UDP)
6900
0
        bpf_error(cstate, "port in range '%s' is udp", name);
6901
0
      else if (real_proto == IPPROTO_SCTP)
6902
0
        bpf_error(cstate, "port in range '%s' is sctp", name);
6903
0
      else
6904
        /* override PROTO_UNDEF */
6905
0
        real_proto = IPPROTO_TCP;
6906
0
    }
6907
0
    if (proto == Q_SCTP) {
6908
0
      if (real_proto == IPPROTO_UDP)
6909
0
        bpf_error(cstate, "port in range '%s' is udp", name);
6910
0
      else if (real_proto == IPPROTO_TCP)
6911
0
        bpf_error(cstate, "port in range '%s' is tcp", name);
6912
0
      else
6913
        /* override PROTO_UNDEF */
6914
0
        real_proto = IPPROTO_SCTP;
6915
0
    }
6916
0
    if (port1 < 0)
6917
0
      bpf_error(cstate, "illegal port number %d < 0", port1);
6918
0
    if (port1 > 65535)
6919
0
      bpf_error(cstate, "illegal port number %d > 65535", port1);
6920
0
    if (port2 < 0)
6921
0
      bpf_error(cstate, "illegal port number %d < 0", port2);
6922
0
    if (port2 > 65535)
6923
0
      bpf_error(cstate, "illegal port number %d > 65535", port2);
6924
6925
0
    b = gen_portrange(cstate, port1, port2, real_proto, dir);
6926
0
    gen_or(gen_portrange6(cstate, port1, port2, real_proto, dir), b);
6927
0
    return b;
6928
6929
0
  case Q_GATEWAY:
6930
#ifndef INET6
6931
    eaddr = pcap_ether_hostton(name);
6932
    if (eaddr == NULL)
6933
      bpf_error(cstate, "unknown ether host: %s", name);
6934
6935
    res = pcap_nametoaddrinfo(name);
6936
    cstate->ai = res;
6937
    if (res == NULL)
6938
      bpf_error(cstate, "unknown host '%s'", name);
6939
    b = gen_gateway(cstate, eaddr, res, proto, dir);
6940
    cstate->ai = NULL;
6941
    freeaddrinfo(res);
6942
    if (b == NULL)
6943
      bpf_error(cstate, "unknown host '%s'", name);
6944
    return b;
6945
#else
6946
0
    bpf_error(cstate, "'gateway' not supported in this configuration");
6947
0
#endif /*INET6*/
6948
6949
668
  case Q_PROTO:
6950
668
    real_proto = lookup_proto(cstate, name, proto);
6951
668
    if (real_proto >= 0)
6952
500
      return gen_proto(cstate, real_proto, proto, dir);
6953
168
    else
6954
168
      bpf_error(cstate, "unknown protocol: %s", name);
6955
6956
0
#if !defined(NO_PROTOCHAIN)
6957
114
  case Q_PROTOCHAIN:
6958
114
    real_proto = lookup_proto(cstate, name, proto);
6959
114
    if (real_proto >= 0)
6960
113
      return gen_protochain(cstate, real_proto, proto);
6961
1
    else
6962
1
      bpf_error(cstate, "unknown protocol: %s", name);
6963
0
#endif /* !defined(NO_PROTOCHAIN) */
6964
6965
124
  case Q_UNDEF:
6966
124
    syntax(cstate);
6967
    /*NOTREACHED*/
6968
1.25k
  }
6969
0
  abort();
6970
  /*NOTREACHED*/
6971
1.25k
}
6972
6973
struct block *
6974
gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2,
6975
    bpf_u_int32 masklen, struct qual q)
6976
203
{
6977
203
  register int nlen, mlen;
6978
203
  bpf_u_int32 n, m;
6979
6980
  /*
6981
   * Catch errors reported by us and routines below us, and return NULL
6982
   * on an error.
6983
   */
6984
203
  if (setjmp(cstate->top_ctx))
6985
92
    return (NULL);
6986
6987
111
  nlen = __pcap_atoin(s1, &n);
6988
111
  if (nlen < 0)
6989
1
    bpf_error(cstate, "invalid IPv4 address '%s'", s1);
6990
  /* Promote short ipaddr */
6991
110
  n <<= 32 - nlen;
6992
6993
110
  if (s2 != NULL) {
6994
0
    mlen = __pcap_atoin(s2, &m);
6995
0
    if (mlen < 0)
6996
0
      bpf_error(cstate, "invalid IPv4 address '%s'", s2);
6997
    /* Promote short ipaddr */
6998
0
    m <<= 32 - mlen;
6999
0
    if ((n & ~m) != 0)
7000
0
      bpf_error(cstate, "non-network bits set in \"%s mask %s\"",
7001
0
          s1, s2);
7002
110
  } else {
7003
    /* Convert mask len to mask */
7004
110
    if (masklen > 32)
7005
45
      bpf_error(cstate, "mask length must be <= 32");
7006
75
    if (masklen == 0) {
7007
      /*
7008
       * X << 32 is not guaranteed by C to be 0; it's
7009
       * undefined.
7010
       */
7011
75
      m = 0;
7012
75
    } else
7013
18.4E
      m = 0xffffffff << (32 - masklen);
7014
65
    if ((n & ~m) != 0)
7015
42
      bpf_error(cstate, "non-network bits set in \"%s/%d\"",
7016
42
          s1, masklen);
7017
65
  }
7018
7019
23
  switch (q.addr) {
7020
7021
112
  case Q_NET:
7022
112
    return gen_host(cstate, n, m, q.proto, q.dir, q.addr);
7023
7024
3
  default:
7025
3
    bpf_error(cstate, "Mask syntax for networks only");
7026
    /*NOTREACHED*/
7027
23
  }
7028
  /*NOTREACHED*/
7029
23
}
7030
7031
struct block *
7032
gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q)
7033
14.4k
{
7034
14.4k
  bpf_u_int32 mask;
7035
14.4k
  int proto;
7036
14.4k
  int dir;
7037
14.4k
  register int vlen;
7038
7039
  /*
7040
   * Catch errors reported by us and routines below us, and return NULL
7041
   * on an error.
7042
   */
7043
14.4k
  if (setjmp(cstate->top_ctx))
7044
241
    return (NULL);
7045
7046
14.2k
  proto = q.proto;
7047
14.2k
  dir = q.dir;
7048
14.2k
  if (s == NULL)
7049
14.2k
    vlen = 32;
7050
18.4E
  else if (q.proto == Q_DECNET) {
7051
52
    vlen = __pcap_atodn(s, &v);
7052
52
    if (vlen == 0)
7053
1
      bpf_error(cstate, "malformed decnet address '%s'", s);
7054
18.4E
  } else {
7055
18.4E
    vlen = __pcap_atoin(s, &v);
7056
18.4E
    if (vlen < 0)
7057
4
      bpf_error(cstate, "invalid IPv4 address '%s'", s);
7058
18.4E
  }
7059
7060
14.2k
  switch (q.addr) {
7061
7062
1.78k
  case Q_DEFAULT:
7063
2.02k
  case Q_HOST:
7064
8.01k
  case Q_NET:
7065
8.01k
    if (proto == Q_DECNET)
7066
671
      return gen_host(cstate, v, 0, proto, dir, q.addr);
7067
7.34k
    else if (proto == Q_LINK) {
7068
11
      bpf_error(cstate, "illegal link layer address");
7069
7.33k
    } else {
7070
7.33k
      mask = 0xffffffff;
7071
7.33k
      if (s == NULL && q.addr == Q_NET) {
7072
        /* Promote short net number */
7073
18.6k
        while (v && (v & 0xff000000) == 0) {
7074
13.0k
          v <<= 8;
7075
13.0k
          mask <<= 8;
7076
13.0k
        }
7077
5.60k
      } else {
7078
        /* Promote short ipaddr */
7079
1.72k
        v <<= 32 - vlen;
7080
1.72k
        mask <<= 32 - vlen ;
7081
1.72k
      }
7082
7.33k
      return gen_host(cstate, v, mask, proto, dir, q.addr);
7083
7.33k
    }
7084
7085
3.16k
  case Q_PORT:
7086
3.16k
    if (proto == Q_UDP)
7087
77
      proto = IPPROTO_UDP;
7088
3.08k
    else if (proto == Q_TCP)
7089
89
      proto = IPPROTO_TCP;
7090
2.99k
    else if (proto == Q_SCTP)
7091
56
      proto = IPPROTO_SCTP;
7092
2.94k
    else if (proto == Q_DEFAULT)
7093
2.93k
      proto = PROTO_UNDEF;
7094
7
    else
7095
7
      bpf_error(cstate, "illegal qualifier of 'port'");
7096
7097
3.15k
    if (v > 65535)
7098
24
      bpf_error(cstate, "illegal port number %u > 65535", v);
7099
7100
3.13k
      {
7101
3.13k
    struct block *b;
7102
3.13k
    b = gen_port(cstate, v, proto, dir);
7103
3.13k
    gen_or(gen_port6(cstate, v, proto, dir), b);
7104
3.13k
    return b;
7105
3.15k
      }
7106
7107
0
  case Q_PORTRANGE:
7108
0
    if (proto == Q_UDP)
7109
0
      proto = IPPROTO_UDP;
7110
0
    else if (proto == Q_TCP)
7111
0
      proto = IPPROTO_TCP;
7112
0
    else if (proto == Q_SCTP)
7113
0
      proto = IPPROTO_SCTP;
7114
0
    else if (proto == Q_DEFAULT)
7115
0
      proto = PROTO_UNDEF;
7116
0
    else
7117
0
      bpf_error(cstate, "illegal qualifier of 'portrange'");
7118
7119
0
    if (v > 65535)
7120
0
      bpf_error(cstate, "illegal port number %u > 65535", v);
7121
7122
0
      {
7123
0
    struct block *b;
7124
0
    b = gen_portrange(cstate, v, v, proto, dir);
7125
0
    gen_or(gen_portrange6(cstate, v, v, proto, dir), b);
7126
0
    return b;
7127
0
      }
7128
7129
0
  case Q_GATEWAY:
7130
0
    bpf_error(cstate, "'gateway' requires a name");
7131
    /*NOTREACHED*/
7132
7133
2.75k
  case Q_PROTO:
7134
2.75k
    return gen_proto(cstate, v, proto, dir);
7135
7136
0
#if !defined(NO_PROTOCHAIN)
7137
514
  case Q_PROTOCHAIN:
7138
514
    return gen_protochain(cstate, v, proto);
7139
0
#endif
7140
7141
11
  case Q_UNDEF:
7142
11
    syntax(cstate);
7143
    /*NOTREACHED*/
7144
7145
0
  default:
7146
0
    abort();
7147
    /*NOTREACHED*/
7148
14.2k
  }
7149
  /*NOTREACHED*/
7150
14.2k
}
7151
7152
#ifdef INET6
7153
struct block *
7154
gen_mcode6(compiler_state_t *cstate, const char *s1, const char *s2,
7155
    bpf_u_int32 masklen, struct qual q)
7156
1.32k
{
7157
1.32k
  struct addrinfo *res;
7158
1.32k
  struct in6_addr *addr;
7159
1.32k
  struct in6_addr mask;
7160
1.32k
  struct block *b;
7161
1.32k
  uint32_t *a, *m;
7162
7163
  /*
7164
   * Catch errors reported by us and routines below us, and return NULL
7165
   * on an error.
7166
   */
7167
1.32k
  if (setjmp(cstate->top_ctx))
7168
243
    return (NULL);
7169
7170
1.08k
  if (s2)
7171
0
    bpf_error(cstate, "no mask %s supported", s2);
7172
7173
1.08k
  res = pcap_nametoaddrinfo(s1);
7174
1.08k
  if (!res)
7175
0
    bpf_error(cstate, "invalid ip6 address %s", s1);
7176
1.08k
  cstate->ai = res;
7177
1.08k
  if (res->ai_next)
7178
0
    bpf_error(cstate, "%s resolved to multiple address", s1);
7179
1.08k
  addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
7180
7181
1.08k
  if (masklen > sizeof(mask.s6_addr) * 8)
7182
44
    bpf_error(cstate, "mask length must be <= %u", (unsigned int)(sizeof(mask.s6_addr) * 8));
7183
1.04k
  memset(&mask, 0, sizeof(mask));
7184
1.04k
  memset(&mask.s6_addr, 0xff, masklen / 8);
7185
1.04k
  if (masklen % 8) {
7186
199
    mask.s6_addr[masklen / 8] =
7187
199
      (0xff << (8 - masklen % 8)) & 0xff;
7188
199
  }
7189
7190
1.04k
  a = (uint32_t *)addr;
7191
1.04k
  m = (uint32_t *)&mask;
7192
1.23k
  if ((a[0] & ~m[0]) || (a[1] & ~m[1])
7193
1.19k
   || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
7194
138
    bpf_error(cstate, "non-network bits set in \"%s/%d\"", s1, masklen);
7195
138
  }
7196
7197
903
  switch (q.addr) {
7198
7199
310
  case Q_DEFAULT:
7200
434
  case Q_HOST:
7201
434
    if (masklen != 128)
7202
11
      bpf_error(cstate, "Mask syntax for networks only");
7203
    /* FALLTHROUGH */
7204
7205
1.13k
  case Q_NET:
7206
1.13k
    b = gen_host6(cstate, addr, &mask, q.proto, q.dir, q.addr);
7207
1.13k
    cstate->ai = NULL;
7208
1.13k
    freeaddrinfo(res);
7209
1.13k
    return b;
7210
7211
4
  default:
7212
4
    bpf_error(cstate, "invalid qualifier against IPv6 address");
7213
    /*NOTREACHED*/
7214
903
  }
7215
903
}
7216
#endif /*INET6*/
7217
7218
struct block *
7219
gen_ecode(compiler_state_t *cstate, const char *s, struct qual q)
7220
1.34k
{
7221
1.34k
  struct block *b, *tmp;
7222
7223
  /*
7224
   * Catch errors reported by us and routines below us, and return NULL
7225
   * on an error.
7226
   */
7227
1.34k
  if (setjmp(cstate->top_ctx))
7228
36
    return (NULL);
7229
7230
1.33k
  if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
7231
1.33k
    cstate->e = pcap_ether_aton(s);
7232
1.33k
    if (cstate->e == NULL)
7233
0
      bpf_error(cstate, "malloc");
7234
1.33k
    switch (cstate->linktype) {
7235
143
    case DLT_EN10MB:
7236
186
    case DLT_NETANALYZER:
7237
248
    case DLT_NETANALYZER_TRANSPARENT:
7238
248
      tmp = gen_prevlinkhdr_check(cstate);
7239
248
      b = gen_ehostop(cstate, cstate->e, (int)q.dir);
7240
248
      if (tmp != NULL)
7241
46
        gen_and(tmp, b);
7242
248
      break;
7243
179
    case DLT_FDDI:
7244
179
      b = gen_fhostop(cstate, cstate->e, (int)q.dir);
7245
179
      break;
7246
181
    case DLT_IEEE802:
7247
181
      b = gen_thostop(cstate, cstate->e, (int)q.dir);
7248
181
      break;
7249
137
    case DLT_IEEE802_11:
7250
282
    case DLT_PRISM_HEADER:
7251
356
    case DLT_IEEE802_11_RADIO_AVS:
7252
446
    case DLT_IEEE802_11_RADIO:
7253
547
    case DLT_PPI:
7254
547
      b = gen_wlanhostop(cstate, cstate->e, (int)q.dir);
7255
547
      break;
7256
177
    case DLT_IP_OVER_FC:
7257
177
      b = gen_ipfchostop(cstate, cstate->e, (int)q.dir);
7258
177
      break;
7259
2
    default:
7260
2
      free(cstate->e);
7261
2
      cstate->e = NULL;
7262
2
      bpf_error(cstate, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7263
      /*NOTREACHED*/
7264
1.33k
    }
7265
1.30k
    free(cstate->e);
7266
1.30k
    cstate->e = NULL;
7267
1.30k
    return (b);
7268
1.33k
  }
7269
18.4E
  bpf_error(cstate, "ethernet address used in non-ether expression");
7270
  /*NOTREACHED*/
7271
1.30k
}
7272
7273
void
7274
sappend(struct slist *s0, struct slist *s1)
7275
355k
{
7276
  /*
7277
   * This is definitely not the best way to do this, but the
7278
   * lists will rarely get long.
7279
   */
7280
8.20M
  while (s0->next)
7281
7.85M
    s0 = s0->next;
7282
355k
  s0->next = s1;
7283
355k
}
7284
7285
static struct slist *
7286
xfer_to_x(compiler_state_t *cstate, struct arth *a)
7287
19.8k
{
7288
19.8k
  struct slist *s;
7289
7290
19.8k
  s = new_stmt(cstate, BPF_LDX|BPF_MEM);
7291
19.8k
  s->s.k = a->regno;
7292
19.8k
  return s;
7293
19.8k
}
7294
7295
static struct slist *
7296
xfer_to_a(compiler_state_t *cstate, struct arth *a)
7297
36.2k
{
7298
36.2k
  struct slist *s;
7299
7300
36.2k
  s = new_stmt(cstate, BPF_LD|BPF_MEM);
7301
36.2k
  s->s.k = a->regno;
7302
36.2k
  return s;
7303
36.2k
}
7304
7305
/*
7306
 * Modify "index" to use the value stored into its register as an
7307
 * offset relative to the beginning of the header for the protocol
7308
 * "proto", and allocate a register and put an item "size" bytes long
7309
 * (1, 2, or 4) at that offset into that register, making it the register
7310
 * for "index".
7311
 */
7312
static struct arth *
7313
gen_load_internal(compiler_state_t *cstate, int proto, struct arth *inst,
7314
    bpf_u_int32 size)
7315
3.85k
{
7316
3.85k
  int size_code;
7317
3.85k
  struct slist *s, *tmp;
7318
3.85k
  struct block *b;
7319
3.85k
  int regno = alloc_reg(cstate);
7320
7321
3.85k
  free_reg(cstate, inst->regno);
7322
3.85k
  switch (size) {
7323
7324
1
  default:
7325
1
    bpf_error(cstate, "data size must be 1, 2, or 4");
7326
    /*NOTREACHED*/
7327
7328
3.35k
  case 1:
7329
3.35k
    size_code = BPF_B;
7330
3.35k
    break;
7331
7332
230
  case 2:
7333
230
    size_code = BPF_H;
7334
230
    break;
7335
7336
268
  case 4:
7337
268
    size_code = BPF_W;
7338
268
    break;
7339
3.85k
  }
7340
3.85k
  switch (proto) {
7341
1
  default:
7342
1
    bpf_error(cstate, "unsupported index operation");
7343
7344
76
  case Q_RADIO:
7345
    /*
7346
     * The offset is relative to the beginning of the packet
7347
     * data, if we have a radio header.  (If we don't, this
7348
     * is an error.)
7349
     */
7350
76
    if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS &&
7351
76
        cstate->linktype != DLT_IEEE802_11_RADIO &&
7352
76
        cstate->linktype != DLT_PRISM_HEADER)
7353
14
      bpf_error(cstate, "radio information not present in capture");
7354
7355
    /*
7356
     * Load into the X register the offset computed into the
7357
     * register specified by "index".
7358
     */
7359
62
    s = xfer_to_x(cstate, inst);
7360
7361
    /*
7362
     * Load the item at that offset.
7363
     */
7364
62
    tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7365
62
    sappend(s, tmp);
7366
62
    sappend(inst->s, s);
7367
62
    break;
7368
7369
845
  case Q_LINK:
7370
    /*
7371
     * The offset is relative to the beginning of
7372
     * the link-layer header.
7373
     *
7374
     * XXX - what about ATM LANE?  Should the index be
7375
     * relative to the beginning of the AAL5 frame, so
7376
     * that 0 refers to the beginning of the LE Control
7377
     * field, or relative to the beginning of the LAN
7378
     * frame, so that 0 refers, for Ethernet LANE, to
7379
     * the beginning of the destination address?
7380
     */
7381
845
    s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr);
7382
7383
    /*
7384
     * If "s" is non-null, it has code to arrange that the
7385
     * X register contains the length of the prefix preceding
7386
     * the link-layer header.  Add to it the offset computed
7387
     * into the register specified by "index", and move that
7388
     * into the X register.  Otherwise, just load into the X
7389
     * register the offset computed into the register specified
7390
     * by "index".
7391
     */
7392
845
    if (s != NULL) {
7393
252
      sappend(s, xfer_to_a(cstate, inst));
7394
252
      sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7395
252
      sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7396
252
    } else
7397
593
      s = xfer_to_x(cstate, inst);
7398
7399
    /*
7400
     * Load the item at the sum of the offset we've put in the
7401
     * X register and the offset of the start of the link
7402
     * layer header (which is 0 if the radio header is
7403
     * variable-length; that header length is what we put
7404
     * into the X register and then added to the index).
7405
     */
7406
845
    tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7407
845
    tmp->s.k = cstate->off_linkhdr.constant_part;
7408
845
    sappend(s, tmp);
7409
845
    sappend(inst->s, s);
7410
845
    break;
7411
7412
782
  case Q_IP:
7413
820
  case Q_ARP:
7414
855
  case Q_RARP:
7415
894
  case Q_ATALK:
7416
946
  case Q_DECNET:
7417
1.03k
  case Q_SCA:
7418
1.06k
  case Q_LAT:
7419
1.12k
  case Q_MOPRC:
7420
1.15k
  case Q_MOPDL:
7421
1.25k
  case Q_IPV6:
7422
    /*
7423
     * The offset is relative to the beginning of
7424
     * the network-layer header.
7425
     * XXX - are there any cases where we want
7426
     * cstate->off_nl_nosnap?
7427
     */
7428
1.25k
    s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
7429
7430
    /*
7431
     * If "s" is non-null, it has code to arrange that the
7432
     * X register contains the variable part of the offset
7433
     * of the link-layer payload.  Add to it the offset
7434
     * computed into the register specified by "index",
7435
     * and move that into the X register.  Otherwise, just
7436
     * load into the X register the offset computed into
7437
     * the register specified by "index".
7438
     */
7439
1.25k
    if (s != NULL) {
7440
362
      sappend(s, xfer_to_a(cstate, inst));
7441
362
      sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7442
362
      sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7443
362
    } else
7444
889
      s = xfer_to_x(cstate, inst);
7445
7446
    /*
7447
     * Load the item at the sum of the offset we've put in the
7448
     * X register, the offset of the start of the network
7449
     * layer header from the beginning of the link-layer
7450
     * payload, and the constant part of the offset of the
7451
     * start of the link-layer payload.
7452
     */
7453
1.25k
    tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7454
1.25k
    tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
7455
1.25k
    sappend(s, tmp);
7456
1.25k
    sappend(inst->s, s);
7457
7458
    /*
7459
     * Do the computation only if the packet contains
7460
     * the protocol in question.
7461
     */
7462
1.25k
    b = gen_proto_abbrev_internal(cstate, proto);
7463
1.25k
    if (inst->b)
7464
435
      gen_and(inst->b, b);
7465
1.25k
    inst->b = b;
7466
1.25k
    break;
7467
7468
60
  case Q_SCTP:
7469
340
  case Q_TCP:
7470
714
  case Q_UDP:
7471
809
  case Q_ICMP:
7472
856
  case Q_IGMP:
7473
944
  case Q_IGRP:
7474
1.37k
  case Q_PIM:
7475
1.40k
  case Q_VRRP:
7476
1.46k
  case Q_CARP:
7477
    /*
7478
     * The offset is relative to the beginning of
7479
     * the transport-layer header.
7480
     *
7481
     * Load the X register with the length of the IPv4 header
7482
     * (plus the offset of the link-layer header, if it's
7483
     * a variable-length header), in bytes.
7484
     *
7485
     * XXX - are there any cases where we want
7486
     * cstate->off_nl_nosnap?
7487
     * XXX - we should, if we're built with
7488
     * IPv6 support, generate code to load either
7489
     * IPv4, IPv6, or both, as appropriate.
7490
     */
7491
1.46k
    s = gen_loadx_iphdrlen(cstate);
7492
7493
    /*
7494
     * The X register now contains the sum of the variable
7495
     * part of the offset of the link-layer payload and the
7496
     * length of the network-layer header.
7497
     *
7498
     * Load into the A register the offset relative to
7499
     * the beginning of the transport layer header,
7500
     * add the X register to that, move that to the
7501
     * X register, and load with an offset from the
7502
     * X register equal to the sum of the constant part of
7503
     * the offset of the link-layer payload and the offset,
7504
     * relative to the beginning of the link-layer payload,
7505
     * of the network-layer header.
7506
     */
7507
1.46k
    sappend(s, xfer_to_a(cstate, inst));
7508
1.46k
    sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7509
1.46k
    sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7510
1.46k
    sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code));
7511
1.46k
    tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
7512
1.46k
    sappend(inst->s, s);
7513
7514
    /*
7515
     * Do the computation only if the packet contains
7516
     * the protocol in question - which is true only
7517
     * if this is an IP datagram and is the first or
7518
     * only fragment of that datagram.
7519
     */
7520
1.46k
    gen_and(gen_proto_abbrev_internal(cstate, proto), b = gen_ipfrag(cstate));
7521
1.46k
    if (inst->b)
7522
645
      gen_and(inst->b, b);
7523
1.46k
    gen_and(gen_proto_abbrev_internal(cstate, Q_IP), b);
7524
1.46k
    inst->b = b;
7525
1.46k
    break;
7526
216
  case Q_ICMPV6:
7527
        /*
7528
        * Do the computation only if the packet contains
7529
        * the protocol in question.
7530
        */
7531
216
        b = gen_proto_abbrev_internal(cstate, Q_IPV6);
7532
216
        if (inst->b) {
7533
169
            gen_and(inst->b, b);
7534
169
        }
7535
216
        inst->b = b;
7536
7537
        /*
7538
        * Check if we have an icmp6 next header
7539
        */
7540
216
        b = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, 58);
7541
216
        if (inst->b) {
7542
216
            gen_and(inst->b, b);
7543
216
        }
7544
216
        inst->b = b;
7545
7546
7547
216
        s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
7548
        /*
7549
        * If "s" is non-null, it has code to arrange that the
7550
        * X register contains the variable part of the offset
7551
        * of the link-layer payload.  Add to it the offset
7552
        * computed into the register specified by "index",
7553
        * and move that into the X register.  Otherwise, just
7554
        * load into the X register the offset computed into
7555
        * the register specified by "index".
7556
        */
7557
216
        if (s != NULL) {
7558
90
            sappend(s, xfer_to_a(cstate, inst));
7559
90
            sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7560
90
            sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7561
126
        } else {
7562
126
            s = xfer_to_x(cstate, inst);
7563
126
        }
7564
7565
        /*
7566
        * Load the item at the sum of the offset we've put in the
7567
        * X register, the offset of the start of the network
7568
        * layer header from the beginning of the link-layer
7569
        * payload, and the constant part of the offset of the
7570
        * start of the link-layer payload.
7571
        */
7572
216
        tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7573
216
        tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40;
7574
7575
216
        sappend(s, tmp);
7576
216
        sappend(inst->s, s);
7577
7578
216
        break;
7579
3.85k
  }
7580
3.83k
  inst->regno = regno;
7581
3.83k
  s = new_stmt(cstate, BPF_ST);
7582
3.83k
  s->s.k = regno;
7583
3.83k
  sappend(inst->s, s);
7584
7585
3.83k
  return inst;
7586
3.85k
}
7587
7588
struct arth *
7589
gen_load(compiler_state_t *cstate, int proto, struct arth *inst,
7590
    bpf_u_int32 size)
7591
3.85k
{
7592
  /*
7593
   * Catch errors reported by us and routines below us, and return NULL
7594
   * on an error.
7595
   */
7596
3.85k
  if (setjmp(cstate->top_ctx))
7597
24
    return (NULL);
7598
7599
3.83k
  return gen_load_internal(cstate, proto, inst, size);
7600
3.85k
}
7601
7602
static struct block *
7603
gen_relation_internal(compiler_state_t *cstate, int code, struct arth *a0,
7604
    struct arth *a1, int reversed)
7605
4.55k
{
7606
4.55k
  struct slist *s0, *s1, *s2;
7607
4.55k
  struct block *b, *tmp;
7608
7609
4.55k
  s0 = xfer_to_x(cstate, a1);
7610
4.55k
  s1 = xfer_to_a(cstate, a0);
7611
4.55k
  if (code == BPF_JEQ) {
7612
1.67k
    s2 = new_stmt(cstate, BPF_ALU|BPF_SUB|BPF_X);
7613
1.67k
    b = new_block(cstate, JMP(code));
7614
1.67k
    sappend(s1, s2);
7615
1.67k
  }
7616
2.87k
  else
7617
2.87k
    b = new_block(cstate, BPF_JMP|code|BPF_X);
7618
4.55k
  if (reversed)
7619
1.71k
    gen_not(b);
7620
7621
4.55k
  sappend(s0, s1);
7622
4.55k
  sappend(a1->s, s0);
7623
4.55k
  sappend(a0->s, a1->s);
7624
7625
4.55k
  b->stmts = a0->s;
7626
7627
4.55k
  free_reg(cstate, a0->regno);
7628
4.55k
  free_reg(cstate, a1->regno);
7629
7630
  /* 'and' together protocol checks */
7631
4.55k
  if (a0->b) {
7632
259
    if (a1->b) {
7633
86
      gen_and(a0->b, tmp = a1->b);
7634
86
    }
7635
173
    else
7636
173
      tmp = a0->b;
7637
259
  } else
7638
4.29k
    tmp = a1->b;
7639
7640
4.55k
  if (tmp)
7641
723
    gen_and(tmp, b);
7642
7643
4.55k
  return b;
7644
4.55k
}
7645
7646
struct block *
7647
gen_relation(compiler_state_t *cstate, int code, struct arth *a0,
7648
    struct arth *a1, int reversed)
7649
4.55k
{
7650
  /*
7651
   * Catch errors reported by us and routines below us, and return NULL
7652
   * on an error.
7653
   */
7654
4.55k
  if (setjmp(cstate->top_ctx))
7655
0
    return (NULL);
7656
7657
4.55k
  return gen_relation_internal(cstate, code, a0, a1, reversed);
7658
4.55k
}
7659
7660
struct arth *
7661
gen_loadlen(compiler_state_t *cstate)
7662
2.66k
{
7663
2.66k
  int regno;
7664
2.66k
  struct arth *a;
7665
2.66k
  struct slist *s;
7666
7667
  /*
7668
   * Catch errors reported by us and routines below us, and return NULL
7669
   * on an error.
7670
   */
7671
2.66k
  if (setjmp(cstate->top_ctx))
7672
4
    return (NULL);
7673
7674
2.66k
  regno = alloc_reg(cstate);
7675
2.66k
  a = (struct arth *)newchunk(cstate, sizeof(*a));
7676
2.66k
  s = new_stmt(cstate, BPF_LD|BPF_LEN);
7677
2.66k
  s->next = new_stmt(cstate, BPF_ST);
7678
2.66k
  s->next->s.k = regno;
7679
2.66k
  a->s = s;
7680
2.66k
  a->regno = regno;
7681
7682
2.66k
  return a;
7683
2.66k
}
7684
7685
static struct arth *
7686
gen_loadi_internal(compiler_state_t *cstate, bpf_u_int32 val)
7687
21.2k
{
7688
21.2k
  struct arth *a;
7689
21.2k
  struct slist *s;
7690
21.2k
  int reg;
7691
7692
21.2k
  a = (struct arth *)newchunk(cstate, sizeof(*a));
7693
7694
21.2k
  reg = alloc_reg(cstate);
7695
7696
21.2k
  s = new_stmt(cstate, BPF_LD|BPF_IMM);
7697
21.2k
  s->s.k = val;
7698
21.2k
  s->next = new_stmt(cstate, BPF_ST);
7699
21.2k
  s->next->s.k = reg;
7700
21.2k
  a->s = s;
7701
21.2k
  a->regno = reg;
7702
7703
21.2k
  return a;
7704
21.2k
}
7705
7706
struct arth *
7707
gen_loadi(compiler_state_t *cstate, bpf_u_int32 val)
7708
21.2k
{
7709
  /*
7710
   * Catch errors reported by us and routines below us, and return NULL
7711
   * on an error.
7712
   */
7713
21.2k
  if (setjmp(cstate->top_ctx))
7714
2
    return (NULL);
7715
7716
21.2k
  return gen_loadi_internal(cstate, val);
7717
21.2k
}
7718
7719
/*
7720
 * The a_arg dance is to avoid annoying whining by compilers that
7721
 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7722
 * It's not *used* after setjmp returns.
7723
 */
7724
struct arth *
7725
gen_neg(compiler_state_t *cstate, struct arth *a_arg)
7726
15.9k
{
7727
15.9k
  struct arth *a = a_arg;
7728
15.9k
  struct slist *s;
7729
7730
  /*
7731
   * Catch errors reported by us and routines below us, and return NULL
7732
   * on an error.
7733
   */
7734
15.9k
  if (setjmp(cstate->top_ctx))
7735
0
    return (NULL);
7736
7737
15.9k
  s = xfer_to_a(cstate, a);
7738
15.9k
  sappend(a->s, s);
7739
15.9k
  s = new_stmt(cstate, BPF_ALU|BPF_NEG);
7740
15.9k
  s->s.k = 0;
7741
15.9k
  sappend(a->s, s);
7742
15.9k
  s = new_stmt(cstate, BPF_ST);
7743
15.9k
  s->s.k = a->regno;
7744
15.9k
  sappend(a->s, s);
7745
7746
15.9k
  return a;
7747
15.9k
}
7748
7749
/*
7750
 * The a0_arg dance is to avoid annoying whining by compilers that
7751
 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7752
 * It's not *used* after setjmp returns.
7753
 */
7754
struct arth *
7755
gen_arth(compiler_state_t *cstate, int code, struct arth *a0_arg,
7756
    struct arth *a1)
7757
13.6k
{
7758
13.6k
  struct arth *a0 = a0_arg;
7759
13.6k
  struct slist *s0, *s1, *s2;
7760
7761
  /*
7762
   * Catch errors reported by us and routines below us, and return NULL
7763
   * on an error.
7764
   */
7765
13.6k
  if (setjmp(cstate->top_ctx))
7766
58
    return (NULL);
7767
7768
  /*
7769
   * Disallow division by, or modulus by, zero; we do this here
7770
   * so that it gets done even if the optimizer is disabled.
7771
   *
7772
   * Also disallow shifts by a value greater than 31; we do this
7773
   * here, for the same reason.
7774
   */
7775
13.6k
  if (code == BPF_DIV) {
7776
1.85k
    if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
7777
6
      bpf_error(cstate, "division by zero");
7778
11.7k
  } else if (code == BPF_MOD) {
7779
1.05k
    if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
7780
5
      bpf_error(cstate, "modulus by zero");
7781
10.7k
  } else if (code == BPF_LSH || code == BPF_RSH) {
7782
1.52k
    if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k > 31)
7783
47
      bpf_error(cstate, "shift by more than 31 bits");
7784
1.52k
  }
7785
13.5k
  s0 = xfer_to_x(cstate, a1);
7786
13.5k
  s1 = xfer_to_a(cstate, a0);
7787
13.5k
  s2 = new_stmt(cstate, BPF_ALU|BPF_X|code);
7788
7789
13.5k
  sappend(s1, s2);
7790
13.5k
  sappend(s0, s1);
7791
13.5k
  sappend(a1->s, s0);
7792
13.5k
  sappend(a0->s, a1->s);
7793
7794
13.5k
  free_reg(cstate, a0->regno);
7795
13.5k
  free_reg(cstate, a1->regno);
7796
7797
13.5k
  s0 = new_stmt(cstate, BPF_ST);
7798
13.5k
  a0->regno = s0->s.k = alloc_reg(cstate);
7799
13.5k
  sappend(a0->s, s0);
7800
7801
13.5k
  return a0;
7802
13.6k
}
7803
7804
/*
7805
 * Initialize the table of used registers and the current register.
7806
 */
7807
static void
7808
init_regs(compiler_state_t *cstate)
7809
13.5k
{
7810
13.5k
  cstate->curreg = 0;
7811
13.5k
  memset(cstate->regused, 0, sizeof cstate->regused);
7812
13.5k
}
7813
7814
/*
7815
 * Return the next free register.
7816
 */
7817
static int
7818
alloc_reg(compiler_state_t *cstate)
7819
44.8k
{
7820
44.8k
  int n = BPF_MEMWORDS;
7821
7822
70.9k
  while (--n >= 0) {
7823
70.9k
    if (cstate->regused[cstate->curreg])
7824
26.1k
      cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS;
7825
44.8k
    else {
7826
44.8k
      cstate->regused[cstate->curreg] = 1;
7827
44.8k
      return cstate->curreg;
7828
44.8k
    }
7829
70.9k
  }
7830
17
  bpf_error(cstate, "too many registers needed to evaluate expression");
7831
  /*NOTREACHED*/
7832
44.8k
}
7833
7834
/*
7835
 * Return a register to the table so it can
7836
 * be used later.
7837
 */
7838
static void
7839
free_reg(compiler_state_t *cstate, int n)
7840
41.1k
{
7841
41.1k
  cstate->regused[n] = 0;
7842
41.1k
}
7843
7844
static struct block *
7845
gen_len(compiler_state_t *cstate, int jmp, int n)
7846
56
{
7847
56
  struct slist *s;
7848
56
  struct block *b;
7849
7850
56
  s = new_stmt(cstate, BPF_LD|BPF_LEN);
7851
56
  b = new_block(cstate, JMP(jmp));
7852
56
  b->stmts = s;
7853
56
  b->s.k = n;
7854
7855
56
  return b;
7856
56
}
7857
7858
struct block *
7859
gen_greater(compiler_state_t *cstate, int n)
7860
0
{
7861
  /*
7862
   * Catch errors reported by us and routines below us, and return NULL
7863
   * on an error.
7864
   */
7865
0
  if (setjmp(cstate->top_ctx))
7866
0
    return (NULL);
7867
7868
0
  return gen_len(cstate, BPF_JGE, n);
7869
0
}
7870
7871
/*
7872
 * Actually, this is less than or equal.
7873
 */
7874
struct block *
7875
gen_less(compiler_state_t *cstate, int n)
7876
56
{
7877
56
  struct block *b;
7878
7879
  /*
7880
   * Catch errors reported by us and routines below us, and return NULL
7881
   * on an error.
7882
   */
7883
56
  if (setjmp(cstate->top_ctx))
7884
0
    return (NULL);
7885
7886
56
  b = gen_len(cstate, BPF_JGT, n);
7887
56
  gen_not(b);
7888
7889
56
  return b;
7890
56
}
7891
7892
/*
7893
 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7894
 * the beginning of the link-layer header.
7895
 * XXX - that means you can't test values in the radiotap header, but
7896
 * as that header is difficult if not impossible to parse generally
7897
 * without a loop, that might not be a severe problem.  A new keyword
7898
 * "radio" could be added for that, although what you'd really want
7899
 * would be a way of testing particular radio header values, which
7900
 * would generate code appropriate to the radio header in question.
7901
 */
7902
struct block *
7903
gen_byteop(compiler_state_t *cstate, int op, int idx, bpf_u_int32 val)
7904
211
{
7905
211
  struct block *b;
7906
211
  struct slist *s;
7907
7908
  /*
7909
   * Catch errors reported by us and routines below us, and return NULL
7910
   * on an error.
7911
   */
7912
211
  if (setjmp(cstate->top_ctx))
7913
1
    return (NULL);
7914
7915
210
  switch (op) {
7916
0
  default:
7917
0
    abort();
7918
7919
31
  case '=':
7920
31
    return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
7921
7922
39
  case '<':
7923
39
    b = gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
7924
39
    return b;
7925
7926
50
  case '>':
7927
50
    b = gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
7928
50
    return b;
7929
7930
43
  case '|':
7931
43
    s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K);
7932
43
    break;
7933
7934
48
  case '&':
7935
48
    s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
7936
48
    break;
7937
210
  }
7938
91
  s->s.k = val;
7939
91
  b = new_block(cstate, JMP(BPF_JEQ));
7940
91
  b->stmts = s;
7941
91
  gen_not(b);
7942
7943
91
  return b;
7944
210
}
7945
7946
static const u_char abroadcast[] = { 0x0 };
7947
7948
struct block *
7949
gen_broadcast(compiler_state_t *cstate, int proto)
7950
0
{
7951
0
  bpf_u_int32 hostmask;
7952
0
  struct block *b0, *b1, *b2;
7953
0
  static const u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7954
7955
  /*
7956
   * Catch errors reported by us and routines below us, and return NULL
7957
   * on an error.
7958
   */
7959
0
  if (setjmp(cstate->top_ctx))
7960
0
    return (NULL);
7961
7962
0
  switch (proto) {
7963
7964
0
  case Q_DEFAULT:
7965
0
  case Q_LINK:
7966
0
    switch (cstate->linktype) {
7967
0
    case DLT_ARCNET:
7968
0
    case DLT_ARCNET_LINUX:
7969
0
      return gen_ahostop(cstate, abroadcast, Q_DST);
7970
0
    case DLT_EN10MB:
7971
0
    case DLT_NETANALYZER:
7972
0
    case DLT_NETANALYZER_TRANSPARENT:
7973
0
      b1 = gen_prevlinkhdr_check(cstate);
7974
0
      b0 = gen_ehostop(cstate, ebroadcast, Q_DST);
7975
0
      if (b1 != NULL)
7976
0
        gen_and(b1, b0);
7977
0
      return b0;
7978
0
    case DLT_FDDI:
7979
0
      return gen_fhostop(cstate, ebroadcast, Q_DST);
7980
0
    case DLT_IEEE802:
7981
0
      return gen_thostop(cstate, ebroadcast, Q_DST);
7982
0
    case DLT_IEEE802_11:
7983
0
    case DLT_PRISM_HEADER:
7984
0
    case DLT_IEEE802_11_RADIO_AVS:
7985
0
    case DLT_IEEE802_11_RADIO:
7986
0
    case DLT_PPI:
7987
0
      return gen_wlanhostop(cstate, ebroadcast, Q_DST);
7988
0
    case DLT_IP_OVER_FC:
7989
0
      return gen_ipfchostop(cstate, ebroadcast, Q_DST);
7990
0
    default:
7991
0
      bpf_error(cstate, "not a broadcast link");
7992
0
    }
7993
    /*NOTREACHED*/
7994
7995
0
  case Q_IP:
7996
    /*
7997
     * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7998
     * as an indication that we don't know the netmask, and fail
7999
     * in that case.
8000
     */
8001
0
    if (cstate->netmask == PCAP_NETMASK_UNKNOWN)
8002
0
      bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported");
8003
0
    b0 = gen_linktype(cstate, ETHERTYPE_IP);
8004
0
    hostmask = ~cstate->netmask;
8005
0
    b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, 0, hostmask);
8006
0
    b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W,
8007
0
            ~0 & hostmask, hostmask);
8008
0
    gen_or(b1, b2);
8009
0
    gen_and(b0, b2);
8010
0
    return b2;
8011
0
  }
8012
0
  bpf_error(cstate, "only link-layer/IP broadcast filters supported");
8013
  /*NOTREACHED*/
8014
0
}
8015
8016
/*
8017
 * Generate code to test the low-order bit of a MAC address (that's
8018
 * the bottom bit of the *first* byte).
8019
 */
8020
static struct block *
8021
gen_mac_multicast(compiler_state_t *cstate, int offset)
8022
0
{
8023
0
  register struct block *b0;
8024
0
  register struct slist *s;
8025
8026
  /* link[offset] & 1 != 0 */
8027
0
  s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B);
8028
0
  b0 = new_block(cstate, JMP(BPF_JSET));
8029
0
  b0->s.k = 1;
8030
0
  b0->stmts = s;
8031
0
  return b0;
8032
0
}
8033
8034
struct block *
8035
gen_multicast(compiler_state_t *cstate, int proto)
8036
0
{
8037
0
  register struct block *b0, *b1, *b2;
8038
0
  register struct slist *s;
8039
8040
  /*
8041
   * Catch errors reported by us and routines below us, and return NULL
8042
   * on an error.
8043
   */
8044
0
  if (setjmp(cstate->top_ctx))
8045
0
    return (NULL);
8046
8047
0
  switch (proto) {
8048
8049
0
  case Q_DEFAULT:
8050
0
  case Q_LINK:
8051
0
    switch (cstate->linktype) {
8052
0
    case DLT_ARCNET:
8053
0
    case DLT_ARCNET_LINUX:
8054
      /* all ARCnet multicasts use the same address */
8055
0
      return gen_ahostop(cstate, abroadcast, Q_DST);
8056
0
    case DLT_EN10MB:
8057
0
    case DLT_NETANALYZER:
8058
0
    case DLT_NETANALYZER_TRANSPARENT:
8059
0
      b1 = gen_prevlinkhdr_check(cstate);
8060
      /* ether[0] & 1 != 0 */
8061
0
      b0 = gen_mac_multicast(cstate, 0);
8062
0
      if (b1 != NULL)
8063
0
        gen_and(b1, b0);
8064
0
      return b0;
8065
0
    case DLT_FDDI:
8066
      /*
8067
       * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8068
       *
8069
       * XXX - was that referring to bit-order issues?
8070
       */
8071
      /* fddi[1] & 1 != 0 */
8072
0
      return gen_mac_multicast(cstate, 1);
8073
0
    case DLT_IEEE802:
8074
      /* tr[2] & 1 != 0 */
8075
0
      return gen_mac_multicast(cstate, 2);
8076
0
    case DLT_IEEE802_11:
8077
0
    case DLT_PRISM_HEADER:
8078
0
    case DLT_IEEE802_11_RADIO_AVS:
8079
0
    case DLT_IEEE802_11_RADIO:
8080
0
    case DLT_PPI:
8081
      /*
8082
       * Oh, yuk.
8083
       *
8084
       *  For control frames, there is no DA.
8085
       *
8086
       *  For management frames, DA is at an
8087
       *  offset of 4 from the beginning of
8088
       *  the packet.
8089
       *
8090
       *  For data frames, DA is at an offset
8091
       *  of 4 from the beginning of the packet
8092
       *  if To DS is clear and at an offset of
8093
       *  16 from the beginning of the packet
8094
       *  if To DS is set.
8095
       */
8096
8097
      /*
8098
       * Generate the tests to be done for data frames.
8099
       *
8100
       * First, check for To DS set, i.e. "link[1] & 0x01".
8101
       */
8102
0
      s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
8103
0
      b1 = new_block(cstate, JMP(BPF_JSET));
8104
0
      b1->s.k = 0x01; /* To DS */
8105
0
      b1->stmts = s;
8106
8107
      /*
8108
       * If To DS is set, the DA is at 16.
8109
       */
8110
0
      b0 = gen_mac_multicast(cstate, 16);
8111
0
      gen_and(b1, b0);
8112
8113
      /*
8114
       * Now, check for To DS not set, i.e. check
8115
       * "!(link[1] & 0x01)".
8116
       */
8117
0
      s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
8118
0
      b2 = new_block(cstate, JMP(BPF_JSET));
8119
0
      b2->s.k = 0x01; /* To DS */
8120
0
      b2->stmts = s;
8121
0
      gen_not(b2);
8122
8123
      /*
8124
       * If To DS is not set, the DA is at 4.
8125
       */
8126
0
      b1 = gen_mac_multicast(cstate, 4);
8127
0
      gen_and(b2, b1);
8128
8129
      /*
8130
       * Now OR together the last two checks.  That gives
8131
       * the complete set of checks for data frames.
8132
       */
8133
0
      gen_or(b1, b0);
8134
8135
      /*
8136
       * Now check for a data frame.
8137
       * I.e, check "link[0] & 0x08".
8138
       */
8139
0
      s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8140
0
      b1 = new_block(cstate, JMP(BPF_JSET));
8141
0
      b1->s.k = 0x08;
8142
0
      b1->stmts = s;
8143
8144
      /*
8145
       * AND that with the checks done for data frames.
8146
       */
8147
0
      gen_and(b1, b0);
8148
8149
      /*
8150
       * If the high-order bit of the type value is 0, this
8151
       * is a management frame.
8152
       * I.e, check "!(link[0] & 0x08)".
8153
       */
8154
0
      s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8155
0
      b2 = new_block(cstate, JMP(BPF_JSET));
8156
0
      b2->s.k = 0x08;
8157
0
      b2->stmts = s;
8158
0
      gen_not(b2);
8159
8160
      /*
8161
       * For management frames, the DA is at 4.
8162
       */
8163
0
      b1 = gen_mac_multicast(cstate, 4);
8164
0
      gen_and(b2, b1);
8165
8166
      /*
8167
       * OR that with the checks done for data frames.
8168
       * That gives the checks done for management and
8169
       * data frames.
8170
       */
8171
0
      gen_or(b1, b0);
8172
8173
      /*
8174
       * If the low-order bit of the type value is 1,
8175
       * this is either a control frame or a frame
8176
       * with a reserved type, and thus not a
8177
       * frame with an SA.
8178
       *
8179
       * I.e., check "!(link[0] & 0x04)".
8180
       */
8181
0
      s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8182
0
      b1 = new_block(cstate, JMP(BPF_JSET));
8183
0
      b1->s.k = 0x04;
8184
0
      b1->stmts = s;
8185
0
      gen_not(b1);
8186
8187
      /*
8188
       * AND that with the checks for data and management
8189
       * frames.
8190
       */
8191
0
      gen_and(b1, b0);
8192
0
      return b0;
8193
0
    case DLT_IP_OVER_FC:
8194
0
      b0 = gen_mac_multicast(cstate, 2);
8195
0
      return b0;
8196
0
    default:
8197
0
      break;
8198
0
    }
8199
    /* Link not known to support multicasts */
8200
0
    break;
8201
8202
0
  case Q_IP:
8203
0
    b0 = gen_linktype(cstate, ETHERTYPE_IP);
8204
0
    b1 = gen_cmp_ge(cstate, OR_LINKPL, 16, BPF_B, 224);
8205
0
    gen_and(b0, b1);
8206
0
    return b1;
8207
8208
0
  case Q_IPV6:
8209
0
    b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
8210
0
    b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, 255);
8211
0
    gen_and(b0, b1);
8212
0
    return b1;
8213
0
  }
8214
0
  bpf_error(cstate, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8215
  /*NOTREACHED*/
8216
0
}
8217
8218
struct block *
8219
gen_ifindex(compiler_state_t *cstate, int ifindex)
8220
0
{
8221
0
  register struct block *b0;
8222
8223
  /*
8224
   * Catch errors reported by us and routines below us, and return NULL
8225
   * on an error.
8226
   */
8227
0
  if (setjmp(cstate->top_ctx))
8228
0
    return (NULL);
8229
8230
  /*
8231
   * Only some data link types support ifindex qualifiers.
8232
   */
8233
0
  switch (cstate->linktype) {
8234
0
  case DLT_LINUX_SLL2:
8235
    /* match packets on this interface */
8236
0
    b0 = gen_cmp(cstate, OR_LINKHDR, 4, BPF_W, ifindex);
8237
0
    break;
8238
0
        default:
8239
0
#if defined(linux)
8240
    /*
8241
     * This is Linux; we require PF_PACKET support.
8242
     * If this is a *live* capture, we can look at
8243
     * special meta-data in the filter expression;
8244
     * if it's a savefile, we can't.
8245
     */
8246
0
    if (cstate->bpf_pcap->rfile != NULL) {
8247
      /* We have a FILE *, so this is a savefile */
8248
0
      bpf_error(cstate, "ifindex not supported on %s when reading savefiles",
8249
0
          pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8250
0
      b0 = NULL;
8251
      /*NOTREACHED*/
8252
0
    }
8253
    /* match ifindex */
8254
0
    b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_IFINDEX, BPF_W,
8255
0
                 ifindex);
8256
#else /* defined(linux) */
8257
    bpf_error(cstate, "ifindex not supported on %s",
8258
        pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8259
    /*NOTREACHED*/
8260
#endif /* defined(linux) */
8261
0
  }
8262
0
  return (b0);
8263
0
}
8264
8265
/*
8266
 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8267
 * Outbound traffic is sent by this machine, while inbound traffic is
8268
 * sent by a remote machine (and may include packets destined for a
8269
 * unicast or multicast link-layer address we are not subscribing to).
8270
 * These are the same definitions implemented by pcap_setdirection().
8271
 * Capturing only unicast traffic destined for this host is probably
8272
 * better accomplished using a higher-layer filter.
8273
 */
8274
struct block *
8275
gen_inbound(compiler_state_t *cstate, int dir)
8276
0
{
8277
0
  register struct block *b0;
8278
8279
  /*
8280
   * Catch errors reported by us and routines below us, and return NULL
8281
   * on an error.
8282
   */
8283
0
  if (setjmp(cstate->top_ctx))
8284
0
    return (NULL);
8285
8286
  /*
8287
   * Only some data link types support inbound/outbound qualifiers.
8288
   */
8289
0
  switch (cstate->linktype) {
8290
0
  case DLT_SLIP:
8291
0
    b0 = gen_relation_internal(cstate, BPF_JEQ,
8292
0
        gen_load_internal(cstate, Q_LINK, gen_loadi_internal(cstate, 0), 1),
8293
0
        gen_loadi_internal(cstate, 0),
8294
0
        dir);
8295
0
    break;
8296
8297
0
  case DLT_IPNET:
8298
0
    if (dir) {
8299
      /* match outgoing packets */
8300
0
      b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_OUTBOUND);
8301
0
    } else {
8302
      /* match incoming packets */
8303
0
      b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_INBOUND);
8304
0
    }
8305
0
    break;
8306
8307
0
  case DLT_LINUX_SLL:
8308
    /* match outgoing packets */
8309
0
    b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING);
8310
0
    if (!dir) {
8311
      /* to filter on inbound traffic, invert the match */
8312
0
      gen_not(b0);
8313
0
    }
8314
0
    break;
8315
8316
0
  case DLT_LINUX_SLL2:
8317
    /* match outgoing packets */
8318
0
    b0 = gen_cmp(cstate, OR_LINKHDR, 10, BPF_B, LINUX_SLL_OUTGOING);
8319
0
    if (!dir) {
8320
      /* to filter on inbound traffic, invert the match */
8321
0
      gen_not(b0);
8322
0
    }
8323
0
    break;
8324
8325
0
  case DLT_PFLOG:
8326
0
    b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B,
8327
0
        ((dir == 0) ? PF_IN : PF_OUT));
8328
0
    break;
8329
8330
0
  case DLT_PPP_PPPD:
8331
0
    if (dir) {
8332
      /* match outgoing packets */
8333
0
      b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_OUT);
8334
0
    } else {
8335
      /* match incoming packets */
8336
0
      b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_IN);
8337
0
    }
8338
0
    break;
8339
8340
0
        case DLT_JUNIPER_MFR:
8341
0
        case DLT_JUNIPER_MLFR:
8342
0
        case DLT_JUNIPER_MLPPP:
8343
0
  case DLT_JUNIPER_ATM1:
8344
0
  case DLT_JUNIPER_ATM2:
8345
0
  case DLT_JUNIPER_PPPOE:
8346
0
  case DLT_JUNIPER_PPPOE_ATM:
8347
0
        case DLT_JUNIPER_GGSN:
8348
0
        case DLT_JUNIPER_ES:
8349
0
        case DLT_JUNIPER_MONITOR:
8350
0
        case DLT_JUNIPER_SERVICES:
8351
0
        case DLT_JUNIPER_ETHER:
8352
0
        case DLT_JUNIPER_PPP:
8353
0
        case DLT_JUNIPER_FRELAY:
8354
0
        case DLT_JUNIPER_CHDLC:
8355
0
        case DLT_JUNIPER_VP:
8356
0
        case DLT_JUNIPER_ST:
8357
0
        case DLT_JUNIPER_ISM:
8358
0
        case DLT_JUNIPER_VS:
8359
0
        case DLT_JUNIPER_SRX_E2E:
8360
0
        case DLT_JUNIPER_FIBRECHANNEL:
8361
0
  case DLT_JUNIPER_ATM_CEMIC:
8362
8363
    /* juniper flags (including direction) are stored
8364
     * the byte after the 3-byte magic number */
8365
0
    if (dir) {
8366
      /* match outgoing packets */
8367
0
      b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 0, 0x01);
8368
0
    } else {
8369
      /* match incoming packets */
8370
0
      b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 1, 0x01);
8371
0
    }
8372
0
    break;
8373
8374
0
  default:
8375
    /*
8376
     * If we have packet meta-data indicating a direction,
8377
     * and that metadata can be checked by BPF code, check
8378
     * it.  Otherwise, give up, as this link-layer type has
8379
     * nothing in the packet data.
8380
     *
8381
     * Currently, the only platform where a BPF filter can
8382
     * check that metadata is Linux with the in-kernel
8383
     * BPF interpreter.  If other packet capture mechanisms
8384
     * and BPF filters also supported this, it would be
8385
     * nice.  It would be even better if they made that
8386
     * metadata available so that we could provide it
8387
     * with newer capture APIs, allowing it to be saved
8388
     * in pcapng files.
8389
     */
8390
0
#if defined(linux)
8391
    /*
8392
     * This is Linux; we require PF_PACKET support.
8393
     * If this is a *live* capture, we can look at
8394
     * special meta-data in the filter expression;
8395
     * if it's a savefile, we can't.
8396
     */
8397
0
    if (cstate->bpf_pcap->rfile != NULL) {
8398
      /* We have a FILE *, so this is a savefile */
8399
0
      bpf_error(cstate, "inbound/outbound not supported on %s when reading savefiles",
8400
0
          pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8401
      /*NOTREACHED*/
8402
0
    }
8403
    /* match outgoing packets */
8404
0
    b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
8405
0
                 PACKET_OUTGOING);
8406
0
    if (!dir) {
8407
      /* to filter on inbound traffic, invert the match */
8408
0
      gen_not(b0);
8409
0
    }
8410
#else /* defined(linux) */
8411
    bpf_error(cstate, "inbound/outbound not supported on %s",
8412
        pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8413
    /*NOTREACHED*/
8414
#endif /* defined(linux) */
8415
0
  }
8416
0
  return (b0);
8417
0
}
8418
8419
/* PF firewall log matched interface */
8420
struct block *
8421
gen_pf_ifname(compiler_state_t *cstate, const char *ifname)
8422
85
{
8423
85
  struct block *b0;
8424
85
  u_int len, off;
8425
8426
  /*
8427
   * Catch errors reported by us and routines below us, and return NULL
8428
   * on an error.
8429
   */
8430
85
  if (setjmp(cstate->top_ctx))
8431
9
    return (NULL);
8432
8433
76
  if (cstate->linktype != DLT_PFLOG) {
8434
8
    bpf_error(cstate, "ifname supported only on PF linktype");
8435
    /*NOTREACHED*/
8436
8
  }
8437
68
  len = sizeof(((struct pfloghdr *)0)->ifname);
8438
68
  off = offsetof(struct pfloghdr, ifname);
8439
68
  if (strlen(ifname) >= len) {
8440
1
    bpf_error(cstate, "ifname interface names can only be %d characters",
8441
1
        len-1);
8442
    /*NOTREACHED*/
8443
1
  }
8444
67
  b0 = gen_bcmp(cstate, OR_LINKHDR, off, (u_int)strlen(ifname),
8445
67
      (const u_char *)ifname);
8446
67
  return (b0);
8447
68
}
8448
8449
/* PF firewall log ruleset name */
8450
struct block *
8451
gen_pf_ruleset(compiler_state_t *cstate, char *ruleset)
8452
23
{
8453
23
  struct block *b0;
8454
8455
  /*
8456
   * Catch errors reported by us and routines below us, and return NULL
8457
   * on an error.
8458
   */
8459
23
  if (setjmp(cstate->top_ctx))
8460
10
    return (NULL);
8461
8462
13
  if (cstate->linktype != DLT_PFLOG) {
8463
9
    bpf_error(cstate, "ruleset supported only on PF linktype");
8464
    /*NOTREACHED*/
8465
9
  }
8466
8467
4
  if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
8468
1
    bpf_error(cstate, "ruleset names can only be %ld characters",
8469
1
        (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
8470
    /*NOTREACHED*/
8471
1
  }
8472
8473
3
  b0 = gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset),
8474
3
      (u_int)strlen(ruleset), (const u_char *)ruleset);
8475
3
  return (b0);
8476
4
}
8477
8478
/* PF firewall log rule number */
8479
struct block *
8480
gen_pf_rnr(compiler_state_t *cstate, int rnr)
8481
21
{
8482
21
  struct block *b0;
8483
8484
  /*
8485
   * Catch errors reported by us and routines below us, and return NULL
8486
   * on an error.
8487
   */
8488
21
  if (setjmp(cstate->top_ctx))
8489
9
    return (NULL);
8490
8491
12
  if (cstate->linktype != DLT_PFLOG) {
8492
9
    bpf_error(cstate, "rnr supported only on PF linktype");
8493
    /*NOTREACHED*/
8494
9
  }
8495
8496
3
  b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W,
8497
3
     (bpf_u_int32)rnr);
8498
3
  return (b0);
8499
12
}
8500
8501
/* PF firewall log sub-rule number */
8502
struct block *
8503
gen_pf_srnr(compiler_state_t *cstate, int srnr)
8504
11
{
8505
11
  struct block *b0;
8506
8507
  /*
8508
   * Catch errors reported by us and routines below us, and return NULL
8509
   * on an error.
8510
   */
8511
11
  if (setjmp(cstate->top_ctx))
8512
10
    return (NULL);
8513
8514
10
  if (cstate->linktype != DLT_PFLOG) {
8515
10
    bpf_error(cstate, "srnr supported only on PF linktype");
8516
    /*NOTREACHED*/
8517
10
  }
8518
8519
18.4E
  b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W,
8520
18.4E
      (bpf_u_int32)srnr);
8521
18.4E
  return (b0);
8522
1
}
8523
8524
/* PF firewall log reason code */
8525
struct block *
8526
gen_pf_reason(compiler_state_t *cstate, int reason)
8527
0
{
8528
0
  struct block *b0;
8529
8530
  /*
8531
   * Catch errors reported by us and routines below us, and return NULL
8532
   * on an error.
8533
   */
8534
0
  if (setjmp(cstate->top_ctx))
8535
0
    return (NULL);
8536
8537
0
  if (cstate->linktype != DLT_PFLOG) {
8538
0
    bpf_error(cstate, "reason supported only on PF linktype");
8539
    /*NOTREACHED*/
8540
0
  }
8541
8542
0
  b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B,
8543
0
      (bpf_u_int32)reason);
8544
0
  return (b0);
8545
0
}
8546
8547
/* PF firewall log action */
8548
struct block *
8549
gen_pf_action(compiler_state_t *cstate, int action)
8550
9
{
8551
9
  struct block *b0;
8552
8553
  /*
8554
   * Catch errors reported by us and routines below us, and return NULL
8555
   * on an error.
8556
   */
8557
9
  if (setjmp(cstate->top_ctx))
8558
8
    return (NULL);
8559
8560
8
  if (cstate->linktype != DLT_PFLOG) {
8561
8
    bpf_error(cstate, "action supported only on PF linktype");
8562
    /*NOTREACHED*/
8563
8
  }
8564
8565
18.4E
  b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B,
8566
18.4E
      (bpf_u_int32)action);
8567
18.4E
  return (b0);
8568
1
}
8569
8570
/* IEEE 802.11 wireless header */
8571
struct block *
8572
gen_p80211_type(compiler_state_t *cstate, bpf_u_int32 type, bpf_u_int32 mask)
8573
206
{
8574
206
  struct block *b0;
8575
8576
  /*
8577
   * Catch errors reported by us and routines below us, and return NULL
8578
   * on an error.
8579
   */
8580
206
  if (setjmp(cstate->top_ctx))
8581
5
    return (NULL);
8582
8583
201
  switch (cstate->linktype) {
8584
8585
67
  case DLT_IEEE802_11:
8586
103
  case DLT_PRISM_HEADER:
8587
151
  case DLT_IEEE802_11_RADIO_AVS:
8588
201
  case DLT_IEEE802_11_RADIO:
8589
201
    b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, type, mask);
8590
201
    break;
8591
8592
5
  default:
8593
5
    bpf_error(cstate, "802.11 link-layer types supported only on 802.11");
8594
    /*NOTREACHED*/
8595
201
  }
8596
8597
201
  return (b0);
8598
201
}
8599
8600
struct block *
8601
gen_p80211_fcdir(compiler_state_t *cstate, bpf_u_int32 fcdir)
8602
178
{
8603
178
  struct block *b0;
8604
8605
  /*
8606
   * Catch errors reported by us and routines below us, and return NULL
8607
   * on an error.
8608
   */
8609
178
  if (setjmp(cstate->top_ctx))
8610
3
    return (NULL);
8611
8612
175
  switch (cstate->linktype) {
8613
8614
47
  case DLT_IEEE802_11:
8615
81
  case DLT_PRISM_HEADER:
8616
136
  case DLT_IEEE802_11_RADIO_AVS:
8617
175
  case DLT_IEEE802_11_RADIO:
8618
175
    break;
8619
8620
3
  default:
8621
3
    bpf_error(cstate, "frame direction supported only with 802.11 headers");
8622
    /*NOTREACHED*/
8623
175
  }
8624
8625
175
  b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, fcdir,
8626
175
      IEEE80211_FC1_DIR_MASK);
8627
8628
175
  return (b0);
8629
175
}
8630
8631
struct block *
8632
gen_acode(compiler_state_t *cstate, const char *s, struct qual q)
8633
222
{
8634
222
  struct block *b;
8635
8636
  /*
8637
   * Catch errors reported by us and routines below us, and return NULL
8638
   * on an error.
8639
   */
8640
222
  if (setjmp(cstate->top_ctx))
8641
25
    return (NULL);
8642
8643
197
  switch (cstate->linktype) {
8644
8645
139
  case DLT_ARCNET:
8646
220
  case DLT_ARCNET_LINUX:
8647
220
    if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
8648
220
        q.proto == Q_LINK) {
8649
215
      cstate->e = pcap_ether_aton(s);
8650
215
      if (cstate->e == NULL)
8651
0
        bpf_error(cstate, "malloc");
8652
215
      b = gen_ahostop(cstate, cstate->e, (int)q.dir);
8653
215
      free(cstate->e);
8654
215
      cstate->e = NULL;
8655
215
      return (b);
8656
215
    } else
8657
5
      bpf_error(cstate, "ARCnet address used in non-arc expression");
8658
    /*NOTREACHED*/
8659
8660
2
  default:
8661
2
    bpf_error(cstate, "aid supported only on ARCnet");
8662
    /*NOTREACHED*/
8663
197
  }
8664
197
}
8665
8666
static struct block *
8667
gen_ahostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
8668
449
{
8669
449
  register struct block *b0, *b1;
8670
8671
449
  switch (dir) {
8672
  /* src comes first, different from Ethernet */
8673
161
  case Q_SRC:
8674
161
    return gen_bcmp(cstate, OR_LINKHDR, 0, 1, eaddr);
8675
8676
153
  case Q_DST:
8677
153
    return gen_bcmp(cstate, OR_LINKHDR, 1, 1, eaddr);
8678
8679
38
  case Q_AND:
8680
38
    b0 = gen_ahostop(cstate, eaddr, Q_SRC);
8681
38
    b1 = gen_ahostop(cstate, eaddr, Q_DST);
8682
38
    gen_and(b0, b1);
8683
38
    return b1;
8684
8685
41
  case Q_DEFAULT:
8686
79
  case Q_OR:
8687
79
    b0 = gen_ahostop(cstate, eaddr, Q_SRC);
8688
79
    b1 = gen_ahostop(cstate, eaddr, Q_DST);
8689
79
    gen_or(b0, b1);
8690
79
    return b1;
8691
8692
1
  case Q_ADDR1:
8693
1
    bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
8694
    /*NOTREACHED*/
8695
8696
1
  case Q_ADDR2:
8697
1
    bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
8698
    /*NOTREACHED*/
8699
8700
1
  case Q_ADDR3:
8701
1
    bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
8702
    /*NOTREACHED*/
8703
8704
1
  case Q_ADDR4:
8705
1
    bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
8706
    /*NOTREACHED*/
8707
8708
2
  case Q_RA:
8709
2
    bpf_error(cstate, "'ra' is only supported on 802.11");
8710
    /*NOTREACHED*/
8711
8712
12
  case Q_TA:
8713
12
    bpf_error(cstate, "'ta' is only supported on 802.11");
8714
    /*NOTREACHED*/
8715
449
  }
8716
0
  abort();
8717
  /*NOTREACHED*/
8718
449
}
8719
8720
static struct block *
8721
gen_vlan_tpid_test(compiler_state_t *cstate)
8722
597
{
8723
597
  struct block *b0, *b1;
8724
8725
  /* check for VLAN, including 802.1ad and QinQ */
8726
597
  b0 = gen_linktype(cstate, ETHERTYPE_8021Q);
8727
597
  b1 = gen_linktype(cstate, ETHERTYPE_8021AD);
8728
597
  gen_or(b0,b1);
8729
597
  b0 = b1;
8730
597
  b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ);
8731
597
  gen_or(b0,b1);
8732
8733
597
  return b1;
8734
597
}
8735
8736
static struct block *
8737
gen_vlan_vid_test(compiler_state_t *cstate, bpf_u_int32 vlan_num)
8738
144
{
8739
144
  if (vlan_num > 0x0fff) {
8740
27
    bpf_error(cstate, "VLAN tag %u greater than maximum %u",
8741
27
        vlan_num, 0x0fff);
8742
27
  }
8743
117
  return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, vlan_num, 0x0fff);
8744
144
}
8745
8746
static struct block *
8747
gen_vlan_no_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,
8748
    int has_vlan_tag)
8749
597
{
8750
597
  struct block *b0, *b1;
8751
8752
597
  b0 = gen_vlan_tpid_test(cstate);
8753
8754
597
  if (has_vlan_tag) {
8755
144
    b1 = gen_vlan_vid_test(cstate, vlan_num);
8756
144
    gen_and(b0, b1);
8757
144
    b0 = b1;
8758
144
  }
8759
8760
  /*
8761
   * Both payload and link header type follow the VLAN tags so that
8762
   * both need to be updated.
8763
   */
8764
597
  cstate->off_linkpl.constant_part += 4;
8765
597
  cstate->off_linktype.constant_part += 4;
8766
8767
597
  return b0;
8768
597
}
8769
8770
#if defined(SKF_AD_VLAN_TAG_PRESENT)
8771
/* add v to variable part of off */
8772
static void
8773
gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off,
8774
    bpf_u_int32 v, struct slist *s)
8775
0
{
8776
0
  struct slist *s2;
8777
8778
0
  if (!off->is_variable)
8779
0
    off->is_variable = 1;
8780
0
  if (off->reg == -1)
8781
0
    off->reg = alloc_reg(cstate);
8782
8783
0
  s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
8784
0
  s2->s.k = off->reg;
8785
0
  sappend(s, s2);
8786
0
  s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
8787
0
  s2->s.k = v;
8788
0
  sappend(s, s2);
8789
0
  s2 = new_stmt(cstate, BPF_ST);
8790
0
  s2->s.k = off->reg;
8791
0
  sappend(s, s2);
8792
0
}
8793
8794
/*
8795
 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8796
 * and link type offsets first
8797
 */
8798
static void
8799
gen_vlan_patch_tpid_test(compiler_state_t *cstate, struct block *b_tpid)
8800
0
{
8801
0
  struct slist s;
8802
8803
  /* offset determined at run time, shift variable part */
8804
0
  s.next = NULL;
8805
0
  cstate->is_vlan_vloffset = 1;
8806
0
  gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s);
8807
0
  gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s);
8808
8809
  /* we get a pointer to a chain of or-ed blocks, patch first of them */
8810
0
  sappend(s.next, b_tpid->head->stmts);
8811
0
  b_tpid->head->stmts = s.next;
8812
0
}
8813
8814
/*
8815
 * patch block b_vid (VLAN id test) to load VID value either from packet
8816
 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8817
 */
8818
static void
8819
gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid)
8820
0
{
8821
0
  struct slist *s, *s2, *sjeq;
8822
0
  unsigned cnt;
8823
8824
0
  s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8825
0
  s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
8826
8827
  /* true -> next instructions, false -> beginning of b_vid */
8828
0
  sjeq = new_stmt(cstate, JMP(BPF_JEQ));
8829
0
  sjeq->s.k = 1;
8830
0
  sjeq->s.jf = b_vid->stmts;
8831
0
  sappend(s, sjeq);
8832
8833
0
  s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8834
0
  s2->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG;
8835
0
  sappend(s, s2);
8836
0
  sjeq->s.jt = s2;
8837
8838
  /* Jump to the test in b_vid. We need to jump one instruction before
8839
   * the end of the b_vid block so that we only skip loading the TCI
8840
   * from packet data and not the 'and' instruction extractging VID.
8841
   */
8842
0
  cnt = 0;
8843
0
  for (s2 = b_vid->stmts; s2; s2 = s2->next)
8844
0
    cnt++;
8845
0
  s2 = new_stmt(cstate, JMP(BPF_JA));
8846
0
  s2->s.k = cnt - 1;
8847
0
  sappend(s, s2);
8848
8849
  /* insert our statements at the beginning of b_vid */
8850
0
  sappend(s, b_vid->stmts);
8851
0
  b_vid->stmts = s;
8852
0
}
8853
8854
/*
8855
 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8856
 * extensions.  Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8857
 * tag can be either in metadata or in packet data; therefore if the
8858
 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8859
 * header for VLAN tag. As the decision is done at run time, we need
8860
 * update variable part of the offsets
8861
 */
8862
static struct block *
8863
gen_vlan_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,
8864
    int has_vlan_tag)
8865
0
{
8866
0
        struct block *b0, *b_tpid, *b_vid = NULL;
8867
0
        struct slist *s;
8868
8869
        /* generate new filter code based on extracting packet
8870
         * metadata */
8871
0
        s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8872
0
        s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
8873
8874
0
        b0 = new_block(cstate, JMP(BPF_JEQ));
8875
0
        b0->stmts = s;
8876
0
        b0->s.k = 1;
8877
8878
  /*
8879
   * This is tricky. We need to insert the statements updating variable
8880
   * parts of offsets before the traditional TPID and VID tests so
8881
   * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8882
   * we do not want this update to affect those checks. That's why we
8883
   * generate both test blocks first and insert the statements updating
8884
   * variable parts of both offsets after that. This wouldn't work if
8885
   * there already were variable length link header when entering this
8886
   * function but gen_vlan_bpf_extensions() isn't called in that case.
8887
   */
8888
0
  b_tpid = gen_vlan_tpid_test(cstate);
8889
0
  if (has_vlan_tag)
8890
0
    b_vid = gen_vlan_vid_test(cstate, vlan_num);
8891
8892
0
  gen_vlan_patch_tpid_test(cstate, b_tpid);
8893
0
  gen_or(b0, b_tpid);
8894
0
  b0 = b_tpid;
8895
8896
0
  if (has_vlan_tag) {
8897
0
    gen_vlan_patch_vid_test(cstate, b_vid);
8898
0
    gen_and(b0, b_vid);
8899
0
    b0 = b_vid;
8900
0
  }
8901
8902
0
        return b0;
8903
0
}
8904
#endif
8905
8906
/*
8907
 * support IEEE 802.1Q VLAN trunk over ethernet
8908
 */
8909
struct block *
8910
gen_vlan(compiler_state_t *cstate, bpf_u_int32 vlan_num, int has_vlan_tag)
8911
603
{
8912
603
  struct  block *b0;
8913
8914
  /*
8915
   * Catch errors reported by us and routines below us, and return NULL
8916
   * on an error.
8917
   */
8918
603
  if (setjmp(cstate->top_ctx))
8919
33
    return (NULL);
8920
8921
  /* can't check for VLAN-encapsulated packets inside MPLS */
8922
570
  if (cstate->label_stack_depth > 0)
8923
3
    bpf_error(cstate, "no VLAN match after MPLS");
8924
8925
  /*
8926
   * Check for a VLAN packet, and then change the offsets to point
8927
   * to the type and data fields within the VLAN packet.  Just
8928
   * increment the offsets, so that we can support a hierarchy, e.g.
8929
   * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8930
   * VLAN 100.
8931
   *
8932
   * XXX - this is a bit of a kludge.  If we were to split the
8933
   * compiler into a parser that parses an expression and
8934
   * generates an expression tree, and a code generator that
8935
   * takes an expression tree (which could come from our
8936
   * parser or from some other parser) and generates BPF code,
8937
   * we could perhaps make the offsets parameters of routines
8938
   * and, in the handler for an "AND" node, pass to subnodes
8939
   * other than the VLAN node the adjusted offsets.
8940
   *
8941
   * This would mean that "vlan" would, instead of changing the
8942
   * behavior of *all* tests after it, change only the behavior
8943
   * of tests ANDed with it.  That would change the documented
8944
   * semantics of "vlan", which might break some expressions.
8945
   * However, it would mean that "(vlan and ip) or ip" would check
8946
   * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8947
   * checking only for VLAN-encapsulated IP, so that could still
8948
   * be considered worth doing; it wouldn't break expressions
8949
   * that are of the form "vlan and ..." or "vlan N and ...",
8950
   * which I suspect are the most common expressions involving
8951
   * "vlan".  "vlan or ..." doesn't necessarily do what the user
8952
   * would really want, now, as all the "or ..." tests would
8953
   * be done assuming a VLAN, even though the "or" could be viewed
8954
   * as meaning "or, if this isn't a VLAN packet...".
8955
   */
8956
567
  switch (cstate->linktype) {
8957
8958
84
  case DLT_EN10MB:
8959
167
  case DLT_NETANALYZER:
8960
255
  case DLT_NETANALYZER_TRANSPARENT:
8961
255
#if defined(SKF_AD_VLAN_TAG_PRESENT)
8962
    /* Verify that this is the outer part of the packet and
8963
     * not encapsulated somehow. */
8964
255
    if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable &&
8965
255
        cstate->off_linkhdr.constant_part ==
8966
39
        cstate->off_outermostlinkhdr.constant_part) {
8967
      /*
8968
       * Do we need special VLAN handling?
8969
       */
8970
35
      if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING)
8971
0
        b0 = gen_vlan_bpf_extensions(cstate, vlan_num,
8972
0
            has_vlan_tag);
8973
35
      else
8974
35
        b0 = gen_vlan_no_bpf_extensions(cstate,
8975
35
            vlan_num, has_vlan_tag);
8976
35
    } else
8977
220
#endif
8978
220
      b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num,
8979
220
          has_vlan_tag);
8980
255
                break;
8981
8982
91
  case DLT_IEEE802_11:
8983
159
  case DLT_PRISM_HEADER:
8984
235
  case DLT_IEEE802_11_RADIO_AVS:
8985
342
  case DLT_IEEE802_11_RADIO:
8986
342
    b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, has_vlan_tag);
8987
342
    break;
8988
8989
3
  default:
8990
3
    bpf_error(cstate, "no VLAN support for %s",
8991
3
          pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8992
    /*NOTREACHED*/
8993
567
  }
8994
8995
570
        cstate->vlan_stack_depth++;
8996
8997
570
  return (b0);
8998
567
}
8999
9000
/*
9001
 * support for MPLS
9002
 *
9003
 * The label_num_arg dance is to avoid annoying whining by compilers that
9004
 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9005
 * It's not *used* after setjmp returns.
9006
 */
9007
struct block *
9008
gen_mpls(compiler_state_t *cstate, bpf_u_int32 label_num_arg,
9009
    int has_label_num)
9010
368
{
9011
368
  volatile bpf_u_int32 label_num = label_num_arg;
9012
368
  struct  block *b0, *b1;
9013
9014
  /*
9015
   * Catch errors reported by us and routines below us, and return NULL
9016
   * on an error.
9017
   */
9018
368
  if (setjmp(cstate->top_ctx))
9019
21
    return (NULL);
9020
9021
347
        if (cstate->label_stack_depth > 0) {
9022
            /* just match the bottom-of-stack bit clear */
9023
156
            b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01);
9024
191
        } else {
9025
            /*
9026
             * We're not in an MPLS stack yet, so check the link-layer
9027
             * type against MPLS.
9028
             */
9029
191
            switch (cstate->linktype) {
9030
9031
7
            case DLT_C_HDLC: /* fall through */
9032
27
            case DLT_HDLC:
9033
70
            case DLT_EN10MB:
9034
121
            case DLT_NETANALYZER:
9035
146
            case DLT_NETANALYZER_TRANSPARENT:
9036
146
                    b0 = gen_linktype(cstate, ETHERTYPE_MPLS);
9037
146
                    break;
9038
9039
63
            case DLT_PPP:
9040
63
                    b0 = gen_linktype(cstate, PPP_MPLS_UCAST);
9041
63
                    break;
9042
9043
                    /* FIXME add other DLT_s ...
9044
                     * for Frame-Relay/and ATM this may get messy due to SNAP headers
9045
                     * leave it for now */
9046
9047
3
            default:
9048
3
                    bpf_error(cstate, "no MPLS support for %s",
9049
3
                          pcap_datalink_val_to_description_or_dlt(cstate->linktype));
9050
                    /*NOTREACHED*/
9051
191
            }
9052
191
        }
9053
9054
  /* If a specific MPLS label is requested, check it */
9055
365
  if (has_label_num) {
9056
141
    if (label_num > 0xFFFFF) {
9057
18
      bpf_error(cstate, "MPLS label %u greater than maximum %u",
9058
18
          label_num, 0xFFFFF);
9059
18
    }
9060
123
    label_num = label_num << 12; /* label is shifted 12 bits on the wire */
9061
123
    b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, label_num,
9062
123
        0xfffff000); /* only compare the first 20 bits */
9063
123
    gen_and(b0, b1);
9064
123
    b0 = b1;
9065
123
  }
9066
9067
        /*
9068
         * Change the offsets to point to the type and data fields within
9069
         * the MPLS packet.  Just increment the offsets, so that we
9070
         * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9071
         * capture packets with an outer label of 100000 and an inner
9072
         * label of 1024.
9073
         *
9074
         * Increment the MPLS stack depth as well; this indicates that
9075
         * we're checking MPLS-encapsulated headers, to make sure higher
9076
         * level code generators don't try to match against IP-related
9077
         * protocols such as Q_ARP, Q_RARP etc.
9078
         *
9079
         * XXX - this is a bit of a kludge.  See comments in gen_vlan().
9080
         */
9081
347
        cstate->off_nl_nosnap += 4;
9082
347
        cstate->off_nl += 4;
9083
347
        cstate->label_stack_depth++;
9084
347
  return (b0);
9085
365
}
9086
9087
/*
9088
 * Support PPPOE discovery and session.
9089
 */
9090
struct block *
9091
gen_pppoed(compiler_state_t *cstate)
9092
45
{
9093
  /*
9094
   * Catch errors reported by us and routines below us, and return NULL
9095
   * on an error.
9096
   */
9097
45
  if (setjmp(cstate->top_ctx))
9098
2
    return (NULL);
9099
9100
  /* check for PPPoE discovery */
9101
43
  return gen_linktype(cstate, ETHERTYPE_PPPOED);
9102
45
}
9103
9104
struct block *
9105
gen_pppoes(compiler_state_t *cstate, bpf_u_int32 sess_num, int has_sess_num)
9106
403
{
9107
403
  struct block *b0, *b1;
9108
9109
  /*
9110
   * Catch errors reported by us and routines below us, and return NULL
9111
   * on an error.
9112
   */
9113
403
  if (setjmp(cstate->top_ctx))
9114
29
    return (NULL);
9115
9116
  /*
9117
   * Test against the PPPoE session link-layer type.
9118
   */
9119
374
  b0 = gen_linktype(cstate, ETHERTYPE_PPPOES);
9120
9121
  /* If a specific session is requested, check PPPoE session id */
9122
374
  if (has_sess_num) {
9123
75
    if (sess_num > 0x0000ffff) {
9124
25
      bpf_error(cstate, "PPPoE session number %u greater than maximum %u",
9125
25
          sess_num, 0x0000ffff);
9126
25
    }
9127
50
    b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, sess_num, 0x0000ffff);
9128
50
    gen_and(b0, b1);
9129
50
    b0 = b1;
9130
50
  }
9131
9132
  /*
9133
   * Change the offsets to point to the type and data fields within
9134
   * the PPP packet, and note that this is PPPoE rather than
9135
   * raw PPP.
9136
   *
9137
   * XXX - this is a bit of a kludge.  See the comments in
9138
   * gen_vlan().
9139
   *
9140
   * The "network-layer" protocol is PPPoE, which has a 6-byte
9141
   * PPPoE header, followed by a PPP packet.
9142
   *
9143
   * There is no HDLC encapsulation for the PPP packet (it's
9144
   * encapsulated in PPPoES instead), so the link-layer type
9145
   * starts at the first byte of the PPP packet.  For PPPoE,
9146
   * that offset is relative to the beginning of the total
9147
   * link-layer payload, including any 802.2 LLC header, so
9148
   * it's 6 bytes past cstate->off_nl.
9149
   */
9150
349
  PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable,
9151
349
      cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */
9152
349
      cstate->off_linkpl.reg);
9153
9154
349
  cstate->off_linktype = cstate->off_linkhdr;
9155
349
  cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2;
9156
9157
349
  cstate->off_nl = 0;
9158
349
  cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */
9159
9160
349
  return b0;
9161
374
}
9162
9163
/* Check that this is Geneve and the VNI is correct if
9164
 * specified. Parameterized to handle both IPv4 and IPv6. */
9165
static struct block *
9166
gen_geneve_check(compiler_state_t *cstate,
9167
    struct block *(*gen_portfn)(compiler_state_t *, u_int, int, int),
9168
    enum e_offrel offrel, bpf_u_int32 vni, int has_vni)
9169
0
{
9170
0
  struct block *b0, *b1;
9171
9172
0
  b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST);
9173
9174
  /* Check that we are operating on version 0. Otherwise, we
9175
   * can't decode the rest of the fields. The version is 2 bits
9176
   * in the first byte of the Geneve header. */
9177
0
  b1 = gen_mcmp(cstate, offrel, 8, BPF_B, 0, 0xc0);
9178
0
  gen_and(b0, b1);
9179
0
  b0 = b1;
9180
9181
0
  if (has_vni) {
9182
0
    if (vni > 0xffffff) {
9183
0
      bpf_error(cstate, "Geneve VNI %u greater than maximum %u",
9184
0
          vni, 0xffffff);
9185
0
    }
9186
0
    vni <<= 8; /* VNI is in the upper 3 bytes */
9187
0
    b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00);
9188
0
    gen_and(b0, b1);
9189
0
    b0 = b1;
9190
0
  }
9191
9192
0
  return b0;
9193
0
}
9194
9195
/* The IPv4 and IPv6 Geneve checks need to do two things:
9196
 * - Verify that this actually is Geneve with the right VNI.
9197
 * - Place the IP header length (plus variable link prefix if
9198
 *   needed) into register A to be used later to compute
9199
 *   the inner packet offsets. */
9200
static struct block *
9201
gen_geneve4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9202
0
{
9203
0
  struct block *b0, *b1;
9204
0
  struct slist *s, *s1;
9205
9206
0
  b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni);
9207
9208
  /* Load the IP header length into A. */
9209
0
  s = gen_loadx_iphdrlen(cstate);
9210
9211
0
  s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
9212
0
  sappend(s, s1);
9213
9214
  /* Forcibly append these statements to the true condition
9215
   * of the protocol check by creating a new block that is
9216
   * always true and ANDing them. */
9217
0
  b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9218
0
  b1->stmts = s;
9219
0
  b1->s.k = 0;
9220
9221
0
  gen_and(b0, b1);
9222
9223
0
  return b1;
9224
0
}
9225
9226
static struct block *
9227
gen_geneve6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9228
0
{
9229
0
  struct block *b0, *b1;
9230
0
  struct slist *s, *s1;
9231
9232
0
  b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni);
9233
9234
  /* Load the IP header length. We need to account for a
9235
   * variable length link prefix if there is one. */
9236
0
  s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
9237
0
  if (s) {
9238
0
    s1 = new_stmt(cstate, BPF_LD|BPF_IMM);
9239
0
    s1->s.k = 40;
9240
0
    sappend(s, s1);
9241
9242
0
    s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
9243
0
    s1->s.k = 0;
9244
0
    sappend(s, s1);
9245
0
  } else {
9246
0
    s = new_stmt(cstate, BPF_LD|BPF_IMM);
9247
0
    s->s.k = 40;
9248
0
  }
9249
9250
  /* Forcibly append these statements to the true condition
9251
   * of the protocol check by creating a new block that is
9252
   * always true and ANDing them. */
9253
0
  s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9254
0
  sappend(s, s1);
9255
9256
0
  b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9257
0
  b1->stmts = s;
9258
0
  b1->s.k = 0;
9259
9260
0
  gen_and(b0, b1);
9261
9262
0
  return b1;
9263
0
}
9264
9265
/* We need to store three values based on the Geneve header::
9266
 * - The offset of the linktype.
9267
 * - The offset of the end of the Geneve header.
9268
 * - The offset of the end of the encapsulated MAC header. */
9269
static struct slist *
9270
gen_geneve_offsets(compiler_state_t *cstate)
9271
0
{
9272
0
  struct slist *s, *s1, *s_proto;
9273
9274
  /* First we need to calculate the offset of the Geneve header
9275
   * itself. This is composed of the IP header previously calculated
9276
   * (include any variable link prefix) and stored in A plus the
9277
   * fixed sized headers (fixed link prefix, MAC length, and UDP
9278
   * header). */
9279
0
  s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9280
0
  s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8;
9281
9282
  /* Stash this in X since we'll need it later. */
9283
0
  s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9284
0
  sappend(s, s1);
9285
9286
  /* The EtherType in Geneve is 2 bytes in. Calculate this and
9287
   * store it. */
9288
0
  s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9289
0
  s1->s.k = 2;
9290
0
  sappend(s, s1);
9291
9292
0
  cstate->off_linktype.reg = alloc_reg(cstate);
9293
0
  cstate->off_linktype.is_variable = 1;
9294
0
  cstate->off_linktype.constant_part = 0;
9295
9296
0
  s1 = new_stmt(cstate, BPF_ST);
9297
0
  s1->s.k = cstate->off_linktype.reg;
9298
0
  sappend(s, s1);
9299
9300
  /* Load the Geneve option length and mask and shift to get the
9301
   * number of bytes. It is stored in the first byte of the Geneve
9302
   * header. */
9303
0
  s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
9304
0
  s1->s.k = 0;
9305
0
  sappend(s, s1);
9306
9307
0
  s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
9308
0
  s1->s.k = 0x3f;
9309
0
  sappend(s, s1);
9310
9311
0
  s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
9312
0
  s1->s.k = 4;
9313
0
  sappend(s, s1);
9314
9315
  /* Add in the rest of the Geneve base header. */
9316
0
  s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9317
0
  s1->s.k = 8;
9318
0
  sappend(s, s1);
9319
9320
  /* Add the Geneve header length to its offset and store. */
9321
0
  s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
9322
0
  s1->s.k = 0;
9323
0
  sappend(s, s1);
9324
9325
  /* Set the encapsulated type as Ethernet. Even though we may
9326
   * not actually have Ethernet inside there are two reasons this
9327
   * is useful:
9328
   * - The linktype field is always in EtherType format regardless
9329
   *   of whether it is in Geneve or an inner Ethernet frame.
9330
   * - The only link layer that we have specific support for is
9331
   *   Ethernet. We will confirm that the packet actually is
9332
   *   Ethernet at runtime before executing these checks. */
9333
0
  PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate));
9334
9335
0
  s1 = new_stmt(cstate, BPF_ST);
9336
0
  s1->s.k = cstate->off_linkhdr.reg;
9337
0
  sappend(s, s1);
9338
9339
  /* Calculate whether we have an Ethernet header or just raw IP/
9340
   * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9341
   * and linktype by 14 bytes so that the network header can be found
9342
   * seamlessly. Otherwise, keep what we've calculated already. */
9343
9344
  /* We have a bare jmp so we can't use the optimizer. */
9345
0
  cstate->no_optimize = 1;
9346
9347
  /* Load the EtherType in the Geneve header, 2 bytes in. */
9348
0
  s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H);
9349
0
  s1->s.k = 2;
9350
0
  sappend(s, s1);
9351
9352
  /* Load X with the end of the Geneve header. */
9353
0
  s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
9354
0
  s1->s.k = cstate->off_linkhdr.reg;
9355
0
  sappend(s, s1);
9356
9357
  /* Check if the EtherType is Transparent Ethernet Bridging. At the
9358
   * end of this check, we should have the total length in X. In
9359
   * the non-Ethernet case, it's already there. */
9360
0
  s_proto = new_stmt(cstate, JMP(BPF_JEQ));
9361
0
  s_proto->s.k = ETHERTYPE_TEB;
9362
0
  sappend(s, s_proto);
9363
9364
0
  s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
9365
0
  sappend(s, s1);
9366
0
  s_proto->s.jt = s1;
9367
9368
  /* Since this is Ethernet, use the EtherType of the payload
9369
   * directly as the linktype. Overwrite what we already have. */
9370
0
  s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9371
0
  s1->s.k = 12;
9372
0
  sappend(s, s1);
9373
9374
0
  s1 = new_stmt(cstate, BPF_ST);
9375
0
  s1->s.k = cstate->off_linktype.reg;
9376
0
  sappend(s, s1);
9377
9378
  /* Advance two bytes further to get the end of the Ethernet
9379
   * header. */
9380
0
  s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9381
0
  s1->s.k = 2;
9382
0
  sappend(s, s1);
9383
9384
  /* Move the result to X. */
9385
0
  s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9386
0
  sappend(s, s1);
9387
9388
  /* Store the final result of our linkpl calculation. */
9389
0
  cstate->off_linkpl.reg = alloc_reg(cstate);
9390
0
  cstate->off_linkpl.is_variable = 1;
9391
0
  cstate->off_linkpl.constant_part = 0;
9392
9393
0
  s1 = new_stmt(cstate, BPF_STX);
9394
0
  s1->s.k = cstate->off_linkpl.reg;
9395
0
  sappend(s, s1);
9396
0
  s_proto->s.jf = s1;
9397
9398
0
  cstate->off_nl = 0;
9399
9400
0
  return s;
9401
0
}
9402
9403
/* Check to see if this is a Geneve packet. */
9404
struct block *
9405
gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9406
0
{
9407
0
  struct block *b0, *b1;
9408
0
  struct slist *s;
9409
9410
  /*
9411
   * Catch errors reported by us and routines below us, and return NULL
9412
   * on an error.
9413
   */
9414
0
  if (setjmp(cstate->top_ctx))
9415
0
    return (NULL);
9416
9417
0
  b0 = gen_geneve4(cstate, vni, has_vni);
9418
0
  b1 = gen_geneve6(cstate, vni, has_vni);
9419
9420
0
  gen_or(b0, b1);
9421
0
  b0 = b1;
9422
9423
  /* Later filters should act on the payload of the Geneve frame,
9424
   * update all of the header pointers. Attach this code so that
9425
   * it gets executed in the event that the Geneve filter matches. */
9426
0
  s = gen_geneve_offsets(cstate);
9427
9428
0
  b1 = gen_true(cstate);
9429
0
  sappend(s, b1->stmts);
9430
0
  b1->stmts = s;
9431
9432
0
  gen_and(b0, b1);
9433
9434
0
  cstate->is_geneve = 1;
9435
9436
0
  return b1;
9437
0
}
9438
9439
/* Check that the encapsulated frame has a link layer header
9440
 * for Ethernet filters. */
9441
static struct block *
9442
gen_geneve_ll_check(compiler_state_t *cstate)
9443
0
{
9444
0
  struct block *b0;
9445
0
  struct slist *s, *s1;
9446
9447
  /* The easiest way to see if there is a link layer present
9448
   * is to check if the link layer header and payload are not
9449
   * the same. */
9450
9451
  /* Geneve always generates pure variable offsets so we can
9452
   * compare only the registers. */
9453
0
  s = new_stmt(cstate, BPF_LD|BPF_MEM);
9454
0
  s->s.k = cstate->off_linkhdr.reg;
9455
9456
0
  s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
9457
0
  s1->s.k = cstate->off_linkpl.reg;
9458
0
  sappend(s, s1);
9459
9460
0
  b0 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9461
0
  b0->stmts = s;
9462
0
  b0->s.k = 0;
9463
0
  gen_not(b0);
9464
9465
0
  return b0;
9466
0
}
9467
9468
static struct block *
9469
gen_atmfield_code_internal(compiler_state_t *cstate, int atmfield,
9470
    bpf_u_int32 jvalue, int jtype, int reverse)
9471
2.22k
{
9472
2.22k
  struct block *b0;
9473
9474
2.22k
  switch (atmfield) {
9475
9476
319
  case A_VPI:
9477
319
    if (!cstate->is_atm)
9478
1
      bpf_error(cstate, "'vpi' supported only on raw ATM");
9479
318
    if (cstate->off_vpi == OFFSET_NOT_SET)
9480
0
      abort();
9481
318
    b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B,
9482
318
        0xffffffffU, jtype, reverse, jvalue);
9483
318
    break;
9484
9485
343
  case A_VCI:
9486
343
    if (!cstate->is_atm)
9487
3
      bpf_error(cstate, "'vci' supported only on raw ATM");
9488
340
    if (cstate->off_vci == OFFSET_NOT_SET)
9489
0
      abort();
9490
340
    b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H,
9491
340
        0xffffffffU, jtype, reverse, jvalue);
9492
340
    break;
9493
9494
1.56k
  case A_PROTOTYPE:
9495
1.56k
    if (cstate->off_proto == OFFSET_NOT_SET)
9496
0
      abort(); /* XXX - this isn't on FreeBSD */
9497
1.56k
    b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,
9498
1.56k
        0x0fU, jtype, reverse, jvalue);
9499
1.56k
    break;
9500
9501
0
  case A_MSGTYPE:
9502
0
    if (cstate->off_payload == OFFSET_NOT_SET)
9503
0
      abort();
9504
0
    b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, BPF_B,
9505
0
        0xffffffffU, jtype, reverse, jvalue);
9506
0
    break;
9507
9508
0
  case A_CALLREFTYPE:
9509
0
    if (!cstate->is_atm)
9510
0
      bpf_error(cstate, "'callref' supported only on raw ATM");
9511
0
    if (cstate->off_proto == OFFSET_NOT_SET)
9512
0
      abort();
9513
0
    b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,
9514
0
        0xffffffffU, jtype, reverse, jvalue);
9515
0
    break;
9516
9517
0
  default:
9518
0
    abort();
9519
2.22k
  }
9520
2.22k
  return b0;
9521
2.22k
}
9522
9523
static struct block *
9524
gen_atmtype_metac(compiler_state_t *cstate)
9525
10
{
9526
10
  struct block *b0, *b1;
9527
9528
10
  b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9529
10
  b1 = gen_atmfield_code_internal(cstate, A_VCI, 1, BPF_JEQ, 0);
9530
10
  gen_and(b0, b1);
9531
10
  return b1;
9532
10
}
9533
9534
static struct block *
9535
gen_atmtype_sc(compiler_state_t *cstate)
9536
75
{
9537
75
  struct block *b0, *b1;
9538
9539
75
  b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9540
75
  b1 = gen_atmfield_code_internal(cstate, A_VCI, 5, BPF_JEQ, 0);
9541
75
  gen_and(b0, b1);
9542
75
  return b1;
9543
75
}
9544
9545
static struct block *
9546
gen_atmtype_llc(compiler_state_t *cstate)
9547
88
{
9548
88
  struct block *b0;
9549
9550
88
  b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
9551
88
  cstate->linktype = cstate->prevlinktype;
9552
88
  return b0;
9553
88
}
9554
9555
struct block *
9556
gen_atmfield_code(compiler_state_t *cstate, int atmfield,
9557
    bpf_u_int32 jvalue, int jtype, int reverse)
9558
368
{
9559
  /*
9560
   * Catch errors reported by us and routines below us, and return NULL
9561
   * on an error.
9562
   */
9563
368
  if (setjmp(cstate->top_ctx))
9564
4
    return (NULL);
9565
9566
364
  return gen_atmfield_code_internal(cstate, atmfield, jvalue, jtype,
9567
364
      reverse);
9568
368
}
9569
9570
struct block *
9571
gen_atmtype_abbrev(compiler_state_t *cstate, int type)
9572
208
{
9573
208
  struct block *b0, *b1;
9574
9575
  /*
9576
   * Catch errors reported by us and routines below us, and return NULL
9577
   * on an error.
9578
   */
9579
208
  if (setjmp(cstate->top_ctx))
9580
7
    return (NULL);
9581
9582
201
  switch (type) {
9583
9584
11
  case A_METAC:
9585
    /* Get all packets in Meta signalling Circuit */
9586
11
    if (!cstate->is_atm)
9587
1
      bpf_error(cstate, "'metac' supported only on raw ATM");
9588
10
    b1 = gen_atmtype_metac(cstate);
9589
10
    break;
9590
9591
9
  case A_BCC:
9592
    /* Get all packets in Broadcast Circuit*/
9593
9
    if (!cstate->is_atm)
9594
1
      bpf_error(cstate, "'bcc' supported only on raw ATM");
9595
8
    b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9596
8
    b1 = gen_atmfield_code_internal(cstate, A_VCI, 2, BPF_JEQ, 0);
9597
8
    gen_and(b0, b1);
9598
8
    break;
9599
9600
11
  case A_OAMF4SC:
9601
    /* Get all cells in Segment OAM F4 circuit*/
9602
11
    if (!cstate->is_atm)
9603
1
      bpf_error(cstate, "'oam4sc' supported only on raw ATM");
9604
10
    b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9605
10
    b1 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
9606
10
    gen_and(b0, b1);
9607
10
    break;
9608
9609
11
  case A_OAMF4EC:
9610
    /* Get all cells in End-to-End OAM F4 Circuit*/
9611
11
    if (!cstate->is_atm)
9612
1
      bpf_error(cstate, "'oam4ec' supported only on raw ATM");
9613
10
    b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9614
10
    b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
9615
10
    gen_and(b0, b1);
9616
10
    break;
9617
9618
76
  case A_SC:
9619
    /*  Get all packets in connection Signalling Circuit */
9620
76
    if (!cstate->is_atm)
9621
1
      bpf_error(cstate, "'sc' supported only on raw ATM");
9622
75
    b1 = gen_atmtype_sc(cstate);
9623
75
    break;
9624
9625
11
  case A_ILMIC:
9626
    /* Get all packets in ILMI Circuit */
9627
11
    if (!cstate->is_atm)
9628
1
      bpf_error(cstate, "'ilmic' supported only on raw ATM");
9629
10
    b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9630
10
    b1 = gen_atmfield_code_internal(cstate, A_VCI, 16, BPF_JEQ, 0);
9631
10
    gen_and(b0, b1);
9632
10
    break;
9633
9634
79
  case A_LANE:
9635
    /* Get all LANE packets */
9636
79
    if (!cstate->is_atm)
9637
1
      bpf_error(cstate, "'lane' supported only on raw ATM");
9638
78
    b1 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
9639
9640
    /*
9641
     * Arrange that all subsequent tests assume LANE
9642
     * rather than LLC-encapsulated packets, and set
9643
     * the offsets appropriately for LANE-encapsulated
9644
     * Ethernet.
9645
     *
9646
     * We assume LANE means Ethernet, not Token Ring.
9647
     */
9648
78
    PUSH_LINKHDR(cstate, DLT_EN10MB, 0,
9649
78
        cstate->off_payload + 2,  /* Ethernet header */
9650
78
        -1);
9651
78
    cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
9652
78
    cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14;  /* Ethernet */
9653
78
    cstate->off_nl = 0;     /* Ethernet II */
9654
78
    cstate->off_nl_nosnap = 3;    /* 802.3+802.2 */
9655
78
    break;
9656
9657
0
  case A_LLC:
9658
    /* Get all LLC-encapsulated packets */
9659
0
    if (!cstate->is_atm)
9660
0
      bpf_error(cstate, "'llc' supported only on raw ATM");
9661
0
    b1 = gen_atmtype_llc(cstate);
9662
0
    break;
9663
9664
0
  default:
9665
0
    abort();
9666
201
  }
9667
201
  return b1;
9668
201
}
9669
9670
/*
9671
 * Filtering for MTP2 messages based on li value
9672
 * FISU, length is null
9673
 * LSSU, length is 1 or 2
9674
 * MSU, length is 3 or more
9675
 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9676
 */
9677
struct block *
9678
gen_mtp2type_abbrev(compiler_state_t *cstate, int type)
9679
959
{
9680
959
  struct block *b0, *b1;
9681
9682
  /*
9683
   * Catch errors reported by us and routines below us, and return NULL
9684
   * on an error.
9685
   */
9686
959
  if (setjmp(cstate->top_ctx))
9687
103
    return (NULL);
9688
9689
856
  switch (type) {
9690
9691
134
  case M_FISU:
9692
134
    if ( (cstate->linktype != DLT_MTP2) &&
9693
134
         (cstate->linktype != DLT_ERF) &&
9694
134
         (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9695
20
      bpf_error(cstate, "'fisu' supported only on MTP2");
9696
    /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9697
114
    b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9698
114
        0x3fU, BPF_JEQ, 0, 0U);
9699
114
    break;
9700
9701
301
  case M_LSSU:
9702
301
    if ( (cstate->linktype != DLT_MTP2) &&
9703
301
         (cstate->linktype != DLT_ERF) &&
9704
301
         (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9705
22
      bpf_error(cstate, "'lssu' supported only on MTP2");
9706
279
    b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9707
279
        0x3fU, BPF_JGT, 1, 2U);
9708
279
    b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9709
279
        0x3fU, BPF_JGT, 0, 0U);
9710
279
    gen_and(b1, b0);
9711
279
    break;
9712
9713
160
  case M_MSU:
9714
160
    if ( (cstate->linktype != DLT_MTP2) &&
9715
160
         (cstate->linktype != DLT_ERF) &&
9716
160
         (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9717
17
      bpf_error(cstate, "'msu' supported only on MTP2");
9718
143
    b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9719
143
        0x3fU, BPF_JGT, 0, 2U);
9720
143
    break;
9721
9722
119
  case MH_FISU:
9723
119
    if ( (cstate->linktype != DLT_MTP2) &&
9724
119
         (cstate->linktype != DLT_ERF) &&
9725
119
         (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9726
14
      bpf_error(cstate, "'hfisu' supported only on MTP2_HSL");
9727
    /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9728
105
    b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9729
105
        0xff80U, BPF_JEQ, 0, 0U);
9730
105
    break;
9731
9732
121
  case MH_LSSU:
9733
121
    if ( (cstate->linktype != DLT_MTP2) &&
9734
121
         (cstate->linktype != DLT_ERF) &&
9735
121
         (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9736
15
      bpf_error(cstate, "'hlssu' supported only on MTP2_HSL");
9737
106
    b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9738
106
        0xff80U, BPF_JGT, 1, 0x0100U);
9739
106
    b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9740
106
        0xff80U, BPF_JGT, 0, 0U);
9741
106
    gen_and(b1, b0);
9742
106
    break;
9743
9744
124
  case MH_MSU:
9745
124
    if ( (cstate->linktype != DLT_MTP2) &&
9746
124
         (cstate->linktype != DLT_ERF) &&
9747
124
         (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9748
15
      bpf_error(cstate, "'hmsu' supported only on MTP2_HSL");
9749
109
    b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9750
109
        0xff80U, BPF_JGT, 0, 0x0100U);
9751
109
    break;
9752
9753
0
  default:
9754
0
    abort();
9755
856
  }
9756
856
  return b0;
9757
856
}
9758
9759
/*
9760
 * The jvalue_arg dance is to avoid annoying whining by compilers that
9761
 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9762
 * It's not *used* after setjmp returns.
9763
 */
9764
struct block *
9765
gen_mtp3field_code(compiler_state_t *cstate, int mtp3field,
9766
    bpf_u_int32 jvalue_arg, int jtype, int reverse)
9767
1.08k
{
9768
1.08k
  volatile bpf_u_int32 jvalue = jvalue_arg;
9769
1.08k
  struct block *b0;
9770
1.08k
  bpf_u_int32 val1 , val2 , val3;
9771
1.08k
  u_int newoff_sio;
9772
1.08k
  u_int newoff_opc;
9773
1.08k
  u_int newoff_dpc;
9774
1.08k
  u_int newoff_sls;
9775
9776
  /*
9777
   * Catch errors reported by us and routines below us, and return NULL
9778
   * on an error.
9779
   */
9780
1.08k
  if (setjmp(cstate->top_ctx))
9781
128
    return (NULL);
9782
9783
953
  newoff_sio = cstate->off_sio;
9784
953
  newoff_opc = cstate->off_opc;
9785
953
  newoff_dpc = cstate->off_dpc;
9786
953
  newoff_sls = cstate->off_sls;
9787
953
  switch (mtp3field) {
9788
9789
95
  case MH_SIO:
9790
95
    newoff_sio += 3; /* offset for MTP2_HSL */
9791
    /* FALLTHROUGH */
9792
9793
303
  case M_SIO:
9794
303
    if (cstate->off_sio == OFFSET_NOT_SET)
9795
3
      bpf_error(cstate, "'sio' supported only on SS7");
9796
    /* sio coded on 1 byte so max value 255 */
9797
300
    if(jvalue > 255)
9798
36
            bpf_error(cstate, "sio value %u too big; max value = 255",
9799
36
                jvalue);
9800
264
    b0 = gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, 0xffffffffU,
9801
264
        jtype, reverse, jvalue);
9802
264
    break;
9803
9804
120
  case MH_OPC:
9805
120
    newoff_opc += 3;
9806
9807
    /* FALLTHROUGH */
9808
279
        case M_OPC:
9809
279
          if (cstate->off_opc == OFFSET_NOT_SET)
9810
1
      bpf_error(cstate, "'opc' supported only on SS7");
9811
    /* opc coded on 14 bits so max value 16383 */
9812
278
    if (jvalue > 16383)
9813
27
            bpf_error(cstate, "opc value %u too big; max value = 16383",
9814
27
                jvalue);
9815
    /* the following instructions are made to convert jvalue
9816
     * to the form used to write opc in an ss7 message*/
9817
251
    val1 = jvalue & 0x00003c00;
9818
251
    val1 = val1 >>10;
9819
251
    val2 = jvalue & 0x000003fc;
9820
251
    val2 = val2 <<6;
9821
251
    val3 = jvalue & 0x00000003;
9822
251
    val3 = val3 <<22;
9823
251
    jvalue = val1 + val2 + val3;
9824
251
    b0 = gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W, 0x00c0ff0fU,
9825
251
        jtype, reverse, jvalue);
9826
251
    break;
9827
9828
98
  case MH_DPC:
9829
98
    newoff_dpc += 3;
9830
    /* FALLTHROUGH */
9831
9832
277
  case M_DPC:
9833
277
          if (cstate->off_dpc == OFFSET_NOT_SET)
9834
2
      bpf_error(cstate, "'dpc' supported only on SS7");
9835
    /* dpc coded on 14 bits so max value 16383 */
9836
275
    if (jvalue > 16383)
9837
22
            bpf_error(cstate, "dpc value %u too big; max value = 16383",
9838
22
                jvalue);
9839
    /* the following instructions are made to convert jvalue
9840
     * to the forme used to write dpc in an ss7 message*/
9841
253
    val1 = jvalue & 0x000000ff;
9842
253
    val1 = val1 << 24;
9843
253
    val2 = jvalue & 0x00003f00;
9844
253
    val2 = val2 << 8;
9845
253
    jvalue = val1 + val2;
9846
253
    b0 = gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_W, 0xff3f0000U,
9847
253
        jtype, reverse, jvalue);
9848
253
    break;
9849
9850
96
  case MH_SLS:
9851
96
    newoff_sls += 3;
9852
    /* FALLTHROUGH */
9853
9854
222
  case M_SLS:
9855
222
          if (cstate->off_sls == OFFSET_NOT_SET)
9856
2
      bpf_error(cstate, "'sls' supported only on SS7");
9857
    /* sls coded on 4 bits so max value 15 */
9858
220
    if (jvalue > 15)
9859
35
             bpf_error(cstate, "sls value %u too big; max value = 15",
9860
35
                 jvalue);
9861
    /* the following instruction is made to convert jvalue
9862
     * to the forme used to write sls in an ss7 message*/
9863
185
    jvalue = jvalue << 4;
9864
185
    b0 = gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B, 0xf0U,
9865
185
        jtype, reverse, jvalue);
9866
185
    break;
9867
9868
0
  default:
9869
0
    abort();
9870
953
  }
9871
953
  return b0;
9872
953
}
9873
9874
static struct block *
9875
gen_msg_abbrev(compiler_state_t *cstate, int type)
9876
0
{
9877
0
  struct block *b1;
9878
9879
  /*
9880
   * Q.2931 signalling protocol messages for handling virtual circuits
9881
   * establishment and teardown
9882
   */
9883
0
  switch (type) {
9884
9885
0
  case A_SETUP:
9886
0
    b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, SETUP, BPF_JEQ, 0);
9887
0
    break;
9888
9889
0
  case A_CALLPROCEED:
9890
0
    b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
9891
0
    break;
9892
9893
0
  case A_CONNECT:
9894
0
    b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT, BPF_JEQ, 0);
9895
0
    break;
9896
9897
0
  case A_CONNECTACK:
9898
0
    b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
9899
0
    break;
9900
9901
0
  case A_RELEASE:
9902
0
    b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE, BPF_JEQ, 0);
9903
0
    break;
9904
9905
0
  case A_RELEASE_DONE:
9906
0
    b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
9907
0
    break;
9908
9909
0
  default:
9910
0
    abort();
9911
0
  }
9912
0
  return b1;
9913
0
}
9914
9915
struct block *
9916
gen_atmmulti_abbrev(compiler_state_t *cstate, int type)
9917
18
{
9918
18
  struct block *b0, *b1;
9919
9920
  /*
9921
   * Catch errors reported by us and routines below us, and return NULL
9922
   * on an error.
9923
   */
9924
18
  if (setjmp(cstate->top_ctx))
9925
2
    return (NULL);
9926
9927
16
  switch (type) {
9928
9929
11
  case A_OAM:
9930
11
    if (!cstate->is_atm)
9931
1
      bpf_error(cstate, "'oam' supported only on raw ATM");
9932
    /* OAM F4 type */
9933
10
    b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
9934
10
    b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
9935
10
    gen_or(b0, b1);
9936
10
    b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9937
10
    gen_and(b0, b1);
9938
10
    break;
9939
9940
7
  case A_OAMF4:
9941
7
    if (!cstate->is_atm)
9942
1
      bpf_error(cstate, "'oamf4' supported only on raw ATM");
9943
    /* OAM F4 type */
9944
6
    b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
9945
6
    b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
9946
6
    gen_or(b0, b1);
9947
6
    b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9948
6
    gen_and(b0, b1);
9949
6
    break;
9950
9951
0
  case A_CONNECTMSG:
9952
    /*
9953
     * Get Q.2931 signalling messages for switched
9954
     * virtual connection
9955
     */
9956
0
    if (!cstate->is_atm)
9957
0
      bpf_error(cstate, "'connectmsg' supported only on raw ATM");
9958
0
    b0 = gen_msg_abbrev(cstate, A_SETUP);
9959
0
    b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
9960
0
    gen_or(b0, b1);
9961
0
    b0 = gen_msg_abbrev(cstate, A_CONNECT);
9962
0
    gen_or(b0, b1);
9963
0
    b0 = gen_msg_abbrev(cstate, A_CONNECTACK);
9964
0
    gen_or(b0, b1);
9965
0
    b0 = gen_msg_abbrev(cstate, A_RELEASE);
9966
0
    gen_or(b0, b1);
9967
0
    b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
9968
0
    gen_or(b0, b1);
9969
0
    b0 = gen_atmtype_sc(cstate);
9970
0
    gen_and(b0, b1);
9971
0
    break;
9972
9973
0
  case A_METACONNECT:
9974
0
    if (!cstate->is_atm)
9975
0
      bpf_error(cstate, "'metaconnect' supported only on raw ATM");
9976
0
    b0 = gen_msg_abbrev(cstate, A_SETUP);
9977
0
    b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
9978
0
    gen_or(b0, b1);
9979
0
    b0 = gen_msg_abbrev(cstate, A_CONNECT);
9980
0
    gen_or(b0, b1);
9981
0
    b0 = gen_msg_abbrev(cstate, A_RELEASE);
9982
0
    gen_or(b0, b1);
9983
0
    b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
9984
0
    gen_or(b0, b1);
9985
0
    b0 = gen_atmtype_metac(cstate);
9986
0
    gen_and(b0, b1);
9987
0
    break;
9988
9989
0
  default:
9990
0
    abort();
9991
16
  }
9992
16
  return b1;
9993
16
}