-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
packetbeat: Enable setting promiscuous mode automatically #11366
Changes from 11 commits
ffd96e6
46db28a
1c2146a
637eaab
1ff4818
7fc011e
4e140f3
9ef3f56
70dc0cd
fbe7398
8c87687
c00eb6f
24db049
1d2f681
185fbff
8e83262
22048ec
49cd7a6
fa877f5
2065731
cdd6069
24109ff
4bafbee
ae6bf1c
498c230
8a99e60
93e48ac
23d5948
9111121
52ce33e
70d02b0
ef0ae7d
7bb2402
a00e370
941f534
bbcc013
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,22 +20,40 @@ | |
package sniffer | ||
|
||
import ( | ||
"fmt" | ||
"syscall" | ||
"time" | ||
"unsafe" | ||
|
||
"github.com/elastic/beats/libbeat/logp" | ||
|
||
"github.com/tsg/gopacket" | ||
"github.com/tsg/gopacket/afpacket" | ||
"github.com/tsg/gopacket/layers" | ||
) | ||
|
||
type afpacketHandle struct { | ||
TPacket *afpacket.TPacket | ||
TPacket *afpacket.TPacket | ||
promicsPreviousState bool | ||
device string | ||
} | ||
|
||
func newAfpacketHandle(device string, snaplen int, block_size int, num_blocks int, | ||
timeout time.Duration) (*afpacketHandle, error) { | ||
|
||
h := &afpacketHandle{} | ||
var err error | ||
promiscEnabled, err := isPromiscEnabled(device) | ||
if err != nil { | ||
logp.Err("Failed to get promiscuous mode for device '%s': %v", device, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it fails here then when closing we don't really know what the previous state was so we should not try to restore it based on this value. |
||
} | ||
|
||
h := &afpacketHandle{ | ||
promicsPreviousState: promiscEnabled, | ||
device: device, | ||
} | ||
|
||
if err := setPromiscMode(device, true); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why change mode if it's already enabled? |
||
logp.Err("Failed to set promiscuous mode for device '%s': %v", device, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good to inform users what the impact is and what a possible workaround is. If Packetbeat is going to continue operating then I'd probably go for the |
||
} | ||
|
||
if device == "any" { | ||
h.TPacket, err = afpacket.NewTPacket( | ||
|
@@ -69,4 +87,42 @@ func (h *afpacketHandle) LinkType() layers.LinkType { | |
|
||
func (h *afpacketHandle) Close() { | ||
h.TPacket.Close() | ||
if err := setPromiscMode(h.device, h.promicsPreviousState); err != nil { | ||
logp.Err("Failed to set promiscuous mode for device '%s': %v", h.device, err) | ||
} | ||
} | ||
|
||
func isPromiscEnabled(device string) (bool, error) { | ||
if device == "any" { | ||
return false, nil | ||
} | ||
|
||
s, e := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0) | ||
if e != nil { | ||
return false, e | ||
} | ||
|
||
defer syscall.Close(s) | ||
|
||
var ifl struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's |
||
name [syscall.IFNAMSIZ]byte | ||
flags uint16 | ||
} | ||
|
||
copy(ifl.name[:], []byte(device)) | ||
_, _, ep := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s), syscall.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifl))) | ||
if ep != 0 { | ||
return false, fmt.Errorf("Syscall SIOCGIFFLAGS exited with %v", ep) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about
|
||
} | ||
|
||
return ifl.flags&uint16(syscall.IFF_PROMISC) != 0, nil | ||
} | ||
|
||
func setPromiscMode(device string, enabled bool) error { | ||
if device == "any" { | ||
logp.Warn("Cannot set promiscuous mode to device 'any'") | ||
return nil | ||
} | ||
|
||
return syscall.SetLsfPromisc(device, enabled) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function says its deprecated. Have you checked out what it recommends to use? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it says There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's at least add a code comment here explaining the issue + follow up issue in repo. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in
promics
.