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.
14 * Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * Neither Dag Brattli nor University of Tromsø admit liability nor
22 * provide warranty for any of this software. This material is
23 * provided "AS-IS" and at no charge.
25 ********************************************************************/
27 #include <linux/config.h>
28 #include <linux/skbuff.h>
29 #include <linux/kernel.h>
31 #include <net/irda/irda.h>
32 #include <net/irda/irlap.h>
33 #include <net/irda/timer.h>
34 #include <net/irda/irlmp.h>
35 #include <net/irda/irlmp_frame.h>
36 #include <net/irda/discovery.h>
38 static struct lsap_cb *irlmp_find_lsap(struct lap_cb *self, __u8 dlsap,
39 __u8 slsap, int status, hashbin_t *);
41 inline void irlmp_send_data_pdu(struct lap_cb *self, __u8 dlsap, __u8 slsap,
42 int expedited, struct sk_buff *skb)
48 IRDA_DEBUG(4, __FUNCTION__ "(), sending expedited data\n");
49 irlap_data_request(self->irlap, skb, TRUE);
51 irlap_data_request(self->irlap, skb, FALSE);
55 * Function irlmp_send_lcf_pdu (dlsap, slsap, opcode,skb)
57 * Send Link Control Frame to IrLAP
59 void irlmp_send_lcf_pdu(struct lap_cb *self, __u8 dlsap, __u8 slsap,
60 __u8 opcode, struct sk_buff *skb)
64 IRDA_DEBUG(2, __FUNCTION__ "()\n");
66 ASSERT(self != NULL, return;);
67 ASSERT(self->magic == LMP_LAP_MAGIC, return;);
68 ASSERT(skb != NULL, return;);
72 frame[0] = dlsap | CONTROL_BIT;
77 if (opcode == DISCONNECT)
78 frame[3] = 0x01; /* Service user request */
80 frame[3] = 0x00; /* rsvd */
82 irlap_data_request(self->irlap, skb, FALSE);
86 * Function irlmp_input (skb)
88 * Used by IrLAP to pass received data frames to IrLMP layer
91 void irlmp_link_data_indication(struct lap_cb *self, struct sk_buff *skb,
95 __u8 slsap_sel; /* Source (this) LSAP address */
96 __u8 dlsap_sel; /* Destination LSAP address */
99 IRDA_DEBUG(4, __FUNCTION__ "()\n");
101 ASSERT(self != NULL, return;);
102 ASSERT(self->magic == LMP_LAP_MAGIC, return;);
103 ASSERT(skb->len > 2, return;);
108 * The next statements may be confusing, but we do this so that
109 * destination LSAP of received frame is source LSAP in our view
111 slsap_sel = fp[0] & LSAP_MASK;
115 * Check if this is an incoming connection, since we must deal with
116 * it in a different way than other established connections.
118 if ((fp[0] & CONTROL_BIT) && (fp[2] == CONNECT_CMD)) {
119 IRDA_DEBUG(3, __FUNCTION__ "(), incoming connection, "
120 "source LSAP=%d, dest LSAP=%d\n",
121 slsap_sel, dlsap_sel);
123 /* Try to find LSAP among the unconnected LSAPs */
124 lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, CONNECT_CMD,
125 irlmp->unconnected_lsaps);
127 /* Maybe LSAP was already connected, so try one more time */
129 IRDA_DEBUG(1, __FUNCTION__ "(), incoming connection for LSAP already connected\n");
130 lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, 0,
134 lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, 0,
138 IRDA_DEBUG(2, "IrLMP, Sorry, no LSAP for received frame!\n");
139 IRDA_DEBUG(2, __FUNCTION__
140 "(), slsap_sel = %02x, dlsap_sel = %02x\n", slsap_sel,
142 if (fp[0] & CONTROL_BIT) {
143 IRDA_DEBUG(2, __FUNCTION__
144 "(), received control frame %02x\n", fp[2]);
146 IRDA_DEBUG(2, __FUNCTION__ "(), received data frame\n");
153 * Check if we received a control frame?
155 if (fp[0] & CONTROL_BIT) {
159 irlmp_do_lsap_event(lsap, LM_CONNECT_INDICATION, skb);
162 irlmp_do_lsap_event(lsap, LM_CONNECT_CONFIRM, skb);
165 IRDA_DEBUG(4, __FUNCTION__
166 "(), Disconnect indication!\n");
167 irlmp_do_lsap_event(lsap, LM_DISCONNECT_INDICATION,
171 IRDA_DEBUG(0, "Access mode cmd not implemented!\n");
175 IRDA_DEBUG(0, "Access mode cnf not implemented!\n");
179 IRDA_DEBUG(0, __FUNCTION__
180 "(), Unknown control frame %02x\n", fp[2]);
184 } else if (unreliable) {
185 /* Optimize and bypass the state machine if possible */
186 if (lsap->lsap_state == LSAP_DATA_TRANSFER_READY)
187 irlmp_udata_indication(lsap, skb);
189 irlmp_do_lsap_event(lsap, LM_UDATA_INDICATION, skb);
191 /* Optimize and bypass the state machine if possible */
192 if (lsap->lsap_state == LSAP_DATA_TRANSFER_READY)
193 irlmp_data_indication(lsap, skb);
195 irlmp_do_lsap_event(lsap, LM_DATA_INDICATION, skb);
200 * Function irlmp_link_unitdata_indication (self, skb)
205 #ifdef CONFIG_IRDA_ULTRA
206 void irlmp_link_unitdata_indication(struct lap_cb *self, struct sk_buff *skb)
208 struct lsap_cb *lsap;
209 __u8 slsap_sel; /* Source (this) LSAP address */
210 __u8 dlsap_sel; /* Destination LSAP address */
211 __u8 pid; /* Protocol identifier */
214 IRDA_DEBUG(4, __FUNCTION__ "()\n");
216 ASSERT(self != NULL, return;);
217 ASSERT(self->magic == LMP_LAP_MAGIC, return;);
218 ASSERT(skb->len > 2, return;);
223 * The next statements may be confusing, but we do this so that
224 * destination LSAP of received frame is source LSAP in our view
226 slsap_sel = fp[0] & LSAP_MASK;
231 IRDA_DEBUG(0, __FUNCTION__ "(), extension in PID not supp!\n");
237 /* Check if frame is addressed to the connectionless LSAP */
238 if ((slsap_sel != LSAP_CONNLESS) || (dlsap_sel != LSAP_CONNLESS)) {
239 IRDA_DEBUG(0, __FUNCTION__ "(), dropping frame!\n");
245 lsap = (struct lsap_cb *) hashbin_get_first(irlmp->unconnected_lsaps);
246 while (lsap != NULL) {
248 * Check if source LSAP and dest LSAP selectors and PID match.
250 if ((lsap->slsap_sel == slsap_sel) &&
251 (lsap->dlsap_sel == dlsap_sel) &&
256 lsap = (struct lsap_cb *) hashbin_get_next(irlmp->unconnected_lsaps);
259 irlmp_connless_data_indication(lsap, skb);
261 IRDA_DEBUG(0, __FUNCTION__ "(), found no matching LSAP!\n");
265 #endif /* CONFIG_IRDA_ULTRA */
268 * Function irlmp_link_disconnect_indication (reason, userdata)
270 * IrLAP has disconnected
273 void irlmp_link_disconnect_indication(struct lap_cb *lap,
274 struct irlap_cb *irlap,
276 struct sk_buff *userdata)
278 IRDA_DEBUG(2, __FUNCTION__ "()\n");
280 ASSERT(lap != NULL, return;);
281 ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
283 lap->reason = reason;
284 lap->daddr = DEV_ADDR_ANY;
286 /* FIXME: must do something with the userdata if any */
288 dev_kfree_skb(userdata);
291 * Inform station state machine
293 irlmp_do_lap_event(lap, LM_LAP_DISCONNECT_INDICATION, NULL);
297 * Function irlmp_link_connect_indication (qos)
299 * Incoming LAP connection!
302 void irlmp_link_connect_indication(struct lap_cb *self, __u32 saddr,
303 __u32 daddr, struct qos_info *qos,
306 IRDA_DEBUG(4, __FUNCTION__ "()\n");
308 /* Copy QoS settings for this session */
311 /* Update destination device address */
313 ASSERT(self->saddr == saddr, return;);
315 irlmp_do_lap_event(self, LM_LAP_CONNECT_INDICATION, skb);
319 * Function irlmp_link_connect_confirm (qos)
321 * LAP connection confirmed!
324 void irlmp_link_connect_confirm(struct lap_cb *self, struct qos_info *qos,
325 struct sk_buff *userdata)
327 IRDA_DEBUG(4, __FUNCTION__ "()\n");
329 ASSERT(self != NULL, return;);
330 ASSERT(self->magic == LMP_LAP_MAGIC, return;);
331 ASSERT(qos != NULL, return;);
333 /* Don't need use the userdata for now */
335 dev_kfree_skb(userdata);
337 /* Copy QoS settings for this session */
340 irlmp_do_lap_event(self, LM_LAP_CONNECT_CONFIRM, NULL);
344 * Function irlmp_link_discovery_indication (self, log)
346 * Device is discovering us
348 * It's not an answer to our own discoveries, just another device trying
349 * to perform discovery, but we don't want to miss the opportunity
350 * to exploit this information, because :
351 * o We may not actively perform discovery (just passive discovery)
352 * o This type of discovery is much more reliable. In some cases, it
353 * seem that less than 50% of our discoveries get an answer, while
354 * we always get ~100% of these.
355 * o Make faster discovery, statistically divide time of discovery
356 * events by 2 (important for the latency aspect and user feel)
357 * o Even is we do active discovery, the other node might not
358 * answer our discoveries (ex: Palm). The Palm will just perform
359 * one active discovery and connect directly to us.
361 * However, when both devices discover each other, they might attempt to
362 * connect to each other following the discovery event, and it would create
363 * collisions on the medium (SNRM battle).
364 * The "fix" for that is to disable all connection requests in IrLAP
365 * for 100ms after a discovery indication by setting the media_busy flag.
366 * Previously, we used to postpone the event which was quite ugly. Now
367 * that IrLAP takes care of this problem, just pass the event up...
371 void irlmp_link_discovery_indication(struct lap_cb *self,
372 discovery_t *discovery)
374 ASSERT(self != NULL, return;);
375 ASSERT(self->magic == LMP_LAP_MAGIC, return;);
377 irlmp_add_discovery(irlmp->cachelog, discovery);
379 /* Just handle it the same way as a discovery confirm,
380 * bypass the LM_LAP state machine (see below) */
381 irlmp_discovery_confirm(irlmp->cachelog, DISCOVERY_PASSIVE);
385 * Function irlmp_link_discovery_confirm (self, log)
387 * Called by IrLAP with a list of discoveries after the discovery
388 * request has been carried out. A NULL log is received if IrLAP
389 * was unable to carry out the discovery request
392 void irlmp_link_discovery_confirm(struct lap_cb *self, hashbin_t *log)
394 IRDA_DEBUG(4, __FUNCTION__ "()\n");
396 ASSERT(self != NULL, return;);
397 ASSERT(self->magic == LMP_LAP_MAGIC, return;);
399 irlmp_add_discovery_log(irlmp->cachelog, log);
401 /* Propagate event to various LSAPs registered for it.
402 * We bypass the LM_LAP state machine because
403 * 1) We do it regardless of the LM_LAP state
404 * 2) It doesn't affect the LM_LAP state
405 * 3) Faster, slimer, simpler, ...
407 irlmp_discovery_confirm(irlmp->cachelog, DISCOVERY_ACTIVE);
410 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
411 inline void irlmp_update_cache(struct lsap_cb *self)
413 /* Update cache entry */
414 irlmp->cache.dlsap_sel = self->dlsap_sel;
415 irlmp->cache.slsap_sel = self->slsap_sel;
416 irlmp->cache.lsap = self;
417 irlmp->cache.valid = TRUE;
422 * Function irlmp_find_handle (self, dlsap_sel, slsap_sel, status, queue)
424 * Find handle assosiated with destination and source LSAP
427 static struct lsap_cb *irlmp_find_lsap(struct lap_cb *self, __u8 dlsap_sel,
428 __u8 slsap_sel, int status,
431 struct lsap_cb *lsap;
434 * Optimize for the common case. We assume that the last frame
435 * received is in the same connection as the last one, so check in
436 * cache first to avoid the linear search
438 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
439 if ((irlmp->cache.valid) &&
440 (irlmp->cache.slsap_sel == slsap_sel) &&
441 (irlmp->cache.dlsap_sel == dlsap_sel))
443 return (irlmp->cache.lsap);
446 lsap = (struct lsap_cb *) hashbin_get_first(queue);
447 while (lsap != NULL) {
449 * If this is an incoming connection, then the destination
450 * LSAP selector may have been specified as LM_ANY so that
451 * any client can connect. In that case we only need to check
452 * if the source LSAP (in our view!) match!
454 if ((status == CONNECT_CMD) &&
455 (lsap->slsap_sel == slsap_sel) &&
456 (lsap->dlsap_sel == LSAP_ANY))
458 lsap->dlsap_sel = dlsap_sel;
460 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
461 irlmp_update_cache(lsap);
466 * Check if source LSAP and dest LSAP selectors match.
468 if ((lsap->slsap_sel == slsap_sel) &&
469 (lsap->dlsap_sel == dlsap_sel))
471 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
472 irlmp_update_cache(lsap);
476 lsap = (struct lsap_cb *) hashbin_get_next(queue);
479 /* Sorry not found! */