-rw-r--r-- | libopie2/opienet/onetwork.cpp | 55 | ||||
-rw-r--r-- | libopie2/opienet/onetwork.h | 2 |
2 files changed, 24 insertions, 33 deletions
diff --git a/libopie2/opienet/onetwork.cpp b/libopie2/opienet/onetwork.cpp index 789e8ca..6a363d7 100644 --- a/libopie2/opienet/onetwork.cpp +++ b/libopie2/opienet/onetwork.cpp @@ -1,305 +1,316 @@ /* This file is part of the Opie Project Copyright (C) 2003 by the Wellenreiter team: Martin J. Muench <mjm@remote-exploit.org> Max Moser <mmo@remote-exploit.org Michael 'Mickey' Lauer <mickey@tm.informatik.uni-frankfurt.de> =. .=l. .>+-= _;:, .> :=|. This program is free software; you can .> <`_, > . <= redistribute it and/or modify it under :`=1 )Y*s>-.-- : the terms of the GNU Library General Public .="- .-=="i, .._ License as published by the Free Software - . .-<_> .<> Foundation; either version 2 of the License, ._= =} : or (at your option) any later version. .%`+i> _;_. .i_,=:_. -<s. This program 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 ..}^=.= = ; Library General Public License for more ++= -. .` .: details. : = ...= . :.=- -. .:....=;==+<; You should have received a copy of the GNU -_. . . )=. = Library General Public License along with -- :-=` this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* OPIE */ #include <opie2/onetwork.h> /* QT */ #include <qfile.h> #include <qtextstream.h> /* UNIX */ #include <arpa/inet.h> #include <cerrno> #include <cstring> #include <cstdlib> #include <math.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #include <linux/sockios.h> +#include <net/if_arp.h> #include <stdarg.h> using namespace std; /*====================================================================================== * ONetwork *======================================================================================*/ ONetwork* ONetwork::_instance = 0; ONetwork::ONetwork() { qDebug( "ONetwork::ONetwork()" ); synchronize(); } void ONetwork::synchronize() { // gather available interfaces by inspecting /proc/net/dev - // we could use SIOCGIFCONF here, but we aren't interested in virtual (e.g. eth0:0) devices + //FIXME: we could use SIOCGIFCONF here, but we aren't interested in virtual (e.g. eth0:0) devices + //FIXME: Use SIOCGIFCONF anway, because we can disable listing of aliased devices _interfaces.clear(); QString str; QFile f( "/proc/net/dev" ); bool hasFile = f.open( IO_ReadOnly ); if ( !hasFile ) { qDebug( "ONetwork: /proc/net/dev not existing. No network devices available" ); return; } QTextStream s( &f ); s.readLine(); s.readLine(); while ( !s.atEnd() ) { s >> str; str.truncate( str.find( ':' ) ); qDebug( "ONetwork: found interface '%s'", (const char*) str ); ONetworkInterface* iface; if ( isWirelessInterface( str ) ) { iface = new OWirelessNetworkInterface( this, str ); qDebug( "ONetwork: interface '%s' has Wireless Extensions", (const char*) str ); } else { iface = new ONetworkInterface( this, str ); } _interfaces.insert( str, iface ); s.readLine(); } } ONetworkInterface* ONetwork::interface( QString iface ) const { return _interfaces[iface]; } ONetwork* ONetwork::instance() { if ( !_instance ) _instance = new ONetwork(); return _instance; } ONetwork::InterfaceIterator ONetwork::iterator() const { return ONetwork::InterfaceIterator( _interfaces ); } bool ONetwork::isWirelessInterface( const char* name ) const { int sfd = socket( AF_INET, SOCK_STREAM, 0 ); struct iwreq iwr; memset( &iwr, 0, sizeof( struct iwreq ) ); strcpy( (char*) &iwr.ifr_name, name ); int result = ::ioctl( sfd, SIOCGIWNAME, &iwr ); - if ( result == -1 ) - qDebug( "ONetwork::ioctl(): SIOCGIWNAME failed: %d (%s)", result, strerror( errno ) ); - else - qDebug( "ONetwork::ioctl(): SIOCGIWNAME ok." ); - return ( result != -1 ); + return result != -1; } /*====================================================================================== * ONetworkInterface *======================================================================================*/ ONetworkInterface::ONetworkInterface( QObject* parent, const char* name ) :QObject( parent, name ), _sfd( socket( AF_INET, SOCK_DGRAM, 0 ) ), _mon( 0 ) { qDebug( "ONetworkInterface::ONetworkInterface()" ); init(); } struct ifreq& ONetworkInterface::ifr() const { return _ifr; } void ONetworkInterface::init() { qDebug( "ONetworkInterface::init()" ); memset( &_ifr, 0, sizeof( struct ifreq ) ); if ( _sfd == -1 ) { qDebug( "ONetworkInterface::init(): Warning - can't get socket for device '%s'", name() ); return; } } bool ONetworkInterface::ioctl( int call, struct ifreq& ifreq ) const { int result = ::ioctl( _sfd, call, &ifreq ); if ( result == -1 ) qDebug( "ONetworkInterface::ioctl(): Call %d - Status: Failed: %d (%s)", call, result, strerror( errno ) ); else qDebug( "ONetworkInterface::ioctl(): Call %d - Status: Ok.", call ); return ( result != -1 ); } bool ONetworkInterface::ioctl( int call ) const { strcpy( _ifr.ifr_name, name() ); return ioctl( call, _ifr ); } bool ONetworkInterface::isLoopback() const { ioctl( SIOCGIFFLAGS ); return _ifr.ifr_flags & IFF_LOOPBACK; } bool ONetworkInterface::setUp( bool b ) { ioctl( SIOCGIFFLAGS ); if ( b ) _ifr.ifr_flags |= IFF_UP; else _ifr.ifr_flags &= (~IFF_UP); return ioctl( SIOCSIFFLAGS ); } bool ONetworkInterface::isUp() const { ioctl( SIOCGIFFLAGS ); return _ifr.ifr_flags & IFF_UP; } QString ONetworkInterface::ipV4Address() const { if ( ioctl( SIOCGIFADDR ) ) { struct sockaddr_in *sa = (struct sockaddr_in *) &_ifr.ifr_addr; //FIXME: Use QHostAddress here return QString( inet_ntoa( sa->sin_addr ) ); } else return "<unknown>"; } OMacAddress ONetworkInterface::macAddress() const { if ( ioctl( SIOCGIFHWADDR ) ) { return OMacAddress( _ifr ); } else { return OMacAddress::unknown; } } +int ONetworkInterface::dataLinkType() const +{ + if ( ioctl( SIOCGIFHWADDR ) ) + { + return _ifr.ifr_hwaddr.sa_family; + } + else + { + return -1; + } +} + + void ONetworkInterface::setMonitoring( OMonitoringInterface* m ) { _mon = m; qDebug( "ONetwork::setMonitoring(): Installed monitoring driver '%s' on interface '%s'", (const char*) m->name(), name() ); } OMonitoringInterface* ONetworkInterface::monitoring() const { return _mon; } ONetworkInterface::~ONetworkInterface() { qDebug( "ONetworkInterface::~ONetworkInterface()" ); if ( _sfd != -1 ) ::close( _sfd ); } bool ONetworkInterface::setPromiscuousMode( bool b ) { ioctl( SIOCGIFFLAGS ); if ( b ) _ifr.ifr_flags |= IFF_PROMISC; else _ifr.ifr_flags &= (~IFF_PROMISC); return ioctl( SIOCSIFFLAGS ); } bool ONetworkInterface::promiscuousMode() const { ioctl( SIOCGIFFLAGS ); return _ifr.ifr_flags & IFF_PROMISC; } bool ONetworkInterface::isWireless() const { return ioctl( SIOCGIWNAME ); } /*====================================================================================== * OChannelHopper *======================================================================================*/ OChannelHopper::OChannelHopper( OWirelessNetworkInterface* iface ) :QObject( 0, "Mickey's funky hopper" ), _iface( iface ), _interval( 0 ), _tid( 0 ) { int _maxChannel = iface->channels()+1; // generate fancy hopping sequence honoring the device capabilities if ( _maxChannel >= 1 ) _channels.append( 1 ); if ( _maxChannel >= 7 ) _channels.append( 7 ); if ( _maxChannel >= 13 ) _channels.append( 13 ); if ( _maxChannel >= 2 ) _channels.append( 2 ); if ( _maxChannel >= 8 ) _channels.append( 8 ); if ( _maxChannel >= 3 ) _channels.append( 3 ); if ( _maxChannel >= 14 ) _channels.append( 14 ); if ( _maxChannel >= 9 ) _channels.append( 9 ); if ( _maxChannel >= 4 ) _channels.append( 4 ); if ( _maxChannel >= 10 ) _channels.append( 10 ); if ( _maxChannel >= 5 ) _channels.append( 5 ); if ( _maxChannel >= 11 ) _channels.append( 11 ); @@ -524,402 +535,382 @@ int OWirelessNetworkInterface::channel() const } void OWirelessNetworkInterface::setChannel( int c ) const { if ( !_mon ) { memset( &_iwr, 0, sizeof( struct iwreq ) ); _iwr.u.freq.m = c; _iwr.u.freq.e = 0; wioctl( SIOCSIWFREQ ); } else { _mon->setChannel( c ); } } double OWirelessNetworkInterface::frequency() const { if ( !wioctl( SIOCGIWFREQ ) ) { return -1.0; } else { return double( _iwr.u.freq.m ) * pow( 10.0, _iwr.u.freq.e ) / 1000000000.0; } } int OWirelessNetworkInterface::channels() const { return _channels.count(); } void OWirelessNetworkInterface::setChannelHopping( int interval ) { if ( !_hopper ) _hopper = new OChannelHopper( this ); _hopper->setInterval( interval ); //FIXME: When and by whom will the channel hopper be deleted? //TODO: rely on QObject hierarchy } int OWirelessNetworkInterface::channelHopping() const { return _hopper->interval(); } void OWirelessNetworkInterface::setMonitorMode( bool b ) { if ( _mon ) _mon->setEnabled( b ); else qDebug( "ONetwork(): can't switch monitor mode without installed monitoring interface" ); } bool OWirelessNetworkInterface::monitorMode() const { - return _mon ? _mon->enabled() : false; + qDebug( "dataLinkType = %d", dataLinkType() ); + return dataLinkType() == ARPHRD_IEEE80211; } QString OWirelessNetworkInterface::nickName() const { char str[IW_ESSID_MAX_SIZE]; _iwr.u.data.pointer = &str[0]; _iwr.u.data.length = IW_ESSID_MAX_SIZE; if ( !wioctl( SIOCGIWNICKN ) ) { return "<unknown>"; } else { str[_iwr.u.data.length] = 0x0; // some drivers (e.g. wlan-ng) don't zero-terminate the string return str; } } void OWirelessNetworkInterface::setPrivate( const QString& call, int numargs, ... ) { OPrivateIOCTL* priv = static_cast<OPrivateIOCTL*>( child( (const char*) call ) ); if ( !priv ) { qDebug( "OWirelessNetworkInterface::setPrivate(): interface '%s' does not support private ioctl '%s'", name(), (const char*) call ); return; } if ( priv->numberSetArgs() != numargs ) { qDebug( "OWirelessNetworkInterface::setPrivate(): parameter count not matching. '%s' expects %d arguments, but got %d", (const char*) call, priv->numberSetArgs(), numargs ); return; } qDebug( "OWirelessNetworkInterface::setPrivate(): about to call '%s' on interface '%s'", (const char*) call, name() ); memset( &_iwr, 0, sizeof _iwr ); va_list argp; va_start( argp, numargs ); for ( int i = 0; i < numargs; ++i ) { priv->setParameter( i, va_arg( argp, int ) ); } va_end( argp ); priv->invoke(); } void OWirelessNetworkInterface::getPrivate( const QString& call ) { } QString OWirelessNetworkInterface::SSID() const { char str[IW_ESSID_MAX_SIZE]; _iwr.u.essid.pointer = &str[0]; _iwr.u.essid.length = IW_ESSID_MAX_SIZE; if ( !wioctl( SIOCGIWESSID ) ) { return "<unknown>"; } else { return str; } } void OWirelessNetworkInterface::setSSID( const QString& ssid ) { _iwr.u.essid.pointer = const_cast<char*>( (const char*) ssid ); _iwr.u.essid.length = ssid.length(); wioctl( SIOCSIWESSID ); } bool OWirelessNetworkInterface::wioctl( int call, struct iwreq& iwreq ) const { int result = ::ioctl( _sfd, call, &iwreq ); if ( result == -1 ) qDebug( "ONetworkInterface::wioctl(): Call %d - Status: Failed: %d (%s)", call, result, strerror( errno ) ); else qDebug( "ONetworkInterface::wioctl(): Call %d - Status: Ok.", call ); return ( result != -1 ); } bool OWirelessNetworkInterface::wioctl( int call ) const { strcpy( _iwr.ifr_name, name() ); return wioctl( call, _iwr ); } /*====================================================================================== * OMonitoringInterface *======================================================================================*/ OMonitoringInterface::OMonitoringInterface( ONetworkInterface* iface ) - :_enabled( false ), _if( static_cast<OWirelessNetworkInterface*>( iface ) ) + :_if( static_cast<OWirelessNetworkInterface*>( iface ) ) { } OMonitoringInterface::~OMonitoringInterface() { } void OMonitoringInterface::setChannel( int c ) { // use standard WE channel switching protocol memset( &_if->_iwr, 0, sizeof( struct iwreq ) ); _if->_iwr.u.freq.m = c; _if->_iwr.u.freq.e = 0; _if->wioctl( SIOCSIWFREQ ); } bool OMonitoringInterface::enabled() const { - return _enabled; + return _if->monitorMode(); } + void OMonitoringInterface::setEnabled( bool b ) { - // open a packet capturer here or leave this to - // the client code? - - /* - - if ( b ) - { - OPacketCapturer* opcap = new OPacketCapturer(); - opcap->open( _if->name() ); - } - */ - - _enabled = b; - } + /*====================================================================================== * OCiscoMonitoringInterface *======================================================================================*/ OCiscoMonitoringInterface::OCiscoMonitoringInterface( ONetworkInterface* iface ) :OMonitoringInterface( iface ) { iface->setMonitoring( this ); } OCiscoMonitoringInterface::~OCiscoMonitoringInterface() { } void OCiscoMonitoringInterface::setEnabled( bool b ) { QString fname; fname.sprintf( "/proc/driver/aironet/%s", (const char*) _if->name() ); QFile f( fname ); if ( !f.exists() ) return; if ( f.open( IO_WriteOnly ) ) { QTextStream s( &f ); s << "Mode: r"; s << "Mode: y"; s << "XmitPower: 1"; - - OMonitoringInterface::setEnabled( b ); - } // flushing and closing will be done automatically when f goes out of scope } QString OCiscoMonitoringInterface::name() const { return "cisco"; } void OCiscoMonitoringInterface::setChannel( int ) { // cisco devices automatically switch channels when in monitor mode } /*====================================================================================== * OWlanNGMonitoringInterface *======================================================================================*/ OWlanNGMonitoringInterface::OWlanNGMonitoringInterface( ONetworkInterface* iface ) :OMonitoringInterface( iface ) { iface->setMonitoring( this ); } OWlanNGMonitoringInterface::~OWlanNGMonitoringInterface() { } void OWlanNGMonitoringInterface::setEnabled( bool b ) { //FIXME: do nothing if its already in the same mode QString enable = b ? "true" : "false"; QString cmd; cmd.sprintf( "$(which wlanctl-ng) %s lnxreq_wlansniff channel=%d enable=%s", (const char*) _if->name(), 1, (const char*) enable ); system( cmd ); - - OMonitoringInterface::setEnabled( b ); } QString OWlanNGMonitoringInterface::name() const { return "wlan-ng"; } void OWlanNGMonitoringInterface::setChannel( int ) { // wlan-ng devices automatically switch channels when in monitor mode } /*====================================================================================== * OHostAPMonitoringInterface *======================================================================================*/ OHostAPMonitoringInterface::OHostAPMonitoringInterface( ONetworkInterface* iface ) :OMonitoringInterface( iface ) { iface->setMonitoring( this ); } OHostAPMonitoringInterface::~OHostAPMonitoringInterface() { } void OHostAPMonitoringInterface::setEnabled( bool b ) { // IW_MODE_MONITOR was introduced in Wireless Extensions Version 15 // Wireless Extensions < Version 15 need iwpriv commandos for monitoring if ( b ) { #if WIRELESS_EXT > 14 _if->_iwr.u.mode = IW_MODE_MONITOR; _if->wioctl( SIOCSIWMODE ); #else int* args = (int*) &_if->_iwr.u.name; args[0] = 2; args[1] = 0; _if->wioctl( SIOCDEVPRIVATE ); #endif } else { #if WIRELESS_EXT > 14 _if->_iwr.u.mode = IW_MODE_INFRA; _if->wioctl( SIOCSIWMODE ); #else int* args = (int*) &_if->_iwr.u.name; args[0] = 0; args[1] = 0; _if->wioctl( SIOCDEVPRIVATE ); #endif } - - OMonitoringInterface::setEnabled( b ); } QString OHostAPMonitoringInterface::name() const { return "hostap"; } /*====================================================================================== * OOrinocoNetworkInterface *======================================================================================*/ OOrinocoMonitoringInterface::OOrinocoMonitoringInterface( ONetworkInterface* iface ) :OMonitoringInterface( iface ) { iface->setMonitoring( this ); } OOrinocoMonitoringInterface::~OOrinocoMonitoringInterface() { } void OOrinocoMonitoringInterface::setChannel( int c ) { // call iwpriv <device> monitor 2 <channel> int* args = (int*) &_if->_iwr.u.name; args[0] = 2; args[1] = c; _if->wioctl( SIOCIWFIRSTPRIV + 0x8 ); } void OOrinocoMonitoringInterface::setEnabled( bool b ) { if ( b ) { setChannel( 1 ); } else { // call iwpriv <device> monitor 0 0 int* args = (int*) &_if->_iwr.u.name; args[0] = 0; args[1] = 0; _if->wioctl( SIOCIWFIRSTPRIV + 0x8 ); } - - OMonitoringInterface::setEnabled( b ); } QString OOrinocoMonitoringInterface::name() const { return "orinoco"; } diff --git a/libopie2/opienet/onetwork.h b/libopie2/opienet/onetwork.h index 509c3db..4cadbeb 100644 --- a/libopie2/opienet/onetwork.h +++ b/libopie2/opienet/onetwork.h @@ -65,251 +65,251 @@ #ifndef SIOCIWFIRSTPRIV #define SIOCIWFIRSTPRIV SIOCDEVPRIVATE #endif class ONetworkInterface; class OWirelessNetworkInterface; class OChannelHopper; class OMonitoringInterface; /*====================================================================================== * ONetwork *======================================================================================*/ class ONetwork : public QObject { Q_OBJECT public: typedef QDict<ONetworkInterface> InterfaceMap; typedef QDictIterator<ONetworkInterface> InterfaceIterator; public: static ONetwork* instance(); InterfaceIterator iterator() const; bool isWirelessInterface( const char* ) const; ONetworkInterface* interface( QString ) const; protected: ONetwork(); void synchronize(); private: static ONetwork* _instance; InterfaceMap _interfaces; }; /*====================================================================================== * ONetworkInterface *======================================================================================*/ class ONetworkInterface : public QObject { friend class OMonitoringInterface; friend class OCiscoMonitoringInterface; friend class OWlanNGMonitoringInterface; friend class OHostAPMonitoringInterface; friend class OOrinocoMonitoringInterface; public: ONetworkInterface( QObject* parent, const char* name ); virtual ~ONetworkInterface(); void setMonitoring( OMonitoringInterface* ); OMonitoringInterface* monitoring() const; bool setPromiscuousMode( bool ); bool promiscuousMode() const; bool setUp( bool ); bool isUp() const; bool isLoopback() const; bool isWireless() const; QString ipV4Address() const; OMacAddress macAddress() const; + int dataLinkType() const; protected: const int _sfd; mutable ifreq _ifr; OMonitoringInterface* _mon; protected: struct ifreq& ifr() const; virtual void init(); bool ioctl( int call ) const; bool ioctl( int call, struct ifreq& ) const; }; /*====================================================================================== * OChannelHopper *======================================================================================*/ class OChannelHopper : public QObject { public: OChannelHopper( OWirelessNetworkInterface* ); virtual ~OChannelHopper(); bool isActive() const; int channel() const; virtual void timerEvent( QTimerEvent* ); void setInterval( int ); int interval() const; private: OWirelessNetworkInterface* _iface; int _interval; int _tid; QValueList<int> _channels; QValueList<int>::Iterator _channel; }; /*====================================================================================== * OWirelessNetworkInterface *======================================================================================*/ class OWirelessNetworkInterface : public ONetworkInterface { friend class OMonitoringInterface; friend class OCiscoMonitoringInterface; friend class OWlanNGMonitoringInterface; friend class OHostAPMonitoringInterface; friend class OOrinocoMonitoringInterface; friend class OPrivateIOCTL; public: enum Mode { AdHoc, Managed, Monitor }; OWirelessNetworkInterface( QObject* parent, const char* name ); virtual ~OWirelessNetworkInterface(); virtual void setChannel( int ) const; virtual int channel() const; virtual double frequency() const; virtual int channels() const; //virtual double frequency(int) const; virtual void setMode( Mode ) {}; virtual bool mode() const {}; virtual void setMonitorMode( bool ); virtual bool monitorMode() const; virtual void setChannelHopping( int interval = 0 ); virtual int channelHopping() const; virtual void setNickName( const QString& ) {}; virtual QString nickName() const; virtual void setPrivate( const QString&, int, ... ); virtual void getPrivate( const QString& ); virtual bool isAssociated() const {}; virtual QString associatedAP() const; virtual void setSSID( const QString& ); virtual QString SSID() const; protected: void buildChannelList(); void buildPrivateList(); virtual void init(); struct iwreq& iwr() const; bool wioctl( int call ) const; bool wioctl( int call, struct iwreq& ) const; protected: mutable struct iwreq _iwr; QMap<int,int> _channels; private: OChannelHopper* _hopper; }; /*====================================================================================== * OMonitoringInterface *======================================================================================*/ class OMonitoringInterface { public: OMonitoringInterface(); OMonitoringInterface( ONetworkInterface* ); virtual ~OMonitoringInterface(); public: virtual void setEnabled( bool ); virtual bool enabled() const; virtual void setChannel( int ); virtual QString name() const = 0; protected: - bool _enabled; const OWirelessNetworkInterface* _if; }; /*====================================================================================== * OCiscoMonitoring *======================================================================================*/ class OCiscoMonitoringInterface : public OMonitoringInterface { public: OCiscoMonitoringInterface( ONetworkInterface* ); virtual ~OCiscoMonitoringInterface(); virtual void setEnabled( bool ); virtual QString name() const; virtual void setChannel( int ); }; /*====================================================================================== * OWlanNGMonitoringInterface *======================================================================================*/ class OWlanNGMonitoringInterface : public OMonitoringInterface { public: OWlanNGMonitoringInterface( ONetworkInterface* ); virtual ~OWlanNGMonitoringInterface(); public: virtual void setEnabled( bool ); virtual QString name() const; virtual void setChannel( int ); }; /*====================================================================================== * OHostAPMonitoringInterface *======================================================================================*/ class OHostAPMonitoringInterface : public OMonitoringInterface { public: OHostAPMonitoringInterface( ONetworkInterface* ); virtual ~OHostAPMonitoringInterface(); public: virtual void setEnabled( bool ); virtual QString name() const; }; /*====================================================================================== * OOrinocoMonitoringInterface *======================================================================================*/ class OOrinocoMonitoringInterface : public OMonitoringInterface { public: OOrinocoMonitoringInterface( ONetworkInterface* ); virtual ~OOrinocoMonitoringInterface(); |