blob: 9494790d9e62b040d209178c867883e8202e9cef [file] [log] [blame]
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -07001// 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#include "shill/netlink_message.h"
6
7#include <limits.h>
8#include <netlink/msg.h>
9#include <netlink/netlink.h>
10
Alex Vakulenko8a532292014-06-16 17:18:44 -070011#include <algorithm>
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -070012#include <map>
13#include <string>
14
15#include <base/format_macros.h>
16#include <base/stl_util.h>
Ben Chana0ddf462014-02-06 11:32:42 -080017#include <base/strings/stringprintf.h>
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -070018
19#include "shill/logging.h"
20
21using base::StringAppendF;
22using base::StringPrintf;
23using std::map;
24using std::min;
25using std::string;
26
27namespace shill {
28
29const uint32_t NetlinkMessage::kBroadcastSequenceNumber = 0;
30const uint16_t NetlinkMessage::kIllegalMessageType = UINT16_MAX;
31
32// NetlinkMessage
33
34ByteString NetlinkMessage::EncodeHeader(uint32_t sequence_number) {
35 ByteString result;
36 if (message_type_ == kIllegalMessageType) {
37 LOG(ERROR) << "Message type not set";
38 return result;
39 }
40 sequence_number_ = sequence_number;
41 if (sequence_number_ == kBroadcastSequenceNumber) {
42 LOG(ERROR) << "Couldn't get a legal sequence number";
43 return result;
44 }
45
46 // Build netlink header.
47 nlmsghdr header;
48 size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header));
49 header.nlmsg_len = nlmsghdr_with_pad;
50 header.nlmsg_type = message_type_;
51 header.nlmsg_flags = NLM_F_REQUEST | flags_;
52 header.nlmsg_seq = sequence_number_;
53 header.nlmsg_pid = getpid();
54
55 // Netlink header + pad.
56 result.Append(ByteString(reinterpret_cast<unsigned char *>(&header),
57 sizeof(header)));
58 result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any).
59 return result;
60}
61
62bool NetlinkMessage::InitAndStripHeader(ByteString *input) {
63 if (!input) {
64 LOG(ERROR) << "NULL input";
65 return false;
66 }
67 if (input->GetLength() < sizeof(nlmsghdr)) {
68 LOG(ERROR) << "Insufficient input to extract nlmsghdr";
69 return false;
70 }
71
72 // Read the nlmsghdr.
73 nlmsghdr *header = reinterpret_cast<nlmsghdr *>(input->GetData());
74 message_type_ = header->nlmsg_type;
75 flags_ = header->nlmsg_flags;
76 sequence_number_ = header->nlmsg_seq;
77
78 // Strip the nlmsghdr.
79 input->RemovePrefix(NLMSG_ALIGN(sizeof(struct nlmsghdr)));
80 return true;
81}
82
83bool NetlinkMessage::InitFromNlmsg(const nlmsghdr *const_msg) {
84 if (!const_msg) {
85 LOG(ERROR) << "Null |const_msg| parameter";
86 return false;
87 }
88 ByteString message(reinterpret_cast<const unsigned char *>(const_msg),
89 const_msg->nlmsg_len);
90 if (!InitAndStripHeader(&message)) {
91 return false;
92 }
93 return true;
94}
95
96// static
97void NetlinkMessage::PrintBytes(int log_level, const unsigned char *buf,
98 size_t num_bytes) {
99 SLOG(WiFi, log_level) << "Netlink Message -- Examining Bytes";
100 if (!buf) {
101 SLOG(WiFi, log_level) << "<NULL Buffer>";
102 return;
103 }
104
105 if (num_bytes >= sizeof(nlmsghdr)) {
106 const nlmsghdr *header = reinterpret_cast<const nlmsghdr *>(buf);
107 SLOG(WiFi, log_level) << StringPrintf(
108 "len: %02x %02x %02x %02x = %u bytes",
109 buf[0], buf[1], buf[2], buf[3], header->nlmsg_len);
110
111 SLOG(WiFi, log_level) << StringPrintf(
112 "type | flags: %02x %02x %02x %02x - type:%u flags:%s%s%s%s%s",
113 buf[4], buf[5], buf[6], buf[7], header->nlmsg_type,
114 ((header->nlmsg_flags & NLM_F_REQUEST) ? " REQUEST" : ""),
115 ((header->nlmsg_flags & NLM_F_MULTI) ? " MULTI" : ""),
116 ((header->nlmsg_flags & NLM_F_ACK) ? " ACK" : ""),
117 ((header->nlmsg_flags & NLM_F_ECHO) ? " ECHO" : ""),
118 ((header->nlmsg_flags & NLM_F_DUMP_INTR) ? " BAD-SEQ" : ""));
119
120 SLOG(WiFi, log_level) << StringPrintf(
121 "sequence: %02x %02x %02x %02x = %u",
122 buf[8], buf[9], buf[10], buf[11], header->nlmsg_seq);
123 SLOG(WiFi, log_level) << StringPrintf(
124 "pid: %02x %02x %02x %02x = %u",
125 buf[12], buf[13], buf[14], buf[15], header->nlmsg_pid);
126 buf += sizeof(nlmsghdr);
127 num_bytes -= sizeof(nlmsghdr);
128 } else {
129 SLOG(WiFi, log_level) << "Not enough bytes (" << num_bytes
130 << ") for a complete nlmsghdr (requires "
131 << sizeof(nlmsghdr) << ").";
132 }
133
134 while (num_bytes) {
135 string output;
136 size_t bytes_this_row = min(num_bytes, static_cast<size_t>(32));
137 for (size_t i = 0; i < bytes_this_row; ++i) {
138 StringAppendF(&output, " %02x", *buf++);
139 }
140 SLOG(WiFi, log_level) << output;
141 num_bytes -= bytes_this_row;
142 }
143}
144
145// ErrorAckMessage.
146
147const uint16_t ErrorAckMessage::kMessageType = NLMSG_ERROR;
148
149bool ErrorAckMessage::InitFromNlmsg(const nlmsghdr *const_msg) {
150 if (!const_msg) {
151 LOG(ERROR) << "Null |const_msg| parameter";
152 return false;
153 }
154 ByteString message(reinterpret_cast<const unsigned char *>(const_msg),
155 const_msg->nlmsg_len);
156 if (!InitAndStripHeader(&message)) {
157 return false;
158 }
159
160 // Get the error code from the payload.
161 error_ = *(reinterpret_cast<const uint32_t *>(message.GetConstData()));
162 return true;
163}
164
165ByteString ErrorAckMessage::Encode(uint32_t sequence_number) {
166 LOG(ERROR) << "We're not supposed to send errors or Acks to the kernel";
167 return ByteString();
168}
169
170string ErrorAckMessage::ToString() const {
171 string output;
172 if (error()) {
Wade Guthriebb9fca22013-04-10 17:21:42 -0700173 StringAppendF(&output, "NETLINK_ERROR 0x%" PRIx32 ": %s",
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -0700174 -error_, strerror(-error_));
175 } else {
176 StringAppendF(&output, "ACK");
177 }
178 return output;
179}
180
Wade Guthrie0b1ebf12013-04-12 09:53:33 -0700181void ErrorAckMessage::Print(int header_log_level,
182 int /*detail_log_level*/) const {
183 SLOG(WiFi, header_log_level) << ToString();
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -0700184}
185
186// NoopMessage.
187
188const uint16_t NoopMessage::kMessageType = NLMSG_NOOP;
189
190ByteString NoopMessage::Encode(uint32_t sequence_number) {
191 LOG(ERROR) << "We're not supposed to send NOOP to the kernel";
192 return ByteString();
193}
194
Wade Guthrie0b1ebf12013-04-12 09:53:33 -0700195void NoopMessage::Print(int header_log_level, int /*detail_log_level*/) const {
196 SLOG(WiFi, header_log_level) << ToString();
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -0700197}
198
199// DoneMessage.
200
201const uint16_t DoneMessage::kMessageType = NLMSG_DONE;
202
203ByteString DoneMessage::Encode(uint32_t sequence_number) {
Wade Guthriebbe59f72013-05-21 08:45:39 -0700204 return EncodeHeader(sequence_number);
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -0700205}
206
Wade Guthrie0b1ebf12013-04-12 09:53:33 -0700207void DoneMessage::Print(int header_log_level, int /*detail_log_level*/) const {
208 SLOG(WiFi, header_log_level) << ToString();
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -0700209}
210
211// OverrunMessage.
212
213const uint16_t OverrunMessage::kMessageType = NLMSG_OVERRUN;
214
215ByteString OverrunMessage::Encode(uint32_t sequence_number) {
216 LOG(ERROR) << "We're not supposed to send Overruns to the kernel";
217 return ByteString();
218}
219
Wade Guthrie0b1ebf12013-04-12 09:53:33 -0700220void OverrunMessage::Print(int header_log_level,
221 int /*detail_log_level*/) const {
222 SLOG(WiFi, header_log_level) << ToString();
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -0700223}
224
225// UnknownMessage.
226
227ByteString UnknownMessage::Encode(uint32_t sequence_number) {
228 LOG(ERROR) << "We're not supposed to send UNKNOWN messages to the kernel";
229 return ByteString();
230}
231
Wade Guthrie0b1ebf12013-04-12 09:53:33 -0700232void UnknownMessage::Print(int header_log_level,
233 int /*detail_log_level*/) const {
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -0700234 int total_bytes = message_body_.GetLength();
235 const uint8_t *const_data = message_body_.GetConstData();
236
237 string output = StringPrintf("%d bytes:", total_bytes);
238 for (int i = 0; i < total_bytes; ++i) {
239 StringAppendF(&output, " 0x%02x", const_data[i]);
240 }
Wade Guthrie0b1ebf12013-04-12 09:53:33 -0700241 SLOG(WiFi, header_log_level) << output;
Wade Guthrie0ae4b8e2013-04-10 16:49:15 -0700242}
243
244//
245// Factory class.
246//
247
248bool NetlinkMessageFactory::AddFactoryMethod(uint16_t message_type,
249 FactoryMethod factory) {
250 if (ContainsKey(factories_, message_type)) {
251 LOG(WARNING) << "Message type " << message_type << " already exists.";
252 return false;
253 }
254 if (message_type == NetlinkMessage::kIllegalMessageType) {
255 LOG(ERROR) << "Not installing factory for illegal message type.";
256 return false;
257 }
258 factories_[message_type] = factory;
259 return true;
260}
261
262NetlinkMessage *NetlinkMessageFactory::CreateMessage(
263 const nlmsghdr *const_msg) const {
264 if (!const_msg) {
265 LOG(ERROR) << "NULL |const_msg| parameter";
266 return NULL;
267 }
268
269 scoped_ptr<NetlinkMessage> message;
270
271 if (const_msg->nlmsg_type == NoopMessage::kMessageType) {
272 message.reset(new NoopMessage());
273 } else if (const_msg->nlmsg_type == DoneMessage::kMessageType) {
274 message.reset(new DoneMessage());
275 } else if (const_msg->nlmsg_type == OverrunMessage::kMessageType) {
276 message.reset(new OverrunMessage());
277 } else if (const_msg->nlmsg_type == ErrorAckMessage::kMessageType) {
278 message.reset(new ErrorAckMessage());
279 } else if (ContainsKey(factories_, const_msg->nlmsg_type)) {
280 map<uint16_t, FactoryMethod>::const_iterator factory;
281 factory = factories_.find(const_msg->nlmsg_type);
282 message.reset(factory->second.Run(const_msg));
283 }
284
285 // If no factory exists for this message _or_ if a factory exists but it
286 // failed, there'll be no message. Handle either of those cases, by
287 // creating an |UnknownMessage|.
288 if (!message) {
289 // Casting away constness since, while nlmsg_data doesn't change its
290 // parameter, it also doesn't declare its paramenter as const.
291 nlmsghdr *msg = const_cast<nlmsghdr *>(const_msg);
292 ByteString payload(reinterpret_cast<char *>(nlmsg_data(msg)),
293 nlmsg_datalen(msg));
294 message.reset(new UnknownMessage(msg->nlmsg_type, payload));
295 }
296
297 if (!message->InitFromNlmsg(const_msg)) {
298 LOG(ERROR) << "Message did not initialize properly";
299 return NULL;
300 }
301
302 return message.release();
303}
304
305} // namespace shill.