- pre1:
[davej-history.git] / net / irda / irlmp_frame.c
blobc9bca1ea2c20433dc955615ae73aa81821014733
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>
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)
43 skb->data[0] = dlsap;
44 skb->data[1] = slsap;
46 if (expedited) {
47 IRDA_DEBUG(4, __FUNCTION__ "(), sending expedited data\n");
48 irlap_data_request(self->irlap, skb, TRUE);
49 } else
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)
61 __u8 *frame;
63 IRDA_DEBUG(2, __FUNCTION__ "()\n");
65 ASSERT(self != NULL, return;);
66 ASSERT(self->magic == LMP_LAP_MAGIC, return;);
67 ASSERT(skb != NULL, return;);
69 frame = skb->data;
71 frame[0] = dlsap | CONTROL_BIT;
72 frame[1] = slsap;
74 frame[2] = opcode;
76 if (opcode == DISCONNECT)
77 frame[3] = 0x01; /* Service user request */
78 else
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,
91 int unreliable)
93 struct lsap_cb *lsap;
94 __u8 slsap_sel; /* Source (this) LSAP address */
95 __u8 dlsap_sel; /* Destination LSAP address */
96 __u8 *fp;
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;);
104 fp = skb->data;
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;
111 dlsap_sel = fp[1];
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 */
127 if (!lsap)
128 lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, 0,
129 self->lsaps);
130 } else
131 lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, 0,
132 self->lsaps);
134 if (lsap == NULL) {
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,
138 dlsap_sel);
139 if (fp[0] & CONTROL_BIT) {
140 IRDA_DEBUG(2, __FUNCTION__
141 "(), received control frame %02x\n", fp[2]);
142 } else {
143 IRDA_DEBUG(2, __FUNCTION__ "(), received data frame\n");
145 dev_kfree_skb(skb);
146 return;
150 * Check if we received a control frame?
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, __FUNCTION__
163 "(), Disconnect indication!\n");
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 dev_kfree_skb(skb);
170 break;
171 case ACCESSMODE_CNF:
172 IRDA_DEBUG(0, "Access mode cnf not implemented!\n");
173 dev_kfree_skb(skb);
174 break;
175 default:
176 IRDA_DEBUG(0, __FUNCTION__
177 "(), Unknown control frame %02x\n", fp[2]);
178 dev_kfree_skb(skb);
179 break;
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);
185 else
186 irlmp_do_lsap_event(lsap, LM_UDATA_INDICATION, skb);
187 } else {
188 /* Optimize and bypass the state machine if possible */
189 if (lsap->lsap_state == LSAP_DATA_TRANSFER_READY)
190 irlmp_data_indication(lsap, skb);
191 else
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 */
209 __u8 *fp;
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;);
217 fp = skb->data;
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;
224 dlsap_sel = fp[1];
225 pid = fp[2];
227 if (pid & 0x80) {
228 IRDA_DEBUG(0, __FUNCTION__ "(), extension in PID not supp!\n");
229 dev_kfree_skb(skb);
231 return;
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");
237 dev_kfree_skb(skb);
239 return;
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) &&
249 (lsap->pid == pid))
251 break;
253 lsap = (struct lsap_cb *) hashbin_get_next(irlmp->unconnected_lsaps);
255 if (lsap)
256 irlmp_connless_data_indication(lsap, skb);
257 else {
258 IRDA_DEBUG(0, __FUNCTION__ "(), found no matching LSAP!\n");
259 dev_kfree_skb(skb);
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,
272 LAP_REASON reason,
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 */
284 if (userdata)
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,
301 struct sk_buff *skb)
303 IRDA_DEBUG(4, __FUNCTION__ "()\n");
305 /* Copy QoS settings for this session */
306 self->qos = qos;
308 /* Update destination device address */
309 self->daddr = daddr;
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 */
331 if (userdata)
332 dev_kfree_skb(userdata);
334 /* Copy QoS settings for this session */
335 self->qos = qos;
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);
358 #endif
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;
390 #endif
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,
400 hashbin_t *queue)
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);
416 #endif
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);
433 #endif
434 return 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);
444 #endif
445 return lsap;
447 lsap = (struct lsap_cb *) hashbin_get_next(queue);
450 /* Sorry not found! */
451 return NULL;