blob: 46816a961b7878435dc7102147e0a2b93e67abed [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* Common bits for RAN message handling */
2/*
3 * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * SPDX-License-Identifier: GPL-2.0+
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#include <osmocom/core/utils.h>
26
27#include <osmocom/msc/ran_msg.h>
28
29const struct value_string ran_msg_type_names[] = {
30 { RAN_MSG_NONE, "NONE" },
31 { RAN_MSG_COMPL_L3, "COMPL_L3" },
32 { RAN_MSG_DTAP, "DTAP" },
33 { RAN_MSG_CLEAR_COMMAND, "CLEAR_COMMAND" },
34 { RAN_MSG_CLEAR_REQUEST, "CLEAR_REQUEST" },
35 { RAN_MSG_CLEAR_COMPLETE, "CLEAR_COMPLETE" },
36 { RAN_MSG_CLASSMARK_REQUEST, "CLASSMARK_REQUEST" },
37 { RAN_MSG_CLASSMARK_UPDATE, "CLASSMARK_UPDATE" },
38 { RAN_MSG_CIPHER_MODE_COMMAND, "CIPHER_MODE_COMMAND" },
39 { RAN_MSG_CIPHER_MODE_COMPLETE, "CIPHER_MODE_COMPLETE" },
40 { RAN_MSG_CIPHER_MODE_REJECT, "CIPHER_MODE_REJECT" },
41 { RAN_MSG_COMMON_ID, "COMMON_ID" },
42 { RAN_MSG_ASSIGNMENT_COMMAND, "ASSIGNMENT_COMMAND" },
43 { RAN_MSG_ASSIGNMENT_COMPLETE, "ASSIGNMENT_COMPLETE" },
44 { RAN_MSG_ASSIGNMENT_FAILURE, "ASSIGNMENT_FAILURE" },
45 { RAN_MSG_SAPI_N_REJECT, "SAPI_N_REJECT" },
46 { RAN_MSG_LCLS_STATUS, "LCLS_STATUS" },
47 { RAN_MSG_LCLS_BREAK_REQ, "LCLS_BREAK_REQ" },
48 { RAN_MSG_HANDOVER_COMMAND, "HANDOVER_COMMAND" },
49 { RAN_MSG_HANDOVER_SUCCEEDED, "HANDOVER_SUCCEEDED" },
50 { RAN_MSG_HANDOVER_PERFORMED, "HANDOVER_PERFORMED" },
51 { RAN_MSG_HANDOVER_REQUIRED, "HANDOVER_REQUIRED" },
52 { RAN_MSG_HANDOVER_REQUIRED_REJECT, "HANDOVER_REQUIRED_REJECT" },
53 { RAN_MSG_HANDOVER_REQUEST, "HANDOVER_REQUEST" },
54 { RAN_MSG_HANDOVER_REQUEST_ACK, "HANDOVER_REQUEST_ACK" },
55 { RAN_MSG_HANDOVER_DETECT, "HANDOVER_DETECT" },
56 { RAN_MSG_HANDOVER_COMPLETE, "HANDOVER_COMPLETE" },
57 { RAN_MSG_HANDOVER_FAILURE, "HANDOVER_FAILURE" },
58 {}
59};
60
61/* extract the N(SD) and return the modulo value for a R99 message */
62static uint8_t ran_dec_dtap_undup_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
63{
64 switch (pdisc) {
65 case GSM48_PDISC_MM:
66 case GSM48_PDISC_CC:
67 case GSM48_PDISC_NC_SS:
68 *n_sd = (msg_type >> 6) & 0x3;
69 return 4;
70 case GSM48_PDISC_GROUP_CC:
71 case GSM48_PDISC_BCAST_CC:
72 case GSM48_PDISC_LOC:
73 *n_sd = (msg_type >> 6) & 0x1;
74 return 2;
75 default:
76 /* no sequence number, we cannot detect dups */
77 return 0;
78 }
79}
80
81/* extract the N(SD) and return the modulo value for a R98 message */
82static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
83{
84 switch (pdisc) {
85 case GSM48_PDISC_MM:
86 case GSM48_PDISC_CC:
87 case GSM48_PDISC_NC_SS:
88 case GSM48_PDISC_GROUP_CC:
89 case GSM48_PDISC_BCAST_CC:
90 case GSM48_PDISC_LOC:
91 *n_sd = (msg_type >> 6) & 0x1;
92 return 2;
93 default:
94 /* no sequence number, we cannot detect dups */
95 return 0;
96 }
97}
98
99/* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection.
100 * (Not static for unit testing). */
101int ran_dec_dtap_undup_pdisc_ctr_bin(uint8_t pdisc)
102{
103 switch (pdisc) {
104 case GSM48_PDISC_MM:
105 case GSM48_PDISC_CC:
106 case GSM48_PDISC_NC_SS:
107 return 0;
108 case GSM48_PDISC_GROUP_CC:
109 return 1;
110 case GSM48_PDISC_BCAST_CC:
111 return 2;
112 case GSM48_PDISC_LOC:
113 return 3;
114 default:
115 return -1;
116 }
117}
118
119/* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
120bool ran_dec_dtap_undup_is_duplicate(struct osmo_fsm_inst *log_fi, uint8_t *n_sd_next, bool is_r99, struct msgb *l3)
121{
122 struct gsm48_hdr *gh;
123 uint8_t pdisc;
124 uint8_t n_sd, modulo;
125 int bin;
126
127 gh = msgb_l3(l3);
128 pdisc = gsm48_hdr_pdisc(gh);
129
130 if (is_r99) {
131 modulo = ran_dec_dtap_undup_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd);
132 } else { /* pre R99 */
133 modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd);
134 }
135 if (modulo == 0)
136 return false;
137 bin = ran_dec_dtap_undup_pdisc_ctr_bin(pdisc);
138 if (bin < 0)
139 return false;
140
141 OSMO_ASSERT(bin >= 0 && bin < 4);
142 if (n_sd != n_sd_next[bin]) {
143 /* not what we expected: duplicate */
144 LOGPFSML(log_fi, LOGL_NOTICE, "Duplicate DTAP: bin=%d, expected n_sd == %u, got %u\n",
145 bin, n_sd_next[bin], n_sd);
146 return true;
147 } else {
148 /* as expected: no dup; update expected counter for next message */
149 n_sd_next[bin] = (n_sd + 1) % modulo;
150 return false;
151 }
152}
153
154/* convenience: RAN decode implementations can call this to dispatch the decode_cb with a decoded ran_msg. */
155int ran_decoded(struct ran_dec *ran_dec, struct ran_msg *ran_msg)
156{
157 if (!ran_dec->decode_cb)
158 return -1;
159 return ran_dec->decode_cb(ran_dec->caller_fi, ran_dec->caller_data, ran_msg);
160}