Diffstat (limited to 'noncore/net/wellenreiter/libwellenreiter/source/sniff.cc') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/wellenreiter/libwellenreiter/source/sniff.cc | 303 |
1 files changed, 303 insertions, 0 deletions
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/sniff.cc b/noncore/net/wellenreiter/libwellenreiter/source/sniff.cc new file mode 100644 index 0000000..fedd8fc --- a/dev/null +++ b/noncore/net/wellenreiter/libwellenreiter/source/sniff.cc | |||
@@ -0,0 +1,303 @@ | |||
1 | /* | ||
2 | * rfmon mode sniffer | ||
3 | * | ||
4 | * $Id$ | ||
5 | */ | ||
6 | |||
7 | #include "sniff.hh" | ||
8 | #include "ieee802_11.hh" | ||
9 | #include "extract.hh" | ||
10 | #include "log.hh" | ||
11 | |||
12 | /* Main function, checks packets */ | ||
13 | void process_packets(const struct pcap_pkthdr *pkthdr, const unsigned char *packet) | ||
14 | { | ||
15 | unsigned int caplen = pkthdr->caplen; | ||
16 | unsigned int length = pkthdr->len; | ||
17 | u_int16_t fc; | ||
18 | unsigned int HEADER_LENGTH; | ||
19 | |||
20 | /* pinfo holds all interresting information for us */ | ||
21 | struct packetinfo pinfo; | ||
22 | struct packetinfo *pinfoptr; | ||
23 | pinfoptr=&pinfo; | ||
24 | |||
25 | pinfoptr->isvalid = 0; | ||
26 | pinfoptr->pktlen = pkthdr->len; | ||
27 | |||
28 | if (caplen < IEEE802_11_FC_LEN) | ||
29 | { | ||
30 | /* This is a garbage packet, because is does not long enough | ||
31 | to hold a 802.11b header */ | ||
32 | pinfoptr->isvalid = 0; | ||
33 | return; | ||
34 | } | ||
35 | |||
36 | /* Gets the framecontrol bits (2bytes long) */ | ||
37 | fc = EXTRACT_LE_16BITS(packet); | ||
38 | |||
39 | HEADER_LENGTH = GetHeaderLength(fc); | ||
40 | |||
41 | if (caplen < HEADER_LENGTH) | ||
42 | { | ||
43 | /* This is a garbage packet, because it is not long enough | ||
44 | to hold a correct header of its type */ | ||
45 | pinfoptr->isvalid = 0; | ||
46 | return; | ||
47 | } | ||
48 | |||
49 | /* Decode 802.11b header out of the packet */ | ||
50 | if (decode_80211b_hdr(packet,pinfoptr) == 0) | ||
51 | { | ||
52 | /* Justification of the ofset to further process the packet */ | ||
53 | length -= HEADER_LENGTH; | ||
54 | caplen -= HEADER_LENGTH; | ||
55 | packet += HEADER_LENGTH; | ||
56 | } | ||
57 | else /* Something is wrong,could not be a correct packet */ | ||
58 | return; | ||
59 | |||
60 | switch (FC_TYPE(fc)) | ||
61 | { | ||
62 | /* Is it a managemnet frame? */ | ||
63 | case T_MGMT: | ||
64 | switch (FC_SUBTYPE(fc)) | ||
65 | { | ||
66 | case ST_BEACON: | ||
67 | if (handle_beacon(fc, packet,pinfoptr) ==0) | ||
68 | { | ||
69 | if (!strcmp(pinfoptr->desthwaddr,"ff:ff:ff:ff:ff:ff") == 0) | ||
70 | { | ||
71 | /* Every beacon must have the broadcast as destination | ||
72 | so it must be a shitti packet */ | ||
73 | pinfoptr->isvalid = 0; | ||
74 | return; | ||
75 | } | ||
76 | |||
77 | if (pinfoptr->cap_ESS == pinfoptr->cap_IBSS) | ||
78 | { | ||
79 | /* Only one of both are possible, so must be | ||
80 | a noise packet, if this comes up */ | ||
81 | pinfoptr->isvalid = 0; | ||
82 | return; | ||
83 | } | ||
84 | if (pinfoptr->channel < 1 || pinfoptr->channel > 14) | ||
85 | { | ||
86 | /* Only channels between 1 and 14 are possible | ||
87 | others must be noise packets */ | ||
88 | pinfoptr->isvalid = 0; | ||
89 | return; | ||
90 | } | ||
91 | |||
92 | |||
93 | /* Here should be the infos to the gui issued */ | ||
94 | if (pinfoptr->cap_ESS == 1 &&pinfoptr->cap_IBSS ==0) | ||
95 | { | ||
96 | printf ("\nHave found an accesspoint:"); | ||
97 | } | ||
98 | else if(pinfoptr->cap_ESS == 0 && pinfoptr->cap_IBSS == 1) | ||
99 | { | ||
100 | printf ("\nHave found an AD-HOC station:"); | ||
101 | |||
102 | } | ||
103 | if (strcmp (pinfoptr->ssid,NONBROADCASTING) ==0) | ||
104 | { | ||
105 | printf ("\n\tOn a non-broadcasting network"); | ||
106 | } | ||
107 | else | ||
108 | { | ||
109 | printf ("\n\tOn network : %s",pinfoptr->ssid); | ||
110 | } | ||
111 | printf ("\n\tLen SSID : %d",pinfoptr->ssid_len); | ||
112 | printf ("\n\tOn Channel : %d",pinfoptr->channel); | ||
113 | printf ("\n\tEncryption : %s", pinfoptr->cap_WEP ? "ON" : "OFF"); | ||
114 | printf ("\n\tMacaddress : %s",pinfoptr->sndhwaddr); | ||
115 | printf ("\n\tBssid : %s",pinfoptr->bssid); | ||
116 | printf ("\n\tDest : %s\n",pinfoptr->desthwaddr); | ||
117 | } | ||
118 | break; | ||
119 | default: | ||
120 | wl_logerr("Unknown IEEE802.11 frame subtype (%d)", FC_SUBTYPE(fc)); | ||
121 | break; | ||
122 | } /* End of switch over different mgt frame types */ | ||
123 | |||
124 | break; | ||
125 | |||
126 | case T_CTRL: | ||
127 | wl_loginfo("Received control frame, not implemented yet"); | ||
128 | break; | ||
129 | |||
130 | case T_DATA: | ||
131 | wl_loginfo("Received date frame, not implemented yet"); | ||
132 | break; | ||
133 | |||
134 | default: | ||
135 | wl_logerr("Unknown IEEE802.11 frame type (%d)", FC_TYPE(fc)); | ||
136 | break; | ||
137 | } | ||
138 | } | ||
139 | |||
140 | /* This decodes the 802.11b frame header out of the 802.11b packet | ||
141 | all the infos is placed into the packetinfo structure */ | ||
142 | int decode_80211b_hdr(const u_char *p,struct packetinfo *ppinfo) | ||
143 | { | ||
144 | const struct mgmt_header_t *mgthdr = (const struct mgmt_header_t *) p; | ||
145 | ppinfo->fcsubtype = FC_SUBTYPE(mgthdr->fc); | ||
146 | |||
147 | /* Get the sender, bssid and dest mac address */ | ||
148 | etheraddr_string(mgthdr->bssid,ppinfo->bssid); | ||
149 | etheraddr_string(mgthdr->da,ppinfo->desthwaddr); | ||
150 | etheraddr_string(mgthdr->sa,ppinfo->sndhwaddr); | ||
151 | ppinfo->fc_wep = FC_WEP(mgthdr->fc); | ||
152 | return 0; | ||
153 | } | ||
154 | |||
155 | |||
156 | void etheraddr_string(register const u_char *ep, char *text) | ||
157 | { | ||
158 | static char hex[] = "0123456789abcdef"; | ||
159 | register unsigned int i, j; | ||
160 | register char *cp; | ||
161 | char buf[sizeof("00:00:00:00:00:00")]; | ||
162 | cp = buf; | ||
163 | if ((j = *ep >> 4) != 0) | ||
164 | *cp++ = hex[j]; | ||
165 | *cp++ = hex[*ep++ & 0xf]; | ||
166 | for (i = 5; (int)--i >= 0;) { | ||
167 | *cp++ = ':'; | ||
168 | if ((j = *ep >> 4) != 0) | ||
169 | *cp++ = hex[j]; | ||
170 | *cp++ = hex[*ep++ & 0xf]; | ||
171 | } | ||
172 | *cp = '\0'; | ||
173 | strcpy(text,buf); | ||
174 | } | ||
175 | |||
176 | /* beacon handler */ | ||
177 | int handle_beacon(u_int16_t fc, const u_char *p,struct packetinfo *ppinfo) | ||
178 | { | ||
179 | struct mgmt_body_t pbody; | ||
180 | int offset = 0; | ||
181 | |||
182 | /* Get the static informations out of the packet */ | ||
183 | memset(&pbody, 0, sizeof(pbody)); | ||
184 | memcpy(&pbody.timestamp, p, 8); | ||
185 | offset += 8; | ||
186 | pbody.beacon_interval = EXTRACT_LE_16BITS(p+offset); | ||
187 | offset += 2; | ||
188 | pbody.capability_info = EXTRACT_LE_16BITS(p+offset); | ||
189 | offset += 2; | ||
190 | |||
191 | /* Gets the different flags out of the capabilities */ | ||
192 | ppinfo->cap_ESS = CAPABILITY_ESS(pbody.capability_info); | ||
193 | ppinfo->cap_IBSS = CAPABILITY_IBSS(pbody.capability_info); | ||
194 | ppinfo->cap_WEP = CAPABILITY_PRIVACY(pbody.capability_info); | ||
195 | |||
196 | /* Gets the tagged elements out of the packets */ | ||
197 | while (offset + 1 < ppinfo->pktlen) | ||
198 | { | ||
199 | switch (*(p + offset)) | ||
200 | { | ||
201 | case E_SSID: | ||
202 | memcpy(&(pbody.ssid),p+offset,2); offset += 2; | ||
203 | if (pbody.ssid.length > 0) | ||
204 | { | ||
205 | memcpy(&(pbody.ssid.ssid),p+offset,pbody.ssid.length); offset += pbody.ssid.length; | ||
206 | pbody.ssid.ssid[pbody.ssid.length]='\0'; | ||
207 | if (strcmp((char *)pbody.ssid.ssid,"")==0) | ||
208 | ppinfo->ssid = NONBROADCASTING; | ||
209 | else | ||
210 | ppinfo->ssid = (char *)pbody.ssid.ssid; | ||
211 | ppinfo->ssid_len = pbody.ssid.length; | ||
212 | } | ||
213 | break; | ||
214 | |||
215 | case E_CHALLENGE: | ||
216 | memcpy(&(pbody.challenge),p+offset,2); offset += 2; | ||
217 | if (pbody.challenge.length > 0) | ||
218 | { | ||
219 | memcpy(&(pbody.challenge.text),p+offset,pbody.challenge.length); offset += pbody.challenge.length; | ||
220 | pbody.challenge.text[pbody.challenge.length]='\0'; | ||
221 | } | ||
222 | break; | ||
223 | case E_RATES: | ||
224 | memcpy(&(pbody.rates),p+offset,2); offset += 2; | ||
225 | if (pbody.rates.length > 0) | ||
226 | { | ||
227 | memcpy(&(pbody.rates.rate),p+offset,pbody.rates.length); offset += pbody.rates.length; | ||
228 | } | ||
229 | break; | ||
230 | case E_DS: | ||
231 | memcpy(&(pbody.ds),p+offset,3); offset +=3; | ||
232 | ppinfo->channel = pbody.ds.channel; | ||
233 | break; | ||
234 | case E_CF: | ||
235 | memcpy(&(pbody.cf),p+offset,8); offset +=8; | ||
236 | break; | ||
237 | case E_TIM: | ||
238 | memcpy(&(pbody.tim),p+offset,2); offset +=2; | ||
239 | memcpy(&(pbody.tim.count),p+offset,3); offset +=3; | ||
240 | if ((pbody.tim.length -3) > 0) | ||
241 | { | ||
242 | memcpy((pbody.tim.bitmap),p+(pbody.tim.length -3),(pbody.tim.length -3)); | ||
243 | offset += pbody.tim.length -3; | ||
244 | } | ||
245 | break; | ||
246 | default: | ||
247 | |||
248 | offset+= *(p+offset+1) + 2; | ||
249 | break; | ||
250 | } /* end of switch*/ | ||
251 | } /* end of for loop */ | ||
252 | return 0; | ||
253 | |||
254 | } /* End of handle_beacon */ | ||
255 | |||
256 | |||
257 | int GetHeaderLength(u_int16_t fc) | ||
258 | { | ||
259 | int iLength=0; | ||
260 | |||
261 | switch (FC_TYPE(fc)) | ||
262 | { | ||
263 | case T_MGMT: | ||
264 | iLength = MGMT_HEADER_LEN; | ||
265 | break; | ||
266 | case T_CTRL: | ||
267 | switch (FC_SUBTYPE(fc)) | ||
268 | { | ||
269 | case CTRL_PS_POLL: | ||
270 | iLength = CTRL_PS_POLL_LEN; | ||
271 | break; | ||
272 | case CTRL_RTS: | ||
273 | iLength = CTRL_RTS_LEN; | ||
274 | break; | ||
275 | case CTRL_CTS: | ||
276 | iLength = CTRL_CTS_LEN; | ||
277 | break; | ||
278 | case CTRL_ACK: | ||
279 | iLength = CTRL_ACK_LEN; | ||
280 | break; | ||
281 | case CTRL_CF_END: | ||
282 | iLength = CTRL_END_LEN; | ||
283 | break; | ||
284 | case CTRL_END_ACK: | ||
285 | iLength = CTRL_END_ACK_LEN; | ||
286 | break; | ||
287 | default: | ||
288 | iLength = 0; | ||
289 | break; | ||
290 | } | ||
291 | break; | ||
292 | case T_DATA: | ||
293 | if (FC_TO_DS(fc) && FC_FROM_DS(fc)) | ||
294 | iLength = 30; | ||
295 | else | ||
296 | iLength = 24; | ||
297 | break; | ||
298 | default: | ||
299 | wl_logerr("unknown IEEE802.11 frame type (%d)", FC_TYPE(fc)); | ||
300 | break; | ||
301 | } | ||
302 | return iLength; | ||
303 | } | ||