Skip to content

Commit a0c22f2

Browse files
committed
[Bluetooth] Move pending packets from RFCOMM socket to TTY
When an incoming RFCOMM socket connection gets converted into a TTY, it can happen that packets are lost. This mainly happens with the Handsfree profile where the remote side starts sending data right away. The problem is that these packets are in the socket receive queue. So when creating the TTY make sure to copy all pending packets from the socket receive queue to a private queue inside the TTY. To make this actually work, the flow control on the newly created TTY will be disabled and only enabled again when the TTY is opened by an application. And right before that, the pending packets will be put into the TTY flip buffer. Signed-off-by: Denis Kenzior <denis.kenzior@trolltech.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
1 parent 8b6b3da commit a0c22f2

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎net/bluetooth/rfcomm/core.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
#define BT_DBG(D...)
5454
#endif
5555

56-
#define VERSION "1.9"
56+
#define VERSION "1.10"
5757

5858
static int disable_cfc = 0;
5959
static int channel_mtu = -1;

‎net/bluetooth/rfcomm/tty.c‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ struct rfcomm_dev {
7777
struct device *tty_dev;
7878

7979
atomic_t wmem_alloc;
80+
81+
struct sk_buff_head pending;
8082
};
8183

8284
static LIST_HEAD(rfcomm_dev_list);
@@ -264,7 +266,25 @@ static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
264266
init_waitqueue_head(&dev->wait);
265267
tasklet_init(&dev->wakeup_task, rfcomm_tty_wakeup, (unsigned long) dev);
266268

269+
skb_queue_head_init(&dev->pending);
270+
267271
rfcomm_dlc_lock(dlc);
272+
273+
if (req->flags & (1 << RFCOMM_REUSE_DLC)) {
274+
struct sock *sk = dlc->owner;
275+
struct sk_buff *skb;
276+
277+
BUG_ON(!sk);
278+
279+
rfcomm_dlc_throttle(dlc);
280+
281+
while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
282+
skb_orphan(skb);
283+
skb_queue_tail(&dev->pending, skb);
284+
atomic_sub(skb->len, &sk->sk_rmem_alloc);
285+
}
286+
}
287+
268288
dlc->data_ready = rfcomm_dev_data_ready;
269289
dlc->state_change = rfcomm_dev_state_change;
270290
dlc->modem_status = rfcomm_dev_modem_status;
@@ -542,11 +562,16 @@ static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
542562
struct rfcomm_dev *dev = dlc->owner;
543563
struct tty_struct *tty;
544564

545-
if (!dev || !(tty = dev->tty)) {
565+
if (!dev) {
546566
kfree_skb(skb);
547567
return;
548568
}
549569

570+
if (!(tty = dev->tty) || !skb_queue_empty(&dev->pending)) {
571+
skb_queue_tail(&dev->pending, skb);
572+
return;
573+
}
574+
550575
BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
551576

552577
tty_insert_flip_string(tty, skb->data, skb->len);
@@ -630,6 +655,30 @@ static void rfcomm_tty_wakeup(unsigned long arg)
630655
#endif
631656
}
632657

658+
static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
659+
{
660+
struct tty_struct *tty = dev->tty;
661+
struct sk_buff *skb;
662+
int inserted = 0;
663+
664+
if (!tty)
665+
return;
666+
667+
BT_DBG("dev %p tty %p", dev, tty);
668+
669+
rfcomm_dlc_lock(dev->dlc);
670+
671+
while ((skb = skb_dequeue(&dev->pending))) {
672+
inserted += tty_insert_flip_string(tty, skb->data, skb->len);
673+
kfree_skb(skb);
674+
}
675+
676+
rfcomm_dlc_unlock(dev->dlc);
677+
678+
if (inserted > 0)
679+
tty_flip_buffer_push(tty);
680+
}
681+
633682
static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
634683
{
635684
DECLARE_WAITQUEUE(wait, current);
@@ -694,6 +743,10 @@ static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
694743
if (err == 0)
695744
device_move(dev->tty_dev, rfcomm_get_device(dev));
696745

746+
rfcomm_tty_copy_pending(dev);
747+
748+
rfcomm_dlc_unthrottle(dev->dlc);
749+
697750
return err;
698751
}
699752

0 commit comments

Comments
 (0)