1 /*********************************************************************
3 * Filename: irlmp_frame.c
5 * Description: IrLMP frame implementation
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Tue Aug 19 02:09:59 1997
9 * Modified at: Sun May 9 21:00:05 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>
13 * All Rights Reserved.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * Neither Dag Brattli nor University of Tromsø admit liability nor
21 * provide warranty for any of this software. This material is
22 * provided "AS-IS" and at no charge.
24 ********************************************************************/
26 #include <linux/config.h>
27 #include <linux/skbuff.h>
28 #include <linux/kernel.h>
30 #include <net/irda/irda.h>
31 #include <net/irda/irlap.h>
32 #include <net/irda/timer.h>
33 #include <net/irda/irlmp.h>
34 #include <net/irda/irlmp_frame.h>
35 #include <net/irda/discovery.h>
37 static struct lsap_cb
*irlmp_find_lsap(struct lap_cb
*self
, __u8 dlsap
,
38 __u8 slsap
, int status
, hashbin_t
*);
40 inline void irlmp_send_data_pdu(struct lap_cb
*self
, __u8 dlsap
, __u8 slsap
,
41 int expedited
, struct sk_buff
*skb
)
51 DEBUG( 4, __FUNCTION__
"(), sending expedited data\n");
52 irlap_data_request( self
->irlap
, skb
, FALSE
);
54 irlap_data_request( self
->irlap
, skb
, TRUE
);
58 * Function irlmp_send_lcf_pdu (dlsap, slsap, opcode,skb)
60 * Send Link Control Frame to IrLAP
62 void irlmp_send_lcf_pdu(struct lap_cb
*self
, __u8 dlsap
, __u8 slsap
,
63 __u8 opcode
, struct sk_buff
*skb
)
67 DEBUG(4, __FUNCTION__
"()\n");
69 ASSERT(self
!= NULL
, return;);
70 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
71 ASSERT(skb
!= NULL
, return;);
75 frame
[0] = dlsap
| CONTROL_BIT
;
80 if (opcode
== DISCONNECT
)
81 frame
[3] = 0x01; /* Service user request */
83 frame
[3] = 0x00; /* rsvd */
85 ASSERT(self
->irlap
!= NULL
, return;);
86 irlap_data_request(self
->irlap
, skb
, TRUE
);
90 * Function irlmp_input (skb)
92 * Used by IrLAP to pass received data frames to IrLMP layer
95 void irlmp_link_data_indication(struct lap_cb
*self
, int reliable
,
99 __u8 slsap_sel
; /* Source (this) LSAP address */
100 __u8 dlsap_sel
; /* Destination LSAP address */
103 ASSERT(self
!= NULL
, return;);
104 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
105 ASSERT(skb
->len
> 2, return;);
110 * The next statements may be confusing, but we do this so that
111 * destination LSAP of received frame is source LSAP in our view
113 slsap_sel
= fp
[0] & LSAP_MASK
;
117 * Check if this is an incoming connection, since we must deal with
118 * it in a different way than other established connections.
120 if ((fp
[0] & CONTROL_BIT
) && (fp
[2] == CONNECT_CMD
)) {
121 DEBUG(3,"Incoming connection, source LSAP=%d, dest LSAP=%d\n",
122 slsap_sel
, dlsap_sel
);
124 /* Try to find LSAP among the unconnected LSAPs */
125 lsap
= irlmp_find_lsap(self
, dlsap_sel
, slsap_sel
, CONNECT_CMD
,
126 irlmp
->unconnected_lsaps
);
128 /* Maybe LSAP was already connected, so try one more time */
130 lsap
= irlmp_find_lsap(self
, dlsap_sel
, slsap_sel
, 0,
133 lsap
= irlmp_find_lsap(self
, dlsap_sel
, slsap_sel
, 0,
137 DEBUG(0, "IrLMP, Sorry, no LSAP for received frame!\n");
138 DEBUG(0, __FUNCTION__
139 "(), slsap_sel = %02x, dlsap_sel = %02x\n", slsap_sel
,
141 if (fp
[0] & CONTROL_BIT
) {
142 DEBUG(0, __FUNCTION__
143 "(), received control frame %02x\n", fp
[2]);
145 DEBUG(0, __FUNCTION__
"(), received data frame\n");
152 * Check if we received a control frame?
154 if (fp
[0] & CONTROL_BIT
) {
158 irlmp_do_lsap_event(lsap
, LM_CONNECT_INDICATION
, skb
);
161 irlmp_do_lsap_event(lsap
, LM_CONNECT_CONFIRM
, skb
);
164 DEBUG( 4, __FUNCTION__
"(), Disconnect indication!\n");
165 irlmp_do_lsap_event(lsap
, LM_DISCONNECT_INDICATION
,
169 DEBUG( 0, "Access mode cmd not implemented!\n");
172 DEBUG( 0, "Access mode cnf not implemented!\n");
175 DEBUG( 0, __FUNCTION__
176 "(), Unknown control frame %02x\n", fp
[2]);
179 } else if (reliable
== LAP_RELIABLE
) {
180 /* Optimize and bypass the state machine if possible */
181 if (lsap
->lsap_state
== LSAP_DATA_TRANSFER_READY
)
182 irlmp_data_indication(lsap
, skb
);
184 irlmp_do_lsap_event(lsap
, LM_DATA_INDICATION
, skb
);
185 } else if (reliable
== LAP_UNRELIABLE
) {
186 /* Optimize and bypass the state machine if possible */
187 if (lsap
->lsap_state
== LSAP_DATA_TRANSFER_READY
)
188 irlmp_data_indication(lsap
, skb
);
190 irlmp_do_lsap_event(lsap
, LM_UDATA_INDICATION
, skb
);
195 * Function irlmp_link_disconnect_indication (reason, userdata)
197 * IrLAP has disconnected
200 void irlmp_link_disconnect_indication(struct lap_cb
*lap
,
201 struct irlap_cb
*irlap
,
203 struct sk_buff
*userdata
)
205 DEBUG(2, __FUNCTION__
"()\n");
207 ASSERT(lap
!= NULL
, return;);
208 ASSERT(lap
->magic
== LMP_LAP_MAGIC
, return;);
210 lap
->reason
= reason
;
211 lap
->daddr
= DEV_ADDR_ANY
;
213 /* FIXME: must do something with the userdata if any */
216 * Inform station state machine
218 irlmp_do_lap_event(lap
, LM_LAP_DISCONNECT_INDICATION
, NULL
);
222 * Function irlmp_link_connect_indication (qos)
224 * Incoming LAP connection!
227 void irlmp_link_connect_indication(struct lap_cb
*self
, __u32 saddr
,
228 __u32 daddr
, struct qos_info
*qos
,
231 DEBUG(4, __FUNCTION__
"()\n");
233 /* Copy QoS settings for this session */
236 /* Update destination device address */
238 ASSERT(self
->saddr
== saddr
, return;);
240 irlmp_do_lap_event(self
, LM_LAP_CONNECT_INDICATION
, skb
);
244 * Function irlmp_link_connect_confirm (qos)
246 * LAP connection confirmed!
249 void irlmp_link_connect_confirm(struct lap_cb
*self
, struct qos_info
*qos
,
250 struct sk_buff
*userdata
)
252 DEBUG(4, __FUNCTION__
"()\n");
254 ASSERT(self
!= NULL
, return;);
255 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
256 ASSERT(qos
!= NULL
, return;);
258 /* Copy QoS settings for this session */
261 irlmp_do_lap_event(self
, LM_LAP_CONNECT_CONFIRM
, NULL
);
265 * Function irlmp_link_discovery_indication (self, log)
267 * Device is discovering us
270 void irlmp_link_discovery_indication(struct lap_cb
*self
,
271 discovery_t
*discovery
)
273 ASSERT(self
!= NULL
, return;);
274 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
276 irlmp_add_discovery(irlmp
->cachelog
, discovery
);
278 /* Just handle it the same way as a discovery confirm */
280 irlmp_do_lap_event(self
, LM_LAP_DISCOVERY_CONFIRM
, NULL
);
285 * Function irlmp_link_discovery_confirm (self, log)
287 * Called by IrLAP with a list of discoveries after the discovery
288 * request has been carried out. A NULL log is received if IrLAP
289 * was unable to carry out the discovery request
292 void irlmp_link_discovery_confirm(struct lap_cb
*self
, hashbin_t
*log
)
294 DEBUG(4, __FUNCTION__
"()\n");
296 ASSERT(self
!= NULL
, return;);
297 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
299 irlmp_add_discovery_log(irlmp
->cachelog
, log
);
301 irlmp_do_lap_event(self
, LM_LAP_DISCOVERY_CONFIRM
, NULL
);
304 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
305 inline void irlmp_update_cache(struct lsap_cb
*self
)
307 /* Update cache entry */
308 irlmp
->cache
.dlsap_sel
= self
->dlsap_sel
;
309 irlmp
->cache
.slsap_sel
= self
->slsap_sel
;
310 irlmp
->cache
.lsap
= self
;
311 irlmp
->cache
.valid
= TRUE
;
316 * Function irlmp_find_handle (self, dlsap_sel, slsap_sel, status, queue)
318 * Find handle assosiated with destination and source LSAP
321 static struct lsap_cb
*irlmp_find_lsap(struct lap_cb
*self
, __u8 dlsap_sel
,
322 __u8 slsap_sel
, int status
,
325 struct lsap_cb
*lsap
;
328 * Optimize for the common case. We assume that the last frame
329 * received is in the same connection as the last one, so check in
330 * cache first to avoid the linear search
332 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
333 if ((irlmp
->cache
.valid
) &&
334 (irlmp
->cache
.slsap_sel
== slsap_sel
) &&
335 (irlmp
->cache
.dlsap_sel
== dlsap_sel
))
337 return (irlmp
->cache
.lsap
);
340 lsap
= (struct lsap_cb
*) hashbin_get_first(queue
);
341 while (lsap
!= NULL
) {
343 * If this is an incomming connection, then the destination
344 * LSAP selector may have been specified as LM_ANY so that
345 * any client can connect. In that case we only need to check
346 * if the source LSAP (in our view!) match!
348 if ((status
== CONNECT_CMD
) &&
349 (lsap
->slsap_sel
== slsap_sel
) &&
350 (lsap
->dlsap_sel
== LSAP_ANY
))
352 lsap
->dlsap_sel
= dlsap_sel
;
354 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
355 irlmp_update_cache(lsap
);
360 * Check if source LSAP and dest LSAP selectors match.
362 if ((lsap
->slsap_sel
== slsap_sel
) &&
363 (lsap
->dlsap_sel
== dlsap_sel
))
365 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
366 irlmp_update_cache(lsap
);
370 lsap
= (struct lsap_cb
*) hashbin_get_next(queue
);
373 /* Sorry not found! */