Add GlobalProtect NLB lifecycle (Phase 5)

Refresh tunnel-opaque via targeted getconfig.esp when valid-period nears
expiry, without disturbing ESP keys or routes. Send periodic NLB ESP
keepalives on established sessions and reset keepalive state after refresh.
This commit is contained in:
Kevin Yue committed 2026-06-25 18:41:22 +08:00
1 parent 81ccdc6358
commit 5fcef8c5ba
1 file changed
+307
@@ -0,0 +1,307 @@
From 0000000000000000000000000000000000000005 Mon Sep 17 00:00:00 2001
From: Kevin Yue <[email protected]>
Date: Wed, 25 Jun 2026 21:00:00 +0800
Subject: [PATCH 11/11] Implement GlobalProtect NLB lifecycle
Refresh tunnel-opaque via a targeted getconfig.esp when valid-period
expires, and send periodic NLB ESP keepalives during established sessions.
---
--- a/gpst.c 2026-06-25 18:39:37
+++ b/gpst.c 2026-06-25 18:40:12
@@ -699,19 +699,12 @@
return ret;
}
-static int gpst_get_config(struct openconnect_info *vpninfo)
+static int gpst_build_getconfig_request(struct openconnect_info *vpninfo,
+ struct oc_text_buf *request_body)
{
- char *orig_path;
- int result;
- struct oc_text_buf *request_body = buf_alloc();
const char *old_addr = vpninfo->ip_info.addr;
const char *old_addr6 = vpninfo->ip_info.addr6;
- char *xml_buf = NULL;
-
-
- /* submit getconfig request */
const char *app_version = openconnect_get_gp_app_version(vpninfo);
- vpn_progress(vpninfo, PRG_DEBUG, _("Using GlobalProtect app version: %s\n"), app_version);
buf_append(request_body, "client-type=1&protocol-version=p1&internal=no");
append_opt(request_body, "app-version", app_version);
@@ -730,8 +723,99 @@
} else
buf_append(request_body, "&%s", vpninfo->cookie);
filter_opts(request_body, vpninfo->cookie, "serialno,clientgpversion,host-id", 1);
- if ((result = buf_error(request_body)))
+ return buf_error(request_body);
+}
+
+static int gpst_parse_nlb_opaque_xml(struct openconnect_info *vpninfo, xmlNode *xml_node, void *cb_data)
+{
+ char *s = NULL;
+
+ for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next) {
+ if (xmlnode_is_named(xml_node, "hs-key")) {
+#ifdef HAVE_ESP
+ int keylen = xml_to_key(xml_node, vpninfo->gp_nlb.hs_key, sizeof(vpninfo->gp_nlb.hs_key));
+
+ if (keylen > 0) {
+ vpninfo->gp_nlb.hs_key_len = keylen;
+ vpninfo->gp_nlb.hs_key_bits = keylen * 8;
+ vpninfo->gp_nlb.enabled = 1;
+ }
+#endif /* HAVE_ESP */
+ } else if (!xmlnode_get_val(xml_node, "enc-hs-key", &s)) {
+ free(vpninfo->gp_nlb.enc_hs_key);
+ vpninfo->gp_nlb.enc_hs_key = s;
+ s = NULL;
+ vpninfo->gp_nlb.enabled = 1;
+ } else if (xmlnode_is_named(xml_node, "tunnel-opaque")) {
+ if (!xmlnode_get_val(xml_node, "val", &s)) {
+ free(vpninfo->gp_nlb.tunnel_opaque);
+ vpninfo->gp_nlb.tunnel_opaque = s;
+ s = NULL;
+ vpninfo->gp_nlb.enabled = 1;
+ }
+ if (!xmlnode_get_val(xml_node, "valid-period", &s))
+ vpninfo->gp_nlb.opaque_valid_until = time(NULL) + atol(s);
+ } else if (!xmlnode_get_val(xml_node, "valid-period", &s)) {
+ vpninfo->gp_nlb.opaque_valid_until = time(NULL) + atol(s);
+ }
+ free(s);
+ s = NULL;
+ }
+ return 0;
+}
+
+int gpst_nlb_refresh_opaque(struct openconnect_info *vpninfo)
+{
+ char *orig_path, *xml_buf = NULL;
+ struct oc_text_buf *request_body = buf_alloc();
+ int result;
+
+ if (!vpninfo->gp_nlb.enabled)
+ return 0;
+
+ if (gpst_build_getconfig_request(vpninfo, request_body)) {
+ buf_free(request_body);
+ return -ENOMEM;
+ }
+
+ orig_path = vpninfo->urlpath;
+ vpninfo->urlpath = strdup("ssl-vpn/getconfig.esp");
+ result = do_https_request(vpninfo, "POST", "application/x-www-form-urlencoded",
+ request_body, &xml_buf, NULL, HTTP_NO_FLAGS);
+ free(vpninfo->urlpath);
+ vpninfo->urlpath = orig_path;
+ buf_free(request_body);
+
+ if (result < 0)
+ return result;
+
+ if (result >= 0 && xml_buf)
+ vpn_progress(vpninfo, PRG_DEBUG,
+ _("GlobalProtect NLB opaque refresh response:\n%s\n"),
+ xml_buf);
+
+ result = gpst_xml_or_error(vpninfo, xml_buf, gpst_parse_nlb_opaque_xml, NULL, NULL);
+ free(xml_buf);
+ if (result)
+ return result;
+
+ return gpst_nlb_prepare(vpninfo);
+}
+
+static int gpst_get_config(struct openconnect_info *vpninfo)
+{
+ char *orig_path;
+ int result;
+ struct oc_text_buf *request_body = buf_alloc();
+ char *xml_buf = NULL;
+
+ vpn_progress(vpninfo, PRG_DEBUG, _("Using GlobalProtect app version: %s\n"),
+ openconnect_get_gp_app_version(vpninfo));
+
+ if (gpst_build_getconfig_request(vpninfo, request_body)) {
+ result = -ENOMEM;
goto out;
+ }
orig_path = vpninfo->urlpath;
vpninfo->urlpath = strdup("ssl-vpn/getconfig.esp");
@@ -1239,6 +1323,8 @@
/* fall through */
case DTLS_ESTABLISHED:
/* Rekey or check-and-resubmit HIP if needed */
+ if (gpst_nlb_maintenance(vpninfo, timeout) > 0)
+ return 1;
if (keepalive_action(&vpninfo->ssl_times, timeout) == KA_REKEY)
goto do_rekey;
else if (trojan_check_deadline(vpninfo, timeout))
@@ -1269,6 +1355,9 @@
;
}
+ if (gpst_nlb_maintenance(vpninfo, timeout) > 0)
+ work_done = 1;
+
if (vpninfo->ssl_fd == -1)
goto do_reconnect;
--- a/gpst_nlb.c 2026-06-25 18:39:37
+++ b/gpst_nlb.c 2026-06-25 18:40:26
@@ -69,6 +69,7 @@
"\x10\x11\x12\x13\x14\x15\x16\x17";
#define GP_NLB_SSL_PROTO_VERSION 110
+#define GP_NLB_OPAQUE_REFRESH_MARGIN 60
static int gp_nlb_is_ipv4(const char *s)
{
@@ -469,8 +470,39 @@
{
gpst_nlb_update_esp_magic(vpninfo);
return gpst_nlb_apply_routes(vpninfo);
+}
+
+int gpst_nlb_opaque_due(struct openconnect_info *vpninfo, int *timeout)
+{
+ struct gp_nlb_config *nlb = &vpninfo->gp_nlb;
+ time_t now = time(NULL);
+ time_t due;
+
+ if (!nlb->enabled || !nlb->tunnel_opaque || !nlb->opaque_valid_until)
+ return 0;
+
+ due = nlb->opaque_valid_until;
+ if (due > GP_NLB_OPAQUE_REFRESH_MARGIN)
+ due -= GP_NLB_OPAQUE_REFRESH_MARGIN;
+
+ return ka_check_deadline(timeout, now, due);
}
+int gpst_nlb_maintenance(struct openconnect_info *vpninfo, int *timeout)
+{
+ if (!gpst_nlb_opaque_due(vpninfo, timeout))
+ return 0;
+
+ vpn_progress(vpninfo, PRG_INFO, _("NLB tunnel-opaque refresh due\n"));
+ if (gpst_nlb_refresh_opaque(vpninfo) < 0) {
+ vpn_progress(vpninfo, PRG_ERR, _("NLB tunnel-opaque refresh failed\n"));
+ return -1;
+ }
+
+ vpninfo->gp_nlb.keepalive_sent = 0;
+ return 1;
+}
+
int gpst_nlb_handle_ssl_connect_response(struct openconnect_info *vpninfo,
const char *buf, int len)
{
--- a/esp.c 2026-06-25 18:39:37
+++ b/esp.c 2026-06-25 18:40:26
@@ -327,9 +327,12 @@
vpninfo->dtls_times.last_tx = now;
}
+
+ if (vpninfo->proto->proto == PROTO_GPST && gpst_nlb_maintenance(vpninfo, timeout) > 0)
+ work_done = 1;
if (vpninfo->dtls_state != DTLS_ESTABLISHED)
- return 0;
+ return work_done;
switch (keepalive_action(&vpninfo->dtls_times, timeout)) {
case KA_REKEY:
@@ -350,7 +353,13 @@
break;
case KA_KEEPALIVE:
- vpn_progress(vpninfo, PRG_ERR, _("Keepalive not implemented for ESP\n"));
+ if (vpninfo->proto->proto == PROTO_GPST && vpninfo->gp_nlb.enabled) {
+ if (gpst_nlb_send_esp_keepalive(vpninfo) >= 0)
+ vpninfo->dtls_times.last_tx = time(NULL);
+ work_done = 1;
+ } else {
+ vpn_progress(vpninfo, PRG_ERR, _("Keepalive not implemented for ESP\n"));
+ }
break;
case KA_NONE:
--- a/openconnect-internal.h 2026-06-25 18:39:37
+++ b/openconnect-internal.h 2026-06-25 18:40:26
@@ -1434,6 +1434,9 @@
int gpst_nlb_on_ssl_tunnel(struct openconnect_info *vpninfo);
int gpst_nlb_handle_ssl_connect_response(struct openconnect_info *vpninfo,
const char *buf, int len);
+int gpst_nlb_refresh_opaque(struct openconnect_info *vpninfo);
+int gpst_nlb_opaque_due(struct openconnect_info *vpninfo, int *timeout);
+int gpst_nlb_maintenance(struct openconnect_info *vpninfo, int *timeout);
int gpst_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable);
int gpst_esp_send_probes(struct openconnect_info *vpninfo);
int gpst_esp_catch_probe(struct openconnect_info *vpninfo, struct pkt *pkt);
--- a/tests/gp-nlb-opaque-due 2026-06-25 18:40:45
+++ b/tests/gp-nlb-opaque-due 2026-06-25 18:40:37
@@ -0,0 +1,58 @@
+#!/bin/sh
+# Validate NLB opaque refresh deadline calculation
+set -eu
+cd "$(dirname "$0")/.."
+CC="${CC:-cc}"
+TMP=$(mktemp -d)
+trap 'rm -rf "$TMP"' EXIT
+
+cat > "$TMP/test.c" <<'EOF'
+#include <stdio.h>
+#include <time.h>
+
+#define GP_NLB_OPAQUE_REFRESH_MARGIN 60
+
+static int ka_check_deadline(int *timeout, time_t now, time_t due)
+{
+ if (now >= due)
+ return 1;
+ if (*timeout > (due - now) * 1000)
+ *timeout = (due - now) * 1000;
+ return 0;
+}
+
+static int gpst_nlb_opaque_due(int enabled, time_t valid_until, int *timeout)
+{
+ time_t now = time(NULL);
+ time_t due;
+
+ if (!enabled || !valid_until)
+ return 0;
+
+ due = valid_until;
+ if (due > GP_NLB_OPAQUE_REFRESH_MARGIN)
+ due -= GP_NLB_OPAQUE_REFRESH_MARGIN;
+
+ return ka_check_deadline(timeout, now, due);
+}
+
+int main(void)
+{
+ int timeout = 999999;
+ time_t now = time(NULL);
+
+ if (gpst_nlb_opaque_due(0, now + 3600, &timeout))
+ return 1;
+ if (gpst_nlb_opaque_due(1, 0, &timeout))
+ return 1;
+ if (!gpst_nlb_opaque_due(1, now + 30, &timeout))
+ return 1;
+ timeout = 999999;
+ if (gpst_nlb_opaque_due(1, now + 3600, &timeout))
+ return 1;
+ printf("gp-nlb-opaque-due: OK\n");
+ return 0;
+}
+EOF
+$CC -o "$TMP/test" "$TMP/test.c"
+"$TMP/test"
\ No newline at end of file