1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

FreeBSDホストからjail上のtailscaleをsubnet clientにしてLANにアクセスする

1
Posted at

概要

  • FreeBSDホストからtailscaleに繋いでいるsubnet routerの先にあるLANのマシンにアクセスしたい
  • FreeBSDホストをMagic DNS等で汚したくなかったので、tailscale接続はjail内に閉じ込める
  • FreeBSDホストからaccept-routes経由で通信させるという構成の情報は、現時点であまり見かけないので書き記しておこう

イメージ図

tailscale_freebsd_jail.png

jail設定

jail用zfsプール作成 (お好みで)

host$ sudo -c - zfs create -o mountpoint=/srv/jail rpool/jail
host$ sudo -c - zfs create -o mountpoint=/srv/jail/tailscale rpool/jail/tailscale

OSインストール

host$ sudo -c - bsdinstall jail /srv/jail/tailscale
  • Select Installation Type: Packages (Tech Preview) = pkgbase を選択 (FreeBSD15の場合)
  • Select System Components: base-jail,lib32
  • Set root password: 適当 (jexecで直接root操作)
  • System Configuration: 全てチェックを外す
  • Add User Accounts: No (必要なら後で手動作成)

jail用ネットワーク設定

ブリッジネットワーク設定

host$ sudo vi /etc/rc.conf
# cloned bridge interface for jail network
cloned_interfaces="bridge0"
ifconfig_bridge0="inet 172.31.254.1 netmask 255.255.255.0"
host$ sudo -c - service netif start bridge0

NAT設定

内→外NATのみ設定

host$ sudo vi /etc/pf.conf
ext_if = "vtnet0"
jail_if = "bridge0"

set block-policy return

scrub in all

# NAT from jail
nat on $ext_if from $jail_if:network to any -> ($ext_if)

pass all
pass quick on lo0 all

host$ sudo vi /etc/rc.conf
# packet forwarding 
gateway_enable="YES"

# pf
pf_enable="YES"
host$ sudo -c - sysctl net.inet.ip.forwarding=1
host$ sudo -c - service pf restart

devfs設定

Tailscaleが使うtunデバイスを見せるルールを追加する

host$ sudo vi /etc/devfs.rules
[devfsrules_jail_vnet_unhide_tun=20]
add include $devfsrules_hide_all
add include $devfsrules_unhide_basic
add include $devfsrules_unhide_login
add include $devfsrules_jail
add include $devfsrules_jail_vnet
add path 'tun*' unhide
host$ sudo -c - service devfs restart

コンテナのリゾルバ設定

恐らくホストのresolv.confと内容が一緒のはずなので、DNSサーバを変えたければこの時点で変える

host$ sudo vi /srv/jail/tailscale/etc/resolv.conf
nameserver 1.1.1.1
...

jailコンフィグ設定

host$ sudo vi /etc/jail.conf
exec.start = "/bin/sh /etc/rc";
exec.stop = "/bin/sh /etc/rc.shutdown jail";
exec.clean;
mount.devfs;

path = "/srv/jail/${name}";

.include "/etc/jail.conf.d/*.conf";
  • VNETでブリッジに接続
  • 作成したdevfsルールを適用する
  • ホストからtailnetのsubnet routesにアクセスできるようにルーティング投入
host$ sudo vi /etc/jail.conf.d/tailscale.conf
tailscale {
        $id = 1;
        $hostname = "jail${id}.example.jp";
        $bridge = "bridge0";
        $ipv4_addr = "172.31.254.10";
        $ipv4_mask = "255.255.255.0";
        $ipv4_gateway = "172.31.254.1";
        $ipv4_lan_net = "192.168.100.0/24";
        $devfs_ruleset_id = 20;

        jid = ${id};
        host.hostname = ${hostname};

        vnet;
        vnet.interface = "epair${id}b";

        exec.prestart = "/sbin/ifconfig epair${id} create up";
        exec.prestart += "/sbin/ifconfig epair${id}a descr jail:${name}";
        exec.prestart += "/sbin/ifconfig ${bridge} addm epair${id}a";
        exec.prestart += "/sbin/route add -net ${ipv4_lan_net} ${ipv4_addr}";
        exec.start = "/sbin/ifconfig epair${id}b inet ${ipv4_addr} netmask ${ipv4_mask} up";
        exec.start += "/sbin/route add default ${ipv4_gateway}";
        exec.start += "/bin/sh /etc/rc";
        exec.poststop = "/sbin/route delete -net ${ipv4_lan_net}";
        exec.poststop += "/sbin/ifconfig ${bridge} deletem epair${id}a";
        exec.poststop += "/sbin/ifconfig epair${id}a destroy";

        devfs_ruleset = ${devfs_ruleset_id};

        allow.chflags;
        allow.raw_sockets;
}

jail内でのNAT設定

Tailscaleインターフェースは、自身のTailscale IPからの通信しか通さない
ホストから転送されてきた通信をtailnetに通せるように、NATで変換する必要がある

host$ sudo vi /srv/jail/tailscale/etc/pf.conf
tailscale_if = "tailscale0"
ext_if = "epair1b"

set block-policy return

scrub in all

# NAT from host to tailnet
nat on $tailscale_if from $ext_if:network to any -> ($tailscale_if)

pass all
pass quick on lo0 all
host$ sudo vi /srv/jail/tailscale/etc/rc.conf
# packet forwarding
gateway_enable="YES"

# pf
pf_enable="YES"

jail起動

host$ sudo vi /etc/rc.conf
# jail
jail_enable="YES"
host$ sudo -c - service jail start

Tailscale設定

Tailscaleインストール

host$ sudo -c - jexec tailscale /bin/sh
# pkg update -f
# pkg install -y tailscale
# vi /etc/rc.conf
# tailscale
tailscaled_enable="YES"
tailscaled_telemetry_enable="NO"
# service tailscaled start
# tailscale up --accept-routes
  • 表示されたURLにアクセスして、tailnetに接続
  • Tailscaleのconsoleにアクセスして、Disable key expiry しておく
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?