Coverage Report

Created: 2023-09-25 06:32

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