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