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 #define LOG_TAG "mac80211_create_radios"
18 
19 #include <memory>
20 #include <log/log.h>
21 #include <netlink/genl/ctrl.h>
22 #include <netlink/genl/genl.h>
23 #include <netlink/netlink.h>
24 #include <net/ethernet.h>
25 
26 enum {
27     HWSIM_CMD_UNSPEC,
28     HWSIM_CMD_REGISTER,
29     HWSIM_CMD_FRAME,
30     HWSIM_CMD_TX_INFO_FRAME,
31     HWSIM_CMD_NEW_RADIO,
32     HWSIM_CMD_DEL_RADIO,
33     HWSIM_CMD_GET_RADIO,
34 };
35 
36 enum {
37     HWSIM_ATTR_UNSPEC,
38     HWSIM_ATTR_ADDR_RECEIVER,
39     HWSIM_ATTR_ADDR_TRANSMITTER,
40     HWSIM_ATTR_FRAME,
41     HWSIM_ATTR_FLAGS,
42     HWSIM_ATTR_RX_RATE,
43     HWSIM_ATTR_SIGNAL,
44     HWSIM_ATTR_TX_INFO,
45     HWSIM_ATTR_COOKIE,
46     HWSIM_ATTR_CHANNELS,
47     HWSIM_ATTR_RADIO_ID,
48     HWSIM_ATTR_REG_HINT_ALPHA2,
49     HWSIM_ATTR_REG_CUSTOM_REG,
50     HWSIM_ATTR_REG_STRICT_REG,
51     HWSIM_ATTR_SUPPORT_P2P_DEVICE,
52     HWSIM_ATTR_USE_CHANCTX,
53     HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE,
54     HWSIM_ATTR_RADIO_NAME,
55     HWSIM_ATTR_NO_VIF,
56     HWSIM_ATTR_FREQ,
57     HWSIM_ATTR_PAD,
58     HWSIM_ATTR_TX_INFO_FLAGS,
59     HWSIM_ATTR_PERM_ADDR,
60     HWSIM_ATTR_IFTYPE_SUPPORT,
61     HWSIM_ATTR_CIPHER_SUPPORT,
62 };
63 
64 struct nl_sock_deleter {
operator ()nl_sock_deleter65     void operator()(struct nl_sock* x) const { nl_socket_free(x); }
66 };
67 
68 struct nl_msg_deleter {
operator ()nl_msg_deleter69     void operator()(struct nl_msg* x) const { nlmsg_free(x); }
70 };
71 
72 constexpr char kHwSimFamilyName[] = "MAC80211_HWSIM";
73 constexpr int kHwSimVersion = 1;
74 
nlErrStr(const int e)75 const char* nlErrStr(const int e) { return (e < 0) ? nl_geterror(e) : ""; }
76 
77 #define RETURN(R) return (R);
78 #define RETURN_ERROR(C, R) \
79     do { \
80         ALOGE("%s:%d '%s' failed", __func__, __LINE__, C); \
81         return (R); \
82     } while (false);
83 #define RETURN_NL_ERROR(C, NLR, R) \
84     do { \
85         ALOGE("%s:%d '%s' failed with '%s'", __func__, __LINE__, C, nlErrStr((NLR))); \
86         return (R); \
87     } while (false);
88 
parseInt(const char * str,int * result)89 int parseInt(const char* str, int* result) { return sscanf(str, "%d", result); }
90 
createNlMessage(const int family,const int cmd)91 std::unique_ptr<struct nl_msg, nl_msg_deleter> createNlMessage(
92         const int family,
93         const int cmd) {
94     std::unique_ptr<struct nl_msg, nl_msg_deleter> msg(nlmsg_alloc());
95     if (!msg) { RETURN_ERROR("nlmsg_alloc", nullptr); }
96 
97     void* user = genlmsg_put(msg.get(), NL_AUTO_PORT, NL_AUTO_SEQ, family, 0,
98                        NLM_F_REQUEST, cmd, kHwSimVersion);
99     if (!user) { RETURN_ERROR("genlmsg_put", nullptr); }
100 
101     RETURN(msg);
102 }
103 
104 std::unique_ptr<struct nl_msg, nl_msg_deleter>
buildCreateRadioMessage(const int family,const uint8_t mac[ETH_ALEN])105 buildCreateRadioMessage(const int family, const uint8_t mac[ETH_ALEN]) {
106     std::unique_ptr<struct nl_msg, nl_msg_deleter> msg =
107         createNlMessage(family, HWSIM_CMD_NEW_RADIO);
108     if (!msg) { RETURN(nullptr); }
109 
110     int ret;
111     ret = nla_put(msg.get(), HWSIM_ATTR_PERM_ADDR, ETH_ALEN, mac);
112     if (ret) { RETURN_NL_ERROR("nla_put(HWSIM_ATTR_PERM_ADDR)", ret, nullptr); }
113 
114     ret = nla_put_flag(msg.get(), HWSIM_ATTR_SUPPORT_P2P_DEVICE);
115     if (ret) { RETURN_NL_ERROR("nla_put(HWSIM_ATTR_SUPPORT_P2P_DEVICE)", ret, nullptr); }
116 
117     RETURN(msg);
118 }
119 
createRadios(struct nl_sock * socket,const int netlinkFamily,const int nRadios,const int macPrefix)120 int createRadios(struct nl_sock* socket, const int netlinkFamily,
121                  const int nRadios, const int macPrefix) {
122     uint8_t mac[ETH_ALEN] = {};
123     mac[0] = 0x02;
124     mac[1] = (macPrefix >> CHAR_BIT) & 0xFF;
125     mac[2] = macPrefix & 0xFF;
126 
127     for (int idx = 0; idx < nRadios; ++idx) {
128         mac[4] = idx;
129 
130         std::unique_ptr<struct nl_msg, nl_msg_deleter> msg =
131             buildCreateRadioMessage(netlinkFamily, mac);
132         if (msg) {
133             int ret = nl_send_auto(socket, msg.get());
134             if (ret < 0) { RETURN_NL_ERROR("nl_send_auto", ret, 1); }
135         } else {
136             RETURN(1);
137         }
138     }
139 
140     RETURN(0);
141 }
142 
manageRadios(const int nRadios,const int macPrefix)143 int manageRadios(const int nRadios, const int macPrefix) {
144     std::unique_ptr<struct nl_sock, nl_sock_deleter> socket(nl_socket_alloc());
145     if (!socket) { RETURN_ERROR("nl_socket_alloc", 1); }
146 
147     int ret;
148     ret = genl_connect(socket.get());
149     if (ret) { RETURN_NL_ERROR("genl_connect", ret, 1); }
150 
151     const int netlinkFamily = genl_ctrl_resolve(socket.get(), kHwSimFamilyName);
152     if (netlinkFamily < 0) { RETURN_NL_ERROR("genl_ctrl_resolve", ret, 1); }
153 
154     ret = createRadios(socket.get(), netlinkFamily, nRadios, macPrefix);
155     if (ret) { RETURN(ret); }
156 
157     RETURN(0);
158 }
159 
printUsage(FILE * dst,const int ret)160 int printUsage(FILE* dst, const int ret) {
161     fprintf(dst, "%s",
162     "Usage:\n"
163     "   mac80211_create_radios n_radios mac_prefix\n"
164     "   where\n"
165     "       n_radios - int, [1,100], e.g. 2;\n"
166     "       mac_prefix - int, [0, 65535], e.g. 5555.\n\n"
167     "   mac80211_create_radios will delete all existing radios and\n"
168     "   create n_radios with MAC addresses\n"
169     "   02:pp:pp:00:nn:00, where nn is incremented (from zero)\n");
170 
171     return ret;
172 }
173 
main(int argc,char * argv[])174 int main(int argc, char* argv[]) {
175     if (argc != 3) { return printUsage(stdout, 0); }
176 
177     int nRadios;
178     if (!parseInt(argv[1], &nRadios)) { return printUsage(stderr, 1); }
179     if (nRadios < 1) { return printUsage(stderr, 1); }
180     if (nRadios > 100) { return printUsage(stderr, 1); }
181 
182     int macPrefix;
183     if (!parseInt(argv[2], &macPrefix)) { return printUsage(stderr, 1); }
184     if (macPrefix < 0) { return printUsage(stderr, 1); }
185     if (macPrefix > UINT16_MAX) { return printUsage(stderr, 1); }
186 
187     return manageRadios(nRadios, macPrefix);
188 }
189