blob: 35dbb63999e8bb3617f528573673ba703399b6c6 [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
Matt Johnson9e0bd802020-08-21 17:31:57 -0700103
104-type binary_or_false() :: false | binary().
105-spec req_resynchronization_info([tuple()]) -> binary_or_false().
106req_resynchronization_info([#'Requested-EUTRAN-Authentication-Info'{'Re-Synchronization-Info'=[]}]) ->
107 false;
108req_resynchronization_info([#'Requested-EUTRAN-Authentication-Info'{'Re-Synchronization-Info'=[Info]}]) ->
109 list_to_binary(Info);
110
111req_resynchronization_info([#'Requested-UTRAN-GERAN-Authentication-Info'{'Re-Synchronization-Info'=[]}]) ->
112 false;
113req_resynchronization_info([#'Requested-UTRAN-GERAN-Authentication-Info'{'Re-Synchronization-Info'=[Info]}]) ->
114 list_to_binary(Info);
115
116req_resynchronization_info(_) ->
117 false.
118
Harald Welte299ba932019-08-15 18:31:12 +0200119-define(PDP_TYPE_DEFAULT, <<0,0,0,16#21>>). % IPv4
120-define(PDP_QOS_DEFAULT, <<0,0,0,0,0,0,0,0,0,0,0,0,0,0>>). % fixme
121
Harald Welte44da7d72019-08-14 13:28:08 +0200122-spec gsup_pdp2dia('GSUPPdpInfo'()) -> #'PDP-Context'{}.
123gsup_pdp2dia(GsupPdpInfo) ->
Harald Welte299ba932019-08-15 18:31:12 +0200124 #'PDP-Context'{'PDP-Type' = maps:get(pdp_type, GsupPdpInfo, ?PDP_TYPE_DEFAULT),
Harald Welte44da7d72019-08-14 13:28:08 +0200125 'Context-Identifier' = maps:get(pdp_context_id, GsupPdpInfo),
Alexander Couzens7c912ff2023-04-26 17:58:00 +0000126 'Service-Selection' = decode_apn:decode_apn(maps:get(access_point_name, GsupPdpInfo)),
Harald Welte299ba932019-08-15 18:31:12 +0200127 'QoS-Subscribed' = maps:get(quality_of_service, GsupPdpInfo, ?PDP_QOS_DEFAULT)
Harald Welte44da7d72019-08-14 13:28:08 +0200128 }.
129
Harald Welte299ba932019-08-15 18:31:12 +0200130-define(PDN_TYPE_DEFAULT, 0). % IPv4
131-define(EPS_QOS_DEFAULT,
132 #'EPS-Subscribed-QoS-Profile'{'QoS-Class-Identifier'=9,
133 'Allocation-Retention-Priority'=
134 #'Allocation-Retention-Priority'{'Priority-Level'=8,
135 'Pre-emption-Capability'=1,
136 'Pre-emption-Vulnerability'=1}
137 }).
138
139-spec gsup_pdp2dia_apn('GSUPPdpInfo'()) -> #'APN-Configuration'{}.
140gsup_pdp2dia_apn(GsupPdpInfo) ->
141 #'APN-Configuration'{'Context-Identifier' = maps:get(pdp_context_id, GsupPdpInfo),
142 'PDN-Type' = maps:get(pdp_type, GsupPdpInfo, ?PDN_TYPE_DEFAULT),
143 % The EPS-Subscribed-QoS-Profile AVP and the AMBR AVP shall be present in the
144 % APN-Configuration AVP when the APN-Configuration AVP is sent in the
145 % APN-Configuration-Profile AVP and when the APN-Configuration-Profile AVP is
146 % sent within a ULA (as part of the Subscription-Data AVP).
147 'EPS-Subscribed-QoS-Profile' = ?EPS_QOS_DEFAULT,
148 'AMBR' = #'AMBR'{'Max-Requested-Bandwidth-UL' = 100000000,
149 'Max-Requested-Bandwidth-DL' = 100000000},
150 % The default APN Configuration shall not contain the Wildcard APN (see 3GPP TS
151 % 23.003 [3], clause 9.2); the default APN shall always contain an explicit APN
Alexander Couzens7c912ff2023-04-26 17:58:00 +0000152 'Service-Selection' = decode_apn:decode_apn(maps:get(access_point_name, GsupPdpInfo))
Harald Welte299ba932019-08-15 18:31:12 +0200153 }.
154
Harald Welte44da7d72019-08-14 13:28:08 +0200155% transient (only in Experimental-Result-Code)
156-define(DIAMETER_AUTHENTICATION_DATA_UNAVAILABLE, 4181).
157-define(DIAMETER_ERROR_CAMEL_SUBSCRIPTION_PRESENT, 4182).
158% permanent (only in Experimental-Result-Code)
159-define(DIAMETER_ERROR_USER_UNKNOWN, 5001).
160-define(DIAMETER_ERROR_ROAMING_NOT_ALLOWED, 5004).
161-define(DIAMETER_ERROR_UNKNOWN_EPS_SUBSCRIPTION, 5420).
162-define(DIAMETER_ERROR_RAT_NOT_ALLOWED, 5421).
163-define(DIAMETER_ERROR_EQUIPMENT_UNKNOWN, 5422).
164-define(DIAMETER_ERROR_UNKOWN_SERVING_NODE, 5423).
165
166% 10.5.5.14
167-define(GMM_CAUSE_IMSI_UNKNOWN, 16#02).
168-define(GMM_CAUSE_PLMN_NOTALLOWED, 16#0b).
169-define(GMM_CAUSE_GPRS_NOTALLOWED, 16#07).
170-define(GMM_CAUSE_INV_MAND_INFO, 16#60).
171-define(GMM_CAUSE_NET_FAIL, 16#11).
172% TODO: more values
173
174-define(EXP_RES(Foo), #'Experimental-Result'{'Vendor-Id'=fixme, 'Experimental-Result-Code'=Foo}).
175
176-type empty_or_intl() :: [] | [integer()].
177-spec gsup_cause2dia(integer()) -> {empty_or_intl(), empty_or_intl()}.
178gsup_cause2dia(?GMM_CAUSE_IMSI_UNKNOWN) -> {[], [?EXP_RES(?DIAMETER_ERROR_USER_UNKNOWN)]};
179gsup_cause2dia(?GMM_CAUSE_PLMN_NOTALLOWED) -> {[], [?DIAMETER_ERROR_ROAMING_NOT_ALLOWED]};
180gsup_cause2dia(?GMM_CAUSE_GPRS_NOTALLOWED) -> {[], [?DIAMETER_ERROR_RAT_NOT_ALLOWED]};
181%gsup_cause2dia(?GMM_CAUSE_INV_MAND_INFO) ->
182%gsup_cause2dia(?GMM_CAUSE_NET_FAIL) ->
183% TODO: more values
184gsup_cause2dia(_) -> {fixme, []}.
185
186% get the value for a tiven key in Map1. If not found, try same key in Map2. If not found, return Default
187-spec twomap_get(atom(), map(), map(), any()) -> any().
188twomap_get(Key, Map1, Map2, Default) ->
189 maps:get(Key, Map1, maps:get(Key, Map2, Default)).
190
191handle_request(#diameter_packet{msg = Req, errors = []}, _SvcName, {_, Caps}) when is_record(Req, 'AIR') ->
192 lager:info("AIR: ~p~n", [Req]),
193 % extract relevant fields from DIAMETER AIR
194 #diameter_caps{origin_host = {OH,_}, origin_realm = {OR,_}} = Caps,
195 #'AIR'{'Session-Id' = SessionId,
196 'User-Name' = UserName,
197 'Visited-PLMN-Id' = VplmnId,
198 'Requested-EUTRAN-Authentication-Info' = ReqEU,
199 'Requested-UTRAN-GERAN-Authentication-Info' = ReqUG} = Req,
200 VplmnIdBin = list_to_binary(VplmnId),
201 NumEutran = req_num_of_vec(ReqEU),
202 NumUgran = req_num_of_vec(ReqUG),
203 lager:info("Num EUTRAN=~p, UTRAN=~p~n", [NumEutran, NumUgran]),
204 % construct GSUP request to HLR and transceive it
Harald Welte388d3872019-12-01 17:03:15 +0100205 GsupTx1 = #{message_type => send_auth_info_req, imsi => list_to_binary(UserName),
206 supported_rat_types => [rat_eutran_sgs], current_rat_type => rat_eutran_sgs},
Matt Johnson9e0bd802020-08-21 17:31:57 -0700207 ResyncInfo = req_resynchronization_info(ReqEU),
208 case ResyncInfo of
209 false ->
210 GsupTx2 = #{};
211 ValidResyncInfo ->
212 lager:info("ResyncInfo is valid ~p", [ResyncInfo]),
213 GsupTx2 = #{rand => binary:part(ValidResyncInfo, 0, 16),
214 auts => binary:part(ValidResyncInfo, 16, 14)}
Harald Welte332fe7f2019-08-20 22:36:50 +0200215 end,
216 GsupTx = maps:merge(GsupTx1, GsupTx2),
Harald Welte44da7d72019-08-14 13:28:08 +0200217 GsupRx = gen_server:call(gsup_client, {transceive_gsup, GsupTx, send_auth_info_res, send_auth_info_err}),
218 lager:info("GsupRx: ~p~n", [GsupRx]),
219 % construct DIAMETER AIA response
220 case GsupRx of
221 #{message_type:=send_auth_info_res, auth_tuples:=GsupAuthTuples} ->
222 AuthInfo = gsup_tuples2dia(GsupAuthTuples, VplmnIdBin, NumEutran, NumUgran, NumUgran),
223 Resp = #'AIA'{'Session-Id'=SessionId, 'Origin-Host'=OH, 'Origin-Realm'=OR,
224 'Result-Code'=2001, 'Auth-Session-State'=1,
225 'Authentication-Info'=AuthInfo};
226 #{message_type := send_auth_info_err} ->
227 Resp = #'AIA'{'Session-Id'=SessionId, 'Origin-Host'=OH, 'Origin-Realm'=OR,
228 'Result-Code'=?DIAMETER_ERROR_USER_UNKNOWN,
229 'Auth-Session-State'=1};
230 timeout ->
231 Resp = #'AIA'{'Session-Id'=SessionId, 'Origin-Host'=OH, 'Origin-Realm'=OR,
232 'Result-Code'=4181, 'Auth-Session-State'=1}
233 end,
234 lager:info("Resp: ~p~n", [Resp]),
235 {reply, Resp};
236
237handle_request(#diameter_packet{msg = Req, errors = []}, _SvcName, {_, Caps}) when is_record(Req, 'ULR') ->
Harald Welte6f529082019-08-21 14:54:27 +0200238 % extract relevant fields from DIAMETER ULR
Harald Welte44da7d72019-08-14 13:28:08 +0200239 #diameter_caps{origin_host = {OH,_}, origin_realm = {OR,_}} = Caps,
240 #'ULR'{'Session-Id' = SessionId,
241 'RAT-Type' = RatType,
242 'ULR-Flags' = UlrFlags,
243 'User-Name' = UserName} = Req,
244
245 % construct GSUP UpdateLocation request to HLR and transceive it; expect InsertSubscrDataReq
Harald Welte299ba932019-08-15 18:31:12 +0200246 GsupTxUlReq = #{message_type => location_upd_req, imsi => list_to_binary(UserName),
247 cn_domain => 1},
Harald Welte44da7d72019-08-14 13:28:08 +0200248 GsupRxIsdReq = gen_server:call(gsup_client,
249 {transceive_gsup, GsupTxUlReq, insert_sub_data_req, location_upd_err}),
250 lager:info("GsupRxIsdReq: ~p~n", [GsupRxIsdReq]),
251 case GsupRxIsdReq of
252 #{message_type:=location_upd_err, cause:=Cause} ->
253 {Res, ExpRes} = gsup_cause2dia(Cause),
254 Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1,
255 'Origin-Host'=OH, 'Origin-Realm'=OR,
256 'Result-Code'=Res, 'Experimental-Result'=ExpRes};
257 #{message_type:=insert_sub_data_req} ->
258 % construct GSUP InsertSubscrData response to HLR and transceive it; expect
259 % UpdateLocationRes
Harald Welte299ba932019-08-15 18:31:12 +0200260 GsupTxIsdRes = #{message_type => insert_sub_data_res,
261 imsi => list_to_binary(UserName)},
Harald Welte44da7d72019-08-14 13:28:08 +0200262 GsupRxUlRes = gen_server:call(gsup_client,
263 {transceive_gsup, GsupTxIsdRes, location_upd_res, location_upd_err}),
264 lager:info("GsupRxUlRes: ~p~n", [GsupRxUlRes]),
265
266 case GsupRxUlRes of
267 #{message_type:=location_upd_res} ->
268 Msisdn = twomap_get(msisdn, GsupRxIsdReq, GsupRxUlRes, []),
Harald Welte299ba932019-08-15 18:31:12 +0200269 Compl = twomap_get(pdp_info_complete, GsupRxIsdReq, GsupRxUlRes, 0),
270
271 % build the GPRS Subscription Data
Harald Welte44da7d72019-08-14 13:28:08 +0200272 PdpInfoList = twomap_get(pdp_info_list, GsupRxIsdReq, GsupRxUlRes, []),
Harald Welte299ba932019-08-15 18:31:12 +0200273 PdpContexts = lists:map(fun gsup_pdp2dia/1, PdpInfoList),
274 GSubD = #'GPRS-Subscription-Data'{'Complete-Data-List-Included-Indicator'=Compl,
Harald Welte44da7d72019-08-14 13:28:08 +0200275 'PDP-Context'=PdpContexts},
Harald Welte299ba932019-08-15 18:31:12 +0200276
277 % build the APN-Configuration-Profile
278 ApnCfgList = lists:map(fun gsup_pdp2dia_apn/1, PdpInfoList),
279 FirstApn = lists:nth(1, ApnCfgList),
280 DefaultCtxId = FirstApn#'APN-Configuration'.'Context-Identifier',
281 ApnCfgProf = #'APN-Configuration-Profile'{'Context-Identifier' = DefaultCtxId,
282 'All-APN-Configurations-Included-Indicator'=Compl,
283 'APN-Configuration' = ApnCfgList},
284
285 % put together the Subscription-Data and finally the ULA response
286 SubscrData = #'Subscription-Data'{'MSISDN' = Msisdn,
287
288 'Network-Access-Mode' = 0, % PACKET_AND_CIRCUIT
289 'GPRS-Subscription-Data' = GSubD,
290 % Subscriber-Status must be present in ULA
291 'Subscriber-Status' = 0,
292 % AMBR must be present if this is an ULA; let's permit 100MBps UL + DL
293 'AMBR' = #'AMBR'{'Max-Requested-Bandwidth-UL' = 100000000,
294 'Max-Requested-Bandwidth-DL' = 100000000},
295 'APN-Configuration-Profile' = ApnCfgProf},
296 Resp = #'ULA'{'Session-Id' = SessionId, 'Auth-Session-State' = 1,
297 'Origin-Host' = OH, 'Origin-Realm' = OR,
298 'Result-Code' = 2001,
299 'Subscription-Data' = SubscrData, 'ULA-Flags' = 0};
Harald Welte44da7d72019-08-14 13:28:08 +0200300 #{message_type:=location_upd_err, cause:=Cause} ->
301 {Res, ExpRes} = gsup_cause2dia(Cause),
302 Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1,
303 'Origin-Host'=OH, 'Origin-Realm'=OR,
304 'Result-Code'=Res, 'Experimental-Result'=ExpRes};
305 _ ->
306 Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1,
307 'Origin-Host'=OH, 'Origin-Realm'=OR,
308 'Result-Code'=fixme}
309 end
310 end,
Harald Welte299ba932019-08-15 18:31:12 +0200311 lager:info("ULR Resp: ~p~n", [Resp]),
Harald Welte44da7d72019-08-14 13:28:08 +0200312 {reply, Resp};
313
314handle_request(Packet, _SvcName, {_,_}) ->
315 lager:error("Unsuppoerted message: ~p~n", [Packet]),
Harald Welte61276c42019-08-10 22:14:50 +0200316 discard.