blob: dc7fe59d44b66dedd5f5d1adbda8cede8e80c97a [file] [log] [blame]
Harald Welte61276c42019-08-10 22:14:50 +02001-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 Welte44da7d72019-08-14 13:28:08 +02007-include_lib("osmo_gsup/include/gsup_protocol.hrl").
Harald Welte61276c42019-08-10 22:14:50 +02008
9
10%% diameter callbacks
11-export([peer_up/3, peer_down/3, pick_peer/4, prepare_request/3, prepare_retransmit/3,
12 handle_answer/4, handle_error/4, handle_request/3]).
13
14-define(UNEXPECTED, erlang:error({unexpected, ?MODULE, ?LINE})).
15
16peer_up(_SvcName, {PeerRef, Caps}, State) ->
17 lager:info("Peer up ~p - ~p~n", [PeerRef, lager:pr(Caps, ?MODULE)]),
18 State.
19
20peer_down(_SvcName, {PeerRef, Caps}, State) ->
21 lager:info("Peer down ~p - ~p~n", [PeerRef, lager:pr(Caps, ?MODULE)]),
22 State.
23
24pick_peer(_, _, _SvcName, _State) ->
25 ?UNEXPECTED.
26
27prepare_request(_, _SvcName, _Peer) ->
28 ?UNEXPECTED.
29
30prepare_retransmit(_Packet, _SvcName, _Peer) ->
31 ?UNEXPECTED.
32
33handle_answer(_Packet, _Request, _SvcName, _Peer) ->
34 ?UNEXPECTED.
35
36handle_error(_Reason, _Request, _SvcName, _Peer) ->
37 lager:error("Request error: ~p~n", [_Reason]),
38 ?UNEXPECTED.
39
Harald Welte44da7d72019-08-14 13:28:08 +020040% generate Diameter E-UTRAN / UTRAN / GERAN Vectors from GSUP tuple input
41-spec gsup_tuple2dia_eutran('GSUPAuthTuple'(), binary(), integer()) -> #'E-UTRAN-Vector'{}.
42gsup_tuple2dia_eutran(#{autn:=Autn, ck:=Ck, ik:=Ik, rand:=Rand, res:=Res}, Vplmn, Idx) ->
43 #'E-UTRAN-Vector'{'Item-Number'=Idx, 'RAND'=Rand, 'XRES'=Res , 'AUTN'=Autn,
44 'KASME'=compute_kasme(Ck, Ik, Vplmn, Autn)}.
45
46-spec gsup_tuple2dia_utran('GSUPAuthTuple'()) -> #'UTRAN-Vector'{}.
47gsup_tuple2dia_utran(#{autn:=Autn, ck:=Ck, ik:=Ik, rand:=Rand, res:=Res}) ->
48 #'UTRAN-Vector'{'RAND'=Rand, 'XRES'=Res, 'AUTN'=Autn, 'Confidentiality-Key'=Ck, 'Integrity-Key'=Ik}.
49
50-spec gsup_tuple2dia_geran('GSUPAuthTuple'()) -> #'GERAN-Vector'{}.
51gsup_tuple2dia_geran(#{rand:=Rand, sres:=Sres, kc:=Kc}) ->
52 #'GERAN-Vector'{'RAND'=Rand, 'SRES'=Sres, 'Kc'=Kc}.
53
54-spec gsup_tuples2dia_eutran(['GSUPAuthTuple'()], binary()) -> [#'E-UTRAN-Vector'{}].
55gsup_tuples2dia_eutran(List, Vplmn) -> gsup_tuples2dia_eutran(List, Vplmn, [], 1).
56gsup_tuples2dia_eutran([], _Vplmn, Out, _Idx) -> Out;
57gsup_tuples2dia_eutran([Head|Tail], Vplmn, Out, Ctr) ->
58 Dia = gsup_tuple2dia_eutran(Head, Vplmn, Ctr),
59 gsup_tuples2dia_eutran(Tail, Vplmn, [Dia|Out], Ctr+1).
60
61-type int_or_false() :: false | integer().
62-spec gsup_tuples2dia(['GSUPAuthTuple'()], binary(), int_or_false(), int_or_false(), int_or_false()) -> #'Authentication-Info'{}.
63gsup_tuples2dia(Tuples, Vplmn, NumEutran, NumUtran, NumGeran) ->
64 case NumEutran of
65 false -> EutranVecs = [];
66 0 -> EutranVecs = [];
67 _ -> EutranVecs = gsup_tuples2dia_eutran(lists:sublist(Tuples,NumEutran), Vplmn)
68 end,
69 case NumUtran of
70 false -> UtranVecs = [];
71 0 -> UtranVecs = [];
72 _ -> UtranVecs = lists:map(fun gsup_tuple2dia_utran/1, lists:sublist(Tuples,NumUtran))
73 end,
74 case NumGeran of
75 false -> GeranVecs = [];
76 0 -> GeranVecs = [];
77 _ -> GeranVecs = lists:map(fun gsup_tuple2dia_geran/1, lists:sublist(Tuples,NumGeran))
78 end,
79 #'Authentication-Info'{'E-UTRAN-Vector'=EutranVecs, 'UTRAN-Vector'=UtranVecs,
80 'GERAN-Vector'=GeranVecs}.
81
82
83-spec compute_kasme(<<_:16>>, <<_:16>>, <<_:3>>, <<_:16>>) -> <<_:32>>.
84compute_kasme(Ck, Ik, VplmnId, Autn) ->
85 Autn6 = binary_part(Autn, 0, 6),
86 K = <<Ck:16/binary, Ik:16/binary>>,
87 S = <<16, VplmnId:3/binary, 0, 3, Autn6:6/binary, 0, 6>>,
Daniel Willmann592cc8b2022-04-22 16:08:48 +020088 Release = erlang:system_info(otp_release),
89 if
90 Release >= "24" ->
91 crypto:macN(hmac, sha256, K, S, 32);
92 true ->
93 crypto:hmac(sha256, K, S, 32)
94 end.
Harald Welte44da7d72019-08-14 13:28:08 +020095
96-spec req_num_of_vec([tuple()]) -> int_or_false().
97req_num_of_vec([#'Requested-EUTRAN-Authentication-Info'{'Number-Of-Requested-Vectors'=[]}]) -> false;
98req_num_of_vec([#'Requested-EUTRAN-Authentication-Info'{'Number-Of-Requested-Vectors'=[Num]}]) -> Num;
99req_num_of_vec([#'Requested-UTRAN-GERAN-Authentication-Info'{'Number-Of-Requested-Vectors'=[]}]) -> false;
100req_num_of_vec([#'Requested-UTRAN-GERAN-Authentication-Info'{'Number-Of-Requested-Vectors'=[Num]}]) -> Num;
101req_num_of_vec(_) -> false.
102
Harald Welte299ba932019-08-15 18:31:12 +0200103-define(PDP_TYPE_DEFAULT, <<0,0,0,16#21>>). % IPv4
104-define(PDP_QOS_DEFAULT, <<0,0,0,0,0,0,0,0,0,0,0,0,0,0>>). % fixme
105
Harald Welte44da7d72019-08-14 13:28:08 +0200106-spec gsup_pdp2dia('GSUPPdpInfo'()) -> #'PDP-Context'{}.
107gsup_pdp2dia(GsupPdpInfo) ->
Harald Welte299ba932019-08-15 18:31:12 +0200108 #'PDP-Context'{'PDP-Type' = maps:get(pdp_type, GsupPdpInfo, ?PDP_TYPE_DEFAULT),
Harald Welte44da7d72019-08-14 13:28:08 +0200109 'Context-Identifier' = maps:get(pdp_context_id, GsupPdpInfo),
Harald Welte299ba932019-08-15 18:31:12 +0200110 'Service-Selection' = maps:get(access_point_name, GsupPdpInfo),
111 'QoS-Subscribed' = maps:get(quality_of_service, GsupPdpInfo, ?PDP_QOS_DEFAULT)
Harald Welte44da7d72019-08-14 13:28:08 +0200112 }.
113
Harald Welte299ba932019-08-15 18:31:12 +0200114-define(PDN_TYPE_DEFAULT, 0). % IPv4
115-define(EPS_QOS_DEFAULT,
116 #'EPS-Subscribed-QoS-Profile'{'QoS-Class-Identifier'=9,
117 'Allocation-Retention-Priority'=
118 #'Allocation-Retention-Priority'{'Priority-Level'=8,
119 'Pre-emption-Capability'=1,
120 'Pre-emption-Vulnerability'=1}
121 }).
122
123-spec gsup_pdp2dia_apn('GSUPPdpInfo'()) -> #'APN-Configuration'{}.
124gsup_pdp2dia_apn(GsupPdpInfo) ->
125 #'APN-Configuration'{'Context-Identifier' = maps:get(pdp_context_id, GsupPdpInfo),
126 'PDN-Type' = maps:get(pdp_type, GsupPdpInfo, ?PDN_TYPE_DEFAULT),
127 % The EPS-Subscribed-QoS-Profile AVP and the AMBR AVP shall be present in the
128 % APN-Configuration AVP when the APN-Configuration AVP is sent in the
129 % APN-Configuration-Profile AVP and when the APN-Configuration-Profile AVP is
130 % sent within a ULA (as part of the Subscription-Data AVP).
131 'EPS-Subscribed-QoS-Profile' = ?EPS_QOS_DEFAULT,
132 'AMBR' = #'AMBR'{'Max-Requested-Bandwidth-UL' = 100000000,
133 'Max-Requested-Bandwidth-DL' = 100000000},
134 % The default APN Configuration shall not contain the Wildcard APN (see 3GPP TS
135 % 23.003 [3], clause 9.2); the default APN shall always contain an explicit APN
136 'Service-Selection' = "internet"%maps:get(access_point_name, GsupPdpInfo)
137 }.
138
Harald Welte44da7d72019-08-14 13:28:08 +0200139% transient (only in Experimental-Result-Code)
140-define(DIAMETER_AUTHENTICATION_DATA_UNAVAILABLE, 4181).
141-define(DIAMETER_ERROR_CAMEL_SUBSCRIPTION_PRESENT, 4182).
142% permanent (only in Experimental-Result-Code)
143-define(DIAMETER_ERROR_USER_UNKNOWN, 5001).
144-define(DIAMETER_ERROR_ROAMING_NOT_ALLOWED, 5004).
145-define(DIAMETER_ERROR_UNKNOWN_EPS_SUBSCRIPTION, 5420).
146-define(DIAMETER_ERROR_RAT_NOT_ALLOWED, 5421).
147-define(DIAMETER_ERROR_EQUIPMENT_UNKNOWN, 5422).
148-define(DIAMETER_ERROR_UNKOWN_SERVING_NODE, 5423).
149
150% 10.5.5.14
151-define(GMM_CAUSE_IMSI_UNKNOWN, 16#02).
152-define(GMM_CAUSE_PLMN_NOTALLOWED, 16#0b).
153-define(GMM_CAUSE_GPRS_NOTALLOWED, 16#07).
154-define(GMM_CAUSE_INV_MAND_INFO, 16#60).
155-define(GMM_CAUSE_NET_FAIL, 16#11).
156% TODO: more values
157
158-define(EXP_RES(Foo), #'Experimental-Result'{'Vendor-Id'=fixme, 'Experimental-Result-Code'=Foo}).
159
160-type empty_or_intl() :: [] | [integer()].
161-spec gsup_cause2dia(integer()) -> {empty_or_intl(), empty_or_intl()}.
162gsup_cause2dia(?GMM_CAUSE_IMSI_UNKNOWN) -> {[], [?EXP_RES(?DIAMETER_ERROR_USER_UNKNOWN)]};
163gsup_cause2dia(?GMM_CAUSE_PLMN_NOTALLOWED) -> {[], [?DIAMETER_ERROR_ROAMING_NOT_ALLOWED]};
164gsup_cause2dia(?GMM_CAUSE_GPRS_NOTALLOWED) -> {[], [?DIAMETER_ERROR_RAT_NOT_ALLOWED]};
165%gsup_cause2dia(?GMM_CAUSE_INV_MAND_INFO) ->
166%gsup_cause2dia(?GMM_CAUSE_NET_FAIL) ->
167% TODO: more values
168gsup_cause2dia(_) -> {fixme, []}.
169
170% get the value for a tiven key in Map1. If not found, try same key in Map2. If not found, return Default
171-spec twomap_get(atom(), map(), map(), any()) -> any().
172twomap_get(Key, Map1, Map2, Default) ->
173 maps:get(Key, Map1, maps:get(Key, Map2, Default)).
174
175handle_request(#diameter_packet{msg = Req, errors = []}, _SvcName, {_, Caps}) when is_record(Req, 'AIR') ->
176 lager:info("AIR: ~p~n", [Req]),
177 % extract relevant fields from DIAMETER AIR
178 #diameter_caps{origin_host = {OH,_}, origin_realm = {OR,_}} = Caps,
179 #'AIR'{'Session-Id' = SessionId,
180 'User-Name' = UserName,
181 'Visited-PLMN-Id' = VplmnId,
182 'Requested-EUTRAN-Authentication-Info' = ReqEU,
183 'Requested-UTRAN-GERAN-Authentication-Info' = ReqUG} = Req,
184 VplmnIdBin = list_to_binary(VplmnId),
185 NumEutran = req_num_of_vec(ReqEU),
186 NumUgran = req_num_of_vec(ReqUG),
187 lager:info("Num EUTRAN=~p, UTRAN=~p~n", [NumEutran, NumUgran]),
188 % construct GSUP request to HLR and transceive it
Harald Welte388d3872019-12-01 17:03:15 +0100189 GsupTx1 = #{message_type => send_auth_info_req, imsi => list_to_binary(UserName),
190 supported_rat_types => [rat_eutran_sgs], current_rat_type => rat_eutran_sgs},
Harald Welte332fe7f2019-08-20 22:36:50 +0200191 case ReqEU of
192 #'Requested-EUTRAN-Authentication-Info'{'Re-Synchronization-Info' = ReSyncInfo}
193 when is_binary(ReSyncInfo) ->
194 GsupTx2 = #{rand => string:substr(ReSyncInfo, 1, 16),
195 auts => string:substr(ReSyncInfo, 17)};
196 _ ->
197 GsupTx2 = #{}
198 end,
199 GsupTx = maps:merge(GsupTx1, GsupTx2),
Harald Welte44da7d72019-08-14 13:28:08 +0200200 GsupRx = gen_server:call(gsup_client, {transceive_gsup, GsupTx, send_auth_info_res, send_auth_info_err}),
201 lager:info("GsupRx: ~p~n", [GsupRx]),
202 % construct DIAMETER AIA response
203 case GsupRx of
204 #{message_type:=send_auth_info_res, auth_tuples:=GsupAuthTuples} ->
205 AuthInfo = gsup_tuples2dia(GsupAuthTuples, VplmnIdBin, NumEutran, NumUgran, NumUgran),
206 Resp = #'AIA'{'Session-Id'=SessionId, 'Origin-Host'=OH, 'Origin-Realm'=OR,
207 'Result-Code'=2001, 'Auth-Session-State'=1,
208 'Authentication-Info'=AuthInfo};
209 #{message_type := send_auth_info_err} ->
210 Resp = #'AIA'{'Session-Id'=SessionId, 'Origin-Host'=OH, 'Origin-Realm'=OR,
211 'Result-Code'=?DIAMETER_ERROR_USER_UNKNOWN,
212 'Auth-Session-State'=1};
213 timeout ->
214 Resp = #'AIA'{'Session-Id'=SessionId, 'Origin-Host'=OH, 'Origin-Realm'=OR,
215 'Result-Code'=4181, 'Auth-Session-State'=1}
216 end,
217 lager:info("Resp: ~p~n", [Resp]),
218 {reply, Resp};
219
220handle_request(#diameter_packet{msg = Req, errors = []}, _SvcName, {_, Caps}) when is_record(Req, 'ULR') ->
Harald Welte6f529082019-08-21 14:54:27 +0200221 % extract relevant fields from DIAMETER ULR
Harald Welte44da7d72019-08-14 13:28:08 +0200222 #diameter_caps{origin_host = {OH,_}, origin_realm = {OR,_}} = Caps,
223 #'ULR'{'Session-Id' = SessionId,
224 'RAT-Type' = RatType,
225 'ULR-Flags' = UlrFlags,
226 'User-Name' = UserName} = Req,
227
228 % construct GSUP UpdateLocation request to HLR and transceive it; expect InsertSubscrDataReq
Harald Welte299ba932019-08-15 18:31:12 +0200229 GsupTxUlReq = #{message_type => location_upd_req, imsi => list_to_binary(UserName),
230 cn_domain => 1},
Harald Welte44da7d72019-08-14 13:28:08 +0200231 GsupRxIsdReq = gen_server:call(gsup_client,
232 {transceive_gsup, GsupTxUlReq, insert_sub_data_req, location_upd_err}),
233 lager:info("GsupRxIsdReq: ~p~n", [GsupRxIsdReq]),
234 case GsupRxIsdReq of
235 #{message_type:=location_upd_err, cause:=Cause} ->
236 {Res, ExpRes} = gsup_cause2dia(Cause),
237 Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1,
238 'Origin-Host'=OH, 'Origin-Realm'=OR,
239 'Result-Code'=Res, 'Experimental-Result'=ExpRes};
240 #{message_type:=insert_sub_data_req} ->
241 % construct GSUP InsertSubscrData response to HLR and transceive it; expect
242 % UpdateLocationRes
Harald Welte299ba932019-08-15 18:31:12 +0200243 GsupTxIsdRes = #{message_type => insert_sub_data_res,
244 imsi => list_to_binary(UserName)},
Harald Welte44da7d72019-08-14 13:28:08 +0200245 GsupRxUlRes = gen_server:call(gsup_client,
246 {transceive_gsup, GsupTxIsdRes, location_upd_res, location_upd_err}),
247 lager:info("GsupRxUlRes: ~p~n", [GsupRxUlRes]),
248
249 case GsupRxUlRes of
250 #{message_type:=location_upd_res} ->
251 Msisdn = twomap_get(msisdn, GsupRxIsdReq, GsupRxUlRes, []),
Harald Welte299ba932019-08-15 18:31:12 +0200252 Compl = twomap_get(pdp_info_complete, GsupRxIsdReq, GsupRxUlRes, 0),
253
254 % build the GPRS Subscription Data
Harald Welte44da7d72019-08-14 13:28:08 +0200255 PdpInfoList = twomap_get(pdp_info_list, GsupRxIsdReq, GsupRxUlRes, []),
Harald Welte299ba932019-08-15 18:31:12 +0200256 PdpContexts = lists:map(fun gsup_pdp2dia/1, PdpInfoList),
257 GSubD = #'GPRS-Subscription-Data'{'Complete-Data-List-Included-Indicator'=Compl,
Harald Welte44da7d72019-08-14 13:28:08 +0200258 'PDP-Context'=PdpContexts},
Harald Welte299ba932019-08-15 18:31:12 +0200259
260 % build the APN-Configuration-Profile
261 ApnCfgList = lists:map(fun gsup_pdp2dia_apn/1, PdpInfoList),
262 FirstApn = lists:nth(1, ApnCfgList),
263 DefaultCtxId = FirstApn#'APN-Configuration'.'Context-Identifier',
264 ApnCfgProf = #'APN-Configuration-Profile'{'Context-Identifier' = DefaultCtxId,
265 'All-APN-Configurations-Included-Indicator'=Compl,
266 'APN-Configuration' = ApnCfgList},
267
268 % put together the Subscription-Data and finally the ULA response
269 SubscrData = #'Subscription-Data'{'MSISDN' = Msisdn,
270
271 'Network-Access-Mode' = 0, % PACKET_AND_CIRCUIT
272 'GPRS-Subscription-Data' = GSubD,
273 % Subscriber-Status must be present in ULA
274 'Subscriber-Status' = 0,
275 % AMBR must be present if this is an ULA; let's permit 100MBps UL + DL
276 'AMBR' = #'AMBR'{'Max-Requested-Bandwidth-UL' = 100000000,
277 'Max-Requested-Bandwidth-DL' = 100000000},
278 'APN-Configuration-Profile' = ApnCfgProf},
279 Resp = #'ULA'{'Session-Id' = SessionId, 'Auth-Session-State' = 1,
280 'Origin-Host' = OH, 'Origin-Realm' = OR,
281 'Result-Code' = 2001,
282 'Subscription-Data' = SubscrData, 'ULA-Flags' = 0};
Harald Welte44da7d72019-08-14 13:28:08 +0200283 #{message_type:=location_upd_err, cause:=Cause} ->
284 {Res, ExpRes} = gsup_cause2dia(Cause),
285 Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1,
286 'Origin-Host'=OH, 'Origin-Realm'=OR,
287 'Result-Code'=Res, 'Experimental-Result'=ExpRes};
288 _ ->
289 Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1,
290 'Origin-Host'=OH, 'Origin-Realm'=OR,
291 'Result-Code'=fixme}
292 end
293 end,
Harald Welte299ba932019-08-15 18:31:12 +0200294 lager:info("ULR Resp: ~p~n", [Resp]),
Harald Welte44da7d72019-08-14 13:28:08 +0200295 {reply, Resp};
296
297handle_request(Packet, _SvcName, {_,_}) ->
298 lager:error("Unsuppoerted message: ~p~n", [Packet]),
Harald Welte61276c42019-08-10 22:14:50 +0200299 discard.