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 | |
| 54 | static u8 llcp_tlv16(u8 *tlv, u8 type) |
| 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 | { |
| 70 | return llcp_tlv16(tlv, LLCP_TLV_MIUX) & 0x7f; |
| 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 | |
| 120 | int nfc_llcp_parse_tlv(struct nfc_llcp_local *local, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 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; |
| 152 | case LLCP_TLV_RW: |
| 153 | local->remote_rw = llcp_tlv_rw(tlv); |
| 154 | break; |
Samuel Ortiz | 9dda50f | 2012-03-05 01:03:49 +0100 | [diff] [blame] | 155 | case LLCP_TLV_SN: |
| 156 | break; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 157 | default: |
| 158 | pr_err("Invalid gt tlv value 0x%x\n", type); |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | offset += length + 2; |
| 163 | tlv += length + 2; |
| 164 | } |
| 165 | |
| 166 | pr_debug("version 0x%x miu %d lto %d opt 0x%x wks 0x%x rw %d\n", |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 167 | local->remote_version, local->remote_miu, |
| 168 | local->remote_lto, local->remote_opt, |
| 169 | local->remote_wks, local->remote_rw); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static struct sk_buff *llcp_add_header(struct sk_buff *pdu, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 175 | u8 dsap, u8 ssap, u8 ptype) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 176 | { |
| 177 | u8 header[2]; |
| 178 | |
| 179 | pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap); |
| 180 | |
| 181 | header[0] = (u8)((dsap << 2) | (ptype >> 2)); |
| 182 | header[1] = (u8)((ptype << 6) | ssap); |
| 183 | |
| 184 | pr_debug("header 0x%x 0x%x\n", header[0], header[1]); |
| 185 | |
| 186 | memcpy(skb_put(pdu, LLCP_HEADER_SIZE), header, LLCP_HEADER_SIZE); |
| 187 | |
| 188 | return pdu; |
| 189 | } |
| 190 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 191 | static struct sk_buff *llcp_add_tlv(struct sk_buff *pdu, u8 *tlv, |
| 192 | u8 tlv_length) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 193 | { |
| 194 | /* XXX Add an skb length check */ |
| 195 | |
| 196 | if (tlv == NULL) |
| 197 | return NULL; |
| 198 | |
| 199 | memcpy(skb_put(pdu, tlv_length), tlv, tlv_length); |
| 200 | |
| 201 | return pdu; |
| 202 | } |
| 203 | |
| 204 | static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 205 | u8 cmd, u16 size) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 206 | { |
| 207 | struct sk_buff *skb; |
| 208 | int err; |
| 209 | |
| 210 | if (sock->ssap == 0) |
| 211 | return NULL; |
| 212 | |
| 213 | skb = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 214 | size + LLCP_HEADER_SIZE, &err); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 215 | if (skb == NULL) { |
| 216 | pr_err("Could not allocate PDU\n"); |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | skb = llcp_add_header(skb, sock->dsap, sock->ssap, cmd); |
| 221 | |
| 222 | return skb; |
| 223 | } |
| 224 | |
| 225 | int nfc_llcp_disconnect(struct nfc_llcp_sock *sock) |
| 226 | { |
| 227 | struct sk_buff *skb; |
| 228 | struct nfc_dev *dev; |
| 229 | struct nfc_llcp_local *local; |
| 230 | u16 size = 0; |
| 231 | |
| 232 | pr_debug("Sending DISC\n"); |
| 233 | |
| 234 | local = sock->local; |
| 235 | if (local == NULL) |
| 236 | return -ENODEV; |
| 237 | |
| 238 | dev = sock->dev; |
| 239 | if (dev == NULL) |
| 240 | return -ENODEV; |
| 241 | |
| 242 | size += LLCP_HEADER_SIZE; |
| 243 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 244 | |
| 245 | skb = alloc_skb(size, GFP_ATOMIC); |
| 246 | if (skb == NULL) |
| 247 | return -ENOMEM; |
| 248 | |
| 249 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 250 | |
Samuel Ortiz | ffc2931 | 2012-04-10 19:43:16 +0200 | [diff] [blame] | 251 | skb = llcp_add_header(skb, sock->dsap, sock->ssap, LLCP_PDU_DISC); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 252 | |
| 253 | skb_queue_tail(&local->tx_queue, skb); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | int nfc_llcp_send_symm(struct nfc_dev *dev) |
| 259 | { |
| 260 | struct sk_buff *skb; |
| 261 | struct nfc_llcp_local *local; |
| 262 | u16 size = 0; |
| 263 | |
| 264 | pr_debug("Sending SYMM\n"); |
| 265 | |
| 266 | local = nfc_llcp_find_local(dev); |
| 267 | if (local == NULL) |
| 268 | return -ENODEV; |
| 269 | |
| 270 | size += LLCP_HEADER_SIZE; |
| 271 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 272 | |
| 273 | skb = alloc_skb(size, GFP_KERNEL); |
| 274 | if (skb == NULL) |
| 275 | return -ENOMEM; |
| 276 | |
| 277 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 278 | |
| 279 | skb = llcp_add_header(skb, 0, 0, LLCP_PDU_SYMM); |
| 280 | |
| 281 | return nfc_data_exchange(dev, local->target_idx, skb, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 282 | nfc_llcp_recv, local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | int nfc_llcp_send_connect(struct nfc_llcp_sock *sock) |
| 286 | { |
| 287 | struct nfc_llcp_local *local; |
| 288 | struct sk_buff *skb; |
| 289 | u8 *service_name_tlv = NULL, service_name_tlv_length; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 290 | u8 *miux_tlv = NULL, miux_tlv_length; |
| 291 | u8 *rw_tlv = NULL, rw_tlv_length, rw; |
| 292 | __be16 miux; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 293 | int err; |
| 294 | u16 size = 0; |
| 295 | |
| 296 | pr_debug("Sending CONNECT\n"); |
| 297 | |
| 298 | local = sock->local; |
| 299 | if (local == NULL) |
| 300 | return -ENODEV; |
| 301 | |
| 302 | if (sock->service_name != NULL) { |
| 303 | service_name_tlv = nfc_llcp_build_tlv(LLCP_TLV_SN, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 304 | sock->service_name, |
| 305 | sock->service_name_len, |
| 306 | &service_name_tlv_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 307 | size += service_name_tlv_length; |
| 308 | } |
| 309 | |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 310 | miux = cpu_to_be16(LLCP_MAX_MIUX); |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 311 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0, |
| 312 | &miux_tlv_length); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 313 | size += miux_tlv_length; |
| 314 | |
| 315 | rw = LLCP_MAX_RW; |
| 316 | rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length); |
| 317 | size += rw_tlv_length; |
| 318 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 319 | pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len); |
| 320 | |
| 321 | skb = llcp_allocate_pdu(sock, LLCP_PDU_CONNECT, size); |
| 322 | if (skb == NULL) { |
| 323 | err = -ENOMEM; |
| 324 | goto error_tlv; |
| 325 | } |
| 326 | |
| 327 | if (service_name_tlv != NULL) |
| 328 | skb = llcp_add_tlv(skb, service_name_tlv, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 329 | service_name_tlv_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 330 | |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 331 | skb = llcp_add_tlv(skb, miux_tlv, miux_tlv_length); |
| 332 | skb = llcp_add_tlv(skb, rw_tlv, rw_tlv_length); |
| 333 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 334 | skb_queue_tail(&local->tx_queue, skb); |
| 335 | |
| 336 | return 0; |
| 337 | |
| 338 | error_tlv: |
| 339 | pr_err("error %d\n", err); |
| 340 | |
| 341 | kfree(service_name_tlv); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 342 | kfree(miux_tlv); |
| 343 | kfree(rw_tlv); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 344 | |
| 345 | return err; |
| 346 | } |
| 347 | |
| 348 | int nfc_llcp_send_cc(struct nfc_llcp_sock *sock) |
| 349 | { |
| 350 | struct nfc_llcp_local *local; |
| 351 | struct sk_buff *skb; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 352 | u8 *miux_tlv = NULL, miux_tlv_length; |
| 353 | u8 *rw_tlv = NULL, rw_tlv_length, rw; |
| 354 | __be16 miux; |
| 355 | int err; |
| 356 | u16 size = 0; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 357 | |
| 358 | pr_debug("Sending CC\n"); |
| 359 | |
| 360 | local = sock->local; |
| 361 | if (local == NULL) |
| 362 | return -ENODEV; |
| 363 | |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 364 | miux = cpu_to_be16(LLCP_MAX_MIUX); |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 365 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0, |
| 366 | &miux_tlv_length); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 367 | size += miux_tlv_length; |
| 368 | |
| 369 | rw = LLCP_MAX_RW; |
| 370 | rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length); |
| 371 | size += rw_tlv_length; |
| 372 | |
| 373 | skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, size); |
| 374 | if (skb == NULL) { |
| 375 | err = -ENOMEM; |
| 376 | goto error_tlv; |
| 377 | } |
| 378 | |
| 379 | skb = llcp_add_tlv(skb, miux_tlv, miux_tlv_length); |
| 380 | skb = llcp_add_tlv(skb, rw_tlv, rw_tlv_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 381 | |
| 382 | skb_queue_tail(&local->tx_queue, skb); |
| 383 | |
| 384 | return 0; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 385 | |
| 386 | error_tlv: |
| 387 | pr_err("error %d\n", err); |
| 388 | |
| 389 | kfree(miux_tlv); |
| 390 | kfree(rw_tlv); |
| 391 | |
| 392 | return err; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason) |
| 396 | { |
| 397 | struct sk_buff *skb; |
| 398 | struct nfc_dev *dev; |
| 399 | u16 size = 1; /* Reason code */ |
| 400 | |
| 401 | pr_debug("Sending DM reason 0x%x\n", reason); |
| 402 | |
| 403 | if (local == NULL) |
| 404 | return -ENODEV; |
| 405 | |
| 406 | dev = local->dev; |
| 407 | if (dev == NULL) |
| 408 | return -ENODEV; |
| 409 | |
| 410 | size += LLCP_HEADER_SIZE; |
| 411 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 412 | |
| 413 | skb = alloc_skb(size, GFP_KERNEL); |
| 414 | if (skb == NULL) |
| 415 | return -ENOMEM; |
| 416 | |
| 417 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 418 | |
Samuel Ortiz | ffc2931 | 2012-04-10 19:43:16 +0200 | [diff] [blame] | 419 | skb = llcp_add_header(skb, dsap, ssap, LLCP_PDU_DM); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 420 | |
| 421 | memcpy(skb_put(skb, 1), &reason, 1); |
| 422 | |
| 423 | skb_queue_head(&local->tx_queue, skb); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock) |
| 429 | { |
| 430 | struct sk_buff *skb; |
| 431 | struct nfc_llcp_local *local; |
| 432 | |
| 433 | pr_debug("Send DISC\n"); |
| 434 | |
| 435 | local = sock->local; |
| 436 | if (local == NULL) |
| 437 | return -ENODEV; |
| 438 | |
| 439 | skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0); |
| 440 | if (skb == NULL) |
| 441 | return -ENOMEM; |
| 442 | |
| 443 | skb_queue_head(&local->tx_queue, skb); |
| 444 | |
| 445 | return 0; |
| 446 | } |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 447 | |
| 448 | int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 449 | struct msghdr *msg, size_t len) |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 450 | { |
| 451 | struct sk_buff *pdu; |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 452 | struct sock *sk = &sock->sk; |
| 453 | struct nfc_llcp_local *local; |
| 454 | size_t frag_len = 0, remaining_len; |
| 455 | u8 *msg_data, *msg_ptr; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 456 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 457 | pr_debug("Send I frame len %zd\n", len); |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 458 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 459 | local = sock->local; |
| 460 | if (local == NULL) |
| 461 | return -ENODEV; |
| 462 | |
| 463 | msg_data = kzalloc(len, GFP_KERNEL); |
| 464 | if (msg_data == NULL) |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 465 | return -ENOMEM; |
| 466 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 467 | if (memcpy_fromiovec(msg_data, msg->msg_iov, len)) { |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 468 | kfree(msg_data); |
| 469 | return -EFAULT; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 470 | } |
| 471 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 472 | remaining_len = len; |
| 473 | msg_ptr = msg_data; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 474 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 475 | while (remaining_len > 0) { |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 476 | |
Samuel Ortiz | b4838d1 | 2012-04-10 19:43:03 +0200 | [diff] [blame] | 477 | frag_len = min_t(size_t, local->remote_miu, remaining_len); |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 478 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 479 | pr_debug("Fragment %zd bytes remaining %zd", |
| 480 | frag_len, remaining_len); |
| 481 | |
| 482 | pdu = llcp_allocate_pdu(sock, LLCP_PDU_I, |
| 483 | frag_len + LLCP_SEQUENCE_SIZE); |
| 484 | if (pdu == NULL) |
| 485 | return -ENOMEM; |
| 486 | |
| 487 | skb_put(pdu, LLCP_SEQUENCE_SIZE); |
| 488 | |
| 489 | memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len); |
| 490 | |
Samuel Ortiz | bdbc59b | 2012-05-10 19:45:52 +0200 | [diff] [blame^] | 491 | skb_queue_tail(&sock->tx_queue, pdu); |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 492 | |
| 493 | lock_sock(sk); |
| 494 | |
| 495 | nfc_llcp_queue_i_frames(sock); |
| 496 | |
| 497 | release_sock(sk); |
| 498 | |
| 499 | remaining_len -= frag_len; |
Samuel Ortiz | b4838d1 | 2012-04-10 19:43:03 +0200 | [diff] [blame] | 500 | msg_ptr += frag_len; |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | kfree(msg_data); |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 504 | |
Samuel Ortiz | 43472ff | 2012-05-07 12:31:21 +0200 | [diff] [blame] | 505 | return len; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 506 | } |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 507 | |
| 508 | int nfc_llcp_send_rr(struct nfc_llcp_sock *sock) |
| 509 | { |
| 510 | struct sk_buff *skb; |
| 511 | struct nfc_llcp_local *local; |
| 512 | |
| 513 | pr_debug("Send rr nr %d\n", sock->recv_n); |
| 514 | |
| 515 | local = sock->local; |
| 516 | if (local == NULL) |
| 517 | return -ENODEV; |
| 518 | |
| 519 | skb = llcp_allocate_pdu(sock, LLCP_PDU_RR, LLCP_SEQUENCE_SIZE); |
| 520 | if (skb == NULL) |
| 521 | return -ENOMEM; |
| 522 | |
| 523 | skb_put(skb, LLCP_SEQUENCE_SIZE); |
| 524 | |
Samuel Ortiz | 279cf17 | 2012-04-10 19:43:14 +0200 | [diff] [blame] | 525 | skb->data[2] = sock->recv_n; |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 526 | |
| 527 | skb_queue_head(&local->tx_queue, skb); |
| 528 | |
| 529 | return 0; |
| 530 | } |