]> git.ipfire.org Git - thirdparty/iw.git/blob - interface.c
ship nl80211.h
[thirdparty/iw.git] / interface.c
1 #include <net/if.h>
2 #include <errno.h>
3 #include <string.h>
4
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>
10
11 #include "nl80211.h"
12 #include "iw.h"
13
14 static char *mntr_flags[NL80211_MNTR_FLAG_MAX + 1] = {
15 NULL,
16 "fcsfail",
17 "plcpfail",
18 "control",
19 "otherbss",
20 "cook",
21 };
22
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;
45 } else if (strcmp(tpstr, "__ap") == 0) {
46 *type = NL80211_IFTYPE_AP;
47 return 1;
48 } else if (strcmp(tpstr, "__ap_vlan") == 0) {
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;
57 } else if (strcmp(tpstr, "mp") == 0 ||
58 strcmp(tpstr, "mesh") == 0) {
59 *type = NL80211_IFTYPE_MESH_POINT;
60 return 1;
61 }
62
63
64 fprintf(stderr, "invalid interface type %s\n", tpstr);
65 return -1;
66 }
67
68 static int handle_interface_add(struct nl_cb *cb,
69 struct nl_msg *msg,
70 int argc, char **argv)
71 {
72 char *name;
73 char *mesh_id = NULL;
74 enum nl80211_iftype type;
75 int tpset;
76
77 if (argc < 1)
78 return 1;
79
80 name = argv[0];
81 argc--;
82 argv++;
83
84 tpset = get_if_type(&argc, &argv, &type);
85 if (tpset <= 0)
86 return 1;
87
88 if (argc) {
89 if (strcmp(argv[0], "mesh_id") != 0)
90 return 1;
91 argc--;
92 argv++;
93
94 if (!argc)
95 return 1;
96 mesh_id = argv[0];
97 argc--;
98 argv++;
99 }
100
101 if (argc)
102 return 1;
103
104 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
105 if (tpset)
106 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
107 if (mesh_id)
108 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
109
110 return 0;
111 nla_put_failure:
112 return -ENOBUFS;
113 }
114 COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>]",
115 NL80211_CMD_NEW_INTERFACE, 0, CIB_PHY, handle_interface_add);
116 COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>]",
117 NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add);
118
119 static int handle_interface_del(struct nl_cb *cb,
120 struct nl_msg *msg,
121 int argc, char **argv)
122 {
123 return 0;
124 }
125 TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
126 HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
127
128 static int print_iface_handler(struct nl_msg *msg, void *arg)
129 {
130 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
131 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
132
133 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
134 genlmsg_attrlen(gnlh, 0), NULL);
135
136 if (tb_msg[NL80211_ATTR_IFNAME])
137 printf("Interface %s\n", nla_get_string(tb_msg[NL80211_ATTR_IFNAME]));
138 if (tb_msg[NL80211_ATTR_IFINDEX])
139 printf("\tifindex %d\n", nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]));
140 if (tb_msg[NL80211_ATTR_IFTYPE])
141 printf("\ttype %s\n", iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE])));
142
143 return NL_SKIP;
144 }
145
146 static int handle_interface_info(struct nl_cb *cb,
147 struct nl_msg *msg,
148 int argc, char **argv)
149 {
150 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL);
151 return 0;
152 }
153 TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info);
154
155 static int handle_interface_set(struct nl_cb *cb,
156 struct nl_msg *msg,
157 int argc, char **argv)
158 {
159 enum nl80211_mntr_flags flag;
160 struct nl_msg *flags;
161 int err;
162
163 if (!argc)
164 return 1;
165
166 flags = nlmsg_alloc();
167 if (!flags) {
168 fprintf(stderr, "failed to allocate flags\n");
169 return 2;
170 }
171
172 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
173
174 while (argc) {
175 int ok = 0;
176 for (flag = __NL80211_MNTR_FLAG_INVALID + 1;
177 flag < NL80211_MNTR_FLAG_MAX; flag++) {
178 if (strcmp(*argv, mntr_flags[flag]) == 0) {
179 ok = 1;
180 NLA_PUT_FLAG(flags, flag);
181 break;
182 }
183 }
184 if (!ok) {
185 fprintf(stderr, "unknown flag %s\n", *argv);
186 err = 2;
187 goto out;
188 }
189 argc--;
190 argv++;
191 }
192
193 nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
194
195 err = 0;
196 goto out;
197 nla_put_failure:
198 err = -ENOBUFS;
199 out:
200 nlmsg_free(flags);
201 return err;
202 }
203 COMMAND(set, monitor, "<flag> [...]",
204 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set);
205
206 static int handle_interface_meshid(struct nl_cb *cb,
207 struct nl_msg *msg,
208 int argc, char **argv)
209 {
210 char *mesh_id = NULL;
211
212 if (argc != 1)
213 return 1;
214
215 mesh_id = argv[0];
216
217 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
218
219 return 0;
220 nla_put_failure:
221 return -ENOBUFS;
222 }
223 COMMAND(set, meshid, "<meshid>",
224 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid);