/*********************************************************************1*2* Filename: irlmp_frame.c3* Version: 0.94* Description: IrLMP frame implementation5* Status: Experimental.6* Author: Dag Brattli <[email protected]>7* Created at: Tue Aug 19 02:09:59 19978* Modified at: Mon Dec 13 13:41:12 19999* Modified by: Dag Brattli <[email protected]>10*11* Copyright (c) 1998-1999 Dag Brattli <[email protected]>12* All Rights Reserved.13* Copyright (c) 2000-2003 Jean Tourrilhes <[email protected]>14*15* This program is free software; you can redistribute it and/or16* modify it under the terms of the GNU General Public License as17* published by the Free Software Foundation; either version 2 of18* the License, or (at your option) any later version.19*20* Neither Dag Brattli nor University of Tromsø admit liability nor21* provide warranty for any of this software. This material is22* provided "AS-IS" and at no charge.23*24********************************************************************/2526#include <linux/skbuff.h>27#include <linux/kernel.h>2829#include <net/irda/irda.h>30#include <net/irda/irlap.h>31#include <net/irda/timer.h>32#include <net/irda/irlmp.h>33#include <net/irda/irlmp_frame.h>34#include <net/irda/discovery.h>3536static struct lsap_cb *irlmp_find_lsap(struct lap_cb *self, __u8 dlsap,37__u8 slsap, int status, hashbin_t *);3839inline void irlmp_send_data_pdu(struct lap_cb *self, __u8 dlsap, __u8 slsap,40int expedited, struct sk_buff *skb)41{42skb->data[0] = dlsap;43skb->data[1] = slsap;4445if (expedited) {46IRDA_DEBUG(4, "%s(), sending expedited data\n", __func__);47irlap_data_request(self->irlap, skb, TRUE);48} else49irlap_data_request(self->irlap, skb, FALSE);50}5152/*53* Function irlmp_send_lcf_pdu (dlsap, slsap, opcode,skb)54*55* Send Link Control Frame to IrLAP56*/57void irlmp_send_lcf_pdu(struct lap_cb *self, __u8 dlsap, __u8 slsap,58__u8 opcode, struct sk_buff *skb)59{60__u8 *frame;6162IRDA_DEBUG(2, "%s()\n", __func__);6364IRDA_ASSERT(self != NULL, return;);65IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);66IRDA_ASSERT(skb != NULL, return;);6768frame = skb->data;6970frame[0] = dlsap | CONTROL_BIT;71frame[1] = slsap;7273frame[2] = opcode;7475if (opcode == DISCONNECT)76frame[3] = 0x01; /* Service user request */77else78frame[3] = 0x00; /* rsvd */7980irlap_data_request(self->irlap, skb, FALSE);81}8283/*84* Function irlmp_input (skb)85*86* Used by IrLAP to pass received data frames to IrLMP layer87*88*/89void irlmp_link_data_indication(struct lap_cb *self, struct sk_buff *skb,90int unreliable)91{92struct lsap_cb *lsap;93__u8 slsap_sel; /* Source (this) LSAP address */94__u8 dlsap_sel; /* Destination LSAP address */95__u8 *fp;9697IRDA_DEBUG(4, "%s()\n", __func__);9899IRDA_ASSERT(self != NULL, return;);100IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);101IRDA_ASSERT(skb->len > 2, return;);102103fp = skb->data;104105/*106* The next statements may be confusing, but we do this so that107* destination LSAP of received frame is source LSAP in our view108*/109slsap_sel = fp[0] & LSAP_MASK;110dlsap_sel = fp[1];111112/*113* Check if this is an incoming connection, since we must deal with114* it in a different way than other established connections.115*/116if ((fp[0] & CONTROL_BIT) && (fp[2] == CONNECT_CMD)) {117IRDA_DEBUG(3, "%s(), incoming connection, "118"source LSAP=%d, dest LSAP=%d\n",119__func__, slsap_sel, dlsap_sel);120121/* Try to find LSAP among the unconnected LSAPs */122lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, CONNECT_CMD,123irlmp->unconnected_lsaps);124125/* Maybe LSAP was already connected, so try one more time */126if (!lsap) {127IRDA_DEBUG(1, "%s(), incoming connection for LSAP already connected\n", __func__);128lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, 0,129self->lsaps);130}131} else132lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, 0,133self->lsaps);134135if (lsap == NULL) {136IRDA_DEBUG(2, "IrLMP, Sorry, no LSAP for received frame!\n");137IRDA_DEBUG(2, "%s(), slsap_sel = %02x, dlsap_sel = %02x\n",138__func__, slsap_sel, dlsap_sel);139if (fp[0] & CONTROL_BIT) {140IRDA_DEBUG(2, "%s(), received control frame %02x\n",141__func__, fp[2]);142} else {143IRDA_DEBUG(2, "%s(), received data frame\n", __func__);144}145return;146}147148/*149* Check if we received a control frame?150*/151if (fp[0] & CONTROL_BIT) {152switch (fp[2]) {153case CONNECT_CMD:154lsap->lap = self;155irlmp_do_lsap_event(lsap, LM_CONNECT_INDICATION, skb);156break;157case CONNECT_CNF:158irlmp_do_lsap_event(lsap, LM_CONNECT_CONFIRM, skb);159break;160case DISCONNECT:161IRDA_DEBUG(4, "%s(), Disconnect indication!\n",162__func__);163irlmp_do_lsap_event(lsap, LM_DISCONNECT_INDICATION,164skb);165break;166case ACCESSMODE_CMD:167IRDA_DEBUG(0, "Access mode cmd not implemented!\n");168break;169case ACCESSMODE_CNF:170IRDA_DEBUG(0, "Access mode cnf not implemented!\n");171break;172default:173IRDA_DEBUG(0, "%s(), Unknown control frame %02x\n",174__func__, fp[2]);175break;176}177} else if (unreliable) {178/* Optimize and bypass the state machine if possible */179if (lsap->lsap_state == LSAP_DATA_TRANSFER_READY)180irlmp_udata_indication(lsap, skb);181else182irlmp_do_lsap_event(lsap, LM_UDATA_INDICATION, skb);183} else {184/* Optimize and bypass the state machine if possible */185if (lsap->lsap_state == LSAP_DATA_TRANSFER_READY)186irlmp_data_indication(lsap, skb);187else188irlmp_do_lsap_event(lsap, LM_DATA_INDICATION, skb);189}190}191192/*193* Function irlmp_link_unitdata_indication (self, skb)194*195*196*197*/198#ifdef CONFIG_IRDA_ULTRA199void irlmp_link_unitdata_indication(struct lap_cb *self, struct sk_buff *skb)200{201struct lsap_cb *lsap;202__u8 slsap_sel; /* Source (this) LSAP address */203__u8 dlsap_sel; /* Destination LSAP address */204__u8 pid; /* Protocol identifier */205__u8 *fp;206unsigned long flags;207208IRDA_DEBUG(4, "%s()\n", __func__);209210IRDA_ASSERT(self != NULL, return;);211IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);212IRDA_ASSERT(skb->len > 2, return;);213214fp = skb->data;215216/*217* The next statements may be confusing, but we do this so that218* destination LSAP of received frame is source LSAP in our view219*/220slsap_sel = fp[0] & LSAP_MASK;221dlsap_sel = fp[1];222pid = fp[2];223224if (pid & 0x80) {225IRDA_DEBUG(0, "%s(), extension in PID not supp!\n",226__func__);227return;228}229230/* Check if frame is addressed to the connectionless LSAP */231if ((slsap_sel != LSAP_CONNLESS) || (dlsap_sel != LSAP_CONNLESS)) {232IRDA_DEBUG(0, "%s(), dropping frame!\n", __func__);233return;234}235236/* Search the connectionless LSAP */237spin_lock_irqsave(&irlmp->unconnected_lsaps->hb_spinlock, flags);238lsap = (struct lsap_cb *) hashbin_get_first(irlmp->unconnected_lsaps);239while (lsap != NULL) {240/*241* Check if source LSAP and dest LSAP selectors and PID match.242*/243if ((lsap->slsap_sel == slsap_sel) &&244(lsap->dlsap_sel == dlsap_sel) &&245(lsap->pid == pid))246{247break;248}249lsap = (struct lsap_cb *) hashbin_get_next(irlmp->unconnected_lsaps);250}251spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);252253if (lsap)254irlmp_connless_data_indication(lsap, skb);255else {256IRDA_DEBUG(0, "%s(), found no matching LSAP!\n", __func__);257}258}259#endif /* CONFIG_IRDA_ULTRA */260261/*262* Function irlmp_link_disconnect_indication (reason, userdata)263*264* IrLAP has disconnected265*266*/267void irlmp_link_disconnect_indication(struct lap_cb *lap,268struct irlap_cb *irlap,269LAP_REASON reason,270struct sk_buff *skb)271{272IRDA_DEBUG(2, "%s()\n", __func__);273274IRDA_ASSERT(lap != NULL, return;);275IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return;);276277lap->reason = reason;278lap->daddr = DEV_ADDR_ANY;279280/* FIXME: must do something with the skb if any */281282/*283* Inform station state machine284*/285irlmp_do_lap_event(lap, LM_LAP_DISCONNECT_INDICATION, NULL);286}287288/*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,296struct sk_buff *skb)297{298IRDA_DEBUG(4, "%s()\n", __func__);299300/* Copy QoS settings for this session */301self->qos = qos;302303/* Update destination device address */304self->daddr = daddr;305IRDA_ASSERT(self->saddr == saddr, return;);306307irlmp_do_lap_event(self, LM_LAP_CONNECT_INDICATION, skb);308}309310/*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,317struct sk_buff *skb)318{319IRDA_DEBUG(4, "%s()\n", __func__);320321IRDA_ASSERT(self != NULL, return;);322IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);323IRDA_ASSERT(qos != NULL, return;);324325/* Don't need use the skb for now */326327/* Copy QoS settings for this session */328self->qos = qos;329330irlmp_do_lap_event(self, LM_LAP_CONNECT_CONFIRM, NULL);331}332333/*334* Function irlmp_link_discovery_indication (self, log)335*336* Device is discovering us337*338* It's not an answer to our own discoveries, just another device trying339* to perform discovery, but we don't want to miss the opportunity340* 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, it343* seem that less than 50% of our discoveries get an answer, while344* we always get ~100% of these.345* o Make faster discovery, statistically divide time of discovery346* events by 2 (important for the latency aspect and user feel)347* o Even is we do active discovery, the other node might not348* answer our discoveries (ex: Palm). The Palm will just perform349* one active discovery and connect directly to us.350*351* However, when both devices discover each other, they might attempt to352* connect to each other following the discovery event, and it would create353* collisions on the medium (SNRM battle).354* The "fix" for that is to disable all connection requests in IrLAP355* for 100ms after a discovery indication by setting the media_busy flag.356* Previously, we used to postpone the event which was quite ugly. Now357* that IrLAP takes care of this problem, just pass the event up...358*359* Jean II360*/361void irlmp_link_discovery_indication(struct lap_cb *self,362discovery_t *discovery)363{364IRDA_ASSERT(self != NULL, return;);365IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);366367/* Add to main log, cleanup */368irlmp_add_discovery(irlmp->cachelog, discovery);369370/* Just handle it the same way as a discovery confirm,371* bypass the LM_LAP state machine (see below) */372irlmp_discovery_confirm(irlmp->cachelog, DISCOVERY_PASSIVE);373}374375/*376* Function irlmp_link_discovery_confirm (self, log)377*378* Called by IrLAP with a list of discoveries after the discovery379* request has been carried out. A NULL log is received if IrLAP380* was unable to carry out the discovery request381*382*/383void irlmp_link_discovery_confirm(struct lap_cb *self, hashbin_t *log)384{385IRDA_DEBUG(4, "%s()\n", __func__);386387IRDA_ASSERT(self != NULL, return;);388IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);389390/* Add to main log, cleanup */391irlmp_add_discovery_log(irlmp->cachelog, log);392393/* Propagate event to various LSAPs registered for it.394* We bypass the LM_LAP state machine because395* 1) We do it regardless of the LM_LAP state396* 2) It doesn't affect the LM_LAP state397* 3) Faster, slimer, simpler, ...398* Jean II */399irlmp_discovery_confirm(irlmp->cachelog, DISCOVERY_ACTIVE);400}401402#ifdef CONFIG_IRDA_CACHE_LAST_LSAP403static inline void irlmp_update_cache(struct lap_cb *lap,404struct lsap_cb *lsap)405{406/* Prevent concurrent read to get garbage */407lap->cache.valid = FALSE;408/* Update cache entry */409lap->cache.dlsap_sel = lsap->dlsap_sel;410lap->cache.slsap_sel = lsap->slsap_sel;411lap->cache.lsap = lsap;412lap->cache.valid = TRUE;413}414#endif415416/*417* Function irlmp_find_handle (self, dlsap_sel, slsap_sel, status, queue)418*419* Find handle associated with destination and source LSAP420*421* Any IrDA connection (LSAP/TSAP) is uniquely identified by422* 3 parameters, the local lsap, the remote lsap and the remote address.423* We may initiate multiple connections to the same remote service424* (they will have different local lsap), a remote device may initiate425* multiple connections to the same local service (they will have426* different remote lsap), or multiple devices may connect to the same427* service and may use the same remote lsap (and they will have428* different remote address).429* So, where is the remote address ? Each LAP connection is made with430* a single remote device, so imply a specific remote address.431* Jean II432*/433static struct lsap_cb *irlmp_find_lsap(struct lap_cb *self, __u8 dlsap_sel,434__u8 slsap_sel, int status,435hashbin_t *queue)436{437struct lsap_cb *lsap;438unsigned long flags;439440/*441* Optimize for the common case. We assume that the last frame442* received is in the same connection as the last one, so check in443* cache first to avoid the linear search444*/445#ifdef CONFIG_IRDA_CACHE_LAST_LSAP446if ((self->cache.valid) &&447(self->cache.slsap_sel == slsap_sel) &&448(self->cache.dlsap_sel == dlsap_sel))449{450return self->cache.lsap;451}452#endif453454spin_lock_irqsave(&queue->hb_spinlock, flags);455456lsap = (struct lsap_cb *) hashbin_get_first(queue);457while (lsap != NULL) {458/*459* If this is an incoming connection, then the destination460* LSAP selector may have been specified as LM_ANY so that461* any client can connect. In that case we only need to check462* if the source LSAP (in our view!) match!463*/464if ((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 incoming468* lsaps */469lsap->dlsap_sel = dlsap_sel;470break;471}472/*473* Check if source LSAP and dest LSAP selectors match.474*/475if ((lsap->slsap_sel == slsap_sel) &&476(lsap->dlsap_sel == dlsap_sel))477break;478479lsap = (struct lsap_cb *) hashbin_get_next(queue);480}481#ifdef CONFIG_IRDA_CACHE_LAST_LSAP482if(lsap)483irlmp_update_cache(self, lsap);484#endif485spin_unlock_irqrestore(&queue->hb_spinlock, flags);486487/* Return what we've found or NULL */488return lsap;489}490491492