]> git.ipfire.org Git - thirdparty/iw.git/blob - phy.c
whitespace cleanups
[thirdparty/iw.git] / phy.c
1 #include <stdbool.h>
2 #include <errno.h>
3 #include <net/if.h>
4 #include <strings.h>
5
6 #include <netlink/genl/genl.h>
7 #include <netlink/genl/family.h>
8 #include <netlink/genl/ctrl.h>
9 #include <netlink/msg.h>
10 #include <netlink/attr.h>
11
12 #include "nl80211.h"
13 #include "iw.h"
14
15 static int handle_name(struct nl80211_state *state,
16 struct nl_cb *cb,
17 struct nl_msg *msg,
18 int argc, char **argv)
19 {
20 if (argc != 1)
21 return 1;
22
23 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, *argv);
24
25 return 0;
26 nla_put_failure:
27 return -ENOBUFS;
28 }
29 COMMAND(set, name, "<new name>", NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_name,
30 "Rename this wireless device.");
31
32 static int handle_freqchan(struct nl_msg *msg, bool chan,
33 int argc, char **argv)
34 {
35 char *end;
36 static const struct {
37 const char *name;
38 unsigned int val;
39 } htmap[] = {
40 { .name = "HT20", .val = NL80211_CHAN_HT20, },
41 { .name = "HT40+", .val = NL80211_CHAN_HT40PLUS, },
42 { .name = "HT40-", .val = NL80211_CHAN_HT40MINUS, },
43 };
44 unsigned int htval = NL80211_CHAN_NO_HT;
45 unsigned int freq;
46 int i;
47
48 if (!argc || argc > 2)
49 return 1;
50
51 if (argc == 2) {
52 for (i = 0; i < ARRAY_SIZE(htmap); i++) {
53 if (strcasecmp(htmap[i].name, argv[1]) == 0) {
54 htval = htmap[i].val;
55 break;
56 }
57 }
58 if (htval == NL80211_CHAN_NO_HT)
59 return 1;
60 }
61
62 if (!*argv[0])
63 return 1;
64 freq = strtoul(argv[0], &end, 10);
65 if (*end)
66 return 1;
67
68 if (chan)
69 freq = ieee80211_channel_to_frequency(freq);
70
71 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
72 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, htval);
73
74 return 0;
75 nla_put_failure:
76 return -ENOBUFS;
77 }
78
79 static int handle_freq(struct nl80211_state *state,
80 struct nl_cb *cb, struct nl_msg *msg,
81 int argc, char **argv)
82 {
83 return handle_freqchan(msg, false, argc, argv);
84 }
85 COMMAND(set, freq, "<freq> [HT20|HT40+|HT40-]",
86 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_freq,
87 "Set frequency/channel the hardware is using, including HT\n"
88 "configuration.");
89 COMMAND(set, freq, "<freq> [HT20|HT40+|HT40-]",
90 NL80211_CMD_SET_WIPHY, 0, CIB_NETDEV, handle_freq, NULL);
91
92 static int handle_chan(struct nl80211_state *state,
93 struct nl_cb *cb, struct nl_msg *msg,
94 int argc, char **argv)
95 {
96 return handle_freqchan(msg, true, argc, argv);
97 }
98 COMMAND(set, channel, "<channel> [HT20|HT40+|HT40-]",
99 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_chan, NULL);
100 COMMAND(set, channel, "<channel> [HT20|HT40+|HT40-]",
101 NL80211_CMD_SET_WIPHY, 0, CIB_NETDEV, handle_chan, NULL);
102
103 static int handle_fragmentation(struct nl80211_state *state,
104 struct nl_cb *cb, struct nl_msg *msg,
105 int argc, char **argv)
106 {
107 unsigned int frag;
108
109 if (argc != 1)
110 return 1;
111
112 if (strcmp("off", argv[0]) == 0)
113 frag = -1;
114 else {
115 char *end;
116
117 if (!*argv[0])
118 return 1;
119 frag = strtoul(argv[0], &end, 10);
120 if (*end != '\0')
121 return 1;
122 }
123
124 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, frag);
125
126 return 0;
127 nla_put_failure:
128 return -ENOBUFS;
129 }
130 COMMAND(set, frag, "<fragmentation threshold|off>",
131 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_fragmentation,
132 "Set fragmentation threshold.");
133
134 static int handle_rts(struct nl80211_state *state,
135 struct nl_cb *cb, struct nl_msg *msg,
136 int argc, char **argv)
137 {
138 unsigned int rts;
139
140 if (argc != 1)
141 return 1;
142
143 if (strcmp("off", argv[0]) == 0)
144 rts = -1;
145 else {
146 char *end;
147
148 if (!*argv[0])
149 return 1;
150 rts = strtoul(argv[0], &end, 10);
151 if (*end != '\0')
152 return 1;
153 }
154
155 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, rts);
156
157 return 0;
158 nla_put_failure:
159 return -ENOBUFS;
160 }
161 COMMAND(set, rts, "<rts threshold|off>",
162 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_rts,
163 "Set rts threshold.");
164
165 static int handle_netns(struct nl80211_state *state,
166 struct nl_cb *cb,
167 struct nl_msg *msg,
168 int argc, char **argv)
169 {
170 char *end;
171
172 if (argc != 1)
173 return 1;
174
175 if (!*argv[0])
176 return 1;
177
178 NLA_PUT_U32(msg, NL80211_ATTR_PID,
179 strtoul(argv[0], &end, 10));
180
181 if (*end != '\0')
182 return 1;
183
184 return 0;
185 nla_put_failure:
186 return -ENOBUFS;
187 }
188 COMMAND(set, netns, "<pid>",
189 NL80211_CMD_SET_WIPHY_NETNS, 0, CIB_PHY, handle_netns,
190 "Put this wireless device into a different network namespace");
191
192 static int handle_coverage(struct nl80211_state *state,
193 struct nl_cb *cb,
194 struct nl_msg *msg,
195 int argc, char **argv)
196 {
197 char *end;
198 unsigned int coverage;
199
200 if (argc != 1)
201 return 1;
202
203 if (!*argv[0])
204 return 1;
205 coverage = strtoul(argv[0], &end, 10);
206 if (coverage > 255)
207 return 1;
208
209 if (*end)
210 return 1;
211
212 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS, coverage);
213
214 return 0;
215 nla_put_failure:
216 return -ENOBUFS;
217 }
218 COMMAND(set, coverage, "<coverage class>",
219 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_coverage,
220 "Set coverage class (1 for every 3 usec of air propagation time).\n"
221 "Valid values: 0 - 255.");
222
223 static int handle_distance(struct nl80211_state *state,
224 struct nl_cb *cb,
225 struct nl_msg *msg,
226 int argc, char **argv)
227 {
228 char *end;
229 unsigned int distance, coverage;
230
231 if (argc != 1)
232 return 1;
233
234 if (!*argv[0])
235 return 1;
236
237 distance = strtoul(argv[0], &end, 10);
238
239 if (*end)
240 return 1;
241
242 /*
243 * Divide double the distance by the speed of light in m/usec (300) to
244 * get round-trip time in microseconds and then divide the result by
245 * three to get coverage class as specified in IEEE 802.11-2007 table
246 * 7-27. Values are rounded upwards.
247 */
248 coverage = (distance + 449) / 450;
249 if (coverage > 255)
250 return 1;
251
252 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS, coverage);
253
254 return 0;
255 nla_put_failure:
256 return -ENOBUFS;
257 }
258 COMMAND(set, distance, "<distance>",
259 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_distance,
260 "Set appropriate coverage class for given link distance in meters.\n"
261 "Valid values: 0 - 114750");
262
263 static int handle_txpower(struct nl80211_state *state,
264 struct nl_cb *cb,
265 struct nl_msg *msg,
266 int argc, char **argv)
267 {
268 enum nl80211_tx_power_setting type;
269 int mbm;
270
271 /* get the required args */
272 if (argc != 1 && argc != 2)
273 return 1;
274
275 if (!strcmp(argv[0], "auto"))
276 type = NL80211_TX_POWER_AUTOMATIC;
277 else if (!strcmp(argv[0], "fixed"))
278 type = NL80211_TX_POWER_FIXED;
279 else if (!strcmp(argv[0], "limit"))
280 type = NL80211_TX_POWER_LIMITED;
281 else {
282 printf("Invalid parameter: %s\n", argv[0]);
283 return 2;
284 }
285
286 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_TX_POWER_SETTING, type);
287
288 if (type != NL80211_TX_POWER_AUTOMATIC) {
289 char *endptr;
290 if (argc != 2) {
291 printf("Missing TX power level argument.\n");
292 return 2;
293 }
294
295 mbm = strtol(argv[1], &endptr, 10);
296 if (*endptr)
297 return 2;
298 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL, mbm);
299 } else if (argc != 1)
300 return 1;
301
302 return 0;
303
304 nla_put_failure:
305 return -ENOBUFS;
306 }
307 COMMAND(set, txpower, "<auto|fixed|limit> [<tx power in mBm>]",
308 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_txpower,
309 "Specify transmit power level and setting type.");
310 COMMAND(set, txpower, "<auto|fixed|limit> [<tx power in mBm>]",
311 NL80211_CMD_SET_WIPHY, 0, CIB_NETDEV, handle_txpower,
312 "Specify transmit power level and setting type.");
313
314 static int handle_antenna(struct nl80211_state *state,
315 struct nl_cb *cb,
316 struct nl_msg *msg,
317 int argc, char **argv)
318 {
319 char *end;
320 uint32_t tx_ant = 0, rx_ant = 0;
321
322 if (argc == 1 && strcmp(argv[0], "all") == 0) {
323 tx_ant = 0xffffffff;
324 rx_ant = 0xffffffff;
325 } else if (argc == 1) {
326 tx_ant = rx_ant = strtoul(argv[0], &end, 0);
327 if (*end)
328 return 1;
329 }
330 else if (argc == 2) {
331 tx_ant = strtoul(argv[0], &end, 0);
332 if (*end)
333 return 1;
334 rx_ant = strtoul(argv[1], &end, 0);
335 if (*end)
336 return 1;
337 } else
338 return 1;
339
340 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
341 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
342
343 return 0;
344
345 nla_put_failure:
346 return -ENOBUFS;
347 }
348 COMMAND(set, antenna, "<bitmap> | all | <tx bitmap> <rx bitmap>",
349 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_antenna,
350 "Set a bitmap of allowed antennas to use for TX and RX.\n"
351 "The driver may reject antenna configurations it cannot support.");