blob: 54e6129c125298e4f90308974849fa278f9e476b [file] [log] [blame]
Andreas Eversbergbbf90342011-10-28 03:55:37 +02001/* 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 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
8 * (C) 2010 by On-Waves
9 * (C) 2011 by Andreas Eversberg <jolly@eversberg.eu>
10 *
11 * All Rights Reserved
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Affero General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Affero General Public License for more details.
22 *
23 * You should have received a copy of the GNU Affero General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *
26 */
27
28/* Notes on msg:
29 *
30 * Messages from lower layer are freed by lower layer.
31 *
32 * Messages to upper layer are freed after upper layer call returns, so upper
33 * layer cannot use data after returning. Upper layer must not free the msg.
34 *
35 * This implies: Lower layer messages can be forwarded to upper layer.
36 *
37 * Upper layer messages are freed by lower layer, so they must not be freed
38 * after calling lower layer.
39 *
40 *
41 * Notes on release:
42 *
43 * Whenever the process returns to IDLE, the MM connection is released using
44 * MMSMS-REL-REQ. It is allowed to destroy this process while processing
45 * this message.
46 *
47 * There is expeption, if MMSMS-REL-IND is received from lower layer, the
48 * process returns to IDLE without sending MMSMS-REL-REQ.
49 *
50 */
51
52#include <string.h>
53#include <errno.h>
54#include <osmocom/core/msgb.h>
55#include <osmocom/core/logging.h>
56#include <osmocom/core/timer.h>
57
58#include <osmocom/gsm/gsm0411_utils.h>
59#include <osmocom/gsm/gsm0411_smc.h>
60#include <osmocom/gsm/protocol/gsm_04_08.h>
61
62static void cp_timer_expired(void *data);
63
Sylvain Munautcc90d492011-11-12 23:52:40 +010064#define MAX_SMS_RETRY 2
Andreas Eversbergbbf90342011-10-28 03:55:37 +020065
66/* init a new instance */
67void gsm411_smc_init(struct gsm411_smc_inst *inst, int network,
68 int (*mn_recv) (struct gsm411_smc_inst *inst, int msg_type,
69 struct msgb *msg),
70 int (*mm_send) (struct gsm411_smc_inst *inst, int msg_type,
71 struct msgb *msg, int cp_msg_type))
72{
73 memset(inst, 0, sizeof(*inst));
74 inst->network = network;
75 inst->cp_max_retr = MAX_SMS_RETRY;
Sylvain Munaut0d9b8ec2011-11-12 23:52:20 +010076 inst->cp_tc1 = GSM411_TMR_TC1A_SEC / (inst->cp_max_retr + 1);
Andreas Eversbergbbf90342011-10-28 03:55:37 +020077 inst->cp_state = GSM411_CPS_IDLE;
78 inst->mn_recv = mn_recv;
79 inst->mm_send = mm_send;
80
81 LOGP(DLSMS, LOGL_INFO, "New SMC instance created\n");
82}
83
84/* clear instance */
85void gsm411_smc_clear(struct gsm411_smc_inst *inst)
86{
87 LOGP(DLSMS, LOGL_INFO, "Clear SMC instance\n");
88
89 osmo_timer_del(&inst->cp_timer);
90
91 /* free stored msg */
92 if (inst->cp_msg) {
93 LOGP(DLSMS, LOGL_INFO, "Dropping pending message\n");
94 msgb_free(inst->cp_msg);
95 inst->cp_msg = NULL;
96 }
97}
98
99const char *smc_state_names[] = {
100 "IDLE",
101 "MM_CONN_PENDING",
102 "WAIT_CP_ACK",
103 "MM_ESTABLISHED",
104};
105
106const struct value_string gsm411_cp_cause_strs[] = {
107 { GSM411_CP_CAUSE_NET_FAIL, "Network Failure" },
108 { GSM411_CP_CAUSE_CONGESTION, "Congestion" },
109 { GSM411_CP_CAUSE_INV_TRANS_ID, "Invalid Transaction ID" },
110 { GSM411_CP_CAUSE_SEMANT_INC_MSG, "Semantically Incorrect Message" },
111 { GSM411_CP_CAUSE_INV_MAND_INF, "Invalid Mandatory Information" },
112 { GSM411_CP_CAUSE_MSGTYPE_NOTEXIST, "Message Type doesn't exist" },
113 { GSM411_CP_CAUSE_MSG_INCOMP_STATE,
114 "Message incompatible with protocol state" },
115 { GSM411_CP_CAUSE_IE_NOTEXIST, "IE does not exist" },
116 { GSM411_CP_CAUSE_PROTOCOL_ERR, "Protocol Error" },
117 { 0, 0 }
118};
119
120static void new_cp_state(struct gsm411_smc_inst *inst,
121 enum gsm411_cp_state state)
122{
123 LOGP(DLSMS, LOGL_INFO, "New CP state %s -> %s\n",
124 smc_state_names[inst->cp_state], smc_state_names[state]);
125 inst->cp_state = state;
126}
127
128static int gsm411_tx_cp_error(struct gsm411_smc_inst *inst, uint8_t cause)
129{
130 struct msgb *nmsg = gsm411_msgb_alloc();
131 uint8_t *causep;
132
133 LOGP(DLSMS, LOGL_NOTICE, "TX CP-ERROR, cause %d (%s)\n", cause,
134 get_value_string(gsm411_cp_cause_strs, cause));
135
136 causep = msgb_put(nmsg, 1);
137 *causep = cause;
138
139 return inst->mm_send(inst, GSM411_MMSMS_DATA_REQ, nmsg,
140 GSM411_MT_CP_ERROR);
141}
142
143/* etablish SMC connection */
144static int gsm411_mnsms_est_req(struct gsm411_smc_inst *inst, struct msgb *msg)
145{
146 struct msgb *nmsg;
147
148 if (inst->cp_msg) {
149 LOGP(DLSMS, LOGL_FATAL, "EST REQ, but we already have an "
150 "cp_msg. This should never happen, please fix!\n");
151 msgb_free(inst->cp_msg);
152 }
153
154 inst->cp_msg = msg;
155 new_cp_state(inst, GSM411_CPS_MM_CONN_PENDING);
156 /* clear stored release flag */
157 inst->cp_rel = 0;
158 /* send MMSMS_EST_REQ */
159 nmsg = gsm411_msgb_alloc();
160 return inst->mm_send(inst, GSM411_MMSMS_EST_REQ, nmsg, 0);
161}
162
163static int gsm411_mmsms_send_msg(struct gsm411_smc_inst *inst)
164{
165 struct msgb *nmsg;
166
167 LOGP(DLSMS, LOGL_INFO, "Send CP data\n");
168 /* reset retry counter */
169 if (inst->cp_state != GSM411_CPS_WAIT_CP_ACK)
170 inst->cp_retx = 0;
171 /* 5.2.3.1.2: enter MO-wait for CP-ACK */
172 /* 5.2.3.2.3: enter MT-wait for CP-ACK */
173 new_cp_state(inst, GSM411_CPS_WAIT_CP_ACK);
174 inst->cp_timer.data = inst;
175 inst->cp_timer.cb = cp_timer_expired;
176 /* 5.3.2.1: Set Timer TC1A */
177 osmo_timer_schedule(&inst->cp_timer, inst->cp_tc1, 0);
178 /* clone cp_msg */
179 nmsg = gsm411_msgb_alloc();
180 memcpy(msgb_put(nmsg, inst->cp_msg->len), inst->cp_msg->data,
181 inst->cp_msg->len);
182 /* send MMSMS_DATA_REQ with CP-DATA */
183 return inst->mm_send(inst, GSM411_MMSMS_DATA_REQ, nmsg,
184 GSM411_MT_CP_DATA);
185}
186
187static int gsm411_mmsms_est_cnf(struct gsm411_smc_inst *inst, struct msgb *msg)
188{
189 if (!inst->cp_msg) {
190 LOGP(DLSMS, LOGL_FATAL, "EST CNF, but we have no cp_msg. This "
191 "should never happen, please fix!\n");
192 return -EINVAL;
193 }
194
195 return gsm411_mmsms_send_msg(inst);
196}
197
198/* SMC TC1* is expired */
199static void cp_timer_expired(void *data)
200{
201 struct gsm411_smc_inst *inst = data;
202 struct msgb *nmsg;
203
204 if (inst->cp_retx == inst->cp_max_retr) {
205
206 LOGP(DLSMS, LOGL_INFO, "TC1* timeout, no more retries.\n");
207 /* 5.3.2.1: enter idle state */
208 new_cp_state(inst, GSM411_CPS_IDLE);
209 /* indicate error */
210 nmsg = gsm411_msgb_alloc();
211 inst->mn_recv(inst, GSM411_MNSMS_ERROR_IND, nmsg);
212 msgb_free(nmsg);
213 /* free pending stored msg */
214 if (inst->cp_msg) {
215 msgb_free(inst->cp_msg);
216 inst->cp_msg = NULL;
217 }
218 /* release MM connection */
219 nmsg = gsm411_msgb_alloc();
220 inst->mm_send(inst, GSM411_MMSMS_REL_REQ, nmsg, 0);
221 return;
222 }
223
224 LOGP(DLSMS, LOGL_INFO, "TC1* timeout, retrying...\n");
225 inst->cp_retx++;
226 gsm411_mmsms_est_cnf(inst, NULL);
227}
228
229static int gsm411_mmsms_cp_ack(struct gsm411_smc_inst *inst, struct msgb *msg)
230{
231 /* free stored msg */
232 if (inst->cp_msg) {
233 msgb_free(inst->cp_msg);
234 inst->cp_msg = NULL;
235 }
236
237 LOGP(DLSMS, LOGL_INFO, "Received CP-ACK\n");
238 /* 5.3.2.1 enter MM Connection established */
239 new_cp_state(inst, GSM411_CPS_MM_ESTABLISHED);
240 /* 5.3.2.1: Reset Timer TC1* */
241 osmo_timer_del(&inst->cp_timer);
242
243 /* pending release? */
244 if (inst->cp_rel) {
245 struct msgb *nmsg;
246
247 LOGP(DLSMS, LOGL_INFO, "We have pending release.\n");
248 new_cp_state(inst, GSM411_CPS_IDLE);
249 /* release MM connection */
250 nmsg = gsm411_msgb_alloc();
251 return inst->mm_send(inst, GSM411_MMSMS_REL_REQ, nmsg, 0);
252 }
253
254 return 0;
255}
256
257static int gsm411_mmsms_cp_data(struct gsm411_smc_inst *inst, struct msgb *msg)
258{
259 struct msgb *nmsg;
260 int mt = GSM411_MNSMS_DATA_IND;
261
262 LOGP(DLSMS, LOGL_INFO, "Received CP-DATA\n");
263 /* 5.3.1 enter MM Connection established (if idle) */
264 if (inst->cp_state == GSM411_CPS_IDLE) {
265 new_cp_state(inst, GSM411_CPS_MM_ESTABLISHED);
266 mt = GSM411_MNSMS_EST_IND;
267 /* clear stored release flag */
268 inst->cp_rel = 0;
269 }
270 /* send MMSMS_DATA_REQ (CP ACK) */
271 nmsg = gsm411_msgb_alloc();
272 inst->mm_send(inst, GSM411_MMSMS_DATA_REQ, nmsg, GSM411_MT_CP_ACK);
273 /* indicate data */
274 inst->mn_recv(inst, mt, msg);
275
276 return 0;
277}
278
279/* send CP DATA */
280static int gsm411_mnsms_data_req(struct gsm411_smc_inst *inst, struct msgb *msg)
281{
282 if (inst->cp_msg) {
283 LOGP(DLSMS, LOGL_FATAL, "DATA REQ, but we already have an "
284 "cp_msg. This should never happen, please fix!\n");
285 msgb_free(inst->cp_msg);
286 }
287
288 /* store and send */
289 inst->cp_msg = msg;
290 return gsm411_mmsms_send_msg(inst);
291}
292
293/* release SMC connection */
294static int gsm411_mnsms_rel_req(struct gsm411_smc_inst *inst, struct msgb *msg)
295{
296 struct msgb *nmsg;
297
298 msgb_free(msg);
299
300 /* discard silently */
301 if (inst->cp_state == GSM411_CPS_IDLE)
302 return 0;
303
304 /* store release, until established or released */
305 if (inst->cp_state != GSM411_CPS_MM_ESTABLISHED) {
306 LOGP(DLSMS, LOGL_NOTICE, "Cannot release yet.\n");
307 inst->cp_rel = 1;
308 return 0;
309 }
310
311 /* free stored msg */
312 if (inst->cp_msg) {
313 msgb_free(inst->cp_msg);
314 inst->cp_msg = NULL;
315 }
316
317 new_cp_state(inst, GSM411_CPS_IDLE);
318 /* release MM connection */
319 nmsg = gsm411_msgb_alloc();
320 return inst->mm_send(inst, GSM411_MMSMS_REL_REQ, nmsg, 0);
321}
322
323static int gsm411_mmsms_cp_error(struct gsm411_smc_inst *inst, struct msgb *msg)
324{
325 struct msgb *nmsg;
326
327 /* free stored msg */
328 if (inst->cp_msg) {
329 msgb_free(inst->cp_msg);
330 inst->cp_msg = NULL;
331 }
332
333 LOGP(DLSMS, LOGL_INFO, "Received CP-ERROR\n");
334 /* 5.3.4 enter idle */
335 new_cp_state(inst, GSM411_CPS_IDLE);
336 /* indicate error */
337 inst->mn_recv(inst, GSM411_MNSMS_ERROR_IND, msg);
338 /* release MM connection */
339 nmsg = gsm411_msgb_alloc();
340 return inst->mm_send(inst, GSM411_MMSMS_REL_REQ, nmsg, 0);
341}
342
343static int gsm411_mmsms_rel_ind(struct gsm411_smc_inst *inst, struct msgb *msg)
344{
345 struct msgb *nmsg;
346
347 /* free stored msg */
348 if (inst->cp_msg) {
349 msgb_free(inst->cp_msg);
350 inst->cp_msg = NULL;
351 }
352
353 LOGP(DLSMS, LOGL_INFO, "MM layer is released\n");
354 /* 5.3.4 enter idle */
355 new_cp_state(inst, GSM411_CPS_IDLE);
356 /* indicate error */
357 nmsg = gsm411_msgb_alloc();
358 inst->mn_recv(inst, GSM411_MNSMS_ERROR_IND, nmsg);
359 msgb_free(nmsg);
360
361 return 0;
362}
363
364/* abort SMC connection */
365static int gsm411_mnsms_abort_req(struct gsm411_smc_inst *inst,
366 struct msgb *msg)
367{
368 struct msgb *nmsg;
369
370 /* free stored msg */
371 if (inst->cp_msg) {
372 msgb_free(inst->cp_msg);
373 inst->cp_msg = NULL;
374 }
375
376 /* 5.3.4 go idle */
377 new_cp_state(inst, GSM411_CPS_IDLE);
378 /* send MMSMS_DATA_REQ with CP-ERROR */
379 inst->mm_send(inst, GSM411_MMSMS_DATA_REQ, msg, GSM411_MT_CP_ERROR);
380 /* release MM connection */
381 nmsg = gsm411_msgb_alloc();
382 return inst->mm_send(inst, GSM411_MMSMS_REL_REQ, nmsg, 0);
383}
384
385/* statefull handling for MNSMS SAP messages */
386static struct smcdownstate {
387 uint32_t states;
388 int type;
389 const char *name;
390 int (*rout) (struct gsm411_smc_inst *inst,
391 struct msgb *msg);
392} smcdownstatelist[] = {
393 /* establish request */
394 {SBIT(GSM411_CPS_IDLE),
395 GSM411_MNSMS_EST_REQ,
396 "MNSMS-EST-REQ", gsm411_mnsms_est_req},
397
398 /* release request */
399 {ALL_STATES,
400 GSM411_MNSMS_REL_REQ,
401 "MNSMS-REL-REQ", gsm411_mnsms_rel_req},
402
403 /* data request */
404 {SBIT(GSM411_CPS_MM_ESTABLISHED),
405 GSM411_MNSMS_DATA_REQ,
406 "MNSMS-DATA-REQ", gsm411_mnsms_data_req},
407
408 /* abort request */
409 {ALL_STATES - SBIT(GSM411_CPS_IDLE),
410 GSM411_MNSMS_ABORT_REQ,
411 "MNSMS-ABORT-REQ", gsm411_mnsms_abort_req},
412};
413
414#define SMCDOWNSLLEN \
415 (sizeof(smcdownstatelist) / sizeof(struct smcdownstate))
416
417/* message from upper layer */
418int gsm411_smc_send(struct gsm411_smc_inst *inst, int msg_type,
419 struct msgb *msg)
420{
421 int i, rc;
422
423 /* find function for current state and message */
424 for (i = 0; i < SMCDOWNSLLEN; i++) {
425 if ((msg_type == smcdownstatelist[i].type)
426 && (SBIT(inst->cp_state) & smcdownstatelist[i].states))
427 break;
428 }
429 if (i == SMCDOWNSLLEN) {
430 LOGP(DLSMS, LOGL_NOTICE, "Message %u unhandled at this state "
431 "%s.\n", msg_type, smc_state_names[inst->cp_state]);
432 msgb_free(msg);
433 return 0;
434 }
435
436 LOGP(DLSMS, LOGL_INFO, "Message %s received in state %s\n",
437 smcdownstatelist[i].name, smc_state_names[inst->cp_state]);
438
439 rc = smcdownstatelist[i].rout(inst, msg);
440
441 return rc;
442}
443
444/* statefull handling for MMSMS SAP messages */
445static struct smcdatastate {
446 uint32_t states;
447 int type, cp_type;
448 const char *name;
449 int (*rout) (struct gsm411_smc_inst *inst,
450 struct msgb *msg);
451} smcdatastatelist[] = {
452 /* establish confirm */
453 {SBIT(GSM411_CPS_MM_CONN_PENDING),
454 GSM411_MMSMS_EST_CNF, 0,
455 "MMSMS-EST-CNF", gsm411_mmsms_est_cnf},
456
457 /* establish indication (CP DATA) */
458 {SBIT(GSM411_CPS_IDLE),
459 GSM411_MMSMS_EST_IND, GSM411_MT_CP_DATA,
460 "MMSMS-EST-IND (CP DATA)", gsm411_mmsms_cp_data},
461
462 /* data indication (CP DATA) */
463 {SBIT(GSM411_CPS_MM_ESTABLISHED),
464 GSM411_MMSMS_DATA_IND, GSM411_MT_CP_DATA,
465 "MMSMS-DATA-IND (CP DATA)", gsm411_mmsms_cp_data},
466
467 /* data indication (CP ACK) */
468 {SBIT(GSM411_CPS_WAIT_CP_ACK),
469 GSM411_MMSMS_DATA_IND, GSM411_MT_CP_ACK,
470 "MMSMS-DATA-IND (CP ACK)", gsm411_mmsms_cp_ack},
471
472 /* data indication (CP ERROR) */
473 {ALL_STATES,
474 GSM411_MMSMS_DATA_IND, GSM411_MT_CP_ERROR,
475 "MMSMS-DATA-IND (CP_ERROR)", gsm411_mmsms_cp_error},
476
477 /* release indication */
478 {ALL_STATES - SBIT(GSM411_CPS_IDLE),
479 GSM411_MMSMS_REL_IND, 0,
480 "MMSMS-REL-IND", gsm411_mmsms_rel_ind},
481
482};
483
484#define SMCDATASLLEN \
485 (sizeof(smcdatastatelist) / sizeof(struct smcdatastate))
486
487/* message from lower layer
488 * WARNING: We must not free msg, since it will be performed by the
489 * lower layer. */
490int gsm411_smc_recv(struct gsm411_smc_inst *inst, int msg_type,
491 struct msgb *msg, int cp_msg_type)
492{
493 int i, rc;
494
495 /* find function for current state and message */
496 for (i = 0; i < SMCDATASLLEN; i++) {
497 /* state must machtch, MM message must match
498 * CP msg must match only in case of MMSMS_DATA_IND
499 */
500 if ((msg_type == smcdatastatelist[i].type)
501 && (SBIT(inst->cp_state) & smcdatastatelist[i].states)
502 && (msg_type != GSM411_MMSMS_DATA_IND
503 || cp_msg_type == smcdatastatelist[i].cp_type))
504 break;
505 }
506 if (i == SMCDATASLLEN) {
507 LOGP(DLSMS, LOGL_NOTICE, "Message 0x%x/%u unhandled at this "
508 "state %s.\n", msg_type, cp_msg_type,
509 smc_state_names[inst->cp_state]);
510 if (msg_type == GSM411_MMSMS_EST_IND
511 || msg_type == GSM411_MMSMS_DATA_IND) {
512 struct msgb *nmsg;
513
514 LOGP(DLSMS, LOGL_NOTICE, "RX Unimplemented CP "
515 "msg_type: 0x%02x\n", msg_type);
516 /* 5.3.4 enter idle */
517 new_cp_state(inst, GSM411_CPS_IDLE);
518 /* indicate error */
519 gsm411_tx_cp_error(inst,
520 GSM411_CP_CAUSE_MSGTYPE_NOTEXIST);
521 /* send error indication to upper layer */
522 nmsg = gsm411_msgb_alloc();
523 inst->mn_recv(inst, GSM411_MNSMS_ERROR_IND, nmsg);
524 msgb_free(nmsg);
525 /* release MM connection */
526 nmsg = gsm411_msgb_alloc();
527 return inst->mm_send(inst, GSM411_MMSMS_REL_REQ, nmsg,
528 0);
529 }
530 return 0;
531 }
532
533 LOGP(DLSMS, LOGL_INFO, "Message %s received in state %s\n",
534 smcdatastatelist[i].name, smc_state_names[inst->cp_state]);
535
536 rc = smcdatastatelist[i].rout(inst, msg);
537
538 return rc;
539}