blob: a4e9d0d1f2e85ca8b1ca976e38a9722a58393dee [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gsm0411_utils.c
2 * Point-to-Point (PP) Short Message Service (SMS).
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +01003 * Support on Mobile Radio Interface
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02004 * 3GPP TS 04.11 version 7.1.0 Release 1998 / ETSI TS 100 942 V7.1.0. */
5/*
6 * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +01007 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther4d7e49b2013-05-02 22:37:16 +02008 * (C) 2010-2013 by Holger Hans Peter Freyther <zecke@selfish.org>
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +01009 * (C) 2010 by On-Waves
10 * (C) 2011 by Andreas Eversberg <jolly@eversberg.eu>
11 *
12 * All Rights Reserved
13 *
14 * This program is free software; you can redistribute it and/or modify
Harald Welte388fb032014-10-26 20:42:49 +010015 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +010017 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte388fb032014-10-26 20:42:49 +010022 * GNU General Public License for more details.
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +010023 *
Harald Welte388fb032014-10-26 20:42:49 +010024 * You should have received a copy of the GNU General Public License
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +010025 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 *
27 */
28
Harald Welte7c8e2cc2012-08-29 16:47:30 +020029#include "../../config.h"
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +010030
31#include <time.h>
32#include <string.h>
33#include <osmocom/core/msgb.h>
34#include <osmocom/core/logging.h>
35
36#include <osmocom/gsm/gsm48.h>
Max2f0b0c92017-01-12 16:47:13 +010037#include <osmocom/gsm/gsm0480.h>
Holger Hans Peter Freyther4d7e49b2013-05-02 22:37:16 +020038#include <osmocom/gsm/gsm_utils.h>
39#include <osmocom/gsm/protocol/gsm_03_40.h>
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +010040#include <osmocom/gsm/protocol/gsm_04_11.h>
41
Harald Welte96e2a002017-06-12 21:44:18 +020042/*! \addtogroup sms
43 * @{
44 */
45
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +010046#define GSM411_ALLOC_SIZE 1024
47#define GSM411_ALLOC_HEADROOM 128
48
Neels Hofmeyr87e45502017-06-20 00:17:59 +020049/*! Allocate a message buffer for use as TS 04.11 message
Harald Welte96e2a002017-06-12 21:44:18 +020050 * \returns allocated message buffer */
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +010051struct msgb *gsm411_msgb_alloc(void)
52{
53 return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM,
54 "GSM 04.11");
55}
56
Neels Hofmeyr87e45502017-06-20 00:17:59 +020057/*! Turn int into semi-octet representation: 98 => 0x89
Harald Welte96e2a002017-06-12 21:44:18 +020058 * \param[in] integer value representing decimal number 0..99
59 * \returns BSC encoded as nibbles, swapped */
Harald Weltead633b02011-12-01 21:08:19 +010060uint8_t gsm411_bcdify(uint8_t value)
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +010061{
62 uint8_t ret;
63
64 ret = value / 10;
65 ret |= (value % 10) << 4;
66
67 return ret;
68}
69
Neels Hofmeyr87e45502017-06-20 00:17:59 +020070/*! Turn semi-octet representation into int: 0x89 => 98
Harald Welte96e2a002017-06-12 21:44:18 +020071 * \param[in] value byte containing two BCD nibbles in revere order
72 * \returns integer representing decoded, re-ordered nibbles */
Harald Weltead633b02011-12-01 21:08:19 +010073uint8_t gsm411_unbcdify(uint8_t value)
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +010074{
75 uint8_t ret;
76
77 if ((value & 0x0F) > 9 || (value >> 4) > 9)
78 LOGP(DLSMS, LOGL_ERROR,
Harald Weltead633b02011-12-01 21:08:19 +010079 "gsm411_unbcdify got too big nibble: 0x%02X\n", value);
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +010080
81 ret = (value&0x0F)*10;
82 ret += value>>4;
83
84 return ret;
85}
86
Neels Hofmeyr87e45502017-06-20 00:17:59 +020087/*! Generate 03.40 TP-SCTS
Harald Welte96e2a002017-06-12 21:44:18 +020088 * \param[out] scts Caller-provided buffer to store SCTS (7 octets)
89 * \param[in] time to encode */
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +010090void gsm340_gen_scts(uint8_t *scts, time_t time)
91{
Keith733810c2017-08-17 21:37:47 +020092 struct tm *tm = localtime(&time);
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +010093
Harald Weltead633b02011-12-01 21:08:19 +010094 *scts++ = gsm411_bcdify(tm->tm_year % 100);
95 *scts++ = gsm411_bcdify(tm->tm_mon + 1);
96 *scts++ = gsm411_bcdify(tm->tm_mday);
97 *scts++ = gsm411_bcdify(tm->tm_hour);
98 *scts++ = gsm411_bcdify(tm->tm_min);
99 *scts++ = gsm411_bcdify(tm->tm_sec);
Harald Welte7c8e2cc2012-08-29 16:47:30 +0200100#ifdef HAVE_TM_GMTOFF_IN_TM
101 *scts++ = gsm411_bcdify(tm->tm_gmtoff/(60*15));
102#else
Pau Espin Pedrol29b7d532017-06-18 14:15:16 +0200103#pragma message ("find a portable way to obtain timezone offset")
Harald Welte7c8e2cc2012-08-29 16:47:30 +0200104 *scts++ = 0;
105#endif
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100106}
107
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200108/*! Decode 03.40 TP-SCTS (into utc/gmt timestamp)
Harald Welte96e2a002017-06-12 21:44:18 +0200109 * \param[in] scts SMS Center Time Stamp
110 * \return time in UTC time_t format */
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100111time_t gsm340_scts(uint8_t *scts)
112{
113 struct tm tm;
Harald Weltead633b02011-12-01 21:08:19 +0100114 uint8_t yr = gsm411_unbcdify(*scts++);
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100115 int ofs;
116
117 memset(&tm, 0x00, sizeof(struct tm));
118
119 if (yr <= 80)
120 tm.tm_year = 100 + yr;
121 else
122 tm.tm_year = yr;
Harald Weltead633b02011-12-01 21:08:19 +0100123 tm.tm_mon = gsm411_unbcdify(*scts++) - 1;
124 tm.tm_mday = gsm411_unbcdify(*scts++);
125 tm.tm_hour = gsm411_unbcdify(*scts++);
126 tm.tm_min = gsm411_unbcdify(*scts++);
127 tm.tm_sec = gsm411_unbcdify(*scts++);
Harald Welte7c8e2cc2012-08-29 16:47:30 +0200128#ifdef HAVE_TM_GMTOFF_IN_TM
129 tm.tm_gmtoff = gsm411_unbcdify(*scts++) * 15*60;
130#endif
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100131
132 /* according to gsm 03.40 time zone is
133 "expressed in quarters of an hour" */
Harald Weltead633b02011-12-01 21:08:19 +0100134 ofs = gsm411_unbcdify(*scts++) * 15*60;
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100135
136 return mktime(&tm) - ofs;
137}
138
139/* Return the default validity period in minutes */
140static unsigned long gsm340_vp_default(void)
141{
142 unsigned long minutes;
143 /* Default validity: two days */
144 minutes = 24 * 60 * 2;
145 return minutes;
146}
147
148/* Decode validity period format 'relative' */
149static unsigned long gsm340_vp_relative(uint8_t *sms_vp)
150{
151 /* Chapter 9.2.3.12.1 */
152 uint8_t vp;
153 unsigned long minutes;
154
155 vp = *(sms_vp);
156 if (vp <= 143)
Alexander Chemeriscc0645b2014-03-07 21:00:19 +0100157 minutes = (vp + 1) * 5;
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100158 else if (vp <= 167)
159 minutes = 12*60 + (vp-143) * 30;
160 else if (vp <= 196)
Alexander Chemeriscc0645b2014-03-07 21:00:19 +0100161 minutes = (vp-166) * 60 * 24;
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100162 else
Alexander Chemeriscc0645b2014-03-07 21:00:19 +0100163 minutes = (vp-192) * 60 * 24 * 7;
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100164 return minutes;
165}
166
167/* Decode validity period format 'absolute' */
168static unsigned long gsm340_vp_absolute(uint8_t *sms_vp)
169{
170 /* Chapter 9.2.3.12.2 */
171 time_t expires, now;
172 unsigned long minutes;
173
174 expires = gsm340_scts(sms_vp);
175 now = time(NULL);
176 if (expires <= now)
177 minutes = 0;
178 else
179 minutes = (expires-now)/60;
180 return minutes;
181}
182
183/* Decode validity period format 'relative in integer representation' */
184static unsigned long gsm340_vp_relative_integer(uint8_t *sms_vp)
185{
186 uint8_t vp;
187 unsigned long minutes;
188 vp = *(sms_vp);
189 if (vp == 0) {
190 LOGP(DLSMS, LOGL_ERROR,
191 "reserved relative_integer validity period\n");
192 return gsm340_vp_default();
193 }
194 minutes = vp/60;
195 return minutes;
196}
197
198/* Decode validity period format 'relative in semi-octet representation' */
199static unsigned long gsm340_vp_relative_semioctet(uint8_t *sms_vp)
200{
201 unsigned long minutes;
Harald Weltead633b02011-12-01 21:08:19 +0100202 minutes = gsm411_unbcdify(*sms_vp++)*60; /* hours */
203 minutes += gsm411_unbcdify(*sms_vp++); /* minutes */
204 minutes += gsm411_unbcdify(*sms_vp++)/60; /* seconds */
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100205 return minutes;
206}
207
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200208/*! decode validity period. return minutes
Harald Welte96e2a002017-06-12 21:44:18 +0200209 * \param[in] sms_vpf Validity Period Format in 03.40 encoding
210 * \param[in] sms_vp Validity Period Information Element
211 * \returns validity period in minutes */
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100212unsigned long gsm340_validity_period(uint8_t sms_vpf, uint8_t *sms_vp)
213{
214 uint8_t fi; /* functionality indicator */
215
216 switch (sms_vpf) {
217 case GSM340_TP_VPF_RELATIVE:
218 return gsm340_vp_relative(sms_vp);
219 case GSM340_TP_VPF_ABSOLUTE:
220 return gsm340_vp_absolute(sms_vp);
221 case GSM340_TP_VPF_ENHANCED:
222 /* Chapter 9.2.3.12.3 */
223 fi = *sms_vp++;
224 /* ignore additional fi */
225 if (fi & (1<<7)) sms_vp++;
226 /* read validity period format */
227 switch (fi & 0x7) {
228 case 0x0:
229 return gsm340_vp_default(); /* no vpf specified */
230 case 0x1:
231 return gsm340_vp_relative(sms_vp);
232 case 0x2:
233 return gsm340_vp_relative_integer(sms_vp);
234 case 0x3:
235 return gsm340_vp_relative_semioctet(sms_vp);
236 default:
237 /* The GSM spec says that the SC should reject any
238 unsupported and/or undefined values. FIXME */
239 LOGP(DLSMS, LOGL_ERROR,
240 "Reserved enhanced validity period format\n");
241 return gsm340_vp_default();
242 }
243 case GSM340_TP_VPF_NONE:
244 default:
245 return gsm340_vp_default();
246 }
247}
248
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200249/*! determine coding alphabet dependent on GSM 03.38 Section 4 DCS
Harald Welte96e2a002017-06-12 21:44:18 +0200250 * \param[in] dcs Data Coding Scheme in 03.38 encoding
251 * \returns libosmogsm internal enum \ref sms_alphabet */
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100252enum sms_alphabet gsm338_get_sms_alphabet(uint8_t dcs)
253{
254 uint8_t cgbits = dcs >> 4;
255 enum sms_alphabet alpha = DCS_NONE;
256
257 if ((cgbits & 0xc) == 0) {
258 if (cgbits & 2) {
259 LOGP(DLSMS, LOGL_NOTICE,
260 "Compressed SMS not supported yet\n");
Pau Espin Pedrol29b7d532017-06-18 14:15:16 +0200261 return -1;
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100262 }
263
264 switch ((dcs >> 2)&0x03) {
265 case 0:
266 alpha = DCS_7BIT_DEFAULT;
267 break;
268 case 1:
269 alpha = DCS_8BIT_DATA;
270 break;
271 case 2:
272 alpha = DCS_UCS2;
273 break;
274 }
275 } else if (cgbits == 0xc || cgbits == 0xd)
276 alpha = DCS_7BIT_DEFAULT;
277 else if (cgbits == 0xe)
278 alpha = DCS_UCS2;
279 else if (cgbits == 0xf) {
280 if (dcs & 4)
281 alpha = DCS_8BIT_DATA;
282 else
283 alpha = DCS_7BIT_DEFAULT;
284 }
285
286 return alpha;
287}
288
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200289/*! generate a TPDU address field compliant with 03.40 sec. 9.1.2.5
Harald Welte96e2a002017-06-12 21:44:18 +0200290 * \param[out] oa caller-provided output buffer
291 * \param[in] oa_len caller-specified length of \a oa in bytes
292 * \param[in] type GSM340_TYPE_*
293 * \param[in] plan Numbering Plan
294 * \param[in] number string containing number
295 * \reurns number of bytes of \a oa that have been used */
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100296int gsm340_gen_oa(uint8_t *oa, unsigned int oa_len, uint8_t type,
297 uint8_t plan, const char *number)
298{
299 int len_in_bytes;
300
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100301 oa[1] = 0x80 | (type << 4) | plan;
302
Holger Hans Peter Freyther4d7e49b2013-05-02 22:37:16 +0200303 if (type == GSM340_TYPE_ALPHA_NUMERIC) {
304 /*
305 * TODO/FIXME: what is the 'useful semi-octets' excluding any
306 * semi octet containing only fill bits.
307 * The current code picks the number of bytes written by the
308 * 7bit encoding routines and multiplies it by two.
309 */
310 gsm_7bit_encode_n(&oa[2], oa_len - 2, number, &len_in_bytes);
311 oa[0] = len_in_bytes * 2;
312 len_in_bytes += 2;
313 } else {
314 /* prevent buffer overflows */
315 if (strlen(number) > 20)
316 number = "";
317 len_in_bytes = gsm48_encode_bcd_number(oa, oa_len, 1, number);
318 /* GSM 03.40 tells us the length is in 'useful semi-octets' */
319 oa[0] = strlen(number) & 0xff;
320 }
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100321
322 return len_in_bytes;
323}
324
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200325/*! Prefix \ref msgb with a RP header
Harald Welte96e2a002017-06-12 21:44:18 +0200326 * \param msg Message Buffer containing message
327 * \param[in] rp_msg_type RP Message Type
328 * \param[in] rp_msg_ref RP Message Reference
329 * \returns 0 */
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100330int gsm411_push_rp_header(struct msgb *msg, uint8_t rp_msg_type,
331 uint8_t rp_msg_ref)
332{
333 struct gsm411_rp_hdr *rp;
334 uint8_t len = msg->len;
335
336 /* GSM 04.11 RP-DATA header */
337 rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp));
338 rp->len = len + 2;
339 rp->msg_type = rp_msg_type;
340 rp->msg_ref = rp_msg_ref; /* FIXME: Choose randomly */
341
342 return 0;
343}
344
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200345/*! Prefix \ref msgb with a 04.08/04.11 CP header
Harald Welte96e2a002017-06-12 21:44:18 +0200346 * \param msg Message Buffer containing message
347 * \param[in] proto Protocol
348 * \param[in] trans Transaction
349 * \param[in] msg_type Message Type
350 * \retrns 0 */
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100351int gsm411_push_cp_header(struct msgb *msg, uint8_t proto, uint8_t trans,
352 uint8_t msg_type)
353{
Neels Hofmeyr25774b92016-11-26 15:21:05 +0100354 /* Outgoing proto_discr needs the highest bit set */
355 gsm0480_l3hdr_push(msg, proto | (trans << 4), msg_type);
Andreas.Eversbergd84f47a2011-11-06 20:22:12 +0100356 return 0;
357}
Harald Welte96e2a002017-06-12 21:44:18 +0200358
359/*! @} */