VPC firewall rules (original) (raw)

Virtual Private Cloud (VPC) firewall rules apply to a given project and network. If you want to apply firewall rules to multiple VPC networks in an organization, see Firewall policies and rules. The rest of this page covers VPC firewall rules only.

VPC firewall rules let you allow or deny connections to or from virtual machine (VM) instances in your VPC network. Enabled VPC firewall rules are always enforced, protecting your instances regardless of their configuration and operating system, even if they have not started up.

Every VPC network functions as a distributed firewall. While firewall rules are defined at the network level, connections are allowed or denied on a per-instance basis. You can think of the VPC firewall rules as existing not only between your instances and other networks, but also between individual instances within the same network.

For more information about firewalls, seeFirewall (computing).

Best practices for firewall rules

When designing and evaluating your firewall rules, keep in mind the following best practices:

Firewall rules in Google Cloud

When you create a VPC firewall rule, you specify a VPC network and a set of components that define what the rule does. The components enable you to target certain types of traffic, based on the traffic's protocol, destination ports, sources, and destinations. For more information, see firewall rule components.

You create or modify VPC firewall rules by using theGoogle Cloud console, theGoogle Cloud CLI, and the REST API. When you create or modify a firewall rule, you can specify the instances to which it is intended to apply by using the target parameter of the rule. For firewall rule examples, see Other configuration examples.

In addition to firewall rules that you create, Google Cloud has other rules that can affect incoming (ingress) or outgoing (egress) connections:

Specifications

VPC firewall rules have the following characteristics:

Implied actions

If no VPC firewall rule applies to a target, the firewall rule evaluation process might reach the last step. At this step, Cloud NGFW uses an implied action. The implied action depends on the traffic direction and the target resource type.

For more information, see Firewall rule evaluation process.

Pre-populated rules in the default network

The default network is pre-populated with firewall rules that allow incoming connections to instances. These rules can be deleted or modified as necessary:

Rule name Direction Priority Source ranges Action Protocols and ports Description
default-allow-internal ingress 65534 10.128.0.0/9 allow tcp:0-65535 udp:0-65535 icmp Permits incoming connections to VM instances from other instances within the same VPC network.
default-allow-ssh ingress 65534 0.0.0.0/0 allow tcp:22 Lets you connect to instances with tools such as ssh,scp, or sftp.
default-allow-rdp ingress 65534 0.0.0.0/0 allow tcp:3389 Lets you connect to instances using the Microsoft Remote Desktop Protocol (RDP).
default-allow-icmp ingress 65534 0.0.0.0/0 allow icmp Lets you use tools such as ping.

You can create similar firewall rules for networks other than the default network. See Configure firewall rules for common use cases for more information.

Blocked and limited traffic

Separate from VPC firewall rules and hierarchical firewall policies, Google Cloud blocks or limits certain traffic as described in the following table.

Traffic type Details
Packet rate and bandwidth Applies to: All egress packets All ingress packets Google Cloud accounts for bandwidth per VM instance, for each network interface (NIC) or IP address. A VM'smachine type defines its maximum possible egress rate; however, you can only achieve that maximum possible egress rate in specific situations.For details, seeNetwork bandwidth in the Compute Engine documentation.
DHCP offers and acknowledgments Applies to: Ingress packets to UDP port 68 (DHCPv4) Ingress packets to UDP port 546 (DHCPv6) Google Cloud blocks incoming DHCP offers and acknowledgments from all sources except for DHCP packets coming fromthe metadata server.
Protocols supported by Google Cloud external IP addresses Applies to: Ingress packets to external IP addresses External IPv4 and IPv6 addresses only accept TCP, UDP, ICMP, IPIP, AH, ESP, SCTP, and GRE packets. Resources that use external IP addresses impose additional protocol restrictions: Forwarding rules for protocol forwarding,external Application Load Balancers,external proxy Network Load Balancers, andexternal passthrough Network Load Balancers only processthe protocols andports configured on the forwarding rule. Cloud VPN gateways only acceptVPN protocols.
SMTP (port 25) traffic Applies to: Egress packets to external IP addresses on TCP port 25 To help prevent spam, by default Google Cloud blocks egress packets sent to TCP destination port 25 of an external IP address (including an external IP address of another Google Cloud resource). Google Cloud automatically removes this block once it determines a project is at low risk of sending large volumes of email over unencrypted connections. Google Cloud excludes SMTP traffic over TLS on port 465 or 587 from this block. To find out if your project permits this traffic, go to the VPC networks page or the Firewall policies page in the Google Cloud console. A banner message on both pages indicates whether your project permits this traffic. For more information, contact a Google Cloud sales specialist. This block does not apply to egress packets sent to TCP destination port 25 of an internal IP address, including a privately used public IP address in a VPC network or an on-premises network. If external SMTP egress on port 25 is allowed in your project, and you want to send this type of traffic, the following additional conditions must be met: Egress firewall rules in the VPC network and hierarchical firewall policies applicable to the VPC network must allow egress to the external IP address on TCP port 25. The implied allow egress rules meet this requirement because they allow egress to (and established inbound responses from) any IP address. The applicable route for the destination must use the default internet gateway next hop. Thesystem-generated default routes meet this requirement. The instance sending packets to the external IP address must meet the internet access requirements. You can prevent external SMTP egress by creating egress deny VPC firewall rules or hierarchical firewall policies.

Always allowed traffic

For VM instances, VPC firewall rules and hierarchical firewall policies do not apply to the following:

Google Cloud metadata server

Google Cloud runs a local metadata server alongside each instance. The server is accessible at 169.254.169.254 (for IPv4) and fd20:ce::254 (for IPv6). This server is essential to the operation of the instance, so the instance can access it regardless of any firewall rules that you configure. The metadata server provides the following basic services to the instance:

Product interactions

The following sections describe how firewall rules and hierarchical firewall policies interact with other Google Cloud products.

Firewall rules and passthrough load balancers

VPC firewall rules and hierarchical firewall policies do control which of the forwarding rule's supported protocols and ports are allowed to access the passthrough load balancer's backends. For details, see:

Firewall rules and proxy load balancers

For external Application Load Balancers, internal Application Load Balancers, internal proxy Network Load Balancers, and external proxy Network Load Balancers, VPC firewall rules and hierarchical firewall policies do not control which protocols and ports are accepted by the proxy load balancer's forwarding rule IP address. The forwarding rule alone determines which protocols and ports are accepted by the proxy load balancer.

VPC firewall rules and hierarchical firewall policies do control how these proxy load balancers communicate to their backends. For details, see:

Firewall rules and Cloud VPN

Firewall rules and hierarchical firewall policies do not control which protocols and ports are accepted by the Cloud VPN gateway.

Cloud VPN gateways only accept packets for the protocols and ports described in the Cloud VPN specifications.

Firewall rules and GKE

Google Kubernetes Engine creates and manages firewall rules automatically when you create a cluster or resources in the cluster (including Services and Ingresses). For more information, see Automatically created firewall rules in the Google Kubernetes Engine documentation.

Firewall rules and AI Hypercomputer

You can create VPC firewall rules when you create the VPC networks required for creating VMs in AI Hypercomputer. Use the firewall rules to specify the protocols and ports that are allowed for your VPC networks. For more information, see AI Hypercomputer overview.

Firewall rule components

Each firewall rule consists of the following configuration components:

Components summary

Ingress (inbound) rule
Priority Action Enforcement Target parameter Source and destination filters Protocols and ports
Integer from 0 to 65535, inclusive; default1000 allow or deny enabled (default) or disabled Specifies the instances that receive packets. Sources for ingress rules Destinations for ingress rules Specify a protocol or a protocol and a destination port. If not set, the rule applies to all protocols and destination ports. For more information, see Protocols and ports.
Egress (outbound) rule
Priority Action Enforcement Target parameter Source and destination filters Protocols and ports
Integer from 0 to 65535, inclusive; default1000 allow or deny enabled (default) or disabled Specifies the instances that send packets. Sources for egress rules Destinations for egress rules Specify a protocol or a protocol and a destination port. If not set, the rule applies to all protocols and destination ports. For more information, see Protocols and ports.

Direction of traffic

You can create firewall rules that apply to ingress or egress traffic. A single rule cannot apply to both ingress and egress traffic. However, you can create multiple rules to define the ingress and egress traffic that you allow or deny through the firewall.

Priority

The firewall rule priority is an integer from 0 to 65535, inclusive. Lower integers indicate higher priorities. If you do not specify a priority when creating a rule, it is assigned a priority of 1000.

The relative priority of a firewall rule determines whether it is applicable when evaluated against others. The evaluation logic works as follows:

Consider the following example where two firewall rules exist:

The priority of the second rule determines whether TCP traffic to port 80 is allowed for the webserver targets:

The previous example demonstrates how you can use priorities to create selectiveallow rules and global deny rules to implement a security best practice of least privilege.

Action on match

The action component of a firewall rule determines whether it permits or blocks traffic, subject to the other components of the rule:

Enforcement

You can choose whether a firewall rule is enforced by setting its state to enabled or disabled. You set the enforcement state when youcreate a rule or when youupdate a rule.

If you don't set an enforcement state when you create a new firewall rule, the firewall rule is automatically enabled.

Use cases

Disabling and enabling are useful for troubleshooting and performing maintenance. Consider changing the enforcement of a firewall rule in the following situations:

Effects on existing traffic

When you change a firewall rule's enforcement state, create a new enforcedrule, or modify firewall rules to deny traffic that was previously allowed, Cloud NGFW enforces this change only on new connections. Existing connections persist, and their associated traffic remains unaffected.

Protocols and ports

You can narrow the scope of a firewall rule by specifying protocols or protocols and destination ports. You can specify a protocol or a combination of protocols and their destination ports. If you omit both protocols and ports, the firewall rule is applicable for all traffic on any protocol and any destination port. Rules based on source ports are not supported.

Not all protocols support ports. For example, ports exist for TCP and UDP, but not for ICMP. ICMP does have different ICMP types, but they are not ports and cannot be specified in a firewall rule.

You can use the following protocol names in firewall rules: tcp, udp, icmp(for IPv4 ICMP), esp, ah, sctp, and ipip. For all other protocols, you must use theIANA protocol numbers.

Many protocols use the same name and number in both IPv4 and IPv6, but some protocols, such as ICMP, do not.

The IPv6 Hop-by-Hop protocol is not supported in firewall rules.

The following table summarizes valid protocol and destination port specification combinations for Google Cloud firewall rules.

Specification Example Explanation
No protocol and port If you do not specify a protocol, the firewall rule applies to all protocols and their applicable destination ports.
Protocol tcp If you specify a protocol without any port information, the firewall rule applies to that protocol and all its applicable ports.
Protocol and single port tcp:80 If you specify a protocol and a single destination port, the firewall rule applies to that destination port of the protocol.
Protocol and port range tcp:20-22 If you specify a protocol and a port range, the firewall rule applies to that destination port range for the protocol.
Combinations icmp,tcp:80tcp:443udp:67-69 You can specify various combinations of protocols and destination ports to which the firewall rule applies. For more information, see Create firewall rules.

Target, source, destination

Targets identify the network interfaces of instances to which the firewall rule applies.

You can specify both source and destination parameters that apply to the packet sources or destinations for both ingress and egress firewall rules. The direction of the firewall rule determines the possible values for the source and destination parameters.

Target parameter

The target parameter identifies the network interfaces of the Compute Engine instances, including GKE nodes and App Engine flexible environment instances.

You can define the following targets for both ingress or egress rules. The target, source, and destination parameters work together as described inSource, destination, target.

For information about the benefits and limitations of target network tags and target service accounts, see filtering by service account versus network tag.

Targets and IP addresses for ingress rules

The packets routed to the network interface of a target VM are processed based on the following conditions:

Targets and IP addresses for egress rules

The processing of packets emitted from the network interface of a target depends on the IP forwarding configuration on the target VM. IP forwarding is disabled by default.

Source parameter

Source parameter values depend on the direction of the firewall rule.

Sources for ingress rules

You can use the following sources for ingress firewall rules:

How source network tags and source service accounts imply packet sources

When an ingress firewall rule uses a source network tag, the packets must be emitted from a network interface that meets the following criteria:

When an ingress firewall rule uses a source service account, the packets must be emitted from a network interface that meets the following criteria:

In addition to specifying a network interface, when an ingress firewall rule uses either a source network tag or a source service account, packets emitted from the network interface of the VM must use one of the following valid source IP addresses:

If an ingress firewall rule also contains destination IP address ranges, the network interface bound to a network tag is resolved to the same IP version as the destination IP range.

No other packet source IP addresses are implied when using source network tags or source service accounts. For example, alias IP ranges and external IPv4 address associated with the network interface are excluded. If you need to create ingress firewall rules whose sources include alias IP address ranges or external IPv4 addresses, use source IPv4 ranges.

Sources for egress rules

You can use the following sources for egress firewall rules:

Follow these guidelines to add source IP address ranges for egress rules:

Destination parameter

Destinations can be specified by using IP address ranges, which are supported by both ingress and egress rules. The default destination behavior depends on the direction of the rule.

Destinations for ingress rules

You can use the following destinations for ingress firewall rules:

Follow these guidelines to add destination IP address ranges for ingress rules:

Destinations for egress rules

You can use the following destinations for egress firewall rules:

Source and target filtering by service account

You can use service accounts to create firewall rules that are more specific in nature:

The service account must becreated in the same project as the firewall rule before you create a firewall rule that relies on it. While the system does not stop you from creating a rule that uses a service account from a different project, the rule is not enforced if the service account doesn't exist in the firewall rule's project.

Firewall rules that use service accounts to identify instances apply to bothnew instances created and associated with the service accountand existing instances if you change their service accounts. Changing the service account associated with an instance requires that you stop and restart it. You can associate service accounts with individual instances and with instance templates used by managed instance groups.

Filter by service account versus network tag

This section highlights key points to consider when deciding if you should use service accounts or network tags to define targets and sources (for ingress rules).

If you need strict control over how firewall rules are applied to VMs, use target service accounts and source service accounts instead of target network tags and source network tags:

You cannot mix and match service accounts and network tags in any firewall rule:

Following are operational considerations for service accounts and network tags:

Roles and permissions

The following table describes the Identity and Access Management (IAM) permissions that you need for working with VPC firewall rules.

Task Required permission Sample role
Create a firewall rule compute.firewalls.create Compute Security Admin role (roles/compute.securityAdmin)
Delete a firewall rule compute.firewalls.delete Compute Security Admin (roles/compute.securityAdmin)
Make changes to firewall rules compute.firewalls.update Compute Security Admin role (roles/compute.securityAdmin)
View details about a firewall rule compute.firewalls.get Compute Network Viewer role (roles/compute.networkViewer)
View a list of firewall rules compute.firewalls.list Compute Network Viewer role (roles/compute.networkViewer)

Use cases

The following use cases demonstrate how firewall rules work. In these examples, all the firewall rules are enabled.

Ingress cases

Ingress firewall rules control incoming connections from a source to target instances in your VPC network. The source for an ingress rule can be defined as one of the following:

The default source is any IPv4 address (0.0.0.0/0). If you want to control incoming connections for sources outside your VPC network, including other sources on the internet, use a range of IP addresses in CIDR format.

Ingress rules with an allow action permit incoming traffic based on the othercomponents of the rule. In addition to specifying the source and target for the rule, you can limit the rule to apply to specific protocols and destination ports. Similarly, ingress rules with a deny action can be used to protect instances by blocking incoming traffic based on the firewall rule components.

Ingress examples

Figure 1 illustrates some examples where firewall rules can control ingress connections. The examples use the _target_parameter in rule assignments to apply rules to specific instances.

In this example VPC network, the allow ingress firewall     rules override the implied deny ingress rule for some VMs.

Figure 1. In this example VPC network, the allow ingress firewall rules override the implied deny ingress rule for some VMs (click to enlarge).

Egress cases

Egress firewall rules control outgoing connections from target instances in your VPC network. Egress rules with an allow action permit traffic from instances based on the other components of the rule. For example, you can permit outbound traffic to specific destinations, such as a range of IPv4 addresses, on protocols and destination ports that you specify. Similarly, egress rules with a denyaction block traffic based on the other components of the rule.

Every egress rule needs a destination. The default destination is any IPv4 address (0.0.0.0/0), but you can create a more specific destination by using a range of IPv4 or IPv6 addresses in CIDR format. When specifying a range of IP addresses, you can control traffic to instances in your network and to destinations outside your network, including destinations on the internet.

Egress examples

Figure 2 illustrates some examples where firewall rules can control egress connections. The examples use the _target_parameter in rule assignments to apply rules to specific instances.

In this example VPC network, the deny egress firewall     rules override the implied allow egress rule for some VMs.

Figure 2. In this example VPC network, the deny egress firewall rules override the implied allow egress rule for some VMs (click to enlarge).

What's next

Try it for yourself

If you're new to Google Cloud, create an account to evaluate how Cloud NGFW performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.

Try Cloud NGFW free