手順2:基本的な ルータ機能の 構築
第一部で、
インターフェースの 準備
最初に、
- VLAN用サブインターフェース
( home
、hgw
)の作成
[NetDev]Name=homeKind=vlan
[VLAN]Id=10
[NetDev]Name=hgwKind=vlan
[VLAN]Id=100
- サブインターフェースと
物理インターフェース ( lan0
)の紐づけ
[Match]Name=lan0
[Network]VLAN=homeVLAN=hgwLinkLocalAddressing=noLLMNR=no
フレッツ網への 接続
IPv6 IPoE
DHCPv6クライアントを
[Match]Name=wan0
[Link]RequiredForOnline=yes
[Network]DHCP=ipv6IPv6Forwarding=yesIPv6AcceptRA=yes# 一時アドレスを割り当てるIPv6PrivacyExtensions=yesDHCPPrefixDelegation=yesTunnel=ip6tnl0
[DHCPv6]DUIDType=link-layerIAID=0# DHCPv6リクエストに余分な情報を含めないようにするSendHostname=noUseCaptivePortal=noUseDNR=no
[DHCPPrefixDelegation]UplinkInterface=:selfSubnetId=0Announce=no# CEアドレスの後半64bit(= インターフェースID)を指定Token=::ce
CEアドレスはNetwork.Address=
でもDHCPPrefixDelegation.Token=
の
ひかり電話関連情報の
IPv4 over IPv6
トンネルデバイスip6tnl0
)を
[NetDev]Name=ip6tnl0Kind=ip6tnl
[Tunnel]Mode=ipip6# CEアドレスLocal=dhcp_pd# BRアドレスRemote=2001:db8::1DiscoverPathMTU=yesEncapsulationLimit=none
[Match]Name=ip6tnl0
[Link]RequiredForOnline=yes
[Network]BindCarrier=wan0IPv4Forwarding=yesLinkLocalAddressing=noLLMNR=no# DefaultRouteOnDevice=yes
[Route]Gateway=0.0.0.0
[QDisc]Parent=clsactHandle=ffff
あとでtc
の
な[Route]
セクションのNetwork.DefaultRouteOnDevice=yes
をRoute.InitialCongestionWindow
など)が
MAP-E関連の 設定
前回紹介したtc
を
まずMSS Clamping・fwmarkの
#!/usr/bin/nft -f
destroy table ip mapetable ip mape { chain mape-clamp { type filter hook forward priority mangle - 5; policy accept; # TCP MSSの調整 iifname "ip6tnl0" counter tcp flags syn tcp option maxseg size set rt mtu oifname "ip6tnl0" counter tcp flags syn tcp option maxseg size set rt mtu }
chain mape-mark { type filter hook postrouting priority filter - 5; policy accept; # fwmarkの付与 oifname "ip6tnl0" meta l4proto tcp mark set mark & 0xffffff00 | 0x54 counter oifname "ip6tnl0" meta l4proto udp mark set mark & 0xffffff00 | 0x55 counter oifname "ip6tnl0" ip protocol icmp icmp type { echo-request } mark set mark & 0xffffff00 | 0x59 counter }
chain mape-snat { type nat hook postrouting priority srcnat - 5; policy accept; # ソースNAT oifname "ip6tnl0" meta l4proto { tcp, udp, icmp } counter snat to 192.0.2.1:32784-33023 }}
起動時にtc
コマンドも、
#!/usr/bin/env bash
DEVICE="ip6tnl0"PSID=ab
# outbound
# TCP, UDPtc filter add dev $DEVICE egress handle 0x55/0xff fw action csum ip4h udp continuetc filter add dev $DEVICE egress handle 0x54/0xff fw action csum ip4h tcp continuetc filter add dev $DEVICE egress handle 0x54/0xfe fw action pedit pedit munge ip sport set "0x0${PSID}0" retain 0x0ff0 continuetc filter add dev $DEVICE egress u32 match mark 0x54 0x000000fe match ip sport 0x0010 0x0010 action pedit pedit munge ip sport set 0x1000 retain 0x1000 continuetc filter add dev $DEVICE egress u32 match mark 0x54 0x000000fe match ip sport 0x0020 0x0020 action pedit pedit munge ip sport set 0x2000 retain 0x2000 continuetc filter add dev $DEVICE egress u32 match mark 0x54 0x000000fe match ip sport 0x0040 0x0040 action pedit pedit munge ip sport set 0x4000 retain 0x4000 continuetc filter add dev $DEVICE egress u32 match mark 0x54 0x000000fe match ip sport 0x0000 0x0080 action pedit pedit munge ip sport set 0x0000 retain 0x8000 continue
# ICMP echo requesttc filter add dev $DEVICE egress u32 match mark 0x59 0x000000ff action pedit pedit munge offset 24 u16 set "0x0${PSID}0" retain 0x0ff0 pipe action csum ip4h icmp continuetc filter add dev $DEVICE egress u32 match mark 0x59 0x000000ff match u16 0x0010 0x0010 at 24 action pedit pedit munge offset 24 u16 set 0x1000 retain 0x1000 continuetc filter add dev $DEVICE egress u32 match mark 0x59 0x000000ff match u16 0x0020 0x0020 at 24 action pedit pedit munge offset 24 u16 set 0x2000 retain 0x2000 continuetc filter add dev $DEVICE egress u32 match mark 0x59 0x000000ff match u16 0x0040 0x0040 at 24 action pedit pedit munge offset 24 u16 set 0x4000 retain 0x4000 continuetc filter add dev $DEVICE egress u32 match mark 0x59 0x000000ff match u16 0x0000 0x0080 at 24 action pedit pedit munge offset 24 u16 set 0x0000 retain 0x8000 continue
# inbound
# TCP, UDPtc filter add dev $DEVICE ingress handle 0x65/0xff fw action csum ip4h udp continuetc filter add dev $DEVICE ingress handle 0x64/0xff fw action csum ip4h tcp continuetc filter add dev $DEVICE ingress handle 0x64/0xfe fw action pedit pedit munge ip dport set 0x8000 retain 0xf000 continuetc filter add dev $DEVICE ingress u32 match mark 0x64 0x000000fe match ip dport 0x8000 0x8000 action pedit pedit munge ip dport set 0x0080 retain 0x0080 continuetc filter add dev $DEVICE ingress u32 match mark 0x64 0x000000fe match ip dport 0x4000 0x4000 action pedit pedit munge ip dport set 0x0040 retain 0x0040 continuetc filter add dev $DEVICE ingress u32 match mark 0x64 0x000000fe match ip dport 0x2000 0x2000 action pedit pedit munge ip dport set 0x0020 retain 0x0020 continuetc filter add dev $DEVICE ingress u32 match mark 0x64 0x000000fe match ip dport 0x1000 0x1000 action pedit pedit munge ip dport set 0x0010 retain 0x0010 continuetc filter add dev $DEVICE ingress u32 match mark 0x64 0x000000fe action pedit pedit munge ip dport set 0 retain 0x0ff0 continuetc filter add dev $DEVICE ingress u32 match ip protocol 17 0xff match u16 0 1fff at 6 match ip dport "0x0${PSID}0" 0x0ff0 action skbedit mark 0x65/0xff continuetc filter add dev $DEVICE ingress u32 match ip protocol 6 0xff match u16 0 1fff at 6 match ip dport "0x0${PSID}0" 0x0ff0 action skbedit mark 0x64/0xff continue
# ICMP echo reply (0)tc filter add dev $DEVICE ingress handle 0x69/0xff fw action pedit pedit munge offset 24 u16 set 0x8000 retain 0xf000 pipe action csum ip4h icmp continuetc filter add dev $DEVICE ingress u32 match mark 0x69 0x000000ff match u16 0x8000 0x8000 at 24 action pedit pedit munge offset 24 u16 set 0x0080 retain 0x0080 continuetc filter add dev $DEVICE ingress u32 match mark 0x69 0x000000ff match u16 0x4000 0x4000 at 24 action pedit pedit munge offset 24 u16 set 0x0040 retain 0x0040 continuetc filter add dev $DEVICE ingress u32 match mark 0x69 0x000000ff match u16 0x2000 0x2000 at 24 action pedit pedit munge offset 24 u16 set 0x0020 retain 0x0020 continuetc filter add dev $DEVICE ingress u32 match mark 0x69 0x000000ff match u16 0x1000 0x1000 at 24 action pedit pedit munge offset 24 u16 set 0x0010 retain 0x0010 continuetc filter add dev $DEVICE ingress u32 match mark 0x69 0x000000ff action pedit pedit munge offset 24 u16 set 0 retain 0x0ff0 continuetc filter add dev $DEVICE ingress u32 match ip protocol 1 0xff match ip icmp_type 0 0xff match ip ihl 0x5 0xf match u16 0 1fff at 6 match u16 "0x0${PSID}0" 0x0ff0 at 24 action skbedit mark 0x69/0xff continue
# ICMP destination unreachable (3): port unreachable (UDP)tc filter add dev $DEVICE ingress handle 0x79/0xff fw action pedit pedit munge offset 48 u16 set 0x8000 retain 0xf000 pipe action csum ip4h and icmp continuetc filter add dev $DEVICE ingress u32 match mark 0x79 0x000000ff match ip dport 0x8000 0x8000 at 48 action pedit pedit munge offset 48 u16 set 0x0080 retain 0x0080 continuetc filter add dev $DEVICE ingress u32 match mark 0x79 0x000000ff match ip dport 0x4000 0x4000 at 48 action pedit pedit munge offset 48 u16 set 0x0040 retain 0x0040 continuetc filter add dev $DEVICE ingress u32 match mark 0x79 0x000000ff match ip dport 0x2000 0x2000 at 48 action pedit pedit munge offset 48 u16 set 0x0020 retain 0x0020 continuetc filter add dev $DEVICE ingress u32 match mark 0x79 0x000000ff match ip dport 0x1000 0x1000 at 48 action pedit pedit munge offset 48 u16 set 0x0010 retain 0x0010 continuetc filter add dev $DEVICE ingress handle 0x79/0xff fw action pedit pedit munge offset 48 u16 set 0 retain 0x0ff0 continuetc filter add dev $DEVICE ingress u32 match ip protocol 1 0xff match ip icmp_type 3 0xff match ip ihl 0x5 0xf match ip ihl 0x5 0xf at 28 match u16 0 1fff at 6 match ip sport "0x0${PSID}0" 0x0ff0 at 48 action skbedit mark 0x79/0xff continue
# ICMP time-exceeded (11)tc filter add dev $DEVICE ingress handle 0x89/0xff fw action pedit pedit munge offset 48 u16 set 0x8000 retain 0xf000 pipe action csum ip4h and icmp continuetc filter add dev $DEVICE ingress u32 match mark 0x89 0x000000ff match ip dport 0x8000 0x8000 at 48 action pedit pedit munge offset 48 u16 set 0x0080 retain 0x0080 continuetc filter add dev $DEVICE ingress u32 match mark 0x89 0x000000ff match ip dport 0x4000 0x4000 at 48 action pedit pedit munge offset 48 u16 set 0x0040 retain 0x0040 continuetc filter add dev $DEVICE ingress u32 match mark 0x89 0x000000ff match ip dport 0x2000 0x2000 at 48 action pedit pedit munge offset 48 u16 set 0x0020 retain 0x0020 continuetc filter add dev $DEVICE ingress u32 match mark 0x89 0x000000ff match ip dport 0x1000 0x1000 at 48 action pedit pedit munge offset 48 u16 set 0x0010 retain 0x0010 continuetc filter add dev $DEVICE ingress handle 0x89/0xff fw action pedit pedit munge offset 48 u16 set 0 retain 0x0ff0 continuetc filter add dev $DEVICE ingress u32 match ip protocol 1 0xff match ip icmp_type 11 0xff match ip ihl 0x5 0xf match ip ihl 0x5 0xf at 28 match u16 0 1fff at 6 match ip sport "0x0${PSID}0" 0x0ff0 at 48 action skbedit mark 0x89/0xff continue
このip6tnl0
デバイスの
[Unit]Description=Apply custom tc rulesWants=network-online.target# 下のように書くと、このサービスはip6tnl0デバイスの設定完了後に実行されるAfter=network-online.target sys-subsystem-net-devices-ip6tnl0.deviceBindsTo=sys-subsystem-net-devices-ip6tnl0.device
[Service]Type=oneshotRemainAfterExit=yesExecStart=/usr/local/sbin/tc-ip6tnl0.shExecStop=/usr/bin/tc filter del dev ip6tnl0 egress && /usr/bin/tc filter del dev ip6tnl0 ingress
[Install]WantedBy=multi-user.target
systemctl enable tc-ip6tnl0reboot
天地の
> ping -c3 2606:4700:4700::1001PING 2606:4700:4700::1001 (2606:4700:4700::1001) 56 data bytes64 bytes from 2606:4700:4700::1001: icmp_seq=1 ttl=54 time=7.45 ms64 bytes from 2606:4700:4700::1001: icmp_seq=2 ttl=54 time=7.19 ms64 bytes from 2606:4700:4700::1001: icmp_seq=3 ttl=54 time=7.12 ms
--- 2606:4700:4700::1001 ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2003msrtt min/avg/max/mdev = 7.123/7.254/7.451/0.141 ms> ping -c3 1.0.0.1PING 1.0.0.1 (1.0.0.1) 56(84) bytes of data.64 bytes from 1.0.0.1: icmp_seq=1 ttl=58 time=7.36 ms64 bytes from 1.0.0.1: icmp_seq=2 ttl=58 time=7.17 ms64 bytes from 1.0.0.1: icmp_seq=3 ttl=58 time=7.84 ms
--- 1.0.0.1 ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2003msrtt min/avg/max/mdev = 7.172/7.458/7.839/0.280 ms
宅内機器側インターフェースの 設定
続いてhome
)を
[Match]Name=home
[Network]IPv6Forwarding=yesIPv6AcceptRA=noIPv6SendRA=yesDHCPPrefixDelegation=yes
Address=10.0.0.1/24DHCPServer=yesMulticastDNS=yes
[DHCPPrefixDelegation]# このセグメントに/64のサブネット(3fff:0:0:ab01::/64)を割り当てるSubnetId=1# ただしインターフェース自体にはアドレスを割り振らないAssign=no
# 割り当てたサブネットとDNSサーバをRAで配布[IPv6SendRA]DNS=_link_localRouterLifetimeSec=9000DNSLifetimeSec=14400
[DHCPServer]DNS=_server_addressPoolOffset=32PoolSize=208DefaultLeaseTimeSec=24hMaxLeaseTimeSec=48h
# IPv4の固定もお手のもの[DHCPServerStaticLease]MACAddress=de:ad:be:ef:00:04Address=10.0.0.2
先ほどwan0
)に
DNS=_link_local
やDNS=_server_address
と
networkctl reload
設定を
な
ファイアウォール設定
インターネット側から
#!/usr/bin/nft -f
destroy table inet filtertable inet filter { set LANv4 { type ipv4_addr; flags interval; elements = { 10.0.0.0/24 } }
chain input { type filter hook input priority filter; policy drop; ct state invalid drop; ct state { established, related } accept; iif lo accept icmpv6 type { destination-unreachable, packet-too-big, time-exceeded, parameter-problem, nd-neighbor-solicit, nd-neighbor-advert } limit rate 5/second burst 10 packets accept icmpv6 type { nd-router-advert } iif "wan0" counter accept icmpv6 type { echo-request, nd-router-solicit } iifname "home" counter accept icmp type { destination-unreachable, time-exceeded, parameter-problem } limit rate 5/second burst 10 packets accept icmp type { echo-request } iifname { "home" } accept udp dport dhcpv6-client iif "wan0" ip6 saddr fe80::/64 counter accept udp dport bootps iifname "home" accept ip saddr @LANv4 iifname { "home" } jump lan-services ip6 saddr fe80::/64 iifname { "home" } jump lan-services counter }
chain lan-services { meta l4proto { tcp, udp } th dport { ssh, domain } accept }
chain forward { type filter hook forward priority filter; policy drop; ct state invalid drop; ct state { established, related } accept; icmpv6 type { destination-unreachable, packet-too-big, time-exceeded, parameter-problem } counter accept icmp type { destination-unreachable, time-exceeded, parameter-problem } accept meta l4proto { tcp, udp } th dport { 135, 137-139, 445 } oifname { "wan0", "ip6tnl0" } counter drop comment "NetBIOS" iifname "home" oif "wan0" accept comment "v6" iifname "home" oifname "ip6tnl0" accept comment "v4" counter }}
include "/etc/nftables.d/*.nft"
systemctl enable --now nftables
以上で、