blob: dc774913d7df0a207f62233324064ab5a1fcc29c [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
repo sync1538d442012-12-20 15:24:35 -080015#include "shill/attribute_list.h"
Wade Guthrie16196242012-11-20 15:53:52 -080016#include "shill/byte_string.h"
repo sync12cca802012-12-19 17:34:22 -080017#include "shill/logging.h"
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080018#include "shill/refptr_types.h"
Wade Guthrie16196242012-11-20 15:53:52 -080019
20struct nlattr;
21
22namespace shill {
23
Wade Guthrie68da97c2013-02-26 13:09:35 -080024// NetlinkAttribute is an abstract base class that describes an attribute in a
Wade Guthrie16196242012-11-20 15:53:52 -080025// netlink-80211 message. Child classes are type-specific and will define
26// Get*Value and Set*Value methods (where * is the type). A second-level of
27// child classes exist for each individual attribute type.
28//
repo sync12cca802012-12-19 17:34:22 -080029// An attribute has an id (which is really an enumerated value), a data type,
Wade Guthrie16196242012-11-20 15:53:52 -080030// and a value. In an nlattr (the underlying format for an attribute in a
31// message), the data is stored as a blob without type information; the writer
32// and reader of the attribute must agree on the data type.
Wade Guthrie68da97c2013-02-26 13:09:35 -080033class NetlinkAttribute {
Wade Guthrie16196242012-11-20 15:53:52 -080034 public:
35 enum Type {
36 kTypeU8,
37 kTypeU16,
38 kTypeU32,
39 kTypeU64,
Wade Guthrie16196242012-11-20 15:53:52 -080040 kTypeFlag,
repo sync90ee0fa2012-12-18 10:08:08 -080041 kTypeString,
Wade Guthrie16196242012-11-20 15:53:52 -080042 kTypeNested,
43 kTypeRaw,
44 kTypeError
45 };
46
Wade Guthrie68da97c2013-02-26 13:09:35 -080047 NetlinkAttribute(int id, const char *id_string,
repo sync12cca802012-12-19 17:34:22 -080048 Type datatype, const char *datatype_string);
Wade Guthrie68da97c2013-02-26 13:09:35 -080049 virtual ~NetlinkAttribute() {}
Wade Guthrie16196242012-11-20 15:53:52 -080050
51 virtual bool InitFromNlAttr(const nlattr *data);
52
repo syncdc085c82012-12-28 08:54:41 -080053 // Accessors for the attribute's id and datatype information.
repo sync12cca802012-12-19 17:34:22 -080054 int id() const { return id_; }
55 virtual const char *id_string() const { return id_string_; }
56 Type datatype() const { return datatype_; }
repo sync1538d442012-12-20 15:24:35 -080057 const char *datatype_string() const { return datatype_string_; }
Wade Guthrie16196242012-11-20 15:53:52 -080058
59 // TODO(wdg): Since |data| is used, externally, to support |nla_parse_nested|,
60 // make it protected once all functionality has been brought inside the
Wade Guthrie68da97c2013-02-26 13:09:35 -080061 // NetlinkAttribute classes.
Wade Guthrie16196242012-11-20 15:53:52 -080062 //
63 // |data_| contains an 'nlattr *' but it's been stored as a ByteString.
64 // This returns a pointer to the data in the form that is intended.
65 const nlattr *data() const {
66 return reinterpret_cast<const nlattr *>(data_.GetConstData());
67 }
68
repo sync1538d442012-12-20 15:24:35 -080069 virtual bool GetU8Value(uint8_t *value) const;
70 virtual bool SetU8Value(uint8_t new_value);
71
72 virtual bool GetU16Value(uint16_t *value) const;
73 virtual bool SetU16Value(uint16_t value);
74
75 virtual bool GetU32Value(uint32_t *value) const;
76 virtual bool SetU32Value(uint32_t value);
77
78 virtual bool GetU64Value(uint64_t *value) const;
79 virtual bool SetU64Value(uint64_t value);
80
81 virtual bool GetFlagValue(bool *value) const;
82 virtual bool SetFlagValue(bool value);
83
84 virtual bool GetStringValue(std::string *value) const;
85 virtual bool SetStringValue(const std::string value);
86
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080087 virtual bool GetNestedAttributeList(AttributeListRefPtr *value);
88 virtual bool ConstGetNestedAttributeList(
89 AttributeListConstRefPtr *value) const;
90 virtual bool SetNestedHasAValue();
repo sync1538d442012-12-20 15:24:35 -080091
92 virtual bool GetRawValue(ByteString *value) const;
93
Wade Guthrie8e278612013-02-26 10:32:34 -080094 // Prints the attribute info -- for debugging.
95 virtual void Print(int log_level, int indent) const;
96
Wade Guthrie16196242012-11-20 15:53:52 -080097 // Fill a string with characters that represents the value of the attribute.
repo syncdc085c82012-12-28 08:54:41 -080098 // If no attribute is found or if the datatype isn't trivially stringizable,
Wade Guthrie16196242012-11-20 15:53:52 -080099 // this method returns 'false' and |value| remains unchanged.
repo sync1538d442012-12-20 15:24:35 -0800100 virtual bool ToString(std::string *value) const = 0;
Wade Guthrie16196242012-11-20 15:53:52 -0800101
102 // Writes the raw attribute data to a string. For debug.
103 std::string RawToString() const;
104
repo syncdc085c82012-12-28 08:54:41 -0800105 // Encodes the attribute suitably for the attributes in the payload portion
106 // of a netlink message suitable for Sockets::Send. Return value is empty on
107 // failure.
108 virtual ByteString Encode() const = 0;
109
Wade Guthrie3af8b4d2013-01-10 08:39:30 -0800110 bool has_a_value() const { return has_a_value_; }
111
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800112 // Note that |nla_get_*| don't change their arguments but don't declare
113 // themselves as 'const', either. These methods wrap the const castness.
114 static char *NlaGetString(const nlattr *input) {
115 return nla_get_string(const_cast<nlattr *>(input));
116 }
117 static uint8_t NlaGetU8(const nlattr *input) {
118 return nla_get_u8(const_cast<nlattr *>(input));
119 }
120 static uint16_t NlaGetU16(const nlattr *input) {
121 return nla_get_u16(const_cast<nlattr *>(input));
122 }
123 static uint32_t NlaGetU32(const nlattr *input) {
124 return nla_get_u32(const_cast<nlattr *>(input));
125 }
126 static uint64_t NlaGetU64(const nlattr *input) {
127 return nla_get_u64(const_cast<nlattr *>(input));
128 }
129
Wade Guthrie68da97c2013-02-26 13:09:35 -0800130 // Static factories generate the appropriate attribute object from the
131 // raw nlattr data.
132 static NetlinkAttribute *NewControlAttributeFromId(int id);
133 static NetlinkAttribute *NewNl80211AttributeFromId(int id);
134
Wade Guthrie16196242012-11-20 15:53:52 -0800135 protected:
Wade Guthrie8e278612013-02-26 10:32:34 -0800136 // Builds a string to precede a printout of this attribute.
137 std::string HeaderToPrint(int indent) const;
138
repo syncdc085c82012-12-28 08:54:41 -0800139 // Encodes the attribute suitably for the attributes in the payload portion
140 // of a netlink message suitable for Sockets::Send. Return value is empty on
141 // failure.
142 virtual ByteString EncodeGeneric(const unsigned char *data, int bytes) const;
143
Wade Guthrie16196242012-11-20 15:53:52 -0800144 // Raw data corresponding to the value in any of the child classes.
Wade Guthrie68da97c2013-02-26 13:09:35 -0800145 // TODO(wdg): When 'data()' is removed, move this to the NetlinkRawAttribute
Wade Guthrie16196242012-11-20 15:53:52 -0800146 // class.
147 ByteString data_;
148
Wade Guthrie3af8b4d2013-01-10 08:39:30 -0800149 // True if a value has been assigned to the attribute; false, otherwise.
150 bool has_a_value_;
151
Wade Guthrie16196242012-11-20 15:53:52 -0800152 private:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800153 int id_;
repo sync12cca802012-12-19 17:34:22 -0800154 const char *id_string_;
155 Type datatype_;
156 const char *datatype_string_;
Wade Guthrie16196242012-11-20 15:53:52 -0800157};
158
Wade Guthrie25cdb382012-12-04 14:04:05 -0800159// U8.
Wade Guthrie16196242012-11-20 15:53:52 -0800160
Wade Guthrie68da97c2013-02-26 13:09:35 -0800161class NetlinkU8Attribute : public NetlinkAttribute {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800162 public:
163 static const char kMyTypeString[];
164 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800165 NetlinkU8Attribute(int id, const char *id_string)
166 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800167 bool InitFromNlAttr(const nlattr *data);
168 bool GetU8Value(uint8_t *value) const;
169 bool SetU8Value(uint8_t new_value);
repo sync1538d442012-12-20 15:24:35 -0800170 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800171 virtual ByteString Encode() const;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800172
173 private:
174 uint8_t value_;
175};
176
Wade Guthrie68da97c2013-02-26 13:09:35 -0800177class Nl80211AttributeKeyIdx : public NetlinkU8Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800178 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800179 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800180 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800181 Nl80211AttributeKeyIdx() : NetlinkU8Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800182};
183
Wade Guthrie68da97c2013-02-26 13:09:35 -0800184class Nl80211AttributeRegType : public NetlinkU8Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800185 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800186 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800187 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800188 Nl80211AttributeRegType() : NetlinkU8Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800189};
190
191// U16.
192
Wade Guthrie68da97c2013-02-26 13:09:35 -0800193class NetlinkU16Attribute : public NetlinkAttribute {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800194 public:
195 static const char kMyTypeString[];
196 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800197 NetlinkU16Attribute(int id, const char *id_string)
198 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800199 bool InitFromNlAttr(const nlattr *data);
200 bool GetU16Value(uint16_t *value) const;
201 bool SetU16Value(uint16_t new_value);
repo sync1538d442012-12-20 15:24:35 -0800202 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800203 virtual ByteString Encode() const;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800204
205 private:
206 uint16_t value_;
207};
208
Wade Guthrie68da97c2013-02-26 13:09:35 -0800209class Nl80211AttributeReasonCode : public NetlinkU16Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800210 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800211 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800212 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800213 Nl80211AttributeReasonCode() : NetlinkU16Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800214};
215
Wade Guthrie68da97c2013-02-26 13:09:35 -0800216class Nl80211AttributeStatusCode : public NetlinkU16Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800217 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800218 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800219 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800220 Nl80211AttributeStatusCode() : NetlinkU16Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800221};
222
223// U32.
224
Wade Guthrie68da97c2013-02-26 13:09:35 -0800225class NetlinkU32Attribute : public NetlinkAttribute {
Wade Guthrie16196242012-11-20 15:53:52 -0800226 public:
227 static const char kMyTypeString[];
228 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800229 NetlinkU32Attribute(int id, const char *id_string)
230 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie16196242012-11-20 15:53:52 -0800231 bool InitFromNlAttr(const nlattr *data);
232 bool GetU32Value(uint32_t *value) const;
233 bool SetU32Value(uint32_t new_value);
repo sync1538d442012-12-20 15:24:35 -0800234 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800235 virtual ByteString Encode() const;
Wade Guthrie16196242012-11-20 15:53:52 -0800236
237 private:
238 uint32_t value_;
239};
240
Wade Guthrie68da97c2013-02-26 13:09:35 -0800241class Nl80211AttributeDuration : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800242 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800243 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800244 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800245 Nl80211AttributeDuration() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800246};
247
Wade Guthrie68da97c2013-02-26 13:09:35 -0800248class Nl80211AttributeGeneration : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800249 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800250 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800251 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800252 Nl80211AttributeGeneration() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800253};
254
Wade Guthrie68da97c2013-02-26 13:09:35 -0800255class Nl80211AttributeIfindex : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800256 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800257 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800258 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800259 Nl80211AttributeIfindex() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800260};
261
Wade Guthrie68da97c2013-02-26 13:09:35 -0800262class Nl80211AttributeKeyType : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800263 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800264 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800265 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800266 Nl80211AttributeKeyType() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800267};
268
Wade Guthrie68da97c2013-02-26 13:09:35 -0800269class Nl80211AttributeRegInitiator : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800270 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800271 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800272 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800273 Nl80211AttributeRegInitiator() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800274};
275
Wade Guthrie68da97c2013-02-26 13:09:35 -0800276class Nl80211AttributeWiphy : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800277 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800278 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800279 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800280 Nl80211AttributeWiphy() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800281};
282
Wade Guthrie68da97c2013-02-26 13:09:35 -0800283class Nl80211AttributeWiphyFreq : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800284 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800285 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800286 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800287 Nl80211AttributeWiphyFreq() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800288};
289
290// U64.
291
Wade Guthrie68da97c2013-02-26 13:09:35 -0800292class NetlinkU64Attribute : public NetlinkAttribute {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800293 public:
294 static const char kMyTypeString[];
295 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800296 NetlinkU64Attribute(int id, const char *id_string)
297 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800298 bool InitFromNlAttr(const nlattr *data);
299 bool GetU64Value(uint64_t *value) const;
300 bool SetU64Value(uint64_t new_value);
repo sync1538d442012-12-20 15:24:35 -0800301 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800302 virtual ByteString Encode() const;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800303
304 private:
305 uint64_t value_;
306};
307
Wade Guthrie68da97c2013-02-26 13:09:35 -0800308class Nl80211AttributeCookie : public NetlinkU64Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800309 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800310 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800311 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800312 Nl80211AttributeCookie() : NetlinkU64Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800313};
314
315// Flag.
316
Wade Guthrie68da97c2013-02-26 13:09:35 -0800317class NetlinkFlagAttribute : public NetlinkAttribute {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800318 public:
319 static const char kMyTypeString[];
320 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800321 NetlinkFlagAttribute(int id, const char *id_string)
322 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800323 bool InitFromNlAttr(const nlattr *data);
324 bool GetFlagValue(bool *value) const;
325 bool SetFlagValue(bool new_value);
repo sync1538d442012-12-20 15:24:35 -0800326 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800327 virtual ByteString Encode() const;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800328
329 private:
330 bool value_;
331};
332
Wade Guthrie68da97c2013-02-26 13:09:35 -0800333class Nl80211AttributeDisconnectedByAp : public NetlinkFlagAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800334 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800335 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800336 static const char kNameString[];
337 Nl80211AttributeDisconnectedByAp() :
Wade Guthrie68da97c2013-02-26 13:09:35 -0800338 NetlinkFlagAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800339};
340
Wade Guthrie68da97c2013-02-26 13:09:35 -0800341class Nl80211AttributeSupportMeshAuth : public NetlinkFlagAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800342 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800343 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800344 static const char kNameString[];
345 Nl80211AttributeSupportMeshAuth() :
Wade Guthrie68da97c2013-02-26 13:09:35 -0800346 NetlinkFlagAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800347};
348
Wade Guthrie68da97c2013-02-26 13:09:35 -0800349class Nl80211AttributeTimedOut : public NetlinkFlagAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800350 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800351 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800352 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800353 Nl80211AttributeTimedOut() : NetlinkFlagAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800354};
355
356// String.
357
Wade Guthrie68da97c2013-02-26 13:09:35 -0800358class NetlinkStringAttribute : public NetlinkAttribute {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800359 public:
360 static const char kMyTypeString[];
361 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800362 NetlinkStringAttribute(int id, const char *id_string)
363 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800364 bool InitFromNlAttr(const nlattr *data);
365 bool GetStringValue(std::string *value) const;
366 bool SetStringValue(const std::string new_value);
repo sync1538d442012-12-20 15:24:35 -0800367 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800368 virtual ByteString Encode() const;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800369
370 private:
371 std::string value_;
372};
Wade Guthrie16196242012-11-20 15:53:52 -0800373
Wade Guthrie68da97c2013-02-26 13:09:35 -0800374class Nl80211AttributeRegAlpha2 : public NetlinkStringAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800375 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800376 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800377 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800378 Nl80211AttributeRegAlpha2() : NetlinkStringAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800379};
380
Wade Guthrie68da97c2013-02-26 13:09:35 -0800381class Nl80211AttributeWiphyName : public NetlinkStringAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800382 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800383 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800384 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800385 Nl80211AttributeWiphyName() : NetlinkStringAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800386};
387
repo sync12cca802012-12-19 17:34:22 -0800388// Nested.
389
Wade Guthrie68da97c2013-02-26 13:09:35 -0800390class NetlinkNestedAttribute : public NetlinkAttribute {
repo sync12cca802012-12-19 17:34:22 -0800391 public:
392 static const char kMyTypeString[];
393 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800394 NetlinkNestedAttribute(int id, const char *id_string);
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800395 virtual bool InitFromNlAttr(const nlattr *data) {
repo sync12cca802012-12-19 17:34:22 -0800396 LOG(FATAL) << "Try initializing a _specific_ nested type, instead.";
397 return false;
398 }
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800399 virtual bool GetNestedAttributeList(AttributeListRefPtr *value);
400 virtual bool ConstGetNestedAttributeList(
401 AttributeListConstRefPtr *value) const;
402 virtual bool SetNestedHasAValue();
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800403 virtual void Print(int log_level, int indent) const;
404 virtual bool ToString(std::string *value) const;
405 virtual ByteString Encode() const;
repo sync12cca802012-12-19 17:34:22 -0800406
407 protected:
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800408 // Describes a single nested attribute. Provides the expected values and
409 // type (including further nesting). Normally, an array of these, one for
410 // each attribute at one level of nesting is presented, along with the data
411 // to be parsed, to |InitNestedFromNlAttr|. If the attributes on one level
412 // represent an array, a single |NestedData| is provided and |is_array| is
413 // set (note that one level of nesting either contains _only_ an array or
414 // _no_ array).
415 struct NestedData {
416 nla_policy policy;
417 const char *attribute_name;
418 const NestedData *deeper_nesting;
419 size_t deeper_nesting_size;
420 bool is_array;
421 // TODO(wdg): Add function pointer for a custom attribute parser.
422 };
423
424 // Builds an AttributeList (|list|) that contains all of the attriubtes in
425 // |const_data|. |const_data| should point to the enclosing nested attribute
426 // header; for the example of the nested attribute NL80211_ATTR_CQM:
427 // nlattr::nla_type: NL80211_ATTR_CQM <-- const_data points here
428 // nlattr::nla_len: 12 bytes
429 // nlattr::nla_type: PKT_LOSS_EVENT (1st and only nested attribute)
430 // nlattr::nla_len: 8 bytes
431 // <data>: 0x32
432 // One can assemble (hence, disassemble) a set of child attributes under a
433 // nested attribute parent as an array of elements or as a structure.
434 //
435 // The data is parsed using the expected configuration in |nested_template|.
436 // If the code expects an array, it will pass a single template element and
437 // mark that as an array.
438 static bool InitNestedFromNlAttr(AttributeList *list,
439 const NestedData *nested_template,
440 size_t nested_template_size,
441 const nlattr *const_data);
442
443 static bool ParseNestedArray(AttributeList *list,
444 const NestedData &nested_template,
445 const nlattr *const_data);
446
447 static bool ParseNestedStructure(AttributeList *list,
448 const NestedData *nested_template,
449 size_t nested_template_size,
450 const nlattr *const_data);
451
452 // Helper function used by InitNestedFromNlAttr to add a single child
453 // attribute to a nested attribute.
454 static void AddAttributeToNested(AttributeList *list, uint16_t type, size_t i,
455 const std::string &attribute_name,
456 const nlattr &attr,
457 const NestedData &nested_data);
458 AttributeListRefPtr value_;
repo sync12cca802012-12-19 17:34:22 -0800459};
460
Wade Guthrie68da97c2013-02-26 13:09:35 -0800461class Nl80211AttributeCqm : public NetlinkNestedAttribute {
repo sync12cca802012-12-19 17:34:22 -0800462 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800463 static const int kName;
repo sync12cca802012-12-19 17:34:22 -0800464 static const char kNameString[];
465 Nl80211AttributeCqm();
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800466 virtual bool InitFromNlAttr(const nlattr *data);
467};
468
469class Nl80211AttributeScanFrequencies : public NetlinkNestedAttribute {
470 public:
471 static const int kName;
472 static const char kNameString[];
473 explicit Nl80211AttributeScanFrequencies();
474 virtual bool InitFromNlAttr(const nlattr *const_data);
475};
476
477class Nl80211AttributeScanSsids : public NetlinkNestedAttribute {
478 public:
479 static const int kName;
480 static const char kNameString[];
481 explicit Nl80211AttributeScanSsids();
482 virtual bool InitFromNlAttr(const nlattr *const_data);
repo sync12cca802012-12-19 17:34:22 -0800483};
484
Wade Guthrie68da97c2013-02-26 13:09:35 -0800485class Nl80211AttributeStaInfo : public NetlinkNestedAttribute {
repo sync12cca802012-12-19 17:34:22 -0800486 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800487 static const int kName;
repo sync12cca802012-12-19 17:34:22 -0800488 static const char kNameString[];
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800489 Nl80211AttributeStaInfo();
490 virtual bool InitFromNlAttr(const nlattr *const_data);
repo sync12cca802012-12-19 17:34:22 -0800491};
492
Wade Guthrie25cdb382012-12-04 14:04:05 -0800493// Raw.
494
Wade Guthrie68da97c2013-02-26 13:09:35 -0800495class NetlinkRawAttribute : public NetlinkAttribute {
Wade Guthrie16196242012-11-20 15:53:52 -0800496 public:
497 static const char kMyTypeString[];
498 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800499 NetlinkRawAttribute(int id, const char *id_string)
500 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie16196242012-11-20 15:53:52 -0800501 bool InitFromNlAttr(const nlattr *data);
Wade Guthrie25cdb382012-12-04 14:04:05 -0800502 bool GetRawValue(ByteString *value) const;
Wade Guthrie16196242012-11-20 15:53:52 -0800503 // Not supporting 'set' for raw data. This type is a "don't know" type to
504 // be used for user-bound massages (via InitFromNlAttr). The 'set' method
505 // is intended for building kernel-bound messages and shouldn't be used with
506 // raw data.
repo sync1538d442012-12-20 15:24:35 -0800507 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800508 virtual ByteString Encode() const {
509 return ByteString(); // TODO(wdg): Actually encode the attribute.
510 }
Wade Guthrie16196242012-11-20 15:53:52 -0800511};
512
Wade Guthrie68da97c2013-02-26 13:09:35 -0800513class Nl80211AttributeFrame : public NetlinkRawAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800514 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800515 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800516 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800517 Nl80211AttributeFrame() : NetlinkRawAttribute(kName, kNameString) {}
Wade Guthrie16196242012-11-20 15:53:52 -0800518};
519
Wade Guthrie68da97c2013-02-26 13:09:35 -0800520class NetlinkAttributeGeneric : public NetlinkRawAttribute {
Wade Guthrie16196242012-11-20 15:53:52 -0800521 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800522 explicit NetlinkAttributeGeneric(int id);
repo sync12cca802012-12-19 17:34:22 -0800523 const char *id_string() const;
Wade Guthrie16196242012-11-20 15:53:52 -0800524
525 private:
repo sync12cca802012-12-19 17:34:22 -0800526 std::string id_string_;
Wade Guthrie16196242012-11-20 15:53:52 -0800527};
528
Wade Guthrie68da97c2013-02-26 13:09:35 -0800529class Nl80211AttributeKeySeq : public NetlinkRawAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800530 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800531 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800532 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800533 Nl80211AttributeKeySeq() : NetlinkRawAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800534};
535
Wade Guthrie68da97c2013-02-26 13:09:35 -0800536class Nl80211AttributeMac : public NetlinkRawAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800537 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800538 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800539 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800540 Nl80211AttributeMac() : NetlinkRawAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800541};
542
Wade Guthrie68da97c2013-02-26 13:09:35 -0800543class Nl80211AttributeRespIe : public NetlinkRawAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800544 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800545 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800546 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800547 Nl80211AttributeRespIe() : NetlinkRawAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800548};
549
Wade Guthrie16196242012-11-20 15:53:52 -0800550} // namespace shill
551
552#endif // SHILL_NLATTRIBUTE_H_