Coverage Report

Created: 2023-06-07 06:26

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