YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 22 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 23 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | SOFTWARE IS DISCLAIMED. |
| 25 | */ |
| 26 | |
Gustavo Padovan | 8c520a5 | 2012-05-23 04:04:22 -0300 | [diff] [blame] | 27 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include "bnep.h" |
| 31 | |
Masatake YAMATO | 77cf558 | 2012-07-26 01:27:35 +0900 | [diff] [blame] | 32 | static struct bt_sock_list bnep_sk_list = { |
| 33 | .lock = __RW_LOCK_UNLOCKED(bnep_sk_list.lock) |
| 34 | }; |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static int bnep_sock_release(struct socket *sock) |
| 37 | { |
| 38 | struct sock *sk = sock->sk; |
| 39 | |
| 40 | BT_DBG("sock %p sk %p", sock, sk); |
| 41 | |
| 42 | if (!sk) |
| 43 | return 0; |
| 44 | |
Masatake YAMATO | 77cf558 | 2012-07-26 01:27:35 +0900 | [diff] [blame] | 45 | bt_sock_unlink(&bnep_sk_list, sk); |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | sock_orphan(sk); |
| 48 | sock_put(sk); |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int bnep_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 53 | { |
| 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; |
Grzegorz Kolodziejczyk | 836a061 | 2015-04-03 12:14:55 +0200 | [diff] [blame] | 60 | __u32 supp_feat = BIT(BNEP_SETUP_RESPONSE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | int err; |
| 62 | |
| 63 | BT_DBG("cmd %x arg %lx", cmd, arg); |
| 64 | |
| 65 | switch (cmd) { |
| 66 | case BNEPCONNADD: |
| 67 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 68 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | if (copy_from_user(&ca, argp, sizeof(ca))) |
| 71 | return -EFAULT; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | nsock = sockfd_lookup(ca.sock, &err); |
| 74 | if (!nsock) |
| 75 | return err; |
| 76 | |
| 77 | if (nsock->sk->sk_state != BT_CONNECTED) { |
Julia Lawall | 67b2321 | 2008-01-07 22:38:42 -0800 | [diff] [blame] | 78 | sockfd_put(nsock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return -EBADFD; |
| 80 | } |
Vasiliy Kulikov | 43629f8 | 2011-02-14 13:54:31 +0300 | [diff] [blame] | 81 | ca.device[sizeof(ca.device)-1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | err = bnep_add_connection(&ca, nsock); |
| 84 | if (!err) { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 85 | if (copy_to_user(argp, &ca, sizeof(ca))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | err = -EFAULT; |
| 87 | } else |
Julia Lawall | 67b2321 | 2008-01-07 22:38:42 -0800 | [diff] [blame] | 88 | sockfd_put(nsock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | return err; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | case BNEPCONNDEL: |
| 93 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 94 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | if (copy_from_user(&cd, argp, sizeof(cd))) |
| 97 | return -EFAULT; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return bnep_del_connection(&cd); |
| 100 | |
| 101 | case BNEPGETCONNLIST: |
| 102 | if (copy_from_user(&cl, argp, sizeof(cl))) |
| 103 | return -EFAULT; |
| 104 | |
| 105 | if (cl.cnum <= 0) |
| 106 | return -EINVAL; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | err = bnep_get_connlist(&cl); |
| 109 | if (!err && copy_to_user(argp, &cl, sizeof(cl))) |
| 110 | return -EFAULT; |
| 111 | |
| 112 | return err; |
| 113 | |
| 114 | case BNEPGETCONNINFO: |
| 115 | if (copy_from_user(&ci, argp, sizeof(ci))) |
| 116 | return -EFAULT; |
| 117 | |
| 118 | err = bnep_get_conninfo(&ci); |
| 119 | if (!err && copy_to_user(argp, &ci, sizeof(ci))) |
| 120 | return -EFAULT; |
| 121 | |
| 122 | return err; |
| 123 | |
Grzegorz Kolodziejczyk | 0477e2e | 2015-04-03 12:14:53 +0200 | [diff] [blame] | 124 | case BNEPGETSUPPFEAT: |
| 125 | if (copy_to_user(argp, &supp_feat, sizeof(supp_feat))) |
| 126 | return -EFAULT; |
| 127 | |
| 128 | return 0; |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | default: |
| 131 | return -EINVAL; |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Marcel Holtmann | e9c5702e | 2006-10-15 17:30:22 +0200 | [diff] [blame] | 137 | #ifdef CONFIG_COMPAT |
| 138 | static int bnep_sock_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 139 | { |
| 140 | if (cmd == BNEPGETCONNLIST) { |
| 141 | struct bnep_connlist_req cl; |
Johan Hedberg | 816a11d | 2012-02-26 13:04:52 +0200 | [diff] [blame] | 142 | u32 uci; |
Marcel Holtmann | e9c5702e | 2006-10-15 17:30:22 +0200 | [diff] [blame] | 143 | int err; |
| 144 | |
Johan Hedberg | 816a11d | 2012-02-26 13:04:52 +0200 | [diff] [blame] | 145 | if (get_user(cl.cnum, (u32 __user *) arg) || |
Marcel Holtmann | e9c5702e | 2006-10-15 17:30:22 +0200 | [diff] [blame] | 146 | get_user(uci, (u32 __user *) (arg + 4))) |
| 147 | return -EFAULT; |
| 148 | |
| 149 | cl.ci = compat_ptr(uci); |
| 150 | |
| 151 | if (cl.cnum <= 0) |
| 152 | return -EINVAL; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 153 | |
Marcel Holtmann | e9c5702e | 2006-10-15 17:30:22 +0200 | [diff] [blame] | 154 | err = bnep_get_connlist(&cl); |
| 155 | |
Johan Hedberg | 816a11d | 2012-02-26 13:04:52 +0200 | [diff] [blame] | 156 | if (!err && put_user(cl.cnum, (u32 __user *) arg)) |
Marcel Holtmann | e9c5702e | 2006-10-15 17:30:22 +0200 | [diff] [blame] | 157 | err = -EFAULT; |
| 158 | |
| 159 | return err; |
| 160 | } |
| 161 | |
| 162 | return bnep_sock_ioctl(sock, cmd, arg); |
| 163 | } |
| 164 | #endif |
| 165 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 166 | static const struct proto_ops bnep_sock_ops = { |
Marcel Holtmann | e9c5702e | 2006-10-15 17:30:22 +0200 | [diff] [blame] | 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, |
Marcel Holtmann | e9c5702e | 2006-10-15 17:30:22 +0200 | [diff] [blame] | 178 | .listen = sock_no_listen, |
| 179 | .shutdown = sock_no_shutdown, |
| 180 | .setsockopt = sock_no_setsockopt, |
| 181 | .getsockopt = sock_no_getsockopt, |
| 182 | .connect = sock_no_connect, |
| 183 | .socketpair = sock_no_socketpair, |
| 184 | .accept = sock_no_accept, |
| 185 | .mmap = sock_no_mmap |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | static struct proto bnep_proto = { |
| 189 | .name = "BNEP", |
| 190 | .owner = THIS_MODULE, |
| 191 | .obj_size = sizeof(struct bt_sock) |
| 192 | }; |
| 193 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 194 | static int bnep_sock_create(struct net *net, struct socket *sock, int protocol, |
| 195 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
| 197 | struct sock *sk; |
| 198 | |
| 199 | BT_DBG("sock %p", sock); |
| 200 | |
| 201 | if (sock->type != SOCK_RAW) |
| 202 | return -ESOCKTNOSUPPORT; |
| 203 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 204 | sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &bnep_proto, kern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | if (!sk) |
| 206 | return -ENOMEM; |
| 207 | |
| 208 | sock_init_data(sock, sk); |
| 209 | |
| 210 | sock->ops = &bnep_sock_ops; |
| 211 | |
| 212 | sock->state = SS_UNCONNECTED; |
| 213 | |
| 214 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 215 | |
| 216 | sk->sk_protocol = protocol; |
| 217 | sk->sk_state = BT_OPEN; |
| 218 | |
Masatake YAMATO | 77cf558 | 2012-07-26 01:27:35 +0900 | [diff] [blame] | 219 | bt_sock_link(&bnep_sk_list, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 223 | static const struct net_proto_family bnep_sock_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | .family = PF_BLUETOOTH, |
| 225 | .owner = THIS_MODULE, |
| 226 | .create = bnep_sock_create |
| 227 | }; |
| 228 | |
| 229 | int __init bnep_sock_init(void) |
| 230 | { |
| 231 | int err; |
| 232 | |
| 233 | err = proto_register(&bnep_proto, 0); |
| 234 | if (err < 0) |
| 235 | return err; |
| 236 | |
| 237 | err = bt_sock_register(BTPROTO_BNEP, &bnep_sock_family_ops); |
Masatake YAMATO | 77cf558 | 2012-07-26 01:27:35 +0900 | [diff] [blame] | 238 | if (err < 0) { |
| 239 | BT_ERR("Can't register BNEP socket"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | goto error; |
Masatake YAMATO | 77cf558 | 2012-07-26 01:27:35 +0900 | [diff] [blame] | 241 | } |
| 242 | |
Al Viro | b031661 | 2013-04-04 19:14:33 -0400 | [diff] [blame] | 243 | err = bt_procfs_init(&init_net, "bnep", &bnep_sk_list, NULL); |
Masatake YAMATO | 77cf558 | 2012-07-26 01:27:35 +0900 | [diff] [blame] | 244 | if (err < 0) { |
| 245 | BT_ERR("Failed to create BNEP proc file"); |
| 246 | bt_sock_unregister(BTPROTO_BNEP); |
| 247 | goto error; |
| 248 | } |
| 249 | |
| 250 | BT_INFO("BNEP socket layer initialized"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | return 0; |
| 253 | |
| 254 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | proto_unregister(&bnep_proto); |
| 256 | return err; |
| 257 | } |
| 258 | |
Tobias Klauser | a4e2acf | 2008-03-05 18:47:40 -0800 | [diff] [blame] | 259 | void __exit bnep_sock_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Masatake YAMATO | 77cf558 | 2012-07-26 01:27:35 +0900 | [diff] [blame] | 261 | bt_procfs_cleanup(&init_net, "bnep"); |
David Herrmann | 5e9d7f8 | 2013-02-24 19:36:51 +0100 | [diff] [blame] | 262 | bt_sock_unregister(BTPROTO_BNEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | proto_unregister(&bnep_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |