blob: 360ab074eb13fe141f45e67be7519335377907ac [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>
Nico Goldef11af072010-09-15 19:49:55 +020040#include <openbsc/db.h>
Daniel Willmann471712b2008-12-29 01:54:02 +000041#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
Holger Hans Peter Freyther0fbb3ec2010-06-16 14:19:31 +0800104static int gsm411_send_sms(struct gsm_subscriber_connection *conn, struct gsm_sms *sms);
105
Harald Welte76042182009-08-08 16:03:15 +0200106struct gsm_sms *sms_alloc(void)
107{
108 return talloc_zero(tall_gsms_ctx, struct gsm_sms);
109}
110
111void sms_free(struct gsm_sms *sms)
112{
113 /* drop references to subscriber structure */
114 if (sms->sender)
115 subscr_put(sms->sender);
116 if (sms->receiver)
117 subscr_put(sms->receiver);
118
119 talloc_free(sms);
120}
121
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800122/*
123 * This should be called whenever all SMS to a given subscriber
124 * on a given connection has been sent. This will inform the higher
125 * layers that a channel can be given up.
126 */
127static void gsm411_release_conn(struct gsm_subscriber_connection *conn)
128{
Holger Hans Peter Freytherbddd1522010-06-16 14:02:41 +0800129 if (!conn)
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800130 return;
131
Holger Hans Peter Freytherbddd1522010-06-16 14:02:41 +0800132 subscr_put_channel(conn);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800133}
134
Holger Freythera553d092009-01-04 20:16:25 +0000135struct msgb *gsm411_msgb_alloc(void)
Daniel Willmann471712b2008-12-29 01:54:02 +0000136{
Harald Welte966636f2009-06-26 19:39:35 +0200137 return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM,
138 "GSM 04.11");
Daniel Willmann471712b2008-12-29 01:54:02 +0000139}
140
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100141static int gsm411_sendmsg(struct gsm_subscriber_connection *conn, struct msgb *msg, u_int8_t link_id)
Daniel Willmann471712b2008-12-29 01:54:02 +0000142{
Harald Welte76042182009-08-08 16:03:15 +0200143 DEBUGP(DSMS, "GSM4.11 TX %s\n", hexdump(msg->data, msg->len));
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100144 msg->l3h = msg->data;
145 return gsm0808_submit_dtap(conn, msg, link_id);
Daniel Willmann471712b2008-12-29 01:54:02 +0000146}
147
Harald Welte41985612009-08-10 00:24:32 +0200148/* SMC TC1* is expired */
149static void cp_timer_expired(void *data)
150{
151 struct gsm_trans *trans = data;
152
153 DEBUGP(DSMS, "SMC Timer TC1* is expired, calling trans_free()\n");
154 /* FIXME: we need to re-transmit the last CP-DATA 1..3 times */
155 trans_free(trans);
156}
157
Harald Welte87f5d632009-07-04 17:39:00 +0200158/* Prefix msg with a 04.08/04.11 CP header */
Harald Weltef3efc592009-07-27 20:11:35 +0200159static int gsm411_cp_sendmsg(struct msgb *msg, struct gsm_trans *trans,
160 u_int8_t msg_type)
Harald Welte87f5d632009-07-04 17:39:00 +0200161{
162 struct gsm48_hdr *gh;
163
164 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
165 /* Outgoing needs the highest bit set */
Harald Weltef3efc592009-07-27 20:11:35 +0200166 gh->proto_discr = trans->protocol | (trans->transaction_id<<4);
Harald Welte87f5d632009-07-04 17:39:00 +0200167 gh->msg_type = msg_type;
168
Harald Weltef3efc592009-07-27 20:11:35 +0200169 /* mobile originating */
170 switch (gh->msg_type) {
171 case GSM411_MT_CP_DATA:
172 /* 5.2.3.1.2: enter MO-wait for CP-ack */
Harald Weltecb8f4432009-08-09 14:59:02 +0200173 /* 5.2.3.2.3: enter MT-wait for CP-ACK */
Harald Weltef3efc592009-07-27 20:11:35 +0200174 trans->sms.cp_state = GSM411_CPS_WAIT_CP_ACK;
Harald Welte41985612009-08-10 00:24:32 +0200175 trans->sms.cp_timer.data = trans;
176 trans->sms.cp_timer.cb = cp_timer_expired;
177 /* 5.3.2.1: Set Timer TC1A */
178 bsc_schedule_timer(&trans->sms.cp_timer, GSM411_TMR_TC1A);
Nico Goldef11af072010-09-15 19:49:55 +0200179 db_sms_inc_deliver_attempts(trans->sms.sms);
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200180 DEBUGP(DSMS, "TX: CP-DATA ");
181 break;
182 case GSM411_MT_CP_ACK:
183 DEBUGP(DSMS, "TX: CP-ACK ");
184 break;
185 case GSM411_MT_CP_ERROR:
Sylvain Munaut1d9efd62009-12-18 18:28:07 +0100186 DEBUGP(DSMS, "TX: CP-ERROR ");
Harald Weltef3efc592009-07-27 20:11:35 +0200187 break;
188 }
189
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200190 DEBUGPC(DSMS, "trans=%x\n", trans->transaction_id);
191
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100192 return gsm411_sendmsg(trans->conn, msg, trans->sms.link_id);
Harald Welte87f5d632009-07-04 17:39:00 +0200193}
194
195/* Prefix msg with a RP-DATA header and send as CP-DATA */
Harald Weltef3efc592009-07-27 20:11:35 +0200196static int gsm411_rp_sendmsg(struct msgb *msg, struct gsm_trans *trans,
197 u_int8_t rp_msg_type, u_int8_t rp_msg_ref)
Harald Welte87f5d632009-07-04 17:39:00 +0200198{
199 struct gsm411_rp_hdr *rp;
Harald Welte0d544e72009-08-10 00:22:19 +0200200 u_int8_t len = msg->len;
Harald Welte87f5d632009-07-04 17:39:00 +0200201
202 /* GSM 04.11 RP-DATA header */
203 rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp));
Harald Welte0d544e72009-08-10 00:22:19 +0200204 rp->len = len + 2;
Harald Welte87f5d632009-07-04 17:39:00 +0200205 rp->msg_type = rp_msg_type;
206 rp->msg_ref = rp_msg_ref; /* FIXME: Choose randomly */
207
Harald Weltef3efc592009-07-27 20:11:35 +0200208 return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_DATA);
Harald Welte87f5d632009-07-04 17:39:00 +0200209}
210
Steffen Neubauerf3262672009-11-26 12:28:41 +0100211/* Turn int into semi-octet representation: 98 => 0x89 */
212static u_int8_t bcdify(u_int8_t value)
213{
214 u_int8_t ret;
Steffen Neubauer5727cf42009-11-11 23:02:07 +0900215
Steffen Neubauerf3262672009-11-26 12:28:41 +0100216 ret = value / 10;
217 ret |= (value % 10) << 4;
218
219 return ret;
220}
221
222/* Turn semi-octet representation into int: 0x89 => 98 */
223static u_int8_t unbcdify(u_int8_t value)
224{
225 u_int8_t ret;
226
227 if ((value & 0x0F) > 9 || (value >> 4) > 9)
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200228 LOGP(DSMS, LOGL_ERROR,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100229 "unbcdify got too big nibble: 0x%02X\n", value);
Steffen Neubauerf3262672009-11-26 12:28:41 +0100230
231 ret = (value&0x0F)*10;
Steffen Neubauerad69d7f2009-11-26 23:38:41 +0100232 ret += value>>4;
Steffen Neubauerf3262672009-11-26 12:28:41 +0100233
234 return ret;
235}
236
237/* Generate 03.40 TP-SCTS */
238static void gsm340_gen_scts(u_int8_t *scts, time_t time)
239{
240 struct tm *tm = localtime(&time);
241
242 *scts++ = bcdify(tm->tm_year % 100);
243 *scts++ = bcdify(tm->tm_mon + 1);
244 *scts++ = bcdify(tm->tm_mday);
245 *scts++ = bcdify(tm->tm_hour);
246 *scts++ = bcdify(tm->tm_min);
247 *scts++ = bcdify(tm->tm_sec);
248 *scts++ = bcdify(tm->tm_gmtoff/(60*15));
249}
250
251/* Decode 03.40 TP-SCTS (into utc/gmt timestamp) */
252static time_t gsm340_scts(u_int8_t *scts)
253{
254 struct tm tm;
255
256 u_int8_t yr = unbcdify(*scts++);
257
258 if (yr <= 80)
259 tm.tm_year = 100 + yr;
260 else
261 tm.tm_year = yr;
262 tm.tm_mon = unbcdify(*scts++) - 1;
263 tm.tm_mday = unbcdify(*scts++);
264 tm.tm_hour = unbcdify(*scts++);
265 tm.tm_min = unbcdify(*scts++);
266 tm.tm_sec = unbcdify(*scts++);
267 /* according to gsm 03.40 time zone is
268 "expressed in quarters of an hour" */
269 tm.tm_gmtoff = unbcdify(*scts++) * 15*60;
270
271 return mktime(&tm);
272}
273
274/* Return the default validity period in minutes */
275static unsigned long gsm340_vp_default(void)
276{
277 unsigned long minutes;
278 /* Default validity: two days */
279 minutes = 24 * 60 * 2;
280 return minutes;
281}
282
283/* Decode validity period format 'relative' */
284static unsigned long gsm340_vp_relative(u_int8_t *sms_vp)
285{
286 /* Chapter 9.2.3.12.1 */
287 u_int8_t vp;
288 unsigned long minutes;
289
290 vp = *(sms_vp);
291 if (vp <= 143)
292 minutes = vp + 1 * 5;
293 else if (vp <= 167)
294 minutes = 12*60 + (vp-143) * 30;
295 else if (vp <= 196)
296 minutes = vp-166 * 60 * 24;
297 else
298 minutes = vp-192 * 60 * 24 * 7;
299 return minutes;
300}
301
302/* Decode validity period format 'absolute' */
303static unsigned long gsm340_vp_absolute(u_int8_t *sms_vp)
304{
305 /* Chapter 9.2.3.12.2 */
306 time_t expires, now;
307 unsigned long minutes;
308
309 expires = gsm340_scts(sms_vp);
Nico Golde4df2ad92010-06-16 15:22:30 +0200310 now = time(NULL);
Steffen Neubauerf3262672009-11-26 12:28:41 +0100311 if (expires <= now)
312 minutes = 0;
313 else
314 minutes = (expires-now)/60;
315 return minutes;
316}
317
318/* Decode validity period format 'relative in integer representation' */
319static unsigned long gsm340_vp_relative_integer(u_int8_t *sms_vp)
Harald Welte7e310b12009-03-30 20:56:32 +0000320{
321 u_int8_t vp;
322 unsigned long minutes;
Steffen Neubauerf3262672009-11-26 12:28:41 +0100323 vp = *(sms_vp);
324 if (vp == 0) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100325 LOGP(DSMS, LOGL_ERROR,
326 "reserved relative_integer validity period\n");
Steffen Neubauerf3262672009-11-26 12:28:41 +0100327 return gsm340_vp_default();
328 }
329 minutes = vp/60;
330 return minutes;
331}
332
333/* Decode validity period format 'relative in semi-octet representation' */
334static unsigned long gsm340_vp_relative_semioctet(u_int8_t *sms_vp)
335{
336 unsigned long minutes;
337 minutes = unbcdify(*sms_vp++)*60; /* hours */
338 minutes += unbcdify(*sms_vp++); /* minutes */
339 minutes += unbcdify(*sms_vp++)/60; /* seconds */
340 return minutes;
341}
342
343/* decode validity period. return minutes */
344static unsigned long gsm340_validity_period(u_int8_t sms_vpf, u_int8_t *sms_vp)
345{
346 u_int8_t fi; /* functionality indicator */
Harald Welte7e310b12009-03-30 20:56:32 +0000347
Harald Welte76042182009-08-08 16:03:15 +0200348 switch (sms_vpf) {
Harald Welte7e310b12009-03-30 20:56:32 +0000349 case GSM340_TP_VPF_RELATIVE:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100350 return gsm340_vp_relative(sms_vp);
Harald Welte7e310b12009-03-30 20:56:32 +0000351 case GSM340_TP_VPF_ABSOLUTE:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100352 return gsm340_vp_absolute(sms_vp);
Harald Welte7e310b12009-03-30 20:56:32 +0000353 case GSM340_TP_VPF_ENHANCED:
354 /* Chapter 9.2.3.12.3 */
Steffen Neubauerf3262672009-11-26 12:28:41 +0100355 fi = *sms_vp++;
356 /* ignore additional fi */
357 if (fi & (1<<7)) sms_vp++;
358 /* read validity period format */
Andreas.Eversberg2db4d462010-06-17 16:32:38 +0200359 switch (fi & 0x7) {
360 case 0x0:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100361 return gsm340_vp_default(); /* no vpf specified */
Andreas.Eversberg2db4d462010-06-17 16:32:38 +0200362 case 0x1:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100363 return gsm340_vp_relative(sms_vp);
Andreas.Eversberg2db4d462010-06-17 16:32:38 +0200364 case 0x2:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100365 return gsm340_vp_relative_integer(sms_vp);
Andreas.Eversberg2db4d462010-06-17 16:32:38 +0200366 case 0x3:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100367 return gsm340_vp_relative_semioctet(sms_vp);
368 default:
369 /* The GSM spec says that the SC should reject any
370 unsupported and/or undefined values. FIXME */
Harald Welted0cf7ba2009-12-24 15:08:18 +0100371 LOGP(DSMS, LOGL_ERROR,
372 "Reserved enhanced validity period format\n");
Steffen Neubauerf3262672009-11-26 12:28:41 +0100373 return gsm340_vp_default();
374 }
Daniel Willmann58c83d82009-08-13 03:40:49 +0200375 case GSM340_TP_VPF_NONE:
Steffen Neubauerf3262672009-11-26 12:28:41 +0100376 default:
377 return gsm340_vp_default();
Harald Welte7e310b12009-03-30 20:56:32 +0000378 }
Harald Welte7e310b12009-03-30 20:56:32 +0000379}
380
381/* determine coding alphabet dependent on GSM 03.38 Section 4 DCS */
382enum sms_alphabet gsm338_get_sms_alphabet(u_int8_t dcs)
383{
384 u_int8_t cgbits = dcs >> 4;
385 enum sms_alphabet alpha = DCS_NONE;
386
387 if ((cgbits & 0xc) == 0) {
Harald Weltee98c6352010-06-09 15:29:13 +0200388 if (cgbits & 2) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100389 LOGP(DSMS, LOGL_NOTICE,
390 "Compressed SMS not supported yet\n");
Harald Weltee98c6352010-06-09 15:29:13 +0200391 return 0xffffffff;
392 }
Harald Welte7e310b12009-03-30 20:56:32 +0000393
Daniel Willmannd5d5e1d2009-08-15 03:01:17 +0200394 switch ((dcs >> 2)&0x03) {
Harald Welte7e310b12009-03-30 20:56:32 +0000395 case 0:
396 alpha = DCS_7BIT_DEFAULT;
397 break;
398 case 1:
399 alpha = DCS_8BIT_DATA;
400 break;
401 case 2:
402 alpha = DCS_UCS2;
403 break;
404 }
405 } else if (cgbits == 0xc || cgbits == 0xd)
406 alpha = DCS_7BIT_DEFAULT;
407 else if (cgbits == 0xe)
408 alpha = DCS_UCS2;
409 else if (cgbits == 0xf) {
410 if (dcs & 4)
411 alpha = DCS_8BIT_DATA;
412 else
413 alpha = DCS_7BIT_DEFAULT;
414 }
415
416 return alpha;
417}
418
Harald Welte76042182009-08-08 16:03:15 +0200419static int gsm340_rx_sms_submit(struct msgb *msg, struct gsm_sms *gsms)
Harald Welte7e310b12009-03-30 20:56:32 +0000420{
421 if (db_sms_store(gsms) != 0) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100422 LOGP(DSMS, LOGL_ERROR, "Failed to store SMS in Database\n");
Harald Welteb9c758b2009-07-05 14:02:46 +0200423 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Harald Welte7e310b12009-03-30 20:56:32 +0000424 }
Harald Welte76042182009-08-08 16:03:15 +0200425 /* dispatch a signal to tell higher level about it */
426 dispatch_signal(SS_SMS, S_SMS_SUBMITTED, gsms);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200427
Harald Welte7e310b12009-03-30 20:56:32 +0000428 return 0;
429}
430
Harald Welte76042182009-08-08 16:03:15 +0200431/* generate a TPDU address field compliant with 03.40 sec. 9.1.2.5 */
432static int gsm340_gen_oa(u_int8_t *oa, unsigned int oa_len,
433 struct gsm_subscriber *subscr)
Harald Weltef3efc592009-07-27 20:11:35 +0200434{
Harald Welte76042182009-08-08 16:03:15 +0200435 int len_in_bytes;
Harald Weltef3efc592009-07-27 20:11:35 +0200436
Harald Welte76042182009-08-08 16:03:15 +0200437 oa[1] = 0xb9; /* networks-specific number, private numbering plan */
438
Harald Welte55c8f352010-03-07 23:40:35 +0100439 len_in_bytes = gsm48_encode_bcd_number(oa, oa_len, 1, subscr->extension);
Harald Welte76042182009-08-08 16:03:15 +0200440
441 /* GSM 03.40 tells us the length is in 'useful semi-octets' */
442 oa[0] = strlen(subscr->extension) & 0xff;
443
444 return len_in_bytes;
Harald Weltef3efc592009-07-27 20:11:35 +0200445}
446
Harald Welte76042182009-08-08 16:03:15 +0200447/* generate a msgb containing a TPDU derived from struct gsm_sms,
448 * returns total size of TPDU */
449static int gsm340_gen_tpdu(struct msgb *msg, struct gsm_sms *sms)
Harald Weltef3efc592009-07-27 20:11:35 +0200450{
Harald Weltef3efc592009-07-27 20:11:35 +0200451 u_int8_t *smsp;
452 u_int8_t oa[12]; /* max len per 03.40 */
453 u_int8_t oa_len = 0;
Daniel Willmann9aef1452009-08-13 03:39:07 +0200454 u_int8_t octet_len;
Harald Welte76042182009-08-08 16:03:15 +0200455 unsigned int old_msg_len = msg->len;
Harald Weltef3efc592009-07-27 20:11:35 +0200456
457 /* generate first octet with masked bits */
458 smsp = msgb_put(msg, 1);
Harald Welte76042182009-08-08 16:03:15 +0200459 /* TP-MTI (message type indicator) */
Harald Weltef3efc592009-07-27 20:11:35 +0200460 *smsp = GSM340_SMS_DELIVER_SC2MS;
Harald Welte76042182009-08-08 16:03:15 +0200461 /* TP-MMS (more messages to send) */
462 if (0 /* FIXME */)
Harald Weltef3efc592009-07-27 20:11:35 +0200463 *smsp |= 0x04;
Harald Welte76042182009-08-08 16:03:15 +0200464 /* TP-SRI(deliver)/SRR(submit) */
Harald Weltef3efc592009-07-27 20:11:35 +0200465 if (sms->status_rep_req)
466 *smsp |= 0x20;
Harald Welte76042182009-08-08 16:03:15 +0200467 /* TP-UDHI (indicating TP-UD contains a header) */
468 if (sms->ud_hdr_ind)
Harald Weltef3efc592009-07-27 20:11:35 +0200469 *smsp |= 0x40;
Harald Weltef3efc592009-07-27 20:11:35 +0200470
471 /* generate originator address */
Harald Welte76042182009-08-08 16:03:15 +0200472 oa_len = gsm340_gen_oa(oa, sizeof(oa), sms->sender);
Harald Weltef3efc592009-07-27 20:11:35 +0200473 smsp = msgb_put(msg, oa_len);
Harald Weltef3efc592009-07-27 20:11:35 +0200474 memcpy(smsp, oa, oa_len);
475
476 /* generate TP-PID */
477 smsp = msgb_put(msg, 1);
478 *smsp = sms->protocol_id;
479
480 /* generate TP-DCS */
481 smsp = msgb_put(msg, 1);
482 *smsp = sms->data_coding_scheme;
483
484 /* generate TP-SCTS */
485 smsp = msgb_put(msg, 7);
486 gsm340_gen_scts(smsp, time(NULL));
Harald Welte76042182009-08-08 16:03:15 +0200487
Harald Weltef3efc592009-07-27 20:11:35 +0200488 /* generate TP-UDL */
489 smsp = msgb_put(msg, 1);
Harald Welte76042182009-08-08 16:03:15 +0200490 *smsp = sms->user_data_len;
Harald Weltef3efc592009-07-27 20:11:35 +0200491
492 /* generate TP-UD */
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200493 switch (gsm338_get_sms_alphabet(sms->data_coding_scheme)) {
494 case DCS_7BIT_DEFAULT:
Daniel Willmann9aef1452009-08-13 03:39:07 +0200495 octet_len = sms->user_data_len*7/8;
496 if (sms->user_data_len*7%8 != 0)
497 octet_len++;
Daniel Willmann6b1e8222009-08-12 21:17:06 +0200498 /* Warning, user_data_len indicates the amount of septets
499 * (characters), we need amount of octets occupied */
Daniel Willmann9aef1452009-08-13 03:39:07 +0200500 smsp = msgb_put(msg, octet_len);
501 memcpy(smsp, sms->user_data, octet_len);
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200502 break;
503 case DCS_UCS2:
504 case DCS_8BIT_DATA:
505 smsp = msgb_put(msg, sms->user_data_len);
506 memcpy(smsp, sms->user_data, sms->user_data_len);
507 break;
508 default:
Harald Welted0cf7ba2009-12-24 15:08:18 +0100509 LOGP(DSMS, LOGL_NOTICE, "Unhandled Data Coding Scheme: 0x%02X\n",
510 sms->data_coding_scheme);
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200511 break;
Daniel Willmann6b1e8222009-08-12 21:17:06 +0200512 }
Harald Weltef3efc592009-07-27 20:11:35 +0200513
Harald Welte76042182009-08-08 16:03:15 +0200514 return msg->len - old_msg_len;
Harald Weltef3efc592009-07-27 20:11:35 +0200515}
516
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200517/* process an incoming TPDU (called from RP-DATA)
518 * return value > 0: RP CAUSE for ERROR; < 0: silent error; 0 = success */
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100519static int gsm340_rx_tpdu(struct gsm_subscriber_connection *conn, struct msgb *msg)
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000520{
521 u_int8_t *smsp = msgb_sms(msg);
Harald Welte7e310b12009-03-30 20:56:32 +0000522 struct gsm_sms *gsms;
Harald Welte76042182009-08-08 16:03:15 +0200523 u_int8_t sms_mti, sms_mms, sms_vpf, sms_alphabet, sms_rp;
524 u_int8_t *sms_vp;
Harald Welte7e310b12009-03-30 20:56:32 +0000525 u_int8_t da_len_bytes;
526 u_int8_t address_lv[12]; /* according to 03.40 / 9.1.2.5 */
527 int rc = 0;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000528
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100529 counter_inc(conn->bts->network->stats.sms.submitted);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100530
Harald Welte76042182009-08-08 16:03:15 +0200531 gsms = sms_alloc();
532 if (!gsms)
Harald Welteb9c758b2009-07-05 14:02:46 +0200533 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Harald Welte7e310b12009-03-30 20:56:32 +0000534
535 /* invert those fields where 0 means active/present */
Harald Welte76042182009-08-08 16:03:15 +0200536 sms_mti = *smsp & 0x03;
537 sms_mms = !!(*smsp & 0x04);
538 sms_vpf = (*smsp & 0x18) >> 3;
539 gsms->status_rep_req = (*smsp & 0x20);
540 gsms->ud_hdr_ind = (*smsp & 0x40);
541 sms_rp = (*smsp & 0x80);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000542
543 smsp++;
Harald Welte76042182009-08-08 16:03:15 +0200544 gsms->msg_ref = *smsp++;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000545
Harald Welte7e310b12009-03-30 20:56:32 +0000546 /* length in bytes of the destination address */
547 da_len_bytes = 2 + *smsp/2 + *smsp%2;
548 if (da_len_bytes > 12) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100549 LOGP(DSMS, LOGL_ERROR, "Destination Address > 12 bytes ?!?\n");
Harald Welteb9c758b2009-07-05 14:02:46 +0200550 rc = GSM411_RP_CAUSE_SEMANT_INC_MSG;
Harald Welte7e310b12009-03-30 20:56:32 +0000551 goto out;
552 }
Harald Welte3cfdb222009-06-12 02:42:11 +0800553 memset(address_lv, 0, sizeof(address_lv));
Harald Welte7e310b12009-03-30 20:56:32 +0000554 memcpy(address_lv, smsp, da_len_bytes);
555 /* mangle first byte to reflect length in bytes, not digits */
Harald Welte3cfdb222009-06-12 02:42:11 +0800556 address_lv[0] = da_len_bytes - 1;
Harald Welte7e310b12009-03-30 20:56:32 +0000557 /* convert to real number */
Harald Welte55c8f352010-03-07 23:40:35 +0100558 gsm48_decode_bcd_number(gsms->dest_addr, sizeof(gsms->dest_addr), address_lv, 1);
Harald Welte7e310b12009-03-30 20:56:32 +0000559 smsp += da_len_bytes;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000560
Harald Welte76042182009-08-08 16:03:15 +0200561 gsms->protocol_id = *smsp++;
562 gsms->data_coding_scheme = *smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000563
Harald Welte76042182009-08-08 16:03:15 +0200564 sms_alphabet = gsm338_get_sms_alphabet(gsms->data_coding_scheme);
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800565 if (sms_alphabet == 0xffffffff) {
566 sms_free(gsms);
Harald Weltee98c6352010-06-09 15:29:13 +0200567 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800568 }
Harald Welte7e310b12009-03-30 20:56:32 +0000569
Harald Welte76042182009-08-08 16:03:15 +0200570 switch (sms_vpf) {
Harald Welte7e310b12009-03-30 20:56:32 +0000571 case GSM340_TP_VPF_RELATIVE:
Harald Welte76042182009-08-08 16:03:15 +0200572 sms_vp = smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000573 break;
574 case GSM340_TP_VPF_ABSOLUTE:
575 case GSM340_TP_VPF_ENHANCED:
Harald Welte76042182009-08-08 16:03:15 +0200576 sms_vp = smsp;
Steffen Neubauerf3262672009-11-26 12:28:41 +0100577 /* the additional functionality indicator... */
Steffen Neubauerac0c13c2009-12-05 12:44:41 +0100578 if (sms_vpf == GSM340_TP_VPF_ENHANCED && *smsp & (1<<7))
579 smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000580 smsp += 7;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000581 break;
Daniel Willmann58c83d82009-08-13 03:40:49 +0200582 case GSM340_TP_VPF_NONE:
583 sms_vp = 0;
584 break;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000585 default:
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200586 LOGP(DSMS, LOGL_NOTICE,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100587 "SMS Validity period not implemented: 0x%02x\n", sms_vpf);
Harald Welte76042182009-08-08 16:03:15 +0200588 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000589 }
Harald Welte76042182009-08-08 16:03:15 +0200590 gsms->user_data_len = *smsp++;
591 if (gsms->user_data_len) {
592 memcpy(gsms->user_data, smsp, gsms->user_data_len);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000593
Harald Welte76042182009-08-08 16:03:15 +0200594 switch (sms_alphabet) {
Harald Welte7e310b12009-03-30 20:56:32 +0000595 case DCS_7BIT_DEFAULT:
Harald Welte76042182009-08-08 16:03:15 +0200596 gsm_7bit_decode(gsms->text, smsp, gsms->user_data_len);
Harald Welte7e310b12009-03-30 20:56:32 +0000597 break;
598 case DCS_8BIT_DATA:
599 case DCS_UCS2:
600 case DCS_NONE:
Harald Welte7e310b12009-03-30 20:56:32 +0000601 break;
602 }
603 }
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000604
Holger Hans Peter Freyther4f834092010-06-16 14:16:47 +0800605 gsms->sender = subscr_get(conn->subscr);
Harald Welted0cf7ba2009-12-24 15:08:18 +0100606
607 LOGP(DSMS, LOGL_INFO, "RX SMS: Sender: %s, MTI: 0x%02x, VPF: 0x%02x, "
608 "MR: 0x%02x PID: 0x%02x, DCS: 0x%02x, DA: %s, "
609 "UserDataLength: 0x%02x, UserData: \"%s\"\n",
610 subscr_name(gsms->sender), sms_mti, sms_vpf, gsms->msg_ref,
611 gsms->protocol_id, gsms->data_coding_scheme, gsms->dest_addr,
612 gsms->user_data_len,
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200613 sms_alphabet == DCS_7BIT_DEFAULT ? gsms->text :
Harald Welte76042182009-08-08 16:03:15 +0200614 hexdump(gsms->user_data, gsms->user_data_len));
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000615
Harald Welte76042182009-08-08 16:03:15 +0200616 gsms->validity_minutes = gsm340_validity_period(sms_vpf, sms_vp);
Harald Welteb9c758b2009-07-05 14:02:46 +0200617
Harald Welte76042182009-08-08 16:03:15 +0200618 dispatch_signal(SS_SMS, 0, gsms);
Harald Welteb9c758b2009-07-05 14:02:46 +0200619
Harald Welte7e310b12009-03-30 20:56:32 +0000620 /* determine gsms->receiver based on dialled number */
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100621 gsms->receiver = subscr_get_by_extension(conn->bts->network, gsms->dest_addr);
Harald Welte7e310b12009-03-30 20:56:32 +0000622 if (!gsms->receiver) {
623 rc = 1; /* cause 1: unknown subscriber */
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100624 counter_inc(conn->bts->network->stats.sms.no_receiver);
Harald Welte7e310b12009-03-30 20:56:32 +0000625 goto out;
626 }
627
Harald Welte76042182009-08-08 16:03:15 +0200628 switch (sms_mti) {
Harald Welte7e310b12009-03-30 20:56:32 +0000629 case GSM340_SMS_SUBMIT_MS2SC:
630 /* MS is submitting a SMS */
Harald Welte76042182009-08-08 16:03:15 +0200631 rc = gsm340_rx_sms_submit(msg, gsms);
Harald Welte7e310b12009-03-30 20:56:32 +0000632 break;
633 case GSM340_SMS_COMMAND_MS2SC:
634 case GSM340_SMS_DELIVER_REP_MS2SC:
Harald Welted0cf7ba2009-12-24 15:08:18 +0100635 LOGP(DSMS, LOGL_NOTICE, "Unimplemented 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 default:
Harald Welted0cf7ba2009-12-24 15:08:18 +0100639 LOGP(DSMS, LOGL_NOTICE, "Undefined MTI 0x%02x\n", sms_mti);
Harald Welteb9c758b2009-07-05 14:02:46 +0200640 rc = GSM411_RP_CAUSE_IE_NOTEXIST;
Harald Welte7e310b12009-03-30 20:56:32 +0000641 break;
642 }
643
Harald Welteb9c758b2009-07-05 14:02:46 +0200644 if (!rc && !gsms->receiver)
645 rc = GSM411_RP_CAUSE_MO_NUM_UNASSIGNED;
646
Harald Welte7e310b12009-03-30 20:56:32 +0000647out:
Harald Welte76042182009-08-08 16:03:15 +0200648 sms_free(gsms);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000649
Harald Welte7e310b12009-03-30 20:56:32 +0000650 return rc;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000651}
652
Harald Weltef3efc592009-07-27 20:11:35 +0200653static int gsm411_send_rp_ack(struct gsm_trans *trans, u_int8_t msg_ref)
Daniel Willmann471712b2008-12-29 01:54:02 +0000654{
655 struct msgb *msg = gsm411_msgb_alloc();
Daniel Willmann471712b2008-12-29 01:54:02 +0000656
Daniel Willmann471712b2008-12-29 01:54:02 +0000657 DEBUGP(DSMS, "TX: SMS RP ACK\n");
658
Harald Weltef3efc592009-07-27 20:11:35 +0200659 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ACK_MT, msg_ref);
Daniel Willmann471712b2008-12-29 01:54:02 +0000660}
661
Harald Weltef3efc592009-07-27 20:11:35 +0200662static int gsm411_send_rp_error(struct gsm_trans *trans,
663 u_int8_t msg_ref, u_int8_t cause)
Daniel Willmann471712b2008-12-29 01:54:02 +0000664{
665 struct msgb *msg = gsm411_msgb_alloc();
Daniel Willmann471712b2008-12-29 01:54:02 +0000666
Harald Welte7e310b12009-03-30 20:56:32 +0000667 msgb_tv_put(msg, 1, cause);
Daniel Willmann471712b2008-12-29 01:54:02 +0000668
Harald Welted0cf7ba2009-12-24 15:08:18 +0100669 LOGP(DSMS, LOGL_NOTICE, "TX: SMS RP ERROR, cause %d (%s)\n", cause,
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200670 get_value_string(rp_cause_strs, cause));
Daniel Willmann471712b2008-12-29 01:54:02 +0000671
Harald Weltef3efc592009-07-27 20:11:35 +0200672 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ERROR_MT, msg_ref);
Daniel Willmann471712b2008-12-29 01:54:02 +0000673}
674
Harald Welte7e310b12009-03-30 20:56:32 +0000675/* Receive a 04.11 TPDU inside RP-DATA / user data */
Harald Weltef3efc592009-07-27 20:11:35 +0200676static int gsm411_rx_rp_ud(struct msgb *msg, struct gsm_trans *trans,
677 struct gsm411_rp_hdr *rph,
Harald Welte7e310b12009-03-30 20:56:32 +0000678 u_int8_t src_len, u_int8_t *src,
679 u_int8_t dst_len, u_int8_t *dst,
680 u_int8_t tpdu_len, u_int8_t *tpdu)
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000681{
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000682 int rc = 0;
683
Harald Welte7e310b12009-03-30 20:56:32 +0000684 if (src_len && src)
Harald Welted0cf7ba2009-12-24 15:08:18 +0100685 LOGP(DSMS, LOGL_ERROR, "RP-DATA (MO) with SRC ?!?\n");
Harald Welte7e310b12009-03-30 20:56:32 +0000686
687 if (!dst_len || !dst || !tpdu_len || !tpdu) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100688 LOGP(DSMS, LOGL_ERROR,
689 "RP-DATA (MO) without DST or TPDU ?!?\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200690 gsm411_send_rp_error(trans, rph->msg_ref,
Harald Welteb9c758b2009-07-05 14:02:46 +0200691 GSM411_RP_CAUSE_INV_MAND_INF);
Harald Welte7e310b12009-03-30 20:56:32 +0000692 return -EIO;
693 }
Harald Weltee9dd9b02010-04-30 14:27:05 +0200694 msg->l4h = tpdu;
Harald Welte7e310b12009-03-30 20:56:32 +0000695
696 DEBUGP(DSMS, "DST(%u,%s)\n", dst_len, hexdump(dst, dst_len));
Harald Welte7e310b12009-03-30 20:56:32 +0000697
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100698 rc = gsm340_rx_tpdu(trans->conn, msg);
Harald Welte7e310b12009-03-30 20:56:32 +0000699 if (rc == 0)
Harald Weltef3efc592009-07-27 20:11:35 +0200700 return gsm411_send_rp_ack(trans, rph->msg_ref);
Harald Welte7e310b12009-03-30 20:56:32 +0000701 else if (rc > 0)
Harald Weltef3efc592009-07-27 20:11:35 +0200702 return gsm411_send_rp_error(trans, rph->msg_ref, rc);
Harald Welte7e310b12009-03-30 20:56:32 +0000703 else
704 return rc;
705}
706
707/* Receive a 04.11 RP-DATA message in accordance with Section 7.3.1.2 */
Harald Weltef3efc592009-07-27 20:11:35 +0200708static int gsm411_rx_rp_data(struct msgb *msg, struct gsm_trans *trans,
709 struct gsm411_rp_hdr *rph)
Harald Welte7e310b12009-03-30 20:56:32 +0000710{
711 u_int8_t src_len, dst_len, rpud_len;
712 u_int8_t *src = NULL, *dst = NULL , *rp_ud = NULL;
713
714 /* in the MO case, this should always be zero length */
715 src_len = rph->data[0];
716 if (src_len)
717 src = &rph->data[1];
718
719 dst_len = rph->data[1+src_len];
720 if (dst_len)
721 dst = &rph->data[1+src_len+1];
722
723 rpud_len = rph->data[1+src_len+1+dst_len];
724 if (rpud_len)
725 rp_ud = &rph->data[1+src_len+1+dst_len+1];
726
Harald Welteb9c758b2009-07-05 14:02:46 +0200727 DEBUGP(DSMS, "RX_RP-DATA: src_len=%u, dst_len=%u ud_len=%u\n",
728 src_len, dst_len, rpud_len);
Harald Weltef3efc592009-07-27 20:11:35 +0200729 return gsm411_rx_rp_ud(msg, trans, rph, src_len, src, dst_len, dst,
Harald Welte7e310b12009-03-30 20:56:32 +0000730 rpud_len, rp_ud);
731}
732
Harald Weltecb8f4432009-08-09 14:59:02 +0200733/* Receive a 04.11 RP-ACK message (response to RP-DATA from us) */
Harald Weltef3efc592009-07-27 20:11:35 +0200734static int gsm411_rx_rp_ack(struct msgb *msg, struct gsm_trans *trans,
735 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200736{
Harald Welte76042182009-08-08 16:03:15 +0200737 struct gsm_sms *sms = trans->sms.sms;
738
Harald Welteb9c758b2009-07-05 14:02:46 +0200739 /* Acnkowledgement to MT RP_DATA, i.e. the MS confirms it
740 * successfully received a SMS. We can now safely mark it as
741 * transmitted */
742
Harald Weltecb8f4432009-08-09 14:59:02 +0200743 if (!trans->sms.is_mt) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100744 LOGP(DSMS, LOGL_ERROR, "RX RP-ACK on a MO transfer ?\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200745 return gsm411_send_rp_error(trans, rph->msg_ref,
746 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
747 }
Harald Welte3e0f6172009-07-09 23:52:59 +0200748
Harald Welte76042182009-08-08 16:03:15 +0200749 if (!sms) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100750 LOGP(DSMS, LOGL_ERROR, "RX RP-ACK but no sms in transaction?!?\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200751 return gsm411_send_rp_error(trans, rph->msg_ref,
752 GSM411_RP_CAUSE_PROTOCOL_ERR);
Harald Welte76042182009-08-08 16:03:15 +0200753 }
754
755 /* mark this SMS as sent in database */
756 db_sms_mark_sent(sms);
757
758 dispatch_signal(SS_SMS, S_SMS_DELIVERED, sms);
759
760 sms_free(sms);
761 trans->sms.sms = NULL;
762
Harald Weltecf6a3812009-08-09 19:07:41 +0200763 /* check for more messages for this subscriber */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100764 sms = db_sms_get_unsent_for_subscr(trans->subscr);
Harald Weltecf6a3812009-08-09 19:07:41 +0200765 if (sms)
Holger Hans Peter Freyther0fbb3ec2010-06-16 14:19:31 +0800766 gsm411_send_sms(trans->conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800767 else
768 gsm411_release_conn(trans->conn);
Sylvain Munautd6c35f62009-12-24 13:33:51 +0100769
770 /* free the transaction here */
771 trans_free(trans);
Harald Welte76042182009-08-08 16:03:15 +0200772 return 0;
Harald Welteb9c758b2009-07-05 14:02:46 +0200773}
774
Harald Weltef3efc592009-07-27 20:11:35 +0200775static int gsm411_rx_rp_error(struct msgb *msg, struct gsm_trans *trans,
776 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200777{
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100778 struct gsm_network *net = trans->conn->bts->network;
Harald Welte76042182009-08-08 16:03:15 +0200779 struct gsm_sms *sms = trans->sms.sms;
Harald Weltef3efc592009-07-27 20:11:35 +0200780 u_int8_t cause_len = rph->data[0];
781 u_int8_t cause = rph->data[1];
782
Harald Welteb9c758b2009-07-05 14:02:46 +0200783 /* Error in response to MT RP_DATA, i.e. the MS did not
784 * successfully receive the SMS. We need to investigate
785 * the cause and take action depending on it */
786
Harald Welted0cf7ba2009-12-24 15:08:18 +0100787 LOGP(DSMS, LOGL_NOTICE, "%s: RX SMS RP-ERROR, cause %d:%d (%s)\n",
Holger Hans Peter Freytherd4c16b12010-03-23 07:56:22 +0100788 subscr_name(trans->conn->subscr), cause_len, cause,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100789 get_value_string(rp_cause_strs, cause));
Harald Weltef3efc592009-07-27 20:11:35 +0200790
Harald Weltecb8f4432009-08-09 14:59:02 +0200791 if (!trans->sms.is_mt) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100792 LOGP(DSMS, LOGL_ERROR, "RX RP-ERR on a MO transfer ?\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200793#if 0
Harald Weltecb8f4432009-08-09 14:59:02 +0200794 return gsm411_send_rp_error(trans, rph->msg_ref,
795 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200796#endif
Harald Weltecb8f4432009-08-09 14:59:02 +0200797 }
Harald Welte3e0f6172009-07-09 23:52:59 +0200798
Harald Welte76042182009-08-08 16:03:15 +0200799 if (!sms) {
Harald Welted0cf7ba2009-12-24 15:08:18 +0100800 LOGP(DSMS, LOGL_ERROR,
801 "RX RP-ERR, but no sms in transaction?!?\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200802 return -EINVAL;
803#if 0
Harald Weltecb8f4432009-08-09 14:59:02 +0200804 return gsm411_send_rp_error(trans, rph->msg_ref,
805 GSM411_RP_CAUSE_PROTOCOL_ERR);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200806#endif
Harald Weltecb8f4432009-08-09 14:59:02 +0200807 }
808
809 if (cause == GSM411_RP_CAUSE_MT_MEM_EXCEEDED) {
810 /* MS has not enough memory to store the message. We need
811 * to store this in our database and wati for a SMMA message */
812 /* FIXME */
813 dispatch_signal(SS_SMS, S_SMS_MEM_EXCEEDED, trans->subscr);
Harald Welteffa55a42009-12-22 19:07:32 +0100814 counter_inc(net->stats.sms.rp_err_mem);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100815 } else
Harald Welteffa55a42009-12-22 19:07:32 +0100816 counter_inc(net->stats.sms.rp_err_other);
Harald Welte76042182009-08-08 16:03:15 +0200817
818 sms_free(sms);
819 trans->sms.sms = NULL;
820
Harald Weltef3efc592009-07-27 20:11:35 +0200821 return 0;
Harald Welteb9c758b2009-07-05 14:02:46 +0200822}
823
Harald Weltef3efc592009-07-27 20:11:35 +0200824static int gsm411_rx_rp_smma(struct msgb *msg, struct gsm_trans *trans,
825 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200826{
Harald Weltecf6a3812009-08-09 19:07:41 +0200827 struct gsm_sms *sms;
Harald Weltef3efc592009-07-27 20:11:35 +0200828 int rc;
829
Harald Weltecf6a3812009-08-09 19:07:41 +0200830 rc = gsm411_send_rp_ack(trans, rph->msg_ref);
831 trans->sms.rp_state = GSM411_RPS_IDLE;
832
Harald Welteb9c758b2009-07-05 14:02:46 +0200833 /* MS tells us that it has memory for more SMS, we need
834 * to check if we have any pending messages for it and then
835 * transfer those */
Harald Welte76042182009-08-08 16:03:15 +0200836 dispatch_signal(SS_SMS, S_SMS_SMMA, trans->subscr);
Harald Weltef3efc592009-07-27 20:11:35 +0200837
Harald Weltecf6a3812009-08-09 19:07:41 +0200838 /* check for more messages for this subscriber */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100839 sms = db_sms_get_unsent_for_subscr(trans->subscr);
Harald Weltecf6a3812009-08-09 19:07:41 +0200840 if (sms)
Holger Hans Peter Freyther0fbb3ec2010-06-16 14:19:31 +0800841 gsm411_send_sms(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 */
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800917int gsm0411_rcv_sms(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800918 struct msgb *msg)
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000919{
920 struct gsm48_hdr *gh = msgb_l3(msg);
921 u_int8_t msg_type = gh->msg_type;
Harald Weltef3efc592009-07-27 20:11:35 +0200922 u_int8_t transaction_id = ((gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Weltef3efc592009-07-27 20:11:35 +0200923 struct gsm_trans *trans;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000924 int rc = 0;
925
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800926 if (!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 Freyther57c4dec2010-06-16 14:24:55 +0800931 trans = trans_find_by_id(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 Freyther57c4dec2010-06-16 14:24:55 +0800935 trans = trans_alloc(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;
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800945 trans->sms.link_id = UM_SAPI_SMS;
Harald Weltef3efc592009-07-27 20:11:35 +0200946
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800947 trans->conn = conn;
Harald Weltef3efc592009-07-27 20:11:35 +0200948 }
949
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000950 switch(msg_type) {
951 case GSM411_MT_CP_DATA:
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200952 DEBUGPC(DSMS, "RX SMS CP-DATA\n");
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100953
954 /* 5.4: For MO, if a CP-DATA is received for a new
955 * transaction, equals reception of an implicit
956 * last CP-ACK for previous transaction */
957 if (trans->sms.cp_state == GSM411_CPS_IDLE) {
958 int i;
959 struct gsm_trans *ptrans;
960
961 /* Scan through all remote initiated transactions */
962 for (i=8; i<15; i++) {
963 if (i == transaction_id)
964 continue;
965
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800966 ptrans = trans_find_by_id(conn->subscr,
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100967 GSM48_PDISC_SMS, i);
968 if (!ptrans)
969 continue;
970
971 DEBUGP(DSMS, "Implicit CP-ACK for trans_id=%x\n", i);
972
973 /* Finish it for good */
974 bsc_del_timer(&ptrans->sms.cp_timer);
975 ptrans->sms.cp_state = GSM411_CPS_IDLE;
976 trans_free(ptrans);
977 }
978 }
979
Harald Weltecb8f4432009-08-09 14:59:02 +0200980 /* 5.2.3.1.3: MO state exists when SMC has received
981 * CP-DATA, including sending of the assoc. CP-ACK */
982 /* 5.2.3.2.4: MT state exists when SMC has received
983 * CP-DATA, including sending of the assoc. CP-ACK */
984 trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED;
Harald Weltef3efc592009-07-27 20:11:35 +0200985
Harald Welte (local)daef6062009-08-14 11:41:12 +0200986 /* SMC instance acknowledges the CP-DATA frame */
987 gsm411_tx_cp_ack(trans);
988
Harald Weltef3efc592009-07-27 20:11:35 +0200989 rc = gsm411_rx_cp_data(msg, gh, trans);
Harald Welte (local)daef6062009-08-14 11:41:12 +0200990#if 0
Harald Weltef3efc592009-07-27 20:11:35 +0200991 /* Send CP-ACK or CP-ERORR in response */
992 if (rc < 0) {
993 rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_NET_FAIL);
994 } else
995 rc = gsm411_tx_cp_ack(trans);
Harald Welte (local)daef6062009-08-14 11:41:12 +0200996#endif
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000997 break;
998 case GSM411_MT_CP_ACK:
Harald Weltef3efc592009-07-27 20:11:35 +0200999 /* previous CP-DATA in this transaction was confirmed */
Harald Welte (local)c89a5112009-08-14 10:42:43 +02001000 DEBUGPC(DSMS, "RX SMS CP-ACK\n");
Harald Weltecb8f4432009-08-09 14:59:02 +02001001 /* 5.2.3.1.3: MO state exists when SMC has received CP-ACK */
1002 /* 5.2.3.2.4: MT state exists when SMC has received CP-ACK */
1003 trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED;
Harald Welte41985612009-08-10 00:24:32 +02001004 /* Stop TC1* after CP-ACK has been received */
1005 bsc_del_timer(&trans->sms.cp_timer);
Harald Weltecb8f4432009-08-09 14:59:02 +02001006
Harald Weltef3efc592009-07-27 20:11:35 +02001007 if (!trans->sms.is_mt) {
Holger Hans Peter Freyther0afa0442010-06-10 18:08:12 +08001008 /* FIXME: we have sent one CP-DATA, which was now
Harald Weltef3efc592009-07-27 20:11:35 +02001009 * acknowledged. Check if we want to transfer more,
1010 * i.e. multi-part message */
1011 trans->sms.cp_state = GSM411_CPS_IDLE;
1012 trans_free(trans);
1013 }
Daniel Willmannbb16e8e2008-12-29 03:53:50 +00001014 break;
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001015 case GSM411_MT_CP_ERROR:
Harald Welte (local)c89a5112009-08-14 10:42:43 +02001016 DEBUGPC(DSMS, "RX SMS CP-ERROR, cause %d (%s)\n", gh->data[0],
1017 get_value_string(cp_cause_strs, gh->data[0]));
Harald Welte41985612009-08-10 00:24:32 +02001018 bsc_del_timer(&trans->sms.cp_timer);
Harald Weltef3efc592009-07-27 20:11:35 +02001019 trans->sms.cp_state = GSM411_CPS_IDLE;
1020 trans_free(trans);
Daniel Willmannbb16e8e2008-12-29 03:53:50 +00001021 break;
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001022 default:
Harald Welte (local)c89a5112009-08-14 10:42:43 +02001023 DEBUGPC(DSMS, "RX Unimplemented CP msg_type: 0x%02x\n", msg_type);
Harald Weltef3efc592009-07-27 20:11:35 +02001024 rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_MSGTYPE_NOTEXIST);
Harald Weltecb8f4432009-08-09 14:59:02 +02001025 trans->sms.cp_state = GSM411_CPS_IDLE;
Harald Weltef3efc592009-07-27 20:11:35 +02001026 trans_free(trans);
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001027 break;
1028 }
1029
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001030 return rc;
1031}
1032
Harald Weltecf6a3812009-08-09 19:07:41 +02001033/* Take a SMS in gsm_sms structure and send it through an already
1034 * existing lchan. We also assume that the caller ensured this lchan already
1035 * has a SAPI3 RLL connection! */
Holger Hans Peter Freyther0fbb3ec2010-06-16 14:19:31 +08001036static int gsm411_send_sms(struct gsm_subscriber_connection *conn, struct gsm_sms *sms)
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001037{
1038 struct msgb *msg = gsm411_msgb_alloc();
Harald Weltef3efc592009-07-27 20:11:35 +02001039 struct gsm_trans *trans;
Harald Welte76042182009-08-08 16:03:15 +02001040 u_int8_t *data, *rp_ud_len;
Harald Welte87f5d632009-07-04 17:39:00 +02001041 u_int8_t msg_ref = 42;
Sylvain Munautc4052cf2009-12-24 13:27:36 +01001042 int transaction_id;
Harald Welte76042182009-08-08 16:03:15 +02001043 int rc;
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001044
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +01001045 transaction_id = trans_assign_trans_id(conn->subscr, GSM48_PDISC_SMS, 0);
Harald Welte49839212009-12-22 13:45:58 +01001046 if (transaction_id == -1) {
Harald Welted0cf7ba2009-12-24 15:08:18 +01001047 LOGP(DSMS, LOGL_ERROR, "No available transaction ids\n");
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +08001048 sms_free(sms);
Harald Welte49839212009-12-22 13:45:58 +01001049 return -EBUSY;
1050 }
Harald Weltecf6a3812009-08-09 19:07:41 +02001051
Harald Welte76042182009-08-08 16:03:15 +02001052 DEBUGP(DSMS, "send_sms_lchan()\n");
Harald Weltef3efc592009-07-27 20:11:35 +02001053
Harald Welte76042182009-08-08 16:03:15 +02001054 /* FIXME: allocate transaction with message reference */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +01001055 trans = trans_alloc(conn->subscr, GSM48_PDISC_SMS,
Harald Welte76042182009-08-08 16:03:15 +02001056 transaction_id, new_callref++);
1057 if (!trans) {
Harald Welted0cf7ba2009-12-24 15:08:18 +01001058 LOGP(DSMS, LOGL_ERROR, "No memory for trans\n");
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +08001059 sms_free(sms);
Harald Welte76042182009-08-08 16:03:15 +02001060 /* FIXME: send some error message */
1061 return -ENOMEM;
1062 }
1063 trans->sms.cp_state = GSM411_CPS_IDLE;
1064 trans->sms.rp_state = GSM411_RPS_IDLE;
1065 trans->sms.is_mt = 1;
1066 trans->sms.sms = sms;
Harald Welte (local)daef6062009-08-14 11:41:12 +02001067 trans->sms.link_id = UM_SAPI_SMS; /* FIXME: main or SACCH ? */
Harald Welte76042182009-08-08 16:03:15 +02001068
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +01001069 trans->conn = conn;
Harald Welte76042182009-08-08 16:03:15 +02001070
1071 /* Hardcode SMSC Originating Address for now */
Daniel Willmanna3e29842008-12-29 16:03:54 +00001072 data = (u_int8_t *)msgb_put(msg, 8);
Harald Welte87f5d632009-07-04 17:39:00 +02001073 data[0] = 0x07; /* originator length == 7 */
Harald Welteb9c758b2009-07-05 14:02:46 +02001074 data[1] = 0x91; /* type of number: international, ISDN */
1075 data[2] = 0x44; /* 447785016005 */
Daniel Willmanna3e29842008-12-29 16:03:54 +00001076 data[3] = 0x77;
1077 data[4] = 0x58;
1078 data[5] = 0x10;
1079 data[6] = 0x06;
1080 data[7] = 0x50;
Harald Welte87f5d632009-07-04 17:39:00 +02001081
1082 /* Hardcoded Destination Address */
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001083 data = (u_int8_t *)msgb_put(msg, 1);
Harald Welte87f5d632009-07-04 17:39:00 +02001084 data[0] = 0; /* destination length == 0 */
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001085
Harald Welte76042182009-08-08 16:03:15 +02001086 /* obtain a pointer for the rp_ud_len, so we can fill it later */
1087 rp_ud_len = (u_int8_t *)msgb_put(msg, 1);
Daniel Willmann4a1e8792008-12-29 06:23:56 +00001088
Harald Welte76042182009-08-08 16:03:15 +02001089 /* generate the 03.40 TPDU */
1090 rc = gsm340_gen_tpdu(msg, sms);
1091 if (rc < 0) {
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +08001092 trans_free(trans);
1093 sms_free(sms);
Harald Welte76042182009-08-08 16:03:15 +02001094 msgb_free(msg);
1095 return rc;
1096 }
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001097
Harald Welte76042182009-08-08 16:03:15 +02001098 *rp_ud_len = rc;
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001099
Harald Welte76042182009-08-08 16:03:15 +02001100 DEBUGP(DSMS, "TX: SMS DELIVER\n");
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001101
Holger Hans Peter Freytherd4c16b12010-03-23 07:56:22 +01001102 counter_inc(conn->bts->network->stats.sms.delivered);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001103
Harald Weltef3efc592009-07-27 20:11:35 +02001104 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_DATA_MT, msg_ref);
1105 /* FIXME: enter 'wait for RP-ACK' state, start TR1N */
Daniel Willmann6fe997e2008-12-29 04:20:41 +00001106}
Harald Weltef3efc592009-07-27 20:11:35 +02001107
Harald Weltecf6a3812009-08-09 19:07:41 +02001108/* paging callback. Here we get called if paging a subscriber has
1109 * succeeded or failed. */
Harald Welte76042182009-08-08 16:03:15 +02001110static int paging_cb_send_sms(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001111 struct msgb *msg, void *_conn, void *_sms)
Harald Weltef3efc592009-07-27 20:11:35 +02001112{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001113 struct gsm_subscriber_connection *conn = _conn;
Harald Welte76042182009-08-08 16:03:15 +02001114 struct gsm_sms *sms = _sms;
Holger Hans Peter Freyther4cd51582010-06-14 16:03:03 +08001115 int rc = 0;
Harald Weltef3efc592009-07-27 20:11:35 +02001116
Harald Welte76042182009-08-08 16:03:15 +02001117 DEBUGP(DSMS, "paging_cb_send_sms(hooknum=%u, event=%u, msg=%p,"
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001118 "conn=%p, sms=%p)\n", hooknum, event, msg, conn, sms);
Harald Welte76042182009-08-08 16:03:15 +02001119
1120 if (hooknum != GSM_HOOK_RR_PAGING)
1121 return -EINVAL;
1122
1123 switch (event) {
1124 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001125 gsm411_send_sms(conn, sms);
Harald Welte76042182009-08-08 16:03:15 +02001126 break;
1127 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherca5702a2010-06-10 18:20:54 +08001128 case GSM_PAGING_OOM:
Harald Welte76042182009-08-08 16:03:15 +02001129 sms_free(sms);
1130 rc = -ETIMEDOUT;
1131 break;
Harald Welte76042182009-08-08 16:03:15 +02001132 }
1133
1134 return rc;
1135}
1136
Harald Weltecf6a3812009-08-09 19:07:41 +02001137/* high-level function to send a SMS to a given subscriber. The function
1138 * will take care of paging the subscriber, establishing the RLL SAPI3
1139 * connection, etc. */
Harald Welte76042182009-08-08 16:03:15 +02001140int gsm411_send_sms_subscr(struct gsm_subscriber *subscr,
1141 struct gsm_sms *sms)
1142{
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001143 struct gsm_subscriber_connection *conn;
Harald Weltecf6a3812009-08-09 19:07:41 +02001144
Harald Welte76042182009-08-08 16:03:15 +02001145 /* check if we already have an open lchan to the subscriber.
1146 * if yes, send the SMS this way */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001147 conn = connection_for_subscr(subscr);
1148 if (conn) {
Holger Hans Peter Freyther0fbb3ec2010-06-16 14:19:31 +08001149 return gsm411_send_sms(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 Freyther0fbb3ec2010-06-16 14:19:31 +08001175 gsm411_send_sms(conn, sms);
Harald Weltecf6a3812009-08-09 19:07:41 +02001176 break;
1177 default:
1178 break;
1179 }
1180 return 0;
1181}
1182
Harald Welte (local)86b17172009-08-14 14:52:17 +02001183void _gsm411_sms_trans_free(struct gsm_trans *trans)
1184{
Holger Hans Peter Freythercea722b2010-06-15 12:29:37 +08001185 if (trans->sms.sms) {
1186 LOGP(DSMS, LOGL_ERROR, "Transaction contains SMS.\n");
1187 sms_free(trans->sms.sms);
1188 trans->sms.sms = NULL;
1189 }
1190
Harald Welte (local)86b17172009-08-14 14:52:17 +02001191 bsc_del_timer(&trans->sms.cp_timer);
1192}
1193
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001194void gsm411_sapi_n_reject(struct gsm_subscriber_connection *conn)
1195{
1196 struct gsm_trans *trans, *tmp;
1197
1198 llist_for_each_entry_safe(trans, tmp, &conn->bts->network->trans_list, entry)
1199 if (trans->conn == conn) {
1200 struct gsm_sms *sms = trans->sms.sms;
1201 if (!sms) {
1202 LOGP(DSMS, LOGL_ERROR, "SAPI Reject but no SMS.\n");
1203 continue;
1204 }
1205
1206 sms_free(sms);
1207 trans->sms.sms = NULL;
1208 trans_free(trans);
1209 }
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +08001210
1211 gsm411_release_conn(conn);
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001212}
1213
Harald Welte7bfc2672009-07-28 00:41:45 +02001214static __attribute__((constructor)) void on_dso_load_sms(void)
1215{
Harald Weltecf6a3812009-08-09 19:07:41 +02001216 register_signal_handler(SS_SUBSCR, subscr_sig_cb, NULL);
Harald Welte7bfc2672009-07-28 00:41:45 +02001217}