惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
V
V2EX
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
Martin Fowler
Martin Fowler
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
The Cloudflare Blog
T
Tailwind CSS Blog
H
Help Net Security
腾讯CDC
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
D
DataBreaches.Net
C
Check Point Blog
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

Jiajun的技术笔记

你好,2026! TiDB 源码阅读(六):TiDB Coprocessor 源码解析 性能优化的核心思想 TiDB 源码阅读(五):索引 TiDB 源码阅读(四):AST、逻辑计划、物理计划 CockroachDB Serverless Architecture podman 无故退出 Cursor Control-L (CTRL-L) Keyboard Shortcuts in Terminal Replace docker with podman Using xmonad with xfce4 A RC script for freebsd frpc 自己动手写一个k8s controller AI 会取代你的(编程)岗位吗? 自建DERP服务器提升Tailscale连接速度(使用Nginx转发) 自动升级Docker容器 再读《程序员修炼之道-从小工到专家》 让浏览器下载文件 再读《软件随想录》/《黑客与画家》/《软技能》 HTTP 压力测试中的 Coordinated Omission 2的补码 编程语言中的 context 是什么? flutter macOS 构建出错 Flatpak 使用小记 Golang CAS 操作是怎么实现的 PostgreSQL 当MQ来使用 Clash 结合 工作VPN 的网络设计 使用 PostgreSQL 搭建 JuiceFS PostgreSQL 配置优化和日志分析 有GitHub Copilot?那就可以搭建你的ChatGPT4服务 窗口函数的使用(以PG为例)
FreeBSD ipfw使用教程
Jiajun Huang · 2020-04-13 · via Jiajun的技术笔记

FreeBSD,古老的UNIX系统,最近在研究它的ipfw防火墙,鉴于国内相关资料较少,我就记录下来,以飨读者。

首先在FreeBSD 12中,ipfw已经默认编译进内核了,所以中文资料包括很多英文资料里,还需要编译的,就不用看了,那是过时的。

注意ipfw有一个比较坑的地方,那就是它默认会有一条规则,规则号为65536,是不可以删除的,这条规则会把所有流量都切断, 所以还没配置好之前,千万不要随意启动ipfw,否则就会面临无法连上远程FreeBSD的尴尬问题了。

$ sudo ipfw list
Password:
65535 deny ip from any to any

ipfw的规则是这样的,ipfw有一个规则编号,按照规则编号一次进行处理,所以由于最后一条是deny所有流量,就会产生刚才所说的 那个问题。

我们来看看ipfw的规则长啥样:

RULE FORMAT
     The format of firewall rules is the following:

           [rule_number] [set set_number] [prob match_probability] action
           [log [logamount number]] [altq queue] [{tag | untag} number] body

     where the body of the rule specifies which information is used for
     filtering packets, among the following:

        Layer-2 header fields                 When available
        IPv4 and IPv6 Protocol                SCTP, TCP, UDP, ICMP, etc.
        Source and dest. addresses and ports
        Direction                             See Section PACKET FLOW
        Transmit and receive interface        By name or address
        Misc. IP header fields                Version, type of service,
                                              datagram length, identification,
                                              fragment flag (non-zero IP
                                              offset), Time To Live
        IP options
        IPv6 Extension headers                Fragmentation, Hop-by-Hop
                                              options, Routing Headers, Source
                                              routing rthdr0, Mobile IPv6
                                              rthdr2, IPSec options.
        IPv6 Flow-ID
        Misc. TCP header fields               TCP flags (SYN, FIN, ACK, RST,
                                              etc.), sequence number,
                                              acknowledgment number, window
        TCP options
        ICMP types                            for ICMP packets
        ICMP6 types                           for ICMP6 packets
        User/group ID                         When the packet can be
                                              associated with a local socket.
        Divert status                         Whether a packet came from a
                                              divert socket (e.g., natd(8)).
        Fib annotation state                  Whether a packet has been tagged
                                              for using a specific FIB
                                              (routing table) in future
                                              forwarding decisions.

  • rule_number 是从1到65536的一个数字,这些规则会按照这个数值从小到大依次检查,如果规则号相同,就会按照在文件中的顺序检查
  • set, tag, untag, altqprob 不管,prob 是用来随机丢包用的
  • log 表示是否打日志
  • action 是我们要对流量采取的行动,比如 deny, allow
  • body 就是我们的具体规则,它的语法是 [proto from src to dst] [options]

我们来看一个具体的规则:

ipfw add 100 allow ip from not 1.2.3.4 to any,其中 ip 这里是协议,可选值是 ip, tcp, udp,而 src 和 dst 可以是 一个ip地址,一个网络号,也可以是 any,比如通常我们不想把本机出去的流量给掐掉,那么就加上这么一句:

ipfw -q add 110 allow all from any to any out

注意,src 和 dst 都可以加一个具体的端口号,比如我们要允许别的机器可以访问22:

ipfw -q add 130 allow tcp from any to any 22 in

配置ipfw

看完了规则的语法要求,我们来看看该怎么配置ipfw,执行以下命令:

$ sudo sysrc firewall_enable="YES"  # 允许防火墙开机自启
$ sudo sysrc firewall_type="open"  # 让系统把流量通过,这样就可以使用防火墙
$ sudo sysrc firewall_script="/etc/ipfw.rules"  # 制定ipfw规则的路径,我们待会儿在这里编辑规则
$ sudo sysrc firewall_logging="YES"  # 这样ipfw就可以打日志
$ sudo sysrc firewall_logif="YES"  # 把日志打到 `ipfw0` 这个设备里

然后编辑 /etc/ipfw.rules

IPF="ipfw -q add"
ipfw -q -f flush

#loopback 
$IPF 10 allow all from any to any via lo0
$IPF 20 deny all from any to 127.0.0.0/8
$IPF 30 deny all from 127.0.0.0/8 to any
$IPF 40 deny tcp from any to any frag

# statefull
$IPF 50 check-state
$IPF 60 allow tcp from any to any established
$IPF 70 allow all from any to any out keep-state
$IPF 80 allow icmp from any to any

# open port for ssh
$IPF 110 allow all from any to any out
$IPF 130 allow tcp from any to any 22 in

# deny and log everything 
$IPF 500 deny log all from any to any

最后,启动ipfw:

$ sudo service ipfw start

参考资料: