]> git.ipfire.org Git - thirdparty/iw.git/blob - vendor.c
iw: add helpful hints for vendor cmd
[thirdparty/iw.git] / vendor.c
1 #include <errno.h>
2 #include <string.h>
3
4 #include <netlink/genl/genl.h>
5 #include <netlink/genl/family.h>
6 #include <netlink/genl/ctrl.h>
7 #include <netlink/msg.h>
8 #include <netlink/attr.h>
9
10 #include "nl80211.h"
11 #include "iw.h"
12
13 SECTION(vendor);
14
15 static int read_file(FILE *file, char *buf, size_t size)
16 {
17 int data, count = 0;
18
19 while ((data = fgetc(file)) != EOF) {
20 if (count >= size)
21 return -EINVAL;
22 buf[count] = data;
23 count++;
24 }
25
26 return count;
27 }
28
29 static int read_hex(int argc, char **argv, char *buf, size_t size)
30 {
31 int i, res;
32 unsigned int data;
33
34 if (argc > size)
35 return -EINVAL;
36
37 for (i = 0; i < argc; i++) {
38 res = sscanf(argv[i], "0x%x", &data);
39 if (res != 1 || data > 0xff)
40 return -EINVAL;
41 buf[i] = data;
42 }
43
44 return argc;
45 }
46
47 static int handle_vendor(struct nl80211_state *state,
48 struct nl_msg *msg, int argc, char **argv,
49 enum id_input id)
50 {
51 unsigned int oui;
52 unsigned int subcmd;
53 char buf[2048] = {};
54 int res, count = 0;
55 FILE *file = NULL;
56
57 if (argc < 3)
58 return 1;
59
60 res = sscanf(argv[0], "0x%x", &oui);
61 if (res != 1) {
62 printf("Vendor command must start with 0x\n");
63 return 2;
64 }
65
66 res = sscanf(argv[1], "0x%x", &subcmd);
67 if (res != 1) {
68 printf("Sub command must start with 0x\n");
69 return 2;
70 }
71
72 if (!strcmp(argv[2], "-"))
73 file = stdin;
74 else
75 file = fopen(argv[2], "r");
76
77 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, oui);
78 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
79
80 if (file) {
81 count = read_file(file, buf, sizeof(buf));
82 fclose(file);
83 } else
84 count = read_hex(argc - 2, &argv[2], buf, sizeof(buf));
85
86 if (count < 0)
87 return -EINVAL;
88
89 if (count > 0)
90 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, count, buf);
91
92 return 0;
93
94 nla_put_failure:
95 return -ENOBUFS;
96 }
97
98 COMMAND(vendor, send, "<oui> <subcmd> <filename|-|hex data>", NL80211_CMD_VENDOR, 0, CIB_NETDEV, handle_vendor, "");