Geo-IP

Geo-IP blocks traffic by country, using MaxMind’s free GeoLite2 data. It is checked on the WAN interfaces before the firewall rules, and it only ever blocks — whatever it lets through still has to pass your rules. Each country gets one rule:

  • Block drops new connections from the country (inbound, the default), to it (outbound — LAN clients and the firewall itself get an immediate refusal instead of a timeout), or both.
  • Allow-list turns on allow-list mode: once any country is allow-listed, new inbound WAN connections from every other country are dropped. Private, CGNAT, link-local and multicast ranges and the WAN’s own subnets are always allowed, so the ISP gateway, DHCP and CARP peers keep working.

A country can have one rule. Existing connections from a country are cut when it is blocked.

The allow-list is only enforced when it can be trusted: every allowed country must have networks in the loaded database, and an interface must have the WAN role. Otherwise it is suspended (blocks still apply) and the page says why — enforcing it with a missing database would drop the whole WAN, and without a WAN role it would cut off LAN and VPN hosts with public addresses.

Upgrading. Before #735 an allow rule never took effect. Since it now turns on allow-list mode, existing allow rules are disabled at upgrade (and when restoring an older backup), with a notice on the page; re-enable them if an allow-list is what you want. Countries that had more than one rule keep them, and those rules stay editable, but new duplicates are refused.

The database comes first

A rule is only as good as the networks behind it. Until a GeoLite2 release is loaded, every country table is empty and the Geo-IP page says so. Two ways to load one, both under Firewall → Geo-IP → GeoLite2 database:

  • Automatic (recommended). Create a free account at maxmind.com, generate a license key, and paste your account ID (shown at the top of the portal) and the key into the page (or aifw geoip database --account-id 123456 and --license-key-stdin). MaxMind authenticates downloads with the account ID and key together; a key alone is refused with HTTP 401 unless it predates that change. AiFw downloads the GeoLite2-Country-CSV release at once and then on a schedule — weekly by default, like pfBlockerNG and OPNsense; choose daily, monthly, or off. The key is stored encrypted and never shown again.
  • Manual. Download GeoLite2-Country-CSV_*.zip from your MaxMind account and upload it on the page (or aifw geoip import FILE.zip). The three CSV files inside are accepted as separate uploads too.

The page shows how many countries and networks are loaded, where the data came from, when it was refreshed, when the next download is due, and the last error if a download failed. Each rule shows how many networks the database holds for its country. Ranges MaxMind can’t place in a country fall back to their registered country.

Quickstart

In the Web UI go to Firewall → Geo-IP, load the database as above, then use Add a country: a two-letter code, Block or Allow-list, and for blocks a direction. Changes apply to pf as soon as they are saved; click a row to edit it in place. The page shows:

  • Blocked traffic — packets and bytes dropped inbound (country blocks + allow-list) and outbound, the current rate, and when counting started. Totals survive rule edits and restarts; Reset zeroes them.
  • Policy — the mode (blocklist, allow-list or both), the interfaces it applies on, how many networks each merged table holds, and pf table capacity.
  • Check an address — the country of an IP and whether the current policy blocks it inbound or outbound.

If a change would block the address you are connected from, the page asks before applying it (the API answers 409; resend with confirm_lockout).

Block a country in both directions:

curl -X POST https://aifw.local/api/v1/geoip \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ "country": "CN", "action": "block", "direction": "both" }'

Only allow inbound connections from the US:

curl -X POST https://aifw.local/api/v1/geoip \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ "country": "US", "action": "allow" }'

What does the policy do with an address?

curl https://aifw.local/api/v1/geoip/check/1.2.3.4 -H "Authorization: Bearer $TOKEN"
{ "data": { "ip": "1.2.3.4", "country": "AU", "network": "1.2.3.0/24", "mode": "blocklist",
  "verdict": { "blocked_in": true, "in_blocklist": true, "outside_allowlist": false, "blocked_out": false } } }

CLI

aifw geoip add --country CN --action block                     # inbound
aifw geoip add --country RU --action block --direction both
aifw geoip add --country US --action allow                     # allow-list mode
aifw geoip edit <uuid> --direction out
aifw geoip list                                                # rules with network counts
aifw geoip remove <uuid>
aifw geoip policy                                              # mode, tables, capacity, blocked in/out
aifw geoip check 1.2.3.4                                       # country + verdict
aifw geoip reset-traffic --yes

# the database
aifw geoip database                                            # loaded? source, schedule, last error
printf '%s' "$KEY" | aifw geoip database --license-key-stdin
aifw geoip database --update-interval-hours 168                # 0 turns the schedule off
aifw geoip refresh                                             # download from MaxMind now
aifw geoip import GeoLite2-Country-CSV_20261001.zip
aifw geoip lookup 1.2.3.4

Commands that would block your own address fail with the API’s explanation; add --confirm-lockout to apply them anyway.

API endpoints

Method Endpoint Description
GET /api/v1/geoip List rules (with direction, merged table, cidr_count)
POST /api/v1/geoip Create a rule: country, action (block/allow), direction (in/out/both, blocks only), label, status, confirm_lockout
PUT /api/v1/geoip/{id} Update a rule (same body)
DELETE /api/v1/geoip/{id} Delete a rule (?confirm_lockout=true)
GET /api/v1/geoip/policy Mode, WAN interfaces, tables, pf capacity, blocked traffic in/out
GET /api/v1/geoip/check/{ip} Country plus the policy’s verdict
POST /api/v1/geoip/traffic/reset Zero the blocked-traffic totals
DELETE /api/v1/geoip/notice Dismiss the upgrade/restore notice
GET /api/v1/geoip/lookup/{ip} Resolve an IP to its country code
GET /api/v1/geoip/database Database status: loaded counts, source, schedule, last error
PUT /api/v1/geoip/database { "license_key": "…" \| null, "account_id": "123456" \| null, "update_interval_hours": 168 }
POST /api/v1/geoip/database/refresh Download the GeoLite2 Country release from MaxMind now
POST /api/v1/geoip/database/upload Multipart upload of the GeoLite2 zip or its CSV files

Refusals carry a reason in the body: 400 for an invalid or conflicting rule (one rule per country; allow-list entries are inbound only) or a policy too large for the box’s memory, 409 for a change that would block the caller.

How it works

All countries share three root pf tables, filled from the GeoLite2 database with overlapping and adjacent networks merged:

Table Holds
aifw_geoip_block_in countries blocked inbound
aifw_geoip_block_out countries blocked outbound
aifw_geoip_allow_in allow-listed countries + always-allowed ranges + WAN subnets

The aifw-geoip anchor holds at most three rules, bound to the WAN interfaces (interfaces with the WAN role plus multi-WAN gateways; every interface if none is set):

block drop in quick on { em0 } from <aifw_geoip_block_in> label "geoip-block-in"
block drop in quick on { em0 } from ! <aifw_geoip_allow_in> label "geoip-allowlist"
block return out quick on { em0 } to <aifw_geoip_block_out> label "geoip-block-out"

One table per direction means a new connection costs a single table lookup no matter how many countries are listed — this matters under a SYN flood, where pf evaluates the ruleset for every packet that matches no state. The anchor never defines the tables itself: a table line inside the anchor would create an empty anchor-local copy that shadows the root table (#704).

Evaluation order. aifw-geoip is hooked into pf.conf right after the plugin anchor and ahead of multi-WAN policy routing and the main rule set (aifw). Every AiFw anchor decides packets with quick rules and the main rule set ends in a catch-all block, so an anchor hooked after it never sees traffic; a geo-IP block therefore wins over any port you have opened. Appliances installed before #735 hooked the anchor after aifw, where it never matched; the API reorders the hooks in pf.conf.aifw on startup.

Applying. Tables are filled before the anchor loads and emptied only after it, so an allow-list rule is never live over an empty table (which would drop all inbound WAN traffic); a failure leaves the previous rules in force. Unchanged tables are not pushed again. States that a new rule would have refused are killed (pfctl -k src -k dst, at most 1000 pairs per apply): states created inbound on a WAN interface from a blocked source, and states created outbound to a blocked destination. A LAN client’s own connection is never cut by an inbound rule, and with floating states (no interface recorded) inbound states are left alone rather than guessed at — the appliance binds states to interfaces.

Capacity. pf’s table-entries limit (200k by default) is shared with threat feeds and aliases. A US-only allow-list alone is about 262k networks, so AiFw raises the limit in pf.conf.aifw when a policy needs it, keeping 200k of headroom for other tables. Policies whose tables would take more than an eighth of physical memory are refused.

Blocked traffic. The totals come from pf’s per-rule label counters (pfctl -a aifw-geoip -sl). pf zeroes those whenever the anchor reloads or pf restarts, so the API banks them — right before and after every reload it performs — keeps running totals in memory, and checkpoints them to the database once a minute.

The freebsd/tests/t12-geoip.sh harness test proves the path on a real kernel: a geo-IP block beating an operator pass quick, outbound blocks, the allow-list and its WAN-local exemption, cut connections and the counters.

Configuration

Field Default Notes
Anchor name aifw-geoip Dedicated anchor, separate from the main rule list
db_path /var/db/aifw/geoip GeoLite2 CSV directory (GeoLite2-Country-Blocks-IPv4.csv, -IPv6.csv, GeoLite2-Country-Locations-en.csv)
update_interval_hours 168 (weekly) Scheduled MaxMind download; 0 disables. A failed download is retried after 6 hours
license_key unset MaxMind license key; sealed at rest, required for downloads (uploads work without one)
account_id unset MaxMind account ID; with it set, downloads use the permalink with HTTP Basic auth (required for keys issued by the current portal)
country (rule) required Two-letter ISO 3166-1 alpha-2 code; case-insensitive on input; one rule per country
direction (rule) in in, out or both for blocks; allow-list rules are always in
label (rule) unset Operator note; not rendered into pf (countries share rules)

See also

Last updated: