6 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: ng_l2cap_evnt.c,v 1.5 2003/09/08 19:11:45 max Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/endian.h>
38 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include <netgraph/ng_message.h>
42 #include <netgraph/netgraph.h>
43 #include <netgraph/bluetooth/include/ng_bluetooth.h>
44 #include <netgraph/bluetooth/include/ng_hci.h>
45 #include <netgraph/bluetooth/include/ng_l2cap.h>
46 #include <netgraph/bluetooth/l2cap/ng_l2cap_var.h>
47 #include <netgraph/bluetooth/l2cap/ng_l2cap_cmds.h>
48 #include <netgraph/bluetooth/l2cap/ng_l2cap_evnt.h>
49 #include <netgraph/bluetooth/l2cap/ng_l2cap_llpi.h>
50 #include <netgraph/bluetooth/l2cap/ng_l2cap_ulpi.h>
51 #include <netgraph/bluetooth/l2cap/ng_l2cap_misc.h>
53 /******************************************************************************
54 ******************************************************************************
55 ** L2CAP events processing module
56 ******************************************************************************
57 ******************************************************************************/
59 static int ng_l2cap_process_signal_cmd (ng_l2cap_con_p);
60 static int ng_l2cap_process_lesignal_cmd (ng_l2cap_con_p);
61 static int ng_l2cap_process_cmd_rej (ng_l2cap_con_p, u_int8_t);
62 static int ng_l2cap_process_cmd_urq (ng_l2cap_con_p, u_int8_t);
63 static int ng_l2cap_process_cmd_urs (ng_l2cap_con_p, u_int8_t);
64 static int ng_l2cap_process_con_req (ng_l2cap_con_p, u_int8_t);
65 static int ng_l2cap_process_con_rsp (ng_l2cap_con_p, u_int8_t);
66 static int ng_l2cap_process_cfg_req (ng_l2cap_con_p, u_int8_t);
67 static int ng_l2cap_process_cfg_rsp (ng_l2cap_con_p, u_int8_t);
68 static int ng_l2cap_process_discon_req (ng_l2cap_con_p, u_int8_t);
69 static int ng_l2cap_process_discon_rsp (ng_l2cap_con_p, u_int8_t);
70 static int ng_l2cap_process_echo_req (ng_l2cap_con_p, u_int8_t);
71 static int ng_l2cap_process_echo_rsp (ng_l2cap_con_p, u_int8_t);
72 static int ng_l2cap_process_info_req (ng_l2cap_con_p, u_int8_t);
73 static int ng_l2cap_process_info_rsp (ng_l2cap_con_p, u_int8_t);
74 static int send_l2cap_reject
75 (ng_l2cap_con_p, u_int8_t, u_int16_t, u_int16_t, u_int16_t, u_int16_t);
76 static int send_l2cap_con_rej
77 (ng_l2cap_con_p, u_int8_t, u_int16_t, u_int16_t, u_int16_t);
78 static int send_l2cap_cfg_rsp
79 (ng_l2cap_con_p, u_int8_t, u_int16_t, u_int16_t, struct mbuf *);
80 static int send_l2cap_param_urs
81 (ng_l2cap_con_p , u_int8_t , u_int16_t);
83 static int get_next_l2cap_opt
84 (struct mbuf *, int *, ng_l2cap_cfg_opt_p, ng_l2cap_cfg_opt_val_p);
87 * Receive L2CAP packet. First get L2CAP header and verify packet. Than
88 * get destination channel and process packet.
92 ng_l2cap_receive(ng_l2cap_con_p con)
94 ng_l2cap_p l2cap = con->l2cap;
95 ng_l2cap_hdr_t *hdr = NULL;
99 if (con->rx_pkt->m_pkthdr.len < sizeof(*hdr)) {
101 "%s: %s - invalid L2CAP packet. Packet too small, len=%d\n",
102 __func__, NG_NODE_NAME(l2cap->node),
103 con->rx_pkt->m_pkthdr.len);
108 /* Get L2CAP header */
109 NG_L2CAP_M_PULLUP(con->rx_pkt, sizeof(*hdr));
110 if (con->rx_pkt == NULL)
113 hdr = mtod(con->rx_pkt, ng_l2cap_hdr_t *);
114 hdr->length = le16toh(hdr->length);
115 hdr->dcid = le16toh(hdr->dcid);
117 /* Check payload size */
118 if (hdr->length != con->rx_pkt->m_pkthdr.len - sizeof(*hdr)) {
120 "%s: %s - invalid L2CAP packet. Payload length mismatch, length=%d, len=%zd\n",
121 __func__, NG_NODE_NAME(l2cap->node), hdr->length,
122 con->rx_pkt->m_pkthdr.len - sizeof(*hdr));
129 case NG_L2CAP_SIGNAL_CID: /* L2CAP command */
130 m_adj(con->rx_pkt, sizeof(*hdr));
131 error = ng_l2cap_process_signal_cmd(con);
133 case NG_L2CAP_LESIGNAL_CID:
134 m_adj(con->rx_pkt, sizeof(*hdr));
135 error = ng_l2cap_process_lesignal_cmd(con);
137 case NG_L2CAP_CLT_CID: /* Connectionless packet */
138 error = ng_l2cap_l2ca_clt_receive(con);
141 default: /* Data packet */
142 error = ng_l2cap_l2ca_receive(con);
148 NG_FREE_M(con->rx_pkt);
151 } /* ng_l2cap_receive */
154 * Process L2CAP signaling command. We already know that destination channel ID
155 * is 0x1 that means we have received signaling command from peer's L2CAP layer.
156 * So get command header, decode and process it.
158 * XXX do we need to check signaling MTU here?
162 ng_l2cap_process_signal_cmd(ng_l2cap_con_p con)
164 ng_l2cap_p l2cap = con->l2cap;
165 ng_l2cap_cmd_hdr_t *hdr = NULL;
166 struct mbuf *m = NULL;
168 while (con->rx_pkt != NULL) {
169 /* Verify packet length */
170 if (con->rx_pkt->m_pkthdr.len < sizeof(*hdr)) {
172 "%s: %s - invalid L2CAP signaling command. Packet too small, len=%d\n",
173 __func__, NG_NODE_NAME(l2cap->node),
174 con->rx_pkt->m_pkthdr.len);
175 NG_FREE_M(con->rx_pkt);
180 /* Get signaling command */
181 NG_L2CAP_M_PULLUP(con->rx_pkt, sizeof(*hdr));
182 if (con->rx_pkt == NULL)
185 hdr = mtod(con->rx_pkt, ng_l2cap_cmd_hdr_t *);
186 hdr->length = le16toh(hdr->length);
187 m_adj(con->rx_pkt, sizeof(*hdr));
189 /* Verify command length */
190 if (con->rx_pkt->m_pkthdr.len < hdr->length) {
192 "%s: %s - invalid L2CAP signaling command, code=%#x, ident=%d. " \
193 "Invalid command length=%d, m_pkthdr.len=%d\n",
194 __func__, NG_NODE_NAME(l2cap->node),
195 hdr->code, hdr->ident, hdr->length,
196 con->rx_pkt->m_pkthdr.len);
197 NG_FREE_M(con->rx_pkt);
202 /* Get the command, save the rest (if any) */
203 if (con->rx_pkt->m_pkthdr.len > hdr->length)
204 m = m_split(con->rx_pkt, hdr->length, M_NOWAIT);
208 /* Process command */
210 case NG_L2CAP_CMD_REJ:
211 ng_l2cap_process_cmd_rej(con, hdr->ident);
214 case NG_L2CAP_CON_REQ:
215 ng_l2cap_process_con_req(con, hdr->ident);
218 case NG_L2CAP_CON_RSP:
219 ng_l2cap_process_con_rsp(con, hdr->ident);
222 case NG_L2CAP_CFG_REQ:
223 ng_l2cap_process_cfg_req(con, hdr->ident);
226 case NG_L2CAP_CFG_RSP:
227 ng_l2cap_process_cfg_rsp(con, hdr->ident);
230 case NG_L2CAP_DISCON_REQ:
231 ng_l2cap_process_discon_req(con, hdr->ident);
234 case NG_L2CAP_DISCON_RSP:
235 ng_l2cap_process_discon_rsp(con, hdr->ident);
238 case NG_L2CAP_ECHO_REQ:
239 ng_l2cap_process_echo_req(con, hdr->ident);
242 case NG_L2CAP_ECHO_RSP:
243 ng_l2cap_process_echo_rsp(con, hdr->ident);
246 case NG_L2CAP_INFO_REQ:
247 ng_l2cap_process_info_req(con, hdr->ident);
250 case NG_L2CAP_INFO_RSP:
251 ng_l2cap_process_info_rsp(con, hdr->ident);
256 "%s: %s - unknown L2CAP signaling command, code=%#x, ident=%d, length=%d\n",
257 __func__, NG_NODE_NAME(l2cap->node),
258 hdr->code, hdr->ident, hdr->length);
261 * Send L2CAP_CommandRej. Do not really care
265 send_l2cap_reject(con, hdr->ident,
266 NG_L2CAP_REJ_NOT_UNDERSTOOD, 0, 0, 0);
267 NG_FREE_M(con->rx_pkt);
275 } /* ng_l2cap_process_signal_cmd */
277 ng_l2cap_process_lesignal_cmd(ng_l2cap_con_p con)
279 ng_l2cap_p l2cap = con->l2cap;
280 ng_l2cap_cmd_hdr_t *hdr = NULL;
281 struct mbuf *m = NULL;
283 while (con->rx_pkt != NULL) {
284 /* Verify packet length */
285 if (con->rx_pkt->m_pkthdr.len < sizeof(*hdr)) {
287 "%s: %s - invalid L2CAP signaling command. Packet too small, len=%d\n",
288 __func__, NG_NODE_NAME(l2cap->node),
289 con->rx_pkt->m_pkthdr.len);
290 NG_FREE_M(con->rx_pkt);
295 /* Get signaling command */
296 NG_L2CAP_M_PULLUP(con->rx_pkt, sizeof(*hdr));
297 if (con->rx_pkt == NULL)
300 hdr = mtod(con->rx_pkt, ng_l2cap_cmd_hdr_t *);
301 hdr->length = le16toh(hdr->length);
302 m_adj(con->rx_pkt, sizeof(*hdr));
304 /* Verify command length */
305 if (con->rx_pkt->m_pkthdr.len < hdr->length) {
307 "%s: %s - invalid L2CAP signaling command, code=%#x, ident=%d. " \
308 "Invalid command length=%d, m_pkthdr.len=%d\n",
309 __func__, NG_NODE_NAME(l2cap->node),
310 hdr->code, hdr->ident, hdr->length,
311 con->rx_pkt->m_pkthdr.len);
312 NG_FREE_M(con->rx_pkt);
317 /* Get the command, save the rest (if any) */
318 if (con->rx_pkt->m_pkthdr.len > hdr->length)
319 m = m_split(con->rx_pkt, hdr->length, M_NOWAIT);
323 /* Process command */
325 case NG_L2CAP_CMD_REJ:
326 ng_l2cap_process_cmd_rej(con, hdr->ident);
328 case NG_L2CAP_CMD_PARAM_UPDATE_REQUEST:
329 ng_l2cap_process_cmd_urq(con, hdr->ident);
331 case NG_L2CAP_CMD_PARAM_UPDATE_RESPONSE:
332 ng_l2cap_process_cmd_urs(con, hdr->ident);
338 "%s: %s - unknown L2CAP signaling command, code=%#x, ident=%d, length=%d\n",
339 __func__, NG_NODE_NAME(l2cap->node),
340 hdr->code, hdr->ident, hdr->length);
343 * Send L2CAP_CommandRej. Do not really care
347 send_l2cap_reject(con, hdr->ident,
348 NG_L2CAP_REJ_NOT_UNDERSTOOD, 0, 0, 0);
349 NG_FREE_M(con->rx_pkt);
357 } /* ng_l2cap_process_signal_cmd */
358 /*Update Paramater Request*/
359 static int ng_l2cap_process_cmd_urq(ng_l2cap_con_p con, uint8_t ident)
361 /*We do not implement paramter negotiasion for now*/
362 send_l2cap_param_urs(con, ident, NG_L2CAP_UPDATE_PARAM_ACCEPT);
363 NG_FREE_M(con->rx_pkt);
367 static int ng_l2cap_process_cmd_urs(ng_l2cap_con_p con, uint8_t ident)
369 /* We only support master side yet .*/
370 //send_l2cap_reject(con,ident ... );
372 NG_FREE_M(con->rx_pkt);
377 * Process L2CAP_CommandRej command
381 ng_l2cap_process_cmd_rej(ng_l2cap_con_p con, u_int8_t ident)
383 ng_l2cap_p l2cap = con->l2cap;
384 ng_l2cap_cmd_rej_cp *cp = NULL;
385 ng_l2cap_cmd_p cmd = NULL;
387 /* Get command parameters */
388 NG_L2CAP_M_PULLUP(con->rx_pkt, sizeof(*cp));
389 if (con->rx_pkt == NULL)
392 cp = mtod(con->rx_pkt, ng_l2cap_cmd_rej_cp *);
393 cp->reason = le16toh(cp->reason);
395 /* Check if we have pending command descriptor */
396 cmd = ng_l2cap_cmd_by_ident(con, ident);
398 /* If command timeout already happened then ignore reject */
399 if (ng_l2cap_command_untimeout(cmd) != 0) {
400 NG_FREE_M(con->rx_pkt);
404 ng_l2cap_unlink_cmd(cmd);
407 case NG_L2CAP_CON_REQ:
408 ng_l2cap_l2ca_con_rsp(cmd->ch,cmd->token,cp->reason,0);
409 ng_l2cap_free_chan(cmd->ch);
412 case NG_L2CAP_CFG_REQ:
413 ng_l2cap_l2ca_cfg_rsp(cmd->ch, cmd->token, cp->reason);
416 case NG_L2CAP_DISCON_REQ:
417 ng_l2cap_l2ca_discon_rsp(cmd->ch,cmd->token,cp->reason);
418 ng_l2cap_free_chan(cmd->ch); /* XXX free channel */
421 case NG_L2CAP_ECHO_REQ:
422 ng_l2cap_l2ca_ping_rsp(cmd->con, cmd->token,
426 case NG_L2CAP_INFO_REQ:
427 ng_l2cap_l2ca_get_info_rsp(cmd->con, cmd->token,
433 "%s: %s - unexpected L2CAP_CommandRej. Unexpected L2CAP command opcode=%d\n",
434 __func__, NG_NODE_NAME(l2cap->node), cmd->code);
438 ng_l2cap_free_cmd(cmd);
441 "%s: %s - unexpected L2CAP_CommandRej command. " \
442 "Requested ident does not exist, ident=%d\n",
443 __func__, NG_NODE_NAME(l2cap->node), ident);
445 NG_FREE_M(con->rx_pkt);
448 } /* ng_l2cap_process_cmd_rej */
451 * Process L2CAP_ConnectReq command
455 ng_l2cap_process_con_req(ng_l2cap_con_p con, u_int8_t ident)
457 ng_l2cap_p l2cap = con->l2cap;
458 struct mbuf *m = con->rx_pkt;
459 ng_l2cap_con_req_cp *cp = NULL;
460 ng_l2cap_chan_p ch = NULL;
465 /* Get command parameters */
466 NG_L2CAP_M_PULLUP(m, sizeof(*cp));
470 cp = mtod(m, ng_l2cap_con_req_cp *);
471 psm = le16toh(cp->psm);
472 dcid = le16toh(cp->scid);
476 if(dcid == NG_L2CAP_ATT_CID)
477 idtype = NG_L2CAP_L2CA_IDTYPE_ATT;
478 else if( con->linktype != NG_HCI_LINK_ACL)
479 idtype = NG_L2CAP_L2CA_IDTYPE_LE;
481 idtype = NG_L2CAP_L2CA_IDTYPE_BREDR;
484 * Create new channel and send L2CA_ConnectInd notification
485 * to the upper layer protocol.
488 ch = ng_l2cap_new_chan(l2cap, con, psm, idtype);
491 return (send_l2cap_con_rej(con, ident, 0, dcid,
492 NG_L2CAP_NO_RESOURCES));
494 /* Update channel IDs */
497 /* Sent L2CA_ConnectInd notification to the upper layer */
499 ch->state = NG_L2CAP_W4_L2CA_CON_RSP;
501 error = ng_l2cap_l2ca_con_ind(ch);
503 send_l2cap_con_rej(con, ident, ch->scid, dcid,
504 (error == ENOMEM)? NG_L2CAP_NO_RESOURCES :
505 NG_L2CAP_PSM_NOT_SUPPORTED);
506 ng_l2cap_free_chan(ch);
510 } /* ng_l2cap_process_con_req */
513 * Process L2CAP_ConnectRsp command
517 ng_l2cap_process_con_rsp(ng_l2cap_con_p con, u_int8_t ident)
519 ng_l2cap_p l2cap = con->l2cap;
520 struct mbuf *m = con->rx_pkt;
521 ng_l2cap_con_rsp_cp *cp = NULL;
522 ng_l2cap_cmd_p cmd = NULL;
523 u_int16_t scid, dcid, result, status;
526 /* Get command parameters */
527 NG_L2CAP_M_PULLUP(m, sizeof(*cp));
531 cp = mtod(m, ng_l2cap_con_rsp_cp *);
532 dcid = le16toh(cp->dcid);
533 scid = le16toh(cp->scid);
534 result = le16toh(cp->result);
535 status = le16toh(cp->status);
540 /* Check if we have pending command descriptor */
541 cmd = ng_l2cap_cmd_by_ident(con, ident);
544 "%s: %s - unexpected L2CAP_ConnectRsp command. ident=%d, con_handle=%d\n",
545 __func__, NG_NODE_NAME(l2cap->node), ident,
551 /* Verify channel state, if invalid - do nothing */
552 if (cmd->ch->state != NG_L2CAP_W4_L2CAP_CON_RSP) {
554 "%s: %s - unexpected L2CAP_ConnectRsp. " \
555 "Invalid channel state, cid=%d, state=%d\n",
556 __func__, NG_NODE_NAME(l2cap->node), scid,
561 /* Verify CIDs and send reject if does not match */
562 if (cmd->ch->scid != scid) {
564 "%s: %s - unexpected L2CAP_ConnectRsp. Channel IDs do not match, scid=%d(%d)\n",
565 __func__, NG_NODE_NAME(l2cap->node), cmd->ch->scid,
571 * Looks good. We got confirmation from our peer. Now process
572 * it. First disable RTX timer. Then check the result and send
573 * notification to the upper layer. If command timeout already
574 * happened then ignore response.
577 if ((error = ng_l2cap_command_untimeout(cmd)) != 0)
580 if (result == NG_L2CAP_PENDING) {
582 * Our peer wants more time to complete connection. We shall
583 * start ERTX timer and wait. Keep command in the list.
586 cmd->ch->dcid = dcid;
587 ng_l2cap_command_timeout(cmd, bluetooth_l2cap_ertx_timeout());
589 error = ng_l2cap_l2ca_con_rsp(cmd->ch, cmd->token,
592 ng_l2cap_free_chan(cmd->ch);
594 ng_l2cap_unlink_cmd(cmd);
596 if (result == NG_L2CAP_SUCCESS) {
598 * Channel is open. Complete command and move to CONFIG
599 * state. Since we have sent positive confirmation we
600 * expect to receive L2CA_Config request from the upper
604 cmd->ch->dcid = dcid;
605 cmd->ch->state = (cmd->ch->scid == NG_L2CAP_ATT_CID)?
606 NG_L2CAP_OPEN : NG_L2CAP_CONFIG;
608 /* There was an error, so close the channel */
610 "%s: %s - failed to open L2CAP channel, result=%d, status=%d\n",
611 __func__, NG_NODE_NAME(l2cap->node), result,
614 error = ng_l2cap_l2ca_con_rsp(cmd->ch, cmd->token,
617 /* XXX do we have to remove the channel on error? */
618 if (error != 0 || result != NG_L2CAP_SUCCESS)
619 ng_l2cap_free_chan(cmd->ch);
621 ng_l2cap_free_cmd(cmd);
627 /* Send reject. Do not really care about the result */
628 send_l2cap_reject(con, ident, NG_L2CAP_REJ_INVALID_CID, 0, scid, dcid);
631 } /* ng_l2cap_process_con_rsp */
634 * Process L2CAP_ConfigReq command
638 ng_l2cap_process_cfg_req(ng_l2cap_con_p con, u_int8_t ident)
640 ng_l2cap_p l2cap = con->l2cap;
641 struct mbuf *m = con->rx_pkt;
642 ng_l2cap_cfg_req_cp *cp = NULL;
643 ng_l2cap_chan_p ch = NULL;
644 u_int16_t dcid, respond, result;
645 ng_l2cap_cfg_opt_t hdr;
646 ng_l2cap_cfg_opt_val_t val;
649 /* Get command parameters */
651 NG_L2CAP_M_PULLUP(m, sizeof(*cp));
655 cp = mtod(m, ng_l2cap_cfg_req_cp *);
656 dcid = le16toh(cp->dcid);
657 respond = NG_L2CAP_OPT_CFLAG(le16toh(cp->flags));
658 m_adj(m, sizeof(*cp));
660 /* Check if we have this channel and it is in valid state */
661 ch = ng_l2cap_chan_by_scid(l2cap, dcid, NG_L2CAP_L2CA_IDTYPE_BREDR);
664 "%s: %s - unexpected L2CAP_ConfigReq command. " \
665 "Channel does not exist, cid=%d\n",
666 __func__, NG_NODE_NAME(l2cap->node), dcid);
670 /* Verify channel state */
671 if (ch->state != NG_L2CAP_CONFIG && ch->state != NG_L2CAP_OPEN) {
673 "%s: %s - unexpected L2CAP_ConfigReq. " \
674 "Invalid channel state, cid=%d, state=%d\n",
675 __func__, NG_NODE_NAME(l2cap->node), dcid, ch->state);
679 if (ch->state == NG_L2CAP_OPEN) { /* Re-configuration */
681 ch->state = NG_L2CAP_CONFIG;
684 for (result = 0, off = 0; ; ) {
685 error = get_next_l2cap_opt(m, &off, &hdr, &val);
686 if (error == 0) { /* We done with this packet */
689 } else if (error > 0) { /* Got option */
691 case NG_L2CAP_OPT_MTU:
695 case NG_L2CAP_OPT_FLUSH_TIMO:
696 ch->flush_timo = val.flush_timo;
699 case NG_L2CAP_OPT_QOS:
700 bcopy(&val.flow, &ch->iflow, sizeof(ch->iflow));
703 default: /* Ignore unknown hint option */
706 } else { /* Oops, something is wrong */
712 * Adjust mbuf so we can get to the start
713 * of the first option we did not like.
716 m_adj(m, off - sizeof(hdr));
717 m->m_pkthdr.len = sizeof(hdr) + hdr.length;
719 result = NG_L2CAP_UNKNOWN_OPTION;
721 /* XXX FIXME Send other reject codes? */
723 result = NG_L2CAP_REJECT;
731 * Now check and see if we have to respond. If everything was OK then
732 * respond contain "C flag" and (if set) we will respond with empty
733 * packet and will wait for more options.
735 * Other case is that we did not like peer's options and will respond
736 * with L2CAP_Config response command with Reject error code.
738 * When "respond == 0" than we have received all options and we will
739 * sent L2CA_ConfigInd event to the upper layer protocol.
743 error = send_l2cap_cfg_rsp(con, ident, ch->dcid, result, m);
745 ng_l2cap_l2ca_discon_ind(ch);
746 ng_l2cap_free_chan(ch);
749 /* Send L2CA_ConfigInd event to the upper layer protocol */
751 error = ng_l2cap_l2ca_cfg_ind(ch);
753 ng_l2cap_free_chan(ch);
759 /* Send reject. Do not really care about the result */
762 send_l2cap_reject(con, ident, NG_L2CAP_REJ_INVALID_CID, 0, 0, dcid);
765 } /* ng_l2cap_process_cfg_req */
768 * Process L2CAP_ConfigRsp command
772 ng_l2cap_process_cfg_rsp(ng_l2cap_con_p con, u_int8_t ident)
774 ng_l2cap_p l2cap = con->l2cap;
775 struct mbuf *m = con->rx_pkt;
776 ng_l2cap_cfg_rsp_cp *cp = NULL;
777 ng_l2cap_cmd_p cmd = NULL;
778 u_int16_t scid, cflag, result;
779 ng_l2cap_cfg_opt_t hdr;
780 ng_l2cap_cfg_opt_val_t val;
783 /* Get command parameters */
785 NG_L2CAP_M_PULLUP(m, sizeof(*cp));
789 cp = mtod(m, ng_l2cap_cfg_rsp_cp *);
790 scid = le16toh(cp->scid);
791 cflag = NG_L2CAP_OPT_CFLAG(le16toh(cp->flags));
792 result = le16toh(cp->result);
793 m_adj(m, sizeof(*cp));
795 /* Check if we have this command */
796 cmd = ng_l2cap_cmd_by_ident(con, ident);
799 "%s: %s - unexpected L2CAP_ConfigRsp command. ident=%d, con_handle=%d\n",
800 __func__, NG_NODE_NAME(l2cap->node), ident,
807 /* Verify CIDs and send reject if does not match */
808 if (cmd->ch->scid != scid) {
810 "%s: %s - unexpected L2CAP_ConfigRsp. " \
811 "Channel ID does not match, scid=%d(%d)\n",
812 __func__, NG_NODE_NAME(l2cap->node), cmd->ch->scid,
817 /* Verify channel state and reject if invalid */
818 if (cmd->ch->state != NG_L2CAP_CONFIG) {
820 "%s: %s - unexpected L2CAP_ConfigRsp. " \
821 "Invalid channel state, scid=%d, state=%d\n",
822 __func__, NG_NODE_NAME(l2cap->node), cmd->ch->scid,
828 * Looks like it is our response, so process it. First parse options,
829 * then verify C flag. If it is set then we shall expect more
830 * configuration options from the peer and we will wait. Otherwise we
831 * have received all options and we will send L2CA_ConfigRsp event to
832 * the upper layer protocol. If command timeout already happened then
836 if ((error = ng_l2cap_command_untimeout(cmd)) != 0) {
842 error = get_next_l2cap_opt(m, &off, &hdr, &val);
843 if (error == 0) /* We done with this packet */
845 else if (error > 0) { /* Got option */
847 case NG_L2CAP_OPT_MTU:
848 cmd->ch->imtu = val.mtu;
851 case NG_L2CAP_OPT_FLUSH_TIMO:
852 cmd->ch->flush_timo = val.flush_timo;
855 case NG_L2CAP_OPT_QOS:
856 bcopy(&val.flow, &cmd->ch->oflow,
857 sizeof(cmd->ch->oflow));
860 default: /* Ignore unknown hint option */
865 * XXX FIXME What to do here?
867 * This is really BAD :( options packet was broken, or
868 * peer sent us option that we did not understand. Let
869 * upper layer know and do not wait for more options.
873 "%s: %s - failed to parse configuration options, error=%d\n",
874 __func__, NG_NODE_NAME(l2cap->node), error);
876 result = NG_L2CAP_UNKNOWN;
885 if (cflag) /* Restart timer and wait for more options */
886 ng_l2cap_command_timeout(cmd, bluetooth_l2cap_rtx_timeout());
888 ng_l2cap_unlink_cmd(cmd);
890 /* Send L2CA_Config response to the upper layer protocol */
891 error = ng_l2cap_l2ca_cfg_rsp(cmd->ch, cmd->token, result);
894 * XXX FIXME what to do here? we were not able to send
895 * response to the upper layer protocol, so for now
896 * just close the channel. Send L2CAP_Disconnect to
901 "%s: %s - failed to send L2CA_Config response, error=%d\n",
902 __func__, NG_NODE_NAME(l2cap->node), error);
904 ng_l2cap_free_chan(cmd->ch);
907 ng_l2cap_free_cmd(cmd);
913 /* Send reject. Do not really care about the result */
916 send_l2cap_reject(con, ident, NG_L2CAP_REJ_INVALID_CID, 0, scid, 0);
919 } /* ng_l2cap_process_cfg_rsp */
922 * Process L2CAP_DisconnectReq command
926 ng_l2cap_process_discon_req(ng_l2cap_con_p con, u_int8_t ident)
928 ng_l2cap_p l2cap = con->l2cap;
929 ng_l2cap_discon_req_cp *cp = NULL;
930 ng_l2cap_chan_p ch = NULL;
931 ng_l2cap_cmd_p cmd = NULL;
932 u_int16_t scid, dcid;
934 /* Get command parameters */
935 NG_L2CAP_M_PULLUP(con->rx_pkt, sizeof(*cp));
936 if (con->rx_pkt == NULL)
939 cp = mtod(con->rx_pkt, ng_l2cap_discon_req_cp *);
940 dcid = le16toh(cp->dcid);
941 scid = le16toh(cp->scid);
943 NG_FREE_M(con->rx_pkt);
945 /* Check if we have this channel and it is in valid state */
946 ch = ng_l2cap_chan_by_scid(l2cap, dcid, NG_L2CAP_L2CA_IDTYPE_BREDR);
949 "%s: %s - unexpected L2CAP_DisconnectReq message. " \
950 "Channel does not exist, cid=%d\n",
951 __func__, NG_NODE_NAME(l2cap->node), dcid);
955 /* XXX Verify channel state and reject if invalid -- is that true? */
956 if (ch->state != NG_L2CAP_OPEN && ch->state != NG_L2CAP_CONFIG &&
957 ch->state != NG_L2CAP_W4_L2CAP_DISCON_RSP) {
959 "%s: %s - unexpected L2CAP_DisconnectReq. " \
960 "Invalid channel state, cid=%d, state=%d\n",
961 __func__, NG_NODE_NAME(l2cap->node), dcid, ch->state);
965 /* Match destination channel ID */
966 if (ch->dcid != scid || ch->scid != dcid) {
968 "%s: %s - unexpected L2CAP_DisconnectReq. " \
969 "Channel IDs does not match, channel: scid=%d, dcid=%d, " \
970 "request: scid=%d, dcid=%d\n",
971 __func__, NG_NODE_NAME(l2cap->node), ch->scid, ch->dcid,
977 * Looks good, so notify upper layer protocol that channel is about
978 * to be disconnected and send L2CA_DisconnectInd message. Then respond
979 * with L2CAP_DisconnectRsp.
982 if (ch->state != NG_L2CAP_W4_L2CAP_DISCON_RSP) {
983 ng_l2cap_l2ca_discon_ind(ch); /* do not care about result */
984 ng_l2cap_free_chan(ch);
987 /* Send L2CAP_DisconnectRsp */
988 cmd = ng_l2cap_new_cmd(con, NULL, ident, NG_L2CAP_DISCON_RSP, 0);
992 _ng_l2cap_discon_rsp(cmd->aux, ident, dcid, scid);
993 if (cmd->aux == NULL) {
994 ng_l2cap_free_cmd(cmd);
999 /* Link command to the queue */
1000 ng_l2cap_link_cmd(con, cmd);
1001 ng_l2cap_lp_deliver(con);
1006 /* Send reject. Do not really care about the result */
1007 send_l2cap_reject(con, ident, NG_L2CAP_REJ_INVALID_CID, 0, scid, dcid);
1010 } /* ng_l2cap_process_discon_req */
1013 * Process L2CAP_DisconnectRsp command
1017 ng_l2cap_process_discon_rsp(ng_l2cap_con_p con, u_int8_t ident)
1019 ng_l2cap_p l2cap = con->l2cap;
1020 ng_l2cap_discon_rsp_cp *cp = NULL;
1021 ng_l2cap_cmd_p cmd = NULL;
1022 u_int16_t scid, dcid;
1025 /* Get command parameters */
1026 NG_L2CAP_M_PULLUP(con->rx_pkt, sizeof(*cp));
1027 if (con->rx_pkt == NULL)
1030 cp = mtod(con->rx_pkt, ng_l2cap_discon_rsp_cp *);
1031 dcid = le16toh(cp->dcid);
1032 scid = le16toh(cp->scid);
1034 NG_FREE_M(con->rx_pkt);
1036 /* Check if we have pending command descriptor */
1037 cmd = ng_l2cap_cmd_by_ident(con, ident);
1040 "%s: %s - unexpected L2CAP_DisconnectRsp command. ident=%d, con_handle=%d\n",
1041 __func__, NG_NODE_NAME(l2cap->node), ident,
1046 /* Verify channel state, do nothing if invalid */
1047 if (cmd->ch->state != NG_L2CAP_W4_L2CAP_DISCON_RSP) {
1049 "%s: %s - unexpected L2CAP_DisconnectRsp. " \
1050 "Invalid channel state, cid=%d, state=%d\n",
1051 __func__, NG_NODE_NAME(l2cap->node), scid,
1056 /* Verify CIDs and send reject if does not match */
1057 if (cmd->ch->scid != scid || cmd->ch->dcid != dcid) {
1059 "%s: %s - unexpected L2CAP_DisconnectRsp. " \
1060 "Channel IDs do not match, scid=%d(%d), dcid=%d(%d)\n",
1061 __func__, NG_NODE_NAME(l2cap->node), cmd->ch->scid,
1062 scid, cmd->ch->dcid, dcid);
1067 * Looks like we have successfuly disconnected channel, so notify
1068 * upper layer. If command timeout already happened then ignore
1072 if ((error = ng_l2cap_command_untimeout(cmd)) != 0)
1075 error = ng_l2cap_l2ca_discon_rsp(cmd->ch, cmd->token, NG_L2CAP_SUCCESS);
1076 ng_l2cap_free_chan(cmd->ch); /* this will free commands too */
1079 } /* ng_l2cap_process_discon_rsp */
1082 * Process L2CAP_EchoReq command
1086 ng_l2cap_process_echo_req(ng_l2cap_con_p con, u_int8_t ident)
1088 ng_l2cap_p l2cap = con->l2cap;
1089 ng_l2cap_cmd_hdr_t *hdr = NULL;
1090 ng_l2cap_cmd_p cmd = NULL;
1092 con->rx_pkt = ng_l2cap_prepend(con->rx_pkt, sizeof(*hdr));
1093 if (con->rx_pkt == NULL) {
1095 "%s: %s - ng_l2cap_prepend() failed, size=%zd\n",
1096 __func__, NG_NODE_NAME(l2cap->node), sizeof(*hdr));
1101 hdr = mtod(con->rx_pkt, ng_l2cap_cmd_hdr_t *);
1102 hdr->code = NG_L2CAP_ECHO_RSP;
1104 hdr->length = htole16(con->rx_pkt->m_pkthdr.len - sizeof(*hdr));
1106 cmd = ng_l2cap_new_cmd(con, NULL, ident, NG_L2CAP_ECHO_RSP, 0);
1108 NG_FREE_M(con->rx_pkt);
1113 /* Attach data and link command to the queue */
1114 cmd->aux = con->rx_pkt;
1116 ng_l2cap_link_cmd(con, cmd);
1117 ng_l2cap_lp_deliver(con);
1120 } /* ng_l2cap_process_echo_req */
1123 * Process L2CAP_EchoRsp command
1127 ng_l2cap_process_echo_rsp(ng_l2cap_con_p con, u_int8_t ident)
1129 ng_l2cap_p l2cap = con->l2cap;
1130 ng_l2cap_cmd_p cmd = NULL;
1133 /* Check if we have this command */
1134 cmd = ng_l2cap_cmd_by_ident(con, ident);
1136 /* If command timeout already happened then ignore response */
1137 if ((error = ng_l2cap_command_untimeout(cmd)) != 0) {
1138 NG_FREE_M(con->rx_pkt);
1142 ng_l2cap_unlink_cmd(cmd);
1144 error = ng_l2cap_l2ca_ping_rsp(cmd->con, cmd->token,
1145 NG_L2CAP_SUCCESS, con->rx_pkt);
1147 ng_l2cap_free_cmd(cmd);
1151 "%s: %s - unexpected L2CAP_EchoRsp command. " \
1152 "Requested ident does not exist, ident=%d\n",
1153 __func__, NG_NODE_NAME(l2cap->node), ident);
1154 NG_FREE_M(con->rx_pkt);
1158 } /* ng_l2cap_process_echo_rsp */
1161 * Process L2CAP_InfoReq command
1165 ng_l2cap_process_info_req(ng_l2cap_con_p con, u_int8_t ident)
1167 ng_l2cap_p l2cap = con->l2cap;
1168 ng_l2cap_cmd_p cmd = NULL;
1171 /* Get command parameters */
1172 NG_L2CAP_M_PULLUP(con->rx_pkt, sizeof(ng_l2cap_info_req_cp));
1173 if (con->rx_pkt == NULL)
1176 type = le16toh(mtod(con->rx_pkt, ng_l2cap_info_req_cp *)->type);
1177 NG_FREE_M(con->rx_pkt);
1179 cmd = ng_l2cap_new_cmd(con, NULL, ident, NG_L2CAP_INFO_RSP, 0);
1184 case NG_L2CAP_CONNLESS_MTU:
1185 _ng_l2cap_info_rsp(cmd->aux, ident, NG_L2CAP_CONNLESS_MTU,
1186 NG_L2CAP_SUCCESS, NG_L2CAP_MTU_DEFAULT);
1190 _ng_l2cap_info_rsp(cmd->aux, ident, type,
1191 NG_L2CAP_NOT_SUPPORTED, 0);
1195 if (cmd->aux == NULL) {
1196 ng_l2cap_free_cmd(cmd);
1201 /* Link command to the queue */
1202 ng_l2cap_link_cmd(con, cmd);
1203 ng_l2cap_lp_deliver(con);
1206 } /* ng_l2cap_process_info_req */
1209 * Process L2CAP_InfoRsp command
1213 ng_l2cap_process_info_rsp(ng_l2cap_con_p con, u_int8_t ident)
1215 ng_l2cap_p l2cap = con->l2cap;
1216 ng_l2cap_info_rsp_cp *cp = NULL;
1217 ng_l2cap_cmd_p cmd = NULL;
1220 /* Get command parameters */
1221 NG_L2CAP_M_PULLUP(con->rx_pkt, sizeof(*cp));
1222 if (con->rx_pkt == NULL)
1225 cp = mtod(con->rx_pkt, ng_l2cap_info_rsp_cp *);
1226 cp->type = le16toh(cp->type);
1227 cp->result = le16toh(cp->result);
1228 m_adj(con->rx_pkt, sizeof(*cp));
1230 /* Check if we have pending command descriptor */
1231 cmd = ng_l2cap_cmd_by_ident(con, ident);
1234 "%s: %s - unexpected L2CAP_InfoRsp command. " \
1235 "Requested ident does not exist, ident=%d\n",
1236 __func__, NG_NODE_NAME(l2cap->node), ident);
1237 NG_FREE_M(con->rx_pkt);
1242 /* If command timeout already happened then ignore response */
1243 if ((error = ng_l2cap_command_untimeout(cmd)) != 0) {
1244 NG_FREE_M(con->rx_pkt);
1248 ng_l2cap_unlink_cmd(cmd);
1250 if (cp->result == NG_L2CAP_SUCCESS) {
1252 case NG_L2CAP_CONNLESS_MTU:
1253 if (con->rx_pkt->m_pkthdr.len == sizeof(u_int16_t))
1254 *mtod(con->rx_pkt, u_int16_t *) =
1255 le16toh(*mtod(con->rx_pkt,u_int16_t *));
1257 cp->result = NG_L2CAP_UNKNOWN; /* XXX */
1260 "%s: %s - invalid L2CAP_InfoRsp command. " \
1261 "Bad connectionless MTU parameter, len=%d\n",
1262 __func__, NG_NODE_NAME(l2cap->node),
1263 con->rx_pkt->m_pkthdr.len);
1269 "%s: %s - invalid L2CAP_InfoRsp command. Unknown info type=%d\n",
1270 __func__, NG_NODE_NAME(l2cap->node), cp->type);
1275 error = ng_l2cap_l2ca_get_info_rsp(cmd->con, cmd->token,
1276 cp->result, con->rx_pkt);
1278 ng_l2cap_free_cmd(cmd);
1282 } /* ng_l2cap_process_info_rsp */
1289 send_l2cap_reject(ng_l2cap_con_p con, u_int8_t ident, u_int16_t reason,
1290 u_int16_t mtu, u_int16_t scid, u_int16_t dcid)
1292 ng_l2cap_cmd_p cmd = NULL;
1294 cmd = ng_l2cap_new_cmd(con, NULL, ident, NG_L2CAP_CMD_REJ, 0);
1298 _ng_l2cap_cmd_rej(cmd->aux, cmd->ident, reason, mtu, scid, dcid);
1299 if (cmd->aux == NULL) {
1300 ng_l2cap_free_cmd(cmd);
1305 /* Link command to the queue */
1306 ng_l2cap_link_cmd(con, cmd);
1307 ng_l2cap_lp_deliver(con);
1310 } /* send_l2cap_reject */
1313 * Send L2CAP connection reject
1317 send_l2cap_con_rej(ng_l2cap_con_p con, u_int8_t ident, u_int16_t scid,
1318 u_int16_t dcid, u_int16_t result)
1320 ng_l2cap_cmd_p cmd = NULL;
1322 cmd = ng_l2cap_new_cmd(con, NULL, ident, NG_L2CAP_CON_RSP, 0);
1326 _ng_l2cap_con_rsp(cmd->aux, cmd->ident, scid, dcid, result, 0);
1327 if (cmd->aux == NULL) {
1328 ng_l2cap_free_cmd(cmd);
1333 /* Link command to the queue */
1334 ng_l2cap_link_cmd(con, cmd);
1335 ng_l2cap_lp_deliver(con);
1338 } /* send_l2cap_con_rej */
1341 * Send L2CAP config response
1345 send_l2cap_cfg_rsp(ng_l2cap_con_p con, u_int8_t ident, u_int16_t scid,
1346 u_int16_t result, struct mbuf *opt)
1348 ng_l2cap_cmd_p cmd = NULL;
1350 cmd = ng_l2cap_new_cmd(con, NULL, ident, NG_L2CAP_CFG_RSP, 0);
1357 _ng_l2cap_cfg_rsp(cmd->aux, cmd->ident, scid, 0, result, opt);
1358 if (cmd->aux == NULL) {
1359 ng_l2cap_free_cmd(cmd);
1364 /* Link command to the queue */
1365 ng_l2cap_link_cmd(con, cmd);
1366 ng_l2cap_lp_deliver(con);
1369 } /* send_l2cap_cfg_rsp */
1372 send_l2cap_param_urs(ng_l2cap_con_p con, u_int8_t ident,
1375 ng_l2cap_cmd_p cmd = NULL;
1377 cmd = ng_l2cap_new_cmd(con, NULL, ident,
1378 NG_L2CAP_CMD_PARAM_UPDATE_RESPONSE,
1385 _ng_l2cap_cmd_urs(cmd->aux, cmd->ident, result);
1386 if (cmd->aux == NULL) {
1387 ng_l2cap_free_cmd(cmd);
1392 /* Link command to the queue */
1393 ng_l2cap_link_cmd(con, cmd);
1394 ng_l2cap_lp_deliver(con);
1397 } /* send_l2cap_cfg_rsp */
1400 * Get next L2CAP configuration option
1404 * 1 we have got option
1405 * -1 header too short
1406 * -2 bad option value or length
1411 get_next_l2cap_opt(struct mbuf *m, int *off, ng_l2cap_cfg_opt_p hdr,
1412 ng_l2cap_cfg_opt_val_p val)
1414 int hint, len = m->m_pkthdr.len - (*off);
1418 if (len < 0 || len < sizeof(*hdr))
1421 m_copydata(m, *off, sizeof(*hdr), (caddr_t) hdr);
1422 *off += sizeof(*hdr);
1423 len -= sizeof(*hdr);
1425 hint = NG_L2CAP_OPT_HINT(hdr->type);
1426 hdr->type &= NG_L2CAP_OPT_HINT_MASK;
1428 switch (hdr->type) {
1429 case NG_L2CAP_OPT_MTU:
1430 if (hdr->length != NG_L2CAP_OPT_MTU_SIZE || len < hdr->length)
1433 m_copydata(m, *off, NG_L2CAP_OPT_MTU_SIZE, (caddr_t) val);
1434 val->mtu = le16toh(val->mtu);
1435 *off += NG_L2CAP_OPT_MTU_SIZE;
1438 case NG_L2CAP_OPT_FLUSH_TIMO:
1439 if (hdr->length != NG_L2CAP_OPT_FLUSH_TIMO_SIZE ||
1443 m_copydata(m, *off, NG_L2CAP_OPT_FLUSH_TIMO_SIZE, (caddr_t)val);
1444 val->flush_timo = le16toh(val->flush_timo);
1445 *off += NG_L2CAP_OPT_FLUSH_TIMO_SIZE;
1448 case NG_L2CAP_OPT_QOS:
1449 if (hdr->length != NG_L2CAP_OPT_QOS_SIZE || len < hdr->length)
1452 m_copydata(m, *off, NG_L2CAP_OPT_QOS_SIZE, (caddr_t) val);
1453 val->flow.token_rate = le32toh(val->flow.token_rate);
1454 val->flow.token_bucket_size =
1455 le32toh(val->flow.token_bucket_size);
1456 val->flow.peak_bandwidth = le32toh(val->flow.peak_bandwidth);
1457 val->flow.latency = le32toh(val->flow.latency);
1458 val->flow.delay_variation = le32toh(val->flow.delay_variation);
1459 *off += NG_L2CAP_OPT_QOS_SIZE;
1464 *off += hdr->length;
1471 } /* get_next_l2cap_opt */