防火墙规则

Cumulus Linux 默认防火墙规则保护交换机控制平面和 CPU 免受 DOS 和其他潜在的恶意网络攻击。

在 Cumulus Linux 5.8 及更早版本中,默认防火墙规则集更加开放;Cumulus Linux 接受来自所有地址和协议的数据包。Cumulus Linux 5.9 及更高版本提供了一组默认防火墙规则,仅允许特定地址和端口,并丢弃不允许的数据包。

默认的防火墙规则集由 IP 和传输层规则组成。要阻止特定的第 2 层数据包(例如 ARP、LLDP 或 STP)或发送到 CPU 作为通用陷阱一部分的任何数据包,您必须在 ebtables 的 INPUT 或 OUTPUT 链中使用控制平面 ACL 配置单独的规则。请参阅访问控制列表配置

没有 NVUE 的默认防火墙规则文件

即使在您首次应用 NVUE 配置之前,Cumulus Linux 也会在交换机上启用默认防火墙规则。默认防火墙规则位于 /etc/cumulus/acl/policy.d/ 目录中的 01control_plane.rules98control_plane_whitelist.rules 文件中。

如果您希望通过编辑 Linux 文件而不是运行 NVUE 命令来配置交换机,则可以更改这些文件以添加其他规则。

DoS 规则

DoS 规则保护交换机控制平面和 CPU 免受 DOS 攻击。Cumulus Linux 提供防火墙 DoS 规则以

  • 仅允许内部流量访问环回接口。
  • 接受已建立的连接和出站流量。
  • 设置 - allow 选项以对来自特定接口的数据包进行着色。如果需要为不同的 eth 接口应用不同的策略,则可以设置此选项。
  • 如果第一个 TCP 段不是 SYN,则丢弃数据包。
  • 丢弃分片的 IP 数据包。
  • 丢弃圣诞树数据包;设置了所有 TCP 标志的数据包。
  • 丢弃 NULL 数据包。
  • 丢弃无效数据包。
  • 丢弃奇怪的 MSS 值。
  • 提供暴力保护。
  • 丢弃路由标头类型为 0 的数据包。
  • 丢弃跳数限制大于 1 的数据包。
  • 限制过多的 TCP 重置数据包。
  • 防止 SYN 洪水攻击。
  • 限制每个 IP 地址的新 TCP 连接速率。
  • 记录所有剩余的数据包,然后丢弃它们。

白名单规则

白名单规则指定在交换机上启用的服务或应用程序端口。Cumulus Linux 提供防火墙白名单规则以启用 TCP 端口和 UDP 端口。

下表列出了 Cumulus Linux 默认启用的端口。

协议端口应用程序
TCP22SSH
TCP179BGP
UDP68DHCP 客户端
UDP67DHCP 服务器
UDP123NTP
UDP323Chrony
UDP161SNMP
UDP6306 内部使用的多播套接字。
UDP69TFTP
TCP/UDP389LDAP
UDP1812,1813RADIUS
TCP/UDP49TACACS
TCP/UDP53DNS
TCP8765NVUE NGINX
UDP6343, 6344sFlow
UDP514远程 syslog
UDP3786BFD
UDP4784多跳 BFD
TCP5342MLAG
UDP4789VXLAN
UDP319,320PTP
TCP443HTTPS
TCP9339gNMI
TCP31980,31982NETQ 代理
OSPFNANA
UDP53 (SPORT)DNS 响应数据包
TCP9999XMLRPC
ICMPNAPing
PIMNANA
IGMPNANA
VRRPNANA
TCP639MSDP

取消设置默认防火墙规则

要取消设置默认防火墙规则并使用 Cumulus Linux 5.8 及更早版本中接受来自所有地址和协议的数据包的设置

cumulus@switch:~$ nv unset system control-plane acl acl-default-dos 
cumulus@switch:~$ nv unset system control-plane acl acl-default-whitelist
cumulus@switch:~$ nv config apply

要将防火墙规则设置回默认设置

cumulus@switch:~$ nv set system control-plane acl acl-default-dos inbound
cumulus@switch:~$ nv set system control-plane acl acl-default-whitelist inbound
cumulus@switch:~$ nv config apply

添加防火墙规则

您无法修改 acl-default-dosacl-default-whitelist 规则。但是,您可以附加或插入其他规则。此外,您可以添加自己的 ACL 并在控制平面上应用它们;当默认防火墙规则为 enabled 时,控制平面 ACL 优先于 acl-default-whitelist 规则。

如果您对应用程序使用非默认端口,NVIDIA 建议您为非默认端口添加白名单规则。例如,如果您使用端口 3020 和 3022 进行 radius 服务器记帐和身份验证,而不是 1812 和 1813,则可以添加以下白名单规则

cumulus@switch:~$ nv set acl acl-default-whitelist rule 73 match ip udp source-port 3020
cumulus@switch:~$ nv set acl acl-default-whitelist rule 73 match ip connection-state new
cumulus@switch:~$ nv set acl acl-default-whitelist rule 73 match ip connection-state established
cumulus@switch:~$ nv set acl acl-default-whitelist rule 73 action permit
cumulus@switch:~$ nv set acl acl-default-whitelist rule 74 match ip udp source-port 3022
cumulus@switch:~$ nv set acl acl-default-whitelist rule 74 match ip connection-state new
cumulus@switch:~$ nv set acl acl-default-whitelist rule 74 match ip connection-state established
cumulus@switch:~$ nv set acl acl-default-whitelist rule 74 action permit
cumulus@switch:~$ nv config apply

Hashlimit 和最近列表匹配

对于控制平面上的防火墙 IPv4 类型 ACL,您可以匹配 hashlimit 和最近列表。数据平面 ACL 不支持这些匹配,这些 ACL 安装在硬件中。

Cumulus Linux 提供了以下命令用于匹配 hashlimit

命令描述
nv set acl <acl> rule <rule> match ip hashlimit namehashlimit 名称。
nv set acl <acl> rule <rule> match ip hashlimit modehashlimit 模式。您可以指定 src-ipdst-ip
nv set acl <acl> rule <rule> match ip hashlimit bursthashlimit 突发速率;突发中要匹配的最大数据包数。您可以指定介于 1 到 4294967295 之间的值。
nv set acl <acl> rule <rule> match ip hashlimit rate-above限制速率。您可以指定 <integer/second><integer/min><integer/hour>。最大速率为 1000000/秒。
nv set acl <acl> rule <rule> match ip hashlimit expire哈希条目过期的毫秒数。
nv set acl <acl> rule <rule> match ip hashlimit source-mask源地址分组前缀长度。
nv set acl <acl> rule <rule> match ip hashlimit destination-mask目标地址分组前缀长度。

以下示例显示了在匹配 hashlimit 时丢弃数据包的 ACL。

要配置 hashlimit 匹配,您必须设置 hashlimit 名称、模式、过期时间、突发和速率;源掩码和目标掩码设置是可选的。

cumulus@switch:~$ nv set acl EXAMPLE1 type ipv4
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip protocol tcp
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip source-ip 10.0.14.2/32
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip hashlimit name ssh
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip hashlimit mode src-ip 
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip hashlimit expire 100
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip hashlimit burst 100
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip hashlimit rate-above 100/second
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip hashlimit source-mask 32
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 action deny
cumulus@switch:~$ nv set interface swp1 acl EXAMPLE1 inbound control-plane
cumulus@switch:~$ nv config apply

NVUE 将此规则写入 /etc/cumulus/acl/policy.d/50_nvue.rules 文件

cumulus@switch:~$ sudo cat /etc/cumulus/acl/policy.d/50_nvue.rules
[iptables]
## ACL EXAMPLE1 in dir inbound on interface swp1 ##
# rule-id #10:  #
-A INPUT -i swp1 -m comment --comment rule_id:10,acl_name:EXAMPLE1,dir:inbound,interface_id:swp1 -s 10.0.14.2/32 -p tcp -m hashlimit --hashlimit-name ssh --hashlimit-mode srcip --hashlimit-htable-expire 100 --hashlimit-burst 100 --hashlimit-above 100/second --hashlimit-srcmask 32 -j DROP

您还可以使用 nv show acl <acl> 命令显示 ACL 设置

cumulus@switch:~$ nv show acl EXAMPLE1
      applied
----  -------
type  ipv4
rule
=======
    Number  Summary                                   
    ------  ------------------------------------------
    10      match.ip.hashlimit.burst:              100
            match.ip.hashlimit.expire:             100
            match.ip.hashlimit.mode:            src-ip
            match.ip.hashlimit.name:            ssh
            match.ip.hashlimit.rate-above: 100/second
            match.ip.hashlimit.source-mask:         32
            match.ip.protocol:                     tcp
            match.ip.source-ip:           10.0.14.2/32

Cumulus Linux 提供了以下命令来匹配 recent list

命令描述
nv set acl <acl> rule <rule> match ip recent-list name最近模块名称。
nv set acl <acl> rule <rule> match ip recent-list action最近操作。您可以指定 setupdate
nv set acl <acl> rule <rule> match ip recent-list hit-count间隔内的命中数。您可以指定介于 1 到 4294967295 之间的值。
nv set acl <acl> rule <rule> match ip recent-list update-interval更新间隔。您可以指定介于 1 到 4294967295 之间的值。

以下示例显示了在匹配 recent-list 时丢弃数据包的 ACL。

要配置最近模块匹配,您必须设置最近列表名称和操作;其他 recent-list 设置是可选的。

cumulus@switch:~$ nv set acl EXAMPLE1 type ipv4
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip protocol tcp
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip source-ip 10.0.14.2/32
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip recent-list name bruteforce
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip recent-list action set
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip recent-list hit-count 5
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 match ip recent-list update-interval 3600
cumulus@switch:~$ nv set acl EXAMPLE1 rule 10 action deny
cumulus@switch:~$ nv set interface swp1 acl EXAMPLE1 inbound control-plane
cumulus@switch:~$ nv config apply

NVUE 将此规则写入 /etc/cumulus/acl/policy.d/50_nvue.rules 文件

cumulus@switch:~$ sudo cat /etc/cumulus/acl/policy.d/50_nvue.rules
[iptables]

## ACL EXAMPLE1 in dir inbound on interface swp1 ##
# rule-id #10:  #
-A INPUT -i swp1 -m comment --comment rule_id:10,acl_name:EXAMPLE1,dir:inbound,interface_id:swp1 -s 10.0.14.2/32 -p tcp -m recent --name bruteforce --set  --hitcount 5 --seconds 360 -j DROP

您还可以使用 NVUE nv show acl <acl> 命令显示 ACL 设置。

显示防火墙规则

要显示 DoS 规则,请运行 nv show acl acl-default-dos 命令

cumulus@switch:~$ nv show acl acl-default-dos
      applied  pending
----  -------  -------
type  ipv4     ipv4   
rule
=======
    Number  Summary                                 
    ------  ----------------------------------------
    30      match.ip.protocol:                   tcp
    40      match.ip.protocol:                   tcp
    41      match.ip.protocol:                   tcp
    42      match.ip.protocol:                   tcp
    50                                              
    60      match.ip.protocol:                   tcp
    70      match.ip.protocol:                   tcp
    80      match.ip.protocol:                   tcp
    90      match.ip.protocol:                   tcp
            match.ip.tcp.all-mss-except:   536-65535
    100     match.ip.recent-list.action:         set
            match.ip.tcp.dest-port:               22
    110     match.ip.recent-list.action:      update
            match.ip.recent-list.hit-count:       50
            match.ip.recent-list.update-interval: 60
            match.ip.tcp.dest-port:               22
    120     match.ip.hashlimit.burst:              2
            match.ip.hashlimit.expire:         30000
            match.ip.hashlimit.mode:          src-ip
            match.ip.hashlimit.name:          TCPRST
            match.ip.hashlimit.rate-above:     5/min
            match.ip.hashlimit.source-mask:       32
            match.ip.protocol:                   tcp
    130     match.ip.hashlimit.burst:             30
            match.ip.hashlimit.expire:         30000
            match.ip.hashlimit.mode:          src-ip
            match.ip.hashlimit.name:      TCPGENERAL
            match.ip.hashlimit.rate-above: 50/second
            match.ip.hashlimit.source-mask:       32
            match.ip.protocol:                   tcp

运行 nv show acl acl-default-dos --rev=applied -o json 命令以显示其他信息,例如连接状态、命中计数和更新间隔

cumulus@switch:~$ nv show acl acl-default-dos --rev=applied -o json
{
  "rule": {
    "100": {
      "action": {
        "recent": {}
      },
      "match": {
        "ip": {
          "connection-state": {
            "new": {}
          },
          "recent-list": {
            "action": "set"
          },
          "tcp": {
            "dest-port": {
              "22": {}
            }
          }
        }
      }
    },
    "110": {
      "action": {
        "deny": {}
      },
      "match": {
        "ip": {
          "connection-state": {
            "new": {}
          },
          "recent-list": {
            "action": "update",
            "hit-count": 50,
            "update-interval": 60
          },
          "tcp": {
            "dest-port": {
              "22": {}
            }
          }
        }
      }
    },
...

要显示白名单规则,请运行 nv show acl acl-default-whitelist 命令

cumulus@switch:~$ nv show acl acl-default-whitelist 
      applied  pending
----  -------  -------
type  ipv4     ipv4
rule
=======
    Number  Summary                                          
    ------  -------------------------------------------------
    5       match.ip.protocol:                            tcp
            match.ip.tcp.dest-port:                       ssh
    10      match.ip.protocol:                            tcp
            match.ip.tcp.dest-port:                       bgp
    15      match.ip.protocol:                            tcp
            match.ip.tcp.dest-port:                      ldap
    20      match.ip.protocol:                            tcp
            match.ip.tcp.dest-port:                      8765
    25      match.ip.protocol:                            tcp
            match.ip.tcp.dest-port:                     https
    30      match.ip.protocol:                            tcp
            match.ip.tcp.dest-port:                      clag
    35      match.ip.protocol:                            tcp
            match.ip.tcp.source-port:                      49
    40      match.ip.protocol:                            udp
            match.ip.udp.dest-port:               dhcp-client
    45      match.ip.protocol:                            udp
            match.ip.udp.dest-port:               dhcp-server
    50      match.ip.protocol:                            udp
            match.ip.udp.dest-port:                       ntp
    55      match.ip.protocol:                            udp
            match.ip.udp.dest-port:                       323
    60      match.ip.protocol:                            udp
            match.ip.udp.dest-port:                      snmp
    65      match.ip.protocol:                            udp
            match.ip.udp.dest-port:                      tftp
    70      match.ip.protocol:                            udp
            match.ip.udp.dest-port:                      ldap
    75      match.ip.protocol:                            udp
            match.ip.udp.source-port:                    1812
    80      match.ip.protocol:                            udp
            match.ip.udp.source-port:                    1813
    85      match.ip.protocol:                            udp
            match.ip.udp.dest-port:                      6343
    90      match.ip.protocol:                            udp
            match.ip.udp.dest-port:                      6344
    95      match.ip.protocol:                            udp
            match.ip.udp.dest-port:                       514
    100     match.ip.protocol:                            udp
            match.ip.udp.dest-port:                       bfd
    105     match.ip.protocol:                            udp
            match.ip.udp.dest-port:              bfd-multihop
    110     match.ip.protocol:                            udp
            match.ip.udp.dest-port:                      4789
    115     match.ip.protocol:                            udp
            match.ip.udp.dest-port:                       319
    120     match.ip.protocol:                            udp
            match.ip.udp.dest-port:                       320
    125     match.ip.protocol:                            tcp
            match.ip.tcp.dest-port:                      9339
    130     match.ip.protocol:                            tcp
            match.ip.tcp.dest-port:                     31980
            match.ip.tcp.dest-port:                     31982
    135     match.ip.protocol:                            tcp
            match.ip.tcp.dest-port:                       639
    140     match.ip.protocol:                            udp
            match.ip.udp.source-port:                      53
    145     match.ip.protocol:                            tcp
            match.ip.tcp.dest-port:                      9999
    150     match.ip.protocol:                           ospf
    155     match.ip.protocol:                            pim
    160     match.ip.protocol:                           vrrp
    165     match.ip.protocol:                           igmp
    170     match.ip.protocol:                           icmp
    9999    Log Level:                                      3
            action.log.log-prefix: IPTables-Dropped-<Domain>:
            Log Rate:                                       1

运行 nv show acl acl-default-whitelist --rev=applied -o json 命令以显示其他信息,例如连接状态

cumulus@switch:~$ nv show acl acl-default-whitelist --rev=applied -o json
{
  "rule": {
    "10": {
      "action": {
        "permit": {}
      },
      "match": {
        "ip": {
          "connection-state": {
            "established": {},
            "new": {}
          },
          "protocol": "tcp",
          "tcp": {
            "dest-port": {
              "bgp": {}
            }
          }
        }
      }
    },
    "100": {
      "action": {
        "permit": {}
      },
      "match": {
        "ip": {
          "connection-state": {
            "established": {},
            "new": {}
          },
          "protocol": "udp",
          "udp": {
            "dest-port": {
              "bfd": {}
            }
          }
        }
      }
...

要显示有关特定规则的信息,请运行 nv show acl acl-default-dos rule <rule> 命令

cumulus@switch:~$ nv show acl acl-default-dos rule 30
              applied  pending
------------  -------  -------
match                         
  ip                          
    protocol  tcp      tcp

运行 nv show acl acl-default-dos rule <rule> --rev=applied -o json 命令以查看其他信息,例如连接状态

cumulus@switch:~$ nv show acl acl-default-dos rule 30 --rev=applied -o json
{
  "action": {
    "permit": {}
  },
  "match": {
    "ip": {
      "connection-state": {
        "established": {},
        "related": {}
      },
      "protocol": "tcp"
    }
  }
}

syslog 消息

默认防火墙规则包括一个日志规则,用于记录到达控制平面且与用户定义或默认防火墙规则不匹配的数据包。交换机为与日志规则匹配的数据包在 /var/log/syslog 中生成日志消息。