接受的答案目前是正确的,但是还没有发布示例代码来解决 OP 的问题(即使晚了将近 4 年),所以我想我会在这里为任何未来的搜索引擎用户添加这个。它改编自这个SO question和前面提到的特定 Aircrack-ng 文件(/src/aircrack-osdep/linux.c ,第 1050 行)。
#include <net/if.h>
#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <linux/nl80211.h>
int main(int argc, char *argv[])
{
/* The device's name and the frequency we wish to set it to. */
char *device = "wlan1";
int frequencyMhz = 2442;
/* Create the socket and connect to it. */
struct nl_sock *sckt = nl_socket_alloc();
genl_connect(sckt);
/* Allocate a new message. */
struct nl_msg *mesg = nlmsg_alloc();
/* Check /usr/include/linux/nl80211.h for a list of commands and attributes. */
enum nl80211_commands command = NL80211_CMD_SET_WIPHY;
/* Create the message so it will send a command to the nl80211 interface. */
genlmsg_put(mesg, 0, 0, genl_ctrl_resolve(sckt, "nl80211"), 0, 0, command, 0);
/* Add specific attributes to change the frequency of the device. */
NLA_PUT_U32(mesg, NL80211_ATTR_IFINDEX, if_nametoindex(device));
NLA_PUT_U32(mesg, NL80211_ATTR_WIPHY_FREQ, frequencyMhz);
/* Finally send it and receive the amount of bytes sent. */
int ret = nl_send_auto_complete(sckt, mesg);
printf("%d Bytes Sent\n", ret);
nlmsg_free(mesg);
return EXIT_SUCCESS;
nla_put_failure:
nlmsg_free(mesg);
printf("PUT Failure\n");
return EXIT_FAILURE;
}
用gcc main.c $(pkg-config --cflags --libs libnl-3.0 libnl-genl-3.0)
. iw wlan1 info
执行后,使用例如或检查设备的频率/频道iwconfig
。这里没有严重的错误检查,所以您只会注意到消息是否已发送。希望这可以帮助像我这样的人从无线扩展过渡到 cfg80211 和 nl80211。