• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.internal.net.ipsec.ike.ike3gpp;
17 
18 import android.net.ipsec.ike.exceptions.InvalidSyntaxException;
19 
20 import com.android.internal.net.ipsec.ike.message.IkeNotifyPayload;
21 
22 import java.nio.ByteBuffer;
23 
24 /**
25  * Ike3gppN1ModeUtils contains functions needed to support 3GPP-specific N1 Mode functionality.
26  *
27  * <p>This class is package-private.
28  */
29 class Ike3gppN1ModeUtils {
30     private static final int N1_MODE_CAPABILITY_PAYLOAD_LENGTH = 2;
31     private static final byte PDU_SESSION_ID_LEN = (byte) 1;
32 
33     /**
34      * Generate N1 Mode Capability Notify payload.
35      *
36      * <p>This method formats the given PDU Session ID to its payload format.
37      *
38      * @see TS 124 302 Section 8.2.9.15 for specification of N1_MODE_CAPABILITY Notify payload.
39      * @param pduSessionId the PDU Session ID to be included in this N1 Mode Capability payload
40      * @return the formatted N1_MODE_CAPABILITY Notify payload data as a byte array.
41      */
generateN1ModeCapabilityPayload(byte pduSessionId)42     static IkeNotifyPayload generateN1ModeCapabilityPayload(byte pduSessionId) {
43         ByteBuffer payloadData = ByteBuffer.allocate(N1_MODE_CAPABILITY_PAYLOAD_LENGTH);
44         payloadData.put(PDU_SESSION_ID_LEN);
45         payloadData.put(pduSessionId);
46 
47         return new IkeNotifyPayload(
48                 Ike3gppExtensionExchange.NOTIFY_TYPE_N1_MODE_CAPABILITY, payloadData.array());
49     }
50 
51     /**
52      * Get the S-NSSAI value from the specified Notify Data.
53      *
54      * @see TS 124 302 Section 8.2.9.16 for specification of N1_MODE_INFORMATION Notify payload.
55      * @param notifyData The Notify-Data payload from which the S-NSSAI value will be parsed.
56      * @return the parsed S-NSSAI value
57      */
getSnssaiFromNotifyData(byte[] notifyData)58     static byte[] getSnssaiFromNotifyData(byte[] notifyData) throws InvalidSyntaxException {
59         ByteBuffer buffer = ByteBuffer.wrap(notifyData);
60 
61         // Format is: | SNSSAI Length (1B) | SNSSAI (Length B) |
62         int snssaiLen = Byte.toUnsignedInt(buffer.get());
63         if (snssaiLen != notifyData.length - 1) {
64             throw new InvalidSyntaxException("SNSSAI does not match expected length");
65         }
66 
67         byte[] snssai = new byte[snssaiLen];
68         buffer.get(snssai);
69         return snssai;
70     }
71 }
72