^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) BlueZ - Bluetooth protocol stack for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) Copyright (C) 2000-2001 Qualcomm Incorporated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) it under the terms of the GNU General Public License version 2 as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) published by the Free Software Foundation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) SOFTWARE IS DISCLAIMED.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* Bluetooth address family and sockets. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/stringify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <asm/ioctls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <net/bluetooth/bluetooth.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include "leds.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include "selftest.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* Bluetooth sockets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define BT_MAX_PROTO 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static const struct net_proto_family *bt_proto[BT_MAX_PROTO];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static DEFINE_RWLOCK(bt_proto_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static struct lock_class_key bt_lock_key[BT_MAX_PROTO];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static const char *const bt_key_strings[BT_MAX_PROTO] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) "sk_lock-AF_BLUETOOTH-BTPROTO_L2CAP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) "sk_lock-AF_BLUETOOTH-BTPROTO_HCI",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) "sk_lock-AF_BLUETOOTH-BTPROTO_SCO",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) "sk_lock-AF_BLUETOOTH-BTPROTO_RFCOMM",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) "sk_lock-AF_BLUETOOTH-BTPROTO_BNEP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) "sk_lock-AF_BLUETOOTH-BTPROTO_CMTP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "sk_lock-AF_BLUETOOTH-BTPROTO_HIDP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) "sk_lock-AF_BLUETOOTH-BTPROTO_AVDTP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static struct lock_class_key bt_slock_key[BT_MAX_PROTO];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static const char *const bt_slock_key_strings[BT_MAX_PROTO] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) "slock-AF_BLUETOOTH-BTPROTO_L2CAP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) "slock-AF_BLUETOOTH-BTPROTO_HCI",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) "slock-AF_BLUETOOTH-BTPROTO_SCO",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) "slock-AF_BLUETOOTH-BTPROTO_RFCOMM",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) "slock-AF_BLUETOOTH-BTPROTO_BNEP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) "slock-AF_BLUETOOTH-BTPROTO_CMTP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) "slock-AF_BLUETOOTH-BTPROTO_HIDP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) "slock-AF_BLUETOOTH-BTPROTO_AVDTP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) void bt_sock_reclassify_lock(struct sock *sk, int proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) BUG_ON(!sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) BUG_ON(!sock_allow_reclassification(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) sock_lock_init_class_and_name(sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) bt_slock_key_strings[proto], &bt_slock_key[proto],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) bt_key_strings[proto], &bt_lock_key[proto]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) EXPORT_SYMBOL(bt_sock_reclassify_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int bt_sock_register(int proto, const struct net_proto_family *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (proto < 0 || proto >= BT_MAX_PROTO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) write_lock(&bt_proto_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (bt_proto[proto])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) bt_proto[proto] = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) write_unlock(&bt_proto_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) EXPORT_SYMBOL(bt_sock_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) void bt_sock_unregister(int proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (proto < 0 || proto >= BT_MAX_PROTO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) write_lock(&bt_proto_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) bt_proto[proto] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) write_unlock(&bt_proto_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) EXPORT_SYMBOL(bt_sock_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int bt_sock_create(struct net *net, struct socket *sock, int proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (net != &init_net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (proto < 0 || proto >= BT_MAX_PROTO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (!bt_proto[proto])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) request_module("bt-proto-%d", proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) err = -EPROTONOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) read_lock(&bt_proto_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (bt_proto[proto] && try_module_get(bt_proto[proto]->owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) err = bt_proto[proto]->create(net, sock, proto, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) bt_sock_reclassify_lock(sock->sk, proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) module_put(bt_proto[proto]->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) read_unlock(&bt_proto_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) void bt_sock_link(struct bt_sock_list *l, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) write_lock(&l->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) sk_add_node(sk, &l->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) write_unlock(&l->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) EXPORT_SYMBOL(bt_sock_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) write_lock(&l->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) sk_del_node_init(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) write_unlock(&l->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) EXPORT_SYMBOL(bt_sock_unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) BT_DBG("parent %p, sk %p", parent, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) bh_lock_sock_nested(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) bt_sk(sk)->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) sk_acceptq_added(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) EXPORT_SYMBOL(bt_accept_enqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* Calling function must hold the sk lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * bt_sk(sk)->parent must be non-NULL meaning sk is in the parent list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) void bt_accept_unlink(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) BT_DBG("sk %p state %d", sk, sk->sk_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) list_del_init(&bt_sk(sk)->accept_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) sk_acceptq_removed(bt_sk(sk)->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) bt_sk(sk)->parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) EXPORT_SYMBOL(bt_accept_unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct bt_sock *s, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) BT_DBG("parent %p", parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) list_for_each_entry_safe(s, n, &bt_sk(parent)->accept_q, accept_q) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) sk = (struct sock *)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* Prevent early freeing of sk due to unlink and sock_kill */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /* Check sk has not already been unlinked via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * bt_accept_unlink() due to serialisation caused by sk locking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!bt_sk(sk)->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) BT_DBG("sk %p, already unlinked", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* Restart the loop as sk is no longer in the list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * and also avoid a potential infinite loop because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * list_for_each_entry_safe() is not thread safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /* sk is safely in the parent list so reduce reference count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /* FIXME: Is this check still needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (sk->sk_state == BT_CLOSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) bt_accept_unlink(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (sk->sk_state == BT_CONNECTED || !newsock ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) bt_accept_unlink(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (newsock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) sock_graft(sk, newsock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) EXPORT_SYMBOL(bt_accept_dequeue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int noblock = flags & MSG_DONTWAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) size_t copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) size_t skblen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) BT_DBG("sock %p sk %p len %zu", sock, sk, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (flags & MSG_OOB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) skb = skb_recv_datagram(sk, flags, noblock, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (sk->sk_shutdown & RCV_SHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) skblen = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) copied = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (len < copied) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) msg->msg_flags |= MSG_TRUNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) copied = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) skb_reset_transport_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) err = skb_copy_datagram_msg(skb, 0, msg, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) sock_recv_ts_and_drops(msg, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (msg->msg_name && bt_sk(sk)->skb_msg_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) bt_sk(sk)->skb_msg_name(skb, msg->msg_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) &msg->msg_namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (bt_sk(sk)->skb_put_cmsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) bt_sk(sk)->skb_put_cmsg(skb, msg, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) skb_free_datagram(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (flags & MSG_TRUNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) copied = skblen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return err ? : copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) EXPORT_SYMBOL(bt_sock_recvmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static long bt_sock_data_wait(struct sock *sk, long timeo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) add_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (!skb_queue_empty(&sk->sk_receive_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (sk->sk_err || (sk->sk_shutdown & RCV_SHUTDOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (signal_pending(current) || !timeo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) timeo = schedule_timeout(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) remove_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return timeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) size_t target, copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) long timeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (flags & MSG_OOB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) BT_DBG("sk %p size %zu", sk, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) skb = skb_dequeue(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (copied >= target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) err = sock_error(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (sk->sk_shutdown & RCV_SHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (!timeo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) timeo = bt_sock_data_wait(sk, timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) err = sock_intr_errno(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) chunk = min_t(unsigned int, skb->len, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (skb_copy_datagram_msg(skb, 0, msg, chunk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) skb_queue_head(&sk->sk_receive_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (!copied)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) copied = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) copied += chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) size -= chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) sock_recv_ts_and_drops(msg, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (!(flags & MSG_PEEK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) int skb_len = skb_headlen(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (chunk <= skb_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) __skb_pull(skb, chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) struct sk_buff *frag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) __skb_pull(skb, skb_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) chunk -= skb_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) skb_walk_frags(skb, frag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (chunk <= frag->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /* Pulling partial data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) skb->len -= chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) skb->data_len -= chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) __skb_pull(frag, chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) } else if (frag->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /* Pulling all frag data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) chunk -= frag->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) skb->len -= frag->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) skb->data_len -= frag->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) __skb_pull(frag, frag->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (skb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) skb_queue_head(&sk->sk_receive_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /* put message back and return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) skb_queue_head(&sk->sk_receive_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) } while (size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return copied ? : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) EXPORT_SYMBOL(bt_sock_stream_recvmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static inline __poll_t bt_accept_poll(struct sock *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct bt_sock *s, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) list_for_each_entry_safe(s, n, &bt_sk(parent)->accept_q, accept_q) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) sk = (struct sock *)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (sk->sk_state == BT_CONNECTED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) sk->sk_state == BT_CONNECT2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) __poll_t bt_sock_poll(struct file *file, struct socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) __poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) poll_wait(file, sk_sleep(sk), wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (sk->sk_state == BT_LISTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return bt_accept_poll(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) mask |= EPOLLERR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (sk->sk_shutdown & RCV_SHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (sk->sk_shutdown == SHUTDOWN_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) mask |= EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (sk->sk_state == BT_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) mask |= EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (sk->sk_state == BT_CONNECT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) sk->sk_state == BT_CONNECT2 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) sk->sk_state == BT_CONFIG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (!test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags) && sock_writeable(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) EXPORT_SYMBOL(bt_sock_poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) long amount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) case TIOCOUTQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (sk->sk_state == BT_LISTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) if (amount < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) amount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) err = put_user(amount, (int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) case TIOCINQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (sk->sk_state == BT_LISTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) skb = skb_peek(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) amount = skb ? skb->len : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) err = put_user(amount, (int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) err = -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) EXPORT_SYMBOL(bt_sock_ioctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) /* This function expects the sk lock to be held when called */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) BT_DBG("sk %p", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) add_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) while (sk->sk_state != state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (!timeo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) err = -EINPROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) err = sock_intr_errno(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) timeo = schedule_timeout(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) err = sock_error(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) remove_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) EXPORT_SYMBOL(bt_sock_wait_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) /* This function expects the sk lock to be held when called */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) int bt_sock_wait_ready(struct sock *sk, unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) unsigned long timeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) BT_DBG("sk %p", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) add_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) while (test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) if (!timeo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) err = sock_intr_errno(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) timeo = schedule_timeout(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) err = sock_error(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) remove_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) EXPORT_SYMBOL(bt_sock_wait_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) static void *bt_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) __acquires(seq->private->l->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) struct bt_sock_list *l = PDE_DATA(file_inode(seq->file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) read_lock(&l->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return seq_hlist_start_head(&l->head, *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) static void *bt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) struct bt_sock_list *l = PDE_DATA(file_inode(seq->file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) return seq_hlist_next(v, &l->head, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) static void bt_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) __releases(seq->private->l->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) struct bt_sock_list *l = PDE_DATA(file_inode(seq->file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) read_unlock(&l->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) static int bt_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) struct bt_sock_list *l = PDE_DATA(file_inode(seq->file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) if (v == SEQ_START_TOKEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) seq_puts(seq ,"sk RefCnt Rmem Wmem User Inode Parent");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (l->custom_seq_show) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) seq_putc(seq, ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) l->custom_seq_show(seq, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) seq_putc(seq, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) struct sock *sk = sk_entry(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) struct bt_sock *bt = bt_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) seq_printf(seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) "%pK %-6d %-6u %-6u %-6u %-6lu %-6lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) refcount_read(&sk->sk_refcnt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) sk_rmem_alloc_get(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) sk_wmem_alloc_get(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) from_kuid(seq_user_ns(seq), sock_i_uid(sk)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) sock_i_ino(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) bt->parent? sock_i_ino(bt->parent): 0LU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) if (l->custom_seq_show) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) seq_putc(seq, ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) l->custom_seq_show(seq, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) seq_putc(seq, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) static const struct seq_operations bt_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) .start = bt_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) .next = bt_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) .stop = bt_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) .show = bt_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) int bt_procfs_init(struct net *net, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) struct bt_sock_list *sk_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) int (* seq_show)(struct seq_file *, void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) sk_list->custom_seq_show = seq_show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (!proc_create_seq_data(name, 0, net->proc_net, &bt_seq_ops, sk_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) void bt_procfs_cleanup(struct net *net, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) remove_proc_entry(name, net->proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) int bt_procfs_init(struct net *net, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) struct bt_sock_list *sk_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) int (* seq_show)(struct seq_file *, void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) void bt_procfs_cleanup(struct net *net, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) EXPORT_SYMBOL(bt_procfs_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) EXPORT_SYMBOL(bt_procfs_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) static const struct net_proto_family bt_sock_family_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) .family = PF_BLUETOOTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) .create = bt_sock_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) struct dentry *bt_debugfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) EXPORT_SYMBOL_GPL(bt_debugfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) #define VERSION __stringify(BT_SUBSYS_VERSION) "." \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) __stringify(BT_SUBSYS_REVISION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static int __init bt_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) sock_skb_cb_check_size(sizeof(struct bt_skb_cb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) BT_INFO("Core ver %s", VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) err = bt_selftest();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) bt_debugfs = debugfs_create_dir("bluetooth", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) bt_leds_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) err = bt_sysfs_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) err = sock_register(&bt_sock_family_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) goto cleanup_sysfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) BT_INFO("HCI device and connection manager initialized");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) err = hci_sock_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) goto unregister_socket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) err = l2cap_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) goto cleanup_socket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) err = sco_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) goto cleanup_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) err = mgmt_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) goto cleanup_sco;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) cleanup_sco:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) sco_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) cleanup_cap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) l2cap_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) cleanup_socket:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) hci_sock_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) unregister_socket:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) sock_unregister(PF_BLUETOOTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) cleanup_sysfs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) bt_sysfs_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) static void __exit bt_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) mgmt_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) sco_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) l2cap_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) hci_sock_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) sock_unregister(PF_BLUETOOTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) bt_sysfs_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) bt_leds_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) debugfs_remove_recursive(bt_debugfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) subsys_initcall(bt_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) module_exit(bt_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) MODULE_DESCRIPTION("Bluetooth Core ver " VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) MODULE_VERSION(VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) MODULE_ALIAS_NETPROTO(PF_BLUETOOTH);