Linux 4.1.18
[linux/fpc-iii.git] / net / bluetooth / bnep / sock.c
blobbde2bdd9e929e854c9e2d001a7bf9e6a364d6a2c
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>
7 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
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;
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.
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.
27 #include <linux/export.h>
28 #include <linux/file.h>
30 #include "bnep.h"
32 static struct bt_sock_list bnep_sk_list = {
33 .lock = __RW_LOCK_UNLOCKED(bnep_sk_list.lock)
36 static int bnep_sock_release(struct socket *sock)
38 struct sock *sk = sock->sk;
40 BT_DBG("sock %p sk %p", sock, sk);
42 if (!sk)
43 return 0;
45 bt_sock_unlink(&bnep_sk_list, sk);
47 sock_orphan(sk);
48 sock_put(sk);
49 return 0;
52 static int bnep_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
54 struct bnep_connlist_req cl;
55 struct bnep_connadd_req ca;
56 struct bnep_conndel_req cd;
57 struct bnep_conninfo ci;
58 struct socket *nsock;
59 void __user *argp = (void __user *)arg;
60 __u32 supp_feat = BIT(BNEP_SETUP_RESPONSE);
61 int err;
63 BT_DBG("cmd %x arg %lx", cmd, arg);
65 switch (cmd) {
66 case BNEPCONNADD:
67 if (!capable(CAP_NET_ADMIN))
68 return -EPERM;
70 if (copy_from_user(&ca, argp, sizeof(ca)))
71 return -EFAULT;
73 nsock = sockfd_lookup(ca.sock, &err);
74 if (!nsock)
75 return err;
77 if (nsock->sk->sk_state != BT_CONNECTED) {
78 sockfd_put(nsock);
79 return -EBADFD;
81 ca.device[sizeof(ca.device)-1] = 0;
83 err = bnep_add_connection(&ca, nsock);
84 if (!err) {
85 if (copy_to_user(argp, &ca, sizeof(ca)))
86 err = -EFAULT;
87 } else
88 sockfd_put(nsock);
90 return err;
92 case BNEPCONNDEL:
93 if (!capable(CAP_NET_ADMIN))
94 return -EPERM;
96 if (copy_from_user(&cd, argp, sizeof(cd)))
97 return -EFAULT;
99 return bnep_del_connection(&cd);
101 case BNEPGETCONNLIST:
102 if (copy_from_user(&cl, argp, sizeof(cl)))
103 return -EFAULT;
105 if (cl.cnum <= 0)
106 return -EINVAL;
108 err = bnep_get_connlist(&cl);
109 if (!err && copy_to_user(argp, &cl, sizeof(cl)))
110 return -EFAULT;
112 return err;
114 case BNEPGETCONNINFO:
115 if (copy_from_user(&ci, argp, sizeof(ci)))
116 return -EFAULT;
118 err = bnep_get_conninfo(&ci);
119 if (!err && copy_to_user(argp, &ci, sizeof(ci)))
120 return -EFAULT;
122 return err;
124 case BNEPGETSUPPFEAT:
125 if (copy_to_user(argp, &supp_feat, sizeof(supp_feat)))
126 return -EFAULT;
128 return 0;
130 default:
131 return -EINVAL;
134 return 0;
137 #ifdef CONFIG_COMPAT
138 static int bnep_sock_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
140 if (cmd == BNEPGETCONNLIST) {
141 struct bnep_connlist_req cl;
142 u32 uci;
143 int err;
145 if (get_user(cl.cnum, (u32 __user *) arg) ||
146 get_user(uci, (u32 __user *) (arg + 4)))
147 return -EFAULT;
149 cl.ci = compat_ptr(uci);
151 if (cl.cnum <= 0)
152 return -EINVAL;
154 err = bnep_get_connlist(&cl);
156 if (!err && put_user(cl.cnum, (u32 __user *) arg))
157 err = -EFAULT;
159 return err;
162 return bnep_sock_ioctl(sock, cmd, arg);
164 #endif
166 static const struct proto_ops bnep_sock_ops = {
167 .family = PF_BLUETOOTH,
168 .owner = THIS_MODULE,
169 .release = bnep_sock_release,
170 .ioctl = bnep_sock_ioctl,
171 #ifdef CONFIG_COMPAT
172 .compat_ioctl = bnep_sock_compat_ioctl,
173 #endif
174 .bind = sock_no_bind,
175 .getname = sock_no_getname,
176 .sendmsg = sock_no_sendmsg,
177 .recvmsg = sock_no_recvmsg,
178 .poll = sock_no_poll,
179 .listen = sock_no_listen,
180 .shutdown = sock_no_shutdown,
181 .setsockopt = sock_no_setsockopt,
182 .getsockopt = sock_no_getsockopt,
183 .connect = sock_no_connect,
184 .socketpair = sock_no_socketpair,
185 .accept = sock_no_accept,
186 .mmap = sock_no_mmap
189 static struct proto bnep_proto = {
190 .name = "BNEP",
191 .owner = THIS_MODULE,
192 .obj_size = sizeof(struct bt_sock)
195 static int bnep_sock_create(struct net *net, struct socket *sock, int protocol,
196 int kern)
198 struct sock *sk;
200 BT_DBG("sock %p", sock);
202 if (sock->type != SOCK_RAW)
203 return -ESOCKTNOSUPPORT;
205 sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &bnep_proto);
206 if (!sk)
207 return -ENOMEM;
209 sock_init_data(sock, sk);
211 sock->ops = &bnep_sock_ops;
213 sock->state = SS_UNCONNECTED;
215 sock_reset_flag(sk, SOCK_ZAPPED);
217 sk->sk_protocol = protocol;
218 sk->sk_state = BT_OPEN;
220 bt_sock_link(&bnep_sk_list, sk);
221 return 0;
224 static const struct net_proto_family bnep_sock_family_ops = {
225 .family = PF_BLUETOOTH,
226 .owner = THIS_MODULE,
227 .create = bnep_sock_create
230 int __init bnep_sock_init(void)
232 int err;
234 err = proto_register(&bnep_proto, 0);
235 if (err < 0)
236 return err;
238 err = bt_sock_register(BTPROTO_BNEP, &bnep_sock_family_ops);
239 if (err < 0) {
240 BT_ERR("Can't register BNEP socket");
241 goto error;
244 err = bt_procfs_init(&init_net, "bnep", &bnep_sk_list, NULL);
245 if (err < 0) {
246 BT_ERR("Failed to create BNEP proc file");
247 bt_sock_unregister(BTPROTO_BNEP);
248 goto error;
251 BT_INFO("BNEP socket layer initialized");
253 return 0;
255 error:
256 proto_unregister(&bnep_proto);
257 return err;
260 void __exit bnep_sock_cleanup(void)
262 bt_procfs_cleanup(&init_net, "bnep");
263 bt_sock_unregister(BTPROTO_BNEP);
264 proto_unregister(&bnep_proto);