l2cap_upper.c revision 1.1.2.3 1 1.1.2.3 yamt /* $NetBSD: l2cap_upper.c,v 1.1.2.3 2007/09/03 14:42:42 yamt Exp $ */
2 1.1.2.2 yamt
3 1.1.2.2 yamt /*-
4 1.1.2.2 yamt * Copyright (c) 2005 Iain Hibbert.
5 1.1.2.2 yamt * Copyright (c) 2006 Itronix Inc.
6 1.1.2.2 yamt * All rights reserved.
7 1.1.2.2 yamt *
8 1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.1.2.2 yamt * modification, are permitted provided that the following conditions
10 1.1.2.2 yamt * are met:
11 1.1.2.2 yamt * 1. Redistributions of source code must retain the above copyright
12 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer.
13 1.1.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer in the
15 1.1.2.2 yamt * documentation and/or other materials provided with the distribution.
16 1.1.2.2 yamt * 3. The name of Itronix Inc. may not be used to endorse
17 1.1.2.2 yamt * or promote products derived from this software without specific
18 1.1.2.2 yamt * prior written permission.
19 1.1.2.2 yamt *
20 1.1.2.2 yamt * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 1.1.2.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 1.1.2.2 yamt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 1.1.2.2 yamt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.1.2.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 1.1.2.2 yamt * ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
31 1.1.2.2 yamt */
32 1.1.2.2 yamt
33 1.1.2.2 yamt #include <sys/cdefs.h>
34 1.1.2.3 yamt __KERNEL_RCSID(0, "$NetBSD: l2cap_upper.c,v 1.1.2.3 2007/09/03 14:42:42 yamt Exp $");
35 1.1.2.2 yamt
36 1.1.2.2 yamt #include <sys/param.h>
37 1.1.2.2 yamt #include <sys/kernel.h>
38 1.1.2.2 yamt #include <sys/mbuf.h>
39 1.1.2.2 yamt #include <sys/proc.h>
40 1.1.2.2 yamt #include <sys/queue.h>
41 1.1.2.2 yamt #include <sys/socket.h>
42 1.1.2.2 yamt #include <sys/socketvar.h>
43 1.1.2.2 yamt #include <sys/systm.h>
44 1.1.2.2 yamt
45 1.1.2.2 yamt #include <netbt/bluetooth.h>
46 1.1.2.2 yamt #include <netbt/hci.h>
47 1.1.2.2 yamt #include <netbt/l2cap.h>
48 1.1.2.2 yamt
49 1.1.2.2 yamt /*******************************************************************************
50 1.1.2.2 yamt *
51 1.1.2.2 yamt * L2CAP Channel - Upper Protocol API
52 1.1.2.2 yamt */
53 1.1.2.2 yamt
54 1.1.2.2 yamt /*
55 1.1.2.2 yamt * l2cap_attach(handle, btproto, upper)
56 1.1.2.2 yamt *
57 1.1.2.2 yamt * attach new l2cap_channel to handle, populate
58 1.1.2.3 yamt * with reasonable defaults
59 1.1.2.2 yamt */
60 1.1.2.2 yamt int
61 1.1.2.2 yamt l2cap_attach(struct l2cap_channel **handle,
62 1.1.2.2 yamt const struct btproto *proto, void *upper)
63 1.1.2.2 yamt {
64 1.1.2.2 yamt struct l2cap_channel *chan;
65 1.1.2.2 yamt
66 1.1.2.3 yamt KASSERT(handle != NULL);
67 1.1.2.3 yamt KASSERT(proto != NULL);
68 1.1.2.3 yamt KASSERT(upper != NULL);
69 1.1.2.2 yamt
70 1.1.2.2 yamt chan = malloc(sizeof(struct l2cap_channel), M_BLUETOOTH,
71 1.1.2.2 yamt M_NOWAIT | M_ZERO);
72 1.1.2.2 yamt if (chan == NULL)
73 1.1.2.2 yamt return ENOMEM;
74 1.1.2.2 yamt
75 1.1.2.2 yamt chan->lc_proto = proto;
76 1.1.2.2 yamt chan->lc_upper = upper;
77 1.1.2.2 yamt
78 1.1.2.2 yamt chan->lc_state = L2CAP_CLOSED;
79 1.1.2.2 yamt
80 1.1.2.2 yamt chan->lc_lcid = L2CAP_NULL_CID;
81 1.1.2.2 yamt chan->lc_rcid = L2CAP_NULL_CID;
82 1.1.2.2 yamt
83 1.1.2.2 yamt chan->lc_laddr.bt_len = sizeof(struct sockaddr_bt);
84 1.1.2.2 yamt chan->lc_laddr.bt_family = AF_BLUETOOTH;
85 1.1.2.2 yamt chan->lc_laddr.bt_psm = L2CAP_PSM_ANY;
86 1.1.2.2 yamt
87 1.1.2.2 yamt chan->lc_raddr.bt_len = sizeof(struct sockaddr_bt);
88 1.1.2.2 yamt chan->lc_raddr.bt_family = AF_BLUETOOTH;
89 1.1.2.2 yamt chan->lc_raddr.bt_psm = L2CAP_PSM_ANY;
90 1.1.2.2 yamt
91 1.1.2.2 yamt chan->lc_imtu = L2CAP_MTU_DEFAULT;
92 1.1.2.2 yamt chan->lc_omtu = L2CAP_MTU_DEFAULT;
93 1.1.2.2 yamt chan->lc_flush = L2CAP_FLUSH_TIMO_DEFAULT;
94 1.1.2.2 yamt
95 1.1.2.2 yamt memcpy(&chan->lc_iqos, &l2cap_default_qos, sizeof(l2cap_qos_t));
96 1.1.2.2 yamt memcpy(&chan->lc_oqos, &l2cap_default_qos, sizeof(l2cap_qos_t));
97 1.1.2.2 yamt
98 1.1.2.2 yamt MBUFQ_INIT(&chan->lc_txq);
99 1.1.2.2 yamt
100 1.1.2.2 yamt *handle = chan;
101 1.1.2.2 yamt return 0;
102 1.1.2.2 yamt }
103 1.1.2.2 yamt
104 1.1.2.2 yamt /*
105 1.1.2.2 yamt * l2cap_bind(l2cap_channel, sockaddr)
106 1.1.2.2 yamt *
107 1.1.2.2 yamt * set local address of channel
108 1.1.2.2 yamt */
109 1.1.2.2 yamt int
110 1.1.2.2 yamt l2cap_bind(struct l2cap_channel *chan, struct sockaddr_bt *addr)
111 1.1.2.2 yamt {
112 1.1.2.2 yamt
113 1.1.2.2 yamt memcpy(&chan->lc_laddr, addr, sizeof(struct sockaddr_bt));
114 1.1.2.2 yamt return 0;
115 1.1.2.2 yamt }
116 1.1.2.2 yamt
117 1.1.2.2 yamt /*
118 1.1.2.2 yamt * l2cap_sockaddr(l2cap_channel, sockaddr)
119 1.1.2.2 yamt *
120 1.1.2.2 yamt * get local address of channel
121 1.1.2.2 yamt */
122 1.1.2.2 yamt int
123 1.1.2.2 yamt l2cap_sockaddr(struct l2cap_channel *chan, struct sockaddr_bt *addr)
124 1.1.2.2 yamt {
125 1.1.2.2 yamt
126 1.1.2.2 yamt memcpy(addr, &chan->lc_laddr, sizeof(struct sockaddr_bt));
127 1.1.2.2 yamt return 0;
128 1.1.2.2 yamt }
129 1.1.2.2 yamt
130 1.1.2.2 yamt /*
131 1.1.2.2 yamt * l2cap_connect(l2cap_channel, sockaddr)
132 1.1.2.2 yamt *
133 1.1.2.2 yamt * Initiate a connection to destination. This corresponds to
134 1.1.2.2 yamt * "Open Channel Request" in the L2CAP specification and will
135 1.1.2.2 yamt * result in one of the following:
136 1.1.2.2 yamt *
137 1.1.2.2 yamt * proto->connected(upper)
138 1.1.2.2 yamt * proto->disconnected(upper, error)
139 1.1.2.2 yamt *
140 1.1.2.2 yamt * and, optionally
141 1.1.2.2 yamt * proto->connecting(upper)
142 1.1.2.2 yamt */
143 1.1.2.2 yamt int
144 1.1.2.2 yamt l2cap_connect(struct l2cap_channel *chan, struct sockaddr_bt *dest)
145 1.1.2.2 yamt {
146 1.1.2.2 yamt struct hci_unit *unit;
147 1.1.2.2 yamt int err;
148 1.1.2.2 yamt
149 1.1.2.2 yamt memcpy(&chan->lc_raddr, dest, sizeof(struct sockaddr_bt));
150 1.1.2.2 yamt
151 1.1.2.2 yamt if (L2CAP_PSM_INVALID(chan->lc_raddr.bt_psm))
152 1.1.2.2 yamt return EINVAL;
153 1.1.2.2 yamt
154 1.1.2.2 yamt if (bdaddr_any(&chan->lc_raddr.bt_bdaddr))
155 1.1.2.2 yamt return EDESTADDRREQ;
156 1.1.2.2 yamt
157 1.1.2.2 yamt /* set local address if it needs setting */
158 1.1.2.2 yamt if (bdaddr_any(&chan->lc_laddr.bt_bdaddr)) {
159 1.1.2.2 yamt err = hci_route_lookup(&chan->lc_laddr.bt_bdaddr,
160 1.1.2.2 yamt &chan->lc_raddr.bt_bdaddr);
161 1.1.2.2 yamt if (err)
162 1.1.2.2 yamt return err;
163 1.1.2.2 yamt }
164 1.1.2.2 yamt
165 1.1.2.2 yamt unit = hci_unit_lookup(&chan->lc_laddr.bt_bdaddr);
166 1.1.2.2 yamt if (unit == NULL)
167 1.1.2.2 yamt return EHOSTUNREACH;
168 1.1.2.2 yamt
169 1.1.2.2 yamt /* attach to active list */
170 1.1.2.2 yamt err = l2cap_cid_alloc(chan);
171 1.1.2.2 yamt if (err)
172 1.1.2.2 yamt return err;
173 1.1.2.2 yamt
174 1.1.2.2 yamt /* open link to remote device */
175 1.1.2.2 yamt chan->lc_link = hci_acl_open(unit, &chan->lc_raddr.bt_bdaddr);
176 1.1.2.2 yamt if (chan->lc_link == NULL)
177 1.1.2.2 yamt return EHOSTUNREACH;
178 1.1.2.2 yamt
179 1.1.2.3 yamt /* set the link mode */
180 1.1.2.3 yamt err = l2cap_setmode(chan);
181 1.1.2.3 yamt if (err == EINPROGRESS) {
182 1.1.2.3 yamt chan->lc_state = L2CAP_WAIT_SEND_CONNECT_REQ;
183 1.1.2.3 yamt (*chan->lc_proto->connecting)(chan->lc_upper);
184 1.1.2.3 yamt return 0;
185 1.1.2.3 yamt }
186 1.1.2.3 yamt if (err)
187 1.1.2.3 yamt goto fail;
188 1.1.2.3 yamt
189 1.1.2.2 yamt /*
190 1.1.2.3 yamt * We can queue a connect request now even though the link may
191 1.1.2.3 yamt * not yet be open; Our mode setting is assured, and the queue
192 1.1.2.3 yamt * will be started automatically at the right time.
193 1.1.2.2 yamt */
194 1.1.2.3 yamt chan->lc_state = L2CAP_WAIT_RECV_CONNECT_RSP;
195 1.1.2.2 yamt err = l2cap_send_connect_req(chan);
196 1.1.2.3 yamt if (err)
197 1.1.2.3 yamt goto fail;
198 1.1.2.2 yamt
199 1.1.2.2 yamt return 0;
200 1.1.2.3 yamt
201 1.1.2.3 yamt fail:
202 1.1.2.3 yamt chan->lc_state = L2CAP_CLOSED;
203 1.1.2.3 yamt hci_acl_close(chan->lc_link, err);
204 1.1.2.3 yamt chan->lc_link = NULL;
205 1.1.2.3 yamt return err;
206 1.1.2.2 yamt }
207 1.1.2.2 yamt
208 1.1.2.2 yamt /*
209 1.1.2.2 yamt * l2cap_peeraddr(l2cap_channel, sockaddr)
210 1.1.2.2 yamt *
211 1.1.2.2 yamt * get remote address of channel
212 1.1.2.2 yamt */
213 1.1.2.2 yamt int
214 1.1.2.2 yamt l2cap_peeraddr(struct l2cap_channel *chan, struct sockaddr_bt *addr)
215 1.1.2.2 yamt {
216 1.1.2.2 yamt
217 1.1.2.2 yamt memcpy(addr, &chan->lc_raddr, sizeof(struct sockaddr_bt));
218 1.1.2.2 yamt return 0;
219 1.1.2.2 yamt }
220 1.1.2.2 yamt
221 1.1.2.2 yamt /*
222 1.1.2.2 yamt * l2cap_disconnect(l2cap_channel, linger)
223 1.1.2.2 yamt *
224 1.1.2.2 yamt * Initiate L2CAP disconnection. This corresponds to
225 1.1.2.2 yamt * "Close Channel Request" in the L2CAP specification
226 1.1.2.2 yamt * and will result in a call to
227 1.1.2.2 yamt *
228 1.1.2.2 yamt * proto->disconnected(upper, error)
229 1.1.2.2 yamt *
230 1.1.2.2 yamt * when the disconnection is complete. If linger is set,
231 1.1.2.2 yamt * the call will not be made until data has flushed from
232 1.1.2.2 yamt * the queue.
233 1.1.2.2 yamt */
234 1.1.2.2 yamt int
235 1.1.2.2 yamt l2cap_disconnect(struct l2cap_channel *chan, int linger)
236 1.1.2.2 yamt {
237 1.1.2.2 yamt int err = 0;
238 1.1.2.2 yamt
239 1.1.2.3 yamt if (chan->lc_state == L2CAP_CLOSED
240 1.1.2.3 yamt || chan->lc_state == L2CAP_WAIT_DISCONNECT)
241 1.1.2.2 yamt return EINVAL;
242 1.1.2.2 yamt
243 1.1.2.2 yamt chan->lc_flags |= L2CAP_SHUTDOWN;
244 1.1.2.2 yamt
245 1.1.2.2 yamt /*
246 1.1.2.2 yamt * no need to do anything unless the queue is empty or
247 1.1.2.2 yamt * we are not lingering..
248 1.1.2.2 yamt */
249 1.1.2.2 yamt if ((MBUFQ_FIRST(&chan->lc_txq) == NULL && chan->lc_pending == 0)
250 1.1.2.2 yamt || linger == 0) {
251 1.1.2.2 yamt chan->lc_state = L2CAP_WAIT_DISCONNECT;
252 1.1.2.2 yamt err = l2cap_send_disconnect_req(chan);
253 1.1.2.2 yamt if (err)
254 1.1.2.2 yamt l2cap_close(chan, err);
255 1.1.2.2 yamt }
256 1.1.2.2 yamt return err;
257 1.1.2.2 yamt }
258 1.1.2.2 yamt
259 1.1.2.2 yamt /*
260 1.1.2.2 yamt * l2cap_detach(handle)
261 1.1.2.2 yamt *
262 1.1.2.2 yamt * Detach l2cap channel from handle & close it down
263 1.1.2.2 yamt */
264 1.1.2.2 yamt int
265 1.1.2.2 yamt l2cap_detach(struct l2cap_channel **handle)
266 1.1.2.2 yamt {
267 1.1.2.2 yamt struct l2cap_channel *chan;
268 1.1.2.2 yamt
269 1.1.2.2 yamt chan = *handle;
270 1.1.2.2 yamt *handle = NULL;
271 1.1.2.2 yamt
272 1.1.2.2 yamt if (chan->lc_state != L2CAP_CLOSED)
273 1.1.2.2 yamt l2cap_close(chan, 0);
274 1.1.2.2 yamt
275 1.1.2.2 yamt if (chan->lc_lcid != L2CAP_NULL_CID) {
276 1.1.2.2 yamt LIST_REMOVE(chan, lc_ncid);
277 1.1.2.2 yamt chan->lc_lcid = L2CAP_NULL_CID;
278 1.1.2.2 yamt }
279 1.1.2.2 yamt
280 1.1.2.2 yamt MBUFQ_DRAIN(&chan->lc_txq);
281 1.1.2.2 yamt
282 1.1.2.2 yamt /*
283 1.1.2.2 yamt * Could implement some kind of delayed expunge to make sure that the
284 1.1.2.2 yamt * CID is really dead before it becomes available for reuse?
285 1.1.2.2 yamt */
286 1.1.2.2 yamt
287 1.1.2.2 yamt free(chan, M_BLUETOOTH);
288 1.1.2.2 yamt return 0;
289 1.1.2.2 yamt }
290 1.1.2.2 yamt
291 1.1.2.2 yamt /*
292 1.1.2.2 yamt * l2cap_listen(l2cap_channel)
293 1.1.2.2 yamt *
294 1.1.2.2 yamt * Use this channel as a listening post (until detached). This will
295 1.1.2.2 yamt * result in calls to:
296 1.1.2.2 yamt *
297 1.1.2.2 yamt * proto->newconn(upper, laddr, raddr)
298 1.1.2.2 yamt *
299 1.1.2.2 yamt * for incoming connections matching the psm and local address of the
300 1.1.2.2 yamt * channel (NULL psm/address are permitted and match any protocol/device).
301 1.1.2.2 yamt *
302 1.1.2.2 yamt * The upper layer should create and return a new channel.
303 1.1.2.2 yamt *
304 1.1.2.2 yamt * You cannot use this channel for anything else subsequent to this call
305 1.1.2.2 yamt */
306 1.1.2.2 yamt int
307 1.1.2.2 yamt l2cap_listen(struct l2cap_channel *chan)
308 1.1.2.2 yamt {
309 1.1.2.2 yamt struct l2cap_channel *used, *prev = NULL;
310 1.1.2.2 yamt
311 1.1.2.2 yamt if (chan->lc_lcid != L2CAP_NULL_CID)
312 1.1.2.2 yamt return EINVAL;
313 1.1.2.2 yamt
314 1.1.2.2 yamt if (chan->lc_laddr.bt_psm != L2CAP_PSM_ANY
315 1.1.2.2 yamt && L2CAP_PSM_INVALID(chan->lc_laddr.bt_psm))
316 1.1.2.2 yamt return EADDRNOTAVAIL;
317 1.1.2.2 yamt
318 1.1.2.2 yamt /*
319 1.1.2.2 yamt * This CID is irrelevant, as the channel is not stored on the active
320 1.1.2.2 yamt * list and the socket code does not allow operations on listening
321 1.1.2.2 yamt * sockets, but we set it so the detach code knows to LIST_REMOVE the
322 1.1.2.2 yamt * channel.
323 1.1.2.2 yamt */
324 1.1.2.2 yamt chan->lc_lcid = L2CAP_SIGNAL_CID;
325 1.1.2.2 yamt
326 1.1.2.2 yamt /*
327 1.1.2.2 yamt * The list of listening channels is stored in an order such that new
328 1.1.2.2 yamt * listeners dont usurp current listeners, but that specific listening
329 1.1.2.2 yamt * takes precedence over promiscuous, and the connect request code can
330 1.1.2.2 yamt * easily use the first matching entry.
331 1.1.2.2 yamt */
332 1.1.2.2 yamt LIST_FOREACH(used, &l2cap_listen_list, lc_ncid) {
333 1.1.2.2 yamt if (used->lc_laddr.bt_psm < chan->lc_laddr.bt_psm)
334 1.1.2.2 yamt break;
335 1.1.2.2 yamt
336 1.1.2.2 yamt if (used->lc_laddr.bt_psm == chan->lc_laddr.bt_psm
337 1.1.2.2 yamt && bdaddr_any(&used->lc_laddr.bt_bdaddr)
338 1.1.2.2 yamt && !bdaddr_any(&chan->lc_laddr.bt_bdaddr))
339 1.1.2.2 yamt break;
340 1.1.2.2 yamt
341 1.1.2.2 yamt prev = used;
342 1.1.2.2 yamt }
343 1.1.2.2 yamt
344 1.1.2.2 yamt if (prev == NULL)
345 1.1.2.2 yamt LIST_INSERT_HEAD(&l2cap_listen_list, chan, lc_ncid);
346 1.1.2.2 yamt else
347 1.1.2.2 yamt LIST_INSERT_AFTER(prev, chan, lc_ncid);
348 1.1.2.2 yamt
349 1.1.2.2 yamt return 0;
350 1.1.2.2 yamt }
351 1.1.2.2 yamt
352 1.1.2.2 yamt /*
353 1.1.2.2 yamt * l2cap_send(l2cap_channel, mbuf)
354 1.1.2.2 yamt *
355 1.1.2.2 yamt * Output SDU on channel described by channel. This corresponds
356 1.1.2.2 yamt * to "Send Data Request" in the L2CAP specification. The upper
357 1.1.2.2 yamt * layer will be notified when SDU's have completed sending by a
358 1.1.2.2 yamt * call to:
359 1.1.2.2 yamt *
360 1.1.2.2 yamt * proto->complete(upper, n)
361 1.1.2.2 yamt *
362 1.1.2.2 yamt * (currently n == 1)
363 1.1.2.2 yamt *
364 1.1.2.2 yamt * Note: I'm not sure how this will work out, but I think that
365 1.1.2.2 yamt * if outgoing Retransmission Mode or Flow Control Mode is
366 1.1.2.2 yamt * negotiated then this call will not be made until the SDU has
367 1.1.2.2 yamt * been acknowleged by the peer L2CAP entity. For 'Best Effort'
368 1.1.2.2 yamt * it will be made when the packet has cleared the controller
369 1.1.2.2 yamt * buffers.
370 1.1.2.2 yamt *
371 1.1.2.2 yamt * We only support Basic mode so far, so encapsulate with a
372 1.1.2.2 yamt * B-Frame header and start sending if we are not already
373 1.1.2.2 yamt */
374 1.1.2.2 yamt int
375 1.1.2.2 yamt l2cap_send(struct l2cap_channel *chan, struct mbuf *m)
376 1.1.2.2 yamt {
377 1.1.2.2 yamt l2cap_hdr_t *hdr;
378 1.1.2.2 yamt int plen;
379 1.1.2.2 yamt
380 1.1.2.2 yamt if (chan->lc_state == L2CAP_CLOSED) {
381 1.1.2.2 yamt m_freem(m);
382 1.1.2.2 yamt return ENOTCONN;
383 1.1.2.2 yamt }
384 1.1.2.2 yamt
385 1.1.2.2 yamt plen = m->m_pkthdr.len;
386 1.1.2.2 yamt
387 1.1.2.2 yamt DPRINTFN(5, "send %d bytes on CID #%d (pending = %d)\n",
388 1.1.2.2 yamt plen, chan->lc_lcid, chan->lc_pending);
389 1.1.2.2 yamt
390 1.1.2.2 yamt /* Encapsulate with B-Frame */
391 1.1.2.2 yamt M_PREPEND(m, sizeof(l2cap_hdr_t), M_DONTWAIT);
392 1.1.2.2 yamt if (m == NULL)
393 1.1.2.2 yamt return ENOMEM;
394 1.1.2.2 yamt
395 1.1.2.2 yamt hdr = mtod(m, l2cap_hdr_t *);
396 1.1.2.2 yamt hdr->length = htole16(plen);
397 1.1.2.2 yamt hdr->dcid = htole16(chan->lc_rcid);
398 1.1.2.2 yamt
399 1.1.2.2 yamt /* Queue it on our list */
400 1.1.2.2 yamt MBUFQ_ENQUEUE(&chan->lc_txq, m);
401 1.1.2.2 yamt
402 1.1.2.2 yamt /* If we are not sending, then start doing so */
403 1.1.2.2 yamt if (chan->lc_pending == 0)
404 1.1.2.2 yamt return l2cap_start(chan);
405 1.1.2.2 yamt
406 1.1.2.2 yamt return 0;
407 1.1.2.2 yamt }
408 1.1.2.2 yamt
409 1.1.2.2 yamt /*
410 1.1.2.3 yamt * l2cap_setopt(l2cap_channel, opt, addr)
411 1.1.2.2 yamt *
412 1.1.2.2 yamt * Apply configuration options to channel. This corresponds to
413 1.1.2.2 yamt * "Configure Channel Request" in the L2CAP specification.
414 1.1.2.3 yamt *
415 1.1.2.3 yamt * for SO_L2CAP_LM, the settings will take effect when the
416 1.1.2.3 yamt * channel is established. If the channel is already open,
417 1.1.2.3 yamt * a call to
418 1.1.2.3 yamt * proto->linkmode(upper, new)
419 1.1.2.3 yamt *
420 1.1.2.3 yamt * will be made when the change is complete.
421 1.1.2.2 yamt */
422 1.1.2.2 yamt int
423 1.1.2.2 yamt l2cap_setopt(struct l2cap_channel *chan, int opt, void *addr)
424 1.1.2.2 yamt {
425 1.1.2.3 yamt int mode, err = 0;
426 1.1.2.3 yamt uint16_t mtu;
427 1.1.2.2 yamt
428 1.1.2.2 yamt switch (opt) {
429 1.1.2.2 yamt case SO_L2CAP_IMTU: /* set Incoming MTU */
430 1.1.2.3 yamt mtu = *(uint16_t *)addr;
431 1.1.2.3 yamt if (mtu < L2CAP_MTU_MINIMUM)
432 1.1.2.2 yamt err = EINVAL;
433 1.1.2.3 yamt else if (chan->lc_state == L2CAP_CLOSED)
434 1.1.2.3 yamt chan->lc_imtu = mtu;
435 1.1.2.3 yamt else
436 1.1.2.3 yamt err = EBUSY;
437 1.1.2.2 yamt
438 1.1.2.2 yamt break;
439 1.1.2.2 yamt
440 1.1.2.3 yamt case SO_L2CAP_LM: /* set link mode */
441 1.1.2.3 yamt mode = *(int *)addr;
442 1.1.2.3 yamt mode &= (L2CAP_LM_SECURE | L2CAP_LM_ENCRYPT | L2CAP_LM_AUTH);
443 1.1.2.2 yamt
444 1.1.2.3 yamt if (mode & L2CAP_LM_SECURE)
445 1.1.2.3 yamt mode |= L2CAP_LM_ENCRYPT;
446 1.1.2.2 yamt
447 1.1.2.3 yamt if (mode & L2CAP_LM_ENCRYPT)
448 1.1.2.3 yamt mode |= L2CAP_LM_AUTH;
449 1.1.2.3 yamt
450 1.1.2.3 yamt chan->lc_mode = mode;
451 1.1.2.3 yamt
452 1.1.2.3 yamt if (chan->lc_state == L2CAP_OPEN)
453 1.1.2.3 yamt err = l2cap_setmode(chan);
454 1.1.2.3 yamt
455 1.1.2.3 yamt break;
456 1.1.2.3 yamt
457 1.1.2.3 yamt case SO_L2CAP_OQOS: /* set Outgoing QoS flow spec */
458 1.1.2.3 yamt case SO_L2CAP_FLUSH: /* set Outgoing Flush Timeout */
459 1.1.2.2 yamt default:
460 1.1.2.3 yamt err = ENOPROTOOPT;
461 1.1.2.2 yamt break;
462 1.1.2.2 yamt }
463 1.1.2.2 yamt
464 1.1.2.2 yamt return err;
465 1.1.2.2 yamt }
466 1.1.2.2 yamt
467 1.1.2.3 yamt /*
468 1.1.2.3 yamt * l2cap_getopt(l2cap_channel, opt, addr)
469 1.1.2.3 yamt *
470 1.1.2.3 yamt * Return configuration parameters.
471 1.1.2.3 yamt */
472 1.1.2.2 yamt int
473 1.1.2.2 yamt l2cap_getopt(struct l2cap_channel *chan, int opt, void *addr)
474 1.1.2.2 yamt {
475 1.1.2.2 yamt
476 1.1.2.2 yamt switch (opt) {
477 1.1.2.2 yamt case SO_L2CAP_IMTU: /* get Incoming MTU */
478 1.1.2.2 yamt *(uint16_t *)addr = chan->lc_imtu;
479 1.1.2.2 yamt return sizeof(uint16_t);
480 1.1.2.2 yamt
481 1.1.2.2 yamt case SO_L2CAP_OMTU: /* get Outgoing MTU */
482 1.1.2.2 yamt *(uint16_t *)addr = chan->lc_omtu;
483 1.1.2.2 yamt return sizeof(uint16_t);
484 1.1.2.2 yamt
485 1.1.2.2 yamt case SO_L2CAP_IQOS: /* get Incoming QoS flow spec */
486 1.1.2.2 yamt memcpy(addr, &chan->lc_iqos, sizeof(l2cap_qos_t));
487 1.1.2.2 yamt return sizeof(l2cap_qos_t);
488 1.1.2.2 yamt
489 1.1.2.2 yamt case SO_L2CAP_OQOS: /* get Outgoing QoS flow spec */
490 1.1.2.2 yamt memcpy(addr, &chan->lc_oqos, sizeof(l2cap_qos_t));
491 1.1.2.2 yamt return sizeof(l2cap_qos_t);
492 1.1.2.2 yamt
493 1.1.2.2 yamt case SO_L2CAP_FLUSH: /* get Flush Timeout */
494 1.1.2.2 yamt *(uint16_t *)addr = chan->lc_flush;
495 1.1.2.2 yamt return sizeof(uint16_t);
496 1.1.2.2 yamt
497 1.1.2.3 yamt case SO_L2CAP_LM: /* get link mode */
498 1.1.2.3 yamt *(int *)addr = chan->lc_mode;
499 1.1.2.3 yamt return sizeof(int);
500 1.1.2.3 yamt
501 1.1.2.2 yamt default:
502 1.1.2.2 yamt break;
503 1.1.2.2 yamt }
504 1.1.2.2 yamt
505 1.1.2.2 yamt return 0;
506 1.1.2.2 yamt }
507