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; |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 470 | u8 *lto_tlv, lto_length; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 471 | u8 *wks_tlv, wks_length; |
Samuel Ortiz | 56d5876 | 2012-04-10 19:43:19 +0200 | [diff] [blame] | 472 | u8 *miux_tlv, miux_length; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 473 | u8 gb_len = 0; |
Wei Yongjun | 52da244 | 2012-09-02 21:21:46 +0800 | [diff] [blame] | 474 | int ret = 0; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 475 | |
| 476 | version = LLCP_VERSION_11; |
| 477 | version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 478 | 1, &version_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 479 | gb_len += version_length; |
| 480 | |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 481 | lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, <o_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 482 | gb_len += lto_length; |
| 483 | |
| 484 | pr_debug("Local wks 0x%lx\n", local->local_wks); |
| 485 | 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] | 486 | &wks_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 487 | gb_len += wks_length; |
| 488 | |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 489 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0, |
Samuel Ortiz | 56d5876 | 2012-04-10 19:43:19 +0200 | [diff] [blame] | 490 | &miux_length); |
| 491 | gb_len += miux_length; |
| 492 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 493 | gb_len += ARRAY_SIZE(llcp_magic); |
| 494 | |
| 495 | if (gb_len > NFC_MAX_GT_LEN) { |
Wei Yongjun | 52da244 | 2012-09-02 21:21:46 +0800 | [diff] [blame] | 496 | ret = -EINVAL; |
| 497 | goto out; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | gb_cur = local->gb; |
| 501 | |
| 502 | memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic)); |
| 503 | gb_cur += ARRAY_SIZE(llcp_magic); |
| 504 | |
| 505 | memcpy(gb_cur, version_tlv, version_length); |
| 506 | gb_cur += version_length; |
| 507 | |
| 508 | memcpy(gb_cur, lto_tlv, lto_length); |
| 509 | gb_cur += lto_length; |
| 510 | |
| 511 | memcpy(gb_cur, wks_tlv, wks_length); |
| 512 | gb_cur += wks_length; |
| 513 | |
Samuel Ortiz | 56d5876 | 2012-04-10 19:43:19 +0200 | [diff] [blame] | 514 | memcpy(gb_cur, miux_tlv, miux_length); |
| 515 | gb_cur += miux_length; |
| 516 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 517 | local->gb_len = gb_len; |
| 518 | |
Wei Yongjun | 52da244 | 2012-09-02 21:21:46 +0800 | [diff] [blame] | 519 | out: |
| 520 | kfree(version_tlv); |
| 521 | kfree(lto_tlv); |
| 522 | kfree(wks_tlv); |
| 523 | kfree(miux_tlv); |
| 524 | |
| 525 | return ret; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 526 | } |
| 527 | |
Samuel Ortiz | b8e7a06 | 2012-06-22 02:04:53 +0200 | [diff] [blame] | 528 | u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len) |
| 529 | { |
| 530 | struct nfc_llcp_local *local; |
| 531 | |
| 532 | local = nfc_llcp_find_local(dev); |
| 533 | if (local == NULL) { |
| 534 | *general_bytes_len = 0; |
| 535 | return NULL; |
| 536 | } |
| 537 | |
| 538 | nfc_llcp_build_gb(local); |
| 539 | |
| 540 | *general_bytes_len = local->gb_len; |
| 541 | |
| 542 | return local->gb; |
| 543 | } |
| 544 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 545 | int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len) |
| 546 | { |
| 547 | struct nfc_llcp_local *local = nfc_llcp_find_local(dev); |
| 548 | |
| 549 | if (local == NULL) { |
| 550 | pr_err("No LLCP device\n"); |
| 551 | return -ENODEV; |
| 552 | } |
| 553 | |
| 554 | memset(local->remote_gb, 0, NFC_MAX_GT_LEN); |
| 555 | memcpy(local->remote_gb, gb, gb_len); |
| 556 | local->remote_gb_len = gb_len; |
| 557 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 558 | if (local->remote_gb == NULL || local->remote_gb_len == 0) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 559 | return -ENODEV; |
| 560 | |
| 561 | if (memcmp(local->remote_gb, llcp_magic, 3)) { |
| 562 | pr_err("MAC does not support LLCP\n"); |
| 563 | return -EINVAL; |
| 564 | } |
| 565 | |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 566 | return nfc_llcp_parse_gb_tlv(local, |
| 567 | &local->remote_gb[3], |
| 568 | local->remote_gb_len - 3); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 571 | static u8 nfc_llcp_dsap(struct sk_buff *pdu) |
| 572 | { |
| 573 | return (pdu->data[0] & 0xfc) >> 2; |
| 574 | } |
| 575 | |
| 576 | static u8 nfc_llcp_ptype(struct sk_buff *pdu) |
| 577 | { |
| 578 | return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6); |
| 579 | } |
| 580 | |
| 581 | static u8 nfc_llcp_ssap(struct sk_buff *pdu) |
| 582 | { |
| 583 | return pdu->data[1] & 0x3f; |
| 584 | } |
| 585 | |
| 586 | static u8 nfc_llcp_ns(struct sk_buff *pdu) |
| 587 | { |
| 588 | return pdu->data[2] >> 4; |
| 589 | } |
| 590 | |
| 591 | static u8 nfc_llcp_nr(struct sk_buff *pdu) |
| 592 | { |
| 593 | return pdu->data[2] & 0xf; |
| 594 | } |
| 595 | |
| 596 | static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu) |
| 597 | { |
Samuel Ortiz | 279cf17 | 2012-04-10 19:43:14 +0200 | [diff] [blame] | 598 | pdu->data[2] = (sock->send_n << 4) | (sock->recv_n); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 599 | sock->send_n = (sock->send_n + 1) % 16; |
| 600 | sock->recv_ack_n = (sock->recv_n - 1) % 16; |
| 601 | } |
| 602 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 603 | void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local, |
| 604 | struct sk_buff *skb, u8 direction) |
| 605 | { |
| 606 | struct hlist_node *node; |
| 607 | struct sk_buff *skb_copy = NULL, *nskb; |
| 608 | struct sock *sk; |
| 609 | u8 *data; |
| 610 | |
| 611 | read_lock(&local->raw_sockets.lock); |
| 612 | |
| 613 | sk_for_each(sk, node, &local->raw_sockets.head) { |
| 614 | if (sk->sk_state != LLCP_BOUND) |
| 615 | continue; |
| 616 | |
| 617 | if (skb_copy == NULL) { |
| 618 | skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE, |
| 619 | GFP_ATOMIC); |
| 620 | |
| 621 | if (skb_copy == NULL) |
| 622 | continue; |
| 623 | |
| 624 | data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE); |
| 625 | |
| 626 | data[0] = local->dev ? local->dev->idx : 0xFF; |
| 627 | data[1] = direction; |
| 628 | } |
| 629 | |
| 630 | nskb = skb_clone(skb_copy, GFP_ATOMIC); |
| 631 | if (!nskb) |
| 632 | continue; |
| 633 | |
| 634 | if (sock_queue_rcv_skb(sk, nskb)) |
| 635 | kfree_skb(nskb); |
| 636 | } |
| 637 | |
| 638 | read_unlock(&local->raw_sockets.lock); |
| 639 | |
| 640 | kfree_skb(skb_copy); |
| 641 | } |
| 642 | |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 643 | static void nfc_llcp_tx_work(struct work_struct *work) |
| 644 | { |
| 645 | struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, |
| 646 | tx_work); |
| 647 | struct sk_buff *skb; |
| 648 | struct sock *sk; |
| 649 | struct nfc_llcp_sock *llcp_sock; |
| 650 | |
| 651 | skb = skb_dequeue(&local->tx_queue); |
| 652 | if (skb != NULL) { |
| 653 | sk = skb->sk; |
| 654 | llcp_sock = nfc_llcp_sock(sk); |
Samuel Ortiz | a6a0915 | 2012-10-05 00:37:22 +0200 | [diff] [blame] | 655 | |
| 656 | if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) { |
| 657 | nfc_llcp_send_symm(local->dev); |
| 658 | } else { |
Samuel Ortiz | be02b6b | 2012-11-01 23:36:07 +0100 | [diff] [blame] | 659 | struct sk_buff *copy_skb = NULL; |
| 660 | u8 ptype = nfc_llcp_ptype(skb); |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 661 | int ret; |
| 662 | |
| 663 | pr_debug("Sending pending skb\n"); |
| 664 | print_hex_dump(KERN_DEBUG, "LLCP Tx: ", |
| 665 | DUMP_PREFIX_OFFSET, 16, 1, |
| 666 | skb->data, skb->len, true); |
| 667 | |
Samuel Ortiz | be02b6b | 2012-11-01 23:36:07 +0100 | [diff] [blame] | 668 | if (ptype == LLCP_PDU_I) |
| 669 | copy_skb = skb_copy(skb, GFP_ATOMIC); |
| 670 | |
Thierry Escande | 2c2d45b | 2012-11-27 15:44:24 +0100 | [diff] [blame^] | 671 | __net_timestamp(skb); |
| 672 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 673 | nfc_llcp_send_to_raw_sock(local, skb, |
| 674 | NFC_LLCP_DIRECTION_TX); |
| 675 | |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 676 | ret = nfc_data_exchange(local->dev, local->target_idx, |
| 677 | skb, nfc_llcp_recv, local); |
| 678 | |
Samuel Ortiz | be02b6b | 2012-11-01 23:36:07 +0100 | [diff] [blame] | 679 | if (ret) { |
| 680 | kfree_skb(copy_skb); |
| 681 | goto out; |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 682 | } |
Samuel Ortiz | be02b6b | 2012-11-01 23:36:07 +0100 | [diff] [blame] | 683 | |
| 684 | if (ptype == LLCP_PDU_I && copy_skb) |
| 685 | skb_queue_tail(&llcp_sock->tx_pending_queue, |
| 686 | copy_skb); |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 687 | } |
| 688 | } else { |
| 689 | nfc_llcp_send_symm(local->dev); |
| 690 | } |
| 691 | |
Samuel Ortiz | be02b6b | 2012-11-01 23:36:07 +0100 | [diff] [blame] | 692 | out: |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 693 | mod_timer(&local->link_timer, |
| 694 | jiffies + msecs_to_jiffies(2 * local->remote_lto)); |
| 695 | } |
| 696 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 697 | static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local, |
| 698 | u8 ssap) |
| 699 | { |
| 700 | struct sock *sk; |
| 701 | struct nfc_llcp_sock *llcp_sock; |
| 702 | struct hlist_node *node; |
| 703 | |
| 704 | read_lock(&local->connecting_sockets.lock); |
| 705 | |
| 706 | sk_for_each(sk, node, &local->connecting_sockets.head) { |
| 707 | llcp_sock = nfc_llcp_sock(sk); |
| 708 | |
Samuel Ortiz | 5a0f6f3 | 2012-05-21 11:44:42 +0200 | [diff] [blame] | 709 | if (llcp_sock->ssap == ssap) { |
| 710 | sock_hold(&llcp_sock->sk); |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 711 | goto out; |
Samuel Ortiz | 5a0f6f3 | 2012-05-21 11:44:42 +0200 | [diff] [blame] | 712 | } |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | llcp_sock = NULL; |
| 716 | |
| 717 | out: |
| 718 | read_unlock(&local->connecting_sockets.lock); |
| 719 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 720 | return llcp_sock; |
| 721 | } |
| 722 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 723 | static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local, |
| 724 | u8 *sn, size_t sn_len) |
| 725 | { |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 726 | struct nfc_llcp_sock *llcp_sock; |
| 727 | |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 728 | llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 729 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 730 | if (llcp_sock == NULL) |
| 731 | return NULL; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 732 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 733 | sock_hold(&llcp_sock->sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 734 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 735 | return llcp_sock; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 736 | } |
| 737 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 738 | static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len) |
| 739 | { |
| 740 | u8 *tlv = &skb->data[2], type, length; |
| 741 | size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0; |
| 742 | |
| 743 | while (offset < tlv_array_len) { |
| 744 | type = tlv[0]; |
| 745 | length = tlv[1]; |
| 746 | |
| 747 | pr_debug("type 0x%x length %d\n", type, length); |
| 748 | |
| 749 | if (type == LLCP_TLV_SN) { |
| 750 | *sn_len = length; |
| 751 | return &tlv[2]; |
| 752 | } |
| 753 | |
| 754 | offset += length + 2; |
| 755 | tlv += length + 2; |
| 756 | } |
| 757 | |
| 758 | return NULL; |
| 759 | } |
| 760 | |
Samuel Ortiz | 968272b | 2012-10-15 16:14:37 +0200 | [diff] [blame] | 761 | static void nfc_llcp_recv_ui(struct nfc_llcp_local *local, |
| 762 | struct sk_buff *skb) |
| 763 | { |
| 764 | struct nfc_llcp_sock *llcp_sock; |
| 765 | struct nfc_llcp_ui_cb *ui_cb; |
| 766 | u8 dsap, ssap; |
| 767 | |
| 768 | dsap = nfc_llcp_dsap(skb); |
| 769 | ssap = nfc_llcp_ssap(skb); |
| 770 | |
| 771 | ui_cb = nfc_llcp_ui_skb_cb(skb); |
| 772 | ui_cb->dsap = dsap; |
| 773 | ui_cb->ssap = ssap; |
| 774 | |
| 775 | printk("%s %d %d\n", __func__, dsap, ssap); |
| 776 | |
| 777 | pr_debug("%d %d\n", dsap, ssap); |
| 778 | |
| 779 | /* We're looking for a bound socket, not a client one */ |
| 780 | llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP); |
| 781 | if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM) |
| 782 | return; |
| 783 | |
| 784 | /* There is no sequence with UI frames */ |
| 785 | skb_pull(skb, LLCP_HEADER_SIZE); |
| 786 | if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) { |
| 787 | pr_err("receive queue is full\n"); |
| 788 | skb_queue_head(&llcp_sock->tx_backlog_queue, skb); |
| 789 | } |
| 790 | |
| 791 | nfc_llcp_sock_put(llcp_sock); |
| 792 | } |
| 793 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 794 | static void nfc_llcp_recv_connect(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 795 | struct sk_buff *skb) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 796 | { |
| 797 | struct sock *new_sk, *parent; |
| 798 | struct nfc_llcp_sock *sock, *new_sock; |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 799 | u8 dsap, ssap, reason; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 800 | |
| 801 | dsap = nfc_llcp_dsap(skb); |
| 802 | ssap = nfc_llcp_ssap(skb); |
| 803 | |
| 804 | pr_debug("%d %d\n", dsap, ssap); |
| 805 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 806 | if (dsap != LLCP_SAP_SDP) { |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 807 | sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP); |
| 808 | if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 809 | reason = LLCP_DM_NOBOUND; |
| 810 | goto fail; |
| 811 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 812 | } else { |
| 813 | u8 *sn; |
| 814 | size_t sn_len; |
| 815 | |
| 816 | sn = nfc_llcp_connect_sn(skb, &sn_len); |
| 817 | if (sn == NULL) { |
| 818 | reason = LLCP_DM_NOBOUND; |
| 819 | goto fail; |
| 820 | } |
| 821 | |
| 822 | pr_debug("Service name length %zu\n", sn_len); |
| 823 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 824 | sock = nfc_llcp_sock_get_sn(local, sn, sn_len); |
| 825 | if (sock == NULL) { |
| 826 | reason = LLCP_DM_NOBOUND; |
| 827 | goto fail; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 828 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 831 | lock_sock(&sock->sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 832 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 833 | parent = &sock->sk; |
| 834 | |
| 835 | if (sk_acceptq_is_full(parent)) { |
| 836 | reason = LLCP_DM_REJ; |
| 837 | release_sock(&sock->sk); |
| 838 | sock_put(&sock->sk); |
| 839 | goto fail; |
| 840 | } |
| 841 | |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 842 | if (sock->ssap == LLCP_SDP_UNBOUND) { |
| 843 | u8 ssap = nfc_llcp_reserve_sdp_ssap(local); |
| 844 | |
| 845 | pr_debug("First client, reserving %d\n", ssap); |
| 846 | |
| 847 | if (ssap == LLCP_SAP_MAX) { |
| 848 | reason = LLCP_DM_REJ; |
| 849 | release_sock(&sock->sk); |
| 850 | sock_put(&sock->sk); |
| 851 | goto fail; |
| 852 | } |
| 853 | |
| 854 | sock->ssap = ssap; |
| 855 | } |
| 856 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 857 | new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 858 | if (new_sk == NULL) { |
| 859 | reason = LLCP_DM_REJ; |
| 860 | release_sock(&sock->sk); |
| 861 | sock_put(&sock->sk); |
| 862 | goto fail; |
| 863 | } |
| 864 | |
| 865 | new_sock = nfc_llcp_sock(new_sk); |
| 866 | new_sock->dev = local->dev; |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 867 | new_sock->local = nfc_llcp_local_get(local); |
Samuel Ortiz | 93d7e49 | 2012-05-14 17:37:32 +0200 | [diff] [blame] | 868 | new_sock->miu = local->remote_miu; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 869 | new_sock->nfc_protocol = sock->nfc_protocol; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 870 | new_sock->dsap = ssap; |
Samuel Ortiz | 025f152 | 2012-06-18 21:38:09 +0200 | [diff] [blame] | 871 | new_sock->target_idx = local->target_idx; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 872 | new_sock->parent = parent; |
Samuel Ortiz | 8f50020 | 2012-06-25 15:46:28 +0200 | [diff] [blame] | 873 | new_sock->ssap = sock->ssap; |
| 874 | if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) { |
| 875 | atomic_t *client_count; |
| 876 | |
| 877 | pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock); |
| 878 | |
| 879 | client_count = |
| 880 | &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP]; |
| 881 | |
| 882 | atomic_inc(client_count); |
| 883 | new_sock->reserved_ssap = sock->ssap; |
| 884 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 885 | |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 886 | nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE], |
| 887 | skb->len - LLCP_HEADER_SIZE); |
| 888 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 889 | pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk); |
| 890 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 891 | nfc_llcp_sock_link(&local->sockets, new_sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 892 | |
| 893 | nfc_llcp_accept_enqueue(&sock->sk, new_sk); |
| 894 | |
| 895 | nfc_get_device(local->dev->idx); |
| 896 | |
| 897 | new_sk->sk_state = LLCP_CONNECTED; |
| 898 | |
| 899 | /* Wake the listening processes */ |
| 900 | parent->sk_data_ready(parent, 0); |
| 901 | |
| 902 | /* Send CC */ |
| 903 | nfc_llcp_send_cc(new_sock); |
| 904 | |
| 905 | release_sock(&sock->sk); |
| 906 | sock_put(&sock->sk); |
| 907 | |
| 908 | return; |
| 909 | |
| 910 | fail: |
| 911 | /* Send DM */ |
| 912 | nfc_llcp_send_dm(local, dsap, ssap, reason); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 913 | } |
| 914 | |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 915 | int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock) |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 916 | { |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 917 | int nr_frames = 0; |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 918 | struct nfc_llcp_local *local = sock->local; |
| 919 | |
| 920 | pr_debug("Remote ready %d tx queue len %d remote rw %d", |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 921 | sock->remote_ready, skb_queue_len(&sock->tx_pending_queue), |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 922 | sock->rw); |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 923 | |
| 924 | /* Try to queue some I frames for transmission */ |
| 925 | while (sock->remote_ready && |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 926 | skb_queue_len(&sock->tx_pending_queue) < sock->rw) { |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 927 | struct sk_buff *pdu; |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 928 | |
| 929 | pdu = skb_dequeue(&sock->tx_queue); |
| 930 | if (pdu == NULL) |
| 931 | break; |
| 932 | |
| 933 | /* Update N(S)/N(R) */ |
| 934 | nfc_llcp_set_nrns(sock, pdu); |
| 935 | |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 936 | skb_queue_tail(&local->tx_queue, pdu); |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 937 | nr_frames++; |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 938 | } |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 939 | |
| 940 | return nr_frames; |
Samuel Ortiz | 4722d2b | 2012-03-05 01:03:35 +0100 | [diff] [blame] | 941 | } |
| 942 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 943 | static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 944 | struct sk_buff *skb) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 945 | { |
| 946 | struct nfc_llcp_sock *llcp_sock; |
| 947 | struct sock *sk; |
| 948 | u8 dsap, ssap, ptype, ns, nr; |
| 949 | |
| 950 | ptype = nfc_llcp_ptype(skb); |
| 951 | dsap = nfc_llcp_dsap(skb); |
| 952 | ssap = nfc_llcp_ssap(skb); |
| 953 | ns = nfc_llcp_ns(skb); |
| 954 | nr = nfc_llcp_nr(skb); |
| 955 | |
| 956 | pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns); |
| 957 | |
| 958 | llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); |
| 959 | if (llcp_sock == NULL) { |
| 960 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); |
| 961 | return; |
| 962 | } |
| 963 | |
| 964 | sk = &llcp_sock->sk; |
| 965 | lock_sock(sk); |
| 966 | if (sk->sk_state == LLCP_CLOSED) { |
| 967 | release_sock(sk); |
| 968 | nfc_llcp_sock_put(llcp_sock); |
| 969 | } |
| 970 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 971 | /* Pass the payload upstream */ |
| 972 | if (ptype == LLCP_PDU_I) { |
| 973 | pr_debug("I frame, queueing on %p\n", &llcp_sock->sk); |
| 974 | |
Samuel Ortiz | 53aef92 | 2012-03-05 01:03:36 +0100 | [diff] [blame] | 975 | if (ns == llcp_sock->recv_n) |
| 976 | llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16; |
| 977 | else |
| 978 | pr_err("Received out of sequence I PDU\n"); |
| 979 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 980 | skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE); |
| 981 | if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) { |
| 982 | pr_err("receive queue is full\n"); |
| 983 | skb_queue_head(&llcp_sock->tx_backlog_queue, skb); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | /* Remove skbs from the pending queue */ |
| 988 | if (llcp_sock->send_ack_n != nr) { |
| 989 | struct sk_buff *s, *tmp; |
Waldemar Rymarkiewicz | 2898149 | 2012-11-26 08:40:04 +0100 | [diff] [blame] | 990 | u8 n; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 991 | |
| 992 | llcp_sock->send_ack_n = nr; |
| 993 | |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 994 | /* Remove and free all skbs until ns == nr */ |
| 995 | skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) { |
Waldemar Rymarkiewicz | 2898149 | 2012-11-26 08:40:04 +0100 | [diff] [blame] | 996 | n = nfc_llcp_ns(s); |
| 997 | |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 998 | skb_unlink(s, &llcp_sock->tx_pending_queue); |
| 999 | kfree_skb(s); |
| 1000 | |
Waldemar Rymarkiewicz | 2898149 | 2012-11-26 08:40:04 +0100 | [diff] [blame] | 1001 | if (n == nr) |
Samuel Ortiz | 8445796 | 2012-05-30 17:48:29 +0200 | [diff] [blame] | 1002 | break; |
| 1003 | } |
| 1004 | |
| 1005 | /* Re-queue the remaining skbs for transmission */ |
| 1006 | skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue, |
| 1007 | s, tmp) { |
| 1008 | skb_unlink(s, &llcp_sock->tx_pending_queue); |
| 1009 | skb_queue_head(&local->tx_queue, s); |
| 1010 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1011 | } |
| 1012 | |
Samuel Ortiz | 53aef92 | 2012-03-05 01:03:36 +0100 | [diff] [blame] | 1013 | if (ptype == LLCP_PDU_RR) |
| 1014 | llcp_sock->remote_ready = true; |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1015 | else if (ptype == LLCP_PDU_RNR) |
Samuel Ortiz | 53aef92 | 2012-03-05 01:03:36 +0100 | [diff] [blame] | 1016 | llcp_sock->remote_ready = false; |
| 1017 | |
Samuel Ortiz | 56af2568 | 2012-05-30 18:06:11 +0200 | [diff] [blame] | 1018 | 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] | 1019 | nfc_llcp_send_rr(llcp_sock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1020 | |
| 1021 | release_sock(sk); |
| 1022 | nfc_llcp_sock_put(llcp_sock); |
| 1023 | } |
| 1024 | |
| 1025 | static void nfc_llcp_recv_disc(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1026 | struct sk_buff *skb) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1027 | { |
| 1028 | struct nfc_llcp_sock *llcp_sock; |
| 1029 | struct sock *sk; |
| 1030 | u8 dsap, ssap; |
| 1031 | |
| 1032 | dsap = nfc_llcp_dsap(skb); |
| 1033 | ssap = nfc_llcp_ssap(skb); |
| 1034 | |
| 1035 | llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); |
| 1036 | if (llcp_sock == NULL) { |
| 1037 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); |
| 1038 | return; |
| 1039 | } |
| 1040 | |
| 1041 | sk = &llcp_sock->sk; |
| 1042 | lock_sock(sk); |
Samuel Ortiz | f31652a | 2012-10-26 18:20:10 +0200 | [diff] [blame] | 1043 | |
| 1044 | nfc_llcp_socket_purge(llcp_sock); |
| 1045 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1046 | if (sk->sk_state == LLCP_CLOSED) { |
| 1047 | release_sock(sk); |
| 1048 | nfc_llcp_sock_put(llcp_sock); |
| 1049 | } |
| 1050 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1051 | if (sk->sk_state == LLCP_CONNECTED) { |
| 1052 | nfc_put_device(local->dev); |
| 1053 | sk->sk_state = LLCP_CLOSED; |
| 1054 | sk->sk_state_change(sk); |
| 1055 | } |
| 1056 | |
| 1057 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC); |
| 1058 | |
| 1059 | release_sock(sk); |
| 1060 | nfc_llcp_sock_put(llcp_sock); |
| 1061 | } |
| 1062 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1063 | 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] | 1064 | { |
| 1065 | struct nfc_llcp_sock *llcp_sock; |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 1066 | struct sock *sk; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1067 | u8 dsap, ssap; |
| 1068 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1069 | dsap = nfc_llcp_dsap(skb); |
| 1070 | ssap = nfc_llcp_ssap(skb); |
| 1071 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 1072 | llcp_sock = nfc_llcp_connecting_sock_get(local, dsap); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1073 | if (llcp_sock == NULL) { |
| 1074 | pr_err("Invalid CC\n"); |
| 1075 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); |
| 1076 | |
| 1077 | return; |
| 1078 | } |
| 1079 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 1080 | sk = &llcp_sock->sk; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1081 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 1082 | /* Unlink from connecting and link to the client array */ |
| 1083 | nfc_llcp_sock_unlink(&local->connecting_sockets, sk); |
| 1084 | nfc_llcp_sock_link(&local->sockets, sk); |
| 1085 | llcp_sock->dsap = ssap; |
| 1086 | |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 1087 | nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE], |
| 1088 | skb->len - LLCP_HEADER_SIZE); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1089 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 1090 | sk->sk_state = LLCP_CONNECTED; |
| 1091 | sk->sk_state_change(sk); |
| 1092 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1093 | nfc_llcp_sock_put(llcp_sock); |
| 1094 | } |
| 1095 | |
Samuel Ortiz | 5c0560b | 2012-06-26 16:13:29 +0200 | [diff] [blame] | 1096 | static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb) |
| 1097 | { |
| 1098 | struct nfc_llcp_sock *llcp_sock; |
| 1099 | struct sock *sk; |
| 1100 | u8 dsap, ssap, reason; |
| 1101 | |
| 1102 | dsap = nfc_llcp_dsap(skb); |
| 1103 | ssap = nfc_llcp_ssap(skb); |
| 1104 | reason = skb->data[2]; |
| 1105 | |
| 1106 | pr_debug("%d %d reason %d\n", ssap, dsap, reason); |
| 1107 | |
| 1108 | switch (reason) { |
| 1109 | case LLCP_DM_NOBOUND: |
| 1110 | case LLCP_DM_REJ: |
| 1111 | llcp_sock = nfc_llcp_connecting_sock_get(local, dsap); |
| 1112 | break; |
| 1113 | |
| 1114 | default: |
| 1115 | llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); |
| 1116 | break; |
| 1117 | } |
| 1118 | |
| 1119 | if (llcp_sock == NULL) { |
Samuel Ortiz | a8df0f3 | 2012-10-16 21:15:59 +0200 | [diff] [blame] | 1120 | pr_debug("Already closed\n"); |
Samuel Ortiz | 5c0560b | 2012-06-26 16:13:29 +0200 | [diff] [blame] | 1121 | return; |
| 1122 | } |
| 1123 | |
| 1124 | sk = &llcp_sock->sk; |
| 1125 | |
| 1126 | sk->sk_err = ENXIO; |
| 1127 | sk->sk_state = LLCP_CLOSED; |
| 1128 | sk->sk_state_change(sk); |
| 1129 | |
| 1130 | nfc_llcp_sock_put(llcp_sock); |
Samuel Ortiz | 5c0560b | 2012-06-26 16:13:29 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Samuel Ortiz | 19cfe58 | 2012-10-05 01:21:47 +0200 | [diff] [blame] | 1133 | static void nfc_llcp_recv_snl(struct nfc_llcp_local *local, |
| 1134 | struct sk_buff *skb) |
| 1135 | { |
| 1136 | struct nfc_llcp_sock *llcp_sock; |
| 1137 | u8 dsap, ssap, *tlv, type, length, tid, sap; |
| 1138 | u16 tlv_len, offset; |
| 1139 | char *service_name; |
| 1140 | size_t service_name_len; |
| 1141 | |
| 1142 | dsap = nfc_llcp_dsap(skb); |
| 1143 | ssap = nfc_llcp_ssap(skb); |
| 1144 | |
| 1145 | pr_debug("%d %d\n", dsap, ssap); |
| 1146 | |
| 1147 | if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) { |
| 1148 | pr_err("Wrong SNL SAP\n"); |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | tlv = &skb->data[LLCP_HEADER_SIZE]; |
| 1153 | tlv_len = skb->len - LLCP_HEADER_SIZE; |
| 1154 | offset = 0; |
| 1155 | |
Szymon Janc | 0f45077 | 2012-10-17 15:23:39 +0200 | [diff] [blame] | 1156 | while (offset < tlv_len) { |
Samuel Ortiz | 19cfe58 | 2012-10-05 01:21:47 +0200 | [diff] [blame] | 1157 | type = tlv[0]; |
| 1158 | length = tlv[1]; |
| 1159 | |
| 1160 | switch (type) { |
| 1161 | case LLCP_TLV_SDREQ: |
| 1162 | tid = tlv[2]; |
| 1163 | service_name = (char *) &tlv[3]; |
| 1164 | service_name_len = length - 1; |
| 1165 | |
Samuel Ortiz | e690408 | 2012-10-15 14:28:13 +0200 | [diff] [blame] | 1166 | pr_debug("Looking for %.16s\n", service_name); |
Samuel Ortiz | 19cfe58 | 2012-10-05 01:21:47 +0200 | [diff] [blame] | 1167 | |
| 1168 | if (service_name_len == strlen("urn:nfc:sn:sdp") && |
| 1169 | !strncmp(service_name, "urn:nfc:sn:sdp", |
| 1170 | service_name_len)) { |
| 1171 | sap = 1; |
Samuel Ortiz | e690408 | 2012-10-15 14:28:13 +0200 | [diff] [blame] | 1172 | goto send_snl; |
Samuel Ortiz | 19cfe58 | 2012-10-05 01:21:47 +0200 | [diff] [blame] | 1173 | } |
| 1174 | |
Samuel Ortiz | e690408 | 2012-10-15 14:28:13 +0200 | [diff] [blame] | 1175 | llcp_sock = nfc_llcp_sock_from_sn(local, service_name, |
| 1176 | service_name_len); |
| 1177 | if (!llcp_sock) { |
| 1178 | sap = 0; |
| 1179 | goto send_snl; |
| 1180 | } |
| 1181 | |
| 1182 | /* |
| 1183 | * We found a socket but its ssap has not been reserved |
| 1184 | * yet. We need to assign it for good and send a reply. |
| 1185 | * The ssap will be freed when the socket is closed. |
| 1186 | */ |
| 1187 | if (llcp_sock->ssap == LLCP_SDP_UNBOUND) { |
| 1188 | atomic_t *client_count; |
| 1189 | |
| 1190 | sap = nfc_llcp_reserve_sdp_ssap(local); |
| 1191 | |
| 1192 | pr_debug("Reserving %d\n", sap); |
| 1193 | |
| 1194 | if (sap == LLCP_SAP_MAX) { |
| 1195 | sap = 0; |
| 1196 | goto send_snl; |
| 1197 | } |
| 1198 | |
| 1199 | client_count = |
| 1200 | &local->local_sdp_cnt[sap - |
| 1201 | LLCP_WKS_NUM_SAP]; |
| 1202 | |
| 1203 | atomic_inc(client_count); |
| 1204 | |
| 1205 | llcp_sock->ssap = sap; |
| 1206 | llcp_sock->reserved_ssap = sap; |
| 1207 | } else { |
| 1208 | sap = llcp_sock->ssap; |
| 1209 | } |
| 1210 | |
| 1211 | pr_debug("%p %d\n", llcp_sock, sap); |
| 1212 | |
Szymon Janc | 0f45077 | 2012-10-17 15:23:39 +0200 | [diff] [blame] | 1213 | send_snl: |
Samuel Ortiz | 19cfe58 | 2012-10-05 01:21:47 +0200 | [diff] [blame] | 1214 | nfc_llcp_send_snl(local, tid, sap); |
| 1215 | break; |
| 1216 | |
| 1217 | default: |
| 1218 | pr_err("Invalid SNL tlv value 0x%x\n", type); |
| 1219 | break; |
| 1220 | } |
| 1221 | |
| 1222 | offset += length + 2; |
| 1223 | tlv += length + 2; |
| 1224 | } |
| 1225 | } |
| 1226 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1227 | static void nfc_llcp_rx_work(struct work_struct *work) |
| 1228 | { |
| 1229 | struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1230 | rx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1231 | u8 dsap, ssap, ptype; |
| 1232 | struct sk_buff *skb; |
| 1233 | |
| 1234 | skb = local->rx_pending; |
| 1235 | if (skb == NULL) { |
| 1236 | pr_debug("No pending SKB\n"); |
| 1237 | return; |
| 1238 | } |
| 1239 | |
| 1240 | ptype = nfc_llcp_ptype(skb); |
| 1241 | dsap = nfc_llcp_dsap(skb); |
| 1242 | ssap = nfc_llcp_ssap(skb); |
| 1243 | |
| 1244 | pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap); |
| 1245 | |
Samuel Ortiz | 4be646e | 2012-04-10 19:43:13 +0200 | [diff] [blame] | 1246 | if (ptype != LLCP_PDU_SYMM) |
| 1247 | print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET, |
| 1248 | 16, 1, skb->data, skb->len, true); |
| 1249 | |
Thierry Escande | 2c2d45b | 2012-11-27 15:44:24 +0100 | [diff] [blame^] | 1250 | __net_timestamp(skb); |
| 1251 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 1252 | nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX); |
| 1253 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1254 | switch (ptype) { |
| 1255 | case LLCP_PDU_SYMM: |
| 1256 | pr_debug("SYMM\n"); |
| 1257 | break; |
| 1258 | |
Samuel Ortiz | 968272b | 2012-10-15 16:14:37 +0200 | [diff] [blame] | 1259 | case LLCP_PDU_UI: |
| 1260 | pr_debug("UI\n"); |
| 1261 | nfc_llcp_recv_ui(local, skb); |
| 1262 | break; |
| 1263 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1264 | case LLCP_PDU_CONNECT: |
| 1265 | pr_debug("CONNECT\n"); |
| 1266 | nfc_llcp_recv_connect(local, skb); |
| 1267 | break; |
| 1268 | |
| 1269 | case LLCP_PDU_DISC: |
| 1270 | pr_debug("DISC\n"); |
| 1271 | nfc_llcp_recv_disc(local, skb); |
| 1272 | break; |
| 1273 | |
| 1274 | case LLCP_PDU_CC: |
| 1275 | pr_debug("CC\n"); |
| 1276 | nfc_llcp_recv_cc(local, skb); |
| 1277 | break; |
| 1278 | |
Samuel Ortiz | 5c0560b | 2012-06-26 16:13:29 +0200 | [diff] [blame] | 1279 | case LLCP_PDU_DM: |
| 1280 | pr_debug("DM\n"); |
| 1281 | nfc_llcp_recv_dm(local, skb); |
| 1282 | break; |
| 1283 | |
Samuel Ortiz | 19cfe58 | 2012-10-05 01:21:47 +0200 | [diff] [blame] | 1284 | case LLCP_PDU_SNL: |
| 1285 | pr_debug("SNL\n"); |
| 1286 | nfc_llcp_recv_snl(local, skb); |
| 1287 | break; |
| 1288 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1289 | case LLCP_PDU_I: |
| 1290 | case LLCP_PDU_RR: |
Samuel Ortiz | 53aef92 | 2012-03-05 01:03:36 +0100 | [diff] [blame] | 1291 | case LLCP_PDU_RNR: |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1292 | pr_debug("I frame\n"); |
| 1293 | nfc_llcp_recv_hdlc(local, skb); |
| 1294 | break; |
| 1295 | |
| 1296 | } |
| 1297 | |
Linus Torvalds | 916082b | 2012-10-02 16:01:31 -0700 | [diff] [blame] | 1298 | schedule_work(&local->tx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1299 | kfree_skb(local->rx_pending); |
| 1300 | local->rx_pending = NULL; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | void nfc_llcp_recv(void *data, struct sk_buff *skb, int err) |
| 1304 | { |
| 1305 | struct nfc_llcp_local *local = (struct nfc_llcp_local *) data; |
| 1306 | |
| 1307 | pr_debug("Received an LLCP PDU\n"); |
| 1308 | if (err < 0) { |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1309 | pr_err("err %d\n", err); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1310 | return; |
| 1311 | } |
| 1312 | |
| 1313 | local->rx_pending = skb_get(skb); |
| 1314 | del_timer(&local->link_timer); |
Linus Torvalds | 916082b | 2012-10-02 16:01:31 -0700 | [diff] [blame] | 1315 | schedule_work(&local->rx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1316 | } |
| 1317 | |
Samuel Ortiz | 73167ce | 2012-05-31 00:05:50 +0200 | [diff] [blame] | 1318 | int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb) |
| 1319 | { |
| 1320 | struct nfc_llcp_local *local; |
| 1321 | |
| 1322 | local = nfc_llcp_find_local(dev); |
| 1323 | if (local == NULL) |
| 1324 | return -ENODEV; |
| 1325 | |
| 1326 | local->rx_pending = skb_get(skb); |
| 1327 | del_timer(&local->link_timer); |
Linus Torvalds | 916082b | 2012-10-02 16:01:31 -0700 | [diff] [blame] | 1328 | schedule_work(&local->rx_work); |
Samuel Ortiz | 73167ce | 2012-05-31 00:05:50 +0200 | [diff] [blame] | 1329 | |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1333 | void nfc_llcp_mac_is_down(struct nfc_dev *dev) |
| 1334 | { |
| 1335 | struct nfc_llcp_local *local; |
| 1336 | |
| 1337 | local = nfc_llcp_find_local(dev); |
| 1338 | if (local == NULL) |
| 1339 | return; |
| 1340 | |
| 1341 | /* Close and purge all existing sockets */ |
Samuel Ortiz | 4d22ea1 | 2012-06-22 14:40:34 +0200 | [diff] [blame] | 1342 | nfc_llcp_socket_release(local, true); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx, |
| 1346 | u8 comm_mode, u8 rf_mode) |
| 1347 | { |
| 1348 | struct nfc_llcp_local *local; |
| 1349 | |
| 1350 | pr_debug("rf mode %d\n", rf_mode); |
| 1351 | |
| 1352 | local = nfc_llcp_find_local(dev); |
| 1353 | if (local == NULL) |
| 1354 | return; |
| 1355 | |
| 1356 | local->target_idx = target_idx; |
| 1357 | local->comm_mode = comm_mode; |
| 1358 | local->rf_mode = rf_mode; |
| 1359 | |
| 1360 | if (rf_mode == NFC_RF_INITIATOR) { |
| 1361 | pr_debug("Queueing Tx work\n"); |
| 1362 | |
Linus Torvalds | 916082b | 2012-10-02 16:01:31 -0700 | [diff] [blame] | 1363 | schedule_work(&local->tx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1364 | } else { |
| 1365 | mod_timer(&local->link_timer, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 1366 | jiffies + msecs_to_jiffies(local->remote_lto)); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | int nfc_llcp_register_device(struct nfc_dev *ndev) |
| 1371 | { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1372 | struct nfc_llcp_local *local; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1373 | |
| 1374 | local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL); |
| 1375 | if (local == NULL) |
| 1376 | return -ENOMEM; |
| 1377 | |
| 1378 | local->dev = ndev; |
| 1379 | INIT_LIST_HEAD(&local->list); |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 1380 | kref_init(&local->ref); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1381 | mutex_init(&local->sdp_lock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1382 | init_timer(&local->link_timer); |
| 1383 | local->link_timer.data = (unsigned long) local; |
| 1384 | local->link_timer.function = nfc_llcp_symm_timer; |
| 1385 | |
| 1386 | skb_queue_head_init(&local->tx_queue); |
| 1387 | INIT_WORK(&local->tx_work, nfc_llcp_tx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1388 | |
| 1389 | local->rx_pending = NULL; |
| 1390 | INIT_WORK(&local->rx_work, nfc_llcp_rx_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1391 | |
| 1392 | INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1393 | |
Szymon Janc | fe235b5 | 2012-09-25 12:42:50 +0200 | [diff] [blame] | 1394 | rwlock_init(&local->sockets.lock); |
| 1395 | rwlock_init(&local->connecting_sockets.lock); |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 1396 | rwlock_init(&local->raw_sockets.lock); |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 1397 | |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 1398 | local->lto = 150; /* 1500 ms */ |
| 1399 | local->rw = LLCP_MAX_RW; |
| 1400 | local->miux = cpu_to_be16(LLCP_MAX_MIUX); |
| 1401 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1402 | nfc_llcp_build_gb(local); |
| 1403 | |
| 1404 | local->remote_miu = LLCP_DEFAULT_MIU; |
| 1405 | local->remote_lto = LLCP_DEFAULT_LTO; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1406 | |
Thierry Escande | 16a78e9 | 2012-10-12 15:25:43 +0200 | [diff] [blame] | 1407 | list_add(&local->list, &llcp_devices); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1408 | |
| 1409 | return 0; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | void nfc_llcp_unregister_device(struct nfc_dev *dev) |
| 1413 | { |
| 1414 | struct nfc_llcp_local *local = nfc_llcp_find_local(dev); |
| 1415 | |
| 1416 | if (local == NULL) { |
| 1417 | pr_debug("No such device\n"); |
| 1418 | return; |
| 1419 | } |
| 1420 | |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 1421 | nfc_llcp_local_put(local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | int __init nfc_llcp_init(void) |
| 1425 | { |
| 1426 | INIT_LIST_HEAD(&llcp_devices); |
| 1427 | |
| 1428 | return nfc_llcp_sock_init(); |
| 1429 | } |
| 1430 | |
| 1431 | void nfc_llcp_exit(void) |
| 1432 | { |
| 1433 | nfc_llcp_sock_exit(); |
| 1434 | } |