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