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_SIGNAL
] = { .type
= NLA_U8
},
43 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
44 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
45 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
46 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
49 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
50 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
51 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
52 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
53 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
56 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
57 genlmsg_attrlen(gnlh
, 0), NULL
);
60 * TODO: validate the interface and mac address!
61 * Otherwise, there's a race condition as soon as
62 * the kernel starts sending station notifications.
65 if (!tb
[NL80211_ATTR_STA_INFO
]) {
66 fprintf(stderr
, "sta stats missing!");
69 if (nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
70 tb
[NL80211_ATTR_STA_INFO
],
72 fprintf(stderr
, "failed to parse nested attributes!");
76 mac_addr_n2a(mac_addr
, nla_data(tb
[NL80211_ATTR_MAC
]));
77 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
78 printf("Station %s (on %s)", mac_addr
, dev
);
80 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
81 printf("\n\tinactive time:\t%d ms",
82 nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]));
83 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
84 printf("\n\trx bytes:\t%d",
85 nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]));
86 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
87 printf("\n\ttx bytes:\t%d",
88 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]));
89 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
90 printf("\n\tsignal: \t%d dBm",
91 (int8_t)nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]));
93 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
]) {
94 if (nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
95 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
)) {
96 fprintf(stderr
, "failed to parse nested rate attributes!");
98 printf("\n\ttx bitrate:\t");
99 if (rinfo
[NL80211_RATE_INFO_BITRATE
]) {
100 int rate
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
101 printf("%d.%d MBit/s", rate
/ 10, rate
% 10);
104 if (rinfo
[NL80211_RATE_INFO_MCS
])
105 printf(" MCS %d", nla_get_u8(rinfo
[NL80211_RATE_INFO_MCS
]));
106 if (rinfo
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
108 if (rinfo
[NL80211_RATE_INFO_SHORT_GI
])
113 if (sinfo
[NL80211_STA_INFO_LLID
])
114 printf("\n\tmesh llid:\t%d",
115 nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]));
116 if (sinfo
[NL80211_STA_INFO_PLID
])
117 printf("\n\tmesh plid:\t%d",
118 nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]));
119 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
]) {
120 switch (nla_get_u16(sinfo
[NL80211_STA_INFO_PLINK_STATE
])) {
122 strcpy(state_name
, "LISTEN");
125 strcpy(state_name
, "OPN_SNT");
128 strcpy(state_name
, "OPN_RCVD");
131 strcpy(state_name
, "CNF_RCVD");
134 strcpy(state_name
, "ESTAB");
137 strcpy(state_name
, "HOLDING");
140 strcpy(state_name
, "BLOCKED");
143 strcpy(state_name
, "UNKNOWN");
146 printf("\n\tmesh plink:\t%s", state_name
);
153 static int handle_station_get(struct nl_cb
*cb
,
155 int argc
, char **argv
)
157 unsigned char mac_addr
[ETH_ALEN
];
162 if (mac_addr_a2n(mac_addr
, argv
[0])) {
163 fprintf(stderr
, "invalid mac address\n");
173 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
175 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_sta_handler
, NULL
);
181 COMMAND(station
, get
, "<MAC address>",
182 NL80211_CMD_GET_STATION
, 0, CIB_NETDEV
, handle_station_get
);
183 COMMAND(station
, del
, "<MAC address>",
184 NL80211_CMD_DEL_STATION
, 0, CIB_NETDEV
, handle_station_get
);
186 static int handle_station_set(struct nl_cb
*cb
,
188 int argc
, char **argv
)
190 unsigned char plink_action
;
191 unsigned char mac_addr
[ETH_ALEN
];
196 if (mac_addr_a2n(mac_addr
, argv
[0])) {
197 fprintf(stderr
, "invalid mac address\n");
203 if (strcmp("plink_action", argv
[0]) != 0)
208 if (strcmp("open", argv
[0]) == 0)
209 plink_action
= PLINK_ACTION_OPEN
;
210 else if (strcmp("block", argv
[0]) == 0)
211 plink_action
= PLINK_ACTION_BLOCK
;
213 fprintf(stderr
, "plink action not supported\n");
222 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
223 NLA_PUT_U8(msg
, NL80211_ATTR_STA_PLINK_ACTION
, plink_action
);
229 COMMAND(station
, set
, "<MAC address> plink_action <open|block>",
230 NL80211_CMD_SET_STATION
, 0, CIB_NETDEV
, handle_station_set
);
232 static int handle_station_dump(struct nl_cb
*cb
,
234 int argc
, char **argv
)
236 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_sta_handler
, NULL
);
239 COMMAND(station
, dump
, NULL
,
240 NL80211_CMD_GET_STATION
, NLM_F_DUMP
, CIB_NETDEV
, handle_station_dump
);