summaryrefslogtreecommitdiffhomepage
path: root/conn/mark_unix.go
diff options
context:
space:
mode:
authorDavid Crawshaw <crawshaw@tailscale.com>2019-11-07 11:13:05 -0500
committerJason A. Donenfeld <Jason@zx2c4.com>2020-05-02 01:46:42 -0600
commit203554620dc8114de1ff70bb30b80f828e9e26ad (patch)
tree49f36961f2090dc07d15cad3204a1a3531dc233d /conn/mark_unix.go
parent6aefb61355c9da539383dd0c2bc5f2bb3dbb3963 (diff)
conn: introduce new package that splits out the Bind and Endpoint types
The sticky socket code stays in the device package for now, as it reaches deeply into the peer list. This is the first step in an effort to split some code out of the very busy device package. Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
Diffstat (limited to 'conn/mark_unix.go')
-rw-r--r--conn/mark_unix.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/conn/mark_unix.go b/conn/mark_unix.go
new file mode 100644
index 0000000..5334582
--- /dev/null
+++ b/conn/mark_unix.go
@@ -0,0 +1,65 @@
+// +build android openbsd freebsd
+
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ */
+
+package conn
+
+import (
+ "runtime"
+
+ "golang.org/x/sys/unix"
+)
+
+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
+ 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))
+ })
+ if err == nil {
+ err = operr
+ }
+ 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))
+ })
+ if err == nil {
+ err = operr
+ }
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+}