Bluetooth: Various L2CAP fixes
Subsystems (2)
v5 cover Bluetooth: Various L2CAP fixes
Hi,
Here's a fifth revision with all feedback taken into account (as far as these 8 patches are concerned). There's still the issue of verifying sk locking in l2cap_sock_sendmsg as well as a potentially missing comment for the wait_state function, but even if those fixes are needed they'll end up being in separate patches. So, I'm hoping this patch set could start reaching its conclusion.
Johan
----------------------------------------------------------------
Johan Hedberg (8):
Bluetooth: Remove unused event mask struct
Bluetooth: Fix double error response for l2cap_create_chan_req
Bluetooth: Fix L2CAP error return used for failed channel lookups
Bluetooth: Fix L2CAP Disconnect response for unknown CID
Bluetooth: Fix L2CAP command reject reason
Bluetooth: Fix sending responses to identified L2CAP response packets
Bluetooth: Fix responding to invalid L2CAP signaling commands
Bluetooth: Fix waiting for clearing of BT_SK_SUSPEND flag
include/net/bluetooth/bluetooth.h | 1 + include/net/bluetooth/hci.h | 3 --- net/bluetooth/af_bluetooth.c | 40 ++++++++++++++++++++++++++++ net/bluetooth/l2cap_core.c | 52 +++++++++++++++++++++++++------------ net/bluetooth/l2cap_sock.c | 6 +++++ net/bluetooth/rfcomm/sock.c | 7 ++++- 6 files changed, 89 insertions(+), 20 deletions(-)
PATCH v5 1/8 Bluetooth: Remove unused event mask struct
From: Johan Hedberg <[email protected]>
The struct for HCI_Set_Event_Mask is never used. Instead a local 8-byte array is used for sending this command. Therefore, remove the unnecessary struct definition.
--- include/net/bluetooth/hci.h | 3 --- 1 file changed, 3 deletions(-)
include/net/bluetooth/hci.h +0 -3
@@ -695,9 +695,6 @@ struct hci_cp_sniff_subrate { } __packed;#define HCI_OP_SET_EVENT_MASK 0x0c01 -struct hci_cp_set_event_mask { -__u8 mask[8 ]; -} __packed;#define HCI_OP_RESET 0x0c03
Signature · 1 line
1.8.4.rc3
PATCH v5 2/8 Bluetooth: Fix double error response for l2cap_create_chan_req
In reply to [email protected]From: Johan Hedberg <[email protected]>
When an L2CAP request handler returns non-zero the calling code will send a command reject response. The l2cap_create_chan_req function will in some cases send its own response but then still return a -EFAULT error which would cause two responses to be sent. This patch fixes this by making the function return 0 after sending its own response.
--- net/bluetooth/l2cap_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/bluetooth/l2cap_core.c +1 -1
@@ -4462,7 +4462,7 @@ error:l2cap_send_cmd (conn, cmd->ident,L2CAP_CREATE_CHAN_RSP ,sizeof (rsp), &rsp); -return -EFAULT ; +return 0 ; }static void l2cap_send_move_chan_req (struct l2cap_chan *chan,u8 dest_amp_id)
Signature · 1 line
1.8.4.rc3
PATCH v5 3/8 Bluetooth: Fix L2CAP error return used for failed channel lookups
In reply to [email protected]From: Johan Hedberg <[email protected]>
The EFAULT error should only be used for memory address related errors and ENOENT might be needed for other purposes than invalid CID errors. This patch fixes the l2cap_config_req, l2cap_connect_create_rsp and l2cap_create_channel_req handlers to use the unique EBADSLT error to indicate failed lookups on a given CID.
--- net/bluetooth/l2cap_core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/bluetooth/l2cap_core.c +4 -4
@@ -3884,13 +3884,13 @@ static int l2cap_connect_create_rsp(struct l2cap_conn *conn,if (scid) { chan =__l2cap_get_chan_by_scid (conn, scid);if (!chan) { - err = -EFAULT ; + err = -EBADSLT ; goto unlock; } }else { chan =__l2cap_get_chan_by_ident (conn, cmd->ident);if (!chan) { - err = -EFAULT ; + err = -EBADSLT ; goto unlock; } }
@@ -3978,7 +3978,7 @@ static inline int l2cap_config_req(struct l2cap_conn *conn, chan =l2cap_get_chan_by_scid (conn, dcid);if (!chan) -return -ENOENT ; +return -EBADSLT ;if (chan->state !=BT_CONFIG && chan->state !=BT_CONNECT2 ) {struct l2cap_cmd_rej_cid rej;
@@ -4438,7 +4438,7 @@ static int l2cap_create_channel_req(struct l2cap_conn *conn, hs_hcon =hci_conn_hash_lookup_ba (hdev,AMP_LINK , conn->dst);if (!hs_hcon) {hci_dev_put (hdev); -return -EFAULT ; +return -EBADSLT ; }BT_DBG ("mgr %p bredr_chan %p hs_hcon %p" , mgr, chan, hs_hcon);
Signature · 1 line
1.8.4.rc3
Re: [PATCH v5 3/8] Bluetooth: Fix L2CAP error return used for failed channel lookups
Hi Johan,
Quoted 10 lines
The EFAULT error should only be used for memory address related errors and ENOENT might be needed for other purposes than invalid CID errors. This patch fixes the
l2cap_config_req,l2cap_connect_create_rspandl2cap_create_channel_reqhandlers to use the unique EBADSLT error to indicate failed lookups on a given CID.Signed-off-by Johan Hedberg <[email protected]>---
net/bluetooth/l2cap_core.c| 8 ++++----1 file changed, 4 insertions(+), 4 deletions(-)
Regards
Marcel
PATCH v5 4/8 Bluetooth: Fix L2CAP Disconnect response for unknown CID
In reply to [email protected]From: Johan Hedberg <[email protected]>
If we receive an L2CAP Disconnect Request for an unknown CID we should not just silently drop it but reply with a proper Command Reject response. This patch fixes this by ensuring that the disconnect handler returns a proper error instead of 0 and will cause the function caller to send the right response.
--- net/bluetooth/l2cap_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/bluetooth/l2cap_core.c +1 -1
@@ -4206,7 +4206,7 @@ static inline int l2cap_disconnect_req(struct l2cap_conn *conn, chan =__l2cap_get_chan_by_scid (conn, dcid);if (!chan) {mutex_unlock (&conn->chan_lock); -return 0 ; +return -EBADSLT ; }l2cap_chan_lock (chan);
Signature · 1 line
1.8.4.rc3
Re: [PATCH v5 4/8] Bluetooth: Fix L2CAP Disconnect response for unknown CID
Hi Johan,
Quoted 10 lines
If we receive an L2CAP Disconnect Request for an unknown CID we should not just silently drop it but reply with a proper Command Reject response. This patch fixes this by ensuring that the disconnect handler returns a proper error instead of 0 and will cause the function caller to send the right response.
Signed-off-by Johan Hedberg <[email protected]>---
net/bluetooth/l2cap_core.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
Regards
Marcel
PATCH v5 5/8 Bluetooth: Fix L2CAP command reject reason
In reply to [email protected]From: Johan Hedberg <[email protected]>
There are several possible reason codes that can be sent in the command reject L2CAP packet. Before this patch the code has used a hard-coded single response code ("command not understood"). This patch adds a helper function to map the return value of an L2CAP handler function to the correct command reject reason.
--- net/bluetooth/l2cap_core.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
net/bluetooth/l2cap_core.c +16 -4
@@ -5294,6 +5294,20 @@ static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, } } +static __le16 l2cap_err_to_reason (int err) +{ +switch (err) { +case -EBADSLT : +return __constant_cpu_to_le16 (L2CAP_REJ_INVALID_CID ); +case -EMSGSIZE : +return __constant_cpu_to_le16 (L2CAP_REJ_MTU_EXCEEDED ); +case -EINVAL : +case -EPROTO : +default : +return __constant_cpu_to_le16 (L2CAP_REJ_NOT_UNDERSTOOD ); + } +} +static inline void l2cap_le_sig_channel (struct l2cap_conn *conn,struct sk_buff *skb) {
@@ -5326,8 +5340,7 @@ static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,BT_ERR ("Wrong link type (%d)" , err); -/* FIXME: Map err to a valid reason */ - rej.reason =__constant_cpu_to_le16 (L2CAP_REJ_NOT_UNDERSTOOD ); + rej.reason =l2cap_err_to_reason (err);l2cap_send_cmd (conn, cmd.ident,L2CAP_COMMAND_REJ ,sizeof (rej), &rej); }
@@ -5371,8 +5384,7 @@ static inline void l2cap_sig_channel(struct l2cap_conn *conn,BT_ERR ("Wrong link type (%d)" , err); -/* FIXME: Map err to a valid reason */ - rej.reason =__constant_cpu_to_le16 (L2CAP_REJ_NOT_UNDERSTOOD ); + rej.reason =l2cap_err_to_reason (err);l2cap_send_cmd (conn, cmd.ident,L2CAP_COMMAND_REJ ,sizeof (rej), &rej); }
Signature · 1 line
1.8.4.rc3
Re: [PATCH v5 5/8] Bluetooth: Fix L2CAP command reject reason
Hi Johan,
Quoted 10 lines
There are several possible reason codes that can be sent in the command reject L2CAP packet. Before this patch the code has used a hard-coded single response code ("command not understood"). This patch adds a helper function to map the return value of an L2CAP handler function to the correct command reject reason.
Signed-off-by Johan Hedberg <[email protected]>---
net/bluetooth/l2cap_core.c| 20 ++++++++++++++++----1 file changed, 16 insertions(+), 4 deletions(-)
Regards
Marcel
PATCH v5 6/8 Bluetooth: Fix sending responses to identified L2CAP response packets
In reply to [email protected]From: Johan Hedberg <[email protected]>
When L2CAP packets return a non-zero error and the value is passed onwards by l2cap_bredr_sig_cmd this will trigger a command reject packet to be sent. However, the core specification (page 1416 in core 4.0) says the following: "Command Reject packets should not be sent in response to an identified Response packet.".
This patch ensures that a command reject packet is not sent for any identified response packet by ignoring the error return value from the response handler functions.
--- net/bluetooth/l2cap_core.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
net/bluetooth/l2cap_core.c +6 -6
@@ -5212,7 +5212,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,case L2CAP_CONN_RSP :case L2CAP_CREATE_CHAN_RSP : - err =l2cap_connect_create_rsp (conn, cmd, cmd_len, data); +l2cap_connect_create_rsp (conn, cmd, cmd_len, data);break ;case L2CAP_CONF_REQ :
@@ -5220,7 +5220,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,break ;case L2CAP_CONF_RSP : - err =l2cap_config_rsp (conn, cmd, cmd_len, data); +l2cap_config_rsp (conn, cmd, cmd_len, data);break ;case L2CAP_DISCONN_REQ :
@@ -5228,7 +5228,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,break ;case L2CAP_DISCONN_RSP : - err =l2cap_disconnect_rsp (conn, cmd, cmd_len, data); +l2cap_disconnect_rsp (conn, cmd, cmd_len, data);break ;case L2CAP_ECHO_REQ :
@@ -5243,7 +5243,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,break ;case L2CAP_INFO_RSP : - err =l2cap_information_rsp (conn, cmd, cmd_len, data); +l2cap_information_rsp (conn, cmd, cmd_len, data);break ;case L2CAP_CREATE_CHAN_REQ :
@@ -5255,7 +5255,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,break ;case L2CAP_MOVE_CHAN_RSP : - err =l2cap_move_channel_rsp (conn, cmd, cmd_len, data); +l2cap_move_channel_rsp (conn, cmd, cmd_len, data);break ;case L2CAP_MOVE_CHAN_CFM :
@@ -5263,7 +5263,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,break ;case L2CAP_MOVE_CHAN_CFM_RSP : - err =l2cap_move_channel_confirm_rsp (conn, cmd, cmd_len, data); +l2cap_move_channel_confirm_rsp (conn, cmd, cmd_len, data);break ;default :
Signature · 1 line
1.8.4.rc3
PATCH v5 7/8 Bluetooth: Fix responding to invalid L2CAP signaling commands
In reply to [email protected]From: Johan Hedberg <[email protected]>
When we have an LE link we should not respond to any data on the BR/EDR L2CAP signaling channel (0x0001) and vice-versa when we have a BR/EDR link we should not respond to LE L2CAP (CID 0x0005) signaling commands. This patch fixes this issue by checking for a valid link type and ignores data if it is wrong.
--- net/bluetooth/l2cap_core.c | 8 ++++++++ 1 file changed, 8 insertions(+)
net/bluetooth/l2cap_core.c +8 -0
@@ -5311,6 +5311,7 @@ static __le16 l2cap_err_to_reason(int err)static inline void l2cap_le_sig_channel (struct l2cap_conn *conn,struct sk_buff *skb) { +struct hci_conn *hcon = conn->hcon;u8 *data = skb->data;int len = skb->len;struct l2cap_cmd_hdr cmd;
@@ -5318,6 +5319,9 @@ static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,l2cap_raw_recv (conn, skb); +if (hcon->type !=LE_LINK ) +return ; +while (len >=L2CAP_CMD_HDR_SIZE ) {u16 cmd_len;memcpy (&cmd, data,L2CAP_CMD_HDR_SIZE );
@@ -5355,6 +5359,7 @@ static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,static inline void l2cap_sig_channel (struct l2cap_conn *conn,struct sk_buff *skb) { +struct hci_conn *hcon = conn->hcon;u8 *data = skb->data;int len = skb->len;struct l2cap_cmd_hdr cmd;
@@ -5362,6 +5367,9 @@ static inline void l2cap_sig_channel(struct l2cap_conn *conn,l2cap_raw_recv (conn, skb); +if (hcon->type !=ACL_LINK ) +return ; +while (len >=L2CAP_CMD_HDR_SIZE ) {u16 cmd_len;memcpy (&cmd, data,L2CAP_CMD_HDR_SIZE );
Signature · 1 line
1.8.4.rc3
PATCH v5 8/8 Bluetooth: Fix waiting for clearing of BT_SK_SUSPEND flag
In reply to [email protected]From: Johan Hedberg <[email protected]>
In the case of blocking sockets we should not proceed with sendmsg() if the socket has the BT_SK_SUSPEND flag set. So far the code was only ensuring that POLLOUT doesn't get set for non-blocking sockets using poll() but there was no code in place to ensure that blocking sockets do the right thing when writing to them.
This patch adds a new bt_sock_wait_ready helper function to sleep in the sendmsg call if the BT_SK_SUSPEND flag is set, and wake up as soon as it is unset. It also updates the L2CAP and RFCOMM sendmsg callbacks to take advantage of this new helper function.
--- include/net/bluetooth/bluetooth.h | 1 + net/bluetooth/af_bluetooth.c | 40 +++++++++++++++++++++++++++++++++++++++ net/bluetooth/l2cap_sock.c | 6 ++++++ net/bluetooth/rfcomm/sock.c | 7 ++++++- 4 files changed, 53 insertions(+), 1 deletion(-)
include/net/bluetooth/bluetooth.h +1 -0
@@ -249,6 +249,7 @@ int bt_sock_stream_recvmsg(struct kiocb *iocb, struct socket *sock,uint bt_sock_poll (struct file *file,struct socket *sock,poll_table *wait);int bt_sock_ioctl (struct socket *sock,unsigned int cmd,unsigned long arg);int bt_sock_wait_state (struct sock *sk,int state,unsigned long timeo); +int bt_sock_wait_ready (struct sock *sk,unsigned long flags);void bt_accept_enqueue (struct sock *parent,struct sock *sk);void bt_accept_unlink (struct sock *sk);
net/bluetooth/af_bluetooth.c +40 -0
@@ -525,6 +525,46 @@ int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo) }EXPORT_SYMBOL (bt_sock_wait_state); +/* This function expects the sk lock to be held when called */ +int bt_sock_wait_ready (struct sock *sk,unsigned long flags) +{ +DECLARE_WAITQUEUE (wait, current); +unsigned long timeo; +int err =0 ; + +BT_DBG ("sk %p" , sk); + + timeo =sock_sndtimeo (sk, flags &O_NONBLOCK ); + +add_wait_queue (sk_sleep (sk), &wait); +set_current_state (TASK_INTERRUPTIBLE ); +while (test_bit (BT_SK_SUSPEND , &bt_sk (sk)->flags)) { +if (!timeo) { + err = -EAGAIN ; +break ; + } + +if (signal_pending (current)) { + err =sock_intr_errno (timeo); +break ; + } + +release_sock (sk); + timeo =schedule_timeout (timeo); +lock_sock (sk); +set_current_state (TASK_INTERRUPTIBLE ); + + err =sock_error (sk); +if (err) +break ; + } +__set_current_state (TASK_RUNNING ); +remove_wait_queue (sk_sleep (sk), &wait); + +return err; +} +EXPORT_SYMBOL (bt_sock_wait_ready); + #ifdef CONFIG_PROC_FS struct bt_seq_state { struct bt_sock_list *l;
net/bluetooth/l2cap_sock.c +6 -0
@@ -777,6 +777,12 @@ static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock,if (sk->sk_state !=BT_CONNECTED )return -ENOTCONN ; +lock_sock (sk); + err =bt_sock_wait_ready (sk, msg->msg_flags); +release_sock (sk); +if (err) +return err; +l2cap_chan_lock (chan); err =l2cap_chan_send (chan, msg, len, sk->sk_priority);l2cap_chan_unlock (chan);
net/bluetooth/rfcomm/sock.c +6 -1
@@ -544,7 +544,7 @@ static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock,struct sock *sk = sock->sk;struct rfcomm_dlc *d =rfcomm_pi (sk)->dlc;struct sk_buff *skb; -int sent =0 ; +int sent;if (test_bit (RFCOMM_DEFER_SETUP , &d->flags))return -ENOTCONN ;
@@ -559,6 +559,10 @@ static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock,lock_sock (sk); + sent =bt_sock_wait_ready (sk, msg->msg_flags); +if (sent) + goto done; +while (len) {size_t size =min_t (size_t, len, d->mtu);int err;
@@ -594,6 +598,7 @@ static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock, len -= size; } +done:release_sock (sk);return sent;
Signature · 1 line
1.8.4.rc3
Re: [PATCH v5 8/8] Bluetooth: Fix waiting for clearing of BT_SK_SUSPEND flag
Hi Johan,
Quoted 11 lines
In the case of blocking sockets we should not proceed with
sendmsg()if the socket has theBT_SK_SUSPENDflag set. So far the code was only ensuring that POLLOUT doesn't get set for non-blocking sockets usingpoll()but there was no code in place to ensure that blocking sockets do the right thing when writing to them.This patch adds a new
bt_sock_wait_readyhelper function to sleep in the sendmsg call if theBT_SK_SUSPENDflag is set, and wake up as soon as it is unset. It also updates the L2CAP and RFCOMM sendmsg callbacks to take advantage of this new helper function.
Quoted 7 lines
Signed-off-by Johan Hedberg <[email protected]>---
include/net/bluetooth/bluetooth.h | 1 + net/bluetooth/af_bluetooth.c | 40 +++++++++++++++++++++++++++++++++++++++ net/bluetooth/l2cap_sock.c | 6 ++++++ net/bluetooth/rfcomm/sock.c | 7 ++++++- 4 files changed, 53 insertions(+), 1 deletion(-)
Regards
Marcel
Re: [PATCH v5 8/8] Bluetooth: Fix waiting for clearing of BT_SK_SUSPEND flag
In reply to [email protected]Hi Johan,
2013-09-16 [email protected] <[email protected]>:
Quoted 13 lines
From: Johan Hedberg <[email protected]>
In the case of blocking sockets we should not proceed with
sendmsg()if the socket has theBT_SK_SUSPENDflag set. So far the code was only ensuring that POLLOUT doesn't get set for non-blocking sockets usingpoll()but there was no code in place to ensure that blocking sockets do the right thing when writing to them.This patch adds a new
bt_sock_wait_readyhelper function to sleep in the sendmsg call if theBT_SK_SUSPENDflag is set, and wake up as soon as it is unset. It also updates the L2CAP and RFCOMM sendmsg callbacks to take advantage of this new helper function.
Quoted 7 lines
Signed-off-by Johan Hedberg <[email protected]>--- include/net/bluetooth/bluetooth.h | 1 + net/bluetooth/af_bluetooth.c | 40 +++++++++++++++++++++++++++++++++++++++ net/bluetooth/l2cap_sock.c | 6 ++++++ net/bluetooth/rfcomm/sock.c | 7 ++++++- 4 files changed, 53 insertions(+), 1 deletion(-)
All patches have been applied to bluetooth-next. Thanks.
Gustavo