1 /******************************************************************************
2 ###############################################################################
3 # Copyright (c) [2017-2020] [ICT/CAS] #
4 # Licensed under the ORAN Software License v1.0 (License) #
5 ###############################################################################
6 ******************************************************************************/
11 #include <sys/socket.h>
12 #include <netinet/in.h>
14 #include "vos_linklist.h"
15 #include "vos_types.h"
16 #include "plat_syslog.h"
17 #include "vos_sockMgr.h"
18 #include "vos_module.h"
19 #include "gnbCommon.h"
23 #define GTP_MAX_TUN_NUM 100
24 #define GTPU_MODULE 1 /* just for compile */
25 #define GTP_MAX_DATA_LEN 8000
26 #define GTP_HEADER_SIZE_SHORT 8
27 #define GTP_HEADER_SIZE_LONG 12
29 /* GTP version 1 extension header type definitions. */
30 #define GTP_EXT_PDCP_PDU 0xC0 /* PDCP PDU Number */
31 #define GTP_EXT_RAN_CONTAINER 0X81 /* RAN Container */
32 #define GTP_EXT_NRRAN_CONTAINER 0X84 /* NR RAN Container */
33 #define GTP_EXT_PDUSESSION_CONTAINER 0X85 /* PDU Session Container */
36 #define GTP_ECHO_REQ 1 /* Echo Request */
37 #define GTP_ECHO_RSP 2 /* Echo Response */
38 #define GTP_NOT_SUPPORTED 3 /* Version Not Supported */
40 #define GTP_ERROR 26 /* Error Indication */
41 #define GTP_SUPP_EXT_HEADER 31 /* Supported Extension Headers Notification */
42 #define GTP_GPDU 255 /* G-PDU */
43 #define GTP_END_MARKER 254
46 #define GTP_DLUSERDATA_EX_LEN 2
47 #define GTP_DLUSERDATA_BYTES_LEN 8
48 #define GTP_DDDS_EX_LEN 6
49 #define GTP_DDDS_BYTES_LEN 24
52 #define GTP_DLPDUSESSION_EX_LEN 2
53 #define GTP_DLPDUSESSION_BYTES_LEN 8
54 #define GTP_ULPDUSESSION_EX_LEN 1
55 #define GTP_ULPDUSESSION_BYTES_LEN 4
57 #define GTP_EXHEAD_LEN 256
60 #define PATH_DETECT_TIMER 60 /* detect path every 60 seconds */
61 #define T3RESPONSE_TIMER 3 /* T3-RESPONSE timer defaults to 1 second */
62 #define N3REQUESTS 5 /* max times of retransmssion of echo quest */
64 #define GTPU_ENTITY_NUM 2 /* CUGTPU+DUGTPU */
66 #define gtpLog(iLog_Level, gtp, content, arg...) \
70 VOS_SysLog(gtp->userModuleID, iLog_Level, __FILE__, __LINE__, __func__, content, ##arg);\
73 vos_err_print("gtp entity is NULL!\n");\
78 #define gtpNullCheck(p,gtp) \
82 gtpLog(LOG_ERR,gtp,"[GTPU] NULL pointer\n");\
109 /* GTP-U header definitions */
112 { /* Descriptions from 3GPP 29060 */
113 UINT8 pn :1; /* .......0 PN: N-PDU Number flag */
114 UINT8 s :1; /* ......0. Sequence number flag: 0 */
115 UINT8 e :1; /* .....0.. Extension header flag: 0 */
116 UINT8 spare :1; /* ....0... Spare = 0 */
117 UINT8 pt :1; /* ...1.... Protocol Type: GTP=1, GTP'=0 */
118 UINT8 ver :3; /* 001..... Version: 1 */
119 UINT8 type; /* 02 Message type. T-PDU = 0xff */
120 UINT16 length; /* 03 Length (of IP packet or signalling) */
121 UINT32 tei; /* 05 - 08 Tunnel Endpoint ID */
125 { /* Descriptions from 3GPP 29060 */
126 UINT8 pn :1; /* .......0 PN: N-PDU Number flag */
127 UINT8 s :1; /* ......0. Sequence number flag: 0 */
128 UINT8 e :1; /* .....0.. Extension header flag: 0 */
129 UINT8 spare :1; /* ....0... Spare = 0 */
130 UINT8 pt :1; /* ...1.... Protocol Type: GTP=1, GTP'=0 */
131 UINT8 ver :3; /* 001..... Version: 1 */
132 UINT8 type; /* 02 Message type. T-PDU = 0xff */
133 UINT16 length; /* 03 Length (of IP packet or signalling) */
134 UINT32 tei; /* 05 - 08 Tunnel Endpoint ID */
135 UINT16 seq; /* 09 - 10 Sequence Number */
136 UINT8 npdu; /* 11 N-PDU Number */
137 UINT8 next; /* 12 Next extension header type. Empty = 0 */
145 } __attribute__((packed));
146 #define GTP_EXTHDR(buf) ((struct gtpExtHead *)buf)
147 #define GTP_LHDR(buf) ((GtpHeadLong_t *)buf)
148 #define GTP_FIRSTHDR(buf) ((GTP_LHDR(buf)->length >= 5 && \
149 GTP_LHDR(buf)->next) ? \
150 (struct gtpExtHead *)>P_LHDR(buf)->next : NULL)
151 #define GTP_NXTHDR(buf, ext) \
152 (ext == NULL ? GTP_FIRSTHDR(buf) : \
153 ((uint8_t *)&ext->len + ext->len * 4 - \
154 (uint8_t *)>P_LHDR(buf)->seq) > \
155 GTP_LHDR(buf)->length ? \
156 NULL : *((uint8_t *)&ext->len + ext->len * 4 - 1) ? \
157 (struct gtpExtHead *)((uint8_t *)&ext->len + \
158 ext->len * 4 - 1) : NULL)
159 #define GTP_EXTDATA(buf) (GTP_LHDR(buf)->data)
167 UINT8 p[GTP_MAX_DATA_LEN]; /* store data */
173 UINT8 p[GTP_MAX_DATA_LEN]; /* store data */
178 GtpPacketShort_t gtps;
179 GtpPacketLong_t gtpl;
183 /* GTP-U tunnels management structures */
186 TunnelStatus_e tunState; /* 0 for suspend, others are active */
187 UINT16 seq; /* G-PUD Sequence number counter */
189 UINT32 ownTeid; /* own TEID */
190 UINT32 remTeid; /* remote TEID */
191 vos_sock_info_t tunOpval; /* Address packet was sent to / received from */
192 TunnelType_e tunType;
196 typedef struct gtpMsg
198 UINT16 seq; /* The sequence number */
199 UINT8 type; /* The type of packet */
200 UINT32 l; /* Length of the packet */
201 union gtpPacket p; /* The packet stored */
202 vos_sock_info_t *pOpval; /* Address packet was sent to / received from */
203 time_t timeout; /* When do we retransmit this packet? */
204 UINT8 retrans; /* How many times did we retransmit this? */
208 /* GTP-U Protocol entity */
214 /* Parameters related to the network INT32erface */
215 vos_sockaddr_t localAddr; /* Local IP address for signaling and G-PDU */
217 /* Parameters related to signaling messages */
218 UINT16 seq_next; /* Next sequence number to use */
219 UINT8 restart_counter; /* Increment on restart. */
220 plist pReqMsgList; /* list header */
221 Tun_t *tuns[GTP_MAX_TUN_NUM]; /* store memory,use TEID_OWN assignment */
223 /* Counters ,some of them may not be used. */
224 UINT32 err_socket; /* Number of socket errors */
225 UINT32 err_readfrom; /* Number of readfrom errors */
226 UINT32 err_sendto; /* Number of sendto errors */
227 UINT32 err_memcpy; /* Number of memcpy */
228 UINT32 err_seq; /* Number of seq out of range */
229 UINT32 err_cause; /* Unexpected cause value received */
231 UINT32 empty; /* Number of empty packets */
232 UINT32 unsup; /* Number of unsupported version 29.60 11.1.1 */
233 UINT32 tooshort; /* Number of too short headers 29.60 11.1.2 */
234 UINT32 unknown; /* Number of unknown messages 29.60 11.1.3 */
235 UINT32 nu_rabid; /* Number of unknown RAD ID message */
236 UINT32 unexpect; /* Number of unexpected messages 29.60 11.1.4 */
237 UINT32 duplicate; /* Number of duplicate or unsolicited replies */
238 UINT32 missing; /* Number of missing information field messages */
239 UINT32 incorrect; /* Number of incorrect information field messages */
240 UINT32 invalid; /* Number of invalid message format messages */
246 UINT8 *pPdu; /* length, content, next extension type */
247 UINT16 len; /* len = exHeaderLen * 4 */
253 typedef enum gtpuLogLevel
255 GTPULOG_CRIT = 1, ///< critical conditions
256 GTPULOG_ERR, ///< error conditions
257 GTPULOG_WARNING, ///< warning conditions
258 GTPULOG_INFO, ///< informational
259 GTPULOG_DEBUG, ///< debug info
262 /* struct for gtpu receive data */
272 MsgbBuff_t *pMsgBuff;
273 ExPduInfo_t *pExPduInfo;
276 typedef INT32 (* GtpuDataProcFn)(GpduInfo_t *pInfo);
280 GtpuDataProcFn action_fn_p;
288 #define gtpCheckTeid(n) if(n<0 || n>GTP_MAX_TUN_NUM) {vos_err_print(#n" is wrong\r\n");return VOS_ERROR;}
291 #endif /* GTPU_COMMON_H */