Skip to content

Commit

Permalink
align coding convention & fix gosec issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen00991 committed Oct 18, 2024
1 parent 442d525 commit ceecf94
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 266 deletions.
4 changes: 2 additions & 2 deletions internal/context/amf.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ type N3IWFAMF struct {
RelativeAMFCapacity *ngapType.RelativeAMFCapacity
PLMNSupportList *ngapType.PLMNSupportList
AMFTNLAssociationList map[string]*AMFTNLAssociationItem // v4+v6 as key
/* Overload related */
// Overload related
AMFOverloadContent *AMFOverloadContent
/* Relative Context */
// Relative Context
N3iwfRanUeList map[int64]RanUe // ranUeNgapId as key
}

Expand Down
8 changes: 4 additions & 4 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ type n3iwf interface {
type N3IWFContext struct {
n3iwf

/* ID generator */
// ID generator
RANUENGAPIDGenerator *idgenerator.IDGenerator
TEIDGenerator *idgenerator.IDGenerator

/* Pools */
// Pools
AMFPool sync.Map // map[string]*N3IWFAMF, SCTPAddr as key
AMFReInitAvailableList sync.Map // map[string]bool, SCTPAddr as key
IKESA sync.Map // map[uint64]*IKESecurityAssociation, SPI as key
Expand All @@ -52,15 +52,15 @@ type N3IWFContext struct {
IKESPIToNGAPId sync.Map // map[uint64]RanUeNgapID, SPI as key
NGAPIdToIKESPI sync.Map // map[uint64]SPI, RanUeNgapID as key

/* Security data */
// Security data
CertificateAuthority []byte
N3IWFCertificate []byte
N3IWFPrivateKey *rsa.PrivateKey

IPSecInnerIPPool *ippool.IPPool
// TODO: [TWIF] TwifUe may has its own IP address pool

/* XFRM interface */
// XFRM interface
XfrmIfaces sync.Map // map[uint32]*netlink.Link, XfrmIfaceId as key
XfrmParentIfaceName string
// Every UE's first UP IPsec will use default XFRM interface, additoinal UP IPsec will offset its XFRM id
Expand Down
10 changes: 5 additions & 5 deletions internal/context/ikeue.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ const (
type N3IWFIkeUe struct {
N3iwfCtx *N3IWFContext

/* UE identity */
// UE identity
IPSecInnerIP net.IP
IPSecInnerIPAddr *net.IPAddr // Used to send UP packets to UE

/* IKE Security Association */
// IKE Security Association
N3IWFIKESecurityAssociation *IKESecurityAssociation
N3IWFChildSecurityAssociation map[uint32]*ChildSecurityAssociation // inbound SPI as key

/* Temporary Mapping of two SPIs */
// Temporary Mapping of two SPIs
// Exchange Message ID(including a SPI) and ChildSA(including a SPI)
// Mapping of Message ID of exchange in IKE and Child SA when creating new child SA
TemporaryExchangeMsgIDChildSAMapping map[uint32]*ChildSecurityAssociation // Message ID as a key

/* Security */
// Security
Kn3iwf []uint8 // 32 bytes (256 bits), value is from NGAP IE "Security Key"

/* NAS IKE Connection */
// NAS IKE Connection
IKEConnection *UDPSocketInfo

// Length of PDU Session List
Expand Down
8 changes: 4 additions & 4 deletions internal/context/n3iwf_ue.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import (
type N3IWFRanUe struct {
RanUeSharedCtx

/* Temporary cached NAS message */
// Temporary cached NAS message
// Used when NAS registration accept arrived before
// UE setup NAS TCP connection with N3IWF, and
// Forward pduSessionEstablishmentAccept to UE after
// UE send CREATE_CHILD_SA response
TemporaryCachedNASMessage []byte

/* NAS TCP Connection Established */
// NAS TCP Connection Established
IsNASTCPConnEstablished bool
IsNASTCPConnEstablishedComplete bool

/* NAS TCP Connection */
// NAS TCP Connection
TCPConnection net.Conn
}

Expand Down Expand Up @@ -74,7 +74,7 @@ func (n3iwfUe *N3IWFRanUe) DetachAMF() {
delete(n3iwfUe.AMF.N3iwfRanUeList, n3iwfUe.RanUeNgapId)
}

/* Implement RanUe interface */
// Implement RanUe interface
func (n3iwfUe *N3IWFRanUe) GetUserLocationInformation() *ngapType.UserLocationInformation {
userLocationInformation := new(ngapType.UserLocationInformation)

Expand Down
18 changes: 9 additions & 9 deletions internal/context/ranue.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ const (
)

type RanUe interface {
/* Get Attributes */
// Get Attributes
GetUserLocationInformation() *ngapType.UserLocationInformation
GetSharedCtx() *RanUeSharedCtx

/* User Plane Traffic */
// User Plane Traffic
// ForwardDL(gtpQoSMsg.QoSTPDUPacket)
// ForwardUL()

/* Others */
// Others
CreatePDUSession(int64, ngapType.SNSSAI) (*PDUSession, error)
DeletePDUSession(int64)
FindPDUSession(int64) *PDUSession
Remove() error
}

type RanUeSharedCtx struct {
/* UE identity */
// UE identity
RanUeNgapId int64
AmfUeNgapId int64
IPAddrv4 string
Expand All @@ -49,20 +49,20 @@ type RanUeSharedCtx struct {
MaskedIMEISV *ngapType.MaskedIMEISV // TS 38.413 9.3.1.54
Guti string

/* Relative Context */
// Relative Context
N3iwfCtx *N3IWFContext
AMF *N3IWFAMF

/* Security */
// Security
SecurityCapabilities *ngapType.UESecurityCapabilities // TS 38.413 9.3.1.86

/* PDU Session */
// PDU Session
PduSessionList map[int64]*PDUSession // pduSessionId as key

/* PDU Session Setup Temporary Data */
// PDU Session Setup Temporary Data
TemporaryPDUSessionSetupData *PDUSessionSetupTemporaryData

/* Others */
// Others
Guami *ngapType.GUAMI
IndexToRfsp int64
Ambr *ngapType.UEAggregateMaximumBitRate
Expand Down
31 changes: 0 additions & 31 deletions internal/context/safe_event_channel.go

This file was deleted.

43 changes: 0 additions & 43 deletions internal/context/safe_event_channel_test.go

This file was deleted.

54 changes: 10 additions & 44 deletions internal/ike/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,16 +601,12 @@ func (s *Server) HandleIKEAUTH(
ranNgapId = 0
}

err := s.SendNgapEvt(n3iwf_context.NewUnmarshalEAP5GDataEvt(
s.SendNgapEvt(n3iwf_context.NewUnmarshalEAP5GDataEvt(
ikeSecurityAssociation.LocalSPI,
eapExpanded.VendorData,
ikeSecurityAssociation.IkeUE != nil,
ranNgapId,
))
if err != nil {
ikeLog.Errorf("SendNgapEvt[Unmarshal EAP5G Data] failed: %+v", err)
return
}

ikeSecurityAssociation.IKEConnection = &n3iwf_context.UDPSocketInfo{
Conn: udpConn,
Expand Down Expand Up @@ -860,7 +856,7 @@ func (s *Server) HandleIKEAUTH(
ikeLog.Errorf("Applying XFRM rules failed: %v", err)
return
}
ikeLog.Debugf(childSecurityAssociationContext.String(cfg.GetXfrmIfaceId()))
ikeLog.Debugln(childSecurityAssociationContext.String(cfg.GetXfrmIfaceId()))

// Send IKE message to UE
err = SendIKEMessageToUE(udpConn, n3iwfAddr, ueAddr, responseIKEMessage,
Expand All @@ -880,20 +876,12 @@ func (s *Server) HandleIKEAUTH(
ikeSecurityAssociation.State++

// After this, N3IWF will forward NAS with Child SA (IPSec SA)
err = s.SendNgapEvt(n3iwf_context.NewStartTCPSignalNASMsgEvt(ranNgapId))
if err != nil {
ikeLog.Errorf("SendNgapEvt[Start TCP Signal NAS Msg] failed: %+v", err)
return
}
s.SendNgapEvt(n3iwf_context.NewStartTCPSignalNASMsgEvt(ranNgapId))

// Get TempPDUSessionSetupData from NGAP to setup PDU session if needed
err = s.SendNgapEvt(n3iwf_context.NewGetNGAPContextEvt(
s.SendNgapEvt(n3iwf_context.NewGetNGAPContextEvt(
ranNgapId, []int64{n3iwf_context.CxtTempPDUSessionSetupData},
))
if err != nil {
ikeLog.Errorf("SendNgapEvt[Get NGAP Context] failed: %+v", err)
return
}
}
}

Expand Down Expand Up @@ -977,11 +965,7 @@ func (s *Server) HandleCREATECHILDSA(

ngapCxtReqNumlist := []int64{n3iwf_context.CxtTempPDUSessionSetupData}

err := s.SendNgapEvt(n3iwf_context.NewGetNGAPContextEvt(ranNgapId, ngapCxtReqNumlist))
if err != nil {
ikeLog.Errorf("SendNgapEvt[Get NGAP Context] failed: %+v", err)
return
}
s.SendNgapEvt(n3iwf_context.NewGetNGAPContextEvt(ranNgapId, ngapCxtReqNumlist))
}

func (s *Server) continueCreateChildSA(
Expand Down Expand Up @@ -1119,11 +1103,7 @@ func (s *Server) continueCreateChildSA(
return
}
// Forward PDU Seesion Establishment Accept to UE
err = s.SendNgapEvt(n3iwf_context.NewSendNASMsgEvt(ranNgapId))
if err != nil {
ikeLog.Errorf("SendNgapEvt[Send NAS Msg] failed: %+v", err)
return
}
s.SendNgapEvt(n3iwf_context.NewSendNASMsgEvt(ranNgapId))

temporaryPDUSessionSetupData.FailedErrStr = append(temporaryPDUSessionSetupData.FailedErrStr, n3iwf_context.ErrNil)

Expand Down Expand Up @@ -1233,16 +1213,12 @@ func (s *Server) HandleUnmarshalEAP5GDataResponse(ikeEvt n3iwf_context.IkeEvt) {

n3iwfCtx.IkeSpiNgapIdMapping(ikeUe.N3IWFIKESecurityAssociation.LocalSPI, ranUeNgapId)

err := s.SendNgapEvt(n3iwf_context.NewSendInitialUEMessageEvt(
s.SendNgapEvt(n3iwf_context.NewSendInitialUEMessageEvt(
ranUeNgapId,
ikeSecurityAssociation.IKEConnection.UEAddr.IP.To4().String(),
ikeSecurityAssociation.IKEConnection.UEAddr.Port,
nasPDU,
))
if err != nil {
ikeLog.Errorf("SendNgapEvt[Send Init UE Message] failed: %+v", err)
return
}
}

func (s *Server) HandleSendEAP5GFailureMsg(ikeEvt n3iwf_context.IkeEvt) {
Expand Down Expand Up @@ -1645,10 +1621,7 @@ func (s *Server) CreatePDUSessionChildSA(
break
}
} else {
err := s.SendNgapEvt(n3iwf_context.NewSendPDUSessionResourceSetupResEvt(ranNgapId))
if err != nil {
ikeLog.Errorf("SendNgapEvt[Send PDU Sess Resource Setup Res] failed: %+v", err)
}
s.SendNgapEvt(n3iwf_context.NewSendPDUSessionResourceSetupResEvt(ranNgapId))
break
}
}
Expand Down Expand Up @@ -1697,13 +1670,9 @@ func (s *Server) StartDPD(ikeUe *n3iwf_context.N3IWFIkeUe) {
return
}

err := s.SendNgapEvt(n3iwf_context.NewSendUEContextReleaseRequestEvt(
s.SendNgapEvt(n3iwf_context.NewSendUEContextReleaseRequestEvt(
ranNgapId, n3iwf_context.ErrRadioConnWithUeLost,
))
if err != nil {
ikeLog.Errorf("SendNgapEvt[Send UE Ctx Release Request] failed: %+v", err)
return
}

ikeSA.DPDReqRetransTimer = nil
timer.Stop()
Expand Down Expand Up @@ -1838,10 +1807,7 @@ func (s *Server) handleDeletePayload(payload *ike_message.Delete, isResponse boo
return nil, errors.Errorf("Get Protocol ID %d in Informational delete payload, "+
"this payload will not be handled by IKE handler", payload.ProtocolID)
}
err = s.SendNgapEvt(evt)
if err != nil {
return nil, errors.Wrapf(err, "handleDeletePayload: SendNgapEvt failed")
}
s.SendNgapEvt(evt)
return responseIKEPayload, nil
}

Expand Down
6 changes: 4 additions & 2 deletions internal/ike/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func TestGenerateNATDetectHash(t *testing.T) {
},
}

for _, tt := range tests {
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
data, err := n3iwf.ikeServer.generateNATDetectHash(tt.initiatorSPI, tt.responderSPI, &tt.Addr)
require.NoError(t, err)
Expand Down Expand Up @@ -278,7 +279,8 @@ func TestHandleNATDetect(t *testing.T) {
},
}

for _, tt := range tests {
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
ueBehindNAT, n3iwfBehindNAT, err := n3iwf.ikeServer.handleNATDetect(
tt.initiatorSPI, tt.responderSPI,
Expand Down
Loading

0 comments on commit ceecf94

Please sign in to comment.