@@ -40,6 +40,12 @@ func (s *Server) HandleIKESAINIT(
4040 ikeLog := logger .IKELog
4141 ikeLog .Infoln ("Handle IKE_SA_INIT" )
4242
43+ // Validate before allocating an IKE SA so an invalid request cannot leak state.
44+ if message .MessageID != 0 {
45+ ikeLog .Warnf ("Invalid IKE_SA_INIT MessageID: got %d, expected 0" , message .MessageID )
46+ return
47+ }
48+
4349 // Used to receive value from peer
4450 var securityAssociation * ike_message.SecurityAssociation
4551 var keyExcahge * ike_message.KeyExchange
@@ -152,8 +158,25 @@ func (s *Server) HandleIKESAINIT(
152158
153159 // Create new IKE security association
154160 ikeSecurityAssociation := n3iwfCtx .NewIKESecurityAssociation ()
161+ if ikeSecurityAssociation == nil {
162+ ikeLog .Error ("Failed to create IKE security association" )
163+ return
164+ }
165+ keepIKESA := false
166+ defer func () {
167+ if ! keepIKESA {
168+ n3iwfCtx .DeleteIKESecurityAssociation (ikeSecurityAssociation .LocalSPI )
169+ }
170+ }()
171+
155172 ikeSecurityAssociation .RemoteSPI = message .InitiatorSPI
156- ikeSecurityAssociation .InitiatorMessageID = message .MessageID
173+
174+ // The next request after IKE_SA_INIT must use Message ID 1.
175+ ikeSecurityAssociation .InitiatorMessageID = 1
176+
177+ // The first N3IWF-initiated request uses Message ID 0.
178+
179+ ikeSecurityAssociation .ResponderMessageID = 0
157180
158181 ikeSecurityAssociation .IKESAKey , localPublicValue , err = ike_security .NewIKESAKey (chooseProposal [0 ],
159182 keyExcahge .KeyExchangeData , concatenatedNonce ,
@@ -221,7 +244,9 @@ func (s *Server) HandleIKESAINIT(
221244 err = SendIKEMessageToUE (udpConn , n3iwfAddr , ueAddr , responseIKEMessage , nil )
222245 if err != nil {
223246 ikeLog .Errorf ("HandleIKESAINIT(): %v" , err )
247+ return
224248 }
249+ keepIKESA = true
225250}
226251
227252const (
@@ -248,6 +273,8 @@ func (s *Server) HandleIKEAUTH(
248273 cfg := s .Config ()
249274 ipsecGwAddr := cfg .GetIPSecGatewayAddr ()
250275
276+ ikeSecurityAssociation .InitiatorMessageID = message .MessageID + 1
277+
251278 // Used for response
252279 var responseIKEPayload ike_message.IKEPayloadContainer
253280
@@ -290,8 +317,6 @@ func (s *Server) HandleIKEAUTH(
290317 }
291318 }
292319
293- ikeSecurityAssociation .InitiatorMessageID = message .MessageID
294-
295320 switch ikeSecurityAssociation .State {
296321 case PreSignalling :
297322 if initiatorID != nil {
@@ -629,8 +654,6 @@ func (s *Server) HandleIKEAUTH(
629654 N3IWFAddr : n3iwfAddr ,
630655 UEAddr : ueAddr ,
631656 }
632-
633- ikeSecurityAssociation .InitiatorMessageID = message .MessageID
634657 } else {
635658 ikeLog .Error ("EAP is nil" )
636659 }
@@ -1183,11 +1206,13 @@ func (s *Server) HandleInformational(
11831206 }
11841207
11851208 if message .IsResponse () {
1209+ // Receive response from UE
11861210 ikeSecurityAssociation .ResponderMessageID ++
11871211 } else { // Get Request message
11881212 SendUEInformationExchange (ikeSecurityAssociation , ikeSecurityAssociation .IKESAKey ,
11891213 responseIKEPayload , false , true , message .MessageID ,
11901214 udpConn , ueAddr , n3iwfAddr )
1215+ ikeSecurityAssociation .InitiatorMessageID ++
11911216 }
11921217}
11931218
@@ -1275,7 +1300,7 @@ func (s *Server) HandleSendEAP5GFailureMsg(ikeEvt n3iwf_context.IkeEvt) {
12751300
12761301 // Build IKE message
12771302 responseIKEMessage := ike_message .NewMessage (ikeSecurityAssociation .RemoteSPI , ikeSecurityAssociation .LocalSPI ,
1278- ike_message .IKE_AUTH , true , false , ikeSecurityAssociation .InitiatorMessageID , responseIKEPayload )
1303+ ike_message .IKE_AUTH , true , false , ikeSecurityAssociation .InitiatorMessageID - 1 , responseIKEPayload )
12791304
12801305 // Send IKE message to UE
12811306 err = SendIKEMessageToUE (ikeSecurityAssociation .IKEConnection .Conn ,
@@ -1327,7 +1352,7 @@ func (s *Server) HandleSendEAPSuccessMsg(ikeEvt n3iwf_context.IkeEvt) {
13271352 // Build IKE message
13281353 responseIKEMessage := ike_message .NewMessage (ikeSecurityAssociation .RemoteSPI ,
13291354 ikeSecurityAssociation .LocalSPI , ike_message .IKE_AUTH , true , false ,
1330- ikeSecurityAssociation .InitiatorMessageID , responseIKEPayload )
1355+ ikeSecurityAssociation .InitiatorMessageID - 1 , responseIKEPayload )
13311356
13321357 // Send IKE message to UE
13331358 err = SendIKEMessageToUE (ikeSecurityAssociation .IKEConnection .Conn ,
@@ -1379,7 +1404,7 @@ func (s *Server) HandleSendEAPNASMsg(ikeEvt n3iwf_context.IkeEvt) {
13791404 // Build IKE message
13801405 responseIKEMessage := ike_message .NewMessage (ikeSecurityAssociation .RemoteSPI ,
13811406 ikeSecurityAssociation .LocalSPI , ike_message .IKE_AUTH , true , false ,
1382- ikeSecurityAssociation .InitiatorMessageID , responseIKEPayload )
1407+ ikeSecurityAssociation .InitiatorMessageID - 1 , responseIKEPayload )
13831408
13841409 // Send IKE message to UE
13851410 err = SendIKEMessageToUE (ikeSecurityAssociation .IKEConnection .Conn ,
0 commit comments