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