]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
b492aea0 | 2 | |
b492aea0 | 3 | #include "log.h" |
abad436d | 4 | #include "string-table.h" |
56fdc16d | 5 | #include "string-util.h" |
b492aea0 ZJS |
6 | #include "wifi-util.h" |
7 | ||
ae2b86de | 8 | int wifi_get_interface(sd_netlink *genl, int ifindex, enum nl80211_iftype *ret_iftype, char **ret_ssid) { |
b492aea0 | 9 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *reply = NULL; |
ae2b86de | 10 | _cleanup_free_ char *ssid = NULL; |
56fdc16d | 11 | const char *family; |
ae2b86de | 12 | uint32_t iftype; |
f3e235ff | 13 | size_t len; |
b492aea0 ZJS |
14 | int r; |
15 | ||
cc17c4c9 YW |
16 | assert(genl); |
17 | assert(ifindex > 0); | |
18 | ||
56fdc16d | 19 | r = sd_genl_message_new(genl, NL80211_GENL_NAME, NL80211_CMD_GET_INTERFACE, &m); |
b492aea0 ZJS |
20 | if (r < 0) |
21 | return log_debug_errno(r, "Failed to create generic netlink message: %m"); | |
22 | ||
23 | r = sd_netlink_message_append_u32(m, NL80211_ATTR_IFINDEX, ifindex); | |
24 | if (r < 0) | |
25 | return log_debug_errno(r, "Could not append NL80211_ATTR_IFINDEX attribute: %m"); | |
26 | ||
27 | r = sd_netlink_call(genl, m, 0, &reply); | |
cc17c4c9 YW |
28 | if (r == -ENODEV) { |
29 | /* For obsolete WEXT driver. */ | |
30 | log_debug_errno(r, "Failed to request information about wifi interface %d. " | |
31 | "The device doesn't seem to have nl80211 interface. Ignoring.", | |
32 | ifindex); | |
33 | goto nodata; | |
34 | } | |
b492aea0 ZJS |
35 | if (r < 0) |
36 | return log_debug_errno(r, "Failed to request information about wifi interface %d: %m", ifindex); | |
a66a402d | 37 | if (!reply) { |
66205cb3 | 38 | log_debug("No reply received to request for information about wifi interface %d, ignoring.", ifindex); |
cc17c4c9 | 39 | goto nodata; |
a66a402d | 40 | } |
b492aea0 ZJS |
41 | |
42 | r = sd_netlink_message_get_errno(reply); | |
43 | if (r < 0) | |
44 | return log_debug_errno(r, "Failed to get information about wifi interface %d: %m", ifindex); | |
45 | ||
56fdc16d | 46 | r = sd_genl_message_get_family_name(genl, reply, &family); |
b492aea0 ZJS |
47 | if (r < 0) |
48 | return log_debug_errno(r, "Failed to determine genl family: %m"); | |
56fdc16d YW |
49 | if (!streq(family, NL80211_GENL_NAME)) { |
50 | log_debug("Received message of unexpected genl family '%s', ignoring.", family); | |
cc17c4c9 | 51 | goto nodata; |
b492aea0 ZJS |
52 | } |
53 | ||
ae2b86de YW |
54 | r = sd_netlink_message_read_u32(reply, NL80211_ATTR_IFTYPE, &iftype); |
55 | if (r < 0) | |
56 | return log_debug_errno(r, "Failed to get NL80211_ATTR_IFTYPE attribute: %m"); | |
78404d22 | 57 | |
70c24d0d | 58 | r = sd_netlink_message_read_data(reply, NL80211_ATTR_SSID, &len, (void**) &ssid); |
ae2b86de YW |
59 | if (r < 0 && r != -ENODATA) |
60 | return log_debug_errno(r, "Failed to get NL80211_ATTR_SSID attribute: %m"); | |
f3e235ff YW |
61 | if (r >= 0) { |
62 | if (len == 0) { | |
0923b425 | 63 | log_debug("SSID has zero length, ignoring it."); |
f3e235ff YW |
64 | ssid = mfree(ssid); |
65 | } else if (strlen_ptr(ssid) != len) { | |
0923b425 | 66 | log_debug("SSID contains NUL characters, ignoring it."); |
f3e235ff YW |
67 | ssid = mfree(ssid); |
68 | } | |
69 | } | |
78404d22 | 70 | |
ae2b86de YW |
71 | if (ret_iftype) |
72 | *ret_iftype = iftype; | |
73 | ||
74 | if (ret_ssid) | |
75 | *ret_ssid = TAKE_PTR(ssid); | |
b492aea0 | 76 | |
cc17c4c9 YW |
77 | return 1; |
78 | ||
79 | nodata: | |
ae2b86de YW |
80 | if (ret_iftype) |
81 | *ret_iftype = 0; | |
82 | if (ret_ssid) | |
83 | *ret_ssid = NULL; | |
cc17c4c9 | 84 | return 0; |
b492aea0 ZJS |
85 | } |
86 | ||
ae2b86de | 87 | int wifi_get_station(sd_netlink *genl, int ifindex, struct ether_addr *ret_bssid) { |
b492aea0 | 88 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *reply = NULL; |
56fdc16d | 89 | const char *family; |
b492aea0 ZJS |
90 | int r; |
91 | ||
cc17c4c9 YW |
92 | assert(genl); |
93 | assert(ifindex > 0); | |
ae2b86de | 94 | assert(ret_bssid); |
cc17c4c9 | 95 | |
56fdc16d | 96 | r = sd_genl_message_new(genl, NL80211_GENL_NAME, NL80211_CMD_GET_STATION, &m); |
b492aea0 ZJS |
97 | if (r < 0) |
98 | return log_debug_errno(r, "Failed to create generic netlink message: %m"); | |
99 | ||
100 | r = sd_netlink_message_set_flags(m, NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP); | |
101 | if (r < 0) | |
102 | return log_debug_errno(r, "Failed to set dump flag: %m"); | |
103 | ||
104 | r = sd_netlink_message_append_u32(m, NL80211_ATTR_IFINDEX, ifindex); | |
105 | if (r < 0) | |
106 | return log_debug_errno(r, "Could not append NL80211_ATTR_IFINDEX attribute: %m"); | |
107 | ||
108 | r = sd_netlink_call(genl, m, 0, &reply); | |
109 | if (r < 0) | |
110 | return log_debug_errno(r, "Failed to request information about wifi station: %m"); | |
a66a402d | 111 | if (!reply) { |
66205cb3 | 112 | log_debug("No reply received to request for information about wifi station, ignoring."); |
cc17c4c9 | 113 | goto nodata; |
a66a402d | 114 | } |
b492aea0 ZJS |
115 | |
116 | r = sd_netlink_message_get_errno(reply); | |
117 | if (r < 0) | |
118 | return log_debug_errno(r, "Failed to get information about wifi station: %m"); | |
119 | ||
56fdc16d | 120 | r = sd_genl_message_get_family_name(genl, reply, &family); |
b492aea0 ZJS |
121 | if (r < 0) |
122 | return log_debug_errno(r, "Failed to determine genl family: %m"); | |
56fdc16d YW |
123 | if (!streq(family, NL80211_GENL_NAME)) { |
124 | log_debug("Received message of unexpected genl family '%s', ignoring.", family); | |
cc17c4c9 | 125 | goto nodata; |
b492aea0 ZJS |
126 | } |
127 | ||
ae2b86de | 128 | r = sd_netlink_message_read_ether_addr(reply, NL80211_ATTR_MAC, ret_bssid); |
cc17c4c9 YW |
129 | if (r == -ENODATA) |
130 | goto nodata; | |
131 | if (r < 0) | |
b492aea0 ZJS |
132 | return log_debug_errno(r, "Failed to get NL80211_ATTR_MAC attribute: %m"); |
133 | ||
cc17c4c9 YW |
134 | return 1; |
135 | ||
136 | nodata: | |
ae2b86de | 137 | *ret_bssid = ETHER_ADDR_NULL; |
cc17c4c9 | 138 | return 0; |
b492aea0 | 139 | } |
abad436d YW |
140 | |
141 | static const char * const nl80211_iftype_table[NUM_NL80211_IFTYPES] = { | |
142 | [NL80211_IFTYPE_ADHOC] = "ad-hoc", | |
143 | [NL80211_IFTYPE_STATION] = "station", | |
144 | [NL80211_IFTYPE_AP] = "ap", | |
145 | [NL80211_IFTYPE_AP_VLAN] = "ap-vlan", | |
146 | [NL80211_IFTYPE_WDS] = "wds", | |
147 | [NL80211_IFTYPE_MONITOR] = "monitor", | |
148 | [NL80211_IFTYPE_MESH_POINT] = "mesh-point", | |
149 | [NL80211_IFTYPE_P2P_CLIENT] = "p2p-client", | |
150 | [NL80211_IFTYPE_P2P_GO] = "p2p-go", | |
151 | [NL80211_IFTYPE_P2P_DEVICE] = "p2p-device", | |
152 | [NL80211_IFTYPE_OCB] = "ocb", | |
153 | [NL80211_IFTYPE_NAN] = "nan", | |
154 | }; | |
155 | ||
b4d55e83 | 156 | DEFINE_STRING_TABLE_LOOKUP(nl80211_iftype, enum nl80211_iftype); |
16653f97 YW |
157 | |
158 | static const char * const nl80211_cmd_table[__NL80211_CMD_AFTER_LAST] = { | |
159 | [NL80211_CMD_GET_WIPHY] = "get_wiphy", | |
160 | [NL80211_CMD_SET_WIPHY] = "set_wiphy", | |
161 | [NL80211_CMD_NEW_WIPHY] = "new_wiphy", | |
162 | [NL80211_CMD_DEL_WIPHY] = "del_wiphy", | |
163 | [NL80211_CMD_GET_INTERFACE] = "get_interface", | |
164 | [NL80211_CMD_SET_INTERFACE] = "set_interface", | |
165 | [NL80211_CMD_NEW_INTERFACE] = "new_interface", | |
166 | [NL80211_CMD_DEL_INTERFACE] = "del_interface", | |
167 | [NL80211_CMD_GET_KEY] = "get_key", | |
168 | [NL80211_CMD_SET_KEY] = "set_key", | |
169 | [NL80211_CMD_NEW_KEY] = "new_key", | |
170 | [NL80211_CMD_DEL_KEY] = "del_key", | |
171 | [NL80211_CMD_GET_BEACON] = "get_beacon", | |
172 | [NL80211_CMD_SET_BEACON] = "set_beacon", | |
173 | [NL80211_CMD_START_AP] = "start_ap", | |
174 | [NL80211_CMD_STOP_AP] = "stop_ap", | |
175 | [NL80211_CMD_GET_STATION] = "get_station", | |
176 | [NL80211_CMD_SET_STATION] = "set_station", | |
177 | [NL80211_CMD_NEW_STATION] = "new_station", | |
178 | [NL80211_CMD_DEL_STATION] = "del_station", | |
179 | [NL80211_CMD_GET_MPATH] = "get_mpath", | |
180 | [NL80211_CMD_SET_MPATH] = "set_mpath", | |
181 | [NL80211_CMD_NEW_MPATH] = "new_mpath", | |
182 | [NL80211_CMD_DEL_MPATH] = "del_mpath", | |
183 | [NL80211_CMD_SET_BSS] = "set_bss", | |
184 | [NL80211_CMD_SET_REG] = "set_reg", | |
185 | [NL80211_CMD_REQ_SET_REG] = "req_set_reg", | |
186 | [NL80211_CMD_GET_MESH_CONFIG] = "get_mesh_config", | |
187 | [NL80211_CMD_SET_MESH_CONFIG] = "set_mesh_config", | |
188 | [NL80211_CMD_SET_MGMT_EXTRA_IE] = "set_mgmt_extra_ie", | |
189 | [NL80211_CMD_GET_REG] = "get_reg", | |
190 | [NL80211_CMD_GET_SCAN] = "get_scan", | |
191 | [NL80211_CMD_TRIGGER_SCAN] = "trigger_scan", | |
192 | [NL80211_CMD_NEW_SCAN_RESULTS] = "new_scan_results", | |
193 | [NL80211_CMD_SCAN_ABORTED] = "scan_aborted", | |
194 | [NL80211_CMD_REG_CHANGE] = "reg_change", | |
195 | [NL80211_CMD_AUTHENTICATE] = "authenticate", | |
196 | [NL80211_CMD_ASSOCIATE] = "associate", | |
197 | [NL80211_CMD_DEAUTHENTICATE] = "deauthenticate", | |
198 | [NL80211_CMD_DISASSOCIATE] = "disassociate", | |
199 | [NL80211_CMD_MICHAEL_MIC_FAILURE] = "michael_mic_failure", | |
200 | [NL80211_CMD_REG_BEACON_HINT] = "reg_beacon_hint", | |
201 | [NL80211_CMD_JOIN_IBSS] = "join_ibss", | |
202 | [NL80211_CMD_LEAVE_IBSS] = "leave_ibss", | |
203 | [NL80211_CMD_TESTMODE] = "testmode", | |
204 | [NL80211_CMD_CONNECT] = "connect", | |
205 | [NL80211_CMD_ROAM] = "roam", | |
206 | [NL80211_CMD_DISCONNECT] = "disconnect", | |
207 | [NL80211_CMD_SET_WIPHY_NETNS] = "set_wiphy_netns", | |
208 | [NL80211_CMD_GET_SURVEY] = "get_survey", | |
209 | [NL80211_CMD_NEW_SURVEY_RESULTS] = "new_survey_results", | |
210 | [NL80211_CMD_SET_PMKSA] = "set_pmksa", | |
211 | [NL80211_CMD_DEL_PMKSA] = "del_pmksa", | |
212 | [NL80211_CMD_FLUSH_PMKSA] = "flush_pmksa", | |
213 | [NL80211_CMD_REMAIN_ON_CHANNEL] = "remain_on_channel", | |
214 | [NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = "cancel_remain_on_channel", | |
215 | [NL80211_CMD_SET_TX_BITRATE_MASK] = "set_tx_bitrate_mask", | |
216 | [NL80211_CMD_REGISTER_FRAME] = "register_frame", | |
217 | [NL80211_CMD_FRAME] = "frame", | |
218 | [NL80211_CMD_FRAME_TX_STATUS] = "frame_tx_status", | |
219 | [NL80211_CMD_SET_POWER_SAVE] = "set_power_save", | |
220 | [NL80211_CMD_GET_POWER_SAVE] = "get_power_save", | |
221 | [NL80211_CMD_SET_CQM] = "set_cqm", | |
222 | [NL80211_CMD_NOTIFY_CQM] = "notify_cqm", | |
223 | [NL80211_CMD_SET_CHANNEL] = "set_channel", | |
224 | [NL80211_CMD_SET_WDS_PEER] = "set_wds_peer", | |
225 | [NL80211_CMD_FRAME_WAIT_CANCEL] = "frame_wait_cancel", | |
226 | [NL80211_CMD_JOIN_MESH] = "join_mesh", | |
227 | [NL80211_CMD_LEAVE_MESH] = "leave_mesh", | |
228 | [NL80211_CMD_UNPROT_DEAUTHENTICATE] = "unprot_deauthenticate", | |
229 | [NL80211_CMD_UNPROT_DISASSOCIATE] = "unprot_disassociate", | |
230 | [NL80211_CMD_NEW_PEER_CANDIDATE] = "new_peer_candidate", | |
231 | [NL80211_CMD_GET_WOWLAN] = "get_wowlan", | |
232 | [NL80211_CMD_SET_WOWLAN] = "set_wowlan", | |
233 | [NL80211_CMD_START_SCHED_SCAN] = "start_sched_scan", | |
234 | [NL80211_CMD_STOP_SCHED_SCAN] = "stop_sched_scan", | |
235 | [NL80211_CMD_SCHED_SCAN_RESULTS] = "sched_scan_results", | |
236 | [NL80211_CMD_SCHED_SCAN_STOPPED] = "sched_scan_stopped", | |
237 | [NL80211_CMD_SET_REKEY_OFFLOAD] = "set_rekey_offload", | |
238 | [NL80211_CMD_PMKSA_CANDIDATE] = "pmksa_candidate", | |
239 | [NL80211_CMD_TDLS_OPER] = "tdls_oper", | |
240 | [NL80211_CMD_TDLS_MGMT] = "tdls_mgmt", | |
241 | [NL80211_CMD_UNEXPECTED_FRAME] = "unexpected_frame", | |
242 | [NL80211_CMD_PROBE_CLIENT] = "probe_client", | |
243 | [NL80211_CMD_REGISTER_BEACONS] = "register_beacons", | |
244 | [NL80211_CMD_UNEXPECTED_4ADDR_FRAME] = "unexpected_4addr_frame", | |
245 | [NL80211_CMD_SET_NOACK_MAP] = "set_noack_map", | |
246 | [NL80211_CMD_CH_SWITCH_NOTIFY] = "ch_switch_notify", | |
247 | [NL80211_CMD_START_P2P_DEVICE] = "start_p2p_device", | |
248 | [NL80211_CMD_STOP_P2P_DEVICE] = "stop_p2p_device", | |
249 | [NL80211_CMD_CONN_FAILED] = "conn_failed", | |
250 | [NL80211_CMD_SET_MCAST_RATE] = "set_mcast_rate", | |
251 | [NL80211_CMD_SET_MAC_ACL] = "set_mac_acl", | |
252 | [NL80211_CMD_RADAR_DETECT] = "radar_detect", | |
253 | [NL80211_CMD_GET_PROTOCOL_FEATURES] = "get_protocol_features", | |
254 | [NL80211_CMD_UPDATE_FT_IES] = "update_ft_ies", | |
255 | [NL80211_CMD_FT_EVENT] = "ft_event", | |
256 | [NL80211_CMD_CRIT_PROTOCOL_START] = "crit_protocol_start", | |
257 | [NL80211_CMD_CRIT_PROTOCOL_STOP] = "crit_protocol_stop", | |
258 | [NL80211_CMD_GET_COALESCE] = "get_coalesce", | |
259 | [NL80211_CMD_SET_COALESCE] = "set_coalesce", | |
260 | [NL80211_CMD_CHANNEL_SWITCH] = "channel_switch", | |
261 | [NL80211_CMD_VENDOR] = "vendor", | |
262 | [NL80211_CMD_SET_QOS_MAP] = "set_qos_map", | |
263 | [NL80211_CMD_ADD_TX_TS] = "add_tx_ts", | |
264 | [NL80211_CMD_DEL_TX_TS] = "del_tx_ts", | |
265 | [NL80211_CMD_GET_MPP] = "get_mpp", | |
266 | [NL80211_CMD_JOIN_OCB] = "join_ocb", | |
267 | [NL80211_CMD_LEAVE_OCB] = "leave_ocb", | |
268 | [NL80211_CMD_CH_SWITCH_STARTED_NOTIFY] = "ch_switch_started_notify", | |
269 | [NL80211_CMD_TDLS_CHANNEL_SWITCH] = "tdls_channel_switch", | |
270 | [NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH] = "tdls_cancel_channel_switch", | |
271 | [NL80211_CMD_WIPHY_REG_CHANGE] = "wiphy_reg_change", | |
272 | [NL80211_CMD_ABORT_SCAN] = "abort_scan", | |
273 | [NL80211_CMD_START_NAN] = "start_nan", | |
274 | [NL80211_CMD_STOP_NAN] = "stop_nan", | |
275 | [NL80211_CMD_ADD_NAN_FUNCTION] = "add_nan_function", | |
276 | [NL80211_CMD_DEL_NAN_FUNCTION] = "del_nan_function", | |
277 | [NL80211_CMD_CHANGE_NAN_CONFIG] = "change_nan_config", | |
278 | [NL80211_CMD_NAN_MATCH] = "nan_match", | |
279 | [NL80211_CMD_SET_MULTICAST_TO_UNICAST] = "set_multicast_to_unicast", | |
280 | [NL80211_CMD_UPDATE_CONNECT_PARAMS] = "update_connect_params", | |
281 | [NL80211_CMD_SET_PMK] = "set_pmk", | |
282 | [NL80211_CMD_DEL_PMK] = "del_pmk", | |
283 | [NL80211_CMD_PORT_AUTHORIZED] = "port_authorized", | |
284 | [NL80211_CMD_RELOAD_REGDB] = "reload_regdb", | |
285 | [NL80211_CMD_EXTERNAL_AUTH] = "external_auth", | |
286 | [NL80211_CMD_STA_OPMODE_CHANGED] = "sta_opmode_changed", | |
287 | [NL80211_CMD_CONTROL_PORT_FRAME] = "control_port_frame", | |
288 | [NL80211_CMD_GET_FTM_RESPONDER_STATS] = "get_ftm_responder_stats", | |
289 | [NL80211_CMD_PEER_MEASUREMENT_START] = "peer_measurement_start", | |
290 | [NL80211_CMD_PEER_MEASUREMENT_RESULT] = "peer_measurement_result", | |
291 | [NL80211_CMD_PEER_MEASUREMENT_COMPLETE] = "peer_measurement_complete", | |
292 | [NL80211_CMD_NOTIFY_RADAR] = "notify_radar", | |
293 | [NL80211_CMD_UPDATE_OWE_INFO] = "update_owe_info", | |
294 | [NL80211_CMD_PROBE_MESH_LINK] = "probe_mesh_link", | |
295 | [NL80211_CMD_SET_TID_CONFIG] = "set_tid_config", | |
296 | [NL80211_CMD_UNPROT_BEACON] = "unprot_beacon", | |
297 | [NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS] = "control_port_frame_tx_status", | |
298 | [NL80211_CMD_SET_SAR_SPECS] = "set_sar_specs", | |
299 | [NL80211_CMD_OBSS_COLOR_COLLISION] = "obss_color_collision", | |
300 | [NL80211_CMD_COLOR_CHANGE_REQUEST] = "color_change_request", | |
301 | [NL80211_CMD_COLOR_CHANGE_STARTED] = "color_change_started", | |
302 | [NL80211_CMD_COLOR_CHANGE_ABORTED] = "color_change_aborted", | |
303 | [NL80211_CMD_COLOR_CHANGE_COMPLETED] = "color_change_completed", | |
304 | }; | |
305 | ||
306 | DEFINE_STRING_TABLE_LOOKUP_TO_STRING(nl80211_cmd, int); |