Yarilo  v0.9
WPA2 Decrypter & Packet Analyzer
net_card_manager.h
Go to the documentation of this file.
1 #ifndef SNIFF_NET_CARD_MANAGER
2 #define SNIFF_NET_CARD_MANAGER
3 
4 #include "netlink/attr.h"
5 #include "netlink/handlers.h"
6 #include "netlink/netlink.h"
7 #include <linux/nl80211.h>
8 #include <memory>
9 #include <optional>
10 #include <set>
11 #include <spdlog/logger.h>
12 #include <spdlog/sinks/stdout_color_sinks.h>
13 #include <spdlog/spdlog.h>
14 #include <string>
15 
16 namespace yarilo {
17 
23 public:
29  NetlinkCallback(nl_sock *sock) { this->sock = sock; };
30 
37  void attach(nl_recvmsg_msg_cb_t func, void *arg);
38 
43  int wait();
44 
45 private:
46  static int finish(nl_msg *msg, void *arg);
47  static int error(sockaddr_nl *nla, nlmsgerr *err, void *arg);
48  static int ack(nl_msg *msg, void *arg);
49 
50  nl_sock *sock = nullptr;
51  nl_cb *callback = nullptr;
52  int result = 1;
53 };
54 
59 public:
60  enum ChannelModes {
61  NO_HT, // Channel does not support High Throughput (HT) mode.
62  HT20, // Channel does support HT mode with a channel width of 20 MHz.
63  HT40MINUS, // Channel does support HT mode with a channel width of 40 MHz,
64  // where the secondary channel is below the primary channel.
65  HT40PLUS, // Channel does support HT mode with a channel width of 40 MHz,
66  // where the secondary channel is above the primary channel.
67  VHT80, // Channel does support Very High Throughput (VHT) mode with a
68  // channel width of 80 MHz
69  VHT80P80, // Channel does support Very High Throughput (VHT) mode with a
70  // channel width of 80 MHz and also supports an additional 80 MHz
71  // channel (80+80 MHz)
72  VHT160 // Channel does support VHT mode with a channel width of 160 MHz
73  };
74 
76 
80  struct phy_info {
81  std::string ifname; // Interface name
82  std::set<uint32_t> frequencies; // Supported frequencies
83  bool can_check_fcs; // Can it check the Frame Check Sequence
84  int channel_opts; // All available channel options, see ChannelModes
85  int can_monitor; // Supports monitor mode
86 
87  bool operator<(const phy_info &other) const {
88  return ifname < other.ifname;
89  }
90  };
91 
95  struct iface_state {
96  int type; // (virtual) interface type, see nl80211_iftype
97  int phy_idx; // Physical index
98  int logic_idx; // Logical index
99  int freq; // Current working frequency
100  ChannelModes chan_type; // Current channel type
101  int center_freq1; // Primary center frequency
102  int center_freq2; // Secondary center frequency in cases of bonded channels.
103  FcsState fcs_state; // Validation state of the Frame Check Sequence
104  };
105 
109  NetCardManager();
110 
115  bool connect();
116 
120  void disconnect();
121 
126  static std::set<std::string> net_interfaces();
127 
132  std::set<std::string> phy_interfaces() const;
133 
141  std::optional<phy_info> phy_details(int phy_idx) const;
142 
149  std::optional<iface_state> net_iface_details(const std::string &ifname) const;
150 
162  bool set_phy_channel(int phy_idx, int chan) const;
163 
169  static int freq_to_chan(int freq);
170 
176  static int chan_to_freq(int chan);
177 
179  if (sock)
180  nl_socket_free(this->sock);
181  }
182 
183 private:
184  static int phy_interfaces_callback(nl_msg *msg, void *arg);
185  static int phy_details_callback(nl_msg *msg, void *arg);
186  static int net_iface_details_callback(nl_msg *msg, void *arg);
187 
188  std::shared_ptr<spdlog::logger> logger;
189  nl_sock *sock = nullptr;
190  int sock_id = -1;
191 };
192 
193 } // namespace yarilo
194 
195 #endif // SNIFF_NET_CARD_MANAGER
Manager for network card information gathering and state control.
Definition: net_card_manager.h:58
void disconnect()
Definition: net_card_manager.cpp:88
bool set_phy_channel(int phy_idx, int chan) const
Definition: net_card_manager.cpp:150
std::optional< phy_info > phy_details(int phy_idx) const
Definition: net_card_manager.cpp:110
FcsState
Definition: net_card_manager.h:75
@ FCS_INVALID
Definition: net_card_manager.h:75
@ FCS_ALL
Definition: net_card_manager.h:75
@ FCS_VALID
Definition: net_card_manager.h:75
std::set< std::string > phy_interfaces() const
Definition: net_card_manager.cpp:97
bool connect()
Definition: net_card_manager.cpp:61
NetCardManager()
Definition: net_card_manager.cpp:59
ChannelModes
Definition: net_card_manager.h:60
@ VHT80
Definition: net_card_manager.h:67
@ VHT80P80
Definition: net_card_manager.h:69
@ HT40MINUS
Definition: net_card_manager.h:63
@ HT20
Definition: net_card_manager.h:62
@ VHT160
Definition: net_card_manager.h:72
@ NO_HT
Definition: net_card_manager.h:61
@ HT40PLUS
Definition: net_card_manager.h:65
std::optional< iface_state > net_iface_details(const std::string &ifname) const
Definition: net_card_manager.cpp:129
~NetCardManager()
Definition: net_card_manager.h:178
static std::set< std::string > net_interfaces()
Definition: net_card_manager.cpp:90
static int freq_to_chan(int freq)
Definition: net_card_manager.cpp:170
static int chan_to_freq(int chan)
Definition: net_card_manager.cpp:174
Netlink socket family callback handler for asynchronous communication.
Definition: net_card_manager.h:22
int wait()
Definition: net_card_manager.cpp:35
NetlinkCallback(nl_sock *sock)
Definition: net_card_manager.h:29
void attach(nl_recvmsg_msg_cb_t func, void *arg)
Definition: net_card_manager.cpp:23
Definition: access_point.cpp:22
Logical network interface (e.g. wlan0) data.
Definition: net_card_manager.h:95
ChannelModes chan_type
Definition: net_card_manager.h:100
int type
Definition: net_card_manager.h:96
int center_freq1
Definition: net_card_manager.h:101
FcsState fcs_state
Definition: net_card_manager.h:103
int freq
Definition: net_card_manager.h:99
int logic_idx
Definition: net_card_manager.h:98
int center_freq2
Definition: net_card_manager.h:102
int phy_idx
Definition: net_card_manager.h:97
Physical network interface (e.g. phy0) capability info.
Definition: net_card_manager.h:80
int can_monitor
Definition: net_card_manager.h:85
bool can_check_fcs
Definition: net_card_manager.h:83
int channel_opts
Definition: net_card_manager.h:84
std::set< uint32_t > frequencies
Definition: net_card_manager.h:82
std::string ifname
Definition: net_card_manager.h:81
bool operator<(const phy_info &other) const
Definition: net_card_manager.h:87