6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: ng_l2cap_cmds.c,v 1.2 2003/09/08 19:11:45 max Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/endian.h>
40 #include <sys/malloc.h>
42 #include <sys/queue.h>
43 #include <netgraph/ng_message.h>
44 #include <netgraph/netgraph.h>
45 #include <netgraph/bluetooth/include/ng_bluetooth.h>
46 #include <netgraph/bluetooth/include/ng_hci.h>
47 #include <netgraph/bluetooth/include/ng_l2cap.h>
48 #include <netgraph/bluetooth/l2cap/ng_l2cap_var.h>
49 #include <netgraph/bluetooth/l2cap/ng_l2cap_cmds.h>
50 #include <netgraph/bluetooth/l2cap/ng_l2cap_evnt.h>
51 #include <netgraph/bluetooth/l2cap/ng_l2cap_llpi.h>
52 #include <netgraph/bluetooth/l2cap/ng_l2cap_ulpi.h>
53 #include <netgraph/bluetooth/l2cap/ng_l2cap_misc.h>
55 /******************************************************************************
56 ******************************************************************************
57 ** L2CAP commands processing module
58 ******************************************************************************
59 ******************************************************************************/
62 * Process L2CAP command queue on connection
66 ng_l2cap_con_wakeup(ng_l2cap_con_p con)
68 ng_l2cap_cmd_p cmd = NULL;
69 struct mbuf *m = NULL;
72 /* Find first non-pending command in the queue */
73 TAILQ_FOREACH(cmd, &con->cmd_list, next) {
74 KASSERT((cmd->con == con),
75 ("%s: %s - invalid connection pointer!\n",
76 __func__, NG_NODE_NAME(con->l2cap->node)));
78 if (!(cmd->flags & NG_L2CAP_CMD_PENDING))
85 /* Detach command packet */
91 case NG_L2CAP_DISCON_RSP:
92 case NG_L2CAP_ECHO_RSP:
93 case NG_L2CAP_INFO_RSP:
95 * Do not check return ng_l2cap_lp_send() value, because
96 * in these cases we do not really have a graceful way out.
97 * ECHO and INFO responses are internal to the stack and not
98 * visible to user. REJect is just being nice to remote end
99 * (otherwise remote end will timeout anyway). DISCON is
100 * probably most interesting here, however, if it fails
101 * there is nothing we can do anyway.
104 (void) ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
105 ng_l2cap_unlink_cmd(cmd);
106 ng_l2cap_free_cmd(cmd);
108 case NG_L2CAP_CMD_REJ:
109 (void) ng_l2cap_lp_send(con,
110 (con->linktype == NG_HCI_LINK_ACL)?
112 NG_L2CAP_LESIGNAL_CID
114 ng_l2cap_unlink_cmd(cmd);
115 ng_l2cap_free_cmd(cmd);
118 case NG_L2CAP_CON_REQ:
119 error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
121 ng_l2cap_l2ca_con_rsp(cmd->ch, cmd->token,
122 NG_L2CAP_NO_RESOURCES, 0);
123 ng_l2cap_free_chan(cmd->ch); /* will free commands */
125 ng_l2cap_command_timeout(cmd,
126 bluetooth_l2cap_rtx_timeout());
128 case NG_L2CAP_CON_RSP:
129 error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
130 ng_l2cap_unlink_cmd(cmd);
131 if (cmd->ch != NULL) {
132 ng_l2cap_l2ca_con_rsp_rsp(cmd->ch, cmd->token,
133 (error == 0)? NG_L2CAP_SUCCESS :
134 NG_L2CAP_NO_RESOURCES);
136 ng_l2cap_free_chan(cmd->ch);
138 ng_l2cap_free_cmd(cmd);
141 case NG_L2CAP_CFG_REQ:
142 error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
144 ng_l2cap_l2ca_cfg_rsp(cmd->ch, cmd->token,
145 NG_L2CAP_NO_RESOURCES);
146 ng_l2cap_unlink_cmd(cmd);
147 ng_l2cap_free_cmd(cmd);
149 ng_l2cap_command_timeout(cmd,
150 bluetooth_l2cap_rtx_timeout());
153 case NG_L2CAP_CFG_RSP:
154 error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
155 ng_l2cap_unlink_cmd(cmd);
157 ng_l2cap_l2ca_cfg_rsp_rsp(cmd->ch, cmd->token,
158 (error == 0)? NG_L2CAP_SUCCESS :
159 NG_L2CAP_NO_RESOURCES);
160 ng_l2cap_free_cmd(cmd);
163 case NG_L2CAP_DISCON_REQ:
164 error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
165 ng_l2cap_l2ca_discon_rsp(cmd->ch, cmd->token,
166 (error == 0)? NG_L2CAP_SUCCESS : NG_L2CAP_NO_RESOURCES);
168 ng_l2cap_free_chan(cmd->ch); /* XXX free channel */
170 ng_l2cap_command_timeout(cmd,
171 bluetooth_l2cap_rtx_timeout());
174 case NG_L2CAP_ECHO_REQ:
175 error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
177 ng_l2cap_l2ca_ping_rsp(con, cmd->token,
178 NG_L2CAP_NO_RESOURCES, NULL);
179 ng_l2cap_unlink_cmd(cmd);
180 ng_l2cap_free_cmd(cmd);
182 ng_l2cap_command_timeout(cmd,
183 bluetooth_l2cap_rtx_timeout());
186 case NG_L2CAP_INFO_REQ:
187 error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
189 ng_l2cap_l2ca_get_info_rsp(con, cmd->token,
190 NG_L2CAP_NO_RESOURCES, NULL);
191 ng_l2cap_unlink_cmd(cmd);
192 ng_l2cap_free_cmd(cmd);
194 ng_l2cap_command_timeout(cmd,
195 bluetooth_l2cap_rtx_timeout());
198 case NGM_L2CAP_L2CA_WRITE: {
199 int length = m->m_pkthdr.len;
201 if (cmd->ch->dcid == NG_L2CAP_CLT_CID) {
202 m = ng_l2cap_prepend(m, sizeof(ng_l2cap_clt_hdr_t));
206 mtod(m, ng_l2cap_clt_hdr_t *)->psm =
207 htole16(cmd->ch->psm);
211 error = ng_l2cap_lp_send(con, cmd->ch->dcid, m);
213 ng_l2cap_l2ca_write_rsp(cmd->ch, cmd->token,
214 (error == 0)? NG_L2CAP_SUCCESS : NG_L2CAP_NO_RESOURCES,
217 ng_l2cap_unlink_cmd(cmd);
218 ng_l2cap_free_cmd(cmd);
220 case NG_L2CAP_CMD_PARAM_UPDATE_RESPONSE:
221 error = ng_l2cap_lp_send(con, NG_L2CAP_LESIGNAL_CID, m);
222 ng_l2cap_unlink_cmd(cmd);
223 ng_l2cap_free_cmd(cmd);
225 case NG_L2CAP_CMD_PARAM_UPDATE_REQUEST:
227 /* XXX FIXME add other commands */
230 "%s: %s - unknown command code=%d\n",
231 __func__, NG_NODE_NAME(con->l2cap->node), cmd->code);
234 } /* ng_l2cap_con_wakeup */
237 * We have failed to open ACL connection to the remote unit. Could be negative
238 * confirmation or timeout. So fail any "delayed" commands, notify upper layer,
239 * remove all channels and remove connection descriptor.
243 ng_l2cap_con_fail(ng_l2cap_con_p con, u_int16_t result)
245 ng_l2cap_p l2cap = con->l2cap;
246 ng_l2cap_cmd_p cmd = NULL;
247 ng_l2cap_chan_p ch = NULL;
250 "%s: %s - ACL connection failed, result=%d\n",
251 __func__, NG_NODE_NAME(l2cap->node), result);
253 /* Connection is dying */
254 con->flags |= NG_L2CAP_CON_DYING;
256 /* Clean command queue */
257 while (!TAILQ_EMPTY(&con->cmd_list)) {
258 cmd = TAILQ_FIRST(&con->cmd_list);
260 ng_l2cap_unlink_cmd(cmd);
261 if(cmd->flags & NG_L2CAP_CMD_PENDING)
262 ng_l2cap_command_untimeout(cmd);
264 KASSERT((cmd->con == con),
265 ("%s: %s - invalid connection pointer!\n",
266 __func__, NG_NODE_NAME(l2cap->node)));
269 case NG_L2CAP_CMD_REJ:
270 case NG_L2CAP_DISCON_RSP:
271 case NG_L2CAP_ECHO_RSP:
272 case NG_L2CAP_INFO_RSP:
273 case NG_L2CAP_CMD_PARAM_UPDATE_RESPONSE:
276 case NG_L2CAP_CON_REQ:
277 ng_l2cap_l2ca_con_rsp(cmd->ch, cmd->token, result, 0);
280 case NG_L2CAP_CON_RSP:
282 ng_l2cap_l2ca_con_rsp_rsp(cmd->ch, cmd->token,
286 case NG_L2CAP_CFG_REQ:
287 case NG_L2CAP_CFG_RSP:
288 case NGM_L2CAP_L2CA_WRITE:
289 ng_l2cap_l2ca_discon_ind(cmd->ch);
292 case NG_L2CAP_DISCON_REQ:
293 ng_l2cap_l2ca_discon_rsp(cmd->ch, cmd->token,
297 case NG_L2CAP_ECHO_REQ:
298 ng_l2cap_l2ca_ping_rsp(cmd->con, cmd->token,
302 case NG_L2CAP_INFO_REQ:
303 ng_l2cap_l2ca_get_info_rsp(cmd->con, cmd->token,
307 /* XXX FIXME add other commands */
311 "%s: %s - unexpected command code=%d\n",
312 __func__, NG_NODE_NAME(l2cap->node), cmd->code);
317 ng_l2cap_free_chan(cmd->ch);
319 ng_l2cap_free_cmd(cmd);
323 * There still might be channels (in OPEN state?) that
324 * did not submit any commands, so disconnect them
327 LIST_FOREACH(ch, &l2cap->chan_list, next)
329 ng_l2cap_l2ca_discon_ind(ch);
331 /* Free connection descriptor */
332 ng_l2cap_free_con(con);
333 } /* ng_l2cap_con_fail */
336 * Process L2CAP command timeout. In general - notify upper layer and destroy
337 * channel. Do not pay much attention to return code, just do our best.
341 ng_l2cap_process_command_timeout(node_p node, hook_p hook, void *arg1, int arg2)
343 ng_l2cap_p l2cap = NULL;
344 ng_l2cap_con_p con = NULL;
345 ng_l2cap_cmd_p cmd = NULL;
346 u_int16_t con_handle = (arg2 & 0x0ffff);
347 u_int8_t ident = ((arg2 >> 16) & 0xff);
349 if (NG_NODE_NOT_VALID(node)) {
350 printf("%s: Netgraph node is not valid\n", __func__);
354 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
356 con = ng_l2cap_con_by_handle(l2cap, con_handle);
359 "%s: %s - could not find connection, con_handle=%d\n",
360 __func__, NG_NODE_NAME(node), con_handle);
364 cmd = ng_l2cap_cmd_by_ident(con, ident);
367 "%s: %s - could not find command, con_handle=%d, ident=%d\n",
368 __func__, NG_NODE_NAME(node), con_handle, ident);
372 cmd->flags &= ~NG_L2CAP_CMD_PENDING;
373 ng_l2cap_unlink_cmd(cmd);
376 case NG_L2CAP_CON_REQ:
377 ng_l2cap_l2ca_con_rsp(cmd->ch, cmd->token, NG_L2CAP_TIMEOUT, 0);
378 ng_l2cap_free_chan(cmd->ch);
381 case NG_L2CAP_CFG_REQ:
382 ng_l2cap_l2ca_cfg_rsp(cmd->ch, cmd->token, NG_L2CAP_TIMEOUT);
385 case NG_L2CAP_DISCON_REQ:
386 ng_l2cap_l2ca_discon_rsp(cmd->ch, cmd->token, NG_L2CAP_TIMEOUT);
387 ng_l2cap_free_chan(cmd->ch); /* XXX free channel */
390 case NG_L2CAP_ECHO_REQ:
391 /* Echo request timed out. Let the upper layer know */
392 ng_l2cap_l2ca_ping_rsp(cmd->con, cmd->token,
393 NG_L2CAP_TIMEOUT, NULL);
396 case NG_L2CAP_INFO_REQ:
397 /* Info request timed out. Let the upper layer know */
398 ng_l2cap_l2ca_get_info_rsp(cmd->con, cmd->token,
399 NG_L2CAP_TIMEOUT, NULL);
402 /* XXX FIXME add other commands */
406 "%s: %s - unexpected command code=%d\n",
407 __func__, NG_NODE_NAME(l2cap->node), cmd->code);
411 ng_l2cap_free_cmd(cmd);
412 } /* ng_l2cap_process_command_timeout */