wireguard-go/device/mark_unix.go

65 lines
1.1 KiB
Go
Raw Normal View History

2018-12-11 17:21:11 +00:00
// +build android openbsd freebsd
2019-01-02 00:55:51 +00:00
/* SPDX-License-Identifier: MIT
2018-12-11 17:21:11 +00:00
*
2019-01-02 00:55:51 +00:00
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
2018-12-11 17:21:11 +00:00
*/
2019-03-03 03:04:41 +00:00
package device
2018-12-11 17:21:11 +00:00
import (
"golang.org/x/sys/unix"
"runtime"
)
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 */
}
}
func (bind *nativeBind) SetMark(mark uint32) error {
var operr error
2018-12-11 17:21:11 +00:00
if fwmarkIoctl == 0 {
return nil
}
if bind.ipv4 != nil {
fd, err := bind.ipv4.SyscallConn()
if err != nil {
return err
}
err = fd.Control(func(fd uintptr) {
operr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, fwmarkIoctl, int(mark))
2018-12-11 17:21:11 +00:00
})
if err == nil {
err = operr
}
2018-12-11 17:21:11 +00:00
if err != nil {
return err
}
}
if bind.ipv6 != nil {
fd, err := bind.ipv6.SyscallConn()
if err != nil {
return err
}
err = fd.Control(func(fd uintptr) {
operr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, fwmarkIoctl, int(mark))
2018-12-11 17:21:11 +00:00
})
if err == nil {
err = operr
}
2018-12-11 17:21:11 +00:00
if err != nil {
return err
}
}
return nil
}