1 #include <linux/nl80211.h>
6 #include <netlink/genl/genl.h>
7 #include <netlink/genl/family.h>
8 #include <netlink/genl/ctrl.h>
9 #include <netlink/msg.h>
10 #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 char mac_addr
[20], state_name
[10], dev
[20];
37 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
38 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
39 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
40 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
41 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
42 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
43 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
46 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
47 genlmsg_attrlen(gnlh
, 0), NULL
);
50 * TODO: validate the interface and mac address!
51 * Otherwise, there's a race condition as soon as
52 * the kernel starts sending station notifications.
55 if (!tb
[NL80211_ATTR_STA_INFO
]) {
56 fprintf(stderr
, "sta stats missing!");
59 if (nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
60 tb
[NL80211_ATTR_STA_INFO
],
62 fprintf(stderr
, "failed to parse nested attributes!");
66 mac_addr_n2a(mac_addr
, nla_data(tb
[NL80211_ATTR_MAC
]));
67 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
68 printf("Station %s (on %s)", mac_addr
, dev
);
70 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
71 printf("\n\tinactive time:\t%d ms",
72 nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]));
73 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
74 printf("\n\trx bytes:\t%d",
75 nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]));
76 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
77 printf("\n\ttx bytes:\t%d",
78 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]));
79 if (sinfo
[NL80211_STA_INFO_LLID
])
80 printf("\n\tmesh llid:\t%d",
81 nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]));
82 if (sinfo
[NL80211_STA_INFO_PLID
])
83 printf("\n\tmesh plid:\t%d",
84 nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]));
85 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
]) {
86 switch (nla_get_u16(sinfo
[NL80211_STA_INFO_PLINK_STATE
])) {
88 strcpy(state_name
, "LISTEN");
91 strcpy(state_name
, "OPN_SNT");
94 strcpy(state_name
, "OPN_RCVD");
97 strcpy(state_name
, "CNF_RCVD");
100 strcpy(state_name
, "ESTAB");
103 strcpy(state_name
, "HOLDING");
106 strcpy(state_name
, "BLOCKED");
109 strcpy(state_name
, "UNKNOWN");
112 printf("\n\tmesh plink:\t%s", state_name
);
119 static int handle_station_get(struct nl_cb
*cb
,
121 int argc
, char **argv
)
123 unsigned char mac_addr
[ETH_ALEN
];
128 if (mac_addr_a2n(mac_addr
, argv
[0])) {
129 fprintf(stderr
, "invalid mac address\n");
139 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
141 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_sta_handler
, NULL
);
147 COMMAND(station
, get
, "<MAC address>",
148 NL80211_CMD_GET_STATION
, 0, CIB_NETDEV
, handle_station_get
);
149 COMMAND(station
, del
, "<MAC address>",
150 NL80211_CMD_DEL_STATION
, 0, CIB_NETDEV
, handle_station_get
);
152 static int handle_station_set(struct nl_cb
*cb
,
154 int argc
, char **argv
)
156 unsigned char plink_action
;
157 unsigned char mac_addr
[ETH_ALEN
];
162 if (mac_addr_a2n(mac_addr
, argv
[0])) {
163 fprintf(stderr
, "invalid mac address\n");
169 if (strcmp("plink_action", argv
[0]) != 0)
174 if (strcmp("open", argv
[0]) == 0)
175 plink_action
= PLINK_ACTION_OPEN
;
176 else if (strcmp("block", argv
[0]) == 0)
177 plink_action
= PLINK_ACTION_BLOCK
;
179 fprintf(stderr
, "plink action not supported\n");
188 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
189 NLA_PUT_U8(msg
, NL80211_ATTR_STA_PLINK_ACTION
, plink_action
);
195 COMMAND(station
, set
, "<MAC address> plink_action <open|block>",
196 NL80211_CMD_SET_STATION
, 0, CIB_NETDEV
, handle_station_set
);
198 static int handle_station_dump(struct nl_cb
*cb
,
200 int argc
, char **argv
)
202 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_sta_handler
, NULL
);
205 COMMAND(station
, dump
, NULL
,
206 NL80211_CMD_GET_STATION
, NLM_F_DUMP
, CIB_NETDEV
, handle_station_dump
);