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 );
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 );
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 );
56 SENF_PARSER_FINALIZE(WLANPacket_MgtFrameParser);
58 //this is needed due to the goto in the WLANPacketParser. Don't know exactly why yet.
61 boost::uint16_t sequenceNumber() const {
62 return (uint16_t)(seqNumber_2()) << 4 | seqNumber_1();
66 /** \brief Control frame parser
67 * <b>Re-ordering of bits due to LSB byte order</b>
69 * currently only CTS, RTS and ACK control frames are supported
72 struct WLANPacket_CtrlFrameParser : public senf::PacketParserBase
74 # include SENF_PARSER()
76 SENF_PARSER_PRIVATE_BITFIELD ( subtype, 4, unsigned );
77 SENF_PARSER_SKIP_BITS ( 4 ); //skip type and version
78 //jump to fist address field
79 SENF_PARSER_SKIP ( 3, 3 );
80 SENF_PARSER_FIELD ( receiverAddress, MACAddressParser );
82 //only RTS frame contains a source address field
83 //variant is also needed to set correct subtype value
84 SENF_PARSER_VARIANT ( subtype__, subtype,
85 ( ids( na, is_cts, set_cts, key(12, VoidPacketParser)) )
86 ( ids( na, is_ack, set_ack, key(13, VoidPacketParser)) )
87 ( ids( sourceAddress, is_rts, set_rts, key(11, MACAddressParser)) ) );
89 SENF_PARSER_FINALIZE(WLANPacket_CtrlFrameParser);
91 //this is needed to due to the goto in the WLANPacketParser. Don't know exactly why yet.
95 /** \brief Data frame parser
96 * <b>Re-ordering of bits due to LSB byte order</b>
99 struct WLANPacket_DataFrameParser : public senf::PacketParserBase
101 # include SENF_PARSER()
102 SENF_PARSER_PRIVATE_BITFIELD ( subtype, 4, unsigned );
103 //jump to 'toDS' and 'fromDS' bits
104 //skip type and version
105 SENF_PARSER_SKIP_BITS ( 4 );
107 SENF_PARSER_SKIP_BITS ( 6 );
108 //needed in data frames due to the variable address fields
109 SENF_PARSER_PRIVATE_BITFIELD ( dsBits, 2, unsigned );
110 //skip duration field
111 SENF_PARSER_SKIP ( 2, 0 );
113 SENF_PARSER_PRIVATE_FIELD ( addr1, MACAddressParser );
114 SENF_PARSER_PRIVATE_FIELD ( addr2, MACAddressParser );
115 SENF_PARSER_PRIVATE_FIELD ( addr3, MACAddressParser );
117 //sequence Number and fragment number
118 //shift bits manually due to LSB
119 SENF_PARSER_PRIVATE_BITFIELD ( seqNumber_1, 4, unsigned );
120 SENF_PARSER_BITFIELD ( fragmentNumber, 4, unsigned );
121 SENF_PARSER_PRIVATE_FIELD ( seqNumber_2, UInt8Parser );
123 //TODO fourth address field in case of WDS
124 // SENF_PARSER_PRIVATE_VARIANT (wds_, dsBits,
125 // ( novalue ( disable_addr4, VoidPacketParser ))
126 // ( id ( addr4, key (3, MACAddressParser ))) );
129 SENF_PARSER_VARIANT ( qosField_, subtype,
130 ( ids( na, na, set_data, key(0, VoidPacketParser)) )
131 ( ids( na, na, set_nullData, key(4, VoidPacketParser)) )
132 ( ids( qosField, has_qosField, set_qosData, key(8, UInt16LSBParser )) )
133 //we cannot parse qos Null (data) frames at the moment
134 ( ids( na, na, set_qosNullData, key(12, UInt16LSBParser)) ) );
136 SENF_PARSER_FINALIZE(WLANPacket_DataFrameParser);
138 //this is needed to due to the goto in the WLANPacketParser. Don't know exactly why yet.
139 SENF_PARSER_INIT() {}
141 boost::uint16_t sequenceNumber() const {
142 return (uint16_t)(seqNumber_2()) << 4 | seqNumber_1();
145 MACAddressParser receiverAddress() const { return addr1(); }; //ra is always addr1
146 MACAddressParser transmitterAddress() const { return addr2(); }; //ta is always addr2
147 MACAddressParser sourceAddress() const;
148 MACAddressParser destinationAddress() const;
149 MACAddressParser bssid() const;
153 /** \brief 802.11 Frame parser
154 * (see IEEE 802.11-2007 standard - Chapter 7 Frame formats)
155 * <b>Re-ordering of bits due to LSB byte order</b>
158 struct WLANPacketParser : public senf::PacketParserBase
160 # include SENF_PARSER()
163 * Frame control field
164 * re-ordering of fields due to the byte order
166 SENF_PARSER_BITFIELD_RO ( subtype, 4, unsigned );
167 SENF_PARSER_BITFIELD_RO ( type, 2, unsigned );
168 SENF_PARSER_BITFIELD ( version, 2, unsigned );
169 SENF_PARSER_BITFIELD ( order, 1, bool );
170 SENF_PARSER_BITFIELD ( protectedFrame, 1, bool );
171 SENF_PARSER_BITFIELD ( moreData, 1, bool );
172 SENF_PARSER_BITFIELD ( pwrMgt, 1, bool );
173 SENF_PARSER_BITFIELD ( retry, 1, bool );
174 SENF_PARSER_BITFIELD ( moreFrag, 1, bool );
175 SENF_PARSER_BITFIELD ( fromDS, 1, bool );
176 SENF_PARSER_BITFIELD ( toDS, 1, bool );
178 SENF_PARSER_FIELD ( duration, UInt16LSBParser );
180 SENF_PARSER_GOTO( subtype ); // subparsers need to know the subtype
181 SENF_PARSER_VARIANT ( frameType_, type,
182 ( ids( mgtFrame, is_mgtFrame, init_mgtFrame, WLANPacket_MgtFrameParser ))
183 ( ids( ctrlFrame, is_ctrlFrame, init_ctrlFrame, WLANPacket_CtrlFrameParser ))
184 ( ids( dataFrame, is_dataFrame, init_dataFrame, WLANPacket_DataFrameParser ))
185 ( novalue( reserved, WLANPacket_CtrlFrameParser )) );
187 SENF_PARSER_CUSTOM_FIELD( fcs, senf::UInt32Parser, fcs_t::fixed_bytes, fcs_t::fixed_bytes) {
188 return parse<UInt32Parser>( data().size()-4 ); }
190 SENF_PARSER_FINALIZE(WLANPacketParser);
196 //Problems can occur with old madwifi and ath5k. Some frames only
197 //contains two byte FCS instead of four.
198 // UInt32Parser fcs() const { return parse<UInt32Parser>( data().size()-4 ); }
202 /** \brief WLAN packet
204 \par Packet type (typedef):
208 \ref WLANPacketParser
210 \ingroup protocolbundle_80211
212 struct WLANPacketType
213 : public senf::PacketTypeBase,
214 public senf::PacketTypeMixin<WLANPacketType>
216 typedef senf::PacketTypeMixin<WLANPacketType> mixin;
217 typedef senf::ConcretePacket<WLANPacketType> packet;
218 typedef WLANPacketParser parser;
220 // using mixin::nextPacketRange;
222 using mixin::initSize;
223 using senf::PacketTypeBase::nextPacketRange;;
225 static void dump(packet p, std::ostream &os);
226 // static PacketParserBase::size_type initSize();
229 typedef senf::ConcretePacket<WLANPacketType> WLANPacket;
233 #endif /* HH_SENF_Packets_80211Bundle_WLANPacket_ */