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 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 59 | 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; |
| 71 | lock_sock(accept_sk); |
| 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 | |
| 77 | release_sock(accept_sk); |
| 78 | |
| 79 | sock_orphan(accept_sk); |
| 80 | } |
Samuel Ortiz | 4d22ea1 | 2012-06-22 14:40:34 +0200 | [diff] [blame] | 81 | |
| 82 | if (listen == true) { |
| 83 | release_sock(sk); |
| 84 | continue; |
| 85 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 88 | sk->sk_state = LLCP_CLOSED; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 89 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 90 | release_sock(sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 91 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 92 | sock_orphan(sk); |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 93 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 94 | sk_del_node_init(sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 97 | write_unlock(&local->sockets.lock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 100 | struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local) |
| 101 | { |
| 102 | kref_get(&local->ref); |
| 103 | |
| 104 | return local; |
| 105 | } |
| 106 | |
| 107 | static void local_release(struct kref *ref) |
| 108 | { |
| 109 | struct nfc_llcp_local *local; |
| 110 | |
| 111 | local = container_of(ref, struct nfc_llcp_local, ref); |
| 112 | |
| 113 | list_del(&local->list); |
Samuel Ortiz | 4d22ea1 | 2012-06-22 14:40:34 +0200 | [diff] [blame] | 114 | nfc_llcp_socket_release(local, false); |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 115 | del_timer_sync(&local->link_timer); |
| 116 | skb_queue_purge(&local->tx_queue); |
| 117 | destroy_workqueue(local->tx_wq); |
| 118 | destroy_workqueue(local->rx_wq); |
Samuel Ortiz | 07922bb | 2012-05-31 17:29:15 +0200 | [diff] [blame] | 119 | destroy_workqueue(local->timeout_wq); |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 120 | kfree_skb(local->rx_pending); |
| 121 | kfree(local); |
| 122 | } |
| 123 | |
| 124 | int nfc_llcp_local_put(struct nfc_llcp_local *local) |
| 125 | { |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 126 | WARN_ON(local == NULL); |
| 127 | |
| 128 | if (local == NULL) |
| 129 | return 0; |
| 130 | |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 131 | return kref_put(&local->ref, local_release); |
| 132 | } |
| 133 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 134 | static void nfc_llcp_timeout_work(struct work_struct *work) |
| 135 | { |
| 136 | struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 137 | timeout_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 138 | |
| 139 | nfc_dep_link_down(local->dev); |
| 140 | } |
| 141 | |
| 142 | static void nfc_llcp_symm_timer(unsigned long data) |
| 143 | { |
| 144 | struct nfc_llcp_local *local = (struct nfc_llcp_local *) data; |
| 145 | |
| 146 | pr_err("SYMM timeout\n"); |
| 147 | |
| 148 | queue_work(local->timeout_wq, &local->timeout_work); |
| 149 | } |
| 150 | |
| 151 | struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev) |
| 152 | { |
| 153 | struct nfc_llcp_local *local, *n; |
| 154 | |
| 155 | list_for_each_entry_safe(local, n, &llcp_devices, list) |
| 156 | if (local->dev == dev) |
| 157 | return local; |
| 158 | |
| 159 | pr_debug("No device found\n"); |
| 160 | |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | static char *wks[] = { |
| 165 | NULL, |
| 166 | NULL, /* SDP */ |
| 167 | "urn:nfc:sn:ip", |
| 168 | "urn:nfc:sn:obex", |
| 169 | "urn:nfc:sn:snep", |
| 170 | }; |
| 171 | |
| 172 | static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len) |
| 173 | { |
| 174 | int sap, num_wks; |
| 175 | |
| 176 | pr_debug("%s\n", service_name); |
| 177 | |
| 178 | if (service_name == NULL) |
| 179 | return -EINVAL; |
| 180 | |
| 181 | num_wks = ARRAY_SIZE(wks); |
| 182 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 183 | for (sap = 0; sap < num_wks; sap++) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 184 | if (wks[sap] == NULL) |
| 185 | continue; |
| 186 | |
| 187 | if (strncmp(wks[sap], service_name, service_name_len) == 0) |
| 188 | return sap; |
| 189 | } |
| 190 | |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 195 | struct nfc_llcp_sock *sock) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 196 | { |
| 197 | mutex_lock(&local->sdp_lock); |
| 198 | |
| 199 | if (sock->service_name != NULL && sock->service_name_len > 0) { |
| 200 | int ssap = nfc_llcp_wks_sap(sock->service_name, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 201 | sock->service_name_len); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 202 | |
| 203 | if (ssap > 0) { |
| 204 | pr_debug("WKS %d\n", ssap); |
| 205 | |
| 206 | /* This is a WKS, let's check if it's free */ |
| 207 | if (local->local_wks & BIT(ssap)) { |
| 208 | mutex_unlock(&local->sdp_lock); |
| 209 | |
| 210 | return LLCP_SAP_MAX; |
| 211 | } |
| 212 | |
Samuel Ortiz | 1762c17 | 2012-03-05 01:03:38 +0100 | [diff] [blame] | 213 | set_bit(ssap, &local->local_wks); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 214 | mutex_unlock(&local->sdp_lock); |
| 215 | |
| 216 | return ssap; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * This is not a well known service, |
| 221 | * we should try to find a local SDP free spot |
| 222 | */ |
| 223 | ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP); |
| 224 | if (ssap == LLCP_SDP_NUM_SAP) { |
| 225 | mutex_unlock(&local->sdp_lock); |
| 226 | |
| 227 | return LLCP_SAP_MAX; |
| 228 | } |
| 229 | |
| 230 | pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap); |
| 231 | |
Samuel Ortiz | 1762c17 | 2012-03-05 01:03:38 +0100 | [diff] [blame] | 232 | set_bit(ssap, &local->local_sdp); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 233 | mutex_unlock(&local->sdp_lock); |
| 234 | |
| 235 | return LLCP_WKS_NUM_SAP + ssap; |
| 236 | |
Samuel Ortiz | ebbb16d | 2012-06-22 17:15:20 +0200 | [diff] [blame^] | 237 | } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) { |
| 238 | if (!test_bit(sock->ssap, &local->local_wks)) { |
| 239 | set_bit(sock->ssap, &local->local_wks); |
| 240 | mutex_unlock(&local->sdp_lock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 241 | |
Samuel Ortiz | ebbb16d | 2012-06-22 17:15:20 +0200 | [diff] [blame^] | 242 | return sock->ssap; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | mutex_unlock(&local->sdp_lock); |
| 247 | |
| 248 | return LLCP_SAP_MAX; |
| 249 | } |
| 250 | |
| 251 | u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local) |
| 252 | { |
| 253 | u8 local_ssap; |
| 254 | |
| 255 | mutex_lock(&local->sdp_lock); |
| 256 | |
| 257 | local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP); |
| 258 | if (local_ssap == LLCP_LOCAL_NUM_SAP) { |
| 259 | mutex_unlock(&local->sdp_lock); |
| 260 | return LLCP_SAP_MAX; |
| 261 | } |
| 262 | |
Samuel Ortiz | 1762c17 | 2012-03-05 01:03:38 +0100 | [diff] [blame] | 263 | set_bit(local_ssap, &local->local_sap); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 264 | |
| 265 | mutex_unlock(&local->sdp_lock); |
| 266 | |
| 267 | return local_ssap + LLCP_LOCAL_SAP_OFFSET; |
| 268 | } |
| 269 | |
| 270 | void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap) |
| 271 | { |
| 272 | u8 local_ssap; |
| 273 | unsigned long *sdp; |
| 274 | |
| 275 | if (ssap < LLCP_WKS_NUM_SAP) { |
| 276 | local_ssap = ssap; |
| 277 | sdp = &local->local_wks; |
| 278 | } else if (ssap < LLCP_LOCAL_NUM_SAP) { |
| 279 | local_ssap = ssap - LLCP_WKS_NUM_SAP; |
| 280 | sdp = &local->local_sdp; |
| 281 | } else if (ssap < LLCP_MAX_SAP) { |
| 282 | local_ssap = ssap - LLCP_LOCAL_NUM_SAP; |
| 283 | sdp = &local->local_sap; |
| 284 | } else { |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | mutex_lock(&local->sdp_lock); |
| 289 | |
Samuel Ortiz | 1762c17 | 2012-03-05 01:03:38 +0100 | [diff] [blame] | 290 | clear_bit(local_ssap, sdp); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 291 | |
| 292 | mutex_unlock(&local->sdp_lock); |
| 293 | } |
| 294 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 295 | static int nfc_llcp_build_gb(struct nfc_llcp_local *local) |
| 296 | { |
| 297 | u8 *gb_cur, *version_tlv, version, version_length; |
| 298 | u8 *lto_tlv, lto, lto_length; |
| 299 | u8 *wks_tlv, wks_length; |
Samuel Ortiz | 56d5876 | 2012-04-10 19:43:19 +0200 | [diff] [blame] | 300 | u8 *miux_tlv, miux_length; |
| 301 | __be16 miux; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 302 | u8 gb_len = 0; |
| 303 | |
| 304 | version = LLCP_VERSION_11; |
| 305 | version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 306 | 1, &version_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 307 | gb_len += version_length; |
| 308 | |
| 309 | /* 1500 ms */ |
| 310 | lto = 150; |
Samuel Ortiz | 91b0ade | 2012-04-10 19:43:20 +0200 | [diff] [blame] | 311 | 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] | 312 | gb_len += lto_length; |
| 313 | |
| 314 | pr_debug("Local wks 0x%lx\n", local->local_wks); |
| 315 | 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] | 316 | &wks_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 317 | gb_len += wks_length; |
| 318 | |
Samuel Ortiz | 56d5876 | 2012-04-10 19:43:19 +0200 | [diff] [blame] | 319 | miux = cpu_to_be16(LLCP_MAX_MIUX); |
| 320 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0, |
| 321 | &miux_length); |
| 322 | gb_len += miux_length; |
| 323 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 324 | gb_len += ARRAY_SIZE(llcp_magic); |
| 325 | |
| 326 | if (gb_len > NFC_MAX_GT_LEN) { |
| 327 | kfree(version_tlv); |
| 328 | return -EINVAL; |
| 329 | } |
| 330 | |
| 331 | gb_cur = local->gb; |
| 332 | |
| 333 | memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic)); |
| 334 | gb_cur += ARRAY_SIZE(llcp_magic); |
| 335 | |
| 336 | memcpy(gb_cur, version_tlv, version_length); |
| 337 | gb_cur += version_length; |
| 338 | |
| 339 | memcpy(gb_cur, lto_tlv, lto_length); |
| 340 | gb_cur += lto_length; |
| 341 | |
| 342 | memcpy(gb_cur, wks_tlv, wks_length); |
| 343 | gb_cur += wks_length; |
| 344 | |
Samuel Ortiz | 56d5876 | 2012-04-10 19:43:19 +0200 | [diff] [blame] | 345 | memcpy(gb_cur, miux_tlv, miux_length); |
| 346 | gb_cur += miux_length; |
| 347 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 348 | kfree(version_tlv); |
| 349 | kfree(lto_tlv); |
| 350 | |
| 351 | local->gb_len = gb_len; |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
Samuel Ortiz | b8e7a06 | 2012-06-22 02:04:53 +0200 | [diff] [blame] | 356 | u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len) |
| 357 | { |
| 358 | struct nfc_llcp_local *local; |
| 359 | |
| 360 | local = nfc_llcp_find_local(dev); |
| 361 | if (local == NULL) { |
| 362 | *general_bytes_len = 0; |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | nfc_llcp_build_gb(local); |
| 367 | |
| 368 | *general_bytes_len = local->gb_len; |
| 369 | |
| 370 | return local->gb; |
| 371 | } |
| 372 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 373 | int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len) |
| 374 | { |
| 375 | struct nfc_llcp_local *local = nfc_llcp_find_local(dev); |
| 376 | |
| 377 | if (local == NULL) { |
| 378 | pr_err("No LLCP device\n"); |
| 379 | return -ENODEV; |
| 380 | } |
| 381 | |
| 382 | memset(local->remote_gb, 0, NFC_MAX_GT_LEN); |
| 383 | memcpy(local->remote_gb, gb, gb_len); |
| 384 | local->remote_gb_len = gb_len; |
| 385 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 386 | if (local->remote_gb == NULL || local->remote_gb_len == 0) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 387 | return -ENODEV; |
| 388 | |
| 389 | if (memcmp(local->remote_gb, llcp_magic, 3)) { |
| 390 | pr_err("MAC does not support LLCP\n"); |
| 391 | return -EINVAL; |
| 392 | } |
| 393 | |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 394 | return nfc_llcp_parse_gb_tlv(local, |
| 395 | &local->remote_gb[3], |
| 396 | local->remote_gb_len - 3); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 397 | } |
| 398 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 399 | static u8 nfc_llcp_dsap(struct sk_buff *pdu) |
| 400 | { |
| 401 | return (pdu->data[0] & 0xfc) >> 2; |
| 402 | } |
| 403 | |
| 404 | static u8 nfc_llcp_ptype(struct sk_buff *pdu) |
| 405 | { |
| 406 | return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6); |
| 407 | } |
| 408 | |
| 409 | static u8 nfc_llcp_ssap(struct sk_buff *pdu) |
| 410 | { |
| 411 | return pdu->data[1] & 0x3f; |
| 412 | } |
| 413 | |
| 414 | static u8 nfc_llcp_ns(struct sk_buff *pdu) |
| 415 | { |
| 416 | return pdu->data[2] >> 4; |
| 417 | } |
| 418 | |
| 419 | static u8 nfc_llcp_nr(struct sk_buff *pdu) |
| 420 | { |
| 421 | return pdu->data[2] & 0xf; |
| 422 | } |
| 423 | |
| 424 | static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu) |
| 425 | { |
Samuel Ortiz | 279cf17 | 2012-04-10 19:43:14 +0200 | [diff] [blame] | 426 | pdu->data[2] = (sock->send_n << 4) | (sock->recv_n); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 427 | sock->send_n = (sock->send_n + 1) % 16; |
| 428 | sock->recv_ack_n = (sock->recv_n - 1) % 16; |
| 429 | } |
| 430 | |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 431 | static void nfc_llcp_tx_work(struct work_struct *work) |
| 432 | { |
| 433 | struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, |
| 434 | tx_work); |
| 435 | struct sk_buff *skb; |
| 436 | struct sock *sk; |
| 437 | struct nfc_llcp_sock *llcp_sock; |
| 438 | |
| 439 | skb = skb_dequeue(&local->tx_queue); |
| 440 | if (skb != NULL) { |
| 441 | sk = skb->sk; |
| 442 | llcp_sock = nfc_llcp_sock(sk); |
| 443 | if (llcp_sock != NULL) { |
| 444 | int ret; |
| 445 | |
| 446 | pr_debug("Sending pending skb\n"); |
| 447 | print_hex_dump(KERN_DEBUG, "LLCP Tx: ", |
| 448 | DUMP_PREFIX_OFFSET, 16, 1, |
| 449 | skb->data, skb->len, true); |
| 450 | |
| 451 | ret = nfc_data_exchange(local->dev, local->target_idx, |
| 452 | skb, nfc_llcp_recv, local); |
| 453 | |
| 454 | if (!ret && nfc_llcp_ptype(skb) == LLCP_PDU_I) { |
| 455 | skb = skb_get(skb); |
| 456 | skb_queue_tail(&llcp_sock->tx_pending_queue, |
| 457 | skb); |
| 458 | } |
| 459 | } else { |
| 460 | nfc_llcp_send_symm(local->dev); |
| 461 | } |
| 462 | } else { |
| 463 | nfc_llcp_send_symm(local->dev); |
| 464 | } |
| 465 | |
| 466 | mod_timer(&local->link_timer, |
| 467 | jiffies + msecs_to_jiffies(2 * local->remote_lto)); |
| 468 | } |
| 469 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 470 | static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local, |
| 471 | u8 ssap) |
| 472 | { |
| 473 | struct sock *sk; |
| 474 | struct nfc_llcp_sock *llcp_sock; |
| 475 | struct hlist_node *node; |
| 476 | |
| 477 | read_lock(&local->connecting_sockets.lock); |
| 478 | |
| 479 | sk_for_each(sk, node, &local->connecting_sockets.head) { |
| 480 | llcp_sock = nfc_llcp_sock(sk); |
| 481 | |
Samuel Ortiz | 5a0f6f3 | 2012-05-21 11:44:42 +0200 | [diff] [blame] | 482 | if (llcp_sock->ssap == ssap) { |
| 483 | sock_hold(&llcp_sock->sk); |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 484 | goto out; |
Samuel Ortiz | 5a0f6f3 | 2012-05-21 11:44:42 +0200 | [diff] [blame] | 485 | } |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | llcp_sock = NULL; |
| 489 | |
| 490 | out: |
| 491 | read_unlock(&local->connecting_sockets.lock); |
| 492 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 493 | return llcp_sock; |
| 494 | } |
| 495 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 496 | static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 497 | u8 ssap, u8 dsap) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 498 | { |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 499 | struct sock *sk; |
| 500 | struct hlist_node *node; |
| 501 | struct nfc_llcp_sock *llcp_sock; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 502 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 503 | pr_debug("ssap dsap %d %d\n", ssap, dsap); |
| 504 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 505 | if (ssap == 0 && dsap == 0) |
| 506 | return NULL; |
| 507 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 508 | read_lock(&local->sockets.lock); |
| 509 | |
| 510 | llcp_sock = NULL; |
| 511 | |
| 512 | sk_for_each(sk, node, &local->sockets.head) { |
| 513 | llcp_sock = nfc_llcp_sock(sk); |
| 514 | |
| 515 | if (llcp_sock->ssap == ssap && |
| 516 | llcp_sock->dsap == dsap) |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | read_unlock(&local->sockets.lock); |
| 521 | |
| 522 | if (llcp_sock == NULL) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 523 | return NULL; |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 524 | |
| 525 | sock_hold(&llcp_sock->sk); |
| 526 | |
| 527 | return llcp_sock; |
| 528 | } |
| 529 | |
| 530 | static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local, |
| 531 | u8 *sn, size_t sn_len) |
| 532 | { |
| 533 | struct sock *sk; |
| 534 | struct hlist_node *node; |
| 535 | struct nfc_llcp_sock *llcp_sock; |
| 536 | |
| 537 | pr_debug("sn %zd\n", sn_len); |
| 538 | |
| 539 | if (sn == NULL || sn_len == 0) |
| 540 | return NULL; |
| 541 | |
| 542 | read_lock(&local->sockets.lock); |
| 543 | |
| 544 | llcp_sock = NULL; |
| 545 | |
| 546 | sk_for_each(sk, node, &local->sockets.head) { |
| 547 | llcp_sock = nfc_llcp_sock(sk); |
| 548 | |
| 549 | if (llcp_sock->sk.sk_state != LLCP_LISTEN) |
| 550 | continue; |
| 551 | |
| 552 | if (llcp_sock->service_name == NULL || |
| 553 | llcp_sock->service_name_len == 0) |
| 554 | continue; |
| 555 | |
| 556 | if (llcp_sock->service_name_len != sn_len) |
| 557 | continue; |
| 558 | |
| 559 | if (memcmp(sn, llcp_sock->service_name, sn_len) == 0) |
| 560 | break; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 561 | } |
| 562 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 563 | read_unlock(&local->sockets.lock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 564 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 565 | if (llcp_sock == NULL) |
| 566 | return NULL; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 567 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 568 | sock_hold(&llcp_sock->sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 569 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 570 | return llcp_sock; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock) |
| 574 | { |
| 575 | sock_put(&sock->sk); |
| 576 | } |
| 577 | |
| 578 | static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len) |
| 579 | { |
| 580 | u8 *tlv = &skb->data[2], type, length; |
| 581 | size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0; |
| 582 | |
| 583 | while (offset < tlv_array_len) { |
| 584 | type = tlv[0]; |
| 585 | length = tlv[1]; |
| 586 | |
| 587 | pr_debug("type 0x%x length %d\n", type, length); |
| 588 | |
| 589 | if (type == LLCP_TLV_SN) { |
| 590 | *sn_len = length; |
| 591 | return &tlv[2]; |
| 592 | } |
| 593 | |
| 594 | offset += length + 2; |
| 595 | tlv += length + 2; |
| 596 | } |
| 597 | |
| 598 | return NULL; |
| 599 | } |
| 600 | |
| 601 | static void nfc_llcp_recv_connect(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 602 | struct sk_buff *skb) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 603 | { |
| 604 | struct sock *new_sk, *parent; |
| 605 | struct nfc_llcp_sock *sock, *new_sock; |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 606 | u8 dsap, ssap, reason; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 607 | |
| 608 | dsap = nfc_llcp_dsap(skb); |
| 609 | ssap = nfc_llcp_ssap(skb); |
| 610 | |
| 611 | pr_debug("%d %d\n", dsap, ssap); |
| 612 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 613 | if (dsap != LLCP_SAP_SDP) { |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 614 | sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP); |
| 615 | if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 616 | reason = LLCP_DM_NOBOUND; |
| 617 | goto fail; |
| 618 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 619 | } else { |
| 620 | u8 *sn; |
| 621 | size_t sn_len; |
| 622 | |
| 623 | sn = nfc_llcp_connect_sn(skb, &sn_len); |
| 624 | if (sn == NULL) { |
| 625 | reason = LLCP_DM_NOBOUND; |
| 626 | goto fail; |
| 627 | } |
| 628 | |
| 629 | pr_debug("Service name length %zu\n", sn_len); |
| 630 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 631 | sock = nfc_llcp_sock_get_sn(local, sn, sn_len); |
| 632 | if (sock == NULL) { |
| 633 | reason = LLCP_DM_NOBOUND; |
| 634 | goto fail; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 635 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 636 | } |
| 637 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 638 | lock_sock(&sock->sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 639 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 640 | parent = &sock->sk; |
| 641 | |
| 642 | if (sk_acceptq_is_full(parent)) { |
| 643 | reason = LLCP_DM_REJ; |
| 644 | release_sock(&sock->sk); |
| 645 | sock_put(&sock->sk); |
| 646 | goto fail; |
| 647 | } |
| 648 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 649 | new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 650 | if (new_sk == NULL) { |
| 651 | reason = LLCP_DM_REJ; |
| 652 | release_sock(&sock->sk); |
| 653 | sock_put(&sock->sk); |
| 654 | goto fail; |
| 655 | } |
| 656 | |
| 657 | new_sock = nfc_llcp_sock(new_sk); |
| 658 | new_sock->dev = local->dev; |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 659 | new_sock->local = nfc_llcp_local_get(local); |
Samuel Ortiz | 93d7e49 | 2012-05-14 17:37:32 +0200 | [diff] [blame] | 660 | new_sock->miu = local->remote_miu; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 661 | new_sock->nfc_protocol = sock->nfc_protocol; |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 662 | new_sock->ssap = sock->ssap; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 663 | new_sock->dsap = ssap; |
Samuel Ortiz | 025f152 | 2012-06-18 21:38:09 +0200 | [diff] [blame] | 664 | new_sock->target_idx = local->target_idx; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 665 | new_sock->parent = parent; |
| 666 | |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 667 | nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE], |
| 668 | skb->len - LLCP_HEADER_SIZE); |
| 669 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 670 | pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk); |
| 671 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 672 | nfc_llcp_sock_link(&local->sockets, new_sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 673 | |
| 674 | nfc_llcp_accept_enqueue(&sock->sk, new_sk); |
| 675 | |
| 676 | nfc_get_device(local->dev->idx); |
| 677 | |
| 678 | new_sk->sk_state = LLCP_CONNECTED; |
| 679 | |
| 680 | /* Wake the listening processes */ |
| 681 | parent->sk_data_ready(parent, 0); |
| 682 | |
| 683 | /* Send CC */ |
| 684 | nfc_llcp_send_cc(new_sock); |
| 685 | |
| 686 | release_sock(&sock->sk); |
| 687 | sock_put(&sock->sk); |
| 688 | |
| 689 | return; |
| 690 | |
| 691 | fail: |
| 692 | /* Send DM */ |
| 693 | nfc_llcp_send_dm(local, dsap, ssap, reason); |
| 694 | |
| 695 | return; |
| 696 | |
| 697 | } |
| 698 | |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 699 | int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock) |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 700 | { |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 701 | int nr_frames = 0; |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 702 | struct nfc_llcp_local *local = sock->local; |
| 703 | |
| 704 | pr_debug("Remote ready %d tx queue len %d remote rw %d", |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 705 | sock->remote_ready, skb_queue_len(&sock->tx_pending_queue), |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 706 | sock->rw); |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 707 | |
| 708 | /* Try to queue some I frames for transmission */ |
| 709 | while (sock->remote_ready && |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 710 | skb_queue_len(&sock->tx_pending_queue) < sock->rw) { |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 711 | struct sk_buff *pdu; |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 712 | |
| 713 | pdu = skb_dequeue(&sock->tx_queue); |
| 714 | if (pdu == NULL) |
| 715 | break; |
| 716 | |
| 717 | /* Update N(S)/N(R) */ |
| 718 | nfc_llcp_set_nrns(sock, pdu); |
| 719 | |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 720 | skb_queue_tail(&local->tx_queue, pdu); |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 721 | nr_frames++; |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 722 | } |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 723 | |
| 724 | return nr_frames; |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 725 | } |
| 726 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 727 | static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 728 | struct sk_buff *skb) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 729 | { |
| 730 | struct nfc_llcp_sock *llcp_sock; |
| 731 | struct sock *sk; |
| 732 | u8 dsap, ssap, ptype, ns, nr; |
| 733 | |
| 734 | ptype = nfc_llcp_ptype(skb); |
| 735 | dsap = nfc_llcp_dsap(skb); |
| 736 | ssap = nfc_llcp_ssap(skb); |
| 737 | ns = nfc_llcp_ns(skb); |
| 738 | nr = nfc_llcp_nr(skb); |
| 739 | |
| 740 | pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns); |
| 741 | |
| 742 | llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); |
| 743 | if (llcp_sock == NULL) { |
| 744 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | sk = &llcp_sock->sk; |
| 749 | lock_sock(sk); |
| 750 | if (sk->sk_state == LLCP_CLOSED) { |
| 751 | release_sock(sk); |
| 752 | nfc_llcp_sock_put(llcp_sock); |
| 753 | } |
| 754 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 755 | /* Pass the payload upstream */ |
| 756 | if (ptype == LLCP_PDU_I) { |
| 757 | pr_debug("I frame, queueing on %p\n", &llcp_sock->sk); |
| 758 | |
Samuel Ortiz | 53aef92 | 2012-03-05 01:03:36 +0100 | [diff] [blame] | 759 | if (ns == llcp_sock->recv_n) |
| 760 | llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16; |
| 761 | else |
| 762 | pr_err("Received out of sequence I PDU\n"); |
| 763 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 764 | skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE); |
| 765 | if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) { |
| 766 | pr_err("receive queue is full\n"); |
| 767 | skb_queue_head(&llcp_sock->tx_backlog_queue, skb); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /* Remove skbs from the pending queue */ |
| 772 | if (llcp_sock->send_ack_n != nr) { |
| 773 | struct sk_buff *s, *tmp; |
| 774 | |
| 775 | llcp_sock->send_ack_n = nr; |
| 776 | |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 777 | /* Remove and free all skbs until ns == nr */ |
| 778 | skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) { |
| 779 | skb_unlink(s, &llcp_sock->tx_pending_queue); |
| 780 | kfree_skb(s); |
| 781 | |
| 782 | if (nfc_llcp_ns(s) == nr) |
| 783 | break; |
| 784 | } |
| 785 | |
| 786 | /* Re-queue the remaining skbs for transmission */ |
| 787 | skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue, |
| 788 | s, tmp) { |
| 789 | skb_unlink(s, &llcp_sock->tx_pending_queue); |
| 790 | skb_queue_head(&local->tx_queue, s); |
| 791 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 792 | } |
| 793 | |
Samuel Ortiz | 53aef92 | 2012-03-05 01:03:36 +0100 | [diff] [blame] | 794 | if (ptype == LLCP_PDU_RR) |
| 795 | llcp_sock->remote_ready = true; |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 796 | else if (ptype == LLCP_PDU_RNR) |
Samuel Ortiz | 53aef92 | 2012-03-05 01:03:36 +0100 | [diff] [blame] | 797 | llcp_sock->remote_ready = false; |
| 798 | |
Samuel Ortiz | 56af256 | 2012-05-30 18:06:11 +0200 | [diff] [blame] | 799 | 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] | 800 | nfc_llcp_send_rr(llcp_sock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 801 | |
| 802 | release_sock(sk); |
| 803 | nfc_llcp_sock_put(llcp_sock); |
| 804 | } |
| 805 | |
| 806 | static void nfc_llcp_recv_disc(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 807 | struct sk_buff *skb) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 808 | { |
| 809 | struct nfc_llcp_sock *llcp_sock; |
| 810 | struct sock *sk; |
| 811 | u8 dsap, ssap; |
| 812 | |
| 813 | dsap = nfc_llcp_dsap(skb); |
| 814 | ssap = nfc_llcp_ssap(skb); |
| 815 | |
| 816 | llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); |
| 817 | if (llcp_sock == NULL) { |
| 818 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); |
| 819 | return; |
| 820 | } |
| 821 | |
| 822 | sk = &llcp_sock->sk; |
| 823 | lock_sock(sk); |
| 824 | if (sk->sk_state == LLCP_CLOSED) { |
| 825 | release_sock(sk); |
| 826 | nfc_llcp_sock_put(llcp_sock); |
| 827 | } |
| 828 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 829 | if (sk->sk_state == LLCP_CONNECTED) { |
| 830 | nfc_put_device(local->dev); |
| 831 | sk->sk_state = LLCP_CLOSED; |
| 832 | sk->sk_state_change(sk); |
| 833 | } |
| 834 | |
| 835 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC); |
| 836 | |
| 837 | release_sock(sk); |
| 838 | nfc_llcp_sock_put(llcp_sock); |
| 839 | } |
| 840 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 841 | 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] | 842 | { |
| 843 | struct nfc_llcp_sock *llcp_sock; |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 844 | struct sock *sk; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 845 | u8 dsap, ssap; |
| 846 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 847 | dsap = nfc_llcp_dsap(skb); |
| 848 | ssap = nfc_llcp_ssap(skb); |
| 849 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 850 | llcp_sock = nfc_llcp_connecting_sock_get(local, dsap); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 851 | if (llcp_sock == NULL) { |
| 852 | pr_err("Invalid CC\n"); |
| 853 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); |
| 854 | |
| 855 | return; |
| 856 | } |
| 857 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 858 | sk = &llcp_sock->sk; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 859 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 860 | /* Unlink from connecting and link to the client array */ |
| 861 | nfc_llcp_sock_unlink(&local->connecting_sockets, sk); |
| 862 | nfc_llcp_sock_link(&local->sockets, sk); |
| 863 | llcp_sock->dsap = ssap; |
| 864 | |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 865 | nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE], |
| 866 | skb->len - LLCP_HEADER_SIZE); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 867 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 868 | sk->sk_state = LLCP_CONNECTED; |
| 869 | sk->sk_state_change(sk); |
| 870 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 871 | nfc_llcp_sock_put(llcp_sock); |
| 872 | } |
| 873 | |
| 874 | static void nfc_llcp_rx_work(struct work_struct *work) |
| 875 | { |
| 876 | struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 877 | rx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 878 | u8 dsap, ssap, ptype; |
| 879 | struct sk_buff *skb; |
| 880 | |
| 881 | skb = local->rx_pending; |
| 882 | if (skb == NULL) { |
| 883 | pr_debug("No pending SKB\n"); |
| 884 | return; |
| 885 | } |
| 886 | |
| 887 | ptype = nfc_llcp_ptype(skb); |
| 888 | dsap = nfc_llcp_dsap(skb); |
| 889 | ssap = nfc_llcp_ssap(skb); |
| 890 | |
| 891 | pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap); |
| 892 | |
Samuel Ortiz | 4be646e | 2012-04-10 19:43:13 +0200 | [diff] [blame] | 893 | if (ptype != LLCP_PDU_SYMM) |
| 894 | print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET, |
| 895 | 16, 1, skb->data, skb->len, true); |
| 896 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 897 | switch (ptype) { |
| 898 | case LLCP_PDU_SYMM: |
| 899 | pr_debug("SYMM\n"); |
| 900 | break; |
| 901 | |
| 902 | case LLCP_PDU_CONNECT: |
| 903 | pr_debug("CONNECT\n"); |
| 904 | nfc_llcp_recv_connect(local, skb); |
| 905 | break; |
| 906 | |
| 907 | case LLCP_PDU_DISC: |
| 908 | pr_debug("DISC\n"); |
| 909 | nfc_llcp_recv_disc(local, skb); |
| 910 | break; |
| 911 | |
| 912 | case LLCP_PDU_CC: |
| 913 | pr_debug("CC\n"); |
| 914 | nfc_llcp_recv_cc(local, skb); |
| 915 | break; |
| 916 | |
| 917 | case LLCP_PDU_I: |
| 918 | case LLCP_PDU_RR: |
Samuel Ortiz | 53aef92 | 2012-03-05 01:03:36 +0100 | [diff] [blame] | 919 | case LLCP_PDU_RNR: |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 920 | pr_debug("I frame\n"); |
| 921 | nfc_llcp_recv_hdlc(local, skb); |
| 922 | break; |
| 923 | |
| 924 | } |
| 925 | |
| 926 | queue_work(local->tx_wq, &local->tx_work); |
| 927 | kfree_skb(local->rx_pending); |
| 928 | local->rx_pending = NULL; |
| 929 | |
| 930 | return; |
| 931 | } |
| 932 | |
| 933 | void nfc_llcp_recv(void *data, struct sk_buff *skb, int err) |
| 934 | { |
| 935 | struct nfc_llcp_local *local = (struct nfc_llcp_local *) data; |
| 936 | |
| 937 | pr_debug("Received an LLCP PDU\n"); |
| 938 | if (err < 0) { |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 939 | pr_err("err %d\n", err); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 940 | return; |
| 941 | } |
| 942 | |
| 943 | local->rx_pending = skb_get(skb); |
| 944 | del_timer(&local->link_timer); |
| 945 | queue_work(local->rx_wq, &local->rx_work); |
| 946 | |
| 947 | return; |
| 948 | } |
| 949 | |
Samuel Ortiz | 73167ce | 2012-05-31 00:05:50 +0200 | [diff] [blame] | 950 | int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb) |
| 951 | { |
| 952 | struct nfc_llcp_local *local; |
| 953 | |
| 954 | local = nfc_llcp_find_local(dev); |
| 955 | if (local == NULL) |
| 956 | return -ENODEV; |
| 957 | |
| 958 | local->rx_pending = skb_get(skb); |
| 959 | del_timer(&local->link_timer); |
| 960 | queue_work(local->rx_wq, &local->rx_work); |
| 961 | |
| 962 | return 0; |
| 963 | } |
| 964 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 965 | void nfc_llcp_mac_is_down(struct nfc_dev *dev) |
| 966 | { |
| 967 | struct nfc_llcp_local *local; |
| 968 | |
| 969 | local = nfc_llcp_find_local(dev); |
| 970 | if (local == NULL) |
| 971 | return; |
| 972 | |
| 973 | /* Close and purge all existing sockets */ |
Samuel Ortiz | 4d22ea1 | 2012-06-22 14:40:34 +0200 | [diff] [blame] | 974 | nfc_llcp_socket_release(local, true); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx, |
| 978 | u8 comm_mode, u8 rf_mode) |
| 979 | { |
| 980 | struct nfc_llcp_local *local; |
| 981 | |
| 982 | pr_debug("rf mode %d\n", rf_mode); |
| 983 | |
| 984 | local = nfc_llcp_find_local(dev); |
| 985 | if (local == NULL) |
| 986 | return; |
| 987 | |
| 988 | local->target_idx = target_idx; |
| 989 | local->comm_mode = comm_mode; |
| 990 | local->rf_mode = rf_mode; |
| 991 | |
| 992 | if (rf_mode == NFC_RF_INITIATOR) { |
| 993 | pr_debug("Queueing Tx work\n"); |
| 994 | |
| 995 | queue_work(local->tx_wq, &local->tx_work); |
| 996 | } else { |
| 997 | mod_timer(&local->link_timer, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 998 | jiffies + msecs_to_jiffies(local->remote_lto)); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | int nfc_llcp_register_device(struct nfc_dev *ndev) |
| 1003 | { |
| 1004 | struct device *dev = &ndev->dev; |
| 1005 | struct nfc_llcp_local *local; |
| 1006 | char name[32]; |
| 1007 | int err; |
| 1008 | |
| 1009 | local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL); |
| 1010 | if (local == NULL) |
| 1011 | return -ENOMEM; |
| 1012 | |
| 1013 | local->dev = ndev; |
| 1014 | INIT_LIST_HEAD(&local->list); |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 1015 | kref_init(&local->ref); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1016 | mutex_init(&local->sdp_lock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1017 | init_timer(&local->link_timer); |
| 1018 | local->link_timer.data = (unsigned long) local; |
| 1019 | local->link_timer.function = nfc_llcp_symm_timer; |
| 1020 | |
| 1021 | skb_queue_head_init(&local->tx_queue); |
| 1022 | INIT_WORK(&local->tx_work, nfc_llcp_tx_work); |
| 1023 | snprintf(name, sizeof(name), "%s_llcp_tx_wq", dev_name(dev)); |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1024 | local->tx_wq = |
| 1025 | alloc_workqueue(name, |
| 1026 | WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM, |
| 1027 | 1); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1028 | if (local->tx_wq == NULL) { |
| 1029 | err = -ENOMEM; |
| 1030 | goto err_local; |
| 1031 | } |
| 1032 | |
| 1033 | local->rx_pending = NULL; |
| 1034 | INIT_WORK(&local->rx_work, nfc_llcp_rx_work); |
| 1035 | snprintf(name, sizeof(name), "%s_llcp_rx_wq", dev_name(dev)); |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1036 | local->rx_wq = |
| 1037 | alloc_workqueue(name, |
| 1038 | WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM, |
| 1039 | 1); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1040 | if (local->rx_wq == NULL) { |
| 1041 | err = -ENOMEM; |
| 1042 | goto err_tx_wq; |
| 1043 | } |
| 1044 | |
| 1045 | INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work); |
| 1046 | snprintf(name, sizeof(name), "%s_llcp_timeout_wq", dev_name(dev)); |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1047 | local->timeout_wq = |
| 1048 | alloc_workqueue(name, |
| 1049 | WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM, |
| 1050 | 1); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1051 | if (local->timeout_wq == NULL) { |
| 1052 | err = -ENOMEM; |
| 1053 | goto err_rx_wq; |
| 1054 | } |
| 1055 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 1056 | local->sockets.lock = __RW_LOCK_UNLOCKED(local->sockets.lock); |
| 1057 | local->connecting_sockets.lock = __RW_LOCK_UNLOCKED(local->connecting_sockets.lock); |
| 1058 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1059 | nfc_llcp_build_gb(local); |
| 1060 | |
| 1061 | local->remote_miu = LLCP_DEFAULT_MIU; |
| 1062 | local->remote_lto = LLCP_DEFAULT_LTO; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1063 | |
| 1064 | list_add(&llcp_devices, &local->list); |
| 1065 | |
| 1066 | return 0; |
| 1067 | |
| 1068 | err_rx_wq: |
| 1069 | destroy_workqueue(local->rx_wq); |
| 1070 | |
| 1071 | err_tx_wq: |
| 1072 | destroy_workqueue(local->tx_wq); |
| 1073 | |
| 1074 | err_local: |
| 1075 | kfree(local); |
| 1076 | |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
| 1080 | void nfc_llcp_unregister_device(struct nfc_dev *dev) |
| 1081 | { |
| 1082 | struct nfc_llcp_local *local = nfc_llcp_find_local(dev); |
| 1083 | |
| 1084 | if (local == NULL) { |
| 1085 | pr_debug("No such device\n"); |
| 1086 | return; |
| 1087 | } |
| 1088 | |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 1089 | nfc_llcp_local_put(local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | int __init nfc_llcp_init(void) |
| 1093 | { |
| 1094 | INIT_LIST_HEAD(&llcp_devices); |
| 1095 | |
| 1096 | return nfc_llcp_sock_init(); |
| 1097 | } |
| 1098 | |
| 1099 | void nfc_llcp_exit(void) |
| 1100 | { |
| 1101 | nfc_llcp_sock_exit(); |
| 1102 | } |