blob: d691d804171aca1ab36a08b169a2ab8440a2ddeb [file] [log] [blame]
Harald Weltea49e36e2018-01-21 19:29:33 +01001module BSC_ConnectionHandler {
2
3import from General_Types all;
4import from Osmocom_Types all;
5import from GSM_Types all;
6import from SCCPasp_Types all;
7import from BSSAP_Types all;
8import from BSSMAP_Emulation all;
9import from BSSMAP_Templates all;
10
11import from GSUP_Types all;
12import from GSUP_Emulation all;
13
14import from MNCC_Types all;
15import from MNCC_Emulation all;
16
Harald Welte4aa970c2018-01-26 10:38:09 +010017import from MGCP_Types all;
18import from MGCP_Emulation all;
19
Harald Weltea49e36e2018-01-21 19:29:33 +010020import from MobileL3_Types all;
21import from MobileL3_CommonIE_Types all;
22import from MobileL3_MM_Types all;
23import from L3_Templates all;
24
25/* this component represents a single subscriber connection */
Harald Welte4aa970c2018-01-26 10:38:09 +010026type component BSC_ConnHdlr extends BSSAP_ConnHdlr, MNCC_ConnHdlr, GSUP_ConnHdlr, MGCP_ConnHdlr {
Harald Weltea49e36e2018-01-21 19:29:33 +010027 var BSC_ConnHdlrPars g_pars;
28}
29
Harald Welte148a7082018-01-26 18:56:43 +010030type record AuthVector {
31 OCT16 rand,
32 OCT4 sres,
33 OCT8 kc
34 /* FIXME: 3G elements */
35}
36
Harald Weltea49e36e2018-01-21 19:29:33 +010037type record BSC_ConnHdlrPars {
38 SCCP_PAR_Address sccp_addr_own,
39 SCCP_PAR_Address sccp_addr_peer,
40 BSSMAP_IE_CellIdentifier cell_id,
Harald Welte256571e2018-01-24 18:47:19 +010041 hexstring imei,
Harald Weltea49e36e2018-01-21 19:29:33 +010042 hexstring imsi,
Harald Welte82600572018-01-21 20:54:08 +010043 hexstring msisdn,
Harald Welte256571e2018-01-24 18:47:19 +010044 OCT4 tmsi optional,
Harald Welte82600572018-01-21 20:54:08 +010045 BSSMAP_IE_ClassmarkInformationType2 cm2,
Harald Welte16114282018-01-24 22:41:21 +010046 BSSMAP_IE_ClassmarkInformationType3 cm3 optional,
Harald Welte148a7082018-01-26 18:56:43 +010047 AuthVector vec optional
Harald Weltea49e36e2018-01-21 19:29:33 +010048};
49
50
51/* Callback function from general BSSMAP_Emulation whenever a connectionless
52 * BSSMAP message arrives. Canreturn a PDU_BSSAPthat should be sent in return */
53private function BscUnitdataCallback(PDU_BSSAP bssap)
54runs on BSSMAP_Emulation_CT return template PDU_BSSAP {
55 var template PDU_BSSAP resp := omit;
56
57 log("BSSMAP_BscUnitdataCallback");
58 /* answer all RESET with RESET ACK */
59 if (match(bssap, tr_BSSMAP_Reset)){
60 log("BSSMAP_BscUnitdataCallback: Responding to RESET with RESET-ACK");
61 resp := ts_BSSMAP_ResetAck;
62 }
63
64 /* FIXME: Handle paging, etc. */
65 return resp;
66}
67
68const BssmapOps BSC_BssmapOps := {
69 /* Create call-back for inbound connections from MSC (hand-over) */
70 create_cb := refers(BSSMAP_Emulation.ExpectedCreateCallback),
71 unitdata_cb := refers(BscUnitdataCallback),
72 decode_dtap := true,
73 role_ms := true
74}
75
76
77private function MnccUnitdataCallback(MNCC_PDU mncc)
78runs on MNCC_Emulation_CT return template MNCC_PDU {
79 log("Ignoring MNCC", mncc);
80 return omit;
81}
82
83const MnccOps BCC_MnccOps := {
84 create_cb := refers(MNCC_Emulation.ExpectedCreateCallback),
85 unitdata_cb := refers(MnccUnitdataCallback)
86}
87
88
89
90template BSSAP_Conn_Req ts_BSSAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, PDU_BSSAP bssap) := {
91 addr_peer := peer,
92 addr_own := own,
93 bssap := bssap
94};
95
Harald Weltea49e36e2018-01-21 19:29:33 +010096/* Encode 'l3' and ask BSSMAP_Emulation to create new connection with COMPL L3 INFO */
97function f_bssap_compl_l3(PDU_ML3_MS_NW l3)
98runs on BSC_ConnHdlr {
99 log("Sending COMPL L3: ", l3);
100 var octetstring l3_enc := enc_PDU_ML3_MS_NW(l3);
101 BSSAP.send(ts_BSSAP_Conn_Req(g_pars.sccp_addr_peer, g_pars.sccp_addr_own,
102 valueof(ts_BSSMAP_ComplL3(g_pars.cell_id, l3_enc))));
Harald Welte71b69332018-01-21 20:43:53 +0100103 alt {
104 [] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_CONF_IND) {}
105 [] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_IND) {
106 setverdict(fail, "DISC.ind from SCCP");
107 self.stop;
108 }
109 }
Harald Weltea49e36e2018-01-21 19:29:33 +0100110}
111
112/* helper function to fully establish a dedicated channel */
Harald Welte148a7082018-01-26 18:56:43 +0100113function f_establish_fully(MobileIdentityLV mi, boolean expect_auth, boolean expect_ciph)
Harald Weltea49e36e2018-01-21 19:29:33 +0100114runs on BSC_ConnHdlr {
Harald Welte6ed6bf92018-01-24 21:09:15 +0100115 var PDU_ML3_MS_NW l3_info := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi));
Harald Weltea49e36e2018-01-21 19:29:33 +0100116 var PDU_DTAP_MT dtap_mt;
117
118 /* Send BSSAP_Conn_Req with COMPL L3 INFO to MSC */
119 f_bssap_compl_l3(l3_info);
120
Harald Welte148a7082018-01-26 18:56:43 +0100121 f_mm_common(expect_auth, expect_ciph);
122 if (expect_ciph) {
123 /* implicit CM SERVICE ACCEPT? */
124 } else {
125 /* explicit CM SERVICE ACCEPT */
126 BSSAP.receive(tr_PDU_DTAP_MT(tr_CM_SERV_ACC));
Harald Weltea49e36e2018-01-21 19:29:33 +0100127 }
Harald Weltea49e36e2018-01-21 19:29:33 +0100128}
129
130/* build a PDU_ML3_MS_NW containing a Location Update by IMSI */
131function f_build_lu_imsi(hexstring imsi) return PDU_ML3_MS_NW
132{
133 var MobileIdentityLV mi := valueof(ts_MI_IMSI_LV(imsi));
134 return f_build_lu(mi);
135}
Harald Welteba7b6d92018-01-23 21:32:34 +0100136function f_build_lu_imei(hexstring imei) return PDU_ML3_MS_NW
137{
138 var MobileIdentityLV mi := valueof(ts_MI_IMEI_LV(imei));
139 return f_build_lu(mi);
140}
141function f_build_lu_tmsi(OCT4 tmsi) return PDU_ML3_MS_NW
142{
143 var MobileIdentityLV mi := valueof(ts_MI_TMSI_LV(tmsi));
144 return f_build_lu(mi);
145}
Harald Weltea49e36e2018-01-21 19:29:33 +0100146private function f_build_lu(MobileIdentityLV mi) return PDU_ML3_MS_NW
147{
148 var LocationAreaIdentification_V old_lai := { '62F220'O, '9999'O };
149 var PDU_ML3_MS_NW l3_info := valueof(ts_ML3_MO_LU_Req(valueof(ts_ML3_IE_LuType_Attach),
150 old_lai, mi, valueof(ts_CM1)));
151 return l3_info;
152}
153
Harald Weltecf66d5a2018-01-23 19:24:28 +0100154private function f_rnd_oct(integer len) return octetstring {
155 var integer i;
156 var octetstring res;
157 for (i := 0; i < len; i := i + 1) {
158 res[i] := int2oct(float2int(rnd()*256.0), 1);
159 }
160 return res;
161}
162
163function f_gen_auth_vec_2g() return AuthVector {
164 var AuthVector vec;
165 vec.rand := f_rnd_oct(16);
166 vec.sres := f_rnd_oct(4);
167 vec.kc := f_rnd_oct(8);
168 return vec;
169}
170
Harald Welte148a7082018-01-26 18:56:43 +0100171
172function f_mm_common(boolean expect_auth, boolean expect_ciph) runs on BSC_ConnHdlr
173{
174 if (expect_auth) {
175 g_pars.vec := f_gen_auth_vec_2g();
176 var GSUP_IE auth_tuple := valueof(ts_GSUP_IE_AuthTuple2G(g_pars.vec.rand,
177 g_pars.vec.sres,
178 g_pars.vec.kc));
179 GSUP.receive(tr_GSUP_SAI_REQ(g_pars.imsi));
180 GSUP.send(ts_GSUP_SAI_RES(g_pars.imsi, auth_tuple));
181
182 BSSAP.receive(tr_PDU_DTAP_MT(tr_ML3_MT_MM_AUTH_REQ(g_pars.vec.rand)));
183 BSSAP.send(ts_PDU_DTAP_MO(ts_ML3_MT_MM_AUTH_RESP_2G(g_pars.vec.sres)));
184 }
185
186 if (expect_ciph) {
187 BSSAP.receive(tr_BSSMAP_CipherModeCmd(?, g_pars.vec.kc));
188 BSSAP.send(ts_BSSMAP_CipherModeCompl('02'O));
189 }
190}
191
Harald Welte16114282018-01-24 22:41:21 +0100192function f_perform_lu(boolean expect_auth, boolean expect_tmsi, boolean send_early_cm,
193 boolean expect_ciph := false)
Harald Weltea49e36e2018-01-21 19:29:33 +0100194runs on BSC_ConnHdlr {
195 var PDU_ML3_MS_NW l3_lu := f_build_lu_imsi(g_pars.imsi)
196 var PDU_DTAP_MT dtap_mt;
197
198 /* tell GSUP dispatcher to send this IMSI to us */
199 f_create_gsup_expect(hex2str(g_pars.imsi));
200
201 /* Send BSSAP_Conn_Req with COMPL L3 INFO to MSC */
202 f_bssap_compl_l3(l3_lu);
203
Harald Welte8a121b32018-01-22 03:00:41 +0100204 if (send_early_cm) {
205 BSSAP.send(ts_BSSMAP_ClassmarkUpd(g_pars.cm2, g_pars.cm3));
206 }
Harald Welte5c2622c2018-01-21 20:45:20 +0100207
Harald Welte148a7082018-01-26 18:56:43 +0100208 f_mm_common(expect_auth, expect_ciph);
Harald Welte16114282018-01-24 22:41:21 +0100209
Harald Weltea49e36e2018-01-21 19:29:33 +0100210 /* Expect MSC to perform LU with HLR */
211 GSUP.receive(tr_GSUP_UL_REQ(g_pars.imsi));
212 GSUP.send(ts_GSUP_ISD_REQ(g_pars.imsi, g_pars.msisdn));
213 GSUP.receive(tr_GSUP_ISD_RES(g_pars.imsi));
214 GSUP.send(ts_GSUP_UL_RES(g_pars.imsi));
215
216 alt {
217 [] BSSAP.receive(tr_PDU_DTAP_MT(tr_ML3_MT_LU_Acc)) -> value dtap_mt {
218 var PDU_ML3_LocationUpdateAccept lu_acc := dtap_mt.dtap.msgs.mm.locationUpdateAccept;
219 if (expect_tmsi) {
220 if (not ispresent(lu_acc.mobileIdentityTLV) or
221 not ischosen(lu_acc.mobileIdentityTLV.mobileIdentityLV.mobileIdentityV.oddEvenInd_identity.tmsi_ptmsi)) {
222 setverdict(fail, "Expected TMSI but no TMSI was allocated");
223 self.stop;
224 } else {
Harald Welte256571e2018-01-24 18:47:19 +0100225 g_pars.tmsi := lu_acc.mobileIdentityTLV.mobileIdentityLV.mobileIdentityV.oddEvenInd_identity.tmsi_ptmsi.octets;
Harald Weltea49e36e2018-01-21 19:29:33 +0100226 BSSAP.send(ts_PDU_DTAP_MO(ts_ML3_MO_TmsiRealloc_Cmpl));
227 }
228 } else {
229 if (ispresent(lu_acc.mobileIdentityTLV) and
230 ischosen(lu_acc.mobileIdentityTLV.mobileIdentityLV.mobileIdentityV.oddEvenInd_identity.tmsi_ptmsi)) {
231 setverdict(fail, "Expected no TMSI but TMSI was allocated");
232 self.stop;
233 }
234 }
235 }
236 [] BSSAP.receive(tr_PDU_DTAP_MT(tr_ML3_MT_LU_Rej)) {
237 setverdict(fail, "Expected LU ACK, but received LU REJ");
238 self.stop;
239 }
240 }
241 /* FIXME: there could be pending SMS or other common procedures by the MSC, let's ignore them */
242 BSSAP.receive(tr_BSSMAP_ClearCommand);
243 BSSAP.send(ts_BSSMAP_ClearComplete);
244 BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_IND);
245 setverdict(pass);
246}
247
248function f_foo() runs on BSC_ConnHdlr{
249 /* SCCP CC handled by BSSMAP_Emulation_CT.main() */
250 /* Expect auth, if enabled */
251
252 /* TODO: ISD */
253 /* Expect encr, if enabled */
254 /* Expect encr, if enabled */
255 /* Expect ASS CMD, if chan_type != requested */
256 /* Send ASS CMPL in successful case */
257
258 /* Expect AoIP port/ip information for RTP stream */
259 /* Expect MSC-originated MGCP to our simulated MGW */
260 /* Verify Counters via CTRL */
261 /* re-configure MSC behaviour via VTY */
262}
263
264
265
266
267
268}
269
270