Firewall utilities
From __init__.py
:
Function | add |
Add default ipv6 rules to firewall config, if they don't exist already. |
Function | add |
Shortuct to add a device to lan zone |
Function | add |
Shortuct to add a device to wan zone |
Function | add |
Add given device to a firewall zone. The device is not added if the firewall zone does not exists Changes are saved to staging area. |
Function | add |
Add forwarding from src to dest. |
Function | add |
Add given interface to a firewall zone. The interface is not added if the firewall zone does not exists Changes are saved to staging area. |
Function | add |
Add nat rule to firewall config. |
Function | add |
Add netmap rule to netmap config. |
Function | add |
Add rule to firewall config. |
Function | add |
Create an ACCEPT traffic rule for the given service Changes are saved to staging area. |
Function | add |
Create a forwarding from templates database. Changes are saved to staging area. |
Function | add |
Create a rule from templates database. Changes are saved to staging area. |
Function | add |
Create all rules for the given service group Changes are saved to staging area. |
Function | add |
Create a zone from templates database. Changes are saved to staging area. |
Function | add |
be able to access lan and wan zone |
Function | add |
Create a network interface for the given device. The interface can be used for PBR (Policy Based Routing). Changes are saved to staging area. |
Function | add |
Add zone to firewall config. |
Function | apply |
commit changes to firewall config |
Function | delete |
Delete all sections matching the given link. Changes are saved to staging area. |
Function | delete |
Delete nat rule from firewall config. |
Function | delete |
Delete netmap rule from netmap config. |
Function | delete |
Delete rule from firewall config. |
Function | delete |
Delete zone and all forwardings that are connected to it. |
Function | disable |
Disable all rules, forwardings, redirects, zones and ipsets for ipv6-only family. Changes are saved to staging area. |
Function | disable |
Disable all rules matching the given link Changes are saved to staging area. |
Function | disable |
Disable rule from firewall config. |
Function | disable |
Disable the ACCEPT rule traffic for the given service. Changes are saved to staging area. |
Function | edit |
Edit nat rule in firewall config. |
Function | edit |
Edit netmap rule in netmap config. |
Function | edit |
Edit rule in firewall config. |
Function | edit |
Edit an existing zone. |
Function | enable |
Enable rule from firewall config. |
Function | enable |
Disable the ACCEPT rule traffic for the given service Changes are saved to staging area. |
Function | enrich |
Enrich rule with more human-readable data and missing fields |
Function | get |
Search all database, execpt templates one, for entities with the given link |
Function | get |
Get rule config name and rule data by rule name, optionally filtered by tag. Assume there is only one rule with the same name. |
Function | get |
Get service by name. |
Function | get |
Get zone config name by zone name. |
Function | is |
Check if rule is a forward rule |
Function | is |
Check if rule is an input rule |
Function | is |
Search the network database for devices and interfaces using IPv6 |
Function | is |
Check if rule is an output rule |
Function | list |
Get all active leases from /tmp/dhcp.leases |
Function | list |
Get all forward rules from firewall config |
Function | list |
Get all forwardings from firewall config |
Function | list |
Get all hosts from dhcp and network config |
Function | list |
Get all input rules from firewall config |
Function | list |
Get all nat rules from firewall config |
Function | list |
Get all network devices from ip command |
Function | list |
Get all netmap rules from netmap config |
Function | list |
Get all objects from objects config |
Function | list |
Get all output rules from firewall config |
Function | list |
Get all rule ids from firewall config |
Function | list |
Get all rules from firewall config |
Function | list |
Get all services from /etc/services |
Function | list |
Get all zones from firewall config |
Function | list |
Get all zones from firewall config, excluding aliases in network section |
Function | order |
Orders firewall rules, moves everything else but rules to the end of the list. |
Function | remove |
Remove the given device from a firewall zone. The operation always succeed if the zone does not exists |
Function | remove |
Remove the given interface from a firewall zone. The operation always succeed if the zone does not exists |
Function | remove |
Remove the ACCEPT traffic rule for the given service Changes are saved to staging area. |
Function | reorder |
Reorder firewall config, moving all rules at the bottom. The order in the file will be: - defaults and includes - zones - forwardings - forward rules - output rules - input rules |
Function | resolve |
Resolve address to a more human-redeable name. |
Function | setup |
Set up a rule in the firewall config. |
Function | split |
Split firewall config into sections. |
Function | update |
Update firewall rules with ipset field set and ns_src set. |
Function | update |
Update redirect rules with ipset field set and ns_src set. |
Function | validate |
Validate address format. Valid formats are: - ip address - ip range like 192.168.100.1-192.168.100.10 - ip cidr |
Function | validate |
Undocumented |
Function | validate |
Validate port format. |
Function | validate |
Validate rule. |
Function | zone |
Check if a zone with name zone_name already exists |
Constant | PROTOCOLS |
Undocumented |
Constant | TARGETS |
Undocumented |
Add default ipv6 rules to firewall config, if they don't exist already.
- Args:
- uci: EUci pointer
- Returns:
- list of added rule config names
Shortuct to add a device to lan zone
- Arguments:
- uci -- EUci pointer
- device -- Device name
- Returns:
- The name of section or None
Shortuct to add a device to wan zone
- Arguments:
- uci -- EUci pointer
- device -- Device name
- Returns:
- The name of the configuration section or None
Add given device to a firewall zone. The device is not added if the firewall zone does not exists Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- device -- Device name
- zone -- Firewall zone name
- Returns:
- If the firewall zone exists, the name of the section where the device has been added.
- None, otherwise.
Add forwarding from src to dest.
- Args:
- uci: EUci pointer src: source zone, must be zone name, not config name dest: destination zone, must be zone name, not config name
- Returns:
- name of forwarding config that was added
Add given interface to a firewall zone. The interface is not added if the firewall zone does not exists Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- interface -- Interface name
- zone -- Firewall zone name
- Returns:
- If the firewall zone exists, the name of the section where the device has been added.
- None, otherwise.
str
, target: str
, src: str
= '*', src_ip: str
= '', dest_ip: str
= '', snat_ip: str
= '') -> str
:
(source)
¶
Add nat rule to firewall config.
- Args:
- uci: EUci pointer name: name of rule target: target, must be one of 'SNAT', 'DNAT' src: source zone, must be zone name, not config name src_ip: source ip dest_ip: destination ip snat_ip: snat ip
- Returns:
- name of rule config that was added
str
, src: str
, dest: str
, device_in: list[ str]
, device_out: list[ str]
, map_from: str
, map_to: str
) -> str
:
(source)
¶
Add netmap rule to netmap config.
- Args:
- uci: EUci pointer name: name of rule src: source zone, must be zone name, not config name dest: destination zone, must be zone name, not config name device_in: list of incoming network interfaces device_out: list of outgoing network interfaces map_from: source network address map_to: destination network address
- Returns:
- name of rule config that was added
str
, src: str
, src_ip: list[ str]
, dest: str
, dest_ip: list[ str]
, proto: list
, dest_port: list[ str]
, target: str
, service: str
, enabled: bool
= True, log: bool
= False, tag=[], add_to_top: bool
= False, ns_src: str
= None, ns_dst: str
= None) -> str
:
(source)
¶
Add rule to firewall config.
- Args:
- uci: EUci pointer
id: id of rule to edit
name: name of rule
src: source zone, must be zone name, not config name
src_ip: a list of source ip
dest: destination zone, must be zone name, not config name
dest_ip: a list of destination ip
proto: protocol, must be a list of protocols in "tcp", "udp", "udplite", "icmp", "esp", "ah", "sctp"
dest_port: a list of destination ports, each element cna be be a port number, a comma-separated list of port numbers or a range with
-
(eg. 80-90) target: target, must be one of 'ACCEPT', 'REJECT', 'DROP' service: service name enabled: if True, rule is enabled, if False, rule is disabled log: if True, log traffic tag: list of optional tags add_to_top: if True, add rule to the top of the list, otherwise add to the bottom ns_src: an object in the form<database>/
ns_dst: an object in the form<database>/
- Returns:
- name of rule config that was added
Create an ACCEPT traffic rule for the given service Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- name -- Service name
- port -- Service port number as string or array
- proto -- List of service protocols
- link -- A reference to an existing key in the format <database>/<keyname> (optional)
- Returns:
- The name of the configuration section
Create a forwarding from templates database. Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- name -- Name of the template forwarding from the templates database
- link -- A reference to an existing key in the format <database>/<keyname> (optional)
- Returns a tuple:
- The name of the configuration section for the forwarding or None in case of error
Create a rule from templates database. Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- name -- Name of the template rule from the templates database
- proto -- A valid UCI protocol (optional)
- port -- A port or comma-separated list of ports (optional)
- link -- A reference to an existing key in the format <database>/<keyname> (optional)
- Returns:
- The name of the configuration section for the rule or None in case of error
Create all rules for the given service group Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- name -- Name of the service group from the templates database
- src -- Source zone, default is 'lan'. The zone must already exists inside the firewall db
- dest -- Destination zone, default is 'wan'. The zone must already exists inside the firewall db
- link -- A reference to an existing key in the format <database>/<keyname> (optional)
- Returns:
- A list of configuration section names of each rule, None in case of error
Create a zone from templates database. Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- name -- Name of the zone from the templates database
- network -- A list of interfaces to be added to the zone (optional)
- link -- A reference to an existing key in the format <database>/<keyname> (optional)
- Returns a tuple:
- The name of the configuration section for the zone or None in case of error
- A list of configuration section names for the forwardings or None in case of error
- Create a trusted zone. The zone will:
- be able to access lan and wan zone
- be accessible from lan zone
If a zone with the same name already exists, do not recreate it. Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- name -- Zone name, maximum length is 12
- network -- A list of interfaces to be added to the zone (optional)
- link -- A reference to an existing key in the format <database>/<keyname> (optional)
- Returns a tuple:
- The name of the configuration section or None in case of error
- A list of configuration sections or an empy list in case of error
Create a network interface for the given device. The interface can be used for PBR (Policy Based Routing). Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- name -- Interface name
- device -- Device name
- link -- A reference to an existing key in the format <database>/<keyname> (optional)
- Returns:
- The name of the configuration section or None in case of error
str
, input: str
, forward: str
, traffic_to_wan: bool
= False, forwards_to: list[ str]
= None, forwards_from: list[ str]
= None, log: bool
= False) -> set([ str, set[ str]])
:
(source)
¶
Add zone to firewall config.
- Args:
- uci: EUci pointer name: name of zone input: rule for input traffic, must be one of 'ACCEPT', 'REJECT', 'DROP' forward: rule for forward traffic, must be one of 'ACCEPT', 'REJECT', 'DROP' traffic_to_wan: if True, add forwarding from zone to wan forwards_to: list of zones to forward traffic to forwards_from: list of zones to forward traffic from log: if True, log blocked traffic destined to this zone
- Returns:
- tuple of zone config name and set of added forwarding configs
- Apply firewall configuration:
- commit changes to firewall config
- reload the firewall service
- Arguments:
- uci -- EUci pointer
Delete all sections matching the given link. Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- link -- A reference to an existing key in the format <database>/<keyname>
- Returns:
- A list of deleted sections
Delete nat rule from firewall config.
- Args:
- uci: EUci pointer id: id of rule to delete
- Returns:
- name of rule config that was deleted
Delete netmap rule from netmap config.
- Args:
- uci: EUci pointer id: id of rule to delete
- Returns:
- name of rule config that was deleted
Delete rule from firewall config.
- Args:
- uci: EUci pointer id: name of rule config to delete
- Returns:
- name of rule config that was deleted
- Raises:
- ValueError: if id is not a valid rule config name
Delete zone and all forwardings that are connected to it.
- Args:
- uci: EUci pointer zone_config_name: name of zone config to delete
- Returns:
- tuple of zone config name and set of deleted forwarding configs
- Raises:
- ValueError: if zone_config_name is not a valid zone config name
Disable all rules, forwardings, redirects, zones and ipsets for ipv6-only family. Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- Returns:
- A list of disabled sections
Disable all rules matching the given link Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- link -- A reference to an existing key in the format <database>/<keyname>
- Returns:
- A list of disabled sections
Disable rule from firewall config.
- Args:
- uci: EUci pointer id: name of rule config to disable
- Returns:
- name of rule config that was disabled
- Raises:
- ValueError: if id is not a valid rule config name
Disable the ACCEPT rule traffic for the given service. Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- name -- Service name
- Returns:
- The name of the configuration section if found, None otherwise
str
, name: str
, target: str
, src: str
= '*', src_ip: str
= '', dest_ip: str
= '', snat_ip: str
= '') -> str
:
(source)
¶
Edit nat rule in firewall config.
- Args:
- uci: EUci pointer id: id of rule to edit name: name of rule target: target, must be one of 'SNAT', 'DNAT' src: source zone, must be zone name, not config name src_ip: source ip dest_ip: destination ip snat_ip: snat ip
- Returns:
- name of rule config that was edited
str
, name: str
, src: str
, dest: str
, device_in: list[ str]
, device_out: list[ str]
, map_from: str
, map_to: str
) -> str
:
(source)
¶
Edit netmap rule in netmap config.
- Args:
- uci: EUci pointer id: id of rule to edit name: name of rule src: source zone, must be zone name, not config name dest: destination zone, must be zone name, not config name device_in: list of incoming network interfaces device_out: list of outgoing network interfaces map_from: source network address map_to: destination network address
- Returns:
- name of rule config that was edited
str
, name: str
, src: str
, src_ip: list[ str]
, dest: str
, dest_ip: list[ str]
, proto: list
, dest_port: list[ str]
, target: str
, service: str
, enabled: bool
= True, log: bool
= False, tag=[], ns_src: str
= None, ns_dst: str
= None) -> str
:
(source)
¶
Edit rule in firewall config.
- Args:
- uci: EUci pointer
id: id of rule to edit
name: name of rule
src: source zone, must be zone name, not config name
src_ip: a list of source ip
dest: destination zone, must be zone name, not config name
dest_ip: a list of destination ip
proto: protocol, must be a list of protocols in "tcp", "udp", "udplite", "icmp", "esp", "ah", "sctp"
dest_port: a list of destination ports, each element cna be be a port number, a comma-separated list of port numbers or a range with
-
(eg. 80-90) target: target, must be one of 'ACCEPT', 'REJECT', 'DROP' service: service name enabled: if True, rule is enabled, if False, rule is disabled log: if True, log traffic tag: list of optional tags ns_src: an object in the form<database>/
ns_dst: an object in the form<database>/
- Returns:
- name of rule config that was edited
str
, input: str
, forward: str
, traffic_to_wan: bool
= False, forwards_to: list[ str]
= None, forwards_from: list[ str]
= None, log: bool
= False) -> set([ str, set[ str]])
:
(source)
¶
Edit an existing zone.
- Args:
- uci: EUci pointer name: name of zone to edit input: rule for input traffic, must be one of 'ACCEPT', 'REJECT', 'DROP' forward: rule for forward traffic, must be one of 'ACCEPT', 'REJECT', 'DROP' traffic_to_wan: if True, add forwarding from zone to wan forwards_to: list of zones to forward traffic to forwards_from: list of zones to forward traffic from log: if True, log blocked traffic destined to this zone
- Returns:
- tuple of zone config name and set of updated forwarding configs
Enable rule from firewall config.
- Args:
- uci: EUci pointer id: name of rule config to enable
- Returns:
- name of rule config that was enabled
- Raises:
- ValueError: if id is not a valid rule config name
Disable the ACCEPT rule traffic for the given service Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- name -- Service name
- Returns:
- The name of the configuration section if found, None otherwise
Enrich rule with more human-readable data and missing fields
- Args:
- uci: EUci pointer rule: rule to enrich
- Returns:
- enriched rule
Search all database, execpt templates one, for entities with the given link
- Arguments:
- uci -- EUci pointer
- link -- A reference to an existing key in the format <database>/<keyname>
- Returns:
- A dictionary of all matched sections like {"db1": ["key1", "key2"], "db2": [...] }
Get rule config name and rule data by rule name, optionally filtered by tag. Assume there is only one rule with the same name.
- Args:
- uci: EUci pointer name: rule name tag: optional tag to filter rules
- Returns:
- tuple of rule config name and rule config if rule with name name exists, (None, None) otherwise
Get service by name.
- Args:
- name: service name
- Returns:
- service dict if service with name name exists, None otherwise
Get zone config name by zone name.
- Args:
- uci: EUci pointer name: zone name
- Returns:
- tuple of zone config name and zone config if zone with name name exists, (None, None) otherwise
Check if rule is a forward rule
- Args:
- rule: rule to check
- Returns:
- True if rule is a forward rule, False otherwise
Check if rule is an input rule
- Args:
- rule: rule to check
- Returns:
- True if rule is an input rule, False otherwise
Search the network database for devices and interfaces using IPv6
- Arguments:
- uci -- EUci pointer
- Returns:
- True if IPv6 is enabled at least on a device or interface, False otherwise
Check if rule is an output rule
- Args:
- rule: rule to check
- Returns:
- True if rule is an output rule, False otherwise
Get all active leases from /tmp/dhcp.leases
- Returns:
- a list of all active leases, each lease is a dict with keys value, label, type
Get all forward rules from firewall config
- Args:
- uci: EUci pointer
- Returns:
- a list of all forward rules
Get all hosts from dhcp and network config
- Args:
- uci: EUci pointer
- Returns:
- a list of all hosts, each host is a dict with keys value, label, type
Get all network devices from ip command
- Args:
- uci: EUci pointer
- Returns:
- a list of all network devices
Get all objects from objects config
- Args:
- uci: EUci pointer expand: if True, expand object details
- Returns:
- a list of all objects, each object is a dict with keys value, label, type
Get all output rules from firewall config
- Args:
- uci: EUci pointer
- Returns:
- a list of all output rules
Get all rules from firewall config
- Args:
- uci: EUci pointer rule_type: optional rule type to filter, must be one of 'input', 'output' or 'forward'
- Returns:
- a list of all rules
Get all services from /etc/services
- Returns:
- a list of all services, each service is a dict with keys id, port, proto
Get all zones from firewall config, excluding aliases in network section
- Args:
- uci: EUci pointer
- Returns:
- dict with all zones
Orders firewall rules, moves everything else but rules to the end of the list.
- Args:
- e_uci: euci instance rule_type: type of rule to order, must be 'input', 'output' or 'forward' rules: which order to put rules
- Returns:
- list of ordered rules entries
- Raises:
- ValidationError: if a rule is not present in /etc/config/firewall
Remove the given device from a firewall zone. The operation always succeed if the zone does not exists
Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- device -- Device name
- zone -- Firewall zone name
- Returns:
- If the firewall zone exists, the name of the section where the device has been removed.
- None, otherwise.
Remove the given interface from a firewall zone. The operation always succeed if the zone does not exists
Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- interface -- Interface name
- zone -- Firewall zone name
- Returns:
- If the firewall zone exists, the name of the section where the interface has been removed.
- None, otherwise.
Remove the ACCEPT traffic rule for the given service Changes are saved to staging area.
- Arguments:
- uci -- EUci pointer
- name -- Service name
- Returns:
- The name of the configuration section
Reorder firewall config, moving all rules at the bottom. The order in the file will be: - defaults and includes - zones - forwardings - forward rules - output rules - input rules
- Args:
- uci: EUci pointer
Resolve address to a more human-redeable name.
- Args:
- uci: EUci pointer address: address to resolve
- Returns:
- resolved address as a dict with keys value, label and type
str
, name: str
, src: str
, src_ip: list[ str]
, dest: str
, dest_ip: list[ str]
, proto: list
, dest_port: list[ str]
, target: str
, service: str
, enabled: bool
= True, log: bool
= False, tag=[], ns_src: str
= None, ns_dst: str
= None):
(source)
¶
Set up a rule in the firewall config.
- Args:
- uci: EUci pointer
id: id of the rule
name: name of the rule
src: source zone, must be zone name, not config name
src_ip: a list of source IP addresses
dest: destination zone, must be zone name, not config name
dest_ip: a list of destination IP addresses
proto: protocol, must be a list of protocols in "tcp", "udp", "udplite", "icmp", "esp", "ah", "sctp"
dest_port: a list of destination ports, each element can be a port number, a comma-separated list of port numbers, or a range with
-
(e.g., 80-90) target: target, must be one of 'ACCEPT', 'REJECT', 'DROP' service: service name enabled: if True, rule is enabled; if False, rule is disabled log: if True, log traffic tag: list of optional tags ns_src: an object in the form<database>/
ns_dst: an object in the form<database>/
Split firewall config into sections.
- Args:
- uci: EUci pointer
- Returns:
- tuple of lists of sections, in the following order: defaults, zones, forwardings, other_rules, forward_rules, output_rules, input_rules
Update redirect rules with ipset field set and ns_src set.
- Args:
- uci: EUci pointer
changed_sections: list of changed objects, each object is in the form of
<database>/
Validate address format. Valid formats are: - ip address - ip range like 192.168.100.1-192.168.100.10 - ip cidr
- Args:
- address: address to validate
- Returns:
- True if address is valid, False otherwise
str
, src_ip: list[ str]
, dest: str
, dest_ip: list[ str]
, proto: list
, dest_port: list[ str]
, target: str
, service: str
, ns_src: str
, ns_dst: str
):
(source)
¶
Validate rule.
- Args:
- src: source zone, must be zone name, not config name
src_ip: a list of source ip
dest: destination zone, must be zone name, not config name
dest_ip: a list of destination ip
proto: protocol, must be a list of protocols in "tcp", "udp", "udplite", "icmp", "esp", "ah", "sctp"
dest_port: a list of destination ports, each element cna be be a port number, a comma-separated list of port numbers or a range with
-
(eg. 80-90) target: target, must be one of 'ACCEPT', 'REJECT', 'DROP' service: service name ns_src: an object in the form<database>/
ns_dst: an object in the form<database>/
- Raises:
- ValidationError: if rule is invalid
Check if a zone with name zone_name already exists
- Args:
- u: EUci pointer zone_name: zone name to check
- Returns:
- true if a zone with name zone_name already exists, false otherwise