Modify licenses
[scp/ocu/5gnr.git] / Include / gtpCommon.h
1 /******************************************************************************
2 *
3 *   Copyright (c) 2020 ICT/CAS.
4 *
5 *   Licensed under the O-RAN Software License, Version 1.0 (the "Software License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       https://www.o-ran.org/software
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19
20 #ifndef GTPU_COMMON_H
21 #define GTPU_COMMON_H
22
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include "vos_lib.h"
26 #include "vos_linklist.h"
27 #include "vos_types.h"
28 #include "plat_syslog.h"
29 #include "vos_sockMgr.h"
30 #include "vos_module.h"
31 #include "gnbCommon.h"
32 #include "msgb.h"
33
34
35 #define GTP_MAX_TUN_NUM                                  100
36 #define GTPU_MODULE                                        1    /* just for compile */
37 #define GTP_MAX_DATA_LEN                                8000
38 #define GTP_HEADER_SIZE_SHORT                      8
39 #define GTP_HEADER_SIZE_LONG                      12
40
41 /* GTP version 1 extension header type definitions. */
42 #define GTP_EXT_PDCP_PDU                        0xC0    /* PDCP PDU Number */
43 #define GTP_EXT_RAN_CONTAINER                   0X81    /* RAN Container */
44 #define GTP_EXT_NRRAN_CONTAINER                 0X84    /* NR RAN Container */
45 #define GTP_EXT_PDUSESSION_CONTAINER    0X85    /* PDU Session Container */
46
47
48 #define GTP_ECHO_REQ                               1    /* Echo Request */
49 #define GTP_ECHO_RSP                               2    /* Echo Response */
50 #define GTP_NOT_SUPPORTED                          3    /* Version Not Supported */
51
52 #define GTP_ERROR                                 26    /* Error Indication */
53 #define GTP_SUPP_EXT_HEADER                       31    /* Supported Extension Headers Notification */
54 #define GTP_GPDU                                 255    /* G-PDU */
55 #define GTP_END_MARKER                           254
56
57 /* for f1u pdu */
58 #define GTP_DLUSERDATA_EX_LEN                      2
59 #define GTP_DLUSERDATA_BYTES_LEN                   8
60 #define GTP_DDDS_EX_LEN                                    6
61 #define GTP_DDDS_BYTES_LEN                                24
62
63 /* for ngu pdu */
64 #define GTP_DLPDUSESSION_EX_LEN                    2
65 #define GTP_DLPDUSESSION_BYTES_LEN                 8
66 #define GTP_ULPDUSESSION_EX_LEN                    1
67 #define GTP_ULPDUSESSION_BYTES_LEN                 4
68
69 #define GTP_EXHEAD_LEN                                   256
70
71
72 #define PATH_DETECT_TIMER                                 60      /* detect path every 60 seconds */
73 #define T3RESPONSE_TIMER                                   3      /* T3-RESPONSE timer defaults to 1 second */
74 #define N3REQUESTS                                         5      /* max times of retransmssion of echo quest */
75
76 #define GTPU_ENTITY_NUM                                    2      /* CUGTPU+DUGTPU */
77
78 #define gtpLog(iLog_Level, gtp, content, arg...) \
79 {\
80         if(NULL != gtp)\
81         {\
82                 VOS_SysLog(gtp->userModuleID, iLog_Level, __FILE__, __LINE__, __func__, content, ##arg);\
83         }else\
84         {\
85                 vos_err_print("gtp entity is NULL!\n");\
86         }\
87 }
88
89 #if 0
90 #define gtpNullCheck(p,gtp) \
91 {\
92    if(NULL == p)\
93    {\
94            gtpLog(LOG_ERR,gtp,"[GTPU] NULL pointer\n");\
95        return VOS_ERROR;\
96    }\
97 }
98 #endif
99
100 typedef enum
101 {
102         CU_GTPU,
103         DU_GTPU
104 }GtpType_e;
105
106 typedef enum
107 {
108         CU_NG_TUNNEL,
109         CU_F1_TUNNEL,
110         DU_TUNNEL
111 }TunnelType_e;
112
113 typedef enum
114 {
115         TUNNEL_INACTIVE = 0,
116         TUNNEL_ACTIVE
117 }TunnelStatus_e;
118
119
120
121 /* GTP-U header definitions */
122
123 typedef struct
124 { /*    Descriptions from 3GPP 29060 */
125     UINT8 pn :1;                /*    .......0 PN: N-PDU Number flag */
126     UINT8 s :1;                 /*    ......0. Sequence number flag: 0 */
127     UINT8 e :1;                 /*    .....0.. Extension header flag: 0 */
128     UINT8 spare :1;             /*    ....0... Spare = 0 */
129     UINT8 pt :1;                /*    ...1.... Protocol Type: GTP=1, GTP'=0 */
130     UINT8 ver :3;               /*    001..... Version: 1 */
131     UINT8 type;                 /* 02 Message type. T-PDU = 0xff */
132     UINT16 length;              /* 03 Length (of IP packet or signalling) */
133     UINT32 tei;                 /* 05 - 08 Tunnel Endpoint ID */
134 } GtpHeadShort_t;
135
136 typedef struct
137 { /*    Descriptions from 3GPP 29060 */
138     UINT8 pn :1;                /*    .......0 PN: N-PDU Number flag */
139     UINT8 s :1;                 /*    ......0. Sequence number flag: 0 */
140     UINT8 e :1;                 /*    .....0.. Extension header flag: 0 */
141     UINT8 spare :1;             /*    ....0... Spare = 0 */
142     UINT8 pt :1;                /*    ...1.... Protocol Type: GTP=1, GTP'=0 */
143     UINT8 ver :3;               /*    001..... Version: 1 */
144     UINT8 type;                 /* 02 Message type. T-PDU = 0xff */
145     UINT16 length;              /* 03 Length (of IP packet or signalling) */
146     UINT32 tei;                 /* 05 - 08 Tunnel Endpoint ID */
147     UINT16 seq;                 /* 09 - 10 Sequence Number */
148     UINT8 npdu;                 /* 11 N-PDU Number */
149     UINT8 next;                 /* 12 Next extension header type. Empty = 0 */
150 }GtpHeadLong_t;
151
152
153 struct gtpExtHead{
154         uint8_t type;
155         uint8_t len;
156         uint8_t data[];
157 } __attribute__((packed));
158 #define GTP_EXTHDR(buf) ((struct gtpExtHead *)buf)
159 #define GTP_LHDR(buf) ((GtpHeadLong_t *)buf)
160 #define GTP_FIRSTHDR(buf) ((GTP_LHDR(buf)->length >= 5 &&               \
161                 GTP_LHDR(buf)->next) ?                                  \
162                 (struct gtpExtHead *)&GTP_LHDR(buf)->next : NULL)
163 #define GTP_NXTHDR(buf, ext)                                            \
164         (ext == NULL ? GTP_FIRSTHDR(buf) :                              \
165                 ((uint8_t *)&ext->len + ext->len * 4 -                  \
166                 (uint8_t *)&GTP_LHDR(buf)->seq) >                       \
167                 GTP_LHDR(buf)->length ?                                 \
168                 NULL : *((uint8_t *)&ext->len + ext->len * 4 - 1) ?     \
169                         (struct gtpExtHead *)((uint8_t *)&ext->len +    \
170                         ext->len * 4 - 1) : NULL)
171 #define GTP_EXTDATA(buf) (GTP_LHDR(buf)->data)
172
173
174
175 /* GTP-U packet */
176 typedef struct
177 {
178     GtpHeadShort_t h;
179     UINT8                        p[GTP_MAX_DATA_LEN];           /* store data */
180 }GtpPacketShort_t;
181
182 typedef struct
183 {
184     GtpHeadLong_t h;
185     UINT8                       p[GTP_MAX_DATA_LEN];            /* store data */
186 }GtpPacketLong_t;
187
188 union gtpPacket
189 {
190     GtpPacketShort_t gtps;
191     GtpPacketLong_t  gtpl;
192 };
193
194
195 /* GTP-U tunnels management structures */
196 typedef struct tun
197 {
198         TunnelStatus_e                  tunState;                               /* 0 for suspend, others are active */
199         UINT16                                  seq;                                    /* G-PUD Sequence number counter */
200     UINT32                                      tunId;
201     UINT32                                      ownTeid;                                /* own TEID */
202     UINT32                                      remTeid;                                /* remote TEID */
203     vos_sock_info_t                     tunOpval;                               /* Address packet was sent to / received from */
204         TunnelType_e                    tunType;
205 } Tun_t;
206
207
208 typedef struct gtpMsg
209 {
210     UINT16                                      seq;                    /* The sequence number */
211     UINT8                                       type;                   /* The type of packet */
212     UINT32                                      l;                              /* Length of the packet */
213     union gtpPacket             p;                              /* The packet stored */
214     vos_sock_info_t                     *pOpval;                /* Address packet was sent to / received from */
215     time_t                                      timeout;                /* When do we retransmit this packet? */
216         UINT8                                   retrans;                /* How many times did we retransmit this? */
217 }GtpMsgNode_t;
218
219
220 /* GTP-U Protocol entity */
221 typedef struct
222 {
223         GtpType_e               gtpId;
224         ULONG                   userModuleID;
225
226         /* Parameters related to the network INT32erface */
227         vos_sockaddr_t  localAddr;                                              /* Local IP address for signaling and G-PDU */
228
229     /* Parameters related to signaling messages */
230     UINT16                      seq_next;                                               /* Next sequence number to use */
231     UINT8                       restart_counter;                                /* Increment on restart. */
232         plist                   pReqMsgList;                                    /* list header */
233         Tun_t              *tuns[GTP_MAX_TUN_NUM];                      /* store memory,use TEID_OWN assignment */
234
235     /* Counters ,some of them may not be used. */
236     UINT32 err_socket;                  /* Number of socket errors */
237     UINT32 err_readfrom;                /* Number of readfrom errors */
238     UINT32 err_sendto;                  /* Number of sendto errors */
239     UINT32 err_memcpy;                  /* Number of memcpy */
240     UINT32 err_seq;                     /* Number of seq out of range */
241     UINT32 err_cause;                   /* Unexpected cause value received */
242
243     UINT32 empty;                               /* Number of empty packets */
244     UINT32 unsup;                               /* Number of unsupported version 29.60 11.1.1 */
245     UINT32 tooshort;                    /* Number of too short headers 29.60 11.1.2 */
246     UINT32 unknown;                     /* Number of unknown messages 29.60 11.1.3 */
247     UINT32 nu_rabid;                    /* Number of unknown RAD ID message */
248     UINT32 unexpect;                    /* Number of unexpected messages 29.60 11.1.4 */
249     UINT32 duplicate;                   /* Number of duplicate or unsolicited replies */
250     UINT32 missing;                     /* Number of missing information field messages */
251     UINT32 incorrect;                   /* Number of incorrect information field messages */
252     UINT32 invalid;                     /* Number of invalid message format messages */
253 } Gtp_t;
254
255
256 typedef struct
257 {
258         UINT8      *pPdu;                       /* length, content, next extension type */
259         UINT16          len;                    /* len = exHeaderLen * 4 */
260         UINT16          exHdrLen;
261         UINT8           exHdrType;
262 }ExPduInfo_t;
263
264 /* log level */
265 typedef enum gtpuLogLevel
266 {
267         GTPULOG_CRIT = 1,        ///<  critical conditions
268         GTPULOG_ERR,             ///<  error conditions
269         GTPULOG_WARNING,         ///<  warning conditions
270         GTPULOG_INFO,            ///<  informational
271         GTPULOG_DEBUG,           ///<  debug info
272 } GtpuLogLevel_e;
273
274 /* struct for gtpu receive data */
275 typedef struct
276 {
277         vos_sock_info_t op;
278         MsgbBuff_t *pData;
279 }GtpuRecvUdpInfo_t;
280
281 typedef struct
282 {
283     UINT32 tunId;
284         MsgbBuff_t *pMsgBuff;
285         ExPduInfo_t *pExPduInfo;
286 }GpduInfo_t;
287
288 typedef INT32 (* GtpuDataProcFn)(GpduInfo_t *pInfo);
289
290 typedef struct
291 {
292         GtpuDataProcFn action_fn_p;
293 }GtpuGpduHandler_t;
294
295
296
297
298
299 //check teid
300 #define gtpCheckTeid(n) if(n<0 || n>GTP_MAX_TUN_NUM) {vos_err_print(#n" is wrong\r\n");return VOS_ERROR;}
301
302
303 #endif /* GTPU_COMMON_H */