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