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