-rw-r--r-- | noncore/net/wellenreiter/libwellenreiter/source/cardmode.cc | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/cardmode.cc b/noncore/net/wellenreiter/libwellenreiter/source/cardmode.cc index a512bc5..a57645b 100644 --- a/noncore/net/wellenreiter/libwellenreiter/source/cardmode.cc +++ b/noncore/net/wellenreiter/libwellenreiter/source/cardmode.cc | |||
@@ -1,174 +1,202 @@ | |||
1 | /* | 1 | /* |
2 | * Set card modes for sniffing | 2 | * Set card modes for sniffing |
3 | * | 3 | * |
4 | * $Id$ | 4 | * $Id$ |
5 | */ | 5 | */ |
6 | 6 | ||
7 | #include "cardmode.hh" | 7 | #include "cardmode.hh" |
8 | #include "wl_log.hh" | 8 | #include "wl_log.hh" |
9 | 9 | ||
10 | /* main card into monitor function */ | 10 | /* main card into monitor function */ |
11 | int card_into_monitormode (pcap_t **orighandle, const char *device, int cardtype) | 11 | int card_into_monitormode (pcap_t **orighandle, const char *device, int cardtype) |
12 | { | 12 | { |
13 | char CiscoRFMON[35] = "/proc/driver/aironet/"; | 13 | char CiscoRFMON[35] = "/proc/driver/aironet/"; |
14 | FILE *CISCO_CONFIG_FILE; | 14 | FILE *CISCO_CONFIG_FILE; |
15 | 15 | ||
16 | /* Checks if we have a device to sniff on */ | 16 | /* Checks if we have a device to sniff on */ |
17 | if(device == NULL) | 17 | if(device == NULL) |
18 | { | 18 | { |
19 | wl_logerr("No device given"); | 19 | wl_logerr("No device given"); |
20 | return 0; | 20 | return 0; |
21 | } | 21 | } |
22 | 22 | ||
23 | /* Setting the promiscous and up flag to the interface */ | 23 | /* Setting the promiscous and up flag to the interface */ |
24 | if (!card_set_promisc_up(device)) | 24 | if (!card_set_promisc_up(device)) |
25 | { | 25 | { |
26 | wl_logerr("Cannot set interface to promisc mode"); | 26 | wl_logerr("Cannot set interface to promisc mode"); |
27 | return 0; | 27 | return 0; |
28 | } | 28 | } |
29 | wl_loginfo("Interface set to promisc mode"); | 29 | wl_loginfo("Interface set to promisc mode"); |
30 | 30 | ||
31 | /* Check the cardtype and executes the commands to go into monitor mode */ | 31 | /* Check the cardtype and executes the commands to go into monitor mode */ |
32 | if (cardtype == CARD_TYPE_CISCO) | 32 | if (cardtype == CARD_TYPE_CISCO) |
33 | { | 33 | { |
34 | /* bring the sniffer into rfmon mode */ | 34 | /* bring the sniffer into rfmon mode */ |
35 | snprintf(CiscoRFMON, sizeof(CiscoRFMON) - 1, DEFAULT_PATH, device); | 35 | snprintf(CiscoRFMON, sizeof(CiscoRFMON) - 1, DEFAULT_PATH, device); |
36 | if((CISCO_CONFIG_FILE = fopen(CiscoRFMON,"w")) == NULL) | 36 | if((CISCO_CONFIG_FILE = fopen(CiscoRFMON,"w")) == NULL) |
37 | { | 37 | { |
38 | wl_logerr("Cannot open config file: %s", strerror(errno)); | 38 | wl_logerr("Cannot open config file: %s", strerror(errno)); |
39 | return 0; | 39 | return 0; |
40 | } | 40 | } |
41 | fputs ("Mode: r",CISCO_CONFIG_FILE); | 41 | fputs ("Mode: r",CISCO_CONFIG_FILE); |
42 | fputs ("Mode: y",CISCO_CONFIG_FILE); | 42 | fputs ("Mode: y",CISCO_CONFIG_FILE); |
43 | fputs ("XmitPower: 1",CISCO_CONFIG_FILE); | 43 | fputs ("XmitPower: 1",CISCO_CONFIG_FILE); |
44 | fclose(CISCO_CONFIG_FILE); | 44 | fclose(CISCO_CONFIG_FILE); |
45 | } | 45 | } |
46 | else if (cardtype == CARD_TYPE_NG) | 46 | else if (cardtype == CARD_TYPE_NG) |
47 | { | 47 | { |
48 | char wlanngcmd[80]; | 48 | char wlanngcmd[80]; |
49 | snprintf(wlanngcmd, sizeof(wlanngcmd) - 1, "$(which wlanctl-ng) %s lnxreq_wlansniff channel=%d enable=true", device, 1); | 49 | snprintf(wlanngcmd, sizeof(wlanngcmd) - 1, "$(which wlanctl-ng) %s lnxreq_wlansniff channel=%d enable=true", device, 1); |
50 | if (system(wlanngcmd) != 0) | 50 | if (system(wlanngcmd) != 0) |
51 | { | 51 | { |
52 | wl_logerr("Could not set %s in raw mode, check cardtype", device); | 52 | wl_logerr("Could not set %s in raw mode, check cardtype", device); |
53 | return 0; | 53 | return 0; |
54 | } | 54 | } |
55 | } | 55 | } |
56 | else if (cardtype == CARD_TYPE_HOSTAP) | 56 | else if (cardtype == CARD_TYPE_HOSTAP) |
57 | { | 57 | { |
58 | #if WIRELESS_EXT > 14 | 58 | #if WIRELESS_EXT > 14 |
59 | // IW_MODE_MONITOR was implemented in Wireless Extensions Version 15 | 59 | // IW_MODE_MONITOR was implemented in Wireless Extensions Version 15 |
60 | int skfd; | 60 | int skfd; |
61 | skfd = socket(AF_INET, SOCK_STREAM, 0); | 61 | skfd = socket(AF_INET, SOCK_STREAM, 0); |
62 | struct iwreq wrq; | 62 | struct iwreq wrq; |
63 | wrq.u.mode = IW_MODE_MONITOR; | 63 | wrq.u.mode = IW_MODE_MONITOR; |
64 | 64 | ||
65 | if(iw_set_ext(skfd,(char *) device,SIOCSIWMODE,&wrq)<0) | 65 | if(iw_set_ext(skfd,(char *) device,SIOCSIWMODE,&wrq)<0) |
66 | { | 66 | { |
67 | wl_logerr("Could not set hostap card %s to raw mode, check cardtype", device); | 67 | wl_logerr("Could not set hostap card %s to raw mode, check cardtype", device); |
68 | return 0; | 68 | return 0; |
69 | } | 69 | } |
70 | else | 70 | else |
71 | { | 71 | { |
72 | wl_loginfo("Successfully set hostap card %s into raw mode",device); | 72 | wl_loginfo("Successfully set hostap card %s into raw mode",device); |
73 | return 1; | 73 | return 1; |
74 | } | 74 | } |
75 | return 1; | 75 | return 1; |
76 | #else | 76 | #else |
77 | #warning Hi _MAX_, please use a system call for hostap with wireless extensions < 15 | 77 | // Wireless Extensions < Version 15 need iwpriv commandos for monitoring |
78 | // TODO: Implement switching HOSTAP into monitor mode with system call | 78 | int fd; |
79 | //Wireless tools structure for the iocalls | ||
80 | struct iwreq ireq; | ||
81 | int *ptr; | ||
82 | /* Socket needed to use the iocall to */ | ||
83 | fd = socket(AF_INET, SOCK_STREAM, 0); | ||
84 | |||
85 | if ( fd == -1 ) { | ||
86 | return -1; | ||
87 | } | ||
88 | |||
89 | ptr = (int *) ireq.u.name; | ||
90 | // This is the monitor mode for 802.11 non-prism header | ||
91 | ptr[0] = 2; | ||
92 | strcpy(ireq.ifr_ifrn.ifrn_name, device); | ||
93 | if (ioctl( fd, SIOCIWFIRSTPRIV + 4, &ireq)==0) | ||
94 | { | ||
95 | /* All was fine... */ | ||
96 | close(fd); | ||
97 | wl_loginfo("Set hostap card %s into monitormode",device); | ||
98 | return 1; | ||
99 | } | ||
100 | else | ||
101 | { | ||
102 | /* iocall does not work */ | ||
103 | close(fd); | ||
104 | wl_logerr("Could not set hostap card %s into monitormode, check cardtype",device); | ||
105 | return 0; | ||
106 | } | ||
79 | #endif | 107 | #endif |
80 | } | 108 | } |
81 | else if (cardtype == CARD_TYPE_ORINOCCO ) | 109 | else if (cardtype == CARD_TYPE_ORINOCCO ) |
82 | { | 110 | { |
83 | if (!card_set_channel (device, 1, CARD_TYPE_ORINOCCO)) | 111 | if (!card_set_channel (device, 1, CARD_TYPE_ORINOCCO)) |
84 | { | 112 | { |
85 | wl_logerr("Could not set %s in raw mode, check cardtype", device); | 113 | wl_logerr("Could not set %s in raw mode, check cardtype", device); |
86 | return 0; | 114 | return 0; |
87 | } | 115 | } |
88 | else | 116 | else |
89 | { | 117 | { |
90 | wl_loginfo("Successfully set %s into raw mode",device); | 118 | wl_loginfo("Successfully set %s into raw mode",device); |
91 | } | 119 | } |
92 | } | 120 | } |
93 | 121 | ||
94 | /* Setting the promiscous and up flag to the interface */ | 122 | /* Setting the promiscous and up flag to the interface */ |
95 | if (!card_check_rfmon_datalink(device)) | 123 | if (!card_check_rfmon_datalink(device)) |
96 | { | 124 | { |
97 | wl_logerr("Cannot set interface to rfmon mode"); | 125 | wl_logerr("Cannot set interface to rfmon mode"); |
98 | return 0; | 126 | return 0; |
99 | } | 127 | } |
100 | else | 128 | else |
101 | { | 129 | { |
102 | wl_loginfo("Interface set to rfmon mode"); | 130 | wl_loginfo("Interface set to rfmon mode"); |
103 | } | 131 | } |
104 | return 1; | 132 | return 1; |
105 | } | 133 | } |
106 | 134 | ||
107 | /* Check card is in the rfmon mode */ | 135 | /* Check card is in the rfmon mode */ |
108 | int card_check_rfmon_datalink (const char *device) | 136 | int card_check_rfmon_datalink (const char *device) |
109 | { | 137 | { |
110 | int datalinktype=0; | 138 | int datalinktype=0; |
111 | pcap_t *phandle; | 139 | pcap_t *phandle; |
112 | phandle = pcap_open_live((char *)device, 65,0,0,NULL); | 140 | phandle = pcap_open_live((char *)device, 65,0,0,NULL); |
113 | datalinktype = pcap_datalink (phandle); | 141 | datalinktype = pcap_datalink (phandle); |
114 | pcap_close(phandle); | 142 | pcap_close(phandle); |
115 | 143 | ||
116 | if (datalinktype != DLT_IEEE802_11) /* Rawmode is IEEE802_11 */ | 144 | if (datalinktype != DLT_IEEE802_11) /* Rawmode is IEEE802_11 */ |
117 | { | 145 | { |
118 | return 0; | 146 | return 0; |
119 | } | 147 | } |
120 | else | 148 | else |
121 | { | 149 | { |
122 | wl_loginfo("Your successfully listen on %s in 802.11 raw mode", device); | 150 | wl_loginfo("Your successfully listen on %s in 802.11 raw mode", device); |
123 | return 1; | 151 | return 1; |
124 | } | 152 | } |
125 | } | 153 | } |
126 | 154 | ||
127 | /* Set card into promisc mode */ | 155 | /* Set card into promisc mode */ |
128 | int card_set_promisc_up (const char *device) | 156 | int card_set_promisc_up (const char *device) |
129 | { | 157 | { |
130 | int err; | 158 | int err; |
131 | /* First generate a socket to use with iocalls */ | 159 | /* First generate a socket to use with iocalls */ |
132 | int fd = socket(AF_INET, SOCK_DGRAM, 0); | 160 | int fd = socket(AF_INET, SOCK_DGRAM, 0); |
133 | if (fd < 0) | 161 | if (fd < 0) |
134 | { | 162 | { |
135 | /* In case of an error */ | 163 | /* In case of an error */ |
136 | perror("socket"); | 164 | perror("socket"); |
137 | return 0; | 165 | return 0; |
138 | } | 166 | } |
139 | 167 | ||
140 | /* Fill an empty an interface structure with the right flags (UP and Promsic) */ | 168 | /* Fill an empty an interface structure with the right flags (UP and Promsic) */ |
141 | struct ifreq ifr; | 169 | struct ifreq ifr; |
142 | strncpy(ifr.ifr_name, device,10); | 170 | strncpy(ifr.ifr_name, device,10); |
143 | ifr.ifr_flags = IFF_UP + IFF_PROMISC; | 171 | ifr.ifr_flags = IFF_UP + IFF_PROMISC; |
144 | err = ioctl(fd, SIOCSIFFLAGS, &ifr); | 172 | err = ioctl(fd, SIOCSIFFLAGS, &ifr); |
145 | if (err < 0) | 173 | if (err < 0) |
146 | { | 174 | { |
147 | perror("Could not access the interface, "); | 175 | perror("Could not access the interface, "); |
148 | close(fd); | 176 | close(fd); |
149 | return 0; | 177 | return 0; |
150 | } | 178 | } |
151 | 179 | ||
152 | /* Get the informations back from the interface to check if the flags are correct */ | 180 | /* Get the informations back from the interface to check if the flags are correct */ |
153 | strncpy(ifr.ifr_name, device,10); | 181 | strncpy(ifr.ifr_name, device,10); |
154 | err = ioctl(fd, SIOCGIFFLAGS, &ifr); | 182 | err = ioctl(fd, SIOCGIFFLAGS, &ifr); |
155 | if (err < 0) | 183 | if (err < 0) |
156 | { | 184 | { |
157 | perror("Could not access the interface, "); | 185 | perror("Could not access the interface, "); |
158 | close(fd); | 186 | close(fd); |
159 | return 0; | 187 | return 0; |
160 | } | 188 | } |
161 | 189 | ||
162 | if(ifr.ifr_flags && IFF_UP) | 190 | if(ifr.ifr_flags && IFF_UP) |
163 | { | 191 | { |
164 | close(fd); | 192 | close(fd); |
165 | return 1; | 193 | return 1; |
166 | } | 194 | } |
167 | else | 195 | else |
168 | { | 196 | { |
169 | wl_logerr("Could not set promisc flag on %d", device); | 197 | wl_logerr("Could not set promisc flag on %d", device); |
170 | close(fd); | 198 | close(fd); |
171 | return 0; | 199 | return 0; |
172 | } | 200 | } |
173 | } | 201 | } |
174 | 202 | ||