1 /******************************************************************************
3 * Copyright (c) 2020 ICT/CAS.
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
9 * https://www.o-ran.org/software
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.
17 *******************************************************************************/
23 #include <sys/socket.h>
24 #include <netinet/in.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"
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
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 */
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 */
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
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
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
69 #define GTP_EXHEAD_LEN 256
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 */
76 #define GTPU_ENTITY_NUM 2 /* CUGTPU+DUGTPU */
78 #define gtpLog(iLog_Level, gtp, content, arg...) \
82 VOS_SysLog(gtp->userModuleID, iLog_Level, __FILE__, __LINE__, __func__, content, ##arg);\
85 vos_err_print("gtp entity is NULL!\n");\
90 #define gtpNullCheck(p,gtp) \
94 gtpLog(LOG_ERR,gtp,"[GTPU] NULL pointer\n");\
121 /* GTP-U header definitions */
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 */
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 */
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 *)>P_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 *)>P_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)
179 UINT8 p[GTP_MAX_DATA_LEN]; /* store data */
185 UINT8 p[GTP_MAX_DATA_LEN]; /* store data */
190 GtpPacketShort_t gtps;
191 GtpPacketLong_t gtpl;
195 /* GTP-U tunnels management structures */
198 TunnelStatus_e tunState; /* 0 for suspend, others are active */
199 UINT16 seq; /* G-PUD Sequence number counter */
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;
208 typedef struct gtpMsg
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? */
220 /* GTP-U Protocol entity */
226 /* Parameters related to the network INT32erface */
227 vos_sockaddr_t localAddr; /* Local IP address for signaling and G-PDU */
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 */
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 */
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 */
258 UINT8 *pPdu; /* length, content, next extension type */
259 UINT16 len; /* len = exHeaderLen * 4 */
265 typedef enum gtpuLogLevel
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
274 /* struct for gtpu receive data */
284 MsgbBuff_t *pMsgBuff;
285 ExPduInfo_t *pExPduInfo;
288 typedef INT32 (* GtpuDataProcFn)(GpduInfo_t *pInfo);
292 GtpuDataProcFn action_fn_p;
300 #define gtpCheckTeid(n) if(n<0 || n>GTP_MAX_TUN_NUM) {vos_err_print(#n" is wrong\r\n");return VOS_ERROR;}
303 #endif /* GTPU_COMMON_H */