From c967f15e443a5953429000e50fad2e117992b72d Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Tue, 11 Dec 2018 18:21:11 +0100 Subject: Separate out mark setting for Windows --- mark_unix.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 mark_unix.go (limited to 'mark_unix.go') diff --git a/mark_unix.go b/mark_unix.go new file mode 100644 index 0000000..0b9d7e0 --- /dev/null +++ b/mark_unix.go @@ -0,0 +1,57 @@ +// +build android openbsd freebsd + +/* SPDX-License-Identifier: GPL-2.0 + * + * Copyright (C) 2017-2018 WireGuard LLC. All Rights Reserved. + */ + +package main + +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 { + 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) { + err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, fwmarkIoctl, int(mark)) + }) + 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) { + err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, fwmarkIoctl, int(mark)) + }) + if err != nil { + return err + } + } + return nil +} -- cgit v1.2.3