blob: dd552f707ab72d0b3706e76251d74cbcdf3f9e9d [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
jjakobae2cd42004-01-09 12:04:39 +000049/* #include <stdint.h> ISO C99 types */
jjako52c24142002-12-16 13:33:51 +000050
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 **/
jjakob7b93fc2004-01-09 11:56:48 +0000182static int get_default_gtp(int version, uint8_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,
jjako193e8b12003-11-10 12:31:41 +0000999 void *cbp) {
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
jjako193e8b12003-11-10 12:31:41 +00001110 /* TODO hisaddr0 */
1111 gtp_req(gsn, pdp->version, pdp, &packet, length, &pdp->hisaddr0, cbp);
jjako52c24142002-12-16 13:33:51 +00001112
1113 return 0;
1114}
1115
jjako08d331d2003-10-13 20:33:30 +00001116/* API: Application response to context indication */
1117int gtp_create_context_resp(struct gsn_t *gsn, struct pdp_t *pdp, int cause) {
1118
1119 /* Now send off a reply to the peer */
1120 gtp_create_pdp_resp(gsn, pdp->version, pdp, cause);
1121
1122 if (cause != GTPCAUSE_ACC_REQ) {
1123 pdp_freepdp(pdp);
1124 }
1125
1126 return 0;
1127}
1128
1129/* API: Register create context indication callback */
1130int gtp_set_cb_create_context_ind(struct gsn_t *gsn,
1131 int (*cb_create_context_ind) (struct pdp_t* pdp))
jjako52c24142002-12-16 13:33:51 +00001132{
jjako08d331d2003-10-13 20:33:30 +00001133 gsn->cb_create_context_ind = cb_create_context_ind;
1134 return 0;
1135}
1136
1137
1138/* Send Create PDP Context Response */
1139int gtp_create_pdp_resp(struct gsn_t *gsn, int version, struct pdp_t *pdp,
1140 uint8_t cause) {
jjako52c24142002-12-16 13:33:51 +00001141 union gtp_packet packet;
jjako08d331d2003-10-13 20:33:30 +00001142 int length = get_default_gtp(version, GTP_CREATE_PDP_RSP, &packet);
jjako52c24142002-12-16 13:33:51 +00001143
jjako08d331d2003-10-13 20:33:30 +00001144 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause);
jjako52c24142002-12-16 13:33:51 +00001145
1146 if (cause == GTPCAUSE_ACC_REQ) {
jjako08d331d2003-10-13 20:33:30 +00001147
1148 if (version == 0)
1149 gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0,
1150 sizeof(pdp->qos_neg0), pdp->qos_neg0);
1151
1152 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_REORDER,
jjako52c24142002-12-16 13:33:51 +00001153 pdp->reorder);
jjako08d331d2003-10-13 20:33:30 +00001154 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY,
jjako52c24142002-12-16 13:33:51 +00001155 gsn->restart_counter);
jjako08d331d2003-10-13 20:33:30 +00001156
1157 if (version == 0) {
1158 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI,
1159 pdp->fllu);
1160 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C,
1161 pdp->fllc);
1162 }
1163
1164 if (version == 1) {
1165 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI,
1166 pdp->teid_own);
1167 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C,
1168 pdp->teic_own);
1169 }
1170
jjako2c381332003-10-21 19:09:53 +00001171 /* TODO: We use teic_own as charging ID */
jjako08d331d2003-10-13 20:33:30 +00001172 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_CHARGING_ID,
jjako2c381332003-10-21 19:09:53 +00001173 pdp->teic_own);
1174
jjako08d331d2003-10-13 20:33:30 +00001175 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA,
jjako52c24142002-12-16 13:33:51 +00001176 pdp->eua.l, pdp->eua.v);
1177
1178 if (pdp->pco_neg.l) { /* Optional PCO */
jjako08d331d2003-10-13 20:33:30 +00001179 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_PCO,
jjako52c24142002-12-16 13:33:51 +00001180 pdp->pco_neg.l, pdp->pco_neg.v);
1181 }
1182
jjako08d331d2003-10-13 20:33:30 +00001183 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
jjako52c24142002-12-16 13:33:51 +00001184 pdp->gsnlc.l, pdp->gsnlc.v);
jjako08d331d2003-10-13 20:33:30 +00001185 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
jjako52c24142002-12-16 13:33:51 +00001186 pdp->gsnlu.l, pdp->gsnlu.v);
jjako08d331d2003-10-13 20:33:30 +00001187
1188 if (version == 1)
1189 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE,
1190 pdp->qos_neg.l, pdp->qos_neg.v);
1191
1192 /* TODO: Charging gateway address */
jjako52c24142002-12-16 13:33:51 +00001193 }
1194
jjako08d331d2003-10-13 20:33:30 +00001195 return gtp_resp(version, gsn, pdp, &packet, length, &pdp->sa_peer,
1196 pdp->fd, pdp->seq, pdp->tid);
jjako52c24142002-12-16 13:33:51 +00001197}
1198
1199/* Handle Create PDP Context Request */
1200int gtp_create_pdp_ind(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00001201 struct sockaddr_in *peer, int fd,
1202 void *pack, unsigned len) {
jjako52c24142002-12-16 13:33:51 +00001203 struct pdp_t *pdp, *pdp_old;
1204 struct pdp_t pdp_buf;
1205 union gtpie_member* ie[GTPIE_SIZE];
1206 uint8_t recovery;
jjako52c24142002-12-16 13:33:51 +00001207
jjako08d331d2003-10-13 20:33:30 +00001208 uint16_t seq = get_seq(pack);
1209 int hlen = get_hlen(pack);
jjako2c381332003-10-21 19:09:53 +00001210 uint8_t linked_nsapi = 0;
1211 struct pdp_t *linked_pdp = NULL;
jjako52c24142002-12-16 13:33:51 +00001212
jjako2c381332003-10-21 19:09:53 +00001213 if(!gtp_dublicate(gsn, version, peer, seq)) return 0;
jjako08d331d2003-10-13 20:33:30 +00001214
1215 pdp = &pdp_buf;
1216 memset(pdp, 0, sizeof(struct pdp_t));
1217
1218 if (version == 0) {
1219 pdp->imsi = ((union gtp_packet*)pack)->gtp0.h.tid & 0x0fffffffffffffff;
1220 pdp->nsapi = (((union gtp_packet*)pack)->gtp0.h.tid & 0xf000000000000000) >> 60;
jjako52c24142002-12-16 13:33:51 +00001221 }
1222
jjako08d331d2003-10-13 20:33:30 +00001223 pdp->seq = seq;
1224 pdp->sa_peer = *peer;
1225 pdp->fd = fd;
1226 pdp->version = version;
1227
jjako52c24142002-12-16 13:33:51 +00001228 /* Decode information elements */
jjako08d331d2003-10-13 20:33:30 +00001229 if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) {
jjako52c24142002-12-16 13:33:51 +00001230 gsn->invalid++;
1231 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1232 "Invalid message format");
1233 if (0 == version)
1234 return EOF;
1235 else
jjako08d331d2003-10-13 20:33:30 +00001236 return gtp_create_pdp_resp(gsn, version, pdp, GTPCAUSE_INVALID_MESSAGE);
jjako52c24142002-12-16 13:33:51 +00001237 }
1238
jjako2c381332003-10-21 19:09:53 +00001239 if (version == 1) {
1240 /* Linked NSAPI (conditional) */
1241 /* If included this is the Secondary PDP Context Activation Procedure */
1242 /* In secondary activation IMSI is not included, so the context must be */
1243 /* identified by the tei */
1244 if (!gtpie_gettv1(ie, GTPIE_NSAPI, 1, &linked_nsapi)) {
1245
1246 /* Find the primary PDP context */
1247 if (pdp_getgtp1(&linked_pdp, get_tei(pack))) {
1248 gsn->incorrect++;
1249 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1250 "Incorrect optional information field");
1251 return gtp_create_pdp_resp(gsn, version, pdp,
1252 GTPCAUSE_OPT_IE_INCORRECT);
1253 }
1254
1255 /* Check that the primary PDP context matches linked nsapi */
1256 if (linked_pdp->nsapi != linked_nsapi) {
1257 gsn->incorrect++;
1258 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1259 "Incorrect optional information field");
1260 return gtp_create_pdp_resp(gsn, version, pdp,
1261 GTPCAUSE_OPT_IE_INCORRECT);
1262 }
1263
1264 /* Copy parameters from primary context */
1265 pdp->selmode = linked_pdp->selmode;
1266 pdp->imsi = linked_pdp->imsi;
1267 pdp->msisdn = linked_pdp->msisdn;
1268 pdp->eua = linked_pdp->eua;
1269 pdp->pco_req = linked_pdp->pco_req;
1270 pdp->apn_req = linked_pdp->apn_req;
1271 pdp->teic_gn = linked_pdp->teic_gn;
1272 pdp->secondary = 1;
1273 }
1274 } /* if (version == 1) */
1275
jjako08d331d2003-10-13 20:33:30 +00001276 if (version == 0) {
1277 if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0,
1278 pdp->qos_req0, sizeof(pdp->qos_req0))) {
1279 gsn->missing++;
1280 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1281 "Missing mandatory information field");
1282 return gtp_create_pdp_resp(gsn, version, pdp, GTPCAUSE_MAN_IE_MISSING);
1283 }
jjako52c24142002-12-16 13:33:51 +00001284 }
jjako2c381332003-10-21 19:09:53 +00001285
1286 if ((version == 1) && (!linked_pdp)) {
1287 /* Not Secondary PDP Context Activation Procedure */
jjako08d331d2003-10-13 20:33:30 +00001288 /* IMSI (conditional) */
1289 if (gtpie_gettv0(ie, GTPIE_IMSI, 0, &pdp->imsi, sizeof(pdp->imsi))) {
1290 gsn->missing++;
1291 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1292 "Missing mandatory information field");
1293 return gtp_create_pdp_resp(gsn, version, pdp,
1294 GTPCAUSE_MAN_IE_MISSING);
1295 }
1296 }
jjako2c381332003-10-21 19:09:53 +00001297
jjako08d331d2003-10-13 20:33:30 +00001298 /* Recovery (optional) */
jjako52c24142002-12-16 13:33:51 +00001299 if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) {
1300 /* TODO: Handle received recovery IE */
1301 }
jjako2c381332003-10-21 19:09:53 +00001302
jjako08d331d2003-10-13 20:33:30 +00001303 /* Selection mode (conditional) */
jjako2c381332003-10-21 19:09:53 +00001304 if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */
1305 if (gtpie_gettv0(ie, GTPIE_SELECTION_MODE, 0,
1306 &pdp->selmode, sizeof(pdp->selmode))) {
1307 gsn->missing++;
1308 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1309 "Missing mandatory information field");
1310 return gtp_create_pdp_resp(gsn, version, pdp,
1311 GTPCAUSE_MAN_IE_MISSING);
1312 }
jjako52c24142002-12-16 13:33:51 +00001313 }
1314
jjako08d331d2003-10-13 20:33:30 +00001315 if (version == 0) {
1316 if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) {
1317 gsn->missing++;
1318 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1319 "Missing mandatory information field");
1320 return gtp_create_pdp_resp(gsn, version, pdp,
1321 GTPCAUSE_MAN_IE_MISSING);
1322 }
1323
1324 if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) {
1325 gsn->missing++;
1326 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1327 "Missing mandatory information field");
1328 return gtp_create_pdp_resp(gsn, version, pdp,
1329 GTPCAUSE_MAN_IE_MISSING);
1330 }
jjako52c24142002-12-16 13:33:51 +00001331 }
jjako2c381332003-10-21 19:09:53 +00001332
1333
jjako08d331d2003-10-13 20:33:30 +00001334 if (version == 1) {
1335 /* TEID (mandatory) */
1336 if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) {
1337 gsn->missing++;
1338 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1339 "Missing mandatory information field");
1340 return gtp_create_pdp_resp(gsn, version, pdp,
1341 GTPCAUSE_MAN_IE_MISSING);
1342 }
1343
1344 /* TEIC (conditional) */
jjako2c381332003-10-21 19:09:53 +00001345 if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */
1346 if (gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn)) {
1347 gsn->missing++;
1348 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1349 "Missing mandatory information field");
1350 return gtp_create_pdp_resp(gsn, version, pdp,
1351 GTPCAUSE_MAN_IE_MISSING);
1352 }
jjako08d331d2003-10-13 20:33:30 +00001353 }
jjako52c24142002-12-16 13:33:51 +00001354 }
1355
jjako2c381332003-10-21 19:09:53 +00001356 /* NSAPI (mandatory) */
1357 if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &pdp->nsapi)) {
1358 gsn->missing++;
1359 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1360 "Missing mandatory information field");
1361 return gtp_create_pdp_resp(gsn, version, pdp,
1362 GTPCAUSE_MAN_IE_MISSING);
1363 }
1364
1365
jjako08d331d2003-10-13 20:33:30 +00001366 /* Charging Characteriatics (optional) */
1367 /* Trace reference (optional) */
1368 /* Trace type (optional) */
1369 /* Charging Characteriatics (optional) */
jjako2c381332003-10-21 19:09:53 +00001370
1371 if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */
1372 /* End User Address (conditional) */
1373 if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l,
jjako52c24142002-12-16 13:33:51 +00001374 &pdp->eua.v, sizeof(pdp->eua.v))) {
jjako2c381332003-10-21 19:09:53 +00001375 gsn->missing++;
1376 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1377 "Missing mandatory information field");
1378 return gtp_create_pdp_resp(gsn, version, pdp,
1379 GTPCAUSE_MAN_IE_MISSING);
1380 }
1381
1382 /* APN */
1383 if (gtpie_gettlv(ie, GTPIE_APN, 0, &pdp->apn_req.l,
jjako52c24142002-12-16 13:33:51 +00001384 &pdp->apn_req.v, sizeof(pdp->apn_req.v))) {
jjako2c381332003-10-21 19:09:53 +00001385 gsn->missing++;
1386 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1387 "Missing mandatory information field");
1388 return gtp_create_pdp_resp(gsn, version, pdp,
1389 GTPCAUSE_MAN_IE_MISSING);
1390 }
1391
1392 /* Extract protocol configuration options (optional) */
1393 if (!gtpie_gettlv(ie, GTPIE_PCO, 0, &pdp->pco_req.l,
1394 &pdp->pco_req.v, sizeof(pdp->pco_req.v))) {
1395 }
jjako52c24142002-12-16 13:33:51 +00001396 }
1397
jjako08d331d2003-10-13 20:33:30 +00001398 /* SGSN address for signalling (mandatory) */
jjako52c24142002-12-16 13:33:51 +00001399 if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l,
1400 &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) {
1401 gsn->missing++;
1402 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1403 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00001404 return gtp_create_pdp_resp(gsn, version, pdp,
jjako52c24142002-12-16 13:33:51 +00001405 GTPCAUSE_MAN_IE_MISSING);
1406 }
1407
jjako08d331d2003-10-13 20:33:30 +00001408 /* SGSN address for user traffic (mandatory) */
jjako52c24142002-12-16 13:33:51 +00001409 if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l,
1410 &pdp->gsnru.v, sizeof(pdp->gsnru.v))) {
1411 gsn->missing++;
1412 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1413 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00001414 return gtp_create_pdp_resp(gsn, version, pdp,
jjako52c24142002-12-16 13:33:51 +00001415 GTPCAUSE_MAN_IE_MISSING);
1416 }
1417
jjako2c381332003-10-21 19:09:53 +00001418 if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */
1419 /* MSISDN (conditional) */
1420 if (gtpie_gettlv(ie, GTPIE_MSISDN, 0, &pdp->msisdn.l,
1421 &pdp->msisdn.v, sizeof(pdp->msisdn.v))) {
1422 gsn->missing++;
1423 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1424 "Missing mandatory information field");
1425 return gtp_create_pdp_resp(gsn, version, pdp,
1426 GTPCAUSE_MAN_IE_MISSING);
1427 }
jjako52c24142002-12-16 13:33:51 +00001428 }
1429
jjako08d331d2003-10-13 20:33:30 +00001430 if (version == 1) {
1431 /* QoS (mandatory) */
1432 if (gtpie_gettlv(ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_req.l,
1433 &pdp->qos_req.v, sizeof(pdp->qos_req.v))) {
1434 gsn->missing++;
1435 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1436 "Missing mandatory information field");
1437 return gtp_create_pdp_resp(gsn, version, pdp,
1438 GTPCAUSE_MAN_IE_MISSING);
1439 }
1440
1441 /* TFT (conditional) */
1442 if (gtpie_gettlv(ie, GTPIE_TFT, 0, &pdp->tft.l,
1443 &pdp->tft.v, sizeof(pdp->tft.v))) {
1444 }
jjako2c381332003-10-21 19:09:53 +00001445
jjako08d331d2003-10-13 20:33:30 +00001446 /* Trigger ID */
1447 /* OMC identity */
1448 }
1449
1450 /* Initialize our own IP addresses */
jjako52c24142002-12-16 13:33:51 +00001451 in_addr2gsna(&pdp->gsnlc, &gsn->gsnc);
1452 in_addr2gsna(&pdp->gsnlu, &gsn->gsnu);
jjako2c381332003-10-21 19:09:53 +00001453
jjako2e840a32003-01-28 16:05:18 +00001454 if (GTP_DEBUG) printf("gtp_create_pdp_ind: Before pdp_tidget\n");
jjako2c381332003-10-21 19:09:53 +00001455
jjako08d331d2003-10-13 20:33:30 +00001456 if (!pdp_getimsi(&pdp_old, pdp->imsi, pdp->nsapi)) {
jjako52c24142002-12-16 13:33:51 +00001457 /* Found old pdp with same tid. Now the voodoo begins! */
jjako08d331d2003-10-13 20:33:30 +00001458 /* 09.60 / 29.060 allows create on existing context to "steal" */
1459 /* the context which was allready established */
jjako52c24142002-12-16 13:33:51 +00001460 /* We check that the APN, selection mode and MSISDN is the same */
jjako2e840a32003-01-28 16:05:18 +00001461 if (GTP_DEBUG) printf("gtp_create_pdp_ind: Old context found\n");
jjako08d331d2003-10-13 20:33:30 +00001462 if ((pdp->apn_req.l == pdp_old->apn_req.l)
jjako52c24142002-12-16 13:33:51 +00001463 && (!memcmp(pdp->apn_req.v, pdp_old->apn_req.v, pdp->apn_req.l))
1464 && (pdp->selmode == pdp_old->selmode)
1465 && (pdp->msisdn.l == pdp_old->msisdn.l)
1466 && (!memcmp(pdp->msisdn.v, pdp_old->msisdn.v, pdp->msisdn.l))) {
1467 /* OK! We are dealing with the same APN. We will copy new
1468 * parameters to the old pdp and send off confirmation
1469 * We ignore the following information elements:
1470 * QoS: MS will get originally negotiated QoS.
1471 * End user address (EUA). MS will get old EUA anyway.
1472 * Protocol configuration option (PCO): Only application can verify */
jjako2e840a32003-01-28 16:05:18 +00001473
1474 if (GTP_DEBUG) printf("gtp_create_pdp_ind: Old context found\n");
jjako52c24142002-12-16 13:33:51 +00001475
1476 /* Copy remote flow label */
1477 pdp_old->flru = pdp->flru;
1478 pdp_old->flrc = pdp->flrc;
1479
jjako08d331d2003-10-13 20:33:30 +00001480 /* Copy remote tei */
1481 pdp_old->teid_gn = pdp->teid_gn;
1482 pdp_old->teic_gn = pdp->teic_gn;
1483
jjako52c24142002-12-16 13:33:51 +00001484 /* Copy peer GSN address */
1485 pdp_old->gsnrc.l = pdp->gsnrc.l;
1486 memcpy(&pdp_old->gsnrc.v, &pdp->gsnrc.v, pdp->gsnrc.l);
1487 pdp_old->gsnru.l = pdp->gsnru.l;
1488 memcpy(&pdp_old->gsnru.v, &pdp->gsnru.v, pdp->gsnru.l);
jjako2c381332003-10-21 19:09:53 +00001489
1490 /* Copy request parameters */
1491 pdp_old->seq = pdp->seq;
1492 pdp_old->sa_peer = pdp->sa_peer;
1493 pdp_old->fd = pdp->fd = fd;
1494 pdp_old->version = pdp->version = version;
1495
1496 /* Switch to using the old pdp context */
jjako52c24142002-12-16 13:33:51 +00001497 pdp = pdp_old;
1498
1499 /* Confirm to peer that things were "successful" */
jjako08d331d2003-10-13 20:33:30 +00001500 return gtp_create_pdp_resp(gsn, version, pdp, GTPCAUSE_ACC_REQ);
jjako52c24142002-12-16 13:33:51 +00001501 }
1502 else { /* This is not the same PDP context. Delete the old one. */
jjako2e840a32003-01-28 16:05:18 +00001503
1504 if (GTP_DEBUG) printf("gtp_create_pdp_ind: Deleting old context\n");
jjako52c24142002-12-16 13:33:51 +00001505
1506 if (gsn->cb_delete_context) gsn->cb_delete_context(pdp_old);
1507 pdp_freepdp(pdp_old);
jjako08d331d2003-10-13 20:33:30 +00001508
jjako2e840a32003-01-28 16:05:18 +00001509 if (GTP_DEBUG) printf("gtp_create_pdp_ind: Deleted...\n");
jjako52c24142002-12-16 13:33:51 +00001510 }
1511 }
1512
jjako08d331d2003-10-13 20:33:30 +00001513 pdp_newpdp(&pdp, pdp->imsi, pdp->nsapi, pdp);
jjako52c24142002-12-16 13:33:51 +00001514
1515 /* Callback function to validata login */
jjako08d331d2003-10-13 20:33:30 +00001516 if (gsn->cb_create_context_ind !=0)
1517 return gsn->cb_create_context_ind(pdp);
jjako52c24142002-12-16 13:33:51 +00001518 else {
jjako08d331d2003-10-13 20:33:30 +00001519 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1520 "No create_context_ind callback defined");
1521 return gtp_create_pdp_resp(gsn, version, pdp, GTPCAUSE_NOT_SUPPORTED);
jjako52c24142002-12-16 13:33:51 +00001522 }
1523}
1524
1525
1526/* Handle Create PDP Context Response */
1527int gtp_create_pdp_conf(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00001528 struct sockaddr_in *peer,
1529 void *pack, unsigned len) {
jjako52c24142002-12-16 13:33:51 +00001530 struct pdp_t *pdp;
1531 union gtpie_member *ie[GTPIE_SIZE];
1532 uint8_t cause, recovery;
jjako08d331d2003-10-13 20:33:30 +00001533 void *cbp = NULL;
jjako52c24142002-12-16 13:33:51 +00001534 uint8_t type = 0;
jjako08d331d2003-10-13 20:33:30 +00001535 int hlen = get_hlen(pack);
jjako52c24142002-12-16 13:33:51 +00001536
1537 /* Remove packet from queue */
jjako08d331d2003-10-13 20:33:30 +00001538 if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) return EOF;
jjako52c24142002-12-16 13:33:51 +00001539
1540 /* Find the context in question */
jjako08d331d2003-10-13 20:33:30 +00001541 if (pdp_getgtp1(&pdp, get_tei(pack))) {
jjako52c24142002-12-16 13:33:51 +00001542 gsn->err_unknownpdp++;
1543 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1544 "Unknown PDP context");
jjako08d331d2003-10-13 20:33:30 +00001545 if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +00001546 return EOF;
1547 }
1548
jjako2c381332003-10-21 19:09:53 +00001549 /* Register that we have received a valid teic from GGSN */
1550 pdp->teic_confirmed = 1;
1551
jjako52c24142002-12-16 13:33:51 +00001552 /* Decode information elements */
jjako08d331d2003-10-13 20:33:30 +00001553 if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) {
jjako52c24142002-12-16 13:33:51 +00001554 gsn->invalid++;
1555 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1556 "Invalid message format");
jjako08d331d2003-10-13 20:33:30 +00001557 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00001558 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1559 pdp_freepdp(pdp); */
jjako52c24142002-12-16 13:33:51 +00001560 return EOF;
1561 }
1562
1563 /* Extract cause value (mandatory) */
1564 if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) {
1565 gsn->missing++;
1566 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1567 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00001568 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00001569 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1570 pdp_freepdp(pdp); */
jjako52c24142002-12-16 13:33:51 +00001571 return EOF;
1572 }
1573
1574 /* Extract recovery (optional) */
1575 if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) {
1576 /* TODO: Handle received recovery IE */
1577 }
1578
1579 /* Extract protocol configuration options (optional) */
1580 if (!gtpie_gettlv(ie, GTPIE_PCO, 0, &pdp->pco_req.l,
1581 &pdp->pco_req.v, sizeof(pdp->pco_req.v))) {
jjako52c24142002-12-16 13:33:51 +00001582 }
1583
1584 /* Check all conditional information elements */
1585 if (GTPCAUSE_ACC_REQ == cause) {
1586
jjako08d331d2003-10-13 20:33:30 +00001587 if (version == 0) {
1588 if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0,
1589 &pdp->qos_neg0, sizeof(pdp->qos_neg0))) {
1590 gsn->missing++;
1591 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1592 "Missing conditional information field");
1593 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00001594 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1595 pdp_freepdp(pdp); */
jjako08d331d2003-10-13 20:33:30 +00001596 return EOF;
1597 }
jjako52c24142002-12-16 13:33:51 +00001598 }
jjako52c24142002-12-16 13:33:51 +00001599
1600 if (gtpie_gettv1(ie, GTPIE_REORDER, 0, &pdp->reorder)) {
1601 gsn->missing++;
1602 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1603 "Missing conditional information field");
jjako08d331d2003-10-13 20:33:30 +00001604 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00001605 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1606 pdp_freepdp(pdp); */
jjako52c24142002-12-16 13:33:51 +00001607 return EOF;
1608 }
1609
jjako08d331d2003-10-13 20:33:30 +00001610 if (version == 0) {
1611 if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) {
1612 gsn->missing++;
1613 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1614 "Missing conditional information field");
1615 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00001616 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1617 pdp_freepdp(pdp); */
jjako08d331d2003-10-13 20:33:30 +00001618 return EOF;
1619 }
jjako52c24142002-12-16 13:33:51 +00001620
jjako08d331d2003-10-13 20:33:30 +00001621 if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) {
1622 gsn->missing++;
1623 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1624 "Missing conditional information field");
1625 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00001626 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1627 pdp_freepdp(pdp); */
jjako08d331d2003-10-13 20:33:30 +00001628 return EOF;
1629 }
1630 }
1631
1632 if (version == 1) {
1633 if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) {
1634 gsn->missing++;
1635 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1636 "Missing conditional information field");
1637 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00001638 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1639 pdp_freepdp(pdp); */
jjako08d331d2003-10-13 20:33:30 +00001640 return EOF;
1641 }
1642
1643 if (gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn)) {
1644 gsn->missing++;
1645 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1646 "Missing conditional information field");
1647 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00001648 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1649 pdp_freepdp(pdp); */
jjako08d331d2003-10-13 20:33:30 +00001650 return EOF;
1651 }
jjako52c24142002-12-16 13:33:51 +00001652 }
1653
1654 if (gtpie_gettv4(ie, GTPIE_CHARGING_ID, 0, &pdp->cid)) {
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);
jjako0b076a32003-10-25 15:59:31 +00001659 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1660 pdp_freepdp(pdp); */
jjako52c24142002-12-16 13:33:51 +00001661 }
1662
1663 if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l,
1664 &pdp->eua.v, sizeof(pdp->eua.v))) {
1665 gsn->missing++;
1666 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1667 "Missing conditional information field");
jjako08d331d2003-10-13 20:33:30 +00001668 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00001669 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1670 pdp_freepdp(pdp); */
jjako52c24142002-12-16 13:33:51 +00001671 return EOF;
1672 }
1673
1674 if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l,
1675 &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) {
1676 gsn->missing++;
1677 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1678 "Missing conditional information field");
jjako08d331d2003-10-13 20:33:30 +00001679 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00001680 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1681 pdp_freepdp(pdp); */
jjako52c24142002-12-16 13:33:51 +00001682 return EOF;
1683 }
1684
1685 if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l,
1686 &pdp->gsnru.v, sizeof(pdp->gsnru.v))) {
1687 gsn->missing++;
1688 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1689 "Missing conditional information field");
jjako08d331d2003-10-13 20:33:30 +00001690 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00001691 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1692 pdp_freepdp(pdp); */
jjako52c24142002-12-16 13:33:51 +00001693 return EOF;
1694 }
jjako52c24142002-12-16 13:33:51 +00001695
jjako08d331d2003-10-13 20:33:30 +00001696 if (version == 1) {
1697 if (gtpie_gettlv(ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_neg.l,
1698 &pdp->qos_neg.v, sizeof(pdp->qos_neg.v))) {
1699 gsn->missing++;
1700 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1701 "Missing conditional information field");
1702 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00001703 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
1704 pdp_freepdp(pdp); */
jjako08d331d2003-10-13 20:33:30 +00001705 return EOF;
1706 }
1707 }
jjakoccc564f2003-10-25 15:40:48 +00001708
jjako08d331d2003-10-13 20:33:30 +00001709 }
1710
1711 if (gsn->cb_conf) gsn->cb_conf(type, cause, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00001712
1713 return 0;
1714}
1715
jjako52c24142002-12-16 13:33:51 +00001716
jjako08d331d2003-10-13 20:33:30 +00001717/* API: Send Update PDP Context Request */
1718int gtp_update_context(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp,
1719 struct in_addr* inetaddr) {
1720 union gtp_packet packet;
1721 int length = get_default_gtp(pdp->version, GTP_UPDATE_PDP_REQ, &packet);
jjako52c24142002-12-16 13:33:51 +00001722
jjako08d331d2003-10-13 20:33:30 +00001723 if (pdp->version == 0)
1724 gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0,
1725 sizeof(pdp->qos_req0), pdp->qos_req0);
jjako2c381332003-10-21 19:09:53 +00001726
1727 /* Include IMSI if updating with unknown teic_gn */
1728 if ((pdp->version == 1) && (!pdp->teic_gn))
jjako08d331d2003-10-13 20:33:30 +00001729 gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_IMSI,
1730 sizeof(pdp->imsi), (uint8_t*) &pdp->imsi);
jjako2c381332003-10-21 19:09:53 +00001731
jjako08d331d2003-10-13 20:33:30 +00001732 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY,
jjako52c24142002-12-16 13:33:51 +00001733 gsn->restart_counter);
jjako2c381332003-10-21 19:09:53 +00001734
jjako08d331d2003-10-13 20:33:30 +00001735 if (pdp->version == 0) {
1736 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI,
jjako52c24142002-12-16 13:33:51 +00001737 pdp->fllu);
jjako08d331d2003-10-13 20:33:30 +00001738 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C,
jjako52c24142002-12-16 13:33:51 +00001739 pdp->fllc);
jjako52c24142002-12-16 13:33:51 +00001740 }
jjako2c381332003-10-21 19:09:53 +00001741
jjako08d331d2003-10-13 20:33:30 +00001742 if (pdp->version == 1) {
1743 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI,
1744 pdp->teid_own);
jjako52c24142002-12-16 13:33:51 +00001745
jjako2c381332003-10-21 19:09:53 +00001746 if (!pdp->teic_confirmed)
1747 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C,
1748 pdp->teic_own);
1749 }
1750
jjako08d331d2003-10-13 20:33:30 +00001751 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI,
1752 pdp->nsapi);
jjako2c381332003-10-21 19:09:53 +00001753
jjako08d331d2003-10-13 20:33:30 +00001754 /* TODO
jjako2c381332003-10-21 19:09:53 +00001755 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_REF,
1756 pdp->traceref);
1757 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_TYPE,
1758 pdp->tracetype); */
1759
jjako08d331d2003-10-13 20:33:30 +00001760 /* TODO if ggsn update message
jjako2c381332003-10-21 19:09:53 +00001761 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA,
1762 pdp->eua.l, pdp->eua.v);
jjako08d331d2003-10-13 20:33:30 +00001763 */
jjako2c381332003-10-21 19:09:53 +00001764
jjako08d331d2003-10-13 20:33:30 +00001765 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
1766 pdp->gsnlc.l, pdp->gsnlc.v);
1767 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
1768 pdp->gsnlu.l, pdp->gsnlu.v);
jjako2c381332003-10-21 19:09:53 +00001769
jjako08d331d2003-10-13 20:33:30 +00001770 if (pdp->version == 1)
1771 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE,
1772 pdp->qos_req.l, pdp->qos_req.v);
jjako2c381332003-10-21 19:09:53 +00001773
1774
jjako08d331d2003-10-13 20:33:30 +00001775 if ((pdp->version == 1) && pdp->tft.l)
1776 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TFT,
1777 pdp->tft.l, pdp->tft.v);
1778
1779 if ((pdp->version == 1) && pdp->triggerid.l)
1780 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TRIGGER_ID,
1781 pdp->triggerid.l, pdp->triggerid.v);
1782
1783 if ((pdp->version == 1) && pdp->omcid.l)
1784 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_OMC_ID,
1785 pdp->omcid.l, pdp->omcid.v);
1786
1787 gtp_req(gsn, pdp->version, NULL, &packet, length, inetaddr, cbp);
1788
1789 return 0;
jjako52c24142002-12-16 13:33:51 +00001790}
1791
jjako08d331d2003-10-13 20:33:30 +00001792
1793/* Send Update PDP Context Response */
1794int gtp_update_pdp_resp(struct gsn_t *gsn, int version,
1795 struct sockaddr_in *peer, int fd,
1796 void *pack, unsigned len,
1797 struct pdp_t *pdp, uint8_t cause) {
1798
1799 union gtp_packet packet;
1800 int length = get_default_gtp(version, GTP_CREATE_PDP_RSP, &packet);
1801
1802 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause);
1803
1804 if (cause == GTPCAUSE_ACC_REQ) {
1805
1806 if (version == 0)
1807 gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0,
1808 sizeof(pdp->qos_neg0), pdp->qos_neg0);
1809
1810 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY,
1811 gsn->restart_counter);
1812
1813 if (version == 0) {
1814 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI,
1815 pdp->fllu);
1816 gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C,
1817 pdp->fllc);
1818 }
1819
1820 if (version == 1) {
1821 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI,
1822 pdp->teid_own);
jjako2c381332003-10-21 19:09:53 +00001823
1824 if (!pdp->teic_confirmed)
1825 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C,
1826 pdp->teic_own);
jjako08d331d2003-10-13 20:33:30 +00001827 }
jjako2c381332003-10-21 19:09:53 +00001828
1829 /* TODO we use teid_own as charging ID address */
jjako08d331d2003-10-13 20:33:30 +00001830 gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_CHARGING_ID,
jjako2c381332003-10-21 19:09:53 +00001831 pdp->teid_own);
1832
jjako08d331d2003-10-13 20:33:30 +00001833 /* If ggsn
jjako2c381332003-10-21 19:09:53 +00001834 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA,
1835 pdp->eua.l, pdp->eua.v); */
1836
jjako08d331d2003-10-13 20:33:30 +00001837 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
1838 pdp->gsnlc.l, pdp->gsnlc.v);
1839 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR,
1840 pdp->gsnlu.l, pdp->gsnlu.v);
jjako2c381332003-10-21 19:09:53 +00001841
jjako08d331d2003-10-13 20:33:30 +00001842 if (version == 1)
1843 gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE,
1844 pdp->qos_neg.l, pdp->qos_neg.v);
jjako2c381332003-10-21 19:09:53 +00001845
jjako08d331d2003-10-13 20:33:30 +00001846 /* TODO: Charging gateway address */
1847 }
jjako2c381332003-10-21 19:09:53 +00001848
1849 return gtp_resp(version, gsn, pdp, &packet, length, peer,
1850 fd, get_seq(pack), get_tid(pack));
jjako08d331d2003-10-13 20:33:30 +00001851}
1852
1853
jjako52c24142002-12-16 13:33:51 +00001854/* Handle Update PDP Context Request */
1855int gtp_update_pdp_ind(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00001856 struct sockaddr_in *peer, int fd,
1857 void *pack, unsigned len) {
1858 struct pdp_t *pdp;
1859 struct pdp_t pdp_backup;
jjako52c24142002-12-16 13:33:51 +00001860 union gtpie_member* ie[GTPIE_SIZE];
1861 uint8_t recovery;
1862
jjako08d331d2003-10-13 20:33:30 +00001863 uint16_t seq = get_seq(pack);
1864 int hlen = get_hlen(pack);
jjako52c24142002-12-16 13:33:51 +00001865
jjako08d331d2003-10-13 20:33:30 +00001866 uint64_t imsi;
1867 uint8_t nsapi;
1868
jjako52c24142002-12-16 13:33:51 +00001869 /* Is this a dublicate ? */
jjako08d331d2003-10-13 20:33:30 +00001870 if(!gtp_dublicate(gsn, version, peer, seq)) {
jjako52c24142002-12-16 13:33:51 +00001871 return 0; /* We allready send of response once */
1872 }
jjako2c381332003-10-21 19:09:53 +00001873
jjako52c24142002-12-16 13:33:51 +00001874
1875 /* Decode information elements */
jjako08d331d2003-10-13 20:33:30 +00001876 if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) {
jjako52c24142002-12-16 13:33:51 +00001877 gsn->invalid++;
1878 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1879 "Invalid message format");
1880 if (0 == version)
1881 return EOF;
1882 else
jjako08d331d2003-10-13 20:33:30 +00001883 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len,
1884 NULL, GTPCAUSE_INVALID_MESSAGE);
jjako52c24142002-12-16 13:33:51 +00001885 }
1886
jjako08d331d2003-10-13 20:33:30 +00001887 /* Finding PDP: */
1888 /* For GTP0 we use the tunnel identifier to provide imsi and nsapi. */
1889 /* For GTP1 we must use imsi and nsapi if imsi is present. Otherwise */
1890 /* we have to use the tunnel endpoint identifier */
1891 if (version == 0) {
1892 imsi = ((union gtp_packet*)pack)->gtp0.h.tid & 0x0fffffffffffffff;
1893 nsapi = (((union gtp_packet*)pack)->gtp0.h.tid & 0xf000000000000000) >> 60;
jjako2c381332003-10-21 19:09:53 +00001894
jjako08d331d2003-10-13 20:33:30 +00001895 /* Find the context in question */
1896 if (pdp_getimsi(&pdp, imsi, nsapi)) {
1897 gsn->err_unknownpdp++;
1898 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1899 "Unknown PDP context");
1900 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len,
1901 NULL, GTPCAUSE_NON_EXIST);
1902 }
jjako52c24142002-12-16 13:33:51 +00001903 }
jjako08d331d2003-10-13 20:33:30 +00001904 else if (version == 1) {
1905 /* NSAPI (mandatory) */
1906 if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &nsapi)) {
1907 gsn->missing++;
1908 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1909 "Missing mandatory information field");
1910 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len,
1911 NULL, GTPCAUSE_MAN_IE_MISSING);
1912 }
jjako2c381332003-10-21 19:09:53 +00001913
jjako08d331d2003-10-13 20:33:30 +00001914 /* IMSI (conditional) */
1915 if (gtpie_gettv0(ie, GTPIE_IMSI, 0, &imsi, sizeof(imsi))) {
1916 /* Find the context in question */
1917 if (pdp_getgtp1(&pdp, get_tei(pack))) {
1918 gsn->err_unknownpdp++;
1919 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1920 "Unknown PDP context");
1921 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len,
1922 NULL, GTPCAUSE_NON_EXIST);
1923 }
1924 }
1925 else {
1926 /* Find the context in question */
1927 if (pdp_getimsi(&pdp, imsi, nsapi)) {
1928 gsn->err_unknownpdp++;
1929 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1930 "Unknown PDP context");
1931 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len,
1932 NULL, GTPCAUSE_NON_EXIST);
1933 }
1934 }
1935 }
1936 else {
1937 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown version");
1938 return EOF;
1939 }
jjako2c381332003-10-21 19:09:53 +00001940
jjako08d331d2003-10-13 20:33:30 +00001941 /* Make a backup copy in case anything is wrong */
1942 memcpy(&pdp_backup, pdp, sizeof(pdp_backup));
1943
1944 if (version == 0) {
1945 if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0,
1946 pdp->qos_req0, sizeof(pdp->qos_req0))) {
1947 gsn->missing++;
1948 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1949 "Missing mandatory information field");
1950 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
1951 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len,
1952 pdp, GTPCAUSE_MAN_IE_MISSING);
1953 }
1954 }
1955
1956 /* Recovery (optional) */
jjako52c24142002-12-16 13:33:51 +00001957 if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) {
1958 /* TODO: Handle received recovery IE */
1959 }
1960
jjako08d331d2003-10-13 20:33:30 +00001961 if (version == 0) {
1962 if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) {
1963 gsn->missing++;
1964 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1965 "Missing mandatory information field");
1966 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
1967 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
1968 GTPCAUSE_MAN_IE_MISSING);
1969 }
1970
1971 if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) {
1972 gsn->missing++;
1973 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1974 "Missing mandatory information field");
1975 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
1976 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
1977 GTPCAUSE_MAN_IE_MISSING);
1978 }
1979 }
1980
1981
1982 if (version == 1) {
1983 /* TEID (mandatory) */
1984 if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) {
1985 gsn->missing++;
1986 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
1987 "Missing mandatory information field");
1988 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
1989 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
1990 GTPCAUSE_MAN_IE_MISSING);
1991 }
jjako2c381332003-10-21 19:09:53 +00001992
jjako08d331d2003-10-13 20:33:30 +00001993 /* TEIC (conditional) */
1994 /* If TEIC is not included it means that we have allready received it */
jjako2c381332003-10-21 19:09:53 +00001995 /* TODO: From 29.060 it is not clear if TEI_C MUST be included for */
1996 /* all updated contexts, or only for one of the linked contexts */
1997 gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn);
1998
jjako08d331d2003-10-13 20:33:30 +00001999 /* NSAPI (mandatory) */
2000 if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &pdp->nsapi)) {
2001 gsn->missing++;
2002 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2003 "Missing mandatory information field");
2004 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
2005 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
2006 GTPCAUSE_MAN_IE_MISSING);
2007 }
2008 }
2009
2010 /* Trace reference (optional) */
2011 /* Trace type (optional) */
2012
2013 /* End User Address (conditional) TODO: GGSN Initiated
2014 if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l,
2015 &pdp->eua.v, sizeof(pdp->eua.v))) {
jjako52c24142002-12-16 13:33:51 +00002016 gsn->missing++;
2017 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2018 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00002019 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
2020 return gtp_update_pdp_resp(gsn, version, pdp,
jjako52c24142002-12-16 13:33:51 +00002021 GTPCAUSE_MAN_IE_MISSING);
jjako08d331d2003-10-13 20:33:30 +00002022 } */
jjako52c24142002-12-16 13:33:51 +00002023
jjako08d331d2003-10-13 20:33:30 +00002024
2025 /* SGSN address for signalling (mandatory) */
2026 /* It is weird that this is mandatory when TEIC is conditional */
2027 if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l,
2028 &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) {
jjako52c24142002-12-16 13:33:51 +00002029 gsn->missing++;
2030 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2031 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00002032 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
2033 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
jjako52c24142002-12-16 13:33:51 +00002034 GTPCAUSE_MAN_IE_MISSING);
2035 }
2036
jjako08d331d2003-10-13 20:33:30 +00002037 /* SGSN address for user traffic (mandatory) */
2038 if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l,
2039 &pdp->gsnru.v, sizeof(pdp->gsnru.v))) {
jjako52c24142002-12-16 13:33:51 +00002040 gsn->missing++;
2041 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2042 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00002043 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
2044 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
jjako52c24142002-12-16 13:33:51 +00002045 GTPCAUSE_MAN_IE_MISSING);
2046 }
jjako08d331d2003-10-13 20:33:30 +00002047
2048 if (version == 1) {
2049 /* QoS (mandatory) */
2050 if (gtpie_gettlv(ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_req.l,
2051 &pdp->qos_req.v, sizeof(pdp->qos_req.v))) {
2052 gsn->missing++;
2053 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2054 "Missing mandatory information field");
2055 memcpy(pdp, &pdp_backup, sizeof(pdp_backup));
2056 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
2057 GTPCAUSE_MAN_IE_MISSING);
2058 }
jjako52c24142002-12-16 13:33:51 +00002059
jjako08d331d2003-10-13 20:33:30 +00002060 /* TFT (conditional) */
2061 if (gtpie_gettlv(ie, GTPIE_TFT, 0, &pdp->tft.l,
2062 &pdp->tft.v, sizeof(pdp->tft.v))) {
2063 }
2064
2065 /* OMC identity */
jjako52c24142002-12-16 13:33:51 +00002066 }
2067
jjako52c24142002-12-16 13:33:51 +00002068 /* Confirm to peer that things were "successful" */
jjako08d331d2003-10-13 20:33:30 +00002069 return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp,
jjako52c24142002-12-16 13:33:51 +00002070 GTPCAUSE_ACC_REQ);
2071}
2072
2073
2074/* Handle Update PDP Context Response */
2075int gtp_update_pdp_conf(struct gsn_t *gsn, int version,
2076 struct sockaddr_in *peer,
2077 void *pack, unsigned len) {
2078 struct pdp_t *pdp;
2079 union gtpie_member *ie[GTPIE_SIZE];
2080 uint8_t cause, recovery;
jjako08d331d2003-10-13 20:33:30 +00002081 void *cbp = NULL;
jjako52c24142002-12-16 13:33:51 +00002082 uint8_t type = 0;
2083
2084 /* Remove packet from queue */
jjako08d331d2003-10-13 20:33:30 +00002085 if (gtp_conf(gsn, 0, peer, pack, len, &type, &cbp)) return EOF;
jjako52c24142002-12-16 13:33:51 +00002086
2087 /* Find the context in question */
2088 if (pdp_getgtp0(&pdp, ntoh16(((union gtp_packet*)pack)->gtp0.h.flow))) {
2089 gsn->err_unknownpdp++;
2090 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2091 "Unknown PDP context");
jjako08d331d2003-10-13 20:33:30 +00002092 if (gsn->cb_conf) gsn->cb_conf(type, cause, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +00002093 return EOF;
2094 }
2095
jjako2c381332003-10-21 19:09:53 +00002096 /* Register that we have received a valid teic from GGSN */
2097 pdp->teic_confirmed = 1;
2098
jjako52c24142002-12-16 13:33:51 +00002099 /* Decode information elements */
jjako08d331d2003-10-13 20:33:30 +00002100 if (gtpie_decaps(ie, 0, pack+GTP0_HEADER_SIZE, len-GTP0_HEADER_SIZE)) {
jjako52c24142002-12-16 13:33:51 +00002101 gsn->invalid++;
2102 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2103 "Invalid message format");
jjako08d331d2003-10-13 20:33:30 +00002104 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00002105 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2106 pdp_freepdp(pdp); */
jjako52c24142002-12-16 13:33:51 +00002107 return EOF;
2108 }
2109
2110 /* Extract cause value (mandatory) */
2111 if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) {
2112 gsn->missing++;
2113 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2114 "Missing mandatory information field");
jjako08d331d2003-10-13 20:33:30 +00002115 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00002116 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2117 pdp_freepdp(pdp); */
jjako52c24142002-12-16 13:33:51 +00002118 return EOF;
2119 }
2120
2121 /* Extract recovery (optional) */
2122 if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) {
2123 /* TODO: Handle received recovery IE */
2124 }
2125
2126 /* Check all conditional information elements */
2127 if (GTPCAUSE_ACC_REQ != cause) {
jjako08d331d2003-10-13 20:33:30 +00002128 if (gsn->cb_conf) gsn->cb_conf(type, cause, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00002129 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2130 pdp_freepdp(pdp); */
jjako52c24142002-12-16 13:33:51 +00002131 return 0;
2132 }
2133 else {
2134 /* Check for missing conditionary information elements */
2135 if (!(gtpie_exist(ie, GTPIE_QOS_PROFILE0, 0) &&
2136 gtpie_exist(ie, GTPIE_REORDER, 0) &&
2137 gtpie_exist(ie, GTPIE_FL_DI, 0) &&
2138 gtpie_exist(ie, GTPIE_FL_C, 0) &&
2139 gtpie_exist(ie, GTPIE_CHARGING_ID, 0) &&
2140 gtpie_exist(ie, GTPIE_EUA, 0) &&
2141 gtpie_exist(ie, GTPIE_GSN_ADDR, 0) &&
2142 gtpie_exist(ie, GTPIE_GSN_ADDR, 1))) {
2143 gsn->missing++;
2144 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2145 "Missing conditional information field");
jjako08d331d2003-10-13 20:33:30 +00002146 if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
jjako0b076a32003-10-25 15:59:31 +00002147 /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2148 pdp_freepdp(pdp); */
jjako52c24142002-12-16 13:33:51 +00002149 return EOF;
2150 }
2151
2152 /* Update pdp with new values */
2153 gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0,
2154 pdp->qos_neg0, sizeof(pdp->qos_neg0));
2155 gtpie_gettv1(ie, GTPIE_REORDER, 0, &pdp->reorder);
2156 gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru);
2157 gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc);
2158 gtpie_gettv4(ie, GTPIE_CHARGING_ID, 0, &pdp->cid);
2159 gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l,
2160 &pdp->eua.v, sizeof(pdp->eua.v));
2161 gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l,
2162 &pdp->gsnrc.v, sizeof(pdp->gsnrc.v));
2163 gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l,
2164 &pdp->gsnru.v, sizeof(pdp->gsnru.v));
2165
jjako08d331d2003-10-13 20:33:30 +00002166 if (gsn->cb_conf) gsn->cb_conf(type, cause, pdp, cbp);
jjako52c24142002-12-16 13:33:51 +00002167 return 0; /* Succes */
2168 }
2169}
2170
jjako52c24142002-12-16 13:33:51 +00002171
jjako08d331d2003-10-13 20:33:30 +00002172/* API: Send Delete PDP Context Request */
jjako2c381332003-10-21 19:09:53 +00002173int gtp_delete_context_req(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp,
2174 int teardown) {
jjako08d331d2003-10-13 20:33:30 +00002175 union gtp_packet packet;
jjako2c381332003-10-21 19:09:53 +00002176 int length = get_default_gtp(pdp->version, GTP_DELETE_PDP_REQ, &packet);
jjako08d331d2003-10-13 20:33:30 +00002177 struct in_addr addr;
jjako2c381332003-10-21 19:09:53 +00002178 struct pdp_t *linked_pdp;
2179 struct pdp_t *secondary_pdp;
2180 int n;
2181 int count = 0;
2182
jjako52c24142002-12-16 13:33:51 +00002183 if (gsna2in_addr(&addr, &pdp->gsnrc)) {
2184 gsn->err_address++;
2185 gtp_err(LOG_ERR, __FILE__, __LINE__, "GSN address conversion failed");
2186 return EOF;
2187 }
jjako2c381332003-10-21 19:09:53 +00002188
2189 if (pdp_getgtp1(&linked_pdp, pdp->teic_own)) {
2190 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown linked PDP context");
2191 return EOF;
2192 }
2193
2194 if (!teardown) {
2195 for (n=0; n< PDP_MAXNSAPI; n++)
2196 if (linked_pdp->secondary_tei[n]) count++;
2197 if (count <= 1) {
2198 gtp_err(LOG_ERR, __FILE__, __LINE__,
2199 "Must use teardown for last context");
2200 return EOF;
2201 }
2202 }
2203
jjako08d331d2003-10-13 20:33:30 +00002204 if (pdp->version == 1) {
2205 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI,
2206 pdp->nsapi);
jjako2c381332003-10-21 19:09:53 +00002207
2208 if (teardown)
2209 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_TEARDOWN,
2210 0xff);
jjako08d331d2003-10-13 20:33:30 +00002211 }
jjako52c24142002-12-16 13:33:51 +00002212
jjako2c381332003-10-21 19:09:53 +00002213 gtp_req(gsn, pdp->version, pdp, &packet, length, &addr, cbp);
jjako52c24142002-12-16 13:33:51 +00002214
jjako2c381332003-10-21 19:09:53 +00002215 if (teardown) { /* Remove all contexts */
2216 for (n=0; n< PDP_MAXNSAPI; n++) {
2217 if (linked_pdp->secondary_tei[n]) {
2218 if (pdp_getgtp1(&secondary_pdp, linked_pdp->secondary_tei[n])) {
2219 gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown secondary PDP context");
2220 return EOF;
2221 }
2222 if (linked_pdp != secondary_pdp) {
2223 if (gsn->cb_delete_context) gsn->cb_delete_context(secondary_pdp);
2224 pdp_freepdp(secondary_pdp);
2225 }
2226 }
2227 }
2228 if (gsn->cb_delete_context) gsn->cb_delete_context(linked_pdp);
2229 pdp_freepdp(linked_pdp);
2230 }
2231 else {
2232 if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2233 if (pdp == linked_pdp) {
2234 linked_pdp->secondary_tei[pdp->nsapi & 0xf0] = 0;
2235 linked_pdp->nodata = 1;
2236 }
2237 else
2238 pdp_freepdp(pdp);
2239 }
2240
2241 return 0;
2242}
jjako08d331d2003-10-13 20:33:30 +00002243
jjako52c24142002-12-16 13:33:51 +00002244/* Send Delete PDP Context Response */
2245int gtp_delete_pdp_resp(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00002246 struct sockaddr_in *peer, int fd,
jjako52c24142002-12-16 13:33:51 +00002247 void *pack, unsigned len,
jjako2c381332003-10-21 19:09:53 +00002248 struct pdp_t *pdp, struct pdp_t *linked_pdp,
2249 uint8_t cause, int teardown)
jjako52c24142002-12-16 13:33:51 +00002250{
2251 union gtp_packet packet;
jjako2c381332003-10-21 19:09:53 +00002252 struct pdp_t *secondary_pdp;
jjako08d331d2003-10-13 20:33:30 +00002253 int length = get_default_gtp(version, GTP_DELETE_PDP_RSP, &packet);
jjako2c381332003-10-21 19:09:53 +00002254 int n;
jjako52c24142002-12-16 13:33:51 +00002255
jjako08d331d2003-10-13 20:33:30 +00002256 gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause);
jjako52c24142002-12-16 13:33:51 +00002257
jjako08d331d2003-10-13 20:33:30 +00002258 gtp_resp(version, gsn, pdp, &packet, length, peer, fd,
jjako2c381332003-10-21 19:09:53 +00002259 get_seq(pack), get_tid(pack));
jjako52c24142002-12-16 13:33:51 +00002260
jjako2c381332003-10-21 19:09:53 +00002261 if (cause == GTPCAUSE_ACC_REQ) {
2262 if ((teardown) || (version == 0)) { /* Remove all contexts */
2263 for (n=0; n< PDP_MAXNSAPI; n++) {
2264 if (linked_pdp->secondary_tei[n]) {
2265 if (pdp_getgtp1(&secondary_pdp, linked_pdp->secondary_tei[n])) {
2266 gtp_err(LOG_ERR, __FILE__, __LINE__,
2267 "Unknown secondary PDP context");
2268 return EOF;
2269 }
2270 if (linked_pdp != secondary_pdp) {
2271 if (gsn->cb_delete_context) gsn->cb_delete_context(secondary_pdp);
2272 pdp_freepdp(secondary_pdp);
2273 }
2274 }
2275 }
2276 if (gsn->cb_delete_context) gsn->cb_delete_context(linked_pdp);
2277 pdp_freepdp(linked_pdp);
2278 }
2279 else { /* Remove only current context */
2280 if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2281 if (pdp == linked_pdp) {
2282 linked_pdp->secondary_tei[pdp->nsapi & 0xf0] = 0;
2283 linked_pdp->nodata = 1;
2284 }
2285 else
2286 pdp_freepdp(pdp);
2287 }
2288 } /* if (cause == GTPCAUSE_ACC_REQ) */
jjako52c24142002-12-16 13:33:51 +00002289
jjako08d331d2003-10-13 20:33:30 +00002290 return 0;
jjako52c24142002-12-16 13:33:51 +00002291}
2292
2293/* Handle Delete PDP Context Request */
2294int gtp_delete_pdp_ind(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00002295 struct sockaddr_in *peer, int fd,
2296 void *pack, unsigned len) {
jjako2c381332003-10-21 19:09:53 +00002297 struct pdp_t *pdp = NULL;
2298 struct pdp_t *linked_pdp = NULL;
jjako52c24142002-12-16 13:33:51 +00002299 union gtpie_member* ie[GTPIE_SIZE];
jjako08d331d2003-10-13 20:33:30 +00002300
2301 uint16_t seq = get_seq(pack);
2302 int hlen = get_hlen(pack);
jjako52c24142002-12-16 13:33:51 +00002303
jjako08d331d2003-10-13 20:33:30 +00002304 uint8_t nsapi;
jjako2c381332003-10-21 19:09:53 +00002305 uint8_t teardown = 0;
2306 int n;
2307 int count = 0;
2308
jjako52c24142002-12-16 13:33:51 +00002309 /* Is this a dublicate ? */
jjako08d331d2003-10-13 20:33:30 +00002310 if(!gtp_dublicate(gsn, version, peer, seq)) {
2311 return 0; /* We allready send off response once */
jjako52c24142002-12-16 13:33:51 +00002312 }
2313
jjako2c381332003-10-21 19:09:53 +00002314 /* Find the linked context in question */
2315 if (pdp_getgtp1(&linked_pdp, get_tei(pack))) {
jjako52c24142002-12-16 13:33:51 +00002316 gsn->err_unknownpdp++;
2317 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2318 "Unknown PDP context");
jjako2c381332003-10-21 19:09:53 +00002319 return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, NULL, NULL,
2320 GTPCAUSE_NON_EXIST, teardown);
jjako52c24142002-12-16 13:33:51 +00002321 }
jjako2c381332003-10-21 19:09:53 +00002322
2323 /* If version 0 this is also the secondary context */
2324 if (version == 0)
2325 pdp = linked_pdp;
jjako52c24142002-12-16 13:33:51 +00002326
2327 /* Decode information elements */
jjako08d331d2003-10-13 20:33:30 +00002328 if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) {
jjako52c24142002-12-16 13:33:51 +00002329 gsn->invalid++;
2330 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2331 "Invalid message format");
2332 if (0 == version)
2333 return EOF;
2334 else
jjako2c381332003-10-21 19:09:53 +00002335 return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, NULL, NULL,
2336 GTPCAUSE_INVALID_MESSAGE, teardown);
jjako52c24142002-12-16 13:33:51 +00002337 }
jjako2c381332003-10-21 19:09:53 +00002338
jjako08d331d2003-10-13 20:33:30 +00002339 if (version == 1) {
2340 /* NSAPI (mandatory) */
2341 if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &nsapi)) {
2342 gsn->missing++;
2343 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2344 "Missing mandatory information field");
jjako2c381332003-10-21 19:09:53 +00002345 return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, NULL, NULL,
2346 GTPCAUSE_MAN_IE_MISSING, teardown);
jjako08d331d2003-10-13 20:33:30 +00002347 }
jjako2c381332003-10-21 19:09:53 +00002348
2349 /* Find the context in question */
2350 if (pdp_getgtp1(&pdp, linked_pdp->secondary_tei[nsapi & 0x0f])) {
2351 gsn->err_unknownpdp++;
2352 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2353 "Unknown PDP context");
2354 return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, NULL, NULL,
2355 GTPCAUSE_NON_EXIST, teardown);
2356 }
jjako08d331d2003-10-13 20:33:30 +00002357
2358 /* Teardown (conditional) */
jjako2c381332003-10-21 19:09:53 +00002359 gtpie_gettv1(ie, GTPIE_TEARDOWN, 0, &teardown);
2360
2361 if (!teardown) {
2362 for (n=0; n< PDP_MAXNSAPI; n++)
2363 if (linked_pdp->secondary_tei[n]) count++;
2364 if (count <= 1) {
2365 return 0; /* 29.060 7.3.5 Ignore message */
2366 }
jjako08d331d2003-10-13 20:33:30 +00002367 }
2368 }
jjako2c381332003-10-21 19:09:53 +00002369
2370 return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len,
2371 pdp, linked_pdp, GTPCAUSE_ACC_REQ, teardown);
jjako52c24142002-12-16 13:33:51 +00002372}
2373
2374
2375/* Handle Delete PDP Context Response */
2376int gtp_delete_pdp_conf(struct gsn_t *gsn, int version,
2377 struct sockaddr_in *peer,
2378 void *pack, unsigned len) {
jjako52c24142002-12-16 13:33:51 +00002379 union gtpie_member *ie[GTPIE_SIZE];
2380 uint8_t cause;
jjako08d331d2003-10-13 20:33:30 +00002381 void *cbp = NULL;
jjako52c24142002-12-16 13:33:51 +00002382 uint8_t type = 0;
jjako08d331d2003-10-13 20:33:30 +00002383 int hlen = get_hlen(pack);
2384
jjako52c24142002-12-16 13:33:51 +00002385 /* Remove packet from queue */
jjako08d331d2003-10-13 20:33:30 +00002386 if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) return EOF;
jjako52c24142002-12-16 13:33:51 +00002387
jjako52c24142002-12-16 13:33:51 +00002388 /* Decode information elements */
jjako08d331d2003-10-13 20:33:30 +00002389 if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) {
jjako52c24142002-12-16 13:33:51 +00002390 gsn->invalid++;
2391 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2392 "Invalid message format");
jjako2c381332003-10-21 19:09:53 +00002393 if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +00002394 return EOF;
2395 }
2396
jjako08d331d2003-10-13 20:33:30 +00002397 /* Extract cause value (mandatory) */
jjako52c24142002-12-16 13:33:51 +00002398 if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) {
2399 gsn->missing++;
2400 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2401 "Missing mandatory information field");
jjako2c381332003-10-21 19:09:53 +00002402 if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +00002403 return EOF;
2404 }
2405
jjako2c381332003-10-21 19:09:53 +00002406 /* Check the cause value (again) */
2407 if ((GTPCAUSE_ACC_REQ != cause) && (GTPCAUSE_NON_EXIST != cause)) {
jjako52c24142002-12-16 13:33:51 +00002408 gsn->err_cause++;
2409 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2410 "Unexpected cause value received: %d", cause);
jjako2c381332003-10-21 19:09:53 +00002411 if (gsn->cb_conf) gsn->cb_conf(type, cause, NULL, cbp);
2412 return EOF;
jjako52c24142002-12-16 13:33:51 +00002413 }
jjako2c381332003-10-21 19:09:53 +00002414
jjako08d331d2003-10-13 20:33:30 +00002415 /* Callback function to notify application */
jjako2c381332003-10-21 19:09:53 +00002416 if (gsn->cb_conf) gsn->cb_conf(type, cause, NULL, cbp);
jjako52c24142002-12-16 13:33:51 +00002417
2418 return 0;
2419}
2420
2421/* Send Error Indication (response to a GPDU message */
2422int gtp_error_ind_resp(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00002423 struct sockaddr_in *peer, int fd,
jjako52c24142002-12-16 13:33:51 +00002424 void *pack, unsigned len)
2425{
2426 union gtp_packet packet;
jjako08d331d2003-10-13 20:33:30 +00002427 int length = get_default_gtp(version, GTP_ERROR, &packet);
jjako52c24142002-12-16 13:33:51 +00002428
jjako08d331d2003-10-13 20:33:30 +00002429 return gtp_resp(version, gsn, NULL, &packet, length, peer, fd,
2430 get_seq(pack), get_tid(pack));
jjako52c24142002-12-16 13:33:51 +00002431}
2432
2433/* Handle Error Indication */
2434int gtp_error_ind_conf(struct gsn_t *gsn, int version,
2435 struct sockaddr_in *peer,
2436 void *pack, unsigned len) {
2437 struct pdp_t *pdp;
2438
2439 /* Find the context in question */
2440 if (pdp_tidget(&pdp, ((union gtp_packet*)pack)->gtp0.h.tid)) {
2441 gsn->err_unknownpdp++;
2442 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2443 "Unknown PDP context");
2444 return EOF;
2445 }
2446
2447 gsn->err_unknownpdp++; /* TODO: Change counter */
2448 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2449 "Received Error Indication");
2450
2451 if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
2452 pdp_freepdp(pdp);
2453 return 0;
2454}
2455
2456int gtp_gpdu_ind(struct gsn_t *gsn, int version,
jjako08d331d2003-10-13 20:33:30 +00002457 struct sockaddr_in *peer, int fd,
2458 void *pack, unsigned len) {
2459
2460 int hlen = GTP1_HEADER_SIZE_SHORT;
jjako52c24142002-12-16 13:33:51 +00002461
2462 /* Need to include code to verify packet src and dest addresses */
2463 struct pdp_t *pdp;
jjako1db1c812003-07-06 20:53:57 +00002464
jjako08d331d2003-10-13 20:33:30 +00002465 if (version == 0) {
2466 if (pdp_getgtp0(&pdp, ntoh16(((union gtp_packet*)pack)->gtp0.h.flow))) {
2467 gsn->err_unknownpdp++;
2468 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2469 "Unknown PDP context");
2470 return gtp_error_ind_resp(gsn, version, peer, fd, pack, len);
2471 }
2472 hlen = GTP0_HEADER_SIZE;
2473 }
2474 else if (version == 1) {
2475 if (pdp_getgtp1(&pdp, ntoh32(((union gtp_packet*)pack)->gtp1l.h.tei))) {
2476 gsn->err_unknownpdp++;
2477 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2478 "Unknown PDP context");
2479 return gtp_error_ind_resp(gsn, version, peer, fd, pack, len);
2480 }
2481
2482 /* Is this a long or a short header ? */
2483 if (((union gtp_packet*)pack)->gtp1l.h.flags & 0x07)
2484 hlen = GTP1_HEADER_SIZE_LONG;
2485 else
2486 hlen = GTP1_HEADER_SIZE_SHORT;
2487 }
2488 else {
2489 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2490 "Unknown version");
2491 }
2492
jjako1db1c812003-07-06 20:53:57 +00002493 /* If the GPDU was not from the peer GSN tell him to delete context */
2494 if (memcmp(&peer->sin_addr, pdp->gsnru.v, pdp->gsnru.l)) { /* TODO Range? */
2495 gsn->err_unknownpdp++;
2496 gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
2497 "Unknown PDP context");
jjako08d331d2003-10-13 20:33:30 +00002498 return gtp_error_ind_resp(gsn, version, peer, fd, pack, len);
jjako1db1c812003-07-06 20:53:57 +00002499 }
jjako08d331d2003-10-13 20:33:30 +00002500
jjako52c24142002-12-16 13:33:51 +00002501 /* Callback function */
jjako08d331d2003-10-13 20:33:30 +00002502 if (gsn->cb_data_ind !=0)
2503 return gsn->cb_data_ind(pdp, pack+hlen, len-hlen);
jjako52c24142002-12-16 13:33:51 +00002504
2505 return 0;
2506}
2507
2508
jjako08d331d2003-10-13 20:33:30 +00002509
jjako52c24142002-12-16 13:33:51 +00002510/* Receives GTP packet and sends off for further processing
2511 * Function will check the validity of the header. If the header
2512 * is not valid the packet is either dropped or a version not
2513 * supported is returned to the peer.
2514 * TODO: Need to decide on return values! */
jjako08d331d2003-10-13 20:33:30 +00002515int gtp_decaps0(struct gsn_t *gsn)
jjako52c24142002-12-16 13:33:51 +00002516{
jjako2c381332003-10-21 19:09:53 +00002517 unsigned char buffer[PACKET_MAX];
jjako52c24142002-12-16 13:33:51 +00002518 struct sockaddr_in peer;
2519 int peerlen;
jjako08d331d2003-10-13 20:33:30 +00002520 int status;
jjako52c24142002-12-16 13:33:51 +00002521 struct gtp0_header *pheader;
2522 int version = 0; /* GTP version should be determined from header!*/
jjako08d331d2003-10-13 20:33:30 +00002523 int fd = gsn->fd0;
jjako52c24142002-12-16 13:33:51 +00002524
jjakoa7cd2492003-04-11 09:40:12 +00002525 /* TODO: Need strategy of userspace buffering and blocking */
2526 /* Currently read is non-blocking and send is blocking. */
2527 /* This means that the program have to wait for busy send calls...*/
jjako52c24142002-12-16 13:33:51 +00002528
jjakoa7cd2492003-04-11 09:40:12 +00002529 while (1) { /* Loop until no more to read */
jjako08d331d2003-10-13 20:33:30 +00002530 if (fcntl(gsn->fd0, F_SETFL, O_NONBLOCK)) {
jjakoa7cd2492003-04-11 09:40:12 +00002531 gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()");
2532 return -1;
2533 }
2534 peerlen = sizeof(peer);
2535 if ((status =
jjako08d331d2003-10-13 20:33:30 +00002536 recvfrom(gsn->fd0, buffer, sizeof(buffer), 0,
jjakoa7cd2492003-04-11 09:40:12 +00002537 (struct sockaddr *) &peer, &peerlen)) < 0 ) {
jjako08d331d2003-10-13 20:33:30 +00002538 if (errno == EAGAIN) return 0;
jjakoa7cd2492003-04-11 09:40:12 +00002539 gsn->err_readfrom++;
jjako08d331d2003-10-13 20:33:30 +00002540 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 +00002541 return -1;
2542 }
jjako52c24142002-12-16 13:33:51 +00002543
jjakoa7cd2492003-04-11 09:40:12 +00002544 /* Need at least 1 byte in order to check version */
2545 if (status < (1)) {
2546 gsn->empty++;
2547 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2548 "Discarding packet - too small");
2549 continue;
2550 }
2551
jjako08d331d2003-10-13 20:33:30 +00002552 pheader = (struct gtp0_header *) (buffer);
jjakoa7cd2492003-04-11 09:40:12 +00002553
jjako08d331d2003-10-13 20:33:30 +00002554 /* Version should be gtp0 (or earlier) */
2555 /* 09.60 is somewhat unclear on this issue. On gsn->fd0 we expect only */
2556 /* GTP 0 messages. If other version message is received we reply that we */
2557 /* only support version 0, implying that this is the only version */
2558 /* supported on this port */
jjakoa7cd2492003-04-11 09:40:12 +00002559 if (((pheader->flags & 0xe0) > 0x00)) {
2560 gsn->unsup++;
2561 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2562 "Unsupported GTP version");
jjako08d331d2003-10-13 20:33:30 +00002563 gtp_unsup_req(gsn, 0, &peer, gsn->fd0, buffer, status); /* 29.60: 11.1.1 */
jjakoa7cd2492003-04-11 09:40:12 +00002564 continue;
2565 }
2566
2567 /* Check length of gtp0 packet */
jjako08d331d2003-10-13 20:33:30 +00002568 if (status < GTP0_HEADER_SIZE) {
jjakoa7cd2492003-04-11 09:40:12 +00002569 gsn->tooshort++;
2570 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2571 "GTP0 packet too short");
2572 continue; /* Silently discard 29.60: 11.1.2 */
2573 }
jjako1db1c812003-07-06 20:53:57 +00002574
jjako08d331d2003-10-13 20:33:30 +00002575 /* Check packet length field versus length of packet */
2576 if (status != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
2577 gsn->tooshort++;
2578 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2579 "GTP packet length field does not match actual length");
2580 continue; /* Silently discard */
2581 }
2582
2583 if ((gsn->mode == GTP_MODE_GGSN) &&
2584 ((pheader->type == GTP_CREATE_PDP_RSP) ||
2585 (pheader->type == GTP_UPDATE_PDP_RSP) ||
2586 (pheader->type == GTP_DELETE_PDP_RSP))) {
2587 gsn->unexpect++;
2588 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2589 "Unexpected GTP Signalling Message");
2590 continue; /* Silently discard 29.60: 11.1.4 */
2591 }
2592
2593 if ((gsn->mode == GTP_MODE_SGSN) &&
2594 ((pheader->type == GTP_CREATE_PDP_REQ) ||
2595 (pheader->type == GTP_UPDATE_PDP_REQ) ||
2596 (pheader->type == GTP_DELETE_PDP_REQ))) {
2597 gsn->unexpect++;
2598 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2599 "Unexpected GTP Signalling Message");
2600 continue; /* Silently discard 29.60: 11.1.4 */
2601 }
2602
2603 switch (pheader->type) {
2604 case GTP_ECHO_REQ:
2605 gtp_echo_ind(gsn, version, &peer, fd, buffer, status);
2606 break;
2607 case GTP_ECHO_RSP:
2608 gtp_echo_conf(gsn, version, &peer, buffer, status);
2609 break;
2610 case GTP_NOT_SUPPORTED:
2611 gtp_unsup_ind(gsn, &peer, buffer, status);
2612 break;
2613 case GTP_CREATE_PDP_REQ:
2614 gtp_create_pdp_ind(gsn, version, &peer, fd, buffer, status);
2615 break;
2616 case GTP_CREATE_PDP_RSP:
2617 gtp_create_pdp_conf(gsn, version, &peer, buffer, status);
2618 break;
2619 case GTP_UPDATE_PDP_REQ:
2620 gtp_update_pdp_ind(gsn, version, &peer, fd, buffer, status);
2621 break;
2622 case GTP_UPDATE_PDP_RSP:
2623 gtp_update_pdp_conf(gsn, version, &peer, buffer, status);
2624 break;
2625 case GTP_DELETE_PDP_REQ:
2626 gtp_delete_pdp_ind(gsn, version, &peer, fd, buffer, status);
2627 break;
2628 case GTP_DELETE_PDP_RSP:
2629 gtp_delete_pdp_conf(gsn, version, &peer, buffer, status);
2630 break;
2631 case GTP_ERROR:
2632 gtp_error_ind_conf(gsn, version, &peer, buffer, status);
2633 break;
2634 case GTP_GPDU:
2635 gtp_gpdu_ind(gsn, version, &peer, fd, buffer, status);
2636 break;
2637 default:
2638 gsn->unknown++;
2639 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2640 "Unknown GTP message type received");
2641 break;
2642 }
2643 }
2644}
2645
2646
2647int gtp_decaps1c(struct gsn_t *gsn)
2648{
jjako2c381332003-10-21 19:09:53 +00002649 unsigned char buffer[PACKET_MAX];
jjako08d331d2003-10-13 20:33:30 +00002650 struct sockaddr_in peer;
2651 int peerlen;
2652 int status;
2653 struct gtp1_header_short *pheader;
jjako2c381332003-10-21 19:09:53 +00002654 int version = 1; /* TODO GTP version should be determined from header!*/
jjako08d331d2003-10-13 20:33:30 +00002655 int fd = gsn->fd1c;
2656
2657 /* TODO: Need strategy of userspace buffering and blocking */
2658 /* Currently read is non-blocking and send is blocking. */
2659 /* This means that the program have to wait for busy send calls...*/
2660
2661 while (1) { /* Loop until no more to read */
jjako2c381332003-10-21 19:09:53 +00002662 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
jjako08d331d2003-10-13 20:33:30 +00002663 gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()");
2664 return -1;
2665 }
2666 peerlen = sizeof(peer);
2667 if ((status =
jjako2c381332003-10-21 19:09:53 +00002668 recvfrom(fd, buffer, sizeof(buffer), 0,
jjako08d331d2003-10-13 20:33:30 +00002669 (struct sockaddr *) &peer, &peerlen)) < 0 ) {
2670 if (errno == EAGAIN) return 0;
2671 gsn->err_readfrom++;
jjako2c381332003-10-21 19:09:53 +00002672 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 +00002673 return -1;
2674 }
2675
2676 /* Need at least 1 byte in order to check version */
2677 if (status < (1)) {
2678 gsn->empty++;
2679 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2680 "Discarding packet - too small");
2681 continue;
2682 }
2683
2684 pheader = (struct gtp1_header_short *) (buffer);
2685
2686 /* Version must be no larger than GTP 1 */
2687 if (((pheader->flags & 0xe0) > 0x20)) {
2688 gsn->unsup++;
2689 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2690 "Unsupported GTP version");
jjako2c381332003-10-21 19:09:53 +00002691 gtp_unsup_req(gsn, version, &peer, fd, buffer, status);
2692 /*29.60: 11.1.1*/
jjako08d331d2003-10-13 20:33:30 +00002693 continue;
2694 }
2695
2696 /* Version must be at least GTP 1 */
2697 /* 29.060 is somewhat unclear on this issue. On gsn->fd1c we expect only */
2698 /* GTP 1 messages. If GTP 0 message is received we silently discard */
2699 /* the message */
2700 if (((pheader->flags & 0xe0) < 0x20)) {
2701 gsn->unsup++;
2702 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2703 "Unsupported GTP version");
2704 continue;
2705 }
2706
2707 /* Check packet flag field */
2708 if (((pheader->flags & 0xf7) != 0x32)) {
2709 gsn->unsup++;
2710 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2711 "Unsupported packet flag");
2712 continue;
2713 }
2714
2715 /* Check length of packet */
2716 if (status < GTP1_HEADER_SIZE_LONG) {
2717 gsn->tooshort++;
2718 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2719 "GTP packet too short");
2720 continue; /* Silently discard 29.60: 11.1.2 */
2721 }
2722
2723 /* Check packet length field versus length of packet */
2724 if (status != (ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT)) {
2725 gsn->tooshort++;
2726 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2727 "GTP packet length field does not match actual length");
2728 continue; /* Silently discard */
2729 }
2730
jjako2c381332003-10-21 19:09:53 +00002731 /* Check for extension headers */
2732 /* TODO: We really should cycle through the headers and determine */
2733 /* if any have the comprehension required flag set */
2734 if (((pheader->flags & 0x04) != 0x00)) {
2735 gsn->unsup++;
2736 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2737 "Unsupported extension header");
2738 gtp_extheader_req(gsn, version, &peer, fd, buffer, status);
2739
2740 continue;
2741 }
2742
jjako1db1c812003-07-06 20:53:57 +00002743 if ((gsn->mode == GTP_MODE_GGSN) &&
2744 ((pheader->type == GTP_CREATE_PDP_RSP) ||
2745 (pheader->type == GTP_UPDATE_PDP_RSP) ||
2746 (pheader->type == GTP_DELETE_PDP_RSP))) {
2747 gsn->unexpect++;
2748 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2749 "Unexpected GTP Signalling Message");
2750 continue; /* Silently discard 29.60: 11.1.4 */
2751 }
2752
2753 if ((gsn->mode == GTP_MODE_SGSN) &&
2754 ((pheader->type == GTP_CREATE_PDP_REQ) ||
2755 (pheader->type == GTP_UPDATE_PDP_REQ) ||
2756 (pheader->type == GTP_DELETE_PDP_REQ))) {
2757 gsn->unexpect++;
2758 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2759 "Unexpected GTP Signalling Message");
2760 continue; /* Silently discard 29.60: 11.1.4 */
2761 }
2762
jjakoa7cd2492003-04-11 09:40:12 +00002763 switch (pheader->type) {
2764 case GTP_ECHO_REQ:
jjako08d331d2003-10-13 20:33:30 +00002765 gtp_echo_ind(gsn, version, &peer, fd, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002766 break;
2767 case GTP_ECHO_RSP:
jjako08d331d2003-10-13 20:33:30 +00002768 gtp_echo_conf(gsn, version, &peer, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002769 break;
2770 case GTP_NOT_SUPPORTED:
jjako08d331d2003-10-13 20:33:30 +00002771 gtp_unsup_ind(gsn, &peer, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002772 break;
jjako2c381332003-10-21 19:09:53 +00002773 case GTP_SUPP_EXT_HEADER:
2774 gtp_extheader_ind(gsn, &peer, buffer, status);
2775 break;
jjakoa7cd2492003-04-11 09:40:12 +00002776 case GTP_CREATE_PDP_REQ:
jjako08d331d2003-10-13 20:33:30 +00002777 gtp_create_pdp_ind(gsn, version, &peer, fd, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002778 break;
2779 case GTP_CREATE_PDP_RSP:
jjako08d331d2003-10-13 20:33:30 +00002780 gtp_create_pdp_conf(gsn, version, &peer, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002781 break;
2782 case GTP_UPDATE_PDP_REQ:
jjako08d331d2003-10-13 20:33:30 +00002783 gtp_update_pdp_ind(gsn, version, &peer, fd, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002784 break;
2785 case GTP_UPDATE_PDP_RSP:
jjako08d331d2003-10-13 20:33:30 +00002786 gtp_update_pdp_conf(gsn, version, &peer, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002787 break;
2788 case GTP_DELETE_PDP_REQ:
jjako08d331d2003-10-13 20:33:30 +00002789 gtp_delete_pdp_ind(gsn, version, &peer, fd, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002790 break;
2791 case GTP_DELETE_PDP_RSP:
jjako08d331d2003-10-13 20:33:30 +00002792 gtp_delete_pdp_conf(gsn, version, &peer, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002793 break;
2794 case GTP_ERROR:
jjako08d331d2003-10-13 20:33:30 +00002795 gtp_error_ind_conf(gsn, version, &peer, buffer, status);
jjakoa7cd2492003-04-11 09:40:12 +00002796 break;
2797 default:
jjako52c24142002-12-16 13:33:51 +00002798 gsn->unknown++;
2799 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2800 "Unknown GTP message type received");
jjakoa7cd2492003-04-11 09:40:12 +00002801 break;
jjako52c24142002-12-16 13:33:51 +00002802 }
2803 }
2804}
2805
jjako08d331d2003-10-13 20:33:30 +00002806int gtp_decaps1u(struct gsn_t *gsn)
2807{
jjako2c381332003-10-21 19:09:53 +00002808 unsigned char buffer[PACKET_MAX];
jjako08d331d2003-10-13 20:33:30 +00002809 struct sockaddr_in peer;
2810 int peerlen;
2811 int status;
2812 struct gtp1_header_short *pheader;
2813 int version = 1; /* GTP version should be determined from header!*/
2814 int fd = gsn->fd1u;
2815
2816 /* TODO: Need strategy of userspace buffering and blocking */
2817 /* Currently read is non-blocking and send is blocking. */
2818 /* This means that the program have to wait for busy send calls...*/
2819
2820 while (1) { /* Loop until no more to read */
2821 if (fcntl(gsn->fd1u, F_SETFL, O_NONBLOCK)) {
2822 gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()");
2823 return -1;
2824 }
2825 peerlen = sizeof(peer);
2826 if ((status =
2827 recvfrom(gsn->fd1u, buffer, sizeof(buffer), 0,
2828 (struct sockaddr *) &peer, &peerlen)) < 0 ) {
2829 if (errno == EAGAIN) return 0;
2830 gsn->err_readfrom++;
2831 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");
2832 return -1;
2833 }
2834
2835 /* Need at least 1 byte in order to check version */
2836 if (status < (1)) {
2837 gsn->empty++;
2838 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2839 "Discarding packet - too small");
2840 continue;
2841 }
2842
2843 pheader = (struct gtp1_header_short *) (buffer);
2844
2845 /* Version must be no larger than GTP 1 */
2846 if (((pheader->flags & 0xe0) > 0x20)) {
2847 gsn->unsup++;
2848 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2849 "Unsupported GTP version");
2850 gtp_unsup_req(gsn, 1, &peer, gsn->fd1c, buffer, status);/*29.60: 11.1.1*/
2851 continue;
2852 }
2853
2854 /* Version must be at least GTP 1 */
2855 /* 29.060 is somewhat unclear on this issue. On gsn->fd1c we expect only */
2856 /* GTP 1 messages. If GTP 0 message is received we silently discard */
2857 /* the message */
2858 if (((pheader->flags & 0xe0) < 0x20)) {
2859 gsn->unsup++;
2860 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2861 "Unsupported GTP version");
2862 continue;
2863 }
2864
2865 /* Check packet flag field (allow both with and without sequence number)*/
2866 if (((pheader->flags & 0xf5) != 0x30)) {
2867 gsn->unsup++;
2868 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2869 "Unsupported packet flag");
2870 continue;
2871 }
2872
2873 /* Check length of packet */
2874 if (status < GTP1_HEADER_SIZE_SHORT) {
2875 gsn->tooshort++;
2876 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2877 "GTP packet too short");
2878 continue; /* Silently discard 29.60: 11.1.2 */
2879 }
2880
2881 /* Check packet length field versus length of packet */
2882 if (status != (ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT)) {
2883 gsn->tooshort++;
2884 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2885 "GTP packet length field does not match actual length");
2886 continue; /* Silently discard */
2887 }
jjako2c381332003-10-21 19:09:53 +00002888
2889 /* Check for extension headers */
2890 /* TODO: We really should cycle through the headers and determine */
2891 /* if any have the comprehension required flag set */
2892 if (((pheader->flags & 0x04) != 0x00)) {
2893 gsn->unsup++;
2894 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2895 "Unsupported extension header");
2896 gtp_extheader_req(gsn, version, &peer, fd, buffer, status);
2897
2898 continue;
2899 }
jjako08d331d2003-10-13 20:33:30 +00002900
2901 switch (pheader->type) {
2902 case GTP_ECHO_REQ:
2903 gtp_echo_ind(gsn, version, &peer, fd, buffer, status);
2904 break;
2905 case GTP_ECHO_RSP:
2906 gtp_echo_conf(gsn, version, &peer, buffer, status);
2907 break;
jjako2c381332003-10-21 19:09:53 +00002908 case GTP_SUPP_EXT_HEADER:
2909 gtp_extheader_ind(gsn, &peer, buffer, status);
2910 break;
jjako08d331d2003-10-13 20:33:30 +00002911 case GTP_ERROR:
2912 gtp_error_ind_conf(gsn, version, &peer, buffer, status);
2913 break;
2914 /* Supported header extensions */
2915 case GTP_GPDU:
2916 gtp_gpdu_ind(gsn, version, &peer, fd, buffer, status);
2917 break;
2918 default:
2919 gsn->unknown++;
2920 gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status,
2921 "Unknown GTP message type received");
2922 break;
2923 }
2924 }
2925}
2926
2927int gtp_data_req(struct gsn_t *gsn, struct pdp_t* pdp,
jjako52c24142002-12-16 13:33:51 +00002928 void *pack, unsigned len)
2929{
2930 union gtp_packet packet;
2931 struct sockaddr_in addr;
jjako08d331d2003-10-13 20:33:30 +00002932 int fd;
2933 int length;
jjako52c24142002-12-16 13:33:51 +00002934
2935 memset(&addr, 0, sizeof(addr));
2936 addr.sin_family = AF_INET;
jjako52c24142002-12-16 13:33:51 +00002937 memcpy(&addr.sin_addr, pdp->gsnru.v,pdp->gsnru.l); /* TODO range check */
jjako52c24142002-12-16 13:33:51 +00002938
jjako08d331d2003-10-13 20:33:30 +00002939 if (pdp->version == 0) {
2940
2941 length = GTP0_HEADER_SIZE+len;
2942 addr.sin_port = htons(GTP0_PORT);
2943 fd = gsn->fd0;
2944
2945 get_default_gtp(0, GTP_GPDU, &packet);
2946 packet.gtp0.h.length = hton16(len);
2947 packet.gtp0.h.seq = hton16(pdp->gtpsntx++);
2948 packet.gtp0.h.flow = hton16(pdp->flru);
2949 packet.gtp0.h.tid = (pdp->imsi & 0x0fffffffffffffff) + ((uint64_t)pdp->nsapi << 60);
jjako52c24142002-12-16 13:33:51 +00002950
jjako08d331d2003-10-13 20:33:30 +00002951 if (len > sizeof (union gtp_packet) - sizeof(struct gtp0_header)) {
2952 gsn->err_memcpy++;
2953 gtp_err(LOG_ERR, __FILE__, __LINE__,
2954 "Memcpy failed");
2955 return EOF;
jjako52c24142002-12-16 13:33:51 +00002956 }
jjako08d331d2003-10-13 20:33:30 +00002957 memcpy(packet.gtp0.p, pack, len); /* TODO Should be avoided! */
2958 }
2959 else if (pdp->version == 1) {
2960
2961 length = GTP1_HEADER_SIZE_LONG+len;
2962 addr.sin_port = htons(GTP1U_PORT);
2963 fd = gsn->fd1u;
jjako52c24142002-12-16 13:33:51 +00002964
jjako08d331d2003-10-13 20:33:30 +00002965 get_default_gtp(1, GTP_GPDU, &packet);
2966 packet.gtp1l.h.length = hton16(len-GTP1_HEADER_SIZE_SHORT+
2967 GTP1_HEADER_SIZE_LONG);
2968 packet.gtp1l.h.seq = hton16(pdp->gtpsntx++);
jjako2c381332003-10-21 19:09:53 +00002969 packet.gtp1l.h.tei = hton32(pdp->teid_gn);
jjako08d331d2003-10-13 20:33:30 +00002970
2971 if (len > sizeof (union gtp_packet) - sizeof(struct gtp1_header_long)) {
2972 gsn->err_memcpy++;
2973 gtp_err(LOG_ERR, __FILE__, __LINE__,
2974 "Memcpy failed");
2975 return EOF;
2976 }
2977 memcpy(packet.gtp1l.p, pack, len); /* TODO Should be avoided! */
2978 }
2979 else {
2980 gtp_err(LOG_ERR, __FILE__, __LINE__,
2981 "Unknown version");
2982 return EOF;
2983 }
2984
2985 if (fcntl(fd, F_SETFL, 0)) {
jjakoa7cd2492003-04-11 09:40:12 +00002986 gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()");
2987 return -1;
2988 }
2989
jjako08d331d2003-10-13 20:33:30 +00002990 if (sendto(fd, &packet, length, 0,
jjako52c24142002-12-16 13:33:51 +00002991 (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2992 gsn->err_sendto++;
jjako08d331d2003-10-13 20:33:30 +00002993 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 +00002994 return EOF;
2995 }
2996 return 0;
2997}
2998
2999
3000/* ***********************************************************
3001 * Conversion functions
3002 *************************************************************/
3003
3004int char2ul_t(char* src, struct ul_t dst) {
3005 dst.l = strlen(src)+1;
3006 dst.v = malloc(dst.l);
3007 dst.v[0] = dst.l - 1;
3008 memcpy(&dst.v[1], src, dst.v[0]);
3009 return 0;
3010}
3011
3012/* ***********************************************************
3013 * IP address conversion functions
3014 * There exist several types of address representations:
3015 * - eua: End User Address. (29.060, 7.7.27, message type 128)
3016 * Used for signalling address to mobile station. Supports IPv4
3017 * IPv6 x.25 etc. etc.
3018 * - gsna: GSN Address. (29.060, 7.7.32, message type 133): IP address
3019 * of GSN. If length is 4 it is IPv4. If length is 16 it is IPv6.
3020 * - in_addr: IPv4 address struct.
3021 * - sockaddr_in: Socket API representation of IP address and
3022 * port number.
3023 *************************************************************/
3024
3025int ipv42eua(struct ul66_t *eua, struct in_addr *src) {
3026 eua->v[0] = 0xf1; /* IETF */
3027 eua->v[1] = 0x21; /* IPv4 */
3028 if (src) {
3029 eua->l = 6;
3030 memcpy(&eua->v[2], src, 4);
3031 }
3032 else
3033 {
3034 eua->l = 2;
3035 }
3036 return 0;
3037}
3038
3039int eua2ipv4(struct in_addr *dst, struct ul66_t *eua) {
3040 if ((eua->l != 6) ||
3041 (eua->v[0] != 0xf1) ||
3042 (eua->v[1] = 0x21))
3043 return -1; /* Not IPv4 address*/
3044 memcpy(dst, &eua->v[2], 4);
3045 return 0;
3046}
3047
3048int gsna2in_addr(struct in_addr *dst, struct ul16_t *gsna) {
3049 memset(dst, 0, sizeof(struct in_addr));
3050 if (gsna->l != 4) return EOF; /* Return if not IPv4 */
3051 memcpy(dst, gsna->v, gsna->l);
3052 return 0;
3053}
3054
3055int in_addr2gsna(struct ul16_t *gsna, struct in_addr *src) {
3056 memset(gsna, 0, sizeof(struct ul16_t));
3057 gsna->l = 4;
3058 memcpy(gsna->v, src, gsna->l);
3059 return 0;
3060}
3061