• Main Page
  • Classes
  • Files
  • File List

/Users/yzchen/ns/ns-allinone-2.33/ns-2.33/mac/mac-802_11.h

00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*-
00002  *
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *      This product includes software developed by the Computer Systems
00017  *      Engineering Group at Lawrence Berkeley Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * $Header: /cvsroot/nsnam/ns-2/mac/mac-802_11.h,v 1.28 2008/01/24 01:53:19 tom_henderson Exp $
00035  *
00036  * Ported from CMU/Monarch's code, nov'98 -Padma.
00037  * wireless-mac-802_11.h
00038  */
00039 
00040 #ifndef ns_mac_80211_h
00041 #define ns_mac_80211_h
00042 
00043 // Added by Sushmita to support event tracing (singal@nunki.usc.edu)
00044 #include "address.h"
00045 #include "ip.h"
00046 
00047 #include "mac-timers.h"
00048 #include "marshall.h"
00049 #include <math.h>
00050 #include <stddef.h>
00051 
00052 class EventTrace;
00053 
00054 #define GET_ETHER_TYPE(x)               GET2BYTE((x))
00055 #define SET_ETHER_TYPE(x,y)            {u_int16_t t = (y); STORE2BYTE(x,&t);}
00056 
00057 /* ======================================================================
00058    Frame Formats
00059    ====================================================================== */
00060 
00061 #define MAC_ProtocolVersion     0x00
00062 
00063 #define MAC_Type_Management     0x00
00064 #define MAC_Type_Control        0x01
00065 #define MAC_Type_Data           0x02
00066 #define MAC_Type_Reserved       0x03
00067 
00068 #define MAC_Subtype_RTS         0x0B
00069 #define MAC_Subtype_CTS         0x0C
00070 #define MAC_Subtype_ACK         0x0D
00071 #define MAC_Subtype_Data        0x00
00072 
00073 #define MAC_Subtype_80211_Beacon        0x08
00074 #define MAC_Subtype_AssocReq    0x00
00075 #define MAC_Subtype_AssocRep    0x01
00076 #define MAC_Subtype_Auth        0x0C
00077 #define MAC_Subtype_ProbeReq    0x04
00078 #define MAC_Subtype_ProbeRep    0x05
00079 
00080 
00081 struct frame_control {
00082         u_char          fc_subtype              : 4;
00083         u_char          fc_type                 : 2;
00084         u_char          fc_protocol_version     : 2;
00085 
00086         u_char          fc_order                : 1;
00087         u_char          fc_wep                  : 1;
00088         u_char          fc_more_data            : 1;
00089         u_char          fc_pwr_mgt              : 1;
00090         u_char          fc_retry                : 1;
00091         u_char          fc_more_frag            : 1;
00092         u_char          fc_from_ds              : 1;
00093         u_char          fc_to_ds                : 1;
00094 };
00095 
00096 struct rts_frame {
00097         struct frame_control    rf_fc;
00098         u_int16_t               rf_duration;
00099         u_char                  rf_ra[ETHER_ADDR_LEN];
00100         u_char                  rf_ta[ETHER_ADDR_LEN];
00101         u_char                  rf_fcs[ETHER_FCS_LEN];
00102 };
00103 
00104 struct cts_frame {
00105         struct frame_control    cf_fc;
00106         u_int16_t               cf_duration;
00107         u_char                  cf_ra[ETHER_ADDR_LEN];
00108         u_char                  cf_fcs[ETHER_FCS_LEN];
00109 };
00110 
00111 struct ack_frame {
00112         struct frame_control    af_fc;
00113         u_int16_t               af_duration;
00114         u_char                  af_ra[ETHER_ADDR_LEN];
00115         u_char                  af_fcs[ETHER_FCS_LEN];
00116 };
00117 
00118 struct beacon_frame {
00119         struct frame_control    bf_fc;
00120         u_int16_t               bf_duration;
00121         u_char                  bf_ra[ETHER_ADDR_LEN];
00122         u_char                  bf_ta[ETHER_ADDR_LEN];
00123         u_char                  bf_3a[ETHER_ADDR_LEN];
00124         u_int16_t               bf_scontrol;
00125         double                  bf_timestamp;
00126         double                  bf_bcninterval;
00127         u_int8_t                bf_datarates[1];
00128         u_char                  bf_fcs[ETHER_FCS_LEN];
00129 };
00130 
00131 struct assocreq_frame {
00132         struct frame_control    acrqf_fc;
00133         u_int16_t               acrqf_duration;
00134         u_char                  acrqf_ra[ETHER_ADDR_LEN];
00135         u_char                  acrqf_ta[ETHER_ADDR_LEN];
00136         u_char                  acrqf_3a[ETHER_ADDR_LEN];
00137         u_int16_t               acrqf_scontrol;
00138         u_char                  acrqf_fcs[ETHER_FCS_LEN];
00139 };
00140 
00141 struct assocrep_frame {
00142         struct frame_control    acrpf_fc;
00143         u_int16_t               acrpf_duration;
00144         u_char                  acrpf_ra[ETHER_ADDR_LEN];
00145         u_char                  acrpf_ta[ETHER_ADDR_LEN];
00146         u_char                  acrpf_3a[ETHER_ADDR_LEN];
00147         u_int16_t               acrpf_scontrol;
00148         u_int16_t               acrpf_statuscode;
00149         u_char                  acrpf_fcs[ETHER_FCS_LEN];
00150 };
00151 
00152 struct auth_frame {
00153         struct frame_control    authf_fc;
00154         u_int16_t               authf_duration;
00155         u_char                  authf_ra[ETHER_ADDR_LEN];
00156         u_char                  authf_ta[ETHER_ADDR_LEN];
00157         u_char                  authf_3a[ETHER_ADDR_LEN];
00158         u_int16_t               authf_scontrol;
00159         u_int16_t               authf_algono;
00160         u_int16_t               authf_seqno;
00161         u_int16_t               authf_statuscode;
00162         u_char                  authf_fcs[ETHER_FCS_LEN];
00163 };
00164 
00165 struct probereq_frame {
00166         struct frame_control    prrqf_fc;
00167         u_int16_t               prrqf_duration;
00168         u_char                  prrqf_ra[ETHER_ADDR_LEN];
00169         u_char                  prrqf_ta[ETHER_ADDR_LEN];
00170         u_char                  prrqf_3a[ETHER_ADDR_LEN];
00171         u_int16_t               prrqf_scontrol;
00172         u_char                  prrqf_fcs[ETHER_FCS_LEN];
00173 };
00174 
00175 struct proberep_frame {
00176         struct frame_control    prrpf_fc;
00177         u_int16_t               prrpf_duration;
00178         u_char                  prrpf_ra[ETHER_ADDR_LEN];
00179         u_char                  prrpf_ta[ETHER_ADDR_LEN];
00180         u_char                  prrpf_3a[ETHER_ADDR_LEN];
00181         u_int16_t               prrpf_scontrol;
00182         double                  prrpf_timestamp;
00183         double                  prrpf_bcninterval;
00184         u_int8_t                prrpf_datarates[1];
00185         u_char                  prrpf_fcs[ETHER_FCS_LEN];
00186 };
00187 
00188 
00189 
00190 // XXX This header does not have its header access function because it shares
00191 // the same header space with hdr_mac.
00192 struct hdr_mac802_11 {
00193         struct frame_control    dh_fc;
00194         u_int16_t               dh_duration;
00195         u_char                  dh_ra[ETHER_ADDR_LEN];
00196         u_char                  dh_ta[ETHER_ADDR_LEN];
00197         u_char                  dh_3a[ETHER_ADDR_LEN];
00198         u_char                  dh_4a[ETHER_ADDR_LEN];
00199         u_int16_t               dh_scontrol;
00200         u_char                  dh_body[1]; // size of 1 for ANSI compatibility
00201 };
00202 
00203 struct client_table {
00204         int client_id;
00205         int auth_status;
00206         int assoc_status;
00207         struct client_table *next;
00208 };
00209 
00210 struct ap_table {
00211         int ap_id;
00212         double ap_power;
00213         struct ap_table *next;
00214 };
00215 
00216 
00217 /* ======================================================================
00218    Definitions
00219    ====================================================================== */
00220 
00221 /* Must account for propagation delays added by the channel model when
00222  * calculating tx timeouts (as set in tcl/lan/ns-mac.tcl).
00223  *   -- Gavin Holland, March 2002
00224  */
00225 #define DSSS_MaxPropagationDelay        0.000002        // 2us   XXXX
00226 
00227 class PHY_MIB {
00228 public:
00229         PHY_MIB(Mac802_11 *parent);
00230 
00231         inline u_int32_t getCWMin() { return(CWMin); }
00232         inline u_int32_t getCWMax() { return(CWMax); }
00233         inline double getSlotTime() { return(SlotTime); }
00234         inline double getBeaconInterval() { return(BeaconInterval); }
00235         inline double getSIFS() { return(SIFSTime); }
00236         inline double getPIFS() { return(SIFSTime + SlotTime); }
00237         inline double getDIFS() { return(SIFSTime + 2 * SlotTime); }
00238         inline double getEIFS() {
00239                 // see (802.11-1999, 9.2.10)
00240                 return(SIFSTime + getDIFS() + (8 *  getACKlen())/PLCPDataRate);
00241         }
00242         inline u_int32_t getPreambleLength() { return(PreambleLength); }
00243         inline double getPLCPDataRate() { return(PLCPDataRate); }
00244 
00245         inline u_int32_t getPLCPhdrLen() {
00246                 return((PreambleLength + PLCPHeaderLength) >> 3);
00247         }
00248 
00249         inline u_int32_t getHdrLen11() {
00250                 return(getPLCPhdrLen() + offsetof(struct hdr_mac802_11, dh_body[0]) + ETHER_FCS_LEN);
00251         }
00252 
00253         inline u_int32_t getRTSlen() {
00254                 return(getPLCPhdrLen() + sizeof(struct rts_frame));
00255         }
00256 
00257         inline u_int32_t getCTSlen() {
00258                 return(getPLCPhdrLen() + sizeof(struct cts_frame));
00259         }
00260 
00261         inline u_int32_t getACKlen() {
00262                 return(getPLCPhdrLen() + sizeof(struct ack_frame));
00263         }
00264         inline u_int32_t getBEACONlen() {
00265                 return(getPLCPhdrLen() + sizeof(struct beacon_frame));
00266         }
00267         inline u_int32_t getASSOCREQlen() {
00268                 return(getPLCPhdrLen() + sizeof(struct assocreq_frame));
00269         }
00270         inline u_int32_t getASSOCREPlen() {
00271                 return(getPLCPhdrLen() + sizeof(struct assocrep_frame));
00272         }
00273         inline u_int32_t getAUTHENTICATElen() {
00274                 return(getPLCPhdrLen() + sizeof(struct auth_frame));
00275         }
00276         inline u_int32_t getPROBEREQlen() {
00277                 return(getPLCPhdrLen() + sizeof(struct probereq_frame));
00278         }
00279         inline u_int32_t getPROBEREPlen() {
00280                 return(getPLCPhdrLen() + sizeof(struct proberep_frame));
00281         }
00282 
00283 
00284 private:
00285         u_int32_t       CWMin;
00286         u_int32_t       CWMax;
00287         double          SlotTime;
00288         double          SIFSTime;
00289         double          BeaconInterval;
00290         u_int32_t       PreambleLength;
00291         u_int32_t       PLCPHeaderLength;
00292         double          PLCPDataRate;
00293 };
00294 
00295 
00296 /*
00297  * IEEE 802.11 Spec, section 11.4.4.2
00298  *      - default values for the MAC Attributes
00299  */
00300 #define MAC_FragmentationThreshold      2346    // bytes
00301 #define MAC_MaxTransmitMSDULifetime     512             // time units
00302 #define MAC_MaxReceiveLifetime          512             // time units
00303 
00304 
00305 class MAC_MIB {
00306 public:
00307 
00308         MAC_MIB(Mac802_11 *parent);
00309 
00310 private:
00311         u_int32_t       RTSThreshold;
00312         u_int32_t       ShortRetryLimit;
00313         u_int32_t       LongRetryLimit;
00314         u_int32_t       ScanType;
00315         double          ProbeDelay;
00316         double          MaxChannelTime;
00317         double          MinChannelTime;
00318         double          ChannelTime;
00319 
00320 public:
00321         u_int32_t       FailedCount;
00322         u_int32_t       RTSFailureCount;
00323         u_int32_t       ACKFailureCount;
00324  public:
00325         inline u_int32_t getRTSThreshold() { return(RTSThreshold);}
00326         inline u_int32_t getShortRetryLimit() { return(ShortRetryLimit);}
00327         inline u_int32_t getLongRetryLimit() { return(LongRetryLimit);}
00328         inline u_int32_t getScanType() { return(ScanType);}
00329         inline double getProbeDelay() { return(ProbeDelay);}
00330         inline double getMaxChannelTime() { return(MaxChannelTime);}
00331         inline double getMinChannelTime() { return(MinChannelTime);}
00332         inline double getChannelTime() { return(ChannelTime);}
00333 };
00334 
00335 
00336 /* ======================================================================
00337    The following destination class is used for duplicate detection.
00338    ====================================================================== */
00339 class Host {
00340 public:
00341         LIST_ENTRY(Host) link;
00342         u_int32_t       index;
00343         u_int32_t       seqno;
00344 };
00345 
00346 
00347 /* ======================================================================
00348    The actual 802.11 MAC class.
00349    ====================================================================== */
00350 class Mac802_11 : public Mac {
00351         friend class DeferTimer;
00352 
00353         friend class BeaconTimer;
00354         friend class ProbeTimer;
00355         friend class BackoffTimer;
00356         friend class IFTimer;
00357         friend class NavTimer;
00358         friend class RxTimer;
00359         friend class TxTimer;
00360 public:
00361         Mac802_11();
00362         void            recv(Packet *p, Handler *h);
00363         inline int      hdr_dst(char* hdr, int dst = -2);
00364         inline int      hdr_src(char* hdr, int src = -2);
00365         inline int      hdr_type(char* hdr, u_int16_t type = 0);
00366 
00367         inline int bss_id() { return bss_id_; }
00368 
00369         // Added by Sushmita to support event tracing
00370         void trace_event(char *, Packet *);
00371         EventTrace *et_;
00372 
00373 protected:
00374         void    backoffHandler(void);
00375         void    deferHandler(void);
00376         void    BeaconHandler(void);
00377         void    ProbeHandler(void);
00378         void    navHandler(void);
00379         void    recvHandler(void);
00380         void    sendHandler(void);
00381         void    txHandler(void);
00382 
00383 private:
00384         void    update_client_table(int num, int auth_status, int assoc_status);
00385         void    push(int num, int auth_status, int assoc_status);
00386         int             find_client(int num);
00387         void    update_ap_table(int num, double power);
00388         void    push_ap(int num, double power);
00389         int     strongest_ap();
00390         int             find_ap(int num, double power);
00391         void    deletelist();
00392         void    passive_scan();
00393         void    active_scan();
00394         int             end();
00395         void    shift_priority_queue();
00396         void    checkAssocAuthStatus();
00397         int             command(int argc, const char*const* argv);
00398 
00399         /* In support of bug fix described at
00400          * http://www.dei.unipd.it/wdyn/?IDsezione=2435
00401          */
00402         int bugFix_timer_;
00403         int infra_mode_;
00404         double BeaconTxtime_;
00405         int associated;
00406         int authenticated;
00407         int handoff;
00408         double Pr;
00409         int ap_temp;
00410         int ap_addr;
00411         int associating_node_;
00412         int authenticating_node_;
00413         int ScanType_;
00414         int OnMinChannelTime;
00415         int OnMaxChannelTime;
00416         int Recv_Busy_;
00417         /*
00418          * Called by the timers.
00419          */
00420         void    recv_timer(void);
00421         void    send_timer(void);
00422         int             check_pktCTRL();
00423         int             check_pktRTS();
00424         int             check_pktTx();
00425         int             check_pktASSOCREQ();
00426         int             check_pktASSOCREP();
00427         int             check_pktBEACON();
00428         int             check_pktAUTHENTICATE();
00429         int             check_pktPROBEREQ();
00430         int             check_pktPROBEREP();
00431 
00432         /*
00433          * Packet Transmission Functions.
00434          */
00435         void    send(Packet *p, Handler *h);
00436         void    sendRTS(int dst);
00437         void    sendCTS(int dst, double duration);
00438         void    sendACK(int dst);
00439         void    sendDATA(Packet *p);
00440         void    sendBEACON(int src);
00441         void    sendASSOCREQ(int dst);
00442         void    sendASSOCREP(int dst);
00443         void    sendPROBEREQ(int dst);
00444         void    sendPROBEREP(int dst);
00445         void    sendAUTHENTICATE(int dst);
00446         void    RetransmitRTS();
00447         void    RetransmitDATA();
00448         void    RetransmitASSOCREP();
00449         void    RetransmitPROBEREP();
00450         void    RetransmitAUTHENTICATE();
00451 
00452         /*
00453          * Packet Reception Functions.
00454          */
00455         void    recvRTS(Packet *p);
00456         void    recvCTS(Packet *p);
00457         void    recvACK(Packet *p);
00458         void    recvDATA(Packet *p);
00459         void    recvBEACON(Packet *p);
00460         void    recvASSOCREQ(Packet *p);
00461         void    recvASSOCREP(Packet *p);
00462         void    recvPROBEREQ(Packet *p);
00463         void    recvPROBEREP(Packet *p);
00464         void    recvAUTHENTICATE(Packet *p);
00465 
00466         void    capture(Packet *p);
00467         void    collision(Packet *p);
00468         void    discard(Packet *p, const char* why);
00469         void    rx_resume(void);
00470         void    tx_resume(void);
00471 
00472         inline int      is_idle(void);
00473 
00474         /*
00475          * Debugging Functions.
00476          */
00477         void    trace_pkt(Packet *p);
00478         void    dump(char* fname);
00479 
00480         inline int initialized() {
00481                 return (cache_ && logtarget_
00482                         && Mac::initialized());
00483         }
00484 
00485         inline void mac_log(Packet *p) {
00486                 logtarget_->recv(p, (Handler*) 0);
00487         }
00488 
00489         double txtime(Packet *p);
00490         double txtime(double psz, double drt);
00491         double txtime(int bytes) { /* clobber inherited txtime() */ abort(); return 0;}
00492 
00493         inline void transmit(Packet *p, double timeout);
00494         inline void checkBackoffTimer(void);
00495         inline void postBackoff(int pri);
00496         inline void setRxState(MacState newState);
00497         inline void setTxState(MacState newState);
00498 
00499 
00500         inline void inc_cw() {
00501                 cw_ = (cw_ << 1) + 1;
00502                 if(cw_ > phymib_.getCWMax())
00503                         cw_ = phymib_.getCWMax();
00504         }
00505         inline void rst_cw() { cw_ = phymib_.getCWMin(); }
00506 
00507         inline double sec(double t) { return(t *= 1.0e-6); }
00508         inline u_int16_t usec(double t) {
00509                 u_int16_t us = (u_int16_t)floor((t *= 1e6) + 0.5);
00510                 return us;
00511         }
00512         inline void set_nav(u_int16_t us) {
00513                 double now = Scheduler::instance().clock();
00514                 double t = us * 1e-6;
00515                 if((now + t) > nav_) {
00516                         nav_ = now + t;
00517                         if(mhNav_.busy())
00518                                 mhNav_.stop();
00519                         mhNav_.start(t);
00520                 }
00521         }
00522 
00523 protected:
00524         PHY_MIB         phymib_;
00525         MAC_MIB         macmib_;
00526 
00527         /* the macaddr of my AP in BSS mode; for IBSS mode
00528         * this is set to a reserved value IBSS_ID - the
00529         * MAC_BROADCAST reserved value can be used for this
00530         * purpose
00531         */
00532         int     bss_id_;
00533         enum    {IBSS_ID = MAC_BROADCAST};
00534         enum    {
00535                                 PASSIVE = 0,
00536                                 ACTIVE = 1
00537                         };
00538 
00539 private:
00540         double          basicRate_;
00541         double          dataRate_;
00542         struct client_table     *client_list;
00543         struct ap_table         *ap_list;
00544         int             priority_queue[4];
00545         int             head;
00546         /*
00547          * Mac Timers
00548          */
00549         IFTimer         mhIF_;          // interface timer
00550         NavTimer        mhNav_;         // NAV timer
00551         RxTimer         mhRecv_;                // incoming packets
00552         TxTimer         mhSend_;                // outgoing packets
00553 
00554         DeferTimer      mhDefer_;       // defer timer
00555         BackoffTimer    mhBackoff_;     // backoff timer
00556         BeaconTimer     mhBeacon_;      // Beacon Timer
00557         ProbeTimer      mhProbe_;       //Probe timer,
00558 
00559         /* ============================================================
00560            Internal MAC State
00561            ============================================================ */
00562         double          nav_;           // Network Allocation Vector
00563 
00564         MacState        rx_state_;      // incoming state (MAC_RECV or MAC_IDLE)
00565         MacState        tx_state_;      // outgoint state
00566         int                     tx_active_;     // transmitter is ACTIVE
00567 
00568         Packet          *eotPacket_;    // copy for eot callback
00569 
00570         Packet          *pktRTS_;       // outgoing RTS packet
00571         Packet          *pktCTRL_;      // outgoing non-RTS packet
00572         Packet          *pktBEACON_;    //outgoing Beacon Packet
00573         Packet          *pktASSOCREQ_;  //Association request
00574         Packet          *pktASSOCREP_;  // Association response
00575         Packet          *pktAUTHENTICATE_; //Authentication
00576         Packet          *pktPROBEREQ_;  //Probe Request
00577         Packet          *pktPROBEREP_;  //Probe Response
00578 
00579         u_int32_t       cw_;            // Contention Window
00580         u_int32_t       ssrc_;          // STA Short Retry Count
00581         u_int32_t       slrc_;          // STA Long Retry Count
00582 
00583         int                     min_frame_len_;
00584 
00585         NsObject*       logtarget_;
00586         NsObject*       EOTtarget_;     // given a copy of packet at TX end
00587 
00588 
00589         /* ============================================================
00590            Duplicate Detection state
00591            ============================================================ */
00592         u_int16_t       sta_seqno_;     // next seqno that I'll use
00593         int                     cache_node_count_;
00594         Host            *cache_;
00595 };
00596 
00597 #endif /* __mac_80211_h__ */
00598 

Generated on Tue Aug 10 2010 16:16:07 for ns-2.33 by  doxygen 1.7.1