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: Mon Dec 13 13:41:12 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
)
47 IRDA_DEBUG(4, __FUNCTION__
"(), sending expedited data\n");
48 irlap_data_request(self
->irlap
, skb
, TRUE
);
50 irlap_data_request(self
->irlap
, skb
, FALSE
);
54 * Function irlmp_send_lcf_pdu (dlsap, slsap, opcode,skb)
56 * Send Link Control Frame to IrLAP
58 void irlmp_send_lcf_pdu(struct lap_cb
*self
, __u8 dlsap
, __u8 slsap
,
59 __u8 opcode
, struct sk_buff
*skb
)
63 IRDA_DEBUG(2, __FUNCTION__
"()\n");
65 ASSERT(self
!= NULL
, return;);
66 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
67 ASSERT(skb
!= NULL
, return;);
71 frame
[0] = dlsap
| CONTROL_BIT
;
76 if (opcode
== DISCONNECT
)
77 frame
[3] = 0x01; /* Service user request */
79 frame
[3] = 0x00; /* rsvd */
81 irlap_data_request(self
->irlap
, skb
, FALSE
);
85 * Function irlmp_input (skb)
87 * Used by IrLAP to pass received data frames to IrLMP layer
90 void irlmp_link_data_indication(struct lap_cb
*self
, struct sk_buff
*skb
,
94 __u8 slsap_sel
; /* Source (this) LSAP address */
95 __u8 dlsap_sel
; /* Destination LSAP address */
98 IRDA_DEBUG(4, __FUNCTION__
"()\n");
100 ASSERT(self
!= NULL
, return;);
101 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
102 ASSERT(skb
->len
> 2, return;);
107 * The next statements may be confusing, but we do this so that
108 * destination LSAP of received frame is source LSAP in our view
110 slsap_sel
= fp
[0] & LSAP_MASK
;
114 * Check if this is an incoming connection, since we must deal with
115 * it in a different way than other established connections.
117 if ((fp
[0] & CONTROL_BIT
) && (fp
[2] == CONNECT_CMD
)) {
118 IRDA_DEBUG(3, __FUNCTION__
"(), incoming connection, "
119 "source LSAP=%d, dest LSAP=%d\n",
120 slsap_sel
, dlsap_sel
);
122 /* Try to find LSAP among the unconnected LSAPs */
123 lsap
= irlmp_find_lsap(self
, dlsap_sel
, slsap_sel
, CONNECT_CMD
,
124 irlmp
->unconnected_lsaps
);
126 /* Maybe LSAP was already connected, so try one more time */
128 lsap
= irlmp_find_lsap(self
, dlsap_sel
, slsap_sel
, 0,
131 lsap
= irlmp_find_lsap(self
, dlsap_sel
, slsap_sel
, 0,
135 IRDA_DEBUG(2, "IrLMP, Sorry, no LSAP for received frame!\n");
136 IRDA_DEBUG(2, __FUNCTION__
137 "(), slsap_sel = %02x, dlsap_sel = %02x\n", slsap_sel
,
139 if (fp
[0] & CONTROL_BIT
) {
140 IRDA_DEBUG(2, __FUNCTION__
141 "(), received control frame %02x\n", fp
[2]);
143 IRDA_DEBUG(2, __FUNCTION__
"(), received data frame\n");
150 * Check if we received a control frame?
152 if (fp
[0] & CONTROL_BIT
) {
156 irlmp_do_lsap_event(lsap
, LM_CONNECT_INDICATION
, skb
);
159 irlmp_do_lsap_event(lsap
, LM_CONNECT_CONFIRM
, skb
);
162 IRDA_DEBUG(4, __FUNCTION__
163 "(), Disconnect indication!\n");
164 irlmp_do_lsap_event(lsap
, LM_DISCONNECT_INDICATION
,
168 IRDA_DEBUG(0, "Access mode cmd not implemented!\n");
172 IRDA_DEBUG(0, "Access mode cnf not implemented!\n");
176 IRDA_DEBUG(0, __FUNCTION__
177 "(), Unknown control frame %02x\n", fp
[2]);
181 } else if (unreliable
) {
182 /* Optimize and bypass the state machine if possible */
183 if (lsap
->lsap_state
== LSAP_DATA_TRANSFER_READY
)
184 irlmp_udata_indication(lsap
, skb
);
186 irlmp_do_lsap_event(lsap
, LM_UDATA_INDICATION
, skb
);
188 /* Optimize and bypass the state machine if possible */
189 if (lsap
->lsap_state
== LSAP_DATA_TRANSFER_READY
)
190 irlmp_data_indication(lsap
, skb
);
192 irlmp_do_lsap_event(lsap
, LM_DATA_INDICATION
, skb
);
197 * Function irlmp_link_unitdata_indication (self, skb)
202 #ifdef CONFIG_IRDA_ULTRA
203 void irlmp_link_unitdata_indication(struct lap_cb
*self
, struct sk_buff
*skb
)
205 struct lsap_cb
*lsap
;
206 __u8 slsap_sel
; /* Source (this) LSAP address */
207 __u8 dlsap_sel
; /* Destination LSAP address */
208 __u8 pid
; /* Protocol identifier */
211 IRDA_DEBUG(4, __FUNCTION__
"()\n");
213 ASSERT(self
!= NULL
, return;);
214 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
215 ASSERT(skb
->len
> 2, return;);
220 * The next statements may be confusing, but we do this so that
221 * destination LSAP of received frame is source LSAP in our view
223 slsap_sel
= fp
[0] & LSAP_MASK
;
228 IRDA_DEBUG(0, __FUNCTION__
"(), extension in PID not supp!\n");
234 /* Check if frame is addressed to the connectionless LSAP */
235 if ((slsap_sel
!= LSAP_CONNLESS
) || (dlsap_sel
!= LSAP_CONNLESS
)) {
236 IRDA_DEBUG(0, __FUNCTION__
"(), dropping frame!\n");
242 lsap
= (struct lsap_cb
*) hashbin_get_first(irlmp
->unconnected_lsaps
);
243 while (lsap
!= NULL
) {
245 * Check if source LSAP and dest LSAP selectors and PID match.
247 if ((lsap
->slsap_sel
== slsap_sel
) &&
248 (lsap
->dlsap_sel
== dlsap_sel
) &&
253 lsap
= (struct lsap_cb
*) hashbin_get_next(irlmp
->unconnected_lsaps
);
256 irlmp_connless_data_indication(lsap
, skb
);
258 IRDA_DEBUG(0, __FUNCTION__
"(), found no matching LSAP!\n");
262 #endif /* CONFIG_IRDA_ULTRA */
265 * Function irlmp_link_disconnect_indication (reason, userdata)
267 * IrLAP has disconnected
270 void irlmp_link_disconnect_indication(struct lap_cb
*lap
,
271 struct irlap_cb
*irlap
,
273 struct sk_buff
*userdata
)
275 IRDA_DEBUG(2, __FUNCTION__
"()\n");
277 ASSERT(lap
!= NULL
, return;);
278 ASSERT(lap
->magic
== LMP_LAP_MAGIC
, return;);
280 lap
->reason
= reason
;
281 lap
->daddr
= DEV_ADDR_ANY
;
283 /* FIXME: must do something with the userdata if any */
285 dev_kfree_skb(userdata
);
288 * Inform station state machine
290 irlmp_do_lap_event(lap
, LM_LAP_DISCONNECT_INDICATION
, NULL
);
294 * Function irlmp_link_connect_indication (qos)
296 * Incoming LAP connection!
299 void irlmp_link_connect_indication(struct lap_cb
*self
, __u32 saddr
,
300 __u32 daddr
, struct qos_info
*qos
,
303 IRDA_DEBUG(4, __FUNCTION__
"()\n");
305 /* Copy QoS settings for this session */
308 /* Update destination device address */
310 ASSERT(self
->saddr
== saddr
, return;);
312 irlmp_do_lap_event(self
, LM_LAP_CONNECT_INDICATION
, skb
);
316 * Function irlmp_link_connect_confirm (qos)
318 * LAP connection confirmed!
321 void irlmp_link_connect_confirm(struct lap_cb
*self
, struct qos_info
*qos
,
322 struct sk_buff
*userdata
)
324 IRDA_DEBUG(4, __FUNCTION__
"()\n");
326 ASSERT(self
!= NULL
, return;);
327 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
328 ASSERT(qos
!= NULL
, return;);
330 /* Don't need use the userdata for now */
332 dev_kfree_skb(userdata
);
334 /* Copy QoS settings for this session */
337 irlmp_do_lap_event(self
, LM_LAP_CONNECT_CONFIRM
, NULL
);
341 * Function irlmp_link_discovery_indication (self, log)
343 * Device is discovering us
346 void irlmp_link_discovery_indication(struct lap_cb
*self
,
347 discovery_t
*discovery
)
349 ASSERT(self
!= NULL
, return;);
350 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
352 irlmp_add_discovery(irlmp
->cachelog
, discovery
);
354 #if 0 /* This will just cause a lot of connection collisions */
356 /* Just handle it the same way as a discovery confirm */
357 irlmp_do_lap_event(self
, LM_LAP_DISCOVERY_CONFIRM
, NULL
);
362 * Function irlmp_link_discovery_confirm (self, log)
364 * Called by IrLAP with a list of discoveries after the discovery
365 * request has been carried out. A NULL log is received if IrLAP
366 * was unable to carry out the discovery request
369 void irlmp_link_discovery_confirm(struct lap_cb
*self
, hashbin_t
*log
)
371 IRDA_DEBUG(4, __FUNCTION__
"()\n");
373 ASSERT(self
!= NULL
, return;);
374 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
376 irlmp_add_discovery_log(irlmp
->cachelog
, log
);
378 irlmp_do_lap_event(self
, LM_LAP_DISCOVERY_CONFIRM
, NULL
);
381 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
382 inline void irlmp_update_cache(struct lsap_cb
*self
)
384 /* Update cache entry */
385 irlmp
->cache
.dlsap_sel
= self
->dlsap_sel
;
386 irlmp
->cache
.slsap_sel
= self
->slsap_sel
;
387 irlmp
->cache
.lsap
= self
;
388 irlmp
->cache
.valid
= TRUE
;
393 * Function irlmp_find_handle (self, dlsap_sel, slsap_sel, status, queue)
395 * Find handle assosiated with destination and source LSAP
398 static struct lsap_cb
*irlmp_find_lsap(struct lap_cb
*self
, __u8 dlsap_sel
,
399 __u8 slsap_sel
, int status
,
402 struct lsap_cb
*lsap
;
405 * Optimize for the common case. We assume that the last frame
406 * received is in the same connection as the last one, so check in
407 * cache first to avoid the linear search
409 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
410 if ((irlmp
->cache
.valid
) &&
411 (irlmp
->cache
.slsap_sel
== slsap_sel
) &&
412 (irlmp
->cache
.dlsap_sel
== dlsap_sel
))
414 return (irlmp
->cache
.lsap
);
417 lsap
= (struct lsap_cb
*) hashbin_get_first(queue
);
418 while (lsap
!= NULL
) {
420 * If this is an incomming connection, then the destination
421 * LSAP selector may have been specified as LM_ANY so that
422 * any client can connect. In that case we only need to check
423 * if the source LSAP (in our view!) match!
425 if ((status
== CONNECT_CMD
) &&
426 (lsap
->slsap_sel
== slsap_sel
) &&
427 (lsap
->dlsap_sel
== LSAP_ANY
))
429 lsap
->dlsap_sel
= dlsap_sel
;
431 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
432 irlmp_update_cache(lsap
);
437 * Check if source LSAP and dest LSAP selectors match.
439 if ((lsap
->slsap_sel
== slsap_sel
) &&
440 (lsap
->dlsap_sel
== dlsap_sel
))
442 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
443 irlmp_update_cache(lsap
);
447 lsap
= (struct lsap_cb
*) hashbin_get_next(queue
);
450 /* Sorry not found! */