iw: backport support for "channels" command
[openwrt.git] / package / network / utils / iw / patches / 304-add-channels-PHY-command-listing-frequencies-with-mo.patch
1 From db9d4050d7dff994a43fa954ff47ac94a898f728 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <zajec5@gmail.com>
3 Date: Wed, 1 Jun 2016 07:51:14 +0200
4 Subject: [PATCH] add "channels" PHY command listing frequencies with more
5  details
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 Channels (frequencies) are getting more details that users may want to
11 know about. E.g. it's important to know which frequencies allow using
12 40/80/160 MHz channels to setup AP properly.
13
14 We list channels in "info" command output but it's already quite big and
15 it was agreed to introduce new command rather than expand the old one.
16
17 This patch adds "channels" command printing what was already available
18 in the "info" plus details about supported channel widths. It also
19 removes DFS info from the "info" output.
20
21 Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
22 Signed-off-by: Johannes Berg <johannes.berg@intel.com>
23 ---
24  info.c |  30 -------------
25  phy.c  | 152 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26  2 files changed, 152 insertions(+), 30 deletions(-)
27
28 --- a/info.c
29 +++ b/info.c
30 @@ -49,20 +49,6 @@ static char *cipher_name(__u32 c)
31         }
32  }
33  
34 -static char *dfs_state_name(enum nl80211_dfs_state state)
35 -{
36 -       switch (state) {
37 -       case NL80211_DFS_USABLE:
38 -               return "usable";
39 -       case NL80211_DFS_AVAILABLE:
40 -               return "available";
41 -       case NL80211_DFS_UNAVAILABLE:
42 -               return "unavailable";
43 -       default:
44 -               return "unknown";
45 -       }
46 -}
47 -
48  static int ext_feature_isset(const unsigned char *ext_features, int ext_features_len,
49                              enum nl80211_ext_feature_index ftidx)
50  {
51 @@ -200,22 +186,6 @@ next:
52                                         if (open)
53                                                 printf(")");
54                                         printf("\n");
55 -
56 -                                       if (!tb_freq[NL80211_FREQUENCY_ATTR_DISABLED] && tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
57 -                                               enum nl80211_dfs_state state = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
58 -                                               unsigned long time;
59 -
60 -                                               printf("\t\t\t  DFS state: %s", dfs_state_name(state));
61 -                                               if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_TIME]) {
62 -                                                       time = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_TIME]);
63 -                                                       printf(" (for %lu sec)", time/1000);
64 -                                               }
65 -                                               printf("\n");
66 -                                               if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME])
67 -                                                       printf("\t\t\t  DFS CAC time: %u ms\n",
68 -                                                              nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]));
69 -                                       }
70 -
71                                 }
72                         }
73  
74 --- a/phy.c
75 +++ b/phy.c
76 @@ -15,6 +15,158 @@
77  #include "nl80211.h"
78  #include "iw.h"
79  
80 +struct channels_ctx {
81 +       int last_band;
82 +       bool width_40;
83 +       bool width_80;
84 +       bool width_160;
85 +};
86 +
87 +static char *dfs_state_name(enum nl80211_dfs_state state)
88 +{
89 +       switch (state) {
90 +       case NL80211_DFS_USABLE:
91 +               return "usable";
92 +       case NL80211_DFS_AVAILABLE:
93 +               return "available";
94 +       case NL80211_DFS_UNAVAILABLE:
95 +               return "unavailable";
96 +       default:
97 +               return "unknown";
98 +       }
99 +}
100 +
101 +static int print_channels_handler(struct nl_msg *msg, void *arg)
102 +{
103 +       struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
104 +       struct channels_ctx *ctx = arg;
105 +       struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
106 +       struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
107 +       struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
108 +       struct nlattr *nl_band;
109 +       struct nlattr *nl_freq;
110 +       int rem_band, rem_freq;
111 +
112 +       nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
113 +
114 +       if (tb_msg[NL80211_ATTR_WIPHY_BANDS]) {
115 +               nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
116 +                       if (ctx->last_band != nl_band->nla_type) {
117 +                               printf("Band %d:\n", nl_band->nla_type + 1);
118 +                               ctx->width_40 = false;
119 +                               ctx->width_80 = false;
120 +                               ctx->width_160 = false;
121 +                               ctx->last_band = nl_band->nla_type;
122 +                       }
123 +
124 +                       nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band), nla_len(nl_band), NULL);
125 +
126 +                       if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
127 +                               __u16 cap = nla_get_u16(tb_band[NL80211_BAND_ATTR_HT_CAPA]);
128 +
129 +                               if (cap & BIT(1))
130 +                                       ctx->width_40 = true;
131 +                       }
132 +
133 +                       if (tb_band[NL80211_BAND_ATTR_VHT_CAPA]) {
134 +                               __u32 capa;
135 +
136 +                               ctx->width_80 = true;
137 +
138 +                               capa = nla_get_u32(tb_band[NL80211_BAND_ATTR_VHT_CAPA]);
139 +                               switch ((capa >> 2) & 3) {
140 +                               case 2:
141 +                                       /* width_80p80 = true; */
142 +                                       /* fall through */
143 +                               case 1:
144 +                                       ctx->width_160 = true;
145 +                               break;
146 +                               }
147 +                       }
148 +
149 +                       if (tb_band[NL80211_BAND_ATTR_FREQS]) {
150 +                               nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
151 +                                       uint32_t freq;
152 +
153 +                                       nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq), nla_len(nl_freq), NULL);
154 +
155 +                                       if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
156 +                                               continue;
157 +                                       freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
158 +                                       printf("\t* %d MHz [%d] ", freq, ieee80211_frequency_to_channel(freq));
159 +
160 +                                       if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED]) {
161 +                                               printf("(disabled)\n");
162 +                                               continue;
163 +                                       }
164 +                                       printf("\n");
165 +
166 +                                       if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])
167 +                                               printf("\t  Maximum TX power: %.1f dBm\n", 0.01 * nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
168 +
169 +                                       /* If both flags are set assume an new kernel */
170 +                                       if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR] && tb_freq[__NL80211_FREQUENCY_ATTR_NO_IBSS]) {
171 +                                               printf("\t  No IR\n");
172 +                                       } else if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]) {
173 +                                               printf("\t  Passive scan\n");
174 +                                       } else if (tb_freq[__NL80211_FREQUENCY_ATTR_NO_IBSS]){
175 +                                               printf("\t  No IBSS\n");
176 +                                       }
177 +
178 +                                       if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
179 +                                               printf("\t  Radar detection\n");
180 +
181 +                                       printf("\t  Channel widths:");
182 +                                       if (!tb_freq[NL80211_FREQUENCY_ATTR_NO_20MHZ])
183 +                                               printf(" 20MHz");
184 +                                       if (ctx->width_40 && !tb_freq[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS])
185 +                                               printf(" HT40-");
186 +                                       if (ctx->width_40 && !tb_freq[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS])
187 +                                               printf(" HT40+");
188 +                                       if (ctx->width_80 && !tb_freq[NL80211_FREQUENCY_ATTR_NO_80MHZ])
189 +                                               printf(" VHT80");
190 +                                       if (ctx->width_160 && !tb_freq[NL80211_FREQUENCY_ATTR_NO_160MHZ])
191 +                                               printf(" VHT160");
192 +                                       printf("\n");
193 +
194 +                                       if (!tb_freq[NL80211_FREQUENCY_ATTR_DISABLED] && tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
195 +                                               enum nl80211_dfs_state state = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
196 +                                               unsigned long time;
197 +
198 +                                               printf("\t  DFS state: %s", dfs_state_name(state));
199 +                                               if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_TIME]) {
200 +                                                       time = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_TIME]);
201 +                                                       printf(" (for %lu sec)", time / 1000);
202 +                                               }
203 +                                               printf("\n");
204 +                                               if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME])
205 +                                                       printf("\t  DFS CAC time: %u ms\n",
206 +                                                              nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]));
207 +                                       }
208 +                               }
209 +                       }
210 +               }
211 +       }
212 +
213 +       return NL_SKIP;
214 +}
215 +
216 +static int handle_channels(struct nl80211_state *state, struct nl_msg *msg,
217 +                          int argc, char **argv, enum id_input id)
218 +{
219 +       static struct channels_ctx ctx = {
220 +               .last_band = -1,
221 +       };
222 +
223 +       nla_put_flag(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
224 +       nlmsg_hdr(msg)->nlmsg_flags |= NLM_F_DUMP;
225 +
226 +       register_handler(print_channels_handler, &ctx);
227 +
228 +       return 0;
229 +}
230 +TOPLEVEL(channels, NULL, NL80211_CMD_GET_WIPHY, 0, CIB_PHY, handle_channels, "Show available channels.");
231 +
232  static int handle_name(struct nl80211_state *state,
233                        struct nl_msg *msg,
234                        int argc, char **argv,