blob: dbb1fb56db082398d38cd57158875c4f53db56f2 [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
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +010029#include <assert.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <errno.h>
Harald Weltef3efc592009-07-27 20:11:35 +020034#include <time.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000035#include <netinet/in.h>
36
Harald Weltedfe6c7d2010-02-20 16:24:02 +010037#include <osmocore/msgb.h>
38#include <osmocore/tlv.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000039#include <openbsc/debug.h>
Daniel Willmann471712b2008-12-29 01:54:02 +000040#include <openbsc/gsm_data.h>
41#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000042#include <openbsc/gsm_04_11.h>
43#include <openbsc/gsm_04_08.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010044#include <osmocore/gsm_utils.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000045#include <openbsc/abis_rsl.h>
Holger Freyther9b177762009-02-16 19:07:18 +000046#include <openbsc/signal.h>
Harald Welte7e310b12009-03-30 20:56:32 +000047#include <openbsc/db.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010048#include <osmocore/talloc.h>
Harald Weltef3efc592009-07-27 20:11:35 +020049#include <openbsc/transaction.h>
Harald Welte76042182009-08-08 16:03:15 +020050#include <openbsc/paging.h>
Harald Weltecb8f4432009-08-09 14:59:02 +020051#include <openbsc/bsc_rll.h>
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020052#include <openbsc/chan_alloc.h>
Holger Hans Peter Freythercbac76e2010-05-23 21:05:18 +080053#include <openbsc/bsc_api.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000054
Daniel Willmann471712b2008-12-29 01:54:02 +000055#define GSM411_ALLOC_SIZE 1024
56#define GSM411_ALLOC_HEADROOM 128
57
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020058void *tall_gsms_ctx;
Harald Weltef3efc592009-07-27 20:11:35 +020059static u_int32_t new_callref = 0x40000001;
60
Harald Welte (local)c89a5112009-08-14 10:42:43 +020061static const struct value_string cp_cause_strs[] = {
62 { GSM411_CP_CAUSE_NET_FAIL, "Network Failure" },
63 { GSM411_CP_CAUSE_CONGESTION, "Congestion" },
64 { GSM411_CP_CAUSE_INV_TRANS_ID, "Invalid Transaction ID" },
65 { GSM411_CP_CAUSE_SEMANT_INC_MSG, "Semantically Incorrect Message" },
66 { GSM411_CP_CAUSE_INV_MAND_INF, "Invalid Mandatory Information" },
67 { GSM411_CP_CAUSE_MSGTYPE_NOTEXIST, "Message Type doesn't exist" },
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +020068 { GSM411_CP_CAUSE_MSG_INCOMP_STATE,
Harald Welte (local)c89a5112009-08-14 10:42:43 +020069 "Message incompatible with protocol state" },
70 { GSM411_CP_CAUSE_IE_NOTEXIST, "IE does not exist" },
71 { GSM411_CP_CAUSE_PROTOCOL_ERR, "Protocol Error" },
72 { 0, 0 }
73};
74
75static const struct value_string rp_cause_strs[] = {
76 { GSM411_RP_CAUSE_MO_NUM_UNASSIGNED, "(MO) Number not assigned" },
77 { GSM411_RP_CAUSE_MO_OP_DET_BARR, "(MO) Operator determined barring" },
78 { GSM411_RP_CAUSE_MO_CALL_BARRED, "(MO) Call barred" },
79 { GSM411_RP_CAUSE_MO_SMS_REJECTED, "(MO) SMS rejected" },
80 { GSM411_RP_CAUSE_MO_DEST_OUT_OF_ORDER, "(MO) Destination out of order" },
81 { GSM411_RP_CAUSE_MO_UNIDENTIFIED_SUBSCR, "(MO) Unidentified subscriber" },
82 { GSM411_RP_CAUSE_MO_FACILITY_REJ, "(MO) Facility reject" },
83 { GSM411_RP_CAUSE_MO_UNKNOWN_SUBSCR, "(MO) Unknown subscriber" },
84 { GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER, "(MO) Network out of order" },
85 { GSM411_RP_CAUSE_MO_TEMP_FAIL, "(MO) Temporary failure" },
86 { GSM411_RP_CAUSE_MO_CONGESTION, "(MO) Congestion" },
87 { GSM411_RP_CAUSE_MO_RES_UNAVAIL, "(MO) Resource unavailable" },
88 { GSM411_RP_CAUSE_MO_REQ_FAC_NOTSUBSCR, "(MO) Requested facility not subscribed" },
89 { GSM411_RP_CAUSE_MO_REQ_FAC_NOTIMPL, "(MO) Requested facility not implemented" },
90 { GSM411_RP_CAUSE_MO_INTERWORKING, "(MO) Interworking" },
91 /* valid only for MT */
92 { GSM411_RP_CAUSE_MT_MEM_EXCEEDED, "(MT) Memory Exceeded" },
93 /* valid for both directions */
94 { GSM411_RP_CAUSE_INV_TRANS_REF, "Invalid Transaction Reference" },
95 { GSM411_RP_CAUSE_SEMANT_INC_MSG, "Semantically Incorrect Message" },
96 { GSM411_RP_CAUSE_INV_MAND_INF, "Invalid Mandatory Information" },
97 { GSM411_RP_CAUSE_MSGTYPE_NOTEXIST, "Message Type non-existant" },
98 { GSM411_RP_CAUSE_MSG_INCOMP_STATE, "Message incompatible with protocol state" },
99 { GSM411_RP_CAUSE_IE_NOTEXIST, "Information Element not existing" },
100 { GSM411_RP_CAUSE_PROTOCOL_ERR, "Protocol Error" },
101 { 0, NULL }
102};
103
Harald Welte76042182009-08-08 16:03:15 +0200104struct gsm_sms *sms_alloc(void)
105{
106 return talloc_zero(tall_gsms_ctx, struct gsm_sms);
107}
108
109void sms_free(struct gsm_sms *sms)
110{
111 /* drop references to subscriber structure */
112 if (sms->sender)
113 subscr_put(sms->sender);
114 if (sms->receiver)
115 subscr_put(sms->receiver);
116
117 talloc_free(sms);
118}
119
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800120/*
121 * This should be called whenever all SMS to a given subscriber
122 * on a given connection has been sent. This will inform the higher
123 * layers that a channel can be given up.
124 */
125static void gsm411_release_conn(struct gsm_subscriber_connection *conn)
126{
Holger Hans Peter Freytherbddd1522010-06-16 14:02:41 +0800127 if (!conn)
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800128 return;
129
Holger Hans Peter Freytherbddd1522010-06-16 14:02:41 +0800130 subscr_put_channel(conn);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800131}
132
Holger Freythera553d092009-01-04 20:16:25 +0000133struct msgb *gsm411_msgb_alloc(void)
Daniel Willmann471712b2008-12-29 01:54:02 +0000134{
Harald Welte966636f2009-06-26 19:39:35 +0200135 return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM,
136 "GSM 04.11");
Daniel Willmann471712b2008-12-29 01:54:02 +0000137}
138
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100139static int gsm411_sendmsg(struct gsm_subscriber_connection *conn, struct msgb *msg, u_int8_t link_id)
Daniel Willmann471712b2008-12-29 01:54:02 +0000140{
Harald Welte76042182009-08-08 16:03:15 +0200141 DEBUGP(DSMS, "GSM4.11 TX %s\n", hexdump(msg->data, msg->len));
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100142 msg->l3h = msg->data;
143 return gsm0808_submit_dtap(conn, msg, link_id);
Daniel Willmann471712b2008-12-29 01:54:02 +0000144}
145
Harald Welte41985612009-08-10 00:24:32 +0200146/* SMC TC1* is expired */
147static void cp_timer_expired(void *data)
148{
149 struct gsm_trans *trans = data;
150
151 DEBUGP(DSMS, "SMC Timer TC1* is expired, calling trans_free()\n");
152 /* FIXME: we need to re-transmit the last CP-DATA 1..3 times */
153 trans_free(trans);
154}
155
Harald Welte87f5d632009-07-04 17:39:00 +0200156/* Prefix msg with a 04.08/04.11 CP header */
Harald Weltef3efc592009-07-27 20:11:35 +0200157static int gsm411_cp_sendmsg(struct msgb *msg, struct gsm_trans *trans,
158 u_int8_t msg_type)
Harald Welte87f5d632009-07-04 17:39:00 +0200159{
160 struct gsm48_hdr *gh;
161
162 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
163 /* Outgoing needs the highest bit set */
Harald Weltef3efc592009-07-27 20:11:35 +0200164 gh->proto_discr = trans->protocol | (trans->transaction_id<<4);
Harald Welte87f5d632009-07-04 17:39:00 +0200165 gh->msg_type = msg_type;
166
Harald Weltef3efc592009-07-27 20:11:35 +0200167 /* mobile originating */
168 switch (gh->msg_type) {
169 case GSM411_MT_CP_DATA:
170 /* 5.2.3.1.2: enter MO-wait for CP-ack */
Harald Weltecb8f4432009-08-09 14:59:02 +0200171 /* 5.2.3.2.3: enter MT-wait for CP-ACK */
Harald Weltef3efc592009-07-27 20:11:35 +0200172 trans->sms.cp_state = GSM411_CPS_WAIT_CP_ACK;
Harald Welte41985612009-08-10 00:24:32 +0200173 trans->sms.cp_timer.data = trans;
174 trans->sms.cp_timer.cb = cp_timer_expired;
175 /* 5.3.2.1: Set Timer TC1A */
176 bsc_schedule_timer(&trans->sms.cp_timer, GSM411_TMR_TC1A);
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200177 DEBUGP(DSMS, "TX: CP-DATA ");
178 break;
179 case GSM411_MT_CP_ACK:
180 DEBUGP(DSMS, "TX: CP-ACK ");
181 break;
182 case GSM411_MT_CP_ERROR:
Sylvain Munaut1d9efd62009-12-18 18:28:07 +0100183 DEBUGP(DSMS, "TX: CP-ERROR ");
Harald Weltef3efc592009-07-27 20:11:35 +0200184 break;
185 }
186
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200187 DEBUGPC(DSMS, "trans=%x\n", trans->transaction_id);
188
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100189 return gsm411_sendmsg(trans->conn, msg, trans->sms.link_id);
Harald Welte87f5d632009-07-04 17:39:00 +0200190}
191
192/* Prefix msg with a RP-DATA header and send as CP-DATA */
Harald Weltef3efc592009-07-27 20:11:35 +0200193static int gsm411_rp_sendmsg(struct msgb *msg, struct gsm_trans *trans,
194 u_int8_t rp_msg_type, u_int8_t rp_msg_ref)
Harald Welte87f5d632009-07-04 17:39:00 +0200195{
196 struct gsm411_rp_hdr *rp;
Harald Welte0d544e72009-08-10 00:22:19 +0200197 u_int8_t len = msg->len;
Harald Welte87f5d632009-07-04 17:39:00 +0200198
199 /* GSM 04.11 RP-DATA header */
200 rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp));
Harald Welte0d544e72009-08-10 00:22:19 +0200201 rp->len = len + 2;
Harald Welte87f5d632009-07-04 17:39:00 +0200202 rp->msg_type = rp_msg_type;
203 rp->msg_ref = rp_msg_ref; /* FIXME: Choose randomly */
204
Harald Weltef3efc592009-07-27 20:11:35 +0200205 return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_DATA);
Harald Welte87f5d632009-07-04 17:39:00 +0200206}
207
Steffen Neubauerf3262672009-11-26 12:28:41 +0100208/* Turn int into semi-octet representation: 98 => 0x89 */
209static u_int8_t bcdify(u_int8_t value)
210{
211 u_int8_t ret;
Steffen Neubauer5727cf42009-11-11 23:02:07 +0900212
Steffen Neubauerf3262672009-11-26 12:28:41 +0100213 ret = value / 10;
214 ret |= (value % 10) << 4;
215
216 return ret;
217}
218
219/* Turn semi-octet representation into int: 0x89 => 98 */
220static u_int8_t unbcdify(u_int8_t value)
221{
222 u_int8_t ret;
223
224 if ((value & 0x0F) > 9 || (value >> 4) > 9)
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200225 LOGP(DSMS, LOGL_ERROR,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100226 "unbcdify got too big nibble: 0x%02X\n", value);
Steffen Neubauerf3262672009-11-26 12:28:41 +0100227
228 ret = (value&0x0F)*10;
Steffen Neubauerad69d7f2009-11-26 23:38:41 +0100229 ret += value>>4;
Steffen Neubauerf3262672009-11-26 12:28:41 +0100230
231 return ret;
232}
233
234/* Generate 03.40 TP-SCTS */
235static void gsm340_gen_scts(u_int8_t *scts, time_t time)
236{
237 struct tm *tm = localtime(&time);
238
239 *scts++ = bcdify(tm->tm_year % 100);
240 *scts++ = bcdify(tm->tm_mon + 1);
241 *scts++ = bcdify(tm->tm_mday);
242 *scts++ = bcdify(tm->tm_hour);
243 *scts++ = bcdify(tm->tm_min);
244 *scts++ = bcdify(tm->tm_sec);
245 *scts++ = bcdify(tm->tm_gmtoff/(60*15));
246}
247
248/* Decode 03.40 TP-SCTS (into utc/gmt timestamp) */
249static time_t gsm340_scts(u_int8_t *scts)
250{
251 struct tm tm;
252
253 u_int8_t yr = unbcdify(*scts++);
254
255 if (yr <= 80)
256 tm.tm_year = 100 + yr;
257 else
258 tm.tm_year = yr;
259 tm.tm_mon = unbcdify(*scts++) - 1;
260 tm.tm_mday = unbcdify(*scts++);
261 tm.tm_hour = unbcdify(*scts++);
262 tm.tm_min = unbcdify(*scts++);
263 tm.tm_sec = unbcdify(*scts++);
264 /* according to gsm 03.40 time zone is
265 "expressed in quarters of an hour" */
266 tm.tm_gmtoff = unbcdify(*scts++) * 15*60;
267
268 return mktime(&tm);
269}
270
271/* Return the default validity period in minutes */
272static unsigned long gsm340_vp_default(void)
273{
274 unsigned long minutes;
275 /* Default validity: two days */
276 minutes = 24 * 60 * 2;
277 return minutes;
278}
279
280/* Decode validity period format 'relative' */
281static unsigned long gsm340_vp_relative(u_int8_t *sms_vp)
282{
283 /* Chapter 9.2.3.12.1 */
284 u_int8_t vp;
285 unsigned long minutes;
286
287 vp = *(sms_vp);
288 if (vp <= 143)
289 minutes = vp + 1 * 5;
290 else if (vp <= 167)
291 minutes = 12*60 + (vp-143) * 30;
292 else if (vp <= 196)
293 minutes = vp-166 * 60 * 24;
294 else
295 minutes = vp-192 * 60 * 24 * 7;
296 return minutes;
297}
298
299/* Decode validity period format 'absolute' */
300static unsigned long gsm340_vp_absolute(u_int8_t *sms_vp)
301{
302 /* Chapter 9.2.3.12.2 */
303 time_t expires, now;
304 unsigned long minutes;
305
306 expires = gsm340_scts(sms_vp);
307 now = mktime(gmtime(NULL));
308 if (expires <= now)
309 minutes = 0;
310 else
311 minutes = (expires-now)/60;
312 return minutes;
313}
314
315/* Decode validity period format 'relative in integer representation' */
316static unsigned long gsm340_vp_relative_integer(u_int8_t *sms_vp)
Harald Welte7e310b12009-03-30 20:56:32 +0000317{
318 u_int8_t vp;
319 unsigned long minutes;
Steffen Neubauerf3262672009-11-26 12:28:41 +0100320 vp = *(sms_vp);
321 if (vp == 0) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100322 LOGP(DSMS, LOGL_ERROR,
323 "reserved relative_integer validity period\n");
Steffen Neubauerf3262672009-11-26 12:28:41 +0100324 return gsm340_vp_default();
325 }
326 minutes = vp/60;
327 return minutes;
328}
329
330/* Decode validity period format 'relative in semi-octet representation' */
331static unsigned long gsm340_vp_relative_semioctet(u_int8_t *sms_vp)
332{
333 unsigned long minutes;
334 minutes = unbcdify(*sms_vp++)*60; /* hours */
335 minutes += unbcdify(*sms_vp++); /* minutes */
336 minutes += unbcdify(*sms_vp++)/60; /* seconds */
337 return minutes;
338}
339
340/* decode validity period. return minutes */
341static unsigned long gsm340_validity_period(u_int8_t sms_vpf, u_int8_t *sms_vp)
342{
343 u_int8_t fi; /* functionality indicator */
Harald Welte7e310b12009-03-30 20:56:32 +0000344
Harald Welte76042182009-08-08 16:03:15 +0200345 switch (sms_vpf) {
Harald Welte7e310b12009-03-30 20:56:32 +0000346 case GSM340_TP_VPF_RELATIVE:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100347 return gsm340_vp_relative(sms_vp);
Harald Welte7e310b12009-03-30 20:56:32 +0000348 case GSM340_TP_VPF_ABSOLUTE:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100349 return gsm340_vp_absolute(sms_vp);
Harald Welte7e310b12009-03-30 20:56:32 +0000350 case GSM340_TP_VPF_ENHANCED:
351 /* Chapter 9.2.3.12.3 */
Steffen Neubauerf3262672009-11-26 12:28:41 +0100352 fi = *sms_vp++;
353 /* ignore additional fi */
354 if (fi & (1<<7)) sms_vp++;
355 /* read validity period format */
356 switch (fi & 0b111) {
357 case 0b000:
358 return gsm340_vp_default(); /* no vpf specified */
359 case 0b001:
360 return gsm340_vp_relative(sms_vp);
361 case 0b010:
362 return gsm340_vp_relative_integer(sms_vp);
363 case 0b011:
364 return gsm340_vp_relative_semioctet(sms_vp);
365 default:
366 /* The GSM spec says that the SC should reject any
367 unsupported and/or undefined values. FIXME */
Harald Welted0cf7ba2009-12-24 15:08:18 +0100368 LOGP(DSMS, LOGL_ERROR,
369 "Reserved enhanced validity period format\n");
Steffen Neubauerf3262672009-11-26 12:28:41 +0100370 return gsm340_vp_default();
371 }
Daniel Willmann58c83d82009-08-13 03:40:49 +0200372 case GSM340_TP_VPF_NONE:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100373 default:
374 return gsm340_vp_default();
Harald Welte7e310b12009-03-30 20:56:32 +0000375 }
Harald Welte7e310b12009-03-30 20:56:32 +0000376}
377
378/* determine coding alphabet dependent on GSM 03.38 Section 4 DCS */
379enum sms_alphabet gsm338_get_sms_alphabet(u_int8_t dcs)
380{
381 u_int8_t cgbits = dcs >> 4;
382 enum sms_alphabet alpha = DCS_NONE;
383
384 if ((cgbits & 0xc) == 0) {
Harald Weltee98c6352010-06-09 15:29:13 +0200385 if (cgbits & 2) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100386 LOGP(DSMS, LOGL_NOTICE,
387 "Compressed SMS not supported yet\n");
Harald Weltee98c6352010-06-09 15:29:13 +0200388 return 0xffffffff;
389 }
Harald Welte7e310b12009-03-30 20:56:32 +0000390
Daniel Willmannd5d5e1d2009-08-15 03:01:17 +0200391 switch ((dcs >> 2)&0x03) {
Harald Welte7e310b12009-03-30 20:56:32 +0000392 case 0:
393 alpha = DCS_7BIT_DEFAULT;
394 break;
395 case 1:
396 alpha = DCS_8BIT_DATA;
397 break;
398 case 2:
399 alpha = DCS_UCS2;
400 break;
401 }
402 } else if (cgbits == 0xc || cgbits == 0xd)
403 alpha = DCS_7BIT_DEFAULT;
404 else if (cgbits == 0xe)
405 alpha = DCS_UCS2;
406 else if (cgbits == 0xf) {
407 if (dcs & 4)
408 alpha = DCS_8BIT_DATA;
409 else
410 alpha = DCS_7BIT_DEFAULT;
411 }
412
413 return alpha;
414}
415
Harald Welte76042182009-08-08 16:03:15 +0200416static int gsm340_rx_sms_submit(struct msgb *msg, struct gsm_sms *gsms)
Harald Welte7e310b12009-03-30 20:56:32 +0000417{
418 if (db_sms_store(gsms) != 0) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100419 LOGP(DSMS, LOGL_ERROR, "Failed to store SMS in Database\n");
Harald Welteb9c758b2009-07-05 14:02:46 +0200420 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Harald Welte7e310b12009-03-30 20:56:32 +0000421 }
Harald Welte76042182009-08-08 16:03:15 +0200422 /* dispatch a signal to tell higher level about it */
423 dispatch_signal(SS_SMS, S_SMS_SUBMITTED, gsms);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200424
Harald Welte7e310b12009-03-30 20:56:32 +0000425 return 0;
426}
427
Harald Welte76042182009-08-08 16:03:15 +0200428/* generate a TPDU address field compliant with 03.40 sec. 9.1.2.5 */
429static int gsm340_gen_oa(u_int8_t *oa, unsigned int oa_len,
430 struct gsm_subscriber *subscr)
Harald Weltef3efc592009-07-27 20:11:35 +0200431{
Harald Welte76042182009-08-08 16:03:15 +0200432 int len_in_bytes;
Harald Weltef3efc592009-07-27 20:11:35 +0200433
Harald Welte76042182009-08-08 16:03:15 +0200434 oa[1] = 0xb9; /* networks-specific number, private numbering plan */
435
Harald Welte55c8f352010-03-07 23:40:35 +0100436 len_in_bytes = gsm48_encode_bcd_number(oa, oa_len, 1, subscr->extension);
Harald Welte76042182009-08-08 16:03:15 +0200437
438 /* GSM 03.40 tells us the length is in 'useful semi-octets' */
439 oa[0] = strlen(subscr->extension) & 0xff;
440
441 return len_in_bytes;
Harald Weltef3efc592009-07-27 20:11:35 +0200442}
443
Harald Welte76042182009-08-08 16:03:15 +0200444/* generate a msgb containing a TPDU derived from struct gsm_sms,
445 * returns total size of TPDU */
446static int gsm340_gen_tpdu(struct msgb *msg, struct gsm_sms *sms)
Harald Weltef3efc592009-07-27 20:11:35 +0200447{
Harald Weltef3efc592009-07-27 20:11:35 +0200448 u_int8_t *smsp;
449 u_int8_t oa[12]; /* max len per 03.40 */
450 u_int8_t oa_len = 0;
Daniel Willmann9aef1452009-08-13 03:39:07 +0200451 u_int8_t octet_len;
Harald Welte76042182009-08-08 16:03:15 +0200452 unsigned int old_msg_len = msg->len;
Harald Weltef3efc592009-07-27 20:11:35 +0200453
454 /* generate first octet with masked bits */
455 smsp = msgb_put(msg, 1);
Harald Welte76042182009-08-08 16:03:15 +0200456 /* TP-MTI (message type indicator) */
Harald Weltef3efc592009-07-27 20:11:35 +0200457 *smsp = GSM340_SMS_DELIVER_SC2MS;
Harald Welte76042182009-08-08 16:03:15 +0200458 /* TP-MMS (more messages to send) */
459 if (0 /* FIXME */)
Harald Weltef3efc592009-07-27 20:11:35 +0200460 *smsp |= 0x04;
Harald Welte76042182009-08-08 16:03:15 +0200461 /* TP-SRI(deliver)/SRR(submit) */
Harald Weltef3efc592009-07-27 20:11:35 +0200462 if (sms->status_rep_req)
463 *smsp |= 0x20;
Harald Welte76042182009-08-08 16:03:15 +0200464 /* TP-UDHI (indicating TP-UD contains a header) */
465 if (sms->ud_hdr_ind)
Harald Weltef3efc592009-07-27 20:11:35 +0200466 *smsp |= 0x40;
Harald Weltef3efc592009-07-27 20:11:35 +0200467
468 /* generate originator address */
Harald Welte76042182009-08-08 16:03:15 +0200469 oa_len = gsm340_gen_oa(oa, sizeof(oa), sms->sender);
Harald Weltef3efc592009-07-27 20:11:35 +0200470 smsp = msgb_put(msg, oa_len);
Harald Weltef3efc592009-07-27 20:11:35 +0200471 memcpy(smsp, oa, oa_len);
472
473 /* generate TP-PID */
474 smsp = msgb_put(msg, 1);
475 *smsp = sms->protocol_id;
476
477 /* generate TP-DCS */
478 smsp = msgb_put(msg, 1);
479 *smsp = sms->data_coding_scheme;
480
481 /* generate TP-SCTS */
482 smsp = msgb_put(msg, 7);
483 gsm340_gen_scts(smsp, time(NULL));
Harald Welte76042182009-08-08 16:03:15 +0200484
Harald Weltef3efc592009-07-27 20:11:35 +0200485 /* generate TP-UDL */
486 smsp = msgb_put(msg, 1);
Harald Welte76042182009-08-08 16:03:15 +0200487 *smsp = sms->user_data_len;
Harald Weltef3efc592009-07-27 20:11:35 +0200488
489 /* generate TP-UD */
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200490 switch (gsm338_get_sms_alphabet(sms->data_coding_scheme)) {
491 case DCS_7BIT_DEFAULT:
Daniel Willmann9aef1452009-08-13 03:39:07 +0200492 octet_len = sms->user_data_len*7/8;
493 if (sms->user_data_len*7%8 != 0)
494 octet_len++;
Daniel Willmann6b1e8222009-08-12 21:17:06 +0200495 /* Warning, user_data_len indicates the amount of septets
496 * (characters), we need amount of octets occupied */
Daniel Willmann9aef1452009-08-13 03:39:07 +0200497 smsp = msgb_put(msg, octet_len);
498 memcpy(smsp, sms->user_data, octet_len);
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200499 break;
500 case DCS_UCS2:
501 case DCS_8BIT_DATA:
502 smsp = msgb_put(msg, sms->user_data_len);
503 memcpy(smsp, sms->user_data, sms->user_data_len);
504 break;
505 default:
Harald Welted0cf7ba2009-12-24 15:08:18 +0100506 LOGP(DSMS, LOGL_NOTICE, "Unhandled Data Coding Scheme: 0x%02X\n",
507 sms->data_coding_scheme);
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200508 break;
Daniel Willmann6b1e8222009-08-12 21:17:06 +0200509 }
Harald Weltef3efc592009-07-27 20:11:35 +0200510
Harald Welte76042182009-08-08 16:03:15 +0200511 return msg->len - old_msg_len;
Harald Weltef3efc592009-07-27 20:11:35 +0200512}
513
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200514/* process an incoming TPDU (called from RP-DATA)
515 * return value > 0: RP CAUSE for ERROR; < 0: silent error; 0 = success */
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100516static int gsm340_rx_tpdu(struct gsm_subscriber_connection *conn, struct msgb *msg)
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000517{
518 u_int8_t *smsp = msgb_sms(msg);
Harald Welte7e310b12009-03-30 20:56:32 +0000519 struct gsm_sms *gsms;
Harald Welte76042182009-08-08 16:03:15 +0200520 u_int8_t sms_mti, sms_mms, sms_vpf, sms_alphabet, sms_rp;
521 u_int8_t *sms_vp;
Harald Welte7e310b12009-03-30 20:56:32 +0000522 u_int8_t da_len_bytes;
523 u_int8_t address_lv[12]; /* according to 03.40 / 9.1.2.5 */
524 int rc = 0;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000525
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100526 counter_inc(conn->bts->network->stats.sms.submitted);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100527
Harald Welte76042182009-08-08 16:03:15 +0200528 gsms = sms_alloc();
529 if (!gsms)
Harald Welteb9c758b2009-07-05 14:02:46 +0200530 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Harald Welte7e310b12009-03-30 20:56:32 +0000531
532 /* invert those fields where 0 means active/present */
Harald Welte76042182009-08-08 16:03:15 +0200533 sms_mti = *smsp & 0x03;
534 sms_mms = !!(*smsp & 0x04);
535 sms_vpf = (*smsp & 0x18) >> 3;
536 gsms->status_rep_req = (*smsp & 0x20);
537 gsms->ud_hdr_ind = (*smsp & 0x40);
538 sms_rp = (*smsp & 0x80);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000539
540 smsp++;
Harald Welte76042182009-08-08 16:03:15 +0200541 gsms->msg_ref = *smsp++;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000542
Harald Welte7e310b12009-03-30 20:56:32 +0000543 /* length in bytes of the destination address */
544 da_len_bytes = 2 + *smsp/2 + *smsp%2;
545 if (da_len_bytes > 12) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100546 LOGP(DSMS, LOGL_ERROR, "Destination Address > 12 bytes ?!?\n");
Harald Welteb9c758b2009-07-05 14:02:46 +0200547 rc = GSM411_RP_CAUSE_SEMANT_INC_MSG;
Harald Welte7e310b12009-03-30 20:56:32 +0000548 goto out;
549 }
Harald Welte3cfdb222009-06-12 02:42:11 +0800550 memset(address_lv, 0, sizeof(address_lv));
Harald Welte7e310b12009-03-30 20:56:32 +0000551 memcpy(address_lv, smsp, da_len_bytes);
552 /* mangle first byte to reflect length in bytes, not digits */
Harald Welte3cfdb222009-06-12 02:42:11 +0800553 address_lv[0] = da_len_bytes - 1;
Harald Welte7e310b12009-03-30 20:56:32 +0000554 /* convert to real number */
Harald Welte55c8f352010-03-07 23:40:35 +0100555 gsm48_decode_bcd_number(gsms->dest_addr, sizeof(gsms->dest_addr), address_lv, 1);
Harald Welte7e310b12009-03-30 20:56:32 +0000556 smsp += da_len_bytes;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000557
Harald Welte76042182009-08-08 16:03:15 +0200558 gsms->protocol_id = *smsp++;
559 gsms->data_coding_scheme = *smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000560
Harald Welte76042182009-08-08 16:03:15 +0200561 sms_alphabet = gsm338_get_sms_alphabet(gsms->data_coding_scheme);
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800562 if (sms_alphabet == 0xffffffff) {
563 sms_free(gsms);
Harald Weltee98c6352010-06-09 15:29:13 +0200564 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800565 }
Harald Welte7e310b12009-03-30 20:56:32 +0000566
Harald Welte76042182009-08-08 16:03:15 +0200567 switch (sms_vpf) {
Harald Welte7e310b12009-03-30 20:56:32 +0000568 case GSM340_TP_VPF_RELATIVE:
Harald Welte76042182009-08-08 16:03:15 +0200569 sms_vp = smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000570 break;
571 case GSM340_TP_VPF_ABSOLUTE:
572 case GSM340_TP_VPF_ENHANCED:
Harald Welte76042182009-08-08 16:03:15 +0200573 sms_vp = smsp;
Steffen Neubauerf3262672009-11-26 12:28:41 +0100574 /* the additional functionality indicator... */
Steffen Neubauerac0c13c2009-12-05 12:44:41 +0100575 if (sms_vpf == GSM340_TP_VPF_ENHANCED && *smsp & (1<<7))
576 smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000577 smsp += 7;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000578 break;
Daniel Willmann58c83d82009-08-13 03:40:49 +0200579 case GSM340_TP_VPF_NONE:
580 sms_vp = 0;
581 break;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000582 default:
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200583 LOGP(DSMS, LOGL_NOTICE,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100584 "SMS Validity period not implemented: 0x%02x\n", sms_vpf);
Harald Welte76042182009-08-08 16:03:15 +0200585 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000586 }
Harald Welte76042182009-08-08 16:03:15 +0200587 gsms->user_data_len = *smsp++;
588 if (gsms->user_data_len) {
589 memcpy(gsms->user_data, smsp, gsms->user_data_len);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000590
Harald Welte76042182009-08-08 16:03:15 +0200591 switch (sms_alphabet) {
Harald Welte7e310b12009-03-30 20:56:32 +0000592 case DCS_7BIT_DEFAULT:
Harald Welte76042182009-08-08 16:03:15 +0200593 gsm_7bit_decode(gsms->text, smsp, gsms->user_data_len);
Harald Welte7e310b12009-03-30 20:56:32 +0000594 break;
595 case DCS_8BIT_DATA:
596 case DCS_UCS2:
597 case DCS_NONE:
Harald Welte7e310b12009-03-30 20:56:32 +0000598 break;
599 }
600 }
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000601
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100602 gsms->sender = subscr_get(msg->lchan->conn.subscr);
Harald Welted0cf7ba2009-12-24 15:08:18 +0100603
604 LOGP(DSMS, LOGL_INFO, "RX SMS: Sender: %s, MTI: 0x%02x, VPF: 0x%02x, "
605 "MR: 0x%02x PID: 0x%02x, DCS: 0x%02x, DA: %s, "
606 "UserDataLength: 0x%02x, UserData: \"%s\"\n",
607 subscr_name(gsms->sender), sms_mti, sms_vpf, gsms->msg_ref,
608 gsms->protocol_id, gsms->data_coding_scheme, gsms->dest_addr,
609 gsms->user_data_len,
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200610 sms_alphabet == DCS_7BIT_DEFAULT ? gsms->text :
Harald Welte76042182009-08-08 16:03:15 +0200611 hexdump(gsms->user_data, gsms->user_data_len));
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000612
Harald Welte76042182009-08-08 16:03:15 +0200613 gsms->validity_minutes = gsm340_validity_period(sms_vpf, sms_vp);
Harald Welteb9c758b2009-07-05 14:02:46 +0200614
Harald Welte76042182009-08-08 16:03:15 +0200615 dispatch_signal(SS_SMS, 0, gsms);
Harald Welteb9c758b2009-07-05 14:02:46 +0200616
Harald Welte7e310b12009-03-30 20:56:32 +0000617 /* determine gsms->receiver based on dialled number */
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100618 gsms->receiver = subscr_get_by_extension(conn->bts->network, gsms->dest_addr);
Harald Welte7e310b12009-03-30 20:56:32 +0000619 if (!gsms->receiver) {
620 rc = 1; /* cause 1: unknown subscriber */
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100621 counter_inc(conn->bts->network->stats.sms.no_receiver);
Harald Welte7e310b12009-03-30 20:56:32 +0000622 goto out;
623 }
624
Harald Welte76042182009-08-08 16:03:15 +0200625 switch (sms_mti) {
Harald Welte7e310b12009-03-30 20:56:32 +0000626 case GSM340_SMS_SUBMIT_MS2SC:
627 /* MS is submitting a SMS */
Harald Welte76042182009-08-08 16:03:15 +0200628 rc = gsm340_rx_sms_submit(msg, gsms);
Harald Welte7e310b12009-03-30 20:56:32 +0000629 break;
630 case GSM340_SMS_COMMAND_MS2SC:
631 case GSM340_SMS_DELIVER_REP_MS2SC:
Harald Welted0cf7ba2009-12-24 15:08:18 +0100632 LOGP(DSMS, LOGL_NOTICE, "Unimplemented MTI 0x%02x\n", sms_mti);
Harald Welteb9c758b2009-07-05 14:02:46 +0200633 rc = GSM411_RP_CAUSE_IE_NOTEXIST;
Harald Welte7e310b12009-03-30 20:56:32 +0000634 break;
635 default:
Harald Welted0cf7ba2009-12-24 15:08:18 +0100636 LOGP(DSMS, LOGL_NOTICE, "Undefined MTI 0x%02x\n", sms_mti);
Harald Welteb9c758b2009-07-05 14:02:46 +0200637 rc = GSM411_RP_CAUSE_IE_NOTEXIST;
Harald Welte7e310b12009-03-30 20:56:32 +0000638 break;
639 }
640
Harald Welteb9c758b2009-07-05 14:02:46 +0200641 if (!rc && !gsms->receiver)
642 rc = GSM411_RP_CAUSE_MO_NUM_UNASSIGNED;
643
Harald Welte7e310b12009-03-30 20:56:32 +0000644out:
Harald Welte76042182009-08-08 16:03:15 +0200645 sms_free(gsms);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000646
Harald Welte7e310b12009-03-30 20:56:32 +0000647 return rc;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000648}
649
Harald Weltef3efc592009-07-27 20:11:35 +0200650static int gsm411_send_rp_ack(struct gsm_trans *trans, u_int8_t msg_ref)
Daniel Willmann471712b2008-12-29 01:54:02 +0000651{
652 struct msgb *msg = gsm411_msgb_alloc();
Daniel Willmann471712b2008-12-29 01:54:02 +0000653
Daniel Willmann471712b2008-12-29 01:54:02 +0000654 DEBUGP(DSMS, "TX: SMS RP ACK\n");
655
Harald Weltef3efc592009-07-27 20:11:35 +0200656 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ACK_MT, msg_ref);
Daniel Willmann471712b2008-12-29 01:54:02 +0000657}
658
Harald Weltef3efc592009-07-27 20:11:35 +0200659static int gsm411_send_rp_error(struct gsm_trans *trans,
660 u_int8_t msg_ref, u_int8_t cause)
Daniel Willmann471712b2008-12-29 01:54:02 +0000661{
662 struct msgb *msg = gsm411_msgb_alloc();
Daniel Willmann471712b2008-12-29 01:54:02 +0000663
Harald Welte7e310b12009-03-30 20:56:32 +0000664 msgb_tv_put(msg, 1, cause);
Daniel Willmann471712b2008-12-29 01:54:02 +0000665
Harald Welted0cf7ba2009-12-24 15:08:18 +0100666 LOGP(DSMS, LOGL_NOTICE, "TX: SMS RP ERROR, cause %d (%s)\n", cause,
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200667 get_value_string(rp_cause_strs, cause));
Daniel Willmann471712b2008-12-29 01:54:02 +0000668
Harald Weltef3efc592009-07-27 20:11:35 +0200669 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ERROR_MT, msg_ref);
Daniel Willmann471712b2008-12-29 01:54:02 +0000670}
671
Harald Welte7e310b12009-03-30 20:56:32 +0000672/* Receive a 04.11 TPDU inside RP-DATA / user data */
Harald Weltef3efc592009-07-27 20:11:35 +0200673static int gsm411_rx_rp_ud(struct msgb *msg, struct gsm_trans *trans,
674 struct gsm411_rp_hdr *rph,
Harald Welte7e310b12009-03-30 20:56:32 +0000675 u_int8_t src_len, u_int8_t *src,
676 u_int8_t dst_len, u_int8_t *dst,
677 u_int8_t tpdu_len, u_int8_t *tpdu)
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000678{
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000679 int rc = 0;
680
Harald Welte7e310b12009-03-30 20:56:32 +0000681 if (src_len && src)
Harald Welted0cf7ba2009-12-24 15:08:18 +0100682 LOGP(DSMS, LOGL_ERROR, "RP-DATA (MO) with SRC ?!?\n");
Harald Welte7e310b12009-03-30 20:56:32 +0000683
684 if (!dst_len || !dst || !tpdu_len || !tpdu) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100685 LOGP(DSMS, LOGL_ERROR,
686 "RP-DATA (MO) without DST or TPDU ?!?\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200687 gsm411_send_rp_error(trans, rph->msg_ref,
Harald Welteb9c758b2009-07-05 14:02:46 +0200688 GSM411_RP_CAUSE_INV_MAND_INF);
Harald Welte7e310b12009-03-30 20:56:32 +0000689 return -EIO;
690 }
Harald Weltee9dd9b02010-04-30 14:27:05 +0200691 msg->l4h = tpdu;
Harald Welte7e310b12009-03-30 20:56:32 +0000692
693 DEBUGP(DSMS, "DST(%u,%s)\n", dst_len, hexdump(dst, dst_len));
Harald Welte7e310b12009-03-30 20:56:32 +0000694
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100695 rc = gsm340_rx_tpdu(trans->conn, msg);
Harald Welte7e310b12009-03-30 20:56:32 +0000696 if (rc == 0)
Harald Weltef3efc592009-07-27 20:11:35 +0200697 return gsm411_send_rp_ack(trans, rph->msg_ref);
Harald Welte7e310b12009-03-30 20:56:32 +0000698 else if (rc > 0)
Harald Weltef3efc592009-07-27 20:11:35 +0200699 return gsm411_send_rp_error(trans, rph->msg_ref, rc);
Harald Welte7e310b12009-03-30 20:56:32 +0000700 else
701 return rc;
702}
703
704/* Receive a 04.11 RP-DATA message in accordance with Section 7.3.1.2 */
Harald Weltef3efc592009-07-27 20:11:35 +0200705static int gsm411_rx_rp_data(struct msgb *msg, struct gsm_trans *trans,
706 struct gsm411_rp_hdr *rph)
Harald Welte7e310b12009-03-30 20:56:32 +0000707{
708 u_int8_t src_len, dst_len, rpud_len;
709 u_int8_t *src = NULL, *dst = NULL , *rp_ud = NULL;
710
711 /* in the MO case, this should always be zero length */
712 src_len = rph->data[0];
713 if (src_len)
714 src = &rph->data[1];
715
716 dst_len = rph->data[1+src_len];
717 if (dst_len)
718 dst = &rph->data[1+src_len+1];
719
720 rpud_len = rph->data[1+src_len+1+dst_len];
721 if (rpud_len)
722 rp_ud = &rph->data[1+src_len+1+dst_len+1];
723
Harald Welteb9c758b2009-07-05 14:02:46 +0200724 DEBUGP(DSMS, "RX_RP-DATA: src_len=%u, dst_len=%u ud_len=%u\n",
725 src_len, dst_len, rpud_len);
Harald Weltef3efc592009-07-27 20:11:35 +0200726 return gsm411_rx_rp_ud(msg, trans, rph, src_len, src, dst_len, dst,
Harald Welte7e310b12009-03-30 20:56:32 +0000727 rpud_len, rp_ud);
728}
729
Harald Weltecb8f4432009-08-09 14:59:02 +0200730/* Receive a 04.11 RP-ACK message (response to RP-DATA from us) */
Harald Weltef3efc592009-07-27 20:11:35 +0200731static int gsm411_rx_rp_ack(struct msgb *msg, struct gsm_trans *trans,
732 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200733{
Harald Welte76042182009-08-08 16:03:15 +0200734 struct gsm_sms *sms = trans->sms.sms;
735
Harald Welteb9c758b2009-07-05 14:02:46 +0200736 /* Acnkowledgement to MT RP_DATA, i.e. the MS confirms it
737 * successfully received a SMS. We can now safely mark it as
738 * transmitted */
739
Harald Weltecb8f4432009-08-09 14:59:02 +0200740 if (!trans->sms.is_mt) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100741 LOGP(DSMS, LOGL_ERROR, "RX RP-ACK on a MO transfer ?\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200742 return gsm411_send_rp_error(trans, rph->msg_ref,
743 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
744 }
Harald Welte3e0f6172009-07-09 23:52:59 +0200745
Harald Welte76042182009-08-08 16:03:15 +0200746 if (!sms) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100747 LOGP(DSMS, LOGL_ERROR, "RX RP-ACK but no sms in transaction?!?\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200748 return gsm411_send_rp_error(trans, rph->msg_ref,
749 GSM411_RP_CAUSE_PROTOCOL_ERR);
Harald Welte76042182009-08-08 16:03:15 +0200750 }
751
752 /* mark this SMS as sent in database */
753 db_sms_mark_sent(sms);
754
755 dispatch_signal(SS_SMS, S_SMS_DELIVERED, sms);
756
757 sms_free(sms);
758 trans->sms.sms = NULL;
759
Harald Weltecf6a3812009-08-09 19:07:41 +0200760 /* check for more messages for this subscriber */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100761 assert(msg->lchan->conn.subscr == trans->subscr);
762
763 sms = db_sms_get_unsent_for_subscr(trans->subscr);
Harald Weltecf6a3812009-08-09 19:07:41 +0200764 if (sms)
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100765 gsm411_send_sms_lchan(trans->conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800766 else
767 gsm411_release_conn(trans->conn);
Sylvain Munautd6c35f62009-12-24 13:33:51 +0100768
769 /* free the transaction here */
770 trans_free(trans);
Harald Welte76042182009-08-08 16:03:15 +0200771 return 0;
Harald Welteb9c758b2009-07-05 14:02:46 +0200772}
773
Harald Weltef3efc592009-07-27 20:11:35 +0200774static int gsm411_rx_rp_error(struct msgb *msg, struct gsm_trans *trans,
775 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200776{
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100777 struct gsm_network *net = trans->conn->bts->network;
Harald Welte76042182009-08-08 16:03:15 +0200778 struct gsm_sms *sms = trans->sms.sms;
Harald Weltef3efc592009-07-27 20:11:35 +0200779 u_int8_t cause_len = rph->data[0];
780 u_int8_t cause = rph->data[1];
781
Harald Welteb9c758b2009-07-05 14:02:46 +0200782 /* Error in response to MT RP_DATA, i.e. the MS did not
783 * successfully receive the SMS. We need to investigate
784 * the cause and take action depending on it */
785
Harald Welted0cf7ba2009-12-24 15:08:18 +0100786 LOGP(DSMS, LOGL_NOTICE, "%s: RX SMS RP-ERROR, cause %d:%d (%s)\n",
Holger Hans Peter Freytherd4c16b12010-03-23 07:56:22 +0100787 subscr_name(trans->conn->subscr), cause_len, cause,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100788 get_value_string(rp_cause_strs, cause));
Harald Weltef3efc592009-07-27 20:11:35 +0200789
Harald Weltecb8f4432009-08-09 14:59:02 +0200790 if (!trans->sms.is_mt) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100791 LOGP(DSMS, LOGL_ERROR, "RX RP-ERR on a MO transfer ?\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200792#if 0
Harald Weltecb8f4432009-08-09 14:59:02 +0200793 return gsm411_send_rp_error(trans, rph->msg_ref,
794 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200795#endif
Harald Weltecb8f4432009-08-09 14:59:02 +0200796 }
Harald Welte3e0f6172009-07-09 23:52:59 +0200797
Harald Welte76042182009-08-08 16:03:15 +0200798 if (!sms) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100799 LOGP(DSMS, LOGL_ERROR,
800 "RX RP-ERR, but no sms in transaction?!?\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200801 return -EINVAL;
802#if 0
Harald Weltecb8f4432009-08-09 14:59:02 +0200803 return gsm411_send_rp_error(trans, rph->msg_ref,
804 GSM411_RP_CAUSE_PROTOCOL_ERR);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200805#endif
Harald Weltecb8f4432009-08-09 14:59:02 +0200806 }
807
808 if (cause == GSM411_RP_CAUSE_MT_MEM_EXCEEDED) {
809 /* MS has not enough memory to store the message. We need
810 * to store this in our database and wati for a SMMA message */
811 /* FIXME */
812 dispatch_signal(SS_SMS, S_SMS_MEM_EXCEEDED, trans->subscr);
Harald Welteffa55a42009-12-22 19:07:32 +0100813 counter_inc(net->stats.sms.rp_err_mem);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100814 } else
Harald Welteffa55a42009-12-22 19:07:32 +0100815 counter_inc(net->stats.sms.rp_err_other);
Harald Welte76042182009-08-08 16:03:15 +0200816
817 sms_free(sms);
818 trans->sms.sms = NULL;
819
Harald Weltef3efc592009-07-27 20:11:35 +0200820 return 0;
Harald Welteb9c758b2009-07-05 14:02:46 +0200821}
822
Harald Weltef3efc592009-07-27 20:11:35 +0200823static int gsm411_rx_rp_smma(struct msgb *msg, struct gsm_trans *trans,
824 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200825{
Harald Weltecf6a3812009-08-09 19:07:41 +0200826 struct gsm_sms *sms;
Harald Weltef3efc592009-07-27 20:11:35 +0200827 int rc;
828
Harald Weltecf6a3812009-08-09 19:07:41 +0200829 rc = gsm411_send_rp_ack(trans, rph->msg_ref);
830 trans->sms.rp_state = GSM411_RPS_IDLE;
831
Harald Welteb9c758b2009-07-05 14:02:46 +0200832 /* MS tells us that it has memory for more SMS, we need
833 * to check if we have any pending messages for it and then
834 * transfer those */
Harald Welte76042182009-08-08 16:03:15 +0200835 dispatch_signal(SS_SMS, S_SMS_SMMA, trans->subscr);
Harald Weltef3efc592009-07-27 20:11:35 +0200836
Harald Weltecf6a3812009-08-09 19:07:41 +0200837 /* check for more messages for this subscriber */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100838 assert(msg->lchan->conn.subscr == trans->subscr);
839 sms = db_sms_get_unsent_for_subscr(trans->subscr);
Harald Weltecf6a3812009-08-09 19:07:41 +0200840 if (sms)
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100841 gsm411_send_sms_lchan(trans->conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800842 else
843 gsm411_release_conn(trans->conn);
Harald Weltef3efc592009-07-27 20:11:35 +0200844
845 return rc;
Harald Welteb9c758b2009-07-05 14:02:46 +0200846}
847
Harald Weltef3efc592009-07-27 20:11:35 +0200848static int gsm411_rx_cp_data(struct msgb *msg, struct gsm48_hdr *gh,
849 struct gsm_trans *trans)
Harald Welte7e310b12009-03-30 20:56:32 +0000850{
Daniel Willmann471712b2008-12-29 01:54:02 +0000851 struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000852 u_int8_t msg_type = rp_data->msg_type & 0x07;
Harald Welte7e310b12009-03-30 20:56:32 +0000853 int rc = 0;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000854
855 switch (msg_type) {
856 case GSM411_MT_RP_DATA_MO:
Harald Weltef3efc592009-07-27 20:11:35 +0200857 DEBUGP(DSMS, "RX SMS RP-DATA (MO)\n");
858 /* start TR2N and enter 'wait to send RP-ACK state' */
859 trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK;
860 rc = gsm411_rx_rp_data(msg, trans, rp_data);
Harald Welte7e310b12009-03-30 20:56:32 +0000861 break;
862 case GSM411_MT_RP_ACK_MO:
Harald Weltef3efc592009-07-27 20:11:35 +0200863 DEBUGP(DSMS,"RX SMS RP-ACK (MO)\n");
864 rc = gsm411_rx_rp_ack(msg, trans, rp_data);
Harald Welteb9c758b2009-07-05 14:02:46 +0200865 break;
Harald Welte7e310b12009-03-30 20:56:32 +0000866 case GSM411_MT_RP_SMMA_MO:
Harald Weltef3efc592009-07-27 20:11:35 +0200867 DEBUGP(DSMS, "RX SMS RP-SMMA\n");
868 /* start TR2N and enter 'wait to send RP-ACK state' */
869 trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK;
870 rc = gsm411_rx_rp_smma(msg, trans, rp_data);
871 break;
872 case GSM411_MT_RP_ERROR_MO:
873 rc = gsm411_rx_rp_error(msg, trans, rp_data);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000874 break;
875 default:
Harald Welted0cf7ba2009-12-24 15:08:18 +0100876 LOGP(DSMS, LOGL_NOTICE, "Invalid RP type 0x%02x\n", msg_type);
Harald Weltef3efc592009-07-27 20:11:35 +0200877 rc = gsm411_send_rp_error(trans, rp_data->msg_ref,
878 GSM411_RP_CAUSE_MSGTYPE_NOTEXIST);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000879 break;
880 }
881
882 return rc;
883}
884
Harald Weltef3efc592009-07-27 20:11:35 +0200885/* send CP-ACK to given transaction */
886static int gsm411_tx_cp_ack(struct gsm_trans *trans)
887{
888 struct msgb *msg = gsm411_msgb_alloc();
Harald Weltecf6a3812009-08-09 19:07:41 +0200889 int rc;
Harald Weltef3efc592009-07-27 20:11:35 +0200890
Harald Weltecf6a3812009-08-09 19:07:41 +0200891 rc = gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ACK);
892
893 if (trans->sms.is_mt) {
894 /* If this is a MT SMS DELIVER, we can clear transaction here */
895 trans->sms.cp_state = GSM411_CPS_IDLE;
Harald Welte (local)86b17172009-08-14 14:52:17 +0200896 //trans_free(trans);
Harald Weltecf6a3812009-08-09 19:07:41 +0200897 }
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200898
899 return rc;
Harald Weltef3efc592009-07-27 20:11:35 +0200900}
901
902static int gsm411_tx_cp_error(struct gsm_trans *trans, u_int8_t cause)
903{
904 struct msgb *msg = gsm411_msgb_alloc();
905 u_int8_t *causep;
906
Harald Welted0cf7ba2009-12-24 15:08:18 +0100907 LOGP(DSMS, LOGL_NOTICE, "TX CP-ERROR, cause %d (%s)\n", cause,
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200908 get_value_string(cp_cause_strs, cause));
909
Harald Welte76042182009-08-08 16:03:15 +0200910 causep = msgb_put(msg, 1);
Harald Weltef3efc592009-07-27 20:11:35 +0200911 *causep = cause;
912
913 return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ERROR);
914}
915
916/* Entry point for incoming GSM48_PDISC_SMS from abis_rsl.c */
Harald Welte (local)daef6062009-08-14 11:41:12 +0200917int gsm0411_rcv_sms(struct msgb *msg, u_int8_t link_id)
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000918{
919 struct gsm48_hdr *gh = msgb_l3(msg);
920 u_int8_t msg_type = gh->msg_type;
Harald Weltef3efc592009-07-27 20:11:35 +0200921 u_int8_t transaction_id = ((gh->proto_discr >> 4) ^ 0x8); /* flip */
922 struct gsm_lchan *lchan = msg->lchan;
923 struct gsm_trans *trans;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000924 int rc = 0;
925
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100926 if (!lchan->conn.subscr)
Harald Weltef3efc592009-07-27 20:11:35 +0200927 return -EIO;
928 /* FIXME: send some error message */
929
Sylvain Munautc56a1412009-12-18 18:28:08 +0100930 DEBUGP(DSMS, "trans_id=%x ", transaction_id);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100931 trans = trans_find_by_id(lchan->conn.subscr, GSM48_PDISC_SMS,
Harald Weltef3efc592009-07-27 20:11:35 +0200932 transaction_id);
933 if (!trans) {
Sylvain Munautfeaca922009-12-18 18:28:09 +0100934 DEBUGPC(DSMS, "(new) ");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100935 trans = trans_alloc(lchan->conn.subscr, GSM48_PDISC_SMS,
Harald Weltef3efc592009-07-27 20:11:35 +0200936 transaction_id, new_callref++);
937 if (!trans) {
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200938 DEBUGPC(DSMS, "No memory for trans\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200939 /* FIXME: send some error message */
940 return -ENOMEM;
941 }
942 trans->sms.cp_state = GSM411_CPS_IDLE;
943 trans->sms.rp_state = GSM411_RPS_IDLE;
944 trans->sms.is_mt = 0;
Harald Welte (local)daef6062009-08-14 11:41:12 +0200945 trans->sms.link_id = link_id;
Harald Weltef3efc592009-07-27 20:11:35 +0200946
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +0100947 trans->conn = &lchan->conn;
948 use_subscr_con(trans->conn);
Harald Weltef3efc592009-07-27 20:11:35 +0200949 }
950
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000951 switch(msg_type) {
952 case GSM411_MT_CP_DATA:
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200953 DEBUGPC(DSMS, "RX SMS CP-DATA\n");
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100954
955 /* 5.4: For MO, if a CP-DATA is received for a new
956 * transaction, equals reception of an implicit
957 * last CP-ACK for previous transaction */
958 if (trans->sms.cp_state == GSM411_CPS_IDLE) {
959 int i;
960 struct gsm_trans *ptrans;
961
962 /* Scan through all remote initiated transactions */
963 for (i=8; i<15; i++) {
964 if (i == transaction_id)
965 continue;
966
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100967 ptrans = trans_find_by_id(lchan->conn.subscr,
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100968 GSM48_PDISC_SMS, i);
969 if (!ptrans)
970 continue;
971
972 DEBUGP(DSMS, "Implicit CP-ACK for trans_id=%x\n", i);
973
974 /* Finish it for good */
975 bsc_del_timer(&ptrans->sms.cp_timer);
976 ptrans->sms.cp_state = GSM411_CPS_IDLE;
977 trans_free(ptrans);
978 }
979 }
980
Harald Weltecb8f4432009-08-09 14:59:02 +0200981 /* 5.2.3.1.3: MO state exists when SMC has received
982 * CP-DATA, including sending of the assoc. CP-ACK */
983 /* 5.2.3.2.4: MT state exists when SMC has received
984 * CP-DATA, including sending of the assoc. CP-ACK */
985 trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED;
Harald Weltef3efc592009-07-27 20:11:35 +0200986
Harald Welte (local)daef6062009-08-14 11:41:12 +0200987 /* SMC instance acknowledges the CP-DATA frame */
988 gsm411_tx_cp_ack(trans);
989
Harald Weltef3efc592009-07-27 20:11:35 +0200990 rc = gsm411_rx_cp_data(msg, gh, trans);
Harald Welte (local)daef6062009-08-14 11:41:12 +0200991#if 0
Harald Weltef3efc592009-07-27 20:11:35 +0200992 /* Send CP-ACK or CP-ERORR in response */
993 if (rc < 0) {
994 rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_NET_FAIL);
995 } else
996 rc = gsm411_tx_cp_ack(trans);
Harald Welte (local)daef6062009-08-14 11:41:12 +0200997#endif
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000998 break;
999 case GSM411_MT_CP_ACK:
Harald Weltef3efc592009-07-27 20:11:35 +02001000 /* previous CP-DATA in this transaction was confirmed */
Harald Welte (local)c89a5112009-08-14 10:42:43 +02001001 DEBUGPC(DSMS, "RX SMS CP-ACK\n");
Harald Weltecb8f4432009-08-09 14:59:02 +02001002 /* 5.2.3.1.3: MO state exists when SMC has received CP-ACK */
1003 /* 5.2.3.2.4: MT state exists when SMC has received CP-ACK */
1004 trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED;
Harald Welte41985612009-08-10 00:24:32 +02001005 /* Stop TC1* after CP-ACK has been received */
1006 bsc_del_timer(&trans->sms.cp_timer);
Harald Weltecb8f4432009-08-09 14:59:02 +02001007
Harald Weltef3efc592009-07-27 20:11:35 +02001008 if (!trans->sms.is_mt) {
Holger Hans Peter Freyther0afa0442010-06-10 18:08:12 +08001009 /* FIXME: we have sent one CP-DATA, which was now
Harald Weltef3efc592009-07-27 20:11:35 +02001010 * acknowledged. Check if we want to transfer more,
1011 * i.e. multi-part message */
1012 trans->sms.cp_state = GSM411_CPS_IDLE;
1013 trans_free(trans);
1014 }
Daniel Willmannbb16e8e2008-12-29 03:53:50 +00001015 break;
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001016 case GSM411_MT_CP_ERROR:
Harald Welte (local)c89a5112009-08-14 10:42:43 +02001017 DEBUGPC(DSMS, "RX SMS CP-ERROR, cause %d (%s)\n", gh->data[0],
1018 get_value_string(cp_cause_strs, gh->data[0]));
Harald Welte41985612009-08-10 00:24:32 +02001019 bsc_del_timer(&trans->sms.cp_timer);
Harald Weltef3efc592009-07-27 20:11:35 +02001020 trans->sms.cp_state = GSM411_CPS_IDLE;
1021 trans_free(trans);
Daniel Willmannbb16e8e2008-12-29 03:53:50 +00001022 break;
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001023 default:
Harald Welte (local)c89a5112009-08-14 10:42:43 +02001024 DEBUGPC(DSMS, "RX Unimplemented CP msg_type: 0x%02x\n", msg_type);
Harald Weltef3efc592009-07-27 20:11:35 +02001025 rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_MSGTYPE_NOTEXIST);
Harald Weltecb8f4432009-08-09 14:59:02 +02001026 trans->sms.cp_state = GSM411_CPS_IDLE;
Harald Weltef3efc592009-07-27 20:11:35 +02001027 trans_free(trans);
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001028 break;
1029 }
1030
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001031 return rc;
1032}
1033
Harald Weltecf6a3812009-08-09 19:07:41 +02001034/* Take a SMS in gsm_sms structure and send it through an already
1035 * existing lchan. We also assume that the caller ensured this lchan already
1036 * has a SAPI3 RLL connection! */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +01001037int gsm411_send_sms_lchan(struct gsm_subscriber_connection *conn, struct gsm_sms *sms)
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001038{
1039 struct msgb *msg = gsm411_msgb_alloc();
Harald Weltef3efc592009-07-27 20:11:35 +02001040 struct gsm_trans *trans;
Harald Welte76042182009-08-08 16:03:15 +02001041 u_int8_t *data, *rp_ud_len;
Harald Welte87f5d632009-07-04 17:39:00 +02001042 u_int8_t msg_ref = 42;
Sylvain Munautc4052cf2009-12-24 13:27:36 +01001043 int transaction_id;
Harald Welte76042182009-08-08 16:03:15 +02001044 int rc;
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001045
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +01001046 transaction_id = trans_assign_trans_id(conn->subscr, GSM48_PDISC_SMS, 0);
Harald Welte49839212009-12-22 13:45:58 +01001047 if (transaction_id == -1) {
Harald Welted0cf7ba2009-12-24 15:08:18 +01001048 LOGP(DSMS, LOGL_ERROR, "No available transaction ids\n");
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +08001049 sms_free(sms);
Harald Welte49839212009-12-22 13:45:58 +01001050 return -EBUSY;
1051 }
Harald Weltecf6a3812009-08-09 19:07:41 +02001052
Harald Welte76042182009-08-08 16:03:15 +02001053 DEBUGP(DSMS, "send_sms_lchan()\n");
Harald Weltef3efc592009-07-27 20:11:35 +02001054
Harald Welte76042182009-08-08 16:03:15 +02001055 /* FIXME: allocate transaction with message reference */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +01001056 trans = trans_alloc(conn->subscr, GSM48_PDISC_SMS,
Harald Welte76042182009-08-08 16:03:15 +02001057 transaction_id, new_callref++);
1058 if (!trans) {
Harald Welted0cf7ba2009-12-24 15:08:18 +01001059 LOGP(DSMS, LOGL_ERROR, "No memory for trans\n");
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +08001060 sms_free(sms);
Harald Welte76042182009-08-08 16:03:15 +02001061 /* FIXME: send some error message */
1062 return -ENOMEM;
1063 }
1064 trans->sms.cp_state = GSM411_CPS_IDLE;
1065 trans->sms.rp_state = GSM411_RPS_IDLE;
1066 trans->sms.is_mt = 1;
1067 trans->sms.sms = sms;
Harald Welte (local)daef6062009-08-14 11:41:12 +02001068 trans->sms.link_id = UM_SAPI_SMS; /* FIXME: main or SACCH ? */
Harald Welte76042182009-08-08 16:03:15 +02001069
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +01001070 trans->conn = conn;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001071 use_subscr_con(trans->conn);
Harald Welte76042182009-08-08 16:03:15 +02001072
1073 /* Hardcode SMSC Originating Address for now */
Daniel Willmanna3e29842008-12-29 16:03:54 +00001074 data = (u_int8_t *)msgb_put(msg, 8);
Harald Welte87f5d632009-07-04 17:39:00 +02001075 data[0] = 0x07; /* originator length == 7 */
Harald Welteb9c758b2009-07-05 14:02:46 +02001076 data[1] = 0x91; /* type of number: international, ISDN */
1077 data[2] = 0x44; /* 447785016005 */
Daniel Willmanna3e29842008-12-29 16:03:54 +00001078 data[3] = 0x77;
1079 data[4] = 0x58;
1080 data[5] = 0x10;
1081 data[6] = 0x06;
1082 data[7] = 0x50;
Harald Welte87f5d632009-07-04 17:39:00 +02001083
1084 /* Hardcoded Destination Address */
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001085 data = (u_int8_t *)msgb_put(msg, 1);
Harald Welte87f5d632009-07-04 17:39:00 +02001086 data[0] = 0; /* destination length == 0 */
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001087
Harald Welte76042182009-08-08 16:03:15 +02001088 /* obtain a pointer for the rp_ud_len, so we can fill it later */
1089 rp_ud_len = (u_int8_t *)msgb_put(msg, 1);
Daniel Willmann4a1e8792008-12-29 06:23:56 +00001090
Harald Welte76042182009-08-08 16:03:15 +02001091 /* generate the 03.40 TPDU */
1092 rc = gsm340_gen_tpdu(msg, sms);
1093 if (rc < 0) {
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +08001094 trans_free(trans);
1095 sms_free(sms);
Harald Welte76042182009-08-08 16:03:15 +02001096 msgb_free(msg);
1097 return rc;
1098 }
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001099
Harald Welte76042182009-08-08 16:03:15 +02001100 *rp_ud_len = rc;
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001101
Harald Welte76042182009-08-08 16:03:15 +02001102 DEBUGP(DSMS, "TX: SMS DELIVER\n");
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001103
Holger Hans Peter Freytherd4c16b12010-03-23 07:56:22 +01001104 counter_inc(conn->bts->network->stats.sms.delivered);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001105
Harald Weltef3efc592009-07-27 20:11:35 +02001106 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_DATA_MT, msg_ref);
1107 /* FIXME: enter 'wait for RP-ACK' state, start TR1N */
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001108}
Harald Weltef3efc592009-07-27 20:11:35 +02001109
Harald Weltecf6a3812009-08-09 19:07:41 +02001110/* paging callback. Here we get called if paging a subscriber has
1111 * succeeded or failed. */
Harald Welte76042182009-08-08 16:03:15 +02001112static int paging_cb_send_sms(unsigned int hooknum, unsigned int event,
1113 struct msgb *msg, void *_lchan, void *_sms)
Harald Weltef3efc592009-07-27 20:11:35 +02001114{
Harald Welte76042182009-08-08 16:03:15 +02001115 struct gsm_lchan *lchan = _lchan;
1116 struct gsm_sms *sms = _sms;
Holger Hans Peter Freyther4cd51582010-06-14 16:03:03 +08001117 int rc = 0;
Harald Weltef3efc592009-07-27 20:11:35 +02001118
Harald Welte76042182009-08-08 16:03:15 +02001119 DEBUGP(DSMS, "paging_cb_send_sms(hooknum=%u, event=%u, msg=%p,"
1120 "lchan=%p, sms=%p)\n", hooknum, event, msg, lchan, sms);
1121
1122 if (hooknum != GSM_HOOK_RR_PAGING)
1123 return -EINVAL;
1124
1125 switch (event) {
1126 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +08001127 use_subscr_con(&lchan->conn);
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001128 gsm411_send_sms_lchan(&lchan->conn, sms);
Harald Welte76042182009-08-08 16:03:15 +02001129 break;
1130 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherca5702a2010-06-10 18:20:54 +08001131 case GSM_PAGING_OOM:
Harald Welte76042182009-08-08 16:03:15 +02001132 sms_free(sms);
1133 rc = -ETIMEDOUT;
1134 break;
Harald Welte76042182009-08-08 16:03:15 +02001135 }
1136
1137 return rc;
1138}
1139
Harald Weltecf6a3812009-08-09 19:07:41 +02001140/* high-level function to send a SMS to a given subscriber. The function
1141 * will take care of paging the subscriber, establishing the RLL SAPI3
1142 * connection, etc. */
Harald Welte76042182009-08-08 16:03:15 +02001143int gsm411_send_sms_subscr(struct gsm_subscriber *subscr,
1144 struct gsm_sms *sms)
1145{
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001146 struct gsm_subscriber_connection *conn;
Harald Weltecf6a3812009-08-09 19:07:41 +02001147
Harald Welte76042182009-08-08 16:03:15 +02001148 /* check if we already have an open lchan to the subscriber.
1149 * if yes, send the SMS this way */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001150 conn = connection_for_subscr(subscr);
1151 if (conn) {
1152 use_subscr_con(conn);
1153 return gsm411_send_sms_lchan(conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +08001154 }
Harald Welte76042182009-08-08 16:03:15 +02001155
1156 /* if not, we have to start paging */
Holger Hans Peter Freyther7afac582010-06-10 18:21:25 +08001157 subscr_get_channel(subscr, RSL_CHANNEED_SDCCH, paging_cb_send_sms, sms);
Harald Welte76042182009-08-08 16:03:15 +02001158 return 0;
1159}
Harald Welte3366a942009-07-28 00:44:49 +02001160
Harald Weltecf6a3812009-08-09 19:07:41 +02001161static int subscr_sig_cb(unsigned int subsys, unsigned int signal,
1162 void *handler_data, void *signal_data)
1163{
1164 struct gsm_subscriber *subscr;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001165 struct gsm_subscriber_connection *conn;
Harald Weltecf6a3812009-08-09 19:07:41 +02001166 struct gsm_sms *sms;
1167
1168 switch (signal) {
1169 case S_SUBSCR_ATTACHED:
1170 /* A subscriber has attached. Check if there are
1171 * any pending SMS for him to be delivered */
1172 subscr = signal_data;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001173 conn = connection_for_subscr(subscr);
1174 if (!conn)
Harald Weltecf6a3812009-08-09 19:07:41 +02001175 break;
1176 sms = db_sms_get_unsent_for_subscr(subscr);
1177 if (!sms)
1178 break;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001179 use_subscr_con(conn);
1180 gsm411_send_sms_lchan(conn, sms);
Harald Weltecf6a3812009-08-09 19:07:41 +02001181 break;
1182 default:
1183 break;
1184 }
1185 return 0;
1186}
1187
Harald Welte (local)86b17172009-08-14 14:52:17 +02001188void _gsm411_sms_trans_free(struct gsm_trans *trans)
1189{
Holger Hans Peter Freythercea722b2010-06-15 12:29:37 +08001190 if (trans->sms.sms) {
1191 LOGP(DSMS, LOGL_ERROR, "Transaction contains SMS.\n");
1192 sms_free(trans->sms.sms);
1193 trans->sms.sms = NULL;
1194 }
1195
Harald Welte (local)86b17172009-08-14 14:52:17 +02001196 bsc_del_timer(&trans->sms.cp_timer);
1197}
1198
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001199void gsm411_sapi_n_reject(struct gsm_subscriber_connection *conn)
1200{
1201 struct gsm_trans *trans, *tmp;
1202
1203 llist_for_each_entry_safe(trans, tmp, &conn->bts->network->trans_list, entry)
1204 if (trans->conn == conn) {
1205 struct gsm_sms *sms = trans->sms.sms;
1206 if (!sms) {
1207 LOGP(DSMS, LOGL_ERROR, "SAPI Reject but no SMS.\n");
1208 continue;
1209 }
1210
1211 sms_free(sms);
1212 trans->sms.sms = NULL;
1213 trans_free(trans);
1214 }
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +08001215
1216 gsm411_release_conn(conn);
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001217}
1218
Harald Welte7bfc2672009-07-28 00:41:45 +02001219static __attribute__((constructor)) void on_dso_load_sms(void)
1220{
Harald Weltecf6a3812009-08-09 19:07:41 +02001221 register_signal_handler(SS_SUBSCR, subscr_sig_cb, NULL);
Harald Welte7bfc2672009-07-28 00:41:45 +02001222}