summaryrefslogtreecommitdiff
path: root/libopie2/opienet/onetwork.cpp
Unidiff
Diffstat (limited to 'libopie2/opienet/onetwork.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opienet/onetwork.cpp774
1 files changed, 774 insertions, 0 deletions
diff --git a/libopie2/opienet/onetwork.cpp b/libopie2/opienet/onetwork.cpp
new file mode 100644
index 0000000..1d3b9fe
--- a/dev/null
+++ b/libopie2/opienet/onetwork.cpp
@@ -0,0 +1,774 @@
1/*
2                 This file is part of the Opie Project
3              Copyright (C) 2003 by the Wellenreiter team:
4 Martin J. Muench <mjm@remote-exploit.org>
5 Max Moser <mmo@remote-exploit.org
6 Michael 'Mickey' Lauer <mickey@tm.informatik.uni-frankfurt.de>
7 =.
8 .=l.
9           .>+-=
10 _;:,     .>    :=|. This program is free software; you can
11.> <`_,   >  .   <= redistribute it and/or modify it under
12:`=1 )Y*s>-.--   : the terms of the GNU Library General Public
13.="- .-=="i,     .._ License as published by the Free Software
14 - .   .-<_>     .<> Foundation; either version 2 of the License,
15     ._= =}       : or (at your option) any later version.
16    .%`+i>       _;_.
17    .i_,=:_.      -<s. This program is distributed in the hope that
18     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
19    : ..    .:,     . . . without even the implied warranty of
20    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
21  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
22..}^=.=       =       ; Library General Public License for more
23++=   -.     .`     .: details.
24 :     =  ...= . :.=-
25 -.   .:....=;==+<; You should have received a copy of the GNU
26  -_. . .   )=.  = Library General Public License along with
27    --        :-=` this library; see the file COPYING.LIB.
28 If not, write to the Free Software Foundation,
29 Inc., 59 Temple Place - Suite 330,
30 Boston, MA 02111-1307, USA.
31
32*/
33
34/* OPIE */
35
36#include <opie2/onetwork.h>
37
38/* QT */
39
40#include <qfile.h>
41#include <qtextstream.h>
42
43/* UNIX */
44
45#include <arpa/inet.h>
46#include <cerrno>
47#include <cstring>
48#include <cstdlib>
49#include <math.h>
50#include <sys/ioctl.h>
51#include <sys/socket.h>
52#include <sys/types.h>
53#include <unistd.h>
54#include <linux/wireless.h>
55
56using namespace std;
57
58/*======================================================================================
59 * ONetwork
60 *======================================================================================*/
61
62ONetwork* ONetwork::_instance = 0;
63
64ONetwork::ONetwork()
65{
66 qDebug( "ONetwork::ONetwork()" );
67 synchronize();
68}
69
70void ONetwork::synchronize()
71{
72 // gather available interfaces by inspecting /proc/net/dev
73 // we could use SIOCGIFCONF here, but we aren't interested in virtual (e.g. eth0:0) devices
74
75 _interfaces.clear();
76 QString str;
77 QFile f( "/proc/net/dev" );
78 bool hasFile = f.open( IO_ReadOnly );
79 if ( !hasFile )
80 {
81 qDebug( "ONetwork: /proc/net/dev not existing. No network devices available" );
82 return;
83 }
84 QTextStream s( &f );
85 s.readLine();
86 s.readLine();
87 while ( !s.atEnd() )
88 {
89 s >> str;
90 str.truncate( str.find( ':' ) );
91 qDebug( "ONetwork: found interface '%s'", (const char*) str );
92 ONetworkInterface* iface;
93 if ( isWirelessInterface( str ) )
94 {
95 iface = new OWirelessNetworkInterface( str );
96 qDebug( "ONetwork: interface '%s' has Wireless Extensions", (const char*) str );
97 }
98 else
99 {
100 iface = new ONetworkInterface( str );
101 }
102 _interfaces.insert( str, iface );
103 s.readLine();
104 }
105}
106
107
108ONetworkInterface* ONetwork::interface( QString iface ) const
109{
110 return _interfaces[iface];
111}
112
113
114ONetwork* ONetwork::instance()
115{
116 if ( !_instance ) _instance = new ONetwork();
117 return _instance;
118}
119
120
121ONetwork::InterfaceIterator ONetwork::iterator() const
122{
123 return ONetwork::InterfaceIterator( _interfaces );
124}
125
126
127bool ONetwork::isWirelessInterface( const char* name ) const
128{
129 int sfd = socket( AF_INET, SOCK_DGRAM, 0 );
130 iwreqstruct iwr;
131 memset( &iwr, 0, sizeof( iwreqstruct ) );
132 strcpy( (char*) &iwr.ifr_name, name );
133 int result = ::ioctl( sfd, SIOCGIWNAME, &iwr );
134 if ( result == -1 )
135 qDebug( "ONetwork::ioctl(): SIOCGIWNAME failed: %d (%s)", result, strerror( errno ) );
136 else
137 qDebug( "ONetwork::ioctl(): SIOCGIWNAME ok." );
138 return ( result != -1 );
139}
140
141/*======================================================================================
142 * ONetworkInterface
143 *======================================================================================*/
144
145ONetworkInterface::ONetworkInterface( const QString& name )
146 :_name( name ), _sfd( socket( AF_INET, SOCK_DGRAM, 0 ) ), _mon( 0 )
147{
148 qDebug( "ONetworkInterface::ONetworkInterface()" );
149 init();
150}
151
152
153ifreqstruct& ONetworkInterface::ifr() const
154{
155 return _ifr;
156}
157
158
159void ONetworkInterface::init()
160{
161 qDebug( "ONetworkInterface::init()" );
162
163 memset( &_ifr, 0, sizeof( struct ifreq ) );
164
165 if ( _sfd == -1 )
166 {
167 qDebug( "ONetworkInterface::init(): Warning - can't get socket for device '%s'", (const char*) _name );
168 return;
169 }
170}
171
172
173bool ONetworkInterface::ioctl( int call, ifreqstruct& ifreq ) const
174{
175 int result = ::ioctl( _sfd, call, &ifreq );
176 if ( result == -1 )
177 qDebug( "ONetworkInterface::ioctl(): Call %d - Status: Failed: %d (%s)", call, result, strerror( errno ) );
178 else
179 qDebug( "ONetworkInterface::ioctl(): Call %d - Status: Ok.", call );
180 return ( result != -1 );
181}
182
183
184bool ONetworkInterface::ioctl( int call ) const
185{
186 strcpy( _ifr.ifr_name, (const char*) _name );
187 return ioctl( call, _ifr );
188}
189
190
191bool ONetworkInterface::isLoopback() const
192{
193 ioctl( SIOCGIFFLAGS );
194 return _ifr.ifr_flags & IFF_LOOPBACK;
195}
196
197
198bool ONetworkInterface::setUp( bool b )
199{
200 ioctl( SIOCGIFFLAGS );
201 if ( b ) _ifr.ifr_flags |= IFF_UP;
202 else _ifr.ifr_flags &= (~IFF_UP);
203 return ioctl( SIOCSIFFLAGS );
204}
205
206
207bool ONetworkInterface::isUp() const
208{
209 ioctl( SIOCGIFFLAGS );
210 return _ifr.ifr_flags & IFF_UP;
211}
212
213
214QString ONetworkInterface::ipV4Address() const
215{
216 if ( ioctl( SIOCGIFADDR ) )
217 {
218 struct sockaddr_in *sa = (struct sockaddr_in *) &_ifr.ifr_addr;
219 //FIXME: Use QHostAddress here
220 return QString( inet_ntoa( sa->sin_addr ) );
221 }
222 else
223 return "<unknown>";
224}
225
226
227OMacAddress ONetworkInterface::macAddress() const
228{
229 if ( ioctl( SIOCGIFHWADDR ) )
230 {
231 return OMacAddress( _ifr );
232 }
233 else
234 {
235 return OMacAddress::unknown;
236 }
237}
238
239
240void ONetworkInterface::setMonitoring( OMonitoringInterface* m )
241{
242 _mon = m;
243 qDebug( "ONetwork::setMonitoring(): Installed monitoring interface '%s'", (const char*) m->name() );
244}
245
246
247OMonitoringInterface* ONetworkInterface::monitoring() const
248{
249 return _mon;
250}
251
252
253const QString& ONetworkInterface::name() const
254{
255 return _name;
256}
257
258
259ONetworkInterface::~ONetworkInterface()
260{
261 qDebug( "ONetworkInterface::~ONetworkInterface()" );
262 if ( _sfd != -1 ) ::close( _sfd );
263}
264
265
266bool ONetworkInterface::setPromiscuousMode( bool b )
267{
268 ioctl( SIOCGIFFLAGS );
269 if ( b ) _ifr.ifr_flags |= IFF_PROMISC;
270 else _ifr.ifr_flags &= (~IFF_PROMISC);
271 return ioctl( SIOCSIFFLAGS );
272}
273
274
275bool ONetworkInterface::promiscuousMode() const
276{
277 ioctl( SIOCGIFFLAGS );
278 return _ifr.ifr_flags & IFF_PROMISC;
279}
280
281
282bool ONetworkInterface::isWireless() const
283{
284 return ioctl( SIOCGIWNAME );
285}
286
287
288/*======================================================================================
289 * OChannelHopper
290 *======================================================================================*/
291
292OChannelHopper::OChannelHopper( OWirelessNetworkInterface* iface )
293 :QObject( 0, "Mickey's funky hopper" ),
294 _iface( iface ), _interval( 0 ), _channel( 0 ), _tid( 0 )
295{
296}
297
298
299OChannelHopper::~OChannelHopper()
300{
301}
302
303
304void OChannelHopper::timerEvent( QTimerEvent* )
305{
306 //FIXME: Get available channels from OWirelessNetworkInterface
307 if ( --_channel < 0 ) _channel = 13;
308 _iface->setChannel( _channel );
309 qDebug( "OChannelHopper::timerEvent(): set channel %d on interface '%s'",
310 _channel, (const char*) _iface->name() );
311}
312
313
314void OChannelHopper::setInterval( int interval )
315{
316 if ( interval == _interval )
317 return;
318
319 if ( _interval )
320 killTimer( _tid );
321
322 _interval = interval;
323
324 if ( _interval )
325 {
326 _tid = startTimer( interval );
327 }
328}
329
330
331int OChannelHopper::interval() const
332{
333 return _interval;
334}
335
336
337/*======================================================================================
338 * OWirelessNetworkInterface
339 *======================================================================================*/
340
341OWirelessNetworkInterface::OWirelessNetworkInterface( const QString& name )
342 :ONetworkInterface( name ), _hopper( this )
343{
344 qDebug( "OWirelessNetworkInterface::OWirelessNetworkInterface()" );
345 init();
346}
347
348
349OWirelessNetworkInterface::~OWirelessNetworkInterface()
350{
351}
352
353
354iwreqstruct& OWirelessNetworkInterface::iwr() const
355{
356 return _iwr;
357}
358
359
360void OWirelessNetworkInterface::init()
361{
362 qDebug( "OWirelessNetworkInterface::init()" );
363
364 memset( &_iwr, 0, sizeof( struct iwreq ) );
365
366 // IEEE802.11(b) radio frequency channels
367 //FIXME: get these directly from the interface
368 //FIXME: check if these channels are off-by-one
369
370 iwrangestruct range;
371 _iwr.u.data.pointer = (char*) &range;
372 _iwr.u.data.length = sizeof( iwrangestruct );
373 if ( !wioctl( SIOCGIWRANGE ) )
374 {
375 qDebug( "OWirelessNetworkInterface::init(): SIOCGIWRANGE failed (%s)", strerror( errno ) );
376 return;
377 }
378
379 //TODO: Find out what the difference between num_channel and
380 // num_frequency is about.
381
382 for ( int i = 0; i < range.num_frequency; ++i )
383 {
384 int freq = (int) ( double( range.freq[i].m ) * pow( 10, range.freq[i].e ) / 1000000.0 );
385 _channels.insert( freq, i );
386 }
387}
388
389
390QString OWirelessNetworkInterface::associatedAP() const
391{
392 //FIXME: use OMacAddress
393 QString mac;
394
395 if ( ioctl( SIOCGIWAP ) )
396 {
397 mac.sprintf( "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
398 _ifr.ifr_hwaddr.sa_data[0]&0xff,
399 _ifr.ifr_hwaddr.sa_data[1]&0xff,
400 _ifr.ifr_hwaddr.sa_data[2]&0xff,
401 _ifr.ifr_hwaddr.sa_data[3]&0xff,
402 _ifr.ifr_hwaddr.sa_data[4]&0xff,
403 _ifr.ifr_hwaddr.sa_data[5]&0xff );
404 }
405 else
406 {
407 mac = "<Unknown>";
408 }
409 return mac;
410}
411
412
413int OWirelessNetworkInterface::channel() const
414{
415 if ( !wioctl( SIOCGIWFREQ ) )
416 {
417 return -1;
418 }
419 else
420 {
421 //FIXME: This is off-by-one !? Why?
422 return _channels[ static_cast<int>(double( _iwr.u.freq.m ) * pow( 10, _iwr.u.freq.e ) / 1000000) ];
423 }
424}
425
426
427void OWirelessNetworkInterface::setChannel( int c ) const
428{
429 if ( !_mon )
430 {
431 memset( &_iwr, 0, sizeof( iwreqstruct ) );
432 _iwr.u.freq.m = c;
433 _iwr.u.freq.e = 0;
434 wioctl( SIOCSIWFREQ );
435 }
436 else
437 {
438 _mon->setChannel( c );
439 }
440}
441
442
443double OWirelessNetworkInterface::frequency() const
444{
445 if ( !wioctl( SIOCGIWFREQ ) )
446 {
447 return -1.0;
448 }
449 else
450 {
451 return double( _iwr.u.freq.m ) * pow( 10, _iwr.u.freq.e ) / 1000000000.0;
452 }
453}
454
455
456int OWirelessNetworkInterface::channels() const
457{
458 return _channels.count();
459}
460
461
462void OWirelessNetworkInterface::setChannelHopping( int interval )
463{
464 _hopper.setInterval( interval );
465}
466
467
468int OWirelessNetworkInterface::channelHopping() const
469{
470 return _hopper.interval();
471}
472
473
474void OWirelessNetworkInterface::setMonitorMode( bool b )
475{
476 if ( _mon )
477 _mon->setEnabled( b );
478 else
479 qDebug( "ONetwork(): can't switch monitor mode without installed monitoring interface" );
480}
481
482bool OWirelessNetworkInterface::monitorMode() const
483{
484 return _mon ? _mon->enabled() : false;
485}
486
487
488QString OWirelessNetworkInterface::nickName() const
489{
490 char str[IW_ESSID_MAX_SIZE];
491 _iwr.u.data.pointer = &str[0];
492 _iwr.u.data.length = IW_ESSID_MAX_SIZE;
493 if ( !wioctl( SIOCGIWNICKN ) )
494 {
495 return "<unknown>";
496 }
497 else
498 {
499 str[_iwr.u.data.length] = 0x0; // some drivers (e.g. wlan-ng) don't zero-terminate the string
500 return str;
501 }
502}
503
504
505QString OWirelessNetworkInterface::SSID() const
506{
507 char str[IW_ESSID_MAX_SIZE];
508 _iwr.u.essid.pointer = &str[0];
509 _iwr.u.essid.length = IW_ESSID_MAX_SIZE;
510 if ( !wioctl( SIOCGIWESSID ) )
511 {
512 return "<unknown>";
513 }
514 else
515 {
516 return str;
517 }
518}
519
520
521void OWirelessNetworkInterface::setSSID( const QString& ssid )
522{
523 _iwr.u.essid.pointer = const_cast<char*>( (const char*) ssid );
524 _iwr.u.essid.length = ssid.length();
525 wioctl( SIOCSIWESSID );
526}
527
528
529bool OWirelessNetworkInterface::wioctl( int call, iwreqstruct& iwreq ) const
530{
531 int result = ::ioctl( _sfd, call, &iwreq );
532 if ( result == -1 )
533 qDebug( "ONetworkInterface::wioctl(): Call %d - Status: Failed: %d (%s)", call, result, strerror( errno ) );
534 else
535 qDebug( "ONetworkInterface::wioctl(): Call %d - Status: Ok.", call );
536 return ( result != -1 );
537}
538
539
540bool OWirelessNetworkInterface::wioctl( int call ) const
541{
542 strcpy( _iwr.ifr_name, (const char*) _name );
543 return wioctl( call, _iwr );
544}
545
546
547/*======================================================================================
548 * OMonitoringInterface
549 *======================================================================================*/
550
551OMonitoringInterface::OMonitoringInterface( ONetworkInterface* iface )
552 :_enabled( false ), _if( static_cast<OWirelessNetworkInterface*>( iface ) )
553{
554}
555
556
557OMonitoringInterface::~OMonitoringInterface()
558{
559}
560
561
562void OMonitoringInterface::setChannel( int c )
563{
564 // use standard WE channel switching protocol
565 memset( &_if->_iwr, 0, sizeof( iwreqstruct ) );
566 _if->_iwr.u.freq.m = c;
567 _if->_iwr.u.freq.e = 0;
568 _if->wioctl( SIOCSIWFREQ );
569}
570
571
572bool OMonitoringInterface::enabled() const
573{
574 return _enabled;
575}
576
577void OMonitoringInterface::setEnabled( bool b )
578{
579 // open a packet capturer here or leave this to
580 // the client code?
581
582 /*
583
584 if ( b )
585 {
586 OPacketCapturer* opcap = new OPacketCapturer();
587 opcap->open( _if->name() );
588 }
589 */
590
591 _enabled = b;
592
593}
594
595/*======================================================================================
596 * OCiscoMonitoringInterface
597 *======================================================================================*/
598
599OCiscoMonitoringInterface::OCiscoMonitoringInterface( ONetworkInterface* iface )
600 :OMonitoringInterface( iface )
601{
602 iface->setMonitoring( this );
603}
604
605
606OCiscoMonitoringInterface::~OCiscoMonitoringInterface()
607{
608}
609
610
611void OCiscoMonitoringInterface::setEnabled( bool b )
612{
613 QString fname;
614 fname.sprintf( "/proc/driver/aironet/%s", (const char*) _if->name() );
615 QFile f( fname );
616 if ( !f.exists() ) return;
617
618 if ( f.open( IO_WriteOnly ) )
619 {
620 QTextStream s( &f );
621 s << "Mode: r";
622 s << "Mode: y";
623 s << "XmitPower: 1";
624
625 OMonitoringInterface::setEnabled( b );
626
627 }
628
629 // flushing and closing will be done automatically when f goes out of scope
630}
631
632
633QString OCiscoMonitoringInterface::name() const
634{
635 return "cisco";
636}
637
638
639void OCiscoMonitoringInterface::setChannel( int )
640{
641 // cisco devices automatically switch channels when in monitor mode
642}
643
644
645/*======================================================================================
646 * OWlanNGMonitoringInterface
647 *======================================================================================*/
648
649
650OWlanNGMonitoringInterface::OWlanNGMonitoringInterface( ONetworkInterface* iface )
651 :OMonitoringInterface( iface )
652{
653 iface->setMonitoring( this );
654}
655
656
657OWlanNGMonitoringInterface::~OWlanNGMonitoringInterface()
658{
659}
660
661
662void OWlanNGMonitoringInterface::setEnabled( bool b )
663{
664 //FIXME: do nothing if its already in the same mode
665
666 QString enable = b ? "true" : "false";
667 QString cmd;
668 cmd.sprintf( "$(which wlanctl-ng) %s lnxreq_wlansniff channel=%d enable=%s", (const char*) _if->name(), 1, (const char*) enable );
669 system( cmd );
670
671 OMonitoringInterface::setEnabled( b );
672}
673
674
675QString OWlanNGMonitoringInterface::name() const
676{
677 return "wlan-ng";
678}
679
680
681void OWlanNGMonitoringInterface::setChannel( int )
682{
683 // wlan-ng devices automatically switch channels when in monitor mode
684}
685
686
687/*======================================================================================
688 * OHostAPMonitoringInterface
689 *======================================================================================*/
690
691OHostAPMonitoringInterface::OHostAPMonitoringInterface( ONetworkInterface* iface )
692 :OMonitoringInterface( iface )
693{
694 iface->setMonitoring( this );
695}
696
697OHostAPMonitoringInterface::~OHostAPMonitoringInterface()
698{
699}
700
701void OHostAPMonitoringInterface::setEnabled( bool b )
702{
703 // IW_MODE_MONITOR was introduced in Wireless Extensions Version 15
704 // Wireless Extensions < Version 15 need iwpriv commandos for monitoring
705
706 #if WIRELESS_EXT > 14
707 _if->_iwr.u.mode = IW_MODE_MONITOR;
708 _if->wioctl( SIOCSIWMODE );
709 #else
710 int* args = (int*) &_if._iwr.u.name;
711 args[0] = 2;
712 args[1] = 0;
713 _if->wioctl( SIOCDEVPRIVATE );
714 #endif
715
716 OMonitoringInterface::setEnabled( b );
717}
718
719
720QString OHostAPMonitoringInterface::name() const
721{
722 return "hostap";
723}
724
725
726/*======================================================================================
727 * OOrinocoNetworkInterface
728 *======================================================================================*/
729
730OOrinocoMonitoringInterface::OOrinocoMonitoringInterface( ONetworkInterface* iface )
731 :OMonitoringInterface( iface )
732{
733 iface->setMonitoring( this );
734}
735
736
737OOrinocoMonitoringInterface::~OOrinocoMonitoringInterface()
738{
739}
740
741
742void OOrinocoMonitoringInterface::setChannel( int c )
743{
744 // call iwpriv <device> monitor 2 <channel>
745 int* args = (int*) &_if->_iwr.u.name;
746 args[0] = 2;
747 args[1] = c;
748 _if->wioctl( SIOCIWFIRSTPRIV + 0x8 );
749}
750
751
752void OOrinocoMonitoringInterface::setEnabled( bool b )
753{
754 if ( b )
755 {
756 setChannel( 1 );
757 }
758 else
759 {
760 // call iwpriv <device> monitor 0 0
761 int* args = (int*) &_if->_iwr.u.name;
762 args[0] = 0;
763 args[1] = 0;
764 _if->wioctl( SIOCIWFIRSTPRIV + 0x8 );
765 }
766
767 OMonitoringInterface::setEnabled( b );
768}
769
770
771QString OOrinocoMonitoringInterface::name() const
772{
773 return "orinoco";
774}