]>
git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
2 * nl80211 userspace tool
4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
11 #include <netlink/genl/genl.h>
12 #include <netlink/genl/family.h>
13 #include <netlink/genl/ctrl.h>
14 #include <netlink/msg.h>
15 #include <netlink/attr.h>
16 #include <linux/nl80211.h>
21 static int nl80211_init(struct nl80211_state
*state
)
25 state
->nl_handle
= nl_handle_alloc();
26 if (!state
->nl_handle
) {
27 fprintf(stderr
, "Failed to allocate netlink handle.\n");
31 if (genl_connect(state
->nl_handle
)) {
32 fprintf(stderr
, "Failed to connect to generic netlink.\n");
34 goto out_handle_destroy
;
37 state
->nl_cache
= genl_ctrl_alloc_cache(state
->nl_handle
);
38 if (!state
->nl_cache
) {
39 fprintf(stderr
, "Failed to allocate generic netlink cache.\n");
41 goto out_handle_destroy
;
44 state
->nl80211
= genl_ctrl_search_by_name(state
->nl_cache
, "nl80211");
45 if (!state
->nl80211
) {
46 fprintf(stderr
, "nl80211 not found.\n");
54 nl_cache_free(state
->nl_cache
);
56 nl_handle_destroy(state
->nl_handle
);
60 static void nl80211_cleanup(struct nl80211_state
*state
)
62 genl_family_put(state
->nl80211
);
63 nl_cache_free(state
->nl_cache
);
64 nl_handle_destroy(state
->nl_handle
);
73 static int get_phy_or_dev(int *argc
, char ***argv
, char **name
)
75 char *type
= (*argv
)[0];
85 if (strcmp(type
, "phy") == 0)
87 if (strcmp(type
, "dev") == 0)
93 static void usage(char *argv0
)
95 fprintf(stderr
, "Usage: %1$s dev <phydev> <OBJECT> <COMMAND> [OPTIONS]"
96 "\n %1$s dev <phydev> info"
97 "\n %1$s reg set <ISO/IEC 3166-1 alpha2>\n"
99 "where OBJECT := { interface | station | mpath | info }\n"
100 "and COMMAND := { add | del | set | get | dump }\n",
104 int main(int argc
, char **argv
)
106 struct nl80211_state nlstate
;
108 char *ifname
= NULL
, *phyname
= NULL
, *type
, *argv0
;
110 err
= nl80211_init(&nlstate
);
118 if (argc
== 0 || (argc
== 1 && strcmp(*argv
, "help") == 0)) {
123 if (strcmp(argv
[0], "reg") == 0) {
126 err
= handle_reg(&nlstate
, argc
, argv
);
130 pod
= get_phy_or_dev(&argc
, &argv
, &ifname
);
150 if (strcmp(type
, "interface") == 0)
151 err
= handle_interface(&nlstate
, phyname
, ifname
, argc
, argv
);
152 else if (strcmp(type
, "info") == 0)
153 err
= handle_info(&nlstate
, phyname
, ifname
);
154 else if (strcmp(type
, "station") == 0)
155 err
= handle_station(&nlstate
, ifname
, argc
, argv
);
156 else if (strcmp(type
, "mpath") == 0)
157 err
= handle_mpath(&nlstate
, ifname
, argc
, argv
);
159 fprintf(stderr
, "No such object type %s\n", type
);
164 nl80211_cleanup(&nlstate
);