-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiw.c
173 lines (135 loc) · 3.12 KB
/
iw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <syslog.h>
#include "nl80211.h"
#include "nlroute.h"
static int iface_info(const char *name)
{
struct nl80211_iface *iface, *p;
int idx = -1, err;
if (name) {
idx = nlr_iface_idx(name);
if (idx < 0) {
printf("Failed to get index of %s\n", name);
return -1;
}
}
iface = nl80211_iface(idx, &err);
for (p = iface; p; p = p->pnext) {
printf("\niface %s\n"
" idx: %d\n"
" phy: %d\n"
" ssid: %s\n"
" type: %s\n"
" mac: %02x:%02x:%02x:%02x:%02x:%02x\n"
" txpower: %d dBm\n"
" channel: %d\n"
" freq: %d MHz\n",
p->name, p->idx, p->wiphy, p->ssid, p->managed > 0 ? "managed" : !p->managed ? "AP" : "unknown",
p->mac[0], p->mac[1], p->mac[2], p->mac[3], p->mac[4], p->mac[5],
p->tx_power, p->channel, p->freq
);
}
nl80211_iface_free(iface);
return err;
}
static int add_ap(const char *ifname)
{
struct nl80211_iface *iface;
iface = NL80211_CREATE_AP(ifname);
if (!iface)
return -1;
nl80211_iface_free(iface);
return 0;
}
static int del_ap(const char *ifname)
{
int idx;
idx = nlr_iface_idx(ifname);
if (idx < 0) {
printf("Failed to get iface idx\n");
return -1;
}
return nl80211_del_iface(idx);
}
static void help(void)
{
printf("\nUtil for managing Wi-Fi.\n" \
"\nUsage: [options] cmd [cmd-args]" \
"\nOptions: -d -- log level info, -d2 --log level debug, -h -- help" \
"\n $ iw [show [IFNAME]]" \
"\n $ iw add ap IFNAME" \
"\n $ iw del iface IFNAME" \
"\n" \
);
}
int main(int argc, char *argv[])
{
int r = -1, i = 1, logmask = LOG_MASK(LOG_ERR);
if (argv[i]) {
if (!strcmp(argv[i], "-d")) {
logmask |= LOG_MASK(LOG_INFO);
i++;
} else if (!strcmp(argv[1], "-d2")) {
logmask |= LOG_MASK(LOG_INFO) |
LOG_MASK(LOG_DEBUG);
/* Enable debugging in the libnel */
setenv("LIBNEL_DEBUG", "1", 0);
i++;
} else if (!strcmp(argv[1], "-h")) {
help();
return 0;
}
}
openlog(NULL, LOG_PERROR, LOG_USER);
setlogmask(logmask);
if (nl80211_init() || nlr_init())
goto fin;
if (!argv[i]) {
r = iface_info(argv[i]);
} else if (!strcmp(argv[i], "show")) {
r = iface_info(argv[i + 1]);
} else if (!strcmp(argv[i], "add")) {
if (!argv[i + 1] || strcmp(argv[i + 1], "ap")) {
printf("Expected 'ap', get '%s'\n", argv[i + 1]);
goto help;
}
if (!argv[i + 2]) {
printf("Missing iface name\n");
goto help;
}
if (argv[i + 3]) {
printf("Too many args\n");
goto help;
}
r = add_ap(argv[i + 2]);
} else if (!strcmp(argv[i], "del")) {
if (!argv[i + 1] || strcmp(argv[i + 1], "iface")) {
printf("Expected 'iface', get '%s'\n",
argv[i + 1]);
goto help;
}
if (!argv[i + 2]) {
printf("Missing iface name\n");
goto help;
}
if (argv[i + 3]) {
printf("Too many args\n");
goto help;
}
r = del_ap(argv[i + 2]);
} else {
printf("Unknown command.\n");
goto help;
}
fin:
nl80211_fin();
nlr_fin();
closelog();
printf("%s\n", r ? "Failed" : "OK");
return r;
help:
help();
goto fin;
}