aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDuncan <duncan@dell-laptop.lan>2011-04-28 19:11:22 +0100
committerDuncan <duncan@dell-laptop.lan>2011-04-28 19:11:22 +0100
commit52b8a86a7cd3dfbf89ecbc2e5320b476943a2929 (patch)
tree557ebf8ec1a5373a98e272970d813b3848e3263c
Initial import
-rw-r--r--COPYING340
-rw-r--r--README29
-rw-r--r--src/.gitignore3
-rw-r--r--src/Makefile13
-rw-r--r--src/gsmtap.h73
-rw-r--r--src/main.c679
6 files changed, 1137 insertions, 0 deletions
diff --git a/COPYING b/COPYING
new file mode 100644
index 0000000..7a8e8ab
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,340 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users. This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it. (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.) You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have. You must make sure that they, too, receive or can get the
+source code. And you must show them these terms so they know their
+rights.
+
+ We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+ Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software. If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary. To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ GNU GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License. The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language. (Hereinafter, translation is included without limitation in
+the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+ 1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+ 2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ b) You must cause any work that you distribute or publish, that in
+ whole or in part contains or is derived from the Program or any
+ part thereof, to be licensed as a whole at no charge to all third
+ parties under the terms of this License.
+
+ c) If the modified program normally reads commands interactively
+ when run, you must cause it, when started running for such
+ interactive use in the most ordinary way, to print or display an
+ announcement including an appropriate copyright notice and a
+ notice that there is no warranty (or else, saying that you provide
+ a warranty) and that users may redistribute the program under
+ these conditions, and telling the user how to view a copy of this
+ License. (Exception: if the Program itself is interactive but
+ does not normally print such an announcement, your work based on
+ the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+ a) Accompany it with the complete corresponding machine-readable
+ source code, which must be distributed under the terms of Sections
+ 1 and 2 above on a medium customarily used for software interchange; or,
+
+ b) Accompany it with a written offer, valid for at least three
+ years, to give any third party, for a charge no more than your
+ cost of physically performing source distribution, a complete
+ machine-readable copy of the corresponding source code, to be
+ distributed under the terms of Sections 1 and 2 above on a medium
+ customarily used for software interchange; or,
+
+ c) Accompany it with the information you received as to the offer
+ to distribute corresponding source code. (This alternative is
+ allowed only for noncommercial distribution and only if you
+ received the program in object code or executable form with such
+ an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it. For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable. However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License. Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+ 5. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Program or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+ 6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+ 7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all. For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation. If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+ 10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission. For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this. Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+ NO WARRANTY
+
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program 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.
+
+ 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 Street, Fifth Floor, Boston, MA 02110-1301, USA
+
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+ Gnomovision version 69, Copyright (C) year name of author
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+ <signature of Ty Coon>, 1 April 1989
+ Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs. If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.
diff --git a/README b/README
new file mode 100644
index 0000000..e44607d
--- /dev/null
+++ b/README
@@ -0,0 +1,29 @@
+DCT3-GSMTAP
+===========
+
+DCT3-GSMTAP is a command line utility to capture the GSM Um and SIM-ME
+interfaces from a Nokia DCT3 phone (eg. 3310) and forward via GSMTAP to the
+Wireshark protocol analyzer. It is released under GNU GPL version 2.
+
+It is heavily based on Gammu <http://www.gammu.org> and Project Blacksphere:
+
+<http://nokix.pasjagsm.pl/help/blacksphere/sub_200nokiaos.htm>
+This site is the result of a great deal of assembly code reading, research,
+countless (mostly futile) searches for data sheets, cross-referencing and
+analysing. If you use this information in any way please mention wumpus
+<blacksphere@goliath.darktech.org> (and others in the credits section) in the
+credits of your program/document. And tell me :) If you have more information
+please contribute. If you just copy this, stick your name on it and call it
+yours I hope you get your genitals bitten off by a three headed monkey. Have a
+nice day.
+
+A serial/USB FBUS cable is required.
+
+Usage
+=====
+
+dct3-gsmtap device host
+
+The device should be something like /dev/ttyUSB0 (check dmesg once plugged in
+for exact name) or /dev/ttyS0. The host is where to send the GSMTAP
+packets to (224.0.0.1 is a good option).
diff --git a/src/.gitignore b/src/.gitignore
new file mode 100644
index 0000000..d8748b4
--- /dev/null
+++ b/src/.gitignore
@@ -0,0 +1,3 @@
+*.o
+dct3-gsmtap
+
diff --git a/src/Makefile b/src/Makefile
new file mode 100644
index 0000000..cc62fd1
--- /dev/null
+++ b/src/Makefile
@@ -0,0 +1,13 @@
+CFLAGS=-Wall -Werror
+
+all: dct3-gsmtap
+
+dct3tap: main.o
+ $(CC) $(LDFLAGS) -o $@ $^
+
+%.o: %.c
+ $(CC) $(CFLAGS) -o $@ -c $^
+
+clean:
+ @rm -f dct3-gsmtap *.o
+
diff --git a/src/gsmtap.h b/src/gsmtap.h
new file mode 100644
index 0000000..3f384a4
--- /dev/null
+++ b/src/gsmtap.h
@@ -0,0 +1,73 @@
+#ifndef _GSMTAP_H
+#define _GSMTAP_H
+
+/* gsmtap header, pseudo-header in front of the actua GSM payload */
+
+/* GSMTAP is a generic header format for GSM protocol captures,
+ * it uses the IANA-assigned UDP port number 4729 and carries
+ * payload in various formats of GSM interfaces such as Um MAC
+ * blocks or Um bursts.
+ *
+ * Example programs generating GSMTAP data are airprobe
+ * (http://airprobe.org/) or OsmocomBB (http://bb.osmocom.org/)
+ */
+
+#include <stdint.h>
+
+#define GSMTAP_VERSION 0x02
+
+#define GSMTAP_TYPE_UM 0x01
+#define GSMTAP_TYPE_ABIS 0x02
+#define GSMTAP_TYPE_UM_BURST 0x03 /* raw burst bits */
+#define GSMTAP_TYPE_SIM 0x04
+
+#define GSMTAP_BURST_UNKNOWN 0x00
+#define GSMTAP_BURST_FCCH 0x01
+#define GSMTAP_BURST_PARTIAL_SCH 0x02
+#define GSMTAP_BURST_SCH 0x03
+#define GSMTAP_BURST_CTS_SCH 0x04
+#define GSMTAP_BURST_COMPACT_SCH 0x05
+#define GSMTAP_BURST_NORMAL 0x06
+#define GSMTAP_BURST_DUMMY 0x07
+#define GSMTAP_BURST_ACCESS 0x08
+#define GSMTAP_BURST_NONE 0x09
+
+#define GSMTAP_CHANNEL_UNKNOWN 0x00
+#define GSMTAP_CHANNEL_BCCH 0x01
+#define GSMTAP_CHANNEL_CCCH 0x02
+#define GSMTAP_CHANNEL_RACH 0x03
+#define GSMTAP_CHANNEL_AGCH 0x04
+#define GSMTAP_CHANNEL_PCH 0x05
+#define GSMTAP_CHANNEL_SDCCH 0x06
+#define GSMTAP_CHANNEL_SDCCH4 0x07
+#define GSMTAP_CHANNEL_SDCCH8 0x08
+#define GSMTAP_CHANNEL_TCH_F 0x09
+#define GSMTAP_CHANNEL_TCH_H 0x0a
+#define GSMTAP_CHANNEL_ACCH 0x80
+
+#define GSMTAP_ARFCN_F_PCS 0x8000
+#define GSMTAP_ARFCN_F_UPLINK 0x4000
+#define GSMTAP_ARFCN_MASK 0x3fff
+
+#define GSMTAP_UDP_PORT 4729
+
+struct gsmtap_hdr {
+ uint8_t version; /* version, set to 0x01 currently */
+ uint8_t hdr_len; /* length in number of 32bit words */
+ uint8_t type; /* see GSMTAP_TYPE_* */
+ uint8_t timeslot; /* timeslot (0..7 on Um) */
+
+ uint16_t arfcn; /* ARFCN (frequency) */
+ int8_t signal_dbm; /* signal level in dBm */
+ int8_t snr_db; /* signal/noise ratio in dB */
+
+ uint32_t frame_number; /* GSM Frame Number (FN) */
+
+ uint8_t sub_type; /* Type of burst/channel, see above */
+ uint8_t antenna_nr; /* Antenna Number */
+ uint8_t sub_slot; /* sub-slot within timeslot */
+ uint8_t res; /* reserved for future use (RFU) */
+
+} __attribute__((packed));
+
+#endif /* _GSMTAP_H */
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..343891f
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,679 @@
+/* (C) 2011 by Duncan Salerno <duncan.salerno@googlemail.com>
+ *
+ * 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 as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <sys/file.h>
+#include <sys/ioctl.h>
+#include <termios.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <signal.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include "gsmtap.h"
+
+#define FBUS_BAUD B115200
+
+#define FBUS2_FRAME_ID 0x1e
+#define FBUS2_DEVICE_PHONE 0x00 /* Nokia mobile phone */
+#define FBUS2_DEVICE_PC 0x0c /* Our PC */
+#define FBUS2_ACK_BYTE 0x7f /* Acknowledge of the received frame */
+#define FBUS2_MAX_TRANSMIT_LENGTH 120
+
+#define FBUS_TIMEOUT 1000000
+
+#define CHANNEL_SDCCH 128
+#define CHANNEL_SACCH 112
+#define CHANNEL_FACCH 176
+#define CHANNEL_CCCH 96
+#define CHANNEL_BCCH 80
+#define CHANNEL_SCH 64
+
+static const unsigned char msg_0xd1[] = { 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x60 };
+static const unsigned char msg_1_0x40[] = { 0x00, 0x01, 0x64, 0x01, 0x01 };
+static const unsigned char msg_2_0x40[] = { 0x00, 0x01, 0x70, 0x00, 0x00, 0x00,
+ 0xc0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
+static const unsigned char reqDisable[] = { 0x01, 0x01, 0x71, 0x01 };
+static const unsigned char reqVersion[] = { 0x00, 0x01, 0x00, 0x03, 0x00, 0x01 };
+
+static volatile int run = 1;
+static int gsmtap_fd;
+static int serial_fd;
+
+
+/*
+ * Serial comms functions
+ */
+
+static int serial_read(void *buf, size_t nbytes)
+{
+ struct timeval timeout = { 0, FBUS_TIMEOUT };
+ fd_set readfds;
+ int actual;
+
+ FD_ZERO(&readfds);
+ FD_SET(serial_fd, &readfds);
+
+ actual = select(serial_fd+1, &readfds, NULL, NULL, &timeout);
+ if( actual > 0 )
+ {
+ actual = read(serial_fd, buf, nbytes);
+ if( actual == 0 )
+ {
+ /* EOF - Terminate program */
+ run = 0;
+ return -1;
+ }
+ }
+
+ if( actual == -1 && errno != EINTR )
+ {
+ /* Serious problem, terminate program */
+ perror("serial_read");
+ run = 0;
+ }
+
+ return actual;
+}
+
+
+static int serial_init(const char *serial_port)
+{
+ struct termios t;
+ int fd;
+
+ /* O_NONBLOCK is required to avoid waiting for DCD */
+ fd = open(serial_port, O_RDWR | O_NOCTTY | O_NONBLOCK);
+ if (fd < 0)
+ {
+ perror("open");
+ return -1;
+ }
+
+#ifdef TIOCEXCL
+ /* open() calls from other applications shall fail now */
+ ioctl(fd, TIOCEXCL, (char *) 0);
+#endif
+#ifdef HAVE_I_SETSIG
+ /* Disable any signals from this file */
+ ioctl(fd, I_SETSIG, (char *) 0);
+#endif
+
+ /* Use previous settings as start */
+ if (tcgetattr(fd, &t) == -1)
+ {
+ perror("tcgetattr");
+ return -1;
+ }
+
+ /* Opening without parity */
+ t.c_iflag = IGNPAR;
+ t.c_oflag = 0;
+ /* disconnect line, 8 bits, enable receiver,
+ * ignore modem lines,lower modem line after disconnect
+ */
+ t.c_cflag = B0 | CS8 | CREAD | CLOCAL | HUPCL;
+ t.c_lflag = 0;
+ t.c_cc[VMIN] = 1;
+ t.c_cc[VTIME] = 0;
+
+#ifdef CRTSCTS
+ /* Disabling hardware flow control */
+ t.c_cflag &= ~CRTSCTS;
+#endif
+
+ unsigned int v24 = TIOCM_DTR | TIOCM_RTS;
+ if (ioctl(fd, TIOCMBIS, &v24) < 0)
+ {
+ perror("ioctl(TIOCMBIS)");
+ return -1;
+ }
+
+ cfsetispeed(&t, FBUS_BAUD);
+ cfsetospeed(&t, FBUS_BAUD);
+
+ if (tcsetattr(fd, TCSADRAIN, &t) == -1)
+ {
+ perror("tcsetattr");
+ return -1;
+ }
+
+ return fd;
+}
+
+
+/*
+ * FBUS functions
+ */
+
+
+static void fbus_init()
+{
+ unsigned int count;
+ unsigned char buff[255];
+ static const unsigned char init_char = 0x55;
+
+ /* Send init sequence */
+ for (count = 0; count < 128; count++)
+ write(serial_fd, &init_char, 1);
+
+ /* Read any possible junk on the line */
+ while (serial_read(buff, sizeof(buff)) > 0)
+ usleep(1000);
+}
+
+
+/* FBUS docs: http://www.embedtronics.com/nokia/fbus.html */
+static int fbus_write_frame(const unsigned char *msg, unsigned int msg_len, unsigned char type, unsigned char seq)
+{
+ unsigned char buffer[FBUS2_MAX_TRANSMIT_LENGTH + 10];
+ unsigned const char *buf = buffer;
+ unsigned char checksum;
+ int i, length, sent = 0;
+
+ if( msg_len > FBUS2_MAX_TRANSMIT_LENGTH )
+ return -1;
+
+ /* Add space for sequence */
+ msg_len++;
+
+ buffer[0] = FBUS2_FRAME_ID;
+ buffer[1] = FBUS2_DEVICE_PHONE; /* destination */
+ buffer[2] = FBUS2_DEVICE_PC; /* source */
+ buffer[3] = type;
+ buffer[4] = msg_len / 256;
+ buffer[5] = msg_len % 256;
+ memcpy(buffer + 6, msg, msg_len-1); /* Excluding Sequence */
+ buffer[6+msg_len-1] = seq;
+ length = msg_len + 6;
+
+ /* Odd messages require additional 0x00 byte */
+ if (msg_len % 2)
+ buffer[length++] = 0x00;
+
+ checksum = 0;
+ for (i = 0; i < length; i+=2)
+ checksum ^= buffer[i];
+
+ buffer[length++] = checksum;
+
+ checksum = 0;
+ for (i = 1; i < length; i+=2)
+ checksum ^= buffer[i];
+
+ buffer[length++] = checksum;
+
+ /* Sending to phone */
+#ifdef DEBUG
+ printf("TX frame: ");
+ for( i = 0; i < length; i++ )
+ printf("%02x ", buf[i]);
+ printf("\n");
+#endif
+ do
+ {
+ int ret = write(serial_fd, buf, length - sent);
+ if (ret < 0)
+ {
+ if (errno == EAGAIN)
+ {
+ usleep(1000);
+ continue;
+ }
+ break;
+ }
+ sent += ret;
+ buf += ret;
+ } while (sent < length);
+
+ if (sent != length)
+ return -1;
+
+ return 0;
+}
+
+
+static int fbus_write_message(const unsigned char *buf, int length, unsigned char type)
+{
+ static unsigned char seq = 0x4f; /* So first is 0x40 */
+ seq = 0x40 | ((seq+1) & 0xf);
+ return fbus_write_frame(buf, length, type, seq);
+}
+
+
+static int fbus_send_ack(unsigned char type, unsigned char seq)
+{
+ unsigned char buffer[1]={type};
+ return fbus_write_frame(buffer, sizeof(buffer), FBUS2_ACK_BYTE, seq);
+}
+
+
+static int fbus_read_message(unsigned char* out_type, unsigned char** out_message, size_t *out_len)
+{
+ static unsigned char frame_buf[65535 + 11]; /* Theoretical max len, plus header, cksum etc */
+ static size_t ptr = 0, remaining = 0;
+
+ do
+ {
+ /* Resync if necessary */
+ while( remaining && frame_buf[ptr] != FBUS2_FRAME_ID )
+ {
+ remaining--;
+ ptr++;
+#ifdef DEBUG
+ fprintf(stderr, "Resyncing\n");
+#endif
+ }
+
+ /* Do we have a frame? */
+ if( remaining >= 6 )
+ {
+ size_t payload_len = frame_buf[ptr+4]*256 + frame_buf[ptr+5];
+ size_t frame_len = payload_len + 8;
+
+ if (payload_len % 2) /* Any padding? */
+ frame_len++;
+ if( frame_len <= remaining )
+ {
+ unsigned char *frame = frame_buf + ptr;
+ remaining -= frame_len;
+ ptr += frame_len;
+
+ /* Calculate checksum */
+ int i;
+ unsigned char checksum1 = 0, checksum2 = 0;;
+ for (i = 0; i < frame_len-2; i+=2)
+ checksum1 ^= frame[i];
+
+ for (i = 1; i < frame_len-1; i+=2)
+ checksum2 ^= frame[i];
+
+ /* Validate checksum */
+ if( frame[frame_len-2] == checksum1 && frame[frame_len-1] == checksum2 )
+ {
+#ifdef DEBUG
+ printf("RX frame: ");
+ for( i = 0; i < frame_len; i++ )
+ printf("%02x ", ((unsigned char*)frame)[i]);
+ printf("\n");
+#endif
+ /* Check message is for us, and its not an ack */
+ if( frame[1] == FBUS2_DEVICE_PC && frame[2] == FBUS2_DEVICE_PHONE && frame[3] != FBUS2_ACK_BYTE)
+ {
+ /* Send ack for non-debug frames */
+ if( frame[3] != 0 )
+ fbus_send_ack(frame[3], frame[6+payload_len-1] & 0x0f);
+ *out_type = frame[3];
+ *out_message = frame + 6;
+ *out_len = payload_len - 1; /* Don't send sequence */
+ return 0;
+ }
+ }
+#ifdef DEBUG
+ else
+ fprintf(stderr, "Dropped frame with bad checksum\n");
+#endif
+ }
+
+ /* Drop frame and continue */
+ }
+
+ /* Shift any remaining buffer contents to beginning */
+ if( remaining )
+ memmove(frame_buf, frame_buf+ptr, remaining);
+ ptr = 0;
+
+ /* Need to read more data */
+ int res = serial_read(frame_buf+remaining, 65535-remaining);
+ if( res > 0 )
+ remaining += res;
+ else if( res == 0 )
+ return -1; /* Timeout */
+ else
+ return -2; /* Error or EINTR */
+ } while(1);
+}
+
+
+static int fbus_receive_message(unsigned char expected_type, unsigned char** out_message, size_t *out_len)
+{
+ /* Keep receiving messages until correct type received, or timeout */
+ while( run )
+ {
+ unsigned char out_type;
+ int ret = fbus_read_message(&out_type, out_message, out_len);
+ if( ret == 0 )
+ {
+ if( out_type == expected_type )
+ return 0;
+#ifdef DEBUG
+ else
+ printf("Wanted msg 0x%x, got 0x%x\n", type, out_type);
+#endif
+ }
+ else
+ break;
+ }
+
+ return -1;
+}
+
+
+static int fbus_send_message(const unsigned char *buf, int length, unsigned char type, unsigned char** out_message, size_t *out_len)
+{
+ /* Write message to phone */
+ if( fbus_write_message(buf, length, type) == -1 )
+ return -1;
+
+ /* Wait for response to message */
+ return fbus_receive_message(type, out_message, out_len);
+}
+
+
+/*
+ * Message processing functions
+ */
+
+
+static void proc_sim(const unsigned char *apdu, unsigned char len)
+{
+ const size_t buf_len = sizeof(struct gsmtap_hdr) + len;
+ unsigned char buf[buf_len];
+ struct gsmtap_hdr *gh = (struct gsmtap_hdr *) buf;
+
+ printf("SIM: ");
+ int i;
+ for( i = 0; i < len; i++ )
+ printf("0x%02X ", apdu[i]);
+ printf("\n");
+ memset(gh, 0, sizeof(*gh));
+ gh->version = GSMTAP_VERSION;
+ gh->hdr_len = sizeof(*gh)/4;
+ gh->type = GSMTAP_TYPE_SIM;
+
+ memcpy(buf + sizeof(*gh), apdu, len);
+
+ if (write(gsmtap_fd, buf, buf_len) < 0)
+ perror("write gsmtap");
+}
+
+
+/* MDISND debug messages: http://nokix.pasjagsm.pl/help/blacksphere/sub_100hardware/sub_dsp/sub_mdi/sub_typestxd.htm */
+static void proc_mdisnd(unsigned char subtype, unsigned const char *mdi, unsigned char mdi_len)
+{
+ struct gsmtap_hdr gh = { .version = GSMTAP_VERSION,
+ .hdr_len = sizeof(struct gsmtap_hdr)/4,
+ .type = GSMTAP_TYPE_UM,
+ .sub_type = GSMTAP_CHANNEL_UNKNOWN,
+ .arfcn = htons(GSMTAP_ARFCN_F_UPLINK) };
+
+ /* 2 byte MDI header, followed by subtype specific payload */
+ if( mdi_len < 2 || mdi[1] != subtype )
+ return;
+
+ /* Subtype 0x0c - RACH */
+ if( subtype == 0x0C && mdi_len > 4 && mdi[3] == 0x0)
+ {
+ printf("MDISND: RACH %02X\n", mdi[4]);
+ gh.sub_type = GSMTAP_CHANNEL_RACH;
+ mdi_len = 5; /* Truncate trailing stuff after CHANNEL REQUEST */
+ }
+
+ /* Subtype 0x1b - GSM L2 */
+ if(subtype == 0x1B && mdi_len > 4) {
+ printf("MDISND %02X: ch=%u (0x%02X) len=%u\n", mdi[2], mdi[3], mdi[3], mdi_len-4);
+ switch(mdi[3])
+ {
+ case CHANNEL_SDCCH:
+ gh.sub_type = GSMTAP_CHANNEL_SDCCH;
+ break;
+ case CHANNEL_FACCH:
+ gh.sub_type = GSMTAP_CHANNEL_TCH_F; /* Assume full rate */
+ break;
+ case CHANNEL_SACCH:
+ gh.sub_type = GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH;
+ break;
+ }
+ }
+
+ if( gh.sub_type != GSMTAP_CHANNEL_UNKNOWN )
+ {
+ const size_t buf_len = sizeof(struct gsmtap_hdr) + mdi_len-4;
+ unsigned char buf[buf_len];
+
+ memcpy(buf, &gh, sizeof(struct gsmtap_hdr));
+ memcpy(buf+sizeof(struct gsmtap_hdr), &mdi[4], mdi_len-4);
+
+ if (write(gsmtap_fd, buf, buf_len) < 0)
+ perror("write gsmtap");
+ }
+}
+
+
+/* MDIRCV debug messages: http://nokix.pasjagsm.pl/help/blacksphere/sub_100hardware/sub_dsp/sub_mdi/sub_typesrxd.htm */
+static void proc_mdircv(unsigned char subtype, unsigned const char *mdi, unsigned char mdi_len)
+{
+ struct gsmtap_hdr gh = { .version = GSMTAP_VERSION,
+ .hdr_len = sizeof(struct gsmtap_hdr)/4,
+ .type = GSMTAP_TYPE_UM,
+ .sub_type = GSMTAP_CHANNEL_UNKNOWN };
+
+ /* 2 byte MDI header, followed by subtype specific payload */
+ if( mdi_len < 2 || mdi[1] != subtype )
+ return;
+
+ /* Subtype 0x80 - GSM L2 */
+ if( subtype == 0x80 && mdi_len > 11 )
+ {
+ gh.frame_number = htonl((mdi[5]<<16) | (mdi[6]<<8) | mdi[7]);
+ gh.arfcn = htons((mdi[8]<<8) | mdi[9]);
+ printf("MDIRCV %02X: ch=%u (0x%02X) bsic=%u err=%u fn=%u arfcn=%u shift=%u len=%u\n", subtype, mdi[2], mdi[2], mdi[3], mdi[4], (mdi[5]<<16)|(mdi[6]<<8)|(mdi[7]), (mdi[8]<<8)|mdi[9], (mdi[10]<<8)|mdi[11], mdi_len-2);
+ switch( mdi[2] )
+ {
+ case CHANNEL_SACCH:
+ gh.sub_type = GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH;
+ break;
+ case CHANNEL_SDCCH:
+ gh.sub_type = GSMTAP_CHANNEL_SDCCH;
+ break;
+ case CHANNEL_FACCH:
+ gh.sub_type = GSMTAP_CHANNEL_TCH_F; /* Assume full rate */
+ break;
+ case CHANNEL_BCCH:
+ if( (mdi[12] & 3) == 1 )
+ gh.sub_type = GSMTAP_CHANNEL_BCCH;
+ break;
+ case CHANNEL_CCCH:
+ if( (mdi[12] & 3) == 1 )
+ gh.sub_type = GSMTAP_CHANNEL_CCCH;
+ break;
+ }
+ }
+
+ if( gh.sub_type != GSMTAP_CHANNEL_UNKNOWN )
+ {
+ const size_t buf_len = sizeof(struct gsmtap_hdr) + mdi_len-12;
+ unsigned char buf[buf_len];
+
+ memcpy(buf, &gh, sizeof(struct gsmtap_hdr));
+ memcpy(buf+sizeof(struct gsmtap_hdr), &mdi[12], mdi_len-12);
+
+ if (write(gsmtap_fd, buf, buf_len) < 0)
+ perror("write gsmtap");
+ }
+}
+
+
+static void gsmtap_open(const char *gsmtap_host)
+{
+ struct sockaddr_in sin;
+
+ sin.sin_family= AF_INET;
+ sin.sin_port = htons(GSMTAP_UDP_PORT);
+
+ if (inet_aton(gsmtap_host, &sin.sin_addr) < 0)
+ {
+ perror("parsing GSMTAP destination address");
+ exit(2);
+ }
+ gsmtap_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ if (gsmtap_fd < 0)
+ {
+ perror("GSMTAP socket initialization");
+ exit(2);
+ }
+ if (connect(gsmtap_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
+ {
+ perror("connecting GSMTAP socket");
+ exit(2);
+ }
+}
+
+
+static void interrupt(int sign)
+{
+ signal(sign, SIG_IGN);
+ run = 0;
+}
+
+
+int main(int argc, char **argv)
+{
+ if( argc != 3 )
+ {
+ fprintf(stderr, "Usage: %s device destination\n\n\teg. %s /dev/ttyUSB0 224.0.0.1\n\n", argv[0], argv[0]);
+ return 1;
+ }
+
+ gsmtap_open(argv[2]);
+
+ serial_fd = serial_init(argv[1]);
+ if( serial_fd < 0 )
+ return 1;
+
+ fbus_init();
+
+ unsigned char *msg;
+ unsigned char type;
+ size_t len;
+
+ /* Try disabling debug, just in case it's already on to make things more sane */
+ fbus_write_message(reqDisable, sizeof(reqDisable), 0x40);
+ fbus_read_message(&type, &msg, &len);
+
+ /* Get info about connected phone */
+ if( fbus_write_message(reqVersion, sizeof(reqVersion), 0xd1) != 0)
+ {
+ fprintf(stderr, "Failed to request version\n");
+ return 1;
+ }
+ if( fbus_receive_message(0xd2, &msg, &len) != 0 )
+ {
+ fprintf(stderr, "Failed get version response\n");
+ return 1;
+ }
+ printf("Phone on %s:\n%s\n", argv[1], msg+4);
+
+ /* Enable security */
+ if( fbus_send_message(msg_1_0x40, sizeof(msg_1_0x40), 0x40, &msg, &len) != 0 )
+ {
+ fprintf(stderr, "Failed to enable security\n");
+ return 1;
+ }
+
+ /* Enable debug */
+ if( fbus_send_message(msg_2_0x40, sizeof(msg_2_0x40), 0x40, &msg, &len) != 0 )
+ {
+ fprintf(stderr, "Failed to enable debug mode\n");
+ return 1;
+ }
+
+ signal(SIGINT, interrupt);
+ printf("Press Ctrl+C to interrupt...\n");
+
+ unsigned char seq = 0;
+ while( run )
+ {
+ if( fbus_read_message(&type, &msg, &len) == 0 && type == 0x0 )
+ {
+ /* Anatomy of debug messages: http://nokix.pasjagsm.pl/help/blacksphere/sub_200nokiaos/sub_fbus/sub_zdebug.htm */
+ /* Process 8 byte header, followed by payload */
+ unsigned char type = msg[2];
+ unsigned char subtype = msg[3];
+ unsigned char seq_nr = msg[6];
+ unsigned char payload_len = msg[7];
+ unsigned const char *payload = msg+8;
+
+ if( payload_len + 8 > len )
+ {
+ fprintf(stderr, "Truncated payload\n");
+ continue;
+ }
+#ifdef DEBUG
+ unsigned short timestamp = (msg[4]<<8) | msg[5];
+ printf("<%02X%02X> t=%04x seq_nr=%02x payload_len=%u payload=", type, subtype, timestamp, seq_nr, payload_len);
+ int ii;
+ for( ii = 0; ii < payload_len; ii++ )
+ {
+ printf("%02x ", payload[ii]);
+ }
+ printf("\n");
+#endif
+ static int first = 1;
+ if( seq != seq_nr )
+ {
+ if( first == 0 )
+ fprintf(stderr, "Missed some packets expected 0x%x, got 0x%x\n", seq, seq_nr);
+ seq = seq_nr;
+ }
+ first = 0;
+ seq++;
+
+ /* Debug types: http://nokix.pasjagsm.pl/help/blacksphere/sub_200nokiaos/sub_fbus/sub_zdebug/sub_types.htm */
+ switch(type) {
+ case 0x18:
+ proc_mdisnd(subtype, payload, payload_len);
+ break;
+ case 0x19:
+ proc_mdircv(subtype, payload, payload_len);
+ break;
+ case 0x20:
+ case 0x23:
+ proc_sim(payload, payload_len);
+ break;
+ }
+ }
+ }
+
+ /* Disable debug before return */
+ printf("Disabling debug\n");
+ fbus_write_message(reqDisable, sizeof(reqDisable), 0x40);
+ fbus_read_message(&type, &msg, &len);
+
+ return 0;
+}
+