2026-09-16 16:48:48 +08:00
package forwarding
2026-09-07 18:05:31 +08:00
import (
2026-10-09 17:45:57 +08:00
"context"
2026-09-07 18:05:31 +08:00
"errors"
"fmt"
"os"
2026-09-21 17:12:17 +08:00
"path/filepath"
"sort"
"strconv"
2026-09-07 18:05:31 +08:00
"strings"
2026-10-09 17:45:57 +08:00
"time"
2026-09-07 18:05:31 +08:00
2026-10-09 17:45:57 +08:00
"github.com/1Panel-dev/1Panel/agent/buserr"
2026-09-07 18:05:31 +08:00
"github.com/1Panel-dev/1Panel/agent/constant"
2026-10-09 17:45:57 +08:00
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
"github.com/1Panel-dev/1Panel/agent/utils/docker"
"github.com/docker/docker/api/types/network"
2026-09-07 18:05:31 +08:00
)
2026-10-08 18:19:30 +08:00
func ensureFamilyForwardingSysctls ( system forwardingSystem , family string ) error {
withIPv6 := family == FamilyIPv6
2026-09-21 17:12:17 +08:00
if withIPv6 {
2026-10-09 17:45:57 +08:00
interfaces , err := IPv6RAInterfaces ( system . ReadFile , LoadDockerIPv4BridgePorts )
2026-09-21 17:12:17 +08:00
if err != nil {
2026-10-09 17:45:57 +08:00
global . LOG . Warnf ( "IPv6 forwarding blocked: failed to check Router Advertisement: %v" , err )
return buserr . New ( "ErrIPv6RACheckFailed" )
2026-09-21 17:12:17 +08:00
}
if len ( interfaces ) > 0 {
2026-10-09 17:45:57 +08:00
global . LOG . Warnf ( "IPv6 forwarding blocked: interfaces %s may depend on RA/SLAAC with accept_ra=1; review the network configuration and persist accept_ra=2 on interfaces that require RA before retrying" , strings . Join ( interfaces , ", " ))
return buserr . New ( "ErrIPv6RARisk" )
2026-09-21 17:12:17 +08:00
}
}
2026-09-07 18:05:31 +08:00
paths := [] string { "/proc/sys/net/ipv4/ip_forward" }
if withIPv6 {
2026-10-08 18:19:30 +08:00
paths = [] string { "/proc/sys/net/ipv6/conf/all/forwarding" }
2026-09-07 18:05:31 +08:00
}
for _ , path := range paths {
if err := system . WriteFile ( path , [] byte ( "1" ), constant . FilePerm ); err != nil {
return fmt . Errorf ( "failed to enable IP forwarding at %s: %w" , path , err )
}
}
data , err := system . ReadFile ( "/etc/sysctl.conf" )
if err != nil && ! errors . Is ( err , os . ErrNotExist ) {
return fmt . Errorf ( "failed to read /etc/sysctl.conf: %w" , err )
}
2026-10-08 18:19:30 +08:00
content := enableFamilyForwardingSysctls ( string ( data ), family )
2026-09-07 18:05:31 +08:00
if err := system . WriteFile ( "/etc/sysctl.conf" , [] byte ( content ), constant . FilePerm ); err != nil {
return fmt . Errorf ( "failed to persist IP forwarding: %w" , err )
}
return nil
}
2026-10-09 17:45:57 +08:00
func IPv6RAInterfaces ( readFile func ( string ) ([] byte , error ), loadDockerPorts func () ( map [ string ] bool , error )) ([] string , error ) {
2026-09-21 17:12:17 +08:00
addresses , err := readFile ( "/proc/net/if_inet6" )
if err != nil {
return nil , err
}
const permanentAddress = 0x80
globalAddresses := make ( map [ string ] bool )
raInterfaces := make ( map [ string ] bool )
for _ , line := range strings . Split ( string ( addresses ), "\n" ) {
fields := strings . Fields ( line )
if len ( fields ) == 0 {
continue
}
if len ( fields ) != 6 {
return nil , fmt . Errorf ( "invalid IPv6 address entry: %q" , line )
}
name := fields [ 5 ]
if name == "lo" {
continue
}
flags , err := strconv . ParseUint ( fields [ 4 ], 16 , 32 )
if err != nil {
return nil , err
}
2026-10-09 17:45:57 +08:00
globalAddresses [ name ] = globalAddresses [ name ] || fields [ 3 ] == "00"
if fields [ 3 ] == "00" && flags & permanentAddress == 0 {
raInterfaces [ name ] = true
2026-09-21 17:12:17 +08:00
}
}
routes , err := readFile ( "/proc/net/ipv6_route" )
if err != nil {
return nil , err
}
2026-10-09 17:45:57 +08:00
const raRouteFlags = 0x00010000 | 0x00080000 | 0x00800000
2026-09-21 17:12:17 +08:00
for _ , line := range strings . Split ( string ( routes ), "\n" ) {
fields := strings . Fields ( line )
if len ( fields ) == 0 {
continue
}
if len ( fields ) != 10 {
return nil , fmt . Errorf ( "invalid IPv6 route entry: %q" , line )
}
flags , err := strconv . ParseUint ( fields [ 8 ], 16 , 32 )
if err != nil {
return nil , err
}
if flags & raRouteFlags != 0 {
raInterfaces [ fields [ 9 ]] = true
}
}
2026-10-09 17:45:57 +08:00
uncertainInterfaces := make ( map [ string ] bool )
2026-09-21 17:12:17 +08:00
for name , hasGlobalAddress := range globalAddresses {
2026-10-09 17:45:57 +08:00
if ! hasGlobalAddress && ! raInterfaces [ name ] {
2026-09-21 17:12:17 +08:00
raInterfaces [ name ] = true
2026-10-09 17:45:57 +08:00
uncertainInterfaces [ name ] = true
2026-09-21 17:12:17 +08:00
}
}
var interfaces [] string
2026-10-09 17:45:57 +08:00
checkDockerPorts := false
2026-09-21 17:12:17 +08:00
for name := range raInterfaces {
if name == "lo" {
continue
}
if name == "." || name == ".." || filepath . Base ( name ) != name {
return nil , fmt . Errorf ( "invalid IPv6 interface %q" , name )
}
value , err := readFile ( "/proc/sys/net/ipv6/conf/" + name + "/accept_ra" )
if err != nil {
return nil , err
}
switch strings . TrimSpace ( string ( value )) {
case "1" :
interfaces = append ( interfaces , name )
2026-10-09 17:45:57 +08:00
checkDockerPorts = checkDockerPorts || uncertainInterfaces [ name ]
2026-09-21 17:12:17 +08:00
case "0" , "2" :
default :
return nil , fmt . Errorf ( "invalid accept_ra value for %s" , name )
}
}
2026-10-09 17:45:57 +08:00
if checkDockerPorts {
ports , err := loadDockerPorts ()
if err != nil {
global . LOG . Warnf ( "IPv6 forwarding RA check: cannot exclude Docker bridge ports: %v" , err )
} else {
kept := interfaces [: 0 ]
for _ , name := range interfaces {
if ! uncertainInterfaces [ name ] || ! ports [ name ] {
kept = append ( kept , name )
}
}
interfaces = kept
}
}
2026-09-21 17:12:17 +08:00
sort . Strings ( interfaces )
return interfaces , nil
}
2026-10-09 17:45:57 +08:00
func LoadDockerIPv4BridgePorts () ( map [ string ] bool , error ) {
cli , err := docker . NewDockerClient ()
if err != nil {
return nil , err
}
defer cli . Close ()
if ! strings . HasPrefix ( cli . DaemonHost (), "unix://" ) {
return nil , errors . New ( "cannot verify local bridge ports using a remote Docker daemon" )
}
ctx , cancel := context . WithTimeout ( context . Background (), 5 * time . Second )
defer cancel ()
info , err := cli . Info ( ctx )
if err != nil {
return nil , err
}
for _ , option := range info . SecurityOptions {
if option == "name=rootless" {
return nil , errors . New ( "cannot verify host bridge ports using a rootless Docker daemon" )
}
}
networks , err := cli . NetworkList ( ctx , network . ListOptions {})
if err != nil {
return nil , err
}
2026-10-14 15:57:07 +08:00
command := cmd . NewCommandMgr ( cmd . WithContext ( ctx ), cmd . WithTimeout ( 5 * time . Second ))
bridges , err := command . RunWithOptionalSudoAndStdout ( "ip" , "-o" , "link" , "show" , "type" , "bridge" )
2026-10-09 17:45:57 +08:00
if err != nil {
return nil , err
}
2026-10-14 15:57:07 +08:00
ports , err := command . RunWithOptionalSudoAndStdout ( "ip" , "-o" , "link" , "show" , "type" , "veth" )
if err != nil {
return nil , err
}
return dockerIPv4BridgePorts ( networks , bridges , ports )
2026-10-09 17:45:57 +08:00
}
2026-10-14 15:57:07 +08:00
func dockerIPv4BridgePorts ( networks [] network . Summary , bridgeOutput , portOutput string ) ( map [ string ] bool , error ) {
2026-10-09 17:45:57 +08:00
bridges := make ( map [ string ] bool )
for _ , item := range networks {
if item . Driver != "bridge" || item . EnableIPv6 || item . ConfigOnly {
continue
}
name := item . Options [ "com.docker.network.bridge.name" ]
if name == "" {
if item . Options [ "com.docker.network.bridge.default_bridge" ] == "true" {
name = "docker0"
} else if len ( item . ID ) >= 12 {
name = "br-" + item . ID [: 12 ]
}
}
if name != "" {
bridges [ name ] = true
}
}
confirmedBridges := make ( map [ string ] bool )
ports := make ( map [ string ] bool )
2026-10-14 15:57:07 +08:00
for index , output := range [] string { bridgeOutput , portOutput } {
for _ , line := range strings . Split ( output , "\n" ) {
header , _ , _ := strings . Cut ( line , "\\" )
fields := strings . Fields ( header )
if len ( fields ) == 0 {
continue
}
if len ( fields ) < 3 || ! strings . HasSuffix ( fields [ 0 ], ":" ) || ! strings . HasSuffix ( fields [ 1 ], ":" ) {
return nil , fmt . Errorf ( "invalid network link entry: %q" , line )
}
if _ , err := strconv . Atoi ( strings . TrimSuffix ( fields [ 0 ], ":" )); err != nil {
return nil , fmt . Errorf ( "invalid network link entry: %q" , line )
}
name , _ , _ := strings . Cut ( strings . TrimSuffix ( fields [ 1 ], ":" ), "@" )
if index == 0 {
if bridges [ name ] {
confirmedBridges [ name ] = true
}
continue
}
for i := 3 ; i + 1 < len ( fields ); i ++ {
if fields [ i ] == "master" {
if confirmedBridges [ fields [ i + 1 ]] {
ports [ name ] = true
}
break
}
}
2026-10-09 17:45:57 +08:00
}
}
return ports , nil
}
2026-09-21 17:12:17 +08:00
2026-10-08 18:19:30 +08:00
func enableFamilyForwardingSysctls ( content , family string ) string {
2026-09-07 18:05:31 +08:00
lines := strings . Split ( strings . TrimRight ( content , "\n" ), "\n" )
wanted := map [ string ] string { "net.ipv4.ip_forward" : "net.ipv4.ip_forward = 1" }
2026-10-08 18:19:30 +08:00
if family == FamilyIPv6 {
wanted = map [ string ] string { "net.ipv6.conf.all.forwarding" : "net.ipv6.conf.all.forwarding = 1" }
2026-09-07 18:05:31 +08:00
}
found := make ( map [ string ] bool , len ( wanted ))
for index , line := range lines {
trimmed := strings . TrimSpace ( line )
if strings . HasPrefix ( trimmed , "#" ) {
continue
}
parts := strings . SplitN ( trimmed , "=" , 2 )
if len ( parts ) != 2 {
continue
}
key := strings . TrimSpace ( parts [ 0 ])
if replacement , ok := wanted [ key ]; ok {
lines [ index ] = replacement
found [ key ] = true
}
}
for _ , key := range [] string { "net.ipv4.ip_forward" , "net.ipv6.conf.all.forwarding" } {
if replacement , ok := wanted [ key ]; ok && ! found [ key ] {
lines = append ( lines , replacement )
}
}
if len ( lines ) > 0 && lines [ 0 ] == "" {
lines = lines [ 1 :]
}
return strings . Join ( lines , "\n" ) + "\n"
}