# 50 Headscale: Voreinstellung und ACL-Regel 'A darf B nur auf TCP 22'
# A = peer-a 100.64.0.2 (sshd 22, iperf3 5201), B = peer-b 100.64.0.1 (sshd 22, iperf3 5201)

## Voreinstellung: policy.path leer
policy:
  mode: file
  path: ""
Error: loading ACL policy: rpc error: code = Unknown desc = reading policy from path "": open : no such file or directory
## 11:35:41 von peer-a nach 100.64.0.1
ping      erreichbar
tcp/22    erreichbar
tcp/5201    erreichbar
## 11:35:42 von peer-b nach 100.64.0.2
ping      erreichbar
tcp/22    erreichbar
tcp/5201    erreichbar

## Regel setzen
$ cat /etc/headscale/policy.hujson
// Headscale-Policy (HuJSON, gleiches Format wie Tailscale): A darf B nur auf TCP 22, sonst nichts.
{
  "hosts": {
    "peer-a": "100.64.0.2/32",
    "peer-b": "100.64.0.1/32"
  },
  "acls": [
    { "action": "accept", "src": ["peer-a"], "dst": ["peer-b:22"] }
  ]
}
$ sed -i "s#^  path: \"\"#  path: /etc/headscale/policy.hujson#" /etc/headscale/config.yaml; systemctl restart headscale
neu gestartet 11:35:55.533
active

## Wirkung: A fragt tcp/5201 bei B alle 0,5 s ab, bis geblockt
tcp/5201 geblockt ab 11:35:57.820
## 11:36:00 von peer-a nach 100.64.0.1
ping      erreichbar
tcp/22    erreichbar
tcp/5201    GEBLOCKT
## 11:36:04 von peer-b nach 100.64.0.2
ping      GEBLOCKT
tcp/22    GEBLOCKT
tcp/5201    GEBLOCKT

## Variante: dieselbe Regel mit "proto": "tcp"
    { "action": "accept", "src": ["peer-a"], "proto": "tcp", "dst": ["peer-b:22"] }
neu gestartet 11:36:24
## 11:36:28 von peer-a nach 100.64.0.1
ping      erreichbar
tcp/22    erreichbar
tcp/5201    GEBLOCKT
## 11:36:32 von peer-b nach 100.64.0.2
ping      GEBLOCKT
tcp/22    GEBLOCKT
tcp/5201    GEBLOCKT

## Wie die Peers die Regel sehen (Paketfilter auf B, tailscale debug netmap, gekürzt)
[{"Caps":[],"Dsts":[{"Net":"100.64.0.1/32","Ports":{"First":22,"Last":22}}],"IPProto":[6],"SrcCaps":null,"Srcs":["100.64.0.2/32"]}]

## Gegenprobe 30 s später, ping -c 5 von A nach B trotz IPProto [6]
11:36:54
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 25.190/25.696/26.370/0.397 ms
# Befund: Der Paketfilter auf B enthält nur TCP (IPProto 6) Port 22, trotzdem antwortet B auf Ping von A.
# Der Tailscale-Client lässt Echo-Anfragen von Quellen zu, die irgendeine Regel zu diesem Ziel haben (Quelltext wgengine/filter, nicht aus der Doku belegt).
# Beleg im Quelltext: tailscale v1.104.2 wgengine/filter/filter.go Zeile 519-520:
	case ipproto.ICMPv4:
		if q.IsEchoResponse() || q.IsError() {
			// ICMP responses are allowed.
			// TODO(apenwarr): consider using conntrack state.
			//  We could choose to reject all packets that aren't
			//  related to an existing ICMP-Echo, TCP, or UDP
			//  session.
			return Accept, "icmp response ok"
		} else if f.matches4.matchIPsOnly(q, f.srcIPHasCap) {
			// If any port is open to an IP, allow ICMP to it.
			return Accept, "icmp ok"
		}
