1// Copyright 2011 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5// Linux socket filter 6 7package syscall 8 9import ( 10 "unsafe" 11) 12 13// Deprecated: Use golang.org/x/net/bpf instead. 14func LsfStmt(code, k int) *SockFilter { 15 return &SockFilter{Code: uint16(code), K: uint32(k)} 16} 17 18// Deprecated: Use golang.org/x/net/bpf instead. 19func LsfJump(code, k, jt, jf int) *SockFilter { 20 return &SockFilter{Code: uint16(code), Jt: uint8(jt), Jf: uint8(jf), K: uint32(k)} 21} 22 23// Deprecated: Use golang.org/x/net/bpf instead. 24func LsfSocket(ifindex, proto int) (int, error) { 25 var lsall SockaddrLinklayer 26 // This is missing SOCK_CLOEXEC, but adding the flag 27 // could break callers. 28 s, e := Socket(AF_PACKET, SOCK_RAW, proto) 29 if e != nil { 30 return 0, e 31 } 32 p := (*[2]byte)(unsafe.Pointer(&lsall.Protocol)) 33 p[0] = byte(proto >> 8) 34 p[1] = byte(proto) 35 lsall.Ifindex = ifindex 36 e = Bind(s, &lsall) 37 if e != nil { 38 Close(s) 39 return 0, e 40 } 41 return s, nil 42} 43 44type iflags struct { 45 name [IFNAMSIZ]byte 46 flags uint16 47} 48 49// Deprecated: Use golang.org/x/net/bpf instead. 50func SetLsfPromisc(name string, m bool) error { 51 s, e := Socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0) 52 if e != nil { 53 return e 54 } 55 defer Close(s) 56 var ifl iflags 57 copy(ifl.name[:], []byte(name)) 58 _, _, ep := Syscall(SYS_IOCTL, uintptr(s), SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifl))) 59 if ep != 0 { 60 return Errno(ep) 61 } 62 if m { 63 ifl.flags |= uint16(IFF_PROMISC) 64 } else { 65 ifl.flags &^= uint16(IFF_PROMISC) 66 } 67 _, _, ep = Syscall(SYS_IOCTL, uintptr(s), SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifl))) 68 if ep != 0 { 69 return Errno(ep) 70 } 71 return nil 72} 73 74// Deprecated: Use golang.org/x/net/bpf instead. 75func AttachLsf(fd int, i []SockFilter) error { 76 var p SockFprog 77 p.Len = uint16(len(i)) 78 p.Filter = (*SockFilter)(unsafe.Pointer(&i[0])) 79 return setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, unsafe.Pointer(&p), unsafe.Sizeof(p)) 80} 81 82// Deprecated: Use golang.org/x/net/bpf instead. 83func DetachLsf(fd int) error { 84 var dummy int 85 return setsockopt(fd, SOL_SOCKET, SO_DETACH_FILTER, unsafe.Pointer(&dummy), unsafe.Sizeof(dummy)) 86} 87