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 | CMTP implementation for Linux Bluetooth stack (BlueZ). |
| 3 | Copyright (C) 2002-2003 Marcel Holtmann <marcel@holtmann.org> |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License version 2 as |
| 7 | published by the Free Software Foundation; |
| 8 | |
| 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 10 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 11 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 12 | 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] | 13 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 14 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 18 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 19 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | SOFTWARE IS DISCLAIMED. |
| 21 | */ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/sched.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/poll.h> |
| 31 | #include <linux/fcntl.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 32 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/skbuff.h> |
| 34 | #include <linux/socket.h> |
| 35 | #include <linux/ioctl.h> |
| 36 | #include <linux/file.h> |
| 37 | #include <linux/init.h> |
Szymon Janc | fada4ac | 2011-03-21 14:20:06 +0100 | [diff] [blame] | 38 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <net/sock.h> |
| 40 | |
| 41 | #include <linux/isdn/capilli.h> |
| 42 | |
| 43 | #include <net/bluetooth/bluetooth.h> |
| 44 | #include <net/bluetooth/l2cap.h> |
| 45 | |
| 46 | #include "cmtp.h" |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #define VERSION "1.0" |
| 49 | |
| 50 | static DECLARE_RWSEM(cmtp_session_sem); |
| 51 | static LIST_HEAD(cmtp_session_list); |
| 52 | |
| 53 | static struct cmtp_session *__cmtp_get_session(bdaddr_t *bdaddr) |
| 54 | { |
| 55 | struct cmtp_session *session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | BT_DBG(""); |
| 58 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 59 | list_for_each_entry(session, &cmtp_session_list, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | if (!bacmp(bdaddr, &session->bdaddr)) |
| 61 | return session; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | static void __cmtp_link_session(struct cmtp_session *session) |
| 67 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | list_add(&session->list, &cmtp_session_list); |
| 69 | } |
| 70 | |
| 71 | static void __cmtp_unlink_session(struct cmtp_session *session) |
| 72 | { |
| 73 | list_del(&session->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void __cmtp_copy_session(struct cmtp_session *session, struct cmtp_conninfo *ci) |
| 77 | { |
Marcel Holtmann | 8bf17a3 | 2015-04-01 13:51:52 -0700 | [diff] [blame] | 78 | u32 valid_flags = BIT(CMTP_LOOPBACK); |
Vasiliy Kulikov | 3185fbd | 2010-10-30 18:26:26 +0400 | [diff] [blame] | 79 | memset(ci, 0, sizeof(*ci)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | bacpy(&ci->bdaddr, &session->bdaddr); |
| 81 | |
Marcel Holtmann | 8bf17a3 | 2015-04-01 13:51:52 -0700 | [diff] [blame] | 82 | ci->flags = session->flags & valid_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | ci->state = session->state; |
| 84 | |
| 85 | ci->num = session->num; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | static inline int cmtp_alloc_block_id(struct cmtp_session *session) |
| 90 | { |
| 91 | int i, id = -1; |
| 92 | |
| 93 | for (i = 0; i < 16; i++) |
| 94 | if (!test_and_set_bit(i, &session->blockids)) { |
| 95 | id = i; |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | return id; |
| 100 | } |
| 101 | |
| 102 | static inline void cmtp_free_block_id(struct cmtp_session *session, int id) |
| 103 | { |
| 104 | clear_bit(id, &session->blockids); |
| 105 | } |
| 106 | |
| 107 | static inline void cmtp_add_msgpart(struct cmtp_session *session, int id, const unsigned char *buf, int count) |
| 108 | { |
| 109 | struct sk_buff *skb = session->reassembly[id], *nskb; |
| 110 | int size; |
| 111 | |
| 112 | BT_DBG("session %p buf %p count %d", session, buf, count); |
| 113 | |
| 114 | size = (skb) ? skb->len + count : count; |
| 115 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 116 | nskb = alloc_skb(size, GFP_ATOMIC); |
| 117 | if (!nskb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | BT_ERR("Can't allocate memory for CAPI message"); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | if (skb && (skb->len > 0)) |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 123 | skb_copy_from_linear_data(skb, skb_put(nskb, skb->len), skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | memcpy(skb_put(nskb, count), buf, count); |
| 126 | |
| 127 | session->reassembly[id] = nskb; |
| 128 | |
Wei Yongjun | 7585b97 | 2009-02-25 18:29:52 +0800 | [diff] [blame] | 129 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static inline int cmtp_recv_frame(struct cmtp_session *session, struct sk_buff *skb) |
| 133 | { |
| 134 | __u8 hdr, hdrlen, id; |
| 135 | __u16 len; |
| 136 | |
| 137 | BT_DBG("session %p skb %p len %d", session, skb, skb->len); |
| 138 | |
| 139 | while (skb->len > 0) { |
| 140 | hdr = skb->data[0]; |
| 141 | |
| 142 | switch (hdr & 0xc0) { |
| 143 | case 0x40: |
| 144 | hdrlen = 2; |
| 145 | len = skb->data[1]; |
| 146 | break; |
| 147 | case 0x80: |
| 148 | hdrlen = 3; |
| 149 | len = skb->data[1] | (skb->data[2] << 8); |
| 150 | break; |
| 151 | default: |
| 152 | hdrlen = 1; |
| 153 | len = 0; |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | id = (hdr & 0x3c) >> 2; |
| 158 | |
| 159 | BT_DBG("hdr 0x%02x hdrlen %d len %d id %d", hdr, hdrlen, len, id); |
| 160 | |
| 161 | if (hdrlen + len > skb->len) { |
| 162 | BT_ERR("Wrong size or header information in CMTP frame"); |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | if (len == 0) { |
| 167 | skb_pull(skb, hdrlen); |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | switch (hdr & 0x03) { |
| 172 | case 0x00: |
| 173 | cmtp_add_msgpart(session, id, skb->data + hdrlen, len); |
| 174 | cmtp_recv_capimsg(session, session->reassembly[id]); |
| 175 | session->reassembly[id] = NULL; |
| 176 | break; |
| 177 | case 0x01: |
| 178 | cmtp_add_msgpart(session, id, skb->data + hdrlen, len); |
| 179 | break; |
| 180 | default: |
Markus Elfring | 06fbb3d | 2015-11-14 22:00:27 +0100 | [diff] [blame] | 181 | kfree_skb(session->reassembly[id]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | session->reassembly[id] = NULL; |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | skb_pull(skb, hdrlen + len); |
| 187 | } |
| 188 | |
| 189 | kfree_skb(skb); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int cmtp_send_frame(struct cmtp_session *session, unsigned char *data, int len) |
| 194 | { |
| 195 | struct socket *sock = session->sock; |
| 196 | struct kvec iv = { data, len }; |
| 197 | struct msghdr msg; |
| 198 | |
| 199 | BT_DBG("session %p data %p len %d", session, data, len); |
| 200 | |
| 201 | if (!len) |
| 202 | return 0; |
| 203 | |
| 204 | memset(&msg, 0, sizeof(msg)); |
| 205 | |
| 206 | return kernel_sendmsg(sock, &msg, &iv, 1, len); |
| 207 | } |
| 208 | |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 209 | static void cmtp_process_transmit(struct cmtp_session *session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
| 211 | struct sk_buff *skb, *nskb; |
| 212 | unsigned char *hdr; |
| 213 | unsigned int size, tail; |
| 214 | |
| 215 | BT_DBG("session %p", session); |
| 216 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 217 | nskb = alloc_skb(session->mtu, GFP_ATOMIC); |
| 218 | if (!nskb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | BT_ERR("Can't allocate memory for new frame"); |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 220 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | while ((skb = skb_dequeue(&session->transmit))) { |
| 224 | struct cmtp_scb *scb = (void *) skb->cb; |
| 225 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 226 | tail = session->mtu - nskb->len; |
| 227 | if (tail < 5) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | cmtp_send_frame(session, nskb->data, nskb->len); |
| 229 | skb_trim(nskb, 0); |
| 230 | tail = session->mtu; |
| 231 | } |
| 232 | |
| 233 | size = min_t(uint, ((tail < 258) ? (tail - 2) : (tail - 3)), skb->len); |
| 234 | |
Szymon Janc | 58aac46 | 2011-03-21 14:20:03 +0100 | [diff] [blame] | 235 | if (scb->id < 0) { |
| 236 | scb->id = cmtp_alloc_block_id(session); |
| 237 | if (scb->id < 0) { |
| 238 | skb_queue_head(&session->transmit, skb); |
| 239 | break; |
| 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if (size < 256) { |
| 244 | hdr = skb_put(nskb, 2); |
| 245 | hdr[0] = 0x40 |
| 246 | | ((scb->id << 2) & 0x3c) |
| 247 | | ((skb->len == size) ? 0x00 : 0x01); |
| 248 | hdr[1] = size; |
| 249 | } else { |
| 250 | hdr = skb_put(nskb, 3); |
| 251 | hdr[0] = 0x80 |
| 252 | | ((scb->id << 2) & 0x3c) |
| 253 | | ((skb->len == size) ? 0x00 : 0x01); |
| 254 | hdr[1] = size & 0xff; |
| 255 | hdr[2] = size >> 8; |
| 256 | } |
| 257 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 258 | skb_copy_from_linear_data(skb, skb_put(nskb, size), size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | skb_pull(skb, size); |
| 260 | |
| 261 | if (skb->len > 0) { |
| 262 | skb_queue_head(&session->transmit, skb); |
| 263 | } else { |
| 264 | cmtp_free_block_id(session, scb->id); |
| 265 | if (scb->data) { |
| 266 | cmtp_send_frame(session, nskb->data, nskb->len); |
| 267 | skb_trim(nskb, 0); |
| 268 | } |
| 269 | kfree_skb(skb); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | cmtp_send_frame(session, nskb->data, nskb->len); |
| 274 | |
| 275 | kfree_skb(nskb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static int cmtp_session(void *arg) |
| 279 | { |
| 280 | struct cmtp_session *session = arg; |
| 281 | struct sock *sk = session->sock->sk; |
| 282 | struct sk_buff *skb; |
| 283 | wait_queue_t wait; |
| 284 | |
| 285 | BT_DBG("session %p", session); |
| 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | set_user_nice(current, -15); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | init_waitqueue_entry(&wait, current); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 290 | add_wait_queue(sk_sleep(sk), &wait); |
Peter Hurley | 3a3f5c7 | 2011-07-24 00:11:10 -0400 | [diff] [blame] | 291 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | set_current_state(TASK_INTERRUPTIBLE); |
| 293 | |
Peter Hurley | 7176522 | 2011-08-05 10:44:21 -0400 | [diff] [blame] | 294 | if (atomic_read(&session->terminate)) |
Peter Hurley | 3a3f5c7 | 2011-07-24 00:11:10 -0400 | [diff] [blame] | 295 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | if (sk->sk_state != BT_CONNECTED) |
| 297 | break; |
| 298 | |
| 299 | while ((skb = skb_dequeue(&sk->sk_receive_queue))) { |
| 300 | skb_orphan(skb); |
Mat Martineau | 4493572 | 2011-07-22 14:53:58 -0700 | [diff] [blame] | 301 | if (!skb_linearize(skb)) |
| 302 | cmtp_recv_frame(session, skb); |
| 303 | else |
| 304 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | cmtp_process_transmit(session); |
| 308 | |
| 309 | schedule(); |
| 310 | } |
Peter Hurley | 3a3f5c7 | 2011-07-24 00:11:10 -0400 | [diff] [blame] | 311 | __set_current_state(TASK_RUNNING); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 312 | remove_wait_queue(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | down_write(&cmtp_session_sem); |
| 315 | |
Marcel Holtmann | b2ddeb1 | 2015-04-03 11:02:09 -0700 | [diff] [blame] | 316 | if (!(session->flags & BIT(CMTP_LOOPBACK))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | cmtp_detach_device(session); |
| 318 | |
| 319 | fput(session->sock->file); |
| 320 | |
| 321 | __cmtp_unlink_session(session); |
| 322 | |
| 323 | up_write(&cmtp_session_sem); |
| 324 | |
| 325 | kfree(session); |
David Herrmann | 48b28b8 | 2011-11-19 13:23:32 +0100 | [diff] [blame] | 326 | module_put_and_exit(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock) |
| 331 | { |
Marcel Holtmann | 8bf17a3 | 2015-04-01 13:51:52 -0700 | [diff] [blame] | 332 | u32 valid_flags = BIT(CMTP_LOOPBACK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | struct cmtp_session *session, *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | int i, err; |
| 335 | |
| 336 | BT_DBG(""); |
| 337 | |
Al Viro | 96c2665 | 2014-12-19 06:20:58 +0000 | [diff] [blame] | 338 | if (!l2cap_is_socket(sock)) |
| 339 | return -EBADFD; |
| 340 | |
Marcel Holtmann | 8bf17a3 | 2015-04-01 13:51:52 -0700 | [diff] [blame] | 341 | if (req->flags & ~valid_flags) |
| 342 | return -EINVAL; |
| 343 | |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 344 | session = kzalloc(sizeof(struct cmtp_session), GFP_KERNEL); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 345 | if (!session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | down_write(&cmtp_session_sem); |
| 349 | |
Marcel Holtmann | 88f1fd2 | 2013-10-13 09:49:55 -0700 | [diff] [blame] | 350 | s = __cmtp_get_session(&l2cap_pi(sock->sk)->chan->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | if (s && s->state == BT_CONNECTED) { |
| 352 | err = -EEXIST; |
| 353 | goto failed; |
| 354 | } |
| 355 | |
Marcel Holtmann | 88f1fd2 | 2013-10-13 09:49:55 -0700 | [diff] [blame] | 356 | bacpy(&session->bdaddr, &l2cap_pi(sock->sk)->chan->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 358 | session->mtu = min_t(uint, l2cap_pi(sock->sk)->chan->omtu, |
| 359 | l2cap_pi(sock->sk)->chan->imtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | BT_DBG("mtu %d", session->mtu); |
| 362 | |
Marcel Holtmann | 88f1fd2 | 2013-10-13 09:49:55 -0700 | [diff] [blame] | 363 | sprintf(session->name, "%pMR", &session->bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| 365 | session->sock = sock; |
| 366 | session->state = BT_CONFIG; |
| 367 | |
| 368 | init_waitqueue_head(&session->wait); |
| 369 | |
| 370 | session->msgnum = CMTP_INITIAL_MSGNUM; |
| 371 | |
| 372 | INIT_LIST_HEAD(&session->applications); |
| 373 | |
| 374 | skb_queue_head_init(&session->transmit); |
| 375 | |
| 376 | for (i = 0; i < 16; i++) |
| 377 | session->reassembly[i] = NULL; |
| 378 | |
| 379 | session->flags = req->flags; |
| 380 | |
| 381 | __cmtp_link_session(session); |
| 382 | |
David Herrmann | 48b28b8 | 2011-11-19 13:23:32 +0100 | [diff] [blame] | 383 | __module_get(THIS_MODULE); |
Szymon Janc | fada4ac | 2011-03-21 14:20:06 +0100 | [diff] [blame] | 384 | session->task = kthread_run(cmtp_session, session, "kcmtpd_ctr_%d", |
| 385 | session->num); |
| 386 | if (IS_ERR(session->task)) { |
David Herrmann | 48b28b8 | 2011-11-19 13:23:32 +0100 | [diff] [blame] | 387 | module_put(THIS_MODULE); |
Szymon Janc | fada4ac | 2011-03-21 14:20:06 +0100 | [diff] [blame] | 388 | err = PTR_ERR(session->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | goto unlink; |
Szymon Janc | fada4ac | 2011-03-21 14:20:06 +0100 | [diff] [blame] | 390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Marcel Holtmann | b2ddeb1 | 2015-04-03 11:02:09 -0700 | [diff] [blame] | 392 | if (!(session->flags & BIT(CMTP_LOOPBACK))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | err = cmtp_attach_device(session); |
Peter Hurley | 687beaa | 2011-08-05 10:53:52 -0400 | [diff] [blame] | 394 | if (err < 0) { |
| 395 | atomic_inc(&session->terminate); |
| 396 | wake_up_process(session->task); |
| 397 | up_write(&cmtp_session_sem); |
| 398 | return err; |
| 399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | up_write(&cmtp_session_sem); |
| 403 | return 0; |
| 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | unlink: |
| 406 | __cmtp_unlink_session(session); |
| 407 | |
| 408 | failed: |
| 409 | up_write(&cmtp_session_sem); |
| 410 | kfree(session); |
| 411 | return err; |
| 412 | } |
| 413 | |
| 414 | int cmtp_del_connection(struct cmtp_conndel_req *req) |
| 415 | { |
Marcel Holtmann | 8bf17a3 | 2015-04-01 13:51:52 -0700 | [diff] [blame] | 416 | u32 valid_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | struct cmtp_session *session; |
| 418 | int err = 0; |
| 419 | |
| 420 | BT_DBG(""); |
| 421 | |
Marcel Holtmann | 8bf17a3 | 2015-04-01 13:51:52 -0700 | [diff] [blame] | 422 | if (req->flags & ~valid_flags) |
| 423 | return -EINVAL; |
| 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | down_read(&cmtp_session_sem); |
| 426 | |
| 427 | session = __cmtp_get_session(&req->bdaddr); |
| 428 | if (session) { |
| 429 | /* Flush the transmit queue */ |
| 430 | skb_queue_purge(&session->transmit); |
| 431 | |
Szymon Janc | fada4ac | 2011-03-21 14:20:06 +0100 | [diff] [blame] | 432 | /* Stop session thread */ |
Peter Hurley | 7176522 | 2011-08-05 10:44:21 -0400 | [diff] [blame] | 433 | atomic_inc(&session->terminate); |
| 434 | wake_up_process(session->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } else |
| 436 | err = -ENOENT; |
| 437 | |
| 438 | up_read(&cmtp_session_sem); |
| 439 | return err; |
| 440 | } |
| 441 | |
| 442 | int cmtp_get_connlist(struct cmtp_connlist_req *req) |
| 443 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 444 | struct cmtp_session *session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | int err = 0, n = 0; |
| 446 | |
| 447 | BT_DBG(""); |
| 448 | |
| 449 | down_read(&cmtp_session_sem); |
| 450 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 451 | list_for_each_entry(session, &cmtp_session_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | struct cmtp_conninfo ci; |
| 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | __cmtp_copy_session(session, &ci); |
| 455 | |
| 456 | if (copy_to_user(req->ci, &ci, sizeof(ci))) { |
| 457 | err = -EFAULT; |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | if (++n >= req->cnum) |
| 462 | break; |
| 463 | |
| 464 | req->ci++; |
| 465 | } |
| 466 | req->cnum = n; |
| 467 | |
| 468 | up_read(&cmtp_session_sem); |
| 469 | return err; |
| 470 | } |
| 471 | |
| 472 | int cmtp_get_conninfo(struct cmtp_conninfo *ci) |
| 473 | { |
| 474 | struct cmtp_session *session; |
| 475 | int err = 0; |
| 476 | |
| 477 | down_read(&cmtp_session_sem); |
| 478 | |
| 479 | session = __cmtp_get_session(&ci->bdaddr); |
| 480 | if (session) |
| 481 | __cmtp_copy_session(session, ci); |
| 482 | else |
| 483 | err = -ENOENT; |
| 484 | |
| 485 | up_read(&cmtp_session_sem); |
| 486 | return err; |
| 487 | } |
| 488 | |
| 489 | |
| 490 | static int __init cmtp_init(void) |
| 491 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | BT_INFO("CMTP (CAPI Emulation) ver %s", VERSION); |
| 493 | |
| 494 | cmtp_init_sockets(); |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static void __exit cmtp_exit(void) |
| 500 | { |
| 501 | cmtp_cleanup_sockets(); |
| 502 | } |
| 503 | |
| 504 | module_init(cmtp_init); |
| 505 | module_exit(cmtp_exit); |
| 506 | |
| 507 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
| 508 | MODULE_DESCRIPTION("Bluetooth CMTP ver " VERSION); |
| 509 | MODULE_VERSION(VERSION); |
| 510 | MODULE_LICENSE("GPL"); |
| 511 | MODULE_ALIAS("bt-proto-5"); |