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 <android-base/logging.h>
22
23 #include "wificond/net/kernel-header-latest/nl80211.h"
24 #include "wificond/net/nl80211_packet.h"
25
26 using std::array;
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,array<uint8_t,ETH_ALEN> * bssid)35 bool GetCommonFields(const NL80211Packet* packet,
36 uint32_t* if_index,
37 array<uint8_t, ETH_ALEN>* 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 return roam_event;
108 }
109
InitFromPacket(const NL80211Packet * packet)110 unique_ptr<MlmeDisconnectEvent> MlmeDisconnectEvent::InitFromPacket(
111 const NL80211Packet* packet) {
112 if (packet->GetCommand() != NL80211_CMD_DISCONNECT) {
113 return nullptr;
114 }
115 unique_ptr<MlmeDisconnectEvent> disconnect_event(new MlmeDisconnectEvent());
116 if (!GetCommonFields(packet,
117 &(disconnect_event->interface_index_),
118 &(disconnect_event->bssid_))){
119 return nullptr;
120 }
121 return disconnect_event;
122 }
123
InitFromPacket(const NL80211Packet * packet)124 unique_ptr<MlmeDisassociateEvent> MlmeDisassociateEvent::InitFromPacket(
125 const NL80211Packet* packet) {
126 if (packet->GetCommand() != NL80211_CMD_DISASSOCIATE) {
127 return nullptr;
128 }
129 unique_ptr<MlmeDisassociateEvent> disassociate_event(new MlmeDisassociateEvent());
130 if (!GetCommonFields(packet,
131 &(disassociate_event->interface_index_),
132 &(disassociate_event->bssid_))){
133 return nullptr;
134 }
135 return disassociate_event;
136 }
137
138 } // namespace wificond
139 } // namespace android
140