2011-04-15 03:59:12 -04:00
---
2021-03-13 13:06:18 -05:00
lang: en
2015-04-10 16:17:45 -04:00
layout: doc
2016-11-12 15:39:24 -05:00
permalink: /doc/firewall/
2015-10-11 03:04:59 -04:00
redirect_from:
2017-05-13 21:09:39 -04:00
- /doc/qubes-firewall/
- /en/doc/qubes-firewall/
2015-10-11 03:04:59 -04:00
- /doc/QubesFirewall/
- /wiki/QubesFirewall/
2021-03-13 13:06:18 -05:00
ref: 166
2021-03-13 12:42:50 -05:00
title: The Qubes Firewall
2011-04-15 03:59:12 -04:00
---
2019-04-06 19:44:36 -04:00
The Qubes Firewall
==================
2013-01-01 07:26:47 -05:00
Understanding firewalling in Qubes
----------------------------------
2011-04-15 03:59:12 -04:00
2019-08-18 14:12:54 -04:00
Every qube in Qubes is connected to the network via a FirewallVM, which is used to enforce network-level policies.
By default there is one default FirewallVM, but the user is free to create more, if needed.
2011-04-15 03:59:12 -04:00
2013-01-01 07:26:47 -05:00
For more information, see the following:
2011-04-15 03:59:12 -04:00
2021-03-13 12:03:23 -05:00
- [https://groups.google.com/group/qubes-devel/browse\_thread/thread/9e231b0e14bf9d62 ](https://groups.google.com/group/qubes-devel/browse_thread/thread/9e231b0e14bf9d62 )
- [https://blog.invisiblethings.org/2011/09/28/playing-with-qubes-networking-for-fun.html ](https://blog.invisiblethings.org/2011/09/28/playing-with-qubes-networking-for-fun.html )
2019-04-06 19:44:36 -04:00
2013-01-01 07:26:47 -05:00
How to edit rules
-----------------
2011-04-15 03:59:12 -04:00
2020-12-27 11:20:30 -05:00
In order to edit rules for a given qube, select it in the Qube Manager and press the "firewall" button.
2011-04-15 03:59:12 -04:00
2021-02-15 20:44:31 -05:00
![r4.0-manager-firewall.png ](/attachment/wiki/QubesFirewall/r4.0-manager-firewall.png )
2013-01-01 07:26:47 -05:00
2020-12-27 11:20:30 -05:00
If the qube is running, you can open Settings from the Qube Popup Menu.
2019-08-18 14:12:54 -04:00
*R4.0 note:* ICMP and DNS are no longer accessible in the GUI, but can be changed via `qvm-firewall` described below.
2021-03-13 12:03:23 -05:00
Connections to Updates Proxy are not made over a network so can not be allowed or blocked with firewall rules, but are controlled using the relevant policy file (see [R4.0 Updates proxy ](/doc/software-update-vm/ ) for more detail).
2018-02-02 08:06:10 -05:00
2019-08-18 14:12:54 -04:00
Note that if you specify a rule by DNS name it will be resolved to IP(s) *at the moment of applying the rules* , and not on the fly for each new connection.
2020-12-27 11:20:30 -05:00
This means it will not work for servers using load balancing, and traffic to complex web sites which draw from many servers will be difficult to control.
Instead of using the firewall GUI, you can use the `qvm-firewall` command in Dom0 to edit the firewall rules by hand.
2021-03-13 12:03:23 -05:00
This gives you greater control than by using the GUI.
2011-04-15 03:59:12 -04:00
2020-12-27 11:20:30 -05:00
The firewall rules for each qube are saved in an XML file in that qube's directory in dom0:
2016-07-30 19:55:25 -04:00
2021-03-13 12:03:23 -05:00
```
/var/lib/qubes/appvms/< vm-name > /firewall.xml
```
2020-12-27 11:20:30 -05:00
Rules are implemented on the netvm.
2016-11-08 13:15:36 -05:00
2021-03-13 12:03:23 -05:00
You can also manually create rules in the qube itself using standard firewalling controls.
See [Where to put firewall rules ](#where-to-put-firewall-rules ).
2019-08-18 14:12:54 -04:00
In complex cases, it might be appropriate to load a ruleset using `iptables-restore` called from `/rw/config/rc.local` .
2020-12-27 11:20:30 -05:00
if you do this, be aware that `rc.local` is called *after* the network is up, so local rules should not be relied upon to block leaks.
2013-01-01 07:42:59 -05:00
2020-12-27 11:20:30 -05:00
Reconnecting qubes after a NetVM reboot
2019-04-06 19:44:36 -04:00
-------------------------------------
2018-02-02 08:06:10 -05:00
2019-08-18 14:12:54 -04:00
Normally Qubes doesn't let the user stop a NetVM if there are other qubes running which use it as their own NetVM.
But in case the NetVM stops for whatever reason (e.g. it crashes, or the user forces its shutdown via qvm-kill via terminal in Dom0), Qubes R4.0 will often automatically repair the connection.
2020-12-27 11:20:30 -05:00
If it does not, then there is an easy way to restore the connection to the NetVM by issuing in dom0:
2018-02-02 08:06:10 -05:00
` qvm-prefs <vm> netvm <netvm> `
2019-08-18 14:12:54 -04:00
Normally qubes do not connect directly to the actual NetVM which has networking devices, but rather to the default sys-firewall first, and in most cases it would be the NetVM that will crash, e.g. in response to S3 sleep/restore or other issues with WiFi drivers.
In that case it is only necessary to issue the above command once, for the sys-firewall (this assumes default VM-naming used by the default Qubes installation):
2018-02-02 08:06:10 -05:00
` qvm-prefs sys-firewall netvm sys-net `
2018-03-13 16:25:59 -04:00
Network service qubes
2019-04-06 19:44:36 -04:00
---------------------
2019-08-18 14:12:54 -04:00
Qubes does not support running any networking services (e.g. VPN, local DNS server, IPS, ...) directly in a qube that is used to run the Qubes firewall service (usually sys-firewall) for good reasons.
2020-12-27 11:20:30 -05:00
In particular, if you want to ensure proper functioning of the Qubes firewall, you should not tinker with iptables or nftables rules in such qubes.
2018-03-13 16:25:59 -04:00
2020-12-27 11:20:30 -05:00
Instead, you should deploy a network infrastructure such as
2021-03-13 12:03:23 -05:00
2018-03-13 16:25:59 -04:00
~~~
sys-net < -- > sys-firewall-1 < -- > network service qube < -- > sys-firewall-2 < -- > [client qubes]
~~~
2021-03-13 12:03:23 -05:00
2020-12-27 11:20:30 -05:00
Thereby sys-firewall-1 is only needed if you have other client qubes connected there, or you want to manage the traffic of the local network service qube.
2019-03-08 22:41:48 -05:00
The sys-firewall-2 proxy ensures that:
2021-03-13 12:03:23 -05:00
2018-03-13 16:25:59 -04:00
1. Firewall changes done in the network service qube cannot render the Qubes firewall ineffective.
2019-03-08 22:41:48 -05:00
2. Changes to the Qubes firewall by the Qubes maintainers cannot lead to unwanted information leakage in combination with user rules deployed in the network service qube.
3. A compromise of the network service qube does not compromise the Qubes firewall.
2018-03-13 16:25:59 -04:00
2020-12-27 11:20:30 -05:00
If you adopt this model, you should be aware that all traffic will arrive at the `network service qube` appearing to originate from the IP address of `sys-firewall-2` .
2020-12-08 01:54:52 -05:00
For the VPN service please also look at the [VPN documentation ](https://github.com/Qubes-Community/Contents/blob/master/docs/configuration/vpn.md ).
2018-03-13 16:25:59 -04:00
2016-11-08 13:15:36 -05:00
Enabling networking between two qubes
2019-04-06 19:44:36 -04:00
-------------------------------------
2013-01-01 07:42:59 -05:00
2021-03-13 12:03:23 -05:00
Normally any networking traffic between qubes is prohibited for security reasons.
2020-12-27 11:20:30 -05:00
However, in special situations, you might want to selectively allow specific qubes to establish networking connectivity between each other.
For example, this might be useful in some development work, when you want to test networking code, or to allow file exchange between HVM domains (which do not have Qubes tools installed) via SMB/scp/NFS protocols.
2013-01-01 07:42:59 -05:00
2016-11-08 13:15:36 -05:00
In order to allow networking between qubes A and B follow these steps:
2013-01-01 07:42:59 -05:00
2021-03-13 12:03:23 -05:00
- Make sure both A and B are connected to the same firewall vm (by default all VMs use the same firewall VM).
- Note the Qubes IP addresses assigned to both qubes.
2019-08-18 14:12:54 -04:00
This can be done using the `qvm-ls -n` command, or via the Qubes Manager preferences pane for each qube.
2021-03-13 12:03:23 -05:00
- Start both qubes, and also open a terminal in the firewall VM
- In the firewall VM's terminal enter the following iptables rule:
2013-01-01 07:42:59 -05:00
2016-04-13 13:01:53 -04:00
~~~
sudo iptables -I FORWARD 2 -s < IP address of A > -d < IP address of B > -j ACCEPT
~~~
2013-01-01 07:42:59 -05:00
2021-03-13 12:03:23 -05:00
- In qube B's terminal enter the following iptables rule:
2016-07-29 15:56:25 -04:00
~~~
sudo iptables -I INPUT -s < IP address of A > -j ACCEPT
~~~
2021-03-13 12:03:23 -05:00
- Now you should be able to reach B from A -- test it using e.g. ping issued from A.
2019-08-18 14:12:54 -04:00
Note however, that this doesn't allow you to reach A from B -- for this you would need two more rules, with A and B swapped.
2021-03-13 12:03:23 -05:00
- If everything works as expected, then you should write the above iptables rules into firewallVM's `qubes-firewall-user-script` script.
This script is run when the netvm starts up.
2020-12-27 11:20:30 -05:00
You should also write relevant rules in A and B's `rc.local` script which is run when the qube is launched.
2021-03-13 12:03:23 -05:00
Here's an example how to update the script:
2013-01-01 07:42:59 -05:00
2016-04-13 13:01:53 -04:00
~~~
2016-07-29 15:56:25 -04:00
[user@sys-firewall ~]$ sudo bash
[root@sys-firewall user]# echo "iptables -I FORWARD 2 -s 10.137.2.25 -d 10.137.2.6 -j ACCEPT" >> /rw/config/qubes-firewall-user-script
[root@sys-firewall user]# chmod +x /rw/config/qubes-firewall-user-script
~~~
2021-03-13 12:03:23 -05:00
- Here is an example how to update `rc.local` :
2016-07-29 15:56:25 -04:00
~~~
[user@B ~]$ sudo bash
[root@B user]# echo "iptables -I INPUT -s 10.137.2.25 -j ACCEPT" >> /rw/config/rc.local
[root@B user]# chmod +x /rw/config/rc.local
2016-04-13 13:01:53 -04:00
~~~
2013-01-01 07:42:59 -05:00
2019-08-09 08:28:59 -04:00
Opening a single TCP port to other network-isolated qube
--------------------------------------------------------
2020-12-27 11:20:30 -05:00
In the case where a specific TCP port needs to be exposed from a qubes to another one, you do not need to enable networking between them but you can use the qubes RPC service `qubes.ConnectTCP` .
2019-08-09 08:28:59 -04:00
**1. Simple port binding**
Consider the following example. `mytcp-service` qube has a TCP service running on port `444` and `untrusted` qube needs to access this service.
2021-03-13 12:03:23 -05:00
- In dom0, add the following to `/etc/qubes-rpc/policy/qubes.ConnectTCP` :
~~~
untrusted @default allow,target=mytcp-service
~~~
- In untrusted, use the Qubes tool `qvm-connect-tcp` :
~~~
[user@untrusted #]$ qvm-connect-tcp 444:@default:444
~~~
2019-08-09 08:28:59 -04:00
2020-12-27 11:20:30 -05:00
> Note: The syntax is the same as SSH tunnel handler. The first `444` correspond to the localport destination of `untrusted`, `@default` the remote machine and the second `444` to the remote machine port.
2019-08-09 08:28:59 -04:00
The service of `mytcp-service` running on port `444` is now accessible in `untrusted` as `localhost:444` .
Here `@default` is used to hide the destination qube which is specified in the Qubes RPC policy by `target=mytcp-service` . Equivalent call is to use the tool as follow:
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
~~~
[user@untrusted #]$ qvm-connect-tcp ::444
~~~
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
which means to use default local port of `unstrusted` as the same of the remote port and unspecified destination qube is `@default` by default in `qrexec` call.
**2. Binding remote port on another local port**
Consider now the case where someone prefers to specify the destination qube and use another port in untrusted,for example `10044` . Instead of previous case, add
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
~~~
untrusted mytcp-service allow
~~~
2021-03-13 12:03:23 -05:00
2019-08-30 11:03:10 -04:00
in `/etc/qubes-rpc/policy/qubes.ConnectTCP` and in untrusted, use the tool as follow:
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
~~~
[user@untrusted #]$ qvm-connect-tcp 10444:mytcp-service:444
~~~
The service of `mytcp-service` running on port `444` is now accessible in `untrusted` as `localhost:10444` .
**3. Binding to different qubes using RPC policies**
2019-08-30 11:03:10 -04:00
One can go further than the previous examples by redirecting different ports to different qubes. For example, let assume that another qube `mytcp-service-bis` with a TCP service is running on port `445` . If someone wants `untrusted` to be able to reach this service but port `445` is reserved to `mytcp-service-bis` then, in dom0, add the following to `/etc/qubes-rpc/policy/qubes.ConnectTCP+445` :
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
~~~
untrusted @default allow,target=mytcp-service-bis
~~~
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
In that case, calling `qvm-connect-tcp` like previous examples, will still bind TCP port `444` of `mytcp-service` to `untrusted` but now, calling it with port `445`
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
~~~
[user@untrusted #]$ qvm-connect-tcp ::445
~~~
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
will restrict the binding to only the corresponding TCP port of `mytcp-service-bis` .
**4. Permanent port binding**
For creating a permanent port bind between two qubes, `systemd` can be used. We use the case of the first example. In `/rw/config` (or any place you find suitable) of qube `untrusted` , create `my-tcp-service.socket` with content:
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
~~~
[Unit]
Description=my-tcp-service
[Socket]
ListenStream=127.0.0.1:444
Accept=true
[Install]
WantedBy=sockets.target
~~~
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
and `my-tcp-service@.service` with content:
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
~~~
[Unit]
Description=my-tcp-service
[Service]
ExecStart=qrexec-client-vm '' qubes.ConnectTCP+444
StandardInput=socket
StandardOutput=inherit
~~~
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
In `/rw/config/rc.local` , append the lines:
2021-03-13 12:03:23 -05:00
2019-08-09 08:28:59 -04:00
~~~
cp -r /rw/config/my-tcp-service.socket /rw/config/my-tcp-service@.service /lib/systemd/system/
systemctl daemon-reload
systemctl start my-tcp-service.socket
~~~
When the qube `unstrusted` has started (after a first reboot), you can directly access the service of `mytcp-service` running on port `444` as `localhost:444` .
2019-04-06 19:44:36 -04:00
2016-11-08 13:15:36 -05:00
Port forwarding to a qube from the outside world
2019-04-06 19:44:36 -04:00
------------------------------------------------
2013-01-01 07:42:59 -05:00
2019-08-18 14:10:51 -04:00
In order to allow a service present in a qube to be exposed to the outside world in the default setup (where the qube has sys-firewall as network VM, which in turn has sys-net as network VM) the following needs to be done:
2013-01-24 18:16:41 -05:00
2021-03-13 12:03:23 -05:00
- In the sys-net VM:
- Route packets from the outside world to the sys-firewall VM
- Allow packets through the sys-net VM firewall
- In the sys-firewall VM:
- Route packets from the sys-net VM to the VM
- Allow packets through the sys-firewall VM firewall
- In the qube:
- Allow packets through the qube firewall to reach the service
2013-01-24 18:16:41 -05:00
2019-08-18 14:10:51 -04:00
As an example we can take the use case of a web server listening on port 443 that we want to expose on our physical interface eth0, but only to our local network 192.168.x.0/24.
2013-01-24 18:16:41 -05:00
2018-02-28 11:06:31 -05:00
> Note: To have all interfaces available and configured, make sure the 3 qubes are up and running
2018-06-24 16:22:27 -04:00
> Note: [Issue #4028](https://github.com/QubesOS/qubes-issues/issues/4028) discusses adding a command to automate exposing the port.
2020-12-27 11:20:30 -05:00
**1. Identify the IP addresses you will need to use for sys-net, sys-firewall and the destination qube.**
2013-01-24 18:16:41 -05:00
2021-03-13 12:03:23 -05:00
You can get this information from the Settings Window for the qube, or by running this command in each qube:
`ifconfig | grep -i cast `
2020-12-27 11:20:30 -05:00
Note the IP addresses you will need.
> Note: The vifx.0 interface is the one used by qubes connected to this netvm so it is _not_ an outside world interface.
2013-01-24 18:16:41 -05:00
2020-12-27 11:20:30 -05:00
**2. Route packets from the outside world to the FirewallVM**
2013-01-24 18:16:41 -05:00
2020-12-27 11:20:30 -05:00
In the sys-net VM's Terminal, code a natting firewall rule to route traffic on the outside interface for the service to the sys-firewall VM
2013-01-24 18:16:41 -05:00
2021-03-13 12:03:23 -05:00
```
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -d 192.168.x.x -j DNAT --to-destination 10.137.1.x
```
2016-02-07 14:09:25 -05:00
2019-08-18 14:10:51 -04:00
Code the appropriate new filtering firewall rule to allow new connections for the service
2016-02-07 14:09:25 -05:00
2021-03-13 12:03:23 -05:00
```
iptables -I FORWARD 2 -i eth0 -d 10.137.1.x -p tcp --dport 443 -m conntrack --ctstate NEW -j ACCEPT
```
2013-01-24 18:16:41 -05:00
2021-03-13 12:03:23 -05:00
> If you want to expose the service on multiple interfaces, repeat the steps described in part 1 for each interface.
2020-12-27 11:20:30 -05:00
> In Qubes R4, at the moment ([QubesOS/qubes-issues#3644](https://github.com/QubesOS/qubes-issues/issues/3644)), nftables is also used which imply that additional rules need to be set in a `qubes-firewall` nft table with a forward chain.
2018-02-28 11:06:31 -05:00
`nft add rule ip qubes-firewall forward meta iifname eth0 ip daddr 10.137.0.x tcp dport 443 ct state new counter accept`
2013-01-24 18:16:41 -05:00
2019-08-18 14:10:51 -04:00
Verify you are cutting through the sys-net VM firewall by looking at its counters (column 2)
2013-01-24 18:16:41 -05:00
2021-03-13 12:03:23 -05:00
```
iptables -t nat -L -v -n
iptables -L -v -n
```
2013-01-24 18:16:41 -05:00
2018-02-28 11:06:31 -05:00
> Note: On Qubes R4, you can also check the nft counters
2021-03-13 12:03:23 -05:00
```
nft list table ip qubes-firewall
```
2018-02-28 11:06:31 -05:00
Send a test packet by trying to connect to the service from an external device
2013-01-24 18:16:41 -05:00
2021-03-13 12:03:23 -05:00
```
telnet 192.168.x.x 443
```
2013-01-24 18:16:41 -05:00
2019-08-18 14:10:51 -04:00
Once you have confirmed that the counters increase, store these command in `/rw/config/rc.local` so they get set on sys-net start-up
2013-01-24 18:16:41 -05:00
2021-03-13 12:03:23 -05:00
```
sudo nano /rw/config/rc.local
```
2013-01-24 18:16:41 -05:00
2015-09-26 19:00:33 -04:00
~~~
2013-01-24 18:16:41 -05:00
#!/bin/sh
2016-02-07 14:09:25 -05:00
####################
# My service routing
2013-01-24 18:16:41 -05:00
2016-02-07 14:09:25 -05:00
# Create a new firewall natting chain for my service
2019-09-02 07:07:09 -04:00
if iptables -w -t nat -N MY-HTTPS; then
2013-01-24 18:16:41 -05:00
2016-02-07 14:09:25 -05:00
# Add a natting rule if it did not exit (to avoid cluter if script executed multiple times)
2019-09-02 07:07:09 -04:00
iptables -w -t nat -A MY-HTTPS -j DNAT --to-destination 10.137.1.x
2013-01-24 18:16:41 -05:00
2016-02-07 14:09:25 -05:00
fi
2013-01-24 18:16:41 -05:00
2016-02-07 14:09:25 -05:00
# If no prerouting rule exist for my service
2019-09-02 07:07:09 -04:00
if ! iptables -w -t nat -n -L PREROUTING | grep --quiet MY-HTTPS; then
2013-01-24 18:16:41 -05:00
2016-02-07 14:09:25 -05:00
# add a natting rule for the traffic (same reason)
2019-09-02 07:07:09 -04:00
iptables -w -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -d 192.168.0.x -j MY-HTTPS
2016-02-07 14:09:25 -05:00
fi
2013-01-24 18:16:41 -05:00
2016-02-07 14:09:25 -05:00
######################
# My service filtering
2013-01-24 18:16:41 -05:00
2016-02-07 14:09:25 -05:00
# Create a new firewall filtering chain for my service
2019-09-02 07:07:09 -04:00
if iptables -w -N MY-HTTPS; then
2016-02-07 14:09:25 -05:00
# Add a filtering rule if it did not exit (to avoid cluter if script executed multiple times)
2019-09-02 07:07:09 -04:00
iptables -w -A MY-HTTPS -s 192.168.x.0/24 -j ACCEPT
2016-02-07 14:09:25 -05:00
fi
2018-02-28 11:06:31 -05:00
# If no forward rule exist for my service
2019-09-02 07:07:09 -04:00
if ! iptables -w -n -L FORWARD | grep --quiet MY-HTTPS; then
2016-02-07 14:09:25 -05:00
2018-02-28 11:06:31 -05:00
# add a forward rule for the traffic (same reason)
2019-09-02 07:07:09 -04:00
iptables -w -I FORWARD 2 -d 10.137.1.x -p tcp --dport 443 -m conntrack --ctstate NEW -j MY-HTTPS
2016-02-07 14:09:25 -05:00
fi
2015-09-26 19:00:33 -04:00
~~~
2013-01-24 18:16:41 -05:00
2018-02-28 11:06:31 -05:00
> Note: Again in R4 the following needs to be added:
~~~
#############
# In Qubes R4
# If not already present
if nft -nn list table ip qubes-firewall | grep "tcp dport 443 ct state new"; then
# Add a filtering rule
nft add rule ip qubes-firewall forward meta iifname eth0 ip daddr 10.137.0.x tcp dport 443 ct state new counter accept
fi
~~~
2020-12-27 11:20:30 -05:00
**3. Route packets from the FirewallVM to the VM**
2013-01-24 18:16:41 -05:00
2020-12-27 11:20:30 -05:00
In the sys-firewall VM's Terminal, code a natting firewall rule to route traffic on its outside interface for the service to the qube
2016-02-07 14:09:25 -05:00
2021-03-13 12:03:23 -05:00
```
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -d 10.137.1.x -j DNAT --to-destination 10.137.2.y
```
2016-02-07 14:09:25 -05:00
2019-08-18 14:10:51 -04:00
Code the appropriate new filtering firewall rule to allow new connections for the service
2016-02-07 14:09:25 -05:00
2021-03-13 12:03:23 -05:00
```
iptables -I FORWARD 2 -i eth0 -s 192.168.x.0/24 -d 10.137.2.y -p tcp --dport 443 -m conntrack --ctstate NEW -j ACCEPT
```
2016-02-07 14:09:25 -05:00
2020-12-27 11:20:30 -05:00
> Note: If you do not wish to limit the IP addresses connecting to the service, remove the ` -s 192.168.0.1/24 `
2016-02-07 14:09:25 -05:00
2018-02-28 11:06:31 -05:00
> Note: On Qubes R4
2021-03-13 12:03:23 -05:00
```
nft add rule ip qubes-firewall forward meta iifname eth0 ip saddr 192.168.x.0/24 ip daddr 10.137.0.y tcp dport 443 ct state new counter accept
```
2018-02-28 11:06:31 -05:00
Once you have confirmed that the counters increase, store these command in `/rw/config/qubes-firewall-user-script`
2021-03-13 12:03:23 -05:00
```
sudo nano /rw/config/qubes-firewall-user-script
```
2013-01-24 18:16:41 -05:00
2015-09-26 19:00:33 -04:00
~~~
2013-01-24 18:16:41 -05:00
#!/bin/sh
2016-02-07 14:09:25 -05:00
####################
# My service routing
# Create a new firewall natting chain for my service
2019-09-02 07:07:09 -04:00
if iptables -w -t nat -N MY-HTTPS; then
2016-02-07 14:09:25 -05:00
# Add a natting rule if it did not exit (to avoid cluter if script executed multiple times)
2019-09-02 07:07:09 -04:00
iptables -w -t nat -A MY-HTTPS -j DNAT --to-destination 10.137.2.y
2016-02-07 14:09:25 -05:00
fi
# If no prerouting rule exist for my service
2019-09-02 07:07:09 -04:00
if ! iptables -w -t nat -n -L PREROUTING | grep --quiet MY-HTTPS; then
2016-02-07 14:09:25 -05:00
# add a natting rule for the traffic (same reason)
2019-09-02 07:07:09 -04:00
iptables -w -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -d 10.137.1.x -j MY-HTTPS
2016-02-07 14:09:25 -05:00
fi
######################
# My service filtering
# Create a new firewall filtering chain for my service
2019-09-02 07:07:09 -04:00
if iptables -w -N MY-HTTPS; then
2016-02-07 14:09:25 -05:00
# Add a filtering rule if it did not exit (to avoid cluter if script executed multiple times)
2019-09-02 07:07:09 -04:00
iptables -w -A MY-HTTPS -s 192.168.x.0/24 -j ACCEPT
2016-02-07 14:09:25 -05:00
fi
2018-02-28 11:06:31 -05:00
# If no forward rule exist for my service
2019-09-02 07:07:09 -04:00
if ! iptables -w -n -L FORWARD | grep --quiet MY-HTTPS; then
2016-02-07 14:09:25 -05:00
2018-02-28 11:06:31 -05:00
# add a forward rule for the traffic (same reason)
2019-09-02 07:07:09 -04:00
iptables -w -I FORWARD 4 -d 10.137.2.y -p tcp --dport 443 -m conntrack --ctstate NEW -j MY-HTTPS
2018-02-28 11:06:31 -05:00
fi
################
# In Qubes OS R4
# If not already present
2018-06-03 22:33:14 -04:00
if ! nft -nn list table ip qubes-firewall | grep "tcp dport 443 ct state new"; then
2018-02-28 11:06:31 -05:00
# Add a filtering rule
nft add rule ip qubes-firewall forward meta iifname eth0 ip saddr 192.168.x.0/24 ip daddr 10.137.0.y tcp dport 443 ct state new counter accept
2016-02-07 14:09:25 -05:00
fi
2015-09-26 19:00:33 -04:00
~~~
2013-01-24 18:16:41 -05:00
2016-02-07 14:09:25 -05:00
Finally make this file executable (so it runs at every Firewall VM update)
2013-01-24 18:16:41 -05:00
2015-09-26 19:00:33 -04:00
~~~
2016-07-20 08:26:17 -04:00
sudo chmod +x /rw/config/qubes-firewall-user-script
2015-09-26 19:00:33 -04:00
~~~
2013-01-24 18:16:41 -05:00
2020-12-27 11:20:30 -05:00
**4. Allow packets into the qube to reach the service**
2013-01-24 18:16:41 -05:00
2021-03-13 12:03:23 -05:00
Here no routing is required, only filtering.
2019-08-18 14:12:54 -04:00
Proceed in the same way as above but store the filtering rule in the `/rw/config/rc.local` script.
2018-02-28 11:06:31 -05:00
2021-03-13 12:03:23 -05:00
```
sudo name /rw/config/rc.local
```
2013-01-24 18:16:41 -05:00
2015-09-26 19:00:33 -04:00
~~~
2016-02-07 14:09:25 -05:00
######################
# My service filtering
# Create a new firewall filtering chain for my service
2019-09-02 07:07:09 -04:00
if iptables -w -N MY-HTTPS; then
2016-02-07 14:09:25 -05:00
# Add a filtering rule if it did not exit (to avoid cluter if script executed multiple times)
2019-09-02 07:07:09 -04:00
iptables -w -A MY-HTTPS -j ACCEPT
2016-02-07 14:09:25 -05:00
fi
2019-09-02 07:01:22 -04:00
# If no input rule exists for my service
2019-09-02 07:07:09 -04:00
if ! iptables -w -n -L INPUT | grep --quiet MY-HTTPS; then
2016-02-07 14:09:25 -05:00
2018-02-28 11:06:31 -05:00
# add a forward rule for the traffic (same reason)
2019-09-02 07:07:09 -04:00
iptables -w -I INPUT 5 -d 10.137.2.x -p tcp --dport 443 -m conntrack --ctstate NEW -j MY-HTTPS
2013-02-10 08:15:49 -05:00
2016-02-07 14:09:25 -05:00
fi
2015-09-26 19:00:33 -04:00
~~~
2013-01-24 18:16:41 -05:00
2019-08-18 14:10:51 -04:00
This time testing should allow connectivity to the service as long as the service is up :-)
2016-07-19 08:09:06 -04:00
2019-04-06 19:44:36 -04:00
Where to put firewall rules
2018-02-02 08:06:10 -05:00
---------------------------
2020-12-27 11:20:30 -05:00
Implicit in the above example [scripts ](/doc/config-files/ ), but worth calling attention to: for all qubes *except* those supplying networking, iptables commands should be added to the `/rw/config/rc.local` script.
2019-08-18 14:12:54 -04:00
For AppVMs supplying networking (`sys-firewall` inclusive), iptables commands should be added to `/rw/config/qubes-firewall-user-script` .
2018-02-02 08:06:10 -05:00
2019-08-29 14:51:12 -04:00
Firewall troubleshooting
------------------------
Firewall logs are stored in the systemd journal of the qube the firewall is running in (probably `sys-firewall` ).
You can view them by running `sudo journalctl -u qubes-firewall.service` in the relevant qube.
Sometimes these logs can contain useful information about errors that are preventing the firewall from behaving as you would expect.