Coverage Report

Created: 2023-01-17 06:15

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