User Tools

Site Tools


doc:uci:firewall

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
doc:uci:firewall [2013/05/19 11:59]
orca
doc:uci:firewall [2015/07/08 10:46] (current)
fillarios State that FORWARD rule for a zone controls traffic between different interfaces in the same zone
Line 1: Line 1:
 ====== Firewall configuration ====== ====== Firewall configuration ======
 The firewall configuration located in **''/​etc/​config/​firewall''​**. The firewall configuration located in **''/​etc/​config/​firewall''​**.
- 
-**''​Note No1''​**:​ Maybe not all newest changes [[https://​dev.openwrt.org/​changeset/​35484/​trunk|trunk r35484]] are documented.\\ 
-**''​Note No2''​**:​ Precompiled OpenWrt Images contain the OpenWrt-Firewall. The OpenWrt-Firewall already contains a couple of rules. For some obscure reasons, this pre-defined rules are not contained in the file **''/​etc/​config/​firewall''​** but are hard-coded in other files. http://​nbd.name/​gitweb.cgi?​p=firewall3.git;​a=summary Since the netfilter system is a chained processing filter, you need to know this pre-existing rules in order to successfully add own rules. 
  
 ===== Overview ===== ===== Overview =====
-UCI Firewall provides a configuration interface that abstracts from the **iptables** system to provide a simplified configuration model that is fit for most regular purposes while enabling the user to supply needed iptables rules on his own when needed.+OpenWrt relies on [[doc:​howto:​netfilter]] for packet filtering, NAT and mangling. The UCI Firewall provides a configuration interface that abstracts from the **iptables** system to provide a simplified configuration model that is fit for most regular purposes while enabling the user to supply needed iptables rules on his own when needed.
  
-UCI Firewall maps two or more //​Interfaces//​ together into //Zones// that are used to describe default rules for a given interface, forwarding rules between interfaces, and extra rules that are not covered by the first two. In the config file, default rules come //first// but they are the last to take effect. The iptables ([[doc:​howto:​netfilter]]) system is a chained processing filter where packets pass through various rules. The first rule that matches is executed, often leading to another rule-chain until a packet hits either ACCEPT or DROP/​REJECT. Such an outcome is final, therefore the default rules take effect last, and the most specific rule takes effect first. Zones are also used to configure //​masquerading//​ also known as NAT (network-address-translation) as well as port forwarding rules, which are more generally known as redirects.+UCI Firewall maps two or more //​Interfaces//​ together into //Zones// that are used to describe default rules for a given interface, forwarding rules between interfaces, and extra rules that are not covered by the first two. In the config file, default rules come //first// but they are the last to take effect. The netfilter system is a chained processing filter where packets pass through various rules. The first rule that matches is executed, often leading to another rule-chain until a packet hits either ACCEPT or DROP/​REJECT. Such an outcome is final, therefore the default rules take effect last, and the most specific rule takes effect first. Zones are also used to configure //​masquerading//​ also known as NAT (network-address-translation) as well as port forwarding rules, which are more generally known as redirects.
  
 Zones must always be mapped onto one or more Interfaces which ultimately map onto physical devices; therefore zones cannot be used to specify networks (subnets), and the generated iptables rules operate on interfaces exclusively. The difference is that interfaces can be used to reach destinations not part of their own subnet, when their subnet contains another gateway. Usually however, forwarding is done between lan and wan interfaces, with the router serving as '​edge'​ gateway to the internet. The default configuration of UCI Firewall provides for such a common setup. Zones must always be mapped onto one or more Interfaces which ultimately map onto physical devices; therefore zones cannot be used to specify networks (subnets), and the generated iptables rules operate on interfaces exclusively. The difference is that interfaces can be used to reach destinations not part of their own subnet, when their subnet contains another gateway. Usually however, forwarding is done between lan and wan interfaces, with the router serving as '​edge'​ gateway to the internet. The default configuration of UCI Firewall provides for such a common setup.
  
 ===== Requirements ===== ===== Requirements =====
-  * **''​firewall''​** and its dependencies (//​pre-installed//​)+  * **''​firewall''​** ​(or  **''​firewall3''​**) ​and its dependencies (//​pre-installed//​)
     * **''​iptables''​** (//​pre-installed//​)     * **''​iptables''​** (//​pre-installed//​)
-    ​* **''​iptables-mod-conntrack''​** (//​pre-installed//​) +    * **''​iptables-mod-?''​** (//​optional//​),​ see [[doc:​howto:​netfilter#OPKG Netfilter Packages]].
-    * **''​iptables-mod-nat''​** (//​pre-installed//​) +
-    ​* **''​iptables-mod-?''​** (//​optional//​),​ see [[doc:​howto:​netfilter]]+
- +
-It is not required to use UCI Firewall, but merely exists to make life easier for you. The WebUI ([[doc:​howto:​LuCI]]) may also be used to configure the UCI firewall. The firewall package may be found here: ''​[[https://​dev.openwrt.org/​browser/​trunk/​package/​network/​config/​firewall|firewall]]''​.+
  
 ===== Sections ===== ===== Sections =====
Line 51: Line 44:
 A ''​zone''​ section groups one or more //​interfaces//​ and serves as a //source// or //​destination//​ for //​forwardings//,​ //rules// and //​redirects//​. Masquerading (NAT) of outgoing traffic is controlled on a per-zone basis. Note that masquerading is defined on the //​outgoing//​ interface. A ''​zone''​ section groups one or more //​interfaces//​ and serves as a //source// or //​destination//​ for //​forwardings//,​ //rules// and //​redirects//​. Masquerading (NAT) of outgoing traffic is controlled on a per-zone basis. Note that masquerading is defined on the //​outgoing//​ interface.
  
-  * INPUT rules for a zone describe what happens to traffic trying to reach the router itself through ​that interface. +  * INPUT rules for a zone describe what happens to traffic trying to reach the router itself through ​an interface ​in that zone
-  * OUTPUT rules for a zone describe what happens to traffic originating from the router itself. +  * OUTPUT rules for a zone describe what happens to traffic originating from the router itself ​going through an interface in that zone
-  * FORWARD rules for a zone describe what happens to traffic ​coming from that zone and passing to another ​zone.+  * FORWARD rules for a zone describe what happens to traffic ​passing between different interfaces in that zone.
  
 The options below are defined within ''​zone''​ sections: The options below are defined within ''​zone''​ sections:
Line 59: Line 52:
 ^ Name ^ Type ^ Required ^ Default ^ Description ^ ^ Name ^ Type ^ Required ^ Default ^ Description ^
 | ''​name''​ | zone name | yes | //(none)// | Unique zone name | | ''​name''​ | zone name | yes | //(none)// | Unique zone name |
-| ''​network''​ | list | no | //(none)// | List of //​[[doc:​uci:​network#​interfaces|interfaces]]//​ attached to this zone. If omitted and neither extra* options, subnets or devices are given, the value of ''​name''​ is used by default |+| ''​network''​ | list | no | //(none)// | List of //​[[doc:​uci:​network#​interfaces|interfaces]]//​ attached to this zone. If omitted and neither extra* options, subnets or devices are given, the value of ''​name''​ is used by default. Use list syntax as explained in [[doc:​uci]]. ​|
 | ''​masq''​ | boolean | no | ''​0''​ | Specifies whether //​outgoing//​ zone traffic should be masqueraded - this is typically enabled on the //wan// zone | | ''​masq''​ | boolean | no | ''​0''​ | Specifies whether //​outgoing//​ zone traffic should be masqueraded - this is typically enabled on the //wan// zone |
 | ''​masq_src''​ | list of subnets | no | ''​0.0.0.0/​0''​ | Limit masquerading to the given source subnets. Negation is possible by prefixing the subnet with ''​!'';​ multiple subnets are allowed. | | ''​masq_src''​ | list of subnets | no | ''​0.0.0.0/​0''​ | Limit masquerading to the given source subnets. Negation is possible by prefixing the subnet with ''​!'';​ multiple subnets are allowed. |
 | ''​masq_dest''​ | list of subnets | no | ''​0.0.0.0/​0''​ | Limit masquerading to the given destination subnets. Negation is possible by prefixing the subnet with ''​!'';​ multiple subnets are allowed. | | ''​masq_dest''​ | list of subnets | no | ''​0.0.0.0/​0''​ | Limit masquerading to the given destination subnets. Negation is possible by prefixing the subnet with ''​!'';​ multiple subnets are allowed. |
-| ''​conntrack''​ | boolean | no | ''​1''​ if masquerading is used, ''​0''​ otherwise | Force connection tracking for this zone (see [[#note.on.connection.tracking.notrack|Note on connection tracking]]) |+| ''​conntrack''​ | boolean | no | ''​1''​ if masquerading is used, ''​0''​ otherwise | Force connection tracking for this zone (see [[#notes_on_connection_tracking|Note on connection tracking]]) |
 | ''​mtu_fix''​ | boolean | no | ''​0''​ | Enable MSS clamping for //​outgoing//​ zone traffic | | ''​mtu_fix''​ | boolean | no | ''​0''​ | Enable MSS clamping for //​outgoing//​ zone traffic |
 | ''​input''​ | string | no | ''​DROP''​ | Default policy (''​ACCEPT'',​ ''​REJECT'',​ ''​DROP''​) for //​incoming//​ zone traffic | | ''​input''​ | string | no | ''​DROP''​ | Default policy (''​ACCEPT'',​ ''​REJECT'',​ ''​DROP''​) for //​incoming//​ zone traffic |
Line 71: Line 64:
 | ''​log''​ | boolean | no | ''​0''​ | Create log rules for rejected and dropped traffic in this zone. | | ''​log''​ | boolean | no | ''​0''​ | Create log rules for rejected and dropped traffic in this zone. |
 | ''​log_limit''​ | string | no | ''​10/​minute''​ | Limits the amount of log messages per interval. | | ''​log_limit''​ | string | no | ''​10/​minute''​ | Limits the amount of log messages per interval. |
-| ''​device''​ | list | no | //(none)// | List of raw network device names attached to this zone, e.g. ''​ppp+''​ to match any PPP interface. \\ :!: Only supported by the Firewall v2, version 58 and above | +| ''​device''​ | list | no | //(none)// | List of raw network device names attached to this zone, e.g. ''​ppp+''​ to match any PPP interface. \\ :!: Only supported by the Firewall v2, version 58 and above ; not supported by 12.09 default installation ​
-| ''​subnet''​ | list | no | //(none)// | List of IP subnets attached to this zone. \\ :!: Only supported by the Firewall v2, version 58 and above | +| ''​subnet''​ | list | no | //(none)// | List of IP subnets attached to this zone. \\ :!: Only supported by the Firewall v2, version 58 and above, not supported by 12.09 default installation ​
-| ''​extra''​ | string | no | //(none)// | Extra arguments passed directly to iptables. Note that these options are passed to both source and destination classification rules, therfore direction-specific options like ''​--dport''​ should not be used here - in this case the ''​extra_src''​ and ''​extra_dest''​ options should be used instead. \\ :!: Only supported by the Firewall v2, version 58 and above | +| ''​extra''​ | string | no | //(none)// | Extra arguments passed directly to iptables. Note that these options are passed to both source and destination classification rules, therfore direction-specific options like ''​--dport''​ should not be used here - in this case the ''​extra_src''​ and ''​extra_dest''​ options should be used instead. \\ :!: Only supported by the Firewall v2, version 58 and above, not supported by 12.09 default installation ​
-| ''​extra_src''​ | string | no | //Value of ''​extra''//​ | Extra arguments passed directly to iptables for source classification rules. \\ :!: Only supported by the Firewall v2, version 58 and above | +| ''​extra_src''​ | string | no | //Value of ''​extra''//​ | Extra arguments passed directly to iptables for source classification rules. \\ :!: Only supported by the Firewall v2, version 58 and above, not supported by 12.09 default installation ​
-| ''​extra_dest''​ | string | no | //Value of ''​extra''//​ | Extra arguments passed directly to iptables for destination classification rules. \\ :!: Only supported by the Firewall v2, version 58 and above |+| ''​extra_dest''​ | string | no | //Value of ''​extra''//​ | Extra arguments passed directly to iptables for destination classification rules. \\ :!: Only supported by the Firewall v2, version 58 and above, not supported by 12.09 default installation ​|
  
 ==== Forwardings ==== ==== Forwardings ====
Line 97: Line 90:
  
 //Redirects are also commonly known as "port forwarding",​ and "​virtual servers"​.//​ //Redirects are also commonly known as "port forwarding",​ and "​virtual servers"​.//​
 +
 +Port ranges are specified as ''​start:​stop'',​ for instance ''​6666:​6670''​. ​ This is similar to the iptables syntax.
  
 The options below are valid for //​redirects//:​ The options below are valid for //​redirects//:​
Line 108: Line 103:
 | ''​src_dport''​ | port or range | no | //(none)// | For //DNAT//, match incoming traffic directed at the given //​destination port or port range// on this host. For //SNAT// rewrite the //source ports// to the given value. | | ''​src_dport''​ | port or range | no | //(none)// | For //DNAT//, match incoming traffic directed at the given //​destination port or port range// on this host. For //SNAT// rewrite the //source ports// to the given value. |
 | ''​proto''​ | protocol name or number | yes | //tcpudp// | Match incoming traffic using the given //​protocol//​ | | ''​proto''​ | protocol name or number | yes | //tcpudp// | Match incoming traffic using the given //​protocol//​ |
-| ''​dest''​ | zone name | yes for ''​SNAT''​ target | //(none)// | Specifies the traffic //​destination zone//, must refer to one of the defined //zone names//. |+| ''​dest''​ | zone name | yes for ''​SNAT''​ target | //(none)// | Specifies the traffic //​destination zone//. Must refer to one of the defined //zone names//. For ''​DNAT''​ target on Attitude Adjustment, NAT reflection works only if this is equal to ''​lan''​. |
 | ''​dest_ip''​ | ip address | yes for ''​DNAT''​ target | //(none)// | For //DNAT//, redirect matched incoming traffic to the specified internal host. For //SNAT//, match traffic directed at the given address. | | ''​dest_ip''​ | ip address | yes for ''​DNAT''​ target | //(none)// | For //DNAT//, redirect matched incoming traffic to the specified internal host. For //SNAT//, match traffic directed at the given address. |
 | ''​dest_port''​ | port or range | no | //(none)// | For //DNAT//, redirect matched incoming traffic to the given port on the internal host. For //SNAT//, match traffic directed at the given ports. | | ''​dest_port''​ | port or range | no | //(none)// | For //DNAT//, redirect matched incoming traffic to the given port on the internal host. For //SNAT//, match traffic directed at the given ports. |
 | ''​ipset''​ | string | no | //(none)// | If specified, match traffic against the given //​[[#​ip.sets|ipset]]//​. The match can be inverted by prefixing the value with an exclamation mark | | ''​ipset''​ | string | no | //(none)// | If specified, match traffic against the given //​[[#​ip.sets|ipset]]//​. The match can be inverted by prefixing the value with an exclamation mark |
 | ''​mark''​ | string | no | //(none)// | If specified, match traffic against the given firewall mark, e.g. ''​0xFF''​ to match mark 255 or ''​0x0/​0x1''​ to match any even mark value. The match can be inverted by prefixing the value with an exclamation mark, e.g. ''​!0x10''​ to match all but mark #16. | | ''​mark''​ | string | no | //(none)// | If specified, match traffic against the given firewall mark, e.g. ''​0xFF''​ to match mark 255 or ''​0x0/​0x1''​ to match any even mark value. The match can be inverted by prefixing the value with an exclamation mark, e.g. ''​!0x10''​ to match all but mark #16. |
-| ''​start_date''​ | date (''​yyyy-mm-dddd''​) | no | //​(always)//​ | If specifed, only match traffic after the given date (inclusive). | +| ''​start_date''​ | date (''​yyyy-mm-dd''​) | no | //​(always)//​ | If specifed, only match traffic after the given date (inclusive). | 
-| ''​stop_date''​ | date (''​yyyy-mm-dddd''​) | no | //​(always)//​ | If specified, only match traffic before the given date (inclusive). |+| ''​stop_date''​ | date (''​yyyy-mm-dd''​) | no | //​(always)//​ | If specified, only match traffic before the given date (inclusive). |
 | ''​start_time''​ | time (''​hh:​mm:​ss''​) | no | //​(always)//​ | If specified, only match traffic after the given time of day (inclusive). | | ''​start_time''​ | time (''​hh:​mm:​ss''​) | no | //​(always)//​ | If specified, only match traffic after the given time of day (inclusive). |
 | ''​stop_time''​ | time (''​hh:​mm:​ss''​) | no | //​(always)//​ | If specified, only match traffic before the given time of day (inclusive). | | ''​stop_time''​ | time (''​hh:​mm:​ss''​) | no | //​(always)//​ | If specified, only match traffic before the given time of day (inclusive). |
Line 122: Line 117:
 | ''​target''​ | string | no | ''​DNAT''​ | NAT target (''​DNAT''​ or ''​SNAT''​) to use when generating the rule | | ''​target''​ | string | no | ''​DNAT''​ | NAT target (''​DNAT''​ or ''​SNAT''​) to use when generating the rule |
 | ''​family''​ | string | no | ''​any''​ | Protocol family (''​ipv4'',​ ''​ipv6''​ or ''​any''​) to generate iptables rules for. | | ''​family''​ | string | no | ''​any''​ | Protocol family (''​ipv4'',​ ''​ipv6''​ or ''​any''​) to generate iptables rules for. |
-| ''​reflection''​ | boolean | no | ''​1''​ | Disables ​NAT reflection for this redirect ​if set to ''​0'' ​- applicable to ''​DNAT''​ targets. |+| ''​reflection''​ | boolean | no | ''​1''​ | Activate ​NAT reflection for this redirect - applicable to ''​DNAT''​ targets. |
 | ''​reflection_src''​ | string | no | ''​internal''​ | The source address to use for NAT-reflected packets if ''​reflection''​ is ''​1''​. This can be ''​internal''​ or ''​external'',​ specifying which interface’s address to use. Applicable to ''​DNAT''​ targets. | | ''​reflection_src''​ | string | no | ''​internal''​ | The source address to use for NAT-reflected packets if ''​reflection''​ is ''​1''​. This can be ''​internal''​ or ''​external'',​ specifying which interface’s address to use. Applicable to ''​DNAT''​ targets. |
-| ''​limit''​ | string | no | //(none)// | Maximum average matching rate; specified as a number, with an optional ''/​second'',​ ''/​minute'',​ ''/​hour''​ or ''/​day''​ suffix. ​Example: ''​3/​hour''​. |+| ''​limit''​ | string | no | //(none)// | Maximum average matching rate; specified as a number, with an optional ''/​second'',​ ''/​minute'',​ ''/​hour''​ or ''/​day''​ suffix. ​Examples: ''​3/​second'',​ ''​3/​sec''​ or ''​3/​s''​. |
 | ''​limit_burst''​ | integer | no | ''​5''​ | Maximum initial number of packets to match, allowing a short-term average above ''​limit''​ | | ''​limit_burst''​ | integer | no | ''​5''​ | Maximum initial number of packets to match, allowing a short-term average above ''​limit''​ |
 | ''​extra''​ | string | no | //(none)// | Extra arguments to pass to iptables. Useful mainly to specify additional match options, such as ''​-m policy %%--%%dir in''​ for IPsec. | | ''​extra''​ | string | no | //(none)// | Extra arguments to pass to iptables. Useful mainly to specify additional match options, such as ''​-m policy %%--%%dir in''​ for IPsec. |
 +| ''​enabled''​ | string | no | ''​1''​ or ''​yes''​ | Enable the redirect rule or not. |
 +
 +:!: On Attitude Adjustment, for NAT reflection to work, you **must** specify ''​option dest lan''​ in the ''​redirect''​ section (even though we're using a ''​DNAT''​ target).
  
 ==== Rules ==== ==== Rules ====
Line 139: Line 137:
   * If only ''​dest''​ is given, the rule matches //​outgoing//​ traffic   * If only ''​dest''​ is given, the rule matches //​outgoing//​ traffic
   * If neither ''​src''​ nor ''​dest''​ are given, the rule defaults to an //​outgoing//​ traffic rule   * If neither ''​src''​ nor ''​dest''​ are given, the rule defaults to an //​outgoing//​ traffic rule
 +
 +Port ranges are specified as ''​start:​stop'',​ for instance ''​6666:​6670''​. ​ This is similar to the iptables syntax.
  
 Valid options for this section are: Valid options for this section are:
Line 153: Line 153:
 | ''​ipset''​ | string | no | //(none)// | If specified, match traffic against the given //​[[#​ip.sets|ipset]]//​. The match can be inverted by prefixing the value with an exclamation mark | | ''​ipset''​ | string | no | //(none)// | If specified, match traffic against the given //​[[#​ip.sets|ipset]]//​. The match can be inverted by prefixing the value with an exclamation mark |
 | ''​mark''​ | mark/mask | no | //(none)// | If specified, match traffic against the given firewall mark, e.g. ''​0xFF''​ to match mark 255 or ''​0x0/​0x1''​ to match any even mark value. The match can be inverted by prefixing the value with an exclamation mark, e.g. ''​!0x10''​ to match all but mark #16. | | ''​mark''​ | mark/mask | no | //(none)// | If specified, match traffic against the given firewall mark, e.g. ''​0xFF''​ to match mark 255 or ''​0x0/​0x1''​ to match any even mark value. The match can be inverted by prefixing the value with an exclamation mark, e.g. ''​!0x10''​ to match all but mark #16. |
-| ''​start_date''​ | date (''​yyyy-mm-dddd''​) | no | //​(always)//​ | If specifed, only match traffic after the given date (inclusive). | +| ''​start_date''​ | date (''​yyyy-mm-dd''​) | no | //​(always)//​ | If specifed, only match traffic after the given date (inclusive). | 
-| ''​stop_date''​ | date (''​yyyy-mm-dddd''​) | no | //​(always)//​ | If specified, only match traffic before the given date (inclusive). |+| ''​stop_date''​ | date (''​yyyy-mm-dd''​) | no | //​(always)//​ | If specified, only match traffic before the given date (inclusive). |
 | ''​start_time''​ | time (''​hh:​mm:​ss''​) | no | //​(always)//​ | If specified, only match traffic after the given time of day (inclusive). | | ''​start_time''​ | time (''​hh:​mm:​ss''​) | no | //​(always)//​ | If specified, only match traffic after the given time of day (inclusive). |
 | ''​stop_time''​ | time (''​hh:​mm:​ss''​) | no | //​(always)//​ | If specified, only match traffic before the given time of day (inclusive). | | ''​stop_time''​ | time (''​hh:​mm:​ss''​) | no | //​(always)//​ | If specified, only match traffic before the given time of day (inclusive). |
Line 164: Line 164:
 | ''​set_xmark''​ | ::: | ::: | ::: | Zeroes out the bits given by mask and XORs value into the packet mark. If mask is omitted, 0xFFFFFFFF is assumed | | ''​set_xmark''​ | ::: | ::: | ::: | Zeroes out the bits given by mask and XORs value into the packet mark. If mask is omitted, 0xFFFFFFFF is assumed |
 | ''​family''​ | string | no | ''​any''​ | Protocol family (''​ipv4'',​ ''​ipv6''​ or ''​any''​) to generate iptables rules for. | | ''​family''​ | string | no | ''​any''​ | Protocol family (''​ipv4'',​ ''​ipv6''​ or ''​any''​) to generate iptables rules for. |
-| ''​limit''​ | string | no | //(none)// | Maximum average matching rate; specified as a number, with an optional ''/​second'',​ ''/​minute'',​ ''/​hour''​ or ''/​day''​ suffix. ​Example: ''​3/​hour''​. |+| ''​limit''​ | string | no | //(none)// | Maximum average matching rate; specified as a number, with an optional ''/​second'',​ ''/​minute'',​ ''/​hour''​ or ''/​day''​ suffix. ​Examples: ''​3/​minute'',​ ''​3/​min''​ or ''​3/​m''​. |
 | ''​limit_burst''​ | integer | no | ''​5''​ | Maximum initial number of packets to match, allowing a short-term average above ''​limit''​ | | ''​limit_burst''​ | integer | no | ''​5''​ | Maximum initial number of packets to match, allowing a short-term average above ''​limit''​ |
 | ''​extra''​ | string | no | //(none)// | Extra arguments to pass to iptables. Useful mainly to specify additional match options, such as ''​-m policy %%--%%dir in''​ for IPsec. | | ''​extra''​ | string | no | //(none)// | Extra arguments to pass to iptables. Useful mainly to specify additional match options, such as ''​-m policy %%--%%dir in''​ for IPsec. |
 +| ''​enabled''​ | boolean | no | yes | Enable or disable rule. |
  
 ==== Includes ==== ==== Includes ====
Line 266: Line 267:
  
 This example enables machines on the internet to use SSH to access your router. This example enables machines on the internet to use SSH to access your router.
-==== Forwarding ports (Destination NAT/DNAT) ====+==== Port forwarding for IPv4 (Destination NAT/DNAT) ====
  
 This example forwards http (but not HTTPS) traffic to the webserver running on 192.168.1.10:​ This example forwards http (but not HTTPS) traffic to the webserver running on 192.168.1.10:​
Line 275: Line 276:
         option src_dport 80         option src_dport 80
         option proto     tcp         option proto     tcp
 +        option dest      lan
         option dest_ip ​  ​192.168.1.10         option dest_ip ​  ​192.168.1.10
 </​code>​ </​code>​
Line 281: Line 283:
  
 <​code>​ <​code>​
-config ​'redirect' +config redirect 
-        option ​'​name'​ '​ssh'​ +        option ​src       wan 
-        option ​'​src'​ '​wan'​ +        option ​src_dport 5555 
-        option ​'proto' '​tcpudp'​ +        option proto     tcp 
-        option ​'​src_dport'​ '​5555'​ +        option ​dest      lan 
-        option ​'dest_ip' '192.168.1.100' +        option dest_ip ​  ​192.168.1.100 
-        option ​'dest_port' '22+        option dest_port 22
-        option '​target'​ '​DNAT'​ +
-        option '​dest'​ '​lan'​+
 </​code>​ </​code>​
 +
 +==== DNAT/SNAT redirects and forwarding combination ====
 +Given a couple of redirect (DNAT and SNAT, like to redirect
 +the traffic from an host to and from a specific ip address) such as:
 +<​file>​
 +config redirect
 +        option name     '​icmp DNAT'
 +        option src      '​wan'​
 +        option src_dip ​  '​1.2.3.4'​
 +        option proto    '​icmp'​
 +        option dest     '​dmz'​
 +        option dest_ip ​ '​192.168.1.79'​
 +        option target ​  '​DNAT'​
 +
 +config redirect
 +        option name     '​icmp SNAT'
 +        option src      '​dmz'​
 +        option src_ip ​  '​192.168.1.79'​
 +        option src_dip ​ '​1.2.3.4'​
 +        option proto    '​icmp'​
 +        option dest     '​wan'​
 +        option target ​  '​SNAT'​
 +</​file>​
 +
 +Someone could ask "//Ok, the packet source or destination is changed,
 +but still has to be forwarded towards the right network interface to reach the
 +endpoint//"​. So the administrator of openwrt could wonder of adding
 +additional forwarding rules but no, it is not needed. The forwarding
 +rules are added by the firewall appliance itself.
 +
 +The same applies to the masquerading,​ the rules are applied //before//
 +the global masquerading (if a masquerading is set), therefore they will
 +not be overridden (at least the SNAT) by the masquerading mechanism.
 +==== Masquerading on lan ====
 +Suppose that you have two routers, connected each other through the 
 +lan zone (both have static ip and dhcp disabled), ​
 +and only one of them is connected to the internet through the wan zone. 
 +In other words the situation is:
 +<​code>​
 +internet <​---->​ wan (172.22.13.228) | router 1 | lan (192.168.1.254) <​---->​ lan (192.168.1.1) | router 2 | wan (no connection)
 +</​code>​
 +
 +If both routers have the default openwrt configuration ​
 +(with the exceptions mentioned above), then a device on the lan side of the
 +router 1 can communicate through the internet if it has the router 1 as
 +gateway, this because the packet flow between devices is managed by routing.
 +In our case the router 2 has no proper setup in terms of gateway,
 +as the default openwrt configuration expects that a wan connection
 +on the router 2 is provided.
 +
 +Anyway suppose that on the router 1 we have the following rule:
 +<​code>​
 +config redirect
 +        option target '​DNAT'​
 +        option src '​wan'​
 +        option dest '​lan'​
 +        option proto '​tcp'​
 +        option src_dip '​172.22.13.228'​
 +        option src_dport '​2023'​
 +        option dest_ip '​192.168.1.1'​
 +        option dest_port '​23'​
 +        option name '​Telnet to new Router' ​
 +</​code>​
 +This rule is redirecting the tcp packets on the port 2023 with destination the wan ip of the router 1 
 +(172.22.13.228) towards the lan ip of the router 2. 
 +The router 2 cannot reply to those packets because we didn't adjust its routing table,
 +that is we didn't specify that the gateway to reply to "​wan"​ sources is the router 1.
 +Indeed those redirected packets will have an source ip external from the (default) "​lan"​ zone 192.168.1.0/​24.
 +
 +We can solve this activating the masquerading on the "​lan"​ zone on the router 1, in this way.
 +<​code>​
 +config zone
 +        option name '​lan'​
 +        option network '​lan'​
 +        option input '​ACCEPT'​
 +        option output '​ACCEPT'​
 +        option forward '​REJECT'​
 +        option masq '​1'​
 +</​code>​
 +This setup will provide the following effect (that is the effect intended by the masquerading):​ if a packet, belonging to a certain [[wp>​Virtual_circuit|connection]],​ is coming into the lan zone with a source ip belonging to another zone, keep track of the connection, taking note of the source ip of that connection, and modify the source ip with the ip of the router in the lan zone (that is: source_ip from a.b.c.d to 192.168.1.254). \\ 
 +Then deliver the packet to the intended destination (that is, 192.168.1.1,​ the router2). Afterwards, if a packet from 192.168.1.1 is coming back towards 192.168.1.254,​ belonging to the connection tracked before, changed back the destination ip (here is the second effect of the masquerading) with the source ip memorized before (that is, dest_ip from 192.168.1.254 to a.b.c.d ). In this way, for the point of view of the router 2, the router 2 just communicate with a device with an ip belonging to its "​lan"​ zone , and therefore the default routing is working without problem.
 +
 +At least one side effect of this setup is that every device in the lan zone of the router 1 cannot see any "​wan"​ ip, and this could be not wanted for several reasons (one of which: if you setup a proper gateway, there is no need for this masquerading). But this was just a "​special case" to expose in brief how the masquerading works and how it could be applied to zones that usually don't use it. An improvement of "​masquerading only for a specific device in the zone" could be the following:
 +<​code>​
 +config zone
 +        option name '​lan'​
 +        option network '​lan'​
 +        option input '​ACCEPT'​
 +        option output '​ACCEPT'​
 +        option forward '​REJECT'​
 +        option masq '​1'​
 +        option masq_dest '​192.168.1.1/​32'​
 +</​code>​
 +This provide the masquerading feature only if the packets are send towards the destination 192.168.1.1/​32 (this subnet should belong to the lan zone).
 +==== Port accept for IPv6 ====
 +
 +To open port 80 so that a local webserver at ''​2001:​db8:​42::​1337''​ can be reached from the Internet:
 +
 +<​code>​
 +config rule
 +        option src       wan
 +        option proto     tcp
 +        option dest      lan
 +        option dest_ip ​  ​2001:​db8:​42::​1337
 +        option dest_port 80
 +        option family ​   ipv6
 +        option target ​   ACCEPT
 +</​code>​
 +
 +To open SSH access to all IPv6 hosts in the local network:
 +
 +<​code>​
 +config rule
 +        option src       wan
 +        option proto     tcp
 +        option dest      lan
 +        option dest_port 22
 +        option family ​   ipv6
 +        option target ​   ACCEPT
 +</​code>​
 +
 +To open all TCP/UDP port between 1024 and 65535 towards the local IPv6 network:
 +
 +<​code>​
 +config rule
 +        option src       wan
 +        option proto     ​tcpudp
 +        option dest      lan
 +        option dest_port 1024:65535
 +        option family ​   ipv6
 +        option target ​   ACCEPT
 +</​code>​
 +
 ==== Source NAT (SNAT) ==== ==== Source NAT (SNAT) ====
  
Line 350: Line 483:
 ==== Block access to the Internet for specific IP on certain times  ==== ==== Block access to the Internet for specific IP on certain times  ====
  
-The following rule blocks all connection attempts to the internet from 192.168.1.27 on weekdays between 21:00pm and 09:00am.\\+The following rule blocks all connection attempts to the internet from 192.168.1.27 on weekdays between 21:00pm and 09:​00am ​(times are specified in UTC unless the --kerneltz switch is used).\\
 :!: The package ''​iptables-mod-ipopt''​ must be installed to provide ''​xt_time''​. :!: The package ''​iptables-mod-ipopt''​ must be installed to provide ''​xt_time''​.
  
Line 473: Line 606:
 </​code>​ </​code>​
  
 +==== Zone declaration for semi non-UCI interfaces, manually listed in the network config, and forwardings ====
 +Scenario: having one or more vpn tunnels using openvpn,
 +with the need of defining a zone to forward the traffic between the
 +vpn interfaces and the lan.
 +
 +First list the interfaces in **/​etc/​config/​network**, ​
 +for example in the following way: (be careful on the limits of interface naming in terms of name length, [[doc/​uci/​network|read more]])
 +<​code>​
 +config interface '​tun0'​
 + option ifname '​tun0'​
 + option proto '​none'​
 +
 +config interface '​tun1'​
 +        option ifname '​tun1'​
 +        option proto '​none'​
 +</​code>​
 +
 +Then create the zone in **/​etc/​config/​firewall**,​ for example one zone for all the vpn interfaces.
 +<​code>​
 +config zone
 +        option name             ​vpn_tunnel
 +        list   ​network ​         '​tun0'​
 + list   ​network ​         '​tun1'​
 +        option input            ACCEPT
 +          #the traffic towards the router from the interface will be accepted
 +          #(as for the lan communications)
 +        option output ​          ​ACCEPT
 +          #the traffic from the router to the interface will be accepted
 +        option forward ​         REJECT
 +          #traffic from this zone to other zones is normally rejected
 +</​code>​
 +
 +Then we want to communicate with the "​lan"​ zone, therefore we need forwardings in both ways
 +(from lan to wan and viceversa)
 +<​code>​
 +config forwarding
 +        option src              lan
 +        option dest             ​vpn_tunnel
 +        #if a packet from lan wants to go to the vpn_tunnel zone
 +        #let it pass
 +
 +config forwarding
 +        option src              vpn_tunnel
 +        option dest             lan
 +        #if a packet from vpn_tunnel wants to go to the lan zone
 +        #let it pass
 +</​code>​
 +This will create a lot of "​automatic"​ iptables rules (because automatic scripting is not
 +as efficient as raw iptable commands in /​etc/​firewall.user) ​
 +but those rules will be more clear in the luci webinterface and also more readable for
 +less expert users.
 +
 +In general remember that forwardings are relying how routing rules are defined, and afterwards which zones are
 +defined on which interfaces.
 ==== Zone declaration for non-UCI interfaces ==== ==== Zone declaration for non-UCI interfaces ====
  
Line 481: Line 668:
 <​code>​ <​code>​
 config zone config zone
 +        option name             ​example
         option input            ACCEPT         option input            ACCEPT
         option output ​          ​ACCEPT         option output ​          ​ACCEPT
Line 495: Line 683:
 <​code>​ <​code>​
 config zone config zone
 +        option name             ​example
         option input            ACCEPT         option input            ACCEPT
         option output ​          ​ACCEPT         option output ​          ​ACCEPT
Line 511: Line 700:
 <​code>​ <​code>​
 config zone config zone
 +        option name             ​example
         option input            ACCEPT         option input            ACCEPT
         option output ​          ​ACCEPT         option output ​          ​ACCEPT
Line 549: Line 739:
  option dest lan  option dest lan
  option src wan6  option src wan6
 +#you don't need the below as you can a firewall rule to open the port that you need
 config forwarding config forwarding
  option dest wan6  option dest wan6
Line 606: Line 796:
  
 When connection attempts are //dropped// the client is not aware of the blocking and will continue to re-transmit its packets until the connection eventually times out. Depending on the way the client software is implemented,​ this could result in frozen or hanging programs that need to wait until a timeout occurs before they'​re able to continue. When connection attempts are //dropped// the client is not aware of the blocking and will continue to re-transmit its packets until the connection eventually times out. Depending on the way the client software is implemented,​ this could result in frozen or hanging programs that need to wait until a timeout occurs before they'​re able to continue.
 +
 +Also there is an interesting article which that claims dropping connections doesnt make you any safer - [[http://​www.chiark.greenend.org.uk/​~peterb/​network/​drop-vs-reject|Drop versus Reject]].
  
 **DROP** **DROP**
Line 619: Line 811:
  
  
-===== Note on connection tracking ​(NOTRACK) ​=====+===== Notes on connection tracking ===== 
 + 
 +==== NOTRACK ​====
  
 By default, the firewall will disable connection tracking for a zone if no masquerading is enabled. This is achieved by generating //NOTRACK// firewall rules matching all traffic passing via interfaces referenced by the firewall zone. The purpose of //NOTRACK// is to speed up routing and save memory by circumventing resource intensive connection tracking in cases where it is not needed. You can check if connection tracking is disabled by issuing ''​iptables -t raw -vnL'',​ it will list all rules, check for //NOTRACK// target. By default, the firewall will disable connection tracking for a zone if no masquerading is enabled. This is achieved by generating //NOTRACK// firewall rules matching all traffic passing via interfaces referenced by the firewall zone. The purpose of //NOTRACK// is to speed up routing and save memory by circumventing resource intensive connection tracking in cases where it is not needed. You can check if connection tracking is disabled by issuing ''​iptables -t raw -vnL'',​ it will list all rules, check for //NOTRACK// target.
Line 627: Line 821:
 If connection tracking is required, for example by custom rules in ''/​etc/​firewall.user'',​ the ''​conntrack''​ option must be enabled in the corresponding zone to disable //​NOTRACK//​. It should appear as ''​option '​conntrack'​ '​1'​ ''​ in the right zone in ''/​etc/​config/​firewall''​. If connection tracking is required, for example by custom rules in ''/​etc/​firewall.user'',​ the ''​conntrack''​ option must be enabled in the corresponding zone to disable //​NOTRACK//​. It should appear as ''​option '​conntrack'​ '​1'​ ''​ in the right zone in ''/​etc/​config/​firewall''​.
 For further information see http://​security.maruhn.com/​iptables-tutorial/​x4772.html . For further information see http://​security.maruhn.com/​iptables-tutorial/​x4772.html .
 +
 +==== nf_conntrack_skip_filter ====
 +
 +:!: Only available in Barrier Breaker. **''​Revoked in Chaos Calmer RC1 and onwards''​** due to various problems.
 +
 +From [[https://​dev.openwrt.org/​changeset/​42048/​trunk/​package|r42048]] to [[https://​dev.openwrt.org/​changeset/​44873|r44873]],​ there was a new setting activated by default which causes the packets with the established state, completely bypass iptables filter table. This is to [[https://​dev.openwrt.org/​ticket/​17690#​comment:​6|help with network performance]] and unless you need all packets to be counted by iptables filter or have some specific rules which would apply to already established connections,​ you should leave it active. ​
 +
 +This behavior can be disabled by editing /​etc/​sysctl.conf :
 +  net.netfilter.nf_conntrack_skip_filter=0
 +and then activating the new setting:
 +  sysctl -p
 +
 +or be temporarily turned off untill the next reboot by issuing :
 +  sysctl -w net.netfilter.nf_conntrack_skip_filter=0
  
 ===== How to delete a rule ===== ===== How to delete a rule =====
Line 687: Line 895:
 | :::    | ''​prerouting_rule'' ​          | user     | Container chain for custom user prerouting rules (firewall.user) | | :::    | ''​prerouting_rule'' ​          | user     | Container chain for custom user prerouting rules (firewall.user) |
 | :::    | ''​zone_//​name//​_prerouting'' ​ | internal | Per-zone container chains for DNAT (port forwarding) rules | | :::    | ''​zone_//​name//​_prerouting'' ​ | internal | Per-zone container chains for DNAT (port forwarding) rules |
 +| :::    | ''​prerouting_//​name//​_rule'' ​ | user     | Per-zone container chains for custom user prerouting rules (firewall.user) |
 | mangle | ''​INPUT'' ​                    | system ​  | | | mangle | ''​INPUT'' ​                    | system ​  | |
 | filter | ''​INPUT'' ​                    | system ​  | | | filter | ''​INPUT'' ​                    | system ​  | |
Line 693: Line 902:
 | :::    | ''​syn_flood'' ​                | internal | Internal chain to match and drop syn flood attempts | | :::    | ''​syn_flood'' ​                | internal | Internal chain to match and drop syn flood attempts |
 | :::    | ''​zone_//​name//​_input'' ​      | internal | Per-zone container chains for input rules | | :::    | ''​zone_//​name//​_input'' ​      | internal | Per-zone container chains for input rules |
 +| :::    | ''​input_//​name//​_rule'' ​      | user     | Per-zone container chains for custom user input rules (firewall.user) |
  
 ==== OUTPUT (originating from router) ==== ==== OUTPUT (originating from router) ====
Line 704: Line 914:
 | :::    | ''​output_rule'' ​              | user     | Container chain for custom user output rules (firewall.user) | | :::    | ''​output_rule'' ​              | user     | Container chain for custom user output rules (firewall.user) |
 | :::    | ''​zone_//​name//​_output'' ​     | internal | Per-zone container chains for output rules | | :::    | ''​zone_//​name//​_output'' ​     | internal | Per-zone container chains for output rules |
 +| :::    | ''​output_//​name//​_rule'' ​     | user     | Per-zone container chains for custom user output rules (firewall.user) |
 | mangle | ''​POSTROUTING'' ​              | system ​  | | | mangle | ''​POSTROUTING'' ​              | system ​  | |
 | nat    | ''​POSTROUTING'' ​              | system ​  | | | nat    | ''​POSTROUTING'' ​              | system ​  | |
Line 709: Line 920:
 | :::    | ''​postrouting_rule'' ​         | user     | Container chain for custom user postrouting rules (firewall.user) | | :::    | ''​postrouting_rule'' ​         | user     | Container chain for custom user postrouting rules (firewall.user) |
 | :::    | ''​zone_//​name//​_postrouting''​ | internal | Per-zone container chains for postrouting rules (masq, snat) | | :::    | ''​zone_//​name//​_postrouting''​ | internal | Per-zone container chains for postrouting rules (masq, snat) |
 +| :::    | ''​postrouting_//​name//​_rule''​ | user     | Per-zone container chains for custom user postrouting rules (firewall.user) |
  
 ==== FORWARD (relayed through router) ==== ==== FORWARD (relayed through router) ====
Line 721: Line 933:
 | :::    | ''​prerouting_rule'' ​          | user     | Container chain for custom user prerouting rules (firewall.user) | | :::    | ''​prerouting_rule'' ​          | user     | Container chain for custom user prerouting rules (firewall.user) |
 | :::    | ''​zone_//​name//​_prerouting'' ​ | internal | Per-zone container chains for DNAT (port forwarding) rules | | :::    | ''​zone_//​name//​_prerouting'' ​ | internal | Per-zone container chains for DNAT (port forwarding) rules |
 +| :::    | ''​prerouting_//​name//​_rule'' ​ | user     | Per-zone container chains for custom user prerouting rules (firewall.user) |
 | mangle | ''​FORWARD'' ​                  | system ​  | | | mangle | ''​FORWARD'' ​                  | system ​  | |
 | :::    | ''​mssfix'' ​                   | internal | Internal chain to hold for TCPMSS rules (mtu_fix) | | :::    | ''​mssfix'' ​                   | internal | Internal chain to hold for TCPMSS rules (mtu_fix) |
Line 727: Line 940:
 | :::    | ''​forwarding_rule'' ​          | user     | Container chain for custom user forward rules (firewall.user) | | :::    | ''​forwarding_rule'' ​          | user     | Container chain for custom user forward rules (firewall.user) |
 | :::    | ''​zone_//​name//​_forward'' ​    | internal | Per-zone container chains for output rules | | :::    | ''​zone_//​name//​_forward'' ​    | internal | Per-zone container chains for output rules |
 +| :::    | ''​forwarding_//​name//​_rule'' ​ | user     | Per-zone container chains for custom user forward rules (firewall.user) |
 | mangle | ''​POSTROUTING'' ​              | system ​  | | | mangle | ''​POSTROUTING'' ​              | system ​  | |
 | nat    | ''​POSTROUTING'' ​              | system ​  | | | nat    | ''​POSTROUTING'' ​              | system ​  | |
Line 732: Line 946:
 | :::    | ''​postrouting_rule'' ​         | user     | Container chain for custom user postrouting rules (firewall.user) | | :::    | ''​postrouting_rule'' ​         | user     | Container chain for custom user postrouting rules (firewall.user) |
 | :::    | ''​zone_//​name//​_postrouting''​ | internal | Per-zone container chains for postrouting rules (masq, snat) | | :::    | ''​zone_//​name//​_postrouting''​ | internal | Per-zone container chains for postrouting rules (masq, snat) |
 +| :::    | ''​postrouting_//​name//​_rule''​ | user     | Per-zone container chains for custom user postrouting rules (firewall.user) |
  
 +===== Open questions =====
 +==== '​enabled'​ option ====
 +Could it be that the enable option is available for every section
 +of the firewall file?
doc/uci/firewall.1368957565.txt.bz2 · Last modified: 2013/05/19 11:59 by orca