Skip to main content

Packet marks

A mark is a 32-bit number the kernel keeps with a packet. Firewall rules set it and match it, ip rule picks a routing table by it, and a socket can put one on every packet it sends. Conntrack keeps a second 32-bit mark for each connection. b4 uses marks to tell the packets it sent itself from the ones it still has to handle, to send a set's traffic to its route or to its own listener, and to keep its own connections out of its routing rules and, for some of them, out of its packet processing.

Services that route by policy or proxy transparently use the same 32 bits: Xray and XKeen, sing-box and mihomo, XrayUI, podkop, Tailscale, WireGuard, mwan3. When two of them use the same bit, one can take the other's packets for its own, and a service that writes the whole mark erases the bits the others put there. The sections below list the bits b4 writes and reads, the routing rules that go with them, and the values some other router services use.

Reading a mark​

FormMeaning
0x5aef/0x27fffThe bits covered by 0x27fff equal 0x5aef; the other bits are not compared
0x8000/0x8000Bit 0x8000 is set, whatever the rest of the mark holds
fwmark 0x5aef/0x27fff lookup 119 in ip rulePackets whose mark matches 0x5aef/0x27fff are routed by table 119
fwmark 0x111 lookup 111 in ip ruleNo mask: the whole 32-bit mark has to equal 0x111

A rule that writes a mark with a mask changes only the bits under the mask and keeps the rest. In iptables that is MARK --set-xmark 0x5aef/0x27fff or CONNMARK --save-mark --nfmask 0x27fff --ctmask 0x27fff. nftables prints the same write as meta mark set meta mark & 0xfffddaef | 0x00005aef: the AND keeps every bit outside 0x27fff (nft folds the value's own bits into that constant), and the OR writes 0x5aef. A rule without a mask, such as nftables meta mark set 0x00021546 or iptables MARK --set-mark 0x111, replaces all 32 bits and erases whatever another service wrote there. nft list prints every value padded to eight digits, 0x00008000 for 0x8000.

KindWhere it livesShown by
Packet mark (fwmark)One packet, from the rule or socket that set it until the packet leaves the routermeta mark in nftables, -m mark and MARK in iptables
Connection markThe conntrack entry, shared by every packet of the connection in both directionsct mark, -m connmark and CONNMARK, mark= in /proc/net/nf_conntrack
Socket mark (SO_MARK)A socket; every packet it sends starts with this packet markfwmark: in the output of ss -e, run as root on kernel 4.9 or later (4.10 for raw sockets)

The bits b4 uses​

Packet mark​

BitsMaskUsed forValue
0-14, 170x27fffThe route of a routing setOne per set; sets routed through the same interface with the same egress IP, gateway and kill switch share one. See sets routed through an interface and proxy sets
15 by default0x8000 by defaultThe queue mark: packets b4 sends from its raw sockets, except those toward a device in TUN mode, the DNS queries it sends for clients and for its sets, and its probes that bypass its own processingPacket Mark under Settings, Core, Packet Engine, 0x8000 (32768) by default
180x40000Connections b4 opens to the upstream of a proxy set, to Telegram, the Community Hub, ipinfo and RIPEstat, the warm and fallback connections of Reuse WSSIZE connection, and its transparent listeners; see socket marksFixed
210x200000b4's own connections whose outgoing packets packet processing leaves alone, such as the connection that carries a device's TLS for Reuse WSSIZE connection; b4 recognizes them by the whole of 0x240000, not by this bitFixed, carried together with bit 18 as 0x240000
240x1000000A proxy set's TCP packet that the router sent itselfFixed, added to the set's mark
250x2000000NFQUEUE mode: the packets b4 sends into a connection from its raw sockets, such as fakes, split segments and re-sent SYNs; see connections b4 sends packets intoFixed, added to the queue mark
260x4000000b4's own connections to Telegram while a Telegram set without routing applies, in NFQUEUE mode the packets b4 sends into the TCP connections it opens itself, and in TUN mode the packets b4 sends into the connections it opens for the Telegram set; see connections b4 opens itselfFixed, carried together with bit 18 as 0x4040000
280x10000000TUN mode: packets b4 writes backFixed; added to the queue mark, and to Discovery's injected mark on the packets a Discovery run in TUN mode sends
290x20000000Packets b4 sends toward a device on the networkFixed; added to the queue mark in NFQUEUE mode, and to Discovery's injected mark on the packets a Discovery run in NFQUEUE mode sends back toward its probes
300x40000000TUN mode: packets steered into b4tun0; unused when the default route itself points at b4tun0Fixed
all0xffffffffDiscovery, only while a run is activeBy default the queue mark plus 1 and plus 2, 0x8001 and 0x8002; system.checker.discovery_flow_mark and system.checker.discovery_injected_mark replace them

With the default queue mark and outside a Discovery run, b4 puts no other bit on a packet: bits 16, 19, 20, 22, 23, 27 and 31 stay free. Its firewall rules read them only in four matches. With 0x240000/0x8efd8000, b4 recognizes its own 0x240000 connections. With 0x4040000/0x8efd8000, B4_TUN on iptables recognizes b4's connections for the Telegram set, which it captures even when a routing set holds their destination. With 0x8000/0x88f98000, the queue chains and the rules that queue or copy DNS queries recognize b4's own packets (see The queue mark). With 0x1000000/0xfffd8000, the queue chains recognize a packet of the router's own that a proxy set marked. All four require these bits clear, and a packet that carries one of them is taken neither for such a connection nor for such a packet. The rules that write the whole mark, those of proxy sets on nftables and of Discovery, clear them on the packets they take. Bit 16 is left free for XrayUI's 0x10000.

Connection mark​

BitsMaskUsed for
300x40000000The connection belongs to a set routed through an interface
0-14, 170x27fffThe mark of that set, copied from the connection's first packet. A set that diverts with TPROXY and uses collected IPs writes its own mark here as well, without bit 0x40000000, on a new connection to a collected address; see Collected addresses
15 by defaultThe queue mark, 0x8000 by defaultNFQUEUE mode, on iptables when the connmark module is available: a packet carrying the queue mark and neither 0x2000000 nor 0x20000000 left the router on this connection, as b4's own lookups and probes do. A fake or split segment b4 sends into a device's connection does not set it, and neither does a packet b4 sends toward a device. b4's queue rules for packets entering the router skip the replies of such connections that reach a socket on the router; see The queue mark
250x2000000NFQUEUE mode, on iptables when the connmark module is available: b4 sent a packet of its own into this connection, such as a fake or split segment. b4's queue rules for packets entering the router skip the connection's TCP packets other than SYN-ACKs and resets, unless an enabled set uses an incoming mode or Collect detected IPs; see connections b4 sends packets into
all0xffffffffDiscovery's probe connections, while a run is active in NFQUEUE mode

Socket marks​

SocketsMark
Raw sockets for fakes, split segments and packets b4 sends back outIn NFQUEUE mode the queue mark with 0x2000000 added, 0x2008000 by default, and with 0x4040000 added as well for the packets sent into a TCP connection b4 opens itself (see Connections b4 sends packets into), 0x6048000 by default; in TUN mode the queue mark with 0x10000000 added, 0x10008000 by default, and with 0x4040000 added as well for the packets sent into a connection b4 opens for the Telegram set, 0x14048000 by default
Raw sockets for packets toward a device: DNS answers b4 builds, resets, ICMP; in TUN mode also the DNS answers and reply packets it captured and writes backThe queue mark with 0x20000000 added, 0x20008000 by default; 0x20000000 alone in TUN mode
The DNS queries b4 sends for clients (a set's DNS redirect, DNS over TCP) and its lookups through a set's resolver, including the lookup of a DoH resolver's host name for eitherThe queue mark
The DPI Detector's checks, except its fetches through b4 and its lookups through the system resolver; health probes of pinned DNS answers and of unreachable addresses; the public address lookup; the fetches that bypass b4 in the MCP tool b4_test_domain_now and in the test of an applied Community Hub setThe queue mark
Transparent listeners of proxy sets and every connection they open, to the upstream or, with Fall back to direct on upstream failure, directly; TCP connections of b4's SOCKS5 server to a domain name a proxy set targets, which go through the set's upstream; connections of the MTProto proxy and Telegram over WebSocket to Telegram and the address lists they download; the MTProto proxy's forward to its fake SNI domain; Community Hub, ipinfo and RIPEstat requests0x40000. When a Community Hub sync cannot connect, b4 retries it with the queue mark; if that retry connects, later Hub requests keep the queue mark until a sync cannot connect with it and a retry with 0x40000 can
Reuse WSSIZE connection: the transparent listener, the warm connection from a source port in 63000-63999 that b4 leases for it, the connection that then carries a device's TLS from that port, and the fallback connection from an ordinary source port0x40000; 0x240000 for the connection that carries the device's TLS
Connections through Cloudflare-proxied domains or a custom WebSocket domain, and to a Cloudflare Worker0x240000 while no Telegram set applies; while one applies, the marks in the next row
Connections of the MTProto proxy and Telegram over WebSocket to Telegram on every route, the Cloudflare-proxied domains, a custom WebSocket domain and a Cloudflare Worker included, their connection test, the address and domain lists they download, and the direct connections of a Telegram over WebSocket set, while a Telegram set applies, in NFQUEUE and in TUN mode0x4040000 for a set without routing; 0x240000 with the set's route mark for a set routed through an interface; none of their own for a proxy set, whose listener opens them through the upstream
Discovery probes and their name lookups; the packets Discovery sendsThe flow mark; the injected mark, with 0x20000000 added on packets sent back toward the probes. By default the queue mark plus 1 and plus 2, and 0x20008002 toward the probes. In TUN mode the injected mark with 0x10000000 added, 0x10008002 by default, and 0x20000000 alone toward the probes
Watchdog checks; the fetches through b4 of the DPI Detector, b4_test_domain_now and the Hub set test; connections b4's SOCKS5 server opens to other destinations; every other lookup through the system resolver, including the names of the other connections above and a set's domains when the set has no resolver of its own, or when the lookup through its resolvers fails and Fail closed when no resolver is reachable is off; GeoIP and GeoSite downloads, update checks and downloads, the device vendor database, AI assistant requestsNone

A connection with no mark is handled like any connection the router opens: it goes through packet processing and through every set that carries the router's own traffic.

What b4 leaves alone​

b4 rulesA packet is left alone when
Packet processing, nftables (inet b4_mangle)b4_chain, the rules for outgoing ports, returns it when its mark reads as b4's own (see The queue mark), its bits under 0x27fff equal 0x24bab or the mark of a proxy set, its mark holds bit 0x1000000, a bit under 0x27fff and no bit outside 0x1027fff, as a packet of the router's own that a proxy set marked does, it matches 0x240000/0x8efd8000, or it leaves by lo with any bit under 0x27fff, as a connection the router opens does on its way to the listener of a proxy set or of Telegram over WebSocket, and during a Discovery run when its mark is exactly the flow or injected mark. postrouting jumps to b4_chain, or, when device filtering selects devices, forward for their packets and output for the router's own. output accepts a packet with every bit of the queue mark, above that jump. Below its returns, b4_chain drops UDP whose addresses and ports are in the set b4_udp_drop4 or b4_udp_drop6, the QUIC connections a set drops, rejects or blocks; see Packet capture. prerouting, with the rules for DNS answers and other replies, tests no packet mark. It skips a reply that reaches a socket on the router when its connection mark has every bit of the queue mark, and, while no enabled set uses an incoming mode or Collect detected IPs, the TCP packets without SYN or RST of connections whose connection mark has bit 0x2000000 and none of the queue mark's bits. raw_prerouting and raw_output, which queue DNS queries at priority -300, ahead of conntrack, return a packet whose mark reads as b4's own, and during a Discovery run one whose mark is exactly the flow or injected mark; raw_output also returns one that leaves through lo. While they queue DNS queries, b4_chain returns UDP to port 53. The MSS clamp rules come first in prerouting, output and forward, apart from the rules a Discovery run adds above them
Packet processing, iptables (B4, B4_PREROUTING)B4, the rules for outgoing ports, first returns a packet whose mark reads as b4's own, as b4_chain does on nftables. The jumps to B4 come from mangle POSTROUTING, or, when device filtering selects devices, from mangle FORWARD for their packets and from mangle OUTPUT for the router's own. Mangle OUTPUT accepts a packet the router sends with every bit of the queue mark, above that jump. B4 also returns a packet whose bits under 0x27fff equal 0x24bab or the mark of a proxy set, whose mark holds bit 0x1000000, a bit under 0x27fff and no bit outside 0x1027fff, whose mark matches 0x240000/0x8efd8000, or that leaves by lo with any bit under 0x27fff, and during a Discovery run one whose mark is exactly the flow or injected mark. B4_PREROUTING and the rules for DNS answers test no packet mark; with the connmark module, B4_PREROUTING skips the same packets as prerouting on nftables. DNS queries are queued from B4_RAW in the raw table, which raw PREROUTING and OUTPUT jump to for UDP to port 53 and which returns a packet whose mark reads as b4's own, and during a Discovery run one whose mark is exactly the flow or injected mark; while it queues them, B4 returns UDP to port 53. On KeeneticOS, where b4 never uses B4_RAW, and on a kernel before 4.18 (4.14 before 4.14.173) whose raw table cannot queue packets, DNS queries are copied instead: mangle INPUT and FORWARD jump at their top to B4_DNSCOPY for UDP to port 53, and mangle OUTPUT for such UDP that does not leave through lo. B4_DNSCOPY returns a packet whose mark reads as b4's own and copies every other one to NFLOG group queue.start_num, 537 by default, with the prefix b4dns, and b4 ignores a copy from mangle OUTPUT whose mark has every bit of the queue mark; while it copies them, B4 returns UDP to port 53 as well, and, as with B4_RAW, the rules for DNS answers skip UDP to port 53. When b4 can neither queue DNS queries from the raw table nor copy them, the rules for DNS queries sit in B4_PREROUTING and mangle OUTPUT instead and test no packet mark
The queue itself, both backendsb4 releases a packet unchanged when its mark matches 0x240000/0x8efd8000, has 0x24bab under 0x27fff, or has every bit of the queue mark with the rest made only of bits of 0x27fff, 0x2000000 and 0x4040000, and is not exactly one of Discovery's two marks. On kernels 3.14 and later it also releases a packet that a socket on the router sent with every bit of the queue mark, unless the mark is exactly one of Discovery's two marks. A TCP packet to port 443 whose mark lacks bit 0x40000 is released unchanged when the set its address matches has Reuse WSSIZE connection in effect, counts as diverted and its IP Version Filter allows the packet's address family; see Reuse WSSIZE connection. A packet that leaves through a tunnel gets no strategy either, and neither does one that another service's mark sends by another route than the packets b4 injects would take. With legacy iptables that check covers every mark, as long as no other service on the router keeps nftables tables and Skip IPTables/NFTables Setup is off. Otherwise it covers only marks with every bit of the queue mark; see Packets that leave by another route
Routing sets other than block sets, packets entering the router (b4r_*_pre), including the router's own packets a proxy set loops through loIts mark has every bit of the queue mark or bit 0x40000, or its bits under 0x27fff are not zero and, in a proxy set's chain, differ from that set's own mark. In a set with Use collected IPs, also a packet to a collected address whose connection is not new and whose connection mark does not carry the set's mark under 0x27fff, unless, in a proxy set's chain, it is a TCP packet of a connection a transparent socket already holds; see Collected addresses
Routing sets, the router's own packets (b4r_*_out)Its mark has bit 0x40000 or any bit under 0x27fff. A packet with every bit of the queue mark is left alone as well, after a set routed through an interface has given it the set's mark when its destination is in the set; a set that leaves the router's own traffic alone and lists its devices only by IP address does this only for packets from those addresses. The IPv4 ICMP redirects that a set with a gateway drops, as sets routed through an interface describes, are dropped whatever their mark. In a set with Use collected IPs, a packet to a collected address is left alone as well when its connection is not new and its connection mark does not carry the set's mark under 0x27fff
QUIC refusal of a proxy set with Route UDP through upstream off or Block QUIC (force TCP) on, for forwarded packets (b4r_*_q) and the router's own (b4r_*_qo)Its mark has every bit of the queue mark, or bit 0x40000; in b4r_*_q also when it comes from a device outside the set's source devices
Block setsThey test no mark, only the destination and, when the set has them, its source interfaces and source devices. A set with Use collected IPs lets a packet to a collected address pass when its connection is not new
TUN capture (B4_TUN)Its mark has every bit of the queue mark or bit 0x20000000, matches 0x240000/0x8efd8000, has 0x24bab as its bits under 0x27fff, or is exactly Discovery's flow mark, or its injected mark with or without 0x10000000, when that mark lacks a bit of the queue mark; on iptables also when its destination is in a routing set other than a block set, unless its mark is 0x4040000 under 0x8efd8000. b4's own connections with 0x40000 or 0x4040000 are captured like any other, and those of the Telegram set also when a routing set holds their destination. A packet to a network in which the router has an IPv4 address of its own is left alone as well, unless it is UDP DNS or a TCP reset the chain captures; while b4 captures only the router's own connections, such a packet is always left alone, except a UDP DNS query to the uplink gateway
TUN on the whole default routeOnly packets with bit 0x10000000, and with bit 0x20000000 when ip accepts suppress_prefixlength, leave by b4's rules; every other packet that follows the default route enters b4tun0. During a Discovery run, packets whose mark is exactly the flow or injected mark follow Discovery's rules at priorities 80-83 instead, see Discovery
NAT Masquerade (b4_masq, B4_MASQ)Its mark has bit 0x20000000. On iptables that RETURN sits at the top of nat POSTROUTING, so such a packet skips every later rule there, other services' included
Set DSCP and the sets' own DSCP values (B4_DSCP, inet b4_dscp)Its mark has bit 0x20000000, it leaves through lo, or it travels in the reply direction of its connection. On iptables the jump to B4_DSCP sits at the top of mangle POSTROUTING; on nftables inet b4_dscp runs in postrouting at priority 150. The sets' values are written in the same chain or table, after the Set DSCP value, by rules that choose the value by the destination address alone
DNS over TCP redirectIts mark has every bit of the queue mark
Discovery, during a run in NFQUEUE modeAt the top of the chain, a packet whose mark is exactly the injected mark is accepted, and a packet whose mark or connection mark is exactly the flow mark goes to Discovery's queue; either way it skips the rest of the chain. On iptables that is the built-in mangle PREROUTING and OUTPUT, so it skips every later rule there. Its DSCP chain, B4_DISCOVERY_DSCP or b4_discovery_dscp, leaves alone every packet whose mark is not exactly one of the two

Where the kernel has the conntrack direction match, the TCP queue rules, those for a set's packet duplication addresses included, also test the direction: ct direction original in b4_chain and -m conntrack --ctdir ORIGINAL in B4, ct direction reply in prerouting and -m conntrack --ctdir REPLY in B4_PREROUTING. See Packet capture.

What b4 writes on packets it did not create​

b4 rulesWhat they write
Sets routed through an interfaceThe packet mark's bits under 0x27fff; the connection mark's bits under 0x27fff and bit 0x40000000. Masked, the other bits stay
Proxy sets, iptablesThe packet mark's bits under 0x1027fff. Masked
Proxy sets, nftablesThe whole packet mark
Proxy sets and sets in the Telegram over WebSocket mode, with Use collected IPsThe connection mark's bits under 0x27fff, set to the set's mark, on a new TCP connection to a collected address and, with Route UDP through upstream on, on a new UDP flow to one; see Collected addresses. Masked
Packet processingThe queue-mark bits into the connection mark of every connection on which the router itself sends a packet with every bit of the queue mark, such as b4's own lookups. Bit 0x2000000 instead when the packet also has that bit, as the fakes and split segments b4 sends into a device's connection do. Nothing for a packet whose mark has bit 0x20000000 that the queue mark lacks, such as a DNS answer b4 sends to a device. NFQUEUE mode only; on nftables not for packets sent to lo, on iptables only with the connmark module. Masked
TUN captureBit 0x40000000 of the packet mark. Masked
Discovery, during a run in NFQUEUE modeThe whole packet mark of a packet whose connection mark is exactly the flow mark, and the whole connection mark of a connection that carries a packet whose mark is exactly the flow mark; both get the flow mark

Set DSCP and the sets' own DSCP values write the DSCP field of the IP header, not a mark. A set's own value adds no packet mark, connection mark or routing rule: its rules sit in the chain or table Set DSCP uses, B4_DSCP or inet b4_dscp, which b4 installs while Set DSCP is on or a set writes its own value, and they choose the value by the destination address alone.

The queue mark​

The queue mark is the Packet Mark setting under Settings, Core, Packet Engine, queue.mark in the configuration file and --mark on the command line. The default is 0x8000 (32768); the web interface takes the value in decimal, and 0 stands for the default. b4 sends every fake, split segment and packet it puts back on the wire from raw sockets carrying this mark, with bit 0x2000000 added in NFQUEUE mode and bit 0x10000000 in TUN mode. The packets toward a device in TUN mode carry 0x20000000 alone, and those of a Discovery run carry Discovery's injected mark, with 0x20000000 added on the packets it sends back toward its probes; in TUN mode they carry the injected mark with 0x10000000 added, and those toward the probes 0x20000000 alone. The DNS queries b4 sends for clients and for its sets carry the queue mark too; its lookups through the router's own resolver carry no mark. Every rule below tests all bits of the queue mark at once, so the bits b4 adds on the same packet do not hide it. The rule that writes the connection mark for the router's own packets also wants bits 0x2000000 and 0x20000000 clear, so neither a packet b4 sends toward a device nor one it sends into a device's connection marks that connection as one of b4's own. A second rule writes bit 0x2000000 for the latter, see connections b4 sends packets into. With a queue mark that has bit 0x2000000 or 0x20000000 itself, the rules do not tell such packets apart.

Rulenftablesiptables
Router's own packetsoutput: meta mark & 0x22008000 == 0x8000 ct mark set ct mark | 0x8000, then meta mark & 0x8000 == 0x8000 acceptmangle OUTPUT: -m mark --mark 0x8000/0x22008000 -j CONNMARK --save-mark --nfmask 0x8000 --ctmask 0x8000 (with the connmark module), then -m mark --mark 0x8000/0x8000 -j ACCEPT
Queue chainb4_chain: meta mark & 0x88f98000 == 0x8000 returnB4: -m mark --mark 0x8000/0x88f98000 -j RETURN
Packets entering the routerprerouting: ct mark & 0x8000 == 0x8000 ct direction reply socket wildcard 0 returnB4_PREROUTING: -m connmark --mark 0x8000/0x8000 -m conntrack --ctdir REPLY -m socket -j RETURN (with the connmark module)
DNS queries, ahead of conntrackraw_prerouting, raw_output: meta mark & 0x88f98000 == 0x8000 returnB4_RAW: -m mark --mark 0x8000/0x88f98000 -j RETURN
DNS query copies-B4_DNSCOPY: -m mark --mark 0x8000/0x88f98000 -j RETURN
Routing sets other than block setsmeta mark & 0x8000 == 0x8000 return-m mark --mark 0x8000/0x8000 -j RETURN
Sets routed through an interface, router's own packetsmeta mark & 0x8000 == 0x8000 ip daddr @<set> meta mark set ...-m mark --mark 0x8000/0x8000 -m set --match-set <set> dst -j MARK --set-xmark <mark>/0x27fff
DNS over TCP redirect, while it is onmeta mark & 0x8000 == 0x8000 returnB4_DNSTCP: -m mark --mark 0x8000/0x8000 -j RETURN
TUN capture-B4_TUN: -m mark --mark 0x8000/0x8000 -j RETURN, written with iptables on both backends; when a Discovery mark lacks a bit of the queue mark, also -m mark --mark <flow mark>/0xffffffff -j RETURN or -m mark --mark <injected mark>/0xefffffff -j RETURN

In the queue chains b4_chain and B4 and in the rules that queue or copy DNS queries, a mark reads as b4's own when it has every bit of the queue mark and no other bit outside 0x77067fff. Those are the bits b4 itself may add to its own packets, 0x4040000 among them on the packets it sends into the TCP connections it opens itself. With the default queue mark this is the match 0x8000/0x88f98000. A queue mark that has bit 0x40000 or 0x4000000 keeps that bit in the match. A connection b4 opens itself, 0x40000 or 0x4040000 without the queue mark, does not read as b4's own and still reaches the queue. The 0xffffaaa that KeeneticOS writes has every bit of the default queue mark and bits outside 0x77067fff. These rules do not take it for b4's own.

The rule for packets entering the router takes a packet only in the reply direction of its connection, and only when the packet reaches a socket on the router, as the replies to b4's own lookups and probes do. The replies of a device connection reach no such socket, whatever value another program writes into its connection mark, and they go to the queue rules. ASUS firmware with AiProtection on often writes values with every bit of the default queue mark, and KeeneticOS does for the devices of its hotspot. -m socket on iptables and socket wildcard 0 on nftables pass over sockets bound to every address. Where nftables has no socket wildcard, b4 uses socket transparent 0, which also matches such sockets. Without the socket match, xt_socket or nft_socket, the rule takes every reply whose connection mark has every bit of the queue mark, and b4 logs a warning. Without the conntrack direction match it tests no direction.

The rule for sets routed through an interface sends the fakes and split segments of a connection such a set routes out of the same interface as the connection itself. It matches the destination, and the source address only in a set limited to source devices that are all given by IP address, so it does the same for the injected packets of connections to the set's destinations that the set does not route, such as those from a device outside its source interfaces. The source address is matched per address family: in a family for which such a set lists no device address, the rule matches the destination alone, so with Enable IPv6 Support on, a set whose devices are all given by IPv4 address gives its mark to every queue-marked IPv6 packet to its destinations.

Saving a changed Packet Mark moves the packet processing rules, the rules of routing sets and the DNS over TCP redirect to the new value at once and removes those of the old value. While a Discovery run is active, the packet processing rules and the redirect wait for it to end; after a run that ends more than five minutes after the save they move at the next check of the firewall monitor on iptables, and at the next start of b4 on nftables or with the monitor off (system.tables.monitor_interval at 0). Until they move, b4 queues its own DNS queries, which already carry the new value, like those of a device, so the DNS redirect of a set can stop applying. The raw sockets, b4's release of its own packets from the queue and, in TUN mode, the B4_TUN chain take the new value at the next start of b4, which the web interface asks for. Discovery's two marks follow the new value unless system.checker sets them to something other than the Packet Mark plus 1 and plus 2. A configuration saved from the web interface of a version before 1.84.0 can hold them at values that no longer follow it, such as the old value plus 1 and plus 2, after a save that changed the Packet Mark or one made while --mark was in effect. Deleting discovery_flow_mark and discovery_injected_mark from system.checker while b4 is stopped lets them follow again.

b4 refuses a value that:

  • is made only of bits of 0x240000, the marks of its own connections;
  • is made only of bits of 0x27fff and 0x1000000, the bits of set marks;
  • overlaps 0x70000000 in TUN mode;
  • has bit 0x20000000 in NFQUEUE mode while NAT Masquerade or Set DSCP is on, or while an enabled set has Enable per-set DSCP on;
  • has bit 0x2000000 in NFQUEUE mode, the bit b4 adds there to the packets it sends into a connection. A configuration that already runs NFQUEUE mode with such a value still loads and saves, and b4 logs a warning;
  • has 0x24bab, the Telegram over WebSocket mark, as its bits under 0x27fff;
  • equals the flow or injected mark of Discovery;
  • is above 0xffffffff, or, while either Discovery mark is left out of the configuration, is above 0xfffffffd or gives that mark (the value plus 1 for the flow mark, plus 2 for the injected mark) a value that matches 0x240000/0x8efd8000 or equals the other Discovery mark.

A value that has a bit of 0x240000 together with other bits is accepted, but Reuse WSSIZE connection does nothing while it is in effect; see Reuse WSSIZE connection.

A packet another service marks with every bit of the queue mark reads to the other rules as one b4 sent itself. Proxy sets, Telegram over WebSocket and the QUIC refusal leave it alone. A set routed through an interface gives it the set's mark when the router sends it to an address in the set and its mark has no bit under 0x27fff and not 0x40000, unless the set is limited to source devices that are all given by IP address, and a block set rejects it by destination as usual. TUN capture skips it whatever the rest of its mark holds; the queue chains of both backends, b4_chain and B4, skip it only when its mark reads as b4's own, and on kernels 3.14 and later b4 releases it from the queue unchanged when a socket on the router sent it. In NFQUEUE mode, when the router sends it, b4 also copies the queue-mark bits into its connection mark (on nftables not for packets sent to lo, on iptables only with the connmark module), so the replies that reach the socket that sent it are skipped as well. A mark that also has bit 0x2000000 puts that bit into the connection mark instead, as b4's fakes do, and one with bit 0x20000000 that the queue mark lacks puts nothing there. A packet that reaches the queue anyway, on both backends through the rules for packets entering the router, which test no packet mark, is released unchanged only when the rest of its mark is made only of bits of 0x27fff, 0x2000000 and 0x4040000, or when the mark matches 0x240000/0x8efd8000 or has 0x24bab under 0x27fff. wg-quick's 51820 (0xca6c), which it puts on the WireGuard socket when a peer routes /0, has bit 15 set, and the rest of it, 0x4a6c, lies within 0x27fff.

Connections b4 sends packets into​

In NFQUEUE mode the fakes, split segments, re-sent SYNs and other packets b4 sends into a connection carry the queue mark with bit 0x2000000 added, 0x2008000 by default. b4's own lookups and probes carry the queue mark alone. The connection mark keeps the two apart: the queue-mark bits mark a connection of b4's own, bit 0x2000000 a connection b4 has sent packets into. With the default queue mark, the rules for the bit are:

Rulenftablesiptables
Connection markoutput: meta mark & 0x22008000 == 0x2008000 ct mark set ct mark | 0x2000000mangle OUTPUT: -m mark --mark 0x2008000/0x22008000 -j CONNMARK --save-mark --nfmask 0x2000000 --ctmask 0x2000000
The server's packetsprerouting: ct mark & 0x2008000 == 0x2000000 tcp flags & (syn|rst) == 0 returnB4_PREROUTING: -p tcp -m connmark --mark 0x2000000/0x2008000 --tcp-flags SYN,RST NONE -j RETURN

On iptables both need the connmark module. Both rules that write the connection mark, for b4's own connections and for this bit, sit above the accept for the queue mark in output and in mangle OUTPUT.

The rule for the server's packets sits right below the return for b4's own connections, above every queue rule there. It matches a connection whose connection mark has bit 0x2000000 and none of the queue mark's bits. The connection's SYN-ACK and resets still reach b4, for RST Injection Protection and the escalation on forged resets, and the server's other packets stay out of the queue. nftables 1.1.6 lists its flag test as tcp flags ! syn,rst.

While an enabled set uses an incoming mode or Collect detected IPs, the rule is left out. The server's packets of every connection b4 sends packets into then reach the queue rules, whichever set the connection belongs to.

Firmware that writes the whole connection mark can remove the bit. ASUS firmware rewrites the connection mark of device connections, for example while AiProtection or QoS is on, and the bit is gone when that rewrite comes after b4 set it. The server's packets then reach the queue rules as on any other connection. While AiProtection is on, the value the firmware writes often has every bit of the queue mark. The replies of such a connection reach no socket on the router, and the return for b4's own connections does not take them. Without the socket match it does, and none of the connection's replies reach b4, the SYN-ACK and resets included.

Into a TCP connection b4 opens itself, b4 sends these packets with 0x4040000 added as well, 0x6048000 by default. b4 tells such a connection by 0x4040000 while a Telegram set applies, and by bit 0x40000 on a packet that a socket on the router sends. On kernels before 3.14, which do not report the socket owner of a packet, only the first applies. The routing chains return on bit 0x40000 as they do on the connection's own packets, so the fakes and split segments leave by the same route as the connection, even when a set routed through an interface lists the connection's address among its targets or b4 has learned it for the set's domains. The queue chains still read the mark as b4's own; see The queue mark. b4 tells these packets by the addresses and ports of the connection, and a later IP fragment, which carries no ports, by its addresses alone.

A queue mark that has bit 0x2000000 itself makes the two kinds of connection look the same. A connection b4 sends packets into then counts as one of b4's own, and its replies skip b4, the SYN-ACK and resets included. b4 refuses to save such a mark when the save would put it on NFQUEUE mode for the first time; a configuration that already runs NFQUEUE mode with one keeps working, and b4 logs a warning.

Sets routed through an interface​

A set routed through an output interface gets a mark and a routing table from the interface name, the egress IP, the gateway and the kill switch. Sets that agree on all four share both.

ItemValue
MarkFrom a hash of the four, in 0x100-0x7eff and never equal to the bits under 0x27fff of the queue mark or of either Discovery mark; if every hashed candidate is taken, counted up from 0x66 instead
Table100-249, skipping tables named in rt_tables, looked up by another service's rule, or holding routes b4 did not add
Ruleip rule add fwmark <mark>/0x27fff lookup <table> priority <10000 + table>, for IPv4, and for IPv6 when Enable IPv6 Support is on
Table contentsA default route through the interface, via the set's gateway in the gateway's address family when b4 can use it, plus blackhole default metric 4096 with the kill switch
Pinned valuesrouting.fwmark and routing.table in the configuration file or through the API, used only when both are set, the mark lies within 0x27fff, is not 0x24bab, does not contain every bit of the queue mark and does not equal the bits under 0x27fff of the queue mark or of either Discovery mark

The set marks the first packet of each connection to its destinations and saves the mark in the connection mark, with bit 0x40000000 as b4's claim. Later packets of that connection sent in the direction of its first packet get the mark back from the connection mark; replies get no mark. The set's chain for packets entering the router, b4r_<set>_pre, in this order:

  1. Returns packets that carry the queue mark, bit 0x40000, or any bit under 0x27fff.
  2. Returns packets that arrive on the output interface itself, narrowed for a set with a gateway as described below.
  3. Restores the mark from the connection mark on later packets of claimed connections that travel in the direction of the first packet.
  4. Marks new connections to the set's destinations and saves the mark with the claim.

A set with Use collected IPs has one more return between steps 3 and 4; see Collected addresses.

With an IPv4 gateway that b4 uses, on an output interface of the Ethernet type (Ethernet, bridge, VLAN, Wi-Fi, TAP), step 2 is narrower. The set first returns packets to local, broadcast, multicast and IPv6 link-local addresses, whatever interface they arrive on. It then returns the IPv4 packets that arrive on the output interface from the usable IPv4 gateway of any set routed through that interface, its own included, and every IPv6 packet that arrives on it. Other IPv4 packets from the output interface go on to steps 3 and 4. A refused gateway, an IPv6 gateway and an interface of another type leave step 2 as it is.

A set that excludes source devices returns their packets before step 2, only the packets of its devices enter the chain of a set limited to source devices, and a set limited to source interfaces applies steps 3 and 4 only to packets arriving on them. Device filtering under Settings, Core, Devices narrows the chain the same way. b4r_<set>_out does the same for the router's own connections when the set carries them, and gives the set's mark to b4's own queue-marked packets addressed to the set; a set limited to source devices that are all given by IP address does this only for packets from those addresses. With the narrower step 2, b4r_<set>_out drops the IPv4 ICMP redirects that leave through the output interface, whatever their mark and whether or not the set carries the router's own traffic. That drop is its first rule, or, in a set with Use collected IPs, the first rule below the collected returns. With net.ipv4.fwmark_reflect set to 1, the kernel gives a redirect the mark of the packet that caused it. Masquerade matches the set's mark and the output interface; with an egress IP present on the interface, SNAT to that address takes its place and also requires the destination to be in the set.

On a router whose firmware overwrites the connection mark b4 writes, the restore never matches. b4 on iptables notices this while the firewall monitor runs: when several connections have been claimed, the restore has never matched, and no answered connection in /proc/net/nf_conntrack still carries bit 0x40000000, on two checks in a row, b4 writes 0 to .conntrack-mark next to the configuration file. From then on, and at every later start on either backend until the file is deleted, the set marks every packet to its destinations that travels in the direction of its connection's first packet and has no bit under 0x27fff, not only the first.

While a set uses an output interface whose effective rp_filter is 1, b4 writes 2 to net.ipv4.conf.<interface>.rp_filter, and writes the old value back when the last set on that interface goes.

On a test box the two sets on wg0 got:

10119:  from all fwmark 0x5aef/0x27fff lookup 119
10123: from all fwmark 0x69cf/0x27fff lookup 123

Proxy sets and Telegram over WebSocket​

Sets in the Upstream SOCKS5 proxy and Telegram over WebSocket routing modes, and the Telegram over WebSocket switch, divert connections to transparent listeners of b4 with TPROXY. Each has a mark, and the mark gives the listener port.

ItemValue
Mark0x20000-0x27dff, from a hash of the set's ID, or the next free value when the hash equals the bits under 0x27fff of the queue mark or of either Discovery mark; routing.fwmark replaces it when it lies within 0x27fff, is not 0x24bab and equals none of those bits
Telegram over WebSocket switch0x24bab, port 13443
Listener port13000 + mark % 50000
Rule, priority 3fwmark <mark>/0x27fff lookup 252, for IPv4, and for IPv6 when Enable IPv6 Support is on; table 252 holds local default dev lo and is shared by every proxy set. When 252 belongs to another service, b4 takes 251, 250, then 300-399
Rule, priority 2fwmark <mark>/0x1027fff iif lo lookup main, IPv4
Router's own packets<mark> + 0x1000000, TCP in the original direction; none for a set limited to source devices or source interfaces

b4r_<set>_pre returns packets that carry the queue mark, bit 0x40000, or bits under 0x27fff other than the set's own mark, and packets to local, broadcast, multicast and IPv6 link-local addresses. Of the packets to the set's destinations, it then marks TCP packets of connections already held by a transparent socket and accepts them, and hands the other TCP packets, and UDP ones when Route UDP through upstream is on, to TPROXY with the set's mark; a set limited to source interfaces hands over only packets arriving on them. In a set with Use collected IPs, the collected rules sit between the accept and TPROXY. When the set refuses QUIC, with Route UDP through upstream off or Block QUIC (force TCP) on, the chain accepts its UDP to port 443 instead, so no later set's chain takes it and the QUIC refusal rejects it; on iptables that accept also skips every later rule in mangle PREROUTING. Source devices limit this chain as they do for sets routed through an interface. iptables writes the mark under the mask 0x1027fff; nftables writes the whole mark.

A set that routes UDP by port has no destinations. After the same returns, its b4r_<set>_pre hands UDP to the set's ports to TPROXY with the set's mark, only in a connection's original direction, and for a set limited to source interfaces only when the packet arrives on them. The chain takes no TCP, and the set has no QUIC refusal. b4 installs the rule only for a set limited to source interfaces or to included source devices.

The router's own connections reach a proxy set by a loop, unless the set is limited to source devices or source interfaces. b4r_<set>_out marks their TCP packets in the original direction with the set's mark plus bit 0x1000000, the priority 3 rule sends them to lo, and they come back through prerouting, where TPROXY takes them. Once new connections to the set's destinations exceed 200 per second (burst 400), b4r_<set>_out leaves the first packet of each further connection unmarked, but marks its later TCP packets in the original direction as usual. The limit does not cover collected addresses where the collected rules are installed: they mark those packets above it.

With src_valid_mark at 1, in net.ipv4.conf.all or on the interface a connection arrives on, the kernel checks the source of every diverted connection in the table its mark selects, and table 252 answers every address with the router itself. The priority 2 rule sends that check to main. Bit 0x1000000 keeps the router's own packets out of it, so they still loop through lo. XrayUI sets net.ipv4.conf.all.src_valid_mark to 1 when Xray has a WireGuard outbound, wg-quick and awg-quick when a peer takes the IPv4 default route, and Tailscale in its default netfilter mode.

b4 also adds an accept for each proxy mark to the input chains. On iptables it is -m mark --mark <mark>/<mark> -j ACCEPT at the top of filter INPUT and of mangle INPUT, through ip6tables as well and through the -legacy variants when they are installed. Mangle INPUT runs before filter INPUT, and the accept there keeps a diverted connection from a drop lower in that chain, such as the KeeneticOS chain that drops TCP to port 443 on the router. When a jump to B4_DNSCOPY heads mangle INPUT, the accept goes right below it, and a DNS query that a proxy set diverts is still copied. On nftables it is meta mark & <mark> == <mark> accept at the top of inet fw4 input, or of inet filter input when fw4 is absent, and nowhere when neither chain exists. Installing a proxy set writes 0 to net.ipv4.conf.lo.rp_filter and 2 to net.ipv4.conf.all.rp_filter; b4 does not write the old values back.

On a test box the Telegram over WebSocket switch and one proxy set got the following; the other proxy set and a set in the Telegram over WebSocket mode each added the same pair of rules with its own mark:

2:      from all fwmark 0x24bab/0x1027fff iif lo lookup main
2: from all fwmark 0x21546/0x1027fff iif lo lookup main
3: from all fwmark 0x24bab/0x27fff lookup 252
3: from all fwmark 0x21546/0x27fff lookup 252

Reuse WSSIZE connection​

A set with Reuse WSSIZE connection on gets the rules of a proxy set, limited to TCP to port 443: a mark and a listener port from the same range, the accept for connections its listener already holds, TPROXY, the mark on the router's own packets with its rate limit, the input accept and the rules at priorities 2 and 3. It has no upstream, no UDP rules and no QUIC refusal. Elsewhere on this page such a set counts as a proxy set within these limits. b4 installs the rules only while reuse is in effect: not while the TUN engine runs or is the saved engine, not with Skip IPTables/NFTables Setup on, and not while the queue mark shares a bit with 0x240000.

The set counts as diverted only while its TPROXY rule went in for every enabled address family its IP Version Filter allows. Without TPROXY or the socket match, xt_TPROXY and xt_socket on iptables, nft_tproxy and nft_socket on nftables, or when the firewall refuses the rule, b4 logs a warning for the set. While the set is diverted, the queue releases unchanged a TCP packet to port 443 of the set, of an address family its IP Version Filter allows, whose mark lacks bit 0x40000. Otherwise the set's connections that reach the queue get its own TCP strategy. Either way, a connection whose ClientHello the queue saw before a change to the collected addresses keeps the set it had then. b4's own connections to the set's addresses get the set's TCP strategy when their mark is 0x40000, and the strategy of the set chosen under Telegram set when it is 0x4040000. Those marked 0x240000, such as the connection that carries a device's TLS after a warm, or one through a Cloudflare-proxied domain while no Telegram set applies, get no strategy.

ConnectionSource portMarkPacket processing
Warm connectionLeased from 63000-639990x40000The set's TCP strategy
The connection that carries the device's TLS after a warmThe warmed port0x240000None
Fallback connectionAny port outside 63000-639990x40000The set's TCP strategy

Collected addresses​

A routing set with Use collected IPs on adds the addresses that Collect detected IPs gathers to its destinations. The rules below leave a connection that was open before its address was collected on the route it had, and send the new ones by the set. b4 also keeps the set's collected addresses in guard sets of their own:

BackendGuard sets
nftablesb4r_<set>_v4c and b4r_<set>_v6c in inet b4_route, of type ipv4_addr and ipv6_addr, without intervals or timeouts
iptablesipsets b4r_<set>_v4c and b4r_<set>_v6c of type hash:net, with entries that do not time out

b4 adds an address when it installs a change of the pool that holds it, and deletes it when it leaves the pool or when b4 rolls back a failed install; a delete the kernel refuses is retried with the routing sync. Turning Use collected IPs off empties the guard sets, and removing the set deletes them. A rebuild of the set's rules, such as after a change of its routing mode, fills them again.

With the IPv4 guard set, the rules read:

Rulenftablesiptables
Tag, TCPip daddr @b4r_<set>_v4c meta l4proto tcp tcp flags & (syn|ack) == syn ct state new ct mark set ct mark & 0xfffd8000 or <mark>-m set --match-set b4r_<set>_v4c dst -p tcp --syn -m conntrack --ctstate NEW -j CONNMARK --set-xmark <mark>/0x27fff
Tag, UDPip daddr @b4r_<set>_v4c meta l4proto udp ct state new ct mark set ct mark & 0xfffd8000 or <mark>-m set --match-set b4r_<set>_v4c dst -p udp -m conntrack --ctstate NEW -j CONNMARK --set-xmark <mark>/0x27fff
Restorect direction original ct mark & 0x27fff == <mark> meta mark set <mark>-m conntrack --ctdir ORIGINAL -m connmark --mark <mark>/0x27fff -j MARK --set-xmark <mark>/0x1027fff
Returnip daddr @b4r_<set>_v4c ct state != new ct mark & 0x27fff != <mark> counter return-m set --match-set b4r_<set>_v4c dst -m conntrack ! --ctstate NEW -m connmark ! --mark <mark>/0x27fff -j RETURN
Return, block setsip daddr @b4r_<set>_v4c ct state != new counter return-m set --match-set b4r_<set>_v4c dst -m conntrack ! --ctstate NEW -j RETURN, in the filter table

The tag keeps every bit of the connection mark outside 0x27fff. nft list prints it as under Reading a mark, with the set's mark folded into the constant: for 0x251b1, ct mark set ct mark & 0xffffd1b1 | 0x000251b1. IPv6 uses ip6 daddr and b4r_<set>_v6c. A set limited to source interfaces gets each rule once per interface, with iifname or -i, and a set with Reuse WSSIZE connection gets each rule for TCP to port 443 only.

Proxy sets, and sets in the Telegram over WebSocket mode, get every rule but the return of block sets:

  • The tag writes the set's mark into the connection mark of a new TCP connection to a collected address. In b4r_<set>_pre it also tags a new UDP flow when Route UDP through upstream is on and the set routes UDP by address, not by port. With Block QUIC (force TCP) on, that tag leaves out UDP to port 443, which the QUIC refusal still rejects.
  • The restore gives the set's mark back to every later packet in the original direction whose connection carries it, whatever its destination. In b4r_<set>_out it writes the set's mark plus 0x1000000.
  • The return takes a connection to a collected address that is not new, and whose connection mark does not carry the set's mark, out of the chain before TPROXY.

In b4r_<set>_pre the rules sit below the accept for connections a transparent socket already holds and above TPROXY, the QUIC pass and UDP. A TCP connection that the listener holds therefore stays diverted whatever its connection mark; the router's own connections and UDP flows depend on the connection mark. On Linux 3.16, a diverted connection whose connection mark another service rewrites at its SYN still breaks. b4r_<set>_out, present when the set carries the router's own traffic, has no UDP tag. There the rules sit below the returns for the queue mark, 0x40000 and any bit under 0x27fff, and a second return for any bit under 0x27fff follows them: it ends the chain for a packet the restore has marked, above the rate limit and the mark rule. On nftables the restore follows the tag of the last enabled address family, so a set with both families has tag, return, tag, restore, return. On iptables each family has its own tag, restore and return.

Sets routed through an interface save their mark in the connection mark when they claim a connection, see Sets routed through an interface. They get only the return: in b4r_<set>_pre between the restore and the marking of new connections, and at the top of b4r_<set>_out, so the packets b4 sends into a connection the set does not route keep that connection's path too. While .conntrack-mark holds 0, they get no collected rules, and the set marks the packets to collected addresses as it marks those to its other destinations.

Block sets get the return of block sets above their block rules. They block new connections to a collected address and let the others pass.

On iptables without the connmark match or the CONNMARK target, xt_connmark and xt_CONNMARK, sets other than block sets get none of these rules, and b4 logs one warning per set: a connection that is open when its address is collected no longer keeps its route. Block sets keep their return. An address family whose iptables binary is missing, such as ip6tables, gets none of the rules.

A UDP flow that was open through the uplink when its address was collected keeps going through the uplink until it goes idle and conntrack drops it; new flows take the set's route. b4's check of whether the router keeps the connection mark, .conntrack-mark, does not count the tags as claims of a set routed through an interface. No mark bit is reserved for collected addresses.

Connections b4 opens itself​

b4 opens these connections with 0x40000: to the upstream of a proxy set and the direct connections a proxy set opens in its place, to Telegram, for the address and domain lists the Telegram features download, the MTProto proxy's forward to its fake SNI domain, the warm and fallback connections of Reuse WSSIZE connection, and requests to the Community Hub, ipinfo and RIPEstat. The chains of interface, proxy and Telegram over WebSocket sets return on it, so none of these sets routes such connections and they never loop into b4's own listeners; they follow the rest of the router's routing rules, normally the main table. Block sets still block them, and in TUN mode they can still pass through b4tun0.

Packet processing does not skip 0x40000: a set whose targets match the destination applies its strategy to b4's own connections as it does to a device's. A set with Reuse WSSIZE connection applies its TCP strategy to them as well, never the release of Reuse WSSIZE connection. A set in a proxy or Telegram over WebSocket mode, or one routed into a TUN, TAP or WireGuard tunnel that is up, lets them through unmodified instead, and since its routing chains skip them, they leave by the main route with no strategy at all.

0x240000 adds bit 0x200000, and packet processing skips a packet whose mark matches 0x240000/0x8efd8000: 0x240000 exactly, apart from bits under 0x71027fff. A mark of another service that only shares bit 0x200000 with it does not take a packet out of packet processing. While no Telegram set applies, the MTProto proxy and Telegram over WebSocket put 0x240000 on their connections through Cloudflare-proxied domains or a custom WebSocket domain and on those to a Cloudflare Worker. In NFQUEUE mode and with port capture in TUN mode, no set processes these connections, whatever it targets. Their connections to the data centres, a DC Relay and Telegram's WebSocket edge carry 0x40000, and a set whose targets cover those addresses applies its strategy to them. Reuse WSSIZE connection puts 0x240000 on the connection that carries a device's TLS after a warm.

A set chosen under Telegram set on the Telegram page handles all of b4's own connections to Telegram, in NFQUEUE and in TUN mode: the connections of the MTProto proxy and Telegram over WebSocket on every route, the Cloudflare-proxied domains, a custom WebSocket domain and a Cloudflare Worker included, their connection test, and the address and domain lists they download. No other set handles them. The chosen set applies while:

  • it exists and is enabled;
  • it is neither a block set nor a set in the Telegram over WebSocket mode;
  • its routing is complete;
  • the Packet Mark is not made only of bits of 0x4040000, and neither Discovery mark has both bits of 0x4040000.

When the set does not apply, or the route of a set with routing is not in place, these connections carry the marks they carry without a set. The forward to the fake SNI domain keeps 0x40000 either way.

For a set without routing, these connections carry 0x4040000, 0x40000 with bit 0x4000000. Packet processing gives a packet with both bits the chosen set's strategy, whatever the set targets, and no other set takes it except a set the chosen set escalates to. On a port outside the set's TCP port filter, when it has one, no set processes them. In NFQUEUE and in TUN mode the packets b4 sends into these connections carry 0x4040000 as well, so they leave by the same route; see connections b4 sends packets into. In TUN mode b4 reads packets without their mark and recognizes these connections by the local port and destination it recorded when it opened them; see TUN mode.

For a set routed through an interface, these connections carry 0x240000 with the set's route mark under 0x27fff instead: packet processing skips them, the routing chains return on 0x40000, and the set's ip rule takes them to its table. b4 installs that rule and table for the chosen set even when it has no targets. For a proxy set, b4 hands them to the set's listener, which opens them through the upstream as it does for the SOCKS5 server. Neither applies its strategy to them. See Telegram upstream.

Lookups and probes carrying the queue mark skip packet processing, and proxy sets leave them alone. The DPI Detector's direct mode uses the queue mark to measure the network without b4's processing. A set routed through an interface gives such packets its mark when their destination is in the set, as it does to the packets b4 injects, so they leave by that set's interface; a set limited to source devices that are all given by IP address does this only for the packets b4 injects for those devices. Lookups through the router's own resolver carry no mark, and a service that exempts b4 by its marks still sees them as ordinary router traffic.

In TUN mode, when the default route itself points at b4tun0, the router's own packets with the queue mark, 0x40000, 0x240000 or 0x4040000 enter b4tun0 like any other whenever they follow the default route. b4 reads them there without their mark, so neither the queue mark nor 0x240000 takes them out of packet processing; in TUN mode they do so only with port capture, where B4_TUN returns such packets. The connections of a Telegram set without routing get that set there as well, by the local port and destination b4 recorded for them; see TUN mode. During a Discovery run, packets with exactly the flow or injected mark take rules of their own instead, see Discovery.

TUN mode​

In TUN mode (queue.mode: "tun") packets reach b4 through a TUN device, b4tun0 unless queue.tun.device_name says otherwise, instead of a queue. These rules are written with iptables also on the nftables backend, and cover IPv4 only.

ItemRule
CaptureChain B4_TUN, jumped from mangle OUTPUT and PREROUTING (from PREROUTING through B4_TUN_GATE when device filtering selects devices), or only from OUTPUT while b4 captures only the router's own connections (see Packet engine): sets 0x40000000/0x40000000 on UDP DNS queries and answers, on the first packets of connections to the capture ports (on every packet of them where xt_connbytes is missing; TCP only in the direction of the connection's first packet where xt_conntrack is available), on every TCP packet to the capture ports of a set's packet duplication addresses (only in the direction of the connection's first packet where xt_conntrack is available), and on TCP resets from the capture ports, only in the reply direction where xt_conntrack is available, while a set has RST Injection Protection on or an escalation target
Rulefwmark 0x40000000/0x40000000 lookup <table> at priority 10, or one less than the lowest rule another service has at 5-9, but not below 4
Router's own addressesfwmark 0x40000000/0x40000000 lookup local at one less than the capture rule, added only when no rule looks up the local table for every packet ahead of the capture rule, as in RouterOS containers (seen on RouterOS 7.24), where that rule sits at 200. Packets for the router's own addresses, such as the answers to its own DNS queries, are then delivered on the router instead of entering b4tun0
Tabledefault dev b4tun0; chosen from 96 down to 61, skipping 77 and tables in use, or next to queue.tun.route_table
Packets b4 writes backSocket mark queue mark + 0x10000000, and + 0x4040000 as well for the packets of a connection b4 opened for the Telegram set, not tracked by conntrack (raw OUTPUT ... -j CT --notrack) unless system.tables.skip_setup is on or the kernel has no raw table or no CT target. When system.tables.skip_setup is off and b4 cannot install this rule, the one for packets toward a device or the SNAT into b4tun0, it captures only the router's own connections
Packets toward a deviceSocket mark 0x20000000, not tracked by conntrack under the same conditions

When xt_connbytes is not available, b4 counts the packets of each connection itself and processes only the first ones, as the match would. While b4 captures only the router's own connections it keeps port capture, and B4_TUN marks every packet of the connections to the capture ports; otherwise b4 points the default route itself at b4tun0 instead. No setting selects either mode. With the default route at b4tun0, b4's own packets leave through rules 88 and 89 (0x20000000) and 99 and 100 (0x10000000): the first of each pair looks in main without its default route, the second in the uplink table, which holds the uplink's default route and is chosen from 97 down to 62 in the same way, or is queue.tun.route_table itself. Rules 88, 89 and 99 need an ip that accepts suppress_prefixlength; without it only rule 100 is added.

With port capture, b4's own connections enter b4tun0 by their destination port like any other router connection, except those that B4_TUN returns. The connections of a Telegram set without routing carry 0x4040000, a mark B4_TUN does not return, so they are captured on the capture ports. b4 binds each such connection's socket before it connects and records its local port and destination, so the record exists before the first packet leaves. The TUN engine gives the connections it recognizes by that record the Telegram set, as the NFQUEUE engine does by 0x4040000. B4_TUN captures these connections also when a routing set holds their destination, and the packets the TUN engine sends into them carry the queue mark with 0x10000000 and 0x4040000 added, 0x14048000 by default, whose bit 0x40000 keeps a routing set's b4r_<set>_out from giving them the set's mark, so they leave by the same route as the connection's own packets. While no Telegram set applies, the connections of the MTProto proxy and Telegram over WebSocket to Cloudflare-proxied domains, a custom WebSocket domain or a Cloudflare Worker carry 0x240000 and stay out of the capture; see connections b4 opens itself. These rules cover IPv4 only, so in TUN mode b4 processes none of its own IPv6 connections to Telegram, those of the Telegram set included.

Discovery​

Discovery's probe connections carry the flow mark, the queue mark plus 1 by default; the packets Discovery sends carry the injected mark, the queue mark plus 2. Both are compared as whole 32-bit values. They are set by system.checker.discovery_flow_mark and system.checker.discovery_injected_mark in the configuration file. Under the TUN engine a run does not start while either mark has a bit of 0x70000000, the marks TUN reserves for its own packets, and b4 logs a warning when it loads or saves such marks in TUN mode. A routing set leaves a packet alone when its mark has a bit under 0x27fff, and no set gets a mark equal to either Discovery mark's bits under 0x27fff, so with the default values no routing set routes Discovery's traffic. b4 logs a warning for Discovery marks without a bit under 0x27fff. The rules below exist while a run is active, including runs the watchdog or the MCP server start. Which of them a run gets follows the engine b4 is running, not the saved queue.mode.

NFQUEUE mode​

A chain at the top of b4's prerouting and output chains in inet b4_mangle (b4_discovery, nftables) or of mangle PREROUTING and OUTPUT (B4_DISCOVERY, iptables and ip6tables) steers the probe connections to a queue of their own, numbered after the main queues (541 with the defaults), and copies the flow mark between the packet and the connection whole. For the length of a run, b4's queue chain, b4_chain on nftables and B4 on iptables and ip6tables, returns a packet whose mark is exactly the flow or injected mark as well, so packet processing leaves Discovery's packets alone on their way out of the router. The DNS query chains, raw_prerouting and raw_output on nftables and B4_RAW on iptables and ip6tables, do the same, so b4's sets never answer Discovery's name lookups. B4_DNSCOPY gets no such rule: with the default values, 0x8001 and 0x8002, its return for the queue mark covers Discovery's packets, and b4 ignores a copy whose mark is exactly the flow or injected mark.

While a set writes its own DSCP value, a run also writes a DSCP value into its packets: the chain B4_DISCOVERY_DSCP in mangle (iptables and ip6tables), jumped to from POSTROUTING right after the jump to B4_DSCP, or b4_discovery_dscp in inet b4_mangle (nftables), hooked at postrouting priority 151, after inet b4_dscp. It sets the DSCP field of every packet whose mark is exactly the flow or the injected mark, on the interfaces the list of Set DSCP allows: in a run for a set with its own value, to that value in the IP versions the set's traffic carries it in, and otherwise to the Set DSCP value, or 0 while it is off. It runs after the sets' rules, so its value replaces the one they chose by destination. See DSCP rules.

During a run, a connection of another service whose mark is exactly the flow mark goes to Discovery's queue too, and a packet whose mark is exactly the flow or injected mark skips the rest of these chains; on iptables that includes every rule below them in mangle PREROUTING and OUTPUT, other services' rules as well.

TUN mode​

In TUN mode a run binds no queue and carries its probes through a TUN device of its own, b4dsc0, or b4dsc1 when the TUN engine's own device is named b4dsc0. The device exists while the run lasts. A run adds no firewall rule, no NOTRACK and no SNAT: the probes leave with the source address of the device's route. These rules cover IPv4 only.

ItemRule
Tabledefault dev b4dsc0 src <uplink address>; chosen from 60 down to 32, skipping tables in use and those of the TUN engine
Probesfwmark <flow mark>/0xffffffff lookup <table> at priority 81
Router's own addressesfwmark <flow mark>/0xffffffff lookup local at priority 80, and the same for the injected mark on the whole default route; added only when no rule looks up the local table for every packet ahead of priority 81, as in RouterOS containers
Lookups of the run's packet processing, on the whole default routePackets with exactly the injected mark, such as its DNS queries: fwmark <injected mark>/0xffffffff lookup main suppress_prefixlength 0 at priority 82, then the uplink table at 83, so they stay out of b4tun0. Priority 82 is left out when ip does not accept suppress_prefixlength
Packets b4 sendsThe probes b4 has processed, its fakes and split segments: the injected mark with 0x10000000 added, 0x10008002 by default. Packets toward the probes: 0x20000000 alone
NAT MasqueradeLeaves the device out, as it does b4tun0

With port capture, B4_TUN returns the probes and the packets b4 sends for the run: with the default marks by the queue-mark rule, since they have every bit of the queue mark, and with a flow or injected mark that lacks one of those bits by a rule for that mark. On the whole default route, priority 81 comes ahead of the main table and its default route into b4tun0. During a run, a packet of another service whose mark is exactly the flow mark goes into the device too. Every 5 seconds b4 checks the device, its route, these rules and the uplink address, and with port capture that B4_TUN still returns the run's marks. A missing one, a changed uplink address, or a change to Discovery's marks during the run that leaves the run's marks to capture stops the run. A device, table or rules left behind by a crash are removed when the TUN engine next starts, before its own rules.

Routing rules and tables​

PriorityRuleTablePresent for
2fwmark <mark>/0x1027fff iif lomainProxy sets, Telegram over WebSocket and Reuse WSSIZE connection, IPv4
3fwmark <mark>/0x27fff252; 251, 250, 300-399Proxy sets, Telegram over WebSocket and Reuse WSSIZE connection
3-9fwmark 0x40000000/0x40000000localTUN mode with port capture, IPv4, when no rule looks up local for every packet ahead of the capture rule
4-10fwmark 0x40000000/0x4000000096 down to 61, or next to queue.tun.route_tableTUN mode with port capture, IPv4
80fwmark <flow mark>/0xffffffff; also fwmark <injected mark>/0xffffffff on the whole default routelocalA Discovery run in TUN mode, IPv4, when no rule looks up local for every packet ahead of priority 81
81fwmark <flow mark>/0xffffffff60 down to 32A Discovery run in TUN mode, IPv4
82, 83fwmark <injected mark>/0xffffffffmain without its default route, then the uplink tableA Discovery run in TUN mode on the whole default route, IPv4
88, 89fwmark 0x20000000/0x20000000main without its default route, then the uplink table: 97 down to 62, or queue.tun.route_tableTUN mode on the whole default route, IPv4
99, 100fwmark 0x10000000/0x10000000The sameTUN mode on the whole default route, IPv4
10000 + tablefwmark <mark>/0x27fff100-249, or the pinned routing.tableSets routed through an interface

The kernel walks the rules from the lowest priority number up and stops at the first one that gives a route. A rule added without a priority gets the priority of the rule after local, minus one; on kernels before 4.3 an IPv6 rule gets 16383 instead. Added after b4's priority 2 rule, such a rule gets priority 1 and sits above every rule of b4; on a system with only the standard rules it gets 32765 and sits below them. XKeen, XrayUI and OpenClash outside their TUN modes, MagiTrickle, mihomo in iptables mode, the Xray guides, wg-quick and awg-quick all add their rules that way, so their rules sit above b4's when they are added while b4 is running, a restart of that service included. Added before b4 starts, they sit below b4's only when no rule other than the standard ones was there: next to Tailscale's 5210, for example, such a rule gets 5209, above b4's interface sets at 10100-10249. On kernels before 4.3 their IPv6 rules get 16383 and sit below b4's either way.

Next to other services​

  • A mark with any bit under 0x27fff on a packet before b4's routing chains keeps interface and proxy sets off that packet; only a proxy set still takes a packet whose bits under 0x27fff equal its own mark. Block sets and the QUIC refusal of a proxy set do not look at those bits. An upstream proxy running on the router is kept out of a set that matches its server this way, for example with Xray's sockopt.mark at 255. Packet processing still applies to its connections unless the mark is one of those in the next point, or its bits under 0x27fff equal 0x24bab or the mark of a proxy set, or it holds bit 0x1000000 and no bit outside 0x1027fff.
  • A mark with every bit of the queue mark reads as b4's own packet to the routing sets, TUN capture and the DNS over TCP redirect; the queue chains and the rules for DNS queries also need the rest of it within 0x77067fff, see The queue mark. Such a packet that reaches the queue is released unchanged when the rest of its mark is made only of bits of 0x27fff, 0x2000000 and 0x4040000 or, on kernels 3.14 and later, when a socket on the router sent it. A mark that matches 0x240000/0x8efd8000 reads as one packet processing leaves alone; a mark that has bit 0x200000 without matching it does not.
  • On nftables, b4's routing chains run at priority mangle - 1 (-151), ahead of chains at the mangle priority (-150): b4 marks first, and a service that later writes the whole mark replaces b4's, so the packet follows whichever rule matches the new value. A packet of the router itself is rerouted only when that rewrite happens in a chain of type route, as iptables mangle OUTPUT is.
  • On iptables, b4 inserts its routing jumps at the top of mangle OUTPUT. In mangle PREROUTING it puts them right above its own B4_PREROUTING jump, which it inserts at the top when it installs its rules, or above an earlier rule of another service that matches local sockets; with neither, as in TUN mode, it appends them, and it moves them back when they end up below either. A rule another service inserts at the top afterwards runs first, unless it matches local sockets.
  • On iptables, the jump to the QUIC refusal of a proxy set for forwarded packets (b4r_*_q) sits at the top of filter FORWARD, so the router firewall's own rules, such as Asuswrt-Merlin's accept for traffic from br0, run after it. The jump in filter OUTPUT to its refusal for the router's own packets (b4r_*_qo) and the jumps of block sets are appended, and a rule above them that accepts a packet keeps it from them. On nftables these chains hang off b4's block_fwd and block_out, filter chains at priority -150, ahead of the filter priority (0).
  • On iptables in NFQUEUE mode, a packet b4 releases from its queue skips every later rule of the built-in chain it was queued from, so another service's rules lower in that chain never see it. That covers the UDP packets from port 53 that B4_PREROUTING and b4's rules near the top of mangle OUTPUT queue unless the packet carries the queue mark or answers a lookup of b4's own, and the first packets of connections on b4's ports. UDP packets to port 53 are queued from the jumps b4 appends to raw PREROUTING and OUTPUT: other services' raw rules run before them, a raw rule another service appends after b4 is skipped for them, and once b4 releases them they pass conntrack and every mangle rule, so the DNS the Xray guides take with TPROXY reaches that rule. On KeeneticOS, and on a kernel before 4.18 whose raw table cannot queue packets, b4 copies them instead, from the jumps to B4_DNSCOPY it inserts at the top of mangle INPUT, FORWARD and OUTPUT: NFLOG does not end the chain, and every later rule there, b4's or another service's, sees the query. In mangle INPUT, b4's accept for a proxy mark sits right below the jump and ends the chain for a query that a proxy set diverts. Another program bound to the NFLOG group b4 uses keeps b4 from copying. When b4 can neither queue them from the raw table nor copy them, it queues them from B4_PREROUTING and mangle OUTPUT, and they skip the later rules there as well. b4's fakes and split segments carry the queue mark and bit 0x2000000, and b4 accepts them near the top of mangle OUTPUT, so a service whose rules come later in that chain, such as mwan3 or KVAS, never marks them either: for a connection such a service routes by its mark, they follow the router's main routing instead. In TUN mode the fakes and split segments carry the queue mark and bit 0x10000000, mangle OUTPUT has no such accept, and those rules see them.
  • When b4 picks a table itself, it skips one it has not claimed yet if the table is named in rt_tables, looked up by another rule, or holds routes b4 did not add. A table pinned with routing.table is used as given only together with routing.fwmark, and a queue.tun.route_table that is taken is refused. Some services use fixed table numbers in b4's ranges without such a check: XKeen 111, podkop 105, the Xray guides 100 and 106, XrayUI 250 in TUN mode. Started after b4 has taken one of them, XKeen flushes the table and adds its own routes, and XrayUI replaces b4's route in it; podkop and the Xray guides fail to add their route (File exists) while b4's default route is in the table. XKeen, podkop and XrayUI flush the table when they stop, and XrayUI also deletes every rule that looks up 250, b4's priority 3 rule included when b4 uses that table.

Values used by other services, read from their source code:

ServiceMarksRules and tables
Xray-core 26.3.27streamSettings.sockopt.mark: the whole socket mark of an outbound, only when set; no firewall rules of its own. The transparent proxy guides write 1, whole value, with TPROXY and on the router's own TCP and UDP in OUTPUT, and exempt Xray's own traffic only by an exact mark of 2 or 255Guides: fwmark 1 lookup 100, and fwmark 1 lookup 106 for IPv6, no mask, no priority
XKeen 2.0.1 BetaTPROXY and MARK 0x111, whole value, in TProxy mode and for UDP in Hybrid mode; Redirect mode and TCP in Hybrid mode use nat REDIRECT without a mark. CONNMARK saves and restores the whole mark. With router traffic on (proxy_router), takes the router's own TCP and UDP the same way, except a mark of exactly 255, a Keenetic policy mark, or bit 0x40000000fwmark 0x111 lookup 111, no priority
XrayUI 0.70.20x10000/0x10000 in DIVERT, TPROXY and the connection markfwmark 0x10000/0x10000 lookup 77, no priority; TUN mode: 49 from <LAN> to <LAN> lookup main and 51 from <LAN> lookup 250, no mark
sing-box 1.14.2route.default_mark, routing_mark: socket marks. TUN auto_redirect: 0x2023 and 0x2024, whole values, on the packet and connection mark of UDP and ICMP and, when NFQUEUE 100 is available, of TCP connections it judges on the first packet; 0x2025 on a TCP first packet it resets; every outbound socket 0x2024TUN auto_route: 9000-9010 to table 2022; with auto_redirect instead 9000 fwmark 0x2024, 9001 fwmark 0x2023 lookup 2022, 9002, and 32768 not fwmark 0x2024 lookup 2022
mihomo 1.19.32routing-mark: socket mark, 2158 when iptables mode is on and it is unset. iptables mode: 0x2d0 (TPROXY under 0x2d0, MARK whole). TUN auto-redirect writes 0x2023 and 0x2024, and puts 0x2024 on every outbound socket, only with route-address-set or route-exclude-address-setTUN: 9000-9010 to table 2022, and 32768 to table 2022 when auto-redirect writes marks; iptables mode: fwmark 0x2d0 lookup 720, IPv4 only, no priority
OpenClash 0.47.1560x162, whole value: on UDP in the default TPROXY mode, where TCP goes through nat REDIRECT without a mark, and on TCP, UDP and ICMP echo in TUN modefwmark 0x162 lookup 354, no priority; TUN mode: 1888
nikki 1.26.10x80/0xff for TPROXY, 0x81/0xff for TUN1024 to table 80, 1025 to table 81
podkop 0.7.220x100000, whole value105: fwmark 0x100000/0x100000 lookup podkop (table 105, named in rt_tables), IPv4 only
zapret 72.13 and zapret2 1.0.5.2Generated packets: the queued packet's mark plus 0x40000000. With FILTER_TTL_EXPIRED_ICMP=1, the default, 0x40000000 is added to the connection mark: in nftables POSTNAT mode, also the default, of every connection queued to nfqws, otherwise of connections nfqws sent generated packets on. In POSTNAT mode 0x20000000 is added to the packets queued after NAT. NFQUEUE 200 and 300None
MagiTrickle 0.8.20x4d616769 and up, one per group, whole value, packet and connection markfwmark <mark> lookup <mark>, no priority
KVAS 1.1.90xd1000, whole value, packet and connection mark1778: fwmark 0xd1000/0xd1000 lookup 1001
Tailscale 1.102.5Its own sockets 0x80000; packets forwarded from tailscale0 get 0x40000/0xff0000 and are masqueraded; bits 0xff0000 of the router's new connections saved in the connection mark and restored on later incoming packets, masked on iptables, whole value on nftables (the OpenWrt package's default)5210, 5230, 5250 on 0x80000/0xff0000; 5270 to table 52; 1310-1370 instead on OpenWrt with mwan3
wg-quick 1.0.20260223, awg-quick 3.1.20260812With a /0 peer and Table unset or auto: 51820 (0xca6c) on the WireGuard socket, or its FwMark, or the next number up when table 51820 holds routes; the whole connection mark copied to the packet mark of every UDP packet of that address family in preroutingnot fwmark 51820 lookup 51820, lookup main suppress_prefixlength 0, no priority
mwan3 2.12.2Interface N as N << 8 under 0x3f00; connection mark saved and restored under the same mask1001-1060 by incoming interface, 2001-2060 by mark, 3001-3060 unreachable by mark, 2061 and 2062 blackhole and unreachable
pbr 1.2.20x10000, 0x20000 and on per interface under 0xff000030000, 29999 and down: fwmark <mark>/0xff0000

Where these meet b4:

  • Xray, XKeen. 255 lies under 0x27fff, so routing sets leave Xray's outbound connections alone and packet processing still handles them. The QUIC refusal of a proxy set with Route UDP through upstream off or Block QUIC (force TCP) on, which tests only the queue mark and 0x40000, and block sets, which test no mark, reject Xray's packets to their addresses as well. XKeen's whole-value writes replace b4's bits on the connections it takes. With router traffic on, it also takes b4's own connections: none of its exemptions covers 0x40000. The nftables variant of the Xray guides starts with flush ruleset, which removes b4's nftables tables too, and the iptables tproxyrules.service of the IPv4 and IPv6 guide runs iptables -t mangle -F and ip6tables -t mangle -F when it stops, which removes b4's mangle rules.
  • XrayUI. Its bit 16 is outside b4's. Its DIVERT rule and the order of the two services in mangle PREROUTING are described in Using b4 with Xray or XrayUI.
  • sing-box, mihomo. Their default marks lie under 0x27fff; only sing-box's bridge outbound, when it falls back to iptables, uses bit 0x40000000, b4's TUN capture bit. auto_redirect, in mihomo only with route-address-set or route-exclude-address-set, writes whole marks after b4's routing chains: on UDP and ICMP, and in sing-box also on TCP connections it judges through NFQUEUE. With auto_route a rule at 9000-9010 sends forwarded traffic to table 2022, before b4's interface sets at 10000 and above. The router's own connections, b4's included, are taken as well: auto_redirect exempts only a mark of exactly 0x2024.
  • nikki. Its mask, 0xff, lies inside the marks of b4's interface sets. A set whose mark has 0x81 in the low byte, or 0x80 when nikki uses a TPROXY mode, matches nikki's rule first, since its priorities are lower. nikki's own writes change only bits 0-7, but these lie under 0x27fff, so on destinations both cover nikki's route wins, whichever runs first. nikki's proxy.bypass_fwmark option takes mark and mask pairs; 0x40000/0x40000 and 0x8000/0x8000 there exempt b4's marked connections and packets from its proxying, but not from its router DNS hijack, which runs before that check, and not b4's connections that carry no mark.
  • mwan3. Its mask, 0x3f00, lies inside the marks of b4's interface sets. A set whose mark has an active mwan3 interface number in bits 8-13 matches mwan3's rule at 2001-2060 first; with 61 or 62 there it matches mwan3's blackhole or unreachable rule, 2061 or 2062, whenever mwan3 runs. A set mark with no bit in 0x3f00 gets an interface number added by mwan3's policy, and then mwan3's rule matches it instead of b4's. With legacy iptables in NFQUEUE mode, a connection that mwan3 sends through another interface than the main table's route gets no strategy; see Packets that leave by another route.
  • zapret, zapret2. Bits 30 and 29 are b4's TUN capture bit and its bit for packets toward a device, and bit 30 of the connection mark is b4's claim. Neither service skips the packets the other generates by their mark. On nftables in zapret's default POSTNAT mode, though, zapret takes its generated packets out of conntrack, and b4 does not queue them: its queue rules count a connection's packets or test its direction. Only on a kernel without the conntrack direction match does b4 still queue them toward a set's packet duplication addresses. On nftables both queue the packets they select, b4 first (priority -150 against zapret's 99 or 101). With Set DSCP or a set's own DSCP value on, b4 writes no value into a packet with bit 0x20000000, so in POSTNAT mode the packets zapret queues after NAT, and the packets it generates from them, leave without b4's DSCP value. On iptables the NFQUEUE rule that sits higher in mangle POSTROUTING takes the packets both select there, and the other never sees them. With device filtering, b4 queues the router's own packets earlier, in mangle OUTPUT, and forwarded packets in mangle FORWARD, so zapret can still process those after b4. zapret drops ICMP time-exceeded messages of connections whose connection mark has bit 0x40000000, which includes every connection a b4 interface set claims. In TUN mode with port capture, b4's rule fwmark 0x40000000/0x40000000 also matches zapret's generated packets and sends them into b4tun0.
  • MagiTrickle. Its marks, 0x4d616769 and the ones after it, have bit 0x200000 but do not match 0x240000/0x8efd8000, so in NFQUEUE mode packet processing sees its connections. A connection it sends into a WireGuard, AmneziaWG, TUN or TAP interface gets no strategy. On legacy iptables, neither does one it sends by another route than b4's injected packets for that address would take; see Packets that leave by another route. On nftables such a connection gets the strategy, and the packets b4 injects for it carry the queue mark instead of MagiTrickle's and do not follow its route. MagiTrickle's marks also have bit 0x40000000: in TUN mode with port capture, b4's rule fwmark 0x40000000/0x40000000, IPv4 only, matches every packet MagiTrickle marks, and when it sits above MagiTrickle's fwmark <mark> rule in ip rule, those packets go into b4tun0, b4 processes them, and they leave by the router's own routing instead of MagiTrickle's table.
  • KeeneticOS. _NDM_HOTSPOT_PREROUTING_MANGL in mangle PREROUTING writes a whole mark, such as 0xffffaaa, on the packets of the devices it lists by MAC address. Such a value has every bit of the default queue mark, bit 0x40000, bits under 0x27fff and bits outside 0x77067fff. The queue chains and the rules for DNS queries do not take it for b4's own, so the connections and DNS queries of these devices reach packet processing. KeeneticOS also saves the mark into the connection mark. The replies of these connections reach no socket on the router, and B4_PREROUTING passes them to packet processing as well. Without the socket match it skips them, DNS answers from resolvers outside the router included; see The queue mark. Interface sets do not route these devices: the whole mark takes the place of b4's route mark, and KeeneticOS's own rule fwmark 0xffffaaa lookup 4096 picks the route. The rule of an interface set still sends the packets b4 injects to the set's addresses through the set's interface. A connection of these devices therefore gets no strategy while an enabled interface set routes addresses through another interface than the connection leaves by. A set with Domain-only matching on and no IP address listed, or a set without destinations, routes nothing and does not count. A proxy set takes their connections when b4's routing chains come before _NDM_HOTSPOT_PREROUTING_MANGL in mangle PREROUTING, where b4 puts them. These connections then leave through b4's proxy, not by the device's KeeneticOS policy. When KeeneticOS sends a connection by that mark through another interface than b4's injected packets would take, the connection gets no strategy; see Packets that leave by another route.
  • zapret, MagiTrickle and b4's connection-mark check. Both put bit 0x40000000 into connection marks. b4 takes any answered connection that carries that bit as proof that the router keeps its connection mark, so they can hide firmware that overwrites b4's claim.
  • KVAS. 0xd1000 contains 0x40000: a packet that already carries it when b4's chains run reads to b4 as one of its own connections. With acceleration on, KVAS also marks the router's own connections to its unblock destinations, b4's included, and its rule 1778 routes them into table 1001.
  • Tailscale. 0x40000 is Tailscale's mark for packets from tailscale0. Its masquerade rule, present by default, matches every packet carrying that value under 0xff0000, so b4's own connections marked 0x40000 are masqueraded to the address of the outgoing interface; those marked 0x240000 are not. Rule 5270 (1370 on OpenWrt with mwan3) comes before b4's interface sets, so a destination that table 52 routes leaves through Tailscale even when an interface set of b4 marked it.
  • wg-quick, awg-quick. With the default queue mark, 0xca6c has every bit of it, so b4 treats WireGuard's own packets as its own. Their prerouting rule replaces the packet mark of every UDP packet with the connection mark. When both they and b4 use nftables, it runs after b4's routing chains: packets of an interface set still match its rule, since b4 saved the set's mark in the connection mark, but UDP that a proxy set diverts loses the set's mark, so the priority 3 rule no longer sends it to b4's listener. Brought up after b4, their two rules sit above every rule of b4, for IPv6 only on kernel 4.3 or later.
  • pbr. 0x40000 is its fourth interface's mark, so with four or more interfaces pbr's rule routes b4's own connections that carry 0x40000 by that interface's table. A pbr policy that matches a packet a b4 interface set has marked adds its interface byte; when that byte has bit 17, b4's rule no longer matches and pbr's rule routes the packet.
  • OpenClash, podkop. Their writes replace b4's mark: whole values, and in OpenClash's default mode nat REDIRECT for TCP, so on destinations both cover, their route wins. OpenClash takes the router's own connections while router self-proxy is on, the default; podkop always takes those to addresses in its lists. Neither exempts b4's own connections.

The values in the table come from the releases named there and change between versions. ip rule and the firewall listing on the router show what is actually installed.

Checking marks on the router​

# b4's rules with their marks
nft list table inet b4_mangle
nft list table inet b4_route
nft list table inet b4_dscp
iptables -t mangle -S
iptables -t nat -S
iptables -t raw -S

# the ipsets of the sets' own DSCP values on iptables, and the entry count of one
ipset list -n | grep '^b4d_'
ipset save b4d_u_v4 | grep -c '^add'

# the guard set of a set's collected addresses
nft list set inet b4_route b4r_<set>_v4c
ipset list b4r_<set>_v4c

# routing rules of every service, and b4's tables
ip rule
ip -6 rule
ip route show table 252

# socket marks of b4's own connections
ss -tanpe | grep b4

# connection marks in decimal, where the kernel exposes them
grep -o 'mark=[0-9]*' /proc/net/nf_conntrack | sort | uniq -c

The addresses of the sets' own DSCP values live in these objects:

iptables, ipsetsnftables, in inet b4_dscpHolds
b4d_u_v4, b4d_u_v6-Every address of the sets' IP, GeoIP and ASN targets; a packet to none of them and to no learned address leaves B4_DSCP before the sets' rules
b4d_s<value>_v4, b4d_s<value>_v6Maps s4_<n> and s6_<n>, and a chain v<value> for each valueThe same addresses, grouped by the value they get
b4d_ul_v4, b4d_ul_v6-Every address b4 learned for the sets' domains
b4d_l_<set>_v4, b4d_l_<set>_v6Sets l_<set>_4 and l_<set>_6The addresses b4 learned for one set, each with its own timeout

System Info in the web interface lists b4's firewall rules and the output of ip rule. While an enabled set has its own DSCP value switched on, its Per-set DSCP group gives each such set's value and why it is refused, or whether it is applied, with the number of its static entries and of the learned addresses b4 wrote for it from each source.