nfc-relay.c 8.47 KB
Newer Older
1
/*-
2
 * Free/Libre Near Field Communication (NFC) library
3
 *
4 5 6 7 8 9
 * Libnfc historical contributors:
 * Copyright (C) 2009      Roel Verdult
 * Copyright (C) 2009-2013 Romuald Conty
 * Copyright (C) 2010-2012 Romain Tartière
 * Copyright (C) 2010-2013 Philippe Teuwen
 * Copyright (C) 2012-2013 Ludovic Rousseau
10
 * See AUTHORS file for a more comprehensive list of contributors.
11
 * Additional contributors of this file:
12
 *
13 14 15
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  1) Redistributions of source code must retain the above copyright notice,
16
 *  this list of conditions and the following disclaimer.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *  2 )Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
32
 *
33
 * Note that this license only applies on the examples, NFC library itself is under LGPL
34
 *
35 36 37
 */

/**
38
 * @file nfc-relay.c
39
 * @brief Relay example using two devices.
40
 */
Roel Verdult's avatar
Roel Verdult committed
41

42
#ifdef HAVE_CONFIG_H
43
#  include "config.h"
44
#endif /* HAVE_CONFIG_H */
45

46
#include <inttypes.h>
Roel Verdult's avatar
Roel Verdult committed
47 48
#include <stdio.h>
#include <stdlib.h>
49
#include <stdint.h>
Roel Verdult's avatar
Roel Verdult committed
50
#include <string.h>
51
#include <signal.h>
52

53
#include <nfc/nfc.h>
54

55
#include "utils/nfc-utils.h"
Roel Verdult's avatar
Roel Verdult committed
56

57
#define MAX_FRAME_LEN 264
58
#define MAX_DEVICE_COUNT 2
59

60 61
static uint8_t abtReaderRx[MAX_FRAME_LEN];
static uint8_t abtReaderRxPar[MAX_FRAME_LEN];
62
static int szReaderRxBits;
63 64
static uint8_t abtTagRx[MAX_FRAME_LEN];
static uint8_t abtTagRxPar[MAX_FRAME_LEN];
65
static int szTagRxBits;
66 67
static nfc_device *pndReader;
static nfc_device *pndTag;
68
static bool quitting = false;
69

Ludovic Rousseau's avatar
Ludovic Rousseau committed
70
static void
71
intr_hdlr(int sig)
72
{
73
  (void) sig;
74
  printf("\nQuitting...\n");
75
  quitting = true;
76 77
  return;
}
Roel Verdult's avatar
Roel Verdult committed
78

Ludovic Rousseau's avatar
Ludovic Rousseau committed
79
static void
80
print_usage(char *argv[])
81
{
82 83 84 85
  printf("Usage: %s [OPTIONS]\n", argv[0]);
  printf("Options:\n");
  printf("\t-h\tHelp. Print this message.\n");
  printf("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
86 87
}

88
int
89
main(int argc, char *argv[])
90
{
91 92
  int     arg;
  bool    quiet_output = false;
93
  const char *acLibnfcVersion = nfc_version();
94 95

  // Get commandline options
96
  for (arg = 1; arg < argc; arg++) {
97 98
    if (0 == strcmp(argv[arg], "-h")) {
      print_usage(argv);
99
      exit(EXIT_SUCCESS);
100
    } else if (0 == strcmp(argv[arg], "-q")) {
101
      quiet_output = true;
102
    } else {
103 104
      ERR("%s is not supported option.", argv[arg]);
      print_usage(argv);
105
      exit(EXIT_FAILURE);
106
    }
107
  }
108

109
  // Display libnfc version
110
  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
111

112
#ifdef WIN32
113
  signal(SIGINT, (void (__cdecl *)(int)) intr_hdlr);
114
#else
115
  signal(SIGINT, intr_hdlr);
116 117
#endif

118 119
  nfc_context *context;
  nfc_init(&context);
120 121 122 123
  if (context == NULL) {
    ERR("Unable to init libnfc (malloc)");
    exit(EXIT_FAILURE);
  }
124
  nfc_connstring connstrings[MAX_DEVICE_COUNT];
125
  // List available devices
126
  size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
127 128

  if (szFound < 2) {
129
    ERR("%" PRIdPTR " device found but two opened devices are needed to relay NFC.", szFound);
130 131
    nfc_exit(context);
    exit(EXIT_FAILURE);
132
  }
133

Roel Verdult's avatar
Roel Verdult committed
134
  // Try to open the NFC emulator device
135
  pndTag = nfc_open(context, connstrings[0]);
136
  if (pndTag == NULL) {
137
    ERR("Error opening NFC emulator device");
138 139
    nfc_exit(context);
    exit(EXIT_FAILURE);
Roel Verdult's avatar
Roel Verdult committed
140
  }
141

142
  printf("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n");
143

144 145 146 147
  printf("NFC emulator device: %s opened\n", nfc_device_get_name(pndTag));
  printf("[+] Try to break out the auto-emulation, this requires a second reader!\n");
  printf("[+] To do this, please send any command after the anti-collision\n");
  printf("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n");
148

149
  nfc_target nt = {
150 151 152 153 154 155 156
    .nm = {
      .nmt = NMT_ISO14443A,
      .nbr = NBR_UNDEFINED,
    },
    .nti = {
      .nai = {
        .abtAtqa = { 0x04, 0x00 },
Philippe Teuwen's avatar
Philippe Teuwen committed
157
        .abtUid = { 0x08, 0xad, 0xbe, 0xef },
158 159 160 161 162
        .btSak = 0x20,
        .szUidLen = 4,
        .szAtsLen = 0,
      },
    },
163 164
  };

165 166 167
  if ((szReaderRxBits = nfc_target_init(pndTag, &nt, abtReaderRx, sizeof(abtReaderRx), 0)) < 0) {
    ERR("%s", "Initialization of NFC emulator failed");
    nfc_close(pndTag);
168
    nfc_exit(context);
169
    exit(EXIT_FAILURE);
170
  }
171 172 173 174
  printf("%s", "Configuring emulator settings...");
  if ((nfc_device_set_property_bool(pndTag, NP_HANDLE_CRC, false) < 0) ||
      (nfc_device_set_property_bool(pndTag, NP_HANDLE_PARITY, false) < 0) || (nfc_device_set_property_bool(pndTag, NP_ACCEPT_INVALID_FRAMES, true)) < 0) {
    nfc_perror(pndTag, "nfc_device_set_property_bool");
175 176
    nfc_close(pndTag);
    nfc_exit(context);
177
    exit(EXIT_FAILURE);
178
  }
179
  printf("%s", "Done, emulated tag is initialized");
Roel Verdult's avatar
Roel Verdult committed
180 181

  // Try to open the NFC reader
182
  pndReader = nfc_open(context, connstrings[1]);
183 184 185 186 187 188
  if (pndReader == NULL) {
    printf("Error opening NFC reader device\n");
    nfc_close(pndTag);
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }
189

190 191
  printf("NFC reader device: %s opened", nfc_device_get_name(pndReader));
  printf("%s", "Configuring NFC reader settings...");
192

193 194
  if (nfc_initiator_init(pndReader) < 0) {
    nfc_perror(pndReader, "nfc_initiator_init");
195 196 197
    nfc_close(pndTag);
    nfc_close(pndReader);
    nfc_exit(context);
198
    exit(EXIT_FAILURE);
199
  }
200 201 202 203
  if ((nfc_device_set_property_bool(pndReader, NP_HANDLE_CRC, false) < 0) ||
      (nfc_device_set_property_bool(pndReader, NP_HANDLE_PARITY, false) < 0) ||
      (nfc_device_set_property_bool(pndReader, NP_ACCEPT_INVALID_FRAMES, true)) < 0) {
    nfc_perror(pndReader, "nfc_device_set_property_bool");
204 205 206
    nfc_close(pndTag);
    nfc_close(pndReader);
    nfc_exit(context);
207
    exit(EXIT_FAILURE);
208
  }
209
  printf("%s", "Done, relaying frames now!");
Roel Verdult's avatar
Roel Verdult committed
210

211
  while (!quitting) {
Roel Verdult's avatar
Roel Verdult committed
212
    // Test if we received a frame from the reader
213
    if ((szReaderRxBits = nfc_target_receive_bits(pndTag, abtReaderRx, sizeof(abtReaderRx), abtReaderRxPar)) > 0) {
Roel Verdult's avatar
Roel Verdult committed
214
      // Drop down the field before sending a REQA command and start a new session
215
      if (szReaderRxBits == 7 && abtReaderRx[0] == 0x26) {
Roel Verdult's avatar
Roel Verdult committed
216
        // Drop down field for a very short time (original tag will reboot)
217 218
        if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, false) < 0) {
          nfc_perror(pndReader, "nfc_device_set_property_bool");
219 220 221
          nfc_close(pndTag);
          nfc_close(pndReader);
          nfc_exit(context);
222
          exit(EXIT_FAILURE);
223
        }
224
        if (!quiet_output)
225 226 227
          printf("\n");
        if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, true) < 0) {
          nfc_perror(pndReader, "nfc_device_set_property_bool");
228 229 230
          nfc_close(pndTag);
          nfc_close(pndReader);
          nfc_exit(context);
231
          exit(EXIT_FAILURE);
232
        }
Roel Verdult's avatar
Roel Verdult committed
233 234
      }
      // Print the reader frame to the screen
235
      if (!quiet_output) {
236 237
        printf("R: ");
        print_hex_par(abtReaderRx, (size_t) szReaderRxBits, abtReaderRxPar);
238
      }
Roel Verdult's avatar
Roel Verdult committed
239
      // Forward the frame to the original tag
240
      if ((szTagRxBits = nfc_initiator_transceive_bits
241
                         (pndReader, abtReaderRx, (size_t) szReaderRxBits, abtReaderRxPar, abtTagRx, sizeof(abtTagRx), abtTagRxPar)) > 0) {
Roel Verdult's avatar
Roel Verdult committed
242
        // Redirect the answer back to the reader
243 244
        if (nfc_target_send_bits(pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) {
          nfc_perror(pndTag, "nfc_target_send_bits");
245 246 247
          nfc_close(pndTag);
          nfc_close(pndReader);
          nfc_exit(context);
248
          exit(EXIT_FAILURE);
249
        }
Roel Verdult's avatar
Roel Verdult committed
250
        // Print the tag frame to the screen
251
        if (!quiet_output) {
252 253
          printf("T: ");
          print_hex_par(abtTagRx, szTagRxBits, abtTagRxPar);
254
        }
Roel Verdult's avatar
Roel Verdult committed
255 256 257 258
      }
    }
  }

259 260
  nfc_close(pndTag);
  nfc_close(pndReader);
261
  nfc_exit(context);
262
  exit(EXIT_SUCCESS);
Roel Verdult's avatar
Roel Verdult committed
263
}