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