blob: b6f94383030432d0e240d5bb2c5cb7ab7ab71cb2 [file] [log] [blame]
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -07001// Copyright (c) 2013 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_GENERIC_NETLINK_MESSAGE_H_
6#define SHILL_GENERIC_NETLINK_MESSAGE_H_
7
8#include "shill/attribute_list.h"
9#include "shill/byte_string.h"
10#include "shill/netlink_message.h"
11#include "shill/refptr_types.h"
12
13struct nlmsghdr;
14
15namespace shill {
16
17// Objects of the |GenericNetlinkMessage| type represent messages that contain
18// a |genlmsghdr| after a |nlmsghdr|. These messages seem to all contain a
19// payload that consists of a list of structured attributes (it's possible that
20// some messages might have a genlmsghdr and a different kind of payload but I
21// haven't seen one, yet). The genlmsghdr contains a command id that, when
22// combined with the family_id (from the nlmsghdr), describes the ultimate use
23// for the netlink message.
24//
25// An attribute contains a header and a chunk of data. The header contains an
26// id which is an enumerated value that describes the use of the attribute's
27// data (the datatype of the attribute's data is implied by the attribute id)
28// and the length of the header+data in bytes. The attribute id is,
29// confusingly, called the type (or nla_type -- this is _not_ the data type of
30// the attribute). Each family defines the meaning of the nla_types in the
31// context of messages in that family (for example, the nla_type with the
32// value 3 will always mean the same thing for attributes in the same family).
33// EXCEPTION: Some attributes are nested (that is, they contain a list of other
34// attributes rather than a single value). Each nested attribute defines the
35// meaning of the nla_types in the context of attributes that are nested under
36// this attribute (for example, the nla_type with the value 3 will have a
37// different meaning when nested under another attribute -- that meaning is
38// defined by the attribute under which it is nested). Fun.
39//
40// The GenericNetlink messages look like this:
41//
42// -----+-----+-+-------------------------------------------------+-+--
43// ... | | | message payload | |
44// | | +------+-+----------------------------------------+ |
45// | nl | | | | attributes | |
46// | msg |p| genl |p+-----------+-+---------+-+--------+-----+p| ...
47// | hdr |a| msg |a| struct |p| attrib |p| struct | ... |a|
48// | |d| hdr |d| nlattr |a| payload |a| nlattr | |d|
49// | | | | | |d| |d| | | |
50// -----+-----+-+------+-+-----------+-+---------+-+--------+-----+-+--
51// | ^ | |
52// |<-NLA_HDRLEN->| | |
53// | +---nla_data()
54// |<----nla_attr_size---->| |
55// |<-----nla_total_size---->|
56
57class GenericNetlinkMessage : public NetlinkMessage {
58 public:
59 GenericNetlinkMessage(uint16_t my_message_type, uint8 command,
60 const char *command_string)
61 : NetlinkMessage(my_message_type),
62 attributes_(new AttributeList),
63 command_(command),
64 command_string_(command_string) {}
65 virtual ~GenericNetlinkMessage() {}
66
67 virtual ByteString Encode(uint32_t sequence_number);
68
69 uint8 command() const { return command_; }
70 const char *command_string() const { return command_string_; }
71 AttributeListConstRefPtr const_attributes() const { return attributes_; }
72 AttributeListRefPtr attributes() { return attributes_; }
73
Wade Guthrie0b1ebf12013-04-12 09:53:33 -070074 virtual void Print(int header_log_level, int detail_log_level) const;
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -070075
76 protected:
77 // Returns a string of bytes representing _both_ an |nlmsghdr| and a
78 // |genlmsghdr|, filled-in, and its padding.
79 virtual ByteString EncodeHeader(uint32_t sequence_number);
80 // Reads the |nlmsghdr| and |genlmsghdr| headers and removes them from
81 // |input|.
82 virtual bool InitAndStripHeader(ByteString *input);
83
84 AttributeListRefPtr attributes_;
85 const uint8 command_;
86 const char *command_string_;
87
88 private:
89 DISALLOW_COPY_AND_ASSIGN(GenericNetlinkMessage);
90};
91
92// Control Messages
93
94class ControlNetlinkMessage : public GenericNetlinkMessage {
95 public:
96 static const uint16_t kMessageType;
97 ControlNetlinkMessage(uint8 command, const char *command_string)
98 : GenericNetlinkMessage(kMessageType, command, command_string) {}
99
100 virtual bool InitFromNlmsg(const nlmsghdr *msg);
101
102 // Message factory for all types of Control netlink message.
103 static NetlinkMessage *CreateMessage(const nlmsghdr *const_msg);
Wade Guthrie40d992c2013-04-19 11:10:11 -0700104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(ControlNetlinkMessage);
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -0700107};
108
109class NewFamilyMessage : public ControlNetlinkMessage {
110 public:
111 static const uint8_t kCommand;
112 static const char kCommandString[];
113
114 NewFamilyMessage() : ControlNetlinkMessage(kCommand, kCommandString) {}
115
116 private:
117 DISALLOW_COPY_AND_ASSIGN(NewFamilyMessage);
118};
119
120class GetFamilyMessage : public ControlNetlinkMessage {
121 public:
122 static const uint8_t kCommand;
123 static const char kCommandString[];
124
125 GetFamilyMessage() : ControlNetlinkMessage(kCommand, kCommandString) {}
126
127 private:
128 DISALLOW_COPY_AND_ASSIGN(GetFamilyMessage);
129};
130
Wade Guthrie40d992c2013-04-19 11:10:11 -0700131class UnknownControlMessage : public ControlNetlinkMessage {
132 public:
133 explicit UnknownControlMessage(uint8_t command)
134 : ControlNetlinkMessage(command, "<UNKNOWN CONTROL MESSAGE>"),
135 command_(command) {}
136
137 private:
138 uint8_t command_;
139 DISALLOW_COPY_AND_ASSIGN(UnknownControlMessage);
140};
141
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -0700142} // namespace shill
143
144#endif // SHILL_GENERIC_NETLINK_MESSAGE_H_