pcap.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. // Copyright 2012 Google, Inc. All rights reserved.
  2. // Copyright 2009-2011 Andreas Krennmair. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license
  5. // that can be found in the LICENSE file in the root of the source
  6. // tree.
  7. package pcap
  8. /*
  9. #cgo solaris LDFLAGS: -L /opt/local/lib -lpcap
  10. #cgo linux LDFLAGS: -Wl,-Bstatic -lpcap -Wl,-Bdynamic
  11. #cgo dragonfly LDFLAGS: -lpcap
  12. #cgo freebsd LDFLAGS: -lpcap
  13. #cgo openbsd LDFLAGS: -lpcap
  14. #cgo netbsd LDFLAGS: -lpcap
  15. #cgo darwin LDFLAGS: -lpcap
  16. #cgo windows CFLAGS: -I C:/WpdPack/Include
  17. #cgo windows,386 LDFLAGS: -L C:/WpdPack/Lib -lwpcap
  18. #cgo windows,amd64 LDFLAGS: -L C:/WpdPack/Lib/x64 -lwpcap
  19. #include <stdlib.h>
  20. #include <pcap.h>
  21. #include <stdint.h>
  22. // Some old versions of pcap don't define this constant.
  23. #ifndef PCAP_NETMASK_UNKNOWN
  24. #define PCAP_NETMASK_UNKNOWN 0xffffffff
  25. #endif
  26. // libpcap doesn't actually export its version in a #define-guardable way,
  27. // so we have to use other defined things to differentiate versions.
  28. // We assume at least libpcap v1.1 at the moment.
  29. // See http://upstream-tracker.org/versions/libpcap.html
  30. #ifndef PCAP_ERROR_TSTAMP_PRECISION_NOTSUP // < v1.5
  31. int pcap_set_immediate_mode(pcap_t *p, int mode) {
  32. return PCAP_ERROR;
  33. }
  34. #ifndef PCAP_TSTAMP_HOST // < v1.2
  35. int pcap_set_tstamp_type(pcap_t* p, int t) { return -1; }
  36. int pcap_list_tstamp_types(pcap_t* p, int** t) { return 0; }
  37. void pcap_free_tstamp_types(int *tstamp_types) {}
  38. const char* pcap_tstamp_type_val_to_name(int t) {
  39. return "pcap timestamp types not supported";
  40. }
  41. int pcap_tstamp_type_name_to_val(const char* t) {
  42. return PCAP_ERROR;
  43. }
  44. #endif // < v1.2
  45. #endif // < v1.5
  46. #ifndef PCAP_ERROR_PROMISC_PERM_DENIED
  47. #define PCAP_ERROR_PROMISC_PERM_DENIED -11
  48. #endif
  49. // WinPcap doesn't export a pcap_statustostr, so use the less-specific
  50. // pcap_strerror. Note that linking against something like cygwin libpcap
  51. // may result is less-specific error messages.
  52. #ifdef WIN32
  53. #define pcap_statustostr pcap_strerror
  54. // WinPcap also doesn't export pcap_can_set_rfmon and pcap_set_rfmon,
  55. // as those are handled by separate libraries (airpcap).
  56. // https://www.winpcap.org/docs/docs_412/html/group__wpcapfunc.html
  57. // Stub out those functions here, returning values that indicate rfmon
  58. // setting is unavailable/unsuccessful.
  59. int pcap_can_set_rfmon(pcap_t *p) {
  60. return 0;
  61. }
  62. int pcap_set_rfmon(pcap_t *p, int rfmon) {
  63. return PCAP_ERROR;
  64. }
  65. #endif
  66. // Windows, Macs, and Linux all use different time types. Joy.
  67. #ifdef WIN32
  68. #define gopacket_time_secs_t long
  69. #define gopacket_time_usecs_t long
  70. #elif __APPLE__
  71. #define gopacket_time_secs_t __darwin_time_t
  72. #define gopacket_time_usecs_t __darwin_suseconds_t
  73. #elif __ANDROID__
  74. #define gopacket_time_secs_t __kernel_time_t
  75. #define gopacket_time_usecs_t __kernel_suseconds_t
  76. #elif __GLIBC__
  77. #define gopacket_time_secs_t __time_t
  78. #define gopacket_time_usecs_t __suseconds_t
  79. #else // Some form of linux/bsd/etc...
  80. #include <sys/param.h>
  81. #ifdef __OpenBSD__
  82. #define gopacket_time_secs_t u_int32_t
  83. #define gopacket_time_usecs_t u_int32_t
  84. #else
  85. #define gopacket_time_secs_t time_t
  86. #define gopacket_time_usecs_t suseconds_t
  87. #endif
  88. #endif
  89. // The things we do to avoid pointers escaping to the heap...
  90. int pcap_next_ex_escaping(pcap_t *p, uintptr_t pkt_hdr, uintptr_t pkt_data) {
  91. return pcap_next_ex(p, (struct pcap_pkthdr**)(pkt_hdr), (const u_char**)(pkt_data));
  92. }
  93. */
  94. import "C"
  95. import (
  96. "errors"
  97. "fmt"
  98. "io"
  99. "net"
  100. "reflect"
  101. "runtime"
  102. "strconv"
  103. "sync"
  104. "sync/atomic"
  105. "syscall"
  106. "time"
  107. "unsafe"
  108. "github.com/google/gopacket"
  109. "github.com/google/gopacket/layers"
  110. )
  111. const errorBufferSize = 256
  112. // ErrNotActive is returned if handle is not activated
  113. const ErrNotActive = int(C.PCAP_ERROR_NOT_ACTIVATED)
  114. // MaxBpfInstructions is the maximum number of BPF instructions supported (BPF_MAXINSNS),
  115. // taken from Linux kernel: include/uapi/linux/bpf_common.h
  116. //
  117. // https://github.com/torvalds/linux/blob/master/include/uapi/linux/bpf_common.h
  118. const MaxBpfInstructions = 4096
  119. // 8 bytes per instruction, max 4096 instructions
  120. const bpfInstructionBufferSize = 8 * MaxBpfInstructions
  121. // Handle provides a connection to a pcap handle, allowing users to read packets
  122. // off the wire (Next), inject packets onto the wire (Inject), and
  123. // perform a number of other functions to affect and understand packet output.
  124. //
  125. // Handles are already pcap_activate'd
  126. type Handle struct {
  127. // cptr is the handle for the actual pcap C object.
  128. cptr *C.pcap_t
  129. timeout time.Duration
  130. device string
  131. deviceIndex int
  132. mu sync.Mutex
  133. closeMu sync.Mutex
  134. // stop is set to a non-zero value by Handle.Close to signal to
  135. // getNextBufPtrLocked to stop trying to read packets
  136. stop uint64
  137. // Since pointers to these objects are passed into a C function, if
  138. // they're declared locally then the Go compiler thinks they may have
  139. // escaped into C-land, so it allocates them on the heap. This causes a
  140. // huge memory hit, so to handle that we store them here instead.
  141. pkthdr *C.struct_pcap_pkthdr
  142. bufptr *C.u_char
  143. }
  144. // Stats contains statistics on how many packets were handled by a pcap handle,
  145. // and what was done with those packets.
  146. type Stats struct {
  147. PacketsReceived int
  148. PacketsDropped int
  149. PacketsIfDropped int
  150. }
  151. // Interface describes a single network interface on a machine.
  152. type Interface struct {
  153. Name string
  154. Description string
  155. Flags uint32
  156. Addresses []InterfaceAddress
  157. }
  158. // Datalink describes the datalink
  159. type Datalink struct {
  160. Name string
  161. Description string
  162. }
  163. // InterfaceAddress describes an address associated with an Interface.
  164. // Currently, it's IPv4/6 specific.
  165. type InterfaceAddress struct {
  166. IP net.IP
  167. Netmask net.IPMask // Netmask may be nil if we were unable to retrieve it.
  168. Broadaddr net.IP // Broadcast address for this IP may be nil
  169. P2P net.IP // P2P destination address for this IP may be nil
  170. }
  171. // BPF is a compiled filter program, useful for offline packet matching.
  172. type BPF struct {
  173. orig string
  174. bpf _Ctype_struct_bpf_program // takes a finalizer, not overriden by outsiders
  175. }
  176. // BPFInstruction is a byte encoded structure holding a BPF instruction
  177. type BPFInstruction struct {
  178. Code uint16
  179. Jt uint8
  180. Jf uint8
  181. K uint32
  182. }
  183. // BlockForever causes it to block forever waiting for packets, when passed
  184. // into SetTimeout or OpenLive, while still returning incoming packets to userland relatively
  185. // quickly.
  186. const BlockForever = -time.Millisecond * 10
  187. func timeoutMillis(timeout time.Duration) C.int {
  188. // Flip sign if necessary. See package docs on timeout for reasoning behind this.
  189. if timeout < 0 {
  190. timeout *= -1
  191. }
  192. // Round up
  193. if timeout != 0 && timeout < time.Millisecond {
  194. timeout = time.Millisecond
  195. }
  196. return C.int(timeout / time.Millisecond)
  197. }
  198. // OpenLive opens a device and returns a *Handle.
  199. // It takes as arguments the name of the device ("eth0"), the maximum size to
  200. // read for each packet (snaplen), whether to put the interface in promiscuous
  201. // mode, and a timeout.
  202. //
  203. // See the package documentation for important details regarding 'timeout'.
  204. func OpenLive(device string, snaplen int32, promisc bool, timeout time.Duration) (handle *Handle, _ error) {
  205. buf := (*C.char)(C.calloc(errorBufferSize, 1))
  206. defer C.free(unsafe.Pointer(buf))
  207. var pro C.int
  208. if promisc {
  209. pro = 1
  210. }
  211. p := &Handle{timeout: timeout, device: device}
  212. ifc, err := net.InterfaceByName(device)
  213. if err != nil {
  214. // The device wasn't found in the OS, but could be "any"
  215. // Set index to 0
  216. p.deviceIndex = 0
  217. } else {
  218. p.deviceIndex = ifc.Index
  219. }
  220. dev := C.CString(device)
  221. defer C.free(unsafe.Pointer(dev))
  222. p.cptr = C.pcap_open_live(dev, C.int(snaplen), pro, timeoutMillis(timeout), buf)
  223. if p.cptr == nil {
  224. return nil, errors.New(C.GoString(buf))
  225. }
  226. // Only set the PCAP handle into non-blocking mode if we have a timeout
  227. // greater than zero. If the user wants to block forever, we'll let libpcap
  228. // handle that.
  229. if p.timeout > 0 {
  230. if err := p.setNonBlocking(); err != nil {
  231. C.pcap_close(p.cptr)
  232. return nil, err
  233. }
  234. }
  235. return p, nil
  236. }
  237. // OpenOffline opens a file and returns its contents as a *Handle.
  238. func OpenOffline(file string) (handle *Handle, err error) {
  239. buf := (*C.char)(C.calloc(errorBufferSize, 1))
  240. defer C.free(unsafe.Pointer(buf))
  241. cf := C.CString(file)
  242. defer C.free(unsafe.Pointer(cf))
  243. cptr := C.pcap_open_offline(cf, buf)
  244. if cptr == nil {
  245. return nil, errors.New(C.GoString(buf))
  246. }
  247. h := &Handle{cptr: cptr}
  248. return h, nil
  249. }
  250. // NextError is the return code from a call to Next.
  251. type NextError int32
  252. // NextError implements the error interface.
  253. func (n NextError) Error() string {
  254. switch n {
  255. case NextErrorOk:
  256. return "OK"
  257. case NextErrorTimeoutExpired:
  258. return "Timeout Expired"
  259. case NextErrorReadError:
  260. return "Read Error"
  261. case NextErrorNoMorePackets:
  262. return "No More Packets In File"
  263. case NextErrorNotActivated:
  264. return "Not Activated"
  265. }
  266. return strconv.Itoa(int(n))
  267. }
  268. // NextError values.
  269. const (
  270. NextErrorOk NextError = 1
  271. NextErrorTimeoutExpired NextError = 0
  272. NextErrorReadError NextError = -1
  273. // NextErrorNoMorePackets is returned when reading from a file (OpenOffline) and
  274. // EOF is reached. When this happens, Next() returns io.EOF instead of this.
  275. NextErrorNoMorePackets NextError = -2
  276. NextErrorNotActivated NextError = -3
  277. )
  278. // ReadPacketData returns the next packet read from the pcap handle, along with an error
  279. // code associated with that packet. If the packet is read successfully, the
  280. // returned error is nil.
  281. func (p *Handle) ReadPacketData() (data []byte, ci gopacket.CaptureInfo, err error) {
  282. p.mu.Lock()
  283. err = p.getNextBufPtrLocked(&ci)
  284. if err == nil {
  285. data = C.GoBytes(unsafe.Pointer(p.bufptr), C.int(ci.CaptureLength))
  286. }
  287. p.mu.Unlock()
  288. if err == NextErrorTimeoutExpired {
  289. runtime.Gosched()
  290. }
  291. return
  292. }
  293. type activateError C.int
  294. const (
  295. aeNoError = 0
  296. aeActivated = C.PCAP_ERROR_ACTIVATED
  297. aePromisc = C.PCAP_WARNING_PROMISC_NOTSUP
  298. aeNoSuchDevice = C.PCAP_ERROR_NO_SUCH_DEVICE
  299. aeDenied = C.PCAP_ERROR_PERM_DENIED
  300. aeNotUp = C.PCAP_ERROR_IFACE_NOT_UP
  301. aeWarning = C.PCAP_WARNING
  302. )
  303. func (a activateError) Error() string {
  304. switch a {
  305. case aeNoError:
  306. return "No Error"
  307. case aeActivated:
  308. return "Already Activated"
  309. case aePromisc:
  310. return "Cannot set as promisc"
  311. case aeNoSuchDevice:
  312. return "No Such Device"
  313. case aeDenied:
  314. return "Permission Denied"
  315. case aeNotUp:
  316. return "Interface Not Up"
  317. case aeWarning:
  318. return fmt.Sprintf("Warning: %v", activateErrMsg.Error())
  319. default:
  320. return fmt.Sprintf("unknown activated error: %d", a)
  321. }
  322. }
  323. // getNextBufPtrLocked is shared code for ReadPacketData and
  324. // ZeroCopyReadPacketData.
  325. func (p *Handle) getNextBufPtrLocked(ci *gopacket.CaptureInfo) error {
  326. if p.cptr == nil {
  327. return io.EOF
  328. }
  329. // This horrible magic allows us to pass a ptr-to-ptr to pcap_next_ex
  330. // without causing that ptr-to-ptr to itself be allocated on the heap.
  331. // Since Handle itself survives through the duration of the pcap_next_ex
  332. // call, this should be perfectly safe for GC stuff, etc.
  333. pp := C.uintptr_t(uintptr(unsafe.Pointer(&p.pkthdr)))
  334. bp := C.uintptr_t(uintptr(unsafe.Pointer(&p.bufptr)))
  335. // set after we have call waitForPacket for the first time
  336. var waited bool
  337. for atomic.LoadUint64(&p.stop) == 0 {
  338. // try to read a packet if one is immediately available
  339. result := NextError(C.pcap_next_ex_escaping(p.cptr, pp, bp))
  340. switch result {
  341. case NextErrorOk:
  342. // got a packet, set capture info and return
  343. sec := int64(p.pkthdr.ts.tv_sec)
  344. // convert micros to nanos
  345. nanos := int64(p.pkthdr.ts.tv_usec) * 1000
  346. ci.Timestamp = time.Unix(sec, nanos)
  347. ci.CaptureLength = int(p.pkthdr.caplen)
  348. ci.Length = int(p.pkthdr.len)
  349. ci.InterfaceIndex = p.deviceIndex
  350. return nil
  351. case NextErrorNoMorePackets:
  352. // no more packets, return EOF rather than libpcap-specific error
  353. return io.EOF
  354. case NextErrorTimeoutExpired:
  355. // we've already waited for a packet and we're supposed to time out
  356. //
  357. // we should never actually hit this if we were passed BlockForever
  358. // since we should block on C.pcap_next_ex until there's a packet
  359. // to read.
  360. if waited && p.timeout > 0 {
  361. return result
  362. }
  363. // wait for packet before trying again
  364. p.waitForPacket()
  365. waited = true
  366. default:
  367. return result
  368. }
  369. }
  370. // stop must be set
  371. return io.EOF
  372. }
  373. // ZeroCopyReadPacketData reads the next packet off the wire, and returns its data.
  374. // The slice returned by ZeroCopyReadPacketData points to bytes owned by the
  375. // the Handle. Each call to ZeroCopyReadPacketData invalidates any data previously
  376. // returned by ZeroCopyReadPacketData. Care must be taken not to keep pointers
  377. // to old bytes when using ZeroCopyReadPacketData... if you need to keep data past
  378. // the next time you call ZeroCopyReadPacketData, use ReadPacketData, which copies
  379. // the bytes into a new buffer for you.
  380. // data1, _, _ := handle.ZeroCopyReadPacketData()
  381. // // do everything you want with data1 here, copying bytes out of it if you'd like to keep them around.
  382. // data2, _, _ := handle.ZeroCopyReadPacketData() // invalidates bytes in data1
  383. func (p *Handle) ZeroCopyReadPacketData() (data []byte, ci gopacket.CaptureInfo, err error) {
  384. p.mu.Lock()
  385. err = p.getNextBufPtrLocked(&ci)
  386. if err == nil {
  387. slice := (*reflect.SliceHeader)(unsafe.Pointer(&data))
  388. slice.Data = uintptr(unsafe.Pointer(p.bufptr))
  389. slice.Len = ci.CaptureLength
  390. slice.Cap = ci.CaptureLength
  391. }
  392. p.mu.Unlock()
  393. if err == NextErrorTimeoutExpired {
  394. runtime.Gosched()
  395. }
  396. return
  397. }
  398. // Close closes the underlying pcap handle.
  399. func (p *Handle) Close() {
  400. p.closeMu.Lock()
  401. defer p.closeMu.Unlock()
  402. if p.cptr == nil {
  403. return
  404. }
  405. atomic.StoreUint64(&p.stop, 1)
  406. // wait for packet reader to stop
  407. p.mu.Lock()
  408. defer p.mu.Unlock()
  409. C.pcap_close(p.cptr)
  410. p.cptr = nil
  411. }
  412. // Error returns the current error associated with a pcap handle (pcap_geterr).
  413. func (p *Handle) Error() error {
  414. return errors.New(C.GoString(C.pcap_geterr(p.cptr)))
  415. }
  416. // Stats returns statistics on the underlying pcap handle.
  417. func (p *Handle) Stats() (stat *Stats, err error) {
  418. var cstats _Ctype_struct_pcap_stat
  419. if -1 == C.pcap_stats(p.cptr, &cstats) {
  420. return nil, p.Error()
  421. }
  422. return &Stats{
  423. PacketsReceived: int(cstats.ps_recv),
  424. PacketsDropped: int(cstats.ps_drop),
  425. PacketsIfDropped: int(cstats.ps_ifdrop),
  426. }, nil
  427. }
  428. // ListDataLinks obtains a list of all possible data link types supported for an interface.
  429. func (p *Handle) ListDataLinks() (datalinks []Datalink, err error) {
  430. var dltbuf *C.int
  431. n := int(C.pcap_list_datalinks(p.cptr, &dltbuf))
  432. if -1 == n {
  433. return nil, p.Error()
  434. }
  435. defer C.pcap_free_datalinks(dltbuf)
  436. datalinks = make([]Datalink, n)
  437. dltArray := (*[100]C.int)(unsafe.Pointer(dltbuf))
  438. for i := 0; i < n; i++ {
  439. expr := C.pcap_datalink_val_to_name((*dltArray)[i])
  440. datalinks[i].Name = C.GoString(expr)
  441. expr = C.pcap_datalink_val_to_description((*dltArray)[i])
  442. datalinks[i].Description = C.GoString(expr)
  443. }
  444. return datalinks, nil
  445. }
  446. // pcap_compile is NOT thread-safe, so protect it.
  447. var pcapCompileMu sync.Mutex
  448. // compileBPFFilter always returns an allocated _Ctype_struct_bpf_program
  449. // It is the callers responsibility to free the memory again, e.g.
  450. //
  451. // C.pcap_freecode(&bpf)
  452. //
  453. func (p *Handle) compileBPFFilter(expr string) (_Ctype_struct_bpf_program, error) {
  454. errorBuf := (*C.char)(C.calloc(errorBufferSize, 1))
  455. defer C.free(unsafe.Pointer(errorBuf))
  456. var netp uint32
  457. var maskp uint32
  458. // Only do the lookup on network interfaces.
  459. // No device indicates we're handling a pcap file.
  460. if len(p.device) > 0 {
  461. dev := C.CString(p.device)
  462. defer C.free(unsafe.Pointer(dev))
  463. if -1 == C.pcap_lookupnet(
  464. dev,
  465. (*C.bpf_u_int32)(unsafe.Pointer(&netp)),
  466. (*C.bpf_u_int32)(unsafe.Pointer(&maskp)),
  467. errorBuf,
  468. ) {
  469. // We can't lookup the network, but that could be because the interface
  470. // doesn't have an IPv4.
  471. }
  472. }
  473. var bpf _Ctype_struct_bpf_program
  474. cexpr := C.CString(expr)
  475. defer C.free(unsafe.Pointer(cexpr))
  476. pcapCompileMu.Lock()
  477. defer pcapCompileMu.Unlock()
  478. if -1 == C.pcap_compile(p.cptr, &bpf, cexpr, 1, C.bpf_u_int32(maskp)) {
  479. return bpf, p.Error()
  480. }
  481. return bpf, nil
  482. }
  483. // CompileBPFFilter compiles and returns a BPF filter with given a link type and capture length.
  484. func CompileBPFFilter(linkType layers.LinkType, captureLength int, expr string) ([]BPFInstruction, error) {
  485. cptr := C.pcap_open_dead(C.int(linkType), C.int(captureLength))
  486. if cptr == nil {
  487. return nil, errors.New("error opening dead capture")
  488. }
  489. h := Handle{cptr: cptr}
  490. defer h.Close()
  491. return h.CompileBPFFilter(expr)
  492. }
  493. // CompileBPFFilter compiles and returns a BPF filter for the pcap handle.
  494. func (p *Handle) CompileBPFFilter(expr string) ([]BPFInstruction, error) {
  495. bpf, err := p.compileBPFFilter(expr)
  496. defer C.pcap_freecode(&bpf)
  497. if err != nil {
  498. return nil, err
  499. }
  500. bpfInsn := (*[bpfInstructionBufferSize]_Ctype_struct_bpf_insn)(unsafe.Pointer(bpf.bf_insns))[0:bpf.bf_len:bpf.bf_len]
  501. bpfInstruction := make([]BPFInstruction, len(bpfInsn), len(bpfInsn))
  502. for i, v := range bpfInsn {
  503. bpfInstruction[i].Code = uint16(v.code)
  504. bpfInstruction[i].Jt = uint8(v.jt)
  505. bpfInstruction[i].Jf = uint8(v.jf)
  506. bpfInstruction[i].K = uint32(v.k)
  507. }
  508. return bpfInstruction, nil
  509. }
  510. // SetBPFFilter compiles and sets a BPF filter for the pcap handle.
  511. func (p *Handle) SetBPFFilter(expr string) (err error) {
  512. bpf, err := p.compileBPFFilter(expr)
  513. defer C.pcap_freecode(&bpf)
  514. if err != nil {
  515. return err
  516. }
  517. if -1 == C.pcap_setfilter(p.cptr, &bpf) {
  518. return p.Error()
  519. }
  520. return nil
  521. }
  522. // SetBPFInstructionFilter may be used to apply a filter in BPF asm byte code format.
  523. //
  524. // Simplest way to generate BPF asm byte code is with tcpdump:
  525. // tcpdump -dd 'udp'
  526. //
  527. // The output may be used directly to add a filter, e.g.:
  528. // bpfInstructions := []pcap.BpfInstruction{
  529. // {0x28, 0, 0, 0x0000000c},
  530. // {0x15, 0, 9, 0x00000800},
  531. // {0x30, 0, 0, 0x00000017},
  532. // {0x15, 0, 7, 0x00000006},
  533. // {0x28, 0, 0, 0x00000014},
  534. // {0x45, 5, 0, 0x00001fff},
  535. // {0xb1, 0, 0, 0x0000000e},
  536. // {0x50, 0, 0, 0x0000001b},
  537. // {0x54, 0, 0, 0x00000012},
  538. // {0x15, 0, 1, 0x00000012},
  539. // {0x6, 0, 0, 0x0000ffff},
  540. // {0x6, 0, 0, 0x00000000},
  541. // }
  542. //
  543. // An other posibility is to write the bpf code in bpf asm.
  544. // Documentation: https://www.kernel.org/doc/Documentation/networking/filter.txt
  545. //
  546. // To compile the code use bpf_asm from
  547. // https://github.com/torvalds/linux/tree/master/tools/net
  548. //
  549. // The following command may be used to convert bpf_asm output to c/go struct, usable for SetBPFFilterByte:
  550. // bpf_asm -c tcp.bpf
  551. func (p *Handle) SetBPFInstructionFilter(bpfInstructions []BPFInstruction) (err error) {
  552. bpf, err := bpfInstructionFilter(bpfInstructions)
  553. if err != nil {
  554. return err
  555. }
  556. if -1 == C.pcap_setfilter(p.cptr, &bpf) {
  557. C.pcap_freecode(&bpf)
  558. return p.Error()
  559. }
  560. C.pcap_freecode(&bpf)
  561. return nil
  562. }
  563. func bpfInstructionFilter(bpfInstructions []BPFInstruction) (bpf _Ctype_struct_bpf_program, err error) {
  564. if len(bpfInstructions) < 1 {
  565. return bpf, errors.New("bpfInstructions must not be empty")
  566. }
  567. if len(bpfInstructions) > MaxBpfInstructions {
  568. return bpf, fmt.Errorf("bpfInstructions must not be larger than %d", MaxBpfInstructions)
  569. }
  570. bpf.bf_len = C.u_int(len(bpfInstructions))
  571. cbpfInsns := C.calloc(C.size_t(len(bpfInstructions)), C.size_t(unsafe.Sizeof(bpfInstructions[0])))
  572. copy((*[bpfInstructionBufferSize]BPFInstruction)(cbpfInsns)[0:len(bpfInstructions)], bpfInstructions)
  573. bpf.bf_insns = (*_Ctype_struct_bpf_insn)(cbpfInsns)
  574. return
  575. }
  576. // NewBPF compiles the given string into a new filter program.
  577. //
  578. // BPF filters need to be created from activated handles, because they need to
  579. // know the underlying link type to correctly compile their offsets.
  580. func (p *Handle) NewBPF(expr string) (*BPF, error) {
  581. bpf := &BPF{orig: expr}
  582. cexpr := C.CString(expr)
  583. defer C.free(unsafe.Pointer(cexpr))
  584. pcapCompileMu.Lock()
  585. defer pcapCompileMu.Unlock()
  586. if C.pcap_compile(p.cptr, &bpf.bpf, cexpr /* optimize */, 1, C.PCAP_NETMASK_UNKNOWN) != 0 {
  587. return nil, p.Error()
  588. }
  589. runtime.SetFinalizer(bpf, destroyBPF)
  590. return bpf, nil
  591. }
  592. // NewBPFInstructionFilter sets the given BPFInstructions as new filter program.
  593. //
  594. // More details see func SetBPFInstructionFilter
  595. //
  596. // BPF filters need to be created from activated handles, because they need to
  597. // know the underlying link type to correctly compile their offsets.
  598. func (p *Handle) NewBPFInstructionFilter(bpfInstructions []BPFInstruction) (*BPF, error) {
  599. var err error
  600. bpf := &BPF{orig: "BPF Instruction Filter"}
  601. bpf.bpf, err = bpfInstructionFilter(bpfInstructions)
  602. if err != nil {
  603. return nil, err
  604. }
  605. runtime.SetFinalizer(bpf, destroyBPF)
  606. return bpf, nil
  607. }
  608. func destroyBPF(bpf *BPF) {
  609. C.pcap_freecode(&bpf.bpf)
  610. }
  611. // String returns the original string this BPF filter was compiled from.
  612. func (b *BPF) String() string {
  613. return b.orig
  614. }
  615. // Matches returns true if the given packet data matches this filter.
  616. func (b *BPF) Matches(ci gopacket.CaptureInfo, data []byte) bool {
  617. var hdr C.struct_pcap_pkthdr
  618. hdr.ts.tv_sec = C.gopacket_time_secs_t(ci.Timestamp.Unix())
  619. hdr.ts.tv_usec = C.gopacket_time_usecs_t(ci.Timestamp.Nanosecond() / 1000)
  620. hdr.caplen = C.bpf_u_int32(len(data)) // Trust actual length over ci.Length.
  621. hdr.len = C.bpf_u_int32(ci.Length)
  622. dataptr := (*C.u_char)(unsafe.Pointer(&data[0]))
  623. return C.pcap_offline_filter(&b.bpf, &hdr, dataptr) != 0
  624. }
  625. // Version returns pcap_lib_version.
  626. func Version() string {
  627. return C.GoString(C.pcap_lib_version())
  628. }
  629. // LinkType returns pcap_datalink, as a layers.LinkType.
  630. func (p *Handle) LinkType() layers.LinkType {
  631. return layers.LinkType(C.pcap_datalink(p.cptr))
  632. }
  633. // SetLinkType calls pcap_set_datalink on the pcap handle.
  634. func (p *Handle) SetLinkType(dlt layers.LinkType) error {
  635. if -1 == C.pcap_set_datalink(p.cptr, C.int(dlt)) {
  636. return p.Error()
  637. }
  638. return nil
  639. }
  640. // DatalinkValToName returns pcap_datalink_val_to_name as string
  641. func DatalinkValToName(dlt int) string {
  642. return C.GoString(C.pcap_datalink_val_to_name(C.int(dlt)))
  643. }
  644. // DatalinkValToDescription returns pcap_datalink_val_to_description as string
  645. func DatalinkValToDescription(dlt int) string {
  646. return C.GoString(C.pcap_datalink_val_to_description(C.int(dlt)))
  647. }
  648. // DatalinkNameToVal returns pcap_datalink_name_to_val as int
  649. func DatalinkNameToVal(name string) C.int {
  650. cptr := C.CString(name)
  651. defer C.free(unsafe.Pointer(cptr))
  652. return C.int(C.pcap_datalink_name_to_val(cptr))
  653. }
  654. // FindAllDevs attempts to enumerate all interfaces on the current machine.
  655. func FindAllDevs() (ifs []Interface, err error) {
  656. var buf *C.char
  657. buf = (*C.char)(C.calloc(errorBufferSize, 1))
  658. defer C.free(unsafe.Pointer(buf))
  659. var alldevsp *C.pcap_if_t
  660. if -1 == C.pcap_findalldevs((**C.pcap_if_t)(&alldevsp), buf) {
  661. return nil, errors.New(C.GoString(buf))
  662. }
  663. defer C.pcap_freealldevs((*C.pcap_if_t)(alldevsp))
  664. dev := alldevsp
  665. var i uint32
  666. for i = 0; dev != nil; dev = (*C.pcap_if_t)(dev.next) {
  667. i++
  668. }
  669. ifs = make([]Interface, i)
  670. dev = alldevsp
  671. for j := uint32(0); dev != nil; dev = (*C.pcap_if_t)(dev.next) {
  672. var iface Interface
  673. iface.Name = C.GoString(dev.name)
  674. iface.Description = C.GoString(dev.description)
  675. iface.Addresses = findalladdresses(dev.addresses)
  676. iface.Flags = uint32(dev.flags)
  677. ifs[j] = iface
  678. j++
  679. }
  680. return
  681. }
  682. func findalladdresses(addresses *_Ctype_struct_pcap_addr) (retval []InterfaceAddress) {
  683. // TODO - make it support more than IPv4 and IPv6?
  684. retval = make([]InterfaceAddress, 0, 1)
  685. for curaddr := addresses; curaddr != nil; curaddr = (*_Ctype_struct_pcap_addr)(curaddr.next) {
  686. // Strangely, it appears that in some cases, we get a pcap address back from
  687. // pcap_findalldevs with a nil .addr. It appears that we can skip over
  688. // these.
  689. if curaddr.addr == nil {
  690. continue
  691. }
  692. var a InterfaceAddress
  693. var err error
  694. if a.IP, err = sockaddrToIP((*syscall.RawSockaddr)(unsafe.Pointer(curaddr.addr))); err != nil {
  695. continue
  696. }
  697. // To be safe, we'll also check for netmask.
  698. if curaddr.netmask == nil {
  699. continue
  700. }
  701. if a.Netmask, err = sockaddrToIP((*syscall.RawSockaddr)(unsafe.Pointer(curaddr.netmask))); err != nil {
  702. // If we got an IP address but we can't get a netmask, just return the IP
  703. // address.
  704. a.Netmask = nil
  705. }
  706. if a.Broadaddr, err = sockaddrToIP((*syscall.RawSockaddr)(unsafe.Pointer(curaddr.broadaddr))); err != nil {
  707. a.Broadaddr = nil
  708. }
  709. if a.P2P, err = sockaddrToIP((*syscall.RawSockaddr)(unsafe.Pointer(curaddr.dstaddr))); err != nil {
  710. a.P2P = nil
  711. }
  712. retval = append(retval, a)
  713. }
  714. return
  715. }
  716. func sockaddrToIP(rsa *syscall.RawSockaddr) (IP []byte, err error) {
  717. if unsafe.Pointer(rsa) == nil {
  718. err = errors.New("Value not set")
  719. return
  720. }
  721. switch rsa.Family {
  722. case syscall.AF_INET:
  723. pp := (*syscall.RawSockaddrInet4)(unsafe.Pointer(rsa))
  724. IP = make([]byte, 4)
  725. for i := 0; i < len(IP); i++ {
  726. IP[i] = pp.Addr[i]
  727. }
  728. return
  729. case syscall.AF_INET6:
  730. pp := (*syscall.RawSockaddrInet6)(unsafe.Pointer(rsa))
  731. IP = make([]byte, 16)
  732. for i := 0; i < len(IP); i++ {
  733. IP[i] = pp.Addr[i]
  734. }
  735. return
  736. }
  737. err = errors.New("Unsupported address type")
  738. return
  739. }
  740. // WritePacketData calls pcap_sendpacket, injecting the given data into the pcap handle.
  741. func (p *Handle) WritePacketData(data []byte) (err error) {
  742. if -1 == C.pcap_sendpacket(p.cptr, (*C.u_char)(&data[0]), (C.int)(len(data))) {
  743. err = p.Error()
  744. }
  745. return
  746. }
  747. // Direction is used by Handle.SetDirection.
  748. type Direction uint8
  749. // Direction values for Handle.SetDirection.
  750. const (
  751. DirectionIn Direction = C.PCAP_D_IN
  752. DirectionOut Direction = C.PCAP_D_OUT
  753. DirectionInOut Direction = C.PCAP_D_INOUT
  754. )
  755. // SetDirection sets the direction for which packets will be captured.
  756. func (p *Handle) SetDirection(direction Direction) error {
  757. if direction != DirectionIn && direction != DirectionOut && direction != DirectionInOut {
  758. return fmt.Errorf("Invalid direction: %v", direction)
  759. }
  760. if status := C.pcap_setdirection(p.cptr, (C.pcap_direction_t)(direction)); status < 0 {
  761. return statusError(status)
  762. }
  763. return nil
  764. }
  765. // SnapLen returns the snapshot length
  766. func (p *Handle) SnapLen() int {
  767. len := C.pcap_snapshot(p.cptr)
  768. return int(len)
  769. }
  770. // TimestampSource tells PCAP which type of timestamp to use for packets.
  771. type TimestampSource C.int
  772. // String returns the timestamp type as a human-readable string.
  773. func (t TimestampSource) String() string {
  774. return C.GoString(C.pcap_tstamp_type_val_to_name(C.int(t)))
  775. }
  776. // TimestampSourceFromString translates a string into a timestamp type, case
  777. // insensitive.
  778. func TimestampSourceFromString(s string) (TimestampSource, error) {
  779. cs := C.CString(s)
  780. defer C.free(unsafe.Pointer(cs))
  781. t := C.pcap_tstamp_type_name_to_val(cs)
  782. if t < 0 {
  783. return 0, statusError(t)
  784. }
  785. return TimestampSource(t), nil
  786. }
  787. func statusError(status C.int) error {
  788. return errors.New(C.GoString(C.pcap_statustostr(status)))
  789. }
  790. // InactiveHandle allows you to call pre-pcap_activate functions on your pcap
  791. // handle to set it up just the way you'd like.
  792. type InactiveHandle struct {
  793. // cptr is the handle for the actual pcap C object.
  794. cptr *C.pcap_t
  795. device string
  796. deviceIndex int
  797. timeout time.Duration
  798. }
  799. // holds the err messoge in case activation returned a Warning
  800. var activateErrMsg error
  801. // Error returns the current error associated with a pcap handle (pcap_geterr).
  802. func (p *InactiveHandle) Error() error {
  803. return errors.New(C.GoString(C.pcap_geterr(p.cptr)))
  804. }
  805. // Activate activates the handle. The current InactiveHandle becomes invalid
  806. // and all future function calls on it will fail.
  807. func (p *InactiveHandle) Activate() (*Handle, error) {
  808. err := activateError(C.pcap_activate(p.cptr))
  809. if err != aeNoError {
  810. if err == aeWarning {
  811. activateErrMsg = p.Error()
  812. }
  813. return nil, err
  814. }
  815. h := &Handle{
  816. cptr: p.cptr,
  817. timeout: p.timeout,
  818. device: p.device,
  819. deviceIndex: p.deviceIndex,
  820. }
  821. p.cptr = nil
  822. return h, nil
  823. }
  824. // CleanUp cleans up any stuff left over from a successful or failed building
  825. // of a handle.
  826. func (p *InactiveHandle) CleanUp() {
  827. if p.cptr != nil {
  828. C.pcap_close(p.cptr)
  829. }
  830. }
  831. // NewInactiveHandle creates a new InactiveHandle, which wraps an un-activated PCAP handle.
  832. // Callers of NewInactiveHandle should immediately defer 'CleanUp', as in:
  833. // inactive := NewInactiveHandle("eth0")
  834. // defer inactive.CleanUp()
  835. func NewInactiveHandle(device string) (*InactiveHandle, error) {
  836. buf := (*C.char)(C.calloc(errorBufferSize, 1))
  837. defer C.free(unsafe.Pointer(buf))
  838. dev := C.CString(device)
  839. defer C.free(unsafe.Pointer(dev))
  840. // Try to get the interface index, but iy could be something like "any"
  841. // in which case use 0, which doesn't exist in nature
  842. deviceIndex := 0
  843. ifc, err := net.InterfaceByName(device)
  844. if err == nil {
  845. deviceIndex = ifc.Index
  846. }
  847. // This copies a bunch of the pcap_open_live implementation from pcap.c:
  848. cptr := C.pcap_create(dev, buf)
  849. if cptr == nil {
  850. return nil, errors.New(C.GoString(buf))
  851. }
  852. return &InactiveHandle{cptr: cptr, device: device, deviceIndex: deviceIndex}, nil
  853. }
  854. // SetSnapLen sets the snap length (max bytes per packet to capture).
  855. func (p *InactiveHandle) SetSnapLen(snaplen int) error {
  856. if status := C.pcap_set_snaplen(p.cptr, C.int(snaplen)); status < 0 {
  857. return statusError(status)
  858. }
  859. return nil
  860. }
  861. // SetPromisc sets the handle to either be promiscuous (capture packets
  862. // unrelated to this host) or not.
  863. func (p *InactiveHandle) SetPromisc(promisc bool) error {
  864. var pro C.int
  865. if promisc {
  866. pro = 1
  867. }
  868. if status := C.pcap_set_promisc(p.cptr, pro); status < 0 {
  869. return statusError(status)
  870. }
  871. return nil
  872. }
  873. // SetTimeout sets the read timeout for the handle.
  874. //
  875. // See the package documentation for important details regarding 'timeout'.
  876. func (p *InactiveHandle) SetTimeout(timeout time.Duration) error {
  877. if status := C.pcap_set_timeout(p.cptr, timeoutMillis(timeout)); status < 0 {
  878. return statusError(status)
  879. }
  880. p.timeout = timeout
  881. return nil
  882. }
  883. // SupportedTimestamps returns a list of supported timstamp types for this
  884. // handle.
  885. func (p *InactiveHandle) SupportedTimestamps() (out []TimestampSource) {
  886. var types *C.int
  887. n := int(C.pcap_list_tstamp_types(p.cptr, &types))
  888. defer C.pcap_free_tstamp_types(types)
  889. typesArray := (*[100]C.int)(unsafe.Pointer(types))
  890. for i := 0; i < n; i++ {
  891. out = append(out, TimestampSource((*typesArray)[i]))
  892. }
  893. return
  894. }
  895. // SetTimestampSource sets the type of timestamp generator PCAP uses when
  896. // attaching timestamps to packets.
  897. func (p *InactiveHandle) SetTimestampSource(t TimestampSource) error {
  898. if status := C.pcap_set_tstamp_type(p.cptr, C.int(t)); status < 0 {
  899. return statusError(status)
  900. }
  901. return nil
  902. }
  903. // CannotSetRFMon is returned by SetRFMon if the handle does not allow
  904. // setting RFMon because pcap_can_set_rfmon returns 0.
  905. var CannotSetRFMon = errors.New("Cannot set rfmon for this handle")
  906. // SetRFMon turns on radio monitoring mode, similar to promiscuous mode but for
  907. // wireless networks. If this mode is enabled, the interface will not need to
  908. // associate with an access point before it can receive traffic.
  909. func (p *InactiveHandle) SetRFMon(monitor bool) error {
  910. var mon C.int
  911. if monitor {
  912. mon = 1
  913. }
  914. switch canset := C.pcap_can_set_rfmon(p.cptr); canset {
  915. case 0:
  916. return CannotSetRFMon
  917. case 1:
  918. // success
  919. default:
  920. return statusError(canset)
  921. }
  922. if status := C.pcap_set_rfmon(p.cptr, mon); status != 0 {
  923. return statusError(status)
  924. }
  925. return nil
  926. }
  927. // SetBufferSize sets the buffer size (in bytes) of the handle.
  928. func (p *InactiveHandle) SetBufferSize(bufferSize int) error {
  929. if status := C.pcap_set_buffer_size(p.cptr, C.int(bufferSize)); status < 0 {
  930. return statusError(status)
  931. }
  932. return nil
  933. }
  934. // SetImmediateMode sets (or unsets) the immediate mode of the
  935. // handle. In immediate mode, packets are delivered to the application
  936. // as soon as they arrive. In other words, this overrides SetTimeout.
  937. func (p *InactiveHandle) SetImmediateMode(mode bool) error {
  938. var md C.int
  939. if mode {
  940. md = 1
  941. }
  942. if status := C.pcap_set_immediate_mode(p.cptr, md); status < 0 {
  943. return statusError(status)
  944. }
  945. return nil
  946. }