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