Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 1 | /* 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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 6 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 7517212 | 2012-11-10 18:11:22 +0100 | [diff] [blame] | 7 | * (C) 2010-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Hans Peter Freyther | 85531cc | 2010-10-06 20:37:09 +0800 | [diff] [blame] | 8 | * (C) 2010 by On-Waves |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 9 | * |
| 10 | * All Rights Reserved |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 13 | * 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 Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 20 | * GNU Affero General Public License for more details. |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 21 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 22 | * 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 Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 24 | * |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <errno.h> |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 32 | #include <time.h> |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 33 | #include <netinet/in.h> |
| 34 | |
Harald Welte | 9c3dc90 | 2012-04-08 16:59:24 +0200 | [diff] [blame] | 35 | #include "bscconfig.h" |
| 36 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 37 | #include <osmocom/core/msgb.h> |
Holger Hans Peter Freyther | 7517212 | 2012-11-10 18:11:22 +0100 | [diff] [blame] | 38 | #include <osmocom/core/talloc.h> |
| 39 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 40 | #include <osmocom/gsm/tlv.h> |
Holger Hans Peter Freyther | 7517212 | 2012-11-10 18:11:22 +0100 | [diff] [blame] | 41 | #include <osmocom/gsm/gsm_utils.h> |
Holger Hans Peter Freyther | 366c331 | 2012-11-10 18:24:54 +0100 | [diff] [blame] | 42 | #include <osmocom/gsm/gsm0411_utils.h> |
Holger Hans Peter Freyther | 7517212 | 2012-11-10 18:11:22 +0100 | [diff] [blame] | 43 | |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 44 | #include <openbsc/debug.h> |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 45 | #include <openbsc/gsm_data.h> |
Nico Golde | f11af07 | 2010-09-15 19:49:55 +0200 | [diff] [blame] | 46 | #include <openbsc/db.h> |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 47 | #include <openbsc/gsm_subscriber.h> |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 48 | #include <openbsc/gsm_04_11.h> |
| 49 | #include <openbsc/gsm_04_08.h> |
| 50 | #include <openbsc/abis_rsl.h> |
Holger Freyther | 9b17776 | 2009-02-16 19:07:18 +0000 | [diff] [blame] | 51 | #include <openbsc/signal.h> |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 52 | #include <openbsc/db.h> |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 53 | #include <openbsc/transaction.h> |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 54 | #include <openbsc/paging.h> |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 55 | #include <openbsc/bsc_rll.h> |
Holger Hans Peter Freyther | 34e9749 | 2009-08-10 07:54:02 +0200 | [diff] [blame] | 56 | #include <openbsc/chan_alloc.h> |
Holger Hans Peter Freyther | cbac76e | 2010-05-23 21:05:18 +0800 | [diff] [blame] | 57 | #include <openbsc/bsc_api.h> |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 58 | |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 59 | #ifdef BUILD_SMPP |
| 60 | #include "smpp_smsc.h" |
| 61 | extern int smpp_try_deliver(struct gsm_sms *sms); |
| 62 | #endif |
| 63 | |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 64 | #define GSM411_ALLOC_SIZE 1024 |
| 65 | #define GSM411_ALLOC_HEADROOM 128 |
| 66 | |
Harald Welte (local) | d19e58b | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 67 | void *tall_gsms_ctx; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 68 | static uint32_t new_callref = 0x40000001; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 69 | |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 70 | static 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 Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 77 | { GSM411_CP_CAUSE_MSG_INCOMP_STATE, |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 78 | "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 | |
| 84 | static 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 Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 113 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 114 | struct gsm_sms *sms_alloc(void) |
| 115 | { |
| 116 | return talloc_zero(tall_gsms_ctx, struct gsm_sms); |
| 117 | } |
| 118 | |
| 119 | void 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 Welte | 6c7680d | 2012-11-16 22:15:22 +0100 | [diff] [blame] | 126 | #ifdef BUILD_SMPP |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 127 | if (sms->smpp.esme) |
| 128 | smpp_esme_put(sms->smpp.esme); |
Harald Welte | 6c7680d | 2012-11-16 22:15:22 +0100 | [diff] [blame] | 129 | #endif |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 130 | |
| 131 | talloc_free(sms); |
| 132 | } |
| 133 | |
Holger Hans Peter Freyther | cdfcbee | 2011-01-10 15:34:09 +0100 | [diff] [blame] | 134 | struct gsm_sms *sms_from_text(struct gsm_subscriber *receiver, int dcs, const char *text) |
Holger Hans Peter Freyther | c464491 | 2011-01-10 15:21:34 +0100 | [diff] [blame] | 135 | { |
| 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 Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 146 | strncpy(sms->src.addr, sms->sender->extension, sizeof(sms->src.addr)-1); |
Holger Hans Peter Freyther | c464491 | 2011-01-10 15:21:34 +0100 | [diff] [blame] | 147 | 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 Freyther | cdfcbee | 2011-01-10 15:34:09 +0100 | [diff] [blame] | 151 | sms->data_coding_scheme = dcs; |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 152 | strncpy(sms->dst.addr, receiver->extension, sizeof(sms->dst.addr)-1); |
Holger Hans Peter Freyther | c464491 | 2011-01-10 15:21:34 +0100 | [diff] [blame] | 153 | /* 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 Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 160 | static 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 Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 169 | osmo_signal_dispatch(SS_SMS, sig_no, &sig); |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 172 | /* |
| 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 | */ |
| 177 | static void gsm411_release_conn(struct gsm_subscriber_connection *conn) |
| 178 | { |
Holger Hans Peter Freyther | bddd152 | 2010-06-16 14:02:41 +0800 | [diff] [blame] | 179 | if (!conn) |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 180 | return; |
| 181 | |
Holger Hans Peter Freyther | 725966d | 2010-12-27 22:43:28 +0100 | [diff] [blame] | 182 | subscr_put_channel(conn->subscr); |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 183 | } |
| 184 | |
Holger Freyther | a553d09 | 2009-01-04 20:16:25 +0000 | [diff] [blame] | 185 | struct msgb *gsm411_msgb_alloc(void) |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 186 | { |
Harald Welte | 966636f | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 187 | return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM, |
| 188 | "GSM 04.11"); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 191 | static int gsm411_sendmsg(struct gsm_subscriber_connection *conn, struct msgb *msg, uint8_t link_id) |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 192 | { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 193 | DEBUGP(DLSMS, "GSM4.11 TX %s\n", osmo_hexdump(msg->data, msg->len)); |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 194 | msg->l3h = msg->data; |
Holger Hans Peter Freyther | 8d380dc | 2010-11-10 10:16:02 +0100 | [diff] [blame] | 195 | return gsm0808_submit_dtap(conn, msg, link_id, 1); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Harald Welte | 4198561 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 198 | /* SMC TC1* is expired */ |
| 199 | static void cp_timer_expired(void *data) |
| 200 | { |
| 201 | struct gsm_trans *trans = data; |
| 202 | |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 203 | DEBUGP(DLSMS, "SMC Timer TC1* is expired, calling trans_free()\n"); |
Harald Welte | 4198561 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 204 | /* FIXME: we need to re-transmit the last CP-DATA 1..3 times */ |
| 205 | trans_free(trans); |
| 206 | } |
| 207 | |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 208 | /* Prefix msg with a 04.08/04.11 CP header */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 209 | static int gsm411_cp_sendmsg(struct msgb *msg, struct gsm_trans *trans, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 210 | uint8_t msg_type) |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 211 | { |
| 212 | struct gsm48_hdr *gh; |
| 213 | |
| 214 | gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh)); |
| 215 | /* Outgoing needs the highest bit set */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 216 | gh->proto_discr = trans->protocol | (trans->transaction_id<<4); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 217 | gh->msg_type = msg_type; |
| 218 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 219 | /* 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 Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 223 | /* 5.2.3.2.3: enter MT-wait for CP-ACK */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 224 | trans->sms.cp_state = GSM411_CPS_WAIT_CP_ACK; |
Harald Welte | 4198561 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 225 | 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 Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 228 | osmo_timer_schedule(&trans->sms.cp_timer, GSM411_TMR_TC1A); |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 229 | DEBUGP(DLSMS, "TX: CP-DATA "); |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 230 | break; |
| 231 | case GSM411_MT_CP_ACK: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 232 | DEBUGP(DLSMS, "TX: CP-ACK "); |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 233 | break; |
| 234 | case GSM411_MT_CP_ERROR: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 235 | DEBUGP(DLSMS, "TX: CP-ERROR "); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 236 | break; |
| 237 | } |
| 238 | |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 239 | DEBUGPC(DLSMS, "trans=%x\n", trans->transaction_id); |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 240 | |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 241 | return gsm411_sendmsg(trans->conn, msg, trans->sms.link_id); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* Prefix msg with a RP-DATA header and send as CP-DATA */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 245 | static int gsm411_rp_sendmsg(struct msgb *msg, struct gsm_trans *trans, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 246 | uint8_t rp_msg_type, uint8_t rp_msg_ref) |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 247 | { |
| 248 | struct gsm411_rp_hdr *rp; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 249 | uint8_t len = msg->len; |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 250 | |
| 251 | /* GSM 04.11 RP-DATA header */ |
| 252 | rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp)); |
Harald Welte | 0d544e7 | 2009-08-10 00:22:19 +0200 | [diff] [blame] | 253 | rp->len = len + 2; |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 254 | rp->msg_type = rp_msg_type; |
| 255 | rp->msg_ref = rp_msg_ref; /* FIXME: Choose randomly */ |
| 256 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 257 | return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_DATA); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 260 | static int gsm340_rx_sms_submit(struct msgb *msg, struct gsm_sms *gsms) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 261 | { |
| 262 | if (db_sms_store(gsms) != 0) { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 263 | LOGP(DLSMS, LOGL_ERROR, "Failed to store SMS in Database\n"); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 264 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 265 | } |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 266 | /* dispatch a signal to tell higher level about it */ |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 267 | send_signal(S_SMS_SUBMITTED, NULL, gsms, 0); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 268 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 272 | /* generate a TPDU address field compliant with 03.40 sec. 9.1.2.5 */ |
Holger Hans Peter Freyther | 366c331 | 2012-11-10 18:24:54 +0100 | [diff] [blame] | 273 | static int gsm340_gen_oa_sub(uint8_t *oa, unsigned int oa_len, |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 274 | const struct gsm_sms_addr *src) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 275 | { |
Holger Hans Peter Freyther | 366c331 | 2012-11-10 18:24:54 +0100 | [diff] [blame] | 276 | /* network specific, private numbering plan */ |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 277 | return gsm340_gen_oa(oa, oa_len, src->ton, src->npi, src->addr); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 280 | /* generate a msgb containing a TPDU derived from struct gsm_sms, |
| 281 | * returns total size of TPDU */ |
| 282 | static int gsm340_gen_tpdu(struct msgb *msg, struct gsm_sms *sms) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 283 | { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 284 | 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 Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 288 | unsigned int old_msg_len = msg->len; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 289 | |
| 290 | /* generate first octet with masked bits */ |
| 291 | smsp = msgb_put(msg, 1); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 292 | /* TP-MTI (message type indicator) */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 293 | *smsp = GSM340_SMS_DELIVER_SC2MS; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 294 | /* TP-MMS (more messages to send) */ |
| 295 | if (0 /* FIXME */) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 296 | *smsp |= 0x04; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 297 | /* TP-SRI(deliver)/SRR(submit) */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 298 | if (sms->status_rep_req) |
| 299 | *smsp |= 0x20; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 300 | /* TP-UDHI (indicating TP-UD contains a header) */ |
| 301 | if (sms->ud_hdr_ind) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 302 | *smsp |= 0x40; |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 303 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 304 | /* generate originator address */ |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 305 | oa_len = gsm340_gen_oa_sub(oa, sizeof(oa), &sms->src); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 306 | smsp = msgb_put(msg, oa_len); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 307 | 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 Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 320 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 321 | /* generate TP-UDL */ |
| 322 | smsp = msgb_put(msg, 1); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 323 | *smsp = sms->user_data_len; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 324 | |
| 325 | /* generate TP-UD */ |
Daniel Willmann | 6b024cb | 2009-08-15 03:01:46 +0200 | [diff] [blame] | 326 | switch (gsm338_get_sms_alphabet(sms->data_coding_scheme)) { |
| 327 | case DCS_7BIT_DEFAULT: |
Daniel Willmann | 9aef145 | 2009-08-13 03:39:07 +0200 | [diff] [blame] | 328 | octet_len = sms->user_data_len*7/8; |
| 329 | if (sms->user_data_len*7%8 != 0) |
| 330 | octet_len++; |
Daniel Willmann | 6b1e822 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 331 | /* Warning, user_data_len indicates the amount of septets |
| 332 | * (characters), we need amount of octets occupied */ |
Daniel Willmann | 9aef145 | 2009-08-13 03:39:07 +0200 | [diff] [blame] | 333 | smsp = msgb_put(msg, octet_len); |
| 334 | memcpy(smsp, sms->user_data, octet_len); |
Daniel Willmann | 6b024cb | 2009-08-15 03:01:46 +0200 | [diff] [blame] | 335 | 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 Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 342 | LOGP(DLSMS, LOGL_NOTICE, "Unhandled Data Coding Scheme: 0x%02X\n", |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 343 | sms->data_coding_scheme); |
Daniel Willmann | 6b024cb | 2009-08-15 03:01:46 +0200 | [diff] [blame] | 344 | break; |
Daniel Willmann | 6b1e822 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 345 | } |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 346 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 347 | return msg->len - old_msg_len; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 350 | /* process an incoming TPDU (called from RP-DATA) |
| 351 | * return value > 0: RP CAUSE for ERROR; < 0: silent error; 0 = success */ |
Holger Hans Peter Freyther | 18b63f4 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 352 | static int gsm340_rx_tpdu(struct gsm_subscriber_connection *conn, struct msgb *msg) |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 353 | { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 354 | uint8_t *smsp = msgb_sms(msg); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 355 | struct gsm_sms *gsms; |
Harald Welte | 258c713 | 2011-07-16 13:34:52 +0200 | [diff] [blame] | 356 | unsigned int sms_alphabet; |
| 357 | uint8_t sms_mti, sms_mms, sms_vpf, sms_rp; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 358 | 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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 361 | int rc = 0; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 362 | |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 363 | osmo_counter_inc(conn->bts->network->stats.sms.submitted); |
Harald Welte | 24ff6ee | 2009-12-22 00:41:05 +0100 | [diff] [blame] | 364 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 365 | gsms = sms_alloc(); |
| 366 | if (!gsms) |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 367 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 368 | |
| 369 | /* invert those fields where 0 means active/present */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 370 | 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 Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 376 | |
| 377 | smsp++; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 378 | gsms->msg_ref = *smsp++; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 379 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 380 | /* 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 Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 383 | LOGP(DLSMS, LOGL_ERROR, "Destination Address > 12 bytes ?!?\n"); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 384 | rc = GSM411_RP_CAUSE_SEMANT_INC_MSG; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 385 | goto out; |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 386 | } 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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 390 | } |
Harald Welte | 3cfdb22 | 2009-06-12 02:42:11 +0800 | [diff] [blame] | 391 | memset(address_lv, 0, sizeof(address_lv)); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 392 | memcpy(address_lv, smsp, da_len_bytes); |
| 393 | /* mangle first byte to reflect length in bytes, not digits */ |
Harald Welte | 3cfdb22 | 2009-06-12 02:42:11 +0800 | [diff] [blame] | 394 | address_lv[0] = da_len_bytes - 1; |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 395 | |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 396 | gsms->dst.ton = (address_lv[1] >> 4) & 7; |
| 397 | gsms->dst.npi = address_lv[1] & 0xF; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 398 | /* convert to real number */ |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 399 | gsm48_decode_bcd_number(gsms->dst.addr, |
| 400 | sizeof(gsms->dst.addr), address_lv, 1); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 401 | smsp += da_len_bytes; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 402 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 403 | gsms->protocol_id = *smsp++; |
| 404 | gsms->data_coding_scheme = *smsp++; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 405 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 406 | sms_alphabet = gsm338_get_sms_alphabet(gsms->data_coding_scheme); |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 407 | if (sms_alphabet == 0xffffffff) { |
| 408 | sms_free(gsms); |
Harald Welte | e98c635 | 2010-06-09 15:29:13 +0200 | [diff] [blame] | 409 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 410 | } |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 411 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 412 | switch (sms_vpf) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 413 | case GSM340_TP_VPF_RELATIVE: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 414 | sms_vp = smsp++; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 415 | break; |
| 416 | case GSM340_TP_VPF_ABSOLUTE: |
| 417 | case GSM340_TP_VPF_ENHANCED: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 418 | sms_vp = smsp; |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 419 | /* the additional functionality indicator... */ |
Steffen Neubauer | ac0c13c | 2009-12-05 12:44:41 +0100 | [diff] [blame] | 420 | if (sms_vpf == GSM340_TP_VPF_ENHANCED && *smsp & (1<<7)) |
| 421 | smsp++; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 422 | smsp += 7; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 423 | break; |
Daniel Willmann | 58c83d8 | 2009-08-13 03:40:49 +0200 | [diff] [blame] | 424 | case GSM340_TP_VPF_NONE: |
| 425 | sms_vp = 0; |
| 426 | break; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 427 | default: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 428 | LOGP(DLSMS, LOGL_NOTICE, |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 429 | "SMS Validity period not implemented: 0x%02x\n", sms_vpf); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 430 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 431 | } |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 432 | gsms->user_data_len = *smsp++; |
| 433 | if (gsms->user_data_len) { |
| 434 | memcpy(gsms->user_data, smsp, gsms->user_data_len); |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 435 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 436 | switch (sms_alphabet) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 437 | case DCS_7BIT_DEFAULT: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 438 | gsm_7bit_decode(gsms->text, smsp, gsms->user_data_len); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 439 | break; |
| 440 | case DCS_8BIT_DATA: |
| 441 | case DCS_UCS2: |
| 442 | case DCS_NONE: |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 443 | break; |
| 444 | } |
| 445 | } |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 446 | |
Holger Hans Peter Freyther | 4f83409 | 2010-06-16 14:16:47 +0800 | [diff] [blame] | 447 | gsms->sender = subscr_get(conn->subscr); |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 448 | |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 449 | LOGP(DLSMS, LOGL_INFO, "RX SMS: Sender: %s, MTI: 0x%02x, VPF: 0x%02x, " |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 450 | "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 Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 453 | gsms->protocol_id, gsms->data_coding_scheme, gsms->dst.addr, |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 454 | gsms->user_data_len, |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 455 | sms_alphabet == DCS_7BIT_DEFAULT ? gsms->text : |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 456 | osmo_hexdump(gsms->user_data, gsms->user_data_len)); |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 457 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 458 | gsms->validity_minutes = gsm340_validity_period(sms_vpf, sms_vp); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 459 | |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 460 | /* FIXME: This looks very wrong */ |
| 461 | send_signal(0, NULL, gsms, 0); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 462 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 463 | /* determine gsms->receiver based on dialled number */ |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 464 | gsms->receiver = subscr_get_by_extension(conn->bts->network, gsms->dst.addr); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 465 | if (!gsms->receiver) { |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 466 | #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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 476 | rc = 1; /* cause 1: unknown subscriber */ |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 477 | osmo_counter_inc(conn->bts->network->stats.sms.no_receiver); |
Harald Welte | e07b6a7 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 478 | #endif |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 479 | goto out; |
| 480 | } |
| 481 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 482 | switch (sms_mti) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 483 | case GSM340_SMS_SUBMIT_MS2SC: |
| 484 | /* MS is submitting a SMS */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 485 | rc = gsm340_rx_sms_submit(msg, gsms); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 486 | break; |
| 487 | case GSM340_SMS_COMMAND_MS2SC: |
| 488 | case GSM340_SMS_DELIVER_REP_MS2SC: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 489 | LOGP(DLSMS, LOGL_NOTICE, "Unimplemented MTI 0x%02x\n", sms_mti); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 490 | rc = GSM411_RP_CAUSE_IE_NOTEXIST; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 491 | break; |
| 492 | default: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 493 | LOGP(DLSMS, LOGL_NOTICE, "Undefined MTI 0x%02x\n", sms_mti); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 494 | rc = GSM411_RP_CAUSE_IE_NOTEXIST; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 495 | break; |
| 496 | } |
| 497 | |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 498 | if (!rc && !gsms->receiver) |
| 499 | rc = GSM411_RP_CAUSE_MO_NUM_UNASSIGNED; |
| 500 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 501 | out: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 502 | sms_free(gsms); |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 503 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 504 | return rc; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 507 | static int gsm411_send_rp_ack(struct gsm_trans *trans, uint8_t msg_ref) |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 508 | { |
| 509 | struct msgb *msg = gsm411_msgb_alloc(); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 510 | |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 511 | DEBUGP(DLSMS, "TX: SMS RP ACK\n"); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 512 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 513 | return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ACK_MT, msg_ref); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 516 | static int gsm411_send_rp_error(struct gsm_trans *trans, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 517 | uint8_t msg_ref, uint8_t cause) |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 518 | { |
| 519 | struct msgb *msg = gsm411_msgb_alloc(); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 520 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 521 | msgb_tv_put(msg, 1, cause); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 522 | |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 523 | LOGP(DLSMS, LOGL_NOTICE, "TX: SMS RP ERROR, cause %d (%s)\n", cause, |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 524 | get_value_string(rp_cause_strs, cause)); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 525 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 526 | return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ERROR_MT, msg_ref); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 529 | /* Receive a 04.11 TPDU inside RP-DATA / user data */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 530 | static int gsm411_rx_rp_ud(struct msgb *msg, struct gsm_trans *trans, |
| 531 | struct gsm411_rp_hdr *rph, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 532 | uint8_t src_len, uint8_t *src, |
| 533 | uint8_t dst_len, uint8_t *dst, |
| 534 | uint8_t tpdu_len, uint8_t *tpdu) |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 535 | { |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 536 | int rc = 0; |
| 537 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 538 | if (src_len && src) |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 539 | LOGP(DLSMS, LOGL_ERROR, "RP-DATA (MO) with SRC ?!?\n"); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 540 | |
| 541 | if (!dst_len || !dst || !tpdu_len || !tpdu) { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 542 | LOGP(DLSMS, LOGL_ERROR, |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 543 | "RP-DATA (MO) without DST or TPDU ?!?\n"); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 544 | gsm411_send_rp_error(trans, rph->msg_ref, |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 545 | GSM411_RP_CAUSE_INV_MAND_INF); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 546 | return -EIO; |
| 547 | } |
Harald Welte | e9dd9b0 | 2010-04-30 14:27:05 +0200 | [diff] [blame] | 548 | msg->l4h = tpdu; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 549 | |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 550 | DEBUGP(DLSMS, "DST(%u,%s)\n", dst_len, osmo_hexdump(dst, dst_len)); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 551 | |
Holger Hans Peter Freyther | 18b63f4 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 552 | rc = gsm340_rx_tpdu(trans->conn, msg); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 553 | if (rc == 0) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 554 | return gsm411_send_rp_ack(trans, rph->msg_ref); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 555 | else if (rc > 0) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 556 | return gsm411_send_rp_error(trans, rph->msg_ref, rc); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 557 | else |
| 558 | return rc; |
| 559 | } |
| 560 | |
| 561 | /* Receive a 04.11 RP-DATA message in accordance with Section 7.3.1.2 */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 562 | static int gsm411_rx_rp_data(struct msgb *msg, struct gsm_trans *trans, |
| 563 | struct gsm411_rp_hdr *rph) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 564 | { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 565 | uint8_t src_len, dst_len, rpud_len; |
| 566 | uint8_t *src = NULL, *dst = NULL , *rp_ud = NULL; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 567 | |
| 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 Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 581 | DEBUGP(DLSMS, "RX_RP-DATA: src_len=%u, dst_len=%u ud_len=%u\n", |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 582 | src_len, dst_len, rpud_len); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 583 | return gsm411_rx_rp_ud(msg, trans, rph, src_len, src, dst_len, dst, |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 584 | rpud_len, rp_ud); |
| 585 | } |
| 586 | |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 587 | /* Receive a 04.11 RP-ACK message (response to RP-DATA from us) */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 588 | static int gsm411_rx_rp_ack(struct msgb *msg, struct gsm_trans *trans, |
| 589 | struct gsm411_rp_hdr *rph) |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 590 | { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 591 | struct gsm_sms *sms = trans->sms.sms; |
| 592 | |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 593 | /* 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 Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 597 | if (!trans->sms.is_mt) { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 598 | LOGP(DLSMS, LOGL_ERROR, "RX RP-ACK on a MO transfer ?\n"); |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 599 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 600 | GSM411_RP_CAUSE_MSG_INCOMP_STATE); |
| 601 | } |
Harald Welte | 3e0f617 | 2009-07-09 23:52:59 +0200 | [diff] [blame] | 602 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 603 | if (!sms) { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 604 | LOGP(DLSMS, LOGL_ERROR, "RX RP-ACK but no sms in transaction?!?\n"); |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 605 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 606 | GSM411_RP_CAUSE_PROTOCOL_ERR); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /* mark this SMS as sent in database */ |
| 610 | db_sms_mark_sent(sms); |
| 611 | |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 612 | send_signal(S_SMS_DELIVERED, trans, sms, 0); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 613 | |
| 614 | sms_free(sms); |
| 615 | trans->sms.sms = NULL; |
| 616 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 617 | /* check for more messages for this subscriber */ |
Holger Hans Peter Freyther | 5179c8e | 2010-03-23 07:32:23 +0100 | [diff] [blame] | 618 | sms = db_sms_get_unsent_for_subscr(trans->subscr); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 619 | if (sms) |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 620 | gsm411_send_sms(trans->conn, sms); |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 621 | else |
| 622 | gsm411_release_conn(trans->conn); |
Sylvain Munaut | d6c35f6 | 2009-12-24 13:33:51 +0100 | [diff] [blame] | 623 | |
| 624 | /* free the transaction here */ |
| 625 | trans_free(trans); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 626 | return 0; |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 629 | static int gsm411_rx_rp_error(struct msgb *msg, struct gsm_trans *trans, |
| 630 | struct gsm411_rp_hdr *rph) |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 631 | { |
Holger Hans Peter Freyther | 18b63f4 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 632 | struct gsm_network *net = trans->conn->bts->network; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 633 | struct gsm_sms *sms = trans->sms.sms; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 634 | uint8_t cause_len = rph->data[0]; |
| 635 | uint8_t cause = rph->data[1]; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 636 | |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 637 | /* 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 Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 641 | LOGP(DLSMS, LOGL_NOTICE, "%s: RX SMS RP-ERROR, cause %d:%d (%s)\n", |
Holger Hans Peter Freyther | d4c16b1 | 2010-03-23 07:56:22 +0100 | [diff] [blame] | 642 | subscr_name(trans->conn->subscr), cause_len, cause, |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 643 | get_value_string(rp_cause_strs, cause)); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 644 | |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 645 | if (!trans->sms.is_mt) { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 646 | LOGP(DLSMS, LOGL_ERROR, "RX RP-ERR on a MO transfer ?\n"); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 647 | #if 0 |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 648 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 649 | GSM411_RP_CAUSE_MSG_INCOMP_STATE); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 650 | #endif |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 651 | } |
Harald Welte | 3e0f617 | 2009-07-09 23:52:59 +0200 | [diff] [blame] | 652 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 653 | if (!sms) { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 654 | LOGP(DLSMS, LOGL_ERROR, |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 655 | "RX RP-ERR, but no sms in transaction?!?\n"); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 656 | return -EINVAL; |
| 657 | #if 0 |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 658 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 659 | GSM411_RP_CAUSE_PROTOCOL_ERR); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 660 | #endif |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 661 | } |
| 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 Freyther | dd715bd | 2010-12-23 22:23:15 +0100 | [diff] [blame] | 665 | * to store this in our database and wait for a SMMA message */ |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 666 | /* FIXME */ |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 667 | send_signal(S_SMS_MEM_EXCEEDED, trans, sms, 0); |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 668 | osmo_counter_inc(net->stats.sms.rp_err_mem); |
Holger Hans Peter Freyther | 0eb7666 | 2010-12-24 09:47:04 +0100 | [diff] [blame] | 669 | } else { |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 670 | send_signal(S_SMS_UNKNOWN_ERROR, trans, sms, 0); |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 671 | osmo_counter_inc(net->stats.sms.rp_err_other); |
Holger Hans Peter Freyther | 0eb7666 | 2010-12-24 09:47:04 +0100 | [diff] [blame] | 672 | } |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 673 | |
| 674 | sms_free(sms); |
| 675 | trans->sms.sms = NULL; |
| 676 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 677 | return 0; |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 680 | static int gsm411_rx_rp_smma(struct msgb *msg, struct gsm_trans *trans, |
| 681 | struct gsm411_rp_hdr *rph) |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 682 | { |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 683 | struct gsm_sms *sms; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 684 | int rc; |
| 685 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 686 | rc = gsm411_send_rp_ack(trans, rph->msg_ref); |
| 687 | trans->sms.rp_state = GSM411_RPS_IDLE; |
| 688 | |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 689 | /* 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 Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 692 | send_signal(S_SMS_SMMA, trans, NULL, 0); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 693 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 694 | /* check for more messages for this subscriber */ |
Holger Hans Peter Freyther | 5179c8e | 2010-03-23 07:32:23 +0100 | [diff] [blame] | 695 | sms = db_sms_get_unsent_for_subscr(trans->subscr); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 696 | if (sms) |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 697 | gsm411_send_sms(trans->conn, sms); |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 698 | else |
| 699 | gsm411_release_conn(trans->conn); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 700 | |
| 701 | return rc; |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 702 | } |
| 703 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 704 | static int gsm411_rx_cp_data(struct msgb *msg, struct gsm48_hdr *gh, |
| 705 | struct gsm_trans *trans) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 706 | { |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 707 | struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 708 | uint8_t msg_type = rp_data->msg_type & 0x07; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 709 | int rc = 0; |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 710 | |
| 711 | switch (msg_type) { |
| 712 | case GSM411_MT_RP_DATA_MO: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 713 | DEBUGP(DLSMS, "RX SMS RP-DATA (MO)\n"); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 714 | /* 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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 717 | break; |
| 718 | case GSM411_MT_RP_ACK_MO: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 719 | DEBUGP(DLSMS,"RX SMS RP-ACK (MO)\n"); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 720 | rc = gsm411_rx_rp_ack(msg, trans, rp_data); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 721 | break; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 722 | case GSM411_MT_RP_SMMA_MO: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 723 | DEBUGP(DLSMS, "RX SMS RP-SMMA\n"); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 724 | /* 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 Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 730 | break; |
| 731 | default: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 732 | LOGP(DLSMS, LOGL_NOTICE, "Invalid RP type 0x%02x\n", msg_type); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 733 | rc = gsm411_send_rp_error(trans, rp_data->msg_ref, |
| 734 | GSM411_RP_CAUSE_MSGTYPE_NOTEXIST); |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 735 | break; |
| 736 | } |
| 737 | |
| 738 | return rc; |
| 739 | } |
| 740 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 741 | /* send CP-ACK to given transaction */ |
| 742 | static int gsm411_tx_cp_ack(struct gsm_trans *trans) |
| 743 | { |
| 744 | struct msgb *msg = gsm411_msgb_alloc(); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 745 | int rc; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 746 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 747 | 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) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 752 | //trans_free(trans); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 753 | } |
Holger Hans Peter Freyther | 09e364b | 2009-08-10 07:59:27 +0200 | [diff] [blame] | 754 | |
| 755 | return rc; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 756 | } |
| 757 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 758 | static int gsm411_tx_cp_error(struct gsm_trans *trans, uint8_t cause) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 759 | { |
| 760 | struct msgb *msg = gsm411_msgb_alloc(); |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 761 | uint8_t *causep; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 762 | |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 763 | LOGP(DLSMS, LOGL_NOTICE, "TX CP-ERROR, cause %d (%s)\n", cause, |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 764 | get_value_string(cp_cause_strs, cause)); |
| 765 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 766 | causep = msgb_put(msg, 1); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 767 | *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 Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 773 | int gsm0411_rcv_sms(struct gsm_subscriber_connection *conn, |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 774 | struct msgb *msg) |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 775 | { |
| 776 | struct gsm48_hdr *gh = msgb_l3(msg); |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 777 | uint8_t msg_type = gh->msg_type; |
| 778 | uint8_t transaction_id = ((gh->proto_discr >> 4) ^ 0x8); /* flip */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 779 | struct gsm_trans *trans; |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 780 | int rc = 0; |
| 781 | |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 782 | if (!conn->subscr) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 783 | return -EIO; |
| 784 | /* FIXME: send some error message */ |
| 785 | |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 786 | DEBUGP(DLSMS, "trans_id=%x ", transaction_id); |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 787 | trans = trans_find_by_id(conn->subscr, GSM48_PDISC_SMS, |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 788 | transaction_id); |
| 789 | if (!trans) { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 790 | DEBUGPC(DLSMS, "(new) "); |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 791 | trans = trans_alloc(conn->subscr, GSM48_PDISC_SMS, |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 792 | transaction_id, new_callref++); |
| 793 | if (!trans) { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 794 | DEBUGPC(DLSMS, "No memory for trans\n"); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 795 | /* 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 Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 801 | trans->sms.link_id = UM_SAPI_SMS; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 802 | |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 803 | trans->conn = conn; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 806 | switch(msg_type) { |
| 807 | case GSM411_MT_CP_DATA: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 808 | DEBUGPC(DLSMS, "RX SMS CP-DATA\n"); |
Sylvain Munaut | 258e2f9 | 2009-12-24 16:47:08 +0100 | [diff] [blame] | 809 | |
| 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 Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 822 | ptrans = trans_find_by_id(conn->subscr, |
Sylvain Munaut | 258e2f9 | 2009-12-24 16:47:08 +0100 | [diff] [blame] | 823 | GSM48_PDISC_SMS, i); |
| 824 | if (!ptrans) |
| 825 | continue; |
| 826 | |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 827 | DEBUGP(DLSMS, "Implicit CP-ACK for trans_id=%x\n", i); |
Sylvain Munaut | 258e2f9 | 2009-12-24 16:47:08 +0100 | [diff] [blame] | 828 | |
| 829 | /* Finish it for good */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 830 | osmo_timer_del(&ptrans->sms.cp_timer); |
Sylvain Munaut | 258e2f9 | 2009-12-24 16:47:08 +0100 | [diff] [blame] | 831 | ptrans->sms.cp_state = GSM411_CPS_IDLE; |
| 832 | trans_free(ptrans); |
| 833 | } |
| 834 | } |
| 835 | |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 836 | /* 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 Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 841 | |
Harald Welte (local) | daef606 | 2009-08-14 11:41:12 +0200 | [diff] [blame] | 842 | /* SMC instance acknowledges the CP-DATA frame */ |
| 843 | gsm411_tx_cp_ack(trans); |
| 844 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 845 | rc = gsm411_rx_cp_data(msg, gh, trans); |
Harald Welte (local) | daef606 | 2009-08-14 11:41:12 +0200 | [diff] [blame] | 846 | #if 0 |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 847 | /* 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) | daef606 | 2009-08-14 11:41:12 +0200 | [diff] [blame] | 852 | #endif |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 853 | break; |
| 854 | case GSM411_MT_CP_ACK: |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 855 | /* previous CP-DATA in this transaction was confirmed */ |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 856 | DEBUGPC(DLSMS, "RX SMS CP-ACK\n"); |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 857 | /* 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 Welte | 4198561 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 860 | /* Stop TC1* after CP-ACK has been received */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 861 | osmo_timer_del(&trans->sms.cp_timer); |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 862 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 863 | if (!trans->sms.is_mt) { |
Holger Hans Peter Freyther | 0afa044 | 2010-06-10 18:08:12 +0800 | [diff] [blame] | 864 | /* FIXME: we have sent one CP-DATA, which was now |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 865 | * 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 Willmann | bb16e8e | 2008-12-29 03:53:50 +0000 | [diff] [blame] | 870 | break; |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 871 | case GSM411_MT_CP_ERROR: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 872 | DEBUGPC(DLSMS, "RX SMS CP-ERROR, cause %d (%s)\n", gh->data[0], |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 873 | get_value_string(cp_cause_strs, gh->data[0])); |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 874 | osmo_timer_del(&trans->sms.cp_timer); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 875 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 876 | trans_free(trans); |
Daniel Willmann | bb16e8e | 2008-12-29 03:53:50 +0000 | [diff] [blame] | 877 | break; |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 878 | default: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 879 | DEBUGPC(DLSMS, "RX Unimplemented CP msg_type: 0x%02x\n", msg_type); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 880 | rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_MSGTYPE_NOTEXIST); |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 881 | trans->sms.cp_state = GSM411_CPS_IDLE; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 882 | trans_free(trans); |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 883 | break; |
| 884 | } |
| 885 | |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 886 | return rc; |
| 887 | } |
| 888 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 889 | /* 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 Freyther | 11b28f9 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 892 | int gsm411_send_sms(struct gsm_subscriber_connection *conn, struct gsm_sms *sms) |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 893 | { |
| 894 | struct msgb *msg = gsm411_msgb_alloc(); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 895 | struct gsm_trans *trans; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 896 | uint8_t *data, *rp_ud_len; |
| 897 | uint8_t msg_ref = 42; |
Sylvain Munaut | c4052cf | 2009-12-24 13:27:36 +0100 | [diff] [blame] | 898 | int transaction_id; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 899 | int rc; |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 900 | |
Holger Hans Peter Freyther | 5179c8e | 2010-03-23 07:32:23 +0100 | [diff] [blame] | 901 | transaction_id = trans_assign_trans_id(conn->subscr, GSM48_PDISC_SMS, 0); |
Harald Welte | 4983921 | 2009-12-22 13:45:58 +0100 | [diff] [blame] | 902 | if (transaction_id == -1) { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 903 | LOGP(DLSMS, LOGL_ERROR, "No available transaction ids\n"); |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 904 | send_signal(S_SMS_UNKNOWN_ERROR, NULL, sms, 0); |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 905 | sms_free(sms); |
Harald Welte | 4983921 | 2009-12-22 13:45:58 +0100 | [diff] [blame] | 906 | return -EBUSY; |
| 907 | } |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 908 | |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 909 | DEBUGP(DLSMS, "send_sms_lchan()\n"); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 910 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 911 | /* FIXME: allocate transaction with message reference */ |
Holger Hans Peter Freyther | 5179c8e | 2010-03-23 07:32:23 +0100 | [diff] [blame] | 912 | trans = trans_alloc(conn->subscr, GSM48_PDISC_SMS, |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 913 | transaction_id, new_callref++); |
| 914 | if (!trans) { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 915 | LOGP(DLSMS, LOGL_ERROR, "No memory for trans\n"); |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 916 | send_signal(S_SMS_UNKNOWN_ERROR, NULL, sms, 0); |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 917 | sms_free(sms); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 918 | /* 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) | daef606 | 2009-08-14 11:41:12 +0200 | [diff] [blame] | 925 | trans->sms.link_id = UM_SAPI_SMS; /* FIXME: main or SACCH ? */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 926 | |
Holger Hans Peter Freyther | 5179c8e | 2010-03-23 07:32:23 +0100 | [diff] [blame] | 927 | trans->conn = conn; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 928 | |
| 929 | /* Hardcode SMSC Originating Address for now */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 930 | data = (uint8_t *)msgb_put(msg, 8); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 931 | data[0] = 0x07; /* originator length == 7 */ |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 932 | data[1] = 0x91; /* type of number: international, ISDN */ |
| 933 | data[2] = 0x44; /* 447785016005 */ |
Daniel Willmann | a3e2984 | 2008-12-29 16:03:54 +0000 | [diff] [blame] | 934 | data[3] = 0x77; |
| 935 | data[4] = 0x58; |
| 936 | data[5] = 0x10; |
| 937 | data[6] = 0x06; |
| 938 | data[7] = 0x50; |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 939 | |
| 940 | /* Hardcoded Destination Address */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 941 | data = (uint8_t *)msgb_put(msg, 1); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 942 | data[0] = 0; /* destination length == 0 */ |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 943 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 944 | /* obtain a pointer for the rp_ud_len, so we can fill it later */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 945 | rp_ud_len = (uint8_t *)msgb_put(msg, 1); |
Daniel Willmann | 4a1e879 | 2008-12-29 06:23:56 +0000 | [diff] [blame] | 946 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 947 | /* generate the 03.40 TPDU */ |
| 948 | rc = gsm340_gen_tpdu(msg, sms); |
| 949 | if (rc < 0) { |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 950 | send_signal(S_SMS_UNKNOWN_ERROR, trans, sms, 0); |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 951 | trans_free(trans); |
| 952 | sms_free(sms); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 953 | msgb_free(msg); |
| 954 | return rc; |
| 955 | } |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 956 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 957 | *rp_ud_len = rc; |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 958 | |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 959 | DEBUGP(DLSMS, "TX: SMS DELIVER\n"); |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 960 | |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 961 | osmo_counter_inc(conn->bts->network->stats.sms.delivered); |
Nico Golde | 1e5ed4e | 2010-09-16 17:50:29 +0200 | [diff] [blame] | 962 | db_sms_inc_deliver_attempts(trans->sms.sms); |
Harald Welte | 24ff6ee | 2009-12-22 00:41:05 +0100 | [diff] [blame] | 963 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 964 | return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_DATA_MT, msg_ref); |
| 965 | /* FIXME: enter 'wait for RP-ACK' state, start TR1N */ |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 966 | } |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 967 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 968 | /* paging callback. Here we get called if paging a subscriber has |
| 969 | * succeeded or failed. */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 970 | static int paging_cb_send_sms(unsigned int hooknum, unsigned int event, |
Holger Hans Peter Freyther | 86481c2 | 2010-06-17 15:05:57 +0800 | [diff] [blame] | 971 | struct msgb *msg, void *_conn, void *_sms) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 972 | { |
Holger Hans Peter Freyther | 86481c2 | 2010-06-17 15:05:57 +0800 | [diff] [blame] | 973 | struct gsm_subscriber_connection *conn = _conn; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 974 | struct gsm_sms *sms = _sms; |
Holger Hans Peter Freyther | 4cd5158 | 2010-06-14 16:03:03 +0800 | [diff] [blame] | 975 | int rc = 0; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 976 | |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 977 | DEBUGP(DLSMS, "paging_cb_send_sms(hooknum=%u, event=%u, msg=%p," |
Holger Hans Peter Freyther | 52ffb92 | 2010-12-29 14:02:34 +0100 | [diff] [blame] | 978 | "conn=%p, sms=%p/id: %llu)\n", hooknum, event, msg, conn, sms, sms->id); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 979 | |
| 980 | if (hooknum != GSM_HOOK_RR_PAGING) |
| 981 | return -EINVAL; |
| 982 | |
| 983 | switch (event) { |
| 984 | case GSM_PAGING_SUCCEEDED: |
Holger Hans Peter Freyther | 86481c2 | 2010-06-17 15:05:57 +0800 | [diff] [blame] | 985 | gsm411_send_sms(conn, sms); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 986 | break; |
| 987 | case GSM_PAGING_EXPIRED: |
Holger Hans Peter Freyther | ca5702a | 2010-06-10 18:20:54 +0800 | [diff] [blame] | 988 | case GSM_PAGING_OOM: |
Holger Hans Peter Freyther | d3baf41 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 989 | case GSM_PAGING_BUSY: |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 990 | send_signal(S_SMS_UNKNOWN_ERROR, NULL, sms, event); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 991 | sms_free(sms); |
| 992 | rc = -ETIMEDOUT; |
| 993 | break; |
Holger Hans Peter Freyther | 52ffb92 | 2010-12-29 14:02:34 +0100 | [diff] [blame] | 994 | default: |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 995 | LOGP(DLSMS, LOGL_ERROR, "Unhandled paging event: %d\n", event); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | return rc; |
| 999 | } |
| 1000 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1001 | /* 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 Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1004 | int gsm411_send_sms_subscr(struct gsm_subscriber *subscr, |
| 1005 | struct gsm_sms *sms) |
| 1006 | { |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 1007 | struct gsm_subscriber_connection *conn; |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1008 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1009 | /* check if we already have an open lchan to the subscriber. |
| 1010 | * if yes, send the SMS this way */ |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 1011 | conn = connection_for_subscr(subscr); |
| 1012 | if (conn) { |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 1013 | return gsm411_send_sms(conn, sms); |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 1014 | } |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1015 | |
| 1016 | /* if not, we have to start paging */ |
Holger Hans Peter Freyther | 7afac58 | 2010-06-10 18:21:25 +0800 | [diff] [blame] | 1017 | subscr_get_channel(subscr, RSL_CHANNEED_SDCCH, paging_cb_send_sms, sms); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
Harald Welte | 3366a94 | 2009-07-28 00:44:49 +0200 | [diff] [blame] | 1020 | |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 1021 | void _gsm411_sms_trans_free(struct gsm_trans *trans) |
| 1022 | { |
Holger Hans Peter Freyther | cea722b | 2010-06-15 12:29:37 +0800 | [diff] [blame] | 1023 | if (trans->sms.sms) { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 1024 | LOGP(DLSMS, LOGL_ERROR, "Transaction contains SMS.\n"); |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 1025 | send_signal(S_SMS_UNKNOWN_ERROR, trans, trans->sms.sms, 0); |
Holger Hans Peter Freyther | cea722b | 2010-06-15 12:29:37 +0800 | [diff] [blame] | 1026 | sms_free(trans->sms.sms); |
| 1027 | trans->sms.sms = NULL; |
| 1028 | } |
| 1029 | |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 1030 | osmo_timer_del(&trans->sms.cp_timer); |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 1031 | } |
| 1032 | |
Holger Hans Peter Freyther | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 1033 | void gsm411_sapi_n_reject(struct gsm_subscriber_connection *conn) |
| 1034 | { |
Holger Hans Peter Freyther | 725966d | 2010-12-27 22:43:28 +0100 | [diff] [blame] | 1035 | struct gsm_subscriber *subscr; |
Holger Hans Peter Freyther | 79eff7e | 2010-12-27 23:25:53 +0100 | [diff] [blame] | 1036 | struct gsm_network *net; |
Holger Hans Peter Freyther | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 1037 | struct gsm_trans *trans, *tmp; |
| 1038 | |
Holger Hans Peter Freyther | 725966d | 2010-12-27 22:43:28 +0100 | [diff] [blame] | 1039 | subscr = subscr_get(conn->subscr); |
Holger Hans Peter Freyther | 79eff7e | 2010-12-27 23:25:53 +0100 | [diff] [blame] | 1040 | net = conn->bts->network; |
| 1041 | |
| 1042 | llist_for_each_entry_safe(trans, tmp, &net->trans_list, entry) |
Holger Hans Peter Freyther | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 1043 | if (trans->conn == conn) { |
| 1044 | struct gsm_sms *sms = trans->sms.sms; |
| 1045 | if (!sms) { |
Holger Hans Peter Freyther | eff4094 | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 1046 | LOGP(DLSMS, LOGL_ERROR, "SAPI Reject but no SMS.\n"); |
Holger Hans Peter Freyther | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 1047 | continue; |
| 1048 | } |
| 1049 | |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 1050 | send_signal(S_SMS_UNKNOWN_ERROR, trans, sms, 0); |
Holger Hans Peter Freyther | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 1051 | sms_free(sms); |
| 1052 | trans->sms.sms = NULL; |
| 1053 | trans_free(trans); |
| 1054 | } |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 1055 | |
Holger Hans Peter Freyther | 725966d | 2010-12-27 22:43:28 +0100 | [diff] [blame] | 1056 | subscr_put_channel(subscr); |
| 1057 | subscr_put(subscr); |
Holger Hans Peter Freyther | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 1058 | } |
| 1059 | |