Bug #120322 IPv6 detection is insufficient when IPv6 is disabled via sysctl
Submitted: 22 Apr 12:47
Reporter: Huaxiong Song (OCA) Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Components / Services Severity:S3 (Non-critical)
Version: OS:Linux (any kernel with IPv6 module compiled-in but disabled via sysctl)
Assigned to: CPU Architecture:Any

[22 Apr 12:47] Huaxiong Song
Description:
When `bind-address=*` (default), MySQL incorrectly detects IPv6 as available on Linux systems where IPv6 is disabled via `sysctl net.ipv6.conf.all.disable_ipv6=1`. The error log shows `IPv6 is available.`, but clients cannot connect via `::1`.

How to repeat:
1. Disable IPv6 via sysctl:

```bash
sysctl -w net.ipv6.conf.all.disable_ipv6=1
sysctl -w net.ipv6.conf.default.disable_ipv6=1
sysctl -w net.ipv6.conf.lo.disable_ipv6=1
```

2. Start MySQL with default `bind-address=*`.

3. Check the error log — it incorrectly shows:

```
[Note] [MY-010264] IPv6 is available.
```

4. Attempt to connect via IPv6:

```bash
mysql -h ::1 -P 3306 -u root
ERROR 2003 (HY000): Can't connect to MySQL server on '::1:3306' (99)
```

Connection fails — IPv6 is not actually functional.

## Expected Result

MySQL should detect that IPv6 is disabled, log `IPv6 is not available.`, and fall back to binding on `0.0.0.0` (IPv4-only).

## Actual Result

MySQL reports `IPv6 is available` and binds to `::` (dual-stack), but IPv6 connections fail.

## Root Cause

The detection code in `sql/conn_handler/socket_connection.cc` only checks whether `socket(AF_INET6, SOCK_STREAM, 0)` succeeds:

```cpp
const MYSQL_SOCKET s = mysql_socket_socket(0, AF_INET6, SOCK_STREAM, 0);
ipv6_available = mysql_socket_getfd(s) != INVALID_SOCKET;
if (ipv6_available) mysql_socket_close(s);
```

`sysctl disable_ipv6=1` disables IPv6 address assignment and routing but does **not** unload the kernel module, so `socket(AF_INET6)` still succeeds. An additional `bind()` check is needed to verify the IPv6 stack is actually functional.

Suggested fix:
After `socket(AF_INET6)` succeeds, attempt `bind([::]:0)` to verify the stack is usable. See attached patch.
[22 Apr 12:59] Huaxiong Song
Fix

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: fix_ipv6_sysctl_detection.patch (application/octet-stream, text), 2.68 KiB.