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