Coverage Report

Created: 2026-02-26 06:15

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