Harald Welte | 61276c4 | 2019-08-10 22:14:50 +0200 | [diff] [blame] | 1 | -module(server_cb). |
| 2 | |
| 3 | |
| 4 | -include_lib("diameter/include/diameter.hrl"). |
| 5 | -include_lib("diameter/include/diameter_gen_base_rfc6733.hrl"). |
| 6 | -include_lib("diameter_3gpp_ts29_272.hrl"). |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 7 | -include_lib("osmo_gsup/include/gsup_protocol.hrl"). |
Harald Welte | 61276c4 | 2019-08-10 22:14:50 +0200 | [diff] [blame] | 8 | |
Alexander Couzens | 6ba22c2 | 2023-09-01 15:42:18 +0200 | [diff] [blame] | 9 | -define(DIA_VENDOR_3GPP, 10415). |
Harald Welte | 61276c4 | 2019-08-10 22:14:50 +0200 | [diff] [blame] | 10 | |
| 11 | %% diameter callbacks |
| 12 | -export([peer_up/3, peer_down/3, pick_peer/4, prepare_request/3, prepare_retransmit/3, |
| 13 | handle_answer/4, handle_error/4, handle_request/3]). |
| 14 | |
| 15 | -define(UNEXPECTED, erlang:error({unexpected, ?MODULE, ?LINE})). |
| 16 | |
| 17 | peer_up(_SvcName, {PeerRef, Caps}, State) -> |
| 18 | lager:info("Peer up ~p - ~p~n", [PeerRef, lager:pr(Caps, ?MODULE)]), |
| 19 | State. |
| 20 | |
| 21 | peer_down(_SvcName, {PeerRef, Caps}, State) -> |
| 22 | lager:info("Peer down ~p - ~p~n", [PeerRef, lager:pr(Caps, ?MODULE)]), |
| 23 | State. |
| 24 | |
| 25 | pick_peer(_, _, _SvcName, _State) -> |
| 26 | ?UNEXPECTED. |
| 27 | |
| 28 | prepare_request(_, _SvcName, _Peer) -> |
| 29 | ?UNEXPECTED. |
| 30 | |
| 31 | prepare_retransmit(_Packet, _SvcName, _Peer) -> |
| 32 | ?UNEXPECTED. |
| 33 | |
| 34 | handle_answer(_Packet, _Request, _SvcName, _Peer) -> |
| 35 | ?UNEXPECTED. |
| 36 | |
| 37 | handle_error(_Reason, _Request, _SvcName, _Peer) -> |
| 38 | lager:error("Request error: ~p~n", [_Reason]), |
| 39 | ?UNEXPECTED. |
| 40 | |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 41 | % generate Diameter E-UTRAN / UTRAN / GERAN Vectors from GSUP tuple input |
| 42 | -spec gsup_tuple2dia_eutran('GSUPAuthTuple'(), binary(), integer()) -> #'E-UTRAN-Vector'{}. |
| 43 | gsup_tuple2dia_eutran(#{autn:=Autn, ck:=Ck, ik:=Ik, rand:=Rand, res:=Res}, Vplmn, Idx) -> |
| 44 | #'E-UTRAN-Vector'{'Item-Number'=Idx, 'RAND'=Rand, 'XRES'=Res , 'AUTN'=Autn, |
| 45 | 'KASME'=compute_kasme(Ck, Ik, Vplmn, Autn)}. |
| 46 | |
| 47 | -spec gsup_tuple2dia_utran('GSUPAuthTuple'()) -> #'UTRAN-Vector'{}. |
| 48 | gsup_tuple2dia_utran(#{autn:=Autn, ck:=Ck, ik:=Ik, rand:=Rand, res:=Res}) -> |
| 49 | #'UTRAN-Vector'{'RAND'=Rand, 'XRES'=Res, 'AUTN'=Autn, 'Confidentiality-Key'=Ck, 'Integrity-Key'=Ik}. |
| 50 | |
| 51 | -spec gsup_tuple2dia_geran('GSUPAuthTuple'()) -> #'GERAN-Vector'{}. |
| 52 | gsup_tuple2dia_geran(#{rand:=Rand, sres:=Sres, kc:=Kc}) -> |
| 53 | #'GERAN-Vector'{'RAND'=Rand, 'SRES'=Sres, 'Kc'=Kc}. |
| 54 | |
| 55 | -spec gsup_tuples2dia_eutran(['GSUPAuthTuple'()], binary()) -> [#'E-UTRAN-Vector'{}]. |
| 56 | gsup_tuples2dia_eutran(List, Vplmn) -> gsup_tuples2dia_eutran(List, Vplmn, [], 1). |
| 57 | gsup_tuples2dia_eutran([], _Vplmn, Out, _Idx) -> Out; |
| 58 | gsup_tuples2dia_eutran([Head|Tail], Vplmn, Out, Ctr) -> |
| 59 | Dia = gsup_tuple2dia_eutran(Head, Vplmn, Ctr), |
| 60 | gsup_tuples2dia_eutran(Tail, Vplmn, [Dia|Out], Ctr+1). |
| 61 | |
| 62 | -type int_or_false() :: false | integer(). |
| 63 | -spec gsup_tuples2dia(['GSUPAuthTuple'()], binary(), int_or_false(), int_or_false(), int_or_false()) -> #'Authentication-Info'{}. |
| 64 | gsup_tuples2dia(Tuples, Vplmn, NumEutran, NumUtran, NumGeran) -> |
| 65 | case NumEutran of |
| 66 | false -> EutranVecs = []; |
| 67 | 0 -> EutranVecs = []; |
| 68 | _ -> EutranVecs = gsup_tuples2dia_eutran(lists:sublist(Tuples,NumEutran), Vplmn) |
| 69 | end, |
| 70 | case NumUtran of |
| 71 | false -> UtranVecs = []; |
| 72 | 0 -> UtranVecs = []; |
| 73 | _ -> UtranVecs = lists:map(fun gsup_tuple2dia_utran/1, lists:sublist(Tuples,NumUtran)) |
| 74 | end, |
| 75 | case NumGeran of |
| 76 | false -> GeranVecs = []; |
| 77 | 0 -> GeranVecs = []; |
| 78 | _ -> GeranVecs = lists:map(fun gsup_tuple2dia_geran/1, lists:sublist(Tuples,NumGeran)) |
| 79 | end, |
| 80 | #'Authentication-Info'{'E-UTRAN-Vector'=EutranVecs, 'UTRAN-Vector'=UtranVecs, |
| 81 | 'GERAN-Vector'=GeranVecs}. |
| 82 | |
| 83 | |
| 84 | -spec compute_kasme(<<_:16>>, <<_:16>>, <<_:3>>, <<_:16>>) -> <<_:32>>. |
| 85 | compute_kasme(Ck, Ik, VplmnId, Autn) -> |
| 86 | Autn6 = binary_part(Autn, 0, 6), |
| 87 | K = <<Ck:16/binary, Ik:16/binary>>, |
| 88 | S = <<16, VplmnId:3/binary, 0, 3, Autn6:6/binary, 0, 6>>, |
Daniel Willmann | 592cc8b | 2022-04-22 16:08:48 +0200 | [diff] [blame] | 89 | Release = erlang:system_info(otp_release), |
| 90 | if |
| 91 | Release >= "24" -> |
| 92 | crypto:macN(hmac, sha256, K, S, 32); |
| 93 | true -> |
| 94 | crypto:hmac(sha256, K, S, 32) |
| 95 | end. |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 96 | |
| 97 | -spec req_num_of_vec([tuple()]) -> int_or_false(). |
| 98 | req_num_of_vec([#'Requested-EUTRAN-Authentication-Info'{'Number-Of-Requested-Vectors'=[]}]) -> false; |
| 99 | req_num_of_vec([#'Requested-EUTRAN-Authentication-Info'{'Number-Of-Requested-Vectors'=[Num]}]) -> Num; |
| 100 | req_num_of_vec([#'Requested-UTRAN-GERAN-Authentication-Info'{'Number-Of-Requested-Vectors'=[]}]) -> false; |
| 101 | req_num_of_vec([#'Requested-UTRAN-GERAN-Authentication-Info'{'Number-Of-Requested-Vectors'=[Num]}]) -> Num; |
| 102 | req_num_of_vec(_) -> false. |
| 103 | |
Matt Johnson | 9e0bd80 | 2020-08-21 17:31:57 -0700 | [diff] [blame] | 104 | |
| 105 | -type binary_or_false() :: false | binary(). |
| 106 | -spec req_resynchronization_info([tuple()]) -> binary_or_false(). |
| 107 | req_resynchronization_info([#'Requested-EUTRAN-Authentication-Info'{'Re-Synchronization-Info'=[]}]) -> |
| 108 | false; |
| 109 | req_resynchronization_info([#'Requested-EUTRAN-Authentication-Info'{'Re-Synchronization-Info'=[Info]}]) -> |
| 110 | list_to_binary(Info); |
| 111 | |
| 112 | req_resynchronization_info([#'Requested-UTRAN-GERAN-Authentication-Info'{'Re-Synchronization-Info'=[]}]) -> |
| 113 | false; |
| 114 | req_resynchronization_info([#'Requested-UTRAN-GERAN-Authentication-Info'{'Re-Synchronization-Info'=[Info]}]) -> |
| 115 | list_to_binary(Info); |
| 116 | |
| 117 | req_resynchronization_info(_) -> |
| 118 | false. |
| 119 | |
Harald Welte | 299ba93 | 2019-08-15 18:31:12 +0200 | [diff] [blame] | 120 | -define(PDP_TYPE_DEFAULT, <<0,0,0,16#21>>). % IPv4 |
| 121 | -define(PDP_QOS_DEFAULT, <<0,0,0,0,0,0,0,0,0,0,0,0,0,0>>). % fixme |
| 122 | |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 123 | -spec gsup_pdp2dia('GSUPPdpInfo'()) -> #'PDP-Context'{}. |
| 124 | gsup_pdp2dia(GsupPdpInfo) -> |
Harald Welte | 299ba93 | 2019-08-15 18:31:12 +0200 | [diff] [blame] | 125 | #'PDP-Context'{'PDP-Type' = maps:get(pdp_type, GsupPdpInfo, ?PDP_TYPE_DEFAULT), |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 126 | 'Context-Identifier' = maps:get(pdp_context_id, GsupPdpInfo), |
Alexander Couzens | 7c912ff | 2023-04-26 17:58:00 +0000 | [diff] [blame] | 127 | 'Service-Selection' = decode_apn:decode_apn(maps:get(access_point_name, GsupPdpInfo)), |
Harald Welte | 299ba93 | 2019-08-15 18:31:12 +0200 | [diff] [blame] | 128 | 'QoS-Subscribed' = maps:get(quality_of_service, GsupPdpInfo, ?PDP_QOS_DEFAULT) |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 129 | }. |
| 130 | |
Harald Welte | 299ba93 | 2019-08-15 18:31:12 +0200 | [diff] [blame] | 131 | -define(PDN_TYPE_DEFAULT, 0). % IPv4 |
| 132 | -define(EPS_QOS_DEFAULT, |
| 133 | #'EPS-Subscribed-QoS-Profile'{'QoS-Class-Identifier'=9, |
| 134 | 'Allocation-Retention-Priority'= |
| 135 | #'Allocation-Retention-Priority'{'Priority-Level'=8, |
| 136 | 'Pre-emption-Capability'=1, |
| 137 | 'Pre-emption-Vulnerability'=1} |
| 138 | }). |
| 139 | |
| 140 | -spec gsup_pdp2dia_apn('GSUPPdpInfo'()) -> #'APN-Configuration'{}. |
| 141 | gsup_pdp2dia_apn(GsupPdpInfo) -> |
| 142 | #'APN-Configuration'{'Context-Identifier' = maps:get(pdp_context_id, GsupPdpInfo), |
| 143 | 'PDN-Type' = maps:get(pdp_type, GsupPdpInfo, ?PDN_TYPE_DEFAULT), |
| 144 | % The EPS-Subscribed-QoS-Profile AVP and the AMBR AVP shall be present in the |
| 145 | % APN-Configuration AVP when the APN-Configuration AVP is sent in the |
| 146 | % APN-Configuration-Profile AVP and when the APN-Configuration-Profile AVP is |
| 147 | % sent within a ULA (as part of the Subscription-Data AVP). |
| 148 | 'EPS-Subscribed-QoS-Profile' = ?EPS_QOS_DEFAULT, |
| 149 | 'AMBR' = #'AMBR'{'Max-Requested-Bandwidth-UL' = 100000000, |
| 150 | 'Max-Requested-Bandwidth-DL' = 100000000}, |
| 151 | % The default APN Configuration shall not contain the Wildcard APN (see 3GPP TS |
| 152 | % 23.003 [3], clause 9.2); the default APN shall always contain an explicit APN |
Alexander Couzens | 7c912ff | 2023-04-26 17:58:00 +0000 | [diff] [blame] | 153 | 'Service-Selection' = decode_apn:decode_apn(maps:get(access_point_name, GsupPdpInfo)) |
Harald Welte | 299ba93 | 2019-08-15 18:31:12 +0200 | [diff] [blame] | 154 | }. |
| 155 | |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 156 | % transient (only in Experimental-Result-Code) |
| 157 | -define(DIAMETER_AUTHENTICATION_DATA_UNAVAILABLE, 4181). |
| 158 | -define(DIAMETER_ERROR_CAMEL_SUBSCRIPTION_PRESENT, 4182). |
| 159 | % permanent (only in Experimental-Result-Code) |
| 160 | -define(DIAMETER_ERROR_USER_UNKNOWN, 5001). |
Pau Espin Pedrol | b524e24 | 2023-08-30 16:33:55 +0200 | [diff] [blame] | 161 | -define(DIAMETER_AUTHORIZATION_REJECTED, 5003). |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 162 | -define(DIAMETER_ERROR_ROAMING_NOT_ALLOWED, 5004). |
Pau Espin Pedrol | b524e24 | 2023-08-30 16:33:55 +0200 | [diff] [blame] | 163 | -define(DIAMETER_MISSING_AVP, 5005). |
| 164 | -define(DIAMETER_UNABLE_TO_COMPLY, 5012). |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 165 | -define(DIAMETER_ERROR_UNKNOWN_EPS_SUBSCRIPTION, 5420). |
| 166 | -define(DIAMETER_ERROR_RAT_NOT_ALLOWED, 5421). |
| 167 | -define(DIAMETER_ERROR_EQUIPMENT_UNKNOWN, 5422). |
| 168 | -define(DIAMETER_ERROR_UNKOWN_SERVING_NODE, 5423). |
| 169 | |
| 170 | % 10.5.5.14 |
| 171 | -define(GMM_CAUSE_IMSI_UNKNOWN, 16#02). |
Pau Espin Pedrol | 5e11d28 | 2023-08-30 16:27:42 +0200 | [diff] [blame] | 172 | -define(GMM_CAUSE_ILLEGAL_MS, 16#03). |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 173 | -define(GMM_CAUSE_GPRS_NOTALLOWED, 16#07). |
Pau Espin Pedrol | 5e11d28 | 2023-08-30 16:27:42 +0200 | [diff] [blame] | 174 | -define(GMM_CAUSE_PLMN_NOTALLOWED, 16#0b). |
| 175 | -define(GMM_CAUSE_LA_NOTALLOWED, 16#0c). |
| 176 | -define(GMM_CAUSE_ROAMING_NOTALLOWED, 16#0d). |
| 177 | -define(GMM_CAUSE_NO_SUIT_CELL_IN_LA, 16#0f). |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 178 | -define(GMM_CAUSE_NET_FAIL, 16#11). |
Pau Espin Pedrol | 5e11d28 | 2023-08-30 16:27:42 +0200 | [diff] [blame] | 179 | -define(GMM_CAUSE_CONGESTION, 16#16). |
| 180 | -define(GMM_CAUSE_GSM_AUTH_UNACCEPT, 16#17). |
| 181 | -define(GMM_CAUSE_INV_MAND_INFO, 16#60). |
| 182 | -define(GMM_CAUSE_PROTO_ERR_UNSPEC, 16#6f). |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 183 | |
Alexander Couzens | 6ba22c2 | 2023-09-01 15:42:18 +0200 | [diff] [blame] | 184 | -define(EXP_RES(Exp), #'Experimental-Result'{'Vendor-Id'=?DIA_VENDOR_3GPP, 'Experimental-Result-Code'=Exp}). |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 185 | |
Pau Espin Pedrol | b524e24 | 2023-08-30 16:33:55 +0200 | [diff] [blame] | 186 | %% see 29.272 Annex A/B |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 187 | -type empty_or_intl() :: [] | [integer()]. |
| 188 | -spec gsup_cause2dia(integer()) -> {empty_or_intl(), empty_or_intl()}. |
| 189 | gsup_cause2dia(?GMM_CAUSE_IMSI_UNKNOWN) -> {[], [?EXP_RES(?DIAMETER_ERROR_USER_UNKNOWN)]}; |
Pau Espin Pedrol | b524e24 | 2023-08-30 16:33:55 +0200 | [diff] [blame] | 190 | gsup_cause2dia(?GMM_CAUSE_ILLEGAL_MS) -> {[], [?EXP_RES(?DIAMETER_ERROR_USER_UNKNOWN)]}; |
| 191 | gsup_cause2dia(?GMM_CAUSE_PLMN_NOTALLOWED) -> {[], [?EXP_RES(?DIAMETER_ERROR_ROAMING_NOT_ALLOWED)]}; |
| 192 | gsup_cause2dia(?GMM_CAUSE_GPRS_NOTALLOWED) -> {[], [?EXP_RES(?DIAMETER_ERROR_UNKNOWN_EPS_SUBSCRIPTION)]}; |
| 193 | |
| 194 | gsup_cause2dia(?GMM_CAUSE_LA_NOTALLOWED) -> {[?DIAMETER_AUTHORIZATION_REJECTED], []}; |
| 195 | gsup_cause2dia(?GMM_CAUSE_ROAMING_NOTALLOWED) -> {[], [?EXP_RES(?DIAMETER_ERROR_ROAMING_NOT_ALLOWED)]}; |
| 196 | gsup_cause2dia(?GMM_CAUSE_NO_SUIT_CELL_IN_LA) -> {[], [?EXP_RES(?DIAMETER_ERROR_UNKNOWN_EPS_SUBSCRIPTION)]}; |
| 197 | gsup_cause2dia(?GMM_CAUSE_NET_FAIL) -> {[?DIAMETER_UNABLE_TO_COMPLY], []}; |
| 198 | gsup_cause2dia(?GMM_CAUSE_CONGESTION) -> {[?DIAMETER_UNABLE_TO_COMPLY], []}; |
| 199 | gsup_cause2dia(?GMM_CAUSE_INV_MAND_INFO) -> {[?DIAMETER_MISSING_AVP], []}; |
| 200 | gsup_cause2dia(?GMM_CAUSE_PROTO_ERR_UNSPEC) -> {[?DIAMETER_UNABLE_TO_COMPLY], []}; |
Alexander Couzens | b8aef30 | 2023-09-01 15:44:58 +0200 | [diff] [blame^] | 201 | gsup_cause2dia(_) -> {[?DIAMETER_UNABLE_TO_COMPLY], []}. |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 202 | |
| 203 | % get the value for a tiven key in Map1. If not found, try same key in Map2. If not found, return Default |
| 204 | -spec twomap_get(atom(), map(), map(), any()) -> any(). |
| 205 | twomap_get(Key, Map1, Map2, Default) -> |
| 206 | maps:get(Key, Map1, maps:get(Key, Map2, Default)). |
| 207 | |
| 208 | handle_request(#diameter_packet{msg = Req, errors = []}, _SvcName, {_, Caps}) when is_record(Req, 'AIR') -> |
| 209 | lager:info("AIR: ~p~n", [Req]), |
| 210 | % extract relevant fields from DIAMETER AIR |
| 211 | #diameter_caps{origin_host = {OH,_}, origin_realm = {OR,_}} = Caps, |
| 212 | #'AIR'{'Session-Id' = SessionId, |
| 213 | 'User-Name' = UserName, |
| 214 | 'Visited-PLMN-Id' = VplmnId, |
| 215 | 'Requested-EUTRAN-Authentication-Info' = ReqEU, |
| 216 | 'Requested-UTRAN-GERAN-Authentication-Info' = ReqUG} = Req, |
| 217 | VplmnIdBin = list_to_binary(VplmnId), |
| 218 | NumEutran = req_num_of_vec(ReqEU), |
| 219 | NumUgran = req_num_of_vec(ReqUG), |
| 220 | lager:info("Num EUTRAN=~p, UTRAN=~p~n", [NumEutran, NumUgran]), |
| 221 | % construct GSUP request to HLR and transceive it |
Harald Welte | 388d387 | 2019-12-01 17:03:15 +0100 | [diff] [blame] | 222 | GsupTx1 = #{message_type => send_auth_info_req, imsi => list_to_binary(UserName), |
| 223 | supported_rat_types => [rat_eutran_sgs], current_rat_type => rat_eutran_sgs}, |
Matt Johnson | 9e0bd80 | 2020-08-21 17:31:57 -0700 | [diff] [blame] | 224 | ResyncInfo = req_resynchronization_info(ReqEU), |
| 225 | case ResyncInfo of |
| 226 | false -> |
| 227 | GsupTx2 = #{}; |
| 228 | ValidResyncInfo -> |
| 229 | lager:info("ResyncInfo is valid ~p", [ResyncInfo]), |
| 230 | GsupTx2 = #{rand => binary:part(ValidResyncInfo, 0, 16), |
| 231 | auts => binary:part(ValidResyncInfo, 16, 14)} |
Harald Welte | 332fe7f | 2019-08-20 22:36:50 +0200 | [diff] [blame] | 232 | end, |
| 233 | GsupTx = maps:merge(GsupTx1, GsupTx2), |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 234 | GsupRx = gen_server:call(gsup_client, {transceive_gsup, GsupTx, send_auth_info_res, send_auth_info_err}), |
| 235 | lager:info("GsupRx: ~p~n", [GsupRx]), |
| 236 | % construct DIAMETER AIA response |
| 237 | case GsupRx of |
| 238 | #{message_type:=send_auth_info_res, auth_tuples:=GsupAuthTuples} -> |
| 239 | AuthInfo = gsup_tuples2dia(GsupAuthTuples, VplmnIdBin, NumEutran, NumUgran, NumUgran), |
| 240 | Resp = #'AIA'{'Session-Id'=SessionId, 'Origin-Host'=OH, 'Origin-Realm'=OR, |
| 241 | 'Result-Code'=2001, 'Auth-Session-State'=1, |
| 242 | 'Authentication-Info'=AuthInfo}; |
Pau Espin Pedrol | b524e24 | 2023-08-30 16:33:55 +0200 | [diff] [blame] | 243 | #{message_type := send_auth_info_err, cause:=Cause} -> |
| 244 | {Res, ExpRes} = gsup_cause2dia(Cause), |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 245 | Resp = #'AIA'{'Session-Id'=SessionId, 'Origin-Host'=OH, 'Origin-Realm'=OR, |
Pau Espin Pedrol | b524e24 | 2023-08-30 16:33:55 +0200 | [diff] [blame] | 246 | 'Result-Code'=Res, |
| 247 | 'Experimental-Result'=ExpRes, |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 248 | 'Auth-Session-State'=1}; |
| 249 | timeout -> |
| 250 | Resp = #'AIA'{'Session-Id'=SessionId, 'Origin-Host'=OH, 'Origin-Realm'=OR, |
| 251 | 'Result-Code'=4181, 'Auth-Session-State'=1} |
| 252 | end, |
| 253 | lager:info("Resp: ~p~n", [Resp]), |
| 254 | {reply, Resp}; |
| 255 | |
| 256 | handle_request(#diameter_packet{msg = Req, errors = []}, _SvcName, {_, Caps}) when is_record(Req, 'ULR') -> |
Harald Welte | 6f52908 | 2019-08-21 14:54:27 +0200 | [diff] [blame] | 257 | % extract relevant fields from DIAMETER ULR |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 258 | #diameter_caps{origin_host = {OH,_}, origin_realm = {OR,_}} = Caps, |
| 259 | #'ULR'{'Session-Id' = SessionId, |
| 260 | 'RAT-Type' = RatType, |
| 261 | 'ULR-Flags' = UlrFlags, |
| 262 | 'User-Name' = UserName} = Req, |
| 263 | |
| 264 | % construct GSUP UpdateLocation request to HLR and transceive it; expect InsertSubscrDataReq |
Harald Welte | 299ba93 | 2019-08-15 18:31:12 +0200 | [diff] [blame] | 265 | GsupTxUlReq = #{message_type => location_upd_req, imsi => list_to_binary(UserName), |
| 266 | cn_domain => 1}, |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 267 | GsupRxIsdReq = gen_server:call(gsup_client, |
| 268 | {transceive_gsup, GsupTxUlReq, insert_sub_data_req, location_upd_err}), |
| 269 | lager:info("GsupRxIsdReq: ~p~n", [GsupRxIsdReq]), |
| 270 | case GsupRxIsdReq of |
| 271 | #{message_type:=location_upd_err, cause:=Cause} -> |
| 272 | {Res, ExpRes} = gsup_cause2dia(Cause), |
| 273 | Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1, |
| 274 | 'Origin-Host'=OH, 'Origin-Realm'=OR, |
| 275 | 'Result-Code'=Res, 'Experimental-Result'=ExpRes}; |
| 276 | #{message_type:=insert_sub_data_req} -> |
| 277 | % construct GSUP InsertSubscrData response to HLR and transceive it; expect |
| 278 | % UpdateLocationRes |
Harald Welte | 299ba93 | 2019-08-15 18:31:12 +0200 | [diff] [blame] | 279 | GsupTxIsdRes = #{message_type => insert_sub_data_res, |
| 280 | imsi => list_to_binary(UserName)}, |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 281 | GsupRxUlRes = gen_server:call(gsup_client, |
| 282 | {transceive_gsup, GsupTxIsdRes, location_upd_res, location_upd_err}), |
| 283 | lager:info("GsupRxUlRes: ~p~n", [GsupRxUlRes]), |
| 284 | |
| 285 | case GsupRxUlRes of |
| 286 | #{message_type:=location_upd_res} -> |
| 287 | Msisdn = twomap_get(msisdn, GsupRxIsdReq, GsupRxUlRes, []), |
Harald Welte | 299ba93 | 2019-08-15 18:31:12 +0200 | [diff] [blame] | 288 | Compl = twomap_get(pdp_info_complete, GsupRxIsdReq, GsupRxUlRes, 0), |
| 289 | |
| 290 | % build the GPRS Subscription Data |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 291 | PdpInfoList = twomap_get(pdp_info_list, GsupRxIsdReq, GsupRxUlRes, []), |
Harald Welte | 299ba93 | 2019-08-15 18:31:12 +0200 | [diff] [blame] | 292 | PdpContexts = lists:map(fun gsup_pdp2dia/1, PdpInfoList), |
| 293 | GSubD = #'GPRS-Subscription-Data'{'Complete-Data-List-Included-Indicator'=Compl, |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 294 | 'PDP-Context'=PdpContexts}, |
Harald Welte | 299ba93 | 2019-08-15 18:31:12 +0200 | [diff] [blame] | 295 | |
| 296 | % build the APN-Configuration-Profile |
| 297 | ApnCfgList = lists:map(fun gsup_pdp2dia_apn/1, PdpInfoList), |
| 298 | FirstApn = lists:nth(1, ApnCfgList), |
| 299 | DefaultCtxId = FirstApn#'APN-Configuration'.'Context-Identifier', |
| 300 | ApnCfgProf = #'APN-Configuration-Profile'{'Context-Identifier' = DefaultCtxId, |
| 301 | 'All-APN-Configurations-Included-Indicator'=Compl, |
| 302 | 'APN-Configuration' = ApnCfgList}, |
| 303 | |
| 304 | % put together the Subscription-Data and finally the ULA response |
| 305 | SubscrData = #'Subscription-Data'{'MSISDN' = Msisdn, |
| 306 | |
| 307 | 'Network-Access-Mode' = 0, % PACKET_AND_CIRCUIT |
| 308 | 'GPRS-Subscription-Data' = GSubD, |
| 309 | % Subscriber-Status must be present in ULA |
| 310 | 'Subscriber-Status' = 0, |
| 311 | % AMBR must be present if this is an ULA; let's permit 100MBps UL + DL |
| 312 | 'AMBR' = #'AMBR'{'Max-Requested-Bandwidth-UL' = 100000000, |
| 313 | 'Max-Requested-Bandwidth-DL' = 100000000}, |
| 314 | 'APN-Configuration-Profile' = ApnCfgProf}, |
| 315 | Resp = #'ULA'{'Session-Id' = SessionId, 'Auth-Session-State' = 1, |
| 316 | 'Origin-Host' = OH, 'Origin-Realm' = OR, |
| 317 | 'Result-Code' = 2001, |
| 318 | 'Subscription-Data' = SubscrData, 'ULA-Flags' = 0}; |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 319 | #{message_type:=location_upd_err, cause:=Cause} -> |
| 320 | {Res, ExpRes} = gsup_cause2dia(Cause), |
| 321 | Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1, |
| 322 | 'Origin-Host'=OH, 'Origin-Realm'=OR, |
| 323 | 'Result-Code'=Res, 'Experimental-Result'=ExpRes}; |
| 324 | _ -> |
| 325 | Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1, |
| 326 | 'Origin-Host'=OH, 'Origin-Realm'=OR, |
| 327 | 'Result-Code'=fixme} |
| 328 | end |
| 329 | end, |
Harald Welte | 299ba93 | 2019-08-15 18:31:12 +0200 | [diff] [blame] | 330 | lager:info("ULR Resp: ~p~n", [Resp]), |
Harald Welte | 44da7d7 | 2019-08-14 13:28:08 +0200 | [diff] [blame] | 331 | {reply, Resp}; |
| 332 | |
| 333 | handle_request(Packet, _SvcName, {_,_}) -> |
| 334 | lager:error("Unsuppoerted message: ~p~n", [Packet]), |
Harald Welte | 61276c4 | 2019-08-10 22:14:50 +0200 | [diff] [blame] | 335 | discard. |