blob: b8eca6c36e4739ff17962ad88582658bd878eab4 [file] [log] [blame]
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001/* Point-to-Point (PP) Short Message Service (SMS)
2 * Support on Mobile Radio Interface
3 * 3GPP TS 04.11 version 7.1.0 Release 1998 / ETSI TS 100 942 V7.1.0 */
4
5/* (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
Harald Welte7e310b12009-03-30 20:56:32 +00006 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08007 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
8 * (C) 2010 by On Waves
Daniel Willmann8b3390e2008-12-28 00:31:09 +00009 *
10 * All Rights Reserved
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 *
26 */
27
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <errno.h>
Harald Weltef3efc592009-07-27 20:11:35 +020033#include <time.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000034#include <netinet/in.h>
35
Harald Weltedfe6c7d2010-02-20 16:24:02 +010036#include <osmocore/msgb.h>
37#include <osmocore/tlv.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000038#include <openbsc/debug.h>
Daniel Willmann471712b2008-12-29 01:54:02 +000039#include <openbsc/gsm_data.h>
40#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000041#include <openbsc/gsm_04_11.h>
42#include <openbsc/gsm_04_08.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010043#include <osmocore/gsm_utils.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000044#include <openbsc/abis_rsl.h>
Holger Freyther9b177762009-02-16 19:07:18 +000045#include <openbsc/signal.h>
Harald Welte7e310b12009-03-30 20:56:32 +000046#include <openbsc/db.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010047#include <osmocore/talloc.h>
Harald Weltef3efc592009-07-27 20:11:35 +020048#include <openbsc/transaction.h>
Harald Welte76042182009-08-08 16:03:15 +020049#include <openbsc/paging.h>
Harald Weltecb8f4432009-08-09 14:59:02 +020050#include <openbsc/bsc_rll.h>
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020051#include <openbsc/chan_alloc.h>
Holger Hans Peter Freythercbac76e2010-05-23 21:05:18 +080052#include <openbsc/bsc_api.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000053
Daniel Willmann471712b2008-12-29 01:54:02 +000054#define GSM411_ALLOC_SIZE 1024
55#define GSM411_ALLOC_HEADROOM 128
56
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020057void *tall_gsms_ctx;
Harald Weltef3efc592009-07-27 20:11:35 +020058static u_int32_t new_callref = 0x40000001;
59
Harald Welte (local)c89a5112009-08-14 10:42:43 +020060static const struct value_string cp_cause_strs[] = {
61 { GSM411_CP_CAUSE_NET_FAIL, "Network Failure" },
62 { GSM411_CP_CAUSE_CONGESTION, "Congestion" },
63 { GSM411_CP_CAUSE_INV_TRANS_ID, "Invalid Transaction ID" },
64 { GSM411_CP_CAUSE_SEMANT_INC_MSG, "Semantically Incorrect Message" },
65 { GSM411_CP_CAUSE_INV_MAND_INF, "Invalid Mandatory Information" },
66 { GSM411_CP_CAUSE_MSGTYPE_NOTEXIST, "Message Type doesn't exist" },
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +020067 { GSM411_CP_CAUSE_MSG_INCOMP_STATE,
Harald Welte (local)c89a5112009-08-14 10:42:43 +020068 "Message incompatible with protocol state" },
69 { GSM411_CP_CAUSE_IE_NOTEXIST, "IE does not exist" },
70 { GSM411_CP_CAUSE_PROTOCOL_ERR, "Protocol Error" },
71 { 0, 0 }
72};
73
74static const struct value_string rp_cause_strs[] = {
75 { GSM411_RP_CAUSE_MO_NUM_UNASSIGNED, "(MO) Number not assigned" },
76 { GSM411_RP_CAUSE_MO_OP_DET_BARR, "(MO) Operator determined barring" },
77 { GSM411_RP_CAUSE_MO_CALL_BARRED, "(MO) Call barred" },
78 { GSM411_RP_CAUSE_MO_SMS_REJECTED, "(MO) SMS rejected" },
79 { GSM411_RP_CAUSE_MO_DEST_OUT_OF_ORDER, "(MO) Destination out of order" },
80 { GSM411_RP_CAUSE_MO_UNIDENTIFIED_SUBSCR, "(MO) Unidentified subscriber" },
81 { GSM411_RP_CAUSE_MO_FACILITY_REJ, "(MO) Facility reject" },
82 { GSM411_RP_CAUSE_MO_UNKNOWN_SUBSCR, "(MO) Unknown subscriber" },
83 { GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER, "(MO) Network out of order" },
84 { GSM411_RP_CAUSE_MO_TEMP_FAIL, "(MO) Temporary failure" },
85 { GSM411_RP_CAUSE_MO_CONGESTION, "(MO) Congestion" },
86 { GSM411_RP_CAUSE_MO_RES_UNAVAIL, "(MO) Resource unavailable" },
87 { GSM411_RP_CAUSE_MO_REQ_FAC_NOTSUBSCR, "(MO) Requested facility not subscribed" },
88 { GSM411_RP_CAUSE_MO_REQ_FAC_NOTIMPL, "(MO) Requested facility not implemented" },
89 { GSM411_RP_CAUSE_MO_INTERWORKING, "(MO) Interworking" },
90 /* valid only for MT */
91 { GSM411_RP_CAUSE_MT_MEM_EXCEEDED, "(MT) Memory Exceeded" },
92 /* valid for both directions */
93 { GSM411_RP_CAUSE_INV_TRANS_REF, "Invalid Transaction Reference" },
94 { GSM411_RP_CAUSE_SEMANT_INC_MSG, "Semantically Incorrect Message" },
95 { GSM411_RP_CAUSE_INV_MAND_INF, "Invalid Mandatory Information" },
96 { GSM411_RP_CAUSE_MSGTYPE_NOTEXIST, "Message Type non-existant" },
97 { GSM411_RP_CAUSE_MSG_INCOMP_STATE, "Message incompatible with protocol state" },
98 { GSM411_RP_CAUSE_IE_NOTEXIST, "Information Element not existing" },
99 { GSM411_RP_CAUSE_PROTOCOL_ERR, "Protocol Error" },
100 { 0, NULL }
101};
102
Harald Welte76042182009-08-08 16:03:15 +0200103struct gsm_sms *sms_alloc(void)
104{
105 return talloc_zero(tall_gsms_ctx, struct gsm_sms);
106}
107
108void sms_free(struct gsm_sms *sms)
109{
110 /* drop references to subscriber structure */
111 if (sms->sender)
112 subscr_put(sms->sender);
113 if (sms->receiver)
114 subscr_put(sms->receiver);
115
116 talloc_free(sms);
117}
118
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800119/*
120 * This should be called whenever all SMS to a given subscriber
121 * on a given connection has been sent. This will inform the higher
122 * layers that a channel can be given up.
123 */
124static void gsm411_release_conn(struct gsm_subscriber_connection *conn)
125{
Holger Hans Peter Freytherbddd1522010-06-16 14:02:41 +0800126 if (!conn)
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800127 return;
128
Holger Hans Peter Freytherbddd1522010-06-16 14:02:41 +0800129 subscr_put_channel(conn);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800130}
131
Holger Freythera553d092009-01-04 20:16:25 +0000132struct msgb *gsm411_msgb_alloc(void)
Daniel Willmann471712b2008-12-29 01:54:02 +0000133{
Harald Welte966636f2009-06-26 19:39:35 +0200134 return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM,
135 "GSM 04.11");
Daniel Willmann471712b2008-12-29 01:54:02 +0000136}
137
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100138static int gsm411_sendmsg(struct gsm_subscriber_connection *conn, struct msgb *msg, u_int8_t link_id)
Daniel Willmann471712b2008-12-29 01:54:02 +0000139{
Harald Welte76042182009-08-08 16:03:15 +0200140 DEBUGP(DSMS, "GSM4.11 TX %s\n", hexdump(msg->data, msg->len));
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100141 msg->l3h = msg->data;
142 return gsm0808_submit_dtap(conn, msg, link_id);
Daniel Willmann471712b2008-12-29 01:54:02 +0000143}
144
Harald Welte41985612009-08-10 00:24:32 +0200145/* SMC TC1* is expired */
146static void cp_timer_expired(void *data)
147{
148 struct gsm_trans *trans = data;
149
150 DEBUGP(DSMS, "SMC Timer TC1* is expired, calling trans_free()\n");
151 /* FIXME: we need to re-transmit the last CP-DATA 1..3 times */
152 trans_free(trans);
153}
154
Harald Welte87f5d632009-07-04 17:39:00 +0200155/* Prefix msg with a 04.08/04.11 CP header */
Harald Weltef3efc592009-07-27 20:11:35 +0200156static int gsm411_cp_sendmsg(struct msgb *msg, struct gsm_trans *trans,
157 u_int8_t msg_type)
Harald Welte87f5d632009-07-04 17:39:00 +0200158{
159 struct gsm48_hdr *gh;
160
161 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
162 /* Outgoing needs the highest bit set */
Harald Weltef3efc592009-07-27 20:11:35 +0200163 gh->proto_discr = trans->protocol | (trans->transaction_id<<4);
Harald Welte87f5d632009-07-04 17:39:00 +0200164 gh->msg_type = msg_type;
165
Harald Weltef3efc592009-07-27 20:11:35 +0200166 /* mobile originating */
167 switch (gh->msg_type) {
168 case GSM411_MT_CP_DATA:
169 /* 5.2.3.1.2: enter MO-wait for CP-ack */
Harald Weltecb8f4432009-08-09 14:59:02 +0200170 /* 5.2.3.2.3: enter MT-wait for CP-ACK */
Harald Weltef3efc592009-07-27 20:11:35 +0200171 trans->sms.cp_state = GSM411_CPS_WAIT_CP_ACK;
Harald Welte41985612009-08-10 00:24:32 +0200172 trans->sms.cp_timer.data = trans;
173 trans->sms.cp_timer.cb = cp_timer_expired;
174 /* 5.3.2.1: Set Timer TC1A */
175 bsc_schedule_timer(&trans->sms.cp_timer, GSM411_TMR_TC1A);
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200176 DEBUGP(DSMS, "TX: CP-DATA ");
177 break;
178 case GSM411_MT_CP_ACK:
179 DEBUGP(DSMS, "TX: CP-ACK ");
180 break;
181 case GSM411_MT_CP_ERROR:
Sylvain Munaut1d9efd62009-12-18 18:28:07 +0100182 DEBUGP(DSMS, "TX: CP-ERROR ");
Harald Weltef3efc592009-07-27 20:11:35 +0200183 break;
184 }
185
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200186 DEBUGPC(DSMS, "trans=%x\n", trans->transaction_id);
187
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100188 return gsm411_sendmsg(trans->conn, msg, trans->sms.link_id);
Harald Welte87f5d632009-07-04 17:39:00 +0200189}
190
191/* Prefix msg with a RP-DATA header and send as CP-DATA */
Harald Weltef3efc592009-07-27 20:11:35 +0200192static int gsm411_rp_sendmsg(struct msgb *msg, struct gsm_trans *trans,
193 u_int8_t rp_msg_type, u_int8_t rp_msg_ref)
Harald Welte87f5d632009-07-04 17:39:00 +0200194{
195 struct gsm411_rp_hdr *rp;
Harald Welte0d544e72009-08-10 00:22:19 +0200196 u_int8_t len = msg->len;
Harald Welte87f5d632009-07-04 17:39:00 +0200197
198 /* GSM 04.11 RP-DATA header */
199 rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp));
Harald Welte0d544e72009-08-10 00:22:19 +0200200 rp->len = len + 2;
Harald Welte87f5d632009-07-04 17:39:00 +0200201 rp->msg_type = rp_msg_type;
202 rp->msg_ref = rp_msg_ref; /* FIXME: Choose randomly */
203
Harald Weltef3efc592009-07-27 20:11:35 +0200204 return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_DATA);
Harald Welte87f5d632009-07-04 17:39:00 +0200205}
206
Steffen Neubauerf3262672009-11-26 12:28:41 +0100207/* Turn int into semi-octet representation: 98 => 0x89 */
208static u_int8_t bcdify(u_int8_t value)
209{
210 u_int8_t ret;
Steffen Neubauer5727cf42009-11-11 23:02:07 +0900211
Steffen Neubauerf3262672009-11-26 12:28:41 +0100212 ret = value / 10;
213 ret |= (value % 10) << 4;
214
215 return ret;
216}
217
218/* Turn semi-octet representation into int: 0x89 => 98 */
219static u_int8_t unbcdify(u_int8_t value)
220{
221 u_int8_t ret;
222
223 if ((value & 0x0F) > 9 || (value >> 4) > 9)
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200224 LOGP(DSMS, LOGL_ERROR,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100225 "unbcdify got too big nibble: 0x%02X\n", value);
Steffen Neubauerf3262672009-11-26 12:28:41 +0100226
227 ret = (value&0x0F)*10;
Steffen Neubauerad69d7f2009-11-26 23:38:41 +0100228 ret += value>>4;
Steffen Neubauerf3262672009-11-26 12:28:41 +0100229
230 return ret;
231}
232
233/* Generate 03.40 TP-SCTS */
234static void gsm340_gen_scts(u_int8_t *scts, time_t time)
235{
236 struct tm *tm = localtime(&time);
237
238 *scts++ = bcdify(tm->tm_year % 100);
239 *scts++ = bcdify(tm->tm_mon + 1);
240 *scts++ = bcdify(tm->tm_mday);
241 *scts++ = bcdify(tm->tm_hour);
242 *scts++ = bcdify(tm->tm_min);
243 *scts++ = bcdify(tm->tm_sec);
244 *scts++ = bcdify(tm->tm_gmtoff/(60*15));
245}
246
247/* Decode 03.40 TP-SCTS (into utc/gmt timestamp) */
248static time_t gsm340_scts(u_int8_t *scts)
249{
250 struct tm tm;
251
252 u_int8_t yr = unbcdify(*scts++);
253
254 if (yr <= 80)
255 tm.tm_year = 100 + yr;
256 else
257 tm.tm_year = yr;
258 tm.tm_mon = unbcdify(*scts++) - 1;
259 tm.tm_mday = unbcdify(*scts++);
260 tm.tm_hour = unbcdify(*scts++);
261 tm.tm_min = unbcdify(*scts++);
262 tm.tm_sec = unbcdify(*scts++);
263 /* according to gsm 03.40 time zone is
264 "expressed in quarters of an hour" */
265 tm.tm_gmtoff = unbcdify(*scts++) * 15*60;
266
267 return mktime(&tm);
268}
269
270/* Return the default validity period in minutes */
271static unsigned long gsm340_vp_default(void)
272{
273 unsigned long minutes;
274 /* Default validity: two days */
275 minutes = 24 * 60 * 2;
276 return minutes;
277}
278
279/* Decode validity period format 'relative' */
280static unsigned long gsm340_vp_relative(u_int8_t *sms_vp)
281{
282 /* Chapter 9.2.3.12.1 */
283 u_int8_t vp;
284 unsigned long minutes;
285
286 vp = *(sms_vp);
287 if (vp <= 143)
288 minutes = vp + 1 * 5;
289 else if (vp <= 167)
290 minutes = 12*60 + (vp-143) * 30;
291 else if (vp <= 196)
292 minutes = vp-166 * 60 * 24;
293 else
294 minutes = vp-192 * 60 * 24 * 7;
295 return minutes;
296}
297
298/* Decode validity period format 'absolute' */
299static unsigned long gsm340_vp_absolute(u_int8_t *sms_vp)
300{
301 /* Chapter 9.2.3.12.2 */
302 time_t expires, now;
303 unsigned long minutes;
304
305 expires = gsm340_scts(sms_vp);
306 now = mktime(gmtime(NULL));
307 if (expires <= now)
308 minutes = 0;
309 else
310 minutes = (expires-now)/60;
311 return minutes;
312}
313
314/* Decode validity period format 'relative in integer representation' */
315static unsigned long gsm340_vp_relative_integer(u_int8_t *sms_vp)
Harald Welte7e310b12009-03-30 20:56:32 +0000316{
317 u_int8_t vp;
318 unsigned long minutes;
Steffen Neubauerf3262672009-11-26 12:28:41 +0100319 vp = *(sms_vp);
320 if (vp == 0) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100321 LOGP(DSMS, LOGL_ERROR,
322 "reserved relative_integer validity period\n");
Steffen Neubauerf3262672009-11-26 12:28:41 +0100323 return gsm340_vp_default();
324 }
325 minutes = vp/60;
326 return minutes;
327}
328
329/* Decode validity period format 'relative in semi-octet representation' */
330static unsigned long gsm340_vp_relative_semioctet(u_int8_t *sms_vp)
331{
332 unsigned long minutes;
333 minutes = unbcdify(*sms_vp++)*60; /* hours */
334 minutes += unbcdify(*sms_vp++); /* minutes */
335 minutes += unbcdify(*sms_vp++)/60; /* seconds */
336 return minutes;
337}
338
339/* decode validity period. return minutes */
340static unsigned long gsm340_validity_period(u_int8_t sms_vpf, u_int8_t *sms_vp)
341{
342 u_int8_t fi; /* functionality indicator */
Harald Welte7e310b12009-03-30 20:56:32 +0000343
Harald Welte76042182009-08-08 16:03:15 +0200344 switch (sms_vpf) {
Harald Welte7e310b12009-03-30 20:56:32 +0000345 case GSM340_TP_VPF_RELATIVE:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100346 return gsm340_vp_relative(sms_vp);
Harald Welte7e310b12009-03-30 20:56:32 +0000347 case GSM340_TP_VPF_ABSOLUTE:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100348 return gsm340_vp_absolute(sms_vp);
Harald Welte7e310b12009-03-30 20:56:32 +0000349 case GSM340_TP_VPF_ENHANCED:
350 /* Chapter 9.2.3.12.3 */
Steffen Neubauerf3262672009-11-26 12:28:41 +0100351 fi = *sms_vp++;
352 /* ignore additional fi */
353 if (fi & (1<<7)) sms_vp++;
354 /* read validity period format */
355 switch (fi & 0b111) {
356 case 0b000:
357 return gsm340_vp_default(); /* no vpf specified */
358 case 0b001:
359 return gsm340_vp_relative(sms_vp);
360 case 0b010:
361 return gsm340_vp_relative_integer(sms_vp);
362 case 0b011:
363 return gsm340_vp_relative_semioctet(sms_vp);
364 default:
365 /* The GSM spec says that the SC should reject any
366 unsupported and/or undefined values. FIXME */
Harald Welted0cf7ba2009-12-24 15:08:18 +0100367 LOGP(DSMS, LOGL_ERROR,
368 "Reserved enhanced validity period format\n");
Steffen Neubauerf3262672009-11-26 12:28:41 +0100369 return gsm340_vp_default();
370 }
Daniel Willmann58c83d82009-08-13 03:40:49 +0200371 case GSM340_TP_VPF_NONE:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100372 default:
373 return gsm340_vp_default();
Harald Welte7e310b12009-03-30 20:56:32 +0000374 }
Harald Welte7e310b12009-03-30 20:56:32 +0000375}
376
377/* determine coding alphabet dependent on GSM 03.38 Section 4 DCS */
378enum sms_alphabet gsm338_get_sms_alphabet(u_int8_t dcs)
379{
380 u_int8_t cgbits = dcs >> 4;
381 enum sms_alphabet alpha = DCS_NONE;
382
383 if ((cgbits & 0xc) == 0) {
Harald Weltee98c6352010-06-09 15:29:13 +0200384 if (cgbits & 2) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100385 LOGP(DSMS, LOGL_NOTICE,
386 "Compressed SMS not supported yet\n");
Harald Weltee98c6352010-06-09 15:29:13 +0200387 return 0xffffffff;
388 }
Harald Welte7e310b12009-03-30 20:56:32 +0000389
Daniel Willmannd5d5e1d2009-08-15 03:01:17 +0200390 switch ((dcs >> 2)&0x03) {
Harald Welte7e310b12009-03-30 20:56:32 +0000391 case 0:
392 alpha = DCS_7BIT_DEFAULT;
393 break;
394 case 1:
395 alpha = DCS_8BIT_DATA;
396 break;
397 case 2:
398 alpha = DCS_UCS2;
399 break;
400 }
401 } else if (cgbits == 0xc || cgbits == 0xd)
402 alpha = DCS_7BIT_DEFAULT;
403 else if (cgbits == 0xe)
404 alpha = DCS_UCS2;
405 else if (cgbits == 0xf) {
406 if (dcs & 4)
407 alpha = DCS_8BIT_DATA;
408 else
409 alpha = DCS_7BIT_DEFAULT;
410 }
411
412 return alpha;
413}
414
Harald Welte76042182009-08-08 16:03:15 +0200415static int gsm340_rx_sms_submit(struct msgb *msg, struct gsm_sms *gsms)
Harald Welte7e310b12009-03-30 20:56:32 +0000416{
417 if (db_sms_store(gsms) != 0) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100418 LOGP(DSMS, LOGL_ERROR, "Failed to store SMS in Database\n");
Harald Welteb9c758b2009-07-05 14:02:46 +0200419 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Harald Welte7e310b12009-03-30 20:56:32 +0000420 }
Harald Welte76042182009-08-08 16:03:15 +0200421 /* dispatch a signal to tell higher level about it */
422 dispatch_signal(SS_SMS, S_SMS_SUBMITTED, gsms);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200423
Harald Welte7e310b12009-03-30 20:56:32 +0000424 return 0;
425}
426
Harald Welte76042182009-08-08 16:03:15 +0200427/* generate a TPDU address field compliant with 03.40 sec. 9.1.2.5 */
428static int gsm340_gen_oa(u_int8_t *oa, unsigned int oa_len,
429 struct gsm_subscriber *subscr)
Harald Weltef3efc592009-07-27 20:11:35 +0200430{
Harald Welte76042182009-08-08 16:03:15 +0200431 int len_in_bytes;
Harald Weltef3efc592009-07-27 20:11:35 +0200432
Harald Welte76042182009-08-08 16:03:15 +0200433 oa[1] = 0xb9; /* networks-specific number, private numbering plan */
434
Harald Welte55c8f352010-03-07 23:40:35 +0100435 len_in_bytes = gsm48_encode_bcd_number(oa, oa_len, 1, subscr->extension);
Harald Welte76042182009-08-08 16:03:15 +0200436
437 /* GSM 03.40 tells us the length is in 'useful semi-octets' */
438 oa[0] = strlen(subscr->extension) & 0xff;
439
440 return len_in_bytes;
Harald Weltef3efc592009-07-27 20:11:35 +0200441}
442
Harald Welte76042182009-08-08 16:03:15 +0200443/* generate a msgb containing a TPDU derived from struct gsm_sms,
444 * returns total size of TPDU */
445static int gsm340_gen_tpdu(struct msgb *msg, struct gsm_sms *sms)
Harald Weltef3efc592009-07-27 20:11:35 +0200446{
Harald Weltef3efc592009-07-27 20:11:35 +0200447 u_int8_t *smsp;
448 u_int8_t oa[12]; /* max len per 03.40 */
449 u_int8_t oa_len = 0;
Daniel Willmann9aef1452009-08-13 03:39:07 +0200450 u_int8_t octet_len;
Harald Welte76042182009-08-08 16:03:15 +0200451 unsigned int old_msg_len = msg->len;
Harald Weltef3efc592009-07-27 20:11:35 +0200452
453 /* generate first octet with masked bits */
454 smsp = msgb_put(msg, 1);
Harald Welte76042182009-08-08 16:03:15 +0200455 /* TP-MTI (message type indicator) */
Harald Weltef3efc592009-07-27 20:11:35 +0200456 *smsp = GSM340_SMS_DELIVER_SC2MS;
Harald Welte76042182009-08-08 16:03:15 +0200457 /* TP-MMS (more messages to send) */
458 if (0 /* FIXME */)
Harald Weltef3efc592009-07-27 20:11:35 +0200459 *smsp |= 0x04;
Harald Welte76042182009-08-08 16:03:15 +0200460 /* TP-SRI(deliver)/SRR(submit) */
Harald Weltef3efc592009-07-27 20:11:35 +0200461 if (sms->status_rep_req)
462 *smsp |= 0x20;
Harald Welte76042182009-08-08 16:03:15 +0200463 /* TP-UDHI (indicating TP-UD contains a header) */
464 if (sms->ud_hdr_ind)
Harald Weltef3efc592009-07-27 20:11:35 +0200465 *smsp |= 0x40;
Harald Weltef3efc592009-07-27 20:11:35 +0200466
467 /* generate originator address */
Harald Welte76042182009-08-08 16:03:15 +0200468 oa_len = gsm340_gen_oa(oa, sizeof(oa), sms->sender);
Harald Weltef3efc592009-07-27 20:11:35 +0200469 smsp = msgb_put(msg, oa_len);
Harald Weltef3efc592009-07-27 20:11:35 +0200470 memcpy(smsp, oa, oa_len);
471
472 /* generate TP-PID */
473 smsp = msgb_put(msg, 1);
474 *smsp = sms->protocol_id;
475
476 /* generate TP-DCS */
477 smsp = msgb_put(msg, 1);
478 *smsp = sms->data_coding_scheme;
479
480 /* generate TP-SCTS */
481 smsp = msgb_put(msg, 7);
482 gsm340_gen_scts(smsp, time(NULL));
Harald Welte76042182009-08-08 16:03:15 +0200483
Harald Weltef3efc592009-07-27 20:11:35 +0200484 /* generate TP-UDL */
485 smsp = msgb_put(msg, 1);
Harald Welte76042182009-08-08 16:03:15 +0200486 *smsp = sms->user_data_len;
Harald Weltef3efc592009-07-27 20:11:35 +0200487
488 /* generate TP-UD */
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200489 switch (gsm338_get_sms_alphabet(sms->data_coding_scheme)) {
490 case DCS_7BIT_DEFAULT:
Daniel Willmann9aef1452009-08-13 03:39:07 +0200491 octet_len = sms->user_data_len*7/8;
492 if (sms->user_data_len*7%8 != 0)
493 octet_len++;
Daniel Willmann6b1e8222009-08-12 21:17:06 +0200494 /* Warning, user_data_len indicates the amount of septets
495 * (characters), we need amount of octets occupied */
Daniel Willmann9aef1452009-08-13 03:39:07 +0200496 smsp = msgb_put(msg, octet_len);
497 memcpy(smsp, sms->user_data, octet_len);
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200498 break;
499 case DCS_UCS2:
500 case DCS_8BIT_DATA:
501 smsp = msgb_put(msg, sms->user_data_len);
502 memcpy(smsp, sms->user_data, sms->user_data_len);
503 break;
504 default:
Harald Welted0cf7ba2009-12-24 15:08:18 +0100505 LOGP(DSMS, LOGL_NOTICE, "Unhandled Data Coding Scheme: 0x%02X\n",
506 sms->data_coding_scheme);
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200507 break;
Daniel Willmann6b1e8222009-08-12 21:17:06 +0200508 }
Harald Weltef3efc592009-07-27 20:11:35 +0200509
Harald Welte76042182009-08-08 16:03:15 +0200510 return msg->len - old_msg_len;
Harald Weltef3efc592009-07-27 20:11:35 +0200511}
512
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200513/* process an incoming TPDU (called from RP-DATA)
514 * return value > 0: RP CAUSE for ERROR; < 0: silent error; 0 = success */
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100515static int gsm340_rx_tpdu(struct gsm_subscriber_connection *conn, struct msgb *msg)
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000516{
517 u_int8_t *smsp = msgb_sms(msg);
Harald Welte7e310b12009-03-30 20:56:32 +0000518 struct gsm_sms *gsms;
Harald Welte76042182009-08-08 16:03:15 +0200519 u_int8_t sms_mti, sms_mms, sms_vpf, sms_alphabet, sms_rp;
520 u_int8_t *sms_vp;
Harald Welte7e310b12009-03-30 20:56:32 +0000521 u_int8_t da_len_bytes;
522 u_int8_t address_lv[12]; /* according to 03.40 / 9.1.2.5 */
523 int rc = 0;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000524
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100525 counter_inc(conn->bts->network->stats.sms.submitted);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100526
Harald Welte76042182009-08-08 16:03:15 +0200527 gsms = sms_alloc();
528 if (!gsms)
Harald Welteb9c758b2009-07-05 14:02:46 +0200529 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Harald Welte7e310b12009-03-30 20:56:32 +0000530
531 /* invert those fields where 0 means active/present */
Harald Welte76042182009-08-08 16:03:15 +0200532 sms_mti = *smsp & 0x03;
533 sms_mms = !!(*smsp & 0x04);
534 sms_vpf = (*smsp & 0x18) >> 3;
535 gsms->status_rep_req = (*smsp & 0x20);
536 gsms->ud_hdr_ind = (*smsp & 0x40);
537 sms_rp = (*smsp & 0x80);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000538
539 smsp++;
Harald Welte76042182009-08-08 16:03:15 +0200540 gsms->msg_ref = *smsp++;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000541
Harald Welte7e310b12009-03-30 20:56:32 +0000542 /* length in bytes of the destination address */
543 da_len_bytes = 2 + *smsp/2 + *smsp%2;
544 if (da_len_bytes > 12) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100545 LOGP(DSMS, LOGL_ERROR, "Destination Address > 12 bytes ?!?\n");
Harald Welteb9c758b2009-07-05 14:02:46 +0200546 rc = GSM411_RP_CAUSE_SEMANT_INC_MSG;
Harald Welte7e310b12009-03-30 20:56:32 +0000547 goto out;
548 }
Harald Welte3cfdb222009-06-12 02:42:11 +0800549 memset(address_lv, 0, sizeof(address_lv));
Harald Welte7e310b12009-03-30 20:56:32 +0000550 memcpy(address_lv, smsp, da_len_bytes);
551 /* mangle first byte to reflect length in bytes, not digits */
Harald Welte3cfdb222009-06-12 02:42:11 +0800552 address_lv[0] = da_len_bytes - 1;
Harald Welte7e310b12009-03-30 20:56:32 +0000553 /* convert to real number */
Harald Welte55c8f352010-03-07 23:40:35 +0100554 gsm48_decode_bcd_number(gsms->dest_addr, sizeof(gsms->dest_addr), address_lv, 1);
Harald Welte7e310b12009-03-30 20:56:32 +0000555 smsp += da_len_bytes;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000556
Harald Welte76042182009-08-08 16:03:15 +0200557 gsms->protocol_id = *smsp++;
558 gsms->data_coding_scheme = *smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000559
Harald Welte76042182009-08-08 16:03:15 +0200560 sms_alphabet = gsm338_get_sms_alphabet(gsms->data_coding_scheme);
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800561 if (sms_alphabet == 0xffffffff) {
562 sms_free(gsms);
Harald Weltee98c6352010-06-09 15:29:13 +0200563 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800564 }
Harald Welte7e310b12009-03-30 20:56:32 +0000565
Harald Welte76042182009-08-08 16:03:15 +0200566 switch (sms_vpf) {
Harald Welte7e310b12009-03-30 20:56:32 +0000567 case GSM340_TP_VPF_RELATIVE:
Harald Welte76042182009-08-08 16:03:15 +0200568 sms_vp = smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000569 break;
570 case GSM340_TP_VPF_ABSOLUTE:
571 case GSM340_TP_VPF_ENHANCED:
Harald Welte76042182009-08-08 16:03:15 +0200572 sms_vp = smsp;
Steffen Neubauerf3262672009-11-26 12:28:41 +0100573 /* the additional functionality indicator... */
Steffen Neubauerac0c13c2009-12-05 12:44:41 +0100574 if (sms_vpf == GSM340_TP_VPF_ENHANCED && *smsp & (1<<7))
575 smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000576 smsp += 7;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000577 break;
Daniel Willmann58c83d82009-08-13 03:40:49 +0200578 case GSM340_TP_VPF_NONE:
579 sms_vp = 0;
580 break;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000581 default:
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200582 LOGP(DSMS, LOGL_NOTICE,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100583 "SMS Validity period not implemented: 0x%02x\n", sms_vpf);
Harald Welte76042182009-08-08 16:03:15 +0200584 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000585 }
Harald Welte76042182009-08-08 16:03:15 +0200586 gsms->user_data_len = *smsp++;
587 if (gsms->user_data_len) {
588 memcpy(gsms->user_data, smsp, gsms->user_data_len);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000589
Harald Welte76042182009-08-08 16:03:15 +0200590 switch (sms_alphabet) {
Harald Welte7e310b12009-03-30 20:56:32 +0000591 case DCS_7BIT_DEFAULT:
Harald Welte76042182009-08-08 16:03:15 +0200592 gsm_7bit_decode(gsms->text, smsp, gsms->user_data_len);
Harald Welte7e310b12009-03-30 20:56:32 +0000593 break;
594 case DCS_8BIT_DATA:
595 case DCS_UCS2:
596 case DCS_NONE:
Harald Welte7e310b12009-03-30 20:56:32 +0000597 break;
598 }
599 }
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000600
Holger Hans Peter Freyther4f834092010-06-16 14:16:47 +0800601 gsms->sender = subscr_get(conn->subscr);
Harald Welted0cf7ba2009-12-24 15:08:18 +0100602
603 LOGP(DSMS, LOGL_INFO, "RX SMS: Sender: %s, MTI: 0x%02x, VPF: 0x%02x, "
604 "MR: 0x%02x PID: 0x%02x, DCS: 0x%02x, DA: %s, "
605 "UserDataLength: 0x%02x, UserData: \"%s\"\n",
606 subscr_name(gsms->sender), sms_mti, sms_vpf, gsms->msg_ref,
607 gsms->protocol_id, gsms->data_coding_scheme, gsms->dest_addr,
608 gsms->user_data_len,
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200609 sms_alphabet == DCS_7BIT_DEFAULT ? gsms->text :
Harald Welte76042182009-08-08 16:03:15 +0200610 hexdump(gsms->user_data, gsms->user_data_len));
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000611
Harald Welte76042182009-08-08 16:03:15 +0200612 gsms->validity_minutes = gsm340_validity_period(sms_vpf, sms_vp);
Harald Welteb9c758b2009-07-05 14:02:46 +0200613
Harald Welte76042182009-08-08 16:03:15 +0200614 dispatch_signal(SS_SMS, 0, gsms);
Harald Welteb9c758b2009-07-05 14:02:46 +0200615
Harald Welte7e310b12009-03-30 20:56:32 +0000616 /* determine gsms->receiver based on dialled number */
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100617 gsms->receiver = subscr_get_by_extension(conn->bts->network, gsms->dest_addr);
Harald Welte7e310b12009-03-30 20:56:32 +0000618 if (!gsms->receiver) {
619 rc = 1; /* cause 1: unknown subscriber */
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100620 counter_inc(conn->bts->network->stats.sms.no_receiver);
Harald Welte7e310b12009-03-30 20:56:32 +0000621 goto out;
622 }
623
Harald Welte76042182009-08-08 16:03:15 +0200624 switch (sms_mti) {
Harald Welte7e310b12009-03-30 20:56:32 +0000625 case GSM340_SMS_SUBMIT_MS2SC:
626 /* MS is submitting a SMS */
Harald Welte76042182009-08-08 16:03:15 +0200627 rc = gsm340_rx_sms_submit(msg, gsms);
Harald Welte7e310b12009-03-30 20:56:32 +0000628 break;
629 case GSM340_SMS_COMMAND_MS2SC:
630 case GSM340_SMS_DELIVER_REP_MS2SC:
Harald Welted0cf7ba2009-12-24 15:08:18 +0100631 LOGP(DSMS, LOGL_NOTICE, "Unimplemented MTI 0x%02x\n", sms_mti);
Harald Welteb9c758b2009-07-05 14:02:46 +0200632 rc = GSM411_RP_CAUSE_IE_NOTEXIST;
Harald Welte7e310b12009-03-30 20:56:32 +0000633 break;
634 default:
Harald Welted0cf7ba2009-12-24 15:08:18 +0100635 LOGP(DSMS, LOGL_NOTICE, "Undefined MTI 0x%02x\n", sms_mti);
Harald Welteb9c758b2009-07-05 14:02:46 +0200636 rc = GSM411_RP_CAUSE_IE_NOTEXIST;
Harald Welte7e310b12009-03-30 20:56:32 +0000637 break;
638 }
639
Harald Welteb9c758b2009-07-05 14:02:46 +0200640 if (!rc && !gsms->receiver)
641 rc = GSM411_RP_CAUSE_MO_NUM_UNASSIGNED;
642
Harald Welte7e310b12009-03-30 20:56:32 +0000643out:
Harald Welte76042182009-08-08 16:03:15 +0200644 sms_free(gsms);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000645
Harald Welte7e310b12009-03-30 20:56:32 +0000646 return rc;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000647}
648
Harald Weltef3efc592009-07-27 20:11:35 +0200649static int gsm411_send_rp_ack(struct gsm_trans *trans, u_int8_t msg_ref)
Daniel Willmann471712b2008-12-29 01:54:02 +0000650{
651 struct msgb *msg = gsm411_msgb_alloc();
Daniel Willmann471712b2008-12-29 01:54:02 +0000652
Daniel Willmann471712b2008-12-29 01:54:02 +0000653 DEBUGP(DSMS, "TX: SMS RP ACK\n");
654
Harald Weltef3efc592009-07-27 20:11:35 +0200655 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ACK_MT, msg_ref);
Daniel Willmann471712b2008-12-29 01:54:02 +0000656}
657
Harald Weltef3efc592009-07-27 20:11:35 +0200658static int gsm411_send_rp_error(struct gsm_trans *trans,
659 u_int8_t msg_ref, u_int8_t cause)
Daniel Willmann471712b2008-12-29 01:54:02 +0000660{
661 struct msgb *msg = gsm411_msgb_alloc();
Daniel Willmann471712b2008-12-29 01:54:02 +0000662
Harald Welte7e310b12009-03-30 20:56:32 +0000663 msgb_tv_put(msg, 1, cause);
Daniel Willmann471712b2008-12-29 01:54:02 +0000664
Harald Welted0cf7ba2009-12-24 15:08:18 +0100665 LOGP(DSMS, LOGL_NOTICE, "TX: SMS RP ERROR, cause %d (%s)\n", cause,
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200666 get_value_string(rp_cause_strs, cause));
Daniel Willmann471712b2008-12-29 01:54:02 +0000667
Harald Weltef3efc592009-07-27 20:11:35 +0200668 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ERROR_MT, msg_ref);
Daniel Willmann471712b2008-12-29 01:54:02 +0000669}
670
Harald Welte7e310b12009-03-30 20:56:32 +0000671/* Receive a 04.11 TPDU inside RP-DATA / user data */
Harald Weltef3efc592009-07-27 20:11:35 +0200672static int gsm411_rx_rp_ud(struct msgb *msg, struct gsm_trans *trans,
673 struct gsm411_rp_hdr *rph,
Harald Welte7e310b12009-03-30 20:56:32 +0000674 u_int8_t src_len, u_int8_t *src,
675 u_int8_t dst_len, u_int8_t *dst,
676 u_int8_t tpdu_len, u_int8_t *tpdu)
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000677{
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000678 int rc = 0;
679
Harald Welte7e310b12009-03-30 20:56:32 +0000680 if (src_len && src)
Harald Welted0cf7ba2009-12-24 15:08:18 +0100681 LOGP(DSMS, LOGL_ERROR, "RP-DATA (MO) with SRC ?!?\n");
Harald Welte7e310b12009-03-30 20:56:32 +0000682
683 if (!dst_len || !dst || !tpdu_len || !tpdu) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100684 LOGP(DSMS, LOGL_ERROR,
685 "RP-DATA (MO) without DST or TPDU ?!?\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200686 gsm411_send_rp_error(trans, rph->msg_ref,
Harald Welteb9c758b2009-07-05 14:02:46 +0200687 GSM411_RP_CAUSE_INV_MAND_INF);
Harald Welte7e310b12009-03-30 20:56:32 +0000688 return -EIO;
689 }
Harald Weltee9dd9b02010-04-30 14:27:05 +0200690 msg->l4h = tpdu;
Harald Welte7e310b12009-03-30 20:56:32 +0000691
692 DEBUGP(DSMS, "DST(%u,%s)\n", dst_len, hexdump(dst, dst_len));
Harald Welte7e310b12009-03-30 20:56:32 +0000693
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100694 rc = gsm340_rx_tpdu(trans->conn, msg);
Harald Welte7e310b12009-03-30 20:56:32 +0000695 if (rc == 0)
Harald Weltef3efc592009-07-27 20:11:35 +0200696 return gsm411_send_rp_ack(trans, rph->msg_ref);
Harald Welte7e310b12009-03-30 20:56:32 +0000697 else if (rc > 0)
Harald Weltef3efc592009-07-27 20:11:35 +0200698 return gsm411_send_rp_error(trans, rph->msg_ref, rc);
Harald Welte7e310b12009-03-30 20:56:32 +0000699 else
700 return rc;
701}
702
703/* Receive a 04.11 RP-DATA message in accordance with Section 7.3.1.2 */
Harald Weltef3efc592009-07-27 20:11:35 +0200704static int gsm411_rx_rp_data(struct msgb *msg, struct gsm_trans *trans,
705 struct gsm411_rp_hdr *rph)
Harald Welte7e310b12009-03-30 20:56:32 +0000706{
707 u_int8_t src_len, dst_len, rpud_len;
708 u_int8_t *src = NULL, *dst = NULL , *rp_ud = NULL;
709
710 /* in the MO case, this should always be zero length */
711 src_len = rph->data[0];
712 if (src_len)
713 src = &rph->data[1];
714
715 dst_len = rph->data[1+src_len];
716 if (dst_len)
717 dst = &rph->data[1+src_len+1];
718
719 rpud_len = rph->data[1+src_len+1+dst_len];
720 if (rpud_len)
721 rp_ud = &rph->data[1+src_len+1+dst_len+1];
722
Harald Welteb9c758b2009-07-05 14:02:46 +0200723 DEBUGP(DSMS, "RX_RP-DATA: src_len=%u, dst_len=%u ud_len=%u\n",
724 src_len, dst_len, rpud_len);
Harald Weltef3efc592009-07-27 20:11:35 +0200725 return gsm411_rx_rp_ud(msg, trans, rph, src_len, src, dst_len, dst,
Harald Welte7e310b12009-03-30 20:56:32 +0000726 rpud_len, rp_ud);
727}
728
Harald Weltecb8f4432009-08-09 14:59:02 +0200729/* Receive a 04.11 RP-ACK message (response to RP-DATA from us) */
Harald Weltef3efc592009-07-27 20:11:35 +0200730static int gsm411_rx_rp_ack(struct msgb *msg, struct gsm_trans *trans,
731 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200732{
Harald Welte76042182009-08-08 16:03:15 +0200733 struct gsm_sms *sms = trans->sms.sms;
734
Harald Welteb9c758b2009-07-05 14:02:46 +0200735 /* Acnkowledgement to MT RP_DATA, i.e. the MS confirms it
736 * successfully received a SMS. We can now safely mark it as
737 * transmitted */
738
Harald Weltecb8f4432009-08-09 14:59:02 +0200739 if (!trans->sms.is_mt) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100740 LOGP(DSMS, LOGL_ERROR, "RX RP-ACK on a MO transfer ?\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200741 return gsm411_send_rp_error(trans, rph->msg_ref,
742 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
743 }
Harald Welte3e0f6172009-07-09 23:52:59 +0200744
Harald Welte76042182009-08-08 16:03:15 +0200745 if (!sms) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100746 LOGP(DSMS, LOGL_ERROR, "RX RP-ACK but no sms in transaction?!?\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200747 return gsm411_send_rp_error(trans, rph->msg_ref,
748 GSM411_RP_CAUSE_PROTOCOL_ERR);
Harald Welte76042182009-08-08 16:03:15 +0200749 }
750
751 /* mark this SMS as sent in database */
752 db_sms_mark_sent(sms);
753
754 dispatch_signal(SS_SMS, S_SMS_DELIVERED, sms);
755
756 sms_free(sms);
757 trans->sms.sms = NULL;
758
Harald Weltecf6a3812009-08-09 19:07:41 +0200759 /* check for more messages for this subscriber */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100760 sms = db_sms_get_unsent_for_subscr(trans->subscr);
Harald Weltecf6a3812009-08-09 19:07:41 +0200761 if (sms)
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100762 gsm411_send_sms_lchan(trans->conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800763 else
764 gsm411_release_conn(trans->conn);
Sylvain Munautd6c35f62009-12-24 13:33:51 +0100765
766 /* free the transaction here */
767 trans_free(trans);
Harald Welte76042182009-08-08 16:03:15 +0200768 return 0;
Harald Welteb9c758b2009-07-05 14:02:46 +0200769}
770
Harald Weltef3efc592009-07-27 20:11:35 +0200771static int gsm411_rx_rp_error(struct msgb *msg, struct gsm_trans *trans,
772 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200773{
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100774 struct gsm_network *net = trans->conn->bts->network;
Harald Welte76042182009-08-08 16:03:15 +0200775 struct gsm_sms *sms = trans->sms.sms;
Harald Weltef3efc592009-07-27 20:11:35 +0200776 u_int8_t cause_len = rph->data[0];
777 u_int8_t cause = rph->data[1];
778
Harald Welteb9c758b2009-07-05 14:02:46 +0200779 /* Error in response to MT RP_DATA, i.e. the MS did not
780 * successfully receive the SMS. We need to investigate
781 * the cause and take action depending on it */
782
Harald Welted0cf7ba2009-12-24 15:08:18 +0100783 LOGP(DSMS, LOGL_NOTICE, "%s: RX SMS RP-ERROR, cause %d:%d (%s)\n",
Holger Hans Peter Freytherd4c16b12010-03-23 07:56:22 +0100784 subscr_name(trans->conn->subscr), cause_len, cause,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100785 get_value_string(rp_cause_strs, cause));
Harald Weltef3efc592009-07-27 20:11:35 +0200786
Harald Weltecb8f4432009-08-09 14:59:02 +0200787 if (!trans->sms.is_mt) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100788 LOGP(DSMS, LOGL_ERROR, "RX RP-ERR on a MO transfer ?\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200789#if 0
Harald Weltecb8f4432009-08-09 14:59:02 +0200790 return gsm411_send_rp_error(trans, rph->msg_ref,
791 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200792#endif
Harald Weltecb8f4432009-08-09 14:59:02 +0200793 }
Harald Welte3e0f6172009-07-09 23:52:59 +0200794
Harald Welte76042182009-08-08 16:03:15 +0200795 if (!sms) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100796 LOGP(DSMS, LOGL_ERROR,
797 "RX RP-ERR, but no sms in transaction?!?\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200798 return -EINVAL;
799#if 0
Harald Weltecb8f4432009-08-09 14:59:02 +0200800 return gsm411_send_rp_error(trans, rph->msg_ref,
801 GSM411_RP_CAUSE_PROTOCOL_ERR);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200802#endif
Harald Weltecb8f4432009-08-09 14:59:02 +0200803 }
804
805 if (cause == GSM411_RP_CAUSE_MT_MEM_EXCEEDED) {
806 /* MS has not enough memory to store the message. We need
807 * to store this in our database and wati for a SMMA message */
808 /* FIXME */
809 dispatch_signal(SS_SMS, S_SMS_MEM_EXCEEDED, trans->subscr);
Harald Welteffa55a42009-12-22 19:07:32 +0100810 counter_inc(net->stats.sms.rp_err_mem);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100811 } else
Harald Welteffa55a42009-12-22 19:07:32 +0100812 counter_inc(net->stats.sms.rp_err_other);
Harald Welte76042182009-08-08 16:03:15 +0200813
814 sms_free(sms);
815 trans->sms.sms = NULL;
816
Harald Weltef3efc592009-07-27 20:11:35 +0200817 return 0;
Harald Welteb9c758b2009-07-05 14:02:46 +0200818}
819
Harald Weltef3efc592009-07-27 20:11:35 +0200820static int gsm411_rx_rp_smma(struct msgb *msg, struct gsm_trans *trans,
821 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200822{
Harald Weltecf6a3812009-08-09 19:07:41 +0200823 struct gsm_sms *sms;
Harald Weltef3efc592009-07-27 20:11:35 +0200824 int rc;
825
Harald Weltecf6a3812009-08-09 19:07:41 +0200826 rc = gsm411_send_rp_ack(trans, rph->msg_ref);
827 trans->sms.rp_state = GSM411_RPS_IDLE;
828
Harald Welteb9c758b2009-07-05 14:02:46 +0200829 /* MS tells us that it has memory for more SMS, we need
830 * to check if we have any pending messages for it and then
831 * transfer those */
Harald Welte76042182009-08-08 16:03:15 +0200832 dispatch_signal(SS_SMS, S_SMS_SMMA, trans->subscr);
Harald Weltef3efc592009-07-27 20:11:35 +0200833
Harald Weltecf6a3812009-08-09 19:07:41 +0200834 /* check for more messages for this subscriber */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100835 sms = db_sms_get_unsent_for_subscr(trans->subscr);
Harald Weltecf6a3812009-08-09 19:07:41 +0200836 if (sms)
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100837 gsm411_send_sms_lchan(trans->conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800838 else
839 gsm411_release_conn(trans->conn);
Harald Weltef3efc592009-07-27 20:11:35 +0200840
841 return rc;
Harald Welteb9c758b2009-07-05 14:02:46 +0200842}
843
Harald Weltef3efc592009-07-27 20:11:35 +0200844static int gsm411_rx_cp_data(struct msgb *msg, struct gsm48_hdr *gh,
845 struct gsm_trans *trans)
Harald Welte7e310b12009-03-30 20:56:32 +0000846{
Daniel Willmann471712b2008-12-29 01:54:02 +0000847 struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000848 u_int8_t msg_type = rp_data->msg_type & 0x07;
Harald Welte7e310b12009-03-30 20:56:32 +0000849 int rc = 0;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000850
851 switch (msg_type) {
852 case GSM411_MT_RP_DATA_MO:
Harald Weltef3efc592009-07-27 20:11:35 +0200853 DEBUGP(DSMS, "RX SMS RP-DATA (MO)\n");
854 /* start TR2N and enter 'wait to send RP-ACK state' */
855 trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK;
856 rc = gsm411_rx_rp_data(msg, trans, rp_data);
Harald Welte7e310b12009-03-30 20:56:32 +0000857 break;
858 case GSM411_MT_RP_ACK_MO:
Harald Weltef3efc592009-07-27 20:11:35 +0200859 DEBUGP(DSMS,"RX SMS RP-ACK (MO)\n");
860 rc = gsm411_rx_rp_ack(msg, trans, rp_data);
Harald Welteb9c758b2009-07-05 14:02:46 +0200861 break;
Harald Welte7e310b12009-03-30 20:56:32 +0000862 case GSM411_MT_RP_SMMA_MO:
Harald Weltef3efc592009-07-27 20:11:35 +0200863 DEBUGP(DSMS, "RX SMS RP-SMMA\n");
864 /* start TR2N and enter 'wait to send RP-ACK state' */
865 trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK;
866 rc = gsm411_rx_rp_smma(msg, trans, rp_data);
867 break;
868 case GSM411_MT_RP_ERROR_MO:
869 rc = gsm411_rx_rp_error(msg, trans, rp_data);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000870 break;
871 default:
Harald Welted0cf7ba2009-12-24 15:08:18 +0100872 LOGP(DSMS, LOGL_NOTICE, "Invalid RP type 0x%02x\n", msg_type);
Harald Weltef3efc592009-07-27 20:11:35 +0200873 rc = gsm411_send_rp_error(trans, rp_data->msg_ref,
874 GSM411_RP_CAUSE_MSGTYPE_NOTEXIST);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000875 break;
876 }
877
878 return rc;
879}
880
Harald Weltef3efc592009-07-27 20:11:35 +0200881/* send CP-ACK to given transaction */
882static int gsm411_tx_cp_ack(struct gsm_trans *trans)
883{
884 struct msgb *msg = gsm411_msgb_alloc();
Harald Weltecf6a3812009-08-09 19:07:41 +0200885 int rc;
Harald Weltef3efc592009-07-27 20:11:35 +0200886
Harald Weltecf6a3812009-08-09 19:07:41 +0200887 rc = gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ACK);
888
889 if (trans->sms.is_mt) {
890 /* If this is a MT SMS DELIVER, we can clear transaction here */
891 trans->sms.cp_state = GSM411_CPS_IDLE;
Harald Welte (local)86b17172009-08-14 14:52:17 +0200892 //trans_free(trans);
Harald Weltecf6a3812009-08-09 19:07:41 +0200893 }
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200894
895 return rc;
Harald Weltef3efc592009-07-27 20:11:35 +0200896}
897
898static int gsm411_tx_cp_error(struct gsm_trans *trans, u_int8_t cause)
899{
900 struct msgb *msg = gsm411_msgb_alloc();
901 u_int8_t *causep;
902
Harald Welted0cf7ba2009-12-24 15:08:18 +0100903 LOGP(DSMS, LOGL_NOTICE, "TX CP-ERROR, cause %d (%s)\n", cause,
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200904 get_value_string(cp_cause_strs, cause));
905
Harald Welte76042182009-08-08 16:03:15 +0200906 causep = msgb_put(msg, 1);
Harald Weltef3efc592009-07-27 20:11:35 +0200907 *causep = cause;
908
909 return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ERROR);
910}
911
912/* Entry point for incoming GSM48_PDISC_SMS from abis_rsl.c */
Harald Welte (local)daef6062009-08-14 11:41:12 +0200913int gsm0411_rcv_sms(struct msgb *msg, u_int8_t link_id)
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000914{
915 struct gsm48_hdr *gh = msgb_l3(msg);
916 u_int8_t msg_type = gh->msg_type;
Harald Weltef3efc592009-07-27 20:11:35 +0200917 u_int8_t transaction_id = ((gh->proto_discr >> 4) ^ 0x8); /* flip */
918 struct gsm_lchan *lchan = msg->lchan;
919 struct gsm_trans *trans;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000920 int rc = 0;
921
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100922 if (!lchan->conn.subscr)
Harald Weltef3efc592009-07-27 20:11:35 +0200923 return -EIO;
924 /* FIXME: send some error message */
925
Sylvain Munautc56a1412009-12-18 18:28:08 +0100926 DEBUGP(DSMS, "trans_id=%x ", transaction_id);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100927 trans = trans_find_by_id(lchan->conn.subscr, GSM48_PDISC_SMS,
Harald Weltef3efc592009-07-27 20:11:35 +0200928 transaction_id);
929 if (!trans) {
Sylvain Munautfeaca922009-12-18 18:28:09 +0100930 DEBUGPC(DSMS, "(new) ");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100931 trans = trans_alloc(lchan->conn.subscr, GSM48_PDISC_SMS,
Harald Weltef3efc592009-07-27 20:11:35 +0200932 transaction_id, new_callref++);
933 if (!trans) {
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200934 DEBUGPC(DSMS, "No memory for trans\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200935 /* FIXME: send some error message */
936 return -ENOMEM;
937 }
938 trans->sms.cp_state = GSM411_CPS_IDLE;
939 trans->sms.rp_state = GSM411_RPS_IDLE;
940 trans->sms.is_mt = 0;
Harald Welte (local)daef6062009-08-14 11:41:12 +0200941 trans->sms.link_id = link_id;
Harald Weltef3efc592009-07-27 20:11:35 +0200942
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +0100943 trans->conn = &lchan->conn;
944 use_subscr_con(trans->conn);
Harald Weltef3efc592009-07-27 20:11:35 +0200945 }
946
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000947 switch(msg_type) {
948 case GSM411_MT_CP_DATA:
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200949 DEBUGPC(DSMS, "RX SMS CP-DATA\n");
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100950
951 /* 5.4: For MO, if a CP-DATA is received for a new
952 * transaction, equals reception of an implicit
953 * last CP-ACK for previous transaction */
954 if (trans->sms.cp_state == GSM411_CPS_IDLE) {
955 int i;
956 struct gsm_trans *ptrans;
957
958 /* Scan through all remote initiated transactions */
959 for (i=8; i<15; i++) {
960 if (i == transaction_id)
961 continue;
962
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100963 ptrans = trans_find_by_id(lchan->conn.subscr,
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100964 GSM48_PDISC_SMS, i);
965 if (!ptrans)
966 continue;
967
968 DEBUGP(DSMS, "Implicit CP-ACK for trans_id=%x\n", i);
969
970 /* Finish it for good */
971 bsc_del_timer(&ptrans->sms.cp_timer);
972 ptrans->sms.cp_state = GSM411_CPS_IDLE;
973 trans_free(ptrans);
974 }
975 }
976
Harald Weltecb8f4432009-08-09 14:59:02 +0200977 /* 5.2.3.1.3: MO state exists when SMC has received
978 * CP-DATA, including sending of the assoc. CP-ACK */
979 /* 5.2.3.2.4: MT state exists when SMC has received
980 * CP-DATA, including sending of the assoc. CP-ACK */
981 trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED;
Harald Weltef3efc592009-07-27 20:11:35 +0200982
Harald Welte (local)daef6062009-08-14 11:41:12 +0200983 /* SMC instance acknowledges the CP-DATA frame */
984 gsm411_tx_cp_ack(trans);
985
Harald Weltef3efc592009-07-27 20:11:35 +0200986 rc = gsm411_rx_cp_data(msg, gh, trans);
Harald Welte (local)daef6062009-08-14 11:41:12 +0200987#if 0
Harald Weltef3efc592009-07-27 20:11:35 +0200988 /* Send CP-ACK or CP-ERORR in response */
989 if (rc < 0) {
990 rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_NET_FAIL);
991 } else
992 rc = gsm411_tx_cp_ack(trans);
Harald Welte (local)daef6062009-08-14 11:41:12 +0200993#endif
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000994 break;
995 case GSM411_MT_CP_ACK:
Harald Weltef3efc592009-07-27 20:11:35 +0200996 /* previous CP-DATA in this transaction was confirmed */
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200997 DEBUGPC(DSMS, "RX SMS CP-ACK\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200998 /* 5.2.3.1.3: MO state exists when SMC has received CP-ACK */
999 /* 5.2.3.2.4: MT state exists when SMC has received CP-ACK */
1000 trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED;
Harald Welte41985612009-08-10 00:24:32 +02001001 /* Stop TC1* after CP-ACK has been received */
1002 bsc_del_timer(&trans->sms.cp_timer);
Harald Weltecb8f4432009-08-09 14:59:02 +02001003
Harald Weltef3efc592009-07-27 20:11:35 +02001004 if (!trans->sms.is_mt) {
Holger Hans Peter Freyther0afa0442010-06-10 18:08:12 +08001005 /* FIXME: we have sent one CP-DATA, which was now
Harald Weltef3efc592009-07-27 20:11:35 +02001006 * acknowledged. Check if we want to transfer more,
1007 * i.e. multi-part message */
1008 trans->sms.cp_state = GSM411_CPS_IDLE;
1009 trans_free(trans);
1010 }
Daniel Willmannbb16e8e2008-12-29 03:53:50 +00001011 break;
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001012 case GSM411_MT_CP_ERROR:
Harald Welte (local)c89a5112009-08-14 10:42:43 +02001013 DEBUGPC(DSMS, "RX SMS CP-ERROR, cause %d (%s)\n", gh->data[0],
1014 get_value_string(cp_cause_strs, gh->data[0]));
Harald Welte41985612009-08-10 00:24:32 +02001015 bsc_del_timer(&trans->sms.cp_timer);
Harald Weltef3efc592009-07-27 20:11:35 +02001016 trans->sms.cp_state = GSM411_CPS_IDLE;
1017 trans_free(trans);
Daniel Willmannbb16e8e2008-12-29 03:53:50 +00001018 break;
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001019 default:
Harald Welte (local)c89a5112009-08-14 10:42:43 +02001020 DEBUGPC(DSMS, "RX Unimplemented CP msg_type: 0x%02x\n", msg_type);
Harald Weltef3efc592009-07-27 20:11:35 +02001021 rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_MSGTYPE_NOTEXIST);
Harald Weltecb8f4432009-08-09 14:59:02 +02001022 trans->sms.cp_state = GSM411_CPS_IDLE;
Harald Weltef3efc592009-07-27 20:11:35 +02001023 trans_free(trans);
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001024 break;
1025 }
1026
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001027 return rc;
1028}
1029
Harald Weltecf6a3812009-08-09 19:07:41 +02001030/* Take a SMS in gsm_sms structure and send it through an already
1031 * existing lchan. We also assume that the caller ensured this lchan already
1032 * has a SAPI3 RLL connection! */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +01001033int gsm411_send_sms_lchan(struct gsm_subscriber_connection *conn, struct gsm_sms *sms)
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001034{
1035 struct msgb *msg = gsm411_msgb_alloc();
Harald Weltef3efc592009-07-27 20:11:35 +02001036 struct gsm_trans *trans;
Harald Welte76042182009-08-08 16:03:15 +02001037 u_int8_t *data, *rp_ud_len;
Harald Welte87f5d632009-07-04 17:39:00 +02001038 u_int8_t msg_ref = 42;
Sylvain Munautc4052cf2009-12-24 13:27:36 +01001039 int transaction_id;
Harald Welte76042182009-08-08 16:03:15 +02001040 int rc;
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001041
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +01001042 transaction_id = trans_assign_trans_id(conn->subscr, GSM48_PDISC_SMS, 0);
Harald Welte49839212009-12-22 13:45:58 +01001043 if (transaction_id == -1) {
Harald Welted0cf7ba2009-12-24 15:08:18 +01001044 LOGP(DSMS, LOGL_ERROR, "No available transaction ids\n");
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +08001045 sms_free(sms);
Harald Welte49839212009-12-22 13:45:58 +01001046 return -EBUSY;
1047 }
Harald Weltecf6a3812009-08-09 19:07:41 +02001048
Harald Welte76042182009-08-08 16:03:15 +02001049 DEBUGP(DSMS, "send_sms_lchan()\n");
Harald Weltef3efc592009-07-27 20:11:35 +02001050
Harald Welte76042182009-08-08 16:03:15 +02001051 /* FIXME: allocate transaction with message reference */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +01001052 trans = trans_alloc(conn->subscr, GSM48_PDISC_SMS,
Harald Welte76042182009-08-08 16:03:15 +02001053 transaction_id, new_callref++);
1054 if (!trans) {
Harald Welted0cf7ba2009-12-24 15:08:18 +01001055 LOGP(DSMS, LOGL_ERROR, "No memory for trans\n");
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +08001056 sms_free(sms);
Harald Welte76042182009-08-08 16:03:15 +02001057 /* FIXME: send some error message */
1058 return -ENOMEM;
1059 }
1060 trans->sms.cp_state = GSM411_CPS_IDLE;
1061 trans->sms.rp_state = GSM411_RPS_IDLE;
1062 trans->sms.is_mt = 1;
1063 trans->sms.sms = sms;
Harald Welte (local)daef6062009-08-14 11:41:12 +02001064 trans->sms.link_id = UM_SAPI_SMS; /* FIXME: main or SACCH ? */
Harald Welte76042182009-08-08 16:03:15 +02001065
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +01001066 trans->conn = conn;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001067 use_subscr_con(trans->conn);
Harald Welte76042182009-08-08 16:03:15 +02001068
1069 /* Hardcode SMSC Originating Address for now */
Daniel Willmanna3e29842008-12-29 16:03:54 +00001070 data = (u_int8_t *)msgb_put(msg, 8);
Harald Welte87f5d632009-07-04 17:39:00 +02001071 data[0] = 0x07; /* originator length == 7 */
Harald Welteb9c758b2009-07-05 14:02:46 +02001072 data[1] = 0x91; /* type of number: international, ISDN */
1073 data[2] = 0x44; /* 447785016005 */
Daniel Willmanna3e29842008-12-29 16:03:54 +00001074 data[3] = 0x77;
1075 data[4] = 0x58;
1076 data[5] = 0x10;
1077 data[6] = 0x06;
1078 data[7] = 0x50;
Harald Welte87f5d632009-07-04 17:39:00 +02001079
1080 /* Hardcoded Destination Address */
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001081 data = (u_int8_t *)msgb_put(msg, 1);
Harald Welte87f5d632009-07-04 17:39:00 +02001082 data[0] = 0; /* destination length == 0 */
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001083
Harald Welte76042182009-08-08 16:03:15 +02001084 /* obtain a pointer for the rp_ud_len, so we can fill it later */
1085 rp_ud_len = (u_int8_t *)msgb_put(msg, 1);
Daniel Willmann4a1e8792008-12-29 06:23:56 +00001086
Harald Welte76042182009-08-08 16:03:15 +02001087 /* generate the 03.40 TPDU */
1088 rc = gsm340_gen_tpdu(msg, sms);
1089 if (rc < 0) {
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +08001090 trans_free(trans);
1091 sms_free(sms);
Harald Welte76042182009-08-08 16:03:15 +02001092 msgb_free(msg);
1093 return rc;
1094 }
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001095
Harald Welte76042182009-08-08 16:03:15 +02001096 *rp_ud_len = rc;
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001097
Harald Welte76042182009-08-08 16:03:15 +02001098 DEBUGP(DSMS, "TX: SMS DELIVER\n");
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001099
Holger Hans Peter Freytherd4c16b12010-03-23 07:56:22 +01001100 counter_inc(conn->bts->network->stats.sms.delivered);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001101
Harald Weltef3efc592009-07-27 20:11:35 +02001102 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_DATA_MT, msg_ref);
1103 /* FIXME: enter 'wait for RP-ACK' state, start TR1N */
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001104}
Harald Weltef3efc592009-07-27 20:11:35 +02001105
Harald Weltecf6a3812009-08-09 19:07:41 +02001106/* paging callback. Here we get called if paging a subscriber has
1107 * succeeded or failed. */
Harald Welte76042182009-08-08 16:03:15 +02001108static int paging_cb_send_sms(unsigned int hooknum, unsigned int event,
1109 struct msgb *msg, void *_lchan, void *_sms)
Harald Weltef3efc592009-07-27 20:11:35 +02001110{
Harald Welte76042182009-08-08 16:03:15 +02001111 struct gsm_lchan *lchan = _lchan;
1112 struct gsm_sms *sms = _sms;
Holger Hans Peter Freyther4cd51582010-06-14 16:03:03 +08001113 int rc = 0;
Harald Weltef3efc592009-07-27 20:11:35 +02001114
Harald Welte76042182009-08-08 16:03:15 +02001115 DEBUGP(DSMS, "paging_cb_send_sms(hooknum=%u, event=%u, msg=%p,"
1116 "lchan=%p, sms=%p)\n", hooknum, event, msg, lchan, sms);
1117
1118 if (hooknum != GSM_HOOK_RR_PAGING)
1119 return -EINVAL;
1120
1121 switch (event) {
1122 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +08001123 use_subscr_con(&lchan->conn);
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001124 gsm411_send_sms_lchan(&lchan->conn, sms);
Harald Welte76042182009-08-08 16:03:15 +02001125 break;
1126 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherca5702a2010-06-10 18:20:54 +08001127 case GSM_PAGING_OOM:
Harald Welte76042182009-08-08 16:03:15 +02001128 sms_free(sms);
1129 rc = -ETIMEDOUT;
1130 break;
Harald Welte76042182009-08-08 16:03:15 +02001131 }
1132
1133 return rc;
1134}
1135
Harald Weltecf6a3812009-08-09 19:07:41 +02001136/* high-level function to send a SMS to a given subscriber. The function
1137 * will take care of paging the subscriber, establishing the RLL SAPI3
1138 * connection, etc. */
Harald Welte76042182009-08-08 16:03:15 +02001139int gsm411_send_sms_subscr(struct gsm_subscriber *subscr,
1140 struct gsm_sms *sms)
1141{
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001142 struct gsm_subscriber_connection *conn;
Harald Weltecf6a3812009-08-09 19:07:41 +02001143
Harald Welte76042182009-08-08 16:03:15 +02001144 /* check if we already have an open lchan to the subscriber.
1145 * if yes, send the SMS this way */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001146 conn = connection_for_subscr(subscr);
1147 if (conn) {
1148 use_subscr_con(conn);
1149 return gsm411_send_sms_lchan(conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +08001150 }
Harald Welte76042182009-08-08 16:03:15 +02001151
1152 /* if not, we have to start paging */
Holger Hans Peter Freyther7afac582010-06-10 18:21:25 +08001153 subscr_get_channel(subscr, RSL_CHANNEED_SDCCH, paging_cb_send_sms, sms);
Harald Welte76042182009-08-08 16:03:15 +02001154 return 0;
1155}
Harald Welte3366a942009-07-28 00:44:49 +02001156
Harald Weltecf6a3812009-08-09 19:07:41 +02001157static int subscr_sig_cb(unsigned int subsys, unsigned int signal,
1158 void *handler_data, void *signal_data)
1159{
1160 struct gsm_subscriber *subscr;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001161 struct gsm_subscriber_connection *conn;
Harald Weltecf6a3812009-08-09 19:07:41 +02001162 struct gsm_sms *sms;
1163
1164 switch (signal) {
1165 case S_SUBSCR_ATTACHED:
1166 /* A subscriber has attached. Check if there are
1167 * any pending SMS for him to be delivered */
1168 subscr = signal_data;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001169 conn = connection_for_subscr(subscr);
1170 if (!conn)
Harald Weltecf6a3812009-08-09 19:07:41 +02001171 break;
1172 sms = db_sms_get_unsent_for_subscr(subscr);
1173 if (!sms)
1174 break;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001175 use_subscr_con(conn);
1176 gsm411_send_sms_lchan(conn, sms);
Harald Weltecf6a3812009-08-09 19:07:41 +02001177 break;
1178 default:
1179 break;
1180 }
1181 return 0;
1182}
1183
Harald Welte (local)86b17172009-08-14 14:52:17 +02001184void _gsm411_sms_trans_free(struct gsm_trans *trans)
1185{
Holger Hans Peter Freythercea722b2010-06-15 12:29:37 +08001186 if (trans->sms.sms) {
1187 LOGP(DSMS, LOGL_ERROR, "Transaction contains SMS.\n");
1188 sms_free(trans->sms.sms);
1189 trans->sms.sms = NULL;
1190 }
1191
Harald Welte (local)86b17172009-08-14 14:52:17 +02001192 bsc_del_timer(&trans->sms.cp_timer);
1193}
1194
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001195void gsm411_sapi_n_reject(struct gsm_subscriber_connection *conn)
1196{
1197 struct gsm_trans *trans, *tmp;
1198
1199 llist_for_each_entry_safe(trans, tmp, &conn->bts->network->trans_list, entry)
1200 if (trans->conn == conn) {
1201 struct gsm_sms *sms = trans->sms.sms;
1202 if (!sms) {
1203 LOGP(DSMS, LOGL_ERROR, "SAPI Reject but no SMS.\n");
1204 continue;
1205 }
1206
1207 sms_free(sms);
1208 trans->sms.sms = NULL;
1209 trans_free(trans);
1210 }
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +08001211
1212 gsm411_release_conn(conn);
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001213}
1214
Harald Welte7bfc2672009-07-28 00:41:45 +02001215static __attribute__((constructor)) void on_dso_load_sms(void)
1216{
Harald Weltecf6a3812009-08-09 19:07:41 +02001217 register_signal_handler(SS_SUBSCR, subscr_sig_cb, NULL);
Harald Welte7bfc2672009-07-28 00:41:45 +02001218}