xref: /openwifi/user_space/sdrctl_src/sdrctl.h (revision c654e5ed88622f78b3db1a2d59dbf2fe7a550e97)
1 /*
2  * Author: Xianjun Jiao
3  * SPDX-FileCopyrightText: 2019 UGent
4  * SPDX-License-Identifier: AGPL-3.0-or-later
5 */
6 
7 #ifndef __IW_H
8 #define __IW_H
9 
10 #include <stdbool.h>
11 #include <netlink/netlink.h>
12 #include <netlink/genl/genl.h>
13 #include <netlink/genl/family.h>
14 #include <netlink/genl/ctrl.h>
15 #include <endian.h>
16 
17 #include "nl80211.h"
18 //#include "ieee80211.h"
19 
20 #define ETH_ALEN 6
21 
22 /* libnl 1.x compatibility code */
23 #if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
24 #  define nl_sock nl_handle
25 #endif
26 
27 struct nl80211_state {
28 	struct nl_sock *nl_sock;
29 	int nl80211_id;
30 };
31 
32 enum command_identify_by {
33 	CIB_NONE,
34 	CIB_PHY,
35 	CIB_NETDEV,
36 	CIB_WDEV,
37 };
38 
39 enum id_input {
40 	II_NONE,
41 	II_NETDEV,
42 	II_PHY_NAME,
43 	II_PHY_IDX,
44 	II_WDEV,
45 };
46 
47 struct cmd {
48 	const char *name;
49 	const char *args;
50 	const char *help;
51 	const enum nl80211_commands cmd;
52 	int nl_msg_flags;
53 	int hidden;
54 	const enum command_identify_by idby;
55 	/*
56 	 * The handler should return a negative error code,
57 	 * zero on success, 1 if the arguments were wrong
58 	 * and the usage message should and 2 otherwise.
59 	 */
60 	int (*handler)(struct nl80211_state *state,
61 		       struct nl_cb *cb,
62 		       struct nl_msg *msg,
63 		       int argc, char **argv,
64 		       enum id_input id);
65 	const struct cmd *(*selector)(int argc, char **argv);
66 	const struct cmd *parent;
67 };
68 
69 #define ARRAY_SIZE(ar) (sizeof(ar)/sizeof(ar[0]))
70 #define DIV_ROUND_UP(x, y) (((x) + (y - 1)) / (y))
71 
72 #define __COMMAND(_section, _symname, _name, _args, _nlcmd, _flags, _hidden, _idby, _handler, _help, _sel)\
73 	static struct cmd						\
74 	__cmd ## _ ## _symname ## _ ## _handler ## _ ## _nlcmd ## _ ## _idby ## _ ## _hidden\
75 	__attribute__((used)) __attribute__((section("__cmd")))	= {	\
76 		.name = (_name),					\
77 		.args = (_args),					\
78 		.cmd = (_nlcmd),					\
79 		.nl_msg_flags = (_flags),				\
80 		.hidden = (_hidden),					\
81 		.idby = (_idby),					\
82 		.handler = (_handler),					\
83 		.help = (_help),					\
84 		.parent = _section,					\
85 		.selector = (_sel),					\
86 	}
87 #define __ACMD(_section, _symname, _name, _args, _nlcmd, _flags, _hidden, _idby, _handler, _help, _sel, _alias)\
88 	__COMMAND(_section, _symname, _name, _args, _nlcmd, _flags, _hidden, _idby, _handler, _help, _sel);\
89 	static const struct cmd *_alias = &__cmd ## _ ## _symname ## _ ## _handler ## _ ## _nlcmd ## _ ## _idby ## _ ## _hidden
90 #define COMMAND(section, name, args, cmd, flags, idby, handler, help)	\
91 	__COMMAND(&(__section ## _ ## section), name, #name, args, cmd, flags, 0, idby, handler, help, NULL)
92 #define COMMAND_ALIAS(section, name, args, cmd, flags, idby, handler, help, selector, alias)\
93 	__ACMD(&(__section ## _ ## section), name, #name, args, cmd, flags, 0, idby, handler, help, selector, alias)
94 #define HIDDEN(section, name, args, cmd, flags, idby, handler)		\
95 	__COMMAND(&(__section ## _ ## section), name, #name, args, cmd, flags, 1, idby, handler, NULL, NULL)
96 
97 #define TOPLEVEL(_name, _args, _nlcmd, _flags, _idby, _handler, _help)	\
98 	struct cmd							\
99 	__section ## _ ## _name						\
100 	__attribute__((used)) __attribute__((section("__cmd")))	= {	\
101 		.name = (#_name),					\
102 		.args = (_args),					\
103 		.cmd = (_nlcmd),					\
104 		.nl_msg_flags = (_flags),				\
105 		.idby = (_idby),					\
106 		.handler = (_handler),					\
107 		.help = (_help),					\
108 	 }
109 #define SECTION(_name)							\
110 	struct cmd __section ## _ ## _name				\
111 	__attribute__((used)) __attribute__((section("__cmd"))) = {	\
112 		.name = (#_name),					\
113 		.hidden = 1,						\
114 	}
115 
116 #define DECLARE_SECTION(_name)						\
117 	extern struct cmd __section ## _ ## _name;
118 
119 extern const char sdrctl_version[];
120 
121 extern int iw_debug;
122 
123 int handle_cmd(struct nl80211_state *state, enum id_input idby,
124 	       int argc, char **argv);
125 
126 struct print_event_args {
127 	struct timeval ts; /* internal */
128 	bool have_ts; /* must be set false */
129 	bool frame, time, reltime;
130 };
131 
132 __u32 listen_events(struct nl80211_state *state,
133 		    const int n_waits, const __u32 *waits);
134 int __prepare_listen_events(struct nl80211_state *state);
135 __u32 __do_listen_events(struct nl80211_state *state,
136 			 const int n_waits, const __u32 *waits,
137 			 struct print_event_args *args);
138 
139 
140 int mac_addr_a2n(unsigned char *mac_addr, char *arg);
141 void mac_addr_n2a(char *mac_addr, unsigned char *arg);
142 int parse_hex_mask(char *hexmask, unsigned char **result, size_t *result_len,
143 		   unsigned char **mask);
144 unsigned char *parse_hex(char *hex, size_t *outlen);
145 
146 int parse_keys(struct nl_msg *msg, char **argv, int argc);
147 
148 void print_ht_mcs(const __u8 *mcs);
149 void print_ampdu_length(__u8 exponent);
150 void print_ampdu_spacing(__u8 spacing);
151 void print_ht_capability(__u16 cap);
152 void print_vht_info(__u32 capa, const __u8 *mcs);
153 
154 char *channel_width_name(enum nl80211_chan_width width);
155 const char *iftype_name(enum nl80211_iftype iftype);
156 const char *command_name(enum nl80211_commands cmd);
157 int ieee80211_channel_to_frequency(int chan, enum nl80211_band band);
158 int ieee80211_frequency_to_channel(int freq);
159 
160 void print_ssid_escaped(const uint8_t len, const uint8_t *data);
161 
162 int nl_get_multicast_id(struct nl_sock *sock, const char *family, const char *group);
163 
164 char *reg_initiator_to_string(__u8 initiator);
165 
166 const char *get_reason_str(uint16_t reason);
167 const char *get_status_str(uint16_t status);
168 
169 enum print_ie_type {
170 	PRINT_SCAN,
171 	PRINT_LINK,
172 };
173 
174 #define BIT(x) (1ULL<<(x))
175 
176 void print_ies(unsigned char *ie, int ielen, bool unknown,
177 	       enum print_ie_type ptype);
178 
179 void parse_bitrate(struct nlattr *bitrate_attr, char *buf, int buflen);
180 void iw_hexdump(const char *prefix, const __u8 *data, size_t len);
181 
182 DECLARE_SECTION(set);
183 DECLARE_SECTION(get);
184 
185 #endif /* __IW_H */
186