blob: 2a4e57af1683598bda4ac0c9fb5157a90ce657e9 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
2 * OpenGGSN - Gateway GPRS Support Node
3 * Copyright (C) 2002 Mondru AB.
4 *
5 * The contents of this file may be used under the terms of the GNU
6 * General Public License Version 2, provided that the above copyright
7 * notice and this permission notice is included in all copies or
8 * substantial portions of the software.
9 *
10 * The initial developer of the original code is
11 * Jens Jakobsen <jj@openggsn.org>
12 *
13 * Contributor(s):
14 *
15 */
16
17#ifndef _GTP_H
18#define _GTP_H
19
20#define GTP0_PORT 3386
21#define GTP1C_PORT 2123
22#define GTP1U_PORT 2152
23#define PACKET_MAX 8196
24
25#define GTP_MAX 0xffff /* TODO: Choose right number */
26#define GTP0_HEADER_SIZE 20
27#define GTP1_HEADER_SIZE_SHORT 8
28#define GTP1_HEADER_SIZE_LONG 12
29
30#define SYSLOG_PRINTSIZE 255
31#define ERRMSG_SIZE 255
32
33#define RESTART_FILE "gsn_restart"
34#define NAMESIZE 1024
35
36/* GTP version 1 message type definitions. Also covers version 0 except *
37 * for anonymous PDP context which was superceded in version 1 */
38
39/* 0 For future use. */
40#define GTP_ECHO_REQ 1 /* Echo Request */
41#define GTP_ECHO_RSP 2 /* Echo Response */
42#define GTP_NOT_SUPPORTED 3 /* Version Not Supported */
43#define GTP_ALIVE_REQ 4 /* Node Alive Request */
44#define GTP_ALIVE_RSP 5 /* Node Alive Response */
45#define GTP_REDIR_REQ 6 /* Redirection Request */
46#define GTP_REDIR_RSP 7 /* Redirection Response */
47/* 8-15 For future use. */
48#define GTP_CREATE_PDP_REQ 16 /* Create PDP Context Request */
49#define GTP_CREATE_PDP_RSP 17 /* Create PDP Context Response */
50#define GTP_UPDATE_PDP_REQ 18 /* Update PDP Context Request */
51#define GTP_UPDATE_PDP_RSP 19 /* Update PDP Context Response */
52#define GTP_DELETE_PDP_REQ 20 /* Delete PDP Context Request */
53#define GTP_DELETE_PDP_RSP 21 /* Delete PDP Context Response */
54/* 22-25 For future use. */ /* In version GTP 1 anonomous PDP context */
55#define GTP_ERROR 26 /* Error Indication */
56#define GTP_PDU_NOT_REQ 27 /* PDU Notification Request */
57#define GTP_PDU_NOT_RSP 28 /* PDU Notification Response */
58#define GTP_PDU_NOT_REJ_REQ 29 /* PDU Notification Reject Request */
59#define GTP_PDU_NOT_REJ_RSP 30 /* PDU Notification Reject Response */
60#define GTP_SUPP_EXT_HEADER 31 /* Supported Extension Headers Notification */
61#define GTP_SND_ROUTE_REQ 32 /* Send Routeing Information for GPRS Request */
62#define GTP_SND_ROUTE_RSP 33 /* Send Routeing Information for GPRS Response */
63#define GTP_FAILURE_REQ 34 /* Failure Report Request */
64#define GTP_FAILURE_RSP 35 /* Failure Report Response */
65#define GTP_MS_PRESENT_REQ 36 /* Note MS GPRS Present Request */
66#define GTP_MS_PRESENT_RSP 37 /* Note MS GPRS Present Response */
67/* 38-47 For future use. */
68#define GTP_IDEN_REQ 48 /* Identification Request */
69#define GTP_IDEN_RSP 49 /* Identification Response */
70#define GTP_SGSN_CONTEXT_REQ 50 /* SGSN Context Request */
71#define GTP_SGSN_CONTEXT_RSP 51 /* SGSN Context Response */
72#define GTP_SGSN_CONTEXT_ACK 52 /* SGSN Context Acknowledge */
73#define GTP_FWD_RELOC_REQ 53 /* Forward Relocation Request */
74#define GTP_FWD_RELOC_RSP 54 /* Forward Relocation Response */
75#define GTP_FWD_RELOC_COMPL 55 /* Forward Relocation Complete */
76#define GTP_RELOC_CANCEL_REQ 56 /* Relocation Cancel Request */
77#define GTP_RELOC_CANCEL_RSP 57 /* Relocation Cancel Response */
78#define GTP_FWD_SRNS 58 /* Forward SRNS Context */
79#define GTP_FWD_RELOC_ACK 59 /* Forward Relocation Complete Acknowledge */
80#define GTP_FWD_SRNS_ACK 60 /* Forward SRNS Context Acknowledge */
81/* 61-239 For future use. */
82#define GTP_DATA_TRAN_REQ 240 /* Data Record Transfer Request */
83#define GTP_DATA_TRAN_RSP 241 /* Data Record Transfer Response */
84/* 242-254 For future use. */
85#define GTP_GPDU 255 /* G-PDU */
86
87/* GTP 0 header.
88 * Explanation to some of the fields:
89 * SNDCP NPDU Number flag = 0 except for inter SGSN handover situations
90 * SNDCP N-PDU LCC Number 0 = 0xff except for inter SGSN handover situations
91 * Sequence number. Used for reliable delivery of signalling messages, and
92 * to discard "illegal" data messages.
93 * Flow label. Is used to point a particular PDP context. Is used in data
94 * messages as well as signalling messages related to a particular context.
95 * Tunnel ID is IMSI+NSAPI. Unique identifier of PDP context. Is somewhat
96 * redundant because the header also includes flow. */
97
98struct gtp0_header { /* Descriptions from 3GPP 09.60 */
99 u_int8_t flags; /* 01 bitfield, with typical values */
100 /* 000..... Version: 1 (0) */
101 /* ...1111. Spare (7) */
102 /* .......0 SNDCP N-PDU Number flag (0) */
103 u_int8_t type; /* 02 Message type. T-PDU = 0xff */
104 u_int16_t length; /* 03 Length (of G-PDU excluding header) */
105 u_int16_t seq; /* 05 Sequence Number */
106 u_int16_t flow; /* 07 Flow Label ( = 0 for signalling) */
107 u_int8_t number; /* 09 SNDCP N-PDU LCC Number ( 0 = 0xff) */
108 u_int8_t spare1; /* 10 Spare */
109 u_int8_t spare2; /* 11 Spare */
110 u_int8_t spare3; /* 12 Spare */
111 u_int64_t tid; /* 13 Tunnel ID */
112}; /* 20 */
113
114struct gtp1_header_short { /* Descriptions from 3GPP 29060 */
115 u_int8_t flags; /* 01 bitfield, with typical values */
116 /* 000..... Version: 1 */
117 /* ...1.... Protocol Type: GTP=1, GTP'=0 */
118 /* ....1... Spare = 1 */
119 /* .....1.. Extension header flag: 1 */
120 /* ......1. Sequence number flag: 1 */
121 /* .......0 PN: N-PDU Number flag */
122 u_int8_t type; /* 02 Message type. T-PDU = 0xff */
123 u_int16_t length; /* 03 Length (of IP packet or signalling) */
124 u_int64_t tid; /* 05 - 08 Tunnel ID */
125};
126
127struct gtp1_header_long { /* Descriptions from 3GPP 29060 */
128 u_int8_t flags; /* 01 bitfield, with typical values */
129 /* 000..... Version: 1 */
130 /* ...1.... Protocol Type: GTP=1, GTP'=0 */
131 /* ....1... Spare = 1 */
132 /* .....1.. Extension header flag: 1 */
133 /* ......1. Sequence number flag: 1 */
134 /* .......0 PN: N-PDU Number flag */
135 u_int8_t type; /* 02 Message type. T-PDU = 0xff */
136 u_int16_t length; /* 03 Length (of IP packet or signalling) */
137 u_int64_t tid; /* 05 Tunnel ID */
138 u_int16_t seq; /* 10 Sequence Number */
139 u_int8_t npdu; /* 11 N-PDU Number */
140 u_int8_t next; /* 12 Next extension header type. Empty = 0 */
141};
142
143struct gtp0_packet {
144 struct gtp0_header h;
145 u_int8_t p[GTP_MAX];
146} __attribute__((packed));
147
148struct gtp1_packet_short {
149 struct gtp1_header_short h;
150 u_int8_t p[GTP_MAX];
151} __attribute__((packed));
152
153struct gtp1_packet_long {
154 struct gtp1_header_long h;
155 u_int8_t p[GTP_MAX];
156} __attribute__((packed));
157
158union gtp_packet {
159 u_int8_t flags;
160 struct gtp0_packet gtp0;
161 struct gtp1_packet_short gtp1s;
162 struct gtp1_packet_long gtp1l;
163} __attribute__((packed)) h;
164
165
166
167
168/* ***********************************************************
169 * Information storage for each gsn instance
170 *
171 * Normally each instance of the application corresponds to
172 * one instance of a gsn.
173 *
174 * In order to avoid global variables in the application, and
175 * also in order to allow several instances of a gsn in the same
176 * application this struct is provided in order to store all
177 * relevant information related to the gsn.
178 *
179 * Note that this does not include information storage for '
180 * each pdp context. This is stored in another struct.
181 *************************************************************/
182
183struct gsn_t {
184 /* Parameters related to the network interface */
185
186 int fd; /* File descriptor to network interface */
187 struct in_addr gsnc; /* IP address of this gsn for signalling */
188 struct in_addr gsnu; /* IP address of this gsn for user traffic */
189
190 /* Parameters related to signalling messages */
191 uint16_t seq_next; /* Next sequence number to use */
192 int seq_first; /* First packet in queue (oldest timeout) */
193 int seq_last; /* Last packet in queue (youngest timeout) */
194
195 unsigned char restart_counter; /* Increment on restart. Stored on disk */
196 char *statedir; /* Disk location for permanent storage */
197
198 struct queue_t *queue_req; /* Request queue */
199 struct queue_t *queue_resp; /* Response queue */
200
201 /* Call back functions */
202 int (*cb_delete_context) (struct pdp_t*);
203 int (*cb_create_context) (struct pdp_t*);
204 int (*cb_conf) (int type, int cause, struct pdp_t *pdp, void* aid);
205 int (*cb_gpdu) (struct pdp_t* pdp, void* pack, unsigned len);
206
207 /* Counters */
208
209 uint64_t err_socket; /* Number of socket errors */
210 uint64_t err_readfrom; /* Number of readfrom errors */
211 uint64_t err_sendto; /* Number of sendto errors */
212 uint64_t err_memcpy; /* Number of memcpy */
213 uint64_t err_queuefull; /* Number of times queue was full */
214 uint64_t err_seq; /* Number of seq out of range */
215 uint64_t err_address; /* GSN address conversion failed */
216 uint64_t err_unknownpdp; /* GSN address conversion failed */
217 uint64_t err_unknowntid; /* Application supplied unknown imsi+nsapi */
218 uint64_t err_cause; /* Unexpected cause value received */
219 uint64_t err_outofpdp; /* Out of storage for PDP contexts */
220
221 uint64_t empty; /* Number of empty packets */
222 uint64_t unsup; /* Number of unsupported version 29.60 11.1.1 */
223 uint64_t tooshort; /* Number of too short headers 29.60 11.1.2 */
224 uint64_t unknown; /* Number of unknown messages 29.60 11.1.3 */
225 uint64_t unexpect; /* Number of unexpected messages 29.60 11.1.4 */
226 uint64_t dublicate; /* Number of dublicate or unsolicited replies */
227 uint64_t missing; /* Number of missing mandatory field messages */
228 uint64_t invalid; /* Number of invalid message format messages */
229};
230
231
232/* External API functions */
233
234extern const char* gtp_version();
235extern int gtp_new(struct gsn_t **gsn, char *statedir, struct in_addr *listen);
236extern int gtp_free(struct gsn_t *gsn);
237
238extern int gtp_newpdp(struct gsn_t *gsn, struct pdp_t **pdp,
239 uint64_t imsi, uint8_t nsapi);
240extern int gtp_freepdp(struct gsn_t *gsn, struct pdp_t *pdp);
241
242extern int gtp_create_context(struct gsn_t *gsn, struct pdp_t *pdp, void *aid,
243 struct in_addr* inetaddr);
244extern int gtp_update_context(struct gsn_t *gsn, struct pdp_t *pdp, void *aid,
245 struct in_addr* inetaddr);
246extern int gtp_delete_context(struct gsn_t *gsn, struct pdp_t *pdp, void *aid);
247
248extern int gtp_gpdu(struct gsn_t *gsn, struct pdp_t *pdp,
249 void *pack, unsigned len);
250
251extern int gtp_fd(struct gsn_t *gsn);
252extern int gtp_decaps(struct gsn_t *gsn);
253extern int gtp_retrans(struct gsn_t *gsn);
254extern int gtp_retranstimeout(struct gsn_t *gsn, struct timeval *timeout);
255
256/*
257extern int gtp_set_cb_newpdp(struct gsn_t *gsn,
258 int (*cb) (struct pdp_t*));
259extern int gtp_set_cb_freepdp(struct gsn_t *gsn,
260 int (*cb) (struct pdp_t*));
261extern int gtp_set_cb_create_pdp_ind(struct gsn_t *gsn,
262 int (*cb) (struct pdp_t*));
263extern int gtp_set_cb_create_pdp_conf(struct gsn_t *gsn,
264 int (*cb) (struct pdp_t*, int));
265extern int gtp_set_cb_update_pdp_conf(struct gsn_t *gsn,
266 int (*cb) (struct pdp_t*, int, int));
267extern int gtp_set_cb_delete_pdp_ind(struct gsn_t *gsn,
268 int (*cb) (struct pdp_t*));
269extern int gtp_set_cb_delete_pdp_conf(struct gsn_t *gsn,
270 int (*cb) (struct pdp_t*, int));
271*/
272
273extern int gtp_set_cb_delete_context(struct gsn_t *gsn,
274 int (*cb_delete_context) (struct pdp_t* pdp));
275extern int gtp_set_cb_create_context(struct gsn_t *gsn,
276 int (*cb_create_context) (struct pdp_t* pdp));
277extern int gtp_set_cb_conf(struct gsn_t *gsn,
278 int (*cb) (int type, int cause, struct pdp_t* pdp, void *aid));
279extern int gtp_set_cb_gpdu(struct gsn_t *gsn,
280 int (*cb_gpdu) (struct pdp_t* pdp, void* pack, unsigned len));
281
282
283/* Internal functions (not part of the API */
284
285extern int gtp_echo_req(struct gsn_t *gsn, struct in_addr *inetaddrs);
286extern int gtp_echo_resp(struct gsn_t *gsn, struct sockaddr_in *peer,
287 void *pack, unsigned len);
288extern int gtp_echo_ind(struct gsn_t *gsn, struct sockaddr_in *peer,
289 void *pack, unsigned len);
290extern int gtp_echo_conf(struct gsn_t *gsn, struct sockaddr_in *peer,
291 void *pack, unsigned len);
292
293extern int gtp_unsup_resp(struct gsn_t *gsn, struct sockaddr_in *peer,
294 void *pack, unsigned len);
295extern int gtp_unsup_conf(struct gsn_t *gsn, struct sockaddr_in *peer,
296 void *pack, unsigned len);
297
298extern int gtp_create_pdp_req(struct gsn_t *gsn, int version, void *aid,
299 struct in_addr* inetaddr, struct pdp_t *pdp);
300
301extern int gtp_create_pdp_resp(struct gsn_t *gsn, int version,
302 struct sockaddr_in *peer,
303 void *pack, unsigned len,
304 struct pdp_t *pdp, uint8_t cause);
305
306extern int gtp_create_pdp_ind(struct gsn_t *gsn, int version,
307 struct sockaddr_in *peer,
308 void *pack, unsigned len);
309
310extern int gtp_create_pdp_conf(struct gsn_t *gsn, int version,
311 struct sockaddr_in *peer,
312 void *pack, unsigned len);
313
314extern int gtp_update_pdp_req(struct gsn_t *gsn, int version, void *aid,
315 struct in_addr* inetaddr, struct pdp_t *pdp);
316
317extern int gtp_delete_pdp_req(struct gsn_t *gsn, int version, void *aid,
318 struct pdp_t *pdp);
319
320extern int gtp_delete_pdp_resp(struct gsn_t *gsn, int version,
321 struct sockaddr_in *peer,
322 void *pack, unsigned len,
323 struct pdp_t *pdp, uint8_t cause);
324
325extern int gtp_delete_pdp_ind(struct gsn_t *gsn, int version,
326 struct sockaddr_in *peer,
327 void *pack, unsigned len);
328
329extern int gtp_delete_pdp_conf(struct gsn_t *gsn, int version,
330 struct sockaddr_in *peer,
331 void *pack, unsigned len);
332
333
334extern int ipv42eua(struct ul66_t *eua, struct in_addr *src);
335extern int eua2ipv4(struct in_addr *dst, struct ul66_t *eua);
336extern int gsna2in_addr(struct in_addr *dst, struct ul16_t *gsna);
337extern int in_addr2gsna(struct ul16_t *gsna, struct in_addr *src);
338
339#endif /* !_GTP_H */