blob: 44835ceb5562ad37a5cc0f6fac21d5f3353978df [file] [log] [blame]
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001/*
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 Ortiz52858b52011-12-14 16:43:05 +010024#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080025
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -030026#include <net/tcp_states.h>
27#include <linux/nfc.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040028#include <linux/export.h>
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -030029
30#include "nfc.h"
31
32static void rawsock_write_queue_purge(struct sock *sk)
33{
Joe Perches20c239c2011-11-29 11:37:33 -080034 pr_debug("sk=%p\n", sk);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -030035
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
42static void rawsock_report_error(struct sock *sk, int err)
43{
Joe Perches20c239c2011-11-29 11:37:33 -080044 pr_debug("sk=%p err=%d\n", sk, err);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -030045
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
53static int rawsock_release(struct socket *sock)
54{
55 struct sock *sk = sock->sk;
56
Eric Dumazet55d07542012-06-12 00:47:58 +020057 pr_debug("sock=%p sk=%p\n", sock, sk);
58
59 if (!sk)
60 return 0;
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -030061
62 sock_orphan(sk);
63 sock_put(sk);
64
65 return 0;
66}
67
68static int rawsock_connect(struct socket *sock, struct sockaddr *_addr,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010069 int len, int flags)
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -030070{
71 struct sock *sk = sock->sk;
72 struct sockaddr_nfc *addr = (struct sockaddr_nfc *)_addr;
73 struct nfc_dev *dev;
74 int rc = 0;
75
Joe Perches20c239c2011-11-29 11:37:33 -080076 pr_debug("sock=%p sk=%p flags=%d\n", sock, sk, flags);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -030077
78 if (!addr || len < sizeof(struct sockaddr_nfc) ||
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010079 addr->sa_family != AF_NFC)
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -030080 return -EINVAL;
81
Joe Perches20c239c2011-11-29 11:37:33 -080082 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n",
83 addr->dev_idx, addr->target_idx, addr->nfc_protocol);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -030084
85 lock_sock(sk);
86
87 if (sock->state == SS_CONNECTED) {
88 rc = -EISCONN;
89 goto error;
90 }
91
92 dev = nfc_get_device(addr->dev_idx);
93 if (!dev) {
94 rc = -ENODEV;
95 goto error;
96 }
97
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -030098 rc = nfc_activate_target(dev, addr->target_idx, addr->nfc_protocol);
99 if (rc)
100 goto put_dev;
101
102 nfc_rawsock(sk)->dev = dev;
103 nfc_rawsock(sk)->target_idx = addr->target_idx;
104 sock->state = SS_CONNECTED;
105 sk->sk_state = TCP_ESTABLISHED;
106 sk->sk_state_change(sk);
107
108 release_sock(sk);
109 return 0;
110
111put_dev:
112 nfc_put_device(dev);
113error:
114 release_sock(sk);
115 return rc;
116}
117
118static int rawsock_add_header(struct sk_buff *skb)
119{
Samuel Ortize8753042011-08-19 15:47:11 +0200120 *skb_push(skb, NFC_HEADER_SIZE) = 0;
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300121
122 return 0;
123}
124
125static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100126 int err)
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300127{
128 struct sock *sk = (struct sock *) context;
129
130 BUG_ON(in_irq());
131
Joe Perches20c239c2011-11-29 11:37:33 -0800132 pr_debug("sk=%p err=%d\n", sk, err);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300133
134 if (err)
135 goto error;
136
137 err = rawsock_add_header(skb);
138 if (err)
139 goto error;
140
141 err = sock_queue_rcv_skb(sk, skb);
142 if (err)
143 goto error;
144
145 spin_lock_bh(&sk->sk_write_queue.lock);
146 if (!skb_queue_empty(&sk->sk_write_queue))
147 schedule_work(&nfc_rawsock(sk)->tx_work);
148 else
149 nfc_rawsock(sk)->tx_work_scheduled = false;
150 spin_unlock_bh(&sk->sk_write_queue.lock);
151
152 sock_put(sk);
153 return;
154
155error:
156 rawsock_report_error(sk, err);
157 sock_put(sk);
158}
159
160static void rawsock_tx_work(struct work_struct *work)
161{
162 struct sock *sk = to_rawsock_sk(work);
163 struct nfc_dev *dev = nfc_rawsock(sk)->dev;
164 u32 target_idx = nfc_rawsock(sk)->target_idx;
165 struct sk_buff *skb;
166 int rc;
167
Joe Perches20c239c2011-11-29 11:37:33 -0800168 pr_debug("sk=%p target_idx=%u\n", sk, target_idx);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300169
170 if (sk->sk_shutdown & SEND_SHUTDOWN) {
171 rawsock_write_queue_purge(sk);
172 return;
173 }
174
175 skb = skb_dequeue(&sk->sk_write_queue);
176
177 sock_hold(sk);
178 rc = nfc_data_exchange(dev, target_idx, skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100179 rawsock_data_exchange_complete, sk);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300180 if (rc) {
181 rawsock_report_error(sk, rc);
182 sock_put(sk);
183 }
184}
185
186static int rawsock_sendmsg(struct kiocb *iocb, struct socket *sock,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100187 struct msghdr *msg, size_t len)
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300188{
189 struct sock *sk = sock->sk;
Samuel Ortize8753042011-08-19 15:47:11 +0200190 struct nfc_dev *dev = nfc_rawsock(sk)->dev;
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300191 struct sk_buff *skb;
192 int rc;
193
Joe Perches20c239c2011-11-29 11:37:33 -0800194 pr_debug("sock=%p sk=%p len=%zu\n", sock, sk, len);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300195
196 if (msg->msg_namelen)
197 return -EOPNOTSUPP;
198
199 if (sock->state != SS_CONNECTED)
200 return -ENOTCONN;
201
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100202 skb = nfc_alloc_send_skb(dev, sk, msg->msg_flags, len, &rc);
203 if (skb == NULL)
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300204 return rc;
205
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300206 rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
207 if (rc < 0) {
208 kfree_skb(skb);
209 return rc;
210 }
211
212 spin_lock_bh(&sk->sk_write_queue.lock);
213 __skb_queue_tail(&sk->sk_write_queue, skb);
214 if (!nfc_rawsock(sk)->tx_work_scheduled) {
215 schedule_work(&nfc_rawsock(sk)->tx_work);
216 nfc_rawsock(sk)->tx_work_scheduled = true;
217 }
218 spin_unlock_bh(&sk->sk_write_queue.lock);
219
220 return len;
221}
222
223static int rawsock_recvmsg(struct kiocb *iocb, struct socket *sock,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100224 struct msghdr *msg, size_t len, int flags)
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300225{
226 int noblock = flags & MSG_DONTWAIT;
227 struct sock *sk = sock->sk;
228 struct sk_buff *skb;
229 int copied;
230 int rc;
231
Joe Perches20c239c2011-11-29 11:37:33 -0800232 pr_debug("sock=%p sk=%p len=%zu flags=%d\n", sock, sk, len, flags);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300233
234 skb = skb_recv_datagram(sk, flags, noblock, &rc);
235 if (!skb)
236 return rc;
237
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300238 copied = skb->len;
239 if (len < copied) {
240 msg->msg_flags |= MSG_TRUNC;
241 copied = len;
242 }
243
244 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
245
246 skb_free_datagram(sk, skb);
247
248 return rc ? : copied;
249}
250
251
252static const struct proto_ops rawsock_ops = {
253 .family = PF_NFC,
254 .owner = THIS_MODULE,
255 .release = rawsock_release,
256 .bind = sock_no_bind,
257 .connect = rawsock_connect,
258 .socketpair = sock_no_socketpair,
259 .accept = sock_no_accept,
260 .getname = sock_no_getname,
261 .poll = datagram_poll,
262 .ioctl = sock_no_ioctl,
263 .listen = sock_no_listen,
264 .shutdown = sock_no_shutdown,
265 .setsockopt = sock_no_setsockopt,
266 .getsockopt = sock_no_getsockopt,
267 .sendmsg = rawsock_sendmsg,
268 .recvmsg = rawsock_recvmsg,
269 .mmap = sock_no_mmap,
270};
271
272static void rawsock_destruct(struct sock *sk)
273{
Joe Perches20c239c2011-11-29 11:37:33 -0800274 pr_debug("sk=%p\n", sk);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300275
276 if (sk->sk_state == TCP_ESTABLISHED) {
277 nfc_deactivate_target(nfc_rawsock(sk)->dev,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100278 nfc_rawsock(sk)->target_idx);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300279 nfc_put_device(nfc_rawsock(sk)->dev);
280 }
281
282 skb_queue_purge(&sk->sk_receive_queue);
283
284 if (!sock_flag(sk, SOCK_DEAD)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800285 pr_err("Freeing alive NFC raw socket %p\n", sk);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300286 return;
287 }
288}
289
290static int rawsock_create(struct net *net, struct socket *sock,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100291 const struct nfc_protocol *nfc_proto)
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300292{
293 struct sock *sk;
294
Joe Perches20c239c2011-11-29 11:37:33 -0800295 pr_debug("sock=%p\n", sock);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300296
297 if (sock->type != SOCK_SEQPACKET)
298 return -ESOCKTNOSUPPORT;
299
300 sock->ops = &rawsock_ops;
301
Samuel Ortizdb81a622011-12-14 16:43:08 +0100302 sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300303 if (!sk)
304 return -ENOMEM;
305
306 sock_init_data(sock, sk);
307 sk->sk_protocol = nfc_proto->id;
308 sk->sk_destruct = rawsock_destruct;
309 sock->state = SS_UNCONNECTED;
310
311 INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
312 nfc_rawsock(sk)->tx_work_scheduled = false;
313
314 return 0;
315}
316
317static struct proto rawsock_proto = {
318 .name = "NFC_RAW",
319 .owner = THIS_MODULE,
320 .obj_size = sizeof(struct nfc_rawsock),
321};
322
323static const struct nfc_protocol rawsock_nfc_proto = {
324 .id = NFC_SOCKPROTO_RAW,
325 .proto = &rawsock_proto,
326 .owner = THIS_MODULE,
327 .create = rawsock_create
328};
329
330int __init rawsock_init(void)
331{
332 int rc;
333
334 rc = nfc_proto_register(&rawsock_nfc_proto);
335
336 return rc;
337}
338
339void rawsock_exit(void)
340{
341 nfc_proto_unregister(&rawsock_nfc_proto);
342}