2021-09-05 16:00:43 +02:00
|
|
|
//go:build linux || openbsd || freebsd
|
2018-12-11 18:21:11 +01:00
|
|
|
|
2019-01-02 01:55:51 +01:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-12-11 18:21:11 +01:00
|
|
|
*
|
2022-09-20 17:21:32 +02:00
|
|
|
* Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
|
2018-12-11 18:21:11 +01:00
|
|
|
*/
|
|
|
|
|
2019-11-07 17:13:05 +01:00
|
|
|
package conn
|
2018-12-11 18:21:11 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
2019-05-14 09:09:52 +02:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2018-12-11 18:21:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var fwmarkIoctl int
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux", "android":
|
|
|
|
fwmarkIoctl = 36 /* unix.SO_MARK */
|
|
|
|
case "freebsd":
|
|
|
|
fwmarkIoctl = 0x1015 /* unix.SO_USER_COOKIE */
|
|
|
|
case "openbsd":
|
|
|
|
fwmarkIoctl = 0x1021 /* unix.SO_RTABLE */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 00:08:28 +01:00
|
|
|
func (s *StdNetBind) SetMark(mark uint32) error {
|
2018-12-25 22:56:36 +01:00
|
|
|
var operr error
|
2018-12-11 18:21:11 +01:00
|
|
|
if fwmarkIoctl == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-03 00:08:28 +01:00
|
|
|
if s.ipv4 != nil {
|
|
|
|
fd, err := s.ipv4.SyscallConn()
|
2018-12-11 18:21:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = fd.Control(func(fd uintptr) {
|
2018-12-25 22:56:36 +01:00
|
|
|
operr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, fwmarkIoctl, int(mark))
|
2018-12-11 18:21:11 +01:00
|
|
|
})
|
2018-12-25 22:56:36 +01:00
|
|
|
if err == nil {
|
|
|
|
err = operr
|
|
|
|
}
|
2018-12-11 18:21:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-03-03 00:08:28 +01:00
|
|
|
if s.ipv6 != nil {
|
|
|
|
fd, err := s.ipv6.SyscallConn()
|
2018-12-11 18:21:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = fd.Control(func(fd uintptr) {
|
2018-12-25 22:56:36 +01:00
|
|
|
operr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, fwmarkIoctl, int(mark))
|
2018-12-11 18:21:11 +01:00
|
|
|
})
|
2018-12-25 22:56:36 +01:00
|
|
|
if err == nil {
|
|
|
|
err = operr
|
|
|
|
}
|
2018-12-11 18:21:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|