Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the |
| 16 | * Free Software Foundation, Inc., |
| 17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #define pr_fmt(fmt) "llcp: %s: " fmt, __func__ |
| 21 | |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/nfc.h> |
| 26 | |
| 27 | #include <net/nfc/nfc.h> |
| 28 | |
| 29 | #include "../nfc.h" |
| 30 | #include "llcp.h" |
| 31 | |
| 32 | static u8 llcp_tlv_length[LLCP_TLV_MAX] = { |
| 33 | 0, |
| 34 | 1, /* VERSION */ |
| 35 | 2, /* MIUX */ |
| 36 | 2, /* WKS */ |
| 37 | 1, /* LTO */ |
| 38 | 1, /* RW */ |
| 39 | 0, /* SN */ |
| 40 | 1, /* OPT */ |
| 41 | 0, /* SDREQ */ |
| 42 | 2, /* SDRES */ |
| 43 | |
| 44 | }; |
| 45 | |
| 46 | static u8 llcp_tlv8(u8 *tlv, u8 type) |
| 47 | { |
| 48 | if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]]) |
| 49 | return 0; |
| 50 | |
| 51 | return tlv[2]; |
| 52 | } |
| 53 | |
Samuel Ortiz | 76762b7 | 2012-05-14 17:38:54 +0200 | [diff] [blame] | 54 | static u16 llcp_tlv16(u8 *tlv, u8 type) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 55 | { |
| 56 | if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]]) |
| 57 | return 0; |
| 58 | |
| 59 | return be16_to_cpu(*((__be16 *)(tlv + 2))); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static u8 llcp_tlv_version(u8 *tlv) |
| 64 | { |
| 65 | return llcp_tlv8(tlv, LLCP_TLV_VERSION); |
| 66 | } |
| 67 | |
| 68 | static u16 llcp_tlv_miux(u8 *tlv) |
| 69 | { |
Samuel Ortiz | 76762b7 | 2012-05-14 17:38:54 +0200 | [diff] [blame] | 70 | return llcp_tlv16(tlv, LLCP_TLV_MIUX) & 0x7ff; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static u16 llcp_tlv_wks(u8 *tlv) |
| 74 | { |
| 75 | return llcp_tlv16(tlv, LLCP_TLV_WKS); |
| 76 | } |
| 77 | |
| 78 | static u16 llcp_tlv_lto(u8 *tlv) |
| 79 | { |
| 80 | return llcp_tlv8(tlv, LLCP_TLV_LTO); |
| 81 | } |
| 82 | |
| 83 | static u8 llcp_tlv_opt(u8 *tlv) |
| 84 | { |
| 85 | return llcp_tlv8(tlv, LLCP_TLV_OPT); |
| 86 | } |
| 87 | |
| 88 | static u8 llcp_tlv_rw(u8 *tlv) |
| 89 | { |
| 90 | return llcp_tlv8(tlv, LLCP_TLV_RW) & 0xf; |
| 91 | } |
| 92 | |
| 93 | u8 *nfc_llcp_build_tlv(u8 type, u8 *value, u8 value_length, u8 *tlv_length) |
| 94 | { |
| 95 | u8 *tlv, length; |
| 96 | |
| 97 | pr_debug("type %d\n", type); |
| 98 | |
| 99 | if (type >= LLCP_TLV_MAX) |
| 100 | return NULL; |
| 101 | |
| 102 | length = llcp_tlv_length[type]; |
| 103 | if (length == 0 && value_length == 0) |
| 104 | return NULL; |
Samuel Ortiz | 324b0af | 2012-04-10 19:43:15 +0200 | [diff] [blame] | 105 | else if (length == 0) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 106 | length = value_length; |
| 107 | |
| 108 | *tlv_length = 2 + length; |
| 109 | tlv = kzalloc(2 + length, GFP_KERNEL); |
| 110 | if (tlv == NULL) |
| 111 | return tlv; |
| 112 | |
| 113 | tlv[0] = type; |
| 114 | tlv[1] = length; |
| 115 | memcpy(tlv + 2, value, length); |
| 116 | |
| 117 | return tlv; |
| 118 | } |
| 119 | |
Thierry Escande | e0ae7ba | 2013-02-15 10:43:05 +0100 | [diff] [blame] | 120 | struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdres_tlv(u8 tid, u8 sap) |
| 121 | { |
| 122 | struct nfc_llcp_sdp_tlv *sdres; |
| 123 | u8 value[2]; |
| 124 | |
| 125 | sdres = kzalloc(sizeof(struct nfc_llcp_sdp_tlv), GFP_KERNEL); |
| 126 | if (sdres == NULL) |
| 127 | return NULL; |
| 128 | |
| 129 | value[0] = tid; |
| 130 | value[1] = sap; |
| 131 | |
| 132 | sdres->tlv = nfc_llcp_build_tlv(LLCP_TLV_SDRES, value, 2, |
| 133 | &sdres->tlv_len); |
| 134 | if (sdres->tlv == NULL) { |
| 135 | kfree(sdres); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | sdres->tid = tid; |
| 140 | sdres->sap = sap; |
| 141 | |
| 142 | INIT_HLIST_NODE(&sdres->node); |
| 143 | |
| 144 | return sdres; |
| 145 | } |
| 146 | |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame^] | 147 | struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdreq_tlv(u8 tid, char *uri, |
| 148 | size_t uri_len) |
| 149 | { |
| 150 | struct nfc_llcp_sdp_tlv *sdreq; |
| 151 | |
| 152 | pr_debug("uri: %s, len: %zu\n", uri, uri_len); |
| 153 | |
| 154 | sdreq = kzalloc(sizeof(struct nfc_llcp_sdp_tlv), GFP_KERNEL); |
| 155 | if (sdreq == NULL) |
| 156 | return NULL; |
| 157 | |
| 158 | sdreq->tlv_len = uri_len + 3; |
| 159 | |
| 160 | if (uri[uri_len - 1] == 0) |
| 161 | sdreq->tlv_len--; |
| 162 | |
| 163 | sdreq->tlv = kzalloc(sdreq->tlv_len + 1, GFP_KERNEL); |
| 164 | if (sdreq->tlv == NULL) { |
| 165 | kfree(sdreq); |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | sdreq->tlv[0] = LLCP_TLV_SDREQ; |
| 170 | sdreq->tlv[1] = sdreq->tlv_len - 2; |
| 171 | sdreq->tlv[2] = tid; |
| 172 | |
| 173 | sdreq->tid = tid; |
| 174 | sdreq->uri = sdreq->tlv + 3; |
| 175 | memcpy(sdreq->uri, uri, uri_len); |
| 176 | |
| 177 | INIT_HLIST_NODE(&sdreq->node); |
| 178 | |
| 179 | return sdreq; |
| 180 | } |
| 181 | |
Thierry Escande | e0ae7ba | 2013-02-15 10:43:05 +0100 | [diff] [blame] | 182 | void nfc_llcp_free_sdp_tlv(struct nfc_llcp_sdp_tlv *sdp) |
| 183 | { |
| 184 | kfree(sdp->tlv); |
| 185 | kfree(sdp); |
| 186 | } |
| 187 | |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame^] | 188 | void nfc_llcp_free_sdp_tlv_list(struct hlist_head *head) |
| 189 | { |
| 190 | struct nfc_llcp_sdp_tlv *sdp; |
| 191 | struct hlist_node *n; |
| 192 | |
| 193 | hlist_for_each_entry_safe(sdp, n, head, node) { |
| 194 | hlist_del(&sdp->node); |
| 195 | |
| 196 | nfc_llcp_free_sdp_tlv(sdp); |
| 197 | } |
| 198 | } |
| 199 | |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 200 | int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local, |
| 201 | u8 *tlv_array, u16 tlv_array_len) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 202 | { |
| 203 | u8 *tlv = tlv_array, type, length, offset = 0; |
| 204 | |
| 205 | pr_debug("TLV array length %d\n", tlv_array_len); |
| 206 | |
| 207 | if (local == NULL) |
| 208 | return -ENODEV; |
| 209 | |
| 210 | while (offset < tlv_array_len) { |
| 211 | type = tlv[0]; |
| 212 | length = tlv[1]; |
| 213 | |
| 214 | pr_debug("type 0x%x length %d\n", type, length); |
| 215 | |
| 216 | switch (type) { |
| 217 | case LLCP_TLV_VERSION: |
| 218 | local->remote_version = llcp_tlv_version(tlv); |
| 219 | break; |
| 220 | case LLCP_TLV_MIUX: |
| 221 | local->remote_miu = llcp_tlv_miux(tlv) + 128; |
| 222 | break; |
| 223 | case LLCP_TLV_WKS: |
| 224 | local->remote_wks = llcp_tlv_wks(tlv); |
| 225 | break; |
| 226 | case LLCP_TLV_LTO: |
| 227 | local->remote_lto = llcp_tlv_lto(tlv) * 10; |
| 228 | break; |
| 229 | case LLCP_TLV_OPT: |
| 230 | local->remote_opt = llcp_tlv_opt(tlv); |
| 231 | break; |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 232 | default: |
| 233 | pr_err("Invalid gt tlv value 0x%x\n", type); |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | offset += length + 2; |
| 238 | tlv += length + 2; |
| 239 | } |
| 240 | |
| 241 | pr_debug("version 0x%x miu %d lto %d opt 0x%x wks 0x%x\n", |
| 242 | local->remote_version, local->remote_miu, |
| 243 | local->remote_lto, local->remote_opt, |
| 244 | local->remote_wks); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | int nfc_llcp_parse_connection_tlv(struct nfc_llcp_sock *sock, |
| 250 | u8 *tlv_array, u16 tlv_array_len) |
| 251 | { |
| 252 | u8 *tlv = tlv_array, type, length, offset = 0; |
| 253 | |
| 254 | pr_debug("TLV array length %d\n", tlv_array_len); |
| 255 | |
| 256 | if (sock == NULL) |
| 257 | return -ENOTCONN; |
| 258 | |
| 259 | while (offset < tlv_array_len) { |
| 260 | type = tlv[0]; |
| 261 | length = tlv[1]; |
| 262 | |
| 263 | pr_debug("type 0x%x length %d\n", type, length); |
| 264 | |
| 265 | switch (type) { |
Samuel Ortiz | 93d7e49 | 2012-05-14 17:37:32 +0200 | [diff] [blame] | 266 | case LLCP_TLV_MIUX: |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 267 | sock->remote_miu = llcp_tlv_miux(tlv) + 128; |
Samuel Ortiz | 93d7e49 | 2012-05-14 17:37:32 +0200 | [diff] [blame] | 268 | break; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 269 | case LLCP_TLV_RW: |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 270 | sock->remote_rw = llcp_tlv_rw(tlv); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 271 | break; |
Samuel Ortiz | 9dda50f | 2012-03-05 01:03:49 +0100 | [diff] [blame] | 272 | case LLCP_TLV_SN: |
| 273 | break; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 274 | default: |
| 275 | pr_err("Invalid gt tlv value 0x%x\n", type); |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | offset += length + 2; |
| 280 | tlv += length + 2; |
| 281 | } |
| 282 | |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 283 | pr_debug("sock %p rw %d miu %d\n", sock, |
| 284 | sock->remote_rw, sock->remote_miu); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static struct sk_buff *llcp_add_header(struct sk_buff *pdu, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 290 | u8 dsap, u8 ssap, u8 ptype) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 291 | { |
| 292 | u8 header[2]; |
| 293 | |
| 294 | pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap); |
| 295 | |
| 296 | header[0] = (u8)((dsap << 2) | (ptype >> 2)); |
| 297 | header[1] = (u8)((ptype << 6) | ssap); |
| 298 | |
| 299 | pr_debug("header 0x%x 0x%x\n", header[0], header[1]); |
| 300 | |
| 301 | memcpy(skb_put(pdu, LLCP_HEADER_SIZE), header, LLCP_HEADER_SIZE); |
| 302 | |
| 303 | return pdu; |
| 304 | } |
| 305 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 306 | static struct sk_buff *llcp_add_tlv(struct sk_buff *pdu, u8 *tlv, |
| 307 | u8 tlv_length) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 308 | { |
| 309 | /* XXX Add an skb length check */ |
| 310 | |
| 311 | if (tlv == NULL) |
| 312 | return NULL; |
| 313 | |
| 314 | memcpy(skb_put(pdu, tlv_length), tlv, tlv_length); |
| 315 | |
| 316 | return pdu; |
| 317 | } |
| 318 | |
| 319 | static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 320 | u8 cmd, u16 size) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 321 | { |
| 322 | struct sk_buff *skb; |
| 323 | int err; |
| 324 | |
| 325 | if (sock->ssap == 0) |
| 326 | return NULL; |
| 327 | |
| 328 | skb = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 329 | size + LLCP_HEADER_SIZE, &err); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 330 | if (skb == NULL) { |
| 331 | pr_err("Could not allocate PDU\n"); |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | skb = llcp_add_header(skb, sock->dsap, sock->ssap, cmd); |
| 336 | |
| 337 | return skb; |
| 338 | } |
| 339 | |
| 340 | int nfc_llcp_disconnect(struct nfc_llcp_sock *sock) |
| 341 | { |
| 342 | struct sk_buff *skb; |
| 343 | struct nfc_dev *dev; |
| 344 | struct nfc_llcp_local *local; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 345 | |
| 346 | pr_debug("Sending DISC\n"); |
| 347 | |
| 348 | local = sock->local; |
| 349 | if (local == NULL) |
| 350 | return -ENODEV; |
| 351 | |
| 352 | dev = sock->dev; |
| 353 | if (dev == NULL) |
| 354 | return -ENODEV; |
| 355 | |
Samuel Ortiz | 9222390 | 2012-10-05 01:09:07 +0200 | [diff] [blame] | 356 | skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 357 | if (skb == NULL) |
| 358 | return -ENOMEM; |
| 359 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 360 | skb_queue_tail(&local->tx_queue, skb); |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | int nfc_llcp_send_symm(struct nfc_dev *dev) |
| 366 | { |
| 367 | struct sk_buff *skb; |
| 368 | struct nfc_llcp_local *local; |
| 369 | u16 size = 0; |
| 370 | |
| 371 | pr_debug("Sending SYMM\n"); |
| 372 | |
| 373 | local = nfc_llcp_find_local(dev); |
| 374 | if (local == NULL) |
| 375 | return -ENODEV; |
| 376 | |
| 377 | size += LLCP_HEADER_SIZE; |
| 378 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 379 | |
| 380 | skb = alloc_skb(size, GFP_KERNEL); |
| 381 | if (skb == NULL) |
| 382 | return -ENOMEM; |
| 383 | |
| 384 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 385 | |
| 386 | skb = llcp_add_header(skb, 0, 0, LLCP_PDU_SYMM); |
| 387 | |
Thierry Escande | 2c2d45b | 2012-11-27 15:44:24 +0100 | [diff] [blame] | 388 | __net_timestamp(skb); |
| 389 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 390 | nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_TX); |
| 391 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 392 | return nfc_data_exchange(dev, local->target_idx, skb, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 393 | nfc_llcp_recv, local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | int nfc_llcp_send_connect(struct nfc_llcp_sock *sock) |
| 397 | { |
| 398 | struct nfc_llcp_local *local; |
| 399 | struct sk_buff *skb; |
| 400 | u8 *service_name_tlv = NULL, service_name_tlv_length; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 401 | u8 *miux_tlv = NULL, miux_tlv_length; |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame] | 402 | u8 *rw_tlv = NULL, rw_tlv_length, rw; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 403 | int err; |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame] | 404 | u16 size = 0, miux; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 405 | |
| 406 | pr_debug("Sending CONNECT\n"); |
| 407 | |
| 408 | local = sock->local; |
| 409 | if (local == NULL) |
| 410 | return -ENODEV; |
| 411 | |
| 412 | if (sock->service_name != NULL) { |
| 413 | service_name_tlv = nfc_llcp_build_tlv(LLCP_TLV_SN, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 414 | sock->service_name, |
| 415 | sock->service_name_len, |
| 416 | &service_name_tlv_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 417 | size += service_name_tlv_length; |
| 418 | } |
| 419 | |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame] | 420 | /* If the socket parameters are not set, use the local ones */ |
| 421 | miux = sock->miux > LLCP_MAX_MIUX ? local->miux : sock->miux; |
| 422 | rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw; |
| 423 | |
| 424 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 425 | &miux_tlv_length); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 426 | size += miux_tlv_length; |
| 427 | |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame] | 428 | rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 429 | size += rw_tlv_length; |
| 430 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 431 | pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len); |
| 432 | |
| 433 | skb = llcp_allocate_pdu(sock, LLCP_PDU_CONNECT, size); |
| 434 | if (skb == NULL) { |
| 435 | err = -ENOMEM; |
| 436 | goto error_tlv; |
| 437 | } |
| 438 | |
| 439 | if (service_name_tlv != NULL) |
| 440 | skb = llcp_add_tlv(skb, service_name_tlv, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 441 | service_name_tlv_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 442 | |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 443 | skb = llcp_add_tlv(skb, miux_tlv, miux_tlv_length); |
| 444 | skb = llcp_add_tlv(skb, rw_tlv, rw_tlv_length); |
| 445 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 446 | skb_queue_tail(&local->tx_queue, skb); |
| 447 | |
| 448 | return 0; |
| 449 | |
| 450 | error_tlv: |
| 451 | pr_err("error %d\n", err); |
| 452 | |
| 453 | kfree(service_name_tlv); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 454 | kfree(miux_tlv); |
| 455 | kfree(rw_tlv); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 456 | |
| 457 | return err; |
| 458 | } |
| 459 | |
| 460 | int nfc_llcp_send_cc(struct nfc_llcp_sock *sock) |
| 461 | { |
| 462 | struct nfc_llcp_local *local; |
| 463 | struct sk_buff *skb; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 464 | u8 *miux_tlv = NULL, miux_tlv_length; |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame] | 465 | u8 *rw_tlv = NULL, rw_tlv_length, rw; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 466 | int err; |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame] | 467 | u16 size = 0, miux; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 468 | |
| 469 | pr_debug("Sending CC\n"); |
| 470 | |
| 471 | local = sock->local; |
| 472 | if (local == NULL) |
| 473 | return -ENODEV; |
| 474 | |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame] | 475 | /* If the socket parameters are not set, use the local ones */ |
| 476 | miux = sock->miux > LLCP_MAX_MIUX ? local->miux : sock->miux; |
| 477 | rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw; |
| 478 | |
| 479 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 480 | &miux_tlv_length); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 481 | size += miux_tlv_length; |
| 482 | |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame] | 483 | rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 484 | size += rw_tlv_length; |
| 485 | |
| 486 | skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, size); |
| 487 | if (skb == NULL) { |
| 488 | err = -ENOMEM; |
| 489 | goto error_tlv; |
| 490 | } |
| 491 | |
| 492 | skb = llcp_add_tlv(skb, miux_tlv, miux_tlv_length); |
| 493 | skb = llcp_add_tlv(skb, rw_tlv, rw_tlv_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 494 | |
| 495 | skb_queue_tail(&local->tx_queue, skb); |
| 496 | |
| 497 | return 0; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 498 | |
| 499 | error_tlv: |
| 500 | pr_err("error %d\n", err); |
| 501 | |
| 502 | kfree(miux_tlv); |
| 503 | kfree(rw_tlv); |
| 504 | |
| 505 | return err; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Thierry Escande | e0ae7ba | 2013-02-15 10:43:05 +0100 | [diff] [blame] | 508 | static struct sk_buff *nfc_llcp_allocate_snl(struct nfc_llcp_local *local, |
| 509 | size_t tlv_length) |
Samuel Ortiz | c43bb03 | 2012-10-05 01:13:24 +0200 | [diff] [blame] | 510 | { |
| 511 | struct sk_buff *skb; |
| 512 | struct nfc_dev *dev; |
Samuel Ortiz | c43bb03 | 2012-10-05 01:13:24 +0200 | [diff] [blame] | 513 | u16 size = 0; |
| 514 | |
Samuel Ortiz | c43bb03 | 2012-10-05 01:13:24 +0200 | [diff] [blame] | 515 | if (local == NULL) |
Thierry Escande | e0ae7ba | 2013-02-15 10:43:05 +0100 | [diff] [blame] | 516 | return ERR_PTR(-ENODEV); |
Samuel Ortiz | c43bb03 | 2012-10-05 01:13:24 +0200 | [diff] [blame] | 517 | |
| 518 | dev = local->dev; |
| 519 | if (dev == NULL) |
Thierry Escande | e0ae7ba | 2013-02-15 10:43:05 +0100 | [diff] [blame] | 520 | return ERR_PTR(-ENODEV); |
Samuel Ortiz | c43bb03 | 2012-10-05 01:13:24 +0200 | [diff] [blame] | 521 | |
| 522 | size += LLCP_HEADER_SIZE; |
| 523 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
Thierry Escande | e0ae7ba | 2013-02-15 10:43:05 +0100 | [diff] [blame] | 524 | size += tlv_length; |
Samuel Ortiz | c43bb03 | 2012-10-05 01:13:24 +0200 | [diff] [blame] | 525 | |
| 526 | skb = alloc_skb(size, GFP_KERNEL); |
Thierry Escande | e0ae7ba | 2013-02-15 10:43:05 +0100 | [diff] [blame] | 527 | if (skb == NULL) |
| 528 | return ERR_PTR(-ENOMEM); |
Samuel Ortiz | c43bb03 | 2012-10-05 01:13:24 +0200 | [diff] [blame] | 529 | |
| 530 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 531 | |
| 532 | skb = llcp_add_header(skb, LLCP_SAP_SDP, LLCP_SAP_SDP, LLCP_PDU_SNL); |
| 533 | |
Thierry Escande | e0ae7ba | 2013-02-15 10:43:05 +0100 | [diff] [blame] | 534 | return skb; |
| 535 | } |
| 536 | |
| 537 | int nfc_llcp_send_snl_sdres(struct nfc_llcp_local *local, |
| 538 | struct hlist_head *tlv_list, size_t tlvs_len) |
| 539 | { |
| 540 | struct nfc_llcp_sdp_tlv *sdp; |
| 541 | struct hlist_node *n; |
| 542 | struct sk_buff *skb; |
| 543 | |
| 544 | skb = nfc_llcp_allocate_snl(local, tlvs_len); |
| 545 | if (IS_ERR(skb)) |
| 546 | return PTR_ERR(skb); |
| 547 | |
| 548 | hlist_for_each_entry_safe(sdp, n, tlv_list, node) { |
| 549 | memcpy(skb_put(skb, sdp->tlv_len), sdp->tlv, sdp->tlv_len); |
| 550 | |
| 551 | hlist_del(&sdp->node); |
| 552 | |
| 553 | nfc_llcp_free_sdp_tlv(sdp); |
| 554 | } |
Samuel Ortiz | c43bb03 | 2012-10-05 01:13:24 +0200 | [diff] [blame] | 555 | |
| 556 | skb_queue_tail(&local->tx_queue, skb); |
| 557 | |
Samuel Ortiz | c43bb03 | 2012-10-05 01:13:24 +0200 | [diff] [blame] | 558 | return 0; |
| 559 | } |
| 560 | |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame^] | 561 | int nfc_llcp_send_snl_sdreq(struct nfc_llcp_local *local, |
| 562 | struct hlist_head *tlv_list, size_t tlvs_len) |
| 563 | { |
| 564 | struct nfc_llcp_sdp_tlv *sdreq; |
| 565 | struct hlist_node *n; |
| 566 | struct sk_buff *skb; |
| 567 | |
| 568 | skb = nfc_llcp_allocate_snl(local, tlvs_len); |
| 569 | if (IS_ERR(skb)) |
| 570 | return PTR_ERR(skb); |
| 571 | |
| 572 | mutex_lock(&local->sdreq_lock); |
| 573 | |
| 574 | hlist_for_each_entry_safe(sdreq, n, tlv_list, node) { |
| 575 | pr_debug("tid %d for %s\n", sdreq->tid, sdreq->uri); |
| 576 | |
| 577 | memcpy(skb_put(skb, sdreq->tlv_len), sdreq->tlv, |
| 578 | sdreq->tlv_len); |
| 579 | |
| 580 | hlist_del(&sdreq->node); |
| 581 | |
| 582 | hlist_add_head(&sdreq->node, &local->pending_sdreqs); |
| 583 | } |
| 584 | |
| 585 | mutex_unlock(&local->sdreq_lock); |
| 586 | |
| 587 | skb_queue_tail(&local->tx_queue, skb); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 592 | int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason) |
| 593 | { |
| 594 | struct sk_buff *skb; |
| 595 | struct nfc_dev *dev; |
| 596 | u16 size = 1; /* Reason code */ |
| 597 | |
| 598 | pr_debug("Sending DM reason 0x%x\n", reason); |
| 599 | |
| 600 | if (local == NULL) |
| 601 | return -ENODEV; |
| 602 | |
| 603 | dev = local->dev; |
| 604 | if (dev == NULL) |
| 605 | return -ENODEV; |
| 606 | |
| 607 | size += LLCP_HEADER_SIZE; |
| 608 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 609 | |
| 610 | skb = alloc_skb(size, GFP_KERNEL); |
| 611 | if (skb == NULL) |
| 612 | return -ENOMEM; |
| 613 | |
| 614 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 615 | |
Samuel Ortiz | ffc2931 | 2012-04-10 19:43:16 +0200 | [diff] [blame] | 616 | skb = llcp_add_header(skb, dsap, ssap, LLCP_PDU_DM); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 617 | |
| 618 | memcpy(skb_put(skb, 1), &reason, 1); |
| 619 | |
| 620 | skb_queue_head(&local->tx_queue, skb); |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock) |
| 626 | { |
| 627 | struct sk_buff *skb; |
| 628 | struct nfc_llcp_local *local; |
| 629 | |
| 630 | pr_debug("Send DISC\n"); |
| 631 | |
| 632 | local = sock->local; |
| 633 | if (local == NULL) |
| 634 | return -ENODEV; |
| 635 | |
| 636 | skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0); |
| 637 | if (skb == NULL) |
| 638 | return -ENOMEM; |
| 639 | |
| 640 | skb_queue_head(&local->tx_queue, skb); |
| 641 | |
| 642 | return 0; |
| 643 | } |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 644 | |
| 645 | int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 646 | struct msghdr *msg, size_t len) |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 647 | { |
| 648 | struct sk_buff *pdu; |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 649 | struct sock *sk = &sock->sk; |
| 650 | struct nfc_llcp_local *local; |
| 651 | size_t frag_len = 0, remaining_len; |
| 652 | u8 *msg_data, *msg_ptr; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 653 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 654 | pr_debug("Send I frame len %zd\n", len); |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 655 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 656 | local = sock->local; |
| 657 | if (local == NULL) |
| 658 | return -ENODEV; |
| 659 | |
Samuel Ortiz | dd2bf43 | 2012-11-01 23:33:00 +0100 | [diff] [blame] | 660 | /* Remote is ready but has not acknowledged our frames */ |
| 661 | if((sock->remote_ready && |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 662 | skb_queue_len(&sock->tx_pending_queue) >= sock->remote_rw && |
| 663 | skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) { |
Samuel Ortiz | dd2bf43 | 2012-11-01 23:33:00 +0100 | [diff] [blame] | 664 | pr_err("Pending queue is full %d frames\n", |
| 665 | skb_queue_len(&sock->tx_pending_queue)); |
| 666 | return -ENOBUFS; |
| 667 | } |
| 668 | |
| 669 | /* Remote is not ready and we've been queueing enough frames */ |
| 670 | if ((!sock->remote_ready && |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 671 | skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) { |
Samuel Ortiz | dd2bf43 | 2012-11-01 23:33:00 +0100 | [diff] [blame] | 672 | pr_err("Tx queue is full %d frames\n", |
| 673 | skb_queue_len(&sock->tx_queue)); |
| 674 | return -ENOBUFS; |
| 675 | } |
| 676 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 677 | msg_data = kzalloc(len, GFP_KERNEL); |
| 678 | if (msg_data == NULL) |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 679 | return -ENOMEM; |
| 680 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 681 | if (memcpy_fromiovec(msg_data, msg->msg_iov, len)) { |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 682 | kfree(msg_data); |
| 683 | return -EFAULT; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 684 | } |
| 685 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 686 | remaining_len = len; |
| 687 | msg_ptr = msg_data; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 688 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 689 | while (remaining_len > 0) { |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 690 | |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 691 | frag_len = min_t(size_t, sock->remote_miu, remaining_len); |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 692 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 693 | pr_debug("Fragment %zd bytes remaining %zd", |
| 694 | frag_len, remaining_len); |
| 695 | |
| 696 | pdu = llcp_allocate_pdu(sock, LLCP_PDU_I, |
| 697 | frag_len + LLCP_SEQUENCE_SIZE); |
| 698 | if (pdu == NULL) |
| 699 | return -ENOMEM; |
| 700 | |
| 701 | skb_put(pdu, LLCP_SEQUENCE_SIZE); |
| 702 | |
| 703 | memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len); |
| 704 | |
Samuel Ortiz | bdbc59b | 2012-05-10 19:45:52 +0200 | [diff] [blame] | 705 | skb_queue_tail(&sock->tx_queue, pdu); |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 706 | |
| 707 | lock_sock(sk); |
| 708 | |
| 709 | nfc_llcp_queue_i_frames(sock); |
| 710 | |
| 711 | release_sock(sk); |
| 712 | |
| 713 | remaining_len -= frag_len; |
Samuel Ortiz | b4838d1 | 2012-04-10 19:43:03 +0200 | [diff] [blame] | 714 | msg_ptr += frag_len; |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | kfree(msg_data); |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 718 | |
Samuel Ortiz | 43472ff | 2012-05-07 12:31:21 +0200 | [diff] [blame] | 719 | return len; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 720 | } |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 721 | |
Samuel Ortiz | 94f418a | 2012-10-16 15:01:40 +0200 | [diff] [blame] | 722 | int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, |
| 723 | struct msghdr *msg, size_t len) |
| 724 | { |
| 725 | struct sk_buff *pdu; |
| 726 | struct nfc_llcp_local *local; |
| 727 | size_t frag_len = 0, remaining_len; |
Samuel Ortiz | 6e950fd | 2012-10-29 14:02:17 +0100 | [diff] [blame] | 728 | u8 *msg_ptr, *msg_data; |
Samuel Ortiz | 94f418a | 2012-10-16 15:01:40 +0200 | [diff] [blame] | 729 | int err; |
| 730 | |
| 731 | pr_debug("Send UI frame len %zd\n", len); |
| 732 | |
| 733 | local = sock->local; |
| 734 | if (local == NULL) |
| 735 | return -ENODEV; |
| 736 | |
Samuel Ortiz | 6e950fd | 2012-10-29 14:02:17 +0100 | [diff] [blame] | 737 | msg_data = kzalloc(len, GFP_KERNEL); |
| 738 | if (msg_data == NULL) |
| 739 | return -ENOMEM; |
| 740 | |
| 741 | if (memcpy_fromiovec(msg_data, msg->msg_iov, len)) { |
| 742 | kfree(msg_data); |
| 743 | return -EFAULT; |
| 744 | } |
| 745 | |
Samuel Ortiz | 94f418a | 2012-10-16 15:01:40 +0200 | [diff] [blame] | 746 | remaining_len = len; |
Samuel Ortiz | 6e950fd | 2012-10-29 14:02:17 +0100 | [diff] [blame] | 747 | msg_ptr = msg_data; |
Samuel Ortiz | 94f418a | 2012-10-16 15:01:40 +0200 | [diff] [blame] | 748 | |
| 749 | while (remaining_len > 0) { |
| 750 | |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 751 | frag_len = min_t(size_t, sock->remote_miu, remaining_len); |
Samuel Ortiz | 94f418a | 2012-10-16 15:01:40 +0200 | [diff] [blame] | 752 | |
| 753 | pr_debug("Fragment %zd bytes remaining %zd", |
| 754 | frag_len, remaining_len); |
| 755 | |
| 756 | pdu = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT, |
| 757 | frag_len + LLCP_HEADER_SIZE, &err); |
| 758 | if (pdu == NULL) { |
| 759 | pr_err("Could not allocate PDU\n"); |
| 760 | continue; |
| 761 | } |
| 762 | |
| 763 | pdu = llcp_add_header(pdu, dsap, ssap, LLCP_PDU_UI); |
| 764 | |
| 765 | memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len); |
| 766 | |
| 767 | /* No need to check for the peer RW for UI frames */ |
| 768 | skb_queue_tail(&local->tx_queue, pdu); |
| 769 | |
| 770 | remaining_len -= frag_len; |
| 771 | msg_ptr += frag_len; |
| 772 | } |
| 773 | |
Samuel Ortiz | 6e950fd | 2012-10-29 14:02:17 +0100 | [diff] [blame] | 774 | kfree(msg_data); |
| 775 | |
Samuel Ortiz | 94f418a | 2012-10-16 15:01:40 +0200 | [diff] [blame] | 776 | return len; |
| 777 | } |
| 778 | |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 779 | int nfc_llcp_send_rr(struct nfc_llcp_sock *sock) |
| 780 | { |
| 781 | struct sk_buff *skb; |
| 782 | struct nfc_llcp_local *local; |
| 783 | |
| 784 | pr_debug("Send rr nr %d\n", sock->recv_n); |
| 785 | |
| 786 | local = sock->local; |
| 787 | if (local == NULL) |
| 788 | return -ENODEV; |
| 789 | |
| 790 | skb = llcp_allocate_pdu(sock, LLCP_PDU_RR, LLCP_SEQUENCE_SIZE); |
| 791 | if (skb == NULL) |
| 792 | return -ENOMEM; |
| 793 | |
| 794 | skb_put(skb, LLCP_SEQUENCE_SIZE); |
| 795 | |
Samuel Ortiz | 279cf17 | 2012-04-10 19:43:14 +0200 | [diff] [blame] | 796 | skb->data[2] = sock->recv_n; |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 797 | |
| 798 | skb_queue_head(&local->tx_queue, skb); |
| 799 | |
| 800 | return 0; |
| 801 | } |