From d4e118a7a059a9d675d0d8a8ea75fdc88f43afb7 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Wed, 3 Mar 2010 14:24:05 +0100 Subject: Rename 'layer2' program to 'layer23' program --- src/host/layer2/.gitignore | 14 - src/host/layer2/COPYING | 339 ---------- src/host/layer2/Makefile.am | 3 - src/host/layer2/configure.ac | 27 - src/host/layer2/include/Makefile.am | 2 - src/host/layer2/include/l1a_l23_interface.h | 1 - src/host/layer2/include/osmocom/Makefile.am | 3 - src/host/layer2/include/osmocom/debug.h | 1 - src/host/layer2/include/osmocom/l1ctl.h | 25 - src/host/layer2/include/osmocom/lapdm.h | 60 -- src/host/layer2/include/osmocom/layer3.h | 10 - src/host/layer2/include/osmocom/osmocom_data.h | 19 - src/host/layer2/include/osmocom/rslms.h | 23 - src/host/layer2/src/Makefile.am | 9 - src/host/layer2/src/gsmtap_util.c | 136 ---- src/host/layer2/src/gsmtap_util.h | 13 - src/host/layer2/src/l1ctl.c | 273 -------- src/host/layer2/src/lapdm.c | 836 ------------------------ src/host/layer2/src/layer2_main.c | 225 ------- src/host/layer2/src/layer3.c | 216 ------ src/host/layer2/src/libosmocom/debug.c | 1 - src/host/layer2/src/rslms.c | 162 ----- src/host/layer23/.gitignore | 14 + src/host/layer23/COPYING | 339 ++++++++++ src/host/layer23/Makefile.am | 3 + src/host/layer23/configure.ac | 27 + src/host/layer23/include/Makefile.am | 2 + src/host/layer23/include/l1a_l23_interface.h | 1 + src/host/layer23/include/osmocom/Makefile.am | 3 + src/host/layer23/include/osmocom/debug.h | 1 + src/host/layer23/include/osmocom/l1ctl.h | 25 + src/host/layer23/include/osmocom/lapdm.h | 60 ++ src/host/layer23/include/osmocom/layer3.h | 10 + src/host/layer23/include/osmocom/osmocom_data.h | 19 + src/host/layer23/include/osmocom/rslms.h | 23 + src/host/layer23/src/Makefile.am | 9 + src/host/layer23/src/gsmtap_util.c | 136 ++++ src/host/layer23/src/gsmtap_util.h | 13 + src/host/layer23/src/l1ctl.c | 273 ++++++++ src/host/layer23/src/lapdm.c | 836 ++++++++++++++++++++++++ src/host/layer23/src/layer3.c | 216 ++++++ src/host/layer23/src/libosmocom/debug.c | 1 + src/host/layer23/src/main.c | 225 +++++++ src/host/layer23/src/rslms.c | 162 +++++ 44 files changed, 2398 insertions(+), 2398 deletions(-) delete mode 100644 src/host/layer2/.gitignore delete mode 100644 src/host/layer2/COPYING delete mode 100644 src/host/layer2/Makefile.am delete mode 100644 src/host/layer2/configure.ac delete mode 100644 src/host/layer2/include/Makefile.am delete mode 120000 src/host/layer2/include/l1a_l23_interface.h delete mode 100644 src/host/layer2/include/osmocom/Makefile.am delete mode 120000 src/host/layer2/include/osmocom/debug.h delete mode 100644 src/host/layer2/include/osmocom/l1ctl.h delete mode 100644 src/host/layer2/include/osmocom/lapdm.h delete mode 100644 src/host/layer2/include/osmocom/layer3.h delete mode 100644 src/host/layer2/include/osmocom/osmocom_data.h delete mode 100644 src/host/layer2/include/osmocom/rslms.h delete mode 100644 src/host/layer2/src/Makefile.am delete mode 100644 src/host/layer2/src/gsmtap_util.c delete mode 100644 src/host/layer2/src/gsmtap_util.h delete mode 100644 src/host/layer2/src/l1ctl.c delete mode 100644 src/host/layer2/src/lapdm.c delete mode 100644 src/host/layer2/src/layer2_main.c delete mode 100644 src/host/layer2/src/layer3.c delete mode 120000 src/host/layer2/src/libosmocom/debug.c delete mode 100644 src/host/layer2/src/rslms.c create mode 100644 src/host/layer23/.gitignore create mode 100644 src/host/layer23/COPYING create mode 100644 src/host/layer23/Makefile.am create mode 100644 src/host/layer23/configure.ac create mode 100644 src/host/layer23/include/Makefile.am create mode 120000 src/host/layer23/include/l1a_l23_interface.h create mode 100644 src/host/layer23/include/osmocom/Makefile.am create mode 120000 src/host/layer23/include/osmocom/debug.h create mode 100644 src/host/layer23/include/osmocom/l1ctl.h create mode 100644 src/host/layer23/include/osmocom/lapdm.h create mode 100644 src/host/layer23/include/osmocom/layer3.h create mode 100644 src/host/layer23/include/osmocom/osmocom_data.h create mode 100644 src/host/layer23/include/osmocom/rslms.h create mode 100644 src/host/layer23/src/Makefile.am create mode 100644 src/host/layer23/src/gsmtap_util.c create mode 100644 src/host/layer23/src/gsmtap_util.h create mode 100644 src/host/layer23/src/l1ctl.c create mode 100644 src/host/layer23/src/lapdm.c create mode 100644 src/host/layer23/src/layer3.c create mode 120000 src/host/layer23/src/libosmocom/debug.c create mode 100644 src/host/layer23/src/main.c create mode 100644 src/host/layer23/src/rslms.c diff --git a/src/host/layer2/.gitignore b/src/host/layer2/.gitignore deleted file mode 100644 index d359466..0000000 --- a/src/host/layer2/.gitignore +++ /dev/null @@ -1,14 +0,0 @@ -Makefile -Makefile.in -aclocal.m4 -configure -missing -*.o -depcomp -config.* -*.sw? -*.deps -layer2 -install-sh -autom4te.cache -*.a diff --git a/src/host/layer2/COPYING b/src/host/layer2/COPYING deleted file mode 100644 index d511905..0000000 --- a/src/host/layer2/COPYING +++ /dev/null @@ -1,339 +0,0 @@ - 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 Lesser 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. - - - Copyright (C) - - 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. - - , 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/src/host/layer2/Makefile.am b/src/host/layer2/Makefile.am deleted file mode 100644 index bc3910f..0000000 --- a/src/host/layer2/Makefile.am +++ /dev/null @@ -1,3 +0,0 @@ -AUTOMAKE_OPTIONS = foreign dist-bzip2 1.6 - -SUBDIRS = include src diff --git a/src/host/layer2/configure.ac b/src/host/layer2/configure.ac deleted file mode 100644 index cbed1c0..0000000 --- a/src/host/layer2/configure.ac +++ /dev/null @@ -1,27 +0,0 @@ -dnl Process this file with autoconf to produce a configure script -AC_INIT - -AM_INIT_AUTOMAKE(layer2, 0.0.0) - -dnl kernel style compile messages -m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) - -dnl checks for programs -AC_PROG_MAKE_SET -AC_PROG_CC -AC_PROG_INSTALL -AC_PROG_RANLIB - -dnl checks for libraries -PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore) - -dnl checks for header files -AC_HEADER_STDC - -dnl Checks for typedefs, structures and compiler characteristics - -AC_OUTPUT( - src/Makefile - include/Makefile - include/osmocom/Makefile - Makefile) diff --git a/src/host/layer2/include/Makefile.am b/src/host/layer2/include/Makefile.am deleted file mode 100644 index ca774b6..0000000 --- a/src/host/layer2/include/Makefile.am +++ /dev/null @@ -1,2 +0,0 @@ -noinst_HEADERS = l1a_l23_interface.h -SUBDIRS = osmocom diff --git a/src/host/layer2/include/l1a_l23_interface.h b/src/host/layer2/include/l1a_l23_interface.h deleted file mode 120000 index 2bbc967..0000000 --- a/src/host/layer2/include/l1a_l23_interface.h +++ /dev/null @@ -1 +0,0 @@ -../../../../include/l1a_l23_interface.h \ No newline at end of file diff --git a/src/host/layer2/include/osmocom/Makefile.am b/src/host/layer2/include/osmocom/Makefile.am deleted file mode 100644 index 6014195..0000000 --- a/src/host/layer2/include/osmocom/Makefile.am +++ /dev/null @@ -1,3 +0,0 @@ -# headers from OpenBSC -noinst_HEADERS = debug.h -noinst_HEADERS += l1ctl.h osmocom_data.h lapdm.h rslms.h layer3.h diff --git a/src/host/layer2/include/osmocom/debug.h b/src/host/layer2/include/osmocom/debug.h deleted file mode 120000 index ae7c52c..0000000 --- a/src/host/layer2/include/osmocom/debug.h +++ /dev/null @@ -1 +0,0 @@ -../../../libosmocom/include/osmocom/debug.h \ No newline at end of file diff --git a/src/host/layer2/include/osmocom/l1ctl.h b/src/host/layer2/include/osmocom/l1ctl.h deleted file mode 100644 index 0d5cba6..0000000 --- a/src/host/layer2/include/osmocom/l1ctl.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef osmocom_l1ctl_h -#define osmocom_l1ctl_h - -#include -#include - -struct osmocom_ms; - -/* Receive incoming data from L1 using L1CTL format */ -int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg); - -/* Transmit L1CTL_DATA_REQ */ -int tx_ph_data_req(struct osmocom_ms *ms, struct msgb *msg, - uint8_t chan_nr, uint8_t link_id); - -/* Transmit L1CTL_RACH_REQ */ -int tx_ph_rach_req(struct osmocom_ms *ms); - -/* Transmit L1CTL_DM_EST_REQ */ -int tx_ph_dm_est_req(struct osmocom_ms *ms, uint16_t band_arfcn, uint8_t chan_nr); - -extern int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg); - - -#endif diff --git a/src/host/layer2/include/osmocom/lapdm.h b/src/host/layer2/include/osmocom/lapdm.h deleted file mode 100644 index f3967a9..0000000 --- a/src/host/layer2/include/osmocom/lapdm.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef _OSMOCOM_LAPDM_H -#define _OSMOCOM_LAPDM_H - -#include - -#include -#include - -#include - -enum lapdm_state { - LAPDm_STATE_IDLE, - LAPDm_STATE_SABM_SENT, - LAPDm_STATE_MF_EST, - LAPDm_STATE_TIMER_RECOV, - LAPDm_STATE_OWN_RCVR_BUSY, - LAPDm_STATE_NULL, -}; - -struct lapdm_entity; -struct osmocom_ms; - -/* TS 04.06 / Section 3.5.2 */ -struct lapdm_datalink { - uint8_t V_send; /* seq nr of next I frame to be transmitted */ - uint8_t V_ack; /* last frame ACKed by peer */ - uint8_t N_send; /* ? set to V_send at Tx time*/ - uint8_t V_recv; /* seq nr of next I frame expected to be received */ - uint8_t N_recv; /* expected send seq nr of the next received I frame */ - enum lapdm_state state; - struct timer_list t200; - uint8_t retrans_ctr; - - struct lapdm_entity *entity; -}; - -enum lapdm_dl_sapi { - DL_SAPI0 = 0, - DL_SAPI3 = 1, - _NR_DL_SAPI -}; - -struct lapdm_entity { - struct lapdm_datalink datalink[_NR_DL_SAPI]; - struct osmocom_ms *ms; -}; - -/* initialize a LAPDm entity */ -void lapdm_init(struct lapdm_entity *le, struct osmocom_ms *ms); - -/* input into layer2 (from layer 1) */ -int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1ctl_info_dl *l1i); - -/* input into layer2 (from layer 3) */ -int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms); - -/* sending messages up from L2 to L3 */ -int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms); - -#endif /* _OSMOCOM_LAPDM_H */ diff --git a/src/host/layer2/include/osmocom/layer3.h b/src/host/layer2/include/osmocom/layer3.h deleted file mode 100644 index 1b4af07..0000000 --- a/src/host/layer2/include/osmocom/layer3.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef _OSMOCOM_L3_H -#define _OSMOCOM_L3_H - -#include -#include - -int gsm48_rx_ccch(struct msgb *msg, struct osmocom_ms *ms); -int gsm48_rx_dcch(struct msgb *msg, struct osmocom_ms *ms); - -#endif diff --git a/src/host/layer2/include/osmocom/osmocom_data.h b/src/host/layer2/include/osmocom/osmocom_data.h deleted file mode 100644 index 48c5579..0000000 --- a/src/host/layer2/include/osmocom/osmocom_data.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef osmocom_data_h -#define osmocom_data_h - -#include -#include - -#include - -/* One Mobilestation for osmocom */ -struct osmocom_ms { - struct bsc_fd bfd; - enum gsm_band band; - int arfcn; - - struct lapdm_entity lapdm_dcch; - struct lapdm_entity lapdm_acch; -}; - -#endif diff --git a/src/host/layer2/include/osmocom/rslms.h b/src/host/layer2/include/osmocom/rslms.h deleted file mode 100644 index 7327c11..0000000 --- a/src/host/layer2/include/osmocom/rslms.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef _OSMOCOM_RSLMS_H -#define _OSMOCOM_RSLMS_H - -#include -#include - -/* From L3 into RSLMS (direction -> L2) */ - -/* Send a 'simple' RLL request to L2 */ -int rslms_tx_rll_req(struct osmocom_ms *ms, uint8_t msg_type, - uint8_t chan_nr, uint8_t link_id); - -/* Send a RLL request (including L3 info) to L2 */ -int rslms_tx_rll_req_l3(struct osmocom_ms *ms, uint8_t msg_type, - uint8_t chan_nr, uint8_t link_id, struct msgb *msg); - - -/* From L2 into RSLMS (direction -> L3) */ - -/* input function that L2 calls when sending messages up to L3 */ -int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms); - -#endif /* _OSMOCOM_RSLMS_H */ diff --git a/src/host/layer2/src/Makefile.am b/src/host/layer2/src/Makefile.am deleted file mode 100644 index d1f7f12..0000000 --- a/src/host/layer2/src/Makefile.am +++ /dev/null @@ -1,9 +0,0 @@ -INCLUDES = $(all_includes) -I$(top_srcdir)/include -AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS) - -noinst_LIBRARIES = libosmocom.a -libosmocom_a_SOURCES = libosmocom/debug.c - -sbin_PROGRAMS = layer2 -layer2_SOURCES = layer2_main.c l1ctl.c gsmtap_util.c lapdm.c rslms.c layer3.c -layer2_LDADD = libosmocom.a $(LIBOSMOCORE_LIBS) diff --git a/src/host/layer2/src/gsmtap_util.c b/src/host/layer2/src/gsmtap_util.c deleted file mode 100644 index 0254c18..0000000 --- a/src/host/layer2/src/gsmtap_util.c +++ /dev/null @@ -1,136 +0,0 @@ -/* GSMTAP output for Osmocom Layer2 (will only work on the host PC) */ -/* - * (C) 2010 by Harald Welte - * - * 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 -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include - -static struct bsc_fd gsmtap_bfd; -static LLIST_HEAD(gsmtap_txqueue); - -/* receive a message from L1/L2 and put it in GSMTAP */ -int gsmtap_sendmsg(uint8_t ts, uint16_t arfcn, uint32_t fn, - const uint8_t *data, unsigned int len) -{ - struct msgb *msg; - struct gsmtap_hdr *gh; - uint8_t *dst; - - msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx"); - if (!msg) - return -ENOMEM; - - gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh)); - - gh->version = GSMTAP_VERSION; - gh->hdr_len = sizeof(*gh)/4; - gh->type = GSMTAP_TYPE_UM; - gh->timeslot = ts; - gh->arfcn = htons(arfcn); - gh->noise_db = 0; - gh->signal_db = 0; - gh->frame_number = htonl(fn); - gh->burst_type = GSMTAP_BURST_NORMAL; - gh->antenna_nr = 0; - - dst = msgb_put(msg, len); - memcpy(dst, data, len); - - msgb_enqueue(&gsmtap_txqueue, msg); - gsmtap_bfd.when |= BSC_FD_WRITE; - - return 0; -} - -/* Callback from select layer if we can write to the socket */ -static int gsmtap_fd_cb(struct bsc_fd *fd, unsigned int flags) -{ - struct msgb *msg; - int rc; - - if (!(flags & BSC_FD_WRITE)) - return 0; - - msg = msgb_dequeue(&gsmtap_txqueue); - if (!msg) { - /* no more messages in the queue, disable READ cb */ - gsmtap_bfd.when = 0; - return 0; - } - rc = write(gsmtap_bfd.fd, msg->data, msg->len); - if (rc < 0) { - perror("writing msgb to gsmtap fd"); - msgb_free(msg); - return rc; - } - if (rc != msg->len) { - perror("short write to gsmtap fd"); - msgb_free(msg); - return -EIO; - } - - msgb_free(msg); - return 0; -} - -int gsmtap_init(void) -{ - int rc; - struct sockaddr_in sin; - - sin.sin_family = AF_INET; - sin.sin_port = htons(GSMTAP_UDP_PORT); - inet_aton("127.0.0.1", &sin.sin_addr); - - /* FIXME: create socket */ - rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (rc < 0) { - perror("creating UDP socket"); - return rc; - } - gsmtap_bfd.fd = rc; - rc = connect(rc, (struct sockaddr *)&sin, sizeof(sin)); - if (rc < 0) { - perror("connecting UDP socket"); - close(gsmtap_bfd.fd); - gsmtap_bfd.fd = 0; - return rc; - } - - gsmtap_bfd.when = BSC_FD_WRITE; - gsmtap_bfd.cb = gsmtap_fd_cb; - gsmtap_bfd.data = NULL; - - return bsc_register_fd(&gsmtap_bfd); -} diff --git a/src/host/layer2/src/gsmtap_util.h b/src/host/layer2/src/gsmtap_util.h deleted file mode 100644 index 3599c33..0000000 --- a/src/host/layer2/src/gsmtap_util.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef GSMTAP_ROUTINES_H -#define GSMTAP_ROUTINES_H - -#include -#include - -int gsmtap_init(void); - -/* receive a message from L1/L2 and put it in GSMTAP */ -int gsmtap_sendmsg(uint8_t ts, uint16_t arfcn, uint32_t fn, - const uint8_t *data, unsigned int len); - -#endif diff --git a/src/host/layer2/src/l1ctl.c b/src/host/layer2/src/l1ctl.c deleted file mode 100644 index b8c636a..0000000 --- a/src/host/layer2/src/l1ctl.c +++ /dev/null @@ -1,273 +0,0 @@ -/* Layer1 control code, talking L1CTL protocol with L1 on the phone */ - -/* (C) 2010 by Holger Hans Peter Freyther - * (C) 2010 by Harald Welte - * - * 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 -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "gsmtap_util.h" - -static struct msgb *osmo_l1_alloc(uint8_t msg_type) -{ - struct l1ctl_info_ul *ul; - struct msgb *msg = msgb_alloc_headroom(256, 4, "osmo_l1"); - - if (!msg) { - fprintf(stderr, "Failed to allocate memory.\n"); - return NULL; - } - - msg->l1h = msgb_put(msg, sizeof(*ul)); - ul = (struct l1ctl_info_ul *) msg->l1h; - ul->msg_type = msg_type; - - return msg; -} - - -static int osmo_make_band_arfcn(struct osmocom_ms *ms) -{ - /* TODO: Include the band */ - return ms->arfcn; -} - -static int rx_l1_ccch_resp(struct osmocom_ms *ms, struct msgb *msg) -{ - struct l1ctl_info_dl *dl; - struct l1ctl_sync_new_ccch_resp *sb; - - if (msgb_l3len(msg) < sizeof(*sb)) { - fprintf(stderr, "MSG too short for CCCH RESP: %u\n", msgb_l3len(msg)); - return -1; - } - - dl = (struct l1ctl_info_dl *) msg->l1h; - sb = (struct l1ctl_sync_new_ccch_resp *) msg->l2h; - - printf("SCH: SNR: %u TDMA: (%.4u/%.2u/%.2u) bsic: %d\n", - dl->snr[0], dl->time.t1, dl->time.t2, dl->time.t3, sb->bsic); - - return 0; -} - -char *chan_nr2string(uint8_t chan_nr) -{ - static char str[20]; - uint8_t cbits = chan_nr >> 3; - - str[0] = '\0'; - - if (cbits == 0x01) - sprintf(str, "TCH/F"); - else if ((cbits & 0x1e) == 0x02) - sprintf(str, "TCH/H(%u)", cbits & 0x01); - else if ((cbits & 0x1c) == 0x04) - sprintf(str, "SDCCH/4(%u)", cbits & 0x03); - else if ((cbits & 0x18) == 0x08) - sprintf(str, "SDCCH/8(%u)", cbits & 0x07); - else if (cbits == 0x10) - sprintf(str, "BCCH"); - else if (cbits == 0x11) - sprintf(str, "RACH"); - else if (cbits == 0x12) - sprintf(str, "PCH/AGCH"); - else - sprintf(str, "UNKNOWN"); - - return str; -} - -/* Receive L1CTL_DATA_IND (Data Indication from L1) */ -static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg) -{ - struct l1ctl_info_dl *dl, dl_cpy; - struct l1ctl_data_ind *ccch; - struct lapdm_entity *le; - - if (msgb_l3len(msg) < sizeof(*ccch)) { - fprintf(stderr, "MSG too short Data Ind: %u\n", msgb_l3len(msg)); - return -1; - } - - dl = (struct l1ctl_info_dl *) msg->l1h; - ccch = (struct l1ctl_data_ind *) msg->l2h; - printf("%s (%.4u/%.2u/%.2u) %s\n", - chan_nr2string(dl->chan_nr), dl->time.t1, dl->time.t2, - dl->time.t3, hexdump(ccch->data, sizeof(ccch->data))); - - /* send CCCH data via GSMTAP */ - gsmtap_sendmsg(dl->chan_nr & 0x07, dl->band_arfcn, dl->time.fn, ccch->data, - sizeof(ccch->data)); - - /* determine LAPDm entity based on SACCH or not */ - if (dl->link_id & 0x40) - le = &ms->lapdm_acch; - else - le = &ms->lapdm_dcch; - /* make local stack copy of l1ctl_info_dl, as LAPDm will overwrite skb hdr */ - memcpy(&dl_cpy, dl, sizeof(dl_cpy)); - - /* pull the L1 header from the msgb */ - msgb_pull(msg, msg->l2h - msg->l1h); - msg->l1h = NULL; - - /* send it up into LAPDm */ - l2_ph_data_ind(msg, le, &dl_cpy); - - return 0; -} - -/* Transmit L1CTL_DATA_REQ */ -int tx_ph_data_req(struct osmocom_ms *ms, struct msgb *msg, - uint8_t chan_nr, uint8_t link_id) -{ - struct l1ctl_info_ul *l1i_ul; - - printf("tx_ph_data_req(%s)\n", hexdump(msg->l2h, msgb_l2len(msg))); - - if (msgb_l2len(msg) > 23) { - printf("L1 cannot handle message length > 23 (%u)\n", msgb_l2len(msg)); - msgb_free(msg); - return -EINVAL; - } else if (msgb_l2len(msg) < 23) - printf("L1 message length < 23 (%u) doesn't seem right!\n", msgb_l2len(msg)); - - /* prepend uplink info header */ - printf("sizeof(struct l1ctl_info_ul)=%lu\n", sizeof(*l1i_ul)); - msg->l1h = msgb_push(msg, sizeof(*l1i_ul)); - l1i_ul = (struct l1ctl_info_ul *) msg->l1h; - - l1i_ul->msg_type = L1CTL_DATA_REQ; - - l1i_ul->chan_nr = chan_nr; - l1i_ul->link_id = link_id; - - /* FIXME: where to get this from? */ - l1i_ul->tx_power = 0; - - return osmo_send_l1(ms, msg); -} - -/* Receive L1CTL_RESET */ -static int rx_l1_reset(struct osmocom_ms *ms) -{ - struct msgb *msg; - struct l1ctl_sync_new_ccch_req *req; - - msg = osmo_l1_alloc(L1CTL_NEW_CCCH_REQ); - if (!msg) - return -1; - - printf("Layer1 Reset.\n"); - req = (struct l1ctl_sync_new_ccch_req *) msgb_put(msg, sizeof(*req)); - req->band_arfcn = osmo_make_band_arfcn(ms); - - return osmo_send_l1(ms, msg); -} - -/* Transmit L1CTL_RACH_REQ */ -int tx_ph_rach_req(struct osmocom_ms *ms) -{ - struct msgb *msg; - struct l1ctl_rach_req *req; - static uint8_t i = 0; - - msg = osmo_l1_alloc(L1CTL_RACH_REQ); - if (!msg) - return -1; - - printf("RACH Req.\n"); - req = (struct l1ctl_rach_req *) msgb_put(msg, sizeof(*req)); - req->ra = i++; - - return osmo_send_l1(ms, msg); -} - -/* Transmit L1CTL_DM_EST_REQ */ -int tx_ph_dm_est_req(struct osmocom_ms *ms, uint16_t band_arfcn, uint8_t chan_nr) -{ - struct msgb *msg; - struct l1ctl_info_ul *ul; - struct l1ctl_dm_est_req *req; - - msg = osmo_l1_alloc(L1CTL_DM_EST_REQ); - if (!msg) - return -1; - - printf("Tx Dedic.Mode Est Req (arfcn=%u, chan_nr=0x%02x)\n", - band_arfcn, chan_nr); - ul = (struct l1ctl_info_ul *) msg->l1h; - ul->chan_nr = chan_nr; - ul->link_id = 0; - ul->tx_power = 0; /* FIXME: initial TX power */ - req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req)); - req->band_arfcn = band_arfcn; - - return osmo_send_l1(ms, msg); - -} - -/* Receive incoming data from L1 using L1CTL format */ -int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg) -{ - int rc = 0; - struct l1ctl_info_dl *dl; - - if (msgb_l2len(msg) < sizeof(*dl)) { - fprintf(stderr, "Short Layer2 message: %u\n", msgb_l2len(msg)); - return -1; - } - - dl = (struct l1ctl_info_dl *) msg->l1h; - msg->l2h = &msg->l1h[0] + sizeof(*dl); - - switch (dl->msg_type) { - case L1CTL_NEW_CCCH_RESP: - rc = rx_l1_ccch_resp(ms, msg); - break; - case L1CTL_DATA_IND: - rc = rx_ph_data_ind(ms, msg); - break; - case L1CTL_RESET: - rc = rx_l1_reset(ms); - break; - default: - fprintf(stderr, "Unknown MSG: %u\n", dl->msg_type); - break; - } - - return rc; -} diff --git a/src/host/layer2/src/lapdm.c b/src/host/layer2/src/lapdm.c deleted file mode 100644 index bdf2c2a..0000000 --- a/src/host/layer2/src/lapdm.c +++ /dev/null @@ -1,836 +0,0 @@ -/* GSM LAPDm (TS 04.06) implementation */ - -/* (C) 2010 by Harald Welte - * - * 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 -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -/* TS 04.06 Figure 4 / Section 3.2 */ -#define LAPDm_LPD_NORMAL 0 -#define LAPDm_LPD_SMSCB 1 -#define LAPDm_SAPI_NORMAL 0 -#define LAPDm_SAPI_SMS 3 -#define LAPDm_ADDR(lpd, sapi, cr) (((lpd & 0x3) << 5) | ((sapi & 0x7) << 2) | ((cr & 0x1) << 1) | 0x1) - -#define LAPDm_ADDR_SAPI(addr) ((addr >> 2) & 0x7) - -/* TS 04.06 Table 3 / Section 3.4.3 */ -#define LAPDm_CTRL_I(nr, ns, p) (((nr & 0x7) << 5) | ((p & 0x1) << 4) | ((ns & 0x7) << 1)) -#define LAPDm_CTRL_S(nr, s, p) (((nr & 0x7) << 5) | ((p & 0x1) << 4) | ((s & 0x3) << 2) | 0x1) -#define LAPDm_CTRL_U(u, p) (((u & 0x1c) << (5-2)) | ((p & 0x1) << 4) | ((u & 0x3) << 2) | 0x3) - -#define LAPDm_CTRL_is_I(ctrl) ((ctrl & 0x1) == 0) -#define LAPDm_CTRL_is_S(ctrl) ((ctrl & 0x3) == 1) -#define LAPDm_CTRL_is_U(ctrl) ((ctrl & 0x3) == 3) - -#define LAPDm_CTRL_U_BITS(ctrl) (((ctrl & 0xC) >> 2) | (ctrl & 0xE0) >> 3) -#define LAPDm_CTRL_PF_BIT(ctrl) ((ctrl >> 4) & 0x1) - -#define LAPDm_CTRL_S_BITS(ctrl) ((ctrl & 0xC) >> 2) - -#define LAPDm_CTRL_I_Ns(ctrl) ((ctrl & 0xE) >> 1) -#define LAPDm_CTRL_I_Nr(ctrl) ((ctrl & 0xE0) >> 5) - -/* TS 04.06 Table 4 / Section 3.8.1 */ -#define LAPDm_U_SABM 0x7 -#define LAPDm_U_DM 0x3 -#define LAPDm_U_UI 0x0 -#define LAPDm_U_DISC 0x8 -#define LAPDm_U_UA 0xC - -#define LAPDm_S_RR 0x0 -#define LAPDm_S_RNR 0x1 -#define LAPDm_S_REJ 0x2 - -#define LAPDm_LEN(len) ((len << 2) | 0x1) - -/* TS 04.06 Section 5.8.3 */ -#define N201_AB_SACCH 18 -#define N201_AB_SDCCH 20 -#define N201_AB_FACCH 20 -#define N201_Bbis 23 -#define N201_Bter_SACCH 21 -#define N201_Bter_SDCCH 23 -#define N201_Bter_FACCH 23 -#define N201_B4 19 - -/* 5.8.2.1 N200 during establish and release */ -#define N200_EST_REL 5 -/* 5.8.2.1 N200 during timer recovery state */ -#define N200_TR_SACCH 5 -#define N200_TR_SDCCH 23 -#define N200_TR_FACCH_FR 34 -#define N200_TR_EFACCH_FR 48 -#define N200_TR_FACCH_HR 29 -/* FIXME: this depends on chan type */ -#define N200 N200_TR_SACCH - -#define CR_MS2BS_CMD 0 -#define CR_MS2BS_RESP 1 -#define CR_BS2MS_CMD 1 -#define CR_BS2MS_RESP 0 - -/* Set T200 to 1 Second (OpenBTS uses 900ms) */ -#define T200 1, 0 - -enum lapdm_format { - LAPDm_FMT_A, - LAPDm_FMT_B, - LAPDm_FMT_Bbis, - LAPDm_FMT_Bter, - LAPDm_FMT_B4, -}; - -struct lapdm_msg_ctx { - struct lapdm_datalink *dl; - enum lapdm_format lapdm_fmt; - uint8_t chan_nr; - uint8_t link_id; - uint8_t addr; - uint8_t ctrl; -}; - -static void lapdm_t200_cb(void *data); - -/* UTILITY FUNCTIONS */ - -static inline uint8_t inc_mod8(uint8_t x) -{ - return (x + 1) % 8; -} - -static void lapdm_dl_init(struct lapdm_datalink *dl, - struct lapdm_entity *entity) -{ - memset(dl, 0, sizeof(*dl)); - dl->t200.data = dl; - dl->t200.cb = &lapdm_t200_cb; - dl->entity = entity; -} - -void lapdm_init(struct lapdm_entity *le, struct osmocom_ms *ms) -{ - unsigned int i; - - for (i = 0; i < ARRAY_SIZE(le->datalink); i++) - lapdm_dl_init(&le->datalink[i], le); - - le->ms = ms; -} - -static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi) -{ - switch (sapi) { - case LAPDm_SAPI_NORMAL: - return &le->datalink[0]; - case LAPDm_SAPI_SMS: - return &le->datalink[1]; - default: - return NULL; - } -} - -/* remove the L2 header from a MSGB */ -static inline unsigned char *msgb_pull_l2h(struct msgb *msg) -{ - unsigned char *ret = msgb_pull(msg, msg->l3h - msg->l2h); - msg->l2h = NULL; - return ret; -} - -/* Append padding (if required) */ -static void lapdm_pad_msgb(struct msgb *msg) -{ - int pad_len = 23 - msgb_l2len(msg); - uint8_t *data; - - if (pad_len < 0) { - printf("cannot pad message that is already too big!\n"); - return; - } - - data = msgb_put(msg, pad_len); - memset(data, 0x2B, pad_len); -} - -static int tx_ph_data_req_pad(struct osmocom_ms *ms, struct msgb *msg, - uint8_t chan_nr, uint8_t link_id) -{ - lapdm_pad_msgb(msg); - return tx_ph_data_req(ms, msg, chan_nr, link_id); -} - -/* Take a Bbis format message from L1 and create RSLms UNIT DATA IND */ -static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx, - struct msgb *msg) -{ - /* Add the RSL + RLL header */ - rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id); - - /* send off the RSLms message to L3 */ - return rslms_sendmsg(msg, mctx->dl->entity->ms); -} - -static int send_rslms_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx) -{ - struct msgb *msg; - - msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id); - - /* send off the RSLms message to L3 */ - return rslms_sendmsg(msg, mctx->dl->entity->ms); -} - -static int check_length_ind(uint8_t length_ind) -{ - if (!(length_ind & 0x01)) { - /* G.4.1 If the EL bit is set to "0", an MDL-ERROR-INDICATION - * primitive with cause "frame not implemented" is sent to the - * mobile management entity. */ - printf("we don't support multi-octet length\n"); - return -EINVAL; - } - if (length_ind & 0x02) { - printf("we don't support LAPDm fragmentation yet\n"); - return -EINVAL; - } - return 0; -} - -/* Timer callback on T200 expiry */ -static void lapdm_t200_cb(void *data) -{ - struct lapdm_datalink *dl = data; - - printf("lapdm_t200_cb(%p) state=%u\n", dl, dl->state); - - switch (dl->state) { - case LAPDm_STATE_SABM_SENT: - /* 5.4.1.3 */ - if (dl->retrans_ctr >= N200_EST_REL + 1) { - /* FIXME: send RELEASE INDICATION to L3 */ - dl->retrans_ctr = 0; - dl->state = LAPDm_STATE_IDLE; - } - /* FIXME: retransmit SABM command */ - //rslms_rx_rll_est_req(msg, dl); - /* increment re-transmission counter */ - dl->retrans_ctr++; - /* restart T200 (PH-READY-TO-SEND) */ - bsc_schedule_timer(&dl->t200, T200); - break; - case LAPDm_STATE_MF_EST: - /* 5.5.7 */ - dl->retrans_ctr = 0; - dl->state = LAPDm_STATE_TIMER_RECOV; - case LAPDm_STATE_TIMER_RECOV: - dl->retrans_ctr++; - if (dl->retrans_ctr < N200) { - /* FIXME: retransmit I frame (V_s-1) with P=1 */ - /* FIXME: send appropriate supervision frame with P=1 */ - /* restart T200 (PH-READY-TO-SEND) */ - bsc_schedule_timer(&dl->t200, T200); - } else { - /* FIXME: send ERROR INDICATION to L3 */ - } - break; - default: - printf("T200 expired in unexpected dl->state %u\n", dl->state); - } -} - -static int lapdm_send_rr(struct lapdm_msg_ctx *mctx, uint8_t f_bit) -{ - uint8_t sapi = mctx->link_id & 7; - struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RR"); - msg->l2h = msgb_put(msg, 3); - - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP); - msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RR, f_bit); - msg->l2h[2] = LAPDm_LEN(0); - - return tx_ph_data_req_pad(mctx->dl->entity->ms, msg, mctx->chan_nr, mctx->link_id); -} - -/* L1 -> L2 */ - -/* Receive a LAPDm S (Unnumbered) message from L1 */ -static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx) -{ - struct lapdm_datalink *dl = mctx->dl; - uint8_t length; - int rc; - - switch (LAPDm_CTRL_U_BITS(mctx->ctrl)) { - case LAPDm_U_SABM: - printf("SABM "); - /* Must be Format B */ - rc = check_length_ind(msg->l2h[2]); - if (rc < 0) - return rc; - length = msg->l2h[2] >> 2; - /* FIXME: G.4.5 check */ - if (dl->state == LAPDm_STATE_MF_EST) { - if (length == 0) { - /* FIXME: re-establishment procedure 5.6 */ - } else { - /* FIXME: check for contention resoultion */ - printf("SABM command, multiple frame established state\n"); - return 0; - } - } - if (length == 0) { - /* 5.4.1.2 Normal establishment procedures */ - rc = send_rslms_rll_simple(RSL_MT_EST_IND, mctx); - } else { - /* 5.4.1.4 Contention resolution establishment */ - msg->l3h = msg->l2h + 3; - msgb_pull_l2h(msg); - rc = send_rslms_rll_l3(RSL_MT_EST_IND, mctx, msg); - } -#if 0 - if (rc == 0) - dl->state = LAPDm_STATE_SABM_SENT; -#endif - break; - case LAPDm_U_DM: - printf("DM "); - if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - /* 5.4.1.2 DM responses with the F bit set to "0" shall be ignored. */ - return 0; - } - switch (dl->state) { - case LAPDm_STATE_IDLE: - /* 5.4.5 all other frame types shall be discarded */ - printf("state=IDLE (discarding) "); - return 0; - case LAPDm_STATE_MF_EST: - if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 1) - printf("unsolicited DM resposne "); - else - printf("unsolicited DM resposne, multiple frame established state "); - return 0; - case LAPDm_STATE_TIMER_RECOV: - /* DM is normal in case PF = 1 */ - if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 0) { - printf("unsolicited DM resposne, multiple frame established state "); - return 0; - } - break; - } - /* reset T200 */ - bsc_del_timer(&dl->t200); - rc = send_rslms_rll_simple(RSL_MT_REL_IND, mctx); - break; - case LAPDm_U_UI: - printf("UI "); - if (mctx->lapdm_fmt == LAPDm_FMT_B4) { - length = N201_B4; - msg->l3h = msg->l2h + 2; - } else { - rc = check_length_ind(msg->l2h[2]); - if (rc < 0) - return rc; - length = msg->l2h[2] >> 2; - msg->l3h = msg->l2h + 3; - } - /* do some length checks */ - if (length == 0) { - /* 5.3.3 UI frames received with the length indicator set to "0" shall be ignored */ - printf("length=0 (discarding) "); - return 0; - } - /* FIXME: G.4.5 check */ - switch (LAPDm_ADDR_SAPI(mctx->ctrl)) { - case LAPDm_SAPI_NORMAL: - case LAPDm_SAPI_SMS: - break; - default: - /* 5.3.3 UI frames with invalid SAPI values shall be discarded */ - printf("sapi=%u (discarding) ", LAPDm_ADDR_SAPI(mctx->ctrl)); - return 0; - } - msgb_pull_l2h(msg); - rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, mctx, msg); - break; - case LAPDm_U_DISC: - printf("DISC "); - length = msg->l2h[2] >> 2; - if (length > 0 || msg->l2h[2] & 0x02) { - /* G.4.4 If a DISC or DM frame is received with L>0 or - * with the M bit set to "1", an MDL-ERROR-INDICATION - * primitive with cause "U frame with incorrect - * parameters" is sent to the mobile management entity. */ - printf("U frame iwth incorrect parameters "); - return -EIO; - } - switch (dl->state) { - case LAPDm_STATE_IDLE: - /* FIXME: send DM with F=P */ - break; - default: - /* FIXME */ - break; - } - break; - case LAPDm_U_UA: - printf("UA "); - /* FIXME: G.4.5 check */ - if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - /* 5.4.1.2 A UA response with the F bit set to "0" shall be ignored. */ - printf("F=0 (discarding) "); - return 0; - } - switch (dl->state) { - case LAPDm_STATE_SABM_SENT: - break; - case LAPDm_STATE_IDLE: - /* 5.4.5 all other frame types shall be discarded */ - default: - printf("unsolicited UA response! (discarding) "); - return 0; - } - /* reset Timer T200 */ - bsc_del_timer(&dl->t200); - /* set Vs, Vr and Va to 0 */ - dl->V_send = dl->V_recv = dl->V_ack = 0; - /* enter multiple-frame-established state */ - dl->state = LAPDm_STATE_MF_EST; - /* send notification to L3 */ - rc = send_rslms_rll_simple(RSL_MT_EST_CONF, mctx); - break; - } - return rc; -} - -/* Receive a LAPDm S (Supervisory) message from L1 */ -static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx) -{ - struct lapdm_datalink *dl = mctx->dl; - uint8_t length; - - length = msg->l2h[2] >> 2; - if (length > 0 || msg->l2h[2] & 0x02) { - /* G.4.3 If a supervisory frame is received with L>0 or - * with the M bit set to "1", an MDL-ERROR-INDICATION - * primitive with cause "S frame with incorrect - * parameters" is sent to the mobile management entity. */ - return -EIO; - } - switch (dl->state) { - case LAPDm_STATE_IDLE: - /* FIXME: if P=1, respond DM with F=1 (5.2.2) */ - /* 5.4.5 all other frame types shall be discarded */ - break; - } - switch (LAPDm_CTRL_S_BITS(mctx->ctrl)) { - case LAPDm_S_RR: - /* FIXME */ - break; - case LAPDm_S_RNR: - /* FIXME */ - break; - case LAPDm_S_REJ: - /* FIXME */ - break; - } - return 0; -} - -/* Receive a LAPDm I (Information) message from L1 */ -static int lapdm_rx_i(struct msgb *msg, struct lapdm_msg_ctx *mctx) -{ - struct lapdm_datalink *dl = mctx->dl; - uint8_t nr = LAPDm_CTRL_I_Nr(mctx->ctrl); - uint8_t ns = LAPDm_CTRL_I_Ns(mctx->ctrl); - uint8_t length; - int rc; - - length = msg->l2h[2] >> 2; - /* FIXME: check for length > N201 */ - if (length == 0) { - /* G.4.2 If the length indicator of an I frame is set - * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION - * primitive with cause "I frame with incorrect length" - * is sent to the mobile management entity. */ - return -EIO; - } - /* FIXME: G.4.2 If the numerical value of L is Lstate) { - case LAPDm_STATE_IDLE: - /* FIXME: if P=1, respond DM with F=1 (5.2.2) */ - /* 5.4.5 all other frame types shall be discarded */ - break; - } - - /* processing of Nr, Ns and P fields */ - if (ns == dl->V_recv) { - /* FIXME: check for M bit! */ - dl->V_recv = inc_mod8(dl->V_recv); - - /* send a DATA INDICATION to L3 */ - msg->l3h = msg->l2h + 2; - msgb_pull_l2h(msg); - rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, msg); - } else { - printf("N(s) sequence error: Ns=%u, V_recv=%u ", ns, dl->V_recv); - /* FIXME: 5.7.1: N(s) sequence error */ - /* discard data */ - return -EIO; - } - - /* Check for P bit */ - if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - /* 5.5.2.1 */ - /* FIXME: check ifwe are in own receiver busy */ - /* FIXME: Send RR with F=1 */ - rc = lapdm_send_rr(mctx, 1); - } else { - /* 5.5.2.2 */ - /* FIXME: check ifwe are in own receiver busy */ - //if (we_have_I_frame_pending) { - if (0) { - /* FIXME: send that I frame with Nr=Vr */ - } else { - /* Send RR with F=0 */ - rc = lapdm_send_rr(mctx, 0); - } - } - - if (dl->state != LAPDm_STATE_TIMER_RECOV) { - /* When not in the timer recovery condition, the data - * link layer entity shall reset the timer T200 on - * receipt of a valid I frame with N(R) higher than V(A) */ - if (nr > dl->V_ack) { - /* FIXME: 5.5.3.1 Note 1 + 2 */ - bsc_del_timer(&dl->t200); - /* FIXME: if there are outstanding I frames - * still unacknowledged, the data link layer - * entity shall set timer T200 */ - } - - /* FIXME: 5.7.4: N(R) sequence error */ - /* N(R) is called valid, if and only if (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8. */ - } - - /* V(A) shall be set to the value of N(R) */ - dl->V_ack = LAPDm_CTRL_I_Nr(mctx->ctrl); - - return rc; -} - -/* Receive a LAPDm message from L1 */ -static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx) -{ - int rc; - - if (LAPDm_CTRL_is_U(mctx->ctrl)) - rc = lapdm_rx_u(msg, mctx); - else if (LAPDm_CTRL_is_S(mctx->ctrl)) - rc = lapdm_rx_s(msg, mctx); - else if (LAPDm_CTRL_is_I(mctx->ctrl)) - rc = lapdm_rx_i(msg, mctx); - else { - printf("unknown LAPDm format "); - rc = -EINVAL; - } - return rc; -} - -/* input into layer2 (from layer 1) */ -int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1ctl_info_dl *l1i) -{ - uint8_t cbits = l1i->chan_nr >> 3; - uint8_t sapi = l1i->link_id & 7; - struct lapdm_msg_ctx mctx; - int rc; - - printf("l2_ph_data_ind() "); - /* when we reach here, we have a msgb with l2h pointing to the raw - * 23byte mac block. The l1h has already been purged. */ - - mctx.dl = datalink_for_sapi(le, sapi); - mctx.chan_nr = l1i->chan_nr; - mctx.link_id = l1i->link_id; - mctx.addr = mctx.ctrl = 0; - - /* check for L1 chan_nr/link_id and determine LAPDm hdr format */ - if (cbits == 0x10 || cbits == 0x12) { - /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */ - mctx.lapdm_fmt = LAPDm_FMT_Bbis; - printf("fmt=Bbis "); - } else { - if (mctx.link_id & 0x40) { - /* It was received from network on SACCH, thus - * lapdm_fmt must be B4 */ - mctx.lapdm_fmt = LAPDm_FMT_B4; - printf("fmt=B4 "); - /* SACCH frames have a two-byte L1 header that OsmocomBB L1 doesn't - * strip */ - msg->l2h += 2; - } else { - mctx.lapdm_fmt = LAPDm_FMT_B; - printf("fmt=B "); - } - } - - switch (mctx.lapdm_fmt) { - case LAPDm_FMT_A: - case LAPDm_FMT_B: - case LAPDm_FMT_B4: - mctx.addr = msg->l2h[0]; - if (!(mctx.addr & 0x01)) { - printf("we don't support multibyte addresses (discarding)\n"); - return -EINVAL; - } - mctx.ctrl = msg->l2h[1]; - /* obtain SAPI from address field */ - mctx.link_id |= LAPDm_ADDR_SAPI(mctx.addr); - rc = lapdm_ph_data_ind(msg, &mctx); - break; - case LAPDm_FMT_Bter: - /* FIXME */ - break; - case LAPDm_FMT_Bbis: - /* directly pass up to layer3 */ - printf("UI "); - msg->l3h = msg->l2h; - msgb_pull_l2h(msg); - rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg); - break; - } - printf("\n"); - - return rc; -} - -/* L3 -> L2 / RSLMS -> LAPDm */ - -/* L3 requests establishment of data link */ -static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl) -{ - struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); - uint8_t chan_nr = rllh->chan_nr; - uint8_t link_id = rllh->link_id; - uint8_t sapi = rllh->link_id & 7; - struct tlv_parsed tv; - uint8_t len; - - printf("RSL_MT_EST_REQ "); - - rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); - if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { - /* contention resolution establishment procedure */ - if (dl->state != LAPDm_STATE_IDLE) { - /* 5.4.1.4: The data link layer shall, however, ignore any such - * service request if it is not in the idle state when the - * request is received. */ - printf("DL state != IDLE (discarding)\n"); - msgb_free(msg); - return 0; - } - if (sapi != 0) { - /* According to clause 6, the contention resolution - * procedure is only permitted with SAPI value 0 */ - printf("SAPI != 0 but contention resolution (discarding)\n"); - msgb_free(msg); - return -EINVAL; - } - /* transmit a SABM command with the P bit set to "1". The SABM - * command shall contain the layer 3 message unit */ - len = LAPDm_LEN(TLVP_LEN(&tv, RSL_IE_L3_INFO)); - - /* FIXME: store information field in dl entity */ - } else { - /* normal establishment procedure */ - len = LAPDm_LEN(0); - } - - /* Remove RLL header from msgb */ - msgb_pull_l2h(msg); - - /* Push LAPDm header on msgb */ - msg->l2h = msgb_push(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD); - msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1); - msg->l2h[2] = len; - - /* Tramsmit and start T200 */ - dl->state = LAPDm_STATE_SABM_SENT; - bsc_schedule_timer(&dl->t200, T200); - return tx_ph_data_req_pad(dl->entity->ms, msg, chan_nr, link_id); -} - -/* L3 requests transfer of unnumbered information */ -static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl) -{ - struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); - uint8_t chan_nr = rllh->chan_nr; - uint8_t link_id = rllh->link_id; - uint8_t sapi = link_id & 7; - struct tlv_parsed tv; - - rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); - - /* Remove RLL header from msgb */ - msgb_pull_l2h(msg); - - /* Push LAPDm header on msgb */ - msg->l2h = msgb_push(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD); - msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1); - msg->l2h[2] = LAPDm_LEN(TLVP_LEN(&tv, RSL_IE_L3_INFO)); - - /* Tramsmit and start T200 */ - bsc_schedule_timer(&dl->t200, T200); - return tx_ph_data_req_pad(dl->entity->ms, msg, chan_nr, link_id); -} - -/* L3 requests transfer of acknowledged information */ -static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl) -{ - struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); - uint8_t chan_nr = rllh->chan_nr; - uint8_t link_id = rllh->link_id; - uint8_t sapi = rllh->link_id & 7; - struct tlv_parsed tv; - - switch (dl->state) { - case LAPDm_STATE_MF_EST: - break; - default: - printf("refusing RLL DATA REQ during DL state %u\n", dl->state); - return -EIO; - break; - } - - /* FIXME: check if the layer3 message length exceeds N201 */ - - rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); - - /* Remove the RSL/RLL header */ - msgb_pull_l2h(msg); - - /* Push the LAPDm header */ - msg->l2h = msgb_put(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD); - msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0); - msg->l2h[2] = LAPDm_LEN(TLVP_LEN(&tv, RSL_IE_L3_INFO)); - - /* The value of the send state variable V(S) shall be incremented by 1 - * at the end of the transmission of the I frame */ - dl->V_send = inc_mod8(dl->V_send); - - /* If timer T200 is not running at the time right before transmitting a - * frame, when the PH-READY-TO-SEND primitive is received from the - * physical layer., it shall be set. */ - if (!bsc_timer_pending(&dl->t200)) - bsc_schedule_timer(&dl->t200, T200); - - /* FIXME: If the send state variable V(S) is equal to V(A) plus k - * (where k is the maximum number of outstanding I frames - see - * subclause 5.8.4), the data link layer entity shall not transmit any - * new I frames, but shall retransmit an I frame as a result - * of the error recovery procedures as described in subclauses 5.5.4 and - * 5.5.7. */ - - return tx_ph_data_req_pad(dl->entity->ms, msg, chan_nr, link_id); -} - -/* incoming RSLms RLL message from L3 */ -static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms) -{ - struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); - int rc = 0; - uint8_t sapi = rllh->link_id & 7; - struct lapdm_entity *le; - struct lapdm_datalink *dl; - - if (rllh->link_id & 0x40) - le = &ms->lapdm_acch; - else - le = &ms->lapdm_dcch; - dl = datalink_for_sapi(le, sapi); - - switch (rllh->c.msg_type) { - case RSL_MT_UNIT_DATA_REQ: - /* create and send UI command */ - rc = rslms_rx_rll_udata_req(msg, dl); - break; - case RSL_MT_EST_REQ: - /* create and send SABM command */ - rc = rslms_rx_rll_est_req(msg, dl); - break; - case RSL_MT_DATA_REQ: - /* create and send I command */ - rc = rslms_rx_rll_data_req(msg, dl); - break; - case RSL_MT_REL_REQ: - /* FIXME: create and send DISC command */ - default: - printf("unknown RLL message type 0x%02x\n", - rllh->c.msg_type); - break; - } - - return rc; -} - -/* input into layer2 (from layer 3) */ -int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms) -{ - struct abis_rsl_common_hdr *rslh = msgb_l2(msg); - int rc = 0; - - switch (rslh->msg_discr & 0xfe) { - case ABIS_RSL_MDISC_RLL: - printf("rslms_recvmsg(ABIS_RSL_MDISC_RLL)\n"); - rc = rslms_rx_rll(msg, ms); - break; - default: - printf("unknown RSLms message discriminator 0x%02x", - rslh->msg_discr); - msgb_free(msg); - return -EINVAL; - } - - return rc; -} - diff --git a/src/host/layer2/src/layer2_main.c b/src/host/layer2/src/layer2_main.c deleted file mode 100644 index 453fe6d..0000000 --- a/src/host/layer2/src/layer2_main.c +++ /dev/null @@ -1,225 +0,0 @@ -/* Main method of the layer2/3 stack */ - -/* (C) 2010 by Holger Hans Peter Freyther - * (C) 2010 by Harald Welte - * - * 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 -#include -#include - -#include -#include -#include -#include - -#include - -#include -#include - -#define _GNU_SOURCE -#include -#include -#include -#include -#include - -#include "gsmtap_util.h" - -#define GSM_L2_LENGTH 256 - -static void *l2_ctx = NULL; -static char *socket_path = "/tmp/osmocom_l2"; -static struct osmocom_ms *ms = NULL; - -static int layer2_read(struct bsc_fd *fd, unsigned int flags) -{ - struct msgb *msg; - u_int16_t len; - int rc; - - msg = msgb_alloc(GSM_L2_LENGTH, "Layer2"); - if (!msg) { - fprintf(stderr, "Failed to allocate msg.\n"); - return -1; - } - - rc = read(fd->fd, &len, sizeof(len)); - if (rc < sizeof(len)) { - fprintf(stderr, "Short read. Error.\n"); - exit(2); - } - - len = ntohs(len); - if (len > GSM_L2_LENGTH) { - fprintf(stderr, "Length is too big: %u\n", len); - msgb_free(msg); - return -1; - } - - - /* blocking read for the poor... we can starve in here... */ - msg->l1h = msgb_put(msg, len); - rc = read(fd->fd, msg->l1h, msgb_l1len(msg)); - if (rc != msgb_l1len(msg)) { - fprintf(stderr, "Can not read data: len=%d rc=%d errno=%d\n", len, rc, errno); - msgb_free(msg); - return -1; - } - - l1ctl_recv((struct osmocom_ms *) fd->data, msg); - msgb_free(msg); - return 0; -} - -int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg) -{ - int rc; - uint16_t *len; - - printf("Sending: '%s'\n", hexdump(msg->data, msg->len)); - - if (msg->l1h != msg->data) - printf("Message L1 header != Message Data\n"); - - /* prepend 16bit length before sending */ - len = (uint16_t *) msgb_push(msg, sizeof(*len)); - *len = htons(msg->len - sizeof(*len)); - - /* TODO: just enqueue the message and wait for ready write.. */ - rc = write(ms->bfd.fd, msg->data, msg->len); - if (rc != msg->len) { - fprintf(stderr, "Failed to write data: rc: %d\n", rc); - msgb_free(msg); - return -1; - } - - msgb_free(msg); - return 0; -} - -static void print_usage() -{ - printf("Usage: ./layer2\n"); -} - -static void print_help() -{ - printf(" Some help...\n"); - printf(" -h --help this text\n"); - printf(" -s --socket /tmp/osmocom_l2. Path to the unix domain socket\n"); - printf(" -a --arfcn NR. The ARFCN to be used for layer2.\n"); -} - -static void handle_options(int argc, char **argv) -{ - while (1) { - int option_index = 0, c; - static struct option long_options[] = { - {"help", 0, 0, 'h'}, - {"socket", 1, 0, 's'}, - {"arfcn", 1, 0, 'a'}, - {0, 0, 0, 0}, - }; - - c = getopt_long(argc, argv, "hs:a:", - long_options, &option_index); - if (c == -1) - break; - - switch (c) { - case 'h': - print_usage(); - print_help(); - exit(0); - break; - case 's': - socket_path = talloc_strdup(l2_ctx, optarg); - break; - case 'a': - ms->arfcn = atoi(optarg); - break; - default: - break; - } - } -} - -int main(int argc, char **argv) -{ - int rc; - struct sockaddr_un local; - struct sockaddr_in gsmtap; - - l2_ctx = talloc_named_const(NULL, 1, "layer2 context"); - - ms = talloc_zero(l2_ctx, struct osmocom_ms); - if (!ms) { - fprintf(stderr, "Failed to allocate MS\n"); - exit(1); - } - - ms->arfcn = 871; - - handle_options(argc, argv); - - ms->bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0); - if (ms->bfd.fd < 0) { - fprintf(stderr, "Failed to create unix domain socket.\n"); - exit(1); - } - - local.sun_family = AF_UNIX; - strncpy(local.sun_path, socket_path, sizeof(local.sun_path)); - local.sun_path[sizeof(local.sun_path) - 1] = '\0'; - - rc = connect(ms->bfd.fd, (struct sockaddr *) &local, - sizeof(local.sun_family) + strlen(local.sun_path)); - if (rc < 0) { - fprintf(stderr, "Failed to connect to '%s'.\n", local.sun_path); - exit(1); - } - - ms->bfd.when = BSC_FD_READ; - ms->bfd.cb = layer2_read; - ms->bfd.data = ms; - - lapdm_init(&ms->lapdm_dcch, ms); - lapdm_init(&ms->lapdm_acch, ms); - - if (bsc_register_fd(&ms->bfd) != 0) { - fprintf(stderr, "Failed to register fd.\n"); - exit(1); - } - - rc = gsmtap_init(); - if (rc < 0) { - fprintf(stderr, "Failed during gsmtap_init()\n"); - exit(1); - } - - while (1) { - bsc_select_main(0); - } - - - return 0; -} diff --git a/src/host/layer2/src/layer3.c b/src/host/layer2/src/layer3.c deleted file mode 100644 index ea6491e..0000000 --- a/src/host/layer2/src/layer3.c +++ /dev/null @@ -1,216 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -static void dump_bcch(uint8_t tc, const uint8_t *data) -{ - struct gsm48_system_information_type_header *si_hdr; - si_hdr = (struct gsm48_system_information_type_header *) data;; - - /* GSM 05.02 ยง6.3.1.3 Mapping of BCCH data */ - switch (si_hdr->system_information) { - case GSM48_MT_RR_SYSINFO_1: - fprintf(stderr, "\tSI1"); -#ifdef BCCH_TC_CHECK - if (tc != 0) - fprintf(stderr, " on wrong TC"); -#endif - break; - case GSM48_MT_RR_SYSINFO_2: - fprintf(stderr, "\tSI2"); -#ifdef BCCH_TC_CHECK - if (tc != 1) - fprintf(stderr, " on wrong TC"); -#endif - break; - case GSM48_MT_RR_SYSINFO_3: - fprintf(stderr, "\tSI3"); -#ifdef BCCH_TC_CHECK - if (tc != 2 && tc != 6) - fprintf(stderr, " on wrong TC"); -#endif - break; - case GSM48_MT_RR_SYSINFO_4: - fprintf(stderr, "\tSI4"); -#ifdef BCCH_TC_CHECK - if (tc != 3 && tc != 7) - fprintf(stderr, " on wrong TC"); -#endif - break; - case GSM48_MT_RR_SYSINFO_5: - fprintf(stderr, "\tSI5"); - break; - case GSM48_MT_RR_SYSINFO_6: - fprintf(stderr, "\tSI6"); - break; - case GSM48_MT_RR_SYSINFO_7: - fprintf(stderr, "\tSI7"); -#ifdef BCCH_TC_CHECK - if (tc != 7) - fprintf(stderr, " on wrong TC"); -#endif - break; - case GSM48_MT_RR_SYSINFO_8: - fprintf(stderr, "\tSI8"); -#ifdef BCCH_TC_CHECK - if (tc != 3) - fprintf(stderr, " on wrong TC"); -#endif - break; - case GSM48_MT_RR_SYSINFO_9: - fprintf(stderr, "\tSI9"); -#ifdef BCCH_TC_CHECK - if (tc != 4) - fprintf(stderr, " on wrong TC"); -#endif - break; - case GSM48_MT_RR_SYSINFO_13: - fprintf(stderr, "\tSI13"); -#ifdef BCCH_TC_CHECK - if (tc != 4 && tc != 0) - fprintf(stderr, " on wrong TC"); -#endif - break; - case GSM48_MT_RR_SYSINFO_16: - fprintf(stderr, "\tSI16"); -#ifdef BCCH_TC_CHECK - if (tc != 6) - fprintf(stderr, " on wrong TC"); -#endif - break; - case GSM48_MT_RR_SYSINFO_17: - fprintf(stderr, "\tSI17"); -#ifdef BCCH_TC_CHECK - if (tc != 2) - fprintf(stderr, " on wrong TC"); -#endif - break; - case GSM48_MT_RR_SYSINFO_2bis: - fprintf(stderr, "\tSI2bis"); -#ifdef BCCH_TC_CHECK - if (tc != 5) - fprintf(stderr, " on wrong TC"); -#endif - break; - case GSM48_MT_RR_SYSINFO_2ter: - fprintf(stderr, "\tSI2ter"); -#ifdef BCCH_TC_CHECK - if (tc != 5 && tc != 4) - fprintf(stderr, " on wrong TC"); -#endif - break; - case GSM48_MT_RR_SYSINFO_5bis: - fprintf(stderr, "\tSI5bis"); - break; - case GSM48_MT_RR_SYSINFO_5ter: - fprintf(stderr, "\tSI5ter"); - break; - default: - fprintf(stderr, "\tUnknown SI"); - break; - }; - - fprintf(stderr, "\n"); -} - - -/* send location updating request * (as part of RSLms EST IND / - LAPDm SABME) */ -static int gsm48_tx_loc_upd_req(struct osmocom_ms *ms, uint8_t chan_nr) -{ - struct msgb *msg = msgb_alloc_headroom(256, 16, "loc_upd_req"); - struct gsm48_hdr *gh; - struct gsm48_loc_upd_req *lu_r; - - printf("gsm48_tx_loc_upd_req()\n"); - - msg->l3h = msgb_put(msg, sizeof(*gh)); - gh = (struct gsm48_hdr *) msg->l3h; - gh->proto_discr = GSM48_PDISC_MM; - gh->msg_type = GSM48_MT_MM_LOC_UPD_REQUEST; - lu_r = (struct gsm48_loc_upd_req *) msgb_put(msg, sizeof(*lu_r)); - lu_r->type = GSM48_LUPD_IMSI_ATT; - lu_r->key_seq = 0; - /* FIXME: set LAI and CM1 */ - /* FIXME: set MI */ - lu_r->mi_len = 0; - - return rslms_tx_rll_req_l3(ms, RSL_MT_EST_REQ, chan_nr, 0, msg); -} - -static int gsm48_rx_imm_ass(struct msgb *msg, struct osmocom_ms *ms) -{ - struct gsm48_imm_ass *ia = msgb_l3(msg); - uint8_t ch_type, ch_subch, ch_ts; - uint16_t arfcn; - - rsl_dec_chan_nr(ia->chan_desc.chan_nr, &ch_type, &ch_subch, &ch_ts); - arfcn = ia->chan_desc.h0.arfcn_low | (ia->chan_desc.h0.arfcn_high << 8); - - printf("GSM48 IMM ASS (ra=0x%02x, chan_nr=0x%02x, ARFCN=%u, TS=%u, SS=%u, TSC=%u) ", - ia->req_ref.ra, ia->chan_desc.chan_nr, arfcn, ch_ts, ch_subch, - ia->chan_desc.h0.tsc); - - /* FIXME: compare RA and GSM time with when we sent RACH req */ - - /* check if we can support this type of channel at the moment */ - if (ch_type != RSL_CHAN_SDCCH4_ACCH || ch_ts != 0 || - ia->chan_desc.h0.h == 1) { - printf("UNSUPPORTED!\n"); - return 0; - } - - /* request L1 to go to dedicated mode on assigned channel */ - tx_ph_dm_est_req(ms, arfcn, ia->chan_desc.chan_nr); - - /* request L2 to establish the SAPI0 connection */ - gsm48_tx_loc_upd_req(ms, ia->chan_desc.chan_nr); - - return 0; -} - -int gsm48_rx_ccch(struct msgb *msg, struct osmocom_ms *ms) -{ - struct gsm48_system_information_type_header *sih = msgb_l3(msg); - int rc = 0; - - if (sih->rr_protocol_discriminator != GSM48_PDISC_RR) - printf("PCH pdisc != RR\n"); - - switch (sih->system_information) { - case GSM48_MT_RR_PAG_REQ_1: - case GSM48_MT_RR_PAG_REQ_2: - case GSM48_MT_RR_PAG_REQ_3: - /* FIXME: implement decoding of paging request */ - break; - case GSM48_MT_RR_IMM_ASS: - rc = gsm48_rx_imm_ass(msg, ms); - break; - default: - printf("unknown PCH/AGCH type 0x%02x\n", sih->system_information); - rc = -EINVAL; - } - - return rc; -} - -int gsm48_rx_bcch(struct msgb *msg, struct osmocom_ms *ms) -{ - /* FIXME: we have lost the gsm frame time until here, need to store it - * in some msgb context */ - //dump_bcch(dl->time.tc, ccch->data); - dump_bcch(0, msg->l3h); - - return 0; -} diff --git a/src/host/layer2/src/libosmocom/debug.c b/src/host/layer2/src/libosmocom/debug.c deleted file mode 120000 index acf4261..0000000 --- a/src/host/layer2/src/libosmocom/debug.c +++ /dev/null @@ -1 +0,0 @@ -../../../libosmocom/src/debug.c \ No newline at end of file diff --git a/src/host/layer2/src/rslms.c b/src/host/layer2/src/rslms.c deleted file mode 100644 index f456029..0000000 --- a/src/host/layer2/src/rslms.c +++ /dev/null @@ -1,162 +0,0 @@ -/* RSLms - GSM 08.58 like protocol between L2 and L3 of GSM Um interface */ - -/* (C) 2010 by Harald Welte - * - * 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 -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -int rsl_dec_chan_nr(uint8_t chan_nr, uint8_t *type, uint8_t *subch, uint8_t *timeslot) -{ - *timeslot = chan_nr & 0x7; - - if ((chan_nr & 0xf8) == RSL_CHAN_Bm_ACCHs) { - *type = RSL_CHAN_Bm_ACCHs; - *subch = 0; - } else if ((chan_nr & 0xf0) == RSL_CHAN_Lm_ACCHs) { - *type = RSL_CHAN_Lm_ACCHs; - *subch = (chan_nr >> 3) & 0x1; - } else if ((chan_nr & 0xe0) == RSL_CHAN_SDCCH4_ACCH) { - *type = RSL_CHAN_SDCCH4_ACCH; - *subch = (chan_nr >> 3) & 0x3; - } else if ((chan_nr & 0xc0) == RSL_CHAN_SDCCH8_ACCH) { - *type = RSL_CHAN_SDCCH8_ACCH; - *subch = (chan_nr >> 3) & 0x7; - } else { - printf("unable to decode chan_nr\n"); - return -EINVAL; - } - - return 0; -} - -/* Send a 'simple' RLL request to L2 */ -int rslms_tx_rll_req(struct osmocom_ms *ms, uint8_t msg_type, - uint8_t chan_nr, uint8_t link_id) -{ - struct msgb *msg; - - msg = rsl_rll_simple(msg_type, chan_nr, link_id); - - return rslms_recvmsg(msg, ms); -} - -/* Send a RLL request (including L3 info) to L2 */ -int rslms_tx_rll_req_l3(struct osmocom_ms *ms, uint8_t msg_type, - uint8_t chan_nr, uint8_t link_id, struct msgb *msg) -{ - rsl_rll_push_l3(msg, msg_type, chan_nr, link_id); - - return rslms_recvmsg(msg, ms); -} - -static int rach_count = 0; - -static int rslms_rx_udata_ind(struct msgb *msg, struct osmocom_ms *ms) -{ - struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); - struct tlv_parsed tv; - int rc = 0; - - printf("RSLms UNIT DATA IND chan_nr=0x%02x link_id=0x%02x\n", - rllh->chan_nr, rllh->link_id); - - rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); - if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { - printf("UNIT_DATA_IND without L3 INFO ?!?\n"); - return -EIO; - } - msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO); - - if (rllh->chan_nr == RSL_CHAN_PCH_AGCH) - rc = gsm48_rx_ccch(msg, ms); - else if (rllh->chan_nr == RSL_CHAN_BCCH) { - rc = gsm48_rx_bcch(msg); - if (rach_count < 2) { - tx_ph_rach_req(ms); - rach_count++; - } - } - - return rc; -} - -static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms) -{ - struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); - int rc = 0; - - switch (rllh->c.msg_type) { - case RSL_MT_DATA_IND: - printf("RSLms DATA IND\n"); - break; - case RSL_MT_UNIT_DATA_IND: - rc = rslms_rx_udata_ind(msg, ms); - break; - case RSL_MT_EST_IND: - printf("RSLms EST IND\n"); - break; - case RSL_MT_EST_CONF: - printf("RSLms EST CONF\n"); - break; - case RSL_MT_REL_CONF: - printf("RSLms REL CONF\n"); - break; - case RSL_MT_ERROR_IND: - printf("RSLms ERR IND\n"); - break; - default: - printf("unknown RSLms message type 0x%02x\n", rllh->c.msg_type); - rc = -EINVAL; - break; - } - return rc; -} - -/* input function that L2 calls when sending messages up to L3 */ -int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms) -{ - struct abis_rsl_common_hdr *rslh = msgb_l2(msg); - int rc = 0; - - switch (rslh->msg_discr & 0xfe) { - case ABIS_RSL_MDISC_RLL: - rc = rslms_rx_rll(msg, ms); - break; - default: - printf("unknown RSLms msg_discr 0x%02x\n", rslh->msg_discr); - rc = -EINVAL; - break; - } - - return rc; -} diff --git a/src/host/layer23/.gitignore b/src/host/layer23/.gitignore new file mode 100644 index 0000000..d359466 --- /dev/null +++ b/src/host/layer23/.gitignore @@ -0,0 +1,14 @@ +Makefile +Makefile.in +aclocal.m4 +configure +missing +*.o +depcomp +config.* +*.sw? +*.deps +layer2 +install-sh +autom4te.cache +*.a diff --git a/src/host/layer23/COPYING b/src/host/layer23/COPYING new file mode 100644 index 0000000..d511905 --- /dev/null +++ b/src/host/layer23/COPYING @@ -0,0 +1,339 @@ + 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 Lesser 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. + + + Copyright (C) + + 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. + + , 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/src/host/layer23/Makefile.am b/src/host/layer23/Makefile.am new file mode 100644 index 0000000..bc3910f --- /dev/null +++ b/src/host/layer23/Makefile.am @@ -0,0 +1,3 @@ +AUTOMAKE_OPTIONS = foreign dist-bzip2 1.6 + +SUBDIRS = include src diff --git a/src/host/layer23/configure.ac b/src/host/layer23/configure.ac new file mode 100644 index 0000000..177d968 --- /dev/null +++ b/src/host/layer23/configure.ac @@ -0,0 +1,27 @@ +dnl Process this file with autoconf to produce a configure script +AC_INIT + +AM_INIT_AUTOMAKE(layer23, 0.0.0) + +dnl kernel style compile messages +m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) + +dnl checks for programs +AC_PROG_MAKE_SET +AC_PROG_CC +AC_PROG_INSTALL +AC_PROG_RANLIB + +dnl checks for libraries +PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore) + +dnl checks for header files +AC_HEADER_STDC + +dnl Checks for typedefs, structures and compiler characteristics + +AC_OUTPUT( + src/Makefile + include/Makefile + include/osmocom/Makefile + Makefile) diff --git a/src/host/layer23/include/Makefile.am b/src/host/layer23/include/Makefile.am new file mode 100644 index 0000000..ca774b6 --- /dev/null +++ b/src/host/layer23/include/Makefile.am @@ -0,0 +1,2 @@ +noinst_HEADERS = l1a_l23_interface.h +SUBDIRS = osmocom diff --git a/src/host/layer23/include/l1a_l23_interface.h b/src/host/layer23/include/l1a_l23_interface.h new file mode 120000 index 0000000..2bbc967 --- /dev/null +++ b/src/host/layer23/include/l1a_l23_interface.h @@ -0,0 +1 @@ +../../../../include/l1a_l23_interface.h \ No newline at end of file diff --git a/src/host/layer23/include/osmocom/Makefile.am b/src/host/layer23/include/osmocom/Makefile.am new file mode 100644 index 0000000..6014195 --- /dev/null +++ b/src/host/layer23/include/osmocom/Makefile.am @@ -0,0 +1,3 @@ +# headers from OpenBSC +noinst_HEADERS = debug.h +noinst_HEADERS += l1ctl.h osmocom_data.h lapdm.h rslms.h layer3.h diff --git a/src/host/layer23/include/osmocom/debug.h b/src/host/layer23/include/osmocom/debug.h new file mode 120000 index 0000000..ae7c52c --- /dev/null +++ b/src/host/layer23/include/osmocom/debug.h @@ -0,0 +1 @@ +../../../libosmocom/include/osmocom/debug.h \ No newline at end of file diff --git a/src/host/layer23/include/osmocom/l1ctl.h b/src/host/layer23/include/osmocom/l1ctl.h new file mode 100644 index 0000000..0d5cba6 --- /dev/null +++ b/src/host/layer23/include/osmocom/l1ctl.h @@ -0,0 +1,25 @@ +#ifndef osmocom_l1ctl_h +#define osmocom_l1ctl_h + +#include +#include + +struct osmocom_ms; + +/* Receive incoming data from L1 using L1CTL format */ +int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg); + +/* Transmit L1CTL_DATA_REQ */ +int tx_ph_data_req(struct osmocom_ms *ms, struct msgb *msg, + uint8_t chan_nr, uint8_t link_id); + +/* Transmit L1CTL_RACH_REQ */ +int tx_ph_rach_req(struct osmocom_ms *ms); + +/* Transmit L1CTL_DM_EST_REQ */ +int tx_ph_dm_est_req(struct osmocom_ms *ms, uint16_t band_arfcn, uint8_t chan_nr); + +extern int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg); + + +#endif diff --git a/src/host/layer23/include/osmocom/lapdm.h b/src/host/layer23/include/osmocom/lapdm.h new file mode 100644 index 0000000..f3967a9 --- /dev/null +++ b/src/host/layer23/include/osmocom/lapdm.h @@ -0,0 +1,60 @@ +#ifndef _OSMOCOM_LAPDM_H +#define _OSMOCOM_LAPDM_H + +#include + +#include +#include + +#include + +enum lapdm_state { + LAPDm_STATE_IDLE, + LAPDm_STATE_SABM_SENT, + LAPDm_STATE_MF_EST, + LAPDm_STATE_TIMER_RECOV, + LAPDm_STATE_OWN_RCVR_BUSY, + LAPDm_STATE_NULL, +}; + +struct lapdm_entity; +struct osmocom_ms; + +/* TS 04.06 / Section 3.5.2 */ +struct lapdm_datalink { + uint8_t V_send; /* seq nr of next I frame to be transmitted */ + uint8_t V_ack; /* last frame ACKed by peer */ + uint8_t N_send; /* ? set to V_send at Tx time*/ + uint8_t V_recv; /* seq nr of next I frame expected to be received */ + uint8_t N_recv; /* expected send seq nr of the next received I frame */ + enum lapdm_state state; + struct timer_list t200; + uint8_t retrans_ctr; + + struct lapdm_entity *entity; +}; + +enum lapdm_dl_sapi { + DL_SAPI0 = 0, + DL_SAPI3 = 1, + _NR_DL_SAPI +}; + +struct lapdm_entity { + struct lapdm_datalink datalink[_NR_DL_SAPI]; + struct osmocom_ms *ms; +}; + +/* initialize a LAPDm entity */ +void lapdm_init(struct lapdm_entity *le, struct osmocom_ms *ms); + +/* input into layer2 (from layer 1) */ +int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1ctl_info_dl *l1i); + +/* input into layer2 (from layer 3) */ +int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms); + +/* sending messages up from L2 to L3 */ +int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms); + +#endif /* _OSMOCOM_LAPDM_H */ diff --git a/src/host/layer23/include/osmocom/layer3.h b/src/host/layer23/include/osmocom/layer3.h new file mode 100644 index 0000000..1b4af07 --- /dev/null +++ b/src/host/layer23/include/osmocom/layer3.h @@ -0,0 +1,10 @@ +#ifndef _OSMOCOM_L3_H +#define _OSMOCOM_L3_H + +#include +#include + +int gsm48_rx_ccch(struct msgb *msg, struct osmocom_ms *ms); +int gsm48_rx_dcch(struct msgb *msg, struct osmocom_ms *ms); + +#endif diff --git a/src/host/layer23/include/osmocom/osmocom_data.h b/src/host/layer23/include/osmocom/osmocom_data.h new file mode 100644 index 0000000..48c5579 --- /dev/null +++ b/src/host/layer23/include/osmocom/osmocom_data.h @@ -0,0 +1,19 @@ +#ifndef osmocom_data_h +#define osmocom_data_h + +#include +#include + +#include + +/* One Mobilestation for osmocom */ +struct osmocom_ms { + struct bsc_fd bfd; + enum gsm_band band; + int arfcn; + + struct lapdm_entity lapdm_dcch; + struct lapdm_entity lapdm_acch; +}; + +#endif diff --git a/src/host/layer23/include/osmocom/rslms.h b/src/host/layer23/include/osmocom/rslms.h new file mode 100644 index 0000000..7327c11 --- /dev/null +++ b/src/host/layer23/include/osmocom/rslms.h @@ -0,0 +1,23 @@ +#ifndef _OSMOCOM_RSLMS_H +#define _OSMOCOM_RSLMS_H + +#include +#include + +/* From L3 into RSLMS (direction -> L2) */ + +/* Send a 'simple' RLL request to L2 */ +int rslms_tx_rll_req(struct osmocom_ms *ms, uint8_t msg_type, + uint8_t chan_nr, uint8_t link_id); + +/* Send a RLL request (including L3 info) to L2 */ +int rslms_tx_rll_req_l3(struct osmocom_ms *ms, uint8_t msg_type, + uint8_t chan_nr, uint8_t link_id, struct msgb *msg); + + +/* From L2 into RSLMS (direction -> L3) */ + +/* input function that L2 calls when sending messages up to L3 */ +int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms); + +#endif /* _OSMOCOM_RSLMS_H */ diff --git a/src/host/layer23/src/Makefile.am b/src/host/layer23/src/Makefile.am new file mode 100644 index 0000000..aed8e24 --- /dev/null +++ b/src/host/layer23/src/Makefile.am @@ -0,0 +1,9 @@ +INCLUDES = $(all_includes) -I$(top_srcdir)/include +AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS) + +noinst_LIBRARIES = libosmocom.a +libosmocom_a_SOURCES = libosmocom/debug.c + +sbin_PROGRAMS = layer23 +layer23_SOURCES = main.c l1ctl.c gsmtap_util.c lapdm.c rslms.c layer3.c +layer23_LDADD = libosmocom.a $(LIBOSMOCORE_LIBS) diff --git a/src/host/layer23/src/gsmtap_util.c b/src/host/layer23/src/gsmtap_util.c new file mode 100644 index 0000000..0254c18 --- /dev/null +++ b/src/host/layer23/src/gsmtap_util.c @@ -0,0 +1,136 @@ +/* GSMTAP output for Osmocom Layer2 (will only work on the host PC) */ +/* + * (C) 2010 by Harald Welte + * + * 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 +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +static struct bsc_fd gsmtap_bfd; +static LLIST_HEAD(gsmtap_txqueue); + +/* receive a message from L1/L2 and put it in GSMTAP */ +int gsmtap_sendmsg(uint8_t ts, uint16_t arfcn, uint32_t fn, + const uint8_t *data, unsigned int len) +{ + struct msgb *msg; + struct gsmtap_hdr *gh; + uint8_t *dst; + + msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx"); + if (!msg) + return -ENOMEM; + + gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh)); + + gh->version = GSMTAP_VERSION; + gh->hdr_len = sizeof(*gh)/4; + gh->type = GSMTAP_TYPE_UM; + gh->timeslot = ts; + gh->arfcn = htons(arfcn); + gh->noise_db = 0; + gh->signal_db = 0; + gh->frame_number = htonl(fn); + gh->burst_type = GSMTAP_BURST_NORMAL; + gh->antenna_nr = 0; + + dst = msgb_put(msg, len); + memcpy(dst, data, len); + + msgb_enqueue(&gsmtap_txqueue, msg); + gsmtap_bfd.when |= BSC_FD_WRITE; + + return 0; +} + +/* Callback from select layer if we can write to the socket */ +static int gsmtap_fd_cb(struct bsc_fd *fd, unsigned int flags) +{ + struct msgb *msg; + int rc; + + if (!(flags & BSC_FD_WRITE)) + return 0; + + msg = msgb_dequeue(&gsmtap_txqueue); + if (!msg) { + /* no more messages in the queue, disable READ cb */ + gsmtap_bfd.when = 0; + return 0; + } + rc = write(gsmtap_bfd.fd, msg->data, msg->len); + if (rc < 0) { + perror("writing msgb to gsmtap fd"); + msgb_free(msg); + return rc; + } + if (rc != msg->len) { + perror("short write to gsmtap fd"); + msgb_free(msg); + return -EIO; + } + + msgb_free(msg); + return 0; +} + +int gsmtap_init(void) +{ + int rc; + struct sockaddr_in sin; + + sin.sin_family = AF_INET; + sin.sin_port = htons(GSMTAP_UDP_PORT); + inet_aton("127.0.0.1", &sin.sin_addr); + + /* FIXME: create socket */ + rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (rc < 0) { + perror("creating UDP socket"); + return rc; + } + gsmtap_bfd.fd = rc; + rc = connect(rc, (struct sockaddr *)&sin, sizeof(sin)); + if (rc < 0) { + perror("connecting UDP socket"); + close(gsmtap_bfd.fd); + gsmtap_bfd.fd = 0; + return rc; + } + + gsmtap_bfd.when = BSC_FD_WRITE; + gsmtap_bfd.cb = gsmtap_fd_cb; + gsmtap_bfd.data = NULL; + + return bsc_register_fd(&gsmtap_bfd); +} diff --git a/src/host/layer23/src/gsmtap_util.h b/src/host/layer23/src/gsmtap_util.h new file mode 100644 index 0000000..3599c33 --- /dev/null +++ b/src/host/layer23/src/gsmtap_util.h @@ -0,0 +1,13 @@ +#ifndef GSMTAP_ROUTINES_H +#define GSMTAP_ROUTINES_H + +#include +#include + +int gsmtap_init(void); + +/* receive a message from L1/L2 and put it in GSMTAP */ +int gsmtap_sendmsg(uint8_t ts, uint16_t arfcn, uint32_t fn, + const uint8_t *data, unsigned int len); + +#endif diff --git a/src/host/layer23/src/l1ctl.c b/src/host/layer23/src/l1ctl.c new file mode 100644 index 0000000..b8c636a --- /dev/null +++ b/src/host/layer23/src/l1ctl.c @@ -0,0 +1,273 @@ +/* Layer1 control code, talking L1CTL protocol with L1 on the phone */ + +/* (C) 2010 by Holger Hans Peter Freyther + * (C) 2010 by Harald Welte + * + * 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 +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "gsmtap_util.h" + +static struct msgb *osmo_l1_alloc(uint8_t msg_type) +{ + struct l1ctl_info_ul *ul; + struct msgb *msg = msgb_alloc_headroom(256, 4, "osmo_l1"); + + if (!msg) { + fprintf(stderr, "Failed to allocate memory.\n"); + return NULL; + } + + msg->l1h = msgb_put(msg, sizeof(*ul)); + ul = (struct l1ctl_info_ul *) msg->l1h; + ul->msg_type = msg_type; + + return msg; +} + + +static int osmo_make_band_arfcn(struct osmocom_ms *ms) +{ + /* TODO: Include the band */ + return ms->arfcn; +} + +static int rx_l1_ccch_resp(struct osmocom_ms *ms, struct msgb *msg) +{ + struct l1ctl_info_dl *dl; + struct l1ctl_sync_new_ccch_resp *sb; + + if (msgb_l3len(msg) < sizeof(*sb)) { + fprintf(stderr, "MSG too short for CCCH RESP: %u\n", msgb_l3len(msg)); + return -1; + } + + dl = (struct l1ctl_info_dl *) msg->l1h; + sb = (struct l1ctl_sync_new_ccch_resp *) msg->l2h; + + printf("SCH: SNR: %u TDMA: (%.4u/%.2u/%.2u) bsic: %d\n", + dl->snr[0], dl->time.t1, dl->time.t2, dl->time.t3, sb->bsic); + + return 0; +} + +char *chan_nr2string(uint8_t chan_nr) +{ + static char str[20]; + uint8_t cbits = chan_nr >> 3; + + str[0] = '\0'; + + if (cbits == 0x01) + sprintf(str, "TCH/F"); + else if ((cbits & 0x1e) == 0x02) + sprintf(str, "TCH/H(%u)", cbits & 0x01); + else if ((cbits & 0x1c) == 0x04) + sprintf(str, "SDCCH/4(%u)", cbits & 0x03); + else if ((cbits & 0x18) == 0x08) + sprintf(str, "SDCCH/8(%u)", cbits & 0x07); + else if (cbits == 0x10) + sprintf(str, "BCCH"); + else if (cbits == 0x11) + sprintf(str, "RACH"); + else if (cbits == 0x12) + sprintf(str, "PCH/AGCH"); + else + sprintf(str, "UNKNOWN"); + + return str; +} + +/* Receive L1CTL_DATA_IND (Data Indication from L1) */ +static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg) +{ + struct l1ctl_info_dl *dl, dl_cpy; + struct l1ctl_data_ind *ccch; + struct lapdm_entity *le; + + if (msgb_l3len(msg) < sizeof(*ccch)) { + fprintf(stderr, "MSG too short Data Ind: %u\n", msgb_l3len(msg)); + return -1; + } + + dl = (struct l1ctl_info_dl *) msg->l1h; + ccch = (struct l1ctl_data_ind *) msg->l2h; + printf("%s (%.4u/%.2u/%.2u) %s\n", + chan_nr2string(dl->chan_nr), dl->time.t1, dl->time.t2, + dl->time.t3, hexdump(ccch->data, sizeof(ccch->data))); + + /* send CCCH data via GSMTAP */ + gsmtap_sendmsg(dl->chan_nr & 0x07, dl->band_arfcn, dl->time.fn, ccch->data, + sizeof(ccch->data)); + + /* determine LAPDm entity based on SACCH or not */ + if (dl->link_id & 0x40) + le = &ms->lapdm_acch; + else + le = &ms->lapdm_dcch; + /* make local stack copy of l1ctl_info_dl, as LAPDm will overwrite skb hdr */ + memcpy(&dl_cpy, dl, sizeof(dl_cpy)); + + /* pull the L1 header from the msgb */ + msgb_pull(msg, msg->l2h - msg->l1h); + msg->l1h = NULL; + + /* send it up into LAPDm */ + l2_ph_data_ind(msg, le, &dl_cpy); + + return 0; +} + +/* Transmit L1CTL_DATA_REQ */ +int tx_ph_data_req(struct osmocom_ms *ms, struct msgb *msg, + uint8_t chan_nr, uint8_t link_id) +{ + struct l1ctl_info_ul *l1i_ul; + + printf("tx_ph_data_req(%s)\n", hexdump(msg->l2h, msgb_l2len(msg))); + + if (msgb_l2len(msg) > 23) { + printf("L1 cannot handle message length > 23 (%u)\n", msgb_l2len(msg)); + msgb_free(msg); + return -EINVAL; + } else if (msgb_l2len(msg) < 23) + printf("L1 message length < 23 (%u) doesn't seem right!\n", msgb_l2len(msg)); + + /* prepend uplink info header */ + printf("sizeof(struct l1ctl_info_ul)=%lu\n", sizeof(*l1i_ul)); + msg->l1h = msgb_push(msg, sizeof(*l1i_ul)); + l1i_ul = (struct l1ctl_info_ul *) msg->l1h; + + l1i_ul->msg_type = L1CTL_DATA_REQ; + + l1i_ul->chan_nr = chan_nr; + l1i_ul->link_id = link_id; + + /* FIXME: where to get this from? */ + l1i_ul->tx_power = 0; + + return osmo_send_l1(ms, msg); +} + +/* Receive L1CTL_RESET */ +static int rx_l1_reset(struct osmocom_ms *ms) +{ + struct msgb *msg; + struct l1ctl_sync_new_ccch_req *req; + + msg = osmo_l1_alloc(L1CTL_NEW_CCCH_REQ); + if (!msg) + return -1; + + printf("Layer1 Reset.\n"); + req = (struct l1ctl_sync_new_ccch_req *) msgb_put(msg, sizeof(*req)); + req->band_arfcn = osmo_make_band_arfcn(ms); + + return osmo_send_l1(ms, msg); +} + +/* Transmit L1CTL_RACH_REQ */ +int tx_ph_rach_req(struct osmocom_ms *ms) +{ + struct msgb *msg; + struct l1ctl_rach_req *req; + static uint8_t i = 0; + + msg = osmo_l1_alloc(L1CTL_RACH_REQ); + if (!msg) + return -1; + + printf("RACH Req.\n"); + req = (struct l1ctl_rach_req *) msgb_put(msg, sizeof(*req)); + req->ra = i++; + + return osmo_send_l1(ms, msg); +} + +/* Transmit L1CTL_DM_EST_REQ */ +int tx_ph_dm_est_req(struct osmocom_ms *ms, uint16_t band_arfcn, uint8_t chan_nr) +{ + struct msgb *msg; + struct l1ctl_info_ul *ul; + struct l1ctl_dm_est_req *req; + + msg = osmo_l1_alloc(L1CTL_DM_EST_REQ); + if (!msg) + return -1; + + printf("Tx Dedic.Mode Est Req (arfcn=%u, chan_nr=0x%02x)\n", + band_arfcn, chan_nr); + ul = (struct l1ctl_info_ul *) msg->l1h; + ul->chan_nr = chan_nr; + ul->link_id = 0; + ul->tx_power = 0; /* FIXME: initial TX power */ + req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req)); + req->band_arfcn = band_arfcn; + + return osmo_send_l1(ms, msg); + +} + +/* Receive incoming data from L1 using L1CTL format */ +int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg) +{ + int rc = 0; + struct l1ctl_info_dl *dl; + + if (msgb_l2len(msg) < sizeof(*dl)) { + fprintf(stderr, "Short Layer2 message: %u\n", msgb_l2len(msg)); + return -1; + } + + dl = (struct l1ctl_info_dl *) msg->l1h; + msg->l2h = &msg->l1h[0] + sizeof(*dl); + + switch (dl->msg_type) { + case L1CTL_NEW_CCCH_RESP: + rc = rx_l1_ccch_resp(ms, msg); + break; + case L1CTL_DATA_IND: + rc = rx_ph_data_ind(ms, msg); + break; + case L1CTL_RESET: + rc = rx_l1_reset(ms); + break; + default: + fprintf(stderr, "Unknown MSG: %u\n", dl->msg_type); + break; + } + + return rc; +} diff --git a/src/host/layer23/src/lapdm.c b/src/host/layer23/src/lapdm.c new file mode 100644 index 0000000..bdf2c2a --- /dev/null +++ b/src/host/layer23/src/lapdm.c @@ -0,0 +1,836 @@ +/* GSM LAPDm (TS 04.06) implementation */ + +/* (C) 2010 by Harald Welte + * + * 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 +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +/* TS 04.06 Figure 4 / Section 3.2 */ +#define LAPDm_LPD_NORMAL 0 +#define LAPDm_LPD_SMSCB 1 +#define LAPDm_SAPI_NORMAL 0 +#define LAPDm_SAPI_SMS 3 +#define LAPDm_ADDR(lpd, sapi, cr) (((lpd & 0x3) << 5) | ((sapi & 0x7) << 2) | ((cr & 0x1) << 1) | 0x1) + +#define LAPDm_ADDR_SAPI(addr) ((addr >> 2) & 0x7) + +/* TS 04.06 Table 3 / Section 3.4.3 */ +#define LAPDm_CTRL_I(nr, ns, p) (((nr & 0x7) << 5) | ((p & 0x1) << 4) | ((ns & 0x7) << 1)) +#define LAPDm_CTRL_S(nr, s, p) (((nr & 0x7) << 5) | ((p & 0x1) << 4) | ((s & 0x3) << 2) | 0x1) +#define LAPDm_CTRL_U(u, p) (((u & 0x1c) << (5-2)) | ((p & 0x1) << 4) | ((u & 0x3) << 2) | 0x3) + +#define LAPDm_CTRL_is_I(ctrl) ((ctrl & 0x1) == 0) +#define LAPDm_CTRL_is_S(ctrl) ((ctrl & 0x3) == 1) +#define LAPDm_CTRL_is_U(ctrl) ((ctrl & 0x3) == 3) + +#define LAPDm_CTRL_U_BITS(ctrl) (((ctrl & 0xC) >> 2) | (ctrl & 0xE0) >> 3) +#define LAPDm_CTRL_PF_BIT(ctrl) ((ctrl >> 4) & 0x1) + +#define LAPDm_CTRL_S_BITS(ctrl) ((ctrl & 0xC) >> 2) + +#define LAPDm_CTRL_I_Ns(ctrl) ((ctrl & 0xE) >> 1) +#define LAPDm_CTRL_I_Nr(ctrl) ((ctrl & 0xE0) >> 5) + +/* TS 04.06 Table 4 / Section 3.8.1 */ +#define LAPDm_U_SABM 0x7 +#define LAPDm_U_DM 0x3 +#define LAPDm_U_UI 0x0 +#define LAPDm_U_DISC 0x8 +#define LAPDm_U_UA 0xC + +#define LAPDm_S_RR 0x0 +#define LAPDm_S_RNR 0x1 +#define LAPDm_S_REJ 0x2 + +#define LAPDm_LEN(len) ((len << 2) | 0x1) + +/* TS 04.06 Section 5.8.3 */ +#define N201_AB_SACCH 18 +#define N201_AB_SDCCH 20 +#define N201_AB_FACCH 20 +#define N201_Bbis 23 +#define N201_Bter_SACCH 21 +#define N201_Bter_SDCCH 23 +#define N201_Bter_FACCH 23 +#define N201_B4 19 + +/* 5.8.2.1 N200 during establish and release */ +#define N200_EST_REL 5 +/* 5.8.2.1 N200 during timer recovery state */ +#define N200_TR_SACCH 5 +#define N200_TR_SDCCH 23 +#define N200_TR_FACCH_FR 34 +#define N200_TR_EFACCH_FR 48 +#define N200_TR_FACCH_HR 29 +/* FIXME: this depends on chan type */ +#define N200 N200_TR_SACCH + +#define CR_MS2BS_CMD 0 +#define CR_MS2BS_RESP 1 +#define CR_BS2MS_CMD 1 +#define CR_BS2MS_RESP 0 + +/* Set T200 to 1 Second (OpenBTS uses 900ms) */ +#define T200 1, 0 + +enum lapdm_format { + LAPDm_FMT_A, + LAPDm_FMT_B, + LAPDm_FMT_Bbis, + LAPDm_FMT_Bter, + LAPDm_FMT_B4, +}; + +struct lapdm_msg_ctx { + struct lapdm_datalink *dl; + enum lapdm_format lapdm_fmt; + uint8_t chan_nr; + uint8_t link_id; + uint8_t addr; + uint8_t ctrl; +}; + +static void lapdm_t200_cb(void *data); + +/* UTILITY FUNCTIONS */ + +static inline uint8_t inc_mod8(uint8_t x) +{ + return (x + 1) % 8; +} + +static void lapdm_dl_init(struct lapdm_datalink *dl, + struct lapdm_entity *entity) +{ + memset(dl, 0, sizeof(*dl)); + dl->t200.data = dl; + dl->t200.cb = &lapdm_t200_cb; + dl->entity = entity; +} + +void lapdm_init(struct lapdm_entity *le, struct osmocom_ms *ms) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(le->datalink); i++) + lapdm_dl_init(&le->datalink[i], le); + + le->ms = ms; +} + +static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi) +{ + switch (sapi) { + case LAPDm_SAPI_NORMAL: + return &le->datalink[0]; + case LAPDm_SAPI_SMS: + return &le->datalink[1]; + default: + return NULL; + } +} + +/* remove the L2 header from a MSGB */ +static inline unsigned char *msgb_pull_l2h(struct msgb *msg) +{ + unsigned char *ret = msgb_pull(msg, msg->l3h - msg->l2h); + msg->l2h = NULL; + return ret; +} + +/* Append padding (if required) */ +static void lapdm_pad_msgb(struct msgb *msg) +{ + int pad_len = 23 - msgb_l2len(msg); + uint8_t *data; + + if (pad_len < 0) { + printf("cannot pad message that is already too big!\n"); + return; + } + + data = msgb_put(msg, pad_len); + memset(data, 0x2B, pad_len); +} + +static int tx_ph_data_req_pad(struct osmocom_ms *ms, struct msgb *msg, + uint8_t chan_nr, uint8_t link_id) +{ + lapdm_pad_msgb(msg); + return tx_ph_data_req(ms, msg, chan_nr, link_id); +} + +/* Take a Bbis format message from L1 and create RSLms UNIT DATA IND */ +static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx, + struct msgb *msg) +{ + /* Add the RSL + RLL header */ + rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id); + + /* send off the RSLms message to L3 */ + return rslms_sendmsg(msg, mctx->dl->entity->ms); +} + +static int send_rslms_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx) +{ + struct msgb *msg; + + msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id); + + /* send off the RSLms message to L3 */ + return rslms_sendmsg(msg, mctx->dl->entity->ms); +} + +static int check_length_ind(uint8_t length_ind) +{ + if (!(length_ind & 0x01)) { + /* G.4.1 If the EL bit is set to "0", an MDL-ERROR-INDICATION + * primitive with cause "frame not implemented" is sent to the + * mobile management entity. */ + printf("we don't support multi-octet length\n"); + return -EINVAL; + } + if (length_ind & 0x02) { + printf("we don't support LAPDm fragmentation yet\n"); + return -EINVAL; + } + return 0; +} + +/* Timer callback on T200 expiry */ +static void lapdm_t200_cb(void *data) +{ + struct lapdm_datalink *dl = data; + + printf("lapdm_t200_cb(%p) state=%u\n", dl, dl->state); + + switch (dl->state) { + case LAPDm_STATE_SABM_SENT: + /* 5.4.1.3 */ + if (dl->retrans_ctr >= N200_EST_REL + 1) { + /* FIXME: send RELEASE INDICATION to L3 */ + dl->retrans_ctr = 0; + dl->state = LAPDm_STATE_IDLE; + } + /* FIXME: retransmit SABM command */ + //rslms_rx_rll_est_req(msg, dl); + /* increment re-transmission counter */ + dl->retrans_ctr++; + /* restart T200 (PH-READY-TO-SEND) */ + bsc_schedule_timer(&dl->t200, T200); + break; + case LAPDm_STATE_MF_EST: + /* 5.5.7 */ + dl->retrans_ctr = 0; + dl->state = LAPDm_STATE_TIMER_RECOV; + case LAPDm_STATE_TIMER_RECOV: + dl->retrans_ctr++; + if (dl->retrans_ctr < N200) { + /* FIXME: retransmit I frame (V_s-1) with P=1 */ + /* FIXME: send appropriate supervision frame with P=1 */ + /* restart T200 (PH-READY-TO-SEND) */ + bsc_schedule_timer(&dl->t200, T200); + } else { + /* FIXME: send ERROR INDICATION to L3 */ + } + break; + default: + printf("T200 expired in unexpected dl->state %u\n", dl->state); + } +} + +static int lapdm_send_rr(struct lapdm_msg_ctx *mctx, uint8_t f_bit) +{ + uint8_t sapi = mctx->link_id & 7; + struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RR"); + msg->l2h = msgb_put(msg, 3); + + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP); + msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RR, f_bit); + msg->l2h[2] = LAPDm_LEN(0); + + return tx_ph_data_req_pad(mctx->dl->entity->ms, msg, mctx->chan_nr, mctx->link_id); +} + +/* L1 -> L2 */ + +/* Receive a LAPDm S (Unnumbered) message from L1 */ +static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx) +{ + struct lapdm_datalink *dl = mctx->dl; + uint8_t length; + int rc; + + switch (LAPDm_CTRL_U_BITS(mctx->ctrl)) { + case LAPDm_U_SABM: + printf("SABM "); + /* Must be Format B */ + rc = check_length_ind(msg->l2h[2]); + if (rc < 0) + return rc; + length = msg->l2h[2] >> 2; + /* FIXME: G.4.5 check */ + if (dl->state == LAPDm_STATE_MF_EST) { + if (length == 0) { + /* FIXME: re-establishment procedure 5.6 */ + } else { + /* FIXME: check for contention resoultion */ + printf("SABM command, multiple frame established state\n"); + return 0; + } + } + if (length == 0) { + /* 5.4.1.2 Normal establishment procedures */ + rc = send_rslms_rll_simple(RSL_MT_EST_IND, mctx); + } else { + /* 5.4.1.4 Contention resolution establishment */ + msg->l3h = msg->l2h + 3; + msgb_pull_l2h(msg); + rc = send_rslms_rll_l3(RSL_MT_EST_IND, mctx, msg); + } +#if 0 + if (rc == 0) + dl->state = LAPDm_STATE_SABM_SENT; +#endif + break; + case LAPDm_U_DM: + printf("DM "); + if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) { + /* 5.4.1.2 DM responses with the F bit set to "0" shall be ignored. */ + return 0; + } + switch (dl->state) { + case LAPDm_STATE_IDLE: + /* 5.4.5 all other frame types shall be discarded */ + printf("state=IDLE (discarding) "); + return 0; + case LAPDm_STATE_MF_EST: + if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 1) + printf("unsolicited DM resposne "); + else + printf("unsolicited DM resposne, multiple frame established state "); + return 0; + case LAPDm_STATE_TIMER_RECOV: + /* DM is normal in case PF = 1 */ + if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 0) { + printf("unsolicited DM resposne, multiple frame established state "); + return 0; + } + break; + } + /* reset T200 */ + bsc_del_timer(&dl->t200); + rc = send_rslms_rll_simple(RSL_MT_REL_IND, mctx); + break; + case LAPDm_U_UI: + printf("UI "); + if (mctx->lapdm_fmt == LAPDm_FMT_B4) { + length = N201_B4; + msg->l3h = msg->l2h + 2; + } else { + rc = check_length_ind(msg->l2h[2]); + if (rc < 0) + return rc; + length = msg->l2h[2] >> 2; + msg->l3h = msg->l2h + 3; + } + /* do some length checks */ + if (length == 0) { + /* 5.3.3 UI frames received with the length indicator set to "0" shall be ignored */ + printf("length=0 (discarding) "); + return 0; + } + /* FIXME: G.4.5 check */ + switch (LAPDm_ADDR_SAPI(mctx->ctrl)) { + case LAPDm_SAPI_NORMAL: + case LAPDm_SAPI_SMS: + break; + default: + /* 5.3.3 UI frames with invalid SAPI values shall be discarded */ + printf("sapi=%u (discarding) ", LAPDm_ADDR_SAPI(mctx->ctrl)); + return 0; + } + msgb_pull_l2h(msg); + rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, mctx, msg); + break; + case LAPDm_U_DISC: + printf("DISC "); + length = msg->l2h[2] >> 2; + if (length > 0 || msg->l2h[2] & 0x02) { + /* G.4.4 If a DISC or DM frame is received with L>0 or + * with the M bit set to "1", an MDL-ERROR-INDICATION + * primitive with cause "U frame with incorrect + * parameters" is sent to the mobile management entity. */ + printf("U frame iwth incorrect parameters "); + return -EIO; + } + switch (dl->state) { + case LAPDm_STATE_IDLE: + /* FIXME: send DM with F=P */ + break; + default: + /* FIXME */ + break; + } + break; + case LAPDm_U_UA: + printf("UA "); + /* FIXME: G.4.5 check */ + if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) { + /* 5.4.1.2 A UA response with the F bit set to "0" shall be ignored. */ + printf("F=0 (discarding) "); + return 0; + } + switch (dl->state) { + case LAPDm_STATE_SABM_SENT: + break; + case LAPDm_STATE_IDLE: + /* 5.4.5 all other frame types shall be discarded */ + default: + printf("unsolicited UA response! (discarding) "); + return 0; + } + /* reset Timer T200 */ + bsc_del_timer(&dl->t200); + /* set Vs, Vr and Va to 0 */ + dl->V_send = dl->V_recv = dl->V_ack = 0; + /* enter multiple-frame-established state */ + dl->state = LAPDm_STATE_MF_EST; + /* send notification to L3 */ + rc = send_rslms_rll_simple(RSL_MT_EST_CONF, mctx); + break; + } + return rc; +} + +/* Receive a LAPDm S (Supervisory) message from L1 */ +static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx) +{ + struct lapdm_datalink *dl = mctx->dl; + uint8_t length; + + length = msg->l2h[2] >> 2; + if (length > 0 || msg->l2h[2] & 0x02) { + /* G.4.3 If a supervisory frame is received with L>0 or + * with the M bit set to "1", an MDL-ERROR-INDICATION + * primitive with cause "S frame with incorrect + * parameters" is sent to the mobile management entity. */ + return -EIO; + } + switch (dl->state) { + case LAPDm_STATE_IDLE: + /* FIXME: if P=1, respond DM with F=1 (5.2.2) */ + /* 5.4.5 all other frame types shall be discarded */ + break; + } + switch (LAPDm_CTRL_S_BITS(mctx->ctrl)) { + case LAPDm_S_RR: + /* FIXME */ + break; + case LAPDm_S_RNR: + /* FIXME */ + break; + case LAPDm_S_REJ: + /* FIXME */ + break; + } + return 0; +} + +/* Receive a LAPDm I (Information) message from L1 */ +static int lapdm_rx_i(struct msgb *msg, struct lapdm_msg_ctx *mctx) +{ + struct lapdm_datalink *dl = mctx->dl; + uint8_t nr = LAPDm_CTRL_I_Nr(mctx->ctrl); + uint8_t ns = LAPDm_CTRL_I_Ns(mctx->ctrl); + uint8_t length; + int rc; + + length = msg->l2h[2] >> 2; + /* FIXME: check for length > N201 */ + if (length == 0) { + /* G.4.2 If the length indicator of an I frame is set + * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION + * primitive with cause "I frame with incorrect length" + * is sent to the mobile management entity. */ + return -EIO; + } + /* FIXME: G.4.2 If the numerical value of L is Lstate) { + case LAPDm_STATE_IDLE: + /* FIXME: if P=1, respond DM with F=1 (5.2.2) */ + /* 5.4.5 all other frame types shall be discarded */ + break; + } + + /* processing of Nr, Ns and P fields */ + if (ns == dl->V_recv) { + /* FIXME: check for M bit! */ + dl->V_recv = inc_mod8(dl->V_recv); + + /* send a DATA INDICATION to L3 */ + msg->l3h = msg->l2h + 2; + msgb_pull_l2h(msg); + rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, msg); + } else { + printf("N(s) sequence error: Ns=%u, V_recv=%u ", ns, dl->V_recv); + /* FIXME: 5.7.1: N(s) sequence error */ + /* discard data */ + return -EIO; + } + + /* Check for P bit */ + if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) { + /* 5.5.2.1 */ + /* FIXME: check ifwe are in own receiver busy */ + /* FIXME: Send RR with F=1 */ + rc = lapdm_send_rr(mctx, 1); + } else { + /* 5.5.2.2 */ + /* FIXME: check ifwe are in own receiver busy */ + //if (we_have_I_frame_pending) { + if (0) { + /* FIXME: send that I frame with Nr=Vr */ + } else { + /* Send RR with F=0 */ + rc = lapdm_send_rr(mctx, 0); + } + } + + if (dl->state != LAPDm_STATE_TIMER_RECOV) { + /* When not in the timer recovery condition, the data + * link layer entity shall reset the timer T200 on + * receipt of a valid I frame with N(R) higher than V(A) */ + if (nr > dl->V_ack) { + /* FIXME: 5.5.3.1 Note 1 + 2 */ + bsc_del_timer(&dl->t200); + /* FIXME: if there are outstanding I frames + * still unacknowledged, the data link layer + * entity shall set timer T200 */ + } + + /* FIXME: 5.7.4: N(R) sequence error */ + /* N(R) is called valid, if and only if (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8. */ + } + + /* V(A) shall be set to the value of N(R) */ + dl->V_ack = LAPDm_CTRL_I_Nr(mctx->ctrl); + + return rc; +} + +/* Receive a LAPDm message from L1 */ +static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx) +{ + int rc; + + if (LAPDm_CTRL_is_U(mctx->ctrl)) + rc = lapdm_rx_u(msg, mctx); + else if (LAPDm_CTRL_is_S(mctx->ctrl)) + rc = lapdm_rx_s(msg, mctx); + else if (LAPDm_CTRL_is_I(mctx->ctrl)) + rc = lapdm_rx_i(msg, mctx); + else { + printf("unknown LAPDm format "); + rc = -EINVAL; + } + return rc; +} + +/* input into layer2 (from layer 1) */ +int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1ctl_info_dl *l1i) +{ + uint8_t cbits = l1i->chan_nr >> 3; + uint8_t sapi = l1i->link_id & 7; + struct lapdm_msg_ctx mctx; + int rc; + + printf("l2_ph_data_ind() "); + /* when we reach here, we have a msgb with l2h pointing to the raw + * 23byte mac block. The l1h has already been purged. */ + + mctx.dl = datalink_for_sapi(le, sapi); + mctx.chan_nr = l1i->chan_nr; + mctx.link_id = l1i->link_id; + mctx.addr = mctx.ctrl = 0; + + /* check for L1 chan_nr/link_id and determine LAPDm hdr format */ + if (cbits == 0x10 || cbits == 0x12) { + /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */ + mctx.lapdm_fmt = LAPDm_FMT_Bbis; + printf("fmt=Bbis "); + } else { + if (mctx.link_id & 0x40) { + /* It was received from network on SACCH, thus + * lapdm_fmt must be B4 */ + mctx.lapdm_fmt = LAPDm_FMT_B4; + printf("fmt=B4 "); + /* SACCH frames have a two-byte L1 header that OsmocomBB L1 doesn't + * strip */ + msg->l2h += 2; + } else { + mctx.lapdm_fmt = LAPDm_FMT_B; + printf("fmt=B "); + } + } + + switch (mctx.lapdm_fmt) { + case LAPDm_FMT_A: + case LAPDm_FMT_B: + case LAPDm_FMT_B4: + mctx.addr = msg->l2h[0]; + if (!(mctx.addr & 0x01)) { + printf("we don't support multibyte addresses (discarding)\n"); + return -EINVAL; + } + mctx.ctrl = msg->l2h[1]; + /* obtain SAPI from address field */ + mctx.link_id |= LAPDm_ADDR_SAPI(mctx.addr); + rc = lapdm_ph_data_ind(msg, &mctx); + break; + case LAPDm_FMT_Bter: + /* FIXME */ + break; + case LAPDm_FMT_Bbis: + /* directly pass up to layer3 */ + printf("UI "); + msg->l3h = msg->l2h; + msgb_pull_l2h(msg); + rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg); + break; + } + printf("\n"); + + return rc; +} + +/* L3 -> L2 / RSLMS -> LAPDm */ + +/* L3 requests establishment of data link */ +static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl) +{ + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + uint8_t chan_nr = rllh->chan_nr; + uint8_t link_id = rllh->link_id; + uint8_t sapi = rllh->link_id & 7; + struct tlv_parsed tv; + uint8_t len; + + printf("RSL_MT_EST_REQ "); + + rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); + if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { + /* contention resolution establishment procedure */ + if (dl->state != LAPDm_STATE_IDLE) { + /* 5.4.1.4: The data link layer shall, however, ignore any such + * service request if it is not in the idle state when the + * request is received. */ + printf("DL state != IDLE (discarding)\n"); + msgb_free(msg); + return 0; + } + if (sapi != 0) { + /* According to clause 6, the contention resolution + * procedure is only permitted with SAPI value 0 */ + printf("SAPI != 0 but contention resolution (discarding)\n"); + msgb_free(msg); + return -EINVAL; + } + /* transmit a SABM command with the P bit set to "1". The SABM + * command shall contain the layer 3 message unit */ + len = LAPDm_LEN(TLVP_LEN(&tv, RSL_IE_L3_INFO)); + + /* FIXME: store information field in dl entity */ + } else { + /* normal establishment procedure */ + len = LAPDm_LEN(0); + } + + /* Remove RLL header from msgb */ + msgb_pull_l2h(msg); + + /* Push LAPDm header on msgb */ + msg->l2h = msgb_push(msg, 3); + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD); + msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1); + msg->l2h[2] = len; + + /* Tramsmit and start T200 */ + dl->state = LAPDm_STATE_SABM_SENT; + bsc_schedule_timer(&dl->t200, T200); + return tx_ph_data_req_pad(dl->entity->ms, msg, chan_nr, link_id); +} + +/* L3 requests transfer of unnumbered information */ +static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl) +{ + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + uint8_t chan_nr = rllh->chan_nr; + uint8_t link_id = rllh->link_id; + uint8_t sapi = link_id & 7; + struct tlv_parsed tv; + + rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); + + /* Remove RLL header from msgb */ + msgb_pull_l2h(msg); + + /* Push LAPDm header on msgb */ + msg->l2h = msgb_push(msg, 3); + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD); + msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1); + msg->l2h[2] = LAPDm_LEN(TLVP_LEN(&tv, RSL_IE_L3_INFO)); + + /* Tramsmit and start T200 */ + bsc_schedule_timer(&dl->t200, T200); + return tx_ph_data_req_pad(dl->entity->ms, msg, chan_nr, link_id); +} + +/* L3 requests transfer of acknowledged information */ +static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl) +{ + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + uint8_t chan_nr = rllh->chan_nr; + uint8_t link_id = rllh->link_id; + uint8_t sapi = rllh->link_id & 7; + struct tlv_parsed tv; + + switch (dl->state) { + case LAPDm_STATE_MF_EST: + break; + default: + printf("refusing RLL DATA REQ during DL state %u\n", dl->state); + return -EIO; + break; + } + + /* FIXME: check if the layer3 message length exceeds N201 */ + + rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); + + /* Remove the RSL/RLL header */ + msgb_pull_l2h(msg); + + /* Push the LAPDm header */ + msg->l2h = msgb_put(msg, 3); + msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD); + msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0); + msg->l2h[2] = LAPDm_LEN(TLVP_LEN(&tv, RSL_IE_L3_INFO)); + + /* The value of the send state variable V(S) shall be incremented by 1 + * at the end of the transmission of the I frame */ + dl->V_send = inc_mod8(dl->V_send); + + /* If timer T200 is not running at the time right before transmitting a + * frame, when the PH-READY-TO-SEND primitive is received from the + * physical layer., it shall be set. */ + if (!bsc_timer_pending(&dl->t200)) + bsc_schedule_timer(&dl->t200, T200); + + /* FIXME: If the send state variable V(S) is equal to V(A) plus k + * (where k is the maximum number of outstanding I frames - see + * subclause 5.8.4), the data link layer entity shall not transmit any + * new I frames, but shall retransmit an I frame as a result + * of the error recovery procedures as described in subclauses 5.5.4 and + * 5.5.7. */ + + return tx_ph_data_req_pad(dl->entity->ms, msg, chan_nr, link_id); +} + +/* incoming RSLms RLL message from L3 */ +static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms) +{ + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + int rc = 0; + uint8_t sapi = rllh->link_id & 7; + struct lapdm_entity *le; + struct lapdm_datalink *dl; + + if (rllh->link_id & 0x40) + le = &ms->lapdm_acch; + else + le = &ms->lapdm_dcch; + dl = datalink_for_sapi(le, sapi); + + switch (rllh->c.msg_type) { + case RSL_MT_UNIT_DATA_REQ: + /* create and send UI command */ + rc = rslms_rx_rll_udata_req(msg, dl); + break; + case RSL_MT_EST_REQ: + /* create and send SABM command */ + rc = rslms_rx_rll_est_req(msg, dl); + break; + case RSL_MT_DATA_REQ: + /* create and send I command */ + rc = rslms_rx_rll_data_req(msg, dl); + break; + case RSL_MT_REL_REQ: + /* FIXME: create and send DISC command */ + default: + printf("unknown RLL message type 0x%02x\n", + rllh->c.msg_type); + break; + } + + return rc; +} + +/* input into layer2 (from layer 3) */ +int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms) +{ + struct abis_rsl_common_hdr *rslh = msgb_l2(msg); + int rc = 0; + + switch (rslh->msg_discr & 0xfe) { + case ABIS_RSL_MDISC_RLL: + printf("rslms_recvmsg(ABIS_RSL_MDISC_RLL)\n"); + rc = rslms_rx_rll(msg, ms); + break; + default: + printf("unknown RSLms message discriminator 0x%02x", + rslh->msg_discr); + msgb_free(msg); + return -EINVAL; + } + + return rc; +} + diff --git a/src/host/layer23/src/layer3.c b/src/host/layer23/src/layer3.c new file mode 100644 index 0000000..ea6491e --- /dev/null +++ b/src/host/layer23/src/layer3.c @@ -0,0 +1,216 @@ +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +static void dump_bcch(uint8_t tc, const uint8_t *data) +{ + struct gsm48_system_information_type_header *si_hdr; + si_hdr = (struct gsm48_system_information_type_header *) data;; + + /* GSM 05.02 ยง6.3.1.3 Mapping of BCCH data */ + switch (si_hdr->system_information) { + case GSM48_MT_RR_SYSINFO_1: + fprintf(stderr, "\tSI1"); +#ifdef BCCH_TC_CHECK + if (tc != 0) + fprintf(stderr, " on wrong TC"); +#endif + break; + case GSM48_MT_RR_SYSINFO_2: + fprintf(stderr, "\tSI2"); +#ifdef BCCH_TC_CHECK + if (tc != 1) + fprintf(stderr, " on wrong TC"); +#endif + break; + case GSM48_MT_RR_SYSINFO_3: + fprintf(stderr, "\tSI3"); +#ifdef BCCH_TC_CHECK + if (tc != 2 && tc != 6) + fprintf(stderr, " on wrong TC"); +#endif + break; + case GSM48_MT_RR_SYSINFO_4: + fprintf(stderr, "\tSI4"); +#ifdef BCCH_TC_CHECK + if (tc != 3 && tc != 7) + fprintf(stderr, " on wrong TC"); +#endif + break; + case GSM48_MT_RR_SYSINFO_5: + fprintf(stderr, "\tSI5"); + break; + case GSM48_MT_RR_SYSINFO_6: + fprintf(stderr, "\tSI6"); + break; + case GSM48_MT_RR_SYSINFO_7: + fprintf(stderr, "\tSI7"); +#ifdef BCCH_TC_CHECK + if (tc != 7) + fprintf(stderr, " on wrong TC"); +#endif + break; + case GSM48_MT_RR_SYSINFO_8: + fprintf(stderr, "\tSI8"); +#ifdef BCCH_TC_CHECK + if (tc != 3) + fprintf(stderr, " on wrong TC"); +#endif + break; + case GSM48_MT_RR_SYSINFO_9: + fprintf(stderr, "\tSI9"); +#ifdef BCCH_TC_CHECK + if (tc != 4) + fprintf(stderr, " on wrong TC"); +#endif + break; + case GSM48_MT_RR_SYSINFO_13: + fprintf(stderr, "\tSI13"); +#ifdef BCCH_TC_CHECK + if (tc != 4 && tc != 0) + fprintf(stderr, " on wrong TC"); +#endif + break; + case GSM48_MT_RR_SYSINFO_16: + fprintf(stderr, "\tSI16"); +#ifdef BCCH_TC_CHECK + if (tc != 6) + fprintf(stderr, " on wrong TC"); +#endif + break; + case GSM48_MT_RR_SYSINFO_17: + fprintf(stderr, "\tSI17"); +#ifdef BCCH_TC_CHECK + if (tc != 2) + fprintf(stderr, " on wrong TC"); +#endif + break; + case GSM48_MT_RR_SYSINFO_2bis: + fprintf(stderr, "\tSI2bis"); +#ifdef BCCH_TC_CHECK + if (tc != 5) + fprintf(stderr, " on wrong TC"); +#endif + break; + case GSM48_MT_RR_SYSINFO_2ter: + fprintf(stderr, "\tSI2ter"); +#ifdef BCCH_TC_CHECK + if (tc != 5 && tc != 4) + fprintf(stderr, " on wrong TC"); +#endif + break; + case GSM48_MT_RR_SYSINFO_5bis: + fprintf(stderr, "\tSI5bis"); + break; + case GSM48_MT_RR_SYSINFO_5ter: + fprintf(stderr, "\tSI5ter"); + break; + default: + fprintf(stderr, "\tUnknown SI"); + break; + }; + + fprintf(stderr, "\n"); +} + + +/* send location updating request * (as part of RSLms EST IND / + LAPDm SABME) */ +static int gsm48_tx_loc_upd_req(struct osmocom_ms *ms, uint8_t chan_nr) +{ + struct msgb *msg = msgb_alloc_headroom(256, 16, "loc_upd_req"); + struct gsm48_hdr *gh; + struct gsm48_loc_upd_req *lu_r; + + printf("gsm48_tx_loc_upd_req()\n"); + + msg->l3h = msgb_put(msg, sizeof(*gh)); + gh = (struct gsm48_hdr *) msg->l3h; + gh->proto_discr = GSM48_PDISC_MM; + gh->msg_type = GSM48_MT_MM_LOC_UPD_REQUEST; + lu_r = (struct gsm48_loc_upd_req *) msgb_put(msg, sizeof(*lu_r)); + lu_r->type = GSM48_LUPD_IMSI_ATT; + lu_r->key_seq = 0; + /* FIXME: set LAI and CM1 */ + /* FIXME: set MI */ + lu_r->mi_len = 0; + + return rslms_tx_rll_req_l3(ms, RSL_MT_EST_REQ, chan_nr, 0, msg); +} + +static int gsm48_rx_imm_ass(struct msgb *msg, struct osmocom_ms *ms) +{ + struct gsm48_imm_ass *ia = msgb_l3(msg); + uint8_t ch_type, ch_subch, ch_ts; + uint16_t arfcn; + + rsl_dec_chan_nr(ia->chan_desc.chan_nr, &ch_type, &ch_subch, &ch_ts); + arfcn = ia->chan_desc.h0.arfcn_low | (ia->chan_desc.h0.arfcn_high << 8); + + printf("GSM48 IMM ASS (ra=0x%02x, chan_nr=0x%02x, ARFCN=%u, TS=%u, SS=%u, TSC=%u) ", + ia->req_ref.ra, ia->chan_desc.chan_nr, arfcn, ch_ts, ch_subch, + ia->chan_desc.h0.tsc); + + /* FIXME: compare RA and GSM time with when we sent RACH req */ + + /* check if we can support this type of channel at the moment */ + if (ch_type != RSL_CHAN_SDCCH4_ACCH || ch_ts != 0 || + ia->chan_desc.h0.h == 1) { + printf("UNSUPPORTED!\n"); + return 0; + } + + /* request L1 to go to dedicated mode on assigned channel */ + tx_ph_dm_est_req(ms, arfcn, ia->chan_desc.chan_nr); + + /* request L2 to establish the SAPI0 connection */ + gsm48_tx_loc_upd_req(ms, ia->chan_desc.chan_nr); + + return 0; +} + +int gsm48_rx_ccch(struct msgb *msg, struct osmocom_ms *ms) +{ + struct gsm48_system_information_type_header *sih = msgb_l3(msg); + int rc = 0; + + if (sih->rr_protocol_discriminator != GSM48_PDISC_RR) + printf("PCH pdisc != RR\n"); + + switch (sih->system_information) { + case GSM48_MT_RR_PAG_REQ_1: + case GSM48_MT_RR_PAG_REQ_2: + case GSM48_MT_RR_PAG_REQ_3: + /* FIXME: implement decoding of paging request */ + break; + case GSM48_MT_RR_IMM_ASS: + rc = gsm48_rx_imm_ass(msg, ms); + break; + default: + printf("unknown PCH/AGCH type 0x%02x\n", sih->system_information); + rc = -EINVAL; + } + + return rc; +} + +int gsm48_rx_bcch(struct msgb *msg, struct osmocom_ms *ms) +{ + /* FIXME: we have lost the gsm frame time until here, need to store it + * in some msgb context */ + //dump_bcch(dl->time.tc, ccch->data); + dump_bcch(0, msg->l3h); + + return 0; +} diff --git a/src/host/layer23/src/libosmocom/debug.c b/src/host/layer23/src/libosmocom/debug.c new file mode 120000 index 0000000..acf4261 --- /dev/null +++ b/src/host/layer23/src/libosmocom/debug.c @@ -0,0 +1 @@ +../../../libosmocom/src/debug.c \ No newline at end of file diff --git a/src/host/layer23/src/main.c b/src/host/layer23/src/main.c new file mode 100644 index 0000000..453fe6d --- /dev/null +++ b/src/host/layer23/src/main.c @@ -0,0 +1,225 @@ +/* Main method of the layer2/3 stack */ + +/* (C) 2010 by Holger Hans Peter Freyther + * (C) 2010 by Harald Welte + * + * 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 +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include + +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +#include "gsmtap_util.h" + +#define GSM_L2_LENGTH 256 + +static void *l2_ctx = NULL; +static char *socket_path = "/tmp/osmocom_l2"; +static struct osmocom_ms *ms = NULL; + +static int layer2_read(struct bsc_fd *fd, unsigned int flags) +{ + struct msgb *msg; + u_int16_t len; + int rc; + + msg = msgb_alloc(GSM_L2_LENGTH, "Layer2"); + if (!msg) { + fprintf(stderr, "Failed to allocate msg.\n"); + return -1; + } + + rc = read(fd->fd, &len, sizeof(len)); + if (rc < sizeof(len)) { + fprintf(stderr, "Short read. Error.\n"); + exit(2); + } + + len = ntohs(len); + if (len > GSM_L2_LENGTH) { + fprintf(stderr, "Length is too big: %u\n", len); + msgb_free(msg); + return -1; + } + + + /* blocking read for the poor... we can starve in here... */ + msg->l1h = msgb_put(msg, len); + rc = read(fd->fd, msg->l1h, msgb_l1len(msg)); + if (rc != msgb_l1len(msg)) { + fprintf(stderr, "Can not read data: len=%d rc=%d errno=%d\n", len, rc, errno); + msgb_free(msg); + return -1; + } + + l1ctl_recv((struct osmocom_ms *) fd->data, msg); + msgb_free(msg); + return 0; +} + +int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg) +{ + int rc; + uint16_t *len; + + printf("Sending: '%s'\n", hexdump(msg->data, msg->len)); + + if (msg->l1h != msg->data) + printf("Message L1 header != Message Data\n"); + + /* prepend 16bit length before sending */ + len = (uint16_t *) msgb_push(msg, sizeof(*len)); + *len = htons(msg->len - sizeof(*len)); + + /* TODO: just enqueue the message and wait for ready write.. */ + rc = write(ms->bfd.fd, msg->data, msg->len); + if (rc != msg->len) { + fprintf(stderr, "Failed to write data: rc: %d\n", rc); + msgb_free(msg); + return -1; + } + + msgb_free(msg); + return 0; +} + +static void print_usage() +{ + printf("Usage: ./layer2\n"); +} + +static void print_help() +{ + printf(" Some help...\n"); + printf(" -h --help this text\n"); + printf(" -s --socket /tmp/osmocom_l2. Path to the unix domain socket\n"); + printf(" -a --arfcn NR. The ARFCN to be used for layer2.\n"); +} + +static void handle_options(int argc, char **argv) +{ + while (1) { + int option_index = 0, c; + static struct option long_options[] = { + {"help", 0, 0, 'h'}, + {"socket", 1, 0, 's'}, + {"arfcn", 1, 0, 'a'}, + {0, 0, 0, 0}, + }; + + c = getopt_long(argc, argv, "hs:a:", + long_options, &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + print_usage(); + print_help(); + exit(0); + break; + case 's': + socket_path = talloc_strdup(l2_ctx, optarg); + break; + case 'a': + ms->arfcn = atoi(optarg); + break; + default: + break; + } + } +} + +int main(int argc, char **argv) +{ + int rc; + struct sockaddr_un local; + struct sockaddr_in gsmtap; + + l2_ctx = talloc_named_const(NULL, 1, "layer2 context"); + + ms = talloc_zero(l2_ctx, struct osmocom_ms); + if (!ms) { + fprintf(stderr, "Failed to allocate MS\n"); + exit(1); + } + + ms->arfcn = 871; + + handle_options(argc, argv); + + ms->bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (ms->bfd.fd < 0) { + fprintf(stderr, "Failed to create unix domain socket.\n"); + exit(1); + } + + local.sun_family = AF_UNIX; + strncpy(local.sun_path, socket_path, sizeof(local.sun_path)); + local.sun_path[sizeof(local.sun_path) - 1] = '\0'; + + rc = connect(ms->bfd.fd, (struct sockaddr *) &local, + sizeof(local.sun_family) + strlen(local.sun_path)); + if (rc < 0) { + fprintf(stderr, "Failed to connect to '%s'.\n", local.sun_path); + exit(1); + } + + ms->bfd.when = BSC_FD_READ; + ms->bfd.cb = layer2_read; + ms->bfd.data = ms; + + lapdm_init(&ms->lapdm_dcch, ms); + lapdm_init(&ms->lapdm_acch, ms); + + if (bsc_register_fd(&ms->bfd) != 0) { + fprintf(stderr, "Failed to register fd.\n"); + exit(1); + } + + rc = gsmtap_init(); + if (rc < 0) { + fprintf(stderr, "Failed during gsmtap_init()\n"); + exit(1); + } + + while (1) { + bsc_select_main(0); + } + + + return 0; +} diff --git a/src/host/layer23/src/rslms.c b/src/host/layer23/src/rslms.c new file mode 100644 index 0000000..f456029 --- /dev/null +++ b/src/host/layer23/src/rslms.c @@ -0,0 +1,162 @@ +/* RSLms - GSM 08.58 like protocol between L2 and L3 of GSM Um interface */ + +/* (C) 2010 by Harald Welte + * + * 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 +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +int rsl_dec_chan_nr(uint8_t chan_nr, uint8_t *type, uint8_t *subch, uint8_t *timeslot) +{ + *timeslot = chan_nr & 0x7; + + if ((chan_nr & 0xf8) == RSL_CHAN_Bm_ACCHs) { + *type = RSL_CHAN_Bm_ACCHs; + *subch = 0; + } else if ((chan_nr & 0xf0) == RSL_CHAN_Lm_ACCHs) { + *type = RSL_CHAN_Lm_ACCHs; + *subch = (chan_nr >> 3) & 0x1; + } else if ((chan_nr & 0xe0) == RSL_CHAN_SDCCH4_ACCH) { + *type = RSL_CHAN_SDCCH4_ACCH; + *subch = (chan_nr >> 3) & 0x3; + } else if ((chan_nr & 0xc0) == RSL_CHAN_SDCCH8_ACCH) { + *type = RSL_CHAN_SDCCH8_ACCH; + *subch = (chan_nr >> 3) & 0x7; + } else { + printf("unable to decode chan_nr\n"); + return -EINVAL; + } + + return 0; +} + +/* Send a 'simple' RLL request to L2 */ +int rslms_tx_rll_req(struct osmocom_ms *ms, uint8_t msg_type, + uint8_t chan_nr, uint8_t link_id) +{ + struct msgb *msg; + + msg = rsl_rll_simple(msg_type, chan_nr, link_id); + + return rslms_recvmsg(msg, ms); +} + +/* Send a RLL request (including L3 info) to L2 */ +int rslms_tx_rll_req_l3(struct osmocom_ms *ms, uint8_t msg_type, + uint8_t chan_nr, uint8_t link_id, struct msgb *msg) +{ + rsl_rll_push_l3(msg, msg_type, chan_nr, link_id); + + return rslms_recvmsg(msg, ms); +} + +static int rach_count = 0; + +static int rslms_rx_udata_ind(struct msgb *msg, struct osmocom_ms *ms) +{ + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + struct tlv_parsed tv; + int rc = 0; + + printf("RSLms UNIT DATA IND chan_nr=0x%02x link_id=0x%02x\n", + rllh->chan_nr, rllh->link_id); + + rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); + if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { + printf("UNIT_DATA_IND without L3 INFO ?!?\n"); + return -EIO; + } + msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO); + + if (rllh->chan_nr == RSL_CHAN_PCH_AGCH) + rc = gsm48_rx_ccch(msg, ms); + else if (rllh->chan_nr == RSL_CHAN_BCCH) { + rc = gsm48_rx_bcch(msg); + if (rach_count < 2) { + tx_ph_rach_req(ms); + rach_count++; + } + } + + return rc; +} + +static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms) +{ + struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + int rc = 0; + + switch (rllh->c.msg_type) { + case RSL_MT_DATA_IND: + printf("RSLms DATA IND\n"); + break; + case RSL_MT_UNIT_DATA_IND: + rc = rslms_rx_udata_ind(msg, ms); + break; + case RSL_MT_EST_IND: + printf("RSLms EST IND\n"); + break; + case RSL_MT_EST_CONF: + printf("RSLms EST CONF\n"); + break; + case RSL_MT_REL_CONF: + printf("RSLms REL CONF\n"); + break; + case RSL_MT_ERROR_IND: + printf("RSLms ERR IND\n"); + break; + default: + printf("unknown RSLms message type 0x%02x\n", rllh->c.msg_type); + rc = -EINVAL; + break; + } + return rc; +} + +/* input function that L2 calls when sending messages up to L3 */ +int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms) +{ + struct abis_rsl_common_hdr *rslh = msgb_l2(msg); + int rc = 0; + + switch (rslh->msg_discr & 0xfe) { + case ABIS_RSL_MDISC_RLL: + rc = rslms_rx_rll(msg, ms); + break; + default: + printf("unknown RSLms msg_discr 0x%02x\n", rslh->msg_discr); + rc = -EINVAL; + break; + } + + return rc; +} -- cgit v1.2.3-55-g7522