blob: 313bf1bc848a8c45b755e838fc9cd595e1a833c2 [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 Dumazet03e934f2012-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
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +020098 if (addr->target_idx > dev->target_next_idx - 1 ||
99 addr->target_idx < dev->target_next_idx - dev->n_targets) {
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200100 rc = -EINVAL;
101 goto error;
102 }
103
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300104 rc = nfc_activate_target(dev, addr->target_idx, addr->nfc_protocol);
105 if (rc)
106 goto put_dev;
107
108 nfc_rawsock(sk)->dev = dev;
109 nfc_rawsock(sk)->target_idx = addr->target_idx;
110 sock->state = SS_CONNECTED;
111 sk->sk_state = TCP_ESTABLISHED;
112 sk->sk_state_change(sk);
113
114 release_sock(sk);
115 return 0;
116
117put_dev:
118 nfc_put_device(dev);
119error:
120 release_sock(sk);
121 return rc;
122}
123
124static int rawsock_add_header(struct sk_buff *skb)
125{
Samuel Ortize8753042011-08-19 15:47:11 +0200126 *skb_push(skb, NFC_HEADER_SIZE) = 0;
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300127
128 return 0;
129}
130
131static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100132 int err)
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300133{
134 struct sock *sk = (struct sock *) context;
135
136 BUG_ON(in_irq());
137
Joe Perches20c239c2011-11-29 11:37:33 -0800138 pr_debug("sk=%p err=%d\n", sk, err);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300139
140 if (err)
141 goto error;
142
143 err = rawsock_add_header(skb);
144 if (err)
145 goto error;
146
147 err = sock_queue_rcv_skb(sk, skb);
148 if (err)
149 goto error;
150
151 spin_lock_bh(&sk->sk_write_queue.lock);
152 if (!skb_queue_empty(&sk->sk_write_queue))
153 schedule_work(&nfc_rawsock(sk)->tx_work);
154 else
155 nfc_rawsock(sk)->tx_work_scheduled = false;
156 spin_unlock_bh(&sk->sk_write_queue.lock);
157
158 sock_put(sk);
159 return;
160
161error:
162 rawsock_report_error(sk, err);
163 sock_put(sk);
164}
165
166static void rawsock_tx_work(struct work_struct *work)
167{
168 struct sock *sk = to_rawsock_sk(work);
169 struct nfc_dev *dev = nfc_rawsock(sk)->dev;
170 u32 target_idx = nfc_rawsock(sk)->target_idx;
171 struct sk_buff *skb;
172 int rc;
173
Joe Perches20c239c2011-11-29 11:37:33 -0800174 pr_debug("sk=%p target_idx=%u\n", sk, target_idx);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300175
176 if (sk->sk_shutdown & SEND_SHUTDOWN) {
177 rawsock_write_queue_purge(sk);
178 return;
179 }
180
181 skb = skb_dequeue(&sk->sk_write_queue);
182
183 sock_hold(sk);
184 rc = nfc_data_exchange(dev, target_idx, skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100185 rawsock_data_exchange_complete, sk);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300186 if (rc) {
187 rawsock_report_error(sk, rc);
188 sock_put(sk);
189 }
190}
191
192static int rawsock_sendmsg(struct kiocb *iocb, struct socket *sock,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100193 struct msghdr *msg, size_t len)
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300194{
195 struct sock *sk = sock->sk;
Samuel Ortize8753042011-08-19 15:47:11 +0200196 struct nfc_dev *dev = nfc_rawsock(sk)->dev;
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300197 struct sk_buff *skb;
198 int rc;
199
Joe Perches20c239c2011-11-29 11:37:33 -0800200 pr_debug("sock=%p sk=%p len=%zu\n", sock, sk, len);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300201
202 if (msg->msg_namelen)
203 return -EOPNOTSUPP;
204
205 if (sock->state != SS_CONNECTED)
206 return -ENOTCONN;
207
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100208 skb = nfc_alloc_send_skb(dev, sk, msg->msg_flags, len, &rc);
209 if (skb == NULL)
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300210 return rc;
211
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300212 rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
213 if (rc < 0) {
214 kfree_skb(skb);
215 return rc;
216 }
217
218 spin_lock_bh(&sk->sk_write_queue.lock);
219 __skb_queue_tail(&sk->sk_write_queue, skb);
220 if (!nfc_rawsock(sk)->tx_work_scheduled) {
221 schedule_work(&nfc_rawsock(sk)->tx_work);
222 nfc_rawsock(sk)->tx_work_scheduled = true;
223 }
224 spin_unlock_bh(&sk->sk_write_queue.lock);
225
226 return len;
227}
228
229static int rawsock_recvmsg(struct kiocb *iocb, struct socket *sock,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100230 struct msghdr *msg, size_t len, int flags)
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300231{
232 int noblock = flags & MSG_DONTWAIT;
233 struct sock *sk = sock->sk;
234 struct sk_buff *skb;
235 int copied;
236 int rc;
237
Joe Perches20c239c2011-11-29 11:37:33 -0800238 pr_debug("sock=%p sk=%p len=%zu flags=%d\n", sock, sk, len, flags);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300239
240 skb = skb_recv_datagram(sk, flags, noblock, &rc);
241 if (!skb)
242 return rc;
243
244 msg->msg_namelen = 0;
245
246 copied = skb->len;
247 if (len < copied) {
248 msg->msg_flags |= MSG_TRUNC;
249 copied = len;
250 }
251
252 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
253
254 skb_free_datagram(sk, skb);
255
256 return rc ? : copied;
257}
258
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300259static const struct proto_ops rawsock_ops = {
260 .family = PF_NFC,
261 .owner = THIS_MODULE,
262 .release = rawsock_release,
263 .bind = sock_no_bind,
264 .connect = rawsock_connect,
265 .socketpair = sock_no_socketpair,
266 .accept = sock_no_accept,
267 .getname = sock_no_getname,
268 .poll = datagram_poll,
269 .ioctl = sock_no_ioctl,
270 .listen = sock_no_listen,
271 .shutdown = sock_no_shutdown,
272 .setsockopt = sock_no_setsockopt,
273 .getsockopt = sock_no_getsockopt,
274 .sendmsg = rawsock_sendmsg,
275 .recvmsg = rawsock_recvmsg,
276 .mmap = sock_no_mmap,
277};
278
279static void rawsock_destruct(struct sock *sk)
280{
Joe Perches20c239c2011-11-29 11:37:33 -0800281 pr_debug("sk=%p\n", sk);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300282
283 if (sk->sk_state == TCP_ESTABLISHED) {
284 nfc_deactivate_target(nfc_rawsock(sk)->dev,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100285 nfc_rawsock(sk)->target_idx);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300286 nfc_put_device(nfc_rawsock(sk)->dev);
287 }
288
289 skb_queue_purge(&sk->sk_receive_queue);
290
291 if (!sock_flag(sk, SOCK_DEAD)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800292 pr_err("Freeing alive NFC raw socket %p\n", sk);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300293 return;
294 }
295}
296
297static int rawsock_create(struct net *net, struct socket *sock,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100298 const struct nfc_protocol *nfc_proto)
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300299{
300 struct sock *sk;
301
Joe Perches20c239c2011-11-29 11:37:33 -0800302 pr_debug("sock=%p\n", sock);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300303
304 if (sock->type != SOCK_SEQPACKET)
305 return -ESOCKTNOSUPPORT;
306
307 sock->ops = &rawsock_ops;
308
Samuel Ortizdb81a622011-12-14 16:43:08 +0100309 sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto);
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300310 if (!sk)
311 return -ENOMEM;
312
313 sock_init_data(sock, sk);
314 sk->sk_protocol = nfc_proto->id;
315 sk->sk_destruct = rawsock_destruct;
316 sock->state = SS_UNCONNECTED;
317
318 INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
319 nfc_rawsock(sk)->tx_work_scheduled = false;
320
321 return 0;
322}
323
324static struct proto rawsock_proto = {
325 .name = "NFC_RAW",
326 .owner = THIS_MODULE,
327 .obj_size = sizeof(struct nfc_rawsock),
328};
329
330static const struct nfc_protocol rawsock_nfc_proto = {
331 .id = NFC_SOCKPROTO_RAW,
332 .proto = &rawsock_proto,
333 .owner = THIS_MODULE,
334 .create = rawsock_create
335};
336
337int __init rawsock_init(void)
338{
339 int rc;
340
341 rc = nfc_proto_register(&rawsock_nfc_proto);
342
343 return rc;
344}
345
346void rawsock_exit(void)
347{
348 nfc_proto_unregister(&rawsock_nfc_proto);
349}