]>
git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
c6e47eb3e4509637c6fb5d082232aa52dd54ee4e
2 * nl80211 userspace tool
4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
9 #include <netlink/genl/genl.h>
10 #include <netlink/genl/family.h>
11 #include <netlink/genl/ctrl.h>
12 #include <netlink/msg.h>
13 #include <netlink/attr.h>
14 #include <linux/nl80211.h>
19 static int nl80211_init(struct nl80211_state
*state
)
23 state
->nl_handle
= nl_handle_alloc();
24 if (!state
->nl_handle
) {
25 fprintf(stderr
, "Failed to allocate netlink handle.\n");
29 if (genl_connect(state
->nl_handle
)) {
30 fprintf(stderr
, "Failed to connect to generic netlink.\n");
32 goto out_handle_destroy
;
35 state
->nl_cache
= genl_ctrl_alloc_cache(state
->nl_handle
);
36 if (!state
->nl_cache
) {
37 fprintf(stderr
, "Failed to allocate generic netlink cache.\n");
39 goto out_handle_destroy
;
42 state
->nl80211
= genl_ctrl_search_by_name(state
->nl_cache
, "nl80211");
43 if (!state
->nl80211
) {
44 fprintf(stderr
, "nl80211 not found.\n");
52 nl_cache_free(state
->nl_cache
);
54 nl_handle_destroy(state
->nl_handle
);
58 static void nl80211_cleanup(struct nl80211_state
*state
)
60 genl_family_put(state
->nl80211
);
61 nl_cache_free(state
->nl_cache
);
62 nl_handle_destroy(state
->nl_handle
);
71 static int get_phy_or_dev(int *argc
, char ***argv
, char **name
)
73 char *type
= (*argv
)[0];
83 if (strcmp(type
, "phy") == 0)
85 if (strcmp(type
, "dev") == 0)
91 void usage(char *argv0
)
93 fprintf(stderr
, "Usage: %1$s dev <phydev> interface <COMMAND> [OPTIONS]\n"
94 " %1$s dev <phydev> info\n"
96 "where COMMAND := { add | del }\n"
98 "For add, OPTIONS := <name> type <type>\n"
99 "For del, OPTIONS should be blank and phydev is the interface to delete.\n", argv0
);
102 int main(int argc
, char **argv
)
104 struct nl80211_state nlstate
;
106 char *ifname
= NULL
, *phyname
= NULL
, *type
, *argv0
;
108 err
= nl80211_init(&nlstate
);
116 if (argc
== 0 || (argc
== 1 && strcmp(*argv
, "help") == 0)) {
121 pod
= get_phy_or_dev(&argc
, &argv
, &ifname
);
141 if (strcmp(type
, "interface") == 0)
142 err
= handle_interface(&nlstate
, phyname
, ifname
, argc
, argv
);
143 if (strcmp(type
, "info") == 0)
144 err
= handle_info(&nlstate
, phyname
, ifname
);
146 fprintf(stderr
, "No such object type %s\n", type
);
151 nl80211_cleanup(&nlstate
);