Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -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 | #include "shill/callback80211_metrics.h" |
| 6 | |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 7 | #include "shill/ieee80211.h" |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 8 | #include "shill/logging.h" |
| 9 | #include "shill/metrics.h" |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 10 | #include "shill/netlink_manager.h" |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 11 | #include "shill/nl80211_message.h" |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 12 | |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 13 | namespace shill { |
| 14 | |
Wade Guthrie | c272896 | 2013-07-10 09:32:16 -0700 | [diff] [blame] | 15 | Callback80211Metrics::Callback80211Metrics(Metrics *metrics) |
Wade Guthrie | 318445d | 2013-05-16 14:05:28 -0700 | [diff] [blame] | 16 | : metrics_(metrics) {} |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 17 | |
Wade Guthrie | c272896 | 2013-07-10 09:32:16 -0700 | [diff] [blame] | 18 | IEEE_80211::WiFiReasonCode Callback80211Metrics::WiFiReasonCodeFromUint16( |
| 19 | uint16_t reason) const { |
| 20 | IEEE_80211::WiFiReasonCode reason_enum = IEEE_80211::kReasonCodeInvalid; |
| 21 | if (reason == IEEE_80211::kReasonCodeReserved0 || |
| 22 | reason == IEEE_80211::kReasonCodeReserved12 || |
| 23 | (reason >= IEEE_80211::kReasonCodeReservedBegin25 && |
| 24 | reason <= IEEE_80211::kReasonCodeReservedEnd31) || |
| 25 | (reason >= IEEE_80211::kReasonCodeReservedBegin40 && |
| 26 | reason <= IEEE_80211::kReasonCodeReservedEnd44) || |
| 27 | reason >= IEEE_80211::kReasonCodeMax) { |
| 28 | SLOG(WiFi, 1) << "Invalid reason code in disconnect message"; |
| 29 | reason_enum = IEEE_80211::kReasonCodeInvalid; |
| 30 | } else { |
| 31 | reason_enum = static_cast<IEEE_80211::WiFiReasonCode>(reason); |
| 32 | } |
| 33 | return reason_enum; |
| 34 | } |
| 35 | |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 36 | void Callback80211Metrics::CollectDisconnectStatistics( |
| 37 | const NetlinkMessage &netlink_message) { |
Wade Guthrie | c272896 | 2013-07-10 09:32:16 -0700 | [diff] [blame] | 38 | if (!metrics_) { |
| 39 | return; |
| 40 | } |
| 41 | // We only handle disconnect and deauthenticate messages, both of which are |
| 42 | // nl80211 messages. |
Wade Guthrie | 318445d | 2013-05-16 14:05:28 -0700 | [diff] [blame] | 43 | if (netlink_message.message_type() != Nl80211Message::GetMessageType()) { |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 44 | return; |
| 45 | } |
| 46 | const Nl80211Message &message = |
| 47 | * reinterpret_cast<const Nl80211Message *>(&netlink_message); |
| 48 | |
Wade Guthrie | c272896 | 2013-07-10 09:32:16 -0700 | [diff] [blame] | 49 | // Station-instigated disconnects provide their information in the |
| 50 | // deauthenticate message but AP-instigated disconnects provide it in the |
| 51 | // disconnect message. |
| 52 | uint16_t reason = IEEE_80211::kReasonCodeUnspecified; |
| 53 | if (message.command() == DeauthenticateMessage::kCommand) { |
Wade Guthrie | 0b1ebf1 | 2013-04-12 09:53:33 -0700 | [diff] [blame] | 54 | SLOG(WiFi, 3) << "Handling Deauthenticate Message"; |
Wade Guthrie | c272896 | 2013-07-10 09:32:16 -0700 | [diff] [blame] | 55 | message.Print(3, 3); |
| 56 | // If there's no frame, this is probably an AP-caused disconnect and |
| 57 | // there'll be a disconnect message to tell us about that. |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 58 | ByteString rawdata; |
Wade Guthrie | c272896 | 2013-07-10 09:32:16 -0700 | [diff] [blame] | 59 | if (!message.const_attributes()->GetRawAttributeValue(NL80211_ATTR_FRAME, |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 60 | &rawdata)) { |
Wade Guthrie | c272896 | 2013-07-10 09:32:16 -0700 | [diff] [blame] | 61 | SLOG(WiFi, 5) << "No frame in deauthenticate message, ignoring"; |
| 62 | return; |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 63 | } |
Wade Guthrie | c272896 | 2013-07-10 09:32:16 -0700 | [diff] [blame] | 64 | Nl80211Frame frame(rawdata); |
| 65 | reason = frame.reason(); |
| 66 | } else if (message.command() == DisconnectMessage::kCommand) { |
| 67 | SLOG(WiFi, 3) << "Handling Disconnect Message"; |
| 68 | message.Print(3, 3); |
| 69 | // If there's no reason code, this is probably a STA-caused disconnect and |
| 70 | // there was be a disconnect message to tell us about that. |
| 71 | if (!message.const_attributes()->GetU16AttributeValue( |
| 72 | NL80211_ATTR_REASON_CODE, &reason)) { |
| 73 | SLOG(WiFi, 5) << "No reason code in disconnect message, ignoring"; |
| 74 | return; |
| 75 | } |
| 76 | } else { |
| 77 | return; |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 78 | } |
Wade Guthrie | c272896 | 2013-07-10 09:32:16 -0700 | [diff] [blame] | 79 | |
| 80 | IEEE_80211::WiFiReasonCode reason_enum = WiFiReasonCodeFromUint16(reason); |
| 81 | |
| 82 | Metrics::WiFiDisconnectByWhom by_whom = |
| 83 | message.const_attributes()->IsFlagAttributeTrue( |
| 84 | NL80211_ATTR_DISCONNECTED_BY_AP) ? Metrics::kDisconnectedByAp : |
| 85 | Metrics::kDisconnectedNotByAp; |
| 86 | SLOG(WiFi, 1) << "Notify80211Disconnect by " << (by_whom ? "station" : "AP") |
| 87 | << " because:" << reason_enum; |
| 88 | metrics_->Notify80211Disconnect(by_whom, reason_enum); |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 91 | } // namespace shill. |