]>
Commit | Line | Data |
---|---|---|
379f8397 | 1 | #include <stdbool.h> |
0f55e0b8 | 2 | #include <errno.h> |
0f55e0b8 | 3 | #include <net/if.h> |
d0260390 | 4 | #include <strings.h> |
0f55e0b8 JB |
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 | ||
f408e01b | 12 | #include "nl80211.h" |
0f55e0b8 JB |
13 | #include "iw.h" |
14 | ||
7c37a24d JB |
15 | static int handle_name(struct nl80211_state *state, |
16 | struct nl_cb *cb, | |
0f55e0b8 JB |
17 | struct nl_msg *msg, |
18 | int argc, char **argv) | |
19 | { | |
0f55e0b8 | 20 | if (argc != 1) |
5e75fd04 | 21 | return 1; |
0f55e0b8 JB |
22 | |
23 | NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, *argv); | |
24 | ||
70391ccf | 25 | return 0; |
0f55e0b8 | 26 | nla_put_failure: |
70391ccf | 27 | return -ENOBUFS; |
0f55e0b8 | 28 | } |
cea8fa1c JB |
29 | COMMAND(set, name, "<new name>", NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_name, |
30 | "Rename this wireless device."); | |
b822cda9 | 31 | |
379f8397 JB |
32 | static int handle_freqchan(struct nl_msg *msg, bool chan, |
33 | int argc, char **argv) | |
b822cda9 | 34 | { |
e86b7e02 | 35 | char *end; |
b822cda9 JB |
36 | static const struct { |
37 | const char *name; | |
38 | unsigned int val; | |
39 | } htmap[] = { | |
68632dc7 JB |
40 | { .name = "HT20", .val = NL80211_CHAN_HT20, }, |
41 | { .name = "HT40+", .val = NL80211_CHAN_HT40PLUS, }, | |
42 | { .name = "HT40-", .val = NL80211_CHAN_HT40MINUS, }, | |
b822cda9 | 43 | }; |
68632dc7 | 44 | unsigned int htval = NL80211_CHAN_NO_HT; |
b822cda9 JB |
45 | unsigned int freq; |
46 | int i; | |
47 | ||
48 | if (!argc || argc > 2) | |
49 | return 1; | |
50 | ||
51 | if (argc == 2) { | |
7d736016 | 52 | for (i = 0; i < ARRAY_SIZE(htmap); i++) { |
b822cda9 JB |
53 | if (strcasecmp(htmap[i].name, argv[1]) == 0) { |
54 | htval = htmap[i].val; | |
55 | break; | |
56 | } | |
57 | } | |
68632dc7 | 58 | if (htval == NL80211_CHAN_NO_HT) |
b822cda9 JB |
59 | return 1; |
60 | } | |
61 | ||
e86b7e02 JB |
62 | if (!*argv[0]) |
63 | return 1; | |
64 | freq = strtoul(argv[0], &end, 10); | |
65 | if (*end) | |
66 | return 1; | |
67 | ||
379f8397 JB |
68 | if (chan) |
69 | freq = ieee80211_channel_to_frequency(freq); | |
b822cda9 JB |
70 | |
71 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); | |
68632dc7 | 72 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, htval); |
b822cda9 JB |
73 | |
74 | return 0; | |
75 | nla_put_failure: | |
76 | return -ENOBUFS; | |
77 | } | |
379f8397 | 78 | |
7c37a24d JB |
79 | static int handle_freq(struct nl80211_state *state, |
80 | struct nl_cb *cb, struct nl_msg *msg, | |
379f8397 JB |
81 | int argc, char **argv) |
82 | { | |
83 | return handle_freqchan(msg, false, argc, argv); | |
84 | } | |
b822cda9 | 85 | COMMAND(set, freq, "<freq> [HT20|HT40+|HT40-]", |
00c448b2 JB |
86 | NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_freq, |
87 | "Set frequency/channel the hardware is using, including HT\n" | |
88 | "configuration."); | |
b822cda9 | 89 | COMMAND(set, freq, "<freq> [HT20|HT40+|HT40-]", |
01ae06f9 | 90 | NL80211_CMD_SET_WIPHY, 0, CIB_NETDEV, handle_freq, NULL); |
379f8397 | 91 | |
7c37a24d JB |
92 | static int handle_chan(struct nl80211_state *state, |
93 | struct nl_cb *cb, struct nl_msg *msg, | |
379f8397 JB |
94 | int argc, char **argv) |
95 | { | |
96 | return handle_freqchan(msg, true, argc, argv); | |
97 | } | |
98 | COMMAND(set, channel, "<channel> [HT20|HT40+|HT40-]", | |
01ae06f9 | 99 | NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_chan, NULL); |
379f8397 | 100 | COMMAND(set, channel, "<channel> [HT20|HT40+|HT40-]", |
01ae06f9 | 101 | NL80211_CMD_SET_WIPHY, 0, CIB_NETDEV, handle_chan, NULL); |
625aa4ae JB |
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; | |
e86b7e02 JB |
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 | } | |
625aa4ae JB |
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; | |
e86b7e02 JB |
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 | } | |
625aa4ae JB |
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."); | |
e960e066 JB |
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 | ||
e86b7e02 JB |
175 | if (!*argv[0]) |
176 | return 1; | |
177 | ||
e960e066 | 178 | NLA_PUT_U32(msg, NL80211_ATTR_PID, |
c551449a | 179 | strtoul(argv[0], &end, 10)); |
e960e066 JB |
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"); | |
b2f92dd0 LT |
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 | { | |
e86b7e02 | 197 | char *end; |
b2f92dd0 LT |
198 | unsigned int coverage; |
199 | ||
200 | if (argc != 1) | |
201 | return 1; | |
202 | ||
e86b7e02 JB |
203 | if (!*argv[0]) |
204 | return 1; | |
205 | coverage = strtoul(argv[0], &end, 10); | |
b2f92dd0 LT |
206 | if (coverage > 255) |
207 | return 1; | |
208 | ||
e86b7e02 JB |
209 | if (*end) |
210 | return 1; | |
211 | ||
b2f92dd0 LT |
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 | { | |
e86b7e02 | 228 | char *end; |
b2f92dd0 LT |
229 | unsigned int distance, coverage; |
230 | ||
231 | if (argc != 1) | |
232 | return 1; | |
233 | ||
e86b7e02 JB |
234 | if (!*argv[0]) |
235 | return 1; | |
236 | ||
237 | distance = strtoul(argv[0], &end, 10); | |
238 | ||
239 | if (*end) | |
240 | return 1; | |
b2f92dd0 LT |
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"); | |
a0b1f574 JO |
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) { | |
18e05613 | 289 | char *endptr; |
a0b1f574 JO |
290 | if (argc != 2) { |
291 | printf("Missing TX power level argument.\n"); | |
292 | return 2; | |
293 | } | |
294 | ||
18e05613 | 295 | mbm = strtol(argv[1], &endptr, 10); |
08ec4c6b | 296 | if (*endptr) |
18e05613 | 297 | return 2; |
a0b1f574 JO |
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."); | |
afce7986 BR |
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."); |