Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Instituto Nokia de Tecnologia |
| 3 | * |
| 4 | * Authors: |
| 5 | * Aloisio Almeida Jr <aloisio.almeida@openbossa.org> |
| 6 | * Lauro Ramos Venancio <lauro.venancio@openbossa.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
Jeff Kirsher | 98b32de | 2013-12-06 08:56:16 -0800 | [diff] [blame] | 19 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 20 | */ |
| 21 | |
Samuel Ortiz | 52858b5 | 2011-12-14 16:43:05 +0100 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 23 | |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 24 | #include <net/tcp_states.h> |
| 25 | #include <linux/nfc.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 26 | #include <linux/export.h> |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 27 | |
| 28 | #include "nfc.h" |
| 29 | |
| 30 | static void rawsock_write_queue_purge(struct sock *sk) |
| 31 | { |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 32 | pr_debug("sk=%p\n", sk); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 33 | |
| 34 | spin_lock_bh(&sk->sk_write_queue.lock); |
| 35 | __skb_queue_purge(&sk->sk_write_queue); |
| 36 | nfc_rawsock(sk)->tx_work_scheduled = false; |
| 37 | spin_unlock_bh(&sk->sk_write_queue.lock); |
| 38 | } |
| 39 | |
| 40 | static void rawsock_report_error(struct sock *sk, int err) |
| 41 | { |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 42 | pr_debug("sk=%p err=%d\n", sk, err); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 43 | |
| 44 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 45 | sk->sk_err = -err; |
| 46 | sk->sk_error_report(sk); |
| 47 | |
| 48 | rawsock_write_queue_purge(sk); |
| 49 | } |
| 50 | |
| 51 | static int rawsock_release(struct socket *sock) |
| 52 | { |
| 53 | struct sock *sk = sock->sk; |
| 54 | |
Eric Dumazet | 03e934f | 2012-06-12 00:47:58 +0200 | [diff] [blame] | 55 | pr_debug("sock=%p sk=%p\n", sock, sk); |
| 56 | |
| 57 | if (!sk) |
| 58 | return 0; |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 59 | |
| 60 | sock_orphan(sk); |
| 61 | sock_put(sk); |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int rawsock_connect(struct socket *sock, struct sockaddr *_addr, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 67 | int len, int flags) |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 68 | { |
| 69 | struct sock *sk = sock->sk; |
| 70 | struct sockaddr_nfc *addr = (struct sockaddr_nfc *)_addr; |
| 71 | struct nfc_dev *dev; |
| 72 | int rc = 0; |
| 73 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 74 | pr_debug("sock=%p sk=%p flags=%d\n", sock, sk, flags); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 75 | |
| 76 | if (!addr || len < sizeof(struct sockaddr_nfc) || |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 77 | addr->sa_family != AF_NFC) |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 78 | return -EINVAL; |
| 79 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 80 | pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", |
| 81 | addr->dev_idx, addr->target_idx, addr->nfc_protocol); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 82 | |
| 83 | lock_sock(sk); |
| 84 | |
| 85 | if (sock->state == SS_CONNECTED) { |
| 86 | rc = -EISCONN; |
| 87 | goto error; |
| 88 | } |
| 89 | |
| 90 | dev = nfc_get_device(addr->dev_idx); |
| 91 | if (!dev) { |
| 92 | rc = -ENODEV; |
| 93 | goto error; |
| 94 | } |
| 95 | |
Eric Lapuyade | 01ae0ee | 2012-04-10 19:43:10 +0200 | [diff] [blame] | 96 | if (addr->target_idx > dev->target_next_idx - 1 || |
| 97 | addr->target_idx < dev->target_next_idx - dev->n_targets) { |
Samuel Ortiz | c4fbb65 | 2012-04-10 19:43:09 +0200 | [diff] [blame] | 98 | rc = -EINVAL; |
| 99 | goto error; |
| 100 | } |
| 101 | |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 102 | rc = nfc_activate_target(dev, addr->target_idx, addr->nfc_protocol); |
| 103 | if (rc) |
| 104 | goto put_dev; |
| 105 | |
| 106 | nfc_rawsock(sk)->dev = dev; |
| 107 | nfc_rawsock(sk)->target_idx = addr->target_idx; |
| 108 | sock->state = SS_CONNECTED; |
| 109 | sk->sk_state = TCP_ESTABLISHED; |
| 110 | sk->sk_state_change(sk); |
| 111 | |
| 112 | release_sock(sk); |
| 113 | return 0; |
| 114 | |
| 115 | put_dev: |
| 116 | nfc_put_device(dev); |
| 117 | error: |
| 118 | release_sock(sk); |
| 119 | return rc; |
| 120 | } |
| 121 | |
| 122 | static int rawsock_add_header(struct sk_buff *skb) |
| 123 | { |
Samuel Ortiz | e875304 | 2011-08-19 15:47:11 +0200 | [diff] [blame] | 124 | *skb_push(skb, NFC_HEADER_SIZE) = 0; |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 130 | int err) |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 131 | { |
| 132 | struct sock *sk = (struct sock *) context; |
| 133 | |
| 134 | BUG_ON(in_irq()); |
| 135 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 136 | pr_debug("sk=%p err=%d\n", sk, err); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 137 | |
| 138 | if (err) |
| 139 | goto error; |
| 140 | |
| 141 | err = rawsock_add_header(skb); |
| 142 | if (err) |
Thierry Escande | 4cf7e03 | 2013-07-10 10:55:37 +0200 | [diff] [blame] | 143 | goto error_skb; |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 144 | |
| 145 | err = sock_queue_rcv_skb(sk, skb); |
| 146 | if (err) |
Thierry Escande | 4cf7e03 | 2013-07-10 10:55:37 +0200 | [diff] [blame] | 147 | goto error_skb; |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 148 | |
| 149 | spin_lock_bh(&sk->sk_write_queue.lock); |
| 150 | if (!skb_queue_empty(&sk->sk_write_queue)) |
| 151 | schedule_work(&nfc_rawsock(sk)->tx_work); |
| 152 | else |
| 153 | nfc_rawsock(sk)->tx_work_scheduled = false; |
| 154 | spin_unlock_bh(&sk->sk_write_queue.lock); |
| 155 | |
| 156 | sock_put(sk); |
| 157 | return; |
| 158 | |
Thierry Escande | 4cf7e03 | 2013-07-10 10:55:37 +0200 | [diff] [blame] | 159 | error_skb: |
| 160 | kfree_skb(skb); |
| 161 | |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 162 | error: |
| 163 | rawsock_report_error(sk, err); |
| 164 | sock_put(sk); |
| 165 | } |
| 166 | |
| 167 | static void rawsock_tx_work(struct work_struct *work) |
| 168 | { |
| 169 | struct sock *sk = to_rawsock_sk(work); |
| 170 | struct nfc_dev *dev = nfc_rawsock(sk)->dev; |
| 171 | u32 target_idx = nfc_rawsock(sk)->target_idx; |
| 172 | struct sk_buff *skb; |
| 173 | int rc; |
| 174 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 175 | pr_debug("sk=%p target_idx=%u\n", sk, target_idx); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 176 | |
| 177 | if (sk->sk_shutdown & SEND_SHUTDOWN) { |
| 178 | rawsock_write_queue_purge(sk); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | skb = skb_dequeue(&sk->sk_write_queue); |
| 183 | |
| 184 | sock_hold(sk); |
| 185 | rc = nfc_data_exchange(dev, target_idx, skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 186 | rawsock_data_exchange_complete, sk); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 187 | if (rc) { |
| 188 | rawsock_report_error(sk, rc); |
| 189 | sock_put(sk); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | static int rawsock_sendmsg(struct kiocb *iocb, struct socket *sock, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 194 | struct msghdr *msg, size_t len) |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 195 | { |
| 196 | struct sock *sk = sock->sk; |
Samuel Ortiz | e875304 | 2011-08-19 15:47:11 +0200 | [diff] [blame] | 197 | struct nfc_dev *dev = nfc_rawsock(sk)->dev; |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 198 | struct sk_buff *skb; |
| 199 | int rc; |
| 200 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 201 | pr_debug("sock=%p sk=%p len=%zu\n", sock, sk, len); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 202 | |
| 203 | if (msg->msg_namelen) |
| 204 | return -EOPNOTSUPP; |
| 205 | |
| 206 | if (sock->state != SS_CONNECTED) |
| 207 | return -ENOTCONN; |
| 208 | |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 209 | skb = nfc_alloc_send_skb(dev, sk, msg->msg_flags, len, &rc); |
| 210 | if (skb == NULL) |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 211 | return rc; |
| 212 | |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 213 | rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); |
| 214 | if (rc < 0) { |
| 215 | kfree_skb(skb); |
| 216 | return rc; |
| 217 | } |
| 218 | |
| 219 | spin_lock_bh(&sk->sk_write_queue.lock); |
| 220 | __skb_queue_tail(&sk->sk_write_queue, skb); |
| 221 | if (!nfc_rawsock(sk)->tx_work_scheduled) { |
| 222 | schedule_work(&nfc_rawsock(sk)->tx_work); |
| 223 | nfc_rawsock(sk)->tx_work_scheduled = true; |
| 224 | } |
| 225 | spin_unlock_bh(&sk->sk_write_queue.lock); |
| 226 | |
| 227 | return len; |
| 228 | } |
| 229 | |
| 230 | static int rawsock_recvmsg(struct kiocb *iocb, struct socket *sock, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 231 | struct msghdr *msg, size_t len, int flags) |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 232 | { |
| 233 | int noblock = flags & MSG_DONTWAIT; |
| 234 | struct sock *sk = sock->sk; |
| 235 | struct sk_buff *skb; |
| 236 | int copied; |
| 237 | int rc; |
| 238 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 239 | pr_debug("sock=%p sk=%p len=%zu flags=%d\n", sock, sk, len, flags); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 240 | |
| 241 | skb = skb_recv_datagram(sk, flags, noblock, &rc); |
| 242 | if (!skb) |
| 243 | return rc; |
| 244 | |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 245 | copied = skb->len; |
| 246 | if (len < copied) { |
| 247 | msg->msg_flags |= MSG_TRUNC; |
| 248 | copied = len; |
| 249 | } |
| 250 | |
| 251 | rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 252 | |
| 253 | skb_free_datagram(sk, skb); |
| 254 | |
| 255 | return rc ? : copied; |
| 256 | } |
| 257 | |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 258 | static const struct proto_ops rawsock_ops = { |
| 259 | .family = PF_NFC, |
| 260 | .owner = THIS_MODULE, |
| 261 | .release = rawsock_release, |
| 262 | .bind = sock_no_bind, |
| 263 | .connect = rawsock_connect, |
| 264 | .socketpair = sock_no_socketpair, |
| 265 | .accept = sock_no_accept, |
| 266 | .getname = sock_no_getname, |
| 267 | .poll = datagram_poll, |
| 268 | .ioctl = sock_no_ioctl, |
| 269 | .listen = sock_no_listen, |
| 270 | .shutdown = sock_no_shutdown, |
| 271 | .setsockopt = sock_no_setsockopt, |
| 272 | .getsockopt = sock_no_getsockopt, |
| 273 | .sendmsg = rawsock_sendmsg, |
| 274 | .recvmsg = rawsock_recvmsg, |
| 275 | .mmap = sock_no_mmap, |
| 276 | }; |
| 277 | |
| 278 | static void rawsock_destruct(struct sock *sk) |
| 279 | { |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 280 | pr_debug("sk=%p\n", sk); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 281 | |
| 282 | if (sk->sk_state == TCP_ESTABLISHED) { |
| 283 | nfc_deactivate_target(nfc_rawsock(sk)->dev, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 284 | nfc_rawsock(sk)->target_idx); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 285 | nfc_put_device(nfc_rawsock(sk)->dev); |
| 286 | } |
| 287 | |
| 288 | skb_queue_purge(&sk->sk_receive_queue); |
| 289 | |
| 290 | if (!sock_flag(sk, SOCK_DEAD)) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 291 | pr_err("Freeing alive NFC raw socket %p\n", sk); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | static int rawsock_create(struct net *net, struct socket *sock, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 297 | const struct nfc_protocol *nfc_proto) |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 298 | { |
| 299 | struct sock *sk; |
| 300 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 301 | pr_debug("sock=%p\n", sock); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 302 | |
| 303 | if (sock->type != SOCK_SEQPACKET) |
| 304 | return -ESOCKTNOSUPPORT; |
| 305 | |
| 306 | sock->ops = &rawsock_ops; |
| 307 | |
Samuel Ortiz | db81a62 | 2011-12-14 16:43:08 +0100 | [diff] [blame] | 308 | sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 309 | if (!sk) |
| 310 | return -ENOMEM; |
| 311 | |
| 312 | sock_init_data(sock, sk); |
| 313 | sk->sk_protocol = nfc_proto->id; |
| 314 | sk->sk_destruct = rawsock_destruct; |
| 315 | sock->state = SS_UNCONNECTED; |
| 316 | |
| 317 | INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work); |
| 318 | nfc_rawsock(sk)->tx_work_scheduled = false; |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static struct proto rawsock_proto = { |
| 324 | .name = "NFC_RAW", |
| 325 | .owner = THIS_MODULE, |
| 326 | .obj_size = sizeof(struct nfc_rawsock), |
| 327 | }; |
| 328 | |
| 329 | static const struct nfc_protocol rawsock_nfc_proto = { |
| 330 | .id = NFC_SOCKPROTO_RAW, |
| 331 | .proto = &rawsock_proto, |
| 332 | .owner = THIS_MODULE, |
| 333 | .create = rawsock_create |
| 334 | }; |
| 335 | |
| 336 | int __init rawsock_init(void) |
| 337 | { |
| 338 | int rc; |
| 339 | |
| 340 | rc = nfc_proto_register(&rawsock_nfc_proto); |
| 341 | |
| 342 | return rc; |
| 343 | } |
| 344 | |
| 345 | void rawsock_exit(void) |
| 346 | { |
| 347 | nfc_proto_unregister(&rawsock_nfc_proto); |
| 348 | } |