commit:     4c0d676de802c6993d63d9d3be70d14f47a03419
Author:     Pacho Ramos <pacho <AT> gentoo <DOT> org>
AuthorDate: Sat Jul  8 09:58:02 2023 +0000
Commit:     Pacho Ramos <pacho <AT> gentoo <DOT> org>
CommitDate: Sat Jul  8 09:59:25 2023 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=4c0d676d

net-wireless/bluez: add 5.68

Closes: https://bugs.gentoo.org/909741
Signed-off-by: Pacho Ramos <pacho <AT> gentoo.org>

 net-wireless/bluez/Manifest                        |   1 +
 net-wireless/bluez/bluez-5.68.ebuild               | 292 +++++++++++++++++++++
 .../bluez/files/bluez-5.68-bap-dettach-io.patch    | 101 +++++++
 .../bluez/files/bluez-5.68-bap-ebusy-fix.patch     | 206 +++++++++++++++
 .../bluez/files/bluez-5.68-bap-nonzero.patch       |  27 ++
 .../bluez/files/bluez-5.68-bap-resume.patch        | 187 +++++++++++++
 .../files/bluez-5.68-heap-use-after-free.patch     |  41 +++
 .../bluez/files/bluez-5.68-monitor-decoding.patch  |  45 ++++
 8 files changed, 900 insertions(+)

diff --git a/net-wireless/bluez/Manifest b/net-wireless/bluez/Manifest
index 590a5b58d4f6..bf90982bd86d 100644
--- a/net-wireless/bluez/Manifest
+++ b/net-wireless/bluez/Manifest
@@ -1 +1,2 @@
 DIST bluez-5.66.tar.xz 2257288 BLAKE2B 
af1911cf590461a874daa16bedb8a021a3b2c2af3254360c035d2180e0f355372651bd41da342c26e6d9631d726dd08e0d6a83a22069333b94b6fce6d546718c
 SHA512 
ed0994932687eacf27207867366671b323671f5d5199daf36ea5eff8f254f2bc99ef989ef7df9883b35c06f2af60452be8bad0a06109428a4717cf2b247b4865
+DIST bluez-5.68.tar.xz 2319788 BLAKE2B 
3beca78fadef4d66df6f237b7460f6ac4bf001d80c856b599faa2cc1232c4342c7945eace5a6667009b9d19f2368f9841e608f07bc826b30ce9112c43dd7e316
 SHA512 
1805fb68923a5e098777b69835d7593396f8f2bbf52e1cfe58e7447621497a700b23389c79e96b2d663c611335f6ea9df11efe8aa75a8842f6b73105f66e799c

diff --git a/net-wireless/bluez/bluez-5.68.ebuild 
b/net-wireless/bluez/bluez-5.68.ebuild
new file mode 100644
index 000000000000..e1bfed36ea31
--- /dev/null
+++ b/net-wireless/bluez/bluez-5.68.ebuild
@@ -0,0 +1,292 @@
+# Copyright 1999-2023 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=8
+PYTHON_COMPAT=( python3_{9..12} )
+
+inherit autotools linux-info python-single-r1 systemd udev multilib-minimal 
#readme.gentoo-r1
+
+DESCRIPTION="Bluetooth Tools and System Daemons for Linux"
+HOMEPAGE="http://www.bluez.org";
+SRC_URI="https://www.kernel.org/pub/linux/bluetooth/${P}.tar.xz";
+
+LICENSE="GPL-2+ LGPL-2.1+"
+SLOT="0/3"
+KEYWORDS="~amd64 ~arm ~arm64 ~hppa ~loong ~mips ~ppc ~ppc64 ~riscv ~x86"
+IUSE="btpclient cups doc debug deprecated extra-tools experimental +mesh midi 
+obex +readline selinux systemd test test-programs +udev"
+
+# Since this release all remaining extra-tools need readline support, but this 
could
+# change in the future, hence, this REQUIRED_USE constraint could be dropped
+# again in the future.
+# btpclient needs mesh, bug #790587
+REQUIRED_USE="
+       btpclient? ( mesh )
+       extra-tools? ( deprecated readline )
+       test? ( ${PYTHON_REQUIRED_USE} )
+       test-programs? ( ${PYTHON_REQUIRED_USE} )
+"
+
+TEST_DEPS="${PYTHON_DEPS}
+       $(python_gen_cond_dep '
+               >=dev-python/dbus-python-1[${PYTHON_USEDEP}]
+               dev-python/pygobject:3[${PYTHON_USEDEP}]
+       ')
+"
+BDEPEND="
+       dev-python/docutils
+       virtual/pkgconfig
+       test? ( ${TEST_DEPS} )
+"
+DEPEND="
+       >=dev-libs/glib-2.28:2[${MULTILIB_USEDEP}]
+       btpclient? ( >=dev-libs/ell-0.39 )
+       cups? ( net-print/cups:= )
+       mesh? (
+               >=dev-libs/ell-0.39
+               >=dev-libs/json-c-0.13:=
+               sys-libs/readline:0=
+       )
+       midi? ( media-libs/alsa-lib )
+       obex? ( dev-libs/libical:= )
+       readline? ( sys-libs/readline:0= )
+       systemd? ( sys-apps/systemd )
+       >=sys-apps/dbus-1.6:=
+       udev? ( >=virtual/udev-172 )
+"
+RDEPEND="${DEPEND}
+       selinux? ( sec-policy/selinux-bluetooth )
+       test-programs? ( ${TEST_DEPS} )
+"
+
+RESTRICT="!test? ( test )"
+
+PATCHES=(
+       # Try both udevadm paths to cover udev/systemd vs. eudev locations 
(#539844)
+       # http://www.spinics.net/lists/linux-bluetooth/msg58739.html
+       # https://bugs.gentoo.org/539844
+       # https://github.com/bluez/bluez/issues/268
+       "${FILESDIR}"/${PN}-udevadm-path-r1.patch
+
+       # Fedora patches
+       # 
https://lore.kernel.org/linux-bluetooth/[email protected]/T/#m9c08d004cd5422783ee1d93154f42303bba9169f
+       "${FILESDIR}"/${PN}-5.66-power-state-adapter-property.patch
+
+       # Fixed in next release
+       "${FILESDIR}"/${P}-bap-resume.patch
+       "${FILESDIR}"/${P}-bap-nonzero.patch
+       "${FILESDIR}"/${P}-bap-dettach-io.patch
+       "${FILESDIR}"/${P}-bap-ebusy-fix.patch
+       "${FILESDIR}"/${P}-monitor-decoding.patch
+       "${FILESDIR}"/${P}-heap-use-after-free.patch
+)
+
+pkg_setup() {
+       # From http://www.linuxfromscratch.org/blfs/view/svn/general/bluez.html
+       # to prevent bugs like:
+       # https://bugzilla.kernel.org/show_bug.cgi?id=196621
+       CONFIG_CHECK="~NET ~BT ~BT_RFCOMM ~BT_RFCOMM_TTY ~BT_BNEP 
~BT_BNEP_MC_FILTER
+               ~BT_BNEP_PROTO_FILTER ~BT_HIDP ~CRYPTO_USER_API_HASH 
~CRYPTO_USER_API_SKCIPHER ~RFKILL"
+       # https://bugzilla.kernel.org/show_bug.cgi?id=196621
+       # https://bugzilla.kernel.org/show_bug.cgi?id=206815
+       if use mesh || use test; then
+               CONFIG_CHECK="${CONFIG_CHECK} ~CRYPTO_USER
+               ~CRYPTO_USER_API ~CRYPTO_USER_API_AEAD ~CRYPTO_AES ~CRYPTO_CCM 
~CRYPTO_AEAD ~CRYPTO_CMAC
+               ~CRYPTO_MD5 ~CRYPTO_SHA1 ~KEY_DH_OPERATIONS"
+       fi
+       linux-info_pkg_setup
+
+       if use test || use test-programs; then
+               python-single-r1_pkg_setup
+       fi
+
+       if ! use udev; then
+               ewarn
+               ewarn "You are installing ${PN} with USE=-udev. This means 
various bluetooth"
+               ewarn "devices and adapters from Apple, Dell, Logitech etc. 
will not work,"
+               ewarn "and hid2hci will not be available."
+               ewarn
+       fi
+}
+
+src_prepare() {
+       default
+
+       # http://www.spinics.net/lists/linux-bluetooth/msg38490.html
+       if ! use systemd; then
+               eapply 
"${FILESDIR}"/0001-Allow-using-obexd-without-systemd-in-the-user-session-r2.patch
+       fi
+
+       eautoreconf
+
+       if use cups; then
+               # Only not .am to not need to run eautoreconf only because of 
this
+               sed -i \
+                       -e "s:cupsdir = \$(libdir)/cups:cupsdir = $(cups-config 
--serverbin):" \
+                       Makefile.{in,tools} || die
+       fi
+
+       multilib_copy_sources
+}
+
+multilib_src_configure() {
+       local myconf=(
+               # readline is automagic when client is enabled
+               # --enable-client always needs readline, bug #504038
+               # --enable-mesh is handled in the same way
+               ac_cv_header_readline_readline_h=$(multilib_native_usex 
readline)
+               ac_cv_header_readline_readline_h=$(multilib_native_usex mesh)
+       )
+
+       if ! multilib_is_native_abi; then
+               myconf+=(
+                       # deps not used for the library
+                       {DBUS,GLIB}_{CFLAGS,LIBS}=' '
+               )
+       fi
+
+       econf \
+               --localstatedir=/var \
+               --disable-android \
+               --enable-datafiles \
+               --enable-optimization \
+               $(use_enable debug) \
+               --enable-pie \
+               --enable-threads \
+               --enable-library \
+               --enable-tools \
+               --enable-manpages \
+               --enable-monitor \
+               --with-systemdsystemunitdir="$(systemd_get_systemunitdir)" \
+               --with-systemduserunitdir="$(systemd_get_userunitdir)" \
+               $(multilib_native_use_enable btpclient) \
+               $(multilib_native_use_enable btpclient external-ell) \
+               $(multilib_native_use_enable cups) \
+               $(multilib_native_use_enable deprecated) \
+               $(multilib_native_use_enable experimental) \
+               $(multilib_native_use_enable mesh) \
+               $(multilib_native_use_enable mesh external-ell) \
+               $(multilib_native_use_enable midi) \
+               $(multilib_native_use_enable obex) \
+               $(multilib_native_use_enable readline client) \
+               $(multilib_native_use_enable systemd) \
+               $(multilib_native_use_enable test-programs test) \
+               $(multilib_native_use_enable udev) \
+               $(multilib_native_use_enable udev hid2hci) \
+               $(multilib_native_use_enable udev sixaxis)
+}
+
+multilib_src_compile() {
+       if multilib_is_native_abi; then
+               default
+       else
+               emake -f Makefile -f - libs \
+                       <<<'libs: $(lib_LTLIBRARIES)'
+       fi
+}
+
+multilib_src_test() {
+       multilib_is_native_abi && default
+}
+
+multilib_src_install() {
+       if multilib_is_native_abi; then
+               emake DESTDIR="${D}" install
+
+               # Only install extra-tools when relevant USE flag is enabled
+               if use extra-tools; then
+                       ewarn "Upstream doesn't support using this tools and 
their bugs are"
+                       ewarn "likely to be ignored forever, also they can 
break without"
+                       ewarn "previous announcement."
+                       ewarn "Upstream also states all this tools are not 
really needed,"
+                       ewarn "then, if you still need to rely on them, you 
must ask them"
+                       ewarn "to either install that tool by default or add 
the needed"
+                       ewarn "functionality to the existing 'official' tools."
+                       ewarn "Please report this issues to:"
+                       ewarn "http://www.bluez.org/development/lists/";
+
+                       # Upstream doesn't install this, bug #524640
+                       # 
http://permalink.gmane.org/gmane.linux.bluez.kernel/53115
+                       # 
http://comments.gmane.org/gmane.linux.bluez.kernel/54564
+                       dobin tools/btmgmt
+                       # gatttool is only built with readline, bug #530776
+                       # https://bugzilla.redhat.com/show_bug.cgi?id=1141909
+                       # 
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=720486
+                       # https://bugs.archlinux.org/task/37686
+                       dobin attrib/gatttool
+                       # https://bugzilla.redhat.com/show_bug.cgi?id=1699680
+                       dobin tools/avinfo
+               fi
+
+               # Not installed by default after being built, bug #666756
+               use btpclient && dobin tools/btpclient
+
+               # Unittests are not that useful once installed, so make them 
optional
+               if use test-programs; then
+                       # Drop python2 only test tools
+                       # https://bugzilla.kernel.org/show_bug.cgi?id=206819
+                       rm "${ED}"/usr/$(get_libdir)/bluez/test/simple-player 
|| die
+                       # https://bugzilla.kernel.org/show_bug.cgi?id=206821
+                       rm "${ED}"/usr/$(get_libdir)/bluez/test/test-hfp || die
+                       # https://bugzilla.kernel.org/show_bug.cgi?id=206823
+                       rm "${ED}"/usr/$(get_libdir)/bluez/test/test-sap-server 
|| die
+
+                       python_fix_shebang "${ED}"/usr/$(get_libdir)/bluez/test
+
+                       for i in $(find "${ED}"/usr/$(get_libdir)/bluez/test 
-maxdepth 1 -type f ! -name "*.*"); do
+                               dosym "${i}" /usr/bin/bluez-"${i##*/}"
+                       done
+               fi
+       else
+               emake DESTDIR="${D}" \
+                       install-pkgincludeHEADERS \
+                       install-libLTLIBRARIES \
+                       install-pkgconfigDATA
+       fi
+}
+
+multilib_src_install_all() {
+       # We need to ensure obexd can be spawned automatically by systemd
+       # when user-session is enabled:
+       # http://marc.info/?l=linux-bluetooth&m=148096094716386&w=2
+       # https://bugs.gentoo.org/show_bug.cgi?id=577842
+       # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=804908
+       # https://bugs.archlinux.org/task/45816
+       # https://bugzilla.redhat.com/show_bug.cgi?id=1318441
+       # https://bugzilla.redhat.com/show_bug.cgi?id=1389347
+       if use systemd; then
+               dosym obex.service 
/usr/lib/systemd/user/dbus-org.bluez.obex.service
+       fi
+
+       find "${D}" -name '*.la' -type f -delete || die
+
+       keepdir /var/lib/bluetooth
+
+       # Upstream don't want people to play with them
+       # But we keep installing them due to 'historical' reasons
+       insinto /etc/bluetooth
+       local d
+       for d in input network; do
+               doins profiles/${d}/${d}.conf
+       done
+       # Setup auto enable as Fedora does for allowing to use
+       # keyboards/mouse as soon as possible
+       sed -i 's/#\[Policy\]$/\[Policy\]/; 
s/#AutoEnable=false/AutoEnable=true/' src/main.conf || die
+       doins src/main.conf
+
+       newinitd "${FILESDIR}"/bluetooth-init.d-r5 bluetooth
+       newconfd "${FILESDIR}"/bluetooth-conf.d bluetooth
+
+       einstalldocs
+       use doc && dodoc doc/*.txt
+}
+
+pkg_postinst() {
+       use udev && udev_reload
+       systemd_reenable bluetooth.service
+
+       has_version net-dialup/ppp || elog "To use dial up networking you must 
install net-dialup/ppp"
+}
+
+pkg_postrm() {
+       use udev && udev_reload
+}

diff --git a/net-wireless/bluez/files/bluez-5.68-bap-dettach-io.patch 
b/net-wireless/bluez/files/bluez-5.68-bap-dettach-io.patch
new file mode 100644
index 000000000000..bb79fc42734b
--- /dev/null
+++ b/net-wireless/bluez/files/bluez-5.68-bap-dettach-io.patch
@@ -0,0 +1,101 @@
+From 7b10e72de6f41585f087e6fc338106b44d3e69c9 Mon Sep 17 00:00:00 2001
+From: Pauli Virtanen <[email protected]>
+Date: Sun, 2 Jul 2023 21:43:04 +0300
+Subject: shared/bap: detach io for source ASEs only after Stop Ready
+
+The Client may terminate a CIS when sink is in QOS and source in
+Disabling states (BAP v1.0.1 Sec 5.6.5).  It may also terminate it when
+Receiver Stop Ready has completed successfully (BAP v1.0.1 Sec 5.6.5.1).
+
+It appears Samsung Galaxy Buds2 Pro (R510XXUOAWA5) ignores the Receiver
+Stop Ready command if CIS is already disconnected, and then gets stuck
+in disabling state. It works if CIS is disconnected after Receiver Stop
+Ready.
+
+For better compatibility as client for this device, and since it
+shouldn't matter for us in which order we do it, disconnect CIS after
+completion of Receiver Stop Ready, instead of immediately in Disabling.
+
+We disconnect also if Receiver Stop Ready fails, given that
+disconnecting in Disabled state should be OK.
+
+Link: https://github.com/bluez/bluez/issues/516
+---
+ src/shared/bap.c | 33 +++++++++++++++------------------
+ 1 file changed, 15 insertions(+), 18 deletions(-)
+
+diff --git a/src/shared/bap.c b/src/shared/bap.c
+index a0f5a0ae3c..72ce67c086 100644
+--- a/src/shared/bap.c
++++ b/src/shared/bap.c
+@@ -1168,18 +1168,6 @@ static bool match_stream_io(const void *data, const 
void *user_data)
+       return stream->io == io;
+ }
+ 
+-static void stream_stop_disabling(void *data, void *user_data)
+-{
+-      struct bt_bap_stream *stream = data;
+-
+-      if (stream->io || stream->ep->state != BT_ASCS_ASE_STATE_DISABLING)
+-              return;
+-
+-      DBG(stream->bap, "stream %p", stream);
+-
+-      bt_bap_stream_stop(stream, NULL, NULL);
+-}
+-
+ static bool bap_stream_io_detach(struct bt_bap_stream *stream)
+ {
+       struct bt_bap_stream *link;
+@@ -1198,9 +1186,6 @@ static bool bap_stream_io_detach(struct bt_bap_stream 
*stream)
+               /* Detach link if in QoS state */
+               if (link->ep->state == BT_ASCS_ASE_STATE_QOS)
+                       bap_stream_io_detach(link);
+-      } else {
+-              /* Links without IO on disabling state shall be stopped. */
+-              queue_foreach(stream->links, stream_stop_disabling, NULL);
+       }
+ 
+       stream_io_unref(io);
+@@ -1244,6 +1229,15 @@ static struct bt_bap *bt_bap_ref_safe(struct bt_bap 
*bap)
+       return bt_bap_ref(bap);
+ }
+ 
++static void stream_stop_complete(struct bt_bap_stream *stream, uint8_t code,
++                                      uint8_t reason, void *user_data)
++{
++      DBG(stream->bap, "stream %p stop 0x%02x 0x%02x", stream, code, reason);
++
++      if (stream->ep->state == BT_ASCS_ASE_STATE_DISABLING)
++              bap_stream_io_detach(stream);
++}
++
+ static void bap_stream_state_changed(struct bt_bap_stream *stream)
+ {
+       struct bt_bap *bap = stream->bap;
+@@ -1271,7 +1265,9 @@ static void bap_stream_state_changed(struct 
bt_bap_stream *stream)
+               bap_stream_update_io_links(stream);
+               break;
+       case BT_ASCS_ASE_STATE_DISABLING:
+-              bap_stream_io_detach(stream);
++              /* As client, we detach after Receiver Stop Ready */
++              if (!stream->client)
++                      bap_stream_io_detach(stream);
+               break;
+       case BT_ASCS_ASE_STATE_QOS:
+               if (stream->io && !stream->io->connecting)
+@@ -1305,8 +1301,9 @@ static void bap_stream_state_changed(struct 
bt_bap_stream *stream)
+                       bt_bap_stream_start(stream, NULL, NULL);
+               break;
+       case BT_ASCS_ASE_STATE_DISABLING:
+-              if (!bt_bap_stream_get_io(stream))
+-                      bt_bap_stream_stop(stream, NULL, NULL);
++              /* Send Stop Ready, and detach IO after remote replies */
++              if (stream->client)
++                      bt_bap_stream_stop(stream, stream_stop_complete, NULL);
+               break;
+       }
+ 
+-- 
+cgit 
+

diff --git a/net-wireless/bluez/files/bluez-5.68-bap-ebusy-fix.patch 
b/net-wireless/bluez/files/bluez-5.68-bap-ebusy-fix.patch
new file mode 100644
index 000000000000..72b875ba4b6f
--- /dev/null
+++ b/net-wireless/bluez/files/bluez-5.68-bap-ebusy-fix.patch
@@ -0,0 +1,206 @@
+From 8c3170190d6f626869f1f382138caf3a16030462 Mon Sep 17 00:00:00 2001
+From: Pauli Virtanen <[email protected]>
+Date: Sun, 2 Jul 2023 21:43:05 +0300
+Subject: bap: wait for CIG to become configurable before recreating CIS
+
+ISO sockets cannot be reconnected before all sockets in the same CIG
+have been closed, if the CIG was previously active.
+
+Keep track which endpoints have active CIG, and postpone connecting CIS
+until their CIG is no longer active.
+
+This addresses getting EBUSY from connect() when multiple CIS in the
+same CIG move streaming -> qos at the same time, which disconnects CIS
+and recreates them.  The EBUSY originates from COMMAND_DISALLOWED
+response to Set CIG Parameters.
+
+This requires the kernel side do the Disconnect CIS / Remove CIG / Set
+CIG Parameters HCI command steps in the right order, when all old
+sockets are closed first before connecting new ones.
+---
+ profiles/audio/bap.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++-----
+ 1 file changed, 97 insertions(+), 10 deletions(-)
+
+diff --git a/profiles/audio/bap.c b/profiles/audio/bap.c
+index 8e2fc1556a..d7ce9e0389 100644
+--- a/profiles/audio/bap.c
++++ b/profiles/audio/bap.c
+@@ -68,6 +68,7 @@ struct bap_ep {
+       GIOChannel *io;
+       unsigned int io_id;
+       bool recreate;
++      bool cig_active;
+       struct iovec *caps;
+       struct iovec *metadata;
+       struct bt_bap_qos qos;
+@@ -525,6 +526,7 @@ static void bap_io_close(struct bap_ep *ep)
+ 
+       g_io_channel_unref(ep->io);
+       ep->io = NULL;
++      ep->cig_active = false;
+ }
+ 
+ static DBusMessage *set_configuration(DBusConnection *conn, DBusMessage *msg,
+@@ -988,7 +990,7 @@ drop:
+       g_io_channel_shutdown(io, TRUE, NULL);
+ }
+ 
+-static void bap_accept_io(struct bap_data *data, struct bt_bap_stream *stream,
++static void bap_accept_io(struct bap_ep *ep, struct bt_bap_stream *stream,
+                                                       int fd, int defer)
+ {
+       char c;
+@@ -1025,12 +1027,52 @@ static void bap_accept_io(struct bap_data *data, 
struct bt_bap_stream *stream,
+               }
+       }
+ 
++      ep->cig_active = true;
++
+       return;
+ 
+ fail:
+       close(fd);
+ }
+ 
++struct cig_busy_data {
++      struct btd_adapter *adapter;
++      uint8_t cig;
++};
++
++static bool cig_busy_ep(const void *data, const void *match_data)
++{
++      const struct bap_ep *ep = data;
++      const struct cig_busy_data *info = match_data;
++
++      return (ep->qos.ucast.cig_id == info->cig) && ep->cig_active;
++}
++
++static bool cig_busy_session(const void *data, const void *match_data)
++{
++      const struct bap_data *session = data;
++      const struct cig_busy_data *info = match_data;
++
++      if (device_get_adapter(session->device) != info->adapter)
++              return false;
++
++      return queue_find(session->snks, cig_busy_ep, match_data) ||
++                      queue_find(session->srcs, cig_busy_ep, match_data);
++}
++
++static bool is_cig_busy(struct bap_data *data, uint8_t cig)
++{
++      struct cig_busy_data info;
++
++      if (cig == BT_ISO_QOS_CIG_UNSET)
++              return false;
++
++      info.adapter = device_get_adapter(data->device);
++      info.cig = cig;
++
++      return queue_find(sessions, cig_busy_session, &info);
++}
++
+ static void bap_create_io(struct bap_data *data, struct bap_ep *ep,
+                               struct bt_bap_stream *stream, int defer);
+ 
+@@ -1047,6 +1089,48 @@ static gboolean bap_io_recreate(void *user_data)
+       return FALSE;
+ }
+ 
++static void recreate_cig_ep(void *data, void *match_data)
++{
++      struct bap_ep *ep = (struct bap_ep *)data;
++      struct cig_busy_data *info = match_data;
++
++      if (ep->qos.ucast.cig_id != info->cig || !ep->recreate || ep->io_id)
++              return;
++
++      ep->recreate = false;
++      ep->io_id = g_idle_add(bap_io_recreate, ep);
++}
++
++static void recreate_cig_session(void *data, void *match_data)
++{
++      struct bap_data *session = data;
++      struct cig_busy_data *info = match_data;
++
++      if (device_get_adapter(session->device) != info->adapter)
++              return;
++
++      queue_foreach(session->snks, recreate_cig_ep, match_data);
++      queue_foreach(session->srcs, recreate_cig_ep, match_data);
++}
++
++static void recreate_cig(struct bap_ep *ep)
++{
++      struct bap_data *data = ep->data;
++      struct cig_busy_data info;
++
++      info.adapter = device_get_adapter(data->device);
++      info.cig = ep->qos.ucast.cig_id;
++
++      DBG("adapter %p ep %p recreate CIG %d", info.adapter, ep, info.cig);
++
++      if (ep->qos.ucast.cig_id == BT_ISO_QOS_CIG_UNSET) {
++              recreate_cig_ep(ep, &info);
++              return;
++      }
++
++      queue_foreach(sessions, recreate_cig_session, &info);
++}
++
+ static gboolean bap_io_disconnected(GIOChannel *io, GIOCondition cond,
+                                                       gpointer user_data)
+ {
+@@ -1059,10 +1143,8 @@ static gboolean bap_io_disconnected(GIOChannel *io, 
GIOCondition cond,
+       bap_io_close(ep);
+ 
+       /* Check if connecting recreate IO */
+-      if (ep->recreate) {
+-              ep->recreate = false;
+-              ep->io_id = g_idle_add(bap_io_recreate, ep);
+-      }
++      if (!is_cig_busy(ep->data, ep->qos.ucast.cig_id))
++              recreate_cig(ep);
+ 
+       return FALSE;
+ }
+@@ -1087,18 +1169,22 @@ static void bap_connect_io(struct bap_data *data, 
struct bap_ep *ep,
+       int fd;
+ 
+       /* If IO already set skip creating it again */
+-      if (bt_bap_stream_get_io(stream))
++      if (bt_bap_stream_get_io(stream)) {
++              DBG("ep %p stream %p has existing io", ep, stream);
+               return;
++      }
+ 
+       if (bt_bap_stream_io_is_connecting(stream, &fd)) {
+-              bap_accept_io(data, stream, fd, defer);
++              bap_accept_io(ep, stream, fd, defer);
+               return;
+       }
+ 
+-      /* If IO channel still up wait for it to be disconnected and then
+-       * recreate.
++      /* If IO channel still up or CIG is busy, wait for it to be
++       * disconnected and then recreate.
+        */
+-      if (ep->io) {
++      if (ep->io || is_cig_busy(data, ep->qos.ucast.cig_id)) {
++              DBG("ep %p stream %p defer %s wait recreate", ep, stream,
++                                              defer ? "true" : "false");
+               ep->recreate = true;
+               return;
+       }
+@@ -1131,6 +1217,7 @@ static void bap_connect_io(struct bap_data *data, struct 
bap_ep *ep,
+                                               bap_io_disconnected, ep);
+ 
+       ep->io = io;
++      ep->cig_active = !defer;
+ 
+       bt_bap_stream_io_connecting(stream, g_io_channel_unix_get_fd(io));
+ }
+-- 
+cgit 
+

diff --git a/net-wireless/bluez/files/bluez-5.68-bap-nonzero.patch 
b/net-wireless/bluez/files/bluez-5.68-bap-nonzero.patch
new file mode 100644
index 000000000000..3a7ddd3c6f2a
--- /dev/null
+++ b/net-wireless/bluez/files/bluez-5.68-bap-nonzero.patch
@@ -0,0 +1,27 @@
+From 8fd0c76b41d35e24e10e45fcf57262482cb978a2 Mon Sep 17 00:00:00 2001
+From: Pauli Virtanen <[email protected]>
+Date: Sun, 2 Jul 2023 22:34:19 +0300
+Subject: shared/bap: use only nonzero req->id
+
+In bap_req_new, use same pattern as elsewhere to not use zero as
+a valid id.
+---
+ src/shared/bap.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/src/shared/bap.c b/src/shared/bap.c
+index cf5d810bba..a0f5a0ae3c 100644
+--- a/src/shared/bap.c
++++ b/src/shared/bap.c
+@@ -4306,7 +4306,7 @@ static struct bt_bap_req *bap_req_new(struct 
bt_bap_stream *stream,
+       static unsigned int id;
+ 
+       req = new0(struct bt_bap_req, 1);
+-      req->id = ++id;
++      req->id = ++id ? id : ++id;
+       req->stream = stream;
+       req->op = op;
+       req->iov = util_iov_dup(iov, len);
+-- 
+cgit 
+

diff --git a/net-wireless/bluez/files/bluez-5.68-bap-resume.patch 
b/net-wireless/bluez/files/bluez-5.68-bap-resume.patch
new file mode 100644
index 000000000000..fbc2eeda89cc
--- /dev/null
+++ b/net-wireless/bluez/files/bluez-5.68-bap-resume.patch
@@ -0,0 +1,187 @@
+From 466fce0209a3878512672159168943047a9e2323 Mon Sep 17 00:00:00 2001
+From: Pauli Virtanen <[email protected]>
+Date: Sun, 2 Jul 2023 22:34:18 +0300
+Subject: transport: handle BAP Enabling state correctly when resuming
+
+If BAP stream is in Enabling state when transport acquire is attempted,
+we should wait for bap_state_changed to emit the completion event.
+
+transport->resume() is only called with new owner with no
+owner->pending, and shall return nonzero completion id on success.
+Currently if BAP stream is Enabling, it returns zero which fails the
+acquire operation.
+
+To fix this, return valid completion id in this case instead.  Also keep
+track of the g_idle_add resume id, so that we don't try to give it to
+bt_bap_stream_cancel.
+
+Fixes sound server getting spurious Not Authorized errors when trying to
+acquire a pending transport.  This can happen on BAP server: linked
+transports become pending when the first of the two enters Streaming. If
+sound server tries to acquire the other linked transport whose stream is
+still Enabling, the acquire fails (media_owner_free +
+btd_error_not_authorized).
+
+Log:
+===============================================================
+profiles/audio/transport.c:bap_state_changed() stream 0x25c2880: qos(2) -> 
enabling(3)
+...
+profiles/audio/transport.c:bap_state_changed() stream 0x25cc590: qos(2) -> 
enabling(3)
+...
+src/shared/bap.c:bap_stream_state_changed() stream 0x25cc590 dir 0x01: 
enabling -> streaming
+profiles/audio/bap.c:bap_state() stream 0x25cc590: enabling(3) -> streaming(4)
+profiles/audio/transport.c:bap_state_changed() stream 0x25cc590: enabling(3) 
-> streaming(4)
+/org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/fd1: fd(36) ready
+profiles/audio/transport.c:transport_update_playing() 
/org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/fd1 State=TRANSPORT_STATE_IDLE Playing=1
+profiles/audio/transport.c:transport_set_state() State changed 
/org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/fd1: TRANSPORT_STATE_IDLE -> 
TRANSPORT_STATE_PENDING
+profiles/audio/transport.c:transport_set_state() State changed 
/org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/fd0: TRANSPORT_STATE_IDLE -> 
TRANSPORT_STATE_PENDING
+profiles/audio/transport.c:media_owner_create() Owner created: sender=:1.1242
+profiles/audio/transport.c:media_owner_free() Owner :1.1242
+===============================================================
+---
+ profiles/audio/transport.c | 67 +++++++++++++++++++++++++++++++++++++---------
+ 1 file changed, 55 insertions(+), 12 deletions(-)
+
+diff --git a/profiles/audio/transport.c b/profiles/audio/transport.c
+index 77216e10b3..aa3a718b0f 100644
+--- a/profiles/audio/transport.c
++++ b/profiles/audio/transport.c
+@@ -86,6 +86,7 @@ struct bap_transport {
+       unsigned int            state_id;
+       bool                    linked;
+       struct bt_bap_qos       qos;
++      guint                   resume_id;
+ };
+ 
+ struct media_transport {
+@@ -1191,17 +1192,27 @@ static void bap_enable_complete(struct bt_bap_stream 
*stream,
+               media_transport_remove_owner(owner->transport);
+ }
+ 
+-static gboolean resume_complete(void *data)
++static void bap_resume_complete(struct media_transport *transport)
+ {
+-      struct media_transport *transport = data;
++      struct bap_transport *bap = transport->data;
+       struct media_owner *owner = transport->owner;
+ 
++      DBG("stream %p owner %p resume complete", bap->stream, owner);
++
++      if (bap->resume_id) {
++              g_source_remove(bap->resume_id);
++              bap->resume_id = 0;
++      }
++
+       if (!owner)
+-              return FALSE;
++              return;
++
++      if (owner->pending)
++              owner->pending->id = 0;
+ 
+       if (transport->fd < 0) {
+               media_transport_remove_owner(transport);
+-              return FALSE;
++              return;
+       }
+ 
+       if (owner->pending) {
+@@ -1215,15 +1226,13 @@ static gboolean resume_complete(void *data)
+                                               DBUS_TYPE_INVALID);
+               if (!ret) {
+                       media_transport_remove_owner(transport);
+-                      return FALSE;
++                      return;
+               }
+       }
+ 
+       media_owner_remove(owner);
+ 
+       transport_set_state(transport, TRANSPORT_STATE_ACTIVE);
+-
+-      return FALSE;
+ }
+ 
+ static void bap_update_links(const struct media_transport *transport);
+@@ -1306,6 +1315,32 @@ static void bap_update_qos(const struct media_transport 
*transport)
+                       "Delay");
+ }
+ 
++static gboolean bap_resume_complete_cb(void *data)
++{
++      struct media_transport *transport = data;
++      struct bap_transport *bap = transport->data;
++
++      bap->resume_id = 0;
++      bap_resume_complete(transport);
++      return FALSE;
++}
++
++static gboolean bap_resume_wait_cb(void *data)
++{
++      struct media_transport *transport = data;
++      struct bap_transport *bap = transport->data;
++      struct media_owner *owner = transport->owner;
++
++      /* bap_state_changed will call completion callback when ready */
++      DBG("stream %p owner %p resume wait", bap->stream, owner);
++
++      bap->resume_id = 0;
++      if (owner && owner->pending)
++              owner->pending->id = 0;
++
++      return FALSE;
++}
++
+ static guint resume_bap(struct media_transport *transport,
+                               struct media_owner *owner)
+ {
+@@ -1315,17 +1350,19 @@ static guint resume_bap(struct media_transport 
*transport,
+ 
+       if (!bap->stream)
+               return 0;
++      if (bap->resume_id)
++              return 0;
+ 
+       bap_update_links(transport);
+ 
+       switch (bt_bap_stream_get_state(bap->stream)) {
+       case BT_BAP_STREAM_STATE_ENABLING:
+               bap_enable_complete(bap->stream, 0x00, 0x00, owner);
+-              if (owner->pending)
+-                      return owner->pending->id;
+-              return 0;
++              bap->resume_id = g_idle_add(bap_resume_wait_cb, transport);
++              return bap->resume_id;
+       case BT_BAP_STREAM_STATE_STREAMING:
+-              return g_idle_add(resume_complete, transport);
++              bap->resume_id = g_idle_add(bap_resume_complete_cb, transport);
++              return bap->resume_id;
+       }
+ 
+       meta = bt_bap_stream_get_metadata(bap->stream);
+@@ -1389,6 +1426,12 @@ static void cancel_bap(struct media_transport 
*transport, guint id)
+ {
+       struct bap_transport *bap = transport->data;
+ 
++      if (id == bap->resume_id && bap->resume_id) {
++              g_source_remove(bap->resume_id);
++              bap->resume_id = 0;
++              return;
++      }
++
+       if (!bap->stream)
+               return;
+ 
+@@ -1491,7 +1534,7 @@ static void bap_state_changed(struct bt_bap_stream 
*stream, uint8_t old_state,
+       transport_update_playing(transport, TRUE);
+ 
+ done:
+-      resume_complete(transport);
++      bap_resume_complete(transport);
+ }
+ 
+ static void bap_connecting(struct bt_bap_stream *stream, bool state, int fd,
+-- 
+cgit 
+

diff --git a/net-wireless/bluez/files/bluez-5.68-heap-use-after-free.patch 
b/net-wireless/bluez/files/bluez-5.68-heap-use-after-free.patch
new file mode 100644
index 000000000000..3cc6733634a5
--- /dev/null
+++ b/net-wireless/bluez/files/bluez-5.68-heap-use-after-free.patch
@@ -0,0 +1,41 @@
+From b741460688925448807c7532fe96182e4d32ec42 Mon Sep 17 00:00:00 2001
+From: Vlad Pruteanu <[email protected]>
+Date: Tue, 4 Jul 2023 08:56:43 +0300
+Subject: plugins/admin: Fix heap-use-after-free when using 2 controllers
+
+This commit fixes the heap-use-after-free error when connecting 2
+controllers. When a controller is connected
+admin_policy_adapter_probe is called. If policy_data was already
+allocated it gets freed, if not, it only gets allocated. Eventually
+add_interface is called. Here policy_data is put in the "data" variable
+(specific for each controller) and the process_changes task is called
+with idle priority. This function ultimately accesses policy_data from
+the "data" variable.
+
+When Bluez crashes the flow is:
+1)first controller is attached
+2)admin_policy_adapter_probe is called and policy_data is allocated
+4)second controller is attached
+5)admin_policy_adapter_probe is called and policy_data is freed, then
+allocated again
+6)process_changes runs and the policy_data for the first controller is
+read, but it was already freed, thus the crash
+---
+ plugins/admin.c | 1 -
+ 1 file changed, 1 deletion(-)
+
+diff --git a/plugins/admin.c b/plugins/admin.c
+index 68e9237d3f..16b74cfbf0 100644
+--- a/plugins/admin.c
++++ b/plugins/admin.c
+@@ -502,7 +502,6 @@ static int admin_policy_adapter_probe(struct btd_adapter 
*adapter)
+       if (policy_data) {
+               btd_warn(policy_data->adapter_id,
+                                               "Policy data already exists");
+-              admin_policy_free(policy_data);
+               policy_data = NULL;
+       }
+ 
+-- 
+cgit 
+

diff --git a/net-wireless/bluez/files/bluez-5.68-monitor-decoding.patch 
b/net-wireless/bluez/files/bluez-5.68-monitor-decoding.patch
new file mode 100644
index 000000000000..6d5c6306ccd3
--- /dev/null
+++ b/net-wireless/bluez/files/bluez-5.68-monitor-decoding.patch
@@ -0,0 +1,45 @@
+From f9557931ad361e08f1f1537ea670fedd1dd0138d Mon Sep 17 00:00:00 2001
+From: Claudia Draghicescu <[email protected]>
+Date: Tue, 4 Jul 2023 10:41:44 +0300
+Subject: monitor: Add decoding support for Sync Receiver events
+
+This commit adds decoding support for PA Sync Established,
+BIG Sync Established and BIG Sync Lost events.
+---
+ lib/mgmt.h       | 3 +++
+ monitor/packet.c | 5 +++++
+ 2 files changed, 8 insertions(+)
+
+diff --git a/lib/mgmt.h b/lib/mgmt.h
+index 062fa02712..8f92b77315 100644
+--- a/lib/mgmt.h
++++ b/lib/mgmt.h
+@@ -1238,6 +1238,9 @@ static const char *mgmt_ev[] = {
+       "Advertisement Monitor Device Lost",
+       "Mesh Packet Found",
+       "Mesh Packet Complete",
++      "PA Sync Established",
++      "BIG Sync Established",
++      "BIG Sync Lost",
+ };
+ 
+ static const char *mgmt_status[] = {
+diff --git a/monitor/packet.c b/monitor/packet.c
+index a871c7f272..f2167fb52a 100644
+--- a/monitor/packet.c
++++ b/monitor/packet.c
+@@ -3093,6 +3093,11 @@ static const struct bitfield_data events_le_table[] = {
+       { 27, "LE Terminate BIG Complete"               },
+       { 28, "LE BIG Sync Estabilished Complete"       },
+       { 29, "LE BIG Sync Lost"                        },
++      { 30, "LE Request Peer SCA Complete"},
++      { 31, "LE Path Loss Threshold"          },
++      { 32, "LE Transmit Power Reporting"     },
++      { 33, "LE BIG Info Advertising Report"  },
++      { 34, "LE Subrate Change"                       },
+       { }
+ };
+ 
+-- 
+cgit 
+

Reply via email to