blob: 89079b3b7bb3b2259eeb64d03b54e7d146073a39 [file] [log] [blame]
Wade Guthrie16196242012-11-20 15:53:52 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_NLATTRIBUTE_H_
6#define SHILL_NLATTRIBUTE_H_
7
8#include <linux/nl80211.h>
Wade Guthrie8343f7f2012-12-04 13:52:32 -08009#include <netlink/attr.h>
Wade Guthrie16196242012-11-20 15:53:52 -080010#include <netlink/netlink.h>
11
12#include <map>
13#include <string>
14
15#include <base/memory/scoped_ptr.h>
16
17#include "shill/byte_string.h"
18
19struct nlattr;
20
21namespace shill {
22
23// Nl80211Attribute is an abstract base class that describes an attribute in a
24// netlink-80211 message. Child classes are type-specific and will define
25// Get*Value and Set*Value methods (where * is the type). A second-level of
26// child classes exist for each individual attribute type.
27//
28// An attribute has a name (which is really an enumerated value), a data type,
29// and a value. In an nlattr (the underlying format for an attribute in a
30// message), the data is stored as a blob without type information; the writer
31// and reader of the attribute must agree on the data type.
32class Nl80211Attribute {
33 public:
34 enum Type {
35 kTypeU8,
36 kTypeU16,
37 kTypeU32,
38 kTypeU64,
39 kTypeString,
40 kTypeFlag,
41 kTypeMsecs,
42 kTypeNested,
43 kTypeRaw,
44 kTypeError
45 };
46
47 Nl80211Attribute(nl80211_attrs name, const char *name_string,
48 Type type, const char *type_string);
49 virtual ~Nl80211Attribute() {}
50
51 virtual bool InitFromNlAttr(const nlattr *data);
52
53 // Static factory generates the appropriate Nl80211Attribute object from the
54 // raw nlattr data.
55 static Nl80211Attribute *NewFromNlAttr(nl80211_attrs name,
56 const nlattr *data);
57
58 // Accessors for the attribute's name and type information.
59 nl80211_attrs name() const { return name_; }
60 virtual const char *name_string() const { return name_string_; }
61 Type type() const { return type_; }
62 std::string type_string() const { return type_string_; }
63
64 // TODO(wdg): Since |data| is used, externally, to support |nla_parse_nested|,
65 // make it protected once all functionality has been brought inside the
66 // Nl80211Attribute classes.
67 //
68 // |data_| contains an 'nlattr *' but it's been stored as a ByteString.
69 // This returns a pointer to the data in the form that is intended.
70 const nlattr *data() const {
71 return reinterpret_cast<const nlattr *>(data_.GetConstData());
72 }
73
74 // TODO(wdg): GetRawData is used to support
75 // UserBoundNlMessage::GetRawAttributeData which, in turn, is used to support
76 // NL80211_ATTR_FRAME and NL80211_ATTR_KEY_SEQ. Remove this method (and that
77 // one) once those classes are fleshed-out.
78 //
79 // If successful, returns 'true' and sets *|value| to the raw attribute data
80 // (after the header) for this attribute. Otherwise, returns 'false' and
81 // leaves |value| unchanged.
Wade Guthrie8343f7f2012-12-04 13:52:32 -080082 bool GetRawData(ByteString *output) const;
Wade Guthrie16196242012-11-20 15:53:52 -080083
84 // Fill a string with characters that represents the value of the attribute.
85 // If no attribute is found or if the type isn't trivially stringizable,
86 // this method returns 'false' and |value| remains unchanged.
87 virtual bool AsString(std::string *value) const = 0;
88
89 // Writes the raw attribute data to a string. For debug.
90 std::string RawToString() const;
91
Wade Guthrie8343f7f2012-12-04 13:52:32 -080092 // Note that |nla_get_*| don't change their arguments but don't declare
93 // themselves as 'const', either. These methods wrap the const castness.
94 static char *NlaGetString(const nlattr *input) {
95 return nla_get_string(const_cast<nlattr *>(input));
96 }
97 static uint8_t NlaGetU8(const nlattr *input) {
98 return nla_get_u8(const_cast<nlattr *>(input));
99 }
100 static uint16_t NlaGetU16(const nlattr *input) {
101 return nla_get_u16(const_cast<nlattr *>(input));
102 }
103 static uint32_t NlaGetU32(const nlattr *input) {
104 return nla_get_u32(const_cast<nlattr *>(input));
105 }
106 static uint64_t NlaGetU64(const nlattr *input) {
107 return nla_get_u64(const_cast<nlattr *>(input));
108 }
109
Wade Guthrie16196242012-11-20 15:53:52 -0800110 protected:
111 // Raw data corresponding to the value in any of the child classes.
112 // TODO(wdg): When 'data()' is removed, move this to the Nl80211RawAttribute
113 // class.
114 ByteString data_;
115
116 private:
117 nl80211_attrs name_;
118 const char *name_string_;
119 Type type_;
120 const char *type_string_;
121};
122
Wade Guthrie25cdb382012-12-04 14:04:05 -0800123// U8.
Wade Guthrie16196242012-11-20 15:53:52 -0800124
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800125class Nl80211U8Attribute : public Nl80211Attribute {
126 public:
127 static const char kMyTypeString[];
128 static const Type kType;
129 Nl80211U8Attribute(nl80211_attrs name, const char *name_string)
130 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
131 bool InitFromNlAttr(const nlattr *data);
132 bool GetU8Value(uint8_t *value) const;
133 bool SetU8Value(uint8_t new_value);
134 bool AsString(std::string *value) const;
135
136 private:
137 uint8_t value_;
138};
139
Wade Guthrie25cdb382012-12-04 14:04:05 -0800140class Nl80211AttributeKeyIdx : public Nl80211U8Attribute {
141 public:
142 static const nl80211_attrs kName;
143 static const char kNameString[];
144 Nl80211AttributeKeyIdx() : Nl80211U8Attribute(kName, kNameString) {}
145};
146
147class Nl80211AttributeRegType : public Nl80211U8Attribute {
148 public:
149 static const nl80211_attrs kName;
150 static const char kNameString[];
151 Nl80211AttributeRegType() : Nl80211U8Attribute(kName, kNameString) {}
152};
153
154// U16.
155
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800156class Nl80211U16Attribute : public Nl80211Attribute {
157 public:
158 static const char kMyTypeString[];
159 static const Type kType;
160 Nl80211U16Attribute(nl80211_attrs name, const char *name_string)
161 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
162 bool InitFromNlAttr(const nlattr *data);
163 bool GetU16Value(uint16_t *value) const;
164 bool SetU16Value(uint16_t new_value);
165 bool AsString(std::string *value) const;
166
167 private:
168 uint16_t value_;
169};
170
Wade Guthrie25cdb382012-12-04 14:04:05 -0800171class Nl80211AttributeReasonCode : public Nl80211U16Attribute {
172 public:
173 static const nl80211_attrs kName;
174 static const char kNameString[];
175 Nl80211AttributeReasonCode() : Nl80211U16Attribute(kName, kNameString) {}
176};
177
178class Nl80211AttributeStatusCode : public Nl80211U16Attribute {
179 public:
180 static const nl80211_attrs kName;
181 static const char kNameString[];
182 Nl80211AttributeStatusCode() : Nl80211U16Attribute(kName, kNameString) {}
183};
184
185// U32.
186
Wade Guthrie16196242012-11-20 15:53:52 -0800187class Nl80211U32Attribute : public Nl80211Attribute {
188 public:
189 static const char kMyTypeString[];
190 static const Type kType;
191 Nl80211U32Attribute(nl80211_attrs name, const char *name_string)
192 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
193 bool InitFromNlAttr(const nlattr *data);
194 bool GetU32Value(uint32_t *value) const;
195 bool SetU32Value(uint32_t new_value);
196 bool AsString(std::string *value) const;
197
198 private:
199 uint32_t value_;
200};
201
Wade Guthrie25cdb382012-12-04 14:04:05 -0800202class Nl80211AttributeDuration : public Nl80211U32Attribute {
203 public:
204 static const nl80211_attrs kName;
205 static const char kNameString[];
206 Nl80211AttributeDuration() : Nl80211U32Attribute(kName, kNameString) {}
207};
208
209class Nl80211AttributeGeneration : public Nl80211U32Attribute {
210 public:
211 static const nl80211_attrs kName;
212 static const char kNameString[];
213 Nl80211AttributeGeneration() : Nl80211U32Attribute(kName, kNameString) {}
214};
215
216class Nl80211AttributeIfindex : public Nl80211U32Attribute {
217 public:
218 static const nl80211_attrs kName;
219 static const char kNameString[];
220 Nl80211AttributeIfindex() : Nl80211U32Attribute(kName, kNameString) {}
221};
222
223class Nl80211AttributeKeyType : public Nl80211U32Attribute {
224 public:
225 static const nl80211_attrs kName;
226 static const char kNameString[];
227 Nl80211AttributeKeyType() : Nl80211U32Attribute(kName, kNameString) {}
228};
229
230class Nl80211AttributeRegInitiator : public Nl80211U32Attribute {
231 public:
232 static const nl80211_attrs kName;
233 static const char kNameString[];
234 Nl80211AttributeRegInitiator() : Nl80211U32Attribute(kName, kNameString) {}
235};
236
237class Nl80211AttributeWiphy : public Nl80211U32Attribute {
238 public:
239 static const nl80211_attrs kName;
240 static const char kNameString[];
241 Nl80211AttributeWiphy() : Nl80211U32Attribute(kName, kNameString) {}
242};
243
244class Nl80211AttributeWiphyFreq : public Nl80211U32Attribute {
245 public:
246 static const nl80211_attrs kName;
247 static const char kNameString[];
248 Nl80211AttributeWiphyFreq() : Nl80211U32Attribute(kName, kNameString) {}
249};
250
251// U64.
252
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800253class Nl80211U64Attribute : public Nl80211Attribute {
254 public:
255 static const char kMyTypeString[];
256 static const Type kType;
257 Nl80211U64Attribute(nl80211_attrs name, const char *name_string)
258 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
259 bool InitFromNlAttr(const nlattr *data);
260 bool GetU64Value(uint64_t *value) const;
261 bool SetU64Value(uint64_t new_value);
262 bool AsString(std::string *value) const;
263
264 private:
265 uint64_t value_;
266};
267
Wade Guthrie25cdb382012-12-04 14:04:05 -0800268class Nl80211AttributeCookie : public Nl80211U64Attribute {
269 public:
270 static const nl80211_attrs kName;
271 static const char kNameString[];
272 Nl80211AttributeCookie() : Nl80211U64Attribute(kName, kNameString) {}
273};
274
275// Flag.
276
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800277class Nl80211FlagAttribute : public Nl80211Attribute {
278 public:
279 static const char kMyTypeString[];
280 static const Type kType;
281 Nl80211FlagAttribute(nl80211_attrs name, const char *name_string)
282 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
283 bool InitFromNlAttr(const nlattr *data);
284 bool GetFlagValue(bool *value) const;
285 bool SetFlagValue(bool new_value);
286 bool AsString(std::string *value) const;
287
288 private:
289 bool value_;
290};
291
Wade Guthrie25cdb382012-12-04 14:04:05 -0800292class Nl80211AttributeDisconnectedByAp : public Nl80211FlagAttribute {
293 public:
294 static const nl80211_attrs kName;
295 static const char kNameString[];
296 Nl80211AttributeDisconnectedByAp() :
297 Nl80211FlagAttribute(kName, kNameString) {}
298};
299
300class Nl80211AttributeSupportMeshAuth : public Nl80211FlagAttribute {
301 public:
302 static const nl80211_attrs kName;
303 static const char kNameString[];
304 Nl80211AttributeSupportMeshAuth() :
305 Nl80211FlagAttribute(kName, kNameString) {}
306};
307
308class Nl80211AttributeTimedOut : public Nl80211FlagAttribute {
309 public:
310 static const nl80211_attrs kName;
311 static const char kNameString[];
312 Nl80211AttributeTimedOut() : Nl80211FlagAttribute(kName, kNameString) {}
313};
314
315// String.
316
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800317class Nl80211StringAttribute : public Nl80211Attribute {
318 public:
319 static const char kMyTypeString[];
320 static const Type kType;
321 Nl80211StringAttribute(nl80211_attrs name, const char *name_string)
322 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
323 bool InitFromNlAttr(const nlattr *data);
324 bool GetStringValue(std::string *value) const;
325 bool SetStringValue(const std::string new_value);
326 bool AsString(std::string *value) const;
327
328 private:
329 std::string value_;
330};
Wade Guthrie16196242012-11-20 15:53:52 -0800331
Wade Guthrie25cdb382012-12-04 14:04:05 -0800332class Nl80211AttributeRegAlpha2 : public Nl80211StringAttribute {
333 public:
334 static const nl80211_attrs kName;
335 static const char kNameString[];
336 Nl80211AttributeRegAlpha2() : Nl80211StringAttribute(kName, kNameString) {}
337};
338
339class Nl80211AttributeWiphyName : public Nl80211StringAttribute {
340 public:
341 static const nl80211_attrs kName;
342 static const char kNameString[];
343 Nl80211AttributeWiphyName() : Nl80211StringAttribute(kName, kNameString) {}
344};
345
346// Raw.
347
Wade Guthrie16196242012-11-20 15:53:52 -0800348class Nl80211RawAttribute : public Nl80211Attribute {
349 public:
350 static const char kMyTypeString[];
351 static const Type kType;
352 Nl80211RawAttribute(nl80211_attrs name, const char *name_string)
353 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
354 bool InitFromNlAttr(const nlattr *data);
Wade Guthrie25cdb382012-12-04 14:04:05 -0800355 bool GetRawValue(ByteString *value) const;
Wade Guthrie16196242012-11-20 15:53:52 -0800356 // Not supporting 'set' for raw data. This type is a "don't know" type to
357 // be used for user-bound massages (via InitFromNlAttr). The 'set' method
358 // is intended for building kernel-bound messages and shouldn't be used with
359 // raw data.
360 bool AsString(std::string *value) const;
361};
362
Wade Guthrie25cdb382012-12-04 14:04:05 -0800363// TODO(wdg): This should inherit from Nl80211NestedAttribute when that class
364// exists.
365class Nl80211AttributeCqm : public Nl80211RawAttribute {
Wade Guthrie16196242012-11-20 15:53:52 -0800366 public:
367 static const nl80211_attrs kName;
368 static const char kNameString[];
Wade Guthrie25cdb382012-12-04 14:04:05 -0800369 Nl80211AttributeCqm() : Nl80211RawAttribute(kName, kNameString) {}
370};
371
372class Nl80211AttributeFrame : public Nl80211RawAttribute {
373 public:
374 static const nl80211_attrs kName;
375 static const char kNameString[];
376 Nl80211AttributeFrame() : Nl80211RawAttribute(kName, kNameString) {}
Wade Guthrie16196242012-11-20 15:53:52 -0800377};
378
379class Nl80211AttributeGeneric : public Nl80211RawAttribute {
380 public:
381 explicit Nl80211AttributeGeneric(nl80211_attrs name);
382 const char *name_string() const;
383
384 private:
385 std::string name_string_;
386};
387
Wade Guthrie25cdb382012-12-04 14:04:05 -0800388class Nl80211AttributeKeySeq : public Nl80211RawAttribute {
389 public:
390 static const nl80211_attrs kName;
391 static const char kNameString[];
392 Nl80211AttributeKeySeq() : Nl80211RawAttribute(kName, kNameString) {}
393};
394
395class Nl80211AttributeMac : public Nl80211RawAttribute {
396 public:
397 static const nl80211_attrs kName;
398 static const char kNameString[];
399 Nl80211AttributeMac() : Nl80211RawAttribute(kName, kNameString) {}
400};
401
402class Nl80211AttributeRespIe : public Nl80211RawAttribute {
403 public:
404 static const nl80211_attrs kName;
405 static const char kNameString[];
406 Nl80211AttributeRespIe() : Nl80211RawAttribute(kName, kNameString) {}
407};
408
409class Nl80211AttributeScanFrequencies : public Nl80211RawAttribute {
410 public:
411 static const nl80211_attrs kName;
412 static const char kNameString[];
413 Nl80211AttributeScanFrequencies() : Nl80211RawAttribute(kName, kNameString) {}
414};
415
416class Nl80211AttributeScanSsids : public Nl80211RawAttribute {
417 public:
418 static const nl80211_attrs kName;
419 static const char kNameString[];
420 Nl80211AttributeScanSsids() : Nl80211RawAttribute(kName, kNameString) {}
421};
422
423class Nl80211AttributeStaInfo : public Nl80211RawAttribute {
424 public:
425 static const nl80211_attrs kName;
426 static const char kNameString[];
427 Nl80211AttributeStaInfo() : Nl80211RawAttribute(kName, kNameString) {}
428};
429
Wade Guthrie16196242012-11-20 15:53:52 -0800430} // namespace shill
431
432#endif // SHILL_NLATTRIBUTE_H_