some clean-ups
[senf.git] / Packets / 80211Bundle / WLANPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Christian Niephaus <cni@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief 802.11 WLANPacket public header */
25
26 #ifndef HH_SENF_Packets_80211Bundle_WLANPacket_
27 #define HH_SENF_Packets_80211Bundle_WLANPacket_ 1
28
29 #include "../../Packets/Packets.hh"
30 #include "../DefaultBundle/EthernetPacket.hh"
31
32 namespace senf
33 {
34
35     /** \brief Management frame parser
36      * <b>Re-ordering of bits due to LSB byte order</b>
37
38      */
39     struct WLANPacket_MgtFrameParser : public senf::PacketParserBase
40     {
41     #   include SENF_FIXED_PARSER()
42         
43         SENF_PARSER_PRIVATE_BITFIELD ( subtype, 4,  unsigned                );
44         SENF_PARSER_SKIP_BITS        (          4                           ); //skip type and version
45         //jump to fist address field
46         SENF_PARSER_SKIP             ( 3                                    );
47         SENF_PARSER_FIELD            ( destinationAddress, MACAddressParser );
48         SENF_PARSER_FIELD            ( sourceAddress,      MACAddressParser );
49         SENF_PARSER_FIELD            ( bssid,              MACAddressParser );
50
51         //workaround since bitfield LSB parsers are not available
52         SENF_PARSER_PRIVATE_BITFIELD ( seqNumber_1,    4, unsigned );
53         SENF_PARSER_BITFIELD         ( fragmentNumber, 4, unsigned );
54         SENF_PARSER_PRIVATE_FIELD    ( seqNumber_2,    UInt8Parser );
55
56         SENF_PARSER_FINALIZE(WLANPacket_MgtFrameParser);
57
58         //this is needed due to the goto in the WLANPacketParser. Don't know exactly why yet.
59         SENF_PARSER_INIT() {}
60
61         boost::uint16_t sequenceNumber() const;
62     };
63
64     /** \brief Control frame parser
65      * <b>Re-ordering of bits due to LSB byte order</b>
66      *
67      * currently only CTS, RTS and ACK control frames are supported
68
69      */
70     struct WLANPacket_CtrlFrameParser : public senf::PacketParserBase
71     {
72     #   include SENF_PARSER()
73         
74         SENF_PARSER_PRIVATE_BITFIELD ( subtype,  4,  unsigned            );
75         SENF_PARSER_SKIP_BITS        (           4                       ); //skip type and version
76         //jump to fist address field
77         SENF_PARSER_SKIP             ( 3, 3                              );
78         SENF_PARSER_FIELD            ( recieverAddress, MACAddressParser );
79
80         //only RTS frame contains a source address field
81         //variant is also needed to set correct subtype value
82         SENF_PARSER_VARIANT ( subtype__, subtype,
83                 ( ids( na,            is_cts, set_cts, key(12, VoidPacketParser)) )
84                 ( ids( na,            is_ack, set_ack, key(13, VoidPacketParser)) )
85                 ( ids( sourceAddress, is_rts, set_rts, key(11, MACAddressParser)) ) );
86
87         SENF_PARSER_FINALIZE(WLANPacket_CtrlFrameParser);
88
89         //this is needed to due to the goto in the WLANPacketParser. Don't know exactly why yet.
90         SENF_PARSER_INIT() {}
91     };
92
93     /** \brief Data frame parser
94      * <b>Re-ordering of bits due to LSB byte order</b>
95
96      */
97     struct WLANPacket_DataFrameParser : public senf::PacketParserBase
98     {
99     #   include SENF_PARSER()
100         SENF_PARSER_PRIVATE_BITFIELD    ( subtype,  4,  unsigned);
101         //jump to 'toDS' and 'fromDS' bits
102         //skip type and version
103         SENF_PARSER_SKIP_BITS   ( 4                                    );
104         //skip other flags
105         SENF_PARSER_SKIP_BITS   ( 6                                    );
106         //needed in data frames due to the variable address fields
107         SENF_PARSER_PRIVATE_BITFIELD    ( dsBits,    2,  unsigned);
108         //skip duration field
109         SENF_PARSER_SKIP        ( 2,0                          );
110
111         SENF_PARSER_PRIVATE_FIELD               ( addr1, MACAddressParser        );
112         SENF_PARSER_PRIVATE_FIELD               ( addr2, MACAddressParser        );
113         SENF_PARSER_PRIVATE_FIELD               ( addr3, MACAddressParser        );
114
115         //sequence Number and fragment number
116         //shift bits manually due to LSB
117         SENF_PARSER_PRIVATE_BITFIELD    (seqNumber_1, 4, unsigned);
118         SENF_PARSER_BITFIELD    (fragmentNumber, 4, unsigned);
119         SENF_PARSER_PRIVATE_FIELD     (seqNumber_2, UInt8Parser)
120
121         //TODO fourth address field in case of WDS
122 //        SENF_PARSER_PRIVATE_VARIANT (wds_, dsBits,
123 //                ( novalue ( disable_addr4,               VoidPacketParser                    ))
124 //                ( id      ( addr4,  key (3,                   MACAddressParser                     ))) );
125
126         //QoS Filed
127         SENF_PARSER_VARIANT (qosField_, subtype,
128                 ( ids (na, na, set_data, key(0, VoidPacketParser)))
129                 ( ids (na, na, set_nullData, key(4, VoidPacketParser)))
130                 ( ids (qosField, has_qosField, set_qosData, key(8, UInt16LSBParser)))
131                 //we cannot parse qos Null (data) frames at the moment
132                 ( ids (na, na, set_qosNullData, key(12, UInt16LSBParser))) );
133
134
135
136         SENF_PARSER_FINALIZE(WLANPacket_DataFrameParser);
137
138         //this is needed to due to the goto in the WLANPacketParser. Don't know exactly why yet.
139         SENF_PARSER_INIT() {}
140
141         boost::uint16_t sequenceNumber() const;
142
143         MACAddressParser ra() const { return addr1(); }; //ra is always addr1
144         MACAddressParser ta() const { return addr2(); }; //ta is always addr2
145         MACAddressParser sa() const;
146         MACAddressParser da() const;
147         MACAddressParser bssid() const;
148     };
149
150
151     /** \brief 802.11 Frame parser
152      * (see IEEE 802.11-2007 standard - Chapter 7 Frame formats)
153      * <b>Re-ordering of bits due to LSB byte order</b>
154
155      */
156     struct WLANPacketParser : public senf::PacketParserBase
157     {
158     #   include SENF_PARSER()
159
160         /*
161          * Frame control field
162          * re-ordering of fields due to the byte order
163          */
164         SENF_PARSER_BITFIELD_RO ( subtype,        4,  unsigned );
165         SENF_PARSER_BITFIELD_RO ( type,           2,  unsigned );
166         SENF_PARSER_BITFIELD    ( version,        2,  unsigned );
167         SENF_PARSER_BITFIELD    ( order,          1,  bool     );
168         SENF_PARSER_BITFIELD    ( protectedFrame, 1,  bool     );
169         SENF_PARSER_BITFIELD    ( moreData,       1,  bool     );
170         SENF_PARSER_BITFIELD    ( pwrMgt,         1,  bool     );
171         SENF_PARSER_BITFIELD    ( retry,          1,  bool     );
172         SENF_PARSER_BITFIELD    ( moreFrag,       1,  bool     );
173         SENF_PARSER_BITFIELD    ( fromDS,         1,  bool     );
174         SENF_PARSER_BITFIELD    ( toDS,           1,  bool     );
175
176         SENF_PARSER_FIELD (duration,          UInt16LSBParser);
177
178         SENF_PARSER_GOTO( subtype ); //subparsers need to know the subtype
179         SENF_PARSER_VARIANT ( type__,             type,
180                 (      id( mgtFrame,   WLANPacket_MgtFrameParser  ))
181                 (      id( ctrlFrame,  WLANPacket_CtrlFrameParser ))
182                 (      id( dataFrame,  WLANPacket_DataFrameParser ))
183                 ( novalue( reserved,   WLANPacket_CtrlFrameParser )) );
184
185         SENF_PARSER_CUSTOM_FIELD( fcs, senf::UInt32Parser, fcs_t::fixed_bytes, fcs_t::fixed_bytes) {
186           return parse<UInt32Parser>( data().size()-4 ); }
187
188         SENF_PARSER_FINALIZE(WLANPacketParser);
189
190         SENF_PARSER_INIT() {
191             version() = 0;
192         }
193
194         //Problems can occur with old madwifi and ath5k. Some frames only
195         //contains two byte FCS instead of four.
196 //        UInt32Parser fcs() const { return parse<UInt32Parser>( data().size()-4 ); }
197
198     };
199
200     struct WLANPacketType
201         : public senf::PacketTypeBase,
202           public senf::PacketTypeMixin<WLANPacketType>
203     {
204         typedef senf::PacketTypeMixin<WLANPacketType> mixin;
205         typedef senf::ConcretePacket<WLANPacketType> packet;
206         typedef WLANPacketParser parser;
207
208 //        using mixin::nextPacketRange;
209         using mixin::init;
210         using mixin::initSize;
211         using senf::PacketTypeBase::nextPacketRange;;
212
213         static void dump(packet p, std::ostream &os);
214 //        static PacketParserBase::size_type initSize();
215     };
216
217     typedef senf::ConcretePacket<WLANPacketType> WLANPacket;
218 }
219
220
221 #endif /* HH_SENF_Packets_80211Bundle_WLANPacket_ */