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 void parse_bitrate(struct nlattr
*bitrate_attr
, char *buf
, int buflen
)
50 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
51 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
52 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
53 [NL80211_RATE_INFO_BITRATE32
] = { .type
= NLA_U32
},
54 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
55 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
56 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
59 if (nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
60 bitrate_attr
, rate_policy
)) {
61 snprintf(buf
, buflen
, "failed to parse nested rate attributes!");
65 if (rinfo
[NL80211_RATE_INFO_BITRATE32
])
66 rate
= nla_get_u32(rinfo
[NL80211_RATE_INFO_BITRATE32
]);
67 else if (rinfo
[NL80211_RATE_INFO_BITRATE
])
68 rate
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
70 pos
+= snprintf(pos
, buflen
- (pos
- buf
),
71 "%d.%d MBit/s", rate
/ 10, rate
% 10);
73 if (rinfo
[NL80211_RATE_INFO_MCS
])
74 pos
+= snprintf(pos
, buflen
- (pos
- buf
),
75 " MCS %d", nla_get_u8(rinfo
[NL80211_RATE_INFO_MCS
]));
76 if (rinfo
[NL80211_RATE_INFO_VHT_MCS
])
77 pos
+= snprintf(pos
, buflen
- (pos
- buf
),
78 " VHT-MCS %d", nla_get_u8(rinfo
[NL80211_RATE_INFO_VHT_MCS
]));
79 if (rinfo
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
80 pos
+= snprintf(pos
, buflen
- (pos
- buf
), " 40MHz");
81 if (rinfo
[NL80211_RATE_INFO_80_MHZ_WIDTH
])
82 pos
+= snprintf(pos
, buflen
- (pos
- buf
), " 80MHz");
83 if (rinfo
[NL80211_RATE_INFO_80P80_MHZ_WIDTH
])
84 pos
+= snprintf(pos
, buflen
- (pos
- buf
), " 80P80MHz");
85 if (rinfo
[NL80211_RATE_INFO_160_MHZ_WIDTH
])
86 pos
+= snprintf(pos
, buflen
- (pos
- buf
), " 160MHz");
87 if (rinfo
[NL80211_RATE_INFO_SHORT_GI
])
88 pos
+= snprintf(pos
, buflen
- (pos
- buf
), " short GI");
89 if (rinfo
[NL80211_RATE_INFO_VHT_NSS
])
90 pos
+= snprintf(pos
, buflen
- (pos
- buf
),
91 " VHT-NSS %d", nla_get_u8(rinfo
[NL80211_RATE_INFO_VHT_NSS
]));
94 static char *get_chain_signal(struct nlattr
*attr_list
)
105 nla_for_each_nested(attr
, attr_list
, rem
) {
111 cur
+= snprintf(cur
, sizeof(buf
) - (cur
- buf
), "%s%d", prefix
,
112 (int8_t) nla_get_u8(attr
));
116 snprintf(cur
, sizeof(buf
) - (cur
- buf
), "] ");
121 static int print_sta_handler(struct nl_msg
*msg
, void *arg
)
123 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
124 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
125 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
126 char mac_addr
[20], state_name
[10], dev
[20];
127 struct nl80211_sta_flag_update
*sta_flags
;
128 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
129 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
130 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
131 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
132 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
133 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
134 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
135 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
136 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
137 [NL80211_STA_INFO_RX_BITRATE
] = { .type
= NLA_NESTED
},
138 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
139 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
140 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
141 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
142 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
143 [NL80211_STA_INFO_STA_FLAGS
] =
144 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
145 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
146 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
147 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
148 [NL80211_STA_INFO_CHAIN_SIGNAL
] = { .type
= NLA_NESTED
},
149 [NL80211_STA_INFO_CHAIN_SIGNAL_AVG
] = { .type
= NLA_NESTED
},
153 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
154 genlmsg_attrlen(gnlh
, 0), NULL
);
157 * TODO: validate the interface and mac address!
158 * Otherwise, there's a race condition as soon as
159 * the kernel starts sending station notifications.
162 if (!tb
[NL80211_ATTR_STA_INFO
]) {
163 fprintf(stderr
, "sta stats missing!\n");
166 if (nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
167 tb
[NL80211_ATTR_STA_INFO
],
169 fprintf(stderr
, "failed to parse nested attributes!\n");
173 mac_addr_n2a(mac_addr
, nla_data(tb
[NL80211_ATTR_MAC
]));
174 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
175 printf("Station %s (on %s)", mac_addr
, dev
);
177 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
178 printf("\n\tinactive time:\t%u ms",
179 nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]));
180 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
181 printf("\n\trx bytes:\t%u",
182 nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]));
183 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
184 printf("\n\trx packets:\t%u",
185 nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]));
186 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
187 printf("\n\ttx bytes:\t%u",
188 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]));
189 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
190 printf("\n\ttx packets:\t%u",
191 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]));
192 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
193 printf("\n\ttx retries:\t%u",
194 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]));
195 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
196 printf("\n\ttx failed:\t%u",
197 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]));
199 chain
= get_chain_signal(sinfo
[NL80211_STA_INFO_CHAIN_SIGNAL
]);
200 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
201 printf("\n\tsignal: \t%d %sdBm",
202 (int8_t)nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]),
205 chain
= get_chain_signal(sinfo
[NL80211_STA_INFO_CHAIN_SIGNAL_AVG
]);
206 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
207 printf("\n\tsignal avg:\t%d %sdBm",
208 (int8_t)nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]),
211 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
212 printf("\n\tToffset:\t%lld us",
213 (unsigned long long)nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]));
215 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
]) {
218 parse_bitrate(sinfo
[NL80211_STA_INFO_TX_BITRATE
], buf
, sizeof(buf
));
219 printf("\n\ttx bitrate:\t%s", buf
);
222 if (sinfo
[NL80211_STA_INFO_RX_BITRATE
]) {
225 parse_bitrate(sinfo
[NL80211_STA_INFO_RX_BITRATE
], buf
, sizeof(buf
));
226 printf("\n\trx bitrate:\t%s", buf
);
229 if (sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
]) {
232 thr
= nla_get_u32(sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
]);
233 /* convert in Mbps but scale by 1000 to save kbps units */
234 thr
= thr
* 1000 / 1024;
236 printf("\n\texpected throughput:\t%u.%uMbps",
237 thr
/ 1000, thr
% 1000);
240 if (sinfo
[NL80211_STA_INFO_LLID
])
241 printf("\n\tmesh llid:\t%d",
242 nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]));
243 if (sinfo
[NL80211_STA_INFO_PLID
])
244 printf("\n\tmesh plid:\t%d",
245 nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]));
246 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
]) {
247 switch (nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
])) {
249 strcpy(state_name
, "LISTEN");
252 strcpy(state_name
, "OPN_SNT");
255 strcpy(state_name
, "OPN_RCVD");
258 strcpy(state_name
, "CNF_RCVD");
261 strcpy(state_name
, "ESTAB");
264 strcpy(state_name
, "HOLDING");
267 strcpy(state_name
, "BLOCKED");
270 strcpy(state_name
, "UNKNOWN");
273 printf("\n\tmesh plink:\t%s", state_name
);
275 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
]) {
276 printf("\n\tmesh local PS mode:\t");
277 print_power_mode(sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
279 if (sinfo
[NL80211_STA_INFO_PEER_PM
]) {
280 printf("\n\tmesh peer PS mode:\t");
281 print_power_mode(sinfo
[NL80211_STA_INFO_PEER_PM
]);
283 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
]) {
284 printf("\n\tmesh non-peer PS mode:\t");
285 print_power_mode(sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
288 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
]) {
289 sta_flags
= (struct nl80211_sta_flag_update
*)
290 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
292 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
)) {
293 printf("\n\tauthorized:\t");
294 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
300 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
)) {
301 printf("\n\tauthenticated:\t");
302 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
308 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
)) {
309 printf("\n\tpreamble:\t");
310 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
316 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
)) {
317 printf("\n\tWMM/WME:\t");
318 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
324 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
)) {
325 printf("\n\tMFP:\t\t");
326 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
332 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
)) {
333 printf("\n\tTDLS peer:\t");
334 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
341 if (sinfo
[NL80211_STA_INFO_CONNECTED_TIME
])
342 printf("\n\tconnected time:\t%u seconds",
343 nla_get_u32(sinfo
[NL80211_STA_INFO_CONNECTED_TIME
]));
349 static int handle_station_get(struct nl80211_state
*state
,
351 int argc
, char **argv
,
354 unsigned char mac_addr
[ETH_ALEN
];
359 if (mac_addr_a2n(mac_addr
, argv
[0])) {
360 fprintf(stderr
, "invalid mac address\n");
370 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
372 register_handler(print_sta_handler
, NULL
);
378 COMMAND(station
, get
, "<MAC address>",
379 NL80211_CMD_GET_STATION
, 0, CIB_NETDEV
, handle_station_get
,
380 "Get information for a specific station.");
381 COMMAND(station
, del
, "<MAC address>",
382 NL80211_CMD_DEL_STATION
, 0, CIB_NETDEV
, handle_station_get
,
383 "Remove the given station entry (use with caution!)");
385 static const struct cmd
*station_set_plink
;
386 static const struct cmd
*station_set_vlan
;
387 static const struct cmd
*station_set_mesh_power_mode
;
389 static const struct cmd
*select_station_cmd(int argc
, char **argv
)
393 if (strcmp(argv
[1], "plink_action") == 0)
394 return station_set_plink
;
395 if (strcmp(argv
[1], "vlan") == 0)
396 return station_set_vlan
;
397 if (strcmp(argv
[1], "mesh_power_mode") == 0)
398 return station_set_mesh_power_mode
;
402 static int handle_station_set_plink(struct nl80211_state
*state
,
404 int argc
, char **argv
,
407 unsigned char plink_action
;
408 unsigned char mac_addr
[ETH_ALEN
];
413 if (mac_addr_a2n(mac_addr
, argv
[0])) {
414 fprintf(stderr
, "invalid mac address\n");
420 if (strcmp("plink_action", argv
[0]) != 0)
425 if (strcmp("open", argv
[0]) == 0)
426 plink_action
= NL80211_PLINK_ACTION_OPEN
;
427 else if (strcmp("block", argv
[0]) == 0)
428 plink_action
= NL80211_PLINK_ACTION_BLOCK
;
430 fprintf(stderr
, "plink action not supported\n");
439 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
440 NLA_PUT_U8(msg
, NL80211_ATTR_STA_PLINK_ACTION
, plink_action
);
446 COMMAND_ALIAS(station
, set
, "<MAC address> plink_action <open|block>",
447 NL80211_CMD_SET_STATION
, 0, CIB_NETDEV
, handle_station_set_plink
,
448 "Set mesh peer link action for this station (peer).",
449 select_station_cmd
, station_set_plink
);
451 static int handle_station_set_vlan(struct nl80211_state
*state
,
453 int argc
, char **argv
,
456 unsigned char mac_addr
[ETH_ALEN
];
457 unsigned long sta_vlan
= 0;
463 if (mac_addr_a2n(mac_addr
, argv
[0])) {
464 fprintf(stderr
, "invalid mac address\n");
470 if (strcmp("vlan", argv
[0]) != 0)
475 sta_vlan
= strtoul(argv
[0], &err
, 0);
477 fprintf(stderr
, "invalid vlan id\n");
486 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
487 NLA_PUT_U32(msg
, NL80211_ATTR_STA_VLAN
, sta_vlan
);
493 COMMAND_ALIAS(station
, set
, "<MAC address> vlan <ifindex>",
494 NL80211_CMD_SET_STATION
, 0, CIB_NETDEV
, handle_station_set_vlan
,
495 "Set an AP VLAN for this station.",
496 select_station_cmd
, station_set_vlan
);
498 static int handle_station_set_mesh_power_mode(struct nl80211_state
*state
,
500 int argc
, char **argv
,
503 unsigned char mesh_power_mode
;
504 unsigned char mac_addr
[ETH_ALEN
];
509 if (mac_addr_a2n(mac_addr
, argv
[0])) {
510 fprintf(stderr
, "invalid mac address\n");
516 if (strcmp("mesh_power_mode", argv
[0]) != 0)
521 if (strcmp("active", argv
[0]) == 0)
522 mesh_power_mode
= NL80211_MESH_POWER_ACTIVE
;
523 else if (strcmp("light", argv
[0]) == 0)
524 mesh_power_mode
= NL80211_MESH_POWER_LIGHT_SLEEP
;
525 else if (strcmp("deep", argv
[0]) == 0)
526 mesh_power_mode
= NL80211_MESH_POWER_DEEP_SLEEP
;
528 fprintf(stderr
, "unknown mesh power mode\n");
537 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
538 NLA_PUT_U32(msg
, NL80211_ATTR_LOCAL_MESH_POWER_MODE
, mesh_power_mode
);
544 COMMAND_ALIAS(station
, set
, "<MAC address> mesh_power_mode "
545 "<active|light|deep>", NL80211_CMD_SET_STATION
, 0, CIB_NETDEV
,
546 handle_station_set_mesh_power_mode
,
547 "Set link-specific mesh power mode for this station",
548 select_station_cmd
, station_set_mesh_power_mode
);
550 static int handle_station_dump(struct nl80211_state
*state
,
552 int argc
, char **argv
,
555 register_handler(print_sta_handler
, NULL
);
558 COMMAND(station
, dump
, NULL
,
559 NL80211_CMD_GET_STATION
, NLM_F_DUMP
, CIB_NETDEV
, handle_station_dump
,
560 "List all stations known, e.g. the AP on managed interfaces");