• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 <libnl++/generic/FamilyTracker.h>
18 
19 #include <android-base/logging.h>
20 
21 namespace android::nl::generic {
22 
track(const Buffer<nlmsghdr> & buffer)23 bool FamilyTracker::track(const Buffer<nlmsghdr>& buffer) {
24     const auto msgMaybe = nl::Message<genlmsghdr>::parse(buffer, {GENL_ID_CTRL});
25     if (!msgMaybe.has_value()) return false;
26 
27     const auto msg = *msgMaybe;
28     if (msg->cmd != CTRL_CMD_NEWFAMILY) return true;
29 
30     const auto familyName = msg.attributes.get<std::string>(CTRL_ATTR_FAMILY_NAME);
31     const auto familyId = msg.attributes.get<uint16_t>(CTRL_ATTR_FAMILY_ID);
32 
33     // TODO(224845900): NETLINK_GENERIC == 16, and (erroneously?) sets off this warning
34     if (familyId < GENL_START_ALLOC) {
35         LOG(WARNING) << "Invalid family ID: " << familyId;
36         return true;
37     }
38 
39     if (familyName == "nl80211") mNl80211FamilyId = familyId;
40 
41     return true;
42 }
43 
parseNl80211(Buffer<nlmsghdr> msg)44 std::optional<Message<genlmsghdr>> FamilyTracker::parseNl80211(Buffer<nlmsghdr> msg) {
45     if (track(msg)) return std::nullopt;
46     if (!mNl80211FamilyId.has_value()) return std::nullopt;
47 
48     return nl::Message<genlmsghdr>::parse(msg, {*mNl80211FamilyId});
49 }
50 
51 }  // namespace android::nl::generic
52