]> git.ipfire.org Git - thirdparty/iw.git/blob - ibss.c
iw: document handler return value 1 as HANDLER_RET_USAGE
[thirdparty/iw.git] / ibss.c
1 #ifndef _POSIX_SOURCE
2 #define _POSIX_SOURCE
3 #endif
4 #include <errno.h>
5 #include <string.h>
6 #include <strings.h>
7
8 #include <netlink/genl/genl.h>
9 #include <netlink/genl/family.h>
10 #include <netlink/genl/ctrl.h>
11 #include <netlink/msg.h>
12 #include <netlink/attr.h>
13
14 #include "nl80211.h"
15 #include "iw.h"
16
17 SECTION(ibss);
18
19 static int join_ibss(struct nl80211_state *state,
20 struct nl_msg *msg,
21 int argc, char **argv,
22 enum id_input id)
23 {
24 char *end;
25 unsigned char abssid[6];
26 unsigned char rates[NL80211_MAX_SUPP_RATES];
27 int n_rates = 0;
28 char *value = NULL, *sptr = NULL;
29 float rate;
30 int bintval;
31 unsigned int i;
32 unsigned long freq;
33 const struct chanmode *chanmode_selected = NULL;
34 static const struct chanmode chanmode[] = {
35 { .name = "HT20",
36 .width = NL80211_CHAN_WIDTH_20,
37 .freq1_diff = 0,
38 .chantype = NL80211_CHAN_HT20 },
39 { .name = "HT40+",
40 .width = NL80211_CHAN_WIDTH_40,
41 .freq1_diff = 10,
42 .chantype = NL80211_CHAN_HT40PLUS },
43 { .name = "HT40-",
44 .width = NL80211_CHAN_WIDTH_40,
45 .freq1_diff = -10,
46 .chantype = NL80211_CHAN_HT40MINUS },
47 { .name = "NOHT",
48 .width = NL80211_CHAN_WIDTH_20_NOHT,
49 .freq1_diff = 0,
50 .chantype = NL80211_CHAN_NO_HT },
51 { .name = "5MHz",
52 .width = NL80211_CHAN_WIDTH_5,
53 .freq1_diff = 0,
54 .chantype = -1 },
55 { .name = "10MHz",
56 .width = NL80211_CHAN_WIDTH_10,
57 .freq1_diff = 0,
58 .chantype = -1 },
59 { .name = "80MHz",
60 .width = NL80211_CHAN_WIDTH_80,
61 .freq1_diff = 0,
62 .chantype = -1 },
63 };
64
65 if (argc < 2)
66 return 1;
67
68 /* SSID */
69 NLA_PUT(msg, NL80211_ATTR_SSID, strlen(argv[0]), argv[0]);
70 argv++;
71 argc--;
72
73 /* freq */
74 freq = strtoul(argv[0], &end, 10);
75 if (*end != '\0')
76 return 1;
77
78 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
79 argv++;
80 argc--;
81
82 if (argc) {
83 for (i = 0; i < ARRAY_SIZE(chanmode); i++) {
84 if (strcasecmp(chanmode[i].name, argv[0]) == 0) {
85 chanmode_selected = &chanmode[i];
86 break;
87 }
88 }
89 if (chanmode_selected) {
90 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
91 chanmode_selected->width);
92 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1,
93 get_cf1(chanmode_selected, freq));
94 if (chanmode_selected->chantype != -1)
95 NLA_PUT_U32(msg,
96 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
97 chanmode_selected->chantype);
98
99 argv++;
100 argc--;
101 }
102
103 }
104
105 if (argc && strcmp(argv[0], "fixed-freq") == 0) {
106 NLA_PUT_FLAG(msg, NL80211_ATTR_FREQ_FIXED);
107 argv++;
108 argc--;
109 }
110
111 if (argc) {
112 if (mac_addr_a2n(abssid, argv[0]) == 0) {
113 NLA_PUT(msg, NL80211_ATTR_MAC, 6, abssid);
114 argv++;
115 argc--;
116 }
117 }
118
119 if (argc > 1 && strcmp(argv[0], "beacon-interval") == 0) {
120 argv++;
121 argc--;
122 bintval = strtoul(argv[0], &end, 10);
123 if (*end != '\0')
124 return 1;
125 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, bintval);
126 argv++;
127 argc--;
128 }
129
130 /* basic rates */
131 if (argc > 1 && strcmp(argv[0], "basic-rates") == 0) {
132 argv++;
133 argc--;
134
135 value = strtok_r(argv[0], ",", &sptr);
136
137 while (value && n_rates < NL80211_MAX_SUPP_RATES) {
138 rate = strtod(value, &end);
139 rates[n_rates] = rate * 2;
140
141 /* filter out suspicious values */
142 if (*end != '\0' || !rates[n_rates] ||
143 rate*2 != rates[n_rates])
144 return 1;
145
146 n_rates++;
147 value = strtok_r(NULL, ",", &sptr);
148 }
149
150 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, n_rates, rates);
151
152 argv++;
153 argc--;
154 }
155
156 /* multicast rate */
157 if (argc > 1 && strcmp(argv[0], "mcast-rate") == 0) {
158 argv++;
159 argc--;
160
161 rate = strtod(argv[0], &end);
162 if (*end != '\0')
163 return 1;
164
165 NLA_PUT_U32(msg, NL80211_ATTR_MCAST_RATE, (int)(rate * 10));
166 argv++;
167 argc--;
168 }
169
170 if (!argc)
171 return 0;
172
173 if (strcmp(*argv, "key") != 0 && strcmp(*argv, "keys") != 0)
174 return 1;
175
176 argv++;
177 argc--;
178
179 return parse_keys(msg, argv, argc);
180 nla_put_failure:
181 return -ENOSPC;
182 }
183
184 static int leave_ibss(struct nl80211_state *state,
185 struct nl_msg *msg,
186 int argc, char **argv,
187 enum id_input id)
188 {
189 return 0;
190 }
191 COMMAND(ibss, leave, NULL,
192 NL80211_CMD_LEAVE_IBSS, 0, CIB_NETDEV, leave_ibss,
193 "Leave the current IBSS cell.");
194 COMMAND(ibss, join,
195 "<SSID> <freq in MHz> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [fixed-freq] [<fixed bssid>] [beacon-interval <TU>]"
196 " [basic-rates <rate in Mbps,rate2,...>] [mcast-rate <rate in Mbps>] "
197 "[key d:0:abcde]",
198 NL80211_CMD_JOIN_IBSS, 0, CIB_NETDEV, join_ibss,
199 "Join the IBSS cell with the given SSID, if it doesn't exist create\n"
200 "it on the given frequency. When fixed frequency is requested, don't\n"
201 "join/create a cell on a different frequency. When a fixed BSSID is\n"
202 "requested use that BSSID and do not adopt another cell's BSSID even\n"
203 "if it has higher TSF and the same SSID. If an IBSS is created, create\n"
204 "it with the specified basic-rates, multicast-rate and beacon-interval.");