Home | History | Annotate | Download | only in libpcap

Lines Matching refs:cstate

421 bpf_syntax_error(compiler_state_t *cstate, const char *msg)
423 bpf_error(cstate, "syntax error in filter expression: %s", msg);
429 bpf_error(compiler_state_t *cstate, const char *fmt, ...)
434 if (cstate->bpf_pcap != NULL)
435 (void)pcap_vsnprintf(pcap_geterr(cstate->bpf_pcap),
438 longjmp(cstate->top_ctx, 1);
448 static void initchunks(compiler_state_t *cstate);
449 static void *newchunk(compiler_state_t *cstate, size_t);
450 static void freechunks(compiler_state_t *cstate);
451 static inline struct block *new_block(compiler_state_t *cstate, int);
452 static inline struct slist *new_stmt(compiler_state_t *cstate, int);
453 static struct block *gen_retblk(compiler_state_t *cstate, int);
454 static inline void syntax(compiler_state_t *cstate);
543 static struct block *gen_geneve_ll_check(compiler_state_t *cstate);
549 initchunks(compiler_state_t *cstate)
554 cstate->chunks[i].n_left = 0;
555 cstate->chunks[i].m = NULL;
557 cstate->cur_chunk = 0;
561 newchunk(compiler_state_t *cstate, size_t n)
575 cp = &cstate->chunks[cstate->cur_chunk];
578 k = ++cstate->cur_chunk;
580 bpf_error(cstate, "out of memory");
584 bpf_error(cstate, "out of memory");
588 bpf_error(cstate, "out of memory");
595 freechunks(compiler_state_t *cstate)
600 if (cstate->chunks[i].m != NULL)
601 free(cstate->chunks[i].m);
608 sdup(compiler_state_t *cstate, const char *s)
611 char *cp = newchunk(cstate, n);
618 new_block(compiler_state_t *cstate, int code)
622 p = (struct block *)newchunk(cstate, sizeof(*p));
630 new_stmt(compiler_state_t *cstate, int code)
634 p = (struct slist *)newchunk(cstate, sizeof(*p));
641 gen_retblk(compiler_state_t *cstate, int v)
643 struct block *b = new_block(cstate, BPF_RET|BPF_K);
650 syntax(compiler_state_t *cstate)
652 bpf_error(cstate, "syntax error in filter expression");
662 compiler_state_t cstate;
706 initchunks(&cstate);
707 cstate.no_optimize = 0;
709 cstate.ai = NULL;
711 cstate.ic.root = NULL;
712 cstate.ic.cur_mark = 0;
713 cstate.bpf_pcap = p;
714 init_regs(&cstate);
716 if (setjmp(cstate.top_ctx)) {
718 if (cstate.ai != NULL)
719 freeaddrinfo(cstate.ai);
725 cstate.netmask = mask;
727 cstate.snaplen = pcap_snapshot(p);
728 if (cstate.snaplen == 0) {
744 pcap_set_extra(&cstate, scanner);
746 init_linktype(&cstate, p);
747 (void)pcap_parse(scanner, &cstate);
749 if (cstate.ic.root == NULL)
750 cstate.ic.root = gen_retblk(&cstate, cstate.snaplen);
752 if (optimize && !cstate.no_optimize) {
753 bpf_optimize(&cstate, &cstate.ic);
754 if (cstate.ic.root == NULL ||
755 (cstate.ic.root->s.code == (BPF_RET|BPF_K) && cstate.ic.root->s.k == 0))
756 bpf_error(&cstate, "expression rejects all packets");
758 program->bf_insns = icode_to_fcode(&cstate, &cstate.ic, cstate.ic.root, &len);
775 freechunks(&cstate);
855 finish_parse(compiler_state_t *cstate, struct block *p)
878 insert_compute_vloffsets(cstate, p->head);
893 ppi_dlt_check = gen_ppi_dlt_check(cstate);
897 backpatch(p, gen_retblk(cstate, cstate->snaplen));
899 backpatch(p, gen_retblk(cstate, 0));
900 cstate->ic.root = p->head;
931 gen_cmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
934 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
938 gen_cmp_gt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
941 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
945 gen_cmp_ge(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
948 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
952 gen_cmp_lt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
955 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
959 gen_cmp_le(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
962 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
966 gen_mcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
969 return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 0, v);
973 gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
984 tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W, w);
994 tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H, w);
1001 tmp = gen_cmp(cstate, offrel, offset, BPF_B, (bpf_int32)v[0]);
1016 gen_ncmp(compiler_state_t *cstate, enum e_offrel offrel, bpf_u_int32 offset,
1023 s = gen_load_a(cstate, offrel, offset, size);
1026 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
1031 b = new_block(cstate, JMP(jtype));
1040 init_linktype(compiler_state_t *cstate, pcap_t *p)
1042 cstate->pcap_fddipad = p->fddipad;
1047 cstate->outermostlinktype = pcap_datalink(p);
1048 cstate->off_outermostlinkhdr.constant_part = 0;
1049 cstate->off_outermostlinkhdr.is_variable = 0;
1050 cstate->off_outermostlinkhdr.reg = -1;
1052 cstate->prevlinktype = cstate->outermostlinktype;
1053 cstate->off_prevlinkhdr.constant_part = 0;
1054 cstate->off_prevlinkhdr.is_variable = 0;
1055 cstate->off_prevlinkhdr.reg = -1;
1057 cstate->linktype = cstate->outermostlinktype;
1058 cstate->off_linkhdr.constant_part = 0;
1059 cstate->off_linkhdr.is_variable = 0;
1060 cstate->off_linkhdr.reg = -1;
1065 cstate->off_linkpl.constant_part = 0;
1066 cstate->off_linkpl.is_variable = 0;
1067 cstate->off_linkpl.reg = -1;
1069 cstate->off_linktype.constant_part = 0;
1070 cstate->off_linktype.is_variable = 0;
1071 cstate->off_linktype.reg = -1;
1076 cstate->is_atm = 0;
1077 cstate->off_vpi = OFFSET_NOT_SET;
1078 cstate->off_vci = OFFSET_NOT_SET;
1079 cstate->off_proto = OFFSET_NOT_SET;
1080 cstate->off_payload = OFFSET_NOT_SET;
1085 cstate->is_geneve = 0;
1090 cstate->is_vlan_vloffset = 0;
1095 cstate->off_li = OFFSET_NOT_SET;
1096 cstate->off_li_hsl = OFFSET_NOT_SET;
1097 cstate->off_sio = OFFSET_NOT_SET;
1098 cstate->off_opc = OFFSET_NOT_SET;
1099 cstate->off_dpc = OFFSET_NOT_SET;
1100 cstate->off_sls = OFFSET_NOT_SET;
1102 cstate->label_stack_depth = 0;
1103 cstate->vlan_stack_depth = 0;
1105 switch (cstate->linktype) {
1108 cstate->off_linktype.constant_part = 2;
1109 cstate->off_linkpl.constant_part = 6;
1110 cstate->off_nl = 0; /* XXX in reality, variable! */
1111 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1115 cstate->off_linktype.constant_part = 4;
1116 cstate->off_linkpl.constant_part = 8;
1117 cstate->off_nl = 0; /* XXX in reality, variable! */
1118 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1122 cstate->off_linktype.constant_part = 12;
1123 cstate->off_linkpl.constant_part = 14; /* Ethernet header length */
1124 cstate->off_nl = 0; /* Ethernet II */
1125 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
1133 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1134 cstate->off_linkpl.constant_part = 16;
1135 cstate->off_nl = 0;
1136 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1141 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1143 cstate->off_linkpl.constant_part = 24;
1144 cstate->off_nl = 0;
1145 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1150 cstate->off_linktype.constant_part = 0;
1151 cstate->off_linkpl.constant_part = 4;
1152 cstate->off_nl = 0;
1153 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1157 cstate->off_linktype.constant_part = 0;
1158 cstate->off_linkpl.constant_part = 12;
1159 cstate->off_nl = 0;
1160 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1167 cstate->off_linktype.constant_part = 2; /* skip HDLC-like framing */
1168 cstate->off_linkpl.constant_part = 4; /* skip HDLC-like framing and protocol field */
1169 cstate->off_nl = 0;
1170 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1178 cstate->off_linktype.constant_part = 6;
1179 cstate->off_linkpl.constant_part = 8;
1180 cstate->off_nl = 0;
1181 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1185 cstate->off_linktype.constant_part = 5;
1186 cstate->off_linkpl.constant_part = 24;
1187 cstate->off_nl = 0;
1188 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1200 cstate->off_linktype.constant_part = 13;
1201 cstate->off_linktype.constant_part += cstate->pcap_fddipad;
1202 cstate->off_linkpl.constant_part = 13; /* FDDI MAC header length */
1203 cstate->off_linkpl.constant_part += cstate->pcap_fddipad;
1204 cstate->off_nl = 8; /* 802.2+SNAP */
1205 cstate->off_nl_nosnap = 3; /* 802.2 */
1232 cstate->off_linktype.constant_part = 14;
1233 cstate->off_linkpl.constant_part = 14; /* Token Ring MAC header length */
1234 cstate->off_nl = 8; /* 802.2+SNAP */
1235 cstate->off_nl_nosnap = 3; /* 802.2 */
1241 cstate->off_linkhdr.is_variable = 1;
1264 cstate->off_linktype.constant_part = 24;
1265 cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */
1266 cstate->off_linkpl.is_variable = 1;
1267 cstate->off_nl = 8; /* 802.2+SNAP */
1268 cstate->off_nl_nosnap = 3; /* 802.2 */
1281 cstate->off_linktype.constant_part = 24;
1282 cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */
1283 cstate->off_linkpl.is_variable = 1;
1284 cstate->off_linkhdr.is_variable = 1;
1285 cstate->off_nl = 8; /* 802.2+SNAP */
1286 cstate->off_nl_nosnap = 3; /* 802.2 */
1302 cstate->off_linktype.constant_part = 0;
1303 cstate->off_linkpl.constant_part = 0; /* packet begins with LLC header */
1304 cstate->off_nl = 8; /* 802.2+SNAP */
1305 cstate->off_nl_nosnap = 3; /* 802.2 */
1313 cstate->is_atm = 1;
1314 cstate->off_vpi = SUNATM_VPI_POS;
1315 cstate->off_vci = SUNATM_VCI_POS;
1316 cstate->off_proto = PROTO_POS;
1317 cstate->off_payload = SUNATM_PKT_BEGIN_POS;
1318 cstate->off_linktype.constant_part = cstate->off_payload;
1319 cstate->off_linkpl.constant_part = cstate->off_payload; /* if LLC-encapsulated */
1320 cstate->off_nl = 8; /* 802.2+SNAP */
1321 cstate->off_nl_nosnap = 3; /* 802.2 */
1327 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1328 cstate->off_linkpl.constant_part = 0;
1329 cstate->off_nl = 0;
1330 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1334 cstate->off_linktype.constant_part = 14;
1335 cstate->off_linkpl.constant_part = 16;
1336 cstate->off_nl = 0;
1337 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1346 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1347 cstate->off_linkpl.constant_part = 0;
1348 cstate->off_nl = 0;
1349 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1363 cstate->off_linktype.constant_part = 16;
1364 cstate->off_linkpl.constant_part = 16;
1365 cstate->off_nl = 8; /* 802.2+SNAP */
1366 cstate->off_nl_nosnap = 3; /* 802.2 */
1374 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1375 cstate->off_linkpl.constant_part = 0;
1376 cstate->off_nl = 0;
1377 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1386 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1387 cstate->off_linkpl.constant_part = 0;
1388 cstate->off_nl = 4;
1389 cstate->off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1393 cstate->off_linktype.constant_part = 16;
1394 cstate->off_linkpl.constant_part = 18;
1395 cstate->off_nl = 0;
1396 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1400 cstate->off_linktype.constant_part = 6;
1401 cstate->off_linkpl.constant_part = 44;
1402 cstate->off_nl = 0; /* Ethernet II */
1403 cstate->off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
1408 cstate->off_linktype.constant_part = 0;
1409 cstate->off_linkpl.constant_part = PFLOG_HDRLEN;
1410 cstate->off_nl = 0;
1411 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1421 cstate->off_linktype.constant_part = 4;
1422 cstate->off_linkpl.constant_part = 4;
1423 cstate->off_nl = 0;
1424 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1428 cstate->off_linktype.constant_part = 4; /* in reality variable between 4-8 */
1429 cstate->off_linkpl.constant_part = 4; /* in reality variable between 4-8 */
1430 cstate->off_nl = 0;
1431 cstate->off_nl_nosnap = 10;
1435 cstate->off_linktype.constant_part = 8; /* in reality variable between 8-12 */
1436 cstate->off_linkpl.constant_part = 8; /* in reality variable between 8-12 */
1437 cstate->off_nl = 0;
1438 cstate->off_nl_nosnap = 10;
1445 cstate->off_linkpl.constant_part = 14;
1446 cstate->off_linktype.constant_part = 16;
1447 cstate->off_nl = 18; /* Ethernet II */
1448 cstate->off_nl_nosnap = 21; /* 802.3+802.2 */
1452 cstate->off_linktype.constant_part = 4;
1453 cstate->off_linkpl.constant_part = 6;
1454 cstate->off_nl = 0;
1455 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1459 cstate->off_linktype.constant_part = 6;
1460 cstate->off_linkpl.constant_part = 12;
1461 cstate->off_nl = 0;
1462 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1466 cstate->off_linktype.constant_part = 6;
1467 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */
1468 cstate->off_nl = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */
1469 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1473 cstate->off_linktype.constant_part = 12;
1474 cstate->off_linkpl.constant_part = 12;
1475 cstate->off_nl = 0; /* raw IP/IP6 header */
1476 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1480 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1481 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1482 cstate->off_nl = OFFSET_NOT_SET;
1483 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1487 cstate->off_linktype.constant_part = 12;
1488 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */
1489 cstate->off_nl = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */
1490 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1494 cstate->off_linktype.constant_part = 18;
1495 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1496 cstate->off_nl = OFFSET_NOT_SET;
1497 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1501 cstate->off_linktype.constant_part = 18;
1502 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1503 cstate->off_nl = OFFSET_NOT_SET;
1504 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1508 cstate->off_linktype.constant_part = 8;
1509 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1510 cstate->off_nl = OFFSET_NOT_SET;
1511 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1518 cstate->off_linktype.constant_part = 8;
1519 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1520 cstate->off_nl = OFFSET_NOT_SET;
1521 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1525 cstate->off_li = 2;
1526 cstate->off_li_hsl = 4;
1527 cstate->off_sio = 3;
1528 cstate->off_opc = 4;
1529 cstate->off_dpc = 4;
1530 cstate->off_sls = 7;
1531 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1532 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1533 cstate->off_nl = OFFSET_NOT_SET;
1534 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1538 cstate->off_li = 6;
1539 cstate->off_li_hsl = 8;
1540 cstate->off_sio = 7;
1541 cstate->off_opc = 8;
1542 cstate->off_dpc = 8;
1543 cstate->off_sls = 11;
1544 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1545 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1546 cstate->off_nl = OFFSET_NOT_SET;
1547 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1551 cstate->off_li = 22;
1552 cstate->off_li_hsl = 24;
1553 cstate->off_sio = 23;
1554 cstate->off_opc = 24;
1555 cstate->off_dpc = 24;
1556 cstate->off_sls = 27;
1557 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1558 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1559 cstate->off_nl = OFFSET_NOT_SET;
1560 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1564 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1565 cstate->off_linkpl.constant_part = 4;
1566 cstate->off_nl = 0;
1567 cstate->off_nl_nosnap = 0;
1574 cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */
1575 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1576 cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */
1577 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1581 cstate->off_linktype.constant_part = 1;
1582 cstate->off_linkpl.constant_part = 24; /* ipnet header length */
1583 cstate->off_nl = 0;
1584 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1588 cstate->off_linkhdr.constant_part = 4; /* Ethernet header is past 4-byte pseudo-header */
1589 cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
1590 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+Ethernet header length */
1591 cstate->off_nl = 0; /* Ethernet II */
1592 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
1596 cstate->off_linkhdr.constant_part = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1597 cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
1598 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1599 cstate->off_nl = 0; /* Ethernet II */
1600 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
1608 if (cstate->linktype >= DLT_MATCHING_MIN &&
1609 cstate->linktype <= DLT_MATCHING_MAX) {
1610 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1611 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1612 cstate->off_nl = OFFSET_NOT_SET;
1613 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1615 bpf_error(cstate, "unknown data link type %d", cstate->linktype);
1620 cstate->off_outermostlinkhdr = cstate->off_prevlinkhdr = cstate->off_linkhdr;
1627 gen_load_absoffsetrel(compiler_state_t *cstate, bpf_abs_offset *abs_offset,
1632 s = gen_abs_offset_varpart(cstate, abs_offset);
1649 s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);
1657 s = new_stmt(cstate, BPF_LD|BPF_ABS|size);
1667 gen_load_a(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1675 cstate, BPF_LD|BPF_ABS|size);
1680 s = gen_load_absoffsetrel(cstate, &cstate->off_linkhdr, offset, size);
1684 s = gen_load_absoffsetrel(cstate, &cstate->off_prevlinkhdr, offset, size);
1688 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, offset, size);
1692 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl - 4 + offset, size);
1696 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + offset, size);
1700 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl_nosnap + offset, size);
1704 s = gen_load_absoffsetrel(cstate, &cstate->off_linktype, offset, size);
1714 s = gen_loadx_iphdrlen(cstate);
1727 s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);
1728 s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + offset;
1733 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + 40 + offset, size);
1749 gen_loadx_iphdrlen(compiler_state_t *cstate)
1753 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
1765 s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
1766 s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
1768 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
1771 s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
1781 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
1782 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
1791 * is at an offset of cstate->off_nl from the beginning of
1793 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1796 s = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
1797 s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
1804 gen_uncond(compiler_state_t *cstate, int rsense)
1809 s = new_stmt(cstate, BPF_LD|BPF_IMM);
1811 b = new_block(cstate, JMP(BPF_JEQ));
1818 gen_true(compiler_state_t *cstate)
1820 return gen_uncond(cstate, 1);
1824 gen_false(compiler_state_t *cstate)
1826 return gen_uncond(cstate, 0);
1847 gen_ether_linktype(compiler_state_t *cstate, int proto)
1868 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
1870 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_int32)
1908 b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1909 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_int32)0xFFFF);
1916 b0 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);
1923 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
1939 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)ETHERTYPE_IPX);
1955 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
1970 b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
1972 b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);
1980 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
1995 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
1997 b1 = gen_cmp(cstate, OR_LINKTYPE, 2, BPF_B, (bpf_int32)proto);
2010 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,
2017 gen_loopback_linktype(compiler_state_t *cstate, int proto)
2032 if (cstate->linktype == DLT_NULL || cstate->linktype == DLT_ENC) {
2044 if (cstate->bpf_pcap->rfile != NULL && cstate->bpf_pcap->swapped)
2048 return (gen_cmp(cstate, OR_LINKHDR, 0, BPF_W, (bpf_int32)proto));
2056 gen_ipnet_linktype(compiler_state_t *cstate, int proto)
2061 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, (bpf_int32)IPH_AF_INET);
2065 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
2073 return gen_false(cstate);
2085 gen_linux_sll_linktype(compiler_state_t *cstate, int proto)
2106 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2107 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_int32)
2138 b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
2139 b1 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);
2141 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2148 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_3);
2156 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)ETHERTYPE_IPX);
2172 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2186 b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
2188 b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);
2196 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
2210 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2211 b1 = gen_cmp(cstate, OR_LINKHDR, cstate->off_linkpl.constant_part, BPF_B,
2225 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
2231 gen_load_prism_llprefixlen(compiler_state_t *cstate)
2242 cstate->no_optimize = 1;
2263 if (cstate->off_linkhdr.reg != -1) {
2267 s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2273 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
2280 sjeq_avs_cookie = new_stmt(cstate, JMP(BPF_JEQ));
2291 s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2304 sjcommon = new_stmt(cstate, JMP(BPF_JA));
2314 s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);
2324 s2 = new_stmt(cstate, BPF_ST);
2325 s2->s.k = cstate->off_linkhdr.reg;
2332 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2341 gen_load_avs_llprefixlen(compiler_state_t *cstate)
2352 if (cstate->off_linkhdr.reg != -1) {
2358 s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2365 s2 = new_stmt(cstate, BPF_ST);
2366 s2->s.k = cstate->off_linkhdr.reg;
2372 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2381 gen_load_radiotap_llprefixlen(compiler_state_t *cstate)
2392 if (cstate->off_linkhdr.reg != -1) {
2404 s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2406 s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
2409 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2416 s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2419 s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);
2426 s2 = new_stmt(cstate, BPF_ST);
2427 s2->s.k = cstate->off_linkhdr.reg;
2433 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2451 gen_load_ppi_llprefixlen(compiler_state_t *cstate)
2460 if (cstate->off_linkhdr.reg != -1) {
2472 s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2474 s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
2477 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2484 s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2487 s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);
2494 s2 = new_stmt(cstate, BPF_ST);
2495 s2->s.k = cstate->off_linkhdr.reg;
2501 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2517 gen_load_802_11_header_len(compiler_state_t *cstate, struct slist *s, struct slist *snext)
2529 if (cstate->off_linkpl.reg == -1) {
2544 cstate->no_optimize = 1;
2561 * and store it in the cstate->off_linkpl.reg register.
2564 s = new_stmt(cstate, BPF_LDX|BPF_IMM);
2565 s->s.k = cstate->off_outermostlinkhdr.constant_part;
2572 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2575 s2 = new_stmt(cstate, BPF_MISC|BPF_TXA);
2577 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
2580 s2 = new_stmt(cstate, BPF_ST);
2581 s2->s.k = cstate->off_linkpl.reg;
2584 s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
2593 sjset_data_frame_1 = new_stmt(cstate, JMP(BPF_JSET));
2601 sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(cstate, JMP(BPF_JSET));
2612 sjset_data_frame_2->s.jf = sjset_qos = new_stmt(cstate, JMP(BPF_JSET));
2617 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2622 sjset_qos->s.jt = s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
2623 s2->s.k = cstate->off_linkpl.reg;
2625 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
2628 s2 = new_stmt(cstate, BPF_ST);
2629 s2->s.k = cstate->off_linkpl.reg;
2651 if (cstate->linktype == DLT_IEEE802_11_RADIO) {
2656 sjset_qos->s.jf = s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_W);
2660 sjset_radiotap_flags_present = new_stmt(cstate, JMP(BPF_JSET));
2672 sjset_radiotap_ext_present = new_stmt(cstate, JMP(BPF_JSET));
2685 sjset_radiotap_tsft_present = new_stmt(cstate, JMP(BPF_JSET));
2699 s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
2704 sjset_tsft_datapad = new_stmt(cstate, JMP(BPF_JSET));
2716 s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
2721 sjset_notsft_datapad = new_stmt(cstate, JMP(BPF_JSET));
2732 s_roundup = new_stmt(cstate, BPF_LD|BPF_MEM);
2733 s_roundup->s.k = cstate->off_linkpl.reg;
2735 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
2738 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_IMM);
2741 s2 = new_stmt(cstate, BPF_ST);
2742 s2->s.k = cstate->off_linkpl.reg;
2756 insert_compute_vloffsets(compiler_state_t *cstate, struct block *b)
2765 if (cstate->off_linkpl.reg != -1 && cstate->off_linkhdr.is_variable &&
2766 cstate->off_linkhdr.reg == -1)
2767 cstate->off_linkhdr.reg = alloc_reg(cstate);
2780 switch (cstate->outermostlinktype) {
2783 s = gen_load_prism_llprefixlen(cstate);
2787 s = gen_load_avs_llprefixlen(cstate);
2791 s = gen_load_radiotap_llprefixlen(cstate);
2795 s = gen_load_ppi_llprefixlen(cstate);
2808 switch (cstate->outermostlinktype) {
2815 s = gen_load_802_11_header_len(cstate, s, b->stmts);
2823 if (s == NULL && cstate->is_vlan_vloffset) {
2826 if (cstate->off_linkpl.reg == -1)
2827 cstate->off_linkpl.reg = alloc_reg(cstate);
2828 if (cstate->off_linktype.reg == -1)
2829 cstate->off_linktype.reg = alloc_reg(cstate);
2831 s = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);
2833 s2 = new_stmt(cstate, BPF_ST);
2834 s2->s.k = cstate->off_linkpl.reg;
2836 s2 = new_stmt(cstate, BPF_ST);
2837 s2->s.k = cstate->off_linktype.reg;
2854 gen_ppi_dlt_check(compiler_state_t *cstate)
2859 if (cstate->linktype == DLT_PPI)
2863 s_load_dlt = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2866 b = new_block(cstate, JMP(BPF_JEQ));
2893 gen_abs_offset_varpart(compiler_state_t *cstate, bpf_abs_offset *off)
2904 off->reg = alloc_reg(cstate);
2911 s = new_stmt(cstate, BPF_LDX|BPF_MEM);
2978 gen_prevlinkhdr_check(compiler_state_t *cstate)
2982 if (cstate->is_geneve)
2983 return gen_geneve_ll_check(cstate);
2985 switch (cstate->prevlinktype) {
2995 b0 = gen_cmp(cstate, OR_PREVLINKHDR, SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
3024 gen_linktype(compiler_state_t *cstate, int proto)
3030 if (cstate->label_stack_depth > 0) {
3035 return gen_mpls_linktype(cstate, Q_IP);
3040 return gen_mpls_linktype(cstate, Q_IPV6);
3043 bpf_error(cstate, "unsupported protocol over mpls");
3048 switch (cstate->linktype) {
3055 if (!cstate->is_geneve)
3056 b0 = gen_prevlinkhdr_check(cstate);
3060 b1 = gen_ether_linktype(cstate, proto);
3075 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
3089 b0 = gen_check_802_11_data_frame(cstate);
3094 b1 = gen_llc_linktype(cstate, proto);
3104 return gen_llc_linktype(cstate, proto);
3112 return gen_llc_linktype(cstate, proto);
3119 return gen_llc_linktype(cstate, proto);
3131 b0 = gen_atmfield_code(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3132 b1 = gen_llc_linktype(cstate, proto);
3139 return gen_linux_sll_linktype(cstate, proto);
3157 return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x40, 0xF0);
3161 return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x60, 0xF0);
3164 return gen_false(cstate); /* always false */
3174 return gen_true(cstate); /* always true */
3177 return gen_false(cstate);
3186 return gen_true(cstate); /* always true */
3189 return gen_false(cstate);
3202 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
3218 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_IP);
3219 b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJC);
3221 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJNC);
3227 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,
3239 return (gen_loopback_linktype(cstate, AF_INET));
3264 if (cstate->bpf_pcap->rfile != NULL) {
3269 b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_BSD);
3270 b1 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_FREEBSD);
3272 b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_DARWIN);
3288 return (gen_loopback_linktype(cstate, 24));
3291 return (gen_loopback_linktype(cstate, AF_INET6));
3297 return gen_false(cstate);
3308 return gen_false(cstate);
3318 return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),
3321 return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),
3324 return gen_false(cstate);
3338 return gen_false(cstate);
3341 return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3345 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3347 b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3353 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3355 b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3361 return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3365 return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3374 return gen_true(cstate);
3376 return gen_false(cstate);
3392 return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0xcc);
3398 return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0x8e);
3412 b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3413 b1 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3414 b2 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3420 return gen_false(cstate);
3426 bpf_error(cstate, "Multi-link Frame Relay link-layer type filtering not implemented");
3458 return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3461 return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x55FF0000, 0xffff0000);
3464 return gen_ipnet_linktype(cstate, proto);
3467 bpf_error(cstate, "IrDA link-layer type filtering not implemented");
3470 bpf_error(cstate, "DOCSIS link-layer type filtering not implemented");
3474 bpf_error(cstate, "MTP2 link-layer type filtering not implemented");
3477 bpf_error(cstate, "ERF link-layer type filtering not implemented");
3480 bpf_error(cstate, "PFSYNC link-layer type filtering not implemented");
3483 bpf_error(cstate, "LAPD link-layer type filtering not implemented");
3489 bpf_error(cstate, "USB link-layer type filtering not implemented");
3493 bpf_error(cstate, "Bluetooth link-layer type filtering not implemented");
3497 bpf_error(cstate
3503 bpf_error(cstate, "IEEE 802.15.4 link-layer type filtering not implemented");
3506 bpf_error(cstate, "IEEE 802.16 link-layer type filtering not implemented");
3509 bpf_error(cstate, "SITA link-layer type filtering not implemented");
3512 bpf_error(cstate, "RAIF1 link-layer type filtering not implemented");
3515 bpf_error(cstate, "IPMB link-layer type filtering not implemented");
3518 bpf_error(cstate, "AX.25 link-layer type filtering not implemented");
3525 bpf_error(cstate, "NFLOG link-layer type filtering not implemented");
3534 if (cstate->off_linktype.constant_part != OFFSET_NOT_SET) {
3540 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
3545 description = pcap_datalink_val_to_description(cstate->linktype);
3547 bpf_error(cstate, "%s link-layer type filtering not implemented",
3550 bpf_error(cstate, "DLT %u link-layer type filtering not implemented",
3551 cstate->linktype);
3566 gen_snap(compiler_state_t *cstate, bpf_u_int32 orgcode, bpf_u_int32 ptype)
3578 return gen_bcmp(cstate, OR_LLC, 0, 8, snapblock);
3585 gen_llc(compiler_state_t *cstate)
3589 switch (cstate->linktype) {
3596 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
3603 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_int32)0xFFFF);
3612 b0 = gen_atmtype_abbrev(cstate, A_LLC);
3619 return gen_true(cstate);
3625 return gen_true(cstate);
3636 return gen_true(cstate);
3646 b0 = gen_check_802_11_data_frame(cstate);
3650 bpf_error(cstate, "'llc' not supported for linktype %d", cstate->linktype);
3656 gen_llc_i(compiler_state_t *cstate)
3664 b0 = gen_llc(cstate);
3670 s = gen_load_a(cstate, OR_LLC, 2, BPF_B);
3671 b1 = new_block(cstate, JMP(BPF_JSET));
3680 gen_llc_s(compiler_state_t *cstate)
3687 b0 = gen_llc(cstate);
3693 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03);
3699 gen_llc_u(compiler_state_t *cstate)
3706 b0 = gen_llc(cstate);
3712 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03);
3718 gen_llc_s_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
3725 b0 = gen_llc(cstate);
3730 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK);
3736 gen_llc_u_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
3743 b0 = gen_llc(cstate);
3748 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK);
3766 gen_llc_linktype(compiler_state_t *cstate, int proto)
3781 return gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_u_int32)
3789 return gen_cmp(cstate, OR_LLC, 0, BPF_B,
3802 return gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
3814 return gen_cmp(cstate, OR_LLC, 0, BPF_B, (bpf_int32)proto);
3829 * return gen_snap(cstate, 0x000000, proto);
3835 return gen_cmp(cstate, OR_LLC, 6, BPF_H, (bpf_int32)proto);
3841 gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
3858 b0 = gen_hostop(cstate, addr, mask, Q_SRC, proto, src_off, dst_off);
3859 b1 = gen_hostop(cstate, addr, mask, Q_DST, proto, src_off, dst_off);
3865 b0 = gen_hostop(cstate, addr, mask, Q_SRC, proto, src_off, dst_off);
3866 b1 = gen_hostop(cstate, addr, mask, Q_DST, proto, src_off, dst_off);
3871 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3875 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3879 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3883 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3887 bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3891 bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3897 b0 = gen_linktype(cstate, proto);
3898 b1 = gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, (bpf_int32)addr, mask);
3905 gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr,
3923 b0 = gen_hostop6(cstate, addr, mask, Q_SRC, proto, src_off, dst_off);
3924 b1 = gen_hostop6(cstate, addr, mask, Q_DST, proto, src_off, dst_off);
3930 b0 = gen_hostop6(cstate, addr, mask, Q_SRC, proto, src_off, dst_off);
3931 b1 = gen_hostop6(cstate, addr, mask, Q_DST, proto, src_off, dst_off);
3936 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3940 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3944 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3948 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3952 bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3956 bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3965 b1 = gen_mcmp(cstate, OR_LINKPL, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
3966 b0 = gen_mcmp(cstate, OR_LINKPL, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
3968 b0 = gen_mcmp(cstate, OR_LINKPL, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
3970 b0 = gen_mcmp(cstate, OR_LINKPL, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
3972 b0 = gen_linktype(cstate, proto);
3979 gen_ehostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
3985 return gen_bcmp(cstate, OR_LINKHDR, 6, 6, eaddr);
3988 return gen_bcmp(cstate, OR_LINKHDR, 0, 6, eaddr);
3991 b0 = gen_ehostop(cstate, eaddr, Q_SRC);
3992 b1 = gen_ehostop(cstate, eaddr, Q_DST);
3998 b0 = gen_ehostop(cstate, eaddr, Q_SRC);
3999 b1 = gen_ehostop(cstate, eaddr, Q_DST);
4004 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4008 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4012 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4016 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4020 bpf_error(cstate, "'ra' is only supported on 802.11 with 802.11 headers");
4024 bpf_error(cstate, "'ta' is only supported on 802.11 with 802.11 headers");
4035 gen_fhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4041 return gen_bcmp(cstate, OR_LINKHDR, 6 + 1 + cstate->pcap_fddipad, 6, eaddr);
4044 return gen_bcmp(cstate, OR_LINKHDR, 0 + 1 + cstate->pcap_fddipad, 6, eaddr);
4047 b0 = gen_fhostop(cstate, eaddr, Q_SRC);
4048 b1 = gen_fhostop(cstate, eaddr, Q_DST);
4054 b0 = gen_fhostop(cstate, eaddr, Q_SRC);
4055 b1 = gen_fhostop(cstate, eaddr, Q_DST);
4060 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4064 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4068 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4072 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4076 bpf_error(cstate, "'ra' is only supported on 802.11");
4080 bpf_error(cstate, "'ta' is only supported on 802.11");
4091 gen_thostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4097 return gen_bcmp(cstate, OR_LINKHDR, 8, 6, eaddr);
4100 return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
4103 b0 = gen_thostop(cstate, eaddr, Q_SRC);
4104 b1 = gen_thostop(cstate, eaddr, Q_DST);
4110 b0 = gen_thostop(cstate, eaddr, Q_SRC);
4111 b1 = gen_thostop(cstate, eaddr, Q_DST);
4116 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4120 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4124 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4128 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4132 bpf_error(cstate, "'ra' is only supported on 802.11");
4136 bpf_error(cstate, "'ta' is only supported on 802.11");
4148 gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4160 cstate->no_optimize = 1;
4190 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4191 b1 = new_block(cstate, JMP(BPF_JSET));
4198 b0 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
4205 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4206 b2 = new_block(cstate, JMP(BPF_JSET));
4214 b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4228 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4229 b1 = new_block(cstate, JMP(BPF_JSET));
4237 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4238 b2 = new_block(cstate, JMP(BPF_JSET));
4246 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4260 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4261 b1 = new_block(cstate, JMP(BPF_JSET));
4275 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4276 b2 = new_block(cstate, JMP(BPF_JSET));
4284 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4302 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4303 b1 = new_block(cstate, JMP(BPF_JSET));
4337 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4338 b1 = new_block(cstate, JMP(BPF_JSET));
4345 b0 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4352 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4353 b2 = new_block(cstate, JMP(BPF_JSET));
4361 b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4374 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4375 b1 = new_block(cstate, JMP(BPF_JSET));
4389 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4390 b2 = new_block(cstate, JMP(BPF_JSET));
4398 b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4416 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4417 b1 = new_block(cstate, JMP(BPF_JSET));
4440 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4441 b1 = new_block(cstate, JMP(BPF_JSET));
4448 b0 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4465 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4468 b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4471 b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4482 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4483 b1 = new_block(cstate, JMP(BPF_JSET));
4496 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4504 return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr));
4510 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4513 b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4516 b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4521 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4529 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4532 b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4543 b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B,
4545 b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
4550 b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
4551 b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
4557 b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
4558 b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
4572 gen_ipfchostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4578 return gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4581 return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
4584 b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
4585 b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
4591 b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
4592 b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
4597 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4601 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4605 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4609 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4613 bpf_error(cstate, "'ra' is only supported on 802.11");
4617 bpf_error(cstate, "'ta' is only supported on 802.11");
4643 gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir)
4663 b0 = gen_dnhostop(cstate, addr, Q_SRC);
4664 b1 = gen_dnhostop(cstate, addr, Q_DST);
4671 b0 = gen_dnhostop(cstate, addr, Q_SRC);
4672 b1 = gen_dnhostop(cstate, addr, Q_DST);
4677 bpf_error(cstate, "ISO host filtering not implemented");
4682 b0 = gen_linktype(cstate, ETHERTYPE_DN);
4684 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
4686 b1 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_lh,
4690 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
4691 b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4695 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
4697 b2 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4701 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
4702 b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4706 /* Combine with test for cstate->linktype */
4717 gen_mpls_linktype(compiler_state_t *cstate, int proto)
4725 b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
4727 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x40, 0xf0);
4733 b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
4735 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x60, 0xf0);
4745 gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
4759 b0 = gen_host(cstate, addr, mask, Q_IP, dir, type);
4764 if (cstate->label_stack_depth == 0) {
4765 b1 = gen_host(cstate, addr, mask, Q_ARP, dir, type);
4767 b0 = gen_host(cstate, addr, mask, Q_RARP, dir, type);
4773 return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_IP, 12, 16);
4776 return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
4779 return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_ARP, 14, 24);
4782 bpf_error(cstate, "'tcp' modifier applied to %s", typestr);
4785 bpf_error(cstate, "'sctp' modifier applied to %s", typestr);
4788 bpf_error(cstate, "'udp' modifier applied to %s", typestr);
4791 bpf_error(cstate, "'icmp' modifier applied to %s", typestr);
4794 bpf_error(cstate, "'igmp' modifier applied to %s", typestr);
4797 bpf_error(cstate
4800 bpf_error(cstate, "'pim' modifier applied to %s", typestr);
4803 bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);
4806 bpf_error(cstate, "'carp' modifier applied to %s", typestr);
4809 bpf_error(cstate, "ATALK host filtering not implemented");
4812 bpf_error(cstate, "AARP host filtering not implemented");
4815 return gen_dnhostop(cstate, addr, dir);
4818 bpf_error(cstate, "SCA host filtering not implemented");
4821 bpf_error(cstate, "LAT host filtering not implemented");
4824 bpf_error(cstate, "MOPDL host filtering not implemented");
4827 bpf_error(cstate, "MOPRC host filtering not implemented");
4830 bpf_error(cstate, "'ip6' modifier applied to ip host");
4833 bpf_error(cstate, "'icmp6' modifier applied to %s", typestr);
4836 bpf_error(cstate, "'ah' modifier applied to %s", typestr);
4839 bpf_error(cstate, "'esp' modifier applied to %s", typestr);
4842 bpf_error(cstate, "ISO host filtering not implemented");
4845 bpf_error(cstate, "'esis' modifier applied to %s", typestr);
4848 bpf_error(cstate, "'isis' modifier applied to %s", typestr);
4851 bpf_error(cstate, "'clnp' modifier applied to %s", typestr);
4854 bpf_error(cstate, "'stp' modifier applied to %s", typestr);
4857 bpf_error(cstate, "IPX host filtering not implemented");
4860 bpf_error(cstate, "'netbeui' modifier applied to %s", typestr);
4863 bpf_error(cstate, "'radio' modifier applied to %s", typestr);
4873 gen_host6(compiler_state_t *cstate, struct in6_addr *addr,
4886 return gen_host6(cstate, addr, mask, Q_IPV6, dir, type);
4889 bpf_error(cstate, "link-layer modifier applied to ip6 %s", typestr);
4892 bpf_error(cstate, "'ip' modifier applied to ip6 %s", typestr);
4895 bpf_error(cstate, "'rarp' modifier applied to ip6 %s", typestr);
4898 bpf_error(cstate, "'arp' modifier applied to ip6 %s", typestr);
4901 bpf_error(cstate, "'sctp' modifier applied to %s", typestr);
4904 bpf_error(cstate, "'tcp' modifier applied to %s", typestr);
4907 bpf_error(cstate, "'udp' modifier applied to %s", typestr);
4910 bpf_error(cstate, "'icmp' modifier applied to %s", typestr);
4913 bpf_error(cstate, "'igmp' modifier applied to %s", typestr);
4916 bpf_error(cstate, "'igrp' modifier applied to %s", typestr);
4919 bpf_error(cstate, "'pim' modifier applied to %s", typestr);
4922 bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);
4925 bpf_error(cstate, "'carp' modifier applied to %s", typestr);
4928 bpf_error(cstate, "ATALK host filtering not implemented");
4931 bpf_error(cstate, "AARP host filtering not implemented");
4934 bpf_error(cstate, "'decnet' modifier applied to ip6 %s", typestr);
4937 bpf_error(cstate, "SCA host filtering not implemented");
4940 bpf_error(cstate, "LAT host filtering not implemented");
4943 bpf_error(cstate, "MOPDL host filtering not implemented");
4946 bpf_error(cstate, "MOPRC host filtering not implemented");
4949 return gen_hostop6(cstate, addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
4952 bpf_error(cstate, "'icmp6' modifier applied to %s", typestr);
4955 bpf_error(cstate, "'ah' modifier applied to %s", typestr);
4958 bpf_error(cstate, "'esp' modifier applied to %s", typestr);
4961 bpf_error(cstate, "ISO host filtering not implemented");
4964 bpf_error(cstate, "'esis' modifier applied to %s", typestr);
4967 bpf_error(cstate, "'isis' modifier applied to %s", typestr);
4970 bpf_error(cstate, "'clnp' modifier applied to %s", typestr);
4973 bpf_error(cstate, "'stp' modifier applied to %s", typestr);
4976 bpf_error(cstate, "IPX host filtering not implemented");
4979 bpf_error(cstate, "'netbeui' modifier applied to %s", typestr);
4982 bpf_error(cstate, "'radio' modifier applied to %s", typestr);
4993 gen_gateway(compiler_state_t *cstate, const u_char *eaddr,
5001 bpf_error(cstate, "direction applied to 'gateway'");
5008 switch (cstate->linktype) {
5012 b1 = gen_prevlinkhdr_check(cstate);
5013 b0 = gen_ehostop(cstate, eaddr, Q_OR);
5018 b0 = gen_fhostop(cstate, eaddr, Q_OR);
5021 b0 = gen_thostop(cstate, eaddr, Q_OR);
5028 b0 = gen_wlanhostop(cstate, eaddr, Q_OR);
5033 * LANE, cstate->linktype would have been set to
5036 bpf_error(cstate,
5040 b0 = gen_ipfchostop(cstate, eaddr, Q_OR);
5043 bpf_error(cstate,
5060 tmp = gen_host(cstate,
5093 bpf_error(cstate, "illegal modifier of 'gateway'");
5099 gen_proto_abbrev(compiler_state_t *cstate, int proto)
5107 b1 = gen_proto(cstate, IPPROTO_SCTP, Q_IP, Q_DEFAULT);
5108 b0 = gen_proto(cstate, IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
5113 b1 = gen_proto(cstate, IPPROTO_TCP, Q_IP, Q_DEFAULT);
5114 b0 = gen_proto(cstate, IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
5119 b1 = gen_proto(cstate, IPPROTO_UDP, Q_IP, Q_DEFAULT);
5120 b0 = gen_proto(cstate, IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
5125 b1 = gen_proto(cstate, IPPROTO_ICMP, Q_IP, Q_DEFAULT);
5133 b1 = gen_proto(cstate, IPPROTO_IGMP, Q_IP, Q_DEFAULT);
5140 b1 = gen_proto(cstate, IPPROTO_IGRP, Q_IP, Q_DEFAULT);
5148 b1 = gen_proto(cstate, IPPROTO_PIM, Q_IP, Q_DEFAULT);
5149 b0 = gen_proto(cstate, IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
5158 b1 = gen_proto(cstate, IPPROTO_VRRP, Q_IP, Q_DEFAULT);
5166 b1 = gen_proto(cstate, IPPROTO_CARP, Q_IP, Q_DEFAULT);
5170 b1 = gen_linktype(cstate, ETHERTYPE_IP);
5174 b1 = gen_linktype(cstate, ETHERTYPE_ARP);
5178 b1 = gen_linktype(cstate, ETHERTYPE_REVARP);
5182 bpf_error(cstate, "link layer applied in wrong context");
5185 b1 = gen_linktype(cstate, ETHERTYPE_ATALK);
5189 b1 = gen_linktype(cstate, ETHERTYPE_AARP);
5193 b1 = gen_linktype(cstate, ETHERTYPE_DN);
5197 b1 = gen_linktype(cstate, ETHERTYPE_SCA);
5201 b1 = gen_linktype(cstate, ETHERTYPE_LAT);
5205 b1 = gen_linktype(cstate, ETHERTYPE_MOPDL);
5209 b1 = gen_linktype(cstate, ETHERTYPE_MOPRC);
5213 b1 = gen_linktype(cstate, ETHERTYPE_IPV6);
5220 b1 = gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
5227 b1 = gen_proto(cstate, IPPROTO_AH, Q_IP, Q_DEFAULT);
5228 b0 = gen_proto(cstate, IPPROTO_AH, Q_IPV6, Q_DEFAULT);
5236 b1 = gen_proto(cstate, IPPROTO_ESP, Q_IP, Q_DEFAULT);
5237 b0 = gen_proto(cstate, IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
5242 b1 = gen_linktype(cstate, LLCSAP_ISONS);
5246 b1 = gen_proto(cstate, ISO9542_ESIS, Q_ISO, Q_DEFAULT);
5250 b1 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5254 b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
5255 b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
5257 b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
5259 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5261 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5266 b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
5267 b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
5269 b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
5271 b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5273 b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5278 b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
5279 b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
5281 b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
5286 b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
5287 b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
5292 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5293 b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5295 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5297 b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5302 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5303 b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5308 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5309 b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5314 b1 = gen_proto(cstate, ISO8473_CLNP, Q_ISO, Q_DEFAULT);
5318 b1 = gen_linktype(cstate, LLCSAP_8021D);
5322 b1 = gen_linktype(cstate, LLCSAP_IPX);
5326 b1 = gen_linktype(cstate, LLCSAP_NETBEUI);
5330 bpf_error(cstate, "'radio' is not a valid protocol type");
5339 gen_ipfrag(compiler_state_t *cstate)
5345 s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H);
5346 b = new_block(cstate, JMP(BPF_JSET));
5364 gen_portatom(compiler_state_t *cstate, int off, bpf_int32 v)
5366 return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v);
5370 gen_portatom6(compiler_state_t *cstate, int off, bpf_int32 v)
5372 return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v);
5376 gen_portop(compiler_state_t *cstate, int port, int proto, int dir)
5381 tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, (bpf_int32)proto);
5382 b0 = gen_ipfrag(cstate);
5387 b1 = gen_portatom(cstate, 0, (bpf_int32)port);
5391 b1 = gen_portatom(cstate, 2, (bpf_int32)port);
5396 tmp = gen_portatom(cstate, 0, (bpf_int32)port);
5397 b1 = gen_portatom(cstate, 2, (bpf_int32)port);
5402 tmp = gen_portatom(cstate, 0, (bpf_int32)port);
5403 b1 = gen_portatom(cstate, 2, (bpf_int32)port);
5416 gen_port(compiler_state_t *cstate, int port, int ip_proto, int dir)
5437 b0 = gen_linktype(cstate, ETHERTYPE_IP);
5443 b1 = gen_portop(cstate, port, ip_proto, dir);
5447 tmp = gen_portop(cstate, port, IPPROTO_TCP, dir);
5448 b1 = gen_portop(cstate, port, IPPROTO_UDP, dir);
5450 tmp = gen_portop(cstate, port, IPPROTO_SCTP, dir);
5462 gen_portop6(compiler_state_t *cstate, int port, int proto, int dir)
5468 b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, (bpf_int32)proto);
5472 b1 = gen_portatom6(cstate, 0, (bpf_int32)port);
5476 b1 = gen_portatom6(cstate, 2, (bpf_int32)port);
5481 tmp = gen_portatom6(cstate, 0, (bpf_int32)port);
5482 b1 = gen_portatom6(cstate, 2, (bpf_int32)port);
5487 tmp = gen_portatom6(cstate, 0, (bpf_int32)port);
5488 b1 = gen_portatom6(cstate, 2, (bpf_int32)port);
5501 gen_port6(compiler_state_t *cstate, int port, int ip_proto, int dir)
5506 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
5512 b1 = gen_portop6(cstate, port, ip_proto, dir);
5516 tmp = gen_portop6(cstate, port, IPPROTO_TCP, dir);
5517 b1 = gen_portop6(cstate, port, IPPROTO_UDP, dir);
5519 tmp = gen_portop6(cstate, port, IPPROTO_SCTP, dir);
5532 gen_portrangeatom(compiler_state_t *cstate, int off, bpf_int32 v1,
5548 b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, v1);
5549 b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, v2);
5557 gen_portrangeop(compiler_state_t *cstate, int port1, int port2, int proto,
5563 tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, (bpf_int32)proto);
5564 b0 = gen_ipfrag(cstate);
5569 b1 = gen_portrangeatom(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
5573 b1 = gen_portrangeatom(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
5578 tmp = gen_portrangeatom(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
5579 b1 = gen_portrangeatom(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
5584 tmp = gen_portrangeatom(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
5585 b1 = gen_portrangeatom(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
5598 gen_portrange(compiler_state_t *cstate, int port1, int port2, int ip_proto,
5604 b0 = gen_linktype(cstate, ETHERTYPE_IP);
5610 b1 = gen_portrangeop(cstate, port1, port2, ip_proto, dir);
5614 tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_TCP, dir);
5615 b1 = gen_portrangeop(cstate, port1, port2, IPPROTO_UDP, dir);
5617 tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_SCTP, dir);
5629 gen_portrangeatom6(compiler_state_t *cstate, int off, bpf_int32 v1,
5645 b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, v1);
5646 b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, v2);
5654 gen_portrangeop6(compiler_state_t *cstate, int port1, int port2, int proto,
5661 b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, (bpf_int32)proto);
5665 b1 = gen_portrangeatom6(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
5669 b1 = gen_portrangeatom6(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
5674 tmp = gen_portrangeatom6(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
5675 b1 = gen_portrangeatom6(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
5680 tmp = gen_portrangeatom6(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
5681 b1 = gen_portrangeatom6(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
5694 gen_portrange6(compiler_state_t *cstate, int port1, int port2, int ip_proto,
5700 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
5706 b1 = gen_portrangeop6(cstate, port1, port2, ip_proto, dir);
5710 tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_TCP, dir);
5711 b1 = gen_portrangeop6(cstate, port1, port2, IPPROTO_UDP, dir);
5713 tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_SCTP, dir);
5725 lookup_proto(compiler_state_t *cstate, const char *name, int proto)
5736 bpf_error(cstate, "unknown ip proto '%s'", name);
5740 /* XXX should look up h/w protocol type based on cstate->linktype */
5745 bpf_error(cstate, "unknown ether proto '%s'", name);
5757 bpf_error(cstate, "unknown osi proto '%s'", name);
5776 gen_protochain(compiler_state_t *cstate, int v, int proto, int dir)
5779 return gen_proto(cstate, v, proto, dir);
5786 int reg2 = alloc_reg(cstate);
5796 b0 = gen_protochain(cstate, v, Q_IP, dir);
5797 b = gen_protochain(cstate, v, Q_IPV6, dir);
5801 bpf_error(cstate, "bad protocol applied for 'protochain'");
5816 if (cstate->off_linkpl.is_variable)
5817 bpf_error(cstate, "'protochain' not supported with variable length headers");
5819 cstate->no_optimize = 1; /* this code is not compatible with optimizer yet */
5827 s[i] = new_stmt(cstate, 0); /*dummy*/
5832 b0 = gen_linktype(cstate, ETHERTYPE_IP);
5835 s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
5836 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 9;
5839 s[i] = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
5840 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
5845 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
5848 s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
5849 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 6;
5852 s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM);
5858 bpf_error(cstate, "unsupported proto to gen_protochain");
5864 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
5875 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
5888 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
5895 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
5901 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
5907 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
5924 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
5925 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
5928 s[i] = new_stmt(cstate, BPF_ST);
5932 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
5933 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1;
5936 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
5940 s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
5944 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
5948 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
5951 s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
5956 s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
5966 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
5975 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
5990 s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
5993 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
5994 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
5997 s[i] = new_stmt(cstate, BPF_ST);
6001 s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
6004 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6008 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6011 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6012 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6015 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6019 s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
6023 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6026 s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
6031 s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
6037 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6055 b = new_block(cstate, JMP(BPF_JEQ));
6059 free_reg(cstate, reg2);
6067 gen_check_802_11_data_frame(compiler_state_t *cstate)
6076 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
6077 b0 = new_block(cstate, JMP(BPF_JSET));
6081 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
6082 b1 = new_block(cstate, JMP(BPF_JSET));
6102 gen_proto(compiler_state_t *cstate, int v, int proto, int dir)
6110 bpf_error(cstate, "direction applied to 'proto'");
6114 b0 = gen_proto(cstate, v, Q_IP, dir);
6115 b1 = gen_proto(cstate, v, Q_IPV6, dir);
6135 b0 = gen_linktype(cstate, ETHERTYPE_IP);
6137 b1 = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, (bpf_int32)v);
6139 b1 = gen_protochain(cstate, v, Q_IP);
6145 switch (cstate->linktype) {
6150 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6166 return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v);
6175 b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS);
6177 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, (long)v);
6182 b0 = gen_linktype(cstate, LLCSAP_ISONS);
6183 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, (long)v);
6189 b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
6194 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, (long)v);
6199 cstate, "arp does not encapsulate another protocol");
6203 bpf_error(cstate, "rarp does not encapsulate another protocol");
6207 bpf_error(cstate, "atalk encapsulation is not specifiable");
6211 bpf_error(cstate, "decnet encapsulation is not specifiable");
6215 bpf_error(cstate, "sca does not encapsulate another protocol");
6219 bpf_error(cstate, "lat does not encapsulate another protocol");
6223 bpf_error(cstate, "moprc does not encapsulate another protocol");
6227 bpf_error(cstate, "mopdl does not encapsulate another protocol");
6231 return gen_linktype(cstate, v);
6234 bpf_error(cstate, "'udp proto' is bogus");
6238 bpf_error(cstate, "'tcp proto' is bogus");
6242 bpf_error(cstate, "'sctp proto' is bogus");
6246 bpf_error(cstate, "'icmp proto' is bogus");
6250 bpf_error(cstate, "'igmp proto' is bogus");
6254 bpf_error(cstate, "'igrp proto' is bogus");
6258 bpf_error(cstate, "'pim proto' is bogus");
6262 bpf_error(cstate, "'vrrp proto' is bogus");
6266 bpf_error(cstate, "'carp proto' is bogus");
6270 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6276 b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, IPPROTO_FRAGMENT);
6277 b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, (bpf_int32)v);
6279 b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, (bpf_int32)v);
6282 b1 = gen_protochain(cstate, v, Q_IPV6);
6288 bpf_error(cstate, "'icmp6 proto' is bogus");
6291 bpf_error(cstate, "'ah proto' is bogus");
6294 bpf_error(cstate, "'ah proto' is bogus");
6297 bpf_error(cstate, "'stp proto' is bogus");
6300 bpf_error(cstate, "'ipx proto' is bogus");
6303 bpf_error(cstate, "'netbeui proto' is bogus");
6306 bpf_error(cstate, "'radio proto' is bogus");
6316 gen_scode(compiler_state_t *cstate, const char *name, struct qual q)
6339 bpf_error(cstate, "unknown network '%s'", name);
6346 return gen_host(cstate, addr, mask, proto, dir, q.addr);
6351 switch (cstate->linktype) {
6358 bpf_error(cstate,
6360 tmp = gen_prevlinkhdr_check(cstate);
6361 b = gen_ehostop(cstate, eaddr, dir);
6370 bpf_error(cstate,
6372 b = gen_fhostop(cstate, eaddr, dir);
6379 bpf_error(cstate,
6381 b = gen_thostop(cstate, eaddr, dir);
6392 bpf_error(cstate,
6394 b = gen_wlanhostop(cstate, eaddr, dir);
6401 bpf_error(cstate,
6403 b = gen_ipfchostop(cstate, eaddr, dir);
6408 bpf_error(cstate, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6414 bpf_error(cstate, "unknown decnet host name '%s'\n", name);
6416 bpf_error(cstate, "decnet name support not included, '%s' cannot be translated\n",
6424 return (gen_host(cstate, dn_addr, 0, proto, dir, q.addr));
6431 bpf_error(cstate, "unknown host '%s'", name);
6432 cstate->ai = res;
6438 if (cstate->off_linktype.constant_part == OFFSET_NOT_SET &&
6455 tmp = gen_host(cstate, ntohl(sin4->sin_addr.s_addr),
6465 tmp = gen_host6(cstate, &sin6->sin6_addr,
6476 cstate->ai = NULL;
6479 bpf_error(cstate, "unknown host '%s'%s", name,
6490 bpf_error(cstate, "illegal qualifier of 'port'");
6492 bpf_error(cstate, "unknown port '%s'", name);
6495 bpf_error(cstate, "port '%s' is tcp", name);
6497 bpf_error(cstate, "port '%s' is sctp", name);
6504 bpf_error(cstate, "port '%s' is udp", name);
6507 bpf_error(cstate, "port '%s' is sctp", name);
6514 bpf_error(cstate, "port '%s' is udp", name);
6517 bpf_error(cstate, "port '%s' is tcp", name);
6523 bpf_error(cstate, "illegal port number %d < 0", port);
6525 bpf_error(cstate, "illegal port number %d > 65535", port);
6526 b = gen_port(cstate, port, real_proto, dir);
6527 gen_or(gen_port6(cstate, port, real_proto, dir), b);
6533 bpf_error(cstate, "illegal qualifier of 'portrange'");
6535 bpf_error(cstate, "unknown port in range '%s'", name);
6538 bpf_error(cstate, "port in range '%s' is tcp", name);
6540 bpf_error(cstate, "port in range '%s' is sctp", name);
6547 bpf_error(cstate, "port in range '%s' is udp", name);
6549 bpf_error(cstate, "port in range '%s' is sctp", name);
6556 bpf_error(cstate, "port in range '%s' is udp", name);
6558 bpf_error(cstate, "port in range '%s' is tcp", name);
6564 bpf_error(cstate, "illegal port number %d < 0", port1);
6566 bpf_error(cstate, "illegal port number %d > 65535", port1);
6568 bpf_error(cstate, "illegal port number %d < 0", port2);
6570 bpf_error(cstate, "illegal port number %d > 65535", port2);
6572 b = gen_portrange(cstate, port1, port2, real_proto, dir);
6573 gen_or(gen_portrange6(cstate, port1, port2, real_proto, dir), b);
6580 bpf_error(cstate, "unknown ether host: %s", name);
6583 cstate->ai = res;
6585 bpf_error(cstate, "unknown host '%s'", name);
6586 b = gen_gateway(cstate, eaddr, res, proto, dir);
6587 cstate->ai = NULL;
6590 bpf_error(cstate, "unknown host '%s'", name);
6593 bpf_error(cstate, "'gateway' not supported in this configuration");
6597 real_proto = lookup_proto(cstate, name, proto);
6599 return gen_proto(cstate, real_proto, proto, dir);
6601 bpf_error(cstate, "unknown protocol: %s", name);
6604 real_proto = lookup_proto(cstate, name, proto);
6606 return gen_protochain(cstate, real_proto, proto, dir);
6608 bpf_error(cstate, "unknown protocol: %s", name);
6611 syntax(cstate);
6619 gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2,
6634 bpf_error(cstate, "non-network bits set in \"%s mask %s\"",
6639 bpf_error(cstate, "mask length must be <= 32");
6649 bpf_error(cstate, "non-network bits set in \"%s/%d\"",
6656 return gen_host(cstate, n, m, q.proto, q.dir, q.addr);
6659 bpf_error(cstate, "Mask syntax for networks only");
6666 gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q)
6678 bpf_error(cstate, "malformed decnet address '%s'", s);
6688 return gen_host(cstate, v, 0, proto, dir, q.addr);
6690 bpf_error(cstate, "illegal link layer address");
6704 return gen_host(cstate, v, mask, proto, dir, q.addr);
6717 bpf_error(cstate, "illegal qualifier of 'port'");
6720 bpf_error(cstate, "illegal port number %u > 65535", v);
6724 b = gen_port(cstate, (int)v, proto, dir);
6725 gen_or(gen_port6(cstate, (int)v, proto, dir), b);
6739 bpf_error(cstate, "illegal qualifier of 'portrange'");
6742 bpf_error(cstate, "illegal port number %u > 65535", v);
6746 b = gen_portrange(cstate, (int)v, (int)v, proto, dir);
6747 gen_or(gen_portrange6(cstate, (int)v, (int)v, proto, dir), b);
6752 bpf_error(cstate, "'gateway' requires a name");
6756 return gen_proto(cstate, (int)v, proto, dir);
6759 return gen_protochain(cstate, (int)v, proto, dir);
6762 syntax(cstate);
6774 gen_mcode6(compiler_state_t *cstate, const char *s1, const char *s2,
6784 bpf_error(cstate, "no mask %s supported", s2);
6788 bpf_error(cstate, "invalid ip6 address %s", s1);
6789 cstate->ai = res;
6791 bpf_error(cstate, "%s resolved to multiple address", s1);
6795 bpf_error(cstate, "mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
6807 bpf_error(cstate, "non-network bits set in \"%s/%d\"", s1, masklen);
6815 bpf_error(cstate, "Mask syntax for networks only");
6819 b = gen_host6(cstate, addr, &mask, q.proto, q.dir, q.addr);
6820 cstate->ai = NULL;
6825 bpf_error(cstate, "invalid qualifier against IPv6 address");
6832 gen_ecode(compiler_state_t *cstate, const u_char *eaddr, struct qual q)
6837 switch (cstate->linktype) {
6841 tmp = gen_prevlinkhdr_check(cstate);
6842 b = gen_ehostop(cstate, eaddr, (int)q.dir);
6847 return gen_fhostop(cstate, eaddr, (int)q.dir);
6849 return gen_thostop(cstate, eaddr, (int)q.dir);
6855 return gen_wlanhostop(cstate, eaddr, (int)q.dir);
6857 return gen_ipfchostop(cstate, eaddr, (int)q.dir);
6859 bpf_error(cstate, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6863 bpf_error(cstate, "ethernet address used in non-ether expression");
6880 xfer_to_x(compiler_state_t *cstate, struct arth *a)
6884 s = new_stmt(cstate, BPF_LDX|BPF_MEM);
6890 xfer_to_a(compiler_state_t *cstate, struct arth *a)
6894 s = new_stmt(cstate, BPF_LD|BPF_MEM);
6907 gen_load(compiler_state_t *cstate, int proto, struct arth *inst, int size)
6911 int regno = alloc_reg(cstate);
6913 free_reg(cstate, inst->regno);
6917 bpf_error(cstate, "data size must be 1, 2, or 4");
6933 bpf_error(cstate, "unsupported index operation");
6941 if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS &&
6942 cstate->linktype != DLT_IEEE802_11_RADIO &&
6943 cstate->linktype != DLT_PRISM_HEADER)
6944 bpf_error(cstate, "radio information not present in capture");
6950 s = xfer_to_x(cstate, inst);
6955 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size);
6972 s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr);
6984 sappend(s, xfer_to_a(cstate, inst));
6985 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
6986 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
6988 s = xfer_to_x(cstate, inst);
6997 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size);
6998 tmp->s.k = cstate->off_linkhdr.constant_part;
7017 * cstate->off_nl_nosnap?
7019 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
7031 sappend(s, xfer_to_a(cstate, inst));
7032 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7033 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7035 s = xfer_to_x(cstate, inst);
7044 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size);
7045 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
7053 b = gen_proto_abbrev(cstate, proto);
7077 * cstate->off_nl_nosnap?
7082 s = gen_loadx_iphdrlen(cstate);
7098 sappend(s, xfer_to_a(cstate, inst));
7099 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7100 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7101 sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size));
7102 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
7111 gen_and(gen_proto_abbrev(cstate, proto), b = gen_ipfrag(cstate));
7114 gen_and(gen_proto_abbrev(cstate, Q_IP), b);
7122 b = gen_proto_abbrev(cstate, Q_IPV6);
7131 b = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, 58);
7138 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
7149 sappend(s, xfer_to_a(cstate, inst));
7150 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7151 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7153 s = xfer_to_x(cstate, inst);
7163 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size);
7164 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40;
7172 s = new_stmt(cstate, BPF_ST);
7180 gen_relation(compiler_state_t *cstate, int code, struct arth *a0,
7186 s0 = xfer_to_x(cstate, a1);
7187 s1 = xfer_to_a(cstate, a0);
7189 s2 = new_stmt(cstate, BPF_ALU|BPF_SUB|BPF_X);
7190 b = new_block(cstate, JMP(code));
7194 b = new_block(cstate, BPF_JMP|code|BPF_X);
7204 free_reg(cstate, a0->regno);
7205 free_reg(cstate, a1->regno);
7224 gen_loadlen(compiler_state_t *cstate)
7226 int regno = alloc_reg(cstate);
7227 struct arth *a = (struct arth *)newchunk(cstate, sizeof(*a));
7230 s = new_stmt(cstate, BPF_LD|BPF_LEN);
7231 s->next = new_stmt(cstate, BPF_ST);
7240 gen_loadi(compiler_state_t *cstate, int val)
7246 a = (struct arth *)newchunk(cstate, sizeof(*a));
7248 reg = alloc_reg(cstate);
7250 s = new_stmt(cstate, BPF_LD|BPF_IMM);
7252 s->next = new_stmt(cstate, BPF_ST);
7261 gen_neg(compiler_state_t *cstate, struct arth *a)
7265 s = xfer_to_a(cstate, a);
7267 s = new_stmt(cstate, BPF_ALU|BPF_NEG);
7270 s = new_stmt(cstate, BPF_ST);
7278 gen_arth(compiler_state_t *cstate, int code, struct arth *a0,
7289 bpf_error(cstate, "division by zero");
7292 bpf_error(cstate, "modulus by zero");
7294 s0 = xfer_to_x(cstate, a1);
7295 s1 = xfer_to_a(cstate, a0);
7296 s2 = new_stmt(cstate, BPF_ALU|BPF_X|code);
7303 free_reg(cstate, a0->regno);
7304 free_reg(cstate, a1->regno);
7306 s0 = new_stmt(cstate, BPF_ST);
7307 a0->regno = s0->s.k = alloc_reg(cstate);
7317 init_regs(compiler_state_t *cstate)
7319 cstate->curreg = 0;
7320 memset(cstate->regused, 0, sizeof cstate->regused);
7327 alloc_reg(compiler_state_t *cstate)
7332 if (cstate->regused[cstate->curreg])
7333 cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS;
7335 cstate->regused[cstate->curreg] = 1;
7336 return cstate->curreg;
7339 bpf_error(cstate, "too many registers needed to evaluate expression");
7348 free_reg(compiler_state_t *cstate, int n)
7350 cstate->regused[n] = 0;
7354 gen_len(compiler_state_t *cstate, int jmp, int n)
7359 s = new_stmt(cstate, BPF_LD|BPF_LEN);
7360 b = new_block(cstate, JMP(jmp));
7368 gen_greater(compiler_state_t *cstate, int n)
7370 return gen_len(cstate, BPF_JGE, n);
7377 gen_less(compiler_state_t *cstate, int n)
7381 b = gen_len(cstate, BPF_JGT, n);
7398 gen_byteop(compiler_state_t *cstate, int op, int idx, int val)
7408 return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, (bpf_int32)val);
7411 b = gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, (bpf_int32)val);
7415 b = gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, (bpf_int32)val);
7419 s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K);
7423 s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
7427 b = new_block(cstate, JMP(BPF_JEQ));
7437 gen_broadcast(compiler_state_t *cstate, int proto)
7447 switch (cstate->linktype) {
7450 return gen_ahostop(cstate, abroadcast, Q_DST);
7454 b1 = gen_prevlinkhdr_check(cstate);
7455 b0 = gen_ehostop(cstate, ebroadcast, Q_DST);
7460 return gen_fhostop(cstate, ebroadcast, Q_DST);
7462 return gen_thostop(cstate, ebroadcast, Q_DST);
7468 return gen_wlanhostop(cstate, ebroadcast, Q_DST);
7470 return gen_ipfchostop(cstate, ebroadcast, Q_DST);
7472 bpf_error(cstate, "not a broadcast link");
7482 if (cstate->netmask == PCAP_NETMASK_UNKNOWN)
7483 bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported");
7484 b0 = gen_linktype(cstate, ETHERTYPE_IP);
7485 hostmask = ~cstate->netmask;
7486 b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, (bpf_int32)0, hostmask);
7487 b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W,
7493 bpf_error(cstate, "only link-layer/IP broadcast filters supported");
7502 gen_mac_multicast(compiler_state_t *cstate, int offset)
7508 s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B);
7509 b0 = new_block(cstate, JMP(BPF_JSET));
7516 gen_multicast(compiler_state_t *cstate, int proto)
7525 switch (cstate
7529 return gen_ahostop(cstate, abroadcast, Q_DST);
7533 b1 = gen_prevlinkhdr_check(cstate);
7535 b0 = gen_mac_multicast(cstate, 0);
7546 return gen_mac_multicast(cstate, 1);
7549 return gen_mac_multicast(cstate, 2);
7576 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
7577 b1 = new_block(cstate, JMP(BPF_JSET));
7584 b0 = gen_mac_multicast(cstate, 16);
7591 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
7592 b2 = new_block(cstate, JMP(BPF_JSET));
7600 b1 = gen_mac_multicast(cstate, 4);
7613 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
7614 b1 = new_block(cstate, JMP(BPF_JSET));
7628 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
7629 b2 = new_block(cstate, JMP(BPF_JSET));
7637 b1 = gen_mac_multicast(cstate, 4);
7655 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
7656 b1 = new_block(cstate, JMP(BPF_JSET));
7668 b0 = gen_mac_multicast(cstate, 2);
7677 b0 = gen_linktype(cstate, ETHERTYPE_IP);
7678 b1 = gen_cmp_ge(cstate, OR_LINKPL, 16, BPF_B, (bpf_int32)224);
7683 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
7684 b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, (bpf_int32)255);
7688 bpf_error(cstate, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7702 gen_inbound(compiler_state_t *cstate, int dir)
7709 switch (cstate->linktype) {
7711 b0 = gen_relation(cstate, BPF_JEQ,
7712 gen_load(cstate, Q_LINK, gen_loadi(cstate, 0), 1),
7713 gen_loadi(cstate, 0),
7720 b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_OUTBOUND);
7723 b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_INBOUND);
7729 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING);
7738 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B,
7746 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_OUT);
7749 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_IN);
7780 b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 0, 0x01);
7783 b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 1, 0x01);
7810 if (cstate->bpf_pcap->rfile != NULL) {
7812 bpf_error(cstate, "inbound/outbound not supported on linktype %d when reading savefiles",
7813 cstate->linktype);
7818 b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
7825 bpf_error(cstate, "inbound/outbound not supported on linktype %d",
7826 cstate->linktype);
7836 gen_pf_ifname(compiler_state_t *cstate, const char *ifname)
7841 if (cstate->linktype != DLT_PFLOG) {
7842 bpf_error(cstate, "ifname supported only on PF linktype");
7848 bpf_error(cstate, "ifname interface names can only be %d characters",
7852 b0 = gen_bcmp(cstate, OR_LINKHDR, off, strlen(ifname), (const u_char *)ifname);
7858 gen_pf_ruleset(compiler_state_t *cstate, char *ruleset)
7862 if (cstate->linktype != DLT_PFLOG) {
7863 bpf_error(cstate, "ruleset supported only on PF linktype");
7868 bpf_error(cstate, "ruleset names can only be %ld characters",
7873 b0 = gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset),
7880 gen_pf_rnr(compiler_state_t *cstate, int rnr)
7884 if (cstate->linktype != DLT_PFLOG) {
7885 bpf_error(cstate, "rnr supported only on PF linktype");
7889 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W,
7896 gen_pf_srnr(compiler_state_t *cstate, int srnr)
7900 if (cstate->linktype != DLT_PFLOG) {
7901 bpf_error(cstate, "srnr supported only on PF linktype");
7905 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W,
7912 gen_pf_reason(compiler_state_t *cstate, int reason)
7916 if (cstate->linktype != DLT_PFLOG) {
7917 bpf_error(cstate, "reason supported only on PF linktype");
7921 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B,
7928 gen_pf_action(compiler_state_t *cstate, int action)
7932 if (cstate->linktype != DLT_PFLOG) {
7933 bpf_error(cstate, "action supported only on PF linktype");
7937 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B,
7943 gen_pf_ifname(compiler_state_t *cstate, const char *ifname _U_)
7945 bpf_error(cstate, "libpcap was compiled without pf support");
7950 gen_pf_ruleset(compiler_state_t *cstate, char *ruleset _U_)
7952 bpf_error(cstate, "libpcap was compiled on a machine without pf support");
7957 gen_pf_rnr(compiler_state_t *cstate, int rnr _U_)
7959 bpf_error(cstate, "libpcap was compiled on a machine without pf support");
7964 gen_pf_srnr(compiler_state_t *cstate, int srnr _U_)
7966 bpf_error(cstate, "libpcap was compiled on a machine without pf support");
7971 gen_pf_reason(compiler_state_t *cstate, int reason _U_)
7973 bpf_error(cstate, "libpcap was compiled on a machine without pf support");
7978 gen_pf_action(compiler_state_t *cstate, int action _U_)
7980 bpf_error(cstate, "libpcap was compiled on a machine without pf support");
7987 gen_p80211_type(compiler_state_t *cstate, int type, int mask)
7991 switch (cstate->linktype) {
7997 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, (bpf_int32)type,
8002 bpf_error(cstate, "802.11 link-layer types supported only on 802.11");
8010 gen_p80211_fcdir(compiler_state_t *cstate, int fcdir)
8014 switch (cstate->linktype) {
8023 bpf_error(cstate, "frame direction supported only with 802.11 headers");
8027 b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, (bpf_int32)fcdir,
8034 gen_acode(compiler_state_t *cstate, const u_char *eaddr, struct qual q)
8036 switch (cstate->linktype) {
8042 return (gen_ahostop(cstate, eaddr, (int)q.dir));
8044 bpf_error(cstate, "ARCnet address used in non-arc expression");
8050 bpf_error(cstate, "aid supported only on ARCnet");
8056 gen_ahostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
8063 return gen_bcmp(cstate, OR_LINKHDR, 0, 1, eaddr);
8066 return gen_bcmp(cstate, OR_LINKHDR, 1, 1, eaddr);
8069 b0 = gen_ahostop(cstate, eaddr, Q_SRC);
8070 b1 = gen_ahostop(cstate, eaddr, Q_DST);
8076 b0 = gen_ahostop(cstate, eaddr, Q_SRC);
8077 b1 = gen_ahostop(cstate, eaddr, Q_DST);
8082 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
8086 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
8090 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
8094 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
8098 bpf_error(cstate, "'ra' is only supported on 802.11");
8102 bpf_error(cstate, "'ta' is only supported on 802.11");
8110 gen_vlan_tpid_test(compiler_state_t *cstate)
8115 b0 = gen_linktype(cstate, ETHERTYPE_8021Q);
8116 b1 = gen_linktype(cstate, ETHERTYPE_8021AD);
8119 b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ);
8126 gen_vlan_vid_test(compiler_state_t *cstate, int vlan_num)
8128 return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, (bpf_int32)vlan_num, 0x0fff);
8132 gen_vlan_no_bpf_extensions(compiler_state_t *cstate, int vlan_num)
8136 b0 = gen_vlan_tpid_test(cstate);
8139 b1 = gen_vlan_vid_test(cstate, vlan_num);
8148 cstate->off_linkpl.constant_part += 4;
8149 cstate->off_linktype.constant_part += 4;
8157 gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off, int v, struct slist *s)
8164 off->reg = alloc_reg(cstate);
8166 s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
8169 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
8172 s2 = new_stmt(cstate, BPF_ST);
8182 cstate, struct block *b_tpid)
8188 cstate->is_vlan_vloffset = 1;
8189 gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s);
8190 gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s);
8202 gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid)
8207 s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8211 sjeq = new_stmt(cstate, JMP(BPF_JEQ));
8216 s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8225 s2 = new_stmt(cstate, JMP(BPF_JA));
8243 gen_vlan_bpf_extensions(compiler_state_t *cstate, int vlan_num)
8250 s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8253 b0 = new_block(cstate, JMP(BPF_JEQ));
8267 b_tpid = gen_vlan_tpid_test(cstate);
8269 b_vid = gen_vlan_vid_test(cstate, vlan_num);
8271 gen_vlan_patch_tpid_test(cstate, b_tpid);
8276 gen_vlan_patch_vid_test(cstate, b_vid);
8289 gen_vlan(compiler_state_t *cstate, int vlan_num)
8294 if (cstate->label_stack_depth > 0)
8295 bpf_error(cstate, "no VLAN match after MPLS");
8328 switch (cstate->linktype) {
8336 if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable &&
8337 cstate->off_linkhdr.constant_part ==
8338 cstate->off_outermostlinkhdr.constant_part) {
8342 if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING)
8343 b0 = gen_vlan_bpf_extensions(cstate, vlan_num);
8345 b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num);
8348 b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num);
8355 b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num);
8359 bpf_error(cstate, "no VLAN support for data link type %d",
8360 cstate->linktype);
8364 cstate->vlan_stack_depth++;
8373 gen_mpls(compiler_state_t *cstate, int label_num)
8377 if (cstate->label_stack_depth > 0) {
8379 b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01);
8385 switch (cstate->linktype) {
8391 b0 = gen_linktype(cstate, ETHERTYPE_MPLS);
8395 b0 = gen_linktype(cstate, PPP_MPLS_UCAST);
8403 bpf_error(cstate, "no MPLS support for data link type %d",
8404 cstate->linktype);
8413 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, (bpf_int32)label_num,
8433 cstate->off_nl_nosnap += 4;
8434 cstate->off_nl += 4;
8435 cstate->label_stack_depth++;
8443 gen_pppoed(compiler_state_t *cstate)
8446 return gen_linktype(cstate, (bpf_int32)ETHERTYPE_PPPOED);
8450 gen_pppoes(compiler_state_t *cstate, int sess_num)
8457 b0 = gen_linktype(cstate, (bpf_int32)ETHERTYPE_PPPOES);
8461 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W,
8504 * it's 6 bytes past cstate->off_nl.
8506 PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable,
8507 cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */
8508 cstate->off_linkpl.reg);
8510 cstate->off_linktype = cstate->off_linkhdr;
8511 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2;
8513 cstate->off_nl = 0;
8514 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
8522 gen_geneve_check(compiler_state_t *cstate,
8528 b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST);
8533 b1 = gen_mcmp(cstate, offrel, 8, BPF_B, (bpf_int32)0, 0xc0);
8539 b1 = gen_mcmp(cstate, offrel, 12, BPF_W, (bpf_int32)vni,
8554 gen_geneve4(compiler_state_t *cstate, int vni)
8559 b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni);
8562 s = gen_loadx_iphdrlen(cstate);
8564 s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
8570 b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
8580 gen_geneve6(compiler_state_t *cstate, int vni)
8585 b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni);
8589 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
8591 s1 = new_stmt(cstate, BPF_LD|BPF_IMM);
8595 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
8599 s = new_stmt(cstate, BPF_LD|BPF_IMM);
8606 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
8609 b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
8623 gen_geneve_offsets(compiler_state_t *cstate)
8632 s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
8633 s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8;
8636 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
8641 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
8645 cstate->off_linktype.reg = alloc_reg(cstate);
8646 cstate->off_linktype.is_variable = 1;
8647 cstate->off_linktype.constant_part = 0;
8649 s1 = new_stmt(cstate, BPF_ST);
8650 s1->s.k = cstate->off_linktype.reg;
8656 s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
8660 s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
8664 s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
8669 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
8674 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
8686 cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate));
8688 s1 = new_stmt(cstate, BPF_ST);
8689 s1->s.k = cstate->off_linkhdr.reg;
8698 cstate->no_optimize = 1;
8701 s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H);
8706 s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
8707 s1->s.k = cstate->off_linkhdr.reg;
8713 s_proto = new_stmt(cstate, JMP(BPF_JEQ));
8717 s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
8723 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
8727 s1 = new_stmt(cstate, BPF_ST);
8728 s1->s.k = cstate->off_linktype.reg;
8733 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
8738 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
8742 cstate->off_linkpl.reg = alloc_reg(cstate);
8743 cstate->off_linkpl.is_variable = 1;
8744 cstate->off_linkpl.constant_part = 0;
8746 s1 = new_stmt(cstate, BPF_STX);
8747 s1->s.k = cstate->off_linkpl.reg;
8751 cstate->off_nl = 0;
8758 gen_geneve(compiler_state_t *cstate, int vni)
8763 b0 = gen_geneve4(cstate, vni);
8764 b1 = gen_geneve6(cstate, vni);
8772 s = gen_geneve_offsets(cstate);
8774 b1 = gen_true(cstate);
8780 cstate->is_geneve = 1;
8788 gen_geneve_ll_check(compiler_state_t *cstate)
8799 s = new_stmt(cstate, BPF_LD|BPF_MEM);
8800 s->s.k = cstate->off_linkhdr.reg;
8802 s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
8803 s1->s.k = cstate->off_linkpl.reg;
8806 b0 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
8815 gen_atmfield_code(compiler_state_t *cstate, int atmfield, bpf_int32 jvalue,
8823 if (!cstate->is_atm)
8824 bpf_error(cstate, "'vpi' supported only on raw ATM");
8825 if (cstate->off_vpi == OFFSET_NOT_SET)
8827 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B, 0xffffffff, jtype,
8832 if (!cstate->is_atm)
8833 bpf_error(cstate, "'vci' supported only on raw ATM");
8834 if (cstate->off_vci == OFFSET_NOT_SET)
8836 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H, 0xffffffff, jtype,
8841 if (cstate->off_proto == OFFSET_NOT_SET)
8843 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B, 0x0f, jtype,
8848 if (cstate->off_payload == OFFSET_NOT_SET)
8850 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, BPF_B,
8855 if (!cstate->is_atm)
8856 bpf_error(cstate, "'callref' supported only on raw ATM");
8857 if (cstate->off_proto == OFFSET_NOT_SET)
8859 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B, 0xffffffff,
8870 gen_atmtype_abbrev(compiler_state_t *cstate, int type)
8878 if (!cstate->is_atm)
8879 bpf_error(cstate, "'metac' supported only on raw ATM");
8880 b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
8881 b1 = gen_atmfield_code(cstate, A_VCI, 1, BPF_JEQ, 0);
8887 if (!cstate->is_atm)
8888 bpf_error(cstate, "'bcc' supported only on raw ATM");
8889 b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
8890 b1 = gen_atmfield_code(cstate, A_VCI, 2, BPF_JEQ, 0);
8896 if (!cstate->is_atm)
8897 bpf_error(cstate, "'oam4sc' supported only on raw ATM");
8898 b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
8899 b1 = gen_atmfield_code(cstate, A_VCI, 3, BPF_JEQ, 0);
8905 if (!cstate->is_atm)
8906 bpf_error(cstate, "'oam4ec' supported only on raw ATM");
8907 b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
8908 b1 = gen_atmfield_code(cstate, A_VCI, 4, BPF_JEQ, 0);
8914 if (!cstate->is_atm)
8915 bpf_error(cstate, "'sc' supported only on raw ATM");
8916 b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
8917 b1 = gen_atmfield_code(cstate, A_VCI, 5, BPF_JEQ, 0);
8923 if (!cstate->is_atm)
8924 bpf_error(cstate, "'ilmic' supported only on raw ATM");
8925 b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
8926 b1 = gen_atmfield_code(cstate, A_VCI, 16, BPF_JEQ, 0);
8932 if (!cstate->is_atm)
8933 bpf_error(cstate, "'lane' supported only on raw ATM");
8934 b1 = gen_atmfield_code(cstate, A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
8944 PUSH_LINKHDR(cstate, DLT_EN10MB, 0,
8945 cstate->off_payload + 2, /* Ethernet header */
8947 cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
8948 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* Ethernet */
8949 cstate->off_nl = 0; /* Ethernet II */
8950 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
8955 if (!cstate->is_atm)
8956 bpf_error(cstate, "'llc' supported only on raw ATM");
8957 b1 = gen_atmfield_code(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
8958 cstate->linktype = cstate->prevlinktype;
8975 gen_mtp2type_abbrev(compiler_state_t *cstate, int type)
8982 if ( (cstate->linktype != DLT_MTP2) &&
8983 (cstate->linktype != DLT_ERF) &&
8984 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
8985 bpf_error(cstate, "'fisu' supported only on MTP2");
8986 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
8987 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
8991 if ( (cstate->linktype != DLT_MTP2) &&
8992 (cstate->linktype != DLT_ERF) &&
8993 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
8994 bpf_error(cstate, "'lssu' supported only on MTP2");
8995 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
8996 b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
9001 if ( (cstate->linktype != DLT_MTP2) &&
9002 (cstate->linktype != DLT_ERF) &&
9003 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9004 bpf_error(cstate, "'msu' supported only on MTP2");
9005 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
9009 if ( (cstate->linktype != DLT_MTP2) &&
9010 (cstate->linktype != DLT_ERF) &&
9011 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9012 bpf_error(cstate, "'hfisu' supported only on MTP2_HSL");
9013 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9014 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 0xff80, BPF_JEQ, 0, 0);
9018 if ( (cstate->linktype != DLT_MTP2) &&
9019 (cstate->linktype != DLT_ERF) &&
9020 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9021 bpf_error(cstate, "'hlssu' supported only on MTP2_HSL");
9022 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 0xff80, BPF_JGT, 1, 0x0100);
9023 b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 0xff80, BPF_JGT, 0, 0);
9028 if ( (cstate->linktype != DLT_MTP2) &&
9029 (cstate->linktype != DLT_ERF) &&
9030 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9031 bpf_error(cstate, "'hmsu' supported only on MTP2_HSL");
9032 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 0xff80, BPF_JGT, 0, 0x0100);
9042 gen_mtp3field_code(compiler_state_t *cstate, int mtp3field, bpf_u_int32 jvalue,
9047 u_int newoff_sio = cstate->off_sio;
9048 u_int newoff_opc = cstate->off_opc;
9049 u_int newoff_dpc = cstate->off_dpc;
9050 u_int newoff_sls = cstate->off_sls;
9059 if (cstate->off_sio == OFFSET_NOT_SET)
9060 bpf_error(cstate, "'sio' supported only on SS7");
9063 bpf_error(cstate, "sio value %u too big; max value = 255",
9065 b0 = gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, 0xffffffff,
9072 if (cstate->off_opc == OFFSET_NOT_SET)
9073 bpf_error(cstate, "'opc' supported only on SS7");
9076 bpf_error(cstate, "opc value %u too big; max value = 16383",
9087 b0 = gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W, 0x00c0ff0f,
9096 if (cstate->off_dpc == OFFSET_NOT_SET)
9097 bpf_error(cstate, "'dpc' supported only on SS7");
9100 bpf_error(cstate, "dpc value %u too big; max value = 16383",
9109 b0 = gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_W, 0xff3f0000,
9116 if (cstate->off_sls == OFFSET_NOT_SET)
9117 bpf_error(cstate, "'sls' supported only on SS7");
9120 bpf_error(cstate, "sls value %u too big; max value = 15",
9125 b0 = gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B, 0xf0,
9136 gen_msg_abbrev(compiler_state_t *cstate, int type)
9147 b1 = gen_atmfield_code(cstate, A_MSGTYPE, SETUP, BPF_JEQ, 0);
9151 b1 = gen_atmfield_code(cstate, A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
9155 b1 = gen_atmfield_code(cstate, A_MSGTYPE, CONNECT, BPF_JEQ, 0);
9159 b1 = gen_atmfield_code(cstate, A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
9163 b1 = gen_atmfield_code(cstate, A_MSGTYPE, RELEASE, BPF_JEQ, 0);
9167 b1 = gen_atmfield_code(cstate, A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
9177 gen_atmmulti_abbrev(compiler_state_t *cstate, int type)
9184 if (!cstate->is_atm)
9185 bpf_error(cstate, "'oam' supported only on raw ATM");
9186 b1 = gen_atmmulti_abbrev(cstate, A_OAMF4);
9190 if (!cstate->is_atm)
9191 bpf_error(cstate, "'oamf4' supported only on raw ATM");
9193 b0 = gen_atmfield_code(cstate, A_VCI, 3, BPF_JEQ, 0);
9194 b1 = gen_atmfield_code(cstate, A_VCI, 4, BPF_JEQ, 0);
9196 b0 = gen_atmfield_code(cstate, A_VPI, 0, BPF_JEQ, 0);
9205 if (!cstate->is_atm)
9206 bpf_error(cstate, "'connectmsg' supported only on raw ATM");
9207 b0 = gen_msg_abbrev(cstate, A_SETUP);
9208 b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
9210 b0 = gen_msg_abbrev(cstate, A_CONNECT);
9212 b0 = gen_msg_abbrev(cstate, A_CONNECTACK);
9214 b0 = gen_msg_abbrev(cstate, A_RELEASE);
9216 b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
9218 b0 = gen_atmtype_abbrev(cstate, A_SC);
9223 if (!cstate->is_atm)
9224 bpf_error(cstate, "'metaconnect' supported only on raw ATM");
9225 b0 = gen_msg_abbrev(cstate, A_SETUP);
9226 b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
9228 b0 = gen_msg_abbrev(cstate, A_CONNECT);
9230 b0 = gen_msg_abbrev(cstate, A_RELEASE);
9232 b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
9234 b0 = gen_atmtype_abbrev(cstate, A_METAC);