Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1 | // 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 | // This code is derived from the 'iw' source code. The copyright and license |
| 6 | // of that code is as follows: |
| 7 | // |
| 8 | // Copyright (c) 2007, 2008 Johannes Berg |
| 9 | // Copyright (c) 2007 Andy Lutomirski |
| 10 | // Copyright (c) 2007 Mike Kershaw |
| 11 | // Copyright (c) 2008-2009 Luis R. Rodriguez |
| 12 | // |
| 13 | // Permission to use, copy, modify, and/or distribute this software for any |
| 14 | // purpose with or without fee is hereby granted, provided that the above |
| 15 | // copyright notice and this permission notice appear in all copies. |
| 16 | // |
| 17 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 18 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 19 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 20 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 21 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 22 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 23 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 24 | |
| 25 | #include "shill/netlink_socket.h" |
| 26 | |
| 27 | #include <ctype.h> |
| 28 | #include <errno.h> |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 29 | #include <string.h> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 30 | |
| 31 | #include <net/if.h> |
| 32 | #include <netlink/attr.h> |
| 33 | #include <netlink/genl/ctrl.h> |
| 34 | #include <netlink/genl/family.h> |
| 35 | #include <netlink/genl/genl.h> |
| 36 | #include <netlink/msg.h> |
| 37 | #include <netlink/netlink.h> |
| 38 | |
| 39 | #include <iomanip> |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 40 | #include <string> |
| 41 | |
| 42 | #include <base/eintr_wrapper.h> |
| 43 | #include <base/logging.h> |
| 44 | #include <base/stringprintf.h> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 45 | |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 46 | #include "shill/logging.h" |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 47 | #include "shill/nl80211_message.h" |
| 48 | |
| 49 | using base::StringAppendF; |
| 50 | using std::string; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 51 | |
| 52 | namespace shill { |
| 53 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 54 | NetlinkSocket::~NetlinkSocket() { |
| 55 | if (nl_sock_) { |
| 56 | nl_socket_free(nl_sock_); |
| 57 | nl_sock_ = NULL; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | bool NetlinkSocket::Init() { |
| 62 | nl_sock_ = nl_socket_alloc(); |
| 63 | if (!nl_sock_) { |
| 64 | LOG(ERROR) << "Failed to allocate netlink socket."; |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | if (genl_connect(nl_sock_)) { |
| 69 | LOG(ERROR) << "Failed to connect to generic netlink."; |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 76 | bool NetlinkSocket::SendMessage(Nl80211Message *message) { |
Wade Guthrie | 5d53d49 | 2012-11-07 09:53:31 -0800 | [diff] [blame] | 77 | if (!message) { |
| 78 | LOG(ERROR) << "NULL |message|."; |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 79 | return false; |
Wade Guthrie | 5d53d49 | 2012-11-07 09:53:31 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (!nl_sock_) { |
| 83 | LOG(ERROR) << "Need to initialize the socket first."; |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 84 | return false; |
Wade Guthrie | 5d53d49 | 2012-11-07 09:53:31 -0800 | [diff] [blame] | 85 | } |
| 86 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 87 | ByteString out_msg = message->Encode(family_id()); |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 88 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 89 | if (SLOG_IS_ON(WiFi, 6)) { |
| 90 | SLOG(WiFi, 6) << "NL Message " << message->sequence_number() << " ===>"; |
| 91 | SLOG(WiFi, 6) << " Sending (" << out_msg.GetLength() << " bytes) : " |
| 92 | << message->GenericToString(); |
| 93 | |
| 94 | const unsigned char *out_data = out_msg.GetConstData(); |
| 95 | string output; |
| 96 | for (size_t i = 0; i < out_msg.GetLength(); ++i) { |
| 97 | StringAppendF(&output, " %02x", out_data[i]); |
| 98 | } |
| 99 | SLOG(WiFi, 6) << output; |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 100 | } |
| 101 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 102 | int result = HANDLE_EINTR(send(GetFd(), out_msg.GetConstData(), |
| 103 | out_msg.GetLength(), 0)); |
| 104 | if (!result) { |
| 105 | PLOG(ERROR) << "Send failed."; |
| 106 | return false; |
Wade Guthrie | 5d53d49 | 2012-11-07 09:53:31 -0800 | [diff] [blame] | 107 | } |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 108 | |
| 109 | return true; |
Wade Guthrie | 5d53d49 | 2012-11-07 09:53:31 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 113 | bool NetlinkSocket::DisableSequenceChecking() { |
| 114 | if (!nl_sock_) { |
| 115 | LOG(ERROR) << "NULL socket"; |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | // NOTE: can't use nl_socket_disable_seq_check(); it's not in this version |
| 120 | // of the library. |
| 121 | int result = nl_socket_modify_cb(nl_sock_, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, |
| 122 | NetlinkSocket::IgnoreSequenceCheck, NULL); |
| 123 | if (result) { |
| 124 | LOG(ERROR) << "Failed call to nl_socket_modify_cb: " << result; |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | int NetlinkSocket::GetFd() const { |
| 132 | if (!nl_sock_) { |
| 133 | LOG(ERROR) << "NULL socket"; |
| 134 | return -1; |
| 135 | } |
| 136 | return nl_socket_get_fd(nl_sock_); |
| 137 | } |
| 138 | |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 139 | unsigned int NetlinkSocket::GetSequenceNumber() { |
| 140 | unsigned int number = nl_socket_use_seq(nl_sock_); |
| 141 | if (number == 0) { |
| 142 | number = nl_socket_use_seq(nl_sock_); |
| 143 | } |
| 144 | if (number == 0) { |
| 145 | LOG(WARNING) << "Couldn't get non-zero sequence number"; |
| 146 | number = 1; |
| 147 | } |
| 148 | return number; |
| 149 | } |
| 150 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 151 | int NetlinkSocket::IgnoreSequenceCheck(struct nl_msg *ignored_msg, |
| 152 | void *ignored_arg) { |
| 153 | return NL_OK; // Proceed. |
| 154 | } |
| 155 | |
| 156 | } // namespace shill. |