forked from free5gc/nas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nas.go
136 lines (114 loc) · 3.08 KB
/
nas.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
package nas
import (
"bytes"
"errors"
"fmt"
"github.com/free5gc/nas/nasMessage"
)
// Message TODO:description
type Message struct {
SecurityHeader
*GmmMessage
*GsmMessage
}
// SecurityHeader TODO:description
type SecurityHeader struct {
ProtocolDiscriminator uint8
SecurityHeaderType uint8
MessageAuthenticationCode uint32
SequenceNumber uint8
}
const (
SecurityHeaderTypePlainNas uint8 = 0x00
SecurityHeaderTypeIntegrityProtected uint8 = 0x01
SecurityHeaderTypeIntegrityProtectedAndCiphered uint8 = 0x02
SecurityHeaderTypeIntegrityProtectedWithNew5gNasSecurityContext uint8 = 0x03
SecurityHeaderTypeIntegrityProtectedAndCipheredWithNew5gNasSecurityContext uint8 = 0x04
)
// NewMessage TODO:desc
func NewMessage() *Message {
Message := &Message{}
return Message
}
// NewGmmMessage TODO:desc
func NewGmmMessage() *GmmMessage {
GmmMessage := &GmmMessage{}
return GmmMessage
}
// NewGmmMessage TODO:desc
func NewGsmMessage() *GsmMessage {
GsmMessage := &GsmMessage{}
return GsmMessage
}
// GmmHeader Octet1 protocolDiscriminator securityHeaderType
//
// Octet2 MessageType
type GmmHeader struct {
Octet [3]uint8
}
type GsmHeader struct {
Octet [4]uint8
}
// GetMessageType 9.8
func (a *GmmHeader) GetMessageType() (messageType uint8) {
messageType = a.Octet[2]
return messageType
}
// GetMessageType 9.8
func (a *GmmHeader) SetMessageType(messageType uint8) {
a.Octet[2] = messageType
}
func (a *GmmHeader) GetExtendedProtocolDiscriminator() uint8 {
return a.Octet[0]
}
func (a *GmmHeader) SetExtendedProtocolDiscriminator(epd uint8) {
a.Octet[0] = epd
}
func (a *GsmHeader) GetExtendedProtocolDiscriminator() uint8 {
return a.Octet[0]
}
func (a *GsmHeader) SetExtendedProtocolDiscriminator(epd uint8) {
a.Octet[0] = epd
}
// GetMessageType 9.8
func (a *GsmHeader) GetMessageType() (messageType uint8) {
messageType = a.Octet[3]
return messageType
}
// GetMessageType 9.8
func (a *GsmHeader) SetMessageType(messageType uint8) {
a.Octet[3] = messageType
}
func GetEPD(byteArray []byte) uint8 {
return byteArray[0]
}
func GetSecurityHeaderType(byteArray []byte) uint8 {
return byteArray[1]
}
func (a *Message) PlainNasDecode(byteArray *[]byte) error {
if byteArray == nil {
return errors.New("byteArray is nil")
}
if len(*byteArray) == 0 {
return errors.New("empty message")
}
epd := GetEPD(*byteArray)
switch epd {
case nasMessage.Epd5GSMobilityManagementMessage:
return a.GmmMessageDecode(byteArray)
case nasMessage.Epd5GSSessionManagementMessage:
return a.GsmMessageDecode(byteArray)
}
return fmt.Errorf("Extended Protocol Discriminator[%d] is not allowed in Nas Message Deocde", epd)
}
func (a *Message) PlainNasEncode() ([]byte, error) {
data := new(bytes.Buffer)
if a.GmmMessage != nil {
err := a.GmmMessageEncode(data)
return data.Bytes(), err
} else if a.GsmMessage != nil {
err := a.GsmMessageEncode(data)
return data.Bytes(), err
}
return nil, fmt.Errorf("Gmm/Gsm Message are both empty in Nas Message Encode")
}