2 BNEP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2001-2002 Inventel Systemes
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>
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);
45 bt_sock_unlink(&bnep_sk_list, sk);
52 static int do_bnep_sock_ioctl(struct socket *sock, unsigned int cmd, void __user *argp)
54 struct bnep_connlist_req cl;
55 struct bnep_connadd_req ca;
56 struct bnep_conndel_req cd;
57 struct bnep_conninfo ci;
59 __u32 supp_feat = BIT(BNEP_SETUP_RESPONSE);
62 BT_DBG("cmd %x arg %p", cmd, argp);
66 if (!capable(CAP_NET_ADMIN))
69 if (copy_from_user(&ca, argp, sizeof(ca)))
72 nsock = sockfd_lookup(ca.sock, &err);
76 if (nsock->sk->sk_state != BT_CONNECTED) {
80 ca.device[sizeof(ca.device)-1] = 0;
82 err = bnep_add_connection(&ca, nsock);
84 if (copy_to_user(argp, &ca, sizeof(ca)))
92 if (!capable(CAP_NET_ADMIN))
95 if (copy_from_user(&cd, argp, sizeof(cd)))
98 return bnep_del_connection(&cd);
100 case BNEPGETCONNLIST:
101 if (copy_from_user(&cl, argp, sizeof(cl)))
107 err = bnep_get_connlist(&cl);
108 if (!err && copy_to_user(argp, &cl, sizeof(cl)))
113 case BNEPGETCONNINFO:
114 if (copy_from_user(&ci, argp, sizeof(ci)))
117 err = bnep_get_conninfo(&ci);
118 if (!err && copy_to_user(argp, &ci, sizeof(ci)))
123 case BNEPGETSUPPFEAT:
124 if (copy_to_user(argp, &supp_feat, sizeof(supp_feat)))
136 static int bnep_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
138 return do_bnep_sock_ioctl(sock, cmd, (void __user *)arg);
142 static int bnep_sock_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
144 void __user *argp = compat_ptr(arg);
145 if (cmd == BNEPGETCONNLIST) {
146 struct bnep_connlist_req cl;
147 unsigned __user *p = argp;
151 if (get_user(cl.cnum, p) || get_user(uci, p + 1))
154 cl.ci = compat_ptr(uci);
159 err = bnep_get_connlist(&cl);
161 if (!err && put_user(cl.cnum, p))
167 return do_bnep_sock_ioctl(sock, cmd, argp);
171 static const struct proto_ops bnep_sock_ops = {
172 .family = PF_BLUETOOTH,
173 .owner = THIS_MODULE,
174 .release = bnep_sock_release,
175 .ioctl = bnep_sock_ioctl,
177 .compat_ioctl = bnep_sock_compat_ioctl,
179 .bind = sock_no_bind,
180 .getname = sock_no_getname,
181 .sendmsg = sock_no_sendmsg,
182 .recvmsg = sock_no_recvmsg,
183 .listen = sock_no_listen,
184 .shutdown = sock_no_shutdown,
185 .connect = sock_no_connect,
186 .socketpair = sock_no_socketpair,
187 .accept = sock_no_accept,
191 static struct proto bnep_proto = {
193 .owner = THIS_MODULE,
194 .obj_size = sizeof(struct bt_sock)
197 static int bnep_sock_create(struct net *net, struct socket *sock, int protocol,
202 BT_DBG("sock %p", sock);
204 if (sock->type != SOCK_RAW)
205 return -ESOCKTNOSUPPORT;
207 sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &bnep_proto, kern);
211 sock_init_data(sock, sk);
213 sock->ops = &bnep_sock_ops;
215 sock->state = SS_UNCONNECTED;
217 sock_reset_flag(sk, SOCK_ZAPPED);
219 sk->sk_protocol = protocol;
220 sk->sk_state = BT_OPEN;
222 bt_sock_link(&bnep_sk_list, sk);
226 static const struct net_proto_family bnep_sock_family_ops = {
227 .family = PF_BLUETOOTH,
228 .owner = THIS_MODULE,
229 .create = bnep_sock_create
232 int __init bnep_sock_init(void)
236 err = proto_register(&bnep_proto, 0);
240 err = bt_sock_register(BTPROTO_BNEP, &bnep_sock_family_ops);
242 BT_ERR("Can't register BNEP socket");
246 err = bt_procfs_init(&init_net, "bnep", &bnep_sk_list, NULL);
248 BT_ERR("Failed to create BNEP proc file");
249 bt_sock_unregister(BTPROTO_BNEP);
253 BT_INFO("BNEP socket layer initialized");
258 proto_unregister(&bnep_proto);
262 void __exit bnep_sock_cleanup(void)
264 bt_procfs_cleanup(&init_net, "bnep");
265 bt_sock_unregister(BTPROTO_BNEP);
266 proto_unregister(&bnep_proto);