1 /**
2 * @file smp_null.c
3 * Security Manager Protocol stub
4 */
5
6 /*
7 * Copyright (c) 2015-2016 Intel Corporation
8 *
9 * SPDX-License-Identifier: Apache-2.0
10 */
11
12 #include <errno.h>
13 #include <stddef.h>
14
15 #include <zephyr/bluetooth/bluetooth.h>
16 #include <zephyr/bluetooth/conn.h>
17 #include <zephyr/bluetooth/buf.h>
18 #include <zephyr/bluetooth/l2cap.h>
19 #include <zephyr/kernel.h>
20 #include <zephyr/logging/log.h>
21 #include <zephyr/net_buf.h>
22 #include <zephyr/sys/atomic.h>
23 #include <zephyr/sys/util.h>
24 #include <zephyr/toolchain.h>
25
26 #include "conn_internal.h"
27 #include "hci_core.h"
28 #include "l2cap_internal.h"
29 #include "smp.h"
30
31 #define LOG_LEVEL CONFIG_BT_HCI_CORE_LOG_LEVEL
32 LOG_MODULE_REGISTER(bt_smp);
33
34 static struct bt_l2cap_le_chan bt_smp_pool[CONFIG_BT_MAX_CONN];
35
bt_smp_sign_verify(struct bt_conn * conn,struct net_buf * buf)36 int bt_smp_sign_verify(struct bt_conn *conn, struct net_buf *buf)
37 {
38 return -ENOTSUP;
39 }
40
bt_smp_sign(struct bt_conn * conn,struct net_buf * buf)41 int bt_smp_sign(struct bt_conn *conn, struct net_buf *buf)
42 {
43 return -ENOTSUP;
44 }
45
bt_smp_recv(struct bt_l2cap_chan * chan,struct net_buf * req_buf)46 static int bt_smp_recv(struct bt_l2cap_chan *chan, struct net_buf *req_buf)
47 {
48 struct bt_l2cap_le_chan *le_chan = BT_L2CAP_LE_CHAN(chan);
49 struct bt_smp_pairing_fail *rsp;
50 struct bt_smp_hdr *hdr;
51 struct net_buf *buf;
52
53 ARG_UNUSED(req_buf);
54
55 /* If a device does not support pairing then it shall respond with
56 * a Pairing Failed command with the reason set to "Pairing Not
57 * Supported" when any command is received.
58 * Core Specification Vol. 3, Part H, 3.3
59 */
60
61 buf = bt_l2cap_create_pdu(NULL, 0);
62 /* NULL is not a possible return due to K_FOREVER */
63
64 hdr = net_buf_add(buf, sizeof(*hdr));
65 hdr->code = BT_SMP_CMD_PAIRING_FAIL;
66
67 rsp = net_buf_add(buf, sizeof(*rsp));
68 rsp->reason = BT_SMP_ERR_PAIRING_NOTSUPP;
69
70 if (bt_l2cap_send_pdu(le_chan, buf, NULL, NULL)) {
71 net_buf_unref(buf);
72 }
73
74 return 0;
75 }
76
bt_smp_accept(struct bt_conn * conn,struct bt_l2cap_chan ** chan)77 static int bt_smp_accept(struct bt_conn *conn, struct bt_l2cap_chan **chan)
78 {
79 int i;
80 static const struct bt_l2cap_chan_ops ops = {
81 .recv = bt_smp_recv,
82 };
83
84 LOG_DBG("conn %p handle %u", conn, conn->handle);
85
86 for (i = 0; i < ARRAY_SIZE(bt_smp_pool); i++) {
87 struct bt_l2cap_le_chan *smp = &bt_smp_pool[i];
88
89 if (smp->chan.conn) {
90 continue;
91 }
92
93 smp->chan.ops = &ops;
94
95 *chan = &smp->chan;
96
97 return 0;
98 }
99
100 LOG_ERR("No available SMP context for conn %p", conn);
101
102 return -ENOMEM;
103 }
104
105 BT_L2CAP_CHANNEL_DEFINE(smp_fixed_chan, BT_L2CAP_CID_SMP, bt_smp_accept, NULL);
106
bt_smp_init(void)107 int bt_smp_init(void)
108 {
109 return 0;
110 }
111