blob: c9a14f03c106de5ec054da0ab92441e6b79ed798 [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>>,
88 crypto:hmac(sha256, K, S, 32).
89
90-spec req_num_of_vec([tuple()]) -> int_or_false().
91req_num_of_vec([#'Requested-EUTRAN-Authentication-Info'{'Number-Of-Requested-Vectors'=[]}]) -> false;
92req_num_of_vec([#'Requested-EUTRAN-Authentication-Info'{'Number-Of-Requested-Vectors'=[Num]}]) -> Num;
93req_num_of_vec([#'Requested-UTRAN-GERAN-Authentication-Info'{'Number-Of-Requested-Vectors'=[]}]) -> false;
94req_num_of_vec([#'Requested-UTRAN-GERAN-Authentication-Info'{'Number-Of-Requested-Vectors'=[Num]}]) -> Num;
95req_num_of_vec(_) -> false.
96
97-spec gsup_pdp2dia('GSUPPdpInfo'()) -> #'PDP-Context'{}.
98gsup_pdp2dia(GsupPdpInfo) ->
99 #'PDP-Context'{'PDP-Type' = maps:get(pdp_type, GsupPdpInfo),
100 'Context-Identifier' = maps:get(pdp_context_id, GsupPdpInfo),
101 'PDP-Address' = maps:get(access_point_name, GsupPdpInfo),
102 'Service-Selection' = fixme,
103 'QoS-Subscribed' = maps:get(quality_of_service, GsupPdpInfo)
104 }.
105
106% transient (only in Experimental-Result-Code)
107-define(DIAMETER_AUTHENTICATION_DATA_UNAVAILABLE, 4181).
108-define(DIAMETER_ERROR_CAMEL_SUBSCRIPTION_PRESENT, 4182).
109% permanent (only in Experimental-Result-Code)
110-define(DIAMETER_ERROR_USER_UNKNOWN, 5001).
111-define(DIAMETER_ERROR_ROAMING_NOT_ALLOWED, 5004).
112-define(DIAMETER_ERROR_UNKNOWN_EPS_SUBSCRIPTION, 5420).
113-define(DIAMETER_ERROR_RAT_NOT_ALLOWED, 5421).
114-define(DIAMETER_ERROR_EQUIPMENT_UNKNOWN, 5422).
115-define(DIAMETER_ERROR_UNKOWN_SERVING_NODE, 5423).
116
117% 10.5.5.14
118-define(GMM_CAUSE_IMSI_UNKNOWN, 16#02).
119-define(GMM_CAUSE_PLMN_NOTALLOWED, 16#0b).
120-define(GMM_CAUSE_GPRS_NOTALLOWED, 16#07).
121-define(GMM_CAUSE_INV_MAND_INFO, 16#60).
122-define(GMM_CAUSE_NET_FAIL, 16#11).
123% TODO: more values
124
125-define(EXP_RES(Foo), #'Experimental-Result'{'Vendor-Id'=fixme, 'Experimental-Result-Code'=Foo}).
126
127-type empty_or_intl() :: [] | [integer()].
128-spec gsup_cause2dia(integer()) -> {empty_or_intl(), empty_or_intl()}.
129gsup_cause2dia(?GMM_CAUSE_IMSI_UNKNOWN) -> {[], [?EXP_RES(?DIAMETER_ERROR_USER_UNKNOWN)]};
130gsup_cause2dia(?GMM_CAUSE_PLMN_NOTALLOWED) -> {[], [?DIAMETER_ERROR_ROAMING_NOT_ALLOWED]};
131gsup_cause2dia(?GMM_CAUSE_GPRS_NOTALLOWED) -> {[], [?DIAMETER_ERROR_RAT_NOT_ALLOWED]};
132%gsup_cause2dia(?GMM_CAUSE_INV_MAND_INFO) ->
133%gsup_cause2dia(?GMM_CAUSE_NET_FAIL) ->
134% TODO: more values
135gsup_cause2dia(_) -> {fixme, []}.
136
137% get the value for a tiven key in Map1. If not found, try same key in Map2. If not found, return Default
138-spec twomap_get(atom(), map(), map(), any()) -> any().
139twomap_get(Key, Map1, Map2, Default) ->
140 maps:get(Key, Map1, maps:get(Key, Map2, Default)).
141
142handle_request(#diameter_packet{msg = Req, errors = []}, _SvcName, {_, Caps}) when is_record(Req, 'AIR') ->
143 lager:info("AIR: ~p~n", [Req]),
144 % extract relevant fields from DIAMETER AIR
145 #diameter_caps{origin_host = {OH,_}, origin_realm = {OR,_}} = Caps,
146 #'AIR'{'Session-Id' = SessionId,
147 'User-Name' = UserName,
148 'Visited-PLMN-Id' = VplmnId,
149 'Requested-EUTRAN-Authentication-Info' = ReqEU,
150 'Requested-UTRAN-GERAN-Authentication-Info' = ReqUG} = Req,
151 VplmnIdBin = list_to_binary(VplmnId),
152 NumEutran = req_num_of_vec(ReqEU),
153 NumUgran = req_num_of_vec(ReqUG),
154 lager:info("Num EUTRAN=~p, UTRAN=~p~n", [NumEutran, NumUgran]),
155 % construct GSUP request to HLR and transceive it
156 GsupTx = #{message_type => send_auth_info_req, imsi => list_to_binary(UserName)},
157 GsupRx = gen_server:call(gsup_client, {transceive_gsup, GsupTx, send_auth_info_res, send_auth_info_err}),
158 lager:info("GsupRx: ~p~n", [GsupRx]),
159 % construct DIAMETER AIA response
160 case GsupRx of
161 #{message_type:=send_auth_info_res, auth_tuples:=GsupAuthTuples} ->
162 AuthInfo = gsup_tuples2dia(GsupAuthTuples, VplmnIdBin, NumEutran, NumUgran, NumUgran),
163 Resp = #'AIA'{'Session-Id'=SessionId, 'Origin-Host'=OH, 'Origin-Realm'=OR,
164 'Result-Code'=2001, 'Auth-Session-State'=1,
165 'Authentication-Info'=AuthInfo};
166 #{message_type := send_auth_info_err} ->
167 Resp = #'AIA'{'Session-Id'=SessionId, 'Origin-Host'=OH, 'Origin-Realm'=OR,
168 'Result-Code'=?DIAMETER_ERROR_USER_UNKNOWN,
169 'Auth-Session-State'=1};
170 timeout ->
171 Resp = #'AIA'{'Session-Id'=SessionId, 'Origin-Host'=OH, 'Origin-Realm'=OR,
172 'Result-Code'=4181, 'Auth-Session-State'=1}
173 end,
174 lager:info("Resp: ~p~n", [Resp]),
175 {reply, Resp};
176
177handle_request(#diameter_packet{msg = Req, errors = []}, _SvcName, {_, Caps}) when is_record(Req, 'ULR') ->
178 % extract relevant fields from DIAMETER AIR
179 #diameter_caps{origin_host = {OH,_}, origin_realm = {OR,_}} = Caps,
180 #'ULR'{'Session-Id' = SessionId,
181 'RAT-Type' = RatType,
182 'ULR-Flags' = UlrFlags,
183 'User-Name' = UserName} = Req,
184
185 % construct GSUP UpdateLocation request to HLR and transceive it; expect InsertSubscrDataReq
186 GsupTxUlReq = #{message_type => location_upd_req, cn_domain => fixme},
187 GsupRxIsdReq = gen_server:call(gsup_client,
188 {transceive_gsup, GsupTxUlReq, insert_sub_data_req, location_upd_err}),
189 lager:info("GsupRxIsdReq: ~p~n", [GsupRxIsdReq]),
190 case GsupRxIsdReq of
191 #{message_type:=location_upd_err, cause:=Cause} ->
192 {Res, ExpRes} = gsup_cause2dia(Cause),
193 Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1,
194 'Origin-Host'=OH, 'Origin-Realm'=OR,
195 'Result-Code'=Res, 'Experimental-Result'=ExpRes};
196 #{message_type:=insert_sub_data_req} ->
197 % construct GSUP InsertSubscrData response to HLR and transceive it; expect
198 % UpdateLocationRes
199 GsupTxIsdRes = #{message_type => insert_sub_data_res},
200 GsupRxUlRes = gen_server:call(gsup_client,
201 {transceive_gsup, GsupTxIsdRes, location_upd_res, location_upd_err}),
202 lager:info("GsupRxUlRes: ~p~n", [GsupRxUlRes]),
203
204 case GsupRxUlRes of
205 #{message_type:=location_upd_res} ->
206 Msisdn = twomap_get(msisdn, GsupRxIsdReq, GsupRxUlRes, []),
207 Compl = twomap_get(pdp_info_complete, GsupRxIsdReq, GsupRxUlRes, []),
208 PdpInfoList = twomap_get(pdp_info_list, GsupRxIsdReq, GsupRxUlRes, []),
209 PdpContexts = gsup_pdp2dia(PdpInfoList),
210 GSubD = #'GPRS-Subscription-Data'{'Complete-Data-List-Included-Indicator'=[Compl],
211 'PDP-Context'=PdpContexts},
212 SubscrData = #'Subscription-Data'{'MSISDN'=Msisdn,'GPRS-Subscription-Data'=GSubD},
213 Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1,
214 'Origin-Host'=OH, 'Origin-Realm'=OR,
215 'Result-Code'=2001, 'Subscription-Data'=SubscrData};
216 #{message_type:=location_upd_err, cause:=Cause} ->
217 {Res, ExpRes} = gsup_cause2dia(Cause),
218 Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1,
219 'Origin-Host'=OH, 'Origin-Realm'=OR,
220 'Result-Code'=Res, 'Experimental-Result'=ExpRes};
221 _ ->
222 Resp = #'ULA'{'Session-Id'= SessionId, 'Auth-Session-State'=1,
223 'Origin-Host'=OH, 'Origin-Realm'=OR,
224 'Result-Code'=fixme}
225 end
226 end,
227 {reply, Resp};
228
229handle_request(Packet, _SvcName, {_,_}) ->
230 lager:error("Unsuppoerted message: ~p~n", [Packet]),
Harald Welte61276c42019-08-10 22:14:50 +0200231 discard.