4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Christian Niephaus <cni@berlios.de>
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.
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.
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.
24 \brief 802.11 WLANPacket public header */
26 #ifndef HH_SENF_Packets_80211Bundle_WLANPacket_
27 #define HH_SENF_Packets_80211Bundle_WLANPacket_ 1
29 #include "../../Packets/Packets.hh"
30 #include "../DefaultBundle/EthernetPacket.hh"
35 /** \brief Management frame parser
36 * <b>Re-ordering of bits due to LSB byte order</b>
39 struct WLANPacket_MgtFrameParser : public senf::PacketParserBase
41 # include SENF_FIXED_PARSER()
43 SENF_PARSER_PRIVATE_BITFIELD ( subtype, 4, unsigned ); //<pkgdraw: hide
44 //skip type and version
45 SENF_PARSER_SKIP_BITS ( 4 ); //<pkgdraw: hide
46 //jump to fist address field
47 SENF_PARSER_SKIP ( 3 ); //<pkgdraw: hide
48 SENF_PARSER_FIELD ( destinationAddress, MACAddressParser );
49 SENF_PARSER_FIELD ( sourceAddress, MACAddressParser );
50 SENF_PARSER_FIELD ( bssid, MACAddressParser );
52 //workaround since bitfield LSB parsers are not available
53 SENF_PARSER_PRIVATE_BITFIELD ( seqNumber_1, 4, unsigned );
54 SENF_PARSER_BITFIELD ( fragmentNumber, 4, unsigned );
55 SENF_PARSER_PRIVATE_FIELD ( seqNumber_2, UInt8Parser );
57 SENF_PARSER_FINALIZE(WLANPacket_MgtFrameParser);
59 //this is needed due to the goto in the WLANPacketParser. Don't know exactly why yet.
62 boost::uint16_t sequenceNumber() const {
63 return (uint16_t)(seqNumber_2()) << 4 | seqNumber_1();
67 /** \brief Control frame parser
68 * <b>Re-ordering of bits due to LSB byte order</b>
70 * currently only CTS, RTS and ACK control frames are supported
73 struct WLANPacket_CtrlFrameParser : public senf::PacketParserBase
75 # include SENF_PARSER()
77 SENF_PARSER_PRIVATE_BITFIELD ( subtype, 4, unsigned ); //<pkgdraw: hide
78 //skip type and version
79 SENF_PARSER_SKIP_BITS ( 4 ); //<pkgdraw: hide
80 //jump to fist address field
81 SENF_PARSER_SKIP ( 3, 3 ); //<pkgdraw: hide
82 SENF_PARSER_FIELD ( receiverAddress, MACAddressParser );
84 //only RTS frame contains a source address field
85 //variant is also needed to set correct subtype value
86 SENF_PARSER_VARIANT ( subtype__, subtype,
87 ( ids( na, is_cts, set_cts, key(12, VoidPacketParser)) )
88 ( ids( na, is_ack, set_ack, key(13, VoidPacketParser)) )
89 ( ids( sourceAddress, is_rts, set_rts, key(11, MACAddressParser)) ) );
91 SENF_PARSER_FINALIZE(WLANPacket_CtrlFrameParser);
93 //this is needed to due to the goto in the WLANPacketParser. Don't know exactly why yet.
97 /** \brief Data frame parser
98 * <b>Re-ordering of bits due to LSB byte order</b>
101 struct WLANPacket_DataFrameParser : public senf::PacketParserBase
103 # include SENF_PARSER()
104 SENF_PARSER_PRIVATE_BITFIELD ( subtype, 4, unsigned ); //<pkgdraw: hide
105 //jump to 'toDS' and 'fromDS' bits
106 //skip type and version
107 SENF_PARSER_SKIP_BITS ( 4 ); //<pkgdraw: hide
109 SENF_PARSER_SKIP_BITS ( 6 ); //<pkgdraw: hide
110 //needed in data frames due to the variable address fields
111 SENF_PARSER_PRIVATE_BITFIELD ( dsBits, 2, unsigned ); //<pkgdraw: hide
112 //skip duration field
113 SENF_PARSER_SKIP ( 2, 2 ); //<pkgdraw: hide
115 SENF_PARSER_PRIVATE_FIELD ( addr1, MACAddressParser );
116 SENF_PARSER_PRIVATE_FIELD ( addr2, MACAddressParser );
117 SENF_PARSER_PRIVATE_FIELD ( addr3, MACAddressParser );
119 //sequence Number and fragment number
120 //shift bits manually due to LSB
121 SENF_PARSER_PRIVATE_BITFIELD ( seqNumber_1, 4, unsigned );
122 SENF_PARSER_BITFIELD ( fragmentNumber, 4, unsigned );
123 SENF_PARSER_PRIVATE_FIELD ( seqNumber_2, UInt8Parser );
125 //TODO fourth address field in case of WDS
126 // SENF_PARSER_PRIVATE_VARIANT (wds_, dsBits,
127 // ( novalue ( disable_addr4, VoidPacketParser ))
128 // ( id ( addr4, key (3, MACAddressParser ))) );
131 SENF_PARSER_VARIANT ( qosField_, subtype,
132 ( ids( na, na, set_data, key(0, VoidPacketParser)) )
133 ( ids( na, na, set_nullData, key(4, VoidPacketParser)) )
134 ( ids( qosField, has_qosField, set_qosData, key(8, UInt16LSBParser )) )
135 //we cannot parse qos Null (data) frames at the moment
136 ( ids( na, na, set_qosNullData, key(12, UInt16LSBParser)) ) );
138 SENF_PARSER_FINALIZE(WLANPacket_DataFrameParser);
140 //this is needed to due to the goto in the WLANPacketParser. Don't know exactly why yet.
141 SENF_PARSER_INIT() {}
143 boost::uint16_t sequenceNumber() const {
144 return (uint16_t)(seqNumber_2()) << 4 | seqNumber_1();
147 MACAddressParser receiverAddress() const { return addr1(); }; //ra is always addr1
148 MACAddressParser transmitterAddress() const { return addr2(); }; //ta is always addr2
149 MACAddressParser sourceAddress() const;
150 MACAddressParser destinationAddress() const;
151 MACAddressParser bssid() const;
155 /** \brief 802.11 Frame parser
156 * (see IEEE 802.11-2007 standard - Chapter 7 Frame formats)
157 * <b>Re-ordering of bits due to LSB byte order</b>
160 struct WLANPacketParser : public senf::PacketParserBase
162 # include SENF_PARSER()
165 * Frame control field
166 * re-ordering of fields due to the byte order
168 SENF_PARSER_BITFIELD_RO ( subtype, 4, unsigned );
169 SENF_PARSER_BITFIELD_RO ( type, 2, unsigned );
170 SENF_PARSER_BITFIELD ( version, 2, unsigned );
171 SENF_PARSER_BITFIELD ( order, 1, bool );
172 SENF_PARSER_BITFIELD ( protectedFrame, 1, bool );
173 SENF_PARSER_BITFIELD ( moreData, 1, bool );
174 SENF_PARSER_BITFIELD ( pwrMgt, 1, bool );
175 SENF_PARSER_BITFIELD ( retry, 1, bool );
176 SENF_PARSER_BITFIELD ( moreFrag, 1, bool );
177 SENF_PARSER_BITFIELD ( fromDS, 1, bool );
178 SENF_PARSER_BITFIELD ( toDS, 1, bool );
180 SENF_PARSER_FIELD ( duration, UInt16LSBParser );
182 SENF_PARSER_GOTO( subtype ); // subparsers need to know the subtype
183 SENF_PARSER_VARIANT ( frameType_, type,
184 ( ids( mgtFrame, is_mgtFrame, init_mgtFrame, WLANPacket_MgtFrameParser ))
185 ( ids( ctrlFrame, is_ctrlFrame, init_ctrlFrame, WLANPacket_CtrlFrameParser ))
186 ( ids( dataFrame, is_dataFrame, init_dataFrame, WLANPacket_DataFrameParser ))
187 ( novalue( reserved, WLANPacket_CtrlFrameParser )) );
189 SENF_PARSER_CUSTOM_FIELD( fcs, senf::UInt32Parser, fcs_t::fixed_bytes, fcs_t::fixed_bytes) {
190 return parse<UInt32Parser>( data().size()-4 ); }
192 SENF_PARSER_FINALIZE(WLANPacketParser);
198 //Problems can occur with old madwifi and ath5k. Some frames only
199 //contains two byte FCS instead of four.
200 // UInt32Parser fcs() const { return parse<UInt32Parser>( data().size()-4 ); }
204 /** \brief WLAN packet
206 \par Packet type (typedef):
210 \ref WLANPacketParser
211 \image html WLANPacket.png
213 \ingroup protocolbundle_80211
215 struct WLANPacketType
216 : public senf::PacketTypeBase,
217 public senf::PacketTypeMixin<WLANPacketType>
219 typedef senf::PacketTypeMixin<WLANPacketType> mixin;
220 typedef senf::ConcretePacket<WLANPacketType> packet;
221 typedef WLANPacketParser parser;
225 using mixin::initSize;
226 // using senf::PacketTypeBase::nextPacketRange;
227 static optional_range nextPacketRange(packet p);
228 static factory_t nextPacketType(packet p);
230 static void dump(packet p, std::ostream &os);
231 // static PacketParserBase::size_type initSize();
234 typedef senf::ConcretePacket<WLANPacketType> WLANPacket;
238 #endif /* HH_SENF_Packets_80211Bundle_WLANPacket_ */