A first-match firewall

A firewall reads its rules from the top, and the first rule that matches a packet decides. If none matches, the default policy does.

What to build

Apply an ordered rule list to a list of packets.

Requirements

  • Line 1: the default policy, allow or deny. Line 2: N. Then N rules action protocol port source, where protocol is tcp, udp or any, port is a number or any, and source is an IPv4 CIDR block or any.
  • Each remaining line is a packet protocol port source-ip.
  • A rule matches when its protocol is any or equal, its port is any or equal, and the source IP is inside its block.
  • Print allow by rule K or deny by rule K (counting rules from 1), or allow by default / deny by default.

Input

As described.

Output

One line per packet.

Constraints

Up to 20 rules and 50 packets. All addresses are valid IPv4.

Example

Input:

deny
2
allow tcp 443 any
allow tcp 22 10.0.0.0/8
tcp 443 203.0.113.7
tcp 22 10.1.2.3
tcp 22 203.0.113.7

Output:

allow by rule 1
allow by rule 2
deny by default

Why it works

Checking the rules in order and stopping at the first match is exactly how the firewall decides, so a broad rule above a specific one hides it, and the default only applies when the whole list is passed.

Examples

Example 1 · Input
deny
2
allow tcp 443 any
allow tcp 22 10.0.0.0/8
tcp 443 203.0.113.7
tcp 22 10.1.2.3
tcp 22 203.0.113.7
Expected output
allow by rule 1
allow by rule 2
deny by default
Example 2 · Input
allow
1
deny udp 53 any
udp 53 8.8.8.8
tcp 53 8.8.8.8
Expected output
deny by rule 1
allow by default

Hints

Hint 1

Turn an address into one 32-bit number; two addresses are in the same /n block when dividing by 2^(32 − n) gives the same whole number.

Hint 2

Remember that `any` can appear in the protocol, port and source fields.

Solution

Show one correct solution (try it yourself first)
function toNumber(ip) {
  const p = ip.split(".").map(Number);
  return ((p[0] * 256 + p[1]) * 256 + p[2]) * 256 + p[3];
}
function inBlock(ip, block) {
  if (block === "any") return true;
  const [base, bits] = block.split("/");
  const size = 2 ** (32 - Number(bits));
  return Math.floor(toNumber(ip) / size) === Math.floor(toNumber(base) / size);
}
const policy = prompt().trim();
const n = Number(prompt());
const rules = [];
for (let k = 0; k < n; k++) {
  const [action, protocol, port, source] = prompt().trim().split(/\s+/);
  rules.push({ action, protocol, port, source });
}
let line = prompt();
while (line !== null) {
  if (line.trim()) {
    const [protocol, port, ip] = line.trim().split(/\s+/);
    let decided = null;
    for (let k = 0; k < rules.length; k++) {
      const r = rules[k];
      if ((r.protocol === "any" || r.protocol === protocol) && (r.port === "any" || r.port === port) && inBlock(ip, r.source)) {
        decided = `${r.action} by rule ${k + 1}`;
        break;
      }
    }
    console.log(decided ?? `${policy} by default`);
  }
  line = prompt();
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Ports and firewalls →