YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | RFCOMM implementation for Linux Bluetooth stack (BlueZ). |
| 3 | Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com> |
| 4 | Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org> |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License version 2 as |
| 8 | published by the Free Software Foundation; |
| 9 | |
| 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 11 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 12 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 13 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 14 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 15 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 16 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 19 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 20 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | SOFTWARE IS DISCLAIMED. |
| 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | /* |
| 25 | * Bluetooth RFCOMM core. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | */ |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/errno.h> |
| 30 | #include <linux/kernel.h> |
| 31 | #include <linux/sched.h> |
| 32 | #include <linux/signal.h> |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/wait.h> |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 35 | #include <linux/device.h> |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 36 | #include <linux/debugfs.h> |
| 37 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/net.h> |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 39 | #include <linux/mutex.h> |
Marcel Holtmann | a524ecc | 2007-10-20 21:37:20 +0200 | [diff] [blame] | 40 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 41 | #include <linux/slab.h> |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <net/sock.h> |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 44 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <asm/unaligned.h> |
| 46 | |
| 47 | #include <net/bluetooth/bluetooth.h> |
| 48 | #include <net/bluetooth/hci_core.h> |
| 49 | #include <net/bluetooth/l2cap.h> |
| 50 | #include <net/bluetooth/rfcomm.h> |
| 51 | |
Marcel Holtmann | 5f9018a | 2009-01-16 10:09:50 +0100 | [diff] [blame] | 52 | #define VERSION "1.11" |
Mallikarjuna GB | 2da40bf | 2011-09-06 17:02:36 +0530 | [diff] [blame^] | 53 | /* 1 Byte DLCI, 1 Byte Control filed, 2 Bytes Length, 1 Byte for Credits, |
| 54 | * 1 Byte FCS */ |
| 55 | #define RFCOMM_HDR_SIZE 6 |
Marcel Holtmann | 56f3a40 | 2006-02-13 11:39:57 +0100 | [diff] [blame] | 56 | |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 57 | static int disable_cfc; |
| 58 | static int l2cap_ertm; |
Marcel Holtmann | 98bcd08 | 2006-07-14 11:42:12 +0200 | [diff] [blame] | 59 | static int channel_mtu = -1; |
Marcel Holtmann | 56f3a40 | 2006-02-13 11:39:57 +0100 | [diff] [blame] | 60 | static unsigned int l2cap_mtu = RFCOMM_MAX_L2CAP_MTU; |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static struct task_struct *rfcomm_thread; |
| 63 | |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 64 | static DEFINE_MUTEX(rfcomm_mutex); |
| 65 | #define rfcomm_lock() mutex_lock(&rfcomm_mutex) |
| 66 | #define rfcomm_unlock() mutex_unlock(&rfcomm_mutex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 68 | static unsigned long rfcomm_event; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | static LIST_HEAD(session_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len); |
| 73 | static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci); |
| 74 | static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci); |
| 75 | static int rfcomm_queue_disc(struct rfcomm_dlc *d); |
| 76 | static int rfcomm_send_nsc(struct rfcomm_session *s, int cr, u8 type); |
| 77 | static int rfcomm_send_pn(struct rfcomm_session *s, int cr, struct rfcomm_dlc *d); |
| 78 | static int rfcomm_send_msc(struct rfcomm_session *s, int cr, u8 dlci, u8 v24_sig); |
| 79 | static int rfcomm_send_test(struct rfcomm_session *s, int cr, u8 *pattern, int len); |
| 80 | static int rfcomm_send_credits(struct rfcomm_session *s, u8 addr, u8 credits); |
| 81 | static void rfcomm_make_uih(struct sk_buff *skb, u8 addr); |
| 82 | |
| 83 | static void rfcomm_process_connect(struct rfcomm_session *s); |
| 84 | |
Luiz Augusto von Dentz | 63ce090 | 2010-08-19 14:06:10 +0300 | [diff] [blame] | 85 | static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src, |
| 86 | bdaddr_t *dst, |
| 87 | u8 sec_level, |
| 88 | int *err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | static struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst); |
| 90 | static void rfcomm_session_del(struct rfcomm_session *s); |
| 91 | |
| 92 | /* ---- RFCOMM frame parsing macros ---- */ |
| 93 | #define __get_dlci(b) ((b & 0xfc) >> 2) |
| 94 | #define __get_channel(b) ((b & 0xf8) >> 3) |
| 95 | #define __get_dir(b) ((b & 0x04) >> 2) |
| 96 | #define __get_type(b) ((b & 0xef)) |
| 97 | |
| 98 | #define __test_ea(b) ((b & 0x01)) |
| 99 | #define __test_cr(b) ((b & 0x02)) |
| 100 | #define __test_pf(b) ((b & 0x10)) |
| 101 | |
| 102 | #define __addr(cr, dlci) (((dlci & 0x3f) << 2) | (cr << 1) | 0x01) |
| 103 | #define __ctrl(type, pf) (((type & 0xef) | (pf << 4))) |
| 104 | #define __dlci(dir, chn) (((chn & 0x1f) << 1) | dir) |
| 105 | #define __srv_channel(dlci) (dlci >> 1) |
| 106 | #define __dir(dlci) (dlci & 0x01) |
| 107 | |
| 108 | #define __len8(len) (((len) << 1) | 1) |
| 109 | #define __len16(len) ((len) << 1) |
| 110 | |
| 111 | /* MCC macros */ |
| 112 | #define __mcc_type(cr, type) (((type << 2) | (cr << 1) | 0x01)) |
| 113 | #define __get_mcc_type(b) ((b & 0xfc) >> 2) |
| 114 | #define __get_mcc_len(b) ((b & 0xfe) >> 1) |
| 115 | |
| 116 | /* RPN macros */ |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 117 | #define __rpn_line_settings(data, stop, parity) ((data & 0x3) | ((stop & 0x1) << 2) | ((parity & 0x7) << 3)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | #define __get_rpn_data_bits(line) ((line) & 0x3) |
| 119 | #define __get_rpn_stop_bits(line) (((line) >> 2) & 0x1) |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 120 | #define __get_rpn_parity(line) (((line) >> 3) & 0x7) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 122 | static inline void rfcomm_schedule(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | if (!rfcomm_thread) |
| 125 | return; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 126 | set_bit(RFCOMM_SCHED_WAKEUP, &rfcomm_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | wake_up_process(rfcomm_thread); |
| 128 | } |
| 129 | |
| 130 | static inline void rfcomm_session_put(struct rfcomm_session *s) |
| 131 | { |
| 132 | if (atomic_dec_and_test(&s->refcnt)) |
| 133 | rfcomm_session_del(s); |
| 134 | } |
| 135 | |
| 136 | /* ---- RFCOMM FCS computation ---- */ |
| 137 | |
Marcel Holtmann | 408c1ce | 2005-10-28 19:20:36 +0200 | [diff] [blame] | 138 | /* reversed, 8-bit, poly=0x07 */ |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 139 | static unsigned char rfcomm_crc_table[256] = { |
Marcel Holtmann | 408c1ce | 2005-10-28 19:20:36 +0200 | [diff] [blame] | 140 | 0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75, |
| 141 | 0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b, |
| 142 | 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69, |
| 143 | 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67, |
| 144 | |
| 145 | 0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d, |
| 146 | 0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43, |
| 147 | 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51, |
| 148 | 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f, |
| 149 | |
| 150 | 0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05, |
| 151 | 0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b, |
| 152 | 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19, |
| 153 | 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17, |
| 154 | |
| 155 | 0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d, |
| 156 | 0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33, |
| 157 | 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21, |
| 158 | 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f, |
| 159 | |
| 160 | 0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95, |
| 161 | 0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b, |
| 162 | 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89, |
| 163 | 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87, |
| 164 | |
| 165 | 0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad, |
| 166 | 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3, |
| 167 | 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1, |
| 168 | 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf, |
| 169 | |
| 170 | 0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5, |
| 171 | 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb, |
| 172 | 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9, |
| 173 | 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7, |
| 174 | |
| 175 | 0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd, |
| 176 | 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3, |
| 177 | 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1, |
| 178 | 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf |
| 179 | }; |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /* CRC on 2 bytes */ |
| 182 | #define __crc(data) (rfcomm_crc_table[rfcomm_crc_table[0xff ^ data[0]] ^ data[1]]) |
| 183 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 184 | /* FCS on 2 bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | static inline u8 __fcs(u8 *data) |
| 186 | { |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 187 | return 0xff - __crc(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 190 | /* FCS on 3 bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | static inline u8 __fcs2(u8 *data) |
| 192 | { |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 193 | return 0xff - rfcomm_crc_table[__crc(data) ^ data[2]]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* Check FCS */ |
| 197 | static inline int __check_fcs(u8 *data, int type, u8 fcs) |
| 198 | { |
| 199 | u8 f = __crc(data); |
| 200 | |
| 201 | if (type != RFCOMM_UIH) |
| 202 | f = rfcomm_crc_table[f ^ data[2]]; |
| 203 | |
| 204 | return rfcomm_crc_table[f ^ fcs] != 0xcf; |
| 205 | } |
| 206 | |
| 207 | /* ---- L2CAP callbacks ---- */ |
| 208 | static void rfcomm_l2state_change(struct sock *sk) |
| 209 | { |
| 210 | BT_DBG("%p state %d", sk, sk->sk_state); |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 211 | rfcomm_schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void rfcomm_l2data_ready(struct sock *sk, int bytes) |
| 215 | { |
| 216 | BT_DBG("%p bytes %d", sk, bytes); |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 217 | rfcomm_schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static int rfcomm_l2sock_create(struct socket **sock) |
| 221 | { |
| 222 | int err; |
| 223 | |
| 224 | BT_DBG(""); |
| 225 | |
| 226 | err = sock_create_kern(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP, sock); |
| 227 | if (!err) { |
| 228 | struct sock *sk = (*sock)->sk; |
| 229 | sk->sk_data_ready = rfcomm_l2data_ready; |
| 230 | sk->sk_state_change = rfcomm_l2state_change; |
| 231 | } |
| 232 | return err; |
| 233 | } |
| 234 | |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 235 | static inline int rfcomm_check_security(struct rfcomm_dlc *d) |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 236 | { |
| 237 | struct sock *sk = d->session->sock->sk; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 238 | __u8 auth_type; |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 239 | |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 240 | switch (d->sec_level) { |
| 241 | case BT_SECURITY_HIGH: |
| 242 | auth_type = HCI_AT_GENERAL_BONDING_MITM; |
| 243 | break; |
| 244 | case BT_SECURITY_MEDIUM: |
| 245 | auth_type = HCI_AT_GENERAL_BONDING; |
| 246 | break; |
| 247 | default: |
| 248 | auth_type = HCI_AT_NO_BONDING; |
| 249 | break; |
| 250 | } |
| 251 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 252 | return hci_conn_security(l2cap_pi(sk)->conn->hcon, d->sec_level, |
| 253 | auth_type); |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 254 | } |
| 255 | |
Luiz Augusto von Dentz | 9e726b1 | 2009-07-15 13:50:58 -0300 | [diff] [blame] | 256 | static void rfcomm_session_timeout(unsigned long arg) |
| 257 | { |
| 258 | struct rfcomm_session *s = (void *) arg; |
| 259 | |
| 260 | BT_DBG("session %p state %ld", s, s->state); |
| 261 | |
| 262 | set_bit(RFCOMM_TIMED_OUT, &s->flags); |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 263 | rfcomm_schedule(); |
Luiz Augusto von Dentz | 9e726b1 | 2009-07-15 13:50:58 -0300 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static void rfcomm_session_set_timer(struct rfcomm_session *s, long timeout) |
| 267 | { |
| 268 | BT_DBG("session %p state %ld timeout %ld", s, s->state, timeout); |
| 269 | |
| 270 | if (!mod_timer(&s->timer, jiffies + timeout)) |
| 271 | rfcomm_session_hold(s); |
| 272 | } |
| 273 | |
| 274 | static void rfcomm_session_clear_timer(struct rfcomm_session *s) |
| 275 | { |
| 276 | BT_DBG("session %p state %ld", s, s->state); |
| 277 | |
| 278 | if (timer_pending(&s->timer) && del_timer(&s->timer)) |
| 279 | rfcomm_session_put(s); |
| 280 | } |
| 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | /* ---- RFCOMM DLCs ---- */ |
| 283 | static void rfcomm_dlc_timeout(unsigned long arg) |
| 284 | { |
| 285 | struct rfcomm_dlc *d = (void *) arg; |
| 286 | |
| 287 | BT_DBG("dlc %p state %ld", d, d->state); |
| 288 | |
| 289 | set_bit(RFCOMM_TIMED_OUT, &d->flags); |
| 290 | rfcomm_dlc_put(d); |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 291 | rfcomm_schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static void rfcomm_dlc_set_timer(struct rfcomm_dlc *d, long timeout) |
| 295 | { |
| 296 | BT_DBG("dlc %p state %ld timeout %ld", d, d->state, timeout); |
| 297 | |
| 298 | if (!mod_timer(&d->timer, jiffies + timeout)) |
| 299 | rfcomm_dlc_hold(d); |
| 300 | } |
| 301 | |
| 302 | static void rfcomm_dlc_clear_timer(struct rfcomm_dlc *d) |
| 303 | { |
| 304 | BT_DBG("dlc %p state %ld", d, d->state); |
| 305 | |
| 306 | if (timer_pending(&d->timer) && del_timer(&d->timer)) |
| 307 | rfcomm_dlc_put(d); |
| 308 | } |
| 309 | |
| 310 | static void rfcomm_dlc_clear_state(struct rfcomm_dlc *d) |
| 311 | { |
| 312 | BT_DBG("%p", d); |
| 313 | |
| 314 | d->state = BT_OPEN; |
| 315 | d->flags = 0; |
| 316 | d->mscex = 0; |
Johan Hedberg | 183f732 | 2010-12-06 15:56:17 +0200 | [diff] [blame] | 317 | d->sec_level = BT_SECURITY_LOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | d->mtu = RFCOMM_DEFAULT_MTU; |
| 319 | d->v24_sig = RFCOMM_V24_RTC | RFCOMM_V24_RTR | RFCOMM_V24_DV; |
| 320 | |
| 321 | d->cfc = RFCOMM_CFC_DISABLED; |
| 322 | d->rx_credits = RFCOMM_DEFAULT_CREDITS; |
| 323 | } |
| 324 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 325 | struct rfcomm_dlc *rfcomm_dlc_alloc(gfp_t prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 327 | struct rfcomm_dlc *d = kzalloc(sizeof(*d), prio); |
| 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | if (!d) |
| 330 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 332 | setup_timer(&d->timer, rfcomm_dlc_timeout, (unsigned long)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | skb_queue_head_init(&d->tx_queue); |
| 335 | spin_lock_init(&d->lock); |
| 336 | atomic_set(&d->refcnt, 1); |
| 337 | |
| 338 | rfcomm_dlc_clear_state(d); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | BT_DBG("%p", d); |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | return d; |
| 343 | } |
| 344 | |
| 345 | void rfcomm_dlc_free(struct rfcomm_dlc *d) |
| 346 | { |
| 347 | BT_DBG("%p", d); |
| 348 | |
| 349 | skb_queue_purge(&d->tx_queue); |
| 350 | kfree(d); |
| 351 | } |
| 352 | |
| 353 | static void rfcomm_dlc_link(struct rfcomm_session *s, struct rfcomm_dlc *d) |
| 354 | { |
| 355 | BT_DBG("dlc %p session %p", d, s); |
| 356 | |
| 357 | rfcomm_session_hold(s); |
| 358 | |
Luiz Augusto von Dentz | 9e726b1 | 2009-07-15 13:50:58 -0300 | [diff] [blame] | 359 | rfcomm_session_clear_timer(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | rfcomm_dlc_hold(d); |
| 361 | list_add(&d->list, &s->dlcs); |
| 362 | d->session = s; |
| 363 | } |
| 364 | |
| 365 | static void rfcomm_dlc_unlink(struct rfcomm_dlc *d) |
| 366 | { |
| 367 | struct rfcomm_session *s = d->session; |
| 368 | |
| 369 | BT_DBG("dlc %p refcnt %d session %p", d, atomic_read(&d->refcnt), s); |
| 370 | |
| 371 | list_del(&d->list); |
| 372 | d->session = NULL; |
| 373 | rfcomm_dlc_put(d); |
| 374 | |
Luiz Augusto von Dentz | 9e726b1 | 2009-07-15 13:50:58 -0300 | [diff] [blame] | 375 | if (list_empty(&s->dlcs)) |
| 376 | rfcomm_session_set_timer(s, RFCOMM_IDLE_TIMEOUT); |
| 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | rfcomm_session_put(s); |
| 379 | } |
| 380 | |
| 381 | static struct rfcomm_dlc *rfcomm_dlc_get(struct rfcomm_session *s, u8 dlci) |
| 382 | { |
| 383 | struct rfcomm_dlc *d; |
| 384 | struct list_head *p; |
| 385 | |
| 386 | list_for_each(p, &s->dlcs) { |
| 387 | d = list_entry(p, struct rfcomm_dlc, list); |
| 388 | if (d->dlci == dlci) |
| 389 | return d; |
| 390 | } |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | static int __rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel) |
| 395 | { |
| 396 | struct rfcomm_session *s; |
| 397 | int err = 0; |
| 398 | u8 dlci; |
| 399 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 400 | BT_DBG("dlc %p state %ld %s %s channel %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | d, d->state, batostr(src), batostr(dst), channel); |
| 402 | |
| 403 | if (channel < 1 || channel > 30) |
| 404 | return -EINVAL; |
| 405 | |
| 406 | if (d->state != BT_OPEN && d->state != BT_CLOSED) |
| 407 | return 0; |
| 408 | |
| 409 | s = rfcomm_session_get(src, dst); |
| 410 | if (!s) { |
Luiz Augusto von Dentz | 63ce090 | 2010-08-19 14:06:10 +0300 | [diff] [blame] | 411 | s = rfcomm_session_create(src, dst, d->sec_level, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | if (!s) |
| 413 | return err; |
| 414 | } |
| 415 | |
| 416 | dlci = __dlci(!s->initiator, channel); |
| 417 | |
| 418 | /* Check if DLCI already exists */ |
| 419 | if (rfcomm_dlc_get(s, dlci)) |
| 420 | return -EBUSY; |
| 421 | |
| 422 | rfcomm_dlc_clear_state(d); |
| 423 | |
| 424 | d->dlci = dlci; |
| 425 | d->addr = __addr(s->initiator, dlci); |
| 426 | d->priority = 7; |
| 427 | |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 428 | d->state = BT_CONFIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | rfcomm_dlc_link(s, d); |
| 430 | |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 431 | d->out = 1; |
| 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | d->mtu = s->mtu; |
| 434 | d->cfc = (s->cfc == RFCOMM_CFC_UNKNOWN) ? 0 : s->cfc; |
| 435 | |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 436 | if (s->state == BT_CONNECTED) { |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 437 | if (rfcomm_check_security(d)) |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 438 | rfcomm_send_pn(s, 1, d); |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 439 | else |
| 440 | set_bit(RFCOMM_AUTH_PENDING, &d->flags); |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | rfcomm_dlc_set_timer(d, RFCOMM_CONN_TIMEOUT); |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | int rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel) |
| 449 | { |
| 450 | int r; |
| 451 | |
| 452 | rfcomm_lock(); |
| 453 | |
| 454 | r = __rfcomm_dlc_open(d, src, dst, channel); |
| 455 | |
| 456 | rfcomm_unlock(); |
| 457 | return r; |
| 458 | } |
| 459 | |
| 460 | static int __rfcomm_dlc_close(struct rfcomm_dlc *d, int err) |
| 461 | { |
| 462 | struct rfcomm_session *s = d->session; |
| 463 | if (!s) |
| 464 | return 0; |
| 465 | |
| 466 | BT_DBG("dlc %p state %ld dlci %d err %d session %p", |
| 467 | d, d->state, d->dlci, err, s); |
| 468 | |
| 469 | switch (d->state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | case BT_CONNECT: |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 471 | if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) { |
| 472 | set_bit(RFCOMM_AUTH_REJECT, &d->flags); |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 473 | rfcomm_schedule(); |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 474 | break; |
| 475 | } |
| 476 | /* Fall through */ |
| 477 | |
| 478 | case BT_CONNECTED: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | d->state = BT_DISCONN; |
| 480 | if (skb_queue_empty(&d->tx_queue)) { |
| 481 | rfcomm_send_disc(s, d->dlci); |
| 482 | rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT); |
| 483 | } else { |
| 484 | rfcomm_queue_disc(d); |
| 485 | rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT * 2); |
| 486 | } |
| 487 | break; |
| 488 | |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 489 | case BT_OPEN: |
Marcel Holtmann | 8bf4794 | 2009-02-16 02:59:49 +0100 | [diff] [blame] | 490 | case BT_CONNECT2: |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 491 | if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) { |
| 492 | set_bit(RFCOMM_AUTH_REJECT, &d->flags); |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 493 | rfcomm_schedule(); |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 494 | break; |
| 495 | } |
| 496 | /* Fall through */ |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | default: |
| 499 | rfcomm_dlc_clear_timer(d); |
| 500 | |
| 501 | rfcomm_dlc_lock(d); |
| 502 | d->state = BT_CLOSED; |
Dave Young | 1905f6c | 2008-04-01 23:59:06 -0700 | [diff] [blame] | 503 | d->state_change(d, err); |
Arjan van de Ven | 4c8411f | 2008-05-29 01:32:47 -0700 | [diff] [blame] | 504 | rfcomm_dlc_unlock(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | skb_queue_purge(&d->tx_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | rfcomm_dlc_unlink(d); |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | int rfcomm_dlc_close(struct rfcomm_dlc *d, int err) |
| 514 | { |
| 515 | int r; |
| 516 | |
| 517 | rfcomm_lock(); |
| 518 | |
| 519 | r = __rfcomm_dlc_close(d, err); |
| 520 | |
| 521 | rfcomm_unlock(); |
| 522 | return r; |
| 523 | } |
| 524 | |
| 525 | int rfcomm_dlc_send(struct rfcomm_dlc *d, struct sk_buff *skb) |
| 526 | { |
| 527 | int len = skb->len; |
| 528 | |
| 529 | if (d->state != BT_CONNECTED) |
| 530 | return -ENOTCONN; |
| 531 | |
| 532 | BT_DBG("dlc %p mtu %d len %d", d, d->mtu, len); |
| 533 | |
| 534 | if (len > d->mtu) |
| 535 | return -EINVAL; |
| 536 | |
| 537 | rfcomm_make_uih(skb, d->addr); |
| 538 | skb_queue_tail(&d->tx_queue, skb); |
| 539 | |
| 540 | if (!test_bit(RFCOMM_TX_THROTTLED, &d->flags)) |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 541 | rfcomm_schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | return len; |
| 543 | } |
| 544 | |
Harvey Harrison | b5606c2 | 2008-02-13 15:03:16 -0800 | [diff] [blame] | 545 | void __rfcomm_dlc_throttle(struct rfcomm_dlc *d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | { |
| 547 | BT_DBG("dlc %p state %ld", d, d->state); |
| 548 | |
| 549 | if (!d->cfc) { |
| 550 | d->v24_sig |= RFCOMM_V24_FC; |
| 551 | set_bit(RFCOMM_MSC_PENDING, &d->flags); |
| 552 | } |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 553 | rfcomm_schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Harvey Harrison | b5606c2 | 2008-02-13 15:03:16 -0800 | [diff] [blame] | 556 | void __rfcomm_dlc_unthrottle(struct rfcomm_dlc *d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
| 558 | BT_DBG("dlc %p state %ld", d, d->state); |
| 559 | |
| 560 | if (!d->cfc) { |
| 561 | d->v24_sig &= ~RFCOMM_V24_FC; |
| 562 | set_bit(RFCOMM_MSC_PENDING, &d->flags); |
| 563 | } |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 564 | rfcomm_schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } |
| 566 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 567 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | Set/get modem status functions use _local_ status i.e. what we report |
| 569 | to the other side. |
| 570 | Remote status is provided by dlc->modem_status() callback. |
| 571 | */ |
| 572 | int rfcomm_dlc_set_modem_status(struct rfcomm_dlc *d, u8 v24_sig) |
| 573 | { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 574 | BT_DBG("dlc %p state %ld v24_sig 0x%x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | d, d->state, v24_sig); |
| 576 | |
| 577 | if (test_bit(RFCOMM_RX_THROTTLED, &d->flags)) |
| 578 | v24_sig |= RFCOMM_V24_FC; |
| 579 | else |
| 580 | v24_sig &= ~RFCOMM_V24_FC; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | d->v24_sig = v24_sig; |
| 583 | |
| 584 | if (!test_and_set_bit(RFCOMM_MSC_PENDING, &d->flags)) |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 585 | rfcomm_schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | int rfcomm_dlc_get_modem_status(struct rfcomm_dlc *d, u8 *v24_sig) |
| 591 | { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 592 | BT_DBG("dlc %p state %ld v24_sig 0x%x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | d, d->state, d->v24_sig); |
| 594 | |
| 595 | *v24_sig = d->v24_sig; |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | /* ---- RFCOMM sessions ---- */ |
| 600 | static struct rfcomm_session *rfcomm_session_add(struct socket *sock, int state) |
| 601 | { |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 602 | struct rfcomm_session *s = kzalloc(sizeof(*s), GFP_KERNEL); |
| 603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | if (!s) |
| 605 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | BT_DBG("session %p sock %p", s, sock); |
| 608 | |
Luiz Augusto von Dentz | 9e726b1 | 2009-07-15 13:50:58 -0300 | [diff] [blame] | 609 | setup_timer(&s->timer, rfcomm_session_timeout, (unsigned long) s); |
| 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | INIT_LIST_HEAD(&s->dlcs); |
| 612 | s->state = state; |
| 613 | s->sock = sock; |
| 614 | |
| 615 | s->mtu = RFCOMM_DEFAULT_MTU; |
Marcel Holtmann | 7c2660b | 2006-07-03 10:02:51 +0200 | [diff] [blame] | 616 | s->cfc = disable_cfc ? RFCOMM_CFC_DISABLED : RFCOMM_CFC_UNKNOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
| 618 | /* Do not increment module usage count for listening sessions. |
| 619 | * Otherwise we won't be able to unload the module. */ |
| 620 | if (state != BT_LISTEN) |
| 621 | if (!try_module_get(THIS_MODULE)) { |
| 622 | kfree(s); |
| 623 | return NULL; |
| 624 | } |
| 625 | |
| 626 | list_add(&s->list, &session_list); |
| 627 | |
| 628 | return s; |
| 629 | } |
| 630 | |
| 631 | static void rfcomm_session_del(struct rfcomm_session *s) |
| 632 | { |
| 633 | int state = s->state; |
| 634 | |
| 635 | BT_DBG("session %p state %ld", s, s->state); |
| 636 | |
| 637 | list_del(&s->list); |
| 638 | |
| 639 | if (state == BT_CONNECTED) |
| 640 | rfcomm_send_disc(s, 0); |
| 641 | |
Luiz Augusto von Dentz | 9e726b1 | 2009-07-15 13:50:58 -0300 | [diff] [blame] | 642 | rfcomm_session_clear_timer(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | sock_release(s->sock); |
| 644 | kfree(s); |
| 645 | |
| 646 | if (state != BT_LISTEN) |
| 647 | module_put(THIS_MODULE); |
| 648 | } |
| 649 | |
| 650 | static struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst) |
| 651 | { |
| 652 | struct rfcomm_session *s; |
| 653 | struct list_head *p, *n; |
| 654 | struct bt_sock *sk; |
| 655 | list_for_each_safe(p, n, &session_list) { |
| 656 | s = list_entry(p, struct rfcomm_session, list); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 657 | sk = bt_sk(s->sock->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
| 659 | if ((!bacmp(src, BDADDR_ANY) || !bacmp(&sk->src, src)) && |
| 660 | !bacmp(&sk->dst, dst)) |
| 661 | return s; |
| 662 | } |
| 663 | return NULL; |
| 664 | } |
| 665 | |
| 666 | static void rfcomm_session_close(struct rfcomm_session *s, int err) |
| 667 | { |
| 668 | struct rfcomm_dlc *d; |
| 669 | struct list_head *p, *n; |
| 670 | |
| 671 | BT_DBG("session %p state %ld err %d", s, s->state, err); |
| 672 | |
| 673 | rfcomm_session_hold(s); |
| 674 | |
| 675 | s->state = BT_CLOSED; |
| 676 | |
| 677 | /* Close all dlcs */ |
| 678 | list_for_each_safe(p, n, &s->dlcs) { |
| 679 | d = list_entry(p, struct rfcomm_dlc, list); |
| 680 | d->state = BT_CLOSED; |
| 681 | __rfcomm_dlc_close(d, err); |
| 682 | } |
| 683 | |
Luiz Augusto von Dentz | 9e726b1 | 2009-07-15 13:50:58 -0300 | [diff] [blame] | 684 | rfcomm_session_clear_timer(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | rfcomm_session_put(s); |
| 686 | } |
| 687 | |
Luiz Augusto von Dentz | 63ce090 | 2010-08-19 14:06:10 +0300 | [diff] [blame] | 688 | static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src, |
| 689 | bdaddr_t *dst, |
| 690 | u8 sec_level, |
| 691 | int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | { |
| 693 | struct rfcomm_session *s = NULL; |
| 694 | struct sockaddr_l2 addr; |
| 695 | struct socket *sock; |
| 696 | struct sock *sk; |
| 697 | |
| 698 | BT_DBG("%s %s", batostr(src), batostr(dst)); |
| 699 | |
| 700 | *err = rfcomm_l2sock_create(&sock); |
| 701 | if (*err < 0) |
| 702 | return NULL; |
| 703 | |
| 704 | bacpy(&addr.l2_bdaddr, src); |
| 705 | addr.l2_family = AF_BLUETOOTH; |
| 706 | addr.l2_psm = 0; |
Marcel Holtmann | 37e62f5 | 2009-02-17 21:49:33 +0100 | [diff] [blame] | 707 | addr.l2_cid = 0; |
Marcel Holtmann | 48db9ca | 2007-05-05 00:36:06 +0200 | [diff] [blame] | 708 | *err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | if (*err < 0) |
| 710 | goto failed; |
| 711 | |
| 712 | /* Set L2CAP options */ |
| 713 | sk = sock->sk; |
| 714 | lock_sock(sk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 715 | l2cap_pi(sk)->imtu = l2cap_mtu; |
| 716 | l2cap_pi(sk)->sec_level = sec_level; |
Marcel Holtmann | eae38ee | 2009-10-05 12:23:48 +0200 | [diff] [blame] | 717 | if (l2cap_ertm) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 718 | l2cap_pi(sk)->mode = L2CAP_MODE_ERTM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | release_sock(sk); |
| 720 | |
| 721 | s = rfcomm_session_add(sock, BT_BOUND); |
| 722 | if (!s) { |
| 723 | *err = -ENOMEM; |
| 724 | goto failed; |
| 725 | } |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | s->initiator = 1; |
| 728 | |
| 729 | bacpy(&addr.l2_bdaddr, dst); |
| 730 | addr.l2_family = AF_BLUETOOTH; |
Marcel Holtmann | b4324b5 | 2009-06-07 18:06:51 +0200 | [diff] [blame] | 731 | addr.l2_psm = cpu_to_le16(RFCOMM_PSM); |
Marcel Holtmann | 37e62f5 | 2009-02-17 21:49:33 +0100 | [diff] [blame] | 732 | addr.l2_cid = 0; |
Marcel Holtmann | 48db9ca | 2007-05-05 00:36:06 +0200 | [diff] [blame] | 733 | *err = kernel_connect(sock, (struct sockaddr *) &addr, sizeof(addr), O_NONBLOCK); |
Marcel Holtmann | b4c612a | 2006-09-23 09:54:38 +0200 | [diff] [blame] | 734 | if (*err == 0 || *err == -EINPROGRESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | return s; |
| 736 | |
| 737 | rfcomm_session_del(s); |
| 738 | return NULL; |
| 739 | |
| 740 | failed: |
| 741 | sock_release(sock); |
| 742 | return NULL; |
| 743 | } |
| 744 | |
| 745 | void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src, bdaddr_t *dst) |
| 746 | { |
| 747 | struct sock *sk = s->sock->sk; |
| 748 | if (src) |
| 749 | bacpy(src, &bt_sk(sk)->src); |
| 750 | if (dst) |
| 751 | bacpy(dst, &bt_sk(sk)->dst); |
| 752 | } |
| 753 | |
| 754 | /* ---- RFCOMM frame sending ---- */ |
| 755 | static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len) |
| 756 | { |
| 757 | struct socket *sock = s->sock; |
| 758 | struct kvec iv = { data, len }; |
| 759 | struct msghdr msg; |
| 760 | |
| 761 | BT_DBG("session %p len %d", s, len); |
| 762 | |
| 763 | memset(&msg, 0, sizeof(msg)); |
| 764 | |
| 765 | return kernel_sendmsg(sock, &msg, &iv, 1, len); |
| 766 | } |
| 767 | |
| 768 | static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci) |
| 769 | { |
| 770 | struct rfcomm_cmd cmd; |
| 771 | |
| 772 | BT_DBG("%p dlci %d", s, dlci); |
| 773 | |
| 774 | cmd.addr = __addr(s->initiator, dlci); |
| 775 | cmd.ctrl = __ctrl(RFCOMM_SABM, 1); |
| 776 | cmd.len = __len8(0); |
| 777 | cmd.fcs = __fcs2((u8 *) &cmd); |
| 778 | |
| 779 | return rfcomm_send_frame(s, (void *) &cmd, sizeof(cmd)); |
| 780 | } |
| 781 | |
| 782 | static int rfcomm_send_ua(struct rfcomm_session *s, u8 dlci) |
| 783 | { |
| 784 | struct rfcomm_cmd cmd; |
| 785 | |
| 786 | BT_DBG("%p dlci %d", s, dlci); |
| 787 | |
| 788 | cmd.addr = __addr(!s->initiator, dlci); |
| 789 | cmd.ctrl = __ctrl(RFCOMM_UA, 1); |
| 790 | cmd.len = __len8(0); |
| 791 | cmd.fcs = __fcs2((u8 *) &cmd); |
| 792 | |
| 793 | return rfcomm_send_frame(s, (void *) &cmd, sizeof(cmd)); |
| 794 | } |
| 795 | |
| 796 | static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci) |
| 797 | { |
| 798 | struct rfcomm_cmd cmd; |
| 799 | |
| 800 | BT_DBG("%p dlci %d", s, dlci); |
| 801 | |
| 802 | cmd.addr = __addr(s->initiator, dlci); |
| 803 | cmd.ctrl = __ctrl(RFCOMM_DISC, 1); |
| 804 | cmd.len = __len8(0); |
| 805 | cmd.fcs = __fcs2((u8 *) &cmd); |
| 806 | |
| 807 | return rfcomm_send_frame(s, (void *) &cmd, sizeof(cmd)); |
| 808 | } |
| 809 | |
| 810 | static int rfcomm_queue_disc(struct rfcomm_dlc *d) |
| 811 | { |
| 812 | struct rfcomm_cmd *cmd; |
| 813 | struct sk_buff *skb; |
| 814 | |
| 815 | BT_DBG("dlc %p dlci %d", d, d->dlci); |
| 816 | |
| 817 | skb = alloc_skb(sizeof(*cmd), GFP_KERNEL); |
| 818 | if (!skb) |
| 819 | return -ENOMEM; |
| 820 | |
| 821 | cmd = (void *) __skb_put(skb, sizeof(*cmd)); |
| 822 | cmd->addr = d->addr; |
| 823 | cmd->ctrl = __ctrl(RFCOMM_DISC, 1); |
| 824 | cmd->len = __len8(0); |
| 825 | cmd->fcs = __fcs2((u8 *) cmd); |
| 826 | |
| 827 | skb_queue_tail(&d->tx_queue, skb); |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 828 | rfcomm_schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | static int rfcomm_send_dm(struct rfcomm_session *s, u8 dlci) |
| 833 | { |
| 834 | struct rfcomm_cmd cmd; |
| 835 | |
| 836 | BT_DBG("%p dlci %d", s, dlci); |
| 837 | |
| 838 | cmd.addr = __addr(!s->initiator, dlci); |
| 839 | cmd.ctrl = __ctrl(RFCOMM_DM, 1); |
| 840 | cmd.len = __len8(0); |
| 841 | cmd.fcs = __fcs2((u8 *) &cmd); |
| 842 | |
| 843 | return rfcomm_send_frame(s, (void *) &cmd, sizeof(cmd)); |
| 844 | } |
| 845 | |
| 846 | static int rfcomm_send_nsc(struct rfcomm_session *s, int cr, u8 type) |
| 847 | { |
| 848 | struct rfcomm_hdr *hdr; |
| 849 | struct rfcomm_mcc *mcc; |
| 850 | u8 buf[16], *ptr = buf; |
| 851 | |
| 852 | BT_DBG("%p cr %d type %d", s, cr, type); |
| 853 | |
| 854 | hdr = (void *) ptr; ptr += sizeof(*hdr); |
| 855 | hdr->addr = __addr(s->initiator, 0); |
| 856 | hdr->ctrl = __ctrl(RFCOMM_UIH, 0); |
| 857 | hdr->len = __len8(sizeof(*mcc) + 1); |
| 858 | |
| 859 | mcc = (void *) ptr; ptr += sizeof(*mcc); |
| 860 | mcc->type = __mcc_type(cr, RFCOMM_NSC); |
| 861 | mcc->len = __len8(1); |
| 862 | |
| 863 | /* Type that we didn't like */ |
| 864 | *ptr = __mcc_type(cr, type); ptr++; |
| 865 | |
| 866 | *ptr = __fcs(buf); ptr++; |
| 867 | |
| 868 | return rfcomm_send_frame(s, buf, ptr - buf); |
| 869 | } |
| 870 | |
| 871 | static int rfcomm_send_pn(struct rfcomm_session *s, int cr, struct rfcomm_dlc *d) |
| 872 | { |
| 873 | struct rfcomm_hdr *hdr; |
| 874 | struct rfcomm_mcc *mcc; |
| 875 | struct rfcomm_pn *pn; |
| 876 | u8 buf[16], *ptr = buf; |
| 877 | |
| 878 | BT_DBG("%p cr %d dlci %d mtu %d", s, cr, d->dlci, d->mtu); |
| 879 | |
| 880 | hdr = (void *) ptr; ptr += sizeof(*hdr); |
| 881 | hdr->addr = __addr(s->initiator, 0); |
| 882 | hdr->ctrl = __ctrl(RFCOMM_UIH, 0); |
| 883 | hdr->len = __len8(sizeof(*mcc) + sizeof(*pn)); |
| 884 | |
| 885 | mcc = (void *) ptr; ptr += sizeof(*mcc); |
| 886 | mcc->type = __mcc_type(cr, RFCOMM_PN); |
| 887 | mcc->len = __len8(sizeof(*pn)); |
| 888 | |
| 889 | pn = (void *) ptr; ptr += sizeof(*pn); |
| 890 | pn->dlci = d->dlci; |
| 891 | pn->priority = d->priority; |
| 892 | pn->ack_timer = 0; |
| 893 | pn->max_retrans = 0; |
| 894 | |
| 895 | if (s->cfc) { |
| 896 | pn->flow_ctrl = cr ? 0xf0 : 0xe0; |
| 897 | pn->credits = RFCOMM_DEFAULT_CREDITS; |
| 898 | } else { |
| 899 | pn->flow_ctrl = 0; |
| 900 | pn->credits = 0; |
| 901 | } |
| 902 | |
Marcel Holtmann | 98bcd08 | 2006-07-14 11:42:12 +0200 | [diff] [blame] | 903 | if (cr && channel_mtu >= 0) |
Marcel Holtmann | b4324b5 | 2009-06-07 18:06:51 +0200 | [diff] [blame] | 904 | pn->mtu = cpu_to_le16(channel_mtu); |
Marcel Holtmann | 98bcd08 | 2006-07-14 11:42:12 +0200 | [diff] [blame] | 905 | else |
Marcel Holtmann | b4324b5 | 2009-06-07 18:06:51 +0200 | [diff] [blame] | 906 | pn->mtu = cpu_to_le16(d->mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | |
| 908 | *ptr = __fcs(buf); ptr++; |
| 909 | |
| 910 | return rfcomm_send_frame(s, buf, ptr - buf); |
| 911 | } |
| 912 | |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 913 | int rfcomm_send_rpn(struct rfcomm_session *s, int cr, u8 dlci, |
| 914 | u8 bit_rate, u8 data_bits, u8 stop_bits, |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 915 | u8 parity, u8 flow_ctrl_settings, |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 916 | u8 xon_char, u8 xoff_char, u16 param_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | { |
| 918 | struct rfcomm_hdr *hdr; |
| 919 | struct rfcomm_mcc *mcc; |
| 920 | struct rfcomm_rpn *rpn; |
| 921 | u8 buf[16], *ptr = buf; |
| 922 | |
| 923 | BT_DBG("%p cr %d dlci %d bit_r 0x%x data_b 0x%x stop_b 0x%x parity 0x%x" |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 924 | " flwc_s 0x%x xon_c 0x%x xoff_c 0x%x p_mask 0x%x", |
| 925 | s, cr, dlci, bit_rate, data_bits, stop_bits, parity, |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 926 | flow_ctrl_settings, xon_char, xoff_char, param_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | |
| 928 | hdr = (void *) ptr; ptr += sizeof(*hdr); |
| 929 | hdr->addr = __addr(s->initiator, 0); |
| 930 | hdr->ctrl = __ctrl(RFCOMM_UIH, 0); |
| 931 | hdr->len = __len8(sizeof(*mcc) + sizeof(*rpn)); |
| 932 | |
| 933 | mcc = (void *) ptr; ptr += sizeof(*mcc); |
| 934 | mcc->type = __mcc_type(cr, RFCOMM_RPN); |
| 935 | mcc->len = __len8(sizeof(*rpn)); |
| 936 | |
| 937 | rpn = (void *) ptr; ptr += sizeof(*rpn); |
| 938 | rpn->dlci = __addr(1, dlci); |
| 939 | rpn->bit_rate = bit_rate; |
| 940 | rpn->line_settings = __rpn_line_settings(data_bits, stop_bits, parity); |
| 941 | rpn->flow_ctrl = flow_ctrl_settings; |
| 942 | rpn->xon_char = xon_char; |
| 943 | rpn->xoff_char = xoff_char; |
Al Viro | e8db8c9 | 2006-11-08 00:28:44 -0800 | [diff] [blame] | 944 | rpn->param_mask = cpu_to_le16(param_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
| 946 | *ptr = __fcs(buf); ptr++; |
| 947 | |
| 948 | return rfcomm_send_frame(s, buf, ptr - buf); |
| 949 | } |
| 950 | |
| 951 | static int rfcomm_send_rls(struct rfcomm_session *s, int cr, u8 dlci, u8 status) |
| 952 | { |
| 953 | struct rfcomm_hdr *hdr; |
| 954 | struct rfcomm_mcc *mcc; |
| 955 | struct rfcomm_rls *rls; |
| 956 | u8 buf[16], *ptr = buf; |
| 957 | |
| 958 | BT_DBG("%p cr %d status 0x%x", s, cr, status); |
| 959 | |
| 960 | hdr = (void *) ptr; ptr += sizeof(*hdr); |
| 961 | hdr->addr = __addr(s->initiator, 0); |
| 962 | hdr->ctrl = __ctrl(RFCOMM_UIH, 0); |
| 963 | hdr->len = __len8(sizeof(*mcc) + sizeof(*rls)); |
| 964 | |
| 965 | mcc = (void *) ptr; ptr += sizeof(*mcc); |
| 966 | mcc->type = __mcc_type(cr, RFCOMM_RLS); |
| 967 | mcc->len = __len8(sizeof(*rls)); |
| 968 | |
| 969 | rls = (void *) ptr; ptr += sizeof(*rls); |
| 970 | rls->dlci = __addr(1, dlci); |
| 971 | rls->status = status; |
| 972 | |
| 973 | *ptr = __fcs(buf); ptr++; |
| 974 | |
| 975 | return rfcomm_send_frame(s, buf, ptr - buf); |
| 976 | } |
| 977 | |
| 978 | static int rfcomm_send_msc(struct rfcomm_session *s, int cr, u8 dlci, u8 v24_sig) |
| 979 | { |
| 980 | struct rfcomm_hdr *hdr; |
| 981 | struct rfcomm_mcc *mcc; |
| 982 | struct rfcomm_msc *msc; |
| 983 | u8 buf[16], *ptr = buf; |
| 984 | |
| 985 | BT_DBG("%p cr %d v24 0x%x", s, cr, v24_sig); |
| 986 | |
| 987 | hdr = (void *) ptr; ptr += sizeof(*hdr); |
| 988 | hdr->addr = __addr(s->initiator, 0); |
| 989 | hdr->ctrl = __ctrl(RFCOMM_UIH, 0); |
| 990 | hdr->len = __len8(sizeof(*mcc) + sizeof(*msc)); |
| 991 | |
| 992 | mcc = (void *) ptr; ptr += sizeof(*mcc); |
| 993 | mcc->type = __mcc_type(cr, RFCOMM_MSC); |
| 994 | mcc->len = __len8(sizeof(*msc)); |
| 995 | |
| 996 | msc = (void *) ptr; ptr += sizeof(*msc); |
| 997 | msc->dlci = __addr(1, dlci); |
| 998 | msc->v24_sig = v24_sig | 0x01; |
| 999 | |
| 1000 | *ptr = __fcs(buf); ptr++; |
| 1001 | |
| 1002 | return rfcomm_send_frame(s, buf, ptr - buf); |
| 1003 | } |
| 1004 | |
| 1005 | static int rfcomm_send_fcoff(struct rfcomm_session *s, int cr) |
| 1006 | { |
| 1007 | struct rfcomm_hdr *hdr; |
| 1008 | struct rfcomm_mcc *mcc; |
| 1009 | u8 buf[16], *ptr = buf; |
| 1010 | |
| 1011 | BT_DBG("%p cr %d", s, cr); |
| 1012 | |
| 1013 | hdr = (void *) ptr; ptr += sizeof(*hdr); |
| 1014 | hdr->addr = __addr(s->initiator, 0); |
| 1015 | hdr->ctrl = __ctrl(RFCOMM_UIH, 0); |
| 1016 | hdr->len = __len8(sizeof(*mcc)); |
| 1017 | |
| 1018 | mcc = (void *) ptr; ptr += sizeof(*mcc); |
| 1019 | mcc->type = __mcc_type(cr, RFCOMM_FCOFF); |
| 1020 | mcc->len = __len8(0); |
| 1021 | |
| 1022 | *ptr = __fcs(buf); ptr++; |
| 1023 | |
| 1024 | return rfcomm_send_frame(s, buf, ptr - buf); |
| 1025 | } |
| 1026 | |
| 1027 | static int rfcomm_send_fcon(struct rfcomm_session *s, int cr) |
| 1028 | { |
| 1029 | struct rfcomm_hdr *hdr; |
| 1030 | struct rfcomm_mcc *mcc; |
| 1031 | u8 buf[16], *ptr = buf; |
| 1032 | |
| 1033 | BT_DBG("%p cr %d", s, cr); |
| 1034 | |
| 1035 | hdr = (void *) ptr; ptr += sizeof(*hdr); |
| 1036 | hdr->addr = __addr(s->initiator, 0); |
| 1037 | hdr->ctrl = __ctrl(RFCOMM_UIH, 0); |
| 1038 | hdr->len = __len8(sizeof(*mcc)); |
| 1039 | |
| 1040 | mcc = (void *) ptr; ptr += sizeof(*mcc); |
| 1041 | mcc->type = __mcc_type(cr, RFCOMM_FCON); |
| 1042 | mcc->len = __len8(0); |
| 1043 | |
| 1044 | *ptr = __fcs(buf); ptr++; |
| 1045 | |
| 1046 | return rfcomm_send_frame(s, buf, ptr - buf); |
| 1047 | } |
| 1048 | |
| 1049 | static int rfcomm_send_test(struct rfcomm_session *s, int cr, u8 *pattern, int len) |
| 1050 | { |
| 1051 | struct socket *sock = s->sock; |
| 1052 | struct kvec iv[3]; |
| 1053 | struct msghdr msg; |
| 1054 | unsigned char hdr[5], crc[1]; |
| 1055 | |
| 1056 | if (len > 125) |
| 1057 | return -EINVAL; |
| 1058 | |
| 1059 | BT_DBG("%p cr %d", s, cr); |
| 1060 | |
| 1061 | hdr[0] = __addr(s->initiator, 0); |
| 1062 | hdr[1] = __ctrl(RFCOMM_UIH, 0); |
| 1063 | hdr[2] = 0x01 | ((len + 2) << 1); |
| 1064 | hdr[3] = 0x01 | ((cr & 0x01) << 1) | (RFCOMM_TEST << 2); |
| 1065 | hdr[4] = 0x01 | (len << 1); |
| 1066 | |
| 1067 | crc[0] = __fcs(hdr); |
| 1068 | |
| 1069 | iv[0].iov_base = hdr; |
| 1070 | iv[0].iov_len = 5; |
| 1071 | iv[1].iov_base = pattern; |
| 1072 | iv[1].iov_len = len; |
| 1073 | iv[2].iov_base = crc; |
| 1074 | iv[2].iov_len = 1; |
| 1075 | |
| 1076 | memset(&msg, 0, sizeof(msg)); |
| 1077 | |
| 1078 | return kernel_sendmsg(sock, &msg, iv, 3, 6 + len); |
| 1079 | } |
| 1080 | |
| 1081 | static int rfcomm_send_credits(struct rfcomm_session *s, u8 addr, u8 credits) |
| 1082 | { |
| 1083 | struct rfcomm_hdr *hdr; |
| 1084 | u8 buf[16], *ptr = buf; |
| 1085 | |
| 1086 | BT_DBG("%p addr %d credits %d", s, addr, credits); |
| 1087 | |
| 1088 | hdr = (void *) ptr; ptr += sizeof(*hdr); |
| 1089 | hdr->addr = addr; |
| 1090 | hdr->ctrl = __ctrl(RFCOMM_UIH, 1); |
| 1091 | hdr->len = __len8(0); |
| 1092 | |
| 1093 | *ptr = credits; ptr++; |
| 1094 | |
| 1095 | *ptr = __fcs(buf); ptr++; |
| 1096 | |
| 1097 | return rfcomm_send_frame(s, buf, ptr - buf); |
| 1098 | } |
| 1099 | |
| 1100 | static void rfcomm_make_uih(struct sk_buff *skb, u8 addr) |
| 1101 | { |
| 1102 | struct rfcomm_hdr *hdr; |
| 1103 | int len = skb->len; |
| 1104 | u8 *crc; |
| 1105 | |
| 1106 | if (len > 127) { |
| 1107 | hdr = (void *) skb_push(skb, 4); |
Marcel Holtmann | b4324b5 | 2009-06-07 18:06:51 +0200 | [diff] [blame] | 1108 | put_unaligned(cpu_to_le16(__len16(len)), (__le16 *) &hdr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | } else { |
| 1110 | hdr = (void *) skb_push(skb, 3); |
| 1111 | hdr->len = __len8(len); |
| 1112 | } |
| 1113 | hdr->addr = addr; |
| 1114 | hdr->ctrl = __ctrl(RFCOMM_UIH, 0); |
| 1115 | |
| 1116 | crc = skb_put(skb, 1); |
| 1117 | *crc = __fcs((void *) hdr); |
| 1118 | } |
| 1119 | |
| 1120 | /* ---- RFCOMM frame reception ---- */ |
| 1121 | static int rfcomm_recv_ua(struct rfcomm_session *s, u8 dlci) |
| 1122 | { |
| 1123 | BT_DBG("session %p state %ld dlci %d", s, s->state, dlci); |
| 1124 | |
| 1125 | if (dlci) { |
| 1126 | /* Data channel */ |
| 1127 | struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci); |
| 1128 | if (!d) { |
| 1129 | rfcomm_send_dm(s, dlci); |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | switch (d->state) { |
| 1134 | case BT_CONNECT: |
| 1135 | rfcomm_dlc_clear_timer(d); |
| 1136 | |
| 1137 | rfcomm_dlc_lock(d); |
| 1138 | d->state = BT_CONNECTED; |
| 1139 | d->state_change(d, 0); |
| 1140 | rfcomm_dlc_unlock(d); |
| 1141 | |
| 1142 | rfcomm_send_msc(s, 1, dlci, d->v24_sig); |
| 1143 | break; |
| 1144 | |
| 1145 | case BT_DISCONN: |
| 1146 | d->state = BT_CLOSED; |
| 1147 | __rfcomm_dlc_close(d, 0); |
Marcel Holtmann | 9cf5b0e | 2007-05-05 00:36:13 +0200 | [diff] [blame] | 1148 | |
| 1149 | if (list_empty(&s->dlcs)) { |
| 1150 | s->state = BT_DISCONN; |
| 1151 | rfcomm_send_disc(s, 0); |
| 1152 | } |
| 1153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | break; |
| 1155 | } |
| 1156 | } else { |
| 1157 | /* Control channel */ |
| 1158 | switch (s->state) { |
| 1159 | case BT_CONNECT: |
| 1160 | s->state = BT_CONNECTED; |
| 1161 | rfcomm_process_connect(s); |
| 1162 | break; |
Marcel Holtmann | 9cf5b0e | 2007-05-05 00:36:13 +0200 | [diff] [blame] | 1163 | |
| 1164 | case BT_DISCONN: |
Nick Pelly | 6c2718d | 2010-02-03 16:18:36 -0800 | [diff] [blame] | 1165 | /* When socket is closed and we are not RFCOMM |
| 1166 | * initiator rfcomm_process_rx already calls |
| 1167 | * rfcomm_session_put() */ |
| 1168 | if (s->sock->sk->sk_state != BT_CLOSED) |
Lukáš Turek | 683d949 | 2011-01-05 02:43:59 +0100 | [diff] [blame] | 1169 | if (list_empty(&s->dlcs)) |
| 1170 | rfcomm_session_put(s); |
Marcel Holtmann | 9cf5b0e | 2007-05-05 00:36:13 +0200 | [diff] [blame] | 1171 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
| 1177 | static int rfcomm_recv_dm(struct rfcomm_session *s, u8 dlci) |
| 1178 | { |
| 1179 | int err = 0; |
| 1180 | |
| 1181 | BT_DBG("session %p state %ld dlci %d", s, s->state, dlci); |
| 1182 | |
| 1183 | if (dlci) { |
| 1184 | /* Data DLC */ |
| 1185 | struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci); |
| 1186 | if (d) { |
| 1187 | if (d->state == BT_CONNECT || d->state == BT_CONFIG) |
| 1188 | err = ECONNREFUSED; |
| 1189 | else |
| 1190 | err = ECONNRESET; |
| 1191 | |
| 1192 | d->state = BT_CLOSED; |
| 1193 | __rfcomm_dlc_close(d, err); |
| 1194 | } |
| 1195 | } else { |
| 1196 | if (s->state == BT_CONNECT) |
| 1197 | err = ECONNREFUSED; |
| 1198 | else |
| 1199 | err = ECONNRESET; |
| 1200 | |
| 1201 | s->state = BT_CLOSED; |
| 1202 | rfcomm_session_close(s, err); |
| 1203 | } |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | static int rfcomm_recv_disc(struct rfcomm_session *s, u8 dlci) |
| 1208 | { |
| 1209 | int err = 0; |
| 1210 | |
| 1211 | BT_DBG("session %p state %ld dlci %d", s, s->state, dlci); |
| 1212 | |
| 1213 | if (dlci) { |
| 1214 | struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci); |
| 1215 | if (d) { |
| 1216 | rfcomm_send_ua(s, dlci); |
| 1217 | |
| 1218 | if (d->state == BT_CONNECT || d->state == BT_CONFIG) |
| 1219 | err = ECONNREFUSED; |
| 1220 | else |
| 1221 | err = ECONNRESET; |
| 1222 | |
| 1223 | d->state = BT_CLOSED; |
| 1224 | __rfcomm_dlc_close(d, err); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1225 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | rfcomm_send_dm(s, dlci); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | } else { |
| 1229 | rfcomm_send_ua(s, 0); |
| 1230 | |
| 1231 | if (s->state == BT_CONNECT) |
| 1232 | err = ECONNREFUSED; |
| 1233 | else |
| 1234 | err = ECONNRESET; |
| 1235 | |
| 1236 | s->state = BT_CLOSED; |
| 1237 | rfcomm_session_close(s, err); |
| 1238 | } |
| 1239 | |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 1243 | void rfcomm_dlc_accept(struct rfcomm_dlc *d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | { |
Marcel Holtmann | 300b939 | 2006-07-03 10:37:55 +0200 | [diff] [blame] | 1245 | struct sock *sk = d->session->sock->sk; |
| 1246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | BT_DBG("dlc %p", d); |
| 1248 | |
| 1249 | rfcomm_send_ua(d->session, d->dlci); |
| 1250 | |
Johan Hedberg | e2139b3 | 2009-03-26 16:41:56 +0200 | [diff] [blame] | 1251 | rfcomm_dlc_clear_timer(d); |
| 1252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | rfcomm_dlc_lock(d); |
| 1254 | d->state = BT_CONNECTED; |
| 1255 | d->state_change(d, 0); |
| 1256 | rfcomm_dlc_unlock(d); |
| 1257 | |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 1258 | if (d->role_switch) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1259 | hci_conn_switch_role(l2cap_pi(sk)->conn->hcon, 0x00); |
Marcel Holtmann | 300b939 | 2006-07-03 10:37:55 +0200 | [diff] [blame] | 1260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | rfcomm_send_msc(d->session, 1, d->dlci, d->v24_sig); |
| 1262 | } |
| 1263 | |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 1264 | static void rfcomm_check_accept(struct rfcomm_dlc *d) |
| 1265 | { |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 1266 | if (rfcomm_check_security(d)) { |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 1267 | if (d->defer_setup) { |
| 1268 | set_bit(RFCOMM_DEFER_SETUP, &d->flags); |
| 1269 | rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT); |
Marcel Holtmann | 8bf4794 | 2009-02-16 02:59:49 +0100 | [diff] [blame] | 1270 | |
| 1271 | rfcomm_dlc_lock(d); |
| 1272 | d->state = BT_CONNECT2; |
| 1273 | d->state_change(d, 0); |
| 1274 | rfcomm_dlc_unlock(d); |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 1275 | } else |
| 1276 | rfcomm_dlc_accept(d); |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1277 | } else { |
| 1278 | set_bit(RFCOMM_AUTH_PENDING, &d->flags); |
| 1279 | rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT); |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | static int rfcomm_recv_sabm(struct rfcomm_session *s, u8 dlci) |
| 1284 | { |
| 1285 | struct rfcomm_dlc *d; |
| 1286 | u8 channel; |
| 1287 | |
| 1288 | BT_DBG("session %p state %ld dlci %d", s, s->state, dlci); |
| 1289 | |
| 1290 | if (!dlci) { |
| 1291 | rfcomm_send_ua(s, 0); |
| 1292 | |
| 1293 | if (s->state == BT_OPEN) { |
| 1294 | s->state = BT_CONNECTED; |
| 1295 | rfcomm_process_connect(s); |
| 1296 | } |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
| 1300 | /* Check if DLC exists */ |
| 1301 | d = rfcomm_dlc_get(s, dlci); |
| 1302 | if (d) { |
| 1303 | if (d->state == BT_OPEN) { |
| 1304 | /* DLC was previously opened by PN request */ |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 1305 | rfcomm_check_accept(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | } |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | /* Notify socket layer about incoming connection */ |
| 1311 | channel = __srv_channel(dlci); |
| 1312 | if (rfcomm_connect_ind(s, channel, &d)) { |
| 1313 | d->dlci = dlci; |
| 1314 | d->addr = __addr(s->initiator, dlci); |
| 1315 | rfcomm_dlc_link(s, d); |
| 1316 | |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 1317 | rfcomm_check_accept(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | } else { |
| 1319 | rfcomm_send_dm(s, dlci); |
| 1320 | } |
| 1321 | |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
| 1325 | static int rfcomm_apply_pn(struct rfcomm_dlc *d, int cr, struct rfcomm_pn *pn) |
| 1326 | { |
| 1327 | struct rfcomm_session *s = d->session; |
| 1328 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1329 | BT_DBG("dlc %p state %ld dlci %d mtu %d fc 0x%x credits %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | d, d->state, d->dlci, pn->mtu, pn->flow_ctrl, pn->credits); |
| 1331 | |
Marcel Holtmann | 7c2660b | 2006-07-03 10:02:51 +0200 | [diff] [blame] | 1332 | if ((pn->flow_ctrl == 0xf0 && s->cfc != RFCOMM_CFC_DISABLED) || |
| 1333 | pn->flow_ctrl == 0xe0) { |
| 1334 | d->cfc = RFCOMM_CFC_ENABLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | d->tx_credits = pn->credits; |
| 1336 | } else { |
Marcel Holtmann | 7c2660b | 2006-07-03 10:02:51 +0200 | [diff] [blame] | 1337 | d->cfc = RFCOMM_CFC_DISABLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | set_bit(RFCOMM_TX_THROTTLED, &d->flags); |
| 1339 | } |
| 1340 | |
Marcel Holtmann | 7c2660b | 2006-07-03 10:02:51 +0200 | [diff] [blame] | 1341 | if (s->cfc == RFCOMM_CFC_UNKNOWN) |
| 1342 | s->cfc = d->cfc; |
| 1343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | d->priority = pn->priority; |
| 1345 | |
Marcel Holtmann | b4324b5 | 2009-06-07 18:06:51 +0200 | [diff] [blame] | 1346 | d->mtu = __le16_to_cpu(pn->mtu); |
Marcel Holtmann | 98bcd08 | 2006-07-14 11:42:12 +0200 | [diff] [blame] | 1347 | |
| 1348 | if (cr && d->mtu > s->mtu) |
| 1349 | d->mtu = s->mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | static int rfcomm_recv_pn(struct rfcomm_session *s, int cr, struct sk_buff *skb) |
| 1355 | { |
| 1356 | struct rfcomm_pn *pn = (void *) skb->data; |
| 1357 | struct rfcomm_dlc *d; |
| 1358 | u8 dlci = pn->dlci; |
| 1359 | |
| 1360 | BT_DBG("session %p state %ld dlci %d", s, s->state, dlci); |
| 1361 | |
| 1362 | if (!dlci) |
| 1363 | return 0; |
| 1364 | |
| 1365 | d = rfcomm_dlc_get(s, dlci); |
| 1366 | if (d) { |
| 1367 | if (cr) { |
| 1368 | /* PN request */ |
| 1369 | rfcomm_apply_pn(d, cr, pn); |
| 1370 | rfcomm_send_pn(s, 0, d); |
| 1371 | } else { |
| 1372 | /* PN response */ |
| 1373 | switch (d->state) { |
| 1374 | case BT_CONFIG: |
| 1375 | rfcomm_apply_pn(d, cr, pn); |
| 1376 | |
| 1377 | d->state = BT_CONNECT; |
| 1378 | rfcomm_send_sabm(s, d->dlci); |
| 1379 | break; |
| 1380 | } |
| 1381 | } |
| 1382 | } else { |
| 1383 | u8 channel = __srv_channel(dlci); |
| 1384 | |
| 1385 | if (!cr) |
| 1386 | return 0; |
| 1387 | |
| 1388 | /* PN request for non existing DLC. |
| 1389 | * Assume incoming connection. */ |
| 1390 | if (rfcomm_connect_ind(s, channel, &d)) { |
| 1391 | d->dlci = dlci; |
| 1392 | d->addr = __addr(s->initiator, dlci); |
| 1393 | rfcomm_dlc_link(s, d); |
| 1394 | |
| 1395 | rfcomm_apply_pn(d, cr, pn); |
| 1396 | |
| 1397 | d->state = BT_OPEN; |
| 1398 | rfcomm_send_pn(s, 0, d); |
| 1399 | } else { |
| 1400 | rfcomm_send_dm(s, dlci); |
| 1401 | } |
| 1402 | } |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | static int rfcomm_recv_rpn(struct rfcomm_session *s, int cr, int len, struct sk_buff *skb) |
| 1407 | { |
| 1408 | struct rfcomm_rpn *rpn = (void *) skb->data; |
| 1409 | u8 dlci = __get_dlci(rpn->dlci); |
| 1410 | |
| 1411 | u8 bit_rate = 0; |
| 1412 | u8 data_bits = 0; |
| 1413 | u8 stop_bits = 0; |
| 1414 | u8 parity = 0; |
| 1415 | u8 flow_ctrl = 0; |
| 1416 | u8 xon_char = 0; |
| 1417 | u8 xoff_char = 0; |
| 1418 | u16 rpn_mask = RFCOMM_RPN_PM_ALL; |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1419 | |
| 1420 | BT_DBG("dlci %d cr %d len 0x%x bitr 0x%x line 0x%x flow 0x%x xonc 0x%x xoffc 0x%x pm 0x%x", |
| 1421 | dlci, cr, len, rpn->bit_rate, rpn->line_settings, rpn->flow_ctrl, |
| 1422 | rpn->xon_char, rpn->xoff_char, rpn->param_mask); |
| 1423 | |
| 1424 | if (!cr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | return 0; |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | if (len == 1) { |
Yuri Kululin | 0860146 | 2010-07-23 13:57:12 +0400 | [diff] [blame] | 1428 | /* This is a request, return default (according to ETSI TS 07.10) settings */ |
| 1429 | bit_rate = RFCOMM_RPN_BR_9600; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | data_bits = RFCOMM_RPN_DATA_8; |
| 1431 | stop_bits = RFCOMM_RPN_STOP_1; |
| 1432 | parity = RFCOMM_RPN_PARITY_NONE; |
| 1433 | flow_ctrl = RFCOMM_RPN_FLOW_NONE; |
| 1434 | xon_char = RFCOMM_RPN_XON_CHAR; |
| 1435 | xoff_char = RFCOMM_RPN_XOFF_CHAR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | goto rpn_out; |
| 1437 | } |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1438 | |
| 1439 | /* Check for sane values, ignore/accept bit_rate, 8 bits, 1 stop bit, |
| 1440 | * no parity, no flow control lines, normal XON/XOFF chars */ |
| 1441 | |
Al Viro | e8db8c9 | 2006-11-08 00:28:44 -0800 | [diff] [blame] | 1442 | if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_BITRATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | bit_rate = rpn->bit_rate; |
Yuri Kululin | 0860146 | 2010-07-23 13:57:12 +0400 | [diff] [blame] | 1444 | if (bit_rate > RFCOMM_RPN_BR_230400) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | BT_DBG("RPN bit rate mismatch 0x%x", bit_rate); |
Yuri Kululin | 0860146 | 2010-07-23 13:57:12 +0400 | [diff] [blame] | 1446 | bit_rate = RFCOMM_RPN_BR_9600; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | rpn_mask ^= RFCOMM_RPN_PM_BITRATE; |
| 1448 | } |
| 1449 | } |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1450 | |
Al Viro | e8db8c9 | 2006-11-08 00:28:44 -0800 | [diff] [blame] | 1451 | if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_DATA)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | data_bits = __get_rpn_data_bits(rpn->line_settings); |
| 1453 | if (data_bits != RFCOMM_RPN_DATA_8) { |
| 1454 | BT_DBG("RPN data bits mismatch 0x%x", data_bits); |
| 1455 | data_bits = RFCOMM_RPN_DATA_8; |
| 1456 | rpn_mask ^= RFCOMM_RPN_PM_DATA; |
| 1457 | } |
| 1458 | } |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1459 | |
Al Viro | e8db8c9 | 2006-11-08 00:28:44 -0800 | [diff] [blame] | 1460 | if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_STOP)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | stop_bits = __get_rpn_stop_bits(rpn->line_settings); |
| 1462 | if (stop_bits != RFCOMM_RPN_STOP_1) { |
| 1463 | BT_DBG("RPN stop bits mismatch 0x%x", stop_bits); |
| 1464 | stop_bits = RFCOMM_RPN_STOP_1; |
| 1465 | rpn_mask ^= RFCOMM_RPN_PM_STOP; |
| 1466 | } |
| 1467 | } |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1468 | |
Al Viro | e8db8c9 | 2006-11-08 00:28:44 -0800 | [diff] [blame] | 1469 | if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_PARITY)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | parity = __get_rpn_parity(rpn->line_settings); |
| 1471 | if (parity != RFCOMM_RPN_PARITY_NONE) { |
| 1472 | BT_DBG("RPN parity mismatch 0x%x", parity); |
| 1473 | parity = RFCOMM_RPN_PARITY_NONE; |
| 1474 | rpn_mask ^= RFCOMM_RPN_PM_PARITY; |
| 1475 | } |
| 1476 | } |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1477 | |
Al Viro | e8db8c9 | 2006-11-08 00:28:44 -0800 | [diff] [blame] | 1478 | if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_FLOW)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | flow_ctrl = rpn->flow_ctrl; |
| 1480 | if (flow_ctrl != RFCOMM_RPN_FLOW_NONE) { |
| 1481 | BT_DBG("RPN flow ctrl mismatch 0x%x", flow_ctrl); |
| 1482 | flow_ctrl = RFCOMM_RPN_FLOW_NONE; |
| 1483 | rpn_mask ^= RFCOMM_RPN_PM_FLOW; |
| 1484 | } |
| 1485 | } |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1486 | |
Al Viro | e8db8c9 | 2006-11-08 00:28:44 -0800 | [diff] [blame] | 1487 | if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_XON)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | xon_char = rpn->xon_char; |
| 1489 | if (xon_char != RFCOMM_RPN_XON_CHAR) { |
| 1490 | BT_DBG("RPN XON char mismatch 0x%x", xon_char); |
| 1491 | xon_char = RFCOMM_RPN_XON_CHAR; |
| 1492 | rpn_mask ^= RFCOMM_RPN_PM_XON; |
| 1493 | } |
| 1494 | } |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1495 | |
Al Viro | e8db8c9 | 2006-11-08 00:28:44 -0800 | [diff] [blame] | 1496 | if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_XOFF)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | xoff_char = rpn->xoff_char; |
| 1498 | if (xoff_char != RFCOMM_RPN_XOFF_CHAR) { |
| 1499 | BT_DBG("RPN XOFF char mismatch 0x%x", xoff_char); |
| 1500 | xoff_char = RFCOMM_RPN_XOFF_CHAR; |
| 1501 | rpn_mask ^= RFCOMM_RPN_PM_XOFF; |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | rpn_out: |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1506 | rfcomm_send_rpn(s, 0, dlci, bit_rate, data_bits, stop_bits, |
| 1507 | parity, flow_ctrl, xon_char, xoff_char, rpn_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
| 1512 | static int rfcomm_recv_rls(struct rfcomm_session *s, int cr, struct sk_buff *skb) |
| 1513 | { |
| 1514 | struct rfcomm_rls *rls = (void *) skb->data; |
| 1515 | u8 dlci = __get_dlci(rls->dlci); |
| 1516 | |
| 1517 | BT_DBG("dlci %d cr %d status 0x%x", dlci, cr, rls->status); |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | if (!cr) |
| 1520 | return 0; |
| 1521 | |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1522 | /* We should probably do something with this information here. But |
| 1523 | * for now it's sufficient just to reply -- Bluetooth 1.1 says it's |
| 1524 | * mandatory to recognise and respond to RLS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | |
| 1526 | rfcomm_send_rls(s, 0, dlci, rls->status); |
| 1527 | |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | static int rfcomm_recv_msc(struct rfcomm_session *s, int cr, struct sk_buff *skb) |
| 1532 | { |
| 1533 | struct rfcomm_msc *msc = (void *) skb->data; |
| 1534 | struct rfcomm_dlc *d; |
| 1535 | u8 dlci = __get_dlci(msc->dlci); |
| 1536 | |
| 1537 | BT_DBG("dlci %d cr %d v24 0x%x", dlci, cr, msc->v24_sig); |
| 1538 | |
| 1539 | d = rfcomm_dlc_get(s, dlci); |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1540 | if (!d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | return 0; |
| 1542 | |
| 1543 | if (cr) { |
| 1544 | if (msc->v24_sig & RFCOMM_V24_FC && !d->cfc) |
| 1545 | set_bit(RFCOMM_TX_THROTTLED, &d->flags); |
| 1546 | else |
| 1547 | clear_bit(RFCOMM_TX_THROTTLED, &d->flags); |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | rfcomm_dlc_lock(d); |
Marcel Holtmann | 8b6b3da | 2008-07-14 20:13:52 +0200 | [diff] [blame] | 1550 | |
| 1551 | d->remote_v24_sig = msc->v24_sig; |
| 1552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | if (d->modem_status) |
| 1554 | d->modem_status(d, msc->v24_sig); |
Marcel Holtmann | 8b6b3da | 2008-07-14 20:13:52 +0200 | [diff] [blame] | 1555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | rfcomm_dlc_unlock(d); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | rfcomm_send_msc(s, 0, dlci, msc->v24_sig); |
| 1559 | |
| 1560 | d->mscex |= RFCOMM_MSCEX_RX; |
J. Suter | 3a5e903 | 2005-08-09 20:28:46 -0700 | [diff] [blame] | 1561 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | d->mscex |= RFCOMM_MSCEX_TX; |
| 1563 | |
| 1564 | return 0; |
| 1565 | } |
| 1566 | |
| 1567 | static int rfcomm_recv_mcc(struct rfcomm_session *s, struct sk_buff *skb) |
| 1568 | { |
| 1569 | struct rfcomm_mcc *mcc = (void *) skb->data; |
| 1570 | u8 type, cr, len; |
| 1571 | |
| 1572 | cr = __test_cr(mcc->type); |
| 1573 | type = __get_mcc_type(mcc->type); |
| 1574 | len = __get_mcc_len(mcc->len); |
| 1575 | |
| 1576 | BT_DBG("%p type 0x%x cr %d", s, type, cr); |
| 1577 | |
| 1578 | skb_pull(skb, 2); |
| 1579 | |
| 1580 | switch (type) { |
| 1581 | case RFCOMM_PN: |
| 1582 | rfcomm_recv_pn(s, cr, skb); |
| 1583 | break; |
| 1584 | |
| 1585 | case RFCOMM_RPN: |
| 1586 | rfcomm_recv_rpn(s, cr, len, skb); |
| 1587 | break; |
| 1588 | |
| 1589 | case RFCOMM_RLS: |
| 1590 | rfcomm_recv_rls(s, cr, skb); |
| 1591 | break; |
| 1592 | |
| 1593 | case RFCOMM_MSC: |
| 1594 | rfcomm_recv_msc(s, cr, skb); |
| 1595 | break; |
| 1596 | |
| 1597 | case RFCOMM_FCOFF: |
| 1598 | if (cr) { |
| 1599 | set_bit(RFCOMM_TX_THROTTLED, &s->flags); |
| 1600 | rfcomm_send_fcoff(s, 0); |
| 1601 | } |
| 1602 | break; |
| 1603 | |
| 1604 | case RFCOMM_FCON: |
| 1605 | if (cr) { |
| 1606 | clear_bit(RFCOMM_TX_THROTTLED, &s->flags); |
| 1607 | rfcomm_send_fcon(s, 0); |
| 1608 | } |
| 1609 | break; |
| 1610 | |
| 1611 | case RFCOMM_TEST: |
| 1612 | if (cr) |
| 1613 | rfcomm_send_test(s, 0, skb->data, skb->len); |
| 1614 | break; |
| 1615 | |
| 1616 | case RFCOMM_NSC: |
| 1617 | break; |
| 1618 | |
| 1619 | default: |
| 1620 | BT_ERR("Unknown control type 0x%02x", type); |
| 1621 | rfcomm_send_nsc(s, cr, type); |
| 1622 | break; |
| 1623 | } |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk_buff *skb) |
| 1628 | { |
| 1629 | struct rfcomm_dlc *d; |
| 1630 | |
| 1631 | BT_DBG("session %p state %ld dlci %d pf %d", s, s->state, dlci, pf); |
| 1632 | |
| 1633 | d = rfcomm_dlc_get(s, dlci); |
| 1634 | if (!d) { |
| 1635 | rfcomm_send_dm(s, dlci); |
| 1636 | goto drop; |
| 1637 | } |
| 1638 | |
| 1639 | if (pf && d->cfc) { |
| 1640 | u8 credits = *(u8 *) skb->data; skb_pull(skb, 1); |
| 1641 | |
| 1642 | d->tx_credits += credits; |
| 1643 | if (d->tx_credits) |
| 1644 | clear_bit(RFCOMM_TX_THROTTLED, &d->flags); |
| 1645 | } |
| 1646 | |
| 1647 | if (skb->len && d->state == BT_CONNECTED) { |
| 1648 | rfcomm_dlc_lock(d); |
| 1649 | d->rx_credits--; |
| 1650 | d->data_ready(d, skb); |
| 1651 | rfcomm_dlc_unlock(d); |
| 1652 | return 0; |
| 1653 | } |
| 1654 | |
| 1655 | drop: |
| 1656 | kfree_skb(skb); |
| 1657 | return 0; |
| 1658 | } |
| 1659 | |
| 1660 | static int rfcomm_recv_frame(struct rfcomm_session *s, struct sk_buff *skb) |
| 1661 | { |
| 1662 | struct rfcomm_hdr *hdr = (void *) skb->data; |
| 1663 | u8 type, dlci, fcs; |
| 1664 | |
| 1665 | dlci = __get_dlci(hdr->addr); |
| 1666 | type = __get_type(hdr->ctrl); |
| 1667 | |
| 1668 | /* Trim FCS */ |
| 1669 | skb->len--; skb->tail--; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1670 | fcs = *(u8 *)skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | |
| 1672 | if (__check_fcs(skb->data, type, fcs)) { |
| 1673 | BT_ERR("bad checksum in packet"); |
| 1674 | kfree_skb(skb); |
| 1675 | return -EILSEQ; |
| 1676 | } |
| 1677 | |
| 1678 | if (__test_ea(hdr->len)) |
| 1679 | skb_pull(skb, 3); |
| 1680 | else |
| 1681 | skb_pull(skb, 4); |
| 1682 | |
| 1683 | switch (type) { |
| 1684 | case RFCOMM_SABM: |
| 1685 | if (__test_pf(hdr->ctrl)) |
| 1686 | rfcomm_recv_sabm(s, dlci); |
| 1687 | break; |
| 1688 | |
| 1689 | case RFCOMM_DISC: |
| 1690 | if (__test_pf(hdr->ctrl)) |
| 1691 | rfcomm_recv_disc(s, dlci); |
| 1692 | break; |
| 1693 | |
| 1694 | case RFCOMM_UA: |
| 1695 | if (__test_pf(hdr->ctrl)) |
| 1696 | rfcomm_recv_ua(s, dlci); |
| 1697 | break; |
| 1698 | |
| 1699 | case RFCOMM_DM: |
| 1700 | rfcomm_recv_dm(s, dlci); |
| 1701 | break; |
| 1702 | |
| 1703 | case RFCOMM_UIH: |
| 1704 | if (dlci) |
| 1705 | return rfcomm_recv_data(s, dlci, __test_pf(hdr->ctrl), skb); |
| 1706 | |
| 1707 | rfcomm_recv_mcc(s, skb); |
| 1708 | break; |
| 1709 | |
| 1710 | default: |
Andrei Emeltchenko | 5017d8d | 2010-09-08 16:26:53 +0300 | [diff] [blame] | 1711 | BT_ERR("Unknown packet type 0x%02x", type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | break; |
| 1713 | } |
| 1714 | kfree_skb(skb); |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | /* ---- Connection and data processing ---- */ |
| 1719 | |
| 1720 | static void rfcomm_process_connect(struct rfcomm_session *s) |
| 1721 | { |
| 1722 | struct rfcomm_dlc *d; |
| 1723 | struct list_head *p, *n; |
| 1724 | |
| 1725 | BT_DBG("session %p state %ld", s, s->state); |
| 1726 | |
| 1727 | list_for_each_safe(p, n, &s->dlcs) { |
| 1728 | d = list_entry(p, struct rfcomm_dlc, list); |
| 1729 | if (d->state == BT_CONFIG) { |
| 1730 | d->mtu = s->mtu; |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 1731 | if (rfcomm_check_security(d)) { |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1732 | rfcomm_send_pn(s, 1, d); |
| 1733 | } else { |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 1734 | set_bit(RFCOMM_AUTH_PENDING, &d->flags); |
| 1735 | rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT); |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1736 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | } |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | /* Send data queued for the DLC. |
| 1742 | * Return number of frames left in the queue. |
| 1743 | */ |
| 1744 | static inline int rfcomm_process_tx(struct rfcomm_dlc *d) |
| 1745 | { |
| 1746 | struct sk_buff *skb; |
| 1747 | int err; |
| 1748 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1749 | BT_DBG("dlc %p state %ld cfc %d rx_credits %d tx_credits %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | d, d->state, d->cfc, d->rx_credits, d->tx_credits); |
| 1751 | |
| 1752 | /* Send pending MSC */ |
| 1753 | if (test_and_clear_bit(RFCOMM_MSC_PENDING, &d->flags)) |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1754 | rfcomm_send_msc(d->session, 1, d->dlci, d->v24_sig); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | |
| 1756 | if (d->cfc) { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1757 | /* CFC enabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | * Give them some credits */ |
| 1759 | if (!test_bit(RFCOMM_RX_THROTTLED, &d->flags) && |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1760 | d->rx_credits <= (d->cfc >> 2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | rfcomm_send_credits(d->session, d->addr, d->cfc - d->rx_credits); |
| 1762 | d->rx_credits = d->cfc; |
| 1763 | } |
| 1764 | } else { |
| 1765 | /* CFC disabled. |
| 1766 | * Give ourselves some credits */ |
| 1767 | d->tx_credits = 5; |
| 1768 | } |
| 1769 | |
| 1770 | if (test_bit(RFCOMM_TX_THROTTLED, &d->flags)) |
| 1771 | return skb_queue_len(&d->tx_queue); |
| 1772 | |
| 1773 | while (d->tx_credits && (skb = skb_dequeue(&d->tx_queue))) { |
| 1774 | err = rfcomm_send_frame(d->session, skb->data, skb->len); |
| 1775 | if (err < 0) { |
| 1776 | skb_queue_head(&d->tx_queue, skb); |
| 1777 | break; |
| 1778 | } |
| 1779 | kfree_skb(skb); |
| 1780 | d->tx_credits--; |
| 1781 | } |
| 1782 | |
| 1783 | if (d->cfc && !d->tx_credits) { |
| 1784 | /* We're out of TX credits. |
| 1785 | * Set TX_THROTTLED flag to avoid unnesary wakeups by dlc_send. */ |
| 1786 | set_bit(RFCOMM_TX_THROTTLED, &d->flags); |
| 1787 | } |
| 1788 | |
| 1789 | return skb_queue_len(&d->tx_queue); |
| 1790 | } |
| 1791 | |
| 1792 | static inline void rfcomm_process_dlcs(struct rfcomm_session *s) |
| 1793 | { |
| 1794 | struct rfcomm_dlc *d; |
| 1795 | struct list_head *p, *n; |
| 1796 | |
| 1797 | BT_DBG("session %p state %ld", s, s->state); |
| 1798 | |
| 1799 | list_for_each_safe(p, n, &s->dlcs) { |
| 1800 | d = list_entry(p, struct rfcomm_dlc, list); |
| 1801 | |
| 1802 | if (test_bit(RFCOMM_TIMED_OUT, &d->flags)) { |
| 1803 | __rfcomm_dlc_close(d, ETIMEDOUT); |
| 1804 | continue; |
| 1805 | } |
| 1806 | |
| 1807 | if (test_and_clear_bit(RFCOMM_AUTH_ACCEPT, &d->flags)) { |
| 1808 | rfcomm_dlc_clear_timer(d); |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 1809 | if (d->out) { |
| 1810 | rfcomm_send_pn(s, 1, d); |
| 1811 | rfcomm_dlc_set_timer(d, RFCOMM_CONN_TIMEOUT); |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 1812 | } else { |
| 1813 | if (d->defer_setup) { |
| 1814 | set_bit(RFCOMM_DEFER_SETUP, &d->flags); |
| 1815 | rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT); |
Marcel Holtmann | 8bf4794 | 2009-02-16 02:59:49 +0100 | [diff] [blame] | 1816 | |
| 1817 | rfcomm_dlc_lock(d); |
| 1818 | d->state = BT_CONNECT2; |
| 1819 | d->state_change(d, 0); |
| 1820 | rfcomm_dlc_unlock(d); |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 1821 | } else |
| 1822 | rfcomm_dlc_accept(d); |
| 1823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | continue; |
| 1825 | } else if (test_and_clear_bit(RFCOMM_AUTH_REJECT, &d->flags)) { |
| 1826 | rfcomm_dlc_clear_timer(d); |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 1827 | if (!d->out) |
| 1828 | rfcomm_send_dm(s, d->dlci); |
| 1829 | else |
| 1830 | d->state = BT_CLOSED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | __rfcomm_dlc_close(d, ECONNREFUSED); |
| 1832 | continue; |
| 1833 | } |
| 1834 | |
Jaikumar Ganesh | 6e1031a | 2009-02-02 18:03:57 -0800 | [diff] [blame] | 1835 | if (test_bit(RFCOMM_SEC_PENDING, &d->flags)) |
| 1836 | continue; |
| 1837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | if (test_bit(RFCOMM_TX_THROTTLED, &s->flags)) |
| 1839 | continue; |
| 1840 | |
| 1841 | if ((d->state == BT_CONNECTED || d->state == BT_DISCONN) && |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 1842 | d->mscex == RFCOMM_MSCEX_OK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | rfcomm_process_tx(d); |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | static inline void rfcomm_process_rx(struct rfcomm_session *s) |
| 1848 | { |
| 1849 | struct socket *sock = s->sock; |
| 1850 | struct sock *sk = sock->sk; |
| 1851 | struct sk_buff *skb; |
| 1852 | |
| 1853 | BT_DBG("session %p state %ld qlen %d", s, s->state, skb_queue_len(&sk->sk_receive_queue)); |
| 1854 | |
| 1855 | /* Get data directly from socket receive queue without copying it. */ |
| 1856 | while ((skb = skb_dequeue(&sk->sk_receive_queue))) { |
| 1857 | skb_orphan(skb); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1858 | if (!skb_linearize(skb)) |
| 1859 | rfcomm_recv_frame(s, skb); |
| 1860 | else |
| 1861 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | if (sk->sk_state == BT_CLOSED) { |
| 1865 | if (!s->initiator) |
| 1866 | rfcomm_session_put(s); |
| 1867 | |
| 1868 | rfcomm_session_close(s, sk->sk_err); |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | static inline void rfcomm_accept_connection(struct rfcomm_session *s) |
| 1873 | { |
| 1874 | struct socket *sock = s->sock, *nsock; |
| 1875 | int err; |
| 1876 | |
| 1877 | /* Fast check for a new connection. |
| 1878 | * Avoids unnesesary socket allocations. */ |
| 1879 | if (list_empty(&bt_sk(sock->sk)->accept_q)) |
| 1880 | return; |
| 1881 | |
| 1882 | BT_DBG("session %p", s); |
| 1883 | |
Marcel Holtmann | 48db9ca | 2007-05-05 00:36:06 +0200 | [diff] [blame] | 1884 | err = kernel_accept(sock, &nsock, O_NONBLOCK); |
| 1885 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | return; |
| 1887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | /* Set our callbacks */ |
| 1889 | nsock->sk->sk_data_ready = rfcomm_l2data_ready; |
| 1890 | nsock->sk->sk_state_change = rfcomm_l2state_change; |
| 1891 | |
| 1892 | s = rfcomm_session_add(nsock, BT_OPEN); |
| 1893 | if (s) { |
| 1894 | rfcomm_session_hold(s); |
Marcel Holtmann | 98bcd08 | 2006-07-14 11:42:12 +0200 | [diff] [blame] | 1895 | |
| 1896 | /* We should adjust MTU on incoming sessions. |
Mallikarjuna GB | 2da40bf | 2011-09-06 17:02:36 +0530 | [diff] [blame^] | 1897 | * L2CAP MTU minus UIH header and FCS. |
| 1898 | * Need to accomodate 1 Byte credits information */ |
| 1899 | s->mtu = min(l2cap_pi(nsock->sk)->omtu, |
| 1900 | l2cap_pi(nsock->sk)->imtu) - RFCOMM_HDR_SIZE; |
Marcel Holtmann | 98bcd08 | 2006-07-14 11:42:12 +0200 | [diff] [blame] | 1901 | |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 1902 | rfcomm_schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | } else |
| 1904 | sock_release(nsock); |
| 1905 | } |
| 1906 | |
| 1907 | static inline void rfcomm_check_connection(struct rfcomm_session *s) |
| 1908 | { |
| 1909 | struct sock *sk = s->sock->sk; |
| 1910 | |
| 1911 | BT_DBG("%p state %ld", s, s->state); |
| 1912 | |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 1913 | switch (sk->sk_state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | case BT_CONNECTED: |
| 1915 | s->state = BT_CONNECT; |
| 1916 | |
| 1917 | /* We can adjust MTU on outgoing sessions. |
Mallikarjuna GB | 2da40bf | 2011-09-06 17:02:36 +0530 | [diff] [blame^] | 1918 | * L2CAP MTU minus UIH header, Credits and FCS. */ |
| 1919 | s->mtu = min(l2cap_pi(sk)->omtu, l2cap_pi(sk)->imtu) - |
| 1920 | RFCOMM_HDR_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | |
| 1922 | rfcomm_send_sabm(s, 0); |
| 1923 | break; |
| 1924 | |
| 1925 | case BT_CLOSED: |
| 1926 | s->state = BT_CLOSED; |
| 1927 | rfcomm_session_close(s, sk->sk_err); |
| 1928 | break; |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | static inline void rfcomm_process_sessions(void) |
| 1933 | { |
| 1934 | struct list_head *p, *n; |
| 1935 | |
| 1936 | rfcomm_lock(); |
| 1937 | |
| 1938 | list_for_each_safe(p, n, &session_list) { |
| 1939 | struct rfcomm_session *s; |
| 1940 | s = list_entry(p, struct rfcomm_session, list); |
| 1941 | |
Luiz Augusto von Dentz | 9e726b1 | 2009-07-15 13:50:58 -0300 | [diff] [blame] | 1942 | if (test_and_clear_bit(RFCOMM_TIMED_OUT, &s->flags)) { |
| 1943 | s->state = BT_DISCONN; |
| 1944 | rfcomm_send_disc(s, 0); |
Marcel Holtmann | 485f1ef | 2010-02-03 15:52:18 -0800 | [diff] [blame] | 1945 | rfcomm_session_put(s); |
Luiz Augusto von Dentz | 9e726b1 | 2009-07-15 13:50:58 -0300 | [diff] [blame] | 1946 | continue; |
| 1947 | } |
| 1948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | if (s->state == BT_LISTEN) { |
| 1950 | rfcomm_accept_connection(s); |
| 1951 | continue; |
| 1952 | } |
| 1953 | |
| 1954 | rfcomm_session_hold(s); |
| 1955 | |
| 1956 | switch (s->state) { |
| 1957 | case BT_BOUND: |
| 1958 | rfcomm_check_connection(s); |
| 1959 | break; |
| 1960 | |
| 1961 | default: |
| 1962 | rfcomm_process_rx(s); |
| 1963 | break; |
| 1964 | } |
| 1965 | |
| 1966 | rfcomm_process_dlcs(s); |
| 1967 | |
| 1968 | rfcomm_session_put(s); |
| 1969 | } |
| 1970 | |
| 1971 | rfcomm_unlock(); |
| 1972 | } |
| 1973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | static int rfcomm_add_listener(bdaddr_t *ba) |
| 1975 | { |
| 1976 | struct sockaddr_l2 addr; |
| 1977 | struct socket *sock; |
| 1978 | struct sock *sk; |
| 1979 | struct rfcomm_session *s; |
| 1980 | int err = 0; |
| 1981 | |
| 1982 | /* Create socket */ |
| 1983 | err = rfcomm_l2sock_create(&sock); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1984 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | BT_ERR("Create socket failed %d", err); |
| 1986 | return err; |
| 1987 | } |
| 1988 | |
| 1989 | /* Bind socket */ |
| 1990 | bacpy(&addr.l2_bdaddr, ba); |
| 1991 | addr.l2_family = AF_BLUETOOTH; |
Marcel Holtmann | b4324b5 | 2009-06-07 18:06:51 +0200 | [diff] [blame] | 1992 | addr.l2_psm = cpu_to_le16(RFCOMM_PSM); |
Marcel Holtmann | 37e62f5 | 2009-02-17 21:49:33 +0100 | [diff] [blame] | 1993 | addr.l2_cid = 0; |
Marcel Holtmann | 48db9ca | 2007-05-05 00:36:06 +0200 | [diff] [blame] | 1994 | err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | if (err < 0) { |
| 1996 | BT_ERR("Bind failed %d", err); |
| 1997 | goto failed; |
| 1998 | } |
| 1999 | |
| 2000 | /* Set L2CAP options */ |
| 2001 | sk = sock->sk; |
| 2002 | lock_sock(sk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2003 | l2cap_pi(sk)->imtu = l2cap_mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | release_sock(sk); |
| 2005 | |
| 2006 | /* Start listening on the socket */ |
Marcel Holtmann | 48db9ca | 2007-05-05 00:36:06 +0200 | [diff] [blame] | 2007 | err = kernel_listen(sock, 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | if (err) { |
| 2009 | BT_ERR("Listen failed %d", err); |
| 2010 | goto failed; |
| 2011 | } |
| 2012 | |
| 2013 | /* Add listening session */ |
| 2014 | s = rfcomm_session_add(sock, BT_LISTEN); |
| 2015 | if (!s) |
| 2016 | goto failed; |
| 2017 | |
| 2018 | rfcomm_session_hold(s); |
| 2019 | return 0; |
| 2020 | failed: |
| 2021 | sock_release(sock); |
| 2022 | return err; |
| 2023 | } |
| 2024 | |
| 2025 | static void rfcomm_kill_listener(void) |
| 2026 | { |
| 2027 | struct rfcomm_session *s; |
| 2028 | struct list_head *p, *n; |
| 2029 | |
| 2030 | BT_DBG(""); |
| 2031 | |
| 2032 | list_for_each_safe(p, n, &session_list) { |
| 2033 | s = list_entry(p, struct rfcomm_session, list); |
| 2034 | rfcomm_session_del(s); |
| 2035 | } |
| 2036 | } |
| 2037 | |
| 2038 | static int rfcomm_run(void *unused) |
| 2039 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | BT_DBG(""); |
| 2041 | |
Marcel Holtmann | a524ecc | 2007-10-20 21:37:20 +0200 | [diff] [blame] | 2042 | set_user_nice(current, -10); |
| 2043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | rfcomm_add_listener(BDADDR_ANY); |
| 2045 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2046 | while (!kthread_should_stop()) { |
Marcel Holtmann | a524ecc | 2007-10-20 21:37:20 +0200 | [diff] [blame] | 2047 | set_current_state(TASK_INTERRUPTIBLE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2048 | if (!test_bit(RFCOMM_SCHED_WAKEUP, &rfcomm_event)) { |
| 2049 | /* No pending events. Let's sleep. |
| 2050 | * Incoming connections and data will wake us up. */ |
| 2051 | schedule(); |
| 2052 | } |
| 2053 | set_current_state(TASK_RUNNING); |
Marcel Holtmann | a524ecc | 2007-10-20 21:37:20 +0200 | [diff] [blame] | 2054 | |
| 2055 | /* Process stuff */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2056 | clear_bit(RFCOMM_SCHED_WAKEUP, &rfcomm_event); |
Marcel Holtmann | a524ecc | 2007-10-20 21:37:20 +0200 | [diff] [blame] | 2057 | rfcomm_process_sessions(); |
| 2058 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | |
| 2060 | rfcomm_kill_listener(); |
| 2061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | return 0; |
| 2063 | } |
| 2064 | |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 2065 | static void rfcomm_security_cfm(struct hci_conn *conn, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | { |
| 2067 | struct rfcomm_session *s; |
| 2068 | struct rfcomm_dlc *d; |
| 2069 | struct list_head *p, *n; |
| 2070 | |
| 2071 | BT_DBG("conn %p status 0x%02x encrypt 0x%02x", conn, status, encrypt); |
| 2072 | |
| 2073 | s = rfcomm_session_get(&conn->hdev->bdaddr, &conn->dst); |
| 2074 | if (!s) |
| 2075 | return; |
| 2076 | |
| 2077 | rfcomm_session_hold(s); |
| 2078 | |
| 2079 | list_for_each_safe(p, n, &s->dlcs) { |
| 2080 | d = list_entry(p, struct rfcomm_dlc, list); |
| 2081 | |
Marcel Holtmann | 8c84b83 | 2009-01-16 08:17:51 +0100 | [diff] [blame] | 2082 | if (test_and_clear_bit(RFCOMM_SEC_PENDING, &d->flags)) { |
| 2083 | rfcomm_dlc_clear_timer(d); |
| 2084 | if (status || encrypt == 0x00) { |
| 2085 | __rfcomm_dlc_close(d, ECONNREFUSED); |
| 2086 | continue; |
| 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | if (d->state == BT_CONNECTED && !status && encrypt == 0x00) { |
| 2091 | if (d->sec_level == BT_SECURITY_MEDIUM) { |
| 2092 | set_bit(RFCOMM_SEC_PENDING, &d->flags); |
| 2093 | rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT); |
| 2094 | continue; |
| 2095 | } else if (d->sec_level == BT_SECURITY_HIGH) { |
| 2096 | __rfcomm_dlc_close(d, ECONNREFUSED); |
| 2097 | continue; |
| 2098 | } |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 2099 | } |
| 2100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | if (!test_and_clear_bit(RFCOMM_AUTH_PENDING, &d->flags)) |
| 2102 | continue; |
| 2103 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2104 | if (!status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | set_bit(RFCOMM_AUTH_ACCEPT, &d->flags); |
| 2106 | else |
| 2107 | set_bit(RFCOMM_AUTH_REJECT, &d->flags); |
| 2108 | } |
| 2109 | |
| 2110 | rfcomm_session_put(s); |
| 2111 | |
Andrei Emeltchenko | 534c92f | 2010-10-01 12:05:11 +0300 | [diff] [blame] | 2112 | rfcomm_schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | static struct hci_cb rfcomm_cb = { |
| 2116 | .name = "RFCOMM", |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 2117 | .security_cfm = rfcomm_security_cfm |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | }; |
| 2119 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 2120 | static int rfcomm_dlc_debugfs_show(struct seq_file *f, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | { |
| 2122 | struct rfcomm_session *s; |
| 2123 | struct list_head *pp, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | |
| 2125 | rfcomm_lock(); |
| 2126 | |
| 2127 | list_for_each(p, &session_list) { |
| 2128 | s = list_entry(p, struct rfcomm_session, list); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 2129 | list_for_each(pp, &s->dlcs) { |
| 2130 | struct sock *sk = s->sock->sk; |
| 2131 | struct rfcomm_dlc *d = list_entry(pp, struct rfcomm_dlc, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 2133 | seq_printf(f, "%s %s %ld %d %d %d %d\n", |
| 2134 | batostr(&bt_sk(sk)->src), |
| 2135 | batostr(&bt_sk(sk)->dst), |
| 2136 | d->state, d->dlci, d->mtu, |
| 2137 | d->rx_credits, d->tx_credits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | } |
| 2139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | rfcomm_unlock(); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 2142 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 2143 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | } |
| 2145 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 2146 | static int rfcomm_dlc_debugfs_open(struct inode *inode, struct file *file) |
| 2147 | { |
| 2148 | return single_open(file, rfcomm_dlc_debugfs_show, inode->i_private); |
| 2149 | } |
| 2150 | |
| 2151 | static const struct file_operations rfcomm_dlc_debugfs_fops = { |
| 2152 | .open = rfcomm_dlc_debugfs_open, |
| 2153 | .read = seq_read, |
| 2154 | .llseek = seq_lseek, |
| 2155 | .release = single_release, |
| 2156 | }; |
| 2157 | |
| 2158 | static struct dentry *rfcomm_dlc_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | |
| 2160 | /* ---- Initialization ---- */ |
| 2161 | static int __init rfcomm_init(void) |
| 2162 | { |
Marcel Holtmann | 52d1834 | 2009-08-22 14:49:36 -0700 | [diff] [blame] | 2163 | int err; |
Dave Young | af0d3b1 | 2009-08-03 04:26:16 +0000 | [diff] [blame] | 2164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | hci_register_cb(&rfcomm_cb); |
| 2166 | |
Marcel Holtmann | a524ecc | 2007-10-20 21:37:20 +0200 | [diff] [blame] | 2167 | rfcomm_thread = kthread_run(rfcomm_run, NULL, "krfcommd"); |
| 2168 | if (IS_ERR(rfcomm_thread)) { |
Marcel Holtmann | 52d1834 | 2009-08-22 14:49:36 -0700 | [diff] [blame] | 2169 | err = PTR_ERR(rfcomm_thread); |
| 2170 | goto unregister; |
Marcel Holtmann | a524ecc | 2007-10-20 21:37:20 +0200 | [diff] [blame] | 2171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 2173 | if (bt_debugfs) { |
| 2174 | rfcomm_dlc_debugfs = debugfs_create_file("rfcomm_dlc", 0444, |
| 2175 | bt_debugfs, NULL, &rfcomm_dlc_debugfs_fops); |
| 2176 | if (!rfcomm_dlc_debugfs) |
| 2177 | BT_ERR("Failed to create RFCOMM debug file"); |
| 2178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | |
Marcel Holtmann | 52d1834 | 2009-08-22 14:49:36 -0700 | [diff] [blame] | 2180 | err = rfcomm_init_ttys(); |
| 2181 | if (err < 0) |
| 2182 | goto stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | |
Marcel Holtmann | 52d1834 | 2009-08-22 14:49:36 -0700 | [diff] [blame] | 2184 | err = rfcomm_init_sockets(); |
| 2185 | if (err < 0) |
| 2186 | goto cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 2188 | BT_INFO("RFCOMM ver %s", VERSION); |
| 2189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | return 0; |
Dave Young | af0d3b1 | 2009-08-03 04:26:16 +0000 | [diff] [blame] | 2191 | |
Marcel Holtmann | 52d1834 | 2009-08-22 14:49:36 -0700 | [diff] [blame] | 2192 | cleanup: |
Dave Young | af0d3b1 | 2009-08-03 04:26:16 +0000 | [diff] [blame] | 2193 | rfcomm_cleanup_ttys(); |
Marcel Holtmann | 52d1834 | 2009-08-22 14:49:36 -0700 | [diff] [blame] | 2194 | |
| 2195 | stop: |
Dave Young | af0d3b1 | 2009-08-03 04:26:16 +0000 | [diff] [blame] | 2196 | kthread_stop(rfcomm_thread); |
Marcel Holtmann | 52d1834 | 2009-08-22 14:49:36 -0700 | [diff] [blame] | 2197 | |
| 2198 | unregister: |
Dave Young | af0d3b1 | 2009-08-03 04:26:16 +0000 | [diff] [blame] | 2199 | hci_unregister_cb(&rfcomm_cb); |
| 2200 | |
Marcel Holtmann | 52d1834 | 2009-08-22 14:49:36 -0700 | [diff] [blame] | 2201 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | static void __exit rfcomm_exit(void) |
| 2205 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 2206 | debugfs_remove(rfcomm_dlc_debugfs); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 2207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | hci_unregister_cb(&rfcomm_cb); |
| 2209 | |
Marcel Holtmann | a524ecc | 2007-10-20 21:37:20 +0200 | [diff] [blame] | 2210 | kthread_stop(rfcomm_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | rfcomm_cleanup_ttys(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | |
| 2214 | rfcomm_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | module_init(rfcomm_init); |
| 2218 | module_exit(rfcomm_exit); |
| 2219 | |
Marcel Holtmann | 7c2660b | 2006-07-03 10:02:51 +0200 | [diff] [blame] | 2220 | module_param(disable_cfc, bool, 0644); |
| 2221 | MODULE_PARM_DESC(disable_cfc, "Disable credit based flow control"); |
| 2222 | |
Marcel Holtmann | 98bcd08 | 2006-07-14 11:42:12 +0200 | [diff] [blame] | 2223 | module_param(channel_mtu, int, 0644); |
| 2224 | MODULE_PARM_DESC(channel_mtu, "Default MTU for the RFCOMM channel"); |
| 2225 | |
Marcel Holtmann | 56f3a40 | 2006-02-13 11:39:57 +0100 | [diff] [blame] | 2226 | module_param(l2cap_mtu, uint, 0644); |
| 2227 | MODULE_PARM_DESC(l2cap_mtu, "Default MTU for the L2CAP connection"); |
| 2228 | |
Marcel Holtmann | eae38ee | 2009-10-05 12:23:48 +0200 | [diff] [blame] | 2229 | module_param(l2cap_ertm, bool, 0644); |
| 2230 | MODULE_PARM_DESC(l2cap_ertm, "Use L2CAP ERTM mode for connection"); |
| 2231 | |
Marcel Holtmann | 63fbd24 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 2232 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | MODULE_DESCRIPTION("Bluetooth RFCOMM ver " VERSION); |
| 2234 | MODULE_VERSION(VERSION); |
| 2235 | MODULE_LICENSE("GPL"); |
| 2236 | MODULE_ALIAS("bt-proto-3"); |