blob: 1c555a790d285e3a6dd94a1dec7301b2a732d0db [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 sync12cca802012-12-19 17:34:22 -080015#include <base/memory/weak_ptr.h>
Wade Guthrie16196242012-11-20 15:53:52 -080016
repo sync1538d442012-12-20 15:24:35 -080017#include "shill/attribute_list.h"
Wade Guthrie16196242012-11-20 15:53:52 -080018#include "shill/byte_string.h"
repo sync12cca802012-12-19 17:34:22 -080019#include "shill/logging.h"
Wade Guthrie16196242012-11-20 15:53:52 -080020
21struct nlattr;
22
23namespace shill {
24
Wade Guthrie68da97c2013-02-26 13:09:35 -080025// NetlinkAttribute is an abstract base class that describes an attribute in a
Wade Guthrie16196242012-11-20 15:53:52 -080026// netlink-80211 message. Child classes are type-specific and will define
27// Get*Value and Set*Value methods (where * is the type). A second-level of
28// child classes exist for each individual attribute type.
29//
repo sync12cca802012-12-19 17:34:22 -080030// An attribute has an id (which is really an enumerated value), a data type,
Wade Guthrie16196242012-11-20 15:53:52 -080031// and a value. In an nlattr (the underlying format for an attribute in a
32// message), the data is stored as a blob without type information; the writer
33// and reader of the attribute must agree on the data type.
Wade Guthrie68da97c2013-02-26 13:09:35 -080034class NetlinkAttribute {
Wade Guthrie16196242012-11-20 15:53:52 -080035 public:
36 enum Type {
37 kTypeU8,
38 kTypeU16,
39 kTypeU32,
40 kTypeU64,
Wade Guthrie16196242012-11-20 15:53:52 -080041 kTypeFlag,
repo sync90ee0fa2012-12-18 10:08:08 -080042 kTypeString,
Wade Guthrie16196242012-11-20 15:53:52 -080043 kTypeNested,
44 kTypeRaw,
45 kTypeError
46 };
47
Wade Guthrie68da97c2013-02-26 13:09:35 -080048 NetlinkAttribute(int id, const char *id_string,
repo sync12cca802012-12-19 17:34:22 -080049 Type datatype, const char *datatype_string);
Wade Guthrie68da97c2013-02-26 13:09:35 -080050 virtual ~NetlinkAttribute() {}
Wade Guthrie16196242012-11-20 15:53:52 -080051
52 virtual bool InitFromNlAttr(const nlattr *data);
53
Wade Guthrie68da97c2013-02-26 13:09:35 -080054 // Static factory generates the appropriate NetlinkAttribute object from the
Wade Guthrie16196242012-11-20 15:53:52 -080055 // raw nlattr data.
Wade Guthrie68da97c2013-02-26 13:09:35 -080056 static NetlinkAttribute *NewFromId(int id);
Wade Guthrie16196242012-11-20 15:53:52 -080057
repo syncdc085c82012-12-28 08:54:41 -080058 // Accessors for the attribute's id and datatype information.
repo sync12cca802012-12-19 17:34:22 -080059 int id() const { return id_; }
60 virtual const char *id_string() const { return id_string_; }
61 Type datatype() const { return datatype_; }
repo sync1538d442012-12-20 15:24:35 -080062 const char *datatype_string() const { return datatype_string_; }
Wade Guthrie16196242012-11-20 15:53:52 -080063
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
Wade Guthrie68da97c2013-02-26 13:09:35 -080066 // NetlinkAttribute classes.
Wade Guthrie16196242012-11-20 15:53:52 -080067 //
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
repo sync1538d442012-12-20 15:24:35 -080074 virtual bool GetU8Value(uint8_t *value) const;
75 virtual bool SetU8Value(uint8_t new_value);
76
77 virtual bool GetU16Value(uint16_t *value) const;
78 virtual bool SetU16Value(uint16_t value);
79
80 virtual bool GetU32Value(uint32_t *value) const;
81 virtual bool SetU32Value(uint32_t value);
82
83 virtual bool GetU64Value(uint64_t *value) const;
84 virtual bool SetU64Value(uint64_t value);
85
86 virtual bool GetFlagValue(bool *value) const;
87 virtual bool SetFlagValue(bool value);
88
89 virtual bool GetStringValue(std::string *value) const;
90 virtual bool SetStringValue(const std::string value);
91
92 virtual bool GetNestedValue(base::WeakPtr<AttributeList> *value);
93
94 virtual bool GetRawValue(ByteString *value) const;
95
Wade Guthrie8e278612013-02-26 10:32:34 -080096 // Prints the attribute info -- for debugging.
97 virtual void Print(int log_level, int indent) const;
98
Wade Guthrie16196242012-11-20 15:53:52 -080099 // Fill a string with characters that represents the value of the attribute.
repo syncdc085c82012-12-28 08:54:41 -0800100 // If no attribute is found or if the datatype isn't trivially stringizable,
Wade Guthrie16196242012-11-20 15:53:52 -0800101 // this method returns 'false' and |value| remains unchanged.
repo sync1538d442012-12-20 15:24:35 -0800102 virtual bool ToString(std::string *value) const = 0;
Wade Guthrie16196242012-11-20 15:53:52 -0800103
104 // Writes the raw attribute data to a string. For debug.
105 std::string RawToString() const;
106
repo syncdc085c82012-12-28 08:54:41 -0800107 // Encodes the attribute suitably for the attributes in the payload portion
108 // of a netlink message suitable for Sockets::Send. Return value is empty on
109 // failure.
110 virtual ByteString Encode() const = 0;
111
Wade Guthrie3af8b4d2013-01-10 08:39:30 -0800112 bool has_a_value() const { return has_a_value_; }
113
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800114 // Note that |nla_get_*| don't change their arguments but don't declare
115 // themselves as 'const', either. These methods wrap the const castness.
116 static char *NlaGetString(const nlattr *input) {
117 return nla_get_string(const_cast<nlattr *>(input));
118 }
119 static uint8_t NlaGetU8(const nlattr *input) {
120 return nla_get_u8(const_cast<nlattr *>(input));
121 }
122 static uint16_t NlaGetU16(const nlattr *input) {
123 return nla_get_u16(const_cast<nlattr *>(input));
124 }
125 static uint32_t NlaGetU32(const nlattr *input) {
126 return nla_get_u32(const_cast<nlattr *>(input));
127 }
128 static uint64_t NlaGetU64(const nlattr *input) {
129 return nla_get_u64(const_cast<nlattr *>(input));
130 }
131
Wade Guthrie68da97c2013-02-26 13:09:35 -0800132 // Static factories generate the appropriate attribute object from the
133 // raw nlattr data.
134 static NetlinkAttribute *NewControlAttributeFromId(int id);
135 static NetlinkAttribute *NewNl80211AttributeFromId(int id);
136
Wade Guthrie16196242012-11-20 15:53:52 -0800137 protected:
Wade Guthrie8e278612013-02-26 10:32:34 -0800138 // Builds a string to precede a printout of this attribute.
139 std::string HeaderToPrint(int indent) const;
140
repo syncdc085c82012-12-28 08:54:41 -0800141 // Encodes the attribute suitably for the attributes in the payload portion
142 // of a netlink message suitable for Sockets::Send. Return value is empty on
143 // failure.
144 virtual ByteString EncodeGeneric(const unsigned char *data, int bytes) const;
145
Wade Guthrie16196242012-11-20 15:53:52 -0800146 // Raw data corresponding to the value in any of the child classes.
Wade Guthrie68da97c2013-02-26 13:09:35 -0800147 // TODO(wdg): When 'data()' is removed, move this to the NetlinkRawAttribute
Wade Guthrie16196242012-11-20 15:53:52 -0800148 // class.
149 ByteString data_;
150
Wade Guthrie3af8b4d2013-01-10 08:39:30 -0800151 // True if a value has been assigned to the attribute; false, otherwise.
152 bool has_a_value_;
153
Wade Guthrie16196242012-11-20 15:53:52 -0800154 private:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800155 int id_;
repo sync12cca802012-12-19 17:34:22 -0800156 const char *id_string_;
157 Type datatype_;
158 const char *datatype_string_;
Wade Guthrie16196242012-11-20 15:53:52 -0800159};
160
Wade Guthrie25cdb382012-12-04 14:04:05 -0800161// U8.
Wade Guthrie16196242012-11-20 15:53:52 -0800162
Wade Guthrie68da97c2013-02-26 13:09:35 -0800163class NetlinkU8Attribute : public NetlinkAttribute {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800164 public:
165 static const char kMyTypeString[];
166 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800167 NetlinkU8Attribute(int id, const char *id_string)
168 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800169 bool InitFromNlAttr(const nlattr *data);
170 bool GetU8Value(uint8_t *value) const;
171 bool SetU8Value(uint8_t new_value);
repo sync1538d442012-12-20 15:24:35 -0800172 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800173 virtual ByteString Encode() const;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800174
175 private:
176 uint8_t value_;
177};
178
Wade Guthrie68da97c2013-02-26 13:09:35 -0800179class Nl80211AttributeKeyIdx : public NetlinkU8Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800180 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800181 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800182 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800183 Nl80211AttributeKeyIdx() : NetlinkU8Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800184};
185
Wade Guthrie68da97c2013-02-26 13:09:35 -0800186class Nl80211AttributeRegType : public NetlinkU8Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800187 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800188 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800189 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800190 Nl80211AttributeRegType() : NetlinkU8Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800191};
192
193// U16.
194
Wade Guthrie68da97c2013-02-26 13:09:35 -0800195class NetlinkU16Attribute : public NetlinkAttribute {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800196 public:
197 static const char kMyTypeString[];
198 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800199 NetlinkU16Attribute(int id, const char *id_string)
200 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800201 bool InitFromNlAttr(const nlattr *data);
202 bool GetU16Value(uint16_t *value) const;
203 bool SetU16Value(uint16_t new_value);
repo sync1538d442012-12-20 15:24:35 -0800204 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800205 virtual ByteString Encode() const;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800206
207 private:
208 uint16_t value_;
209};
210
Wade Guthrie68da97c2013-02-26 13:09:35 -0800211class Nl80211AttributeReasonCode : public NetlinkU16Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800212 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800213 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800214 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800215 Nl80211AttributeReasonCode() : NetlinkU16Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800216};
217
Wade Guthrie68da97c2013-02-26 13:09:35 -0800218class Nl80211AttributeStatusCode : public NetlinkU16Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800219 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800220 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800221 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800222 Nl80211AttributeStatusCode() : NetlinkU16Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800223};
224
225// U32.
226
Wade Guthrie68da97c2013-02-26 13:09:35 -0800227class NetlinkU32Attribute : public NetlinkAttribute {
Wade Guthrie16196242012-11-20 15:53:52 -0800228 public:
229 static const char kMyTypeString[];
230 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800231 NetlinkU32Attribute(int id, const char *id_string)
232 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie16196242012-11-20 15:53:52 -0800233 bool InitFromNlAttr(const nlattr *data);
234 bool GetU32Value(uint32_t *value) const;
235 bool SetU32Value(uint32_t new_value);
repo sync1538d442012-12-20 15:24:35 -0800236 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800237 virtual ByteString Encode() const;
Wade Guthrie16196242012-11-20 15:53:52 -0800238
239 private:
240 uint32_t value_;
241};
242
Wade Guthrie68da97c2013-02-26 13:09:35 -0800243class Nl80211AttributeDuration : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800244 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800245 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800246 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800247 Nl80211AttributeDuration() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800248};
249
Wade Guthrie68da97c2013-02-26 13:09:35 -0800250class Nl80211AttributeGeneration : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800251 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800252 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800253 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800254 Nl80211AttributeGeneration() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800255};
256
Wade Guthrie68da97c2013-02-26 13:09:35 -0800257class Nl80211AttributeIfindex : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800258 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800259 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800260 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800261 Nl80211AttributeIfindex() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800262};
263
Wade Guthrie68da97c2013-02-26 13:09:35 -0800264class Nl80211AttributeKeyType : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800265 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800266 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800267 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800268 Nl80211AttributeKeyType() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800269};
270
Wade Guthrie68da97c2013-02-26 13:09:35 -0800271class Nl80211AttributeRegInitiator : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800272 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800273 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800274 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800275 Nl80211AttributeRegInitiator() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800276};
277
Wade Guthrie68da97c2013-02-26 13:09:35 -0800278class Nl80211AttributeWiphy : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800279 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800280 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800281 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800282 Nl80211AttributeWiphy() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800283};
284
Wade Guthrie68da97c2013-02-26 13:09:35 -0800285class Nl80211AttributeWiphyFreq : public NetlinkU32Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800286 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800287 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800288 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800289 Nl80211AttributeWiphyFreq() : NetlinkU32Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800290};
291
292// U64.
293
Wade Guthrie68da97c2013-02-26 13:09:35 -0800294class NetlinkU64Attribute : public NetlinkAttribute {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800295 public:
296 static const char kMyTypeString[];
297 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800298 NetlinkU64Attribute(int id, const char *id_string)
299 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800300 bool InitFromNlAttr(const nlattr *data);
301 bool GetU64Value(uint64_t *value) const;
302 bool SetU64Value(uint64_t new_value);
repo sync1538d442012-12-20 15:24:35 -0800303 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800304 virtual ByteString Encode() const;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800305
306 private:
307 uint64_t value_;
308};
309
Wade Guthrie68da97c2013-02-26 13:09:35 -0800310class Nl80211AttributeCookie : public NetlinkU64Attribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800311 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800312 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800313 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800314 Nl80211AttributeCookie() : NetlinkU64Attribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800315};
316
317// Flag.
318
Wade Guthrie68da97c2013-02-26 13:09:35 -0800319class NetlinkFlagAttribute : public NetlinkAttribute {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800320 public:
321 static const char kMyTypeString[];
322 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800323 NetlinkFlagAttribute(int id, const char *id_string)
324 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800325 bool InitFromNlAttr(const nlattr *data);
326 bool GetFlagValue(bool *value) const;
327 bool SetFlagValue(bool new_value);
repo sync1538d442012-12-20 15:24:35 -0800328 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800329 virtual ByteString Encode() const;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800330
331 private:
332 bool value_;
333};
334
Wade Guthrie68da97c2013-02-26 13:09:35 -0800335class Nl80211AttributeDisconnectedByAp : public NetlinkFlagAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800336 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800337 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800338 static const char kNameString[];
339 Nl80211AttributeDisconnectedByAp() :
Wade Guthrie68da97c2013-02-26 13:09:35 -0800340 NetlinkFlagAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800341};
342
Wade Guthrie68da97c2013-02-26 13:09:35 -0800343class Nl80211AttributeSupportMeshAuth : public NetlinkFlagAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800344 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800345 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800346 static const char kNameString[];
347 Nl80211AttributeSupportMeshAuth() :
Wade Guthrie68da97c2013-02-26 13:09:35 -0800348 NetlinkFlagAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800349};
350
Wade Guthrie68da97c2013-02-26 13:09:35 -0800351class Nl80211AttributeTimedOut : public NetlinkFlagAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800352 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800353 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800354 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800355 Nl80211AttributeTimedOut() : NetlinkFlagAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800356};
357
358// String.
359
Wade Guthrie68da97c2013-02-26 13:09:35 -0800360class NetlinkStringAttribute : public NetlinkAttribute {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800361 public:
362 static const char kMyTypeString[];
363 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800364 NetlinkStringAttribute(int id, const char *id_string)
365 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800366 bool InitFromNlAttr(const nlattr *data);
367 bool GetStringValue(std::string *value) const;
368 bool SetStringValue(const std::string new_value);
repo sync1538d442012-12-20 15:24:35 -0800369 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800370 virtual ByteString Encode() const;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800371
372 private:
373 std::string value_;
374};
Wade Guthrie16196242012-11-20 15:53:52 -0800375
Wade Guthrie68da97c2013-02-26 13:09:35 -0800376class Nl80211AttributeRegAlpha2 : public NetlinkStringAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800377 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800378 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800379 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800380 Nl80211AttributeRegAlpha2() : NetlinkStringAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800381};
382
Wade Guthrie68da97c2013-02-26 13:09:35 -0800383class Nl80211AttributeWiphyName : public NetlinkStringAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800384 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800385 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800386 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800387 Nl80211AttributeWiphyName() : NetlinkStringAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800388};
389
repo sync12cca802012-12-19 17:34:22 -0800390// Nested.
391
Wade Guthrie68da97c2013-02-26 13:09:35 -0800392class NetlinkNestedAttribute : public NetlinkAttribute {
repo sync12cca802012-12-19 17:34:22 -0800393 public:
394 static const char kMyTypeString[];
395 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800396 NetlinkNestedAttribute(int id, const char *id_string);
repo sync12cca802012-12-19 17:34:22 -0800397 bool InitFromNlAttr(const nlattr *data) {
398 LOG(FATAL) << "Try initializing a _specific_ nested type, instead.";
399 return false;
400 }
repo sync1538d442012-12-20 15:24:35 -0800401 bool GetNestedValue(base::WeakPtr<AttributeList> *value);
402 bool ToString(std::string *value) const {
Wade Guthrie3af8b4d2013-01-10 08:39:30 -0800403 return false; // TODO(wdg): Actually generate a string, here.
repo sync12cca802012-12-19 17:34:22 -0800404 }
repo syncdc085c82012-12-28 08:54:41 -0800405 virtual ByteString Encode() const {
406 return ByteString(); // TODO(wdg): Actually encode the attribute.
407 }
repo sync12cca802012-12-19 17:34:22 -0800408
409 protected:
repo sync1538d442012-12-20 15:24:35 -0800410 AttributeList value_;
repo sync12cca802012-12-19 17:34:22 -0800411};
412
Wade Guthrie68da97c2013-02-26 13:09:35 -0800413class Nl80211AttributeCqm : public NetlinkNestedAttribute {
repo sync12cca802012-12-19 17:34:22 -0800414 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800415 static const int kName;
repo sync12cca802012-12-19 17:34:22 -0800416 static const char kNameString[];
417 Nl80211AttributeCqm();
418 bool InitFromNlAttr(const nlattr *data);
repo sync1538d442012-12-20 15:24:35 -0800419 bool ToString(std::string *value) const {
Wade Guthrie3af8b4d2013-01-10 08:39:30 -0800420 return false; // TODO(wdg): Need |ToString|.
repo sync12cca802012-12-19 17:34:22 -0800421 }
422};
423
Wade Guthrie68da97c2013-02-26 13:09:35 -0800424class Nl80211AttributeStaInfo : public NetlinkNestedAttribute {
repo sync12cca802012-12-19 17:34:22 -0800425 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800426 static const int kName;
repo sync12cca802012-12-19 17:34:22 -0800427 static const char kNameString[];
428 explicit Nl80211AttributeStaInfo() :
Wade Guthrie68da97c2013-02-26 13:09:35 -0800429 NetlinkNestedAttribute(kName, kNameString) {}
repo sync12cca802012-12-19 17:34:22 -0800430 bool InitFromNlAttr(const nlattr *const_data);
Wade Guthrie3af8b4d2013-01-10 08:39:30 -0800431 bool ToString(std::string *value) const {
432 return false; // TODO(wdg): Need |ToString|.
repo sync12cca802012-12-19 17:34:22 -0800433 }
434};
435
Wade Guthrie25cdb382012-12-04 14:04:05 -0800436// Raw.
437
Wade Guthrie68da97c2013-02-26 13:09:35 -0800438class NetlinkRawAttribute : public NetlinkAttribute {
Wade Guthrie16196242012-11-20 15:53:52 -0800439 public:
440 static const char kMyTypeString[];
441 static const Type kType;
Wade Guthrie68da97c2013-02-26 13:09:35 -0800442 NetlinkRawAttribute(int id, const char *id_string)
443 : NetlinkAttribute(id, id_string, kType, kMyTypeString) {}
Wade Guthrie16196242012-11-20 15:53:52 -0800444 bool InitFromNlAttr(const nlattr *data);
Wade Guthrie25cdb382012-12-04 14:04:05 -0800445 bool GetRawValue(ByteString *value) const;
Wade Guthrie16196242012-11-20 15:53:52 -0800446 // Not supporting 'set' for raw data. This type is a "don't know" type to
447 // be used for user-bound massages (via InitFromNlAttr). The 'set' method
448 // is intended for building kernel-bound messages and shouldn't be used with
449 // raw data.
repo sync1538d442012-12-20 15:24:35 -0800450 bool ToString(std::string *value) const;
repo syncdc085c82012-12-28 08:54:41 -0800451 virtual ByteString Encode() const {
452 return ByteString(); // TODO(wdg): Actually encode the attribute.
453 }
Wade Guthrie16196242012-11-20 15:53:52 -0800454};
455
Wade Guthrie68da97c2013-02-26 13:09:35 -0800456class Nl80211AttributeFrame : public NetlinkRawAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800457 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800458 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800459 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800460 Nl80211AttributeFrame() : NetlinkRawAttribute(kName, kNameString) {}
Wade Guthrie16196242012-11-20 15:53:52 -0800461};
462
Wade Guthrie68da97c2013-02-26 13:09:35 -0800463class NetlinkAttributeGeneric : public NetlinkRawAttribute {
Wade Guthrie16196242012-11-20 15:53:52 -0800464 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800465 explicit NetlinkAttributeGeneric(int id);
repo sync12cca802012-12-19 17:34:22 -0800466 const char *id_string() const;
Wade Guthrie16196242012-11-20 15:53:52 -0800467
468 private:
repo sync12cca802012-12-19 17:34:22 -0800469 std::string id_string_;
Wade Guthrie16196242012-11-20 15:53:52 -0800470};
471
Wade Guthrie68da97c2013-02-26 13:09:35 -0800472class Nl80211AttributeKeySeq : public NetlinkRawAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800473 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800474 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800475 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800476 Nl80211AttributeKeySeq() : NetlinkRawAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800477};
478
Wade Guthrie68da97c2013-02-26 13:09:35 -0800479class Nl80211AttributeMac : public NetlinkRawAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800480 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800481 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800482 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800483 Nl80211AttributeMac() : NetlinkRawAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800484};
485
Wade Guthrie68da97c2013-02-26 13:09:35 -0800486class Nl80211AttributeRespIe : public NetlinkRawAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800487 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800488 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800489 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800490 Nl80211AttributeRespIe() : NetlinkRawAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800491};
492
Wade Guthrie68da97c2013-02-26 13:09:35 -0800493class Nl80211AttributeScanFrequencies : public NetlinkRawAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800494 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800495 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800496 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800497 Nl80211AttributeScanFrequencies() : NetlinkRawAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800498};
499
Wade Guthrie68da97c2013-02-26 13:09:35 -0800500class Nl80211AttributeScanSsids : public NetlinkRawAttribute {
Wade Guthrie25cdb382012-12-04 14:04:05 -0800501 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -0800502 static const int kName;
Wade Guthrie25cdb382012-12-04 14:04:05 -0800503 static const char kNameString[];
Wade Guthrie68da97c2013-02-26 13:09:35 -0800504 Nl80211AttributeScanSsids() : NetlinkRawAttribute(kName, kNameString) {}
Wade Guthrie25cdb382012-12-04 14:04:05 -0800505};
506
Wade Guthrie25cdb382012-12-04 14:04:05 -0800507
Wade Guthrie16196242012-11-20 15:53:52 -0800508} // namespace shill
509
510#endif // SHILL_NLATTRIBUTE_H_