Lines Matching refs:cstate

424 bpf_set_error(compiler_state_t *cstate, const char *fmt, ...)
437 if (!cstate->error_set) {
439 (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE,
442 cstate->error_set = 1;
454 bpf_error(compiler_state_t *cstate, const char *fmt, ...)
459 (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE,
462 longjmp(cstate->top_ctx, 1);
475 static void initchunks(compiler_state_t *cstate);
476 static void *newchunk_nolongjmp(compiler_state_t *cstate, size_t);
477 static void *newchunk(compiler_state_t *cstate, size_t);
478 static void freechunks(compiler_state_t *cstate);
479 static inline struct block *new_block(compiler_state_t *cstate, int);
480 static inline struct slist *new_stmt(compiler_state_t *cstate, int);
481 static struct block *gen_retblk(compiler_state_t *cstate, int);
482 static inline void syntax(compiler_state_t *cstate);
576 static struct block *gen_geneve_ll_check(compiler_state_t *cstate);
585 initchunks(compiler_state_t *cstate)
590 cstate->chunks[i].n_left = 0;
591 cstate->chunks[i].m = NULL;
593 cstate->cur_chunk = 0;
597 newchunk_nolongjmp(compiler_state_t *cstate, size_t n)
611 cp = &cstate->chunks[cstate->cur_chunk];
614 k = ++cstate->cur_chunk;
616 bpf_set_error(cstate, "out of memory");
622 bpf_set_error(cstate, "out of memory");
628 bpf_set_error(cstate, "out of memory");
637 newchunk(compiler_state_t *cstate, size_t n)
641 p = newchunk_nolongjmp(cstate, n);
643 longjmp(cstate->top_ctx, 1);
650 freechunks(compiler_state_t *cstate)
655 if (cstate->chunks[i].m != NULL)
656 free(cstate->chunks[i].m);
666 sdup(compiler_state_t *cstate, const char *s)
669 char *cp = newchunk_nolongjmp(cstate, n);
678 new_block(compiler_state_t *cstate, int code)
682 p = (struct block *)newchunk(cstate, sizeof(*p));
690 new_stmt(compiler_state_t *cstate, int code)
694 p = (struct slist *)newchunk(cstate, sizeof(*p));
701 gen_retblk(compiler_state_t *cstate, int v)
703 struct block *b = new_block(cstate, BPF_RET|BPF_K);
710 syntax(compiler_state_t *cstate)
712 bpf_error(cstate, "syntax error in filter expression");
722 compiler_state_t cstate;
766 initchunks(&cstate);
767 cstate.no_optimize = 0;
769 cstate.ai = NULL;
771 cstate.e = NULL;
772 cstate.ic.root = NULL;
773 cstate.ic.cur_mark = 0;
774 cstate.bpf_pcap = p;
775 cstate.error_set = 0;
776 init_regs(&cstate);
778 cstate.netmask = mask;
780 cstate.snaplen = pcap_snapshot(p);
781 if (cstate.snaplen == 0) {
797 pcap_set_extra(&cstate, scanner);
799 if (init_linktype(&cstate, p) == -1) {
803 if (pcap_parse(scanner, &cstate) != 0) {
805 if (cstate.ai != NULL)
806 freeaddrinfo(cstate.ai);
808 if (cstate.e != NULL)
809 free(cstate.e);
814 if (cstate.ic.root == NULL) {
818 if (setjmp(cstate.top_ctx)) {
822 cstate.ic.root = gen_retblk(&cstate, cstate.snaplen);
825 if (optimize && !cstate.no_optimize) {
826 if (bpf_optimize(&cstate.ic, p->errbuf) == -1) {
831 if (cstate.ic.root == NULL ||
832 (cstate.ic.root->s.code == (BPF_RET|BPF_K) && cstate.ic.root->s.k == 0)) {
839 program->bf_insns = icode_to_fcode(&cstate.ic,
840 cstate.ic.root, &len, p->errbuf);
862 freechunks(&cstate);
942 finish_parse(compiler_state_t *cstate, struct block *p)
950 if (setjmp(cstate->top_ctx))
972 insert_compute_vloffsets(cstate, p->head);
987 ppi_dlt_check = gen_ppi_dlt_check(cstate);
991 backpatch(p, gen_retblk(cstate, cstate->snaplen));
993 backpatch(p, gen_retblk(cstate, 0));
994 cstate->ic.root = p->head;
1026 gen_cmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1029 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
1033 gen_cmp_gt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1036 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
1040 gen_cmp_ge(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1043 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
1047 gen_cmp_lt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1050 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
1054 gen_cmp_le(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1057 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
1061 gen_mcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1064 return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 0, v);
1068 gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1077 tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W,
1087 tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H,
1095 tmp = gen_cmp(cstate, offrel, offset, BPF_B, v[0]);
1110 gen_ncmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1117 s = gen_load_a(cstate, offrel, offset, size);
1120 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
1125 b = new_block(cstate, JMP(jtype));
1134 init_linktype(compiler_state_t *cstate, pcap_t *p)
1136 cstate->pcap_fddipad = p->fddipad;
1141 cstate->outermostlinktype = pcap_datalink(p);
1142 cstate->off_outermostlinkhdr.constant_part = 0;
1143 cstate->off_outermostlinkhdr.is_variable = 0;
1144 cstate->off_outermostlinkhdr.reg = -1;
1146 cstate->prevlinktype = cstate->outermostlinktype;
1147 cstate->off_prevlinkhdr.constant_part = 0;
1148 cstate->off_prevlinkhdr.is_variable = 0;
1149 cstate->off_prevlinkhdr.reg = -1;
1151 cstate->linktype = cstate->outermostlinktype;
1152 cstate->off_linkhdr.constant_part = 0;
1153 cstate->off_linkhdr.is_variable = 0;
1154 cstate->off_linkhdr.reg = -1;
1159 cstate->off_linkpl.constant_part = 0;
1160 cstate->off_linkpl.is_variable = 0;
1161 cstate->off_linkpl.reg = -1;
1163 cstate->off_linktype.constant_part = 0;
1164 cstate->off_linktype.is_variable = 0;
1165 cstate->off_linktype.reg = -1;
1170 cstate->is_atm = 0;
1171 cstate->off_vpi = OFFSET_NOT_SET;
1172 cstate->off_vci = OFFSET_NOT_SET;
1173 cstate->off_proto = OFFSET_NOT_SET;
1174 cstate->off_payload = OFFSET_NOT_SET;
1179 cstate->is_geneve = 0;
1184 cstate->is_vlan_vloffset = 0;
1189 cstate->off_li = OFFSET_NOT_SET;
1190 cstate->off_li_hsl = OFFSET_NOT_SET;
1191 cstate->off_sio = OFFSET_NOT_SET;
1192 cstate->off_opc = OFFSET_NOT_SET;
1193 cstate->off_dpc = OFFSET_NOT_SET;
1194 cstate->off_sls = OFFSET_NOT_SET;
1196 cstate->label_stack_depth = 0;
1197 cstate->vlan_stack_depth = 0;
1199 switch (cstate->linktype) {
1202 cstate->off_linktype.constant_part = 2;
1203 cstate->off_linkpl.constant_part = 6;
1204 cstate->off_nl = 0; /* XXX in reality, variable! */
1205 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1209 cstate->off_linktype.constant_part = 4;
1210 cstate->off_linkpl.constant_part = 8;
1211 cstate->off_nl = 0; /* XXX in reality, variable! */
1212 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1216 cstate->off_linktype.constant_part = 12;
1217 cstate->off_linkpl.constant_part = 14; /* Ethernet header length */
1218 cstate->off_nl = 0; /* Ethernet II */
1219 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
1227 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1228 cstate->off_linkpl.constant_part = 16;
1229 cstate->off_nl = 0;
1230 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1235 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1237 cstate->off_linkpl.constant_part = 24;
1238 cstate->off_nl = 0;
1239 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1244 cstate->off_linktype.constant_part = 0;
1245 cstate->off_linkpl.constant_part = 4;
1246 cstate->off_nl = 0;
1247 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1251 cstate->off_linktype.constant_part = 0;
1252 cstate->off_linkpl.constant_part = 12;
1253 cstate->off_nl = 0;
1254 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1262 cstate->off_linktype.constant_part = 2; /* skip HDLC-like framing */
1263 cstate->off_linkpl.constant_part = 4; /* skip HDLC-like framing and protocol field */
1264 cstate->off_nl = 0;
1265 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1273 cstate->off_linktype.constant_part = 6;
1274 cstate->off_linkpl.constant_part = 8;
1275 cstate->off_nl = 0;
1276 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1280 cstate->off_linktype.constant_part = 5;
1281 cstate->off_linkpl.constant_part = 24;
1282 cstate->off_nl = 0;
1283 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1295 cstate->off_linktype.constant_part = 13;
1296 cstate->off_linktype.constant_part += cstate->pcap_fddipad;
1297 cstate->off_linkpl.constant_part = 13; /* FDDI MAC header length */
1298 cstate->off_linkpl.constant_part += cstate->pcap_fddipad;
1299 cstate->off_nl = 8; /* 802.2+SNAP */
1300 cstate->off_nl_nosnap = 3; /* 802.2 */
1327 cstate->off_linktype.constant_part = 14;
1328 cstate->off_linkpl.constant_part = 14; /* Token Ring MAC header length */
1329 cstate->off_nl = 8; /* 802.2+SNAP */
1330 cstate->off_nl_nosnap = 3; /* 802.2 */
1336 cstate->off_linkhdr.is_variable = 1;
1360 cstate->off_linktype.constant_part = 24;
1361 cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */
1362 cstate->off_linkpl.is_variable = 1;
1363 cstate->off_nl = 8; /* 802.2+SNAP */
1364 cstate->off_nl_nosnap = 3; /* 802.2 */
1377 cstate->off_linktype.constant_part = 24;
1378 cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */
1379 cstate->off_linkpl.is_variable = 1;
1380 cstate->off_linkhdr.is_variable = 1;
1381 cstate->off_nl = 8; /* 802.2+SNAP */
1382 cstate->off_nl_nosnap = 3; /* 802.2 */
1398 cstate->off_linktype.constant_part = 0;
1399 cstate->off_linkpl.constant_part = 0; /* packet begins with LLC header */
1400 cstate->off_nl = 8; /* 802.2+SNAP */
1401 cstate->off_nl_nosnap = 3; /* 802.2 */
1409 cstate->is_atm = 1;
1410 cstate->off_vpi = SUNATM_VPI_POS;
1411 cstate->off_vci = SUNATM_VCI_POS;
1412 cstate->off_proto = PROTO_POS;
1413 cstate->off_payload = SUNATM_PKT_BEGIN_POS;
1414 cstate->off_linktype.constant_part = cstate->off_payload;
1415 cstate->off_linkpl.constant_part = cstate->off_payload; /* if LLC-encapsulated */
1416 cstate->off_nl = 8; /* 802.2+SNAP */
1417 cstate->off_nl_nosnap = 3; /* 802.2 */
1423 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1424 cstate->off_linkpl.constant_part = 0;
1425 cstate->off_nl = 0;
1426 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1430 cstate->off_linktype.constant_part = 14;
1431 cstate->off_linkpl.constant_part = 16;
1432 cstate->off_nl = 0;
1433 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1437 cstate->off_linktype.constant_part = 0;
1438 cstate->off_linkpl.constant_part = 20;
1439 cstate->off_nl = 0;
1440 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1449 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1450 cstate->off_linkpl.constant_part = 0;
1451 cstate->off_nl = 0;
1452 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1466 cstate->off_linktype.constant_part = 16;
1467 cstate->off_linkpl.constant_part = 16;
1468 cstate->off_nl = 8; /* 802.2+SNAP */
1469 cstate->off_nl_nosnap = 3; /* 802.2 */
1477 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1478 cstate->off_linkpl.constant_part = 0;
1479 cstate->off_nl = 0;
1480 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1489 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1490 cstate->off_linkpl.constant_part = 0;
1491 cstate->off_nl = 4;
1492 cstate->off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1496 cstate->off_linktype.constant_part = 16;
1497 cstate->off_linkpl.constant_part = 18;
1498 cstate->off_nl = 0;
1499 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1503 cstate->off_linktype.constant_part = 6;
1504 cstate->off_linkpl.constant_part = 44;
1505 cstate->off_nl = 0; /* Ethernet II */
1506 cstate->off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
1510 cstate->off_linktype.constant_part = 0;
1511 cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */
1512 cstate->off_linkpl.is_variable = 1;
1513 cstate->off_nl = 0;
1514 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
1523 cstate->off_linktype.constant_part = 4;
1524 cstate->off_linkpl.constant_part = 4;
1525 cstate->off_nl = 0;
1526 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1530 cstate->off_linktype.constant_part = 4; /* in reality variable between 4-8 */
1531 cstate->off_linkpl.constant_part = 4; /* in reality variable between 4-8 */
1532 cstate->off_nl = 0;
1533 cstate->off_nl_nosnap = 10;
1537 cstate->off_linktype.constant_part = 8; /* in reality variable between 8-12 */
1538 cstate->off_linkpl.constant_part = 8; /* in reality variable between 8-12 */
1539 cstate->off_nl = 0;
1540 cstate->off_nl_nosnap = 10;
1547 cstate->off_linkpl.constant_part = 14;
1548 cstate->off_linktype.constant_part = 16;
1549 cstate->off_nl = 18; /* Ethernet II */
1550 cstate->off_nl_nosnap = 21; /* 802.3+802.2 */
1554 cstate->off_linktype.constant_part = 4;
1555 cstate->off_linkpl.constant_part = 6;
1556 cstate->off_nl = 0;
1557 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1561 cstate->off_linktype.constant_part = 6;
1562 cstate->off_linkpl.constant_part = 12;
1563 cstate->off_nl = 0;
1564 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1568 cstate->off_linktype.constant_part = 6;
1569 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */
1570 cstate->off_nl = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */
1571 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1575 cstate->off_linktype.constant_part = 12;
1576 cstate->off_linkpl.constant_part = 12;
1577 cstate->off_nl = 0; /* raw IP/IP6 header */
1578 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1582 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1583 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1584 cstate->off_nl = OFFSET_NOT_SET;
1585 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1589 cstate->off_linktype.constant_part = 12;
1590 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */
1591 cstate->off_nl = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */
1592 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1596 cstate->off_linktype.constant_part = 18;
1597 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1598 cstate->off_nl = OFFSET_NOT_SET;
1599 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1603 cstate->off_linktype.constant_part = 18;
1604 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1605 cstate->off_nl = OFFSET_NOT_SET;
1606 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1610 cstate->off_linktype.constant_part = 8;
1611 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1612 cstate->off_nl = OFFSET_NOT_SET;
1613 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1620 cstate->off_linktype.constant_part = 8;
1621 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1622 cstate->off_nl = OFFSET_NOT_SET;
1623 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1627 cstate->off_li = 2;
1628 cstate->off_li_hsl = 4;
1629 cstate->off_sio = 3;
1630 cstate->off_opc = 4;
1631 cstate->off_dpc = 4;
1632 cstate->off_sls = 7;
1633 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1634 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1635 cstate->off_nl = OFFSET_NOT_SET;
1636 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1640 cstate->off_li = 6;
1641 cstate->off_li_hsl = 8;
1642 cstate->off_sio = 7;
1643 cstate->off_opc = 8;
1644 cstate->off_dpc = 8;
1645 cstate->off_sls = 11;
1646 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1647 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1648 cstate->off_nl = OFFSET_NOT_SET;
1649 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1653 cstate->off_li = 22;
1654 cstate->off_li_hsl = 24;
1655 cstate->off_sio = 23;
1656 cstate->off_opc = 24;
1657 cstate->off_dpc = 24;
1658 cstate->off_sls = 27;
1659 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1660 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1661 cstate->off_nl = OFFSET_NOT_SET;
1662 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1666 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1667 cstate->off_linkpl.constant_part = 4;
1668 cstate->off_nl = 0;
1669 cstate->off_nl_nosnap = 0;
1676 cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */
1677 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1678 cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */
1679 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
1683 cstate->off_linktype.constant_part = 1;
1684 cstate->off_linkpl.constant_part = 24; /* ipnet header length */
1685 cstate->off_nl = 0;
1686 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1690 cstate->off_linkhdr.constant_part = 4; /* Ethernet header is past 4-byte pseudo-header */
1691 cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
1692 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+Ethernet header length */
1693 cstate->off_nl = 0; /* Ethernet II */
1694 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
1698 cstate->off_linkhdr.constant_part = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1699 cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
1700 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1701 cstate->off_nl = 0; /* Ethernet II */
1702 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
1710 if (cstate->linktype >= DLT_MATCHING_MIN &&
1711 cstate->linktype <= DLT_MATCHING_MAX) {
1712 cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1713 cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1714 cstate->off_nl = OFFSET_NOT_SET;
1715 cstate->off_nl_nosnap = OFFSET_NOT_SET;
1717 bpf_set_error(cstate, "unknown data link type %d (min %d, max %d)",
1718 cstate->linktype, DLT_MATCHING_MIN, DLT_MATCHING_MAX);
1724 cstate->off_outermostlinkhdr = cstate->off_prevlinkhdr = cstate->off_linkhdr;
1732 gen_load_absoffsetrel(compiler_state_t *cstate, bpf_abs_offset *abs_offset,
1737 s = gen_abs_offset_varpart(cstate, abs_offset);
1754 s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);
1762 s = new_stmt(cstate, BPF_LD|BPF_ABS|size);
1772 gen_load_a(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1793 s = new_stmt(cstate, BPF_LD|BPF_ABS|size);
1798 s = gen_load_absoffsetrel(cstate, &cstate->off_linkhdr, offset, size);
1802 s = gen_load_absoffsetrel(cstate, &cstate->off_prevlinkhdr, offset, size);
1806 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, offset, size);
1810 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl - 4 + offset, size);
1814 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + offset, size);
1818 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl_nosnap + offset, size);
1822 s = gen_load_absoffsetrel(cstate, &cstate->off_linktype, offset, size);
1832 s = gen_loadx_iphdrlen(cstate);
1845 s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);
1846 s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + offset;
1851 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + 40 + offset, size);
1863 gen_loadx_iphdrlen(compiler_state_t *cstate)
1867 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
1879 s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
1880 s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
1882 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
1885 s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
1895 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
1896 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
1905 * is at an offset of cstate->off_nl from the beginning of
1907 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1910 s = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
1911 s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
1918 gen_uncond(compiler_state_t *cstate, int rsense)
1923 s = new_stmt(cstate, BPF_LD|BPF_IMM);
1925 b = new_block(cstate, JMP(BPF_JEQ));
1932 gen_true(compiler_state_t *cstate)
1934 return gen_uncond(cstate, 1);
1938 gen_false(compiler_state_t *cstate)
1940 return gen_uncond(cstate, 0);
1961 gen_ether_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
1982 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
1984 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto);
2021 b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
2022 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF);
2029 b0 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);
2036 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
2052 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX);
2068 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
2083 b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
2085 b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);
2093 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
2108 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
2110 b1 = gen_cmp(cstate, OR_LINKTYPE, 2, BPF_B, ll_proto);
2123 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
2129 gen_loopback_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
2144 if (cstate->linktype == DLT_NULL || cstate->linktype == DLT_ENC) {
2156 if (cstate->bpf_pcap->rfile != NULL && cstate->bpf_pcap->swapped)
2160 return (gen_cmp(cstate, OR_LINKHDR, 0, BPF_W, ll_proto));
2168 gen_ipnet_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
2173 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET);
2177 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET6);
2184 return gen_false(cstate);
2196 gen_linux_sll_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
2217 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2218 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto);
2248 b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
2249 b1 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);
2251 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2258 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_3);
2266 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX);
2282 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2296 b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
2298 b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);
2306 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
2320 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2321 b1 = gen_cmp(cstate, OR_LINKHDR, cstate->off_linkpl.constant_part, BPF_B,
2335 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
2345 gen_load_pflog_llprefixlen(compiler_state_t *cstate)
2356 if (cstate->off_linkpl.reg != -1) {
2360 s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2367 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
2370 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
2378 s2 = new_stmt(cstate, BPF_ST);
2379 s2->s.k = cstate->off_linkpl.reg;
2385 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2394 gen_load_prism_llprefixlen(compiler_state_t *cstate)
2405 cstate->no_optimize = 1;
2426 if (cstate->off_linkhdr.reg != -1) {
2430 s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2436 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
2443 sjeq_avs_cookie = new_stmt(cstate, JMP(BPF_JEQ));
2454 s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2467 sjcommon = new_stmt(cstate, JMP(BPF_JA));
2477 s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);
2487 s2 = new_stmt(cstate, BPF_ST);
2488 s2->s.k = cstate->off_linkhdr.reg;
2495 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2504 gen_load_avs_llprefixlen(compiler_state_t *cstate)
2515 if (cstate->off_linkhdr.reg != -1) {
2521 s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2528 s2 = new_stmt(cstate, BPF_ST);
2529 s2->s.k = cstate->off_linkhdr.reg;
2535 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2544 gen_load_radiotap_llprefixlen(compiler_state_t *cstate)
2555 if (cstate->off_linkhdr.reg != -1) {
2567 s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2569 s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
2572 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2579 s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2582 s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);
2589 s2 = new_stmt(cstate, BPF_ST);
2590 s2->s.k = cstate->off_linkhdr.reg;
2596 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2614 gen_load_ppi_llprefixlen(compiler_state_t *cstate)
2623 if (cstate->off_linkhdr.reg != -1) {
2635 s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2637 s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
2640 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2647 s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2650 s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);
2657 s2 = new_stmt(cstate, BPF_ST);
2658 s2->s.k = cstate->off_linkhdr.reg;
2664 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2680 gen_load_802_11_header_len(compiler_state_t *cstate, struct slist *s, struct slist *snext)
2692 if (cstate->off_linkpl.reg == -1) {
2707 cstate->no_optimize = 1;
2724 * and store it in the cstate->off_linkpl.reg register.
2727 s = new_stmt(cstate, BPF_LDX|BPF_IMM);
2728 s->s.k = cstate->off_outermostlinkhdr.constant_part;
2735 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2738 s2 = new_stmt(cstate, BPF_MISC|BPF_TXA);
2740 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
2743 s2 = new_stmt(cstate, BPF_ST);
2744 s2->s.k = cstate->off_linkpl.reg;
2747 s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
2756 sjset_data_frame_1 = new_stmt(cstate, JMP(BPF_JSET));
2764 sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(cstate, JMP(BPF_JSET));
2775 sjset_data_frame_2->s.jf = sjset_qos = new_stmt(cstate, JMP(BPF_JSET));
2780 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2785 sjset_qos->s.jt = s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
2786 s2->s.k = cstate->off_linkpl.reg;
2788 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
2791 s2 = new_stmt(cstate, BPF_ST);
2792 s2->s.k = cstate->off_linkpl.reg;
2814 if (cstate->linktype == DLT_IEEE802_11_RADIO) {
2819 sjset_qos->s.jf = s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_W);
2823 sjset_radiotap_flags_present = new_stmt(cstate, JMP(BPF_JSET));
2835 sjset_radiotap_ext_present = new_stmt(cstate, JMP(BPF_JSET));
2848 sjset_radiotap_tsft_present = new_stmt(cstate, JMP(BPF_JSET));
2862 s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
2867 sjset_tsft_datapad = new_stmt(cstate, JMP(BPF_JSET));
2879 s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
2884 sjset_notsft_datapad = new_stmt(cstate, JMP(BPF_JSET));
2895 s_roundup = new_stmt(cstate, BPF_LD|BPF_MEM);
2896 s_roundup->s.k = cstate->off_linkpl.reg;
2898 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
2901 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_IMM);
2904 s2 = new_stmt(cstate, BPF_ST);
2905 s2->s.k = cstate->off_linkpl.reg;
2919 insert_compute_vloffsets(compiler_state_t *cstate, struct block *b)
2928 if (cstate->off_linkpl.reg != -1 && cstate->off_linkhdr.is_variable &&
2929 cstate->off_linkhdr.reg == -1)
2930 cstate->off_linkhdr.reg = alloc_reg(cstate);
2943 switch (cstate->outermostlinktype) {
2946 s = gen_load_prism_llprefixlen(cstate);
2950 s = gen_load_avs_llprefixlen(cstate);
2954 s = gen_load_radiotap_llprefixlen(cstate);
2958 s = gen_load_ppi_llprefixlen(cstate);
2971 switch (cstate->outermostlinktype) {
2978 s = gen_load_802_11_header_len(cstate, s, b->stmts);
2982 s = gen_load_pflog_llprefixlen(cstate);
2990 if (s == NULL && cstate->is_vlan_vloffset) {
2993 if (cstate->off_linkpl.reg == -1)
2994 cstate->off_linkpl.reg = alloc_reg(cstate);
2995 if (cstate->off_linktype.reg == -1)
2996 cstate->off_linktype.reg = alloc_reg(cstate);
2998 s = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);
3000 s2 = new_stmt(cstate, BPF_ST);
3001 s2->s.k = cstate->off_linkpl.reg;
3003 s2 = new_stmt(cstate, BPF_ST);
3004 s2->s.k = cstate->off_linktype.reg;
3021 gen_ppi_dlt_check(compiler_state_t *cstate)
3026 if (cstate->linktype == DLT_PPI)
3030 s_load_dlt = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
3033 b = new_block(cstate, JMP(BPF_JEQ));
3060 gen_abs_offset_varpart(compiler_state_t *cstate, bpf_abs_offset *off)
3071 off->reg = alloc_reg(cstate);
3078 s = new_stmt(cstate, BPF_LDX|BPF_MEM);
3145 gen_prevlinkhdr_check(compiler_state_t *cstate)
3149 if (cstate->is_geneve)
3150 return gen_geneve_ll_check(cstate);
3152 switch (cstate->prevlinktype) {
3162 b0 = gen_cmp(cstate, OR_PREVLINKHDR, SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
3191 gen_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
3197 if (cstate->label_stack_depth > 0)
3198 return gen_mpls_linktype(cstate, ll_proto);
3200 switch (cstate->linktype) {
3207 if (!cstate->is_geneve)
3208 b0 = gen_prevlinkhdr_check(cstate);
3212 b1 = gen_ether_linktype(cstate, ll_proto);
3227 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
3239 b0 = gen_check_802_11_data_frame(cstate);
3244 b1 = gen_llc_linktype(cstate, ll_proto);
3253 return gen_llc_linktype(cstate, ll_proto);
3260 return gen_llc_linktype(cstate, ll_proto);
3266 return gen_llc_linktype(cstate, ll_proto);
3277 b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3278 b1 = gen_llc_linktype(cstate, ll_proto);
3284 return gen_linux_sll_linktype(cstate, ll_proto);
3301 return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x40, 0xF0);
3305 return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x60, 0xF0);
3308 return gen_false(cstate); /* always false */
3317 return gen_true(cstate); /* always true */
3320 return gen_false(cstate);
3328 return gen_true(cstate); /* always true */
3331 return gen_false(cstate);
3342 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,
3358 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_IP);
3359 b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJC);
3361 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJNC);
3366 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,
3377 return (gen_loopback_linktype(cstate, AF_INET));
3402 if (cstate->bpf_pcap->rfile != NULL) {
3407 b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_BSD);
3408 b1 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_FREEBSD);
3410 b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_DARWIN);
3426 return (gen_loopback_linktype(cstate, 24));
3429 return (gen_loopback_linktype(cstate, AF_INET6));
3435 return gen_false(cstate);
3446 return gen_false(cstate);
3455 return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),
3458 return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),
3461 return gen_false(cstate);
3473 return gen_false(cstate);
3476 return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3480 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3482 b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3488 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3490 b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3496 return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3500 return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3508 return gen_true(cstate);
3510 return gen_false(cstate);
3525 return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0xcc);
3531 return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0x8e);
3545 b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3546 b1 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3547 b2 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3553 return gen_false(cstate);
3558 bpf_error(cstate, "Multi-link Frame Relay link-layer type filtering not implemented");
3590 return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3593 return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x55FF0000, 0xffff0000);
3596 return gen_ipnet_linktype(cstate, ll_proto);
3599 bpf_error(cstate, "IrDA link-layer type filtering not implemented");
3602 bpf_error(cstate, "DOCSIS link-layer type filtering not implemented");
3606 bpf_error(cstate, "MTP2 link-layer type filtering not implemented");
3609 bpf_error(cstate, "ERF link-layer type filtering not implemented");
3612 bpf_error(cstate, "PFSYNC link-layer type filtering not implemented");
3615 bpf_error(cstate, "LAPD link-layer type filtering not implemented");
3621 bpf_error(cstate, "USB link-layer type filtering not implemented");
3625 bpf_error(cstate, "Bluetooth link-layer type filtering not implemented");
3629 bpf_error(cstate, "CAN link-layer type filtering not implemented");
3636 bpf_error(cstate, "IEEE 802.15.4 link-layer type filtering not implemented");
3639 bpf_error(cstate, "IEEE 802.16 link-layer type filtering not implemented");
3642 bpf_error(cstate, "SITA link-layer type filtering not implemented");
3645 bpf_error(cstate, "RAIF1 link-layer type filtering not implemented");
3649 bpf_error(cstate, "IPMB link-layer type filtering not implemented");
3652 bpf_error(cstate, "AX.25 link-layer type filtering not implemented");
3659 bpf_error(cstate, "NFLOG link-layer type filtering not implemented");
3668 if (cstate->off_linktype.constant_part != OFFSET_NOT_SET) {
3674 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
3680 description = pcap_datalink_val_to_description_or_dlt(cstate->linktype);
3681 bpf_error(cstate, "%s link-layer type filtering not implemented",
3696 gen_snap(compiler_state_t *cstate, bpf_u_int32 orgcode, bpf_u_int32 ptype)
3708 return gen_bcmp(cstate, OR_LLC, 0, 8, snapblock);
3715 gen_llc_internal(compiler_state_t *cstate)
3719 switch (cstate->linktype) {
3726 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
3733 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF);
3742 b0 = gen_atmtype_llc(cstate);
3749 return gen_true(cstate);
3755 return gen_true(cstate);
3766 return gen_true(cstate);
3776 b0 = gen_check_802_11_data_frame(cstate);
3780 bpf_error(cstate, "'llc' not supported for %s",
3781 pcap_datalink_val_to_description_or_dlt(cstate->linktype));
3787 gen_llc(compiler_state_t *cstate)
3793 if (setjmp(cstate->top_ctx))
3796 return gen_llc_internal(cstate);
3800 gen_llc_i(compiler_state_t *cstate)
3809 if (setjmp(cstate->top_ctx))
3815 b0 = gen_llc_internal(cstate);
3821 s = gen_load_a(cstate, OR_LLC, 2, BPF_B);
3822 b1 = new_block(cstate, JMP(BPF_JSET));
3831 gen_llc_s(compiler_state_t *cstate)
3839 if (setjmp(cstate->top_ctx))
3845 b0 = gen_llc_internal(cstate);
3851 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03);
3857 gen_llc_u(compiler_state_t *cstate)
3865 if (setjmp(cstate->top_ctx))
3871 b0 = gen_llc_internal(cstate);
3877 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03);
3883 gen_llc_s_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
3891 if (setjmp(cstate->top_ctx))
3897 b0 = gen_llc_internal(cstate);
3902 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK);
3908 gen_llc_u_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
3916 if (setjmp(cstate->top_ctx))
3922 b0 = gen_llc_internal(cstate);
3927 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK);
3945 gen_llc_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
3960 return gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_u_int32)
3968 return gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
3980 return gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
3992 return gen_cmp(cstate, OR_LLC, 0, BPF_B, ll_proto);
4007 * return gen_snap(cstate, 0x000000, ll_proto);
4013 return gen_cmp(cstate, OR_LLC, 6, BPF_H, ll_proto);
4019 gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
4036 b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4037 b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4043 b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4044 b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4049 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4053 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4057 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4061 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4065 bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4069 bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4076 b0 = gen_linktype(cstate, ll_proto);
4077 b1 = gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, addr, mask);
4084 gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr,
4103 b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4104 b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4110 b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4111 b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4116 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4120 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4124 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4128 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4132 bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4136 bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4146 b1 = gen_mcmp(cstate, OR_LINKPL, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
4147 b0 = gen_mcmp(cstate, OR_LINKPL, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
4149 b0 = gen_mcmp(cstate, OR_LINKPL, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
4151 b0 = gen_mcmp(cstate, OR_LINKPL, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
4153 b0 = gen_linktype(cstate, ll_proto);
4160 gen_ehostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4166 return gen_bcmp(cstate, OR_LINKHDR, 6, 6, eaddr);
4169 return gen_bcmp(cstate, OR_LINKHDR, 0, 6, eaddr);
4172 b0 = gen_ehostop(cstate, eaddr, Q_SRC);
4173 b1 = gen_ehostop(cstate, eaddr, Q_DST);
4179 b0 = gen_ehostop(cstate, eaddr, Q_SRC);
4180 b1 = gen_ehostop(cstate, eaddr, Q_DST);
4185 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4189 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4193 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4197 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4201 bpf_error(cstate, "'ra' is only supported on 802.11 with 802.11 headers");
4205 bpf_error(cstate, "'ta' is only supported on 802.11 with 802.11 headers");
4216 gen_fhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4222 return gen_bcmp(cstate, OR_LINKHDR, 6 + 1 + cstate->pcap_fddipad, 6, eaddr);
4225 return gen_bcmp(cstate, OR_LINKHDR, 0 + 1 + cstate->pcap_fddipad, 6, eaddr);
4228 b0 = gen_fhostop(cstate, eaddr, Q_SRC);
4229 b1 = gen_fhostop(cstate, eaddr, Q_DST);
4235 b0 = gen_fhostop(cstate, eaddr, Q_SRC);
4236 b1 = gen_fhostop(cstate, eaddr, Q_DST);
4241 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4245 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4249 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4253 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4257 bpf_error(cstate, "'ra' is only supported on 802.11");
4261 bpf_error(cstate, "'ta' is only supported on 802.11");
4272 gen_thostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4278 return gen_bcmp(cstate, OR_LINKHDR, 8, 6, eaddr);
4281 return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
4284 b0 = gen_thostop(cstate, eaddr, Q_SRC);
4285 b1 = gen_thostop(cstate, eaddr, Q_DST);
4291 b0 = gen_thostop(cstate, eaddr, Q_SRC);
4292 b1 = gen_thostop(cstate, eaddr, Q_DST);
4297 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4301 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4305 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4309 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4313 bpf_error(cstate, "'ra' is only supported on 802.11");
4317 bpf_error(cstate, "'ta' is only supported on 802.11");
4329 gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4341 cstate->no_optimize = 1;
4371 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4372 b1 = new_block(cstate, JMP(BPF_JSET));
4379 b0 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
4386 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4387 b2 = new_block(cstate, JMP(BPF_JSET));
4395 b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4409 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4410 b1 = new_block(cstate, JMP(BPF_JSET));
4418 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4419 b2 = new_block(cstate, JMP(BPF_JSET));
4427 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4441 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4442 b1 = new_block(cstate, JMP(BPF_JSET));
4456 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4457 b2 = new_block(cstate, JMP(BPF_JSET));
4465 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4483 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4484 b1 = new_block(cstate, JMP(BPF_JSET));
4518 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4519 b1 = new_block(cstate, JMP(BPF_JSET));
4526 b0 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4533 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4534 b2 = new_block(cstate, JMP(BPF_JSET));
4542 b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4555 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4556 b1 = new_block(cstate, JMP(BPF_JSET));
4570 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4571 b2 = new_block(cstate, JMP(BPF_JSET));
4579 b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4597 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4598 b1 = new_block(cstate, JMP(BPF_JSET));
4611 b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
4612 b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
4618 b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
4619 b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
4627 return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr));
4633 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4636 b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4639 b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4644 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4652 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4655 b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4666 b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B,
4668 b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
4683 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4684 b1 = new_block(cstate, JMP(BPF_JSET));
4691 b0 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4708 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4711 b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4714 b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4725 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4726 b1 = new_block(cstate, JMP(BPF_JSET));
4739 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4753 gen_ipfchostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4759 return gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4762 return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
4765 b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
4766 b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
4772 b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
4773 b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
4778 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4782 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4786 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4790 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4794 bpf_error(cstate, "'ra' is only supported on 802.11");
4798 bpf_error(cstate, "'ta' is only supported on 802.11");
4824 gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir)
4844 b0 = gen_dnhostop(cstate, addr, Q_SRC);
4845 b1 = gen_dnhostop(cstate, addr, Q_DST);
4852 b0 = gen_dnhostop(cstate, addr, Q_SRC);
4853 b1 = gen_dnhostop(cstate, addr, Q_DST);
4858 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4862 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4866 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4870 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4874 bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4878 bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4885 b0 = gen_linktype(cstate, ETHERTYPE_DN);
4887 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
4889 b1 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_lh,
4893 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x06,
4895 b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_lh, BPF_H,
4900 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
4902 b2 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_sh, BPF_H,
4907 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x02,
4909 b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_sh, BPF_H,
4914 /* Combine with test for cstate->linktype */
4925 gen_mpls_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
4933 b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
4935 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x40, 0xf0);
4941 b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
4943 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x60, 0xf0);
4949 bpf_error(cstate, "unsupported protocol over mpls");
4955 gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
4969 b0 = gen_host(cstate, addr, mask, Q_IP, dir, type);
4974 if (cstate->label_stack_depth == 0) {
4975 b1 = gen_host(cstate, addr, mask, Q_ARP, dir, type);
4977 b0 = gen_host(cstate, addr, mask, Q_RARP, dir, type);
4983 bpf_error(cstate, "link-layer modifier applied to %s", typestr);
4986 return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_IP, 12, 16);
4989 return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
4992 return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_ARP, 14, 24);
4995 bpf_error(cstate, "'sctp' modifier applied to %s", typestr);
4998 bpf_error(cstate, "'tcp' modifier applied to %s", typestr);
5001 bpf_error(cstate, "'udp' modifier applied to %s", typestr);
5004 bpf_error(cstate, "'icmp' modifier applied to %s", typestr);
5007 bpf_error(cstate, "'igmp' modifier applied to %s", typestr);
5010 bpf_error(cstate, "'igrp' modifier applied to %s", typestr);
5013 bpf_error(cstate, "AppleTalk host filtering not implemented");
5016 return gen_dnhostop(cstate, addr, dir);
5019 bpf_error(cstate, "LAT host filtering not implemented");
5022 bpf_error(cstate, "SCA host filtering not implemented");
5025 bpf_error(cstate, "MOPRC host filtering not implemented");
5028 bpf_error(cstate, "MOPDL host filtering not implemented");
5031 bpf_error(cstate, "'ip6' modifier applied to ip host");
5034 bpf_error(cstate, "'icmp6' modifier applied to %s", typestr);
5037 bpf_error(cstate, "'ah' modifier applied to %s", typestr);
5040 bpf_error(cstate, "'esp' modifier applied to %s", typestr);
5043 bpf_error(cstate, "'pim' modifier applied to %s", typestr);
5046 bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);
5049 bpf_error(cstate, "AARP host filtering not implemented");
5052 bpf_error(cstate, "ISO host filtering not implemented");
5055 bpf_error(cstate, "'esis' modifier applied to %s", typestr);
5058 bpf_error(cstate, "'isis' modifier applied to %s", typestr);
5061 bpf_error(cstate, "'clnp' modifier applied to %s", typestr);
5064 bpf_error(cstate, "'stp' modifier applied to %s", typestr);
5067 bpf_error(cstate, "IPX host filtering not implemented");
5070 bpf_error(cstate, "'netbeui' modifier applied to %s", typestr);
5073 bpf_error(cstate, "'l1' modifier applied to %s", typestr);
5076 bpf_error(cstate, "'l2' modifier applied to %s", typestr);
5079 bpf_error(cstate, "'iih' modifier applied to %s", typestr);
5082 bpf_error(cstate, "'snp' modifier applied to %s", typestr);
5085 bpf_error(cstate, "'csnp' modifier applied to %s", typestr);
5088 bpf_error(cstate, "'psnp' modifier applied to %s", typestr);
5091 bpf_error(cstate, "'lsp' modifier applied to %s", typestr);
5094 bpf_error(cstate, "'radio' modifier applied to %s", typestr);
5097 bpf_error(cstate, "'carp' modifier applied to %s", typestr);
5107 gen_host6(compiler_state_t *cstate, struct in6_addr *addr,
5120 return gen_host6(cstate, addr, mask, Q_IPV6, dir, type);
5123 bpf_error(cstate, "link-layer modifier applied to ip6 %s", typestr);
5126 bpf_error(cstate, "'ip' modifier applied to ip6 %s", typestr);
5129 bpf_error(cstate, "'rarp' modifier applied to ip6 %s", typestr);
5132 bpf_error(cstate, "'arp' modifier applied to ip6 %s", typestr);
5135 bpf_error(cstate, "'sctp' modifier applied to ip6 %s", typestr);
5138 bpf_error(cstate, "'tcp' modifier applied to ip6 %s", typestr);
5141 bpf_error(cstate, "'udp' modifier applied to ip6 %s", typestr);
5144 bpf_error(cstate, "'icmp' modifier applied to ip6 %s", typestr);
5147 bpf_error(cstate, "'igmp' modifier applied to ip6 %s", typestr);
5150 bpf_error(cstate, "'igrp' modifier applied to ip6 %s", typestr);
5153 bpf_error(cstate, "AppleTalk modifier applied to ip6 %s", typestr);
5156 bpf_error(cstate, "'decnet' modifier applied to ip6 %s", typestr);
5159 bpf_error(cstate, "'lat' modifier applied to ip6 %s", typestr);
5162 bpf_error(cstate, "'sca' modifier applied to ip6 %s", typestr);
5165 bpf_error(cstate, "'moprc' modifier applied to ip6 %s", typestr);
5168 bpf_error(cstate, "'mopdl' modifier applied to ip6 %s", typestr);
5171 return gen_hostop6(cstate, addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
5174 bpf_error(cstate, "'icmp6' modifier applied to ip6 %s", typestr);
5177 bpf_error(cstate, "'ah' modifier applied to ip6 %s", typestr);
5180 bpf_error(cstate, "'esp' modifier applied to ip6 %s", typestr);
5183 bpf_error(cstate, "'pim' modifier applied to ip6 %s", typestr);
5186 bpf_error(cstate, "'vrrp' modifier applied to ip6 %s", typestr);
5189 bpf_error(cstate, "'aarp' modifier applied to ip6 %s", typestr);
5192 bpf_error(cstate, "'iso' modifier applied to ip6 %s", typestr);
5195 bpf_error(cstate, "'esis' modifier applied to ip6 %s", typestr);
5198 bpf_error(cstate, "'isis' modifier applied to ip6 %s", typestr);
5201 bpf_error(cstate, "'clnp' modifier applied to ip6 %s", typestr);
5204 bpf_error(cstate, "'stp' modifier applied to ip6 %s", typestr);
5207 bpf_error(cstate, "'ipx' modifier applied to ip6 %s", typestr);
5210 bpf_error(cstate, "'netbeui' modifier applied to ip6 %s", typestr);
5213 bpf_error(cstate, "'l1' modifier applied to ip6 %s", typestr);
5216 bpf_error(cstate, "'l2' modifier applied to ip6 %s", typestr);
5219 bpf_error(cstate, "'iih' modifier applied to ip6 %s", typestr);
5222 bpf_error(cstate, "'snp' modifier applied to ip6 %s", typestr);
5225 bpf_error(cstate, "'csnp' modifier applied to ip6 %s", typestr);
5228 bpf_error(cstate, "'psnp' modifier applied to ip6 %s", typestr);
5231 bpf_error(cstate, "'lsp' modifier applied to ip6 %s", typestr);
5234 bpf_error(cstate, "'radio' modifier applied to ip6 %s", typestr);
5237 bpf_error(cstate, "'carp' modifier applied to ip6 %s", typestr);
5248 gen_gateway(compiler_state_t *cstate, const u_char *eaddr,
5256 bpf_error(cstate, "direction applied to 'gateway'");
5263 switch (cstate->linktype) {
5267 b1 = gen_prevlinkhdr_check(cstate);
5268 b0 = gen_ehostop(cstate, eaddr, Q_OR);
5273 b0 = gen_fhostop(cstate, eaddr, Q_OR);
5276 b0 = gen_thostop(cstate, eaddr, Q_OR);
5283 b0 = gen_wlanhostop(cstate, eaddr, Q_OR);
5288 * LANE, cstate->linktype would have been set to
5291 bpf_error(cstate,
5295 b0 = gen_ipfchostop(cstate, eaddr, Q_OR);
5298 bpf_error(cstate,
5315 tmp = gen_host(cstate,
5348 bpf_error(cstate, "illegal modifier of 'gateway'");
5354 gen_proto_abbrev_internal(compiler_state_t *cstate, int proto)
5362 b1 = gen_proto(cstate, IPPROTO_SCTP, Q_DEFAULT, Q_DEFAULT);
5366 b1 = gen_proto(cstate, IPPROTO_TCP, Q_DEFAULT, Q_DEFAULT);
5370 b1 = gen_proto(cstate, IPPROTO_UDP, Q_DEFAULT, Q_DEFAULT);
5374 b1 = gen_proto(cstate, IPPROTO_ICMP, Q_IP, Q_DEFAULT);
5382 b1 = gen_proto(cstate, IPPROTO_IGMP, Q_IP, Q_DEFAULT);
5389 b1 = gen_proto(cstate, IPPROTO_IGRP, Q_IP, Q_DEFAULT);
5397 b1 = gen_proto(cstate, IPPROTO_PIM, Q_DEFAULT, Q_DEFAULT);
5405 b1 = gen_proto(cstate, IPPROTO_VRRP, Q_IP, Q_DEFAULT);
5413 b1 = gen_proto(cstate, IPPROTO_CARP, Q_IP, Q_DEFAULT);
5417 b1 = gen_linktype(cstate, ETHERTYPE_IP);
5421 b1 = gen_linktype(cstate, ETHERTYPE_ARP);
5425 b1 = gen_linktype(cstate, ETHERTYPE_REVARP);
5429 bpf_error(cstate, "link layer applied in wrong context");
5432 b1 = gen_linktype(cstate, ETHERTYPE_ATALK);
5436 b1 = gen_linktype(cstate, ETHERTYPE_AARP);
5440 b1 = gen_linktype(cstate, ETHERTYPE_DN);
5444 b1 = gen_linktype(cstate, ETHERTYPE_SCA);
5448 b1 = gen_linktype(cstate, ETHERTYPE_LAT);
5452 b1 = gen_linktype(cstate, ETHERTYPE_MOPDL);
5456 b1 = gen_linktype(cstate, ETHERTYPE_MOPRC);
5460 b1 = gen_linktype(cstate, ETHERTYPE_IPV6);
5467 b1 = gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
5474 b1 = gen_proto(cstate, IPPROTO_AH, Q_DEFAULT, Q_DEFAULT);
5481 b1 = gen_proto(cstate, IPPROTO_ESP, Q_DEFAULT, Q_DEFAULT);
5485 b1 = gen_linktype(cstate, LLCSAP_ISONS);
5489 b1 = gen_proto(cstate, ISO9542_ESIS, Q_ISO, Q_DEFAULT);
5493 b1 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5497 b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
5498 b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
5500 b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
5502 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5504 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5509 b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
5510 b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
5512 b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
5514 b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5516 b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5521 b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
5522 b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
5524 b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
5529 b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
5530 b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
5535 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5536 b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5538 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5540 b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5545 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5546 b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5551 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5552 b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5557 b1 = gen_proto(cstate, ISO8473_CLNP, Q_ISO, Q_DEFAULT);
5561 b1 = gen_linktype(cstate, LLCSAP_8021D);
5565 b1 = gen_linktype(cstate, LLCSAP_IPX);
5569 b1 = gen_linktype(cstate, LLCSAP_NETBEUI);
5573 bpf_error(cstate, "'radio' is not a valid protocol type");
5582 gen_proto_abbrev(compiler_state_t *cstate, int proto)
5588 if (setjmp(cstate->top_ctx))
5591 return gen_proto_abbrev_internal(cstate, proto);
5595 gen_ipfrag(compiler_state_t *cstate)
5601 s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H);
5602 b = new_block(cstate, JMP(BPF_JSET));
5620 gen_portatom(compiler_state_t *cstate, int off, bpf_u_int32 v)
5622 return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v);
5626 gen_portatom6(compiler_state_t *cstate, int off, bpf_u_int32 v)
5628 return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v);
5632 gen_portop(compiler_state_t *cstate, u_int port, u_int proto, int dir)
5637 tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);
5638 b0 = gen_ipfrag(cstate);
5643 b1 = gen_portatom(cstate, 0, port);
5647 b1 = gen_portatom(cstate, 2, port);
5651 tmp = gen_portatom(cstate, 0, port);
5652 b1 = gen_portatom(cstate, 2, port);
5658 tmp = gen_portatom(cstate, 0, port);
5659 b1 = gen_portatom(cstate, 2, port);
5664 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for ports");
5668 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for ports");
5672 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for ports");
5676 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for ports");
5680 bpf_error(cstate, "'ra' is not a valid qualifier for ports");
5684 bpf_error(cstate, "'ta' is not a valid qualifier for ports");
5697 gen_port(compiler_state_t *cstate, u_int port, int ip_proto, int dir)
5718 b0 = gen_linktype(cstate, ETHERTYPE_IP);
5724 b1 = gen_portop(cstate, port, (u_int)ip_proto, dir);
5728 tmp = gen_portop(cstate, port, IPPROTO_TCP, dir);
5729 b1 = gen_portop(cstate, port, IPPROTO_UDP, dir);
5731 tmp = gen_portop(cstate, port, IPPROTO_SCTP, dir);
5743 gen_portop6(compiler_state_t *cstate, u_int port, u_int proto, int dir)
5749 b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);
5753 b1 = gen_portatom6(cstate, 0, port);
5757 b1 = gen_portatom6(cstate, 2, port);
5761 tmp = gen_portatom6(cstate, 0, port);
5762 b1 = gen_portatom6(cstate, 2, port);
5768 tmp = gen_portatom6(cstate, 0, port);
5769 b1 = gen_portatom6(cstate, 2, port);
5782 gen_port6(compiler_state_t *cstate, u_int port, int ip_proto, int dir)
5787 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
5793 b1 = gen_portop6(cstate, port, (u_int)ip_proto, dir);
5797 tmp = gen_portop6(cstate, port, IPPROTO_TCP, dir);
5798 b1 = gen_portop6(cstate, port, IPPROTO_UDP, dir);
5800 tmp = gen_portop6(cstate, port, IPPROTO_SCTP, dir);
5813 gen_portrangeatom(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,
5829 b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, v1);
5830 b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, v2);
5838 gen_portrangeop(compiler_state_t *cstate, u_int port1, u_int port2,
5844 tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);
5845 b0 = gen_ipfrag(cstate);
5850 b1 = gen_portrangeatom(cstate, 0, port1, port2);
5854 b1 = gen_portrangeatom(cstate, 2, port1, port2);
5858 tmp = gen_portrangeatom(cstate, 0, port1, port2);
5859 b1 = gen_portrangeatom(cstate, 2, port1, port2);
5865 tmp = gen_portrangeatom(cstate, 0, port1, port2);
5866 b1 = gen_portrangeatom(cstate, 2, port1, port2);
5871 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5875 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5879 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5883 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5887 bpf_error(cstate, "'ra' is not a valid qualifier for port ranges");
5891 bpf_error(cstate, "'ta' is not a valid qualifier for port ranges");
5904 gen_portrange(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,
5910 b0 = gen_linktype(cstate, ETHERTYPE_IP);
5916 b1 = gen_portrangeop(cstate, port1, port2, (bpf_u_int32)ip_proto,
5921 tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_TCP, dir);
5922 b1 = gen_portrangeop(cstate, port1, port2, IPPROTO_UDP, dir);
5924 tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_SCTP, dir);
5936 gen_portrangeatom6(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,
5952 b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, v1);
5953 b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, v2);
5961 gen_portrangeop6(compiler_state_t *cstate, u_int port1, u_int port2,
5968 b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);
5972 b1 = gen_portrangeatom6(cstate, 0, port1, port2);
5976 b1 = gen_portrangeatom6(cstate, 2, port1, port2);
5980 tmp = gen_portrangeatom6(cstate, 0, port1, port2);
5981 b1 = gen_portrangeatom6(cstate, 2, port1, port2);
5987 tmp = gen_portrangeatom6(cstate, 0, port1, port2);
5988 b1 = gen_portrangeatom6(cstate, 2, port1, port2);
6001 gen_portrange6(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,
6007 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6013 b1 = gen_portrangeop6(cstate, port1, port2, (bpf_u_int32)ip_proto,
6018 tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_TCP, dir);
6019 b1 = gen_portrangeop6(cstate, port1, port2, IPPROTO_UDP, dir);
6021 tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_SCTP, dir);
6033 lookup_proto(compiler_state_t *cstate, const char *name, int proto)
6044 bpf_error(cstate, "unknown ip proto '%s'", name);
6048 /* XXX should look up h/w protocol type based on cstate->linktype */
6053 bpf_error(cstate, "unknown ether proto '%s'", name);
6065 bpf_error(cstate, "unknown osi proto '%s'", name);
6077 gen_protochain(compiler_state_t *cstate, bpf_u_int32 v, int proto)
6084 int reg2 = alloc_reg(cstate);
6094 b0 = gen_protochain(cstate, v, Q_IP);
6095 b = gen_protochain(cstate, v, Q_IPV6);
6099 bpf_error(cstate, "bad protocol applied for 'protochain'");
6114 if (cstate->off_linkpl.is_variable)
6115 bpf_error(cstate, "'protochain' not supported with variable length headers");
6128 cstate->no_optimize = 1;
6136 s[i] = new_stmt(cstate, 0); /*dummy*/
6141 b0 = gen_linktype(cstate, ETHERTYPE_IP);
6144 s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
6145 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 9;
6148 s[i] = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
6149 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6154 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6157 s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
6158 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 6;
6161 s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM);
6167 bpf_error(cstate, "unsupported proto to gen_protochain");
6173 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6184 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6197 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6204 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6210 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6216 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6233 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6234 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6237 s[i] = new_stmt(cstate, BPF_ST);
6241 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6242 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1;
6245 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6249 s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
6253 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
6257 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6260 s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
6265 s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
6275 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6284 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6299 s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
6302 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6303 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6306 s[i] = new_stmt(cstate, BPF_ST);
6310 s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
6313 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6317 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6320 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6321 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6324 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6328 s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
6332 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6335 s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
6340 s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
6346 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6364 b = new_block(cstate, JMP(BPF_JEQ));
6368 free_reg(cstate, reg2);
6376 gen_check_802_11_data_frame(compiler_state_t *cstate)
6385 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
6386 b0 = new_block(cstate, JMP(BPF_JSET));
6390 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
6391 b1 = new_block(cstate, JMP(BPF_JSET));
6411 gen_proto(compiler_state_t *cstate, bpf_u_int32 v, int proto, int dir)
6417 bpf_error(cstate, "direction applied to 'proto'");
6421 b0 = gen_proto(cstate, v, Q_IP, dir);
6422 b1 = gen_proto(cstate, v, Q_IPV6, dir);
6427 return gen_linktype(cstate, v);
6445 b0 = gen_linktype(cstate, ETHERTYPE_IP);
6446 b1 = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, v);
6451 bpf_error(cstate, "arp does not encapsulate another protocol");
6455 bpf_error(cstate, "rarp does not encapsulate another protocol");
6459 bpf_error(cstate, "'sctp proto' is bogus");
6463 bpf_error(cstate, "'tcp proto' is bogus");
6467 bpf_error(cstate, "'udp proto' is bogus");
6471 bpf_error(cstate, "'icmp proto' is bogus");
6475 bpf_error(cstate, "'igmp proto' is bogus");
6479 bpf_error(cstate, "'igrp proto' is bogus");
6483 bpf_error(cstate, "AppleTalk encapsulation is not specifiable");
6487 bpf_error(cstate, "DECNET encapsulation is not specifiable");
6491 bpf_error(cstate, "LAT does not encapsulate another protocol");
6495 bpf_error(cstate, "SCA does not encapsulate another protocol");
6499 bpf_error(cstate, "MOPRC does not encapsulate another protocol");
6503 bpf_error(cstate, "MOPDL does not encapsulate another protocol");
6507 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6512 b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, IPPROTO_FRAGMENT);
6513 b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, v);
6515 b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, v);
6521 bpf_error(cstate, "'icmp6 proto' is bogus");
6525 bpf_error(cstate, "'ah proto' is bogus");
6529 bpf_error(cstate, "'esp proto' is bogus");
6533 bpf_error(cstate, "'pim proto' is bogus");
6537 bpf_error(cstate, "'vrrp proto' is bogus");
6541 bpf_error(cstate, "'aarp proto' is bogus");
6545 switch (cstate->linktype) {
6550 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6566 return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v);
6575 b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS);
6577 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, v);
6582 b0 = gen_linktype(cstate, LLCSAP_ISONS);
6583 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, v);
6589 bpf_error(cstate, "'esis proto' is bogus");
6593 b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
6598 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, v);
6603 bpf_error(cstate, "'clnp proto' is not supported");
6607 bpf_error(cstate, "'stp proto' is bogus");
6611 bpf_error(cstate, "'ipx proto' is bogus");
6615 bpf_error(cstate, "'netbeui proto' is bogus");
6619 bpf_error(cstate, "'l1 proto' is bogus");
6623 bpf_error(cstate, "'l2 proto' is bogus");
6627 bpf_error(cstate, "'iih proto' is bogus");
6631 bpf_error(cstate, "'snp proto' is bogus");
6635 bpf_error(cstate, "'csnp proto' is bogus");
6639 bpf_error(cstate, "'psnp proto' is bogus");
6643 bpf_error(cstate, "'lsp proto' is bogus");
6647 bpf_error(cstate, "'radio proto' is bogus");
6651 bpf_error(cstate, "'carp proto' is bogus");
6662 gen_scode(compiler_state_t *cstate, const char *name, struct qual q)
6684 if (setjmp(cstate->top_ctx))
6692 bpf_error(cstate, "unknown network '%s'", name);
6699 return gen_host(cstate, addr, mask, proto, dir, q.addr);
6704 switch (cstate->linktype) {
6711 bpf_error(cstate,
6713 tmp = gen_prevlinkhdr_check(cstate);
6714 b = gen_ehostop(cstate, eaddr, dir);
6723 bpf_error(cstate,
6725 b = gen_fhostop(cstate, eaddr, dir);
6732 bpf_error(cstate,
6734 b = gen_thostop(cstate, eaddr, dir);
6745 bpf_error(cstate,
6747 b = gen_wlanhostop(cstate, eaddr, dir);
6754 bpf_error(cstate,
6756 b = gen_ipfchostop(cstate, eaddr, dir);
6761 bpf_error(cstate, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6767 bpf_error(cstate, "unknown decnet host name '%s'\n", name);
6769 bpf_error(cstate, "decnet name support not included, '%s' cannot be translated\n",
6777 return (gen_host(cstate, dn_addr, 0, proto, dir, q.addr));
6784 bpf_error(cstate, "unknown host '%s'", name);
6785 cstate->ai = res;
6791 if (cstate->off_linktype.constant_part == OFFSET_NOT_SET &&
6808 tmp = gen_host(cstate, ntohl(sin4->sin_addr.s_addr),
6818 tmp = gen_host6(cstate, &sin6->sin6_addr,
6829 cstate->ai = NULL;
6832 bpf_error(cstate, "unknown host '%s'%s", name,
6843 bpf_error(cstate, "illegal qualifier of 'port'");
6845 bpf_error(cstate, "unknown port '%s'", name);
6848 bpf_error(cstate, "port '%s' is tcp", name);
6850 bpf_error(cstate, "port '%s' is sctp", name);
6857 bpf_error(cstate, "port '%s' is udp", name);
6860 bpf_error(cstate, "port '%s' is sctp", name);
6867 bpf_error(cstate, "port '%s' is udp", name);
6870 bpf_error(cstate, "port '%s' is tcp", name);
6876 bpf_error(cstate, "illegal port number %d < 0", port);
6878 bpf_error(cstate, "illegal port number %d > 65535", port);
6879 b = gen_port(cstate, port, real_proto, dir);
6880 gen_or(gen_port6(cstate, port, real_proto, dir), b);
6886 bpf_error(cstate, "illegal qualifier of 'portrange'");
6888 bpf_error(cstate, "unknown port in range '%s'", name);
6891 bpf_error(cstate, "port in range '%s' is tcp", name);
6893 bpf_error(cstate, "port in range '%s' is sctp", name);
6900 bpf_error(cstate, "port in range '%s' is udp", name);
6902 bpf_error(cstate, "port in range '%s' is sctp", name);
6909 bpf_error(cstate, "port in range '%s' is udp", name);
6911 bpf_error(cstate, "port in range '%s' is tcp", name);
6917 bpf_error(cstate, "illegal port number %d < 0", port1);
6919 bpf_error(cstate, "illegal port number %d > 65535", port1);
6921 bpf_error(cstate, "illegal port number %d < 0", port2);
6923 bpf_error(cstate, "illegal port number %d > 65535", port2);
6925 b = gen_portrange(cstate, port1, port2, real_proto, dir);
6926 gen_or(gen_portrange6(cstate, port1, port2, real_proto, dir), b);
6933 bpf_error(cstate, "unknown ether host: %s", name);
6936 cstate->ai = res;
6938 bpf_error(cstate, "unknown host '%s'", name);
6939 b = gen_gateway(cstate, eaddr, res, proto, dir);
6940 cstate->ai = NULL;
6943 bpf_error(cstate, "unknown host '%s'", name);
6946 bpf_error(cstate, "'gateway' not supported in this configuration");
6950 real_proto = lookup_proto(cstate, name, proto);
6952 return gen_proto(cstate, real_proto, proto, dir);
6954 bpf_error(cstate, "unknown protocol: %s", name);
6958 real_proto = lookup_proto(cstate, name, proto);
6960 return gen_protochain(cstate, real_proto, proto);
6962 bpf_error(cstate, "unknown protocol: %s", name);
6966 syntax(cstate);
6974 gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2,
6984 if (setjmp(cstate->top_ctx))
6989 bpf_error(cstate, "invalid IPv4 address '%s'", s1);
6996 bpf_error(cstate, "invalid IPv4 address '%s'", s2);
7000 bpf_error(cstate, "non-network bits set in \"%s mask %s\"",
7005 bpf_error(cstate, "mask length must be <= 32");
7015 bpf_error(cstate, "non-network bits set in \"%s/%d\"",
7022 return gen_host(cstate, n, m, q.proto, q.dir, q.addr);
7025 bpf_error(cstate, "Mask syntax for networks only");
7032 gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q)
7043 if (setjmp(cstate->top_ctx))
7053 bpf_error(cstate, "malformed decnet address '%s'", s);
7057 bpf_error(cstate, "invalid IPv4 address '%s'", s);
7066 return gen_host(cstate, v, 0, proto, dir, q.addr);
7068 bpf_error(cstate, "illegal link layer address");
7082 return gen_host(cstate, v, mask, proto, dir, q.addr);
7095 bpf_error(cstate, "illegal qualifier of 'port'");
7098 bpf_error(cstate, "illegal port number %u > 65535", v);
7102 b = gen_port(cstate, v, proto, dir);
7103 gen_or(gen_port6(cstate, v, proto, dir), b);
7117 bpf_error(cstate, "illegal qualifier of 'portrange'");
7120 bpf_error(cstate, "illegal port number %u > 65535", v);
7124 b = gen_portrange(cstate, v, v, proto, dir);
7125 gen_or(gen_portrange6(cstate, v, v, proto, dir), b);
7130 bpf_error(cstate, "'gateway' requires a name");
7134 return gen_proto(cstate, v, proto, dir);
7138 return gen_protochain(cstate, v, proto);
7142 syntax(cstate);
7154 gen_mcode6(compiler_state_t *cstate, const char *s1, const char *s2,
7167 if (setjmp(cstate->top_ctx))
7171 bpf_error(cstate, "no mask %s supported", s2);
7175 bpf_error(cstate, "invalid ip6 address %s", s1);
7176 cstate->ai = res;
7178 bpf_error(cstate, "%s resolved to multiple address", s1);
7182 bpf_error(cstate, "mask length must be <= %u", (unsigned int)(sizeof(mask.s6_addr) * 8));
7194 bpf_error(cstate, "non-network bits set in \"%s/%d\"", s1, masklen);
7202 bpf_error(cstate, "Mask syntax for networks only");
7206 b = gen_host6(cstate, addr, &mask, q.proto, q.dir, q.addr);
7207 cstate->ai = NULL;
7212 bpf_error(cstate, "invalid qualifier against IPv6 address");
7219 gen_ecode(compiler_state_t *cstate, const char *s, struct qual q)
7227 if (setjmp(cstate->top_ctx))
7231 cstate->e = pcap_ether_aton(s);
7232 if (cstate->e == NULL)
7233 bpf_error(cstate, "malloc");
7234 switch (cstate->linktype) {
7238 tmp = gen_prevlinkhdr_check(cstate);
7239 b = gen_ehostop(cstate, cstate->e, (int)q.dir);
7244 b = gen_fhostop(cstate, cstate->e, (int)q.dir);
7247 b = gen_thostop(cstate, cstate->e, (int)q.dir);
7254 b = gen_wlanhostop(cstate, cstate->e, (int)q.dir);
7257 b = gen_ipfchostop(cstate, cstate->e, (int)q.dir);
7260 free(cstate->e);
7261 cstate->e = NULL;
7262 bpf_error(cstate, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7265 free(cstate->e);
7266 cstate->e = NULL;
7269 bpf_error(cstate, "ethernet address used in non-ether expression");
7286 xfer_to_x(compiler_state_t *cstate, struct arth *a)
7290 s = new_stmt(cstate, BPF_LDX|BPF_MEM);
7296 xfer_to_a(compiler_state_t *cstate, struct arth *a)
7300 s = new_stmt(cstate, BPF_LD|BPF_MEM);
7313 gen_load_internal(compiler_state_t *cstate, int proto, struct arth *inst,
7319 int regno = alloc_reg(cstate);
7321 free_reg(cstate, inst->regno);
7325 bpf_error(cstate, "data size must be 1, 2, or 4");
7342 bpf_error(cstate, "unsupported index operation");
7350 if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS &&
7351 cstate->linktype != DLT_IEEE802_11_RADIO &&
7352 cstate->linktype != DLT_PRISM_HEADER)
7353 bpf_error(cstate, "radio information not present in capture");
7359 s = xfer_to_x(cstate, inst);
7364 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7381 s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr);
7393 sappend(s, xfer_to_a(cstate, inst));
7394 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7395 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7397 s = xfer_to_x(cstate, inst);
7406 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7407 tmp->s.k = cstate->off_linkhdr.constant_part;
7426 * cstate->off_nl_nosnap?
7428 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
7440 sappend(s, xfer_to_a(cstate, inst));
7441 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7442 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7444 s = xfer_to_x(cstate, inst);
7453 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7454 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
7462 b = gen_proto_abbrev_internal(cstate, proto);
7486 * cstate->off_nl_nosnap?
7491 s = gen_loadx_iphdrlen(cstate);
7507 sappend(s, xfer_to_a(cstate, inst));
7508 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7509 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7510 sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code));
7511 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
7520 gen_and(gen_proto_abbrev_internal(cstate, proto), b = gen_ipfrag(cstate));
7523 gen_and(gen_proto_abbrev_internal(cstate, Q_IP), b);
7531 b = gen_proto_abbrev_internal(cstate, Q_IPV6);
7540 b = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, 58);
7547 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
7558 sappend(s, xfer_to_a(cstate, inst));
7559 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7560 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7562 s = xfer_to_x(cstate, inst);
7572 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7573 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40;
7581 s = new_stmt(cstate, BPF_ST);
7589 gen_load(compiler_state_t *cstate, int proto, struct arth *inst,
7596 if (setjmp(cstate->top_ctx))
7599 return gen_load_internal(cstate, proto, inst, size);
7603 gen_relation_internal(compiler_state_t *cstate, int code, struct arth *a0,
7609 s0 = xfer_to_x(cstate, a1);
7610 s1 = xfer_to_a(cstate, a0);
7612 s2 = new_stmt(cstate, BPF_ALU|BPF_SUB|BPF_X);
7613 b = new_block(cstate, JMP(code));
7617 b = new_block(cstate, BPF_JMP|code|BPF_X);
7627 free_reg(cstate, a0->regno);
7628 free_reg(cstate, a1->regno);
7647 gen_relation(compiler_state_t *cstate, int code, struct arth *a0,
7654 if (setjmp(cstate->top_ctx))
7657 return gen_relation_internal(cstate, code, a0, a1, reversed);
7661 gen_loadlen(compiler_state_t *cstate)
7671 if (setjmp(cstate->top_ctx))
7674 regno = alloc_reg(cstate);
7675 a = (struct arth *)newchunk(cstate, sizeof(*a));
7676 s = new_stmt(cstate, BPF_LD|BPF_LEN);
7677 s->next = new_stmt(cstate, BPF_ST);
7686 gen_loadi_internal(compiler_state_t *cstate, bpf_u_int32 val)
7692 a = (struct arth *)newchunk(cstate, sizeof(*a));
7694 reg = alloc_reg(cstate);
7696 s = new_stmt(cstate, BPF_LD|BPF_IMM);
7698 s->next = new_stmt(cstate, BPF_ST);
7707 gen_loadi(compiler_state_t *cstate, bpf_u_int32 val)
7713 if (setjmp(cstate->top_ctx))
7716 return gen_loadi_internal(cstate, val);
7725 gen_neg(compiler_state_t *cstate, struct arth *a_arg)
7734 if (setjmp(cstate->top_ctx))
7737 s = xfer_to_a(cstate, a);
7739 s = new_stmt(cstate, BPF_ALU|BPF_NEG);
7742 s = new_stmt(cstate, BPF_ST);
7755 gen_arth(compiler_state_t *cstate, int code, struct arth *a0_arg,
7765 if (setjmp(cstate->top_ctx))
7777 bpf_error(cstate, "division by zero");
7780 bpf_error(cstate, "modulus by zero");
7783 bpf_error(cstate, "shift by more than 31 bits");
7785 s0 = xfer_to_x(cstate, a1);
7786 s1 = xfer_to_a(cstate, a0);
7787 s2 = new_stmt(cstate, BPF_ALU|BPF_X|code);
7794 free_reg(cstate, a0->regno);
7795 free_reg(cstate, a1->regno);
7797 s0 = new_stmt(cstate, BPF_ST);
7798 a0->regno = s0->s.k = alloc_reg(cstate);
7808 init_regs(compiler_state_t *cstate)
7810 cstate->curreg = 0;
7811 memset(cstate->regused, 0, sizeof cstate->regused);
7818 alloc_reg(compiler_state_t *cstate)
7823 if (cstate->regused[cstate->curreg])
7824 cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS;
7826 cstate->regused[cstate->curreg] = 1;
7827 return cstate->curreg;
7830 bpf_error(cstate, "too many registers needed to evaluate expression");
7839 free_reg(compiler_state_t *cstate, int n)
7841 cstate->regused[n] = 0;
7845 gen_len(compiler_state_t *cstate, int jmp, int n)
7850 s = new_stmt(cstate, BPF_LD|BPF_LEN);
7851 b = new_block(cstate, JMP(jmp));
7859 gen_greater(compiler_state_t *cstate, int n)
7865 if (setjmp(cstate->top_ctx))
7868 return gen_len(cstate, BPF_JGE, n);
7875 gen_less(compiler_state_t *cstate, int n)
7883 if (setjmp(cstate->top_ctx))
7886 b = gen_len(cstate, BPF_JGT, n);
7903 gen_byteop(compiler_state_t *cstate, int op, int idx, bpf_u_int32 val)
7912 if (setjmp(cstate->top_ctx))
7920 return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
7923 b = gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
7927 b = gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
7931 s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K);
7935 s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
7939 b = new_block(cstate, JMP(BPF_JEQ));
7949 gen_broadcast(compiler_state_t *cstate, int proto)
7959 if (setjmp(cstate->top_ctx))
7966 switch (cstate->linktype) {
7969 return gen_ahostop(cstate, abroadcast, Q_DST);
7973 b1 = gen_prevlinkhdr_check(cstate);
7974 b0 = gen_ehostop(cstate, ebroadcast, Q_DST);
7979 return gen_fhostop(cstate, ebroadcast, Q_DST);
7981 return gen_thostop(cstate, ebroadcast, Q_DST);
7987 return gen_wlanhostop(cstate, ebroadcast, Q_DST);
7989 return gen_ipfchostop(cstate, ebroadcast, Q_DST);
7991 bpf_error(cstate, "not a broadcast link");
8001 if (cstate->netmask == PCAP_NETMASK_UNKNOWN)
8002 bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported");
8003 b0 = gen_linktype(cstate, ETHERTYPE_IP);
8004 hostmask = ~cstate->netmask;
8005 b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, 0, hostmask);
8006 b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W,
8012 bpf_error(cstate, "only link-layer/IP broadcast filters supported");
8021 gen_mac_multicast(compiler_state_t *cstate, int offset)
8027 s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B);
8028 b0 = new_block(cstate, JMP(BPF_JSET));
8035 gen_multicast(compiler_state_t *cstate, int proto)
8044 if (setjmp(cstate->top_ctx))
8051 switch (cstate->linktype) {
8055 return gen_ahostop(cstate, abroadcast, Q_DST);
8059 b1 = gen_prevlinkhdr_check(cstate);
8061 b0 = gen_mac_multicast(cstate, 0);
8072 return gen_mac_multicast(cstate, 1);
8075 return gen_mac_multicast(cstate, 2);
8102 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
8103 b1 = new_block(cstate, JMP(BPF_JSET));
8110 b0 = gen_mac_multicast(cstate, 16);
8117 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
8118 b2 = new_block(cstate, JMP(BPF_JSET));
8126 b1 = gen_mac_multicast(cstate, 4);
8139 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8140 b1 = new_block(cstate, JMP(BPF_JSET));
8154 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8155 b2 = new_block(cstate, JMP(BPF_JSET));
8163 b1 = gen_mac_multicast(cstate, 4);
8181 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8182 b1 = new_block(cstate, JMP(BPF_JSET));
8194 b0 = gen_mac_multicast(cstate, 2);
8203 b0 = gen_linktype(cstate, ETHERTYPE_IP);
8204 b1 = gen_cmp_ge(cstate, OR_LINKPL, 16, BPF_B, 224);
8209 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
8210 b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, 255);
8214 bpf_error(cstate, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8219 gen_ifindex(compiler_state_t *cstate, int ifindex)
8227 if (setjmp(cstate->top_ctx))
8233 switch (cstate->linktype) {
8236 b0 = gen_cmp(cstate, OR_LINKHDR, 4, BPF_W, ifindex);
8246 if (cstate->bpf_pcap->rfile != NULL) {
8248 bpf_error(cstate, "ifindex not supported on %s when reading savefiles",
8249 pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8254 b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_IFINDEX, BPF_W,
8257 bpf_error(cstate, "ifindex not supported on %s",
8258 pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8275 gen_inbound(compiler_state_t *cstate, int dir)
8283 if (setjmp(cstate->top_ctx))
8289 switch (cstate->linktype) {
8291 b0 = gen_relation_internal(cstate, BPF_JEQ,
8292 gen_load_internal(cstate, Q_LINK, gen_loadi_internal(cstate, 0), 1),
8293 gen_loadi_internal(cstate, 0),
8300 b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_OUTBOUND);
8303 b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_INBOUND);
8309 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING);
8318 b0 = gen_cmp(cstate, OR_LINKHDR, 10, BPF_B, LINUX_SLL_OUTGOING);
8326 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B,
8333 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_OUT);
8336 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_IN);
8367 b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 0, 0x01);
8370 b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 1, 0x01);
8397 if (cstate->bpf_pcap->rfile != NULL) {
8399 bpf_error(cstate, "inbound/outbound not supported on %s when reading savefiles",
8400 pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8404 b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
8411 bpf_error(cstate, "inbound/outbound not supported on %s",
8412 pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8421 gen_pf_ifname(compiler_state_t *cstate, const char *ifname)
8430 if (setjmp(cstate->top_ctx))
8433 if (cstate->linktype != DLT_PFLOG) {
8434 bpf_error(cstate, "ifname supported only on PF linktype");
8440 bpf_error(cstate, "ifname interface names can only be %d characters",
8444 b0 = gen_bcmp(cstate, OR_LINKHDR, off, (u_int)strlen(ifname),
8451 gen_pf_ruleset(compiler_state_t *cstate, char *ruleset)
8459 if (setjmp(cstate->top_ctx))
8462 if (cstate->linktype != DLT_PFLOG) {
8463 bpf_error(cstate, "ruleset supported only on PF linktype");
8468 bpf_error(cstate, "ruleset names can only be %ld characters",
8473 b0 = gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset),
8480 gen_pf_rnr(compiler_state_t *cstate, int rnr)
8488 if (setjmp(cstate->top_ctx))
8491 if (cstate->linktype != DLT_PFLOG) {
8492 bpf_error(cstate, "rnr supported only on PF linktype");
8496 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W,
8503 gen_pf_srnr(compiler_state_t *cstate, int srnr)
8511 if (setjmp(cstate->top_ctx))
8514 if (cstate->linktype != DLT_PFLOG) {
8515 bpf_error(cstate, "srnr supported only on PF linktype");
8519 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W,
8526 gen_pf_reason(compiler_state_t *cstate, int reason)
8534 if (setjmp(cstate->top_ctx))
8537 if (cstate->linktype != DLT_PFLOG) {
8538 bpf_error(cstate, "reason supported only on PF linktype");
8542 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B,
8549 gen_pf_action(compiler_state_t *cstate, int action)
8557 if (setjmp(cstate->top_ctx))
8560 if (cstate->linktype != DLT_PFLOG) {
8561 bpf_error(cstate, "action supported only on PF linktype");
8565 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B,
8572 gen_p80211_type(compiler_state_t *cstate, bpf_u_int32 type, bpf_u_int32 mask)
8580 if (setjmp(cstate->top_ctx))
8583 switch (cstate->linktype) {
8589 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, type, mask);
8593 bpf_error(cstate, "802.11 link-layer types supported only on 802.11");
8601 gen_p80211_fcdir(compiler_state_t *cstate, bpf_u_int32 fcdir)
8609 if (setjmp(cstate->top_ctx))
8612 switch (cstate->linktype) {
8621 bpf_error(cstate, "frame direction supported only with 802.11 headers");
8625 b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, fcdir,
8632 gen_acode(compiler_state_t *cstate, const char *s, struct qual q)
8640 if (setjmp(cstate->top_ctx))
8643 switch (cstate->linktype) {
8649 cstate->e = pcap_ether_aton(s);
8650 if (cstate->e == NULL)
8651 bpf_error(cstate, "malloc");
8652 b = gen_ahostop(cstate, cstate->e, (int)q.dir);
8653 free(cstate->e);
8654 cstate->e = NULL;
8657 bpf_error(cstate, "ARCnet address used in non-arc expression");
8661 bpf_error(cstate, "aid supported only on ARCnet");
8667 gen_ahostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
8674 return gen_bcmp(cstate, OR_LINKHDR, 0, 1, eaddr);
8677 return gen_bcmp(cstate, OR_LINKHDR, 1, 1, eaddr);
8680 b0 = gen_ahostop(cstate, eaddr, Q_SRC);
8681 b1 = gen_ahostop(cstate, eaddr, Q_DST);
8687 b0 = gen_ahostop(cstate, eaddr, Q_SRC);
8688 b1 = gen_ahostop(cstate, eaddr, Q_DST);
8693 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
8697 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
8701 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
8705 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
8709 bpf_error(cstate, "'ra' is only supported on 802.11");
8713 bpf_error(cstate, "'ta' is only supported on 802.11");
8721 gen_vlan_tpid_test(compiler_state_t *cstate)
8726 b0 = gen_linktype(cstate, ETHERTYPE_8021Q);
8727 b1 = gen_linktype(cstate, ETHERTYPE_8021AD);
8730 b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ);
8737 gen_vlan_vid_test(compiler_state_t *cstate, bpf_u_int32 vlan_num)
8740 bpf_error(cstate, "VLAN tag %u greater than maximum %u",
8743 return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, vlan_num, 0x0fff);
8747 gen_vlan_no_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,
8752 b0 = gen_vlan_tpid_test(cstate);
8755 b1 = gen_vlan_vid_test(cstate, vlan_num);
8764 cstate->off_linkpl.constant_part += 4;
8765 cstate->off_linktype.constant_part += 4;
8773 gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off,
8781 off->reg = alloc_reg(cstate);
8783 s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
8786 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
8789 s2 = new_stmt(cstate, BPF_ST);
8799 gen_vlan_patch_tpid_test(compiler_state_t *cstate, struct block *b_tpid)
8805 cstate->is_vlan_vloffset = 1;
8806 gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s);
8807 gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s);
8819 gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid)
8824 s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8828 sjeq = new_stmt(cstate, JMP(BPF_JEQ));
8833 s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8845 s2 = new_stmt(cstate, JMP(BPF_JA));
8863 gen_vlan_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,
8871 s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8874 b0 = new_block(cstate, JMP(BPF_JEQ));
8888 b_tpid = gen_vlan_tpid_test(cstate);
8890 b_vid = gen_vlan_vid_test(cstate, vlan_num);
8892 gen_vlan_patch_tpid_test(cstate, b_tpid);
8897 gen_vlan_patch_vid_test(cstate, b_vid);
8910 gen_vlan(compiler_state_t *cstate, bpf_u_int32 vlan_num, int has_vlan_tag)
8918 if (setjmp(cstate->top_ctx))
8922 if (cstate->label_stack_depth > 0)
8923 bpf_error(cstate, "no VLAN match after MPLS");
8956 switch (cstate->linktype) {
8964 if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable &&
8965 cstate->off_linkhdr.constant_part ==
8966 cstate->off_outermostlinkhdr.constant_part) {
8970 if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING)
8971 b0 = gen_vlan_bpf_extensions(cstate, vlan_num,
8974 b0 = gen_vlan_no_bpf_extensions(cstate,
8978 b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num,
8986 b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, has_vlan_tag);
8990 bpf_error(cstate, "no VLAN support for %s",
8991 pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8995 cstate->vlan_stack_depth++;
9008 gen_mpls(compiler_state_t *cstate, bpf_u_int32 label_num_arg,
9018 if (setjmp(cstate->top_ctx))
9021 if (cstate->label_stack_depth > 0) {
9023 b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01);
9029 switch (cstate->linktype) {
9036 b0 = gen_linktype(cstate, ETHERTYPE_MPLS);
9040 b0 = gen_linktype(cstate, PPP_MPLS_UCAST);
9048 bpf_error(cstate, "no MPLS support for %s",
9049 pcap_datalink_val_to_description_or_dlt(cstate->linktype));
9057 bpf_error(cstate, "MPLS label %u greater than maximum %u",
9061 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, label_num,
9081 cstate->off_nl_nosnap += 4;
9082 cstate->off_nl += 4;
9083 cstate->label_stack_depth++;
9091 gen_pppoed(compiler_state_t *cstate)
9097 if (setjmp(cstate->top_ctx))
9101 return gen_linktype(cstate, ETHERTYPE_PPPOED);
9105 gen_pppoes(compiler_state_t *cstate, bpf_u_int32 sess_num, int has_sess_num)
9113 if (setjmp(cstate->top_ctx))
9119 b0 = gen_linktype(cstate, ETHERTYPE_PPPOES);
9124 bpf_error(cstate, "PPPoE session number %u greater than maximum %u",
9127 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, sess_num, 0x0000ffff);
9148 * it's 6 bytes past cstate->off_nl.
9150 PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable,
9151 cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */
9152 cstate->off_linkpl.reg);
9154 cstate->off_linktype = cstate->off_linkhdr;
9155 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2;
9157 cstate->off_nl = 0;
9158 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
9166 gen_geneve_check(compiler_state_t *cstate,
9172 b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST);
9177 b1 = gen_mcmp(cstate, offrel, 8, BPF_B, 0, 0xc0);
9183 bpf_error(cstate, "Geneve VNI %u greater than maximum %u",
9187 b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00);
9201 gen_geneve4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9206 b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni);
9209 s = gen_loadx_iphdrlen(cstate);
9211 s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
9217 b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9227 gen_geneve6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9232 b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni);
9236 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
9238 s1 = new_stmt(cstate, BPF_LD|BPF_IMM);
9242 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
9246 s = new_stmt(cstate, BPF_LD|BPF_IMM);
9253 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9256 b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9270 gen_geneve_offsets(compiler_state_t *cstate)
9279 s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9280 s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8;
9283 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9288 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9292 cstate->off_linktype.reg = alloc_reg(cstate);
9293 cstate->off_linktype.is_variable = 1;
9294 cstate->off_linktype.constant_part = 0;
9296 s1 = new_stmt(cstate, BPF_ST);
9297 s1->s.k = cstate->off_linktype.reg;
9303 s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
9307 s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
9311 s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
9316 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9321 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
9333 PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate));
9335 s1 = new_stmt(cstate, BPF_ST);
9336 s1->s.k = cstate->off_linkhdr.reg;
9345 cstate->no_optimize = 1;
9348 s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H);
9353 s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
9354 s1->s.k = cstate->off_linkhdr.reg;
9360 s_proto = new_stmt(cstate, JMP(BPF_JEQ));
9364 s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
9370 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9374 s1 = new_stmt(cstate, BPF_ST);
9375 s1->s.k = cstate->off_linktype.reg;
9380 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9385 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9389 cstate->off_linkpl.reg = alloc_reg(cstate);
9390 cstate->off_linkpl.is_variable = 1;
9391 cstate->off_linkpl.constant_part = 0;
9393 s1 = new_stmt(cstate, BPF_STX);
9394 s1->s.k = cstate->off_linkpl.reg;
9398 cstate->off_nl = 0;
9405 gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9414 if (setjmp(cstate->top_ctx))
9417 b0 = gen_geneve4(cstate, vni, has_vni);
9418 b1 = gen_geneve6(cstate, vni, has_vni);
9426 s = gen_geneve_offsets(cstate);
9428 b1 = gen_true(cstate);
9434 cstate->is_geneve = 1;
9442 gen_geneve_ll_check(compiler_state_t *cstate)
9453 s = new_stmt(cstate, BPF_LD|BPF_MEM);
9454 s->s.k = cstate->off_linkhdr.reg;
9456 s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
9457 s1->s.k = cstate->off_linkpl.reg;
9460 b0 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9469 gen_atmfield_code_internal(compiler_state_t *cstate, int atmfield,
9477 if (!cstate->is_atm)
9478 bpf_error(cstate, "'vpi' supported only on raw ATM");
9479 if (cstate->off_vpi == OFFSET_NOT_SET)
9481 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B,
9486 if (!cstate->is_atm)
9487 bpf_error(cstate, "'vci' supported only on raw ATM");
9488 if (cstate->off_vci == OFFSET_NOT_SET)
9490 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H,
9495 if (cstate->off_proto == OFFSET_NOT_SET)
9497 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,
9502 if (cstate->off_payload == OFFSET_NOT_SET)
9504 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, BPF_B,
9509 if (!cstate->is_atm)
9510 bpf_error(cstate, "'callref' supported only on raw ATM");
9511 if (cstate->off_proto == OFFSET_NOT_SET)
9513 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,
9524 gen_atmtype_metac(compiler_state_t *cstate)
9528 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9529 b1 = gen_atmfield_code_internal(cstate, A_VCI, 1, BPF_JEQ, 0);
9535 gen_atmtype_sc(compiler_state_t *cstate)
9539 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9540 b1 = gen_atmfield_code_internal(cstate, A_VCI, 5, BPF_JEQ, 0);
9546 gen_atmtype_llc(compiler_state_t *cstate)
9550 b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
9551 cstate->linktype = cstate->prevlinktype;
9556 gen_atmfield_code(compiler_state_t *cstate, int atmfield,
9563 if (setjmp(cstate->top_ctx))
9566 return gen_atmfield_code_internal(cstate, atmfield, jvalue, jtype,
9571 gen_atmtype_abbrev(compiler_state_t *cstate, int type)
9579 if (setjmp(cstate->top_ctx))
9586 if (!cstate->is_atm)
9587 bpf_error(cstate, "'metac' supported only on raw ATM");
9588 b1 = gen_atmtype_metac(cstate);
9593 if (!cstate->is_atm)
9594 bpf_error(cstate, "'bcc' supported only on raw ATM");
9595 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9596 b1 = gen_atmfield_code_internal(cstate, A_VCI, 2, BPF_JEQ, 0);
9602 if (!cstate->is_atm)
9603 bpf_error(cstate, "'oam4sc' supported only on raw ATM");
9604 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9605 b1 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
9611 if (!cstate->is_atm)
9612 bpf_error(cstate, "'oam4ec' supported only on raw ATM");
9613 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9614 b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
9620 if (!cstate->is_atm)
9621 bpf_error(cstate, "'sc' supported only on raw ATM");
9622 b1 = gen_atmtype_sc(cstate);
9627 if (!cstate->is_atm)
9628 bpf_error(cstate, "'ilmic' supported only on raw ATM");
9629 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9630 b1 = gen_atmfield_code_internal(cstate, A_VCI, 16, BPF_JEQ, 0);
9636 if (!cstate->is_atm)
9637 bpf_error(cstate, "'lane' supported only on raw ATM");
9638 b1 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
9648 PUSH_LINKHDR(cstate, DLT_EN10MB, 0,
9649 cstate->off_payload + 2, /* Ethernet header */
9651 cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
9652 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* Ethernet */
9653 cstate->off_nl = 0; /* Ethernet II */
9654 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
9659 if (!cstate->is_atm)
9660 bpf_error(cstate, "'llc' supported only on raw ATM");
9661 b1 = gen_atmtype_llc(cstate);
9678 gen_mtp2type_abbrev(compiler_state_t *cstate, int type)
9686 if (setjmp(cstate->top_ctx))
9692 if ( (cstate->linktype != DLT_MTP2) &&
9693 (cstate->linktype != DLT_ERF) &&
9694 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9695 bpf_error(cstate, "'fisu' supported only on MTP2");
9696 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9697 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9702 if ( (cstate->linktype != DLT_MTP2) &&
9703 (cstate->linktype != DLT_ERF) &&
9704 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9705 bpf_error(cstate, "'lssu' supported only on MTP2");
9706 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9708 b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9714 if ( (cstate->linktype != DLT_MTP2) &&
9715 (cstate->linktype != DLT_ERF) &&
9716 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9717 bpf_error(cstate, "'msu' supported only on MTP2");
9718 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9723 if ( (cstate->linktype != DLT_MTP2) &&
9724 (cstate->linktype != DLT_ERF) &&
9725 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9726 bpf_error(cstate, "'hfisu' supported only on MTP2_HSL");
9727 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9728 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9733 if ( (cstate->linktype != DLT_MTP2) &&
9734 (cstate->linktype != DLT_ERF) &&
9735 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9736 bpf_error(cstate, "'hlssu' supported only on MTP2_HSL");
9737 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9739 b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9745 if ( (cstate->linktype != DLT_MTP2) &&
9746 (cstate->linktype != DLT_ERF) &&
9747 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9748 bpf_error(cstate, "'hmsu' supported only on MTP2_HSL");
9749 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9765 gen_mtp3field_code(compiler_state_t *cstate, int mtp3field,
9780 if (setjmp(cstate->top_ctx))
9783 newoff_sio = cstate->off_sio;
9784 newoff_opc = cstate->off_opc;
9785 newoff_dpc = cstate->off_dpc;
9786 newoff_sls = cstate->off_sls;
9794 if (cstate->off_sio == OFFSET_NOT_SET)
9795 bpf_error(cstate, "'sio' supported only on SS7");
9798 bpf_error(cstate, "sio value %u too big; max value = 255",
9800 b0 = gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, 0xffffffffU,
9809 if (cstate->off_opc == OFFSET_NOT_SET)
9810 bpf_error(cstate, "'opc' supported only on SS7");
9813 bpf_error(cstate, "opc value %u too big; max value = 16383",
9824 b0 = gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W, 0x00c0ff0fU,
9833 if (cstate->off_dpc == OFFSET_NOT_SET)
9834 bpf_error(cstate, "'dpc' supported only on SS7");
9837 bpf_error(cstate, "dpc value %u too big; max value = 16383",
9846 b0 = gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_W, 0xff3f0000U,
9855 if (cstate->off_sls == OFFSET_NOT_SET)
9856 bpf_error(cstate, "'sls' supported only on SS7");
9859 bpf_error(cstate, "sls value %u too big; max value = 15",
9864 b0 = gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B, 0xf0U,
9875 gen_msg_abbrev(compiler_state_t *cstate, int type)
9886 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, SETUP, BPF_JEQ, 0);
9890 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
9894 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT, BPF_JEQ, 0);
9898 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
9902 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE, BPF_JEQ, 0);
9906 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
9916 gen_atmmulti_abbrev(compiler_state_t *cstate, int type)
9924 if (setjmp(cstate->top_ctx))
9930 if (!cstate->is_atm)
9931 bpf_error(cstate, "'oam' supported only on raw ATM");
9933 b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
9934 b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
9936 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9941 if (!cstate->is_atm)
9942 bpf_error(cstate, "'oamf4' supported only on raw ATM");
9944 b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
9945 b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
9947 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9956 if (!cstate->is_atm)
9957 bpf_error(cstate, "'connectmsg' supported only on raw ATM");
9958 b0 = gen_msg_abbrev(cstate, A_SETUP);
9959 b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
9961 b0 = gen_msg_abbrev(cstate, A_CONNECT);
9963 b0 = gen_msg_abbrev(cstate, A_CONNECTACK);
9965 b0 = gen_msg_abbrev(cstate, A_RELEASE);
9967 b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
9969 b0 = gen_atmtype_sc(cstate);
9974 if (!cstate->is_atm)
9975 bpf_error(cstate, "'metaconnect' supported only on raw ATM");
9976 b0 = gen_msg_abbrev(cstate, A_SETUP);
9977 b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
9979 b0 = gen_msg_abbrev(cstate, A_CONNECT);
9981 b0 = gen_msg_abbrev(cstate, A_RELEASE);
9983 b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
9985 b0 = gen_atmtype_metac(cstate);