找回密码
 加入我们
搜索
      
查看: 9725|回复: 15

[网络] 防火墙nftables初探,ipv4和ipv6双栈

[复制链接]
发表于 2022-12-24 15:06 | 显示全部楼层 |阅读模式
前一段时间,投诉电信一直不给家里千兆宽带分配ipv6地址,反复几个来回后,终于拿到ipv6地址。

于是开始在软路由上设置ipv6,折腾软路由过程中,正好利用这个机会,研究了一下nftables这个iptables的替代者。

初步体验就是nftables优势明显,语句灵活,真香。附上我的nftables防火墙设置,互相学习,请高手指点。

  1. flush ruleset

  2. define DEV_LAN = eth1
  3. define DEV_WAN = ppp0
  4. define DEV_MODEM = eth0
  5. define IP_MODEM = 192.168.0.11
  6. define IPTV_VLAN85 = eth0.85
  7. define IPTV_VLAN51 = eth0.51

  8. define GUEST_LAN = { 192.168.4.0/24 }
  9. define IOT_LAN = { 192.168.5.0/24 }
  10. define HOME_LAN = { 192.168.10.0/24 }

  11. define LAN_SET = {
  12.     $GUEST_LAN,
  13.     $IOT_LAN,
  14.     $HOME_LAN
  15. }

  16. table inet global {

  17.     flowtable f {
  18.         hook ingress priority 0; devices = { $DEV_LAN, $DEV_MODEM };
  19.     }

  20.     chain inbound_wan {

  21.         icmp type { echo-reply, echo-request } limit rate 50/second burst 100 packets counter accept \
  22.         comment "Allow-ping-from-WAN"

  23.         ip6 nexthdr icmpv6 icmpv6 type {
  24.             destination-unreachable,  # type 1
  25.             packet-too-big,  # type 2
  26.             time-exceeded,  # type 3
  27.             parameter-problem,  # type 4
  28.             echo-request,  # type 128
  29.             echo-reply,  # type 129
  30.             } limit rate 50/second burst 100 packets counter accept \
  31.             comment "Accept basic IPv6 functionality"
  32.     }

  33.     chain inbound_lan {

  34.         icmp type { echo-reply, echo-request } accept

  35.         ip6 nexthdr icmpv6 icmpv6 type {
  36.             destination-unreachable,  # type 1
  37.             packet-too-big,  # type 2
  38.             time-exceeded,  # type 3
  39.             parameter-problem,  # type 4
  40.             echo-request,  # type 128
  41.             echo-reply,  # type 129
  42.             } accept

  43.         # allow DNS, NTP and SSH from the private network
  44.         meta l4proto . th dport vmap { udp . 53 : accept, tcp . 53 : accept, udp . 123 : accept }
  45.         ip saddr $HOME_LAN tcp dport 22 accept
  46.     }

  47.     chain inbound {
  48.         type filter hook input priority 0; policy drop;

  49.         # Allow traffic from established and related packets, drop invalid
  50.         ct state vmap { established : accept, related : accept, invalid : drop }

  51.         ip6 nexthdr icmpv6 icmpv6 type {
  52.             nd-router-solicit,  # type 133
  53.             nd-router-advert,  # type 134
  54.             nd-neighbor-solicit,  # type 135
  55.             nd-neighbor-advert,  # type 136
  56.             } ip6 hoplimit 255 accept \
  57.             comment "Allow IPv6 SLAAC"

  58.         ip6 nexthdr icmpv6 icmpv6 type {
  59.             mld-listener-query,  # type 130
  60.             mld-listener-report,  # type 131
  61.             mld-listener-reduction,  # type 132
  62.             mld-listener-report,  # type 131
  63.             mld-listener-reduction,  # type 132
  64.             mld2-listener-report,  # type 143
  65.             } ip6 saddr fe80::/10 accept \
  66.             comment "Allow IPv6 multicast listener discovery on link-local"

  67.         meta nfproto ipv6 udp sport 547 udp dport 546 accept \
  68.             comment "Accept DHCPv6 replies from IPv6 link-local addresses"

  69.         # allow loopback traffic, anything else jump to chain for further evaluation
  70.         iifname vmap { lo : accept, $DEV_WAN : jump inbound_wan, $DEV_LAN : jump inbound_lan }

  71.         # the rest will be dropped
  72.     }

  73.     chain outbound {
  74.         type filter hook output priority 0; policy accept;
  75.     }

  76.     chain forward {
  77.         type filter hook forward priority 0; policy drop;

  78.         meta l4proto { tcp, udp } flow offload @f
  79.         counter

  80.         # Allow traffic from established and related packets, drop invalid
  81.         ct state vmap { established : accept, related : accept, invalid : drop }

  82.         iifname $DEV_WAN ip6 nexthdr icmpv6 icmpv6 type {
  83.             destination-unreachable,  # type 1
  84.             packet-too-big,  # type 2
  85.             time-exceeded,  # type 3
  86.             parameter-problem,  # type 4
  87.             echo-request,  # type 128
  88.             echo-reply,  # type 129
  89.             } limit rate 50/second burst 100 packets counter accept \
  90.             comment "Accept basic IPv6 functionality"

  91.         meta l4proto esp counter accept comment "Allow-IPSec-ESP"
  92.         udp dport { 500, 4500 } counter accept comment "Allow-ISAKMP"

  93.         # internal nets are allowed
  94.         iifname $DEV_LAN counter accept

  95.         # connections from the internet to the internal net
  96.         iifname $DEV_WAN meta l4proto . th dport vmap { tcp . 8006 : accept, udp . 53847 : accept, tcp . 53847 : accept, udp . 13231 : accept, tcp . 8123 : accept } \

  97.         # the rest will be dropped
  98.     }

  99.     chain forward_mangle {
  100.         type filter hook forward priority mangle; policy accept;

  101.         oifname $DEV_WAN meta nfproto ipv4 tcp flags syn tcp option maxseg size set 1452 comment "IPV4 TCP MSS Clamping"
  102.         oifname $DEV_WAN meta nfproto ipv6 tcp flags syn tcp option maxseg size set 1432 comment "IPV6 TCP MSS Clamping"
  103.     }
  104. }

  105. table ip nat {

  106.     chain prerouting {
  107.         type nat hook prerouting priority -100; policy accept;

  108.         iifname $DEV_WAN ip protocol { tcp, udp } th dport 53847 dnat to 192.168.10.4 comment "port forwarding: qbittorrent"
  109.         iifname $DEV_WAN dnat to tcp dport map { 8006 : 192.168.10.250, 8123 : 192.168.10.252 } comment "port forwarding: PVE & HomeAssistant"
  110.         iifname $DEV_WAN udp dport 13231 dnat to 192.168.10.249 comment "port forwarding: WireGuard"
  111.     }

  112.     chain postrouting {
  113.         type nat hook postrouting priority 100; policy accept;

  114.         # masquerade private IP addresses
  115.         ip saddr $LAN_SET oifname $DEV_WAN masquerade

  116.         ip saddr $HOME_LAN oifname $DEV_MODEM snat to $IP_MODEM comment "access from home LAN to Modem"
  117.     }
  118. }

  119. table bridge filter {
  120.         chain forward {
  121.                 type filter hook forward priority 0; policy accept;

  122.                 iif $IPTV_VLAN85 oif $IPTV_VLAN51 counter drop comment "drop IPTV packages between VLAN85 & VLAN51"
  123.                 iif $IPTV_VLAN51 oif $IPTV_VLAN85 counter drop comment "drop IPTV packages between VLAN85 & VLAN51"
  124.         }
  125. }
复制代码
发表于 2022-12-25 10:22 | 显示全部楼层
学习一下
发表于 2022-12-25 13:40 | 显示全部楼层
我也分享一下我的,是根据openwrt的导出,然后我自己修改了一部分:


  1. #!/usr/sbin/nft -f

  2. # This configuration file is customized by fox
  3. # Optimize system nftables for linux router

  4. flush ruleset

  5. table inet router {
  6.         #
  7.         # Flowtable
  8.         #

  9.         flowtable ft {
  10.                 hook ingress priority filter;
  11.                 devices = { enp6s18, enp6s19, enp6s20, enp6s21, enp6s22 };
  12.         }


  13.         #
  14.         # Defines
  15.         #

  16.         define local_dns_ipv4 = { 172.16.1.1, 172.16.1.2, 172.16.1.3 }
  17.         define local_dns_ipv6 = { fd10::1, fd10::2, fd10::3 }


  18.         #
  19.         # Filter rules
  20.         #

  21.         chain input {
  22.                 type filter hook input priority filter; policy accept;
  23.                 iifname "lo" accept comment "defconf: Accept traffic from loopback"
  24.                 ct state established,related accept comment "defconf: Allow inbound established and related flows"
  25.                 ct state invalid counter drop comment "defconf: Drop input flows with invalid conntrack state"
  26.                 tcp flags & (fin | syn | rst | ack) == syn counter jump syn_flood comment "defconf: Rate limit TCP syn packets"
  27.                 iifname "bridge1" jump input_lan comment "defconf: Handle lan IPv4/IPv6 input traffic"
  28.                 iifname { "enp6s18", "pppoe-out1" } jump input_wan comment "defconf: Handle wan IPv4/IPv6 input traffic"
  29.         }

  30.         chain forward {
  31.                 type filter hook forward priority filter; policy drop;
  32.                 meta l4proto { tcp, udp } flow offload @ft comment "defconf: Track forwarded flows"
  33.                 ct state established,related accept comment "defconf: Allow forwarded established and related flows"
  34.                 ct state invalid counter drop comment "defconf: Drop forward flows with invalid conntrack state"
  35.                 iifname "bridge1" jump forward_lan comment "defconf: Handle lan IPv4/IPv6 forward traffic"
  36.                 iifname { "enp6s18", "pppoe-out1" } jump forward_wan comment "defconf: Handle wan IPv4/IPv6 forward traffic"
  37.         }

  38.         chain output {
  39.                 type filter hook output priority filter; policy accept;
  40.                 oifname "lo" accept comment "defconf: Accept traffic towards loopback"
  41.                 ct state established,related accept comment "defconf: Allow outbound established and related flows"
  42.                 ct state invalid counter drop comment "defconf: Drop output flows with invalid conntrack state"
  43.                 oifname "bridge1" jump output_lan comment "defconf: Handle lan IPv4/IPv6 output traffic"
  44.                 oifname { "enp6s18", "pppoe-out1" } jump output_wan comment "defconf: Handle wan IPv4/IPv6 output traffic"
  45.         }

  46.         chain prerouting {
  47.                 type filter hook prerouting priority filter; policy accept;
  48.                 iifname "bridge1" jump helper_lan comment "defconf: Handle lan IPv4/IPv6 helper assignment"
  49.         }

  50.         chain handle_reject {
  51.                 meta l4proto tcp reject with tcp reset comment "defconf: Reject TCP traffic"
  52.                 counter reject comment "defconf: Reject any other traffic"
  53.         }

  54.         chain syn_flood {
  55.                 limit rate 25/second burst 50 packets return comment "defconf: Accept SYN packets below rate-limit"
  56.                 counter drop comment "defconf: Drop excess packets"
  57.         }

  58.         chain input_lan {
  59.                 ct status dnat counter accept comment "lanconf: Accept port redirections"
  60.                 jump accept_from_lan
  61.         }

  62.         chain output_lan {
  63.                 jump accept_to_lan
  64.         }

  65.         chain forward_lan {
  66.                 jump accept_to_wan comment "defconf: Accept lan to wan forwarding"
  67.                 ct status dnat counter accept comment "lanconf: Accept port forwards"
  68.                 jump accept_to_lan
  69.         }

  70.         chain helper_lan {
  71.         }

  72.         chain accept_from_lan {
  73.                 iifname "bridge1" counter accept comment "defconf: Accept lan IPv4/IPv6 traffic"
  74.         }

  75.         chain accept_to_lan {
  76.                 oifname "bridge1" counter accept comment "defconf: Accept lan IPv4/IPv6 traffic"
  77.         }

  78.         chain input_wan {
  79.                 meta nfproto ipv4 udp dport 68 counter accept comment "defconf: Allow-DHCP-Renew"
  80.                 meta nfproto ipv4 icmp type echo-request counter drop comment "defconf: Drop-ICMP-Ping-Input"
  81.                 meta nfproto ipv6 icmpv6 type echo-request counter drop comment "defconf: Drop-ICMPv6-Ping-Input"
  82.                 meta nfproto ipv4 meta l4proto igmp counter accept comment "defconf: Allow-IGMP"
  83.                 meta nfproto ipv6 udp dport 546 counter accept comment "defconf: Allow-DHCPv6"
  84.                 ip6 saddr fe80::/10 icmpv6 type . icmpv6 code { mld-listener-query . no-route, mld-listener-report . no-route, mld-listener-done . no-route, mld2-listener-report . no-route } counter accept comment "defconf: Allow-MLD"
  85.                 meta nfproto ipv6 icmpv6 type { destination-unreachable, time-exceeded, echo-request, echo-reply, nd-router-solicit, nd-router-advert } limit rate 100/second burst 200 packets counter accept comment "defconf: Allow-ICMPv6-Input"
  86.                 meta nfproto ipv6 icmpv6 type . icmpv6 code { packet-too-big . no-route, parameter-problem . no-route, nd-neighbor-solicit . no-route, nd-neighbor-advert . no-route, parameter-problem . admin-prohibited } limit rate 100/second burst 200 packets counter accept comment "defconf: Allow-ICMPv6-Input"
  87.                 jump drop_from_wan
  88.         }

  89.         chain output_wan {
  90.                 jump accept_to_wan
  91.         }

  92.         chain forward_wan {
  93.                 meta nfproto ipv4 icmp type echo-request counter drop comment "defconf: Drop-ICMP-Ping-Forward"
  94.                 meta nfproto ipv6 icmpv6 type echo-request counter drop comment "defconf: Drop-ICMPv6-Ping-Forward"
  95.                 meta nfproto ipv6 icmpv6 type { destination-unreachable, time-exceeded, echo-request, echo-reply } limit rate 100/second burst 200 packets counter accept comment "defconf: Allow-ICMPv6-Forward"
  96.                 meta nfproto ipv6 icmpv6 type . icmpv6 code { packet-too-big . no-route, parameter-problem . no-route, parameter-problem . admin-prohibited } limit rate 100/second burst 200 packets counter accept comment "defconf: Allow-ICMPv6-Forward"
  97.                 meta l4proto esp counter jump accept_to_lan comment "defconf: Allow-IPSec-ESP"
  98.                 udp dport 500 counter jump accept_to_lan comment "defconf: Allow-ISAKMP"
  99.                 jump drop_to_wan
  100.         }

  101.         chain accept_to_wan {
  102.                 oifname { "enp6s18", "pppoe-out1" } counter accept comment "defconf: Accept wan IPv4/IPv6 traffic"
  103.         }

  104.         chain drop_from_wan {
  105.                 iifname { "enp6s18", "pppoe-out1" } counter drop comment "defconf: Drop wan IPv4/IPv6 traffic"
  106.         }

  107.         chain drop_to_wan {
  108.                 oifname { "enp6s18", "pppoe-out1" } counter drop comment "defconf: Drop wan IPv4/IPv6 traffic"
  109.         }


  110.         #
  111.         # NAT rules
  112.         #

  113.         chain dstnat {
  114.                 type nat hook prerouting priority dstnat; policy accept;
  115.                 iifname "bridge1" meta l4proto { tcp, udp } th dport domain counter jump dstnat_lan comment "!fw4: Handle lan IPv4/IPv6 dstnat traffic"
  116.         }

  117.         chain srcnat {
  118.                 type nat hook postrouting priority srcnat; policy accept;
  119.                 oifname { "enp6s18", "pppoe-out1" } jump srcnat_wan comment "defconf: Handle wan IPv4/IPv6 srcnat traffic"
  120.         }

  121.         chain dstnat_lan {
  122.                 ip saddr $local_dns_ipv4 meta l4proto { tcp, udp } th dport domain counter accept comment "lanconf: Accept lan dns IPv4 bootstrap query"
  123.                 ip6 saddr $local_dns_ipv6 meta l4proto { tcp, udp } th dport domain counter accept comment "lanconf: Accept lan dns IPv6 bootstrap query"
  124.                 meta l4proto { tcp, udp } th dport domain counter redirect to domain comment "lanconf: Lan dns redirect"
  125.         }

  126.         chain srcnat_wan {
  127.                 meta nfproto ipv4 masquerade comment "defconf: Masquerade IPv4 wan traffic"
  128.         }


  129.         #
  130.         # Raw rules (notrack)
  131.         #

  132.         chain raw_prerouting {
  133.                 type filter hook prerouting priority raw; policy accept;
  134.         }

  135.         chain raw_output {
  136.                 type filter hook output priority raw; policy accept;
  137.         }


  138.         #
  139.         # Mangle rules
  140.         #

  141.         chain mangle_prerouting {
  142.                 type filter hook prerouting priority mangle; policy accept;
  143.         }

  144.         chain mangle_postrouting {
  145.                 type filter hook postrouting priority mangle; policy accept;
  146.         }

  147.         chain mangle_input {
  148.                 type filter hook input priority mangle; policy accept;
  149.         }

  150.         chain mangle_output {
  151.                 type route hook output priority mangle; policy accept;
  152.         }

  153.         chain mangle_forward {
  154.                 type filter hook forward priority mangle; policy accept;
  155.                 iifname { "enp6s18", "pppoe-out1" } tcp flags syn tcp option maxseg size set rt mtu comment "defconf: Zone wan IPv4/IPv6 ingress MTU fixing"
  156.                 oifname { "enp6s18", "pppoe-out1" } tcp flags syn tcp option maxseg size set rt mtu comment "defconf: Zone wan IPv4/IPv6 egress MTU fixing"
  157.         }

  158. }



复制代码
 楼主| 发表于 2022-12-25 14:10 | 显示全部楼层
aitkots 发表于 2022-12-25 13:40
我也分享一下我的,是根据openwrt的导出,然后我自己修改了一部分:

请教一下,这条规则起什么作用?

  1. chain helper_lan
复制代码
发表于 2022-12-25 15:26 | 显示全部楼层
本帖最后由 Vampire_KILLer 于 2022-12-25 15:40 编辑

防火墙现在我只会在PA和CP的集中管控上点点点了

不过Juniper和Hillstone还时不时需要敲几个命令行

华为的防火墙不管是seco还是ssh我都不想碰
发表于 2022-12-25 18:21 | 显示全部楼层
normanlu 发表于 2022-12-25 14:10
请教一下,这条规则起什么作用?

这个是openwrt留给自定义防火墙配置用的
 楼主| 发表于 2022-12-25 18:30 | 显示全部楼层
aitkots 发表于 2022-12-25 13:40
我也分享一下我的,是根据openwrt的导出,然后我自己修改了一部分:

多问一句,我研究了一下你的防火墙规则,貌似你的路由器并没有跑什么tcp服务,这条input链syn_flood应该并没什么作用吧。
 楼主| 发表于 2022-12-25 18:32 | 显示全部楼层
Vampire_KILLer 发表于 2022-12-25 15:26
防火墙现在我只会在PA和CP的集中管控上点点点了

不过Juniper和Hillstone还时不时需要敲几个命令行

家庭宽带,linux自带防火墙用用足够了,专业防火墙就不必了吧。
发表于 2022-12-25 19:14 | 显示全部楼层
normanlu 发表于 2022-12-25 18:30
多问一句,我研究了一下你的防火墙规则,貌似你的路由器并没有跑什么tcp服务,这条input链syn_flood应该 ...

作用是比较小咯,主要是对路由器的DDoS的一个防御
发表于 2022-12-25 19:20 | 显示全部楼层
Vampire_KILLer 发表于 2022-12-25 15:26
防火墙现在我只会在PA和CP的集中管控上点点点了

不过Juniper和Hillstone还时不时需要敲几个命令行

为什么华为的不想碰?
发表于 2022-12-26 05:09 | 显示全部楼层
normanlu 发表于 2022-12-25 18:32
家庭宽带,linux自带防火墙用用足够了,专业防火墙就不必了吧。

主要是我在我看来,折腾软路由啥的,真不如搞个二手的山石小防火墙,基本上pppoe多拨、多WAN口、运营商路由啥的都有

当然这是指路由这一块哈,其他诸如下载、插件功能的话这些是欠奉的
发表于 2022-12-26 05:19 | 显示全部楼层
Krakenius 发表于 2022-12-25 19:20
为什么华为的不想碰?

使用体验差,日志功能差

网络厂商做的墙都有大病,思科如此、华为也如此

低层向高层做,要做好很难

高层转做低层,比较容易出彩——Juniper和Hillstone做核心交换机、F5做安全设备
 楼主| 发表于 2022-12-26 07:41 | 显示全部楼层
Vampire_KILLer 发表于 2022-12-26 05:09
主要是我在我看来,折腾软路由啥的,真不如搞个二手的山石小防火墙,基本上pppoe多拨、多WAN口、运营商路 ...

公司用fortigate,专业的事情交给专业的。家里嘛,自己折腾一下linux就好了,不想家里整一堆的硬件设备。
 楼主| 发表于 2022-12-27 16:22 | 显示全部楼层
看到一篇很好的nftables hardening的文章,做了一些修改,除了ipv6的hardening规则还需完善外,基本算完工了。

https://blog.samuel.domains/blog/security/nftables-hardening-rules-and-good-practices

  1. flush ruleset

  2. define DEV_LAN = eth1
  3. define DEV_WAN = ppp0
  4. define DEV_MODEM = eth0
  5. define IP_MODEM = 192.168.0.11
  6. define IPTV_VLAN85 = eth0.85
  7. define IPTV_VLAN51 = eth0.51

  8. define GUEST_LAN = 192.168.4.0/24
  9. define IOT_LAN = 192.168.5.0/24
  10. define HOME_LAN = 192.168.10.0/24

  11. define LAN_SET = {
  12.         $GUEST_LAN,
  13.         $IOT_LAN,
  14.         $HOME_LAN
  15. }

  16. table netdev filter {
  17.         chain ingress {
  18.                 type filter hook ingress device $DEV_WAN priority -500;

  19.                 # IP FRAGMENTS
  20.                 ip frag-off & 0x1fff != 0 counter drop

  21.                 # IP BOGONS
  22.                 # From <https://www.team-cymru.com/bogon-reference.html>.
  23.                 ip saddr {
  24.                         0.0.0.0/8,
  25.                         10.0.0.0/8,
  26.                         100.64.0.0/10,
  27.                         127.0.0.0/8,
  28.                         169.254.0.0/16,
  29.                         172.16.0.0/12,
  30.                         192.0.0.0/24,
  31.                         192.0.2.0/24,
  32.                         192.168.0.0/16,
  33.                         198.18.0.0/15,
  34.                         198.51.100.0/24,
  35.                         203.0.113.0/24,
  36.                         224.0.0.0/3
  37.                 } counter drop

  38.                 # TCP XMAS
  39.                 tcp flags & (fin|syn|rst|psh|ack|urg) == fin|syn|rst|psh|ack|urg counter drop

  40.                 # TCP NULL
  41.                 tcp flags & (fin|syn|rst|psh|ack|urg) == 0x0 counter drop

  42.                 # TCP MSS
  43.                 tcp flags syn tcp option maxseg size 1-536 counter drop
  44.         }
  45. }

  46. table inet global {
  47.         flowtable f {
  48.                 hook ingress priority 0; devices = { $DEV_LAN, $DEV_MODEM };
  49.         }

  50.         chain inbound_wan {
  51.                 # Allow-ping-from-WAN
  52.                 icmp type { echo-reply, echo-request } limit rate 10/second burst 20 packets counter accept

  53.                 # Accept basic IPv6 functionality
  54.                 ip6 nexthdr icmpv6 icmpv6 type {
  55.                         destination-unreachable,  # type 1
  56.                         packet-too-big,  # type 2
  57.                         time-exceeded,  # type 3
  58.                         parameter-problem,  # type 4
  59.                         echo-request,  # type 128
  60.                         echo-reply,  # type 129
  61.                 } limit rate 10/second burst 20 packets counter accept
  62.         }

  63.         chain inbound_lan {
  64.                 icmp type { echo-reply, echo-request } accept

  65.                 ip6 nexthdr icmpv6 icmpv6 type {
  66.                         destination-unreachable,  # type 1
  67.                         packet-too-big,  # type 2
  68.                         time-exceeded,  # type 3
  69.                         parameter-problem,  # type 4
  70.                         echo-request,  # type 128
  71.                         echo-reply,  # type 129
  72.                 } accept

  73.                 # allow DNS, NTP and SSH from the private network
  74.                 meta l4proto . th dport vmap { udp . 53 : accept, tcp . 53 : accept, udp . 123 : accept }
  75.                 ip saddr $HOME_LAN tcp dport 22 accept
  76.         }

  77.         chain inbound {
  78.                 type filter hook input priority 0; policy drop;

  79.                 # Allow traffic from established and related packets, drop invalid
  80.                 ct state vmap { established : accept, related : accept, invalid : drop }

  81.                 # Allow IPv6 SLAAC
  82.                 ip6 nexthdr icmpv6 icmpv6 type {
  83.                         nd-router-solicit,  # type 133
  84.                         nd-router-advert,  # type 134
  85.                         nd-neighbor-solicit,  # type 135
  86.                         nd-neighbor-advert,  # type 136
  87.                 } ip6 hoplimit 255 counter accept

  88.                 # Allow IPv6 multicast listener discovery on link-local
  89.                 ip6 nexthdr icmpv6 icmpv6 type {
  90.                         mld-listener-query,  # type 130
  91.                         mld-listener-report,  # type 131
  92.                         mld-listener-reduction,  # type 132
  93.                         mld2-listener-report,  # type 143
  94.                 } ip6 saddr fe80::/10 counter accept

  95.                 # Accept DHCPv6 replies from IPv6 link-local addresses
  96.                 meta nfproto ipv6 udp sport 547 udp dport 546 counter accept

  97.                 # allow loopback traffic, anything else jump to chain for further evaluation
  98.                 iifname vmap { lo : accept, $DEV_WAN : jump inbound_wan, $DEV_LAN : jump inbound_lan }

  99.                 # the rest will be dropped
  100.         }

  101.         chain outbound {
  102.                 type filter hook output priority 0; policy accept;
  103.         }

  104.         chain forward {
  105.                 type filter hook forward priority 0; policy drop;

  106.                 meta l4proto { tcp, udp } flow offload @f
  107.                 counter

  108.                 # Allow traffic from established and related packets, drop invalid
  109.                 ct state vmap { established : accept, related : accept, invalid : drop }

  110.                 # Accept basic IPv6 functionality
  111.                 iifname $DEV_WAN ip6 nexthdr icmpv6 icmpv6 type {
  112.                         destination-unreachable,  # type 1
  113.                         packet-too-big,  # type 2
  114.                         time-exceeded,  # type 3
  115.                         parameter-problem,  # type 4
  116.                         echo-request,  # type 128
  117.                         echo-reply,  # type 129
  118.                 } limit rate 10/second burst 20 packets counter accept

  119.                 # internal nets are allowed
  120.                 iifname $DEV_LAN counter accept

  121.                 # connections from the internet to the home lan
  122.                 iifname $DEV_WAN ip daddr $HOME_LAN ct status dnat counter accept

  123.                 # the rest will be dropped
  124.         }

  125.         chain forward_mangle {
  126.                 type filter hook forward priority mangle; policy accept;

  127.                 # IPV4 TCP MSS Clamping
  128.                 oifname $DEV_WAN meta nfproto ipv4 tcp flags syn tcp option maxseg size set 1452

  129.                 # IPV6 TCP MSS Clamping               
  130.                 oifname $DEV_WAN meta nfproto ipv6 tcp flags syn tcp option maxseg size set 1432
  131.         }

  132.         chain prerouting {
  133.                 type filter hook prerouting priority -150; policy accept;

  134.                 # CT INVALID
  135.                 ct state invalid counter drop

  136.                 # TCP SYN (CT NEW)
  137.                 tcp flags & (fin|syn|rst|ack) != syn ct state new counter drop
  138.         }
  139. }

  140. table ip nat {
  141.         chain nat_prerouting {
  142.                 type nat hook prerouting priority -100; policy accept;

  143.                 # port forwarding: PVE, HomeAssistant, qbittorrent & WireGuard
  144.                 iifname $DEV_WAN ip protocol { tcp, udp } th dport 53847 dnat to 192.168.10.4
  145.                 iifname $DEV_WAN dnat to tcp dport map { 8006 : 192.168.10.250, 8123 : 192.168.10.252 }
  146.                 iifname $DEV_WAN udp dport 13231 dnat to 192.168.10.251
  147.         }

  148.         chain nat_postrouting {
  149.                 type nat hook postrouting priority 100; policy accept;

  150.                 # masquerade private IP addresses
  151.                 ip saddr $LAN_SET oifname $DEV_WAN masquerade

  152.                 # allow access from home LAN to Modem
  153.                 ip saddr $HOME_LAN oifname $DEV_MODEM snat to $IP_MODEM
  154.         }
  155. }

  156. table bridge iptv {
  157.         chain port_block {
  158.                 type filter hook forward priority 0; policy accept;

  159.                 # drop IPTV packages between VLAN85 & VLAN51
  160.                 iif $IPTV_VLAN85 oif $IPTV_VLAN51 counter drop
  161.                 iif $IPTV_VLAN51 oif $IPTV_VLAN85 counter drop
  162.         }
  163. }
复制代码
发表于 2023-5-18 18:32 | 显示全部楼层
不懂防火墙设置,最近在er-x路由器上刷了openwrt 22.03.5,ipv6有时打开某些网站很慢,在网上看到https://www.v2ex.com/t/800024
附:在基于 Linux 的路由器启用MSS Clamping的命令:

# 自动MSS,假设PPPOE虚接口是pppoe0
iptables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -o pppoe0 -j TCPMSS --clamp-mss-to-pmtu
ip6tables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -o pppoe0 -j TCPMSS --clamp-mss-to-pmtu

# 手动指定MSS,假设PPPOE虚接口是pppoe0
$ iptables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -o pppoe0 -j TCPMSS --set-mss 1452
$ ip6tables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -o pppoe0 -j TCPMSS --set-mss 1432

这个在firewall4 里应该如何写?还是系统防火墙了已经自带了?我看你的帖子里的第159到163好像就是类似命令。
 楼主| 发表于 2023-5-18 20:22 | 显示全部楼层
二手烟 发表于 2023-5-18 18:32
不懂防火墙设置,最近在er-x路由器上刷了openwrt 22.03.5,ipv6有时打开某些网站很慢,在网上看到https://w ...
  1. oifname $DEV_WAN meta nfproto ipv4 tcp flags syn tcp option maxseg size set 1452
复制代码


160行就是ipv4的mss clamping
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

Archiver|手机版|小黑屋|Chiphell ( 沪ICP备12027953号-5 )沪公网备310112100042806 上海市互联网违法与不良信息举报中心

GMT+8, 2025-2-3 10:51 , Processed in 0.012517 second(s), 6 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2007-2024 Chiphell.com All rights reserved.

快速回复 返回顶部 返回列表