syscall: add LSF support for linux

R=rsc
CC=golang-dev
https://golang.org/cl/4626041
This commit is contained in:
Mikio Hara 2011-06-20 14:51:34 -04:00 committed by Russ Cox
parent 58ff8c1dfd
commit 10f1436bad
10 changed files with 447 additions and 1 deletions

View File

@ -32,6 +32,7 @@ GOFILES_darwin=\
GOFILES_linux=\ GOFILES_linux=\
exec_unix.go\ exec_unix.go\
lsf_linux.go\
netlink_linux.go\ netlink_linux.go\
syscall_unix.go\ syscall_unix.go\

View File

@ -0,0 +1,78 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Linux socket filter
package syscall
import (
"unsafe"
)
func LsfStmt(code, k int) *SockFilter {
return &SockFilter{Code: uint16(code), K: uint32(k)}
}
func LsfJump(code, k, jt, jf int) *SockFilter {
return &SockFilter{Code: uint16(code), Jt: uint8(jt), Jf: uint8(jf), K: uint32(k)}
}
func LsfSocket(ifindex, proto int) (int, int) {
var lsall SockaddrLinklayer
s, e := Socket(AF_PACKET, SOCK_RAW, proto)
if e != 0 {
return 0, e
}
p := (*[2]byte)(unsafe.Pointer(&lsall.Protocol))
p[0] = byte(proto >> 8)
p[1] = byte(proto)
lsall.Ifindex = ifindex
e = Bind(s, &lsall)
if e != 0 {
Close(s)
return 0, e
}
return s, 0
}
type iflags struct {
name [IFNAMSIZ]byte
flags uint16
}
func SetLsfPromisc(name string, m bool) int {
s, e := Socket(AF_INET, SOCK_DGRAM, 0)
if e != 0 {
return e
}
defer Close(s)
var ifl iflags
copy(ifl.name[:], []byte(name))
_, _, ep := Syscall(SYS_IOCTL, uintptr(s), SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
if e := int(ep); e != 0 {
return e
}
if m {
ifl.flags |= uint16(IFF_PROMISC)
} else {
ifl.flags &= ^uint16(IFF_PROMISC)
}
_, _, ep = Syscall(SYS_IOCTL, uintptr(s), SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
if e := int(ep); e != 0 {
return e
}
return 0
}
func AttachLsf(fd int, i []SockFilter) int {
var p SockFprog
p.Len = uint16(len(i))
p.Filter = (*SockFilter)(unsafe.Pointer(&i[0]))
return setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, uintptr(unsafe.Pointer(&p)), unsafe.Sizeof(p))
}
func DetachLsf(fd int) int {
var dummy int
return setsockopt(fd, SOL_SOCKET, SO_DETACH_FILTER, uintptr(unsafe.Pointer(&dummy)), unsafe.Sizeof(dummy))
}

View File

@ -30,7 +30,9 @@ includes_Linux='
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <linux/if_addr.h> #include <linux/if_addr.h>
#include <linux/if_ether.h>
#include <linux/if_tun.h> #include <linux/if_tun.h>
#include <linux/filter.h>
#include <linux/netlink.h> #include <linux/netlink.h>
#include <linux/reboot.h> #include <linux/reboot.h>
#include <linux/rtnetlink.h> #include <linux/rtnetlink.h>
@ -141,7 +143,7 @@ done
$2 ~ /^LINUX_REBOOT_CMD_/ || $2 ~ /^LINUX_REBOOT_CMD_/ ||
$2 ~ /^LINUX_REBOOT_MAGIC[12]$/ || $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
$2 !~ "NLA_TYPE_MASK" && $2 !~ "NLA_TYPE_MASK" &&
$2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|RTM|RTN|RTPROT|RTA|RTAX|RTNH|ARPHRD)_/ || $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|RTM|RTN|RTPROT|RTA|RTAX|RTNH|ARPHRD|ETH_P)_/ ||
$2 ~ /^SIOC/ || $2 ~ /^SIOC/ ||
$2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ || $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
$2 ~ /^BIOC/ || $2 ~ /^BIOC/ ||

View File

@ -38,6 +38,7 @@ Input to godefs. See also mkerrors.sh and mkall.sh
#include <sys/user.h> #include <sys/user.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <linux/filter.h>
#include <linux/netlink.h> #include <linux/netlink.h>
#include <linux/rtnetlink.h> #include <linux/rtnetlink.h>
#include <time.h> #include <time.h>
@ -225,6 +226,16 @@ typedef struct ifaddrmsg $IfAddrmsg;
typedef struct rtmsg $RtMsg; typedef struct rtmsg $RtMsg;
typedef struct rtnexthop $RtNexthop; typedef struct rtnexthop $RtNexthop;
// Linux socket filter
enum {
$SizeofSockFilter = sizeof(struct sock_filter),
$SizeofSockFprog = sizeof(struct sock_fprog),
};
typedef struct sock_filter $SockFilter;
typedef struct sock_fprog $SockFprog;
// Inotify // Inotify
typedef struct inotify_event $InotifyEvent; typedef struct inotify_event $InotifyEvent;

View File

@ -106,6 +106,46 @@ const (
ARPHRD_TUNNEL6 = 0x301 ARPHRD_TUNNEL6 = 0x301
ARPHRD_VOID = 0xffff ARPHRD_VOID = 0xffff
ARPHRD_X25 = 0x10f ARPHRD_X25 = 0x10f
BPF_A = 0x10
BPF_ABS = 0x20
BPF_ADD = 0
BPF_ALU = 0x4
BPF_AND = 0x50
BPF_B = 0x10
BPF_DIV = 0x30
BPF_H = 0x8
BPF_IMM = 0
BPF_IND = 0x40
BPF_JA = 0
BPF_JEQ = 0x10
BPF_JGE = 0x30
BPF_JGT = 0x20
BPF_JMP = 0x5
BPF_JSET = 0x40
BPF_K = 0
BPF_LD = 0
BPF_LDX = 0x1
BPF_LEN = 0x80
BPF_LSH = 0x60
BPF_MAJOR_VERSION = 0x1
BPF_MAXINSNS = 0x1000
BPF_MEM = 0x60
BPF_MEMWORDS = 0x10
BPF_MINOR_VERSION = 0x1
BPF_MISC = 0x7
BPF_MSH = 0xa0
BPF_MUL = 0x20
BPF_NEG = 0x80
BPF_OR = 0x40
BPF_RET = 0x6
BPF_RSH = 0x70
BPF_ST = 0x2
BPF_STX = 0x3
BPF_SUB = 0x10
BPF_TAX = 0
BPF_TXA = 0x80
BPF_W = 0
BPF_X = 0x8
DT_BLK = 0x6 DT_BLK = 0x6
DT_CHR = 0x2 DT_CHR = 0x2
DT_DIR = 0x4 DT_DIR = 0x4
@ -256,6 +296,68 @@ const (
ESRMNT = 0x45 ESRMNT = 0x45
ESTALE = 0x74 ESTALE = 0x74
ESTRPIPE = 0x56 ESTRPIPE = 0x56
ETH_P_1588 = 0x88f7
ETH_P_8021Q = 0x8100
ETH_P_802_2 = 0x4
ETH_P_802_3 = 0x1
ETH_P_AARP = 0x80f3
ETH_P_ALL = 0x3
ETH_P_AOE = 0x88a2
ETH_P_ARCNET = 0x1a
ETH_P_ARP = 0x806
ETH_P_ATALK = 0x809b
ETH_P_ATMFATE = 0x8884
ETH_P_ATMMPOA = 0x884c
ETH_P_AX25 = 0x2
ETH_P_BPQ = 0x8ff
ETH_P_CAN = 0xc
ETH_P_CONTROL = 0x16
ETH_P_CUST = 0x6006
ETH_P_DDCMP = 0x6
ETH_P_DEC = 0x6000
ETH_P_DIAG = 0x6005
ETH_P_DNA_DL = 0x6001
ETH_P_DNA_RC = 0x6002
ETH_P_DNA_RT = 0x6003
ETH_P_DSA = 0x1b
ETH_P_ECONET = 0x18
ETH_P_EDSA = 0xdada
ETH_P_FCOE = 0x8906
ETH_P_FIP = 0x8914
ETH_P_HDLC = 0x19
ETH_P_IEEE802154 = 0xf6
ETH_P_IEEEPUP = 0xa00
ETH_P_IEEEPUPAT = 0xa01
ETH_P_IP = 0x800
ETH_P_IPV6 = 0x86dd
ETH_P_IPX = 0x8137
ETH_P_IRDA = 0x17
ETH_P_LAT = 0x6004
ETH_P_LOCALTALK = 0x9
ETH_P_LOOP = 0x60
ETH_P_MOBITEX = 0x15
ETH_P_MPLS_MC = 0x8848
ETH_P_MPLS_UC = 0x8847
ETH_P_PAE = 0x888e
ETH_P_PAUSE = 0x8808
ETH_P_PHONET = 0xf5
ETH_P_PPPTALK = 0x10
ETH_P_PPP_DISC = 0x8863
ETH_P_PPP_MP = 0x8
ETH_P_PPP_SES = 0x8864
ETH_P_PUP = 0x200
ETH_P_PUPAT = 0x201
ETH_P_RARP = 0x8035
ETH_P_SCA = 0x6007
ETH_P_SLOW = 0x8809
ETH_P_SNAP = 0x5
ETH_P_TEB = 0x6558
ETH_P_TIPC = 0x88ca
ETH_P_TRAILER = 0x1c
ETH_P_TR_802_2 = 0x11
ETH_P_WAN_PPP = 0x7
ETH_P_WCCP = 0x883e
ETH_P_X25 = 0x805
ETIME = 0x3e ETIME = 0x3e
ETIMEDOUT = 0x6e ETIMEDOUT = 0x6e
ETOOMANYREFS = 0x6d ETOOMANYREFS = 0x6d

View File

@ -106,6 +106,46 @@ const (
ARPHRD_TUNNEL6 = 0x301 ARPHRD_TUNNEL6 = 0x301
ARPHRD_VOID = 0xffff ARPHRD_VOID = 0xffff
ARPHRD_X25 = 0x10f ARPHRD_X25 = 0x10f
BPF_A = 0x10
BPF_ABS = 0x20
BPF_ADD = 0
BPF_ALU = 0x4
BPF_AND = 0x50
BPF_B = 0x10
BPF_DIV = 0x30
BPF_H = 0x8
BPF_IMM = 0
BPF_IND = 0x40
BPF_JA = 0
BPF_JEQ = 0x10
BPF_JGE = 0x30
BPF_JGT = 0x20
BPF_JMP = 0x5
BPF_JSET = 0x40
BPF_K = 0
BPF_LD = 0
BPF_LDX = 0x1
BPF_LEN = 0x80
BPF_LSH = 0x60
BPF_MAJOR_VERSION = 0x1
BPF_MAXINSNS = 0x1000
BPF_MEM = 0x60
BPF_MEMWORDS = 0x10
BPF_MINOR_VERSION = 0x1
BPF_MISC = 0x7
BPF_MSH = 0xa0
BPF_MUL = 0x20
BPF_NEG = 0x80
BPF_OR = 0x40
BPF_RET = 0x6
BPF_RSH = 0x70
BPF_ST = 0x2
BPF_STX = 0x3
BPF_SUB = 0x10
BPF_TAX = 0
BPF_TXA = 0x80
BPF_W = 0
BPF_X = 0x8
DT_BLK = 0x6 DT_BLK = 0x6
DT_CHR = 0x2 DT_CHR = 0x2
DT_DIR = 0x4 DT_DIR = 0x4
@ -256,6 +296,68 @@ const (
ESRMNT = 0x45 ESRMNT = 0x45
ESTALE = 0x74 ESTALE = 0x74
ESTRPIPE = 0x56 ESTRPIPE = 0x56
ETH_P_1588 = 0x88f7
ETH_P_8021Q = 0x8100
ETH_P_802_2 = 0x4
ETH_P_802_3 = 0x1
ETH_P_AARP = 0x80f3
ETH_P_ALL = 0x3
ETH_P_AOE = 0x88a2
ETH_P_ARCNET = 0x1a
ETH_P_ARP = 0x806
ETH_P_ATALK = 0x809b
ETH_P_ATMFATE = 0x8884
ETH_P_ATMMPOA = 0x884c
ETH_P_AX25 = 0x2
ETH_P_BPQ = 0x8ff
ETH_P_CAN = 0xc
ETH_P_CONTROL = 0x16
ETH_P_CUST = 0x6006
ETH_P_DDCMP = 0x6
ETH_P_DEC = 0x6000
ETH_P_DIAG = 0x6005
ETH_P_DNA_DL = 0x6001
ETH_P_DNA_RC = 0x6002
ETH_P_DNA_RT = 0x6003
ETH_P_DSA = 0x1b
ETH_P_ECONET = 0x18
ETH_P_EDSA = 0xdada
ETH_P_FCOE = 0x8906
ETH_P_FIP = 0x8914
ETH_P_HDLC = 0x19
ETH_P_IEEE802154 = 0xf6
ETH_P_IEEEPUP = 0xa00
ETH_P_IEEEPUPAT = 0xa01
ETH_P_IP = 0x800
ETH_P_IPV6 = 0x86dd
ETH_P_IPX = 0x8137
ETH_P_IRDA = 0x17
ETH_P_LAT = 0x6004
ETH_P_LOCALTALK = 0x9
ETH_P_LOOP = 0x60
ETH_P_MOBITEX = 0x15
ETH_P_MPLS_MC = 0x8848
ETH_P_MPLS_UC = 0x8847
ETH_P_PAE = 0x888e
ETH_P_PAUSE = 0x8808
ETH_P_PHONET = 0xf5
ETH_P_PPPTALK = 0x10
ETH_P_PPP_DISC = 0x8863
ETH_P_PPP_MP = 0x8
ETH_P_PPP_SES = 0x8864
ETH_P_PUP = 0x200
ETH_P_PUPAT = 0x201
ETH_P_RARP = 0x8035
ETH_P_SCA = 0x6007
ETH_P_SLOW = 0x8809
ETH_P_SNAP = 0x5
ETH_P_TEB = 0x6558
ETH_P_TIPC = 0x88ca
ETH_P_TRAILER = 0x1c
ETH_P_TR_802_2 = 0x11
ETH_P_WAN_PPP = 0x7
ETH_P_WCCP = 0x883e
ETH_P_X25 = 0x805
ETIME = 0x3e ETIME = 0x3e
ETIMEDOUT = 0x6e ETIMEDOUT = 0x6e
ETOOMANYREFS = 0x6d ETOOMANYREFS = 0x6d

View File

@ -106,6 +106,46 @@ const (
ARPHRD_TUNNEL6 = 0x301 ARPHRD_TUNNEL6 = 0x301
ARPHRD_VOID = 0xffff ARPHRD_VOID = 0xffff
ARPHRD_X25 = 0x10f ARPHRD_X25 = 0x10f
BPF_A = 0x10
BPF_ABS = 0x20
BPF_ADD = 0
BPF_ALU = 0x4
BPF_AND = 0x50
BPF_B = 0x10
BPF_DIV = 0x30
BPF_H = 0x8
BPF_IMM = 0
BPF_IND = 0x40
BPF_JA = 0
BPF_JEQ = 0x10
BPF_JGE = 0x30
BPF_JGT = 0x20
BPF_JMP = 0x5
BPF_JSET = 0x40
BPF_K = 0
BPF_LD = 0
BPF_LDX = 0x1
BPF_LEN = 0x80
BPF_LSH = 0x60
BPF_MAJOR_VERSION = 0x1
BPF_MAXINSNS = 0x1000
BPF_MEM = 0x60
BPF_MEMWORDS = 0x10
BPF_MINOR_VERSION = 0x1
BPF_MISC = 0x7
BPF_MSH = 0xa0
BPF_MUL = 0x20
BPF_NEG = 0x80
BPF_OR = 0x40
BPF_RET = 0x6
BPF_RSH = 0x70
BPF_ST = 0x2
BPF_STX = 0x3
BPF_SUB = 0x10
BPF_TAX = 0
BPF_TXA = 0x80
BPF_W = 0
BPF_X = 0x8
DT_BLK = 0x6 DT_BLK = 0x6
DT_CHR = 0x2 DT_CHR = 0x2
DT_DIR = 0x4 DT_DIR = 0x4
@ -258,6 +298,68 @@ const (
ESRMNT = 0x45 ESRMNT = 0x45
ESTALE = 0x74 ESTALE = 0x74
ESTRPIPE = 0x56 ESTRPIPE = 0x56
ETH_P_1588 = 0x88f7
ETH_P_8021Q = 0x8100
ETH_P_802_2 = 0x4
ETH_P_802_3 = 0x1
ETH_P_AARP = 0x80f3
ETH_P_ALL = 0x3
ETH_P_AOE = 0x88a2
ETH_P_ARCNET = 0x1a
ETH_P_ARP = 0x806
ETH_P_ATALK = 0x809b
ETH_P_ATMFATE = 0x8884
ETH_P_ATMMPOA = 0x884c
ETH_P_AX25 = 0x2
ETH_P_BPQ = 0x8ff
ETH_P_CAN = 0xc
ETH_P_CONTROL = 0x16
ETH_P_CUST = 0x6006
ETH_P_DDCMP = 0x6
ETH_P_DEC = 0x6000
ETH_P_DIAG = 0x6005
ETH_P_DNA_DL = 0x6001
ETH_P_DNA_RC = 0x6002
ETH_P_DNA_RT = 0x6003
ETH_P_DSA = 0x1b
ETH_P_ECONET = 0x18
ETH_P_EDSA = 0xdada
ETH_P_FCOE = 0x8906
ETH_P_FIP = 0x8914
ETH_P_HDLC = 0x19
ETH_P_IEEE802154 = 0xf6
ETH_P_IEEEPUP = 0xa00
ETH_P_IEEEPUPAT = 0xa01
ETH_P_IP = 0x800
ETH_P_IPV6 = 0x86dd
ETH_P_IPX = 0x8137
ETH_P_IRDA = 0x17
ETH_P_LAT = 0x6004
ETH_P_LOCALTALK = 0x9
ETH_P_LOOP = 0x60
ETH_P_MOBITEX = 0x15
ETH_P_MPLS_MC = 0x8848
ETH_P_MPLS_UC = 0x8847
ETH_P_PAE = 0x888e
ETH_P_PAUSE = 0x8808
ETH_P_PHONET = 0xf5
ETH_P_PPPTALK = 0x10
ETH_P_PPP_DISC = 0x8863
ETH_P_PPP_MP = 0x8
ETH_P_PPP_SES = 0x8864
ETH_P_PUP = 0x200
ETH_P_PUPAT = 0x201
ETH_P_RARP = 0x8035
ETH_P_SCA = 0x6007
ETH_P_SLOW = 0x8809
ETH_P_SNAP = 0x5
ETH_P_TEB = 0x6558
ETH_P_TIPC = 0x88ca
ETH_P_TRAILER = 0x1c
ETH_P_TR_802_2 = 0x11
ETH_P_WAN_PPP = 0x7
ETH_P_WCCP = 0x883e
ETH_P_X25 = 0x805
ETIME = 0x3e ETIME = 0x3e
ETIMEDOUT = 0x6e ETIMEDOUT = 0x6e
ETOOMANYREFS = 0x6d ETOOMANYREFS = 0x6d
@ -696,6 +798,9 @@ const (
PTRACE_SINGLESTEP = 0x9 PTRACE_SINGLESTEP = 0x9
PTRACE_SYSCALL = 0x18 PTRACE_SYSCALL = 0x18
PTRACE_TRACEME = 0 PTRACE_TRACEME = 0
PT_DATA_ADDR = 0x10004
PT_TEXT_ADDR = 0x10000
PT_TEXT_END_ADDR = 0x10008
RTAX_ADVMSS = 0x8 RTAX_ADVMSS = 0x8
RTAX_CWND = 0x7 RTAX_CWND = 0x7
RTAX_FEATURES = 0xc RTAX_FEATURES = 0xc

View File

@ -100,6 +100,8 @@ const (
SizeofIfAddrmsg = 0x8 SizeofIfAddrmsg = 0x8
SizeofRtmsg = 0xc SizeofRtmsg = 0xc
SizeofRtNexthop = 0x8 SizeofRtNexthop = 0x8
SizeofSockFilter = 0x8
SizeofSockFprog = 0x8
SizeofInotifyEvent = 0x10 SizeofInotifyEvent = 0x10
) )
@ -400,6 +402,19 @@ type RtNexthop struct {
Ifindex int32 Ifindex int32
} }
type SockFilter struct {
Code uint16
Jt uint8
Jf uint8
K uint32
}
type SockFprog struct {
Len uint16
Pad_godefs_0 [2]byte
Filter *SockFilter
}
type InotifyEvent struct { type InotifyEvent struct {
Wd int32 Wd int32
Mask uint32 Mask uint32

View File

@ -100,6 +100,8 @@ const (
SizeofIfAddrmsg = 0x8 SizeofIfAddrmsg = 0x8
SizeofRtmsg = 0xc SizeofRtmsg = 0xc
SizeofRtNexthop = 0x8 SizeofRtNexthop = 0x8
SizeofSockFilter = 0x8
SizeofSockFprog = 0x10
SizeofInotifyEvent = 0x10 SizeofInotifyEvent = 0x10
) )
@ -402,6 +404,19 @@ type RtNexthop struct {
Ifindex int32 Ifindex int32
} }
type SockFilter struct {
Code uint16
Jt uint8
Jf uint8
K uint32
}
type SockFprog struct {
Len uint16
Pad_godefs_0 [6]byte
Filter *SockFilter
}
type InotifyEvent struct { type InotifyEvent struct {
Wd int32 Wd int32
Mask uint32 Mask uint32

View File

@ -105,6 +105,8 @@ const (
SizeofIfAddrmsg = 0x8 SizeofIfAddrmsg = 0x8
SizeofRtmsg = 0xc SizeofRtmsg = 0xc
SizeofRtNexthop = 0x8 SizeofRtNexthop = 0x8
SizeofSockFilter = 0x8
SizeofSockFprog = 0x8
SizeofInotifyEvent = 0x10 SizeofInotifyEvent = 0x10
) )
@ -407,6 +409,19 @@ type RtNexthop struct {
Ifindex int32 Ifindex int32
} }
type SockFilter struct {
Code uint16
Jt uint8
Jf uint8
K uint32
}
type SockFprog struct {
Len uint16
Pad_godefs_0 [2]byte
Filter *SockFilter
}
type InotifyEvent struct { type InotifyEvent struct {
Wd int32 Wd int32
Mask uint32 Mask uint32