]>
Commit | Line | Data |
---|---|---|
cad53b3f JB |
1 | /* |
2 | * nl80211 userspace tool | |
3 | * | |
4 | * Copyright 2007 Johannes Berg <johannes@sipsolutions.net> | |
5 | * | |
6 | * GPLv2 | |
7 | */ | |
8 | ||
9 | #include <errno.h> | |
10 | #include <stdio.h> | |
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 | ||
1cdd9016 MK |
93 | void usage(char *argv0) |
94 | { | |
79f99b9a JB |
95 | fprintf(stderr, "Usage: %1$s dev <phydev> interface <COMMAND> [OPTIONS]\n" |
96 | " %1$s dev <phydev> info\n" | |
97 | "\n" | |
2dfd6bfa AL |
98 | "where COMMAND := { add | del }\n" |
99 | "\n" | |
100 | "For add, OPTIONS := <name> type <type>\n" | |
101 | "For del, OPTIONS should be blank and phydev is the interface to delete.\n", argv0); | |
1cdd9016 MK |
102 | } |
103 | ||
cad53b3f JB |
104 | int main(int argc, char **argv) |
105 | { | |
106 | struct nl80211_state nlstate; | |
45c7212c | 107 | int err = 0, pod; |
1cdd9016 | 108 | char *ifname = NULL, *phyname = NULL, *type, *argv0; |
cad53b3f JB |
109 | |
110 | err = nl80211_init(&nlstate); | |
111 | if (err) | |
112 | return 1; | |
113 | ||
45c7212c JB |
114 | /* strip off self */ |
115 | argc--; | |
1cdd9016 MK |
116 | argv0 = *argv++; |
117 | ||
118 | if (argc == 0 || (argc == 1 && strcmp(*argv, "help") == 0)) { | |
119 | usage(argv0); | |
120 | goto out; | |
121 | } | |
45c7212c JB |
122 | |
123 | pod = get_phy_or_dev(&argc, &argv, &ifname); | |
124 | if (pod == 0) { | |
125 | err = 1; | |
126 | goto out; | |
127 | } | |
128 | ||
129 | if (pod == 1) { | |
130 | phyname = ifname; | |
131 | ifname = NULL; | |
132 | } | |
133 | ||
134 | if (argc <= 0) { | |
135 | err = 1; | |
136 | goto out; | |
137 | } | |
138 | ||
139 | type = argv[0]; | |
140 | argc--; | |
141 | argv++; | |
142 | ||
143 | if (strcmp(type, "interface") == 0) | |
144 | err = handle_interface(&nlstate, phyname, ifname, argc, argv); | |
79f99b9a JB |
145 | if (strcmp(type, "info") == 0) |
146 | err = handle_info(&nlstate, phyname, ifname); | |
45c7212c JB |
147 | else { |
148 | fprintf(stderr, "No such object type %s\n", type); | |
149 | err = 1; | |
150 | } | |
cad53b3f | 151 | |
45c7212c | 152 | out: |
cad53b3f JB |
153 | nl80211_cleanup(&nlstate); |
154 | ||
45c7212c | 155 | return err; |
cad53b3f | 156 | } |