]>
Commit | Line | Data |
---|---|---|
79f99b9a | 1 | #include <errno.h> |
2ef1be68 JB |
2 | #include <linux/nl80211.h> |
3 | #include <net/if.h> | |
4 | ||
79f99b9a JB |
5 | #include <netlink/genl/genl.h> |
6 | #include <netlink/genl/family.h> | |
7 | #include <netlink/genl/ctrl.h> | |
8 | #include <netlink/msg.h> | |
9 | #include <netlink/attr.h> | |
79f99b9a JB |
10 | |
11 | #include "iw.h" | |
12 | ||
13 | static void print_flag(const char *name, int *open) | |
14 | { | |
15 | if (!*open) | |
16 | printf(" ("); | |
17 | else | |
18 | printf(", "); | |
19 | printf(name); | |
20 | *open = 1; | |
21 | } | |
22 | ||
23 | static int print_phy_handler(struct nl_msg *msg, void *arg) | |
24 | { | |
25 | struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; | |
26 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
27 | ||
28 | struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1]; | |
29 | ||
30 | struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1]; | |
31 | static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { | |
32 | [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 }, | |
33 | [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG }, | |
34 | [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG }, | |
35 | [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG }, | |
36 | [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG }, | |
37 | }; | |
38 | ||
39 | struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1]; | |
40 | static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = { | |
41 | [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 }, | |
42 | [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG }, | |
43 | }; | |
44 | ||
6367e71a JB |
45 | static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = { |
46 | "unspecified", | |
47 | "IBSS", | |
48 | "Station", | |
49 | "AP", | |
50 | "AP(VLAN)", | |
51 | "WDS", | |
52 | "Monitor", | |
53 | "mesh point" | |
54 | }; | |
55 | ||
79f99b9a JB |
56 | struct nlattr *nl_band; |
57 | struct nlattr *nl_freq; | |
58 | struct nlattr *nl_rate; | |
6367e71a | 59 | struct nlattr *nl_mode; |
79f99b9a | 60 | int bandidx = 1; |
6367e71a | 61 | int rem_band, rem_freq, rem_rate, rem_mode; |
79f99b9a JB |
62 | int open; |
63 | ||
64 | nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
65 | genlmsg_attrlen(gnlh, 0), NULL); | |
66 | ||
67 | if (!tb_msg[NL80211_ATTR_WIPHY_BANDS]) | |
68 | return NL_SKIP; | |
69 | ||
70 | nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) { | |
71 | printf("Band %d:\n", bandidx); | |
72 | bandidx++; | |
73 | ||
74 | nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band), | |
75 | nla_len(nl_band), NULL); | |
76 | ||
77 | printf("\tFrequencies:\n"); | |
78 | ||
79 | nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) { | |
80 | nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq), | |
81 | nla_len(nl_freq), freq_policy); | |
82 | if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) | |
83 | continue; | |
84 | printf("\t\t* %d MHz", nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ])); | |
85 | open = 0; | |
86 | if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED]) | |
87 | print_flag("disabled", &open); | |
88 | if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]) | |
89 | print_flag("passive scanning", &open); | |
90 | if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS]) | |
91 | print_flag("no IBSS", &open); | |
92 | if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR]) | |
93 | print_flag("radar detection", &open); | |
94 | if (open) | |
95 | printf(")"); | |
96 | printf("\n"); | |
97 | } | |
98 | ||
99 | printf("\tBitrates:\n"); | |
100 | ||
101 | nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) { | |
102 | nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate), | |
103 | nla_len(nl_rate), rate_policy); | |
104 | if (!tb_rate[NL80211_BITRATE_ATTR_RATE]) | |
105 | continue; | |
106 | printf("\t\t* %2.1f Mbps", 0.1 * nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE])); | |
107 | open = 0; | |
108 | if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE]) | |
109 | print_flag("short preamble supported", &open); | |
110 | if (open) | |
111 | printf(")"); | |
112 | printf("\n"); | |
113 | } | |
114 | } | |
115 | ||
6367e71a JB |
116 | if (!tb_msg[NL80211_ATTR_SUPPORTED_IFTYPES]) |
117 | return NL_SKIP; | |
118 | ||
119 | printf("Supported interface modes:\n"); | |
120 | nla_for_each_nested(nl_mode, tb_msg[NL80211_ATTR_SUPPORTED_IFTYPES], rem_mode) { | |
121 | if (nl_mode->nla_type > NL80211_IFTYPE_MAX) | |
122 | printf("\t * Unknown mode (%d)\n", nl_mode->nla_type); | |
123 | else | |
124 | printf("\t * %s\n", ifmodes[nl_mode->nla_type]); | |
125 | } | |
126 | ||
79f99b9a JB |
127 | return NL_SKIP; |
128 | } | |
129 | ||
130 | ||
131 | ||
132 | static int ack_wait_handler(struct nl_msg *msg, void *arg) | |
133 | { | |
134 | int *finished = arg; | |
135 | ||
136 | *finished = 1; | |
137 | return NL_STOP; | |
138 | } | |
139 | ||
140 | int handle_info(struct nl80211_state *state, char *phy, char *dev) | |
141 | { | |
142 | struct nl_msg *msg; | |
143 | int err = -1; | |
144 | struct nl_cb *cb = NULL; | |
145 | int finished; | |
146 | ||
147 | msg = nlmsg_alloc(); | |
148 | if (!msg) { | |
149 | fprintf(stderr, "failed to allocate netlink msg\n"); | |
150 | return -1; | |
151 | } | |
152 | ||
153 | genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, | |
154 | 0, NL80211_CMD_GET_WIPHY, 0); | |
155 | if (dev) | |
156 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(dev)); | |
157 | if (phy) | |
158 | return -1; /* XXX TODO */ | |
159 | ||
160 | cb = nl_cb_alloc(NL_CB_CUSTOM); | |
161 | if (!cb) | |
162 | goto out; | |
163 | ||
164 | if (nl_send_auto_complete(state->nl_handle, msg) < 0) | |
165 | goto out; | |
166 | ||
167 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_phy_handler, NULL); | |
168 | nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished); | |
169 | ||
170 | err = nl_recvmsgs(state->nl_handle, cb); | |
171 | ||
172 | if (!finished) | |
173 | err = nl_wait_for_ack(state->nl_handle); | |
174 | ||
175 | if (err < 0) | |
176 | goto out; | |
177 | err = 0; | |
178 | ||
179 | out: | |
180 | nl_cb_put(cb); | |
181 | nla_put_failure: | |
182 | if (err) | |
183 | fprintf(stderr, "failed to get information: %d\n", err); | |
184 | nlmsg_free(msg); | |
185 | return err; | |
186 | } |