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