summaryrefslogtreecommitdiffstats
path: root/kismet-dect-v2/packetsource_dect.cc
blob: e6e249f7988b1705c0e98bff824735db820ef85f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
    This file is part of Kismet

    Kismet 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.

    Kismet 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 Kismet; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

	DECT source v2
	(c) 2008 by Mike Kershaw <dragorn (at) kismetwireless (dot) net,
	Jacob Appelbaum <jacob (at) appelbaum (dot) net,
	Christian Fromme <kaner (at) strace (dot) org
*/

#include "config.h"

#include <packetsource.h>
#include "packet_dect.h"
#include "packetsource_dect.h"

PacketSource_Dect::PacketSource_Dect(GlobalRegistry *in_globalreg):
	KisPacketSource(in_globalreg) {

	serial_fd = -1;
	sync = false;
	scan_mode = MODE_ASYNC_FP_SCAN;
}

PacketSource_Dect::PacketSource_Dect(GlobalRegistry *in_globalreg, string in_interface,
									 vector<opt_pair> *in_opts) :
	KisPacketSource(in_globalreg, in_interface, in_opts) {

	serial_fd = -1;
	sync = false;
	scan_mode = -1;

	ParseOptions(in_opts);
}

PacketSource_Dect::~PacketSource_Dect() {
	if (serial_fd >= 0) {
		close(serial_fd);
		serial_fd = -1;
	}
}

int PacketSource_Dect::AutotypeProbe(string in_device) {
	if (StrLower(in_device) == "dect") {
		type = "dect";
		return 1;
	}

	if (StrLower(in_device) == "dectus") {
		type = "dectus";
		return 1;
	}

	return 0;
}

int PacketSource_Dect::ParseOptions(vector<opt_pair> *in_opts) {
	if ((serialdevice = FetchOpt("device", in_opts)) == "") {
		_MSG("DECT packet source defaulting to default serial /dev/coa",
			 MSGFLAG_INFO);
		serialdevice = "/dev/coa";
	} else {
		_MSG("DECT packet source using serial device " + serialdevice,
			 MSGFLAG_INFO);
	}

	return 1;
}

int PacketSource_Dect::FetchDescriptor() {
	return serial_fd;
}

int PacketSource_Dect::SetChannel(unsigned int in_ch) {
	if (ioctl(serial_fd, COA_IOCTL_CHAN, &in_ch)) {
		_MSG("DECT packet source '" + name + "': Failed to set channel: " +
			 string(strerror(errno)), MSGFLAG_ERROR);
		return 0;
	}

	last_channel = in_ch;

	// printf("debug - dect set channel %u\n", in_ch);

	return 1;
}

int PacketSource_Dect::StartScanPp(mac_addr in_rfpi) {
	if (serial_fd == -1)
		return -1;

	/* always accept mode set since we might change RFPI */
	/* remove the sync early since we've disrupted the state of the world */

	if (sync) {
		sync = false;
	}

	/* Set sync sniff mode */
	uint16_t val;
	val = COA_MODE_SNIFF | COA_SUBMODE_SNIFF_SYNC;
	if (ioctl(serial_fd, COA_IOCTL_MODE, &val)) {
		_MSG("DECT packet source '" + name + "': Failed to set sniffer mode PP: " +
			 string(strerror(errno)), MSGFLAG_ERROR);
		return -1;
	}

	/* set RFPI to sync with */
	uint8_t mrfpi[5];
	for (unsigned int x = 0; x < 5; x++)
		mrfpi[x] = in_rfpi[x];

	if (ioctl(serial_fd, COA_IOCTL_SETRFPI, mrfpi)) {
		_MSG("DECT packet source '" + name + "': Failed to set PP RFPI: " +
			 string(strerror(errno)), MSGFLAG_ERROR);
		return -1;
	}

	scan_mode = MODE_ASYNC_PP_SCAN;

	// Caller should turn off PST channel hopping and set channel
	// globalreg->sourcetracker->SetSourceHopping(FetchUUID(), 0, 0);

	return 1;
}

int PacketSource_Dect::StartScanCalls(mac_addr in_rfpi, int in_channel) {
	if (serial_fd == -1)
		return -1;

	if (sync) 
		sync = false;

	/* Set sync sniff mode */
	uint16_t val;

	val = COA_MODE_SNIFF | COA_SUBMODE_SNIFF_SYNC;

	if (ioctl(serial_fd, COA_IOCTL_MODE, &val)) {
		_MSG("DECT packet source '" + name + "': Failed to set sniffer mode SYNC: " +
			 string(strerror(errno)), MSGFLAG_ERROR);
		return -1;
	}

	/* set RFPI we're syncing to */
	uint8_t mrfpi[5];
	for (unsigned int x = 0; x < 5; x++)
		mrfpi[x] = in_rfpi[x];

	if (ioctl(serial_fd, COA_IOCTL_SETRFPI, mrfpi)) {
		_MSG("DECT packet source '" + name + "': Failed to set sniffer sync RFPI: " +
			 string(strerror(errno)), MSGFLAG_ERROR);
		return -1;
	}

	scan_mode = MODE_SYNC_CALL_SCAN;

	// Caller should turn off PST channel hopping
	// globalreg->sourcetracker->SetSourceHopping(FetchUUID(), 0, in_channel);

	return 1;
}

int PacketSource_Dect::StartScanFp() {
	if (serial_fd == -1)
		return -1;

	if (scan_mode == MODE_ASYNC_FP_SCAN)
		return 0;

	/* start sniffer mode */
	int val = COA_MODE_SNIFF | COA_SUBMODE_SNIFF_SCANFP;

	if (ioctl(serial_fd, COA_IOCTL_MODE, &val)) {
		_MSG("DECT packet source '" + name + "': Failed to set sniffer scan FP: " +
			 string(strerror(errno)), MSGFLAG_ERROR);
		return -1;
	}

	scan_mode = MODE_ASYNC_FP_SCAN;

	if (sync) 
		sync = false;

	// Caller should turn on hopping
	// globalreg->sourcetracker->SetSourceHopping(FetchUUID(), 1, 0);

	return 1;
}

int PacketSource_Dect::OpenSource() {
	if ((serial_fd = open(serialdevice.c_str(), O_RDONLY)) == -1) {
		_MSG("DECT packet source '" + name + "': Failed to open DECT serial device: " +
			 string(strerror(errno)), MSGFLAG_ERROR);
		return 0;
	}

	return StartScanFp();
}

int PacketSource_Dect::CloseSource() {
	if (serial_fd >= 0) {
		close(serial_fd);
		serial_fd = -1;
	}

	return 1;
}

int PacketSource_Dect::Poll() {
	unsigned char buf[7];
	int rbytes = -1;
	dect_packinfo *pi = new dect_packinfo;
	kis_packet *newpack;
	kis_datachunk *dectchunk = NULL; 

	pi->sync = sync;
	pi->scanmode = scan_mode;
	pi->channel = FetchChannel();

	if (scan_mode == MODE_ASYNC_FP_SCAN || scan_mode == MODE_ASYNC_PP_SCAN) {
		if ((rbytes = read(serial_fd, &(pi->sdata), sizeof(dect_data_scan_t))) !=
			 sizeof(dect_data_scan_t)) {
			_MSG("DECT packet source '" + name + "': Failed to read scan data: " +
				 string(strerror(errno)), MSGFLAG_ERROR);
			delete(pi);
			return 0;
		}

		// Make a scan chunk
		dectchunk = new kis_datachunk;
		dectchunk->length = sizeof(dect_data_scan_t);
		dectchunk->data = new uint8_t[dectchunk->length];
		memcpy(dectchunk->data, &(pi->sdata), dectchunk->length);
		dectchunk->source_id = source_id;
		dectchunk->dlt = KDLT_DECTSCAN;

	} else if (scan_mode == MODE_SYNC_CALL_SCAN) {
		if ((rbytes = read(serial_fd, &(pi->pdata), sizeof(pp_packet_t))) !=
			 sizeof(pp_packet_t)) {
			_MSG("DECT packet source '" + name + "': Failed to read scan data: " +
				 string(strerror(errno)), MSGFLAG_ERROR);
			delete(pi);
			return 0;
		} else {
			if (!sync) {
				_MSG("DECT packet source '" + name + "': Synced to call", MSGFLAG_INFO);
				sync = true;
			}

			// Make a data chunk
			dectchunk = new kis_datachunk;
			dectchunk->length = 53;
			dectchunk->data = new uint8_t[dectchunk->length];
			memcpy(dectchunk->data, pi->pdata.data, dectchunk->length);
			dectchunk->source_id = source_id;
			dectchunk->dlt = KDLT_DECTCALL;
		}
	} else {
		// Unknown mode during poll
		delete(pi);
		return 0;
	}

	newpack = globalreg->packetchain->GeneratePacket();

	kis_ref_capsource *csrc_ref = new kis_ref_capsource;
	csrc_ref->ref_source = this;
	newpack->insert(_PCM(PACK_COMP_KISCAPSRC), csrc_ref);

	newpack->insert(pack_comp_dect, pi);
	if (dectchunk) 
		newpack->insert(_PCM(PACK_COMP_LINKFRAME), dectchunk);
	globalreg->packetchain->ProcessPacket(newpack);

	// printf("debug - dect newpack\n");

	return 1;
}