Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the |
| 16 | * Free Software Foundation, Inc., |
| 17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #define pr_fmt(fmt) "llcp: %s: " fmt, __func__ |
| 21 | |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/nfc.h> |
| 26 | |
| 27 | #include "../nfc.h" |
| 28 | #include "llcp.h" |
| 29 | |
| 30 | static u8 llcp_magic[3] = {0x46, 0x66, 0x6d}; |
| 31 | |
| 32 | static struct list_head llcp_devices; |
| 33 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 34 | void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk) |
| 35 | { |
| 36 | write_lock(&l->lock); |
| 37 | sk_add_node(sk, &l->head); |
| 38 | write_unlock(&l->lock); |
| 39 | } |
| 40 | |
| 41 | void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk) |
| 42 | { |
| 43 | write_lock(&l->lock); |
| 44 | sk_del_node_init(sk); |
| 45 | write_unlock(&l->lock); |
| 46 | } |
| 47 | |
Samuel Ortiz | 4d22ea1 | 2012-06-22 14:40:34 +0200 | [diff] [blame] | 48 | static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 49 | { |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 50 | struct sock *sk; |
| 51 | struct hlist_node *node, *tmp; |
| 52 | struct nfc_llcp_sock *llcp_sock; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 53 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 54 | write_lock(&local->sockets.lock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 55 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 56 | sk_for_each_safe(sk, node, tmp, &local->sockets.head) { |
| 57 | llcp_sock = nfc_llcp_sock(sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 58 | |
Szymon Janc | 50b78b2 | 2012-09-26 14:22:10 +0200 | [diff] [blame] | 59 | bh_lock_sock(sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 60 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 61 | if (sk->sk_state == LLCP_CONNECTED) |
| 62 | nfc_put_device(llcp_sock->dev); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 63 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 64 | if (sk->sk_state == LLCP_LISTEN) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 65 | struct nfc_llcp_sock *lsk, *n; |
| 66 | struct sock *accept_sk; |
| 67 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 68 | list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 69 | accept_queue) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 70 | accept_sk = &lsk->sk; |
Szymon Janc | 50b78b2 | 2012-09-26 14:22:10 +0200 | [diff] [blame] | 71 | bh_lock_sock(accept_sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 72 | |
| 73 | nfc_llcp_accept_unlink(accept_sk); |
| 74 | |
| 75 | accept_sk->sk_state = LLCP_CLOSED; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 76 | |
Szymon Janc | 50b78b2 | 2012-09-26 14:22:10 +0200 | [diff] [blame] | 77 | bh_unlock_sock(accept_sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 78 | |
| 79 | sock_orphan(accept_sk); |
| 80 | } |
Samuel Ortiz | 4d22ea1 | 2012-06-22 14:40:34 +0200 | [diff] [blame] | 81 | |
| 82 | if (listen == true) { |
Szymon Janc | 50b78b2 | 2012-09-26 14:22:10 +0200 | [diff] [blame] | 83 | bh_unlock_sock(sk); |
Samuel Ortiz | 4d22ea1 | 2012-06-22 14:40:34 +0200 | [diff] [blame] | 84 | continue; |
| 85 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Samuel Ortiz | c8512be | 2012-10-15 15:09:52 +0200 | [diff] [blame] | 88 | /* |
| 89 | * If we have a connection less socket bound, we keep it alive |
| 90 | * if the device is still present. |
| 91 | */ |
| 92 | if (sk->sk_state == LLCP_BOUND && sk->sk_type == SOCK_DGRAM && |
| 93 | listen == true) { |
| 94 | bh_unlock_sock(sk); |
| 95 | continue; |
| 96 | } |
| 97 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 98 | sk->sk_state = LLCP_CLOSED; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 99 | |
Szymon Janc | 50b78b2 | 2012-09-26 14:22:10 +0200 | [diff] [blame] | 100 | bh_unlock_sock(sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 101 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 102 | sock_orphan(sk); |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 103 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 104 | sk_del_node_init(sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 107 | write_unlock(&local->sockets.lock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 110 | struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local) |
| 111 | { |
| 112 | kref_get(&local->ref); |
| 113 | |
| 114 | return local; |
| 115 | } |
| 116 | |
| 117 | static void local_release(struct kref *ref) |
| 118 | { |
| 119 | struct nfc_llcp_local *local; |
| 120 | |
| 121 | local = container_of(ref, struct nfc_llcp_local, ref); |
| 122 | |
| 123 | list_del(&local->list); |
Samuel Ortiz | 4d22ea1 | 2012-06-22 14:40:34 +0200 | [diff] [blame] | 124 | nfc_llcp_socket_release(local, false); |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 125 | del_timer_sync(&local->link_timer); |
| 126 | skb_queue_purge(&local->tx_queue); |
Tejun Heo | 474fee3 | 2012-08-22 16:22:16 -0700 | [diff] [blame] | 127 | cancel_work_sync(&local->tx_work); |
| 128 | cancel_work_sync(&local->rx_work); |
| 129 | cancel_work_sync(&local->timeout_work); |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 130 | kfree_skb(local->rx_pending); |
| 131 | kfree(local); |
| 132 | } |
| 133 | |
| 134 | int nfc_llcp_local_put(struct nfc_llcp_local *local) |
| 135 | { |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 136 | if (local == NULL) |
| 137 | return 0; |
| 138 | |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 139 | return kref_put(&local->ref, local_release); |
| 140 | } |
| 141 | |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 142 | static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local, |
| 143 | u8 ssap, u8 dsap) |
| 144 | { |
| 145 | struct sock *sk; |
| 146 | struct hlist_node *node; |
Samuel Ortiz | a8df0f3 | 2012-10-16 21:15:59 +0200 | [diff] [blame^] | 147 | struct nfc_llcp_sock *llcp_sock, *tmp_sock; |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 148 | |
| 149 | pr_debug("ssap dsap %d %d\n", ssap, dsap); |
| 150 | |
| 151 | if (ssap == 0 && dsap == 0) |
| 152 | return NULL; |
| 153 | |
| 154 | read_lock(&local->sockets.lock); |
| 155 | |
| 156 | llcp_sock = NULL; |
| 157 | |
| 158 | sk_for_each(sk, node, &local->sockets.head) { |
Samuel Ortiz | a8df0f3 | 2012-10-16 21:15:59 +0200 | [diff] [blame^] | 159 | tmp_sock = nfc_llcp_sock(sk); |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 160 | |
Samuel Ortiz | a8df0f3 | 2012-10-16 21:15:59 +0200 | [diff] [blame^] | 161 | if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) { |
| 162 | llcp_sock = tmp_sock; |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 163 | break; |
Samuel Ortiz | a8df0f3 | 2012-10-16 21:15:59 +0200 | [diff] [blame^] | 164 | } |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | read_unlock(&local->sockets.lock); |
| 168 | |
| 169 | if (llcp_sock == NULL) |
| 170 | return NULL; |
| 171 | |
| 172 | sock_hold(&llcp_sock->sk); |
| 173 | |
| 174 | return llcp_sock; |
| 175 | } |
| 176 | |
| 177 | static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock) |
| 178 | { |
| 179 | sock_put(&sock->sk); |
| 180 | } |
| 181 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 182 | static void nfc_llcp_timeout_work(struct work_struct *work) |
| 183 | { |
| 184 | struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 185 | timeout_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 186 | |
| 187 | nfc_dep_link_down(local->dev); |
| 188 | } |
| 189 | |
| 190 | static void nfc_llcp_symm_timer(unsigned long data) |
| 191 | { |
| 192 | struct nfc_llcp_local *local = (struct nfc_llcp_local *) data; |
| 193 | |
| 194 | pr_err("SYMM timeout\n"); |
| 195 | |
Linus Torvalds | 916082b | 2012-10-02 16:01:31 -0700 | [diff] [blame] | 196 | schedule_work(&local->timeout_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev) |
| 200 | { |
| 201 | struct nfc_llcp_local *local, *n; |
| 202 | |
| 203 | list_for_each_entry_safe(local, n, &llcp_devices, list) |
| 204 | if (local->dev == dev) |
| 205 | return local; |
| 206 | |
| 207 | pr_debug("No device found\n"); |
| 208 | |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | static char *wks[] = { |
| 213 | NULL, |
| 214 | NULL, /* SDP */ |
| 215 | "urn:nfc:sn:ip", |
| 216 | "urn:nfc:sn:obex", |
| 217 | "urn:nfc:sn:snep", |
| 218 | }; |
| 219 | |
| 220 | static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len) |
| 221 | { |
| 222 | int sap, num_wks; |
| 223 | |
| 224 | pr_debug("%s\n", service_name); |
| 225 | |
| 226 | if (service_name == NULL) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | num_wks = ARRAY_SIZE(wks); |
| 230 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 231 | for (sap = 0; sap < num_wks; sap++) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 232 | if (wks[sap] == NULL) |
| 233 | continue; |
| 234 | |
| 235 | if (strncmp(wks[sap], service_name, service_name_len) == 0) |
| 236 | return sap; |
| 237 | } |
| 238 | |
| 239 | return -EINVAL; |
| 240 | } |
| 241 | |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 242 | static |
| 243 | struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local, |
| 244 | u8 *sn, size_t sn_len) |
| 245 | { |
| 246 | struct sock *sk; |
| 247 | struct hlist_node *node; |
| 248 | struct nfc_llcp_sock *llcp_sock, *tmp_sock; |
| 249 | |
| 250 | pr_debug("sn %zd %p\n", sn_len, sn); |
| 251 | |
| 252 | if (sn == NULL || sn_len == 0) |
| 253 | return NULL; |
| 254 | |
| 255 | read_lock(&local->sockets.lock); |
| 256 | |
| 257 | llcp_sock = NULL; |
| 258 | |
| 259 | sk_for_each(sk, node, &local->sockets.head) { |
| 260 | tmp_sock = nfc_llcp_sock(sk); |
| 261 | |
| 262 | pr_debug("llcp sock %p\n", tmp_sock); |
| 263 | |
Samuel Ortiz | 54292d6 | 2012-10-15 15:08:29 +0200 | [diff] [blame] | 264 | if (tmp_sock->sk.sk_type == SOCK_STREAM && |
| 265 | tmp_sock->sk.sk_state != LLCP_LISTEN) |
| 266 | continue; |
| 267 | |
| 268 | if (tmp_sock->sk.sk_type == SOCK_DGRAM && |
| 269 | tmp_sock->sk.sk_state != LLCP_BOUND) |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 270 | continue; |
| 271 | |
| 272 | if (tmp_sock->service_name == NULL || |
| 273 | tmp_sock->service_name_len == 0) |
| 274 | continue; |
| 275 | |
| 276 | if (tmp_sock->service_name_len != sn_len) |
| 277 | continue; |
| 278 | |
| 279 | if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) { |
| 280 | llcp_sock = tmp_sock; |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | read_unlock(&local->sockets.lock); |
| 286 | |
| 287 | pr_debug("Found llcp sock %p\n", llcp_sock); |
| 288 | |
| 289 | return llcp_sock; |
| 290 | } |
| 291 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 292 | u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 293 | struct nfc_llcp_sock *sock) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 294 | { |
| 295 | mutex_lock(&local->sdp_lock); |
| 296 | |
| 297 | if (sock->service_name != NULL && sock->service_name_len > 0) { |
| 298 | int ssap = nfc_llcp_wks_sap(sock->service_name, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 299 | sock->service_name_len); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 300 | |
| 301 | if (ssap > 0) { |
| 302 | pr_debug("WKS %d\n", ssap); |
| 303 | |
| 304 | /* This is a WKS, let's check if it's free */ |
| 305 | if (local->local_wks & BIT(ssap)) { |
| 306 | mutex_unlock(&local->sdp_lock); |
| 307 | |
| 308 | return LLCP_SAP_MAX; |
| 309 | } |
| 310 | |
Samuel Ortiz | 1762c17 | 2012-03-05 01:03:38 +0100 | [diff] [blame] | 311 | set_bit(ssap, &local->local_wks); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 312 | mutex_unlock(&local->sdp_lock); |
| 313 | |
| 314 | return ssap; |
| 315 | } |
| 316 | |
| 317 | /* |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 318 | * Check if there already is a non WKS socket bound |
| 319 | * to this service name. |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 320 | */ |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 321 | if (nfc_llcp_sock_from_sn(local, sock->service_name, |
| 322 | sock->service_name_len) != NULL) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 323 | mutex_unlock(&local->sdp_lock); |
| 324 | |
| 325 | return LLCP_SAP_MAX; |
| 326 | } |
| 327 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 328 | mutex_unlock(&local->sdp_lock); |
| 329 | |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 330 | return LLCP_SDP_UNBOUND; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 331 | |
Samuel Ortiz | ebbb16d | 2012-06-22 17:15:20 +0200 | [diff] [blame] | 332 | } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) { |
| 333 | if (!test_bit(sock->ssap, &local->local_wks)) { |
| 334 | set_bit(sock->ssap, &local->local_wks); |
| 335 | mutex_unlock(&local->sdp_lock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 336 | |
Samuel Ortiz | ebbb16d | 2012-06-22 17:15:20 +0200 | [diff] [blame] | 337 | return sock->ssap; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | mutex_unlock(&local->sdp_lock); |
| 342 | |
| 343 | return LLCP_SAP_MAX; |
| 344 | } |
| 345 | |
| 346 | u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local) |
| 347 | { |
| 348 | u8 local_ssap; |
| 349 | |
| 350 | mutex_lock(&local->sdp_lock); |
| 351 | |
| 352 | local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP); |
| 353 | if (local_ssap == LLCP_LOCAL_NUM_SAP) { |
| 354 | mutex_unlock(&local->sdp_lock); |
| 355 | return LLCP_SAP_MAX; |
| 356 | } |
| 357 | |
Samuel Ortiz | 1762c17 | 2012-03-05 01:03:38 +0100 | [diff] [blame] | 358 | set_bit(local_ssap, &local->local_sap); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 359 | |
| 360 | mutex_unlock(&local->sdp_lock); |
| 361 | |
| 362 | return local_ssap + LLCP_LOCAL_SAP_OFFSET; |
| 363 | } |
| 364 | |
| 365 | void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap) |
| 366 | { |
| 367 | u8 local_ssap; |
| 368 | unsigned long *sdp; |
| 369 | |
| 370 | if (ssap < LLCP_WKS_NUM_SAP) { |
| 371 | local_ssap = ssap; |
| 372 | sdp = &local->local_wks; |
| 373 | } else if (ssap < LLCP_LOCAL_NUM_SAP) { |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 374 | atomic_t *client_cnt; |
| 375 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 376 | local_ssap = ssap - LLCP_WKS_NUM_SAP; |
| 377 | sdp = &local->local_sdp; |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 378 | client_cnt = &local->local_sdp_cnt[local_ssap]; |
| 379 | |
| 380 | pr_debug("%d clients\n", atomic_read(client_cnt)); |
| 381 | |
| 382 | mutex_lock(&local->sdp_lock); |
| 383 | |
| 384 | if (atomic_dec_and_test(client_cnt)) { |
| 385 | struct nfc_llcp_sock *l_sock; |
| 386 | |
| 387 | pr_debug("No more clients for SAP %d\n", ssap); |
| 388 | |
| 389 | clear_bit(local_ssap, sdp); |
| 390 | |
| 391 | /* Find the listening sock and set it back to UNBOUND */ |
| 392 | l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP); |
| 393 | if (l_sock) { |
| 394 | l_sock->ssap = LLCP_SDP_UNBOUND; |
| 395 | nfc_llcp_sock_put(l_sock); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | mutex_unlock(&local->sdp_lock); |
| 400 | |
| 401 | return; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 402 | } else if (ssap < LLCP_MAX_SAP) { |
| 403 | local_ssap = ssap - LLCP_LOCAL_NUM_SAP; |
| 404 | sdp = &local->local_sap; |
| 405 | } else { |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | mutex_lock(&local->sdp_lock); |
| 410 | |
Samuel Ortiz | 1762c17 | 2012-03-05 01:03:38 +0100 | [diff] [blame] | 411 | clear_bit(local_ssap, sdp); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 412 | |
| 413 | mutex_unlock(&local->sdp_lock); |
| 414 | } |
| 415 | |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 416 | static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local) |
| 417 | { |
| 418 | u8 ssap; |
| 419 | |
| 420 | mutex_lock(&local->sdp_lock); |
| 421 | |
| 422 | ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP); |
| 423 | if (ssap == LLCP_SDP_NUM_SAP) { |
| 424 | mutex_unlock(&local->sdp_lock); |
| 425 | |
| 426 | return LLCP_SAP_MAX; |
| 427 | } |
| 428 | |
| 429 | pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap); |
| 430 | |
| 431 | set_bit(ssap, &local->local_sdp); |
| 432 | |
| 433 | mutex_unlock(&local->sdp_lock); |
| 434 | |
| 435 | return LLCP_WKS_NUM_SAP + ssap; |
| 436 | } |
| 437 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 438 | static int nfc_llcp_build_gb(struct nfc_llcp_local *local) |
| 439 | { |
| 440 | u8 *gb_cur, *version_tlv, version, version_length; |
| 441 | u8 *lto_tlv, lto, lto_length; |
| 442 | u8 *wks_tlv, wks_length; |
Samuel Ortiz | 56d5876 | 2012-04-10 19:43:19 +0200 | [diff] [blame] | 443 | u8 *miux_tlv, miux_length; |
| 444 | __be16 miux; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 445 | u8 gb_len = 0; |
Wei Yongjun | 52da244 | 2012-09-02 21:21:46 +0800 | [diff] [blame] | 446 | int ret = 0; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 447 | |
| 448 | version = LLCP_VERSION_11; |
| 449 | version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 450 | 1, &version_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 451 | gb_len += version_length; |
| 452 | |
| 453 | /* 1500 ms */ |
| 454 | lto = 150; |
Samuel Ortiz | 91b0ade | 2012-04-10 19:43:20 +0200 | [diff] [blame] | 455 | lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, <o, 1, <o_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 456 | gb_len += lto_length; |
| 457 | |
| 458 | pr_debug("Local wks 0x%lx\n", local->local_wks); |
| 459 | wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 460 | &wks_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 461 | gb_len += wks_length; |
| 462 | |
Samuel Ortiz | 56d5876 | 2012-04-10 19:43:19 +0200 | [diff] [blame] | 463 | miux = cpu_to_be16(LLCP_MAX_MIUX); |
| 464 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0, |
| 465 | &miux_length); |
| 466 | gb_len += miux_length; |
| 467 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 468 | gb_len += ARRAY_SIZE(llcp_magic); |
| 469 | |
| 470 | if (gb_len > NFC_MAX_GT_LEN) { |
Wei Yongjun | 52da244 | 2012-09-02 21:21:46 +0800 | [diff] [blame] | 471 | ret = -EINVAL; |
| 472 | goto out; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | gb_cur = local->gb; |
| 476 | |
| 477 | memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic)); |
| 478 | gb_cur += ARRAY_SIZE(llcp_magic); |
| 479 | |
| 480 | memcpy(gb_cur, version_tlv, version_length); |
| 481 | gb_cur += version_length; |
| 482 | |
| 483 | memcpy(gb_cur, lto_tlv, lto_length); |
| 484 | gb_cur += lto_length; |
| 485 | |
| 486 | memcpy(gb_cur, wks_tlv, wks_length); |
| 487 | gb_cur += wks_length; |
| 488 | |
Samuel Ortiz | 56d5876 | 2012-04-10 19:43:19 +0200 | [diff] [blame] | 489 | memcpy(gb_cur, miux_tlv, miux_length); |
| 490 | gb_cur += miux_length; |
| 491 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 492 | local->gb_len = gb_len; |
| 493 | |
Wei Yongjun | 52da244 | 2012-09-02 21:21:46 +0800 | [diff] [blame] | 494 | out: |
| 495 | kfree(version_tlv); |
| 496 | kfree(lto_tlv); |
| 497 | kfree(wks_tlv); |
| 498 | kfree(miux_tlv); |
| 499 | |
| 500 | return ret; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Samuel Ortiz | b8e7a06 | 2012-06-22 02:04:53 +0200 | [diff] [blame] | 503 | u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len) |
| 504 | { |
| 505 | struct nfc_llcp_local *local; |
| 506 | |
| 507 | local = nfc_llcp_find_local(dev); |
| 508 | if (local == NULL) { |
| 509 | *general_bytes_len = 0; |
| 510 | return NULL; |
| 511 | } |
| 512 | |
| 513 | nfc_llcp_build_gb(local); |
| 514 | |
| 515 | *general_bytes_len = local->gb_len; |
| 516 | |
| 517 | return local->gb; |
| 518 | } |
| 519 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 520 | int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len) |
| 521 | { |
| 522 | struct nfc_llcp_local *local = nfc_llcp_find_local(dev); |
| 523 | |
| 524 | if (local == NULL) { |
| 525 | pr_err("No LLCP device\n"); |
| 526 | return -ENODEV; |
| 527 | } |
| 528 | |
| 529 | memset(local->remote_gb, 0, NFC_MAX_GT_LEN); |
| 530 | memcpy(local->remote_gb, gb, gb_len); |
| 531 | local->remote_gb_len = gb_len; |
| 532 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 533 | if (local->remote_gb == NULL || local->remote_gb_len == 0) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 534 | return -ENODEV; |
| 535 | |
| 536 | if (memcmp(local->remote_gb, llcp_magic, 3)) { |
| 537 | pr_err("MAC does not support LLCP\n"); |
| 538 | return -EINVAL; |
| 539 | } |
| 540 | |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 541 | return nfc_llcp_parse_gb_tlv(local, |
| 542 | &local->remote_gb[3], |
| 543 | local->remote_gb_len - 3); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 544 | } |
| 545 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 546 | static u8 nfc_llcp_dsap(struct sk_buff *pdu) |
| 547 | { |
| 548 | return (pdu->data[0] & 0xfc) >> 2; |
| 549 | } |
| 550 | |
| 551 | static u8 nfc_llcp_ptype(struct sk_buff *pdu) |
| 552 | { |
| 553 | return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6); |
| 554 | } |
| 555 | |
| 556 | static u8 nfc_llcp_ssap(struct sk_buff *pdu) |
| 557 | { |
| 558 | return pdu->data[1] & 0x3f; |
| 559 | } |
| 560 | |
| 561 | static u8 nfc_llcp_ns(struct sk_buff *pdu) |
| 562 | { |
| 563 | return pdu->data[2] >> 4; |
| 564 | } |
| 565 | |
| 566 | static u8 nfc_llcp_nr(struct sk_buff *pdu) |
| 567 | { |
| 568 | return pdu->data[2] & 0xf; |
| 569 | } |
| 570 | |
| 571 | static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu) |
| 572 | { |
Samuel Ortiz | 279cf17 | 2012-04-10 19:43:14 +0200 | [diff] [blame] | 573 | pdu->data[2] = (sock->send_n << 4) | (sock->recv_n); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 574 | sock->send_n = (sock->send_n + 1) % 16; |
| 575 | sock->recv_ack_n = (sock->recv_n - 1) % 16; |
| 576 | } |
| 577 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 578 | void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local, |
| 579 | struct sk_buff *skb, u8 direction) |
| 580 | { |
| 581 | struct hlist_node *node; |
| 582 | struct sk_buff *skb_copy = NULL, *nskb; |
| 583 | struct sock *sk; |
| 584 | u8 *data; |
| 585 | |
| 586 | read_lock(&local->raw_sockets.lock); |
| 587 | |
| 588 | sk_for_each(sk, node, &local->raw_sockets.head) { |
| 589 | if (sk->sk_state != LLCP_BOUND) |
| 590 | continue; |
| 591 | |
| 592 | if (skb_copy == NULL) { |
| 593 | skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE, |
| 594 | GFP_ATOMIC); |
| 595 | |
| 596 | if (skb_copy == NULL) |
| 597 | continue; |
| 598 | |
| 599 | data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE); |
| 600 | |
| 601 | data[0] = local->dev ? local->dev->idx : 0xFF; |
| 602 | data[1] = direction; |
| 603 | } |
| 604 | |
| 605 | nskb = skb_clone(skb_copy, GFP_ATOMIC); |
| 606 | if (!nskb) |
| 607 | continue; |
| 608 | |
| 609 | if (sock_queue_rcv_skb(sk, nskb)) |
| 610 | kfree_skb(nskb); |
| 611 | } |
| 612 | |
| 613 | read_unlock(&local->raw_sockets.lock); |
| 614 | |
| 615 | kfree_skb(skb_copy); |
| 616 | } |
| 617 | |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 618 | static void nfc_llcp_tx_work(struct work_struct *work) |
| 619 | { |
| 620 | struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, |
| 621 | tx_work); |
| 622 | struct sk_buff *skb; |
| 623 | struct sock *sk; |
| 624 | struct nfc_llcp_sock *llcp_sock; |
| 625 | |
| 626 | skb = skb_dequeue(&local->tx_queue); |
| 627 | if (skb != NULL) { |
| 628 | sk = skb->sk; |
| 629 | llcp_sock = nfc_llcp_sock(sk); |
Samuel Ortiz | a6a0915 | 2012-10-05 00:37:22 +0200 | [diff] [blame] | 630 | |
| 631 | if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) { |
| 632 | nfc_llcp_send_symm(local->dev); |
| 633 | } else { |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 634 | int ret; |
| 635 | |
| 636 | pr_debug("Sending pending skb\n"); |
| 637 | print_hex_dump(KERN_DEBUG, "LLCP Tx: ", |
| 638 | DUMP_PREFIX_OFFSET, 16, 1, |
| 639 | skb->data, skb->len, true); |
| 640 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 641 | nfc_llcp_send_to_raw_sock(local, skb, |
| 642 | NFC_LLCP_DIRECTION_TX); |
| 643 | |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 644 | ret = nfc_data_exchange(local->dev, local->target_idx, |
| 645 | skb, nfc_llcp_recv, local); |
| 646 | |
| 647 | if (!ret && nfc_llcp_ptype(skb) == LLCP_PDU_I) { |
| 648 | skb = skb_get(skb); |
| 649 | skb_queue_tail(&llcp_sock->tx_pending_queue, |
| 650 | skb); |
| 651 | } |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 652 | } |
| 653 | } else { |
| 654 | nfc_llcp_send_symm(local->dev); |
| 655 | } |
| 656 | |
| 657 | mod_timer(&local->link_timer, |
| 658 | jiffies + msecs_to_jiffies(2 * local->remote_lto)); |
| 659 | } |
| 660 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 661 | static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local, |
| 662 | u8 ssap) |
| 663 | { |
| 664 | struct sock *sk; |
| 665 | struct nfc_llcp_sock *llcp_sock; |
| 666 | struct hlist_node *node; |
| 667 | |
| 668 | read_lock(&local->connecting_sockets.lock); |
| 669 | |
| 670 | sk_for_each(sk, node, &local->connecting_sockets.head) { |
| 671 | llcp_sock = nfc_llcp_sock(sk); |
| 672 | |
Samuel Ortiz | 5a0f6f3 | 2012-05-21 11:44:42 +0200 | [diff] [blame] | 673 | if (llcp_sock->ssap == ssap) { |
| 674 | sock_hold(&llcp_sock->sk); |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 675 | goto out; |
Samuel Ortiz | 5a0f6f3 | 2012-05-21 11:44:42 +0200 | [diff] [blame] | 676 | } |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | llcp_sock = NULL; |
| 680 | |
| 681 | out: |
| 682 | read_unlock(&local->connecting_sockets.lock); |
| 683 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 684 | return llcp_sock; |
| 685 | } |
| 686 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 687 | static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local, |
| 688 | u8 *sn, size_t sn_len) |
| 689 | { |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 690 | struct nfc_llcp_sock *llcp_sock; |
| 691 | |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 692 | llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 693 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 694 | if (llcp_sock == NULL) |
| 695 | return NULL; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 696 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 697 | sock_hold(&llcp_sock->sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 698 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 699 | return llcp_sock; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 700 | } |
| 701 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 702 | static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len) |
| 703 | { |
| 704 | u8 *tlv = &skb->data[2], type, length; |
| 705 | size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0; |
| 706 | |
| 707 | while (offset < tlv_array_len) { |
| 708 | type = tlv[0]; |
| 709 | length = tlv[1]; |
| 710 | |
| 711 | pr_debug("type 0x%x length %d\n", type, length); |
| 712 | |
| 713 | if (type == LLCP_TLV_SN) { |
| 714 | *sn_len = length; |
| 715 | return &tlv[2]; |
| 716 | } |
| 717 | |
| 718 | offset += length + 2; |
| 719 | tlv += length + 2; |
| 720 | } |
| 721 | |
| 722 | return NULL; |
| 723 | } |
| 724 | |
Samuel Ortiz | 968272b | 2012-10-15 16:14:37 +0200 | [diff] [blame] | 725 | static void nfc_llcp_recv_ui(struct nfc_llcp_local *local, |
| 726 | struct sk_buff *skb) |
| 727 | { |
| 728 | struct nfc_llcp_sock *llcp_sock; |
| 729 | struct nfc_llcp_ui_cb *ui_cb; |
| 730 | u8 dsap, ssap; |
| 731 | |
| 732 | dsap = nfc_llcp_dsap(skb); |
| 733 | ssap = nfc_llcp_ssap(skb); |
| 734 | |
| 735 | ui_cb = nfc_llcp_ui_skb_cb(skb); |
| 736 | ui_cb->dsap = dsap; |
| 737 | ui_cb->ssap = ssap; |
| 738 | |
| 739 | printk("%s %d %d\n", __func__, dsap, ssap); |
| 740 | |
| 741 | pr_debug("%d %d\n", dsap, ssap); |
| 742 | |
| 743 | /* We're looking for a bound socket, not a client one */ |
| 744 | llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP); |
| 745 | if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM) |
| 746 | return; |
| 747 | |
| 748 | /* There is no sequence with UI frames */ |
| 749 | skb_pull(skb, LLCP_HEADER_SIZE); |
| 750 | if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) { |
| 751 | pr_err("receive queue is full\n"); |
| 752 | skb_queue_head(&llcp_sock->tx_backlog_queue, skb); |
| 753 | } |
| 754 | |
| 755 | nfc_llcp_sock_put(llcp_sock); |
| 756 | } |
| 757 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 758 | static void nfc_llcp_recv_connect(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 759 | struct sk_buff *skb) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 760 | { |
| 761 | struct sock *new_sk, *parent; |
| 762 | struct nfc_llcp_sock *sock, *new_sock; |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 763 | u8 dsap, ssap, reason; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 764 | |
| 765 | dsap = nfc_llcp_dsap(skb); |
| 766 | ssap = nfc_llcp_ssap(skb); |
| 767 | |
| 768 | pr_debug("%d %d\n", dsap, ssap); |
| 769 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 770 | if (dsap != LLCP_SAP_SDP) { |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 771 | sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP); |
| 772 | if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 773 | reason = LLCP_DM_NOBOUND; |
| 774 | goto fail; |
| 775 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 776 | } else { |
| 777 | u8 *sn; |
| 778 | size_t sn_len; |
| 779 | |
| 780 | sn = nfc_llcp_connect_sn(skb, &sn_len); |
| 781 | if (sn == NULL) { |
| 782 | reason = LLCP_DM_NOBOUND; |
| 783 | goto fail; |
| 784 | } |
| 785 | |
| 786 | pr_debug("Service name length %zu\n", sn_len); |
| 787 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 788 | sock = nfc_llcp_sock_get_sn(local, sn, sn_len); |
| 789 | if (sock == NULL) { |
| 790 | reason = LLCP_DM_NOBOUND; |
| 791 | goto fail; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 792 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 793 | } |
| 794 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 795 | lock_sock(&sock->sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 796 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 797 | parent = &sock->sk; |
| 798 | |
| 799 | if (sk_acceptq_is_full(parent)) { |
| 800 | reason = LLCP_DM_REJ; |
| 801 | release_sock(&sock->sk); |
| 802 | sock_put(&sock->sk); |
| 803 | goto fail; |
| 804 | } |
| 805 | |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 806 | if (sock->ssap == LLCP_SDP_UNBOUND) { |
| 807 | u8 ssap = nfc_llcp_reserve_sdp_ssap(local); |
| 808 | |
| 809 | pr_debug("First client, reserving %d\n", ssap); |
| 810 | |
| 811 | if (ssap == LLCP_SAP_MAX) { |
| 812 | reason = LLCP_DM_REJ; |
| 813 | release_sock(&sock->sk); |
| 814 | sock_put(&sock->sk); |
| 815 | goto fail; |
| 816 | } |
| 817 | |
| 818 | sock->ssap = ssap; |
| 819 | } |
| 820 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 821 | new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 822 | if (new_sk == NULL) { |
| 823 | reason = LLCP_DM_REJ; |
| 824 | release_sock(&sock->sk); |
| 825 | sock_put(&sock->sk); |
| 826 | goto fail; |
| 827 | } |
| 828 | |
| 829 | new_sock = nfc_llcp_sock(new_sk); |
| 830 | new_sock->dev = local->dev; |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 831 | new_sock->local = nfc_llcp_local_get(local); |
Samuel Ortiz | 93d7e49 | 2012-05-14 17:37:32 +0200 | [diff] [blame] | 832 | new_sock->miu = local->remote_miu; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 833 | new_sock->nfc_protocol = sock->nfc_protocol; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 834 | new_sock->dsap = ssap; |
Samuel Ortiz | 025f152 | 2012-06-18 21:38:09 +0200 | [diff] [blame] | 835 | new_sock->target_idx = local->target_idx; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 836 | new_sock->parent = parent; |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 837 | new_sock->ssap = sock->ssap; |
| 838 | if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) { |
| 839 | atomic_t *client_count; |
| 840 | |
| 841 | pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock); |
| 842 | |
| 843 | client_count = |
| 844 | &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP]; |
| 845 | |
| 846 | atomic_inc(client_count); |
| 847 | new_sock->reserved_ssap = sock->ssap; |
| 848 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 849 | |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 850 | nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE], |
| 851 | skb->len - LLCP_HEADER_SIZE); |
| 852 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 853 | pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk); |
| 854 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 855 | nfc_llcp_sock_link(&local->sockets, new_sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 856 | |
| 857 | nfc_llcp_accept_enqueue(&sock->sk, new_sk); |
| 858 | |
| 859 | nfc_get_device(local->dev->idx); |
| 860 | |
| 861 | new_sk->sk_state = LLCP_CONNECTED; |
| 862 | |
| 863 | /* Wake the listening processes */ |
| 864 | parent->sk_data_ready(parent, 0); |
| 865 | |
| 866 | /* Send CC */ |
| 867 | nfc_llcp_send_cc(new_sock); |
| 868 | |
| 869 | release_sock(&sock->sk); |
| 870 | sock_put(&sock->sk); |
| 871 | |
| 872 | return; |
| 873 | |
| 874 | fail: |
| 875 | /* Send DM */ |
| 876 | nfc_llcp_send_dm(local, dsap, ssap, reason); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 877 | } |
| 878 | |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 879 | int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock) |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 880 | { |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 881 | int nr_frames = 0; |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 882 | struct nfc_llcp_local *local = sock->local; |
| 883 | |
| 884 | pr_debug("Remote ready %d tx queue len %d remote rw %d", |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 885 | sock->remote_ready, skb_queue_len(&sock->tx_pending_queue), |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 886 | sock->rw); |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 887 | |
| 888 | /* Try to queue some I frames for transmission */ |
| 889 | while (sock->remote_ready && |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 890 | skb_queue_len(&sock->tx_pending_queue) < sock->rw) { |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 891 | struct sk_buff *pdu; |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 892 | |
| 893 | pdu = skb_dequeue(&sock->tx_queue); |
| 894 | if (pdu == NULL) |
| 895 | break; |
| 896 | |
| 897 | /* Update N(S)/N(R) */ |
| 898 | nfc_llcp_set_nrns(sock, pdu); |
| 899 | |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 900 | skb_queue_tail(&local->tx_queue, pdu); |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 901 | nr_frames++; |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 902 | } |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 903 | |
| 904 | return nr_frames; |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 905 | } |
| 906 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 907 | static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 908 | struct sk_buff *skb) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 909 | { |
| 910 | struct nfc_llcp_sock *llcp_sock; |
| 911 | struct sock *sk; |
| 912 | u8 dsap, ssap, ptype, ns, nr; |
| 913 | |
| 914 | ptype = nfc_llcp_ptype(skb); |
| 915 | dsap = nfc_llcp_dsap(skb); |
| 916 | ssap = nfc_llcp_ssap(skb); |
| 917 | ns = nfc_llcp_ns(skb); |
| 918 | nr = nfc_llcp_nr(skb); |
| 919 | |
| 920 | pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns); |
| 921 | |
| 922 | llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); |
| 923 | if (llcp_sock == NULL) { |
| 924 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); |
| 925 | return; |
| 926 | } |
| 927 | |
| 928 | sk = &llcp_sock->sk; |
| 929 | lock_sock(sk); |
| 930 | if (sk->sk_state == LLCP_CLOSED) { |
| 931 | release_sock(sk); |
| 932 | nfc_llcp_sock_put(llcp_sock); |
| 933 | } |
| 934 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 935 | /* Pass the payload upstream */ |
| 936 | if (ptype == LLCP_PDU_I) { |
| 937 | pr_debug("I frame, queueing on %p\n", &llcp_sock->sk); |
| 938 | |
Samuel Ortiz | 53aef92 | 2012-03-05 01:03:36 +0100 | [diff] [blame] | 939 | if (ns == llcp_sock->recv_n) |
| 940 | llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16; |
| 941 | else |
| 942 | pr_err("Received out of sequence I PDU\n"); |
| 943 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 944 | skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE); |
| 945 | if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) { |
| 946 | pr_err("receive queue is full\n"); |
| 947 | skb_queue_head(&llcp_sock->tx_backlog_queue, skb); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | /* Remove skbs from the pending queue */ |
| 952 | if (llcp_sock->send_ack_n != nr) { |
| 953 | struct sk_buff *s, *tmp; |
| 954 | |
| 955 | llcp_sock->send_ack_n = nr; |
| 956 | |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 957 | /* Remove and free all skbs until ns == nr */ |
| 958 | skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) { |
| 959 | skb_unlink(s, &llcp_sock->tx_pending_queue); |
| 960 | kfree_skb(s); |
| 961 | |
| 962 | if (nfc_llcp_ns(s) == nr) |
| 963 | break; |
| 964 | } |
| 965 | |
| 966 | /* Re-queue the remaining skbs for transmission */ |
| 967 | skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue, |
| 968 | s, tmp) { |
| 969 | skb_unlink(s, &llcp_sock->tx_pending_queue); |
| 970 | skb_queue_head(&local->tx_queue, s); |
| 971 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 972 | } |
| 973 | |
Samuel Ortiz | 53aef92 | 2012-03-05 01:03:36 +0100 | [diff] [blame] | 974 | if (ptype == LLCP_PDU_RR) |
| 975 | llcp_sock->remote_ready = true; |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 976 | else if (ptype == LLCP_PDU_RNR) |
Samuel Ortiz | 53aef92 | 2012-03-05 01:03:36 +0100 | [diff] [blame] | 977 | llcp_sock->remote_ready = false; |
| 978 | |
Samuel Ortiz | 56af256 | 2012-05-30 18:06:11 +0200 | [diff] [blame] | 979 | if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I) |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 980 | nfc_llcp_send_rr(llcp_sock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 981 | |
| 982 | release_sock(sk); |
| 983 | nfc_llcp_sock_put(llcp_sock); |
| 984 | } |
| 985 | |
| 986 | static void nfc_llcp_recv_disc(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 987 | struct sk_buff *skb) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 988 | { |
| 989 | struct nfc_llcp_sock *llcp_sock; |
| 990 | struct sock *sk; |
| 991 | u8 dsap, ssap; |
| 992 | |
| 993 | dsap = nfc_llcp_dsap(skb); |
| 994 | ssap = nfc_llcp_ssap(skb); |
| 995 | |
| 996 | llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); |
| 997 | if (llcp_sock == NULL) { |
| 998 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | sk = &llcp_sock->sk; |
| 1003 | lock_sock(sk); |
| 1004 | if (sk->sk_state == LLCP_CLOSED) { |
| 1005 | release_sock(sk); |
| 1006 | nfc_llcp_sock_put(llcp_sock); |
| 1007 | } |
| 1008 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1009 | if (sk->sk_state == LLCP_CONNECTED) { |
| 1010 | nfc_put_device(local->dev); |
| 1011 | sk->sk_state = LLCP_CLOSED; |
| 1012 | sk->sk_state_change(sk); |
| 1013 | } |
| 1014 | |
| 1015 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC); |
| 1016 | |
| 1017 | release_sock(sk); |
| 1018 | nfc_llcp_sock_put(llcp_sock); |
| 1019 | } |
| 1020 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1021 | static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1022 | { |
| 1023 | struct nfc_llcp_sock *llcp_sock; |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 1024 | struct sock *sk; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1025 | u8 dsap, ssap; |
| 1026 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1027 | dsap = nfc_llcp_dsap(skb); |
| 1028 | ssap = nfc_llcp_ssap(skb); |
| 1029 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 1030 | llcp_sock = nfc_llcp_connecting_sock_get(local, dsap); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1031 | if (llcp_sock == NULL) { |
| 1032 | pr_err("Invalid CC\n"); |
| 1033 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); |
| 1034 | |
| 1035 | return; |
| 1036 | } |
| 1037 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 1038 | sk = &llcp_sock->sk; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1039 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 1040 | /* Unlink from connecting and link to the client array */ |
| 1041 | nfc_llcp_sock_unlink(&local->connecting_sockets, sk); |
| 1042 | nfc_llcp_sock_link(&local->sockets, sk); |
| 1043 | llcp_sock->dsap = ssap; |
| 1044 | |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 1045 | nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE], |
| 1046 | skb->len - LLCP_HEADER_SIZE); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1047 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 1048 | sk->sk_state = LLCP_CONNECTED; |
| 1049 | sk->sk_state_change(sk); |
| 1050 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1051 | nfc_llcp_sock_put(llcp_sock); |
| 1052 | } |
| 1053 | |
Samuel Ortiz | 5c0560b | 2012-06-26 16:13:29 +0200 | [diff] [blame] | 1054 | static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb) |
| 1055 | { |
| 1056 | struct nfc_llcp_sock *llcp_sock; |
| 1057 | struct sock *sk; |
| 1058 | u8 dsap, ssap, reason; |
| 1059 | |
| 1060 | dsap = nfc_llcp_dsap(skb); |
| 1061 | ssap = nfc_llcp_ssap(skb); |
| 1062 | reason = skb->data[2]; |
| 1063 | |
| 1064 | pr_debug("%d %d reason %d\n", ssap, dsap, reason); |
| 1065 | |
| 1066 | switch (reason) { |
| 1067 | case LLCP_DM_NOBOUND: |
| 1068 | case LLCP_DM_REJ: |
| 1069 | llcp_sock = nfc_llcp_connecting_sock_get(local, dsap); |
| 1070 | break; |
| 1071 | |
| 1072 | default: |
| 1073 | llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); |
| 1074 | break; |
| 1075 | } |
| 1076 | |
| 1077 | if (llcp_sock == NULL) { |
Samuel Ortiz | a8df0f3 | 2012-10-16 21:15:59 +0200 | [diff] [blame^] | 1078 | pr_debug("Already closed\n"); |
Samuel Ortiz | 5c0560b | 2012-06-26 16:13:29 +0200 | [diff] [blame] | 1079 | return; |
| 1080 | } |
| 1081 | |
| 1082 | sk = &llcp_sock->sk; |
| 1083 | |
| 1084 | sk->sk_err = ENXIO; |
| 1085 | sk->sk_state = LLCP_CLOSED; |
| 1086 | sk->sk_state_change(sk); |
| 1087 | |
| 1088 | nfc_llcp_sock_put(llcp_sock); |
Samuel Ortiz | 5c0560b | 2012-06-26 16:13:29 +0200 | [diff] [blame] | 1089 | } |
| 1090 | |
Samuel Ortiz | 19cfe58 | 2012-10-05 01:21:47 +0200 | [diff] [blame] | 1091 | static void nfc_llcp_recv_snl(struct nfc_llcp_local *local, |
| 1092 | struct sk_buff *skb) |
| 1093 | { |
| 1094 | struct nfc_llcp_sock *llcp_sock; |
| 1095 | u8 dsap, ssap, *tlv, type, length, tid, sap; |
| 1096 | u16 tlv_len, offset; |
| 1097 | char *service_name; |
| 1098 | size_t service_name_len; |
| 1099 | |
| 1100 | dsap = nfc_llcp_dsap(skb); |
| 1101 | ssap = nfc_llcp_ssap(skb); |
| 1102 | |
| 1103 | pr_debug("%d %d\n", dsap, ssap); |
| 1104 | |
| 1105 | if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) { |
| 1106 | pr_err("Wrong SNL SAP\n"); |
| 1107 | return; |
| 1108 | } |
| 1109 | |
| 1110 | tlv = &skb->data[LLCP_HEADER_SIZE]; |
| 1111 | tlv_len = skb->len - LLCP_HEADER_SIZE; |
| 1112 | offset = 0; |
| 1113 | |
| 1114 | while(offset < tlv_len) { |
| 1115 | type = tlv[0]; |
| 1116 | length = tlv[1]; |
| 1117 | |
| 1118 | switch (type) { |
| 1119 | case LLCP_TLV_SDREQ: |
| 1120 | tid = tlv[2]; |
| 1121 | service_name = (char *) &tlv[3]; |
| 1122 | service_name_len = length - 1; |
| 1123 | |
Samuel Ortiz | e690408 | 2012-10-15 14:28:13 +0200 | [diff] [blame] | 1124 | pr_debug("Looking for %.16s\n", service_name); |
Samuel Ortiz | 19cfe58 | 2012-10-05 01:21:47 +0200 | [diff] [blame] | 1125 | |
| 1126 | if (service_name_len == strlen("urn:nfc:sn:sdp") && |
| 1127 | !strncmp(service_name, "urn:nfc:sn:sdp", |
| 1128 | service_name_len)) { |
| 1129 | sap = 1; |
Samuel Ortiz | e690408 | 2012-10-15 14:28:13 +0200 | [diff] [blame] | 1130 | goto send_snl; |
Samuel Ortiz | 19cfe58 | 2012-10-05 01:21:47 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Samuel Ortiz | e690408 | 2012-10-15 14:28:13 +0200 | [diff] [blame] | 1133 | llcp_sock = nfc_llcp_sock_from_sn(local, service_name, |
| 1134 | service_name_len); |
| 1135 | if (!llcp_sock) { |
| 1136 | sap = 0; |
| 1137 | goto send_snl; |
| 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * We found a socket but its ssap has not been reserved |
| 1142 | * yet. We need to assign it for good and send a reply. |
| 1143 | * The ssap will be freed when the socket is closed. |
| 1144 | */ |
| 1145 | if (llcp_sock->ssap == LLCP_SDP_UNBOUND) { |
| 1146 | atomic_t *client_count; |
| 1147 | |
| 1148 | sap = nfc_llcp_reserve_sdp_ssap(local); |
| 1149 | |
| 1150 | pr_debug("Reserving %d\n", sap); |
| 1151 | |
| 1152 | if (sap == LLCP_SAP_MAX) { |
| 1153 | sap = 0; |
| 1154 | goto send_snl; |
| 1155 | } |
| 1156 | |
| 1157 | client_count = |
| 1158 | &local->local_sdp_cnt[sap - |
| 1159 | LLCP_WKS_NUM_SAP]; |
| 1160 | |
| 1161 | atomic_inc(client_count); |
| 1162 | |
| 1163 | llcp_sock->ssap = sap; |
| 1164 | llcp_sock->reserved_ssap = sap; |
| 1165 | } else { |
| 1166 | sap = llcp_sock->ssap; |
| 1167 | } |
| 1168 | |
| 1169 | pr_debug("%p %d\n", llcp_sock, sap); |
| 1170 | |
| 1171 | send_snl: |
Samuel Ortiz | 19cfe58 | 2012-10-05 01:21:47 +0200 | [diff] [blame] | 1172 | nfc_llcp_send_snl(local, tid, sap); |
| 1173 | break; |
| 1174 | |
| 1175 | default: |
| 1176 | pr_err("Invalid SNL tlv value 0x%x\n", type); |
| 1177 | break; |
| 1178 | } |
| 1179 | |
| 1180 | offset += length + 2; |
| 1181 | tlv += length + 2; |
| 1182 | } |
| 1183 | } |
| 1184 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1185 | static void nfc_llcp_rx_work(struct work_struct *work) |
| 1186 | { |
| 1187 | struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1188 | rx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1189 | u8 dsap, ssap, ptype; |
| 1190 | struct sk_buff *skb; |
| 1191 | |
| 1192 | skb = local->rx_pending; |
| 1193 | if (skb == NULL) { |
| 1194 | pr_debug("No pending SKB\n"); |
| 1195 | return; |
| 1196 | } |
| 1197 | |
| 1198 | ptype = nfc_llcp_ptype(skb); |
| 1199 | dsap = nfc_llcp_dsap(skb); |
| 1200 | ssap = nfc_llcp_ssap(skb); |
| 1201 | |
| 1202 | pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap); |
| 1203 | |
Samuel Ortiz | 4be646e | 2012-04-10 19:43:13 +0200 | [diff] [blame] | 1204 | if (ptype != LLCP_PDU_SYMM) |
| 1205 | print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET, |
| 1206 | 16, 1, skb->data, skb->len, true); |
| 1207 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 1208 | nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX); |
| 1209 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1210 | switch (ptype) { |
| 1211 | case LLCP_PDU_SYMM: |
| 1212 | pr_debug("SYMM\n"); |
| 1213 | break; |
| 1214 | |
Samuel Ortiz | 968272b | 2012-10-15 16:14:37 +0200 | [diff] [blame] | 1215 | case LLCP_PDU_UI: |
| 1216 | pr_debug("UI\n"); |
| 1217 | nfc_llcp_recv_ui(local, skb); |
| 1218 | break; |
| 1219 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1220 | case LLCP_PDU_CONNECT: |
| 1221 | pr_debug("CONNECT\n"); |
| 1222 | nfc_llcp_recv_connect(local, skb); |
| 1223 | break; |
| 1224 | |
| 1225 | case LLCP_PDU_DISC: |
| 1226 | pr_debug("DISC\n"); |
| 1227 | nfc_llcp_recv_disc(local, skb); |
| 1228 | break; |
| 1229 | |
| 1230 | case LLCP_PDU_CC: |
| 1231 | pr_debug("CC\n"); |
| 1232 | nfc_llcp_recv_cc(local, skb); |
| 1233 | break; |
| 1234 | |
Samuel Ortiz | 5c0560b | 2012-06-26 16:13:29 +0200 | [diff] [blame] | 1235 | case LLCP_PDU_DM: |
| 1236 | pr_debug("DM\n"); |
| 1237 | nfc_llcp_recv_dm(local, skb); |
| 1238 | break; |
| 1239 | |
Samuel Ortiz | 19cfe58 | 2012-10-05 01:21:47 +0200 | [diff] [blame] | 1240 | case LLCP_PDU_SNL: |
| 1241 | pr_debug("SNL\n"); |
| 1242 | nfc_llcp_recv_snl(local, skb); |
| 1243 | break; |
| 1244 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1245 | case LLCP_PDU_I: |
| 1246 | case LLCP_PDU_RR: |
Samuel Ortiz | 53aef92 | 2012-03-05 01:03:36 +0100 | [diff] [blame] | 1247 | case LLCP_PDU_RNR: |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1248 | pr_debug("I frame\n"); |
| 1249 | nfc_llcp_recv_hdlc(local, skb); |
| 1250 | break; |
| 1251 | |
| 1252 | } |
| 1253 | |
Linus Torvalds | 916082b | 2012-10-02 16:01:31 -0700 | [diff] [blame] | 1254 | schedule_work(&local->tx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1255 | kfree_skb(local->rx_pending); |
| 1256 | local->rx_pending = NULL; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | void nfc_llcp_recv(void *data, struct sk_buff *skb, int err) |
| 1260 | { |
| 1261 | struct nfc_llcp_local *local = (struct nfc_llcp_local *) data; |
| 1262 | |
| 1263 | pr_debug("Received an LLCP PDU\n"); |
| 1264 | if (err < 0) { |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1265 | pr_err("err %d\n", err); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1266 | return; |
| 1267 | } |
| 1268 | |
| 1269 | local->rx_pending = skb_get(skb); |
| 1270 | del_timer(&local->link_timer); |
Linus Torvalds | 916082b | 2012-10-02 16:01:31 -0700 | [diff] [blame] | 1271 | schedule_work(&local->rx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1272 | } |
| 1273 | |
Samuel Ortiz | 73167ce | 2012-05-31 00:05:50 +0200 | [diff] [blame] | 1274 | int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb) |
| 1275 | { |
| 1276 | struct nfc_llcp_local *local; |
| 1277 | |
| 1278 | local = nfc_llcp_find_local(dev); |
| 1279 | if (local == NULL) |
| 1280 | return -ENODEV; |
| 1281 | |
| 1282 | local->rx_pending = skb_get(skb); |
| 1283 | del_timer(&local->link_timer); |
Linus Torvalds | 916082b | 2012-10-02 16:01:31 -0700 | [diff] [blame] | 1284 | schedule_work(&local->rx_work); |
Samuel Ortiz | 73167ce | 2012-05-31 00:05:50 +0200 | [diff] [blame] | 1285 | |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1289 | void nfc_llcp_mac_is_down(struct nfc_dev *dev) |
| 1290 | { |
| 1291 | struct nfc_llcp_local *local; |
| 1292 | |
| 1293 | local = nfc_llcp_find_local(dev); |
| 1294 | if (local == NULL) |
| 1295 | return; |
| 1296 | |
| 1297 | /* Close and purge all existing sockets */ |
Samuel Ortiz | 4d22ea1 | 2012-06-22 14:40:34 +0200 | [diff] [blame] | 1298 | nfc_llcp_socket_release(local, true); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx, |
| 1302 | u8 comm_mode, u8 rf_mode) |
| 1303 | { |
| 1304 | struct nfc_llcp_local *local; |
| 1305 | |
| 1306 | pr_debug("rf mode %d\n", rf_mode); |
| 1307 | |
| 1308 | local = nfc_llcp_find_local(dev); |
| 1309 | if (local == NULL) |
| 1310 | return; |
| 1311 | |
| 1312 | local->target_idx = target_idx; |
| 1313 | local->comm_mode = comm_mode; |
| 1314 | local->rf_mode = rf_mode; |
| 1315 | |
| 1316 | if (rf_mode == NFC_RF_INITIATOR) { |
| 1317 | pr_debug("Queueing Tx work\n"); |
| 1318 | |
Linus Torvalds | 916082b | 2012-10-02 16:01:31 -0700 | [diff] [blame] | 1319 | schedule_work(&local->tx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1320 | } else { |
| 1321 | mod_timer(&local->link_timer, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1322 | jiffies + msecs_to_jiffies(local->remote_lto)); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | int nfc_llcp_register_device(struct nfc_dev *ndev) |
| 1327 | { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1328 | struct nfc_llcp_local *local; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1329 | |
| 1330 | local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL); |
| 1331 | if (local == NULL) |
| 1332 | return -ENOMEM; |
| 1333 | |
| 1334 | local->dev = ndev; |
| 1335 | INIT_LIST_HEAD(&local->list); |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 1336 | kref_init(&local->ref); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1337 | mutex_init(&local->sdp_lock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1338 | init_timer(&local->link_timer); |
| 1339 | local->link_timer.data = (unsigned long) local; |
| 1340 | local->link_timer.function = nfc_llcp_symm_timer; |
| 1341 | |
| 1342 | skb_queue_head_init(&local->tx_queue); |
| 1343 | INIT_WORK(&local->tx_work, nfc_llcp_tx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1344 | |
| 1345 | local->rx_pending = NULL; |
| 1346 | INIT_WORK(&local->rx_work, nfc_llcp_rx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1347 | |
| 1348 | INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1349 | |
Szymon Janc | fe235b5 | 2012-09-25 12:42:50 +0200 | [diff] [blame] | 1350 | rwlock_init(&local->sockets.lock); |
| 1351 | rwlock_init(&local->connecting_sockets.lock); |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 1352 | rwlock_init(&local->raw_sockets.lock); |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 1353 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1354 | nfc_llcp_build_gb(local); |
| 1355 | |
| 1356 | local->remote_miu = LLCP_DEFAULT_MIU; |
| 1357 | local->remote_lto = LLCP_DEFAULT_LTO; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1358 | |
| 1359 | list_add(&llcp_devices, &local->list); |
| 1360 | |
| 1361 | return 0; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | void nfc_llcp_unregister_device(struct nfc_dev *dev) |
| 1365 | { |
| 1366 | struct nfc_llcp_local *local = nfc_llcp_find_local(dev); |
| 1367 | |
| 1368 | if (local == NULL) { |
| 1369 | pr_debug("No such device\n"); |
| 1370 | return; |
| 1371 | } |
| 1372 | |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 1373 | nfc_llcp_local_put(local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | int __init nfc_llcp_init(void) |
| 1377 | { |
| 1378 | INIT_LIST_HEAD(&llcp_devices); |
| 1379 | |
| 1380 | return nfc_llcp_sock_init(); |
| 1381 | } |
| 1382 | |
| 1383 | void nfc_llcp_exit(void) |
| 1384 | { |
| 1385 | nfc_llcp_sock_exit(); |
| 1386 | } |