]> git.ipfire.org Git - thirdparty/iw.git/blame - station.c
ship nl80211.h
[thirdparty/iw.git] / station.c
CommitLineData
2ef1be68 1#include <net/if.h>
3d1e8704 2#include <errno.h>
d5ac8ad3 3#include <string.h>
2ef1be68 4
3d1e8704
LCC
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>
3d1e8704 10
f408e01b 11#include "nl80211.h"
3d1e8704
LCC
12#include "iw.h"
13
14enum plink_state {
15 LISTEN,
16 OPN_SNT,
17 OPN_RCVD,
18 CNF_RCVD,
19 ESTAB,
20 HOLDING,
21 BLOCKED
22};
23
24enum plink_actions {
25 PLINK_ACTION_UNDEFINED,
26 PLINK_ACTION_OPEN,
27 PLINK_ACTION_BLOCK,
28};
29
30
3d1e8704
LCC
31static 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);
fbb181fa 68 printf("Station %s (on %s)", mac_addr, dev);
3d1e8704
LCC
69
70 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
fbb181fa 71 printf("\n\tinactive time:\t%d ms",
3d1e8704
LCC
72 nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]));
73 if (sinfo[NL80211_STA_INFO_RX_BYTES])
fbb181fa 74 printf("\n\trx bytes:\t%d",
3d1e8704
LCC
75 nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]));
76 if (sinfo[NL80211_STA_INFO_TX_BYTES])
fbb181fa 77 printf("\n\ttx bytes:\t%d",
3d1e8704
LCC
78 nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]));
79 if (sinfo[NL80211_STA_INFO_LLID])
fbb181fa 80 printf("\n\tmesh llid:\t%d",
3d1e8704
LCC
81 nla_get_u16(sinfo[NL80211_STA_INFO_LLID]));
82 if (sinfo[NL80211_STA_INFO_PLID])
fbb181fa 83 printf("\n\tmesh plid:\t%d",
3d1e8704
LCC
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 }
fbb181fa 112 printf("\n\tmesh plink:\t%s", state_name);
3d1e8704
LCC
113 }
114
115 printf("\n");
116 return NL_SKIP;
117}
118
70391ccf 119static int handle_station_get(struct nl_cb *cb,
b1ca19a8
JB
120 struct nl_msg *msg,
121 int argc, char **argv)
3d1e8704 122{
3d1e8704
LCC
123 unsigned char mac_addr[ETH_ALEN];
124
b1ca19a8 125 if (argc < 1)
5e75fd04 126 return 1;
3d1e8704
LCC
127
128 if (mac_addr_a2n(mac_addr, argv[0])) {
129 fprintf(stderr, "invalid mac address\n");
5e75fd04 130 return 2;
3d1e8704
LCC
131 }
132
133 argc--;
134 argv++;
135
b1ca19a8 136 if (argc)
5e75fd04 137 return 1;
3d1e8704
LCC
138
139 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
3d1e8704 140
3d1e8704 141 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
3d1e8704 142
70391ccf 143 return 0;
3d1e8704 144 nla_put_failure:
70391ccf 145 return -ENOBUFS;
3d1e8704 146}
b1ca19a8
JB
147COMMAND(station, get, "<MAC address>",
148 NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get);
149COMMAND(station, del, "<MAC address>",
150 NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_get);
3d1e8704 151
70391ccf 152static int handle_station_set(struct nl_cb *cb,
b1ca19a8
JB
153 struct nl_msg *msg,
154 int argc, char **argv)
3d1e8704 155{
3d1e8704
LCC
156 unsigned char plink_action;
157 unsigned char mac_addr[ETH_ALEN];
158
b1ca19a8 159 if (argc < 3)
5e75fd04 160 return 1;
3d1e8704
LCC
161
162 if (mac_addr_a2n(mac_addr, argv[0])) {
163 fprintf(stderr, "invalid mac address\n");
5e75fd04 164 return 2;
3d1e8704
LCC
165 }
166 argc--;
167 argv++;
168
b1ca19a8
JB
169 if (strcmp("plink_action", argv[0]) != 0)
170 return 1;
3d1e8704
LCC
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");
5e75fd04 180 return 2;
3d1e8704
LCC
181 }
182 argc--;
183 argv++;
184
b1ca19a8 185 if (argc)
5e75fd04 186 return 1;
3d1e8704
LCC
187
188 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
3d1e8704
LCC
189 NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action);
190
70391ccf 191 return 0;
3d1e8704 192 nla_put_failure:
70391ccf 193 return -ENOBUFS;
3d1e8704 194}
b1ca19a8
JB
195COMMAND(station, set, "<MAC address> plink_action <open|block>",
196 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set);
197
70391ccf 198static int handle_station_dump(struct nl_cb *cb,
b1ca19a8
JB
199 struct nl_msg *msg,
200 int argc, char **argv)
3d1e8704 201{
3d1e8704 202 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
70391ccf 203 return 0;
3d1e8704 204}
b1ca19a8 205COMMAND(station, dump, NULL,
f903d035 206 NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump);