1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "wificond/net/mlme_event.h"
18
19 #include <vector>
20
21 #include <linux/nl80211.h>
22
23 #include <android-base/logging.h>
24
25 #include "wificond/net/nl80211_packet.h"
26
27 using std::unique_ptr;
28 using std::vector;
29
30 namespace android {
31 namespace wificond {
32
33 namespace {
34
GetCommonFields(const NL80211Packet * packet,uint32_t * if_index,vector<uint8_t> * bssid)35 bool GetCommonFields(const NL80211Packet* packet,
36 uint32_t* if_index,
37 vector<uint8_t>* bssid) {
38 if (!packet->GetAttributeValue(NL80211_ATTR_IFINDEX, if_index)) {
39 LOG(ERROR) << "Failed to get NL80211_ATTR_IFINDEX";
40 return false;
41 }
42 // Some MLME events do not contain MAC address.
43 if (!packet->GetAttributeValue(NL80211_ATTR_MAC, bssid)) {
44 LOG(DEBUG) << "Failed to get NL80211_ATTR_MAC";
45 }
46 return true;
47 }
48
49 } // namespace
50
InitFromPacket(const NL80211Packet * packet)51 unique_ptr<MlmeAssociateEvent> MlmeAssociateEvent::InitFromPacket(
52 const NL80211Packet* packet) {
53 if (packet->GetCommand() != NL80211_CMD_ASSOCIATE) {
54 return nullptr;
55 }
56 unique_ptr<MlmeAssociateEvent> associate_event(new MlmeAssociateEvent());
57
58 if (!GetCommonFields(packet,
59 &(associate_event->interface_index_),
60 &(associate_event->bssid_))){
61 return nullptr;
62 }
63 // According to wpa_supplicant, status code of an ASSOCIATE event should be
64 // parsed from NL80211_ATTR_FRAME attribute.
65 // TODO(nywang): Parse NL80211_ATTR_FRAME 80211 management frame and get
66 // status code.
67 associate_event->status_code_ = 0;
68 associate_event->is_timeout_ = packet->HasAttribute(NL80211_ATTR_TIMED_OUT);
69
70 return associate_event;
71 }
72
InitFromPacket(const NL80211Packet * packet)73 unique_ptr<MlmeConnectEvent> MlmeConnectEvent::InitFromPacket(
74 const NL80211Packet* packet) {
75 if (packet->GetCommand() != NL80211_CMD_CONNECT) {
76 return nullptr;
77 }
78 unique_ptr<MlmeConnectEvent> connect_event(new MlmeConnectEvent());
79 if (!GetCommonFields(packet,
80 &(connect_event->interface_index_),
81 &(connect_event->bssid_))){
82 return nullptr;
83 }
84
85 if (!packet->GetAttributeValue(NL80211_ATTR_STATUS_CODE,
86 &(connect_event->status_code_))) {
87 LOG(WARNING) << "Failed to get NL80211_ATTR_STATUS_CODE";
88 connect_event->status_code_ = 0;
89 }
90 connect_event->is_timeout_ = packet->HasAttribute(NL80211_ATTR_TIMED_OUT);
91
92 return connect_event;
93 }
94
InitFromPacket(const NL80211Packet * packet)95 unique_ptr<MlmeRoamEvent> MlmeRoamEvent::InitFromPacket(
96 const NL80211Packet* packet) {
97 if (packet->GetCommand() != NL80211_CMD_ROAM) {
98 return nullptr;
99 }
100 unique_ptr<MlmeRoamEvent> roam_event(new MlmeRoamEvent());
101 if (!GetCommonFields(packet,
102 &(roam_event->interface_index_),
103 &(roam_event->bssid_))){
104 return nullptr;
105 }
106
107 if (!packet->GetAttributeValue(NL80211_ATTR_STATUS_CODE,
108 &(roam_event->status_code_))) {
109 LOG(WARNING) << "Failed to get NL80211_ATTR_STATUS_CODE";
110 roam_event->status_code_ = 0;
111 }
112
113 return roam_event;
114 }
115
InitFromPacket(const NL80211Packet * packet)116 unique_ptr<MlmeDisconnectEvent> MlmeDisconnectEvent::InitFromPacket(
117 const NL80211Packet* packet) {
118 if (packet->GetCommand() != NL80211_CMD_DISCONNECT) {
119 return nullptr;
120 }
121 unique_ptr<MlmeDisconnectEvent> disconnect_event(new MlmeDisconnectEvent());
122 if (!GetCommonFields(packet,
123 &(disconnect_event->interface_index_),
124 &(disconnect_event->bssid_))){
125 return nullptr;
126 }
127 return disconnect_event;
128 }
129
InitFromPacket(const NL80211Packet * packet)130 unique_ptr<MlmeDisassociateEvent> MlmeDisassociateEvent::InitFromPacket(
131 const NL80211Packet* packet) {
132 if (packet->GetCommand() != NL80211_CMD_DISASSOCIATE) {
133 return nullptr;
134 }
135 unique_ptr<MlmeDisassociateEvent> disassociate_event(new MlmeDisassociateEvent());
136 if (!GetCommonFields(packet,
137 &(disassociate_event->interface_index_),
138 &(disassociate_event->bssid_))){
139 return nullptr;
140 }
141 return disassociate_event;
142 }
143
144 } // namespace wificond
145 } // namespace android
146