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