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> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 12 | #include <linux/sched/signal.h> |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 13 | |
Samuel Ortiz | 30cc458 | 2013-04-26 11:49:40 +0200 | [diff] [blame] | 14 | #include "nfc.h" |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 15 | #include "llcp.h" |
| 16 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 17 | static int sock_wait_state(struct sock *sk, int state, unsigned long timeo) |
| 18 | { |
| 19 | DECLARE_WAITQUEUE(wait, current); |
| 20 | int err = 0; |
| 21 | |
| 22 | pr_debug("sk %p", sk); |
| 23 | |
| 24 | add_wait_queue(sk_sleep(sk), &wait); |
| 25 | set_current_state(TASK_INTERRUPTIBLE); |
| 26 | |
| 27 | while (sk->sk_state != state) { |
| 28 | if (!timeo) { |
| 29 | err = -EINPROGRESS; |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | if (signal_pending(current)) { |
| 34 | err = sock_intr_errno(timeo); |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | release_sock(sk); |
| 39 | timeo = schedule_timeout(timeo); |
| 40 | lock_sock(sk); |
| 41 | set_current_state(TASK_INTERRUPTIBLE); |
| 42 | |
| 43 | err = sock_error(sk); |
| 44 | if (err) |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | __set_current_state(TASK_RUNNING); |
| 49 | remove_wait_queue(sk_sleep(sk), &wait); |
| 50 | return err; |
| 51 | } |
| 52 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 53 | static struct proto llcp_sock_proto = { |
| 54 | .name = "NFC_LLCP", |
| 55 | .owner = THIS_MODULE, |
| 56 | .obj_size = sizeof(struct nfc_llcp_sock), |
| 57 | }; |
| 58 | |
| 59 | static int llcp_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) |
| 60 | { |
| 61 | struct sock *sk = sock->sk; |
| 62 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 63 | struct nfc_llcp_local *local; |
| 64 | struct nfc_dev *dev; |
| 65 | struct sockaddr_nfc_llcp llcp_addr; |
| 66 | int len, ret = 0; |
| 67 | |
Mateusz Jurczyk | f6a5885 | 2017-06-13 18:44:28 +0200 | [diff] [blame] | 68 | if (!addr || alen < offsetofend(struct sockaddr, sa_family) || |
| 69 | addr->sa_family != AF_NFC) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 70 | return -EINVAL; |
| 71 | |
Samuel Ortiz | c66433d | 2012-06-29 12:03:55 +0200 | [diff] [blame] | 72 | pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family); |
| 73 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 74 | memset(&llcp_addr, 0, sizeof(llcp_addr)); |
| 75 | len = min_t(unsigned int, sizeof(llcp_addr), alen); |
| 76 | memcpy(&llcp_addr, addr, len); |
| 77 | |
| 78 | /* This is going to be a listening socket, dsap must be 0 */ |
| 79 | if (llcp_addr.dsap != 0) |
| 80 | return -EINVAL; |
| 81 | |
| 82 | lock_sock(sk); |
| 83 | |
| 84 | if (sk->sk_state != LLCP_CLOSED) { |
| 85 | ret = -EBADFD; |
| 86 | goto error; |
| 87 | } |
| 88 | |
| 89 | dev = nfc_get_device(llcp_addr.dev_idx); |
| 90 | if (dev == NULL) { |
| 91 | ret = -ENODEV; |
| 92 | goto error; |
| 93 | } |
| 94 | |
| 95 | local = nfc_llcp_find_local(dev); |
| 96 | if (local == NULL) { |
| 97 | ret = -ENODEV; |
| 98 | goto put_dev; |
| 99 | } |
| 100 | |
| 101 | llcp_sock->dev = dev; |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 102 | llcp_sock->local = nfc_llcp_local_get(local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 103 | llcp_sock->nfc_protocol = llcp_addr.nfc_protocol; |
| 104 | llcp_sock->service_name_len = min_t(unsigned int, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 105 | llcp_addr.service_name_len, |
| 106 | NFC_LLCP_MAX_SERVICE_NAME); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 107 | llcp_sock->service_name = kmemdup(llcp_addr.service_name, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 108 | llcp_sock->service_name_len, |
| 109 | GFP_KERNEL); |
Eric Dumazet | a0c2dc1 | 2019-10-04 11:08:34 -0700 | [diff] [blame] | 110 | if (!llcp_sock->service_name) { |
Xiaoming Ni | c33b1cc | 2021-03-25 11:51:10 +0800 | [diff] [blame] | 111 | nfc_llcp_local_put(llcp_sock->local); |
Or Cohen | c61760e | 2021-05-04 10:16:46 +0300 | [diff] [blame] | 112 | llcp_sock->local = NULL; |
Krzysztof Kozlowski | 4ac06a1 | 2021-05-31 09:21:38 +0200 | [diff] [blame^] | 113 | llcp_sock->dev = NULL; |
Eric Dumazet | a0c2dc1 | 2019-10-04 11:08:34 -0700 | [diff] [blame] | 114 | ret = -ENOMEM; |
| 115 | goto put_dev; |
| 116 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 117 | llcp_sock->ssap = nfc_llcp_get_sdp_ssap(local, llcp_sock); |
Samuel Ortiz | 8b7e8ed | 2012-06-22 15:32:20 +0200 | [diff] [blame] | 118 | if (llcp_sock->ssap == LLCP_SAP_MAX) { |
Xiaoming Ni | c33b1cc | 2021-03-25 11:51:10 +0800 | [diff] [blame] | 119 | nfc_llcp_local_put(llcp_sock->local); |
Or Cohen | c61760e | 2021-05-04 10:16:46 +0300 | [diff] [blame] | 120 | llcp_sock->local = NULL; |
Eric Dumazet | a0c2dc1 | 2019-10-04 11:08:34 -0700 | [diff] [blame] | 121 | kfree(llcp_sock->service_name); |
| 122 | llcp_sock->service_name = NULL; |
Krzysztof Kozlowski | 4ac06a1 | 2021-05-31 09:21:38 +0200 | [diff] [blame^] | 123 | llcp_sock->dev = NULL; |
Samuel Ortiz | 8b7e8ed | 2012-06-22 15:32:20 +0200 | [diff] [blame] | 124 | ret = -EADDRINUSE; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 125 | goto put_dev; |
Samuel Ortiz | 8b7e8ed | 2012-06-22 15:32:20 +0200 | [diff] [blame] | 126 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 127 | |
Samuel Ortiz | cbbf472 | 2012-06-22 14:48:11 +0200 | [diff] [blame] | 128 | llcp_sock->reserved_ssap = llcp_sock->ssap; |
| 129 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 130 | nfc_llcp_sock_link(&local->sockets, sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 131 | |
| 132 | pr_debug("Socket bound to SAP %d\n", llcp_sock->ssap); |
| 133 | |
| 134 | sk->sk_state = LLCP_BOUND; |
| 135 | |
| 136 | put_dev: |
| 137 | nfc_put_device(dev); |
| 138 | |
| 139 | error: |
| 140 | release_sock(sk); |
| 141 | return ret; |
| 142 | } |
| 143 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 144 | static int llcp_raw_sock_bind(struct socket *sock, struct sockaddr *addr, |
| 145 | int alen) |
| 146 | { |
| 147 | struct sock *sk = sock->sk; |
| 148 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 149 | struct nfc_llcp_local *local; |
| 150 | struct nfc_dev *dev; |
| 151 | struct sockaddr_nfc_llcp llcp_addr; |
| 152 | int len, ret = 0; |
| 153 | |
Mateusz Jurczyk | f6a5885 | 2017-06-13 18:44:28 +0200 | [diff] [blame] | 154 | if (!addr || alen < offsetofend(struct sockaddr, sa_family) || |
| 155 | addr->sa_family != AF_NFC) |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 156 | return -EINVAL; |
| 157 | |
| 158 | pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family); |
| 159 | |
| 160 | memset(&llcp_addr, 0, sizeof(llcp_addr)); |
| 161 | len = min_t(unsigned int, sizeof(llcp_addr), alen); |
| 162 | memcpy(&llcp_addr, addr, len); |
| 163 | |
| 164 | lock_sock(sk); |
| 165 | |
| 166 | if (sk->sk_state != LLCP_CLOSED) { |
| 167 | ret = -EBADFD; |
| 168 | goto error; |
| 169 | } |
| 170 | |
| 171 | dev = nfc_get_device(llcp_addr.dev_idx); |
| 172 | if (dev == NULL) { |
| 173 | ret = -ENODEV; |
| 174 | goto error; |
| 175 | } |
| 176 | |
| 177 | local = nfc_llcp_find_local(dev); |
| 178 | if (local == NULL) { |
| 179 | ret = -ENODEV; |
| 180 | goto put_dev; |
| 181 | } |
| 182 | |
| 183 | llcp_sock->dev = dev; |
| 184 | llcp_sock->local = nfc_llcp_local_get(local); |
| 185 | llcp_sock->nfc_protocol = llcp_addr.nfc_protocol; |
| 186 | |
| 187 | nfc_llcp_sock_link(&local->raw_sockets, sk); |
| 188 | |
| 189 | sk->sk_state = LLCP_BOUND; |
| 190 | |
| 191 | put_dev: |
| 192 | nfc_put_device(dev); |
| 193 | |
| 194 | error: |
| 195 | release_sock(sk); |
| 196 | return ret; |
| 197 | } |
| 198 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 199 | static int llcp_sock_listen(struct socket *sock, int backlog) |
| 200 | { |
| 201 | struct sock *sk = sock->sk; |
| 202 | int ret = 0; |
| 203 | |
| 204 | pr_debug("sk %p backlog %d\n", sk, backlog); |
| 205 | |
| 206 | lock_sock(sk); |
| 207 | |
Szymon Janc | 874934f | 2012-10-04 15:15:51 +0200 | [diff] [blame] | 208 | if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) || |
| 209 | sk->sk_state != LLCP_BOUND) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 210 | ret = -EBADFD; |
| 211 | goto error; |
| 212 | } |
| 213 | |
| 214 | sk->sk_max_ack_backlog = backlog; |
| 215 | sk->sk_ack_backlog = 0; |
| 216 | |
| 217 | pr_debug("Socket listening\n"); |
| 218 | sk->sk_state = LLCP_LISTEN; |
| 219 | |
| 220 | error: |
| 221 | release_sock(sk); |
| 222 | |
| 223 | return ret; |
| 224 | } |
| 225 | |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 226 | static int nfc_llcp_setsockopt(struct socket *sock, int level, int optname, |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 227 | sockptr_t optval, unsigned int optlen) |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 228 | { |
| 229 | struct sock *sk = sock->sk; |
| 230 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 231 | u32 opt; |
| 232 | int err = 0; |
| 233 | |
| 234 | pr_debug("%p optname %d\n", sk, optname); |
| 235 | |
| 236 | if (level != SOL_NFC) |
| 237 | return -ENOPROTOOPT; |
| 238 | |
| 239 | lock_sock(sk); |
| 240 | |
| 241 | switch (optname) { |
| 242 | case NFC_LLCP_RW: |
| 243 | if (sk->sk_state == LLCP_CONNECTED || |
| 244 | sk->sk_state == LLCP_BOUND || |
| 245 | sk->sk_state == LLCP_LISTEN) { |
| 246 | err = -EINVAL; |
| 247 | break; |
| 248 | } |
| 249 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 250 | if (copy_from_sockptr(&opt, optval, sizeof(u32))) { |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 251 | err = -EFAULT; |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | if (opt > LLCP_MAX_RW) { |
| 256 | err = -EINVAL; |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | llcp_sock->rw = (u8) opt; |
| 261 | |
| 262 | break; |
| 263 | |
| 264 | case NFC_LLCP_MIUX: |
| 265 | if (sk->sk_state == LLCP_CONNECTED || |
| 266 | sk->sk_state == LLCP_BOUND || |
| 267 | sk->sk_state == LLCP_LISTEN) { |
| 268 | err = -EINVAL; |
| 269 | break; |
| 270 | } |
| 271 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 272 | if (copy_from_sockptr(&opt, optval, sizeof(u32))) { |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 273 | err = -EFAULT; |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | if (opt > LLCP_MAX_MIUX) { |
| 278 | err = -EINVAL; |
| 279 | break; |
| 280 | } |
| 281 | |
Samuel Ortiz | 5eef666 | 2013-03-20 16:06:12 +0100 | [diff] [blame] | 282 | llcp_sock->miux = cpu_to_be16((u16) opt); |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 283 | |
| 284 | break; |
| 285 | |
| 286 | default: |
| 287 | err = -ENOPROTOOPT; |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | release_sock(sk); |
| 292 | |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame] | 293 | pr_debug("%p rw %d miux %d\n", llcp_sock, |
| 294 | llcp_sock->rw, llcp_sock->miux); |
| 295 | |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 296 | return err; |
| 297 | } |
| 298 | |
| 299 | static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname, |
| 300 | char __user *optval, int __user *optlen) |
| 301 | { |
Samuel Ortiz | 00e856d | 2013-03-20 16:36:13 +0100 | [diff] [blame] | 302 | struct nfc_llcp_local *local; |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 303 | struct sock *sk = sock->sk; |
| 304 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 305 | int len, err = 0; |
Thierry Escande | 064f370 | 2013-04-02 10:25:16 +0200 | [diff] [blame] | 306 | u16 miux, remote_miu; |
Samuel Ortiz | 00e856d | 2013-03-20 16:36:13 +0100 | [diff] [blame] | 307 | u8 rw; |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 308 | |
| 309 | pr_debug("%p optname %d\n", sk, optname); |
| 310 | |
| 311 | if (level != SOL_NFC) |
| 312 | return -ENOPROTOOPT; |
| 313 | |
| 314 | if (get_user(len, optlen)) |
| 315 | return -EFAULT; |
| 316 | |
Samuel Ortiz | 00e856d | 2013-03-20 16:36:13 +0100 | [diff] [blame] | 317 | local = llcp_sock->local; |
| 318 | if (!local) |
| 319 | return -ENODEV; |
| 320 | |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 321 | len = min_t(u32, len, sizeof(u32)); |
| 322 | |
| 323 | lock_sock(sk); |
| 324 | |
| 325 | switch (optname) { |
| 326 | case NFC_LLCP_RW: |
Samuel Ortiz | 00e856d | 2013-03-20 16:36:13 +0100 | [diff] [blame] | 327 | rw = llcp_sock->rw > LLCP_MAX_RW ? local->rw : llcp_sock->rw; |
| 328 | if (put_user(rw, (u32 __user *) optval)) |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 329 | err = -EFAULT; |
| 330 | |
| 331 | break; |
| 332 | |
| 333 | case NFC_LLCP_MIUX: |
Samuel Ortiz | 00e856d | 2013-03-20 16:36:13 +0100 | [diff] [blame] | 334 | miux = be16_to_cpu(llcp_sock->miux) > LLCP_MAX_MIUX ? |
| 335 | be16_to_cpu(local->miux) : be16_to_cpu(llcp_sock->miux); |
| 336 | |
| 337 | if (put_user(miux, (u32 __user *) optval)) |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 338 | err = -EFAULT; |
| 339 | |
| 340 | break; |
| 341 | |
Thierry Escande | 064f370 | 2013-04-02 10:25:16 +0200 | [diff] [blame] | 342 | case NFC_LLCP_REMOTE_MIU: |
| 343 | remote_miu = llcp_sock->remote_miu > LLCP_MAX_MIU ? |
| 344 | local->remote_miu : llcp_sock->remote_miu; |
| 345 | |
| 346 | if (put_user(remote_miu, (u32 __user *) optval)) |
| 347 | err = -EFAULT; |
| 348 | |
| 349 | break; |
| 350 | |
| 351 | case NFC_LLCP_REMOTE_LTO: |
| 352 | if (put_user(local->remote_lto / 10, (u32 __user *) optval)) |
| 353 | err = -EFAULT; |
| 354 | |
| 355 | break; |
| 356 | |
| 357 | case NFC_LLCP_REMOTE_RW: |
| 358 | if (put_user(llcp_sock->remote_rw, (u32 __user *) optval)) |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 359 | err = -EFAULT; |
| 360 | |
| 361 | break; |
| 362 | |
| 363 | default: |
| 364 | err = -ENOPROTOOPT; |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | release_sock(sk); |
| 369 | |
| 370 | if (put_user(len, optlen)) |
| 371 | return -EFAULT; |
| 372 | |
| 373 | return err; |
| 374 | } |
| 375 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 376 | void nfc_llcp_accept_unlink(struct sock *sk) |
| 377 | { |
| 378 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 379 | |
| 380 | pr_debug("state %d\n", sk->sk_state); |
| 381 | |
| 382 | list_del_init(&llcp_sock->accept_queue); |
| 383 | sk_acceptq_removed(llcp_sock->parent); |
| 384 | llcp_sock->parent = NULL; |
| 385 | |
| 386 | sock_put(sk); |
| 387 | } |
| 388 | |
| 389 | void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk) |
| 390 | { |
| 391 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 392 | struct nfc_llcp_sock *llcp_sock_parent = nfc_llcp_sock(parent); |
| 393 | |
| 394 | /* Lock will be free from unlink */ |
| 395 | sock_hold(sk); |
| 396 | |
| 397 | list_add_tail(&llcp_sock->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 398 | &llcp_sock_parent->accept_queue); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 399 | llcp_sock->parent = parent; |
| 400 | sk_acceptq_added(parent); |
| 401 | } |
| 402 | |
| 403 | struct sock *nfc_llcp_accept_dequeue(struct sock *parent, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 404 | struct socket *newsock) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 405 | { |
| 406 | struct nfc_llcp_sock *lsk, *n, *llcp_parent; |
| 407 | struct sock *sk; |
| 408 | |
| 409 | llcp_parent = nfc_llcp_sock(parent); |
| 410 | |
| 411 | list_for_each_entry_safe(lsk, n, &llcp_parent->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 412 | accept_queue) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 413 | sk = &lsk->sk; |
| 414 | lock_sock(sk); |
| 415 | |
| 416 | if (sk->sk_state == LLCP_CLOSED) { |
| 417 | release_sock(sk); |
| 418 | nfc_llcp_accept_unlink(sk); |
| 419 | continue; |
| 420 | } |
| 421 | |
| 422 | if (sk->sk_state == LLCP_CONNECTED || !newsock) { |
Samuel Ortiz | 39a352a | 2013-03-26 14:35:57 +0100 | [diff] [blame] | 423 | list_del_init(&lsk->accept_queue); |
| 424 | sock_put(sk); |
| 425 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 426 | if (newsock) |
| 427 | sock_graft(sk, newsock); |
| 428 | |
| 429 | release_sock(sk); |
| 430 | |
| 431 | pr_debug("Returning sk state %d\n", sk->sk_state); |
| 432 | |
Samuel Ortiz | b141e81 | 2013-02-21 11:04:45 +0100 | [diff] [blame] | 433 | sk_acceptq_removed(parent); |
| 434 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 435 | return sk; |
| 436 | } |
| 437 | |
| 438 | release_sock(sk); |
| 439 | } |
| 440 | |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | static int llcp_sock_accept(struct socket *sock, struct socket *newsock, |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 445 | int flags, bool kern) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 446 | { |
| 447 | DECLARE_WAITQUEUE(wait, current); |
| 448 | struct sock *sk = sock->sk, *new_sk; |
| 449 | long timeo; |
| 450 | int ret = 0; |
| 451 | |
| 452 | pr_debug("parent %p\n", sk); |
| 453 | |
| 454 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 455 | |
| 456 | if (sk->sk_state != LLCP_LISTEN) { |
| 457 | ret = -EBADFD; |
| 458 | goto error; |
| 459 | } |
| 460 | |
| 461 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 462 | |
| 463 | /* Wait for an incoming connection. */ |
| 464 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
| 465 | while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) { |
| 466 | set_current_state(TASK_INTERRUPTIBLE); |
| 467 | |
| 468 | if (!timeo) { |
| 469 | ret = -EAGAIN; |
| 470 | break; |
| 471 | } |
| 472 | |
| 473 | if (signal_pending(current)) { |
| 474 | ret = sock_intr_errno(timeo); |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | release_sock(sk); |
| 479 | timeo = schedule_timeout(timeo); |
| 480 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 481 | } |
| 482 | __set_current_state(TASK_RUNNING); |
| 483 | remove_wait_queue(sk_sleep(sk), &wait); |
| 484 | |
| 485 | if (ret) |
| 486 | goto error; |
| 487 | |
| 488 | newsock->state = SS_CONNECTED; |
| 489 | |
| 490 | pr_debug("new socket %p\n", new_sk); |
| 491 | |
| 492 | error: |
| 493 | release_sock(sk); |
| 494 | |
| 495 | return ret; |
| 496 | } |
| 497 | |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 498 | static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 499 | int peer) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 500 | { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 501 | struct sock *sk = sock->sk; |
| 502 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 503 | DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, llcp_addr, uaddr); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 504 | |
Samuel Ortiz | fe3c094 | 2012-07-05 17:43:08 +0200 | [diff] [blame] | 505 | if (llcp_sock == NULL || llcp_sock->dev == NULL) |
| 506 | return -EBADFD; |
| 507 | |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 508 | pr_debug("%p %d %d %d\n", sk, llcp_sock->target_idx, |
| 509 | llcp_sock->dsap, llcp_sock->ssap); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 510 | |
Dan Carpenter | 5ffedc6 | 2013-04-22 20:24:52 +0000 | [diff] [blame] | 511 | memset(llcp_addr, 0, sizeof(*llcp_addr)); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 512 | |
Cong Wang | 03c0535 | 2016-01-29 11:37:40 -0800 | [diff] [blame] | 513 | lock_sock(sk); |
| 514 | if (!llcp_sock->dev) { |
| 515 | release_sock(sk); |
| 516 | return -EBADFD; |
| 517 | } |
Dan Carpenter | 5ffedc6 | 2013-04-22 20:24:52 +0000 | [diff] [blame] | 518 | llcp_addr->sa_family = AF_NFC; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 519 | llcp_addr->dev_idx = llcp_sock->dev->idx; |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 520 | llcp_addr->target_idx = llcp_sock->target_idx; |
Dan Carpenter | 5ffedc6 | 2013-04-22 20:24:52 +0000 | [diff] [blame] | 521 | llcp_addr->nfc_protocol = llcp_sock->nfc_protocol; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 522 | llcp_addr->dsap = llcp_sock->dsap; |
| 523 | llcp_addr->ssap = llcp_sock->ssap; |
| 524 | llcp_addr->service_name_len = llcp_sock->service_name_len; |
| 525 | memcpy(llcp_addr->service_name, llcp_sock->service_name, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 526 | llcp_addr->service_name_len); |
Cong Wang | 03c0535 | 2016-01-29 11:37:40 -0800 | [diff] [blame] | 527 | release_sock(sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 528 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 529 | return sizeof(struct sockaddr_nfc_llcp); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Al Viro | ade994f | 2017-07-03 00:01:49 -0400 | [diff] [blame] | 532 | static inline __poll_t llcp_accept_poll(struct sock *parent) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 533 | { |
Axel Lin | 413df10 | 2014-11-06 18:20:41 +0800 | [diff] [blame] | 534 | struct nfc_llcp_sock *llcp_sock, *parent_sock; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 535 | struct sock *sk; |
| 536 | |
| 537 | parent_sock = nfc_llcp_sock(parent); |
| 538 | |
Axel Lin | 413df10 | 2014-11-06 18:20:41 +0800 | [diff] [blame] | 539 | list_for_each_entry(llcp_sock, &parent_sock->accept_queue, |
| 540 | accept_queue) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 541 | sk = &llcp_sock->sk; |
| 542 | |
| 543 | if (sk->sk_state == LLCP_CONNECTED) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 544 | return EPOLLIN | EPOLLRDNORM; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 550 | static __poll_t llcp_sock_poll(struct file *file, struct socket *sock, |
| 551 | poll_table *wait) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 552 | { |
| 553 | struct sock *sk = sock->sk; |
Al Viro | ade994f | 2017-07-03 00:01:49 -0400 | [diff] [blame] | 554 | __poll_t mask = 0; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 555 | |
| 556 | pr_debug("%p\n", sk); |
| 557 | |
Karsten Graul | 89ab066 | 2018-10-23 13:40:39 +0200 | [diff] [blame] | 558 | sock_poll_wait(file, sock, wait); |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 559 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 560 | if (sk->sk_state == LLCP_LISTEN) |
| 561 | return llcp_accept_poll(sk); |
| 562 | |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 563 | if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 564 | mask |= EPOLLERR | |
| 565 | (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 566 | |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 567 | if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 568 | mask |= EPOLLIN | EPOLLRDNORM; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 569 | |
| 570 | if (sk->sk_state == LLCP_CLOSED) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 571 | mask |= EPOLLHUP; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 572 | |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 573 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 574 | mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM; |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 575 | |
| 576 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 577 | mask |= EPOLLHUP; |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 578 | |
Samuel Ortiz | b401123 | 2013-05-03 18:29:30 +0200 | [diff] [blame] | 579 | if (sock_writeable(sk) && sk->sk_state == LLCP_CONNECTED) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 580 | mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 581 | else |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 582 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 583 | |
| 584 | pr_debug("mask 0x%x\n", mask); |
| 585 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 586 | return mask; |
| 587 | } |
| 588 | |
| 589 | static int llcp_sock_release(struct socket *sock) |
| 590 | { |
| 591 | struct sock *sk = sock->sk; |
| 592 | struct nfc_llcp_local *local; |
| 593 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 594 | int err = 0; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 595 | |
| 596 | if (!sk) |
| 597 | return 0; |
| 598 | |
| 599 | pr_debug("%p\n", sk); |
| 600 | |
| 601 | local = llcp_sock->local; |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 602 | if (local == NULL) { |
| 603 | err = -ENODEV; |
| 604 | goto out; |
| 605 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 606 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 607 | lock_sock(sk); |
| 608 | |
| 609 | /* Send a DISC */ |
| 610 | if (sk->sk_state == LLCP_CONNECTED) |
Thierry Escande | 58e3dd1 | 2013-06-04 11:34:50 +0200 | [diff] [blame] | 611 | nfc_llcp_send_disconnect(llcp_sock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 612 | |
| 613 | if (sk->sk_state == LLCP_LISTEN) { |
| 614 | struct nfc_llcp_sock *lsk, *n; |
| 615 | struct sock *accept_sk; |
| 616 | |
| 617 | list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 618 | accept_queue) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 619 | accept_sk = &lsk->sk; |
| 620 | lock_sock(accept_sk); |
| 621 | |
Thierry Escande | 58e3dd1 | 2013-06-04 11:34:50 +0200 | [diff] [blame] | 622 | nfc_llcp_send_disconnect(lsk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 623 | nfc_llcp_accept_unlink(accept_sk); |
| 624 | |
| 625 | release_sock(accept_sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
Samuel Ortiz | cbbf472 | 2012-06-22 14:48:11 +0200 | [diff] [blame] | 629 | if (llcp_sock->reserved_ssap < LLCP_SAP_MAX) |
| 630 | nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 631 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 632 | release_sock(sk); |
| 633 | |
Thierry Escande | 17f7ae16 | 2013-06-04 11:34:51 +0200 | [diff] [blame] | 634 | /* Keep this sock alive and therefore do not remove it from the sockets |
| 635 | * list until the DISC PDU has been actually sent. Otherwise we would |
| 636 | * reply with DM PDUs before sending the DISC one. |
| 637 | */ |
| 638 | if (sk->sk_state == LLCP_DISCONNECTING) |
| 639 | return err; |
| 640 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 641 | if (sock->type == SOCK_RAW) |
| 642 | nfc_llcp_sock_unlink(&local->raw_sockets, sk); |
| 643 | else |
| 644 | nfc_llcp_sock_unlink(&local->sockets, sk); |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 645 | |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 646 | out: |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 647 | sock_orphan(sk); |
| 648 | sock_put(sk); |
| 649 | |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 650 | return err; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 654 | int len, int flags) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 655 | { |
| 656 | struct sock *sk = sock->sk; |
| 657 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 658 | struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr; |
| 659 | struct nfc_dev *dev; |
| 660 | struct nfc_llcp_local *local; |
| 661 | int ret = 0; |
| 662 | |
| 663 | pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags); |
| 664 | |
Mateusz Jurczyk | 608c4ad | 2017-05-24 12:26:20 +0200 | [diff] [blame] | 665 | if (!addr || len < sizeof(*addr) || addr->sa_family != AF_NFC) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 666 | return -EINVAL; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 667 | |
Dave Jones | 32418cf | 2012-10-04 09:51:11 +0000 | [diff] [blame] | 668 | if (addr->service_name_len == 0 && addr->dsap == 0) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 669 | return -EINVAL; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 670 | |
| 671 | pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr->dev_idx, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 672 | addr->target_idx, addr->nfc_protocol); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 673 | |
| 674 | lock_sock(sk); |
| 675 | |
| 676 | if (sk->sk_state == LLCP_CONNECTED) { |
| 677 | ret = -EISCONN; |
| 678 | goto error; |
| 679 | } |
Xiaoming Ni | 4b5db93 | 2021-03-25 11:51:13 +0800 | [diff] [blame] | 680 | if (sk->sk_state == LLCP_CONNECTING) { |
| 681 | ret = -EINPROGRESS; |
| 682 | goto error; |
| 683 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 684 | |
| 685 | dev = nfc_get_device(addr->dev_idx); |
| 686 | if (dev == NULL) { |
| 687 | ret = -ENODEV; |
| 688 | goto error; |
| 689 | } |
| 690 | |
| 691 | local = nfc_llcp_find_local(dev); |
| 692 | if (local == NULL) { |
| 693 | ret = -ENODEV; |
| 694 | goto put_dev; |
| 695 | } |
| 696 | |
| 697 | device_lock(&dev->dev); |
| 698 | if (dev->dep_link_up == false) { |
| 699 | ret = -ENOLINK; |
| 700 | device_unlock(&dev->dev); |
| 701 | goto put_dev; |
| 702 | } |
| 703 | device_unlock(&dev->dev); |
| 704 | |
| 705 | if (local->rf_mode == NFC_RF_INITIATOR && |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 706 | addr->target_idx != local->target_idx) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 707 | ret = -ENOLINK; |
| 708 | goto put_dev; |
| 709 | } |
| 710 | |
| 711 | llcp_sock->dev = dev; |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 712 | llcp_sock->local = nfc_llcp_local_get(local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 713 | llcp_sock->ssap = nfc_llcp_get_local_ssap(local); |
| 714 | if (llcp_sock->ssap == LLCP_SAP_MAX) { |
Xiaoming Ni | 8a4cd82d | 2021-03-25 11:51:11 +0800 | [diff] [blame] | 715 | nfc_llcp_local_put(llcp_sock->local); |
Or Cohen | c61760e | 2021-05-04 10:16:46 +0300 | [diff] [blame] | 716 | llcp_sock->local = NULL; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 717 | ret = -ENOMEM; |
| 718 | goto put_dev; |
| 719 | } |
Samuel Ortiz | cbbf472 | 2012-06-22 14:48:11 +0200 | [diff] [blame] | 720 | |
| 721 | llcp_sock->reserved_ssap = llcp_sock->ssap; |
| 722 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 723 | if (addr->service_name_len == 0) |
| 724 | llcp_sock->dsap = addr->dsap; |
| 725 | else |
| 726 | llcp_sock->dsap = LLCP_SAP_SDP; |
| 727 | llcp_sock->nfc_protocol = addr->nfc_protocol; |
| 728 | llcp_sock->service_name_len = min_t(unsigned int, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 729 | addr->service_name_len, |
| 730 | NFC_LLCP_MAX_SERVICE_NAME); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 731 | llcp_sock->service_name = kmemdup(addr->service_name, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 732 | llcp_sock->service_name_len, |
| 733 | GFP_KERNEL); |
Aditya Pakki | d7737d4 | 2019-03-18 22:19:44 -0500 | [diff] [blame] | 734 | if (!llcp_sock->service_name) { |
| 735 | ret = -ENOMEM; |
| 736 | goto sock_llcp_release; |
| 737 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 738 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 739 | nfc_llcp_sock_link(&local->connecting_sockets, sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 740 | |
| 741 | ret = nfc_llcp_send_connect(llcp_sock); |
| 742 | if (ret) |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 743 | goto sock_unlink; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 744 | |
Samuel Ortiz | b401123 | 2013-05-03 18:29:30 +0200 | [diff] [blame] | 745 | sk->sk_state = LLCP_CONNECTING; |
| 746 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 747 | ret = sock_wait_state(sk, LLCP_CONNECTED, |
| 748 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
Samuel Ortiz | b401123 | 2013-05-03 18:29:30 +0200 | [diff] [blame] | 749 | if (ret && ret != -EINPROGRESS) |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 750 | goto sock_unlink; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 751 | |
| 752 | release_sock(sk); |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 753 | |
Samuel Ortiz | b401123 | 2013-05-03 18:29:30 +0200 | [diff] [blame] | 754 | return ret; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 755 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 756 | sock_unlink: |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 757 | nfc_llcp_sock_unlink(&local->connecting_sockets, sk); |
Xiaoming Ni | 7574fcd | 2021-03-25 11:51:12 +0800 | [diff] [blame] | 758 | kfree(llcp_sock->service_name); |
| 759 | llcp_sock->service_name = NULL; |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 760 | |
Aditya Pakki | d7737d4 | 2019-03-18 22:19:44 -0500 | [diff] [blame] | 761 | sock_llcp_release: |
| 762 | nfc_llcp_put_ssap(local, llcp_sock->ssap); |
Xiaoming Ni | 8a4cd82d | 2021-03-25 11:51:11 +0800 | [diff] [blame] | 763 | nfc_llcp_local_put(llcp_sock->local); |
Or Cohen | c61760e | 2021-05-04 10:16:46 +0300 | [diff] [blame] | 764 | llcp_sock->local = NULL; |
Aditya Pakki | d7737d4 | 2019-03-18 22:19:44 -0500 | [diff] [blame] | 765 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 766 | put_dev: |
| 767 | nfc_put_device(dev); |
| 768 | |
| 769 | error: |
| 770 | release_sock(sk); |
| 771 | return ret; |
| 772 | } |
| 773 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 774 | static int llcp_sock_sendmsg(struct socket *sock, struct msghdr *msg, |
| 775 | size_t len) |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 776 | { |
| 777 | struct sock *sk = sock->sk; |
| 778 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 779 | int ret; |
| 780 | |
| 781 | pr_debug("sock %p sk %p", sock, sk); |
| 782 | |
| 783 | ret = sock_error(sk); |
| 784 | if (ret) |
| 785 | return ret; |
| 786 | |
| 787 | if (msg->msg_flags & MSG_OOB) |
| 788 | return -EOPNOTSUPP; |
| 789 | |
| 790 | lock_sock(sk); |
| 791 | |
Samuel Ortiz | b874dec | 2012-10-16 15:04:10 +0200 | [diff] [blame] | 792 | if (sk->sk_type == SOCK_DGRAM) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 793 | DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, addr, |
| 794 | msg->msg_name); |
Samuel Ortiz | b874dec | 2012-10-16 15:04:10 +0200 | [diff] [blame] | 795 | |
| 796 | if (msg->msg_namelen < sizeof(*addr)) { |
| 797 | release_sock(sk); |
Samuel Ortiz | b874dec | 2012-10-16 15:04:10 +0200 | [diff] [blame] | 798 | return -EINVAL; |
| 799 | } |
| 800 | |
| 801 | release_sock(sk); |
| 802 | |
| 803 | return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap, |
| 804 | msg, len); |
| 805 | } |
| 806 | |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 807 | if (sk->sk_state != LLCP_CONNECTED) { |
| 808 | release_sock(sk); |
| 809 | return -ENOTCONN; |
| 810 | } |
| 811 | |
| 812 | release_sock(sk); |
| 813 | |
| 814 | return nfc_llcp_send_i_frame(llcp_sock, msg, len); |
| 815 | } |
| 816 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 817 | static int llcp_sock_recvmsg(struct socket *sock, struct msghdr *msg, |
| 818 | size_t len, int flags) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 819 | { |
| 820 | int noblock = flags & MSG_DONTWAIT; |
| 821 | struct sock *sk = sock->sk; |
| 822 | unsigned int copied, rlen; |
| 823 | struct sk_buff *skb, *cskb; |
| 824 | int err = 0; |
| 825 | |
| 826 | pr_debug("%p %zu\n", sk, len); |
| 827 | |
| 828 | lock_sock(sk); |
| 829 | |
| 830 | if (sk->sk_state == LLCP_CLOSED && |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 831 | skb_queue_empty(&sk->sk_receive_queue)) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 832 | release_sock(sk); |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | release_sock(sk); |
| 837 | |
| 838 | if (flags & (MSG_OOB)) |
| 839 | return -EOPNOTSUPP; |
| 840 | |
| 841 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 842 | if (!skb) { |
| 843 | pr_err("Recv datagram failed state %d %d %d", |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 844 | sk->sk_state, err, sock_error(sk)); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 845 | |
| 846 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 847 | return 0; |
| 848 | |
| 849 | return err; |
| 850 | } |
| 851 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 852 | rlen = skb->len; /* real length of skb */ |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 853 | copied = min_t(unsigned int, rlen, len); |
| 854 | |
| 855 | cskb = skb; |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 856 | if (skb_copy_datagram_msg(cskb, 0, msg, copied)) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 857 | if (!(flags & MSG_PEEK)) |
| 858 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 859 | return -EFAULT; |
| 860 | } |
| 861 | |
Thierry Escande | 2c2d45b | 2012-11-27 15:44:24 +0100 | [diff] [blame] | 862 | sock_recv_timestamp(msg, sk, skb); |
| 863 | |
Samuel Ortiz | 31ca61a | 2012-10-15 17:44:44 +0200 | [diff] [blame] | 864 | if (sk->sk_type == SOCK_DGRAM && msg->msg_name) { |
| 865 | struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb); |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 866 | DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, sockaddr, |
| 867 | msg->msg_name); |
Samuel Ortiz | fad2e37 | 2013-01-11 14:48:48 +0100 | [diff] [blame] | 868 | |
| 869 | msg->msg_namelen = sizeof(struct sockaddr_nfc_llcp); |
Samuel Ortiz | 31ca61a | 2012-10-15 17:44:44 +0200 | [diff] [blame] | 870 | |
| 871 | pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap); |
| 872 | |
Mathias Krause | d26d650 | 2013-04-07 01:51:58 +0000 | [diff] [blame] | 873 | memset(sockaddr, 0, sizeof(*sockaddr)); |
Samuel Ortiz | fad2e37 | 2013-01-11 14:48:48 +0100 | [diff] [blame] | 874 | sockaddr->sa_family = AF_NFC; |
| 875 | sockaddr->nfc_protocol = NFC_PROTO_NFC_DEP; |
| 876 | sockaddr->dsap = ui_cb->dsap; |
| 877 | sockaddr->ssap = ui_cb->ssap; |
Samuel Ortiz | 31ca61a | 2012-10-15 17:44:44 +0200 | [diff] [blame] | 878 | } |
| 879 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 880 | /* Mark read part of skb as used */ |
| 881 | if (!(flags & MSG_PEEK)) { |
| 882 | |
| 883 | /* SOCK_STREAM: re-queue skb if it contains unreceived data */ |
Samuel Ortiz | 31ca61a | 2012-10-15 17:44:44 +0200 | [diff] [blame] | 884 | if (sk->sk_type == SOCK_STREAM || |
| 885 | sk->sk_type == SOCK_DGRAM || |
| 886 | sk->sk_type == SOCK_RAW) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 887 | skb_pull(skb, copied); |
| 888 | if (skb->len) { |
| 889 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 890 | goto done; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | kfree_skb(skb); |
| 895 | } |
| 896 | |
| 897 | /* XXX Queue backlogged skbs */ |
| 898 | |
| 899 | done: |
| 900 | /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */ |
| 901 | if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC)) |
| 902 | copied = rlen; |
| 903 | |
| 904 | return copied; |
| 905 | } |
| 906 | |
| 907 | static const struct proto_ops llcp_sock_ops = { |
| 908 | .family = PF_NFC, |
| 909 | .owner = THIS_MODULE, |
| 910 | .bind = llcp_sock_bind, |
| 911 | .connect = llcp_sock_connect, |
| 912 | .release = llcp_sock_release, |
| 913 | .socketpair = sock_no_socketpair, |
| 914 | .accept = llcp_sock_accept, |
| 915 | .getname = llcp_sock_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 916 | .poll = llcp_sock_poll, |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 917 | .ioctl = sock_no_ioctl, |
| 918 | .listen = llcp_sock_listen, |
| 919 | .shutdown = sock_no_shutdown, |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 920 | .setsockopt = nfc_llcp_setsockopt, |
| 921 | .getsockopt = nfc_llcp_getsockopt, |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 922 | .sendmsg = llcp_sock_sendmsg, |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 923 | .recvmsg = llcp_sock_recvmsg, |
| 924 | .mmap = sock_no_mmap, |
| 925 | }; |
| 926 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 927 | static const struct proto_ops llcp_rawsock_ops = { |
| 928 | .family = PF_NFC, |
| 929 | .owner = THIS_MODULE, |
| 930 | .bind = llcp_raw_sock_bind, |
| 931 | .connect = sock_no_connect, |
| 932 | .release = llcp_sock_release, |
| 933 | .socketpair = sock_no_socketpair, |
| 934 | .accept = sock_no_accept, |
| 935 | .getname = llcp_sock_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 936 | .poll = llcp_sock_poll, |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 937 | .ioctl = sock_no_ioctl, |
| 938 | .listen = sock_no_listen, |
| 939 | .shutdown = sock_no_shutdown, |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 940 | .sendmsg = sock_no_sendmsg, |
| 941 | .recvmsg = llcp_sock_recvmsg, |
| 942 | .mmap = sock_no_mmap, |
| 943 | }; |
| 944 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 945 | static void llcp_sock_destruct(struct sock *sk) |
| 946 | { |
| 947 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 948 | |
| 949 | pr_debug("%p\n", sk); |
| 950 | |
| 951 | if (sk->sk_state == LLCP_CONNECTED) |
| 952 | nfc_put_device(llcp_sock->dev); |
| 953 | |
| 954 | skb_queue_purge(&sk->sk_receive_queue); |
| 955 | |
| 956 | nfc_llcp_sock_free(llcp_sock); |
| 957 | |
| 958 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 959 | pr_err("Freeing alive NFC LLCP socket %p\n", sk); |
| 960 | return; |
| 961 | } |
| 962 | } |
| 963 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 964 | struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp, int kern) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 965 | { |
| 966 | struct sock *sk; |
| 967 | struct nfc_llcp_sock *llcp_sock; |
| 968 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 969 | sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto, kern); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 970 | if (!sk) |
| 971 | return NULL; |
| 972 | |
| 973 | llcp_sock = nfc_llcp_sock(sk); |
| 974 | |
| 975 | sock_init_data(sock, sk); |
| 976 | sk->sk_state = LLCP_CLOSED; |
| 977 | sk->sk_protocol = NFC_SOCKPROTO_LLCP; |
| 978 | sk->sk_type = type; |
| 979 | sk->sk_destruct = llcp_sock_destruct; |
| 980 | |
| 981 | llcp_sock->ssap = 0; |
| 982 | llcp_sock->dsap = LLCP_SAP_SDP; |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame] | 983 | llcp_sock->rw = LLCP_MAX_RW + 1; |
Samuel Ortiz | 5eef666 | 2013-03-20 16:06:12 +0100 | [diff] [blame] | 984 | llcp_sock->miux = cpu_to_be16(LLCP_MAX_MIUX + 1); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 985 | llcp_sock->send_n = llcp_sock->send_ack_n = 0; |
| 986 | llcp_sock->recv_n = llcp_sock->recv_ack_n = 0; |
| 987 | llcp_sock->remote_ready = 1; |
Samuel Ortiz | cbbf472 | 2012-06-22 14:48:11 +0200 | [diff] [blame] | 988 | llcp_sock->reserved_ssap = LLCP_SAP_MAX; |
Thierry Escande | abd18d4 | 2013-04-02 10:25:15 +0200 | [diff] [blame] | 989 | nfc_llcp_socket_remote_param_init(llcp_sock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 990 | skb_queue_head_init(&llcp_sock->tx_queue); |
| 991 | skb_queue_head_init(&llcp_sock->tx_pending_queue); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 992 | INIT_LIST_HEAD(&llcp_sock->accept_queue); |
| 993 | |
| 994 | if (sock != NULL) |
| 995 | sock->state = SS_UNCONNECTED; |
| 996 | |
| 997 | return sk; |
| 998 | } |
| 999 | |
| 1000 | void nfc_llcp_sock_free(struct nfc_llcp_sock *sock) |
| 1001 | { |
| 1002 | kfree(sock->service_name); |
| 1003 | |
| 1004 | skb_queue_purge(&sock->tx_queue); |
| 1005 | skb_queue_purge(&sock->tx_pending_queue); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1006 | |
| 1007 | list_del_init(&sock->accept_queue); |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 1008 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1009 | sock->parent = NULL; |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 1010 | |
| 1011 | nfc_llcp_local_put(sock->local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | static int llcp_sock_create(struct net *net, struct socket *sock, |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 1015 | const struct nfc_protocol *nfc_proto, int kern) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1016 | { |
| 1017 | struct sock *sk; |
| 1018 | |
| 1019 | pr_debug("%p\n", sock); |
| 1020 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 1021 | if (sock->type != SOCK_STREAM && |
| 1022 | sock->type != SOCK_DGRAM && |
| 1023 | sock->type != SOCK_RAW) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1024 | return -ESOCKTNOSUPPORT; |
| 1025 | |
Ori Nimron | 3a35979 | 2019-09-20 09:35:49 +0200 | [diff] [blame] | 1026 | if (sock->type == SOCK_RAW) { |
| 1027 | if (!capable(CAP_NET_RAW)) |
| 1028 | return -EPERM; |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 1029 | sock->ops = &llcp_rawsock_ops; |
Ori Nimron | 3a35979 | 2019-09-20 09:35:49 +0200 | [diff] [blame] | 1030 | } else { |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 1031 | sock->ops = &llcp_sock_ops; |
Ori Nimron | 3a35979 | 2019-09-20 09:35:49 +0200 | [diff] [blame] | 1032 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1033 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 1034 | sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC, kern); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1035 | if (sk == NULL) |
| 1036 | return -ENOMEM; |
| 1037 | |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | static const struct nfc_protocol llcp_nfc_proto = { |
| 1042 | .id = NFC_SOCKPROTO_LLCP, |
| 1043 | .proto = &llcp_sock_proto, |
| 1044 | .owner = THIS_MODULE, |
| 1045 | .create = llcp_sock_create |
| 1046 | }; |
| 1047 | |
| 1048 | int __init nfc_llcp_sock_init(void) |
| 1049 | { |
| 1050 | return nfc_proto_register(&llcp_nfc_proto); |
| 1051 | } |
| 1052 | |
| 1053 | void nfc_llcp_sock_exit(void) |
| 1054 | { |
| 1055 | nfc_proto_unregister(&llcp_nfc_proto); |
| 1056 | } |