]> git.ipfire.org Git - thirdparty/iw.git/blob - station.c
fix typo in station code
[thirdparty/iw.git] / station.c
1 #include <linux/nl80211.h>
2 #include <net/if.h>
3 #include <errno.h>
4 #include <string.h>
5
6 #include <netlink/genl/genl.h>
7 #include <netlink/genl/family.h>
8 #include <netlink/genl/ctrl.h>
9 #include <netlink/msg.h>
10 #include <netlink/attr.h>
11
12 #include "iw.h"
13
14 enum plink_state {
15 LISTEN,
16 OPN_SNT,
17 OPN_RCVD,
18 CNF_RCVD,
19 ESTAB,
20 HOLDING,
21 BLOCKED
22 };
23
24 enum plink_actions {
25 PLINK_ACTION_UNDEFINED,
26 PLINK_ACTION_OPEN,
27 PLINK_ACTION_BLOCK,
28 };
29
30
31 static int print_sta_handler(struct nl_msg *msg, void *arg)
32 {
33 struct nlattr *tb[NL80211_ATTR_MAX + 1];
34 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
35 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
36 char mac_addr[20], state_name[10], dev[20];
37 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
38 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
39 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
40 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
41 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
42 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
43 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
44 };
45
46 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
47 genlmsg_attrlen(gnlh, 0), NULL);
48
49 /*
50 * TODO: validate the interface and mac address!
51 * Otherwise, there's a race condition as soon as
52 * the kernel starts sending station notifications.
53 */
54
55 if (!tb[NL80211_ATTR_STA_INFO]) {
56 fprintf(stderr, "sta stats missing!");
57 return NL_SKIP;
58 }
59 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
60 tb[NL80211_ATTR_STA_INFO],
61 stats_policy)) {
62 fprintf(stderr, "failed to parse nested attributes!");
63 return NL_SKIP;
64 }
65
66 mac_addr_n2a(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
67 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
68 printf("Station %s (on %s)", mac_addr, dev);
69
70 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
71 printf("\n\tinactive time:\t%d ms",
72 nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]));
73 if (sinfo[NL80211_STA_INFO_RX_BYTES])
74 printf("\n\trx bytes:\t%d",
75 nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]));
76 if (sinfo[NL80211_STA_INFO_TX_BYTES])
77 printf("\n\ttx bytes:\t%d",
78 nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]));
79 if (sinfo[NL80211_STA_INFO_LLID])
80 printf("\n\tmesh llid:\t%d",
81 nla_get_u16(sinfo[NL80211_STA_INFO_LLID]));
82 if (sinfo[NL80211_STA_INFO_PLID])
83 printf("\n\tmesh plid:\t%d",
84 nla_get_u16(sinfo[NL80211_STA_INFO_PLID]));
85 if (sinfo[NL80211_STA_INFO_PLINK_STATE]) {
86 switch (nla_get_u16(sinfo[NL80211_STA_INFO_PLINK_STATE])) {
87 case LISTEN:
88 strcpy(state_name, "LISTEN");
89 break;
90 case OPN_SNT:
91 strcpy(state_name, "OPN_SNT");
92 break;
93 case OPN_RCVD:
94 strcpy(state_name, "OPN_RCVD");
95 break;
96 case CNF_RCVD:
97 strcpy(state_name, "CNF_RCVD");
98 break;
99 case ESTAB:
100 strcpy(state_name, "ESTAB");
101 break;
102 case HOLDING:
103 strcpy(state_name, "HOLDING");
104 break;
105 case BLOCKED:
106 strcpy(state_name, "BLOCKED");
107 break;
108 default:
109 strcpy(state_name, "UNKNOWN");
110 break;
111 }
112 printf("\n\tmesh plink:\t%s", state_name);
113 }
114
115 printf("\n");
116 return NL_SKIP;
117 }
118
119 static int handle_station_get(struct nl_cb *cb,
120 struct nl_msg *msg,
121 int argc, char **argv)
122 {
123 unsigned char mac_addr[ETH_ALEN];
124
125 if (argc < 1)
126 return 1;
127
128 if (mac_addr_a2n(mac_addr, argv[0])) {
129 fprintf(stderr, "invalid mac address\n");
130 return 2;
131 }
132
133 argc--;
134 argv++;
135
136 if (argc)
137 return 1;
138
139 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
140
141 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
142
143 return 0;
144 nla_put_failure:
145 return -ENOBUFS;
146 }
147 COMMAND(station, get, "<MAC address>",
148 NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get);
149 COMMAND(station, del, "<MAC address>",
150 NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_get);
151
152 static int handle_station_set(struct nl_cb *cb,
153 struct nl_msg *msg,
154 int argc, char **argv)
155 {
156 unsigned char plink_action;
157 unsigned char mac_addr[ETH_ALEN];
158
159 if (argc < 3)
160 return 1;
161
162 if (mac_addr_a2n(mac_addr, argv[0])) {
163 fprintf(stderr, "invalid mac address\n");
164 return 2;
165 }
166 argc--;
167 argv++;
168
169 if (strcmp("plink_action", argv[0]) != 0)
170 return 1;
171 argc--;
172 argv++;
173
174 if (strcmp("open", argv[0]) == 0)
175 plink_action = PLINK_ACTION_OPEN;
176 else if (strcmp("block", argv[0]) == 0)
177 plink_action = PLINK_ACTION_BLOCK;
178 else {
179 fprintf(stderr, "plink action not supported\n");
180 return 2;
181 }
182 argc--;
183 argv++;
184
185 if (argc)
186 return 1;
187
188 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
189 NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action);
190
191 return 0;
192 nla_put_failure:
193 return -ENOBUFS;
194 }
195 COMMAND(station, set, "<MAC address> plink_action <open|block>",
196 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set);
197
198 static int handle_station_dump(struct nl_cb *cb,
199 struct nl_msg *msg,
200 int argc, char **argv)
201 {
202 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
203 return 0;
204 }
205 COMMAND(station, dump, NULL,
206 NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump);