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