jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * OpenGGSN - Gateway GPRS Support Node |
| 3 | * Copyright (C) 2002 Mondru AB. |
| 4 | * |
| 5 | * The contents of this file may be used under the terms of the GNU |
| 6 | * General Public License Version 2, provided that the above copyright |
| 7 | * notice and this permission notice is included in all copies or |
| 8 | * substantial portions of the software. |
| 9 | * |
| 10 | * The initial developer of the original code is |
| 11 | * Jens Jakobsen <jj@openggsn.org> |
| 12 | * |
| 13 | * Contributor(s): |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * gtp.c: Contains all GTP functionality. Should be able to handle multiple |
| 19 | * tunnels in the same program. |
| 20 | * |
| 21 | * TODO: |
| 22 | * - Do we need to handle fragmentation? |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #ifdef __linux__ |
| 27 | #define _GNU_SOURCE 1 |
| 28 | #endif |
| 29 | |
| 30 | |
| 31 | #include <syslog.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdarg.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <sys/time.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/socket.h> |
| 38 | #include <netinet/in.h> |
| 39 | #include <arpa/inet.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <time.h> |
| 42 | #include <unistd.h> |
| 43 | #include <string.h> |
| 44 | #include <errno.h> |
| 45 | #include <fcntl.h> |
| 46 | |
| 47 | #include <arpa/inet.h> |
| 48 | |
| 49 | #include <stdint.h> /* ISO C99 types */ |
| 50 | |
jjako | 3c13e30 | 2003-01-28 22:17:29 +0000 | [diff] [blame] | 51 | #include "../config.h" |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 52 | #include "pdp.h" |
| 53 | #include "gtp.h" |
| 54 | #include "gtpie.h" |
| 55 | #include "queue.h" |
| 56 | |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 57 | |
| 58 | /* Error reporting functions */ |
| 59 | |
| 60 | void 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 | |
| 71 | void 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 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 106 | /* API Functions */ |
| 107 | |
| 108 | const char* gtp_version() |
| 109 | { |
| 110 | return VERSION; |
| 111 | } |
| 112 | |
| 113 | /* gtp_new */ |
| 114 | /* gtp_free */ |
| 115 | |
| 116 | int 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 | |
| 121 | int gtp_freepdp(struct gsn_t* gsn, struct pdp_t *pdp) { |
| 122 | return pdp_freepdp(pdp); |
| 123 | } |
| 124 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 125 | /* gtp_gpdu */ |
| 126 | |
| 127 | extern int gtp_fd(struct gsn_t *gsn) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 128 | return gsn->fd0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* gtp_decaps */ |
| 132 | /* gtp_retrans */ |
| 133 | /* gtp_retranstimeout */ |
| 134 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 135 | |
| 136 | int 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 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 142 | int 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 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 148 | |
| 149 | /* API: Initialise delete context callback */ |
| 150 | /* Called whenever a pdp context is deleted for any reason */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 151 | int gtp_set_cb_delete_context(struct gsn_t *gsn, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 152 | int (*cb) (struct pdp_t* pdp)) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 153 | { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 154 | gsn->cb_delete_context = cb; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 158 | int gtp_set_cb_conf(struct gsn_t *gsn, |
| 159 | int (*cb) (int type, int cause, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 160 | struct pdp_t* pdp, void *cbp)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 161 | gsn->cb_conf = cb; |
| 162 | return 0; |
| 163 | } |
| 164 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 165 | extern int gtp_set_cb_data_ind(struct gsn_t *gsn, |
| 166 | int (*cb_data_ind) (struct pdp_t* pdp, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 167 | void* pack, |
| 168 | unsigned len)) |
| 169 | { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 170 | gsn->cb_data_ind = cb_data_ind; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 174 | /** |
| 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 | **/ |
| 182 | static int get_default_gtp(int version, u_int8_t type, void *packet) { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 183 | struct gtp0_header *gtp0_default = (struct gtp0_header*) packet; |
| 184 | struct gtp1_header_long *gtp1_default = (struct gtp1_header_long*) packet; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 185 | switch (version) { |
| 186 | case 0: |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 187 | /* Initialise "standard" GTP0 header */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 188 | memset(gtp0_default, 0, sizeof(struct gtp0_header)); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 189 | gtp0_default->flags=0x1e; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 190 | gtp0_default->type=hton8(type); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 191 | gtp0_default->spare1=0xff; |
| 192 | gtp0_default->spare2=0xff; |
| 193 | gtp0_default->spare3=0xff; |
| 194 | gtp0_default->number=0xff; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 195 | return GTP0_HEADER_SIZE; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 196 | case 1: |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 197 | /* Initialise "standard" GTP1 header */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 198 | /* 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; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 213 | /** |
| 214 | * get_seq() |
| 215 | * Get sequence number of a packet. |
| 216 | * Returns 0 on error |
| 217 | **/ |
| 218 | static 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 | **/ |
| 237 | static 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 | **/ |
| 251 | static 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 | **/ |
| 273 | static 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 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 287 | |
| 288 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 289 | int 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 | |
| 301 | char* 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); |
jjako | 2e840a3 | 2003-01-28 16:05:18 +0000 | [diff] [blame] | 309 | buf[size-1] = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 310 | 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 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 321 | |
| 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 |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 362 | * side effects in the application which is asked twice to validate |
| 363 | * the login. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 364 | * 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: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 390 | * Send off a response to a request. Use the same sequence |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 391 | * number in the response as in the request. |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 392 | * gtp_notification: |
| 393 | * Send off a notification message. This is neither a request nor |
| 394 | * a response. Both TEI and SEQ are zero. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 395 | * gtp_retrans: |
| 396 | * Retransmit any outstanding packets which have exceeded |
| 397 | * a predefined timeout. |
| 398 | *************************************************************/ |
| 399 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 400 | int 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) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 403 | struct sockaddr_in addr; |
| 404 | struct qmsg_t *qmsg; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 405 | int fd; |
| 406 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 407 | memset(&addr, 0, sizeof(addr)); |
| 408 | addr.sin_family = AF_INET; |
| 409 | addr.sin_addr = *inetaddr; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 410 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 411 | 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 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 439 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 440 | if (sendto(fd, packet, len, 0, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 441 | (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 442 | gsn->err_sendto++; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 443 | gtp_err(LOG_ERR, __FILE__, __LINE__, "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s", fd, (unsigned long) &packet, len, strerror(errno)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 444 | 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 */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 457 | qmsg->cbp = cbp; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 458 | qmsg->type = ntoh8(packet->gtp0.h.type); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 459 | qmsg->fd = fd; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 460 | } |
| 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 | |
| 469 | int gtp_conf(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 470 | union gtp_packet *packet, int len, uint8_t *type, void **cbp) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 471 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 472 | 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)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 485 | 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 | |
| 494 | int 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 */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 506 | if (gsn->cb_conf) gsn->cb_conf(qmsg->type, EOF, NULL, qmsg->cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 507 | queue_freemsg(gsn->queue_req, qmsg); |
| 508 | } |
| 509 | else { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 510 | if (sendto(qmsg->fd, &qmsg->p, qmsg->l, 0, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 511 | (struct sockaddr *) &qmsg->peer, sizeof(struct sockaddr_in)) < 0) { |
| 512 | gsn->err_sendto++; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 513 | 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)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 514 | } |
| 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 | |
| 531 | int 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 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 550 | int 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) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 554 | struct qmsg_t *qmsg; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 555 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 556 | 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"); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 576 | return -1; |
| 577 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 578 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 579 | 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, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 585 | (struct sockaddr *) peer, sizeof(struct sockaddr_in)) < 0) { |
| 586 | gsn->err_sendto++; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 587 | gtp_err(LOG_ERR, __FILE__, __LINE__, "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s", fd, (unsigned long) &packet, len, strerror(errno)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 588 | 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 */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 601 | qmsg->cbp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 602 | qmsg->type = 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 603 | qmsg->fd = fd; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 604 | } |
| 605 | return 0; |
| 606 | } |
| 607 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 608 | int 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 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 652 | int 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 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 659 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 660 | if (fcntl(qmsg->fd, F_SETFL, 0)) { |
| 661 | gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()"); |
| 662 | return -1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 663 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 664 | |
| 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; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | |
| 674 | |
| 675 | /* Perform restoration and recovery error handling as described in 29.060 */ |
| 676 | static 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"))) { |
jjako | 581c9f0 | 2003-10-22 11:28:20 +0000 | [diff] [blame] | 691 | |
| 692 | gtp_err(LOG_ERR, __FILE__, __LINE__, "State information file (%s) not found. Creating new file.", filename); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 693 | } |
| 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 | |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 720 | int gtp_new(struct gsn_t **gsn, char *statedir, struct in_addr *listen, |
| 721 | int mode) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 722 | { |
| 723 | struct sockaddr_in addr; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 724 | |
| 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); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 731 | |
| 732 | /* Initialise sequence number */ |
| 733 | (*gsn)->seq_next = (*gsn)->restart_counter * 1024; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 734 | |
| 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 */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 743 | (*gsn)->cb_create_context_ind = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 744 | (*gsn)->cb_delete_context = 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 745 | (*gsn)->cb_unsup_ind = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 746 | (*gsn)->cb_conf = 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 747 | (*gsn)->cb_data_ind = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 748 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 749 | /* 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 ) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 757 | (*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 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 761 | |
| 762 | memset(&addr, 0, sizeof(addr)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 763 | addr.sin_family = AF_INET; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 764 | addr.sin_addr = *listen; /* Same IP for user traffic and signalling*/ |
| 765 | addr.sin_port = htons(GTP0_PORT); |
| 766 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 767 | if (bind((*gsn)->fd0, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 768 | (*gsn)->err_socket++; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 769 | 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)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 806 | return -1; |
| 807 | } |
| 808 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | int gtp_free(struct gsn_t *gsn) { |
| 813 | |
| 814 | /* Clean up retransmit queues */ |
| 815 | queue_free(gsn->queue_req); |
| 816 | queue_free(gsn->queue_resp); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 817 | |
| 818 | close(gsn->fd0); |
| 819 | close(gsn->fd1c); |
| 820 | close(gsn->fd1u); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 821 | |
| 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 */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 856 | int gtp_echo_req(struct gsn_t *gsn, int version, void *cbp, |
| 857 | struct in_addr *inetaddr) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 858 | { |
| 859 | union gtp_packet packet; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 860 | int length = get_default_gtp(version, GTP_ECHO_REQ, &packet); |
| 861 | return gtp_req(gsn, version, NULL, &packet, length, inetaddr, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 862 | } |
| 863 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 864 | /* Send off an echo reply */ |
| 865 | int gtp_echo_resp(struct gsn_t *gsn, int version, |
| 866 | struct sockaddr_in *peer, int fd, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 867 | void *pack, unsigned len) |
| 868 | { |
| 869 | union gtp_packet packet; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 870 | 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)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | |
| 877 | /* Handle a received echo request */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 878 | int gtp_echo_ind(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
| 879 | int fd, void *pack, unsigned len) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 880 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 881 | /* Check if it was a dublicate request */ |
| 882 | if(!gtp_dublicate(gsn, 0, peer, get_seq(pack))) return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 883 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 884 | /* Send off reply to request */ |
| 885 | return gtp_echo_resp(gsn, version, peer, fd, pack, len); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /* Handle a received echo reply */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 889 | int gtp_echo_conf(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 890 | void *pack, unsigned len) { |
| 891 | union gtpie_member *ie[GTPIE_SIZE]; |
| 892 | unsigned char recovery; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 893 | void *cbp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 894 | uint8_t type = 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 895 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 896 | |
| 897 | /* Remove packet from queue */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 898 | if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 899 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 900 | /* Extract information elements into a pointer array */ |
| 901 | if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 902 | gsn->invalid++; |
| 903 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 904 | "Invalid message format"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 905 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 906 | 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 913 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 914 | return EOF; |
| 915 | } |
| 916 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 917 | /* 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); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 920 | |
| 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 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 935 | int gtp_unsup_req(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
| 936 | int fd, void *pack, unsigned len) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 937 | { |
| 938 | union gtp_packet packet; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 939 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 940 | /* GTP 1 is the highest supported protocol */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 941 | int length = get_default_gtp(1, GTP_NOT_SUPPORTED, &packet); |
| 942 | return gtp_notification(gsn, version, &packet, length, |
| 943 | peer, fd, 0); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /* Handle a Version Not Supported message */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 947 | int gtp_unsup_ind(struct gsn_t *gsn, struct sockaddr_in *peer, |
| 948 | void *pack, unsigned len) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 949 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 950 | if (gsn->cb_unsup_ind) gsn->cb_unsup_ind(peer); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 951 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 952 | return 0; |
| 953 | } |
| 954 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 955 | /* Send off an Supported Extension Headers Notification */ |
| 956 | int 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 */ |
| 976 | int 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 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 985 | /* *********************************************************** |
| 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 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 997 | /* API: Send Create PDP Context Request */ |
| 998 | extern int gtp_create_context_req(struct gsn_t *gsn, struct pdp_t *pdp, |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 999 | void *cbp) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1000 | union gtp_packet packet; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1001 | int length = get_default_gtp(pdp->version, GTP_CREATE_PDP_REQ, &packet); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1002 | struct pdp_t *linked_pdp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1003 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1004 | /* 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) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1018 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1019 | sizeof(pdp->qos_req0), pdp->qos_req0); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1020 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1021 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1022 | 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 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1027 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1028 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 1029 | gsn->restart_counter); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1030 | |
| 1031 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1032 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_SELECTION_MODE, |
| 1033 | pdp->selmode); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1034 | |
| 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); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1045 | |
| 1046 | if (!pdp->teic_confirmed) |
| 1047 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C, |
| 1048 | pdp->teic_own); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, |
| 1052 | pdp->nsapi); |
| 1053 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1054 | |
| 1055 | if (pdp->version == 1) { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1056 | if (pdp->secondary) /* Secondary PDP Context Activation Procedure */ |
| 1057 | gtpie_tv1(packet.gtp1l.p, &length, GTP_MAX, GTPIE_NSAPI, |
| 1058 | linked_pdp->nsapi); |
| 1059 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1060 | 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 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1070 | 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 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1074 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1075 | 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); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1083 | |
| 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); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1088 | |
| 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); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1092 | |
| 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 | |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1110 | /* TODO hisaddr0 */ |
| 1111 | gtp_req(gsn, pdp->version, pdp, &packet, length, &pdp->hisaddr0, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1112 | |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1116 | /* API: Application response to context indication */ |
| 1117 | int 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 */ |
| 1130 | int gtp_set_cb_create_context_ind(struct gsn_t *gsn, |
| 1131 | int (*cb_create_context_ind) (struct pdp_t* pdp)) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1132 | { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1133 | gsn->cb_create_context_ind = cb_create_context_ind; |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | |
| 1138 | /* Send Create PDP Context Response */ |
| 1139 | int gtp_create_pdp_resp(struct gsn_t *gsn, int version, struct pdp_t *pdp, |
| 1140 | uint8_t cause) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1141 | union gtp_packet packet; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1142 | int length = get_default_gtp(version, GTP_CREATE_PDP_RSP, &packet); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1143 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1144 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1145 | |
| 1146 | if (cause == GTPCAUSE_ACC_REQ) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1147 | |
| 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, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1153 | pdp->reorder); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1154 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1155 | gsn->restart_counter); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1156 | |
| 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 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1171 | /* TODO: We use teic_own as charging ID */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1172 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_CHARGING_ID, |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1173 | pdp->teic_own); |
| 1174 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1175 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1176 | pdp->eua.l, pdp->eua.v); |
| 1177 | |
| 1178 | if (pdp->pco_neg.l) { /* Optional PCO */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1179 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_PCO, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1180 | pdp->pco_neg.l, pdp->pco_neg.v); |
| 1181 | } |
| 1182 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1183 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1184 | pdp->gsnlc.l, pdp->gsnlc.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1185 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1186 | pdp->gsnlu.l, pdp->gsnlu.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1187 | |
| 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 */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1195 | return gtp_resp(version, gsn, pdp, &packet, length, &pdp->sa_peer, |
| 1196 | pdp->fd, pdp->seq, pdp->tid); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /* Handle Create PDP Context Request */ |
| 1200 | int gtp_create_pdp_ind(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1201 | struct sockaddr_in *peer, int fd, |
| 1202 | void *pack, unsigned len) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1203 | struct pdp_t *pdp, *pdp_old; |
| 1204 | struct pdp_t pdp_buf; |
| 1205 | union gtpie_member* ie[GTPIE_SIZE]; |
| 1206 | uint8_t recovery; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1207 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1208 | uint16_t seq = get_seq(pack); |
| 1209 | int hlen = get_hlen(pack); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1210 | uint8_t linked_nsapi = 0; |
| 1211 | struct pdp_t *linked_pdp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1212 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1213 | if(!gtp_dublicate(gsn, version, peer, seq)) return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1214 | |
| 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; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1223 | pdp->seq = seq; |
| 1224 | pdp->sa_peer = *peer; |
| 1225 | pdp->fd = fd; |
| 1226 | pdp->version = version; |
| 1227 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1228 | /* Decode information elements */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1229 | if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1230 | 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 |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1236 | return gtp_create_pdp_resp(gsn, version, pdp, GTPCAUSE_INVALID_MESSAGE); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1239 | 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 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1276 | 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 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1284 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1285 | |
| 1286 | if ((version == 1) && (!linked_pdp)) { |
| 1287 | /* Not Secondary PDP Context Activation Procedure */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1288 | /* 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 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1297 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1298 | /* Recovery (optional) */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1299 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
| 1300 | /* TODO: Handle received recovery IE */ |
| 1301 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1302 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1303 | /* Selection mode (conditional) */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1304 | 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 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1315 | 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 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1331 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1332 | |
| 1333 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1334 | 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) */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1345 | 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 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1353 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1356 | /* 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 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1366 | /* Charging Characteriatics (optional) */ |
| 1367 | /* Trace reference (optional) */ |
| 1368 | /* Trace type (optional) */ |
| 1369 | /* Charging Characteriatics (optional) */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1370 | |
| 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, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1374 | &pdp->eua.v, sizeof(pdp->eua.v))) { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1375 | 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, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1384 | &pdp->apn_req.v, sizeof(pdp->apn_req.v))) { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1385 | 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 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1398 | /* SGSN address for signalling (mandatory) */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1399 | 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1404 | return gtp_create_pdp_resp(gsn, version, pdp, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1405 | GTPCAUSE_MAN_IE_MISSING); |
| 1406 | } |
| 1407 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1408 | /* SGSN address for user traffic (mandatory) */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1409 | 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1414 | return gtp_create_pdp_resp(gsn, version, pdp, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1415 | GTPCAUSE_MAN_IE_MISSING); |
| 1416 | } |
| 1417 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1418 | 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 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1430 | 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 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1445 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1446 | /* Trigger ID */ |
| 1447 | /* OMC identity */ |
| 1448 | } |
| 1449 | |
| 1450 | /* Initialize our own IP addresses */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1451 | in_addr2gsna(&pdp->gsnlc, &gsn->gsnc); |
| 1452 | in_addr2gsna(&pdp->gsnlu, &gsn->gsnu); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1453 | |
jjako | 2e840a3 | 2003-01-28 16:05:18 +0000 | [diff] [blame] | 1454 | if (GTP_DEBUG) printf("gtp_create_pdp_ind: Before pdp_tidget\n"); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1455 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1456 | if (!pdp_getimsi(&pdp_old, pdp->imsi, pdp->nsapi)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1457 | /* Found old pdp with same tid. Now the voodoo begins! */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1458 | /* 09.60 / 29.060 allows create on existing context to "steal" */ |
| 1459 | /* the context which was allready established */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1460 | /* We check that the APN, selection mode and MSISDN is the same */ |
jjako | 2e840a3 | 2003-01-28 16:05:18 +0000 | [diff] [blame] | 1461 | if (GTP_DEBUG) printf("gtp_create_pdp_ind: Old context found\n"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1462 | if ((pdp->apn_req.l == pdp_old->apn_req.l) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1463 | && (!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 */ |
jjako | 2e840a3 | 2003-01-28 16:05:18 +0000 | [diff] [blame] | 1473 | |
| 1474 | if (GTP_DEBUG) printf("gtp_create_pdp_ind: Old context found\n"); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1475 | |
| 1476 | /* Copy remote flow label */ |
| 1477 | pdp_old->flru = pdp->flru; |
| 1478 | pdp_old->flrc = pdp->flrc; |
| 1479 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1480 | /* Copy remote tei */ |
| 1481 | pdp_old->teid_gn = pdp->teid_gn; |
| 1482 | pdp_old->teic_gn = pdp->teic_gn; |
| 1483 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1484 | /* 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); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1489 | |
| 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 */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1497 | pdp = pdp_old; |
| 1498 | |
| 1499 | /* Confirm to peer that things were "successful" */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1500 | return gtp_create_pdp_resp(gsn, version, pdp, GTPCAUSE_ACC_REQ); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1501 | } |
| 1502 | else { /* This is not the same PDP context. Delete the old one. */ |
jjako | 2e840a3 | 2003-01-28 16:05:18 +0000 | [diff] [blame] | 1503 | |
| 1504 | if (GTP_DEBUG) printf("gtp_create_pdp_ind: Deleting old context\n"); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1505 | |
| 1506 | if (gsn->cb_delete_context) gsn->cb_delete_context(pdp_old); |
| 1507 | pdp_freepdp(pdp_old); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1508 | |
jjako | 2e840a3 | 2003-01-28 16:05:18 +0000 | [diff] [blame] | 1509 | if (GTP_DEBUG) printf("gtp_create_pdp_ind: Deleted...\n"); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1513 | pdp_newpdp(&pdp, pdp->imsi, pdp->nsapi, pdp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1514 | |
| 1515 | /* Callback function to validata login */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1516 | if (gsn->cb_create_context_ind !=0) |
| 1517 | return gsn->cb_create_context_ind(pdp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1518 | else { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1519 | 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); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | /* Handle Create PDP Context Response */ |
| 1527 | int gtp_create_pdp_conf(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1528 | struct sockaddr_in *peer, |
| 1529 | void *pack, unsigned len) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1530 | struct pdp_t *pdp; |
| 1531 | union gtpie_member *ie[GTPIE_SIZE]; |
| 1532 | uint8_t cause, recovery; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1533 | void *cbp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1534 | uint8_t type = 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1535 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1536 | |
| 1537 | /* Remove packet from queue */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1538 | if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1539 | |
| 1540 | /* Find the context in question */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1541 | if (pdp_getgtp1(&pdp, get_tei(pack))) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1542 | gsn->err_unknownpdp++; |
| 1543 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 1544 | "Unknown PDP context"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1545 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1546 | return EOF; |
| 1547 | } |
| 1548 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1549 | /* Register that we have received a valid teic from GGSN */ |
| 1550 | pdp->teic_confirmed = 1; |
| 1551 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1552 | /* Decode information elements */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1553 | if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1554 | gsn->invalid++; |
| 1555 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 1556 | "Invalid message format"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1557 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1558 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1559 | pdp_freepdp(pdp); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1560 | 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1568 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1569 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1570 | pdp_freepdp(pdp); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1571 | 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))) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /* Check all conditional information elements */ |
| 1585 | if (GTPCAUSE_ACC_REQ == cause) { |
| 1586 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1587 | 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); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1594 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1595 | pdp_freepdp(pdp); */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1596 | return EOF; |
| 1597 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1598 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1599 | |
| 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1604 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1605 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1606 | pdp_freepdp(pdp); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1607 | return EOF; |
| 1608 | } |
| 1609 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1610 | 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); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1616 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1617 | pdp_freepdp(pdp); */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1618 | return EOF; |
| 1619 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1620 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1621 | 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); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1626 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1627 | pdp_freepdp(pdp); */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1628 | 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); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1638 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1639 | pdp_freepdp(pdp); */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1640 | 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); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1648 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1649 | pdp_freepdp(pdp); */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1650 | return EOF; |
| 1651 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1652 | } |
| 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1658 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1659 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1660 | pdp_freepdp(pdp); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1661 | } |
| 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1668 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1669 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1670 | pdp_freepdp(pdp); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1671 | 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1679 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1680 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1681 | pdp_freepdp(pdp); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1682 | 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1690 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1691 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1692 | pdp_freepdp(pdp); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1693 | return EOF; |
| 1694 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1695 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1696 | 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); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 1703 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1704 | pdp_freepdp(pdp); */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1705 | return EOF; |
| 1706 | } |
| 1707 | } |
jjako | ccc564f | 2003-10-25 15:40:48 +0000 | [diff] [blame] | 1708 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | if (gsn->cb_conf) gsn->cb_conf(type, cause, pdp, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1712 | |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1716 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1717 | /* API: Send Update PDP Context Request */ |
| 1718 | int 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); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1722 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1723 | if (pdp->version == 0) |
| 1724 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 1725 | sizeof(pdp->qos_req0), pdp->qos_req0); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1726 | |
| 1727 | /* Include IMSI if updating with unknown teic_gn */ |
| 1728 | if ((pdp->version == 1) && (!pdp->teic_gn)) |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1729 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_IMSI, |
| 1730 | sizeof(pdp->imsi), (uint8_t*) &pdp->imsi); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1731 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1732 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1733 | gsn->restart_counter); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1734 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1735 | if (pdp->version == 0) { |
| 1736 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1737 | pdp->fllu); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1738 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1739 | pdp->fllc); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1740 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1741 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1742 | if (pdp->version == 1) { |
| 1743 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 1744 | pdp->teid_own); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1745 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1746 | if (!pdp->teic_confirmed) |
| 1747 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C, |
| 1748 | pdp->teic_own); |
| 1749 | } |
| 1750 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1751 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, |
| 1752 | pdp->nsapi); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1753 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1754 | /* TODO |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1755 | 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 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1760 | /* TODO if ggsn update message |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1761 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
| 1762 | pdp->eua.l, pdp->eua.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1763 | */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1764 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1765 | 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); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1769 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1770 | if (pdp->version == 1) |
| 1771 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 1772 | pdp->qos_req.l, pdp->qos_req.v); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1773 | |
| 1774 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1775 | 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; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1792 | |
| 1793 | /* Send Update PDP Context Response */ |
| 1794 | int 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); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1823 | |
| 1824 | if (!pdp->teic_confirmed) |
| 1825 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C, |
| 1826 | pdp->teic_own); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1827 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1828 | |
| 1829 | /* TODO we use teid_own as charging ID address */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1830 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_CHARGING_ID, |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1831 | pdp->teid_own); |
| 1832 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1833 | /* If ggsn |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1834 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
| 1835 | pdp->eua.l, pdp->eua.v); */ |
| 1836 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1837 | 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); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1841 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1842 | if (version == 1) |
| 1843 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 1844 | pdp->qos_neg.l, pdp->qos_neg.v); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1845 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1846 | /* TODO: Charging gateway address */ |
| 1847 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1848 | |
| 1849 | return gtp_resp(version, gsn, pdp, &packet, length, peer, |
| 1850 | fd, get_seq(pack), get_tid(pack)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1854 | /* Handle Update PDP Context Request */ |
| 1855 | int gtp_update_pdp_ind(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1856 | struct sockaddr_in *peer, int fd, |
| 1857 | void *pack, unsigned len) { |
| 1858 | struct pdp_t *pdp; |
| 1859 | struct pdp_t pdp_backup; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1860 | union gtpie_member* ie[GTPIE_SIZE]; |
| 1861 | uint8_t recovery; |
| 1862 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1863 | uint16_t seq = get_seq(pack); |
| 1864 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1865 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1866 | uint64_t imsi; |
| 1867 | uint8_t nsapi; |
| 1868 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1869 | /* Is this a dublicate ? */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1870 | if(!gtp_dublicate(gsn, version, peer, seq)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1871 | return 0; /* We allready send of response once */ |
| 1872 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1873 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1874 | |
| 1875 | /* Decode information elements */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1876 | if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1877 | 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 |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1883 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, |
| 1884 | NULL, GTPCAUSE_INVALID_MESSAGE); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1887 | /* 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; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1894 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1895 | /* 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 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1903 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1904 | 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 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1913 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1914 | /* 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 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1940 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1941 | /* 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) */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1957 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
| 1958 | /* TODO: Handle received recovery IE */ |
| 1959 | } |
| 1960 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1961 | 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 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1992 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1993 | /* TEIC (conditional) */ |
| 1994 | /* If TEIC is not included it means that we have allready received it */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1995 | /* 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 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1999 | /* 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))) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2016 | gsn->missing++; |
| 2017 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2018 | "Missing mandatory information field"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2019 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2020 | return gtp_update_pdp_resp(gsn, version, pdp, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2021 | GTPCAUSE_MAN_IE_MISSING); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2022 | } */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2023 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2024 | |
| 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))) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2029 | gsn->missing++; |
| 2030 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2031 | "Missing mandatory information field"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2032 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2033 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2034 | GTPCAUSE_MAN_IE_MISSING); |
| 2035 | } |
| 2036 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2037 | /* 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))) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2040 | gsn->missing++; |
| 2041 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2042 | "Missing mandatory information field"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2043 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2044 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2045 | GTPCAUSE_MAN_IE_MISSING); |
| 2046 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2047 | |
| 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 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2059 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2060 | /* 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 */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2068 | /* Confirm to peer that things were "successful" */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2069 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2070 | GTPCAUSE_ACC_REQ); |
| 2071 | } |
| 2072 | |
| 2073 | |
| 2074 | /* Handle Update PDP Context Response */ |
| 2075 | int 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; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2081 | void *cbp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2082 | uint8_t type = 0; |
| 2083 | |
| 2084 | /* Remove packet from queue */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2085 | if (gtp_conf(gsn, 0, peer, pack, len, &type, &cbp)) return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2086 | |
| 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2092 | if (gsn->cb_conf) gsn->cb_conf(type, cause, NULL, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2093 | return EOF; |
| 2094 | } |
| 2095 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2096 | /* Register that we have received a valid teic from GGSN */ |
| 2097 | pdp->teic_confirmed = 1; |
| 2098 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2099 | /* Decode information elements */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2100 | if (gtpie_decaps(ie, 0, pack+GTP0_HEADER_SIZE, len-GTP0_HEADER_SIZE)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2101 | gsn->invalid++; |
| 2102 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2103 | "Invalid message format"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2104 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 2105 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 2106 | pdp_freepdp(pdp); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2107 | 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2115 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 2116 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 2117 | pdp_freepdp(pdp); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2118 | 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) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2128 | if (gsn->cb_conf) gsn->cb_conf(type, cause, pdp, cbp); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 2129 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 2130 | pdp_freepdp(pdp); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2131 | 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2146 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp); |
jjako | 0b076a3 | 2003-10-25 15:59:31 +0000 | [diff] [blame] | 2147 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 2148 | pdp_freepdp(pdp); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2149 | 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 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2166 | if (gsn->cb_conf) gsn->cb_conf(type, cause, pdp, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2167 | return 0; /* Succes */ |
| 2168 | } |
| 2169 | } |
| 2170 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2171 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2172 | /* API: Send Delete PDP Context Request */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2173 | int gtp_delete_context_req(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp, |
| 2174 | int teardown) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2175 | union gtp_packet packet; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2176 | int length = get_default_gtp(pdp->version, GTP_DELETE_PDP_REQ, &packet); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2177 | struct in_addr addr; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2178 | struct pdp_t *linked_pdp; |
| 2179 | struct pdp_t *secondary_pdp; |
| 2180 | int n; |
| 2181 | int count = 0; |
| 2182 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2183 | 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 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2188 | |
| 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 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2204 | if (pdp->version == 1) { |
| 2205 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, |
| 2206 | pdp->nsapi); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2207 | |
| 2208 | if (teardown) |
| 2209 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_TEARDOWN, |
| 2210 | 0xff); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2211 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2212 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2213 | gtp_req(gsn, pdp->version, pdp, &packet, length, &addr, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2214 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2215 | 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 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2243 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2244 | /* Send Delete PDP Context Response */ |
| 2245 | int gtp_delete_pdp_resp(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2246 | struct sockaddr_in *peer, int fd, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2247 | void *pack, unsigned len, |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2248 | struct pdp_t *pdp, struct pdp_t *linked_pdp, |
| 2249 | uint8_t cause, int teardown) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2250 | { |
| 2251 | union gtp_packet packet; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2252 | struct pdp_t *secondary_pdp; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2253 | int length = get_default_gtp(version, GTP_DELETE_PDP_RSP, &packet); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2254 | int n; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2255 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2256 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2257 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2258 | gtp_resp(version, gsn, pdp, &packet, length, peer, fd, |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2259 | get_seq(pack), get_tid(pack)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2260 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2261 | 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) */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2289 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2290 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | /* Handle Delete PDP Context Request */ |
| 2294 | int gtp_delete_pdp_ind(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2295 | struct sockaddr_in *peer, int fd, |
| 2296 | void *pack, unsigned len) { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2297 | struct pdp_t *pdp = NULL; |
| 2298 | struct pdp_t *linked_pdp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2299 | union gtpie_member* ie[GTPIE_SIZE]; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2300 | |
| 2301 | uint16_t seq = get_seq(pack); |
| 2302 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2303 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2304 | uint8_t nsapi; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2305 | uint8_t teardown = 0; |
| 2306 | int n; |
| 2307 | int count = 0; |
| 2308 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2309 | /* Is this a dublicate ? */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2310 | if(!gtp_dublicate(gsn, version, peer, seq)) { |
| 2311 | return 0; /* We allready send off response once */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2314 | /* Find the linked context in question */ |
| 2315 | if (pdp_getgtp1(&linked_pdp, get_tei(pack))) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2316 | gsn->err_unknownpdp++; |
| 2317 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2318 | "Unknown PDP context"); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2319 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, NULL, NULL, |
| 2320 | GTPCAUSE_NON_EXIST, teardown); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2321 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2322 | |
| 2323 | /* If version 0 this is also the secondary context */ |
| 2324 | if (version == 0) |
| 2325 | pdp = linked_pdp; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2326 | |
| 2327 | /* Decode information elements */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2328 | if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2329 | 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 |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2335 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, NULL, NULL, |
| 2336 | GTPCAUSE_INVALID_MESSAGE, teardown); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2337 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2338 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2339 | 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"); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2345 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, NULL, NULL, |
| 2346 | GTPCAUSE_MAN_IE_MISSING, teardown); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2347 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2348 | |
| 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 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2357 | |
| 2358 | /* Teardown (conditional) */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2359 | 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 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2367 | } |
| 2368 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2369 | |
| 2370 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2371 | pdp, linked_pdp, GTPCAUSE_ACC_REQ, teardown); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | |
| 2375 | /* Handle Delete PDP Context Response */ |
| 2376 | int gtp_delete_pdp_conf(struct gsn_t *gsn, int version, |
| 2377 | struct sockaddr_in *peer, |
| 2378 | void *pack, unsigned len) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2379 | union gtpie_member *ie[GTPIE_SIZE]; |
| 2380 | uint8_t cause; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2381 | void *cbp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2382 | uint8_t type = 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2383 | int hlen = get_hlen(pack); |
| 2384 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2385 | /* Remove packet from queue */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2386 | if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2387 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2388 | /* Decode information elements */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2389 | if (gtpie_decaps(ie, version, pack+hlen, len-hlen)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2390 | gsn->invalid++; |
| 2391 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2392 | "Invalid message format"); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2393 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2394 | return EOF; |
| 2395 | } |
| 2396 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2397 | /* Extract cause value (mandatory) */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2398 | 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"); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2402 | if (gsn->cb_conf) gsn->cb_conf(type, EOF, NULL, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2403 | return EOF; |
| 2404 | } |
| 2405 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2406 | /* Check the cause value (again) */ |
| 2407 | if ((GTPCAUSE_ACC_REQ != cause) && (GTPCAUSE_NON_EXIST != cause)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2408 | gsn->err_cause++; |
| 2409 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2410 | "Unexpected cause value received: %d", cause); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2411 | if (gsn->cb_conf) gsn->cb_conf(type, cause, NULL, cbp); |
| 2412 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2413 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2414 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2415 | /* Callback function to notify application */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2416 | if (gsn->cb_conf) gsn->cb_conf(type, cause, NULL, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2417 | |
| 2418 | return 0; |
| 2419 | } |
| 2420 | |
| 2421 | /* Send Error Indication (response to a GPDU message */ |
| 2422 | int gtp_error_ind_resp(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2423 | struct sockaddr_in *peer, int fd, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2424 | void *pack, unsigned len) |
| 2425 | { |
| 2426 | union gtp_packet packet; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2427 | int length = get_default_gtp(version, GTP_ERROR, &packet); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2428 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2429 | return gtp_resp(version, gsn, NULL, &packet, length, peer, fd, |
| 2430 | get_seq(pack), get_tid(pack)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | /* Handle Error Indication */ |
| 2434 | int 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 | |
| 2456 | int gtp_gpdu_ind(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2457 | struct sockaddr_in *peer, int fd, |
| 2458 | void *pack, unsigned len) { |
| 2459 | |
| 2460 | int hlen = GTP1_HEADER_SIZE_SHORT; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2461 | |
| 2462 | /* Need to include code to verify packet src and dest addresses */ |
| 2463 | struct pdp_t *pdp; |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2464 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2465 | 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 | |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2493 | /* 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2498 | return gtp_error_ind_resp(gsn, version, peer, fd, pack, len); |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2499 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2500 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2501 | /* Callback function */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2502 | if (gsn->cb_data_ind !=0) |
| 2503 | return gsn->cb_data_ind(pdp, pack+hlen, len-hlen); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2504 | |
| 2505 | return 0; |
| 2506 | } |
| 2507 | |
| 2508 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2509 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2510 | /* 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! */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2515 | int gtp_decaps0(struct gsn_t *gsn) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2516 | { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2517 | unsigned char buffer[PACKET_MAX]; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2518 | struct sockaddr_in peer; |
| 2519 | int peerlen; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2520 | int status; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2521 | struct gtp0_header *pheader; |
| 2522 | int version = 0; /* GTP version should be determined from header!*/ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2523 | int fd = gsn->fd0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2524 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2525 | /* 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...*/ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2528 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2529 | while (1) { /* Loop until no more to read */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2530 | if (fcntl(gsn->fd0, F_SETFL, O_NONBLOCK)) { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2531 | gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()"); |
| 2532 | return -1; |
| 2533 | } |
| 2534 | peerlen = sizeof(peer); |
| 2535 | if ((status = |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2536 | recvfrom(gsn->fd0, buffer, sizeof(buffer), 0, |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2537 | (struct sockaddr *) &peer, &peerlen)) < 0 ) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2538 | if (errno == EAGAIN) return 0; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2539 | gsn->err_readfrom++; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2540 | 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"); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2541 | return -1; |
| 2542 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2543 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2544 | /* 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 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2552 | pheader = (struct gtp0_header *) (buffer); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2553 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2554 | /* 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 */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2559 | if (((pheader->flags & 0xe0) > 0x00)) { |
| 2560 | gsn->unsup++; |
| 2561 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status, |
| 2562 | "Unsupported GTP version"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2563 | gtp_unsup_req(gsn, 0, &peer, gsn->fd0, buffer, status); /* 29.60: 11.1.1 */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2564 | continue; |
| 2565 | } |
| 2566 | |
| 2567 | /* Check length of gtp0 packet */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2568 | if (status < GTP0_HEADER_SIZE) { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2569 | 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 | } |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2574 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2575 | /* 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 | |
| 2647 | int gtp_decaps1c(struct gsn_t *gsn) |
| 2648 | { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2649 | unsigned char buffer[PACKET_MAX]; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2650 | struct sockaddr_in peer; |
| 2651 | int peerlen; |
| 2652 | int status; |
| 2653 | struct gtp1_header_short *pheader; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2654 | int version = 1; /* TODO GTP version should be determined from header!*/ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2655 | 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 */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2662 | if (fcntl(fd, F_SETFL, O_NONBLOCK)) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2663 | gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()"); |
| 2664 | return -1; |
| 2665 | } |
| 2666 | peerlen = sizeof(peer); |
| 2667 | if ((status = |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2668 | recvfrom(fd, buffer, sizeof(buffer), 0, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2669 | (struct sockaddr *) &peer, &peerlen)) < 0 ) { |
| 2670 | if (errno == EAGAIN) return 0; |
| 2671 | gsn->err_readfrom++; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2672 | 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"); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2673 | 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"); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2691 | gtp_unsup_req(gsn, version, &peer, fd, buffer, status); |
| 2692 | /*29.60: 11.1.1*/ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2693 | 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 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2731 | /* 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 | |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2743 | 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 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2763 | switch (pheader->type) { |
| 2764 | case GTP_ECHO_REQ: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2765 | gtp_echo_ind(gsn, version, &peer, fd, buffer, status); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2766 | break; |
| 2767 | case GTP_ECHO_RSP: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2768 | gtp_echo_conf(gsn, version, &peer, buffer, status); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2769 | break; |
| 2770 | case GTP_NOT_SUPPORTED: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2771 | gtp_unsup_ind(gsn, &peer, buffer, status); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2772 | break; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2773 | case GTP_SUPP_EXT_HEADER: |
| 2774 | gtp_extheader_ind(gsn, &peer, buffer, status); |
| 2775 | break; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2776 | case GTP_CREATE_PDP_REQ: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2777 | gtp_create_pdp_ind(gsn, version, &peer, fd, buffer, status); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2778 | break; |
| 2779 | case GTP_CREATE_PDP_RSP: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2780 | gtp_create_pdp_conf(gsn, version, &peer, buffer, status); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2781 | break; |
| 2782 | case GTP_UPDATE_PDP_REQ: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2783 | gtp_update_pdp_ind(gsn, version, &peer, fd, buffer, status); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2784 | break; |
| 2785 | case GTP_UPDATE_PDP_RSP: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2786 | gtp_update_pdp_conf(gsn, version, &peer, buffer, status); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2787 | break; |
| 2788 | case GTP_DELETE_PDP_REQ: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2789 | gtp_delete_pdp_ind(gsn, version, &peer, fd, buffer, status); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2790 | break; |
| 2791 | case GTP_DELETE_PDP_RSP: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2792 | gtp_delete_pdp_conf(gsn, version, &peer, buffer, status); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2793 | break; |
| 2794 | case GTP_ERROR: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2795 | gtp_error_ind_conf(gsn, version, &peer, buffer, status); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2796 | break; |
| 2797 | default: |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2798 | gsn->unknown++; |
| 2799 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, status, |
| 2800 | "Unknown GTP message type received"); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2801 | break; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2802 | } |
| 2803 | } |
| 2804 | } |
| 2805 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2806 | int gtp_decaps1u(struct gsn_t *gsn) |
| 2807 | { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2808 | unsigned char buffer[PACKET_MAX]; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2809 | 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 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2888 | |
| 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 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2900 | |
| 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; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2908 | case GTP_SUPP_EXT_HEADER: |
| 2909 | gtp_extheader_ind(gsn, &peer, buffer, status); |
| 2910 | break; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2911 | 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 | |
| 2927 | int gtp_data_req(struct gsn_t *gsn, struct pdp_t* pdp, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2928 | void *pack, unsigned len) |
| 2929 | { |
| 2930 | union gtp_packet packet; |
| 2931 | struct sockaddr_in addr; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2932 | int fd; |
| 2933 | int length; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2934 | |
| 2935 | memset(&addr, 0, sizeof(addr)); |
| 2936 | addr.sin_family = AF_INET; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2937 | memcpy(&addr.sin_addr, pdp->gsnru.v,pdp->gsnru.l); /* TODO range check */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2938 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2939 | 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); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2950 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2951 | 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; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2956 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2957 | 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; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2964 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2965 | 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++); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2969 | packet.gtp1l.h.tei = hton32(pdp->teid_gn); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2970 | |
| 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)) { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2986 | gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()"); |
| 2987 | return -1; |
| 2988 | } |
| 2989 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2990 | if (sendto(fd, &packet, length, 0, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2991 | (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 2992 | gsn->err_sendto++; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2993 | 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)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2994 | return EOF; |
| 2995 | } |
| 2996 | return 0; |
| 2997 | } |
| 2998 | |
| 2999 | |
| 3000 | /* *********************************************************** |
| 3001 | * Conversion functions |
| 3002 | *************************************************************/ |
| 3003 | |
| 3004 | int 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 | |
| 3025 | int 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 | |
| 3039 | int 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 | |
| 3048 | int 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 | |
| 3055 | int 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 | |