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