Configure tunnels·续

<details>

<summary>

GRE tunnel

</summary>

Create a <code>POST</code> request <a href="https://developers.cloudflare.com/api/resources/magic_transit/subresources/gre_tunnels/methods/create/">using the API</a> to create a GRE tunnel.

<details>

<summary>

Required API token permissions

</summary>

At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:

  • <code>Magic WAN Write</code>
  • <code>Magic Transit Write</code>

</details>

Create a GRE tunnelbash

curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/magic/gre_tunnels" \
    --request POST \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    --json '{
        "name": "<TUNNEL_NAME>",
        "description": "<TUNNEL_DESCRIPTION>",
        "interface_address": "<INTERFACE_ADDRESS>",
        "cloudflare_gre_endpoint": "<CLOUDFLARE_ENDPOINT>",
        "customer_gre_endpoint": "<CUSTOMER_ENDPOINT>"
    }'
{
  "errors": [
    {
      "code": 1000,
      "message": "message"
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message"
    }
  ],
  "result": {
    "gre_tunnels": [
      {
        "cloudflare_gre_endpoint": "<IP_ADDRESS>",
        "customer_gre_endpoint": "<IP_ADDRESS>",
        "interface_address": "<INTERFACE_CIDR>",
        "name": "<TUNNEL_NAME>",
        "description": "<TUNNEL_DESCRIPTION>",
        "health_check": {
          "direction": "unidirectional",
          "enabled": true,
          "rate": "low",
          "type": "reply"
        },
        "mtu": 0,
        "ttl": 0
      }
    ]
  },
  "success": true
}

</details>

<details>

<summary>

IPsec tunnel

</summary>

  1. Create a <code>POST</code> request <a href="https://developers.cloudflare.com/api/resources/magic_transit/subresources/ipsec_tunnels/methods/create/">using the API</a> to create an IPsec tunnel.

Note that in the example, replay protection is disabled by default. You can enable it with the flag <code>"replay_protection": true</code> for each IPsec tunnel, if the devices you use do not support disabling this feature. If you have already created IPsec tunnels, update them with a <a href="https://developers.cloudflare.com/api/resources/magic_transit/subresources/ipsec_tunnels/methods/update/"><code>PUT</code> request</a>. Refer to <a href="https://developers.cloudflare.com/magic-transit/reference/anti-replay-protection/">Anti-replay protection</a> for more information on this topic.<details><summary>

Required API token permissions</summary>

At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required: - <code>Magic WAN Write</code> - <code>Magic Transit Write</code></details>

Create an IPsec tunnelbash

   curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/magic/ipsec_tunnels" \
    --request POST \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
    --json '{
        "name": "<TUNNEL_NAME>",
        "description": "<TUNNEL_DESCRIPTION>",
        "interface_address": "<INTERFACE_ADDRESS>",
        "cloudflare_endpoint": "<CLOUDFLARE_ENDPOINT>",
        "customer_endpoint": "<CUSTOMER_ENDPOINT>"
    }'
   ```

```json
   {
    "errors": [
        {
            "code": 1000,
            "message": "message"
        }
    ],
    "messages": [
        {
            "code": 1000,
            "message": "message"
        }
    ],
    "result": {
        "ipsec_tunnels": [
            {
                "id": "<IPSEC_TUNNEL_ID>",
                "interface_address": "<INTERFACE_CIDR>",
                "name": "<TUNNEL_NAME>",
                "cloudflare_endpoint": "<IP_ADDRESS>",
                "customer_endpoint": "<IP_ADDRESS>",
                "description": "<TUNNEL_DESCRIPTION>",
                "health_check": {
                    "direction": "unidirectional",
                    "enabled": true,
                    "rate": "low",
                    "type": "reply"
                },
                "psk_metadata": {},
                "replay_protection": false
            }
        ]
    },
    "success": true
   }
   ```

Take note of the tunnel &lt;code>id&lt;/code> value. We will use it to generate a pre-shared key (PSK).
2. Create a &lt;code>POST&lt;/code> &lt;a href="https://developers.cloudflare.com/api/resources/magic_transit/subresources/ipsec_tunnels/methods/psk_generate/">request&lt;/a> to generate a PSK. Use the tunnel &lt;code>id&lt;/code> value you received from the previous command.&lt;details>&lt;summary>

Required API token permissions&lt;/summary>

At least one of the following &lt;a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions&lt;/a> is required:
   - &lt;code>Magic WAN Write&lt;/code>
   - &lt;code>Magic Transit Write&lt;/code>&lt;/details>

*Generate Pre-Shared Key (PSK) for IPsec tunnelsbash*

```bash
   curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/magic/ipsec_tunnels/$IPSEC_TUNNEL_ID/psk_generate" \
    --request POST \
    --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"
   ```

```json
   {
    "result": {
        "ipsec_id": "<IPSEC_ID>",
        "ipsec_tunnel_id": "<IPSEC_TUNNEL_ID>",
        "psk": "<PSK_CODE>",
        "psk_metadata": {
            "last_generated_on": "2025-03-13T14:28:47.054317925Z"
        }
    },
    "success": true,
    "errors": [],
    "messages": []
   }
   ```

Take note of your &lt;code>psk&lt;/code> value.
3. Create a &lt;code>PUT&lt;/code> &lt;a href="https://developers.cloudflare.com/api/resources/magic_transit/subresources/ipsec_tunnels/methods/update/">request&lt;/a> to update your IPsec tunnel with the PSK.

```bash
   curl "https://api.cloudflare.com/client/v4/accounts/%7Baccount_id%7D/magic/ipsec_tunnels/%7Bipsec_tunnel_id%7D" \
    --request PUT \
    --json '{
        "psk": "<PSK_VALUE>"
    }'
   ```

```json
{
  "result": {
    "modified": true,
    "modified_ipsec_tunnel": {
      "id": "<IPSEC_ID>",
      "interface_address": "<IPSEC_CIDR>",
      "created_on": "2025-03-13T14:28:21.139535Z",
      "modified_on": "2025-03-13T14:33:26.09683Z",
      "name": "<TUNNEL_NAME>",
      "cloudflare_endpoint": "<IP_ADDRESS>",
      "customer_endpoint": "<IP_ADDRESS>",
      "remote_identities": {
        "hex_id": "",
        "fqdn_id": "",
        "user_id": ""
      },
      "psk_metadata": {
        "last_generated_on": "2025-03-13T14:28:47.054318Z"
      },
      "description": "<TUNNEL_DESCRIPTION>",
      "health_check": {
        "enabled": true,
        "target": "",
        "type": "reply",
        "rate": "mid",
        "direction": "unidirectional"
      }
    }
  },
  "success": true,
  "errors": [],
  "messages": []
}
  1. Use the <code>psk</code> value from step 3 to configure the IPsec tunnel on your equipment as well.

</details>

<details>

<summary>

Configure bidirectional health checks

</summary>

Bidirectional health checks are available for GRE and IPsec tunnels. For Magic Transit this option defaults to unidirectional.

You can change this setting via the API with <code>"bidirectional"</code> or <code>"unidirectional"</code>:

curl "https://api.cloudflare.com/client/v4/accounts/%7Baccount_id%7D/magic/ipsec_tunnels/%7Bipsec_tunnel_id%7D" \
    --request PUT \
    --json '{
        "health_check": {
                "direction": "bidirectional"
        }
    }'
{
  "result": {
    "modified": true,
    "modified_ipsec_tunnel": {
      "id": "<IPSEC_ID>",
      "interface_address": "<IPSEC_CIDR>",
      "created_on": "2025-03-13T14:28:21.139535Z",
      "modified_on": "2025-03-13T14:33:26.09683Z",
      "name": "<TUNNEL_NAME>",
      "cloudflare_endpoint": "<IP_ADDRESS>",
      "customer_endpoint": "<IP_ADDRESS>",
      "remote_identities": {
        "hex_id": "",
        "fqdn_id": "",
        "user_id": ""
      },
      "psk_metadata": {
        "last_generated_on": "2025-03-13T14:28:47.054318Z"
      },
      "description": "<TUNNEL_DESCRIPTION>",
      "health_check": {
        "enabled": true,
        "target": "",
        "type": "reply",
        "rate": "mid",
        "direction": "bidirectional"
      }
    }
  },
  "success": true,
  "errors": [],
  "messages": []
}

</details>

Bidirectional vs unidirectional health checks

To check for tunnel health, Cloudflare sends a health check probe consisting of ICMP (Internet Control Message Protocol) reply packets ↗︎ to your network. Cloudflare needs to receive these probes to know if your tunnel is healthy.

Cloudflare defaults to unidirectional health checks for Magic Transit (direct server return), and bidirectional health checks for Cloudflare WAN. However, routing unidirectional ICMP reply packets over the Internet to Cloudflare is sometimes subject to drops by intermediate network devices, such as stateful firewalls. Magic Transit customers with egress traffic can modify this setting to bidirectional.

If you are a Magic Transit customer with egress traffic, refer to Magic Transit egress traffic for more information on the technical aspects you need to consider to create a successful connection to Cloudflare.

Legacy bidirectional health checks

For customers using the legacy health check system with a public IP range, Cloudflare recommends:

  • Configuring the tunnel health check target IP address to one within the 172.64.240.252/30 prefix range.
  • Applying a policy-based route that matches packets ↗︎ with a source IP address equal to the configured tunnel health check target (for example 172.64.240.253/32), and route them over the tunnel back to Cloudflare.

Next steps

Now that you have set up your tunnel endpoints, you need to configure routes to direct your traffic through Cloudflare. You have two routing options:

  • Static routes: Best for simple, stable networks where routes rarely change. You manually define each route.
  • BGP peering: Best for dynamic environments with frequently changing routes, multiple prefixes, or when you need automatic failover. Requires enabling BGP on your tunnel during creation.

Refer to Configure routes for detailed instructions on both options.

Troubleshooting

If you experience issues with your tunnels:

Was this helpful?

YesNo

On this page

Configure tunnels·续 - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/learning-paths/data-center-protection/configure-tunnels-routes/configure-tunnels/#page","headline":"Configure tunnels","description":"Create GRE or IPsec tunnels for Magic Transit.","url":"https://developers.cloudflare.com/learning-paths/data-center-protection/configure-tunnels-routes/configure-tunnels/","inLanguage":"en","image":"https://developers.cloudflare.com/learning-paths/data-center-protection/configure-tunnels-routes/configure-tunnels/og.png?v=71a1376474263190","dateModified":"2026-10-06","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Learn about enable cloudflare network firewall in this guide. title: Enable Cloudflare Network Firewall image: https://developers.cloudflare.com/learning-paths/data-center-protection/enable-network-firewall/og.png?v=3fbfefd26d93b50d


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/learning-paths/llms.txt
Use this file to discover all available pages before exploring further.