]> git.ipfire.org Git - thirdparty/iw.git/blob - vendor.c
d0b4f9ef4d5cc966b639b0141622fb9c91fa20f2
[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 -EINVAL;
59
60 res = sscanf(argv[0], "0x%x", &oui);
61 if (res != 1)
62 return -EINVAL;
63
64 res = sscanf(argv[1], "0x%x", &subcmd);
65 if (res != 1)
66 return -EINVAL;
67
68 if (!strcmp(argv[2], "-"))
69 file = stdin;
70 else
71 file = fopen(argv[2], "r");
72
73 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, oui);
74 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
75
76 if (file) {
77 count = read_file(file, buf, sizeof(buf));
78 fclose(file);
79 } else
80 count = read_hex(argc - 2, &argv[2], buf, sizeof(buf));
81
82 if (count < 0)
83 return -EINVAL;
84
85 if (count > 0)
86 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, count, buf);
87
88 return 0;
89
90 nla_put_failure:
91 return -ENOBUFS;
92 }
93
94 COMMAND(vendor, send, "<oui> <subcmd> <filename|-|hex data>", NL80211_CMD_VENDOR, 0, CIB_NETDEV, handle_vendor, "");