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