Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -0700 | [diff] [blame] | 1 | // 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 | |
| 13 | struct nlmsghdr; |
| 14 | |
| 15 | namespace 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 | |
| 57 | class 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 | |
| 74 | virtual void Print(int log_level) const; |
| 75 | |
| 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 | |
| 94 | class 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); |
| 104 | }; |
| 105 | |
| 106 | class NewFamilyMessage : public ControlNetlinkMessage { |
| 107 | public: |
| 108 | static const uint8_t kCommand; |
| 109 | static const char kCommandString[]; |
| 110 | |
| 111 | NewFamilyMessage() : ControlNetlinkMessage(kCommand, kCommandString) {} |
| 112 | |
| 113 | private: |
| 114 | DISALLOW_COPY_AND_ASSIGN(NewFamilyMessage); |
| 115 | }; |
| 116 | |
| 117 | class GetFamilyMessage : public ControlNetlinkMessage { |
| 118 | public: |
| 119 | static const uint8_t kCommand; |
| 120 | static const char kCommandString[]; |
| 121 | |
| 122 | GetFamilyMessage() : ControlNetlinkMessage(kCommand, kCommandString) {} |
| 123 | |
| 124 | private: |
| 125 | DISALLOW_COPY_AND_ASSIGN(GetFamilyMessage); |
| 126 | }; |
| 127 | |
| 128 | } // namespace shill |
| 129 | |
| 130 | #endif // SHILL_GENERIC_NETLINK_MESSAGE_H_ |