blob: ad8fc88cf73e8554babb23a40e98bd562b75c93b [file] [log] [blame]
Johannes Berg2ef1be62008-04-02 17:40:11 +02001#include <linux/nl80211.h>
2#include <net/if.h>
Johannes Berg45c72122007-09-28 23:47:38 +02003#include <errno.h>
Johannes Bergd5ac8ad2008-04-03 15:24:13 +02004#include <string.h>
Johannes Berg2ef1be62008-04-02 17:40:11 +02005
Johannes Berg45c72122007-09-28 23:47:38 +02006#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>
Johannes Berg45c72122007-09-28 23:47:38 +020011
12#include "iw.h"
13
14/* return 0 if not found, 1 if ok, -1 on error */
15static int get_if_type(int *argc, char ***argv, enum nl80211_iftype *type)
16{
17 char *tpstr;
18
19 if (*argc < 2)
20 return 0;
21
22 if (strcmp((*argv)[0], "type"))
23 return 0;
24
25 tpstr = (*argv)[1];
26 *argc -= 2;
27 *argv += 2;
28
29 if (strcmp(tpstr, "adhoc") == 0 ||
30 strcmp(tpstr, "ibss") == 0) {
31 *type = NL80211_IFTYPE_ADHOC;
32 return 1;
33 } else if (strcmp(tpstr, "monitor") == 0) {
34 *type = NL80211_IFTYPE_MONITOR;
35 return 1;
Johannes Berg4d3a72d2008-04-14 15:41:31 +020036 } else if (strcmp(tpstr, "__ap") == 0) {
Johannes Berg45c72122007-09-28 23:47:38 +020037 *type = NL80211_IFTYPE_AP;
38 return 1;
Johannes Berg4d3a72d2008-04-14 15:41:31 +020039 } else if (strcmp(tpstr, "__ap_vlan") == 0) {
Johannes Berg45c72122007-09-28 23:47:38 +020040 *type = NL80211_IFTYPE_AP_VLAN;
41 return 1;
42 } else if (strcmp(tpstr, "wds") == 0) {
43 *type = NL80211_IFTYPE_WDS;
44 return 1;
45 } else if (strcmp(tpstr, "station") == 0) {
46 *type = NL80211_IFTYPE_STATION;
47 return 1;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -070048 } else if (strcmp(tpstr, "mp") == 0 ||
49 strcmp(tpstr, "mesh") == 0) {
50 *type = NL80211_IFTYPE_MESH_POINT;
51 return 1;
Johannes Berg45c72122007-09-28 23:47:38 +020052 }
53
54
55 fprintf(stderr, "invalid interface type %s\n", tpstr);
56 return -1;
57}
58
59static int handle_interface_add(struct nl80211_state *state,
Johannes Bergbd396f22008-09-16 16:56:09 +020060 struct nl_msg *msg,
61 int argc, char **argv)
Johannes Berg45c72122007-09-28 23:47:38 +020062{
Andrew Lutomirski2dfd6bf2007-12-20 18:50:07 +010063 char *name;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -070064 char *mesh_id = NULL;
Johannes Berg45c72122007-09-28 23:47:38 +020065 enum nl80211_iftype type;
Johannes Berg23dfe292008-04-03 15:39:27 +020066 int tpset, err = -ENOBUFS;
Johannes Berg45c72122007-09-28 23:47:38 +020067
Johannes Bergbd396f22008-09-16 16:56:09 +020068 if (argc < 1)
Johannes Berg5e75fd02008-09-16 18:13:12 +020069 return 1;
Johannes Berg45c72122007-09-28 23:47:38 +020070
Andrew Lutomirski2dfd6bf2007-12-20 18:50:07 +010071 name = argv[0];
Johannes Berg45c72122007-09-28 23:47:38 +020072 argc--;
73 argv++;
74
Andrew Lutomirski2dfd6bf2007-12-20 18:50:07 +010075 tpset = get_if_type(&argc, &argv, &type);
Andrew Lutomirski2dfd6bf2007-12-20 18:50:07 +010076 if (tpset <= 0)
Johannes Berg5e75fd02008-09-16 18:13:12 +020077 return 1;
Johannes Berg45c72122007-09-28 23:47:38 +020078
79 if (argc) {
Johannes Berg5e75fd02008-09-16 18:13:12 +020080 if (strcmp(argv[0], "mesh_id") != 0)
81 return 1;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -070082 argc--;
83 argv++;
84
Johannes Berg5e75fd02008-09-16 18:13:12 +020085 if (!argc)
86 return 1;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -070087 mesh_id = argv[0];
88 argc--;
89 argv++;
90 }
91
Johannes Berg5e75fd02008-09-16 18:13:12 +020092 if (argc)
93 return 1;
Johannes Berg45c72122007-09-28 23:47:38 +020094
Johannes Berg45c72122007-09-28 23:47:38 +020095 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
96 if (tpset)
97 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -070098 if (mesh_id)
99 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
Johannes Berg45c72122007-09-28 23:47:38 +0200100
Johannes Berg5e75fd02008-09-16 18:13:12 +0200101 err = nl_send_auto_complete(state->nl_handle, msg);
102 if (err > 0)
103 err = nl_wait_for_ack(state->nl_handle);
Johannes Berg45c72122007-09-28 23:47:38 +0200104
Johannes Berg5e75fd02008-09-16 18:13:12 +0200105 nla_put_failure:
106 return err;
Johannes Berg45c72122007-09-28 23:47:38 +0200107}
Johannes Bergbd396f22008-09-16 16:56:09 +0200108COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>]",
109 NL80211_CMD_NEW_INTERFACE, 0, CIB_PHY, handle_interface_add);
Johannes Berg1cd3b6c2008-09-16 17:52:56 +0200110COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>]",
111 NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add);
Johannes Berg45c72122007-09-28 23:47:38 +0200112
Johannes Berg3fcfe402007-09-28 23:50:25 +0200113static int handle_interface_del(struct nl80211_state *state,
Johannes Bergbd396f22008-09-16 16:56:09 +0200114 struct nl_msg *msg,
115 int argc, char **argv)
Johannes Berg3fcfe402007-09-28 23:50:25 +0200116{
Johannes Berg5e75fd02008-09-16 18:13:12 +0200117 int err;
Johannes Berg3fcfe402007-09-28 23:50:25 +0200118
Johannes Berg5e75fd02008-09-16 18:13:12 +0200119 err = nl_send_auto_complete(state->nl_handle, msg);
120 if (err > 0)
121 err = nl_wait_for_ack(state->nl_handle);
Johannes Berg3fcfe402007-09-28 23:50:25 +0200122
Johannes Berg5e75fd02008-09-16 18:13:12 +0200123 return err;
Johannes Berg3fcfe402007-09-28 23:50:25 +0200124}
Johannes Bergbd396f22008-09-16 16:56:09 +0200125TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
Johannes Berg3fcfe402007-09-28 23:50:25 +0200126
Johannes Berg541ef422008-09-16 14:50:11 +0200127static 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
145static int ack_wait_handler(struct nl_msg *msg, void *arg)
146{
147 int *finished = arg;
148
149 *finished = 1;
150 return NL_STOP;
151}
152
153static int handle_interface_info(struct nl80211_state *state,
Johannes Bergbd396f22008-09-16 16:56:09 +0200154 struct nl_msg *msg,
155 int argc, char **argv)
Johannes Berg541ef422008-09-16 14:50:11 +0200156{
157 int err = -ENOBUFS;
Johannes Berg541ef422008-09-16 14:50:11 +0200158 struct nl_cb *cb = NULL;
159 int finished = 0;
160
Johannes Berg541ef422008-09-16 14:50:11 +0200161 cb = nl_cb_alloc(NL_CB_CUSTOM);
162 if (!cb)
163 goto out;
164
Johannes Berg5e75fd02008-09-16 18:13:12 +0200165 err = nl_send_auto_complete(state->nl_handle, msg);
166 if (err)
Johannes Berg541ef422008-09-16 14:50:11 +0200167 goto out;
168
169 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL);
170 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
171
Johannes Berg5e75fd02008-09-16 18:13:12 +0200172 nl_recvmsgs(state->nl_handle, cb);
173 err = 0;
Johannes Berg541ef422008-09-16 14:50:11 +0200174
175 if (!finished)
176 err = nl_wait_for_ack(state->nl_handle);
177
Johannes Berg541ef422008-09-16 14:50:11 +0200178 out:
Johannes Berg541ef422008-09-16 14:50:11 +0200179 nl_cb_put(cb);
Johannes Berg5e75fd02008-09-16 18:13:12 +0200180 return err;
Johannes Berg541ef422008-09-16 14:50:11 +0200181}
Johannes Bergbd396f22008-09-16 16:56:09 +0200182TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info);