1/*********************************************************************
2 *
3 * Filename:      irlmp_frame.c
4 * Version:       0.9
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>
11 *
12 *     Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>
13 *     All Rights Reserved.
14 *     Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
15 *
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.
20 *
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.
24 *
25 ********************************************************************/
26
27#include <linux/skbuff.h>
28#include <linux/kernel.h>
29
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>
36
37static struct lsap_cb *irlmp_find_lsap(struct lap_cb *self, __u8 dlsap,
38				       __u8 slsap, int status, hashbin_t *);
39
40inline void irlmp_send_data_pdu(struct lap_cb *self, __u8 dlsap, __u8 slsap,
41				int expedited, struct sk_buff *skb)
42{
43	skb->data[0] = dlsap;
44	skb->data[1] = slsap;
45
46	if (expedited) {
47		IRDA_DEBUG(4, "%s(), sending expedited data\n", __FUNCTION__);
48		irlap_data_request(self->irlap, skb, TRUE);
49	} else
50		irlap_data_request(self->irlap, skb, FALSE);
51}
52
53/*
54 * Function irlmp_send_lcf_pdu (dlsap, slsap, opcode,skb)
55 *
56 *    Send Link Control Frame to IrLAP
57 */
58void irlmp_send_lcf_pdu(struct lap_cb *self, __u8 dlsap, __u8 slsap,
59			__u8 opcode, struct sk_buff *skb)
60{
61	__u8 *frame;
62
63	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
64
65	IRDA_ASSERT(self != NULL, return;);
66	IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
67	IRDA_ASSERT(skb != NULL, return;);
68
69	frame = skb->data;
70
71	frame[0] = dlsap | CONTROL_BIT;
72	frame[1] = slsap;
73
74	frame[2] = opcode;
75
76	if (opcode == DISCONNECT)
77		frame[3] = 0x01; /* Service user request */
78	else
79		frame[3] = 0x00; /* rsvd */
80
81	irlap_data_request(self->irlap, skb, FALSE);
82}
83
84/*
85 * Function irlmp_input (skb)
86 *
87 *    Used by IrLAP to pass received data frames to IrLMP layer
88 *
89 */
90void irlmp_link_data_indication(struct lap_cb *self, struct sk_buff *skb,
91				int unreliable)
92{
93	struct lsap_cb *lsap;
94	__u8   slsap_sel;   /* Source (this) LSAP address */
95	__u8   dlsap_sel;   /* Destination LSAP address */
96	__u8   *fp;
97
98	IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
99
100	IRDA_ASSERT(self != NULL, return;);
101	IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
102	IRDA_ASSERT(skb->len > 2, return;);
103
104	fp = skb->data;
105
106	/*
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
109	 */
110	slsap_sel = fp[0] & LSAP_MASK;
111	dlsap_sel = fp[1];
112
113	/*
114	 *  Check if this is an incoming connection, since we must deal with
115	 *  it in a different way than other established connections.
116	 */
117	if ((fp[0] & CONTROL_BIT) && (fp[2] == CONNECT_CMD)) {
118		IRDA_DEBUG(3, "%s(), incoming connection, "
119			   "source LSAP=%d, dest LSAP=%d\n",
120			   __FUNCTION__, slsap_sel, dlsap_sel);
121
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);
125
126		/* Maybe LSAP was already connected, so try one more time */
127		if (!lsap) {
128			IRDA_DEBUG(1, "%s(), incoming connection for LSAP already connected\n", __FUNCTION__);
129			lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, 0,
130					       self->lsaps);
131		}
132	} else
133		lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, 0,
134				       self->lsaps);
135
136	if (lsap == NULL) {
137		IRDA_DEBUG(2, "IrLMP, Sorry, no LSAP for received frame!\n");
138		IRDA_DEBUG(2, "%s(), slsap_sel = %02x, dlsap_sel = %02x\n",
139			   __FUNCTION__, slsap_sel, dlsap_sel);
140		if (fp[0] & CONTROL_BIT) {
141			IRDA_DEBUG(2, "%s(), received control frame %02x\n",
142				   __FUNCTION__, fp[2]);
143		} else {
144			IRDA_DEBUG(2, "%s(), received data frame\n", __FUNCTION__);
145		}
146		return;
147	}
148
149	/*
150	 *  Check if we received a control frame?
151	 */
152	if (fp[0] & CONTROL_BIT) {
153		switch (fp[2]) {
154		case CONNECT_CMD:
155			lsap->lap = self;
156			irlmp_do_lsap_event(lsap, LM_CONNECT_INDICATION, skb);
157			break;
158		case CONNECT_CNF:
159			irlmp_do_lsap_event(lsap, LM_CONNECT_CONFIRM, skb);
160			break;
161		case DISCONNECT:
162			IRDA_DEBUG(4, "%s(), Disconnect indication!\n",
163				   __FUNCTION__);
164			irlmp_do_lsap_event(lsap, LM_DISCONNECT_INDICATION,
165					    skb);
166			break;
167		case ACCESSMODE_CMD:
168			IRDA_DEBUG(0, "Access mode cmd not implemented!\n");
169			break;
170		case ACCESSMODE_CNF:
171			IRDA_DEBUG(0, "Access mode cnf not implemented!\n");
172			break;
173		default:
174			IRDA_DEBUG(0, "%s(), Unknown control frame %02x\n",
175				   __FUNCTION__, fp[2]);
176			break;
177		}
178	} else if (unreliable) {
179		/* Optimize and bypass the state machine if possible */
180		if (lsap->lsap_state == LSAP_DATA_TRANSFER_READY)
181			irlmp_udata_indication(lsap, skb);
182		else
183			irlmp_do_lsap_event(lsap, LM_UDATA_INDICATION, skb);
184	} else {
185		/* Optimize and bypass the state machine if possible */
186		if (lsap->lsap_state == LSAP_DATA_TRANSFER_READY)
187			irlmp_data_indication(lsap, skb);
188		else
189			irlmp_do_lsap_event(lsap, LM_DATA_INDICATION, skb);
190	}
191}
192
193/*
194 * Function irlmp_link_unitdata_indication (self, skb)
195 *
196 *
197 *
198 */
199#ifdef CONFIG_IRDA_ULTRA
200void irlmp_link_unitdata_indication(struct lap_cb *self, struct sk_buff *skb)
201{
202	struct lsap_cb *lsap;
203	__u8   slsap_sel;   /* Source (this) LSAP address */
204	__u8   dlsap_sel;   /* Destination LSAP address */
205	__u8   pid;         /* Protocol identifier */
206	__u8   *fp;
207	unsigned long flags;
208
209	IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
210
211	IRDA_ASSERT(self != NULL, return;);
212	IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
213	IRDA_ASSERT(skb->len > 2, return;);
214
215	fp = skb->data;
216
217	/*
218	 *  The next statements may be confusing, but we do this so that
219	 *  destination LSAP of received frame is source LSAP in our view
220	 */
221	slsap_sel = fp[0] & LSAP_MASK;
222	dlsap_sel = fp[1];
223	pid       = fp[2];
224
225	if (pid & 0x80) {
226		IRDA_DEBUG(0, "%s(), extension in PID not supp!\n",
227			   __FUNCTION__);
228		return;
229	}
230
231	/* Check if frame is addressed to the connectionless LSAP */
232	if ((slsap_sel != LSAP_CONNLESS) || (dlsap_sel != LSAP_CONNLESS)) {
233		IRDA_DEBUG(0, "%s(), dropping frame!\n", __FUNCTION__);
234		return;
235	}
236
237	/* Search the connectionless LSAP */
238	spin_lock_irqsave(&irlmp->unconnected_lsaps->hb_spinlock, flags);
239	lsap = (struct lsap_cb *) hashbin_get_first(irlmp->unconnected_lsaps);
240	while (lsap != NULL) {
241		/*
242		 *  Check if source LSAP and dest LSAP selectors and PID match.
243		 */
244		if ((lsap->slsap_sel == slsap_sel) &&
245		    (lsap->dlsap_sel == dlsap_sel) &&
246		    (lsap->pid == pid))
247		{
248			break;
249		}
250		lsap = (struct lsap_cb *) hashbin_get_next(irlmp->unconnected_lsaps);
251	}
252	spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);
253
254	if (lsap)
255		irlmp_connless_data_indication(lsap, skb);
256	else {
257		IRDA_DEBUG(0, "%s(), found no matching LSAP!\n", __FUNCTION__);
258	}
259}
260#endif /* CONFIG_IRDA_ULTRA */
261
262/*
263 * Function irlmp_link_disconnect_indication (reason, userdata)
264 *
265 *    IrLAP has disconnected
266 *
267 */
268void irlmp_link_disconnect_indication(struct lap_cb *lap,
269				      struct irlap_cb *irlap,
270				      LAP_REASON reason,
271				      struct sk_buff *skb)
272{
273	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
274
275	IRDA_ASSERT(lap != NULL, return;);
276	IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
277
278	lap->reason = reason;
279	lap->daddr = DEV_ADDR_ANY;
280
281
282	/*
283	 *  Inform station state machine
284	 */
285	irlmp_do_lap_event(lap, LM_LAP_DISCONNECT_INDICATION, NULL);
286}
287
288/*
289 * Function irlmp_link_connect_indication (qos)
290 *
291 *    Incoming LAP connection!
292 *
293 */
294void irlmp_link_connect_indication(struct lap_cb *self, __u32 saddr,
295				   __u32 daddr, struct qos_info *qos,
296				   struct sk_buff *skb)
297{
298	IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
299
300	/* Copy QoS settings for this session */
301	self->qos = qos;
302
303	/* Update destination device address */
304	self->daddr = daddr;
305	IRDA_ASSERT(self->saddr == saddr, return;);
306
307	irlmp_do_lap_event(self, LM_LAP_CONNECT_INDICATION, skb);
308}
309
310/*
311 * Function irlmp_link_connect_confirm (qos)
312 *
313 *    LAP connection confirmed!
314 *
315 */
316void irlmp_link_connect_confirm(struct lap_cb *self, struct qos_info *qos,
317				struct sk_buff *skb)
318{
319	IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
320
321	IRDA_ASSERT(self != NULL, return;);
322	IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
323	IRDA_ASSERT(qos != NULL, return;);
324
325	/* Don't need use the skb for now */
326
327	/* Copy QoS settings for this session */
328	self->qos = qos;
329
330	irlmp_do_lap_event(self, LM_LAP_CONNECT_CONFIRM, NULL);
331}
332
333/*
334 * Function irlmp_link_discovery_indication (self, log)
335 *
336 *    Device is discovering us
337 *
338 * It's not an answer to our own discoveries, just another device trying
339 * to perform discovery, but we don't want to miss the opportunity
340 * to exploit this information, because :
341 *	o We may not actively perform discovery (just passive discovery)
342 *	o This type of discovery is much more reliable. In some cases, it
343 *	  seem that less than 50% of our discoveries get an answer, while
344 *	  we always get ~100% of these.
345 *	o Make faster discovery, statistically divide time of discovery
346 *	  events by 2 (important for the latency aspect and user feel)
347 *	o Even is we do active discovery, the other node might not
348 *	  answer our discoveries (ex: Palm). The Palm will just perform
349 *	  one active discovery and connect directly to us.
350 *
351 * However, when both devices discover each other, they might attempt to
352 * connect to each other following the discovery event, and it would create
353 * collisions on the medium (SNRM battle).
354 * The "fix" for that is to disable all connection requests in IrLAP
355 * for 100ms after a discovery indication by setting the media_busy flag.
356 * Previously, we used to postpone the event which was quite ugly. Now
357 * that IrLAP takes care of this problem, just pass the event up...
358 *
359 * Jean II
360 */
361void irlmp_link_discovery_indication(struct lap_cb *self,
362				     discovery_t *discovery)
363{
364	IRDA_ASSERT(self != NULL, return;);
365	IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
366
367	/* Add to main log, cleanup */
368	irlmp_add_discovery(irlmp->cachelog, discovery);
369
370	/* Just handle it the same way as a discovery confirm,
371	 * bypass the LM_LAP state machine (see below) */
372	irlmp_discovery_confirm(irlmp->cachelog, DISCOVERY_PASSIVE);
373}
374
375/*
376 * Function irlmp_link_discovery_confirm (self, log)
377 *
378 *    Called by IrLAP with a list of discoveries after the discovery
379 *    request has been carried out. A NULL log is received if IrLAP
380 *    was unable to carry out the discovery request
381 *
382 */
383void irlmp_link_discovery_confirm(struct lap_cb *self, hashbin_t *log)
384{
385	IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
386
387	IRDA_ASSERT(self != NULL, return;);
388	IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
389
390	/* Add to main log, cleanup */
391	irlmp_add_discovery_log(irlmp->cachelog, log);
392
393	/* Propagate event to various LSAPs registered for it.
394	 * We bypass the LM_LAP state machine because
395	 *	1) We do it regardless of the LM_LAP state
396	 *	2) It doesn't affect the LM_LAP state
397	 *	3) Faster, slimer, simpler, ...
398	 * Jean II */
399	irlmp_discovery_confirm(irlmp->cachelog, DISCOVERY_ACTIVE);
400}
401
402#ifdef CONFIG_IRDA_CACHE_LAST_LSAP
403static inline void irlmp_update_cache(struct lap_cb *lap,
404				      struct lsap_cb *lsap)
405{
406	/* Prevent concurrent read to get garbage */
407	lap->cache.valid = FALSE;
408	/* Update cache entry */
409	lap->cache.dlsap_sel = lsap->dlsap_sel;
410	lap->cache.slsap_sel = lsap->slsap_sel;
411	lap->cache.lsap = lsap;
412	lap->cache.valid = TRUE;
413}
414#endif
415
416/*
417 * Function irlmp_find_handle (self, dlsap_sel, slsap_sel, status, queue)
418 *
419 *    Find handle associated with destination and source LSAP
420 *
421 * Any IrDA connection (LSAP/TSAP) is uniquely identified by
422 * 3 parameters, the local lsap, the remote lsap and the remote address.
423 * We may initiate multiple connections to the same remote service
424 * (they will have different local lsap), a remote device may initiate
425 * multiple connections to the same local service (they will have
426 * different remote lsap), or multiple devices may connect to the same
427 * service and may use the same remote lsap (and they will have
428 * different remote address).
429 * So, where is the remote address ? Each LAP connection is made with
430 * a single remote device, so imply a specific remote address.
431 * Jean II
432 */
433static struct lsap_cb *irlmp_find_lsap(struct lap_cb *self, __u8 dlsap_sel,
434				       __u8 slsap_sel, int status,
435				       hashbin_t *queue)
436{
437	struct lsap_cb *lsap;
438	unsigned long flags;
439
440	/*
441	 *  Optimize for the common case. We assume that the last frame
442	 *  received is in the same connection as the last one, so check in
443	 *  cache first to avoid the linear search
444	 */
445#ifdef CONFIG_IRDA_CACHE_LAST_LSAP
446	if ((self->cache.valid) &&
447	    (self->cache.slsap_sel == slsap_sel) &&
448	    (self->cache.dlsap_sel == dlsap_sel))
449	{
450		return (self->cache.lsap);
451	}
452#endif
453
454	spin_lock_irqsave(&queue->hb_spinlock, flags);
455
456	lsap = (struct lsap_cb *) hashbin_get_first(queue);
457	while (lsap != NULL) {
458		/*
459		 *  If this is an incoming connection, then the destination
460		 *  LSAP selector may have been specified as LM_ANY so that
461		 *  any client can connect. In that case we only need to check
462		 *  if the source LSAP (in our view!) match!
463		 */
464		if ((status == CONNECT_CMD) &&
465		    (lsap->slsap_sel == slsap_sel) &&
466		    (lsap->dlsap_sel == LSAP_ANY)) {
467			/* This is where the dest lsap sel is set on incoming
468			 * lsaps */
469			lsap->dlsap_sel = dlsap_sel;
470			break;
471		}
472		/*
473		 *  Check if source LSAP and dest LSAP selectors match.
474		 */
475		if ((lsap->slsap_sel == slsap_sel) &&
476		    (lsap->dlsap_sel == dlsap_sel))
477			break;
478
479		lsap = (struct lsap_cb *) hashbin_get_next(queue);
480	}
481#ifdef CONFIG_IRDA_CACHE_LAST_LSAP
482	if(lsap)
483		irlmp_update_cache(self, lsap);
484#endif
485	spin_unlock_irqrestore(&queue->hb_spinlock, flags);
486
487	/* Return what we've found or NULL */
488	return lsap;
489}
490