xref: /linux/net/bluetooth/hidp/sock.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2    HIDP implementation for Linux Bluetooth stack (BlueZ).
3    Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org>
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License version 2 as
7    published by the Free Software Foundation;
8 
9    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20    SOFTWARE IS DISCLAIMED.
21 */
22 
23 #include <linux/export.h>
24 #include <linux/file.h>
25 
26 #include "hidp.h"
27 
28 static struct bt_sock_list hidp_sk_list = {
29 	.lock = __RW_LOCK_UNLOCKED(hidp_sk_list.lock)
30 };
31 
32 static int hidp_sock_release(struct socket *sock)
33 {
34 	struct sock *sk = sock->sk;
35 
36 	BT_DBG("sock %p sk %p", sock, sk);
37 
38 	if (!sk)
39 		return 0;
40 
41 	bt_sock_unlink(&hidp_sk_list, sk);
42 
43 	sock_orphan(sk);
44 	sock_put(sk);
45 
46 	return 0;
47 }
48 
49 static int hidp_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
50 {
51 	void __user *argp = (void __user *) arg;
52 	struct hidp_connadd_req ca;
53 	struct hidp_conndel_req cd;
54 	struct hidp_connlist_req cl;
55 	struct hidp_conninfo ci;
56 	struct socket *csock;
57 	struct socket *isock;
58 	int err;
59 
60 	BT_DBG("cmd %x arg %lx", cmd, arg);
61 
62 	switch (cmd) {
63 	case HIDPCONNADD:
64 		if (!capable(CAP_NET_ADMIN))
65 			return -EPERM;
66 
67 		if (copy_from_user(&ca, argp, sizeof(ca)))
68 			return -EFAULT;
69 
70 		csock = sockfd_lookup(ca.ctrl_sock, &err);
71 		if (!csock)
72 			return err;
73 
74 		isock = sockfd_lookup(ca.intr_sock, &err);
75 		if (!isock) {
76 			sockfd_put(csock);
77 			return err;
78 		}
79 
80 		err = hidp_connection_add(&ca, csock, isock);
81 		if (!err && copy_to_user(argp, &ca, sizeof(ca)))
82 			err = -EFAULT;
83 
84 		sockfd_put(csock);
85 		sockfd_put(isock);
86 
87 		return err;
88 
89 	case HIDPCONNDEL:
90 		if (!capable(CAP_NET_ADMIN))
91 			return -EPERM;
92 
93 		if (copy_from_user(&cd, argp, sizeof(cd)))
94 			return -EFAULT;
95 
96 		return hidp_connection_del(&cd);
97 
98 	case HIDPGETCONNLIST:
99 		if (copy_from_user(&cl, argp, sizeof(cl)))
100 			return -EFAULT;
101 
102 		if (cl.cnum <= 0)
103 			return -EINVAL;
104 
105 		err = hidp_get_connlist(&cl);
106 		if (!err && copy_to_user(argp, &cl, sizeof(cl)))
107 			return -EFAULT;
108 
109 		return err;
110 
111 	case HIDPGETCONNINFO:
112 		if (copy_from_user(&ci, argp, sizeof(ci)))
113 			return -EFAULT;
114 
115 		err = hidp_get_conninfo(&ci);
116 		if (!err && copy_to_user(argp, &ci, sizeof(ci)))
117 			return -EFAULT;
118 
119 		return err;
120 	}
121 
122 	return -EINVAL;
123 }
124 
125 #ifdef CONFIG_COMPAT
126 struct compat_hidp_connadd_req {
127 	int   ctrl_sock;	/* Connected control socket */
128 	int   intr_sock;	/* Connected interrupt socket */
129 	__u16 parser;
130 	__u16 rd_size;
131 	compat_uptr_t rd_data;
132 	__u8  country;
133 	__u8  subclass;
134 	__u16 vendor;
135 	__u16 product;
136 	__u16 version;
137 	__u32 flags;
138 	__u32 idle_to;
139 	char  name[128];
140 };
141 
142 static int hidp_sock_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
143 {
144 	if (cmd == HIDPGETCONNLIST) {
145 		struct hidp_connlist_req cl;
146 		u32 uci;
147 		int err;
148 
149 		if (get_user(cl.cnum, (u32 __user *) arg) ||
150 				get_user(uci, (u32 __user *) (arg + 4)))
151 			return -EFAULT;
152 
153 		cl.ci = compat_ptr(uci);
154 
155 		if (cl.cnum <= 0)
156 			return -EINVAL;
157 
158 		err = hidp_get_connlist(&cl);
159 
160 		if (!err && put_user(cl.cnum, (u32 __user *) arg))
161 			err = -EFAULT;
162 
163 		return err;
164 	} else if (cmd == HIDPCONNADD) {
165 		struct compat_hidp_connadd_req ca;
166 		struct hidp_connadd_req __user *uca;
167 
168 		uca = compat_alloc_user_space(sizeof(*uca));
169 
170 		if (copy_from_user(&ca, (void __user *) arg, sizeof(ca)))
171 			return -EFAULT;
172 
173 		if (put_user(ca.ctrl_sock, &uca->ctrl_sock) ||
174 				put_user(ca.intr_sock, &uca->intr_sock) ||
175 				put_user(ca.parser, &uca->parser) ||
176 				put_user(ca.rd_size, &uca->rd_size) ||
177 				put_user(compat_ptr(ca.rd_data), &uca->rd_data) ||
178 				put_user(ca.country, &uca->country) ||
179 				put_user(ca.subclass, &uca->subclass) ||
180 				put_user(ca.vendor, &uca->vendor) ||
181 				put_user(ca.product, &uca->product) ||
182 				put_user(ca.version, &uca->version) ||
183 				put_user(ca.flags, &uca->flags) ||
184 				put_user(ca.idle_to, &uca->idle_to) ||
185 				copy_to_user(&uca->name[0], &ca.name[0], 128))
186 			return -EFAULT;
187 
188 		arg = (unsigned long) uca;
189 
190 		/* Fall through. We don't actually write back any _changes_
191 		   to the structure anyway, so there's no need to copy back
192 		   into the original compat version */
193 	}
194 
195 	return hidp_sock_ioctl(sock, cmd, arg);
196 }
197 #endif
198 
199 static const struct proto_ops hidp_sock_ops = {
200 	.family		= PF_BLUETOOTH,
201 	.owner		= THIS_MODULE,
202 	.release	= hidp_sock_release,
203 	.ioctl		= hidp_sock_ioctl,
204 #ifdef CONFIG_COMPAT
205 	.compat_ioctl	= hidp_sock_compat_ioctl,
206 #endif
207 	.bind		= sock_no_bind,
208 	.getname	= sock_no_getname,
209 	.sendmsg	= sock_no_sendmsg,
210 	.recvmsg	= sock_no_recvmsg,
211 	.poll		= sock_no_poll,
212 	.listen		= sock_no_listen,
213 	.shutdown	= sock_no_shutdown,
214 	.setsockopt	= sock_no_setsockopt,
215 	.getsockopt	= sock_no_getsockopt,
216 	.connect	= sock_no_connect,
217 	.socketpair	= sock_no_socketpair,
218 	.accept		= sock_no_accept,
219 	.mmap		= sock_no_mmap
220 };
221 
222 static struct proto hidp_proto = {
223 	.name		= "HIDP",
224 	.owner		= THIS_MODULE,
225 	.obj_size	= sizeof(struct bt_sock)
226 };
227 
228 static int hidp_sock_create(struct net *net, struct socket *sock, int protocol,
229 			    int kern)
230 {
231 	struct sock *sk;
232 
233 	BT_DBG("sock %p", sock);
234 
235 	if (sock->type != SOCK_RAW)
236 		return -ESOCKTNOSUPPORT;
237 
238 	sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hidp_proto, kern);
239 	if (!sk)
240 		return -ENOMEM;
241 
242 	sock_init_data(sock, sk);
243 
244 	sock->ops = &hidp_sock_ops;
245 
246 	sock->state = SS_UNCONNECTED;
247 
248 	sock_reset_flag(sk, SOCK_ZAPPED);
249 
250 	sk->sk_protocol = protocol;
251 	sk->sk_state	= BT_OPEN;
252 
253 	bt_sock_link(&hidp_sk_list, sk);
254 
255 	return 0;
256 }
257 
258 static const struct net_proto_family hidp_sock_family_ops = {
259 	.family	= PF_BLUETOOTH,
260 	.owner	= THIS_MODULE,
261 	.create	= hidp_sock_create
262 };
263 
264 int __init hidp_init_sockets(void)
265 {
266 	int err;
267 
268 	err = proto_register(&hidp_proto, 0);
269 	if (err < 0)
270 		return err;
271 
272 	err = bt_sock_register(BTPROTO_HIDP, &hidp_sock_family_ops);
273 	if (err < 0) {
274 		BT_ERR("Can't register HIDP socket");
275 		goto error;
276 	}
277 
278 	err = bt_procfs_init(&init_net, "hidp", &hidp_sk_list, NULL);
279 	if (err < 0) {
280 		BT_ERR("Failed to create HIDP proc file");
281 		bt_sock_unregister(BTPROTO_HIDP);
282 		goto error;
283 	}
284 
285 	BT_INFO("HIDP socket layer initialized");
286 
287 	return 0;
288 
289 error:
290 	proto_unregister(&hidp_proto);
291 	return err;
292 }
293 
294 void __exit hidp_cleanup_sockets(void)
295 {
296 	bt_procfs_cleanup(&init_net, "hidp");
297 	bt_sock_unregister(BTPROTO_HIDP);
298 	proto_unregister(&hidp_proto);
299 }
300