xref: /openbmc/linux/net/bluetooth/bnep/sock.c (revision b9ccfda2)
1 /*
2    BNEP implementation for Linux Bluetooth stack (BlueZ).
3    Copyright (C) 2001-2002 Inventel Systemes
4    Written 2001-2002 by
5 	David Libault  <david.libault@inventel.fr>
6 
7    Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License version 2 as
11    published by the Free Software Foundation;
12 
13    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
16    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
17    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
18    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 
22    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
23    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
24    SOFTWARE IS DISCLAIMED.
25 */
26 
27 #include <linux/export.h>
28 #include <linux/file.h>
29 
30 #include "bnep.h"
31 
32 static int bnep_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 	sock_orphan(sk);
42 	sock_put(sk);
43 	return 0;
44 }
45 
46 static int bnep_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
47 {
48 	struct bnep_connlist_req cl;
49 	struct bnep_connadd_req  ca;
50 	struct bnep_conndel_req  cd;
51 	struct bnep_conninfo ci;
52 	struct socket *nsock;
53 	void __user *argp = (void __user *)arg;
54 	int err;
55 
56 	BT_DBG("cmd %x arg %lx", cmd, arg);
57 
58 	switch (cmd) {
59 	case BNEPCONNADD:
60 		if (!capable(CAP_NET_ADMIN))
61 			return -EACCES;
62 
63 		if (copy_from_user(&ca, argp, sizeof(ca)))
64 			return -EFAULT;
65 
66 		nsock = sockfd_lookup(ca.sock, &err);
67 		if (!nsock)
68 			return err;
69 
70 		if (nsock->sk->sk_state != BT_CONNECTED) {
71 			sockfd_put(nsock);
72 			return -EBADFD;
73 		}
74 		ca.device[sizeof(ca.device)-1] = 0;
75 
76 		err = bnep_add_connection(&ca, nsock);
77 		if (!err) {
78 			if (copy_to_user(argp, &ca, sizeof(ca)))
79 				err = -EFAULT;
80 		} else
81 			sockfd_put(nsock);
82 
83 		return err;
84 
85 	case BNEPCONNDEL:
86 		if (!capable(CAP_NET_ADMIN))
87 			return -EACCES;
88 
89 		if (copy_from_user(&cd, argp, sizeof(cd)))
90 			return -EFAULT;
91 
92 		return bnep_del_connection(&cd);
93 
94 	case BNEPGETCONNLIST:
95 		if (copy_from_user(&cl, argp, sizeof(cl)))
96 			return -EFAULT;
97 
98 		if (cl.cnum <= 0)
99 			return -EINVAL;
100 
101 		err = bnep_get_connlist(&cl);
102 		if (!err && copy_to_user(argp, &cl, sizeof(cl)))
103 			return -EFAULT;
104 
105 		return err;
106 
107 	case BNEPGETCONNINFO:
108 		if (copy_from_user(&ci, argp, sizeof(ci)))
109 			return -EFAULT;
110 
111 		err = bnep_get_conninfo(&ci);
112 		if (!err && copy_to_user(argp, &ci, sizeof(ci)))
113 			return -EFAULT;
114 
115 		return err;
116 
117 	default:
118 		return -EINVAL;
119 	}
120 
121 	return 0;
122 }
123 
124 #ifdef CONFIG_COMPAT
125 static int bnep_sock_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
126 {
127 	if (cmd == BNEPGETCONNLIST) {
128 		struct bnep_connlist_req cl;
129 		u32 uci;
130 		int err;
131 
132 		if (get_user(cl.cnum, (u32 __user *) arg) ||
133 				get_user(uci, (u32 __user *) (arg + 4)))
134 			return -EFAULT;
135 
136 		cl.ci = compat_ptr(uci);
137 
138 		if (cl.cnum <= 0)
139 			return -EINVAL;
140 
141 		err = bnep_get_connlist(&cl);
142 
143 		if (!err && put_user(cl.cnum, (u32 __user *) arg))
144 			err = -EFAULT;
145 
146 		return err;
147 	}
148 
149 	return bnep_sock_ioctl(sock, cmd, arg);
150 }
151 #endif
152 
153 static const struct proto_ops bnep_sock_ops = {
154 	.family		= PF_BLUETOOTH,
155 	.owner		= THIS_MODULE,
156 	.release	= bnep_sock_release,
157 	.ioctl		= bnep_sock_ioctl,
158 #ifdef CONFIG_COMPAT
159 	.compat_ioctl	= bnep_sock_compat_ioctl,
160 #endif
161 	.bind		= sock_no_bind,
162 	.getname	= sock_no_getname,
163 	.sendmsg	= sock_no_sendmsg,
164 	.recvmsg	= sock_no_recvmsg,
165 	.poll		= sock_no_poll,
166 	.listen		= sock_no_listen,
167 	.shutdown	= sock_no_shutdown,
168 	.setsockopt	= sock_no_setsockopt,
169 	.getsockopt	= sock_no_getsockopt,
170 	.connect	= sock_no_connect,
171 	.socketpair	= sock_no_socketpair,
172 	.accept		= sock_no_accept,
173 	.mmap		= sock_no_mmap
174 };
175 
176 static struct proto bnep_proto = {
177 	.name		= "BNEP",
178 	.owner		= THIS_MODULE,
179 	.obj_size	= sizeof(struct bt_sock)
180 };
181 
182 static int bnep_sock_create(struct net *net, struct socket *sock, int protocol,
183 			    int kern)
184 {
185 	struct sock *sk;
186 
187 	BT_DBG("sock %p", sock);
188 
189 	if (sock->type != SOCK_RAW)
190 		return -ESOCKTNOSUPPORT;
191 
192 	sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &bnep_proto);
193 	if (!sk)
194 		return -ENOMEM;
195 
196 	sock_init_data(sock, sk);
197 
198 	sock->ops = &bnep_sock_ops;
199 
200 	sock->state = SS_UNCONNECTED;
201 
202 	sock_reset_flag(sk, SOCK_ZAPPED);
203 
204 	sk->sk_protocol = protocol;
205 	sk->sk_state	= BT_OPEN;
206 
207 	return 0;
208 }
209 
210 static const struct net_proto_family bnep_sock_family_ops = {
211 	.family = PF_BLUETOOTH,
212 	.owner	= THIS_MODULE,
213 	.create = bnep_sock_create
214 };
215 
216 int __init bnep_sock_init(void)
217 {
218 	int err;
219 
220 	err = proto_register(&bnep_proto, 0);
221 	if (err < 0)
222 		return err;
223 
224 	err = bt_sock_register(BTPROTO_BNEP, &bnep_sock_family_ops);
225 	if (err < 0)
226 		goto error;
227 
228 	return 0;
229 
230 error:
231 	BT_ERR("Can't register BNEP socket");
232 	proto_unregister(&bnep_proto);
233 	return err;
234 }
235 
236 void __exit bnep_sock_cleanup(void)
237 {
238 	if (bt_sock_unregister(BTPROTO_BNEP) < 0)
239 		BT_ERR("Can't unregister BNEP socket");
240 
241 	proto_unregister(&bnep_proto);
242 }
243