7 #include <netlink/genl/genl.h>
8 #include <netlink/genl/family.h>
9 #include <netlink/genl/ctrl.h>
10 #include <netlink/msg.h>
11 #include <netlink/attr.h>
22 static struct link_result lr
= { .link_found
= false };
24 static int link_bss_handler(struct nl_msg
*msg
, void *arg
)
26 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
27 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
28 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
29 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
30 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
31 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
32 [NL80211_BSS_BSSID
] = { },
33 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
34 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
35 [NL80211_BSS_INFORMATION_ELEMENTS
] = { },
36 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
37 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
38 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
40 struct link_result
*result
= arg
;
41 char mac_addr
[20], dev
[20];
43 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
44 genlmsg_attrlen(gnlh
, 0), NULL
);
46 if (!tb
[NL80211_ATTR_BSS
]) {
47 fprintf(stderr
, "bss info missing!");
50 if (nla_parse_nested(bss
, NL80211_BSS_MAX
,
53 fprintf(stderr
, "failed to parse nested attributes!");
57 if (!bss
[NL80211_BSS_BSSID
])
60 if (!bss
[NL80211_BSS_STATUS
])
63 mac_addr_n2a(mac_addr
, nla_data(bss
[NL80211_BSS_BSSID
]));
64 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
66 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
])) {
67 case NL80211_BSS_STATUS_ASSOCIATED
:
68 printf("Connected to %s (on %s)\n", mac_addr
, dev
);
70 case NL80211_BSS_STATUS_AUTHENTICATED
:
71 printf("Authenticated with %s (on %s)\n", mac_addr
, dev
);
77 result
->anything_found
= true;
79 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
80 print_ies(nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
81 nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
84 if (bss
[NL80211_BSS_FREQUENCY
])
85 printf("\tfreq: %d\n",
86 nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]));
88 if (nla_get_u32(bss
[NL80211_BSS_STATUS
]) != NL80211_BSS_STATUS_ASSOCIATED
)
91 /* only in the assoc case do we want more info from station get */
92 result
->link_found
= true;
93 memcpy(result
->bssid
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
97 static int handle_scan_for_link(struct nl80211_state
*state
,
100 int argc
, char **argv
)
105 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, link_bss_handler
, &lr
);
108 HIDDEN(link
, get_bss
, NULL
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
109 CIB_NETDEV
, handle_scan_for_link
);
111 static int print_link_sta(struct nl_msg
*msg
, void *arg
)
113 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
114 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
115 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
116 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
117 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
118 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
119 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
120 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
121 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
122 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
123 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
124 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
125 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
126 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
127 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
130 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
131 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
132 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
133 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
134 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
137 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
138 genlmsg_attrlen(gnlh
, 0), NULL
);
140 if (!tb
[NL80211_ATTR_STA_INFO
]) {
141 fprintf(stderr
, "sta stats missing!");
144 if (nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
145 tb
[NL80211_ATTR_STA_INFO
],
147 fprintf(stderr
, "failed to parse nested attributes!");
151 if (sinfo
[NL80211_STA_INFO_RX_BYTES
] && sinfo
[NL80211_STA_INFO_RX_PACKETS
])
152 printf("\tRX: %u bytes (%u packets)\n",
153 nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]),
154 nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]));
155 if (sinfo
[NL80211_STA_INFO_TX_BYTES
] && sinfo
[NL80211_STA_INFO_TX_PACKETS
])
156 printf("\tTX: %u bytes (%u packets)\n",
157 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]),
158 nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]));
159 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
160 printf("\tsignal: %d dBm\n",
161 (int8_t)nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]));
163 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
]) {
164 if (nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
165 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
)) {
166 fprintf(stderr
, "failed to parse nested rate attributes!");
168 printf("\ttx bitrate: ");
169 if (rinfo
[NL80211_RATE_INFO_BITRATE
]) {
170 int rate
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
171 printf("%d.%d MBit/s", rate
/ 10, rate
% 10);
174 if (rinfo
[NL80211_RATE_INFO_MCS
])
175 printf(" MCS %d", nla_get_u8(rinfo
[NL80211_RATE_INFO_MCS
]));
176 if (rinfo
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
178 if (rinfo
[NL80211_RATE_INFO_SHORT_GI
])
187 static int handle_link_sta(struct nl80211_state
*state
,
190 int argc
, char **argv
)
192 unsigned char mac_addr
[ETH_ALEN
];
197 if (mac_addr_a2n(mac_addr
, argv
[0])) {
198 fprintf(stderr
, "invalid mac address\n");
208 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
210 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_link_sta
, NULL
);
216 HIDDEN(link
, get_sta
, "", NL80211_CMD_GET_STATION
, 0,
217 CIB_NETDEV
, handle_link_sta
);
219 static int handle_link(struct nl80211_state
*state
, struct nl_cb
*cb
,
220 struct nl_msg
*msg
, int argc
, char **argv
)
222 char *link_argv
[] = {
228 char *station_argv
[] = {
238 link_argv
[0] = argv
[0];
239 err
= handle_cmd(state
, II_NETDEV
, 3, link_argv
);
243 if (!lr
.link_found
) {
244 if (!lr
.anything_found
)
245 printf("Not connected.\n");
249 mac_addr_n2a(bssid_buf
, lr
.bssid
);
250 bssid_buf
[17] = '\0';
252 station_argv
[0] = argv
[0];
253 station_argv
[3] = bssid_buf
;
254 return handle_cmd(state
, II_NETDEV
, 4, station_argv
);
256 TOPLEVEL(link
, NULL
, 0, 0, CIB_NETDEV
, handle_link
,
257 "Print information about the current link, if any.");