diff options
author | Sylvain Munaut <tnt@246tNt.com> | 2011-10-21 22:14:52 +0200 |
---|---|---|
committer | Sylvain Munaut <tnt@246tNt.com> | 2011-10-21 22:14:52 +0200 |
commit | 02d469ad67459c8d28c808a19802613a5666364c (patch) | |
tree | 137bf0649440b0f1ac9a72c168c7996484deda91 /src/shared | |
parent | 2a64b42111e50d71198bfec1ab28d01e6d1cfd2b (diff) | |
parent | 07f1103782a94090c2cef46de8a3f6d03ddfeef7 (diff) |
Merge commit '07f1103782a94090c2cef46de8a3f6d03ddfeef7'
Diffstat (limited to 'src/shared')
30 files changed, 4028 insertions, 1829 deletions
diff --git a/src/shared/libosmocore/.gitignore b/src/shared/libosmocore/.gitignore index b4d657f6..c77c750d 100644 --- a/src/shared/libosmocore/.gitignore +++ b/src/shared/libosmocore/.gitignore @@ -7,6 +7,8 @@ Makefile.in *.la *.pc aclocal.m4 +acinclude.m4 +aminclude.am m4/*.m4 autom4te.cache config.h* @@ -36,6 +38,7 @@ tests/timer/timer_test tests/msgfile/msgfile_test tests/ussd/ussd_test tests/smscb/smscb_test +tests/bits/bitrev_test utils/osmo-arfcn @@ -44,3 +47,6 @@ doc/core doc/vty doc/gsm doc/html.tar + +src/crc*gen.c +include/osmocom/core/crc*gen.h diff --git a/src/shared/libosmocore/include/osmocom/core/Makefile.am b/src/shared/libosmocore/include/osmocom/core/Makefile.am index f131269d..1df111af 100644 --- a/src/shared/libosmocore/include/osmocom/core/Makefile.am +++ b/src/shared/libosmocore/include/osmocom/core/Makefile.am @@ -2,8 +2,11 @@ osmocore_HEADERS = signal.h linuxlist.h timer.h select.h msgb.h bits.h \ bitvec.h statistics.h utils.h socket.h \ gsmtap.h write_queue.h prim.h \ logging.h rate_ctr.h gsmtap_util.h \ - crc16.h panic.h process.h \ - backtrace.h conv.h application.h + crc16.h panic.h process.h linuxrbtree.h \ + backtrace.h conv.h application.h \ + crcgen.h crc8gen.h crc16gen.h crc32gen.h crc64gen.h + +noinst_HEADERS = timer_compat.h if ENABLE_PLUGIN osmocore_HEADERS += plugin.h @@ -22,3 +25,7 @@ osmocore_HEADERS += serial.h endif osmocoredir = $(includedir)/osmocom/core + +crc%gen.h: crcXXgen.h.tpl + @echo " SED $< -> $@" + @sed -e's/XX/$*/g' $< > $@ diff --git a/src/shared/libosmocore/include/osmocom/core/crcXXgen.h.tpl b/src/shared/libosmocore/include/osmocom/core/crcXXgen.h.tpl new file mode 100644 index 00000000..b411276e --- /dev/null +++ b/src/shared/libosmocore/include/osmocom/core/crcXXgen.h.tpl @@ -0,0 +1,59 @@ +/* + * crcXXgen.h + * + * Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com> + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __OSMO_CRCXXGEN_H__ +#define __OSMO_CRCXXGEN_H__ + +/*! \addtogroup crcgen + * @{ + */ + +/*! \file crcXXgen.h + * \file Osmocom generic CRC routines (for max XX bits poly) header + */ + + +#include <stdint.h> +#include <osmocom/core/bits.h> + + +/*! \brief structure describing a given CRC code of max XX bits */ +struct osmo_crcXXgen_code { + int bits; /*!< \brief Actual number of bits of the CRC */ + uintXX_t poly; /*!< \brief Polynom (normal representation, MSB omitted */ + uintXX_t init; /*!< \brief Initialization value of the CRC state */ + uintXX_t remainder; /*!< \brief Remainder of the CRC (final XOR) */ +}; + +uintXX_t osmo_crcXXgen_compute_bits(const struct osmo_crcXXgen_code *code, + const ubit_t *in, int len); +int osmo_crcXXgen_check_bits(const struct osmo_crcXXgen_code *code, + const ubit_t *in, int len, const ubit_t *crc_bits); +void osmo_crcXXgen_set_bits(const struct osmo_crcXXgen_code *code, + const ubit_t *in, int len, ubit_t *crc_bits); + + +/*! }@ */ + +#endif /* __OSMO_CRCXXGEN_H__ */ + +/* vim: set syntax=c: */ diff --git a/src/shared/libosmocore/include/osmocom/core/crcgen.h b/src/shared/libosmocore/include/osmocom/core/crcgen.h new file mode 100644 index 00000000..cd916c76 --- /dev/null +++ b/src/shared/libosmocore/include/osmocom/core/crcgen.h @@ -0,0 +1,41 @@ +/* + * crcgen.h + * + * Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com> + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __OSMO_CRCGEN_H__ +#define __OSMO_CRCGEN_H__ + +/*! \defgroup crcgen Osmocom generic CRC routines + * @{ + */ + +/*! \file crcgen.h + * \file Osmocom generic CRC routines global header + */ + +#include <osmocom/core/crc8gen.h> +#include <osmocom/core/crc16gen.h> +#include <osmocom/core/crc32gen.h> +#include <osmocom/core/crc64gen.h> + +/*! }@ */ + +#endif /* __OSMO_CRCGEN_H__ */ diff --git a/src/shared/libosmocore/include/osmocom/core/gsmtap.h b/src/shared/libosmocore/include/osmocom/core/gsmtap.h index 236b25ac..dbc3d314 100644 --- a/src/shared/libosmocore/include/osmocom/core/gsmtap.h +++ b/src/shared/libosmocore/include/osmocom/core/gsmtap.h @@ -14,6 +14,21 @@ #include <stdint.h> +/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */ + +/* The GSMTAP format definition is maintained in libosmocore, + * specifically the latest version can always be obtained from + * http://cgit.osmocom.org/cgit/libosmocore/tree/include/osmocom/core/gsmtap.h + * + * If you want to introduce new protocol/burst/channel types or extend + * GSMTAP in any way, please contact the GSMTAP maintainer at either the + * public openbsc@lists.osmocom.org mailing list, or privately at + * Harald Welte <laforge@gnumonks.org>. + * + * Your cooperation ensures that all projects will use the same GSMTAP + * definitions and remain compatible with each other. + */ + #define GSMTAP_VERSION 0x02 #define GSMTAP_TYPE_UM 0x01 @@ -22,6 +37,12 @@ #define GSMTAP_TYPE_SIM 0x04 #define GSMTAP_TYPE_TETRA_I1 0x05 /* tetra air interface */ #define GSMTAP_TYPE_TETRA_I1_BURST 0x06 /* tetra air interface */ +#define GSMTAP_TYPE_WMX_BURST 0x07 /* WiMAX burst */ +#define GSMTAP_TYPE_GB_LLC 0x08 /* GPRS Gb interface: LLC */ +#define GSMTAP_TYPE_GB_SNDCP 0x09 /* GPRS Gb interface: SNDCP */ +#define GSMTAP_TYPE_GMR1_UM 0x0a /* GMR-1 L2 packets */ + +/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */ /* sub-types for TYPE_UM_BURST */ #define GSMTAP_BURST_UNKNOWN 0x00 @@ -34,6 +55,15 @@ #define GSMTAP_BURST_DUMMY 0x07 #define GSMTAP_BURST_ACCESS 0x08 #define GSMTAP_BURST_NONE 0x09 +/* WiMAX bursts */ +#define GSMTAP_BURST_CDMA_CODE 0x10 /* WiMAX CDMA Code Attribute burst */ +#define GSMTAP_BURST_FCH 0x11 /* WiMAX FCH burst */ +#define GSMTAP_BURST_FFB 0x12 /* WiMAX Fast Feedback burst */ +#define GSMTAP_BURST_PDU 0x13 /* WiMAX PDU burst */ +#define GSMTAP_BURST_HACK 0x14 /* WiMAX HARQ ACK burst */ +#define GSMTAP_BURST_PHY_ATTRIBUTES 0x15 /* WiMAX PHY Attributes burst */ + +/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */ /* sub-types for TYPE_UM */ #define GSMTAP_CHANNEL_UNKNOWN 0x00 @@ -47,8 +77,15 @@ #define GSMTAP_CHANNEL_SDCCH8 0x08 #define GSMTAP_CHANNEL_TCH_F 0x09 #define GSMTAP_CHANNEL_TCH_H 0x0a +#define GSMTAP_CHANNEL_CBCH51 0x0b +#define GSMTAP_CHANNEL_CBCH52 0x0c +#define GSMTAP_CHANNEL_PDCH 0x0d +#define GSMTAP_CHANNEL_PTCCH 0x0e +#define GSMTAP_CHANNEL_PACCH 0x0f #define GSMTAP_CHANNEL_ACCH 0x80 +/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */ + /* sub-types for TYPE_TETRA_AIR */ #define GSMTAP_TETRA_BSCH 0x01 #define GSMTAP_TETRA_AACH 0x02 @@ -59,6 +96,30 @@ #define GSMTAP_TETRA_STCH 0x07 #define GSMTAP_TETRA_TCH_F 0x08 +/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */ + +/* sub-types for TYPE_GMR1_UM */ +#define GSMTAP_GMR1_UNKNOWN 0x00 +#define GSMTAP_GMR1_BCCH 0x01 +#define GSMTAP_GMR1_CCCH 0x02 /* either AGCH or PCH */ +#define GSMTAP_GMR1_PCH 0x03 +#define GSMTAP_GMR1_AGCH 0x04 +#define GSMTAP_GMR1_BACH 0x05 +#define GSMTAP_GMR1_RACH 0x06 +#define GSMTAP_GMR1_CBCH 0x07 +#define GSMTAP_GMR1_SDCCH 0x08 +#define GSMTAP_GMR1_TACCH 0x09 +#define GSMTAP_GMR1_GBCH 0x0a + +#define GSMTAP_GMR1_SACCH 0x01 /* to be combined with _TCH{6,9} */ +#define GSMTAP_GMR1_FACCH 0x02 /* to be combines with _TCH{3,6,9} */ +#define GSMTAP_GMR1_DKAB 0x03 /* to be combined with _TCH3 */ +#define GSMTAP_GMR1_TCH3 0x10 +#define GSMTAP_GMR1_TCH6 0x14 +#define GSMTAP_GMR1_TCH9 0x18 + +/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */ + /* flags for the ARFCN */ #define GSMTAP_ARFCN_F_PCS 0x8000 #define GSMTAP_ARFCN_F_UPLINK 0x4000 @@ -67,6 +128,7 @@ /* IANA-assigned well-known UDP port for GSMTAP messages */ #define GSMTAP_UDP_PORT 4729 +/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */ struct gsmtap_hdr { uint8_t version; /* version, set to 0x01 currently */ uint8_t hdr_len; /* length in number of 32bit words */ diff --git a/src/shared/libosmocore/include/osmocom/core/gsmtap_util.h b/src/shared/libosmocore/include/osmocom/core/gsmtap_util.h index 36cbf532..5609381f 100644 --- a/src/shared/libosmocore/include/osmocom/core/gsmtap_util.h +++ b/src/shared/libosmocore/include/osmocom/core/gsmtap_util.h @@ -12,6 +12,10 @@ uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t rsl_link_id); +struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type, + uint8_t ss, uint32_t fn, int8_t signal_dbm, + uint8_t snr, const uint8_t *data, unsigned int len); + struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, uint8_t ss, uint32_t fn, int8_t signal_dbm, uint8_t snr, const uint8_t *data, unsigned int len); @@ -40,6 +44,11 @@ int gsmtap_source_add_sink(struct gsmtap_inst *gti); int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg); +int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts, + uint8_t chan_type, uint8_t ss, uint32_t fn, + int8_t signal_dbm, uint8_t snr, const uint8_t *data, + unsigned int len); + int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts, uint8_t chan_type, uint8_t ss, uint32_t fn, int8_t signal_dbm, uint8_t snr, const uint8_t *data, diff --git a/src/shared/libosmocore/include/osmocom/core/linuxrbtree.h b/src/shared/libosmocore/include/osmocom/core/linuxrbtree.h new file mode 100644 index 00000000..ee988918 --- /dev/null +++ b/src/shared/libosmocore/include/osmocom/core/linuxrbtree.h @@ -0,0 +1,160 @@ +/* + Red Black Trees + (C) 1999 Andrea Arcangeli <andrea@suse.de> + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + linux/include/linux/rbtree.h + + To use rbtrees you'll have to implement your own insert and search cores. + This will avoid us to use callbacks and to drop drammatically performances. + I know it's not the cleaner way, but in C (not in C++) to get + performances and genericity... + + Some example of insert and search follows here. The search is a plain + normal search over an ordered tree. The insert instead must be implemented + int two steps: as first thing the code must insert the element in + order as a red leaf in the tree, then the support library function + rb_insert_color() must be called. Such function will do the + not trivial work to rebalance the rbtree if necessary. + +----------------------------------------------------------------------- +static inline struct page * rb_search_page_cache(struct inode * inode, + unsigned long offset) +{ + struct rb_node * n = inode->i_rb_page_cache.rb_node; + struct page * page; + + while (n) + { + page = rb_entry(n, struct page, rb_page_cache); + + if (offset < page->offset) + n = n->rb_left; + else if (offset > page->offset) + n = n->rb_right; + else + return page; + } + return NULL; +} + +static inline struct page * __rb_insert_page_cache(struct inode * inode, + unsigned long offset, + struct rb_node * node) +{ + struct rb_node ** p = &inode->i_rb_page_cache.rb_node; + struct rb_node * parent = NULL; + struct page * page; + + while (*p) + { + parent = *p; + page = rb_entry(parent, struct page, rb_page_cache); + + if (offset < page->offset) + p = &(*p)->rb_left; + else if (offset > page->offset) + p = &(*p)->rb_right; + else + return page; + } + + rb_link_node(node, parent, p); + + return NULL; +} + +static inline struct page * rb_insert_page_cache(struct inode * inode, + unsigned long offset, + struct rb_node * node) +{ + struct page * ret; + if ((ret = __rb_insert_page_cache(inode, offset, node))) + goto out; + rb_insert_color(node, &inode->i_rb_page_cache); + out: + return ret; +} +----------------------------------------------------------------------- +*/ + +#ifndef _LINUX_RBTREE_H +#define _LINUX_RBTREE_H + +#include <stdlib.h> + +struct rb_node +{ + unsigned long rb_parent_color; +#define RB_RED 0 +#define RB_BLACK 1 + struct rb_node *rb_right; + struct rb_node *rb_left; +} __attribute__((aligned(sizeof(long)))); + /* The alignment might seem pointless, but allegedly CRIS needs it */ + +struct rb_root +{ + struct rb_node *rb_node; +}; + + +#define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3)) +#define rb_color(r) ((r)->rb_parent_color & 1) +#define rb_is_red(r) (!rb_color(r)) +#define rb_is_black(r) rb_color(r) +#define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0) +#define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0) + +static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p) +{ + rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p; +} +static inline void rb_set_color(struct rb_node *rb, int color) +{ + rb->rb_parent_color = (rb->rb_parent_color & ~1) | color; +} + +#define RB_ROOT { NULL, } +#define rb_entry(ptr, type, member) container_of(ptr, type, member) + +#define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL) +#define RB_EMPTY_NODE(node) (rb_parent(node) == node) +#define RB_CLEAR_NODE(node) (rb_set_parent(node, node)) + +extern void rb_insert_color(struct rb_node *, struct rb_root *); +extern void rb_erase(struct rb_node *, struct rb_root *); + +/* Find logical next and previous nodes in a tree */ +extern struct rb_node *rb_next(struct rb_node *); +extern struct rb_node *rb_prev(struct rb_node *); +extern struct rb_node *rb_first(struct rb_root *); +extern struct rb_node *rb_last(struct rb_root *); + +/* Fast replacement of a single node without remove/rebalance/add/rebalance */ +extern void rb_replace_node(struct rb_node *victim, struct rb_node *new, + struct rb_root *root); + +static inline void rb_link_node(struct rb_node * node, struct rb_node * parent, + struct rb_node ** rb_link) +{ + node->rb_parent_color = (unsigned long )parent; + node->rb_left = node->rb_right = NULL; + + *rb_link = node; +} + +#endif /* _LINUX_RBTREE_H */ diff --git a/src/shared/libosmocore/include/osmocom/core/logging.h b/src/shared/libosmocore/include/osmocom/core/logging.h index 72e4c93e..b207c1b4 100644 --- a/src/shared/libosmocore/include/osmocom/core/logging.h +++ b/src/shared/libosmocore/include/osmocom/core/logging.h @@ -62,7 +62,7 @@ void logp(int subsys, char *file, int line, int cont, const char *format, ...) _ /* logging levels defined by the library itself */ #define DLGLOBAL -1 -#define DLLAPDM -2 +#define DLLAPD -2 #define DLINP -3 #define DLMUX -4 #define DLMI -5 diff --git a/src/shared/libosmocore/include/osmocom/core/prim.h b/src/shared/libosmocore/include/osmocom/core/prim.h index 2e60c46d..b1026fe3 100644 --- a/src/shared/libosmocore/include/osmocom/core/prim.h +++ b/src/shared/libosmocore/include/osmocom/core/prim.h @@ -10,6 +10,9 @@ #include <stdint.h> #include <osmocom/core/msgb.h> +#define OSMO_PRIM(prim, op) ((prim << 8) | (op & 0xFF)) +#define OSMO_PRIM_HDR(oph) OSMO_PRIM((oph)->primitive, (oph)->operation) + /*! \brief primitive operation */ enum osmo_prim_operation { PRIM_OP_REQUEST, /*!< \brief request */ diff --git a/src/shared/libosmocore/include/osmocom/core/timer.h b/src/shared/libosmocore/include/osmocom/core/timer.h index 8f8c826d..30f558b4 100644 --- a/src/shared/libosmocore/include/osmocom/core/timer.h +++ b/src/shared/libosmocore/include/osmocom/core/timer.h @@ -32,6 +32,7 @@ #include <sys/time.h> #include <osmocom/core/linuxlist.h> +#include <osmocom/core/linuxrbtree.h> /** * Timer management: @@ -51,11 +52,10 @@ */ /*! \brief A structure representing a single instance of a timer */ struct osmo_timer_list { - struct llist_head entry; /*!< \brief linked list header */ + struct rb_node node; /*!< \brief rb-tree node header */ + struct llist_head list; /*!< \brief internal list header */ struct timeval timeout; /*!< \brief expiration time */ unsigned int active : 1; /*!< \brief is it active? */ - unsigned int handled : 1; /*!< \brief did we already handle it */ - unsigned int in_list : 1; /*!< \brief is it in the global list? */ void (*cb)(void*); /*!< \brief call-back called at timeout */ void *data; /*!< \brief user data for callback */ diff --git a/src/shared/libosmocore/include/osmocom/core/timer_compat.h b/src/shared/libosmocore/include/osmocom/core/timer_compat.h new file mode 100644 index 00000000..209e84a3 --- /dev/null +++ b/src/shared/libosmocore/include/osmocom/core/timer_compat.h @@ -0,0 +1,79 @@ +/* + * (C) 2011 Sylvain Munaut <tnt@246tNt.com> + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +/*! \defgroup timer Osmocom timers + * @{ + */ + +/*! \file timer_compat.h + * \brief Compatibility header with some helpers + */ + +#ifndef TIMER_COMPAT_H +#define TIMER_COMPAT_H + + +/* Convenience macros for operations on timevals. + NOTE: `timercmp' does not work for >= or <=. */ + +#ifndef timerisset +# define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) +#endif + +#ifndef timerclear +# define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) +#endif + +#ifndef timercmp +# define timercmp(a, b, CMP) \ + (((a)->tv_sec == (b)->tv_sec) ? \ + ((a)->tv_usec CMP (b)->tv_usec) : \ + ((a)->tv_sec CMP (b)->tv_sec)) +#endif + +#ifndef timeradd +# define timeradd(a, b, result) \ + do { \ + (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \ + (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \ + if ((result)->tv_usec >= 1000000) \ + { \ + ++(result)->tv_sec; \ + (result)->tv_usec -= 1000000; \ + } \ + } while (0) +#endif + +#ifndef timersub +# define timersub(a, b, result) \ + do { \ + (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ + (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ + if ((result)->tv_usec < 0) { \ + --(result)->tv_sec; \ + (result)->tv_usec += 1000000; \ + } \ + } while (0) +#endif + + +/*! }@ */ + +#endif /* TIMER_COMPAT_H */ diff --git a/src/shared/libosmocore/include/osmocom/gsm/Makefile.am b/src/shared/libosmocore/include/osmocom/gsm/Makefile.am index 90f19bc5..5971d0c5 100644 --- a/src/shared/libosmocore/include/osmocom/gsm/Makefile.am +++ b/src/shared/libosmocore/include/osmocom/gsm/Makefile.am @@ -1,6 +1,6 @@ osmogsm_HEADERS = a5.h comp128.h gsm0808.h gsm48_ie.h mncc.h rxlev_stat.h \ gsm0480.h gsm48.h gsm_utils.h rsl.h tlv.h abis_nm.h \ - sysinfo.h prim.h gsm0502.h lapdm.h + sysinfo.h prim.h gsm0502.h lapd_core.h lapdm.h SUBDIRS = protocol diff --git a/src/shared/libosmocore/include/osmocom/gsm/lapd_core.h b/src/shared/libosmocore/include/osmocom/gsm/lapd_core.h new file mode 100644 index 00000000..0f4e8899 --- /dev/null +++ b/src/shared/libosmocore/include/osmocom/gsm/lapd_core.h @@ -0,0 +1,171 @@ +#ifndef _OSMOCOM_LAPD_H +#define _OSMOCOM_LAPD_H + +#include <stdint.h> + +#include <osmocom/core/timer.h> +#include <osmocom/core/msgb.h> +#include <osmocom/gsm/prim.h> + +/*! \defgroup lapd LAPD implementation common part + * @{ + */ + +/*! \file lapd.h */ + +/* primitive related sutff */ + +/*! \brief LAPD related primitives (L2<->L3 SAP)*/ +enum osmo_dl_prim { + PRIM_DL_UNIT_DATA, /*!< \brief DL-UNIT-DATA */ + PRIM_DL_DATA, /*!< \brief DL-DATA */ + PRIM_DL_EST, /*!< \brief DL-ESTABLISH */ + PRIM_DL_REL, /*!< \brief DL-RLEEASE */ + PRIM_DL_SUSP, /*!< \brief DL-SUSPEND */ + PRIM_DL_RES, /*!< \brief DL-RESUME */ + PRIM_DL_RECON, /*!< \brief DL-RECONNECT */ + PRIM_MDL_ERROR, /*!< \brief MDL-ERROR */ +}; + +/* Uses the same values as RLL, so no conversion for GSM is required. */ +#define MDL_CAUSE_T200_EXPIRED 0x01 +#define MDL_CAUSE_REEST_REQ 0x02 +#define MDL_CAUSE_UNSOL_UA_RESP 0x03 +#define MDL_CAUSE_UNSOL_DM_RESP 0x04 +#define MDL_CAUSE_UNSOL_DM_RESP_MF 0x05 +#define MDL_CAUSE_UNSOL_SPRV_RESP 0x06 +#define MDL_CAUSE_SEQ_ERR 0x07 +#define MDL_CAUSE_UFRM_INC_PARAM 0x08 +#define MDL_CAUSE_SFRM_INC_PARAM 0x09 +#define MDL_CAUSE_IFRM_INC_MBITS 0x0a +#define MDL_CAUSE_IFRM_INC_LEN 0x0b +#define MDL_CAUSE_FRM_UNIMPL 0x0c +#define MDL_CAUSE_SABM_MF 0x0d +#define MDL_CAUSE_SABM_INFO_NOTALL 0x0e +#define MDL_CAUSE_FRMR 0x0f + +/*! \brief for MDL-ERROR.ind */ +struct mdl_error_ind_param { + uint8_t cause; /*!< \brief generic cause value */ +}; + +/*! \brief for DL-REL.req */ +struct dl_rel_req_param { + uint8_t mode; /*!< \brief release mode */ +}; + +/*! \brief primitive header for LAPD DL-SAP primitives */ +struct osmo_dlsap_prim { + struct osmo_prim_hdr oph; /*!< \brief generic primitive header */ + union { + struct mdl_error_ind_param error_ind; + struct dl_rel_req_param rel_req; + } u; /*!< \brief request-specific data */ +}; + +/*! \brief LAPD mode/role */ +enum lapd_mode { + LAPD_MODE_USER, /*!< \brief behave like user */ + LAPD_MODE_NETWORK, /*!< \brief behave like network */ +}; + +/*! \brief LAPD state (Figure B.2/Q.921)*/ +enum lapd_state { + LAPD_STATE_NULL = 0, + LAPD_STATE_TEI_UNASS, + LAPD_STATE_ASS_TEI_WAIT, + LAPD_STATE_EST_TEI_WAIT, + LAPD_STATE_IDLE, + LAPD_STATE_SABM_SENT, + LAPD_STATE_DISC_SENT, + LAPD_STATE_MF_EST, + LAPD_STATE_TIMER_RECOV, +}; + +/*! \brief LAPD message format (I / S / U) */ +enum lapd_format { + LAPD_FORM_UKN = 0, + LAPD_FORM_I, + LAPD_FORM_S, + LAPD_FORM_U, +}; + +/*! \brief LAPD message context */ +struct lapd_msg_ctx { + struct lapd_datalink *dl; + int n201; + /* address */ + uint8_t cr; + uint8_t sapi; + uint8_t tei; + uint8_t lpd; + /* control */ + uint8_t format; + uint8_t p_f; /* poll / final bit */ + uint8_t n_send; + uint8_t n_recv; + uint8_t s_u; /* S or repectivly U function bits */ + /* length */ + int length; + uint8_t more; +}; + +struct lapd_cr_ent { + uint8_t cmd; + uint8_t resp; +}; + +struct lapd_history { + struct msgb *msg; /* message to be sent / NULL, if histoy is empty */ + int more; /* if message is fragmented */ +}; + +/*! \brief LAPD datalink */ +struct lapd_datalink { + int (*send_dlsap)(struct osmo_dlsap_prim *dp, + struct lapd_msg_ctx *lctx); + int (*send_ph_data_req)(struct lapd_msg_ctx *lctx, struct msgb *msg); + struct { + /*! \brief filled-in once we set the lapd_mode above */ + struct lapd_cr_ent loc2rem; + struct lapd_cr_ent rem2loc; + } cr; + enum lapd_mode mode; /*!< \brief current mode of link */ + int use_sabme; /*!< \brief use SABME instead of SABM */ + int reestablish; /*!< \brief enable reestablish support */ + int n200, n200_est_rel; /*!< \brief number of retranmissions */ + struct lapd_msg_ctx lctx; /*!< \brief LAPD context */ + int maxf; /*!< \brief maximum frame size (after defragmentation) */ + uint8_t k; /*!< \brief maximum number of unacknowledged frames */ + uint8_t v_range; /*!< \brief range of sequence numbers */ + uint8_t v_send; /*!< \brief seq nr of next I frame to be transmitted */ + uint8_t v_ack; /*!< \brief last frame ACKed by peer */ + uint8_t v_recv; /*!< \brief seq nr of next I frame expected to be received */ + uint32_t state; /*!< \brief LAPD state (\ref lapd_state) */ + int seq_err_cond; /*!< \brief condition of sequence error */ + uint8_t own_busy; /*!< \brief receiver busy on our side */ + uint8_t peer_busy; /*!< \brief receiver busy on remote side */ + int t200_sec, t200_usec; /*!< \brief retry timer (default 1 sec) */ + int t203_sec, t203_usec; /*!< \brief retry timer (default 10 secs) */ + struct osmo_timer_list t200; /*!< \brief T200 timer */ + struct osmo_timer_list t203; /*!< \brief T203 timer */ + uint8_t retrans_ctr; /*!< \brief re-transmission counter */ + struct llist_head tx_queue; /*!< \brief frames to L1 */ + struct llist_head send_queue; /*!< \brief frames from L3 */ + struct msgb *send_buffer; /*!< \brief current frame transmitting */ + int send_out; /*!< \brief how much was sent from send_buffer */ + struct lapd_history *tx_hist; /*!< \brief tx history structure array */ + uint8_t range_hist; /*!< \brief range of history buffer 2..2^n */ + struct msgb *rcv_buffer; /*!< \brief buffer to assemble the received message */ + struct msgb *cont_res; /*!< \brief buffer to store content resolution data on network side, to detect multiple phones on same channel */ +}; + +void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range, + int maxf); +void lapd_dl_exit(struct lapd_datalink *dl); +void lapd_dl_reset(struct lapd_datalink *dl); +int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode); +int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx); +int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx); + +#endif /* _OSMOCOM_LAPD_H */ diff --git a/src/shared/libosmocore/include/osmocom/gsm/lapdm.h b/src/shared/libosmocore/include/osmocom/gsm/lapdm.h index b71feef1..cc9c63fe 100644 --- a/src/shared/libosmocore/include/osmocom/gsm/lapdm.h +++ b/src/shared/libosmocore/include/osmocom/gsm/lapdm.h @@ -1,11 +1,7 @@ #ifndef _OSMOCOM_LAPDM_H #define _OSMOCOM_LAPDM_H -#include <stdint.h> - -#include <osmocom/core/timer.h> -#include <osmocom/core/msgb.h> -#include <osmocom/gsm/prim.h> +#include <osmocom/gsm/lapd_core.h> /*! \defgroup lapdm LAPDm implementation according to GSM TS 04.06 * @{ @@ -15,7 +11,7 @@ /* primitive related sutff */ -/*! \brief LAPDm related primitives */ +/*! \brief LAPDm related primitives (L1<->L2 SAP) */ enum osmo_ph_prim { PRIM_PH_DATA, /*!< \brief PH-DATA */ PRIM_PH_RACH, /*!< \brief PH-RANDOM_ACCESS */ @@ -68,52 +64,22 @@ enum lapdm_mode { LAPDM_MODE_BTS, /*!< \brief behave like a BTS (network) */ }; -/*! \brief LAPDm state */ -enum lapdm_state { - LAPDm_STATE_NULL = 0, - LAPDm_STATE_IDLE, - LAPDm_STATE_SABM_SENT, - LAPDm_STATE_MF_EST, - LAPDm_STATE_TIMER_RECOV, - LAPDm_STATE_DISC_SENT, -}; - struct lapdm_entity; /*! \brief LAPDm message context */ struct lapdm_msg_ctx { struct lapdm_datalink *dl; int lapdm_fmt; - uint8_t n201; uint8_t chan_nr; uint8_t link_id; - uint8_t addr; - uint8_t ctrl; uint8_t ta_ind; uint8_t tx_power_ind; }; /*! \brief LAPDm datalink like TS 04.06 / Section 3.5.2 */ struct lapdm_datalink { - uint8_t V_send; /*!< \brief seq nr of next I frame to be transmitted */ - uint8_t V_ack; /*!< \brief last frame ACKed by peer */ - uint8_t N_send; /*!< \brief ? set to V_send at Tx time*/ - uint8_t V_recv; /*!< \brief seq nr of next I frame expected to be received */ - uint8_t N_recv; /*!< \brief expected send seq nr of the next received I frame */ - uint32_t state; /*!< \brief LAPDm state (\ref lapdm_state) */ - int seq_err_cond; /*!< \brief condition of sequence error */ - uint8_t own_busy; /*!< \brief receiver busy on our side */ - uint8_t peer_busy; /*!< \brief receiver busy on remote side */ - struct osmo_timer_list t200; /*!< \brief T200 timer */ - uint8_t retrans_ctr; /*!< \brief re-transmission counter */ - struct llist_head send_queue; /*!< \brief frames from L3 */ - struct msgb *send_buffer; /*!< \brief current frame transmitting */ - int send_out; /*!< \brief how much was sent from send_buffer */ - uint8_t tx_hist[8][200]; /*!< \brief tx history buffer */ - int tx_length[8]; /*!< \brief length in history buffer */ - struct llist_head tx_queue; /*!< \brief frames to L1 */ + struct lapd_datalink dl; /* \brief common LAPD */ struct lapdm_msg_ctx mctx; /*!< \brief context of established connection */ - struct msgb *rcv_buffer; /*!< \brief buffer to assemble the received message */ struct lapdm_entity *entity; /*!< \brief LAPDm entity we are part of */ }; @@ -127,11 +93,6 @@ enum lapdm_dl_sapi { typedef int (*lapdm_cb_t)(struct msgb *msg, struct lapdm_entity *le, void *ctx); -struct lapdm_cr_ent { - uint8_t cmd; - uint8_t resp; -}; - #define LAPDM_ENT_F_EMPTY_FRAME 0x0001 #define LAPDM_ENT_F_POLLING_ONLY 0x0002 @@ -144,12 +105,6 @@ struct lapdm_entity { enum lapdm_mode mode; /*!< \brief are we in BTS mode or MS mode */ unsigned int flags; - struct { - /*! \brief filled-in once we set the lapdm_mode above */ - struct lapdm_cr_ent loc2rem; - struct lapdm_cr_ent rem2loc; - } cr; - void *l1_ctx; /*!< \brief context for layer1 instance */ void *l3_ctx; /*!< \brief context for layer3 instance */ diff --git a/src/shared/libosmocore/include/osmocom/gsm/protocol/Makefile.am b/src/shared/libosmocore/include/osmocom/gsm/protocol/Makefile.am index 7f6de639..6ed55e46 100644 --- a/src/shared/libosmocore/include/osmocom/gsm/protocol/Makefile.am +++ b/src/shared/libosmocore/include/osmocom/gsm/protocol/Makefile.am @@ -1,6 +1,6 @@ osmogsm_proto_HEADERS = gsm_03_41.h \ gsm_04_08.h gsm_04_11.h gsm_04_12.h gsm_04_80.h \ - gsm_08_08.h gsm_08_58.h \ + gsm_08_08.h gsm_08_58.h gsm_44_318.h \ gsm_12_21.h ipaccess.h osmogsm_protodir = $(includedir)/osmocom/gsm/protocol diff --git a/src/shared/libosmocore/include/osmocom/gsm/protocol/gsm_44_318.h b/src/shared/libosmocore/include/osmocom/gsm/protocol/gsm_44_318.h new file mode 100644 index 00000000..31c0ea7c --- /dev/null +++ b/src/shared/libosmocore/include/osmocom/gsm/protocol/gsm_44_318.h @@ -0,0 +1,153 @@ +#ifndef PROTO_GSM_44_318_H +#define PROTO_GSM_44_318_H + +#include <stdint.h> + +/* Definitions according to 3GPP TS 44.318 6.8.0 Release 6 */ + +/* Table 11.1.1.4.1: Message types for URR */ + +enum gan_msg_type { + GA_MT_RC_DISCOVERY_REQUEST = 0x01, + GA_MT_RC_DISCOVERY_ACCEPT = 0x02, + GA_MT_RC_DISCOVERY_REJECT = 0x03, + + GA_MT_RC_REGISTER_REQUEST = 0x10, + GA_MT_RC_REGISTER_ACCEPT = 0x11, + GA_MT_RC_REGISTER_REDIRECT = 0x12, + GA_MT_RC_REGISTER_REJECT = 0x13, + GA_MT_RC_DEREGISTER = 0x14, + GA_MT_RC_REGISTER_UPDATE_UL = 0x15, + GA_MT_RC_REGISTER_UPDATE_DL = 0x16, + GA_MT_RC_CELL_BCAST_INFO = 0x17, + + GA_MT_CSR_CIPH_MODE_CMD = 0x20, + GA_MT_CSR_CIPH_MODE_COMPL = 0x21, + + GA_MT_CSR_ACT_CHAN = 0x30, + GA_MT_CSR_ACT_CHAN_ACK = 0x31, + GA_MT_CSR_ACT_CHAN_COMPL = 0x32, + GA_MT_CSR_ACT_CHAN_FAIL = 0x33, + GA_MT_CSR_CHAN_MODE_MOD = 0x34, + GA_MT_CSR_CHAN_MODE_MOD_ACK = 0x35, + + GA_MT_CSR_RELEASE = 0x40, + GA_MT_CSR_RELEASE_COMPL = 0x41, + GA_MT_CSR_CLEAR_REQ = 0x42, + + GA_MT_CSR_HO_ACCESS = 0x50, + GA_MT_CSR_HO_COMPL = 0x51, + GA_MT_CSR_UL_QUAL_IND = 0x52, + GA_MT_CSR_HO_INFO = 0x53, + GA_MT_CSR_HO_CMD = 0x54, + GA_MT_CSR_HO_FAIL = 0x55, + + GA_MT_CSR_PAGING_REQ = 0x60, + GA_MT_CSR_PAGING_RESP = 0x61, + + GA_MT_CSR_UL_DIRECT_XFER = 0x70, + GA_MT_CSR_DL_DIRECT_XFER = 0x72, + GA_MT_CSR_STATUS = 0x73, + GA_MT_RC_KEEPALIVE = 0x74, + GA_MT_CSR_CM_ENQ = 0x75, + GA_MT_CSR_CM_CHANGE = 0x76, + + GA_MT_CSR_REQUEST = 0x80, + GA_MT_CSR_REQUEST_ACCEPT = 0x81, + GA_MT_CSR_REQUEST_REJECT = 0x82, +}; + +/* All tables in 10.1.x and 10.2.x / Table 11.2.1 */ +enum gan_iei { + GA_IE_MI = 1, + GA_IE_GAN_RELEASE_IND = 2, + GA_IE_RADIO_IE = 3, + GA_IE_GERAN_CELL_ID = 4, + GA_IE_LAC = 5, + GA_IE_GERAN_COV_IND = 6, + GA_IE_GAN_CM = 7, + GA_IE_GEO_LOC = 8, + GA_IE_DEF_SEGW_IP = 9, + GA_IE_DEF_SEGW_FQDN = 10, + GA_IE_REDIR_CTR = 11, + GA_IE_DISCOV_REJ_CAUSE = 12, + GA_IE_GANC_CELL_DESC = 13, + GA_IE_GANC_CTRL_CH_DESC = 14, + GA_IE_GERAN_CELL_ID_LIST= 15, + GA_IE_TU3907_TIMER = 16, + GA_IE_RR_STATE = 17, + GA_IE_RAI = 18, + GA_IE_GAN_BAND = 19, + GA_IE_GARC_GACSR_STATE = 20, + GA_IE_REG_REJ_CAUSE = 21, + GA_IE_TU3906_TIMER = 22, + GA_IE_TU3910_TIMER = 23, + GA_IE_TU3902_TIMER = 24, + GA_IE_L3_MSG = 26, + GA_IE_CHAN_MODE = 27, + GA_IE_MS_CLASSMARK2 = 28, + GA_IE_RR_CAUSE = 29, + GA_EI_CIPH_MODE_SET = 30, + GA_IE_GPRS_RESUMPTION = 31, + GA_IE_HO_FROM_GAN_CMD = 32, + GA_IE_UL_QUAL_IND = 33, + GA_IE_TLLI = 34, + GA_IE_PFI = 35, + GA_IE_SUSP_CAUSE = 36, + GA_IE_TU3820_TIMER = 37, + GA_IE_REQD_QOS = 38, + GA_IE_P_DEACT_CAUSE = 39 + GA_IE_REQD_UL_RATE = 40, + GA_IE_RAC = 41, + GA_IE_AP_LOCATION = 42, + GA_IE_TU4001_TIMER = 43, + GA_IE_LOC_STATUS = 44, + GA_IE_CIPH_RESP = 45, + GA_IE_CIPH_RAND = 46, + GA_IE_CIPH_MAC = 47, + GA_IE_CKSN = 48, + GA_IE_SAPI_ID = 49, + GA_IE_EST_CAUSE = 50, + GA_IE_CHAN_NEEDED = 51, + GA_IE_PDU_IN_ERROR = 52, + GA_IE_SAMPLE_SIZE = 53, + GA_IE_PAYLOAD_TYPE = 54, + GA_IE_MULTIRATE_CONF = 55, + GA_IE_MS_CLASSMARK3 = 56, + GA_IE_LLC_PDU = 57, + GA_IE_LOC_BLACKL_IND = 58, + GA_IE_RESET_IND = 59, + GA_IE_TU4003_TIMER = 60, + GA_IE_AP_SERV_NAME = 61, + GA_IE_SERV_ZONE_INFO = 62, + GA_IE_RTP_RED_CONF = 63, + GA_IE_UTRAN_CLASSMARK = 64, + GA_IE_CM_ENQ_MASK = 65, + GA_IE_UTRAN_CELLID_LIST = 66, + GA_IE_SERV_GANC_TBL_IND = 67, + GA_IE_AP_REG_IND = 68, + GA_IE_GAN_PLMN_LIST = 69, + GA_IE_REQD_GAN_SERV = 71, + GA_IE_BCAST_CONTAINER = 72, + GA_IE_3G_CELL_ID = 73, + GA_IE_MS_RADIO_ID = 96, + GA_IE_DEF_GANC_IP = 97, + GA_IE_DEF_GANC_FQDN = 98, + GA_IE_GPRS_IP_ADDR = 99, + GA_IE_GPRS_UDP_PORT = 100 + GA_IE_GANC_TCP_PORT = 103, + GA_IE_RTP_UDP_PORT = 104, + GA_IE_RTCP_UDP_PORT = 105, + GA_IE_GERAN_RCV_SIGL_LIST = 106, + GA_IE_UTRAN_RCV_SIGL_LIST = 107, +}; + +/* 11.1.1 GA-RC and GA-CSR Message header IE */ +struct gan_rc_csr_hdr { + uint16_t len; + uint8_t pdisc:4, + skip_ind:4; + uint8_t msg_type; +} __attribute__((packed)); + +#endif /* PROTO_GSM_44_318_H */ diff --git a/src/shared/libosmocore/include/osmocom/gsm/sysinfo.h b/src/shared/libosmocore/include/osmocom/gsm/sysinfo.h index b808d6f9..06feb1de 100644 --- a/src/shared/libosmocore/include/osmocom/gsm/sysinfo.h +++ b/src/shared/libosmocore/include/osmocom/gsm/sysinfo.h @@ -37,7 +37,7 @@ typedef uint8_t sysinfo_buf_t[GSM_MACBLOCK_LEN]; extern const struct value_string osmo_sitype_strs[_MAX_SYSINFO_TYPE]; -uint8_t gsm_sitype2rsl(enum osmo_sysinfo_type si_type); +uint8_t osmo_sitype2rsl(enum osmo_sysinfo_type si_type); enum osmo_sysinfo_type osmo_rsl2sitype(uint8_t rsl_si); #endif /* _OSMO_GSM_SYSINFO_H */ diff --git a/src/shared/libosmocore/include/osmocom/vty/vty.h b/src/shared/libosmocore/include/osmocom/vty/vty.h index d1f6f440..ffe3c591 100644 --- a/src/shared/libosmocore/include/osmocom/vty/vty.h +++ b/src/shared/libosmocore/include/osmocom/vty/vty.h @@ -179,6 +179,9 @@ int vty_current_node(struct vty *vty); extern void *tall_vty_ctx; +extern struct cmd_element cfg_description_cmd; +extern struct cmd_element cfg_no_description_cmd; + /*! }@ */ #endif diff --git a/src/shared/libosmocore/src/Makefile.am b/src/shared/libosmocore/src/Makefile.am index 739095c8..6c0398bc 100644 --- a/src/shared/libosmocore/src/Makefile.am +++ b/src/shared/libosmocore/src/Makefile.am @@ -2,9 +2,9 @@ SUBDIRS=. vty codec gsm # This is _NOT_ the library release version, it's an API version. # Please read Chapter 6 "Library interface versions" of the libtool documentation before making any modification -LIBVERSION=2:0:0 +LIBVERSION=3:0:0 -INCLUDES = $(all_includes) -I$(top_srcdir)/include +INCLUDES = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir)/include AM_CFLAGS = -fPIC -Wall lib_LTLIBRARIES = libosmocore.la @@ -14,7 +14,8 @@ libosmocore_la_SOURCES = timer.c select.c signal.c msgb.c bits.c \ write_queue.c utils.c socket.c \ logging.c logging_syslog.c rate_ctr.c \ gsmtap_util.c crc16.c panic.c backtrace.c \ - conv.c application.c + conv.c application.c rbtree.c \ + crc8gen.c crc16gen.c crc32gen.c crc64gen.c if ENABLE_PLUGIN libosmocore_la_SOURCES += plugin.c @@ -25,6 +26,8 @@ endif if ENABLE_TALLOC libosmocore_la_SOURCES += talloc.c +else +libosmocore_la_LIBADD = -ltalloc endif if ENABLE_MSGFILE @@ -34,3 +37,7 @@ endif if ENABLE_SERIAL libosmocore_la_SOURCES += serial.c endif + +crc%gen.c: crcXXgen.c.tpl + @echo " SED $< -> $@" + @sed -e's/XX/$*/g' $< > $@ diff --git a/src/shared/libosmocore/src/crcXXgen.c.tpl b/src/shared/libosmocore/src/crcXXgen.c.tpl new file mode 100644 index 00000000..5d70753a --- /dev/null +++ b/src/shared/libosmocore/src/crcXXgen.c.tpl @@ -0,0 +1,120 @@ +/* + * crcXXgen.c + * + * Generic CRC routines (for max XX bits poly) + * + * Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com> + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/*! \addtogroup crcgen + * @{ + */ + +/*! \file crcXXgen.c + * \file Osmocom generic CRC routines (for max XX bits poly) + */ + +#include <stdint.h> + +#include <osmocom/core/bits.h> +#include <osmocom/core/crcXXgen.h> + + +/*! \brief Compute the CRC value of a given array of hard-bits + * \param[in] code The CRC code description to apply + * \param[in] in Array of hard bits + * \param[in] len Length of the array of hard bits + * \returns The CRC value + */ +uintXX_t +osmo_crcXXgen_compute_bits(const struct osmo_crcXXgen_code *code, + const ubit_t *in, int len) +{ + const uintXX_t poly = code->poly; + uintXX_t crc = code->init; + int i, n = code->bits-1; + + for (i=0; i<len; i++) { + uintXX_t bit = in[i] & 1; + crc ^= (bit << n); + if (crc & (1 << n)) { + crc <<= 1; + crc ^= poly; + } else { + crc <<= 1; + } + crc &= (1ULL << code->bits) - 1; + } + + crc ^= code->remainder; + + return crc; +} + + +/*! \brief Checks the CRC value of a given array of hard-bits + * \param[in] code The CRC code description to apply + * \param[in] in Array of hard bits + * \param[in] len Length of the array of hard bits + * \param[in] crc_bits Array of hard bits with the alleged CRC + * \returns 0 if CRC matches. 1 in case of error. + * + * The crc_bits array must have a length of code->len + */ +int +osmo_crcXXgen_check_bits(const struct osmo_crcXXgen_code *code, + const ubit_t *in, int len, const ubit_t *crc_bits) +{ + uintXX_t crc; + int i; + + crc = osmo_crcXXgen_compute_bits(code, in, len); + + for (i=0; i<code->bits; i++) + if (crc_bits[i] ^ ((crc >> (code->bits-i-1)) & 1)) + return 1; + + return 0; +} + + +/*! \brief Computes and writes the CRC value of a given array of bits + * \param[in] code The CRC code description to apply + * \param[in] in Array of hard bits + * \param[in] len Length of the array of hard bits + * \param[in] crc_bits Array of hard bits to write the computed CRC to + * + * The crc_bits array must have a length of code->len + */ +void +osmo_crcXXgen_set_bits(const struct osmo_crcXXgen_code *code, + const ubit_t *in, int len, ubit_t *crc_bits) +{ + uintXX_t crc; + int i; + + crc = osmo_crcXXgen_compute_bits(code, in, len); + + for (i=0; i<code->bits; i++) + crc_bits[i] = ((crc >> (code->bits-i-1)) & 1); +} + +/*! }@ */ + +/* vim: set syntax=c: */ diff --git a/src/shared/libosmocore/src/gsm/Makefile.am b/src/shared/libosmocore/src/gsm/Makefile.am index f5e46769..f27dff2f 100644 --- a/src/shared/libosmocore/src/gsm/Makefile.am +++ b/src/shared/libosmocore/src/gsm/Makefile.am @@ -1,6 +1,6 @@ # This is _NOT_ the library release version, it's an API version. # Please read Chapter 6 "Library interface versions" of the libtool documentation before making any modification -LIBVERSION=1:0:0 +LIBVERSION=1:1:0 INCLUDES = $(all_includes) -I$(top_srcdir)/include AM_CFLAGS = -fPIC -Wall @@ -10,6 +10,6 @@ lib_LTLIBRARIES = libosmogsm.la libosmogsm_la_SOURCES = a5.c rxlev_stat.c tlv_parser.c comp128.c gsm_utils.c \ rsl.c gsm48.c gsm48_ie.c gsm0808.c sysinfo.c \ gprs_cipher_core.c gsm0480.c abis_nm.c gsm0502.c \ - lapdm.c + lapd_core.c lapdm.c libosmogsm_la_LDFLAGS = -version-info $(LIBVERSION) libosmogsm_la_LIBADD = $(top_builddir)/src/libosmocore.la diff --git a/src/shared/libosmocore/src/gsm/lapd_core.c b/src/shared/libosmocore/src/gsm/lapd_core.c new file mode 100644 index 00000000..dcc21506 --- /dev/null +++ b/src/shared/libosmocore/src/gsm/lapd_core.c @@ -0,0 +1,2143 @@ +/* LAPD core implementation */ + +/* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org> + * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu> + * + * 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. + * + */ + +/*! \addtogroup lapd + * @{ + */ + +/*! \file lapd.c */ + +/*! + * Notes on Buffering: rcv_buffer, tx_queue, tx_hist, send_buffer, send_queue + * + * RX data is stored in the rcv_buffer (pointer). If the message is complete, it + * is removed from rcv_buffer pointer and forwarded to L3. If the RX data is + * received while there is an incomplete rcv_buffer, it is appended to it. + * + * TX data is stored in the send_queue first. When transmitting a frame, + * the first message in the send_queue is moved to the send_buffer. There it + * resides until all fragments are acknowledged. Fragments to be sent by I + * frames are stored in the tx_hist buffer for resend, if required. Also the + * current fragment is copied into the tx_queue. There it resides until it is + * forwarded to layer 1. + * + * In case we have SAPI 0, we only have a window size of 1, so the unack- + * nowledged message resides always in the send_buffer. In case of a suspend, + * it can be written back to the first position of the send_queue. + * + * The layer 1 normally sends a PH-READY-TO-SEND. But because we use + * asynchronous transfer between layer 1 and layer 2 (serial link), we must + * send a frame before layer 1 reaches the right timeslot to send it. So we + * move the tx_queue to layer 1 when there is not already a pending frame, and + * wait until acknowledge after the frame has been sent. If we receive an + * acknowledge, we can send the next frame from the buffer, if any. + * + * The moving of tx_queue to layer 1 may also trigger T200, if desired. Also it + * will trigger next I frame, if possible. + * + * T203 is optional. It will be stated when entering MF EST state. It will also + * be started when I or S frame is received in that state . It will be + * restarted in the lapd_acknowledge() function, in case outstanding frames + * will not trigger T200. It will be stoped, when T200 is started in MF EST + * state. It will also be stoped when leaving MF EST state. + * + */ + +/* Enable this to test content resolution on network side: + * - The first SABM is received, UA is dropped. + * - The phone repeats SABM, but it's content is wrong, so it is ignored + * - The phone repeats SABM again, content is right, so UA is sent. + */ +//#define TEST_CONTENT_RESOLUTION_NETWORK + +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <errno.h> +#include <arpa/inet.h> + +#include <osmocom/core/logging.h> +#include <osmocom/core/timer.h> +#include <osmocom/core/msgb.h> +#include <osmocom/core/utils.h> +#include <osmocom/core/talloc.h> +#include <osmocom/gsm/lapd_core.h> + +/* TS 04.06 Table 4 / Section 3.8.1 */ +#define LAPD_U_SABM 0x7 +#define LAPD_U_SABME 0xf +#define LAPD_U_DM 0x3 +#define LAPD_U_UI 0x0 +#define LAPD_U_DISC 0x8 +#define LAPD_U_UA 0xC +#define LAPD_U_FRMR 0x11 + +#define LAPD_S_RR 0x0 +#define LAPD_S_RNR 0x1 +#define LAPD_S_REJ 0x2 + +#define CR_USER2NET_CMD 0 +#define CR_USER2NET_RESP 1 +#define CR_NET2USER_CMD 1 +#define CR_NET2USER_RESP 0 + +#define LAPD_HEADROOM 56 + +#define SBIT(a) (1 << a) +#define ALL_STATES 0xffffffff + +static void lapd_t200_cb(void *data); +static void lapd_t203_cb(void *data); +static int lapd_send_i(struct lapd_msg_ctx *lctx, int line); +static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx); + +/* UTILITY FUNCTIONS */ + +struct msgb *lapd_msgb_alloc(int length, const char *name) +{ + /* adding space for padding, FIXME: add as an option */ + if (length < 21) + length = 21; + return msgb_alloc_headroom(length + LAPD_HEADROOM, LAPD_HEADROOM, name); +} + +static inline uint8_t do_mod(uint8_t x, uint8_t m) +{ + return x & (m - 1); +} + +static inline uint8_t inc_mod(uint8_t x, uint8_t m) +{ + return (x + 1) & (m - 1); +} + +static inline uint8_t add_mod(uint8_t x, uint8_t y, uint8_t m) +{ + return (x + y) & (m - 1); +} + +static inline uint8_t sub_mod(uint8_t x, uint8_t y, uint8_t m) +{ + return (x - y) & (m - 1); /* handle negative results correctly */ +} + +static void lapd_dl_flush_send(struct lapd_datalink *dl) +{ + struct msgb *msg; + + /* Flush send-queue */ + while ((msg = msgb_dequeue(&dl->send_queue))) + msgb_free(msg); + + /* Clear send-buffer */ + if (dl->send_buffer) { + msgb_free(dl->send_buffer); + dl->send_buffer = NULL; + } +} + +static void lapd_dl_flush_hist(struct lapd_datalink *dl) +{ + unsigned int i; + + for (i = 0; i < dl->range_hist; i++) { + if (dl->tx_hist[i].msg) { + msgb_free(dl->tx_hist[i].msg); + dl->tx_hist[i].msg = NULL; + } + } +} + +static void lapd_dl_flush_tx(struct lapd_datalink *dl) +{ + struct msgb *msg; + + while ((msg = msgb_dequeue(&dl->tx_queue))) + msgb_free(msg); + lapd_dl_flush_hist(dl); +} + +/* Figure B.2/Q.921 */ +const char *lapd_state_names[] = { + "LAPD_STATE_NULL", + "LAPD_STATE_TEI_UNASS", + "LAPD_STATE_ASS_TEI_WAIT", + "LAPD_STATE_EST_TEI_WAIT", + "LAPD_STATE_IDLE", + "LAPD_STATE_SABM_SENT", + "LAPD_STATE_DISC_SENT", + "LAPD_STATE_MF_EST", + "LAPD_STATE_TIMER_RECOV", + +}; + +static void lapd_start_t200(struct lapd_datalink *dl) +{ + if (osmo_timer_pending(&dl->t200)) + return; + LOGP(DLLAPD, LOGL_INFO, "start T200\n"); + osmo_timer_schedule(&dl->t200, dl->t200_sec, dl->t200_usec); +} + +static void lapd_start_t203(struct lapd_datalink *dl) +{ + if (osmo_timer_pending(&dl->t203)) + return; + LOGP(DLLAPD, LOGL_INFO, "start T203\n"); + osmo_timer_schedule(&dl->t203, dl->t203_sec, dl->t203_usec); +} + +static void lapd_stop_t200(struct lapd_datalink *dl) +{ + if (!osmo_timer_pending(&dl->t200)) + return; + LOGP(DLLAPD, LOGL_INFO, "stop T200\n"); + osmo_timer_del(&dl->t200); +} + +static void lapd_stop_t203(struct lapd_datalink *dl) +{ + if (!osmo_timer_pending(&dl->t203)) + return; + LOGP(DLLAPD, LOGL_INFO, "stop T203\n"); + osmo_timer_del(&dl->t203); +} + +static void lapd_dl_newstate(struct lapd_datalink *dl, uint32_t state) +{ + LOGP(DLLAPD, LOGL_INFO, "new state %s -> %s\n", + lapd_state_names[dl->state], lapd_state_names[state]); + + if (state != LAPD_STATE_MF_EST && dl->state == LAPD_STATE_MF_EST) { + /* stop T203 on leaving MF EST state, if running */ + lapd_stop_t203(dl); + /* remove content res. (network side) on leaving MF EST state */ + if (dl->cont_res) { + msgb_free(dl->cont_res); + dl->cont_res = NULL; + } + } + + /* start T203 on entering MF EST state, if enabled */ + if ((dl->t203_sec || dl->t203_usec) + && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST) + lapd_start_t203(dl); + + dl->state = state; +} + +static void *tall_lapd_ctx = NULL; + +/* init datalink instance and allocate history */ +void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range, + int maxf) +{ + int m; + + memset(dl, 0, sizeof(*dl)); + INIT_LLIST_HEAD(&dl->send_queue); + INIT_LLIST_HEAD(&dl->tx_queue); + dl->reestablish = 1; + dl->n200_est_rel = 3; + dl->n200 = 3; + dl->t200_sec = 1; + dl->t200_usec = 0; + dl->t200.data = dl; + dl->t200.cb = &lapd_t200_cb; + dl->t203_sec = 10; + dl->t203_usec = 0; + dl->t203.data = dl; + dl->t203.cb = &lapd_t203_cb; + dl->maxf = maxf; + if (k > v_range - 1) + k = v_range - 1; + dl->k = k; + dl->v_range = v_range; + + /* Calculate modulo for history array: + * - The history range must be at least k+1. + * - The history range must be 2^x, where x is as low as possible. + */ + k++; + for (m = 0x80; m; m >>= 1) { + if ((m & k)) { + if (k > m) + m <<= 1; + dl->range_hist = m; + break; + } + } + + LOGP(DLLAPD, LOGL_INFO, "Init DL layer: sequence range = %d, k = %d, " + "history range = %d\n", dl->v_range, dl->k, dl->range_hist); + + lapd_dl_newstate(dl, LAPD_STATE_IDLE); + + if (!tall_lapd_ctx) + tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context"); + dl->tx_hist = (struct lapd_history *) talloc_zero_array(tall_lapd_ctx, + struct log_info, dl->range_hist); +} + +/* reset to IDLE state */ +void lapd_dl_reset(struct lapd_datalink *dl) +{ + if (dl->state == LAPD_STATE_IDLE) + return; + LOGP(DLLAPD, LOGL_INFO, "Resetting LAPDm instance\n"); + /* enter idle state (and remove eventual cont_res) */ + lapd_dl_newstate(dl, LAPD_STATE_IDLE); + /* flush buffer */ + lapd_dl_flush_tx(dl); + lapd_dl_flush_send(dl); + /* Discard partly received L3 message */ + if (dl->rcv_buffer) { + msgb_free(dl->rcv_buffer); + dl->rcv_buffer = NULL; + } + /* stop Timers */ + lapd_stop_t200(dl); + lapd_stop_t203(dl); +} + +/* reset and de-allocate history buffer */ +void lapd_dl_exit(struct lapd_datalink *dl) +{ + /* free all ressources except history buffer */ + lapd_dl_reset(dl); + /* free history buffer list */ + talloc_free(dl->tx_hist); +} + +/*! \brief Set the \ref lapdm_mode of a LAPDm entity */ +int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode) +{ + switch (mode) { + case LAPD_MODE_USER: + dl->cr.loc2rem.cmd = CR_USER2NET_CMD; + dl->cr.loc2rem.resp = CR_USER2NET_RESP; + dl->cr.rem2loc.cmd = CR_NET2USER_CMD; + dl->cr.rem2loc.resp = CR_NET2USER_RESP; + break; + case LAPD_MODE_NETWORK: + dl->cr.loc2rem.cmd = CR_NET2USER_CMD; + dl->cr.loc2rem.resp = CR_NET2USER_RESP; + dl->cr.rem2loc.cmd = CR_USER2NET_CMD; + dl->cr.rem2loc.resp = CR_USER2NET_RESP; + break; + default: + return -EINVAL; + } + dl->mode = mode; + + return 0; +} + +/* send DL message with optional msgb */ +static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx, + struct msgb *msg) +{ + struct lapd_datalink *dl = lctx->dl; + struct osmo_dlsap_prim dp; + + osmo_prim_init(&dp.oph, 0, prim, op, msg); + return dl->send_dlsap(&dp, lctx); +} + +/* send simple DL message */ +static inline int send_dl_simple(uint8_t prim, uint8_t op, + struct lapd_msg_ctx *lctx) +{ + struct msgb *msg = lapd_msgb_alloc(0, "DUMMY"); + + return send_dl_l3(prim, op, lctx, msg); +} + +/* send MDL-ERROR INDICATION */ +static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + struct osmo_dlsap_prim dp; + + LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND cause %d\n", + cause); + osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL); + dp.u.error_ind.cause = cause; + return dl->send_dlsap(&dp, lctx); +} + +/* send UA response */ +static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data) +{ + struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA"); + struct lapd_msg_ctx nctx; + struct lapd_datalink *dl = lctx->dl; + + memcpy(&nctx, lctx, sizeof(nctx)); + msg->l3h = msgb_put(msg, len); + if (len) + memcpy(msg->l3h, data, len); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = dl->cr.loc2rem.resp; + nctx.format = LAPD_FORM_U; + nctx.s_u = LAPD_U_UA; + /* keep nctx.p_f */ + nctx.length = len; + nctx.more = 0; + + return dl->send_ph_data_req(&nctx, msg); +} + +/* send DM response */ +static int lapd_send_dm(struct lapd_msg_ctx *lctx) +{ + struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM"); + struct lapd_msg_ctx nctx; + struct lapd_datalink *dl = lctx->dl; + + memcpy(&nctx, lctx, sizeof(nctx)); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = dl->cr.loc2rem.resp; + nctx.format = LAPD_FORM_U; + nctx.s_u = LAPD_U_DM; + /* keep nctx.p_f */ + nctx.length = 0; + nctx.more = 0; + + return dl->send_ph_data_req(&nctx, msg); +} + +/* send RR response / command */ +static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd) +{ + struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR"); + struct lapd_msg_ctx nctx; + struct lapd_datalink *dl = lctx->dl; + + memcpy(&nctx, lctx, sizeof(nctx)); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp; + nctx.format = LAPD_FORM_S; + nctx.s_u = LAPD_S_RR; + nctx.p_f = f_bit; + nctx.n_recv = dl->v_recv; + nctx.length = 0; + nctx.more = 0; + + return dl->send_ph_data_req(&nctx, msg); +} + +/* send RNR response / command */ +static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd) +{ + struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR"); + struct lapd_msg_ctx nctx; + struct lapd_datalink *dl = lctx->dl; + + memcpy(&nctx, lctx, sizeof(nctx)); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp; + nctx.format = LAPD_FORM_S; + nctx.s_u = LAPD_S_RNR; + nctx.p_f = f_bit; + nctx.n_recv = dl->v_recv; + nctx.length = 0; + nctx.more = 0; + + return dl->send_ph_data_req(&nctx, msg); +} + +/* send REJ response */ +static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit) +{ + struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ"); + struct lapd_msg_ctx nctx; + struct lapd_datalink *dl = lctx->dl; + + memcpy(&nctx, lctx, sizeof(nctx)); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = dl->cr.loc2rem.resp; + nctx.format = LAPD_FORM_S; + nctx.s_u = LAPD_S_REJ; + nctx.p_f = f_bit; + nctx.n_recv = dl->v_recv; + nctx.length = 0; + nctx.more = 0; + + return dl->send_ph_data_req(&nctx, msg); +} + +/* resend SABM or DISC message */ +static int lapd_send_resend(struct lapd_datalink *dl) +{ + struct msgb *msg; + uint8_t h = do_mod(dl->v_send, dl->range_hist); + int length = dl->tx_hist[h].msg->len; + struct lapd_msg_ctx nctx; + + /* assemble message */ + memcpy(&nctx, &dl->lctx, sizeof(nctx)); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = dl->cr.loc2rem.cmd; + nctx.format = LAPD_FORM_U; + if (dl->state == LAPD_STATE_SABM_SENT) + nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM; + else + nctx.s_u = LAPD_U_DISC; + nctx.p_f = 1; + nctx.length = length; + nctx.more = 0; + + /* Resend SABM/DISC from tx_hist */ + msg = lapd_msgb_alloc(length, "LAPD resend"); + msg->l3h = msgb_put(msg, length); + if (length) + memcpy(msg->l3h, dl->tx_hist[h].msg->data, length); + + return dl->send_ph_data_req(&nctx, msg); +} + +/* reestablish link */ +static int lapd_reestablish(struct lapd_datalink *dl) +{ + struct osmo_dlsap_prim dp; + struct msgb *msg; + + msg = lapd_msgb_alloc(0, "DUMMY"); + osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg); + + return lapd_est_req(&dp, &dl->lctx); +} + +/* Timer callback on T200 expiry */ +static void lapd_t200_cb(void *data) +{ + struct lapd_datalink *dl = data; + + LOGP(DLLAPD, LOGL_INFO, "Timeout T200 (%p) state=%d\n", dl, + (int) dl->state); + + switch (dl->state) { + case LAPD_STATE_SABM_SENT: + /* 5.4.1.3 */ + if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) { + /* send RELEASE INDICATION to L3 */ + send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, + &dl->lctx); + /* send MDL ERROR INIDCATION to L3 */ + mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx); + /* flush tx and send buffers */ + lapd_dl_flush_tx(dl); + lapd_dl_flush_send(dl); + /* go back to idle state */ + lapd_dl_newstate(dl, LAPD_STATE_IDLE); + /* NOTE: we must not change any other states or buffers + * and queues, since we may reconnect after handover + * failure. the buffered messages is replaced there */ + break; + } + /* retransmit SABM command */ + lapd_send_resend(dl); + /* increment re-transmission counter */ + dl->retrans_ctr++; + /* restart T200 (PH-READY-TO-SEND) */ + lapd_start_t200(dl); + break; + case LAPD_STATE_DISC_SENT: + /* 5.4.4.3 */ + if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) { + /* send RELEASE INDICATION to L3 */ + send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx); + /* send MDL ERROR INIDCATION to L3 */ + mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx); + /* flush tx and send buffers */ + lapd_dl_flush_tx(dl); + lapd_dl_flush_send(dl); + /* go back to idle state */ + lapd_dl_newstate(dl, LAPD_STATE_IDLE); + /* NOTE: we must not change any other states or buffers + * and queues, since we may reconnect after handover + * failure. the buffered messages is replaced there */ + break; + } + /* retransmit DISC command */ + lapd_send_resend(dl); + /* increment re-transmission counter */ + dl->retrans_ctr++; + /* restart T200 (PH-READY-TO-SEND) */ + lapd_start_t200(dl); + break; + case LAPD_STATE_MF_EST: + /* 5.5.7 */ + dl->retrans_ctr = 0; + lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV); + /* fall through */ + case LAPD_STATE_TIMER_RECOV: + dl->retrans_ctr++; + if (dl->retrans_ctr < dl->n200) { + uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range); + uint8_t h = do_mod(vs, dl->range_hist); + /* retransmit I frame (V_s-1) with P=1, if any */ + if (dl->tx_hist[h].msg) { + struct msgb *msg; + int length = dl->tx_hist[h].msg->len; + struct lapd_msg_ctx nctx; + + LOGP(DLLAPD, LOGL_INFO, "retransmit last frame" + " V(S)=%d\n", vs); + /* Create I frame (segment) from tx_hist */ + memcpy(&nctx, &dl->lctx, sizeof(nctx)); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = dl->cr.loc2rem.cmd; + nctx.format = LAPD_FORM_I; + nctx.p_f = 1; + nctx.n_send = vs; + nctx.n_recv = dl->v_recv; + nctx.length = length; + nctx.more = dl->tx_hist[h].more; + msg = lapd_msgb_alloc(length, "LAPD I resend"); + msg->l3h = msgb_put(msg, length); + memcpy(msg->l3h, dl->tx_hist[h].msg->data, + length); + dl->send_ph_data_req(&nctx, msg); + } else { + /* OR send appropriate supervision frame with P=1 */ + if (!dl->own_busy && !dl->seq_err_cond) { + lapd_send_rr(&dl->lctx, 1, 1); + /* NOTE: In case of sequence error + * condition, the REJ frame has been + * transmitted when entering the + * condition, so it has not be done + * here + */ + } else if (dl->own_busy) { + lapd_send_rnr(&dl->lctx, 1, 1); + } else { + LOGP(DLLAPD, LOGL_INFO, "unhandled, " + "pls. fix\n"); + } + } + /* restart T200 (PH-READY-TO-SEND) */ + lapd_start_t200(dl); + } else { + /* send MDL ERROR INIDCATION to L3 */ + mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx); + /* reestablish */ + if (!dl->reestablish) + break; + LOGP(DLLAPD, LOGL_NOTICE, "N200 reached, performing " + "reestablishment.\n"); + lapd_reestablish(dl); + } + break; + default: + LOGP(DLLAPD, LOGL_INFO, "T200 expired in unexpected " + "dl->state %d\n", (int) dl->state); + } +} + +/* Timer callback on T203 expiry */ +static void lapd_t203_cb(void *data) +{ + struct lapd_datalink *dl = data; + + LOGP(DLLAPD, LOGL_INFO, "Timeout T203 (%p) state=%d\n", dl, + (int) dl->state); + + if (dl->state != LAPD_STATE_MF_EST) { + LOGP(DLLAPD, LOGL_ERROR, "T203 fired outside MF EST state, " + "please fix!\n"); + return; + } + + /* set retransmission counter to 0 */ + dl->retrans_ctr = 0; + /* enter timer recovery state */ + lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV); + /* transmit a supervisory command with P bit set to 1 as follows: */ + if (!dl->own_busy) { + LOGP(DLLAPD, LOGL_INFO, "transmit an RR poll command\n"); + /* Send RR with P=1 */ + lapd_send_rr(&dl->lctx, 1, 1); + } else { + LOGP(DLLAPD, LOGL_INFO, "transmit an RNR poll command\n"); + /* Send RNR with P=1 */ + lapd_send_rnr(&dl->lctx, 1, 1); + } + /* start T200 */ + lapd_start_t200(dl); +} + +/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */ +static void lapd_acknowledge(struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + uint8_t nr = lctx->n_recv; + int s = 0, rej = 0, t200_reset = 0, t200_start = 0; + int i, h; + + /* supervisory frame ? */ + if (lctx->format == LAPD_FORM_S) + s = 1; + /* REJ frame ? */ + if (s && lctx->s_u == LAPD_S_REJ) + rej = 1; + + /* Flush all transmit buffers of acknowledged frames */ + for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) { + h = do_mod(i, dl->range_hist); + if (dl->tx_hist[h].msg) { + msgb_free(dl->tx_hist[h].msg); + dl->tx_hist[h].msg = NULL; + LOGP(DLLAPD, LOGL_INFO, "ack frame %d\n", i); + } + } + + if (dl->state != LAPD_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), + * or an REJ with an N(R) equal to V(A). */ + if ((!rej && nr != dl->v_ack) + || (rej && nr == dl->v_ack)) { + t200_reset = 1; + lapd_stop_t200(dl); + /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */ + } + /* 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. + */ + if (sub_mod(nr, dl->v_ack, dl->v_range) + > sub_mod(dl->v_send, dl->v_ack, dl->v_range)) { + LOGP(DLLAPD, LOGL_NOTICE, "N(R) sequence error\n"); + mdl_error(MDL_CAUSE_SEQ_ERR, lctx); + } + } + + /* V(A) shall be set to the value of N(R) */ + dl->v_ack = nr; + + /* If T200 has been stopped by the receipt of an I, RR or RNR frame, + * and if there are outstanding I frames, restart T200 */ + if (t200_reset && !rej) { + if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) { + LOGP(DLLAPD, LOGL_INFO, "start T200, due to unacked I " + "frame(s)\n"); + t200_start = 1; + lapd_start_t200(dl); + } + } + + /* This also does a restart, when I or S frame is received */ + + /* Stop T203, if running */ + lapd_stop_t203(dl); + /* Start T203, if T200 is not running in MF EST state, if enabled */ + if (!osmo_timer_pending(&dl->t200) + && (dl->t203_sec || dl->t203_usec) + && (dl->state == LAPD_STATE_MF_EST)) { + lapd_start_t203(dl); + } +} + +/* L1 -> L2 */ + +/* Receive a LAPD U (Unnumbered) message from L1 */ +static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + int length = lctx->length; + int rc; + uint8_t prim, op; + + switch (lctx->s_u) { + case LAPD_U_SABM: + case LAPD_U_SABME: + prim = PRIM_DL_EST; + op = PRIM_OP_INDICATION; + + LOGP(DLLAPD, LOGL_INFO, "SABM(E) received in state %s\n", + lapd_state_names[dl->state]); + /* 5.7.1 */ + dl->seq_err_cond = 0; + /* G.2.2 Wrong value of the C/R bit */ + if (lctx->cr == dl->cr.rem2loc.resp) { + LOGP(DLLAPD, LOGL_NOTICE, "SABM response error\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx); + return -EINVAL; + } + + /* G.4.5 If SABM is received with L>N201 or with M bit + * set, AN MDL-ERROR-INDICATION is sent to MM. + */ + if (lctx->more || length > lctx->n201) { + LOGP(DLLAPD, LOGL_NOTICE, "SABM too large error\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx); + return -EIO; + } + + switch (dl->state) { + case LAPD_STATE_IDLE: + break; + case LAPD_STATE_MF_EST: + LOGP(DLLAPD, LOGL_INFO, "SABM command, multiple " + "frame established state\n"); + /* If link is lost on the remote side, we start over + * and send DL-ESTABLISH indication again. */ + if (dl->v_send != dl->v_recv) { + LOGP(DLLAPD, LOGL_INFO, "Remote reestablish\n"); + mdl_error(MDL_CAUSE_SABM_MF, lctx); + break; + } + /* Ignore SABM if content differs from first SABM. */ + if (dl->mode == LAPD_MODE_NETWORK && length + && dl->cont_res) { +#ifdef TEST_CONTENT_RESOLUTION_NETWORK + dl->cont_res->data[0] ^= 0x01; +#endif + if (memcmp(dl->cont_res, msg->data, length)) { + LOGP(DLLAPD, LOGL_INFO, "Another SABM " + "with diffrent content - " + "ignoring!\n"); + msgb_free(msg); + return 0; + } + } + /* send UA again */ + lapd_send_ua(lctx, length, msg->l3h); + msgb_free(msg); + return 0; + case LAPD_STATE_DISC_SENT: + /* 5.4.6.2 send DM with F=P */ + lapd_send_dm(lctx); + /* stop Timer T200 */ + lapd_stop_t200(dl); + msgb_free(msg); + return send_dl_simple(prim, op, lctx); + default: + /* collision: Send UA, but still wait for rx UA, then + * change to MF_EST state. + */ + /* check for contention resoultion */ + if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) { + LOGP(DLLAPD, LOGL_NOTICE, "SABM not allowed " + "during contention resolution\n"); + mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx); + } + lapd_send_ua(lctx, length, msg->l3h); + msgb_free(msg); + return 0; + } + /* save message context for further use */ + memcpy(&dl->lctx, lctx, sizeof(dl->lctx)); +#ifndef TEST_CONTENT_RESOLUTION_NETWORK + /* send UA response */ + lapd_send_ua(lctx, length, msg->l3h); +#endif + /* set Vs, Vr and Va to 0 */ + dl->v_send = dl->v_recv = dl->v_ack = 0; + /* clear tx_hist */ + lapd_dl_flush_hist(dl); + /* enter multiple-frame-established state */ + lapd_dl_newstate(dl, LAPD_STATE_MF_EST); + /* store content resolution data on network side + * Note: cont_res will be removed when changing state again, + * so it must be allocated AFTER lapd_dl_newstate(). */ + if (dl->mode == LAPD_MODE_NETWORK && length) { + dl->cont_res = lapd_msgb_alloc(length, "CONT RES"); + memcpy(msgb_put(dl->cont_res, length), msg->l3h, + length); + LOGP(DLLAPD, LOGL_NOTICE, "Store content res.\n"); + } + /* send notification to L3 */ + if (length == 0) { + /* 5.4.1.2 Normal establishment procedures */ + rc = send_dl_simple(prim, op, lctx); + msgb_free(msg); + } else { + /* 5.4.1.4 Contention resolution establishment */ + rc = send_dl_l3(prim, op, lctx, msg); + } + break; + case LAPD_U_DM: + LOGP(DLLAPD, LOGL_INFO, "DM received in state %s\n", + lapd_state_names[dl->state]); + /* G.2.2 Wrong value of the C/R bit */ + if (lctx->cr == dl->cr.rem2loc.cmd) { + LOGP(DLLAPD, LOGL_NOTICE, "DM command error\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx); + return -EINVAL; + } + if (!lctx->p_f) { + /* 5.4.1.2 DM responses with the F bit set to "0" + * shall be ignored. + */ + msgb_free(msg); + return 0; + } + switch (dl->state) { + case LAPD_STATE_SABM_SENT: + break; + case LAPD_STATE_MF_EST: + if (lctx->p_f) { + LOGP(DLLAPD, LOGL_INFO, "unsolicited DM " + "response\n"); + mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx); + } else { + LOGP(DLLAPD, LOGL_INFO, "unsolicited DM " + "response, multiple frame established " + "state\n"); + mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx); + /* reestablish */ + if (!dl->reestablish) { + msgb_free(msg); + return 0; + } + LOGP(DLLAPD, LOGL_NOTICE, "Performing " + "reestablishment.\n"); + lapd_reestablish(dl); + } + msgb_free(msg); + return 0; + case LAPD_STATE_TIMER_RECOV: + /* FP = 0 (DM is normal in case PF = 1) */ + if (!lctx->p_f) { + LOGP(DLLAPD, LOGL_INFO, "unsolicited DM " + "response, multiple frame established " + "state\n"); + mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx); + msgb_free(msg); + /* reestablish */ + if (!dl->reestablish) + return 0; + LOGP(DLLAPD, LOGL_NOTICE, "Performing " + "reestablishment.\n"); + return lapd_reestablish(dl); + } + break; + case LAPD_STATE_DISC_SENT: + /* stop Timer T200 */ + lapd_stop_t200(dl); + /* go to idle state */ + lapd_dl_flush_tx(dl); + lapd_dl_flush_send(dl); + lapd_dl_newstate(dl, LAPD_STATE_IDLE); + rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx); + msgb_free(msg); + return 0; + case LAPD_STATE_IDLE: + /* 5.4.5 all other frame types shall be discarded */ + default: + LOGP(DLLAPD, LOGL_INFO, "unsolicited DM response! " + "(discarding)\n"); + msgb_free(msg); + return 0; + } + /* stop timer T200 */ + lapd_stop_t200(dl); + /* go to idle state */ + lapd_dl_newstate(dl, LAPD_STATE_IDLE); + rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx); + msgb_free(msg); + break; + case LAPD_U_UI: + LOGP(DLLAPD, LOGL_INFO, "UI received\n"); + /* G.2.2 Wrong value of the C/R bit */ + if (lctx->cr == dl->cr.rem2loc.resp) { + LOGP(DLLAPD, LOGL_NOTICE, "UI indicates response " + "error\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx); + return -EINVAL; + } + + /* G.4.5 If UI is received with L>N201 or with M bit + * set, AN MDL-ERROR-INDICATION is sent to MM. + */ + if (length > lctx->n201 || lctx->more) { + LOGP(DLLAPD, LOGL_NOTICE, "UI too large error " + "(%d > N201(%d) or M=%d)\n", length, + lctx->n201, lctx->more); + msgb_free(msg); + mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx); + return -EIO; + } + + /* do some length checks */ + if (length == 0) { + /* 5.3.3 UI frames received with the length indicator + * set to "0" shall be ignored + */ + LOGP(DLLAPD, LOGL_INFO, "length=0 (discarding)\n"); + msgb_free(msg); + return 0; + } + rc = send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx, + msg); + break; + case LAPD_U_DISC: + prim = PRIM_DL_REL; + op = PRIM_OP_INDICATION; + + LOGP(DLLAPD, LOGL_INFO, "DISC received in state %s\n", + lapd_state_names[dl->state]); + /* flush tx and send buffers */ + lapd_dl_flush_tx(dl); + lapd_dl_flush_send(dl); + /* 5.7.1 */ + dl->seq_err_cond = 0; + /* G.2.2 Wrong value of the C/R bit */ + if (lctx->cr == dl->cr.rem2loc.resp) { + LOGP(DLLAPD, LOGL_NOTICE, "DISC response error\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx); + return -EINVAL; + } + if (length > 0 || lctx->more) { + /* 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. + */ + LOGP(DLLAPD, LOGL_NOTICE, + "U frame iwth incorrect parameters "); + msgb_free(msg); + mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx); + return -EIO; + } + switch (dl->state) { + case LAPD_STATE_IDLE: + LOGP(DLLAPD, LOGL_INFO, "DISC in idle state\n"); + /* send DM with F=P */ + msgb_free(msg); + return lapd_send_dm(lctx); + case LAPD_STATE_SABM_SENT: + LOGP(DLLAPD, LOGL_INFO, "DISC in SABM state\n"); + /* 5.4.6.2 send DM with F=P */ + lapd_send_dm(lctx); + /* stop Timer T200 */ + lapd_stop_t200(dl); + /* go to idle state */ + lapd_dl_newstate(dl, LAPD_STATE_IDLE); + msgb_free(msg); + return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, + lctx); + case LAPD_STATE_MF_EST: + case LAPD_STATE_TIMER_RECOV: + LOGP(DLLAPD, LOGL_INFO, "DISC in est state\n"); + break; + case LAPD_STATE_DISC_SENT: + LOGP(DLLAPD, LOGL_INFO, "DISC in disc state\n"); + prim = PRIM_DL_REL; + op = PRIM_OP_CONFIRM; + break; + default: + lapd_send_ua(lctx, length, msg->l3h); + msgb_free(msg); + return 0; + } + /* send UA response */ + lapd_send_ua(lctx, length, msg->l3h); + /* stop Timer T200 */ + lapd_stop_t200(dl); + /* enter idle state, keep tx-buffer with UA response */ + lapd_dl_newstate(dl, LAPD_STATE_IDLE); + /* send notification to L3 */ + rc = send_dl_simple(prim, op, lctx); + msgb_free(msg); + break; + case LAPD_U_UA: + LOGP(DLLAPD, LOGL_INFO, "UA received in state %s\n", + lapd_state_names[dl->state]); + /* G.2.2 Wrong value of the C/R bit */ + if (lctx->cr == dl->cr.rem2loc.cmd) { + LOGP(DLLAPD, LOGL_NOTICE, "UA indicates command " + "error\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx); + return -EINVAL; + } + + /* G.4.5 If UA is received with L>N201 or with M bit + * set, AN MDL-ERROR-INDICATION is sent to MM. + */ + if (lctx->more || length > lctx->n201) { + LOGP(DLLAPD, LOGL_NOTICE, "UA too large error\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx); + return -EIO; + } + + if (!lctx->p_f) { + /* 5.4.1.2 A UA response with the F bit set to "0" + * shall be ignored. + */ + LOGP(DLLAPD, LOGL_INFO, "F=0 (discarding)\n"); + msgb_free(msg); + return 0; + } + switch (dl->state) { + case LAPD_STATE_SABM_SENT: + break; + case LAPD_STATE_MF_EST: + case LAPD_STATE_TIMER_RECOV: + LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! " + "(discarding)\n"); + mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx); + msgb_free(msg); + return 0; + case LAPD_STATE_DISC_SENT: + LOGP(DLLAPD, LOGL_INFO, "UA in disconnect state\n"); + /* stop Timer T200 */ + lapd_stop_t200(dl); + /* go to idle state */ + lapd_dl_flush_tx(dl); + lapd_dl_flush_send(dl); + lapd_dl_newstate(dl, LAPD_STATE_IDLE); + rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx); + msgb_free(msg); + return 0; + case LAPD_STATE_IDLE: + /* 5.4.5 all other frame types shall be discarded */ + default: + LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! " + "(discarding)\n"); + msgb_free(msg); + return 0; + } + LOGP(DLLAPD, LOGL_INFO, "UA in SABM state\n"); + /* stop Timer T200 */ + lapd_stop_t200(dl); + /* compare UA with SABME if contention resolution is applied */ + if (dl->tx_hist[0].msg->len) { + if (length != (dl->tx_hist[0].msg->len) + || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h, + length)) { + LOGP(DLLAPD, LOGL_INFO, "**** UA response " + "mismatches ****\n"); + rc = send_dl_simple(PRIM_DL_REL, + PRIM_OP_INDICATION, lctx); + msgb_free(msg); + /* go to idle state */ + lapd_dl_flush_tx(dl); + lapd_dl_flush_send(dl); + lapd_dl_newstate(dl, LAPD_STATE_IDLE); + return 0; + } + } + /* set Vs, Vr and Va to 0 */ + dl->v_send = dl->v_recv = dl->v_ack = 0; + /* clear tx_hist */ + lapd_dl_flush_hist(dl); + /* enter multiple-frame-established state */ + lapd_dl_newstate(dl, LAPD_STATE_MF_EST); + /* send outstanding frames, if any (resume / reconnect) */ + lapd_send_i(lctx, __LINE__); + /* send notification to L3 */ + rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx); + msgb_free(msg); + break; + case LAPD_U_FRMR: + LOGP(DLLAPD, LOGL_NOTICE, "Frame reject received\n"); + /* send MDL ERROR INIDCATION to L3 */ + mdl_error(MDL_CAUSE_FRMR, lctx); + msgb_free(msg); + /* reestablish */ + if (!dl->reestablish) + break; + LOGP(DLLAPD, LOGL_NOTICE, "Performing reestablishment.\n"); + rc = lapd_reestablish(dl); + break; + default: + /* G.3.1 */ + LOGP(DLLAPD, LOGL_NOTICE, "Unnumbered frame not allowed.\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx); + return -EINVAL; + } + return rc; +} + +/* Receive a LAPD S (Supervisory) message from L1 */ +static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + int length = lctx->length; + + if (length > 0 || lctx->more) { + /* 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. */ + LOGP(DLLAPD, LOGL_NOTICE, + "S frame with incorrect parameters\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx); + return -EIO; + } + + if (lctx->cr == dl->cr.rem2loc.resp + && lctx->p_f + && dl->state != LAPD_STATE_TIMER_RECOV) { + /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */ + LOGP(DLLAPD, LOGL_NOTICE, "S frame response with F=1 error\n"); + mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx); + } + + switch (dl->state) { + case LAPD_STATE_IDLE: + /* if P=1, respond DM with F=1 (5.2.2) */ + /* 5.4.5 all other frame types shall be discarded */ + if (lctx->p_f) + lapd_send_dm(lctx); /* F=P */ + /* fall though */ + case LAPD_STATE_SABM_SENT: + case LAPD_STATE_DISC_SENT: + LOGP(DLLAPD, LOGL_NOTICE, "S frame ignored in this state\n"); + msgb_free(msg); + return 0; + } + switch (lctx->s_u) { + case LAPD_S_RR: + LOGP(DLLAPD, LOGL_INFO, "RR received in state %s\n", + lapd_state_names[dl->state]); + /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */ + lapd_acknowledge(lctx); + + /* 5.5.3.2 */ + if (lctx->cr == dl->cr.rem2loc.cmd + && lctx->p_f) { + if (!dl->own_busy && !dl->seq_err_cond) { + LOGP(DLLAPD, LOGL_INFO, "RR frame command " + "with polling bit set and we are not " + "busy, so we reply with RR frame " + "response\n"); + lapd_send_rr(lctx, 1, 0); + /* NOTE: In case of sequence error condition, + * the REJ frame has been transmitted when + * entering the condition, so it has not be + * done here + */ + } else if (dl->own_busy) { + LOGP(DLLAPD, LOGL_INFO, "RR frame command " + "with polling bit set and we are busy, " + "so we reply with RR frame response\n"); + lapd_send_rnr(lctx, 1, 0); + } + } else if (lctx->cr == dl->cr.rem2loc.resp + && lctx->p_f + && dl->state == LAPD_STATE_TIMER_RECOV) { + LOGP(DLLAPD, LOGL_INFO, "RR response with F==1, " + "and we are in timer recovery state, so " + "we leave that state\n"); + /* V(S) to the N(R) in the RR frame */ + dl->v_send = lctx->n_recv; + /* stop Timer T200 */ + lapd_stop_t200(dl); + /* 5.5.7 Clear timer recovery condition */ + lapd_dl_newstate(dl, LAPD_STATE_MF_EST); + } + /* Send message, if possible due to acknowledged data */ + lapd_send_i(lctx, __LINE__); + + break; + case LAPD_S_RNR: + LOGP(DLLAPD, LOGL_INFO, "RNR received in state %s\n", + lapd_state_names[dl->state]); + /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */ + lapd_acknowledge(lctx); + + /* 5.5.5 */ + /* Set peer receiver busy condition */ + dl->peer_busy = 1; + + if (lctx->p_f) { + if (lctx->cr == dl->cr.rem2loc.cmd) { + if (!dl->own_busy) { + LOGP(DLLAPD, LOGL_INFO, "RNR poll " + "command and we are not busy, " + "so we reply with RR final " + "response\n"); + /* Send RR with F=1 */ + lapd_send_rr(lctx, 1, 0); + } else { + LOGP(DLLAPD, LOGL_INFO, "RNR poll " + "command and we are busy, so " + "we reply with RNR final " + "response\n"); + /* Send RNR with F=1 */ + lapd_send_rnr(lctx, 1, 0); + } + } else if (dl->state == LAPD_STATE_TIMER_RECOV) { + LOGP(DLLAPD, LOGL_INFO, "RNR poll response " + "and we in timer recovery state, so " + "we leave that state\n"); + /* 5.5.7 Clear timer recovery condition */ + lapd_dl_newstate(dl, LAPD_STATE_MF_EST); + /* V(S) to the N(R) in the RNR frame */ + dl->v_send = lctx->n_recv; + } + } else + LOGP(DLLAPD, LOGL_INFO, "RNR not polling/final state " + "received\n"); + + /* Send message, if possible due to acknowledged data */ + lapd_send_i(lctx, __LINE__); + + break; + case LAPD_S_REJ: + LOGP(DLLAPD, LOGL_INFO, "REJ received in state %s\n", + lapd_state_names[dl->state]); + /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */ + lapd_acknowledge(lctx); + + /* 5.5.4.1 */ + if (dl->state != LAPD_STATE_TIMER_RECOV) { + /* Clear an existing peer receiver busy condition */ + dl->peer_busy = 0; + /* V(S) and V(A) to the N(R) in the REJ frame */ + dl->v_send = dl->v_ack = lctx->n_recv; + /* stop Timer T200 */ + lapd_stop_t200(dl); + /* 5.5.3.2 */ + if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) { + if (!dl->own_busy && !dl->seq_err_cond) { + LOGP(DLLAPD, LOGL_INFO, "REJ poll " + "command not in timer recovery " + "state and not in own busy " + "condition received, so we " + "respond with RR final " + "response\n"); + lapd_send_rr(lctx, 1, 0); + /* NOTE: In case of sequence error + * condition, the REJ frame has been + * transmitted when entering the + * condition, so it has not be done + * here + */ + } else if (dl->own_busy) { + LOGP(DLLAPD, LOGL_INFO, "REJ poll " + "command not in timer recovery " + "state and in own busy " + "condition received, so we " + "respond with RNR final " + "response\n"); + lapd_send_rnr(lctx, 1, 0); + } + } else + LOGP(DLLAPD, LOGL_INFO, "REJ response or not " + "polling command not in timer recovery " + "state received\n"); + /* send MDL ERROR INIDCATION to L3 */ + if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) { + mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx); + } + + } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) { + LOGP(DLLAPD, LOGL_INFO, "REJ poll response in timer " + "recovery state received\n"); + /* Clear an existing peer receiver busy condition */ + dl->peer_busy = 0; + /* V(S) and V(A) to the N(R) in the REJ frame */ + dl->v_send = dl->v_ack = lctx->n_recv; + /* stop Timer T200 */ + lapd_stop_t200(dl); + /* 5.5.7 Clear timer recovery condition */ + lapd_dl_newstate(dl, LAPD_STATE_MF_EST); + } else { + /* Clear an existing peer receiver busy condition */ + dl->peer_busy = 0; + /* V(S) and V(A) to the N(R) in the REJ frame */ + dl->v_send = dl->v_ack = lctx->n_recv; + /* 5.5.3.2 */ + if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) { + if (!dl->own_busy && !dl->seq_err_cond) { + LOGP(DLLAPD, LOGL_INFO, "REJ poll " + "command in timer recovery " + "state and not in own busy " + "condition received, so we " + "respond with RR final " + "response\n"); + lapd_send_rr(lctx, 1, 0); + /* NOTE: In case of sequence error + * condition, the REJ frame has been + * transmitted when entering the + * condition, so it has not be done + * here + */ + } else if (dl->own_busy) { + LOGP(DLLAPD, LOGL_INFO, "REJ poll " + "command in timer recovery " + "state and in own busy " + "condition received, so we " + "respond with RNR final " + "response\n"); + lapd_send_rnr(lctx, 1, 0); + } + } else + LOGP(DLLAPD, LOGL_INFO, "REJ response or not " + "polling command in timer recovery " + "state received\n"); + } + + /* FIXME: 5.5.4.2 2) */ + + /* Send message, if possible due to acknowledged data */ + lapd_send_i(lctx, __LINE__); + + break; + default: + /* G.3.1 */ + LOGP(DLLAPD, LOGL_NOTICE, "Supervisory frame not allowed.\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx); + return -EINVAL; + } + msgb_free(msg); + return 0; +} + +/* Receive a LAPD I (Information) message from L1 */ +static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + //uint8_t nr = lctx->n_recv; + uint8_t ns = lctx->n_send; + int length = lctx->length; + int rc; + + LOGP(DLLAPD, LOGL_INFO, "I received in state %s\n", + lapd_state_names[dl->state]); + + /* G.2.2 Wrong value of the C/R bit */ + if (lctx->cr == dl->cr.rem2loc.resp) { + LOGP(DLLAPD, LOGL_NOTICE, "I frame response not allowed\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx); + return -EINVAL; + } + + if (length == 0 || length > lctx->n201) { + /* 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. */ + LOGP(DLLAPD, LOGL_NOTICE, "I frame length not allowed\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx); + return -EIO; + } + + /* G.4.2 If the numerical value of L is L<N201 and the M + * bit is set to "1", then an MDL-ERROR-INDICATION primitive with + * cause "I frame with incorrect use of M bit" is sent to the + * mobile management entity. */ + if (lctx->more && length < lctx->n201) { + LOGP(DLLAPD, LOGL_NOTICE, "I frame with M bit too short\n"); + msgb_free(msg); + mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx); + return -EIO; + } + + switch (dl->state) { + case LAPD_STATE_IDLE: + /* if P=1, respond DM with F=1 (5.2.2) */ + /* 5.4.5 all other frame types shall be discarded */ + if (lctx->p_f) + lapd_send_dm(lctx); /* F=P */ + /* fall though */ + case LAPD_STATE_SABM_SENT: + case LAPD_STATE_DISC_SENT: + LOGP(DLLAPD, LOGL_NOTICE, "I frame ignored in this state\n"); + msgb_free(msg); + return 0; + } + + /* 5.7.1: N(s) sequence error */ + if (ns != dl->v_recv) { + LOGP(DLLAPD, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, " + "V(R)=%u\n", ns, dl->v_recv); + /* discard data */ + msgb_free(msg); + if (!dl->seq_err_cond) { + /* FIXME: help me understand what exactly todo here + dl->seq_err_cond = 1; + */ + lapd_send_rej(lctx, lctx->p_f); + } else { + } + return -EIO; + } + dl->seq_err_cond = 0; + + /* Increment receiver state */ + dl->v_recv = inc_mod(dl->v_recv, dl->v_range); + LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u\n", dl->v_recv); + + /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */ + lapd_acknowledge(lctx); /* V(A) is also set here */ + + /* Only if we are not in own receiver busy condition */ + if (!dl->own_busy) { + /* if the frame carries a complete segment */ + if (!lctx->more && !dl->rcv_buffer) { + LOGP(DLLAPD, LOGL_INFO, "message in single I frame\n"); + /* send a DATA INDICATION to L3 */ + msg->len = length; + msg->tail = msg->data + length; + rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx, + msg); + } else { + /* create rcv_buffer */ + if (!dl->rcv_buffer) { + LOGP(DLLAPD, LOGL_INFO, "message in multiple " + "I frames (first message)\n"); + dl->rcv_buffer = lapd_msgb_alloc(dl->maxf, + "LAPD RX"); + dl->rcv_buffer->l3h = dl->rcv_buffer->data; + } + /* concat. rcv_buffer */ + if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) { + LOGP(DLLAPD, LOGL_NOTICE, "Received frame " + "overflow!\n"); + } else { + memcpy(msgb_put(dl->rcv_buffer, length), + msg->l3h, length); + } + /* if the last segment was received */ + if (!lctx->more) { + LOGP(DLLAPD, LOGL_INFO, "message in multiple " + "I frames (last message)\n"); + rc = send_dl_l3(PRIM_DL_DATA, + PRIM_OP_INDICATION, lctx, + dl->rcv_buffer); + dl->rcv_buffer = NULL; + } else + LOGP(DLLAPD, LOGL_INFO, "message in multiple " + "I frames (next message)\n"); + msgb_free(msg); + + } + } else + LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver " + "busy condition\n"); + + /* Check for P bit */ + if (lctx->p_f) { + /* 5.5.2.1 */ + /* check if we are not in own receiver busy */ + if (!dl->own_busy) { + LOGP(DLLAPD, LOGL_INFO, "we are not busy, send RR\n"); + /* Send RR with F=1 */ + rc = lapd_send_rr(lctx, 1, 0); + } else { + LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n"); + /* Send RNR with F=1 */ + rc = lapd_send_rnr(lctx, 1, 0); + } + } else { + /* 5.5.2.2 */ + /* check if we are not in own receiver busy */ + if (!dl->own_busy) { + /* NOTE: V(R) is already set above */ + rc = lapd_send_i(lctx, __LINE__); + if (rc) { + LOGP(DLLAPD, LOGL_INFO, "we are not busy and " + "have no pending data, send RR\n"); + /* Send RR with F=0 */ + return lapd_send_rr(lctx, 0, 0); + } + /* all I or one RR is sent, we are done */ + return 0; + } else { + LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n"); + /* Send RNR with F=0 */ + rc = lapd_send_rnr(lctx, 0, 0); + } + } + + /* Send message, if possible due to acknowledged data */ + lapd_send_i(lctx, __LINE__); + + return rc; +} + +/* Receive a LAPD message from L1 */ +int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx) +{ + int rc; + + switch (lctx->format) { + case LAPD_FORM_U: + rc = lapd_rx_u(msg, lctx); + break; + case LAPD_FORM_S: + rc = lapd_rx_s(msg, lctx); + break; + case LAPD_FORM_I: + rc = lapd_rx_i(msg, lctx); + break; + default: + LOGP(DLLAPD, LOGL_NOTICE, "unknown LAPD format\n"); + msgb_free(msg); + rc = -EINVAL; + } + return rc; +} + +/* L3 -> L2 */ + +/* send unit data */ +static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + struct msgb *msg = dp->oph.msg; + struct lapd_msg_ctx nctx; + + memcpy(&nctx, lctx, sizeof(nctx)); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = dl->cr.loc2rem.cmd; + nctx.format = LAPD_FORM_U; + nctx.s_u = LAPD_U_UI; + /* keep nctx.p_f */ + nctx.length = msg->len; + nctx.more = 0; + + return dl->send_ph_data_req(&nctx, msg); +} + +/* request link establishment */ +static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + struct msgb *msg = dp->oph.msg; + struct lapd_msg_ctx nctx; + + if (msg->len) + LOGP(DLLAPD, LOGL_INFO, "perform establishment with content " + "(SABM)\n"); + else + LOGP(DLLAPD, LOGL_INFO, "perform normal establishm. (SABM)\n"); + + /* Flush send-queue */ + /* Clear send-buffer */ + lapd_dl_flush_send(dl); + /* be sure that history is empty */ + lapd_dl_flush_hist(dl); + + /* save message context for further use */ + memcpy(&dl->lctx, lctx, sizeof(dl->lctx)); + + /* Discard partly received L3 message */ + if (dl->rcv_buffer) { + msgb_free(dl->rcv_buffer); + dl->rcv_buffer = NULL; + } + + /* assemble message */ + memcpy(&nctx, &dl->lctx, sizeof(nctx)); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = dl->cr.loc2rem.cmd; + nctx.format = LAPD_FORM_U; + nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM; + nctx.p_f = 1; + nctx.length = msg->len; + nctx.more = 0; + + /* Transmit-buffer carries exactly one segment */ + dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST"); + msgb_put(dl->tx_hist[0].msg, msg->len); + if (msg->len) + memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len); + dl->tx_hist[0].more = 0; + /* set Vs to 0, because it is used as index when resending SABM */ + dl->v_send = 0; + + /* Set states */ + dl->own_busy = dl->peer_busy = 0; + dl->retrans_ctr = 0; + lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT); + + /* Tramsmit and start T200 */ + dl->send_ph_data_req(&nctx, msg); + lapd_start_t200(dl); + + return 0; +} + +/* send data */ +static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + struct msgb *msg = dp->oph.msg; + + LOGP(DLLAPD, LOGL_INFO, "writing message to send-queue\n"); + + /* Write data into the send queue */ + msgb_enqueue(&dl->send_queue, msg); + + /* Send message, if possible */ + lapd_send_i(&dl->lctx, __LINE__); + + return 0; +} + +/* Send next I frame from queued/buffered data */ +static int lapd_send_i(struct lapd_msg_ctx *lctx, int line) +{ + struct lapd_datalink *dl = lctx->dl; + uint8_t k = dl->k; + uint8_t h; + struct msgb *msg; + int length, left; + int rc = - 1; /* we sent nothing */ + struct lapd_msg_ctx nctx; + + + LOGP(DLLAPD, LOGL_INFO, "%s() called from line %d\n", __func__, line); + + next_frame: + + if (dl->peer_busy) { + LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending\n"); + return rc; + } + + if (dl->state == LAPD_STATE_TIMER_RECOV) { + LOGP(DLLAPD, LOGL_INFO, "timer recovery, not sending\n"); + return rc; + } + + /* 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. */ + if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) { + LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending " + "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->v_send, + dl->v_ack); + return rc; + } + + h = do_mod(dl->v_send, dl->range_hist); + + /* if we have no tx_hist yet, we create it */ + if (!dl->tx_hist[h].msg) { + /* Get next message into send-buffer, if any */ + if (!dl->send_buffer) { + next_message: + dl->send_out = 0; + dl->send_buffer = msgb_dequeue(&dl->send_queue); + /* No more data to be sent */ + if (!dl->send_buffer) + return rc; + LOGP(DLLAPD, LOGL_INFO, "get message from " + "send-queue\n"); + } + + /* How much is left in the send-buffer? */ + left = msgb_l3len(dl->send_buffer) - dl->send_out; + /* Segment, if data exceeds N201 */ + length = left; + if (length > lctx->n201) + length = lctx->n201; + LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d " + "length %d first byte %02x\n", + msgb_l3len(dl->send_buffer), dl->send_out, left, + lctx->n201, length, dl->send_buffer->l3h[0]); + /* If message in send-buffer is completely sent */ + if (left == 0) { + msgb_free(dl->send_buffer); + dl->send_buffer = NULL; + goto next_message; + } + + LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d\n", + (left > length) ? "segment " : "", dl->v_send); + + /* Create I frame (segment) and transmit-buffer content */ + msg = lapd_msgb_alloc(length, "LAPD I"); + msg->l3h = msgb_put(msg, length); + /* assemble message */ + memcpy(&nctx, &dl->lctx, sizeof(nctx)); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = dl->cr.loc2rem.cmd; + nctx.format = LAPD_FORM_I; + nctx.p_f = 0; + nctx.n_send = dl->v_send; + nctx.n_recv = dl->v_recv; + nctx.length = length; + if (left > length) + nctx.more = 1; + else + nctx.more = 0; + if (length) + memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out, + length); + /* store in tx_hist */ + dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST"); + msgb_put(dl->tx_hist[h].msg, msg->len); + if (length) + memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len); + dl->tx_hist[h].more = nctx.more; + /* Add length to track how much is already in the tx buffer */ + dl->send_out += length; + } else { + LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer " + "V(S)=%d\n", dl->v_send); + + /* Create I frame (segment) from tx_hist */ + length = dl->tx_hist[h].msg->len; + msg = lapd_msgb_alloc(length, "LAPD I resend"); + msg->l3h = msgb_put(msg, length); + /* assemble message */ + memcpy(&nctx, &dl->lctx, sizeof(nctx)); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = dl->cr.loc2rem.cmd; + nctx.format = LAPD_FORM_I; + nctx.p_f = 0; + nctx.n_send = dl->v_send; + nctx.n_recv = dl->v_recv; + nctx.length = length; + nctx.more = dl->tx_hist[h].more; + if (length) + memcpy(msg->l3h, dl->tx_hist[h].msg->data, length); + } + + /* 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_mod(dl->v_send, dl->v_range); + + /* 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 (!osmo_timer_pending(&dl->t200)) { + /* stop Timer T203, if running */ + lapd_stop_t203(dl); + /* start Timer T200 */ + lapd_start_t200(dl); + } + + dl->send_ph_data_req(&nctx, msg); + + rc = 0; /* we sent something */ + goto next_frame; +} + +/* request link suspension */ +static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + struct msgb *msg = dp->oph.msg; + + LOGP(DLLAPD, LOGL_INFO, "perform suspension\n"); + + /* put back the send-buffer to the send-queue (first position) */ + if (dl->send_buffer) { + LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to " + "queue\n"); + llist_add(&dl->send_buffer->list, &dl->send_queue); + dl->send_buffer = NULL; + } else + LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer\n"); + + /* Clear transmit buffer, but keep send buffer */ + lapd_dl_flush_tx(dl); + /* Stop timers (there is no state change, so we must stop all timers */ + lapd_stop_t200(dl); + lapd_stop_t203(dl); + + msgb_free(msg); + + return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx); +} + +/* requesst resume or reconnect of link */ +static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + struct msgb *msg = dp->oph.msg; + struct lapd_msg_ctx nctx; + + LOGP(DLLAPD, LOGL_INFO, "perform re-establishment (SABM) length=%d\n", + msg->len); + + /* be sure that history is empty */ + lapd_dl_flush_hist(dl); + + /* save message context for further use */ + memcpy(&dl->lctx, lctx, sizeof(dl->lctx)); + + /* Replace message in the send-buffer (reconnect) */ + if (dl->send_buffer) + msgb_free(dl->send_buffer); + dl->send_out = 0; + if (msg && msg->len) { + /* Write data into the send buffer, to be sent first */ + dl->send_buffer = msg; + } + + /* Discard partly received L3 message */ + if (dl->rcv_buffer) { + msgb_free(dl->rcv_buffer); + dl->rcv_buffer = NULL; + } + + /* Create new msgb (old one is now free) */ + msg = lapd_msgb_alloc(0, "LAPD SABM"); + msg->l3h = msg->data; + /* assemble message */ + memcpy(&nctx, &dl->lctx, sizeof(nctx)); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = dl->cr.loc2rem.cmd; + nctx.format = LAPD_FORM_U; + nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM; + nctx.p_f = 1; + nctx.length = 0; + nctx.more = 0; + + dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST"); + msgb_put(dl->tx_hist[0].msg, msg->len); + if (msg->len) + memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len); + dl->tx_hist[0].more = 0; + /* set Vs to 0, because it is used as index when resending SABM */ + dl->v_send = 0; + + /* Set states */ + dl->own_busy = dl->peer_busy = 0; + dl->retrans_ctr = 0; + lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT); + + /* Tramsmit and start T200 */ + dl->send_ph_data_req(&nctx, msg); + lapd_start_t200(dl); + + return 0; +} + +/* requesst release of link */ +static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + struct msgb *msg = dp->oph.msg; + struct lapd_msg_ctx nctx; + + /* local release */ + if (dp->u.rel_req.mode) { + LOGP(DLLAPD, LOGL_INFO, "perform local release\n"); + msgb_free(msg); + /* stop Timer T200 */ + lapd_stop_t200(dl); + /* enter idle state, T203 is stopped here, if running */ + lapd_dl_newstate(dl, LAPD_STATE_IDLE); + /* flush buffers */ + lapd_dl_flush_tx(dl); + lapd_dl_flush_send(dl); + /* send notification to L3 */ + return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx); + } + + /* in case we are already disconnecting */ + if (dl->state == LAPD_STATE_DISC_SENT) + return -EBUSY; + + /* flush tx_hist */ + lapd_dl_flush_hist(dl); + + LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC)\n"); + + /* Push LAPD header on msgb */ + /* assemble message */ + memcpy(&nctx, &dl->lctx, sizeof(nctx)); + /* keep nctx.ldp */ + /* keep nctx.sapi */ + /* keep nctx.tei */ + nctx.cr = dl->cr.loc2rem.cmd; + nctx.format = LAPD_FORM_U; + nctx.s_u = LAPD_U_DISC; + nctx.p_f = 1; + nctx.length = 0; + nctx.more = 0; + + dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST"); + msgb_put(dl->tx_hist[0].msg, msg->len); + if (msg->len) + memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len); + dl->tx_hist[0].more = 0; + /* set Vs to 0, because it is used as index when resending DISC */ + dl->v_send = 0; + + /* Set states */ + dl->own_busy = dl->peer_busy = 0; + dl->retrans_ctr = 0; + lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT); + + /* Tramsmit and start T200 */ + dl->send_ph_data_req(&nctx, msg); + lapd_start_t200(dl); + + return 0; +} + +/* request release of link in idle state */ +static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp, + struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + struct msgb *msg = dp->oph.msg; + + msgb_free(msg); + + /* send notification to L3 */ + return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx); +} + +/* statefull handling for DL SAP messages from L3 */ +static struct l2downstate { + uint32_t states; + int prim, op; + const char *name; + int (*rout) (struct osmo_dlsap_prim *dp, + struct lapd_msg_ctx *lctx); +} l2downstatelist[] = { + /* create and send UI command */ + {ALL_STATES, + PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST, + "DL-UNIT-DATA-REQUEST", lapd_udata_req}, + + /* create and send SABM command */ + {SBIT(LAPD_STATE_IDLE), + PRIM_DL_EST, PRIM_OP_REQUEST, + "DL-ESTABLISH-REQUEST", lapd_est_req}, + + /* create and send I command */ + {SBIT(LAPD_STATE_MF_EST) | + SBIT(LAPD_STATE_TIMER_RECOV), + PRIM_DL_DATA, PRIM_OP_REQUEST, + "DL-DATA-REQUEST", lapd_data_req}, + + /* suspend datalink */ + {SBIT(LAPD_STATE_MF_EST) | + SBIT(LAPD_STATE_TIMER_RECOV), + PRIM_DL_SUSP, PRIM_OP_REQUEST, + "DL-SUSPEND-REQUEST", lapd_susp_req}, + + /* create and send SABM command (resume) */ + {SBIT(LAPD_STATE_MF_EST) | + SBIT(LAPD_STATE_TIMER_RECOV), + PRIM_DL_RES, PRIM_OP_REQUEST, + "DL-RESUME-REQUEST", lapd_res_req}, + + /* create and send SABM command (reconnect) */ + {SBIT(LAPD_STATE_IDLE) | + SBIT(LAPD_STATE_MF_EST) | + SBIT(LAPD_STATE_TIMER_RECOV), + PRIM_DL_RECON, PRIM_OP_REQUEST, + "DL-RECONNECT-REQUEST", lapd_res_req}, + + /* create and send DISC command */ + {SBIT(LAPD_STATE_SABM_SENT) | + SBIT(LAPD_STATE_MF_EST) | + SBIT(LAPD_STATE_TIMER_RECOV) | + SBIT(LAPD_STATE_DISC_SENT), + PRIM_DL_REL, PRIM_OP_REQUEST, + "DL-RELEASE-REQUEST", lapd_rel_req}, + + /* release in idle state */ + {SBIT(LAPD_STATE_IDLE), + PRIM_DL_REL, PRIM_OP_REQUEST, + "DL-RELEASE-REQUEST", lapd_rel_req_idle}, +}; + +#define L2DOWNSLLEN \ + (sizeof(l2downstatelist) / sizeof(struct l2downstate)) + +int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) +{ + struct lapd_datalink *dl = lctx->dl; + int i, supported = 0; + struct msgb *msg = dp->oph.msg; + int rc; + + /* find function for current state and message */ + for (i = 0; i < L2DOWNSLLEN; i++) { + if (dp->oph.primitive == l2downstatelist[i].prim + && dp->oph.operation == l2downstatelist[i].op) { + supported = 1; + if ((SBIT(dl->state) & l2downstatelist[i].states)) + break; + } + } + if (!supported) { + LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unsupported.\n", + dp->oph.primitive, dp->oph.operation); + msgb_free(msg); + return 0; + } + if (i == L2DOWNSLLEN) { + LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this " + "state %s.\n", dp->oph.primitive, dp->oph.operation, + lapd_state_names[dl->state]); + msgb_free(msg); + return 0; + } + + LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s\n", + l2downstatelist[i].name, lapd_state_names[dl->state]); + + rc = l2downstatelist[i].rout(dp, lctx); + + return rc; +} + diff --git a/src/shared/libosmocore/src/gsm/lapdm.c b/src/shared/libosmocore/src/gsm/lapdm.c index f99c1193..82f8b0c1 100644 --- a/src/shared/libosmocore/src/gsm/lapdm.c +++ b/src/shared/libosmocore/src/gsm/lapdm.c @@ -1,7 +1,7 @@ /* GSM LAPDm (TS 04.06) implementation */ /* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org> - * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu> + * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu> * * All Rights Reserved * @@ -27,36 +27,6 @@ /*! \file lapdm.c */ -/*! - * Notes on Buffering: rcv_buffer, tx_queue, tx_hist, send_buffer, send_queue - * - * RX data is stored in the rcv_buffer (pointer). If the message is complete, it - * is removed from rcv_buffer pointer and forwarded to L3. If the RX data is - * received while there is an incomplete rcv_buffer, it is appended to it. - * - * TX data is stored in the send_queue first. When transmitting a frame, - * the first message in the send_queue is moved to the send_buffer. There it - * resides until all fragments are acknowledged. Fragments to be sent by I - * frames are stored in the tx_hist buffer for resend, if required. Also the - * current fragment is copied into the tx_queue. There it resides until it is - * forwarded to layer 1. - * - * In case we have SAPI 0, we only have a window size of 1, so the unack- - * nowledged message resides always in the send_buffer. In case of a suspend, - * it can be written back to the first position of the send_queue. - * - * The layer 1 normally sends a PH-READY-TO-SEND. But because we use - * asynchronous transfer between layer 1 and layer 2 (serial link), we must - * send a frame before layer 1 reaches the right timeslot to send it. So we - * move the tx_queue to layer 1 when there is not already a pending frame, and - * wait until acknowledge after the frame has been sent. If we receive an - * acknowledge, we can send the next frame from the buffer, if any. - * - * The moving of tx_queue to layer 1 may also trigger T200, if desired. Also it - * will trigger next I frame, if possible. - * - */ - #include <stdio.h> #include <stdint.h> #include <string.h> @@ -84,6 +54,7 @@ #define LAPDm_SAPI_SMS 3 #define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1) +#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3) #define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7) #define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1) #define LAPDm_ADDR_EA(addr) ((addr) & 0x1) @@ -105,19 +76,11 @@ #define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1) #define LAPDm_CTRL_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) #define LAPDm_MORE 0x2 +#define LAPDm_EL 0x1 + +#define LAPDm_U_UI 0x0 /* TS 04.06 Section 5.8.3 */ #define N201_AB_SACCH 18 @@ -137,19 +100,8 @@ #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 - -/* k value for each SAPI */ -static uint8_t k_sapi[] = {1, 1, 1, 1, 1, 1, 1, 1}; +/* FIXME: set N200 depending on chan_nr */ +#define N200 N200_TR_SDCCH enum lapdm_format { LAPDm_FMT_A, @@ -159,36 +111,22 @@ enum lapdm_format { LAPDm_FMT_B4, }; -static void lapdm_t200_cb(void *data); -static int rslms_send_i(struct lapdm_msg_ctx *mctx, int line); - -/* UTILITY FUNCTIONS */ - -static inline uint8_t inc_mod8(uint8_t x) -{ - return (x + 1) & 7; -} - -static inline uint8_t add_mod8(uint8_t x, uint8_t y) -{ - return (x + y) & 7; -} - -static inline uint8_t sub_mod8(uint8_t x, uint8_t y) -{ - return (x - y) & 7; /* handle negative results correctly */ -} +static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg); +static int send_rslms_dlsap(struct osmo_dlsap_prim *dp, + struct lapd_msg_ctx *lctx); static void lapdm_dl_init(struct lapdm_datalink *dl, struct lapdm_entity *entity) { memset(dl, 0, sizeof(*dl)); - INIT_LLIST_HEAD(&dl->send_queue); - INIT_LLIST_HEAD(&dl->tx_queue); - dl->state = LAPDm_STATE_IDLE; - dl->t200.data = dl; - dl->t200.cb = &lapdm_t200_cb; dl->entity = entity; + lapd_dl_init(&dl->dl, 1, 8, 200); + dl->dl.reestablish = 0; /* GSM uses no reestablish */ + dl->dl.send_ph_data_req = lapdm_send_ph_data_req; + dl->dl.send_dlsap = send_rslms_dlsap; + dl->dl.n200_est_rel = N200_EST_REL; + dl->dl.n200 = N200; + dl->dl.t203_sec = 0; dl->dl.t203_usec = 0; } /*! \brief initialize a LAPDm entity and all datalinks inside @@ -215,35 +153,10 @@ void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode) void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode) { lapdm_entity_init(&lc->lapdm_acch, mode); + /* FIXME: this depends on chan type */ lapdm_entity_init(&lc->lapdm_dcch, mode); } -static void lapdm_dl_flush_send(struct lapdm_datalink *dl) -{ - struct msgb *msg; - - /* Flush send-queue */ - while ((msg = msgb_dequeue(&dl->send_queue))) - msgb_free(msg); - - /* Clear send-buffer */ - if (dl->send_buffer) { - msgb_free(dl->send_buffer); - dl->send_buffer = NULL; - } -} - -static void lapdm_dl_flush_tx(struct lapdm_datalink *dl) -{ - struct msgb *msg; - unsigned int i; - - while ((msg = msgb_dequeue(&dl->tx_queue))) - msgb_free(msg); - for (i = 0; i < 8; i++) - dl->tx_length[i] = 0; -} - /*! \brief flush and release all resoures in LAPDm entity */ void lapdm_entity_exit(struct lapdm_entity *le) { @@ -252,10 +165,7 @@ void lapdm_entity_exit(struct lapdm_entity *le) for (i = 0; i < ARRAY_SIZE(le->datalink); i++) { dl = &le->datalink[i]; - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - if (dl->rcv_buffer) - msgb_free(dl->rcv_buffer); + lapd_dl_exit(&dl->dl); } } @@ -270,14 +180,6 @@ void lapdm_channel_exit(struct lapdm_channel *lc) lapdm_entity_exit(&lc->lapdm_dcch); } -static void lapdm_dl_newstate(struct lapdm_datalink *dl, uint32_t state) -{ - LOGP(DLLAPDM, LOGL_INFO, "new state %s -> %s\n", - lapdm_state_names[dl->state], lapdm_state_names[state]); - - dl->state = state; -} - static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi) { switch (sapi) { @@ -305,7 +207,7 @@ static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201) uint8_t *data; if (pad_len < 0) { - LOGP(DLLAPDM, LOGL_ERROR, + LOGP(DLLAPD, LOGL_ERROR, "cannot pad message that is already too big!\n"); return; } @@ -328,17 +230,17 @@ static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le) /* write a frame into the tx queue */ static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg, - uint8_t chan_nr, uint8_t link_id, uint8_t n201) + uint8_t chan_nr, uint8_t link_id, uint8_t pad) { struct lapdm_entity *le = dl->entity; struct osmo_phsap_prim pp; /* if there is a pending message, queue it */ if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) { - *msgb_push(msg, 1) = n201; + *msgb_push(msg, 1) = pad; *msgb_push(msg, 1) = link_id; *msgb_push(msg, 1) = chan_nr; - msgb_enqueue(&dl->tx_queue, msg); + msgb_enqueue(&dl->dl.tx_queue, msg); return -EBUSY; } @@ -349,7 +251,7 @@ static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg, /* send the frame now */ le->tx_pending = 0; /* disabled flow control */ - lapdm_pad_msgb(msg, n201); + lapdm_pad_msgb(msg, pad); return le->l1_prim_cb(&pp.oph, le->l1_ctx); } @@ -366,7 +268,7 @@ static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le) /* next */ i = (i + 1) % n; dl = &le->datalink[i]; - if ((msg = msgb_dequeue(&dl->tx_queue))) + if ((msg = msgb_dequeue(&dl->dl.tx_queue))) break; } while (i != last); @@ -383,7 +285,7 @@ static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le) int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp) { struct msgb *msg; - uint8_t n201; + uint8_t pad; msg = tx_dequeue_msgb(le); if (!msg) @@ -398,11 +300,11 @@ int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp msgb_pull(msg, 1); pp->u.data.link_id = *msg->data; msgb_pull(msg, 1); - n201 = *msg->data; + pad = *msg->data; msgb_pull(msg, 1); /* Pad the frame, we can transmit now */ - lapdm_pad_msgb(msg, n201); + lapdm_pad_msgb(msg, pad); return 0; } @@ -486,1143 +388,123 @@ static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx) { struct msgb *msg; - LOGP(DLLAPDM, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause); + LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause); msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1); msg->l2h = msgb_put(msg, sizeof(struct abis_rsl_rll_hdr)); msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause); return rslms_sendmsg(msg, mctx->dl->entity); } -static int check_length_ind(struct lapdm_msg_ctx *mctx, 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. */ - LOGP(DLLAPDM, LOGL_NOTICE, - "we don't support multi-octet length\n"); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - return 0; -} - -static int lapdm_send_resend(struct lapdm_datalink *dl) +/* DLSAP L2 -> L3 (RSLms) */ +static int send_rslms_dlsap(struct osmo_dlsap_prim *dp, + struct lapd_msg_ctx *lctx) { - struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm resend"); - int length; - - /* Resend SABM/DISC from tx_hist */ - length = dl->tx_length[0]; - msg->l2h = msgb_put(msg, length); - memcpy(msg->l2h, dl->tx_hist[dl->V_send], length); - - return tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr, dl->mctx.link_id, - dl->mctx.n201); -} - -static int lapdm_send_ua(struct lapdm_msg_ctx *mctx, uint8_t len, uint8_t *data) -{ - uint8_t sapi = mctx->link_id & 7; - uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl); - struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm UA"); - struct lapdm_entity *le = mctx->dl->entity; - - msg->l2h = msgb_put(msg, 3 + len); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); - msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UA, f_bit); - msg->l2h[2] = LAPDm_LEN(len); - if (len) - memcpy(msg->l2h + 3, data, len); - - return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, - mctx->n201); -} - -static int lapdm_send_dm(struct lapdm_msg_ctx *mctx) -{ - uint8_t sapi = mctx->link_id & 7; - uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl); - struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm DM"); - struct lapdm_entity *le = mctx->dl->entity; - - msg->l2h = msgb_put(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); - msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DM, f_bit); - msg->l2h[2] = 0; - - return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, - mctx->n201); -} - -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"); - struct lapdm_entity *le = mctx->dl->entity; - - msg->l2h = msgb_put(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.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_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, - mctx->n201); -} - -static int lapdm_send_rnr(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 RNR"); - struct lapdm_entity *le = mctx->dl->entity; - - msg->l2h = msgb_put(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); - msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RNR, f_bit); - msg->l2h[2] = LAPDm_LEN(0); - - return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, - mctx->n201); -} - -static int lapdm_send_rej(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 REJ"); - struct lapdm_entity *le = mctx->dl->entity; - - msg->l2h = msgb_put(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); - msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_REJ, f_bit); - msg->l2h[2] = LAPDm_LEN(0); - - return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, - mctx->n201); -} - -/* Timer callback on T200 expiry */ -static void lapdm_t200_cb(void *data) -{ - struct lapdm_datalink *dl = data; - - LOGP(DLLAPDM, LOGL_INFO, "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 + 1 >= N200_EST_REL + 1) { - /* send RELEASE INDICATION to L3 */ - send_rll_simple(RSL_MT_REL_IND, &dl->mctx); - /* send MDL ERROR INIDCATION to L3 */ - rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx); - /* flush tx buffers */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - /* go back to idle state */ - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - /* NOTE: we must not change any other states or buffers - * and queues, since we may reconnect after handover - * failure. the buffered messages is replaced there */ - break; - } - /* retransmit SABM command */ - lapdm_send_resend(dl); - /* increment re-transmission counter */ - dl->retrans_ctr++; - /* restart T200 (PH-READY-TO-SEND) */ - osmo_timer_schedule(&dl->t200, T200); - break; - case LAPDm_STATE_DISC_SENT: - /* 5.4.4.3 */ - if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) { - /* send RELEASE INDICATION to L3 */ - send_rll_simple(RSL_MT_REL_CONF, &dl->mctx); - /* send MDL ERROR INIDCATION to L3 */ - rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx); - /* flush buffers */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - /* go back to idle state */ - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - /* NOTE: we must not change any other states or buffers - * and queues, since we may reconnect after handover - * failure. the buffered messages is replaced there */ - break; - } - /* retransmit DISC command */ - lapdm_send_resend(dl); - /* increment re-transmission counter */ - dl->retrans_ctr++; - /* restart T200 (PH-READY-TO-SEND) */ - osmo_timer_schedule(&dl->t200, T200); + struct lapd_datalink *dl = lctx->dl; + struct lapdm_datalink *mdl = + container_of(dl, struct lapdm_datalink, dl); + struct lapdm_msg_ctx *mctx = &mdl->mctx; + uint8_t rll_msg = 0; + + switch (OSMO_PRIM_HDR(&dp->oph)) { + case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION): + rll_msg = RSL_MT_EST_IND; break; - case LAPDm_STATE_MF_EST: - /* 5.5.7 */ - dl->retrans_ctr = 0; - lapdm_dl_newstate(dl, LAPDm_STATE_TIMER_RECOV); - /* fall through */ - case LAPDm_STATE_TIMER_RECOV: - dl->retrans_ctr++; - if (dl->retrans_ctr < N200) { - /* retransmit I frame (V_s-1) with P=1, if any */ - if (dl->tx_length[sub_mod8(dl->V_send, 1)]) { - struct msgb *msg; - int length; - - LOGP(DLLAPDM, LOGL_INFO, "retransmit last frame " - "V(S)=%d\n", sub_mod8(dl->V_send, 1)); - /* Create I frame (segment) from tx_hist */ - length = dl->tx_length[sub_mod8(dl->V_send, 1)]; - msg = msgb_alloc_headroom(23+10, 10, "LAPDm I"); - msg->l2h = msgb_put(msg, length); - memcpy(msg->l2h, - dl->tx_hist[sub_mod8(dl->V_send, 1)], - length); - msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, - sub_mod8(dl->V_send, 1), 1); /* P=1 */ - tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr, - dl->mctx.link_id, dl->mctx.n201); - } else { - /* OR send appropriate supervision frame with P=1 */ - if (!dl->own_busy && !dl->seq_err_cond) { - lapdm_send_rr(&dl->mctx, 1); - /* NOTE: In case of sequence error - * condition, the REJ frame has been - * transmitted when entering the - * condition, so it has not be done - * here - */ - } else if (dl->own_busy) { - lapdm_send_rnr(&dl->mctx, 1); - } else { - LOGP(DLLAPDM, LOGL_INFO, "unhandled, " - "pls. fix\n"); - } - } - /* restart T200 (PH-READY-TO-SEND) */ - osmo_timer_schedule(&dl->t200, T200); - } else { - /* send MDL ERROR INIDCATION to L3 */ - rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx); - } + case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM): + rll_msg = RSL_MT_EST_CONF; break; - default: - LOGP(DLLAPDM, LOGL_INFO, "T200 expired in unexpected " - "dl->state %u\n", dl->state); - } -} - -/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */ -static void lapdm_acknowledge(struct lapdm_msg_ctx *mctx) -{ - struct lapdm_datalink *dl = mctx->dl; - uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl); - int s = 0, rej = 0, t200_reset = 0; - int i; - - /* supervisory frame ? */ - if (LAPDm_CTRL_is_S(mctx->ctrl)) - s = 1; - /* REJ frame ? */ - if (s && LAPDm_CTRL_S_BITS(mctx->ctrl) == LAPDm_S_REJ) - rej = 1; - - /* Flush all transmit buffers of acknowledged frames */ - for (i = dl->V_ack; i != nr; i = inc_mod8(i)) { - if (dl->tx_length[i]) { - dl->tx_length[i] = 0; - LOGP(DLLAPDM, LOGL_INFO, "ack frame %d\n", i); - } - } - - 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), - * or an REJ with an N(R) equal to V(A). */ - if ((!rej && nr != dl->V_ack) - || (rej && nr == dl->V_ack)) { - LOGP(DLLAPDM, LOGL_INFO, "reset t200\n"); - t200_reset = 1; - osmo_timer_del(&dl->t200); - /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */ - } - /* 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. - */ - if (sub_mod8(nr, dl->V_ack) > sub_mod8(dl->V_send, dl->V_ack)) { - LOGP(DLLAPDM, LOGL_NOTICE, "N(R) sequence error\n"); - rsl_rll_error(RLL_CAUSE_SEQ_ERR, mctx); - } - } - - /* V(A) shall be set to the value of N(R) */ - dl->V_ack = nr; - - /* If T200 has been reset by the receipt of an I, RR or RNR frame, - * and if there are outstanding I frames, restart T200 */ - if (t200_reset && !rej) { - if (dl->tx_length[dl->V_send - 1]) { - LOGP(DLLAPDM, LOGL_INFO, "start T200, due to unacked I " - "frame(s)\n"); - osmo_timer_schedule(&dl->t200, T200); - } - } -} - -/* L1 -> L2 */ - -/* Receive a LAPDm U (Unnumbered) message from L1 */ -static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx) -{ - struct lapdm_datalink *dl = mctx->dl; - struct lapdm_entity *le = dl->entity; - uint8_t length; - int rc; - int rsl_msg; - - switch (LAPDm_CTRL_U_BITS(mctx->ctrl)) { - case LAPDm_U_SABM: - rsl_msg = RSL_MT_EST_IND; - - LOGP(DLLAPDM, LOGL_INFO, "SABM received\n"); - /* 5.7.1 */ - dl->seq_err_cond = 0; - /* G.2.2 Wrong value of the C/R bit */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) { - LOGP(DLLAPDM, LOGL_NOTICE, "SABM response error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - - length = msg->l2h[2] >> 2; - /* G.4.5 If SABM is received with L>N201 or with M bit - * set, AN MDL-ERROR-INDICATION is sent to MM. - */ - if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) { - LOGP(DLLAPDM, LOGL_NOTICE, "SABM too large error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx); - return -EIO; - } - - /* Must be Format B */ - rc = check_length_ind(mctx, msg->l2h[2]); - if (rc < 0) { - msgb_free(msg); - return rc; - } - switch (dl->state) { - case LAPDm_STATE_IDLE: - /* Set chan_nr and link_id for established connection */ - memset(&dl->mctx, 0, sizeof(dl->mctx)); - dl->mctx.dl = dl; - dl->mctx.chan_nr = mctx->chan_nr; - dl->mctx.link_id = mctx->link_id; - dl->mctx.n201 = mctx->n201; - break; - case LAPDm_STATE_MF_EST: - if (length == 0) { - rsl_msg = RSL_MT_EST_CONF; - break; - } - LOGP(DLLAPDM, LOGL_INFO, "SABM command, multiple " - "frame established state\n"); - /* check for contention resoultion */ - if (dl->tx_hist[0][2] >> 2) { - LOGP(DLLAPDM, LOGL_NOTICE, "SABM not allowed " - "during contention resolution\n"); - rsl_rll_error(RLL_CAUSE_SABM_INFO_NOTALL, mctx); - } - msgb_free(msg); - return 0; - case LAPDm_STATE_DISC_SENT: - /* 5.4.6.2 send DM with F=P */ - lapdm_send_dm(mctx); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - msgb_free(msg); - return send_rll_simple(RSL_MT_REL_CONF, mctx); - default: - lapdm_send_ua(mctx, length, msg->l2h + 3); - msgb_free(msg); - return 0; - } - /* send UA response */ - lapdm_send_ua(mctx, length, msg->l2h + 3); - /* set Vs, Vr and Va to 0 */ - dl->V_send = dl->V_recv = dl->V_ack = 0; - /* clear tx_hist */ - dl->tx_length[0] = 0; - /* enter multiple-frame-established state */ - lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST); - /* send notification to L3 */ - if (length == 0) { - /* 5.4.1.2 Normal establishment procedures */ - rc = send_rll_simple(rsl_msg, mctx); - msgb_free(msg); - } else { - /* 5.4.1.4 Contention resolution establishment */ - msg->l3h = msg->l2h + 3; - msgb_pull_l2h(msg); - rc = send_rslms_rll_l3(rsl_msg, mctx, msg); - } + case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION): + rll_msg = RSL_MT_DATA_IND; break; - case LAPDm_U_DM: - LOGP(DLLAPDM, LOGL_INFO, "DM received\n"); - /* G.2.2 Wrong value of the C/R bit */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd) { - LOGP(DLLAPDM, LOGL_NOTICE, "DM command error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - /* 5.4.1.2 DM responses with the F bit set to "0" - * shall be ignored. - */ - msgb_free(msg); - return 0; - } - switch (dl->state) { - case LAPDm_STATE_SABM_SENT: - break; - case LAPDm_STATE_MF_EST: - if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 1) { - LOGP(DLLAPDM, LOGL_INFO, "unsolicited DM " - "response\n"); - rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP, mctx); - } else { - LOGP(DLLAPDM, LOGL_INFO, "unsolicited DM " - "response, multiple frame established " - "state\n"); - rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx); - } - msgb_free(msg); - return 0; - case LAPDm_STATE_TIMER_RECOV: - /* DM is normal in case PF = 1 */ - if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 0) { - LOGP(DLLAPDM, LOGL_INFO, "unsolicited DM " - "response, multiple frame established " - "state\n"); - rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx); - msgb_free(msg); - return 0; - } - break; - case LAPDm_STATE_DISC_SENT: - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - /* go to idle state */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - rc = send_rll_simple(RSL_MT_REL_CONF, mctx); - msgb_free(msg); - return 0; - case LAPDm_STATE_IDLE: - /* 5.4.5 all other frame types shall be discarded */ - default: - LOGP(DLLAPDM, LOGL_INFO, "unsolicited DM response! " - "(discarding)\n"); - msgb_free(msg); - return 0; - } - /* reset T200 */ - osmo_timer_del(&dl->t200); - rc = send_rll_simple(RSL_MT_REL_IND, mctx); - msgb_free(msg); + case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION): + return send_rslms_rll_l3_ui(mctx, dp->oph.msg); + case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION): + rll_msg = RSL_MT_REL_IND; break; - case LAPDm_U_UI: - LOGP(DLLAPDM, LOGL_INFO, "UI received\n"); - /* G.2.2 Wrong value of the C/R bit */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) { - LOGP(DLLAPDM, LOGL_NOTICE, "UI indicates response " - "error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - - length = msg->l2h[2] >> 2; - /* FIXME: G.4.5 If UI is received with L>N201 or with M bit - * set, AN MDL-ERROR-INDICATION is sent to MM. - */ - - if (mctx->lapdm_fmt == LAPDm_FMT_B4) { - length = N201_B4; - msg->l3h = msg->l2h + 2; - } else { - rc = check_length_ind(mctx, msg->l2h[2]); - if (rc < 0) { - msgb_free(msg); - 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 - */ - LOGP(DLLAPDM, LOGL_INFO, "length=0 (discarding)\n"); - msgb_free(msg); - return 0; - } - switch (LAPDm_ADDR_SAPI(mctx->addr)) { - case LAPDm_SAPI_NORMAL: - case LAPDm_SAPI_SMS: - break; - default: - /* 5.3.3 UI frames with invalid SAPI values shall be - * discarded - */ - LOGP(DLLAPDM, LOGL_INFO, "sapi=%u (discarding)\n", - LAPDm_ADDR_SAPI(mctx->addr)); - msgb_free(msg); - return 0; - } - msgb_pull_l2h(msg); - rc = send_rslms_rll_l3_ui(mctx, msg); + case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM): + rll_msg = RSL_MT_REL_CONF; break; - case LAPDm_U_DISC: - rsl_msg = RSL_MT_REL_IND; - - LOGP(DLLAPDM, LOGL_INFO, "DISC received\n"); - /* flush buffers */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - /* 5.7.1 */ - dl->seq_err_cond = 0; - /* G.2.2 Wrong value of the C/R bit */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) { - LOGP(DLLAPDM, LOGL_NOTICE, "DISC response error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - 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. - */ - LOGP(DLLAPDM, LOGL_NOTICE, - "U frame iwth incorrect parameters "); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx); - return -EIO; - } - switch (dl->state) { - case LAPDm_STATE_IDLE: - LOGP(DLLAPDM, LOGL_INFO, "DISC in idle state\n"); - /* send DM with F=P */ - msgb_free(msg); - return lapdm_send_dm(mctx); - case LAPDm_STATE_SABM_SENT: - LOGP(DLLAPDM, LOGL_INFO, "DISC in SABM state\n"); - /* 5.4.6.2 send DM with F=P */ - lapdm_send_dm(mctx); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - msgb_free(msg); - return send_rll_simple(RSL_MT_REL_IND, mctx); - case LAPDm_STATE_MF_EST: - case LAPDm_STATE_TIMER_RECOV: - LOGP(DLLAPDM, LOGL_INFO, "DISC in est state\n"); - break; - case LAPDm_STATE_DISC_SENT: - LOGP(DLLAPDM, LOGL_INFO, "DISC in disc state\n"); - rsl_msg = RSL_MT_REL_CONF; - break; - default: - lapdm_send_ua(mctx, length, msg->l2h + 3); - msgb_free(msg); - return 0; - } - /* send UA response */ - lapdm_send_ua(mctx, length, msg->l2h + 3); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - /* enter idle state */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - /* send notification to L3 */ - rc = send_rll_simple(rsl_msg, mctx); - msgb_free(msg); + case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM): + rll_msg = RSL_MT_SUSP_CONF; break; - case LAPDm_U_UA: - LOGP(DLLAPDM, LOGL_INFO, "UA received\n"); - /* G.2.2 Wrong value of the C/R bit */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd) { - LOGP(DLLAPDM, LOGL_NOTICE, "UA indicates command " - "error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - - length = msg->l2h[2] >> 2; - /* G.4.5 If UA is received with L>N201 or with M bit - * set, AN MDL-ERROR-INDICATION is sent to MM. - */ - if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) { - LOGP(DLLAPDM, LOGL_NOTICE, "UA too large error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx); - return -EIO; - } - - if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - /* 5.4.1.2 A UA response with the F bit set to "0" - * shall be ignored. - */ - LOGP(DLLAPDM, LOGL_INFO, "F=0 (discarding)\n"); - msgb_free(msg); - return 0; - } - switch (dl->state) { - case LAPDm_STATE_SABM_SENT: - break; - case LAPDm_STATE_MF_EST: - case LAPDm_STATE_TIMER_RECOV: - LOGP(DLLAPDM, LOGL_INFO, "unsolicited UA response! " - "(discarding)\n"); - rsl_rll_error(RLL_CAUSE_UNSOL_UA_RESP, mctx); - msgb_free(msg); - return 0; - case LAPDm_STATE_DISC_SENT: - LOGP(DLLAPDM, LOGL_INFO, "UA in disconnect state\n"); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - /* go to idle state */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - rc = send_rll_simple(RSL_MT_REL_CONF, mctx); - msgb_free(msg); - return 0; - case LAPDm_STATE_IDLE: - /* 5.4.5 all other frame types shall be discarded */ - default: - LOGP(DLLAPDM, LOGL_INFO, "unsolicited UA response! " - "(discarding)\n"); - msgb_free(msg); - return 0; - } - LOGP(DLLAPDM, LOGL_INFO, "UA in SABM state\n"); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - /* compare UA with SABME if contention resolution is applied */ - if (dl->tx_hist[0][2] >> 2) { - rc = check_length_ind(mctx, msg->l2h[2]); - if (rc < 0) { - rc = send_rll_simple(RSL_MT_REL_IND, mctx); - msgb_free(msg); - /* go to idle state */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - return 0; - } - length = msg->l2h[2] >> 2; - if (length != (dl->tx_hist[0][2] >> 2) - || !!memcmp(dl->tx_hist[0] + 3, msg->l2h + 3, - length)) { - LOGP(DLLAPDM, LOGL_INFO, "**** UA response " - "mismatches ****\n"); - rc = send_rll_simple(RSL_MT_REL_IND, mctx); - msgb_free(msg); - /* go to idle state */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - return 0; - } - } - /* set Vs, Vr and Va to 0 */ - dl->V_send = dl->V_recv = dl->V_ack = 0; - /* clear tx_hist */ - dl->tx_length[0] = 0; - /* enter multiple-frame-established state */ - lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST); - /* send outstanding frames, if any (resume / reconnect) */ - rslms_send_i(mctx, __LINE__); - /* send notification to L3 */ - rc = send_rll_simple(RSL_MT_EST_CONF, mctx); - msgb_free(msg); - break; - default: - /* G.3.1 */ - LOGP(DLLAPDM, LOGL_NOTICE, "Unnumbered frame not allowed.\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - 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; - struct lapdm_entity *le = dl->entity; - 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. */ - LOGP(DLLAPDM, LOGL_NOTICE, - "S frame with incorrect parameters\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_SFRM_INC_PARAM, mctx); - return -EIO; - } - - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp - && LAPDm_CTRL_PF_BIT(mctx->ctrl) - && dl->state != LAPDm_STATE_TIMER_RECOV) { - /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */ - LOGP(DLLAPDM, LOGL_NOTICE, "S frame response with F=1 error\n"); - rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx); - } - - switch (dl->state) { - case LAPDm_STATE_IDLE: - /* if P=1, respond DM with F=1 (5.2.2) */ - /* 5.4.5 all other frame types shall be discarded */ - if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) - lapdm_send_dm(mctx); /* F=P */ - /* fall though */ - case LAPDm_STATE_SABM_SENT: - case LAPDm_STATE_DISC_SENT: - LOGP(DLLAPDM, LOGL_NOTICE, "S frame ignored in this state\n"); - msgb_free(msg); + case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION): + rsl_rll_error(dp->u.error_ind.cause, mctx); + if (dp->oph.msg) + msgb_free(dp->oph.msg); return 0; } - switch (LAPDm_CTRL_S_BITS(mctx->ctrl)) { - case LAPDm_S_RR: - LOGP(DLLAPDM, LOGL_INFO, "RR received\n"); - /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */ - lapdm_acknowledge(mctx); - - /* 5.5.3.2 */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd - && LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - if (!dl->own_busy && !dl->seq_err_cond) { - LOGP(DLLAPDM, LOGL_NOTICE, "RR frame command " - "with polling bit set and we are not " - "busy, so we reply with RR frame\n"); - lapdm_send_rr(mctx, 1); - /* NOTE: In case of sequence error condition, - * the REJ frame has been transmitted when - * entering the condition, so it has not be - * done here - */ - } else if (dl->own_busy) { - LOGP(DLLAPDM, LOGL_NOTICE, "RR frame command " - "with polling bit set and we are busy, " - "so we reply with RR frame\n"); - lapdm_send_rnr(mctx, 1); - } - } else if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp - && LAPDm_CTRL_PF_BIT(mctx->ctrl) - && dl->state == LAPDm_STATE_TIMER_RECOV) { - LOGP(DLLAPDM, LOGL_INFO, "RR response with F==1, " - "and we are in timer recovery state, so " - "we leave that state\n"); - /* V(S) to the N(R) in the RR frame */ - dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - /* 5.5.7 Clear timer recovery condition */ - lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST); - } - /* Send message, if possible due to acknowledged data */ - rslms_send_i(mctx, __LINE__); - - break; - case LAPDm_S_RNR: - LOGP(DLLAPDM, LOGL_INFO, "RNR received\n"); - /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */ - lapdm_acknowledge(mctx); - - /* 5.5.5 */ - /* Set peer receiver busy condition */ - dl->peer_busy = 1; - - if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd) { - if (!dl->own_busy) { - LOGP(DLLAPDM, LOGL_INFO, "RNR poll " - "command and we are not busy, " - "so we reply with RR final " - "response\n"); - /* Send RR with F=1 */ - lapdm_send_rr(mctx, 1); - } else { - LOGP(DLLAPDM, LOGL_INFO, "RNR poll " - "command and we are busy, so " - "we reply with RNR final " - "response\n"); - /* Send RNR with F=1 */ - lapdm_send_rnr(mctx, 1); - } - } else if (dl->state == LAPDm_STATE_TIMER_RECOV) { - LOGP(DLLAPDM, LOGL_INFO, "RNR poll response " - "and we in timer recovery state, so " - "we leave that state\n"); - /* 5.5.7 Clear timer recovery condition */ - lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST); - /* V(S) to the N(R) in the RNR frame */ - dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl); - } - } else - LOGP(DLLAPDM, LOGL_INFO, "RNR not polling/final state " - "received\n"); - - /* Send message, if possible due to acknowledged data */ - rslms_send_i(mctx, __LINE__); - - break; - case LAPDm_S_REJ: - LOGP(DLLAPDM, LOGL_INFO, "REJ received\n"); - /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */ - lapdm_acknowledge(mctx); - - /* 5.5.4.1 */ - if (dl->state != LAPDm_STATE_TIMER_RECOV) { - /* Clear an existing peer receiver busy condition */ - dl->peer_busy = 0; - /* V(S) and V(A) to the N(R) in the REJ frame */ - dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - /* 5.5.3.2 */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd - && LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - if (!dl->own_busy && !dl->seq_err_cond) { - LOGP(DLLAPDM, LOGL_INFO, "REJ poll " - "command not in timer recovery " - "state and not in own busy " - "condition received, so we " - "respond with RR final " - "response\n"); - lapdm_send_rr(mctx, 1); - /* NOTE: In case of sequence error - * condition, the REJ frame has been - * transmitted when entering the - * condition, so it has not be done - * here - */ - } else if (dl->own_busy) { - LOGP(DLLAPDM, LOGL_INFO, "REJ poll " - "command not in timer recovery " - "state and in own busy " - "condition received, so we " - "respond with RNR final " - "response\n"); - lapdm_send_rnr(mctx, 1); - } - } else - LOGP(DLLAPDM, LOGL_INFO, "REJ response or not " - "polling command not in timer recovery " - "state received\n"); - /* send MDL ERROR INIDCATION to L3 */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp - && LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx); - } - - } else if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp - && LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - LOGP(DLLAPDM, LOGL_INFO, "REJ poll response in timer " - "recovery state received\n"); - /* Clear an existing peer receiver busy condition */ - dl->peer_busy = 0; - /* 5.5.7 Clear timer recovery condition */ - lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST); - /* V(S) and V(A) to the N(R) in the REJ frame */ - dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - } else { - /* Clear an existing peer receiver busy condition */ - dl->peer_busy = 0; - /* V(S) and V(A) to the N(R) in the REJ frame */ - dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl); - /* 5.5.3.2 */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd - && LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - if (!dl->own_busy && !dl->seq_err_cond) { - LOGP(DLLAPDM, LOGL_INFO, "REJ poll " - "command in timer recovery " - "state and not in own busy " - "condition received, so we " - "respond with RR final " - "response\n"); - lapdm_send_rr(mctx, 1); - /* NOTE: In case of sequence error - * condition, the REJ frame has been - * transmitted when entering the - * condition, so it has not be done - * here - */ - } else if (dl->own_busy) { - LOGP(DLLAPDM, LOGL_INFO, "REJ poll " - "command in timer recovery " - "state and in own busy " - "condition received, so we " - "respond with RNR final " - "response\n"); - lapdm_send_rnr(mctx, 1); - } - } else - LOGP(DLLAPDM, LOGL_INFO, "REJ response or not " - "polling command in timer recovery " - "state received\n"); - } - - /* FIXME: 5.5.4.2 2) */ - - /* Send message, if possible due to acknowledged data */ - rslms_send_i(mctx, __LINE__); - - break; - default: - /* G.3.1 */ - LOGP(DLLAPDM, LOGL_NOTICE, "Supervisory frame not allowed.\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - msgb_free(msg); - 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; - struct lapdm_entity *le = dl->entity; - //uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl); - uint8_t ns = LAPDm_CTRL_I_Ns(mctx->ctrl); - uint8_t length; - int rc; - LOGP(DLLAPDM, LOGL_NOTICE, "I received\n"); - - /* G.2.2 Wrong value of the C/R bit */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) { - LOGP(DLLAPDM, LOGL_NOTICE, "I frame response not allowed\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); + if (!rll_msg) { + LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please " + "fix!\n", dp->oph.primitive, dp->oph.operation); return -EINVAL; } - length = msg->l2h[2] >> 2; - if (length == 0 || length + 3 > mctx->n201) { - /* 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. */ - LOGP(DLLAPDM, LOGL_NOTICE, "I frame length not allowed\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_IFRM_INC_LEN, mctx); - return -EIO; - } + if (!dp->oph.msg) + return send_rll_simple(rll_msg, mctx); - /* G.4.2 If the numerical value of L is L<N201 and the M - * bit is set to "1", then an MDL-ERROR-INDICATION primitive with - * cause "I frame with incorrect use of M bit" is sent to the - * mobile management entity. */ - if ((msg->l2h[2] & LAPDm_MORE) && length + 3 < mctx->n201) { - LOGP(DLLAPDM, LOGL_NOTICE, "I frame with M bit too short\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_IFRM_INC_MBITS, mctx); - return -EIO; - } - - switch (dl->state) { - case LAPDm_STATE_IDLE: - /* if P=1, respond DM with F=1 (5.2.2) */ - /* 5.4.5 all other frame types shall be discarded */ - if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) - lapdm_send_dm(mctx); /* F=P */ - /* fall though */ - case LAPDm_STATE_SABM_SENT: - case LAPDm_STATE_DISC_SENT: - LOGP(DLLAPDM, LOGL_NOTICE, "I frame ignored in this state\n"); - msgb_free(msg); - return 0; - } - - /* 5.7.1: N(s) sequence error */ - if (ns != dl->V_recv) { - LOGP(DLLAPDM, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, " - "V(R)=%u\n", ns, dl->V_recv); - /* discard data */ - msgb_free(msg); - if (!dl->seq_err_cond) { - /* FIXME: help me understand what exactly todo here - dl->seq_err_cond = 1; - */ - lapdm_send_rej(mctx, LAPDm_CTRL_PF_BIT(mctx->ctrl)); - } else { - } - return -EIO; - } - dl->seq_err_cond = 0; - - /* Increment receiver state */ - dl->V_recv = inc_mod8(dl->V_recv); - LOGP(DLLAPDM, LOGL_NOTICE, "incrementing V(R) to %u\n", dl->V_recv); - - /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */ - lapdm_acknowledge(mctx); /* V(A) is also set here */ - - /* Only if we are not in own receiver busy condition */ - if (!dl->own_busy) { - /* if the frame carries a complete segment */ - if (!(msg->l2h[2] & LAPDm_MORE) - && !dl->rcv_buffer) { - LOGP(DLLAPDM, LOGL_INFO, "message in single I frame\n"); - /* send a DATA INDICATION to L3 */ - msg->l3h = msg->l2h + 3; - msgb_pull_l2h(msg); - msg->len = length; - msg->tail = msg->data + length; - rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, msg); - } else { - /* create rcv_buffer */ - if (!dl->rcv_buffer) { - LOGP(DLLAPDM, LOGL_INFO, "message in multiple I " - "frames (first message)\n"); - dl->rcv_buffer = msgb_alloc_headroom(200+56, 56, - "LAPDm RX"); - dl->rcv_buffer->l3h = dl->rcv_buffer->data; - } - /* concat. rcv_buffer */ - if (msgb_l3len(dl->rcv_buffer) + length > 200) { - LOGP(DLLAPDM, LOGL_NOTICE, "Received frame " - "overflow!\n"); - } else { - memcpy(msgb_put(dl->rcv_buffer, length), - msg->l2h + 3, length); - } - /* if the last segment was received */ - if (!(msg->l2h[2] & LAPDm_MORE)) { - LOGP(DLLAPDM, LOGL_INFO, "message in multiple I " - "frames (last message)\n"); - rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, - dl->rcv_buffer); - dl->rcv_buffer = NULL; - } else - LOGP(DLLAPDM, LOGL_INFO, "message in multiple I " - "frames (next message)\n"); - msgb_free(msg); - - } - } else - LOGP(DLLAPDM, LOGL_INFO, "I frame ignored during own receiver " - "busy condition\n"); - - /* Check for P bit */ - if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - /* 5.5.2.1 */ - /* check if we are not in own receiver busy */ - if (!dl->own_busy) { - LOGP(DLLAPDM, LOGL_INFO, "we are not busy, send RR\n"); - /* Send RR with F=1 */ - rc = lapdm_send_rr(mctx, 1); - } else { - LOGP(DLLAPDM, LOGL_INFO, "we are busy, send RNR\n"); - /* Send RNR with F=1 */ - rc = lapdm_send_rnr(mctx, 1); - } - } else { - /* 5.5.2.2 */ - /* check if we are not in own receiver busy */ - if (!dl->own_busy) { - /* NOTE: V(R) is already set above */ - rc = rslms_send_i(mctx, __LINE__); - if (rc) { - LOGP(DLLAPDM, LOGL_INFO, "we are not busy and " - "have no pending data, send RR\n"); - /* Send RR with F=0 */ - return lapdm_send_rr(mctx, 0); - } - /* all I or one RR is sent, we are done */ - return 0; - } else { - LOGP(DLLAPDM, LOGL_INFO, "we are busy, send RNR\n"); - /* Send RNR with F=0 */ - rc = lapdm_send_rnr(mctx, 0); - } - } - - /* Send message, if possible due to acknowledged data */ - rslms_send_i(mctx, __LINE__); - - return rc; + return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg); } -/* Receive a LAPDm message from L1 */ -static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx) +/* send a data frame to layer 1 */ +static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg) { - int rc; - - /* G.2.3 EA bit set to "0" is not allowed in GSM */ - if (!LAPDm_ADDR_EA(mctx->addr)) { - LOGP(DLLAPDM, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n"); + uint8_t l3_len = msg->tail - msg->data; + struct lapd_datalink *dl = lctx->dl; + struct lapdm_datalink *mdl = + container_of(dl, struct lapdm_datalink, dl); + struct lapdm_msg_ctx *mctx = &mdl->mctx; + int format = lctx->format; + + /* prepend l2 header */ + msg->l2h = msgb_push(msg, 3); + msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr); + /* EA is set here too */ + switch (format) { + case LAPD_FORM_I: + msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send, + lctx->p_f); + break; + case LAPD_FORM_S: + msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f); + break; + case LAPD_FORM_U: + msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f); + break; + default: msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); return -EINVAL; } + msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */ + if (lctx->more) + msg->l2h[2] |= LAPDm_MORE; - 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 { - LOGP(DLLAPDM, LOGL_NOTICE, "unknown LAPDm format\n"); - msgb_free(msg); - rc = -EINVAL; - } - return rc; + return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, + 23); } /* input into layer2 (from layer 1) */ -static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t chan_nr, uint8_t link_id) +static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, + uint8_t chan_nr, uint8_t link_id) { uint8_t cbits = chan_nr >> 3; uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */ struct lapdm_msg_ctx mctx; + struct lapd_msg_ctx lctx; int rc = 0; + int n201; /* when we reach here, we have a msgb with l2h pointing to the raw * 23byte mac block. The l1h has already been purged. */ + memset(&mctx, 0, sizeof(mctx)); mctx.chan_nr = chan_nr; mctx.link_id = 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; - mctx.n201 = N201_Bbis; + n201 = N201_Bbis; sapi = 0; } else { if (mctx.link_id & 0x40) { @@ -1631,12 +513,12 @@ static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t cha /* If sent by BTS, lapdm_fmt must be B4 */ if (le->mode == LAPDM_MODE_MS) { mctx.lapdm_fmt = LAPDm_FMT_B4; - mctx.n201 = N201_B4; - LOGP(DLLAPDM, LOGL_INFO, "fmt=B4\n"); + n201 = N201_B4; + LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n"); } else { mctx.lapdm_fmt = LAPDm_FMT_B; - mctx.n201 = N201_AB_SACCH; - LOGP(DLLAPDM, LOGL_INFO, "fmt=B\n"); + n201 = N201_AB_SACCH; + LOGP(DLLAPD, LOGL_INFO, "fmt=B\n"); } /* SACCH frames have a two-byte L1 header that * OsmocomBB L1 doesn't strip */ @@ -1647,8 +529,8 @@ static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t cha sapi = (msg->l2h[0] >> 2) & 7; } else { mctx.lapdm_fmt = LAPDm_FMT_B; - LOGP(DLLAPDM, LOGL_INFO, "fmt=B\n"); - mctx.n201 = 23; // FIXME: select correct size by chan. + LOGP(DLLAPD, LOGL_INFO, "fmt=B\n"); + n201 = 20; // FIXME: select correct size by chan. sapi = (msg->l2h[0] >> 2) & 7; } } @@ -1656,7 +538,7 @@ static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t cha mctx.dl = datalink_for_sapi(le, sapi); /* G.2.1 No action on frames containing an unallocated SAPI. */ if (!mctx.dl) { - LOGP(DLLAPDM, LOGL_NOTICE, "Received frame for unsupported " + LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported " "SAPI %d!\n", sapi); msgb_free(msg); return -EIO; @@ -1666,17 +548,77 @@ static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t cha case LAPDm_FMT_A: case LAPDm_FMT_B: case LAPDm_FMT_B4: - mctx.addr = msg->l2h[0]; - if (!(mctx.addr & 0x01)) { - LOGP(DLLAPDM, LOGL_ERROR, "we don't support " - "multibyte addresses (discarding)\n"); + lctx.dl = &mctx.dl->dl; + /* obtain SAPI from address field */ + mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]); + /* G.2.3 EA bit set to "0" is not allowed in GSM */ + if (!LAPDm_ADDR_EA(msg->l2h[0])) { + LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in " + "GSM\n"); msgb_free(msg); + rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx); 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); + /* adress field */ + lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]); + lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]); + lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]); + /* command field */ + if (LAPDm_CTRL_is_I(msg->l2h[1])) { + lctx.format = LAPD_FORM_I; + lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]); + lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]); + } else if (LAPDm_CTRL_is_S(msg->l2h[1])) { + lctx.format = LAPD_FORM_S; + lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]); + lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]); + } else if (LAPDm_CTRL_is_U(msg->l2h[1])) { + lctx.format = LAPD_FORM_U; + lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]); + } else + lctx.format = LAPD_FORM_UKN; + lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]); + if (lctx.sapi != LAPDm_SAPI_NORMAL + && lctx.sapi != LAPDm_SAPI_SMS + && lctx.format == LAPD_FORM_U + && lctx.s_u == LAPDm_U_UI) { + /* 5.3.3 UI frames with invalid SAPI values shall be + * discarded + */ + LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n", + lctx.sapi); + msgb_free(msg); + return 0; + } + if (mctx.lapdm_fmt == LAPDm_FMT_B4) { + lctx.n201 = n201; + lctx.length = n201; + lctx.more = 0; + msg->l3h = msg->l2h + 2; + msgb_pull_l2h(msg); + } else { + /* length field */ + if (!(msg->l2h[2] & LAPDm_EL)) { + /* 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. */ + LOGP(DLLAPD, LOGL_NOTICE, "we don't support " + "multi-octet length\n"); + msgb_free(msg); + rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx); + return -EINVAL; + } + lctx.n201 = n201; + lctx.length = msg->l2h[2] >> 2; + lctx.more = !!(msg->l2h[2] & LAPDm_MORE); + msg->l3h = msg->l2h + 3; + msgb_pull_l2h(msg); + } + /* store context for messages from lapd */ + memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx)); + /* send to LAPD */ + rc = lapd_ph_data_ind(msg, &lctx); break; case LAPDm_FMT_Bter: /* FIXME */ @@ -1684,7 +626,7 @@ static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t cha break; case LAPDm_FMT_Bbis: /* directly pass up to layer3 */ - LOGP(DLLAPDM, LOGL_INFO, "fmt=Bbis UI\n"); + LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n"); msg->l3h = msg->l2h; msgb_pull_l2h(msg); rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg); @@ -1732,7 +674,7 @@ int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le) int rc = 0; if (oph->sap != SAP_GSM_PH) { - LOGP(DLLAPDM, LOGL_ERROR, "primitive for unknown SAP %u\n", + LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n", oph->sap); return -ENODEV; } @@ -1740,7 +682,7 @@ int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le) switch (oph->primitive) { case PRIM_PH_DATA: if (oph->operation != PRIM_OP_INDICATION) { - LOGP(DLLAPDM, LOGL_ERROR, "PH_DATA is not INDICATION %u\n", + LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n", oph->operation); return -ENODEV; } @@ -1749,7 +691,7 @@ int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le) break; case PRIM_PH_RTS: if (oph->operation != PRIM_OP_INDICATION) { - LOGP(DLLAPDM, LOGL_ERROR, "PH_RTS is not INDICATION %u\n", + LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n", oph->operation); return -ENODEV; } @@ -1776,33 +718,44 @@ int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le) /* L3 -> L2 / RSLMS -> LAPDm */ +/* Set LAPDm context for established connection */ +static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr, + uint8_t link_id, int n201, uint8_t sapi) +{ + memset(&dl->mctx, 0, sizeof(dl->mctx)); + dl->mctx.dl = dl; + dl->mctx.chan_nr = chan_nr; + dl->mctx.link_id = link_id; + dl->dl.lctx.dl = &dl->dl; + dl->dl.lctx.n201 = n201; + dl->dl.lctx.sapi = sapi; + + return 0; +} + /* L3 requests establishment of data link */ static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl) { - struct lapdm_entity *le = dl->entity; 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 length; - uint8_t n201 = 23; //FIXME + int n201 = 20; //FIXME + struct osmo_dlsap_prim dp; - /* Set chan_nr and link_id for established connection */ - memset(&dl->mctx, 0, sizeof(dl->mctx)); - dl->mctx.dl = dl; - dl->mctx.n201 = n201; - dl->mctx.chan_nr = chan_nr; - dl->mctx.link_id = link_id; + /* Set LAPDm context for established connection */ + set_lapdm_context(dl, chan_nr, link_id, n201, sapi); - rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); + rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh)); if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { - msg->l3h = TLVP_VAL(&tv, RSL_IE_L3_INFO); + msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO); /* contention resolution establishment procedure */ if (sapi != 0) { /* According to clause 6, the contention resolution * procedure is only permitted with SAPI value 0 */ - LOGP(DLLAPDM, LOGL_ERROR, "SAPI != 0 but contention" + LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention" "resolution (discarding)\n"); msgb_free(msg); return send_rll_simple(RSL_MT_REL_IND, &dl->mctx); @@ -1810,67 +763,41 @@ static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl) /* transmit a SABM command with the P bit set to "1". The SABM * command shall contain the layer 3 message unit */ length = TLVP_LEN(&tv, RSL_IE_L3_INFO); - LOGP(DLLAPDM, LOGL_INFO, "perform establishment with content " - "(SABM)\n"); } else { /* normal establishment procedure */ + msg->l3h = msg->l2h + sizeof(*rllh); length = 0; - LOGP(DLLAPDM, LOGL_INFO, "perform normal establishm. (SABM)\n"); } /* check if the layer3 message length exceeds N201 */ if (length + 3 > 21) { /* FIXME: do we know the channel N201? */ - LOGP(DLLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) " + LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) " "(discarding)\n", length + 3, 21); msgb_free(msg); return send_rll_simple(RSL_MT_REL_IND, &dl->mctx); } - /* Flush send-queue */ - /* Clear send-buffer */ - lapdm_dl_flush_send(dl); - - /* Discard partly received L3 message */ - if (dl->rcv_buffer) { - msgb_free(dl->rcv_buffer); - dl->rcv_buffer = NULL; - } - - /* Remove RLL header from msgb */ + /* Remove RLL header from msgb and set length to L3-info */ msgb_pull_l2h(msg); + msg->len = length; + msg->tail = msg->data + length; - /* Push LAPDm header on msgb */ - msg->l2h = msgb_push(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd); - msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1); - msg->l2h[2] = LAPDm_LEN(length); - /* Transmit-buffer carries exactly one segment */ - memcpy(dl->tx_hist[0], msg->l2h, 3 + length); - dl->tx_length[0] = 3 + length; - /* set Vs to 0, because it is used as index when resending SABM */ - dl->V_send = 0; - - /* Set states */ - dl->own_busy = dl->peer_busy = 0; - dl->retrans_ctr = 0; - lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT); - - /* Tramsmit and start T200 */ - osmo_timer_schedule(&dl->t200, T200); - return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201); + /* prepare prim */ + osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg); + + /* send to L2 */ + return lapd_recv_dlsap(&dp, &dl->dl.lctx); } /* L3 requests transfer of unnumbered information */ static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl) { - struct lapdm_entity *le = dl->entity; 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; int length; - uint8_t n201 = 23; //FIXME uint8_t ta = 0, tx_power = 0; /* check if the layer3 message length exceeds N201 */ @@ -1884,38 +811,40 @@ static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl) tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER); } if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { - LOGP(DLLAPDM, LOGL_ERROR, "unit data request without message " + LOGP(DLLAPD, LOGL_ERROR, "unit data request without message " "error\n"); msgb_free(msg); return -EINVAL; } - msg->l3h = TLVP_VAL(&tv, RSL_IE_L3_INFO); + msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO); length = TLVP_LEN(&tv, RSL_IE_L3_INFO); /* check if the layer3 message length exceeds N201 */ if (length + 5 > 23) { /* FIXME: do we know the channel N201? */ - LOGP(DLLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) " + LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) " "(discarding)\n", length + 5, 23); msgb_free(msg); return -EIO; } - LOGP(DLLAPDM, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n", + LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n", tx_power, ta); - /* Remove RLL header from msgb */ + /* Remove RLL header from msgb and set length to L3-info */ msgb_pull_l2h(msg); + msg->len = length; + msg->tail = msg->data + length; /* Push L1 + LAPDm header on msgb */ msg->l2h = msgb_push(msg, 2 + 3); msg->l2h[0] = tx_power; msg->l2h[1] = ta; - msg->l2h[2] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd); + msg->l2h[2] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd); msg->l2h[3] = LAPDm_CTRL_U(LAPDm_U_UI, 0); msg->l2h[4] = LAPDm_LEN(length); // FIXME: short L2 header support /* Tramsmit */ - return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201); + return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23); } /* L3 requests transfer of acknowledged information */ @@ -1923,143 +852,29 @@ static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl) { struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); struct tlv_parsed tv; + int length; + struct osmo_dlsap_prim dp; rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { - LOGP(DLLAPDM, LOGL_ERROR, "data request without message " + LOGP(DLLAPD, LOGL_ERROR, "data request without message " "error\n"); msgb_free(msg); return -EINVAL; } - msg->l3h = TLVP_VAL(&tv, RSL_IE_L3_INFO); - - LOGP(DLLAPDM, LOGL_INFO, "writing message to send-queue\n"); + msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO); + length = TLVP_LEN(&tv, RSL_IE_L3_INFO); - /* Remove the RSL/RLL header */ + /* Remove RLL header from msgb and set length to L3-info */ msgb_pull_l2h(msg); + msg->len = length; + msg->tail = msg->data + length; - /* Write data into the send queue */ - msgb_enqueue(&dl->send_queue, msg); + /* prepare prim */ + osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg); - /* Send message, if possible */ - rslms_send_i(&dl->mctx, __LINE__); - return 0; -} - -/* Send next I frame from queued/buffered data */ -static int rslms_send_i(struct lapdm_msg_ctx *mctx, int line) -{ - struct lapdm_datalink *dl = mctx->dl; - struct lapdm_entity *le = dl->entity; - uint8_t chan_nr = mctx->chan_nr; - uint8_t link_id = mctx->link_id; - uint8_t sapi = link_id & 7; - int k = k_sapi[sapi]; - struct msgb *msg; - int length, left; - int rc = - 1; /* we sent nothing */ - - LOGP(DLLAPDM, LOGL_INFO, "%s() called from line %d\n", __func__, line); - - next_frame: - - if (dl->peer_busy) { - LOGP(DLLAPDM, LOGL_INFO, "peer busy, not sending\n"); - return rc; - } - - if (dl->state == LAPDm_STATE_TIMER_RECOV) { - LOGP(DLLAPDM, LOGL_INFO, "timer recovery, not sending\n"); - return rc; - } - - /* 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. */ - if (dl->V_send == add_mod8(dl->V_ack, k)) { - LOGP(DLLAPDM, LOGL_INFO, "k frames outstanding, not sending " - "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->V_send, - dl->V_ack); - return rc; - } - - /* if we have no tx_hist yet, we create it */ - if (!dl->tx_length[dl->V_send]) { - /* Get next message into send-buffer, if any */ - if (!dl->send_buffer) { - next_message: - dl->send_out = 0; - dl->send_buffer = msgb_dequeue(&dl->send_queue); - /* No more data to be sent */ - if (!dl->send_buffer) - return rc; - LOGP(DLLAPDM, LOGL_INFO, "get message from " - "send-queue\n"); - } - - /* How much is left in the send-buffer? */ - left = msgb_l3len(dl->send_buffer) - dl->send_out; - /* Segment, if data exceeds N201 */ - length = left; - if (length > mctx->n201 - 3) - length = mctx->n201 - 3; - LOGP(DLLAPDM, LOGL_INFO, "msg-len %d sent %d left %d N201 %d " - "length %d first byte %02x\n", - msgb_l3len(dl->send_buffer), dl->send_out, left, - mctx->n201, length, dl->send_buffer->l3h[0]); - /* If message in send-buffer is completely sent */ - if (left == 0) { - msgb_free(dl->send_buffer); - dl->send_buffer = NULL; - goto next_message; - } - - LOGP(DLLAPDM, LOGL_INFO, "send I frame %sV(S)=%d\n", - (left > length) ? "segment " : "", dl->V_send); - - /* Create I frame (segment) and transmit-buffer content */ - msg = msgb_alloc_headroom(23+10, 10, "LAPDm I"); - msg->l2h = msgb_put(msg, 3 + length); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd); - msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0); - msg->l2h[2] = LAPDm_LEN(length); - if (left > length) - msg->l2h[2] |= LAPDm_MORE; - memcpy(msg->l2h + 3, dl->send_buffer->l3h + dl->send_out, - length); - memcpy(dl->tx_hist[dl->V_send], msg->l2h, 3 + length); - dl->tx_length[dl->V_send] = 3 + length; - /* Add length to track how much is already in the tx buffer */ - dl->send_out += length; - } else { - LOGP(DLLAPDM, LOGL_INFO, "resend I frame from tx buffer " - "V(S)=%d\n", dl->V_send); - - /* Create I frame (segment) from tx_hist */ - length = dl->tx_length[dl->V_send]; - msg = msgb_alloc_headroom(23+10, 10, "LAPDm I"); - msg->l2h = msgb_put(msg, length); - memcpy(msg->l2h, dl->tx_hist[dl->V_send], length); - msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0); - } - - /* 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 (!osmo_timer_pending(&dl->t200)) - osmo_timer_schedule(&dl->t200, T200); - - tx_ph_data_enqueue(dl, msg, chan_nr, link_id, mctx->n201); - - rc = 0; /* we sent something */ - goto next_frame; + /* send to L2 */ + return lapd_recv_dlsap(&dp, &dl->dl.lctx); } /* L3 requests suspension of data link */ @@ -2067,165 +882,79 @@ static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl) { struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); uint8_t sapi = rllh->link_id & 7; + struct osmo_dlsap_prim dp; if (sapi != 0) { - LOGP(DLLAPDM, LOGL_ERROR, "SAPI != 0 while suspending\n"); + LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n"); msgb_free(msg); return -EINVAL; } - LOGP(DLLAPDM, LOGL_INFO, "perform suspension\n"); - - /* put back the send-buffer to the send-queue (first position) */ - if (dl->send_buffer) { - LOGP(DLLAPDM, LOGL_INFO, "put frame in sendbuffer back to " - "queue\n"); - llist_add(&dl->send_buffer->list, &dl->send_queue); - dl->send_buffer = NULL; - } else - LOGP(DLLAPDM, LOGL_INFO, "no frame in sendbuffer\n"); + /* prepare prim */ + osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg); - /* Clear transmit buffer, but keep send buffer */ - lapdm_dl_flush_tx(dl); - - msgb_free(msg); - - return send_rll_simple(RSL_MT_SUSP_CONF, &dl->mctx); + /* send to L2 */ + return lapd_recv_dlsap(&dp, &dl->dl.lctx); } /* L3 requests resume of data link */ static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl) { - struct lapdm_entity *le = dl->entity; struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); + int msg_type = rllh->c.msg_type; 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 length; - uint8_t n201 = 23; //FIXME + uint8_t n201 = 20; //FIXME + struct osmo_dlsap_prim dp; - /* Set chan_nr and link_id for established connection */ - memset(&dl->mctx, 0, sizeof(dl->mctx)); - dl->mctx.dl = dl; - dl->mctx.n201 = n201; - dl->mctx.chan_nr = chan_nr; - dl->mctx.link_id = link_id; + /* Set LAPDm context for established connection */ + set_lapdm_context(dl, chan_nr, link_id, n201, sapi); rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { - LOGP(DLLAPDM, LOGL_ERROR, "resume without message error\n"); + LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n"); msgb_free(msg); return send_rll_simple(RSL_MT_REL_IND, &dl->mctx); } + msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO); length = TLVP_LEN(&tv, RSL_IE_L3_INFO); - LOGP(DLLAPDM, LOGL_INFO, "perform re-establishment (SABM) length=%d\n", - length); - - /* Replace message in the send-buffer (reconnect) */ - if (dl->send_buffer) - msgb_free(dl->send_buffer); - dl->send_out = 0; - if (length) { - /* Remove the RSL/RLL header */ - msgb_pull_l2h(msg); - /* Write data into the send buffer, to be sent first */ - dl->send_buffer = msg; - } + /* Remove RLL header from msgb and set length to L3-info */ + msgb_pull_l2h(msg); + msg->len = length; + msg->tail = msg->data + length; - /* Discard partly received L3 message */ - if (dl->rcv_buffer) { - msgb_free(dl->rcv_buffer); - dl->rcv_buffer = NULL; - } + /* prepare prim */ + osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES + : PRIM_DL_RECON, PRIM_OP_REQUEST, msg); - /* Create new msgb (old one is now free) */ - msg = msgb_alloc_headroom(23+10, 10, "LAPDm SABM"); - msg->l2h = msgb_put(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd); - msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1); - msg->l2h[2] = LAPDm_LEN(0); - /* Transmit-buffer carries exactly one segment */ - memcpy(dl->tx_hist[0], msg->l2h, 3); - dl->tx_length[0] = 3; - /* set Vs to 0, because it is used as index when resending SABM */ - dl->V_send = 0; - - /* Set states */ - dl->own_busy = dl->peer_busy = 0; - dl->retrans_ctr = 0; - lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT); - - /* Tramsmit and start T200 */ - osmo_timer_schedule(&dl->t200, T200); - return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201); + /* send to L2 */ + return lapd_recv_dlsap(&dp, &dl->dl.lctx); } /* L3 requests release of data link */ static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl) { - struct lapdm_entity *le = dl->entity; 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; uint8_t mode = 0; + struct osmo_dlsap_prim dp; /* get release mode */ if (rllh->data[0] == RSL_IE_RELEASE_MODE) mode = rllh->data[1] & 1; - /* local release */ - if (mode) { - LOGP(DLLAPDM, LOGL_INFO, "perform local release\n"); - msgb_free(msg); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - /* enter idle state */ - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - /* flush buffers */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - /* send notification to L3 */ - return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx); - } - - /* in case we are already disconnecting */ - if (dl->state == LAPDm_STATE_DISC_SENT) - return -EBUSY; - - LOGP(DLLAPDM, LOGL_INFO, "perform normal release (DISC)\n"); - /* Pull rllh */ msgb_pull(msg, msg->tail - msg->l2h); - /* Push LAPDm header on msgb */ - msg->l2h = msgb_push(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd); - msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DISC, 1); - msg->l2h[2] = LAPDm_LEN(0); - /* Transmit-buffer carries exactly one segment */ - memcpy(dl->tx_hist[0], msg->l2h, 3); - dl->tx_length[0] = 3; - - /* Set states */ - dl->own_busy = dl->peer_busy = 0; - dl->retrans_ctr = 0; - lapdm_dl_newstate(dl, LAPDm_STATE_DISC_SENT); - - /* Tramsmit and start T200 */ - osmo_timer_schedule(&dl->t200, T200); - return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, dl->mctx.n201); -} + /* prepare prim */ + osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg); + dp.u.rel_req.mode = mode; -/* L3 requests release in idle state */ -static int rslms_rx_rll_rel_req_idle(struct msgb *msg, struct lapdm_datalink *dl) -{ - msgb_free(msg); - - /* send notification to L3 */ - return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx); + /* send to L2 */ + return lapd_recv_dlsap(&dp, &dl->dl.lctx); } /* L3 requests channel in idle state */ @@ -2239,11 +968,11 @@ static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg) PRIM_OP_REQUEST, NULL); if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) { - LOGP(DLLAPDM, LOGL_ERROR, "Message too short for CHAN RQD!\n"); + LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n"); return -EINVAL; } if (cch->data[0] != RSL_IE_REQ_REFERENCE) { - LOGP(DLLAPDM, LOGL_ERROR, "Missing REQ REFERENCE IE\n"); + LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n"); return -EINVAL; } pp.u.rach_req.ra = cch->data[1]; @@ -2251,14 +980,14 @@ static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg) pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7; if (cch->data[4] != RSL_IE_ACCESS_DELAY) { - LOGP(DLLAPDM, LOGL_ERROR, "Missing ACCESS_DELAY IE\n"); + LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n"); return -EINVAL; } /* TA = 0 - delay */ pp.u.rach_req.ta = 0 - cch->data[5]; if (cch->data[6] != RSL_IE_MS_POWER) { - LOGP(DLLAPDM, LOGL_ERROR, "Missing MS POWER IE\n"); + LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n"); return -EINVAL; } pp.u.rach_req.tx_power = cch->data[7]; @@ -2292,65 +1021,6 @@ static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t f return rslms_sendmsg(msg, le); } -const char *lapdm_state_names[] = { - "LAPDm_STATE_NULL", - "LAPDm_STATE_IDLE", - "LAPDm_STATE_SABM_SENT", - "LAPDm_STATE_MF_EST", - "LAPDm_STATE_TIMER_RECOV", - "LAPDm_STATE_DISC_SENT", -}; - -/* statefull handling for RSLms RLL messages from L3 */ -static struct l2downstate { - uint32_t states; - int type; - int (*rout) (struct msgb *msg, struct lapdm_datalink *dl); -} l2downstatelist[] = { - /* create and send UI command */ - {ALL_STATES, - RSL_MT_UNIT_DATA_REQ, rslms_rx_rll_udata_req}, - - /* create and send SABM command */ - {SBIT(LAPDm_STATE_IDLE), - RSL_MT_EST_REQ, rslms_rx_rll_est_req}, - - /* create and send I command */ - {SBIT(LAPDm_STATE_MF_EST) | - SBIT(LAPDm_STATE_TIMER_RECOV), - RSL_MT_DATA_REQ, rslms_rx_rll_data_req}, - - /* suspend datalink */ - {SBIT(LAPDm_STATE_MF_EST) | - SBIT(LAPDm_STATE_TIMER_RECOV), - RSL_MT_SUSP_REQ, rslms_rx_rll_susp_req}, - - /* create and send SABM command (resume) */ - {SBIT(LAPDm_STATE_MF_EST) | - SBIT(LAPDm_STATE_TIMER_RECOV), - RSL_MT_RES_REQ, rslms_rx_rll_res_req}, - - /* create and send SABM command (reconnect) */ - {SBIT(LAPDm_STATE_IDLE) | - SBIT(LAPDm_STATE_MF_EST) | - SBIT(LAPDm_STATE_TIMER_RECOV), - RSL_MT_RECON_REQ, rslms_rx_rll_res_req}, - - /* create and send DISC command */ - {SBIT(LAPDm_STATE_SABM_SENT) | - SBIT(LAPDm_STATE_MF_EST) | - SBIT(LAPDm_STATE_TIMER_RECOV) | - SBIT(LAPDm_STATE_DISC_SENT), - RSL_MT_REL_REQ, rslms_rx_rll_rel_req}, - - /* release in idle state */ - {SBIT(LAPDm_STATE_IDLE), - RSL_MT_REL_REQ, rslms_rx_rll_rel_req_idle}, -}; - -#define L2DOWNSLLEN \ - (sizeof(l2downstatelist) / sizeof(struct l2downstate)) - /* incoming RSLms RLL message from L3 */ static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc) { @@ -2359,11 +1029,10 @@ static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc) uint8_t sapi = rllh->link_id & 7; struct lapdm_entity *le; struct lapdm_datalink *dl; - int i, supported = 0; int rc = 0; if (msgb_l2len(msg) < sizeof(*rllh)) { - LOGP(DLLAPDM, LOGL_ERROR, "Message too short for RLL hdr!\n"); + LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n"); return -EINVAL; } @@ -2377,34 +1046,41 @@ static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc) */ dl = datalink_for_sapi(le, sapi); if (!dl) { - LOGP(DLLAPDM, LOGL_ERROR, "No instance for SAPI %d!\n", sapi); + LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi); return -EINVAL; } - LOGP(DLLAPDM, LOGL_INFO, "(%p) RLL Message '%s' received in state %s\n", - lc->name, rsl_msg_name(msg_type), lapdm_state_names[dl->state]); + LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received.\n", + lc->name, rsl_msg_name(msg_type)); - /* find function for current state and message */ - for (i = 0; i < L2DOWNSLLEN; i++) { - if (msg_type == l2downstatelist[i].type) - supported = 1; - if ((msg_type == l2downstatelist[i].type) - && ((1 << dl->state) & l2downstatelist[i].states)) - break; - } - if (!supported) { - LOGP(DLLAPDM, LOGL_NOTICE, "Message unsupported.\n"); - msgb_free(msg); - return 0; - } - if (i == L2DOWNSLLEN) { - LOGP(DLLAPDM, LOGL_NOTICE, "Message unhandled at this state.\n"); + switch (msg_type) { + case RSL_MT_UNIT_DATA_REQ: + rc = rslms_rx_rll_udata_req(msg, dl); + break; + case RSL_MT_EST_REQ: + rc = rslms_rx_rll_est_req(msg, dl); + break; + case RSL_MT_DATA_REQ: + rc = rslms_rx_rll_data_req(msg, dl); + break; + case RSL_MT_SUSP_REQ: + rc = rslms_rx_rll_susp_req(msg, dl); + break; + case RSL_MT_RES_REQ: + rc = rslms_rx_rll_res_req(msg, dl); + break; + case RSL_MT_RECON_REQ: + rc = rslms_rx_rll_res_req(msg, dl); + break; + case RSL_MT_REL_REQ: + rc = rslms_rx_rll_rel_req(msg, dl); + break; + default: + LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n"); msgb_free(msg); - return 0; + rc = -EINVAL; } - rc = l2downstatelist[i].rout(msg, dl); - return rc; } @@ -2416,7 +1092,7 @@ static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc) int rc = 0; if (msgb_l2len(msg) < sizeof(*cch)) { - LOGP(DLLAPDM, LOGL_ERROR, "Message too short for COM CHAN hdr!\n"); + LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n"); return -EINVAL; } @@ -2426,7 +1102,7 @@ static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc) rc = rslms_rx_chan_rqd(lc, msg); break; default: - LOGP(DLLAPDM, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n", + LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n", msg_type); msgb_free(msg); return 0; @@ -2442,7 +1118,7 @@ int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc) int rc = 0; if (msgb_l2len(msg) < sizeof(*rslh)) { - LOGP(DLLAPDM, LOGL_ERROR, "Message too short RSL hdr!\n"); + LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n"); return -EINVAL; } @@ -2454,7 +1130,7 @@ int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc) rc = rslms_rx_com_chan(msg, lc); break; default: - LOGP(DLLAPDM, LOGL_ERROR, "unknown RSLms message " + LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message " "discriminator 0x%02x", rslh->msg_discr); msgb_free(msg); return -EINVAL; @@ -2466,23 +1142,24 @@ int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc) /*! \brief Set the \ref lapdm_mode of a LAPDm entity */ int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode) { + int i; + enum lapd_mode lm; + switch (mode) { case LAPDM_MODE_MS: - le->cr.loc2rem.cmd = CR_MS2BS_CMD; - le->cr.loc2rem.resp = CR_MS2BS_RESP; - le->cr.rem2loc.cmd = CR_BS2MS_CMD; - le->cr.rem2loc.resp = CR_BS2MS_RESP; + lm = LAPD_MODE_USER; break; case LAPDM_MODE_BTS: - le->cr.loc2rem.cmd = CR_BS2MS_CMD; - le->cr.loc2rem.resp = CR_BS2MS_RESP; - le->cr.rem2loc.cmd = CR_MS2BS_CMD; - le->cr.rem2loc.resp = CR_MS2BS_RESP; + lm = LAPD_MODE_NETWORK; break; default: return -EINVAL; } + for (i = 0; i < ARRAY_SIZE(le->datalink); i++) { + lapd_set_mode(&le->datalink[i].dl, lm); + } + le->mode = mode; return 0; @@ -2526,16 +1203,7 @@ void lapdm_entity_reset(struct lapdm_entity *le) for (i = 0; i < ARRAY_SIZE(le->datalink); i++) { dl = &le->datalink[i]; - if (dl->state == LAPDm_STATE_IDLE) - continue; - LOGP(DLLAPDM, LOGL_INFO, "Resetting LAPDm instance\n"); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - /* enter idle state */ - dl->state = LAPDm_STATE_IDLE; - /* flush buffer */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); + lapd_dl_reset(&dl->dl); } } diff --git a/src/shared/libosmocore/src/gsmtap_util.c b/src/shared/libosmocore/src/gsmtap_util.c index 0e4d61e5..ce722da9 100644 --- a/src/shared/libosmocore/src/gsmtap_util.c +++ b/src/shared/libosmocore/src/gsmtap_util.c @@ -88,7 +88,8 @@ uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id) return ret; } -/*! \brief create L1/L2 data and put it into GSMTAP +/*! \brief create an arbitrary type GSMTAP message + * \param[in] type The GSMTAP_TYPE_xxx constant of the message to create * \param[in] arfcn GSM ARFCN (Channel Number) * \param[in] ts GSM time slot * \param[in] chan_type Channel Type @@ -102,7 +103,7 @@ uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id) * This function will allocate a new msgb and fill it with a GSMTAP * header containing the information */ -struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, +struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type, uint8_t ss, uint32_t fn, int8_t signal_dbm, uint8_t snr, const uint8_t *data, unsigned int len) { @@ -118,7 +119,7 @@ struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, gh->version = GSMTAP_VERSION; gh->hdr_len = sizeof(*gh)/4; - gh->type = GSMTAP_TYPE_UM; + gh->type = type; gh->timeslot = ts; gh->sub_slot = ss; gh->arfcn = htons(arfcn); @@ -134,6 +135,28 @@ struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, return msg; } +/*! \brief create L1/L2 data and put it into GSMTAP + * \param[in] arfcn GSM ARFCN (Channel Number) + * \param[in] ts GSM time slot + * \param[in] chan_type Channel Type + * \param[in] ss Sub-slot + * \param[in] fn GSM Frame Number + * \param[in] signal_dbm Signal Strength (dBm) + * \param[in] snr Signal/Noise Ratio (SNR) + * \param[in] data Pointer to data buffer + * \param[in] len Length of \ref data + * + * This function will allocate a new msgb and fill it with a GSMTAP + * header containing the information + */ +struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, + uint8_t ss, uint32_t fn, int8_t signal_dbm, + uint8_t snr, const uint8_t *data, unsigned int len) +{ + return gsmtap_makemsg_ex(GSMTAP_TYPE_UM, arfcn, ts, chan_type, + ss, fn, signal_dbm, snr, data, len); +} + #ifdef HAVE_SYS_SOCKET_H #include <sys/socket.h> @@ -208,8 +231,10 @@ int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg) } } -/*! \brief receive a message from L1/L2 and put it in GSMTAP */ -int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts, +/*! \brief send an arbitrary type through GSMTAP. + * See \ref gsmtap_makemsg_ex for arguments + */ +int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type, uint8_t ss, uint32_t fn, int8_t signal_dbm, uint8_t snr, const uint8_t *data, unsigned int len) @@ -219,7 +244,7 @@ int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts, if (!gti) return -ENODEV; - msg = gsmtap_makemsg(arfcn, ts, chan_type, ss, fn, signal_dbm, + msg = gsmtap_makemsg_ex(type, arfcn, ts, chan_type, ss, fn, signal_dbm, snr, data, len); if (!msg) return -ENOMEM; @@ -227,6 +252,18 @@ int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts, return gsmtap_sendmsg(gti, msg); } +/*! \brief send a message from L1/L2 through GSMTAP. + * See \ref gsmtap_makemsg for arguments + */ +int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts, + uint8_t chan_type, uint8_t ss, uint32_t fn, + int8_t signal_dbm, uint8_t snr, const uint8_t *data, + unsigned int len) +{ + return gsmtap_send_ex(gti, GSMTAP_TYPE_UM, arfcn, ts, chan_type, ss, fn, + signal_dbm, snr, data, len); +} + /* Callback from select layer if we can write to the socket */ static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg) { diff --git a/src/shared/libosmocore/src/logging.c b/src/shared/libosmocore/src/logging.c index 9dd63e73..db00331a 100644 --- a/src/shared/libosmocore/src/logging.c +++ b/src/shared/libosmocore/src/logging.c @@ -72,9 +72,9 @@ static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = { .loglevel = LOGL_NOTICE, .enabled = 1, }, - [INT2IDX(DLLAPDM)] = { /* -2 becomes 1 */ - .name = "DLLAPDM", - .description = "LAPDm in libosmogsm", + [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */ + .name = "DLLAPD", + .description = "LAPD in libosmogsm", .loglevel = LOGL_NOTICE, .enabled = 1, }, diff --git a/src/shared/libosmocore/src/rbtree.c b/src/shared/libosmocore/src/rbtree.c new file mode 100644 index 00000000..07e1041a --- /dev/null +++ b/src/shared/libosmocore/src/rbtree.c @@ -0,0 +1,389 @@ +/* + Red Black Trees + (C) 1999 Andrea Arcangeli <andrea@suse.de> + (C) 2002 David Woodhouse <dwmw2@infradead.org> + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + linux/lib/rbtree.c +*/ + +#include <osmocom/core/linuxrbtree.h> + +static void __rb_rotate_left(struct rb_node *node, struct rb_root *root) +{ + struct rb_node *right = node->rb_right; + struct rb_node *parent = rb_parent(node); + + if ((node->rb_right = right->rb_left)) + rb_set_parent(right->rb_left, node); + right->rb_left = node; + + rb_set_parent(right, parent); + + if (parent) + { + if (node == parent->rb_left) + parent->rb_left = right; + else + parent->rb_right = right; + } + else + root->rb_node = right; + rb_set_parent(node, right); +} + +static void __rb_rotate_right(struct rb_node *node, struct rb_root *root) +{ + struct rb_node *left = node->rb_left; + struct rb_node *parent = rb_parent(node); + + if ((node->rb_left = left->rb_right)) + rb_set_parent(left->rb_right, node); + left->rb_right = node; + + rb_set_parent(left, parent); + + if (parent) + { + if (node == parent->rb_right) + parent->rb_right = left; + else + parent->rb_left = left; + } + else + root->rb_node = left; + rb_set_parent(node, left); +} + +void rb_insert_color(struct rb_node *node, struct rb_root *root) +{ + struct rb_node *parent, *gparent; + + while ((parent = rb_parent(node)) && rb_is_red(parent)) + { + gparent = rb_parent(parent); + + if (parent == gparent->rb_left) + { + { + register struct rb_node *uncle = gparent->rb_right; + if (uncle && rb_is_red(uncle)) + { + rb_set_black(uncle); + rb_set_black(parent); + rb_set_red(gparent); + node = gparent; + continue; + } + } + + if (parent->rb_right == node) + { + register struct rb_node *tmp; + __rb_rotate_left(parent, root); + tmp = parent; + parent = node; + node = tmp; + } + + rb_set_black(parent); + rb_set_red(gparent); + __rb_rotate_right(gparent, root); + } else { + { + register struct rb_node *uncle = gparent->rb_left; + if (uncle && rb_is_red(uncle)) + { + rb_set_black(uncle); + rb_set_black(parent); + rb_set_red(gparent); + node = gparent; + continue; + } + } + + if (parent->rb_left == node) + { + register struct rb_node *tmp; + __rb_rotate_right(parent, root); + tmp = parent; + parent = node; + node = tmp; + } + + rb_set_black(parent); + rb_set_red(gparent); + __rb_rotate_left(gparent, root); + } + } + + rb_set_black(root->rb_node); +} + +static void __rb_erase_color(struct rb_node *node, struct rb_node *parent, + struct rb_root *root) +{ + struct rb_node *other; + + while ((!node || rb_is_black(node)) && node != root->rb_node) + { + if (parent->rb_left == node) + { + other = parent->rb_right; + if (rb_is_red(other)) + { + rb_set_black(other); + rb_set_red(parent); + __rb_rotate_left(parent, root); + other = parent->rb_right; + } + if ((!other->rb_left || rb_is_black(other->rb_left)) && + (!other->rb_right || rb_is_black(other->rb_right))) + { + rb_set_red(other); + node = parent; + parent = rb_parent(node); + } + else + { + if (!other->rb_right || rb_is_black(other->rb_right)) + { + struct rb_node *o_left; + if ((o_left = other->rb_left)) + rb_set_black(o_left); + rb_set_red(other); + __rb_rotate_right(other, root); + other = parent->rb_right; + } + rb_set_color(other, rb_color(parent)); + rb_set_black(parent); + if (other->rb_right) + rb_set_black(other->rb_right); + __rb_rotate_left(parent, root); + node = root->rb_node; + break; + } + } + else + { + other = parent->rb_left; + if (rb_is_red(other)) + { + rb_set_black(other); + rb_set_red(parent); + __rb_rotate_right(parent, root); + other = parent->rb_left; + } + if ((!other->rb_left || rb_is_black(other->rb_left)) && + (!other->rb_right || rb_is_black(other->rb_right))) + { + rb_set_red(other); + node = parent; + parent = rb_parent(node); + } + else + { + if (!other->rb_left || rb_is_black(other->rb_left)) + { + register struct rb_node *o_right; + if ((o_right = other->rb_right)) + rb_set_black(o_right); + rb_set_red(other); + __rb_rotate_left(other, root); + other = parent->rb_left; + } + rb_set_color(other, rb_color(parent)); + rb_set_black(parent); + if (other->rb_left) + rb_set_black(other->rb_left); + __rb_rotate_right(parent, root); + node = root->rb_node; + break; + } + } + } + if (node) + rb_set_black(node); +} + +void rb_erase(struct rb_node *node, struct rb_root *root) +{ + struct rb_node *child, *parent; + int color; + + if (!node->rb_left) + child = node->rb_right; + else if (!node->rb_right) + child = node->rb_left; + else + { + struct rb_node *old = node, *left; + + node = node->rb_right; + while ((left = node->rb_left) != NULL) + node = left; + child = node->rb_right; + parent = rb_parent(node); + color = rb_color(node); + + if (child) + rb_set_parent(child, parent); + if (parent == old) { + parent->rb_right = child; + parent = node; + } else + parent->rb_left = child; + + node->rb_parent_color = old->rb_parent_color; + node->rb_right = old->rb_right; + node->rb_left = old->rb_left; + + if (rb_parent(old)) + { + if (rb_parent(old)->rb_left == old) + rb_parent(old)->rb_left = node; + else + rb_parent(old)->rb_right = node; + } else + root->rb_node = node; + + rb_set_parent(old->rb_left, node); + if (old->rb_right) + rb_set_parent(old->rb_right, node); + goto color; + } + + parent = rb_parent(node); + color = rb_color(node); + + if (child) + rb_set_parent(child, parent); + if (parent) + { + if (parent->rb_left == node) + parent->rb_left = child; + else + parent->rb_right = child; + } + else + root->rb_node = child; + + color: + if (color == RB_BLACK) + __rb_erase_color(child, parent, root); +} + +/* + * This function returns the first node (in sort order) of the tree. + */ +struct rb_node *rb_first(struct rb_root *root) +{ + struct rb_node *n; + + n = root->rb_node; + if (!n) + return NULL; + while (n->rb_left) + n = n->rb_left; + return n; +} + +struct rb_node *rb_last(struct rb_root *root) +{ + struct rb_node *n; + + n = root->rb_node; + if (!n) + return NULL; + while (n->rb_right) + n = n->rb_right; + return n; +} + +struct rb_node *rb_next(struct rb_node *node) +{ + struct rb_node *parent; + + if (rb_parent(node) == node) + return NULL; + + /* If we have a right-hand child, go down and then left as far + as we can. */ + if (node->rb_right) { + node = node->rb_right; + while (node->rb_left) + node=node->rb_left; + return node; + } + + /* No right-hand children. Everything down and left is + smaller than us, so any 'next' node must be in the general + direction of our parent. Go up the tree; any time the + ancestor is a right-hand child of its parent, keep going + up. First time it's a left-hand child of its parent, said + parent is our 'next' node. */ + while ((parent = rb_parent(node)) && node == parent->rb_right) + node = parent; + + return parent; +} + +struct rb_node *rb_prev(struct rb_node *node) +{ + struct rb_node *parent; + + if (rb_parent(node) == node) + return NULL; + + /* If we have a left-hand child, go down and then right as far + as we can. */ + if (node->rb_left) { + node = node->rb_left; + while (node->rb_right) + node=node->rb_right; + return node; + } + + /* No left-hand children. Go up till we find an ancestor which + is a right-hand child of its parent */ + while ((parent = rb_parent(node)) && node == parent->rb_left) + node = parent; + + return parent; +} + +void rb_replace_node(struct rb_node *victim, struct rb_node *new, + struct rb_root *root) +{ + struct rb_node *parent = rb_parent(victim); + + /* Set the surrounding nodes to point to the replacement */ + if (parent) { + if (victim == parent->rb_left) + parent->rb_left = new; + else + parent->rb_right = new; + } else { + root->rb_node = new; + } + if (victim->rb_left) + rb_set_parent(victim->rb_left, new); + if (victim->rb_right) + rb_set_parent(victim->rb_right, new); + + /* Copy the pointers/colour from the victim to the replacement */ + *new = *victim; +} diff --git a/src/shared/libosmocore/src/serial.c b/src/shared/libosmocore/src/serial.c index 26cf59d7..e61edb71 100644 --- a/src/shared/libosmocore/src/serial.c +++ b/src/shared/libosmocore/src/serial.c @@ -38,8 +38,9 @@ #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h> +#ifdef __linux__ #include <linux/serial.h> - +#endif #include <osmocom/core/serial.h> @@ -155,6 +156,7 @@ osmo_serial_set_baudrate(int fd, speed_t baudrate) int osmo_serial_set_custom_baudrate(int fd, int baudrate) { +#ifdef __linux__ int rc; struct serial_struct ser_info; @@ -174,6 +176,23 @@ osmo_serial_set_custom_baudrate(int fd, int baudrate) } return _osmo_serial_set_baudrate(fd, B38400); /* 38400 is a kind of magic ... */ +#elif defined(__APPLE__) +#ifndef IOSSIOSPEED +#define IOSSIOSPEED _IOW('T', 2, speed_t) +#endif + int rc; + + unsigned int speed = baudrate; + rc = ioctl(fd, IOSSIOSPEED, &speed); + if (rc < 0) { + dbg_perror("ioctl(IOSSIOSPEED)"); + return -errno; + } + return 0; +#else +#warning osmo_serial_set_custom_baudrate: unsupported platform + return 0; +#endif } /*! \brief Clear any custom baudrate @@ -186,6 +205,7 @@ int osmo_serial_clear_custom_baudrate(int fd) { int rc; +#ifdef __linux__ struct serial_struct ser_info; rc = ioctl(fd, TIOCGSERIAL, &ser_info); @@ -202,7 +222,7 @@ osmo_serial_clear_custom_baudrate(int fd) dbg_perror("ioctl(TIOCSSERIAL)"); return -errno; } - +#endif return 0; } diff --git a/src/shared/libosmocore/src/timer.c b/src/shared/libosmocore/src/timer.c index ed2b2963..217f6521 100644 --- a/src/shared/libosmocore/src/timer.c +++ b/src/shared/libosmocore/src/timer.c @@ -1,7 +1,12 @@ /* * (C) 2008,2009 by Holger Hans Peter Freyther <zecke@selfish.org> + * (C) 2011 by Harald Welte <laforge@gnumonks.org> * All Rights Reserved * + * Authors: Holger Hans Peter Freyther <zecke@selfish.org> + * Harald Welte <laforge@gnumonks.org> + * Pablo Neira Ayuso <pablo@gnumonks.org> + * * 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 @@ -18,6 +23,10 @@ * */ +/* These store the amount of time that we wait until next timer expires. */ +static struct timeval nearest; +static struct timeval *nearest_p; + /*! \addtogroup timer * @{ */ @@ -27,35 +36,42 @@ #include <assert.h> #include <string.h> +#include <limits.h> #include <osmocom/core/timer.h> +#include <osmocom/core/timer_compat.h> +#include <osmocom/core/linuxlist.h> + +static struct rb_root timer_root = RB_ROOT; + +static void __add_timer(struct osmo_timer_list *timer) +{ + struct rb_node **new = &(timer_root.rb_node); + struct rb_node *parent = NULL; -static LLIST_HEAD(timer_list); -static struct timeval s_nearest_time; -static struct timeval s_select_time; + while (*new) { + struct osmo_timer_list *this; -#define MICRO_SECONDS 1000000LL + this = container_of(*new, struct osmo_timer_list, node); -#define TIME_SMALLER(left, right) \ - (left.tv_sec*MICRO_SECONDS+left.tv_usec) <= (right.tv_sec*MICRO_SECONDS+right.tv_usec) + parent = *new; + if (timercmp(&timer->timeout, &this->timeout, <)) + new = &((*new)->rb_left); + else + new = &((*new)->rb_right); + } + rb_link_node(&timer->node, parent, new); + rb_insert_color(&timer->node, &timer_root); +} /*! \brief add a new timer to the timer management * \param[in] timer the timer that should be added */ void osmo_timer_add(struct osmo_timer_list *timer) { - struct osmo_timer_list *list_timer; - - /* TODO: Optimize and remember the closest item... */ timer->active = 1; - - /* this might be called from within update_timers */ - llist_for_each_entry(list_timer, &timer_list, entry) - if (timer == list_timer) - return; - - timer->in_list = 1; - llist_add(&timer->entry, &timer_list); + INIT_LLIST_HEAD(&timer->list); + __add_timer(timer); } /*! \brief schedule a timer at a given future relative time @@ -74,10 +90,9 @@ osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds struct timeval current_time; gettimeofday(¤t_time, NULL); - unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec; - currentTime += seconds * MICRO_SECONDS + microseconds; - timer->timeout.tv_sec = currentTime / MICRO_SECONDS; - timer->timeout.tv_usec = currentTime % MICRO_SECONDS; + timer->timeout.tv_sec = seconds; + timer->timeout.tv_usec = microseconds; + timeradd(&timer->timeout, ¤t_time, &timer->timeout); osmo_timer_add(timer); } @@ -89,10 +104,12 @@ osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds */ void osmo_timer_del(struct osmo_timer_list *timer) { - if (timer->in_list) { + if (timer->active) { timer->active = 0; - timer->in_list = 0; - llist_del(&timer->entry); + rb_erase(&timer->node, &timer_root); + /* make sure this is not already scheduled for removal. */ + if (!llist_empty(&timer->list)) + llist_del_init(&timer->list); } } @@ -116,26 +133,26 @@ int osmo_timer_pending(struct osmo_timer_list *timer) */ struct timeval *osmo_timers_nearest(void) { - struct timeval current_time; - - if (s_nearest_time.tv_sec == 0 && s_nearest_time.tv_usec == 0) - return NULL; - - if (gettimeofday(¤t_time, NULL) == -1) - return NULL; - - unsigned long long nearestTime = s_nearest_time.tv_sec * MICRO_SECONDS + s_nearest_time.tv_usec; - unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec; + /* nearest_p is exactly what we need already: NULL if nothing is + * waiting, {0,0} if we must dispatch immediately, and the correct + * delay if we need to wait */ + return nearest_p; +} - if (nearestTime < currentTime) { - s_select_time.tv_sec = 0; - s_select_time.tv_usec = 0; +static void update_nearest(struct timeval *cand, struct timeval *current) +{ + if (cand->tv_sec != LONG_MAX) { + if (timercmp(cand, current, >)) + timersub(cand, current, &nearest); + else { + /* loop again inmediately */ + nearest.tv_sec = 0; + nearest.tv_usec = 0; + } + nearest_p = &nearest; } else { - s_select_time.tv_sec = (nearestTime - currentTime) / MICRO_SECONDS; - s_select_time.tv_usec = (nearestTime - currentTime) % MICRO_SECONDS; + nearest_p = NULL; } - - return &s_select_time; } /* @@ -143,17 +160,18 @@ struct timeval *osmo_timers_nearest(void) */ void osmo_timers_prepare(void) { - struct osmo_timer_list *timer, *nearest_timer = NULL; - llist_for_each_entry(timer, &timer_list, entry) { - if (!nearest_timer || TIME_SMALLER(timer->timeout, nearest_timer->timeout)) { - nearest_timer = timer; - } - } + struct rb_node *node; + struct timeval current; + + gettimeofday(¤t, NULL); - if (nearest_timer) { - s_nearest_time = nearest_timer->timeout; + node = rb_first(&timer_root); + if (node) { + struct osmo_timer_list *this; + this = container_of(node, struct osmo_timer_list, node); + update_nearest(&this->timeout, ¤t); } else { - memset(&s_nearest_time, 0, sizeof(struct timeval)); + nearest_p = NULL; } } @@ -163,46 +181,41 @@ void osmo_timers_prepare(void) int osmo_timers_update(void) { struct timeval current_time; - struct osmo_timer_list *timer, *tmp; + struct rb_node *node; + struct llist_head timer_eviction_list; + struct osmo_timer_list *this; int work = 0; gettimeofday(¤t_time, NULL); + INIT_LLIST_HEAD(&timer_eviction_list); + for (node = rb_first(&timer_root); node; node = rb_next(node)) { + this = container_of(node, struct osmo_timer_list, node); + + if (timercmp(&this->timeout, ¤t_time, >)) + break; + + llist_add(&this->list, &timer_eviction_list); + } + /* * The callbacks might mess with our list and in this case * even llist_for_each_entry_safe is not safe to use. To allow - * del_timer, add_timer, schedule_timer to be called from within - * the callback we jump through some loops. + * osmo_timer_del to be called from within the callback we need + * to restart the iteration for each element scheduled for removal. * - * First we set the handled flag of each active timer to zero, - * then we iterate over the list and execute the callbacks. As the - * list might have been changed (specially the next) from within - * the callback we have to start over again. Once every callback - * is dispatched we will remove the non-active from the list. - * - * TODO: If this is a performance issue we can poison a global - * variable in add_timer and del_timer and only then restart. + * The problematic scenario is the following: Given two timers A + * and B that have expired at the same time. Thus, they are both + * in the eviction list in this order: A, then B. If we remove + * timer B from the A's callback, we continue with B in the next + * iteration step, leading to an access-after-release. */ - llist_for_each_entry(timer, &timer_list, entry) { - timer->handled = 0; - } - restart: - llist_for_each_entry(timer, &timer_list, entry) { - if (!timer->handled && TIME_SMALLER(timer->timeout, current_time)) { - timer->handled = 1; - timer->active = 0; - (*timer->cb)(timer->data); - work = 1; - goto restart; - } - } - - llist_for_each_entry_safe(timer, tmp, &timer_list, entry) { - timer->handled = 0; - if (!timer->active) { - osmo_timer_del(timer); - } + llist_for_each_entry(this, &timer_eviction_list, list) { + osmo_timer_del(this); + this->cb(this->data); + work = 1; + goto restart; } return work; @@ -210,10 +223,10 @@ restart: int osmo_timers_check(void) { - struct osmo_timer_list *timer; + struct rb_node *node; int i = 0; - llist_for_each_entry(timer, &timer_list, entry) { + for (node = rb_first(&timer_root); node; node = rb_next(node)) { i++; } return i; diff --git a/src/shared/libosmocore/src/vty/logging_vty.c b/src/shared/libosmocore/src/vty/logging_vty.c index b037a5bc..6be30b4a 100644 --- a/src/shared/libosmocore/src/vty/logging_vty.c +++ b/src/shared/libosmocore/src/vty/logging_vty.c @@ -185,9 +185,10 @@ DEFUN(logging_level, DEFUN(logging_set_category_mask, logging_set_category_mask_cmd, - "logging set log mask MASK", + "logging set-log-mask MASK", LOGGING_STR - "Decide which categories to output.\n") + "Set the logmask of this logging target\n" + "The logmask to use\n") { struct log_target *tgt = osmo_log_vty2tgt(vty); @@ -198,6 +199,14 @@ DEFUN(logging_set_category_mask, return CMD_SUCCESS; } +ALIAS_DEPRECATED(logging_set_category_mask, + logging_set_category_mask_old_cmd, + "logging set log mask MASK", + LOGGING_STR + "Decide which categories to output.\n" + "Log commands\n" "Mask commands\n" "The logmask to use\n"); + + DEFUN(diable_logging, disable_logging_cmd, "logging disable", @@ -381,7 +390,16 @@ static struct value_string sysl_level_names[] = { DEFUN(cfg_log_syslog, cfg_log_syslog_cmd, "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)", - LOG_STR "Logging via syslog\n") + LOG_STR "Logging via syslog\n" + "Security/authorization messages facility\n" + "Clock daemon (cron/at) facility\n" + "General system daemon facility\n" + "Ftp daemon facility\n" + "Line printer facility\n" + "Mail facility\n" + "News facility\n" + "Generic facility\n" + "UUCP facility\n") { int facility = get_string_value(sysl_level_names, argv[0]); @@ -560,6 +578,7 @@ void logging_vty_add_cmds(const struct log_info *cat) install_element_ve(&logging_use_clr_cmd); install_element_ve(&logging_prnt_timestamp_cmd); install_element_ve(&logging_set_category_mask_cmd); + install_element_ve(&logging_set_category_mask_old_cmd); /* Logging level strings are generated dynamically. */ logging_level_cmd.string = log_vty_command_string(cat); diff --git a/src/shared/libosmocore/tests/timer/timer_test.c b/src/shared/libosmocore/tests/timer/timer_test.c index 240bc480..bcaafdb2 100644 --- a/src/shared/libosmocore/tests/timer/timer_test.c +++ b/src/shared/libosmocore/tests/timer/timer_test.c @@ -1,7 +1,11 @@ /* * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org> + * (C) 2011 by Harald Welte <laforge@gnumonks.org> * All Rights Reserved * + * Authors: Holger Hans Peter Freyther <zecke@selfish.org> + * Pablo Neira Ayuso <pablo@gnumonks.org> + * * 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 @@ -19,59 +23,130 @@ */ #include <stdio.h> +#include <stdlib.h> +#include <osmocom/core/talloc.h> #include <osmocom/core/timer.h> #include <osmocom/core/select.h> +#include <osmocom/core/linuxlist.h> #include "../../config.h" -static void timer_fired(void *data); +static void main_timer_fired(void *data); +static void secondary_timer_fired(void *data); -static struct osmo_timer_list timer_one = { - .cb = timer_fired, - .data = (void*)1, +static unsigned int main_timer_step = 0; +static struct osmo_timer_list main_timer = { + .cb = main_timer_fired, + .data = &main_timer_step, }; -static struct osmo_timer_list timer_two = { - .cb = timer_fired, - .data = (void*)2, -}; +static LLIST_HEAD(timer_test_list); -static struct osmo_timer_list timer_three = { - .cb = timer_fired, - .data = (void*)3, +struct test_timer { + struct llist_head head; + struct osmo_timer_list timer; + struct timeval start; + struct timeval stop; }; -static void timer_fired(void *_data) +/* number of test steps. We add fact(steps) timers in the whole test. */ +#define MAIN_TIMER_NSTEPS 16 + +/* time between two steps, in secs. */ +#define TIME_BETWEEN_STEPS 1 + +/* timer imprecision that we accept for this test: 10 milliseconds. */ +#define TIMER_PRES_SECS 0 +#define TIMER_PRES_USECS 10000 + +static unsigned int expired_timers = 0; +static unsigned int total_timers = 0; +static unsigned int too_late = 0; + +static void main_timer_fired(void *data) +{ + unsigned int *step = data; + unsigned int add_in_this_step; + int i; + + if (*step == MAIN_TIMER_NSTEPS) { + printf("Main timer has finished, please, wait a bit for the " + "final report.\n"); + return; + } + /* add 2^step pair of timers per step. */ + add_in_this_step = (1 << *step); + + for (i=0; i<add_in_this_step; i++) { + struct test_timer *v; + + v = talloc_zero(NULL, struct test_timer); + if (v == NULL) { + fprintf(stderr, "timer_test: OOM!\n"); + return; + } + gettimeofday(&v->start, NULL); + v->timer.cb = secondary_timer_fired; + v->timer.data = v; + unsigned int seconds = (random() % 10) + 1; + v->stop.tv_sec = v->start.tv_sec + seconds; + osmo_timer_schedule(&v->timer, seconds, 0); + llist_add(&v->head, &timer_test_list); + } + printf("added %d timers in step %u (expired=%u)\n", + add_in_this_step, *step, expired_timers); + total_timers += add_in_this_step; + osmo_timer_schedule(&main_timer, TIME_BETWEEN_STEPS, 0); + (*step)++; +} + +static void secondary_timer_fired(void *data) { - unsigned long data = (unsigned long) _data; - printf("Fired timer: %lu\n", data); - - if (data == 1) { - osmo_timer_schedule(&timer_one, 3, 0); - osmo_timer_del(&timer_two); - } else if (data == 2) { - printf("Should not be fired... bug in del_timer\n"); - } else if (data == 3) { - printf("Timer fired not registering again\n"); - } else { - printf("wtf... wrong data\n"); - } + struct test_timer *v = data, *this, *tmp; + struct timeval current, res, precision = { 1, 0 }; + + gettimeofday(¤t, NULL); + + timersub(¤t, &v->stop, &res); + if (timercmp(&res, &precision, >)) { + printf("ERROR: timer %p has expired too late!\n", v->timer); + too_late++; + } + + llist_del(&v->head); + talloc_free(data); + expired_timers++; + if (expired_timers == total_timers) { + printf("test over: added=%u expired=%u too_late=%u \n", + total_timers, expired_timers, too_late); + exit(EXIT_SUCCESS); + } + + /* randomly (10%) deletion of timers. */ + llist_for_each_entry_safe(this, tmp, &timer_test_list, head) { + if ((random() % 100) < 10) { + osmo_timer_del(&this->timer); + llist_del(&this->head); + talloc_free(this); + expired_timers++; + } + } } int main(int argc, char** argv) { - printf("Starting... timer\n"); + printf("Running timer test for %u steps, accepting imprecision " + "of %u.%.6u seconds\n", + MAIN_TIMER_NSTEPS, TIMER_PRES_SECS, TIMER_PRES_USECS); - osmo_timer_schedule(&timer_one, 3, 0); - osmo_timer_schedule(&timer_two, 5, 0); - osmo_timer_schedule(&timer_three, 4, 0); + osmo_timer_schedule(&main_timer, 1, 0); #ifdef HAVE_SYS_SELECT_H - while (1) { - osmo_select_main(0); - } + while (1) { + osmo_select_main(0); + } #else - printf("Select not supported on this platform!\n"); + printf("Select not supported on this platform!\n"); #endif } |