]>
Commit | Line | Data |
---|---|---|
cad53b3f JB |
1 | /* |
2 | * nl80211 userspace tool | |
3 | * | |
2a1fced2 | 4 | * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net> |
cad53b3f JB |
5 | */ |
6 | ||
7 | #include <errno.h> | |
8 | #include <stdio.h> | |
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> | |
15 | ||
16 | #include "iw.h" | |
17 | ||
18 | ||
19 | static int nl80211_init(struct nl80211_state *state) | |
20 | { | |
21 | int err; | |
22 | ||
23 | state->nl_handle = nl_handle_alloc(); | |
24 | if (!state->nl_handle) { | |
25 | fprintf(stderr, "Failed to allocate netlink handle.\n"); | |
26 | return -ENOMEM; | |
27 | } | |
28 | ||
29 | if (genl_connect(state->nl_handle)) { | |
30 | fprintf(stderr, "Failed to connect to generic netlink.\n"); | |
31 | err = -ENOLINK; | |
32 | goto out_handle_destroy; | |
33 | } | |
34 | ||
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"); | |
38 | err = -ENOMEM; | |
39 | goto out_handle_destroy; | |
40 | } | |
41 | ||
42 | state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211"); | |
43 | if (!state->nl80211) { | |
44 | fprintf(stderr, "nl80211 not found.\n"); | |
45 | err = -ENOENT; | |
46 | goto out_cache_free; | |
47 | } | |
48 | ||
49 | return 0; | |
50 | ||
51 | out_cache_free: | |
52 | nl_cache_free(state->nl_cache); | |
53 | out_handle_destroy: | |
54 | nl_handle_destroy(state->nl_handle); | |
55 | return err; | |
56 | } | |
57 | ||
58 | static void nl80211_cleanup(struct nl80211_state *state) | |
59 | { | |
60 | genl_family_put(state->nl80211); | |
61 | nl_cache_free(state->nl_cache); | |
62 | nl_handle_destroy(state->nl_handle); | |
63 | } | |
64 | ||
45c7212c JB |
65 | /* |
66 | * return | |
67 | * 0 - error | |
68 | * 1 - phy | |
69 | * 2 - dev | |
70 | */ | |
71 | static int get_phy_or_dev(int *argc, char ***argv, char **name) | |
72 | { | |
73 | char *type = (*argv)[0]; | |
74 | ||
75 | if (*argc < 2) | |
76 | return 0; | |
77 | ||
78 | *name = (*argv)[1]; | |
79 | ||
80 | *argc -= 2; | |
81 | *argv += 2; | |
82 | ||
83 | if (strcmp(type, "phy") == 0) | |
84 | return 1; | |
85 | if (strcmp(type, "dev") == 0) | |
86 | return 2; | |
87 | ||
88 | return 0; | |
89 | } | |
90 | ||
441e16d8 | 91 | static void usage(char *argv0) |
1cdd9016 | 92 | { |
3d1e8704 LCC |
93 | fprintf(stderr, "Usage: %1$s dev <phydev> <OBJECT> <COMMAND> [OPTIONS]" |
94 | "\n %1$s dev <phydev> info\n" | |
79f99b9a | 95 | "\n" |
3d1e8704 LCC |
96 | "where OBJECT := { interface | station | mpath }\n" |
97 | "and COMMAND := { add | del | set | get | dump }\n", | |
98 | argv0); | |
1cdd9016 MK |
99 | } |
100 | ||
cad53b3f JB |
101 | int main(int argc, char **argv) |
102 | { | |
103 | struct nl80211_state nlstate; | |
45c7212c | 104 | int err = 0, pod; |
1cdd9016 | 105 | char *ifname = NULL, *phyname = NULL, *type, *argv0; |
cad53b3f JB |
106 | |
107 | err = nl80211_init(&nlstate); | |
108 | if (err) | |
109 | return 1; | |
110 | ||
45c7212c JB |
111 | /* strip off self */ |
112 | argc--; | |
1cdd9016 MK |
113 | argv0 = *argv++; |
114 | ||
115 | if (argc == 0 || (argc == 1 && strcmp(*argv, "help") == 0)) { | |
116 | usage(argv0); | |
117 | goto out; | |
118 | } | |
45c7212c JB |
119 | |
120 | pod = get_phy_or_dev(&argc, &argv, &ifname); | |
121 | if (pod == 0) { | |
122 | err = 1; | |
123 | goto out; | |
124 | } | |
125 | ||
126 | if (pod == 1) { | |
127 | phyname = ifname; | |
128 | ifname = NULL; | |
129 | } | |
130 | ||
131 | if (argc <= 0) { | |
132 | err = 1; | |
133 | goto out; | |
134 | } | |
135 | ||
136 | type = argv[0]; | |
137 | argc--; | |
138 | argv++; | |
139 | ||
140 | if (strcmp(type, "interface") == 0) | |
141 | err = handle_interface(&nlstate, phyname, ifname, argc, argv); | |
b612d360 | 142 | else if (strcmp(type, "info") == 0) |
79f99b9a | 143 | err = handle_info(&nlstate, phyname, ifname); |
3d1e8704 LCC |
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); | |
45c7212c JB |
148 | else { |
149 | fprintf(stderr, "No such object type %s\n", type); | |
150 | err = 1; | |
151 | } | |
cad53b3f | 152 | |
45c7212c | 153 | out: |
cad53b3f JB |
154 | nl80211_cleanup(&nlstate); |
155 | ||
45c7212c | 156 | return err; |
cad53b3f | 157 | } |