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>
25 PLINK_ACTION_UNDEFINED
,
31 static int print_sta_handler(struct nl_msg
*msg
, void *arg
)
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 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
37 char mac_addr
[20], state_name
[10], dev
[20];
38 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
39 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
40 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
41 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
42 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
43 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
44 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
45 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
46 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
47 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
48 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
51 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
52 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
53 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
54 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
55 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
58 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
59 genlmsg_attrlen(gnlh
, 0), NULL
);
62 * TODO: validate the interface and mac address!
63 * Otherwise, there's a race condition as soon as
64 * the kernel starts sending station notifications.
67 if (!tb
[NL80211_ATTR_STA_INFO
]) {
68 fprintf(stderr
, "sta stats missing!");
71 if (nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
72 tb
[NL80211_ATTR_STA_INFO
],
74 fprintf(stderr
, "failed to parse nested attributes!");
78 mac_addr_n2a(mac_addr
, nla_data(tb
[NL80211_ATTR_MAC
]));
79 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
80 printf("Station %s (on %s)", mac_addr
, dev
);
82 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
83 printf("\n\tinactive time:\t%d ms",
84 nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]));
85 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
86 printf("\n\trx bytes:\t%d",
87 nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]));
88 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
89 printf("\n\trx packets:\t%d",
90 nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]));
91 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
92 printf("\n\ttx bytes:\t%d",
93 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]));
94 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
95 printf("\n\ttx packets:\t%d",
96 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]));
97 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
98 printf("\n\tsignal: \t%d dBm",
99 (int8_t)nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]));
101 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
]) {
102 if (nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
103 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
)) {
104 fprintf(stderr
, "failed to parse nested rate attributes!");
106 printf("\n\ttx bitrate:\t");
107 if (rinfo
[NL80211_RATE_INFO_BITRATE
]) {
108 int rate
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
109 printf("%d.%d MBit/s", rate
/ 10, rate
% 10);
112 if (rinfo
[NL80211_RATE_INFO_MCS
])
113 printf(" MCS %d", nla_get_u8(rinfo
[NL80211_RATE_INFO_MCS
]));
114 if (rinfo
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
116 if (rinfo
[NL80211_RATE_INFO_SHORT_GI
])
121 if (sinfo
[NL80211_STA_INFO_LLID
])
122 printf("\n\tmesh llid:\t%d",
123 nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]));
124 if (sinfo
[NL80211_STA_INFO_PLID
])
125 printf("\n\tmesh plid:\t%d",
126 nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]));
127 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
]) {
128 switch (nla_get_u16(sinfo
[NL80211_STA_INFO_PLINK_STATE
])) {
130 strcpy(state_name
, "LISTEN");
133 strcpy(state_name
, "OPN_SNT");
136 strcpy(state_name
, "OPN_RCVD");
139 strcpy(state_name
, "CNF_RCVD");
142 strcpy(state_name
, "ESTAB");
145 strcpy(state_name
, "HOLDING");
148 strcpy(state_name
, "BLOCKED");
151 strcpy(state_name
, "UNKNOWN");
154 printf("\n\tmesh plink:\t%s", state_name
);
161 static int handle_station_get(struct nl80211_state
*state
,
164 int argc
, char **argv
)
166 unsigned char mac_addr
[ETH_ALEN
];
171 if (mac_addr_a2n(mac_addr
, argv
[0])) {
172 fprintf(stderr
, "invalid mac address\n");
182 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
184 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_sta_handler
, NULL
);
190 COMMAND(station
, get
, "<MAC address>",
191 NL80211_CMD_GET_STATION
, 0, CIB_NETDEV
, handle_station_get
);
192 COMMAND(station
, del
, "<MAC address>",
193 NL80211_CMD_DEL_STATION
, 0, CIB_NETDEV
, handle_station_get
);
195 static int handle_station_set(struct nl80211_state
*state
,
198 int argc
, char **argv
)
200 unsigned char plink_action
;
201 unsigned char mac_addr
[ETH_ALEN
];
206 if (mac_addr_a2n(mac_addr
, argv
[0])) {
207 fprintf(stderr
, "invalid mac address\n");
213 if (strcmp("plink_action", argv
[0]) != 0)
218 if (strcmp("open", argv
[0]) == 0)
219 plink_action
= PLINK_ACTION_OPEN
;
220 else if (strcmp("block", argv
[0]) == 0)
221 plink_action
= PLINK_ACTION_BLOCK
;
223 fprintf(stderr
, "plink action not supported\n");
232 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
233 NLA_PUT_U8(msg
, NL80211_ATTR_STA_PLINK_ACTION
, plink_action
);
239 COMMAND(station
, set
, "<MAC address> plink_action <open|block>",
240 NL80211_CMD_SET_STATION
, 0, CIB_NETDEV
, handle_station_set
);
242 static int handle_station_dump(struct nl80211_state
*state
,
245 int argc
, char **argv
)
247 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_sta_handler
, NULL
);
250 COMMAND(station
, dump
, NULL
,
251 NL80211_CMD_GET_STATION
, NLM_F_DUMP
, CIB_NETDEV
, handle_station_dump
);