summaryrefslogtreecommitdiffstats
path: root/src/network/netdev/wlan.c
blob: 904e40fe81c552f972e57ee784a6e6c1e8fa1972 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <net/if_arp.h>

#include "sd-netlink.h"

#include "netlink-util.h"
#include "networkd-manager.h"
#include "networkd-wiphy.h"
#include "parse-util.h"
#include "wifi-util.h"
#include "wlan.h"

static void wlan_done(NetDev *netdev) {
        WLan *w = WLAN(netdev);

        w->wiphy_name = mfree(w->wiphy_name);
}

static void wlan_init(NetDev *netdev) {
        WLan *w = WLAN(netdev);

        w->wiphy_index = UINT32_MAX;
        w->wds = -1;
}

static int wlan_get_wiphy(NetDev *netdev, Wiphy **ret) {
        WLan *w = WLAN(netdev);

        if (w->wiphy_name)
                return wiphy_get_by_name(netdev->manager, w->wiphy_name, ret);

        return wiphy_get_by_index(netdev->manager, w->wiphy_index, ret);
}

static int wlan_is_ready_to_create(NetDev *netdev, Link *link) {
        return wlan_get_wiphy(netdev, NULL) >= 0;
}

static int wlan_fill_message(NetDev *netdev, sd_netlink_message *m) {
        WLan *w = WLAN(netdev);
        Wiphy *wiphy;
        int r;

        r = wlan_get_wiphy(netdev, &wiphy);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_u32(m, NL80211_ATTR_WIPHY, wiphy->index);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_string(m, NL80211_ATTR_IFNAME, netdev->ifname);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_u32(m, NL80211_ATTR_IFTYPE, w->iftype);
        if (r < 0)
                return r;

        if (!hw_addr_is_null(&netdev->hw_addr) && netdev->hw_addr.length == ETH_ALEN) {
                r = sd_netlink_message_append_ether_addr(m, NL80211_ATTR_MAC, &netdev->hw_addr.ether);
                if (r < 0)
                        return r;
        }

        if (w->wds >= 0) {
                r = sd_netlink_message_append_u8(m, NL80211_ATTR_4ADDR, w->wds);
                if (r < 0)
                        return r;
        }

        return 0;
}

static int wlan_create_handler(sd_netlink *genl, sd_netlink_message *m, NetDev *netdev) {
        int r;

        assert(netdev);
        assert(netdev->state != _NETDEV_STATE_INVALID);

        r = sd_netlink_message_get_errno(m);
        if (IN_SET(r, -EEXIST, -ENFILE))
                /* Unlike the other netdevs, the kernel may return -ENFILE. See dev_alloc_name(). */
                log_netdev_info(netdev, "WLAN interface exists, using existing without changing its parameters.");
        else if (r < 0) {
                log_netdev_warning_errno(netdev, r, "WLAN interface could not be created: %m");
                netdev_enter_failed(netdev);

                return 1;
        }

        log_netdev_debug(netdev, "WLAN interface is created.");
        return 1;
}

static int wlan_create(NetDev *netdev) {
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
        int r;

        assert(netdev);
        assert(netdev->manager);
        assert(netdev->manager->genl);

        r = sd_genl_message_new(netdev->manager->genl, NL80211_GENL_NAME, NL80211_CMD_NEW_INTERFACE, &m);
        if (r < 0)
                return log_netdev_warning_errno(netdev, r, "Failed to allocate netlink message: %m");

        r = wlan_fill_message(netdev, m);
        if (r < 0)
                return log_netdev_warning_errno(netdev, r, "Failed to fill netlink message: %m");

        r = netlink_call_async(netdev->manager->genl, NULL, m, wlan_create_handler,
                               netdev_destroy_callback, netdev);
        if (r < 0)
                return log_netdev_warning_errno(netdev, r, "Failed to send netlink message: %m");

        netdev_ref(netdev);
        return 0;
}

static int wlan_verify(NetDev *netdev, const char *filename) {
        WLan *w = WLAN(netdev);

        assert(filename);

        if (w->iftype == NL80211_IFTYPE_UNSPECIFIED)
                return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
                                                "%s: WLAN interface type is not specified, ignoring.",
                                                filename);

        if (w->wiphy_index == UINT32_MAX && isempty(w->wiphy_name))
                return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
                                                "%s: physical WLAN device is not specified, ignoring.",
                                                filename);

        return 0;
}

int config_parse_wiphy(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {

        WLan *w = WLAN(userdata);
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);

        if (isempty(rvalue)) {
                w->wiphy_name = mfree(w->wiphy_name);
                w->wiphy_index = UINT32_MAX;
                return 0;
        }

        r = safe_atou32(rvalue, &w->wiphy_index);
        if (r >= 0) {
                w->wiphy_name = mfree(w->wiphy_name);
                return 0;
        }

        r = free_and_strdup_warn(&w->wiphy_name, rvalue);
        if (r < 0)
                return r;

        w->wiphy_index = UINT32_MAX;
        return 0;
}

int config_parse_wlan_iftype(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {

        enum nl80211_iftype t, *iftype = ASSERT_PTR(data);

        assert(filename);
        assert(lvalue);
        assert(rvalue);

        if (isempty(rvalue)) {
                *iftype = NL80211_IFTYPE_UNSPECIFIED;
                return 0;
        }

        t = nl80211_iftype_from_string(rvalue);
        /* We reuse the kernel provided enum which does not contain negative value. So, the cast
         * below is mandatory. Otherwise, the check below always passes. */
        if ((int) t < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, t,
                           "Failed to parse wlan interface type, ignoring assignment: %s",
                           rvalue);
                return 0;
        }

        *iftype = t;
        return 0;
}

const NetDevVTable wlan_vtable = {
        .object_size = sizeof(WLan),
        .init = wlan_init,
        .done = wlan_done,
        .sections = NETDEV_COMMON_SECTIONS "WLAN\0",
        .is_ready_to_create = wlan_is_ready_to_create,
        .create = wlan_create,
        .create_type = NETDEV_CREATE_INDEPENDENT,
        .config_verify = wlan_verify,
        .iftype = ARPHRD_ETHER,
        .generate_mac = true,
        .skip_netdev_kind_check = true,
};