]>
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; | |
39 | flag < NL80211_MNTR_FLAG_MAX; flag++) { | |
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; | |
4d3a72da | 95 | } else if (strcmp(tpstr, "__ap") == 0) { |
45c7212c JB |
96 | *type = NL80211_IFTYPE_AP; |
97 | return 1; | |
4d3a72da | 98 | } else if (strcmp(tpstr, "__ap_vlan") == 0) { |
45c7212c JB |
99 | *type = NL80211_IFTYPE_AP_VLAN; |
100 | return 1; | |
101 | } else if (strcmp(tpstr, "wds") == 0) { | |
102 | *type = NL80211_IFTYPE_WDS; | |
103 | return 1; | |
104 | } else if (strcmp(tpstr, "station") == 0) { | |
105 | *type = NL80211_IFTYPE_STATION; | |
106 | return 1; | |
3d1e8704 LCC |
107 | } else if (strcmp(tpstr, "mp") == 0 || |
108 | strcmp(tpstr, "mesh") == 0) { | |
109 | *type = NL80211_IFTYPE_MESH_POINT; | |
110 | return 1; | |
45c7212c JB |
111 | } |
112 | ||
113 | ||
114 | fprintf(stderr, "invalid interface type %s\n", tpstr); | |
115 | return -1; | |
116 | } | |
117 | ||
70391ccf | 118 | static int handle_interface_add(struct nl_cb *cb, |
bd396f2a JB |
119 | struct nl_msg *msg, |
120 | int argc, char **argv) | |
45c7212c | 121 | { |
2dfd6bfa | 122 | char *name; |
3d1e8704 | 123 | char *mesh_id = NULL; |
45c7212c | 124 | enum nl80211_iftype type; |
70391ccf | 125 | int tpset; |
45c7212c | 126 | |
bd396f2a | 127 | if (argc < 1) |
5e75fd04 | 128 | return 1; |
45c7212c | 129 | |
2dfd6bfa | 130 | name = argv[0]; |
45c7212c JB |
131 | argc--; |
132 | argv++; | |
133 | ||
2dfd6bfa | 134 | tpset = get_if_type(&argc, &argv, &type); |
2dfd6bfa | 135 | if (tpset <= 0) |
5e75fd04 | 136 | return 1; |
45c7212c | 137 | |
3d1e8704 | 138 | if (argc) { |
dd65496b JB |
139 | if (strcmp(argv[0], "mesh_id") == 0) { |
140 | argc--; | |
141 | argv++; | |
142 | ||
143 | if (!argc) | |
144 | return 1; | |
145 | mesh_id = argv[0]; | |
146 | argc--; | |
147 | argv++; | |
148 | } else if (strcmp(argv[0], "flags") == 0) { | |
149 | argc--; | |
150 | argv++; | |
151 | if (parse_mntr_flags(&argc, &argv, msg)) { | |
152 | fprintf(stderr, "flags error\n"); | |
153 | return 2; | |
154 | } | |
155 | } else { | |
5e75fd04 | 156 | return 1; |
dd65496b | 157 | } |
3d1e8704 LCC |
158 | } |
159 | ||
5e75fd04 JB |
160 | if (argc) |
161 | return 1; | |
45c7212c | 162 | |
45c7212c JB |
163 | NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name); |
164 | if (tpset) | |
165 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type); | |
3d1e8704 LCC |
166 | if (mesh_id) |
167 | NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id); | |
45c7212c | 168 | |
70391ccf | 169 | return 0; |
5e75fd04 | 170 | nla_put_failure: |
70391ccf | 171 | return -ENOBUFS; |
45c7212c | 172 | } |
dd65496b | 173 | COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [flags ...]", |
bd396f2a | 174 | NL80211_CMD_NEW_INTERFACE, 0, CIB_PHY, handle_interface_add); |
dd65496b | 175 | COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [flags ...]", |
1cd3b6c6 | 176 | NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add); |
45c7212c | 177 | |
70391ccf | 178 | static int handle_interface_del(struct nl_cb *cb, |
bd396f2a JB |
179 | struct nl_msg *msg, |
180 | int argc, char **argv) | |
3fcfe40e | 181 | { |
70391ccf | 182 | return 0; |
3fcfe40e | 183 | } |
bd396f2a | 184 | TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del); |
ce5af55c | 185 | HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del); |
3fcfe40e | 186 | |
541ef425 JB |
187 | static int print_iface_handler(struct nl_msg *msg, void *arg) |
188 | { | |
189 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
190 | struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; | |
191 | ||
192 | nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
193 | genlmsg_attrlen(gnlh, 0), NULL); | |
194 | ||
195 | if (tb_msg[NL80211_ATTR_IFNAME]) | |
196 | printf("Interface %s\n", nla_get_string(tb_msg[NL80211_ATTR_IFNAME])); | |
197 | if (tb_msg[NL80211_ATTR_IFINDEX]) | |
198 | printf("\tifindex %d\n", nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX])); | |
199 | if (tb_msg[NL80211_ATTR_IFTYPE]) | |
200 | printf("\ttype %s\n", iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE]))); | |
201 | ||
202 | return NL_SKIP; | |
203 | } | |
204 | ||
70391ccf | 205 | static int handle_interface_info(struct nl_cb *cb, |
bd396f2a JB |
206 | struct nl_msg *msg, |
207 | int argc, char **argv) | |
541ef425 | 208 | { |
541ef425 | 209 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL); |
70391ccf | 210 | return 0; |
541ef425 | 211 | } |
bd396f2a | 212 | TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info); |
cd293761 JB |
213 | |
214 | static int handle_interface_set(struct nl_cb *cb, | |
215 | struct nl_msg *msg, | |
216 | int argc, char **argv) | |
217 | { | |
cd293761 JB |
218 | if (!argc) |
219 | return 1; | |
220 | ||
cd293761 JB |
221 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR); |
222 | ||
dd65496b JB |
223 | switch (parse_mntr_flags(&argc, &argv, msg)) { |
224 | case 0: | |
225 | return 0; | |
226 | case -ENOMEM: | |
227 | fprintf(stderr, "failed to allocate flags\n"); | |
228 | return 2; | |
229 | case -EINVAL: | |
230 | fprintf(stderr, "unknown flag %s\n", *argv); | |
231 | return 2; | |
232 | default: | |
233 | return 2; | |
cd293761 | 234 | } |
cd293761 | 235 | nla_put_failure: |
dd65496b | 236 | return -ENOBUFS; |
cd293761 JB |
237 | } |
238 | COMMAND(set, monitor, "<flag> [...]", | |
239 | NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set); | |
82d028d0 JB |
240 | |
241 | static int handle_interface_meshid(struct nl_cb *cb, | |
242 | struct nl_msg *msg, | |
243 | int argc, char **argv) | |
244 | { | |
245 | char *mesh_id = NULL; | |
246 | ||
247 | if (argc != 1) | |
248 | return 1; | |
249 | ||
250 | mesh_id = argv[0]; | |
251 | ||
252 | NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id); | |
253 | ||
254 | return 0; | |
255 | nla_put_failure: | |
256 | return -ENOBUFS; | |
257 | } | |
258 | COMMAND(set, meshid, "<meshid>", | |
259 | NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid); |