| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <net/if_arp.h> |
| 4 |
|
| 5 |
#include "sd-netlink.h" |
| 6 |
|
| 7 |
#include "netlink-util.h" |
| 8 |
#include "networkd-manager.h" |
| 9 |
#include "networkd-wiphy.h" |
| 10 |
#include "parse-util.h" |
| 11 |
#include "string-util.h" |
| 12 |
#include "wifi-util.h" |
| 13 |
#include "wlan.h" |
| 14 |
|
| 15 |
static void wlan_done(NetDev *netdev) { |
| 16 |
WLan *w = WLAN(netdev); |
| 17 |
|
| 18 |
w->wiphy_name = mfree(w->wiphy_name); |
| 19 |
} |
| 20 |
|
| 21 |
static void wlan_init(NetDev *netdev) { |
| 22 |
WLan *w = WLAN(netdev); |
| 23 |
|
| 24 |
w->wiphy_index = UINT32_MAX; |
| 25 |
w->wds = -1; |
| 26 |
} |
| 27 |
|
| 28 |
static int wlan_get_wiphy(NetDev *netdev, Wiphy **ret) { |
| 29 |
WLan *w = WLAN(netdev); |
| 30 |
|
| 31 |
if (!netdev_is_managed(netdev)) |
| 32 |
return -ENOENT; /* Already detached, due to e.g. reloading .netdev files. */ |
| 33 |
|
| 34 |
if (w->wiphy_name) |
| 35 |
return wiphy_get_by_name(netdev->manager, w->wiphy_name, ret); |
| 36 |
|
| 37 |
return wiphy_get_by_index(netdev->manager, w->wiphy_index, ret); |
| 38 |
} |
| 39 |
|
| 40 |
static int wlan_is_ready_to_create(NetDev *netdev, Link *link) { |
| 41 |
return wlan_get_wiphy(netdev, NULL) >= 0; |
| 42 |
} |
| 43 |
|
| 44 |
static int wlan_fill_message(NetDev *netdev, sd_netlink_message *m) { |
| 45 |
WLan *w = WLAN(netdev); |
| 46 |
Wiphy *wiphy; |
| 47 |
int r; |
| 48 |
|
| 49 |
r = wlan_get_wiphy(netdev, &wiphy); |
| 50 |
if (r < 0) |
| 51 |
return r; |
| 52 |
|
| 53 |
r = sd_netlink_message_append_u32(m, NL80211_ATTR_WIPHY, wiphy->index); |
| 54 |
if (r < 0) |
| 55 |
return r; |
| 56 |
|
| 57 |
r = sd_netlink_message_append_string(m, NL80211_ATTR_IFNAME, netdev->ifname); |
| 58 |
if (r < 0) |
| 59 |
return r; |
| 60 |
|
| 61 |
r = sd_netlink_message_append_u32(m, NL80211_ATTR_IFTYPE, w->iftype); |
| 62 |
if (r < 0) |
| 63 |
return r; |
| 64 |
|
| 65 |
if (!hw_addr_is_null(&netdev->hw_addr) && netdev->hw_addr.length == ETH_ALEN) { |
| 66 |
r = sd_netlink_message_append_ether_addr(m, NL80211_ATTR_MAC, &netdev->hw_addr.ether); |
| 67 |
if (r < 0) |
| 68 |
return r; |
| 69 |
} |
| 70 |
|
| 71 |
if (w->wds >= 0) { |
| 72 |
r = sd_netlink_message_append_u8(m, NL80211_ATTR_4ADDR, w->wds); |
| 73 |
if (r < 0) |
| 74 |
return r; |
| 75 |
} |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static int wlan_create_handler(sd_netlink *genl, sd_netlink_message *m, NetDev *netdev) { |
| 81 |
int r; |
| 82 |
|
| 83 |
assert(netdev); |
| 84 |
assert(netdev->state != _NETDEV_STATE_INVALID); |
| 85 |
|
| 86 |
r = sd_netlink_message_get_errno(m); |
| 87 |
if (IN_SET(r, -EEXIST, -ENFILE)) |
| 88 |
/* Unlike the other netdevs, the kernel may return -ENFILE. See dev_alloc_name(). */ |
| 89 |
log_netdev_info(netdev, "WLAN interface exists, using existing without changing its parameters."); |
| 90 |
else if (r < 0) { |
| 91 |
log_netdev_warning_errno(netdev, r, "WLAN interface could not be created: %m"); |
| 92 |
netdev_enter_failed(netdev); |
| 93 |
|
| 94 |
return 1; |
| 95 |
} |
| 96 |
|
| 97 |
log_netdev_debug(netdev, "WLAN interface is created."); |
| 98 |
return 1; |
| 99 |
} |
| 100 |
|
| 101 |
static int wlan_create(NetDev *netdev) { |
| 102 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 103 |
int r; |
| 104 |
|
| 105 |
assert(netdev); |
| 106 |
assert(netdev->manager); |
| 107 |
assert(netdev->manager->genl); |
| 108 |
|
| 109 |
r = sd_genl_message_new(netdev->manager->genl, NL80211_GENL_NAME, NL80211_CMD_NEW_INTERFACE, &m); |
| 110 |
if (r < 0) |
| 111 |
return log_netdev_warning_errno(netdev, r, "Failed to allocate netlink message: %m"); |
| 112 |
|
| 113 |
r = wlan_fill_message(netdev, m); |
| 114 |
if (r < 0) |
| 115 |
return log_netdev_warning_errno(netdev, r, "Failed to fill netlink message: %m"); |
| 116 |
|
| 117 |
r = netlink_call_async(netdev->manager->genl, NULL, m, wlan_create_handler, |
| 118 |
netdev_destroy_callback, netdev); |
| 119 |
if (r < 0) |
| 120 |
return log_netdev_warning_errno(netdev, r, "Failed to send netlink message: %m"); |
| 121 |
|
| 122 |
netdev_ref(netdev); |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
static int wlan_verify(NetDev *netdev, const char *filename) { |
| 127 |
WLan *w = WLAN(netdev); |
| 128 |
|
| 129 |
assert(filename); |
| 130 |
|
| 131 |
if (w->iftype == NL80211_IFTYPE_UNSPECIFIED) |
| 132 |
return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), |
| 133 |
"%s: WLAN interface type is not specified, ignoring.", |
| 134 |
filename); |
| 135 |
|
| 136 |
if (w->wiphy_index == UINT32_MAX && isempty(w->wiphy_name)) |
| 137 |
return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), |
| 138 |
"%s: physical WLAN device is not specified, ignoring.", |
| 139 |
filename); |
| 140 |
|
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
int config_parse_wiphy( |
| 145 |
const char *unit, |
| 146 |
const char *filename, |
| 147 |
unsigned line, |
| 148 |
const char *section, |
| 149 |
unsigned section_line, |
| 150 |
const char *lvalue, |
| 151 |
int ltype, |
| 152 |
const char *rvalue, |
| 153 |
void *data, |
| 154 |
void *userdata) { |
| 155 |
|
| 156 |
WLan *w = WLAN(userdata); |
| 157 |
int r; |
| 158 |
|
| 159 |
assert(filename); |
| 160 |
assert(lvalue); |
| 161 |
assert(rvalue); |
| 162 |
|
| 163 |
if (isempty(rvalue)) { |
| 164 |
w->wiphy_name = mfree(w->wiphy_name); |
| 165 |
w->wiphy_index = UINT32_MAX; |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
r = safe_atou32(rvalue, &w->wiphy_index); |
| 170 |
if (r >= 0) { |
| 171 |
w->wiphy_name = mfree(w->wiphy_name); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
r = free_and_strdup_warn(&w->wiphy_name, rvalue); |
| 176 |
if (r < 0) |
| 177 |
return r; |
| 178 |
|
| 179 |
w->wiphy_index = UINT32_MAX; |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
int config_parse_wlan_iftype( |
| 184 |
const char *unit, |
| 185 |
const char *filename, |
| 186 |
unsigned line, |
| 187 |
const char *section, |
| 188 |
unsigned section_line, |
| 189 |
const char *lvalue, |
| 190 |
int ltype, |
| 191 |
const char *rvalue, |
| 192 |
void *data, |
| 193 |
void *userdata) { |
| 194 |
|
| 195 |
enum nl80211_iftype t, *iftype = ASSERT_PTR(data); |
| 196 |
|
| 197 |
assert(filename); |
| 198 |
assert(lvalue); |
| 199 |
assert(rvalue); |
| 200 |
|
| 201 |
if (isempty(rvalue)) { |
| 202 |
*iftype = NL80211_IFTYPE_UNSPECIFIED; |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
t = nl80211_iftype_from_string(rvalue); |
| 207 |
/* We reuse the kernel provided enum which does not contain negative value. So, the cast |
| 208 |
* below is mandatory. Otherwise, the check below always passes. */ |
| 209 |
if ((int) t < 0) { |
| 210 |
log_syntax(unit, LOG_WARNING, filename, line, t, |
| 211 |
"Failed to parse wlan interface type, ignoring assignment: %s", |
| 212 |
rvalue); |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
*iftype = t; |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
const NetDevVTable wlan_vtable = { |
| 221 |
.object_size = sizeof(WLan), |
| 222 |
.init = wlan_init, |
| 223 |
.done = wlan_done, |
| 224 |
.sections = NETDEV_COMMON_SECTIONS "WLAN\0", |
| 225 |
.is_ready_to_create = wlan_is_ready_to_create, |
| 226 |
.create = wlan_create, |
| 227 |
.create_type = NETDEV_CREATE_INDEPENDENT, |
| 228 |
.config_verify = wlan_verify, |
| 229 |
.iftype = ARPHRD_ETHER, |
| 230 |
.generate_mac = true, |
| 231 |
.skip_netdev_kind_check = true, |
| 232 |
}; |