mac-802_11.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:73k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*-
  2.  *
  3.  * Copyright (c) 1997 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. All advertising materials mentioning features or use of this software
  15.  *    must display the following acknowledgement:
  16.  * This product includes software developed by the Computer Systems
  17.  * Engineering Group at Lawrence Berkeley Laboratory.
  18.  * 4. Neither the name of the University nor of the Laboratory may be used
  19.  *    to endorse or promote products derived from this software without
  20.  *    specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32.  * SUCH DAMAGE.
  33.  *
  34.  * $Header: /cvsroot/nsnam/ns-2/mac/mac-802_11.cc,v 1.56 2008/03/28 04:43:06 tom_henderson Exp $
  35.  *
  36.  * Ported from CMU/Monarch's code, nov'98 -Padma.
  37.  * Contributions by:
  38.  *   - Mike Holland
  39.  *   - Sushmita
  40.  */
  41. #include "delay.h"
  42. #include "connector.h"
  43. #include "packet.h"
  44. #include "random.h"
  45. #include "mobilenode.h"
  46. // #define DEBUG 99
  47. #include "arp.h"
  48. #include "ll.h"
  49. #include "mac.h"
  50. #include "mac-timers.h"
  51. #include "mac-802_11.h"
  52. #include "cmu-trace.h"
  53. // Added by Sushmita to support event tracing
  54. #include "agent.h"
  55. #include "basetrace.h"
  56. /* our backoff timer doesn't count down in idle times during a
  57.  * frame-exchange sequence as the mac tx state isn't idle; genreally
  58.  * these idle times are less than DIFS and won't contribute to
  59.  * counting down the backoff period, but this could be a real
  60.  * problem if the frame exchange ends up in a timeout! in that case,
  61.  * i.e. if a timeout happens we've not been counting down for the
  62.  * duration of the timeout, and in fact begin counting down only
  63.  * DIFS after the timeout!! we lose the timeout interval - which
  64.  * will is not the REAL case! also, the backoff timer could be NULL
  65.  * and we could have a pending transmission which we could have
  66.  * sent! one could argue this is an implementation artifact which
  67.  * doesn't violate the spec.. and the timeout interval is expected
  68.  * to be less than DIFS .. which means its not a lot of time we
  69.  * lose.. anyway if everyone hears everyone the only reason a ack will
  70.  * be delayed will be due to a collision => the medium won't really be
  71.  * idle for a DIFS for this to really matter!!
  72.  */
  73. inline void
  74. Mac802_11::checkBackoffTimer()
  75. {
  76. if(is_idle() && mhBackoff_.paused())
  77. mhBackoff_.resume(phymib_.getDIFS());
  78. if(! is_idle() && mhBackoff_.busy() && ! mhBackoff_.paused())
  79. mhBackoff_.pause();
  80. }
  81. inline void
  82. Mac802_11::transmit(Packet *p, double timeout)
  83. {
  84. tx_active_ = 1;
  85. if (EOTtarget_) {
  86. assert (eotPacket_ == NULL);
  87. eotPacket_ = p->copy();
  88. }
  89. /*
  90.  * If I'm transmitting without doing CS, such as when
  91.  * sending an ACK, any incoming packet will be "missed"
  92.  * and hence, must be discarded.
  93.  */
  94. if(rx_state_ != MAC_IDLE) {
  95. //assert(dh->dh_fc.fc_type == MAC_Type_Control);
  96. //assert(dh->dh_fc.fc_subtype == MAC_Subtype_ACK);
  97. assert(pktRx_);
  98. struct hdr_cmn *ch = HDR_CMN(pktRx_);
  99. ch->error() = 1;        /* force packet discard */
  100. }
  101. /*
  102.  * pass the packet on the "interface" which will in turn
  103.  * place the packet on the channel.
  104.  *
  105.  * NOTE: a handler is passed along so that the Network
  106.  *       Interface can distinguish between incoming and
  107.  *       outgoing packets.
  108.  */
  109. downtarget_->recv(p->copy(), this);
  110. mhSend_.start(timeout);
  111. mhIF_.start(txtime(p));
  112. }
  113. inline void
  114. Mac802_11::setRxState(MacState newState)
  115. {
  116. rx_state_ = newState;
  117. checkBackoffTimer();
  118. }
  119. inline void
  120. Mac802_11::setTxState(MacState newState)
  121. {
  122. tx_state_ = newState;
  123. checkBackoffTimer();
  124. }
  125. /* ======================================================================
  126.    TCL Hooks for the simulator
  127.    ====================================================================== */
  128. static class Mac802_11Class : public TclClass {
  129. public:
  130. Mac802_11Class() : TclClass("Mac/802_11") {}
  131. TclObject* create(int, const char*const*) {
  132. return (new Mac802_11());
  133. }
  134. } class_mac802_11;
  135. /* ======================================================================
  136.    Mac  and Phy MIB Class Functions
  137.    ====================================================================== */
  138. PHY_MIB::PHY_MIB(Mac802_11 *parent)
  139. {
  140. /*
  141.  * Bind the phy mib objects.  Note that these will be bound
  142.  * to Mac/802_11 variables
  143.  */
  144. parent->bind("CWMin_", &CWMin);
  145. parent->bind("CWMax_", &CWMax);
  146. parent->bind("SlotTime_", &SlotTime);
  147. parent->bind("SIFS_", &SIFSTime);
  148. parent->bind("BeaconInterval_", &BeaconInterval);
  149. parent->bind("PreambleLength_", &PreambleLength);
  150. parent->bind("PLCPHeaderLength_", &PLCPHeaderLength);
  151. parent->bind_bw("PLCPDataRate_", &PLCPDataRate);
  152. }
  153. MAC_MIB::MAC_MIB(Mac802_11 *parent)
  154. {
  155. /*
  156.  * Bind the phy mib objects.  Note that these will be bound
  157.  * to Mac/802_11 variables
  158.  */
  159. parent->bind("RTSThreshold_", &RTSThreshold);
  160. parent->bind("ShortRetryLimit_", &ShortRetryLimit);
  161. parent->bind("LongRetryLimit_", &LongRetryLimit);
  162. parent->bind("ScanType_", &ScanType);
  163. parent->bind("ProbeDelay_", &ProbeDelay);
  164. parent->bind("MaxChannelTime_", &MaxChannelTime);
  165. parent->bind("MinChannelTime_", &MinChannelTime);
  166. parent->bind("ChannelTime_", &ChannelTime);
  167. }
  168. /* ======================================================================
  169.    Mac Class Functions
  170.    ====================================================================== */
  171. Mac802_11::Mac802_11() : 
  172. Mac(), phymib_(this), macmib_(this), mhIF_(this), mhNav_(this), 
  173. mhRecv_(this), mhSend_(this), 
  174. mhDefer_(this), mhBackoff_(this), mhBeacon_(this), mhProbe_(this)
  175. {
  176. nav_ = 0.0;
  177. tx_state_ = rx_state_ = MAC_IDLE;
  178. tx_active_ = 0;
  179. eotPacket_ = NULL;
  180. pktRTS_ = 0;
  181. pktCTRL_ = 0;
  182. pktBEACON_ = 0;
  183. pktASSOCREP_ = 0;
  184. pktASSOCREQ_ = 0;
  185. pktAUTHENTICATE_ = 0;
  186. pktPROBEREQ_ = 0;
  187. pktPROBEREP_ = 0;
  188. BeaconTxtime_ = 0;
  189. infra_mode_ = 0;
  190. cw_ = phymib_.getCWMin();
  191. ssrc_ = slrc_ = 0;
  192. // Added by Sushmita
  193.         et_ = new EventTrace();
  194. sta_seqno_ = 1;
  195. cache_ = 0;
  196. cache_node_count_ = 0;
  197. client_list = NULL;
  198. ap_list = NULL;
  199. Pr = 0;
  200. ap_temp = -1;
  201. head = 0;
  202. ap_addr = -1;
  203. associated = 0;
  204. authenticated = 0;
  205. OnMinChannelTime = 0;
  206. OnMaxChannelTime = 0;
  207. Recv_Busy_ = 0;
  208. handoff= 0;
  209. // ssid_ = "0";
  210.         memset(priority_queue, 0, sizeof(priority_queue));
  211. // chk if basic/data rates are set
  212. // otherwise use bandwidth_ as default;
  213. Tcl& tcl = Tcl::instance();
  214. tcl.evalf("Mac/802_11 set basicRate_");
  215. if (strcmp(tcl.result(), "0") != 0) 
  216. bind_bw("basicRate_", &basicRate_);
  217. else
  218. basicRate_ = bandwidth_;
  219. tcl.evalf("Mac/802_11 set dataRate_");
  220. if (strcmp(tcl.result(), "0") != 0) 
  221. bind_bw("dataRate_", &dataRate_);
  222. else
  223. dataRate_ = bandwidth_;
  224. bind_bool("bugFix_timer_", &bugFix_timer_);
  225.         EOTtarget_ = 0;
  226.         bss_id_ = IBSS_ID;
  227. }
  228. int
  229. Mac802_11::command(int argc, const char*const* argv)
  230. {
  231. if (argc == 3) {
  232. if (strcmp(argv[1], "eot-target") == 0) {
  233. EOTtarget_ = (NsObject*) TclObject::lookup(argv[2]);
  234. if (EOTtarget_ == 0)
  235. return TCL_ERROR;
  236. return TCL_OK;
  237. }
  238. if (strcmp(argv[1], "ap") == 0) {
  239. ap_addr = addr();
  240. bss_id_ = addr();
  241. infra_mode_ = 1;
  242. mhBeacon_.start((Random::random() % cw_) * 
  243. phymib_.getSlotTime());
  244. return TCL_OK;
  245. if (strcmp(argv[1], "ScanType") == 0) {
  246. if (strcmp(argv[2], "ACTIVE") == 0) {
  247. ScanType_ = ACTIVE;
  248. infra_mode_ = 1;
  249. ap_list = NULL;
  250. mhProbe_.start(macmib_.getProbeDelay());
  251. } else if (strcmp(argv[2], "PASSIVE") == 0) {
  252. ScanType_ = PASSIVE;
  253. mhProbe_.start(macmib_.getChannelTime());
  254. infra_mode_ = 1;
  255. }
  256. return TCL_OK;
  257. } else if (strcmp(argv[1], "log-target") == 0) { 
  258. logtarget_ = (NsObject*) TclObject::lookup(argv[2]);
  259. if(logtarget_ == 0)
  260. return TCL_ERROR;
  261. return TCL_OK;
  262. } else if(strcmp(argv[1], "nodes") == 0) {
  263. if(cache_) return TCL_ERROR;
  264. cache_node_count_ = atoi(argv[2]);
  265. cache_ = new Host[cache_node_count_ + 1];
  266. assert(cache_);
  267. bzero(cache_, sizeof(Host) * (cache_node_count_+1 ));
  268. return TCL_OK;
  269. } else if(strcmp(argv[1], "eventtrace") == 0) {
  270. // command added to support event tracing by Sushmita
  271.                         et_ = (EventTrace *)TclObject::lookup(argv[2]);
  272.                         return (TCL_OK);
  273.                 }
  274. }
  275. return Mac::command(argc, argv);
  276. }
  277. // Added by Sushmita to support event tracing
  278. void Mac802_11::trace_event(char *eventtype, Packet *p) 
  279. {
  280.         if (et_ == NULL) return;
  281.         char *wrk = et_->buffer();
  282.         char *nwrk = et_->nbuffer();
  283.         //char *src_nodeaddr =
  284. //       Address::instance().print_nodeaddr(iph->saddr());
  285.         //char *dst_nodeaddr =
  286.         //      Address::instance().print_nodeaddr(iph->daddr());
  287.         struct hdr_mac802_11* dh = HDR_MAC802_11(p);
  288.         //struct hdr_cmn *ch = HDR_CMN(p);
  289. if(wrk != 0) {
  290. sprintf(wrk, "E -t "TIME_FORMAT" %s %2x ",
  291. et_->round(Scheduler::instance().clock()),
  292.                         eventtype,
  293.                         //ETHER_ADDR(dh->dh_sa)
  294.                         ETHER_ADDR(dh->dh_ta)
  295.                         );
  296.         }
  297.         if(nwrk != 0) {
  298.                 sprintf(nwrk, "E -t "TIME_FORMAT" %s %2x ",
  299.                         et_->round(Scheduler::instance().clock()),
  300.                         eventtype,
  301.                         //ETHER_ADDR(dh->dh_sa)
  302.                         ETHER_ADDR(dh->dh_ta)
  303.                         );
  304.         }
  305.         et_->dump();
  306. }
  307. /* ======================================================================
  308.    Debugging Routines
  309.    ====================================================================== */
  310. void
  311. Mac802_11::trace_pkt(Packet *p) 
  312. {
  313. struct hdr_cmn *ch = HDR_CMN(p);
  314. struct hdr_mac802_11* dh = HDR_MAC802_11(p);
  315. u_int16_t *t = (u_int16_t*) &dh->dh_fc;
  316. fprintf(stderr, "t[ %2x %2x %2x %2x ] %x %s %dn",
  317. *t, dh->dh_duration,
  318.  ETHER_ADDR(dh->dh_ra), ETHER_ADDR(dh->dh_ta),
  319. index_, packet_info.name(ch->ptype()), ch->size());
  320. }
  321. void
  322. Mac802_11::dump(char *fname)
  323. {
  324. fprintf(stderr,
  325. "n%s --- (INDEX: %d, time: %2.9f)n",
  326. fname, index_, Scheduler::instance().clock());
  327. fprintf(stderr,
  328. "ttx_state_: %x, rx_state_: %x, nav: %2.9f, idle: %dn",
  329. tx_state_, rx_state_, nav_, is_idle());
  330. fprintf(stderr,
  331. "tpktTx_: %lx, pktRx_: %lx, pktRTS_: %lx, pktCTRL_: %lx, pktBEACON_: %lx, pktASSOCREQ_: %lx, pktASSOCREP_: %lx, pktPROBEREQ_: %lx, pktPROBEREP_: %lx, pktAUTHENTICATE_: %lx, callback: %lxn", (long) pktTx_, (long) pktRx_, (long) pktRTS_,
  332. (long) pktCTRL_, (long) pktBEACON_, (long) pktASSOCREQ_, (long) pktASSOCREP_, (long) pktPROBEREQ_, (long) pktPROBEREP_, (long) pktAUTHENTICATE_, (long) callback_);
  333. fprintf(stderr,
  334. "tDefer: %d, Backoff: %d (%d), Recv: %d, Timer: %d Nav: %dn",
  335. mhDefer_.busy(), mhBackoff_.busy(), mhBackoff_.paused(),
  336. mhRecv_.busy(), mhSend_.busy(), mhNav_.busy());
  337. fprintf(stderr,
  338. "tBackoff Expire: %fn",
  339. mhBackoff_.expire());
  340. }
  341. /* ======================================================================
  342.    Packet Headers Routines
  343.    ====================================================================== */
  344. inline int
  345. Mac802_11::hdr_dst(char* hdr, int dst )
  346. {
  347. struct hdr_mac802_11 *dh = (struct hdr_mac802_11*) hdr;
  348.        if (dst > -2) {
  349.                if (bss_id() == ((int)IBSS_ID)) {
  350. STORE4BYTE(&dst, (dh->dh_ra));
  351. } else if ( addr() == bss_id_) {
  352. if ( find_client(dst) == 1 || (u_int32_t) dst == MAC_BROADCAST) {
  353. STORE4BYTE(&dst, (dh->dh_ra));
  354. } else {
  355. int dst_broadcast;
  356. dst_broadcast = MAC_BROADCAST;
  357. STORE4BYTE(&dst_broadcast, (dh->dh_ra));
  358. STORE4BYTE(&dst, (dh->dh_3a));
  359. dh->dh_fc.fc_to_ds      = 1;
  360. dh->dh_fc.fc_from_ds    = 1;
  361. }
  362. } else {
  363. STORE4BYTE(&bss_id_, (dh->dh_ra));
  364.                         STORE4BYTE(&dst, (dh->dh_3a));
  365. }
  366. }
  367.        return (u_int32_t)ETHER_ADDR(dh->dh_ra);
  368. }
  369. inline int 
  370. Mac802_11::hdr_src(char* hdr, int src )
  371. {
  372. struct hdr_mac802_11 *dh = (struct hdr_mac802_11*) hdr;
  373.         if(src > -2)
  374.                STORE4BYTE(&src, (dh->dh_ta));
  375.         return ETHER_ADDR(dh->dh_ta);
  376. }
  377. inline int 
  378. Mac802_11::hdr_type(char* hdr, u_int16_t type)
  379. {
  380. struct hdr_mac802_11 *dh = (struct hdr_mac802_11*) hdr;
  381. if(type)
  382. STORE2BYTE(&type,(dh->dh_body));
  383. return GET2BYTE(dh->dh_body);
  384. }
  385. /* ======================================================================
  386.    Misc Routines
  387.    ====================================================================== */
  388. inline int
  389. Mac802_11::is_idle()
  390. {
  391. if(rx_state_ != MAC_IDLE)
  392. return 0;
  393. if(tx_state_ != MAC_IDLE)
  394. return 0;
  395. if(nav_ > Scheduler::instance().clock())
  396. return 0;
  397. return 1;
  398. }
  399. void
  400. Mac802_11::discard(Packet *p, const char* why)
  401. {
  402. hdr_mac802_11* mh = HDR_MAC802_11(p);
  403. hdr_cmn *ch = HDR_CMN(p);
  404. /* if the rcvd pkt contains errors, a real MAC layer couldn't
  405.    necessarily read any data from it, so we just toss it now */
  406. if(ch->error() != 0) {
  407. Packet::free(p);
  408. return;
  409. }
  410. switch(mh->dh_fc.fc_type) {
  411. case MAC_Type_Management:
  412. switch(mh->dh_fc.fc_subtype) {
  413. case MAC_Subtype_Auth:
  414.  if((u_int32_t)ETHER_ADDR(mh->dh_ra) == (u_int32_t)index_) {
  415. drop(p, why);
  416. return;
  417. }
  418. break;
  419. // drop(p, why);
  420. // return;
  421. case MAC_Subtype_AssocReq:
  422. if((u_int32_t)ETHER_ADDR(mh->dh_ra) == (u_int32_t)index_) {
  423. drop(p, why);
  424. return;
  425. }
  426. break;
  427. case MAC_Subtype_AssocRep:
  428. break;
  429. case MAC_Subtype_ProbeReq:
  430.  if((u_int32_t)ETHER_ADDR(mh->dh_ra) == (u_int32_t)index_) {
  431. drop(p, why);
  432. return;
  433. }
  434. break;
  435. case MAC_Subtype_ProbeRep:
  436. break;
  437. case MAC_Subtype_80211_Beacon:
  438. break;
  439. default:
  440. fprintf(stderr, "invalid MAC Management subtypen");
  441. exit(1);
  442. }
  443. break;
  444. case MAC_Type_Control:
  445. switch(mh->dh_fc.fc_subtype) {
  446. case MAC_Subtype_RTS:
  447.  if((u_int32_t)ETHER_ADDR(mh->dh_ta) ==  (u_int32_t)index_) {
  448. drop(p, why);
  449. return;
  450. }
  451. /* fall through - if necessary */
  452. case MAC_Subtype_CTS:
  453. case MAC_Subtype_ACK:
  454. if((u_int32_t)ETHER_ADDR(mh->dh_ra) == (u_int32_t)index_) {
  455. drop(p, why);
  456. return;
  457. }
  458. break;
  459. default:
  460. fprintf(stderr, "invalid MAC Control subtypen");
  461. exit(1);
  462. }
  463. break;
  464. case MAC_Type_Data:
  465. switch(mh->dh_fc.fc_subtype) {
  466. case MAC_Subtype_Data:
  467. if((u_int32_t)ETHER_ADDR(mh->dh_ra) == 
  468.                            (u_int32_t)index_ ||
  469.                           (u_int32_t)ETHER_ADDR(mh->dh_ta) == 
  470.                            (u_int32_t)index_ ||
  471.                           ((u_int32_t)ETHER_ADDR(mh->dh_ra) == MAC_BROADCAST && mh->dh_fc.fc_to_ds == 0)) {
  472.                                 drop(p,why);
  473.                                 return;
  474. }
  475. break;
  476. default:
  477. fprintf(stderr, "invalid MAC Data subtypen");
  478. exit(1);
  479. }
  480. break;
  481. default:
  482. fprintf(stderr, "invalid MAC type (%x)n", mh->dh_fc.fc_type);
  483. trace_pkt(p);
  484. exit(1);
  485. }
  486. Packet::free(p);
  487. }
  488. void
  489. Mac802_11::capture(Packet *p)
  490. {
  491. /*
  492.  * Update the NAV so that this does not screw
  493.  * up carrier sense.
  494.  */
  495. set_nav(usec(phymib_.getEIFS() + txtime(p)));
  496. Packet::free(p);
  497. }
  498. void
  499. Mac802_11::collision(Packet *p)
  500. {
  501. switch(rx_state_) {
  502. case MAC_RECV:
  503. setRxState(MAC_COLL);
  504. /* fall through */
  505. case MAC_COLL:
  506. assert(pktRx_);
  507. assert(mhRecv_.busy());
  508. /*
  509.  *  Since a collision has occurred, figure out
  510.  *  which packet that caused the collision will
  511.  *  "last" the longest.  Make this packet,
  512.  *  pktRx_ and reset the Recv Timer if necessary.
  513.  */
  514. if(txtime(p) > mhRecv_.expire()) {
  515. mhRecv_.stop();
  516. discard(pktRx_, DROP_MAC_COLLISION);
  517. pktRx_ = p;
  518. mhRecv_.start(txtime(pktRx_));
  519. }
  520. else {
  521. discard(p, DROP_MAC_COLLISION);
  522. }
  523. break;
  524. default:
  525. assert(0);
  526. }
  527. }
  528. void
  529. Mac802_11::tx_resume()
  530. {
  531. double rTime;
  532. assert(mhSend_.busy() == 0);
  533. assert(mhDefer_.busy() == 0);
  534. if(pktCTRL_) {
  535. /*
  536.  *  Need to send a CTS or ACK.
  537.  */
  538. mhDefer_.start(phymib_.getSIFS());
  539. } else if(pktRTS_) {
  540. if (mhBackoff_.busy() == 0) {
  541. if (bugFix_timer_) {
  542. mhBackoff_.start(cw_, is_idle(), 
  543.  phymib_.getDIFS());
  544. }
  545. else {
  546. rTime = (Random::random() % cw_) * 
  547. phymib_.getSlotTime();
  548. mhDefer_.start( phymib_.getDIFS() + rTime);
  549. }
  550. }
  551. } else if(pktTx_) {
  552. if (mhBackoff_.busy() == 0) {
  553. hdr_cmn *ch = HDR_CMN(pktTx_);
  554. struct hdr_mac802_11 *mh = HDR_MAC802_11(pktTx_);
  555. if ((u_int32_t) ch->size() < macmib_.getRTSThreshold()
  556.     || (u_int32_t) ETHER_ADDR(mh->dh_ra) == MAC_BROADCAST) {
  557. if (bugFix_timer_) {
  558. mhBackoff_.start(cw_, is_idle(), 
  559.  phymib_.getDIFS());
  560. }
  561. else {
  562. rTime = (Random::random() % cw_)
  563. * phymib_.getSlotTime();
  564. mhDefer_.start(phymib_.getDIFS() + 
  565.        rTime);
  566. }
  567.                         } else {
  568. mhDefer_.start(phymib_.getSIFS());
  569.                         }
  570. }
  571. } else if(callback_) {
  572. Handler *h = callback_;
  573. callback_ = 0;
  574. h->handle((Event*) 0);
  575. }
  576. setTxState(MAC_IDLE);
  577. }
  578. void
  579. Mac802_11::rx_resume()
  580. {
  581. assert(pktRx_ == 0);
  582. assert(mhRecv_.busy() == 0);
  583. setRxState(MAC_IDLE);
  584. }
  585. /* ======================================================================
  586.    Timer Handler Routines
  587.    ====================================================================== */
  588. void
  589. Mac802_11::backoffHandler()
  590. {
  591. if(addr() != bss_id_ && infra_mode_ == 1) {
  592. if(check_pktPROBEREQ() == 0)
  593. return;
  594. if(check_pktAUTHENTICATE() == 0)
  595. return;
  596. if(check_pktASSOCREQ() == 0)
  597. return;
  598. }
  599. if(pktCTRL_) {
  600. assert(mhSend_.busy() || mhDefer_.busy());
  601. return;
  602. }
  603. if ( addr() == bss_id_ ) {
  604. if (pktPROBEREP_ && priority_queue[head] == 1) {
  605. if (check_pktPROBEREP() == 0) 
  606. return;
  607. } else if (pktBEACON_ && priority_queue[head] == 2) {
  608. if (check_pktBEACON() == 0) {
  609. return;
  610. }
  611. } else if (pktAUTHENTICATE_ && priority_queue[head] == 3) { 
  612. if (check_pktAUTHENTICATE() == 0)
  613. return;
  614. } else if(pktASSOCREP_ && priority_queue[head] == 4) {
  615. if (check_pktASSOCREP() == 0)
  616. return;
  617. }
  618. }
  619. if(check_pktRTS() == 0)
  620. return;
  621. if(check_pktTx() == 0)
  622. return;
  623. }
  624. void
  625. Mac802_11::BeaconHandler()
  626. {
  627. mhBeacon_.start(phymib_.getBeaconInterval());
  628. sendBEACON(index_);
  629. }
  630. // Probe timer is used for multiple purposes. It can be set to 4 different values
  631. // Probe Delay, MinChannelTime and MaxChannelTime -  for Active Scanning
  632. // ChannelTime - Passive Scanning
  633. void
  634. Mac802_11::ProbeHandler()
  635. {
  636. if (ScanType_ == ACTIVE) {
  637. if ( (bss_id_ == (int)IBSS_ID || handoff == 1) && OnMinChannelTime == 0 && Recv_Busy_ == 0 && OnMaxChannelTime == 0) {
  638. if (strongest_ap() < 0) {   // Probe delay over - Active Scan starts here, when the ap_table has not been built yet
  639. sendPROBEREQ(MAC_BROADCAST); 
  640. return;
  641. } else {
  642. checkAssocAuthStatus();  // MaxChannelTime Over - Handoff is taking place and ap_table is built, complete authentication and (re)association
  643. return;
  644. }
  645. }
  646. else if (OnMinChannelTime == 1 && Recv_Busy_ == 1 && OnMaxChannelTime == 0) {
  647. // MinChannelTime Over - receiver indicated busy before timer expiry, hence Probe timer should be continued for MaxChannelTime, to reeive all probe responses
  648. OnMinChannelTime = 0;
  649. OnMaxChannelTime = 1;
  650. mhProbe_.start(macmib_.getMaxChannelTime()); 
  651. return;
  652. }
  653. else if (OnMinChannelTime == 0 && Recv_Busy_ == 1 && OnMaxChannelTime == 1) {
  654. // MaxChannelTime Over - Active Scanning is over, start authentication and association
  655. OnMaxChannelTime = 0;
  656. Recv_Busy_ = 0;
  657. if (strongest_ap() > -1)
  658. active_scan();
  659. else 
  660. printf("No APs in rangen");
  661. return;
  662. }
  663. else if (OnMinChannelTime == 1 && Recv_Busy_ == 0 && OnMaxChannelTime == 0) {
  664. //printf("Out of range of any Access Point or channel without APsn");
  665. OnMinChannelTime = 0;
  666. deletelist(); //  MinChannelTime Over - Delete ap_table
  667. return;
  668. }
  669. } else {
  670. if (ScanType_ == PASSIVE && bss_id_ == (int)IBSS_ID) {
  671.  //  ChannelTime Over - Passive Scanning is over, start authentication and association
  672. passive_scan();  // 
  673. return;
  674. }
  675. }
  676. if (bss_id_ != (int)IBSS_ID && !mhProbe_.busy()) {
  677. // Start Authentication and Association
  678. checkAssocAuthStatus();
  679. }
  680. }
  681. void
  682. Mac802_11::deferHandler()
  683. {
  684. assert(pktCTRL_ || pktRTS_ || pktTx_);
  685. if(check_pktCTRL() == 0)
  686. return;
  687. assert(mhBackoff_.busy() == 0);
  688. if(check_pktRTS() == 0)
  689. return;
  690. if(check_pktTx() == 0)
  691. return;
  692. }
  693. void
  694. Mac802_11::navHandler()
  695. {
  696. if(is_idle() && mhBackoff_.paused())
  697. mhBackoff_.resume(phymib_.getDIFS());
  698. }
  699. void
  700. Mac802_11::recvHandler()
  701. {
  702. recv_timer();
  703. }
  704. void
  705. Mac802_11::sendHandler()
  706. {
  707. send_timer();
  708. }
  709. void
  710. Mac802_11::txHandler()
  711. {
  712. if (EOTtarget_) {
  713. assert(eotPacket_);
  714. EOTtarget_->recv(eotPacket_, (Handler *) 0);
  715. eotPacket_ = NULL;
  716. }
  717. tx_active_ = 0;
  718. }
  719. /* ======================================================================
  720.    The "real" Timer Handler Routines
  721.    ====================================================================== */
  722. void
  723. Mac802_11::send_timer()
  724. {
  725. switch(tx_state_) {
  726. case MAC_MGMT:
  727. if (pktAUTHENTICATE_) {
  728. assert(pktAUTHENTICATE_);
  729. Packet::free(pktAUTHENTICATE_);
  730. pktAUTHENTICATE_ = 0;
  731. if(addr() == bss_id_) {
  732. if (end() > (head + 1)) {
  733. shift_priority_queue();
  734. assert(mhBackoff_.busy() == 0);
  735. mhBackoff_.start(cw_, is_idle());
  736. } else 
  737. priority_queue[head] = 0;
  738. } else
  739. checkAssocAuthStatus();
  740. break;
  741. }
  742.   if (pktASSOCREQ_) {
  743. assert(pktASSOCREQ_);
  744. Packet::free(pktASSOCREQ_);
  745. pktASSOCREQ_ = 0;
  746. checkAssocAuthStatus();
  747. break;
  748.   }
  749. if (pktASSOCREP_) {
  750. assert(pktASSOCREP_);
  751. Packet::free(pktASSOCREP_);
  752. pktASSOCREP_ = 0;
  753. if (end() > (head + 1)) {
  754. shift_priority_queue();
  755. assert(mhBackoff_.busy() == 0);
  756. mhBackoff_.start(cw_, is_idle());
  757. } else 
  758. priority_queue[head] = 0;
  759. break;
  760. }
  761. if (pktPROBEREQ_) {
  762. assert(pktPROBEREQ_);
  763. Packet::free(pktPROBEREQ_);
  764. pktPROBEREQ_ = 0;
  765. break;
  766.   }
  767. if (pktPROBEREP_) {
  768. assert(pktPROBEREP_);
  769. Packet::free(pktPROBEREP_);
  770. pktPROBEREP_ = 0;
  771. if (end() > (head + 1)) {
  772. shift_priority_queue();
  773. assert(mhBackoff_.busy() == 0);
  774. mhBackoff_.start(cw_, is_idle());
  775. } else 
  776. priority_queue[head] = 0;
  777. break;
  778. }
  779. case MAC_BCN:
  780. assert(pktBEACON_);
  781. Packet::free(pktBEACON_);
  782. pktBEACON_ = 0;
  783. if (end() > (head + 1)) {
  784. shift_priority_queue();
  785. assert(mhBackoff_.busy() == 0);
  786. mhBackoff_.start(cw_, is_idle());
  787. } else 
  788. priority_queue[head] = 0;
  789. break;
  790. case MAC_RTS:
  791. RetransmitRTS();
  792. break;
  793. /*
  794.  * Sent a CTS, but did not receive a DATA packet.
  795.  */
  796. case MAC_CTS:
  797. assert(pktCTRL_);
  798. Packet::free(pktCTRL_); 
  799. pktCTRL_ = 0;
  800. break;
  801. /*
  802.  * Sent DATA, but did not receive an ACK packet.
  803.  */
  804. case MAC_SEND:
  805. RetransmitDATA();
  806. break;
  807. /*
  808.  * Sent an ACK, and now ready to resume transmission.
  809.  */
  810. case MAC_ACK:
  811. assert(pktCTRL_);
  812. Packet::free(pktCTRL_); 
  813. pktCTRL_ = 0;
  814. break;
  815. case MAC_IDLE:
  816. break;
  817. default:
  818. assert(0);
  819. }
  820. tx_resume();
  821. }
  822. /* ======================================================================
  823.    Outgoing Packet Routines
  824.    ====================================================================== */
  825. int
  826. Mac802_11::check_pktCTRL()
  827. {
  828. struct hdr_mac802_11 *mh;
  829. double timeout;
  830. if(pktCTRL_ == 0)
  831. return -1;
  832. if(tx_state_ == MAC_CTS || tx_state_ == MAC_ACK)
  833. return -1;
  834. mh = HDR_MAC802_11(pktCTRL_);
  835.   
  836. switch(mh->dh_fc.fc_subtype) {
  837. /*
  838.  *  If the medium is not IDLE, don't send the CTS.
  839.  */
  840. case MAC_Subtype_CTS:
  841. if(!is_idle()) {
  842. discard(pktCTRL_, DROP_MAC_BUSY); pktCTRL_ = 0;
  843. return 0;
  844. }
  845. setTxState(MAC_CTS);
  846. /*
  847.  * timeout:  cts + data tx time calculated by
  848.  *           adding cts tx time to the cts duration
  849.  *           minus ack tx time -- this timeout is
  850.  *           a guess since it is unspecified
  851.  *           (note: mh->dh_duration == cf->cf_duration)
  852.  */
  853.  timeout = txtime(phymib_.getCTSlen(), basicRate_)
  854.                         + DSSS_MaxPropagationDelay                      // XXX
  855.                         + sec(mh->dh_duration)
  856.                         + DSSS_MaxPropagationDelay                      // XXX
  857.                        - phymib_.getSIFS()
  858.                        - txtime(phymib_.getACKlen(), basicRate_);
  859. break;
  860. /*
  861.  * IEEE 802.11 specs, section 9.2.8
  862.  * Acknowledments are sent after an SIFS, without regard to
  863.  * the busy/idle state of the medium.
  864.  */
  865. case MAC_Subtype_ACK:
  866. setTxState(MAC_ACK);
  867. timeout = txtime(phymib_.getACKlen(), basicRate_);
  868. break;
  869. default:
  870. fprintf(stderr, "check_pktCTRL:Invalid MAC Control subtypen");
  871. exit(1);
  872. }
  873. transmit(pktCTRL_, timeout);
  874. return 0;
  875. }
  876. int
  877. Mac802_11::check_pktRTS()
  878. {
  879. struct hdr_mac802_11 *mh;
  880. double timeout;
  881. assert(mhBackoff_.busy() == 0);
  882. if(pktRTS_ == 0)
  883.   return -1;
  884. mh = HDR_MAC802_11(pktRTS_);
  885.   switch(mh->dh_fc.fc_subtype) {
  886. case MAC_Subtype_RTS:
  887. if(! is_idle()) {
  888. inc_cw();
  889. mhBackoff_.start(cw_, is_idle());
  890. return 0;
  891. }
  892. setTxState(MAC_RTS);
  893. timeout = txtime(phymib_.getRTSlen(), basicRate_)
  894. + DSSS_MaxPropagationDelay                      // XXX
  895. + phymib_.getSIFS()
  896. + txtime(phymib_.getCTSlen(), basicRate_)
  897. + DSSS_MaxPropagationDelay;
  898. break;
  899. default:
  900. fprintf(stderr, "check_pktRTS:Invalid MAC Control subtypen");
  901. exit(1);
  902. }
  903. transmit(pktRTS_, timeout);
  904.   
  905. return 0;
  906. }
  907. int
  908. Mac802_11::check_pktTx()
  909. {
  910. struct hdr_mac802_11 *mh;
  911. double timeout;
  912. assert(mhBackoff_.busy() == 0);
  913. if(pktTx_ == 0)
  914. return -1;
  915. mh = HDR_MAC802_11(pktTx_);
  916. if (addr() != bss_id_ && bss_id_ != (int)IBSS_ID) {
  917. if (handoff == 0)
  918. STORE4BYTE(&bss_id_, (mh->dh_ra));
  919. }
  920. switch(mh->dh_fc.fc_subtype) {
  921. case MAC_Subtype_Data:
  922. if(! is_idle()) {
  923. sendRTS(ETHER_ADDR(mh->dh_ra));
  924. inc_cw();
  925. mhBackoff_.start(cw_, is_idle());
  926. return 0;
  927. }
  928. setTxState(MAC_SEND);
  929. if((u_int32_t)ETHER_ADDR(mh->dh_ra) != MAC_BROADCAST)
  930.                         timeout = txtime(pktTx_)
  931.                                 + DSSS_MaxPropagationDelay              // XXX
  932.                                + phymib_.getSIFS()
  933.                                + txtime(phymib_.getACKlen(), basicRate_)
  934.                                + DSSS_MaxPropagationDelay;             // XXX
  935. else
  936. timeout = txtime(pktTx_);
  937. break;
  938. default:
  939. fprintf(stderr, "check_pktTx:Invalid MAC Control subtypen");
  940. exit(1);
  941. }
  942. transmit(pktTx_, timeout);
  943. return 0;
  944. }
  945. /*
  946.  * Low-level transmit functions that actually place the packet onto
  947.  * the channel.
  948.  */
  949. void
  950. Mac802_11::sendRTS(int dst)
  951. {
  952. Packet *p = Packet::alloc();
  953. hdr_cmn* ch = HDR_CMN(p);
  954. struct rts_frame *rf = (struct rts_frame*)p->access(hdr_mac::offset_);
  955. assert(pktTx_);
  956. assert(pktRTS_ == 0);
  957. /*
  958.  *  If the size of the packet is larger than the
  959.  *  RTSThreshold, then perform the RTS/CTS exchange.
  960.  */
  961. if( (u_int32_t) HDR_CMN(pktTx_)->size() < macmib_.getRTSThreshold() ||
  962.             (u_int32_t) dst == MAC_BROADCAST) {
  963. Packet::free(p);
  964. return;
  965. }
  966. ch->uid() = 0;
  967. ch->ptype() = PT_MAC;
  968. ch->size() = phymib_.getRTSlen();
  969. ch->iface() = -2;
  970. ch->error() = 0;
  971. bzero(rf, MAC_HDR_LEN);
  972. rf->rf_fc.fc_protocol_version = MAC_ProtocolVersion;
  973.   rf->rf_fc.fc_type = MAC_Type_Control;
  974.   rf->rf_fc.fc_subtype = MAC_Subtype_RTS;
  975. rf->rf_fc.fc_to_ds = 0;
  976. rf->rf_fc.fc_from_ds = 0;
  977.   rf->rf_fc.fc_more_frag = 0;
  978.   rf->rf_fc.fc_retry = 0;
  979.   rf->rf_fc.fc_pwr_mgt = 0;
  980.   rf->rf_fc.fc_more_data = 0;
  981.   rf->rf_fc.fc_wep = 0;
  982.   rf->rf_fc.fc_order = 0;
  983. //rf->rf_duration = RTS_DURATION(pktTx_);
  984. STORE4BYTE(&dst, (rf->rf_ra));
  985. /* store rts tx time */
  986.   ch->txtime() = txtime(ch->size(), basicRate_);
  987. STORE4BYTE(&index_, (rf->rf_ta));
  988. /* calculate rts duration field */
  989. rf->rf_duration = usec(phymib_.getSIFS()
  990.        + txtime(phymib_.getCTSlen(), basicRate_)
  991.        + phymib_.getSIFS()
  992.                                + txtime(pktTx_)
  993.        + phymib_.getSIFS()
  994.        + txtime(phymib_.getACKlen(), basicRate_));
  995. pktRTS_ = p;
  996. }
  997. void
  998. Mac802_11::sendCTS(int dst, double rts_duration)
  999. {
  1000. Packet *p = Packet::alloc();
  1001. hdr_cmn* ch = HDR_CMN(p);
  1002. struct cts_frame *cf = (struct cts_frame*)p->access(hdr_mac::offset_);
  1003. assert(pktCTRL_ == 0);
  1004. ch->uid() = 0;
  1005. ch->ptype() = PT_MAC;
  1006. ch->size() = phymib_.getCTSlen();
  1007. ch->iface() = -2;
  1008. ch->error() = 0;
  1009. //ch->direction() = hdr_cmn::DOWN;
  1010. bzero(cf, MAC_HDR_LEN);
  1011. cf->cf_fc.fc_protocol_version = MAC_ProtocolVersion;
  1012. cf->cf_fc.fc_type = MAC_Type_Control;
  1013. cf->cf_fc.fc_subtype = MAC_Subtype_CTS;
  1014.   cf->cf_fc.fc_to_ds = 0;
  1015. cf->cf_fc.fc_from_ds = 0;
  1016.   cf->cf_fc.fc_more_frag = 0;
  1017.   cf->cf_fc.fc_retry = 0;
  1018.   cf->cf_fc.fc_pwr_mgt = 0;
  1019.   cf->cf_fc.fc_more_data = 0;
  1020.   cf->cf_fc.fc_wep = 0;
  1021.   cf->cf_fc.fc_order = 0;
  1022. //cf->cf_duration = CTS_DURATION(rts_duration);
  1023. STORE4BYTE(&dst, (cf->cf_ra));
  1024. /* store cts tx time */
  1025. ch->txtime() = txtime(ch->size(), basicRate_);
  1026. /* calculate cts duration */
  1027. cf->cf_duration = usec(sec(rts_duration)
  1028.                               - phymib_.getSIFS()
  1029.                               - txtime(phymib_.getCTSlen(), basicRate_));
  1030. pktCTRL_ = p;
  1031. }
  1032. void
  1033. Mac802_11::sendACK(int dst)
  1034. {
  1035. Packet *p = Packet::alloc();
  1036. hdr_cmn* ch = HDR_CMN(p);
  1037. struct ack_frame *af = (struct ack_frame*)p->access(hdr_mac::offset_);
  1038. assert(pktCTRL_ == 0);
  1039. ch->uid() = 0;
  1040. ch->ptype() = PT_MAC;
  1041. // CHANGE WRT Mike's code
  1042. ch->size() = phymib_.getACKlen();
  1043. ch->iface() = -2;
  1044. ch->error() = 0;
  1045. bzero(af, MAC_HDR_LEN);
  1046. af->af_fc.fc_protocol_version = MAC_ProtocolVersion;
  1047.   af->af_fc.fc_type = MAC_Type_Control;
  1048.   af->af_fc.fc_subtype = MAC_Subtype_ACK;
  1049.   af->af_fc.fc_to_ds = 0;
  1050.   af->af_fc.fc_from_ds = 0;
  1051.   af->af_fc.fc_more_frag = 0;
  1052.   af->af_fc.fc_retry = 0;
  1053.   af->af_fc.fc_pwr_mgt = 0;
  1054.   af->af_fc.fc_more_data = 0;
  1055.   af->af_fc.fc_wep = 0;
  1056.   af->af_fc.fc_order = 0;
  1057. //af->af_duration = ACK_DURATION();
  1058. STORE4BYTE(&dst, (af->af_ra));
  1059. /* store ack tx time */
  1060.   ch->txtime() = txtime(ch->size(), basicRate_);
  1061. /* calculate ack duration */
  1062.   af->af_duration = 0;
  1063. pktCTRL_ = p;
  1064. }
  1065. void
  1066. Mac802_11::sendDATA(Packet *p)
  1067. {
  1068. hdr_cmn* ch = HDR_CMN(p);
  1069. struct hdr_mac802_11* dh = HDR_MAC802_11(p);
  1070. u_int32_t dst = ETHER_ADDR(dh->dh_ra);
  1071. assert(pktTx_ == 0);
  1072. /*
  1073.  * Update the MAC header
  1074.  */
  1075. ch->size() += phymib_.getHdrLen11();
  1076. dh->dh_fc.fc_protocol_version = MAC_ProtocolVersion;
  1077. dh->dh_fc.fc_type       = MAC_Type_Data;
  1078. dh->dh_fc.fc_subtype    = MAC_Subtype_Data;
  1079. if ( bss_id_ != (int)IBSS_ID ) {
  1080. if ( index_ == ap_addr ) {
  1081. if (dh->dh_fc.fc_to_ds == 0) {
  1082. if (find_client(dst) == 1 || dst == MAC_BROADCAST) {
  1083.   dh->dh_fc.fc_to_ds      = 0;
  1084. dh->dh_fc.fc_from_ds    = 1;
  1085. }
  1086. } else {
  1087. dh->dh_fc.fc_to_ds = 1;
  1088. dh->dh_fc.fc_from_ds = 0;
  1089. }
  1090. } else {
  1091. dh->dh_fc.fc_to_ds = 0;
  1092. dh->dh_fc.fc_from_ds = 0;
  1093. }
  1094. dh->dh_fc.fc_more_frag  = 0;
  1095. dh->dh_fc.fc_retry      = 0;
  1096. dh->dh_fc.fc_pwr_mgt    = 0;
  1097. dh->dh_fc.fc_more_data  = 0;
  1098. dh->dh_fc.fc_wep        = 0;
  1099. dh->dh_fc.fc_order      = 0;
  1100. /* store data tx time */
  1101.   ch->txtime() = txtime(ch->size(), dataRate_);
  1102. if((u_int32_t)ETHER_ADDR(dh->dh_ra) != MAC_BROADCAST) {
  1103. /* store data tx time for unicast packets */
  1104. ch->txtime() = txtime(ch->size(), dataRate_);
  1105. dh->dh_duration = usec(txtime(phymib_.getACKlen(), basicRate_)
  1106.        + phymib_.getSIFS());
  1107. } else {
  1108. /* store data tx time for broadcast packets (see 9.6) */
  1109. ch->txtime() = txtime(ch->size(), basicRate_);
  1110. dh->dh_duration = 0;
  1111. }
  1112. pktTx_ = p;
  1113. }
  1114. /* ======================================================================
  1115.    Retransmission Routines
  1116.    ====================================================================== */
  1117. void
  1118. Mac802_11::RetransmitRTS()
  1119. {
  1120. assert(pktTx_);
  1121. assert(pktRTS_);
  1122. assert(mhBackoff_.busy() == 0);
  1123. macmib_.RTSFailureCount++;
  1124. ssrc_ += 1; // STA Short Retry Count
  1125. if(ssrc_ >= macmib_.getShortRetryLimit()) {
  1126. discard(pktRTS_, DROP_MAC_RETRY_COUNT_EXCEEDED); pktRTS_ = 0;
  1127. /* tell the callback the send operation failed 
  1128.    before discarding the packet */
  1129. hdr_cmn *ch = HDR_CMN(pktTx_);
  1130. if (ch->xmit_failure_) {
  1131.                         /*
  1132.                          *  Need to remove the MAC header so that 
  1133.                          *  re-cycled packets don't keep getting
  1134.                          *  bigger.
  1135.                          */
  1136. ch->size() -= phymib_.getHdrLen11();
  1137.                         ch->xmit_reason_ = XMIT_REASON_RTS;
  1138.                         ch->xmit_failure_(pktTx_->copy(),
  1139.                                           ch->xmit_failure_data_);
  1140.                 }
  1141. discard(pktTx_, DROP_MAC_RETRY_COUNT_EXCEEDED); 
  1142. pktTx_ = 0;
  1143. ssrc_ = 0;
  1144. rst_cw();
  1145. } else {
  1146. struct rts_frame *rf;
  1147. rf = (struct rts_frame*)pktRTS_->access(hdr_mac::offset_);
  1148. rf->rf_fc.fc_retry = 1;
  1149. inc_cw();
  1150. mhBackoff_.start(cw_, is_idle());
  1151. }
  1152. }
  1153. void
  1154. Mac802_11::RetransmitDATA()
  1155. {
  1156. struct hdr_cmn *ch;
  1157. struct hdr_mac802_11 *mh;
  1158. u_int32_t *rcount, thresh;
  1159. assert(mhBackoff_.busy() == 0);
  1160. assert(pktTx_);
  1161. assert(pktRTS_ == 0);
  1162. ch = HDR_CMN(pktTx_);
  1163. mh = HDR_MAC802_11(pktTx_);
  1164. /*
  1165.  *  Broadcast packets don't get ACKed and therefore
  1166.  *  are never retransmitted.
  1167.  */
  1168. if((u_int32_t)ETHER_ADDR(mh->dh_ra) == MAC_BROADCAST) {
  1169. Packet::free(pktTx_); 
  1170. pktTx_ = 0;
  1171. /*
  1172.  * Backoff at end of TX.
  1173.  */
  1174. rst_cw();
  1175. mhBackoff_.start(cw_, is_idle());
  1176. return;
  1177. }
  1178. macmib_.ACKFailureCount++;
  1179. if((u_int32_t) ch->size() <= macmib_.getRTSThreshold()) {
  1180.                 rcount = &ssrc_;
  1181.                thresh = macmib_.getShortRetryLimit();
  1182.         } else {
  1183.                 rcount = &slrc_;
  1184.                thresh = macmib_.getLongRetryLimit();
  1185.         }
  1186. (*rcount)++;
  1187. if (*rcount == 3 && handoff == 0) {
  1188. //start handoff process
  1189. printf("Client %d: Handoff Attemptedn",index_);
  1190. associated = 0;
  1191. authenticated = 0;
  1192. handoff = 1;
  1193. ScanType_ = ACTIVE;
  1194. sendPROBEREQ(MAC_BROADCAST);
  1195. return;
  1196. }
  1197. if(*rcount >= thresh) {
  1198. /* IEEE Spec section 9.2.3.5 says this should be greater than
  1199.    or equal */
  1200. macmib_.FailedCount++;
  1201. /* tell the callback the send operation failed 
  1202.    before discarding the packet */
  1203. hdr_cmn *ch = HDR_CMN(pktTx_);
  1204. if (ch->xmit_failure_) {
  1205.                         ch->size() -= phymib_.getHdrLen11();
  1206. ch->xmit_reason_ = XMIT_REASON_ACK;
  1207.                         ch->xmit_failure_(pktTx_->copy(),
  1208.                                           ch->xmit_failure_data_);
  1209.                 }
  1210. discard(pktTx_, DROP_MAC_RETRY_COUNT_EXCEEDED); 
  1211. pktTx_ = 0;
  1212. *rcount = 0;
  1213. rst_cw();
  1214. }
  1215. else {
  1216. struct hdr_mac802_11 *dh;
  1217. dh = HDR_MAC802_11(pktTx_);
  1218. dh->dh_fc.fc_retry = 1;
  1219. sendRTS(ETHER_ADDR(mh->dh_ra));
  1220. inc_cw();
  1221. mhBackoff_.start(cw_, is_idle());
  1222. }
  1223. }
  1224. void
  1225. Mac802_11::RetransmitPROBEREP()
  1226. {
  1227. double rTime;
  1228. if(mhBackoff_.busy() == 0) {
  1229. if(is_idle()) {
  1230. if (mhDefer_.busy() == 0) {
  1231. /*
  1232.  * If we are already deferring, there is no
  1233.  * need to reset the Defer timer.
  1234.  */
  1235. if (bugFix_timer_) {
  1236.   mhBackoff_.start(cw_, is_idle(), 
  1237.   phymib_.getDIFS());
  1238. priority_queue[head] = 1;
  1239. }
  1240. else {
  1241. rTime = (Random::random() % cw_)
  1242. * (phymib_.getSlotTime());
  1243. mhDefer_.start(phymib_.getDIFS() + 
  1244.        rTime);
  1245. }
  1246. }
  1247. } else {
  1248. /*
  1249.  * If the medium is NOT IDLE, then we start
  1250.  * the backoff timer.
  1251.  */
  1252. mhBackoff_.start(cw_, is_idle());
  1253. priority_queue[head] = 1;
  1254. }
  1255. } else {
  1256. priority_queue[end()] = 1;
  1257. }
  1258. }
  1259. void
  1260. Mac802_11::RetransmitAUTHENTICATE()
  1261. {
  1262. double rTime;
  1263. if(mhBackoff_.busy() == 0) {
  1264. if(is_idle()) {
  1265. if (mhDefer_.busy() == 0) {
  1266. /*
  1267.  * If we are already deferring, there is no
  1268.  * need to reset the Defer timer.
  1269.  */
  1270. if (bugFix_timer_) {
  1271.   mhBackoff_.start(cw_, is_idle(), 
  1272.   phymib_.getDIFS());
  1273. priority_queue[head] = 3;
  1274. }
  1275. else {
  1276. rTime = (Random::random() % cw_)
  1277. * (phymib_.getSlotTime());
  1278. mhDefer_.start(phymib_.getDIFS() + 
  1279.        rTime);
  1280. }
  1281. }
  1282. } else {
  1283. /*
  1284.  * If the medium is NOT IDLE, then we start
  1285.  * the backoff timer.
  1286.  */
  1287. mhBackoff_.start(cw_, is_idle());
  1288. priority_queue[head] = 3;
  1289. }
  1290. } else {
  1291. priority_queue[end()] = 3;
  1292. }
  1293. }
  1294. void
  1295. Mac802_11::RetransmitASSOCREP()
  1296. {
  1297. double rTime;
  1298. if(mhBackoff_.busy() == 0) {
  1299. if(is_idle()) {
  1300. if (mhDefer_.busy() == 0) {
  1301. /*
  1302.  * If we are already deferring, there is no
  1303.  * need to reset the Defer timer.
  1304.  */
  1305. if (bugFix_timer_) {
  1306.   mhBackoff_.start(cw_, is_idle(), 
  1307.   phymib_.getDIFS());
  1308. priority_queue[head] = 4;
  1309. }
  1310. else {
  1311. rTime = (Random::random() % cw_)
  1312. * (phymib_.getSlotTime());
  1313. mhDefer_.start(phymib_.getDIFS() + 
  1314.        rTime);
  1315. }
  1316. }
  1317. } else {
  1318. /*
  1319.  * If the medium is NOT IDLE, then we start
  1320.  * the backoff timer.
  1321.  */
  1322. mhBackoff_.start(cw_, is_idle());
  1323. priority_queue[head] = 4;
  1324. }
  1325. } else {
  1326. priority_queue[end()] = 4;
  1327. }
  1328. }
  1329. /* ======================================================================
  1330.    Incoming Packet Routines
  1331.    ====================================================================== */
  1332. void
  1333. Mac802_11::send(Packet *p, Handler *h)
  1334. {
  1335. double rTime;
  1336. struct hdr_mac802_11* dh = HDR_MAC802_11(p);
  1337. EnergyModel *em = netif_->node()->energy_model();
  1338. if (em && em->sleep()) {
  1339. em->set_node_sleep(0);
  1340. em->set_node_state(EnergyModel::INROUTE);
  1341. }
  1342. callback_ = h;
  1343. sendDATA(p);
  1344. sendRTS(ETHER_ADDR(dh->dh_ra));
  1345. /*
  1346.  * Assign the data packet a sequence number.
  1347.  */
  1348. dh->dh_scontrol = sta_seqno_++;
  1349. /*
  1350.  *  If the medium is IDLE, we must wait for a DIFS
  1351.  *  Space before transmitting.
  1352.  */
  1353.        
  1354. if(mhBackoff_.busy() == 0) {
  1355. if(is_idle()) {
  1356. if (mhDefer_.busy() == 0) {
  1357. /*
  1358.  * If we are already deferring, there is no
  1359.  * need to reset the Defer timer.
  1360.  */
  1361. if (bugFix_timer_) {
  1362.  mhBackoff_.start(cw_, is_idle(), 
  1363.   phymib_.getDIFS());
  1364. }
  1365. else {
  1366. rTime = (Random::random() % cw_)
  1367. * (phymib_.getSlotTime());
  1368. mhDefer_.start(phymib_.getDIFS() + 
  1369.        rTime);
  1370. }
  1371. } else {
  1372. /*
  1373.  * If the medium is NOT IDLE, then we start
  1374.  * the backoff timer.
  1375.  */
  1376. mhBackoff_.start(cw_, is_idle());
  1377. }
  1378. }
  1379. }
  1380. void
  1381. Mac802_11::recv(Packet *p, Handler *h)
  1382. {
  1383. struct hdr_cmn *hdr = HDR_CMN(p);
  1384. /*
  1385.  * Sanity Check
  1386.  */
  1387. assert(initialized());
  1388. /*
  1389.  *  Handle outgoing packets.
  1390.  */
  1391. if(hdr->direction() == hdr_cmn::DOWN) {
  1392.                 send(p, h);
  1393.                 return;
  1394.         }
  1395. /*
  1396.  *  Handle incoming packets.
  1397.  *
  1398.  *  We just received the 1st bit of a packet on the network
  1399.  *  interface.
  1400.  *
  1401.  */
  1402. /*
  1403.  *  If the interface is currently in transmit mode, then
  1404.  *  it probably won't even see this packet.  However, the
  1405.  *  "air" around me is BUSY so I need to let the packet
  1406.  *  proceed.  Just set the error flag in the common header
  1407.  *  to that the packet gets thrown away.
  1408.  */
  1409. if(tx_active_ && hdr->error() == 0) {
  1410. hdr->error() = 1;
  1411. }
  1412. if(rx_state_ == MAC_IDLE) {
  1413. setRxState(MAC_RECV);
  1414. pktRx_ = p;
  1415. /*
  1416.  * Schedule the reception of this packet, in
  1417.  * txtime seconds.
  1418.  */
  1419. if (mhProbe_.busy() && OnMinChannelTime) {
  1420. Recv_Busy_ = 1;  // Receiver busy indication for Probe Timer 
  1421. }
  1422. mhRecv_.start(txtime(p));
  1423. } else {
  1424. /*
  1425.  *  If the power of the incoming packet is smaller than the
  1426.  *  power of the packet currently being received by at least
  1427.                  *  the capture threshold, then we ignore the new packet.
  1428.  */
  1429. if(pktRx_->txinfo_.RxPr / p->txinfo_.RxPr >= p->txinfo_.CPThresh) {
  1430. capture(p);
  1431. } else {
  1432. collision(p);
  1433. }
  1434. }
  1435. }
  1436. void
  1437. Mac802_11::recv_timer()
  1438. {
  1439. u_int32_t src; 
  1440. hdr_cmn *ch = HDR_CMN(pktRx_);
  1441. hdr_mac802_11 *mh = HDR_MAC802_11(pktRx_);
  1442. u_int32_t dst = ETHER_ADDR(mh->dh_ra);
  1443. u_int32_t ap_dst = ETHER_ADDR(mh->dh_3a);
  1444. u_int8_t  type = mh->dh_fc.fc_type;
  1445. u_int8_t  subtype = mh->dh_fc.fc_subtype;
  1446. assert(pktRx_);
  1447. assert(rx_state_ == MAC_RECV || rx_state_ == MAC_COLL);
  1448.         /*
  1449.          *  If the interface is in TRANSMIT mode when this packet
  1450.          *  "arrives", then I would never have seen it and should
  1451.          *  do a silent discard without adjusting the NAV.
  1452.          */
  1453.         if(tx_active_) {
  1454.                 Packet::free(pktRx_);
  1455.                 goto done;
  1456.         }
  1457. /*
  1458.  * Handle collisions.
  1459.  */
  1460. if(rx_state_ == MAC_COLL) {
  1461. discard(pktRx_, DROP_MAC_COLLISION);
  1462. set_nav(usec(phymib_.getEIFS()));
  1463. goto done;
  1464. }
  1465. /*
  1466.  * Check to see if this packet was received with enough
  1467.  * bit errors that the current level of FEC still could not
  1468.  * fix all of the problems - ie; after FEC, the checksum still
  1469.  * failed.
  1470.  */
  1471. if( ch->error() ) {
  1472. Packet::free(pktRx_);
  1473. set_nav(usec(phymib_.getEIFS()));
  1474. goto done;
  1475. }
  1476. /*
  1477.  * IEEE 802.11 specs, section 9.2.5.6
  1478.  * - update the NAV (Network Allocation Vector)
  1479.  */
  1480. if(dst != (u_int32_t)index_) {
  1481. set_nav(mh->dh_duration);
  1482. }
  1483.         /* tap out - */
  1484.         if (tap_ && type == MAC_Type_Data &&
  1485.             MAC_Subtype_Data == subtype ) 
  1486. tap_->tap(pktRx_);
  1487. /*
  1488.  * Adaptive Fidelity Algorithm Support - neighborhood infomation 
  1489.  * collection
  1490.  *
  1491.  * Hacking: Before filter the packet, log the neighbor node
  1492.  * I can hear the packet, the src is my neighbor
  1493.  */
  1494. if (netif_->node()->energy_model() && 
  1495.     netif_->node()->energy_model()->adaptivefidelity()) {
  1496. src = ETHER_ADDR(mh->dh_ta);
  1497. netif_->node()->energy_model()->add_neighbor(src);
  1498. }
  1499. /*
  1500.  * Address Filtering
  1501.  */
  1502. if(dst != (u_int32_t)index_ && dst != MAC_BROADCAST) {
  1503. /*
  1504.  *  We don't want to log this event, so we just free
  1505.  *  the packet instead of calling the drop routine.
  1506.  */
  1507. discard(pktRx_, "---");
  1508. goto done;
  1509. }
  1510. if ( dst == MAC_BROADCAST && mh->dh_fc.fc_to_ds == 1 && mh->dh_fc.fc_from_ds == 1) {
  1511. if (addr() != bss_id_) {
  1512.   discard(pktRx_, "---");
  1513.   goto done;
  1514. }
  1515. if (addr() == bss_id_ && find_client(ap_dst) == 0) {
  1516. discard(pktRx_, "---");
  1517.   goto done;
  1518. }
  1519.   }
  1520. if ( addr() != bss_id_ && subtype == MAC_Subtype_ProbeReq) {
  1521. discard(pktRx_, "---");
  1522.   goto done;
  1523. }
  1524. switch(type) {
  1525. case MAC_Type_Management:
  1526. //discard(pktRx_, DROP_MAC_PACKET_ERROR);
  1527. switch(subtype) {
  1528. case MAC_Subtype_80211_Beacon:
  1529. recvBEACON(pktRx_);
  1530. break;
  1531. case MAC_Subtype_Auth:
  1532. recvAUTHENTICATE(pktRx_);
  1533. break;
  1534. case MAC_Subtype_AssocReq:
  1535. recvASSOCREQ(pktRx_);
  1536. break;
  1537. case MAC_Subtype_AssocRep:
  1538. recvASSOCREP(pktRx_);
  1539. break;
  1540. case MAC_Subtype_ProbeReq:
  1541. recvPROBEREQ(pktRx_);
  1542. break;
  1543. case MAC_Subtype_ProbeRep:
  1544. recvPROBEREP(pktRx_);
  1545. break;
  1546. default:
  1547. fprintf(stderr,"recvTimer1:Invalid MAC Management Subtype %xn",
  1548. subtype);
  1549. exit(1);
  1550. }
  1551. break;
  1552. case MAC_Type_Control:
  1553. switch(subtype) {
  1554. case MAC_Subtype_RTS:
  1555. recvRTS(pktRx_);
  1556. break;
  1557. case MAC_Subtype_CTS:
  1558. recvCTS(pktRx_);
  1559. break;
  1560. case MAC_Subtype_ACK:
  1561. recvACK(pktRx_);
  1562. break;
  1563. default:
  1564. fprintf(stderr,"recvTimer2:Invalid MAC Control Subtype %xn",
  1565. subtype);
  1566. exit(1);
  1567. }
  1568. break;
  1569. case MAC_Type_Data:
  1570. switch(subtype) {
  1571. case MAC_Subtype_Data:
  1572. recvDATA(pktRx_);
  1573. break;
  1574. default:
  1575. fprintf(stderr, "recv_timer3:Invalid MAC Data Subtype %xn",
  1576. subtype);
  1577. exit(1);
  1578. }
  1579. break;
  1580. default:
  1581. fprintf(stderr, "recv_timer4:Invalid MAC Type %xn", subtype);
  1582. exit(1);
  1583. }
  1584.  done:
  1585. pktRx_ = 0;
  1586. rx_resume();
  1587. }
  1588. void
  1589. Mac802_11::recvRTS(Packet *p)
  1590. {
  1591. struct rts_frame *rf = (struct rts_frame*)p->access(hdr_mac::offset_);
  1592. if(tx_state_ != MAC_IDLE) {
  1593. discard(p, DROP_MAC_BUSY);
  1594. return;
  1595. }
  1596. /*
  1597.  *  If I'm responding to someone else, discard this RTS.
  1598.  */
  1599. if(pktCTRL_) {
  1600. discard(p, DROP_MAC_BUSY);
  1601. return;
  1602. }
  1603. sendCTS(ETHER_ADDR(rf->rf_ta), rf->rf_duration);
  1604. /*
  1605.  *  Stop deferring - will be reset in tx_resume().
  1606.  */
  1607. if(mhDefer_.busy()) mhDefer_.stop();
  1608. tx_resume();
  1609. mac_log(p);
  1610. }
  1611. /*
  1612.  * txtime() - pluck the precomputed tx time from the packet header
  1613.  */
  1614. double
  1615. Mac802_11::txtime(Packet *p)
  1616. {
  1617. struct hdr_cmn *ch = HDR_CMN(p);
  1618. double t = ch->txtime();
  1619. if (t < 0.0) {
  1620. drop(p, "XXX");
  1621.   exit(1);
  1622. }
  1623. return t;
  1624. }
  1625.  
  1626. /*
  1627.  * txtime() - calculate tx time for packet of size "psz" bytes 
  1628.  *   at rate "drt" bps
  1629.  */
  1630. double
  1631. Mac802_11::txtime(double psz, double drt)
  1632. {
  1633. double dsz = psz - phymib_.getPLCPhdrLen();
  1634.         int plcp_hdr = phymib_.getPLCPhdrLen() << 3;
  1635. int datalen = (int)dsz << 3;
  1636. double t = (((double)plcp_hdr)/phymib_.getPLCPDataRate())
  1637.                                        + (((double)datalen)/drt);
  1638. return(t);
  1639. }
  1640. void
  1641. Mac802_11::recvCTS(Packet *p)
  1642. {
  1643. if(tx_state_ != MAC_RTS) {
  1644. discard(p, DROP_MAC_INVALID_STATE);
  1645. return;
  1646. }
  1647. assert(pktRTS_);
  1648. Packet::free(pktRTS_); pktRTS_ = 0;
  1649. assert(pktTx_);
  1650. mhSend_.stop();
  1651. /*
  1652.  * The successful reception of this CTS packet implies
  1653.  * that our RTS was successful. 
  1654.  * According to the IEEE spec 9.2.5.3, you must 
  1655.  * reset the ssrc_, but not the congestion window.
  1656.  */
  1657. ssrc_ = 0;
  1658. tx_resume();
  1659. mac_log(p);
  1660. }
  1661. void
  1662. Mac802_11::recvDATA(Packet *p)
  1663. {
  1664. struct hdr_mac802_11 *dh = HDR_MAC802_11(p);
  1665. u_int32_t dst, src, size;
  1666. struct hdr_cmn *ch = HDR_CMN(p);
  1667. dst = ETHER_ADDR(dh->dh_ra);
  1668. src = ETHER_ADDR(dh->dh_ta);
  1669. size = ch->size();
  1670. /*
  1671.  * Adjust the MAC packet size - ie; strip
  1672.  * off the mac header
  1673.  */
  1674. ch->size() -= phymib_.getHdrLen11();
  1675. ch->num_forwards() += 1;
  1676. /*
  1677.  *  If we sent a CTS, clean up...
  1678.  */
  1679. if(dst != MAC_BROADCAST) {
  1680. if(size >= macmib_.getRTSThreshold()) {
  1681. if (tx_state_ == MAC_CTS) {
  1682. assert(pktCTRL_);
  1683. Packet::free(pktCTRL_); pktCTRL_ = 0;
  1684. mhSend_.stop();
  1685. /*
  1686.  * Our CTS got through.
  1687.  */
  1688. } else {
  1689. discard(p, DROP_MAC_BUSY);
  1690. return;
  1691. }
  1692. sendACK(src);
  1693. tx_resume();
  1694. } else {
  1695. /*
  1696.  *  We did not send a CTS and there's no
  1697.  *  room to buffer an ACK.
  1698.  */
  1699. if(pktCTRL_) {
  1700. discard(p, DROP_MAC_BUSY);
  1701. return;
  1702. }
  1703. sendACK(src);
  1704. if(mhSend_.busy() == 0)
  1705. tx_resume();
  1706. }
  1707. }
  1708. /* ============================================================
  1709.    Make/update an entry in our sequence number cache.
  1710.    ============================================================ */
  1711. /* Changed by Debojyoti Dutta. This upper loop of if{}else was 
  1712.    suggested by Joerg Diederich <dieder@ibr.cs.tu-bs.de>. 
  1713.    Changed on 19th Oct'2000 */
  1714.         if(dst != MAC_BROADCAST) {
  1715.                 if (src < (u_int32_t) cache_node_count_) {
  1716.                         Host *h = &cache_[src];
  1717.                         if(h->seqno && h->seqno == dh->dh_scontrol) {
  1718.                                 discard(p, DROP_MAC_DUPLICATE);
  1719.                                 return;
  1720.                         }
  1721.                         h->seqno = dh->dh_scontrol;
  1722.                 } else {
  1723. static int count = 0;
  1724. if (++count <= 10) {
  1725. printf ("MAC_802_11: accessing MAC cache_ array out of range (src %u, dst %u, size %d)!n", src, dst, cache_node_count_);
  1726. if (count == 10)
  1727. printf ("[suppressing additional MAC cache_ warnings]n");
  1728. };
  1729. };
  1730. }
  1731. /*
  1732.  *  Pass the packet up to the link-layer.
  1733.  *  XXX - we could schedule an event to account
  1734.  *  for this processing delay.
  1735.  */
  1736. /* in BSS mode, if a station receives a packet via
  1737.  * the AP, and higher layers are interested in looking
  1738.  * at the src address, we might need to put it at
  1739.  * the right place - lest the higher layers end up
  1740.  * believing the AP address to be the src addr! a quick
  1741.  * grep didn't turn up any higher layers interested in
  1742.  * the src addr though!
  1743.  * anyway, here if I'm the AP and the destination
  1744.  * address (in dh_3a) isn't me, then we have to fwd
  1745.  * the packet; we pick the real destination and set
  1746.  * set it up for the LL; we save the real src into
  1747.  * the dh_3a field for the 'interested in the info'
  1748.  * receiver; we finally push the packet towards the
  1749.  * LL to be added back to my queue - accomplish this
  1750.  * by reversing the direction!*/
  1751. if ((bss_id() == addr()) && ((u_int32_t)ETHER_ADDR(dh->dh_ra)!= MAC_BROADCAST) && ((u_int32_t)ETHER_ADDR(dh->dh_3a) != ((u_int32_t)addr())) && dh->dh_fc.fc_from_ds == 0) {
  1752. struct hdr_cmn *ch = HDR_CMN(p);
  1753. u_int32_t dst = ETHER_ADDR(dh->dh_3a);
  1754. u_int32_t src = ETHER_ADDR(dh->dh_ta);
  1755. /* if it is a broadcast pkt then send a copy up
  1756.  * my stack also
  1757.  */
  1758. if (dst == MAC_BROADCAST) {
  1759. uptarget_->recv(p->copy(), (Handler*) 0);
  1760. }
  1761. ch->next_hop() = dst;
  1762. if (find_client(dst) == 1 || dst == MAC_BROADCAST) {
  1763. STORE4BYTE(&src, (dh->dh_3a));
  1764. } else {
  1765. STORE4BYTE(&src, (dh->dh_4a));
  1766. }
  1767. ch->addr_type() = NS_AF_ILINK;
  1768. ch->direction() = hdr_cmn::DOWN;
  1769. }
  1770.   if ((bss_id() == addr()) && dh->dh_fc.fc_to_ds == 1 && dh->dh_fc.fc_from_ds == 1) {
  1771.   u_int32_t dst = ETHER_ADDR(dh->dh_3a);
  1772.   u_int32_t src = ETHER_ADDR(dh->dh_4a);
  1773. if (find_client(src)) { 
  1774. update_client_table(src,0,0);   // If the source is from another BSS and it is found in this AP's table, delete the node from the table
  1775. }
  1776.   ch->next_hop() = dst;
  1777.   STORE4BYTE(&src, (dh->dh_3a));
  1778.   ch->addr_type() = NS_AF_ILINK;
  1779.   ch->direction() = hdr_cmn::DOWN;
  1780.   }
  1781. uptarget_->recv(p, (Handler*) 0);
  1782. }
  1783. void
  1784. Mac802_11::recvACK(Packet *p)
  1785. {
  1786. if (tx_state_ == MAC_MGMT) {
  1787. mhSend_.stop();
  1788. if (addr() == bss_id_) {
  1789. if (pktASSOCREP_ && priority_queue[head] == 4) {
  1790. Packet::free(pktASSOCREP_);
  1791. pktASSOCREP_ = 0;
  1792. update_client_table(associating_node_,1,1);
  1793. if (pktPROBEREP_ && priority_queue[head] == 1) {
  1794. Packet::free(pktPROBEREP_);
  1795. pktPROBEREP_ = 0;
  1796. }
  1797. if (pktAUTHENTICATE_ && priority_queue[head] == 3) {
  1798. Packet::free(pktAUTHENTICATE_);
  1799. pktAUTHENTICATE_ = 0;
  1800. update_client_table(authenticating_node_,1,0);
  1801. }
  1802. }
  1803. goto done;
  1804. }
  1805. if(tx_state_ != MAC_SEND) {
  1806. discard(p, DROP_MAC_INVALID_STATE);
  1807. return;
  1808. }
  1809. assert(pktTx_);
  1810. mhSend_.stop();
  1811. /*
  1812.  * The successful reception of this ACK packet implies
  1813.  * that our DATA transmission was successful.  Hence,
  1814.  * we can reset the Short/Long Retry Count and the CW.
  1815.  *
  1816.  * need to check the size of the packet we sent that's being
  1817.  * ACK'd, not the size of the ACK packet.
  1818.  */
  1819. if((u_int32_t) HDR_CMN(pktTx_)->size() <= macmib_.getRTSThreshold())
  1820. ssrc_ = 0;
  1821. else
  1822. slrc_ = 0;
  1823. rst_cw();
  1824. Packet::free(pktTx_); 
  1825. pktTx_ = 0;
  1826. /*
  1827.  * Backoff before sending again.
  1828. //   */
  1829. assert(mhBackoff_.busy() == 0);
  1830. mhBackoff_.start(cw_, is_idle());
  1831. done:
  1832. if (addr() == bss_id_) {
  1833. if (end() > (head + 1)) {
  1834. shift_priority_queue();
  1835. assert(mhBackoff_.busy() == 0);
  1836. mhBackoff_.start(cw_, is_idle());
  1837. } else 
  1838. priority_queue[head] = 0;
  1839. }
  1840. tx_resume();
  1841. mac_log(p);
  1842. }
  1843. /* AP's association table funtions
  1844. */
  1845. void Mac802_11::update_client_table(int num, int auth_status, int assoc_status) {
  1846. if (client_list == NULL) {
  1847. client_list = (struct client_table*)malloc(sizeof(struct client_table));
  1848. client_list->client_id=num;
  1849. client_list->auth_status=auth_status;
  1850. client_list->assoc_status=assoc_status;
  1851. client_list->next=NULL;
  1852. }
  1853. else {
  1854. push(num, auth_status, assoc_status);
  1855. }
  1856. //  printf("Client List for AP %dn",index_);
  1857. //  struct client_table *temp;
  1858. //  temp = client_list;
  1859. // 
  1860. //  while (temp != NULL) {
  1861. //  printf("Client %d: Authenticated = %d Associated = %dn", temp->client_id,temp->auth_status,temp->assoc_status,NOW);
  1862. //  temp=temp->next;
  1863. //  }
  1864. //  printf("n");
  1865. }
  1866. void Mac802_11::push(int num, int auth_status, int assoc_status) {
  1867. struct client_table *temp;
  1868. temp = client_list;
  1869. while (temp != NULL) {
  1870. if (temp->client_id == num) {
  1871. temp->auth_status=auth_status;
  1872. temp->assoc_status=assoc_status;
  1873. return;
  1874. }
  1875. temp=temp->next;
  1876. if (temp == NULL) {
  1877. break; 
  1878. }
  1879. }
  1880. temp = client_list;
  1881. while (temp->next != NULL) {
  1882. temp=temp->next;
  1883. }
  1884. temp->next = (struct client_table*)malloc(sizeof(struct client_table));
  1885. temp->next->client_id = num;
  1886. temp->next->auth_status=auth_status;
  1887. temp->next->assoc_status=assoc_status;
  1888. temp->next->next = NULL; 
  1889. }
  1890. int Mac802_11::find_client(int num) {
  1891. struct client_table *temp;
  1892. temp = client_list;
  1893. while (temp != NULL) {
  1894. if (temp->client_id == num && temp->auth_status == 1 && temp->assoc_status == 1) {
  1895. return 1;
  1896. break;
  1897. }
  1898. temp=temp->next;
  1899. if (temp == NULL) {
  1900. return 0; 
  1901. }
  1902. }
  1903. return 0;
  1904. }
  1905. /* Beacon send and Receive functions
  1906. */
  1907. void
  1908. Mac802_11::sendBEACON(int src)
  1909. {
  1910. Packet *p = Packet::alloc();
  1911. hdr_cmn* ch = HDR_CMN(p);
  1912. struct beacon_frame *bf = (struct beacon_frame*)p->access(hdr_mac::offset_);
  1913. double rTime;
  1914. pktBEACON_ = 0;
  1915. ch->uid() = 0;
  1916. ch->ptype() = PT_MAC;
  1917. ch->size() = phymib_.getBEACONlen();
  1918. ch->iface() = -2;
  1919. ch->error() = 0;
  1920. bzero(bf, MAC_HDR_LEN);
  1921. /* Note: I had to give a different name for MAC_Subtype_80211_Beacon as MAC_Subtype_80211_Beacon as MAC_Subtype_80211_Beacon is already defined for 802.15.4 with a different value!! (See cmu-trace.cc).
  1922. */
  1923. bf->bf_fc.fc_protocol_version = MAC_ProtocolVersion;
  1924.   bf->bf_fc.fc_type = MAC_Type_Management;
  1925.   bf->bf_fc.fc_subtype = MAC_Subtype_80211_Beacon;
  1926.   bf->bf_fc.fc_to_ds = 0;
  1927.   bf->bf_fc.fc_from_ds = 0;
  1928.   bf->bf_fc.fc_more_frag = 0;
  1929.   bf->bf_fc.fc_retry = 0;
  1930.   bf->bf_fc.fc_pwr_mgt = 0;
  1931.   bf->bf_fc.fc_more_data = 0;
  1932.   bf->bf_fc.fc_wep = 0;
  1933.   bf->bf_fc.fc_order = 0;
  1934. int  dst = MAC_BROADCAST;
  1935. STORE4BYTE(&dst, (bf->bf_ra));
  1936. STORE4BYTE(&src, (bf->bf_ta));
  1937. STORE4BYTE(&ap_addr, (bf->bf_3a));
  1938. bf->bf_timestamp = Scheduler::instance().clock();
  1939. bf->bf_bcninterval = phymib_.getBeaconInterval();
  1940. /* store beacon tx time */
  1941.   ch->txtime() = txtime(ch->size(), basicRate_);
  1942. /* calculate beacon duration??? */
  1943.   bf->bf_duration = 0;
  1944. pktBEACON_ = p;
  1945. BeaconTxtime_ = txtime(phymib_.getBEACONlen(), basicRate_);
  1946. if(mhBackoff_.busy() == 0) {
  1947. if(is_idle()) {
  1948. if (mhDefer_.busy() == 0) {
  1949. if (bugFix_timer_) {
  1950.   mhBackoff_.start(cw_, is_idle(), 
  1951.   phymib_.getDIFS());
  1952. priority_queue[head] = 2;
  1953. }
  1954. else {
  1955. rTime = (Random::random() % cw_)
  1956. * (phymib_.getSlotTime());
  1957. mhDefer_.start(phymib_.getDIFS() + 
  1958.        rTime);
  1959. }
  1960. }
  1961. } else {
  1962. /*
  1963.  * If the medium is NOT IDLE, then we start
  1964.  * the backoff timer.
  1965.  */
  1966. mhBackoff_.start(cw_, is_idle());
  1967. priority_queue[head] = 2;
  1968. }
  1969. } else {
  1970. priority_queue[end()] = 2;
  1971. }
  1972. }
  1973. int
  1974. Mac802_11::check_pktBEACON()
  1975. {
  1976. struct beacon_frame *bf = (struct beacon_frame*)pktBEACON_->access(hdr_mac::offset_);
  1977. bf->bf_timestamp = Scheduler::instance().clock();
  1978. struct hdr_mac802_11 *mh;
  1979. double timeout;
  1980. assert(mhBackoff_.busy() == 0);
  1981. if(pktBEACON_ == 0)
  1982.   return -1;
  1983. mh = HDR_MAC802_11(pktBEACON_);
  1984.   switch(mh->dh_fc.fc_subtype) {
  1985. case MAC_Subtype_80211_Beacon:
  1986. if(! is_idle()) {
  1987. inc_cw();
  1988. mhBackoff_.start(cw_, is_idle());
  1989. return 0;
  1990. }
  1991. setTxState(MAC_BCN);
  1992. timeout = txtime(phymib_.getBEACONlen(), basicRate_);
  1993. break;
  1994. default:
  1995. fprintf(stderr, "check_pktBEACON:Invalid MAC Control subtypen");
  1996. exit(1);
  1997. }
  1998. transmit(pktBEACON_, timeout);
  1999. return 0;
  2000. }
  2001.  
  2002. void
  2003. Mac802_11::recvBEACON(Packet *p)
  2004. {
  2005. struct beacon_frame *bf = (struct beacon_frame*)p->access(hdr_mac::offset_);
  2006. if(tx_state_ != MAC_IDLE) {
  2007. discard(p, DROP_MAC_BUSY);
  2008. return;
  2009. }
  2010. u_int32_t bss_id, src;
  2011. //double timestamp, beaconint;
  2012. bss_id = ETHER_ADDR(bf->bf_3a);
  2013.   src = ETHER_ADDR(bf->bf_ta);
  2014. infra_mode_ = 1;
  2015. //timestamp = bf->bf_timestamp;
  2016. Pr = p->txinfo_.RxPr;
  2017. if ( addr() != ap_addr && ScanType_ == PASSIVE) {
  2018. if (authenticated == 0 && associated == 0) {
  2019. if (find_ap(src,Pr) != 1) {
  2020. update_ap_table(src,Pr);
  2021. }
  2022.   }
  2023. }
  2024. mac_log(p);
  2025. }
  2026. void
  2027. Mac802_11::passive_scan()
  2028. {
  2029. if ( addr() != ap_addr && ScanType_ == PASSIVE) {
  2030. if (authenticated == 0 && associated == 0) {
  2031. ap_temp = strongest_ap();
  2032. if (!pktAUTHENTICATE_)
  2033. sendAUTHENTICATE(ap_temp);
  2034. }
  2035. }
  2036. }
  2037. void
  2038. Mac802_11::active_scan()
  2039. {
  2040. if ( addr() != ap_addr && ScanType_ == ACTIVE) {
  2041. if (authenticated == 0 && associated == 0) {
  2042. ap_temp = strongest_ap();
  2043. sendAUTHENTICATE(ap_temp);
  2044. }
  2045. }
  2046. }
  2047. // Association functions
  2048. void
  2049. Mac802_11::sendASSOCREQ(int dst)
  2050. {
  2051. Packet *p = Packet::alloc();
  2052. hdr_cmn* ch = HDR_CMN(p);
  2053. struct assocreq_frame *acrqf =(struct assocreq_frame*)p->access(hdr_mac::offset_);
  2054. double rTime;
  2055. pktASSOCREQ_ = 0;
  2056. ch->uid() = 0;
  2057. ch->ptype() = PT_MAC;
  2058. ch->size() = phymib_.getASSOCREQlen();
  2059. ch->iface() = -2;
  2060. ch->error() = 0;
  2061. bzero(acrqf, MAC_HDR_LEN);
  2062. acrqf->acrqf_fc.fc_protocol_version = MAC_ProtocolVersion;
  2063.   acrqf->acrqf_fc.fc_type = MAC_Type_Management;
  2064.   acrqf->acrqf_fc.fc_subtype = MAC_Subtype_AssocReq;
  2065.   acrqf->acrqf_fc.fc_to_ds = 0;
  2066.   acrqf->acrqf_fc.fc_from_ds = 0;
  2067.   acrqf->acrqf_fc.fc_more_frag= 0;
  2068.   acrqf->acrqf_fc.fc_retry = 0;
  2069.   acrqf->acrqf_fc.fc_pwr_mgt = 0;
  2070.   acrqf->acrqf_fc.fc_more_data= 0;
  2071.   acrqf->acrqf_fc.fc_wep = 0;
  2072.   acrqf->acrqf_fc.fc_order = 0;
  2073. STORE4BYTE(&dst, (acrqf->acrqf_ra));
  2074. STORE4BYTE(&index_, (acrqf->acrqf_ta));
  2075. STORE4BYTE(&dst, (acrqf->acrqf_3a));
  2076. ch->txtime() = txtime(ch->size(), basicRate_);
  2077.   acrqf->acrqf_duration = 0;
  2078. pktASSOCREQ_ = p;
  2079. if(mhBackoff_.busy() == 0) {
  2080. if(is_idle()) {
  2081. if (mhDefer_.busy() == 0) {
  2082. /*
  2083.  * If we are already deferring, there is no
  2084.  * need to reset the Defer timer.
  2085.  */
  2086. if (bugFix_timer_) {
  2087.   mhBackoff_.start(cw_, is_idle(), 
  2088.   phymib_.getDIFS());
  2089. }
  2090. else {
  2091. rTime = (Random::random() % cw_)
  2092. * (phymib_.getSlotTime());
  2093. mhDefer_.start(phymib_.getDIFS() + 
  2094.        rTime);
  2095. }
  2096. }
  2097. } else {
  2098. /*
  2099.  * If the medium is NOT IDLE, then we start
  2100.  * the backoff timer.
  2101.  */
  2102. mhBackoff_.start(cw_, is_idle());
  2103. }
  2104. }
  2105. }
  2106. int
  2107. Mac802_11::check_pktASSOCREQ()
  2108. {
  2109. struct hdr_mac802_11 *mh;
  2110. double timeout;
  2111. assert(mhBackoff_.busy() == 0);
  2112. if(pktASSOCREQ_ == 0)
  2113.   return -1;
  2114. mh = HDR_MAC802_11(pktASSOCREQ_);
  2115.   switch(mh->dh_fc.fc_subtype) {
  2116. case MAC_Subtype_AssocReq:
  2117. if(! is_idle()) {
  2118. inc_cw();
  2119. mhBackoff_.start(cw_, is_idle());
  2120. return 0;
  2121. }
  2122. setTxState(MAC_MGMT);
  2123. timeout = txtime(phymib_.getASSOCREQlen(), basicRate_)
  2124. + DSSS_MaxPropagationDelay
  2125. + macmib_.getMaxChannelTime()
  2126. + txtime(phymib_.getASSOCREPlen(), basicRate_)
  2127. + DSSS_MaxPropagationDelay;
  2128. break;
  2129. default:
  2130. fprintf(stderr, "check_pktASSOCREQ:Invalid MAC Control subtypen");
  2131. exit(1);
  2132. }
  2133. transmit(pktASSOCREQ_, timeout);
  2134.   
  2135. return 0;
  2136. }
  2137. void
  2138. Mac802_11::sendASSOCREP(int dst)
  2139. {
  2140. Packet *p = Packet::alloc();
  2141. hdr_cmn* ch = HDR_CMN(p);
  2142. struct assocrep_frame *acrpf =(struct assocrep_frame*)p->access(hdr_mac::offset_);
  2143. double rTime;
  2144. pktASSOCREP_ = 0;
  2145. ch->uid() = 0;
  2146. ch->ptype() = PT_MAC;
  2147. ch->size() = phymib_.getASSOCREPlen();
  2148. ch->iface() = -2;
  2149. ch->error() = 0;
  2150. bzero(acrpf, MAC_HDR_LEN);
  2151. acrpf->acrpf_fc.fc_protocol_version = MAC_ProtocolVersion;
  2152.   acrpf->acrpf_fc.fc_type = MAC_Type_Management;
  2153.   acrpf->acrpf_fc.fc_subtype = MAC_Subtype_AssocRep;
  2154.   acrpf->acrpf_fc.fc_to_ds = 0;
  2155.   acrpf->acrpf_fc.fc_from_ds = 0;
  2156.   acrpf->acrpf_fc.fc_more_frag= 0;
  2157.   acrpf->acrpf_fc.fc_retry = 0;
  2158.   acrpf->acrpf_fc.fc_pwr_mgt = 0;
  2159.   acrpf->acrpf_fc.fc_more_data= 0;
  2160.   acrpf->acrpf_fc.fc_wep = 0;
  2161.   acrpf->acrpf_fc.fc_order = 0;
  2162. STORE4BYTE(&dst, (acrpf->acrpf_ra));
  2163. STORE4BYTE(&index_, (acrpf->acrpf_ta));
  2164. STORE4BYTE(&index_, (acrpf->acrpf_3a));
  2165. acrpf->acrpf_statuscode = 0;
  2166. ch->txtime() = txtime(ch->size(), basicRate_);
  2167.   acrpf->acrpf_duration = 0;
  2168.   pktASSOCREP_ = p;
  2169. if(mhBackoff_.busy() == 0) {
  2170. if(is_idle()) {
  2171. if (mhDefer_.busy() == 0) {
  2172. /*
  2173.  * If we are already deferring, there is no
  2174.  * need to reset the Defer timer.
  2175.  */
  2176. if (bugFix_timer_) {
  2177.   mhBackoff_.start(cw_, is_idle(), 
  2178.   phymib_.getDIFS());
  2179. priority_queue[head] = 4;
  2180. }
  2181. else {
  2182. rTime = (Random::random() % cw_)
  2183. * (phymib_.getSlotTime());
  2184. mhDefer_.start(phymib_.getDIFS() + 
  2185.        rTime);
  2186. }
  2187. }
  2188. } else {
  2189. /*
  2190.  * If the medium is NOT IDLE, then we start
  2191.  * the backoff timer.
  2192.  */
  2193. mhBackoff_.start(cw_, is_idle());
  2194. priority_queue[head] = 4;
  2195. } else {
  2196. priority_queue[end()] = 4;
  2197. }
  2198. }
  2199. int
  2200. Mac802_11::check_pktASSOCREP()
  2201. {
  2202.   struct hdr_mac802_11 *mh;
  2203.   double timeout;
  2204.   assert(mhBackoff_.busy() == 0);
  2205.   if(pktASSOCREP_ == 0)
  2206.    return -1;
  2207. mh = HDR_MAC802_11(pktASSOCREP_);
  2208.    switch(mh->dh_fc.fc_subtype) {
  2209.   case MAC_Subtype_AssocRep:
  2210.   if(!is_idle()) {
  2211.   inc_cw();
  2212. mhBackoff_.start(cw_, is_idle());
  2213. return 0;
  2214.   }
  2215.   setTxState(MAC_MGMT);
  2216.   timeout = txtime(phymib_.getASSOCREPlen(), basicRate_)
  2217. + DSSS_MaxPropagationDelay
  2218. + phymib_.getSIFS()
  2219. + txtime(phymib_.getACKlen(), basicRate_)
  2220. + DSSS_MaxPropagationDelay;
  2221.   break;
  2222. default:
  2223.   fprintf(stderr, "check_pktASSOCREP:Invalid MAC Control subtypen");
  2224.   exit(1);
  2225. }
  2226.   transmit(pktASSOCREP_, timeout);
  2227.   return 0;
  2228. }
  2229. void
  2230. Mac802_11::recvASSOCREQ(Packet *p)
  2231. {
  2232. struct assocreq_frame *acrqf = (struct assocreq_frame*)p->access(hdr_mac::offset_);
  2233. if(tx_state_ != MAC_IDLE) {
  2234. discard(p, DROP_MAC_BUSY);
  2235. return;
  2236. }
  2237. u_int32_t bss_id, src;
  2238. bss_id = ETHER_ADDR(acrqf->acrqf_3a);
  2239.   src = ETHER_ADDR(acrqf->acrqf_ta);
  2240. if (!pktASSOCREP_) {
  2241. sendASSOCREP(src);
  2242. associating_node_ = src;
  2243. } else {
  2244. discard(p, DROP_MAC_BUSY);
  2245. return;
  2246. }
  2247. tx_resume();
  2248. mac_log(p);
  2249. }
  2250. void
  2251. Mac802_11::recvASSOCREP(Packet *p)
  2252. {
  2253. struct assocrep_frame *acrpf = (struct assocrep_frame*)p->access(hdr_mac::offset_);
  2254. assert(pktASSOCREQ_);
  2255. Packet::free(pktASSOCREQ_);
  2256. pktASSOCREQ_ = 0;
  2257. mhSend_.stop();
  2258. u_int32_t src;
  2259. u_int16_t statuscode;
  2260.   src = ETHER_ADDR(acrpf->acrpf_ta);
  2261. statuscode = acrpf->acrpf_statuscode;
  2262. if (statuscode == 0) {
  2263. associated = 1;
  2264. deletelist();
  2265. if (handoff) {
  2266. handoff = 0; //handoff completed
  2267. }
  2268. sendACK(src);
  2269. }
  2270. if(mhSend_.busy() == 0)
  2271. tx_resume();
  2272. mac_log(p);
  2273. }
  2274. //Authentication functions
  2275. void
  2276. Mac802_11::sendAUTHENTICATE(int dst)
  2277. {
  2278. Packet *p = Packet::alloc();
  2279. hdr_cmn* ch = HDR_CMN(p);
  2280. struct auth_frame *authf =(struct auth_frame*)p->access(hdr_mac::offset_);
  2281. double rTime;
  2282. pktAUTHENTICATE_ = 0;
  2283. ch->uid() = 0;
  2284. ch->ptype() = PT_MAC;
  2285. ch->size() = phymib_.getAUTHENTICATElen();
  2286. ch->iface() = -2;
  2287. ch->error() = 0;
  2288. bzero(authf, MAC_HDR_LEN);
  2289. authf->authf_fc.fc_protocol_version = MAC_ProtocolVersion;
  2290.   authf->authf_fc.fc_type = MAC_Type_Management;
  2291.   authf->authf_fc.fc_subtype = MAC_Subtype_Auth;
  2292.   authf->authf_fc.fc_to_ds = 0;
  2293.   authf->authf_fc.fc_from_ds = 0;
  2294.   authf->authf_fc.fc_more_frag= 0;
  2295.   authf->authf_fc.fc_retry = 0;
  2296.   authf->authf_fc.fc_pwr_mgt = 0;
  2297.   authf->authf_fc.fc_more_data= 0;
  2298.   authf->authf_fc.fc_wep = 0;
  2299.   authf->authf_fc.fc_order = 0;
  2300. STORE4BYTE(&dst, (authf->authf_ra));
  2301. STORE4BYTE(&index_, (authf->authf_ta));
  2302. if (addr() != bss_id_)
  2303. STORE4BYTE(&dst, (authf->authf_3a));
  2304. else 
  2305. STORE4BYTE(&index_, (authf->authf_3a));
  2306. authf->authf_algono = 0; //Open system authentication
  2307. if (addr() != bss_id_) {
  2308. authf->authf_seqno = 1;  // 
  2309. } else {
  2310. authf->authf_seqno = 2;  //
  2311. authf->authf_statuscode = 0;
  2312. }
  2313. ch->txtime() = txtime(ch->size(), basicRate_);
  2314. if (addr() == bss_id_) {
  2315.   authf->authf_duration = usec(txtime(phymib_.getACKlen(), basicRate_)
  2316.        + phymib_.getSIFS());
  2317. } else { 
  2318. authf->authf_duration = 0;
  2319. }
  2320. pktAUTHENTICATE_ = p;
  2321. if(mhBackoff_.busy() == 0) {
  2322. if(is_idle()) {
  2323. if (mhDefer_.busy() == 0) {
  2324. /*
  2325.  * If we are already deferring, there is no
  2326.  * need to reset the Defer timer.
  2327.  */
  2328. if (bugFix_timer_) {
  2329.   mhBackoff_.start(cw_, is_idle(), 
  2330.   phymib_.getDIFS());
  2331. priority_queue[head] = 3;
  2332. }
  2333. else {
  2334. rTime = (Random::random() % cw_)
  2335. * (phymib_.getSlotTime());
  2336. mhDefer_.start(phymib_.getDIFS() + 
  2337.        rTime);
  2338. }
  2339. }
  2340. } else {
  2341. /*
  2342.  * If the medium is NOT IDLE, then we start
  2343.  * the backoff timer.
  2344.  */
  2345. mhBackoff_.start(cw_, is_idle());
  2346. priority_queue[head] = 3;
  2347. }
  2348. } else {
  2349. priority_queue[end()] = 3;
  2350. }
  2351. }
  2352. int
  2353. Mac802_11::check_pktAUTHENTICATE()
  2354. {
  2355. struct hdr_mac802_11 *mh;
  2356. double timeout;
  2357. assert(mhBackoff_.busy() == 0);
  2358. if(pktAUTHENTICATE_ == 0)
  2359.   return -1;
  2360. mh = HDR_MAC802_11(pktAUTHENTICATE_);
  2361.   switch(mh->dh_fc.fc_subtype) {
  2362. case MAC_Subtype_Auth:
  2363. if(! is_idle()) {
  2364. inc_cw();
  2365. mhBackoff_.start(cw_, is_idle());
  2366. return 0;
  2367. }
  2368. setTxState(MAC_MGMT);
  2369. if (addr() != bss_id_) {
  2370. timeout = txtime(phymib_.getAUTHENTICATElen(), basicRate_)
  2371. + DSSS_MaxPropagationDelay                      // XXX
  2372. + macmib_.getMaxChannelTime()
  2373. + txtime(phymib_.getAUTHENTICATElen(), basicRate_)
  2374. + DSSS_MaxPropagationDelay;
  2375. } else {
  2376. timeout = txtime(phymib_.getAUTHENTICATElen(), basicRate_)
  2377. + DSSS_MaxPropagationDelay                      // XXX
  2378. + phymib_.getSIFS()
  2379. + txtime(phymib_.getACKlen(), basicRate_)
  2380. + DSSS_MaxPropagationDelay;
  2381. }
  2382. break;
  2383. default:
  2384. fprintf(stderr, "check_pktAUTHENTICATE:Invalid MAC Control subtypen");
  2385. exit(1);
  2386. }
  2387. transmit(pktAUTHENTICATE_, timeout);
  2388. return 0;
  2389. }
  2390. void
  2391. Mac802_11::recvAUTHENTICATE(Packet *p)
  2392. {
  2393. struct auth_frame *authf = (struct auth_frame*)p->access(hdr_mac::offset_);
  2394. if (addr() != bss_id_) {
  2395. assert(pktAUTHENTICATE_);
  2396. Packet::free(pktAUTHENTICATE_);
  2397. pktAUTHENTICATE_ = 0;
  2398. mhSend_.stop();
  2399. } else {
  2400. if ( tx_state_ != MAC_IDLE) {
  2401. discard(p, DROP_MAC_BUSY);
  2402. return;
  2403. }
  2404. }
  2405. u_int32_t src;
  2406.   src = ETHER_ADDR(authf->authf_ta);
  2407. if (addr() == ap_addr) {
  2408. if (authf->authf_seqno == 1) {
  2409. if (!pktAUTHENTICATE_) {// AP is not currently involved in Authentication with any other STA 
  2410. sendAUTHENTICATE(src);
  2411. authenticating_node_ = src;
  2412. } else {
  2413. discard(p, DROP_MAC_BUSY);
  2414. return;
  2415. }
  2416. } else 
  2417. printf("Out of sequencen");
  2418. } else if (authf->authf_seqno == 2 && authf->authf_statuscode == 0) {
  2419. authenticated = 1;
  2420. if (bss_id_ != ETHER_ADDR(authf->authf_3a) && handoff == 1) {
  2421. printf("Client %d: Handoff from AP %d to AP %dn",index_, bss_id_,ETHER_ADDR(authf->authf_3a));
  2422. }
  2423. bss_id_ = ETHER_ADDR(authf->authf_3a);
  2424. sendACK(src);
  2425. mhProbe_.start(phymib_.getSIFS() + txtime(phymib_.getACKlen(), basicRate_) + macmib_.getMaxChannelTime());
  2426. }
  2427. if(mhSend_.busy() == 0)
  2428. tx_resume();
  2429. mac_log(p);
  2430. }
  2431. // Active Scanning Functions 
  2432. void
  2433. Mac802_11::sendPROBEREQ(int dst)
  2434. {
  2435. Packet *p = Packet::alloc();
  2436. hdr_cmn* ch = HDR_CMN(p);
  2437. struct probereq_frame *prrqf =(struct probereq_frame*)p->access(hdr_mac::offset_);
  2438. double rTime;
  2439. pktPROBEREQ_ = 0;
  2440. ch->uid() = 0;
  2441. ch->ptype() = PT_MAC;
  2442. ch->size() = phymib_.getPROBEREQlen();
  2443. ch->iface() = -2;
  2444. ch->error() = 0;
  2445. bzero(prrqf, MAC_HDR_LEN);
  2446. prrqf->prrqf_fc.fc_protocol_version = MAC_ProtocolVersion;
  2447.   prrqf->prrqf_fc.fc_type = MAC_Type_Management;
  2448.   prrqf->prrqf_fc.fc_subtype = MAC_Subtype_ProbeReq;
  2449.   prrqf->prrqf_fc.fc_to_ds = 0;
  2450.   prrqf->prrqf_fc.fc_from_ds = 0;
  2451.   prrqf->prrqf_fc.fc_more_frag= 0;
  2452.   prrqf->prrqf_fc.fc_retry = 0;
  2453.   prrqf->prrqf_fc.fc_pwr_mgt = 0;
  2454.   prrqf->prrqf_fc.fc_more_data= 0;
  2455.   prrqf->prrqf_fc.fc_wep = 0;
  2456.   prrqf->prrqf_fc.fc_order = 0;
  2457. STORE4BYTE(&dst, (prrqf->prrqf_ra));
  2458. STORE4BYTE(&index_, (prrqf->prrqf_ta));
  2459. STORE4BYTE(&dst, (prrqf->prrqf_3a));
  2460. ch->txtime() = txtime(ch->size(), basicRate_);
  2461.   prrqf->prrqf_duration = 0;
  2462. pktPROBEREQ_ = p;
  2463. if(mhBackoff_.busy() == 0) {
  2464. if(is_idle()) {
  2465. if (mhDefer_.busy() == 0) {
  2466. /*
  2467.  * If we are already deferring, there is no
  2468.  * need to reset the Defer timer.
  2469.  */
  2470. if (bugFix_timer_) {
  2471.   mhBackoff_.start(cw_, is_idle(), 
  2472.   phymib_.getDIFS());
  2473. }
  2474. else {
  2475. rTime = (Random::random() % cw_)
  2476. * (phymib_.getSlotTime());
  2477. mhDefer_.start(phymib_.getDIFS() + 
  2478.        rTime);
  2479. }
  2480. }
  2481. } else {
  2482. /*
  2483.  * If the medium is NOT IDLE, then we start
  2484.  * the backoff timer.
  2485.  */
  2486. mhBackoff_.start(cw_, is_idle());
  2487. }
  2488. }
  2489. }
  2490. int
  2491. Mac802_11::check_pktPROBEREQ()
  2492. {
  2493. struct hdr_mac802_11 *mh;
  2494. double timeout;
  2495. assert(mhBackoff_.busy() == 0);
  2496. if(pktPROBEREQ_ == 0)
  2497.   return -1;
  2498. mh = HDR_MAC802_11(pktPROBEREQ_);
  2499.   switch(mh->dh_fc.fc_subtype) {
  2500. case MAC_Subtype_ProbeReq:
  2501. if(! is_idle()) {
  2502. inc_cw();
  2503. mhBackoff_.start(cw_, is_idle());
  2504. return 0;
  2505. }
  2506. setTxState(MAC_MGMT);
  2507. timeout = txtime(phymib_.getPROBEREQlen(), basicRate_)
  2508. + DSSS_MaxPropagationDelay;                      // XXX
  2509. break;
  2510. default:
  2511. fprintf(stderr, "check_pktPROBEREQ:Invalid MAC Control subtypen");
  2512. exit(1);
  2513. }
  2514. transmit(pktPROBEREQ_, timeout);
  2515. mhProbe_.start(macmib_.getMinChannelTime());
  2516. OnMinChannelTime = 1;
  2517. return 0;
  2518. }
  2519. void
  2520. Mac802_11::sendPROBEREP(int dst)
  2521. {
  2522. Packet *p = Packet::alloc();
  2523. hdr_cmn* ch = HDR_CMN(p);
  2524. struct proberep_frame *prrpf = (struct proberep_frame*)p->access(hdr_mac::offset_);
  2525. pktPROBEREP_ = 0;
  2526. ch->uid() = 0;
  2527. double rTime;
  2528. ch->ptype() = PT_MAC;
  2529. ch->size() = phymib_.getPROBEREPlen();
  2530. ch->iface() = -2;
  2531. ch->error() = 0;
  2532. bzero(prrpf, MAC_HDR_LEN);
  2533. prrpf->prrpf_fc.fc_protocol_version = MAC_ProtocolVersion;
  2534.   prrpf->prrpf_fc.fc_type = MAC_Type_Management;
  2535.   prrpf->prrpf_fc.fc_subtype = MAC_Subtype_ProbeRep;
  2536.   prrpf->prrpf_fc.fc_to_ds = 0;
  2537.   prrpf->prrpf_fc.fc_from_ds = 0;
  2538.   prrpf->prrpf_fc.fc_more_frag= 0;
  2539.   prrpf->prrpf_fc.fc_retry = 0;
  2540.   prrpf->prrpf_fc.fc_pwr_mgt = 0;
  2541.   prrpf->prrpf_fc.fc_more_data= 0;
  2542.   prrpf->prrpf_fc.fc_wep = 0;
  2543.   prrpf->prrpf_fc.fc_order = 0;
  2544. STORE4BYTE(&dst, (prrpf->prrpf_ra));
  2545. STORE4BYTE(&index_, (prrpf->prrpf_ta));
  2546. STORE4BYTE(&index_, (prrpf->prrpf_3a));
  2547. // prrpf->prrpf_timestamp = Scheduler::instance().clock();
  2548. prrpf->prrpf_bcninterval = phymib_.getBeaconInterval();
  2549. ch->txtime() = txtime(ch->size(), basicRate_);
  2550.   prrpf->prrpf_duration = 0;
  2551. pktPROBEREP_ = p;
  2552. if(mhBackoff_.busy() == 0) {
  2553. if(is_idle()) {
  2554. if (mhDefer_.busy() == 0) {
  2555. /*
  2556.  * If we are already deferring, there is no
  2557.  * need to reset the Defer timer.
  2558.  */
  2559. if (bugFix_timer_) {
  2560.   mhBackoff_.start(cw_, is_idle(), 
  2561.   phymib_.getDIFS());
  2562. priority_queue[head] = 1;
  2563. }
  2564. else {
  2565. rTime = (Random::random() % cw_)
  2566. * (phymib_.getSlotTime());
  2567. mhDefer_.start(phymib_.getDIFS() + 
  2568.        rTime);
  2569. }
  2570. }
  2571. } else {
  2572. /*
  2573.  * If the medium is NOT IDLE, then we start
  2574.  * the backoff timer.
  2575.  */
  2576. mhBackoff_.start(cw_, is_idle());
  2577. priority_queue[head] = 1;
  2578. }
  2579. } else {
  2580. priority_queue[end()] = 1;
  2581. }
  2582. }
  2583. int
  2584. Mac802_11::check_pktPROBEREP()
  2585. {
  2586. struct proberep_frame *prrpf = (struct proberep_frame*)pktPROBEREP_->access(hdr_mac::offset_);
  2587. prrpf->prrpf_timestamp = Scheduler::instance().clock();
  2588. struct hdr_mac802_11 *mh;
  2589. double timeout;
  2590. assert(mhBackoff_.busy() == 0);
  2591. if(pktPROBEREP_ == 0)
  2592.   return -1;
  2593. mh = HDR_MAC802_11(pktPROBEREP_);
  2594.   switch(mh->dh_fc.fc_subtype) {
  2595. case MAC_Subtype_ProbeRep:
  2596. if(! is_idle()) {
  2597. inc_cw();
  2598. mhBackoff_.start(cw_, is_idle());
  2599. return 0;
  2600. }
  2601. setTxState(MAC_MGMT);
  2602. timeout = txtime(phymib_.getPROBEREPlen(), basicRate_)
  2603. + DSSS_MaxPropagationDelay                     // XXX
  2604. + phymib_.getSIFS()
  2605. + txtime(phymib_.getACKlen(), basicRate_)
  2606. + DSSS_MaxPropagationDelay;
  2607. break;
  2608. default:
  2609. fprintf(stderr, "check_pktPROBEREP:Invalid MAC Control subtypen");
  2610. exit(1);
  2611. }
  2612. transmit(pktPROBEREP_, timeout);
  2613.   
  2614. return 0;
  2615. }
  2616. void
  2617. Mac802_11::recvPROBEREQ(Packet *p)
  2618. {
  2619. struct probereq_frame *prrqf = (struct probereq_frame*)p->access(hdr_mac::offset_);
  2620. if(tx_state_ != MAC_IDLE) {
  2621. discard(p, DROP_MAC_BUSY);
  2622. return;
  2623. }
  2624. u_int32_t bss_id, src;
  2625. bss_id = ETHER_ADDR(prrqf->prrqf_3a);
  2626.   src = ETHER_ADDR(prrqf->prrqf_ta);
  2627. if (!pktPROBEREP_) {
  2628. sendPROBEREP(src);
  2629. } else {
  2630. discard(p, DROP_MAC_BUSY);
  2631. return;
  2632. }  
  2633. tx_resume();
  2634. mac_log(p);
  2635. }
  2636. void
  2637. Mac802_11::recvPROBEREP(Packet *p)
  2638. {
  2639. struct proberep_frame *prrpf = (struct proberep_frame*)p->access(hdr_mac::offset_);
  2640. u_int32_t bss_id, src;
  2641. Pr = p->txinfo_.RxPr;
  2642.   src = ETHER_ADDR(prrpf->prrpf_ta);
  2643. bss_id = ETHER_ADDR(prrpf->prrpf_ta);
  2644. update_ap_table(src,Pr);
  2645. sendACK(src);
  2646. if(mhSend_.busy() == 0)
  2647. tx_resume();
  2648. mac_log(p);
  2649. }
  2650. void Mac802_11::checkAssocAuthStatus() {
  2651. if ( addr() != bss_id_ ) {
  2652. if (authenticated == 0 && associated == 0) {
  2653. sendAUTHENTICATE(strongest_ap());
  2654. if (authenticated == 1 && associated == 0) {
  2655. sendASSOCREQ(bss_id_);
  2656. }
  2657. }
  2658. }
  2659. /* STA's beacon power table funtions
  2660. */
  2661. void Mac802_11::update_ap_table(int num, double power) {
  2662. if (ap_list == NULL) {
  2663. ap_list = (struct ap_table*)malloc(sizeof(struct ap_table));
  2664. ap_list->ap_id=num;
  2665. ap_list->ap_power = power;
  2666. ap_list->next=NULL;
  2667. }
  2668. else {
  2669. push_ap(num, power);
  2670. }
  2671. struct ap_table *temp;
  2672. temp = ap_list;
  2673. //  while (temp != NULL) {
  2674. //  printf("Client %d: AP %d and %ft", index_, temp->ap_id,temp->ap_power);
  2675. //  temp=temp->next;
  2676. //  }
  2677. //  printf("n");
  2678. }
  2679. void Mac802_11::push_ap(int num, double power) {
  2680. struct ap_table *temp;
  2681. temp = ap_list;
  2682. while (temp->next != NULL) {
  2683. temp=temp->next;
  2684. }
  2685. temp->next = (struct ap_table*)malloc(sizeof(struct ap_table));
  2686. temp->next->ap_id = num;
  2687. temp->next->ap_power= power;
  2688. temp->next->next = NULL; 
  2689. }
  2690. void Mac802_11::deletelist() {
  2691. struct ap_table *temp;
  2692. while (ap_list != NULL) {
  2693. temp = ap_list;
  2694. ap_list = ap_list->next;
  2695. free(temp);
  2696. }
  2697. }
  2698. int Mac802_11::strongest_ap() {
  2699. struct ap_table *temp;
  2700. double max_power;
  2701. int ap;
  2702. temp = ap_list;
  2703. if (ap_list == NULL)
  2704. return -1;
  2705. max_power = 0;
  2706. while (temp != NULL) {
  2707. if (temp->ap_power > max_power) {
  2708. max_power = temp->ap_power;
  2709. ap = temp->ap_id;
  2710. }
  2711. temp = temp->next;
  2712. }
  2713. return ap;
  2714. }
  2715. int Mac802_11::find_ap(int num, double power) {
  2716. struct ap_table *temp;
  2717. temp = ap_list;
  2718. while (temp != NULL) {
  2719. if (temp->ap_id == num && temp->ap_power == power) {
  2720. return 1;
  2721. temp=temp->next;
  2722. if (temp == NULL) {
  2723. return 0; 
  2724. }
  2725. }
  2726. return 0;
  2727. }
  2728. int Mac802_11::end() 
  2729. {
  2730. int end;
  2731. end = head;
  2732. while (priority_queue[end] != 0) {
  2733. end = end + 1;
  2734. }
  2735. return end;
  2736. }
  2737. void Mac802_11::shift_priority_queue()
  2738. {
  2739. int i;
  2740. i = head;
  2741. while (priority_queue[i] != 0) {
  2742. priority_queue[i] = priority_queue[i+1];
  2743. i = i + 1;
  2744. }
  2745. }