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