Packets/80211Bundle: integrated GenericTLVRegistry
[senf.git] / senf / 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 <senf/Packets/Packets.hh>
30 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
31 #include <senf/Packets/DefaultBundle/LlcSnapPacket.hh>
32
33 ///////////////////////////////hh.p////////////////////////////////////////
34
35 namespace senf
36 {
37
38     /** \brief 802.11 Frame parser
39         (see IEEE 802.11-2007 standard - Chapter 7 Frame formats)
40         <b>Re-ordering of bits due to host byte order</b>
41
42         Frame base class.
43      */
44     struct WLANPacketParser : public PacketParserBase
45     {
46     #   include SENF_PARSER()
47
48         /*
49          * Frame control field
50          * re-ordering of fields due to the byte order
51          */
52         SENF_PARSER_BITFIELD_RO ( subtype,        4,  unsigned );
53         SENF_PARSER_BITFIELD_RO ( type,           2,  unsigned );
54         SENF_PARSER_BITFIELD    ( version,        2,  unsigned );
55         SENF_PARSER_BITFIELD    ( order,          1,  bool     );
56         SENF_PARSER_BITFIELD    ( protectedFrame, 1,  bool     );
57         SENF_PARSER_BITFIELD    ( moreData,       1,  bool     );
58         SENF_PARSER_BITFIELD    ( pwrMgt,         1,  bool     );
59         SENF_PARSER_BITFIELD    ( retry,          1,  bool     );
60         SENF_PARSER_BITFIELD    ( moreFrag,       1,  bool     );
61         SENF_PARSER_BITFIELD    ( fromDS,         1,  bool     );
62         SENF_PARSER_BITFIELD    ( toDS,           1,  bool     );
63
64         SENF_PARSER_FIELD       ( duration,       UInt16LSBParser );
65
66         SENF_PARSER_FINALIZE(WLANPacketParser);
67     };
68
69     ///////////////////////////////////////////////////////////////////////////
70
71     /** \brief Management frame parser
72         <b>Re-ordering of bits due to LSB byte order</b>
73      */
74     struct WLANPacket_MgtFrameParser : public WLANPacketParser
75     {
76     #   include SENF_PARSER()
77
78         SENF_PARSER_INHERIT(WLANPacketParser);
79
80         SENF_PARSER_FIELD            ( destinationAddress, MACAddressParser );
81         SENF_PARSER_FIELD            ( sourceAddress,      MACAddressParser );
82         SENF_PARSER_FIELD            ( bssid,              MACAddressParser );
83
84         //workaround since bitfield LSB parsers are not available
85         SENF_PARSER_PRIVATE_BITFIELD ( seqNumber_1,    4, unsigned );
86         SENF_PARSER_BITFIELD         ( fragmentNumber, 4, unsigned );
87         SENF_PARSER_PRIVATE_FIELD    ( seqNumber_2,    UInt8Parser );
88
89         SENF_PARSER_INIT() { type_() = 0; }
90
91         SENF_PARSER_FINALIZE(WLANPacket_MgtFrameParser);
92
93         boost::uint16_t sequenceNumber() const {
94             return (uint16_t)(seqNumber_2()) << 4 | seqNumber_1();
95         };
96     };
97
98     /** \brief WLAN Management frame packet
99
100         \par Packet type (typedef):
101             \ref WLANPacket_MgtFrame
102
103         \par Fields:
104             \ref WLANPacket_MgtFrameParser
105             \image html WLANPacket_MgtFrame.png
106
107         \ingroup protocolbundle_80211
108      */
109     struct WLANPacket_MgtFrameType
110         : public PacketTypeBase,
111           public PacketTypeMixin<WLANPacket_MgtFrameType>
112     {
113         typedef PacketTypeMixin<WLANPacket_MgtFrameType> mixin;
114         typedef ConcretePacket<WLANPacket_MgtFrameType> packet;
115         typedef WLANPacket_MgtFrameParser parser;
116
117         using mixin::init;
118         using mixin::initSize;
119         using PacketTypeBase::nextPacketRange;
120
121         static void dump(packet p, std::ostream &os);
122     };
123
124     /** \brief WLAN Management frame packet typedef
125         \ingroup protocolbundle_80211
126      */
127     typedef WLANPacket_MgtFrameType::packet WLANPacket_MgtFrame;
128
129     ///////////////////////////////////////////////////////////////////////////
130
131     /** \brief Control frame parser
132         <b>Re-ordering of bits due to LSB byte order</b>
133
134         currently only CTS, RTS and ACK control frames are supported
135      */
136     struct WLANPacket_CtrlFrameParser : public WLANPacketParser
137     {
138     #   include SENF_PARSER()
139
140         SENF_PARSER_INHERIT(WLANPacketParser);
141
142         SENF_PARSER_FIELD   ( receiverAddress, MACAddressParser );
143
144         //only RTS frame contains a source address field
145         //variant is also needed to set correct subtype value
146         SENF_PARSER_VARIANT ( subtype__, subtype,
147                 ( ids( na,            is_cts, set_cts, key(12, VoidPacketParser)) )
148                 ( ids( na,            is_ack, set_ack, key(13, VoidPacketParser)) )
149                 ( ids( sourceAddress, is_rts, set_rts, key(11, MACAddressParser)) ) );
150
151         SENF_PARSER_INIT() { type_() = 1; }
152
153         SENF_PARSER_FINALIZE(WLANPacket_CtrlFrameParser);
154     };
155
156     /** \brief WLAN Control frame packet
157
158         \par Packet type (typedef):
159             \ref WLANPacket_CtrlFrame
160
161         \par Fields:
162             \ref WLANPacket_CtrlFrameParser
163             \image html WLANPacket_CtrlFrame.png
164
165         \ingroup protocolbundle_80211
166      */
167     struct WLANPacket_CtrlFrameType
168         : public PacketTypeBase,
169           public PacketTypeMixin<WLANPacket_CtrlFrameType>
170     {
171         typedef PacketTypeMixin<WLANPacket_CtrlFrameType> mixin;
172         typedef ConcretePacket<WLANPacket_CtrlFrameType> packet;
173         typedef WLANPacket_CtrlFrameParser parser;
174
175         using mixin::init;
176         using mixin::initSize;
177         using PacketTypeBase::nextPacketRange;
178
179         static void dump(packet p, std::ostream &os);
180     };
181
182     /** \brief WLAN Control frame packet typedef
183         \ingroup protocolbundle_80211
184      */
185     typedef WLANPacket_CtrlFrameType::packet WLANPacket_CtrlFrame;
186
187     ///////////////////////////////////////////////////////////////////////////
188
189     /** \brief Data frame parser
190         <b>Re-ordering of bits due to LSB byte order</b>
191      */
192     struct WLANPacket_DataFrameParser : public WLANPacketParser
193     {
194     #   include SENF_PARSER()
195
196         SENF_PARSER_INHERIT(WLANPacketParser);
197
198         SENF_PARSER_GOTO(subtype);
199         SENF_PARSER_SKIP_BITS(14);                                //<pkgdraw: hide
200         SENF_PARSER_PRIVATE_BITFIELD ( dsBits,  2,  unsigned   ); //<pkgdraw: hide
201         SENF_PARSER_SKIP             ( 2, 2                    ); //<pkgdraw: hide
202
203         SENF_PARSER_PRIVATE_FIELD    ( addr1, MACAddressParser );
204         SENF_PARSER_PRIVATE_FIELD    ( addr2, MACAddressParser );
205         SENF_PARSER_PRIVATE_FIELD    ( addr3, MACAddressParser );
206
207         //sequence Number and fragment number
208         //shift bits manually due to LSB
209         SENF_PARSER_PRIVATE_BITFIELD ( seqNumber_1,    4, unsigned );
210         SENF_PARSER_BITFIELD         ( fragmentNumber, 4, unsigned );
211         SENF_PARSER_PRIVATE_FIELD    ( seqNumber_2,    UInt8Parser );
212
213         boost::uint16_t sequenceNumber() const {
214             return (uint16_t)(seqNumber_2()) << 4 | seqNumber_1();
215         };
216
217         // TODO fourth address field in case of WDS
218         // SENF_PARSER_PRIVATE_VARIANT (wds_, dsBits,
219         //     ( novalue ( disable_addr4,               VoidPacketParser ))
220         //     ( id      ( addr4,  key (3,              MACAddressParser ))) );
221
222         //QoS Field
223         SENF_PARSER_VARIANT ( qosField_, subtype,
224                 ( ids( na,       na,           set_data,        key(0, VoidPacketParser)) )
225                 ( ids( na,       na,           set_nullData,    key(4, VoidPacketParser)) )
226                 ( ids( qosField, has_qosField, set_qosData,     key(8, UInt16LSBParser )) )
227                 //we cannot parse qos Null (data) frames at the moment
228                 ( ids( na,       na,           set_qosNullData, key(12, UInt16LSBParser)) ) );
229
230         SENF_PARSER_INIT() { type_() = 2; }
231
232         SENF_PARSER_FINALIZE(WLANPacket_DataFrameParser);
233
234         MACAddressParser receiverAddress() const    { return addr1(); }; //ra is always addr1
235         MACAddressParser transmitterAddress() const { return addr2(); }; //ta is always addr2
236         MACAddressParser sourceAddress() const;
237         MACAddressParser destinationAddress() const;
238         MACAddressParser bssid() const;
239
240         friend class WLANPacket_DataFrameType;
241     };
242
243     /** \brief WLAN Data frame packet
244
245         \par Packet type (typedef):
246             \ref WLANPacket_DataFrame
247
248         \par Fields:
249             \ref WLANPacket_DataFrameParser
250             \image html WLANPacket_DataFrame.png
251
252         \ingroup protocolbundle_80211
253      */
254     struct WLANPacket_DataFrameType
255         : public PacketTypeBase,
256           public PacketTypeMixin<WLANPacket_DataFrameType>
257     {
258         typedef PacketTypeMixin<WLANPacket_DataFrameType> mixin;
259         typedef ConcretePacket<WLANPacket_DataFrameType> packet;
260         typedef WLANPacket_DataFrameParser parser;
261
262         using mixin::init;
263         using mixin::initSize;
264         using mixin::nextPacketRange;
265
266         static factory_t nextPacketType(packet p) { 
267             return p->subtype() == 0 || p->subtype() == 8 
268                 ? LlcSnapPacket::factory() 
269                 : no_factory();
270         }
271
272         static void dump(packet p, std::ostream &os);
273     };
274
275     /** \brief WLAN Data frame packet typedef
276         \ingroup protocolbundle_80211
277      */
278     typedef WLANPacket_DataFrameType::packet WLANPacket_DataFrame;
279 }
280
281 ///////////////////////////////hh.e////////////////////////////////////////
282 //#include "WLANPacket.cci"
283 //#include "WLANPacket.ct"
284 //#include "WLANPacket.cti"
285 #endif
286
287 \f
288 // Local Variables:
289 // mode: c++
290 // fill-column: 100
291 // c-file-style: "senf"
292 // indent-tabs-mode: nil
293 // ispell-local-dictionary: "american"
294 // compile-command: "scons -u test"
295 // comment-column: 40
296 // End: