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> |
Holger Hans Peter Freyther | 85531cc | 2010-10-06 20:37:09 +0800 | [diff] [blame] | 8 | * (C) 2010 by On-Waves |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 9 | * |
| 10 | * All Rights Reserved |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 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> |
Nico Golde | f11af07 | 2010-09-15 19:49:55 +0200 | [diff] [blame] | 40 | #include <openbsc/db.h> |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 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 | |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 104 | static int gsm411_send_sms(struct gsm_subscriber_connection *conn, struct gsm_sms *sms); |
| 105 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 106 | struct gsm_sms *sms_alloc(void) |
| 107 | { |
| 108 | return talloc_zero(tall_gsms_ctx, struct gsm_sms); |
| 109 | } |
| 110 | |
| 111 | void sms_free(struct gsm_sms *sms) |
| 112 | { |
| 113 | /* drop references to subscriber structure */ |
| 114 | if (sms->sender) |
| 115 | subscr_put(sms->sender); |
| 116 | if (sms->receiver) |
| 117 | subscr_put(sms->receiver); |
| 118 | |
| 119 | talloc_free(sms); |
| 120 | } |
| 121 | |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 122 | /* |
| 123 | * This should be called whenever all SMS to a given subscriber |
| 124 | * on a given connection has been sent. This will inform the higher |
| 125 | * layers that a channel can be given up. |
| 126 | */ |
| 127 | static void gsm411_release_conn(struct gsm_subscriber_connection *conn) |
| 128 | { |
Holger Hans Peter Freyther | bddd152 | 2010-06-16 14:02:41 +0800 | [diff] [blame] | 129 | if (!conn) |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 130 | return; |
| 131 | |
Holger Hans Peter Freyther | bddd152 | 2010-06-16 14:02:41 +0800 | [diff] [blame] | 132 | subscr_put_channel(conn); |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 133 | } |
| 134 | |
Holger Freyther | a553d09 | 2009-01-04 20:16:25 +0000 | [diff] [blame] | 135 | struct msgb *gsm411_msgb_alloc(void) |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 136 | { |
Harald Welte | 966636f | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 137 | return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM, |
| 138 | "GSM 04.11"); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 141 | 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] | 142 | { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 143 | 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] | 144 | msg->l3h = msg->data; |
| 145 | return gsm0808_submit_dtap(conn, msg, link_id); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Harald Welte | 4198561 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 148 | /* SMC TC1* is expired */ |
| 149 | static void cp_timer_expired(void *data) |
| 150 | { |
| 151 | struct gsm_trans *trans = data; |
| 152 | |
| 153 | DEBUGP(DSMS, "SMC Timer TC1* is expired, calling trans_free()\n"); |
| 154 | /* FIXME: we need to re-transmit the last CP-DATA 1..3 times */ |
| 155 | trans_free(trans); |
| 156 | } |
| 157 | |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 158 | /* Prefix msg with a 04.08/04.11 CP header */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 159 | static int gsm411_cp_sendmsg(struct msgb *msg, struct gsm_trans *trans, |
| 160 | u_int8_t msg_type) |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 161 | { |
| 162 | struct gsm48_hdr *gh; |
| 163 | |
| 164 | gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh)); |
| 165 | /* Outgoing needs the highest bit set */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 166 | gh->proto_discr = trans->protocol | (trans->transaction_id<<4); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 167 | gh->msg_type = msg_type; |
| 168 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 169 | /* mobile originating */ |
| 170 | switch (gh->msg_type) { |
| 171 | case GSM411_MT_CP_DATA: |
| 172 | /* 5.2.3.1.2: enter MO-wait for CP-ack */ |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 173 | /* 5.2.3.2.3: enter MT-wait for CP-ACK */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 174 | trans->sms.cp_state = GSM411_CPS_WAIT_CP_ACK; |
Harald Welte | 4198561 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 175 | trans->sms.cp_timer.data = trans; |
| 176 | trans->sms.cp_timer.cb = cp_timer_expired; |
| 177 | /* 5.3.2.1: Set Timer TC1A */ |
| 178 | bsc_schedule_timer(&trans->sms.cp_timer, GSM411_TMR_TC1A); |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 179 | DEBUGP(DSMS, "TX: CP-DATA "); |
| 180 | break; |
| 181 | case GSM411_MT_CP_ACK: |
| 182 | DEBUGP(DSMS, "TX: CP-ACK "); |
| 183 | break; |
| 184 | case GSM411_MT_CP_ERROR: |
Sylvain Munaut | 1d9efd6 | 2009-12-18 18:28:07 +0100 | [diff] [blame] | 185 | DEBUGP(DSMS, "TX: CP-ERROR "); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 189 | DEBUGPC(DSMS, "trans=%x\n", trans->transaction_id); |
| 190 | |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 191 | return gsm411_sendmsg(trans->conn, msg, trans->sms.link_id); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* Prefix msg with a RP-DATA header and send as CP-DATA */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 195 | static int gsm411_rp_sendmsg(struct msgb *msg, struct gsm_trans *trans, |
| 196 | u_int8_t rp_msg_type, u_int8_t rp_msg_ref) |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 197 | { |
| 198 | struct gsm411_rp_hdr *rp; |
Harald Welte | 0d544e7 | 2009-08-10 00:22:19 +0200 | [diff] [blame] | 199 | u_int8_t len = msg->len; |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 200 | |
| 201 | /* GSM 04.11 RP-DATA header */ |
| 202 | rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp)); |
Harald Welte | 0d544e7 | 2009-08-10 00:22:19 +0200 | [diff] [blame] | 203 | rp->len = len + 2; |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 204 | rp->msg_type = rp_msg_type; |
| 205 | rp->msg_ref = rp_msg_ref; /* FIXME: Choose randomly */ |
| 206 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 207 | return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_DATA); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 210 | /* Turn int into semi-octet representation: 98 => 0x89 */ |
| 211 | static u_int8_t bcdify(u_int8_t value) |
| 212 | { |
| 213 | u_int8_t ret; |
Steffen Neubauer | 5727cf4 | 2009-11-11 23:02:07 +0900 | [diff] [blame] | 214 | |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 215 | ret = value / 10; |
| 216 | ret |= (value % 10) << 4; |
| 217 | |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | /* Turn semi-octet representation into int: 0x89 => 98 */ |
| 222 | static u_int8_t unbcdify(u_int8_t value) |
| 223 | { |
| 224 | u_int8_t ret; |
| 225 | |
| 226 | if ((value & 0x0F) > 9 || (value >> 4) > 9) |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 227 | LOGP(DSMS, LOGL_ERROR, |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 228 | "unbcdify got too big nibble: 0x%02X\n", value); |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 229 | |
| 230 | ret = (value&0x0F)*10; |
Steffen Neubauer | ad69d7f | 2009-11-26 23:38:41 +0100 | [diff] [blame] | 231 | ret += value>>4; |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | /* Generate 03.40 TP-SCTS */ |
| 237 | static void gsm340_gen_scts(u_int8_t *scts, time_t time) |
| 238 | { |
| 239 | struct tm *tm = localtime(&time); |
| 240 | |
| 241 | *scts++ = bcdify(tm->tm_year % 100); |
| 242 | *scts++ = bcdify(tm->tm_mon + 1); |
| 243 | *scts++ = bcdify(tm->tm_mday); |
| 244 | *scts++ = bcdify(tm->tm_hour); |
| 245 | *scts++ = bcdify(tm->tm_min); |
| 246 | *scts++ = bcdify(tm->tm_sec); |
| 247 | *scts++ = bcdify(tm->tm_gmtoff/(60*15)); |
| 248 | } |
| 249 | |
| 250 | /* Decode 03.40 TP-SCTS (into utc/gmt timestamp) */ |
| 251 | static time_t gsm340_scts(u_int8_t *scts) |
| 252 | { |
| 253 | struct tm tm; |
| 254 | |
| 255 | u_int8_t yr = unbcdify(*scts++); |
| 256 | |
| 257 | if (yr <= 80) |
| 258 | tm.tm_year = 100 + yr; |
| 259 | else |
| 260 | tm.tm_year = yr; |
| 261 | tm.tm_mon = unbcdify(*scts++) - 1; |
| 262 | tm.tm_mday = unbcdify(*scts++); |
| 263 | tm.tm_hour = unbcdify(*scts++); |
| 264 | tm.tm_min = unbcdify(*scts++); |
| 265 | tm.tm_sec = unbcdify(*scts++); |
| 266 | /* according to gsm 03.40 time zone is |
| 267 | "expressed in quarters of an hour" */ |
| 268 | tm.tm_gmtoff = unbcdify(*scts++) * 15*60; |
| 269 | |
| 270 | return mktime(&tm); |
| 271 | } |
| 272 | |
| 273 | /* Return the default validity period in minutes */ |
| 274 | static unsigned long gsm340_vp_default(void) |
| 275 | { |
| 276 | unsigned long minutes; |
| 277 | /* Default validity: two days */ |
| 278 | minutes = 24 * 60 * 2; |
| 279 | return minutes; |
| 280 | } |
| 281 | |
| 282 | /* Decode validity period format 'relative' */ |
| 283 | static unsigned long gsm340_vp_relative(u_int8_t *sms_vp) |
| 284 | { |
| 285 | /* Chapter 9.2.3.12.1 */ |
| 286 | u_int8_t vp; |
| 287 | unsigned long minutes; |
| 288 | |
| 289 | vp = *(sms_vp); |
| 290 | if (vp <= 143) |
| 291 | minutes = vp + 1 * 5; |
| 292 | else if (vp <= 167) |
| 293 | minutes = 12*60 + (vp-143) * 30; |
| 294 | else if (vp <= 196) |
| 295 | minutes = vp-166 * 60 * 24; |
| 296 | else |
| 297 | minutes = vp-192 * 60 * 24 * 7; |
| 298 | return minutes; |
| 299 | } |
| 300 | |
| 301 | /* Decode validity period format 'absolute' */ |
| 302 | static unsigned long gsm340_vp_absolute(u_int8_t *sms_vp) |
| 303 | { |
| 304 | /* Chapter 9.2.3.12.2 */ |
| 305 | time_t expires, now; |
| 306 | unsigned long minutes; |
| 307 | |
| 308 | expires = gsm340_scts(sms_vp); |
Nico Golde | 4df2ad9 | 2010-06-16 15:22:30 +0200 | [diff] [blame] | 309 | now = time(NULL); |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 310 | if (expires <= now) |
| 311 | minutes = 0; |
| 312 | else |
| 313 | minutes = (expires-now)/60; |
| 314 | return minutes; |
| 315 | } |
| 316 | |
| 317 | /* Decode validity period format 'relative in integer representation' */ |
| 318 | static unsigned long gsm340_vp_relative_integer(u_int8_t *sms_vp) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 319 | { |
| 320 | u_int8_t vp; |
| 321 | unsigned long minutes; |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 322 | vp = *(sms_vp); |
| 323 | if (vp == 0) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 324 | LOGP(DSMS, LOGL_ERROR, |
| 325 | "reserved relative_integer validity period\n"); |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 326 | return gsm340_vp_default(); |
| 327 | } |
| 328 | minutes = vp/60; |
| 329 | return minutes; |
| 330 | } |
| 331 | |
| 332 | /* Decode validity period format 'relative in semi-octet representation' */ |
| 333 | static unsigned long gsm340_vp_relative_semioctet(u_int8_t *sms_vp) |
| 334 | { |
| 335 | unsigned long minutes; |
| 336 | minutes = unbcdify(*sms_vp++)*60; /* hours */ |
| 337 | minutes += unbcdify(*sms_vp++); /* minutes */ |
| 338 | minutes += unbcdify(*sms_vp++)/60; /* seconds */ |
| 339 | return minutes; |
| 340 | } |
| 341 | |
| 342 | /* decode validity period. return minutes */ |
| 343 | static unsigned long gsm340_validity_period(u_int8_t sms_vpf, u_int8_t *sms_vp) |
| 344 | { |
| 345 | u_int8_t fi; /* functionality indicator */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 346 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 347 | switch (sms_vpf) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 348 | case GSM340_TP_VPF_RELATIVE: |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 349 | return gsm340_vp_relative(sms_vp); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 350 | case GSM340_TP_VPF_ABSOLUTE: |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 351 | return gsm340_vp_absolute(sms_vp); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 352 | case GSM340_TP_VPF_ENHANCED: |
| 353 | /* Chapter 9.2.3.12.3 */ |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 354 | fi = *sms_vp++; |
| 355 | /* ignore additional fi */ |
| 356 | if (fi & (1<<7)) sms_vp++; |
| 357 | /* read validity period format */ |
Andreas.Eversberg | 2db4d46 | 2010-06-17 16:32:38 +0200 | [diff] [blame] | 358 | switch (fi & 0x7) { |
| 359 | case 0x0: |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 360 | return gsm340_vp_default(); /* no vpf specified */ |
Andreas.Eversberg | 2db4d46 | 2010-06-17 16:32:38 +0200 | [diff] [blame] | 361 | case 0x1: |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 362 | return gsm340_vp_relative(sms_vp); |
Andreas.Eversberg | 2db4d46 | 2010-06-17 16:32:38 +0200 | [diff] [blame] | 363 | case 0x2: |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 364 | return gsm340_vp_relative_integer(sms_vp); |
Andreas.Eversberg | 2db4d46 | 2010-06-17 16:32:38 +0200 | [diff] [blame] | 365 | case 0x3: |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 366 | return gsm340_vp_relative_semioctet(sms_vp); |
| 367 | default: |
| 368 | /* The GSM spec says that the SC should reject any |
| 369 | unsupported and/or undefined values. FIXME */ |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 370 | LOGP(DSMS, LOGL_ERROR, |
| 371 | "Reserved enhanced validity period format\n"); |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 372 | return gsm340_vp_default(); |
| 373 | } |
Daniel Willmann | 58c83d8 | 2009-08-13 03:40:49 +0200 | [diff] [blame] | 374 | case GSM340_TP_VPF_NONE: |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 375 | default: |
| 376 | return gsm340_vp_default(); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 377 | } |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /* determine coding alphabet dependent on GSM 03.38 Section 4 DCS */ |
| 381 | enum sms_alphabet gsm338_get_sms_alphabet(u_int8_t dcs) |
| 382 | { |
| 383 | u_int8_t cgbits = dcs >> 4; |
| 384 | enum sms_alphabet alpha = DCS_NONE; |
| 385 | |
| 386 | if ((cgbits & 0xc) == 0) { |
Harald Welte | e98c635 | 2010-06-09 15:29:13 +0200 | [diff] [blame] | 387 | if (cgbits & 2) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 388 | LOGP(DSMS, LOGL_NOTICE, |
| 389 | "Compressed SMS not supported yet\n"); |
Harald Welte | e98c635 | 2010-06-09 15:29:13 +0200 | [diff] [blame] | 390 | return 0xffffffff; |
| 391 | } |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 392 | |
Daniel Willmann | d5d5e1d | 2009-08-15 03:01:17 +0200 | [diff] [blame] | 393 | switch ((dcs >> 2)&0x03) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 394 | case 0: |
| 395 | alpha = DCS_7BIT_DEFAULT; |
| 396 | break; |
| 397 | case 1: |
| 398 | alpha = DCS_8BIT_DATA; |
| 399 | break; |
| 400 | case 2: |
| 401 | alpha = DCS_UCS2; |
| 402 | break; |
| 403 | } |
| 404 | } else if (cgbits == 0xc || cgbits == 0xd) |
| 405 | alpha = DCS_7BIT_DEFAULT; |
| 406 | else if (cgbits == 0xe) |
| 407 | alpha = DCS_UCS2; |
| 408 | else if (cgbits == 0xf) { |
| 409 | if (dcs & 4) |
| 410 | alpha = DCS_8BIT_DATA; |
| 411 | else |
| 412 | alpha = DCS_7BIT_DEFAULT; |
| 413 | } |
| 414 | |
| 415 | return alpha; |
| 416 | } |
| 417 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 418 | 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] | 419 | { |
| 420 | if (db_sms_store(gsms) != 0) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 421 | LOGP(DSMS, LOGL_ERROR, "Failed to store SMS in Database\n"); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 422 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 423 | } |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 424 | /* dispatch a signal to tell higher level about it */ |
| 425 | dispatch_signal(SS_SMS, S_SMS_SUBMITTED, gsms); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 426 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 430 | /* generate a TPDU address field compliant with 03.40 sec. 9.1.2.5 */ |
| 431 | static int gsm340_gen_oa(u_int8_t *oa, unsigned int oa_len, |
| 432 | struct gsm_subscriber *subscr) |
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 | int len_in_bytes; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 435 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 436 | oa[1] = 0xb9; /* networks-specific number, private numbering plan */ |
| 437 | |
Harald Welte | 55c8f35 | 2010-03-07 23:40:35 +0100 | [diff] [blame] | 438 | 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] | 439 | |
| 440 | /* GSM 03.40 tells us the length is in 'useful semi-octets' */ |
| 441 | oa[0] = strlen(subscr->extension) & 0xff; |
| 442 | |
| 443 | return len_in_bytes; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 446 | /* generate a msgb containing a TPDU derived from struct gsm_sms, |
| 447 | * returns total size of TPDU */ |
| 448 | static int gsm340_gen_tpdu(struct msgb *msg, struct gsm_sms *sms) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 449 | { |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 450 | u_int8_t *smsp; |
| 451 | u_int8_t oa[12]; /* max len per 03.40 */ |
| 452 | u_int8_t oa_len = 0; |
Daniel Willmann | 9aef145 | 2009-08-13 03:39:07 +0200 | [diff] [blame] | 453 | u_int8_t octet_len; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 454 | unsigned int old_msg_len = msg->len; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 455 | |
| 456 | /* generate first octet with masked bits */ |
| 457 | smsp = msgb_put(msg, 1); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 458 | /* TP-MTI (message type indicator) */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 459 | *smsp = GSM340_SMS_DELIVER_SC2MS; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 460 | /* TP-MMS (more messages to send) */ |
| 461 | if (0 /* FIXME */) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 462 | *smsp |= 0x04; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 463 | /* TP-SRI(deliver)/SRR(submit) */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 464 | if (sms->status_rep_req) |
| 465 | *smsp |= 0x20; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 466 | /* TP-UDHI (indicating TP-UD contains a header) */ |
| 467 | if (sms->ud_hdr_ind) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 468 | *smsp |= 0x40; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 469 | |
| 470 | /* generate originator address */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 471 | oa_len = gsm340_gen_oa(oa, sizeof(oa), sms->sender); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 472 | smsp = msgb_put(msg, oa_len); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 473 | memcpy(smsp, oa, oa_len); |
| 474 | |
| 475 | /* generate TP-PID */ |
| 476 | smsp = msgb_put(msg, 1); |
| 477 | *smsp = sms->protocol_id; |
| 478 | |
| 479 | /* generate TP-DCS */ |
| 480 | smsp = msgb_put(msg, 1); |
| 481 | *smsp = sms->data_coding_scheme; |
| 482 | |
| 483 | /* generate TP-SCTS */ |
| 484 | smsp = msgb_put(msg, 7); |
| 485 | gsm340_gen_scts(smsp, time(NULL)); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 486 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 487 | /* generate TP-UDL */ |
| 488 | smsp = msgb_put(msg, 1); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 489 | *smsp = sms->user_data_len; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 490 | |
| 491 | /* generate TP-UD */ |
Daniel Willmann | 6b024cb | 2009-08-15 03:01:46 +0200 | [diff] [blame] | 492 | switch (gsm338_get_sms_alphabet(sms->data_coding_scheme)) { |
| 493 | case DCS_7BIT_DEFAULT: |
Daniel Willmann | 9aef145 | 2009-08-13 03:39:07 +0200 | [diff] [blame] | 494 | octet_len = sms->user_data_len*7/8; |
| 495 | if (sms->user_data_len*7%8 != 0) |
| 496 | octet_len++; |
Daniel Willmann | 6b1e822 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 497 | /* Warning, user_data_len indicates the amount of septets |
| 498 | * (characters), we need amount of octets occupied */ |
Daniel Willmann | 9aef145 | 2009-08-13 03:39:07 +0200 | [diff] [blame] | 499 | smsp = msgb_put(msg, octet_len); |
| 500 | memcpy(smsp, sms->user_data, octet_len); |
Daniel Willmann | 6b024cb | 2009-08-15 03:01:46 +0200 | [diff] [blame] | 501 | break; |
| 502 | case DCS_UCS2: |
| 503 | case DCS_8BIT_DATA: |
| 504 | smsp = msgb_put(msg, sms->user_data_len); |
| 505 | memcpy(smsp, sms->user_data, sms->user_data_len); |
| 506 | break; |
| 507 | default: |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 508 | LOGP(DSMS, LOGL_NOTICE, "Unhandled Data Coding Scheme: 0x%02X\n", |
| 509 | sms->data_coding_scheme); |
Daniel Willmann | 6b024cb | 2009-08-15 03:01:46 +0200 | [diff] [blame] | 510 | break; |
Daniel Willmann | 6b1e822 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 511 | } |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 512 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 513 | return msg->len - old_msg_len; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 516 | /* process an incoming TPDU (called from RP-DATA) |
| 517 | * 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] | 518 | 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] | 519 | { |
| 520 | u_int8_t *smsp = msgb_sms(msg); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 521 | struct gsm_sms *gsms; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 522 | u_int8_t sms_mti, sms_mms, sms_vpf, sms_alphabet, sms_rp; |
| 523 | u_int8_t *sms_vp; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 524 | u_int8_t da_len_bytes; |
| 525 | u_int8_t address_lv[12]; /* according to 03.40 / 9.1.2.5 */ |
| 526 | int rc = 0; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 527 | |
Holger Hans Peter Freyther | 18b63f4 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 528 | counter_inc(conn->bts->network->stats.sms.submitted); |
Harald Welte | 24ff6ee | 2009-12-22 00:41:05 +0100 | [diff] [blame] | 529 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 530 | gsms = sms_alloc(); |
| 531 | if (!gsms) |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 532 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 533 | |
| 534 | /* invert those fields where 0 means active/present */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 535 | sms_mti = *smsp & 0x03; |
| 536 | sms_mms = !!(*smsp & 0x04); |
| 537 | sms_vpf = (*smsp & 0x18) >> 3; |
| 538 | gsms->status_rep_req = (*smsp & 0x20); |
| 539 | gsms->ud_hdr_ind = (*smsp & 0x40); |
| 540 | sms_rp = (*smsp & 0x80); |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 541 | |
| 542 | smsp++; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 543 | gsms->msg_ref = *smsp++; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 544 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 545 | /* length in bytes of the destination address */ |
| 546 | da_len_bytes = 2 + *smsp/2 + *smsp%2; |
| 547 | if (da_len_bytes > 12) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 548 | LOGP(DSMS, LOGL_ERROR, "Destination Address > 12 bytes ?!?\n"); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 549 | rc = GSM411_RP_CAUSE_SEMANT_INC_MSG; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 550 | goto out; |
| 551 | } |
Harald Welte | 3cfdb22 | 2009-06-12 02:42:11 +0800 | [diff] [blame] | 552 | memset(address_lv, 0, sizeof(address_lv)); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 553 | memcpy(address_lv, smsp, da_len_bytes); |
| 554 | /* mangle first byte to reflect length in bytes, not digits */ |
Harald Welte | 3cfdb22 | 2009-06-12 02:42:11 +0800 | [diff] [blame] | 555 | address_lv[0] = da_len_bytes - 1; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 556 | /* convert to real number */ |
Harald Welte | 55c8f35 | 2010-03-07 23:40:35 +0100 | [diff] [blame] | 557 | 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] | 558 | smsp += da_len_bytes; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 559 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 560 | gsms->protocol_id = *smsp++; |
| 561 | gsms->data_coding_scheme = *smsp++; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 562 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 563 | sms_alphabet = gsm338_get_sms_alphabet(gsms->data_coding_scheme); |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 564 | if (sms_alphabet == 0xffffffff) { |
| 565 | sms_free(gsms); |
Harald Welte | e98c635 | 2010-06-09 15:29:13 +0200 | [diff] [blame] | 566 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 567 | } |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 568 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 569 | switch (sms_vpf) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 570 | case GSM340_TP_VPF_RELATIVE: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 571 | sms_vp = smsp++; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 572 | break; |
| 573 | case GSM340_TP_VPF_ABSOLUTE: |
| 574 | case GSM340_TP_VPF_ENHANCED: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 575 | sms_vp = smsp; |
Steffen Neubauer | f326267 | 2009-11-26 12:28:41 +0100 | [diff] [blame] | 576 | /* the additional functionality indicator... */ |
Steffen Neubauer | ac0c13c | 2009-12-05 12:44:41 +0100 | [diff] [blame] | 577 | if (sms_vpf == GSM340_TP_VPF_ENHANCED && *smsp & (1<<7)) |
| 578 | smsp++; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 579 | smsp += 7; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 580 | break; |
Daniel Willmann | 58c83d8 | 2009-08-13 03:40:49 +0200 | [diff] [blame] | 581 | case GSM340_TP_VPF_NONE: |
| 582 | sms_vp = 0; |
| 583 | break; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 584 | default: |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 585 | LOGP(DSMS, LOGL_NOTICE, |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 586 | "SMS Validity period not implemented: 0x%02x\n", sms_vpf); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 587 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 588 | } |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 589 | gsms->user_data_len = *smsp++; |
| 590 | if (gsms->user_data_len) { |
| 591 | memcpy(gsms->user_data, smsp, gsms->user_data_len); |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 592 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 593 | switch (sms_alphabet) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 594 | case DCS_7BIT_DEFAULT: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 595 | gsm_7bit_decode(gsms->text, smsp, gsms->user_data_len); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 596 | break; |
| 597 | case DCS_8BIT_DATA: |
| 598 | case DCS_UCS2: |
| 599 | case DCS_NONE: |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 600 | break; |
| 601 | } |
| 602 | } |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 603 | |
Holger Hans Peter Freyther | 4f83409 | 2010-06-16 14:16:47 +0800 | [diff] [blame] | 604 | gsms->sender = subscr_get(conn->subscr); |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 605 | |
| 606 | LOGP(DSMS, LOGL_INFO, "RX SMS: Sender: %s, MTI: 0x%02x, VPF: 0x%02x, " |
| 607 | "MR: 0x%02x PID: 0x%02x, DCS: 0x%02x, DA: %s, " |
| 608 | "UserDataLength: 0x%02x, UserData: \"%s\"\n", |
| 609 | subscr_name(gsms->sender), sms_mti, sms_vpf, gsms->msg_ref, |
| 610 | gsms->protocol_id, gsms->data_coding_scheme, gsms->dest_addr, |
| 611 | gsms->user_data_len, |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 612 | sms_alphabet == DCS_7BIT_DEFAULT ? gsms->text : |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 613 | hexdump(gsms->user_data, gsms->user_data_len)); |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 614 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 615 | gsms->validity_minutes = gsm340_validity_period(sms_vpf, sms_vp); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 616 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 617 | dispatch_signal(SS_SMS, 0, gsms); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 618 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 619 | /* determine gsms->receiver based on dialled number */ |
Holger Hans Peter Freyther | 18b63f4 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 620 | gsms->receiver = subscr_get_by_extension(conn->bts->network, gsms->dest_addr); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 621 | if (!gsms->receiver) { |
| 622 | rc = 1; /* cause 1: unknown subscriber */ |
Holger Hans Peter Freyther | 18b63f4 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 623 | counter_inc(conn->bts->network->stats.sms.no_receiver); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 624 | goto out; |
| 625 | } |
| 626 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 627 | switch (sms_mti) { |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 628 | case GSM340_SMS_SUBMIT_MS2SC: |
| 629 | /* MS is submitting a SMS */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 630 | rc = gsm340_rx_sms_submit(msg, gsms); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 631 | break; |
| 632 | case GSM340_SMS_COMMAND_MS2SC: |
| 633 | case GSM340_SMS_DELIVER_REP_MS2SC: |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 634 | LOGP(DSMS, LOGL_NOTICE, "Unimplemented MTI 0x%02x\n", sms_mti); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 635 | rc = GSM411_RP_CAUSE_IE_NOTEXIST; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 636 | break; |
| 637 | default: |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 638 | LOGP(DSMS, LOGL_NOTICE, "Undefined MTI 0x%02x\n", sms_mti); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 639 | rc = GSM411_RP_CAUSE_IE_NOTEXIST; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 643 | if (!rc && !gsms->receiver) |
| 644 | rc = GSM411_RP_CAUSE_MO_NUM_UNASSIGNED; |
| 645 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 646 | out: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 647 | sms_free(gsms); |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 648 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 649 | return rc; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 652 | 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] | 653 | { |
| 654 | struct msgb *msg = gsm411_msgb_alloc(); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 655 | |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 656 | DEBUGP(DSMS, "TX: SMS RP ACK\n"); |
| 657 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 658 | 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] | 659 | } |
| 660 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 661 | static int gsm411_send_rp_error(struct gsm_trans *trans, |
| 662 | u_int8_t msg_ref, u_int8_t cause) |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 663 | { |
| 664 | struct msgb *msg = gsm411_msgb_alloc(); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 665 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 666 | msgb_tv_put(msg, 1, cause); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 667 | |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 668 | 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] | 669 | get_value_string(rp_cause_strs, cause)); |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 670 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 671 | 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] | 672 | } |
| 673 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 674 | /* Receive a 04.11 TPDU inside RP-DATA / user data */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 675 | static int gsm411_rx_rp_ud(struct msgb *msg, struct gsm_trans *trans, |
| 676 | struct gsm411_rp_hdr *rph, |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 677 | u_int8_t src_len, u_int8_t *src, |
| 678 | u_int8_t dst_len, u_int8_t *dst, |
| 679 | u_int8_t tpdu_len, u_int8_t *tpdu) |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 680 | { |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 681 | int rc = 0; |
| 682 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 683 | if (src_len && src) |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 684 | LOGP(DSMS, LOGL_ERROR, "RP-DATA (MO) with SRC ?!?\n"); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 685 | |
| 686 | if (!dst_len || !dst || !tpdu_len || !tpdu) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 687 | LOGP(DSMS, LOGL_ERROR, |
| 688 | "RP-DATA (MO) without DST or TPDU ?!?\n"); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 689 | gsm411_send_rp_error(trans, rph->msg_ref, |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 690 | GSM411_RP_CAUSE_INV_MAND_INF); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 691 | return -EIO; |
| 692 | } |
Harald Welte | e9dd9b0 | 2010-04-30 14:27:05 +0200 | [diff] [blame] | 693 | msg->l4h = tpdu; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 694 | |
| 695 | DEBUGP(DSMS, "DST(%u,%s)\n", dst_len, hexdump(dst, dst_len)); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 696 | |
Holger Hans Peter Freyther | 18b63f4 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 697 | rc = gsm340_rx_tpdu(trans->conn, msg); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 698 | if (rc == 0) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 699 | return gsm411_send_rp_ack(trans, rph->msg_ref); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 700 | else if (rc > 0) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 701 | return gsm411_send_rp_error(trans, rph->msg_ref, rc); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 702 | else |
| 703 | return rc; |
| 704 | } |
| 705 | |
| 706 | /* 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] | 707 | static int gsm411_rx_rp_data(struct msgb *msg, struct gsm_trans *trans, |
| 708 | struct gsm411_rp_hdr *rph) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 709 | { |
| 710 | u_int8_t src_len, dst_len, rpud_len; |
| 711 | u_int8_t *src = NULL, *dst = NULL , *rp_ud = NULL; |
| 712 | |
| 713 | /* in the MO case, this should always be zero length */ |
| 714 | src_len = rph->data[0]; |
| 715 | if (src_len) |
| 716 | src = &rph->data[1]; |
| 717 | |
| 718 | dst_len = rph->data[1+src_len]; |
| 719 | if (dst_len) |
| 720 | dst = &rph->data[1+src_len+1]; |
| 721 | |
| 722 | rpud_len = rph->data[1+src_len+1+dst_len]; |
| 723 | if (rpud_len) |
| 724 | rp_ud = &rph->data[1+src_len+1+dst_len+1]; |
| 725 | |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 726 | DEBUGP(DSMS, "RX_RP-DATA: src_len=%u, dst_len=%u ud_len=%u\n", |
| 727 | src_len, dst_len, rpud_len); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 728 | 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] | 729 | rpud_len, rp_ud); |
| 730 | } |
| 731 | |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 732 | /* 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] | 733 | static int gsm411_rx_rp_ack(struct msgb *msg, struct gsm_trans *trans, |
| 734 | struct gsm411_rp_hdr *rph) |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 735 | { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 736 | struct gsm_sms *sms = trans->sms.sms; |
| 737 | |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 738 | /* Acnkowledgement to MT RP_DATA, i.e. the MS confirms it |
| 739 | * successfully received a SMS. We can now safely mark it as |
| 740 | * transmitted */ |
| 741 | |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 742 | if (!trans->sms.is_mt) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 743 | LOGP(DSMS, LOGL_ERROR, "RX RP-ACK on a MO transfer ?\n"); |
Harald Welte | cb8f443 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 744 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 745 | GSM411_RP_CAUSE_MSG_INCOMP_STATE); |
| 746 | } |
Harald Welte | 3e0f617 | 2009-07-09 23:52:59 +0200 | [diff] [blame] | 747 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 748 | if (!sms) { |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 749 | 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] | 750 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 751 | GSM411_RP_CAUSE_PROTOCOL_ERR); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /* mark this SMS as sent in database */ |
| 755 | db_sms_mark_sent(sms); |
| 756 | |
| 757 | dispatch_signal(SS_SMS, S_SMS_DELIVERED, sms); |
| 758 | |
| 759 | sms_free(sms); |
| 760 | trans->sms.sms = NULL; |
| 761 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 762 | /* check for more messages for this subscriber */ |
Holger Hans Peter Freyther | 5179c8e | 2010-03-23 07:32:23 +0100 | [diff] [blame] | 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 | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 765 | gsm411_send_sms(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 | sms = db_sms_get_unsent_for_subscr(trans->subscr); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 839 | if (sms) |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 840 | gsm411_send_sms(trans->conn, sms); |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 841 | else |
| 842 | gsm411_release_conn(trans->conn); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 843 | |
| 844 | return rc; |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 845 | } |
| 846 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 847 | static int gsm411_rx_cp_data(struct msgb *msg, struct gsm48_hdr *gh, |
| 848 | struct gsm_trans *trans) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 849 | { |
Daniel Willmann | 471712b | 2008-12-29 01:54:02 +0000 | [diff] [blame] | 850 | struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data; |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 851 | u_int8_t msg_type = rp_data->msg_type & 0x07; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 852 | int rc = 0; |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 853 | |
| 854 | switch (msg_type) { |
| 855 | case GSM411_MT_RP_DATA_MO: |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 856 | DEBUGP(DSMS, "RX SMS RP-DATA (MO)\n"); |
| 857 | /* start TR2N and enter 'wait to send RP-ACK state' */ |
| 858 | trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK; |
| 859 | rc = gsm411_rx_rp_data(msg, trans, rp_data); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 860 | break; |
| 861 | case GSM411_MT_RP_ACK_MO: |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 862 | DEBUGP(DSMS,"RX SMS RP-ACK (MO)\n"); |
| 863 | rc = gsm411_rx_rp_ack(msg, trans, rp_data); |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 864 | break; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 865 | case GSM411_MT_RP_SMMA_MO: |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 866 | DEBUGP(DSMS, "RX SMS RP-SMMA\n"); |
| 867 | /* start TR2N and enter 'wait to send RP-ACK state' */ |
| 868 | trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK; |
| 869 | rc = gsm411_rx_rp_smma(msg, trans, rp_data); |
| 870 | break; |
| 871 | case GSM411_MT_RP_ERROR_MO: |
| 872 | rc = gsm411_rx_rp_error(msg, trans, rp_data); |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 873 | break; |
| 874 | default: |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 875 | LOGP(DSMS, LOGL_NOTICE, "Invalid RP type 0x%02x\n", msg_type); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 876 | rc = gsm411_send_rp_error(trans, rp_data->msg_ref, |
| 877 | GSM411_RP_CAUSE_MSGTYPE_NOTEXIST); |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 878 | break; |
| 879 | } |
| 880 | |
| 881 | return rc; |
| 882 | } |
| 883 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 884 | /* send CP-ACK to given transaction */ |
| 885 | static int gsm411_tx_cp_ack(struct gsm_trans *trans) |
| 886 | { |
| 887 | struct msgb *msg = gsm411_msgb_alloc(); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 888 | int rc; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 889 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 890 | rc = gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ACK); |
| 891 | |
| 892 | if (trans->sms.is_mt) { |
| 893 | /* If this is a MT SMS DELIVER, we can clear transaction here */ |
| 894 | trans->sms.cp_state = GSM411_CPS_IDLE; |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 895 | //trans_free(trans); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 896 | } |
Holger Hans Peter Freyther | 09e364b | 2009-08-10 07:59:27 +0200 | [diff] [blame] | 897 | |
| 898 | return rc; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | static int gsm411_tx_cp_error(struct gsm_trans *trans, u_int8_t cause) |
| 902 | { |
| 903 | struct msgb *msg = gsm411_msgb_alloc(); |
| 904 | u_int8_t *causep; |
| 905 | |
Harald Welte | d0cf7ba | 2009-12-24 15:08:18 +0100 | [diff] [blame] | 906 | 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] | 907 | get_value_string(cp_cause_strs, cause)); |
| 908 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 909 | causep = msgb_put(msg, 1); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 910 | *causep = cause; |
| 911 | |
| 912 | return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ERROR); |
| 913 | } |
| 914 | |
| 915 | /* 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] | 916 | int gsm0411_rcv_sms(struct gsm_subscriber_connection *conn, |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 917 | struct msgb *msg) |
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 */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 922 | struct gsm_trans *trans; |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 923 | int rc = 0; |
| 924 | |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 925 | if (!conn->subscr) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 926 | return -EIO; |
| 927 | /* FIXME: send some error message */ |
| 928 | |
Sylvain Munaut | c56a141 | 2009-12-18 18:28:08 +0100 | [diff] [blame] | 929 | DEBUGP(DSMS, "trans_id=%x ", transaction_id); |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 930 | trans = trans_find_by_id(conn->subscr, GSM48_PDISC_SMS, |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 931 | transaction_id); |
| 932 | if (!trans) { |
Sylvain Munaut | feaca92 | 2009-12-18 18:28:09 +0100 | [diff] [blame] | 933 | DEBUGPC(DSMS, "(new) "); |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 934 | trans = trans_alloc(conn->subscr, GSM48_PDISC_SMS, |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 935 | transaction_id, new_callref++); |
| 936 | if (!trans) { |
Harald Welte (local) | c89a511 | 2009-08-14 10:42:43 +0200 | [diff] [blame] | 937 | DEBUGPC(DSMS, "No memory for trans\n"); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 938 | /* FIXME: send some error message */ |
| 939 | return -ENOMEM; |
| 940 | } |
| 941 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 942 | trans->sms.rp_state = GSM411_RPS_IDLE; |
| 943 | trans->sms.is_mt = 0; |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 944 | trans->sms.link_id = UM_SAPI_SMS; |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 945 | |
Holger Hans Peter Freyther | 57c4dec | 2010-06-16 14:24:55 +0800 | [diff] [blame] | 946 | trans->conn = 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; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1069 | |
| 1070 | /* Hardcode SMSC Originating Address for now */ |
Daniel Willmann | a3e2984 | 2008-12-29 16:03:54 +0000 | [diff] [blame] | 1071 | data = (u_int8_t *)msgb_put(msg, 8); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 1072 | data[0] = 0x07; /* originator length == 7 */ |
Harald Welte | b9c758b | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 1073 | data[1] = 0x91; /* type of number: international, ISDN */ |
| 1074 | data[2] = 0x44; /* 447785016005 */ |
Daniel Willmann | a3e2984 | 2008-12-29 16:03:54 +0000 | [diff] [blame] | 1075 | data[3] = 0x77; |
| 1076 | data[4] = 0x58; |
| 1077 | data[5] = 0x10; |
| 1078 | data[6] = 0x06; |
| 1079 | data[7] = 0x50; |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 1080 | |
| 1081 | /* Hardcoded Destination Address */ |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 1082 | data = (u_int8_t *)msgb_put(msg, 1); |
Harald Welte | 87f5d63 | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 1083 | data[0] = 0; /* destination length == 0 */ |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 1084 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1085 | /* obtain a pointer for the rp_ud_len, so we can fill it later */ |
| 1086 | rp_ud_len = (u_int8_t *)msgb_put(msg, 1); |
Daniel Willmann | 4a1e879 | 2008-12-29 06:23:56 +0000 | [diff] [blame] | 1087 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1088 | /* generate the 03.40 TPDU */ |
| 1089 | rc = gsm340_gen_tpdu(msg, sms); |
| 1090 | if (rc < 0) { |
Holger Hans Peter Freyther | 8e78fa7 | 2010-06-14 16:13:16 +0800 | [diff] [blame] | 1091 | trans_free(trans); |
| 1092 | sms_free(sms); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1093 | msgb_free(msg); |
| 1094 | return rc; |
| 1095 | } |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 1096 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1097 | *rp_ud_len = rc; |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 1098 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1099 | DEBUGP(DSMS, "TX: SMS DELIVER\n"); |
Daniel Willmann | 6fe997e | 2008-12-29 04:20:41 +0000 | [diff] [blame] | 1100 | |
Holger Hans Peter Freyther | d4c16b1 | 2010-03-23 07:56:22 +0100 | [diff] [blame] | 1101 | counter_inc(conn->bts->network->stats.sms.delivered); |
Nico Golde | 1e5ed4e | 2010-09-16 17:50:29 +0200 | [diff] [blame] | 1102 | db_sms_inc_deliver_attempts(trans->sms.sms); |
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, |
Holger Hans Peter Freyther | 86481c2 | 2010-06-17 15:05:57 +0800 | [diff] [blame] | 1111 | struct msgb *msg, void *_conn, void *_sms) |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1112 | { |
Holger Hans Peter Freyther | 86481c2 | 2010-06-17 15:05:57 +0800 | [diff] [blame] | 1113 | struct gsm_subscriber_connection *conn = _conn; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 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," |
Holger Hans Peter Freyther | 86481c2 | 2010-06-17 15:05:57 +0800 | [diff] [blame] | 1118 | "conn=%p, sms=%p)\n", hooknum, event, msg, conn, sms); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1119 | |
| 1120 | if (hooknum != GSM_HOOK_RR_PAGING) |
| 1121 | return -EINVAL; |
| 1122 | |
| 1123 | switch (event) { |
| 1124 | case GSM_PAGING_SUCCEEDED: |
Holger Hans Peter Freyther | 86481c2 | 2010-06-17 15:05:57 +0800 | [diff] [blame] | 1125 | gsm411_send_sms(conn, sms); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1126 | break; |
| 1127 | case GSM_PAGING_EXPIRED: |
Holger Hans Peter Freyther | ca5702a | 2010-06-10 18:20:54 +0800 | [diff] [blame] | 1128 | case GSM_PAGING_OOM: |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1129 | sms_free(sms); |
| 1130 | rc = -ETIMEDOUT; |
| 1131 | break; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | return rc; |
| 1135 | } |
| 1136 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1137 | /* high-level function to send a SMS to a given subscriber. The function |
| 1138 | * will take care of paging the subscriber, establishing the RLL SAPI3 |
| 1139 | * connection, etc. */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1140 | int gsm411_send_sms_subscr(struct gsm_subscriber *subscr, |
| 1141 | struct gsm_sms *sms) |
| 1142 | { |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 1143 | struct gsm_subscriber_connection *conn; |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1144 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1145 | /* check if we already have an open lchan to the subscriber. |
| 1146 | * if yes, send the SMS this way */ |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 1147 | conn = connection_for_subscr(subscr); |
| 1148 | if (conn) { |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 1149 | return gsm411_send_sms(conn, sms); |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 1150 | } |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1151 | |
| 1152 | /* if not, we have to start paging */ |
Holger Hans Peter Freyther | 7afac58 | 2010-06-10 18:21:25 +0800 | [diff] [blame] | 1153 | subscr_get_channel(subscr, RSL_CHANNEED_SDCCH, paging_cb_send_sms, sms); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1154 | return 0; |
| 1155 | } |
Harald Welte | 3366a94 | 2009-07-28 00:44:49 +0200 | [diff] [blame] | 1156 | |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1157 | static int subscr_sig_cb(unsigned int subsys, unsigned int signal, |
| 1158 | void *handler_data, void *signal_data) |
| 1159 | { |
| 1160 | struct gsm_subscriber *subscr; |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 1161 | struct gsm_subscriber_connection *conn; |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1162 | struct gsm_sms *sms; |
| 1163 | |
| 1164 | switch (signal) { |
| 1165 | case S_SUBSCR_ATTACHED: |
| 1166 | /* A subscriber has attached. Check if there are |
| 1167 | * any pending SMS for him to be delivered */ |
| 1168 | subscr = signal_data; |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 1169 | conn = connection_for_subscr(subscr); |
| 1170 | if (!conn) |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1171 | break; |
| 1172 | sms = db_sms_get_unsent_for_subscr(subscr); |
| 1173 | if (!sms) |
| 1174 | break; |
Holger Hans Peter Freyther | 0fbb3ec | 2010-06-16 14:19:31 +0800 | [diff] [blame] | 1175 | gsm411_send_sms(conn, sms); |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1176 | break; |
| 1177 | default: |
| 1178 | break; |
| 1179 | } |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 1183 | void _gsm411_sms_trans_free(struct gsm_trans *trans) |
| 1184 | { |
Holger Hans Peter Freyther | cea722b | 2010-06-15 12:29:37 +0800 | [diff] [blame] | 1185 | if (trans->sms.sms) { |
| 1186 | LOGP(DSMS, LOGL_ERROR, "Transaction contains SMS.\n"); |
| 1187 | sms_free(trans->sms.sms); |
| 1188 | trans->sms.sms = NULL; |
| 1189 | } |
| 1190 | |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 1191 | bsc_del_timer(&trans->sms.cp_timer); |
| 1192 | } |
| 1193 | |
Holger Hans Peter Freyther | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 1194 | void gsm411_sapi_n_reject(struct gsm_subscriber_connection *conn) |
| 1195 | { |
| 1196 | struct gsm_trans *trans, *tmp; |
| 1197 | |
| 1198 | llist_for_each_entry_safe(trans, tmp, &conn->bts->network->trans_list, entry) |
| 1199 | if (trans->conn == conn) { |
| 1200 | struct gsm_sms *sms = trans->sms.sms; |
| 1201 | if (!sms) { |
| 1202 | LOGP(DSMS, LOGL_ERROR, "SAPI Reject but no SMS.\n"); |
| 1203 | continue; |
| 1204 | } |
| 1205 | |
| 1206 | sms_free(sms); |
| 1207 | trans->sms.sms = NULL; |
| 1208 | trans_free(trans); |
| 1209 | } |
Holger Hans Peter Freyther | ecd9933 | 2010-06-15 12:53:08 +0800 | [diff] [blame] | 1210 | |
| 1211 | gsm411_release_conn(conn); |
Holger Hans Peter Freyther | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 1212 | } |
| 1213 | |
Harald Welte | 7bfc267 | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 1214 | static __attribute__((constructor)) void on_dso_load_sms(void) |
| 1215 | { |
Harald Welte | cf6a381 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 1216 | register_signal_handler(SS_SUBSCR, subscr_sig_cb, NULL); |
Harald Welte | 7bfc267 | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 1217 | } |