]>
Commit | Line | Data |
---|---|---|
3563f4c5 JB |
1 | #include <net/if.h> |
2 | #include <errno.h> | |
3 | #include <string.h> | |
4 | #include <ctype.h> | |
764fe753 | 5 | #include <stdbool.h> |
3563f4c5 JB |
6 | |
7 | #include <netlink/genl/genl.h> | |
8 | #include <netlink/genl/family.h> | |
9 | #include <netlink/genl/ctrl.h> | |
10 | #include <netlink/msg.h> | |
11 | #include <netlink/attr.h> | |
12 | ||
13 | #include "nl80211.h" | |
14 | #include "iw.h" | |
15 | ||
764fe753 JB |
16 | struct scan_params { |
17 | bool unknown; | |
18 | }; | |
19 | ||
3563f4c5 JB |
20 | static int handle_scan(struct nl_cb *cb, |
21 | struct nl_msg *msg, | |
22 | int argc, char **argv) | |
23 | { | |
24 | struct nl_msg *ssids = NULL; | |
25 | int err = -ENOBUFS; | |
26 | ||
27 | ssids = nlmsg_alloc(); | |
28 | if (!ssids) | |
29 | return -ENOMEM; | |
30 | NLA_PUT(ssids, 1, 0, ""); | |
31 | nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids); | |
32 | ||
33 | err = 0; | |
34 | nla_put_failure: | |
35 | nlmsg_free(ssids); | |
36 | return err; | |
37 | } | |
38 | COMMAND(scan, trigger, NULL, | |
39 | NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan); | |
40 | ||
41 | typedef void (*printfn)(unsigned char type, unsigned char len, unsigned char *data); | |
42 | ||
43 | static void print_ssid(unsigned char type, unsigned char len, unsigned char *data) | |
44 | { | |
45 | int i; | |
46 | printf("\tSSID: "); | |
47 | for (i=0; i<len; i++) { | |
48 | if (isprint(data[i])) | |
49 | printf("%c", data[i]); | |
50 | else | |
51 | printf("\\x%.2x", data[i]); | |
52 | } | |
53 | printf("\n"); | |
54 | } | |
55 | ||
56 | static void print_supprates(unsigned char type, unsigned char len, unsigned char *data) | |
57 | { | |
58 | int i; | |
59 | ||
60 | if (type == 1) | |
61 | printf("\tSupported rates: "); | |
62 | else | |
63 | printf("\tExtended supported rates: "); | |
64 | ||
65 | for (i=0; i<len; i++) { | |
66 | int r = data[i] & 0x7f; | |
67 | printf("%d.%d%s ", r/2, 5*(r&1), data[i] & 0x80 ? "*":""); | |
68 | } | |
69 | printf("\n"); | |
70 | } | |
71 | ||
72 | static void print_ds(unsigned char type, unsigned char len, unsigned char *data) | |
73 | { | |
74 | printf("\tDS Parameter set: channel %d\n", data[0]); | |
75 | } | |
76 | ||
77 | static void print_ign(unsigned char type, unsigned char len, unsigned char *data) | |
78 | { | |
79 | /* ignore for now, not too useful */ | |
80 | } | |
81 | ||
764fe753 JB |
82 | static const printfn ieprinters[] = { |
83 | [0] = print_ssid, | |
84 | [1] = print_supprates, | |
85 | [3] = print_ds, | |
86 | [5] = print_ign, | |
87 | [50] = print_supprates, | |
88 | }; | |
89 | ||
90 | static void print_vendor(unsigned char len, unsigned char *data, | |
91 | struct scan_params *params) | |
3563f4c5 JB |
92 | { |
93 | int i; | |
94 | ||
764fe753 JB |
95 | /* currently _all_ vendor IEs are unknown (not parsed) */ |
96 | if (!params->unknown) | |
97 | return; | |
98 | ||
3563f4c5 JB |
99 | printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data: ", |
100 | data[0], data[1], data[2]); | |
101 | for (i=3; i<len; i++) | |
102 | printf("\\x%.2x", data[i]); | |
103 | printf("\n"); | |
104 | } | |
105 | ||
764fe753 | 106 | static void print_ies(unsigned char *ie, int ielen, struct scan_params *params) |
3563f4c5 JB |
107 | { |
108 | while (ielen >= 2 && ielen >= ie[1]) { | |
97ebbaf5 | 109 | if (ie[0] < ARRAY_SIZE(ieprinters) && ieprinters[ie[0]]) { |
3563f4c5 | 110 | ieprinters[ie[0]](ie[0], ie[1], ie + 2); |
764fe753 JB |
111 | } else if (ie[0] == 221 /* vendor */) { |
112 | print_vendor(ie[1], ie + 2, params); | |
113 | } else if (params->unknown) { | |
3563f4c5 JB |
114 | int i; |
115 | ||
116 | printf("\tUnknown IE (%d): ", ie[0]); | |
117 | for (i=0; i<ie[1]; i++) | |
118 | printf("\\x%.2x", ie[2+i]); | |
119 | printf("\n"); | |
120 | } | |
121 | ielen -= ie[1] + 2; | |
122 | ie += ie[1] + 2; | |
123 | } | |
124 | } | |
125 | ||
126 | static int print_bss_handler(struct nl_msg *msg, void *arg) | |
127 | { | |
128 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
129 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
130 | struct nlattr *bss[NL80211_BSS_MAX + 1]; | |
131 | char mac_addr[20], dev[20]; | |
132 | static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { | |
133 | [NL80211_BSS_TSF] = { .type = NLA_U64 }, | |
134 | [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, | |
135 | [NL80211_BSS_BSSID] = { }, | |
136 | [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, | |
137 | [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, | |
138 | [NL80211_BSS_INFORMATION_ELEMENTS] = { }, | |
f2e17e1f JB |
139 | [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, |
140 | [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, | |
3563f4c5 JB |
141 | }; |
142 | ||
143 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
144 | genlmsg_attrlen(gnlh, 0), NULL); | |
145 | ||
146 | if (!tb[NL80211_ATTR_BSS]) { | |
147 | fprintf(stderr, "bss info missing!"); | |
148 | return NL_SKIP; | |
149 | } | |
150 | if (nla_parse_nested(bss, NL80211_BSS_MAX, | |
151 | tb[NL80211_ATTR_BSS], | |
152 | bss_policy)) { | |
153 | fprintf(stderr, "failed to parse nested attributes!"); | |
154 | return NL_SKIP; | |
155 | } | |
156 | ||
157 | if (!bss[NL80211_BSS_BSSID]) | |
158 | return NL_SKIP; | |
159 | ||
160 | mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID])); | |
161 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev); | |
162 | printf("BSS %s (on %s)\n", mac_addr, dev); | |
163 | ||
e7109a8a JB |
164 | if (bss[NL80211_BSS_TSF]) { |
165 | unsigned long long tsf; | |
166 | tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]); | |
167 | printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n", | |
168 | tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24, | |
169 | (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60); | |
170 | } | |
3563f4c5 JB |
171 | if (bss[NL80211_BSS_FREQUENCY]) |
172 | printf("\tfreq: %d\n", | |
173 | nla_get_u32(bss[NL80211_BSS_FREQUENCY])); | |
174 | if (bss[NL80211_BSS_BEACON_INTERVAL]) | |
175 | printf("\tbeacon interval: %d\n", | |
176 | nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL])); | |
177 | if (bss[NL80211_BSS_CAPABILITY]) | |
178 | printf("\tcapability: 0x%.4x\n", | |
179 | nla_get_u16(bss[NL80211_BSS_CAPABILITY])); | |
f2e17e1f JB |
180 | if (bss[NL80211_BSS_SIGNAL_MBM]) { |
181 | int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]); | |
182 | printf("\tsignal: %d.%.2d dBm\n", s/100, s%100); | |
183 | } | |
184 | if (bss[NL80211_BSS_SIGNAL_UNSPEC]) { | |
185 | unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]); | |
186 | printf("\tsignal: %d/100\n", s); | |
187 | } | |
3563f4c5 JB |
188 | if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) |
189 | print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), | |
764fe753 JB |
190 | nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), |
191 | arg); | |
3563f4c5 JB |
192 | |
193 | return NL_SKIP; | |
194 | } | |
195 | ||
764fe753 | 196 | static struct scan_params scan_params; |
3563f4c5 JB |
197 | |
198 | static int handle_scan_dump(struct nl_cb *cb, | |
199 | struct nl_msg *msg, | |
200 | int argc, char **argv) | |
201 | { | |
764fe753 JB |
202 | if (argc > 1) |
203 | return 1; | |
204 | ||
205 | scan_params.unknown = false; | |
206 | if (argc == 1 && !strcmp(argv[0], "-u")) | |
207 | scan_params.unknown = true; | |
208 | ||
209 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler, | |
210 | &scan_params); | |
3563f4c5 JB |
211 | return 0; |
212 | } | |
764fe753 | 213 | COMMAND(scan, dump, "[-u]", |
3563f4c5 | 214 | NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump); |