Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 1 | module BSC_MS_ConnectionHandler { |
| 2 | |
| 3 | import from General_Types all; |
| 4 | import from Osmocom_Types all; |
| 5 | import from SCCPasp_Types all; |
| 6 | import from BSSAP_Types all; |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 7 | import from BSSAP_CodecPort all; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 8 | import from BSSMAP_Emulation all; |
| 9 | import from BSSMAP_Templates all; |
| 10 | |
| 11 | import from MobileL3_Types all; |
| 12 | import from MobileL3_CommonIE_Types all; |
| 13 | import from L3_Templates all; |
| 14 | |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 15 | import from MGCP_Types all; |
| 16 | import from MGCP_Templates all; |
| 17 | import from SDP_Types all; |
| 18 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 19 | /* this component represents a single subscriber connection at the MSC. |
| 20 | * There is a 1:1 mapping between SCCP connections and BSSAP_ConnHdlr components. |
| 21 | * We inherit all component variables, ports, functions, ... from BSSAP_ConnHdlr */ |
| 22 | type component BSC_MS_ConnHdlr extends BSSAP_ConnHdlr { |
| 23 | /* SCCP Connecction Identifier for the underlying SCCP connection */ |
| 24 | var integer g_sccp_conn_id; |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 25 | var MgcpConnectionId g_mgcp_conn_id; |
| 26 | var SDP_Message g_sdp; |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 27 | var BSC_State g_state; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | /* Callback function from general BSSMAP_Emulation whenever a new incoming |
| 31 | * SCCP connection arrivces. Must create + start a new component */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 32 | private function CreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id) |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 33 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 34 | log("Incoming SCCP Connection on BSC ?!?"); |
| 35 | self.stop; |
| 36 | } |
| 37 | |
| 38 | /* Callback function from general BSSMAP_Emulation whenever a connectionless |
| 39 | * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */ |
| 40 | private function UnitdataCallback(PDU_BSSAP bssap) |
| 41 | runs on BSSMAP_Emulation_CT return template PDU_BSSAP { |
| 42 | var template PDU_BSSAP resp := omit; |
| 43 | |
| 44 | if (match(bssap, tr_BSSMAP_Reset)) { |
| 45 | resp := ts_BSSMAP_ResetAck; |
| 46 | } |
| 47 | |
| 48 | return resp; |
| 49 | } |
| 50 | |
| 51 | const BssmapOps BSC_MS_BssmapOps := { |
| 52 | create_cb := refers(CreateCallback), |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 53 | unitdata_cb := refers(UnitdataCallback), |
| 54 | decode_dtap := false, |
Daniel Willmann | aa17059 | 2018-10-24 18:33:10 +0200 | [diff] [blame] | 55 | role_ms := true, |
| 56 | sccp_addr_local := omit, |
| 57 | sccp_addr_peer := omit |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
| 61 | function f_gen_cl3(hexstring imsi) return PDU_BSSAP { |
| 62 | var MobileIdentityLV mi := valueof(ts_MI_IMSI_LV(imsi)); |
Harald Welte | 6ed6bf9 | 2018-01-24 21:09:15 +0100 | [diff] [blame] | 63 | var PDU_ML3_MS_NW l3 := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi)); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 64 | var BSSMAP_IE_CellIdentifier cell_id := valueof(ts_CellID_LAC_CI(23, 42)); |
| 65 | var PDU_BSSAP bssap := valueof(ts_BSSMAP_ComplL3(cell_id, enc_PDU_ML3_MS_NW(l3))); |
| 66 | return bssap; |
| 67 | } |
| 68 | |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 69 | type enumerated BSC_State { |
| 70 | BSC_STATE_NONE, |
| 71 | BSC_STATE_WAIT_ASS_REQ, |
| 72 | BSC_STATE_WAIT_CRCX, |
| 73 | BSC_STATE_WAIT_MDCX, |
Daniel Willmann | 04541d7 | 2017-11-30 13:14:37 +0100 | [diff] [blame] | 74 | BSC_STATE_WAIT_MDCX2, |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 75 | BSC_STATE_WAIT_CLEAR_CMD, |
| 76 | BSC_STATE_WAIT_DLCX, |
| 77 | BSC_STATE_WAIT_DISC_IND |
| 78 | } |
| 79 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 80 | /* main function processing various incoming events */ |
| 81 | function main(SCCP_PAR_Address sccp_addr_own, SCCP_PAR_Address sccp_addr_remote) |
| 82 | runs on BSC_MS_ConnHdlr { |
| 83 | var PDU_BSSAP bssap; |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 84 | var MgcpCommand mgcp_cmd; |
| 85 | var MgcpResponse mgcp_resp; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 86 | |
| 87 | log("Starting main of BSC_MS_ConnHdlr"); |
| 88 | |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 89 | g_mgcp_conn_id := f_mgcp_alloc_conn_id(); |
| 90 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 91 | /* generate and send the Complete Layer3 Info */ |
| 92 | bssap := f_gen_cl3('901770123456789'H); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 93 | var BSSAP_Conn_Req creq := { |
| 94 | addr_peer := sccp_addr_remote, |
| 95 | addr_own := sccp_addr_own, |
| 96 | bssap := bssap |
| 97 | } |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 98 | g_state := BSC_STATE_WAIT_ASS_REQ; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 99 | BSSAP.send(creq); |
| 100 | |
| 101 | while (true) { |
| 102 | alt { |
| 103 | /* new SCCP-level connection indication from BSC */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 104 | [g_state == BSC_STATE_WAIT_ASS_REQ] BSSAP.receive(tr_BSSMAP_AssignmentReq) -> value bssap { |
Harald Welte | 8f3a962 | 2017-11-25 01:19:13 +0100 | [diff] [blame] | 105 | /* FIXME: Read CIC */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 106 | /* respond with ASSIGNMENT COMPL */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 107 | g_state := BSC_STATE_WAIT_CRCX; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 108 | BSSAP.send(ts_BSSMAP_AssignmentComplete(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)); |
| 109 | } |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 110 | |
| 111 | /* CRCX -> OK */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 112 | [g_state == BSC_STATE_WAIT_CRCX] BSSAP.receive(tr_CRCX) -> value mgcp_cmd { |
Harald Welte | 8f3a962 | 2017-11-25 01:19:13 +0100 | [diff] [blame] | 113 | /* FIXME: proper SDP parameters */ |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 114 | g_sdp := valueof(ts_SDP("127.0.0.1", "127.0.0.1", "foo", "21", 1000, { "98" }, |
| 115 | { valueof(ts_SDP_rtpmap(98, "AMR/8000")), |
| 116 | valueof(ts_SDP_ptime(20)) })); |
| 117 | /* respond with CRCX_ACK */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 118 | g_state := BSC_STATE_WAIT_MDCX; |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 119 | BSSAP.send(ts_CRCX_ACK(mgcp_cmd.line.trans_id, g_mgcp_conn_id, g_sdp)); |
| 120 | } |
| 121 | |
| 122 | /* MDCX -> OK */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 123 | [g_state == BSC_STATE_WAIT_MDCX] BSSAP.receive(tr_MDCX) -> value mgcp_cmd { |
Harald Welte | 8f3a962 | 2017-11-25 01:19:13 +0100 | [diff] [blame] | 124 | /* FIXME: verify if local part of endpoint name matches CIC */ |
Daniel Willmann | 04541d7 | 2017-11-30 13:14:37 +0100 | [diff] [blame] | 125 | /* respond with MDCX_ACK */ |
| 126 | g_state := BSC_STATE_WAIT_MDCX2; |
| 127 | BSSAP.send(ts_MDCX_ACK(mgcp_cmd.line.trans_id, g_mgcp_conn_id, g_sdp)); |
| 128 | } |
| 129 | |
| 130 | /* MDCX -> OK */ |
| 131 | [g_state == BSC_STATE_WAIT_MDCX2] BSSAP.receive(tr_MDCX) -> value mgcp_cmd { |
| 132 | /* FIXME: verify if local part of endpoint name matches CIC */ |
| 133 | /* respond with MDCX_ACK */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 134 | g_state := BSC_STATE_WAIT_CLEAR_CMD; |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 135 | BSSAP.send(ts_MDCX_ACK(mgcp_cmd.line.trans_id, g_mgcp_conn_id, g_sdp)); |
| 136 | } |
| 137 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 138 | /* CLEAR COMMAND from MSC; respond with CLEAR COMPLETE) */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 139 | [g_state == BSC_STATE_WAIT_CLEAR_CMD] BSSAP.receive(tr_BSSMAP_ClearCommand) -> value bssap { |
| 140 | g_state := BSC_STATE_WAIT_DLCX; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 141 | BSSAP.send(ts_BSSMAP_ClearComplete); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 144 | /* DLCX -> OK */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 145 | [g_state == BSC_STATE_WAIT_DLCX] BSSAP.receive(tr_DLCX) -> value mgcp_cmd { |
Harald Welte | 8f3a962 | 2017-11-25 01:19:13 +0100 | [diff] [blame] | 146 | /* FIXME: verify if local part of endpoint name matches CIC */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 147 | g_state := BSC_STATE_WAIT_DISC_IND; |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 148 | BSSAP.send(ts_DLCX_ACK(mgcp_cmd.line.trans_id, g_mgcp_conn_id)); |
| 149 | } |
| 150 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 151 | [] BSSAP.receive(tr_BSSAP_DTAP) -> value bssap { |
Harald Welte | 8f3a962 | 2017-11-25 01:19:13 +0100 | [diff] [blame] | 152 | /* FIXME: verify if local part of endpoint name matches CIC */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 153 | var PDU_ML3_MS_NW l3 := dec_PDU_ML3_MS_NW(bssap.pdu.dtap); |
| 154 | log("Unhandled DTAP ", l3); |
| 155 | } |
| 156 | |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 157 | [g_state == BSC_STATE_WAIT_DISC_IND] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_IND) { |
Harald Welte | 0a4317a | 2017-11-25 00:32:46 +0100 | [diff] [blame] | 158 | setverdict(pass); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 159 | self.stop; |
| 160 | } |
| 161 | |
Daniel Willmann | aa17059 | 2018-10-24 18:33:10 +0200 | [diff] [blame] | 162 | [] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_CONF_IND) { |
| 163 | } |
| 164 | |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 165 | /* disconnect in invalid state */ |
| 166 | [] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_IND) { |
| 167 | setverdict(fail); |
| 168 | self.stop; |
| 169 | } |
| 170 | |
| 171 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 172 | [] BSSAP.receive(PDU_BSSAP:?) -> value bssap { |
| 173 | log("Received unhandled SCCP-CO: ", bssap); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | } |