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 | |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 120 | int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local, |
| 121 | u8 *tlv_array, u16 tlv_array_len) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 122 | { |
| 123 | u8 *tlv = tlv_array, type, length, offset = 0; |
| 124 | |
| 125 | pr_debug("TLV array length %d\n", tlv_array_len); |
| 126 | |
| 127 | if (local == NULL) |
| 128 | return -ENODEV; |
| 129 | |
| 130 | while (offset < tlv_array_len) { |
| 131 | type = tlv[0]; |
| 132 | length = tlv[1]; |
| 133 | |
| 134 | pr_debug("type 0x%x length %d\n", type, length); |
| 135 | |
| 136 | switch (type) { |
| 137 | case LLCP_TLV_VERSION: |
| 138 | local->remote_version = llcp_tlv_version(tlv); |
| 139 | break; |
| 140 | case LLCP_TLV_MIUX: |
| 141 | local->remote_miu = llcp_tlv_miux(tlv) + 128; |
| 142 | break; |
| 143 | case LLCP_TLV_WKS: |
| 144 | local->remote_wks = llcp_tlv_wks(tlv); |
| 145 | break; |
| 146 | case LLCP_TLV_LTO: |
| 147 | local->remote_lto = llcp_tlv_lto(tlv) * 10; |
| 148 | break; |
| 149 | case LLCP_TLV_OPT: |
| 150 | local->remote_opt = llcp_tlv_opt(tlv); |
| 151 | break; |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 152 | default: |
| 153 | pr_err("Invalid gt tlv value 0x%x\n", type); |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | offset += length + 2; |
| 158 | tlv += length + 2; |
| 159 | } |
| 160 | |
| 161 | pr_debug("version 0x%x miu %d lto %d opt 0x%x wks 0x%x\n", |
| 162 | local->remote_version, local->remote_miu, |
| 163 | local->remote_lto, local->remote_opt, |
| 164 | local->remote_wks); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | int nfc_llcp_parse_connection_tlv(struct nfc_llcp_sock *sock, |
| 170 | u8 *tlv_array, u16 tlv_array_len) |
| 171 | { |
| 172 | u8 *tlv = tlv_array, type, length, offset = 0; |
| 173 | |
| 174 | pr_debug("TLV array length %d\n", tlv_array_len); |
| 175 | |
| 176 | if (sock == NULL) |
| 177 | return -ENOTCONN; |
| 178 | |
| 179 | while (offset < tlv_array_len) { |
| 180 | type = tlv[0]; |
| 181 | length = tlv[1]; |
| 182 | |
| 183 | pr_debug("type 0x%x length %d\n", type, length); |
| 184 | |
| 185 | switch (type) { |
Samuel Ortiz | 93d7e49 | 2012-05-14 17:37:32 +0200 | [diff] [blame] | 186 | case LLCP_TLV_MIUX: |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 187 | sock->remote_miu = llcp_tlv_miux(tlv) + 128; |
Samuel Ortiz | 93d7e49 | 2012-05-14 17:37:32 +0200 | [diff] [blame] | 188 | break; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 189 | case LLCP_TLV_RW: |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 190 | sock->remote_rw = llcp_tlv_rw(tlv); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 191 | break; |
Samuel Ortiz | 9dda50f | 2012-03-05 01:03:49 +0100 | [diff] [blame] | 192 | case LLCP_TLV_SN: |
| 193 | break; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 194 | default: |
| 195 | pr_err("Invalid gt tlv value 0x%x\n", type); |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | offset += length + 2; |
| 200 | tlv += length + 2; |
| 201 | } |
| 202 | |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 203 | pr_debug("sock %p rw %d miu %d\n", sock, |
| 204 | sock->remote_rw, sock->remote_miu); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static struct sk_buff *llcp_add_header(struct sk_buff *pdu, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 210 | u8 dsap, u8 ssap, u8 ptype) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 211 | { |
| 212 | u8 header[2]; |
| 213 | |
| 214 | pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap); |
| 215 | |
| 216 | header[0] = (u8)((dsap << 2) | (ptype >> 2)); |
| 217 | header[1] = (u8)((ptype << 6) | ssap); |
| 218 | |
| 219 | pr_debug("header 0x%x 0x%x\n", header[0], header[1]); |
| 220 | |
| 221 | memcpy(skb_put(pdu, LLCP_HEADER_SIZE), header, LLCP_HEADER_SIZE); |
| 222 | |
| 223 | return pdu; |
| 224 | } |
| 225 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 226 | static struct sk_buff *llcp_add_tlv(struct sk_buff *pdu, u8 *tlv, |
| 227 | u8 tlv_length) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 228 | { |
| 229 | /* XXX Add an skb length check */ |
| 230 | |
| 231 | if (tlv == NULL) |
| 232 | return NULL; |
| 233 | |
| 234 | memcpy(skb_put(pdu, tlv_length), tlv, tlv_length); |
| 235 | |
| 236 | return pdu; |
| 237 | } |
| 238 | |
| 239 | static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 240 | u8 cmd, u16 size) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 241 | { |
| 242 | struct sk_buff *skb; |
| 243 | int err; |
| 244 | |
| 245 | if (sock->ssap == 0) |
| 246 | return NULL; |
| 247 | |
| 248 | skb = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 249 | size + LLCP_HEADER_SIZE, &err); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 250 | if (skb == NULL) { |
| 251 | pr_err("Could not allocate PDU\n"); |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | skb = llcp_add_header(skb, sock->dsap, sock->ssap, cmd); |
| 256 | |
| 257 | return skb; |
| 258 | } |
| 259 | |
| 260 | int nfc_llcp_disconnect(struct nfc_llcp_sock *sock) |
| 261 | { |
| 262 | struct sk_buff *skb; |
| 263 | struct nfc_dev *dev; |
| 264 | struct nfc_llcp_local *local; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 265 | |
| 266 | pr_debug("Sending DISC\n"); |
| 267 | |
| 268 | local = sock->local; |
| 269 | if (local == NULL) |
| 270 | return -ENODEV; |
| 271 | |
| 272 | dev = sock->dev; |
| 273 | if (dev == NULL) |
| 274 | return -ENODEV; |
| 275 | |
Samuel Ortiz | 9222390 | 2012-10-05 01:09:07 +0200 | [diff] [blame] | 276 | skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 277 | if (skb == NULL) |
| 278 | return -ENOMEM; |
| 279 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 280 | skb_queue_tail(&local->tx_queue, skb); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | int nfc_llcp_send_symm(struct nfc_dev *dev) |
| 286 | { |
| 287 | struct sk_buff *skb; |
| 288 | struct nfc_llcp_local *local; |
| 289 | u16 size = 0; |
| 290 | |
| 291 | pr_debug("Sending SYMM\n"); |
| 292 | |
| 293 | local = nfc_llcp_find_local(dev); |
| 294 | if (local == NULL) |
| 295 | return -ENODEV; |
| 296 | |
| 297 | size += LLCP_HEADER_SIZE; |
| 298 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 299 | |
| 300 | skb = alloc_skb(size, GFP_KERNEL); |
| 301 | if (skb == NULL) |
| 302 | return -ENOMEM; |
| 303 | |
| 304 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 305 | |
| 306 | skb = llcp_add_header(skb, 0, 0, LLCP_PDU_SYMM); |
| 307 | |
Thierry Escande | 2c2d45b | 2012-11-27 15:44:24 +0100 | [diff] [blame] | 308 | __net_timestamp(skb); |
| 309 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 310 | nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_TX); |
| 311 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 312 | return nfc_data_exchange(dev, local->target_idx, skb, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 313 | nfc_llcp_recv, local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | int nfc_llcp_send_connect(struct nfc_llcp_sock *sock) |
| 317 | { |
| 318 | struct nfc_llcp_local *local; |
| 319 | struct sk_buff *skb; |
| 320 | u8 *service_name_tlv = NULL, service_name_tlv_length; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 321 | u8 *miux_tlv = NULL, miux_tlv_length; |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame^] | 322 | u8 *rw_tlv = NULL, rw_tlv_length, rw; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 323 | int err; |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame^] | 324 | u16 size = 0, miux; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 325 | |
| 326 | pr_debug("Sending CONNECT\n"); |
| 327 | |
| 328 | local = sock->local; |
| 329 | if (local == NULL) |
| 330 | return -ENODEV; |
| 331 | |
| 332 | if (sock->service_name != NULL) { |
| 333 | service_name_tlv = nfc_llcp_build_tlv(LLCP_TLV_SN, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 334 | sock->service_name, |
| 335 | sock->service_name_len, |
| 336 | &service_name_tlv_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 337 | size += service_name_tlv_length; |
| 338 | } |
| 339 | |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame^] | 340 | /* If the socket parameters are not set, use the local ones */ |
| 341 | miux = sock->miux > LLCP_MAX_MIUX ? local->miux : sock->miux; |
| 342 | rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw; |
| 343 | |
| 344 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 345 | &miux_tlv_length); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 346 | size += miux_tlv_length; |
| 347 | |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame^] | 348 | 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] | 349 | size += rw_tlv_length; |
| 350 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 351 | pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len); |
| 352 | |
| 353 | skb = llcp_allocate_pdu(sock, LLCP_PDU_CONNECT, size); |
| 354 | if (skb == NULL) { |
| 355 | err = -ENOMEM; |
| 356 | goto error_tlv; |
| 357 | } |
| 358 | |
| 359 | if (service_name_tlv != NULL) |
| 360 | skb = llcp_add_tlv(skb, service_name_tlv, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 361 | service_name_tlv_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 362 | |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 363 | skb = llcp_add_tlv(skb, miux_tlv, miux_tlv_length); |
| 364 | skb = llcp_add_tlv(skb, rw_tlv, rw_tlv_length); |
| 365 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 366 | skb_queue_tail(&local->tx_queue, skb); |
| 367 | |
| 368 | return 0; |
| 369 | |
| 370 | error_tlv: |
| 371 | pr_err("error %d\n", err); |
| 372 | |
| 373 | kfree(service_name_tlv); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 374 | kfree(miux_tlv); |
| 375 | kfree(rw_tlv); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 376 | |
| 377 | return err; |
| 378 | } |
| 379 | |
| 380 | int nfc_llcp_send_cc(struct nfc_llcp_sock *sock) |
| 381 | { |
| 382 | struct nfc_llcp_local *local; |
| 383 | struct sk_buff *skb; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 384 | u8 *miux_tlv = NULL, miux_tlv_length; |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame^] | 385 | u8 *rw_tlv = NULL, rw_tlv_length, rw; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 386 | int err; |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame^] | 387 | u16 size = 0, miux; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 388 | |
| 389 | pr_debug("Sending CC\n"); |
| 390 | |
| 391 | local = sock->local; |
| 392 | if (local == NULL) |
| 393 | return -ENODEV; |
| 394 | |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame^] | 395 | /* If the socket parameters are not set, use the local ones */ |
| 396 | miux = sock->miux > LLCP_MAX_MIUX ? local->miux : sock->miux; |
| 397 | rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw; |
| 398 | |
| 399 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 400 | &miux_tlv_length); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 401 | size += miux_tlv_length; |
| 402 | |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame^] | 403 | 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] | 404 | size += rw_tlv_length; |
| 405 | |
| 406 | skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, size); |
| 407 | if (skb == NULL) { |
| 408 | err = -ENOMEM; |
| 409 | goto error_tlv; |
| 410 | } |
| 411 | |
| 412 | skb = llcp_add_tlv(skb, miux_tlv, miux_tlv_length); |
| 413 | skb = llcp_add_tlv(skb, rw_tlv, rw_tlv_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 414 | |
| 415 | skb_queue_tail(&local->tx_queue, skb); |
| 416 | |
| 417 | return 0; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 418 | |
| 419 | error_tlv: |
| 420 | pr_err("error %d\n", err); |
| 421 | |
| 422 | kfree(miux_tlv); |
| 423 | kfree(rw_tlv); |
| 424 | |
| 425 | return err; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 426 | } |
| 427 | |
Samuel Ortiz | c43bb03 | 2012-10-05 01:13:24 +0200 | [diff] [blame] | 428 | int nfc_llcp_send_snl(struct nfc_llcp_local *local, u8 tid, u8 sap) |
| 429 | { |
| 430 | struct sk_buff *skb; |
| 431 | struct nfc_dev *dev; |
| 432 | u8 *sdres_tlv = NULL, sdres_tlv_length, sdres[2]; |
| 433 | u16 size = 0; |
| 434 | |
| 435 | pr_debug("Sending SNL tid 0x%x sap 0x%x\n", tid, sap); |
| 436 | |
| 437 | if (local == NULL) |
| 438 | return -ENODEV; |
| 439 | |
| 440 | dev = local->dev; |
| 441 | if (dev == NULL) |
| 442 | return -ENODEV; |
| 443 | |
| 444 | sdres[0] = tid; |
| 445 | sdres[1] = sap; |
| 446 | sdres_tlv = nfc_llcp_build_tlv(LLCP_TLV_SDRES, sdres, 0, |
| 447 | &sdres_tlv_length); |
| 448 | if (sdres_tlv == NULL) |
| 449 | return -ENOMEM; |
| 450 | |
| 451 | size += LLCP_HEADER_SIZE; |
| 452 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 453 | size += sdres_tlv_length; |
| 454 | |
| 455 | skb = alloc_skb(size, GFP_KERNEL); |
| 456 | if (skb == NULL) { |
| 457 | kfree(sdres_tlv); |
| 458 | return -ENOMEM; |
| 459 | } |
| 460 | |
| 461 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 462 | |
| 463 | skb = llcp_add_header(skb, LLCP_SAP_SDP, LLCP_SAP_SDP, LLCP_PDU_SNL); |
| 464 | |
| 465 | memcpy(skb_put(skb, sdres_tlv_length), sdres_tlv, sdres_tlv_length); |
| 466 | |
| 467 | skb_queue_tail(&local->tx_queue, skb); |
| 468 | |
| 469 | kfree(sdres_tlv); |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 474 | int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason) |
| 475 | { |
| 476 | struct sk_buff *skb; |
| 477 | struct nfc_dev *dev; |
| 478 | u16 size = 1; /* Reason code */ |
| 479 | |
| 480 | pr_debug("Sending DM reason 0x%x\n", reason); |
| 481 | |
| 482 | if (local == NULL) |
| 483 | return -ENODEV; |
| 484 | |
| 485 | dev = local->dev; |
| 486 | if (dev == NULL) |
| 487 | return -ENODEV; |
| 488 | |
| 489 | size += LLCP_HEADER_SIZE; |
| 490 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 491 | |
| 492 | skb = alloc_skb(size, GFP_KERNEL); |
| 493 | if (skb == NULL) |
| 494 | return -ENOMEM; |
| 495 | |
| 496 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 497 | |
Samuel Ortiz | ffc2931 | 2012-04-10 19:43:16 +0200 | [diff] [blame] | 498 | skb = llcp_add_header(skb, dsap, ssap, LLCP_PDU_DM); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 499 | |
| 500 | memcpy(skb_put(skb, 1), &reason, 1); |
| 501 | |
| 502 | skb_queue_head(&local->tx_queue, skb); |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock) |
| 508 | { |
| 509 | struct sk_buff *skb; |
| 510 | struct nfc_llcp_local *local; |
| 511 | |
| 512 | pr_debug("Send DISC\n"); |
| 513 | |
| 514 | local = sock->local; |
| 515 | if (local == NULL) |
| 516 | return -ENODEV; |
| 517 | |
| 518 | skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0); |
| 519 | if (skb == NULL) |
| 520 | return -ENOMEM; |
| 521 | |
| 522 | skb_queue_head(&local->tx_queue, skb); |
| 523 | |
| 524 | return 0; |
| 525 | } |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 526 | |
| 527 | int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 528 | struct msghdr *msg, size_t len) |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 529 | { |
| 530 | struct sk_buff *pdu; |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 531 | struct sock *sk = &sock->sk; |
| 532 | struct nfc_llcp_local *local; |
| 533 | size_t frag_len = 0, remaining_len; |
| 534 | u8 *msg_data, *msg_ptr; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 535 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 536 | pr_debug("Send I frame len %zd\n", len); |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 537 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 538 | local = sock->local; |
| 539 | if (local == NULL) |
| 540 | return -ENODEV; |
| 541 | |
Samuel Ortiz | dd2bf43 | 2012-11-01 23:33:00 +0100 | [diff] [blame] | 542 | /* Remote is ready but has not acknowledged our frames */ |
| 543 | if((sock->remote_ready && |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 544 | skb_queue_len(&sock->tx_pending_queue) >= sock->remote_rw && |
| 545 | skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) { |
Samuel Ortiz | dd2bf43 | 2012-11-01 23:33:00 +0100 | [diff] [blame] | 546 | pr_err("Pending queue is full %d frames\n", |
| 547 | skb_queue_len(&sock->tx_pending_queue)); |
| 548 | return -ENOBUFS; |
| 549 | } |
| 550 | |
| 551 | /* Remote is not ready and we've been queueing enough frames */ |
| 552 | if ((!sock->remote_ready && |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 553 | skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) { |
Samuel Ortiz | dd2bf43 | 2012-11-01 23:33:00 +0100 | [diff] [blame] | 554 | pr_err("Tx queue is full %d frames\n", |
| 555 | skb_queue_len(&sock->tx_queue)); |
| 556 | return -ENOBUFS; |
| 557 | } |
| 558 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 559 | msg_data = kzalloc(len, GFP_KERNEL); |
| 560 | if (msg_data == NULL) |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 561 | return -ENOMEM; |
| 562 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 563 | if (memcpy_fromiovec(msg_data, msg->msg_iov, len)) { |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 564 | kfree(msg_data); |
| 565 | return -EFAULT; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 566 | } |
| 567 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 568 | remaining_len = len; |
| 569 | msg_ptr = msg_data; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 570 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 571 | while (remaining_len > 0) { |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 572 | |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 573 | frag_len = min_t(size_t, sock->remote_miu, remaining_len); |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 574 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 575 | pr_debug("Fragment %zd bytes remaining %zd", |
| 576 | frag_len, remaining_len); |
| 577 | |
| 578 | pdu = llcp_allocate_pdu(sock, LLCP_PDU_I, |
| 579 | frag_len + LLCP_SEQUENCE_SIZE); |
| 580 | if (pdu == NULL) |
| 581 | return -ENOMEM; |
| 582 | |
| 583 | skb_put(pdu, LLCP_SEQUENCE_SIZE); |
| 584 | |
| 585 | memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len); |
| 586 | |
Samuel Ortiz | bdbc59b | 2012-05-10 19:45:52 +0200 | [diff] [blame] | 587 | skb_queue_tail(&sock->tx_queue, pdu); |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 588 | |
| 589 | lock_sock(sk); |
| 590 | |
| 591 | nfc_llcp_queue_i_frames(sock); |
| 592 | |
| 593 | release_sock(sk); |
| 594 | |
| 595 | remaining_len -= frag_len; |
Samuel Ortiz | b4838d1 | 2012-04-10 19:43:03 +0200 | [diff] [blame] | 596 | msg_ptr += frag_len; |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | kfree(msg_data); |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 600 | |
Samuel Ortiz | 43472ff | 2012-05-07 12:31:21 +0200 | [diff] [blame] | 601 | return len; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 602 | } |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 603 | |
Samuel Ortiz | 94f418a | 2012-10-16 15:01:40 +0200 | [diff] [blame] | 604 | int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, |
| 605 | struct msghdr *msg, size_t len) |
| 606 | { |
| 607 | struct sk_buff *pdu; |
| 608 | struct nfc_llcp_local *local; |
| 609 | size_t frag_len = 0, remaining_len; |
Samuel Ortiz | 6e950fd | 2012-10-29 14:02:17 +0100 | [diff] [blame] | 610 | u8 *msg_ptr, *msg_data; |
Samuel Ortiz | 94f418a | 2012-10-16 15:01:40 +0200 | [diff] [blame] | 611 | int err; |
| 612 | |
| 613 | pr_debug("Send UI frame len %zd\n", len); |
| 614 | |
| 615 | local = sock->local; |
| 616 | if (local == NULL) |
| 617 | return -ENODEV; |
| 618 | |
Samuel Ortiz | 6e950fd | 2012-10-29 14:02:17 +0100 | [diff] [blame] | 619 | msg_data = kzalloc(len, GFP_KERNEL); |
| 620 | if (msg_data == NULL) |
| 621 | return -ENOMEM; |
| 622 | |
| 623 | if (memcpy_fromiovec(msg_data, msg->msg_iov, len)) { |
| 624 | kfree(msg_data); |
| 625 | return -EFAULT; |
| 626 | } |
| 627 | |
Samuel Ortiz | 94f418a | 2012-10-16 15:01:40 +0200 | [diff] [blame] | 628 | remaining_len = len; |
Samuel Ortiz | 6e950fd | 2012-10-29 14:02:17 +0100 | [diff] [blame] | 629 | msg_ptr = msg_data; |
Samuel Ortiz | 94f418a | 2012-10-16 15:01:40 +0200 | [diff] [blame] | 630 | |
| 631 | while (remaining_len > 0) { |
| 632 | |
Samuel Ortiz | e4306be | 2013-02-22 01:12:28 +0100 | [diff] [blame] | 633 | frag_len = min_t(size_t, sock->remote_miu, remaining_len); |
Samuel Ortiz | 94f418a | 2012-10-16 15:01:40 +0200 | [diff] [blame] | 634 | |
| 635 | pr_debug("Fragment %zd bytes remaining %zd", |
| 636 | frag_len, remaining_len); |
| 637 | |
| 638 | pdu = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT, |
| 639 | frag_len + LLCP_HEADER_SIZE, &err); |
| 640 | if (pdu == NULL) { |
| 641 | pr_err("Could not allocate PDU\n"); |
| 642 | continue; |
| 643 | } |
| 644 | |
| 645 | pdu = llcp_add_header(pdu, dsap, ssap, LLCP_PDU_UI); |
| 646 | |
| 647 | memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len); |
| 648 | |
| 649 | /* No need to check for the peer RW for UI frames */ |
| 650 | skb_queue_tail(&local->tx_queue, pdu); |
| 651 | |
| 652 | remaining_len -= frag_len; |
| 653 | msg_ptr += frag_len; |
| 654 | } |
| 655 | |
Samuel Ortiz | 6e950fd | 2012-10-29 14:02:17 +0100 | [diff] [blame] | 656 | kfree(msg_data); |
| 657 | |
Samuel Ortiz | 94f418a | 2012-10-16 15:01:40 +0200 | [diff] [blame] | 658 | return len; |
| 659 | } |
| 660 | |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 661 | int nfc_llcp_send_rr(struct nfc_llcp_sock *sock) |
| 662 | { |
| 663 | struct sk_buff *skb; |
| 664 | struct nfc_llcp_local *local; |
| 665 | |
| 666 | pr_debug("Send rr nr %d\n", sock->recv_n); |
| 667 | |
| 668 | local = sock->local; |
| 669 | if (local == NULL) |
| 670 | return -ENODEV; |
| 671 | |
| 672 | skb = llcp_allocate_pdu(sock, LLCP_PDU_RR, LLCP_SEQUENCE_SIZE); |
| 673 | if (skb == NULL) |
| 674 | return -ENOMEM; |
| 675 | |
| 676 | skb_put(skb, LLCP_SEQUENCE_SIZE); |
| 677 | |
Samuel Ortiz | 279cf17 | 2012-04-10 19:43:14 +0200 | [diff] [blame] | 678 | skb->data[2] = sock->recv_n; |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 679 | |
| 680 | skb_queue_head(&local->tx_queue, skb); |
| 681 | |
| 682 | return 0; |
| 683 | } |