blob: fd2105fe4e56500469c3d9bb4d23cef5a7d16db2 [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/*
18 * gtp.c: Contains all GTP functionality. Should be able to handle multiple
19 * tunnels in the same program.
20 *
21 * TODO:
22 * - Do we need to handle fragmentation?
23 */
24
25
26#ifdef __linux__
27#define _GNU_SOURCE 1
28#endif
29
30
31#include <syslog.h>
32#include <stdio.h>
33#include <stdarg.h>
34#include <stdlib.h>
35#include <sys/time.h>
36#include <sys/types.h>
37#include <sys/socket.h>
38#include <netinet/in.h>
39#include <arpa/inet.h>
40#include <sys/stat.h>
41#include <time.h>
42#include <unistd.h>
43#include <string.h>
44#include <errno.h>
45#include <fcntl.h>
46
47#include <arpa/inet.h>
48
49#include <stdint.h> /* ISO C99 types */
50
jjako3c13e302003-01-28 22:17:29 +000051#include "../config.h"
jjako52c24142002-12-16 13:33:51 +000052#include "pdp.h"
53#include "gtp.h"
54#include "gtpie.h"
55#include "queue.h"
56
jjako1db1c812003-07-06 20:53:57 +000057
58/* Error reporting functions */
59
60void gtp_err(int priority, char *filename, int linenum, char *fmt, ...) {
61 va_list args;
62 char buf[ERRMSG_SIZE];
63
64 va_start(args, fmt);
65 vsnprintf(buf, ERRMSG_SIZE, fmt, args);
66 va_end(args);
67 buf[ERRMSG_SIZE-1] = 0;
68 syslog(priority, "%s: %d: %s", filename, linenum, buf);
69}
70
71void gtp_errpack(int pri, char *fn, int ln, struct sockaddr_in *peer,
72 void *pack, unsigned len, char *fmt, ...) {
73
74 va_list args;
75 char buf[ERRMSG_SIZE];
76 char buf2[ERRMSG_SIZE];
77 int n;
78 int pos;
79
80 va_start(args, fmt);
81 vsnprintf(buf, ERRMSG_SIZE, fmt, args);
82 va_end(args);
83 buf[ERRMSG_SIZE-1] = 0;
84
85 snprintf(buf2, ERRMSG_SIZE, "Packet from %s:%u, length: %d, content:",
86 inet_ntoa(peer->sin_addr),
87 ntohs(peer->sin_port),
88 len);
89 buf2[ERRMSG_SIZE-1] = 0;
90 pos = strlen(buf2);
91 for(n=0; n<len; n++) {
92 if ((pos+4)<ERRMSG_SIZE) {
93 sprintf((buf2+pos), " %02hhx", ((unsigned char*)pack)[n]);
94 pos += 3;
95 }
96 }
97 buf2[pos] = 0;
98
99 syslog(pri, "%s: %d: %s. %s", fn, ln, buf, buf2);
100
101}
102
103
104
105
jjako52c24142002-12-16 13:33:51 +0000106/* API Functions */
107
108const char* gtp_version()
109{
110 return VERSION;
111}
112
113/* gtp_new */
114/* gtp_free */
115
116int gtp_newpdp(struct gsn_t* gsn, struct pdp_t **pdp,
117 uint64_t imsi, uint8_t nsapi) {
118 return pdp_newpdp(pdp, imsi, nsapi, NULL);
119}
120
121int gtp_freepdp(struct gsn_t* gsn, struct pdp_t *pdp) {
122 return pdp_freepdp(pdp);
123}
124
jjako52c24142002-12-16 13:33:51 +0000125/* gtp_gpdu */
126
127extern int gtp_fd(struct gsn_t *gsn) {
jjako08d331d2003-10-13 20:33:30 +0000128 return gsn->fd0;
jjako52c24142002-12-16 13:33:51 +0000129}
130
131/* gtp_decaps */
132/* gtp_retrans */
133/* gtp_retranstimeout */
134
jjako08d331d2003-10-13 20:33:30 +0000135
136int gtp_set_cb_unsup_ind(struct gsn_t *gsn,
137 int (*cb) (struct sockaddr_in *peer)) {
138 gsn->cb_unsup_ind = cb;
139 return 0;
140}
141
jjako2c381332003-10-21 19:09:53 +0000142int gtp_set_cb_extheader_ind(struct gsn_t *gsn,
143 int (*cb) (struct sockaddr_in *peer)) {
144 gsn->cb_extheader_ind = cb;
145 return 0;
146}
147
jjako08d331d2003-10-13 20:33:30 +0000148
149/* API: Initialise delete context callback */
150/* Called whenever a pdp context is deleted for any reason */
jjako52c24142002-12-16 13:33:51 +0000151int gtp_set_cb_delete_context(struct gsn_t *gsn,
jjako08d331d2003-10-13 20:33:30 +0000152 int (*cb) (struct pdp_t* pdp))
jjako52c24142002-12-16 13:33:51 +0000153{
jjako08d331d2003-10-13 20:33:30 +0000154 gsn->cb_delete_context = cb;
jjako52c24142002-12-16 13:33:51 +0000155 return 0;
156}
157
jjako52c24142002-12-16 13:33:51 +0000158int gtp_set_cb_conf(struct gsn_t *gsn,
159 int (*cb) (int type, int cause,
jjako08d331d2003-10-13 20:33:30 +0000160 struct pdp_t* pdp, void *cbp)) {
jjako52c24142002-12-16 13:33:51 +0000161 gsn->cb_conf = cb;
162 return 0;
163}
164
jjako08d331d2003-10-13 20:33:30 +0000165extern int gtp_set_cb_data_ind(struct gsn_t *gsn,
166 int (*cb_data_ind) (struct pdp_t* pdp,
jjako52c24142002-12-16 13:33:51 +0000167 void* pack,
168 unsigned len))
169{
jjako08d331d2003-10-13 20:33:30 +0000170 gsn->cb_data_ind = cb_data_ind;
jjako52c24142002-12-16 13:33:51 +0000171 return 0;
172}
173
jjako08d331d2003-10-13 20:33:30 +0000174/**
175 * get_default_gtp()
176 * Generate a GPRS Tunneling Protocol signalling packet header, depending
177 * on GTP version and message type. pdp is used for teid/flow label.
178 * *packet must be allocated by the calling function, and be large enough
179 * to hold the packet header.
180 * returns the length of the header. 0 on error.
181 **/
182static int get_default_gtp(int version, u_int8_t type, void *packet) {
jjakoa7cd2492003-04-11 09:40:12 +0000183 struct gtp0_header *gtp0_default = (struct gtp0_header*) packet;
184 struct gtp1_header_long *gtp1_default = (struct gtp1_header_long*) packet;
jjako52c24142002-12-16 13:33:51 +0000185 switch (version) {
186 case 0:
jjakoa7cd2492003-04-11 09:40:12 +0000187 /* Initialise "standard" GTP0 header */
jjako08d331d2003-10-13 20:33:30 +0000188 memset(gtp0_default, 0, sizeof(struct gtp0_header));
jjakoa7cd2492003-04-11 09:40:12 +0000189 gtp0_default->flags=0x1e;
jjako08d331d2003-10-13 20:33:30 +0000190 gtp0_default->type=hton8(type);
jjakoa7cd2492003-04-11 09:40:12 +0000191 gtp0_default->spare1=0xff;
192 gtp0_default->spare2=0xff;
193 gtp0_default->spare3=0xff;
194 gtp0_default->number=0xff;
jjako08d331d2003-10-13 20:33:30 +0000195 return GTP0_HEADER_SIZE;
jjako52c24142002-12-16 13:33:51 +0000196 case 1:
jjakoa7cd2492003-04-11 09:40:12 +0000197 /* Initialise "standard" GTP1 header */
jjako08d331d2003-10-13 20:33:30 +0000198 /* 29.060: 8.2: S=1 and PN=0 */
199 /* 29.060 9.3.1: For GTP-U messages Echo Request, Echo Response */
200 /* and Supported Extension Headers Notification, the S field shall be */
201 /* set to 1 */
202 /* Currently extension headers are not supported */
203 memset(gtp1_default, 0, sizeof(struct gtp1_header_long));
204 gtp1_default->flags=0x32; /* No extension, enable sequence, no N-PDU */
205 gtp1_default->type=hton8(type);
206 return GTP1_HEADER_SIZE_LONG;
207 default:
208 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown GTP packet version");
209 return 0;
jjako52c24142002-12-16 13:33:51 +0000210 }
211}
212
jjako08d331d2003-10-13 20:33:30 +0000213/**
214 * get_seq()
215 * Get sequence number of a packet.
216 * Returns 0 on error
217 **/
218static uint16_t get_seq(void *pack) {
219 union gtp_packet *packet = (union gtp_packet *) pack;
220
221 if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */
222 return ntoh16(packet->gtp0.h.seq);
223 }
224 else if ((packet->flags & 0xe2) == 0x22) { /* Version 1 with seq */
225 return ntoh16(packet->gtp1l.h.seq);
226 } else {
227 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown packet flag");
228 return 0;
229 }
230}
231
232/**
233 * get_tid()
234 * Get tunnel identifier of a packet.
235 * Returns 0 on error
236 **/
237static uint64_t get_tid(void *pack) {
238 union gtp_packet *packet = (union gtp_packet *) pack;
239
240 if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */
241 return packet->gtp0.h.tid;
242 }
243 return 0;
244}
245
246/**
247 * get_hlen()
248 * Get the header length of a packet.
249 * Returns 0 on error
250 **/
251static uint16_t get_hlen(void *pack) {
252 union gtp_packet *packet = (union gtp_packet *) pack;
253
254 if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */
255 return GTP0_HEADER_SIZE;
256 }
257 else if ((packet->flags & 0xe2) == 0x22) { /* Version 1 with seq */
258 return GTP1_HEADER_SIZE_LONG;
259 }
260 else if ((packet->flags & 0xe7) == 0x20) { /* Short version 1 */
261 return GTP1_HEADER_SIZE_SHORT;
262 } else {
263 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown packet flag");
264 return 0;
265 }
266}
267
268/**
269 * get_tei()
270 * Get the tunnel endpoint identifier (flow label) of a packet.
271 * Returns 0xffffffff on error.
272 **/
273static uint32_t get_tei(void *pack) {
274 union gtp_packet *packet = (union gtp_packet *) pack;
275
276 if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */
277 return ntoh16(packet->gtp0.h.flow);
278 }
279 else if ((packet->flags & 0xe0) == 0x20) { /* Version 1 */
280 return ntoh32(packet->gtp1l.h.tei);
281 }
282 else {
283 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown packet flag");
284 return 0xffffffff;
285 }
286}
jjakoa7cd2492003-04-11 09:40:12 +0000287
288
jjako52c24142002-12-16 13:33:51 +0000289int print_packet(void *packet, unsigned len)
290{
291 int i;
292 printf("The packet looks like this (%d bytes):\n", len);
293 for( i=0; i<len; i++) {
294 printf("%02x ", (unsigned char)*(char *)(packet+i));
295 if (!((i+1)%16)) printf("\n");
296 };
297 printf("\n");
298 return 0;
299}
300
301char* snprint_packet(struct gsn_t *gsn, struct sockaddr_in *peer,
302 void *pack, unsigned len, char *buf, int size) {
303 int n;
304 int pos;
305 snprintf(buf, size, "Packet from %s:%u, length: %d, content:",
306 inet_ntoa(peer->sin_addr),
307 ntohs(peer->sin_port),
308 len);
jjako2e840a32003-01-28 16:05:18 +0000309 buf[size-1] = 0;
jjako52c24142002-12-16 13:33:51 +0000310 pos = strlen(buf);
311 for(n=0; n<len; n++) {
312 if ((pos+4)<size) {
313 sprintf((buf+pos), " %02hhx", ((unsigned char*)pack)[n]);
314 pos += 3;
315 }
316 }
317 buf[pos] = 0;
318 return buf;
319}
320
jjako52c24142002-12-16 13:33:51 +0000321
322/* ***********************************************************
323 * Reliable delivery of signalling messages
324 *
325 * Sequence numbers are used for both signalling messages and
326 * data messages.
327 *
328 * For data messages each tunnel maintains a sequence counter,
329 * which is incremented by one each time a new data message
330 * is sent. The sequence number starts at (0) zero at tunnel
331 * establishment, and wraps around at 65535 (29.060 9.3.1.1
332 * and 09.60 8.1.1.1). The sequence numbers are either ignored,
333 * or can be used to check the validity of the message in the
334 * receiver, or for reordering af packets.
335 *
336 * For signalling messages the sequence number is used by
337 * signalling messages for which a response is defined. A response
338 * message should copy the sequence from the corresponding request
339 * message. The sequence number "unambiguously" identifies a request
340 * message within a given path, with a path being defined as a set of
341 * two endpoints (29.060 8.2, 29.060 7.6, 09.60 7.8). "All request
342 * messages shall be responded to, and all response messages associated
343 * with a certain request shall always include the same information"
344 *
345 * We take this to mean that the GSN transmitting a request is free to
346 * choose the sequence number, as long as it is unique within a given path.
347 * It means that we are allowed to count backwards, or roll over at 17
348 * if we prefer that. It also means that we can use the same counter for
349 * all paths. This has the advantage that the transmitted request sequence
350 * numbers are unique within each GSN, and also we dont have to mess around
351 * with path setup and teardown.
352 *
353 * If a response message is lost, the request will be retransmitted, and
354 * the receiving GSN will receive a "duplicated" request. The standard
355 * requires the receiving GSN to send a response, with the same information
356 * as in the original response. For most messages this happens automatically:
357 *
358 * Echo: Automatically dublicates the original response
359 * Create pdp context: The SGSN may send create context request even if
360 * a context allready exist (imsi+nsapi?). This means that the reply will
361 automatically dublicate the original response. It might however have
jjako08d331d2003-10-13 20:33:30 +0000362 * side effects in the application which is asked twice to validate
363 * the login.
jjako52c24142002-12-16 13:33:51 +0000364 * Update pdp context: Automatically dublicates the original response???
365 * Delete pdp context. Automatically in gtp0, but in gtp1 will generate
366 * a nonexist reply message.
367 *
368 * The correct solution will be to make a queue containing response messages.
369 * This queue should be checked whenever a request is received. If the
370 * response is allready in the queue that response should be transmitted.
371 * It should be possible to find messages in this queue on the basis of
372 * the sequence number and peer GSN IP address (The sequense number is unique
373 * within each path). This need to be implemented by a hash table. Furthermore
374 * it should be possibly to delete messages based on a timeout. This can be
375 * achieved by means of a linked list. The timeout value need to be larger
376 * than T3-RESPONSE * N3-REQUESTS (recommended value 5). These timers are
377 * set in the peer GSN, so there is no way to know these parameters. On the
378 * other hand the timeout value need to be so small that we do not receive
379 * wraparound sequence numbere before the message is deleted. 60 seconds is
380 * probably not a bad choise.
381 *
382 * This queue however is first really needed from gtp1.
383 *
384 * gtp_req:
385 * Send off a signalling message with appropiate sequence
386 * number. Store packet in queue.
387 * gtp_conf:
388 * Remove an incoming confirmation from the queue
389 * gtp_resp:
jjako08d331d2003-10-13 20:33:30 +0000390 * Send off a response to a request. Use the same sequence
jjako52c24142002-12-16 13:33:51 +0000391 * number in the response as in the request.
jjako2c381332003-10-21 19:09:53 +0000392 * gtp_notification:
393 * Send off a notification message. This is neither a request nor
394 * a response. Both TEI and SEQ are zero.
jjako52c24142002-12-16 13:33:51 +0000395 * gtp_retrans:
396 * Retransmit any outstanding packets which have exceeded
397 * a predefined timeout.
398 *************************************************************/
399
jjako08d331d2003-10-13 20:33:30 +0000400int gtp_req(struct gsn_t *gsn, int version, struct pdp_t *pdp,
401 union gtp_packet *packet, int len,
402 struct in_addr *inetaddr, void *cbp) {
jjako52c24142002-12-16 13:33:51 +0000403 struct sockaddr_in addr;
404 struct qmsg_t *qmsg;
jjako08d331d2003-10-13 20:33:30 +0000405 int fd;
406
jjako52c24142002-12-16 13:33:51 +0000407 memset(&addr, 0, sizeof(addr));
408 addr.sin_family = AF_INET;
409 addr.sin_addr = *inetaddr;
jjako52c24142002-12-16 13:33:51 +0000410
jjako08d331d2003-10-13 20:33:30 +0000411 if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */
412 addr.sin_port = htons(GTP0_PORT);
413 packet->gtp0.h.length = hton16(len - GTP0_HEADER_SIZE);
414 packet->gtp0.h.seq = hton16(gsn->seq_next);
415 if (pdp)
416 packet->gtp0.h.tid = (pdp->imsi & 0x0fffffffffffffff) +
417 ((uint64_t)pdp->nsapi << 60);
418 if (pdp && ((packet->gtp0.h.type == GTP_GPDU) ||
419 (packet->gtp0.h.type == GTP_ERROR)))
420 packet->gtp0.h.flow=hton16(pdp->flru);
421 else if (pdp)
422 packet->gtp0.h.flow=hton16(pdp->flrc);
423 fd = gsn->fd0;
424 }
425 else if ((packet->flags & 0xe2) == 0x22) { /* Version 1 with seq */
426 addr.sin_port = htons(GTP1C_PORT);
427 packet->gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT);
428 packet->gtp1l.h.seq = hton16(gsn->seq_next);
429 if (pdp && ((packet->gtp1l.h.type == GTP_GPDU) ||
430 (packet->gtp1l.h.type == GTP_ERROR)))
431 packet->gtp1l.h.tei=hton32(pdp->teid_gn);
432 else if (pdp)
433 packet->gtp1l.h.tei=hton32(pdp->teic_gn);
434 fd = gsn->fd1c;
435 } else {
436 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown packet flag");
437 return -1;
438 }
jjako52c24142002-12-16 13:33:51 +0000439
jjako08d331d2003-10-13 20:33:30 +0000440 if (sendto(fd, packet, len, 0,
jjako52c24142002-12-16 13:33:51 +0000441 (struct sockaddr *) &addr, sizeof(addr)) < 0) {
442 gsn->err_sendto++;
jjako08d331d2003-10-13 20:33:30 +0000443 gtp_err(LOG_ERR, __FILE__, __LINE__, "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s", fd, (unsigned long) &packet, len, strerror(errno));
jjako52c24142002-12-16 13:33:51 +0000444 return -1;
445 }
446
447 /* Use new queue structure */
448 if (queue_newmsg(gsn->queue_req, &qmsg, &addr, gsn->seq_next)) {
449 gsn->err_queuefull++;
450 gtp_err(LOG_ERR, __FILE__, __LINE__, "Retransmit queue is full");
451 }
452 else {
453 memcpy(&qmsg->p, packet, sizeof(union gtp_packet));
454 qmsg->l = len;
455 qmsg->timeout = time(NULL) + 3; /* When to timeout */
456 qmsg->retrans = 0; /* No retransmissions so far */
jjako08d331d2003-10-13 20:33:30 +0000457 qmsg->cbp = cbp;
jjako52c24142002-12-16 13:33:51 +0000458 qmsg->type = ntoh8(packet->gtp0.h.type);
jjako08d331d2003-10-13 20:33:30 +0000459 qmsg->fd = fd;
jjako52c24142002-12-16 13:33:51 +0000460 }
461 gsn->seq_next++; /* Count up this time */
462 return 0;
463}
464
465/* gtp_conf
466 * Remove signalling packet from retransmission queue.
467 * return 0 on success, EOF if packet was not found */
468
469int gtp_conf(struct gsn_t *gsn, int version, struct sockaddr_in *peer,
jjako08d331d2003-10-13 20:33:30 +0000470 union gtp_packet *packet, int len, uint8_t *type, void **cbp) {
jjako52c24142002-12-16 13:33:51 +0000471
jjako08d331d2003-10-13 20:33:30 +0000472 uint16_t seq;
473
474 if ((packet->gtp0.h.flags & 0xe0) == 0x00)
475 seq = ntoh16(packet->gtp0.h.seq);
476 else if ((packet->gtp1l.h.flags & 0xe2) == 0x22)
477 seq = ntoh16(packet->gtp1l.h.seq);
478 else {
479 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, packet, len,
480 "Unknown GTP packet version");
481 return EOF;
482 }
483
484 if (queue_freemsg_seq(gsn->queue_req, peer, seq, type, cbp)) {
jjako52c24142002-12-16 13:33:51 +0000485 gsn->err_seq++;
486 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, packet, len,
487 "Confirmation packet not found in queue");
488 return EOF;
489 }
490
491 return 0;
492}
493
494int gtp_retrans(struct gsn_t *gsn) {
495 /* Retransmit any outstanding packets */
496 /* Remove from queue if maxretrans exceeded */
497 time_t now;
498 struct qmsg_t *qmsg;
499 now = time(NULL);
500 /*printf("Retrans: New beginning %d\n", (int) now);*/
501
502 while ((!queue_getfirst(gsn->queue_req, &qmsg)) &&
503 (qmsg->timeout <= now)) {
504 /*printf("Retrans timeout found: %d\n", (int) time(NULL));*/
505 if (qmsg->retrans > 3) { /* To many retrans */
jjako08d331d2003-10-13 20:33:30 +0000506 if (gsn->cb_conf) gsn->cb_conf(qmsg->type, EOF, NULL, qmsg->cbp);
jjako52c24142002-12-16 13:33:51 +0000507 queue_freemsg(gsn->queue_req, qmsg);
508 }
509 else {
jjako08d331d2003-10-13 20:33:30 +0000510 if (sendto(qmsg->fd, &qmsg->p, qmsg->l, 0,
jjako52c24142002-12-16 13:33:51 +0000511 (struct sockaddr *) &qmsg->peer, sizeof(struct sockaddr_in)) < 0) {
512 gsn->err_sendto++;
jjako08d331d2003-10-13 20:33:30 +0000513 gtp_err(LOG_ERR, __FILE__, __LINE__, "Sendto(fd0=%d, msg=%lx, len=%d) failed: Error = %s", gsn->fd0, (unsigned long) &qmsg->p, qmsg->l, strerror(errno));
jjako52c24142002-12-16 13:33:51 +0000514 }
515 queue_back(gsn->queue_req, qmsg);
516 qmsg->timeout = now + 3;
517 qmsg->retrans++;
518 }
519 }
520
521 /* Also clean up reply timeouts */
522 while ((!queue_getfirst(gsn->queue_resp, &qmsg)) &&
523 (qmsg->timeout < now)) {
524 /*printf("Retrans (reply) timeout found: %d\n", (int) time(NULL));*/
525 queue_freemsg(gsn->queue_resp, qmsg);
526 }
527
528 return 0;
529}
530
531int gtp_retranstimeout(struct gsn_t *gsn, struct timeval *timeout) {
532 time_t now, later;
533 struct qmsg_t *qmsg;
534
535 if (queue_getfirst(gsn->queue_req, &qmsg)) {
536 timeout->tv_sec = 10;
537 timeout->tv_usec = 0;
538 }
539 else {
540 now = time(NULL);
541 later = qmsg->timeout;
542 timeout->tv_sec = later - now;
543 timeout->tv_usec = 0;
544 if (timeout->tv_sec < 0) timeout->tv_sec = 0; /* No negative allowed */
545 if (timeout->tv_sec > 10) timeout->tv_sec = 10; /* Max sleep for 10 sec*/
546 }
547 return 0;
548}
549
jjako08d331d2003-10-13 20:33:30 +0000550int gtp_resp(int version, struct gsn_t *gsn, struct pdp_t *pdp,
551 union gtp_packet *packet, int len,
552 struct sockaddr_in *peer, int fd,
553 uint16_t seq, uint64_t tid) {
jjako52c24142002-12-16 13:33:51 +0000554 struct qmsg_t *qmsg;
jjako52c24142002-12-16 13:33:51 +0000555
jjako08d331d2003-10-13 20:33:30 +0000556 if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */
557 packet->gtp0.h.length = hton16(len - GTP0_HEADER_SIZE);
558 packet->gtp0.h.seq = hton16(seq);
559 packet->gtp0.h.tid = tid;
560 if (pdp && ((packet->gtp0.h.type == GTP_GPDU) ||
561 (packet->gtp0.h.type == GTP_ERROR)))
562 packet->gtp0.h.flow=hton16(pdp->flru);
563 else if (pdp)
564 packet->gtp0.h.flow=hton16(pdp->flrc);
565 }
566 else if ((packet->flags & 0xe2) == 0x22) { /* Version 1 with seq */
567 packet->gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT);
568 packet->gtp1l.h.seq = hton16(seq);
569 if (pdp && (fd == gsn->fd1u))
570 packet->gtp1l.h.tei=hton32(pdp->teid_gn);
571 else if (pdp)
572 packet->gtp1l.h.tei=hton32(pdp->teic_gn);
573 }
574 else {
575 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown packet flag");
jjakoa7cd2492003-04-11 09:40:12 +0000576 return -1;
577 }
jjako52c24142002-12-16 13:33:51 +0000578
jjako08d331d2003-10-13 20:33:30 +0000579 if (fcntl(fd, F_SETFL, 0)) {
580 gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()");
581 return -1;
582 }
583
584 if (sendto(fd, packet, len, 0,
jjako52c24142002-12-16 13:33:51 +0000585 (struct sockaddr *) peer, sizeof(struct sockaddr_in)) < 0) {
586 gsn->err_sendto++;
jjako08d331d2003-10-13 20:33:30 +0000587 gtp_err(LOG_ERR, __FILE__, __LINE__, "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s", fd, (unsigned long) &packet, len, strerror(errno));
jjako52c24142002-12-16 13:33:51 +0000588 return -1;
589 }
590
591 /* Use new queue structure */
592 if (queue_newmsg(gsn->queue_resp, &qmsg, peer, seq)) {
593 gsn->err_queuefull++;
594 gtp_err(LOG_ERR, __FILE__, __LINE__, "Retransmit queue is full");
595 }
596 else {
597 memcpy(&qmsg->p, packet, sizeof(union gtp_packet));
598 qmsg->l = len;
599 qmsg->timeout = time(NULL) + 60; /* When to timeout */
600 qmsg->retrans = 0; /* No retransmissions so far */
jjako08d331d2003-10-13 20:33:30 +0000601 qmsg->cbp = NULL;
jjako52c24142002-12-16 13:33:51 +0000602 qmsg->type = 0;
jjako08d331d2003-10-13 20:33:30 +0000603 qmsg->fd = fd;
jjako52c24142002-12-16 13:33:51 +0000604 }
605 return 0;
606}
607
jjako2c381332003-10-21 19:09:53 +0000608int gtp_notification(struct gsn_t *gsn, int version,
609 union gtp_packet *packet, int len,
610 struct sockaddr_in *peer, int fd,
611 uint16_t seq) {
612
613 struct sockaddr_in addr;
614
615 memcpy(&addr, peer, sizeof(addr));
616
617 /* In GTP0 notifications are treated as replies. In GTP1 they
618 are requests for which there is no reply */
619
620 if (fd == gsn->fd1c)
621 addr.sin_port = htons(GTP1C_PORT);
622 else if (fd == gsn->fd1u)
623 addr.sin_port = htons(GTP1C_PORT);
624
625 if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */
626 packet->gtp0.h.length = hton16(len - GTP0_HEADER_SIZE);
627 packet->gtp0.h.seq = hton16(seq);
628 }
629 else if ((packet->flags & 0xe2) == 0x22) { /* Version 1 with seq */
630 packet->gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT);
631 packet->gtp1l.h.seq = hton16(seq);
632 }
633 else {
634 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown packet flag");
635 return -1;
636 }
637
638 if (fcntl(fd, F_SETFL, 0)) {
639 gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()");
640 return -1;
641 }
642
643 if (sendto(fd, packet, len, 0,
644 (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0) {
645 gsn->err_sendto++;
646 gtp_err(LOG_ERR, __FILE__, __LINE__, "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s", fd, (unsigned long) &packet, len, strerror(errno));
647 return -1;
648 }
649 return 0;
650}
651
jjako52c24142002-12-16 13:33:51 +0000652int gtp_dublicate(struct gsn_t *gsn, int version,
653 struct sockaddr_in *peer, uint16_t seq) {
654 struct qmsg_t *qmsg;
655
656 if(queue_seqget(gsn->queue_resp, &qmsg, peer, seq)) {
657 return EOF; /* Notfound */
658 }
jjakoa7cd2492003-04-11 09:40:12 +0000659
jjako08d331d2003-10-13 20:33:30 +0000660 if (fcntl(qmsg->fd, F_SETFL, 0)) {
661 gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()");
662 return -1;
jjako52c24142002-12-16 13:33:51 +0000663 }
jjako08d331d2003-10-13 20:33:30 +0000664
665 if (sendto(qmsg->fd, &qmsg->p, qmsg->l, 0,
666 (struct sockaddr *) peer, sizeof(struct sockaddr_in)) < 0) {
667 gsn->err_sendto++;
668 gtp_err(LOG_ERR, __FILE__, __LINE__, "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s", qmsg->fd, (unsigned long) &qmsg->p, qmsg->l, strerror(errno));
669 }
670 return 0;
jjako52c24142002-12-16 13:33:51 +0000671}
672
673
674
675/* Perform restoration and recovery error handling as described in 29.060 */
676static void log_restart(struct gsn_t *gsn) {
677 FILE *f;
678 int i;
679 int counter = 0;
680 char filename[NAMESIZE];
681
682 filename[NAMESIZE-1] = 0; /* No null term. guarantee by strncpy */
683 strncpy(filename, gsn->statedir, NAMESIZE-1);
684 strncat(filename, RESTART_FILE,
685 NAMESIZE-1-sizeof(RESTART_FILE));
686
687 i = umask(022);
688
689 /* We try to open file. On failure we will later try to create file */
690 if (!(f = fopen(filename, "r"))) {
jjako581c9f02003-10-22 11:28:20 +0000691
692 gtp_err(LOG_ERR, __FILE__, __LINE__, "State information file (%s) not found. Creating new file.", filename);
jjako52c24142002-12-16 13:33:51 +0000693 }
694 else {
695 umask(i);
696 fscanf(f, "%d", &counter);
697 if (fclose(f)) {
698 gtp_err(LOG_ERR, __FILE__, __LINE__, "fclose failed: Error = %s", strerror(errno));
699 }
700 }
701
702 gsn->restart_counter = (unsigned char) counter;
703 gsn->restart_counter++;
704
705 if (!(f = fopen(filename, "w"))) {
706 gtp_err(LOG_ERR, __FILE__, __LINE__, "fopen(path=%s, mode=%s) failed: Error = %s", filename, "w", strerror(errno));
707 return;
708 }
709
710 umask(i);
711 fprintf(f, "%d\n", gsn->restart_counter);
712 if (fclose(f)) {
713 gtp_err(LOG_ERR, __FILE__, __LINE__, "fclose failed: Error = %s", strerror(errno));
714 return;
715 }
716}
717
718
719
jjako1db1c812003-07-06 20:53:57 +0000720int gtp_new(struct gsn_t **gsn, char *statedir, struct in_addr *listen,
721 int mode)
jjako52c24142002-12-16 13:33:51 +0000722{
723 struct sockaddr_in addr;
jjako52c24142002-12-16 13:33:51 +0000724
725 syslog(LOG_ERR, "GTP: gtp_newgsn() started");
726
727 *gsn = calloc(sizeof(struct gsn_t), 1); /* TODO */
728
729 (*gsn)->statedir = statedir;
730 log_restart(*gsn);
jjakoa7cd2492003-04-11 09:40:12 +0000731
732 /* Initialise sequence number */
733 (*gsn)->seq_next = (*gsn)->restart_counter * 1024;
jjako52c24142002-12-16 13:33:51 +0000734
735 /* Initialise request retransmit queue */
736 queue_new(&(*gsn)->queue_req);
737 queue_new(&(*gsn)->queue_resp);
738
739 /* Initialise pdp table */
740 pdp_init();
741
742 /* Initialise call back functions */
jjako08d331d2003-10-13 20:33:30 +0000743 (*gsn)->cb_create_context_ind = 0;
jjako52c24142002-12-16 13:33:51 +0000744 (*gsn)->cb_delete_context = 0;
jjako08d331d2003-10-13 20:33:30 +0000745 (*gsn)->cb_unsup_ind = 0;
jjako52c24142002-12-16 13:33:51 +0000746 (*gsn)->cb_conf = 0;
jjako08d331d2003-10-13 20:33:30 +0000747 (*gsn)->cb_data_ind = 0;
jjako52c24142002-12-16 13:33:51 +0000748
jjako08d331d2003-10-13 20:33:30 +0000749 /* Store function parameters */
750 (*gsn)->gsnc = *listen;
751 (*gsn)->gsnu = *listen;
752 (*gsn)->mode = mode;
753
754
755 /* Create GTP version 0 socket */
756 if (((*gsn)->fd0 = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
jjako52c24142002-12-16 13:33:51 +0000757 (*gsn)->err_socket++;
758 gtp_err(LOG_ERR, __FILE__, __LINE__, "socket(domain=%d, type=%d, protocol=%d) failed: Error = %s", AF_INET, SOCK_DGRAM, 0, strerror(errno));
759 return -1;
760 }
jjako52c24142002-12-16 13:33:51 +0000761
762 memset(&addr, 0, sizeof(addr));
jjako52c24142002-12-16 13:33:51 +0000763 addr.sin_family = AF_INET;
jjako52c24142002-12-16 13:33:51 +0000764 addr.sin_addr = *listen; /* Same IP for user traffic and signalling*/
765 addr.sin_port = htons(GTP0_PORT);
766
jjako08d331d2003-10-13 20:33:30 +0000767 if (bind((*gsn)->fd0, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
jjako52c24142002-12-16 13:33:51 +0000768 (*gsn)->err_socket++;
jjako08d331d2003-10-13 20:33:30 +0000769 gtp_err(LOG_ERR, __FILE__, __LINE__, "bind(fd0=%d, addr=%lx, len=%d) failed: Error = %s", (*gsn)->fd0, (unsigned long) &addr, sizeof(addr), strerror(errno));
770 return -1;
771 }
772
773 /* Create GTP version 1 control plane socket */
774 if (((*gsn)->fd1c = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
775 (*gsn)->err_socket++;
776 gtp_err(LOG_ERR, __FILE__, __LINE__, "socket(domain=%d, type=%d, protocol=%d) failed: Error = %s", AF_INET, SOCK_DGRAM, 0, strerror(errno));
777 return -1;
778 }
779
780 memset(&addr, 0, sizeof(addr));
781 addr.sin_family = AF_INET;
782 addr.sin_addr = *listen; /* Same IP for user traffic and signalling*/
783 addr.sin_port = htons(GTP1C_PORT);
784
785 if (bind((*gsn)->fd1c, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
786 (*gsn)->err_socket++;
787 gtp_err(LOG_ERR, __FILE__, __LINE__, "bind(fd1c=%d, addr=%lx, len=%d) failed: Error = %s", (*gsn)->fd1c, (unsigned long) &addr, sizeof(addr), strerror(errno));
788 return -1;
789 }
790
791 /* Create GTP version 1 user plane socket */
792 if (((*gsn)->fd1u = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
793 (*gsn)->err_socket++;
794 gtp_err(LOG_ERR, __FILE__, __LINE__, "socket(domain=%d, type=%d, protocol=%d) failed: Error = %s", AF_INET, SOCK_DGRAM, 0, strerror(errno));
795 return -1;
796 }
797
798 memset(&addr, 0, sizeof(addr));
799 addr.sin_family = AF_INET;
800 addr.sin_addr = *listen; /* Same IP for user traffic and signalling*/
801 addr.sin_port = htons(GTP1U_PORT);
802
803 if (bind((*gsn)->fd1u, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
804 (*gsn)->err_socket++;
805 gtp_err(LOG_ERR, __FILE__, __LINE__, "bind(fd1c=%d, addr=%lx, len=%d) failed: Error = %s", (*gsn)->fd1c, (unsigned long) &addr, sizeof(addr), strerror(errno));
jjako52c24142002-12-16 13:33:51 +0000806 return -1;
807 }
808
jjako52c24142002-12-16 13:33:51 +0000809 return 0;
810}
811
812int gtp_free(struct gsn_t *gsn) {
813
814 /* Clean up retransmit queues */
815 queue_free(gsn->queue_req);
816 queue_free(gsn->queue_resp);
jjako08d331d2003-10-13 20:33:30 +0000817
818 close(gsn->fd0);
819 close(gsn->fd1c);
820 close(gsn->fd1u);
jjako52c24142002-12-16 13:33:51 +0000821
822 free(gsn);
823 return 0;
824}
825
826/* ***********************************************************
827 * Path management messages
828 * Messages: echo and version not supported.
829 * A path is connection between two UDP/IP endpoints
830 *
831 * A path is either using GTP0 or GTP1. A path can be
832 * established by any kind of GTP message??
833
834 * Which source port to use?
835 * GTP-C request destination port is 2123/3386
836 * GTP-U request destination port is 2152/3386
837 * T-PDU destination port is 2152/3386.
838 * For the above messages the source port is locally allocated.
839 * For response messages src=rx-dst and dst=rx-src.
840 * For simplicity we should probably use 2123+2152/3386 as
841 * src port even for the cases where src can be locally
842 * allocated. This also means that we have to listen only to
843 * the same ports.
844 * For response messages we need to be able to respond to
845 * the relevant src port even if it is locally allocated by
846 * the peer.
847 *
848 * The need for path management!
849 * We might need to keep a list of active paths. This might
850 * be in the form of remote IP address + UDP port numbers.
851 * (We will consider a path astablished if we have a context
852 * with the node in question)
853 *************************************************************/
854
855/* Send off an echo request */
jjako08d331d2003-10-13 20:33:30 +0000856int gtp_echo_req(struct gsn_t *gsn, int version, void *cbp,
857 struct in_addr *inetaddr)
jjako52c24142002-12-16 13:33:51 +0000858{
859 union gtp_packet packet;
jjako08d331d2003-10-13 20:33:30 +0000860 int length = get_default_gtp(version, GTP_ECHO_REQ, &packet);
861 return gtp_req(gsn, version, NULL, &packet, length, inetaddr, cbp);
jjako52c24142002-12-16 13:33:51 +0000862}
863
jjako08d331d2003-10-13 20:33:30 +0000864/* Send off an echo reply */
865int gtp_echo_resp(struct gsn_t *gsn, int version,
866 struct sockaddr_in *peer, int fd,
jjako52c24142002-12-16 13:33:51 +0000867 void *pack, unsigned len)
868{
869 union gtp_packet packet;
jjako08d331d2003-10-13 20:33:30 +0000870 int length = get_default_gtp(version, GTP_ECHO_RSP, &packet);
871 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, gsn->restart_counter);
872 return gtp_resp(version, gsn, NULL, &packet, length, peer, fd,
873 get_seq(pack), get_tid(pack));
jjako52c24142002-12-16 13:33:51 +0000874}
875
876
877/* Handle a received echo request */
jjako08d331d2003-10-13 20:33:30 +0000878int gtp_echo_ind(struct gsn_t *gsn, int version, struct sockaddr_in *peer,
879 int fd, void *pack, unsigned len) {
jjako52c24142002-12-16 13:33:51 +0000880
jjako08d331d2003-10-13 20:33:30 +0000881 /* Check if it was a dublicate request */
882 if(!gtp_dublicate(gsn, 0, peer, get_seq(pack))) return 0;
jjako52c24142002-12-16 13:33:51 +0000883
jjako08d331d2003-10-13 20:33:30 +0000884 /* Send off reply to request */
885 return gtp_echo_resp(gsn, version, peer, fd, pack, len);
jjako52c24142002-12-16 13:33:51 +0000886}
887
888/* Handle a received echo reply */
jjako08d331d2003-10-13 20:33:30 +0000889int gtp_echo_conf(struct gsn_t *gsn, int version, struct sockaddr_in *peer,
jjako52c24142002-12-16 13:33:51 +0000890 void *pack, unsigned len) {
891 union gtpie_member *ie[GTPIE_SIZE];
892 unsigned char recovery;
jjako08d331d2003-10-13 20:33:30 +0000893 void *cbp = NULL;
jjako52c24142002-12-16 13:33:51 +0000894 uint8_t type = 0;
jjako08d331d2003-10-13 20:33:30 +0000895 int hlen = get_hlen(pack);
jjako52c24142002-12-16 13:33:51 +0000896
897 /* Remove packet from queue */
jjako08d331d2003-10-13 20:33:30 +0000898 if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) return EOF;
jjako52c24142002-12-16 13:33:51 +0000899
jjako08d331d2003-10-13 20:33:30 +0000900 /* Extract information elements into a pointer array */
901 if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) {
jjako52c24142002-12-16 13:33:51 +0000902 gsn->invalid++;
903 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
904 "Invalid message format");
jjako08d331d2003-10-13 20:33:30 +0000905 if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +0000906 return EOF;
907 }
908
909 if (gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) {
910 gsn->missing++;
911 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
912 "Missing mandatory field");
jjako08d331d2003-10-13 20:33:30 +0000913 if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +0000914 return EOF;
915 }
916
jjako08d331d2003-10-13 20:33:30 +0000917 /* Echo reply packages does not have a cause information element */
918 /* Instead we return the recovery number in the callback function */
919 if (gsn->cb_conf) gsn->cb_conf(type, recovery, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +0000920
921 return 0;
922}
923
924/* Send off a Version Not Supported message */
925/* This message is somewhat special in that it actually is a
926 * response to some other message with unsupported GTP version
927 * For this reason it has parameters like a response, and does
928 * its own message transmission. No signalling queue is used
929 * The reply is sent to the peer IP and peer UDP. This means that
930 * the peer will be receiving a GTP0 message on a GTP1 port!
931 * In practice however this will never happen as a GTP0 GSN will
932 * only listen to the GTP0 port, and therefore will never receive
933 * anything else than GTP0 */
934
jjako08d331d2003-10-13 20:33:30 +0000935int gtp_unsup_req(struct gsn_t *gsn, int version, struct sockaddr_in *peer,
936 int fd, void *pack, unsigned len)
jjako52c24142002-12-16 13:33:51 +0000937{
938 union gtp_packet packet;
jjako52c24142002-12-16 13:33:51 +0000939
jjako08d331d2003-10-13 20:33:30 +0000940 /* GTP 1 is the highest supported protocol */
jjako2c381332003-10-21 19:09:53 +0000941 int length = get_default_gtp(1, GTP_NOT_SUPPORTED, &packet);
942 return gtp_notification(gsn, version, &packet, length,
943 peer, fd, 0);
jjako52c24142002-12-16 13:33:51 +0000944}
945
946/* Handle a Version Not Supported message */
jjako08d331d2003-10-13 20:33:30 +0000947int gtp_unsup_ind(struct gsn_t *gsn, struct sockaddr_in *peer,
948 void *pack, unsigned len) {
jjako52c24142002-12-16 13:33:51 +0000949
jjako08d331d2003-10-13 20:33:30 +0000950 if (gsn->cb_unsup_ind) gsn->cb_unsup_ind(peer);
jjako52c24142002-12-16 13:33:51 +0000951
jjako52c24142002-12-16 13:33:51 +0000952 return 0;
953}
954
jjako2c381332003-10-21 19:09:53 +0000955/* Send off an Supported Extension Headers Notification */
956int gtp_extheader_req(struct gsn_t *gsn, int version, struct sockaddr_in *peer,
957 int fd, void *pack, unsigned len)
958{
959 union gtp_packet packet;
960 int length = get_default_gtp(version, GTP_SUPP_EXT_HEADER, &packet);
961
962 uint8_t pdcp_pdu = GTP_EXT_PDCP_PDU;
963
964 if (version < 1)
965 return 0;
966
967 /* We report back that we support only PDCP PDU headers */
968 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EXT_HEADER_T, sizeof(pdcp_pdu),
969 &pdcp_pdu);
970
971 return gtp_notification(gsn, version, &packet, length,
972 peer, fd, get_seq(pack));
973}
974
975/* Handle a Supported Extension Headers Notification */
976int gtp_extheader_ind(struct gsn_t *gsn, struct sockaddr_in *peer,
977 void *pack, unsigned len) {
978
979 if (gsn->cb_extheader_ind) gsn->cb_extheader_ind(peer);
980
981 return 0;
982}
983
984
jjako52c24142002-12-16 13:33:51 +0000985/* ***********************************************************
986 * Session management messages
987 * Messages: create, update and delete PDP context
988 *
989 * Information storage
990 * Information storage for each PDP context is defined in
991 * 23.060 section 13.3. Includes IMSI, MSISDN, APN, PDP-type,
992 * PDP-address (IP address), sequence numbers, charging ID.
993 * For the SGSN it also includes radio related mobility
994 * information.
995 *************************************************************/
996
jjako08d331d2003-10-13 20:33:30 +0000997/* API: Send Create PDP Context Request */
998extern int gtp_create_context_req(struct gsn_t *gsn, struct pdp_t *pdp,
999 void *cbp, struct in_addr* inetaddr) {
jjako52c24142002-12-16 13:33:51 +00001000 union gtp_packet packet;
jjako08d331d2003-10-13 20:33:30 +00001001 int length = get_default_gtp(pdp->version, GTP_CREATE_PDP_REQ, &packet);
jjako2c381332003-10-21 19:09:53 +00001002 struct pdp_t *linked_pdp = NULL;
jjako52c24142002-12-16 13:33:51 +00001003
jjako2c381332003-10-21 19:09:53 +00001004 /* TODO: Secondary PDP Context Activation Procedure */
1005 /* In secondary activation procedure the PDP context is identified
1006 by tei in the header. The following fields are omitted: Selection
1007 mode, IMSI, MSISDN, End User Address, Access Point Name and
1008 Protocol Configuration Options */
1009
1010 if (pdp->secondary) {
1011 if (pdp_getgtp1(&linked_pdp, pdp->teic_own)) {
1012 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown linked PDP context");
1013 return EOF;
1014 }
1015 }
1016
1017 if (pdp->version == 0) {
jjako08d331d2003-10-13 20:33:30 +00001018 gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0,
jjako52c24142002-12-16 13:33:51 +00001019 sizeof(pdp->qos_req0), pdp->qos_req0);
jjako2c381332003-10-21 19:09:53 +00001020 }
jjako52c24142002-12-16 13:33:51 +00001021
jjako2c381332003-10-21 19:09:53 +00001022 if (pdp->version == 1) {
1023 if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */
1024 gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_IMSI,
1025 sizeof(pdp->imsi), (uint8_t*) &pdp->imsi);
1026 }
jjako52c24142002-12-16 13:33:51 +00001027
jjako08d331d2003-10-13 20:33:30 +00001028 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY,
1029 gsn->restart_counter);
jjako2c381332003-10-21 19:09:53 +00001030
1031 if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */
1032 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_SELECTION_MODE,
1033 pdp->selmode);
jjako08d331d2003-10-13 20:33:30 +00001034
1035 if (pdp->version == 0) {
1036 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI,
1037 pdp->fllu);
1038 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C,
1039 pdp->fllc);
1040 }
1041
1042 if (pdp->version == 1) {
1043 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI,
1044 pdp->teid_own);
jjako2c381332003-10-21 19:09:53 +00001045
1046 if (!pdp->teic_confirmed)
1047 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C,
1048 pdp->teic_own);
jjako08d331d2003-10-13 20:33:30 +00001049 }
1050
1051 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI,
1052 pdp->nsapi);
1053
jjako08d331d2003-10-13 20:33:30 +00001054
1055 if (pdp->version == 1) {
jjako2c381332003-10-21 19:09:53 +00001056 if (pdp->secondary) /* Secondary PDP Context Activation Procedure */
1057 gtpie_tv1(packet.gtp1l.p, &length, GTP_MAX, GTPIE_NSAPI,
1058 linked_pdp->nsapi);
1059
jjako08d331d2003-10-13 20:33:30 +00001060 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_CHARGING_C,
1061 pdp->cch_pdp);
1062 }
1063
1064 /* TODO
1065 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_REF,
1066 pdp->traceref);
1067 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_TYPE,
1068 pdp->tracetype); */
1069
jjako2c381332003-10-21 19:09:53 +00001070 if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */
1071 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA,
1072 pdp->eua.l, pdp->eua.v);
1073
jjako08d331d2003-10-13 20:33:30 +00001074
jjako2c381332003-10-21 19:09:53 +00001075 if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */
1076 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_APN,
1077 pdp->apn_use.l, pdp->apn_use.v);
1078
1079 if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */
1080 if (pdp->pco_req.l)
1081 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_PCO,
1082 pdp->pco_req.l, pdp->pco_req.v);
jjako08d331d2003-10-13 20:33:30 +00001083
1084 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
1085 pdp->gsnlc.l, pdp->gsnlc.v);
1086 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
1087 pdp->gsnlu.l, pdp->gsnlu.v);
jjako2c381332003-10-21 19:09:53 +00001088
1089 if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */
1090 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_MSISDN,
1091 pdp->msisdn.l, pdp->msisdn.v);
jjako08d331d2003-10-13 20:33:30 +00001092
1093 if (pdp->version == 1)
1094 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE,
1095 pdp->qos_req.l, pdp->qos_req.v);
1096
1097
1098 if ((pdp->version == 1) && pdp->tft.l)
1099 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TFT,
1100 pdp->tft.l, pdp->tft.v);
1101
1102 if ((pdp->version == 1) && pdp->triggerid.l)
1103 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TRIGGER_ID,
1104 pdp->triggerid.l, pdp->triggerid.v);
1105
1106 if ((pdp->version == 1) && pdp->omcid.l)
1107 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_OMC_ID,
1108 pdp->omcid.l, pdp->omcid.v);
1109
1110 gtp_req(gsn, pdp->version, pdp, &packet, length, inetaddr, cbp);
jjako52c24142002-12-16 13:33:51 +00001111
1112 return 0;
1113}
1114
jjako08d331d2003-10-13 20:33:30 +00001115/* API: Application response to context indication */
1116int gtp_create_context_resp(struct gsn_t *gsn, struct pdp_t *pdp, int cause) {
1117
1118 /* Now send off a reply to the peer */
1119 gtp_create_pdp_resp(gsn, pdp->version, pdp, cause);
1120
1121 if (cause != GTPCAUSE_ACC_REQ) {
1122 pdp_freepdp(pdp);
1123 }
1124
1125 return 0;
1126}
1127
1128/* API: Register create context indication callback */
1129int gtp_set_cb_create_context_ind(struct gsn_t *gsn,
1130 int (*cb_create_context_ind) (struct pdp_t* pdp))
jjako52c24142002-12-16 13:33:51 +00001131{
jjako08d331d2003-10-13 20:33:30 +00001132 gsn->cb_create_context_ind = cb_create_context_ind;
1133 return 0;
1134}
1135
1136
1137/* Send Create PDP Context Response */
1138int gtp_create_pdp_resp(struct gsn_t *gsn, int version, struct pdp_t *pdp,
1139 uint8_t cause) {
jjako52c24142002-12-16 13:33:51 +00001140 union gtp_packet packet;
jjako08d331d2003-10-13 20:33:30 +00001141 int length = get_default_gtp(version, GTP_CREATE_PDP_RSP, &packet);
jjako52c24142002-12-16 13:33:51 +00001142
jjako08d331d2003-10-13 20:33:30 +00001143 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause);
jjako52c24142002-12-16 13:33:51 +00001144
1145 if (cause == GTPCAUSE_ACC_REQ) {
jjako08d331d2003-10-13 20:33:30 +00001146
1147 if (version == 0)
1148 gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0,
1149 sizeof(pdp->qos_neg0), pdp->qos_neg0);
1150
1151 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_REORDER,
jjako52c24142002-12-16 13:33:51 +00001152 pdp->reorder);
jjako08d331d2003-10-13 20:33:30 +00001153 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY,
jjako52c24142002-12-16 13:33:51 +00001154 gsn->restart_counter);
jjako08d331d2003-10-13 20:33:30 +00001155
1156 if (version == 0) {
1157 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI,
1158 pdp->fllu);
1159 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C,
1160 pdp->fllc);
1161 }
1162
1163 if (version == 1) {
1164 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI,
1165 pdp->teid_own);
1166 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C,
1167 pdp->teic_own);
1168 }
1169
jjako2c381332003-10-21 19:09:53 +00001170 /* TODO: We use teic_own as charging ID */
jjako08d331d2003-10-13 20:33:30 +00001171 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_CHARGING_ID,
jjako2c381332003-10-21 19:09:53 +00001172 pdp->teic_own);
1173
jjako08d331d2003-10-13 20:33:30 +00001174 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA,
jjako52c24142002-12-16 13:33:51 +00001175 pdp->eua.l, pdp->eua.v);
1176
1177 if (pdp->pco_neg.l) { /* Optional PCO */
jjako08d331d2003-10-13 20:33:30 +00001178 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_PCO,
jjako52c24142002-12-16 13:33:51 +00001179 pdp->pco_neg.l, pdp->pco_neg.v);
1180 }
1181
jjako08d331d2003-10-13 20:33:30 +00001182 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
jjako52c24142002-12-16 13:33:51 +00001183 pdp->gsnlc.l, pdp->gsnlc.v);
jjako08d331d2003-10-13 20:33:30 +00001184 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
jjako52c24142002-12-16 13:33:51 +00001185 pdp->gsnlu.l, pdp->gsnlu.v);
jjako08d331d2003-10-13 20:33:30 +00001186
1187 if (version == 1)
1188 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE,
1189 pdp->qos_neg.l, pdp->qos_neg.v);
1190
1191 /* TODO: Charging gateway address */
jjako52c24142002-12-16 13:33:51 +00001192 }
1193
jjako08d331d2003-10-13 20:33:30 +00001194 return gtp_resp(version, gsn, pdp, &packet, length, &pdp->sa_peer,
1195 pdp->fd, pdp->seq, pdp->tid);
jjako52c24142002-12-16 13:33:51 +00001196}
1197
1198/* Handle Create PDP Context Request */
1199int gtp_create_pdp_ind(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00001200 struct sockaddr_in *peer, int fd,
1201 void *pack, unsigned len) {
jjako52c24142002-12-16 13:33:51 +00001202 struct pdp_t *pdp, *pdp_old;
1203 struct pdp_t pdp_buf;
1204 union gtpie_member* ie[GTPIE_SIZE];
1205 uint8_t recovery;
jjako52c24142002-12-16 13:33:51 +00001206
jjako08d331d2003-10-13 20:33:30 +00001207 uint16_t seq = get_seq(pack);
1208 int hlen = get_hlen(pack);
jjako2c381332003-10-21 19:09:53 +00001209 uint8_t linked_nsapi = 0;
1210 struct pdp_t *linked_pdp = NULL;
jjako52c24142002-12-16 13:33:51 +00001211
jjako2c381332003-10-21 19:09:53 +00001212 if(!gtp_dublicate(gsn, version, peer, seq)) return 0;
jjako08d331d2003-10-13 20:33:30 +00001213
1214 pdp = &pdp_buf;
1215 memset(pdp, 0, sizeof(struct pdp_t));
1216
1217 if (version == 0) {
1218 pdp->imsi = ((union gtp_packet*)pack)->gtp0.h.tid & 0x0fffffffffffffff;
1219 pdp->nsapi = (((union gtp_packet*)pack)->gtp0.h.tid & 0xf000000000000000) >> 60;
jjako52c24142002-12-16 13:33:51 +00001220 }
1221
jjako08d331d2003-10-13 20:33:30 +00001222 pdp->seq = seq;
1223 pdp->sa_peer = *peer;
1224 pdp->fd = fd;
1225 pdp->version = version;
1226
jjako52c24142002-12-16 13:33:51 +00001227 /* Decode information elements */
jjako08d331d2003-10-13 20:33:30 +00001228 if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) {
jjako52c24142002-12-16 13:33:51 +00001229 gsn->invalid++;
1230 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1231 "Invalid message format");
1232 if (0 == version)
1233 return EOF;
1234 else
jjako08d331d2003-10-13 20:33:30 +00001235 return gtp_create_pdp_resp(gsn, version, pdp, GTPCAUSE_INVALID_MESSAGE);
jjako52c24142002-12-16 13:33:51 +00001236 }
1237
jjako2c381332003-10-21 19:09:53 +00001238 if (version == 1) {
1239 /* Linked NSAPI (conditional) */
1240 /* If included this is the Secondary PDP Context Activation Procedure */
1241 /* In secondary activation IMSI is not included, so the context must be */
1242 /* identified by the tei */
1243 if (!gtpie_gettv1(ie, GTPIE_NSAPI, 1, &linked_nsapi)) {
1244
1245 /* Find the primary PDP context */
1246 if (pdp_getgtp1(&linked_pdp, get_tei(pack))) {
1247 gsn->incorrect++;
1248 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1249 "Incorrect optional information field");
1250 return gtp_create_pdp_resp(gsn, version, pdp,
1251 GTPCAUSE_OPT_IE_INCORRECT);
1252 }
1253
1254 /* Check that the primary PDP context matches linked nsapi */
1255 if (linked_pdp->nsapi != linked_nsapi) {
1256 gsn->incorrect++;
1257 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1258 "Incorrect optional information field");
1259 return gtp_create_pdp_resp(gsn, version, pdp,
1260 GTPCAUSE_OPT_IE_INCORRECT);
1261 }
1262
1263 /* Copy parameters from primary context */
1264 pdp->selmode = linked_pdp->selmode;
1265 pdp->imsi = linked_pdp->imsi;
1266 pdp->msisdn = linked_pdp->msisdn;
1267 pdp->eua = linked_pdp->eua;
1268 pdp->pco_req = linked_pdp->pco_req;
1269 pdp->apn_req = linked_pdp->apn_req;
1270 pdp->teic_gn = linked_pdp->teic_gn;
1271 pdp->secondary = 1;
1272 }
1273 } /* if (version == 1) */
1274
jjako08d331d2003-10-13 20:33:30 +00001275 if (version == 0) {
1276 if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0,
1277 pdp->qos_req0, sizeof(pdp->qos_req0))) {
1278 gsn->missing++;
1279 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1280 "Missing mandatory information field");
1281 return gtp_create_pdp_resp(gsn, version, pdp, GTPCAUSE_MAN_IE_MISSING);
1282 }
jjako52c24142002-12-16 13:33:51 +00001283 }
jjako2c381332003-10-21 19:09:53 +00001284
1285 if ((version == 1) && (!linked_pdp)) {
1286 /* Not Secondary PDP Context Activation Procedure */
jjako08d331d2003-10-13 20:33:30 +00001287 /* IMSI (conditional) */
1288 if (gtpie_gettv0(ie, GTPIE_IMSI, 0, &pdp->imsi, sizeof(pdp->imsi))) {
1289 gsn->missing++;
1290 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1291 "Missing mandatory information field");
1292 return gtp_create_pdp_resp(gsn, version, pdp,
1293 GTPCAUSE_MAN_IE_MISSING);
1294 }
1295 }
jjako2c381332003-10-21 19:09:53 +00001296
jjako08d331d2003-10-13 20:33:30 +00001297 /* Recovery (optional) */
jjako52c24142002-12-16 13:33:51 +00001298 if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) {
1299 /* TODO: Handle received recovery IE */
1300 }
jjako2c381332003-10-21 19:09:53 +00001301
jjako08d331d2003-10-13 20:33:30 +00001302 /* Selection mode (conditional) */
jjako2c381332003-10-21 19:09:53 +00001303 if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */
1304 if (gtpie_gettv0(ie, GTPIE_SELECTION_MODE, 0,
1305 &pdp->selmode, sizeof(pdp->selmode))) {
1306 gsn->missing++;
1307 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1308 "Missing mandatory information field");
1309 return gtp_create_pdp_resp(gsn, version, pdp,
1310 GTPCAUSE_MAN_IE_MISSING);
1311 }
jjako52c24142002-12-16 13:33:51 +00001312 }
1313
jjako08d331d2003-10-13 20:33:30 +00001314 if (version == 0) {
1315 if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) {
1316 gsn->missing++;
1317 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1318 "Missing mandatory information field");
1319 return gtp_create_pdp_resp(gsn, version, pdp,
1320 GTPCAUSE_MAN_IE_MISSING);
1321 }
1322
1323 if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) {
1324 gsn->missing++;
1325 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1326 "Missing mandatory information field");
1327 return gtp_create_pdp_resp(gsn, version, pdp,
1328 GTPCAUSE_MAN_IE_MISSING);
1329 }
jjako52c24142002-12-16 13:33:51 +00001330 }
jjako2c381332003-10-21 19:09:53 +00001331
1332
jjako08d331d2003-10-13 20:33:30 +00001333 if (version == 1) {
1334 /* TEID (mandatory) */
1335 if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) {
1336 gsn->missing++;
1337 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1338 "Missing mandatory information field");
1339 return gtp_create_pdp_resp(gsn, version, pdp,
1340 GTPCAUSE_MAN_IE_MISSING);
1341 }
1342
1343 /* TEIC (conditional) */
jjako2c381332003-10-21 19:09:53 +00001344 if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */
1345 if (gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn)) {
1346 gsn->missing++;
1347 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1348 "Missing mandatory information field");
1349 return gtp_create_pdp_resp(gsn, version, pdp,
1350 GTPCAUSE_MAN_IE_MISSING);
1351 }
jjako08d331d2003-10-13 20:33:30 +00001352 }
jjako52c24142002-12-16 13:33:51 +00001353 }
1354
jjako2c381332003-10-21 19:09:53 +00001355 /* NSAPI (mandatory) */
1356 if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &pdp->nsapi)) {
1357 gsn->missing++;
1358 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1359 "Missing mandatory information field");
1360 return gtp_create_pdp_resp(gsn, version, pdp,
1361 GTPCAUSE_MAN_IE_MISSING);
1362 }
1363
1364
jjako08d331d2003-10-13 20:33:30 +00001365 /* Charging Characteriatics (optional) */
1366 /* Trace reference (optional) */
1367 /* Trace type (optional) */
1368 /* Charging Characteriatics (optional) */
jjako2c381332003-10-21 19:09:53 +00001369
1370 if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */
1371 /* End User Address (conditional) */
1372 if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l,
jjako52c24142002-12-16 13:33:51 +00001373 &pdp->eua.v, sizeof(pdp->eua.v))) {
jjako2c381332003-10-21 19:09:53 +00001374 gsn->missing++;
1375 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1376 "Missing mandatory information field");
1377 return gtp_create_pdp_resp(gsn, version, pdp,
1378 GTPCAUSE_MAN_IE_MISSING);
1379 }
1380
1381 /* APN */
1382 if (gtpie_gettlv(ie, GTPIE_APN, 0, &pdp->apn_req.l,
jjako52c24142002-12-16 13:33:51 +00001383 &pdp->apn_req.v, sizeof(pdp->apn_req.v))) {
jjako2c381332003-10-21 19:09:53 +00001384 gsn->missing++;
1385 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1386 "Missing mandatory information field");
1387 return gtp_create_pdp_resp(gsn, version, pdp,
1388 GTPCAUSE_MAN_IE_MISSING);
1389 }
1390
1391 /* Extract protocol configuration options (optional) */
1392 if (!gtpie_gettlv(ie, GTPIE_PCO, 0, &pdp->pco_req.l,
1393 &pdp->pco_req.v, sizeof(pdp->pco_req.v))) {
1394 }
jjako52c24142002-12-16 13:33:51 +00001395 }
1396
jjako08d331d2003-10-13 20:33:30 +00001397 /* SGSN address for signalling (mandatory) */
jjako52c24142002-12-16 13:33:51 +00001398 if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l,
1399 &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) {
1400 gsn->missing++;
1401 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1402 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00001403 return gtp_create_pdp_resp(gsn, version, pdp,
jjako52c24142002-12-16 13:33:51 +00001404 GTPCAUSE_MAN_IE_MISSING);
1405 }
1406
jjako08d331d2003-10-13 20:33:30 +00001407 /* SGSN address for user traffic (mandatory) */
jjako52c24142002-12-16 13:33:51 +00001408 if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l,
1409 &pdp->gsnru.v, sizeof(pdp->gsnru.v))) {
1410 gsn->missing++;
1411 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1412 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00001413 return gtp_create_pdp_resp(gsn, version, pdp,
jjako52c24142002-12-16 13:33:51 +00001414 GTPCAUSE_MAN_IE_MISSING);
1415 }
1416
jjako2c381332003-10-21 19:09:53 +00001417 if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */
1418 /* MSISDN (conditional) */
1419 if (gtpie_gettlv(ie, GTPIE_MSISDN, 0, &pdp->msisdn.l,
1420 &pdp->msisdn.v, sizeof(pdp->msisdn.v))) {
1421 gsn->missing++;
1422 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1423 "Missing mandatory information field");
1424 return gtp_create_pdp_resp(gsn, version, pdp,
1425 GTPCAUSE_MAN_IE_MISSING);
1426 }
jjako52c24142002-12-16 13:33:51 +00001427 }
1428
jjako08d331d2003-10-13 20:33:30 +00001429 if (version == 1) {
1430 /* QoS (mandatory) */
1431 if (gtpie_gettlv(ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_req.l,
1432 &pdp->qos_req.v, sizeof(pdp->qos_req.v))) {
1433 gsn->missing++;
1434 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1435 "Missing mandatory information field");
1436 return gtp_create_pdp_resp(gsn, version, pdp,
1437 GTPCAUSE_MAN_IE_MISSING);
1438 }
1439
1440 /* TFT (conditional) */
1441 if (gtpie_gettlv(ie, GTPIE_TFT, 0, &pdp->tft.l,
1442 &pdp->tft.v, sizeof(pdp->tft.v))) {
1443 }
jjako2c381332003-10-21 19:09:53 +00001444
jjako08d331d2003-10-13 20:33:30 +00001445 /* Trigger ID */
1446 /* OMC identity */
1447 }
1448
1449 /* Initialize our own IP addresses */
jjako52c24142002-12-16 13:33:51 +00001450 in_addr2gsna(&pdp->gsnlc, &gsn->gsnc);
1451 in_addr2gsna(&pdp->gsnlu, &gsn->gsnu);
jjako2c381332003-10-21 19:09:53 +00001452
jjako2e840a32003-01-28 16:05:18 +00001453 if (GTP_DEBUG) printf("gtp_create_pdp_ind: Before pdp_tidget\n");
jjako2c381332003-10-21 19:09:53 +00001454
jjako08d331d2003-10-13 20:33:30 +00001455 if (!pdp_getimsi(&pdp_old, pdp->imsi, pdp->nsapi)) {
jjako52c24142002-12-16 13:33:51 +00001456 /* Found old pdp with same tid. Now the voodoo begins! */
jjako08d331d2003-10-13 20:33:30 +00001457 /* 09.60 / 29.060 allows create on existing context to "steal" */
1458 /* the context which was allready established */
jjako52c24142002-12-16 13:33:51 +00001459 /* We check that the APN, selection mode and MSISDN is the same */
jjako2e840a32003-01-28 16:05:18 +00001460 if (GTP_DEBUG) printf("gtp_create_pdp_ind: Old context found\n");
jjako08d331d2003-10-13 20:33:30 +00001461 if ((pdp->apn_req.l == pdp_old->apn_req.l)
jjako52c24142002-12-16 13:33:51 +00001462 && (!memcmp(pdp->apn_req.v, pdp_old->apn_req.v, pdp->apn_req.l))
1463 && (pdp->selmode == pdp_old->selmode)
1464 && (pdp->msisdn.l == pdp_old->msisdn.l)
1465 && (!memcmp(pdp->msisdn.v, pdp_old->msisdn.v, pdp->msisdn.l))) {
1466 /* OK! We are dealing with the same APN. We will copy new
1467 * parameters to the old pdp and send off confirmation
1468 * We ignore the following information elements:
1469 * QoS: MS will get originally negotiated QoS.
1470 * End user address (EUA). MS will get old EUA anyway.
1471 * Protocol configuration option (PCO): Only application can verify */
jjako2e840a32003-01-28 16:05:18 +00001472
1473 if (GTP_DEBUG) printf("gtp_create_pdp_ind: Old context found\n");
jjako52c24142002-12-16 13:33:51 +00001474
1475 /* Copy remote flow label */
1476 pdp_old->flru = pdp->flru;
1477 pdp_old->flrc = pdp->flrc;
1478
jjako08d331d2003-10-13 20:33:30 +00001479 /* Copy remote tei */
1480 pdp_old->teid_gn = pdp->teid_gn;
1481 pdp_old->teic_gn = pdp->teic_gn;
1482
jjako52c24142002-12-16 13:33:51 +00001483 /* Copy peer GSN address */
1484 pdp_old->gsnrc.l = pdp->gsnrc.l;
1485 memcpy(&pdp_old->gsnrc.v, &pdp->gsnrc.v, pdp->gsnrc.l);
1486 pdp_old->gsnru.l = pdp->gsnru.l;
1487 memcpy(&pdp_old->gsnru.v, &pdp->gsnru.v, pdp->gsnru.l);
jjako2c381332003-10-21 19:09:53 +00001488
1489 /* Copy request parameters */
1490 pdp_old->seq = pdp->seq;
1491 pdp_old->sa_peer = pdp->sa_peer;
1492 pdp_old->fd = pdp->fd = fd;
1493 pdp_old->version = pdp->version = version;
1494
1495 /* Switch to using the old pdp context */
jjako52c24142002-12-16 13:33:51 +00001496 pdp = pdp_old;
1497
1498 /* Confirm to peer that things were "successful" */
jjako08d331d2003-10-13 20:33:30 +00001499 return gtp_create_pdp_resp(gsn, version, pdp, GTPCAUSE_ACC_REQ);
jjako52c24142002-12-16 13:33:51 +00001500 }
1501 else { /* This is not the same PDP context. Delete the old one. */
jjako2e840a32003-01-28 16:05:18 +00001502
1503 if (GTP_DEBUG) printf("gtp_create_pdp_ind: Deleting old context\n");
jjako52c24142002-12-16 13:33:51 +00001504
1505 if (gsn->cb_delete_context) gsn->cb_delete_context(pdp_old);
1506 pdp_freepdp(pdp_old);
jjako08d331d2003-10-13 20:33:30 +00001507
jjako2e840a32003-01-28 16:05:18 +00001508 if (GTP_DEBUG) printf("gtp_create_pdp_ind: Deleted...\n");
jjako52c24142002-12-16 13:33:51 +00001509 }
1510 }
1511
jjako08d331d2003-10-13 20:33:30 +00001512 pdp_newpdp(&pdp, pdp->imsi, pdp->nsapi, pdp);
jjako52c24142002-12-16 13:33:51 +00001513
1514 /* Callback function to validata login */
jjako08d331d2003-10-13 20:33:30 +00001515 if (gsn->cb_create_context_ind !=0)
1516 return gsn->cb_create_context_ind(pdp);
jjako52c24142002-12-16 13:33:51 +00001517 else {
jjako08d331d2003-10-13 20:33:30 +00001518 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1519 "No create_context_ind callback defined");
1520 return gtp_create_pdp_resp(gsn, version, pdp, GTPCAUSE_NOT_SUPPORTED);
jjako52c24142002-12-16 13:33:51 +00001521 }
1522}
1523
1524
1525/* Handle Create PDP Context Response */
1526int gtp_create_pdp_conf(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00001527 struct sockaddr_in *peer,
1528 void *pack, unsigned len) {
jjako52c24142002-12-16 13:33:51 +00001529 struct pdp_t *pdp;
1530 union gtpie_member *ie[GTPIE_SIZE];
1531 uint8_t cause, recovery;
jjako08d331d2003-10-13 20:33:30 +00001532 void *cbp = NULL;
jjako52c24142002-12-16 13:33:51 +00001533 uint8_t type = 0;
jjako08d331d2003-10-13 20:33:30 +00001534 int hlen = get_hlen(pack);
jjako52c24142002-12-16 13:33:51 +00001535
1536 /* Remove packet from queue */
jjako08d331d2003-10-13 20:33:30 +00001537 if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) return EOF;
jjako52c24142002-12-16 13:33:51 +00001538
1539 /* Find the context in question */
jjako08d331d2003-10-13 20:33:30 +00001540 if (pdp_getgtp1(&pdp, get_tei(pack))) {
jjako52c24142002-12-16 13:33:51 +00001541 gsn->err_unknownpdp++;
1542 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1543 "Unknown PDP context");
jjako08d331d2003-10-13 20:33:30 +00001544 if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +00001545 return EOF;
1546 }
1547
jjako2c381332003-10-21 19:09:53 +00001548 /* Register that we have received a valid teic from GGSN */
1549 pdp->teic_confirmed = 1;
1550
jjako52c24142002-12-16 13:33:51 +00001551 /* Decode information elements */
jjako08d331d2003-10-13 20:33:30 +00001552 if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) {
jjako52c24142002-12-16 13:33:51 +00001553 gsn->invalid++;
1554 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1555 "Invalid message format");
jjako08d331d2003-10-13 20:33:30 +00001556 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00001557 return EOF;
1558 }
1559
1560 /* Extract cause value (mandatory) */
1561 if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) {
1562 gsn->missing++;
1563 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1564 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00001565 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00001566 return EOF;
1567 }
1568
1569 /* Extract recovery (optional) */
1570 if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) {
1571 /* TODO: Handle received recovery IE */
1572 }
1573
1574 /* Extract protocol configuration options (optional) */
1575 if (!gtpie_gettlv(ie, GTPIE_PCO, 0, &pdp->pco_req.l,
1576 &pdp->pco_req.v, sizeof(pdp->pco_req.v))) {
jjako52c24142002-12-16 13:33:51 +00001577 }
1578
1579 /* Check all conditional information elements */
1580 if (GTPCAUSE_ACC_REQ == cause) {
1581
jjako08d331d2003-10-13 20:33:30 +00001582 if (version == 0) {
1583 if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0,
1584 &pdp->qos_neg0, sizeof(pdp->qos_neg0))) {
1585 gsn->missing++;
1586 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1587 "Missing conditional information field");
1588 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
1589 return EOF;
1590 }
jjako52c24142002-12-16 13:33:51 +00001591 }
jjako52c24142002-12-16 13:33:51 +00001592
1593 if (gtpie_gettv1(ie, GTPIE_REORDER, 0, &pdp->reorder)) {
1594 gsn->missing++;
1595 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1596 "Missing conditional information field");
jjako08d331d2003-10-13 20:33:30 +00001597 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00001598 return EOF;
1599 }
1600
jjako08d331d2003-10-13 20:33:30 +00001601 if (version == 0) {
1602 if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) {
1603 gsn->missing++;
1604 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1605 "Missing conditional information field");
1606 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
1607 return EOF;
1608 }
jjako52c24142002-12-16 13:33:51 +00001609
jjako08d331d2003-10-13 20:33:30 +00001610 if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) {
1611 gsn->missing++;
1612 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1613 "Missing conditional information field");
1614 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
1615 return EOF;
1616 }
1617 }
1618
1619 if (version == 1) {
1620 if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) {
1621 gsn->missing++;
1622 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1623 "Missing conditional information field");
1624 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
1625 return EOF;
1626 }
1627
1628 if (gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn)) {
1629 gsn->missing++;
1630 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1631 "Missing conditional information field");
1632 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
1633 return EOF;
1634 }
jjako52c24142002-12-16 13:33:51 +00001635 }
1636
1637 if (gtpie_gettv4(ie, GTPIE_CHARGING_ID, 0, &pdp->cid)) {
1638 gsn->missing++;
1639 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1640 "Missing conditional information field");
jjako08d331d2003-10-13 20:33:30 +00001641 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00001642 }
1643
1644 if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l,
1645 &pdp->eua.v, sizeof(pdp->eua.v))) {
1646 gsn->missing++;
1647 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1648 "Missing conditional information field");
jjako08d331d2003-10-13 20:33:30 +00001649 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00001650 return EOF;
1651 }
1652
1653 if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l,
1654 &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) {
1655 gsn->missing++;
1656 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1657 "Missing conditional information field");
jjako08d331d2003-10-13 20:33:30 +00001658 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00001659 return EOF;
1660 }
1661
1662 if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l,
1663 &pdp->gsnru.v, sizeof(pdp->gsnru.v))) {
1664 gsn->missing++;
1665 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1666 "Missing conditional information field");
jjako08d331d2003-10-13 20:33:30 +00001667 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00001668 return EOF;
1669 }
jjako52c24142002-12-16 13:33:51 +00001670
jjako08d331d2003-10-13 20:33:30 +00001671 if (version == 1) {
1672 if (gtpie_gettlv(ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_neg.l,
1673 &pdp->qos_neg.v, sizeof(pdp->qos_neg.v))) {
1674 gsn->missing++;
1675 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1676 "Missing conditional information field");
1677 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
1678 return EOF;
1679 }
1680 }
1681
1682 }
1683
1684 if (gsn->cb_conf) gsn->cb_conf(type, cause, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00001685
1686 return 0;
1687}
1688
jjako52c24142002-12-16 13:33:51 +00001689
jjako08d331d2003-10-13 20:33:30 +00001690/* API: Send Update PDP Context Request */
1691int gtp_update_context(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp,
1692 struct in_addr* inetaddr) {
1693 union gtp_packet packet;
1694 int length = get_default_gtp(pdp->version, GTP_UPDATE_PDP_REQ, &packet);
jjako52c24142002-12-16 13:33:51 +00001695
jjako08d331d2003-10-13 20:33:30 +00001696 if (pdp->version == 0)
1697 gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0,
1698 sizeof(pdp->qos_req0), pdp->qos_req0);
jjako2c381332003-10-21 19:09:53 +00001699
1700 /* Include IMSI if updating with unknown teic_gn */
1701 if ((pdp->version == 1) && (!pdp->teic_gn))
jjako08d331d2003-10-13 20:33:30 +00001702 gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_IMSI,
1703 sizeof(pdp->imsi), (uint8_t*) &pdp->imsi);
jjako2c381332003-10-21 19:09:53 +00001704
jjako08d331d2003-10-13 20:33:30 +00001705 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY,
jjako52c24142002-12-16 13:33:51 +00001706 gsn->restart_counter);
jjako2c381332003-10-21 19:09:53 +00001707
jjako08d331d2003-10-13 20:33:30 +00001708 if (pdp->version == 0) {
1709 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI,
jjako52c24142002-12-16 13:33:51 +00001710 pdp->fllu);
jjako08d331d2003-10-13 20:33:30 +00001711 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C,
jjako52c24142002-12-16 13:33:51 +00001712 pdp->fllc);
jjako52c24142002-12-16 13:33:51 +00001713 }
jjako2c381332003-10-21 19:09:53 +00001714
jjako08d331d2003-10-13 20:33:30 +00001715 if (pdp->version == 1) {
1716 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI,
1717 pdp->teid_own);
jjako52c24142002-12-16 13:33:51 +00001718
jjako2c381332003-10-21 19:09:53 +00001719 if (!pdp->teic_confirmed)
1720 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C,
1721 pdp->teic_own);
1722 }
1723
jjako08d331d2003-10-13 20:33:30 +00001724 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI,
1725 pdp->nsapi);
jjako2c381332003-10-21 19:09:53 +00001726
jjako08d331d2003-10-13 20:33:30 +00001727 /* TODO
jjako2c381332003-10-21 19:09:53 +00001728 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_REF,
1729 pdp->traceref);
1730 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_TYPE,
1731 pdp->tracetype); */
1732
jjako08d331d2003-10-13 20:33:30 +00001733 /* TODO if ggsn update message
jjako2c381332003-10-21 19:09:53 +00001734 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA,
1735 pdp->eua.l, pdp->eua.v);
jjako08d331d2003-10-13 20:33:30 +00001736 */
jjako2c381332003-10-21 19:09:53 +00001737
jjako08d331d2003-10-13 20:33:30 +00001738 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
1739 pdp->gsnlc.l, pdp->gsnlc.v);
1740 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
1741 pdp->gsnlu.l, pdp->gsnlu.v);
jjako2c381332003-10-21 19:09:53 +00001742
jjako08d331d2003-10-13 20:33:30 +00001743 if (pdp->version == 1)
1744 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE,
1745 pdp->qos_req.l, pdp->qos_req.v);
jjako2c381332003-10-21 19:09:53 +00001746
1747
jjako08d331d2003-10-13 20:33:30 +00001748 if ((pdp->version == 1) && pdp->tft.l)
1749 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TFT,
1750 pdp->tft.l, pdp->tft.v);
1751
1752 if ((pdp->version == 1) && pdp->triggerid.l)
1753 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TRIGGER_ID,
1754 pdp->triggerid.l, pdp->triggerid.v);
1755
1756 if ((pdp->version == 1) && pdp->omcid.l)
1757 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_OMC_ID,
1758 pdp->omcid.l, pdp->omcid.v);
1759
1760 gtp_req(gsn, pdp->version, NULL, &packet, length, inetaddr, cbp);
1761
1762 return 0;
jjako52c24142002-12-16 13:33:51 +00001763}
1764
jjako08d331d2003-10-13 20:33:30 +00001765
1766/* Send Update PDP Context Response */
1767int gtp_update_pdp_resp(struct gsn_t *gsn, int version,
1768 struct sockaddr_in *peer, int fd,
1769 void *pack, unsigned len,
1770 struct pdp_t *pdp, uint8_t cause) {
1771
1772 union gtp_packet packet;
1773 int length = get_default_gtp(version, GTP_CREATE_PDP_RSP, &packet);
1774
1775 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause);
1776
1777 if (cause == GTPCAUSE_ACC_REQ) {
1778
1779 if (version == 0)
1780 gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0,
1781 sizeof(pdp->qos_neg0), pdp->qos_neg0);
1782
1783 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY,
1784 gsn->restart_counter);
1785
1786 if (version == 0) {
1787 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI,
1788 pdp->fllu);
1789 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C,
1790 pdp->fllc);
1791 }
1792
1793 if (version == 1) {
1794 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI,
1795 pdp->teid_own);
jjako2c381332003-10-21 19:09:53 +00001796
1797 if (!pdp->teic_confirmed)
1798 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C,
1799 pdp->teic_own);
jjako08d331d2003-10-13 20:33:30 +00001800 }
jjako2c381332003-10-21 19:09:53 +00001801
1802 /* TODO we use teid_own as charging ID address */
jjako08d331d2003-10-13 20:33:30 +00001803 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_CHARGING_ID,
jjako2c381332003-10-21 19:09:53 +00001804 pdp->teid_own);
1805
jjako08d331d2003-10-13 20:33:30 +00001806 /* If ggsn
jjako2c381332003-10-21 19:09:53 +00001807 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA,
1808 pdp->eua.l, pdp->eua.v); */
1809
jjako08d331d2003-10-13 20:33:30 +00001810 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
1811 pdp->gsnlc.l, pdp->gsnlc.v);
1812 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
1813 pdp->gsnlu.l, pdp->gsnlu.v);
jjako2c381332003-10-21 19:09:53 +00001814
jjako08d331d2003-10-13 20:33:30 +00001815 if (version == 1)
1816 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE,
1817 pdp->qos_neg.l, pdp->qos_neg.v);
jjako2c381332003-10-21 19:09:53 +00001818
jjako08d331d2003-10-13 20:33:30 +00001819 /* TODO: Charging gateway address */
1820 }
jjako2c381332003-10-21 19:09:53 +00001821
1822 return gtp_resp(version, gsn, pdp, &packet, length, peer,
1823 fd, get_seq(pack), get_tid(pack));
jjako08d331d2003-10-13 20:33:30 +00001824}
1825
1826
jjako52c24142002-12-16 13:33:51 +00001827/* Handle Update PDP Context Request */
1828int gtp_update_pdp_ind(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00001829 struct sockaddr_in *peer, int fd,
1830 void *pack, unsigned len) {
1831 struct pdp_t *pdp;
1832 struct pdp_t pdp_backup;
jjako52c24142002-12-16 13:33:51 +00001833 union gtpie_member* ie[GTPIE_SIZE];
1834 uint8_t recovery;
1835
jjako08d331d2003-10-13 20:33:30 +00001836 uint16_t seq = get_seq(pack);
1837 int hlen = get_hlen(pack);
jjako52c24142002-12-16 13:33:51 +00001838
jjako08d331d2003-10-13 20:33:30 +00001839 uint64_t imsi;
1840 uint8_t nsapi;
1841
jjako52c24142002-12-16 13:33:51 +00001842 /* Is this a dublicate ? */
jjako08d331d2003-10-13 20:33:30 +00001843 if(!gtp_dublicate(gsn, version, peer, seq)) {
jjako52c24142002-12-16 13:33:51 +00001844 return 0; /* We allready send of response once */
1845 }
jjako2c381332003-10-21 19:09:53 +00001846
jjako52c24142002-12-16 13:33:51 +00001847
1848 /* Decode information elements */
jjako08d331d2003-10-13 20:33:30 +00001849 if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) {
jjako52c24142002-12-16 13:33:51 +00001850 gsn->invalid++;
1851 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1852 "Invalid message format");
1853 if (0 == version)
1854 return EOF;
1855 else
jjako08d331d2003-10-13 20:33:30 +00001856 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len,
1857 NULL, GTPCAUSE_INVALID_MESSAGE);
jjako52c24142002-12-16 13:33:51 +00001858 }
1859
jjako08d331d2003-10-13 20:33:30 +00001860 /* Finding PDP: */
1861 /* For GTP0 we use the tunnel identifier to provide imsi and nsapi. */
1862 /* For GTP1 we must use imsi and nsapi if imsi is present. Otherwise */
1863 /* we have to use the tunnel endpoint identifier */
1864 if (version == 0) {
1865 imsi = ((union gtp_packet*)pack)->gtp0.h.tid & 0x0fffffffffffffff;
1866 nsapi = (((union gtp_packet*)pack)->gtp0.h.tid & 0xf000000000000000) >> 60;
jjako2c381332003-10-21 19:09:53 +00001867
jjako08d331d2003-10-13 20:33:30 +00001868 /* Find the context in question */
1869 if (pdp_getimsi(&pdp, imsi, nsapi)) {
1870 gsn->err_unknownpdp++;
1871 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1872 "Unknown PDP context");
1873 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len,
1874 NULL, GTPCAUSE_NON_EXIST);
1875 }
jjako52c24142002-12-16 13:33:51 +00001876 }
jjako08d331d2003-10-13 20:33:30 +00001877 else if (version == 1) {
1878 /* NSAPI (mandatory) */
1879 if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &nsapi)) {
1880 gsn->missing++;
1881 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1882 "Missing mandatory information field");
1883 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len,
1884 NULL, GTPCAUSE_MAN_IE_MISSING);
1885 }
jjako2c381332003-10-21 19:09:53 +00001886
jjako08d331d2003-10-13 20:33:30 +00001887 /* IMSI (conditional) */
1888 if (gtpie_gettv0(ie, GTPIE_IMSI, 0, &imsi, sizeof(imsi))) {
1889 /* Find the context in question */
1890 if (pdp_getgtp1(&pdp, get_tei(pack))) {
1891 gsn->err_unknownpdp++;
1892 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1893 "Unknown PDP context");
1894 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len,
1895 NULL, GTPCAUSE_NON_EXIST);
1896 }
1897 }
1898 else {
1899 /* Find the context in question */
1900 if (pdp_getimsi(&pdp, imsi, nsapi)) {
1901 gsn->err_unknownpdp++;
1902 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1903 "Unknown PDP context");
1904 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len,
1905 NULL, GTPCAUSE_NON_EXIST);
1906 }
1907 }
1908 }
1909 else {
1910 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown version");
1911 return EOF;
1912 }
jjako2c381332003-10-21 19:09:53 +00001913
jjako08d331d2003-10-13 20:33:30 +00001914 /* Make a backup copy in case anything is wrong */
1915 memcpy(&pdp_backup, pdp, sizeof(pdp_backup));
1916
1917 if (version == 0) {
1918 if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0,
1919 pdp->qos_req0, sizeof(pdp->qos_req0))) {
1920 gsn->missing++;
1921 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1922 "Missing mandatory information field");
1923 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
1924 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len,
1925 pdp, GTPCAUSE_MAN_IE_MISSING);
1926 }
1927 }
1928
1929 /* Recovery (optional) */
jjako52c24142002-12-16 13:33:51 +00001930 if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) {
1931 /* TODO: Handle received recovery IE */
1932 }
1933
jjako08d331d2003-10-13 20:33:30 +00001934 if (version == 0) {
1935 if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) {
1936 gsn->missing++;
1937 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1938 "Missing mandatory information field");
1939 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
1940 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
1941 GTPCAUSE_MAN_IE_MISSING);
1942 }
1943
1944 if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) {
1945 gsn->missing++;
1946 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1947 "Missing mandatory information field");
1948 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
1949 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
1950 GTPCAUSE_MAN_IE_MISSING);
1951 }
1952 }
1953
1954
1955 if (version == 1) {
1956 /* TEID (mandatory) */
1957 if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) {
1958 gsn->missing++;
1959 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1960 "Missing mandatory information field");
1961 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
1962 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
1963 GTPCAUSE_MAN_IE_MISSING);
1964 }
jjako2c381332003-10-21 19:09:53 +00001965
jjako08d331d2003-10-13 20:33:30 +00001966 /* TEIC (conditional) */
1967 /* If TEIC is not included it means that we have allready received it */
jjako2c381332003-10-21 19:09:53 +00001968 /* TODO: From 29.060 it is not clear if TEI_C MUST be included for */
1969 /* all updated contexts, or only for one of the linked contexts */
1970 gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn);
1971
jjako08d331d2003-10-13 20:33:30 +00001972 /* NSAPI (mandatory) */
1973 if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &pdp->nsapi)) {
1974 gsn->missing++;
1975 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1976 "Missing mandatory information field");
1977 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
1978 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
1979 GTPCAUSE_MAN_IE_MISSING);
1980 }
1981 }
1982
1983 /* Trace reference (optional) */
1984 /* Trace type (optional) */
1985
1986 /* End User Address (conditional) TODO: GGSN Initiated
1987 if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l,
1988 &pdp->eua.v, sizeof(pdp->eua.v))) {
jjako52c24142002-12-16 13:33:51 +00001989 gsn->missing++;
1990 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1991 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00001992 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
1993 return gtp_update_pdp_resp(gsn, version, pdp,
jjako52c24142002-12-16 13:33:51 +00001994 GTPCAUSE_MAN_IE_MISSING);
jjako08d331d2003-10-13 20:33:30 +00001995 } */
jjako52c24142002-12-16 13:33:51 +00001996
jjako08d331d2003-10-13 20:33:30 +00001997
1998 /* SGSN address for signalling (mandatory) */
1999 /* It is weird that this is mandatory when TEIC is conditional */
2000 if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l,
2001 &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) {
jjako52c24142002-12-16 13:33:51 +00002002 gsn->missing++;
2003 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2004 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00002005 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
2006 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
jjako52c24142002-12-16 13:33:51 +00002007 GTPCAUSE_MAN_IE_MISSING);
2008 }
2009
jjako08d331d2003-10-13 20:33:30 +00002010 /* SGSN address for user traffic (mandatory) */
2011 if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l,
2012 &pdp->gsnru.v, sizeof(pdp->gsnru.v))) {
jjako52c24142002-12-16 13:33:51 +00002013 gsn->missing++;
2014 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2015 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00002016 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
2017 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
jjako52c24142002-12-16 13:33:51 +00002018 GTPCAUSE_MAN_IE_MISSING);
2019 }
jjako08d331d2003-10-13 20:33:30 +00002020
2021 if (version == 1) {
2022 /* QoS (mandatory) */
2023 if (gtpie_gettlv(ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_req.l,
2024 &pdp->qos_req.v, sizeof(pdp->qos_req.v))) {
2025 gsn->missing++;
2026 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2027 "Missing mandatory information field");
2028 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
2029 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
2030 GTPCAUSE_MAN_IE_MISSING);
2031 }
jjako52c24142002-12-16 13:33:51 +00002032
jjako08d331d2003-10-13 20:33:30 +00002033 /* TFT (conditional) */
2034 if (gtpie_gettlv(ie, GTPIE_TFT, 0, &pdp->tft.l,
2035 &pdp->tft.v, sizeof(pdp->tft.v))) {
2036 }
2037
2038 /* OMC identity */
jjako52c24142002-12-16 13:33:51 +00002039 }
2040
jjako52c24142002-12-16 13:33:51 +00002041 /* Confirm to peer that things were "successful" */
jjako08d331d2003-10-13 20:33:30 +00002042 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
jjako52c24142002-12-16 13:33:51 +00002043 GTPCAUSE_ACC_REQ);
2044}
2045
2046
2047/* Handle Update PDP Context Response */
2048int gtp_update_pdp_conf(struct gsn_t *gsn, int version,
2049 struct sockaddr_in *peer,
2050 void *pack, unsigned len) {
2051 struct pdp_t *pdp;
2052 union gtpie_member *ie[GTPIE_SIZE];
2053 uint8_t cause, recovery;
jjako08d331d2003-10-13 20:33:30 +00002054 void *cbp = NULL;
jjako52c24142002-12-16 13:33:51 +00002055 uint8_t type = 0;
2056
2057 /* Remove packet from queue */
jjako08d331d2003-10-13 20:33:30 +00002058 if (gtp_conf(gsn, 0, peer, pack, len, &type, &cbp)) return EOF;
jjako52c24142002-12-16 13:33:51 +00002059
2060 /* Find the context in question */
2061 if (pdp_getgtp0(&pdp, ntoh16(((union gtp_packet*)pack)->gtp0.h.flow))) {
2062 gsn->err_unknownpdp++;
2063 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2064 "Unknown PDP context");
jjako08d331d2003-10-13 20:33:30 +00002065 if (gsn->cb_conf) gsn->cb_conf(type, cause, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +00002066 return EOF;
2067 }
2068
jjako2c381332003-10-21 19:09:53 +00002069 /* Register that we have received a valid teic from GGSN */
2070 pdp->teic_confirmed = 1;
2071
jjako52c24142002-12-16 13:33:51 +00002072 /* Decode information elements */
jjako08d331d2003-10-13 20:33:30 +00002073 if (gtpie_decaps(ie, 0, pack+GTP0_HEADER_SIZE, len-GTP0_HEADER_SIZE)) {
jjako52c24142002-12-16 13:33:51 +00002074 gsn->invalid++;
2075 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2076 "Invalid message format");
jjako08d331d2003-10-13 20:33:30 +00002077 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00002078 if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2079 pdp_freepdp(pdp);
2080 return EOF;
2081 }
2082
2083 /* Extract cause value (mandatory) */
2084 if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) {
2085 gsn->missing++;
2086 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2087 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00002088 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00002089 if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2090 pdp_freepdp(pdp);
2091 return EOF;
2092 }
2093
2094 /* Extract recovery (optional) */
2095 if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) {
2096 /* TODO: Handle received recovery IE */
2097 }
2098
2099 /* Check all conditional information elements */
2100 if (GTPCAUSE_ACC_REQ != cause) {
jjako08d331d2003-10-13 20:33:30 +00002101 if (gsn->cb_conf) gsn->cb_conf(type, cause, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00002102 if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2103 pdp_freepdp(pdp);
2104 return 0;
2105 }
2106 else {
2107 /* Check for missing conditionary information elements */
2108 if (!(gtpie_exist(ie, GTPIE_QOS_PROFILE0, 0) &&
2109 gtpie_exist(ie, GTPIE_REORDER, 0) &&
2110 gtpie_exist(ie, GTPIE_FL_DI, 0) &&
2111 gtpie_exist(ie, GTPIE_FL_C, 0) &&
2112 gtpie_exist(ie, GTPIE_CHARGING_ID, 0) &&
2113 gtpie_exist(ie, GTPIE_EUA, 0) &&
2114 gtpie_exist(ie, GTPIE_GSN_ADDR, 0) &&
2115 gtpie_exist(ie, GTPIE_GSN_ADDR, 1))) {
2116 gsn->missing++;
2117 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2118 "Missing conditional information field");
jjako08d331d2003-10-13 20:33:30 +00002119 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00002120 if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2121 pdp_freepdp(pdp);
2122 return EOF;
2123 }
2124
2125 /* Update pdp with new values */
2126 gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0,
2127 pdp->qos_neg0, sizeof(pdp->qos_neg0));
2128 gtpie_gettv1(ie, GTPIE_REORDER, 0, &pdp->reorder);
2129 gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru);
2130 gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc);
2131 gtpie_gettv4(ie, GTPIE_CHARGING_ID, 0, &pdp->cid);
2132 gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l,
2133 &pdp->eua.v, sizeof(pdp->eua.v));
2134 gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l,
2135 &pdp->gsnrc.v, sizeof(pdp->gsnrc.v));
2136 gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l,
2137 &pdp->gsnru.v, sizeof(pdp->gsnru.v));
2138
jjako08d331d2003-10-13 20:33:30 +00002139 if (gsn->cb_conf) gsn->cb_conf(type, cause, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00002140 return 0; /* Succes */
2141 }
2142}
2143
jjako52c24142002-12-16 13:33:51 +00002144
jjako08d331d2003-10-13 20:33:30 +00002145/* API: Send Delete PDP Context Request */
jjako2c381332003-10-21 19:09:53 +00002146int gtp_delete_context_req(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp,
2147 int teardown) {
jjako08d331d2003-10-13 20:33:30 +00002148 union gtp_packet packet;
jjako2c381332003-10-21 19:09:53 +00002149 int length = get_default_gtp(pdp->version, GTP_DELETE_PDP_REQ, &packet);
jjako08d331d2003-10-13 20:33:30 +00002150 struct in_addr addr;
jjako2c381332003-10-21 19:09:53 +00002151 struct pdp_t *linked_pdp;
2152 struct pdp_t *secondary_pdp;
2153 int n;
2154 int count = 0;
2155
jjako52c24142002-12-16 13:33:51 +00002156 if (gsna2in_addr(&addr, &pdp->gsnrc)) {
2157 gsn->err_address++;
2158 gtp_err(LOG_ERR, __FILE__, __LINE__, "GSN address conversion failed");
2159 return EOF;
2160 }
jjako2c381332003-10-21 19:09:53 +00002161
2162 if (pdp_getgtp1(&linked_pdp, pdp->teic_own)) {
2163 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown linked PDP context");
2164 return EOF;
2165 }
2166
2167 if (!teardown) {
2168 for (n=0; n< PDP_MAXNSAPI; n++)
2169 if (linked_pdp->secondary_tei[n]) count++;
2170 if (count <= 1) {
2171 gtp_err(LOG_ERR, __FILE__, __LINE__,
2172 "Must use teardown for last context");
2173 return EOF;
2174 }
2175 }
2176
jjako08d331d2003-10-13 20:33:30 +00002177 if (pdp->version == 1) {
2178 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI,
2179 pdp->nsapi);
jjako2c381332003-10-21 19:09:53 +00002180
2181 if (teardown)
2182 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_TEARDOWN,
2183 0xff);
jjako08d331d2003-10-13 20:33:30 +00002184 }
jjako52c24142002-12-16 13:33:51 +00002185
jjako2c381332003-10-21 19:09:53 +00002186 gtp_req(gsn, pdp->version, pdp, &packet, length, &addr, cbp);
jjako52c24142002-12-16 13:33:51 +00002187
jjako2c381332003-10-21 19:09:53 +00002188 if (teardown) { /* Remove all contexts */
2189 for (n=0; n< PDP_MAXNSAPI; n++) {
2190 if (linked_pdp->secondary_tei[n]) {
2191 if (pdp_getgtp1(&secondary_pdp, linked_pdp->secondary_tei[n])) {
2192 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown secondary PDP context");
2193 return EOF;
2194 }
2195 if (linked_pdp != secondary_pdp) {
2196 if (gsn->cb_delete_context) gsn->cb_delete_context(secondary_pdp);
2197 pdp_freepdp(secondary_pdp);
2198 }
2199 }
2200 }
2201 if (gsn->cb_delete_context) gsn->cb_delete_context(linked_pdp);
2202 pdp_freepdp(linked_pdp);
2203 }
2204 else {
2205 if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2206 if (pdp == linked_pdp) {
2207 linked_pdp->secondary_tei[pdp->nsapi & 0xf0] = 0;
2208 linked_pdp->nodata = 1;
2209 }
2210 else
2211 pdp_freepdp(pdp);
2212 }
2213
2214 return 0;
2215}
jjako08d331d2003-10-13 20:33:30 +00002216
jjako52c24142002-12-16 13:33:51 +00002217/* Send Delete PDP Context Response */
2218int gtp_delete_pdp_resp(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00002219 struct sockaddr_in *peer, int fd,
jjako52c24142002-12-16 13:33:51 +00002220 void *pack, unsigned len,
jjako2c381332003-10-21 19:09:53 +00002221 struct pdp_t *pdp, struct pdp_t *linked_pdp,
2222 uint8_t cause, int teardown)
jjako52c24142002-12-16 13:33:51 +00002223{
2224 union gtp_packet packet;
jjako2c381332003-10-21 19:09:53 +00002225 struct pdp_t *secondary_pdp;
jjako08d331d2003-10-13 20:33:30 +00002226 int length = get_default_gtp(version, GTP_DELETE_PDP_RSP, &packet);
jjako2c381332003-10-21 19:09:53 +00002227 int n;
jjako52c24142002-12-16 13:33:51 +00002228
jjako08d331d2003-10-13 20:33:30 +00002229 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause);
jjako52c24142002-12-16 13:33:51 +00002230
jjako08d331d2003-10-13 20:33:30 +00002231 gtp_resp(version, gsn, pdp, &packet, length, peer, fd,
jjako2c381332003-10-21 19:09:53 +00002232 get_seq(pack), get_tid(pack));
jjako52c24142002-12-16 13:33:51 +00002233
jjako2c381332003-10-21 19:09:53 +00002234 if (cause == GTPCAUSE_ACC_REQ) {
2235 if ((teardown) || (version == 0)) { /* Remove all contexts */
2236 for (n=0; n< PDP_MAXNSAPI; n++) {
2237 if (linked_pdp->secondary_tei[n]) {
2238 if (pdp_getgtp1(&secondary_pdp, linked_pdp->secondary_tei[n])) {
2239 gtp_err(LOG_ERR, __FILE__, __LINE__,
2240 "Unknown secondary PDP context");
2241 return EOF;
2242 }
2243 if (linked_pdp != secondary_pdp) {
2244 if (gsn->cb_delete_context) gsn->cb_delete_context(secondary_pdp);
2245 pdp_freepdp(secondary_pdp);
2246 }
2247 }
2248 }
2249 if (gsn->cb_delete_context) gsn->cb_delete_context(linked_pdp);
2250 pdp_freepdp(linked_pdp);
2251 }
2252 else { /* Remove only current context */
2253 if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2254 if (pdp == linked_pdp) {
2255 linked_pdp->secondary_tei[pdp->nsapi & 0xf0] = 0;
2256 linked_pdp->nodata = 1;
2257 }
2258 else
2259 pdp_freepdp(pdp);
2260 }
2261 } /* if (cause == GTPCAUSE_ACC_REQ) */
jjako52c24142002-12-16 13:33:51 +00002262
jjako08d331d2003-10-13 20:33:30 +00002263 return 0;
jjako52c24142002-12-16 13:33:51 +00002264}
2265
2266/* Handle Delete PDP Context Request */
2267int gtp_delete_pdp_ind(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00002268 struct sockaddr_in *peer, int fd,
2269 void *pack, unsigned len) {
jjako2c381332003-10-21 19:09:53 +00002270 struct pdp_t *pdp = NULL;
2271 struct pdp_t *linked_pdp = NULL;
jjako52c24142002-12-16 13:33:51 +00002272 union gtpie_member* ie[GTPIE_SIZE];
jjako08d331d2003-10-13 20:33:30 +00002273
2274 uint16_t seq = get_seq(pack);
2275 int hlen = get_hlen(pack);
jjako52c24142002-12-16 13:33:51 +00002276
jjako08d331d2003-10-13 20:33:30 +00002277 uint8_t nsapi;
jjako2c381332003-10-21 19:09:53 +00002278 uint8_t teardown = 0;
2279 int n;
2280 int count = 0;
2281
jjako52c24142002-12-16 13:33:51 +00002282 /* Is this a dublicate ? */
jjako08d331d2003-10-13 20:33:30 +00002283 if(!gtp_dublicate(gsn, version, peer, seq)) {
2284 return 0; /* We allready send off response once */
jjako52c24142002-12-16 13:33:51 +00002285 }
2286
jjako2c381332003-10-21 19:09:53 +00002287 /* Find the linked context in question */
2288 if (pdp_getgtp1(&linked_pdp, get_tei(pack))) {
jjako52c24142002-12-16 13:33:51 +00002289 gsn->err_unknownpdp++;
2290 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2291 "Unknown PDP context");
jjako2c381332003-10-21 19:09:53 +00002292 return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, NULL, NULL,
2293 GTPCAUSE_NON_EXIST, teardown);
jjako52c24142002-12-16 13:33:51 +00002294 }
jjako2c381332003-10-21 19:09:53 +00002295
2296 /* If version 0 this is also the secondary context */
2297 if (version == 0)
2298 pdp = linked_pdp;
jjako52c24142002-12-16 13:33:51 +00002299
2300 /* Decode information elements */
jjako08d331d2003-10-13 20:33:30 +00002301 if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) {
jjako52c24142002-12-16 13:33:51 +00002302 gsn->invalid++;
2303 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2304 "Invalid message format");
2305 if (0 == version)
2306 return EOF;
2307 else
jjako2c381332003-10-21 19:09:53 +00002308 return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, NULL, NULL,
2309 GTPCAUSE_INVALID_MESSAGE, teardown);
jjako52c24142002-12-16 13:33:51 +00002310 }
jjako2c381332003-10-21 19:09:53 +00002311
jjako08d331d2003-10-13 20:33:30 +00002312 if (version == 1) {
2313 /* NSAPI (mandatory) */
2314 if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &nsapi)) {
2315 gsn->missing++;
2316 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2317 "Missing mandatory information field");
jjako2c381332003-10-21 19:09:53 +00002318 return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, NULL, NULL,
2319 GTPCAUSE_MAN_IE_MISSING, teardown);
jjako08d331d2003-10-13 20:33:30 +00002320 }
jjako2c381332003-10-21 19:09:53 +00002321
2322 /* Find the context in question */
2323 if (pdp_getgtp1(&pdp, linked_pdp->secondary_tei[nsapi & 0x0f])) {
2324 gsn->err_unknownpdp++;
2325 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2326 "Unknown PDP context");
2327 return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, NULL, NULL,
2328 GTPCAUSE_NON_EXIST, teardown);
2329 }
jjako08d331d2003-10-13 20:33:30 +00002330
2331 /* Teardown (conditional) */
jjako2c381332003-10-21 19:09:53 +00002332 gtpie_gettv1(ie, GTPIE_TEARDOWN, 0, &teardown);
2333
2334 if (!teardown) {
2335 for (n=0; n< PDP_MAXNSAPI; n++)
2336 if (linked_pdp->secondary_tei[n]) count++;
2337 if (count <= 1) {
2338 return 0; /* 29.060 7.3.5 Ignore message */
2339 }
jjako08d331d2003-10-13 20:33:30 +00002340 }
2341 }
jjako2c381332003-10-21 19:09:53 +00002342
2343 return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len,
2344 pdp, linked_pdp, GTPCAUSE_ACC_REQ, teardown);
jjako52c24142002-12-16 13:33:51 +00002345}
2346
2347
2348/* Handle Delete PDP Context Response */
2349int gtp_delete_pdp_conf(struct gsn_t *gsn, int version,
2350 struct sockaddr_in *peer,
2351 void *pack, unsigned len) {
jjako52c24142002-12-16 13:33:51 +00002352 union gtpie_member *ie[GTPIE_SIZE];
2353 uint8_t cause;
jjako08d331d2003-10-13 20:33:30 +00002354 void *cbp = NULL;
jjako52c24142002-12-16 13:33:51 +00002355 uint8_t type = 0;
jjako08d331d2003-10-13 20:33:30 +00002356 int hlen = get_hlen(pack);
2357
jjako52c24142002-12-16 13:33:51 +00002358 /* Remove packet from queue */
jjako08d331d2003-10-13 20:33:30 +00002359 if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) return EOF;
jjako52c24142002-12-16 13:33:51 +00002360
jjako52c24142002-12-16 13:33:51 +00002361 /* Decode information elements */
jjako08d331d2003-10-13 20:33:30 +00002362 if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) {
jjako52c24142002-12-16 13:33:51 +00002363 gsn->invalid++;
2364 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2365 "Invalid message format");
jjako2c381332003-10-21 19:09:53 +00002366 if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +00002367 return EOF;
2368 }
2369
jjako08d331d2003-10-13 20:33:30 +00002370 /* Extract cause value (mandatory) */
jjako52c24142002-12-16 13:33:51 +00002371 if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) {
2372 gsn->missing++;
2373 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2374 "Missing mandatory information field");
jjako2c381332003-10-21 19:09:53 +00002375 if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +00002376 return EOF;
2377 }
2378
jjako2c381332003-10-21 19:09:53 +00002379 /* Check the cause value (again) */
2380 if ((GTPCAUSE_ACC_REQ != cause) && (GTPCAUSE_NON_EXIST != cause)) {
jjako52c24142002-12-16 13:33:51 +00002381 gsn->err_cause++;
2382 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2383 "Unexpected cause value received: %d", cause);
jjako2c381332003-10-21 19:09:53 +00002384 if (gsn->cb_conf) gsn->cb_conf(type, cause, NULL, cbp);
2385 return EOF;
jjako52c24142002-12-16 13:33:51 +00002386 }
jjako2c381332003-10-21 19:09:53 +00002387
jjako08d331d2003-10-13 20:33:30 +00002388 /* Callback function to notify application */
jjako2c381332003-10-21 19:09:53 +00002389 if (gsn->cb_conf) gsn->cb_conf(type, cause, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +00002390
2391 return 0;
2392}
2393
2394/* Send Error Indication (response to a GPDU message */
2395int gtp_error_ind_resp(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00002396 struct sockaddr_in *peer, int fd,
jjako52c24142002-12-16 13:33:51 +00002397 void *pack, unsigned len)
2398{
2399 union gtp_packet packet;
jjako08d331d2003-10-13 20:33:30 +00002400 int length = get_default_gtp(version, GTP_ERROR, &packet);
jjako52c24142002-12-16 13:33:51 +00002401
jjako08d331d2003-10-13 20:33:30 +00002402 return gtp_resp(version, gsn, NULL, &packet, length, peer, fd,
2403 get_seq(pack), get_tid(pack));
jjako52c24142002-12-16 13:33:51 +00002404}
2405
2406/* Handle Error Indication */
2407int gtp_error_ind_conf(struct gsn_t *gsn, int version,
2408 struct sockaddr_in *peer,
2409 void *pack, unsigned len) {
2410 struct pdp_t *pdp;
2411
2412 /* Find the context in question */
2413 if (pdp_tidget(&pdp, ((union gtp_packet*)pack)->gtp0.h.tid)) {
2414 gsn->err_unknownpdp++;
2415 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2416 "Unknown PDP context");
2417 return EOF;
2418 }
2419
2420 gsn->err_unknownpdp++; /* TODO: Change counter */
2421 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2422 "Received Error Indication");
2423
2424 if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2425 pdp_freepdp(pdp);
2426 return 0;
2427}
2428
2429int gtp_gpdu_ind(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00002430 struct sockaddr_in *peer, int fd,
2431 void *pack, unsigned len) {
2432
2433 int hlen = GTP1_HEADER_SIZE_SHORT;
jjako52c24142002-12-16 13:33:51 +00002434
2435 /* Need to include code to verify packet src and dest addresses */
2436 struct pdp_t *pdp;
jjako1db1c812003-07-06 20:53:57 +00002437
jjako08d331d2003-10-13 20:33:30 +00002438 if (version == 0) {
2439 if (pdp_getgtp0(&pdp, ntoh16(((union gtp_packet*)pack)->gtp0.h.flow))) {
2440 gsn->err_unknownpdp++;
2441 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2442 "Unknown PDP context");
2443 return gtp_error_ind_resp(gsn, version, peer, fd, pack, len);
2444 }
2445 hlen = GTP0_HEADER_SIZE;
2446 }
2447 else if (version == 1) {
2448 if (pdp_getgtp1(&pdp, ntoh32(((union gtp_packet*)pack)->gtp1l.h.tei))) {
2449 gsn->err_unknownpdp++;
2450 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2451 "Unknown PDP context");
2452 return gtp_error_ind_resp(gsn, version, peer, fd, pack, len);
2453 }
2454
2455 /* Is this a long or a short header ? */
2456 if (((union gtp_packet*)pack)->gtp1l.h.flags & 0x07)
2457 hlen = GTP1_HEADER_SIZE_LONG;
2458 else
2459 hlen = GTP1_HEADER_SIZE_SHORT;
2460 }
2461 else {
2462 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2463 "Unknown version");
2464 }
2465
jjako1db1c812003-07-06 20:53:57 +00002466 /* If the GPDU was not from the peer GSN tell him to delete context */
2467 if (memcmp(&peer->sin_addr, pdp->gsnru.v, pdp->gsnru.l)) { /* TODO Range? */
2468 gsn->err_unknownpdp++;
2469 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2470 "Unknown PDP context");
jjako08d331d2003-10-13 20:33:30 +00002471 return gtp_error_ind_resp(gsn, version, peer, fd, pack, len);
jjako1db1c812003-07-06 20:53:57 +00002472 }
jjako08d331d2003-10-13 20:33:30 +00002473
jjako52c24142002-12-16 13:33:51 +00002474 /* Callback function */
jjako08d331d2003-10-13 20:33:30 +00002475 if (gsn->cb_data_ind !=0)
2476 return gsn->cb_data_ind(pdp, pack+hlen, len-hlen);
jjako52c24142002-12-16 13:33:51 +00002477
2478 return 0;
2479}
2480
2481
jjako08d331d2003-10-13 20:33:30 +00002482
jjako52c24142002-12-16 13:33:51 +00002483/* Receives GTP packet and sends off for further processing
2484 * Function will check the validity of the header. If the header
2485 * is not valid the packet is either dropped or a version not
2486 * supported is returned to the peer.
2487 * TODO: Need to decide on return values! */
jjako08d331d2003-10-13 20:33:30 +00002488int gtp_decaps0(struct gsn_t *gsn)
jjako52c24142002-12-16 13:33:51 +00002489{
jjako2c381332003-10-21 19:09:53 +00002490 unsigned char buffer[PACKET_MAX];
jjako52c24142002-12-16 13:33:51 +00002491 struct sockaddr_in peer;
2492 int peerlen;
jjako08d331d2003-10-13 20:33:30 +00002493 int status;
jjako52c24142002-12-16 13:33:51 +00002494 struct gtp0_header *pheader;
2495 int version = 0; /* GTP version should be determined from header!*/
jjako08d331d2003-10-13 20:33:30 +00002496 int fd = gsn->fd0;
jjako52c24142002-12-16 13:33:51 +00002497
jjakoa7cd2492003-04-11 09:40:12 +00002498 /* TODO: Need strategy of userspace buffering and blocking */
2499 /* Currently read is non-blocking and send is blocking. */
2500 /* This means that the program have to wait for busy send calls...*/
jjako52c24142002-12-16 13:33:51 +00002501
jjakoa7cd2492003-04-11 09:40:12 +00002502 while (1) { /* Loop until no more to read */
jjako08d331d2003-10-13 20:33:30 +00002503 if (fcntl(gsn->fd0, F_SETFL, O_NONBLOCK)) {
jjakoa7cd2492003-04-11 09:40:12 +00002504 gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()");
2505 return -1;
2506 }
2507 peerlen = sizeof(peer);
2508 if ((status =
jjako08d331d2003-10-13 20:33:30 +00002509 recvfrom(gsn->fd0, buffer, sizeof(buffer), 0,
jjakoa7cd2492003-04-11 09:40:12 +00002510 (struct sockaddr *) &peer, &peerlen)) < 0 ) {
jjako08d331d2003-10-13 20:33:30 +00002511 if (errno == EAGAIN) return 0;
jjakoa7cd2492003-04-11 09:40:12 +00002512 gsn->err_readfrom++;
jjako08d331d2003-10-13 20:33:30 +00002513 gtp_err(LOG_ERR, __FILE__, __LINE__, "recvfrom(fd0=%d, buffer=%lx, len=%d) failed: status = %d error = %s", gsn->fd0, (unsigned long) buffer, sizeof(buffer), status, status ? strerror(errno) : "No error");
jjakoa7cd2492003-04-11 09:40:12 +00002514 return -1;
2515 }
jjako52c24142002-12-16 13:33:51 +00002516
jjakoa7cd2492003-04-11 09:40:12 +00002517 /* Need at least 1 byte in order to check version */
2518 if (status < (1)) {
2519 gsn->empty++;
2520 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2521 "Discarding packet - too small");
2522 continue;
2523 }
2524
jjako08d331d2003-10-13 20:33:30 +00002525 pheader = (struct gtp0_header *) (buffer);
jjakoa7cd2492003-04-11 09:40:12 +00002526
jjako08d331d2003-10-13 20:33:30 +00002527 /* Version should be gtp0 (or earlier) */
2528 /* 09.60 is somewhat unclear on this issue. On gsn->fd0 we expect only */
2529 /* GTP 0 messages. If other version message is received we reply that we */
2530 /* only support version 0, implying that this is the only version */
2531 /* supported on this port */
jjakoa7cd2492003-04-11 09:40:12 +00002532 if (((pheader->flags & 0xe0) > 0x00)) {
2533 gsn->unsup++;
2534 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2535 "Unsupported GTP version");
jjako08d331d2003-10-13 20:33:30 +00002536 gtp_unsup_req(gsn, 0, &peer, gsn->fd0, buffer, status); /* 29.60: 11.1.1 */
jjakoa7cd2492003-04-11 09:40:12 +00002537 continue;
2538 }
2539
2540 /* Check length of gtp0 packet */
jjako08d331d2003-10-13 20:33:30 +00002541 if (status < GTP0_HEADER_SIZE) {
jjakoa7cd2492003-04-11 09:40:12 +00002542 gsn->tooshort++;
2543 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2544 "GTP0 packet too short");
2545 continue; /* Silently discard 29.60: 11.1.2 */
2546 }
jjako1db1c812003-07-06 20:53:57 +00002547
jjako08d331d2003-10-13 20:33:30 +00002548 /* Check packet length field versus length of packet */
2549 if (status != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
2550 gsn->tooshort++;
2551 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2552 "GTP packet length field does not match actual length");
2553 continue; /* Silently discard */
2554 }
2555
2556 if ((gsn->mode == GTP_MODE_GGSN) &&
2557 ((pheader->type == GTP_CREATE_PDP_RSP) ||
2558 (pheader->type == GTP_UPDATE_PDP_RSP) ||
2559 (pheader->type == GTP_DELETE_PDP_RSP))) {
2560 gsn->unexpect++;
2561 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2562 "Unexpected GTP Signalling Message");
2563 continue; /* Silently discard 29.60: 11.1.4 */
2564 }
2565
2566 if ((gsn->mode == GTP_MODE_SGSN) &&
2567 ((pheader->type == GTP_CREATE_PDP_REQ) ||
2568 (pheader->type == GTP_UPDATE_PDP_REQ) ||
2569 (pheader->type == GTP_DELETE_PDP_REQ))) {
2570 gsn->unexpect++;
2571 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2572 "Unexpected GTP Signalling Message");
2573 continue; /* Silently discard 29.60: 11.1.4 */
2574 }
2575
2576 switch (pheader->type) {
2577 case GTP_ECHO_REQ:
2578 gtp_echo_ind(gsn, version, &peer, fd, buffer, status);
2579 break;
2580 case GTP_ECHO_RSP:
2581 gtp_echo_conf(gsn, version, &peer, buffer, status);
2582 break;
2583 case GTP_NOT_SUPPORTED:
2584 gtp_unsup_ind(gsn, &peer, buffer, status);
2585 break;
2586 case GTP_CREATE_PDP_REQ:
2587 gtp_create_pdp_ind(gsn, version, &peer, fd, buffer, status);
2588 break;
2589 case GTP_CREATE_PDP_RSP:
2590 gtp_create_pdp_conf(gsn, version, &peer, buffer, status);
2591 break;
2592 case GTP_UPDATE_PDP_REQ:
2593 gtp_update_pdp_ind(gsn, version, &peer, fd, buffer, status);
2594 break;
2595 case GTP_UPDATE_PDP_RSP:
2596 gtp_update_pdp_conf(gsn, version, &peer, buffer, status);
2597 break;
2598 case GTP_DELETE_PDP_REQ:
2599 gtp_delete_pdp_ind(gsn, version, &peer, fd, buffer, status);
2600 break;
2601 case GTP_DELETE_PDP_RSP:
2602 gtp_delete_pdp_conf(gsn, version, &peer, buffer, status);
2603 break;
2604 case GTP_ERROR:
2605 gtp_error_ind_conf(gsn, version, &peer, buffer, status);
2606 break;
2607 case GTP_GPDU:
2608 gtp_gpdu_ind(gsn, version, &peer, fd, buffer, status);
2609 break;
2610 default:
2611 gsn->unknown++;
2612 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2613 "Unknown GTP message type received");
2614 break;
2615 }
2616 }
2617}
2618
2619
2620int gtp_decaps1c(struct gsn_t *gsn)
2621{
jjako2c381332003-10-21 19:09:53 +00002622 unsigned char buffer[PACKET_MAX];
jjako08d331d2003-10-13 20:33:30 +00002623 struct sockaddr_in peer;
2624 int peerlen;
2625 int status;
2626 struct gtp1_header_short *pheader;
jjako2c381332003-10-21 19:09:53 +00002627 int version = 1; /* TODO GTP version should be determined from header!*/
jjako08d331d2003-10-13 20:33:30 +00002628 int fd = gsn->fd1c;
2629
2630 /* TODO: Need strategy of userspace buffering and blocking */
2631 /* Currently read is non-blocking and send is blocking. */
2632 /* This means that the program have to wait for busy send calls...*/
2633
2634 while (1) { /* Loop until no more to read */
jjako2c381332003-10-21 19:09:53 +00002635 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
jjako08d331d2003-10-13 20:33:30 +00002636 gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()");
2637 return -1;
2638 }
2639 peerlen = sizeof(peer);
2640 if ((status =
jjako2c381332003-10-21 19:09:53 +00002641 recvfrom(fd, buffer, sizeof(buffer), 0,
jjako08d331d2003-10-13 20:33:30 +00002642 (struct sockaddr *) &peer, &peerlen)) < 0 ) {
2643 if (errno == EAGAIN) return 0;
2644 gsn->err_readfrom++;
jjako2c381332003-10-21 19:09:53 +00002645 gtp_err(LOG_ERR, __FILE__, __LINE__, "recvfrom(fd=%d, buffer=%lx, len=%d) failed: status = %d error = %s", fd, (unsigned long) buffer, sizeof(buffer), status, status ? strerror(errno) : "No error");
jjako08d331d2003-10-13 20:33:30 +00002646 return -1;
2647 }
2648
2649 /* Need at least 1 byte in order to check version */
2650 if (status < (1)) {
2651 gsn->empty++;
2652 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2653 "Discarding packet - too small");
2654 continue;
2655 }
2656
2657 pheader = (struct gtp1_header_short *) (buffer);
2658
2659 /* Version must be no larger than GTP 1 */
2660 if (((pheader->flags & 0xe0) > 0x20)) {
2661 gsn->unsup++;
2662 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2663 "Unsupported GTP version");
jjako2c381332003-10-21 19:09:53 +00002664 gtp_unsup_req(gsn, version, &peer, fd, buffer, status);
2665 /*29.60: 11.1.1*/
jjako08d331d2003-10-13 20:33:30 +00002666 continue;
2667 }
2668
2669 /* Version must be at least GTP 1 */
2670 /* 29.060 is somewhat unclear on this issue. On gsn->fd1c we expect only */
2671 /* GTP 1 messages. If GTP 0 message is received we silently discard */
2672 /* the message */
2673 if (((pheader->flags & 0xe0) < 0x20)) {
2674 gsn->unsup++;
2675 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2676 "Unsupported GTP version");
2677 continue;
2678 }
2679
2680 /* Check packet flag field */
2681 if (((pheader->flags & 0xf7) != 0x32)) {
2682 gsn->unsup++;
2683 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2684 "Unsupported packet flag");
2685 continue;
2686 }
2687
2688 /* Check length of packet */
2689 if (status < GTP1_HEADER_SIZE_LONG) {
2690 gsn->tooshort++;
2691 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2692 "GTP packet too short");
2693 continue; /* Silently discard 29.60: 11.1.2 */
2694 }
2695
2696 /* Check packet length field versus length of packet */
2697 if (status != (ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT)) {
2698 gsn->tooshort++;
2699 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2700 "GTP packet length field does not match actual length");
2701 continue; /* Silently discard */
2702 }
2703
jjako2c381332003-10-21 19:09:53 +00002704 /* Check for extension headers */
2705 /* TODO: We really should cycle through the headers and determine */
2706 /* if any have the comprehension required flag set */
2707 if (((pheader->flags & 0x04) != 0x00)) {
2708 gsn->unsup++;
2709 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2710 "Unsupported extension header");
2711 gtp_extheader_req(gsn, version, &peer, fd, buffer, status);
2712
2713 continue;
2714 }
2715
jjako1db1c812003-07-06 20:53:57 +00002716 if ((gsn->mode == GTP_MODE_GGSN) &&
2717 ((pheader->type == GTP_CREATE_PDP_RSP) ||
2718 (pheader->type == GTP_UPDATE_PDP_RSP) ||
2719 (pheader->type == GTP_DELETE_PDP_RSP))) {
2720 gsn->unexpect++;
2721 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2722 "Unexpected GTP Signalling Message");
2723 continue; /* Silently discard 29.60: 11.1.4 */
2724 }
2725
2726 if ((gsn->mode == GTP_MODE_SGSN) &&
2727 ((pheader->type == GTP_CREATE_PDP_REQ) ||
2728 (pheader->type == GTP_UPDATE_PDP_REQ) ||
2729 (pheader->type == GTP_DELETE_PDP_REQ))) {
2730 gsn->unexpect++;
2731 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2732 "Unexpected GTP Signalling Message");
2733 continue; /* Silently discard 29.60: 11.1.4 */
2734 }
2735
jjakoa7cd2492003-04-11 09:40:12 +00002736 switch (pheader->type) {
2737 case GTP_ECHO_REQ:
jjako08d331d2003-10-13 20:33:30 +00002738 gtp_echo_ind(gsn, version, &peer, fd, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002739 break;
2740 case GTP_ECHO_RSP:
jjako08d331d2003-10-13 20:33:30 +00002741 gtp_echo_conf(gsn, version, &peer, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002742 break;
2743 case GTP_NOT_SUPPORTED:
jjako08d331d2003-10-13 20:33:30 +00002744 gtp_unsup_ind(gsn, &peer, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002745 break;
jjako2c381332003-10-21 19:09:53 +00002746 case GTP_SUPP_EXT_HEADER:
2747 gtp_extheader_ind(gsn, &peer, buffer, status);
2748 break;
jjakoa7cd2492003-04-11 09:40:12 +00002749 case GTP_CREATE_PDP_REQ:
jjako08d331d2003-10-13 20:33:30 +00002750 gtp_create_pdp_ind(gsn, version, &peer, fd, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002751 break;
2752 case GTP_CREATE_PDP_RSP:
jjako08d331d2003-10-13 20:33:30 +00002753 gtp_create_pdp_conf(gsn, version, &peer, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002754 break;
2755 case GTP_UPDATE_PDP_REQ:
jjako08d331d2003-10-13 20:33:30 +00002756 gtp_update_pdp_ind(gsn, version, &peer, fd, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002757 break;
2758 case GTP_UPDATE_PDP_RSP:
jjako08d331d2003-10-13 20:33:30 +00002759 gtp_update_pdp_conf(gsn, version, &peer, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002760 break;
2761 case GTP_DELETE_PDP_REQ:
jjako08d331d2003-10-13 20:33:30 +00002762 gtp_delete_pdp_ind(gsn, version, &peer, fd, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002763 break;
2764 case GTP_DELETE_PDP_RSP:
jjako08d331d2003-10-13 20:33:30 +00002765 gtp_delete_pdp_conf(gsn, version, &peer, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002766 break;
2767 case GTP_ERROR:
jjako08d331d2003-10-13 20:33:30 +00002768 gtp_error_ind_conf(gsn, version, &peer, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002769 break;
2770 default:
jjako52c24142002-12-16 13:33:51 +00002771 gsn->unknown++;
2772 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2773 "Unknown GTP message type received");
jjakoa7cd2492003-04-11 09:40:12 +00002774 break;
jjako52c24142002-12-16 13:33:51 +00002775 }
2776 }
2777}
2778
jjako08d331d2003-10-13 20:33:30 +00002779int gtp_decaps1u(struct gsn_t *gsn)
2780{
jjako2c381332003-10-21 19:09:53 +00002781 unsigned char buffer[PACKET_MAX];
jjako08d331d2003-10-13 20:33:30 +00002782 struct sockaddr_in peer;
2783 int peerlen;
2784 int status;
2785 struct gtp1_header_short *pheader;
2786 int version = 1; /* GTP version should be determined from header!*/
2787 int fd = gsn->fd1u;
2788
2789 /* TODO: Need strategy of userspace buffering and blocking */
2790 /* Currently read is non-blocking and send is blocking. */
2791 /* This means that the program have to wait for busy send calls...*/
2792
2793 while (1) { /* Loop until no more to read */
2794 if (fcntl(gsn->fd1u, F_SETFL, O_NONBLOCK)) {
2795 gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()");
2796 return -1;
2797 }
2798 peerlen = sizeof(peer);
2799 if ((status =
2800 recvfrom(gsn->fd1u, buffer, sizeof(buffer), 0,
2801 (struct sockaddr *) &peer, &peerlen)) < 0 ) {
2802 if (errno == EAGAIN) return 0;
2803 gsn->err_readfrom++;
2804 gtp_err(LOG_ERR, __FILE__, __LINE__, "recvfrom(fd1u=%d, buffer=%lx, len=%d) failed: status = %d error = %s", gsn->fd1u, (unsigned long) buffer, sizeof(buffer), status, status ? strerror(errno) : "No error");
2805 return -1;
2806 }
2807
2808 /* Need at least 1 byte in order to check version */
2809 if (status < (1)) {
2810 gsn->empty++;
2811 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2812 "Discarding packet - too small");
2813 continue;
2814 }
2815
2816 pheader = (struct gtp1_header_short *) (buffer);
2817
2818 /* Version must be no larger than GTP 1 */
2819 if (((pheader->flags & 0xe0) > 0x20)) {
2820 gsn->unsup++;
2821 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2822 "Unsupported GTP version");
2823 gtp_unsup_req(gsn, 1, &peer, gsn->fd1c, buffer, status);/*29.60: 11.1.1*/
2824 continue;
2825 }
2826
2827 /* Version must be at least GTP 1 */
2828 /* 29.060 is somewhat unclear on this issue. On gsn->fd1c we expect only */
2829 /* GTP 1 messages. If GTP 0 message is received we silently discard */
2830 /* the message */
2831 if (((pheader->flags & 0xe0) < 0x20)) {
2832 gsn->unsup++;
2833 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2834 "Unsupported GTP version");
2835 continue;
2836 }
2837
2838 /* Check packet flag field (allow both with and without sequence number)*/
2839 if (((pheader->flags & 0xf5) != 0x30)) {
2840 gsn->unsup++;
2841 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2842 "Unsupported packet flag");
2843 continue;
2844 }
2845
2846 /* Check length of packet */
2847 if (status < GTP1_HEADER_SIZE_SHORT) {
2848 gsn->tooshort++;
2849 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2850 "GTP packet too short");
2851 continue; /* Silently discard 29.60: 11.1.2 */
2852 }
2853
2854 /* Check packet length field versus length of packet */
2855 if (status != (ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT)) {
2856 gsn->tooshort++;
2857 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2858 "GTP packet length field does not match actual length");
2859 continue; /* Silently discard */
2860 }
jjako2c381332003-10-21 19:09:53 +00002861
2862 /* Check for extension headers */
2863 /* TODO: We really should cycle through the headers and determine */
2864 /* if any have the comprehension required flag set */
2865 if (((pheader->flags & 0x04) != 0x00)) {
2866 gsn->unsup++;
2867 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2868 "Unsupported extension header");
2869 gtp_extheader_req(gsn, version, &peer, fd, buffer, status);
2870
2871 continue;
2872 }
jjako08d331d2003-10-13 20:33:30 +00002873
2874 switch (pheader->type) {
2875 case GTP_ECHO_REQ:
2876 gtp_echo_ind(gsn, version, &peer, fd, buffer, status);
2877 break;
2878 case GTP_ECHO_RSP:
2879 gtp_echo_conf(gsn, version, &peer, buffer, status);
2880 break;
jjako2c381332003-10-21 19:09:53 +00002881 case GTP_SUPP_EXT_HEADER:
2882 gtp_extheader_ind(gsn, &peer, buffer, status);
2883 break;
jjako08d331d2003-10-13 20:33:30 +00002884 case GTP_ERROR:
2885 gtp_error_ind_conf(gsn, version, &peer, buffer, status);
2886 break;
2887 /* Supported header extensions */
2888 case GTP_GPDU:
2889 gtp_gpdu_ind(gsn, version, &peer, fd, buffer, status);
2890 break;
2891 default:
2892 gsn->unknown++;
2893 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2894 "Unknown GTP message type received");
2895 break;
2896 }
2897 }
2898}
2899
2900int gtp_data_req(struct gsn_t *gsn, struct pdp_t* pdp,
jjako52c24142002-12-16 13:33:51 +00002901 void *pack, unsigned len)
2902{
2903 union gtp_packet packet;
2904 struct sockaddr_in addr;
jjako08d331d2003-10-13 20:33:30 +00002905 int fd;
2906 int length;
jjako52c24142002-12-16 13:33:51 +00002907
2908 memset(&addr, 0, sizeof(addr));
2909 addr.sin_family = AF_INET;
jjako52c24142002-12-16 13:33:51 +00002910 memcpy(&addr.sin_addr, pdp->gsnru.v,pdp->gsnru.l); /* TODO range check */
jjako52c24142002-12-16 13:33:51 +00002911
jjako08d331d2003-10-13 20:33:30 +00002912 if (pdp->version == 0) {
2913
2914 length = GTP0_HEADER_SIZE+len;
2915 addr.sin_port = htons(GTP0_PORT);
2916 fd = gsn->fd0;
2917
2918 get_default_gtp(0, GTP_GPDU, &packet);
2919 packet.gtp0.h.length = hton16(len);
2920 packet.gtp0.h.seq = hton16(pdp->gtpsntx++);
2921 packet.gtp0.h.flow = hton16(pdp->flru);
2922 packet.gtp0.h.tid = (pdp->imsi & 0x0fffffffffffffff) + ((uint64_t)pdp->nsapi << 60);
jjako52c24142002-12-16 13:33:51 +00002923
jjako08d331d2003-10-13 20:33:30 +00002924 if (len > sizeof (union gtp_packet) - sizeof(struct gtp0_header)) {
2925 gsn->err_memcpy++;
2926 gtp_err(LOG_ERR, __FILE__, __LINE__,
2927 "Memcpy failed");
2928 return EOF;
jjako52c24142002-12-16 13:33:51 +00002929 }
jjako08d331d2003-10-13 20:33:30 +00002930 memcpy(packet.gtp0.p, pack, len); /* TODO Should be avoided! */
2931 }
2932 else if (pdp->version == 1) {
2933
2934 length = GTP1_HEADER_SIZE_LONG+len;
2935 addr.sin_port = htons(GTP1U_PORT);
2936 fd = gsn->fd1u;
jjako52c24142002-12-16 13:33:51 +00002937
jjako08d331d2003-10-13 20:33:30 +00002938 get_default_gtp(1, GTP_GPDU, &packet);
2939 packet.gtp1l.h.length = hton16(len-GTP1_HEADER_SIZE_SHORT+
2940 GTP1_HEADER_SIZE_LONG);
2941 packet.gtp1l.h.seq = hton16(pdp->gtpsntx++);
jjako2c381332003-10-21 19:09:53 +00002942 packet.gtp1l.h.tei = hton32(pdp->teid_gn);
jjako08d331d2003-10-13 20:33:30 +00002943
2944 if (len > sizeof (union gtp_packet) - sizeof(struct gtp1_header_long)) {
2945 gsn->err_memcpy++;
2946 gtp_err(LOG_ERR, __FILE__, __LINE__,
2947 "Memcpy failed");
2948 return EOF;
2949 }
2950 memcpy(packet.gtp1l.p, pack, len); /* TODO Should be avoided! */
2951 }
2952 else {
2953 gtp_err(LOG_ERR, __FILE__, __LINE__,
2954 "Unknown version");
2955 return EOF;
2956 }
2957
2958 if (fcntl(fd, F_SETFL, 0)) {
jjakoa7cd2492003-04-11 09:40:12 +00002959 gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()");
2960 return -1;
2961 }
2962
jjako08d331d2003-10-13 20:33:30 +00002963 if (sendto(fd, &packet, length, 0,
jjako52c24142002-12-16 13:33:51 +00002964 (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2965 gsn->err_sendto++;
jjako08d331d2003-10-13 20:33:30 +00002966 gtp_err(LOG_ERR, __FILE__, __LINE__, "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s", fd, (unsigned long) &packet, GTP0_HEADER_SIZE+len, strerror(errno));
jjako52c24142002-12-16 13:33:51 +00002967 return EOF;
2968 }
2969 return 0;
2970}
2971
2972
2973/* ***********************************************************
2974 * Conversion functions
2975 *************************************************************/
2976
2977int char2ul_t(char* src, struct ul_t dst) {
2978 dst.l = strlen(src)+1;
2979 dst.v = malloc(dst.l);
2980 dst.v[0] = dst.l - 1;
2981 memcpy(&dst.v[1], src, dst.v[0]);
2982 return 0;
2983}
2984
2985/* ***********************************************************
2986 * IP address conversion functions
2987 * There exist several types of address representations:
2988 * - eua: End User Address. (29.060, 7.7.27, message type 128)
2989 * Used for signalling address to mobile station. Supports IPv4
2990 * IPv6 x.25 etc. etc.
2991 * - gsna: GSN Address. (29.060, 7.7.32, message type 133): IP address
2992 * of GSN. If length is 4 it is IPv4. If length is 16 it is IPv6.
2993 * - in_addr: IPv4 address struct.
2994 * - sockaddr_in: Socket API representation of IP address and
2995 * port number.
2996 *************************************************************/
2997
2998int ipv42eua(struct ul66_t *eua, struct in_addr *src) {
2999 eua->v[0] = 0xf1; /* IETF */
3000 eua->v[1] = 0x21; /* IPv4 */
3001 if (src) {
3002 eua->l = 6;
3003 memcpy(&eua->v[2], src, 4);
3004 }
3005 else
3006 {
3007 eua->l = 2;
3008 }
3009 return 0;
3010}
3011
3012int eua2ipv4(struct in_addr *dst, struct ul66_t *eua) {
3013 if ((eua->l != 6) ||
3014 (eua->v[0] != 0xf1) ||
3015 (eua->v[1] = 0x21))
3016 return -1; /* Not IPv4 address*/
3017 memcpy(dst, &eua->v[2], 4);
3018 return 0;
3019}
3020
3021int gsna2in_addr(struct in_addr *dst, struct ul16_t *gsna) {
3022 memset(dst, 0, sizeof(struct in_addr));
3023 if (gsna->l != 4) return EOF; /* Return if not IPv4 */
3024 memcpy(dst, gsna->v, gsna->l);
3025 return 0;
3026}
3027
3028int in_addr2gsna(struct ul16_t *gsna, struct in_addr *src) {
3029 memset(gsna, 0, sizeof(struct ul16_t));
3030 gsna->l = 4;
3031 memcpy(gsna->v, src, gsna->l);
3032 return 0;
3033}
3034