fix master Makefile with layer23 rename
[osmocom-bb.git] / src / shared / libosmocore / src / rsl.c
1 /* GSM Radio Signalling Link messages on the A-bis interface 
2  * 3GPP TS 08.58 version 8.6.0 Release 1999 / ETSI TS 100 596 V8.6.0 */
3
4 /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
5  *
6  * All Rights Reserved
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  */
23
24 #include <osmocore/tlv.h>
25 #include <osmocore/rsl.h>
26
27 void rsl_init_rll_hdr(struct abis_rsl_rll_hdr *dh, uint8_t msg_type)
28 {
29         dh->c.msg_discr = ABIS_RSL_MDISC_RLL;
30         dh->c.msg_type = msg_type;
31         dh->ie_chan = RSL_IE_CHAN_NR;
32         dh->ie_link_id = RSL_IE_LINK_IDENT;
33 }
34
35 const struct tlv_definition rsl_att_tlvdef = {
36         .def = {
37                 [RSL_IE_CHAN_NR]                = { TLV_TYPE_TV },
38                 [RSL_IE_LINK_IDENT]             = { TLV_TYPE_TV },
39                 [RSL_IE_ACT_TYPE]               = { TLV_TYPE_TV },
40                 [RSL_IE_BS_POWER]               = { TLV_TYPE_TV },
41                 [RSL_IE_CHAN_IDENT]             = { TLV_TYPE_TLV },
42                 [RSL_IE_CHAN_MODE]              = { TLV_TYPE_TLV },
43                 [RSL_IE_ENCR_INFO]              = { TLV_TYPE_TLV },
44                 [RSL_IE_FRAME_NUMBER]           = { TLV_TYPE_FIXED, 2 },
45                 [RSL_IE_HANDO_REF]              = { TLV_TYPE_TV },
46                 [RSL_IE_L1_INFO]                = { TLV_TYPE_FIXED, 2 },
47                 [RSL_IE_L3_INFO]                = { TLV_TYPE_TL16V },
48                 [RSL_IE_MS_IDENTITY]            = { TLV_TYPE_TLV },
49                 [RSL_IE_MS_POWER]               = { TLV_TYPE_TV },
50                 [RSL_IE_PAGING_GROUP]           = { TLV_TYPE_TV },
51                 [RSL_IE_PAGING_LOAD]            = { TLV_TYPE_FIXED, 2 },
52                 [RSL_IE_PYHS_CONTEXT]           = { TLV_TYPE_TLV },
53                 [RSL_IE_ACCESS_DELAY]           = { TLV_TYPE_TV },
54                 [RSL_IE_RACH_LOAD]              = { TLV_TYPE_TLV },
55                 [RSL_IE_REQ_REFERENCE]          = { TLV_TYPE_FIXED, 3 },
56                 [RSL_IE_RELEASE_MODE]           = { TLV_TYPE_TV },
57                 [RSL_IE_RESOURCE_INFO]          = { TLV_TYPE_TLV },
58                 [RSL_IE_RLM_CAUSE]              = { TLV_TYPE_TLV },
59                 [RSL_IE_STARTNG_TIME]           = { TLV_TYPE_FIXED, 2 },
60                 [RSL_IE_TIMING_ADVANCE]         = { TLV_TYPE_TV },
61                 [RSL_IE_UPLINK_MEAS]            = { TLV_TYPE_TLV },
62                 [RSL_IE_CAUSE]                  = { TLV_TYPE_TLV },
63                 [RSL_IE_MEAS_RES_NR]            = { TLV_TYPE_TV },
64                 [RSL_IE_MSG_ID]                 = { TLV_TYPE_TV },
65                 [RSL_IE_SYSINFO_TYPE]           = { TLV_TYPE_TV },
66                 [RSL_IE_MS_POWER_PARAM]         = { TLV_TYPE_TLV },
67                 [RSL_IE_BS_POWER_PARAM]         = { TLV_TYPE_TLV },
68                 [RSL_IE_PREPROC_PARAM]          = { TLV_TYPE_TLV },
69                 [RSL_IE_PREPROC_MEAS]           = { TLV_TYPE_TLV },
70                 [RSL_IE_IMM_ASS_INFO]           = { TLV_TYPE_TLV },
71                 [RSL_IE_SMSCB_INFO]             = { TLV_TYPE_FIXED, 23 },
72                 [RSL_IE_MS_TIMING_OFFSET]       = { TLV_TYPE_TV },
73                 [RSL_IE_ERR_MSG]                = { TLV_TYPE_TLV },
74                 [RSL_IE_FULL_BCCH_INFO]         = { TLV_TYPE_TLV },
75                 [RSL_IE_CHAN_NEEDED]            = { TLV_TYPE_TV },
76                 [RSL_IE_CB_CMD_TYPE]            = { TLV_TYPE_TV },
77                 [RSL_IE_SMSCB_MSG]              = { TLV_TYPE_TLV },
78                 [RSL_IE_FULL_IMM_ASS_INFO]      = { TLV_TYPE_TLV },
79                 [RSL_IE_SACCH_INFO]             = { TLV_TYPE_TLV },
80                 [RSL_IE_CBCH_LOAD_INFO]         = { TLV_TYPE_TV },
81                 [RSL_IE_SMSCB_CHAN_INDICATOR]   = { TLV_TYPE_TV },
82                 [RSL_IE_GROUP_CALL_REF]         = { TLV_TYPE_TLV },
83                 [RSL_IE_CHAN_DESC]              = { TLV_TYPE_TLV },
84                 [RSL_IE_NCH_DRX_INFO]           = { TLV_TYPE_TLV },
85                 [RSL_IE_CMD_INDICATOR]          = { TLV_TYPE_TLV },
86                 [RSL_IE_EMLPP_PRIO]             = { TLV_TYPE_TV },
87                 [RSL_IE_UIC]                    = { TLV_TYPE_TLV },
88                 [RSL_IE_MAIN_CHAN_REF]          = { TLV_TYPE_TV },
89                 [RSL_IE_MR_CONFIG]              = { TLV_TYPE_TLV },
90                 [RSL_IE_MR_CONTROL]             = { TLV_TYPE_TV },
91                 [RSL_IE_SUP_CODEC_TYPES]        = { TLV_TYPE_TLV },
92                 [RSL_IE_CODEC_CONFIG]           = { TLV_TYPE_TLV },
93                 [RSL_IE_RTD]                    = { TLV_TYPE_TV },
94                 [RSL_IE_TFO_STATUS]             = { TLV_TYPE_TV },
95                 [RSL_IE_LLP_APDU]               = { TLV_TYPE_TLV },
96                 [RSL_IE_SIEMENS_MRPCI]          = { TLV_TYPE_TV },
97                 [RSL_IE_IPAC_PROXY_UDP]         = { TLV_TYPE_FIXED, 2 },
98                 [RSL_IE_IPAC_BSCMPL_TOUT]       = { TLV_TYPE_TV },
99                 [RSL_IE_IPAC_REMOTE_IP]         = { TLV_TYPE_FIXED, 4 },
100                 [RSL_IE_IPAC_REMOTE_PORT]       = { TLV_TYPE_FIXED, 2 },
101                 [RSL_IE_IPAC_RTP_PAYLOAD]       = { TLV_TYPE_TV },
102                 [RSL_IE_IPAC_LOCAL_PORT]        = { TLV_TYPE_FIXED, 2 },
103                 [RSL_IE_IPAC_SPEECH_MODE]       = { TLV_TYPE_TV },
104                 [RSL_IE_IPAC_LOCAL_IP]          = { TLV_TYPE_FIXED, 4 },
105                 [RSL_IE_IPAC_CONN_ID]           = { TLV_TYPE_FIXED, 2 },
106                 [RSL_IE_IPAC_RTP_CSD_FMT]       = { TLV_TYPE_TV },
107                 [RSL_IE_IPAC_RTP_JIT_BUF]       = { TLV_TYPE_FIXED, 2 },
108                 [RSL_IE_IPAC_RTP_COMPR]         = { TLV_TYPE_TV },
109                 [RSL_IE_IPAC_RTP_PAYLOAD2]      = { TLV_TYPE_TV },
110                 [RSL_IE_IPAC_RTP_MPLEX]         = { TLV_TYPE_FIXED, 8 },
111                 [RSL_IE_IPAC_RTP_MPLEX_ID]      = { TLV_TYPE_TV },
112         },
113 };
114
115 /* encode channel number as per Section 9.3.1 */
116 uint8_t rsl_enc_chan_nr(uint8_t type, uint8_t subch, uint8_t timeslot)
117 {
118         uint8_t ret;
119
120         ret = (timeslot & 0x07) | type;
121
122         switch (type) {
123         case RSL_CHAN_Lm_ACCHs:
124                 subch &= 0x01;
125                 break;
126         case RSL_CHAN_SDCCH4_ACCH:
127                 subch &= 0x07;
128                 break;
129         case RSL_CHAN_SDCCH8_ACCH:
130                 subch &= 0x07;
131                 break;
132         default:
133                 /* no subchannels allowed */
134                 subch = 0x00;
135                 break;
136         }
137         ret |= (subch << 3);
138
139         return ret;
140 }
141
142 /* FIXME: convert to value_string */
143 static const char *rsl_err_vals[0xff] = {
144         [RSL_ERR_RADIO_IF_FAIL] =       "Radio Interface Failure",
145         [RSL_ERR_RADIO_LINK_FAIL] =     "Radio Link Failure",
146         [RSL_ERR_HANDOVER_ACC_FAIL] =   "Handover Access Failure",
147         [RSL_ERR_TALKER_ACC_FAIL] =     "Talker Access Failure",
148         [RSL_ERR_OM_INTERVENTION] =     "O&M Intervention",
149         [RSL_ERR_NORMAL_UNSPEC] =       "Normal event, unspecified",
150         [RSL_ERR_T_MSRFPCI_EXP] =       "Siemens: T_MSRFPCI Expired",
151         [RSL_ERR_EQUIPMENT_FAIL] =      "Equipment Failure",
152         [RSL_ERR_RR_UNAVAIL] =          "Radio Resource not available",
153         [RSL_ERR_TERR_CH_FAIL] =        "Terrestrial Channel Failure",
154         [RSL_ERR_CCCH_OVERLOAD] =       "CCCH Overload",
155         [RSL_ERR_ACCH_OVERLOAD] =       "ACCH Overload",
156         [RSL_ERR_PROCESSOR_OVERLOAD] =  "Processor Overload",
157         [RSL_ERR_RES_UNAVAIL] =         "Resource not available, unspecified",
158         [RSL_ERR_TRANSC_UNAVAIL] =      "Transcoding not available",
159         [RSL_ERR_SERV_OPT_UNAVAIL] =    "Service or Option not available",
160         [RSL_ERR_ENCR_UNIMPL] =         "Encryption algorithm not implemented",
161         [RSL_ERR_SERV_OPT_UNIMPL] =     "Service or Option not implemented",
162         [RSL_ERR_RCH_ALR_ACTV_ALLOC] =  "Radio channel already activated",
163         [RSL_ERR_INVALID_MESSAGE] =     "Invalid Message, unspecified",
164         [RSL_ERR_MSG_DISCR] =           "Message Discriminator Error",
165         [RSL_ERR_MSG_TYPE] =            "Message Type Error",
166         [RSL_ERR_MSG_SEQ] =             "Message Sequence Error",
167         [RSL_ERR_IE_ERROR] =            "General IE error",
168         [RSL_ERR_MAND_IE_ERROR] =       "Mandatory IE error",
169         [RSL_ERR_OPT_IE_ERROR] =        "Optional IE error",
170         [RSL_ERR_IE_NONEXIST] =         "IE non-existent",
171         [RSL_ERR_IE_LENGTH] =           "IE length error",
172         [RSL_ERR_IE_CONTENT] =          "IE content error",
173         [RSL_ERR_PROTO] =               "Protocol error, unspecified",
174         [RSL_ERR_INTERWORKING] =        "Interworking error, unspecified",
175 };
176
177 const struct value_string rsl_rlm_cause_strs[] = {
178         { RLL_CAUSE_T200_EXPIRED,       "Timer T200 expired (N200+1) times" },
179         { RLL_CAUSE_REEST_REQ,          "Re-establishment request" },
180         { RLL_CAUSE_UNSOL_UA_RESP,      "Unsolicited UA response" },
181         { RLL_CAUSE_UNSOL_DM_RESP,      "Unsolicited DM response" },
182         { RLL_CAUSE_UNSOL_DM_RESP_MF,   "Unsolicited DM response, multiple frame" },
183         { RLL_CAUSE_UNSOL_SPRV_RESP,    "Unsolicited supervisory response" },
184         { RLL_CAUSE_SEQ_ERR,            "Sequence Error" },
185         { RLL_CAUSE_UFRM_INC_PARAM,     "U-Frame with incorrect parameters" },
186         { RLL_CAUSE_SFRM_INC_PARAM,     "S-Frame with incorrect parameters" },
187         { RLL_CAUSE_IFRM_INC_MBITS,     "I-Frame with incorrect use of M bit" },
188         { RLL_CAUSE_IFRM_INC_LEN,       "I-Frame with incorrect length" },
189         { RLL_CAUSE_FRM_UNIMPL,         "Fraeme not implemented" },
190         { RLL_CAUSE_SABM_MF,            "SABM command, multiple frame established state" },
191         { RLL_CAUSE_SABM_INFO_NOTALL,   "SABM frame with information not allowed in this state" },
192         { 0,                            NULL },
193 };
194
195 const char *rsl_err_name(uint8_t err)
196 {
197         if (rsl_err_vals[err])
198                 return rsl_err_vals[err];
199         else
200                 return "unknown";
201 }
202
203 /* Section 3.3.2.3 TS 05.02. I think this looks like a table */
204 int rsl_ccch_conf_to_bs_cc_chans(int ccch_conf)
205 {
206         switch (ccch_conf) {
207         case RSL_BCCH_CCCH_CONF_1_NC:
208                 return 1;
209         case RSL_BCCH_CCCH_CONF_1_C:
210                 return 1;
211         case RSL_BCCH_CCCH_CONF_2_NC:
212                 return 2;
213         case RSL_BCCH_CCCH_CONF_3_NC:
214                 return 3;
215         case RSL_BCCH_CCCH_CONF_4_NC:
216                 return 4;
217         default:
218                 return -1;
219         }
220 }
221
222 /* Section 3.3.2.3 TS 05.02 */
223 int rsl_ccch_conf_to_bs_ccch_sdcch_comb(int ccch_conf)
224 {
225         switch (ccch_conf) {
226         case RSL_BCCH_CCCH_CONF_1_NC:
227                 return 0;
228         case RSL_BCCH_CCCH_CONF_1_C:
229                 return 1;
230         case RSL_BCCH_CCCH_CONF_2_NC:
231                 return 0;
232         case RSL_BCCH_CCCH_CONF_3_NC:
233                 return 0;
234         case RSL_BCCH_CCCH_CONF_4_NC:
235                 return 0;
236         default:
237                 return -1;
238         }
239 }
240
241 /* Push a RSL RLL header with L3_INFO IE */
242 int rsl_rll_push_l3(struct msgb *msg, uint8_t msg_type,
243                     uint8_t chan_nr, uint8_t link_id)
244 {
245         uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
246         struct abis_rsl_rll_hdr *rh;
247
248         /* construct a RSLms RLL message (DATA INDICATION, UNIT DATA
249          * INDICATION) and send it off via RSLms */
250
251         /* Push the L3 IE tag and lengh */
252         msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
253
254         /* Then push the RSL header */
255         rh = (struct abis_rsl_rll_hdr *) msgb_push(msg, sizeof(*rh));
256         rsl_init_rll_hdr(rh, msg_type);
257         rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
258         rh->chan_nr = chan_nr;
259         rh->link_id = link_id;
260
261         /* set the l2 header pointer */
262         msg->l2h = (uint8_t *)rh;
263
264         return 0;
265 }
266
267 struct msgb *rsl_rll_simple(uint8_t msg_type, uint8_t chan_nr,
268                             uint8_t link_id)
269 {
270         struct abis_rsl_rll_hdr *rh;
271         struct msgb *msg = msgb_alloc(sizeof(*rh), "rsl_rll_simple");
272
273         if (!msg)
274                 return NULL;
275
276         /* put the RSL header */
277         rh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rh));
278         rsl_init_rll_hdr(rh, msg_type);
279         rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
280         rh->chan_nr = chan_nr;
281         rh->link_id = link_id;
282
283         /* set the l2 header pointer */
284         msg->l2h = (uint8_t *)rh;
285
286         return msg;
287 }