]> git.ipfire.org Git - network.git/blob - src/libnetwork/phy.c
libnetwork: Actually store index
[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 ssize_t max_mpdu_length;
43 unsigned int vht_caps;
44 unsigned int ht_caps;
45 };
46
47 static int phy_get_index(const char* name) {
48 char path[1024];
49 char index[8];
50
51 snprintf(path, sizeof(path), "/sys/class/ieee80211/%s/index", name);
52
53 FILE* f = fopen(path, "r");
54 if (!f)
55 return -1;
56
57 int p = fread(index, 1, sizeof(index), f);
58 if (p < 0) {
59 fclose(f);
60 return -1;
61 }
62
63 // Terminate buffer
64 index[p] = '\0';
65 fclose(f);
66
67 return atoi(index);
68 }
69
70 static void phy_parse_vht_capabilities(struct network_phy* phy, __u32 caps) {
71 // Max MPDU length
72 switch (caps & 0x3) {
73 case 0:
74 phy->max_mpdu_length = 3895;
75 break;
76
77 case 1:
78 phy->max_mpdu_length = 7991;
79 break;
80
81 case 2:
82 phy->max_mpdu_length = 11454;
83 break;
84
85 case 3:
86 phy->max_mpdu_length = -1;
87 }
88
89 // Supported channel widths
90 switch ((caps >> 2) & 0x3) {
91 case 0:
92 break;
93
94 // Supports 160 MHz
95 case 1:
96 phy->vht_caps |= NETWORK_PHY_VHT_CAP_VHT160;
97 break;
98
99 // Supports 160 MHz and 80+80 MHz
100 case 2:
101 phy->vht_caps |= NETWORK_PHY_VHT_CAP_VHT160;
102 phy->vht_caps |= NETWORK_PHY_VHT_CAP_VHT80PLUS80;
103 break;
104 }
105
106 // RX LDPC
107 if (caps & BIT(4))
108 phy->vht_caps |= NETWORK_PHY_VHT_CAP_RX_LDPC;
109
110 // RX Short GI 80 MHz
111 if (caps & BIT(5))
112 phy->vht_caps |= NETWORK_PHY_VHT_CAP_RX_SHORT_GI_80;
113
114 // RX Short GI 160 MHz and 80+80 MHz
115 if (caps & BIT(6))
116 phy->vht_caps |= NETWORK_PHY_VHT_CAP_RX_SHORT_GI_160;
117
118 // TX STBC
119 if (caps & BIT(7))
120 phy->vht_caps |= NETWORK_PHY_VHT_CAP_TX_STBC;
121
122 // Single User Beamformer
123 if (caps & BIT(11))
124 phy->vht_caps |= NETWORK_PHY_VHT_CAP_SU_BEAMFORMER;
125
126 // Single User Beamformee
127 if (caps & BIT(12))
128 phy->vht_caps |= NETWORK_PHY_VHT_CAP_SU_BEAMFORMEE;
129
130 // Multi User Beamformer
131 if (caps & BIT(19))
132 phy->vht_caps |= NETWORK_PHY_VHT_CAP_MU_BEAMFORMER;
133
134 // Multi User Beamformee
135 if (caps & BIT(20))
136 phy->vht_caps |= NETWORK_PHY_VHT_CAP_MU_BEAMFORMEE;
137
138 // TX-OP-PS
139 if (caps & BIT(21))
140 phy->vht_caps |= NETWORK_PHY_VHT_CAP_TXOP_PS;
141
142 // HTC-VHT
143 if (caps & BIT(22))
144 phy->vht_caps |= NETWORK_PHY_VHT_CAP_HTC_VHT;
145
146 // RX Antenna Pattern Consistency
147 if (caps & BIT(28))
148 phy->vht_caps |= NETWORK_PHY_VHT_CAP_RX_ANTENNA_PATTERN;
149
150 // TX Antenna Pattern Consistency
151 if (caps & BIT(29))
152 phy->vht_caps |= NETWORK_PHY_VHT_CAP_TX_ANTENNA_PATTERN;
153 }
154
155 static void phy_parse_ht_capabilities(struct network_phy* phy, __u16 caps) {
156 // RX LDPC
157 if (caps & BIT(0))
158 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_LDPC;
159
160 // HT40
161 if (caps & BIT(1))
162 phy->ht_caps |= NETWORK_PHY_HT_CAP_HT40;
163
164 // Static/Dynamic SM Power Save
165 switch ((caps >> 2) & 0x3) {
166 case 0:
167 phy->ht_caps |= NETWORK_PHY_HT_CAP_SMPS_STATIC;
168 break;
169
170 case 1:
171 phy->ht_caps |= NETWORK_PHY_HT_CAP_SMPS_DYNAMIC;
172 break;
173
174 default:
175 break;
176 }
177
178 // RX Greenfield
179 if (caps & BIT(4))
180 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_GF;
181
182 // RX HT20 Short GI
183 if (caps & BIT(5))
184 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_HT20_SGI;
185
186 // RX HT40 Short GI
187 if (caps & BIT(6))
188 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_HT40_SGI;
189
190 // TX STBC
191 if (caps & BIT(7))
192 phy->ht_caps |= NETWORK_PHY_HT_CAP_TX_STBC;
193
194 // RX STBC
195 switch ((caps >> 8) & 0x3) {
196 case 1:
197 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_STBC1;
198 break;
199
200 case 2:
201 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_STBC2;
202 break;
203
204 case 3:
205 phy->ht_caps |= NETWORK_PHY_HT_CAP_RX_STBC3;
206 break;
207
208 default:
209 break;
210 }
211
212 // HT Delayed Block ACK
213 if (caps & BIT(10))
214 phy->ht_caps |= NETWORK_PHY_HT_CAP_DELAYED_BA;
215
216 // Max AMSDU length 7935
217 if (caps & BIT(11))
218 phy->ht_caps |= NETWORK_PHY_HT_CAP_MAX_AMSDU_7935;
219
220 // DSSS/CCK HT40
221 if (caps & BIT(12))
222 phy->ht_caps |= NETWORK_PHY_HT_CAP_DSSS_CCK_HT40;
223
224 // Bit 13 is reserved
225
226 // HT40 Intolerant
227 if (caps & BIT(14))
228 phy->ht_caps |= NETWORK_PHY_HT_CAP_HT40_INTOLERANT;
229
230 // L-SIG TXOP protection
231 if (caps & BIT(15))
232 phy->ht_caps |= NETWORK_PHY_HT_CAP_LSIG_TXOP_PROT;
233 }
234
235 static int phy_parse_info(struct nl_msg* msg, void* data) {
236 struct network_phy* phy = data;
237
238 struct nlattr* attrs[NL80211_ATTR_MAX + 1];
239 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
240
241 nla_parse(attrs, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
242 genlmsg_attrlen(gnlh, 0), NULL);
243
244 if (attrs[NL80211_ATTR_WIPHY_BANDS]) {
245 struct nlattr* nl_band;
246 int i;
247
248 nla_for_each_nested(nl_band, attrs[NL80211_ATTR_WIPHY_BANDS], i) {
249 struct nlattr* band_attrs[NL80211_BAND_ATTR_MAX + 1];
250 nla_parse(band_attrs, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
251 nla_len(nl_band), NULL);
252
253 // HT Capabilities
254 if (band_attrs[NL80211_BAND_ATTR_HT_CAPA]) {
255 __u16 ht_caps = nla_get_u16(band_attrs[NL80211_BAND_ATTR_HT_CAPA]);
256 phy_parse_ht_capabilities(phy, ht_caps);
257 }
258
259 // VHT Capabilities
260 if (band_attrs[NL80211_BAND_ATTR_VHT_CAPA]) {
261 __u32 vht_caps = nla_get_u32(band_attrs[NL80211_BAND_ATTR_VHT_CAPA]);
262
263 phy_parse_vht_capabilities(phy, vht_caps);
264 }
265 }
266 }
267
268 return NL_OK;
269 }
270
271 static int phy_get_info(struct network_phy* phy) {
272 DEBUG(phy->ctx, "Getting information for %s\n", phy->name);
273
274 struct nl_msg* msg = network_phy_make_netlink_message(phy, NL80211_CMD_GET_WIPHY, 0);
275 if (!msg)
276 return -1;
277
278 int r = network_send_netlink_message(phy->ctx, msg, phy_parse_info, phy);
279
280 // This is fine since some devices are not supported by NL80211
281 if (r == -ENODEV) {
282 DEBUG(phy->ctx, "Could not fetch information from kernel\n");
283 return 0;
284 }
285
286 return r;
287 }
288
289 static void network_phy_free(struct network_phy* phy) {
290 DEBUG(phy->ctx, "Releasing phy at %p\n", phy);
291
292 if (phy->name)
293 free(phy->name);
294
295 network_unref(phy->ctx);
296 free(phy);
297 }
298
299 NETWORK_EXPORT int network_phy_new(struct network_ctx* ctx, struct network_phy** phy,
300 const char* name) {
301 if (!name)
302 return -EINVAL;
303
304 int index = phy_get_index(name);
305 if (index < 0)
306 return -ENODEV;
307
308 struct network_phy* p = calloc(1, sizeof(*p));
309 if (!p)
310 return -ENOMEM;
311
312 // Initalise object
313 p->ctx = network_ref(ctx);
314 p->refcount = 1;
315
316 p->name = strdup(name);
317 p->index = index;
318
319 // Load information from kernel
320 int r = phy_get_info(p);
321 if (r) {
322 ERROR(p->ctx, "Error getting PHY information from kernel\n");
323 network_phy_free(p);
324
325 return r;
326 }
327
328 DEBUG(p->ctx, "Allocated phy at %p (index = %d)\n", p, p->index);
329 *phy = p;
330 return 0;
331 }
332
333 NETWORK_EXPORT struct network_phy* network_phy_ref(struct network_phy* phy) {
334 if (!phy)
335 return NULL;
336
337 phy->refcount++;
338 return phy;
339 }
340
341 NETWORK_EXPORT struct network_phy* network_phy_unref(struct network_phy* phy) {
342 if (!phy)
343 return NULL;
344
345 if (--phy->refcount > 0)
346 return phy;
347
348 network_phy_free(phy);
349 return NULL;
350 }
351
352 struct nl_msg* network_phy_make_netlink_message(struct network_phy* phy,
353 enum nl80211_commands cmd, int flags) {
354 struct nl_msg* msg = network_make_netlink_message(phy->ctx, cmd, flags);
355 if (!msg)
356 return NULL;
357
358 // Set PHY index
359 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, phy->index);
360
361 return msg;
362
363 nla_put_failure:
364 nlmsg_free(msg);
365
366 return NULL;
367 }
368
369 NETWORK_EXPORT int network_phy_has_vht_capability(struct network_phy* phy, const enum network_phy_vht_caps cap) {
370 return phy->vht_caps & cap;
371 }
372
373 NETWORK_EXPORT int network_phy_has_ht_capability(struct network_phy* phy, const enum network_phy_ht_caps cap) {
374 return phy->ht_caps & cap;
375 }
376
377 static const char* network_phy_get_vht_capability_string(const enum network_phy_vht_caps cap) {
378 switch (cap) {
379 case NETWORK_PHY_VHT_CAP_VHT160:
380 return "[VHT-160]";
381
382 case NETWORK_PHY_VHT_CAP_VHT80PLUS80:
383 return "[VHT-160-80PLUS80]";
384
385 case NETWORK_PHY_VHT_CAP_RX_LDPC:
386 return "[RXLDPC]";
387
388 case NETWORK_PHY_VHT_CAP_RX_SHORT_GI_80:
389 return "[SHORT-GI-80]";
390
391 case NETWORK_PHY_VHT_CAP_RX_SHORT_GI_160:
392 return "[SHORT-GI-160]";
393
394 case NETWORK_PHY_VHT_CAP_TX_STBC:
395 return "[TX-STBC-2BY1]";
396
397 case NETWORK_PHY_VHT_CAP_SU_BEAMFORMER:
398 return "[SU-BEAMFORMER]";
399
400 case NETWORK_PHY_VHT_CAP_SU_BEAMFORMEE:
401 return "[SU-BEAMFORMEE]";
402
403 case NETWORK_PHY_VHT_CAP_MU_BEAMFORMER:
404 return "[MU-BEAMFORMER]";
405
406 case NETWORK_PHY_VHT_CAP_MU_BEAMFORMEE:
407 return "[MU-BEAMFORMEE]";
408
409 case NETWORK_PHY_VHT_CAP_TXOP_PS:
410 return "[VHT-TXOP-PS]";
411
412 case NETWORK_PHY_VHT_CAP_HTC_VHT:
413 return "[HTC-VHT]";
414
415 case NETWORK_PHY_VHT_CAP_RX_ANTENNA_PATTERN:
416 return "[RX-ANTENNA-PATTERN]";
417
418 case NETWORK_PHY_VHT_CAP_TX_ANTENNA_PATTERN:
419 return "[TX-ANTENNA-PATTERN]";
420 }
421
422 return NULL;
423 }
424
425 static const char* network_phy_get_ht_capability_string(const enum network_phy_ht_caps cap) {
426 switch (cap) {
427 case NETWORK_PHY_HT_CAP_RX_LDPC:
428 return "[LDPC]";
429
430 case NETWORK_PHY_HT_CAP_HT40:
431 return "[HT40+][HT40-]";
432
433 case NETWORK_PHY_HT_CAP_SMPS_STATIC:
434 return "[SMPS-STATIC]";
435
436 case NETWORK_PHY_HT_CAP_SMPS_DYNAMIC:
437 return "[SMPS-DYNAMIC]";
438
439 case NETWORK_PHY_HT_CAP_RX_GF:
440 return "[GF]";
441
442 case NETWORK_PHY_HT_CAP_RX_HT20_SGI:
443 return "[SHORT-GI-20]";
444
445 case NETWORK_PHY_HT_CAP_RX_HT40_SGI:
446 return "[SHORT-GI-40]";
447
448 case NETWORK_PHY_HT_CAP_TX_STBC:
449 return "[TX-STBC]";
450
451 case NETWORK_PHY_HT_CAP_RX_STBC1:
452 return "[RX-STBC1]";
453
454 case NETWORK_PHY_HT_CAP_RX_STBC2:
455 return "[RX-STBC12]";
456
457 case NETWORK_PHY_HT_CAP_RX_STBC3:
458 return "[RX-STBC123]";
459
460 case NETWORK_PHY_HT_CAP_DELAYED_BA:
461 return "[DELAYED-BA]";
462
463 case NETWORK_PHY_HT_CAP_MAX_AMSDU_7935:
464 return "[MAX-AMSDU-7935]";
465
466 case NETWORK_PHY_HT_CAP_DSSS_CCK_HT40:
467 return "[DSSS_CCK-40]";
468
469 case NETWORK_PHY_HT_CAP_HT40_INTOLERANT:
470 return "[40-INTOLERANT]";
471
472 case NETWORK_PHY_HT_CAP_LSIG_TXOP_PROT:
473 return "[LSIG-TXOP-PROT]";
474 }
475
476 return NULL;
477 }
478
479 NETWORK_EXPORT char* network_phy_list_vht_capabilities(struct network_phy* phy) {
480 char* buffer = malloc(1024);
481 *buffer = '\0';
482
483 char* p = buffer;
484
485 switch (phy->max_mpdu_length) {
486 case 7991:
487 case 11454:
488 snprintf(p, 1024 - 1, "[MAX-MPDU-%zu]", phy->max_mpdu_length);
489 break;
490
491 }
492
493 foreach_vht_cap(cap) {
494 if (network_phy_has_vht_capability(phy, cap)) {
495 const char* cap_str = network_phy_get_vht_capability_string(cap);
496
497 if (cap_str)
498 p = strncat(p, cap_str, 1024 - 1);
499 }
500 }
501
502 return buffer;
503 }
504
505 NETWORK_EXPORT char* network_phy_list_ht_capabilities(struct network_phy* phy) {
506 char* buffer = malloc(1024);
507 *buffer = '\0';
508
509 char* p = buffer;
510 foreach_ht_cap(cap) {
511 if (network_phy_has_ht_capability(phy, cap)) {
512 const char* cap_str = network_phy_get_ht_capability_string(cap);
513
514 if (cap_str)
515 p = strncat(p, cap_str, 1024 - 1);
516 }
517 }
518
519 return buffer;
520 }