Coverage Report

Created: 2026-09-03 06:05

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