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