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>
21 static struct link_result lr
= { .link_found
= false };
23 static int link_bss_handler(struct nl_msg
*msg
, void *arg
)
25 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
26 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
27 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
28 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
29 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
30 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
31 [NL80211_BSS_BSSID
] = { },
32 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
33 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
34 [NL80211_BSS_INFORMATION_ELEMENTS
] = { },
35 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
36 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
37 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
39 struct link_result
*result
= arg
;
40 char mac_addr
[20], dev
[20];
42 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
43 genlmsg_attrlen(gnlh
, 0), NULL
);
45 if (!tb
[NL80211_ATTR_BSS
]) {
46 fprintf(stderr
, "bss info missing!\n");
49 if (nla_parse_nested(bss
, NL80211_BSS_MAX
,
52 fprintf(stderr
, "failed to parse nested attributes!\n");
56 if (!bss
[NL80211_BSS_BSSID
])
59 if (!bss
[NL80211_BSS_STATUS
])
62 mac_addr_n2a(mac_addr
, nla_data(bss
[NL80211_BSS_BSSID
]));
63 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
65 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
])) {
66 case NL80211_BSS_STATUS_ASSOCIATED
:
67 printf("Connected to %s (on %s)\n", mac_addr
, dev
);
69 case NL80211_BSS_STATUS_AUTHENTICATED
:
70 printf("Authenticated with %s (on %s)\n", mac_addr
, dev
);
72 case NL80211_BSS_STATUS_IBSS_JOINED
:
73 printf("Joined IBSS %s (on %s)\n", mac_addr
, dev
);
79 result
->anything_found
= true;
81 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
82 print_ies(nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
83 nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]),
86 if (bss
[NL80211_BSS_FREQUENCY
])
87 printf("\tfreq: %d\n",
88 nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]));
90 if (nla_get_u32(bss
[NL80211_BSS_STATUS
]) != NL80211_BSS_STATUS_ASSOCIATED
)
93 /* only in the assoc case do we want more info from station get */
94 result
->link_found
= true;
95 memcpy(result
->bssid
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
99 static int handle_scan_for_link(struct nl80211_state
*state
,
101 int argc
, char **argv
,
107 register_handler(link_bss_handler
, &lr
);
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
*binfo
[NL80211_STA_BSS_PARAM_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
},
129 static struct nla_policy bss_policy
[NL80211_STA_BSS_PARAM_MAX
+ 1] = {
130 [NL80211_STA_BSS_PARAM_CTS_PROT
] = { .type
= NLA_FLAG
},
131 [NL80211_STA_BSS_PARAM_SHORT_PREAMBLE
] = { .type
= NLA_FLAG
},
132 [NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME
] = { .type
= NLA_FLAG
},
133 [NL80211_STA_BSS_PARAM_DTIM_PERIOD
] = { .type
= NLA_U8
},
134 [NL80211_STA_BSS_PARAM_BEACON_INTERVAL
] = { .type
= NLA_U16
},
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!\n");
144 if (nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
145 tb
[NL80211_ATTR_STA_INFO
],
147 fprintf(stderr
, "failed to parse nested attributes!\n");
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
]) {
166 parse_bitrate(sinfo
[NL80211_STA_INFO_TX_BITRATE
], buf
, sizeof(buf
));
167 printf("\ttx bitrate: %s\n", buf
);
170 if (sinfo
[NL80211_STA_INFO_BSS_PARAM
]) {
171 if (nla_parse_nested(binfo
, NL80211_STA_BSS_PARAM_MAX
,
172 sinfo
[NL80211_STA_INFO_BSS_PARAM
],
174 fprintf(stderr
, "failed to parse nested bss parameters!\n");
177 printf("\n\tbss flags:\t");
178 if (binfo
[NL80211_STA_BSS_PARAM_CTS_PROT
]) {
179 printf("CTS-protection");
182 if (binfo
[NL80211_STA_BSS_PARAM_SHORT_PREAMBLE
]) {
183 printf("%sshort-preamble", delim
);
186 if (binfo
[NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME
])
187 printf("%sshort-slot-time", delim
);
188 printf("\n\tdtim period:\t%d",
189 nla_get_u8(binfo
[NL80211_STA_BSS_PARAM_DTIM_PERIOD
]));
190 printf("\n\tbeacon int:\t%d",
191 nla_get_u16(binfo
[NL80211_STA_BSS_PARAM_BEACON_INTERVAL
]));
199 static int handle_link_sta(struct nl80211_state
*state
,
201 int argc
, char **argv
,
204 unsigned char mac_addr
[ETH_ALEN
];
209 if (mac_addr_a2n(mac_addr
, argv
[0])) {
210 fprintf(stderr
, "invalid mac address\n");
220 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, mac_addr
);
222 register_handler(print_link_sta
, NULL
);
229 static int handle_link(struct nl80211_state
*state
,
230 struct nl_msg
*msg
, int argc
, char **argv
,
233 char *link_argv
[] = {
239 char *station_argv
[] = {
249 link_argv
[0] = argv
[0];
250 err
= handle_cmd(state
, id
, 3, link_argv
);
254 if (!lr
.link_found
) {
255 if (!lr
.anything_found
)
256 printf("Not connected.\n");
260 mac_addr_n2a(bssid_buf
, lr
.bssid
);
261 bssid_buf
[17] = '\0';
263 station_argv
[0] = argv
[0];
264 station_argv
[3] = bssid_buf
;
265 return handle_cmd(state
, id
, 4, station_argv
);
267 TOPLEVEL(link
, NULL
, 0, 0, CIB_NETDEV
, handle_link
,
268 "Print information about the current link, if any.");
269 HIDDEN(link
, get_sta
, "", NL80211_CMD_GET_STATION
, 0,
270 CIB_NETDEV
, handle_link_sta
);
271 HIDDEN(link
, get_bss
, NULL
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
272 CIB_NETDEV
, handle_scan_for_link
);