From ea3a93b1bc42a38c01b2a12928707912b37e2da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Arreola=20Rodr=C3=ADguez?= Date: Sat, 18 Jul 2020 21:54:39 -0500 Subject: [PATCH] =?UTF-8?q?Agrego=20primer=20prueba=20de=20interfaces=20in?= =?UTF-8?q?al=C3=A1mbricas.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- INSTALL | 316 +++++++++++++-------------- configure.ac | 4 + src/Makefile.am | 8 +- src/common.h | 56 ++++- src/interfaces.c | 5 + src/ip-address.h | 1 + src/main.c | 3 + src/netlink-events.c | 45 ++-- src/netlink-events.h | 2 + src/wireless_bss.c | 184 ++++++++++++++++ src/wireless_bss.h | 45 ++++ src/wireless_if.c | 508 +++++++++++++++++++++++++++++++++++++++++++ src/wireless_if.h | 48 ++++ 13 files changed, 1044 insertions(+), 181 deletions(-) create mode 100644 src/wireless_bss.c create mode 100644 src/wireless_bss.h create mode 100644 src/wireless_if.c create mode 100644 src/wireless_if.h diff --git a/INSTALL b/INSTALL index 2099840..8865734 100644 --- a/INSTALL +++ b/INSTALL @@ -1,8 +1,8 @@ Installation Instructions ************************* -Copyright (C) 1994-1996, 1999-2002, 2004-2013 Free Software Foundation, -Inc. + Copyright (C) 1994-1996, 1999-2002, 2004-2016 Free Software +Foundation, Inc. Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright @@ -12,97 +12,96 @@ without warranty of any kind. Basic Installation ================== - Briefly, the shell command `./configure && make && make install' + Briefly, the shell command './configure && make && make install' should configure, build, and install this package. The following -more-detailed instructions are generic; see the `README' file for +more-detailed instructions are generic; see the 'README' file for instructions specific to this package. Some packages provide this -`INSTALL' file but do not implement all of the features documented +'INSTALL' file but do not implement all of the features documented below. The lack of an optional feature in a given package is not necessarily a bug. More recommendations for GNU packages can be found in *note Makefile Conventions: (standards)Makefile Conventions. - The `configure' shell script attempts to guess correct values for + The 'configure' shell script attempts to guess correct values for various system-dependent variables used during compilation. It uses -those values to create a `Makefile' in each directory of the package. -It may also create one or more `.h' files containing system-dependent -definitions. Finally, it creates a shell script `config.status' that +those values to create a 'Makefile' in each directory of the package. +It may also create one or more '.h' files containing system-dependent +definitions. Finally, it creates a shell script 'config.status' that you can run in the future to recreate the current configuration, and a -file `config.log' containing compiler output (useful mainly for -debugging `configure'). +file 'config.log' containing compiler output (useful mainly for +debugging 'configure'). - It can also use an optional file (typically called `config.cache' -and enabled with `--cache-file=config.cache' or simply `-C') that saves -the results of its tests to speed up reconfiguring. Caching is -disabled by default to prevent problems with accidental use of stale -cache files. + It can also use an optional file (typically called 'config.cache' and +enabled with '--cache-file=config.cache' or simply '-C') that saves the +results of its tests to speed up reconfiguring. Caching is disabled by +default to prevent problems with accidental use of stale cache files. If you need to do unusual things to compile the package, please try -to figure out how `configure' could check whether to do them, and mail -diffs or instructions to the address given in the `README' so they can +to figure out how 'configure' could check whether to do them, and mail +diffs or instructions to the address given in the 'README' so they can be considered for the next release. If you are using the cache, and at -some point `config.cache' contains results you don't want to keep, you +some point 'config.cache' contains results you don't want to keep, you may remove or edit it. - The file `configure.ac' (or `configure.in') is used to create -`configure' by a program called `autoconf'. You need `configure.ac' if -you want to change it or regenerate `configure' using a newer version -of `autoconf'. + The file 'configure.ac' (or 'configure.in') is used to create +'configure' by a program called 'autoconf'. You need 'configure.ac' if +you want to change it or regenerate 'configure' using a newer version of +'autoconf'. The simplest way to compile this package is: - 1. `cd' to the directory containing the package's source code and type - `./configure' to configure the package for your system. + 1. 'cd' to the directory containing the package's source code and type + './configure' to configure the package for your system. - Running `configure' might take a while. While running, it prints + Running 'configure' might take a while. While running, it prints some messages telling which features it is checking for. - 2. Type `make' to compile the package. + 2. Type 'make' to compile the package. - 3. Optionally, type `make check' to run any self-tests that come with + 3. Optionally, type 'make check' to run any self-tests that come with the package, generally using the just-built uninstalled binaries. - 4. Type `make install' to install the programs and any data files and + 4. Type 'make install' to install the programs and any data files and documentation. When installing into a prefix owned by root, it is recommended that the package be configured and built as a regular - user, and only the `make install' phase executed with root + user, and only the 'make install' phase executed with root privileges. - 5. Optionally, type `make installcheck' to repeat any self-tests, but + 5. Optionally, type 'make installcheck' to repeat any self-tests, but this time using the binaries in their final installed location. This target does not install anything. Running this target as a - regular user, particularly if the prior `make install' required + regular user, particularly if the prior 'make install' required root privileges, verifies that the installation completed correctly. 6. You can remove the program binaries and object files from the - source code directory by typing `make clean'. To also remove the - files that `configure' created (so you can compile the package for - a different kind of computer), type `make distclean'. There is - also a `make maintainer-clean' target, but that is intended mainly + source code directory by typing 'make clean'. To also remove the + files that 'configure' created (so you can compile the package for + a different kind of computer), type 'make distclean'. There is + also a 'make maintainer-clean' target, but that is intended mainly for the package's developers. If you use it, you may have to get all sorts of other programs in order to regenerate files that came with the distribution. - 7. Often, you can also type `make uninstall' to remove the installed + 7. Often, you can also type 'make uninstall' to remove the installed files again. In practice, not all packages have tested that uninstallation works correctly, even though it is required by the GNU Coding Standards. - 8. Some packages, particularly those that use Automake, provide `make + 8. Some packages, particularly those that use Automake, provide 'make distcheck', which can by used by developers to test that all other - targets like `make install' and `make uninstall' work correctly. + targets like 'make install' and 'make uninstall' work correctly. This target is generally not run by end users. Compilers and Options ===================== Some systems require unusual options for compilation or linking that -the `configure' script does not know about. Run `./configure --help' +the 'configure' script does not know about. Run './configure --help' for details on some of the pertinent environment variables. - You can give `configure' initial values for configuration parameters -by setting variables in the command line or in the environment. Here -is an example: + You can give 'configure' initial values for configuration parameters +by setting variables in the command line or in the environment. Here is +an example: ./configure CC=c99 CFLAGS=-g LIBS=-lposix @@ -113,21 +112,21 @@ Compiling For Multiple Architectures You can compile the package for more than one kind of computer at the same time, by placing the object files for each architecture in their -own directory. To do this, you can use GNU `make'. `cd' to the +own directory. To do this, you can use GNU 'make'. 'cd' to the directory where you want the object files and executables to go and run -the `configure' script. `configure' automatically checks for the -source code in the directory that `configure' is in and in `..'. This -is known as a "VPATH" build. +the 'configure' script. 'configure' automatically checks for the source +code in the directory that 'configure' is in and in '..'. This is known +as a "VPATH" build. - With a non-GNU `make', it is safer to compile the package for one + With a non-GNU 'make', it is safer to compile the package for one architecture at a time in the source code directory. After you have -installed the package for one architecture, use `make distclean' before +installed the package for one architecture, use 'make distclean' before reconfiguring for another architecture. On MacOS X 10.5 and later systems, you can create libraries and executables that work on multiple system types--known as "fat" or -"universal" binaries--by specifying multiple `-arch' options to the -compiler but only a single `-arch' option to the preprocessor. Like +"universal" binaries--by specifying multiple '-arch' options to the +compiler but only a single '-arch' option to the preprocessor. Like this: ./configure CC="gcc -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ @@ -136,105 +135,104 @@ this: This is not guaranteed to produce working output in all cases, you may have to build one architecture at a time and combine the results -using the `lipo' tool if you have problems. +using the 'lipo' tool if you have problems. Installation Names ================== - By default, `make install' installs the package's commands under -`/usr/local/bin', include files under `/usr/local/include', etc. You -can specify an installation prefix other than `/usr/local' by giving -`configure' the option `--prefix=PREFIX', where PREFIX must be an + By default, 'make install' installs the package's commands under +'/usr/local/bin', include files under '/usr/local/include', etc. You +can specify an installation prefix other than '/usr/local' by giving +'configure' the option '--prefix=PREFIX', where PREFIX must be an absolute file name. You can specify separate installation prefixes for architecture-specific files and architecture-independent files. If you -pass the option `--exec-prefix=PREFIX' to `configure', the package uses +pass the option '--exec-prefix=PREFIX' to 'configure', the package uses PREFIX as the prefix for installing programs and libraries. Documentation and other data files still use the regular prefix. In addition, if you use an unusual directory layout you can give -options like `--bindir=DIR' to specify different values for particular -kinds of files. Run `configure --help' for a list of the directories -you can set and what kinds of files go in them. In general, the -default for these options is expressed in terms of `${prefix}', so that -specifying just `--prefix' will affect all of the other directory +options like '--bindir=DIR' to specify different values for particular +kinds of files. Run 'configure --help' for a list of the directories +you can set and what kinds of files go in them. In general, the default +for these options is expressed in terms of '${prefix}', so that +specifying just '--prefix' will affect all of the other directory specifications that were not explicitly provided. The most portable way to affect installation locations is to pass the -correct locations to `configure'; however, many packages provide one or +correct locations to 'configure'; however, many packages provide one or both of the following shortcuts of passing variable assignments to the -`make install' command line to change installation locations without +'make install' command line to change installation locations without having to reconfigure or recompile. The first method involves providing an override variable for each -affected directory. For example, `make install +affected directory. For example, 'make install prefix=/alternate/directory' will choose an alternate location for all directory configuration variables that were expressed in terms of -`${prefix}'. Any directories that were specified during `configure', -but not in terms of `${prefix}', must each be overridden at install -time for the entire installation to be relocated. The approach of -makefile variable overrides for each directory variable is required by -the GNU Coding Standards, and ideally causes no recompilation. -However, some platforms have known limitations with the semantics of -shared libraries that end up requiring recompilation when using this -method, particularly noticeable in packages that use GNU Libtool. +'${prefix}'. Any directories that were specified during 'configure', +but not in terms of '${prefix}', must each be overridden at install time +for the entire installation to be relocated. The approach of makefile +variable overrides for each directory variable is required by the GNU +Coding Standards, and ideally causes no recompilation. However, some +platforms have known limitations with the semantics of shared libraries +that end up requiring recompilation when using this method, particularly +noticeable in packages that use GNU Libtool. - The second method involves providing the `DESTDIR' variable. For -example, `make install DESTDIR=/alternate/directory' will prepend -`/alternate/directory' before all installation names. The approach of -`DESTDIR' overrides is not required by the GNU Coding Standards, and + The second method involves providing the 'DESTDIR' variable. For +example, 'make install DESTDIR=/alternate/directory' will prepend +'/alternate/directory' before all installation names. The approach of +'DESTDIR' overrides is not required by the GNU Coding Standards, and does not work on platforms that have drive letters. On the other hand, it does better at avoiding recompilation issues, and works well even -when some directory options were not specified in terms of `${prefix}' -at `configure' time. +when some directory options were not specified in terms of '${prefix}' +at 'configure' time. Optional Features ================= If the package supports it, you can cause programs to be installed -with an extra prefix or suffix on their names by giving `configure' the -option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. +with an extra prefix or suffix on their names by giving 'configure' the +option '--program-prefix=PREFIX' or '--program-suffix=SUFFIX'. - Some packages pay attention to `--enable-FEATURE' options to -`configure', where FEATURE indicates an optional part of the package. -They may also pay attention to `--with-PACKAGE' options, where PACKAGE -is something like `gnu-as' or `x' (for the X Window System). The -`README' should mention any `--enable-' and `--with-' options that the + Some packages pay attention to '--enable-FEATURE' options to +'configure', where FEATURE indicates an optional part of the package. +They may also pay attention to '--with-PACKAGE' options, where PACKAGE +is something like 'gnu-as' or 'x' (for the X Window System). The +'README' should mention any '--enable-' and '--with-' options that the package recognizes. - For packages that use the X Window System, `configure' can usually + For packages that use the X Window System, 'configure' can usually find the X include and library files automatically, but if it doesn't, -you can use the `configure' options `--x-includes=DIR' and -`--x-libraries=DIR' to specify their locations. +you can use the 'configure' options '--x-includes=DIR' and +'--x-libraries=DIR' to specify their locations. Some packages offer the ability to configure how verbose the -execution of `make' will be. For these packages, running `./configure +execution of 'make' will be. For these packages, running './configure --enable-silent-rules' sets the default to minimal output, which can be -overridden with `make V=1'; while running `./configure +overridden with 'make V=1'; while running './configure --disable-silent-rules' sets the default to verbose, which can be -overridden with `make V=0'. +overridden with 'make V=0'. Particular systems ================== - On HP-UX, the default C compiler is not ANSI C compatible. If GNU -CC is not installed, it is recommended to use the following options in + On HP-UX, the default C compiler is not ANSI C compatible. If GNU CC +is not installed, it is recommended to use the following options in order to use an ANSI C compiler: ./configure CC="cc -Ae -D_XOPEN_SOURCE=500" and if that doesn't work, install pre-built binaries of GCC for HP-UX. - HP-UX `make' updates targets which have the same time stamps as -their prerequisites, which makes it generally unusable when shipped -generated files such as `configure' are involved. Use GNU `make' -instead. + HP-UX 'make' updates targets which have the same time stamps as their +prerequisites, which makes it generally unusable when shipped generated +files such as 'configure' are involved. Use GNU 'make' instead. On OSF/1 a.k.a. Tru64, some versions of the default C compiler cannot -parse its `' header file. The option `-nodtk' can be used as -a workaround. If GNU CC is not installed, it is therefore recommended -to try +parse its '' header file. The option '-nodtk' can be used as a +workaround. If GNU CC is not installed, it is therefore recommended to +try ./configure CC="cc" @@ -242,26 +240,26 @@ and if that doesn't work, try ./configure CC="cc -nodtk" - On Solaris, don't put `/usr/ucb' early in your `PATH'. This + On Solaris, don't put '/usr/ucb' early in your 'PATH'. This directory contains several dysfunctional programs; working variants of -these programs are available in `/usr/bin'. So, if you need `/usr/ucb' -in your `PATH', put it _after_ `/usr/bin'. +these programs are available in '/usr/bin'. So, if you need '/usr/ucb' +in your 'PATH', put it _after_ '/usr/bin'. - On Haiku, software installed for all users goes in `/boot/common', -not `/usr/local'. It is recommended to use the following options: + On Haiku, software installed for all users goes in '/boot/common', +not '/usr/local'. It is recommended to use the following options: ./configure --prefix=/boot/common Specifying the System Type ========================== - There may be some features `configure' cannot figure out + There may be some features 'configure' cannot figure out automatically, but needs to determine by the type of machine the package will run on. Usually, assuming the package is built to be run on the -_same_ architectures, `configure' can figure that out, but if it prints +_same_ architectures, 'configure' can figure that out, but if it prints a message saying it cannot guess the machine type, give it the -`--build=TYPE' option. TYPE can either be a short name for the system -type, such as `sun4', or a canonical name which has the form: +'--build=TYPE' option. TYPE can either be a short name for the system +type, such as 'sun4', or a canonical name which has the form: CPU-COMPANY-SYSTEM @@ -270,101 +268,101 @@ where SYSTEM can have one of these forms: OS KERNEL-OS - See the file `config.sub' for the possible values of each field. If -`config.sub' isn't included in this package, then this package doesn't + See the file 'config.sub' for the possible values of each field. If +'config.sub' isn't included in this package, then this package doesn't need to know the machine type. If you are _building_ compiler tools for cross-compiling, you should -use the option `--target=TYPE' to select the type of system they will +use the option '--target=TYPE' to select the type of system they will produce code for. If you want to _use_ a cross compiler, that generates code for a platform different from the build platform, you should specify the "host" platform (i.e., that on which the generated programs will -eventually be run) with `--host=TYPE'. +eventually be run) with '--host=TYPE'. Sharing Defaults ================ - If you want to set default values for `configure' scripts to share, -you can create a site shell script called `config.site' that gives -default values for variables like `CC', `cache_file', and `prefix'. -`configure' looks for `PREFIX/share/config.site' if it exists, then -`PREFIX/etc/config.site' if it exists. Or, you can set the -`CONFIG_SITE' environment variable to the location of the site script. -A warning: not all `configure' scripts look for a site script. + If you want to set default values for 'configure' scripts to share, +you can create a site shell script called 'config.site' that gives +default values for variables like 'CC', 'cache_file', and 'prefix'. +'configure' looks for 'PREFIX/share/config.site' if it exists, then +'PREFIX/etc/config.site' if it exists. Or, you can set the +'CONFIG_SITE' environment variable to the location of the site script. +A warning: not all 'configure' scripts look for a site script. Defining Variables ================== Variables not defined in a site shell script can be set in the -environment passed to `configure'. However, some packages may run +environment passed to 'configure'. However, some packages may run configure again during the build, and the customized values of these variables may be lost. In order to avoid this problem, you should set -them in the `configure' command line, using `VAR=value'. For example: +them in the 'configure' command line, using 'VAR=value'. For example: ./configure CC=/usr/local2/bin/gcc -causes the specified `gcc' to be used as the C compiler (unless it is +causes the specified 'gcc' to be used as the C compiler (unless it is overridden in the site shell script). -Unfortunately, this technique does not work for `CONFIG_SHELL' due to -an Autoconf limitation. Until the limitation is lifted, you can use -this workaround: +Unfortunately, this technique does not work for 'CONFIG_SHELL' due to an +Autoconf limitation. Until the limitation is lifted, you can use this +workaround: CONFIG_SHELL=/bin/bash ./configure CONFIG_SHELL=/bin/bash -`configure' Invocation +'configure' Invocation ====================== - `configure' recognizes the following options to control how it + 'configure' recognizes the following options to control how it operates. -`--help' -`-h' - Print a summary of all of the options to `configure', and exit. +'--help' +'-h' + Print a summary of all of the options to 'configure', and exit. -`--help=short' -`--help=recursive' +'--help=short' +'--help=recursive' Print a summary of the options unique to this package's - `configure', and exit. The `short' variant lists options used - only in the top level, while the `recursive' variant lists options - also present in any nested packages. + 'configure', and exit. The 'short' variant lists options used only + in the top level, while the 'recursive' variant lists options also + present in any nested packages. -`--version' -`-V' - Print the version of Autoconf used to generate the `configure' +'--version' +'-V' + Print the version of Autoconf used to generate the 'configure' script, and exit. -`--cache-file=FILE' +'--cache-file=FILE' Enable the cache: use and save the results of the tests in FILE, - traditionally `config.cache'. FILE defaults to `/dev/null' to + traditionally 'config.cache'. FILE defaults to '/dev/null' to disable caching. -`--config-cache' -`-C' - Alias for `--cache-file=config.cache'. +'--config-cache' +'-C' + Alias for '--cache-file=config.cache'. -`--quiet' -`--silent' -`-q' +'--quiet' +'--silent' +'-q' Do not print messages saying which checks are being made. To - suppress all normal output, redirect it to `/dev/null' (any error + suppress all normal output, redirect it to '/dev/null' (any error messages will still be shown). -`--srcdir=DIR' +'--srcdir=DIR' Look for the package's source code in directory DIR. Usually - `configure' can determine that directory automatically. + 'configure' can determine that directory automatically. -`--prefix=DIR' - Use DIR as the installation prefix. *note Installation Names:: - for more details, including other options available for fine-tuning - the installation locations. +'--prefix=DIR' + Use DIR as the installation prefix. *note Installation Names:: for + more details, including other options available for fine-tuning the + installation locations. -`--no-create' -`-n' +'--no-create' +'-n' Run the configure checks, but stop before creating any output files. -`configure' also accepts some other, not widely useful, options. Run -`configure --help' for more details. +'configure' also accepts some other, not widely useful, options. Run +'configure --help' for more details. diff --git a/configure.ac b/configure.ac index bf062b0..52556a2 100644 --- a/configure.ac +++ b/configure.ac @@ -45,6 +45,10 @@ AC_MSG_CHECKING([if you have libnl-3 installed on your system]) PKG_CHECK_EXISTS([libnl-3.0 >= LIBNL_VERSION], [AC_MSG_RESULT([yes])], [AC_MSG_FAILURE([libnl-3 not found in your system])]) PKG_CHECK_MODULES(LIBNL3, [libnl-3.0 >= $LIBNL_VERSION], [], []) +AC_MSG_CHECKING([if you have libnl-gen-3 installed on your system]) +PKG_CHECK_EXISTS([libnl-genl-3.0 >= LIBNL_VERSION], [AC_MSG_RESULT([yes])], [AC_MSG_FAILURE([libnl-gen-3 not found in your system])]) +PKG_CHECK_MODULES(LIBNLGEN3, [libnl-genl-3.0 >= $LIBNL_VERSION], [], []) + AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([ diff --git a/src/Makefile.am b/src/Makefile.am index 6fe1925..fe85544 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,12 +4,14 @@ bin_PROGRAMS = network-inador network_inador_SOURCES = main.c common.h \ netlink-events.c netlink-events.h \ interfaces.c interfaces.h \ - ip-address.c ip-address.h + ip-address.c ip-address.h \ + wireless_if.c wireless_if.h \ + wireless_bss.c wireless_bss.h #network_inador_CPPFLAGS = -DGAMEDATA_DIR=\"$(gamedatadir)/\" -DLOCALEDIR=\"$(localedir)\" $(AM_CPPFLAGS) network_inador_CPPFLAGS = -DLOCALEDIR=\"$(localedir)\" $(AM_CPPFLAGS) -network_inador_CFLAGS = $(GLIB_CFLAGS) $(LIBNL3_CFLAGS) $(AM_CFLAGS) -network_inador_LDADD = $(GLIB_LIBS) $(LIBNL3_LIBS) +network_inador_CFLAGS = $(GLIB_CFLAGS) $(LIBNL3_CFLAGS) $(LIBNLGEN3_CFLAGS) $(AM_CFLAGS) +network_inador_LDADD = $(GLIB_LIBS) $(LIBNL3_LIBS) $(LIBNLGEN3_LIBS) LDADD = $(LIBINTL) #SUBDIRS = client_test dhcpc diff --git a/src/common.h b/src/common.h index 0d4d689..36c6c85 100644 --- a/src/common.h +++ b/src/common.h @@ -24,6 +24,7 @@ #define __COMMON_H__ #include +#include #include #include @@ -52,6 +53,45 @@ typedef struct _IPAddr { unsigned char scope; } IPAddr; +#define SSID_MAX_LEN 32 + +typedef struct _WirelessBSS { + /** Number of counts without seeing this BSS */ + unsigned int scan_miss_count; + /** Index of the last scan update */ + unsigned int last_update_idx; + + /** BSSID */ + uint8_t bssid[ETHER_ADDR_LEN * 2 + 1]; + /** HESSID */ + //u8 hessid[ETHER_ADDR_LEN * 2 + 1]; + /** SSID */ + uint8_t ssid[SSID_MAX_LEN]; + /** Length of SSID */ + size_t ssid_len; + /** Frequency of the channel in MHz (e.g., 2412 = channel 1) */ + int freq; + /** Capability information field in host byte order */ + uint16_t caps; + /** Timestamp of last Beacon/Probe Response frame */ + uint64_t tsf; + /** Time of the last update (i.e., Beacon or Probe Response RX) */ + struct timespec last_update; +} WirelessBSS; + +typedef struct _WirelessInfo { + int phy; + uint32_t *freqs; + int num_freqs; + uint32_t caps; + gboolean can_scan; + gboolean can_scan_ssid; + gboolean supported; + + unsigned int bss_update_idx; + GList *aps; +} WirelessInfo; + typedef struct _Interface { char name[IFNAMSIZ]; int ifi_type; @@ -84,16 +124,26 @@ typedef struct _Interface { //DHCPStateInfo dhcp_info; /* Información wireless */ - //WirelessInfo *wireless; + WirelessInfo *wireless; } Interface; +/* Para vigilar eventos */ +typedef struct _netlink_event_pair { + struct nl_sock * nl_sock; + guint source; +} NetlinkEventPair; + typedef struct { GList *interfaces; //Routev4 *rtable_v4; struct nl_sock * nl_sock_route; - struct nl_sock * nl_sock_route_events; - guint route_events_source; + struct nl_sock * nl_sock_nl80211; + + NetlinkEventPair route_events; + + NetlinkEventPair nl80211_scan; + NetlinkEventPair nl80211_scan_results; } NetworkInadorHandle; #endif /* __COMMON_H__ */ diff --git a/src/interfaces.c b/src/interfaces.c index a3cab69..008d4b5 100644 --- a/src/interfaces.c +++ b/src/interfaces.c @@ -32,6 +32,7 @@ #include "common.h" #include "interfaces.h" #include "ip-address.h" +#include "wireless_if.h" static int _interfaces_receive_message_interface (struct nl_msg *msg, void *arg, int first_time); @@ -178,6 +179,10 @@ static int _interfaces_receive_message_interface (struct nl_msg *msg, void *arg, } } + if (was_new) { + wireless_interface_check (handle, iface); + } + return NL_SKIP; } diff --git a/src/ip-address.h b/src/ip-address.h index cd59df3..1a9fc31 100644 --- a/src/ip-address.h +++ b/src/ip-address.h @@ -29,6 +29,7 @@ #include "common.h" int ip_address_receive_message_newaddr (struct nl_msg *msg, void *arg); +int ip_address_receive_message_deladdr (struct nl_msg *msg, void *arg); void ip_address_init (NetworkInadorHandle *handle); int ip_address_add_ip (NetworkInadorHandle *handle, int index, IPAddr *addr); diff --git a/src/main.c b/src/main.c index d220858..eaeb1c8 100644 --- a/src/main.c +++ b/src/main.c @@ -46,6 +46,7 @@ static int sigterm_pipe_fds[2] = { -1, -1 }; static void _init_handle (NetworkInadorHandle *handle) { assert (handle != NULL); + memset (handle, 0, sizeof (NetworkInadorHandle)); handle->interfaces = NULL; } @@ -134,5 +135,7 @@ int main (int argc, char *argv[]) { /* Detener la llegada de eventos */ netlink_events_clear (&handle); + // nl_socket_free??? + return 0; } diff --git a/src/netlink-events.c b/src/netlink-events.c index 27f5229..3e7ea25 100644 --- a/src/netlink-events.c +++ b/src/netlink-events.c @@ -25,6 +25,8 @@ #include +#include + #include "common.h" #include "interfaces.h" #include "ip-address.h" @@ -53,47 +55,58 @@ static int _netlink_events_route_dispatcher (struct nl_msg *msg, void *arg) { } static gboolean _netlink_events_handle_read (GIOChannel *source, GIOCondition condition, gpointer data) { - NetworkInadorHandle *handle = (NetworkInadorHandle *) data; + struct nl_sock *socket = (struct nl_sock *) data; - nl_recvmsgs_default (handle->nl_sock_route_events); + nl_recvmsgs_default (socket); return TRUE; } -void netlink_events_setup (NetworkInadorHandle *handle) { +void netlink_events_create_pair (NetlinkEventPair *pair, int family) { struct nl_sock * sock_req; int fd; GIOChannel *channel; + if (pair == NULL) return; + sock_req = nl_socket_alloc (); - if (nl_connect (sock_req, NETLINK_ROUTE) != 0) { + if (nl_connect (sock_req, family) != 0) { perror ("Falló conectar netlink socket para eventos\n"); return; } nl_socket_set_nonblocking (sock_req); - nl_socket_add_memberships (sock_req, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR, RTNLGRP_IPV6_IFINFO, 0); nl_socket_disable_seq_check (sock_req); - nl_socket_modify_cb (sock_req, NL_CB_VALID, NL_CB_CUSTOM, _netlink_events_route_dispatcher, handle); fd = nl_socket_get_fd (sock_req); channel = g_io_channel_unix_new (fd); - handle->route_events_source = g_io_add_watch (channel, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, _netlink_events_handle_read, handle); - handle->nl_sock_route_events = sock_req; + pair->source = g_io_add_watch (channel, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, _netlink_events_handle_read, sock_req); + pair->nl_sock = sock_req; + g_io_channel_unref (channel); +} + +void netlink_events_clear_pair (NetlinkEventPair *pair) { + g_source_remove (pair->source); + + pair->source = 0; + + /* Cerrar el socket */ + nl_close (pair->nl_sock); + pair->nl_sock = NULL; +} + +void netlink_events_setup (NetworkInadorHandle *handle) { + netlink_events_create_pair (&handle->route_events, NETLINK_ROUTE); + + nl_socket_add_memberships (handle->route_events.nl_sock, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR, RTNLGRP_IPV6_IFINFO, 0); + nl_socket_modify_cb (handle->route_events.nl_sock, NL_CB_VALID, NL_CB_CUSTOM, _netlink_events_route_dispatcher, handle); } void netlink_events_clear (NetworkInadorHandle *handle) { /* Primero, detener los eventos del source watch */ - - g_source_remove (handle->route_events_source); - - handle->route_events_source = 0; - /* Cerrar el socket */ - - nl_close (handle->nl_sock_route_events); - handle->nl_sock_route_events = NULL; + netlink_events_clear_pair (&handle->route_events); } diff --git a/src/netlink-events.h b/src/netlink-events.h index 68bace6..ad6cd40 100644 --- a/src/netlink-events.h +++ b/src/netlink-events.h @@ -25,6 +25,8 @@ #include "common.h" +void netlink_events_create_pair (NetlinkEventPair *pair, int family); +void netlink_events_clear_pair (NetlinkEventPair *pair); void netlink_events_setup (NetworkInadorHandle *handle); void netlink_events_clear (NetworkInadorHandle *handle); diff --git a/src/wireless_bss.c b/src/wireless_bss.c new file mode 100644 index 0000000..d529726 --- /dev/null +++ b/src/wireless_bss.c @@ -0,0 +1,184 @@ +/* + * wireless_bss.c + * This file is part of Network-inador + * + * Copyright (C) 2020 - Félix Arreola Rodríguez + * + * Network-inador is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Network-inador is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Network-inador; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include "common.h" +#include "interfaces.h" +#include "netlink-events.h" +#include "wireless_if.h" +#include "wireless_bss.h" + +#define DEFAULT_BSS_EXPIRATION_SCAN_COUNT 2 + +int wireless_bss_finish_scan (struct nl_msg *msg, void *arg) { + #if 0 + GList *g; + WirelessBSS *bss; + printf ("Scan finish handler\n"); + + g = + if (bss->last_update_idx < wpa_s->bss_update_idx) + bss->scan_miss_count++; + #endif +} + +// Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c +static void _wireless_bss_find_ssid (uint8_t *ies, uint32_t ies_len, uint8_t **ssid, uint32_t *ssid_len) { +#define WLAN_EID_SSID 0 + *ssid = NULL; + *ssid_len = 0; + + while (ies_len > 2 && ies[0] != WLAN_EID_SSID) { + ies_len -= ies[1] + 2; + ies += ies[1] + 2; + } + if (ies_len < 2) return; + if (ies_len < (uint32_t)(2 + ies[1])) return; + + *ssid_len = ies[1]; + *ssid = ies + 2; +} + +static WirelessBSS * _wireless_bss_get (Interface *iface, const uint8_t *bssid, const uint8_t *ssid, int ssid_len) { + GList *g; + WirelessBSS *bss; + if (iface->is_wireless == 0) return NULL; + + for (g = iface->wireless->aps; g != NULL; g = g->next) { + bss = (WirelessBSS *) g->data; + + if (memcmp (bss->bssid, bssid, ETHER_ADDR_LEN) == 0 && + bss->ssid_len == ssid_len && + memcmp (bss->ssid, ssid, ssid_len) == 0) { + return bss; + } + } + + return NULL; +} + +static WirelessBSS * _wireless_bss_add_bss (Interface *iface, WirelessBSS *bss) { + WirelessBSS *new; + + new = (WirelessBSS *) malloc (sizeof (WirelessBSS)); + + if (new == NULL) return NULL; + + memcpy (new, bss, sizeof (WirelessBSS)); + + new->last_update_idx = iface->wireless->bss_update_idx; + new->scan_miss_count = 0; + + iface->wireless->aps = g_list_append (iface->wireless->aps, new); +} + +static void _wireless_bss_update_bss (Interface *iface, WirelessBSS *bss, WirelessBSS *updated) { + bss->last_update_idx = iface->wireless->bss_update_idx; + bss->scan_miss_count = 0; + + /* Actualizar este bss con los nuevos datos */ + bss->freq = updated->freq; +} + +int wireless_bss_parse_station_scan (struct nl_msg *msg, void *arg) { + NetworkInadorHandle *handle = (NetworkInadorHandle *) arg; + struct nlmsgerr *l_err; + struct nlmsghdr *reply; + struct genlmsghdr *gnlh; + int remaining, remaining2; + struct nlattr *attr, *bss_attr; + Interface *iface = NULL; + + WirelessBSS bss, *search_bss; + + reply = nlmsg_hdr (msg); + + if (reply->nlmsg_type == NLMSG_ERROR) { + l_err = nlmsg_data (reply); + + return NL_SKIP; + } + + printf ("Estación recibida por escaneo\n"); + gnlh = nlmsg_data (reply); + memset (&bss, 0, sizeof (bss)); + + nlmsg_for_each_attr(attr, reply, sizeof (struct genlmsghdr), remaining) { + //printf ("Estacion nueva. Attr = %i\n", nla_type (attr)); + if (nla_type (attr) == NL80211_ATTR_IFINDEX) { + iface = _interfaces_locate_by_index (handle->interfaces, nla_get_u32 (attr)); + //printf ("Estación nueva en la interfaz %i\n", nla_get_u32 (attr)); + } else if (nla_type (attr) == NL80211_ATTR_BSS) { + //printf ("Información BSS:\n"); + + remaining2 = nla_len (attr); + bss_attr = nla_data (attr); + nla_for_each_nested(bss_attr, attr, remaining2) { + printf ("-> BSS: %i\n", nla_type (bss_attr)); + if (nla_type (bss_attr) == NL80211_BSS_BSSID) { + char *mac = nla_data (bss_attr); + printf ("MAC access point: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + memcpy (bss.bssid, nla_data (bss_attr), ETHER_ADDR_LEN); + } else if (nla_type (bss_attr) == NL80211_BSS_FREQUENCY) { + printf ("Freq: %i\n", nla_get_u32 (bss_attr)); + bss.freq = nla_get_u32 (bss_attr); + } else if (nla_type (bss_attr) == NL80211_BSS_INFORMATION_ELEMENTS) { + char *ssid; + int ssid_len; + _wireless_bss_find_ssid (nla_data (bss_attr), nla_len (bss_attr), (uint8_t **) &ssid, &ssid_len); + + if (ssid != NULL) { + printf ("Essid: «%.*s»\n", ssid_len, ssid); + memcpy (bss.ssid, ssid, ssid_len); + } + } + } + } + } + + if (iface == NULL) return NL_SKIP; + + search_bss = _wireless_bss_get (iface, bss.bssid, bss.ssid, bss.ssid_len); + + if (search_bss == NULL) { + /* Agregar como nuevo */ + search_bss = _wireless_bss_add_bss (iface, &bss); + } else { + /* Actualizar este bss */ + _wireless_bss_update_bss (iface, search_bss, &bss); + } + + return NL_SKIP; +} + diff --git a/src/wireless_bss.h b/src/wireless_bss.h new file mode 100644 index 0000000..52ffdd5 --- /dev/null +++ b/src/wireless_bss.h @@ -0,0 +1,45 @@ +/* + * wireless_bss.h + * This file is part of Network-inador + * + * Copyright (C) 2020 - Félix Arreola Rodríguez + * + * Network-inador is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Network-inador is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Network-inador; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#ifndef __WIRELESS_BSS__ +#define __WIRELESS_BSS__ + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include "common.h" + +int wireless_bss_parse_station_scan (struct nl_msg *msg, void *arg); +int wireless_bss_finish_scan (struct nl_msg *msg, void *arg); + +#endif /* __WIRELESS_BSS__ */ + diff --git a/src/wireless_if.c b/src/wireless_if.c new file mode 100644 index 0000000..18aca24 --- /dev/null +++ b/src/wireless_if.c @@ -0,0 +1,508 @@ +/* + * wireless_if.c + * This file is part of Network-inador + * + * Copyright (C) 2020 - Félix Arreola Rodríguez + * + * Network-inador is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Network-inador is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Network-inador; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include "common.h" +#include "interfaces.h" +#include "netlink-events.h" +#include "wireless_if.h" +#include "wireless_bss.h" + +uint16_t wireless_if_nl80211_id = 0; +static uint16_t wireless_if_nl80211_scan_multicast_group_id = 0; + +int wireless_interface_new_scan_results (NetworkInadorHandle *handle, struct nl_msg *msg); +void wireless_interface_get_scan (NetworkInadorHandle *handle, Interface *iface); +void wireless_interface_trigger_scan (NetworkInadorHandle *handle, Interface *iface); + +struct _wireless_iface_is_wifi { + int error; + int is_wifi; + int index; + WirelessInfo *info; +}; + +static void _wireless_if_process_bands (struct nlattr *list_bands, WirelessInfo *info) { + int remaining, remaining2, remaining3, remaining4; + struct nlattr *band, *band_attr, *freq, *freq_attr; + int freq_index; + + printf ("%s: El info es: %p\n", __func__, info); + + info->num_freqs = 0; + printf ("La lista de bandas tiene por tamaño: %i\n", nla_len (list_bands)); + nla_for_each_nested (band, list_bands, remaining) { + + nla_for_each_nested (band_attr, band, remaining2) { + if (nla_type (band_attr) != NL80211_BAND_ATTR_FREQS) continue; + nla_for_each_nested (freq, band_attr, remaining3) { + printf ("Dentro de BAND attr freqs, atributo es: %i (%i)\n", nla_type (freq), nla_len (freq)); + nla_for_each_nested (freq_attr, freq, remaining4) { + printf ("Dentro de FREQ attr, atributo es: %i (%i)\n", nla_type (freq_attr), nla_len (freq_attr)); + if (nla_type (freq_attr) != NL80211_FREQUENCY_ATTR_FREQ) continue; + info->num_freqs++; + } + } + } + } + + printf ("La supuesta cantidad de frecuencias es: %i\n", info->num_freqs); + info->freqs = (guint32 *) malloc (sizeof (guint32) * info->num_freqs); + freq_index = 0; + nla_for_each_nested (band, list_bands, remaining) { + + nla_for_each_nested (band_attr, band, remaining2) { + if (nla_type (band_attr) != NL80211_BAND_ATTR_FREQS) continue; + nla_for_each_nested (freq, band_attr, remaining3) { + nla_for_each_nested (freq_attr, freq, remaining4) { + if (nla_type (freq_attr) != NL80211_FREQUENCY_ATTR_FREQ) continue; + + info->freqs[freq_index] = nla_get_u32 (freq_attr); + + info->caps |= WIFI_DEVICE_CAP_FREQ_VALID; + + if (info->freqs[freq_index] > 2400 && info->freqs[freq_index] < 2500) { + info->caps |= WIFI_DEVICE_CAP_FREQ_2GHZ; + } + if (info->freqs[freq_index] > 4900 && info->freqs[freq_index] < 6000) { + info->caps |= WIFI_DEVICE_CAP_FREQ_5GHZ; + } + + printf ("Frecuencia: %i\n", info->freqs[freq_index]); + freq_index++; + } + } + } + } +} + +static int _wireless_if_cb_valid_is_wifi (struct nl_msg *msg, void *arg) { + struct _wireless_iface_is_wifi *is_wifi = (struct _wireless_iface_is_wifi *) arg; + + printf ("%s: Argumento extra: %p\n", __func__, arg); + + struct nlmsgerr *l_err; + struct nlmsghdr *reply; + struct genlmsghdr *gnlh; + struct nlattr *attr, *nest_attr; + int remaining, remaining2; + + reply = nlmsg_hdr (msg); + printf ("CB Valid is wifi\n"); + if (reply->nlmsg_type == NLMSG_ERROR) { + l_err = nlmsg_data (reply); + + is_wifi->error = l_err->error; + printf ("---> (type = %i, NLMSG_ERROR = %i) Error %i", reply->nlmsg_type, NLMSG_ERROR, is_wifi->error); + return NL_SKIP; + } + + if (reply->nlmsg_type != wireless_if_nl80211_id) { + return NL_SKIP; + } + gnlh = nlmsg_data (reply); + + printf ("---> type = nl80211_id, "); + printf ("CMD: %i\n", gnlh->cmd); + + if (gnlh->cmd != NL80211_CMD_NEW_WIPHY) { + /* Ignorar */ + return NL_SKIP; + } + + /* Como está la información de la interfaz, es una interfaz wifi */ + is_wifi->is_wifi = 1; + + nlmsg_for_each_attr(attr, reply, sizeof (struct genlmsghdr), remaining) { + printf ("Atributo de la interfaz wifi: %i\n", nla_type (attr)); + switch (nla_type (attr)) { + case NL80211_ATTR_WIPHY: + is_wifi->info->phy = nla_get_u32 (attr); + break; + case NL80211_ATTR_SUPPORTED_COMMANDS: + nla_for_each_nested (nest_attr, attr, remaining2) { + switch (nla_get_u32 (nest_attr)) { + case NL80211_CMD_TRIGGER_SCAN: + is_wifi->info->can_scan = TRUE; + break; + case NL80211_CMD_CONNECT: + case NL80211_CMD_AUTHENTICATE: + /* Only devices that support CONNECT or AUTH actually support + * 802.11, unlike say ipw2x00 (up to at least kernel 3.4) which + * has minimal info support, but no actual command support. + * This check mirrors what wpa_supplicant does to determine + * whether or not to use the nl80211 driver. + */ + is_wifi->info->supported = TRUE; + break; + default: + break; + } + } + break; + case NL80211_ATTR_WIPHY_BANDS: + printf ("Hay attr wiphy bands\n"); + _wireless_if_process_bands (attr, is_wifi->info); + break; + } + } + + return NL_SKIP; +} + +static int _wireless_if_wait_ack_or_error (struct nl_msg *msg, void *arg) { + int *ret = (int *) arg; + struct nlmsgerr *l_err; + struct nlmsghdr *reply; + + reply = nlmsg_hdr (msg); + printf ("--> _wireless_if_wait_ack_or_error"); + if (reply->nlmsg_type == NLMSG_ERROR) { + l_err = nlmsg_data (reply); + + *ret = l_err->error; + printf ("---> (type = %i, NLMSG_ERROR = %i) Error %i", reply->nlmsg_type, NLMSG_ERROR, *ret); + } else { + printf ("---> type = %i (nl802 = %i).", reply->nlmsg_type, wireless_if_nl80211_id); + } + printf ("\n"); + return NL_SKIP; +} + +static int _wireless_if_wait_error (struct sockaddr_nl *nla, struct nlmsgerr *l_err, void *arg) { + int *ret = (int *) arg; + + *ret = l_err->error; + printf ("--> _wireless_if_wait_error\n"); + return NL_SKIP; +} + +static int _wireless_if_valid (struct nl_msg *msg, void *arg) { + printf ("CB Valid\n"); + return _wireless_if_wait_ack_or_error (msg, arg); +} + +static int _wireless_if_invalid (struct nl_msg *msg, void *arg) { + printf ("CB INValid\n"); + return _wireless_if_wait_ack_or_error (msg, arg); +} + +static int _wireless_if_ack (struct nl_msg *msg, void *arg) { + printf ("CB ACK\n"); + return _wireless_if_wait_ack_or_error (msg, arg); +} + +#if 0 +gboolean wireless_interface_timer_trigger (gpointer data) { + NetworkInadorHandle *handle = (NetworkInadorHandle *) data; + Interface *iface; + + static int c = 0; + + printf ("Ejecutando Trigger SCAN\n"); + + iface = _interfaces_locate_by_index (handle->interfaces, 3); + if (iface == NULL) return FALSE; + wireless_interface_trigger_scan (handle, iface); + + if (c == 0) { + c++; + return TRUE; + } + return FALSE; +} +#endif + +static void wireless_interface_init_nl80211 (NetworkInadorHandle *handle) { + int family_id; + int mcid; + struct nl_sock * sock_req; + + sock_req = nl_socket_alloc (); + + if (nl_connect (sock_req, NETLINK_GENERIC) != 0) { + perror ("Falló conectar netlink socket GENERIC\n"); + + return; + } + + family_id = genl_ctrl_resolve (sock_req, "nl80211"); + + if (family_id < 0) { + nl_socket_free (sock_req); + return; + } + + mcid = genl_ctrl_resolve_grp (sock_req, "nl80211", "scan"); + + if (mcid >= 0) { + /* Solo si tenemos el id del grupo multicast crear el generic de eventos */ + netlink_events_create_pair (&handle->nl80211_scan, NETLINK_GENERIC); + if (handle->nl80211_scan.nl_sock != NULL) { + nl_socket_add_membership (handle->nl80211_scan.nl_sock, mcid); + nl_socket_modify_cb (handle->nl80211_scan.nl_sock, NL_CB_VALID, NL_CB_CUSTOM, wireless_events_dispatcher, handle); + } + wireless_if_nl80211_scan_multicast_group_id = mcid; + } + + netlink_events_create_pair (&handle->nl80211_scan_results, NETLINK_GENERIC); + nl_socket_modify_cb (handle->nl80211_scan_results.nl_sock, NL_CB_VALID, NL_CB_CUSTOM, wireless_bss_parse_station_scan, handle); + nl_socket_modify_cb (handle->nl80211_scan_results.nl_sock, NL_CB_FINISH, NL_CB_CUSTOM, wireless_bss_finish_scan, handle); + + /* Guardar la familia nl80211 */ + wireless_if_nl80211_id = family_id; + handle->nl_sock_nl80211 = sock_req; + + /* Instalar un timer para ejecutar TRIGGER_SCAN cada minuto */ + //g_timeout_add_seconds (50, wireless_interface_timer_trigger, handle); +} + +int wireless_events_dispatcher (struct nl_msg *msg, void *arg) { + NetworkInadorHandle *handle = (NetworkInadorHandle *) arg; + struct nlmsghdr *reply; + struct genlmsghdr *gnlh; + struct nlattr *attr; + int remaining; + Interface *iface; + int has_bss; + int index; + + reply = nlmsg_hdr (msg); + + if (reply->nlmsg_type != wireless_if_nl80211_id) return NL_SKIP; + + gnlh = nlmsg_data (reply); + + switch (gnlh->cmd) { + case NL80211_CMD_NEW_SCAN_RESULTS: + /* Es un mensaje del grupo multicast "scan" */ + has_bss = 0; + index = 0; + iface = NULL; + nlmsg_for_each_attr(attr, reply, sizeof (struct genlmsghdr), remaining) { + if (nla_type (attr) == NL80211_ATTR_BSS) { + has_bss = 1; + } else if (nla_type (attr) == NL80211_ATTR_IFINDEX) { + if (nla_len (attr) != 4) { + /* Tamaño incorrecto para el nuevo master */ + continue; + } + index = nla_get_u32 (attr); + printf ("CMD_NEW_SCAN_RESULTS sobre interfaz %i\n", index); + } + } + + if (index > 0) { + iface = _interfaces_locate_by_index (handle->interfaces, index); + } + + if (iface == NULL) { + /* Si no hay interfaz, tenemos un gran problema */ + break; + } + + if (has_bss == 1) { + /* Es un mensaje de una estación */ + printf ("Mensaje NEW RESULTS con estación\n"); + } else { + /* Ejecutar el CMD_GET_SCAN */ + printf ("Ejecutando GET_SCAN...\n"); + wireless_interface_get_scan (handle, iface); + } + return NL_SKIP; + break; + } + + return NL_SKIP; +} + +void wireless_interface_trigger_scan (NetworkInadorHandle *handle, Interface *iface) { + struct nl_msg *msg; + int error, ret; + struct nlattr *ssid; + + msg = nlmsg_alloc (); + if (msg == NULL) { + return; + } + + if (iface->is_wireless == 0) { + /* No puedo ejecutar trigger scan sobre una interfaz que no he visto como wireless */ + return; + } + + printf ("Ejecutando NL80211_CMD_TRIGGER_SCAN\n"); + + genlmsg_put (msg, NL_AUTO_PORT, NL_AUTO_SEQ, wireless_if_nl80211_id, 0, 0, NL80211_CMD_TRIGGER_SCAN, 0); + + nla_put_u32 (msg, NL80211_ATTR_IFINDEX, iface->index); + + ssid = nla_nest_start (msg, NL80211_ATTR_SCAN_SSIDS); + nla_put (msg, 1, 0, ""); // Scan all SSIDs. + nla_nest_end (msg, ssid); + + nl_complete_msg (handle->nl_sock_nl80211, msg); + + ret = nl_send (handle->nl_sock_nl80211, msg); + + nlmsg_free (msg); + if (ret <= 0) { + return; + } + + error = 0; + nl_socket_modify_cb (handle->nl_sock_nl80211, NL_CB_VALID, NL_CB_CUSTOM, _wireless_if_valid, &error); + nl_socket_modify_cb (handle->nl_sock_nl80211, NL_CB_INVALID, NL_CB_CUSTOM, _wireless_if_invalid, &error); + nl_socket_modify_cb (handle->nl_sock_nl80211, NL_CB_ACK, NL_CB_CUSTOM, _wireless_if_ack, &error); + nl_socket_modify_err_cb (handle->nl_sock_nl80211, NL_CB_CUSTOM, _wireless_if_wait_error, &error); + + nl_recvmsgs_default (handle->nl_sock_nl80211); + + if (ret <= 0 || error < 0) { + printf ("Error al hacer NL80211_CMD_TRIGGER_SCAN error = %i\n", error); + return; + } +} + +void wireless_interface_get_scan (NetworkInadorHandle *handle, Interface *iface) { + struct nl_msg *msg; + int error, ret; + struct genlmsghdr *gnlh; + struct nlmsghdr *req; + + msg = nlmsg_alloc (); + if (msg == NULL) { + return; + } + + if (iface->is_wireless == 0) { + /* No puedo ejecutar Get scan sobre una interfaz que no he visto como wireless */ + return; + } + + printf ("Ejecutando CMD_GET_SCAN\n"); + + req = nlmsg_put (msg, NL_AUTO_PORT, NL_AUTO_SEQ, wireless_if_nl80211_id, sizeof (struct genlmsghdr), NLM_F_DUMP); + gnlh = (struct genlmsghdr *) nlmsg_data (req); + + gnlh->cmd = NL80211_CMD_GET_SCAN; + gnlh->version = 0; + + nla_put_u32 (msg, NL80211_ATTR_IFINDEX, iface->index); + + nl_complete_msg (handle->nl80211_scan_results.nl_sock, msg); + + ret = nl_send (handle->nl80211_scan_results.nl_sock, msg); + + nlmsg_free (msg); + if (ret <= 0) { + return; + } + + //nl_socket_modify_cb (handle->nl80211_scan_results, NL_CB_VALID, NL_CB_CUSTOM, _wireless_if_valid, &error); + /*nl_socket_modify_cb (handle->nl80211_scan_results, NL_CB_INVALID, NL_CB_CUSTOM, _wireless_if_invalid, &error); + nl_socket_modify_cb (handle->nl80211_scan_results, NL_CB_ACK, NL_CB_CUSTOM, _wireless_if_ack, &error); + nl_socket_modify_err_cb (handle->nl80211_scan_results, NL_CB_CUSTOM, _wireless_if_wait_error, &error);*/ + + //nl_recvmsgs_default (handle->nl_sock_nl80211); + + if (ret <= 0) { + printf ("Error al hacer CMD_GET_SCAN error = %i\n", error); + //return; + } + + //nl_recvmsgs_default (handle->nl_sock_nl80211); +} + +void wireless_interface_check (NetworkInadorHandle *handle, Interface *iface) { + struct nl_msg * msg; + int ret; + struct _wireless_iface_is_wifi is_wifi; + WirelessInfo *info; + + if (wireless_if_nl80211_id == 0) { + wireless_interface_init_nl80211 (handle); + } + + info = (WirelessInfo *) malloc (sizeof (WirelessInfo)); + + if (info == NULL) { + return; + } + + msg = nlmsg_alloc (); + if (msg == NULL) { + free (info); + return; + } + + genlmsg_put (msg, NL_AUTO_PORT, NL_AUTO_SEQ, wireless_if_nl80211_id, 0, 0, NL80211_CMD_GET_WIPHY, 0); // Setup which command to run. + nla_put_u32 (msg, NL80211_ATTR_IFINDEX, iface->index); + + nl_complete_msg (handle->nl_sock_nl80211, msg); + + ret = nl_send (handle->nl_sock_nl80211, msg); + + nlmsg_free (msg); + if (ret <= 0) { + free (info); + return; + } + + memset (&is_wifi, 0, sizeof (is_wifi)); + memset (info, 0, sizeof (WirelessInfo)); + is_wifi.info = info; + + nl_socket_modify_cb (handle->nl_sock_nl80211, NL_CB_VALID, NL_CB_CUSTOM, _wireless_if_cb_valid_is_wifi, &is_wifi); + nl_socket_modify_cb (handle->nl_sock_nl80211, NL_CB_INVALID, NL_CB_CUSTOM, _wireless_if_invalid, &is_wifi.error); + nl_socket_modify_cb (handle->nl_sock_nl80211, NL_CB_ACK, NL_CB_CUSTOM, _wireless_if_ack, &is_wifi.error); + nl_socket_modify_err_cb (handle->nl_sock_nl80211, NL_CB_CUSTOM, _wireless_if_wait_error, &is_wifi.error); + + nl_recvmsgs_default (handle->nl_sock_nl80211); + + if (ret <= 0 || is_wifi.error < 0) { + free (info); + return; + } + + if (is_wifi.is_wifi != 0) { + /* Es una interfaz inalámbrica */ + printf ("La interfaz %s es inalambrica\n", iface->name); + + iface->is_wireless = 1; + iface->wireless = info; + } else { + free (info); + } +} diff --git a/src/wireless_if.h b/src/wireless_if.h new file mode 100644 index 0000000..775ca2b --- /dev/null +++ b/src/wireless_if.h @@ -0,0 +1,48 @@ +/* + * wireless_if.h + * This file is part of Network-inador + * + * Copyright (C) 2020 - Félix Arreola Rodríguez + * + * Network-inador is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Network-inador is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Network-inador; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#ifndef __WIRELESS_IF_H__ +#define __WIRELESS_IF_H__ + +#include "common.h" + +typedef enum { /*< flags >*/ + WIFI_DEVICE_CAP_NONE = 0x00000000, + WIFI_DEVICE_CAP_CIPHER_WEP40 = 0x00000001, + WIFI_DEVICE_CAP_CIPHER_WEP104 = 0x00000002, + WIFI_DEVICE_CAP_CIPHER_TKIP = 0x00000004, + WIFI_DEVICE_CAP_CIPHER_CCMP = 0x00000008, + WIFI_DEVICE_CAP_WPA = 0x00000010, + WIFI_DEVICE_CAP_RSN = 0x00000020, + WIFI_DEVICE_CAP_AP = 0x00000040, + WIFI_DEVICE_CAP_ADHOC = 0x00000080, + WIFI_DEVICE_CAP_FREQ_VALID = 0x00000100, + WIFI_DEVICE_CAP_FREQ_2GHZ = 0x00000200, + WIFI_DEVICE_CAP_FREQ_5GHZ = 0x00000400, +} DeviceWifiCapabilities; + +void wireless_interface_check (NetworkInadorHandle *handle, Interface *iface); +int wireless_events_dispatcher (struct nl_msg *msg, void *arg); + +extern uint16_t wireless_if_nl80211_id; + +#endif /* __WIRELESS_IF_H__ */