This repository has been archived by the owner on Apr 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
2151 lines (1935 loc) · 86.1 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gmm_handler
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"free5gc/lib/Nnrf_NFDiscovery"
"free5gc/lib/fsm"
"free5gc/lib/nas"
"free5gc/lib/nas/nasConvert"
"free5gc/lib/nas/nasMessage"
"free5gc/lib/ngap/ngapType"
"free5gc/lib/openapi/models"
"free5gc/src/amf/amf_consumer"
"free5gc/src/amf/amf_context"
"free5gc/src/amf/amf_ngap/ngap_message"
"free5gc/src/amf/amf_producer/amf_producer_callback"
"free5gc/src/amf/amf_util"
"free5gc/src/amf/gmm/gmm_event"
"free5gc/src/amf/gmm/gmm_message"
"free5gc/src/amf/gmm/gmm_state"
"free5gc/src/amf/logger"
"net/url"
"reflect"
"strconv"
"time"
"github.com/antihax/optional"
"github.com/mitchellh/mapstructure"
"github.com/mohae/deepcopy"
)
func HandleULNASTransport(ue *amf_context.AmfUe, anType models.AccessType, ulNasTransport *nasMessage.ULNASTransport) error {
switch ulNasTransport.GetPayloadContainerType() {
case nasMessage.PayloadContainerTypeN1SMInfo:
var pduSessionId int32
var requestType models.RequestType
var sNssai *models.Snssai
var dnn string
if nasPduSessionId := ulNasTransport.PduSessionID2Value; nasPduSessionId != nil {
pduSessionId = int32(nasPduSessionId.GetPduSessionID2Value())
} else {
return fmt.Errorf("NAS N1 SM info need PDU Session ID")
}
// TODO: implement SSC mode3 Op
if oldPduSessionId := ulNasTransport.OldPDUSessionID; oldPduSessionId != nil {
return fmt.Errorf("SSC mode3 operation has not been implemented yet")
}
if NasSnssai := ulNasTransport.SNSSAI; NasSnssai != nil {
snssai := nasConvert.SnssaiToModels(NasSnssai)
sNssai = &snssai
}
if NasDnn := ulNasTransport.DNN; NasDnn != nil {
dnn = string(NasDnn.GetDNN())
}
if ulNasTransport.RequestType != nil {
NasRequestType := ulNasTransport.RequestType.GetRequestTypeValue()
switch NasRequestType {
case nasMessage.ULNASTransportRequestTypeInitialRequest:
requestType = models.RequestType_INITIAL_REQUEST
case nasMessage.ULNASTransportRequestTypeExistingPduSession:
requestType = models.RequestType_EXISTING_PDU_SESSION
case nasMessage.ULNASTransportRequestTypeInitialEmergencyRequest:
requestType = models.RequestType_INITIAL_EMERGENCY_REQUEST
case nasMessage.ULNASTransportRequestTypeExistingEmergencyPduSession:
requestType = models.RequestType_EXISTING_EMERGENCY_PDU_SESSION
}
}
m := nas.NewMessage()
err := m.GsmMessageDecode(&ulNasTransport.PayloadContainer.Buffer)
if err != nil {
return err
}
switch m.GsmMessage.GetMessageType() {
case nas.MsgTypePDUSessionEstablishmentRequest:
return HandlePDUSessionEstablishmentRequest(ue, anType, ulNasTransport.PayloadContainer.Buffer, pduSessionId, requestType, sNssai, dnn)
case nas.MsgTypePDUSessionModificationRequest, nas.MsgTypePDUSessionModificationComplete, nas.MsgTypePDUSessionModificationCommandReject:
return HandlePDUSessionModificationForward(ue, anType, ulNasTransport.PayloadContainer.Buffer, pduSessionId)
case nas.MsgTypePDUSessionReleaseRequest, nas.MsgTypePDUSessionReleaseComplete, nas.MsgTypePDUSessionReleaseReject:
return HandlePDUSessionReleaseForward(ue, anType, ulNasTransport.PayloadContainer.Buffer, pduSessionId)
case nas.MsgTypePDUSessionAuthenticationComplete:
return HandlePDUSessionAuthenticationForward(ue, anType, ulNasTransport.PayloadContainer.GetPayloadContainerContents(), pduSessionId)
case nas.MsgTypeStatus5GSM:
return HandleStatus5GSM(ue, anType, ulNasTransport.PayloadContainer.Buffer, pduSessionId)
}
case nasMessage.PayloadContainerTypeSMS:
return fmt.Errorf("PayloadContainerTypeSMS has not been implemented yet in UL NAS TRANSPORT")
case nasMessage.PayloadContainerTypeLPP:
return fmt.Errorf("PayloadContainerTypeLPP has not been implemented yet in UL NAS TRANSPORT")
case nasMessage.PayloadContainerTypeSOR:
return fmt.Errorf("PayloadContainerTypeSOR has not been implemented yet in UL NAS TRANSPORT")
case nasMessage.PayloadContainerTypeUEPolicy:
logger.GmmLog.Infoln("AMF Transfer UEPolicy To PCF")
amf_producer_callback.SendN1MessageNotify(ue, models.N1MessageClass_UPDP, ulNasTransport.PayloadContainer.GetPayloadContainerContents(), nil)
case nasMessage.PayloadContainerTypeUEParameterUpdate:
logger.GmmLog.Infoln("AMF Transfer UEParameterUpdate To UDM")
upuMac, err := nasConvert.UpuAckToModels(ulNasTransport.PayloadContainer.GetPayloadContainerContents())
if err != nil {
return err
}
err = amf_consumer.PutUpuAck(ue, upuMac)
if err != nil {
return err
}
logger.GmmLog.Debugf("UpuMac[%s] in UPU ACK NAS Msg", upuMac)
case nasMessage.PayloadContainerTypeMultiplePayload:
return fmt.Errorf("PayloadContainerTypeMultiplePayload has not been implemented yet in UL NAS TRANSPORT")
}
return nil
}
func HandlePDUSessionEstablishmentRequest(ue *amf_context.AmfUe, anType models.AccessType, payload []byte, pduSessionID int32, requestType models.RequestType, sNssai *models.Snssai, dnn string) error {
// TODO Request Type Emergency requset
var pduSession models.PduSessionContext
pduSession.PduSessionId = pduSessionID
pduSession.AccessType = anType
amfSelf := amf_context.AMF_Self()
if requestType == models.RequestType_INITIAL_REQUEST {
if sNssai == nil {
if ue.SmfSelectionData != nil {
for snssai, sNssaiInfo := range ue.SmfSelectionData.SubscribedSnssaiInfos {
var err error
sNssai, err = amf_util.SnssaiHexToModels(snssai)
if err != nil {
return err
}
if dnn == "" {
for _, dnnInfo := range sNssaiInfo.DnnInfos {
if dnnInfo.DefaultDnnIndicator {
dnn = dnnInfo.Dnn
break
}
}
}
}
}
if sNssai == nil {
allowedNssai := ue.AllowedNssai[anType]
if len(allowedNssai) > 0 {
sNssai = allowedNssai[0].AllowedSnssai
} else {
err := fmt.Errorf("Ue[%s] doesn't have allowedNssai\n", ue.Supi)
logger.GmmLog.Errorf(err.Error())
return err
}
}
}
pduSession.SNssai = sNssai
if dnn == "" {
// default DNN decided by AMF
dnn = amfSelf.SupportDnnLists[0]
}
pduSession.Dnn = dnn
smfID, smfUri, err := selectSmf(ue, anType, &pduSession, payload)
if err != nil {
logger.GmmLog.Errorf("[AMF] SMF Selection for Snssai[%+v] Failed[%+v]", sNssai, err)
return err
}
// Store PduSessionContext For duplicated PDU Session Id
if smContext, ok := ue.SmContextList[pduSessionID]; ok {
ue.StoredSmContext[pduSessionID] = &amf_context.StoredSmContext{
SmfId: smfID,
SmfUri: smfUri,
PduSessionContext: &pduSession,
AnType: anType,
Payload: payload,
}
updateData := models.SmContextUpdateData{
Release: true,
Cause: models.Cause_REL_DUE_TO_DUPLICATE_SESSION_ID,
SmContextStatusUri: fmt.Sprintf("%s/namf-callback/v1/smContextStatus/%s/%d", amfSelf.GetIPv4Uri(), ue.Guti, pduSessionID),
}
response, _, _, err := amf_consumer.SendUpdateSmContextRequest(ue, smContext.SmfUri, smContext.PduSessionContext.SmContextRef, updateData, nil, nil)
if err != nil {
return err
} else if response == nil {
err := fmt.Errorf("PDU Session ID[%d] can't be released in DUPLICATE_SESSION_ID case for request Type \"Initial Request\" in PDU Session Establishment Request\n", pduSessionID)
logger.GmmLog.Errorf(err.Error())
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, payload, &pduSessionID, nasMessage.Cause5GMMPayloadWasNotForwarded, nil, 0)
}
return nil
}
smContextCreateData := amf_consumer.BuildCreateSmContextRequest(ue, pduSession, requestType)
response, smContextRef, errResponse, problemDetail, err := amf_consumer.SendCreateSmContextRequest(ue, smfUri, payload, smContextCreateData)
if response != nil {
var smContext amf_context.SmContext
pduSession.SmContextRef = smContextRef
logger.GmmLog.Infof("smconetxt ef : %s", smContextRef)
smContext.PduSessionContext = &pduSession
smContext.UserLocation = deepcopy.Copy(ue.Location).(models.UserLocation)
smContext.SmfUri = smfUri
smContext.SmfId = smfID
ue.SmContextList[pduSession.PduSessionId] = &smContext
logger.GmmLog.Infof("Http create smContext[pduSessionID: %d] Success", pduSession.PduSessionId)
// TODO: handle response(response N2SmInfo to RAN if exists)
} else if errResponse != nil {
logger.GmmLog.Warnf("PDU Session Establishment Request is rejected by SMF[pduSessionId:%d]\n", pduSession.PduSessionId)
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, errResponse.BinaryDataN1SmInfoToUe, &pduSessionID, 0, nil, 0)
} else if err != nil {
return err
} else {
// TODO: error handling
err1 := fmt.Errorf("Failed to Create smContext[pduSessionID: %d], Error[%v]\n", pduSessionID, problemDetail)
logger.GmmLog.Errorf(err1.Error())
return err1
}
} else if requestType == models.RequestType_EXISTING_PDU_SESSION {
smContext, ok := ue.SmContextList[pduSessionID]
if !ok {
err := fmt.Errorf("PDU Session ID[%d] dose not exist for request Type \"Existing Pdu Session\" in PDU Session Establishment Request\n", pduSessionID)
logger.GmmLog.Errorf(err.Error())
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, payload, &pduSessionID, nasMessage.Cause5GMMPayloadWasNotForwarded, nil, 0)
return err
}
// TODO: Compare Smfid PLMN with AMF
if smContext.SmfId == "" {
err := fmt.Errorf("PDU Session ID[%d] correspond SMF-ID does not exist\n", pduSessionID)
logger.GmmLog.Errorf(err.Error())
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, payload, &pduSessionID, nasMessage.Cause5GMMPayloadWasNotForwarded, nil, 0)
return err
}
targetAccessType := models.AccessType__3_GPP_ACCESS
if anType == models.AccessType__3_GPP_ACCESS {
targetAccessType = models.AccessType_NON_3_GPP_ACCESS
}
if !ue.InAllowedNssai(*smContext.PduSessionContext.SNssai, targetAccessType) {
err := fmt.Errorf("S-NSSAI[%v] is not present in the Allowed NSSAI of target Access\n", *smContext.PduSessionContext.SNssai)
logger.GmmLog.Errorf(err.Error())
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, payload, &pduSessionID, nasMessage.Cause5GMMPayloadWasNotForwarded, nil, 0)
return err
}
response, errResponse, problemDetail, err := amf_consumer.SendUpdateSmContextHandoverBetweenAccessType(ue, pduSessionID, targetAccessType, payload)
if response != nil {
smContext.PduSessionContext.AccessType = targetAccessType
logger.GmmLog.Infof("Success update smContext Handover Between AccessType[pduSessionID: %d]", pduSessionID)
// TODO: handle response(response N2SmInfo to RAN if exists)
} else if errResponse != nil {
logger.GmmLog.Warnf("PDU Session Establishment Request is rejected by SMF[pduSessionId:%d]\n", pduSession.PduSessionId)
if errResponse.BinaryDataN1SmMessage != nil {
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, errResponse.BinaryDataN1SmMessage, &pduSessionID, 0, nil, 0)
}
} else if err != nil {
return err
} else {
// TODO: error handling
logger.GmmLog.Errorf("Failed to Create smContext[pduSessionID: %d], Error[%v]\n", pduSessionID, problemDetail)
return nil
}
} else {
return fmt.Errorf("RequestType INITIAL_EMERGENCY_REQUEST and EXISTING_EMERGENCY_PDU_SESSION haven't been supported")
}
return nil
}
func selectSmf(ue *amf_context.AmfUe, anType models.AccessType, pduSession *models.PduSessionContext, payload []byte) (smfID string, smfUri string, err error) {
amfSelf := amf_context.AMF_Self()
nrfUri := amfSelf.NrfUri // default NRF URI is pre-configured by AMF
nsiInformation := ue.GetNsiInformationFromSnssai(anType, *pduSession.SNssai)
if nsiInformation == nil {
if ue.NssfUri == "" {
for {
err := amf_consumer.SearchNssfNSSelectionInstance(ue, amfSelf.NrfUri, models.NfType_NSSF, models.NfType_AMF, nil)
if err != nil {
logger.GmmLog.Errorf("AMF can not select an NSSF Instance by NRF[Error: %+v]", err)
time.Sleep(2 * time.Second)
} else {
break
}
}
}
res, problemDetails, err := amf_consumer.NSSelectionGetForPduSession(ue, *pduSession.SNssai)
if problemDetails != nil {
logger.GmmLog.Errorf("NSSelection Get Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.GmmLog.Errorf("NSSelection Get Error[%+v]", err)
} else {
nsiInformation = res.NsiInformation
}
}
pduSession.NsInstance = nsiInformation.NsiId
nrfApiUrl, err := url.Parse(nsiInformation.NrfId)
if err != nil {
logger.GmmLog.Errorf("Parse NRF URI error, use default NRF[%s]", nrfUri)
} else {
nrfUri = fmt.Sprintf("%s://%s", nrfApiUrl.Scheme, nrfApiUrl.Host)
}
param := Nnrf_NFDiscovery.SearchNFInstancesParamOpts{
ServiceNames: optional.NewInterface([]models.ServiceName{models.ServiceName_NSMF_PDUSESSION}),
Dnn: optional.NewString(pduSession.Dnn),
Snssais: optional.NewInterface(amf_util.MarshToJsonString([]models.Snssai{*pduSession.SNssai})),
}
if ue.PlmnId.Mcc != "" {
param.TargetPlmnList = optional.NewInterface(amf_util.MarshToJsonString(ue.PlmnId))
}
logger.GmmLog.Debugf("Search SMF from NRF[%s]", nrfUri)
result, err := amf_consumer.SendSearchNFInstances(nrfUri, models.NfType_SMF, models.NfType_AMF, ¶m)
if err != nil || result.NfInstances == nil {
err = fmt.Errorf("DNN[%s] is not support by network and AMF can not select an SMF by NRF\n", pduSession.Dnn)
logger.GmmLog.Errorf(err.Error())
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, payload, &pduSession.PduSessionId, nasMessage.Cause5GMMDNNNotSupportedOrNotSubscribedInTheSlice, nil, 0)
return
}
// select the first SMF, TODO: select base on other info
for _, nfProfile := range result.NfInstances {
smfUri = amf_util.SearchNFServiceUri(nfProfile, models.ServiceName_NSMF_PDUSESSION, models.NfServiceStatus_REGISTERED)
if smfUri != "" {
break
}
}
return
}
func HandlePDUSessionModificationForward(ue *amf_context.AmfUe, anType models.AccessType, payload []byte, pduSessionID int32) (err error) {
smContext, ok := ue.SmContextList[pduSessionID]
if !ok {
err := fmt.Errorf("PDU Session ID[%d] in PDU Session Modification Procedure is not exist\n", pduSessionID)
logger.GmmLog.Errorf(err.Error())
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, payload, &pduSessionID, nasMessage.Cause5GMMPayloadWasNotForwarded, nil, 0)
return err
}
smContextUpdateData := models.SmContextUpdateData{
N1SmMsg: &models.RefToBinaryData{
ContentId: "N1SmMsg",
},
}
smContextUpdateData.Pei = ue.Pei
smContextUpdateData.Gpsi = ue.Gpsi
if !amf_context.CompareUserLocation(ue.Location, smContext.UserLocation) {
smContextUpdateData.UeLocation = &ue.Location
}
response, errResponse, problemDetail, err := amf_consumer.SendUpdateSmContextRequest(ue, smContext.SmfUri, smContext.PduSessionContext.SmContextRef, smContextUpdateData, payload, nil)
if response != nil {
smContext.UserLocation = ue.Location
responseData := response.JsonData
n1Msg := response.BinaryDataN1SmMessage
n2Info := response.BinaryDataN2SmInformation
if n2Info != nil {
switch responseData.N2SmInfoType {
case models.N2SmInfoType_PDU_RES_MOD_REQ:
logger.GmmLog.Infoln("AMF Transfer NGAP PDU Resource Modify Req from SMF")
var nasPdu []byte
if n1Msg != nil {
pduSessionId := uint8(pduSessionID)
nasPdu, err = gmm_message.BuildDLNASTransport(ue, nasMessage.PayloadContainerTypeN1SMInfo, n1Msg, &pduSessionId, nil, nil, 0)
if err != nil {
return
}
}
list := ngapType.PDUSessionResourceModifyListModReq{}
ngap_message.AppendPDUSessionResourceModifyListModReq(&list, pduSessionID, nasPdu, n2Info)
ngap_message.SendPDUSessionResourceModifyRequest(ue.RanUe[anType], list)
}
} else if n1Msg != nil {
logger.GmmLog.Infoln("AMF tansfer Only N1 PDU Session Modification Msg to UE")
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, errResponse.BinaryDataN1SmMessage, &pduSessionID, 0, nil, 0)
}
} else if errResponse != nil {
errJSON := errResponse.JsonData
n1Msg := errResponse.BinaryDataN1SmMessage
logger.GmmLog.Warnf("PDU Session Modification Procedure is rejected by SMF[pduSessionId:%d], Error[%s]\n", pduSessionID, errJSON.Error.Cause)
if n1Msg != nil {
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, errResponse.BinaryDataN1SmMessage, &pduSessionID, 0, nil, 0)
}
// TODO: handle n2 info transfer
} else if err != nil {
return err
} else {
// TODO: error handling
err1 := fmt.Errorf("Failed to Update smContext[pduSessionID: %d], Error[%v]", pduSessionID, problemDetail)
logger.GmmLog.Errorf(err1.Error())
return err1
}
return nil
}
func HandlePDUSessionReleaseForward(ue *amf_context.AmfUe, anType models.AccessType, payload []byte, pduSessionID int32) (err error) {
smContext, ok := ue.SmContextList[pduSessionID]
if !ok {
err := fmt.Errorf("PDU Session ID[%d] in PDU Session Release Request is not exist\n", pduSessionID)
logger.GmmLog.Errorf(err.Error())
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, payload, &pduSessionID, nasMessage.Cause5GMMPayloadWasNotForwarded, nil, 0)
return err
}
smContextUpdateData := models.SmContextUpdateData{
N1SmMsg: &models.RefToBinaryData{
ContentId: "N1SmMsg",
},
}
smContextUpdateData.UeLocation = &ue.Location
response, errResponse, problemDetail, err := amf_consumer.SendUpdateSmContextRequest(ue, smContext.SmfUri, smContext.PduSessionContext.SmContextRef, smContextUpdateData, payload, nil)
if response != nil {
smContext.UserLocation = ue.Location
responseData := response.JsonData
n1Msg := response.BinaryDataN1SmMessage
n2Info := response.BinaryDataN2SmInformation
if n2Info != nil {
switch responseData.N2SmInfoType {
case models.N2SmInfoType_PDU_RES_REL_CMD:
logger.GmmLog.Infoln("AMF Transfer NGAP PDU Session Resource Rel Co from SMF")
var nasPdu []byte
if n1Msg != nil {
pduSessionId := uint8(pduSessionID)
nasPdu, err = gmm_message.BuildDLNASTransport(ue, nasMessage.PayloadContainerTypeN1SMInfo, n1Msg, &pduSessionId, nil, nil, 0)
if err != nil {
return
}
}
list := ngapType.PDUSessionResourceToReleaseListRelCmd{}
ngap_message.AppendPDUSessionResourceToReleaseListRelCmd(&list, pduSessionID, n2Info)
ngap_message.SendPDUSessionResourceReleaseCommand(ue.RanUe[anType], nasPdu, list)
}
} else if n1Msg != nil {
logger.GmmLog.Infoln("AMF tansfer Only N1 PDU Session Release Msg to UE")
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, n1Msg, &pduSessionID, 0, nil, 0)
}
} else if errResponse != nil {
errJSON := errResponse.JsonData
n1Msg := errResponse.BinaryDataN1SmMessage
logger.GmmLog.Warnf("PDU Session Release Procedure is rejected by SMF[pduSessionId:%d], Error[%s]\n", pduSessionID, errJSON.Error.Cause)
if n1Msg != nil {
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, errResponse.BinaryDataN1SmMessage, &pduSessionID, 0, nil, 0)
}
} else if err != nil {
return err
} else {
// TODO: error handling
err1 := fmt.Errorf("Failed to Update smContext[pduSessionID: %d], Error[%v]", pduSessionID, problemDetail)
logger.GmmLog.Errorf(err1.Error())
return err1
}
return nil
}
func HandlePDUSessionAuthenticationForward(ue *amf_context.AmfUe, anType models.AccessType, payload []byte, pduSessionID int32) error {
smContext, ok := ue.SmContextList[pduSessionID]
if !ok {
err := fmt.Errorf("PDU Session ID[%d] in PDU Session Authentication Complete is not exist\n", pduSessionID)
logger.GmmLog.Errorf(err.Error())
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, payload, &pduSessionID, nasMessage.Cause5GMMPayloadWasNotForwarded, nil, 0)
return err
}
smContextUpdateData := models.SmContextUpdateData{
N1SmMsg: &models.RefToBinaryData{
ContentId: "N1SmMsg",
},
}
response, errResponse, problemDetail, err := amf_consumer.SendUpdateSmContextRequest(ue, smContext.SmfUri, smContext.PduSessionContext.SmContextRef, smContextUpdateData, payload, nil)
if response != nil {
logger.GmmLog.Infoln("AMF tansfer PDU Session Authentication Complete to SMF Success")
} else if errResponse != nil {
errJSON := errResponse.JsonData
n1Msg := errResponse.BinaryDataN1SmMessage
logger.GmmLog.Warnf("PDU Session Authentication Complete is rejected by SMF[pduSessionId:%d], Error[%s]\n", pduSessionID, errJSON.Error.Cause)
if n1Msg != nil {
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, errResponse.BinaryDataN1SmMessage, &pduSessionID, 0, nil, 0)
}
} else if err != nil {
return err
} else {
// TODO: error handling
err1 := fmt.Errorf("Failed to Update smContext[pduSessionID: %d], Error[%v]", pduSessionID, problemDetail)
logger.GmmLog.Errorf(err1.Error())
return err1
}
return nil
}
func HandleRegistrationRequest(ue *amf_context.AmfUe, anType models.AccessType, procedureCode int64, registrationRequest *nasMessage.RegistrationRequest) error {
logger.GmmLog.Info("[AMF] Handle Registration Request")
amf_util.ClearT3513(ue)
amf_util.ClearT3565(ue)
amfSelf := amf_context.AMF_Self()
if ue == nil {
return fmt.Errorf("AmfUe is nil")
}
ranUe := ue.RanUe[ue.GetAnType()]
if ranUe == nil {
return fmt.Errorf("RanUe is nil")
}
ue.RegistrationRequest = registrationRequest
ue.RegistrationType5GS = registrationRequest.NgksiAndRegistrationType5GS.GetRegistrationType5GS()
switch ue.RegistrationType5GS {
case nasMessage.RegistrationType5GSInitialRegistration:
logger.GmmLog.Debugf("RegistrationType: Initial Registration")
case nasMessage.RegistrationType5GSMobilityRegistrationUpdating:
logger.GmmLog.Debugf("RegistrationType: Mobility Registration Updating")
case nasMessage.RegistrationType5GSPeriodicRegistrationUpdating:
logger.GmmLog.Debugf("RegistrationType: Periodic Registration Updating")
case nasMessage.RegistrationType5GSEmergencyRegistration:
return fmt.Errorf("Not Supportted RegistrationType: Emergency Registration")
case nasMessage.RegistrationType5GSReserved:
ue.RegistrationType5GS = nasMessage.RegistrationType5GSInitialRegistration
logger.GmmLog.Debugf("RegistrationType: Reserved")
default:
logger.GmmLog.Debugf("RegistrationType: %v, chage state to InitialRegistration", ue.RegistrationType5GS)
ue.RegistrationType5GS = nasMessage.RegistrationType5GSInitialRegistration
}
mobileIdentity5GSContents := registrationRequest.MobileIdentity5GS.GetMobileIdentity5GSContents()
ue.IdentityTypeUsedForRegistration = nasConvert.GetTypeOfIdentity(mobileIdentity5GSContents[0])
switch ue.IdentityTypeUsedForRegistration { // get type of identity
case nasMessage.MobileIdentity5GSTypeNoIdentity:
logger.GmmLog.Debugf("No Identity")
case nasMessage.MobileIdentity5GSTypeSuci:
var plmnId string
ue.Suci, plmnId = nasConvert.SuciToString(mobileIdentity5GSContents)
ue.PlmnId = amf_util.PlmnIdStringToModels(plmnId)
logger.GmmLog.Debugf("SUCI: %s", ue.Suci)
ue.IsCleartext = true
case nasMessage.MobileIdentity5GSType5gGuti:
guami, guti := nasConvert.GutiToString(mobileIdentity5GSContents)
ue.Guti = guti
logger.GmmLog.Debugf("GUTI: %s", guti)
servedGuami := amfSelf.ServedGuamiList[0]
if reflect.DeepEqual(guami, servedGuami) {
logger.GmmLog.Debugf("Serving AMF has changed")
ue.ServingAmfChanged = true
} else {
ue.ServingAmfChanged = false
}
ue.IsCleartext = true
case nasMessage.MobileIdentity5GSTypeImei:
imei := nasConvert.PeiToString(mobileIdentity5GSContents)
ue.Pei = imei
logger.GmmLog.Debugf("PEI: %s", imei)
ue.IsCleartext = false
case nasMessage.MobileIdentity5GSTypeImeisv:
imeisv := nasConvert.PeiToString(mobileIdentity5GSContents)
ue.Pei = imeisv
logger.GmmLog.Debugf("PEI: %s", imeisv)
ue.IsCleartext = false
}
// NgKsi: TS 24.501 9.11.3.32
switch registrationRequest.NgksiAndRegistrationType5GS.GetTSC() {
case nasMessage.TypeOfSecurityContextFlagNative:
ue.NgKsi.Tsc = models.ScType_NATIVE
case nasMessage.TypeOfSecurityContextFlagMapped:
ue.NgKsi.Tsc = models.ScType_MAPPED
}
ue.NgKsi.Ksi = int32(registrationRequest.NgksiAndRegistrationType5GS.GetNasKeySetIdentifiler())
if ue.NgKsi.Tsc == models.ScType_NATIVE && ue.NgKsi.Ksi != 7 {
} else {
ue.NgKsi.Tsc = models.ScType_NATIVE
ue.NgKsi.Ksi = 0
}
// Copy UserLocation from ranUe
ue.Location = ranUe.Location
ue.Tai = ranUe.Tai
// Check TAI
if !amf_context.InTaiList(ue.Tai, amfSelf.SupportTaiLists) {
gmm_message.SendRegistrationReject(ue.RanUe[anType], nasMessage.Cause5GMMTrackingAreaNotAllowed, "")
return fmt.Errorf("Registration Reject[Tracking area not allowed]")
}
if ue.RegistrationType5GS != nasMessage.RegistrationType5GSPeriodicRegistrationUpdating {
if registrationRequest.Capability5GMM != nil {
ue.Capability5GMM = *registrationRequest.Capability5GMM
} else {
if procedureCode == ngapType.ProcedureCodeInitialUEMessage {
logger.GmmLog.Infoln("ue.Capability5GMM is nil in InitialUEMEssage")
} else if procedureCode == ngapType.ProcedureCodeUplinkNASTransport {
gmm_message.SendRegistrationReject(ue.RanUe[anType], nasMessage.Cause5GMMProtocolErrorUnspecified, "")
return fmt.Errorf("Capability5GMM is nil")
} else {
return fmt.Errorf("wrong procedurecode")
}
}
if registrationRequest.UESecurityCapability != nil {
ue.NasUESecurityCapability = *registrationRequest.UESecurityCapability
ue.SecurityCapabilities.NREncryptionAlgorithms, ue.SecurityCapabilities.NRIntegrityProtectionAlgorithms,
ue.SecurityCapabilities.EUTRAEncryptionAlgorithms, ue.SecurityCapabilities.EUTRAIntegrityProtectionAlgorithms =
nasConvert.UESecurityCapabilityToByteArray(registrationRequest.UESecurityCapability.Buffer)
} else {
gmm_message.SendRegistrationReject(ue.RanUe[anType], nasMessage.Cause5GMMProtocolErrorUnspecified, "")
return fmt.Errorf("UESecurityCapability is nil")
}
}
// TS 23.502 4.2.2.2.2 step 1
// If available, the last visited TAI shall be included in order to help the AMF produce Registration Area for the UE
if registrationRequest.LastVisitedRegisteredTAI != nil {
plmnID := nasConvert.PlmnIDToString(registrationRequest.LastVisitedRegisteredTAI.Octet[1:4])
nasTac := registrationRequest.LastVisitedRegisteredTAI.GetTAC()
tac := hex.EncodeToString(nasTac[:])
lastVisitedRegisteredTai := models.Tai{
PlmnId: &models.PlmnId{
Mcc: plmnID[:3],
Mnc: plmnID[3:],
},
Tac: tac,
}
ue.LastVisitedRegisteredTai = lastVisitedRegisteredTai
logger.GmmLog.Debugf("Ue Last Visited Registered Tai; %v", ue.LastVisitedRegisteredTai)
}
if registrationRequest.MICOIndication != nil {
logger.GmmLog.Warnf("Receive MICO Indication[RAAI: %d], Not Supported", registrationRequest.MICOIndication.GetRAAI())
}
// TODO: Negotiate DRX value if need (TS 23.501 5.4.5)
if registrationRequest.RequestedDRXParameters != nil {
switch registrationRequest.RequestedDRXParameters.GetDRXValue() {
case nasMessage.DRXcycleParameterT32:
logger.GmmLog.Tracef("Requested DRX: T = 32")
ue.UESpecificDRX = nasMessage.DRXcycleParameterT32
case nasMessage.DRXcycleParameterT64:
logger.GmmLog.Tracef("Requested DRX: T = 64")
ue.UESpecificDRX = nasMessage.DRXcycleParameterT64
case nasMessage.DRXcycleParameterT128:
logger.GmmLog.Tracef("Requested DRX: T = 128")
ue.UESpecificDRX = nasMessage.DRXcycleParameterT128
case nasMessage.DRXcycleParameterT256:
logger.GmmLog.Tracef("Requested DRX: T = 256")
ue.UESpecificDRX = nasMessage.DRXcycleParameterT256
case nasMessage.DRXValueNotSpecified:
fallthrough
default:
ue.UESpecificDRX = nasMessage.DRXValueNotSpecified
logger.GmmLog.Tracef("Requested DRX: Value not specified")
}
}
// TODO (TS 23.502 4.2.2.2 step 4): if UE's 5g-GUTI is included & serving AMF has changed since last registration procedure,
// new AMF may invoke Namf_Communication_UEContextTransfer to old AMF, including the complete registration request nas
// msg, to request UE's SUPI & UE Context
if ue.ServingAmfChanged {
}
return nil
}
func HandleStatus5GSM(ue *amf_context.AmfUe, anType models.AccessType, payload []byte, pduSessionID int32) (err error) {
logger.GmmLog.Info("[AMF] Handle Status5GSM")
smContext, ok := ue.SmContextList[pduSessionID]
if !ok {
err := fmt.Errorf("PDU Session ID[%d] in Status 5GSM Procedure is not exist\n", pduSessionID)
logger.GmmLog.Errorf(err.Error())
gmm_message.SendDLNASTransport(ue.RanUe[anType], nasMessage.PayloadContainerTypeN1SMInfo, payload, &pduSessionID, nasMessage.Cause5GMMPayloadWasNotForwarded, nil, 0)
return err
}
smContextUpdateData := models.SmContextUpdateData{
N1SmMsg: &models.RefToBinaryData{
ContentId: "N1SmMsg",
},
}
_, _, _, err = amf_consumer.SendUpdateSmContextRequest(ue, smContext.SmfUri, smContext.PduSessionContext.SmContextRef, smContextUpdateData, payload, nil)
if err != nil {
return err
}
return nil
}
func HandleInitialRegistration(ue *amf_context.AmfUe, anType models.AccessType) error {
amfSelf := amf_context.AMF_Self()
// Common Registration procedure
if ue == nil {
return fmt.Errorf("AmfUe is nil")
}
if len(ue.Suci) == 0 {
gmm_message.SendIdentityRequest(ue.RanUe[anType], nasMessage.MobileIdentity5GSTypeSuci)
return nil
}
if !ue.SecurityContextIsValid() {
return startAuthenticationProcedure(ue, anType)
}
// update Kgnb/Kn3iwf
ue.UpdateSecurityContext(anType)
// Registration with AMF re-allocation (TS 23.502 4.2.2.2.3)
if len(ue.SubscribedNssai) == 0 {
getSubscribedNssai(ue)
}
if err := handleRequestedNssai(ue, anType); err != nil {
return err
}
// TODO (step 10 optional): send Namf_Communication_RegistrationCompleteNotify to old AMF if need
if ue.ServingAmfChanged {
// If the AMF has changed the new AMF notifies the old AMF that the registration of the UE in the new AMF is completed
}
if len(ue.Pei) == 0 {
gmm_message.SendIdentityRequest(ue.RanUe[anType], nasMessage.MobileIdentity5GSTypeImei)
return nil
}
// TODO (step 12 optional): the new AMF initiates ME identity check by invoking the N5g-eir_EquipmentIdentityCheck_Get service operation
if ue.ServingAmfChanged || ue.Sm[models.AccessType_NON_3_GPP_ACCESS].Check(gmm_state.REGISTERED) || !ue.ContextValid {
// UDM selection described in TS 23.501 6.3.8
// TODO: consider udm group id, Routing ID part of SUCI, GPSI or External Group ID (e.g., by the NEF)
param := Nnrf_NFDiscovery.SearchNFInstancesParamOpts{
Supi: optional.NewString(ue.Supi),
}
resp, err := amf_consumer.SendSearchNFInstances(amfSelf.NrfUri, models.NfType_UDM, models.NfType_AMF, ¶m)
if err != nil {
logger.GmmLog.Error("AMF can not select an UDM by NRF")
return err
}
// select the first UDM, TODO: select base on other info
var uecmUri, sdmUri string
for _, nfProfile := range resp.NfInstances {
ue.UdmId = nfProfile.NfInstanceId
uecmUri = amf_util.SearchNFServiceUri(nfProfile, models.ServiceName_NUDM_UECM, models.NfServiceStatus_REGISTERED)
sdmUri = amf_util.SearchNFServiceUri(nfProfile, models.ServiceName_NUDM_SDM, models.NfServiceStatus_REGISTERED)
if uecmUri != "" && sdmUri != "" {
break
}
}
ue.NudmUECMUri = uecmUri
ue.NudmSDMUri = sdmUri
if ue.NudmUECMUri == "" || ue.NudmSDMUri == "" {
err := fmt.Errorf("AMF can not select an UDM by NRF")
logger.GmmLog.Errorf(err.Error())
return err
}
problemDetails, err := amf_consumer.UeCmRegistration(ue, anType, true)
if problemDetails != nil {
logger.GmmLog.Errorf("UECM_Registration Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.GmmLog.Errorf("UECM_Registration Error[%+v]", err)
}
problemDetails, err = amf_consumer.SDMGetAmData(ue)
if problemDetails != nil {
logger.GmmLog.Errorf("SDM_Get AmData Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.GmmLog.Errorf("SDM_Get AmData Error[%+v]", err)
}
problemDetails, err = amf_consumer.SDMGetSmfSelectData(ue)
if problemDetails != nil {
logger.GmmLog.Errorf("SDM_Get SmfSelectData Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.GmmLog.Errorf("SDM_Get SmfSelectData Error[%+v]", err)
}
problemDetails, err = amf_consumer.SDMGetUeContextInSmfData(ue)
if problemDetails != nil {
logger.GmmLog.Errorf("SDM_Get UeContextInSmfData Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.GmmLog.Errorf("SDM_Get UeContextInSmfData Error[%+v]", err)
}
problemDetails, err = amf_consumer.SDMSubscribe(ue)
if problemDetails != nil {
logger.GmmLog.Errorf("SDM Subscribe Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.GmmLog.Errorf("SDM Subscribe Error[%+v]", err)
}
ue.ContextValid = true
}
param := Nnrf_NFDiscovery.SearchNFInstancesParamOpts{
Supi: optional.NewString(ue.Supi),
}
for {
resp, err := amf_consumer.SendSearchNFInstances(amfSelf.NrfUri, models.NfType_PCF, models.NfType_AMF, ¶m)
if err != nil {
logger.GmmLog.Error("AMF can not select an PCF by NRF")
} else {
// select the first PCF, TODO: select base on other info
var pcfUri string
for _, nfProfile := range resp.NfInstances {
pcfUri = amf_util.SearchNFServiceUri(nfProfile, models.ServiceName_NPCF_AM_POLICY_CONTROL, models.NfServiceStatus_REGISTERED)
if pcfUri != "" {
ue.PcfId = nfProfile.NfInstanceId
break
}
}
if ue.PcfUri = pcfUri; ue.PcfUri == "" {
logger.GmmLog.Error("AMF can not select an PCF by NRF")
} else {
break
}
}
time.Sleep(500 * time.Millisecond) // sleep a while when search NF Instance fail
}
problemDetails, err := amf_consumer.AMPolicyControlCreate(ue, anType)
if problemDetails != nil {
logger.GmmLog.Errorf("AM Policy Control Create Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.GmmLog.Errorf("AM Policy Control Create Error[%+v]", err)
}
// Service Area Restriction are applicable only to 3GPP access
if anType == models.AccessType__3_GPP_ACCESS {
if ue.AmPolicyAssociation != nil && ue.AmPolicyAssociation.ServAreaRes != nil {
servAreaRes := ue.AmPolicyAssociation.ServAreaRes
if servAreaRes.RestrictionType == models.RestrictionType_ALLOWED_AREAS {
numOfallowedTAs := 0
for _, area := range servAreaRes.Areas {
numOfallowedTAs += len(area.Tacs)
}
if numOfallowedTAs < int(servAreaRes.MaxNumOfTAs) {
// TODO: based on AMF Policy, assign additional allowed area for UE,
// and the upper limit is servAreaRes.MaxNumOfTAs (TS 29.507 4.2.2.3)
}
}
}
}
// TODO (step 18 optional): If the AMF has changed and the old AMF has indicated an existing NGAP UE association towards a N3IWF, the new AMF
// creates an NGAP UE association towards the N3IWF to which the UE is connectedsend N2 AMF mobility request to N3IWF
if anType == models.AccessType_NON_3_GPP_ACCESS && ue.ServingAmfChanged {
// TODO: send N2 AMF Mobility Request
}
amfSelf.AllocateRegistrationArea(ue, anType)
logger.GmmLog.Debugf("Allocate GUTI[%s]", ue.Guti)
assignLadnInfo(ue)
amfSelf.AddAmfUeToUePool(ue, ue.Supi)
ue.T3502Value = amfSelf.T3502Value
if anType == models.AccessType__3_GPP_ACCESS {
ue.T3512Value = amfSelf.T3512Value
} else {
ue.Non3gppDeregistrationTimerValue = amfSelf.Non3gppDeregistrationTimerValue
}
if anType == models.AccessType__3_GPP_ACCESS {
gmm_message.SendRegistrationAccept(ue, anType, nil, nil, nil, nil, nil)
} else {
// TS 23.502 4.12.2.2 10a ~ 13: if non-3gpp, AMF should send initial context setup request to N3IWF first,
// and send registration accept after receiving initial context setup response
ngap_message.SendInitialContextSetupRequest(ue, anType, nil, nil, nil, nil, nil, nil)
registrationAccept, err := gmm_message.BuildRegistrationAccept(ue, anType, nil, nil, nil, nil)
if err != nil {
logger.GmmLog.Error("Build Registration Accept: %+v", err)
return nil
}
ue.RegistrationAcceptForNon3GPPAccess = registrationAccept
}
return ue.Sm[anType].Transfer(gmm_state.INITIAL_CONTEXT_SETUP, nil)
}
func HandleMobilityAndPeriodicRegistrationUpdating(ue *amf_context.AmfUe, anType models.AccessType, procedureCode int64) error {
logger.GmmLog.Infoln("[AMF] Handle MobilityAndPeriodicRegistrationUpdating")
amfSelf := amf_context.AMF_Self()
initialContextSetup := false
if procedureCode == ngapType.ProcedureCodeInitialUEMessage {
initialContextSetup = true
}
// Common Registration procedure
if ue == nil {
return fmt.Errorf("AmfUe is nil")
}
if len(ue.Suci) == 0 {
gmm_message.SendIdentityRequest(ue.RanUe[anType], nasMessage.MobileIdentity5GSTypeSuci)
return nil
}
if !ue.SecurityContextIsValid() {
return startAuthenticationProcedure(ue, anType)
}
if ue.RegistrationRequest.UpdateType5GS != nil {
if ue.RegistrationRequest.UpdateType5GS.GetNGRanRcu() == nasMessage.NGRanRadioCapabilityUpdateNeeded {
ue.UeRadioCapability = ""
ue.UeRadioCapabilityForPaging = nil
}
}
// Registration with AMF re-allocation (TS 23.502 4.2.2.2.3)
if len(ue.SubscribedNssai) == 0 {
getSubscribedNssai(ue)
}
if err := handleRequestedNssai(ue, anType); err != nil {
return err
}
// TODO (step 10 optional): send Namf_Communication_RegistrationCompleteNotify to old AMF if need
if ue.ServingAmfChanged {
// If the AMF has changed the new AMF notifies the old AMF that the registration of the UE in the new AMF is completed
}
if len(ue.Pei) == 0 {
gmm_message.SendIdentityRequest(ue.RanUe[anType], nasMessage.MobileIdentity5GSTypeImei)
return nil
}
// TODO (step 12 optional): the new AMF initiates ME identity check by invoking the N5g-eir_EquipmentIdentityCheck_Get service operation
if ue.ServingAmfChanged || ue.Sm[models.AccessType_NON_3_GPP_ACCESS].Check(gmm_state.REGISTERED) || !ue.ContextValid {
// UDM selection described in TS 23.501 6.3.8
// TODO: consider udm group id, Routing ID part of SUCI, GPSI or External Group ID (e.g., by the NEF)
param := Nnrf_NFDiscovery.SearchNFInstancesParamOpts{
Supi: optional.NewString(ue.Supi),
}
resp, err := amf_consumer.SendSearchNFInstances(amfSelf.NrfUri, models.NfType_UDM, models.NfType_AMF, ¶m)
if err != nil {
logger.GmmLog.Error("AMF can not select an UDM by NRF")
return err
}
var uecmUri, sdmUri string
for _, nfProfile := range resp.NfInstances {
ue.UdmId = nfProfile.NfInstanceId
uecmUri = amf_util.SearchNFServiceUri(nfProfile, models.ServiceName_NUDM_UECM, models.NfServiceStatus_REGISTERED)
sdmUri = amf_util.SearchNFServiceUri(nfProfile, models.ServiceName_NUDM_SDM, models.NfServiceStatus_REGISTERED)
if uecmUri != "" && sdmUri != "" {
break
}
}
ue.NudmUECMUri = uecmUri
ue.NudmSDMUri = sdmUri
problemDetails, err := amf_consumer.UeCmRegistration(ue, anType, true)
if problemDetails != nil {
logger.GmmLog.Errorf("UECM_Registration Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.GmmLog.Errorf("UECM_Registration Error[%+v]", err)
}
problemDetails, err = amf_consumer.SDMGetAmData(ue)
if problemDetails != nil {
logger.GmmLog.Errorf("SDM_Get AmData Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.GmmLog.Errorf("SDM_Get AmData Error[%+v]", err)
}
problemDetails, err = amf_consumer.SDMGetSmfSelectData(ue)
if problemDetails != nil {
logger.GmmLog.Errorf("SDM_Get SmfSelectData Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.GmmLog.Errorf("SDM_Get SmfSelectData Error[%+v]", err)
}
problemDetails, err = amf_consumer.SDMGetUeContextInSmfData(ue)
if problemDetails != nil {
logger.GmmLog.Errorf("SDM_Get UeContextInSmfData Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.GmmLog.Errorf("SDM_Get UeContextInSmfData Error[%+v]", err)
}
problemDetails, err = amf_consumer.SDMSubscribe(ue)
if problemDetails != nil {
logger.GmmLog.Errorf("SDM Subscribe Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.GmmLog.Errorf("SDM Subscribe Error[%+v]", err)
}
ue.ContextValid = true
}
var reactivationResult *[16]bool
var errPduSessionId, errCause []uint8
ctxList := ngapType.PDUSessionResourceSetupListCxtReq{}