]>
git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
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 static void usage(char *argv0
)
93 fprintf(stderr
, "Usage: %1$s dev <phydev> <OBJECT> <COMMAND> [OPTIONS]"
94 "\n %1$s dev <phydev> info\n"
96 "where OBJECT := { interface | station | mpath }\n"
97 "and COMMAND := { add | del | set | get | dump }\n",
101 int main(int argc
, char **argv
)
103 struct nl80211_state nlstate
;
105 char *ifname
= NULL
, *phyname
= NULL
, *type
, *argv0
;
107 err
= nl80211_init(&nlstate
);
115 if (argc
== 0 || (argc
== 1 && strcmp(*argv
, "help") == 0)) {
120 pod
= get_phy_or_dev(&argc
, &argv
, &ifname
);
140 if (strcmp(type
, "interface") == 0)
141 err
= handle_interface(&nlstate
, phyname
, ifname
, argc
, argv
);
142 else if (strcmp(type
, "info") == 0)
143 err
= handle_info(&nlstate
, phyname
, ifname
);
144 else if (strcmp(type
, "station") == 0)
145 err
= handle_station(&nlstate
, ifname
, argc
, argv
);
146 else if (strcmp(type
, "mpath") == 0)
147 err
= handle_mpath(&nlstate
, ifname
, argc
, argv
);
149 fprintf(stderr
, "No such object type %s\n", type
);
154 nl80211_cleanup(&nlstate
);