47f4930ace53e05b6d2e0f3f91330b3bcb7590c6
[senf.git] / Packets / 80211Bundle / WLANPacket.hh
1 // $Id:$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Christian Niephaus <christian.niephaus@fokus.fraunhofer.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 WLan MAC frame \n
25
26
27     */
28
29 #ifndef WLANPACKET_HH_
30 #define WLANPACKET_HH_
31
32 #include "../../Packets/Packets.hh"
33 #include "../DefaultBundle/EthernetPacket.hh"
34 #include <boost/cstdint.hpp>
35
36 namespace senf
37 {
38
39     /** \brief Management frame parser
40      * <b>Re-ordering of bits due to LSB byte order</b>
41
42      */
43     struct WLANPacketParser_MgtFrameParser : public senf::PacketParserBase
44     {
45     #   include SENF_FIXED_PARSER()
46         SENF_PARSER_PRIVATE_BITFIELD    ( subtype,  4,  unsigned);
47         SENF_PARSER_SKIP_BITS   ( 4                                    ); //skip type and version
48         //jump to fist address field
49         SENF_PARSER_SKIP                ( 3                                    );
50         SENF_PARSER_FIELD               ( da, MACAddressParser        );
51         SENF_PARSER_FIELD               ( sa, MACAddressParser        );
52         SENF_PARSER_FIELD               ( bssid, MACAddressParser        );
53
54         //workaround since bitfield LSB parsers are not available
55         SENF_PARSER_PRIVATE_BITFIELD    (seqNumber_1, 4, unsigned);
56         SENF_PARSER_BITFIELD    (fragmentNumber, 4, unsigned);
57         SENF_PARSER_PRIVATE_FIELD     (seqNumber_2, UInt8Parser);
58
59         SENF_PARSER_FINALIZE(WLANPacketParser_MgtFrameParser);
60
61         //this is needed due to the goto in the WLANPacketParser. Don't know exactly why yet.
62         SENF_PARSER_INIT() {}
63
64         boost::uint16_t sequenceNumber() const;
65
66     };
67
68     /** \brief Control frame parser
69      * <b>Re-ordering of bits due to LSB byte order</b>
70      *
71      * currently only CTS, RTS and ACK control frames are supported
72
73      */
74     struct WLANPacketParser_CtrlFrameParser : public senf::PacketParserBase
75     {
76     #   include SENF_PARSER()
77         SENF_PARSER_PRIVATE_BITFIELD    ( subtype,  4,  unsigned);
78         SENF_PARSER_SKIP_BITS   ( 4                                    ); //skip type and version
79         //jump to fist address field
80         SENF_PARSER_SKIP                ( 3,3                        );
81         SENF_PARSER_FIELD               ( ra, MACAddressParser         );
82
83         //only RTS frame contains a source address field
84         //variant is also needed so set to correct subtype value
85         SENF_PARSER_VARIANT ( subtype__,             subtype,
86                 ( ids (na, na, set_cts, key(12, VoidPacketParser)) )
87                 ( ids (na, na, set_ack, key(13, VoidPacketParser)) )
88                 ( ids (sa, has_sa, set_rts, key(11, MACAddressParser)) ) );
89
90         SENF_PARSER_FINALIZE(WLANPacketParser_CtrlFrameParser);
91
92         //this is needed to due to the goto in the WLANPacketParser. Don't know exactly why yet.
93         SENF_PARSER_INIT() {}
94     };
95
96     /** \brief Data frame parser
97      * <b>Re-ordering of bits due to LSB byte order</b>
98
99      */
100     struct WLANPacketParser_DataFrameParser : public senf::PacketParserBase
101     {
102     #   include SENF_PARSER()
103         SENF_PARSER_PRIVATE_BITFIELD    ( subtype,  4,  unsigned);
104         //jump to 'toDS' and 'fromDS' bits
105         //skip type and version
106         SENF_PARSER_SKIP_BITS   ( 4                                    );
107         //skip other flags
108         SENF_PARSER_SKIP_BITS   ( 6                                    );
109         //needed in data frames due to the variable address fields
110         SENF_PARSER_PRIVATE_BITFIELD    ( dsBits,    2,  unsigned);
111         //skip duration field
112         SENF_PARSER_SKIP        ( 2,0                          );
113
114         SENF_PARSER_PRIVATE_FIELD               ( addr1, MACAddressParser        );
115         SENF_PARSER_PRIVATE_FIELD               ( addr2, MACAddressParser        );
116         SENF_PARSER_PRIVATE_FIELD               ( addr3, MACAddressParser        );
117
118         //sequence Number and fragment number
119         //shift bits manually due to LSB
120         SENF_PARSER_PRIVATE_BITFIELD    (seqNumber_1, 4, unsigned);
121         SENF_PARSER_BITFIELD    (fragmentNumber, 4, unsigned);
122         SENF_PARSER_PRIVATE_FIELD     (seqNumber_2, UInt8Parser)
123
124         //TODO fourth address field in case of WDS
125 //        SENF_PARSER_PRIVATE_VARIANT (wds_, dsBits,
126 //                ( novalue ( disable_addr4,               VoidPacketParser                    ))
127 //                ( id      ( addr4,  key (3,                   MACAddressParser                     ))) );
128
129         //QoS Filed
130         SENF_PARSER_VARIANT (qosField_, subtype,
131                 ( ids (na, na, set_data, key(0, VoidPacketParser)))
132                 ( ids (na, na, set_nullData, key(4, VoidPacketParser)))
133                 ( ids (qosField, has_qosField, set_qosData, key(8, UInt16LSBParser)))
134                 //we cannot parse qos Null (data) frames at the moment
135                 ( ids (na, na, set_qosNullData, key(12, UInt16LSBParser))) );
136
137
138
139         SENF_PARSER_FINALIZE(WLANPacketParser_DataFrameParser);
140
141         //this is needed to due to the goto in the WLANPacketParser. Don't know exactly why yet.
142         SENF_PARSER_INIT() {}
143
144         boost::uint16_t sequenceNumber() const;
145
146         MACAddressParser ra() const { return addr1(); }; //ra is always addr1
147         MACAddressParser ta() const { return addr2(); }; //ta is always addr2
148         MACAddressParser sa() const;
149         MACAddressParser da() const;
150         MACAddressParser bssid() const;
151     };
152
153
154     /** \brief 802.11 Frame parser
155      * (see IEEE 802.11-2007 standard - Chapter 7 Frame formats)
156      * <b>Re-ordering of bits due to LSB byte order</b>
157
158      */
159     struct WLANPacketParser : public senf::PacketParserBase
160     {
161     #   include SENF_PARSER()
162
163         /*
164          * Frame control field
165          * re-ordering of fields due to the byte order
166          */
167         SENF_PARSER_BITFIELD_RO    ( subtype,  4,  unsigned);
168         SENF_PARSER_BITFIELD_RO    ( type,     2,  unsigned);
169         SENF_PARSER_BITFIELD    ( version,  2,  unsigned);
170         SENF_PARSER_BITFIELD    ( order,     1,  bool);
171         SENF_PARSER_BITFIELD    ( protectedFrame,    1,  bool);
172         SENF_PARSER_BITFIELD    ( moreData,  1,  bool);
173         SENF_PARSER_BITFIELD    ( pwrMgt,   1,  bool);
174         SENF_PARSER_BITFIELD    ( retry,     1,  bool);
175         SENF_PARSER_BITFIELD    ( moreFrag,  1,  bool);
176         SENF_PARSER_BITFIELD    ( fromDS,    1,  bool);
177         SENF_PARSER_BITFIELD    ( toDS,      1,  bool);
178
179         SENF_PARSER_FIELD   (duration,          UInt16LSBParser);
180
181         SENF_PARSER_GOTO( subtype ); //subparsers need to know the subtype
182         SENF_PARSER_VARIANT ( type__,             type,
183                                 ( id      ( mgtFrame,   WLANPacketParser_MgtFrameParser     ))
184                                 ( id      ( ctrlFrame,  WLANPacketParser_CtrlFrameParser    ))
185                                 ( id      ( dataFrame,  WLANPacketParser_DataFrameParser    ))
186                                 ( novalue ( reserved,   WLANPacketParser_CtrlFrameParser    )) );
187
188         SENF_PARSER_CUSTOM_FIELD( fcs, senf:: UInt32Parser, fcs_t::fixed_bytes, fcs_t::fixed_bytes) {
189           return parse<UInt32Parser>( data().size()-4 ); }
190
191         SENF_PARSER_FINALIZE(WLANPacketParser);
192
193         SENF_PARSER_INIT() {
194             version() = 0;
195
196
197         }
198
199         //Problems can occur with old madwifi and ath5k. Some frames only
200         //contains two byte FCS instead of four.
201 //        UInt32Parser fcs() const { return parse<UInt32Parser>( data().size()-4 ); }
202
203     };
204
205     struct WLANPacketType
206         : public senf::PacketTypeBase,
207         public senf::PacketTypeMixin<WLANPacketType>
208     {
209
210         typedef senf::PacketTypeMixin<WLANPacketType> mixin;
211         typedef senf::ConcretePacket<WLANPacketType> packet;
212         typedef WLANPacketParser parser;
213
214 //        using mixin::nextPacketRange;
215         using mixin::init;
216         using mixin::initSize;
217         using senf::PacketTypeBase::nextPacketRange;;
218
219
220         static void dump(packet p, std::ostream &os);
221 //        static PacketParserBase::size_type initSize();
222     };
223
224     typedef WLANPacketType::packet WLANPacket;
225
226 }
227
228
229
230 #endif /* WLANPACKET_HH_ */