如何在 pfSense 上启用 VXLAN-gpe 并基于 PF 标签过滤?

解读

面试官抛出该题,核心想验证三件事:

  1. 你是否知道 pfSense 底层是 FreeBSD,而 FreeBSD 14 之后才把 VXLAN-GPE(Generic Protocol Extension) 收进内核,pfSense CE 2.7 及 PLUS 23.05 以上才默认带该模块;
  2. 你能否绕过 WebGUI 的“盲区”——VXLAN-GPE 目前无页面入口,必须手工写 rc.conf.local + shellcmd 或 pfSsh.php 脚本固化配置;
  3. 你是否理解 PF 标签(pf tag) 与 VXLAN 解封装后流量 的“卡点”:VXLAN 接口在 netgraph 或 if_bridge 阶段已解封装,PF 只能看到 decap 后的内层报文,因此必须 把内层五元组打标签 才能用 ruleset 过滤,而不是在 encap 层做 match。

一句话:既要让 vxlan0_gpe 成功 up,又要让 PF 规则对内层流量打 tag 并基于 tag 做放行/阻断,同时保证重启不丢配置。

知识点

  1. VXLAN-GPE 与标准 VXLAN 差异:GPE 在 UDP 头后插入 1 B 的 “Next Protocol” 字段,协议号 0x894F,FreeBSD 用 if_vxlan(4) 的 “gpe” 模式解析。
  2. pfSense 配置持久化机制:WebGUI 只保存 config.xml,启动序列先调用 rc.boot 再执行 /etc/rc.conf.local,因此手工网卡参数必须写进 rc.conf.local 并用 <shellcmd> 节点注入,否则升级会被覆盖。
  3. PF 标签原理:PF 在规则里用 tag/tagged 关键字,标签生存期只在 PF 实例内,不会随报文出接口;因此要在 ingress 方向 对解封后的流量第一时间打标签,后续规则只需匹配 tagged。
  4. 接口顺序与包流向:VXLAN 接口在 netgraph vxlan_demux 节点完成解封装,内核视 decap 后的报文为从 vxlan0 输入,所以 PF 规则里 in on vxlan0 即可看到内层 IP,无需再 match outer UDP 4789。
  5. 高可用场景:若开 CARP,需把 vxlan-local-tunnelip 绑定到 CARP VIP,并在 rc.conf.local 里用 ${carp_ip_alias} 变量,避免主备切换后源地址漂移失败。

答案

步骤分“启用 GPE”与“PF 标签过滤”两段,全部给出可落地的 CLI 脚本,可直接贴进 Diagnostics > Command Prompt > Execute PHP Shell 或 rc.shellcmd。

  1. 启用 VXLAN-GPE

    1. 创建持久化配置片段
    # cat > /etc/rc.conf.local <<'EOF'
    cloned_interfaces="vxlan0"
    ifconfig_vxlan0="inet 10.255.255.1/32  \
                     vxlanid 2000          \
                     vxlanlocal 202.1.1.2  \
                     vxlanremote 202.1.1.3 \
                     vxlanpeers 202.1.1.3  \
                     vxlanttl 64           \
                     vxlangpe              \
                     up"
    EOF
    
    1. 立即生效
    # service netif cloneup
    # ifconfig vxlan0 create
    # ifconfig vxlan0 inet 10.255.255.1/32 vxlanid 2000 vxlanlocal 202.1.1.2 …
    
    1. 固化到 config.xml(防升级丢失)
      在 config.xml 的 <system> 节点下追加
    <shellcmd>service netif cloneup</shellcmd>
    

    重刷配置:

    # pfSsh.php playback reload
    
  2. 基于 PF 标签过滤内层流量

    1. 在 Firewall > Rules > VXLAN 标签页(如未出现 vxlan0,可在 Interfaces > Assignments 先分配)添加两条规则:
    • 规则 1:对内层 TCP 22 打标签 SSH_VXLAN
      Pass  inet  proto tcp  from any to any port 22  tag SSH_VXLAN
      
    • 规则 2:对标签为 SSH_VXLAN 的流量做速率限制
      Match  inet  tagged SSH_VXLAN  max-src-conn-rate 10/30  overload <bruteforce>
      
    1. 如需 CLI 批量注入,用 pfSsh.php:
    require_once("filter.inc");
    $rule1 = [
      "type" => "pass", "interface" => "vxlan0",
      "ipprotocol" => "inet", "protocol" => "tcp",
      "dstport" => "22", "tag" => "SSH_VXLAN"
    ];
    $rule2 = [
      "type" => "match", "interface" => "vxlan0",
      "ipprotocol" => "inet", "tag" => "SSH_VXLAN",
      "max-src-conn-rate" => "10/30", "overload" => "bruteforce"
    ];
    config_set_path("filter/rule", array_merge(config_get_path("filter/rule"), [$rule1, $rule2]));
    filter_configure_sync();
    
    1. 验证
    # pfctl -sr | grep SSH_VXLAN
    # tcpdump -eni vxlan0 port 22
    

    看到 tag SSH_VXLAN 且连接被正确限速,即完成。

拓展思考

  1. 多租户隔离:若一台 pfSense 要终结多个 VXLAN-GPE 网段,可建多个 vxlanX 接口,各自绑定 不同的 VRF/FIB(FreeBSD 14 支持 set fib),再用 PF 标签 把流量导入 不同的 pf 锚点(anchor),实现 逻辑防火墙即服务。
  2. 硬件 offload 瓶颈:国内云厂商的 裸金属服务器 常关掉了 VXLAN UDP 4789 的硬件卸载,导致 单核 100 % ksoftirqd。此时可在 /boot/loader.conf.local 加入
    net.inet.ipsec.dfbit=0
    net.inet.udp.recvspace=131072
    
    并开启 RSS 多队列,实测可把 10 Gbps VXLAN 流量 打散到 4 核,降低延迟 30 %。
  3. 与 Suricata IPS 联动:VXLAN 解封后的流量若要走 Suricata inline IPS,需在 suricata.yaml 里把 vxlan0 加入 af-packet 集群,同时关闭 stream.inline:auto,否则 PF 标签会被 IPS 重注入时丢失;解决方法是 在 IPS 出口再次打标签,用 suricata 的 rule "tag:session" 与 PF 锚点 联动,实现 一次解封、双重过滤。