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 | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 7 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 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 |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along |
| 23 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <errno.h> |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 33 | #include <time.h> |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 34 | #include <netinet/in.h> |
| 35 | |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 36 | #include <osmocore/msgb.h> |
| 37 | #include <osmocore/tlv.h> |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 38 | #include <openbsc/debug.h> |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 39 | #include <openbsc/gsm_data.h> |
| 40 | #include <openbsc/gsm_subscriber.h> |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 41 | #include <openbsc/gsm_04_11.h> |
| 42 | #include <openbsc/gsm_04_08.h> |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 43 | #include <osmocore/gsm_utils.h> |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 44 | #include <openbsc/abis_rsl.h> |
Holger Freyther | 9b17776 | 2009-02-16 19:07:18 +0000 | [diff] [blame] | 45 | #include <openbsc/signal.h> |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 46 | #include <openbsc/db.h> |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 47 | #include <osmocore/talloc.h> |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 48 | #include <openbsc/transaction.h> |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 49 | #include <openbsc/paging.h> |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 50 | #include <openbsc/bsc_rll.h> |
Holger Hans Peter Freyther | 34e9749 | 2009-08-10 07:54:02 +0200 | [diff] [blame] | 51 | #include <openbsc/chan_alloc.h> |
Holger Hans Peter Freyther | cbac76e | 2010-05-23 21:05:18 +0800 | [diff] [blame] | 52 | #include <openbsc/bsc_api.h> |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 53 | |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 54 | #define GSM411_ALLOC_SIZE 1024 |
| 55 | #define GSM411_ALLOC_HEADROOM 128 |
| 56 | |
Harald Welte (local) | d19e58b | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 57 | void *tall_gsms_ctx; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 58 | static u_int32_t new_callref = 0x40000001; |
| 59 | |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 60 | static const struct value_string cp_cause_strs[] = { |
| 61 | { GSM411_CP_CAUSE_NET_FAIL, "Network Failure" }, |
| 62 | { GSM411_CP_CAUSE_CONGESTION, "Congestion" }, |
| 63 | { GSM411_CP_CAUSE_INV_TRANS_ID, "Invalid Transaction ID" }, |
| 64 | { GSM411_CP_CAUSE_SEMANT_INC_MSG, "Semantically Incorrect Message" }, |
| 65 | { GSM411_CP_CAUSE_INV_MAND_INF, "Invalid Mandatory Information" }, |
| 66 | { GSM411_CP_CAUSE_MSGTYPE_NOTEXIST, "Message Type doesn't exist" }, |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 67 | { GSM411_CP_CAUSE_MSG_INCOMP_STATE, |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 68 | "Message incompatible with protocol state" }, |
| 69 | { GSM411_CP_CAUSE_IE_NOTEXIST, "IE does not exist" }, |
| 70 | { GSM411_CP_CAUSE_PROTOCOL_ERR, "Protocol Error" }, |
| 71 | { 0, 0 } |
| 72 | }; |
| 73 | |
| 74 | static const struct value_string rp_cause_strs[] = { |
| 75 | { GSM411_RP_CAUSE_MO_NUM_UNASSIGNED, "(MO) Number not assigned" }, |
| 76 | { GSM411_RP_CAUSE_MO_OP_DET_BARR, "(MO) Operator determined barring" }, |
| 77 | { GSM411_RP_CAUSE_MO_CALL_BARRED, "(MO) Call barred" }, |
| 78 | { GSM411_RP_CAUSE_MO_SMS_REJECTED, "(MO) SMS rejected" }, |
| 79 | { GSM411_RP_CAUSE_MO_DEST_OUT_OF_ORDER, "(MO) Destination out of order" }, |
| 80 | { GSM411_RP_CAUSE_MO_UNIDENTIFIED_SUBSCR, "(MO) Unidentified subscriber" }, |
| 81 | { GSM411_RP_CAUSE_MO_FACILITY_REJ, "(MO) Facility reject" }, |
| 82 | { GSM411_RP_CAUSE_MO_UNKNOWN_SUBSCR, "(MO) Unknown subscriber" }, |
| 83 | { GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER, "(MO) Network out of order" }, |
| 84 | { GSM411_RP_CAUSE_MO_TEMP_FAIL, "(MO) Temporary failure" }, |
| 85 | { GSM411_RP_CAUSE_MO_CONGESTION, "(MO) Congestion" }, |
| 86 | { GSM411_RP_CAUSE_MO_RES_UNAVAIL, "(MO) Resource unavailable" }, |
| 87 | { GSM411_RP_CAUSE_MO_REQ_FAC_NOTSUBSCR, "(MO) Requested facility not subscribed" }, |
| 88 | { GSM411_RP_CAUSE_MO_REQ_FAC_NOTIMPL, "(MO) Requested facility not implemented" }, |
| 89 | { GSM411_RP_CAUSE_MO_INTERWORKING, "(MO) Interworking" }, |
| 90 | /* valid only for MT */ |
| 91 | { GSM411_RP_CAUSE_MT_MEM_EXCEEDED, "(MT) Memory Exceeded" }, |
| 92 | /* valid for both directions */ |
| 93 | { GSM411_RP_CAUSE_INV_TRANS_REF, "Invalid Transaction Reference" }, |
| 94 | { GSM411_RP_CAUSE_SEMANT_INC_MSG, "Semantically Incorrect Message" }, |
| 95 | { GSM411_RP_CAUSE_INV_MAND_INF, "Invalid Mandatory Information" }, |
| 96 | { GSM411_RP_CAUSE_MSGTYPE_NOTEXIST, "Message Type non-existant" }, |
| 97 | { GSM411_RP_CAUSE_MSG_INCOMP_STATE, "Message incompatible with protocol state" }, |
| 98 | { GSM411_RP_CAUSE_IE_NOTEXIST, "Information Element not existing" }, |
| 99 | { GSM411_RP_CAUSE_PROTOCOL_ERR, "Protocol Error" }, |
| 100 | { 0, NULL } |
| 101 | }; |
| 102 | |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 103 | static int gsm411_send_sms(struct gsm_subscriber_connection *conn, struct gsm_sms *sms); |
| 104 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 105 | struct gsm_sms *sms_alloc(void) |
| 106 | { |
| 107 | return talloc_zero(tall_gsms_ctx, struct gsm_sms); |
| 108 | } |
| 109 | |
| 110 | void sms_free(struct gsm_sms *sms) |
| 111 | { |
| 112 | /* drop references to subscriber structure */ |
| 113 | if (sms->sender) |
| 114 | subscr_put(sms->sender); |
| 115 | if (sms->receiver) |
| 116 | subscr_put(sms->receiver); |
| 117 | |
| 118 | talloc_free(sms); |
| 119 | } |
| 120 | |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 121 | /* |
| 122 | * This should be called whenever all SMS to a given subscriber |
| 123 | * on a given connection has been sent. This will inform the higher |
| 124 | * layers that a channel can be given up. |
| 125 | */ |
| 126 | static void gsm411_release_conn(struct gsm_subscriber_connection *conn) |
| 127 | { |
Holger Hans Peter Freyther | bddd152 | 2010-06-16 14:02:41 +0800 | [diff] [blame] | 128 | if (!conn) |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 129 | return; |
| 130 | |
Holger Hans Peter Freyther | bddd152 | 2010-06-16 14:02:41 +0800 | [diff] [blame] | 131 | subscr_put_channel(conn); |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 132 | } |
| 133 | |
Holger Freyther | a553d09 | 2009-01-04 20:16:25 +0000 | [diff] [blame] | 134 | struct msgb *gsm411_msgb_alloc(void) |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 135 | { |
Harald Welte | 966636f | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 136 | return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM, |
| 137 | "GSM 04.11"); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 140 | static int gsm411_sendmsg(struct gsm_subscriber_connection *conn, struct msgb *msg, u_int8_t link_id) |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 141 | { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 142 | DEBUGP(DSMS, "GSM4.11 TX %s\n", hexdump(msg->data, msg->len)); |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 143 | msg->l3h = msg->data; |
| 144 | return gsm0808_submit_dtap(conn, msg, link_id); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Harald Welte | 4198561 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 147 | /* SMC TC1* is expired */ |
| 148 | static void cp_timer_expired(void *data) |
| 149 | { |
| 150 | struct gsm_trans *trans = data; |
| 151 | |
| 152 | DEBUGP(DSMS, "SMC Timer TC1* is expired, calling trans_free()\n"); |
| 153 | /* FIXME: we need to re-transmit the last CP-DATA 1..3 times */ |
| 154 | trans_free(trans); |
| 155 | } |
| 156 | |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 157 | /* Prefix msg with a 04.08/04.11 CP header */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 158 | static int gsm411_cp_sendmsg(struct msgb *msg, struct gsm_trans *trans, |
| 159 | u_int8_t msg_type) |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 160 | { |
| 161 | struct gsm48_hdr *gh; |
| 162 | |
| 163 | gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh)); |
| 164 | /* Outgoing needs the highest bit set */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 165 | gh->proto_discr = trans->protocol | (trans->transaction_id<<4); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 166 | gh->msg_type = msg_type; |
| 167 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 168 | /* mobile originating */ |
| 169 | switch (gh->msg_type) { |
| 170 | case GSM411_MT_CP_DATA: |
| 171 | /* 5.2.3.1.2: enter MO-wait for CP-ack */ |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 172 | /* 5.2.3.2.3: enter MT-wait for CP-ACK */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 173 | trans->sms.cp_state = GSM411_CPS_WAIT_CP_ACK; |
Harald Welte | 4198561 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 174 | trans->sms.cp_timer.data = trans; |
| 175 | trans->sms.cp_timer.cb = cp_timer_expired; |
| 176 | /* 5.3.2.1: Set Timer TC1A */ |
| 177 | bsc_schedule_timer(&trans->sms.cp_timer, GSM411_TMR_TC1A); |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 178 | DEBUGP(DSMS, "TX: CP-DATA "); |
| 179 | break; |
| 180 | case GSM411_MT_CP_ACK: |
| 181 | DEBUGP(DSMS, "TX: CP-ACK "); |
| 182 | break; |
| 183 | case GSM411_MT_CP_ERROR: |
Sylvain Munaut | 1d9efd6 | 2009-12-18 18:28:07 +0100 | [diff] [blame] | 184 | DEBUGP(DSMS, "TX: CP-ERROR "); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 185 | break; |
| 186 | } |
| 187 | |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 188 | DEBUGPC(DSMS, "trans=%x\n", trans->transaction_id); |
| 189 | |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 190 | return gsm411_sendmsg(trans->conn, msg, trans->sms.link_id); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* Prefix msg with a RP-DATA header and send as CP-DATA */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 194 | static int gsm411_rp_sendmsg(struct msgb *msg, struct gsm_trans *trans, |
| 195 | u_int8_t rp_msg_type, u_int8_t rp_msg_ref) |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 196 | { |
| 197 | struct gsm411_rp_hdr *rp; |
Harald Welte | 0d544e7 | 2009-08-10 00:22:19 +0200 | [diff] [blame] | 198 | u_int8_t len = msg->len; |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 199 | |
| 200 | /* GSM 04.11 RP-DATA header */ |
| 201 | rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp)); |
Harald Welte | 0d544e7 | 2009-08-10 00:22:19 +0200 | [diff] [blame] | 202 | rp->len = len + 2; |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 203 | rp->msg_type = rp_msg_type; |
| 204 | rp->msg_ref = rp_msg_ref; /* FIXME: Choose randomly */ |
| 205 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 206 | return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_DATA); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 209 | /* Turn int into semi-octet representation: 98 => 0x89 */ |
| 210 | static u_int8_t bcdify(u_int8_t value) |
| 211 | { |
| 212 | u_int8_t ret; |
Steffen Neubauer | 5727cf4 | 2009-11-11 23:02:07 +0900 | [diff] [blame] | 213 | |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 214 | ret = value / 10; |
| 215 | ret |= (value % 10) << 4; |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | /* Turn semi-octet representation into int: 0x89 => 98 */ |
| 221 | static u_int8_t unbcdify(u_int8_t value) |
| 222 | { |
| 223 | u_int8_t ret; |
| 224 | |
| 225 | if ((value & 0x0F) > 9 || (value >> 4) > 9) |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 226 | LOGP(DSMS, LOGL_ERROR, |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 227 | "unbcdify got too big nibble: 0x%02X\n", value); |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 228 | |
| 229 | ret = (value&0x0F)*10; |
Steffen Neubauer | ad69d7f | 2009-11-26 23:38:41 +0100 | [diff] [blame] | 230 | ret += value>>4; |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | /* Generate 03.40 TP-SCTS */ |
| 236 | static void gsm340_gen_scts(u_int8_t *scts, time_t time) |
| 237 | { |
| 238 | struct tm *tm = localtime(&time); |
| 239 | |
| 240 | *scts++ = bcdify(tm->tm_year % 100); |
| 241 | *scts++ = bcdify(tm->tm_mon + 1); |
| 242 | *scts++ = bcdify(tm->tm_mday); |
| 243 | *scts++ = bcdify(tm->tm_hour); |
| 244 | *scts++ = bcdify(tm->tm_min); |
| 245 | *scts++ = bcdify(tm->tm_sec); |
| 246 | *scts++ = bcdify(tm->tm_gmtoff/(60*15)); |
| 247 | } |
| 248 | |
| 249 | /* Decode 03.40 TP-SCTS (into utc/gmt timestamp) */ |
| 250 | static time_t gsm340_scts(u_int8_t *scts) |
| 251 | { |
| 252 | struct tm tm; |
| 253 | |
| 254 | u_int8_t yr = unbcdify(*scts++); |
| 255 | |
| 256 | if (yr <= 80) |
| 257 | tm.tm_year = 100 + yr; |
| 258 | else |
| 259 | tm.tm_year = yr; |
| 260 | tm.tm_mon = unbcdify(*scts++) - 1; |
| 261 | tm.tm_mday = unbcdify(*scts++); |
| 262 | tm.tm_hour = unbcdify(*scts++); |
| 263 | tm.tm_min = unbcdify(*scts++); |
| 264 | tm.tm_sec = unbcdify(*scts++); |
| 265 | /* according to gsm 03.40 time zone is |
| 266 | "expressed in quarters of an hour" */ |
| 267 | tm.tm_gmtoff = unbcdify(*scts++) * 15*60; |
| 268 | |
| 269 | return mktime(&tm); |
| 270 | } |
| 271 | |
| 272 | /* Return the default validity period in minutes */ |
| 273 | static unsigned long gsm340_vp_default(void) |
| 274 | { |
| 275 | unsigned long minutes; |
| 276 | /* Default validity: two days */ |
| 277 | minutes = 24 * 60 * 2; |
| 278 | return minutes; |
| 279 | } |
| 280 | |
| 281 | /* Decode validity period format 'relative' */ |
| 282 | static unsigned long gsm340_vp_relative(u_int8_t *sms_vp) |
| 283 | { |
| 284 | /* Chapter 9.2.3.12.1 */ |
| 285 | u_int8_t vp; |
| 286 | unsigned long minutes; |
| 287 | |
| 288 | vp = *(sms_vp); |
| 289 | if (vp <= 143) |
| 290 | minutes = vp + 1 * 5; |
| 291 | else if (vp <= 167) |
| 292 | minutes = 12*60 + (vp-143) * 30; |
| 293 | else if (vp <= 196) |
| 294 | minutes = vp-166 * 60 * 24; |
| 295 | else |
| 296 | minutes = vp-192 * 60 * 24 * 7; |
| 297 | return minutes; |
| 298 | } |
| 299 | |
| 300 | /* Decode validity period format 'absolute' */ |
| 301 | static unsigned long gsm340_vp_absolute(u_int8_t *sms_vp) |
| 302 | { |
| 303 | /* Chapter 9.2.3.12.2 */ |
| 304 | time_t expires, now; |
| 305 | unsigned long minutes; |
| 306 | |
| 307 | expires = gsm340_scts(sms_vp); |
| 308 | now = mktime(gmtime(NULL)); |
| 309 | if (expires <= now) |
| 310 | minutes = 0; |
| 311 | else |
| 312 | minutes = (expires-now)/60; |
| 313 | return minutes; |
| 314 | } |
| 315 | |
| 316 | /* Decode validity period format 'relative in integer representation' */ |
| 317 | static unsigned long gsm340_vp_relative_integer(u_int8_t *sms_vp) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 318 | { |
| 319 | u_int8_t vp; |
| 320 | unsigned long minutes; |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 321 | vp = *(sms_vp); |
| 322 | if (vp == 0) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 323 | LOGP(DSMS, LOGL_ERROR, |
| 324 | "reserved relative_integer validity period\n"); |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 325 | return gsm340_vp_default(); |
| 326 | } |
| 327 | minutes = vp/60; |
| 328 | return minutes; |
| 329 | } |
| 330 | |
| 331 | /* Decode validity period format 'relative in semi-octet representation' */ |
| 332 | static unsigned long gsm340_vp_relative_semioctet(u_int8_t *sms_vp) |
| 333 | { |
| 334 | unsigned long minutes; |
| 335 | minutes = unbcdify(*sms_vp++)*60; /* hours */ |
| 336 | minutes += unbcdify(*sms_vp++); /* minutes */ |
| 337 | minutes += unbcdify(*sms_vp++)/60; /* seconds */ |
| 338 | return minutes; |
| 339 | } |
| 340 | |
| 341 | /* decode validity period. return minutes */ |
| 342 | static unsigned long gsm340_validity_period(u_int8_t sms_vpf, u_int8_t *sms_vp) |
| 343 | { |
| 344 | u_int8_t fi; /* functionality indicator */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 345 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 346 | switch (sms_vpf) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 347 | case GSM340_TP_VPF_RELATIVE: |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 348 | return gsm340_vp_relative(sms_vp); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 349 | case GSM340_TP_VPF_ABSOLUTE: |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 350 | return gsm340_vp_absolute(sms_vp); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 351 | case GSM340_TP_VPF_ENHANCED: |
| 352 | /* Chapter 9.2.3.12.3 */ |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 353 | fi = *sms_vp++; |
| 354 | /* ignore additional fi */ |
| 355 | if (fi & (1<<7)) sms_vp++; |
| 356 | /* read validity period format */ |
| 357 | switch (fi & 0b111) { |
| 358 | case 0b000: |
| 359 | return gsm340_vp_default(); /* no vpf specified */ |
| 360 | case 0b001: |
| 361 | return gsm340_vp_relative(sms_vp); |
| 362 | case 0b010: |
| 363 | return gsm340_vp_relative_integer(sms_vp); |
| 364 | case 0b011: |
| 365 | return gsm340_vp_relative_semioctet(sms_vp); |
| 366 | default: |
| 367 | /* The GSM spec says that the SC should reject any |
| 368 | unsupported and/or undefined values. FIXME */ |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 369 | LOGP(DSMS, LOGL_ERROR, |
| 370 | "Reserved enhanced validity period format\n"); |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 371 | return gsm340_vp_default(); |
| 372 | } |
Daniel Willmann | 58c83d8 | 2009-08-13 03:40:49 +0200 | [diff] [blame] | 373 | case GSM340_TP_VPF_NONE: |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 374 | default: |
| 375 | return gsm340_vp_default(); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 376 | } |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* determine coding alphabet dependent on GSM 03.38 Section 4 DCS */ |
| 380 | enum sms_alphabet gsm338_get_sms_alphabet(u_int8_t dcs) |
| 381 | { |
| 382 | u_int8_t cgbits = dcs >> 4; |
| 383 | enum sms_alphabet alpha = DCS_NONE; |
| 384 | |
| 385 | if ((cgbits & 0xc) == 0) { |
Harald Welte | e98c635 | 2010-06-09 15:29:13 +0200 | [diff] [blame] | 386 | if (cgbits & 2) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 387 | LOGP(DSMS, LOGL_NOTICE, |
| 388 | "Compressed SMS not supported yet\n"); |
Harald Welte | e98c635 | 2010-06-09 15:29:13 +0200 | [diff] [blame] | 389 | return 0xffffffff; |
| 390 | } |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 391 | |
Daniel Willmann | d5d5e1d | 2009-08-15 03:01:17 +0200 | [diff] [blame] | 392 | switch ((dcs >> 2)&0x03) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 393 | case 0: |
| 394 | alpha = DCS_7BIT_DEFAULT; |
| 395 | break; |
| 396 | case 1: |
| 397 | alpha = DCS_8BIT_DATA; |
| 398 | break; |
| 399 | case 2: |
| 400 | alpha = DCS_UCS2; |
| 401 | break; |
| 402 | } |
| 403 | } else if (cgbits == 0xc || cgbits == 0xd) |
| 404 | alpha = DCS_7BIT_DEFAULT; |
| 405 | else if (cgbits == 0xe) |
| 406 | alpha = DCS_UCS2; |
| 407 | else if (cgbits == 0xf) { |
| 408 | if (dcs & 4) |
| 409 | alpha = DCS_8BIT_DATA; |
| 410 | else |
| 411 | alpha = DCS_7BIT_DEFAULT; |
| 412 | } |
| 413 | |
| 414 | return alpha; |
| 415 | } |
| 416 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 417 | 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] | 418 | { |
| 419 | if (db_sms_store(gsms) != 0) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 420 | LOGP(DSMS, LOGL_ERROR, "Failed to store SMS in Database\n"); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 421 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 422 | } |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 423 | /* dispatch a signal to tell higher level about it */ |
| 424 | dispatch_signal(SS_SMS, S_SMS_SUBMITTED, gsms); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 425 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 426 | return 0; |
| 427 | } |
| 428 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 429 | /* generate a TPDU address field compliant with 03.40 sec. 9.1.2.5 */ |
| 430 | static int gsm340_gen_oa(u_int8_t *oa, unsigned int oa_len, |
| 431 | struct gsm_subscriber *subscr) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 432 | { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 433 | int len_in_bytes; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 434 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 435 | oa[1] = 0xb9; /* networks-specific number, private numbering plan */ |
| 436 | |
Harald Welte | 55c8f35 | 2010-03-07 23:40:35 +0100 | [diff] [blame] | 437 | len_in_bytes = gsm48_encode_bcd_number(oa, oa_len, 1, subscr->extension); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 438 | |
| 439 | /* GSM 03.40 tells us the length is in 'useful semi-octets' */ |
| 440 | oa[0] = strlen(subscr->extension) & 0xff; |
| 441 | |
| 442 | return len_in_bytes; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 445 | /* generate a msgb containing a TPDU derived from struct gsm_sms, |
| 446 | * returns total size of TPDU */ |
| 447 | static int gsm340_gen_tpdu(struct msgb *msg, struct gsm_sms *sms) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 448 | { |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 449 | u_int8_t *smsp; |
| 450 | u_int8_t oa[12]; /* max len per 03.40 */ |
| 451 | u_int8_t oa_len = 0; |
Daniel Willmann | 9aef145 | 2009-08-13 03:39:07 +0200 | [diff] [blame] | 452 | u_int8_t octet_len; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 453 | unsigned int old_msg_len = msg->len; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 454 | |
| 455 | /* generate first octet with masked bits */ |
| 456 | smsp = msgb_put(msg, 1); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 457 | /* TP-MTI (message type indicator) */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 458 | *smsp = GSM340_SMS_DELIVER_SC2MS; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 459 | /* TP-MMS (more messages to send) */ |
| 460 | if (0 /* FIXME */) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 461 | *smsp |= 0x04; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 462 | /* TP-SRI(deliver)/SRR(submit) */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 463 | if (sms->status_rep_req) |
| 464 | *smsp |= 0x20; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 465 | /* TP-UDHI (indicating TP-UD contains a header) */ |
| 466 | if (sms->ud_hdr_ind) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 467 | *smsp |= 0x40; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 468 | |
| 469 | /* generate originator address */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 470 | oa_len = gsm340_gen_oa(oa, sizeof(oa), sms->sender); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 471 | smsp = msgb_put(msg, oa_len); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 472 | memcpy(smsp, oa, oa_len); |
| 473 | |
| 474 | /* generate TP-PID */ |
| 475 | smsp = msgb_put(msg, 1); |
| 476 | *smsp = sms->protocol_id; |
| 477 | |
| 478 | /* generate TP-DCS */ |
| 479 | smsp = msgb_put(msg, 1); |
| 480 | *smsp = sms->data_coding_scheme; |
| 481 | |
| 482 | /* generate TP-SCTS */ |
| 483 | smsp = msgb_put(msg, 7); |
| 484 | gsm340_gen_scts(smsp, time(NULL)); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 485 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 486 | /* generate TP-UDL */ |
| 487 | smsp = msgb_put(msg, 1); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 488 | *smsp = sms->user_data_len; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 489 | |
| 490 | /* generate TP-UD */ |
Daniel Willmann | 6b024cb | 2009-08-15 03:01:46 +0200 | [diff] [blame] | 491 | switch (gsm338_get_sms_alphabet(sms->data_coding_scheme)) { |
| 492 | case DCS_7BIT_DEFAULT: |
Daniel Willmann | 9aef145 | 2009-08-13 03:39:07 +0200 | [diff] [blame] | 493 | octet_len = sms->user_data_len*7/8; |
| 494 | if (sms->user_data_len*7%8 != 0) |
| 495 | octet_len++; |
Daniel Willmann | 6b1e822 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 496 | /* Warning, user_data_len indicates the amount of septets |
| 497 | * (characters), we need amount of octets occupied */ |
Daniel Willmann | 9aef145 | 2009-08-13 03:39:07 +0200 | [diff] [blame] | 498 | smsp = msgb_put(msg, octet_len); |
| 499 | memcpy(smsp, sms->user_data, octet_len); |
Daniel Willmann | 6b024cb | 2009-08-15 03:01:46 +0200 | [diff] [blame] | 500 | break; |
| 501 | case DCS_UCS2: |
| 502 | case DCS_8BIT_DATA: |
| 503 | smsp = msgb_put(msg, sms->user_data_len); |
| 504 | memcpy(smsp, sms->user_data, sms->user_data_len); |
| 505 | break; |
| 506 | default: |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 507 | LOGP(DSMS, LOGL_NOTICE, "Unhandled Data Coding Scheme: 0x%02X\n", |
| 508 | sms->data_coding_scheme); |
Daniel Willmann | 6b024cb | 2009-08-15 03:01:46 +0200 | [diff] [blame] | 509 | break; |
Daniel Willmann | 6b1e822 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 510 | } |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 511 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 512 | return msg->len - old_msg_len; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 513 | } |
| 514 | |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 515 | /* process an incoming TPDU (called from RP-DATA) |
| 516 | * 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] | 517 | 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] | 518 | { |
| 519 | u_int8_t *smsp = msgb_sms(msg); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 520 | struct gsm_sms *gsms; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 521 | u_int8_t sms_mti, sms_mms, sms_vpf, sms_alphabet, sms_rp; |
| 522 | u_int8_t *sms_vp; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 523 | u_int8_t da_len_bytes; |
| 524 | u_int8_t address_lv[12]; /* according to 03.40 / 9.1.2.5 */ |
| 525 | int rc = 0; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 526 | |
Holger Hans Peter Freyther | 18b63f4 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 527 | counter_inc(conn->bts->network->stats.sms.submitted); |
Harald Welte | 24ff6ee | 2009-12-22 00:41:05 +0100 | [diff] [blame] | 528 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 529 | gsms = sms_alloc(); |
| 530 | if (!gsms) |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 531 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 532 | |
| 533 | /* invert those fields where 0 means active/present */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 534 | sms_mti = *smsp & 0x03; |
| 535 | sms_mms = !!(*smsp & 0x04); |
| 536 | sms_vpf = (*smsp & 0x18) >> 3; |
| 537 | gsms->status_rep_req = (*smsp & 0x20); |
| 538 | gsms->ud_hdr_ind = (*smsp & 0x40); |
| 539 | sms_rp = (*smsp & 0x80); |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 540 | |
| 541 | smsp++; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 542 | gsms->msg_ref = *smsp++; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 543 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 544 | /* length in bytes of the destination address */ |
| 545 | da_len_bytes = 2 + *smsp/2 + *smsp%2; |
| 546 | if (da_len_bytes > 12) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 547 | LOGP(DSMS, LOGL_ERROR, "Destination Address > 12 bytes ?!?\n"); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 548 | rc = GSM411_RP_CAUSE_SEMANT_INC_MSG; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 549 | goto out; |
| 550 | } |
Harald Welte | 3cfdb22 | 2009-06-12 02:42:11 +0800 | [diff] [blame] | 551 | memset(address_lv, 0, sizeof(address_lv)); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 552 | memcpy(address_lv, smsp, da_len_bytes); |
| 553 | /* mangle first byte to reflect length in bytes, not digits */ |
Harald Welte | 3cfdb22 | 2009-06-12 02:42:11 +0800 | [diff] [blame] | 554 | address_lv[0] = da_len_bytes - 1; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 555 | /* convert to real number */ |
Harald Welte | 55c8f35 | 2010-03-07 23:40:35 +0100 | [diff] [blame] | 556 | gsm48_decode_bcd_number(gsms->dest_addr, sizeof(gsms->dest_addr), address_lv, 1); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 557 | smsp += da_len_bytes; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 558 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 559 | gsms->protocol_id = *smsp++; |
| 560 | gsms->data_coding_scheme = *smsp++; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 561 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 562 | sms_alphabet = gsm338_get_sms_alphabet(gsms->data_coding_scheme); |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 563 | if (sms_alphabet == 0xffffffff) { |
| 564 | sms_free(gsms); |
Harald Welte | e98c635 | 2010-06-09 15:29:13 +0200 | [diff] [blame] | 565 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 566 | } |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 567 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 568 | switch (sms_vpf) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 569 | case GSM340_TP_VPF_RELATIVE: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 570 | sms_vp = smsp++; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 571 | break; |
| 572 | case GSM340_TP_VPF_ABSOLUTE: |
| 573 | case GSM340_TP_VPF_ENHANCED: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 574 | sms_vp = smsp; |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 575 | /* the additional functionality indicator... */ |
Steffen Neubauer | ac0c13c | 2009-12-05 12:44:41 +0100 | [diff] [blame] | 576 | if (sms_vpf == GSM340_TP_VPF_ENHANCED && *smsp & (1<<7)) |
| 577 | smsp++; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 578 | smsp += 7; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 579 | break; |
Daniel Willmann | 58c83d8 | 2009-08-13 03:40:49 +0200 | [diff] [blame] | 580 | case GSM340_TP_VPF_NONE: |
| 581 | sms_vp = 0; |
| 582 | break; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 583 | default: |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 584 | LOGP(DSMS, LOGL_NOTICE, |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 585 | "SMS Validity period not implemented: 0x%02x\n", sms_vpf); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 586 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 587 | } |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 588 | gsms->user_data_len = *smsp++; |
| 589 | if (gsms->user_data_len) { |
| 590 | memcpy(gsms->user_data, smsp, gsms->user_data_len); |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 591 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 592 | switch (sms_alphabet) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 593 | case DCS_7BIT_DEFAULT: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 594 | gsm_7bit_decode(gsms->text, smsp, gsms->user_data_len); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 595 | break; |
| 596 | case DCS_8BIT_DATA: |
| 597 | case DCS_UCS2: |
| 598 | case DCS_NONE: |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 599 | break; |
| 600 | } |
| 601 | } |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 602 | |
Holger Hans Peter Freyther | 4f83409 | 2010-06-16 14:16:47 +0800 | [diff] [blame] | 603 | gsms->sender = subscr_get(conn->subscr); |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 604 | |
| 605 | LOGP(DSMS, LOGL_INFO, "RX SMS: Sender: %s, MTI: 0x%02x, VPF: 0x%02x, " |
| 606 | "MR: 0x%02x PID: 0x%02x, DCS: 0x%02x, DA: %s, " |
| 607 | "UserDataLength: 0x%02x, UserData: \"%s\"\n", |
| 608 | subscr_name(gsms->sender), sms_mti, sms_vpf, gsms->msg_ref, |
| 609 | gsms->protocol_id, gsms->data_coding_scheme, gsms->dest_addr, |
| 610 | gsms->user_data_len, |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 611 | sms_alphabet == DCS_7BIT_DEFAULT ? gsms->text : |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 612 | hexdump(gsms->user_data, gsms->user_data_len)); |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 613 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 614 | gsms->validity_minutes = gsm340_validity_period(sms_vpf, sms_vp); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 615 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 616 | dispatch_signal(SS_SMS, 0, gsms); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 617 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 618 | /* determine gsms->receiver based on dialled number */ |
Holger Hans Peter Freyther | 18b63f4 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 619 | gsms->receiver = subscr_get_by_extension(conn->bts->network, gsms->dest_addr); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 620 | if (!gsms->receiver) { |
| 621 | rc = 1; /* cause 1: unknown subscriber */ |
Holger Hans Peter Freyther | 18b63f4 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 622 | counter_inc(conn->bts->network->stats.sms.no_receiver); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 623 | goto out; |
| 624 | } |
| 625 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 626 | switch (sms_mti) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 627 | case GSM340_SMS_SUBMIT_MS2SC: |
| 628 | /* MS is submitting a SMS */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 629 | rc = gsm340_rx_sms_submit(msg, gsms); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 630 | break; |
| 631 | case GSM340_SMS_COMMAND_MS2SC: |
| 632 | case GSM340_SMS_DELIVER_REP_MS2SC: |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 633 | LOGP(DSMS, LOGL_NOTICE, "Unimplemented MTI 0x%02x\n", sms_mti); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 634 | rc = GSM411_RP_CAUSE_IE_NOTEXIST; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 635 | break; |
| 636 | default: |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 637 | LOGP(DSMS, LOGL_NOTICE, "Undefined MTI 0x%02x\n", sms_mti); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 638 | rc = GSM411_RP_CAUSE_IE_NOTEXIST; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 639 | break; |
| 640 | } |
| 641 | |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 642 | if (!rc && !gsms->receiver) |
| 643 | rc = GSM411_RP_CAUSE_MO_NUM_UNASSIGNED; |
| 644 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 645 | out: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 646 | sms_free(gsms); |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 647 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 648 | return rc; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 651 | static int gsm411_send_rp_ack(struct gsm_trans *trans, u_int8_t msg_ref) |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 652 | { |
| 653 | struct msgb *msg = gsm411_msgb_alloc(); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 654 | |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 655 | DEBUGP(DSMS, "TX: SMS RP ACK\n"); |
| 656 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 657 | 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] | 658 | } |
| 659 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 660 | static int gsm411_send_rp_error(struct gsm_trans *trans, |
| 661 | u_int8_t msg_ref, u_int8_t cause) |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 662 | { |
| 663 | struct msgb *msg = gsm411_msgb_alloc(); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 664 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 665 | msgb_tv_put(msg, 1, cause); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 666 | |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 667 | LOGP(DSMS, LOGL_NOTICE, "TX: SMS RP ERROR, cause %d (%s)\n", cause, |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 668 | get_value_string(rp_cause_strs, cause)); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 669 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 670 | 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] | 671 | } |
| 672 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 673 | /* Receive a 04.11 TPDU inside RP-DATA / user data */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 674 | static int gsm411_rx_rp_ud(struct msgb *msg, struct gsm_trans *trans, |
| 675 | struct gsm411_rp_hdr *rph, |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 676 | u_int8_t src_len, u_int8_t *src, |
| 677 | u_int8_t dst_len, u_int8_t *dst, |
| 678 | u_int8_t tpdu_len, u_int8_t *tpdu) |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 679 | { |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 680 | int rc = 0; |
| 681 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 682 | if (src_len && src) |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 683 | LOGP(DSMS, LOGL_ERROR, "RP-DATA (MO) with SRC ?!?\n"); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 684 | |
| 685 | if (!dst_len || !dst || !tpdu_len || !tpdu) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 686 | LOGP(DSMS, LOGL_ERROR, |
| 687 | "RP-DATA (MO) without DST or TPDU ?!?\n"); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 688 | gsm411_send_rp_error(trans, rph->msg_ref, |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 689 | GSM411_RP_CAUSE_INV_MAND_INF); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 690 | return -EIO; |
| 691 | } |
Harald Welte | e9dd9b0 | 2010-04-30 14:27:05 +0200 | [diff] [blame] | 692 | msg->l4h = tpdu; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 693 | |
| 694 | DEBUGP(DSMS, "DST(%u,%s)\n", dst_len, hexdump(dst, dst_len)); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 695 | |
Holger Hans Peter Freyther | 18b63f4 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 696 | rc = gsm340_rx_tpdu(trans->conn, msg); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 697 | if (rc == 0) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 698 | return gsm411_send_rp_ack(trans, rph->msg_ref); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 699 | else if (rc > 0) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 700 | return gsm411_send_rp_error(trans, rph->msg_ref, rc); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 701 | else |
| 702 | return rc; |
| 703 | } |
| 704 | |
| 705 | /* 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] | 706 | static int gsm411_rx_rp_data(struct msgb *msg, struct gsm_trans *trans, |
| 707 | struct gsm411_rp_hdr *rph) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 708 | { |
| 709 | u_int8_t src_len, dst_len, rpud_len; |
| 710 | u_int8_t *src = NULL, *dst = NULL , *rp_ud = NULL; |
| 711 | |
| 712 | /* in the MO case, this should always be zero length */ |
| 713 | src_len = rph->data[0]; |
| 714 | if (src_len) |
| 715 | src = &rph->data[1]; |
| 716 | |
| 717 | dst_len = rph->data[1+src_len]; |
| 718 | if (dst_len) |
| 719 | dst = &rph->data[1+src_len+1]; |
| 720 | |
| 721 | rpud_len = rph->data[1+src_len+1+dst_len]; |
| 722 | if (rpud_len) |
| 723 | rp_ud = &rph->data[1+src_len+1+dst_len+1]; |
| 724 | |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 725 | DEBUGP(DSMS, "RX_RP-DATA: src_len=%u, dst_len=%u ud_len=%u\n", |
| 726 | src_len, dst_len, rpud_len); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 727 | 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] | 728 | rpud_len, rp_ud); |
| 729 | } |
| 730 | |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 731 | /* 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] | 732 | static int gsm411_rx_rp_ack(struct msgb *msg, struct gsm_trans *trans, |
| 733 | struct gsm411_rp_hdr *rph) |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 734 | { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 735 | struct gsm_sms *sms = trans->sms.sms; |
| 736 | |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 737 | /* Acnkowledgement to MT RP_DATA, i.e. the MS confirms it |
| 738 | * successfully received a SMS. We can now safely mark it as |
| 739 | * transmitted */ |
| 740 | |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 741 | if (!trans->sms.is_mt) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 742 | LOGP(DSMS, LOGL_ERROR, "RX RP-ACK on a MO transfer ?\n"); |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 743 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 744 | GSM411_RP_CAUSE_MSG_INCOMP_STATE); |
| 745 | } |
Harald Welte | 3e0f617 | 2009-07-09 23:52:59 +0200 | [diff] [blame] | 746 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 747 | if (!sms) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 748 | LOGP(DSMS, LOGL_ERROR, "RX RP-ACK but no sms in transaction?!?\n"); |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 749 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 750 | GSM411_RP_CAUSE_PROTOCOL_ERR); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /* mark this SMS as sent in database */ |
| 754 | db_sms_mark_sent(sms); |
| 755 | |
| 756 | dispatch_signal(SS_SMS, S_SMS_DELIVERED, sms); |
| 757 | |
| 758 | sms_free(sms); |
| 759 | trans->sms.sms = NULL; |
| 760 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 761 | /* check for more messages for this subscriber */ |
Holger Hans Peter Freyther | 5179c8e | 2010-03-23 07:32:23 +0100 | [diff] [blame] | 762 | sms = db_sms_get_unsent_for_subscr(trans->subscr); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 763 | if (sms) |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 764 | gsm411_send_sms(trans->conn, sms); |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 765 | else |
| 766 | gsm411_release_conn(trans->conn); |
Sylvain Munaut | d6c35f6 | 2009-12-24 13:33:51 +0100 | [diff] [blame] | 767 | |
| 768 | /* free the transaction here */ |
| 769 | trans_free(trans); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 770 | return 0; |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 771 | } |
| 772 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 773 | static int gsm411_rx_rp_error(struct msgb *msg, struct gsm_trans *trans, |
| 774 | struct gsm411_rp_hdr *rph) |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 775 | { |
Holger Hans Peter Freyther | 18b63f4 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 776 | struct gsm_network *net = trans->conn->bts->network; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 777 | struct gsm_sms *sms = trans->sms.sms; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 778 | u_int8_t cause_len = rph->data[0]; |
| 779 | u_int8_t cause = rph->data[1]; |
| 780 | |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 781 | /* Error in response to MT RP_DATA, i.e. the MS did not |
| 782 | * successfully receive the SMS. We need to investigate |
| 783 | * the cause and take action depending on it */ |
| 784 | |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 785 | LOGP(DSMS, 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] | 786 | subscr_name(trans->conn->subscr), cause_len, cause, |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 787 | get_value_string(rp_cause_strs, cause)); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 788 | |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 789 | if (!trans->sms.is_mt) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 790 | LOGP(DSMS, LOGL_ERROR, "RX RP-ERR on a MO transfer ?\n"); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 791 | #if 0 |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 792 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 793 | GSM411_RP_CAUSE_MSG_INCOMP_STATE); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 794 | #endif |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 795 | } |
Harald Welte | 3e0f617 | 2009-07-09 23:52:59 +0200 | [diff] [blame] | 796 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 797 | if (!sms) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 798 | LOGP(DSMS, LOGL_ERROR, |
| 799 | "RX RP-ERR, but no sms in transaction?!?\n"); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 800 | return -EINVAL; |
| 801 | #if 0 |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 802 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 803 | GSM411_RP_CAUSE_PROTOCOL_ERR); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 804 | #endif |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | if (cause == GSM411_RP_CAUSE_MT_MEM_EXCEEDED) { |
| 808 | /* MS has not enough memory to store the message. We need |
| 809 | * to store this in our database and wati for a SMMA message */ |
| 810 | /* FIXME */ |
| 811 | dispatch_signal(SS_SMS, S_SMS_MEM_EXCEEDED, trans->subscr); |
Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 812 | counter_inc(net->stats.sms.rp_err_mem); |
Harald Welte | 24ff6ee | 2009-12-22 00:41:05 +0100 | [diff] [blame] | 813 | } else |
Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 814 | counter_inc(net->stats.sms.rp_err_other); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 815 | |
| 816 | sms_free(sms); |
| 817 | trans->sms.sms = NULL; |
| 818 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 819 | return 0; |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 820 | } |
| 821 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 822 | static int gsm411_rx_rp_smma(struct msgb *msg, struct gsm_trans *trans, |
| 823 | struct gsm411_rp_hdr *rph) |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 824 | { |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 825 | struct gsm_sms *sms; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 826 | int rc; |
| 827 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 828 | rc = gsm411_send_rp_ack(trans, rph->msg_ref); |
| 829 | trans->sms.rp_state = GSM411_RPS_IDLE; |
| 830 | |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 831 | /* MS tells us that it has memory for more SMS, we need |
| 832 | * to check if we have any pending messages for it and then |
| 833 | * transfer those */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 834 | dispatch_signal(SS_SMS, S_SMS_SMMA, trans->subscr); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 835 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 836 | /* check for more messages for this subscriber */ |
Holger Hans Peter Freyther | 5179c8e | 2010-03-23 07:32:23 +0100 | [diff] [blame] | 837 | sms = db_sms_get_unsent_for_subscr(trans->subscr); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 838 | if (sms) |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 839 | gsm411_send_sms(trans->conn, sms); |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 840 | else |
| 841 | gsm411_release_conn(trans->conn); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 842 | |
| 843 | return rc; |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 844 | } |
| 845 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 846 | static int gsm411_rx_cp_data(struct msgb *msg, struct gsm48_hdr *gh, |
| 847 | struct gsm_trans *trans) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 848 | { |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 849 | struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data; |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 850 | u_int8_t msg_type = rp_data->msg_type & 0x07; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 851 | int rc = 0; |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 852 | |
| 853 | switch (msg_type) { |
| 854 | case GSM411_MT_RP_DATA_MO: |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 855 | DEBUGP(DSMS, "RX SMS RP-DATA (MO)\n"); |
| 856 | /* start TR2N and enter 'wait to send RP-ACK state' */ |
| 857 | trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK; |
| 858 | rc = gsm411_rx_rp_data(msg, trans, rp_data); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 859 | break; |
| 860 | case GSM411_MT_RP_ACK_MO: |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 861 | DEBUGP(DSMS,"RX SMS RP-ACK (MO)\n"); |
| 862 | rc = gsm411_rx_rp_ack(msg, trans, rp_data); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 863 | break; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 864 | case GSM411_MT_RP_SMMA_MO: |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 865 | DEBUGP(DSMS, "RX SMS RP-SMMA\n"); |
| 866 | /* start TR2N and enter 'wait to send RP-ACK state' */ |
| 867 | trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK; |
| 868 | rc = gsm411_rx_rp_smma(msg, trans, rp_data); |
| 869 | break; |
| 870 | case GSM411_MT_RP_ERROR_MO: |
| 871 | rc = gsm411_rx_rp_error(msg, trans, rp_data); |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 872 | break; |
| 873 | default: |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 874 | LOGP(DSMS, LOGL_NOTICE, "Invalid RP type 0x%02x\n", msg_type); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 875 | rc = gsm411_send_rp_error(trans, rp_data->msg_ref, |
| 876 | GSM411_RP_CAUSE_MSGTYPE_NOTEXIST); |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 877 | break; |
| 878 | } |
| 879 | |
| 880 | return rc; |
| 881 | } |
| 882 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 883 | /* send CP-ACK to given transaction */ |
| 884 | static int gsm411_tx_cp_ack(struct gsm_trans *trans) |
| 885 | { |
| 886 | struct msgb *msg = gsm411_msgb_alloc(); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 887 | int rc; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 888 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 889 | rc = gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ACK); |
| 890 | |
| 891 | if (trans->sms.is_mt) { |
| 892 | /* If this is a MT SMS DELIVER, we can clear transaction here */ |
| 893 | trans->sms.cp_state = GSM411_CPS_IDLE; |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 894 | //trans_free(trans); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 895 | } |
Holger Hans Peter Freyther | 09e364b | 2009-08-10 07:59:27 +0200 | [diff] [blame] | 896 | |
| 897 | return rc; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | static int gsm411_tx_cp_error(struct gsm_trans *trans, u_int8_t cause) |
| 901 | { |
| 902 | struct msgb *msg = gsm411_msgb_alloc(); |
| 903 | u_int8_t *causep; |
| 904 | |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 905 | LOGP(DSMS, LOGL_NOTICE, "TX CP-ERROR, cause %d (%s)\n", cause, |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 906 | get_value_string(cp_cause_strs, cause)); |
| 907 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 908 | causep = msgb_put(msg, 1); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 909 | *causep = cause; |
| 910 | |
| 911 | return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ERROR); |
| 912 | } |
| 913 | |
| 914 | /* 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] | 915 | int gsm0411_rcv_sms(struct gsm_subscriber_connection *conn, |
| 916 | struct msgb *msg, u_int8_t link_id) |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 917 | { |
| 918 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 919 | u_int8_t msg_type = gh->msg_type; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 920 | u_int8_t transaction_id = ((gh->proto_discr >> 4) ^ 0x8); /* flip */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 921 | struct gsm_trans *trans; |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 922 | int rc = 0; |
| 923 | |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 924 | if (!conn->subscr) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 925 | return -EIO; |
| 926 | /* FIXME: send some error message */ |
| 927 | |
Sylvain Munaut | c56a141 | 2009-12-18 18:28:08 +0100 | [diff] [blame] | 928 | DEBUGP(DSMS, "trans_id=%x ", transaction_id); |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 929 | trans = trans_find_by_id(conn->subscr, GSM48_PDISC_SMS, |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 930 | transaction_id); |
| 931 | if (!trans) { |
Sylvain Munaut | feaca92 | 2009-12-18 18:28:09 +0100 | [diff] [blame] | 932 | DEBUGPC(DSMS, "(new) "); |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 933 | trans = trans_alloc(conn->subscr, GSM48_PDISC_SMS, |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 934 | transaction_id, new_callref++); |
| 935 | if (!trans) { |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 936 | DEBUGPC(DSMS, "No memory for trans\n"); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 937 | /* FIXME: send some error message */ |
| 938 | return -ENOMEM; |
| 939 | } |
| 940 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 941 | trans->sms.rp_state = GSM411_RPS_IDLE; |
| 942 | trans->sms.is_mt = 0; |
Harald Welte (local) | daef606 | 2009-08-14 11:41:12 +0200 | [diff] [blame] | 943 | trans->sms.link_id = link_id; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 944 | |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 945 | trans->conn = conn; |
Holger Hans Peter Freyther | e95d482 | 2010-03-23 07:00:22 +0100 | [diff] [blame] | 946 | use_subscr_con(trans->conn); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 947 | } |
| 948 | |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 949 | switch(msg_type) { |
| 950 | case GSM411_MT_CP_DATA: |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 951 | DEBUGPC(DSMS, "RX SMS CP-DATA\n"); |
Sylvain Munaut | 258e2f9 | 2009-12-24 16:47:08 +0100 | [diff] [blame] | 952 | |
| 953 | /* 5.4: For MO, if a CP-DATA is received for a new |
| 954 | * transaction, equals reception of an implicit |
| 955 | * last CP-ACK for previous transaction */ |
| 956 | if (trans->sms.cp_state == GSM411_CPS_IDLE) { |
| 957 | int i; |
| 958 | struct gsm_trans *ptrans; |
| 959 | |
| 960 | /* Scan through all remote initiated transactions */ |
| 961 | for (i=8; i<15; i++) { |
| 962 | if (i == transaction_id) |
| 963 | continue; |
| 964 | |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 965 | ptrans = trans_find_by_id(conn->subscr, |
Sylvain Munaut | 258e2f9 | 2009-12-24 16:47:08 +0100 | [diff] [blame] | 966 | GSM48_PDISC_SMS, i); |
| 967 | if (!ptrans) |
| 968 | continue; |
| 969 | |
| 970 | DEBUGP(DSMS, "Implicit CP-ACK for trans_id=%x\n", i); |
| 971 | |
| 972 | /* Finish it for good */ |
| 973 | bsc_del_timer(&ptrans->sms.cp_timer); |
| 974 | ptrans->sms.cp_state = GSM411_CPS_IDLE; |
| 975 | trans_free(ptrans); |
| 976 | } |
| 977 | } |
| 978 | |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 979 | /* 5.2.3.1.3: MO state exists when SMC has received |
| 980 | * CP-DATA, including sending of the assoc. CP-ACK */ |
| 981 | /* 5.2.3.2.4: MT state exists when SMC has received |
| 982 | * CP-DATA, including sending of the assoc. CP-ACK */ |
| 983 | trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 984 | |
Harald Welte (local) | daef606 | 2009-08-14 11:41:12 +0200 | [diff] [blame] | 985 | /* SMC instance acknowledges the CP-DATA frame */ |
| 986 | gsm411_tx_cp_ack(trans); |
| 987 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 988 | rc = gsm411_rx_cp_data(msg, gh, trans); |
Harald Welte (local) | daef606 | 2009-08-14 11:41:12 +0200 | [diff] [blame] | 989 | #if 0 |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 990 | /* Send CP-ACK or CP-ERORR in response */ |
| 991 | if (rc < 0) { |
| 992 | rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_NET_FAIL); |
| 993 | } else |
| 994 | rc = gsm411_tx_cp_ack(trans); |
Harald Welte (local) | daef606 | 2009-08-14 11:41:12 +0200 | [diff] [blame] | 995 | #endif |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 996 | break; |
| 997 | case GSM411_MT_CP_ACK: |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 998 | /* previous CP-DATA in this transaction was confirmed */ |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 999 | DEBUGPC(DSMS, "RX SMS CP-ACK\n"); |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 1000 | /* 5.2.3.1.3: MO state exists when SMC has received CP-ACK */ |
| 1001 | /* 5.2.3.2.4: MT state exists when SMC has received CP-ACK */ |
| 1002 | trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED; |
Harald Welte | 4198561 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 1003 | /* Stop TC1* after CP-ACK has been received */ |
| 1004 | bsc_del_timer(&trans->sms.cp_timer); |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 1005 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1006 | if (!trans->sms.is_mt) { |
Holger Hans Peter Freyther | 0afa044 | 2010-06-10 18:08:12 +0800 | [diff] [blame] | 1007 | /* FIXME: we have sent one CP-DATA, which was now |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1008 | * acknowledged. Check if we want to transfer more, |
| 1009 | * i.e. multi-part message */ |
| 1010 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 1011 | trans_free(trans); |
| 1012 | } |
Daniel Willmann | bb16e8e | 2008-12-29 03:53:50 +0000 | [diff] [blame] | 1013 | break; |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 1014 | case GSM411_MT_CP_ERROR: |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 1015 | DEBUGPC(DSMS, "RX SMS CP-ERROR, cause %d (%s)\n", gh->data[0], |
| 1016 | get_value_string(cp_cause_strs, gh->data[0])); |
Harald Welte | 4198561 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 1017 | bsc_del_timer(&trans->sms.cp_timer); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1018 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 1019 | trans_free(trans); |
Daniel Willmann | bb16e8e | 2008-12-29 03:53:50 +0000 | [diff] [blame] | 1020 | break; |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 1021 | default: |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 1022 | DEBUGPC(DSMS, "RX Unimplemented CP msg_type: 0x%02x\n", msg_type); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1023 | rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_MSGTYPE_NOTEXIST); |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 1024 | trans->sms.cp_state = GSM411_CPS_IDLE; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1025 | trans_free(trans); |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 1026 | break; |
| 1027 | } |
| 1028 | |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 1029 | return rc; |
| 1030 | } |
| 1031 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1032 | /* Take a SMS in gsm_sms structure and send it through an already |
| 1033 | * existing lchan. We also assume that the caller ensured this lchan already |
| 1034 | * has a SAPI3 RLL connection! */ |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 1035 | static 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] | 1036 | { |
| 1037 | struct msgb *msg = gsm411_msgb_alloc(); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1038 | struct gsm_trans *trans; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1039 | u_int8_t *data, *rp_ud_len; |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 1040 | u_int8_t msg_ref = 42; |
Sylvain Munaut | c4052cf | 2009-12-24 13:27:36 +0100 | [diff] [blame] | 1041 | int transaction_id; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1042 | int rc; |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 1043 | |
Holger Hans Peter Freyther | 5179c8e | 2010-03-23 07:32:23 +0100 | [diff] [blame] | 1044 | transaction_id = trans_assign_trans_id(conn->subscr, GSM48_PDISC_SMS, 0); |
Harald Welte | 4983921 | 2009-12-22 13:45:58 +0100 | [diff] [blame] | 1045 | if (transaction_id == -1) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 1046 | LOGP(DSMS, LOGL_ERROR, "No available transaction ids\n"); |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 1047 | sms_free(sms); |
Harald Welte | 4983921 | 2009-12-22 13:45:58 +0100 | [diff] [blame] | 1048 | return -EBUSY; |
| 1049 | } |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1050 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1051 | DEBUGP(DSMS, "send_sms_lchan()\n"); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1052 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1053 | /* FIXME: allocate transaction with message reference */ |
Holger Hans Peter Freyther | 5179c8e | 2010-03-23 07:32:23 +0100 | [diff] [blame] | 1054 | trans = trans_alloc(conn->subscr, GSM48_PDISC_SMS, |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1055 | transaction_id, new_callref++); |
| 1056 | if (!trans) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 1057 | LOGP(DSMS, LOGL_ERROR, "No memory for trans\n"); |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 1058 | sms_free(sms); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1059 | /* FIXME: send some error message */ |
| 1060 | return -ENOMEM; |
| 1061 | } |
| 1062 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 1063 | trans->sms.rp_state = GSM411_RPS_IDLE; |
| 1064 | trans->sms.is_mt = 1; |
| 1065 | trans->sms.sms = sms; |
Harald Welte (local) | daef606 | 2009-08-14 11:41:12 +0200 | [diff] [blame] | 1066 | trans->sms.link_id = UM_SAPI_SMS; /* FIXME: main or SACCH ? */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1067 | |
Holger Hans Peter Freyther | 5179c8e | 2010-03-23 07:32:23 +0100 | [diff] [blame] | 1068 | trans->conn = conn; |
Holger Hans Peter Freyther | e95d482 | 2010-03-23 07:00:22 +0100 | [diff] [blame] | 1069 | use_subscr_con(trans->conn); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1070 | |
| 1071 | /* Hardcode SMSC Originating Address for now */ |
Daniel Willmann | a3e2984 | 2008-12-29 16:03:54 +0000 | [diff] [blame] | 1072 | data = (u_int8_t *)msgb_put(msg, 8); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 1073 | data[0] = 0x07; /* originator length == 7 */ |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 1074 | data[1] = 0x91; /* type of number: international, ISDN */ |
| 1075 | data[2] = 0x44; /* 447785016005 */ |
Daniel Willmann | a3e2984 | 2008-12-29 16:03:54 +0000 | [diff] [blame] | 1076 | data[3] = 0x77; |
| 1077 | data[4] = 0x58; |
| 1078 | data[5] = 0x10; |
| 1079 | data[6] = 0x06; |
| 1080 | data[7] = 0x50; |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 1081 | |
| 1082 | /* Hardcoded Destination Address */ |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 1083 | data = (u_int8_t *)msgb_put(msg, 1); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 1084 | data[0] = 0; /* destination length == 0 */ |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 1085 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1086 | /* obtain a pointer for the rp_ud_len, so we can fill it later */ |
| 1087 | rp_ud_len = (u_int8_t *)msgb_put(msg, 1); |
Daniel Willmann | 4a1e879 | 2008-12-29 06:23:56 +0000 | [diff] [blame] | 1088 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1089 | /* generate the 03.40 TPDU */ |
| 1090 | rc = gsm340_gen_tpdu(msg, sms); |
| 1091 | if (rc < 0) { |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 1092 | trans_free(trans); |
| 1093 | sms_free(sms); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1094 | msgb_free(msg); |
| 1095 | return rc; |
| 1096 | } |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 1097 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1098 | *rp_ud_len = rc; |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 1099 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1100 | DEBUGP(DSMS, "TX: SMS DELIVER\n"); |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 1101 | |
Holger Hans Peter Freyther | d4c16b1 | 2010-03-23 07:56:22 +0100 | [diff] [blame] | 1102 | counter_inc(conn->bts->network->stats.sms.delivered); |
Harald Welte | 24ff6ee | 2009-12-22 00:41:05 +0100 | [diff] [blame] | 1103 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1104 | return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_DATA_MT, msg_ref); |
| 1105 | /* FIXME: enter 'wait for RP-ACK' state, start TR1N */ |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 1106 | } |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1107 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1108 | /* paging callback. Here we get called if paging a subscriber has |
| 1109 | * succeeded or failed. */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1110 | static int paging_cb_send_sms(unsigned int hooknum, unsigned int event, |
| 1111 | struct msgb *msg, void *_lchan, void *_sms) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1112 | { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1113 | struct gsm_lchan *lchan = _lchan; |
| 1114 | struct gsm_sms *sms = _sms; |
Holger Hans Peter Freyther | 4cd5158 | 2010-06-14 16:03:03 +0800 | [diff] [blame] | 1115 | int rc = 0; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1116 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1117 | DEBUGP(DSMS, "paging_cb_send_sms(hooknum=%u, event=%u, msg=%p," |
| 1118 | "lchan=%p, sms=%p)\n", hooknum, event, msg, lchan, sms); |
| 1119 | |
| 1120 | if (hooknum != GSM_HOOK_RR_PAGING) |
| 1121 | return -EINVAL; |
| 1122 | |
| 1123 | switch (event) { |
| 1124 | case GSM_PAGING_SUCCEEDED: |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 1125 | use_subscr_con(&lchan->conn); |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 1126 | gsm411_send_sms(&lchan->conn, sms); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1127 | break; |
| 1128 | case GSM_PAGING_EXPIRED: |
Holger Hans Peter Freyther | ca5702a | 2010-06-10 18:20:54 +0800 | [diff] [blame] | 1129 | case GSM_PAGING_OOM: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1130 | sms_free(sms); |
| 1131 | rc = -ETIMEDOUT; |
| 1132 | break; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | return rc; |
| 1136 | } |
| 1137 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1138 | /* high-level function to send a SMS to a given subscriber. The function |
| 1139 | * will take care of paging the subscriber, establishing the RLL SAPI3 |
| 1140 | * connection, etc. */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1141 | int gsm411_send_sms_subscr(struct gsm_subscriber *subscr, |
| 1142 | struct gsm_sms *sms) |
| 1143 | { |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 1144 | struct gsm_subscriber_connection *conn; |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1145 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1146 | /* check if we already have an open lchan to the subscriber. |
| 1147 | * if yes, send the SMS this way */ |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 1148 | conn = connection_for_subscr(subscr); |
| 1149 | if (conn) { |
| 1150 | use_subscr_con(conn); |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 1151 | return gsm411_send_sms(conn, sms); |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 1152 | } |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1153 | |
| 1154 | /* if not, we have to start paging */ |
Holger Hans Peter Freyther | 7afac58 | 2010-06-10 18:21:25 +0800 | [diff] [blame] | 1155 | subscr_get_channel(subscr, RSL_CHANNEED_SDCCH, paging_cb_send_sms, sms); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1156 | return 0; |
| 1157 | } |
Harald Welte | 3366a94 | 2009-07-28 00:44:49 +0200 | [diff] [blame] | 1158 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1159 | static int subscr_sig_cb(unsigned int subsys, unsigned int signal, |
| 1160 | void *handler_data, void *signal_data) |
| 1161 | { |
| 1162 | struct gsm_subscriber *subscr; |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 1163 | struct gsm_subscriber_connection *conn; |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1164 | struct gsm_sms *sms; |
| 1165 | |
| 1166 | switch (signal) { |
| 1167 | case S_SUBSCR_ATTACHED: |
| 1168 | /* A subscriber has attached. Check if there are |
| 1169 | * any pending SMS for him to be delivered */ |
| 1170 | subscr = signal_data; |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 1171 | conn = connection_for_subscr(subscr); |
| 1172 | if (!conn) |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1173 | break; |
| 1174 | sms = db_sms_get_unsent_for_subscr(subscr); |
| 1175 | if (!sms) |
| 1176 | break; |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 1177 | use_subscr_con(conn); |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 1178 | gsm411_send_sms(conn, sms); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1179 | break; |
| 1180 | default: |
| 1181 | break; |
| 1182 | } |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 1186 | void _gsm411_sms_trans_free(struct gsm_trans *trans) |
| 1187 | { |
Holger Hans Peter Freyther | cea722b | 2010-06-15 12:29:37 +0800 | [diff] [blame] | 1188 | if (trans->sms.sms) { |
| 1189 | LOGP(DSMS, LOGL_ERROR, "Transaction contains SMS.\n"); |
| 1190 | sms_free(trans->sms.sms); |
| 1191 | trans->sms.sms = NULL; |
| 1192 | } |
| 1193 | |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 1194 | bsc_del_timer(&trans->sms.cp_timer); |
| 1195 | } |
| 1196 | |
Holger Hans Peter Freyther | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 1197 | void gsm411_sapi_n_reject(struct gsm_subscriber_connection *conn) |
| 1198 | { |
| 1199 | struct gsm_trans *trans, *tmp; |
| 1200 | |
| 1201 | llist_for_each_entry_safe(trans, tmp, &conn->bts->network->trans_list, entry) |
| 1202 | if (trans->conn == conn) { |
| 1203 | struct gsm_sms *sms = trans->sms.sms; |
| 1204 | if (!sms) { |
| 1205 | LOGP(DSMS, LOGL_ERROR, "SAPI Reject but no SMS.\n"); |
| 1206 | continue; |
| 1207 | } |
| 1208 | |
| 1209 | sms_free(sms); |
| 1210 | trans->sms.sms = NULL; |
| 1211 | trans_free(trans); |
| 1212 | } |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 1213 | |
| 1214 | gsm411_release_conn(conn); |
Holger Hans Peter Freyther | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 1215 | } |
| 1216 | |
Harald Welte | 7bfc267 | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 1217 | static __attribute__((constructor)) void on_dso_load_sms(void) |
| 1218 | { |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1219 | register_signal_handler(SS_SUBSCR, subscr_sig_cb, NULL); |
Harald Welte | 7bfc267 | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 1220 | } |