diff options
author | csjp <csjp@FreeBSD.org> | 2006-06-02 19:59:33 +0000 |
---|---|---|
committer | csjp <csjp@FreeBSD.org> | 2006-06-02 19:59:33 +0000 |
commit | 2c4f67981e37d4914db61b39de9ce50520b8ab77 (patch) | |
tree | 91b5bc64ab856cef269d9fab6ff3feca3e06cf2c /sys/net/bpf.h | |
parent | 420f0a56b11b92d44992ae037cd8d5e18cc582f6 (diff) | |
download | FreeBSD-src-2c4f67981e37d4914db61b39de9ce50520b8ab77.zip FreeBSD-src-2c4f67981e37d4914db61b39de9ce50520b8ab77.tar.gz |
Fix the following bpf(4) race condition which can result in a panic:
(1) bpf peer attaches to interface netif0
(2) Packet is received by netif0
(3) ifp->if_bpf pointer is checked and handed off to bpf
(4) bpf peer detaches from netif0 resulting in ifp->if_bpf being
initialized to NULL.
(5) ifp->if_bpf is dereferenced by bpf machinery
(6) Kaboom
This race condition likely explains the various different kernel panics
reported around sending SIGINT to tcpdump or dhclient processes. But really
this race can result in kernel panics anywhere you have frequent bpf attach
and detach operations with high packet per second load.
Summary of changes:
- Remove the bpf interface's "driverp" member
- When we attach bpf interfaces, we now set the ifp->if_bpf member to the
bpf interface structure. Once this is done, ifp->if_bpf should never be
NULL. [1]
- Introduce bpf_peers_present function, an inline operation which will do
a lockless read bpf peer list associated with the interface. It should
be noted that the bpf code will pickup the bpf_interface lock before adding
or removing bpf peers. This should serialize the access to the bpf descriptor
list, removing the race.
- Expose the bpf_if structure in bpf.h so that the bpf_peers_present function
can use it. This also removes the struct bpf_if; hack that was there.
- Adjust all consumers of the raw if_bpf structure to use bpf_peers_present
Now what happens is:
(1) Packet is received by netif0
(2) Check to see if bpf descriptor list is empty
(3) Pickup the bpf interface lock
(4) Hand packet off to process
From the attach/detach side:
(1) Pickup the bpf interface lock
(2) Add/remove from bpf descriptor list
Now that we are storing the bpf interface structure with the ifnet, there is
is no need to walk the bpf interface list to locate the correct bpf interface.
We now simply look up the interface, and initialize the pointer. This has a
nice side effect of changing a bpf interface attach operation from O(N) (where
N is the number of bpf interfaces), to O(1).
[1] From now on, we can no longer check ifp->if_bpf to tell us whether or
not we have any bpf peers that might be interested in receiving packets.
In collaboration with: sam@
MFC after: 1 month
Diffstat (limited to 'sys/net/bpf.h')
-rw-r--r-- | sys/net/bpf.h | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/sys/net/bpf.h b/sys/net/bpf.h index 189b30f..b2868c2 100644 --- a/sys/net/bpf.h +++ b/sys/net/bpf.h @@ -603,7 +603,18 @@ struct bpf_dltlist { }; #ifdef _KERNEL -struct bpf_if; +/* + * Descriptor associated with each attached hardware interface. + */ +struct bpf_if { + LIST_ENTRY(bpf_if) bif_next; /* list of all interfaces */ + LIST_HEAD(, bpf_d) bif_dlist; /* descriptor list */ + u_int bif_dlt; /* link layer type */ + u_int bif_hdrlen; /* length of header (with padding) */ + struct ifnet *bif_ifp; /* corresponding interface */ + struct mtx bif_mtx; /* mutex for interface */ +}; + int bpf_validate(const struct bpf_insn *, int); void bpf_tap(struct bpf_if *, u_char *, u_int); void bpf_mtap(struct bpf_if *, struct mbuf *); @@ -615,18 +626,25 @@ void bpfdetach(struct ifnet *); void bpfilterattach(int); u_int bpf_filter(const struct bpf_insn *, u_char *, u_int, u_int); +static __inline int +bpf_peers_present(struct bpf_if *bpf) +{ + + return !LIST_EMPTY(&bpf->bif_dlist); +} + #define BPF_TAP(_ifp,_pkt,_pktlen) do { \ - if ((_ifp)->if_bpf) \ + if (bpf_peers_present((_ifp)->if_bpf)) \ bpf_tap((_ifp)->if_bpf, (_pkt), (_pktlen)); \ } while (0) #define BPF_MTAP(_ifp,_m) do { \ - if ((_ifp)->if_bpf) { \ + if (bpf_peers_present((_ifp)->if_bpf)) { \ M_ASSERTVALID(_m); \ bpf_mtap((_ifp)->if_bpf, (_m)); \ } \ } while (0) #define BPF_MTAP2(_ifp,_data,_dlen,_m) do { \ - if ((_ifp)->if_bpf) { \ + if (bpf_peers_present((_ifp)->if_bpf)) { \ M_ASSERTVALID(_m); \ bpf_mtap2((_ifp)->if_bpf,(_data),(_dlen),(_m)); \ } \ |