]>
Commit | Line | Data |
---|---|---|
2ef1be68 | 1 | #include <net/if.h> |
45c7212c | 2 | #include <errno.h> |
d5ac8ad3 | 3 | #include <string.h> |
2ef1be68 | 4 | |
45c7212c JB |
5 | #include <netlink/genl/genl.h> |
6 | #include <netlink/genl/family.h> | |
7 | #include <netlink/genl/ctrl.h> | |
8 | #include <netlink/msg.h> | |
9 | #include <netlink/attr.h> | |
45c7212c | 10 | |
f408e01b | 11 | #include "nl80211.h" |
45c7212c JB |
12 | #include "iw.h" |
13 | ||
cd293761 | 14 | static char *mntr_flags[NL80211_MNTR_FLAG_MAX + 1] = { |
dd65496b | 15 | "none", |
cd293761 JB |
16 | "fcsfail", |
17 | "plcpfail", | |
18 | "control", | |
19 | "otherbss", | |
20 | "cook", | |
21 | }; | |
22 | ||
dd65496b JB |
23 | static int parse_mntr_flags(int *_argc, char ***_argv, |
24 | struct nl_msg *msg) | |
25 | { | |
26 | struct nl_msg *flags; | |
27 | int err = -ENOBUFS; | |
28 | enum nl80211_mntr_flags flag; | |
29 | int argc = *_argc; | |
30 | char **argv = *_argv; | |
31 | ||
32 | flags = nlmsg_alloc(); | |
33 | if (!flags) | |
34 | return -ENOMEM; | |
35 | ||
36 | while (argc) { | |
37 | int ok = 0; | |
38 | for (flag = __NL80211_MNTR_FLAG_INVALID; | |
e14cc99e | 39 | flag <= NL80211_MNTR_FLAG_MAX; flag++) { |
dd65496b JB |
40 | if (strcmp(*argv, mntr_flags[flag]) == 0) { |
41 | ok = 1; | |
4a3bf753 JB |
42 | /* |
43 | * This shouldn't be adding "flag" if that is | |
44 | * zero, but due to a problem in the kernel's | |
45 | * nl80211 code (using NLA_NESTED policy) it | |
46 | * will reject an empty nested attribute but | |
47 | * not one that contains an invalid attribute | |
48 | */ | |
dd65496b JB |
49 | NLA_PUT_FLAG(flags, flag); |
50 | break; | |
51 | } | |
52 | } | |
53 | if (!ok) { | |
54 | err = -EINVAL; | |
55 | goto out; | |
56 | } | |
57 | argc--; | |
58 | argv++; | |
59 | } | |
60 | ||
61 | nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags); | |
62 | err = 0; | |
63 | nla_put_failure: | |
64 | out: | |
65 | nlmsg_free(flags); | |
66 | ||
67 | *_argc = argc; | |
68 | *_argv = argv; | |
69 | ||
70 | return err; | |
71 | } | |
72 | ||
45c7212c JB |
73 | /* return 0 if not found, 1 if ok, -1 on error */ |
74 | static int get_if_type(int *argc, char ***argv, enum nl80211_iftype *type) | |
75 | { | |
76 | char *tpstr; | |
77 | ||
78 | if (*argc < 2) | |
79 | return 0; | |
80 | ||
81 | if (strcmp((*argv)[0], "type")) | |
82 | return 0; | |
83 | ||
84 | tpstr = (*argv)[1]; | |
85 | *argc -= 2; | |
86 | *argv += 2; | |
87 | ||
88 | if (strcmp(tpstr, "adhoc") == 0 || | |
89 | strcmp(tpstr, "ibss") == 0) { | |
90 | *type = NL80211_IFTYPE_ADHOC; | |
91 | return 1; | |
92 | } else if (strcmp(tpstr, "monitor") == 0) { | |
93 | *type = NL80211_IFTYPE_MONITOR; | |
94 | return 1; | |
c1d44a6c JB |
95 | } else if (strcmp(tpstr, "master") == 0) { |
96 | *type = NL80211_IFTYPE_UNSPECIFIED; | |
97 | fprintf(stderr, "See http://wireless.kernel.org/RTFM-AP.\n"); | |
98 | return 2; | |
99 | } else if (strcmp(tpstr, "ap") == 0) { | |
100 | *type = NL80211_IFTYPE_UNSPECIFIED; | |
101 | fprintf(stderr, "See http://wireless.kernel.org/RTFM-AP.\n"); | |
102 | return 2; | |
4d3a72da | 103 | } else if (strcmp(tpstr, "__ap") == 0) { |
45c7212c JB |
104 | *type = NL80211_IFTYPE_AP; |
105 | return 1; | |
4d3a72da | 106 | } else if (strcmp(tpstr, "__ap_vlan") == 0) { |
45c7212c JB |
107 | *type = NL80211_IFTYPE_AP_VLAN; |
108 | return 1; | |
109 | } else if (strcmp(tpstr, "wds") == 0) { | |
110 | *type = NL80211_IFTYPE_WDS; | |
111 | return 1; | |
112 | } else if (strcmp(tpstr, "station") == 0) { | |
113 | *type = NL80211_IFTYPE_STATION; | |
114 | return 1; | |
3d1e8704 LCC |
115 | } else if (strcmp(tpstr, "mp") == 0 || |
116 | strcmp(tpstr, "mesh") == 0) { | |
117 | *type = NL80211_IFTYPE_MESH_POINT; | |
118 | return 1; | |
45c7212c JB |
119 | } |
120 | ||
121 | ||
122 | fprintf(stderr, "invalid interface type %s\n", tpstr); | |
123 | return -1; | |
124 | } | |
125 | ||
70391ccf | 126 | static int handle_interface_add(struct nl_cb *cb, |
bd396f2a JB |
127 | struct nl_msg *msg, |
128 | int argc, char **argv) | |
45c7212c | 129 | { |
2dfd6bfa | 130 | char *name; |
3d1e8704 | 131 | char *mesh_id = NULL; |
45c7212c | 132 | enum nl80211_iftype type; |
70391ccf | 133 | int tpset; |
45c7212c | 134 | |
bd396f2a | 135 | if (argc < 1) |
5e75fd04 | 136 | return 1; |
45c7212c | 137 | |
2dfd6bfa | 138 | name = argv[0]; |
45c7212c JB |
139 | argc--; |
140 | argv++; | |
141 | ||
2dfd6bfa | 142 | tpset = get_if_type(&argc, &argv, &type); |
c1d44a6c JB |
143 | if (tpset != 1) |
144 | return tpset; | |
45c7212c | 145 | |
3d1e8704 | 146 | if (argc) { |
dd65496b JB |
147 | if (strcmp(argv[0], "mesh_id") == 0) { |
148 | argc--; | |
149 | argv++; | |
150 | ||
151 | if (!argc) | |
152 | return 1; | |
153 | mesh_id = argv[0]; | |
154 | argc--; | |
155 | argv++; | |
156 | } else if (strcmp(argv[0], "flags") == 0) { | |
157 | argc--; | |
158 | argv++; | |
159 | if (parse_mntr_flags(&argc, &argv, msg)) { | |
160 | fprintf(stderr, "flags error\n"); | |
161 | return 2; | |
162 | } | |
163 | } else { | |
5e75fd04 | 164 | return 1; |
dd65496b | 165 | } |
3d1e8704 LCC |
166 | } |
167 | ||
5e75fd04 JB |
168 | if (argc) |
169 | return 1; | |
45c7212c | 170 | |
45c7212c JB |
171 | NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name); |
172 | if (tpset) | |
173 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type); | |
3d1e8704 LCC |
174 | if (mesh_id) |
175 | NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id); | |
45c7212c | 176 | |
70391ccf | 177 | return 0; |
5e75fd04 | 178 | nla_put_failure: |
70391ccf | 179 | return -ENOBUFS; |
45c7212c | 180 | } |
dd65496b | 181 | COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [flags ...]", |
bd396f2a | 182 | NL80211_CMD_NEW_INTERFACE, 0, CIB_PHY, handle_interface_add); |
dd65496b | 183 | COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [flags ...]", |
1cd3b6c6 | 184 | NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add); |
45c7212c | 185 | |
70391ccf | 186 | static int handle_interface_del(struct nl_cb *cb, |
bd396f2a JB |
187 | struct nl_msg *msg, |
188 | int argc, char **argv) | |
3fcfe40e | 189 | { |
70391ccf | 190 | return 0; |
3fcfe40e | 191 | } |
bd396f2a | 192 | TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del); |
ce5af55c | 193 | HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del); |
3fcfe40e | 194 | |
541ef425 JB |
195 | static int print_iface_handler(struct nl_msg *msg, void *arg) |
196 | { | |
197 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
198 | struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; | |
199 | ||
200 | nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
201 | genlmsg_attrlen(gnlh, 0), NULL); | |
202 | ||
203 | if (tb_msg[NL80211_ATTR_IFNAME]) | |
204 | printf("Interface %s\n", nla_get_string(tb_msg[NL80211_ATTR_IFNAME])); | |
205 | if (tb_msg[NL80211_ATTR_IFINDEX]) | |
206 | printf("\tifindex %d\n", nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX])); | |
207 | if (tb_msg[NL80211_ATTR_IFTYPE]) | |
208 | printf("\ttype %s\n", iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE]))); | |
209 | ||
210 | return NL_SKIP; | |
211 | } | |
212 | ||
70391ccf | 213 | static int handle_interface_info(struct nl_cb *cb, |
bd396f2a JB |
214 | struct nl_msg *msg, |
215 | int argc, char **argv) | |
541ef425 | 216 | { |
541ef425 | 217 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL); |
70391ccf | 218 | return 0; |
541ef425 | 219 | } |
bd396f2a | 220 | TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info); |
cd293761 JB |
221 | |
222 | static int handle_interface_set(struct nl_cb *cb, | |
223 | struct nl_msg *msg, | |
224 | int argc, char **argv) | |
225 | { | |
cd293761 JB |
226 | if (!argc) |
227 | return 1; | |
228 | ||
cd293761 JB |
229 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR); |
230 | ||
dd65496b JB |
231 | switch (parse_mntr_flags(&argc, &argv, msg)) { |
232 | case 0: | |
233 | return 0; | |
234 | case -ENOMEM: | |
235 | fprintf(stderr, "failed to allocate flags\n"); | |
236 | return 2; | |
237 | case -EINVAL: | |
238 | fprintf(stderr, "unknown flag %s\n", *argv); | |
239 | return 2; | |
240 | default: | |
241 | return 2; | |
cd293761 | 242 | } |
cd293761 | 243 | nla_put_failure: |
dd65496b | 244 | return -ENOBUFS; |
cd293761 JB |
245 | } |
246 | COMMAND(set, monitor, "<flag> [...]", | |
247 | NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set); | |
82d028d0 JB |
248 | |
249 | static int handle_interface_meshid(struct nl_cb *cb, | |
250 | struct nl_msg *msg, | |
251 | int argc, char **argv) | |
252 | { | |
253 | char *mesh_id = NULL; | |
254 | ||
255 | if (argc != 1) | |
256 | return 1; | |
257 | ||
258 | mesh_id = argv[0]; | |
259 | ||
260 | NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id); | |
261 | ||
262 | return 0; | |
263 | nla_put_failure: | |
264 | return -ENOBUFS; | |
265 | } | |
266 | COMMAND(set, meshid, "<meshid>", | |
267 | NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid); |