blob: be6b5b0780a616c2d6f947843027eaa27d6af84b [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);
Harald Weltec0de14d2012-11-23 23:35:01 +0100146 strncpy(sms->src.addr, sms->sender->extension, sizeof(sms->src.addr)-1);
Holger Hans Peter Freytherc4644912011-01-10 15:21:34 +0100147 sms->reply_path_req = 0;
148 sms->status_rep_req = 0;
149 sms->ud_hdr_ind = 0;
150 sms->protocol_id = 0; /* implicit */
Holger Hans Peter Freythercdfcbee2011-01-10 15:34:09 +0100151 sms->data_coding_scheme = dcs;
Harald Weltec0de14d2012-11-23 23:35:01 +0100152 strncpy(sms->dst.addr, receiver->extension, sizeof(sms->dst.addr)-1);
Holger Hans Peter Freytherc4644912011-01-10 15:21:34 +0100153 /* Generate user_data */
154 sms->user_data_len = gsm_7bit_encode(sms->user_data, sms->text);
155
156 return sms;
157}
158
159
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100160static void send_signal(int sig_no,
161 struct gsm_trans *trans,
162 struct gsm_sms *sms,
163 int paging_result)
164{
165 struct sms_signal_data sig;
166 sig.trans = trans;
167 sig.sms = sms;
168 sig.paging_result = paging_result;
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200169 osmo_signal_dispatch(SS_SMS, sig_no, &sig);
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100170}
171
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800172/*
173 * This should be called whenever all SMS to a given subscriber
174 * on a given connection has been sent. This will inform the higher
175 * layers that a channel can be given up.
176 */
177static void gsm411_release_conn(struct gsm_subscriber_connection *conn)
178{
Holger Hans Peter Freytherbddd1522010-06-16 14:02:41 +0800179 if (!conn)
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800180 return;
181
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +0100182 subscr_put_channel(conn->subscr);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800183}
184
Holger Freythera553d092009-01-04 20:16:25 +0000185struct msgb *gsm411_msgb_alloc(void)
Daniel Willmann471712b2008-12-29 01:54:02 +0000186{
Harald Welte966636f2009-06-26 19:39:35 +0200187 return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM,
188 "GSM 04.11");
Daniel Willmann471712b2008-12-29 01:54:02 +0000189}
190
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200191static int gsm411_sendmsg(struct gsm_subscriber_connection *conn, struct msgb *msg, uint8_t link_id)
Daniel Willmann471712b2008-12-29 01:54:02 +0000192{
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100193 DEBUGP(DLSMS, "GSM4.11 TX %s\n", osmo_hexdump(msg->data, msg->len));
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100194 msg->l3h = msg->data;
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100195 return gsm0808_submit_dtap(conn, msg, link_id, 1);
Daniel Willmann471712b2008-12-29 01:54:02 +0000196}
197
Harald Welte41985612009-08-10 00:24:32 +0200198/* SMC TC1* is expired */
199static void cp_timer_expired(void *data)
200{
201 struct gsm_trans *trans = data;
202
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100203 DEBUGP(DLSMS, "SMC Timer TC1* is expired, calling trans_free()\n");
Harald Welte41985612009-08-10 00:24:32 +0200204 /* FIXME: we need to re-transmit the last CP-DATA 1..3 times */
205 trans_free(trans);
206}
207
Harald Welte87f5d632009-07-04 17:39:00 +0200208/* Prefix msg with a 04.08/04.11 CP header */
Harald Weltef3efc592009-07-27 20:11:35 +0200209static int gsm411_cp_sendmsg(struct msgb *msg, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200210 uint8_t msg_type)
Harald Welte87f5d632009-07-04 17:39:00 +0200211{
212 struct gsm48_hdr *gh;
213
214 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
215 /* Outgoing needs the highest bit set */
Harald Weltef3efc592009-07-27 20:11:35 +0200216 gh->proto_discr = trans->protocol | (trans->transaction_id<<4);
Harald Welte87f5d632009-07-04 17:39:00 +0200217 gh->msg_type = msg_type;
218
Harald Weltef3efc592009-07-27 20:11:35 +0200219 /* mobile originating */
220 switch (gh->msg_type) {
221 case GSM411_MT_CP_DATA:
222 /* 5.2.3.1.2: enter MO-wait for CP-ack */
Harald Weltecb8f4432009-08-09 14:59:02 +0200223 /* 5.2.3.2.3: enter MT-wait for CP-ACK */
Harald Weltef3efc592009-07-27 20:11:35 +0200224 trans->sms.cp_state = GSM411_CPS_WAIT_CP_ACK;
Harald Welte41985612009-08-10 00:24:32 +0200225 trans->sms.cp_timer.data = trans;
226 trans->sms.cp_timer.cb = cp_timer_expired;
227 /* 5.3.2.1: Set Timer TC1A */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200228 osmo_timer_schedule(&trans->sms.cp_timer, GSM411_TMR_TC1A);
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100229 DEBUGP(DLSMS, "TX: CP-DATA ");
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200230 break;
231 case GSM411_MT_CP_ACK:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100232 DEBUGP(DLSMS, "TX: CP-ACK ");
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200233 break;
234 case GSM411_MT_CP_ERROR:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100235 DEBUGP(DLSMS, "TX: CP-ERROR ");
Harald Weltef3efc592009-07-27 20:11:35 +0200236 break;
237 }
238
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100239 DEBUGPC(DLSMS, "trans=%x\n", trans->transaction_id);
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200240
Holger Hans Peter Freytherec32b582010-03-23 07:40:46 +0100241 return gsm411_sendmsg(trans->conn, msg, trans->sms.link_id);
Harald Welte87f5d632009-07-04 17:39:00 +0200242}
243
244/* Prefix msg with a RP-DATA header and send as CP-DATA */
Harald Weltef3efc592009-07-27 20:11:35 +0200245static int gsm411_rp_sendmsg(struct msgb *msg, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200246 uint8_t rp_msg_type, uint8_t rp_msg_ref)
Harald Welte87f5d632009-07-04 17:39:00 +0200247{
248 struct gsm411_rp_hdr *rp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200249 uint8_t len = msg->len;
Harald Welte87f5d632009-07-04 17:39:00 +0200250
251 /* GSM 04.11 RP-DATA header */
252 rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp));
Harald Welte0d544e72009-08-10 00:22:19 +0200253 rp->len = len + 2;
Harald Welte87f5d632009-07-04 17:39:00 +0200254 rp->msg_type = rp_msg_type;
255 rp->msg_ref = rp_msg_ref; /* FIXME: Choose randomly */
256
Harald Weltef3efc592009-07-27 20:11:35 +0200257 return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_DATA);
Harald Welte87f5d632009-07-04 17:39:00 +0200258}
259
Harald Welte76042182009-08-08 16:03:15 +0200260static int gsm340_rx_sms_submit(struct msgb *msg, struct gsm_sms *gsms)
Harald Welte7e310b12009-03-30 20:56:32 +0000261{
262 if (db_sms_store(gsms) != 0) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100263 LOGP(DLSMS, LOGL_ERROR, "Failed to store SMS in Database\n");
Harald Welteb9c758b2009-07-05 14:02:46 +0200264 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Harald Welte7e310b12009-03-30 20:56:32 +0000265 }
Harald Welte76042182009-08-08 16:03:15 +0200266 /* dispatch a signal to tell higher level about it */
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100267 send_signal(S_SMS_SUBMITTED, NULL, gsms, 0);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200268
Harald Welte7e310b12009-03-30 20:56:32 +0000269 return 0;
270}
271
Harald Welte76042182009-08-08 16:03:15 +0200272/* generate a TPDU address field compliant with 03.40 sec. 9.1.2.5 */
Holger Hans Peter Freyther366c3312012-11-10 18:24:54 +0100273static int gsm340_gen_oa_sub(uint8_t *oa, unsigned int oa_len,
Harald Weltec0de14d2012-11-23 23:35:01 +0100274 const struct gsm_sms_addr *src)
Harald Weltef3efc592009-07-27 20:11:35 +0200275{
Holger Hans Peter Freyther366c3312012-11-10 18:24:54 +0100276 /* network specific, private numbering plan */
Harald Weltec0de14d2012-11-23 23:35:01 +0100277 return gsm340_gen_oa(oa, oa_len, src->ton, src->npi, src->addr);
Harald Weltef3efc592009-07-27 20:11:35 +0200278}
279
Harald Welte76042182009-08-08 16:03:15 +0200280/* generate a msgb containing a TPDU derived from struct gsm_sms,
281 * returns total size of TPDU */
282static int gsm340_gen_tpdu(struct msgb *msg, struct gsm_sms *sms)
Harald Weltef3efc592009-07-27 20:11:35 +0200283{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200284 uint8_t *smsp;
285 uint8_t oa[12]; /* max len per 03.40 */
286 uint8_t oa_len = 0;
287 uint8_t octet_len;
Harald Welte76042182009-08-08 16:03:15 +0200288 unsigned int old_msg_len = msg->len;
Harald Weltef3efc592009-07-27 20:11:35 +0200289
290 /* generate first octet with masked bits */
291 smsp = msgb_put(msg, 1);
Harald Welte76042182009-08-08 16:03:15 +0200292 /* TP-MTI (message type indicator) */
Harald Weltef3efc592009-07-27 20:11:35 +0200293 *smsp = GSM340_SMS_DELIVER_SC2MS;
Harald Welte76042182009-08-08 16:03:15 +0200294 /* TP-MMS (more messages to send) */
295 if (0 /* FIXME */)
Harald Weltef3efc592009-07-27 20:11:35 +0200296 *smsp |= 0x04;
Harald Welte76042182009-08-08 16:03:15 +0200297 /* TP-SRI(deliver)/SRR(submit) */
Harald Weltef3efc592009-07-27 20:11:35 +0200298 if (sms->status_rep_req)
299 *smsp |= 0x20;
Harald Welte76042182009-08-08 16:03:15 +0200300 /* TP-UDHI (indicating TP-UD contains a header) */
301 if (sms->ud_hdr_ind)
Harald Weltef3efc592009-07-27 20:11:35 +0200302 *smsp |= 0x40;
Harald Weltec0de14d2012-11-23 23:35:01 +0100303
Harald Weltef3efc592009-07-27 20:11:35 +0200304 /* generate originator address */
Harald Weltec0de14d2012-11-23 23:35:01 +0100305 oa_len = gsm340_gen_oa_sub(oa, sizeof(oa), &sms->src);
Harald Weltef3efc592009-07-27 20:11:35 +0200306 smsp = msgb_put(msg, oa_len);
Harald Weltef3efc592009-07-27 20:11:35 +0200307 memcpy(smsp, oa, oa_len);
308
309 /* generate TP-PID */
310 smsp = msgb_put(msg, 1);
311 *smsp = sms->protocol_id;
312
313 /* generate TP-DCS */
314 smsp = msgb_put(msg, 1);
315 *smsp = sms->data_coding_scheme;
316
317 /* generate TP-SCTS */
318 smsp = msgb_put(msg, 7);
319 gsm340_gen_scts(smsp, time(NULL));
Harald Welte76042182009-08-08 16:03:15 +0200320
Harald Weltef3efc592009-07-27 20:11:35 +0200321 /* generate TP-UDL */
322 smsp = msgb_put(msg, 1);
Harald Welte76042182009-08-08 16:03:15 +0200323 *smsp = sms->user_data_len;
Harald Weltef3efc592009-07-27 20:11:35 +0200324
325 /* generate TP-UD */
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200326 switch (gsm338_get_sms_alphabet(sms->data_coding_scheme)) {
327 case DCS_7BIT_DEFAULT:
Daniel Willmann9aef1452009-08-13 03:39:07 +0200328 octet_len = sms->user_data_len*7/8;
329 if (sms->user_data_len*7%8 != 0)
330 octet_len++;
Daniel Willmann6b1e8222009-08-12 21:17:06 +0200331 /* Warning, user_data_len indicates the amount of septets
332 * (characters), we need amount of octets occupied */
Daniel Willmann9aef1452009-08-13 03:39:07 +0200333 smsp = msgb_put(msg, octet_len);
334 memcpy(smsp, sms->user_data, octet_len);
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200335 break;
336 case DCS_UCS2:
337 case DCS_8BIT_DATA:
338 smsp = msgb_put(msg, sms->user_data_len);
339 memcpy(smsp, sms->user_data, sms->user_data_len);
340 break;
341 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100342 LOGP(DLSMS, LOGL_NOTICE, "Unhandled Data Coding Scheme: 0x%02X\n",
Harald Welted0cf7ba2009-12-24 15:08:18 +0100343 sms->data_coding_scheme);
Daniel Willmann6b024cb2009-08-15 03:01:46 +0200344 break;
Daniel Willmann6b1e8222009-08-12 21:17:06 +0200345 }
Harald Weltef3efc592009-07-27 20:11:35 +0200346
Harald Welte76042182009-08-08 16:03:15 +0200347 return msg->len - old_msg_len;
Harald Weltef3efc592009-07-27 20:11:35 +0200348}
349
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200350/* process an incoming TPDU (called from RP-DATA)
351 * return value > 0: RP CAUSE for ERROR; < 0: silent error; 0 = success */
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100352static int gsm340_rx_tpdu(struct gsm_subscriber_connection *conn, struct msgb *msg)
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000353{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200354 uint8_t *smsp = msgb_sms(msg);
Harald Welte7e310b12009-03-30 20:56:32 +0000355 struct gsm_sms *gsms;
Harald Welte258c7132011-07-16 13:34:52 +0200356 unsigned int sms_alphabet;
357 uint8_t sms_mti, sms_mms, sms_vpf, sms_rp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200358 uint8_t *sms_vp;
359 uint8_t da_len_bytes;
360 uint8_t address_lv[12]; /* according to 03.40 / 9.1.2.5 */
Harald Welte7e310b12009-03-30 20:56:32 +0000361 int rc = 0;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000362
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200363 osmo_counter_inc(conn->bts->network->stats.sms.submitted);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100364
Harald Welte76042182009-08-08 16:03:15 +0200365 gsms = sms_alloc();
366 if (!gsms)
Harald Welteb9c758b2009-07-05 14:02:46 +0200367 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Harald Welte7e310b12009-03-30 20:56:32 +0000368
369 /* invert those fields where 0 means active/present */
Harald Welte76042182009-08-08 16:03:15 +0200370 sms_mti = *smsp & 0x03;
371 sms_mms = !!(*smsp & 0x04);
372 sms_vpf = (*smsp & 0x18) >> 3;
373 gsms->status_rep_req = (*smsp & 0x20);
374 gsms->ud_hdr_ind = (*smsp & 0x40);
375 sms_rp = (*smsp & 0x80);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000376
377 smsp++;
Harald Welte76042182009-08-08 16:03:15 +0200378 gsms->msg_ref = *smsp++;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000379
Harald Welte7e310b12009-03-30 20:56:32 +0000380 /* length in bytes of the destination address */
381 da_len_bytes = 2 + *smsp/2 + *smsp%2;
382 if (da_len_bytes > 12) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100383 LOGP(DLSMS, LOGL_ERROR, "Destination Address > 12 bytes ?!?\n");
Harald Welteb9c758b2009-07-05 14:02:46 +0200384 rc = GSM411_RP_CAUSE_SEMANT_INC_MSG;
Harald Welte7e310b12009-03-30 20:56:32 +0000385 goto out;
Harald Weltee07b6a72012-11-23 19:02:37 +0100386 } else if (da_len_bytes < 4) {
387 LOGP(DLSMS, LOGL_ERROR, "Destination Address < 4 bytes ?!?\n");
388 rc = GSM411_RP_CAUSE_SEMANT_INC_MSG;
389 goto out;
Harald Welte7e310b12009-03-30 20:56:32 +0000390 }
Harald Welte3cfdb222009-06-12 02:42:11 +0800391 memset(address_lv, 0, sizeof(address_lv));
Harald Welte7e310b12009-03-30 20:56:32 +0000392 memcpy(address_lv, smsp, da_len_bytes);
393 /* mangle first byte to reflect length in bytes, not digits */
Harald Welte3cfdb222009-06-12 02:42:11 +0800394 address_lv[0] = da_len_bytes - 1;
Harald Weltee07b6a72012-11-23 19:02:37 +0100395
Harald Weltec0de14d2012-11-23 23:35:01 +0100396 gsms->dst.ton = (address_lv[1] >> 4) & 7;
397 gsms->dst.npi = address_lv[1] & 0xF;
Harald Welte7e310b12009-03-30 20:56:32 +0000398 /* convert to real number */
Harald Weltec0de14d2012-11-23 23:35:01 +0100399 gsm48_decode_bcd_number(gsms->dst.addr,
400 sizeof(gsms->dst.addr), address_lv, 1);
Harald Welte7e310b12009-03-30 20:56:32 +0000401 smsp += da_len_bytes;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000402
Harald Welte76042182009-08-08 16:03:15 +0200403 gsms->protocol_id = *smsp++;
404 gsms->data_coding_scheme = *smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000405
Harald Welte76042182009-08-08 16:03:15 +0200406 sms_alphabet = gsm338_get_sms_alphabet(gsms->data_coding_scheme);
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800407 if (sms_alphabet == 0xffffffff) {
408 sms_free(gsms);
Harald Weltee98c6352010-06-09 15:29:13 +0200409 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800410 }
Harald Welte7e310b12009-03-30 20:56:32 +0000411
Harald Welte76042182009-08-08 16:03:15 +0200412 switch (sms_vpf) {
Harald Welte7e310b12009-03-30 20:56:32 +0000413 case GSM340_TP_VPF_RELATIVE:
Harald Welte76042182009-08-08 16:03:15 +0200414 sms_vp = smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000415 break;
416 case GSM340_TP_VPF_ABSOLUTE:
417 case GSM340_TP_VPF_ENHANCED:
Harald Welte76042182009-08-08 16:03:15 +0200418 sms_vp = smsp;
Steffen Neubauerf3262672009-11-26 12:28:41 +0100419 /* the additional functionality indicator... */
Steffen Neubauerac0c13c2009-12-05 12:44:41 +0100420 if (sms_vpf == GSM340_TP_VPF_ENHANCED && *smsp & (1<<7))
421 smsp++;
Harald Welte7e310b12009-03-30 20:56:32 +0000422 smsp += 7;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000423 break;
Daniel Willmann58c83d82009-08-13 03:40:49 +0200424 case GSM340_TP_VPF_NONE:
425 sms_vp = 0;
426 break;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000427 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100428 LOGP(DLSMS, LOGL_NOTICE,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100429 "SMS Validity period not implemented: 0x%02x\n", sms_vpf);
Harald Welte76042182009-08-08 16:03:15 +0200430 return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000431 }
Harald Welte76042182009-08-08 16:03:15 +0200432 gsms->user_data_len = *smsp++;
433 if (gsms->user_data_len) {
434 memcpy(gsms->user_data, smsp, gsms->user_data_len);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000435
Harald Welte76042182009-08-08 16:03:15 +0200436 switch (sms_alphabet) {
Harald Welte7e310b12009-03-30 20:56:32 +0000437 case DCS_7BIT_DEFAULT:
Harald Welte76042182009-08-08 16:03:15 +0200438 gsm_7bit_decode(gsms->text, smsp, gsms->user_data_len);
Harald Welte7e310b12009-03-30 20:56:32 +0000439 break;
440 case DCS_8BIT_DATA:
441 case DCS_UCS2:
442 case DCS_NONE:
Harald Welte7e310b12009-03-30 20:56:32 +0000443 break;
444 }
445 }
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000446
Holger Hans Peter Freyther4f834092010-06-16 14:16:47 +0800447 gsms->sender = subscr_get(conn->subscr);
Harald Welted0cf7ba2009-12-24 15:08:18 +0100448
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100449 LOGP(DLSMS, LOGL_INFO, "RX SMS: Sender: %s, MTI: 0x%02x, VPF: 0x%02x, "
Harald Welted0cf7ba2009-12-24 15:08:18 +0100450 "MR: 0x%02x PID: 0x%02x, DCS: 0x%02x, DA: %s, "
451 "UserDataLength: 0x%02x, UserData: \"%s\"\n",
452 subscr_name(gsms->sender), sms_mti, sms_vpf, gsms->msg_ref,
Harald Weltec0de14d2012-11-23 23:35:01 +0100453 gsms->protocol_id, gsms->data_coding_scheme, gsms->dst.addr,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100454 gsms->user_data_len,
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200455 sms_alphabet == DCS_7BIT_DEFAULT ? gsms->text :
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200456 osmo_hexdump(gsms->user_data, gsms->user_data_len));
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000457
Harald Welte76042182009-08-08 16:03:15 +0200458 gsms->validity_minutes = gsm340_validity_period(sms_vpf, sms_vp);
Harald Welteb9c758b2009-07-05 14:02:46 +0200459
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100460 /* FIXME: This looks very wrong */
461 send_signal(0, NULL, gsms, 0);
Harald Welteb9c758b2009-07-05 14:02:46 +0200462
Harald Welte7e310b12009-03-30 20:56:32 +0000463 /* determine gsms->receiver based on dialled number */
Harald Weltec0de14d2012-11-23 23:35:01 +0100464 gsms->receiver = subscr_get_by_extension(conn->bts->network, gsms->dst.addr);
Harald Welte7e310b12009-03-30 20:56:32 +0000465 if (!gsms->receiver) {
Harald Weltee07b6a72012-11-23 19:02:37 +0100466#ifdef BUILD_SMPP
467 rc = smpp_try_deliver(gsms);
468 if (rc == 1) {
469 rc = 1; /* cause 1: unknown subscriber */
470 osmo_counter_inc(conn->bts->network->stats.sms.no_receiver);
471 } else if (rc < 0) {
472 rc = 21; /* cause 21: short message transfer rejected */
473 /* FIXME: handle the error somehow? */
474 }
475#else
Harald Welte7e310b12009-03-30 20:56:32 +0000476 rc = 1; /* cause 1: unknown subscriber */
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200477 osmo_counter_inc(conn->bts->network->stats.sms.no_receiver);
Harald Weltee07b6a72012-11-23 19:02:37 +0100478#endif
Harald Welte7e310b12009-03-30 20:56:32 +0000479 goto out;
480 }
481
Harald Welte76042182009-08-08 16:03:15 +0200482 switch (sms_mti) {
Harald Welte7e310b12009-03-30 20:56:32 +0000483 case GSM340_SMS_SUBMIT_MS2SC:
484 /* MS is submitting a SMS */
Harald Welte76042182009-08-08 16:03:15 +0200485 rc = gsm340_rx_sms_submit(msg, gsms);
Harald Welte7e310b12009-03-30 20:56:32 +0000486 break;
487 case GSM340_SMS_COMMAND_MS2SC:
488 case GSM340_SMS_DELIVER_REP_MS2SC:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100489 LOGP(DLSMS, LOGL_NOTICE, "Unimplemented MTI 0x%02x\n", sms_mti);
Harald Welteb9c758b2009-07-05 14:02:46 +0200490 rc = GSM411_RP_CAUSE_IE_NOTEXIST;
Harald Welte7e310b12009-03-30 20:56:32 +0000491 break;
492 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100493 LOGP(DLSMS, LOGL_NOTICE, "Undefined MTI 0x%02x\n", sms_mti);
Harald Welteb9c758b2009-07-05 14:02:46 +0200494 rc = GSM411_RP_CAUSE_IE_NOTEXIST;
Harald Welte7e310b12009-03-30 20:56:32 +0000495 break;
496 }
497
Harald Welteb9c758b2009-07-05 14:02:46 +0200498 if (!rc && !gsms->receiver)
499 rc = GSM411_RP_CAUSE_MO_NUM_UNASSIGNED;
500
Harald Welte7e310b12009-03-30 20:56:32 +0000501out:
Harald Welte76042182009-08-08 16:03:15 +0200502 sms_free(gsms);
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000503
Harald Welte7e310b12009-03-30 20:56:32 +0000504 return rc;
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000505}
506
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200507static int gsm411_send_rp_ack(struct gsm_trans *trans, uint8_t msg_ref)
Daniel Willmann471712b2008-12-29 01:54:02 +0000508{
509 struct msgb *msg = gsm411_msgb_alloc();
Daniel Willmann471712b2008-12-29 01:54:02 +0000510
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100511 DEBUGP(DLSMS, "TX: SMS RP ACK\n");
Daniel Willmann471712b2008-12-29 01:54:02 +0000512
Harald Weltef3efc592009-07-27 20:11:35 +0200513 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ACK_MT, msg_ref);
Daniel Willmann471712b2008-12-29 01:54:02 +0000514}
515
Harald Weltef3efc592009-07-27 20:11:35 +0200516static int gsm411_send_rp_error(struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200517 uint8_t msg_ref, uint8_t cause)
Daniel Willmann471712b2008-12-29 01:54:02 +0000518{
519 struct msgb *msg = gsm411_msgb_alloc();
Daniel Willmann471712b2008-12-29 01:54:02 +0000520
Harald Welte7e310b12009-03-30 20:56:32 +0000521 msgb_tv_put(msg, 1, cause);
Daniel Willmann471712b2008-12-29 01:54:02 +0000522
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100523 LOGP(DLSMS, LOGL_NOTICE, "TX: SMS RP ERROR, cause %d (%s)\n", cause,
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200524 get_value_string(rp_cause_strs, cause));
Daniel Willmann471712b2008-12-29 01:54:02 +0000525
Harald Weltef3efc592009-07-27 20:11:35 +0200526 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ERROR_MT, msg_ref);
Daniel Willmann471712b2008-12-29 01:54:02 +0000527}
528
Harald Welte7e310b12009-03-30 20:56:32 +0000529/* Receive a 04.11 TPDU inside RP-DATA / user data */
Harald Weltef3efc592009-07-27 20:11:35 +0200530static int gsm411_rx_rp_ud(struct msgb *msg, struct gsm_trans *trans,
531 struct gsm411_rp_hdr *rph,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200532 uint8_t src_len, uint8_t *src,
533 uint8_t dst_len, uint8_t *dst,
534 uint8_t tpdu_len, uint8_t *tpdu)
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000535{
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000536 int rc = 0;
537
Harald Welte7e310b12009-03-30 20:56:32 +0000538 if (src_len && src)
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100539 LOGP(DLSMS, LOGL_ERROR, "RP-DATA (MO) with SRC ?!?\n");
Harald Welte7e310b12009-03-30 20:56:32 +0000540
541 if (!dst_len || !dst || !tpdu_len || !tpdu) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100542 LOGP(DLSMS, LOGL_ERROR,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100543 "RP-DATA (MO) without DST or TPDU ?!?\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200544 gsm411_send_rp_error(trans, rph->msg_ref,
Harald Welteb9c758b2009-07-05 14:02:46 +0200545 GSM411_RP_CAUSE_INV_MAND_INF);
Harald Welte7e310b12009-03-30 20:56:32 +0000546 return -EIO;
547 }
Harald Weltee9dd9b02010-04-30 14:27:05 +0200548 msg->l4h = tpdu;
Harald Welte7e310b12009-03-30 20:56:32 +0000549
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100550 DEBUGP(DLSMS, "DST(%u,%s)\n", dst_len, osmo_hexdump(dst, dst_len));
Harald Welte7e310b12009-03-30 20:56:32 +0000551
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100552 rc = gsm340_rx_tpdu(trans->conn, msg);
Harald Welte7e310b12009-03-30 20:56:32 +0000553 if (rc == 0)
Harald Weltef3efc592009-07-27 20:11:35 +0200554 return gsm411_send_rp_ack(trans, rph->msg_ref);
Harald Welte7e310b12009-03-30 20:56:32 +0000555 else if (rc > 0)
Harald Weltef3efc592009-07-27 20:11:35 +0200556 return gsm411_send_rp_error(trans, rph->msg_ref, rc);
Harald Welte7e310b12009-03-30 20:56:32 +0000557 else
558 return rc;
559}
560
561/* Receive a 04.11 RP-DATA message in accordance with Section 7.3.1.2 */
Harald Weltef3efc592009-07-27 20:11:35 +0200562static int gsm411_rx_rp_data(struct msgb *msg, struct gsm_trans *trans,
563 struct gsm411_rp_hdr *rph)
Harald Welte7e310b12009-03-30 20:56:32 +0000564{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200565 uint8_t src_len, dst_len, rpud_len;
566 uint8_t *src = NULL, *dst = NULL , *rp_ud = NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000567
568 /* in the MO case, this should always be zero length */
569 src_len = rph->data[0];
570 if (src_len)
571 src = &rph->data[1];
572
573 dst_len = rph->data[1+src_len];
574 if (dst_len)
575 dst = &rph->data[1+src_len+1];
576
577 rpud_len = rph->data[1+src_len+1+dst_len];
578 if (rpud_len)
579 rp_ud = &rph->data[1+src_len+1+dst_len+1];
580
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100581 DEBUGP(DLSMS, "RX_RP-DATA: src_len=%u, dst_len=%u ud_len=%u\n",
Harald Welteb9c758b2009-07-05 14:02:46 +0200582 src_len, dst_len, rpud_len);
Harald Weltef3efc592009-07-27 20:11:35 +0200583 return gsm411_rx_rp_ud(msg, trans, rph, src_len, src, dst_len, dst,
Harald Welte7e310b12009-03-30 20:56:32 +0000584 rpud_len, rp_ud);
585}
586
Harald Weltecb8f4432009-08-09 14:59:02 +0200587/* Receive a 04.11 RP-ACK message (response to RP-DATA from us) */
Harald Weltef3efc592009-07-27 20:11:35 +0200588static int gsm411_rx_rp_ack(struct msgb *msg, struct gsm_trans *trans,
589 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200590{
Harald Welte76042182009-08-08 16:03:15 +0200591 struct gsm_sms *sms = trans->sms.sms;
592
Harald Welteb9c758b2009-07-05 14:02:46 +0200593 /* Acnkowledgement to MT RP_DATA, i.e. the MS confirms it
594 * successfully received a SMS. We can now safely mark it as
595 * transmitted */
596
Harald Weltecb8f4432009-08-09 14:59:02 +0200597 if (!trans->sms.is_mt) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100598 LOGP(DLSMS, LOGL_ERROR, "RX RP-ACK on a MO transfer ?\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200599 return gsm411_send_rp_error(trans, rph->msg_ref,
600 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
601 }
Harald Welte3e0f6172009-07-09 23:52:59 +0200602
Harald Welte76042182009-08-08 16:03:15 +0200603 if (!sms) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100604 LOGP(DLSMS, LOGL_ERROR, "RX RP-ACK but no sms in transaction?!?\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200605 return gsm411_send_rp_error(trans, rph->msg_ref,
606 GSM411_RP_CAUSE_PROTOCOL_ERR);
Harald Welte76042182009-08-08 16:03:15 +0200607 }
608
609 /* mark this SMS as sent in database */
610 db_sms_mark_sent(sms);
611
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100612 send_signal(S_SMS_DELIVERED, trans, sms, 0);
Harald Welte76042182009-08-08 16:03:15 +0200613
614 sms_free(sms);
615 trans->sms.sms = NULL;
616
Harald Weltecf6a3812009-08-09 19:07:41 +0200617 /* check for more messages for this subscriber */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100618 sms = db_sms_get_unsent_for_subscr(trans->subscr);
Harald Weltecf6a3812009-08-09 19:07:41 +0200619 if (sms)
Holger Hans Peter Freyther0fbb3ec2010-06-16 14:19:31 +0800620 gsm411_send_sms(trans->conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800621 else
622 gsm411_release_conn(trans->conn);
Sylvain Munautd6c35f62009-12-24 13:33:51 +0100623
624 /* free the transaction here */
625 trans_free(trans);
Harald Welte76042182009-08-08 16:03:15 +0200626 return 0;
Harald Welteb9c758b2009-07-05 14:02:46 +0200627}
628
Harald Weltef3efc592009-07-27 20:11:35 +0200629static int gsm411_rx_rp_error(struct msgb *msg, struct gsm_trans *trans,
630 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200631{
Holger Hans Peter Freyther18b63f42010-03-23 07:52:17 +0100632 struct gsm_network *net = trans->conn->bts->network;
Harald Welte76042182009-08-08 16:03:15 +0200633 struct gsm_sms *sms = trans->sms.sms;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200634 uint8_t cause_len = rph->data[0];
635 uint8_t cause = rph->data[1];
Harald Weltef3efc592009-07-27 20:11:35 +0200636
Harald Welteb9c758b2009-07-05 14:02:46 +0200637 /* Error in response to MT RP_DATA, i.e. the MS did not
638 * successfully receive the SMS. We need to investigate
639 * the cause and take action depending on it */
640
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100641 LOGP(DLSMS, LOGL_NOTICE, "%s: RX SMS RP-ERROR, cause %d:%d (%s)\n",
Holger Hans Peter Freytherd4c16b12010-03-23 07:56:22 +0100642 subscr_name(trans->conn->subscr), cause_len, cause,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100643 get_value_string(rp_cause_strs, cause));
Harald Weltef3efc592009-07-27 20:11:35 +0200644
Harald Weltecb8f4432009-08-09 14:59:02 +0200645 if (!trans->sms.is_mt) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100646 LOGP(DLSMS, LOGL_ERROR, "RX RP-ERR on a MO transfer ?\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200647#if 0
Harald Weltecb8f4432009-08-09 14:59:02 +0200648 return gsm411_send_rp_error(trans, rph->msg_ref,
649 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200650#endif
Harald Weltecb8f4432009-08-09 14:59:02 +0200651 }
Harald Welte3e0f6172009-07-09 23:52:59 +0200652
Harald Welte76042182009-08-08 16:03:15 +0200653 if (!sms) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100654 LOGP(DLSMS, LOGL_ERROR,
Harald Welted0cf7ba2009-12-24 15:08:18 +0100655 "RX RP-ERR, but no sms in transaction?!?\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200656 return -EINVAL;
657#if 0
Harald Weltecb8f4432009-08-09 14:59:02 +0200658 return gsm411_send_rp_error(trans, rph->msg_ref,
659 GSM411_RP_CAUSE_PROTOCOL_ERR);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200660#endif
Harald Weltecb8f4432009-08-09 14:59:02 +0200661 }
662
663 if (cause == GSM411_RP_CAUSE_MT_MEM_EXCEEDED) {
664 /* MS has not enough memory to store the message. We need
Holger Hans Peter Freytherdd715bd2010-12-23 22:23:15 +0100665 * to store this in our database and wait for a SMMA message */
Harald Weltecb8f4432009-08-09 14:59:02 +0200666 /* FIXME */
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100667 send_signal(S_SMS_MEM_EXCEEDED, trans, sms, 0);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200668 osmo_counter_inc(net->stats.sms.rp_err_mem);
Holger Hans Peter Freyther0eb76662010-12-24 09:47:04 +0100669 } else {
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100670 send_signal(S_SMS_UNKNOWN_ERROR, trans, sms, 0);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200671 osmo_counter_inc(net->stats.sms.rp_err_other);
Holger Hans Peter Freyther0eb76662010-12-24 09:47:04 +0100672 }
Harald Welte76042182009-08-08 16:03:15 +0200673
674 sms_free(sms);
675 trans->sms.sms = NULL;
676
Harald Weltef3efc592009-07-27 20:11:35 +0200677 return 0;
Harald Welteb9c758b2009-07-05 14:02:46 +0200678}
679
Harald Weltef3efc592009-07-27 20:11:35 +0200680static int gsm411_rx_rp_smma(struct msgb *msg, struct gsm_trans *trans,
681 struct gsm411_rp_hdr *rph)
Harald Welteb9c758b2009-07-05 14:02:46 +0200682{
Harald Weltecf6a3812009-08-09 19:07:41 +0200683 struct gsm_sms *sms;
Harald Weltef3efc592009-07-27 20:11:35 +0200684 int rc;
685
Harald Weltecf6a3812009-08-09 19:07:41 +0200686 rc = gsm411_send_rp_ack(trans, rph->msg_ref);
687 trans->sms.rp_state = GSM411_RPS_IDLE;
688
Harald Welteb9c758b2009-07-05 14:02:46 +0200689 /* MS tells us that it has memory for more SMS, we need
690 * to check if we have any pending messages for it and then
691 * transfer those */
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100692 send_signal(S_SMS_SMMA, trans, NULL, 0);
Harald Weltef3efc592009-07-27 20:11:35 +0200693
Harald Weltecf6a3812009-08-09 19:07:41 +0200694 /* check for more messages for this subscriber */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100695 sms = db_sms_get_unsent_for_subscr(trans->subscr);
Harald Weltecf6a3812009-08-09 19:07:41 +0200696 if (sms)
Holger Hans Peter Freyther0fbb3ec2010-06-16 14:19:31 +0800697 gsm411_send_sms(trans->conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +0800698 else
699 gsm411_release_conn(trans->conn);
Harald Weltef3efc592009-07-27 20:11:35 +0200700
701 return rc;
Harald Welteb9c758b2009-07-05 14:02:46 +0200702}
703
Harald Weltef3efc592009-07-27 20:11:35 +0200704static int gsm411_rx_cp_data(struct msgb *msg, struct gsm48_hdr *gh,
705 struct gsm_trans *trans)
Harald Welte7e310b12009-03-30 20:56:32 +0000706{
Daniel Willmann471712b2008-12-29 01:54:02 +0000707 struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200708 uint8_t msg_type = rp_data->msg_type & 0x07;
Harald Welte7e310b12009-03-30 20:56:32 +0000709 int rc = 0;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000710
711 switch (msg_type) {
712 case GSM411_MT_RP_DATA_MO:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100713 DEBUGP(DLSMS, "RX SMS RP-DATA (MO)\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200714 /* start TR2N and enter 'wait to send RP-ACK state' */
715 trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK;
716 rc = gsm411_rx_rp_data(msg, trans, rp_data);
Harald Welte7e310b12009-03-30 20:56:32 +0000717 break;
718 case GSM411_MT_RP_ACK_MO:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100719 DEBUGP(DLSMS,"RX SMS RP-ACK (MO)\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200720 rc = gsm411_rx_rp_ack(msg, trans, rp_data);
Harald Welteb9c758b2009-07-05 14:02:46 +0200721 break;
Harald Welte7e310b12009-03-30 20:56:32 +0000722 case GSM411_MT_RP_SMMA_MO:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100723 DEBUGP(DLSMS, "RX SMS RP-SMMA\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200724 /* start TR2N and enter 'wait to send RP-ACK state' */
725 trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK;
726 rc = gsm411_rx_rp_smma(msg, trans, rp_data);
727 break;
728 case GSM411_MT_RP_ERROR_MO:
729 rc = gsm411_rx_rp_error(msg, trans, rp_data);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000730 break;
731 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100732 LOGP(DLSMS, LOGL_NOTICE, "Invalid RP type 0x%02x\n", msg_type);
Harald Weltef3efc592009-07-27 20:11:35 +0200733 rc = gsm411_send_rp_error(trans, rp_data->msg_ref,
734 GSM411_RP_CAUSE_MSGTYPE_NOTEXIST);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000735 break;
736 }
737
738 return rc;
739}
740
Harald Weltef3efc592009-07-27 20:11:35 +0200741/* send CP-ACK to given transaction */
742static int gsm411_tx_cp_ack(struct gsm_trans *trans)
743{
744 struct msgb *msg = gsm411_msgb_alloc();
Harald Weltecf6a3812009-08-09 19:07:41 +0200745 int rc;
Harald Weltef3efc592009-07-27 20:11:35 +0200746
Harald Weltecf6a3812009-08-09 19:07:41 +0200747 rc = gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ACK);
748
749 if (trans->sms.is_mt) {
750 /* If this is a MT SMS DELIVER, we can clear transaction here */
751 trans->sms.cp_state = GSM411_CPS_IDLE;
Harald Welte (local)86b17172009-08-14 14:52:17 +0200752 //trans_free(trans);
Harald Weltecf6a3812009-08-09 19:07:41 +0200753 }
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200754
755 return rc;
Harald Weltef3efc592009-07-27 20:11:35 +0200756}
757
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200758static int gsm411_tx_cp_error(struct gsm_trans *trans, uint8_t cause)
Harald Weltef3efc592009-07-27 20:11:35 +0200759{
760 struct msgb *msg = gsm411_msgb_alloc();
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200761 uint8_t *causep;
Harald Weltef3efc592009-07-27 20:11:35 +0200762
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100763 LOGP(DLSMS, LOGL_NOTICE, "TX CP-ERROR, cause %d (%s)\n", cause,
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200764 get_value_string(cp_cause_strs, cause));
765
Harald Welte76042182009-08-08 16:03:15 +0200766 causep = msgb_put(msg, 1);
Harald Weltef3efc592009-07-27 20:11:35 +0200767 *causep = cause;
768
769 return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ERROR);
770}
771
772/* Entry point for incoming GSM48_PDISC_SMS from abis_rsl.c */
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800773int gsm0411_rcv_sms(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800774 struct msgb *msg)
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000775{
776 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200777 uint8_t msg_type = gh->msg_type;
778 uint8_t transaction_id = ((gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Weltef3efc592009-07-27 20:11:35 +0200779 struct gsm_trans *trans;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000780 int rc = 0;
781
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800782 if (!conn->subscr)
Harald Weltef3efc592009-07-27 20:11:35 +0200783 return -EIO;
784 /* FIXME: send some error message */
785
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100786 DEBUGP(DLSMS, "trans_id=%x ", transaction_id);
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800787 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_SMS,
Harald Weltef3efc592009-07-27 20:11:35 +0200788 transaction_id);
789 if (!trans) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100790 DEBUGPC(DLSMS, "(new) ");
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800791 trans = trans_alloc(conn->subscr, GSM48_PDISC_SMS,
Harald Weltef3efc592009-07-27 20:11:35 +0200792 transaction_id, new_callref++);
793 if (!trans) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100794 DEBUGPC(DLSMS, "No memory for trans\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200795 /* FIXME: send some error message */
796 return -ENOMEM;
797 }
798 trans->sms.cp_state = GSM411_CPS_IDLE;
799 trans->sms.rp_state = GSM411_RPS_IDLE;
800 trans->sms.is_mt = 0;
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800801 trans->sms.link_id = UM_SAPI_SMS;
Harald Weltef3efc592009-07-27 20:11:35 +0200802
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800803 trans->conn = conn;
Harald Weltef3efc592009-07-27 20:11:35 +0200804 }
805
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000806 switch(msg_type) {
807 case GSM411_MT_CP_DATA:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100808 DEBUGPC(DLSMS, "RX SMS CP-DATA\n");
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100809
810 /* 5.4: For MO, if a CP-DATA is received for a new
811 * transaction, equals reception of an implicit
812 * last CP-ACK for previous transaction */
813 if (trans->sms.cp_state == GSM411_CPS_IDLE) {
814 int i;
815 struct gsm_trans *ptrans;
816
817 /* Scan through all remote initiated transactions */
818 for (i=8; i<15; i++) {
819 if (i == transaction_id)
820 continue;
821
Holger Hans Peter Freyther57c4dec2010-06-16 14:24:55 +0800822 ptrans = trans_find_by_id(conn->subscr,
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100823 GSM48_PDISC_SMS, i);
824 if (!ptrans)
825 continue;
826
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100827 DEBUGP(DLSMS, "Implicit CP-ACK for trans_id=%x\n", i);
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100828
829 /* Finish it for good */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200830 osmo_timer_del(&ptrans->sms.cp_timer);
Sylvain Munaut258e2f92009-12-24 16:47:08 +0100831 ptrans->sms.cp_state = GSM411_CPS_IDLE;
832 trans_free(ptrans);
833 }
834 }
835
Harald Weltecb8f4432009-08-09 14:59:02 +0200836 /* 5.2.3.1.3: MO state exists when SMC has received
837 * CP-DATA, including sending of the assoc. CP-ACK */
838 /* 5.2.3.2.4: MT state exists when SMC has received
839 * CP-DATA, including sending of the assoc. CP-ACK */
840 trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED;
Harald Weltef3efc592009-07-27 20:11:35 +0200841
Harald Welte (local)daef6062009-08-14 11:41:12 +0200842 /* SMC instance acknowledges the CP-DATA frame */
843 gsm411_tx_cp_ack(trans);
844
Harald Weltef3efc592009-07-27 20:11:35 +0200845 rc = gsm411_rx_cp_data(msg, gh, trans);
Harald Welte (local)daef6062009-08-14 11:41:12 +0200846#if 0
Harald Weltef3efc592009-07-27 20:11:35 +0200847 /* Send CP-ACK or CP-ERORR in response */
848 if (rc < 0) {
849 rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_NET_FAIL);
850 } else
851 rc = gsm411_tx_cp_ack(trans);
Harald Welte (local)daef6062009-08-14 11:41:12 +0200852#endif
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000853 break;
854 case GSM411_MT_CP_ACK:
Harald Weltef3efc592009-07-27 20:11:35 +0200855 /* previous CP-DATA in this transaction was confirmed */
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100856 DEBUGPC(DLSMS, "RX SMS CP-ACK\n");
Harald Weltecb8f4432009-08-09 14:59:02 +0200857 /* 5.2.3.1.3: MO state exists when SMC has received CP-ACK */
858 /* 5.2.3.2.4: MT state exists when SMC has received CP-ACK */
859 trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED;
Harald Welte41985612009-08-10 00:24:32 +0200860 /* Stop TC1* after CP-ACK has been received */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200861 osmo_timer_del(&trans->sms.cp_timer);
Harald Weltecb8f4432009-08-09 14:59:02 +0200862
Harald Weltef3efc592009-07-27 20:11:35 +0200863 if (!trans->sms.is_mt) {
Holger Hans Peter Freyther0afa0442010-06-10 18:08:12 +0800864 /* FIXME: we have sent one CP-DATA, which was now
Harald Weltef3efc592009-07-27 20:11:35 +0200865 * acknowledged. Check if we want to transfer more,
866 * i.e. multi-part message */
867 trans->sms.cp_state = GSM411_CPS_IDLE;
868 trans_free(trans);
869 }
Daniel Willmannbb16e8e2008-12-29 03:53:50 +0000870 break;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000871 case GSM411_MT_CP_ERROR:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100872 DEBUGPC(DLSMS, "RX SMS CP-ERROR, cause %d (%s)\n", gh->data[0],
Harald Welte (local)c89a5112009-08-14 10:42:43 +0200873 get_value_string(cp_cause_strs, gh->data[0]));
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200874 osmo_timer_del(&trans->sms.cp_timer);
Harald Weltef3efc592009-07-27 20:11:35 +0200875 trans->sms.cp_state = GSM411_CPS_IDLE;
876 trans_free(trans);
Daniel Willmannbb16e8e2008-12-29 03:53:50 +0000877 break;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000878 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100879 DEBUGPC(DLSMS, "RX Unimplemented CP msg_type: 0x%02x\n", msg_type);
Harald Weltef3efc592009-07-27 20:11:35 +0200880 rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_MSGTYPE_NOTEXIST);
Harald Weltecb8f4432009-08-09 14:59:02 +0200881 trans->sms.cp_state = GSM411_CPS_IDLE;
Harald Weltef3efc592009-07-27 20:11:35 +0200882 trans_free(trans);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000883 break;
884 }
885
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000886 return rc;
887}
888
Harald Weltecf6a3812009-08-09 19:07:41 +0200889/* Take a SMS in gsm_sms structure and send it through an already
890 * existing lchan. We also assume that the caller ensured this lchan already
891 * has a SAPI3 RLL connection! */
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100892int gsm411_send_sms(struct gsm_subscriber_connection *conn, struct gsm_sms *sms)
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000893{
894 struct msgb *msg = gsm411_msgb_alloc();
Harald Weltef3efc592009-07-27 20:11:35 +0200895 struct gsm_trans *trans;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200896 uint8_t *data, *rp_ud_len;
897 uint8_t msg_ref = 42;
Sylvain Munautc4052cf2009-12-24 13:27:36 +0100898 int transaction_id;
Harald Welte76042182009-08-08 16:03:15 +0200899 int rc;
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000900
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100901 transaction_id = trans_assign_trans_id(conn->subscr, GSM48_PDISC_SMS, 0);
Harald Welte49839212009-12-22 13:45:58 +0100902 if (transaction_id == -1) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100903 LOGP(DLSMS, LOGL_ERROR, "No available transaction ids\n");
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100904 send_signal(S_SMS_UNKNOWN_ERROR, NULL, sms, 0);
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800905 sms_free(sms);
Harald Welte49839212009-12-22 13:45:58 +0100906 return -EBUSY;
907 }
Harald Weltecf6a3812009-08-09 19:07:41 +0200908
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100909 DEBUGP(DLSMS, "send_sms_lchan()\n");
Harald Weltef3efc592009-07-27 20:11:35 +0200910
Harald Welte76042182009-08-08 16:03:15 +0200911 /* FIXME: allocate transaction with message reference */
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100912 trans = trans_alloc(conn->subscr, GSM48_PDISC_SMS,
Harald Welte76042182009-08-08 16:03:15 +0200913 transaction_id, new_callref++);
914 if (!trans) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100915 LOGP(DLSMS, LOGL_ERROR, "No memory for trans\n");
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100916 send_signal(S_SMS_UNKNOWN_ERROR, NULL, sms, 0);
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800917 sms_free(sms);
Harald Welte76042182009-08-08 16:03:15 +0200918 /* FIXME: send some error message */
919 return -ENOMEM;
920 }
921 trans->sms.cp_state = GSM411_CPS_IDLE;
922 trans->sms.rp_state = GSM411_RPS_IDLE;
923 trans->sms.is_mt = 1;
924 trans->sms.sms = sms;
Harald Welte (local)daef6062009-08-14 11:41:12 +0200925 trans->sms.link_id = UM_SAPI_SMS; /* FIXME: main or SACCH ? */
Harald Welte76042182009-08-08 16:03:15 +0200926
Holger Hans Peter Freyther5179c8e2010-03-23 07:32:23 +0100927 trans->conn = conn;
Harald Welte76042182009-08-08 16:03:15 +0200928
929 /* Hardcode SMSC Originating Address for now */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200930 data = (uint8_t *)msgb_put(msg, 8);
Harald Welte87f5d632009-07-04 17:39:00 +0200931 data[0] = 0x07; /* originator length == 7 */
Harald Welteb9c758b2009-07-05 14:02:46 +0200932 data[1] = 0x91; /* type of number: international, ISDN */
933 data[2] = 0x44; /* 447785016005 */
Daniel Willmanna3e29842008-12-29 16:03:54 +0000934 data[3] = 0x77;
935 data[4] = 0x58;
936 data[5] = 0x10;
937 data[6] = 0x06;
938 data[7] = 0x50;
Harald Welte87f5d632009-07-04 17:39:00 +0200939
940 /* Hardcoded Destination Address */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200941 data = (uint8_t *)msgb_put(msg, 1);
Harald Welte87f5d632009-07-04 17:39:00 +0200942 data[0] = 0; /* destination length == 0 */
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000943
Harald Welte76042182009-08-08 16:03:15 +0200944 /* obtain a pointer for the rp_ud_len, so we can fill it later */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200945 rp_ud_len = (uint8_t *)msgb_put(msg, 1);
Daniel Willmann4a1e8792008-12-29 06:23:56 +0000946
Harald Welte76042182009-08-08 16:03:15 +0200947 /* generate the 03.40 TPDU */
948 rc = gsm340_gen_tpdu(msg, sms);
949 if (rc < 0) {
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100950 send_signal(S_SMS_UNKNOWN_ERROR, trans, sms, 0);
Holger Hans Peter Freyther8e78fa72010-06-14 16:13:16 +0800951 trans_free(trans);
952 sms_free(sms);
Harald Welte76042182009-08-08 16:03:15 +0200953 msgb_free(msg);
954 return rc;
955 }
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000956
Harald Welte76042182009-08-08 16:03:15 +0200957 *rp_ud_len = rc;
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000958
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100959 DEBUGP(DLSMS, "TX: SMS DELIVER\n");
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000960
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200961 osmo_counter_inc(conn->bts->network->stats.sms.delivered);
Nico Golde1e5ed4e2010-09-16 17:50:29 +0200962 db_sms_inc_deliver_attempts(trans->sms.sms);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100963
Harald Weltef3efc592009-07-27 20:11:35 +0200964 return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_DATA_MT, msg_ref);
965 /* FIXME: enter 'wait for RP-ACK' state, start TR1N */
Daniel Willmann6fe997e2008-12-29 04:20:41 +0000966}
Harald Weltef3efc592009-07-27 20:11:35 +0200967
Harald Weltecf6a3812009-08-09 19:07:41 +0200968/* paging callback. Here we get called if paging a subscriber has
969 * succeeded or failed. */
Harald Welte76042182009-08-08 16:03:15 +0200970static int paging_cb_send_sms(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +0800971 struct msgb *msg, void *_conn, void *_sms)
Harald Weltef3efc592009-07-27 20:11:35 +0200972{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +0800973 struct gsm_subscriber_connection *conn = _conn;
Harald Welte76042182009-08-08 16:03:15 +0200974 struct gsm_sms *sms = _sms;
Holger Hans Peter Freyther4cd51582010-06-14 16:03:03 +0800975 int rc = 0;
Harald Weltef3efc592009-07-27 20:11:35 +0200976
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100977 DEBUGP(DLSMS, "paging_cb_send_sms(hooknum=%u, event=%u, msg=%p,"
Holger Hans Peter Freyther52ffb922010-12-29 14:02:34 +0100978 "conn=%p, sms=%p/id: %llu)\n", hooknum, event, msg, conn, sms, sms->id);
Harald Welte76042182009-08-08 16:03:15 +0200979
980 if (hooknum != GSM_HOOK_RR_PAGING)
981 return -EINVAL;
982
983 switch (event) {
984 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +0800985 gsm411_send_sms(conn, sms);
Harald Welte76042182009-08-08 16:03:15 +0200986 break;
987 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherca5702a2010-06-10 18:20:54 +0800988 case GSM_PAGING_OOM:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100989 case GSM_PAGING_BUSY:
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100990 send_signal(S_SMS_UNKNOWN_ERROR, NULL, sms, event);
Harald Welte76042182009-08-08 16:03:15 +0200991 sms_free(sms);
992 rc = -ETIMEDOUT;
993 break;
Holger Hans Peter Freyther52ffb922010-12-29 14:02:34 +0100994 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100995 LOGP(DLSMS, LOGL_ERROR, "Unhandled paging event: %d\n", event);
Harald Welte76042182009-08-08 16:03:15 +0200996 }
997
998 return rc;
999}
1000
Harald Weltecf6a3812009-08-09 19:07:41 +02001001/* high-level function to send a SMS to a given subscriber. The function
1002 * will take care of paging the subscriber, establishing the RLL SAPI3
1003 * connection, etc. */
Harald Welte76042182009-08-08 16:03:15 +02001004int gsm411_send_sms_subscr(struct gsm_subscriber *subscr,
1005 struct gsm_sms *sms)
1006{
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001007 struct gsm_subscriber_connection *conn;
Harald Weltecf6a3812009-08-09 19:07:41 +02001008
Harald Welte76042182009-08-08 16:03:15 +02001009 /* check if we already have an open lchan to the subscriber.
1010 * if yes, send the SMS this way */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08001011 conn = connection_for_subscr(subscr);
1012 if (conn) {
Holger Hans Peter Freyther0fbb3ec2010-06-16 14:19:31 +08001013 return gsm411_send_sms(conn, sms);
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +08001014 }
Harald Welte76042182009-08-08 16:03:15 +02001015
1016 /* if not, we have to start paging */
Holger Hans Peter Freyther7afac582010-06-10 18:21:25 +08001017 subscr_get_channel(subscr, RSL_CHANNEED_SDCCH, paging_cb_send_sms, sms);
Harald Welte76042182009-08-08 16:03:15 +02001018 return 0;
1019}
Harald Welte3366a942009-07-28 00:44:49 +02001020
Harald Welte (local)86b17172009-08-14 14:52:17 +02001021void _gsm411_sms_trans_free(struct gsm_trans *trans)
1022{
Holger Hans Peter Freythercea722b2010-06-15 12:29:37 +08001023 if (trans->sms.sms) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +01001024 LOGP(DLSMS, LOGL_ERROR, "Transaction contains SMS.\n");
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +01001025 send_signal(S_SMS_UNKNOWN_ERROR, trans, trans->sms.sms, 0);
Holger Hans Peter Freythercea722b2010-06-15 12:29:37 +08001026 sms_free(trans->sms.sms);
1027 trans->sms.sms = NULL;
1028 }
1029
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001030 osmo_timer_del(&trans->sms.cp_timer);
Harald Welte (local)86b17172009-08-14 14:52:17 +02001031}
1032
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001033void gsm411_sapi_n_reject(struct gsm_subscriber_connection *conn)
1034{
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +01001035 struct gsm_subscriber *subscr;
Holger Hans Peter Freyther79eff7e2010-12-27 23:25:53 +01001036 struct gsm_network *net;
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001037 struct gsm_trans *trans, *tmp;
1038
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +01001039 subscr = subscr_get(conn->subscr);
Holger Hans Peter Freyther79eff7e2010-12-27 23:25:53 +01001040 net = conn->bts->network;
1041
1042 llist_for_each_entry_safe(trans, tmp, &net->trans_list, entry)
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001043 if (trans->conn == conn) {
1044 struct gsm_sms *sms = trans->sms.sms;
1045 if (!sms) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +01001046 LOGP(DLSMS, LOGL_ERROR, "SAPI Reject but no SMS.\n");
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001047 continue;
1048 }
1049
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +01001050 send_signal(S_SMS_UNKNOWN_ERROR, trans, sms, 0);
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001051 sms_free(sms);
1052 trans->sms.sms = NULL;
1053 trans_free(trans);
1054 }
Holger Hans Peter Freytherecd99332010-06-15 12:53:08 +08001055
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +01001056 subscr_put_channel(subscr);
1057 subscr_put(subscr);
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +08001058}
1059