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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/*
*
* Near Field Communication nfctool
*
* Copyright (C) 2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/tcp.h>
#include <linux/ip.h>
#include <errno.h>
#include <glib.h>
#include <near/nfc_copy.h>
#include "nfctool.h"
#include "llcp-decode.h"
#include "sniffer.h"
#define PCAP_MAGIC_NUMBER 0xa1b2c3d4
#define PCAP_MAJOR_VER 2
#define PCAP_MINOR_VER 4
#define PCAP_SNAP_LEN 0xFFFF
#define PCAP_NETWORK 0xF5
#define SNAP_LEN 1024
static GIOChannel *gio_channel = NULL;
guint watch = 0;
static FILE *pcap_file = NULL;
static guint8 *buffer;
static int pcap_file_write_packet(guint8 *data, guint32 len,
struct timeval *timestamp)
{
guint32 val32;
guint32 incl_len;
if (pcap_file == NULL || data == NULL || len == 0)
return -EINVAL;
val32 = timestamp->tv_sec;
if (fwrite(&val32, 4, 1, pcap_file) < 1)
goto exit_err;
val32 = timestamp->tv_usec;
if (fwrite(&val32, 4, 1, pcap_file) < 1)
goto exit_err;
if (len > PCAP_SNAP_LEN)
incl_len = PCAP_SNAP_LEN;
else
incl_len = len;
if (fwrite(&incl_len, 4, 1, pcap_file) < 1)
goto exit_err;
if (fwrite(&len, 4, 1, pcap_file) < 1)
goto exit_err;
if (fwrite(data, 1, incl_len, pcap_file) < incl_len)
goto exit_err;
return 0;
exit_err:
return -errno;
}
static int pcap_file_init(char *pcap_filename)
{
int err = 0;
guint16 value16;
guint32 value32;
pcap_file = fopen(pcap_filename, "w");
if (!pcap_file) {
err = errno;
print_error("Can't open file %s: %s",
pcap_filename, strerror(err));
return -err;
}
value32 = PCAP_MAGIC_NUMBER;
if (fwrite(&value32, 4, 1, pcap_file) < 1)
goto exit_err;
value16 = PCAP_MAJOR_VER;
if (fwrite(&value16, 2, 1, pcap_file) < 1)
goto exit_err;
value16 = PCAP_MINOR_VER;
if (fwrite(&value16, 2, 1, pcap_file) < 1)
goto exit_err;
value32 = 0;
if (fwrite(&value32, 4, 1, pcap_file) < 1)
goto exit_err;
if (fwrite(&value32, 4, 1, pcap_file) < 1)
goto exit_err;
value32 = PCAP_SNAP_LEN;
if (fwrite(&value32, 4, 1, pcap_file) < 1)
goto exit_err;
value32 = PCAP_NETWORK;
if (fwrite(&value32, 4, 1, pcap_file) < 1)
goto exit_err;
return 0;
exit_err:
return -errno;
}
static void pcap_file_cleanup(void)
{
if (pcap_file != NULL) {
fclose(pcap_file);
pcap_file = NULL;
}
}
#define LINE_SIZE (10 + 3 * 16 + 2 + 18 + 1)
#define HUMAN_READABLE_OFFSET 59
/*
* Dumps data in Hex+ASCII format as:
*
* 00000000: 01 01 43 20 30 70 72 6F 70 65 72 74 69 65 73 20 |..C 0properties |
*
*/
void sniffer_print_hexdump(FILE *file, guint8 *data, guint32 len,
gchar *line_prefix, gboolean print_len)
{
guint8 digits;
guint32 offset;
guint32 total;
gchar line[LINE_SIZE];
gchar *hexa = NULL, *human = NULL;
if (len <= 0)
return;
offset = 0;
digits = 0;
total = 0;
if (print_len) {
if (line_prefix)
fprintf(file, "%s", line_prefix);
fprintf(file, "Total length: %u bytes\n", len);
}
while (total < len) {
if (digits == 0) {
memset(line, ' ', HUMAN_READABLE_OFFSET);
sprintf(line, "%08X: ", offset);
offset += 16;
hexa = line + 8 + 2;
human = line + HUMAN_READABLE_OFFSET;
*human++ = '|';
}
sprintf(hexa, "%02hhX ", data[total]);
*human++ = isprint((int)data[total]) ? (char)data[total] : '.';
hexa += 3;
if (++digits >= 16) {
*hexa = ' ';
strcpy(human, "|");
if (line_prefix)
fprintf(file, "%s", line_prefix);
fprintf(file, "%s\n", line);
digits = 0;
}
total++;
}
if ((len & 0xF) != 0) {
*hexa = ' ';
strcpy(human, "|");
if (line_prefix)
fprintf(file, "%s", line_prefix);
fprintf(file, "%s\n", line);
}
}
static gboolean gio_handler(GIOChannel *channel,
GIOCondition cond, gpointer data)
{
struct msghdr msg;
struct iovec iov;
int sock;
int len;
guint8 ctrl[CMSG_SPACE(sizeof(struct timeval))];
struct cmsghdr *cmsg;
struct timeval msg_timestamp;
if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
print_error("Sniffer IO error 0x%x\n", cond);
if (watch > 0)
g_source_remove(watch);
watch = 0;
gio_channel = NULL;
return FALSE;
}
sock = g_io_channel_unix_get_fd(channel);
memset(&msg, 0, sizeof(struct msghdr));
msg.msg_control = &ctrl;
msg.msg_controllen = sizeof(ctrl);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
iov.iov_base = buffer;
iov.iov_len = opts.snap_len;
len = recvmsg(sock, &msg, 0);
if (len < 0) {
print_error("recv: %s", strerror(errno));
return FALSE;
}
cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg && cmsg->cmsg_type == SCM_TIMESTAMP)
memcpy(&msg_timestamp, CMSG_DATA(cmsg), sizeof(struct timeval));
else
gettimeofday(&msg_timestamp, NULL);
llcp_print_pdu(buffer, len, &msg_timestamp);
pcap_file_write_packet(buffer, len, &msg_timestamp);
return TRUE;
}
void sniffer_cleanup(void)
{
DBG("gio_channel: %p", gio_channel);
if (gio_channel) {
g_io_channel_shutdown(gio_channel, TRUE, NULL);
g_io_channel_unref(gio_channel);
gio_channel = NULL;
}
pcap_file_cleanup();
llcp_decode_cleanup();
}
int sniffer_init(void)
{
struct sockaddr_nfc_llcp sockaddr;
int sock = 0;
int err;
int one = 1;
if (opts.snap_len < SNAP_LEN)
opts.snap_len = SNAP_LEN;
buffer = g_malloc(opts.snap_len);
sock = socket(AF_NFC, SOCK_RAW, NFC_SOCKPROTO_LLCP);
if (sock < 0) {
print_error("socket: %s", strerror(errno));
return -1;
}
err = setsockopt(sock, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
if (err < 0)
print_error("setsockopt: %s", strerror(errno));
memset(&sockaddr, 0, sizeof(struct sockaddr_nfc_llcp));
sockaddr.sa_family = AF_NFC;
sockaddr.dev_idx = opts.adapter_idx;
sockaddr.nfc_protocol = NFC_PROTO_NFC_DEP;
err = bind(sock, (struct sockaddr *)&sockaddr,
sizeof(struct sockaddr_nfc_llcp));
if (err < 0) {
print_error("bind: %s", strerror(errno));
goto exit;
}
gio_channel = g_io_channel_unix_new(sock);
g_io_channel_set_close_on_unref(gio_channel, TRUE);
g_io_channel_set_encoding(gio_channel, NULL, NULL);
g_io_channel_set_buffered(gio_channel, FALSE);
watch = g_io_add_watch(gio_channel,
G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
gio_handler, NULL);
g_io_channel_unref(gio_channel);
if (opts.pcap_filename != NULL) {
err = pcap_file_init(opts.pcap_filename);
if (err)
goto exit;
}
err = llcp_decode_init();
if (err)
goto exit;
printf("Start sniffer on nfc%d\n\n", opts.adapter_idx);
exit:
if (err)
sniffer_cleanup();
return err;
}
|