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>
26 static void print_power_mode(struct nlattr
*a
)
28 enum nl80211_mesh_power_mode pm
= nla_get_u32(a
);
31 case NL80211_MESH_POWER_ACTIVE
:
34 case NL80211_MESH_POWER_LIGHT_SLEEP
:
35 printf("LIGHT SLEEP");
37 case NL80211_MESH_POWER_DEEP_SLEEP
:
46 static int print_sta_handler(struct nl_msg
*msg
, void *arg
)
48 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
49 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
50 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
51 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
52 char mac_addr
[20], state_name
[10], dev
[20];
53 struct nl80211_sta_flag_update
*sta_flags
;
54 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
55 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
56 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
57 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
58 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
59 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
60 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
61 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
62 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
63 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
64 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
65 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
66 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
67 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
68 [NL80211_STA_INFO_STA_FLAGS
] =
69 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
70 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
71 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
72 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
75 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
76 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
77 [NL80211_RATE_INFO_BITRATE32
] = { .type
= NLA_U32
},
78 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
79 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
80 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
83 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
84 genlmsg_attrlen(gnlh
, 0), NULL
);
87 * TODO: validate the interface and mac address!
88 * Otherwise, there's a race condition as soon as
89 * the kernel starts sending station notifications.
92 if (!tb
[NL80211_ATTR_STA_INFO
]) {
93 fprintf(stderr
, "sta stats missing!\n");
96 if (nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
97 tb
[NL80211_ATTR_STA_INFO
],
99 fprintf(stderr
, "failed to parse nested attributes!\n");
103 mac_addr_n2a(mac_addr
, nla_data(tb
[NL80211_ATTR_MAC
]));
104 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
105 printf("Station %s (on %s)", mac_addr
, dev
);
107 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
108 printf("\n\tinactive time:\t%u ms",
109 nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]));
110 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
111 printf("\n\trx bytes:\t%u",
112 nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]));
113 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
114 printf("\n\trx packets:\t%u",
115 nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]));
116 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
117 printf("\n\ttx bytes:\t%u",
118 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]));
119 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
120 printf("\n\ttx packets:\t%u",
121 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]));
122 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
123 printf("\n\ttx retries:\t%u",
124 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]));
125 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
126 printf("\n\ttx failed:\t%u",
127 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]));
128 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
129 printf("\n\tsignal: \t%d dBm",
130 (int8_t)nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]));
131 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
132 printf("\n\tsignal avg:\t%d dBm",
133 (int8_t)nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]));
134 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
135 printf("\n\tToffset:\t%lld us",
136 (unsigned long long)nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]));
138 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
]) {
139 if (nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
140 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
)) {
141 fprintf(stderr
, "failed to parse nested rate attributes!\n");
144 printf("\n\ttx bitrate:\t");
145 if (rinfo
[NL80211_RATE_INFO_BITRATE32
])
146 rate
= nla_get_u32(rinfo
[NL80211_RATE_INFO_BITRATE32
]);
147 else if (rinfo
[NL80211_RATE_INFO_BITRATE
])
148 rate
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
150 printf("%d.%d MBit/s", rate
/ 10, rate
% 10);
152 if (rinfo
[NL80211_RATE_INFO_MCS
])
153 printf(" MCS %d", nla_get_u8(rinfo
[NL80211_RATE_INFO_MCS
]));
154 if (rinfo
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
156 if (rinfo
[NL80211_RATE_INFO_SHORT_GI
])
161 if (sinfo
[NL80211_STA_INFO_LLID
])
162 printf("\n\tmesh llid:\t%d",
163 nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]));
164 if (sinfo
[NL80211_STA_INFO_PLID
])
165 printf("\n\tmesh plid:\t%d",
166 nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]));
167 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
]) {
168 switch (nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
])) {
170 strcpy(state_name
, "LISTEN");
173 strcpy(state_name
, "OPN_SNT");
176 strcpy(state_name
, "OPN_RCVD");
179 strcpy(state_name
, "CNF_RCVD");
182 strcpy(state_name
, "ESTAB");
185 strcpy(state_name
, "HOLDING");
188 strcpy(state_name
, "BLOCKED");
191 strcpy(state_name
, "UNKNOWN");
194 printf("\n\tmesh plink:\t%s", state_name
);
196 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
]) {
197 printf("\n\tmesh local PS mode:\t");
198 print_power_mode(sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
200 if (sinfo
[NL80211_STA_INFO_PEER_PM
]) {
201 printf("\n\tmesh peer PS mode:\t");
202 print_power_mode(sinfo
[NL80211_STA_INFO_PEER_PM
]);
204 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
]) {
205 printf("\n\tmesh non-peer PS mode:\t");
206 print_power_mode(sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
209 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
]) {
210 sta_flags
= (struct nl80211_sta_flag_update
*)
211 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
213 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
)) {
214 printf("\n\tauthorized:\t");
215 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
221 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
)) {
222 printf("\n\tauthenticated:\t");
223 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
229 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
)) {
230 printf("\n\tpreamble:\t");
231 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
237 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
)) {
238 printf("\n\tWMM/WME:\t");
239 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
245 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
)) {
246 printf("\n\tMFP:\t\t");
247 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
253 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
)) {
254 printf("\n\tTDLS peer:\t\t");
255 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
266 static int handle_station_get(struct nl80211_state
*state
,
269 int argc
, char **argv
,
272 unsigned char mac_addr
[ETH_ALEN
];
277 if (mac_addr_a2n(mac_addr
, argv
[0])) {
278 fprintf(stderr
, "invalid mac address\n");
288 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
290 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_sta_handler
, NULL
);
296 COMMAND(station
, get
, "<MAC address>",
297 NL80211_CMD_GET_STATION
, 0, CIB_NETDEV
, handle_station_get
,
298 "Get information for a specific station.");
299 COMMAND(station
, del
, "<MAC address>",
300 NL80211_CMD_DEL_STATION
, 0, CIB_NETDEV
, handle_station_get
,
301 "Remove the given station entry (use with caution!)");
303 static const struct cmd
*station_set_plink
;
304 static const struct cmd
*station_set_vlan
;
305 static const struct cmd
*station_set_mesh_power_mode
;
307 static const struct cmd
*select_station_cmd(int argc
, char **argv
)
311 if (strcmp(argv
[1], "plink_action") == 0)
312 return station_set_plink
;
313 if (strcmp(argv
[1], "vlan") == 0)
314 return station_set_vlan
;
315 if (strcmp(argv
[1], "mesh_power_mode") == 0)
316 return station_set_mesh_power_mode
;
320 static int handle_station_set_plink(struct nl80211_state
*state
,
323 int argc
, char **argv
,
326 unsigned char plink_action
;
327 unsigned char mac_addr
[ETH_ALEN
];
332 if (mac_addr_a2n(mac_addr
, argv
[0])) {
333 fprintf(stderr
, "invalid mac address\n");
339 if (strcmp("plink_action", argv
[0]) != 0)
344 if (strcmp("open", argv
[0]) == 0)
345 plink_action
= NL80211_PLINK_ACTION_OPEN
;
346 else if (strcmp("block", argv
[0]) == 0)
347 plink_action
= NL80211_PLINK_ACTION_BLOCK
;
349 fprintf(stderr
, "plink action not supported\n");
358 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
359 NLA_PUT_U8(msg
, NL80211_ATTR_STA_PLINK_ACTION
, plink_action
);
365 COMMAND_ALIAS(station
, set
, "<MAC address> plink_action <open|block>",
366 NL80211_CMD_SET_STATION
, 0, CIB_NETDEV
, handle_station_set_plink
,
367 "Set mesh peer link action for this station (peer).",
368 select_station_cmd
, station_set_plink
);
370 static int handle_station_set_vlan(struct nl80211_state
*state
,
373 int argc
, char **argv
,
376 unsigned char mac_addr
[ETH_ALEN
];
377 unsigned long sta_vlan
= 0;
383 if (mac_addr_a2n(mac_addr
, argv
[0])) {
384 fprintf(stderr
, "invalid mac address\n");
390 if (strcmp("vlan", argv
[0]) != 0)
395 sta_vlan
= strtoul(argv
[0], &err
, 0);
397 fprintf(stderr
, "invalid vlan id\n");
406 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
407 NLA_PUT_U32(msg
, NL80211_ATTR_STA_VLAN
, sta_vlan
);
413 COMMAND_ALIAS(station
, set
, "<MAC address> vlan <ifindex>",
414 NL80211_CMD_SET_STATION
, 0, CIB_NETDEV
, handle_station_set_vlan
,
415 "Set an AP VLAN for this station.",
416 select_station_cmd
, station_set_vlan
);
418 static int handle_station_set_mesh_power_mode(struct nl80211_state
*state
,
421 int argc
, char **argv
,
424 unsigned char mesh_power_mode
;
425 unsigned char mac_addr
[ETH_ALEN
];
430 if (mac_addr_a2n(mac_addr
, argv
[0])) {
431 fprintf(stderr
, "invalid mac address\n");
437 if (strcmp("mesh_power_mode", argv
[0]) != 0)
442 if (strcmp("active", argv
[0]) == 0)
443 mesh_power_mode
= NL80211_MESH_POWER_ACTIVE
;
444 else if (strcmp("light", argv
[0]) == 0)
445 mesh_power_mode
= NL80211_MESH_POWER_LIGHT_SLEEP
;
446 else if (strcmp("deep", argv
[0]) == 0)
447 mesh_power_mode
= NL80211_MESH_POWER_DEEP_SLEEP
;
449 fprintf(stderr
, "unknown mesh power mode\n");
458 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
459 NLA_PUT_U32(msg
, NL80211_ATTR_LOCAL_MESH_POWER_MODE
, mesh_power_mode
);
465 COMMAND_ALIAS(station
, set
, "<MAC address> mesh_power_mode "
466 "<active|light|deep>", NL80211_CMD_SET_STATION
, 0, CIB_NETDEV
,
467 handle_station_set_mesh_power_mode
,
468 "Set link-specific mesh power mode for this station",
469 select_station_cmd
, station_set_mesh_power_mode
);
471 static int handle_station_dump(struct nl80211_state
*state
,
474 int argc
, char **argv
,
477 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_sta_handler
, NULL
);
480 COMMAND(station
, dump
, NULL
,
481 NL80211_CMD_GET_STATION
, NLM_F_DUMP
, CIB_NETDEV
, handle_station_dump
,
482 "List all stations known, e.g. the AP on managed interfaces");