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>
27 PLINK_ACTION_UNDEFINED
,
32 static void print_power_mode(struct nlattr
*a
)
34 enum nl80211_mesh_power_mode pm
= nla_get_u32(a
);
37 case NL80211_MESH_POWER_ACTIVE
:
40 case NL80211_MESH_POWER_LIGHT_SLEEP
:
41 printf("LIGHT SLEEP");
43 case NL80211_MESH_POWER_DEEP_SLEEP
:
52 static int print_sta_handler(struct nl_msg
*msg
, void *arg
)
54 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
55 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
56 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
57 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
58 char mac_addr
[20], state_name
[10], dev
[20];
59 struct nl80211_sta_flag_update
*sta_flags
;
60 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
61 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
62 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
63 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
64 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
65 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
66 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
67 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
68 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
69 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
70 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
71 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
72 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
73 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
74 [NL80211_STA_INFO_STA_FLAGS
] =
75 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
76 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
77 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
78 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
81 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
82 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
83 [NL80211_RATE_INFO_BITRATE32
] = { .type
= NLA_U32
},
84 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
85 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
86 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
89 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
90 genlmsg_attrlen(gnlh
, 0), NULL
);
93 * TODO: validate the interface and mac address!
94 * Otherwise, there's a race condition as soon as
95 * the kernel starts sending station notifications.
98 if (!tb
[NL80211_ATTR_STA_INFO
]) {
99 fprintf(stderr
, "sta stats missing!\n");
102 if (nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
103 tb
[NL80211_ATTR_STA_INFO
],
105 fprintf(stderr
, "failed to parse nested attributes!\n");
109 mac_addr_n2a(mac_addr
, nla_data(tb
[NL80211_ATTR_MAC
]));
110 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
111 printf("Station %s (on %s)", mac_addr
, dev
);
113 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
114 printf("\n\tinactive time:\t%u ms",
115 nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]));
116 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
117 printf("\n\trx bytes:\t%u",
118 nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]));
119 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
120 printf("\n\trx packets:\t%u",
121 nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]));
122 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
123 printf("\n\ttx bytes:\t%u",
124 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]));
125 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
126 printf("\n\ttx packets:\t%u",
127 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]));
128 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
129 printf("\n\ttx retries:\t%u",
130 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]));
131 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
132 printf("\n\ttx failed:\t%u",
133 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]));
134 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
135 printf("\n\tsignal: \t%d dBm",
136 (int8_t)nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]));
137 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
138 printf("\n\tsignal avg:\t%d dBm",
139 (int8_t)nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]));
140 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
141 printf("\n\tToffset:\t%lld us",
142 (unsigned long long)nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]));
144 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
]) {
145 if (nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
146 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
)) {
147 fprintf(stderr
, "failed to parse nested rate attributes!\n");
150 printf("\n\ttx bitrate:\t");
151 if (rinfo
[NL80211_RATE_INFO_BITRATE32
])
152 rate
= nla_get_u32(rinfo
[NL80211_RATE_INFO_BITRATE32
]);
153 else if (rinfo
[NL80211_RATE_INFO_BITRATE
])
154 rate
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
156 printf("%d.%d MBit/s", rate
/ 10, rate
% 10);
158 if (rinfo
[NL80211_RATE_INFO_MCS
])
159 printf(" MCS %d", nla_get_u8(rinfo
[NL80211_RATE_INFO_MCS
]));
160 if (rinfo
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
162 if (rinfo
[NL80211_RATE_INFO_SHORT_GI
])
167 if (sinfo
[NL80211_STA_INFO_LLID
])
168 printf("\n\tmesh llid:\t%d",
169 nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]));
170 if (sinfo
[NL80211_STA_INFO_PLID
])
171 printf("\n\tmesh plid:\t%d",
172 nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]));
173 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
]) {
174 switch (nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
])) {
176 strcpy(state_name
, "LISTEN");
179 strcpy(state_name
, "OPN_SNT");
182 strcpy(state_name
, "OPN_RCVD");
185 strcpy(state_name
, "CNF_RCVD");
188 strcpy(state_name
, "ESTAB");
191 strcpy(state_name
, "HOLDING");
194 strcpy(state_name
, "BLOCKED");
197 strcpy(state_name
, "UNKNOWN");
200 printf("\n\tmesh plink:\t%s", state_name
);
202 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
]) {
203 printf("\n\tmesh local PS mode:\t");
204 print_power_mode(sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
206 if (sinfo
[NL80211_STA_INFO_PEER_PM
]) {
207 printf("\n\tmesh peer PS mode:\t");
208 print_power_mode(sinfo
[NL80211_STA_INFO_PEER_PM
]);
210 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
]) {
211 printf("\n\tmesh non-peer PS mode:\t");
212 print_power_mode(sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
215 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
]) {
216 sta_flags
= (struct nl80211_sta_flag_update
*)
217 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
219 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
)) {
220 printf("\n\tauthorized:\t");
221 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
227 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
)) {
228 printf("\n\tauthenticated:\t");
229 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
235 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
)) {
236 printf("\n\tpreamble:\t");
237 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
243 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
)) {
244 printf("\n\tWMM/WME:\t");
245 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
251 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
)) {
252 printf("\n\tMFP:\t\t");
253 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
259 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
)) {
260 printf("\n\tTDLS peer:\t\t");
261 if (sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
272 static int handle_station_get(struct nl80211_state
*state
,
275 int argc
, char **argv
,
278 unsigned char mac_addr
[ETH_ALEN
];
283 if (mac_addr_a2n(mac_addr
, argv
[0])) {
284 fprintf(stderr
, "invalid mac address\n");
294 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
296 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_sta_handler
, NULL
);
302 COMMAND(station
, get
, "<MAC address>",
303 NL80211_CMD_GET_STATION
, 0, CIB_NETDEV
, handle_station_get
,
304 "Get information for a specific station.");
305 COMMAND(station
, del
, "<MAC address>",
306 NL80211_CMD_DEL_STATION
, 0, CIB_NETDEV
, handle_station_get
,
307 "Remove the given station entry (use with caution!)");
309 static const struct cmd
*station_set_plink
;
310 static const struct cmd
*station_set_vlan
;
311 static const struct cmd
*station_set_mesh_power_mode
;
313 static const struct cmd
*select_station_cmd(int argc
, char **argv
)
317 if (strcmp(argv
[1], "plink_action") == 0)
318 return station_set_plink
;
319 if (strcmp(argv
[1], "vlan") == 0)
320 return station_set_vlan
;
321 if (strcmp(argv
[1], "mesh_power_mode") == 0)
322 return station_set_mesh_power_mode
;
326 static int handle_station_set_plink(struct nl80211_state
*state
,
329 int argc
, char **argv
,
332 unsigned char plink_action
;
333 unsigned char mac_addr
[ETH_ALEN
];
338 if (mac_addr_a2n(mac_addr
, argv
[0])) {
339 fprintf(stderr
, "invalid mac address\n");
345 if (strcmp("plink_action", argv
[0]) != 0)
350 if (strcmp("open", argv
[0]) == 0)
351 plink_action
= PLINK_ACTION_OPEN
;
352 else if (strcmp("block", argv
[0]) == 0)
353 plink_action
= PLINK_ACTION_BLOCK
;
355 fprintf(stderr
, "plink action not supported\n");
364 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
365 NLA_PUT_U8(msg
, NL80211_ATTR_STA_PLINK_ACTION
, plink_action
);
371 COMMAND_ALIAS(station
, set
, "<MAC address> plink_action <open|block>",
372 NL80211_CMD_SET_STATION
, 0, CIB_NETDEV
, handle_station_set_plink
,
373 "Set mesh peer link action for this station (peer).",
374 select_station_cmd
, station_set_plink
);
376 static int handle_station_set_vlan(struct nl80211_state
*state
,
379 int argc
, char **argv
,
382 unsigned char mac_addr
[ETH_ALEN
];
383 unsigned long sta_vlan
= 0;
389 if (mac_addr_a2n(mac_addr
, argv
[0])) {
390 fprintf(stderr
, "invalid mac address\n");
396 if (strcmp("vlan", argv
[0]) != 0)
401 sta_vlan
= strtoul(argv
[0], &err
, 0);
403 fprintf(stderr
, "invalid vlan id\n");
412 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
413 NLA_PUT_U32(msg
, NL80211_ATTR_STA_VLAN
, sta_vlan
);
419 COMMAND_ALIAS(station
, set
, "<MAC address> vlan <ifindex>",
420 NL80211_CMD_SET_STATION
, 0, CIB_NETDEV
, handle_station_set_vlan
,
421 "Set an AP VLAN for this station.",
422 select_station_cmd
, station_set_vlan
);
424 static int handle_station_set_mesh_power_mode(struct nl80211_state
*state
,
427 int argc
, char **argv
,
430 unsigned char mesh_power_mode
;
431 unsigned char mac_addr
[ETH_ALEN
];
436 if (mac_addr_a2n(mac_addr
, argv
[0])) {
437 fprintf(stderr
, "invalid mac address\n");
443 if (strcmp("mesh_power_mode", argv
[0]) != 0)
448 if (strcmp("active", argv
[0]) == 0)
449 mesh_power_mode
= NL80211_MESH_POWER_ACTIVE
;
450 else if (strcmp("light", argv
[0]) == 0)
451 mesh_power_mode
= NL80211_MESH_POWER_LIGHT_SLEEP
;
452 else if (strcmp("deep", argv
[0]) == 0)
453 mesh_power_mode
= NL80211_MESH_POWER_DEEP_SLEEP
;
455 fprintf(stderr
, "unknown mesh power mode\n");
464 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
465 NLA_PUT_U32(msg
, NL80211_ATTR_LOCAL_MESH_POWER_MODE
, mesh_power_mode
);
471 COMMAND_ALIAS(station
, set
, "<MAC address> mesh_power_mode "
472 "<active|light|deep>", NL80211_CMD_SET_STATION
, 0, CIB_NETDEV
,
473 handle_station_set_mesh_power_mode
,
474 "Set link-specific mesh power mode for this station",
475 select_station_cmd
, station_set_mesh_power_mode
);
477 static int handle_station_dump(struct nl80211_state
*state
,
480 int argc
, char **argv
,
483 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_sta_handler
, NULL
);
486 COMMAND(station
, dump
, NULL
,
487 NL80211_CMD_GET_STATION
, NLM_F_DUMP
, CIB_NETDEV
, handle_station_dump
,
488 "List all stations known, e.g. the AP on managed interfaces");