]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
iw: Add support to send a request to set the regulatory domain
[thirdparty/iw.git] / iw.c
1 /*
2 * nl80211 userspace tool
3 *
4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
5 */
6
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10
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
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
93 static void usage(char *argv0)
94 {
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"
98 "\n"
99 "where OBJECT := { interface | station | mpath | info }\n"
100 "and COMMAND := { add | del | set | get | dump }\n",
101 argv0);
102 }
103
104 int main(int argc, char **argv)
105 {
106 struct nl80211_state nlstate;
107 int err = 0, pod;
108 char *ifname = NULL, *phyname = NULL, *type, *argv0;
109
110 err = nl80211_init(&nlstate);
111 if (err)
112 return 1;
113
114 /* strip off self */
115 argc--;
116 argv0 = *argv++;
117
118 if (argc == 0 || (argc == 1 && strcmp(*argv, "help") == 0)) {
119 usage(argv0);
120 goto out;
121 }
122
123 if (strcmp(argv[0], "reg") == 0) {
124 argc--;
125 argv++;
126 err = handle_reg(&nlstate, argc, argv);
127 goto out;
128 }
129
130 pod = get_phy_or_dev(&argc, &argv, &ifname);
131 if (pod == 0) {
132 err = 1;
133 goto out;
134 }
135
136 if (pod == 1) {
137 phyname = ifname;
138 ifname = NULL;
139 }
140
141 if (argc <= 0) {
142 err = 1;
143 goto out;
144 }
145
146 type = argv[0];
147 argc--;
148 argv++;
149
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);
158 else {
159 fprintf(stderr, "No such object type %s\n", type);
160 err = 1;
161 }
162
163 out:
164 nl80211_cleanup(&nlstate);
165
166 return err;
167 }