Johannes Berg | 2ef1be6 | 2008-04-02 17:40:11 +0200 | [diff] [blame] | 1 | #include <net/if.h> |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 2 | #include <errno.h> |
Johannes Berg | d5ac8ad | 2008-04-03 15:24:13 +0200 | [diff] [blame] | 3 | #include <string.h> |
Johannes Berg | 2ef1be6 | 2008-04-02 17:40:11 +0200 | [diff] [blame] | 4 | |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 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> |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 10 | |
Johannes Berg | f408e01 | 2008-09-18 19:32:11 +0200 | [diff] [blame] | 11 | #include "nl80211.h" |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 12 | #include "iw.h" |
| 13 | |
Johannes Berg | cd29376 | 2008-09-16 20:13:59 +0200 | [diff] [blame] | 14 | static char *mntr_flags[NL80211_MNTR_FLAG_MAX + 1] = { |
Johannes Berg | dd65496 | 2008-10-05 14:00:11 +0200 | [diff] [blame] | 15 | "none", |
Johannes Berg | cd29376 | 2008-09-16 20:13:59 +0200 | [diff] [blame] | 16 | "fcsfail", |
| 17 | "plcpfail", |
| 18 | "control", |
| 19 | "otherbss", |
| 20 | "cook", |
| 21 | }; |
| 22 | |
Johannes Berg | dd65496 | 2008-10-05 14:00:11 +0200 | [diff] [blame] | 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; |
Johannes Berg | e14cc99 | 2009-03-08 13:32:26 +0100 | [diff] [blame] | 39 | flag <= NL80211_MNTR_FLAG_MAX; flag++) { |
Johannes Berg | dd65496 | 2008-10-05 14:00:11 +0200 | [diff] [blame] | 40 | if (strcmp(*argv, mntr_flags[flag]) == 0) { |
| 41 | ok = 1; |
Johannes Berg | 4a3bf75 | 2008-10-15 11:55:42 +0200 | [diff] [blame] | 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 | */ |
Johannes Berg | dd65496 | 2008-10-05 14:00:11 +0200 | [diff] [blame] | 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 | |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 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; |
Johannes Berg | c1d44a6 | 2009-01-24 15:35:30 +0100 | [diff] [blame] | 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; |
Johannes Berg | 4d3a72d | 2008-04-14 15:41:31 +0200 | [diff] [blame] | 103 | } else if (strcmp(tpstr, "__ap") == 0) { |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 104 | *type = NL80211_IFTYPE_AP; |
| 105 | return 1; |
Johannes Berg | 4d3a72d | 2008-04-14 15:41:31 +0200 | [diff] [blame] | 106 | } else if (strcmp(tpstr, "__ap_vlan") == 0) { |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 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; |
Luis Carlos Cobo | 3d1e870 | 2008-04-01 12:03:41 -0700 | [diff] [blame] | 115 | } else if (strcmp(tpstr, "mp") == 0 || |
| 116 | strcmp(tpstr, "mesh") == 0) { |
| 117 | *type = NL80211_IFTYPE_MESH_POINT; |
| 118 | return 1; |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
| 122 | fprintf(stderr, "invalid interface type %s\n", tpstr); |
| 123 | return -1; |
| 124 | } |
| 125 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 126 | static int handle_interface_add(struct nl_cb *cb, |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 127 | struct nl_msg *msg, |
| 128 | int argc, char **argv) |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 129 | { |
Andrew Lutomirski | 2dfd6bf | 2007-12-20 18:50:07 +0100 | [diff] [blame] | 130 | char *name; |
Luis Carlos Cobo | 3d1e870 | 2008-04-01 12:03:41 -0700 | [diff] [blame] | 131 | char *mesh_id = NULL; |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 132 | enum nl80211_iftype type; |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 133 | int tpset; |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 134 | |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 135 | if (argc < 1) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 136 | return 1; |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 137 | |
Andrew Lutomirski | 2dfd6bf | 2007-12-20 18:50:07 +0100 | [diff] [blame] | 138 | name = argv[0]; |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 139 | argc--; |
| 140 | argv++; |
| 141 | |
Andrew Lutomirski | 2dfd6bf | 2007-12-20 18:50:07 +0100 | [diff] [blame] | 142 | tpset = get_if_type(&argc, &argv, &type); |
Johannes Berg | c1d44a6 | 2009-01-24 15:35:30 +0100 | [diff] [blame] | 143 | if (tpset != 1) |
| 144 | return tpset; |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 145 | |
| 146 | if (argc) { |
Johannes Berg | dd65496 | 2008-10-05 14:00:11 +0200 | [diff] [blame] | 147 | if (strcmp(argv[0], "mesh_id") == 0) { |
| 148 | argc--; |
| 149 | argv++; |
Luis Carlos Cobo | 3d1e870 | 2008-04-01 12:03:41 -0700 | [diff] [blame] | 150 | |
Johannes Berg | dd65496 | 2008-10-05 14:00:11 +0200 | [diff] [blame] | 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 { |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 164 | return 1; |
Johannes Berg | dd65496 | 2008-10-05 14:00:11 +0200 | [diff] [blame] | 165 | } |
Luis Carlos Cobo | 3d1e870 | 2008-04-01 12:03:41 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 168 | if (argc) |
| 169 | return 1; |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 170 | |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 171 | NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name); |
| 172 | if (tpset) |
| 173 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type); |
Luis Carlos Cobo | 3d1e870 | 2008-04-01 12:03:41 -0700 | [diff] [blame] | 174 | if (mesh_id) |
| 175 | NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id); |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 176 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 177 | return 0; |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 178 | nla_put_failure: |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 179 | return -ENOBUFS; |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 180 | } |
Johannes Berg | dd65496 | 2008-10-05 14:00:11 +0200 | [diff] [blame] | 181 | COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [flags ...]", |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 182 | NL80211_CMD_NEW_INTERFACE, 0, CIB_PHY, handle_interface_add); |
Johannes Berg | dd65496 | 2008-10-05 14:00:11 +0200 | [diff] [blame] | 183 | COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [flags ...]", |
Johannes Berg | 1cd3b6c | 2008-09-16 17:52:56 +0200 | [diff] [blame] | 184 | NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add); |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 185 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 186 | static int handle_interface_del(struct nl_cb *cb, |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 187 | struct nl_msg *msg, |
| 188 | int argc, char **argv) |
Johannes Berg | 3fcfe40 | 2007-09-28 23:50:25 +0200 | [diff] [blame] | 189 | { |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 190 | return 0; |
Johannes Berg | 3fcfe40 | 2007-09-28 23:50:25 +0200 | [diff] [blame] | 191 | } |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 192 | TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del); |
Johannes Berg | ce5af55 | 2008-09-17 13:27:04 +0200 | [diff] [blame] | 193 | HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del); |
Johannes Berg | 3fcfe40 | 2007-09-28 23:50:25 +0200 | [diff] [blame] | 194 | |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 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 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 213 | static int handle_interface_info(struct nl_cb *cb, |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 214 | struct nl_msg *msg, |
| 215 | int argc, char **argv) |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 216 | { |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 217 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL); |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 218 | return 0; |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 219 | } |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 220 | TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info); |
Johannes Berg | cd29376 | 2008-09-16 20:13:59 +0200 | [diff] [blame] | 221 | |
| 222 | static int handle_interface_set(struct nl_cb *cb, |
| 223 | struct nl_msg *msg, |
| 224 | int argc, char **argv) |
| 225 | { |
Johannes Berg | cd29376 | 2008-09-16 20:13:59 +0200 | [diff] [blame] | 226 | if (!argc) |
| 227 | return 1; |
| 228 | |
Johannes Berg | cd29376 | 2008-09-16 20:13:59 +0200 | [diff] [blame] | 229 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR); |
| 230 | |
Johannes Berg | dd65496 | 2008-10-05 14:00:11 +0200 | [diff] [blame] | 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; |
Johannes Berg | cd29376 | 2008-09-16 20:13:59 +0200 | [diff] [blame] | 242 | } |
Johannes Berg | cd29376 | 2008-09-16 20:13:59 +0200 | [diff] [blame] | 243 | nla_put_failure: |
Johannes Berg | dd65496 | 2008-10-05 14:00:11 +0200 | [diff] [blame] | 244 | return -ENOBUFS; |
Johannes Berg | cd29376 | 2008-09-16 20:13:59 +0200 | [diff] [blame] | 245 | } |
| 246 | COMMAND(set, monitor, "<flag> [...]", |
| 247 | NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set); |
Johannes Berg | 82d028d | 2008-09-16 20:45:34 +0200 | [diff] [blame] | 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); |