]> git.ipfire.org Git - people/jschlag/network.git/blob - src/libnetwork/phy.c
de20aa907c7df126e8e115f6e87d529cdb01a03c
[people/jschlag/network.git] / src / libnetwork / phy.c
1 /*#############################################################################
2 # #
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2018 IPFire Network Development Team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 #############################################################################*/
20
21 #include <errno.h>
22 #include <linux/nl80211.h>
23 #include <netlink/attr.h>
24 #include <netlink/genl/genl.h>
25 #include <netlink/msg.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <network/libnetwork.h>
31 #include <network/logging.h>
32 #include <network/phy.h>
33 #include "libnetwork-private.h"
34
35 struct network_phy {
36 struct network_ctx* ctx;
37 int refcount;
38
39 int index;
40 char* name;
41
42 unsigned int ht_caps;
43 };
44
45 static int phy_get_index(const char* name) {
46 char path[1024];
47 char index[8];
48
49 snprintf(path, sizeof(path), "/sys/class/ieee80211/%s/index", name);
50
51 FILE* f = fopen(path, "r");
52 if (!f)
53 return -1;
54
55 int p = fread(index, 1, sizeof(index), f);
56 if (p < 0) {
57 fclose(f);
58 return -1;
59 }
60
61 // Terminate buffer
62 index[p] = '\0';
63 fclose(f);
64
65 return atoi(index);
66 }
67
68 static void phy_parse_ht_capabilities(struct network_phy* phy, __u16 caps) {
69 // RX LDCP
70 if (caps & BIT(0))
71 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_LDCP;
72
73 // HT40
74 if (caps & BIT(1))
75 phy->ht_caps |= NETWORK_PHY_HT_CAP_HT40;
76
77 // Static/Dynamic SM Power Save
78 switch ((caps >> 2) & 0x3) {
79 case 0:
80 phy->ht_caps |= NETWORK_PHY_HT_CAP_SMPS_STATIC;
81 break;
82
83 case 1:
84 phy->ht_caps |= NETWORK_PHY_HT_CAP_SMPS_DYNAMIC;
85 break;
86
87 default:
88 break;
89 }
90
91 // RX Greenfield
92 if (caps & BIT(4))
93 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_GF;
94
95 // RX HT20 Short GI
96 if (caps & BIT(5))
97 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_HT20_SGI;
98
99 // RX HT40 Short GI
100 if (caps & BIT(6))
101 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_HT40_SGI;
102
103 // TX STBC
104 if (caps & BIT(7))
105 phy->ht_caps |= NETWORK_PHY_HT_CAP_TX_STBC;
106
107 // RX STBC
108 switch ((caps >> 8) & 0x3) {
109 case 1:
110 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_STBC1;
111 break;
112
113 case 2:
114 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_STBC2;
115 break;
116
117 case 3:
118 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_STBC3;
119 break;
120
121 default:
122 break;
123 }
124
125 // HT Delayed Block ACK
126 if (caps & BIT(10))
127 phy->ht_caps |= NETWORK_PHY_HT_CAP_DELAYED_BA;
128
129 // Max AMSDU length 7935
130 if (caps & BIT(11))
131 phy->ht_caps |= NETWORK_PHY_HT_CAP_MAX_AMSDU_7935;
132
133 // DSSS/CCK HT40
134 if (caps & BIT(12))
135 phy->ht_caps |= NETWORK_PHY_HT_CAP_DSSS_CCK_HT40;
136
137 // Bit 13 is reserved
138
139 // HT40 Intolerant
140 if (caps & BIT(14))
141 phy->ht_caps |= NETWORK_PHY_HT_CAP_HT40_INTOLERANT;
142
143 // L-SIG TXOP protection
144 if (caps & BIT(15))
145 phy->ht_caps |= NETWORK_PHY_HT_CAP_LSIG_TXOP_PROT;
146 }
147
148 static int phy_parse_info(struct nl_msg* msg, void* data) {
149 struct network_phy* phy = data;
150
151 struct nlattr* attrs[NL80211_ATTR_MAX + 1];
152 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
153
154 nla_parse(attrs, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
155 genlmsg_attrlen(gnlh, 0), NULL);
156
157 if (attrs[NL80211_ATTR_WIPHY_BANDS]) {
158 struct nlattr* nl_band;
159 int i;
160
161 nla_for_each_nested(nl_band, attrs[NL80211_ATTR_WIPHY_BANDS], i) {
162 struct nlattr* band_attrs[NL80211_BAND_ATTR_MAX + 1];
163 nla_parse(band_attrs, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
164 nla_len(nl_band), NULL);
165
166 // HT Capabilities
167 if (band_attrs[NL80211_BAND_ATTR_HT_CAPA]) {
168 __u16 caps = nla_get_u16(band_attrs[NL80211_BAND_ATTR_HT_CAPA]);
169 phy_parse_ht_capabilities(phy, caps);
170 }
171 }
172 }
173
174 return NL_OK;
175 }
176
177 static int phy_get_info(struct network_phy* phy) {
178 DEBUG(phy->ctx, "Getting information for %s\n", phy->name);
179
180 struct nl_msg* msg = network_phy_make_netlink_message(phy, NL80211_CMD_GET_WIPHY, 0);
181 if (!msg)
182 return -1;
183
184 return network_send_netlink_message(phy->ctx, msg, phy_parse_info, phy);
185 }
186
187 static void network_phy_free(struct network_phy* phy) {
188 DEBUG(phy->ctx, "Releasing phy at %p\n", phy);
189
190 if (phy->name)
191 free(phy->name);
192
193 network_unref(phy->ctx);
194 free(phy);
195 }
196
197 NETWORK_EXPORT int network_phy_new(struct network_ctx* ctx, struct network_phy** phy,
198 const char* name) {
199 if (!name)
200 return -EINVAL;
201
202 int index = phy_get_index(name);
203 if (index < 0)
204 return -ENODEV;
205
206 struct network_phy* p = calloc(1, sizeof(*p));
207 if (!p)
208 return -ENOMEM;
209
210 // Initalise object
211 p->ctx = network_ref(ctx);
212 p->refcount = 1;
213
214 p->name = strdup(name);
215
216 // Load information from kernel
217 int r = phy_get_info(p);
218 if (r) {
219 ERROR(p->ctx, "Error getting PHY information from kernel\n");
220 network_phy_free(p);
221
222 return r;
223 }
224
225 DEBUG(p->ctx, "Allocated phy at %p\n", p);
226 *phy = p;
227 return 0;
228 }
229
230 NETWORK_EXPORT struct network_phy* network_phy_ref(struct network_phy* phy) {
231 if (!phy)
232 return NULL;
233
234 phy->refcount++;
235 return phy;
236 }
237
238 NETWORK_EXPORT struct network_phy* network_phy_unref(struct network_phy* phy) {
239 if (!phy)
240 return NULL;
241
242 if (--phy->refcount > 0)
243 return phy;
244
245 network_phy_free(phy);
246 return NULL;
247 }
248
249 struct nl_msg* network_phy_make_netlink_message(struct network_phy* phy,
250 enum nl80211_commands cmd, int flags) {
251 struct nl_msg* msg = network_make_netlink_message(phy->ctx, cmd, flags);
252 if (!msg)
253 return NULL;
254
255 // Set PHY index
256 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, phy->index);
257
258 return msg;
259
260 nla_put_failure:
261 nlmsg_free(msg);
262
263 return NULL;
264 }
265
266 NETWORK_EXPORT int network_phy_has_ht_capability(struct network_phy* phy, const enum network_phy_ht_caps cap) {
267 return phy->ht_caps & cap;
268 }
269
270 static const char* network_phy_get_ht_capability_string(const enum network_phy_ht_caps cap) {
271 switch (cap) {
272 case NETWORK_PHY_HT_CAP_RX_LDCP:
273 return "[LDPC]";
274
275 case NETWORK_PHY_HT_CAP_HT40:
276 return "[HT40+][HT40-]";
277
278 case NETWORK_PHY_HT_CAP_SMPS_STATIC:
279 return "[SMPS-STATIC]";
280
281 case NETWORK_PHY_HT_CAP_SMPS_DYNAMIC:
282 return "[SMPS-DYNAMIC]";
283
284 case NETWORK_PHY_HT_CAP_RX_GF:
285 return "[GF]";
286
287 case NETWORK_PHY_HT_CAP_RX_HT20_SGI:
288 return "[SHORT-GI-20]";
289
290 case NETWORK_PHY_HT_CAP_RX_HT40_SGI:
291 return "[SHORT-GI-40]";
292
293 case NETWORK_PHY_HT_CAP_TX_STBC:
294 return "[TX-STBC]";
295
296 case NETWORK_PHY_HT_CAP_RX_STBC1:
297 return "[RX-STBC1]";
298
299 case NETWORK_PHY_HT_CAP_RX_STBC2:
300 return "[RX-STBC12]";
301
302 case NETWORK_PHY_HT_CAP_RX_STBC3:
303 return "[RX-STBC123]";
304
305 case NETWORK_PHY_HT_CAP_DELAYED_BA:
306 return "[DELAYED-BA]";
307
308 case NETWORK_PHY_HT_CAP_MAX_AMSDU_7935:
309 return "[MAX-AMSDU-7935]";
310
311 case NETWORK_PHY_HT_CAP_DSSS_CCK_HT40:
312 return "[DSSS_CCK-40]";
313
314 case NETWORK_PHY_HT_CAP_HT40_INTOLERANT:
315 return "[40-INTOLERANT]";
316
317 case NETWORK_PHY_HT_CAP_LSIG_TXOP_PROT:
318 return "[LSIG-TXOP-PROT]";
319 }
320
321 return NULL;
322 }
323
324 NETWORK_EXPORT char* network_phy_list_ht_capabilities(struct network_phy* phy) {
325 char* buffer = malloc(1024);
326 *buffer = '\0';
327
328 char* p = buffer;
329 foreach_ht_cap(cap) {
330 if (network_phy_has_ht_capability(phy, cap)) {
331 const char* cap_str = network_phy_get_ht_capability_string(cap);
332
333 if (cap_str)
334 p = strncat(p, cap_str, 1024 - 1);
335 }
336 }
337
338 return buffer;
339 }