Coverage Report

Created: 2026-04-12 06:37

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