blob: 5beae53012611476106b9eca2e45fc0882277aa1 [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 Freyther75172122012-11-10 18:11:22 +01007 * (C) 2010-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freyther85531cc2010-10-06 20:37:09 +08008 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010013 * it under the terms of the GNU Affero General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
Daniel Willmann8b3390e2008-12-28 00:31:09 +000015 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010020 * GNU Affero General Public License for more details.
Daniel Willmann8b3390e2008-12-28 00:31:09 +000021 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010022 * You should have received a copy of the GNU Affero General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Daniel Willmann8b3390e2008-12-28 00:31:09 +000024 *
25 */
26
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <errno.h>
Harald Weltef3efc592009-07-27 20:11:35 +020032#include <time.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000033#include <netinet/in.h>
34
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010037#include <osmocom/core/msgb.h>
Holger Hans Peter Freyther75172122012-11-10 18:11:22 +010038#include <osmocom/core/talloc.h>
39
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010040#include <osmocom/gsm/tlv.h>
Holger Hans Peter Freyther75172122012-11-10 18:11:22 +010041#include <osmocom/gsm/gsm_utils.h>
Holger Hans Peter Freyther366c3312012-11-10 18:24:54 +010042#include <osmocom/gsm/gsm0411_utils.h>
Holger Hans Peter Freyther75172122012-11-10 18:11:22 +010043
Daniel Willmann8b3390e2008-12-28 00:31:09 +000044#include <openbsc/debug.h>
Daniel Willmann471712b2008-12-29 01:54:02 +000045#include <openbsc/gsm_data.h>
Nico Goldef11af072010-09-15 19:49:55 +020046#include <openbsc/db.h>
Daniel Willmann471712b2008-12-29 01:54:02 +000047#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000048#include <openbsc/gsm_04_11.h>
49#include <openbsc/gsm_04_08.h>
50#include <openbsc/abis_rsl.h>
Holger Freyther9b177762009-02-16 19:07:18 +000051#include <openbsc/signal.h>
Harald Welte7e310b12009-03-30 20:56:32 +000052#include <openbsc/db.h>
Harald Weltef3efc592009-07-27 20:11:35 +020053#include <openbsc/transaction.h>
Harald Welte76042182009-08-08 16:03:15 +020054#include <openbsc/paging.h>
Harald Weltecb8f4432009-08-09 14:59:02 +020055#include <openbsc/bsc_rll.h>
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020056#include <openbsc/chan_alloc.h>
Holger Hans Peter Freythercbac76e2010-05-23 21:05:18 +080057#include <openbsc/bsc_api.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000058
Harald Weltee07b6a72012-11-23 19:02:37 +010059#ifdef BUILD_SMPP
60#include "smpp_smsc.h"
61extern int smpp_try_deliver(struct gsm_sms *sms);
62#endif
63
Daniel Willmann471712b2008-12-29 01:54:02 +000064#define GSM411_ALLOC_SIZE 1024
65#define GSM411_ALLOC_HEADROOM 128
66
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020067void *tall_gsms_ctx;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020068static uint32_t new_callref = 0x40000001;
Harald Weltef3efc592009-07-27 20:11:35 +020069
Harald Welte (local)c89a5112009-08-14 10:42:43 +020070static const struct value_string cp_cause_strs[] = {
71 { GSM411_CP_CAUSE_NET_FAIL, "Network Failure" },
72 { GSM411_CP_CAUSE_CONGESTION, "Congestion" },
73 { GSM411_CP_CAUSE_INV_TRANS_ID, "Invalid Transaction ID" },
74 { GSM411_CP_CAUSE_SEMANT_INC_MSG, "Semantically Incorrect Message" },
75 { GSM411_CP_CAUSE_INV_MAND_INF, "Invalid Mandatory Information" },
76 { GSM411_CP_CAUSE_MSGTYPE_NOTEXIST, "Message Type doesn't exist" },
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +020077 { GSM411_CP_CAUSE_MSG_INCOMP_STATE,
Harald Welte (local)c89a5112009-08-14 10:42:43 +020078 "Message incompatible with protocol state" },
79 { GSM411_CP_CAUSE_IE_NOTEXIST, "IE does not exist" },
80 { GSM411_CP_CAUSE_PROTOCOL_ERR, "Protocol Error" },
81 { 0, 0 }
82};
83
84static const struct value_string rp_cause_strs[] = {
85 { GSM411_RP_CAUSE_MO_NUM_UNASSIGNED, "(MO) Number not assigned" },
86 { GSM411_RP_CAUSE_MO_OP_DET_BARR, "(MO) Operator determined barring" },
87 { GSM411_RP_CAUSE_MO_CALL_BARRED, "(MO) Call barred" },
88 { GSM411_RP_CAUSE_MO_SMS_REJECTED, "(MO) SMS rejected" },
89 { GSM411_RP_CAUSE_MO_DEST_OUT_OF_ORDER, "(MO) Destination out of order" },
90 { GSM411_RP_CAUSE_MO_UNIDENTIFIED_SUBSCR, "(MO) Unidentified subscriber" },
91 { GSM411_RP_CAUSE_MO_FACILITY_REJ, "(MO) Facility reject" },
92 { GSM411_RP_CAUSE_MO_UNKNOWN_SUBSCR, "(MO) Unknown subscriber" },
93 { GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER, "(MO) Network out of order" },
94 { GSM411_RP_CAUSE_MO_TEMP_FAIL, "(MO) Temporary failure" },
95 { GSM411_RP_CAUSE_MO_CONGESTION, "(MO) Congestion" },
96 { GSM411_RP_CAUSE_MO_RES_UNAVAIL, "(MO) Resource unavailable" },
97 { GSM411_RP_CAUSE_MO_REQ_FAC_NOTSUBSCR, "(MO) Requested facility not subscribed" },
98 { GSM411_RP_CAUSE_MO_REQ_FAC_NOTIMPL, "(MO) Requested facility not implemented" },
99 { GSM411_RP_CAUSE_MO_INTERWORKING, "(MO) Interworking" },
100 /* valid only for MT */
101 { GSM411_RP_CAUSE_MT_MEM_EXCEEDED, "(MT) Memory Exceeded" },
102 /* valid for both directions */
103 { GSM411_RP_CAUSE_INV_TRANS_REF, "Invalid Transaction Reference" },
104 { GSM411_RP_CAUSE_SEMANT_INC_MSG, "Semantically Incorrect Message" },
105 { GSM411_RP_CAUSE_INV_MAND_INF, "Invalid Mandatory Information" },
106 { GSM411_RP_CAUSE_MSGTYPE_NOTEXIST, "Message Type non-existant" },
107 { GSM411_RP_CAUSE_MSG_INCOMP_STATE, "Message incompatible with protocol state" },
108 { GSM411_RP_CAUSE_IE_NOTEXIST, "Information Element not existing" },
109 { GSM411_RP_CAUSE_PROTOCOL_ERR, "Protocol Error" },
110 { 0, NULL }
111};
112
Holger Hans Peter Freyther0fbb3ec2010-06-16 14:19:31 +0800113
Harald Welte76042182009-08-08 16:03:15 +0200114struct gsm_sms *sms_alloc(void)
115{
116 return talloc_zero(tall_gsms_ctx, struct gsm_sms);
117}
118
119void sms_free(struct gsm_sms *sms)
120{
121 /* drop references to subscriber structure */
122 if (sms->sender)
123 subscr_put(sms->sender);
124 if (sms->receiver)
125 subscr_put(sms->receiver);
Harald Welte6c7680d2012-11-16 22:15:22 +0100126#ifdef BUILD_SMPP
Harald Weltee94db492012-11-08 20:11:05 +0100127 if (sms->smpp.esme)
128 smpp_esme_put(sms->smpp.esme);
Harald Welte6c7680d2012-11-16 22:15:22 +0100129#endif
Harald Welte76042182009-08-08 16:03:15 +0200130
131 talloc_free(sms);
132}
133
Holger Hans Peter Freythercdfcbee2011-01-10 15:34:09 +0100134struct gsm_sms *sms_from_text(struct gsm_subscriber *receiver, int dcs, const char *text)
Holger Hans Peter Freytherc4644912011-01-10 15:21:34 +0100135{
136 struct gsm_sms *sms = sms_alloc();
137
138 if (!sms)
139 return NULL;
140
141 sms->receiver = subscr_get(receiver);
142 strncpy(sms->text, text, sizeof(sms->text)-1);
143
144 /* FIXME: don't use ID 1 static */
145 sms->sender = subscr_get_by_id(receiver->net, 1);
146 sms->reply_path_req = 0;
147 sms->status_rep_req = 0;
148 sms->ud_hdr_ind = 0;
149 sms->protocol_id = 0; /* implicit */
Holger Hans Peter Freythercdfcbee2011-01-10 15:34:09 +0100150 sms->data_coding_scheme = dcs;
Holger Hans Peter Freytherc4644912011-01-10 15:21:34 +0100151 strncpy(sms->dest_addr, receiver->extension, sizeof(sms->dest_addr)-1);
152 /* Generate user_data */
153 sms->user_data_len = gsm_7bit_encode(sms->user_data, sms->text);
154
155 return sms;
156}
157
158
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100159static void send_signal(int sig_no,
160 struct gsm_trans *trans,
161 struct gsm_sms *sms,
162 int paging_result)
163{
164 struct sms_signal_data sig;
165 sig.trans = trans;
166 sig.sms = sms;
167 sig.paging_result = paging_result;
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200168 osmo_signal_dispatch(SS_SMS, sig_no, &sig);
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100169}
170
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800171/*
172 * This should be called whenever all SMS to a given subscriber
173 * on a given connection has been sent. This will inform the higher
174 * layers that a channel can be given up.
175 */
176static void gsm411_release_conn(struct gsm_subscriber_connection *conn)
177{
Holger Hans Peter Freytherbddd1522010-06-16 14:02:41 +0800178 if (!conn)
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800179 return;
180
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +0100181 subscr_put_channel(conn->subscr);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800182}
183
Holger Freythera553d092009-01-04 20:16:25 +0000184struct msgb *gsm411_msgb_alloc(void)
Daniel Willmann471712b2008-12-29 01:54:02 +0000185{
Harald Welte966636f2009-06-26 19:39:35 +0200186 return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM,
187 "GSM 04.11");
Daniel Willmann471712b2008-12-29 01:54:02 +0000188}
189
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200190static int gsm411_sendmsg(struct gsm_subscriber_connection *conn, struct msgb *msg, uint8_t link_id)
Daniel Willmann471712b2008-12-29 01:54:02 +0000191{
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100192 DEBUGP(DLSMS, "GSM4.11 TX %s\n", osmo_hexdump(msg->data, msg->len));
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100193 msg->l3h = msg->data;
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100194 return gsm0808_submit_dtap(conn, msg, link_id, 1);
Daniel Willmann471712b2008-12-29 01:54:02 +0000195}
196
Harald Welte41985612009-08-10 00:24:32 +0200197/* SMC TC1* is expired */
198static void cp_timer_expired(void *data)
199{
200 struct gsm_trans *trans = data;
201
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100202 DEBUGP(DLSMS, "SMC Timer TC1* is expired, calling trans_free()\n");
Harald Welte41985612009-08-10 00:24:32 +0200203 /* FIXME: we need to re-transmit the last CP-DATA 1..3 times */
204 trans_free(trans);
205}
206
Harald Welte87f5d632009-07-04 17:39:00 +0200207/* Prefix msg with a 04.08/04.11 CP header */
Harald Weltef3efc592009-07-27 20:11:35 +0200208static int gsm411_cp_sendmsg(struct msgb *msg, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200209 uint8_t msg_type)
Harald Welte87f5d632009-07-04 17:39:00 +0200210{
211 struct gsm48_hdr *gh;
212
213 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
214 /* Outgoing needs the highest bit set */
Harald Weltef3efc592009-07-27 20:11:35 +0200215 gh->proto_discr = trans->protocol | (trans->transaction_id<<4);
Harald Welte87f5d632009-07-04 17:39:00 +0200216 gh->msg_type = msg_type;
217
Harald Weltef3efc592009-07-27 20:11:35 +0200218 /* mobile originating */
219 switch (gh->msg_type) {
220 case GSM411_MT_CP_DATA:
221 /* 5.2.3.1.2: enter MO-wait for CP-ack */
Harald Weltecb8f4432009-08-09 14:59:02 +0200222 /* 5.2.3.2.3: enter MT-wait for CP-ACK */
Harald Weltef3efc592009-07-27 20:11:35 +0200223 trans->sms.cp_state = GSM411_CPS_WAIT_CP_ACK;
Harald Welte41985612009-08-10 00:24:32 +0200224 trans->sms.cp_timer.data = trans;
225 trans->sms.cp_timer.cb = cp_timer_expired;
226 /* 5.3.2.1: Set Timer TC1A */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200227 osmo_timer_schedule(&trans->sms.cp_timer, GSM411_TMR_TC1A);
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100228 DEBUGP(DLSMS, "TX: CP-DATA ");
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200229 break;
230 case GSM411_MT_CP_ACK:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100231 DEBUGP(DLSMS, "TX: CP-ACK ");
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200232 break;
233 case GSM411_MT_CP_ERROR:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100234 DEBUGP(DLSMS, "TX: CP-ERROR ");
Harald Weltef3efc592009-07-27 20:11:35 +0200235 break;
236 }
237
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100238 DEBUGPC(DLSMS, "trans=%x\n", trans->transaction_id);
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200239
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100240 return gsm411_sendmsg(trans->conn, msg, trans->sms.link_id);
Harald Welte87f5d632009-07-04 17:39:00 +0200241}
242
243/* Prefix msg with a RP-DATA header and send as CP-DATA */
Harald Weltef3efc592009-07-27 20:11:35 +0200244static int gsm411_rp_sendmsg(struct msgb *msg, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200245 uint8_t rp_msg_type, uint8_t rp_msg_ref)
Harald Welte87f5d632009-07-04 17:39:00 +0200246{
247 struct gsm411_rp_hdr *rp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200248 uint8_t len = msg->len;
Harald Welte87f5d632009-07-04 17:39:00 +0200249
250 /* GSM 04.11 RP-DATA header */
251 rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp));
Harald Welte0d544e72009-08-10 00:22:19 +0200252 rp->len = len + 2;
Harald Welte87f5d632009-07-04 17:39:00 +0200253 rp->msg_type = rp_msg_type;
254 rp->msg_ref = rp_msg_ref; /* FIXME: Choose randomly */
255
Harald Weltef3efc592009-07-27 20:11:35 +0200256 return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_DATA);
Harald Welte87f5d632009-07-04 17:39:00 +0200257}
258
Harald Welte76042182009-08-08 16:03:15 +0200259static int gsm340_rx_sms_submit(struct msgb *msg, struct gsm_sms *gsms)
Harald Welte7e310b12009-03-30 20:56:32 +0000260{
261 if (db_sms_store(gsms) != 0) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100262 LOGP(DLSMS, LOGL_ERROR, "Failed to store SMS in Database\n");
Harald Welteb9c758b2009-07-05 14:02:46 +0200263 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Harald Welte7e310b12009-03-30 20:56:32 +0000264 }
Harald Welte76042182009-08-08 16:03:15 +0200265 /* dispatch a signal to tell higher level about it */
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100266 send_signal(S_SMS_SUBMITTED, NULL, gsms, 0);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200267
Harald Welte7e310b12009-03-30 20:56:32 +0000268 return 0;
269}
270
Harald Welte76042182009-08-08 16:03:15 +0200271/* generate a TPDU address field compliant with 03.40 sec. 9.1.2.5 */
Holger Hans Peter Freyther366c3312012-11-10 18:24:54 +0100272static int gsm340_gen_oa_sub(uint8_t *oa, unsigned int oa_len,
Harald Welte76042182009-08-08 16:03:15 +0200273 struct gsm_subscriber *subscr)
Harald Weltef3efc592009-07-27 20:11:35 +0200274{
Holger Hans Peter Freyther366c3312012-11-10 18:24:54 +0100275 /* network specific, private numbering plan */
276 return gsm340_gen_oa(oa, oa_len, 0x3, 0x9, subscr->extension);
Harald Weltef3efc592009-07-27 20:11:35 +0200277}
278
Harald Welte76042182009-08-08 16:03:15 +0200279/* generate a msgb containing a TPDU derived from struct gsm_sms,
280 * returns total size of TPDU */
281static int gsm340_gen_tpdu(struct msgb *msg, struct gsm_sms *sms)
Harald Weltef3efc592009-07-27 20:11:35 +0200282{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200283 uint8_t *smsp;
284 uint8_t oa[12]; /* max len per 03.40 */
285 uint8_t oa_len = 0;
286 uint8_t octet_len;
Harald Welte76042182009-08-08 16:03:15 +0200287 unsigned int old_msg_len = msg->len;
Harald Weltef3efc592009-07-27 20:11:35 +0200288
289 /* generate first octet with masked bits */
290 smsp = msgb_put(msg, 1);
Harald Welte76042182009-08-08 16:03:15 +0200291 /* TP-MTI (message type indicator) */
Harald Weltef3efc592009-07-27 20:11:35 +0200292 *smsp = GSM340_SMS_DELIVER_SC2MS;
Harald Welte76042182009-08-08 16:03:15 +0200293 /* TP-MMS (more messages to send) */
294 if (0 /* FIXME */)
Harald Weltef3efc592009-07-27 20:11:35 +0200295 *smsp |= 0x04;
Harald Welte76042182009-08-08 16:03:15 +0200296 /* TP-SRI(deliver)/SRR(submit) */
Harald Weltef3efc592009-07-27 20:11:35 +0200297 if (sms->status_rep_req)
298 *smsp |= 0x20;
Harald Welte76042182009-08-08 16:03:15 +0200299 /* TP-UDHI (indicating TP-UD contains a header) */
300 if (sms->ud_hdr_ind)
Harald Weltef3efc592009-07-27 20:11:35 +0200301 *smsp |= 0x40;
Harald Weltef3efc592009-07-27 20:11:35 +0200302
303 /* generate originator address */
Holger Hans Peter Freyther366c3312012-11-10 18:24:54 +0100304 oa_len = gsm340_gen_oa_sub(oa, sizeof(oa), sms->sender);
Harald Weltef3efc592009-07-27 20:11:35 +0200305 smsp = msgb_put(msg, oa_len);
Harald Weltef3efc592009-07-27 20:11:35 +0200306 memcpy(smsp, oa, oa_len);
307
308 /* generate TP-PID */
309 smsp = msgb_put(msg, 1);
310 *smsp = sms->protocol_id;
311
312 /* generate TP-DCS */
313 smsp = msgb_put(msg, 1);
314 *smsp = sms->data_coding_scheme;
315
316 /* generate TP-SCTS */
317 smsp = msgb_put(msg, 7);
318 gsm340_gen_scts(smsp, time(NULL));
Harald Welte76042182009-08-08 16:03:15 +0200319
Harald Weltef3efc592009-07-27 20:11:35 +0200320 /* generate TP-UDL */
321 smsp = msgb_put(msg, 1);
Harald Welte76042182009-08-08 16:03:15 +0200322 *smsp = sms->user_data_len;
Harald Weltef3efc592009-07-27 20:11:35 +0200323
324 /* generate TP-UD */
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200325 switch (gsm338_get_sms_alphabet(sms->data_coding_scheme)) {
326 case DCS_7BIT_DEFAULT:
Daniel Willmann9aef1452009-08-13 03:39:07 +0200327 octet_len = sms->user_data_len*7/8;
328 if (sms->user_data_len*7%8 != 0)
329 octet_len++;
Daniel Willmann6b1e8222009-08-12 21:17:06 +0200330 /* Warning, user_data_len indicates the amount of septets
331 * (characters), we need amount of octets occupied */
Daniel Willmann9aef1452009-08-13 03:39:07 +0200332 smsp = msgb_put(msg, octet_len);
333 memcpy(smsp, sms->user_data, octet_len);
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200334 break;
335 case DCS_UCS2:
336 case DCS_8BIT_DATA:
337 smsp = msgb_put(msg, sms->user_data_len);
338 memcpy(smsp, sms->user_data, sms->user_data_len);
339 break;
340 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100341 LOGP(DLSMS, LOGL_NOTICE, "Unhandled Data Coding Scheme: 0x%02X\n",
Harald Welted0cf7ba2009-12-24 15:08:18 +0100342 sms->data_coding_scheme);
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200343 break;
Daniel Willmann6b1e8222009-08-12 21:17:06 +0200344 }
Harald Weltef3efc592009-07-27 20:11:35 +0200345
Harald Welte76042182009-08-08 16:03:15 +0200346 return msg->len - old_msg_len;
Harald Weltef3efc592009-07-27 20:11:35 +0200347}
348
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200349/* process an incoming TPDU (called from RP-DATA)
350 * return value > 0: RP CAUSE for ERROR; < 0: silent error; 0 = success */
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100351static int gsm340_rx_tpdu(struct gsm_subscriber_connection *conn, struct msgb *msg)
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000352{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200353 uint8_t *smsp = msgb_sms(msg);
Harald Welte7e310b12009-03-30 20:56:32 +0000354 struct gsm_sms *gsms;
Harald Welte258c7132011-07-16 13:34:52 +0200355 unsigned int sms_alphabet;
356 uint8_t sms_mti, sms_mms, sms_vpf, sms_rp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200357 uint8_t *sms_vp;
358 uint8_t da_len_bytes;
359 uint8_t address_lv[12]; /* according to 03.40 / 9.1.2.5 */
Harald Welte7e310b12009-03-30 20:56:32 +0000360 int rc = 0;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000361
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200362 osmo_counter_inc(conn->bts->network->stats.sms.submitted);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100363
Harald Welte76042182009-08-08 16:03:15 +0200364 gsms = sms_alloc();
365 if (!gsms)
Harald Welteb9c758b2009-07-05 14:02:46 +0200366 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Harald Welte7e310b12009-03-30 20:56:32 +0000367
368 /* invert those fields where 0 means active/present */
Harald Welte76042182009-08-08 16:03:15 +0200369 sms_mti = *smsp & 0x03;
370 sms_mms = !!(*smsp & 0x04);
371 sms_vpf = (*smsp & 0x18) >> 3;
372 gsms->status_rep_req = (*smsp & 0x20);
373 gsms->ud_hdr_ind = (*smsp & 0x40);
374 sms_rp = (*smsp & 0x80);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000375
376 smsp++;
Harald Welte76042182009-08-08 16:03:15 +0200377 gsms->msg_ref = *smsp++;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000378
Harald Welte7e310b12009-03-30 20:56:32 +0000379 /* length in bytes of the destination address */
380 da_len_bytes = 2 + *smsp/2 + *smsp%2;
381 if (da_len_bytes > 12) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100382 LOGP(DLSMS, LOGL_ERROR, "Destination Address > 12 bytes ?!?\n");
Harald Welteb9c758b2009-07-05 14:02:46 +0200383 rc = GSM411_RP_CAUSE_SEMANT_INC_MSG;
Harald Welte7e310b12009-03-30 20:56:32 +0000384 goto out;
Harald Weltee07b6a72012-11-23 19:02:37 +0100385 } else if (da_len_bytes < 4) {
386 LOGP(DLSMS, LOGL_ERROR, "Destination Address < 4 bytes ?!?\n");
387 rc = GSM411_RP_CAUSE_SEMANT_INC_MSG;
388 goto out;
Harald Welte7e310b12009-03-30 20:56:32 +0000389 }
Harald Welte3cfdb222009-06-12 02:42:11 +0800390 memset(address_lv, 0, sizeof(address_lv));
Harald Welte7e310b12009-03-30 20:56:32 +0000391 memcpy(address_lv, smsp, da_len_bytes);
392 /* mangle first byte to reflect length in bytes, not digits */
Harald Welte3cfdb222009-06-12 02:42:11 +0800393 address_lv[0] = da_len_bytes - 1;
Harald Weltee07b6a72012-11-23 19:02:37 +0100394
395 gsms->destination.ton = (address_lv[1] >> 4) & 7;
396 gsms->destination.npi = address_lv[1] & 0xF;
Harald Welte7e310b12009-03-30 20:56:32 +0000397 /* convert to real number */
Harald Weltee07b6a72012-11-23 19:02:37 +0100398 gsm48_decode_bcd_number(gsms->destination.addr,
399 sizeof(gsms->destination.addr), address_lv, 1);
Harald Welte7e310b12009-03-30 20:56:32 +0000400 smsp += da_len_bytes;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000401
Harald Welte76042182009-08-08 16:03:15 +0200402 gsms->protocol_id = *smsp++;
403 gsms->data_coding_scheme = *smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000404
Harald Welte76042182009-08-08 16:03:15 +0200405 sms_alphabet = gsm338_get_sms_alphabet(gsms->data_coding_scheme);
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800406 if (sms_alphabet == 0xffffffff) {
407 sms_free(gsms);
Harald Weltee98c6352010-06-09 15:29:13 +0200408 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800409 }
Harald Welte7e310b12009-03-30 20:56:32 +0000410
Harald Welte76042182009-08-08 16:03:15 +0200411 switch (sms_vpf) {
Harald Welte7e310b12009-03-30 20:56:32 +0000412 case GSM340_TP_VPF_RELATIVE:
Harald Welte76042182009-08-08 16:03:15 +0200413 sms_vp = smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000414 break;
415 case GSM340_TP_VPF_ABSOLUTE:
416 case GSM340_TP_VPF_ENHANCED:
Harald Welte76042182009-08-08 16:03:15 +0200417 sms_vp = smsp;
Steffen Neubauerf3262672009-11-26 12:28:41 +0100418 /* the additional functionality indicator... */
Steffen Neubauerac0c13c2009-12-05 12:44:41 +0100419 if (sms_vpf == GSM340_TP_VPF_ENHANCED && *smsp & (1<<7))
420 smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000421 smsp += 7;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000422 break;
Daniel Willmann58c83d82009-08-13 03:40:49 +0200423 case GSM340_TP_VPF_NONE:
424 sms_vp = 0;
425 break;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000426 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100427 LOGP(DLSMS, LOGL_NOTICE,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100428 "SMS Validity period not implemented: 0x%02x\n", sms_vpf);
Harald Welte76042182009-08-08 16:03:15 +0200429 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000430 }
Harald Welte76042182009-08-08 16:03:15 +0200431 gsms->user_data_len = *smsp++;
432 if (gsms->user_data_len) {
433 memcpy(gsms->user_data, smsp, gsms->user_data_len);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000434
Harald Welte76042182009-08-08 16:03:15 +0200435 switch (sms_alphabet) {
Harald Welte7e310b12009-03-30 20:56:32 +0000436 case DCS_7BIT_DEFAULT:
Harald Welte76042182009-08-08 16:03:15 +0200437 gsm_7bit_decode(gsms->text, smsp, gsms->user_data_len);
Harald Welte7e310b12009-03-30 20:56:32 +0000438 break;
439 case DCS_8BIT_DATA:
440 case DCS_UCS2:
441 case DCS_NONE:
Harald Welte7e310b12009-03-30 20:56:32 +0000442 break;
443 }
444 }
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000445
Holger Hans Peter Freyther4f834092010-06-16 14:16:47 +0800446 gsms->sender = subscr_get(conn->subscr);
Harald Welted0cf7ba2009-12-24 15:08:18 +0100447
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100448 LOGP(DLSMS, LOGL_INFO, "RX SMS: Sender: %s, MTI: 0x%02x, VPF: 0x%02x, "
Harald Welted0cf7ba2009-12-24 15:08:18 +0100449 "MR: 0x%02x PID: 0x%02x, DCS: 0x%02x, DA: %s, "
450 "UserDataLength: 0x%02x, UserData: \"%s\"\n",
451 subscr_name(gsms->sender), sms_mti, sms_vpf, gsms->msg_ref,
452 gsms->protocol_id, gsms->data_coding_scheme, gsms->dest_addr,
453 gsms->user_data_len,
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200454 sms_alphabet == DCS_7BIT_DEFAULT ? gsms->text :
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200455 osmo_hexdump(gsms->user_data, gsms->user_data_len));
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000456
Harald Welte76042182009-08-08 16:03:15 +0200457 gsms->validity_minutes = gsm340_validity_period(sms_vpf, sms_vp);
Harald Welteb9c758b2009-07-05 14:02:46 +0200458
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100459 /* FIXME: This looks very wrong */
460 send_signal(0, NULL, gsms, 0);
Harald Welteb9c758b2009-07-05 14:02:46 +0200461
Harald Welte7e310b12009-03-30 20:56:32 +0000462 /* determine gsms->receiver based on dialled number */
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100463 gsms->receiver = subscr_get_by_extension(conn->bts->network, gsms->dest_addr);
Harald Welte7e310b12009-03-30 20:56:32 +0000464 if (!gsms->receiver) {
Harald Weltee07b6a72012-11-23 19:02:37 +0100465#ifdef BUILD_SMPP
466 rc = smpp_try_deliver(gsms);
467 if (rc == 1) {
468 rc = 1; /* cause 1: unknown subscriber */
469 osmo_counter_inc(conn->bts->network->stats.sms.no_receiver);
470 } else if (rc < 0) {
471 rc = 21; /* cause 21: short message transfer rejected */
472 /* FIXME: handle the error somehow? */
473 }
474#else
Harald Welte7e310b12009-03-30 20:56:32 +0000475 rc = 1; /* cause 1: unknown subscriber */
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200476 osmo_counter_inc(conn->bts->network->stats.sms.no_receiver);
Harald Weltee07b6a72012-11-23 19:02:37 +0100477#endif
Harald Welte7e310b12009-03-30 20:56:32 +0000478 goto out;
479 }
480
Harald Welte76042182009-08-08 16:03:15 +0200481 switch (sms_mti) {
Harald Welte7e310b12009-03-30 20:56:32 +0000482 case GSM340_SMS_SUBMIT_MS2SC:
483 /* MS is submitting a SMS */
Harald Welte76042182009-08-08 16:03:15 +0200484 rc = gsm340_rx_sms_submit(msg, gsms);
Harald Welte7e310b12009-03-30 20:56:32 +0000485 break;
486 case GSM340_SMS_COMMAND_MS2SC:
487 case GSM340_SMS_DELIVER_REP_MS2SC:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100488 LOGP(DLSMS, LOGL_NOTICE, "Unimplemented MTI 0x%02x\n", sms_mti);
Harald Welteb9c758b2009-07-05 14:02:46 +0200489 rc = GSM411_RP_CAUSE_IE_NOTEXIST;
Harald Welte7e310b12009-03-30 20:56:32 +0000490 break;
491 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100492 LOGP(DLSMS, LOGL_NOTICE, "Undefined MTI 0x%02x\n", sms_mti);
Harald Welteb9c758b2009-07-05 14:02:46 +0200493 rc = GSM411_RP_CAUSE_IE_NOTEXIST;
Harald Welte7e310b12009-03-30 20:56:32 +0000494 break;
495 }
496
Harald Welteb9c758b2009-07-05 14:02:46 +0200497 if (!rc && !gsms->receiver)
498 rc = GSM411_RP_CAUSE_MO_NUM_UNASSIGNED;
499
Harald Welte7e310b12009-03-30 20:56:32 +0000500out:
Harald Welte76042182009-08-08 16:03:15 +0200501 sms_free(gsms);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000502
Harald Welte7e310b12009-03-30 20:56:32 +0000503 return rc;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000504}
505
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200506static int gsm411_send_rp_ack(struct gsm_trans *trans, uint8_t msg_ref)
Daniel Willmann471712b2008-12-29 01:54:02 +0000507{
508 struct msgb *msg = gsm411_msgb_alloc();
Daniel Willmann471712b2008-12-29 01:54:02 +0000509
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100510 DEBUGP(DLSMS, "TX: SMS RP ACK\n");
Daniel Willmann471712b2008-12-29 01:54:02 +0000511
Harald Weltef3efc592009-07-27 20:11:35 +0200512 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ACK_MT, msg_ref);
Daniel Willmann471712b2008-12-29 01:54:02 +0000513}
514
Harald Weltef3efc592009-07-27 20:11:35 +0200515static int gsm411_send_rp_error(struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200516 uint8_t msg_ref, uint8_t cause)
Daniel Willmann471712b2008-12-29 01:54:02 +0000517{
518 struct msgb *msg = gsm411_msgb_alloc();
Daniel Willmann471712b2008-12-29 01:54:02 +0000519
Harald Welte7e310b12009-03-30 20:56:32 +0000520 msgb_tv_put(msg, 1, cause);
Daniel Willmann471712b2008-12-29 01:54:02 +0000521
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100522 LOGP(DLSMS, LOGL_NOTICE, "TX: SMS RP ERROR, cause %d (%s)\n", cause,
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200523 get_value_string(rp_cause_strs, cause));
Daniel Willmann471712b2008-12-29 01:54:02 +0000524
Harald Weltef3efc592009-07-27 20:11:35 +0200525 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ERROR_MT, msg_ref);
Daniel Willmann471712b2008-12-29 01:54:02 +0000526}
527
Harald Welte7e310b12009-03-30 20:56:32 +0000528/* Receive a 04.11 TPDU inside RP-DATA / user data */
Harald Weltef3efc592009-07-27 20:11:35 +0200529static int gsm411_rx_rp_ud(struct msgb *msg, struct gsm_trans *trans,
530 struct gsm411_rp_hdr *rph,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200531 uint8_t src_len, uint8_t *src,
532 uint8_t dst_len, uint8_t *dst,
533 uint8_t tpdu_len, uint8_t *tpdu)
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000534{
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000535 int rc = 0;
536
Harald Welte7e310b12009-03-30 20:56:32 +0000537 if (src_len && src)
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100538 LOGP(DLSMS, LOGL_ERROR, "RP-DATA (MO) with SRC ?!?\n");
Harald Welte7e310b12009-03-30 20:56:32 +0000539
540 if (!dst_len || !dst || !tpdu_len || !tpdu) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100541 LOGP(DLSMS, LOGL_ERROR,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100542 "RP-DATA (MO) without DST or TPDU ?!?\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200543 gsm411_send_rp_error(trans, rph->msg_ref,
Harald Welteb9c758b2009-07-05 14:02:46 +0200544 GSM411_RP_CAUSE_INV_MAND_INF);
Harald Welte7e310b12009-03-30 20:56:32 +0000545 return -EIO;
546 }
Harald Weltee9dd9b02010-04-30 14:27:05 +0200547 msg->l4h = tpdu;
Harald Welte7e310b12009-03-30 20:56:32 +0000548
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100549 DEBUGP(DLSMS, "DST(%u,%s)\n", dst_len, osmo_hexdump(dst, dst_len));
Harald Welte7e310b12009-03-30 20:56:32 +0000550
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100551 rc = gsm340_rx_tpdu(trans->conn, msg);
Harald Welte7e310b12009-03-30 20:56:32 +0000552 if (rc == 0)
Harald Weltef3efc592009-07-27 20:11:35 +0200553 return gsm411_send_rp_ack(trans, rph->msg_ref);
Harald Welte7e310b12009-03-30 20:56:32 +0000554 else if (rc > 0)
Harald Weltef3efc592009-07-27 20:11:35 +0200555 return gsm411_send_rp_error(trans, rph->msg_ref, rc);
Harald Welte7e310b12009-03-30 20:56:32 +0000556 else
557 return rc;
558}
559
560/* Receive a 04.11 RP-DATA message in accordance with Section 7.3.1.2 */
Harald Weltef3efc592009-07-27 20:11:35 +0200561static int gsm411_rx_rp_data(struct msgb *msg, struct gsm_trans *trans,
562 struct gsm411_rp_hdr *rph)
Harald Welte7e310b12009-03-30 20:56:32 +0000563{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200564 uint8_t src_len, dst_len, rpud_len;
565 uint8_t *src = NULL, *dst = NULL , *rp_ud = NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000566
567 /* in the MO case, this should always be zero length */
568 src_len = rph->data[0];
569 if (src_len)
570 src = &rph->data[1];
571
572 dst_len = rph->data[1+src_len];
573 if (dst_len)
574 dst = &rph->data[1+src_len+1];
575
576 rpud_len = rph->data[1+src_len+1+dst_len];
577 if (rpud_len)
578 rp_ud = &rph->data[1+src_len+1+dst_len+1];
579
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100580 DEBUGP(DLSMS, "RX_RP-DATA: src_len=%u, dst_len=%u ud_len=%u\n",
Harald Welteb9c758b2009-07-05 14:02:46 +0200581 src_len, dst_len, rpud_len);
Harald Weltef3efc592009-07-27 20:11:35 +0200582 return gsm411_rx_rp_ud(msg, trans, rph, src_len, src, dst_len, dst,
Harald Welte7e310b12009-03-30 20:56:32 +0000583 rpud_len, rp_ud);
584}
585
Harald Weltecb8f4432009-08-09 14:59:02 +0200586/* Receive a 04.11 RP-ACK message (response to RP-DATA from us) */
Harald Weltef3efc592009-07-27 20:11:35 +0200587static int gsm411_rx_rp_ack(struct msgb *msg, struct gsm_trans *trans,
588 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200589{
Harald Welte76042182009-08-08 16:03:15 +0200590 struct gsm_sms *sms = trans->sms.sms;
591
Harald Welteb9c758b2009-07-05 14:02:46 +0200592 /* Acnkowledgement to MT RP_DATA, i.e. the MS confirms it
593 * successfully received a SMS. We can now safely mark it as
594 * transmitted */
595
Harald Weltecb8f4432009-08-09 14:59:02 +0200596 if (!trans->sms.is_mt) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100597 LOGP(DLSMS, LOGL_ERROR, "RX RP-ACK on a MO transfer ?\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200598 return gsm411_send_rp_error(trans, rph->msg_ref,
599 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
600 }
Harald Welte3e0f6172009-07-09 23:52:59 +0200601
Harald Welte76042182009-08-08 16:03:15 +0200602 if (!sms) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100603 LOGP(DLSMS, LOGL_ERROR, "RX RP-ACK but no sms in transaction?!?\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200604 return gsm411_send_rp_error(trans, rph->msg_ref,
605 GSM411_RP_CAUSE_PROTOCOL_ERR);
Harald Welte76042182009-08-08 16:03:15 +0200606 }
607
608 /* mark this SMS as sent in database */
609 db_sms_mark_sent(sms);
610
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100611 send_signal(S_SMS_DELIVERED, trans, sms, 0);
Harald Welte76042182009-08-08 16:03:15 +0200612
613 sms_free(sms);
614 trans->sms.sms = NULL;
615
Harald Weltecf6a3812009-08-09 19:07:41 +0200616 /* check for more messages for this subscriber */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100617 sms = db_sms_get_unsent_for_subscr(trans->subscr);
Harald Weltecf6a3812009-08-09 19:07:41 +0200618 if (sms)
Holger Hans Peter Freyther0fbb3ec2010-06-16 14:19:31 +0800619 gsm411_send_sms(trans->conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800620 else
621 gsm411_release_conn(trans->conn);
Sylvain Munautd6c35f62009-12-24 13:33:51 +0100622
623 /* free the transaction here */
624 trans_free(trans);
Harald Welte76042182009-08-08 16:03:15 +0200625 return 0;
Harald Welteb9c758b2009-07-05 14:02:46 +0200626}
627
Harald Weltef3efc592009-07-27 20:11:35 +0200628static int gsm411_rx_rp_error(struct msgb *msg, struct gsm_trans *trans,
629 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200630{
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100631 struct gsm_network *net = trans->conn->bts->network;
Harald Welte76042182009-08-08 16:03:15 +0200632 struct gsm_sms *sms = trans->sms.sms;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200633 uint8_t cause_len = rph->data[0];
634 uint8_t cause = rph->data[1];
Harald Weltef3efc592009-07-27 20:11:35 +0200635
Harald Welteb9c758b2009-07-05 14:02:46 +0200636 /* Error in response to MT RP_DATA, i.e. the MS did not
637 * successfully receive the SMS. We need to investigate
638 * the cause and take action depending on it */
639
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100640 LOGP(DLSMS, LOGL_NOTICE, "%s: RX SMS RP-ERROR, cause %d:%d (%s)\n",
Holger Hans Peter Freytherd4c16b12010-03-23 07:56:22 +0100641 subscr_name(trans->conn->subscr), cause_len, cause,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100642 get_value_string(rp_cause_strs, cause));
Harald Weltef3efc592009-07-27 20:11:35 +0200643
Harald Weltecb8f4432009-08-09 14:59:02 +0200644 if (!trans->sms.is_mt) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100645 LOGP(DLSMS, LOGL_ERROR, "RX RP-ERR on a MO transfer ?\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200646#if 0
Harald Weltecb8f4432009-08-09 14:59:02 +0200647 return gsm411_send_rp_error(trans, rph->msg_ref,
648 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200649#endif
Harald Weltecb8f4432009-08-09 14:59:02 +0200650 }
Harald Welte3e0f6172009-07-09 23:52:59 +0200651
Harald Welte76042182009-08-08 16:03:15 +0200652 if (!sms) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100653 LOGP(DLSMS, LOGL_ERROR,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100654 "RX RP-ERR, but no sms in transaction?!?\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200655 return -EINVAL;
656#if 0
Harald Weltecb8f4432009-08-09 14:59:02 +0200657 return gsm411_send_rp_error(trans, rph->msg_ref,
658 GSM411_RP_CAUSE_PROTOCOL_ERR);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200659#endif
Harald Weltecb8f4432009-08-09 14:59:02 +0200660 }
661
662 if (cause == GSM411_RP_CAUSE_MT_MEM_EXCEEDED) {
663 /* MS has not enough memory to store the message. We need
Holger Hans Peter Freytherdd715bd2010-12-23 22:23:15 +0100664 * to store this in our database and wait for a SMMA message */
Harald Weltecb8f4432009-08-09 14:59:02 +0200665 /* FIXME */
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100666 send_signal(S_SMS_MEM_EXCEEDED, trans, sms, 0);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200667 osmo_counter_inc(net->stats.sms.rp_err_mem);
Holger Hans Peter Freyther0eb76662010-12-24 09:47:04 +0100668 } else {
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100669 send_signal(S_SMS_UNKNOWN_ERROR, trans, sms, 0);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200670 osmo_counter_inc(net->stats.sms.rp_err_other);
Holger Hans Peter Freyther0eb76662010-12-24 09:47:04 +0100671 }
Harald Welte76042182009-08-08 16:03:15 +0200672
673 sms_free(sms);
674 trans->sms.sms = NULL;
675
Harald Weltef3efc592009-07-27 20:11:35 +0200676 return 0;
Harald Welteb9c758b2009-07-05 14:02:46 +0200677}
678
Harald Weltef3efc592009-07-27 20:11:35 +0200679static int gsm411_rx_rp_smma(struct msgb *msg, struct gsm_trans *trans,
680 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200681{
Harald Weltecf6a3812009-08-09 19:07:41 +0200682 struct gsm_sms *sms;
Harald Weltef3efc592009-07-27 20:11:35 +0200683 int rc;
684
Harald Weltecf6a3812009-08-09 19:07:41 +0200685 rc = gsm411_send_rp_ack(trans, rph->msg_ref);
686 trans->sms.rp_state = GSM411_RPS_IDLE;
687
Harald Welteb9c758b2009-07-05 14:02:46 +0200688 /* MS tells us that it has memory for more SMS, we need
689 * to check if we have any pending messages for it and then
690 * transfer those */
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100691 send_signal(S_SMS_SMMA, trans, NULL, 0);
Harald Weltef3efc592009-07-27 20:11:35 +0200692
Harald Weltecf6a3812009-08-09 19:07:41 +0200693 /* check for more messages for this subscriber */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100694 sms = db_sms_get_unsent_for_subscr(trans->subscr);
Harald Weltecf6a3812009-08-09 19:07:41 +0200695 if (sms)
Holger Hans Peter Freyther0fbb3ec2010-06-16 14:19:31 +0800696 gsm411_send_sms(trans->conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800697 else
698 gsm411_release_conn(trans->conn);
Harald Weltef3efc592009-07-27 20:11:35 +0200699
700 return rc;
Harald Welteb9c758b2009-07-05 14:02:46 +0200701}
702
Harald Weltef3efc592009-07-27 20:11:35 +0200703static int gsm411_rx_cp_data(struct msgb *msg, struct gsm48_hdr *gh,
704 struct gsm_trans *trans)
Harald Welte7e310b12009-03-30 20:56:32 +0000705{
Daniel Willmann471712b2008-12-29 01:54:02 +0000706 struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200707 uint8_t msg_type = rp_data->msg_type & 0x07;
Harald Welte7e310b12009-03-30 20:56:32 +0000708 int rc = 0;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000709
710 switch (msg_type) {
711 case GSM411_MT_RP_DATA_MO:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100712 DEBUGP(DLSMS, "RX SMS RP-DATA (MO)\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200713 /* start TR2N and enter 'wait to send RP-ACK state' */
714 trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK;
715 rc = gsm411_rx_rp_data(msg, trans, rp_data);
Harald Welte7e310b12009-03-30 20:56:32 +0000716 break;
717 case GSM411_MT_RP_ACK_MO:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100718 DEBUGP(DLSMS,"RX SMS RP-ACK (MO)\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200719 rc = gsm411_rx_rp_ack(msg, trans, rp_data);
Harald Welteb9c758b2009-07-05 14:02:46 +0200720 break;
Harald Welte7e310b12009-03-30 20:56:32 +0000721 case GSM411_MT_RP_SMMA_MO:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100722 DEBUGP(DLSMS, "RX SMS RP-SMMA\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200723 /* start TR2N and enter 'wait to send RP-ACK state' */
724 trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK;
725 rc = gsm411_rx_rp_smma(msg, trans, rp_data);
726 break;
727 case GSM411_MT_RP_ERROR_MO:
728 rc = gsm411_rx_rp_error(msg, trans, rp_data);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000729 break;
730 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100731 LOGP(DLSMS, LOGL_NOTICE, "Invalid RP type 0x%02x\n", msg_type);
Harald Weltef3efc592009-07-27 20:11:35 +0200732 rc = gsm411_send_rp_error(trans, rp_data->msg_ref,
733 GSM411_RP_CAUSE_MSGTYPE_NOTEXIST);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000734 break;
735 }
736
737 return rc;
738}
739
Harald Weltef3efc592009-07-27 20:11:35 +0200740/* send CP-ACK to given transaction */
741static int gsm411_tx_cp_ack(struct gsm_trans *trans)
742{
743 struct msgb *msg = gsm411_msgb_alloc();
Harald Weltecf6a3812009-08-09 19:07:41 +0200744 int rc;
Harald Weltef3efc592009-07-27 20:11:35 +0200745
Harald Weltecf6a3812009-08-09 19:07:41 +0200746 rc = gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ACK);
747
748 if (trans->sms.is_mt) {
749 /* If this is a MT SMS DELIVER, we can clear transaction here */
750 trans->sms.cp_state = GSM411_CPS_IDLE;
Harald Welte (local)86b17172009-08-14 14:52:17 +0200751 //trans_free(trans);
Harald Weltecf6a3812009-08-09 19:07:41 +0200752 }
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200753
754 return rc;
Harald Weltef3efc592009-07-27 20:11:35 +0200755}
756
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200757static int gsm411_tx_cp_error(struct gsm_trans *trans, uint8_t cause)
Harald Weltef3efc592009-07-27 20:11:35 +0200758{
759 struct msgb *msg = gsm411_msgb_alloc();
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200760 uint8_t *causep;
Harald Weltef3efc592009-07-27 20:11:35 +0200761
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100762 LOGP(DLSMS, LOGL_NOTICE, "TX CP-ERROR, cause %d (%s)\n", cause,
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200763 get_value_string(cp_cause_strs, cause));
764
Harald Welte76042182009-08-08 16:03:15 +0200765 causep = msgb_put(msg, 1);
Harald Weltef3efc592009-07-27 20:11:35 +0200766 *causep = cause;
767
768 return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ERROR);
769}
770
771/* Entry point for incoming GSM48_PDISC_SMS from abis_rsl.c */
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800772int gsm0411_rcv_sms(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800773 struct msgb *msg)
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000774{
775 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200776 uint8_t msg_type = gh->msg_type;
777 uint8_t transaction_id = ((gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Weltef3efc592009-07-27 20:11:35 +0200778 struct gsm_trans *trans;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000779 int rc = 0;
780
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800781 if (!conn->subscr)
Harald Weltef3efc592009-07-27 20:11:35 +0200782 return -EIO;
783 /* FIXME: send some error message */
784
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100785 DEBUGP(DLSMS, "trans_id=%x ", transaction_id);
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800786 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_SMS,
Harald Weltef3efc592009-07-27 20:11:35 +0200787 transaction_id);
788 if (!trans) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100789 DEBUGPC(DLSMS, "(new) ");
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800790 trans = trans_alloc(conn->subscr, GSM48_PDISC_SMS,
Harald Weltef3efc592009-07-27 20:11:35 +0200791 transaction_id, new_callref++);
792 if (!trans) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100793 DEBUGPC(DLSMS, "No memory for trans\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200794 /* FIXME: send some error message */
795 return -ENOMEM;
796 }
797 trans->sms.cp_state = GSM411_CPS_IDLE;
798 trans->sms.rp_state = GSM411_RPS_IDLE;
799 trans->sms.is_mt = 0;
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800800 trans->sms.link_id = UM_SAPI_SMS;
Harald Weltef3efc592009-07-27 20:11:35 +0200801
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800802 trans->conn = conn;
Harald Weltef3efc592009-07-27 20:11:35 +0200803 }
804
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000805 switch(msg_type) {
806 case GSM411_MT_CP_DATA:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100807 DEBUGPC(DLSMS, "RX SMS CP-DATA\n");
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100808
809 /* 5.4: For MO, if a CP-DATA is received for a new
810 * transaction, equals reception of an implicit
811 * last CP-ACK for previous transaction */
812 if (trans->sms.cp_state == GSM411_CPS_IDLE) {
813 int i;
814 struct gsm_trans *ptrans;
815
816 /* Scan through all remote initiated transactions */
817 for (i=8; i<15; i++) {
818 if (i == transaction_id)
819 continue;
820
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800821 ptrans = trans_find_by_id(conn->subscr,
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100822 GSM48_PDISC_SMS, i);
823 if (!ptrans)
824 continue;
825
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100826 DEBUGP(DLSMS, "Implicit CP-ACK for trans_id=%x\n", i);
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100827
828 /* Finish it for good */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200829 osmo_timer_del(&ptrans->sms.cp_timer);
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100830 ptrans->sms.cp_state = GSM411_CPS_IDLE;
831 trans_free(ptrans);
832 }
833 }
834
Harald Weltecb8f4432009-08-09 14:59:02 +0200835 /* 5.2.3.1.3: MO state exists when SMC has received
836 * CP-DATA, including sending of the assoc. CP-ACK */
837 /* 5.2.3.2.4: MT state exists when SMC has received
838 * CP-DATA, including sending of the assoc. CP-ACK */
839 trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED;
Harald Weltef3efc592009-07-27 20:11:35 +0200840
Harald Welte (local)daef6062009-08-14 11:41:12 +0200841 /* SMC instance acknowledges the CP-DATA frame */
842 gsm411_tx_cp_ack(trans);
843
Harald Weltef3efc592009-07-27 20:11:35 +0200844 rc = gsm411_rx_cp_data(msg, gh, trans);
Harald Welte (local)daef6062009-08-14 11:41:12 +0200845#if 0
Harald Weltef3efc592009-07-27 20:11:35 +0200846 /* Send CP-ACK or CP-ERORR in response */
847 if (rc < 0) {
848 rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_NET_FAIL);
849 } else
850 rc = gsm411_tx_cp_ack(trans);
Harald Welte (local)daef6062009-08-14 11:41:12 +0200851#endif
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000852 break;
853 case GSM411_MT_CP_ACK:
Harald Weltef3efc592009-07-27 20:11:35 +0200854 /* previous CP-DATA in this transaction was confirmed */
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100855 DEBUGPC(DLSMS, "RX SMS CP-ACK\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200856 /* 5.2.3.1.3: MO state exists when SMC has received CP-ACK */
857 /* 5.2.3.2.4: MT state exists when SMC has received CP-ACK */
858 trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED;
Harald Welte41985612009-08-10 00:24:32 +0200859 /* Stop TC1* after CP-ACK has been received */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200860 osmo_timer_del(&trans->sms.cp_timer);
Harald Weltecb8f4432009-08-09 14:59:02 +0200861
Harald Weltef3efc592009-07-27 20:11:35 +0200862 if (!trans->sms.is_mt) {
Holger Hans Peter Freyther0afa0442010-06-10 18:08:12 +0800863 /* FIXME: we have sent one CP-DATA, which was now
Harald Weltef3efc592009-07-27 20:11:35 +0200864 * acknowledged. Check if we want to transfer more,
865 * i.e. multi-part message */
866 trans->sms.cp_state = GSM411_CPS_IDLE;
867 trans_free(trans);
868 }
Daniel Willmannbb16e8e2008-12-29 03:53:50 +0000869 break;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000870 case GSM411_MT_CP_ERROR:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100871 DEBUGPC(DLSMS, "RX SMS CP-ERROR, cause %d (%s)\n", gh->data[0],
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200872 get_value_string(cp_cause_strs, gh->data[0]));
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200873 osmo_timer_del(&trans->sms.cp_timer);
Harald Weltef3efc592009-07-27 20:11:35 +0200874 trans->sms.cp_state = GSM411_CPS_IDLE;
875 trans_free(trans);
Daniel Willmannbb16e8e2008-12-29 03:53:50 +0000876 break;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000877 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100878 DEBUGPC(DLSMS, "RX Unimplemented CP msg_type: 0x%02x\n", msg_type);
Harald Weltef3efc592009-07-27 20:11:35 +0200879 rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_MSGTYPE_NOTEXIST);
Harald Weltecb8f4432009-08-09 14:59:02 +0200880 trans->sms.cp_state = GSM411_CPS_IDLE;
Harald Weltef3efc592009-07-27 20:11:35 +0200881 trans_free(trans);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000882 break;
883 }
884
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000885 return rc;
886}
887
Harald Weltecf6a3812009-08-09 19:07:41 +0200888/* Take a SMS in gsm_sms structure and send it through an already
889 * existing lchan. We also assume that the caller ensured this lchan already
890 * has a SAPI3 RLL connection! */
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100891int gsm411_send_sms(struct gsm_subscriber_connection *conn, struct gsm_sms *sms)
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000892{
893 struct msgb *msg = gsm411_msgb_alloc();
Harald Weltef3efc592009-07-27 20:11:35 +0200894 struct gsm_trans *trans;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200895 uint8_t *data, *rp_ud_len;
896 uint8_t msg_ref = 42;
Sylvain Munautc4052cf2009-12-24 13:27:36 +0100897 int transaction_id;
Harald Welte76042182009-08-08 16:03:15 +0200898 int rc;
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000899
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100900 transaction_id = trans_assign_trans_id(conn->subscr, GSM48_PDISC_SMS, 0);
Harald Welte49839212009-12-22 13:45:58 +0100901 if (transaction_id == -1) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100902 LOGP(DLSMS, LOGL_ERROR, "No available transaction ids\n");
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100903 send_signal(S_SMS_UNKNOWN_ERROR, NULL, sms, 0);
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800904 sms_free(sms);
Harald Welte49839212009-12-22 13:45:58 +0100905 return -EBUSY;
906 }
Harald Weltecf6a3812009-08-09 19:07:41 +0200907
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100908 DEBUGP(DLSMS, "send_sms_lchan()\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200909
Harald Welte76042182009-08-08 16:03:15 +0200910 /* FIXME: allocate transaction with message reference */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100911 trans = trans_alloc(conn->subscr, GSM48_PDISC_SMS,
Harald Welte76042182009-08-08 16:03:15 +0200912 transaction_id, new_callref++);
913 if (!trans) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100914 LOGP(DLSMS, LOGL_ERROR, "No memory for trans\n");
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100915 send_signal(S_SMS_UNKNOWN_ERROR, NULL, sms, 0);
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800916 sms_free(sms);
Harald Welte76042182009-08-08 16:03:15 +0200917 /* FIXME: send some error message */
918 return -ENOMEM;
919 }
920 trans->sms.cp_state = GSM411_CPS_IDLE;
921 trans->sms.rp_state = GSM411_RPS_IDLE;
922 trans->sms.is_mt = 1;
923 trans->sms.sms = sms;
Harald Welte (local)daef6062009-08-14 11:41:12 +0200924 trans->sms.link_id = UM_SAPI_SMS; /* FIXME: main or SACCH ? */
Harald Welte76042182009-08-08 16:03:15 +0200925
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100926 trans->conn = conn;
Harald Welte76042182009-08-08 16:03:15 +0200927
928 /* Hardcode SMSC Originating Address for now */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200929 data = (uint8_t *)msgb_put(msg, 8);
Harald Welte87f5d632009-07-04 17:39:00 +0200930 data[0] = 0x07; /* originator length == 7 */
Harald Welteb9c758b2009-07-05 14:02:46 +0200931 data[1] = 0x91; /* type of number: international, ISDN */
932 data[2] = 0x44; /* 447785016005 */
Daniel Willmanna3e29842008-12-29 16:03:54 +0000933 data[3] = 0x77;
934 data[4] = 0x58;
935 data[5] = 0x10;
936 data[6] = 0x06;
937 data[7] = 0x50;
Harald Welte87f5d632009-07-04 17:39:00 +0200938
939 /* Hardcoded Destination Address */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200940 data = (uint8_t *)msgb_put(msg, 1);
Harald Welte87f5d632009-07-04 17:39:00 +0200941 data[0] = 0; /* destination length == 0 */
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000942
Harald Welte76042182009-08-08 16:03:15 +0200943 /* obtain a pointer for the rp_ud_len, so we can fill it later */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200944 rp_ud_len = (uint8_t *)msgb_put(msg, 1);
Daniel Willmann4a1e8792008-12-29 06:23:56 +0000945
Harald Welte76042182009-08-08 16:03:15 +0200946 /* generate the 03.40 TPDU */
947 rc = gsm340_gen_tpdu(msg, sms);
948 if (rc < 0) {
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100949 send_signal(S_SMS_UNKNOWN_ERROR, trans, sms, 0);
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800950 trans_free(trans);
951 sms_free(sms);
Harald Welte76042182009-08-08 16:03:15 +0200952 msgb_free(msg);
953 return rc;
954 }
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000955
Harald Welte76042182009-08-08 16:03:15 +0200956 *rp_ud_len = rc;
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000957
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100958 DEBUGP(DLSMS, "TX: SMS DELIVER\n");
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000959
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200960 osmo_counter_inc(conn->bts->network->stats.sms.delivered);
Nico Golde1e5ed4e2010-09-16 17:50:29 +0200961 db_sms_inc_deliver_attempts(trans->sms.sms);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100962
Harald Weltef3efc592009-07-27 20:11:35 +0200963 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_DATA_MT, msg_ref);
964 /* FIXME: enter 'wait for RP-ACK' state, start TR1N */
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000965}
Harald Weltef3efc592009-07-27 20:11:35 +0200966
Harald Weltecf6a3812009-08-09 19:07:41 +0200967/* paging callback. Here we get called if paging a subscriber has
968 * succeeded or failed. */
Harald Welte76042182009-08-08 16:03:15 +0200969static int paging_cb_send_sms(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +0800970 struct msgb *msg, void *_conn, void *_sms)
Harald Weltef3efc592009-07-27 20:11:35 +0200971{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +0800972 struct gsm_subscriber_connection *conn = _conn;
Harald Welte76042182009-08-08 16:03:15 +0200973 struct gsm_sms *sms = _sms;
Holger Hans Peter Freyther4cd51582010-06-14 16:03:03 +0800974 int rc = 0;
Harald Weltef3efc592009-07-27 20:11:35 +0200975
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100976 DEBUGP(DLSMS, "paging_cb_send_sms(hooknum=%u, event=%u, msg=%p,"
Holger Hans Peter Freyther52ffb922010-12-29 14:02:34 +0100977 "conn=%p, sms=%p/id: %llu)\n", hooknum, event, msg, conn, sms, sms->id);
Harald Welte76042182009-08-08 16:03:15 +0200978
979 if (hooknum != GSM_HOOK_RR_PAGING)
980 return -EINVAL;
981
982 switch (event) {
983 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +0800984 gsm411_send_sms(conn, sms);
Harald Welte76042182009-08-08 16:03:15 +0200985 break;
986 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherca5702a2010-06-10 18:20:54 +0800987 case GSM_PAGING_OOM:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100988 case GSM_PAGING_BUSY:
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100989 send_signal(S_SMS_UNKNOWN_ERROR, NULL, sms, event);
Harald Welte76042182009-08-08 16:03:15 +0200990 sms_free(sms);
991 rc = -ETIMEDOUT;
992 break;
Holger Hans Peter Freyther52ffb922010-12-29 14:02:34 +0100993 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100994 LOGP(DLSMS, LOGL_ERROR, "Unhandled paging event: %d\n", event);
Harald Welte76042182009-08-08 16:03:15 +0200995 }
996
997 return rc;
998}
999
Harald Weltecf6a3812009-08-09 19:07:41 +02001000/* high-level function to send a SMS to a given subscriber. The function
1001 * will take care of paging the subscriber, establishing the RLL SAPI3
1002 * connection, etc. */
Harald Welte76042182009-08-08 16:03:15 +02001003int gsm411_send_sms_subscr(struct gsm_subscriber *subscr,
1004 struct gsm_sms *sms)
1005{
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001006 struct gsm_subscriber_connection *conn;
Harald Weltecf6a3812009-08-09 19:07:41 +02001007
Harald Welte76042182009-08-08 16:03:15 +02001008 /* check if we already have an open lchan to the subscriber.
1009 * if yes, send the SMS this way */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001010 conn = connection_for_subscr(subscr);
1011 if (conn) {
Holger Hans Peter Freyther0fbb3ec2010-06-16 14:19:31 +08001012 return gsm411_send_sms(conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +08001013 }
Harald Welte76042182009-08-08 16:03:15 +02001014
1015 /* if not, we have to start paging */
Holger Hans Peter Freyther7afac582010-06-10 18:21:25 +08001016 subscr_get_channel(subscr, RSL_CHANNEED_SDCCH, paging_cb_send_sms, sms);
Harald Welte76042182009-08-08 16:03:15 +02001017 return 0;
1018}
Harald Welte3366a942009-07-28 00:44:49 +02001019
Harald Welte (local)86b17172009-08-14 14:52:17 +02001020void _gsm411_sms_trans_free(struct gsm_trans *trans)
1021{
Holger Hans Peter Freythercea722b2010-06-15 12:29:37 +08001022 if (trans->sms.sms) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +01001023 LOGP(DLSMS, LOGL_ERROR, "Transaction contains SMS.\n");
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +01001024 send_signal(S_SMS_UNKNOWN_ERROR, trans, trans->sms.sms, 0);
Holger Hans Peter Freythercea722b2010-06-15 12:29:37 +08001025 sms_free(trans->sms.sms);
1026 trans->sms.sms = NULL;
1027 }
1028
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001029 osmo_timer_del(&trans->sms.cp_timer);
Harald Welte (local)86b17172009-08-14 14:52:17 +02001030}
1031
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001032void gsm411_sapi_n_reject(struct gsm_subscriber_connection *conn)
1033{
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +01001034 struct gsm_subscriber *subscr;
Holger Hans Peter Freyther79eff7e2010-12-27 23:25:53 +01001035 struct gsm_network *net;
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001036 struct gsm_trans *trans, *tmp;
1037
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +01001038 subscr = subscr_get(conn->subscr);
Holger Hans Peter Freyther79eff7e2010-12-27 23:25:53 +01001039 net = conn->bts->network;
1040
1041 llist_for_each_entry_safe(trans, tmp, &net->trans_list, entry)
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001042 if (trans->conn == conn) {
1043 struct gsm_sms *sms = trans->sms.sms;
1044 if (!sms) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +01001045 LOGP(DLSMS, LOGL_ERROR, "SAPI Reject but no SMS.\n");
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001046 continue;
1047 }
1048
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +01001049 send_signal(S_SMS_UNKNOWN_ERROR, trans, sms, 0);
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001050 sms_free(sms);
1051 trans->sms.sms = NULL;
1052 trans_free(trans);
1053 }
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +08001054
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +01001055 subscr_put_channel(subscr);
1056 subscr_put(subscr);
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001057}
1058