Repository: secdev/scapy

Issue:
No /dev/bpf handle is available !
I'm running on mac high sierra 10.13.4
after downloading from https://github.com/secdev/scapy/archive/v2.4.0.zip I unzipping and ran each of the following as root: 
run_scapy, run_scapy2 and run_scapy_py3
within each repl I ran:
```send(IP(dst="2.2.2.2", src="1.1.1.1"))```
and the traceback was the same:
```
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/sendrecv.py", line 302, in send
    realtime=realtime, return_packets=return_packets)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/sendrecv.py", line 276, in __gen_send
    s.send(p)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/arch/bpf/supersocket.py", line 345, in send
    frame = raw(self.guessed_cls()/pkt)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/compat.py", line 96, in raw
    return bytes(x)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/packet.py", line 345, in __bytes__
    return self.build()
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/packet.py", line 444, in build
    p = self.do_build()
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/packet.py", line 426, in do_build
    pkt = self.self_build()
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/packet.py", line 407, in self_build
    p = f.addfield(self, p, val)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/fields.py", line 80, in addfield
    return s+struct.pack(self.fmt, self.i2m(pkt,val))
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/layers/l2.py", line 109, in i2m
    return MACField.i2m(self, pkt, self.i2h(pkt, x))
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/layers/l2.py", line 101, in i2h
    x = conf.neighbor.resolve(pkt,pkt.payload)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/layers/l2.py", line 49, in resolve
    return self.resolvers[k](l2inst,l3inst)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/layers/inet.py", line 821, in inet_register_l3
    return getmacbyip(l3.dst)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/layers/l2.py", line 84, in getmacbyip
    nofilter=1)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/sendrecv.py", line 434, in srp1
    ans, _ = srp(*args, **kargs)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/sendrecv.py", line 416, in srp
    s = conf.L2socket(promisc=promisc, iface=iface, filter=filter, nofilter=nofilter, type=type)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/arch/bpf/supersocket.py", line 58, in __init__
    (self.ins, self.dev_bpf) = get_dev_bpf()
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/arch/bpf/core.py", line 98, in get_dev_bpf
    raise Scapy_Exception("No /dev/bpf handle is available !")
scapy.error.Scapy_Exception: No /dev/bpf handle is available !
```

after looking at some of the past issues it appears similar to this one: [#1015](https://github.com/secdev/scapy/issues/1015)
however it was solved some time ago...

Update:
The above was ran while I had wireshark running, after quitting wireshark the error stopped.


Discussion:
It is likely that you are running other softwares that opened BPF handles, and Scapy isn't trying hard enough to open new handles. Is the following fix works for you?
```
diff --git a/scapy/arch/bpf/core.py b/scapy/arch/bpf/core.py
index 5753fcd..35aff23 100644
--- a/scapy/arch/bpf/core.py
+++ b/scapy/arch/bpf/core.py
@@ -88,7 +88,7 @@ def get_dev_bpf():
     """Returns an opened BPF file object"""
 
     # Get the first available BPF handle
-    for bpf in range(0, 8):
+    for bpf in range(0, 256):
         try:
             fd = os.open("/dev/bpf%i" % bpf, os.O_RDWR)
             return (fd, bpf)
```
@IdoBn is the proposed fix works?
Hi @guedou, 

I think I am having the same issue: 

```bash
$ ./run_scapy
INFO: Can't import PyX. Won't be able to use psdump() or pdfdump().
WARNING: No route found for IPv6 destination :: (no default route?)

                     aSPY//YASa
             apyyyyCY//////////YCa       |
            sY//////YSpcs  scpCY//Pp     | Welcome to Scapy
 ayp ayyyyyyySCP//Pp           syY//C    | Version 2.4.0.dev149
 AYAsAYYYYYYYY///Ps              cY//S   |
         pCCCCY//p          cSSps y//Y   | https://github.com/secdev/scapy
         SPPPP///a          pP///AC//Y   |
              A//A            cyP////C   | Have fun!
              p///Ac            sC///a   |
              P////YCpc           A//A   | Wanna support scapy? Rate it on
       scccccp///pSP///p          p//Y   | sectools!
      sY/////////y  caa           S//P   | http://sectools.org/tool/scapy/
       cayCyayP//Ya              pY/Ya   |             -- Satoshi Nakamoto
        sY/PsY////YCc          aC//Yp    |
         sc  sccaCY//PCypaapyCP//YSs
                  spCPY//////YPSps
                       ccaacs
                                       using IPython 5.3.0
>>> a = sniff(iface="en0", filter="tcp and port 80", count=10)
---------------------------------------------------------------------------
Scapy_Exception                           Traceback (most recent call last)
<ipython-input-1-42ea8830eddd> in <module>()
----> 1 a = sniff(iface="en0", filter="tcp and port 80", count=10)

/Users/keng/playground/scapy/scapy/sendrecv.pyc in sniff(count, store, offline, prn, lfilter, L2socket, timeout, opened_socket, stop_filter, iface, started_callback, *arg, **karg)
    780         else:
    781             sniff_sockets[L2socket(type=ETH_P_ALL, iface=iface,
--> 782                                    *arg, **karg)] = iface
    783     lst = []
    784     if timeout is not None:

/Users/keng/playground/scapy/scapy/arch/bpf/supersocket.pyc in __init__(self, iface, type, promisc, filter, nofilter, monitor)
     56
     57         # Get the BPF handle
---> 58         (self.ins, self.dev_bpf) = get_dev_bpf()
     59         self.outs = self.ins
     60

/Users/keng/playground/scapy/scapy/arch/bpf/core.pyc in get_dev_bpf()
     96             continue
     97
---> 98     raise Scapy_Exception("No /dev/bpf handle is available !")
     99
    100

Scapy_Exception: No /dev/bpf handle is available !
>>>
```

I just cloned the repo from the library's github page (https://github.com/secdev/scapy), I first had this error, then read a related issue and tried `python setup.py install`, but still the same error. I'm running python 2.7.13, so I also tried `./run_scapy_py2` but got the same error. I'm on macOS High Sierra (10.13.4). Thanks in advance for the assistance! 
Update: running `./run_scapy` with `sudo` appears to have eliminated the `No /dev/bpf handle is available !` error.  

Although I seem to have found a different, interesting (aesthetic) problem. The output does not appear to be generated in the REPL:

```bash
>>> a = sniff(iface="en0",prn=lambda x:x.sprintf("{Dot11Beacon:%Dot11.addr3%\t%Dot11Beacon.info%\t%PrismHeader.channel%\t%Dot11Beacon.cap%}"
...:), count=10)










>>> a
<Sniffed: TCP:5 UDP:2 ICMP:0 Other:3>
>>>

```

After executing the command, it appears to be printing in real time, but printing nothing. Seems odd, any ideas for this one? Thanks!
Scapy don’t sniff 802.11 frames by default, use the sniff() monitor argument.


Resolving patch:
diff --git a/scapy/arch/bpf/core.py b/scapy/arch/bpf/core.py
--- a/scapy/arch/bpf/core.py
+++ b/scapy/arch/bpf/core.py
@@ -88,7 +88,7 @@ def get_dev_bpf():
     """Returns an opened BPF file object"""
 
     # Get the first available BPF handle
-    for bpf in range(0, 8):
+    for bpf in range(256):
         try:
             fd = os.open("/dev/bpf%i" % bpf, os.O_RDWR)
             return (fd, bpf)


Test patch: