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