11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds HIDP implementation for Linux Bluetooth stack (BlueZ). 31da177e4SLinus Torvalds Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org> 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify 61da177e4SLinus Torvalds it under the terms of the GNU General Public License version 2 as 71da177e4SLinus Torvalds published by the Free Software Foundation; 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 101da177e4SLinus Torvalds OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 111da177e4SLinus Torvalds FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 121da177e4SLinus Torvalds IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 131da177e4SLinus Torvalds CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 141da177e4SLinus Torvalds WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 151da177e4SLinus Torvalds ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 161da177e4SLinus Torvalds OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 191da177e4SLinus Torvalds COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 201da177e4SLinus Torvalds SOFTWARE IS DISCLAIMED. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 23b6459415SJakub Kicinski #include <linux/compat.h> 248c520a59SGustavo Padovan #include <linux/export.h> 251da177e4SLinus Torvalds #include <linux/file.h> 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #include "hidp.h" 281da177e4SLinus Torvalds 295c6ad8eeSMasatake YAMATO static struct bt_sock_list hidp_sk_list = { 305c6ad8eeSMasatake YAMATO .lock = __RW_LOCK_UNLOCKED(hidp_sk_list.lock) 315c6ad8eeSMasatake YAMATO }; 325c6ad8eeSMasatake YAMATO 331da177e4SLinus Torvalds static int hidp_sock_release(struct socket *sock) 341da177e4SLinus Torvalds { 351da177e4SLinus Torvalds struct sock *sk = sock->sk; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds if (!sk) 401da177e4SLinus Torvalds return 0; 411da177e4SLinus Torvalds 425c6ad8eeSMasatake YAMATO bt_sock_unlink(&hidp_sk_list, sk); 435c6ad8eeSMasatake YAMATO 441da177e4SLinus Torvalds sock_orphan(sk); 451da177e4SLinus Torvalds sock_put(sk); 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds return 0; 481da177e4SLinus Torvalds } 491da177e4SLinus Torvalds 50702ec307SAl Viro static int do_hidp_sock_ioctl(struct socket *sock, unsigned int cmd, void __user *argp) 511da177e4SLinus Torvalds { 521da177e4SLinus Torvalds struct hidp_connadd_req ca; 531da177e4SLinus Torvalds struct hidp_conndel_req cd; 541da177e4SLinus Torvalds struct hidp_connlist_req cl; 551da177e4SLinus Torvalds struct hidp_conninfo ci; 561da177e4SLinus Torvalds struct socket *csock; 571da177e4SLinus Torvalds struct socket *isock; 581da177e4SLinus Torvalds int err; 591da177e4SLinus Torvalds 60702ec307SAl Viro BT_DBG("cmd %x arg %p", cmd, argp); 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds switch (cmd) { 631da177e4SLinus Torvalds case HIDPCONNADD: 641da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 65bf5b30b8SZhao Hongjiang return -EPERM; 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds if (copy_from_user(&ca, argp, sizeof(ca))) 681da177e4SLinus Torvalds return -EFAULT; 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds csock = sockfd_lookup(ca.ctrl_sock, &err); 711da177e4SLinus Torvalds if (!csock) 721da177e4SLinus Torvalds return err; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds isock = sockfd_lookup(ca.intr_sock, &err); 751da177e4SLinus Torvalds if (!isock) { 7667b23219SJulia Lawall sockfd_put(csock); 771da177e4SLinus Torvalds return err; 781da177e4SLinus Torvalds } 79a1616a5aSYoung Xiao ca.name[sizeof(ca.name)-1] = 0; 801da177e4SLinus Torvalds 815205185dSDavid Herrmann err = hidp_connection_add(&ca, csock, isock); 825205185dSDavid Herrmann if (!err && copy_to_user(argp, &ca, sizeof(ca))) 831da177e4SLinus Torvalds err = -EFAULT; 845205185dSDavid Herrmann 8567b23219SJulia Lawall sockfd_put(csock); 8667b23219SJulia Lawall sockfd_put(isock); 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds return err; 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds case HIDPCONNDEL: 911da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 92bf5b30b8SZhao Hongjiang return -EPERM; 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds if (copy_from_user(&cd, argp, sizeof(cd))) 951da177e4SLinus Torvalds return -EFAULT; 961da177e4SLinus Torvalds 975205185dSDavid Herrmann return hidp_connection_del(&cd); 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds case HIDPGETCONNLIST: 1001da177e4SLinus Torvalds if (copy_from_user(&cl, argp, sizeof(cl))) 1011da177e4SLinus Torvalds return -EFAULT; 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds if (cl.cnum <= 0) 1041da177e4SLinus Torvalds return -EINVAL; 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds err = hidp_get_connlist(&cl); 1071da177e4SLinus Torvalds if (!err && copy_to_user(argp, &cl, sizeof(cl))) 1081da177e4SLinus Torvalds return -EFAULT; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds return err; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds case HIDPGETCONNINFO: 1131da177e4SLinus Torvalds if (copy_from_user(&ci, argp, sizeof(ci))) 1141da177e4SLinus Torvalds return -EFAULT; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds err = hidp_get_conninfo(&ci); 1171da177e4SLinus Torvalds if (!err && copy_to_user(argp, &ci, sizeof(ci))) 1181da177e4SLinus Torvalds return -EFAULT; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds return err; 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds return -EINVAL; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 126702ec307SAl Viro static int hidp_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 127702ec307SAl Viro { 128702ec307SAl Viro return do_hidp_sock_ioctl(sock, cmd, (void __user *)arg); 129702ec307SAl Viro } 130702ec307SAl Viro 131e9c5702eSMarcel Holtmann #ifdef CONFIG_COMPAT 132e9c5702eSMarcel Holtmann struct compat_hidp_connadd_req { 13317f09a7eSSzymon Janc int ctrl_sock; /* Connected control socket */ 13417f09a7eSSzymon Janc int intr_sock; /* Connected interrupt socket */ 135e9c5702eSMarcel Holtmann __u16 parser; 136e9c5702eSMarcel Holtmann __u16 rd_size; 137e9c5702eSMarcel Holtmann compat_uptr_t rd_data; 138e9c5702eSMarcel Holtmann __u8 country; 139e9c5702eSMarcel Holtmann __u8 subclass; 140e9c5702eSMarcel Holtmann __u16 vendor; 141e9c5702eSMarcel Holtmann __u16 product; 142e9c5702eSMarcel Holtmann __u16 version; 143e9c5702eSMarcel Holtmann __u32 flags; 144e9c5702eSMarcel Holtmann __u32 idle_to; 145e9c5702eSMarcel Holtmann char name[128]; 146e9c5702eSMarcel Holtmann }; 147e9c5702eSMarcel Holtmann 148e9c5702eSMarcel Holtmann static int hidp_sock_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 149e9c5702eSMarcel Holtmann { 150702ec307SAl Viro void __user *argp = compat_ptr(arg); 151e9c5702eSMarcel Holtmann int err; 152e9c5702eSMarcel Holtmann 153702ec307SAl Viro if (cmd == HIDPGETCONNLIST) { 154702ec307SAl Viro struct hidp_connlist_req cl; 155702ec307SAl Viro u32 __user *p = argp; 156702ec307SAl Viro u32 uci; 157702ec307SAl Viro 158702ec307SAl Viro if (get_user(cl.cnum, p) || get_user(uci, p + 1)) 159e9c5702eSMarcel Holtmann return -EFAULT; 160e9c5702eSMarcel Holtmann 161e9c5702eSMarcel Holtmann cl.ci = compat_ptr(uci); 162e9c5702eSMarcel Holtmann 163e9c5702eSMarcel Holtmann if (cl.cnum <= 0) 164e9c5702eSMarcel Holtmann return -EINVAL; 165e9c5702eSMarcel Holtmann 166e9c5702eSMarcel Holtmann err = hidp_get_connlist(&cl); 167e9c5702eSMarcel Holtmann 168702ec307SAl Viro if (!err && put_user(cl.cnum, p)) 169e9c5702eSMarcel Holtmann err = -EFAULT; 170e9c5702eSMarcel Holtmann 171e9c5702eSMarcel Holtmann return err; 172e9c5702eSMarcel Holtmann } else if (cmd == HIDPCONNADD) { 173702ec307SAl Viro struct compat_hidp_connadd_req ca32; 174702ec307SAl Viro struct hidp_connadd_req ca; 175702ec307SAl Viro struct socket *csock; 176702ec307SAl Viro struct socket *isock; 177e9c5702eSMarcel Holtmann 178702ec307SAl Viro if (!capable(CAP_NET_ADMIN)) 179702ec307SAl Viro return -EPERM; 180e9c5702eSMarcel Holtmann 181702ec307SAl Viro if (copy_from_user(&ca32, (void __user *) arg, sizeof(ca32))) 182e9c5702eSMarcel Holtmann return -EFAULT; 183e9c5702eSMarcel Holtmann 184702ec307SAl Viro ca.ctrl_sock = ca32.ctrl_sock; 185702ec307SAl Viro ca.intr_sock = ca32.intr_sock; 186702ec307SAl Viro ca.parser = ca32.parser; 187702ec307SAl Viro ca.rd_size = ca32.rd_size; 188702ec307SAl Viro ca.rd_data = compat_ptr(ca32.rd_data); 189702ec307SAl Viro ca.country = ca32.country; 190702ec307SAl Viro ca.subclass = ca32.subclass; 191702ec307SAl Viro ca.vendor = ca32.vendor; 192702ec307SAl Viro ca.product = ca32.product; 193702ec307SAl Viro ca.version = ca32.version; 194702ec307SAl Viro ca.flags = ca32.flags; 195702ec307SAl Viro ca.idle_to = ca32.idle_to; 196dcae9052SDan Carpenter ca32.name[sizeof(ca32.name) - 1] = '\0'; 197702ec307SAl Viro memcpy(ca.name, ca32.name, 128); 198e9c5702eSMarcel Holtmann 199702ec307SAl Viro csock = sockfd_lookup(ca.ctrl_sock, &err); 200702ec307SAl Viro if (!csock) 201702ec307SAl Viro return err; 202e9c5702eSMarcel Holtmann 203702ec307SAl Viro isock = sockfd_lookup(ca.intr_sock, &err); 204702ec307SAl Viro if (!isock) { 205702ec307SAl Viro sockfd_put(csock); 206702ec307SAl Viro return err; 207702ec307SAl Viro } 208702ec307SAl Viro 209702ec307SAl Viro err = hidp_connection_add(&ca, csock, isock); 210702ec307SAl Viro if (!err && copy_to_user(argp, &ca32, sizeof(ca32))) 211702ec307SAl Viro err = -EFAULT; 212702ec307SAl Viro 213702ec307SAl Viro sockfd_put(csock); 214702ec307SAl Viro sockfd_put(isock); 215702ec307SAl Viro 216702ec307SAl Viro return err; 217e9c5702eSMarcel Holtmann } 218e9c5702eSMarcel Holtmann 219e9c5702eSMarcel Holtmann return hidp_sock_ioctl(sock, cmd, arg); 220e9c5702eSMarcel Holtmann } 221e9c5702eSMarcel Holtmann #endif 222e9c5702eSMarcel Holtmann 22390ddc4f0SEric Dumazet static const struct proto_ops hidp_sock_ops = { 2241da177e4SLinus Torvalds .family = PF_BLUETOOTH, 2251da177e4SLinus Torvalds .owner = THIS_MODULE, 2261da177e4SLinus Torvalds .release = hidp_sock_release, 2271da177e4SLinus Torvalds .ioctl = hidp_sock_ioctl, 228e9c5702eSMarcel Holtmann #ifdef CONFIG_COMPAT 229e9c5702eSMarcel Holtmann .compat_ioctl = hidp_sock_compat_ioctl, 230e9c5702eSMarcel Holtmann #endif 2311da177e4SLinus Torvalds .bind = sock_no_bind, 2321da177e4SLinus Torvalds .getname = sock_no_getname, 2331da177e4SLinus Torvalds .sendmsg = sock_no_sendmsg, 2341da177e4SLinus Torvalds .recvmsg = sock_no_recvmsg, 2351da177e4SLinus Torvalds .listen = sock_no_listen, 2361da177e4SLinus Torvalds .shutdown = sock_no_shutdown, 2371da177e4SLinus Torvalds .connect = sock_no_connect, 2381da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 2391da177e4SLinus Torvalds .accept = sock_no_accept, 2401da177e4SLinus Torvalds .mmap = sock_no_mmap 2411da177e4SLinus Torvalds }; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds static struct proto hidp_proto = { 2441da177e4SLinus Torvalds .name = "HIDP", 2451da177e4SLinus Torvalds .owner = THIS_MODULE, 2461da177e4SLinus Torvalds .obj_size = sizeof(struct bt_sock) 2471da177e4SLinus Torvalds }; 2481da177e4SLinus Torvalds 2493f378b68SEric Paris static int hidp_sock_create(struct net *net, struct socket *sock, int protocol, 2503f378b68SEric Paris int kern) 2511da177e4SLinus Torvalds { 2521da177e4SLinus Torvalds struct sock *sk; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds BT_DBG("sock %p", sock); 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds if (sock->type != SOCK_RAW) 2571da177e4SLinus Torvalds return -ESOCKTNOSUPPORT; 2581da177e4SLinus Torvalds 259*464c702fSLuiz Augusto von Dentz sk = bt_sock_alloc(net, sock, &hidp_proto, protocol, GFP_ATOMIC, kern); 2601da177e4SLinus Torvalds if (!sk) 2611da177e4SLinus Torvalds return -ENOMEM; 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds sock->ops = &hidp_sock_ops; 2641da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 2651da177e4SLinus Torvalds 2665c6ad8eeSMasatake YAMATO bt_sock_link(&hidp_sk_list, sk); 2675c6ad8eeSMasatake YAMATO 2681da177e4SLinus Torvalds return 0; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 271ec1b4cf7SStephen Hemminger static const struct net_proto_family hidp_sock_family_ops = { 2721da177e4SLinus Torvalds .family = PF_BLUETOOTH, 2731da177e4SLinus Torvalds .owner = THIS_MODULE, 2741da177e4SLinus Torvalds .create = hidp_sock_create 2751da177e4SLinus Torvalds }; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds int __init hidp_init_sockets(void) 2781da177e4SLinus Torvalds { 2791da177e4SLinus Torvalds int err; 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds err = proto_register(&hidp_proto, 0); 2821da177e4SLinus Torvalds if (err < 0) 2831da177e4SLinus Torvalds return err; 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds err = bt_sock_register(BTPROTO_HIDP, &hidp_sock_family_ops); 2865c6ad8eeSMasatake YAMATO if (err < 0) { 2875c6ad8eeSMasatake YAMATO BT_ERR("Can't register HIDP socket"); 2881da177e4SLinus Torvalds goto error; 2895c6ad8eeSMasatake YAMATO } 2905c6ad8eeSMasatake YAMATO 291b0316615SAl Viro err = bt_procfs_init(&init_net, "hidp", &hidp_sk_list, NULL); 2925c6ad8eeSMasatake YAMATO if (err < 0) { 2935c6ad8eeSMasatake YAMATO BT_ERR("Failed to create HIDP proc file"); 2945c6ad8eeSMasatake YAMATO bt_sock_unregister(BTPROTO_HIDP); 2955c6ad8eeSMasatake YAMATO goto error; 2965c6ad8eeSMasatake YAMATO } 2975c6ad8eeSMasatake YAMATO 2985c6ad8eeSMasatake YAMATO BT_INFO("HIDP socket layer initialized"); 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds return 0; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds error: 3031da177e4SLinus Torvalds proto_unregister(&hidp_proto); 3041da177e4SLinus Torvalds return err; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds void __exit hidp_cleanup_sockets(void) 3081da177e4SLinus Torvalds { 3095c6ad8eeSMasatake YAMATO bt_procfs_cleanup(&init_net, "hidp"); 3105e9d7f86SDavid Herrmann bt_sock_unregister(BTPROTO_HIDP); 3111da177e4SLinus Torvalds proto_unregister(&hidp_proto); 3121da177e4SLinus Torvalds } 313